Monitor Hot-plug Script

Background

I recently installed Pop!_OS from system76 (maker of my laptop) which happens to use a customized implementation of the Unity desktop. I’ve never been a fan of Ubuntu and certainly never cared for the Unity desktop. But I figured I would give it a try for a period of time before switching to something that is, in my opinion, more user-friendly and configurable (such as XFCE, Openbox, etc.). This isn’t about my opinion of Ubuntu, Unity or all efforts in Linux that go out of their way to mimic Microsoft Windows and Apple.

While it is a laptop that I use, I do, for the most part have an external monitor connected to it via an A/B switch for my work laptop and my personal one. One the functionalities lacking in Gnome3 (as well as other desktop environments) in general is the detection of when an external monitor is connected. Sure, you could use something like arandr the will generate a script for you and you can map it to a keyboard shortcut (binding), but that requires some manual effort that, frankly, shouldn’t be necessary. So, what I did was create a utility (chreli-monitor-hotplug) that simulates detecting when an external monitor is connected or disconnected, leveraging xrandr. The script is written in Perl and loops continuously (sleeping for 5 seconds) parsing the output of xrandr to see what’s connected, primary, etc. It’s not elegant, as I’m sure there are other like scripts or applications out there that can do the same thing better, but this gets the job done (for me any way).

Requirements

The following are required for chreli-monitor-hotplug:

  • x11-xserver-utils (needed for xrandr)
  • perl

Installation

To install, download chreli-monitor-hotplug-1.0.20171103.deb and execute the following command:

Disclaimer: This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

sudo /usr/bin/dpkg -i chreli-monitor-hotplug-1.0.20171103.deb

Usage

Using GNOME Tweaks, navigate to Startup Applications, click the + and in dialog, search for ChrEli.

Select ChrEli Monitor Hotplug and click the Add button.

Alternatively, you could just copy /usr/share/applications/chreli-monitor-hotplug.desktop to ~/.config/autostart.

The above will result in chreli-monitor-hotplug auto-starting on next restart (or re-login).

Configuration

Upon initial execution of chreli-monitor-hotplug, it will create and set defaults in ~/.config/chreli-monitor-hotplug/chreli-monitor-hotplug:

# chreli-monitor-hotplug 1.0.20171104
#
# configuration file for chreli-monitor-hotplug

# debug
# default: 0
chreli-monitor-hotplug.debug=0

# interval (in seconds) to check monitor changes
# default: 5
chreli-monitor-hotplug.interval=5

# dual
#  o flag to use both connected monitors
# default: 0
chreli-monitor-hotplug.usedual=0

# external.primary
#  o flag to make external primary monitor
# default: 1
chreli-monitor-hotplug.external.primary=1

Conclusion

That’s it. Nothing complex, but gets the job done and meets my needs (at least on my system). Given that every system and distro is different in how they behave, name devices, etc., chreli-monitor-hotplug may not work out-of-the-box on all system, but should for most.

And that’s all I have to say about that.

Leave a Reply