#!/usr/bin/python # vim: set fileencoding=utf-8 # # Alexander Grothe May 2012 # seahawk1986@hotmail.com # # a heavily modified version of: # # Beagleboard user button # Copyright 2009 mechomaniac.com # http://mechomaniac.com/BeagleboardUserButton import struct import os import fcntl import gobject import sys import uinput from optparse import OptionParser # check if this works on 32-bit systems... '''if os.uname()[4] == "i686": inputEventFormat = 'iihhi' inputEventSize = 16''' ########################################## if os.uname()[4] == "x86_64": inputEventFormat = 'llHHi' inputEventSize = 24 else: sys.exit(1) events = [] for element in dir(uinput): if element.startswith(("KEY_", "REL_", "BTN_")): events.append(eval("uinput.%s" % (element))) class devinput(): timers = {} def __init__(self): parser = Options() self.options = parser.get_opts() #self.socketpath = self.options.socket self.uinputdev = uinput.Device(events, self.options.name) def clear_timers(self): for key, t in self.timers.iteritems(): gobject.source_remove(t) self.uinputdev.emit((1, key), 0) self.timers = {} def eventhandler(self, sock, *args): event = self.socketpath.read(inputEventSize) (time1, time2, typeev, code, value) = struct.unpack(inputEventFormat, event) if typeev == 0 and code == 0 and value == 0: # sync event pass elif typeev == 1 and value == 0: # key release self.clear_timers() elif typeev == 1 and value == 1: # key down self.clear_timers() if not code in self.timers.keys(): self.timers[code] = gobject.timeout_add(self.options.repeat, self.repeatkey, code) self.uinputdev.emit((typeev, code), value) return True def repeatkey(self, code): self.uinputdev.emit((1, code), 2) return True class Options: def __init__(self): self.parser = OptionParser() self.parser.add_option( "-s", "--socket", dest="socket", default=None, help="choose devinput socket to listen on", metavar="SOCKET") self.parser.add_option( "-r", "--repeat", dest="repeat", default=150, type="int", help="wait -r before sending repeated keystrokes", metavar="REPEAT") self.parser.add_option( "-n", "--name", dest="name", default="Bluetooth Remote", help="name of uinput device", metavar="NAME") def get_opts(self): (options, args) = self.parser.parse_args() return options def main(): devinput2lircd = devinput() try: with open(devinput2lircd.options.socket, "rwb") as socketpath: devinput2lircd.socketpath = socketpath gobject.io_add_watch(socketpath, gobject.IO_IN, devinput2lircd.eventhandler) # aquire exclusive access to input device fcntl.ioctl(socketpath.fileno(), 0x40044590, 1) gobject.MainLoop().run() except: exit() if __name__ == '__main__': main()