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.

user447607
  • 734
  • 2
  • 15
  • 34

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
  • 82,471
  • 12
  • 195
  • 299
  • 1
    So there isn't like an event or something I can monitor? I have to poll? :-/ Leaves a bad taste in my mouth. – user447607 Mar 31 '15 at 20:58
  • I just had an idea. I never use synergy on the laptop at home unless it's plugged into this monitor. So probably what I need to do is force the resolution/screensize to be what I need. – user447607 Mar 31 '15 at 21:05
  • @user447607 Keep in mind that the system itself runs numerous loops continuously. Running an `xrandr` check once per 5 seconds is an *extremely* low burden. – Jacob Vlijm Mar 31 '15 at 21:05
  • @user447607 btw have you tried it? – Jacob Vlijm Mar 31 '15 at 21:07
  • Not yet. Well as it turns out there's little or no information regarding how to order synergy to use a specified resolution so now I'm thinking yours is the the best idea. I'm not a python guy though so I probably will use BASH. – user447607 Mar 31 '15 at 21:23
  • OK I've gotten to the bottom of this. Its not about resolution but it's about which screen is active. If I execute "$(xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}')" it comes back 1920x1080 every time, however if I do "xrander -q | grep primary" then I can see the change in active screens. This is enough. – user447607 Apr 01 '15 at 01:52