From ea81a9d14146b0d188fa51c16fca7d3e93cea86d Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Thu, 16 Nov 2017 13:38:01 -0800 Subject: [PATCH] Fix the style to be more compliant with Python 2.7's general style for executables. This also enables the user to import autorotate.py as a library, although why anyone would want to do that is beyond me. --- autorotate/autorotate.py | 73 ++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/autorotate/autorotate.py b/autorotate/autorotate.py index 67f68ca..73c482a 100755 --- a/autorotate/autorotate.py +++ b/autorotate/autorotate.py @@ -73,38 +73,45 @@ def is_in(pen): return (res and res.group(1).lower() == 'in') -while True: - int_displays = countdisplays() - time.sleep(1.0/freq) - if int_displays == 1: +def manage_orientation_and_palm_rejection(options): + current_orientation = '' + currently_proximate = False - # Check accelerometers - # Do we need to check the touch_devices list every time? I - # think we do; the list will change dynamically if we - # dynamically load the stylus driver after the system has - # booted. - touch_devices = filter(lambda n: DIGITIZER_RE.match(n), - subprocess.check_output(['xinput', '--list', '--name-only']).splitlines()) - with open(x_accel_path, 'r') as fx: - with open(y_accel_path, 'r') as fy: - thex = float(fx.readline()) - they = float(fy.readline()) - for check in transforms: - if check.xrule(thex, they) and check.yrule(thex, they): - if current_orientation != check.name: - print "Switching to orientation %s" % check.name - os.system('xrandr -o %s' % check.name) - for device in touch_devices: - os.system("xinput set-prop '%s' 'Coordinate Transformation Matrix' %s" % - (device, check.matrix)) - current_orientation = check.name - refreshtouch() - break + while True: + int_displays = countdisplays() + time.sleep(1.0/freq) + if int_displays == 1: + + # Check accelerometers + # Do we need to check the touch_devices list every time? I + # think we do; the list will change dynamically if we + # dynamically load the stylus driver after the system has + # booted. + touch_devices = filter(lambda n: DIGITIZER_RE.match(n), + subprocess.check_output(['xinput', '--list', '--name-only']).splitlines()) + with open(x_accel_path, 'r') as fx: + with open(y_accel_path, 'r') as fy: + thex = float(fx.readline()) + they = float(fy.readline()) + for check in transforms: + if check.xrule(thex, they) and check.yrule(thex, they): + if current_orientation != check.name: + print "Switching to orientation %s" % check.name + os.system('xrandr -o %s' % check.name) + for device in touch_devices: + os.system("xinput set-prop '%s' 'Coordinate Transformation Matrix' %s" % + (device, check.matrix)) + current_orientation = check.name + refreshtouch() + break + + # Palm rejection (sort-of): + pen_devices = [p for p in touch_devices if PEN_RE.search(p)] + pen_status = bool([p for p in pen_devices if is_in(p)]) + if pen_status != currently_proximate: + print "%s palm rejection" % ("Activating" if pen_status else "Deactivating") + currently_proximate = pen_status + os.system("xinput %s '%s'" % (xinput_statemap[pen_status], devicename)) - # Palm rejection (sort-of): - pen_devices = [p for p in touch_devices if PEN_RE.search(p)] - pen_status = bool([p for p in pen_devices if is_in(p)]) - if pen_status != currently_proximate: - print "%s palm rejection" % ("Activating" if pen_status else "Deactivating") - currently_proximate = pen_status - os.system("xinput %s '%s'" % (xinput_statemap[pen_status], devicename)) +if __name__ == '__main__': + manage_orientation_and_palm_rejection(sys.argv[1:])