Guest
Public paste!

Robert Jordens

By: a guest | Aug 6th, 2007 | Syntax: Python | Size: 2.68 KB | Hits: 69 | Expires: Never
Copy text to clipboard
  1. #!/usr/bin/python
  2.  
  3. from optparse import OptionParser
  4. import sys, os, fcntl, struct, ctypes
  5.  
  6. def io(d, t, n, s):
  7.     return ((d<<(8+8+14) | (t<<8) | (s<<(8+8)) | (n<<0)))
  8.  
  9. class hiddev_report_info(ctypes.Structure):
  10.     _fields_ = [
  11.             ("report_type", ctypes.c_uint32),
  12.             ("report_id", ctypes.c_uint32),
  13.             ("num_fields", ctypes.c_uint32),
  14.             ]
  15.  
  16. class hiddev_usage_ref(ctypes.Structure):
  17.     _fields_ = [
  18.             ("report_type", ctypes.c_uint32),
  19.             ("report_id", ctypes.c_uint32),
  20.             ("field_index", ctypes.c_uint32),
  21.             ("usage_index", ctypes.c_uint32),
  22.             ("usage_code", ctypes.c_uint32),
  23.             ("value", ctypes.c_int32),
  24.             ]
  25.  
  26. HIDIOCINITREPORT    = io(0, ord("H"), 0x05, 0)
  27. HIDIOCSUSAGE        = io(1, ord("H"), 0x0c,
  28.         ctypes.sizeof(hiddev_usage_ref))
  29. HIDIOCSREPORT       = io(1, ord("H"), 0x08,
  30.         ctypes.sizeof(hiddev_report_info))
  31.  
  32. class mx610(object):
  33.     states = {
  34.             "off": 0x6, "on": 0x5,
  35.             "pulse": 0x4, "flash": 0x3,
  36.             "inston": 0x2, "instoff": 0x1,
  37.             "nop": 0x0}
  38.     pfx = "\x01\x80\x52"
  39.     susage = hiddev_usage_ref(2, 0x10, 0, 0, 0xff000001, 0)
  40.     sreport = hiddev_report_info(2, 0x10, 1)
  41.  
  42.     def __init__(self, dev):
  43.         self._d = open(dev, "rb")
  44.         self.ctl(HIDIOCINITREPORT)
  45.  
  46.     def ctl(self, op, dat=0):
  47.         return fcntl.ioctl(self._d.fileno(), op, dat)
  48.  
  49.     def cmd(self, c):
  50.         for i, v in enumerate(c):
  51.             self.susage.usage_index = i
  52.             self.susage.value = ord(v)
  53.             self.ctl(HIDIOCSUSAGE, ctypes.string_at(ctypes.byref(
  54.                 self.susage), ctypes.sizeof(self.susage)))
  55.         self.ctl(HIDIOCSREPORT, ctypes.string_at(ctypes.byref(
  56.             self.sreport), ctypes.sizeof(self.sreport)))
  57.  
  58.     def state(self, s0="nop", s1="nop", s2="nop"):
  59.         self.cmd(self.pfx+struct.pack("BBB", self.states[s0],
  60.             self.states[s1], self.states[s2]))
  61.         self.confirm()
  62.  
  63.     def confirm(self):
  64.         self.cmd(self.pfx+"\x00"*3)
  65.  
  66. def main():
  67.     states = mx610.states.keys()
  68.     p = OptionParser()
  69.     p.add_option("-D", "--dev", type="string",
  70.         help="device [%default]")
  71.     p.add_option("-E", "--email", type="choice", choices=states,
  72.         help="email light, one of %s, default: %%default" % states)
  73.     p.add_option("-I", "--im", type="choice", choices=states,
  74.         help="instant messaging light, one of %s, default: %%default" % states)
  75.     p.set_defaults(dev="/dev/hiddev0", email="nop", im="nop")
  76.     (o, a) = p.parse_args()
  77.     m = mx610(o.dev)
  78.     m.state(o.email, o.im)
  79.  
  80. if __name__ == "__main__":
  81.     main()