aboutsummaryrefslogtreecommitdiffstats
path: root/drv_generic_text.c
blob: a970da49f4e7d569ffe68f51f746606c97950a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
#----------------------------------------------------------------------------------------------
# Auto-generated from:
# <http://www.digitaluk.co.uk/coveragechecker/main/tradeexport/CF44+0DA/NA/0/>
#----------------------------------------------------------------------------------------------
# location and provider: UK, Aberdare
# date (yyyy-mm-dd)    : 2014-03-25
#
#----------------------------------------------------------------------------------------------
[C24- BBC A]
	DELIVERY_SYSTEM = DVBT
	FREQUENCY = 497833000
	BANDWIDTH_HZ = 8000000
	CODE_RATE_HP = 2/3
	CODE_RATE_LP = NONE
	MODULATION = QAM/64
	TRANSMISSION_MODE = 8K
	GUARD_INTERVAL = 1/32
	HIERARCHY = NONE
	INVERSION = AUTO

[C21+ D3&4]
	DELIVERY_SYSTEM = DVBT
	FREQUENCY = 474167000
	BANDWIDTH_HZ = 8000000
	CODE_RATE_HP = 2/3
	CODE_RATE_LP = NONE
	MODULATION = QAM/64
	TRANSMISSION_MODE = 8K
	GUARD_INTERVAL = 1/32
	HIERARCHY = NONE
	INVERSION = AUTO

[C25+ SDN]
	DELIVERY_SYSTEM = DVBT
	FREQUENCY = 506167000
	BANDWIDTH_HZ = 8000000
	CODE_RATE_HP = 3/4
	CODE_RATE_LP = NONE
	MODULATION = QAM/64
	TRANSMISSION_MODE = 8K
	GUARD_INTERVAL = 1/32
	HIERARCHY = NONE
	INVERSION = AUTO

[C22+ ARQ A]
	DELIVERY_SYSTEM = DVBT
	FREQUENCY = 482167000
	BANDWIDTH_HZ = 8000000
	CODE_RATE_HP = 3/4
	CODE_RATE_LP = NONE
	MODULATION = QAM/64
	TRANSMISSION_MODE = 8K
	GUARD_INTERVAL = 1/32
	HIERARCHY = NONE
	INVERSION = AUTO

[C28+ ARQ B]
	DELIVERY_SYSTEM = DVBT
	FREQUENCY = 530167000
	BANDWIDTH_HZ = 8000000
	CODE_RATE_HP = 3/4
	CODE_RATE_LP = NONE
	MODULATION = QAM/64
	TRANSMISSION_MODE = 8K
	GUARD_INTERVAL = 1/32
	HIERARCHY = NONE
	INVERSION = AUTO

[C27- BBC B HD]
	DELIVERY_SYSTEM = DVBT2
	FREQUENCY = 521833000
	BANDWIDTH_HZ = 8000000
	CODE_RATE_HP = 2/3
	CODE_RATE_LP = NONE
	MODULATION = QAM/256
	TRANSMISSION_MODE = 32K
	GUARD_INTERVAL = 1/128
	HIERARCHY = NONE
	STREAM_ID = 0
	INVERSION = AUTO
f='#n307'>307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 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 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
/* $Id$
 * $URL$
 *
 * generic driver helper for text-based 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 variables:
 *
 * extern int CHARS, CHAR0;    number of user-defineable characters, ASCII of first char
 * extern int ICONS;           number of user-defineable characters reserved for icons
 * extern int GOTO_COST;       number of bytes a goto command requires
 * extern int INVALIDATE;      re-send a modified userdefined char?
 *
 *
 * these functions must be implemented by the real driver:
 *
 * void (*drv_generic_text_real_write)(int row, int col, unsigned char *data, int len);
 *  writes a text of specified length at position (row, col)
 *
 * void (*drv_generic_text_real_defchar)(int ascii, unsigned char *buffer);
 *  defines the bitmap of a user-defined character
 *
 *
 * exported fuctions:
 *
 * int drv_generic_text_init (char *section, char *driver);
 *   initializes the generic text driver
 *
 * int drv_generic_text_draw (WIDGET *W);
 *   renders Text widget into framebuffer
 *   calls drv_generic_text_real_write()
 *
 * int drv_generic_text_icon_init (void);
 *   initializes the generic icon driver
 *   
 * int drv_generic_text_icon_draw (WIDGET *W);
 *   renders Icon widget into framebuffer
 *   calls drv_generic_text_real_write() and drv_generic_text_real_defchar()
 *
 * int drv_generic_text_bar_init (int single_segments);
 *   initializes the generic icon driver
 *
 * void drv_generic_text_bar_add_segment (int val1, int val2, DIRECTION dir, int ascii);
 *   adds a 'fixed' character to the bar-renderer
 *
 * int drv_generic_text_bar_draw (WIDGET *W);
 *   renders Bar widget into framebuffer
 *   calls drv_generic_text_real_write() and drv_generic_text_real_defchar()
 *
 * int drv_generic_text_quit (void);
 *   closes the generic text 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 "widget.h"
#include "widget_text.h"
#include "widget_icon.h"
#include "widget_bar.h"
#include "drv.h"
#include "drv_generic.h"
#include "drv_generic_text.h"

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


typedef struct {
    int val1;
    int val2;
    DIRECTION dir;
    STYLE style;
    int segment;
    int invalid;
} BAR;

typedef struct {
    int val1;
    int val2;
    DIRECTION dir;
    STYLE style;
    int used;
    int ascii;
} SEGMENT;

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

int CHARS = 0;			/* number of user-defineable characters */
int CHAR0 = 0;			/* ASCII of first user-defineable char */
int ICONS = 0;			/* number of user-defineable characters reserved for icons */

int GOTO_COST = 0;		/* number of bytes a goto command requires */
int INVALIDATE = 0;		/* re-send a modified userdefined char? */


void (*drv_generic_text_real_write) () = NULL;
void (*drv_generic_text_real_defchar) () = NULL;


static char *LayoutFB = NULL;
static char *DisplayFB = NULL;
static int *L2D = NULL;

static int Single_Segments = 0;

static int nSegment = 0;
static int fSegment = 0;
static SEGMENT Segment[128];
static BAR *BarFB = NULL;


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

static void drv_generic_text_resizeFB(int rows, int cols)
{
    char *newFB;
    int *newL2D;
    BAR *newBar;
    int i, 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;

    /* allocate new Layout FB */
    newFB = malloc(cols * rows * sizeof(*newFB));
    memset(newFB, ' ', rows * cols * sizeof(*newFB));

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

    /* allocate new transformer */
    newL2D = malloc(cols * rows * sizeof(*newL2D));

    /* intitialize translator */
    for (row = 0; row < rows; row++) {
	for (col = 0; col < cols; col++) {
	    newL2D[row * cols + col] = row * DCOLS + col;
	}
    }

    /* transfer transformer */
    if (L2D != NULL) {
	for (row = 0; row < LROWS; row++) {
	    for (col = 0; col < LCOLS; col++) {
		newL2D[row * cols + col] = L2D[row * LCOLS + col];
	    }
	}
	free(L2D);
    }
    L2D = newL2D;

    /* resize Bar buffer */
    if (BarFB) {

	newBar = malloc(rows * cols * sizeof(BAR));

	for (i = 0; i < rows * cols; i++) {
	    newBar[i].val1 = -1;
	    newBar[i].val2 = -1;
	    newBar[i].dir = 0;
	    newBar[i].segment = -1;
	    newBar[i].invalid = 0;
	}

	/* transfer contents */
	for (row = 0; row < LROWS; row++) {
	    for (col = 0; col < LCOLS; col++) {
		newBar[row * cols + col] = BarFB[row * LCOLS + col];
	    }
	}

	free(BarFB);
	BarFB = newBar;
    }

    LCOLS = cols;
    LROWS = rows;
}


static void drv_generic_text_blit(const int row, const int col, const int height, const int width)
{
    int lr, lc;			/* layout  row/col */
    int dr, dc;			/* display row/col */
    int p1, p2;			/* start/end positon of changed area */
    int eq;			/* counter for equal contents */

    /* loop over layout rows */
    for (lr = row; lr < LROWS && lr < row + height; lr++) {
	/* transform layout to display row */
	dr = lr;
	/* sanity check */
	if (dr < 0 || dr >= DROWS)
	    continue;
	/* loop over layout cols */
	for (lc = col; lc < LCOLS && lc < col + width; lc++) {
	    /* transform layout to display column */
	    dc = lc;
	    /* sanity check */
	    if (dc < 0 || dc >= DCOLS)
		continue;
	    /* find start of difference */
	    if (DisplayFB[dr * DCOLS + dc] == LayoutFB[lr * LCOLS + lc])
		continue;
	    /* find end of difference */
	    for (p1 = dc, p2 = p1, eq = 0, lc++; lc < LCOLS && lc < col + width; lc++) {
		/* transform layout to display column */
		dc = lc;
		/* sanity check */
		if (dc < 0 || dc >= DCOLS)
		    continue;
		if (DisplayFB[dr * DCOLS + dc] == LayoutFB[lr * LCOLS + lc]) {
		    if (++eq > GOTO_COST)
			break;
		} else {
		    p2 = dc;
		    eq = 0;
		}
	    }
	    /* send to display */
	    memcpy(DisplayFB + dr * DCOLS + p1, LayoutFB + lr * LCOLS + p1, p2 - p1 + 1);
	    if (drv_generic_text_real_write)
		drv_generic_text_real_write(dr, p1, DisplayFB + dr * DCOLS + p1, p2 - p1 + 1);
	}
    }
}


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

int drv_generic_text_init(const char *section, const char *driver)
{

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

    /* init display framebuffer */
    DisplayFB = (char *) malloc(DCOLS * DROWS * sizeof(*DisplayFB));
    memset(DisplayFB, ' ', DROWS * DCOLS * sizeof(*DisplayFB));

    /* init layout framebuffer */
    LROWS = 0;
    LCOLS = 0;
    LayoutFB = NULL;
    L2D = NULL;
    drv_generic_text_resizeFB(DROWS, DCOLS);

    /* sanity check */
    if (DisplayFB == NULL || LayoutFB == NULL || L2D == NULL) {
	error("%s: framebuffer could not be allocated: malloc() failed", Driver);
	return -1;
    }

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

    return 0;
}


/* say hello to the user */
int drv_generic_text_greet(const char *msg1, const char *msg2)
{
    int i;
    int flag = 0;

    char *line1[] = { "* LCD4Linux " VERSION " *",
	"LCD4Linux " VERSION,
	"* LCD4Linux *",
	"LCD4Linux",
	"L4Linux",
	NULL
    };

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


    for (i = 0; line1[i]; i++) {
	if (strlen(line1[i]) <= (unsigned) DCOLS) {
	    if (drv_generic_text_real_write)
		drv_generic_text_real_write(0, (DCOLS - strlen(line1[i])) / 2, line1[i], strlen(line1[i]));
	    flag = 1;
	    break;
	}
    }

    if (DROWS >= 2) {
	for (i = 0; line2[i]; i++) {
	    if (strlen(line2[i]) <= (unsigned) DCOLS) {
		if (drv_generic_text_real_write)
		    drv_generic_text_real_write(1, (DCOLS - strlen(line2[i])) / 2, line2[i], strlen(line2[i]));
		flag = 1;
		break;
	    }
	}
    }

    if (msg1 && DROWS >= 3) {
	int len = strlen(msg1);
	if (len <= DCOLS) {
	    if (drv_generic_text_real_write)
		drv_generic_text_real_write(2, (DCOLS - len) / 2, msg1, len);
	    flag = 1;
	}
    }

    if (msg2 && DROWS >= 4) {
	int len = strlen(msg2);
	if (len <= DCOLS) {
	    if (drv_generic_text_real_write)
		drv_generic_text_real_write(3, (DCOLS - len) / 2, msg2, len);
	    flag = 1;
	}
    }

    return flag;
}


int drv_generic_text_draw(WIDGET * W)
{
    WIDGET_TEXT *Text = W->data;
    char *txt;
    int row, col, len;

    row = W->row;
    col = W->col;
    txt = Text->buffer;
    len = strlen(txt);

    /* maybe grow layout framebuffer */
    drv_generic_text_resizeFB(row + 1, col + len);

    /* transfer new text into layout buffer */
    memcpy(LayoutFB + row * LCOLS + col, txt, len);

    /* blit it */
    drv_generic_text_blit(row, col, 1, len);

    return 0;
}


