├── README.md └── autorotate ├── autorotate.py ├── changeautorotatestatus.py ├── status.txt ├── touch.py └── touch.txt /README.md: -------------------------------------------------------------------------------- 1 | Surface3-Scripts 2 | ================ 3 | 4 | Some simple scripts for Linux on the Surface Pro 3. 5 | 6 | Autorotate: autorotate.py run with python2 needs 7 | 8 | autorotate.py 9 | ============= 10 | 11 | Script for managing autorotation of the screen and deactivation of the touchscreen through the pen. 12 | 13 | Instalation 14 | ----------- 15 | * install libnotify and python2-gobject (needed for notifications) 16 | * copy autorotate.py, touch.py, changeautorotatestatus.py, touch.txt, status.txt in the same folder 17 | 18 | Usage 19 | ----- 20 | Start script: 21 | ``` 22 | $ python2 /path/to/autorotate.py 23 | ``` 24 | Deactivate/reactivate autorotation: 25 | ``` 26 | $ python2 /path/to/changeautorotatestatus.py 27 | ``` 28 | Deactivate/reactivate touch: 29 | ``` 30 | $ python2 path/to/touch.py 31 | ``` 32 | -------------------------------------------------------------------------------- /autorotate/autorotate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | import time 3 | import os 4 | import subprocess 5 | import sys 6 | from gi.repository import Notify 7 | 8 | #FUNCTIONS 9 | def readFile(path): #self.filename 10 | myDatei = open(path, "r") 11 | myList = [] 12 | #Liste aus Datei erstellen 13 | for Line in myDatei: 14 | Line = Line.rstrip() 15 | #Line = Line.decode('utf8') 16 | myList.append(Line) 17 | myDatei.close() 18 | return(myList) 19 | 20 | def writeFile(path, myList): #self.filename 21 | myDatei = open(path, "w") 22 | #Liste aus Datei erstelle 23 | myDatei.writelines(myList) 24 | myDatei.close() 25 | 26 | def refreshtouch(): 27 | os.system('xinput disable "NTRG0001:01 1B96:1B05"') 28 | os.system('xinput enable "NTRG0001:01 1B96:1B05"') 29 | 30 | def checkdisplays(): 31 | check_displays = "xrandr | grep -w 'connected'" 32 | str_displays = str(subprocess.check_output(check_displays, shell=True).lower().rstrip()) 33 | list_displays = str_displays.splitlines() 34 | int_displays = len(list_displays) 35 | return int_displays 36 | 37 | #PARAMETERS 38 | count = 0 39 | path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))) 40 | devicename = "'NTRG0001:01 1B96:1B05'" 41 | penname = "'NTRG0001:01 1B96:1B05 Pen'" 42 | freq = 5.0 43 | 44 | 45 | # Look for accelerometer 46 | while count <= 9: 47 | if os.path.exists('/sys/bus/iio/devices/iio:device' + str(count) + '/in_accel_scale') == True: 48 | dpath = '/sys/bus/iio/devices/iio:device' + str(count) + '/' # directory of accelerometer device (iio) 49 | break 50 | count = count + 1 51 | #print(dpath) 52 | 53 | #Commands for correct rotation 54 | normal = 'xrandr -o normal; '+'xinput set-prop ' + devicename +" 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1;"+'xinput set-prop ' + penname +" 'Coordinate Transformation Matrix' 1 0 0 0 1 0 0 0 1;" 55 | inverted = 'xrandr -o inverted; '+'xinput set-prop ' + devicename +" 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1;"+'xinput set-prop ' + penname +" 'Coordinate Transformation Matrix' -1 0 1 0 -1 1 0 0 1;" 56 | right = 'xrandr -o left; '+'xinput set-prop ' + devicename +" 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1;"+'xinput set-prop ' + penname +" 'Coordinate Transformation Matrix' 0 -1 1 1 0 0 0 0 1;" 57 | left = 'xrandr -o right; '+'xinput set-prop ' + devicename +" 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1;"+'xinput set-prop ' + penname +" 'Coordinate Transformation Matrix' 0 1 0 -1 0 1 0 0 1;" 58 | state_dict = {0: "normal", 1: "inverted", 2: "right", 3: "left"} 59 | current_state = 0 60 | previous_tstate = "on" 61 | previousStylusProximityStatus = "out" 62 | firstrun = True 63 | #ACCELEROMETER 64 | with open(dpath + 'in_accel_scale') as f: 65 | scale = float(f.readline()) 66 | while True: 67 | multimonitor = False 68 | int_displays = checkdisplays() 69 | if int_displays > 1: 70 | multimonitor = True 71 | time.sleep(1.0/freq) 72 | previous_state = current_state 73 | status = readFile(os.path.join(path, 'status.txt')) 74 | if str(status[0]) == "on" and multimonitor == False: 75 | with open(dpath + 'in_accel_x_raw', 'r') as fx: 76 | with open(dpath + 'in_accel_y_raw', 'r') as fy: 77 | with open(dpath + 'in_accel_z_raw', 'r') as fz: 78 | thex = float(fx.readline()) 79 | they = float(fy.readline()) 80 | thez = float(fz.readline()) 81 | if checkdisplays() == 1: 82 | if (thex >= 65000 or thex <=650): 83 | if (they <= 65000 and they >= 64000): 84 | os.system(normal) 85 | current_state = 0 86 | if (they >= 650 and they <= 1100): 87 | os.system(inverted) 88 | current_state = 1 89 | if (thex <= 64999 and thex >= 650): 90 | if (thex >= 800 and thex <= 1000): 91 | os.system(right) 92 | current_state = 2 93 | if (thex >= 64500 and thex <=64700): 94 | os.system(left) 95 | current_state = 3 96 | 97 | os.system('clear') 98 | print("ExtDi: " + str(multimonitor)) 99 | print("A-ROT: " + status[0]) 100 | print(" x: " + str(thex)) 101 | print(" y: " + str(they)) 102 | print(" z: " + str(thez)) 103 | print(" POS: " + state_dict[current_state]) 104 | if status[0] == "off" or multimonitor == True: 105 | os.system('clear') 106 | print("ExtDi: " + str(multimonitor)) 107 | print("A-ROT: " + status[0]) 108 | print(" x: " + status[0]) 109 | print(" y: " + status[0]) 110 | print(" z: " + status[0]) 111 | print(" POS: " + state_dict[previous_state]) 112 | if current_state != previous_state: 113 | refreshtouch() 114 | print "Touchscreen refreshed" 115 | 116 | print("##########################") 117 | #SCREEN 118 | stylusProximityCommand = 'xinput query-state "NTRG0001:01 1B96:1B05 Pen" | grep Proximity | cut -d " " -f3 | cut -d "=" -f2' 119 | stylusProximityStatus = str(subprocess.check_output(stylusProximityCommand, shell=True).lower().rstrip()) 120 | tstatus = readFile(os.path.join(path, 'touch.txt')) 121 | #TOUCHSCREEN 122 | if str(tstatus[0]) == "on" and stylusProximityStatus == "out": 123 | os.system('xinput enable ' + devicename + '') 124 | print("TOUCH: " + tstatus[0]) 125 | if str(tstatus[0]) != previous_tstate: 126 | Notify.init ("Touchscreen-ON") 127 | RotationON=Notify.Notification.new ("Touchscreen","Touchscreen is now turned ON","dialog-information") 128 | RotationON.show() 129 | elif str(tstatus[0]) == "off" and stylusProximityStatus == "out": 130 | os.system('xinput disable ' + devicename + '') 131 | print("TOUCH: " + tstatus[0]) 132 | if str(tstatus[0]) != previous_tstate: 133 | Notify.init ("Touchscreen-OFF") 134 | RotationOFF=Notify.Notification.new ("Touchscreen","Touchscreen is now turned OFF","dialog-information") 135 | RotationOFF.show() 136 | previous_tstate = str(tstatus[0]) 137 | #PEN 138 | if str(tstatus[0]) == "off" and stylusProximityStatus == "in": 139 | print("TOUCH: " + tstatus[0]) 140 | print(" PEN: " + stylusProximityStatus) 141 | elif str(tstatus[0]) == "on" and stylusProximityStatus == "in" and firstrun == False: 142 | os.system('xinput disable "NTRG0001:01 1B96:1B05"') 143 | print("TOUCH: " + "off") 144 | print(" PEN: " + stylusProximityStatus) 145 | elif stylusProximityStatus == "out": 146 | print(" PEN: " + stylusProximityStatus) 147 | firstrun == False 148 | -------------------------------------------------------------------------------- /autorotate/changeautorotatestatus.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | import os 3 | from gi.repository import Notify 4 | 5 | path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))) 6 | 7 | def readFile(path): #self.filename 8 | myDatei = open(path, "r") 9 | myList = [] 10 | #Liste aus Datei erstellen 11 | for Line in myDatei: 12 | Line = Line.rstrip() 13 | #Line = Line.decode('utf8') 14 | myList.append(Line) 15 | myDatei.close() 16 | return(myList) 17 | 18 | def writeFile(path, myList): #self.filename 19 | myDatei = open(path, "w") 20 | #Liste aus Datei erstelle 21 | myDatei.writelines(myList) 22 | myDatei.close() 23 | 24 | status = readFile(os.path.join(path, 'status.txt')) 25 | if status[0] == "on": 26 | writeFile(os.path.join(path, 'status.txt'), ["off"]) 27 | Notify.init ("Rotation-ON") 28 | RotationOFF=Notify.Notification.new ("Rotation","Screenrotation is now turned OFF","dialog-information") 29 | RotationOFF.show () 30 | if status[0] == "off": 31 | writeFile(os.path.join(path, 'status.txt'), ["on"]) 32 | Notify.init ("Rotation-OFF") 33 | RotationON=Notify.Notification.new ("Rotation","Screenrotation is now turned ON","dialog-information") 34 | RotationON.show () 35 | -------------------------------------------------------------------------------- /autorotate/status.txt: -------------------------------------------------------------------------------- 1 | on -------------------------------------------------------------------------------- /autorotate/touch.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python2 2 | import os 3 | 4 | def readFile(path): #self.filename 5 | myDatei = open(path, "r") 6 | myList = [] 7 | #Liste aus Datei erstellen 8 | for Line in myDatei: 9 | Line = Line.rstrip() 10 | #Line = Line.decode('utf8') 11 | myList.append(Line) 12 | myDatei.close() 13 | return(myList) 14 | 15 | def writeFile(path, myList): #self.filename 16 | myDatei = open(path, "w") 17 | #Liste aus Datei erstelle 18 | myDatei.writelines(myList) 19 | myDatei.close() 20 | 21 | 22 | devicename = "NTRG0001:01 1B96:1B05" 23 | penname = "'NTRG0001:01 1B96:1B05 Pen'" 24 | path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))) 25 | 26 | status = readFile(os.path.join(path, 'touch.txt')) 27 | if str(status[0]) == "on": 28 | writeFile(os.path.join(path, 'touch.txt'), ["off"]) 29 | if str(status[0]) == "off": 30 | writeFile(os.path.join(path, 'touch.txt'), ["on"]) 31 | -------------------------------------------------------------------------------- /autorotate/touch.txt: -------------------------------------------------------------------------------- 1 | on 2 | --------------------------------------------------------------------------------