Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 303 lines (271 sloc) 10.295 kB
dc12090 @temoto Initial commit
authored
1 from ctypes import *
2
3 _libraries = {}
4 _libraries['libtermkey.so.0'] = CDLL('libtermkey.so.0')
5 STRING = c_char_p
6
7 # values for enumeration 'termkey_sym'
8 TERMKEY_SYM_UNKNOWN = -1
9 TERMKEY_SYM_NONE = 0
10 TERMKEY_SYM_BACKSPACE = 1
11 TERMKEY_SYM_TAB = 2
12 TERMKEY_SYM_ENTER = 3
13 TERMKEY_SYM_ESCAPE = 4
14 TERMKEY_SYM_SPACE = 5
15 TERMKEY_SYM_DEL = 6
16 TERMKEY_SYM_UP = 7
17 TERMKEY_SYM_DOWN = 8
18 TERMKEY_SYM_LEFT = 9
19 TERMKEY_SYM_RIGHT = 10
20 TERMKEY_SYM_BEGIN = 11
21 TERMKEY_SYM_FIND = 12
22 TERMKEY_SYM_INSERT = 13
23 TERMKEY_SYM_DELETE = 14
24 TERMKEY_SYM_SELECT = 15
25 TERMKEY_SYM_PAGEUP = 16
26 TERMKEY_SYM_PAGEDOWN = 17
27 TERMKEY_SYM_HOME = 18
28 TERMKEY_SYM_END = 19
29 TERMKEY_SYM_CANCEL = 20
30 TERMKEY_SYM_CLEAR = 21
31 TERMKEY_SYM_CLOSE = 22
32 TERMKEY_SYM_COMMAND = 23
33 TERMKEY_SYM_COPY = 24
34 TERMKEY_SYM_EXIT = 25
35 TERMKEY_SYM_HELP = 26
36 TERMKEY_SYM_MARK = 27
37 TERMKEY_SYM_MESSAGE = 28
38 TERMKEY_SYM_MOVE = 29
39 TERMKEY_SYM_OPEN = 30
40 TERMKEY_SYM_OPTIONS = 31
41 TERMKEY_SYM_PRINT = 32
42 TERMKEY_SYM_REDO = 33
43 TERMKEY_SYM_REFERENCE = 34
44 TERMKEY_SYM_REFRESH = 35
45 TERMKEY_SYM_REPLACE = 36
46 TERMKEY_SYM_RESTART = 37
47 TERMKEY_SYM_RESUME = 38
48 TERMKEY_SYM_SAVE = 39
49 TERMKEY_SYM_SUSPEND = 40
50 TERMKEY_SYM_UNDO = 41
51 TERMKEY_SYM_KP0 = 42
52 TERMKEY_SYM_KP1 = 43
53 TERMKEY_SYM_KP2 = 44
54 TERMKEY_SYM_KP3 = 45
55 TERMKEY_SYM_KP4 = 46
56 TERMKEY_SYM_KP5 = 47
57 TERMKEY_SYM_KP6 = 48
58 TERMKEY_SYM_KP7 = 49
59 TERMKEY_SYM_KP8 = 50
60 TERMKEY_SYM_KP9 = 51
61 TERMKEY_SYM_KPENTER = 52
62 TERMKEY_SYM_KPPLUS = 53
63 TERMKEY_SYM_KPMINUS = 54
64 TERMKEY_SYM_KPMULT = 55
65 TERMKEY_SYM_KPDIV = 56
66 TERMKEY_SYM_KPCOMMA = 57
67 TERMKEY_SYM_KPPERIOD = 58
68 TERMKEY_SYM_KPEQUALS = 59
69 termkey_sym = c_int # enum
70
71 # values for enumeration 'termkey_type'
72 TERMKEY_TYPE_UNICODE = 0
73 TERMKEY_TYPE_FUNCTION = 1
74 TERMKEY_TYPE_KEYSYM = 2
75 termkey_type = c_int # enum
76
77 # values for enumeration 'termkey_result'
78 TERMKEY_RES_NONE = 0
79 TERMKEY_RES_KEY = 1
80 TERMKEY_RES_EOF = 2
81 TERMKEY_RES_AGAIN = 3
82 termkey_result = c_int # enum
83 termkey_keysym = c_int
84 class termkey(Structure):
85 pass
86 termkey_t = termkey
87 termkey_check_version = _libraries['libtermkey.so.0'].termkey_check_version
88 termkey_check_version.restype = None
89 termkey_check_version.argtypes = [c_int, c_int]
90 termkey_new = _libraries['libtermkey.so.0'].termkey_new
91 termkey_new.restype = POINTER(termkey_t)
92 termkey_new.argtypes = [c_int, c_int]
93 termkey_free = _libraries['libtermkey.so.0'].termkey_free
94 termkey_free.restype = None
95 termkey_free.argtypes = [POINTER(termkey_t)]
96 termkey_destroy = _libraries['libtermkey.so.0'].termkey_destroy
97 termkey_destroy.restype = None
98 termkey_destroy.argtypes = [POINTER(termkey_t)]
99 termkey_get_flags = _libraries['libtermkey.so.0'].termkey_get_flags
100 termkey_get_flags.restype = c_int
101 termkey_get_flags.argtypes = [POINTER(termkey_t)]
102 termkey_set_flags = _libraries['libtermkey.so.0'].termkey_set_flags
103 termkey_set_flags.restype = None
104 termkey_set_flags.argtypes = [POINTER(termkey_t), c_int]
105 termkey_set_waittime = _libraries['libtermkey.so.0'].termkey_set_waittime
106 termkey_set_waittime.restype = None
107 termkey_set_waittime.argtypes = [POINTER(termkey_t), c_int]
108 termkey_get_waittime = _libraries['libtermkey.so.0'].termkey_get_waittime
109 termkey_get_waittime.restype = c_int
110 termkey_get_waittime.argtypes = [POINTER(termkey_t)]
111 class termkey_key(Structure):
112 pass
113 termkey_getkey = _libraries['libtermkey.so.0'].termkey_getkey
114 termkey_getkey.restype = termkey_result
115 termkey_getkey.argtypes = [POINTER(termkey_t), POINTER(termkey_key)]
116 termkey_getkey_force = _libraries['libtermkey.so.0'].termkey_getkey_force
117 termkey_getkey_force.restype = termkey_result
118 termkey_getkey_force.argtypes = [POINTER(termkey_t), POINTER(termkey_key)]
119 termkey_waitkey = _libraries['libtermkey.so.0'].termkey_waitkey
120 termkey_waitkey.restype = termkey_result
121 termkey_waitkey.argtypes = [POINTER(termkey_t), POINTER(termkey_key)]
122 size_t = c_ulong
123 termkey_pushinput = _libraries['libtermkey.so.0'].termkey_pushinput
124 termkey_pushinput.restype = None
125 termkey_pushinput.argtypes = [POINTER(termkey_t), POINTER(c_ubyte), size_t]
126 termkey_advisereadable = _libraries['libtermkey.so.0'].termkey_advisereadable
127 termkey_advisereadable.restype = termkey_result
128 termkey_advisereadable.argtypes = [POINTER(termkey_t)]
129 termkey_register_keyname = _libraries['libtermkey.so.0'].termkey_register_keyname
130 termkey_register_keyname.restype = termkey_keysym
131 termkey_register_keyname.argtypes = [POINTER(termkey_t), termkey_keysym, STRING]
132 termkey_get_keyname = _libraries['libtermkey.so.0'].termkey_get_keyname
133 termkey_get_keyname.restype = STRING
134 termkey_get_keyname.argtypes = [POINTER(termkey_t), termkey_keysym]
135
136 # values for enumeration 'termkey_format'
137 TERMKEY_FORMAT_LONGMOD = 1
138 TERMKEY_FORMAT_CARETCTRL = 2
139 TERMKEY_FORMAT_ALTISMETA = 4
140 TERMKEY_FORMAT_WRAPBRACKET = 8
141 termkey_format = c_int # enum
142 termkey_snprint_key = _libraries['libtermkey.so.0'].termkey_snprint_key
143 termkey_snprint_key.restype = size_t
144 termkey_snprint_key.argtypes = [POINTER(termkey_t), STRING, size_t, POINTER(termkey_key), termkey_format]
145 termkey_key._fields_ = [
146 ('type_', termkey_type),
147 ('code', c_ulong),
148 ('modifiers', c_int),
149 ('utf8', c_char * 7),
150 ]
151 termkey._fields_ = [
152 ]
153
154
155 # shortcuts over ctypes code generation
156 class Result(object):
157 NONE = 0
158 KEY = 1
159 EOF = 2
160 AGAIN = 3
161
162
163 class KeyType(object):
164 UNICODE = 0
165 FUNCTION = 1
166 KEYSYM = 2
167
168
169 class KeyModifier(object):
170 SHIFT = 1 << 0
171 ALT = 1 << 1
172 CTRL = 1 << 2
173
174
175 def check_termkey_pointer(func):
176 def wrapped(self, *args, **kwargs):
177 if not self._tk:
178 raise Exception("TermKey is not initialized. Create another TermKey object.")
179 return func(self, *args, **kwargs)
180 return wrapped
181
182
183 class TermKey(object):
184 def __init__(self, fd=0, flags=0):
185 self.sprint_buffer_size = 50
186 self._tk = termkey_new(fd, flags)
187
188 def __del__(self):
189 self.close()
190
191 def close(self):
192 if self._tk:
193 termkey_destroy(self._tk)
194 self._tk = None
195
196 @check_termkey_pointer
197 def set_waittime(self, time):
198 termkey_set_waittime(self._tk, time)
199
200 @check_termkey_pointer
201 def get_waittime(self):
202 return termkey_get_waittime(self._tk)
203
204 @check_termkey_pointer
205 def get_flags(self):
206 return termkey_get_flags(self._tk)
207
208 @check_termkey_pointer
209 def set_flags(self, new_flags):
210 termkey_set_flags(self, new_flags)
211
212 @check_termkey_pointer
213 def getkey(self):
214 new_key = termkey_key()
215 result = termkey_getkey(self._tk, new_key)
216 return result, new_key
217
218 @check_termkey_pointer
219 def getkey_force(self):
220 new_key = termkey_key()
221 result = termkey_getkey_force(self._tk, new_key)
222 return result, new_key
223
224 @check_termkey_pointer
225 def waitkey(self):
226 new_key = termkey_key()
227 result = termkey_waitkey(self._tk, new_key)
228 return result, new_key
229
230 @check_termkey_pointer
231 def pushinput(self, input_):
232 inputlen = len(input_)
233 termkey_pushinput(self._tk, input_, inputlen)
234
235 @check_termkey_pointer
236 def advisereadable(self):
237 return termkey_advisereadable(self._tk)
238
239 @check_termkey_pointer
240 def termkey_register_keyname(self, sym, name):
241 return termkey_register_keyname(self._tk, sym, name)
242
243 @check_termkey_pointer
244 def termkey_get_keyname(self, sym):
245 return termkey_get_keyname(self._tk, sym)
246
247 @check_termkey_pointer
248 def sprint_key(self, key, format=0):
249 buf = '\0' * self.sprint_buffer_size
250 real_size = termkey_snprint_key(self._tk, buf, len(buf), key, format)
251 if real_size < self.sprint_buffer_size:
252 buf = buf[:real_size]
253 return buf
254 else:
255 raise Exception("Key sprint value doesn't fit %d bytes"
256 % (self.sprint_buffer_size,))
257
258
259 __all__ = ['TERMKEY_SYM_REFRESH', 'TERMKEY_SYM_PAGEUP',
260 'TERMKEY_SYM_PAGEDOWN', 'size_t', 'TERMKEY_SYM_DELETE',
261 'termkey_register_keyname', 'termkey_set_flags',
262 'TERMKEY_SYM_RESUME', 'TERMKEY_SYM_REDO',
263 'TERMKEY_SYM_COPY', 'TERMKEY_SYM_SUSPEND',
264 'TERMKEY_SYM_COMMAND', 'TERMKEY_RES_KEY',
265 'TERMKEY_FORMAT_LONGMOD', 'TERMKEY_SYM_UNDO',
266 'TERMKEY_SYM_HELP', 'termkey_keysym',
267 'TERMKEY_TYPE_KEYSYM', 'TERMKEY_RES_NONE',
268 'termkey_get_flags', 'TERMKEY_SYM_UP', 'TERMKEY_SYM_DOWN',
269 'TERMKEY_SYM_OPTIONS', 'TERMKEY_SYM_BEGIN',
270 'TERMKEY_SYM_NONE', 'termkey_set_waittime',
271 'termkey_destroy', 'termkey_waitkey',
272 'termkey_advisereadable', 'TERMKEY_TYPE_FUNCTION',
273 'TERMKEY_RES_AGAIN', 'TERMKEY_SYM_RIGHT',
274 'termkey_pushinput', 'termkey_check_version',
275 'TERMKEY_SYM_KP9', 'TERMKEY_SYM_REFERENCE', 'termkey_key',
276 'TERMKEY_SYM_REPLACE', 'TERMKEY_SYM_ESCAPE', 'termkey_sym',
277 'TERMKEY_FORMAT_ALTISMETA', 'TERMKEY_SYM_MARK',
278 'termkey_format', 'TERMKEY_RES_EOF', 'TERMKEY_SYM_LEFT',
279 'TERMKEY_SYM_CLEAR', 'TERMKEY_SYM_INSERT',
280 'TERMKEY_SYM_KPEQUALS', 'TERMKEY_SYM_KPENTER',
281 'termkey_getkey_force', 'TERMKEY_SYM_KPMULT',
282 'TERMKEY_SYM_KPPERIOD', 'TERMKEY_SYM_KPPLUS',
283 'TERMKEY_FORMAT_CARETCTRL', 'termkey_new',
284 'TERMKEY_SYM_MESSAGE', 'termkey_get_waittime',
285 'TERMKEY_SYM_OPEN', 'TERMKEY_SYM_MOVE', 'termkey_getkey',
286 'TERMKEY_SYM_KP8', 'termkey_result', 'TERMKEY_SYM_KP6',
287 'TERMKEY_SYM_KP7', 'TERMKEY_SYM_KP4', 'TERMKEY_SYM_KP5',
288 'TERMKEY_SYM_KP2', 'TERMKEY_SYM_KP3', 'TERMKEY_SYM_KP0',
289 'TERMKEY_SYM_KP1', 'TERMKEY_SYM_RESTART',
290 'TERMKEY_SYM_CANCEL', 'termkey_type',
291 'termkey_snprint_key', 'termkey_free',
292 'TERMKEY_SYM_KPMINUS', 'termkey_t', 'TERMKEY_SYM_SAVE',
293 'TERMKEY_SYM_END', 'TERMKEY_SYM_SPACE',
294 'TERMKEY_SYM_PRINT', 'TERMKEY_SYM_HOME',
295 'TERMKEY_SYM_KPDIV', 'termkey_get_keyname',
296 'TERMKEY_SYM_BACKSPACE', 'TERMKEY_FORMAT_WRAPBRACKET',
297 'TERMKEY_SYM_UNKNOWN', 'TERMKEY_SYM_TAB',
298 'TERMKEY_SYM_DEL', 'TERMKEY_SYM_CLOSE',
299 'TERMKEY_SYM_SELECT', 'TERMKEY_SYM_EXIT', 'termkey',
300 'TERMKEY_SYM_FIND', 'TERMKEY_TYPE_UNICODE',
301 'TERMKEY_SYM_ENTER', 'TERMKEY_SYM_KPCOMMA',
302 'waitkey']
Something went wrong with that request. Please try again.