From feb815bc39ff4d6849458e607ecc444b383d743b Mon Sep 17 00:00:00 2001 From: Pavel Kachalouski Date: Thu, 4 May 2017 12:58:31 +0200 Subject: [PATCH] Added monitor switch files. --- monitor-switch/10-monitor-hotplug.rules | 1 + monitor-switch/monitor-hotplug.path | 6 ++++++ monitor-switch/monitor-hotplug.service | 9 +++++++++ monitor-switch/update-output.scala | 27 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+) create mode 100644 monitor-switch/10-monitor-hotplug.rules create mode 100644 monitor-switch/monitor-hotplug.path create mode 100644 monitor-switch/monitor-hotplug.service create mode 100755 monitor-switch/update-output.scala diff --git a/monitor-switch/10-monitor-hotplug.rules b/monitor-switch/10-monitor-hotplug.rules new file mode 100644 index 0000000..241bc99 --- /dev/null +++ b/monitor-switch/10-monitor-hotplug.rules @@ -0,0 +1 @@ +ACTION=="change", SUBSYSTEM=="drm", ENV{HOTPLUG}=="1", RUN+="/usr/bin/touch /tmp/monitor-hotplug-changed" diff --git a/monitor-switch/monitor-hotplug.path b/monitor-switch/monitor-hotplug.path new file mode 100644 index 0000000..fab221e --- /dev/null +++ b/monitor-switch/monitor-hotplug.path @@ -0,0 +1,6 @@ +[Path] +PathModified=/tmp/monitor-hotplug-changed +Unit=monitor-hotplug.service + +[Install] +WantedBy=multi-user.target diff --git a/monitor-switch/monitor-hotplug.service b/monitor-switch/monitor-hotplug.service new file mode 100644 index 0000000..556616c --- /dev/null +++ b/monitor-switch/monitor-hotplug.service @@ -0,0 +1,9 @@ +[Unit] +Description=Update Monitor outputs + +[Service] +Environment=DISPLAY=:0 +Environment=XAUTHORITY=/home/nnm/.Xauthority +Type=oneshot +ExecStart=/usr/bin/scala -nc -save /usr/local/bin/update-output.scala +User=nnm diff --git a/monitor-switch/update-output.scala b/monitor-switch/update-output.scala new file mode 100755 index 0000000..3f140e8 --- /dev/null +++ b/monitor-switch/update-output.scala @@ -0,0 +1,27 @@ +import scala.sys.process._ + +val eDP1 = List("--output", "eDP1", "--primary", "--auto", "--pos", "0x0", "--rotate", "normal") + +val interfaceMapOn = Map[String, Seq[String]]( + "DP1" -> List("--output", "DP1", "--auto", "--right-of", "eDP1", "--rotate", "normal"), + "HDMI2" -> List("--output", "HDMI2", "--auto", "--left-of", "eDP1", "--rotate", "normal") +) + +val interfaceMapOff = Map[String, Seq[String]]( + "DP1" -> List("--output", "DP1", "--off"), + "DP2" -> List("--output", "DP2", "--off"), + "HDMI1" -> List("--output", "HDMI1", "--off"), + "HDMI2" -> List("--output", "HDMI2", "--off") +) + +val interfacesOn = ("/usr/bin/xrandr" #| Seq("grep", "-w", "connected") #| Seq("tr", "-s", "' '") #| Seq("cut", "-d", " ", "-f", "1")).!!.trim.split("\n") +val interfacesOff = ("/usr/bin/xrandr" #| Seq("grep", "-w", "disconnected") #| Seq("tr", "-s", "' '") #| Seq("cut", "-d", " ", "-f", "1")).!!.trim.split("\n") +var execCmd = "/usr/bin/xrandr" :: eDP1 ++ + interfacesOn.flatMap(i => interfaceMapOn.getOrElse(i.toUpperCase, List.empty)) ++ + interfacesOff.flatMap(i => interfaceMapOff.getOrElse(i.toUpperCase, List.empty)) + +println(s"Enabled interfaces: ${interfacesOn.mkString(" ")}") +println(s"Disabled interfaces: ${interfacesOff.mkString(" ")}") +println(s"Executing command line: $execCmd") + +execCmd.!