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

Last change on this file since 540 was 540, checked in by Sam Hocevar, 15 years ago
  • Polished the driver split a bit (still no events, except resize events), properly credited authors and documented a few things.
  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1/*
2 *  libcaca       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 540 2006-03-07 09:17:35Z 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#if defined(HAVE_X11_XKBLIB_H)
26#   include <X11/XKBlib.h>
27#endif
28
29#include <stdio.h> /* BUFSIZ */
30#include <string.h>
31#include <stdlib.h>
32#if defined(HAVE_UNISTD_H)
33#   include <unistd.h>
34#endif
35#include <stdarg.h>
36
37#include "caca.h"
38#include "caca_internals.h"
39#include "cucul.h"
40#include "cucul_internals.h"
41
42/*
43 * Local functions
44 */
45static int x11_error_handler(Display *, XErrorEvent *);
46
47static int x11_init_graphics(caca_t *kk)
48{
49    static int const x11_palette[] =
50    {
51        /* Standard curses colours */
52        0x0,    0x0,    0x0,
53        0x0,    0x0,    0x8000,
54        0x0,    0x8000, 0x0,
55        0x0,    0x8000, 0x8000,
56        0x8000, 0x0,    0x0,
57        0x8000, 0x0,    0x8000,
58        0x8000, 0x8000, 0x0,
59        0x8000, 0x8000, 0x8000,
60        /* Extra values for xterm-16color */
61        0x4000, 0x4000, 0x4000,
62        0x4000, 0x4000, 0xffff,
63        0x4000, 0xffff, 0x4000,
64        0x4000, 0xffff, 0xffff,
65        0xffff, 0x4000, 0x4000,
66        0xffff, 0x4000, 0xffff,
67        0xffff, 0xffff, 0x4000,
68        0xffff, 0xffff, 0xffff,
69    };
70
71    Colormap colormap;
72    XSetWindowAttributes x11_winattr;
73    int (*old_error_handler)(Display *, XErrorEvent *);
74    char const *fonts[] = { NULL, "8x13bold", "fixed" }, **parser;
75    char const *geometry;
76    unsigned int width = 0, height = 0;
77    int i;
78
79    geometry = getenv("CACA_GEOMETRY");
80    if(geometry && *(geometry))
81        sscanf(geometry, "%ux%u", &width, &height);
82
83    if(width && height)
84        cucul_set_size(kk->qq, width, height);
85
86    kk->x11.dpy = XOpenDisplay(NULL);
87    if(kk->x11.dpy == NULL)
88        return -1;
89
90    fonts[0] = getenv("CACA_FONT");
91    if(fonts[0] && *fonts[0])
92        parser = fonts;
93    else
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(kk->x11.dpy);
106            return -1;
107        }
108
109        kk->x11.font = XLoadFont(kk->x11.dpy, *parser);
110        if(!kk->x11.font)
111            continue;
112
113        kk->x11.font_struct = XQueryFont(kk->x11.dpy, kk->x11.font);
114        if(!kk->x11.font_struct)
115        {
116            XUnloadFont(kk->x11.dpy, kk->x11.font);
117            continue;
118        }
119
120        break;
121    }
122
123    /* Reset the default X11 error handler */
124    XSetErrorHandler(old_error_handler);
125
126    kk->x11.font_width = kk->x11.font_struct->max_bounds.width;
127    kk->x11.font_height = kk->x11.font_struct->max_bounds.ascent
128                         + kk->x11.font_struct->max_bounds.descent;
129    kk->x11.font_offset = kk->x11.font_struct->max_bounds.descent;
130
131    colormap = DefaultColormap(kk->x11.dpy, DefaultScreen(kk->x11.dpy));
132    for(i = 0; i < 16; i++)
133    {
134        XColor color;
135        color.red = x11_palette[i * 3];
136        color.green = x11_palette[i * 3 + 1];
137        color.blue = x11_palette[i * 3 + 2];
138        XAllocColor(kk->x11.dpy, colormap, &color);
139        kk->x11.colors[i] = color.pixel;
140    }
141
142    x11_winattr.backing_store = Always;
143    x11_winattr.background_pixel = kk->x11.colors[0];
144    x11_winattr.event_mask = ExposureMask | StructureNotifyMask;
145
146    kk->x11.window =
147        XCreateWindow(kk->x11.dpy, DefaultRootWindow(kk->x11.dpy), 0, 0,
148                      kk->qq->width * kk->x11.font_width,
149                      kk->qq->height * kk->x11.font_height,
150                      0, 0, InputOutput, 0,
151                      CWBackingStore | CWBackPixel | CWEventMask,
152                      &x11_winattr);
153
154    XStoreName(kk->x11.dpy, kk->x11.window, "caca for X");
155
156    XSelectInput(kk->x11.dpy, kk->x11.window, StructureNotifyMask);
157    XMapWindow(kk->x11.dpy, kk->x11.window);
158
159    kk->x11.gc = XCreateGC(kk->x11.dpy, kk->x11.window, 0, NULL);
160    XSetForeground(kk->x11.dpy, kk->x11.gc, kk->x11.colors[15]);
161    XSetFont(kk->x11.dpy, kk->x11.gc, kk->x11.font);
162
163    for(;;)
164    {
165        XEvent event;
166        XNextEvent(kk->x11.dpy, &event);
167        if (event.type == MapNotify)
168            break;
169    }
170
171#if defined(HAVE_X11_XKBLIB_H)
172    /* Disable autorepeat */
173    XkbSetDetectableAutoRepeat(kk->x11.dpy, True, &kk->x11.autorepeat);
174    if(!kk->x11.autorepeat)
175        XAutoRepeatOff(kk->x11.dpy);
176#endif
177
178    kk->x11.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
179          | ButtonReleaseMask | PointerMotionMask | StructureNotifyMask
180          | ExposureMask;
181
182    XSelectInput(kk->x11.dpy, kk->x11.window, kk->x11.event_mask);
183
184    XSync(kk->x11.dpy, False);
185
186    kk->x11.pixmap = XCreatePixmap(kk->x11.dpy, kk->x11.window,
187                                   kk->qq->width * kk->x11.font_width,
188                                   kk->qq->height * kk->x11.font_height,
189                                   DefaultDepth(kk->x11.dpy,
190                                            DefaultScreen(kk->x11.dpy)));
191
192    kk->x11.new_width = kk->x11.new_height = 0;
193
194    return 0;
195}
196
197static int x11_end_graphics(caca_t *kk)
198{
199    XSync(kk->x11.dpy, False);
200#if defined(HAVE_X11_XKBLIB_H)
201    if(!kk->x11.autorepeat)
202        XAutoRepeatOn(kk->x11.dpy);
203#endif
204    XFreePixmap(kk->x11.dpy, kk->x11.pixmap);
205    XFreeFont(kk->x11.dpy, kk->x11.font_struct);
206    XFreeGC(kk->x11.dpy, kk->x11.gc);
207    XUnmapWindow(kk->x11.dpy, kk->x11.window);
208    XDestroyWindow(kk->x11.dpy, kk->x11.window);
209    XCloseDisplay(kk->x11.dpy);
210
211    return 0;
212}
213
214static int x11_set_window_title(caca_t *kk, char const *title)
215{
216    XStoreName(kk->x11.dpy, kk->x11.window, title);
217    return 0;
218}
219
220static unsigned int x11_get_window_width(caca_t *kk)
221{
222    return kk->qq->width * kk->x11.font_width;
223}
224
225static unsigned int x11_get_window_height(caca_t *kk)
226{
227    return kk->qq->height * kk->x11.font_height;
228}
229
230static void x11_display(caca_t *kk)
231{
232    unsigned int x, y, len;
233
234    /* First draw the background colours. Splitting the process in two
235     * loops like this is actually slightly faster. */
236    for(y = 0; y < kk->qq->height; y++)
237    {
238        for(x = 0; x < kk->qq->width; x += len)
239        {
240            uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
241
242            len = 1;
243            while(x + len < kk->qq->width
244                   && (attr[len] >> 4) == (attr[0] >> 4))
245                len++;
246
247            XSetForeground(kk->x11.dpy, kk->x11.gc,
248                           kk->x11.colors[attr[0] >> 4]);
249            XFillRectangle(kk->x11.dpy, kk->x11.pixmap, kk->x11.gc,
250                           x * kk->x11.font_width, y * kk->x11.font_height,
251                           len * kk->x11.font_width, kk->x11.font_height);
252        }
253    }
254
255    /* Then print the foreground characters */
256    for(y = 0; y < kk->qq->height; y++)
257    {
258        for(x = 0; x < kk->qq->width; x += len)
259        {
260            char buffer[BUFSIZ]; /* FIXME: use a smaller buffer */
261            uint32_t *chars = kk->qq->chars + x + y * kk->qq->width;
262            uint8_t *attr = kk->qq->attr + x + y * kk->qq->width;
263
264            len = 1;
265
266            /* Skip spaces */
267            if(chars[0] == ' ')
268                continue;
269
270            buffer[0] = chars[0] & 0x7f;
271
272            while(x + len < kk->qq->width
273                   && (attr[len] & 0xf) == (attr[0] & 0xf))
274            {
275                buffer[len] = chars[len] & 0x7f;
276                len++;
277            }
278
279            XSetForeground(kk->x11.dpy, kk->x11.gc, kk->x11.colors[attr[0] & 0xf]);
280            XDrawString(kk->x11.dpy, kk->x11.pixmap, kk->x11.gc,
281                        x * kk->x11.font_width,
282                        (y + 1) * kk->x11.font_height - kk->x11.font_offset,
283                        buffer, len);
284        }
285    }
286
287    XCopyArea(kk->x11.dpy, kk->x11.pixmap, kk->x11.window, kk->x11.gc, 0, 0,
288              kk->qq->width * kk->x11.font_width,
289              kk->qq->height * kk->x11.font_height,
290              0, 0);
291    XFlush(kk->x11.dpy);
292}
293
294static void x11_handle_resize(caca_t *kk, unsigned int *new_width,
295                                          unsigned int *new_height)
296{
297    Pixmap new_pixmap;
298
299    *new_width = kk->x11.new_width;
300    *new_height = kk->x11.new_height;
301
302    new_pixmap = XCreatePixmap(kk->x11.dpy, kk->x11.window,
303                               kk->qq->width * kk->x11.font_width,
304                               kk->qq->height * kk->x11.font_height,
305                               DefaultDepth(kk->x11.dpy,
306                                            DefaultScreen(kk->x11.dpy)));
307    XCopyArea(kk->x11.dpy, kk->x11.pixmap, new_pixmap, kk->x11.gc, 0, 0,
308              kk->qq->width * kk->x11.font_width,
309              kk->qq->height * kk->x11.font_height, 0, 0);
310    XFreePixmap(kk->x11.dpy, kk->x11.pixmap);
311    kk->x11.pixmap = new_pixmap;
312}
313
314/*
315 * XXX: following functions are local
316 */
317
318static int x11_error_handler(Display *dpy, XErrorEvent *event)
319{
320    /* Ignore the error */
321    return 0;
322}
323
324/*
325 * Driver initialisation
326 */
327
328void x11_init_driver(caca_t *kk)
329{
330    kk->driver.driver = CACA_DRIVER_X11;
331
332    kk->driver.init_graphics = x11_init_graphics;
333    kk->driver.end_graphics = x11_end_graphics;
334    kk->driver.set_window_title = x11_set_window_title;
335    kk->driver.get_window_width = x11_get_window_width;
336    kk->driver.get_window_height = x11_get_window_height;
337    kk->driver.display = x11_display;
338    kk->driver.handle_resize = x11_handle_resize;
339}
340
341#endif /* USE_X11 */
342
Note: See TracBrowser for help on using the repository browser.