1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | /** \file driver_x11.c |
---|
13 | * \version \$Id: driver_x11.c 681 2006-03-23 18:36:59Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief X11 driver |
---|
16 | * |
---|
17 | * This file contains the libcaca X11 input and output driver |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #if defined(USE_X11) |
---|
23 | |
---|
24 | #include <X11/Xlib.h> |
---|
25 | #include <X11/Xutil.h> |
---|
26 | #include <X11/keysym.h> |
---|
27 | #if defined(HAVE_X11_XKBLIB_H) |
---|
28 | # include <X11/XKBlib.h> |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <stdio.h> /* BUFSIZ */ |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "caca.h" |
---|
35 | #include "caca_internals.h" |
---|
36 | #include "cucul.h" |
---|
37 | #include "cucul_internals.h" |
---|
38 | |
---|
39 | /* |
---|
40 | * Local functions |
---|
41 | */ |
---|
42 | static int x11_error_handler(Display *, XErrorEvent *); |
---|
43 | |
---|
44 | struct driver_private |
---|
45 | { |
---|
46 | Display *dpy; |
---|
47 | Window window; |
---|
48 | Pixmap pixmap; |
---|
49 | GC gc; |
---|
50 | long int event_mask; |
---|
51 | int font_width, font_height; |
---|
52 | int colors[16]; |
---|
53 | Font font; |
---|
54 | XFontStruct *font_struct; |
---|
55 | int font_offset; |
---|
56 | #if defined(HAVE_X11_XKBLIB_H) |
---|
57 | Bool autorepeat; |
---|
58 | #endif |
---|
59 | }; |
---|
60 | |
---|
61 | static int x11_init_graphics(caca_t *kk) |
---|
62 | { |
---|
63 | static int const x11_palette[] = |
---|
64 | { |
---|
65 | /* Standard curses colours */ |
---|
66 | 0x0, 0x0, 0x0, |
---|
67 | 0x0, 0x0, 0x8000, |
---|
68 | 0x0, 0x8000, 0x0, |
---|
69 | 0x0, 0x8000, 0x8000, |
---|
70 | 0x8000, 0x0, 0x0, |
---|
71 | 0x8000, 0x0, 0x8000, |
---|
72 | 0x8000, 0x8000, 0x0, |
---|
73 | 0x8000, 0x8000, 0x8000, |
---|
74 | /* Extra values for xterm-16color */ |
---|
75 | 0x4000, 0x4000, 0x4000, |
---|
76 | 0x4000, 0x4000, 0xffff, |
---|
77 | 0x4000, 0xffff, 0x4000, |
---|
78 | 0x4000, 0xffff, 0xffff, |
---|
79 | 0xffff, 0x4000, 0x4000, |
---|
80 | 0xffff, 0x4000, 0xffff, |
---|
81 | 0xffff, 0xffff, 0x4000, |
---|
82 | 0xffff, 0xffff, 0xffff, |
---|
83 | }; |
---|
84 | |
---|
85 | Colormap colormap; |
---|
86 | XSetWindowAttributes x11_winattr; |
---|
87 | int (*old_error_handler)(Display *, XErrorEvent *); |
---|
88 | char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser; |
---|
89 | char const *geometry; |
---|
90 | unsigned int width = 0, height = 0; |
---|
91 | int i; |
---|
92 | |
---|
93 | kk->drv.p = malloc(sizeof(struct driver_private)); |
---|
94 | |
---|
95 | geometry = getenv("CACA_GEOMETRY"); |
---|
96 | if(geometry && *geometry) |
---|
97 | sscanf(geometry, "%ux%u", &width, &height); |
---|
98 | |
---|
99 | if(width && height) |
---|
100 | _cucul_set_size(kk->qq, width, height); |
---|
101 | |
---|
102 | kk->drv.p->dpy = XOpenDisplay(NULL); |
---|
103 | if(kk->drv.p->dpy == NULL) |
---|
104 | return -1; |
---|
105 | |
---|
106 | fonts[0] = getenv("CACA_FONT"); |
---|
107 | if(fonts[0] && *fonts[0]) |
---|
108 | parser = fonts; |
---|
109 | else |
---|
110 | parser = fonts + 1; |
---|
111 | |
---|
112 | /* Ignore font errors */ |
---|
113 | old_error_handler = XSetErrorHandler(x11_error_handler); |
---|
114 | |
---|
115 | /* Parse our font list */ |
---|
116 | for( ; ; parser++) |
---|
117 | { |
---|
118 | if(!*parser) |
---|
119 | { |
---|
120 | XSetErrorHandler(old_error_handler); |
---|
121 | XCloseDisplay(kk->drv.p->dpy); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | kk->drv.p->font = XLoadFont(kk->drv.p->dpy, *parser); |
---|
126 | if(!kk->drv.p->font) |
---|
127 | continue; |
---|
128 | |
---|
129 | kk->drv.p->font_struct = XQueryFont(kk->drv.p->dpy, kk->drv.p->font); |
---|
130 | if(!kk->drv.p->font_struct) |
---|
131 | { |
---|
132 | XUnloadFont(kk->drv.p->dpy, kk->drv.p->font); |
---|
133 | continue; |
---|
134 | } |
---|
135 | |
---|
136 | break; |
---|
137 | } |
---|
138 | |
---|
139 | /* Reset the default X11 error handler */ |
---|
140 | XSetErrorHandler(old_error_handler); |
---|
141 | |
---|
142 | kk->drv.p->font_width = kk->drv.p->font_struct->max_bounds.width; |
---|
143 | kk->drv.p->font_height = kk->drv.p->font_struct->max_bounds.ascent |
---|
144 | + kk->drv.p->font_struct->max_bounds.descent; |
---|
145 | kk->drv.p->font_offset = kk->drv.p->font_struct->max_bounds.descent; |
---|
146 | |
---|
147 | colormap = DefaultColormap(kk->drv.p->dpy, DefaultScreen(kk->drv.p->dpy)); |
---|
148 | for(i = 0; i < 16; i++) |
---|
149 | { |
---|
150 | XColor color; |
---|
151 | color.red = x11_palette[i * 3]; |
---|
152 | color.green = x11_palette[i * 3 + 1]; |
---|
153 | color.blue = x11_palette[i * 3 + 2]; |
---|
154 | XAllocColor(kk->drv.p->dpy, colormap, &color); |
---|
155 | kk->drv.p->colors[i] = color.pixel; |
---|
156 | } |
---|
157 | |
---|
158 | x11_winattr.backing_store = Always; |
---|
159 | x11_winattr.background_pixel = kk->drv.p->colors[0]; |
---|
160 | x11_winattr.event_mask = ExposureMask | StructureNotifyMask; |
---|
161 | |
---|
162 | kk->drv.p->window = |
---|
163 | XCreateWindow(kk->drv.p->dpy, DefaultRootWindow(kk->drv.p->dpy), 0, 0, |
---|
164 | kk->qq->width * kk->drv.p->font_width, |
---|
165 | kk->qq->height * kk->drv.p->font_height, |
---|
166 | 0, 0, InputOutput, 0, |
---|
167 | CWBackingStore | CWBackPixel | CWEventMask, |
---|
168 | &x11_winattr); |
---|
169 | |
---|
170 | XStoreName(kk->drv.p->dpy, kk->drv.p->window, "caca for X"); |
---|
171 | |
---|
172 | XSelectInput(kk->drv.p->dpy, kk->drv.p->window, StructureNotifyMask); |
---|
173 | XMapWindow(kk->drv.p->dpy, kk->drv.p->window); |
---|
174 | |
---|
175 | kk->drv.p->gc = XCreateGC(kk->drv.p->dpy, kk->drv.p->window, 0, NULL); |
---|
176 | XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->colors[15]); |
---|
177 | XSetFont(kk->drv.p->dpy, kk->drv.p->gc, kk->drv.p->font); |
---|
178 | |
---|
179 | for(;;) |
---|
180 | { |
---|
181 | XEvent xevent; |
---|
182 | XNextEvent(kk->drv.p->dpy, &xevent); |
---|
183 | if (xevent.type == MapNotify) |
---|
184 | break; |
---|
185 | } |
---|
186 | |
---|
187 | #if defined(HAVE_X11_XKBLIB_H) |
---|
188 | /* Disable autorepeat */ |
---|
189 | XkbSetDetectableAutoRepeat(kk->drv.p->dpy, True, &kk->drv.p->autorepeat); |
---|
190 | if(!kk->drv.p->autorepeat) |
---|
191 | XAutoRepeatOff(kk->drv.p->dpy); |
---|
192 | #endif |
---|
193 | |
---|
194 | kk->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
---|
195 | | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask |
---|
196 | | ExposureMask; |
---|
197 | |
---|
198 | XSelectInput(kk->drv.p->dpy, kk->drv.p->window, kk->drv.p->event_mask); |
---|
199 | |
---|
200 | XSync(kk->drv.p->dpy, False); |
---|
201 | |
---|
202 | kk->drv.p->pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window, |
---|
203 | kk->qq->width * kk->drv.p->font_width, |
---|
204 | kk->qq->height * kk->drv.p->font_height, |
---|
205 | DefaultDepth(kk->drv.p->dpy, |
---|
206 | DefaultScreen(kk->drv.p->dpy))); |
---|
207 | |
---|
208 | return 0; |
---|
209 | } |
---|
210 | |
---|
211 | static int x11_end_graphics(caca_t *kk) |
---|
212 | { |
---|
213 | XSync(kk->drv.p->dpy, False); |
---|
214 | #if defined(HAVE_X11_XKBLIB_H) |
---|
215 | if(!kk->drv.p->autorepeat) |
---|
216 | XAutoRepeatOn(kk->drv.p->dpy); |
---|
217 | #endif |
---|
218 | XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap); |
---|
219 | XFreeFont(kk->drv.p->dpy, kk->drv.p->font_struct); |
---|
220 | XFreeGC(kk->drv.p->dpy, kk->drv.p->gc); |
---|
221 | XUnmapWindow(kk->drv.p->dpy, kk->drv.p->window); |
---|
222 | XDestroyWindow(kk->drv.p->dpy, kk->drv.p->window); |
---|
223 | XCloseDisplay(kk->drv.p->dpy); |
---|
224 | |
---|
225 | free(kk->drv.p); |
---|
226 | |
---|
227 | return 0; |
---|
228 | } |
---|
229 | |
---|
230 | static int x11_set_window_title(caca_t *kk, char const *title) |
---|
231 | { |
---|
232 | XStoreName(kk->drv.p->dpy, kk->drv.p->window, title); |
---|
233 | return 0; |
---|
234 | } |
---|
235 | |
---|
236 | static unsigned int x11_get_window_width(caca_t *kk) |
---|
237 | { |
---|
238 | return kk->qq->width * kk->drv.p->font_width; |
---|
239 | } |
---|
240 | |
---|
241 | static unsigned int x11_get_window_height(caca_t *kk) |
---|
242 | { |
---|
243 | return kk->qq->height * kk->drv.p->font_height; |
---|
244 | } |
---|
245 | |
---|
246 | static void x11_display(caca_t *kk) |
---|
247 | { |
---|
248 | unsigned int x, y, len; |
---|
249 | |
---|
250 | /* First draw the background colours. Splitting the process in two |
---|
251 | * loops like this is actually slightly faster. */ |
---|
252 | for(y = 0; y < kk->qq->height; y++) |
---|
253 | { |
---|
254 | for(x = 0; x < kk->qq->width; x += len) |
---|
255 | { |
---|
256 | uint8_t *attr = kk->qq->attr + x + y * kk->qq->width; |
---|
257 | |
---|
258 | len = 1; |
---|
259 | while(x + len < kk->qq->width |
---|
260 | && (attr[len] >> 4) == (attr[0] >> 4)) |
---|
261 | len++; |
---|
262 | |
---|
263 | XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, |
---|
264 | kk->drv.p->colors[attr[0] >> 4]); |
---|
265 | XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc, |
---|
266 | x * kk->drv.p->font_width, y * kk->drv.p->font_height, |
---|
267 | len * kk->drv.p->font_width, kk->drv.p->font_height); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | /* Then print the foreground characters */ |
---|
272 | for(y = 0; y < kk->qq->height; y++) |
---|
273 | { |
---|
274 | unsigned int yoff = (y + 1) * kk->drv.p->font_height |
---|
275 | - kk->drv.p->font_offset; |
---|
276 | uint32_t *chars = kk->qq->chars + y * kk->qq->width; |
---|
277 | |
---|
278 | for(x = 0; x < kk->qq->width; x++, chars++) |
---|
279 | { |
---|
280 | uint8_t *attr = kk->qq->attr + x + y * kk->qq->width; |
---|
281 | |
---|
282 | /* Skip spaces */ |
---|
283 | if(*chars == 0x00000020) |
---|
284 | continue; |
---|
285 | |
---|
286 | XSetForeground(kk->drv.p->dpy, kk->drv.p->gc, |
---|
287 | kk->drv.p->colors[*attr & 0xf]); |
---|
288 | |
---|
289 | /* Plain ASCII, no problem. */ |
---|
290 | if(*chars > 0x00000020 && *chars < 0x00000080) |
---|
291 | { |
---|
292 | char c = (uint8_t)*chars; |
---|
293 | XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->gc, |
---|
294 | x * kk->drv.p->font_width, yoff, &c, 1); |
---|
295 | continue; |
---|
296 | } |
---|
297 | |
---|
298 | /* We want to be able to print a few special Unicode characters |
---|
299 | * such as the CP437 gradients and half blocks. For unknown |
---|
300 | * characters, just print '?'. */ |
---|
301 | switch(*chars) |
---|
302 | { |
---|
303 | case 0x00002580: /* ▀ */ |
---|
304 | XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
305 | kk->drv.p->gc, |
---|
306 | x * kk->drv.p->font_width, |
---|
307 | y * kk->drv.p->font_height, |
---|
308 | kk->drv.p->font_width, |
---|
309 | kk->drv.p->font_height / 2); |
---|
310 | break; |
---|
311 | case 0x00002584: /* ▄ */ |
---|
312 | XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
313 | kk->drv.p->gc, |
---|
314 | x * kk->drv.p->font_width, |
---|
315 | (y + 1) * kk->drv.p->font_height |
---|
316 | - kk->drv.p->font_height / 2, |
---|
317 | kk->drv.p->font_width, |
---|
318 | kk->drv.p->font_height / 2); |
---|
319 | break; |
---|
320 | case 0x00002588: /* █ */ |
---|
321 | XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
322 | kk->drv.p->gc, |
---|
323 | x * kk->drv.p->font_width, |
---|
324 | y * kk->drv.p->font_height, |
---|
325 | kk->drv.p->font_width, |
---|
326 | kk->drv.p->font_height); |
---|
327 | break; |
---|
328 | case 0x0000258c: /* ▌ */ |
---|
329 | XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
330 | kk->drv.p->gc, |
---|
331 | x * kk->drv.p->font_width, |
---|
332 | y * kk->drv.p->font_height, |
---|
333 | kk->drv.p->font_width / 2, |
---|
334 | kk->drv.p->font_height); |
---|
335 | break; |
---|
336 | case 0x00002590: /* ▐ */ |
---|
337 | XFillRectangle(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
338 | kk->drv.p->gc, |
---|
339 | (x + 1) * kk->drv.p->font_width |
---|
340 | - kk->drv.p->font_width / 2, |
---|
341 | y * kk->drv.p->font_height, |
---|
342 | kk->drv.p->font_width / 2, |
---|
343 | kk->drv.p->font_height); |
---|
344 | break; |
---|
345 | case 0x00002593: /* ▓ */ |
---|
346 | case 0x00002592: /* ▒ */ |
---|
347 | case 0x00002591: /* ░ */ |
---|
348 | { |
---|
349 | /* FIXME: this sucks utterly */ |
---|
350 | int i, j, k = *chars - 0x00002591; |
---|
351 | for(j = kk->drv.p->font_height; j--; ) |
---|
352 | for(i = kk->drv.p->font_width; i--; ) |
---|
353 | { |
---|
354 | if(((i + 2 * (j & 1)) & 3) > k) |
---|
355 | continue; |
---|
356 | |
---|
357 | XDrawPoint(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
358 | kk->drv.p->gc, |
---|
359 | x * kk->drv.p->font_width + i, |
---|
360 | y * kk->drv.p->font_height + j); |
---|
361 | } |
---|
362 | break; |
---|
363 | } |
---|
364 | default: |
---|
365 | { |
---|
366 | char c; |
---|
367 | c = '?'; |
---|
368 | XDrawString(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
369 | kk->drv.p->gc, |
---|
370 | x * kk->drv.p->font_width, yoff, &c, 1); |
---|
371 | break; |
---|
372 | } |
---|
373 | } |
---|
374 | } |
---|
375 | } |
---|
376 | |
---|
377 | XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, kk->drv.p->window, |
---|
378 | kk->drv.p->gc, 0, 0, |
---|
379 | kk->qq->width * kk->drv.p->font_width, |
---|
380 | kk->qq->height * kk->drv.p->font_height, |
---|
381 | 0, 0); |
---|
382 | XFlush(kk->drv.p->dpy); |
---|
383 | } |
---|
384 | |
---|
385 | static void x11_handle_resize(caca_t *kk) |
---|
386 | { |
---|
387 | Pixmap new_pixmap; |
---|
388 | |
---|
389 | new_pixmap = XCreatePixmap(kk->drv.p->dpy, kk->drv.p->window, |
---|
390 | kk->resize.w * kk->drv.p->font_width, |
---|
391 | kk->resize.h * kk->drv.p->font_height, |
---|
392 | DefaultDepth(kk->drv.p->dpy, |
---|
393 | DefaultScreen(kk->drv.p->dpy))); |
---|
394 | XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, new_pixmap, |
---|
395 | kk->drv.p->gc, 0, 0, |
---|
396 | kk->resize.w * kk->drv.p->font_width, |
---|
397 | kk->resize.h * kk->drv.p->font_height, 0, 0); |
---|
398 | XFreePixmap(kk->drv.p->dpy, kk->drv.p->pixmap); |
---|
399 | kk->drv.p->pixmap = new_pixmap; |
---|
400 | } |
---|
401 | |
---|
402 | static int x11_get_event(caca_t *kk, struct caca_event *ev) |
---|
403 | { |
---|
404 | XEvent xevent; |
---|
405 | char key; |
---|
406 | |
---|
407 | while(XCheckWindowEvent(kk->drv.p->dpy, kk->drv.p->window, |
---|
408 | kk->drv.p->event_mask, &xevent) == True) |
---|
409 | { |
---|
410 | KeySym keysym; |
---|
411 | |
---|
412 | /* Expose event */ |
---|
413 | if(xevent.type == Expose) |
---|
414 | { |
---|
415 | XCopyArea(kk->drv.p->dpy, kk->drv.p->pixmap, |
---|
416 | kk->drv.p->window, kk->drv.p->gc, 0, 0, |
---|
417 | kk->qq->width * kk->drv.p->font_width, |
---|
418 | kk->qq->height * kk->drv.p->font_height, 0, 0); |
---|
419 | continue; |
---|
420 | } |
---|
421 | |
---|
422 | /* Resize event */ |
---|
423 | if(xevent.type == ConfigureNotify) |
---|
424 | { |
---|
425 | unsigned int w, h; |
---|
426 | |
---|
427 | w = (xevent.xconfigure.width + kk->drv.p->font_width / 3) |
---|
428 | / kk->drv.p->font_width; |
---|
429 | h = (xevent.xconfigure.height + kk->drv.p->font_height / 3) |
---|
430 | / kk->drv.p->font_height; |
---|
431 | |
---|
432 | if(!w || !h || (w == kk->qq->width && h == kk->qq->height)) |
---|
433 | continue; |
---|
434 | |
---|
435 | kk->resize.w = w; |
---|
436 | kk->resize.h = h; |
---|
437 | kk->resize.resized = 1; |
---|
438 | |
---|
439 | continue; |
---|
440 | } |
---|
441 | |
---|
442 | /* Check for mouse motion events */ |
---|
443 | if(xevent.type == MotionNotify) |
---|
444 | { |
---|
445 | unsigned int newx = xevent.xmotion.x / kk->drv.p->font_width; |
---|
446 | unsigned int newy = xevent.xmotion.y / kk->drv.p->font_height; |
---|
447 | |
---|
448 | if(newx >= kk->qq->width) |
---|
449 | newx = kk->qq->width - 1; |
---|
450 | if(newy >= kk->qq->height) |
---|
451 | newy = kk->qq->height - 1; |
---|
452 | |
---|
453 | if(kk->mouse.x == newx && kk->mouse.y == newy) |
---|
454 | continue; |
---|
455 | |
---|
456 | kk->mouse.x = newx; |
---|
457 | kk->mouse.y = newy; |
---|
458 | |
---|
459 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
460 | ev->data.mouse.x = kk->mouse.x; |
---|
461 | ev->data.mouse.y = kk->mouse.y; |
---|
462 | return 1; |
---|
463 | } |
---|
464 | |
---|
465 | /* Check for mouse press and release events */ |
---|
466 | if(xevent.type == ButtonPress) |
---|
467 | { |
---|
468 | ev->type = CACA_EVENT_MOUSE_PRESS; |
---|
469 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
470 | return 1; |
---|
471 | } |
---|
472 | |
---|
473 | if(xevent.type == ButtonRelease) |
---|
474 | { |
---|
475 | ev->type = CACA_EVENT_MOUSE_RELEASE; |
---|
476 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
477 | return 1; |
---|
478 | } |
---|
479 | |
---|
480 | /* Check for key press and release events */ |
---|
481 | if(xevent.type == KeyPress) |
---|
482 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
483 | else if(xevent.type == KeyRelease) |
---|
484 | ev->type = CACA_EVENT_KEY_RELEASE; |
---|
485 | else |
---|
486 | continue; |
---|
487 | |
---|
488 | if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL)) |
---|
489 | { |
---|
490 | ev->data.key.c = key; |
---|
491 | ev->data.key.ucs4 = key; |
---|
492 | ev->data.key.utf8[0] = key; |
---|
493 | ev->data.key.utf8[1] = '\0'; |
---|
494 | return 1; |
---|
495 | } |
---|
496 | |
---|
497 | keysym = XKeycodeToKeysym(kk->drv.p->dpy, xevent.xkey.keycode, 0); |
---|
498 | switch(keysym) |
---|
499 | { |
---|
500 | case XK_F1: ev->data.key.c = CACA_KEY_F1; break; |
---|
501 | case XK_F2: ev->data.key.c = CACA_KEY_F2; break; |
---|
502 | case XK_F3: ev->data.key.c = CACA_KEY_F3; break; |
---|
503 | case XK_F4: ev->data.key.c = CACA_KEY_F4; break; |
---|
504 | case XK_F5: ev->data.key.c = CACA_KEY_F5; break; |
---|
505 | case XK_F6: ev->data.key.c = CACA_KEY_F6; break; |
---|
506 | case XK_F7: ev->data.key.c = CACA_KEY_F7; break; |
---|
507 | case XK_F8: ev->data.key.c = CACA_KEY_F8; break; |
---|
508 | case XK_F9: ev->data.key.c = CACA_KEY_F9; break; |
---|
509 | case XK_F10: ev->data.key.c = CACA_KEY_F10; break; |
---|
510 | case XK_F11: ev->data.key.c = CACA_KEY_F11; break; |
---|
511 | case XK_F12: ev->data.key.c = CACA_KEY_F12; break; |
---|
512 | case XK_F13: ev->data.key.c = CACA_KEY_F13; break; |
---|
513 | case XK_F14: ev->data.key.c = CACA_KEY_F14; break; |
---|
514 | case XK_F15: ev->data.key.c = CACA_KEY_F15; break; |
---|
515 | case XK_Left: ev->data.key.c = CACA_KEY_LEFT; break; |
---|
516 | case XK_Right: ev->data.key.c = CACA_KEY_RIGHT; break; |
---|
517 | case XK_Up: ev->data.key.c = CACA_KEY_UP; break; |
---|
518 | case XK_Down: ev->data.key.c = CACA_KEY_DOWN; break; |
---|
519 | |
---|
520 | default: ev->type = CACA_EVENT_NONE; return 0; |
---|
521 | } |
---|
522 | |
---|
523 | ev->data.key.ucs4 = 0; |
---|
524 | ev->data.key.utf8[0] = '\0'; |
---|
525 | return 1; |
---|
526 | } |
---|
527 | |
---|
528 | ev->type = CACA_EVENT_NONE; |
---|
529 | return 0; |
---|
530 | } |
---|
531 | |
---|
532 | /* |
---|
533 | * XXX: following functions are local |
---|
534 | */ |
---|
535 | |
---|
536 | static int x11_error_handler(Display *dpy, XErrorEvent *xevent) |
---|
537 | { |
---|
538 | /* Ignore the error */ |
---|
539 | return 0; |
---|
540 | } |
---|
541 | |
---|
542 | /* |
---|
543 | * Driver initialisation |
---|
544 | */ |
---|
545 | |
---|
546 | void x11_init_driver(caca_t *kk) |
---|
547 | { |
---|
548 | kk->drv.driver = CACA_DRIVER_X11; |
---|
549 | |
---|
550 | kk->drv.init_graphics = x11_init_graphics; |
---|
551 | kk->drv.end_graphics = x11_end_graphics; |
---|
552 | kk->drv.set_window_title = x11_set_window_title; |
---|
553 | kk->drv.get_window_width = x11_get_window_width; |
---|
554 | kk->drv.get_window_height = x11_get_window_height; |
---|
555 | kk->drv.display = x11_display; |
---|
556 | kk->drv.handle_resize = x11_handle_resize; |
---|
557 | kk->drv.get_event = x11_get_event; |
---|
558 | } |
---|
559 | |
---|
560 | #endif /* USE_X11 */ |
---|
561 | |
---|