Advertisement
Guest User

Hogwog

a guest
May 17th, 2009
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.23 KB | None | 0 0
  1. #!/etc/bin/python
  2. import usb
  3. import virtkey
  4. from sys import exit
  5.  
  6. class KeyMapper():
  7.     def __init__(self):
  8.         self.map_dic = {}
  9.         self.map_dic2 = {}
  10.         self.v = virtkey.virtkey()
  11.        
  12.     def map(self, remotenr, keycode):
  13.         self.map_dic[remotenr] = keycode
  14.    
  15.     def execute(self, remotenr):
  16.         try:
  17.             self.v.press_keycode(self.map_dic[remotenr])
  18.             self.v.release_keycode(self.map_dic[remotenr])
  19.         except KeyError:
  20.             print str(remotenr)+' (not mapped)'
  21.  
  22. #what busses are availible?
  23. busses = usb.busses()
  24.  
  25. ai = None
  26.  
  27. #we only need the ASUS Ai Remote
  28. while not ai:
  29.     for bus in busses:
  30.         for dev in bus.devices:
  31.             if dev.idVendor == 0x0b05 and dev.idProduct == 0x172e:
  32.                 print 'ASUS Ai Remote found.'
  33.                 ai = dev
  34.        
  35. #if the remote isn't found - exit
  36. if not ai:
  37.     print 'No Device found!'
  38.     exit()
  39.  
  40. #If we want our RC to to something, we should map its keys.
  41. mapper = KeyMapper()
  42. mapper.map(4,121)   #Mute
  43. mapper.map(7,123)   #VolumeUp
  44. mapper.map(8,173)   #Previous
  45. mapper.map(9,172)   #Play/Pause
  46. mapper.map(10,171)  #Next
  47. mapper.map(11,122)  #VolumeDown
  48.  
  49. #we're still there so we open the device
  50. handler = ai.open()
  51.  
  52. #we know that there is only one confoguration
  53. conf = ai.configurations[0]
  54. #only one interface
  55. iface = conf.interfaces[0][0]
  56. #only one endpoint
  57. endp = iface.endpoints[0]
  58.  
  59. #we don't want the kernel to handle this device
  60. try:
  61.     handler.detachKernelDriver(iface)
  62. except usb.USBError, e:
  63.     print e.args[0]
  64.     if e.args != ('could not detach kernel driver from interface 0: No data available',):
  65.         raise e
  66.  
  67. #the try...except statement in the next loop is due to a pyusb bug, that raises
  68. #an error ('USBError: no error') where there is none.
  69. while True:
  70.     data = (2,0,0,0,0,0,0,0)
  71.     try:
  72.         data = handler.interruptRead(endp.address, endp.maxPacketSize,10)
  73.     except usb.USBError, e:
  74.         if e.args != ('No error',) and e.args != ('could not detach kernel driver from interface 0: No data available',): # http://bugs.debian.org/476796
  75.             raise e
  76.    
  77.     if data[1] != 0:
  78.         mapper.execute(data[1])
  79.     if data[1] == 4:
  80.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement