aboutsummaryrefslogtreecommitdiffstats
path: root/offset_layout.py
blob: 6c2f87932955ae7763ddeea13a967ba82272fda5 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/python

import re
import sys
import getopt

DX = DY = PX = PY = KX = KY = 0

_RE_LINE = re.compile("^\s*(?P<keyword>[\w-]+)\s+{\s*$")
_RE_XY = re.compile("^(?P<start>\s*)(?P<xy>[x|y]\s+)(?P<num>\d+)(?P<end>\s*)$")

def main():
  ParseArgs()
  ParseInput()

def Usage():
  print >>sys.stderr, """
  Usage: %s --dx offset-x-display --dy offset-y-display --px offset-x-phone-buttons --py offset-y-phone-buttons --kx offset-x-keyboard --ky offset-y-keyboard < layout > layout2.

  Unspecified offsets default to 0 (unchanged).
  Reads from stdin, outputs to stdout.
  Phone buttons: soft-left/top/righ/bottom, home, dpad, dial, power, etc.
  Keyboard is the soft keyboard.

  If your shell doesn't let you use negative integers, use _ for minus sign,
  i.e. --dx _40 --dy _42 for <-40,-42).
  """ % (sys.argv[0])
  sys.exit(1)

def ParseArgs():
  global DX, DY, PX, PY, KX, KY
  try:
    options, args = getopt.getopt(sys.argv[1:], "", ["dx=", "dy=", "px=", "py=", "kx=", "ky="])
    for opt, value in options:
      if opt in ["--dx"]:
        DX = int(value.replace("_", "-"))
      elif opt in ["--dy"]:
        DY = int(value.replace("_", "-"))
      elif opt in ["--px"]:
        PX = int(value.replace("_", "-"))
      elif opt in ["--py"]:
        PY = int(value.replace("_", "-"))
      elif opt in ["--kx"]:
        KX = int(value.replace("_", "-"))
      elif opt in ["--ky"]:
        KY = int(value.replace("_", "-"))
      else:
        Usage()
  except getopt.error, msg:
    Usage()

def ParseInput():
  global DX, DY, PX, PY, KX, KY

  PHONE = [	"soft-left", "home", "back", "dpad-up", "dpad-down", "dpad-left", "dpad-right", "dpad-center", "phone-dial", "phone-hangup", "power", "volume-up", "volume-down" ]
  KEYBOARD = [   "DEL", "CAP", "CAP2", "PERIOD", "ENTER", "ALT", "SYM", "AT", "SPACE", "SLASH", "COMMA", "ALT2" ]

  mode = None
  while True:
    line = sys.stdin.readline()
    if not line:
      return
    m_line = _RE_LINE.match(line)
    if m_line:
      keyword = m_line.group("keyword")
      if keyword in ["display", "button"]:
        mode = keyword
        is_phone = False
        is_keyboard = False
        print >>sys.stderr, "Mode:", mode
      else:
        if mode == "button" and "{" in line:
          is_phone = keyword in PHONE
          is_keyboard = (len(keyword) == 1 and keyword.isalnum())
          if not is_keyboard:
            is_keyboard = keyword in KEYBOARD
    elif "}" in line:
      is_phone = False
      is_keyboard = False
      if mode == "display":
        mode = None
    else:
      m_xy = _RE_XY.match(line)
      if m_xy:
        x = 0
        y = 0
        if mode == "display":
          x = DX
          y = DY
        elif mode == "button" and is_phone:
          x = PX
          y = PY
        elif mode == "button" and is_keyboard:
          x = KX
          y = KY
        if x or y:
          d = m_xy.groupdict()
          n = int(d["num"])
          if d["xy"].startswith("x"):
            n += x
          else:
            n += y
          d["num"] = n
          line = "%(start)s%(xy)s%(num)s%(end)s" % d
    sys.stdout.write(line)




if __name__ == "__main__":
  main()