forked from evgeni/local-bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tablet_rotate
executable file
·65 lines (52 loc) · 2.16 KB
/
tablet_rotate
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
#!/usr/bin/python
SCREEN = "LVDS1"
WACOM_DEVICES = ["Serial Wacom Tablet eraser", "Serial Wacom Tablet stylus"]
NORMAL="normal"
TABLET="right"
xrandr_rotations = ["normal", "left", "inverted", "right"]
xsetwacom_rotations = ["NONE", "CCW", "HALF", "CW"]
xmodmap_rotations = ["Left", "Up", "Right", "Down"]
from subprocess import call
try:
from subprocess import check_output
except:
from subprocess import Popen, PIPE
def check_output(*popenargs, **kwargs):
return Popen(*popenargs, stdout=PIPE).communicate()[0]
def get_current_rotation(screen):
rotation = "normal"
xrandr_output = check_output(["xrandr", "-q"])
for line in xrandr_output.splitlines():
if line.startswith(screen):
if not line.split()[3].startswith("("):
rotation = line.split()[3]
return rotation
def get_next_rotation(rotation):
return (xrandr_rotations.index(rotation)+1)%len(xrandr_rotations)
def set_xrandr_rotation(screen, rotation):
call(["xrandr", "--output", screen, "--rotate", rotation])
def set_xsetwacom_rotation(device, rotation):
call(["xsetwacom", "set", device, "rotate", rotation])
def set_rotation(rotation):
set_xrandr_rotation(SCREEN, xrandr_rotations[rotation])
for device in WACOM_DEVICES:
set_xsetwacom_rotation(device, xsetwacom_rotations[rotation])
def set_arrow_rotation(rotation):
cmd = ["xmodmap"]
cmd += ["-e", "keycode 113 = %s"%xmodmap_rotations[(rotation+0)%len(xmodmap_rotations)]]
cmd += ["-e", "keycode 111 = %s"%xmodmap_rotations[(rotation+1)%len(xmodmap_rotations)]]
cmd += ["-e", "keycode 114 = %s"%xmodmap_rotations[(rotation+2)%len(xmodmap_rotations)]]
cmd += ["-e", "keycode 116 = %s"%xmodmap_rotations[(rotation+3)%len(xmodmap_rotations)]]
call(cmd)
def set_next_rotation():
current_rotation = get_current_rotation(SCREEN)
next_rotation = get_next_rotation(current_rotation)
set_rotation(next_rotation)
set_arrow_rotation(next_rotation)
def set_normal_rotation():
normal = xrandr_rotations.index(NORMAL)
set_rotation(normal)
def set_tablet_rotation():
tablet = xrandr_rotations.index(TABLET)
set_rotation(tablet)
set_next_rotation()