Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matejc
Created November 15, 2017 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matejc/9ba3078eaa8b880a57762cc271fc8281 to your computer and use it in GitHub Desktop.
Save matejc/9ba3078eaa8b880a57762cc271fc8281 to your computer and use it in GitHub Desktop.
cycle through monitors with mouse
#!/usr/bin/env python3
import subprocess
import re
mouselocation = subprocess.check_output(['xdotool', 'getmouselocation']).decode('utf-8')
mouse = {
'x': int(mouselocation.split(' ')[0].split(':')[1]),
'y': int(mouselocation.split(' ')[1].split(':')[1])
}
results = []
for line in subprocess.check_output('xrandr').decode('utf-8').split('\n'):
words = line.split(' ')
if len(words) < 2:
continue
if words[1] == 'connected':
gs = [s for s in words if re.match(r'[0-9]+x[0-9]+\+[0-9]+\+[0-9]+', s)]
g = "" if len(gs) == 0 else gs[0]
data = [s.split('x') for s in g.split('+')]
entry = {
'name': words[0],
'state': words[1],
'index': len(results),
'width': int(data[0][0]),
'height': int(data[0][1]),
'x': int(data[1][0]),
'y': int(data[2][0]),
'mouse': False
}
entry['mouse'] = entry['x'] < mouse['x'] and \
entry['x']+entry['width'] > mouse['x'] and \
entry['y'] < mouse['y'] and \
entry['y']+entry['height'] > mouse['y']
results += [entry]
def monitor_sort(current):
y_score = 1
x_score = 1
for r in results:
if current['index'] == r['index']:
continue
if current['y'] > r['y']:
y_score += 2
if current['x'] > r['x']:
x_score += 1
return y_score * x_score
monitor = next((i for i, x in enumerate(results) if x['mouse']), 0)
entry = results[(monitor + 1) % len(results)]
subprocess.Popen(['xdotool', 'mousemove', str((entry['width']/2)+entry['x']), str((entry['height']/2)+entry['y'])])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment