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

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