int drv_generic_text_quit(void)
{

    if (DisplayFB) {
	free(DisplayFB);
	DisplayFB = NULL;
    }

    if (LayoutFB) {
	free(LayoutFB);
	LayoutFB = NULL;
    }

    if (L2D) {
	free(L2D);
	L2D = NULL;
    }

    if (BarFB) {
	free(BarFB);
	BarFB = NULL;
    }

    widget_unregister();

    return (0);
}


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

int drv_generic_text_icon_init(void)
{
    if (cfg_number(Section, "Icons", 0, 0, CHARS, &ICONS) < 0)
	return -1;
    if (ICONS > 0) {
	info("%s: reserving %d of %d user-defined characters for icons", Driver, ICONS, CHARS);
    }
    return 0;
}


int drv_generic_text_icon_draw(WIDGET * W)
{
    static int icon_counter = 0;
    WIDGET_ICON *Icon = W->data;
    int row, col;
    int visible;
    int invalidate = 0;
    unsigned char ascii;

    row = W->row;
    col = W->col;

    /* maybe grow layout framebuffer */
    drv_generic_text_resizeFB(row + 1, col + 1);

    /* icon deactivated? */
    if (Icon->ascii == -2)
	return 0;

    /* ASCII already assigned? */
    if (Icon->ascii == -1) {
	if (icon_counter >= ICONS) {
	    error("cannot process icon '%s': out of icons", W->name);
	    Icon->ascii = -2;
	    return -1;
	}
	icon_counter++;
	Icon->ascii = CHAR0 + CHARS - icon_counter;
    }

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

    /* maybe redefine icon */
    if (Icon->curmap != Icon->prvmap && visible) {
	Icon->prvmap = Icon->curmap;
	if (drv_generic_text_real_defchar)
	    drv_generic_text_real_defchar(Icon->ascii, Icon->bitmap + YRES * Icon->curmap);
	invalidate = INVALIDATE;
    }

    /* use blank if invisible */
    ascii = visible ? Icon->ascii : ' ';

    /* transfer icon into layout buffer */
    LayoutFB[row * LCOLS + col] = ascii;

    /* ugly invalidation: change display FB to a wrong value so blit() will really send it */
    if (invalidate) {
	DisplayFB[row * DCOLS + col] = ~ascii;
    }

    /* blit it */
    drv_generic_text_blit(row, col, 1, 1);

    return 0;

}


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

static void drv_generic_text_bar_clear(void)
{
    int i;

    for (i = 0; i < LROWS * LCOLS; i++) {
	BarFB[i].val1 = -1;
	BarFB[i].val2 = -1;
	BarFB[i].dir = 0;
	BarFB[i].style = 0;
	BarFB[i].segment = -1;
	BarFB[i].invalid = 0;
    }

    for (i = 0; i < nSegment; i++) {
	Segment[i].used = 0;
    }
}


int drv_generic_text_bar_init(const int single_segments)
{
    if (BarFB)
	free(BarFB);

    if ((BarFB = malloc(LROWS * LCOLS * sizeof(BAR))) == NULL) {
	error("bar buffer allocation failed: out of memory");
	return -1;
    }

    Single_Segments = single_segments;

    nSegment = 0;
    fSegment = 0;

    drv_generic_text_bar_clear();

    return 0;
}


void drv_generic_text_bar_add_segment(const int val1, const int val2, const DIRECTION dir, const int ascii)
{
    Segment[fSegment].val1 = val1;
    Segment[fSegment].val2 = val2;
    Segment[fSegment].dir = dir;
    if (val1 == 0 && val2 == 0)
	Segment[fSegment].style = 0;
    else
	Segment[fSegment].style = 255;
    Segment[fSegment].used = 0;
    Segment[fSegment].ascii = ascii;

    fSegment++;
    nSegment = fSegment;
}


static void drv_generic_text_bar_create_bar(int row, int col, const DIRECTION dir, STYLE style, int len, int val1,
					    int val2)
{
    int rev = 0, max;
    if (style)
	BarFB[row * LCOLS + col].style = STYLE_FIRST;

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

    case DIR_EAST:
	while (len > 0 && col < LCOLS) {
	    BarFB[row * LCOLS + col].dir = dir;
	    BarFB[row * LCOLS + col].segment = -1;
	    if (style && BarFB[row * LCOLS + col].style == 0)
		BarFB[row * LCOLS + col].style = style;
	    if (val1 >= XRES) {
		BarFB[row * LCOLS + col].val1 = rev ? 0 : XRES;
		val1 -= XRES;
	    } else {
		BarFB[row * LCOLS + col].val1 = rev ? XRES - val1 : val1;
		val1 = 0;
	    }
	    if (val2 >= XRES) {
		BarFB[row * LCOLS + col].val2 = rev ? 0 : XRES;
		val2 -= XRES;
	    } else {
		BarFB[row * LCOLS + col].val2 = rev ? XRES - val2 : val2;
		val2 = 0;
	    }
	    len--;
	    col++;
	}
	if (style)
	    BarFB[row * LCOLS + col - 1].style = STYLE_LAST;
	break;

    case DIR_NORTH:
	max = len * YRES;
	val1 = max - val1;
	val2 = max - val2;
	rev = 1;

    case DIR_SOUTH:
	while (len > 0 && row < LROWS) {
	    BarFB[row * LCOLS + col].dir = dir;
	    BarFB[row * LCOLS + col].segment = -1;
	    if (val1 >= YRES) {
		BarFB[row * LCOLS + col].val1 = rev ? 0 : YRES;
		val1 -= YRES;
	    } else {
		BarFB[row * LCOLS + col].val1 = rev ? YRES - val1 : val1;
		val1 = 0;
	    }
	    if (val2 >= YRES) {
		BarFB[row * LCOLS + col].val2 = rev ? 0 : YRES;
		val2 -= YRES;
	    } else {
		BarFB[row * LCOLS + col].val2 = rev ? YRES - val2 : val2;
		val2 = 0;
	    }
	    len--;
	    row++;
	}
	break;
    }
}


static void drv_generic_text_bar_create_segments(void)
{
    int i, j, n;
    int res, l1, l2;

    /* find first unused segment */
    for (i = fSegment; i < nSegment && Segment[i].used; i++);

    /* pack unused segments */
    for (j = i + 1; j < nSegment; j++) {
	if (Segment[j].used)
	    Segment[i++] = Segment[j];
    }
    nSegment = i;

    /* create needed segments */
    for (n = 0; n < LROWS * LCOLS; n++) {
	if (BarFB[n].dir == 0)
	    continue;
	res = BarFB[n].dir & (DIR_EAST | DIR_WEST) ? XRES : YRES;
	for (i = 0; i < nSegment; i++) {
	    l1 = Segment[i].val1;
	    if (l1 > res)
		l1 = res;
	    l2 = Segment[i].val2;
	    if (l2 > res)
		l2 = res;

	    /* same value */
	    if (l1 == BarFB[n].val1 && l2 == BarFB[n].val2) {
		/* empty block, only style is interesting */
		if (l1 == 0 && l2 == 0 && Segment[i].style == BarFB[n].style)
		    break;
		/* full block, style doesn't matter */
		if (l1 == res && l2 == res)
		    break;
		/* half upper block */
		if (l1 == res && l2 == 0 && Segment[i].style == BarFB[n].style)
		    break;
		/* half lower block */
		if (l1 == 0 && l2 == res && Segment[i].style == BarFB[n].style)
		    break;
		/* same style, same direction */
		if (Segment[i].style == BarFB[n].style && Segment[i].dir & BarFB[n].dir)
		    break;
#if 0
		/* hollow style, val(1,2) == 1, like '[' */
		if (l1 == 1 && l2 == 1 && Segment[i].style == STYLE_FIRST && BarFB[n].style == STYLE_HOLLOW)
		    break;
		/* hollow style, val(1,2) == 1, like ']' */
		if (l1 == 1 && l2 == 1 && Segment[i].style == STYLE_LAST && BarFB[n].style == STYLE_HOLLOW)
		    break;
#endif
	    }
	}
	if (i == nSegment) {
	    nSegment++;
	    Segment[i].val1 = BarFB[n].val1;
	    Segment[i].val2 = BarFB[n].val2;
	    Segment[i].dir = BarFB[n].dir;
	    Segment[i].style = BarFB[n].style;
	    Segment[i].used = 0;
	    Segment[i].ascii = -1;
	}
	BarFB[n].segment = i;
    }
}


static int drv_generic_text_bar_segment_error(const int i, const int j)
{
    int res;
    int i1, i2, j1, j2;

    if (i == j)
	return 65535;
    if (!(Segment[i].dir & Segment[j].dir))
	return 65535;
    if (Segment[i].style != Segment[j].style)
	return 65535;

    res = Segment[i].dir & (DIR_EAST | DIR_WEST) ? XRES : YRES;

    i1 = Segment[i].val1;
    if (i1 > res)
	i1 = res;
    i2 = Segment[i].val2;
    if (i2 > res)
	i2 = res;
    j1 = Segment[j].val1;
    if (j1 > res)
	j1 = res;
    j2 = Segment[j].val2;
    if (j2 > res)
	j2 = res;

    /* do not replace empty with non-empty */
    if (i1 == 0 && j1 != 0)
	return 65535;
    if (i2 == 0 && j2 != 0)
	return 65535;

    /* do not replace full with non-full */
    if (i1 == res && j1 < res)
	return 65535;
    if (i2 == res && j2 < res)
	return 65535;

    /* do not replace start line */
    /* but only if there are at least some chars available */
    if (CHARS - ICONS > 0) {
	if (i1 == 1 && j1 != 1 && i2 > 0)
	    return 65535;
	if (i2 == 1 && j2 != 1 && j1 > 0)
	    return 65535;
    }

    /* do not replace equal length with non-equal length */
    if (i1 == i2 && j1 != j2)
	return 65535;

    return (i1 - j1) * (i1 - j1) + (i2 - j2) * (i2 - j2);
}


static void drv_generic_text_bar_pack_segments(void)
{
    int i, j, n, min;
    int pack_i, pack_j;
    int pass1 = 1;
    int error[nSegment][nSegment];

    if (nSegment <= fSegment + CHARS - ICONS) {
	return;
    }

    for (i = 0; i < nSegment; i++) {
	for (j = 0; j < nSegment; j++) {
	    error[i][j] = drv_generic_text_bar_segment_error(i, j);
	    /* debug ("[%d][%d] = %d", i, j, error[i][j]); */
	}
    }

    while (nSegment > fSegment + CHARS - ICONS) {

	min = 65535;
	pack_i = -1;
	pack_j = -1;
	for (i = fSegment; i < nSegment; i++) {
	    if (pass1 && Segment[i].used)
		continue;
	    for (j = 0; j < nSegment; j++) {
		if (error[i][j] < min) {
		    min = error[i][j];
		    pack_i = i;
		    pack_j = j;
		}
	    }
	}
	if (pack_i == -1) {
	    if (pass1) {
		pass1 = 0;
		continue;
	    } else {
		error("unable to compact bar characters");
		error("nSegment=%d fSegment=%d CHARS=%d ICONS=%d", nSegment, fSegment, CHARS, ICONS);
		error("Segment[0].val1=%d val2=%d", Segment[0].val1, Segment[0].val2);
		error("Segment[1].val1=%d val2=%d", Segment[1].val1, Segment[1].val2);
		error("Segment[2].val1=%d val2=%d", Segment[2].val1, Segment[2].val2);
		nSegment = CHARS - ICONS;
		break;
	    }
	}
#if 0
	debug("pack_segment: n=%d i=%d j=%d min=%d", nSegment, pack_i, pack_j, min);
	debug("Pack_segment: i1=%d i2=%d j1=%d j2=%d\n", Segment[pack_i].val1, Segment[pack_i].val2,
	      Segment[pack_j].val1, Segment[pack_j].val2);
#endif

	nSegment--;
	Segment[pack_i] = Segment[nSegment];

	for (i = 0; i < nSegment; i++) {
	    error[pack_i][i] = error[nSegment][i];
	    error[i][pack_i] = error[i][nSegment];
	}

	for (n = 0; n < LROWS * LCOLS; n++) {
	    if (BarFB[n].segment == pack_i)
		BarFB[n].segment = pack_j;
	    if (BarFB[n].segment == nSegment)
		BarFB[n].segment = pack_i;
	}
    }
}


