author | Radek Brich <radek.brich@devl.cz> |
Wed, 16 Mar 2011 15:19:05 +0100 | |
changeset 3 | 33ec838dc021 |
parent 2 | 684cdc352562 |
child 4 | d197ca00496f |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
||
3 |
import curses |
|
4 |
import curses.ascii |
|
5 |
import locale |
|
6 |
import logging |
|
7 |
||
8 |
from .common import Rect |
|
9 |
||
10 |
||
11 |
class MouseEvent: |
|
12 |
def __init__(self, x=0, y=0): |
|
13 |
self.x = x # global coordinates |
|
14 |
self.y = y |
|
15 |
self.wx = x # local widget coordinates |
|
16 |
self.wy = y |
|
17 |
self.px = 0 # parent coordinates |
|
18 |
self.py = 0 |
|
19 |
self.button = 0 |
|
20 |
||
21 |
||
22 |
def childevent(self, child): |
|
23 |
ev = MouseEvent(self.x, self.y) |
|
24 |
# original local coordinates are new parent coordinates |
|
25 |
ev.px = self.wx |
|
26 |
ev.py = self.wy |
|
27 |
# update local coordinates |
|
28 |
ev.wx = self.wx - child.x |
|
29 |
ev.wy = self.wy - child.y |
|
30 |
||
31 |
return ev |
|
32 |
||
33 |
||
34 |
class BackendCurses: |
|
35 |
xterm_codes = ( |
|
36 |
(0x09, 'tab' ), |
|
37 |
(0x0a, 'enter' ), |
|
38 |
(0x7f, 'backspace' ), |
|
39 |
(0x1b,0x4f,0x50, 'f1' ), |
|
40 |
(0x1b,0x4f,0x51, 'f2' ), |
|
41 |
(0x1b,0x4f,0x52, 'f3' ), |
|
42 |
(0x1b,0x4f,0x53, 'f4' ), |
|
43 |
(0x1b,0x5b,0x4d, 'mouse' ), |
|
44 |
(0x1b,0x5b,0x41, 'up' ), |
|
45 |
(0x1b,0x5b,0x42, 'down' ), |
|
46 |
(0x1b,0x5b,0x43, 'right' ), |
|
47 |
(0x1b,0x5b,0x44, 'left' ), |
|
48 |
(0x1b,0x5b,0x31,0x35,0x7e, 'f5' ), |
|
49 |
(0x1b,0x5b,0x31,0x37,0x7e, 'f6' ), |
|
50 |
(0x1b,0x5b,0x31,0x38,0x7e, 'f7' ), |
|
51 |
(0x1b,0x5b,0x31,0x39,0x7e, 'f8' ), |
|
52 |
(0x1b,0x5b,0x32,0x30,0x7e, 'f9' ), |
|
53 |
(0x1b,0x5b,0x32,0x31,0x7e, 'f10' ), |
|
54 |
(0x1b,0x5b,0x32,0x33,0x7e, 'f11' ), |
|
55 |
(0x1b,0x5b,0x32,0x34,0x7e, 'f12' ), |
|
56 |
(0x1b,0x5b,0x32,0x7e, 'insert' ), |
|
57 |
(0x1b,0x5b,0x33,0x7e, 'delete' ), |
|
58 |
(0x1b,0x5b,0x35,0x7e, 'pageup' ), |
|
59 |
(0x1b,0x5b,0x36,0x7e, 'pagedown' ), |
|
60 |
(0x1b,0x5b,0x46, 'end' ), |
|
61 |
(0x1b,0x5b,0x48, 'home' ), |
|
62 |
(0x1b, 'escape' ), |
|
63 |
) |
|
64 |
||
65 |
def __init__(self, screen): |
|
66 |
self.screen = screen |
|
67 |
self.height, self.width = screen.getmaxyx() |
|
68 |
||
69 |
self.clipstack = [] |
|
70 |
self.colorstack = [] |
|
71 |
self.inputqueue = [] |
|
72 |
self.mbtnstack = [] |
|
73 |
||
74 |
self.log = logging.getLogger('tuikit') |
|
75 |
||
76 |
# initialize curses |
|
77 |
curses.curs_set(False) |
|
78 |
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION) |
|
79 |
curses.mouseinterval(0) # do not wait to detect clicks, we use only press/release |
|
80 |
||
81 |
screen.immedok(0) |
|
82 |
screen.keypad(0) |
|
83 |
||
84 |
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) |
|
85 |
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_CYAN) |
|
86 |
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_CYAN) |
|
2
684cdc352562
Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
87 |
curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_BLUE) |
684cdc352562
Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
88 |
curses.init_pair(5, curses.COLOR_CYAN, curses.COLOR_BLUE) |
0 | 89 |
|
90 |
self.BOLD = curses.A_BOLD |
|
91 |
self.BLINK = curses.A_BLINK |
|
92 |
self.UNDERLINE = curses.A_UNDERLINE |
|
93 |
||
94 |
# http://en.wikipedia.org/wiki/List_of_Unicode_characters#Geometric_shapes |
|
95 |
self.UP_ARROW = '▲' #curses.ACS_UARROW |
|
96 |
self.DOWN_ARROW = '▼' #curses.ACS_DARROW |
|
97 |
||
98 |
# http://en.wikipedia.org/wiki/Box-drawing_characters |
|
99 |
self.LIGHT_SHADE = '░' #curses.ACS_BOARD |
|
100 |
self.MEDIUM_SHADE = '▒' |
|
101 |
self.DARK_SHADE = '▓' |
|
102 |
self.BLOCK = '█' |
|
103 |
||
104 |
self.COLUMN = '▁▂▃▄▅▆▇█' |
|
105 |
self.CORNER_ROUND = '╭╮╰╯' |
|
106 |
self.CORNER = '┌┐└┘' |
|
107 |
self.LINE = '─━│┃┄┅┆┇┈┉┊┋' |
|
108 |
||
109 |
self.HLINE = '─' # curses.ACS_HLINE |
|
110 |
self.VLINE = '│' # curses.ACS_VLINE |
|
111 |
self.ULCORNER = '┌' # curses.ACS_ULCORNER |
|
112 |
self.URCORNER = '┐' # curses.ACS_URCORNER |
|
113 |
self.LLCORNER = '└' # curses.ACS_LLCORNER |
|
114 |
self.LRCORNER = '┘' # curses.ACS_LRCORNER |
|
115 |
self.LTEE = '├' |
|
116 |
self.RTEE = '┤' |
|
117 |
||
118 |
||
119 |
## clip operations ## |
|
120 |
||
121 |
def pushclip(self, x, y, w, h): |
|
122 |
newclip = Rect(x, y, w, h) |
|
123 |
if len(self.clipstack): |
|
124 |
oldclip = self.clipstack[-1] |
|
125 |
newclip = self.intersect(oldclip, newclip) |
|
126 |
self.clipstack.append(newclip) |
|
127 |
||
128 |
||
129 |
def popclip(self): |
|
130 |
self.clipstack.pop() |
|
131 |
||
132 |
||
133 |
def testclip(self, x, y): |
|
134 |
# no clip rectangle on stack => passed |
|
135 |
if not len(self.clipstack): |
|
136 |
return True |
|
137 |
# test against top clip rect from stack |
|
138 |
clip = self.clipstack[-1] |
|
139 |
if x < clip.x or y < clip.y \ |
|
140 |
or x >= clip.x + clip.w or y >= clip.y + clip.h: |
|
141 |
return False |
|
142 |
# passed |
|
143 |
return True |
|
144 |
||
145 |
||
146 |
def intersect(self, r1, r2): |
|
147 |
x1 = max(r1.x, r2.x) |
|
148 |
y1 = max(r1.y, r2.y) |
|
149 |
x2 = min(r1.x + r1.w, r2.x + r2.w) |
|
150 |
y2 = min(r1.y + r1.h, r2.y + r2.h) |
|
151 |
if x1 >= x2 or y1 >= y2: |
|
152 |
return Rect() |
|
153 |
return Rect(x1, y1, x2-x1, y2-y1) |
|
154 |
||
155 |
||
156 |
def union(self, r1, r2): |
|
157 |
x = min(r1.x, r2.x) |
|
158 |
y = min(r1.y, r2.y) |
|
159 |
w = max(r1.x + r1.w, r2.x + r2.w) - x |
|
160 |
h = max(r1.y + r1.h, r2.y + r2.h) - y |
|
161 |
return Rect(x, y, w, h) |
|
162 |
||
163 |
||
164 |
## attributes ## |
|
165 |
||
166 |
def pushcolor(self, col, attr=0): |
|
2
684cdc352562
Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
167 |
if type(col) is tuple: |
684cdc352562
Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
168 |
col, attr = col |
684cdc352562
Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
169 |
if attr == 'bold': |
684cdc352562
Menu, Window and other improvements.
Radek Brich <radek.brich@devl.cz>
parents:
0
diff
changeset
|
170 |
attr = self.BOLD |
0 | 171 |
self.screen.attrset(curses.color_pair(col) | attr) |
172 |
self.colorstack.append((col, attr)) |
|
173 |
||
174 |
||
175 |
def popcolor(self): |
|
176 |
self.colorstack.pop() |
|
177 |
if len(self.colorstack): |
|
178 |
col, attr = self.colorstack[-1] |
|
179 |
else: |
|
180 |
col, attr = 0, 0 |
|
181 |
self.screen.attrset(curses.color_pair(col) | attr) |
|
182 |
||
183 |
||
184 |
## drawing ## |
|
185 |
||
186 |
def putch(self, x, y, c): |
|
187 |
if not self.testclip(x, y): |
|
188 |
return |
|
189 |
try: |
|
190 |
if type(c) is str and len(c) == 1: |
|
191 |
self.screen.addstr(y, x, c) |
|
192 |
else: |
|
193 |
self.screen.addch(y, x, c) |
|
194 |
except curses.error: |
|
195 |
pass |
|
196 |
||
197 |
||
198 |
def puts(self, x, y, s): |
|
199 |
for c in s: |
|
200 |
self.putch(x, y, c) |
|
201 |
x += 1 |
|
202 |
||
203 |
||
204 |
def hline(self, x, y, w, c=' '): |
|
205 |
if type(c) is str: |
|
206 |
s = c*w |
|
207 |
else: |
|
208 |
s = [c]*w |
|
209 |
self.puts(x, y, s) |
|
210 |
||
211 |
||
212 |
def vline(self, x, y, h, c=' '): |
|
213 |
for i in range(h): |
|
214 |
self.putch(x, y+i, c) |
|
215 |
||
216 |
||
217 |
def frame(self, x, y, w, h): |
|
218 |
self.putch(x, y, self.ULCORNER) |
|
219 |
self.putch(x+w-1, y, self.URCORNER) |
|
220 |
self.putch(x, y+h-1, self.LLCORNER) |
|
221 |
self.putch(x+w-1, y+h-1, self.LRCORNER) |
|
222 |
self.hline(x+1, y, w-2, self.HLINE) |
|
223 |
self.hline(x+1, y+h-1, w-2, self.HLINE) |
|
224 |
self.vline(x, y+1, h-2, self.VLINE) |
|
225 |
self.vline(x+w-1, y+1, h-2, self.VLINE) |
|
226 |
||
227 |
||
228 |
def fill(self, x, y, w, h, c=' '): |
|
229 |
for i in range(h): |
|
230 |
self.hline(x, y + i, w, c) |
|
231 |
||
232 |
||
233 |
def erase(self): |
|
234 |
curses.curs_set(False) |
|
235 |
self.cursor = None |
|
236 |
self.screen.erase() |
|
237 |
||
238 |
||
239 |
def commit(self): |
|
240 |
if self.cursor: |
|
241 |
self.screen.move(*self.cursor) |
|
242 |
curses.curs_set(True) |
|
243 |
else: |
|
244 |
curses.curs_set(False) |
|
245 |
self.screen.refresh() |
|
246 |
||
247 |
||
248 |
## cursor ## |
|
249 |
||
250 |
def showcursor(self, x, y): |
|
251 |
if not self.testclip(x, y): |
|
252 |
return |
|
253 |
self.cursor = (y, x) |
|
254 |
||
255 |
||
256 |
## input ## |
|
257 |
||
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
258 |
def inputqueue_fill(self, timeout=None): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
259 |
if timeout is None: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
260 |
# wait indefinitely |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
261 |
c = self.screen.getch() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
262 |
self.inputqueue.insert(0, c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
263 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
264 |
elif timeout > 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
265 |
# wait |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
266 |
curses.halfdelay(timeout) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
267 |
c = self.screen.getch() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
268 |
curses.cbreak() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
269 |
if c == -1: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
270 |
return |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
271 |
self.inputqueue.insert(0, c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
272 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
273 |
# timeout = 0 -> no wait |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
274 |
|
0 | 275 |
self.screen.nodelay(1) |
276 |
||
277 |
while True: |
|
278 |
c = self.screen.getch() |
|
279 |
if c == -1: |
|
280 |
break |
|
281 |
self.inputqueue.insert(0, c) |
|
282 |
||
283 |
self.screen.nodelay(0) |
|
284 |
||
285 |
||
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
286 |
def inputqueue_top(self, num=0): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
287 |
return self.inputqueue[-1-num] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
288 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
289 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
290 |
def inputqueue_get(self): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
291 |
c = None |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
292 |
try: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
293 |
c = self.inputqueue.pop() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
294 |
except IndexError: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
295 |
pass |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
296 |
return c |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
297 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
298 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
299 |
def inputqueue_get_wait(self): |
0 | 300 |
c = None |
301 |
while c is None: |
|
302 |
try: |
|
303 |
c = self.inputqueue.pop() |
|
304 |
except IndexError: |
|
305 |
curses.napms(25) |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
306 |
self.inputqueue_fill(0) |
0 | 307 |
return c |
308 |
||
309 |
||
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
310 |
def inputqueue_unget(self, c): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
311 |
self.inputqueue.append(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
312 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
313 |
|
0 | 314 |
def process_input(self, timeout=None): |
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
315 |
# empty queue -> fill |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
316 |
if len(self.inputqueue) == 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
317 |
self.inputqueue_fill(timeout) |
0 | 318 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
319 |
res = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
320 |
while len(self.inputqueue): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
321 |
c = self.inputqueue_get() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
322 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
323 |
if c == curses.KEY_MOUSE: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
324 |
res += self.process_mouse() |
0 | 325 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
326 |
elif curses.ascii.isctrl(c): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
327 |
self.inputqueue_unget(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
328 |
res += self.process_control_chars() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
329 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
330 |
elif c >= 192 and c <= 255: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
331 |
self.inputqueue_unget(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
332 |
res += self.process_utf8_chars() |
0 | 333 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
334 |
elif curses.ascii.isprint(c): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
335 |
res += [('keypress', None, str(chr(c)))] |
0 | 336 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
337 |
else: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
338 |
#self.top.keypress(None, unicode(chr(c))) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
339 |
self.inputqueue_unget(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
340 |
res += self.process_control_chars() |
0 | 341 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
342 |
return res |
0 | 343 |
|
344 |
||
345 |
def process_mouse(self): |
|
346 |
id, x, y, z, bstate = curses.getmouse() |
|
347 |
ev = MouseEvent(x, y) |
|
348 |
||
349 |
out = [] |
|
350 |
||
351 |
if bstate & curses.REPORT_MOUSE_POSITION: |
|
352 |
out += [('mousemove', ev)] |
|
353 |
||
354 |
if bstate & curses.BUTTON1_PRESSED: |
|
355 |
ev.button = 1 |
|
356 |
out += [('mousedown', ev)] |
|
357 |
||
358 |
if bstate & curses.BUTTON3_PRESSED: |
|
359 |
ev.button = 3 |
|
360 |
out += [('mousedown', ev)] |
|
361 |
||
362 |
if bstate & curses.BUTTON1_RELEASED: |
|
363 |
ev.button = 1 |
|
364 |
out += [('mouseup', ev)] |
|
365 |
||
366 |
if bstate & curses.BUTTON3_RELEASED: |
|
367 |
ev.button = 3 |
|
368 |
out += [('mouseup', ev)] |
|
369 |
||
370 |
return out |
|
371 |
||
372 |
||
373 |
def process_utf8_chars(self): |
|
374 |
#FIXME read exact number of chars as defined by utf-8 |
|
375 |
utf = '' |
|
376 |
while len(utf) <= 6: |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
377 |
c = self.inputqueue_get_wait() |
0 | 378 |
utf += chr(c) |
379 |
try: |
|
380 |
uni = str(utf, 'utf-8') |
|
381 |
return [('keypress', None, uni)] |
|
382 |
except UnicodeDecodeError: |
|
383 |
continue |
|
384 |
raise Exception('Invalid UTF-8 sequence: %r' % utf) |
|
385 |
||
386 |
||
387 |
def process_control_chars(self): |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
388 |
codes = self.xterm_codes |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
389 |
matchingcodes = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
390 |
match = None |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
391 |
consumed = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
392 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
393 |
# consume next char, filter out matching codes |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
394 |
c = self.inputqueue_get_wait() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
395 |
consumed.append(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
396 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
397 |
while True: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
398 |
self.log.debug('c=%s len=%s', c, len(codes)) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
399 |
for code in codes: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
400 |
if c == code[len(consumed)-1]: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
401 |
if len(code) - 1 == len(consumed): |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
402 |
match = code |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
403 |
else: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
404 |
matchingcodes += [code] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
405 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
406 |
self.log.debug('matching=%s', len(matchingcodes)) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
407 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
408 |
# match found, or no matching code found -> stop |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
409 |
if len(matchingcodes) == 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
410 |
break |
0 | 411 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
412 |
# match found and some sequencies still match -> continue |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
413 |
if len(matchingcodes) > 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
414 |
if len(self.inputqueue) == 0: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
415 |
self.inputqueue_fill(1) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
416 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
417 |
c = self.inputqueue_get() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
418 |
if c: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
419 |
consumed.append(c) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
420 |
codes = matchingcodes |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
421 |
matchingcodes = [] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
422 |
else: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
423 |
break |
0 | 424 |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
425 |
keyname = None |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
426 |
if match: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
427 |
# compare match to consumed, return unused chars |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
428 |
l = len(match) - 1 |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
429 |
while len(consumed) > l: |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
430 |
self.inputqueue_unget(consumed[-1]) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
431 |
del consumed[-1] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
432 |
keyname = match[-1] |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
433 |
|
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
434 |
if match is None: |
0 | 435 |
self.log.debug('Unknown control sequence: %s', |
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
436 |
','.join(['0x%x'%x for x in consumed])) |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
437 |
return [('keypress', 'Unknown', None)] |
0 | 438 |
|
439 |
if keyname == 'mouse': |
|
440 |
return self.process_xterm_mouse() |
|
441 |
||
442 |
return [('keypress', keyname, None)] |
|
443 |
||
444 |
||
445 |
def process_xterm_mouse(self): |
|
3
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
446 |
t = self.inputqueue_get_wait() |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
447 |
x = self.inputqueue_get_wait() - 0x21 |
33ec838dc021
Fixed escape sequence handling.
Radek Brich <radek.brich@devl.cz>
parents:
2
diff
changeset
|
448 |
y = self.inputqueue_get_wait() - 0x21 |
0 | 449 |
|
450 |
ev = MouseEvent(x, y) |
|
451 |
out = [] |
|
452 |
||
453 |
if t in (0x20, 0x21, 0x22): # button press |
|
454 |
btn = t - 0x1f |
|
455 |
ev.button = btn |
|
456 |
if not btn in self.mbtnstack: |
|
457 |
self.mbtnstack.append(btn) |
|
458 |
out += [('mousedown', ev)] |
|
459 |
else: |
|
460 |
out += [('mousemove', ev)] |
|
461 |
||
462 |
elif t == 0x23: # button release |
|
463 |
ev.button = self.mbtnstack.pop() |
|
464 |
out += [('mouseup', ev)] |
|
465 |
||
466 |
elif t in (0x60, 0x61): # wheel up, down |
|
467 |
ev.button = 4 + t - 0x60 |
|
468 |
out += [('mousewheel', ev)] |
|
469 |
||
470 |
else: |
|
471 |
raise Exception('Unknown mouse event: %x' % t) |
|
472 |
||
473 |
return out |