1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
34 #include "lcd-charcell.h"
35 #include "rbunicode.h"
36 #include "scroll_engine.h"
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
);
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
=
67 static struct viewport
* current_vp
= &default_vp
;
74 memset(lcd_patterns
, 0, sizeof(lcd_patterns
));
75 xspace
= find_xchar(' ');
76 memset(lcd_charbuffer
, xchar_info
[xspace
].hw_char
, sizeof(lcd_charbuffer
));
82 void lcd_set_viewport(struct viewport
* vp
)
85 current_vp
= &default_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
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)
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
);
112 void lcd_update_viewport(void)
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
);
141 /** low-level functions **/
143 static int find_xchar(unsigned long ucs
)
146 int high
= xchar_info_size
- 1;
150 int probe
= (low
+ high
) >> 1;
152 if (xchar_info
[probe
].ucs
< ucs
)
154 else if (xchar_info
[probe
].ucs
> ucs
)
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
)
169 for (i
= 0; i
< lcd_pattern_count
; i
++)
170 if (lcd_patterns
[i
].glyph
== glyph
)
176 static void lcd_free_pat(int pat
)
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
;
203 for (i
= 0; i
< lcd_pattern_count
; i
++)
205 if (++pat
>= lcd_pattern_count
) /* Keep 'pat' within limits */
208 if (lcd_patterns
[pat
].count
== 0)
213 if (lcd_patterns
[pat
].priority
< least_priority
)
215 least_priority
= lcd_patterns
[pat
].priority
;
219 if (xchar_info
[xchar
].priority
> least_priority
) /* prioritized char */
221 lcd_free_pat(least_pat
);
222 last_used_pat
= least_pat
;
228 static const unsigned char *glyph_to_pattern(unsigned glyph
)
231 return xfont_variable
[glyph
& 0x7fff];
233 return xfont_fixed
[glyph
];
236 static int map_xchar(int xchar
, unsigned char *substitute
)
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
;
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
),
260 lcd_patterns
[pat
].count
++; /* increase reference count */
261 *substitute
= xchar_info
[xchar
].hw_char
;
264 else /* hardware char */
265 return xchar_info
[xchar
].hw_char
;
268 static void lcd_putxchar(int x
, int y
, int xchar
)
272 /* Adjust for viewport */
276 #if defined(HAVE_VIEWPORT_CLIP)
277 if((unsigned)x
> (unsigned)LCD_WIDTH
|| (unsigned)y
> (unsigned)LCD_HEIGHT
)
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)
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 */
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
;
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)
339 struct viewport
* old_vp
= current_vp
;
341 lcd_scroll_info
.lines
= 0;
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
);
354 /* Clear the current viewport */
355 void lcd_clear_viewport(void)
359 if (current_vp
== &default_vp
)
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
))
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
)
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
||
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
)
426 const unsigned char *utf8
= str
;
428 while (*utf8
&& x
< current_vp
->width
)
430 utf8
= utf8decode(utf8
, &ucs
);
437 lcd_putxchar(x
++, y
, find_xchar(ucs
));
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
)
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
, ...)
465 vsnprintf(buf
, sizeof (buf
), fmt
, ap
);
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
)
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
);
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
,
493 struct scrollinfo
* s
;
496 if ((unsigned)y
>= (unsigned)current_vp
->height
)
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 */
516 memset(s
->line
, 0, sizeof s
->line
);
517 strcpy(s
->line
, string
);
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;
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
));
546 lcd_scroll_info
.lines
++;
550 void lcd_scroll_fn(void)
552 struct scrollinfo
* s
;
556 struct viewport
* old_vp
= current_vp
;
559 for ( index
= 0; index
< lcd_scroll_info
.lines
; index
++ ) {
560 s
= &lcd_scroll_info
.scroll
[index
];
563 if (TIME_BEFORE(current_tick
, s
->start_tick
))
566 lcd_set_viewport(s
->vp
);
576 if (s
->bidir
) /* scroll bidirectional */
578 if (s
->offset
<= 0) {
579 /* at beginning of line */
582 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
* 2;
584 if (s
->offset
>= s
->len
- (current_vp
->width
- xpos
)) {
586 s
->offset
= s
->len
- (current_vp
->width
- xpos
);
588 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
* 2;
591 else /* scroll forward the whole time */
593 if (s
->offset
>= s
->len
)
597 lcd_putsxyofs(xpos
, ypos
, s
->offset
, s
->line
);
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
;