1

I have a computer with a bizarre bug. When I turn off the monitor and turn it back on, the screen stays blank. This only happens while logged in under X (but not at the Gdm screen). I've found a way to cause the screen to turn on:

xset dpms force off
xset dpms force on

How can I run these commands automatically when the monitor is turned on?

The monitor is connected over DisplayPort and reports EDID information. The operating system is Ubuntu 18.04.

A polling-based solution is not acceptable. Turning the monitor on should react without waiting so polling would require a very short interval, but it's an infrequent action so it doesn't warrant keeping the CPU hot enough to run a task at subsecond intervals. I'm looking for a trigger when a monitor is plugged in, to run in the X session.

  • The really interesting question is why this only happens when logged it, but not at the gdm screen. This suggests some configuration that is applied when logging in that your monitor does't like, and it might be possible to find out this and change it. – dirkt Jun 30 '19 at 6:41
  • @dirkt That's a separate problem and I didn't ask a question about it because it's pretty much guaranteed to be a video driver bug. The installation is a pretty basic Ubuntu (it's not my computer). – Gilles 'SO- stop being evil' Jun 30 '19 at 7:23
  • If it's a video driver bug, I'm sure the developers would like a bugreport so they can fix it. DPMS is really really old, so I'd be surprised to find bugs in there in the driver, and my bet would be on some unintended connection of circumstances which make it fail. In any case, one should eventually fix the cause, instead of doing a workaround. – dirkt Jun 30 '19 at 8:04
  • @dirkt I'll assist the computer owner to make a bug report (I haven't found an already-reported bug, but “black screen” is very generic and ”black screen“ + exact video card model turned up nothing relevant), but in the meantime the computer still needs to be usable. This has nothing to do with DPMS; it was just a way I stumbled into to force the screen to turn back on. – Gilles 'SO- stop being evil' Jun 30 '19 at 8:08
2

I have a solution using udev. It isn't robust, but good enough in my case.

Create a file /etc/udev/rules.d/99-monitor.rules (the exact file name doesn't matter as long as it only contains letters, digits, _ and - plus the .rules extension):

KERNEL=="card0", SUBSYSTEM=="drm", ACTION=="change", RUN+="/usr/local/sbin/monitor-change.udev.sh"

Create a file /usr/local/sbin/monitor-change.udev.sh and make it executable:

#!/bin/bash
set -e

start_logging () {
  exec >/var/log/monitor-change.log 2>&1
  echo
  set -x
  date
}

awaken_monitor () {
  # Hack: the monitor stays blank, but forcing it off then on in software
  # somehow turns it on.
  xset dpms force off
  xset dpms force on
}

detect_xorg () {
  tty=/dev/tty$(fgconsole)
  # Find Xorg running on the foreground console
  pids=$(fuser "$tty" 2>/dev/null)
  auth_regexp=' -auth ([^ ]*)'
  for pid in $pids; do
    args=$(ps -o args= -p $pid)
    if [[ "$args" = /usr/lib/xorg/Xorg* ]] &&
       [[ "$args" =~ $auth_regexp ]]
    then 
      # Hard-code DISPLAY=:0 because it's hard to detect
      export DISPLAY=":0" XAUTHORITY="${BASH_REMATCH[1]}"
      return 0
    fi
  done
  return 1
}

#start_logging
detect_xorg
awaken_monitor

It acts on any monitor change. This includes turning off the monitor as well as turning it on, and doesn't distinguish between multiple monitors, but this particular machine only ever has a single monitor so it's enough for me.

The udev script detects the X session (which normally runs on tty2 in this Ubuntu 18.04 default setup). There's only one user on this machine so I keep things simple. Note that the X invocation line assumes the Gdm way of invoking the X server; you may need to change this part if you don't use Gdm.

I would prefer a dbus-based solution but I don't know what event to react on.

| improve this answer | |
  • Note that "udev" and "X" don't mix on principle, even with your loop to look for "the" X server, because there might be several X servers with several displays. If you can assure that will never happen on your system, it will of course work. – dirkt Jun 30 '19 at 6:44
  • @dirkt I know, that's why my answer has all these caveats. I did make sure to hit the X server running on the foreground console, though. What I didn't bother to do was detect the display number. – Gilles 'SO- stop being evil' Jun 30 '19 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.