Fix mistaken changes in r30219
[kugel-rb.git] / firmware / drivers / lcd-charcell.c
blobb59b270f9b2a4d04fbe6d9dcaba8ff7f609e741b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 by Jens Arnold
11 * Based on the work of Alan Korr, Kjell Ericson and others
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include <stdio.h>
24 #include "config.h"
25 #include "hwcompat.h"
26 #include "stdarg.h"
27 #include "lcd.h"
28 #include "kernel.h"
29 #include "thread.h"
30 #include "string-extra.h"
31 #include <stdlib.h>
32 #include "debug.h"
33 #include "file.h"
34 #include "system.h"
35 #include "lcd-charcell.h"
36 #include "rbunicode.h"
37 #include "scroll_engine.h"
39 /** definitions **/
41 #define VARIABLE_XCHARS 16 /* number of software user-definable characters */
42 /* There must be mappings for this many characters in the 0xe000 unicode range
43 * in lcd-charset-<target>.c */
45 #define NO_PATTERN (-1)
47 static int find_xchar(unsigned long ucs);
49 /** globals **/
51 unsigned char lcd_charbuffer[LCD_HEIGHT][LCD_WIDTH]; /* The "frame"buffer */
52 static unsigned char lcd_substbuffer[LCD_HEIGHT][LCD_WIDTH];
53 struct pattern_info lcd_patterns[MAX_HW_PATTERNS];
54 struct cursor_info lcd_cursor;
56 static unsigned char xfont_variable[VARIABLE_XCHARS][HW_PATTERN_SIZE];
57 static bool xfont_variable_locked[VARIABLE_XCHARS];
58 static int xspace; /* stores xhcar id of ' ' - often needed */
60 static struct viewport default_vp =
62 .x = 0,
63 .y = 0,
64 .width = LCD_WIDTH,
65 .height = LCD_HEIGHT,
68 static struct viewport* current_vp = &default_vp;
70 /* LCD init */
71 void lcd_init (void)
73 lcd_init_device();
74 lcd_charset_init();
75 memset(lcd_patterns, 0, sizeof(lcd_patterns));
76 xspace = find_xchar(' ');
77 memset(lcd_charbuffer, xchar_info[xspace].hw_char, sizeof(lcd_charbuffer));
78 scroll_init();
81 /* Viewports */
83 void lcd_set_viewport(struct viewport* vp)
85 if (vp == NULL)
86 current_vp = &default_vp;
87 else
88 current_vp = vp;
90 #if defined(SIMULATOR)
91 /* Force the viewport to be within bounds. If this happens it should
92 * be considered an error - the viewport will not draw as it might be
93 * expected.
95 if((unsigned) current_vp->x > (unsigned) LCD_WIDTH
96 || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT
97 || current_vp->x + current_vp->width > LCD_WIDTH
98 || current_vp->y + current_vp->height > LCD_HEIGHT)
100 #if !defined(HAVE_VIEWPORT_CLIP)
101 DEBUGF("ERROR: "
102 #else
103 DEBUGF("NOTE: "
104 #endif
105 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
106 current_vp->x, current_vp->y,
107 current_vp->width, current_vp->height);
110 #endif
113 void lcd_update_viewport(void)
115 lcd_update();
118 /** parameter handling **/
120 int lcd_getwidth(void)
122 return current_vp->width;
125 int lcd_getheight(void)
127 return current_vp->height;
130 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
132 int width = utf8length(str);
134 if (w)
135 *w = width;
136 if (h)
137 *h = 1;
139 return width;
142 /** low-level functions **/
144 static int find_xchar(unsigned long ucs)
146 int low = 0;
147 int high = xchar_info_size - 1;
151 int probe = (low + high) >> 1;
153 if (xchar_info[probe].ucs < ucs)
154 low = probe + 1;
155 else if (xchar_info[probe].ucs > ucs)
156 high = probe - 1;
157 else
158 return probe;
160 while (low <= high);
162 /* Not found: return index of no-char symbol (last symbol, hardcoded). */
163 return xchar_info_size - 1;
166 static int glyph_to_pat(unsigned glyph)
168 int i;
170 for (i = 0; i < lcd_pattern_count; i++)
171 if (lcd_patterns[i].glyph == glyph)
172 return i;
174 return NO_PATTERN;
177 static void lcd_free_pat(int pat)
179 int x, y;
181 if (pat != NO_PATTERN)
183 for (x = 0; x < LCD_WIDTH; x++)
184 for (y = 0; y < LCD_HEIGHT; y++)
185 if (pat == lcd_charbuffer[y][x])
186 lcd_charbuffer[y][x] = lcd_substbuffer[y][x];
188 if (lcd_cursor.enabled && pat == lcd_cursor.hw_char)
189 lcd_cursor.hw_char = lcd_cursor.subst_char;
191 lcd_patterns[pat].count = 0;
195 static int lcd_get_free_pat(int xchar)
197 static int last_used_pat = 0;
199 int pat = last_used_pat; /* start from last used pattern */
200 int least_pat = pat; /* pattern with least priority */
201 int least_priority = lcd_patterns[pat].priority;
202 int i;
204 for (i = 0; i < lcd_pattern_count; i++)
206 if (++pat >= lcd_pattern_count) /* Keep 'pat' within limits */
207 pat = 0;
209 if (lcd_patterns[pat].count == 0)
211 last_used_pat = pat;
212 return pat;
214 if (lcd_patterns[pat].priority < least_priority)
216 least_priority = lcd_patterns[pat].priority;
217 least_pat = pat;
220 if (xchar_info[xchar].priority > least_priority) /* prioritized char */
222 lcd_free_pat(least_pat);
223 last_used_pat = least_pat;
224 return least_pat;
226 return NO_PATTERN;
229 static const unsigned char *glyph_to_pattern(unsigned glyph)
231 if (glyph & 0x8000)
232 return xfont_variable[glyph & 0x7fff];
233 else
234 return xfont_fixed[glyph];
237 static int map_xchar(int xchar, unsigned char *substitute)
239 int pat;
240 unsigned glyph;
242 if (xchar_info[xchar].priority > 0) /* soft char */
244 glyph = xchar_info[xchar].glyph;
245 pat = glyph_to_pat(glyph);
247 if (pat == NO_PATTERN) /* not yet mapped */
249 pat = lcd_get_free_pat(xchar); /* try to map */
251 if (pat == NO_PATTERN) /* failed: just use substitute */
252 return xchar_info[xchar].hw_char;
253 else
254 { /* define pattern */
255 lcd_patterns[pat].priority = xchar_info[xchar].priority;
256 lcd_patterns[pat].glyph = glyph;
257 memcpy(lcd_patterns[pat].pattern, glyph_to_pattern(glyph),
258 HW_PATTERN_SIZE);
261 lcd_patterns[pat].count++; /* increase reference count */
262 *substitute = xchar_info[xchar].hw_char;
263 return pat;
265 else /* hardware char */
266 return xchar_info[xchar].hw_char;
269 static void lcd_putxchar(int x, int y, int xchar)
271 int lcd_char;
273 /* Adjust for viewport */
274 x += current_vp->x;
275 y += current_vp->y;
277 #if defined(HAVE_VIEWPORT_CLIP)
278 if((unsigned)x > (unsigned)LCD_WIDTH || (unsigned)y > (unsigned)LCD_HEIGHT)
279 return;
280 #endif
282 lcd_char = lcd_charbuffer[y][x];
284 if (lcd_char < lcd_pattern_count) /* old char was soft */
285 lcd_patterns[lcd_char].count--; /* decrease old reference count */
287 lcd_charbuffer[y][x] = map_xchar(xchar, &lcd_substbuffer[y][x]);
290 /** user-definable pattern handling **/
292 unsigned long lcd_get_locked_pattern(void)
294 int i = 0;
296 for (i = 0; i < VARIABLE_XCHARS; i++)
298 if (!xfont_variable_locked[i])
300 xfont_variable_locked[i] = true;
301 return 0xe000 + i; /* hard-coded */
304 return 0;
307 void lcd_unlock_pattern(unsigned long ucs)
309 int xchar = find_xchar(ucs);
310 unsigned glyph = xchar_info[xchar].glyph;
312 if (glyph & 0x8000) /* variable extended char */
314 lcd_free_pat(glyph_to_pat(glyph));
315 xfont_variable_locked[glyph & 0x7fff] = false;
319 void lcd_define_pattern(unsigned long ucs, const char *pattern)
321 int xchar = find_xchar(ucs);
322 unsigned glyph = xchar_info[xchar].glyph;
323 int pat;
325 if (glyph & 0x8000) /* variable extended char */
327 memcpy(xfont_variable[glyph & 0x7fff], pattern, HW_PATTERN_SIZE);
328 pat = glyph_to_pat(glyph);
329 if (pat != NO_PATTERN)
330 memcpy(lcd_patterns[pat].pattern, pattern, HW_PATTERN_SIZE);
334 /** output functions **/
336 /* Clear the whole display */
337 void lcd_clear_display(void)
339 int x, y;
340 struct viewport* old_vp = current_vp;
342 lcd_scroll_info.lines = 0;
343 lcd_remove_cursor();
345 /* Set the default viewport - required for lcd_putxchar */
346 current_vp = &default_vp;
348 for (x = 0; x < LCD_WIDTH; x++)
349 for (y = 0; y < LCD_HEIGHT; y++)
350 lcd_putxchar(x, y, xspace);
352 current_vp = old_vp;
355 /* Clear the current viewport */
356 void lcd_clear_viewport(void)
358 int x, y;
360 if (current_vp == &default_vp)
362 lcd_clear_display();
364 else
366 /* Remove the cursor if it is within the current viewport */
367 if (lcd_cursor.enabled &&
368 (lcd_cursor.x >= current_vp->x) &&
369 (lcd_cursor.x <= current_vp->x + current_vp->width) &&
370 (lcd_cursor.y >= current_vp->y) &&
371 (lcd_cursor.y <= current_vp->y + current_vp->height))
373 lcd_remove_cursor();
376 for (x = 0; x < current_vp->width; x++)
377 for (y = 0; y < current_vp->height; y++)
378 lcd_putxchar(x, y, xspace);
380 lcd_scroll_stop(current_vp);
384 /* Put an unicode character at the given position */
385 void lcd_putc(int x, int y, unsigned long ucs)
387 if ((unsigned)x >= (unsigned)current_vp->width ||
388 (unsigned)y >= (unsigned)current_vp->height)
389 return;
391 lcd_putxchar(x, y, find_xchar(ucs));
394 /* Show cursor (alternating with existing character) at the given position */
395 void lcd_put_cursor(int x, int y, unsigned long cursor_ucs)
397 if ((unsigned)x >= (unsigned)current_vp->width ||
398 (unsigned)y >= (unsigned)current_vp->height ||
399 lcd_cursor.enabled)
400 return;
402 lcd_cursor.enabled = true;
403 lcd_cursor.visible = false;
404 lcd_cursor.hw_char = map_xchar(find_xchar(cursor_ucs), &lcd_cursor.subst_char);
405 lcd_cursor.x = current_vp->x + x;
406 lcd_cursor.y = current_vp->y + y;
407 lcd_cursor.downcount = 0;
408 lcd_cursor.divider = MAX((HZ/2) / lcd_scroll_info.ticks, 1);
411 /* Remove the cursor */
412 void lcd_remove_cursor(void)
414 if (lcd_cursor.enabled)
416 if (lcd_cursor.hw_char < lcd_pattern_count) /* soft char, unmap */
417 lcd_patterns[lcd_cursor.hw_char].count--;
419 lcd_cursor.enabled = lcd_cursor.visible = false;
423 /* Put a string at a given position, skipping first ofs chars */
424 static int lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
426 unsigned short ucs;
427 const unsigned char *utf8 = str;
429 while (*utf8 && x < current_vp->width)
431 utf8 = utf8decode(utf8, &ucs);
433 if (ofs > 0)
435 ofs--;
436 continue;
438 lcd_putxchar(x++, y, find_xchar(ucs));
440 return x;
443 /* Put a string at a given position */
444 void lcd_putsxy(int x, int y, const unsigned char *str)
446 if ((unsigned)y >= (unsigned)current_vp->height)
447 return;
449 lcd_putsxyofs(x, y, 0, str);
452 /* Formatting version of lcd_putsxy */
453 void lcd_putsxyf(int x, int y, const unsigned char *fmt, ...)
455 va_list ap;
456 char buf[256];
457 va_start(ap, fmt);
458 vsnprintf(buf, sizeof (buf), fmt, ap);
459 va_end(ap);
460 lcd_putsxy(x, y, buf);
463 /*** Line oriented text output ***/
465 /* Put a string at a given char position */
466 void lcd_puts(int x, int y, const unsigned char *str)
468 lcd_puts_offset(x, y, str, 0);
471 /* Formatting version of lcd_puts */
472 void lcd_putsf(int x, int y, const unsigned char *fmt, ...)
474 va_list ap;
475 char buf[256];
476 va_start(ap, fmt);
477 vsnprintf(buf, sizeof (buf), fmt, ap);
478 va_end(ap);
479 lcd_puts(x, y, buf);
482 /* Put a string at a given char position, skipping first offset chars */
483 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
485 if ((unsigned)y >= (unsigned)current_vp->height)
486 return;
488 /* make sure scrolling is turned off on the line we are updating */
489 lcd_scroll_stop_line(current_vp, y);
491 x = lcd_putsxyofs(x, y, offset, str);
492 while (x < current_vp->width)
493 lcd_putxchar(x++, y, xspace);
496 /** scrolling **/
497 void lcd_puts_scroll(int x, int y, const unsigned char *string)
499 lcd_puts_scroll_offset(x, y, string, 0);
502 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string,
503 int offset)
505 struct scrollinfo* s;
506 int len;
508 if ((unsigned)y >= (unsigned)current_vp->height)
509 return;
511 /* remove any previously scrolling line at the same location */
512 lcd_scroll_stop_line(current_vp, y);
514 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
516 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
518 s->start_tick = current_tick + lcd_scroll_info.delay;
520 lcd_puts_offset(x, y, string, offset);
521 len = utf8length(string);
523 if (current_vp->width - x < len)
525 /* prepare scroll line */
526 char *end;
527 int count;
529 memset(s->line, 0, sizeof s->line);
530 strlcpy(s->line, string, sizeof s->line);
532 /* get width */
533 s->len = utf8length(s->line);
535 /* scroll bidirectional or forward only depending on the string width */
536 if (lcd_scroll_info.bidir_limit)
538 s->bidir = s->len < (current_vp->width) *
539 (100 + lcd_scroll_info.bidir_limit) / 100;
541 else
542 s->bidir = false;
544 if (!s->bidir) /* add spaces if scrolling in the round */
546 strlcat(s->line, " ", sizeof s->line);
547 /* get new width incl. spaces */
548 s->len += SCROLL_SPACING;
551 end = strchr(s->line, '\0');
552 len = sizeof s->line - (end - s->line);
553 count = utf8seek(s->line, current_vp->width);
554 strlcpy(end, string, MIN(count, len));
556 s->vp = current_vp;
557 s->y = y;
558 s->offset = offset;
559 s->startx = x;
560 s->backward = false;
561 lcd_scroll_info.lines++;
565 void lcd_scroll_fn(void)
567 struct scrollinfo* s;
568 int index;
569 int xpos, ypos;
570 bool update;
571 struct viewport* old_vp = current_vp;
572 bool makedelay;
574 update = false;
575 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
576 s = &lcd_scroll_info.scroll[index];
578 /* check pause */
579 if (TIME_BEFORE(current_tick, s->start_tick))
580 continue;
582 lcd_set_viewport(s->vp);
584 if (s->backward)
585 s->offset--;
586 else
587 s->offset++;
589 xpos = s->startx;
590 ypos = s->y;
592 makedelay = false;
593 if (s->bidir) /* scroll bidirectional */
595 if (s->offset <= 0) {
596 /* at beginning of line */
597 s->offset = 0;
598 s->backward = false;
599 makedelay = true;
601 else if (s->offset >= s->len - (current_vp->width - xpos)) {
602 /* at end of line */
603 s->offset = s->len - (current_vp->width - xpos);
604 s->backward = true;
605 makedelay = true;
608 else /* scroll forward the whole time */
610 if (s->offset >= s->len) {
611 s->offset = 0;
612 makedelay = true;
616 if (makedelay)
617 s->start_tick = current_tick + lcd_scroll_info.delay +
618 lcd_scroll_info.ticks;
620 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
621 update = true;
624 lcd_set_viewport(old_vp);
626 if (lcd_cursor.enabled)
628 if (--lcd_cursor.downcount <= 0)
630 lcd_cursor.downcount = lcd_cursor.divider;
631 lcd_cursor.visible = !lcd_cursor.visible;
632 update = true;
636 if (update)
637 lcd_update();