• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2
3import re
4import sys
5import getopt
6
7DX = DY = PX = PY = KX = KY = 0
8
9_RE_LINE = re.compile("^\s*(?P<keyword>[\w-]+)\s+{\s*$")
10_RE_XY = re.compile("^(?P<start>\s*)(?P<xy>[x|y]\s+)(?P<num>\d+)(?P<end>\s*)$")
11
12def main():
13  ParseArgs()
14  ParseInput()
15
16def Usage():
17  print >>sys.stderr, """
18  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.
19
20  Unspecified offsets default to 0 (unchanged).
21  Reads from stdin, outputs to stdout.
22  Phone buttons: soft-left/top/righ/bottom, home, dpad, dial, power, etc.
23  Keyboard is the soft keyboard.
24
25  If your shell doesn't let you use negative integers, use _ for minus sign,
26  i.e. --dx _40 --dy _42 for <-40,-42).
27  """ % (sys.argv[0])
28  sys.exit(1)
29
30def ParseArgs():
31  global DX, DY, PX, PY, KX, KY
32  try:
33    options, args = getopt.getopt(sys.argv[1:], "", ["dx=", "dy=", "px=", "py=", "kx=", "ky="])
34    for opt, value in options:
35      if opt in ["--dx"]:
36        DX = int(value.replace("_", "-"))
37      elif opt in ["--dy"]:
38        DY = int(value.replace("_", "-"))
39      elif opt in ["--px"]:
40        PX = int(value.replace("_", "-"))
41      elif opt in ["--py"]:
42        PY = int(value.replace("_", "-"))
43      elif opt in ["--kx"]:
44        KX = int(value.replace("_", "-"))
45      elif opt in ["--ky"]:
46        KY = int(value.replace("_", "-"))
47      else:
48        Usage()
49  except getopt.error, msg:
50    Usage()
51
52def ParseInput():
53  global DX, DY, PX, PY, KX, KY
54
55  PHONE = [	"soft-left", "home", "back", "dpad-up", "dpad-down", "dpad-left", "dpad-right", "dpad-center", "phone-dial", "phone-hangup", "power", "volume-up", "volume-down" ]
56  KEYBOARD = [   "DEL", "CAP", "CAP2", "PERIOD", "ENTER", "ALT", "SYM", "AT", "SPACE", "SLASH", "COMMA", "ALT2" ]
57
58  mode = None
59  while True:
60    line = sys.stdin.readline()
61    if not line:
62      return
63    m_line = _RE_LINE.match(line)
64    if m_line:
65      keyword = m_line.group("keyword")
66      if keyword in ["display", "button"]:
67        mode = keyword
68        is_phone = False
69        is_keyboard = False
70        print >>sys.stderr, "Mode:", mode
71      else:
72        if mode == "button" and "{" in line:
73          is_phone = keyword in PHONE
74          is_keyboard = (len(keyword) == 1 and keyword.isalnum())
75          if not is_keyboard:
76            is_keyboard = keyword in KEYBOARD
77    elif "}" in line:
78      is_phone = False
79      is_keyboard = False
80      if mode == "display":
81        mode = None
82    else:
83      m_xy = _RE_XY.match(line)
84      if m_xy:
85        x = 0
86        y = 0
87        if mode == "display":
88          x = DX
89          y = DY
90        elif mode == "button" and is_phone:
91          x = PX
92          y = PY
93        elif mode == "button" and is_keyboard:
94          x = KX
95          y = KY
96        if x or y:
97          d = m_xy.groupdict()
98          n = int(d["num"])
99          if d["xy"].startswith("x"):
100            n += x
101          else:
102            n += y
103          d["num"] = n
104          line = "%(start)s%(xy)s%(num)s%(end)s" % d
105    sys.stdout.write(line)
106
107
108
109
110if __name__ == "__main__":
111  main()
112