make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / daemons / tablet-rotation-watcher
blobfb3bc4338e146660d3c2420209197ab486ce4510
1 #!/usr/bin/env python3
2 """
3 Derived from https://github.com/togald/thinkpad-l390-yoga-scripts/
4 Derived from https://github.com/ffejery/thinkpad-l380-yoga-scripts
5 thinkpad-rotate.py
6 Rotates any detected screen, wacom digitizers, touchscreens,
7 touchpad/trackpoint based on orientation gathered from accelerometer. 
8 Tested with Lenovo ThinkPad L380 Yoga
9 https://github.com/ffejery/thinkpad-l380-yoga-scripts
10 Originally from AdmiralAkber:
11 Tested with Lenovo Thinkpad Yoga S1
12 https://github.com/admiralakber/thinkpad-yoga-scripts
13 Acknowledgements:
14 Modified from source:
15 https://gist.githubusercontent.com/ei-grad/4d9d23b1463a99d24a8d/raw/rotate.py
16 """
19 import time
20 import os
21 import sys
22 from glob import glob
25 class IIODevice(object):
26         def __init__(self, device_dir):
27                 # raise an exception if the directory does not exist
28                 os.stat(device_dir)
29                 self.device_dir = device_dir
30         
31         def _prop(self, name, ret_type = None):
32                 data = open(os.path.join(self.device_dir, name), 'r').read()
33                 if ret_type is not None:
34                         return ret_type(data)
35                 return data
36         
37         @property
38         def name(self):
39                 return self._prop('name')
40         
41         @property
42         def accel_xy(self):
43                 return self.accel_x, self.accel_y
44         
45         @property
46         def accel_x(self):
47                 return self._prop('in_accel_x_raw', float) * self._prop('in_accel_x_scale', float)
48         
49         @property
50         def accel_y(self):
51                 return self._prop('in_accel_y_raw', float) * self._prop('in_accel_y_scale', float)
54 for device_dir in glob('/sys/bus/iio/devices/iio:device*'):
55         iio_device = IIODevice(device_dir)
56         if 'accel' in iio_device.name:
57                 break
58 else:
59         sys.stderr.write("Can't find an accellerator device!\n")
60         sys.exit(1)
63 g = 7.0  # (m^2 / s) sensibility, gravity trigger
66 orientations = {
67         'normal': {
68                 'condition': lambda x, y: y > g,
69         },
70         'upside-down': {
71                 'condition': lambda x, y: y <= -g,
72         },
73         'left-up': {
74                 'condition': lambda x, y: x > g,
75         },
76         'right-up': {
77                 'condition': lambda x, y: x <= -g,
78         },
82 def notify_new_orientation(orientation):
83         open('/dev/orientation', 'w').write(orientation)
86 if __name__ == '__main__':
87         current_orientation_name = None
88         
89         while True:
90                 x, y = iio_device.accel_xy
91                 
92                 for orientation_name, orientation in orientations.items():
93                         if orientation_name == current_orientation_name:
94                                 continue
95                         else:
96                                 if orientation['condition'](x, y):
97                                         notify_new_orientation(orientation_name)
98                                         current_orientation_name = orientation_name
99                 
100                 # TODO listen on events instead of periodic polling
101                 time.sleep(0.25)