KpdObj.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import re
  4. import string
  5. import ConfigParser
  6. import xml.dom.minidom
  7. from ModuleObj import ModuleObj
  8. from utility.util import LogLevel
  9. from utility.util import log
  10. from data.KpdData import KpdData
  11. class KpdObj(ModuleObj):
  12. def __init__(self):
  13. ModuleObj.__init__(self, 'cust_kpd.h', 'cust_kpd.dtsi')
  14. def get_cfgInfo(self):
  15. cp = ConfigParser.ConfigParser(allow_no_value=True)
  16. cp.read(ModuleObj.get_cmpPath())
  17. ops = cp.options('Key_definition')
  18. for op in ops:
  19. KpdData._keyValueMap[op.upper()] = string.atoi(cp.get('Key_definition', op))
  20. KpdData._keyValueMap['NC'] = 0
  21. cp.read(ModuleObj.get_figPath())
  22. if cp.has_option('KEYPAD_EXTEND_TYPE', 'KEY_ROW'):
  23. KpdData.set_row_ext(string.atoi(cp.get('KEYPAD_EXTEND_TYPE', 'KEY_ROW')))
  24. if cp.has_option('KEYPAD_EXTEND_TYPE', 'KEY_COLUMN'):
  25. KpdData.set_col_ext(string.atoi(cp.get('KEYPAD_EXTEND_TYPE', 'KEY_COLUMN')))
  26. return True
  27. def read(self, node):
  28. nodes = node.childNodes
  29. for node in nodes:
  30. if node.nodeType == xml.dom.Node.ELEMENT_NODE:
  31. if node.nodeName == 'row':
  32. row = string.atoi(node.childNodes[0].nodeValue)
  33. KpdData.set_row(row)
  34. if node.nodeName == 'column':
  35. col = string.atoi(node.childNodes[0].nodeValue)
  36. KpdData.set_col(col)
  37. if node.nodeName == 'keyMatrix':
  38. content = node.childNodes[0].nodeValue
  39. content = content.replace('\t', '')
  40. rows = content.split('''\n''')
  41. matrix = []
  42. for row in rows:
  43. for item in row.split(' '):
  44. matrix.append(item)
  45. KpdData.set_matrix(matrix)
  46. for item in matrix:
  47. if cmp(item, 'NC') != 0:
  48. KpdData._usedKeys.append(item)
  49. KpdData._usedKeys.append('POWER')
  50. if node.nodeName == "keyMatrix_ext" and node.childNodes:
  51. content = node.childNodes[0].nodeValue
  52. content = content.replace('\t', '')
  53. rows = content.split('''\n''')
  54. matrix = []
  55. for row in rows:
  56. for item in row.split(' '):
  57. matrix.append(item)
  58. KpdData.set_matrix_ext(matrix)
  59. if node.nodeName == 'downloadKey':
  60. keys = node.childNodes[0].nodeValue
  61. KpdData.set_downloadKeys(keys.split(' '))
  62. if node.nodeName == 'modeKey':
  63. value = node.childNodes[0].nodeValue
  64. keys = value.split(' ')
  65. KpdData._modeKeys['META'] = keys[0]
  66. KpdData._modeKeys['RECOVERY'] = keys[1]
  67. KpdData._modeKeys['FACTORY'] = keys[2]
  68. if node.nodeName == 'pwrKeyEint_gpioNum':
  69. num = string.atoi(node.childNodes[0].nodeValue)
  70. KpdData.set_gpioNum(num)
  71. if node.nodeName == 'pwrKeyUtility':
  72. util = node.childNodes[0].nodeValue
  73. KpdData.set_utility(util)
  74. if node.nodeName == 'home_key':
  75. if len(node.childNodes) != 0:
  76. home = node.childNodes[0].nodeValue
  77. else:
  78. home = ''
  79. KpdData.set_homeKey(home)
  80. if node.nodeName == 'bPwrKeyUseEint':
  81. flag = False
  82. if node.childNodes[0].nodeValue == 'false':
  83. flag = False
  84. else:
  85. flag = True
  86. KpdData.set_useEint(flag)
  87. if node.nodeName == 'bPwrKeyGpioDinHigh':
  88. flag = False
  89. if node.childNodes[0].nodeValue == 'false':
  90. flag = False
  91. else:
  92. flag = True
  93. KpdData.set_gpioDinHigh(flag)
  94. if node.nodeName == 'pressPeriod':
  95. time = string.atoi(node.childNodes[0].nodeValue)
  96. KpdData.set_pressTime(time)
  97. if node.nodeName == 'keyType':
  98. keyType = node.childNodes[0].nodeValue
  99. KpdData.set_keyType(keyType)
  100. return True
  101. def parse(self, node):
  102. self.get_cfgInfo()
  103. self.read(node)
  104. def gen_files(self):
  105. ModuleObj.gen_files(self)
  106. def gen_spec(self, para):
  107. ModuleObj.gen_spec(self, para)
  108. def fill_hFile(self):
  109. gen_str = '''#include <linux/input.h>\n'''
  110. gen_str += '''#include <cust_eint.h>\n'''
  111. gen_str += '''\n'''
  112. gen_str += '''#define KPD_YES\t\t1\n'''
  113. gen_str += '''#define KPD_NO\t\t0\n'''
  114. gen_str += '''\n'''
  115. gen_str += '''/* available keys (Linux keycodes) */\n'''
  116. gen_str += '''#define KEY_CALL\t\tKEY_SEND\n'''
  117. gen_str += '''#define KEY_ENDCALL\tKEY_END\n'''
  118. gen_str += '''#undef KEY_OK\n'''
  119. gen_str += '''#define KEY_OK\t\tKEY_REPLY /* DPAD_CENTER */\n'''
  120. gen_str += '''#define KEY_FOCUS\tKEY_HP\n'''
  121. gen_str += '''#define KEY_AT\t\tKEY_EMAIL\n'''
  122. gen_str += '''#define KEY_POUND\t228\t//KEY_KBDILLUMTOGGLE\n'''
  123. gen_str += '''#define KEY_STAR\t227\t//KEY_SWITCHVIDEOMODE\n'''
  124. gen_str += '''#define KEY_DEL\t\tKEY_BACKSPACE\n'''
  125. gen_str += '''#define KEY_SYM\t\tKEY_COMPOSE\n'''
  126. gen_str += '''\n'''
  127. gen_str += '''#define KPD_KEY_DEBOUNCE\t%d\n''' %(KpdData.get_pressTime())
  128. gen_str += '''#define KPD_PWRKEY_MAP\tKEY_%s\n''' %(KpdData.get_utility())
  129. # do not gen this macro if the home key is null
  130. if KpdData.get_homeKey() != '':
  131. gen_str += '''#define KPD_PMIC_RSTKEY_MAP\tKEY_%s\n''' %(KpdData.get_homeKey())
  132. if cmp(KpdData.get_keyType(), 'EXTEND_TYPE') != 0:
  133. gen_str += '''#define MTK_PMIC_PWR_KEY\t%d\n''' %(KpdData.get_col() - 1)
  134. if KpdData.get_homeKey() != '':
  135. gen_str += '''#define MTK_PMIC_RST_KEY\t\t%d\n''' %(2*KpdData.get_col() - 1)
  136. gen_str += '''\n'''
  137. gen_str += '''#define KPD_USE_EXTEND_TYPE\tKPD_NO\n'''
  138. else:
  139. gen_str += '''#define MTK_PMIC_PWR_KEY\t%d\n''' %(KpdData.get_col_ext() - 1)
  140. if KpdData.get_keyType() != '':
  141. gen_str += '''#define MTK_PMIC_RST_KEY\t\t%d\n''' %(2*KpdData.get_col_ext() - 1)
  142. gen_str += '''\n'''
  143. gen_str += '''#define KPD_USE_EXTEND_TYPE\tKPD_YES\n'''
  144. gen_str += '''\n'''
  145. gen_str += '''/* HW keycode [0 ~ 71] -> Linux keycode */\n'''
  146. gen_str += '''#define KPD_INIT_KEYMAP()\t\\\n'''
  147. gen_str += '''{\t\\\n'''
  148. if KpdData.get_keyType() == 'NORMAL_TYPE':
  149. for key in KpdData.get_matrix():
  150. if cmp(key, 'NC') != 0:
  151. gen_str += '''\t[%d] = KEY_%s,\t\\\n''' %(KpdData.get_matrix().index(key), key)
  152. else:
  153. for key in KpdData.get_matrix_ext():
  154. if cmp(key, 'NC') != 0:
  155. gen_str += '''\t[%d] = KEY_%s,\t\\\n''' %(KpdData.get_matrix_ext().index(key), key)
  156. gen_str += '''}\n'''
  157. gen_str += '''\n'''
  158. gen_str += '''/***********************************************************/\n'''
  159. gen_str += '''/****************Preload Customation************************/\n'''
  160. gen_str += '''/***********************************************************/\n'''
  161. gen_str += '''#define KPD_PWRKEY_EINT_GPIO\tGPIO%d\n''' %(KpdData.get_gpioNum())
  162. gen_str += '''#define KPD_PWRKEY_GPIO_DIN\t%d\n''' %(int(KpdData.get_gpioDinHigh()))
  163. gen_str += '''\n'''
  164. for key in KpdData.get_downloadKeys():
  165. if cmp(key, 'NC') != 0:
  166. dlIdx = KpdData.get_downloadKeys().index(key)
  167. mtxIdx = self.get_matrixIdx(key)
  168. gen_str += '''#define KPD_DL_KEY%d\t%d\t/* KEY_%s */\n''' %(dlIdx+1, mtxIdx, key)
  169. gen_str += '''\n'''
  170. gen_str += '''/***********************************************************/\n'''
  171. gen_str += '''/****************Uboot Customation**************************/\n'''
  172. gen_str += '''/***********************************************************/\n'''
  173. for (key, value) in KpdData.get_modeKeys().items():
  174. if cmp(value, 'NC') != 0:
  175. idx = self.get_matrixIdx(value)
  176. #idx = KpdData.get_matrix().index(value)
  177. gen_str += '''#define MT65XX_%s_KEY\t%d\t/* KEY_%s */\n''' %(key, idx, value)
  178. gen_str += '''\n'''
  179. return gen_str
  180. def get_matrixIdx(self, value):
  181. if KpdData.get_keyType() == 'NORMAL_TYPE':
  182. if cmp(value, 'POWER') == 0:
  183. return KpdData.get_col() - 1
  184. elif cmp(value, KpdData.get_homeKey()) == 0:
  185. return 2 * KpdData.get_col() - 1
  186. else:
  187. return KpdData.get_matrix().index(value)
  188. elif KpdData.get_keyType() == 'EXTEND_TYPE':
  189. if cmp(value, 'POWER') == 0:
  190. return KpdData.get_col_ext() - 1
  191. elif cmp(value, KpdData.get_homeKey()) == 0:
  192. return 2 * KpdData.get_col_ext() - 1
  193. else:
  194. return KpdData.get_matrix_ext().index(value)
  195. def fill_dtsiFile(self):
  196. gen_str = '''&keypad {\n'''
  197. gen_str += '''\tmediatek,kpd-key-debounce = <%d>;\n''' %(KpdData.get_pressTime())
  198. gen_str += '''\tmediatek,kpd-sw-pwrkey = <%d>;\n''' %(KpdData._keyValueMap[KpdData.get_utility()])
  199. if KpdData.get_keyType() == 'NORMAL_TYPE':
  200. gen_str += '''\tmediatek,kpd-hw-pwrkey = <%d>;\n''' %(KpdData.get_col()-1)
  201. else:
  202. gen_str += '''\tmediatek,kpd-hw-pwrkey = <%d>;\n''' %(KpdData.get_col_ext()-1)
  203. #gen_str += '''\tmediatek,kpd-sw-rstkey = <%d>;\n''' %(KpdData._keyValueMap[KpdData.get_homeKey()])
  204. if KpdData.get_homeKey() != '':
  205. gen_str += '''\tmediatek,kpd-sw-rstkey = <%d>;\n''' %(KpdData.get_keyVal(KpdData.get_homeKey()))
  206. if KpdData.get_keyType() == 'NORMAL_TYPE':
  207. if KpdData.get_homeKey() != '':
  208. gen_str += '''\tmediatek,kpd-hw-rstkey = <%d>;\n''' %(2*KpdData.get_col() - 1)
  209. gen_str += '''\tmediatek,kpd-use-extend-type = <0>;\n'''
  210. else:
  211. if KpdData.get_homeKey() != '':
  212. gen_str += '''\tmediatek,kpd-hw-rstkey = <%d>;\n''' %(2*KpdData.get_col_ext() - 1)
  213. gen_str += '''\tmediatek,kpd-use-extend-type = <1>;\n'''
  214. #gen_str += '''\tmediatek,kpd-use-extend-type = <0>;\n'''
  215. gen_str += '''\t/*HW Keycode [0~%d] -> Linux Keycode*/\n''' %(KpdData.get_row() * KpdData.get_col() - 1)
  216. gen_str += '''\tmediatek,kpd-hw-map-num = <%d>;\n''' %(KpdData.get_row() * KpdData.get_col())
  217. gen_str += '''\tmediatek,kpd-hw-init-map = <'''
  218. if KpdData.get_keyType() == 'NORMAL_TYPE':
  219. for key in KpdData.get_matrix():
  220. idx = KpdData._keyValueMap[key]
  221. gen_str += '''%d ''' %(idx)
  222. else:
  223. for key in KpdData.get_matrix_ext():
  224. idx = KpdData._keyValueMap[key]
  225. gen_str += '''%d ''' %(idx)
  226. gen_str.rstrip()
  227. gen_str += '''>;\n'''
  228. gen_str += '''\tmediatek,kpd-pwrkey-eint-gpio = <%d>;\n''' %(KpdData.get_gpioNum())
  229. gen_str += '''\tmediatek,kpd-pwkey-gpio-din = <%d>;\n''' %(int(KpdData.get_gpioDinHigh()))
  230. for key in KpdData.get_downloadKeys():
  231. if cmp(key, 'NC') == 0:
  232. continue
  233. gen_str += '''\tmediatek,kpd-hw-dl-key%d = <%s>;\n''' %(KpdData.get_downloadKeys().index(key), self.get_matrixIdx(key))
  234. for (key, value) in KpdData.get_modeKeys().items():
  235. if cmp(value, 'NC') == 0:
  236. continue
  237. gen_str += '''\tmediatek,kpd-hw-%s-key = <%d>;\n''' %(key.lower(), self.get_matrixIdx(value))
  238. gen_str += '''\tstatus = \"okay\";\n'''
  239. gen_str += '''};\n'''
  240. return gen_str