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)
|
|
87 |
|
|
88 |
self.BOLD = curses.A_BOLD
|
|
89 |
self.BLINK = curses.A_BLINK
|
|
90 |
self.UNDERLINE = curses.A_UNDERLINE
|
|
91 |
|
|
92 |
# http://en.wikipedia.org/wiki/List_of_Unicode_characters#Geometric_shapes
|
|
93 |
self.UP_ARROW = '▲' #curses.ACS_UARROW
|
|
94 |
self.DOWN_ARROW = '▼' #curses.ACS_DARROW
|
|
95 |
|
|
96 |
# http://en.wikipedia.org/wiki/Box-drawing_characters
|
|
97 |
self.LIGHT_SHADE = '░' #curses.ACS_BOARD
|
|
98 |
self.MEDIUM_SHADE = '▒'
|
|
99 |
self.DARK_SHADE = '▓'
|
|
100 |
self.BLOCK = '█'
|
|
101 |
|
|
102 |
self.COLUMN = '▁▂▃▄▅▆▇█'
|
|
103 |
self.CORNER_ROUND = '╭╮╰╯'
|
|
104 |
self.CORNER = '┌┐└┘'
|
|
105 |
self.LINE = '─━│┃┄┅┆┇┈┉┊┋'
|
|
106 |
|
|
107 |
self.HLINE = '─' # curses.ACS_HLINE
|
|
108 |
self.VLINE = '│' # curses.ACS_VLINE
|
|
109 |
self.ULCORNER = '┌' # curses.ACS_ULCORNER
|
|
110 |
self.URCORNER = '┐' # curses.ACS_URCORNER
|
|
111 |
self.LLCORNER = '└' # curses.ACS_LLCORNER
|
|
112 |
self.LRCORNER = '┘' # curses.ACS_LRCORNER
|
|
113 |
self.LTEE = '├'
|
|
114 |
self.RTEE = '┤'
|
|
115 |
|
|
116 |
|
|
117 |
## clip operations ##
|
|
118 |
|
|
119 |
def pushclip(self, x, y, w, h):
|
|
120 |
newclip = Rect(x, y, w, h)
|
|
121 |
if len(self.clipstack):
|
|
122 |
oldclip = self.clipstack[-1]
|
|
123 |
newclip = self.intersect(oldclip, newclip)
|
|
124 |
self.clipstack.append(newclip)
|
|
125 |
|
|
126 |
|
|
127 |
def popclip(self):
|
|
128 |
self.clipstack.pop()
|
|
129 |
|
|
130 |
|
|
131 |
def testclip(self, x, y):
|
|
132 |
# no clip rectangle on stack => passed
|
|
133 |
if not len(self.clipstack):
|
|
134 |
return True
|
|
135 |
# test against top clip rect from stack
|
|
136 |
clip = self.clipstack[-1]
|
|
137 |
if x < clip.x or y < clip.y \
|
|
138 |
or x >= clip.x + clip.w or y >= clip.y + clip.h:
|
|
139 |
return False
|
|
140 |
# passed
|
|
141 |
return True
|
|
142 |
|
|
143 |
|
|
144 |
def intersect(self, r1, r2):
|
|
145 |
x1 = max(r1.x, r2.x)
|
|
146 |
y1 = max(r1.y, r2.y)
|
|
147 |
x2 = min(r1.x + r1.w, r2.x + r2.w)
|
|
148 |
y2 = min(r1.y + r1.h, r2.y + r2.h)
|
|
149 |
if x1 >= x2 or y1 >= y2:
|
|
150 |
return Rect()
|
|
151 |
return Rect(x1, y1, x2-x1, y2-y1)
|
|
152 |
|
|
153 |
|
|
154 |
def union(self, r1, r2):
|
|
155 |
x = min(r1.x, r2.x)
|
|
156 |
y = min(r1.y, r2.y)
|
|
157 |
w = max(r1.x + r1.w, r2.x + r2.w) - x
|
|
158 |
h = max(r1.y + r1.h, r2.y + r2.h) - y
|
|
159 |
return Rect(x, y, w, h)
|
|
160 |
|
|
161 |
|
|
162 |
## attributes ##
|
|
163 |
|
|
164 |
def pushcolor(self, col, attr=0):
|
|
165 |
self.screen.attrset(curses.color_pair(col) | attr)
|
|
166 |
self.colorstack.append((col, attr))
|
|
167 |
|
|
168 |
|
|
169 |
def popcolor(self):
|
|
170 |
self.colorstack.pop()
|
|
171 |
if len(self.colorstack):
|
|
172 |
col, attr = self.colorstack[-1]
|
|
173 |
else:
|
|
174 |
col, attr = 0, 0
|
|
175 |
self.screen.attrset(curses.color_pair(col) | attr)
|
|
176 |
|
|
177 |
|
|
178 |
## drawing ##
|
|
179 |
|
|
180 |
def putch(self, x, y, c):
|
|
181 |
if not self.testclip(x, y):
|
|
182 |
return
|
|
183 |
try:
|
|
184 |
if type(c) is str and len(c) == 1:
|
|
185 |
self.screen.addstr(y, x, c)
|
|
186 |
else:
|
|
187 |
self.screen.addch(y, x, c)
|
|
188 |
except curses.error:
|
|
189 |
pass
|
|
190 |
|
|
191 |
|
|
192 |
def puts(self, x, y, s):
|
|
193 |
for c in s:
|
|
194 |
self.putch(x, y, c)
|
|
195 |
x += 1
|
|
196 |
|
|
197 |
|
|
198 |
def hline(self, x, y, w, c=' '):
|
|
199 |
if type(c) is str:
|
|
200 |
s = c*w
|
|
201 |
else:
|
|
202 |
s = [c]*w
|
|
203 |
self.puts(x, y, s)
|
|
204 |
|
|
205 |
|
|
206 |
def vline(self, x, y, h, c=' '):
|
|
207 |
for i in range(h):
|
|
208 |
self.putch(x, y+i, c)
|
|
209 |
|
|
210 |
|
|
211 |
def frame(self, x, y, w, h):
|
|
212 |
self.putch(x, y, self.ULCORNER)
|
|
213 |
self.putch(x+w-1, y, self.URCORNER)
|
|
214 |
self.putch(x, y+h-1, self.LLCORNER)
|
|
215 |
self.putch(x+w-1, y+h-1, self.LRCORNER)
|
|
216 |
self.hline(x+1, y, w-2, self.HLINE)
|
|
217 |
self.hline(x+1, y+h-1, w-2, self.HLINE)
|
|
218 |
self.vline(x, y+1, h-2, self.VLINE)
|
|
219 |
self.vline(x+w-1, y+1, h-2, self.VLINE)
|
|
220 |
|
|
221 |
|
|
222 |
def fill(self, x, y, w, h, c=' '):
|
|
223 |
for i in range(h):
|
|
224 |
self.hline(x, y + i, w, c)
|
|
225 |
|
|
226 |
|
|
227 |
def erase(self):
|
|
228 |
curses.curs_set(False)
|
|
229 |
self.cursor = None
|
|
230 |
self.screen.erase()
|
|
231 |
|
|
232 |
|
|
233 |
def commit(self):
|
|
234 |
if self.cursor:
|
|
235 |
self.screen.move(*self.cursor)
|
|
236 |
curses.curs_set(True)
|
|
237 |
else:
|
|
238 |
curses.curs_set(False)
|
|
239 |
self.screen.refresh()
|
|
240 |
|
|
241 |
|
|
242 |
## cursor ##
|
|
243 |
|
|
244 |
def showcursor(self, x, y):
|
|
245 |
if not self.testclip(x, y):
|
|
246 |
return
|
|
247 |
self.cursor = (y, x)
|
|
248 |
|
|
249 |
|
|
250 |
## input ##
|
|
251 |
|
|
252 |
def inputqueue_fill(self):
|
|
253 |
self.screen.nodelay(1)
|
|
254 |
|
|
255 |
while True:
|
|
256 |
c = self.screen.getch()
|
|
257 |
if c == -1:
|
|
258 |
break
|
|
259 |
self.inputqueue.insert(0, c)
|
|
260 |
|
|
261 |
self.screen.nodelay(0)
|
|
262 |
|
|
263 |
|
|
264 |
def inputqueue_next(self):
|
|
265 |
c = None
|
|
266 |
while c is None:
|
|
267 |
try:
|
|
268 |
c = self.inputqueue.pop()
|
|
269 |
except IndexError:
|
|
270 |
curses.napms(25)
|
|
271 |
self.inputqueue_fill()
|
|
272 |
return c
|
|
273 |
|
|
274 |
|
|
275 |
def process_input(self, timeout=None):
|
|
276 |
if len(self.inputqueue) > 0:
|
|
277 |
c = self.inputqueue_next()
|
|
278 |
else:
|
|
279 |
if not timeout is None:
|
|
280 |
curses.halfdelay(timeout)
|
|
281 |
c = self.screen.getch()
|
|
282 |
curses.cbreak()
|
|
283 |
if c == -1:
|
|
284 |
return []
|
|
285 |
else:
|
|
286 |
c = self.screen.getch()
|
|
287 |
|
|
288 |
if c == curses.KEY_MOUSE:
|
|
289 |
return self.process_mouse()
|
|
290 |
|
|
291 |
elif curses.ascii.isctrl(c):
|
|
292 |
self.inputqueue.append(c)
|
|
293 |
self.inputqueue_fill()
|
|
294 |
return self.process_control_chars()
|
|
295 |
|
|
296 |
elif c >= 192 and c <= 255:
|
|
297 |
self.inputqueue.append(c)
|
|
298 |
self.inputqueue_fill()
|
|
299 |
return self.process_utf8_chars()
|
|
300 |
|
|
301 |
elif curses.ascii.isprint(c):
|
|
302 |
return [('keypress', None, str(chr(c)))]
|
|
303 |
|
|
304 |
else:
|
|
305 |
#self.top.keypress(None, unicode(chr(c)))
|
|
306 |
self.inputqueue.append(c)
|
|
307 |
self.inputqueue_fill()
|
|
308 |
return self.process_control_chars()
|
|
309 |
|
|
310 |
|
|
311 |
def process_mouse(self):
|
|
312 |
id, x, y, z, bstate = curses.getmouse()
|
|
313 |
ev = MouseEvent(x, y)
|
|
314 |
|
|
315 |
out = []
|
|
316 |
|
|
317 |
if bstate & curses.REPORT_MOUSE_POSITION:
|
|
318 |
out += [('mousemove', ev)]
|
|
319 |
|
|
320 |
if bstate & curses.BUTTON1_PRESSED:
|
|
321 |
ev.button = 1
|
|
322 |
out += [('mousedown', ev)]
|
|
323 |
|
|
324 |
if bstate & curses.BUTTON3_PRESSED:
|
|
325 |
ev.button = 3
|
|
326 |
out += [('mousedown', ev)]
|
|
327 |
|
|
328 |
if bstate & curses.BUTTON1_RELEASED:
|
|
329 |
ev.button = 1
|
|
330 |
out += [('mouseup', ev)]
|
|
331 |
|
|
332 |
if bstate & curses.BUTTON3_RELEASED:
|
|
333 |
ev.button = 3
|
|
334 |
out += [('mouseup', ev)]
|
|
335 |
|
|
336 |
return out
|
|
337 |
|
|
338 |
|
|
339 |
def process_utf8_chars(self):
|
|
340 |
#FIXME read exact number of chars as defined by utf-8
|
|
341 |
utf = ''
|
|
342 |
while len(utf) <= 6:
|
|
343 |
c = self.inputqueue_next()
|
|
344 |
utf += chr(c)
|
|
345 |
try:
|
|
346 |
uni = str(utf, 'utf-8')
|
|
347 |
return [('keypress', None, uni)]
|
|
348 |
except UnicodeDecodeError:
|
|
349 |
continue
|
|
350 |
raise Exception('Invalid UTF-8 sequence: %r' % utf)
|
|
351 |
|
|
352 |
|
|
353 |
def process_control_chars(self):
|
|
354 |
keyname = None
|
|
355 |
for code in self.xterm_codes:
|
|
356 |
ok = False
|
|
357 |
if len(self.inputqueue) >= len(code) - 1:
|
|
358 |
ok = True
|
|
359 |
for i in range(len(code)-1):
|
|
360 |
if self.inputqueue[-i-1] != code[i]:
|
|
361 |
ok = False
|
|
362 |
break
|
|
363 |
|
|
364 |
if ok:
|
|
365 |
keyname = code[-1]
|
|
366 |
self.inputqueue = self.inputqueue[:-len(code)+1]
|
|
367 |
|
|
368 |
if keyname is None:
|
|
369 |
self.log.debug('Unknown control sequence: %s',
|
|
370 |
','.join(reversed(['0x%x'%x for x in self.inputqueue])))
|
|
371 |
c = self.inputqueue_next()
|
|
372 |
return [('keypress', 'Unknown%x' % c, None)]
|
|
373 |
|
|
374 |
if keyname == 'mouse':
|
|
375 |
return self.process_xterm_mouse()
|
|
376 |
|
|
377 |
return [('keypress', keyname, None)]
|
|
378 |
|
|
379 |
|
|
380 |
def process_xterm_mouse(self):
|
|
381 |
t = self.inputqueue_next()
|
|
382 |
x = self.inputqueue_next() - 0x21
|
|
383 |
y = self.inputqueue_next() - 0x21
|
|
384 |
|
|
385 |
ev = MouseEvent(x, y)
|
|
386 |
out = []
|
|
387 |
|
|
388 |
if t in (0x20, 0x21, 0x22): # button press
|
|
389 |
btn = t - 0x1f
|
|
390 |
ev.button = btn
|
|
391 |
if not btn in self.mbtnstack:
|
|
392 |
self.mbtnstack.append(btn)
|
|
393 |
out += [('mousedown', ev)]
|
|
394 |
else:
|
|
395 |
out += [('mousemove', ev)]
|
|
396 |
|
|
397 |
elif t == 0x23: # button release
|
|
398 |
ev.button = self.mbtnstack.pop()
|
|
399 |
out += [('mouseup', ev)]
|
|
400 |
|
|
401 |
elif t in (0x60, 0x61): # wheel up, down
|
|
402 |
ev.button = 4 + t - 0x60
|
|
403 |
out += [('mousewheel', ev)]
|
|
404 |
|
|
405 |
else:
|
|
406 |
raise Exception('Unknown mouse event: %x' % t)
|
|
407 |
|
|
408 |
return out
|