root/tags/0.10.0/drv_generic_text.c

Revision 547, 25.5 kB (checked in by reinelt, 4 years ago)

[lcd4linux @ 2005-05-08 04:32:43 by reinelt]
CodingStyle? added and applied

Line 
1/* $Id: drv_generic_text.c,v 1.27 2005/05/08 04:32:44 reinelt Exp $
2 *
3 * generic driver helper for text-based displays
4 *
5 * Copyright (C) 1999, 2000 Michael Reinelt <reinelt@eunet.at>
6 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
7 *
8 * This file is part of LCD4Linux.
9 *
10 * LCD4Linux is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2, or (at your option)
13 * any later version.
14 *
15 * LCD4Linux is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *
25 * $Log: drv_generic_text.c,v $
26 * Revision 1.27  2005/05/08 04:32:44  reinelt
27 * CodingStyle added and applied
28 *
29 * Revision 1.26  2005/05/06 06:37:34  reinelt
30 * hollow bar patch from geronet
31 *
32 * Revision 1.25  2005/02/24 07:06:48  reinelt
33 * SimpleLCD driver added
34 *
35 * Revision 1.24  2005/01/18 06:30:23  reinelt
36 * added (C) to all copyright statements
37 *
38 * Revision 1.23  2005/01/09 10:53:24  reinelt
39 * small type in plugin_uname fixed
40 * new homepage lcd4linux.bulix.org
41 *
42 * Revision 1.22  2004/11/28 15:50:24  reinelt
43 * Cwlinux fixes (invalidation of user-defined chars)
44 *
45 * Revision 1.21  2004/06/26 12:04:59  reinelt
46 *
47 * uh-oh... the last CVS log message messed up things a lot...
48 *
49 * Revision 1.20  2004/06/26 09:27:21  reinelt
50 *
51 * added '-W' to CFLAGS
52 * changed all C++ comments to C ones
53 * cleaned up a lot of signed/unsigned mistakes
54 *
55 * Revision 1.19  2004/06/26 06:12:15  reinelt
56 *
57 * support for Beckmann+Egle Compact Terminals
58 * some mostly cosmetic changes in the MatrixOrbital and USBLCD driver
59 * added debugging to the generic serial driver
60 * fixed a bug in the generic text driver where icons could be drawn outside
61 * the display bounds
62 *
63 * Revision 1.18  2004/06/20 10:09:55  reinelt
64 *
65 * 'const'ified the whole source
66 *
67 * Revision 1.17  2004/06/05 06:41:40  reinelt
68 *
69 * chancged splash screen again
70 *
71 * Revision 1.16  2004/06/02 10:09:22  reinelt
72 *
73 * splash screen for HD44780
74 *
75 * Revision 1.15  2004/06/01 06:45:30  reinelt
76 *
77 * some Fixme's processed
78 * documented some code
79 *
80 * Revision 1.14  2004/05/26 11:37:36  reinelt
81 *
82 * Curses driver ported.
83 *
84 * Revision 1.13  2004/03/19 09:17:46  reinelt
85 *
86 * removed the extra 'goto' function, row and col are additional parameters
87 * of the write() function now.
88 *
89 * Revision 1.12  2004/03/03 03:47:04  reinelt
90 * big patch from Martin Hejl:
91 * - use qprintf() where appropriate
92 * - save CPU cycles on gettimeofday()
93 * - add quit() functions to free allocated memory
94 * - fixed lots of memory leaks
95 *
96 * Revision 1.11  2004/02/15 21:43:43  reinelt
97 * T6963 driver nearly finished
98 * framework for graphic displays done
99 * i2c_sensors patch from Xavier
100 * some more old generation files removed
101 *
102 * Revision 1.10  2004/02/14 11:56:17  reinelt
103 * M50530 driver ported
104 * changed lots of 'char' to 'unsigned char'
105 *
106 * Revision 1.9  2004/02/07 13:45:23  reinelt
107 * icon visibility patch #2 from Xavier
108 *
109 * Revision 1.8  2004/01/30 20:57:56  reinelt
110 * HD44780 patch from Martin Hejl
111 * dmalloc integrated
112 *
113 * Revision 1.7  2004/01/29 04:40:02  reinelt
114 * every .c file includes "config.h" now
115 *
116 * Revision 1.6  2004/01/23 07:04:23  reinelt
117 * icons finished!
118 *
119 * Revision 1.5  2004/01/23 04:53:54  reinelt
120 * icon widget added (not finished yet!)
121 *
122 * Revision 1.4  2004/01/22 07:57:45  reinelt
123 * several bugs fixed where segfaulting on layout>display
124 * Crystalfontz driver optimized, 632 display already works
125 *
126 * Revision 1.3  2004/01/20 14:25:12  reinelt
127 * some reorganization
128 * moved drv_generic to drv_generic_serial
129 * moved port locking stuff to drv_generic_serial
130 *
131 * Revision 1.2  2004/01/20 12:45:47  reinelt
132 * "Default screen" working with MatrixOrbital
133 *
134 * Revision 1.1  2004/01/20 05:36:59  reinelt
135 * moved text-display-specific stuff to drv_generic_text
136 * moved all the bar stuff from drv_generic_bar to generic_text
137 *
138 */
139
140/*
141 *
142 * exported variables:
143 *
144 * extern int DROWS, DCOLS;    display size
145 * extern int LROWS, LCOLS;    layout size
146 * extern int XRES,  YRES;     pixel width/height of one char
147 * extern int CHARS, CHAR0;    number of user-defineable characters, ASCII of first char
148 * extern int ICONS;           number of user-defineable characters reserved for icons
149 * extern int GOTO_COST;       number of bytes a goto command requires
150 * extern int INVALIDATE;      re-send a modified userdefined char?
151 *
152 *
153 * these functions must be implemented by the real driver:
154 *
155 * void (*drv_generic_text_real_write)(int row, int col, unsigned char *data, int len);
156 *  writes a text of specified length at position (row, col)
157 *
158 * void (*drv_generic_text_real_defchar)(int ascii, unsigned char *buffer);
159 *  defines the bitmap of a user-defined character
160 *
161 *
162 * exported fuctions:
163 *
164 * int drv_generic_text_init (char *section, char *driver);
165 *   initializes the generic text driver
166 *
167 * int drv_generic_text_draw (WIDGET *W);
168 *   renders Text widget into framebuffer
169 *   calls drv_generic_text_real_write()
170 *
171 * int drv_generic_text_icon_init       (void);
172 *   initializes the generic icon driver
173 *   
174 * int drv_generic_text_icon_draw (WIDGET *W);
175 *   renders Icon widget into framebuffer
176 *   calls drv_generic_text_real_write() and drv_generic_text_real_defchar()
177 *
178 * int drv_generic_text_bar_init (int single_segments);
179 *   initializes the generic icon driver
180 *
181 * void drv_generic_text_bar_add_segment (int val1, int val2, DIRECTION dir, int ascii);
182 *   adds a 'fixed' character to the bar-renderer
183 *
184 * int drv_generic_text_bar_draw (WIDGET *W);
185 *   renders Bar widget into framebuffer
186 *   calls drv_generic_text_real_write() and drv_generic_text_real_defchar()
187 *
188 * int drv_generic_text_quit (void);
189 *   closes the generic text driver
190 *
191 */
192
193#include "config.h"
194
195#include <stdlib.h>
196#include <stdio.h>
197#include <string.h>
198#include <errno.h>
199#include <unistd.h>
200#include <termios.h>
201#include <fcntl.h>
202
203#include "debug.h"
204#include "cfg.h"
205#include "plugin.h"
206#include "widget.h"
207#include "widget_text.h"
208#include "widget_icon.h"
209#include "widget_bar.h"
210#include "drv.h"
211#include "drv_generic_text.h"
212
213#ifdef WITH_DMALLOC
214#include <dmalloc.h>
215#endif
216
217
218typedef struct {
219    int val1;
220    int val2;
221    DIRECTION dir;
222    STYLE style;
223    int segment;
224    int invalid;
225} BAR;
226
227typedef struct {
228    int val1;
229    int val2;
230    DIRECTION dir;
231    STYLE style;
232    int used;
233    int ascii;
234} SEGMENT;
235
236static char *Section = NULL;
237static char *Driver = NULL;
238
239
240int DROWS = 20;     /* display size: rows */
241int DCOLS = 4;      /* display size: columns */
242int LROWS = 20;     /* layout size: rows */
243int LCOLS = 4;      /* layout size: columns */
244int XRES = 6;     /* pixels of one char cell */
245int YRES = 8;     /* pixels of one char cell */
246int CHARS = 0;      /* number of user-defineable characters */
247int CHAR0 = 0;      /* ASCII of first user-defineable char */
248int ICONS = 0;      /* number of user-defineable characters reserved for icons */
249
250int GOTO_COST = 0;    /* number of bytes a goto command requires */
251int INVALIDATE = 0;   /* re-send a modified userdefined char? */
252
253
254static char *LayoutFB = NULL;
255static char *DisplayFB = NULL;
256
257static int Single_Segments = 0;
258
259static int nSegment = 0;
260static int fSegment = 0;
261static SEGMENT Segment[128];
262static BAR *BarFB = NULL;
263
264
265
266/****************************************/
267/*** generic Framebuffer stuff        ***/
268/****************************************/
269
270static void drv_generic_text_resizeFB(int rows, int cols)
271{
272    char *newFB;
273    BAR *newBar;
274    int i, row, col;
275
276    /* Layout FB is large enough */
277    if (rows <= LROWS && cols <= LCOLS)
278  return;
279
280    /* get maximum values */
281    if (rows < LROWS)
282  rows = LROWS;
283    if (cols < LCOLS)
284  cols = LCOLS;
285
286    /* allocate new Layout FB */
287    newFB = malloc(cols * rows * sizeof(char));
288    memset(newFB, ' ', rows * cols * sizeof(char));
289
290    /* transfer contents */
291    if (LayoutFB != NULL) {
292  for (row = 0; row < LROWS; row++) {
293      for (col = 0; col < LCOLS; col++) {
294    newFB[row * cols + col] = LayoutFB[row * LCOLS + col];
295      }
296  }
297  free(LayoutFB);
298    }
299    LayoutFB = newFB;
300
301
302    /* resize Bar buffer */
303    if (BarFB) {
304
305  newBar = malloc(rows * cols * sizeof(BAR));
306
307  for (i = 0; i < rows * cols; i++) {
308      newBar[i].val1 = -1;
309      newBar[i].val2 = -1;
310      newBar[i].dir = 0;
311      newBar[i].segment = -1;
312      newBar[i].invalid = 0;
313  }
314
315  /* transfer contents */
316  for (row = 0; row < LROWS; row++) {
317      for (col = 0; col < LCOLS; col++) {
318    newBar[row * cols + col] = BarFB[row * LCOLS + col];
319      }
320  }
321
322  free(BarFB);
323  BarFB = newBar;
324    }
325
326    LCOLS = cols;
327    LROWS = rows;
328}
329
330
331/****************************************/
332/*** generic text handling            ***/
333/****************************************/
334
335int drv_generic_text_init(const char *section, const char *driver)
336{
337
338    Section = (char *) section;
339    Driver = (char *) driver;
340
341    /* init display framebuffer */
342    DisplayFB = (char *) malloc(DCOLS * DROWS * sizeof(char));
343    memset(DisplayFB, ' ', DROWS * DCOLS * sizeof(char));
344
345    /* init layout framebuffer */
346    LROWS = 0;
347    LCOLS = 0;
348    LayoutFB = NULL;
349    drv_generic_text_resizeFB(DROWS, DCOLS);
350
351    /* sanity check */
352    if (LayoutFB == NULL || DisplayFB == NULL) {
353  error("%s: framebuffer could not be allocated: malloc() failed", Driver);
354  return -1;
355    }
356
357    return 0;
358}
359
360
361/* say hello to the user */
362int drv_generic_text_greet(const char *msg1, const char *msg2)
363{
364    int i;
365    int flag = 0;
366
367    char *line1[] = { "* LCD4Linux " VERSION " *",
368  "LCD4Linux " VERSION,
369  "* LCD4Linux *",
370  "LCD4Linux",
371  "L4Linux",
372  NULL
373    };
374
375    char *line2[] = { "http://lcd4linux.bulix.org",
376  "lcd4linux.bulix.org",
377  NULL
378    };
379
380
381    for (i = 0; line1[i]; i++) {
382  if (strlen(line1[i]) <= (unsigned) DCOLS) {
383      drv_generic_text_real_write(0, (DCOLS - strlen(line1[i])) / 2, line1[i], strlen(line1[i]));
384      flag = 1;
385      break;
386  }
387    }
388
389    if (DROWS >= 2) {
390  for (i = 0; line2[i]; i++) {
391      if (strlen(line2[i]) <= (unsigned) DCOLS) {
392    drv_generic_text_real_write(1, (DCOLS - strlen(line2[i])) / 2, line2[i], strlen(line2[i]));
393    flag = 1;
394    break;
395      }
396  }
397    }
398
399    if (msg1 && DROWS >= 3) {
400  int len = strlen(msg1);
401  if (len <= DCOLS) {
402      drv_generic_text_real_write(2, (DCOLS - len) / 2, msg1, len);
403      flag = 1;
404  }
405    }
406
407    if (msg2 && DROWS >= 4) {
408  int len = strlen(msg2);
409  if (len <= DCOLS) {
410      drv_generic_text_real_write(3, (DCOLS - len) / 2, msg2, len);
411      flag = 1;
412  }
413    }
414
415    return flag;
416}
417
418
419int drv_generic_text_draw(WIDGET * W)
420{
421    WIDGET_TEXT *Text = W->data;
422    char *txt, *fb1, *fb2;
423    int row, col, col0, len, end;
424
425    row = W->row;
426    col = W->col;
427    txt = Text->buffer;
428    len = strlen(txt);
429    end = col + len;
430
431    /* maybe grow layout framebuffer */
432    drv_generic_text_resizeFB(row + 1, col + len);
433
434    fb1 = LayoutFB + row * LCOLS;
435    fb2 = DisplayFB + row * DCOLS;
436
437    /* transfer new text into layout buffer */
438    memcpy(fb1 + col, txt, len);
439
440    if (row < DROWS) {
441  for (; col <= end && col < DCOLS; col++) {
442      int pos1, pos2, equal;
443      if (fb1[col] == fb2[col])
444    continue;
445      col0 = col;
446      for (pos1 = col, pos2 = pos1, col++, equal = 0; col <= end && col < DCOLS; col++) {
447    if (fb1[col] == fb2[col]) {
448        /* If we find just one equal byte, we don't break, because this  */
449        /* would require a goto, which takes several bytes, too. */
450        if (GOTO_COST >= 0 && ++equal > GOTO_COST)
451      break;
452    } else {
453        pos2 = col;
454        equal = 0;
455    }
456      }
457      memcpy(fb2 + pos1, fb1 + pos1, pos2 - pos1 + 1);
458      drv_generic_text_real_write(row, col0, fb2 + pos1, pos2 - pos1 + 1);
459  }
460    }
461
462    return 0;
463}
464
465
466int drv_generic_text_quit(void)
467{
468
469    if (LayoutFB) {
470  free(LayoutFB);
471  LayoutFB = NULL;
472    }
473
474    if (DisplayFB) {
475  free(DisplayFB);
476  DisplayFB = NULL;
477    }
478
479    if (BarFB) {
480  free(BarFB);
481  BarFB = NULL;
482    }
483    widget_unregister();
484
485    return (0);
486}
487
488
489/****************************************/
490/*** generic icon handling            ***/
491/****************************************/
492
493int drv_generic_text_icon_init(void)
494{
495    if (cfg_number(Section, "Icons", 0, 0, CHARS, &ICONS) < 0)
496  return -1;
497    if (ICONS > 0) {
498  info("%s: reserving %d of %d user-defined characters for icons", Driver, ICONS, CHARS);
499    }
500    return 0;
501}
502
503
504int drv_generic_text_icon_draw(WIDGET * W)
505{
506    static int icon_counter = 0;
507    WIDGET_ICON *Icon = W->data;
508    int row, col;
509    int l_idx, d_idx;
510    int invalidate = 0;
511    unsigned char ascii;
512
513    row = W->row;
514    col = W->col;
515
516    /* maybe grow layout framebuffer */
517    drv_generic_text_resizeFB(row + 1, col + 1);
518
519    /* icon deactivated? */
520    if (Icon->ascii == -2)
521  return 0;
522
523    /* ASCII already assigned? */
524    if (Icon->ascii == -1) {
525  if (icon_counter >= ICONS) {
526      error("cannot process icon '%s': out of icons", W->name);
527      Icon->ascii = -2;
528      return -1;
529  }
530  icon_counter++;
531  Icon->ascii = CHAR0 + CHARS - icon_counter;
532    }
533
534    /* maybe redefine icon */
535    if (Icon->curmap != Icon->prvmap && Icon->visible) {
536  Icon->prvmap = Icon->curmap;
537  drv_generic_text_real_defchar(Icon->ascii, Icon->bitmap + YRES * Icon->curmap);
538  invalidate = INVALIDATE;
539    }
540
541    /* use blank if invisible */
542    ascii = Icon->visible ? Icon->ascii : ' ';
543
544    /* index into the two framebuffers */
545    l_idx = row * LCOLS + col;
546    d_idx = row * DCOLS + col;
547
548    /* transfer icon into layout buffer */
549    LayoutFB[l_idx] = ascii;
550
551    /* maybe send icon to the display */
552    if (row < DROWS && col < DCOLS && (DisplayFB[d_idx] != ascii || invalidate)) {
553  DisplayFB[d_idx] = ascii;
554  drv_generic_text_real_write(row, col, DisplayFB + d_idx, 1);
555    }
556
557    return 0;
558
559}
560
561
562/****************************************/
563/*** generic bar handling             ***/
564/****************************************/
565
566static void drv_generic_text_bar_clear(void)
567{
568    int i;
569
570    for (i = 0; i < LROWS * LCOLS; i++) {
571  BarFB[i].val1 = -1;
572  BarFB[i].val2 = -1;
573  BarFB[i].dir = 0;
574  BarFB[i].style = 0;
575  BarFB[i].segment = -1;
576  BarFB[i].invalid = 0;
577    }
578
579    for (i = 0; i < nSegment; i++) {
580  Segment[i].used = 0;
581    }
582}
583
584
585int drv_generic_text_bar_init(const int single_segments)
586{
587    if (BarFB)
588  free(BarFB);
589
590    if ((BarFB = malloc(LROWS * LCOLS * sizeof(BAR))) == NULL) {
591  error("bar buffer allocation failed: out of memory");
592  return -1;
593    }
594
595    Single_Segments = single_segments;
596
597    nSegment = 0;
598    fSegment = 0;
599
600    drv_generic_text_bar_clear();
601
602    return 0;
603}
604
605
606void drv_generic_text_bar_add_segment(const int val1, const int val2, const DIRECTION dir, const int ascii)
607{
608    Segment[fSegment].val1 = val1;
609    Segment[fSegment].val2 = val2;
610    Segment[fSegment].dir = dir;
611    if (val1 == 0 && val2 == 0)
612  Segment[fSegment].style = 0;
613    else
614  Segment[fSegment].style = 255;
615    Segment[fSegment].used = 0;
616    Segment[fSegment].ascii = ascii;
617
618    fSegment++;
619    nSegment = fSegment;
620}
621
622
623static void drv_generic_text_bar_create_bar(int row, int col, const DIRECTION dir, STYLE style, int len, int val1, int val2)
624{
625    int rev = 0, max;
626    if (style)
627  BarFB[row * LCOLS + col].style = STYLE_FIRST;
628
629    switch (dir) {
630    case DIR_WEST:
631  max = len * XRES;
632  val1 = max - val1;
633  val2 = max - val2;
634  rev = 1;
635
636    case DIR_EAST:
637  while (len > 0 && col < LCOLS) {
638      BarFB[row * LCOLS + col].dir = dir;
639      BarFB[row * LCOLS + col].segment = -1;
640      if (style && BarFB[row * LCOLS + col].style == 0)
641    BarFB[row * LCOLS + col].style = STYLE_HOLLOW;
642      if (val1 >= XRES) {
643    BarFB[row * LCOLS + col].val1 = rev ? 0 : XRES;
644    val1 -= XRES;
645      } else {
646    BarFB[row * LCOLS + col].val1 = rev ? XRES - val1 : val1;
647    val1 = 0;
648      }
649      if (val2 >= XRES) {
650    BarFB[row * LCOLS + col].val2 = rev ? 0 : XRES;
651    val2 -= XRES;
652      } else {
653    BarFB[row * LCOLS + col].val2 = rev ? XRES - val2 : val2;
654    val2 = 0;
655      }
656      len--;
657      col++;
658  }
659  if (style)
660      BarFB[row * LCOLS + col - 1].style = STYLE_LAST;
661  break;
662
663    case DIR_SOUTH:
664  max = len * YRES;
665  val1 = max - val1;
666  val2 = max - val2;
667  rev = 1;
668
669    case DIR_NORTH:
670  while (len > 0 && row < LROWS) {
671      BarFB[row * LCOLS + col].dir = dir;
672      BarFB[row * LCOLS + col].segment = -1;
673      if (val1 >= YRES) {
674    BarFB[row * LCOLS + col].val1 = rev ? 0 : YRES;
675    val1 -= YRES;
676      } else {
677    BarFB[row * LCOLS + col].val1 = rev ? YRES - val1 : val1;
678    val1 = 0;
679      }
680      if (val2 >= YRES) {
681    BarFB[row * LCOLS + col].val2 = rev ? 0 : YRES;
682    val2 -= YRES;
683      } else {
684    BarFB[row * LCOLS + col].val2 = rev ? YRES - val2 : val2;
685    val2 = 0;
686      }
687      len--;
688      row++;
689  }
690  break;
691    }
692}
693
694
695static void drv_generic_text_bar_create_segments(void)
696{
697    int i, j, n;
698    int res, l1, l2;
699
700    /* find first unused segment */
701    for (i = fSegment; i < nSegment && Segment[i].used; i++);
702
703    /* pack unused segments */
704    for (j = i + 1; j < nSegment; j++) {
705  if (Segment[j].used)
706      Segment[i++] = Segment[j];
707    }
708    nSegment = i;
709
710    /* create needed segments */
711    for (n = 0; n < LROWS * LCOLS; n++) {
712  if (BarFB[n].dir == 0)
713      continue;
714  res = BarFB[n].dir & (DIR_EAST | DIR_WEST) ? XRES : YRES;
715  for (i = 0; i < nSegment; i++) {
716      l1 = Segment[i].val1;
717      if (l1 > res)
718    l1 = res;
719      l2 = Segment[i].val2;
720      if (l2 > res)
721    l2 = res;
722
723      /* same value */
724      if (l1 == BarFB[n].val1 && l2 == BarFB[n].val2) {
725    /* empty block, only style is interesting */
726    if (l1 == 0 && l2 == 0 && Segment[i].style == BarFB[n].style)
727        break;
728    /* full block, style doesn't matter */
729    if (l1 == res && l2 == res)
730        break;
731    /* half upper block */
732    if (l1 == res && l2 == 0 && Segment[i].style == BarFB[n].style)
733        break;
734    /* half lower block */
735    if (l1 == 0 && l2 == res && Segment[i].style == BarFB[n].style)
736        break;
737    /* same style, same direction */
738    if (Segment[i].style == BarFB[n].style && Segment[i].dir & BarFB[n].dir)
739        break;
740    /* hollow style, val(1,2) == 1, like '[' */
741/*                        if (l1 == 1 && l2 == 1 && Segment[i].style == STYLE_FIRST && BarFB[n].style == STYLE_HOLLOW)
742                                                                      break;
743*//* hollow style, val(1,2) == 1, like ']' */
744/*                        if (l1 == 1 && l2 == 1 && Segment[i].style == STYLE_LAST && BarFB[n].style == STYLE_HOLLOW)
745                              break;
746*/ }
747  }
748  if (i == nSegment) {
749      nSegment++;
750      Segment[i].val1 = BarFB[n].val1;
751      Segment[i].val2 = BarFB[n].val2;
752      Segment[i].dir = BarFB[n].dir;
753      Segment[i].style = BarFB[n].style;
754      Segment[i].used = 0;
755      Segment[i].ascii = -1;
756  }
757  BarFB[n].segment = i;
758    }
759}
760
761
762static int drv_generic_text_bar_segment_error(const int i, const int j)
763{
764    int res;
765    int i1, i2, j1, j2;
766
767    if (i == j)
768  return 65535;
769    if (!(Segment[i].dir & Segment[j].dir))
770  return 65535;
771    if (Segment[i].style != Segment[j].style)
772  return 65535;
773
774    res = Segment[i].dir & (DIR_EAST | DIR_WEST) ? XRES : YRES;
775
776    i1 = Segment[i].val1;
777    if (i1 > res)
778  i1 = res;
779    i2 = Segment[i].val2;
780    if (i2 > res)
781  i2 = res;
782    j1 = Segment[j].val1;
783    if (j1 > res)
784  j1 = res;
785    j2 = Segment[j].val2;
786    if (j2 > res)
787  j2 = res;
788
789    if (i1 == 0 && j1 != 0)
790  return 65535;
791    if (i2 == 0 && j2 != 0)
792  return 65535;
793    if (i1 == res && j1 < res)
794  return 65535;
795    if (i2 == res && j2 < res)
796  return 65535;
797    if (i1 == 1 && j1 != 1 && i2 > 0)
798  return 65535;
799    if (i2 == 1 && j2 != 1 && j1 > 0)
800  return 65535;
801    if (i1 == i2 && j1 != j2)
802  return 65535;
803
804    return (i1 - j1) * (i1 - j1) + (i2 - j2) * (i2 - j2);
805}
806
807
808static void drv_generic_text_bar_pack_segments(void)
809{
810    int i, j, n, min;
811    int pack_i, pack_j;
812    int pass1 = 1;
813    int error[nSegment][nSegment];
814
815    if (nSegment <= fSegment + CHARS - ICONS) {
816  return;
817    }
818
819    for (i = 0; i < nSegment; i++) {
820  for (j = 0; j < nSegment; j++) {
821      error[i][j] = drv_generic_text_bar_segment_error(i, j);
822  }
823    }
824
825    while (nSegment > fSegment + CHARS - ICONS) {
826
827  min =