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