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