-
Notifications
You must be signed in to change notification settings - Fork 8
/
zhcnkbd.py
323 lines (299 loc) · 12 KB
/
zhcnkbd.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# -*- coding: utf-8 -*-
import os
import sys
import time
import httplib
import xbmc
import xbmcgui
import json
from xbmcswift2 import Plugin
plugin = Plugin()
pgpath = plugin.addon.getAddonInfo('path')
ACTION_PARENT_DIR = 9
ACTION_PREVIOUS_MENU = (10, 92)
ACTION_CONTEXT_MENU = 117
CTRL_ID_BACK = 8
CTRL_ID_SPACE = 32
CTRL_ID_RETN = 300
CTRL_ID_LANG = 302
CTRL_ID_CAPS = 303
CTRL_ID_SYMB = 304
CTRL_ID_LEFT = 305
CTRL_ID_RIGHT = 306
CTRL_ID_IP = 307
CTRL_ID_TEXT = 310
CTRL_ID_HEAD = 311
CTRL_ID_CODE = 401
CTRL_ID_HZLIST = 402
class InputWindow(xbmcgui.WindowXMLDialog):
def __init__( self, *args, **kwargs ):
self.totalpage = 0
self.nowpage = 0
self.words = ''
self.py = ''
self.bg = 0
self.ed = 20
self.wordpgs = [] #word page metadata
self.inputString = kwargs.get("default") or ""
self.heading = kwargs.get("heading") or ""
self.conn = httplib.HTTPConnection('olime.baidu.com')
self.headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) {0}{1}'.format(
'AppleWebKit/537.36 (KHTML, like Gecko) ',
'Chrome/28.0.1500.71 Safari/537.36'),
}
xbmcgui.WindowXMLDialog.__init__(self)
def onInit(self):
self.setKeyToChinese()
self.getControl(CTRL_ID_HEAD).setLabel(self.heading)
self.getControl(CTRL_ID_CODE).setLabel('')
self.getControl(CTRL_ID_TEXT).setLabel(self.inputString)
self.confirmed = False
def onFocus( self, controlId ):
self.controlId = controlId
def onClick( self, controlID ):
if controlID == CTRL_ID_CAPS:#big
self.getControl(CTRL_ID_SYMB).setSelected(False)
if self.getControl(CTRL_ID_CAPS).isSelected():
self.getControl(CTRL_ID_LANG).setSelected(False)
self.setKeyToChinese()
elif controlID == CTRL_ID_IP:#ip
dialog = xbmcgui.Dialog()
value = dialog.numeric( 3, __language__(2), '' )
self.getControl(CTRL_ID_TEXT).setLabel(
self.getControl(CTRL_ID_TEXT).getLabel()+value)
elif controlID == CTRL_ID_SYMB:#num
self.setKeyToChinese()
elif controlID == CTRL_ID_LANG:#en/ch
if self.getControl(CTRL_ID_LANG).isSelected():
self.getControl(CTRL_ID_CAPS).setSelected(False)
self.setKeyToChinese()
elif controlID == CTRL_ID_BACK:#back
if self.getControl(CTRL_ID_LANG).isSelected() and \
len(self.getControl(CTRL_ID_CODE).getLabel())>0:
self.getControl(CTRL_ID_CODE).setLabel(
self.getControl(CTRL_ID_CODE).getLabel()[0:-1])
self.getChineseWord(self.getControl(CTRL_ID_CODE).getLabel())
else:
self.getControl(CTRL_ID_TEXT).setLabel(
self.getControl(CTRL_ID_TEXT).getLabel().
decode("utf-8")[0:-1])
elif controlID == CTRL_ID_RETN:#enter
newText = self.getControl(CTRL_ID_TEXT).getLabel()
if not newText: return
self.inputString = newText
self.confirmed = True
self.close()
elif controlID == CTRL_ID_LEFT:#left
if self.nowpage>0:
self.nowpage -=1
self.changepages()
elif controlID == CTRL_ID_RIGHT:#right
self.nowpage +=1
self.changepages()
elif controlID == CTRL_ID_SPACE:#space
self.getControl(CTRL_ID_TEXT).setLabel(
self.getControl(CTRL_ID_TEXT).getLabel() + ' ')
else:
if self.getControl(CTRL_ID_LANG).isSelected() and \
not self.getControl(CTRL_ID_SYMB).isSelected():
if controlID>=65 and controlID<=90:
s = self.getControl(CTRL_ID_CODE).getLabel() + \
self.getControl(controlID).getLabel().lower()
self.getControl(CTRL_ID_CODE).setLabel(s)
self.getChineseWord(s)
elif controlID>=48 and controlID<=57:#0-9
#i = self.nowpage*(self.wordperpage+1)+(controlID-48)
i = self.wordpgs[self.nowpage][0] + (controlID - 48)
hanzi = self.words[i]
self.getControl(CTRL_ID_TEXT).setLabel(
self.getControl(CTRL_ID_TEXT).getLabel()+ hanzi)
self.getControl(CTRL_ID_CODE).setLabel('')
#Comment out to allow user to reselect the last hzlist
#self.getControl(CTRL_ID_HZLIST).setLabel('')
#self.words = []
else:
self.getControl(CTRL_ID_TEXT).setLabel('%s%s' % (
self.getControl(CTRL_ID_TEXT).getLabel(),
self.getControl(controlID).getLabel().encode('utf-8')))
def onAction(self,action):
keycode = action.getButtonCode()
# xbmc remote keyboard control handler
if keycode >= 61728 and keycode <= 61823:
keychar = chr(keycode - 61728 + ord(' '))
if keychar >='0' and keychar <= '9':
self.onClick(ord(keychar))
elif self.getControl(CTRL_ID_LANG).isSelected():
s = self.getControl(CTRL_ID_CODE).getLabel() + keychar
self.getControl(CTRL_ID_CODE).setLabel(s)
self.getChineseWord(s)
else:
self.getControl(CTRL_ID_TEXT).setLabel(
self.getControl(CTRL_ID_TEXT).getLabel()+keychar)
elif keycode == 61706:
self.onClick(CTRL_ID_RETN)
# Hard keyboard handler
elif keycode >= 61505 and keycode <= 61530: #a-z
if self.getControl(CTRL_ID_LANG).isSelected():
keychar = chr(keycode - 61505 + ord('a'))
s = self.getControl(CTRL_ID_CODE).getLabel() + keychar
self.getControl(CTRL_ID_CODE).setLabel(s)
self.getChineseWord(s)
else:
if self.getControl(CTRL_ID_CAPS).isSelected():
keychar = chr(keycode - 61505 + ord('A')) #A-Z
else:
keychar = chr(keycode - 61505 + ord('a'))
self.getControl(CTRL_ID_TEXT).setLabel(
self.getControl(CTRL_ID_TEXT).getLabel()+keychar)
#0-9(Eden)-no overlapping code with Dharma
elif keycode >= 61488 and keycode <= 61497:
self.onClick( keycode-61488+48 )
#0-9(Dharma)-no overlapping code with Eden
elif keycode >= 61536 and keycode <= 61545:
self.onClick( keycode-61536+48 )
#Eden & Dharma scancode difference
elif keycode == 61500 or keycode == 192700:
self.onClick( CTRL_ID_LEFT ) # <
#Eden & Dharma scancode difference
elif keycode == 61502 or keycode == 192702:
self.onClick( CTRL_ID_RIGHT ) # >
elif keycode == 61472:
self.onClick( CTRL_ID_SPACE )
elif keycode == 61448:
self.onClick( CTRL_ID_BACK )
elif action.getId() in ACTION_PREVIOUS_MENU:
self.close()
def changepages (self):
self.getControl(CTRL_ID_HZLIST).setLabel('')
hzlist = ''
if not self.wordpgs: return
if self.nowpage >= self.totalpage-1:
self.bg += 20
self.ed += 20
self.getChineseWord(self.py, self.bg, self.ed)
return
curwpg = self.wordpgs[self.nowpage]
for i, w in enumerate(self.words[curwpg[0]:curwpg[1]]):
hzlist = hzlist + str(i) + '.' + w +' '
if self.nowpage > 0:
hzlist = '<' + hzlist
if self.nowpage < self.totalpage-1:
hzlist = hzlist + '>'
self.getControl(CTRL_ID_HZLIST).setLabel(hzlist)
def getChineseWord (self, py, bg=0, ed=20):
self.getControl(CTRL_ID_HZLIST).setLabel('')
#if HANZI_MB.has_key(py):
if py=='': return
if not bg:
self.nowpage = 0
self.totalpage = 0
self.wordpgs = []
self.words= []
self.py = py
self.bg = 0
self.ed = 20
wres = self.getwords(py, bg, ed)
if wres:
self.words.extend(wres)
self.wordpgs = []
inum = 0
for s, w in enumerate(self.words):
if len(''.join(self.words[inum:s+1]).decode('utf-8')) + (
s+1-inum)*2 >30:
self.wordpgs.append((inum, s))
inum = s
if len(self.words) > inum:
self.wordpgs.append((inum, len(self.words)))
self.totalpage = len(self.wordpgs)
else:
self.nowpage = self.totalpage - 1
hzlist = ''
curwpg = self.wordpgs[self.nowpage]
for i, w in enumerate(self.words[curwpg[0]:curwpg[1]]):
hzlist = hzlist + str(i) + '.' + w +' '
if self.nowpage > 0:
hzlist = '<' + hzlist
if self.nowpage < self.totalpage-1:
hzlist = hzlist + '>'
self.getControl(CTRL_ID_HZLIST).setLabel(hzlist)
def getwords(self, py, bg, ed):
t = time.time()
urlsuf = '/py?input={0}&inputtype=py&bg={1}&ed={2}&{3}'.format(
py, bg, ed,
'result=hanzi&resultcoding=unicode&ch_en=0&clientinfo=web')
try:
self.conn.request('GET', urlsuf, headers=self.headers)
except Exception as e:
self.conn = httplib.HTTPConnection('olime.baidu.com')
self.conn.request('GET', urlsuf, headers=self.headers)
rsp = self.conn.getresponse()
cookie = rsp.getheader('Set-Cookie', '')
if 'Cookie' not in self.headers and cookie:
self.headers['Cookie'] = cookie
httpdata = rsp.read()
words = []
try:
jsondata = json.loads(httpdata)
except ValueError:
return None
for word in jsondata[0]:
words.append(word[0].encode('utf-8'))
#print match, words
return words
def setKeyToChinese (self):
self.getControl(CTRL_ID_CODE).setLabel('')
if self.getControl(CTRL_ID_SYMB).isSelected():
i = 48
for c in ')!@#$%^&*(':
self.getControl(i).setLabel(c)
i+=1
if i > 57: break
i = 65
for c in '[]{}-_=+;:\'",.<>/?\\|`~':
self.getControl(i).setLabel(c)
i+=1
if i > 90: break
for j in range(i,90+1):
self.getControl(j).setLabel('')
else:
for i in range(48, 57+1):
keychar = chr(i - 48 + ord('0'))
self.getControl(i).setLabel(keychar)
if self.getControl(CTRL_ID_CAPS).isSelected():
for i in range(65, 90+1):
keychar = chr(i - 65 + ord('A'))
self.getControl(i).setLabel(keychar)
else:
for i in range(65, 90+1):
keychar = chr(i - 65 + ord('a'))
self.getControl(i).setLabel(keychar)
if self.getControl(CTRL_ID_LANG).isSelected():
self.getControl(400).setVisible(True)
self.nowpage = 0
self.changepages()
else:
self.getControl(400).setVisible(False)
def isConfirmed(self):
return self.confirmed
def getText(self):
return self.inputString
class Keyboard:
def __init__( self, default='', heading='' ):
self.confirmed = False
self.inputString = default
self.heading = heading
def doModal (self):
self.win = InputWindow(
"DialogKeyboardChinese.xml", pgpath, defaultSkin='Default',
heading=self.heading, default=self.inputString)
self.win.doModal()
self.confirmed = self.win.isConfirmed()
self.inputString = self.win.getText()
del self.win
def setHeading(self, heading):
self.heading = heading
def isConfirmed(self):
return self.confirmed
def getText(self):
return self.inputString