Last active
December 11, 2015 14:08
-
-
Save BenoitZugmeyer/23299e3830be3f794838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import usb | |
import subprocess | |
import time | |
import json | |
# Put: | |
# SUBSYSTEMS=="usb", ATTRS{idVendor}=="0c45", ATTRS{idProduct}=="7403", GROUP="users", | |
# MODE="0666" | |
# In: | |
# /etc/udev/rules.d/50-embedded_devices.rules | |
# Execute a lua script in awesome | |
def run_awesome(cmd): | |
p = subprocess.Popen( | |
['awesome-client'], | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE, | |
) | |
out = p.communicate(input=cmd) | |
if out[1]: | |
raise Exception(out[1]) | |
type, result = out[0].decode().strip().partition(' ')[::2] | |
if type == 'string': | |
result = result[1:-1] | |
return result | |
# Get the currently focused client class name | |
def get_focused_client(): | |
return run_awesome(rb''' | |
return client.focus.class | |
''') | |
# Send events to X (see xte --help) | |
def send_x_events(*keys): | |
subprocess.check_call(['xte'] + list(keys)) | |
def on_keydown(): | |
res = run_awesome(rb''' | |
pedal_focused_client = nil | |
pedal_new_client = nil | |
local awful = require("awful") | |
for i, c in pairs(client.get()) do | |
if c.class == "Chromium" or c.class == "chromium-continuous-bin" or c.class == "chromium" then | |
pedal_focused_client = client.focus | |
pedal_new_client = c | |
awful.client.jumpto(c) | |
return | |
end | |
end | |
return "not found" | |
''') | |
if res != 'not found': | |
time.sleep(.1) | |
send_x_events('key F5') | |
def on_keyup(): | |
run_awesome(rb''' | |
local awful = require("awful") | |
if pedal_new_client == client.focus and pedal_focused_client ~= nil then | |
awful.client.jumpto(pedal_focused_client) | |
end | |
''') | |
vendor_id = 0x0c45 | |
device_id = 0x7403 | |
try: | |
device = usb.core.find(idVendor=vendor_id, idProduct=device_id) | |
if device is None: | |
raise Exception('device not found') | |
try: | |
device.detach_kernel_driver(0) | |
except: | |
pass | |
usb.util.claim_interface(device, 0) | |
while True: | |
data = device.read(0x81, 8, 0) | |
if data[3]: | |
on_keydown() | |
else: | |
on_keyup() | |
finally: | |
if device: | |
usb.util.release_interface(device, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Foot switch driver | |
[Service] | |
ExecStart=/opt/bin/foot-switch-driver | |
Restart=always | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment