Skip to content

Instantly share code, notes, and snippets.

@oprypin
Last active January 13, 2016 15:10
Show Gist options
  • Save oprypin/4feff2eec0473198e2ef to your computer and use it in GitHub Desktop.
Save oprypin/4feff2eec0473198e2ef to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# Requirements: Python, PyGTK, seems like it wants NumPy as well
# Arch Linux: pygtk, python2-numpy
# Use `xdotool getmouselocation` to find coordinates
coords = (1864, 185)
min_difference = 10 # percent
command = 'mpg123 ping.mp3'
check_interval = 1 # check every second
cooldown = 5*60 # do nothing for 5 minutes after a ping
import subprocess
import time
import gtk.gdk
root_window = gtk.gdk.get_default_root_window()
def pixel_at(x, y):
pixel_at.pixbuf = pixel_at.pixbuf.get_from_drawable(
root_window, root_window.get_colormap(), x, y, 0, 0, 1, 1
)
return tuple(int(x) for x in pixel_at.pixbuf.pixel_array[0, 0])
pixel_at.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
min_difference *= 256 / 100.0 # convert to 0-255 range
prev_pixel = pixel_at(*coords)
print("Started")
while True:
time.sleep(check_interval)
pixel = pixel_at(*coords)
if any(
abs(a - b) > min_difference # check for difference in every color component individually
for a, b in zip(prev_pixel, pixel)
):
print("Running {!r}".format(command))
subprocess.Popen(command, shell=True)
print("Waiting {} seconds".format(cooldown))
time.sleep(cooldown)
print("Resumed")
pixel = pixel_at(*coords)
prev_pixel = pixel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment