root/trunk/drv_X11.c

Revision 840, 13.8 kB (checked in by michael, 15 months ago)

email address changed

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * new style X11 Driver for LCD4Linux
5 *
6 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
8 *
9 * based on the old XWindow.c which is
10 * Copyright (C) 2000 Herbert Rosmanith <herp@wildsau.idv.uni-linz.ac.at>
11 *
12 * This file is part of LCD4Linux.
13 *
14 * LCD4Linux is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * LCD4Linux is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 */
29
30/*
31 *
32 * exported fuctions:
33 *
34 * struct DRIVER drv_X11
35 *
36 */
37
38
39#include "config.h"
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
44#include <errno.h>
45#include <unistd.h>
46#include <termios.h>
47#include <fcntl.h>
48#include <sys/time.h>
49#include <X11/Xlib.h>
50#include <X11/Xutil.h>
51
52#include "debug.h"
53#include "cfg.h"
54#include "qprintf.h"
55#include "timer.h"
56#include "plugin.h"
57#include "drv.h"
58#include "widget.h"
59#include "widget_keypad.h"
60#include "drv_generic_graphic.h"
61#include "drv_generic_keypad.h"
62
63#ifdef WITH_DMALLOC
64#include <dmalloc.h>
65#endif
66
67static char Name[] = "X11";
68
69static int pixel = -1;    /* pointsize in pixel */
70static int pgap = 0;    /* gap between points */
71static int rgap = 0;    /* row gap between lines */
72static int cgap = 0;    /* column gap between characters */
73static int border = 0;    /* window border */
74static int buttons = 0;   /* number of keypad buttons */
75static int btnwidth = 0;
76static int btnheight = 0;
77
78static int dimx, dimy;    /* total window dimension in pixel */
79
80static RGBA *drv_X11_FB = NULL;
81
82static Display *dp;
83static int sc, dd;
84static Window w, rw;
85static Visual *vi;
86static GC gc;
87static Colormap cm;
88static XColor xc;
89static Pixmap pm;
90
91
92/****************************************/
93/***  hardware dependant functions    ***/
94/****************************************/
95
96static void drv_X11_color(RGBA c)
97{
98    xc.red = 255 * c.R;
99    xc.green = 255 * c.G;
100    xc.blue = 255 * c.B;
101    xc.flags = DoRed | DoGreen | DoBlue;
102    if (XAllocColor(dp, cm, &xc) == False) {
103  error("%s: XAllocColor(%02x%02x%02x) failed!", Name, c.R, c.G, c.B);
104    }
105    XSetForeground(dp, gc, xc.pixel);
106    XSetBackground(dp, gc, xc.pixel);
107
108}
109
110
111static void drv_X11_blit(const int row, const int col, const int height, const int width)
112{
113    int r, c;
114    int dirty = 0;
115
116    for (r = row; r < row + height; r++) {
117  int y = border + (r / YRES) * rgap + r * (pixel + pgap);
118  for (c = col; c < col + width; c++) {
119      int x = border + (c / XRES) * cgap + c * (pixel + pgap);
120      RGBA p1 = drv_X11_FB[r * DCOLS + c];
121      RGBA p2 = drv_generic_graphic_rgb(r, c);
122      if (p1.R != p2.R || p1.G != p2.G || p1.B != p2.B) {
123    drv_X11_color(p2);
124    XFillRectangle(dp, w, gc, x, y, pixel, pixel);
125    drv_X11_FB[r * DCOLS + c] = p2;
126    dirty = 1;
127      }
128  }
129    }
130    if (dirty) {
131  XSync(dp, False);
132    }
133}
134
135
136static int drv_X11_brightness(int brightness)
137{
138    static unsigned char Brightness = 0;
139    RGBA col = BL_COL;
140    int i;
141    float dim;
142
143    /* -1 is used to query the current brightness */
144    if (brightness == -1)
145  return Brightness;
146
147    if (brightness < 0)
148  brightness = 0;
149    if (brightness > 255)
150  brightness = 255;
151    Brightness = brightness;
152    dim = Brightness / 255.0;
153    col.R *= dim;
154    col.G *= dim;
155    col.B *= dim;
156
157    debug("%s: set backlight to %d%%, original backlight color: 0x%02x%02x%02x%02x, dimmed: 0x%02x%02x%02x%02x",
158    Name, (int) (dim * 100), BL_COL.R, BL_COL.G, BL_COL.B, BL_COL.A, col.R, col.G, col.B, col.A);
159    for (i = 0; i < DCOLS * DROWS; i++) {
160  drv_X11_FB[i] = col;
161    }
162
163    drv_X11_color(col);
164
165    XFillRectangle(dp, pm, gc, 0, 0, dimx + 2 * border + btnwidth, dimy + 2 * border);
166    XSetWindowBackground(dp, w, xc.pixel);
167    XClearWindow(dp, w);
168
169    /* redraw every LCD pixel */
170    drv_X11_blit(0, 0, LROWS, LCOLS);
171
172    return Brightness;
173}
174
175
176static int drv_X11_keypad(const int num)
177{
178    int val = WIDGET_KEY_PRESSED;
179
180    switch (num) {
181    case 1:
182  val += WIDGET_KEY_UP;
183  break;
184    case 2:
185  val += WIDGET_KEY_DOWN;
186  break;
187    case 3:
188  val += WIDGET_KEY_LEFT;
189  break;
190    case 4:
191  val += WIDGET_KEY_RIGHT;
192  break;
193    case 5:
194  val += WIDGET_KEY_CONFIRM;
195  break;
196    case 6:
197  val += WIDGET_KEY_CANCEL;
198  break;
199    default:
200  error("%s: unknown keypad value %d", Name, num);
201    }
202
203    debug("%s: key %c (0x%x) pressed", Name, num, num);
204    return val;
205}
206
207
208static void drv_X11_expose(const int x, const int y, const int width, const int height)
209{
210    /*
211     * theory of operation:
212     * instead of the old, fully-featured but complicated update
213     * region calculation, we do an update of the whole display,
214     * but check before every pixel if the pixel region is inside
215     * the update region.
216     */
217
218    int r, c;
219    int x0, y0;
220    int x1, y1;
221    XFontStruct *xfs;
222    int xoffset = border + (DCOLS / XRES) * cgap + DCOLS * (pixel + pgap);
223    int yoffset = border + (DROWS / YRES) * rgap;
224    int yk;
225    char *s;
226    char unknownTxt[10];
227
228    x0 = x - pixel;
229    x1 = x + pixel + width;
230    y0 = y - pixel;
231    y1 = y + pixel + height;
232
233    for (r = 0; r < DROWS; r++) {
234  int yc = border + (r / YRES) * rgap + r * (pixel + pgap);
235  if (yc < y0 || yc > y1)
236      continue;
237  for (c = 0; c < DCOLS; c++) {
238      int xc = border + (c / XRES) * cgap + c * (pixel + pgap);
239      if (xc < x0 || xc > x1)
240    continue;
241      drv_X11_color(drv_generic_graphic_rgb(r, c));
242      XFillRectangle(dp, w, gc, xc, yc, pixel, pixel);
243  }
244    }
245
246    /* Keypad on the right side */
247    if (x1 >= xoffset) {
248  xfs = XQueryFont(dp, XGContextFromGC(DefaultGC(dp, 0)));
249  if (drv_X11_brightness(-1) > 127) {
250      drv_X11_color(FG_COL);
251  } else {
252      drv_X11_color(BG_COL);
253  }
254  for (r = 0; r < buttons; r++) {
255      yk = yoffset + r * (btnheight + pgap);
256      switch (r) {
257      case 0:
258    s = "Up";
259    break;
260      case 1:
261    s = "Down";
262    break;
263      case 2:
264    s = "Left";
265    break;
266      case 3:
267    s = "Right";
268    break;
269      case 4:
270    s = "Confirm";
271    break;
272      case 5:
273    s = "Cancel";
274    break;
275      default:
276    snprintf(unknownTxt, sizeof(unknownTxt), "#%d??", r);
277    s = unknownTxt;
278      }
279      XDrawRectangle(dp, w, gc, xoffset, yk, btnwidth, btnheight - 2);
280      XDrawString(dp, w, gc,
281      xoffset + btnwidth / 2 - (xfs->max_bounds.width * strlen(s)) / 2,
282      yk + btnheight / 2 + xfs->max_bounds.ascent / 2, s, strlen(s));
283  }
284    }
285    //XSync(dp, False);
286}
287
288
289static void drv_X11_timer( __attribute__ ((unused))
290        void *notused)
291{
292    XEvent ev;
293    int xoffset = border + (DCOLS / XRES) * cgap + DCOLS * (pixel + pgap);
294    int yoffset = border + (DROWS / YRES) * rgap;
295    static int btn = 0;
296
297    if (XCheckWindowEvent(dp, w, ExposureMask | ButtonPressMask | ButtonReleaseMask, &ev) == 0)
298  return;
299    switch (ev.type) {
300    case Expose:
301  drv_X11_expose(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height);
302  break;
303    case ButtonPress:
304  if (ev.xbutton.x >= xoffset && ev.xbutton.x <= xoffset + btnwidth
305      && ev.xbutton.y >= yoffset && ev.xbutton.y <= yoffset + buttons * btnheight + (buttons - 1) * pgap) {
306      btn = (ev.xbutton.y - yoffset) / (btnheight + pgap) + 1;  /* btn 0 is unused */
307      drv_X11_color(BG_COL);
308      XFillRectangle(dp, w, gc, xoffset + 1, yoffset + (btn - 1) * (btnheight + pgap) + 1, btnwidth - 1,
309         btnheight - 2 - 1);
310      drv_generic_keypad_press(btn);
311  }
312  break;
313    case ButtonRelease:
314  if (ev.xbutton.x >= xoffset && ev.xbutton.x <= xoffset + btnwidth
315      && ev.xbutton.y >= yoffset && ev.xbutton.y <= yoffset + buttons * btnheight + (buttons - 1) * pgap) {
316      XClearArea(dp, w, xoffset, yoffset + (btn - 1) * (btnheight + pgap), btnwidth, btnheight - 2,
317           1 /* true */ );
318      btn = (ev.xbutton.y - yoffset) / (btnheight + pgap) + 1;  /* btn 0 is unused */
319      info("%s: Button %d released", Name, btn);
320  }
321  break;
322    default:
323  debug("%s: unknown XEvent %d", Name, ev.type);
324    }
325}
326
327
328static int drv_X11_start(const char *section)
329{
330    int i;
331    char *s;
332    XSetWindowAttributes wa;
333    XSizeHints sh;
334    XEvent ev;
335
336    /* read display size from config */
337    if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) {
338  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
339  free(s);
340  return -1;
341    }
342    free(s);
343
344    if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
345  error("%s: bad %s.font '%s' from %s", Name, section, s, cfg_source());
346  free(s);
347  return -1;
348    }
349    free(s);
350
351    if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) {
352  error("%s: bad %s.pixel '%s' from %s", Name, section, s, cfg_source());
353  free(s);
354  return -1;
355    }
356    free(s);
357
358    if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) {
359  error("%s: bad %s.gap '%s' from %s", Name, section, s, cfg_source());
360  free(s);
361  return -1;
362    }
363    free(s);
364
365    if (rgap < 0)
366  rgap = pixel + pgap;
367    if (cgap < 0)
368  cgap = pixel + pgap;
369
370    if (cfg_number(section, "border", 0, 0, -1, &border) < 0)
371  return -1;
372
373    /* we need the basecolor for the brightness early */
374    s = cfg_get(section, "basecolor", "000000ff");
375    if (color2RGBA(s, &BL_COL) < 0) {
376  error("%s: ignoring illegal color '%s'", Name, s);
377    }
378    free(s);
379
380    /* virtual keyboard: number of buttons (0..6) */
381    if (cfg_number(section, "buttons", 0, 0, 6, &buttons) < 0)
382  return -1;
383
384    drv_X11_FB = malloc(DCOLS * DROWS * sizeof(*drv_X11_FB));
385    if (drv_X11_FB == NULL) {
386  error("%s: framebuffer could not be allocated: malloc() failed", Name);
387  return -1;
388    }
389
390    for (i = 0; i < DCOLS * DROWS; i++) {
391  drv_X11_FB[i] = BL_COL;
392    }
393
394    if ((dp = XOpenDisplay(NULL)) == NULL) {
395  error("%s: can't open display", Name);
396  return -1;
397    }
398
399    sc = DefaultScreen(dp);
400    gc = DefaultGC(dp, sc);
401    vi = DefaultVisual(dp, sc);
402    dd = DefaultDepth(dp, sc);
403    rw = DefaultRootWindow(dp);
404    cm = DefaultColormap(dp, sc);
405
406    dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap;
407    dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap;
408    if (buttons != 0) {
409  btnwidth = (DCOLS * pixel + (DCOLS - 1) * pgap) / 10;
410  btnheight = (DROWS * pixel + (DROWS - 1) * pgap) / buttons;
411    }
412
413    wa.event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask;
414
415    sh.min_width = sh.max_width = dimx + 2 * border + btnwidth;
416    sh.min_height = sh.max_height = dimy + 2 * border;
417    sh.flags = PPosition | PSize | PMinSize | PMaxSize;
418
419    w = XCreateWindow(dp, rw, 0, 0, sh.min_width, sh.min_height, 0, 0, InputOutput, vi, CWEventMask, &wa);
420
421    pm = XCreatePixmap(dp, w, dimx, dimy, dd);
422
423    XSetWMProperties(dp, w, NULL, NULL, NULL, 0, &sh, NULL, NULL);
424
425    drv_X11_color(BL_COL);
426
427    XFillRectangle(dp, pm, gc, 0, 0, sh.min_width, sh.min_height);
428    XSetWindowBackground(dp, w, xc.pixel);
429    XClearWindow(dp, w);
430
431    /* set brightness (after first background painting) */
432    if (cfg_number(section, "Brightness", 255, 0, 255, &i) > 0) {
433  drv_X11_brightness(i);
434    }
435
436    XStoreName(dp, w, "LCD4Linux");
437    XMapWindow(dp, w);
438
439    XFlush(dp);
440
441    while (1) {
442  XNextEvent(dp, &ev);
443  if (ev.type == Expose && ev.xexpose.count == 0)
444      break;
445    }
446
447    /* regularly process X events */
448    /* Fixme: make 20msec configurable */
449    timer_add(drv_X11_timer, NULL, 20, 0);
450
451    return 0;
452}
453
454
455
456/****************************************/
457/***            plugins               ***/
458/****************************************/
459
460static void plugin_brightness(RESULT * result, const int argc, RESULT * argv[])
461{
462    double brightness;
463
464    switch (argc) {
465    case 0:
466  brightness = drv_X11_brightness(-1);
467  SetResult(&result, R_NUMBER, &brightness);
468  break;
469    case 1:
470  brightness = drv_X11_brightness(R2N(argv[0]));
471  SetResult(&result, R_NUMBER, &brightness);
472  break;
473    default:
474  error("%s.brightness(): wrong number of parameters", Name);
475  SetResult(&result, R_STRING, "");
476    }
477}
478
479
480/****************************************/
481/***        exported functions        ***/
482/****************************************/
483
484
485/* list models */
486int drv_X11_list(void)
487{
488    printf("any");
489    return 0;
490}
491
492
493/* initialize driver & display */
494int drv_X11_init(const char *section, const int quiet)
495{
496    int ret;
497
498    info("%s: %s", Name, "$Rev$");
499
500    /* start display */
501    if ((ret = drv_X11_start(section)) != 0)
502  return ret;
503
504    /* real worker functions */
505    drv_generic_graphic_real_blit = drv_X11_blit;
506    drv_generic_keypad_real_press = drv_X11_keypad;
507
508    /* initialize generic graphic driver */
509    if ((ret = drv_generic_graphic_init(section, Name)) != 0)
510  return ret;
511
512    /* initialize generic key pad driver */
513    if ((ret = drv_generic_keypad_init(section, Name)) != 0)
514  return ret;
515
516    drv_generic_graphic_clear();
517
518    /* initially expose window */
519    drv_X11_expose(0, 0, dimx + 2 * border, dimy + 2 * border);
520
521    if (!quiet) {
522  char buffer[40];
523  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
524  if (drv_generic_graphic_greet(buffer, NULL)) {
525      sleep(3);
526      drv_generic_graphic_clear();
527  }
528    }
529
530    /* register plugins */
531    AddFunction("LCD::brightness", -1, plugin_brightness);
532
533
534    return 0;
535}
536
537
538/* close driver & display */
539int drv_X11_quit(const __attribute__ ((unused))
540     int quiet)
541{
542
543    info("%s: shutting down.", Name);
544    drv_generic_graphic_quit();
545    drv_generic_keypad_quit();
546
547    if (drv_X11_FB) {
548  free(drv_X11_FB);
549  drv_X11_FB = NULL;
550    }
551
552    return (0);
553}
554
555
556DRIVER drv_X11 = {
557    .name = Name,
558    .list = drv_X11_list,
559    .init = drv_X11_init,
560    .quit = drv_X11_quit,
561};
Note: See TracBrowser for help on using the browser.