-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdaps.py
80 lines (69 loc) · 2.17 KB
/
hdaps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'''
Python bindings for the HDAPS interface
'''
# Copyright: 2008-2009 Evgeni Golov <[email protected]>
# License: GPL-2
import struct
import os.path
__SYSFS_HDAPS='/sys/devices/platform/hdaps'
__SYSFS_HDAPS_CALIBRATION='%s/calibrate' % __SYSFS_HDAPS
__SYSFS_HDAPS_POSITION='%s/position' % __SYSFS_HDAPS
__SYSFS_HDAPS_KEYBOARD_ACTIVITY='%s/keyboard_activity' % __SYSFS_HDAPS
__SYSFS_HDAPS_MOUSE_ACTIVITY='%s/mouse_activity' % __SYSFS_HDAPS
__INPUT_HDAPS_EVENT='/dev/input/hdaps/accelerometer-event'
def readPosition(calibrate=False):
if calibrate:
filename = __SYSFS_HDAPS_CALIBRATION
else:
filename = __SYSFS_HDAPS_POSITION
line = __readSysfs(filename).strip(')(')
posX,posY = line.split(',')
return (int(posX), int(posY))
def readKeyboardActivity():
return int(__readSysfs(__SYSFS_HDAPS_KEYBOARD_ACTIVITY))
def readMouseActivity():
return int(__readSysfs(__SYSFS_HDAPS_MOUSE_ACTIVITY))
def __readSysfs(filename):
try:
fd = file(filename, 'r')
line = fd.readline().strip()
fd.close()
except IOError:
raise IOError, '%s could not be read, is the "hdaps" module loaded?' % filename
return line
def hasInputInterface():
return os.path.exists(__INPUT_HDAPS_EVENT)
def readInputPosition(callback):
if not hasInputInterface():
raise IOError, 'You don\'t have %s. Is the "hdaps" module from tp_smapi loaded?' % __INPUT_HDAPS_EVENT
x = 0
y = 0
input = open(__INPUT_HDAPS_EVENT,"rb")
event = input.read(16)
while event:
(tv_sec, tv_usec, type, code, value) = struct.unpack('iihhi',event)
if type == 3:
if code == 0:
x = value
if code == 1:
y = value
if type == 0 and code == 0:
callback(x, y)
event = input.read(16)
input.close()
def isParked(disk):
newInterface = '/sys/block/%s/device/unload_heads' % disk
oldInterface = '/sys/block/%s/queue/protect' % disk
if os.path.exists(newInterface):
try:
ret = int(__readSysfs(newInterface))
except:
raise IOError, "Your disk '%s' seems not to support IDLE_IMMEDIATE." % disk
elif os.path.exists(oldInterface):
ret = int(__readSysfs(oldInterface))
else:
raise IOError, "Your kernel does not have an interface to check the state of the disk."
if ret > 0:
return True
else:
return False