source: libcaca/trunk/caca/driver_x11.c @ 906

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