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 ****************************************************************************/
33 #include "lcd-charcell.h"
34 #include "rbunicode.h"
35 #include "scroll_engine.h"
39 #define VARIABLE_XCHARS 16 /* number of software user-definable characters */
40 /* There must be mappings for this many characters in the 0xe000 unicode range
41 * in lcd-charset-<target>.c */
43 #define NO_PATTERN (-1)
45 static int find_xchar(unsigned long ucs
);
49 unsigned char lcd_charbuffer
[LCD_HEIGHT
][LCD_WIDTH
]; /* The "frame"buffer */
50 static unsigned char lcd_substbuffer
[LCD_HEIGHT
][LCD_WIDTH
];
51 struct pattern_info lcd_patterns
[MAX_HW_PATTERNS
];
52 struct cursor_info lcd_cursor
;
54 static unsigned char xfont_variable
[VARIABLE_XCHARS
][HW_PATTERN_SIZE
];
55 static bool xfont_variable_locked
[VARIABLE_XCHARS
];
56 static int xspace
; /* stores xhcar id of ' ' - often needed */
58 static struct viewport default_vp
=
66 static struct viewport
* current_vp
= &default_vp
;
73 memset(lcd_patterns
, 0, sizeof(lcd_patterns
));
74 xspace
= find_xchar(' ');
75 memset(lcd_charbuffer
, xchar_info
[xspace
].hw_char
, sizeof(lcd_charbuffer
));
81 void lcd_set_viewport(struct viewport
* vp
)
84 current_vp
= &default_vp
;
88 #if defined(SIMULATOR)
89 /* Force the viewport to be within bounds. If this happens it should
90 * be considered an error - the viewport will not draw as it might be
93 if((unsigned) current_vp
->x
> (unsigned) LCD_WIDTH
94 || (unsigned) current_vp
->y
> (unsigned) LCD_HEIGHT
95 || current_vp
->x
+ current_vp
->width
> LCD_WIDTH
96 || current_vp
->y
+ current_vp
->height
> LCD_HEIGHT
)
98 #if !defined(HAVE_VIEWPORT_CLIP)
103 "set_viewport out of bounds: x: %d y: %d width: %d height:%d\n",
104 current_vp
->x
, current_vp
->y
,
105 current_vp
->width
, current_vp
->height
);
111 void lcd_update_viewport(void)
116 /** parameter handling **/
118 int lcd_getwidth(void)
120 return current_vp
->width
;
123 int lcd_getheight(void)
125 return current_vp
->height
;
128 int lcd_getstringsize(const unsigned char *str
, int *w
, int *h
)
130 int width
= utf8length(str
);
140 /** low-level functions **/
142 static int find_xchar(unsigned long ucs
)
145 int high
= xchar_info_size
- 1;
149 int probe
= (low
+ high
) >> 1;
151 if (xchar_info
[probe
].ucs
< ucs
)
153 else if (xchar_info
[probe
].ucs
> ucs
)
160 /* Not found: return index of no-char symbol (last symbol, hardcoded). */
161 return xchar_info_size
- 1;
164 static int glyph_to_pat(unsigned glyph
)
168 for (i
= 0; i
< lcd_pattern_count
; i
++)
169 if (lcd_patterns
[i
].glyph
== glyph
)
175 static void lcd_free_pat(int pat
)
179 if (pat
!= NO_PATTERN
)
181 for (x
= 0; x
< LCD_WIDTH
; x
++)
182 for (y
= 0; y
< LCD_HEIGHT
; y
++)
183 if (pat
== lcd_charbuffer
[y
][x
])
184 lcd_charbuffer
[y
][x
] = lcd_substbuffer
[y
][x
];
186 if (lcd_cursor
.enabled
&& pat
== lcd_cursor
.hw_char
)
187 lcd_cursor
.hw_char
= lcd_cursor
.subst_char
;
189 lcd_patterns
[pat
].count
= 0;
193 static int lcd_get_free_pat(int xchar
)
195 static int last_used_pat
= 0;
197 int pat
= last_used_pat
; /* start from last used pattern */
198 int least_pat
= pat
; /* pattern with least priority */
199 int least_priority
= lcd_patterns
[pat
].priority
;
202 for (i
= 0; i
< lcd_pattern_count
; i
++)
204 if (++pat
>= lcd_pattern_count
) /* Keep 'pat' within limits */
207 if (lcd_patterns
[pat
].count
== 0)
212 if (lcd_patterns
[pat
].priority
< least_priority
)
214 least_priority
= lcd_patterns
[pat
].priority
;
218 if (xchar_info
[xchar
].priority
> least_priority
) /* prioritized char */
220 lcd_free_pat(least_pat
);
221 last_used_pat
= least_pat
;
227 static const unsigned char *glyph_to_pattern(unsigned glyph
)
230 return xfont_variable
[glyph
& 0x7fff];
232 return xfont_fixed
[glyph
];
235 static int map_xchar(int xchar
, unsigned char *substitute
)
240 if (xchar_info
[xchar
].priority
> 0) /* soft char */
242 glyph
= xchar_info
[xchar
].glyph
;
243 pat
= glyph_to_pat(glyph
);
245 if (pat
== NO_PATTERN
) /* not yet mapped */
247 pat
= lcd_get_free_pat(xchar
); /* try to map */
249 if (pat
== NO_PATTERN
) /* failed: just use substitute */
250 return xchar_info
[xchar
].hw_char
;
252 { /* define pattern */
253 lcd_patterns
[pat
].priority
= xchar_info
[xchar
].priority
;
254 lcd_patterns
[pat
].glyph
= glyph
;
255 memcpy(lcd_patterns
[pat
].pattern
, glyph_to_pattern(glyph
),
259 lcd_patterns
[pat
].count
++; /* increase reference count */
260 *substitute
= xchar_info
[xchar
].hw_char
;
263 else /* hardware char */
264 return xchar_info
[xchar
].hw_char
;
267 static void lcd_putxchar(int x
, int y
, int xchar
)
271 /* Adjust for viewport */
275 #if defined(HAVE_VIEWPORT_CLIP)
276 if((unsigned)x
> (unsigned)LCD_WIDTH
|| (unsigned)y
> (unsigned)LCD_HEIGHT
)
280 lcd_char
= lcd_charbuffer
[y
][x
];
282 if (lcd_char
< lcd_pattern_count
) /* old char was soft */
283 lcd_patterns
[lcd_char
].count
--; /* decrease old reference count */
285 lcd_charbuffer
[y
][x
] = map_xchar(xchar
, &lcd_substbuffer
[y
][x
]);
288 /** user-definable pattern handling **/
290 unsigned long lcd_get_locked_pattern(void)
294 for (i
= 0; i
< VARIABLE_XCHARS
; i
++)
296 if (!xfont_variable_locked
[i
])
298 xfont_variable_locked
[i
] = true;
299 return 0xe000 + i
; /* hard-coded */
305 void lcd_unlock_pattern(unsigned long ucs
)
307 int xchar
= find_xchar(ucs
);
308 unsigned glyph
= xchar_info
[xchar
].glyph
;
310 if (glyph
& 0x8000) /* variable extended char */
312 lcd_free_pat(glyph_to_pat(glyph
));
313 xfont_variable_locked
[glyph
& 0x7fff] = false;
317 void lcd_define_pattern(unsigned long ucs
, const char *pattern
)
319 int xchar
= find_xchar(ucs
);
320 unsigned glyph
= xchar_info
[xchar
].glyph
;
323 if (glyph
& 0x8000) /* variable extended char */
325 memcpy(xfont_variable
[glyph
& 0x7fff], pattern
, HW_PATTERN_SIZE
);
326 pat
= glyph_to_pat(glyph
);
327 if (pat
!= NO_PATTERN
)
328 memcpy(lcd_patterns
[pat
].pattern
, pattern
, HW_PATTERN_SIZE
);
332 /** output functions **/
334 /* Clear the whole display */
335 void lcd_clear_display(void)
338 struct viewport
* old_vp
= current_vp
;
340 lcd_scroll_info
.lines
= 0;
343 /* Set the default viewport - required for lcd_putxchar */
344 current_vp
= &default_vp
;
346 for (x
= 0; x
< LCD_WIDTH
; x
++)
347 for (y
= 0; y
< LCD_HEIGHT
; y
++)
348 lcd_putxchar(x
, y
, xspace
);
353 /* Clear the current viewport */
354 void lcd_clear_viewport(void)
358 if (current_vp
== &default_vp
)
364 /* Remove the cursor if it is within the current viewport */
365 if (lcd_cursor
.enabled
&&
366 (lcd_cursor
.x
>= current_vp
->x
) &&
367 (lcd_cursor
.x
<= current_vp
->x
+ current_vp
->width
) &&
368 (lcd_cursor
.y
>= current_vp
->y
) &&
369 (lcd_cursor
.y
<= current_vp
->y
+ current_vp
->height
))
374 for (x
= 0; x
< current_vp
->width
; x
++)
375 for (y
= 0; y
< current_vp
->height
; y
++)
376 lcd_putxchar(x
, y
, xspace
);
378 lcd_scroll_stop(current_vp
);
382 /* Put an unicode character at the given position */
383 void lcd_putc(int x
, int y
, unsigned long ucs
)
385 if ((unsigned)x
>= (unsigned)current_vp
->width
||
386 (unsigned)y
>= (unsigned)current_vp
->height
)
389 lcd_putxchar(x
, y
, find_xchar(ucs
));
392 /* Show cursor (alternating with existing character) at the given position */
393 void lcd_put_cursor(int x
, int y
, unsigned long cursor_ucs
)
395 if ((unsigned)x
>= (unsigned)current_vp
->width
||
396 (unsigned)y
>= (unsigned)current_vp
->height
||
400 lcd_cursor
.enabled
= true;
401 lcd_cursor
.visible
= false;
402 lcd_cursor
.hw_char
= map_xchar(find_xchar(cursor_ucs
), &lcd_cursor
.subst_char
);
403 lcd_cursor
.x
= current_vp
->x
+ x
;
404 lcd_cursor
.y
= current_vp
->y
+ y
;
405 lcd_cursor
.downcount
= 0;
406 lcd_cursor
.divider
= MAX((HZ
/2) / lcd_scroll_info
.ticks
, 1);
409 /* Remove the cursor */
410 void lcd_remove_cursor(void)
412 if (lcd_cursor
.enabled
)
414 if (lcd_cursor
.hw_char
< lcd_pattern_count
) /* soft char, unmap */
415 lcd_patterns
[lcd_cursor
.hw_char
].count
--;
417 lcd_cursor
.enabled
= lcd_cursor
.visible
= false;
421 /* Put a string at a given position, skipping first ofs chars */
422 static int lcd_putsxyofs(int x
, int y
, int ofs
, const unsigned char *str
)
425 const unsigned char *utf8
= str
;
427 while (*utf8
&& x
< current_vp
->width
)
429 utf8
= utf8decode(utf8
, &ucs
);
436 lcd_putxchar(x
++, y
, find_xchar(ucs
));
441 /* Put a string at a given position */
442 void lcd_putsxy(int x
, int y
, const unsigned char *str
)
444 if ((unsigned)y
>= (unsigned)current_vp
->height
)
447 lcd_putsxyofs(x
, y
, 0, str
);
450 /*** Line oriented text output ***/
452 /* Put a string at a given char position */
453 void lcd_puts(int x
, int y
, const unsigned char *str
)
455 lcd_puts_offset(x
, y
, str
, 0);
458 /* Formatting version of lcd_puts */
459 void lcd_putsf(int x
, int y
, const unsigned char *fmt
, ...)
464 vsnprintf(buf
, sizeof (buf
), fmt
, ap
);
469 /* Put a string at a given char position, skipping first offset chars */
470 void lcd_puts_offset(int x
, int y
, const unsigned char *str
, int offset
)
472 if ((unsigned)y
>= (unsigned)current_vp
->height
)
475 /* make sure scrolling is turned off on the line we are updating */
476 lcd_scroll_stop_line(current_vp
, y
);
478 x
= lcd_putsxyofs(x
, y
, offset
, str
);
479 while (x
< current_vp
->width
)
480 lcd_putxchar(x
++, y
, xspace
);
484 void lcd_puts_scroll(int x
, int y
, const unsigned char *string
)
486 lcd_puts_scroll_offset(x
, y
, string
, 0);
489 void lcd_puts_scroll_offset(int x
, int y
, const unsigned char *string
,
492 struct scrollinfo
* s
;
495 if ((unsigned)y
>= (unsigned)current_vp
->height
)
498 /* remove any previously scrolling line at the same location */
499 lcd_scroll_stop_line(current_vp
, y
);
501 if (lcd_scroll_info
.lines
>= LCD_SCROLLABLE_LINES
) return;
503 s
= &lcd_scroll_info
.scroll
[lcd_scroll_info
.lines
];
505 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
;
507 lcd_puts_offset(x
, y
, string
, offset
);
508 len
= utf8length(string
);
510 if (current_vp
->width
- x
< len
)
512 /* prepare scroll line */
515 memset(s
->line
, 0, sizeof s
->line
);
516 strcpy(s
->line
, string
);
519 s
->len
= utf8length(s
->line
);
521 /* scroll bidirectional or forward only depending on the string width */
522 if (lcd_scroll_info
.bidir_limit
)
524 s
->bidir
= s
->len
< (current_vp
->width
) *
525 (100 + lcd_scroll_info
.bidir_limit
) / 100;
530 if (!s
->bidir
) /* add spaces if scrolling in the round */
532 strcat(s
->line
, " ");
533 /* get new width incl. spaces */
534 s
->len
+= SCROLL_SPACING
;
537 end
= strchr(s
->line
, '\0');
538 strlcpy(end
, string
, utf8seek(s
->line
, current_vp
->width
));
545 lcd_scroll_info
.lines
++;
549 void lcd_scroll_fn(void)
551 struct scrollinfo
* s
;
555 struct viewport
* old_vp
= current_vp
;
558 for ( index
= 0; index
< lcd_scroll_info
.lines
; index
++ ) {
559 s
= &lcd_scroll_info
.scroll
[index
];
562 if (TIME_BEFORE(current_tick
, s
->start_tick
))
565 lcd_set_viewport(s
->vp
);
575 if (s
->bidir
) /* scroll bidirectional */
577 if (s
->offset
<= 0) {
578 /* at beginning of line */
581 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
* 2;
583 if (s
->offset
>= s
->len
- (current_vp
->width
- xpos
)) {
585 s
->offset
= s
->len
- (current_vp
->width
- xpos
);
587 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
* 2;
590 else /* scroll forward the whole time */
592 if (s
->offset
>= s
->len
)
596 lcd_putsxyofs(xpos
, ypos
, s
->offset
, s
->line
);
600 lcd_set_viewport(old_vp
);
602 if (lcd_cursor
.enabled
)
604 if (--lcd_cursor
.downcount
<= 0)
606 lcd_cursor
.downcount
= lcd_cursor
.divider
;
607 lcd_cursor
.visible
= !lcd_cursor
.visible
;