1

I have a two monitor setup on Ubuntu 18.04 with an Nvidia GeForce GTX 1080 graphics card. The first monitor is 2k and the second is 4k. I have the 4k monitor scaled down to 2k using the Nvidia X Server Settings app, and it works fine when first booted up. But when I shut off both monitors, or just the second, gnome collapses the contents of the second monitor onto the first. When I then turn both monitors on, it moves the contents of the second monitor back to the second screen ... with the wrong scaling on the second monitor now and a barely readable tiny font.

I have an xrandr command that fixes the problem (until I power cycle the monitors again):

xrandr --fb 5120x1440 \
  --output HDMI-0 --scale 1x1 --mode 2560x1440 --pos 0x0 --panning 2560x1440+0+0 \
  --output HDMI-1 --scale .6666x.6666 --mode 3840x2160 --pos 2560x0 --panning 2560x1440+2560+0

Currently I run this command every time I turn the monitors on. Is there a way to automatically run it whenever the second monitor goes from off to on?

Or is there another solution for the problem of a second monitor changing its settings when cycled on-off-on?

3
  • I stumbled across this: github.com/jceb/srandrd. I'm still searching for a bash solution though... Sep 15, 2018 at 2:50
  • Thanks for the suggestion @WinEunuuchs2Unix, but I am unable to make install srandrd. I just get one undocumented missing dependency after another.
    – Mori
    Sep 15, 2018 at 4:57
  • Others have had problems with srandrd I just read. So I've written an answer which might help you get started. Sep 16, 2018 at 17:23

1 Answer 1

2

This is a work-in-progress script I'm developing for a package to control monitor brightness and gamma based on sunrise/sunset times.

When closing the lid of the laptop, xrandr resets all external monitors to full brightness. The script below detects xrandr changes to /sys/class/drm/?/status file using inotify which is a more efficient polling method than sleeping every second.

The script below contains credits to original authors and portions are commented out that may be deleted in the future or changed.

Use ll /sys/class/drm/*/status to discover your monitor card name(s). Then replace MONITOR= below with the appropriate name.

Bash script

#!/bin/bash

# NAME: monitory-eyesome.sh
# PATH: /usr/lib/bin
# DESC: Instantly adjust display brightness when xrandr reconfigures monitors
#       and resets them to full brightness.

# CALL: /etc/cron.d calls this script during boot.
#       Called from command line for testing/debugging.

# DATE: Sepetmber ??, 2018.

# PARM: No parameters yet, but $1 will be /sys/class/drm/<MONITOR>/status
#       in the future. ie $1 = <MONITOR>

# source eyesome-src.sh # Common code for eyesome___.sh bash scripts

# Must have the inotify-tools package.
command -v inotifywait >/dev/null 2>&1 || { echo >&2 \
        "inotify-tools package required but it is not installed.  Aborting."; \
        exit 2; }

# Copied from: https://bbs.archlinux.org/viewtopic.php?id=171655
#inspired of: 
#   http://unix.stackexchange.com/questions/4489/a-tool-for-automatically-applying-randr-configuration-when-external-display-is-p
#   http://ozlabs.org/~jk/docs/mergefb/
#   http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/181543#181543

export MONITOR="/sys/class/drm/card1-DP-1/status"
echo "$0: $(date) Monitoring: $MONITOR" > /tmp/monitor-eyesome.sh
while inotifywait -e modify,create,delete,open,close,close_write,access \
        "$MONITOR";

dmode="$(cat "$MONITOR")"

do
    echo "$0: $(date) $dmode" >> /tmp/monitor-eyesome.sh
#    if [ "${dmode}" = disconnected ]; then
#         /usr/bin/xrandr --auto
#         echo "${dmode}"
#    elif [ "${dmode}" = connected ];then
#         /usr/bin/xrandr --output VGA1 --auto --right-of LVDS1
#         echo "${dmode}"
#    else /usr/bin/xrandr --auto
#         echo "${dmode}"
#    fi
done

Sample output when closing laptop lid

$ sudo ./monitor-eyesome.sh
Setting up watches.
Watches established.
/sys/class/drm/card1-DP-1/status OPEN 
Setting up watches.
Watches established.
/sys/class/drm/card1-DP-1/status OPEN 
Setting up watches.
Watches established.
/sys/class/drm/card1-DP-1/status OPEN 
Setting up watches.
Watches established.

Sample output of log file

$ cat /tmp/mon*
./monitor-eyesome.sh: Sun Sep 16 11:16:51 MDT 2018 Monitoring: /sys/class/drm/card1-DP-1/status
./monitor-eyesome.sh: Sun Sep 16 11:16:55 MDT 2018 connected
./monitor-eyesome.sh: Sun Sep 16 11:16:56 MDT 2018 connected
./monitor-eyesome.sh: Sun Sep 16 11:16:56 MDT 2018 connected

Summary

This script was created a few minutes ago (Sept 16 2018 @ 11:30am MST). I'll update it as the project progresses.

2
  • You're on the right track with /sys/class/drm/. +1 Sep 16, 2018 at 19:23
  • @SergiyKolodyazhnyy Thank you good sir! I wonder if you might comment/answer on my first github page for the above project. I posted a question on Stackoverflow just now: stackoverflow.com/questions/52357904/… Your thoughts would be most appreciated :) Sep 16, 2018 at 20:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .