/* $Id$ * $URL$ * * new style X11 Driver for LCD4Linux * * Copyright (C) 2003 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * based on the old XWindow.c which is * Copyright (C) 2000 Herbert Rosmanith * * This file is part of LCD4Linux. * * LCD4Linux is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * LCD4Linux is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * * exported fuctions: * * struct DRIVER drv_X11 * */ #include "config.h" #include #include #include #include #include #include #include #include #include #include #include "debug.h" #include "cfg.h" #include "qprintf.h" #include "timer.h" #include "plugin.h" #include "drv.h" #include "widget.h" #include "widget_keypad.h" #include "drv_generic_graphic.h" #include "drv_generic_keypad.h" #ifdef WITH_DMALLOC #include #endif static char Name[] = "X11"; static int pixel = -1; /* pointsize in pixel */ static int pgap = 0; /* gap between points */ static int rgap = 0; /* row gap between lines */ static int cgap = 0; /* column gap between characters */ static int border = 0; /* window border */ static int buttons = 0; /* number of keypad buttons */ static int btnwidth = 0; static int btnheight = 0; static int dimx, dimy; /* total window dimension in pixel */ static RGBA *drv_X11_FB = NULL; static Display *dp; static int sc, dd; static Window w, rw; static Visual *vi; static GC gc; static Colormap cm; static XColor xc; static Pixmap pm; /****************************************/ /*** hardware dependant functions ***/ /****************************************/ static void drv_X11_color(RGBA c) { xc.red = 255 * c.R; xc.green = 255 * c.G; xc.blue = 255 * c.B; xc.flags = DoRed | DoGreen | DoBlue; if (XAllocColor(dp, cm, &xc) == False) { error("%s: XAllocColor(%02x%02x%02x) failed!", Name, c.R, c.G, c.B); } XSetForeground(dp, gc, xc.pixel); XSetBackground(dp, gc, xc.pixel); } static void drv_X11_blit(const int row, const int col, const int height, const int width) { int r, c; int dirty = 0; for (r = row; r < row + height; r++) { int y = border + (r / YRES) * rgap + r * (pixel + pgap); for (c = col; c < col + width; c++) { int x = border + (c / XRES) * cgap + c * (pixel + pgap); RGBA p1 = drv_X11_FB[r * DCOLS + c]; RGBA p2 = drv_generic_graphic_rgb(r, c); if (p1.R != p2.R || p1.G != p2.G || p1.B != p2.B) { drv_X11_color(p2); XFillRectangle(dp, w, gc, x, y, pixel, pixel); drv_X11_FB[r * DCOLS + c] = p2; dirty = 1; } } } if (dirty) { XSync(dp, False); } } static int drv_X11_brightness(int brightness) { static unsigned char Brightness = 0; RGBA col = BL_COL; int i; float dim; /* -1 is used to query the current brightness */ if (brightness == -1) return Brightness; if (brightness < 0) brightness = 0; if (brightness > 255) brightness = 255; if (Brightness != brightness) { Brightness = brightness; dim = Brightness / 255.0; col.R *= dim; col.G *= dim; col.B *= dim; debug("%s: set backlight to %d%%, original backlight color: 0x%02x%02x%02x%02x, dimmed: 0x%02x%02x%02x%02x", Name, (int) (dim * 100), BL_COL.R, BL_COL.G, BL_COL.B, BL_COL.A, col.R, col.G, col.B, col.A); for (i = 0; i < DCOLS * DROWS; i++) { drv_X11_FB[i] = col; } drv_X11_color(col); XFillRectangle(dp, pm, gc, 0, 0, dimx + 2 * border + btnwidth, dimy + 2 * border); XSetWindowBackground(dp, w, xc.pixel); XClearWindow(dp, w); /* redraw every LCD pixel */ drv_X11_blit(0, 0, LROWS, LCOLS); } return Brightness; } static int drv_X11_keypad(const int num) { int val = WIDGET_KEY_PRESSED; switch (num) { case 1: val += WIDGET_KEY_UP; break; case 2: val += WIDGET_KEY_DOWN; break; case 3: val += WIDGET_KEY_LEFT; break; case 4: val += WIDGET_KEY_RIGHT; break; case 5: val += WIDGET_KEY_CONFIRM; break; case 6: val += WIDGET_KEY_CANCEL; break; default: error("%s: unknown keypad value %d", Name, num); } return val; } static void drv_X11_expose(const int x, const int y, const int width, const int height) { /* * theory of operation: * instead of the old, fully-featured but complicated update * region calculation, we do an update of the whole display, * but check before every pixel if the pixel region is inside * the update region. */ int r, c; int x0, y0; int x1, y1; XFontStruct *xfs; int xoffset = border + (DCOLS / XRES) * cgap + DCOLS * (pixel + pgap); int yoffset = border + (DROWS / YRES) * rgap; int yk; char *s; char unknownTxt[10]; x0 = x - pixel; x1 = x + pixel + width; y0 = y - pixel; y1 = y + pixel + height; for (r = 0; r < DROWS; r++) { int yc = border + (r / YRES) * rgap + r * (pixel + pgap); if (yc < y0 || yc > y1) continue; for (c = 0; c < DCOLS; c++) { int xc = border + (c / XRES) * cgap + c * (pixel + pgap); if (xc < x0 || xc > x1) continue; drv_X11_color(drv_generic_graphic_rgb(r, c)); XFillRectangle(dp, w, gc, xc, yc, pixel, pixel); } } /* Keypad on the right side */ if (x1 >= xoffset) { xfs = XQueryFont(dp, XGContextFromGC(DefaultGC(dp, 0))); if (drv_X11_brightness(-1) > 127) { drv_X11_color(FG_COL); } else { drv_X11_color(BG_COL); } for (r = 0; r < buttons; r++) { yk = yoffset + r * (btnheight + pgap); switch (r) { case 0: s = "Up"; break; case 1: s = "Down"; break; case 2: s = "Left"; break; case 3: s = "Right"; break; case 4: s = "Confirm"; break; case 5: s = "Cancel"; break; default: snprintf(unknownTxt, sizeof(unknownTxt), "#%d??", r); s = unknownTxt; } XDrawRectangle(dp, w, gc, xoffset, yk, btnwidth, btnheight - 2); XDrawString(dp, w, gc, xoffset + btnwidth / 2 - (xfs->max_bounds.width * strlen(s)) / 2, yk + btnheight / 2 + xfs->max_bounds.ascent / 2, s, strlen(s)); } } //XSync(dp, False); } static void drv_X11_timer( __attribute__ ((unused)) void *notused) { XEvent ev; int xoffset = border + (DCOLS / XRES) * cgap + DCOLS * (pixel + pgap); int yoffset = border + (DROWS / YRES) * rgap; static int btn = 0; if (XCheckWindowEvent(dp, w, ExposureMask | ButtonPressMask | ButtonReleaseMask, &ev) == 0) return; switch (ev.type) { case Expose: drv_X11_expose(ev.xexpose.x, ev.xexpose.y, ev.xexpose.width, ev.xexpose.height); break; case ButtonPress: if (ev.xbutton.x >= xoffset && ev.xbutton.x <= xoffset + btnwidth && ev.xbutton.y >= yoffset && ev.xbutton.y <= yoffset + buttons * btnheight + (buttons - 1) * pgap) { btn = (ev.xbutton.y - yoffset) / (btnheight + pgap) + 1; /* btn 0 is unused */ debug("button %d pressed", btn); drv_X11_color(BG_COL); XFillRectangle(dp, w, gc, xoffset + 1, yoffset + (btn - 1) * (btnheight + pgap) + 1, btnwidth - 1, btnheight - 2 - 1); drv_generic_keypad_press(btn); } break; case ButtonRelease: if (ev.xbutton.x >= xoffset && ev.xbutton.x <= xoffset + btnwidth && ev.xbutton.y >= yoffset && ev.xbutton.y <= yoffset + buttons * btnheight + (buttons - 1) * pgap) { btn = (ev.xbutton.y - yoffset) / (btnheight + pgap) + 1; /* btn 0 is unused */ debug("button %d released", btn); XClearArea(dp, w, xoffset, yoffset + (btn - 1) * (btnheight + pgap), btnwidth, btnheight - 2, 1 /* true */ ); } break; default: debug("%s: unknown XEvent %d", Name, ev.type); } } static int drv_X11_start(const char *section) { int i; char *s; XSetWindowAttributes wa; XSizeHints sh; XEvent ev; /* read display size from config */ if (sscanf(s = cfg_get(section, "Size", "120x32"), "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) { error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); free(s); return -1; } free(s); if (sscanf(s = cfg_get(section, "font", "5x8"), "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) { error("%s: bad %s.font '%s' from %s", Name, section, s, cfg_source()); free(s); return -1; } free(s); if (sscanf(s = cfg_get(section, "pixel", "4+1"), "%d+%d", &pixel, &pgap) != 2 || pixel < 1 || pgap < 0) { error("%s: bad %s.pixel '%s' from %s", Name, section, s, cfg_source()); free(s); return -1; } free(s); if (sscanf(s = cfg_get(section, "gap", "-1x-1"), "%dx%d", &cgap, &rgap) != 2 || cgap < -1 || rgap < -1) { error("%s: bad %s.gap '%s' from %s", Name, section, s, cfg_source()); free(s); return -1; } free(s); if (rgap < 0) rgap = pixel + pgap; if (cgap < 0) cgap = pixel + pgap; if (cfg_number(section, "border", 0, 0, -1, &border) < 0) return -1; /* we need the basecolor for the brightness early */ s = cfg_get(section, "basecolor", "000000ff"); if (color2RGBA(s, &BL_COL) < 0) { error("%s: ignoring illegal color '%s'", Name, s); } free(s); /* virtual keyboard: number of buttons (0..6) */ if (cfg_number(section, "buttons", 0, 0, 6, &buttons) < 0) return -1; drv_X11_FB = malloc(DCOLS * DROWS * sizeof(*drv_X11_FB)); if (drv_X11_FB == NULL) { error("%s: framebuffer could not be allocated: malloc() failed", Name); return -1; } for (i = 0; i < DCOLS * DROWS; i++) { drv_X11_FB[i] = BL_COL; } if ((dp = XOpenDisplay(NULL)) == NULL) { error("%s: can't open display", Name); return -1; } sc = DefaultScreen(dp); gc = DefaultGC(dp, sc); vi = DefaultVisual(dp, sc); dd = DefaultDepth(dp, sc); rw = DefaultRootWindow(dp); cm = DefaultColormap(dp, sc); dimx = DCOLS * pixel + (DCOLS - 1) * pgap + (DCOLS / XRES - 1) * cgap; dimy = DROWS * pixel + (DROWS - 1) * pgap + (DROWS / YRES - 1) * rgap; if (buttons != 0) { btnwidth = (DCOLS * pixel + (DCOLS - 1) * pgap) / 10; btnheight = (DROWS * pixel + (DROWS - 1) * pgap) / buttons; } wa.event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask; sh.min_width = sh.max_width = dimx + 2 * border + btnwidth; sh.min_height = sh.max_height = dimy + 2 * border; sh.flags = PPosition | PSize | PMinSize | PMaxSize; w = XCreateWindow(dp, rw, 0, 0, sh.min_width, sh.min_height, 0, 0, InputOutput, vi, CWEventMask, &wa); pm = XCreatePixmap(dp, w, dimx, dimy, dd); XSetWMProperties(dp, w, NULL, NULL, NULL, 0, &sh, NULL, NULL); drv_X11_color(BL_COL); XFillRectangle(dp, pm, gc, 0, 0, sh.min_width, sh.min_height); XSetWindowBackground(dp, w, xc.pixel); XClearWindow(dp, w); /* set brightness (after first background painting) */ if (cfg_number(section, "Brightness", 255, 0, 255, &i) > 0) { drv_X11_brightness(i); } XStoreName(dp, w, "LCD4Linux"); XMapWindow(dp, w); XFlush(dp); while (1) { XNextEvent(dp, &ev); if (ev.type == Expose && ev.xexpose.count == 0) break; } /* regularly process X events */ /* Fixme: make 20msec configurable */ timer_add(drv_X11_timer, NULL, 20, 0); return 0; } /****************************************/ /*** plugins ***/ /****************************************/ static void plugin_brightness(RESULT * result, const int argc, RESULT * argv[]) { double brightness; switch (argc) { case 0: brightness = drv_X11_brightness(-1); SetResult(&result, R_NUMBER, &brightness); break; case 1: brightness = drv_X11_brightness(R2N(argv[0])); SetResult(&result, R_NUMBER, &brightness); break; default: error("%s.brightness(): wrong number of parameters", Name); SetResult(&result, R_STRING, ""); } } /****************************************/ /*** exported functions ***/ /****************************************/ /* list models */ int drv_X11_list(void) { printf("any"); return 0; } /* initialize driver & display */ int drv_X11_init(const char *section, const int quiet) { int ret; info("%s: %s", Name, "$Rev$"); /* start display */ if ((ret = drv_X11_start(section)) != 0) return ret; /* real worker functions */ drv_generic_graphic_real_blit = drv_X11_blit; drv_generic_keypad_real_press = drv_X11_keypad; /* initialize generic graphic driver */ if ((ret = drv_generic_graphic_init(section, Name)) != 0) return ret; /* initialize generic key pad driver */ if ((ret = drv_generic_keypad_init(section, Name)) != 0) return ret; drv_generic_graphic_clear(); /* initially expose window */ drv_X11_expose(0, 0, dimx + 2 * border, dimy + 2 * border); if (!quiet) { char buffer[40]; qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); if (drv_generic_graphic_greet(buffer, NULL)) { sleep(3); drv_generic_graphic_clear(); } } /* register plugins */ AddFunction("LCD::brightness", -1, plugin_brightness); return 0; } /* close driver & display */ int drv_X11_quit(const __attribute__ ((unused)) int quiet) { info("%s: shutting down.", Name); drv_generic_graphic_quit(); drv_generic_keypad_quit(); if (drv_X11_FB) { free(drv_X11_FB); drv_X11_FB = NULL; } return (0); } DRIVER drv_X11 = { .name = Name, .list = drv_X11_list, .init = drv_X11_init, .quit = drv_X11_quit, }; 42'>342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
/* $Id$
 * $URL$
 *
 * generic driver helper for graphic displays
 *
 * Copyright (C) 1999, 2000 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 *
 * exported functions:
 *
 * int drv_generic_graphic_init (char *section, char *driver);
 *   initializes the generic graphic driver
 *
 * int drv_generic_graphic_draw (WIDGET *W);
 *   renders Text widget into framebuffer
 *   calls drv_generic_graphic_real_blit()
 *
 * int drv_generic_graphic_icon_draw (WIDGET *W);
 *   renders Icon widget into framebuffer
 *   calls drv_generic_graphic_real_blit()
 *
 * int drv_generic_graphic_bar_draw (WIDGET *W);
 *   renders Bar widget into framebuffer
 *   calls drv_generic_graphic_real_blit()
 *
 * int drv_generic_graphic_quit (void);
 *   closes generic graphic driver
 *
 */


#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

#include "debug.h"
#include "cfg.h"
#include "plugin.h"
#include "layout.h"
#include "widget.h"
#include "property.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "widget_image.h"
#include "rgb.h"
#include "drv.h"
#include "drv_generic.h"
#include "drv_generic_graphic.h"
#include "font_6x8.h"
#include "font_6x8_bold.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

/* pixel colors */
RGBA FG_COL = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0xff };
RGBA BG_COL = {.R = 0xff,.G = 0xff,.B = 0xff,.A = 0xff };
RGBA BL_COL = {.R = 0xff,.G = 0xff,.B = 0xff,.A = 0x00 };
RGBA NO_COL = {.R = 0x00,.G = 0x00,.B = 0x00,.A = 0x00 };

static char *Section = NULL;
static char *Driver = NULL;

/* framebuffer */
static RGBA *drv_generic_graphic_FB[LAYERS] = { NULL, };

/* inverted colors */
static int INVERTED = 0;

/* must be implemented by the real driver */
void (*drv_generic_graphic_real_blit) () = NULL;


/****************************************/
/*** generic Framebuffer stuff        ***/
/****************************************/

static void drv_generic_graphic_resizeFB(int rows, int cols)
{
    RGBA *newFB;
    int i, l, row, col;

    /* Layout FB is large enough */
    if (rows <= LROWS && cols <= LCOLS)
	return;

    /* get maximum values */
    if (rows < LROWS)
	rows = LROWS;
    if (cols < LCOLS)
	cols = LCOLS;

    for (l = 0; l < LAYERS; l++) {

	/* allocate and initialize new Layout FB */
	newFB = malloc(cols * rows * sizeof(*newFB));
	for (i = 0; i < rows * cols; i++)
	    newFB[i] = (l == 0) ? BG_COL : NO_COL;

	/* transfer contents */
	if (drv_generic_graphic_FB[l] != NULL) {
	    for (row = 0; row < LROWS; row++) {
		for (col = 0; col < LCOLS; col++) {
		    newFB[row * cols + col] = drv_generic_graphic_FB[l][row * LCOLS + col];
		}
	    }
	    free(drv_generic_graphic_FB[l]);
	}
	drv_generic_graphic_FB[l] = newFB;
    }

    LCOLS = cols;
    LROWS = rows;

}

static void drv_generic_graphic_window(int pos, int size, int max, int *wpos, int *wsize)
{
    int p1 = pos;
    int p2 = pos + size;

    *wpos = 0;
    *wsize = 0;

    if (p1 > max || p2 < 0 || size < 1)
	return;

    if (p1 < 0)
	p1 = 0;

    if (p2 > max)
	p2 = max;

    *wpos = p1;
    *wsize = p2 - p1;
}

static void drv_generic_graphic_blit(const int row, const int col, const int height, const int width)
{
    if (drv_generic_graphic_real_blit) {
	int r, c, h, w;
	drv_generic_graphic_window(row, height, DROWS, &r, &h);
	drv_generic_graphic_window(col, width, DCOLS, &c, &w);
	if (h > 0 && w > 0) {
	    drv_generic_graphic_real_blit(r, c, h, w);
	}
    }
}

static RGBA drv_generic_graphic_blend(const int row, const int col)
{
    int l;
    RGBA p;
    RGBA ret;

    ret.R = BL_COL.R;
    ret.G = BL_COL.G;
    ret.B = BL_COL.B;
    ret.A = 0xff;
    for (l = LAYERS - 1; l >= 0; l--) {
	p = drv_generic_graphic_FB[l][row * LCOLS + col];
	if (p.A > 0) {
	    ret.R = (p.R * p.A + ret.R * (255 - p.A)) / 255;
	    ret.G = (p.G * p.A + ret.G * (255 - p.A)) / 255;
	    ret.B = (p.B * p.A + ret.B * (255 - p.A)) / 255;
	}
    }
    if (INVERTED) {
	ret.R = 255 - ret.R;
	ret.G = 255 - ret.G;
	ret.B = 255 - ret.B;
    }

    return ret;
}


/****************************************/
/*** generic text handling            ***/
/****************************************/

static void drv_generic_graphic_render(const int layer, const int row, const int col, const RGBA fg, const RGBA bg,
				       const char *style, const char *txt)
{
    int c, r, x, y, len;

    /* sanity checks */
    if (layer < 0 || layer >= LAYERS) {
	error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1);
	return;
    }

    len = strlen(txt);

    /* maybe grow layout framebuffer */
    drv_generic_graphic_resizeFB(row + YRES, col + XRES * len);

    r = row;
    c = col;

    /* render text into layout FB */
    while (*txt != '\0') {
	unsigned char *chr;

	if (strcasestr(style, "bold") != NULL) {
	    chr = Font_6x8_bold[(int) *(unsigned char *) txt];
	} else {
	    chr = Font_6x8[(int) *(unsigned char *) txt];
	}

	for (y = 0; y < YRES; y++) {
	    int mask = 1 << XRES;
	    for (x = 0; x < XRES; x++) {
		mask >>= 1;
		if (chr[y] & mask)
		    drv_generic_graphic_FB[layer][(r + y) * LCOLS + c + x] = fg;
		else
		    drv_generic_graphic_FB[layer][(r + y) * LCOLS + c + x] = bg;
	    }
	}
	c += XRES;
	txt++;
    }

    /* flush area */
    drv_generic_graphic_blit(row, col, YRES, XRES * len);

}


/* say hello to the user */
int drv_generic_graphic_greet(const char *msg1, const char *msg2)
{
    char *line1[] = { "* LCD4Linux " VERSION " *",
	"LCD4Linux " VERSION,
	"* LCD4Linux *",
	"LCD4Linux",
	"L4Linux",
	NULL
    };

    char *line2[] = { "http://lcd4linux.bulix.org",
	"lcd4linux.bulix.org",
	NULL
    };

    int i;
    int flag = 0;

    unsigned int cols = DCOLS / XRES;
    unsigned int rows = DROWS / YRES;

    for (i = 0; line1[i]; i++) {
	if (strlen(line1[i]) <= cols) {
	    drv_generic_graphic_render(0, YRES * 0, XRES * ((cols - strlen(line1[i])) / 2), FG_COL, BG_COL, "norm",
				       line1[i]);
	    flag = 1;
	    break;
	}
    }

    if (rows >= 2) {
	for (i = 0; line2[i]; i++) {
	    if (strlen(line2[i]) <= cols) {
		drv_generic_graphic_render(0, YRES * 1, XRES * ((cols - strlen(line2[i])) / 2), FG_COL, BG_COL, "norm",
					   line2[i]);
		flag = 1;
		break;
	    }
	}
    }

    if (msg1 && rows >= 3) {
	unsigned int len = strlen(msg1);
	if (len <= cols) {
	    drv_generic_graphic_render(0, YRES * 2, XRES * ((cols - len) / 2), FG_COL, BG_COL, "norm", msg1);
	    flag = 1;
	}
    }

    if (msg2 && rows >= 4) {
	unsigned int len = strlen(msg2);
	if (len <= cols) {
	    drv_generic_graphic_render(0, YRES * 3, XRES * ((cols - len) / 2), FG_COL, BG_COL, "norm", msg2);
	    flag = 1;
	}
    }

    return flag;
}


int drv_generic_graphic_draw(WIDGET * W)
{
    WIDGET_TEXT *Text = W->data;
    RGBA fg, bg;

    fg = W->fg_valid ? W->fg_color : FG_COL;
    bg = W->bg_valid ? W->bg_color : BG_COL;

    drv_generic_graphic_render(W->layer, YRES * W->row, XRES * W->col, fg, bg, P2S(&Text->style), Text->buffer);

    return 0;
}


/****************************************/
/*** generic icon handling            ***/
/****************************************/

int drv_generic_graphic_icon_draw(WIDGET * W)
{
    WIDGET_ICON *Icon = W->data;
    RGBA fg, bg;
    unsigned char *bitmap = Icon->bitmap + YRES * Icon->curmap;
    int layer, row, col;
    int x, y;
    int visible;

    layer = W->layer;
    row = YRES * W->row;
    col = XRES * W->col;

    fg = W->fg_valid ? W->fg_color : FG_COL;
    bg = W->bg_valid ? W->bg_color : BG_COL;

    /* sanity check */
    if (layer < 0 || layer >= LAYERS) {
	error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1);
	return -1;
    }

    /* maybe grow layout framebuffer */
    drv_generic_graphic_resizeFB(row + YRES, col + XRES);

    /* Icon visible? */
    visible = P2N(&Icon->visible) > 0;

    /* render icon */
    for (y = 0; y < YRES; y++) {
	int mask = 1 << XRES;
	for (x = 0; x < XRES; x++) {
	    int i = (row + y) * LCOLS + col + x;
	    mask >>= 1;
	    if (visible) {
		if (bitmap[y] & mask)
		    drv_generic_graphic_FB[layer][i] = fg;
		else
		    drv_generic_graphic_FB[layer][i] = bg;
	    } else {
		drv_generic_graphic_FB[layer][i] = BG_COL;
	    }
	}
    }

    /* flush area */
    drv_generic_graphic_blit(row, col, YRES, XRES);

    return 0;

}


/****************************************/
/*** generic bar handling             ***/
/****************************************/

int drv_generic_graphic_bar_draw(WIDGET * W)
{
    WIDGET_BAR *Bar = W->data;
    RGBA fg, bg, bar[2];
    int layer, row, col, len, res, rev, max, val1, val2;
    int x, y;
    DIRECTION dir;
    STYLE style;

    layer = W->layer;
    row = YRES * W->row;
    col = XRES * W->col;
    dir = Bar->direction;
    style = Bar->style;
    len = Bar->length;

    fg = W->fg_valid ? W->fg_color : FG_COL;
    bg = W->bg_valid ? W->bg_color : BG_COL;

    bar[0] = Bar->color_valid[0] ? Bar->color[0] : fg;
    bar[1] = Bar->color_valid[1] ? Bar->color[1] : fg;

    /* sanity check */
    if (layer < 0 || layer >= LAYERS) {
	error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1);
	return -1;
    }

    /* maybe grow layout framebuffer */
    if (dir & (DIR_EAST | DIR_WEST)) {
	drv_generic_graphic_resizeFB(row + YRES, col + XRES * len);
    } else {
	drv_generic_graphic_resizeFB(row + YRES * len, col + XRES);
    }

    res = dir & (DIR_EAST | DIR_WEST) ? XRES : YRES;
    max = len * res;
    val1 = Bar->val1 * (double) (max);
    val2 = Bar->val2 * (double) (max);

    if (val1 < 1)
	val1 = 1;
    else if (val1 > max)
	val1 = max;

    if (val2 < 1)
	val2 = 1;
    else if (val2 > max)
	val2 = max;

    rev = 0;

    switch (dir) {
    case DIR_WEST:
	val1 = max - val1;
	val2 = max - val2;
	rev = 1;

    case DIR_EAST:
	for (y = 0; y < YRES; y++) {
	    int val = y < YRES / 2 ? val1 : val2;
	    RGBA bcol = y < YRES / 2 ? bar[0] : bar[1];

	    for (x = 0; x < max; x++) {
		if (x < val)
		    drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? bg : bcol;
		else
		    drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? bcol : bg;

		if (style) {
		    drv_generic_graphic_FB[layer][(row) * LCOLS + col + x] = fg;
		    drv_generic_graphic_FB[layer][(row + YRES - 1) * LCOLS + col + x] = fg;
		}
	    }
	    if (style) {
		drv_generic_graphic_FB[layer][(row + y) * LCOLS + col] = fg;
		drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + max - 1] = fg;
	    }
	}
	break;

    case DIR_NORTH:
	val1 = max - val1;
	val2 = max - val2;
	rev = 1;

    case DIR_SOUTH:
	for (x = 0; x < XRES; x++) {
	    int val = x < XRES / 2 ? val1 : val2;
	    for (y = 0; y < max; y++) {
		if (y < val)
		    drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? bg : fg;
		else
		    drv_generic_graphic_FB[layer][(row + y) * LCOLS + col + x] = rev ? fg : bg;
	    }
	}
	break;
    }

    /* flush area */
    if (dir & (DIR_EAST | DIR_WEST)) {
	drv_generic_graphic_blit(row, col, YRES, XRES * len);
    } else {
	drv_generic_graphic_blit(row, col, YRES * len, XRES);
    }

    return 0;
}


/****************************************/
/*** generic image handling           ***/
/****************************************/

int drv_generic_graphic_image_draw(WIDGET * W)
{
    WIDGET_IMAGE *Image = W->data;
    int layer, row, col, width, height;
    int x, y;
    int visible;

    layer = W->layer;
    row = W->row;
    col = W->col;
    width = Image->width;
    height = Image->height;

    /* sanity check */
    if (layer < 0 || layer >= LAYERS) {
	error("%s: layer %d out of bounds (0..%d)", Driver, layer, LAYERS - 1);
	return -1;
    }

    /* if no size or no image at all, do nothing */
    if (width <= 0 || height <= 0 || Image->bitmap == NULL) {
	return 0;
    }

    /* maybe grow layout framebuffer */
    drv_generic_graphic_resizeFB(row + height, col + width);

    /* render image */
    visible = P2N(&Image->visible);
    for (y = 0; y < height; y++) {
	for (x = 0; x < width; x++) {
	    int i = (row + y) * LCOLS + col + x;
	    if (visible) {
		drv_generic_graphic_FB[layer][i] = Image->bitmap[y * width + x];
	    } else {
		drv_generic_graphic_FB[layer][i] = BG_COL;
	    }
	}
    }

    /* flush area */
    drv_generic_graphic_blit(row, col, height, width);

    return 0;

}


/****************************************/
/*** generic init/quit                ***/
/****************************************/

int drv_generic_graphic_init(const char *section, const char *driver)
{
    int l;
    char *color;
    WIDGET_CLASS wc;

    Section = (char *) section;
    Driver = (char *) driver;

    /* init layout framebuffer */
    LROWS = 0;
    LCOLS = 0;

    for (l = 0; l < LAYERS; l++)
	drv_generic_graphic_FB[l] = NULL;

    drv_generic_graphic_resizeFB(DROWS, DCOLS);

    /* sanity check */
    for (l = 0; l < LAYERS; l++) {
	if (drv_generic_graphic_FB[l] == NULL) {
	    error("%s: framebuffer could not be allocated: malloc() failed", Driver);
	    return -1;
	}
    }

    /* init generic driver & register plugins */
    drv_generic_init();

    /* set default colors */
    color = cfg_get(Section, "foreground", "000000ff");
    if (color2RGBA(color, &FG_COL) < 0) {
	error("%s: ignoring illegal color '%s'", Driver, color);
    }
    if (color)
	free(color);

    color = cfg_get(Section, "background", "ffffff00");
    if (color2RGBA(color, &BG_COL) < 0) {
	error("%s: ignoring illegal color '%s'", Driver, color);
    }
    if (color)
	free(color);

    color = cfg_get(Section, "basecolor", "ffffff");
    if (color2RGBA(color, &BL_COL) < 0) {
	error("%s: ignoring illegal color '%s'", Driver, color);
    }
    if (color)
	free(color);

    /* inverted display? */
    cfg_number(section, "inverted", 0, 0, 1, &INVERTED);

    /* register text widget */
    wc = Widget_Text;
    wc.draw = drv_generic_graphic_draw;
    widget_register(&wc);

    /* register icon widget */
    wc = Widget_Icon;
    wc.draw = drv_generic_graphic_icon_draw;
    widget_register(&wc);

    /* register bar widget */
    wc = Widget_Bar;
    wc.draw = drv_generic_graphic_bar_draw;
    widget_register(&wc);

    /* register image widget */
#ifdef WITH_IMAGE
    wc = Widget_Image;
    wc.draw = drv_generic_graphic_image_draw;
    widget_register(&wc);
#endif

    /* clear framebuffer */
    drv_generic_graphic_clear();

    return 0;
}


int drv_generic_graphic_clear(void)
{
    int i, l;

    for (l = 0; l < LAYERS; l++)
	for (i = 0; i < LCOLS * LROWS; i++)
	    drv_generic_graphic_FB[l][i] = NO_COL;

    drv_generic_graphic_blit(0, 0, LROWS, LCOLS);

    return 0;
}


RGBA drv_generic_graphic_rgb(const int row, const int col)
{
    return drv_generic_graphic_blend(row, col);
}


unsigned char drv_generic_graphic_gray(const int row, const int col)
{
    RGBA p = drv_generic_graphic_blend(row, col);
    return (77 * p.R + 150 * p.G + 28 * p.B) / 255;
}


unsigned char drv_generic_graphic_black(const int row, const int col)
{
    return drv_generic_graphic_gray(row, col) < 127;
}


int drv_generic_graphic_quit(void)
{
    int l;

    for (l = 0; l < LAYERS; l++) {
	if (drv_generic_graphic_FB[l]) {
	    free(drv_generic_graphic_FB[l]);
	    drv_generic_graphic_FB[l] = NULL;
	}
    }
    widget_unregister();
    return (0);
}