~tinarussell/tabuntu/rotation-app

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
#!/usr/bin/python

#Licensed under European Union Public License v1.0
#http://ec.europa.eu/idabc/en/document/7330/5980

#Licensed under GNU Lesser General Public License v2.1
#/usr/share/common-licenses/LGPL-2.1
#http://www.gnu.org/licenses/lgpl-2.1.html

import os
import subprocess

normal = 0
right = 1
left = 2
inverted = 3

orArray = ["normal", "right", "left", "inverted"]

#Use this dictionary to affect the program's definition of "toggle"
toggleDict = {"normal" : "left", "left" : "normal"}

class Or:
  """Datatype for orientation"""
  def __init__(self, dr): #dr = direction
    if dr == "normal" or dr == "right" or dr == "left" or dr == "inverted":
      self.xr = dr #xr = xrandr; this is the orientation in xrandr's terminology
      self.xsw = str(eval(dr)) #xr = xsetwacom; same for xsetwacom
    else:
      self.xr = "normal"
      self.xsw = 0

def getCurrentOrientation():
  """Returns current display orientation as an Or class object"""
  orNumber = eval(subprocess.Popen(["xsetwacom", "get", "stylus", "Rotate"], stdout=subprocess.PIPE).communicate()[0])
  return Or(orArray[orNumber])

def changeOrientation(newOr):
  """Sets a new display orientation using xrandr and xsetwacom"""
  xrandrOutput = os.system("xrandr --output LVDS --rotate " + newOr.xr)
  xsetwacomOutput = os.system("xsetwacom set stylus Rotate " + newOr.xsw)
  return (xrandrOutput, xsetwacomOutput)

def toggleOrientation(curOr):
  """Toggles the orientation based on toggleDict dictionary"""
  output = changeOrientation(Or(toggleDict.get(curOr.xr)))
  return output

if __name__ == "__main__":
  curOr = getCurrentOrientation()
  toggleOrientation(curOr)