1

Context: I have one large monitor, a desktop and a laptop. I use the large monitor and a HDMI switch when I'm at home. This give synergy a problem in that it doesn't recognize parts of the screen if the screen changes. You have to restart the synergy client on the laptop if that is the case in order to be able to access the entire screen.

How can I restart synergy automagically using a script when ever I change screens? I know how to write the script but I don't know from whence to launch it, or if there is such a place. Note that I am referring to physical screens not synergy screens.

1 Answers1

2

Assuming the change in resolution is both causing the error and the trigger to run your script, periodicalloy checking the resolution and run your script if it changes should do the job:

#!/usr/bin/env python3
import subprocess
import time

command = "</path/to/script_to_run>"

def get_res(): # get resolution xr = subprocess.check_output(["xrandr"]).decode("utf-8").split() pos = xr.index("current") return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]

res1 = get_res() while True: time.sleep(5) res2 = get_res() if res2 != res1: subprocess.Popen(["/bin/bash", "-c", command]) res1 = res2

It checks the current resolution every five seconds and runs the script, as set in the head section, if there is a resolution change.

How to use

  1. Copy the script into an empty file, save it as check_resolution.py

  2. In the head section, set the path to your script to restart synergy

  3. Test-run it by running it in a terminal window with the command:

     python3 /path/to/check_resolution.py
    
  4. if all works fine, add it to your startup applications.

Note

The command as a startup application probably needs a small break to make sure the desktop is fully loaded when the script starts. The command to add to Startup Applications would then be:

/bin/bash -c "sleep 15&&python3 /path/to/check_resolution.py

Add to Startup Applications: Dash > Startup Applications > Add, add the command.


Edit

Just to be complete:

If for some reason, you'd prefer a bash version of the script:

#!/bin/bash

function get_res { echo $(xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}') }

res1=$(get_res)

while true; do sleep 5 res2=$(get_res) if [ "$res1" != "$res2" ]; then <command_to_run> fi res1=$res2 done

The use is pretty much the same except the language extension:

check_resolution.sh

and the command to run it:

/bin/bash /path/to/check_resolution.sh

and replace <command_to_run> by the command to restart synergy or the path to your script.

Jacob Vlijm
  • 85,475