static void drv_generic_text_bar_define_chars(void)
{
    int c, i, j;
    unsigned char buffer[8];

    for (i = fSegment; i < nSegment; i++) {
	if (Segment[i].used)
	    continue;
	if (Segment[i].ascii != -1)
	    continue;
	for (c = 0; c < CHARS - ICONS; c++) {
	    for (j = fSegment; j < nSegment; j++) {
		if (Segment[j].ascii == c)
		    break;
	    }
	    if (j == nSegment)
		break;
	}
	Segment[i].ascii = c;
	switch (Segment[i].dir) {
	case DIR_WEST:
	    if (Segment[i].style) {
		buffer[0] = 255;
		buffer[7] = 255;
		if (Segment[i].style & (STYLE_FIRST | STYLE_LAST))
		    for (j = 1; j < 7; j++) {
			buffer[j] |= Segment[i].style & STYLE_FIRST ? 16 : 1;
		    }
	    }
	    break;
	case DIR_EAST:
	    for (j = 0; j < 4; j++) {
		buffer[j] = 255 << (XRES - Segment[i].val1);
		buffer[j + 4] = 255 << (XRES - Segment[i].val2);
	    }
	    if (Segment[i].style) {
		buffer[0] = 255;
		buffer[7] = 255;
		if (Segment[i].style & (STYLE_FIRST | STYLE_LAST))
		    for (j = 1; j < 7; j++) {
			buffer[j] |= Segment[i].style & STYLE_FIRST ? 16 : 1;
		    }
	    }
	    break;
	case DIR_NORTH:
	    for (j = 0; j < Segment[i].val1; j++) {
		buffer[7 - j] = (1 << XRES) - 1;
	    }
	    for (; j < YRES; j++) {
		buffer[7 - j] = 0;
	    }
	    break;
	case DIR_SOUTH:
	    for (j = 0; j < Segment[i].val1; j++) {
		buffer[j] = (1 << XRES) - 1;
	    }
	    for (; j < YRES; j++) {
		buffer[j] = 0;
	    }
	    break;
	}
	if (drv_generic_text_real_defchar)
	    drv_generic_text_real_defchar(CHAR0 + c, buffer);

	/* maybe invalidate framebuffer */
	if (INVALIDATE) {
	    for (j = 0; j < LROWS * LCOLS; j++) {
		if (BarFB[j].segment == i) {
		    BarFB[j].invalid = 1;
		}
	    }
	}
    }
}


int drv_generic_text_bar_draw(WIDGET * W)
{
    WIDGET_BAR *Bar = W->data;
    int row, col, len, res, max, val1, val2;
    int c, n, s;
    DIRECTION dir;
    STYLE style;

    row = W->row;
    col = W->col;
    dir = Bar->direction;
    style = Bar->style;
    len = Bar->length;

    /* maybe grow layout framebuffer */
    /* bars *always* grow heading North or East! */
    if (dir & (DIR_EAST | DIR_WEST)) {
	drv_generic_text_resizeFB(row + 1, col + len);
    } else {
	drv_generic_text_resizeFB(row + 1, col + 1);
    }

    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;

    if (Single_Segments)
	val2 = val1;

    /* create this bar */
    drv_generic_text_bar_create_bar(row, col, dir, style, len, val1, val2);

    /* process all bars */
    drv_generic_text_bar_create_segments();
    drv_generic_text_bar_pack_segments();
    drv_generic_text_bar_define_chars();

    /* reset usage flags */
    for (s = 0; s < nSegment; s++) {
	Segment[s].used = 0;
    }

    /* set usage flags */
    for (n = 0; n < LROWS * LCOLS; n++) {
	if ((s = BarFB[n].segment) != -1)
	    Segment[s].used = 1;
    }

    /* transfer bars into layout buffer */
    for (row = 0; row < LROWS; row++) {
	for (col = 0; col < LCOLS; col++) {
	    n = row * LCOLS + col;
	    s = BarFB[n].segment;
	    if (s == -1)
		continue;
	    c = Segment[s].ascii;
	    if (c == -1)
		continue;
	    if (s >= fSegment)
		c += CHAR0;	/* ascii offset for user-defineable chars */
	    LayoutFB[n] = c;
	    /* maybe invalidate display framebuffer */
	    if (BarFB[n].invalid) {
		BarFB[n].invalid = 0;
		/* ugly invalidation: change display FB to a wrong value so blit() will really send it */
		DisplayFB[row * DCOLS + col] = ~LayoutFB[n];
	    }
	}
    }

    /* blit whole layout FB */
    drv_generic_text_blit(0, 0, LROWS, LCOLS);


    return 0;

}