Don't use the same completion_event for both directions. This could cause problems...
[kugel-rb.git] / firmware / drivers / lcd-charcell.c
blob8af08e64bbbbad3f1bf63bc59782a9d656dd9e5e
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 ****************************************************************************/
22 #include "config.h"
23 #include "hwcompat.h"
24 #include "stdarg.h"
25 #include "sprintf.h"
26 #include "lcd.h"
27 #include "kernel.h"
28 #include "thread.h"
29 #include <string.h>
30 #include <stdlib.h>
31 #include "debug.h"
32 #include "file.h"
33 #include "system.h"
34 #include "lcd-charcell.h"
35 #include "rbunicode.h"
36 #include "scroll_engine.h"
38 /** definitions **/
40 #define VARIABLE_XCHARS 16 /* number of software user-definable characters */
41 /* There must be mappings for this many characters in the 0xe000 unicode range
42 * in lcd-charset-<target>.c */
44 #define NO_PATTERN (-1)
46 static int find_xchar(unsigned long ucs);
48 /** globals **/
50 unsigned char lcd_charbuffer[LCD_HEIGHT][LCD_WIDTH]; /* The "frame"buffer */
51 static unsigned char lcd_substbuffer[LCD_HEIGHT][LCD_WIDTH];
52 struct pattern_info lcd_patterns[MAX_HW_PATTERNS];
53 struct cursor_info lcd_cursor;
55 static unsigned char xfont_variable[VARIABLE_XCHARS][HW_PATTERN_SIZE];
56 static bool xfont_variable_locked[VARIABLE_XCHARS];
57 static int xspace; /* stores xhcar id of ' ' - often needed */
59 static struct viewport default_vp =
61 .x = 0,
62 .y = 0,
63 .width = LCD_WIDTH,
64 .height = LCD_HEIGHT,
67 static struct viewport* current_vp = &default_vp;
69 /* LCD init */
70 void lcd_init (void)
72 lcd_init_device();
73 lcd_charset_init();
74 memset(lcd_patterns, 0, sizeof(lcd_patterns));
75 xspace = find_xchar(' ');
76 memset(lcd_charbuffer, xchar_info[xspace].hw_char, sizeof(lcd_charbuffer));
77 scroll_init();
80 /* Viewports */
82 void lcd_set_viewport(struct viewport* vp)
84 if (vp == NULL)
85 current_vp = &default_vp;
86 else
87 current_vp = vp;
89 #if defined(SIMULATOR)
90 /* Force the viewport to be within bounds. If this happens it should
91 * be considered an error - the viewport will not draw as it might be
92 * expected.
94 if((unsigned) current_vp->x > (unsigned) LCD_WIDTH
95 || (unsigned) current_vp->y > (unsigned) LCD_HEIGHT
96 || current_vp->x + current_vp->width > LCD_WIDTH
97 || current_vp->y + current_vp->height > LCD_HEIGHT)
99 #if !defined(HAVE_VIEWPORT_CLIP)
100 DEBUGF("ERROR: "
101 #else
102 DEBUGF("NOTE: "
103 #endif
104 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
105 current_vp->x, current_vp->y,
106 current_vp->width, current_vp->height);
109 #endif
112 void lcd_update_viewport(void)
114 lcd_update();
117 /** parameter handling **/
119 int lcd_getwidth(void)
121 return current_vp->width;
124 int lcd_getheight(void)
126 return current_vp->height;
129 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
131 int width = utf8length(str);
133 if (w)
134 *w = width;
135 if (h)
136 *h = 1;
138 return width;
141 /** low-level functions **/
143 static int find_xchar(unsigned long ucs)
145 int low = 0;
146 int high = xchar_info_size - 1;
150 int probe = (low + high) >> 1;
152 if (xchar_info[probe].ucs < ucs)
153 low = probe + 1;
154 else if (xchar_info[probe].ucs > ucs)
155 high = probe - 1;
156 else
157 return probe;
159 while (low <= high);
161 /* Not found: return index of no-char symbol (last symbol, hardcoded). */
162 return xchar_info_size - 1;
165 static int glyph_to_pat(unsigned glyph)
167 int i;
169 for (i = 0; i < lcd_pattern_count; i++)
170 if (lcd_patterns[i].glyph == glyph)
171 return i;
173 return NO_PATTERN;
176 static void lcd_free_pat(int pat)
178 int x, y;
180 if (pat != NO_PATTERN)
182 for (x = 0; x < LCD_WIDTH; x++)
183 for (y = 0; y < LCD_HEIGHT; y++)
184 if (pat == lcd_charbuffer[y][x])
185 lcd_charbuffer[y][x] = lcd_substbuffer[y][x];
187 if (lcd_cursor.enabled && pat == lcd_cursor.hw_char)
188 lcd_cursor.hw_char = lcd_cursor.subst_char;
190 lcd_patterns[pat].count = 0;
194 static int lcd_get_free_pat(int xchar)
196 static int last_used_pat = 0;
198 int pat = last_used_pat; /* start from last used pattern */
199 int least_pat = pat; /* pattern with least priority */
200 int least_priority = lcd_patterns[pat].priority;
201 int i;
203 for (i = 0; i < lcd_pattern_count; i++)
205 if (++pat >= lcd_pattern_count) /* Keep 'pat' within limits */
206 pat = 0;
208 if (lcd_patterns[pat].count == 0)
210 last_used_pat = pat;
211 return pat;
213 if (lcd_patterns[pat].priority < least_priority)
215 least_priority = lcd_patterns[pat].priority;
216 least_pat = pat;
219 if (xchar_info[xchar].priority > least_priority) /* prioritized char */
221 lcd_free_pat(least_pat);
222 last_used_pat = least_pat;
223 return least_pat;
225 return NO_PATTERN;
228 static const unsigned char *glyph_to_pattern(unsigned glyph)
230 if (glyph & 0x8000)
231 return xfont_variable[glyph & 0x7fff];
232 else
233 return xfont_fixed[glyph];
236 static int map_xchar(int xchar, unsigned char *substitute)
238 int pat;
239 unsigned glyph;
241 if (xchar_info[xchar].priority > 0) /* soft char */
243 glyph = xchar_info[xchar].glyph;
244 pat = glyph_to_pat(glyph);
246 if (pat == NO_PATTERN) /* not yet mapped */
248 pat = lcd_get_free_pat(xchar); /* try to map */
250 if (pat == NO_PATTERN) /* failed: just use substitute */
251 return xchar_info[xchar].hw_char;
252 else
253 { /* define pattern */
254 lcd_patterns[pat].priority = xchar_info[xchar].priority;
255 lcd_patterns[pat].glyph = glyph;
256 memcpy(lcd_patterns[pat].pattern, glyph_to_pattern(glyph),
257 HW_PATTERN_SIZE);
260 lcd_patterns[pat].count++; /* increase reference count */
261 *substitute = xchar_info[xchar].hw_char;
262 return pat;
264 else /* hardware char */
265 return xchar_info[xchar].hw_char;
268 static void lcd_putxchar(int x, int y, int xchar)
270 int lcd_char;
272 /* Adjust for viewport */
273 x += current_vp->x;
274 y += current_vp->y;
276 #if defined(HAVE_VIEWPORT_CLIP)
277 if((unsigned)x > (unsigned)LCD_WIDTH || (unsigned)y > (unsigned)LCD_HEIGHT)
278 return;
279 #endif
281 lcd_char = lcd_charbuffer[y][x];
283 if (lcd_char < lcd_pattern_count) /* old char was soft */
284 lcd_patterns[lcd_char].count--; /* decrease old reference count */
286 lcd_charbuffer[y][x] = map_xchar(xchar, &lcd_substbuffer[y][x]);
289 /** user-definable pattern handling **/
291 unsigned long lcd_get_locked_pattern(void)
293 int i = 0;
295 for (i = 0; i < VARIABLE_XCHARS; i++)
297 if (!xfont_variable_locked[i])
299 xfont_variable_locked[i] = true;
300 return 0xe000 + i; /* hard-coded */
303 return 0;
306 void lcd_unlock_pattern(unsigned long ucs)
308 int xchar = find_xchar(ucs);
309 unsigned glyph = xchar_info[xchar].glyph;
311 if (glyph & 0x8000) /* variable extended char */
313 lcd_free_pat(glyph_to_pat(glyph));
314 xfont_variable_locked[glyph & 0x7fff] = false;
318 void lcd_define_pattern(unsigned long ucs, const char *pattern)
320 int xchar = find_xchar(ucs);
321 unsigned glyph = xchar_info[xchar].glyph;
322 int pat;
324 if (glyph & 0x8000) /* variable extended char */
326 memcpy(xfont_variable[glyph & 0x7fff], pattern, HW_PATTERN_SIZE);
327 pat = glyph_to_pat(glyph);
328 if (pat != NO_PATTERN)
329 memcpy(lcd_patterns[pat].pattern, pattern, HW_PATTERN_SIZE);
333 /** output functions **/
335 /* Clear the whole display */
336 void lcd_clear_display(void)
338 int x, y;
339 struct viewport* old_vp = current_vp;
341 lcd_scroll_info.lines = 0;
342 lcd_remove_cursor();
344 /* Set the default viewport - required for lcd_putxchar */
345 current_vp = &default_vp;
347 for (x = 0; x < LCD_WIDTH; x++)
348 for (y = 0; y < LCD_HEIGHT; y++)
349 lcd_putxchar(x, y, xspace);
351 current_vp = old_vp;
354 /* Clear the current viewport */
355 void lcd_clear_viewport(void)
357 int x, y;
359 if (current_vp == &default_vp)
361 lcd_clear_display();
363 else
365 /* Remove the cursor if it is within the current viewport */
366 if (lcd_cursor.enabled &&
367 (lcd_cursor.x >= current_vp->x) &&
368 (lcd_cursor.x <= current_vp->x + current_vp->width) &&
369 (lcd_cursor.y >= current_vp->y) &&
370 (lcd_cursor.y <= current_vp->y + current_vp->height))
372 lcd_remove_cursor();
375 for (x = 0; x < current_vp->width; x++)
376 for (y = 0; y < current_vp->height; y++)
377 lcd_putxchar(x, y, xspace);
379 lcd_scroll_stop(current_vp);
383 /* Put an unicode character at the given position */
384 void lcd_putc(int x, int y, unsigned long ucs)
386 if ((unsigned)x >= (unsigned)current_vp->width ||
387 (unsigned)y >= (unsigned)current_vp->height)
388 return;
390 lcd_putxchar(x, y, find_xchar(ucs));
393 /* Show cursor (alternating with existing character) at the given position */
394 void lcd_put_cursor(int x, int y, unsigned long cursor_ucs)
396 if ((unsigned)x >= (unsigned)current_vp->width ||
397 (unsigned)y >= (unsigned)current_vp->height ||
398 lcd_cursor.enabled)
399 return;
401 lcd_cursor.enabled = true;
402 lcd_cursor.visible = false;
403 lcd_cursor.hw_char = map_xchar(find_xchar(cursor_ucs), &lcd_cursor.subst_char);
404 lcd_cursor.x = current_vp->x + x;
405 lcd_cursor.y = current_vp->y + y;
406 lcd_cursor.downcount = 0;
407 lcd_cursor.divider = MAX((HZ/2) / lcd_scroll_info.ticks, 1);
410 /* Remove the cursor */
411 void lcd_remove_cursor(void)
413 if (lcd_cursor.enabled)
415 if (lcd_cursor.hw_char < lcd_pattern_count) /* soft char, unmap */
416 lcd_patterns[lcd_cursor.hw_char].count--;
418 lcd_cursor.enabled = lcd_cursor.visible = false;
422 /* Put a string at a given position, skipping first ofs chars */
423 static int lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
425 unsigned short ucs;
426 const unsigned char *utf8 = str;
428 while (*utf8 && x < current_vp->width)
430 utf8 = utf8decode(utf8, &ucs);
432 if (ofs > 0)
434 ofs--;
435 continue;
437 lcd_putxchar(x++, y, find_xchar(ucs));
439 return x;
442 /* Put a string at a given position */
443 void lcd_putsxy(int x, int y, const unsigned char *str)
445 if ((unsigned)y >= (unsigned)current_vp->height)
446 return;
448 lcd_putsxyofs(x, y, 0, str);
451 /*** Line oriented text output ***/
453 /* Put a string at a given char position */
454 void lcd_puts(int x, int y, const unsigned char *str)
456 lcd_puts_offset(x, y, str, 0);
459 /* Formatting version of lcd_puts */
460 void lcd_putsf(int x, int y, const unsigned char *fmt, ...)
462 va_list ap;
463 char buf[256];
464 va_start(ap, fmt);
465 vsnprintf(buf, sizeof (buf), fmt, ap);
466 va_end(ap);
467 lcd_puts(x, y, buf);
470 /* Put a string at a given char position, skipping first offset chars */
471 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
473 if ((unsigned)y >= (unsigned)current_vp->height)
474 return;
476 /* make sure scrolling is turned off on the line we are updating */
477 lcd_scroll_stop_line(current_vp, y);
479 x = lcd_putsxyofs(x, y, offset, str);
480 while (x < current_vp->width)
481 lcd_putxchar(x++, y, xspace);
484 /** scrolling **/
485 void lcd_puts_scroll(int x, int y, const unsigned char *string)
487 lcd_puts_scroll_offset(x, y, string, 0);
490 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string,
491 int offset)
493 struct scrollinfo* s;
494 int len;
496 if ((unsigned)y >= (unsigned)current_vp->height)
497 return;
499 /* remove any previously scrolling line at the same location */
500 lcd_scroll_stop_line(current_vp, y);
502 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
504 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
506 s->start_tick = current_tick + lcd_scroll_info.delay;
508 lcd_puts_offset(x, y, string, offset);
509 len = utf8length(string);
511 if (current_vp->width - x < len)
513 /* prepare scroll line */
514 char *end;
516 memset(s->line, 0, sizeof s->line);
517 strcpy(s->line, string);
519 /* get width */
520 s->len = utf8length(s->line);
522 /* scroll bidirectional or forward only depending on the string width */
523 if (lcd_scroll_info.bidir_limit)
525 s->bidir = s->len < (current_vp->width) *
526 (100 + lcd_scroll_info.bidir_limit) / 100;
528 else
529 s->bidir = false;
531 if (!s->bidir) /* add spaces if scrolling in the round */
533 strcat(s->line, " ");
534 /* get new width incl. spaces */
535 s->len += SCROLL_SPACING;
538 end = strchr(s->line, '\0');
539 strlcpy(end, string, utf8seek(s->line, current_vp->width));
541 s->vp = current_vp;
542 s->y = y;
543 s->offset = offset;
544 s->startx = x;
545 s->backward = false;
546 lcd_scroll_info.lines++;
550 void lcd_scroll_fn(void)
552 struct scrollinfo* s;
553 int index;
554 int xpos, ypos;
555 bool update;
556 struct viewport* old_vp = current_vp;
558 update = false;
559 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
560 s = &lcd_scroll_info.scroll[index];
562 /* check pause */
563 if (TIME_BEFORE(current_tick, s->start_tick))
564 continue;
566 lcd_set_viewport(s->vp);
568 if (s->backward)
569 s->offset--;
570 else
571 s->offset++;
573 xpos = s->startx;
574 ypos = s->y;
576 if (s->bidir) /* scroll bidirectional */
578 if (s->offset <= 0) {
579 /* at beginning of line */
580 s->offset = 0;
581 s->backward = false;
582 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
584 if (s->offset >= s->len - (current_vp->width - xpos)) {
585 /* at end of line */
586 s->offset = s->len - (current_vp->width - xpos);
587 s->backward = true;
588 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
591 else /* scroll forward the whole time */
593 if (s->offset >= s->len)
594 s->offset -= s->len;
597 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
598 update = true;
601 lcd_set_viewport(old_vp);
603 if (lcd_cursor.enabled)
605 if (--lcd_cursor.downcount <= 0)
607 lcd_cursor.downcount = lcd_cursor.divider;
608 lcd_cursor.visible = !lcd_cursor.visible;
609 update = true;
613 if (update)
614 lcd_update();