1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net> |
---|
4 | * 2007 Ben Wiley Sittler <bsittler@gmail.com> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id: x11.c 3587 2009-07-26 23:26:15Z sam $ |
---|
8 | * |
---|
9 | * This library is free software. It comes without any warranty, to |
---|
10 | * the extent permitted by applicable law. You can redistribute it |
---|
11 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
12 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | */ |
---|
15 | |
---|
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 | #include <string.h> |
---|
34 | |
---|
35 | #include "caca.h" |
---|
36 | #include "caca.h" |
---|
37 | #include "caca_internals.h" |
---|
38 | |
---|
39 | /* |
---|
40 | * Local functions |
---|
41 | */ |
---|
42 | static int x11_error_handler(Display *, XErrorEvent *); |
---|
43 | static void x11_put_glyph(caca_display_t *, int, int, int, int, int, |
---|
44 | uint32_t, uint32_t); |
---|
45 | |
---|
46 | struct driver_private |
---|
47 | { |
---|
48 | Display *dpy; |
---|
49 | Window window; |
---|
50 | Pixmap pixmap; |
---|
51 | GC gc; |
---|
52 | long int event_mask; |
---|
53 | int font_width, font_height; |
---|
54 | int colors[4096]; |
---|
55 | Font font; |
---|
56 | XFontStruct *font_struct; |
---|
57 | int font_offset; |
---|
58 | Cursor pointer; |
---|
59 | Atom wm_protocols; |
---|
60 | Atom wm_delete_window; |
---|
61 | #if defined(HAVE_X11_XKBLIB_H) |
---|
62 | Bool autorepeat; |
---|
63 | #endif |
---|
64 | uint32_t max_char; |
---|
65 | int cursor_flags; |
---|
66 | int dirty_cursor_x, dirty_cursor_y; |
---|
67 | }; |
---|
68 | |
---|
69 | #define UNICODE_XLFD_SUFFIX "-iso10646-1" |
---|
70 | #define LATIN_1_XLFD_SUFFIX "-iso8859-1" |
---|
71 | |
---|
72 | static int x11_init_graphics(caca_display_t *dp) |
---|
73 | { |
---|
74 | Colormap colormap; |
---|
75 | XSetWindowAttributes x11_winattr; |
---|
76 | int (*old_error_handler)(Display *, XErrorEvent *); |
---|
77 | char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser; |
---|
78 | char const *geometry; |
---|
79 | int width = caca_get_canvas_width(dp->cv); |
---|
80 | int height = caca_get_canvas_height(dp->cv); |
---|
81 | int i; |
---|
82 | |
---|
83 | dp->drv.p = malloc(sizeof(struct driver_private)); |
---|
84 | |
---|
85 | #if defined(HAVE_GETENV) |
---|
86 | geometry = getenv("CACA_GEOMETRY"); |
---|
87 | if(geometry && *geometry) |
---|
88 | sscanf(geometry, "%ux%u", &width, &height); |
---|
89 | #endif |
---|
90 | |
---|
91 | caca_add_dirty_rect(dp->cv, 0, 0, dp->cv->width, dp->cv->height); |
---|
92 | dp->resize.allow = 1; |
---|
93 | caca_set_canvas_size(dp->cv, width ? width : 80, height ? height : 32); |
---|
94 | width = caca_get_canvas_width(dp->cv); |
---|
95 | height = caca_get_canvas_height(dp->cv); |
---|
96 | dp->resize.allow = 0; |
---|
97 | |
---|
98 | dp->drv.p->dpy = XOpenDisplay(NULL); |
---|
99 | if(dp->drv.p->dpy == NULL) |
---|
100 | return -1; |
---|
101 | |
---|
102 | #if defined(HAVE_GETENV) |
---|
103 | fonts[0] = getenv("CACA_FONT"); |
---|
104 | if(fonts[0] && *fonts[0]) |
---|
105 | parser = fonts; |
---|
106 | else |
---|
107 | #endif |
---|
108 | parser = fonts + 1; |
---|
109 | |
---|
110 | /* Ignore font errors */ |
---|
111 | old_error_handler = XSetErrorHandler(x11_error_handler); |
---|
112 | |
---|
113 | /* Parse our font list */ |
---|
114 | for( ; ; parser++) |
---|
115 | { |
---|
116 | uint32_t font_max_char; |
---|
117 | |
---|
118 | if(!*parser) |
---|
119 | { |
---|
120 | XSetErrorHandler(old_error_handler); |
---|
121 | XCloseDisplay(dp->drv.p->dpy); |
---|
122 | return -1; |
---|
123 | } |
---|
124 | |
---|
125 | dp->drv.p->font = XLoadFont(dp->drv.p->dpy, *parser); |
---|
126 | if(!dp->drv.p->font) |
---|
127 | continue; |
---|
128 | |
---|
129 | dp->drv.p->font_struct = XQueryFont(dp->drv.p->dpy, dp->drv.p->font); |
---|
130 | if(!dp->drv.p->font_struct) |
---|
131 | { |
---|
132 | XUnloadFont(dp->drv.p->dpy, dp->drv.p->font); |
---|
133 | continue; |
---|
134 | } |
---|
135 | |
---|
136 | if((strlen(*parser) > sizeof(UNICODE_XLFD_SUFFIX)) |
---|
137 | && !strcasecmp(*parser + strlen(*parser) |
---|
138 | - strlen(UNICODE_XLFD_SUFFIX), UNICODE_XLFD_SUFFIX)) |
---|
139 | dp->drv.p->max_char = 0xffff; |
---|
140 | else if((strlen(*parser) > sizeof(LATIN_1_XLFD_SUFFIX)) |
---|
141 | && !strcasecmp(*parser + strlen(*parser) |
---|
142 | - strlen(LATIN_1_XLFD_SUFFIX), LATIN_1_XLFD_SUFFIX)) |
---|
143 | dp->drv.p->max_char = 0xff; |
---|
144 | else |
---|
145 | dp->drv.p->max_char = 0x7f; |
---|
146 | |
---|
147 | font_max_char = |
---|
148 | (dp->drv.p->font_struct->max_byte1 << 8) |
---|
149 | | dp->drv.p->font_struct->max_char_or_byte2; |
---|
150 | if(font_max_char && (font_max_char < dp->drv.p->max_char)) |
---|
151 | dp->drv.p->max_char = font_max_char; |
---|
152 | |
---|
153 | break; |
---|
154 | } |
---|
155 | |
---|
156 | /* Reset the default X11 error handler */ |
---|
157 | XSetErrorHandler(old_error_handler); |
---|
158 | |
---|
159 | dp->drv.p->font_width = 0; |
---|
160 | if(dp->drv.p->font_struct->per_char |
---|
161 | && !dp->drv.p->font_struct->min_byte1 |
---|
162 | && dp->drv.p->font_struct->min_char_or_byte2 <= 0x21 |
---|
163 | && dp->drv.p->font_struct->max_char_or_byte2 >= 0x7e) |
---|
164 | { |
---|
165 | for(i = 0x21; i < 0x7f; i++) |
---|
166 | { |
---|
167 | int cw = dp->drv.p->font_struct->per_char[i |
---|
168 | - dp->drv.p->font_struct->min_char_or_byte2].width; |
---|
169 | if(cw > dp->drv.p->font_width) |
---|
170 | dp->drv.p->font_width = cw; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | if(!dp->drv.p->font_width) |
---|
175 | dp->drv.p->font_width = dp->drv.p->font_struct->max_bounds.width; |
---|
176 | |
---|
177 | dp->drv.p->font_height = dp->drv.p->font_struct->max_bounds.ascent |
---|
178 | + dp->drv.p->font_struct->max_bounds.descent; |
---|
179 | dp->drv.p->font_offset = dp->drv.p->font_struct->max_bounds.descent; |
---|
180 | |
---|
181 | colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy)); |
---|
182 | for(i = 0x000; i < 0x1000; i++) |
---|
183 | { |
---|
184 | XColor color; |
---|
185 | color.red = ((i & 0xf00) >> 8) * 0x1111; |
---|
186 | color.green = ((i & 0x0f0) >> 4) * 0x1111; |
---|
187 | color.blue = (i & 0x00f) * 0x1111; |
---|
188 | XAllocColor(dp->drv.p->dpy, colormap, &color); |
---|
189 | dp->drv.p->colors[i] = color.pixel; |
---|
190 | } |
---|
191 | |
---|
192 | x11_winattr.backing_store = Always; |
---|
193 | x11_winattr.background_pixel = dp->drv.p->colors[0x000]; |
---|
194 | x11_winattr.event_mask = ExposureMask | StructureNotifyMask; |
---|
195 | |
---|
196 | dp->drv.p->window = |
---|
197 | XCreateWindow(dp->drv.p->dpy, DefaultRootWindow(dp->drv.p->dpy), 0, 0, |
---|
198 | width * dp->drv.p->font_width, |
---|
199 | height * dp->drv.p->font_height, |
---|
200 | 0, 0, InputOutput, 0, |
---|
201 | CWBackingStore | CWBackPixel | CWEventMask, |
---|
202 | &x11_winattr); |
---|
203 | |
---|
204 | dp->drv.p->wm_protocols = |
---|
205 | XInternAtom(dp->drv.p->dpy, "WM_PROTOCOLS", True); |
---|
206 | dp->drv.p->wm_delete_window = |
---|
207 | XInternAtom(dp->drv.p->dpy, "WM_DELETE_WINDOW", True); |
---|
208 | |
---|
209 | if(dp->drv.p->wm_protocols != None && dp->drv.p->wm_delete_window != None) |
---|
210 | XSetWMProtocols(dp->drv.p->dpy, dp->drv.p->window, |
---|
211 | &dp->drv.p->wm_delete_window, 1); |
---|
212 | |
---|
213 | XStoreName(dp->drv.p->dpy, dp->drv.p->window, "caca for X"); |
---|
214 | |
---|
215 | XSelectInput(dp->drv.p->dpy, dp->drv.p->window, StructureNotifyMask); |
---|
216 | XMapWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
217 | |
---|
218 | dp->drv.p->gc = XCreateGC(dp->drv.p->dpy, dp->drv.p->window, 0, NULL); |
---|
219 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->colors[0x888]); |
---|
220 | XSetFont(dp->drv.p->dpy, dp->drv.p->gc, dp->drv.p->font); |
---|
221 | |
---|
222 | for(;;) |
---|
223 | { |
---|
224 | XEvent xevent; |
---|
225 | XNextEvent(dp->drv.p->dpy, &xevent); |
---|
226 | if(xevent.type == MapNotify) |
---|
227 | break; |
---|
228 | } |
---|
229 | |
---|
230 | #if defined(HAVE_X11_XKBLIB_H) |
---|
231 | /* Disable autorepeat */ |
---|
232 | XkbSetDetectableAutoRepeat(dp->drv.p->dpy, True, &dp->drv.p->autorepeat); |
---|
233 | if(!dp->drv.p->autorepeat) |
---|
234 | XAutoRepeatOff(dp->drv.p->dpy); |
---|
235 | #endif |
---|
236 | |
---|
237 | dp->drv.p->event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask |
---|
238 | | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask |
---|
239 | | ExposureMask; |
---|
240 | |
---|
241 | XSelectInput(dp->drv.p->dpy, dp->drv.p->window, dp->drv.p->event_mask); |
---|
242 | |
---|
243 | XSync(dp->drv.p->dpy, False); |
---|
244 | |
---|
245 | dp->drv.p->pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window, |
---|
246 | width * dp->drv.p->font_width, |
---|
247 | height * dp->drv.p->font_height, |
---|
248 | DefaultDepth(dp->drv.p->dpy, |
---|
249 | DefaultScreen(dp->drv.p->dpy))); |
---|
250 | dp->drv.p->pointer = None; |
---|
251 | |
---|
252 | dp->drv.p->cursor_flags = 0; |
---|
253 | dp->drv.p->dirty_cursor_x = -1; |
---|
254 | dp->drv.p->dirty_cursor_y = -1; |
---|
255 | |
---|
256 | return 0; |
---|
257 | } |
---|
258 | |
---|
259 | static int x11_end_graphics(caca_display_t *dp) |
---|
260 | { |
---|
261 | XSync(dp->drv.p->dpy, False); |
---|
262 | #if defined(HAVE_X11_XKBLIB_H) |
---|
263 | if(!dp->drv.p->autorepeat) |
---|
264 | XAutoRepeatOn(dp->drv.p->dpy); |
---|
265 | #endif |
---|
266 | XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap); |
---|
267 | XFreeFont(dp->drv.p->dpy, dp->drv.p->font_struct); |
---|
268 | XFreeGC(dp->drv.p->dpy, dp->drv.p->gc); |
---|
269 | XUnmapWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
270 | XDestroyWindow(dp->drv.p->dpy, dp->drv.p->window); |
---|
271 | XCloseDisplay(dp->drv.p->dpy); |
---|
272 | |
---|
273 | free(dp->drv.p); |
---|
274 | |
---|
275 | return 0; |
---|
276 | } |
---|
277 | |
---|
278 | static int x11_set_display_title(caca_display_t *dp, char const *title) |
---|
279 | { |
---|
280 | XStoreName(dp->drv.p->dpy, dp->drv.p->window, title); |
---|
281 | return 0; |
---|
282 | } |
---|
283 | |
---|
284 | static int x11_get_display_width(caca_display_t const *dp) |
---|
285 | { |
---|
286 | return caca_get_canvas_width(dp->cv) * dp->drv.p->font_width; |
---|
287 | } |
---|
288 | |
---|
289 | static int x11_get_display_height(caca_display_t const *dp) |
---|
290 | { |
---|
291 | return caca_get_canvas_height(dp->cv) * dp->drv.p->font_height; |
---|
292 | } |
---|
293 | |
---|
294 | static void x11_display(caca_display_t *dp) |
---|
295 | { |
---|
296 | uint32_t const *cvchars = (uint32_t const *)caca_get_canvas_chars(dp->cv); |
---|
297 | uint32_t const *cvattrs = (uint32_t const *)caca_get_canvas_attrs(dp->cv); |
---|
298 | int width = caca_get_canvas_width(dp->cv); |
---|
299 | int height = caca_get_canvas_height(dp->cv); |
---|
300 | int x, y, i, len; |
---|
301 | |
---|
302 | /* XXX: the magic value -1 is used to handle the cursor area */ |
---|
303 | for(i = -1; i < caca_get_dirty_rect_count(dp->cv); i++) |
---|
304 | { |
---|
305 | int dx, dy, dw, dh; |
---|
306 | |
---|
307 | /* Get the dirty rectangle coordinates, either from the previous |
---|
308 | * cursor position, or from the canvas's list. */ |
---|
309 | if(i == -1) |
---|
310 | { |
---|
311 | if(dp->drv.p->dirty_cursor_x < 0 || dp->drv.p->dirty_cursor_y < 0 |
---|
312 | || dp->drv.p->dirty_cursor_x >= width |
---|
313 | || dp->drv.p->dirty_cursor_y >= height) |
---|
314 | continue; |
---|
315 | |
---|
316 | dx = dp->drv.p->dirty_cursor_x; |
---|
317 | dy = dp->drv.p->dirty_cursor_y; |
---|
318 | dw = dh = 1; |
---|
319 | |
---|
320 | dp->drv.p->dirty_cursor_x = -1; |
---|
321 | dp->drv.p->dirty_cursor_y = -1; |
---|
322 | } |
---|
323 | else |
---|
324 | { |
---|
325 | caca_get_dirty_rect(dp->cv, i, &dx, &dy, &dw, &dh); |
---|
326 | } |
---|
327 | |
---|
328 | /* First draw the background colours. Splitting the process in two |
---|
329 | * loops like this is actually slightly faster. */ |
---|
330 | for(y = dy; y < dy + dh; y++) |
---|
331 | { |
---|
332 | for(x = dx; x < dx + dw; x += len) |
---|
333 | { |
---|
334 | uint32_t const *attrs = cvattrs + x + y * width; |
---|
335 | uint16_t bg = caca_attr_to_rgb12_bg(*attrs); |
---|
336 | |
---|
337 | len = 1; |
---|
338 | while(x + len < dx + dw |
---|
339 | && caca_attr_to_rgb12_bg(attrs[len]) == bg) |
---|
340 | len++; |
---|
341 | |
---|
342 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
343 | dp->drv.p->colors[bg]); |
---|
344 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
345 | dp->drv.p->gc, |
---|
346 | x * dp->drv.p->font_width, |
---|
347 | y * dp->drv.p->font_height, |
---|
348 | len * dp->drv.p->font_width, |
---|
349 | dp->drv.p->font_height); |
---|
350 | } |
---|
351 | } |
---|
352 | |
---|
353 | /* Then print the foreground characters */ |
---|
354 | for(y = dy; y < dy + dh; y++) |
---|
355 | { |
---|
356 | int yoff = (y + 1) * dp->drv.p->font_height |
---|
357 | - dp->drv.p->font_offset; |
---|
358 | uint32_t const *chars = cvchars + dx + y * width; |
---|
359 | uint32_t const *attrs = cvattrs + dx + y * width; |
---|
360 | |
---|
361 | for(x = dx; x < dx + dw; x++, chars++, attrs++) |
---|
362 | { |
---|
363 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
364 | dp->drv.p->colors[caca_attr_to_rgb12_fg(*attrs)]); |
---|
365 | |
---|
366 | x11_put_glyph(dp, x * dp->drv.p->font_width, |
---|
367 | y * dp->drv.p->font_height, yoff, |
---|
368 | dp->drv.p->font_width, dp->drv.p->font_height, |
---|
369 | *attrs, *chars); |
---|
370 | } |
---|
371 | } |
---|
372 | } |
---|
373 | |
---|
374 | /* Print the cursor if necessary. */ |
---|
375 | if(dp->drv.p->cursor_flags) |
---|
376 | { |
---|
377 | XSetForeground(dp->drv.p->dpy, dp->drv.p->gc, |
---|
378 | dp->drv.p->colors[0xfff]); |
---|
379 | x = caca_wherex(dp->cv); |
---|
380 | y = caca_wherey(dp->cv); |
---|
381 | XFillRectangle(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->gc, |
---|
382 | x * dp->drv.p->font_width, y * dp->drv.p->font_height, |
---|
383 | dp->drv.p->font_width, dp->drv.p->font_height); |
---|
384 | |
---|
385 | /* Mark the area as dirty */ |
---|
386 | dp->drv.p->dirty_cursor_x = x; |
---|
387 | dp->drv.p->dirty_cursor_y = y; |
---|
388 | } |
---|
389 | |
---|
390 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, dp->drv.p->window, |
---|
391 | dp->drv.p->gc, 0, 0, |
---|
392 | width * dp->drv.p->font_width, |
---|
393 | height * dp->drv.p->font_height, |
---|
394 | 0, 0); |
---|
395 | XFlush(dp->drv.p->dpy); |
---|
396 | } |
---|
397 | |
---|
398 | static void x11_handle_resize(caca_display_t *dp) |
---|
399 | { |
---|
400 | Pixmap new_pixmap; |
---|
401 | |
---|
402 | new_pixmap = XCreatePixmap(dp->drv.p->dpy, dp->drv.p->window, |
---|
403 | dp->resize.w * dp->drv.p->font_width, |
---|
404 | dp->resize.h * dp->drv.p->font_height, |
---|
405 | DefaultDepth(dp->drv.p->dpy, |
---|
406 | DefaultScreen(dp->drv.p->dpy))); |
---|
407 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, new_pixmap, |
---|
408 | dp->drv.p->gc, 0, 0, |
---|
409 | dp->resize.w * dp->drv.p->font_width, |
---|
410 | dp->resize.h * dp->drv.p->font_height, 0, 0); |
---|
411 | XFreePixmap(dp->drv.p->dpy, dp->drv.p->pixmap); |
---|
412 | dp->drv.p->pixmap = new_pixmap; |
---|
413 | } |
---|
414 | |
---|
415 | static int x11_get_event(caca_display_t *dp, caca_privevent_t *ev) |
---|
416 | { |
---|
417 | int width = caca_get_canvas_width(dp->cv); |
---|
418 | int height = caca_get_canvas_height(dp->cv); |
---|
419 | XEvent xevent; |
---|
420 | char key; |
---|
421 | |
---|
422 | while(XCheckWindowEvent(dp->drv.p->dpy, dp->drv.p->window, |
---|
423 | dp->drv.p->event_mask, &xevent) == True) |
---|
424 | { |
---|
425 | KeySym keysym; |
---|
426 | |
---|
427 | /* Expose event */ |
---|
428 | if(xevent.type == Expose) |
---|
429 | { |
---|
430 | XCopyArea(dp->drv.p->dpy, dp->drv.p->pixmap, |
---|
431 | dp->drv.p->window, dp->drv.p->gc, 0, 0, |
---|
432 | width * dp->drv.p->font_width, |
---|
433 | height * dp->drv.p->font_height, 0, 0); |
---|
434 | continue; |
---|
435 | } |
---|
436 | |
---|
437 | /* Resize event */ |
---|
438 | if(xevent.type == ConfigureNotify) |
---|
439 | { |
---|
440 | int w, h; |
---|
441 | |
---|
442 | w = (xevent.xconfigure.width + dp->drv.p->font_width / 3) |
---|
443 | / dp->drv.p->font_width; |
---|
444 | h = (xevent.xconfigure.height + dp->drv.p->font_height / 3) |
---|
445 | / dp->drv.p->font_height; |
---|
446 | |
---|
447 | if(!w || !h || (w == width && h == height)) |
---|
448 | continue; |
---|
449 | |
---|
450 | dp->resize.w = w; |
---|
451 | dp->resize.h = h; |
---|
452 | dp->resize.resized = 1; |
---|
453 | |
---|
454 | continue; |
---|
455 | } |
---|
456 | |
---|
457 | /* Check for mouse motion events */ |
---|
458 | if(xevent.type == MotionNotify) |
---|
459 | { |
---|
460 | int newx = xevent.xmotion.x / dp->drv.p->font_width; |
---|
461 | int newy = xevent.xmotion.y / dp->drv.p->font_height; |
---|
462 | |
---|
463 | if(newx >= width) |
---|
464 | newx = width - 1; |
---|
465 | if(newy >= height) |
---|
466 | newy = height - 1; |
---|
467 | |
---|
468 | if(dp->mouse.x == newx && dp->mouse.y == newy) |
---|
469 | continue; |
---|
470 | |
---|
471 | dp->mouse.x = newx; |
---|
472 | dp->mouse.y = newy; |
---|
473 | |
---|
474 | ev->type = CACA_EVENT_MOUSE_MOTION; |
---|
475 | ev->data.mouse.x = dp->mouse.x; |
---|
476 | ev->data.mouse.y = dp->mouse.y; |
---|
477 | return 1; |
---|
478 | } |
---|
479 | |
---|
480 | /* Check for mouse press and release events */ |
---|
481 | if(xevent.type == ButtonPress) |
---|
482 | { |
---|
483 | ev->type = CACA_EVENT_MOUSE_PRESS; |
---|
484 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
485 | return 1; |
---|
486 | } |
---|
487 | |
---|
488 | if(xevent.type == ButtonRelease) |
---|
489 | { |
---|
490 | ev->type = CACA_EVENT_MOUSE_RELEASE; |
---|
491 | ev->data.mouse.button = ((XButtonEvent *)&xevent)->button; |
---|
492 | return 1; |
---|
493 | } |
---|
494 | |
---|
495 | /* Check for key press and release events */ |
---|
496 | if(xevent.type == KeyPress) |
---|
497 | ev->type = CACA_EVENT_KEY_PRESS; |
---|
498 | else if(xevent.type == KeyRelease) |
---|
499 | ev->type = CACA_EVENT_KEY_RELEASE; |
---|
500 | else |
---|
501 | continue; |
---|
502 | |
---|
503 | if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL)) |
---|
504 | { |
---|
505 | ev->data.key.ch = key; |
---|
506 | ev->data.key.utf32 = key; |
---|
507 | ev->data.key.utf8[0] = key; |
---|
508 | ev->data.key.utf8[1] = '\0'; |
---|
509 | return 1; |
---|
510 | } |
---|
511 | |
---|
512 | keysym = XKeycodeToKeysym(dp->drv.p->dpy, xevent.xkey.keycode, 0); |
---|
513 | switch(keysym) |
---|
514 | { |
---|
515 | case XK_F1: ev->data.key.ch = CACA_KEY_F1; break; |
---|
516 | case XK_F2: ev->data.key.ch = CACA_KEY_F2; break; |
---|
517 | case XK_F3: ev->data.key.ch = CACA_KEY_F3; break; |
---|
518 | case XK_F4: ev->data.key.ch = CACA_KEY_F4; break; |
---|
519 | case XK_F5: ev->data.key.ch = CACA_KEY_F5; break; |
---|
520 | case XK_F6: ev->data.key.ch = CACA_KEY_F6; break; |
---|
521 | case XK_F7: ev->data.key.ch = CACA_KEY_F7; break; |
---|
522 | case XK_F8: ev->data.key.ch = CACA_KEY_F8; break; |
---|
523 | case XK_F9: ev->data.key.ch = CACA_KEY_F9; break; |
---|
524 | case XK_F10: ev->data.key.ch = CACA_KEY_F10; break; |
---|
525 | case XK_F11: ev->data.key.ch = CACA_KEY_F11; break; |
---|
526 | case XK_F12: ev->data.key.ch = CACA_KEY_F12; break; |
---|
527 | case XK_F13: ev->data.key.ch = CACA_KEY_F13; break; |
---|
528 | case XK_F14: ev->data.key.ch = CACA_KEY_F14; break; |
---|
529 | case XK_F15: ev->data.key.ch = CACA_KEY_F15; break; |
---|
530 | case XK_Left: ev->data.key.ch = CACA_KEY_LEFT; break; |
---|
531 | case XK_Right: ev->data.key.ch = CACA_KEY_RIGHT; break; |
---|
532 | case XK_Up: ev->data.key.ch = CACA_KEY_UP; break; |
---|
533 | case XK_Down: ev->data.key.ch = CACA_KEY_DOWN; break; |
---|
534 | case XK_KP_Page_Up: |
---|
535 | case XK_Page_Up: ev->data.key.ch = CACA_KEY_PAGEUP; break; |
---|
536 | case XK_KP_Page_Down: |
---|
537 | case XK_Page_Down: ev->data.key.ch = CACA_KEY_PAGEDOWN; break; |
---|
538 | case XK_KP_Home: |
---|
539 | case XK_Home: ev->data.key.ch = CACA_KEY_HOME; break; |
---|
540 | case XK_KP_End: |
---|
541 | case XK_End: ev->data.key.ch = CACA_KEY_END; break; |
---|
542 | |
---|
543 | default: ev->type = CACA_EVENT_NONE; return 0; |
---|
544 | } |
---|
545 | |
---|
546 | ev->data.key.utf32 = 0; |
---|
547 | ev->data.key.utf8[0] = '\0'; |
---|
548 | return 1; |
---|
549 | } |
---|
550 | |
---|
551 | while(XCheckTypedEvent(dp->drv.p->dpy, ClientMessage, &xevent)) |
---|
552 | { |
---|
553 | if(xevent.xclient.message_type != dp->drv.p->wm_protocols) |
---|
554 | continue; |
---|
555 | |
---|
556 | if((Atom)xevent.xclient.data.l[0] == dp->drv.p->wm_delete_window) |
---|
557 | { |
---|
558 | ev->type = CACA_EVENT_QUIT; |
---|
559 | return 1; |
---|
560 | } |
---|
561 | } |
---|
562 | |
---|
563 | ev->type = CACA_EVENT_NONE; |
---|
564 | return 0; |
---|
565 | } |
---|
566 | |
---|
567 | static void x11_set_mouse(caca_display_t *dp, int flags) |
---|
568 | { |
---|
569 | Cursor no_ptr; |
---|
570 | Pixmap bm_no; |
---|
571 | XColor black, dummy; |
---|
572 | Colormap colormap; |
---|
573 | static char const empty[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
---|
574 | |
---|
575 | if(flags) |
---|
576 | { |
---|
577 | XDefineCursor(dp->drv.p->dpy,dp->drv.p->window, 0); |
---|
578 | return; |
---|
579 | } |
---|
580 | |
---|
581 | colormap = DefaultColormap(dp->drv.p->dpy, DefaultScreen(dp->drv.p->dpy)); |
---|
582 | if(!XAllocNamedColor(dp->drv.p->dpy, colormap, "black", &black, &dummy)) |
---|
583 | { |
---|
584 | return; |
---|
585 | } |
---|
586 | bm_no = XCreateBitmapFromData(dp->drv.p->dpy, dp->drv.p->window, |
---|
587 | empty, 8, 8); |
---|
588 | no_ptr = XCreatePixmapCursor(dp->drv.p->dpy, bm_no, bm_no, |
---|
589 | &black, &black, 0, 0); |
---|
590 | XDefineCursor(dp->drv.p->dpy, dp->drv.p->window, no_ptr); |
---|
591 | XFreeCursor(dp->drv.p->dpy, no_ptr); |
---|
592 | if(bm_no != None) |
---|
593 | XFreePixmap(dp->drv.p->dpy, bm_no); |
---|
594 | XFreeColors(dp->drv.p->dpy, colormap, &black.pixel, 1, 0); |
---|
595 | |
---|
596 | XSync(dp->drv.p->dpy, False); |
---|
597 | } |
---|
598 | |
---|
599 | static void x11_set_cursor(caca_display_t *dp, int flags) |
---|
600 | { |
---|
601 | dp->drv.p->cursor_flags = flags; |
---|
602 | } |
---|
603 | |
---|
604 | /* |
---|
605 | * XXX: following functions are local |
---|
606 | */ |
---|
607 | |
---|
608 | static int x11_error_handler(Display *dpy, XErrorEvent *xevent) |
---|
609 | { |
---|
610 | /* Ignore the error */ |
---|
611 | return 0; |
---|
612 | } |
---|
613 | |
---|
614 | static void x11_put_glyph(caca_display_t *dp, int x, int y, int yoff, |
---|
615 | int w, int h, uint32_t attr, uint32_t ch) |
---|
616 | { |
---|
617 | static uint8_t const udlr[] = |
---|
618 | { |
---|
619 | /* 0x2500 - 0x250f: ─ . │ . . . . . . . . . ┌ . . . */ |
---|
620 | 0x05, 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
621 | 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, |
---|
622 | /* 0x2510 - 0x251f: ┐ . . . └ . . . ┘ . . . ├ . . . */ |
---|
623 | 0x14, 0x00, 0x00, 0x00, 0x41, 0x00, 0x00, 0x00, |
---|
624 | 0x44, 0x00, 0x00, 0x00, 0x51, 0x00, 0x00, 0x00, |
---|
625 | /* 0x2520 - 0x252f: . . . . ┤ . . . . . . . ┬ . . . */ |
---|
626 | 0x00, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, |
---|
627 | 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, |
---|
628 | /* 0x2530 - 0x253f: . . . . ┴ . . . . . . . ┼ . . . */ |
---|
629 | 0x00, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, |
---|
630 | 0x00, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00, |
---|
631 | /* 0x2540 - 0x254f: . . . . . . . . . . . . . . . . */ |
---|
632 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
633 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
---|
634 | /* 0x2550 - 0x255f: ═ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ */ |
---|
635 | 0x0a, 0xa0, 0x12, 0x21, 0x22, 0x18, 0x24, 0x28, |
---|
636 | 0x42, 0x81, 0x82, 0x48, 0x84, 0x88, 0x52, 0xa1, |
---|
637 | /* 0x2560 - 0x256c: ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬ */ |
---|
638 | 0xa2, 0x58, 0xa4, 0xa8, 0x1a, 0x25, 0x2a, 0x4a, |
---|
639 | 0x85, 0x8a, 0x5a, 0xa5, 0xaa, |
---|
640 | }; |
---|
641 | |
---|
642 | Display *dpy = dp->drv.p->dpy; |
---|
643 | Pixmap px = dp->drv.p->pixmap; |
---|
644 | GC gc = dp->drv.p->gc; |
---|
645 | int fw; |
---|
646 | XChar2b ch16; |
---|
647 | |
---|
648 | /* Underline */ |
---|
649 | if(attr & CACA_UNDERLINE) |
---|
650 | XFillRectangle(dpy, px, gc, x, y + h - 1, w, 1); |
---|
651 | |
---|
652 | /* Skip spaces and magic stuff */ |
---|
653 | if(ch <= 0x00000020) |
---|
654 | return; |
---|
655 | |
---|
656 | if(ch == CACA_MAGIC_FULLWIDTH) |
---|
657 | return; |
---|
658 | |
---|
659 | fw = w; |
---|
660 | if(caca_utf32_is_fullwidth(ch)) |
---|
661 | fw *= 2; |
---|
662 | |
---|
663 | /* We want to be able to print a few special Unicode characters |
---|
664 | * such as the CP437 gradients and half blocks. For unknown |
---|
665 | * characters, print what caca_utf32_to_ascii() returns. */ |
---|
666 | |
---|
667 | if(ch >= 0x2500 && ch <= 0x256c && udlr[ch - 0x2500]) |
---|
668 | { |
---|
669 | uint16_t D = udlr[ch - 0x2500]; |
---|
670 | |
---|
671 | if(D & 0x04) |
---|
672 | XFillRectangle(dpy, px, gc, x, y + h / 2, fw / 2 + 1, 1); |
---|
673 | |
---|
674 | if(D & 0x01) |
---|
675 | XFillRectangle(dpy, px, gc, |
---|
676 | x + fw / 2, y + h / 2, (fw + 1) / 2, 1); |
---|
677 | |
---|
678 | if(D & 0x40) |
---|
679 | XFillRectangle(dpy, px, gc, x + fw / 2, y, 1, h / 2 + 1); |
---|
680 | |
---|
681 | if(D & 0x10) |
---|
682 | XFillRectangle(dpy, px, gc, x + fw / 2, y + h / 2, 1, (h + 1) / 2); |
---|
683 | |
---|
684 | #define STEPIF(a,b) (D&(a)?-1:(D&(b))?1:0) |
---|
685 | |
---|
686 | if(D & 0x08) |
---|
687 | { |
---|
688 | XFillRectangle(dpy, px, gc, x, y - 1 + h / 2, |
---|
689 | fw / 2 + 1 + STEPIF(0xc0,0x20), 1); |
---|
690 | XFillRectangle(dpy, px, gc, x, y + 1 + h / 2, |
---|
691 | fw / 2 + 1 + STEPIF(0x30,0x80), 1); |
---|
692 | } |
---|
693 | |
---|
694 | if(D & 0x02) |
---|
695 | { |
---|
696 | XFillRectangle(dpy, px, gc, x - STEPIF(0xc0,0x20) + fw / 2, |
---|
697 | y - 1 + h / 2, (fw + 1) / 2 + STEPIF(0xc0,0x20), 1); |
---|
698 | XFillRectangle(dpy, px, gc, x - STEPIF(0x30,0x80) + fw / 2, |
---|
699 | y + 1 + h / 2, (fw + 1) / 2 + STEPIF(0x30,0x80), 1); |
---|
700 | } |
---|
701 | |
---|
702 | if(D & 0x80) |
---|
703 | { |
---|
704 | XFillRectangle(dpy, px, gc, x - 1 + fw / 2, y, |
---|
705 | 1, h / 2 + 1 + STEPIF(0x0c,0x02)); |
---|
706 | XFillRectangle(dpy, px, gc, x + 1 + fw / 2, y, |
---|
707 | 1, h / 2 + 1 + STEPIF(0x03,0x08)); |
---|
708 | } |
---|
709 | |
---|
710 | if(D & 0x20) |
---|
711 | { |
---|
712 | XFillRectangle(dpy, px, gc, x - 1 + fw / 2, |
---|
713 | y - STEPIF(0x0c,0x02) + h / 2, |
---|
714 | 1, (h + 1) / 2 + STEPIF(0x0c,0x02)); |
---|
715 | XFillRectangle(dpy, px, gc, x + 1 + fw / 2, |
---|
716 | y - STEPIF(0x03,0x08) + h / 2, |
---|
717 | 1, (h + 1) / 2 + STEPIF(0x03,0x08)); |
---|
718 | } |
---|
719 | |
---|
720 | return; |
---|
721 | } |
---|
722 | |
---|
723 | switch(ch) |
---|
724 | { |
---|
725 | case 0x000000b7: /* · */ |
---|
726 | case 0x00002219: /* ∙ */ |
---|
727 | case 0x000030fb: /* ・ */ |
---|
728 | XFillRectangle(dpy, px, gc, x + fw / 2 - 1, y + h / 2 - 1, 2, 2); |
---|
729 | return; |
---|
730 | |
---|
731 | case 0x00002261: /* ≡ */ |
---|
732 | XFillRectangle(dpy, px, gc, x + 1, y - 2 + h / 2, fw - 1, 1); |
---|
733 | XFillRectangle(dpy, px, gc, x + 1, y + h / 2, fw - 1, 1); |
---|
734 | XFillRectangle(dpy, px, gc, x + 1, y + 2 + h / 2, fw - 1, 1); |
---|
735 | return; |
---|
736 | |
---|
737 | case 0x00002580: /* ▀ */ |
---|
738 | XFillRectangle(dpy, px, gc, x, y, fw, h / 2); |
---|
739 | return; |
---|
740 | |
---|
741 | case 0x00002584: /* ▄ */ |
---|
742 | XFillRectangle(dpy, px, gc, x, y + h - h / 2, fw, h / 2); |
---|
743 | return; |
---|
744 | |
---|
745 | case 0x00002588: /* █ */ |
---|
746 | case 0x000025ae: /* ▮ */ |
---|
747 | XFillRectangle(dpy, px, gc, x, y, fw, h); |
---|
748 | return; |
---|
749 | |
---|
750 | case 0x0000258c: /* ▌ */ |
---|
751 | XFillRectangle(dpy, px, gc, x, y, fw / 2, h); |
---|
752 | return; |
---|
753 | |
---|
754 | case 0x00002590: /* ▐ */ |
---|
755 | XFillRectangle(dpy, px, gc, x + fw - fw / 2, y, fw / 2, h); |
---|
756 | return; |
---|
757 | |
---|
758 | case 0x000025a0: /* ■ */ |
---|
759 | case 0x000025ac: /* ▬ */ |
---|
760 | XFillRectangle(dpy, px, gc, x, y + h / 4, fw, h / 2); |
---|
761 | return; |
---|
762 | |
---|
763 | case 0x00002593: /* ▓ */ |
---|
764 | case 0x00002592: /* ▒ */ |
---|
765 | case 0x00002591: /* ░ */ |
---|
766 | { |
---|
767 | /* FIXME: this sucks utterly */ |
---|
768 | int i, j, k = ch - 0x00002591; |
---|
769 | for(j = h; j--; ) |
---|
770 | for(i = fw; i--; ) |
---|
771 | { |
---|
772 | if(((i + 2 * (j & 1)) & 3) > k) |
---|
773 | continue; |
---|
774 | |
---|
775 | XDrawPoint(dpy, px, gc, x + i, y + j); |
---|
776 | } |
---|
777 | return; |
---|
778 | } |
---|
779 | |
---|
780 | case 0x000025cb: /* ○ */ |
---|
781 | case 0x00002022: /* • */ |
---|
782 | case 0x000025cf: /* ● */ |
---|
783 | { |
---|
784 | int d, xo, yo; |
---|
785 | |
---|
786 | d = fw >> (~ch & 0x1); /* XXX: hack */ |
---|
787 | if(h < fw) |
---|
788 | d = h; |
---|
789 | if(d < 1) |
---|
790 | d = 1; |
---|
791 | xo = (fw - d) / 2; |
---|
792 | yo = (h - d) / 2; |
---|
793 | if(ch == 0x000025cb) |
---|
794 | XDrawArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360); |
---|
795 | else |
---|
796 | XFillArc(dpy, px, gc, x + xo, y + yo, d, d, 0, 64 * 360); |
---|
797 | return; |
---|
798 | } |
---|
799 | } |
---|
800 | |
---|
801 | if(ch >= 0x00000020 && ch <= dp->drv.p->max_char) |
---|
802 | { |
---|
803 | /* ascii, latin-1 or unicode font (might draw a blank square) */ |
---|
804 | ch16.byte1 = (ch) >> 8; |
---|
805 | ch16.byte2 = (ch) & 0xff; |
---|
806 | } |
---|
807 | else |
---|
808 | { |
---|
809 | ch16.byte1 = 0; |
---|
810 | ch16.byte2 = caca_utf32_to_ascii(ch); |
---|
811 | } |
---|
812 | |
---|
813 | XDrawString16(dpy, px, gc, x + (ch16.byte1 ? 0 : (fw - w) / 2), yoff, &ch16, 1); |
---|
814 | } |
---|
815 | |
---|
816 | /* |
---|
817 | * Driver initialisation |
---|
818 | */ |
---|
819 | |
---|
820 | int x11_install(caca_display_t *dp) |
---|
821 | { |
---|
822 | #if defined(HAVE_GETENV) |
---|
823 | if(!getenv("DISPLAY") || !*(getenv("DISPLAY"))) |
---|
824 | return -1; |
---|
825 | #endif |
---|
826 | |
---|
827 | dp->drv.id = CACA_DRIVER_X11; |
---|
828 | dp->drv.driver = "x11"; |
---|
829 | |
---|
830 | dp->drv.init_graphics = x11_init_graphics; |
---|
831 | dp->drv.end_graphics = x11_end_graphics; |
---|
832 | dp->drv.set_display_title = x11_set_display_title; |
---|
833 | dp->drv.get_display_width = x11_get_display_width; |
---|
834 | dp->drv.get_display_height = x11_get_display_height; |
---|
835 | dp->drv.display = x11_display; |
---|
836 | dp->drv.handle_resize = x11_handle_resize; |
---|
837 | dp->drv.get_event = x11_get_event; |
---|
838 | dp->drv.set_mouse = x11_set_mouse; |
---|
839 | dp->drv.set_cursor = x11_set_cursor; |
---|
840 | |
---|
841 | return 0; |
---|
842 | } |
---|
843 | |
---|
844 | #endif /* USE_X11 */ |
---|
845 | |
---|