root/branches/volker_dev/drv_generic_text.c

Revision 771, 23.1 kB (checked in by michael, 23 months ago)

lots of compiler warnings removed, C++-style comments removed, changed struc initialisation from 'field:value' to '.field=value'

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