DisplayPort monitor not waking on resume

I run Manjaro i3 on my desktop with both a 4K monitor connected via DisplayPort and a 1080p monitor connected via HDMI. When I suspend my machine, the DisplayPort monitor shuts off and fails to correctly turn back on when the system resumes - everything is moved to the 1080p monitor and the 4K monitor remains off.

I've so far found two workarounds for this issue, the first is simply power-cycling the monitor before resuming the system and the second uses xrandr. Neither of these is a perfect solution, but it's better than rebooting!

Background

I run Manjaro i3 on my desktop with both a 4K monitor (iiyama B2888UHSU) connected via DisplayPort and a 1080p monitor connected via HDMI. I don't think that the issue is GPU/kernel/driver specific, but for the record, my graphics card is a GTX 980 and I have the proprietary drivers installed from the Arch repos (version 375.26) on kernel version 4.9.

The problem:

When I suspend my machine, the DisplayPort monitor shuts off and fails to correctly turn back on when the system resumes - everything is moved to the 1080p monitor and the 4K monitor remains off. During the resume, the 4K monitor does actually turn on, but it fails to display anything and it soon displays "No input detected" and then turns off.

The solution:

So far I've found two separate solutions/workarounds to this issue, although I'm not entirely satisfied because both methods require my intervention.

1. Power-cycle the monitor

The first workaround I found was to turn the monitor off and then back on again just before waking my computer up. This suggests to me that the issue lays in the timing of various events as the computer resumes and negotiates the connection with the monitors. My experience here is that a DisplayPort connection tends to switch off entirely when the monitor is off, while HDMI connections (and VGA/DVI) tend to stay live. This behaviour manifests on Windows as well, with all of my windows being moved to one monitor when my 4K monitor is turned off and on again, despite the fact that I can turn my HDMI monitor off and still have the display active within the OS.

However, if resume the computer without power cycling the monitor, then the screen layout gets messed up and this method doesn't help. The result of this is that this method is useless if you want to do wake-on-LAN - you aren't there to turn the monitor on, so the screen gets messed up!

2. Use xrandr

My initial attempts to use xrandr went appallingly badly... If I tell it exactly what I want the monitor layout to be then it completely ignores me and does nothing. But if I run xrandr --output DP-4 --auto then my monitor setup gets really messed up and the secondary monitor is a subset of the primary monitor, and to make it even weirder, it scrolls around as I move the mouse.

There's a solution though! The issue with the second command was that it sets my secondary monitor to have panning across the whole "screen", which can actually be seen from the output of xrandr. To combat this, I just needed to be overly specific and set panning for each monitor to its dimensions and position. I ended up with the following script, which I have now set a keyboard shortcut for.

#!/bin/bash

# Start the primary monitor up again
xrandr --output DP-4 --auto

# Sort the layout out
xrandr --output HDMI-0 --pos 0x540 --panning 1920x1080+0+540 --output DP-4 --pos 1920x0 --primary --right-of HDMI-0 --panning 3840x2160+1920+0

There's still a slight issue here, since all of the workspaces end up on one monitor, but it's still a good rescue method. Ideally I'd like to either automatically save and reload the workspace layout, or find a way to get the monitor to either not disconnect or automatically reconnect cleanly.

Going forward

For now, my intention is to try to remember to power-cycle the monitor before I resume my computer, but I am safe in the knowledge that I can rescue my session with xrandr if I for some reason forget (or I do wake-on-LAN).