add copyright headers
[pyhdaps.git] / hdaps.py
blobac3f71c2a46499e7cad45c8397c35e6dd3be024f
1 '''
2 Python bindings for the HDAPS interface
3 '''
5 # Copyright: 2008-2009 Evgeni Golov <sargentd@die-welt.net>
6 # License: GPL-2
8 import struct
9 import os.path
11 __SYSFS_HDAPS='/sys/devices/platform/hdaps'
12 __SYSFS_HDAPS_CALIBRATION='%s/calibrate' % __SYSFS_HDAPS
13 __SYSFS_HDAPS_POSITION='%s/position' % __SYSFS_HDAPS
14 __SYSFS_HDAPS_KEYBOARD_ACTIVITY='%s/keyboard_activity' % __SYSFS_HDAPS
15 __SYSFS_HDAPS_MOUSE_ACTIVITY='%s/mouse_activity' % __SYSFS_HDAPS
16 __INPUT_HDAPS_EVENT='/dev/input/hdaps/accelerometer-event'
18 def readPosition(calibrate=False):
19 if calibrate:
20 filename = __SYSFS_HDAPS_CALIBRATION
21 else:
22 filename = __SYSFS_HDAPS_POSITION
23 line = __readSysfs(filename).strip(')(')
24 posX,posY = line.split(',')
25 return (int(posX), int(posY))
27 def readKeyboardActivity():
28 return int(__readSysfs(__SYSFS_HDAPS_KEYBOARD_ACTIVITY))
30 def readMouseActivity():
31 return int(__readSysfs(__SYSFS_HDAPS_MOUSE_ACTIVITY))
33 def __readSysfs(filename):
34 try:
35 fd = file(filename, 'r')
36 line = fd.readline().strip()
37 fd.close()
38 except IOError:
39 raise IOError, '%s could not be read, is the "hdaps" module loaded?' % filename
40 return line
42 def hasInputInterface():
43 return os.path.exists(__INPUT_HDAPS_EVENT)
45 def readInputPosition(callback):
46 if not hasInputInterface():
47 raise IOError, 'You don\'t have %s. Is the "hdaps" module from tp_smapi loaded?' % __INPUT_HDAPS_EVENT
48 x = 0
49 y = 0
50 input = open(__INPUT_HDAPS_EVENT,"rb")
51 event = input.read(16)
52 while event:
53 (tv_sec, tv_usec, type, code, value) = struct.unpack('iihhi',event)
54 if type == 3:
55 if code == 0:
56 x = value
57 if code == 1:
58 y = value
59 if type == 0 and code == 0:
60 callback(x, y)
61 event = input.read(16)
62 input.close()