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
;
89 void lcd_update_viewport(void)
94 /** parameter handling **/
96 int lcd_getwidth(void)
98 return current_vp
->width
;
101 int lcd_getheight(void)
103 return current_vp
->height
;
106 int lcd_getstringsize(const unsigned char *str
, int *w
, int *h
)
108 int width
= utf8length(str
);
118 /** low-level functions **/
120 static int find_xchar(unsigned long ucs
)
123 int high
= xchar_info_size
- 1;
127 int probe
= (low
+ high
) >> 1;
129 if (xchar_info
[probe
].ucs
< ucs
)
131 else if (xchar_info
[probe
].ucs
> ucs
)
138 /* Not found: return index of no-char symbol (last symbol, hardcoded). */
139 return xchar_info_size
- 1;
142 static int glyph_to_pat(unsigned glyph
)
146 for (i
= 0; i
< lcd_pattern_count
; i
++)
147 if (lcd_patterns
[i
].glyph
== glyph
)
153 static void lcd_free_pat(int pat
)
157 if (pat
!= NO_PATTERN
)
159 for (x
= 0; x
< LCD_WIDTH
; x
++)
160 for (y
= 0; y
< LCD_HEIGHT
; y
++)
161 if (pat
== lcd_charbuffer
[y
][x
])
162 lcd_charbuffer
[y
][x
] = lcd_substbuffer
[y
][x
];
164 if (lcd_cursor
.enabled
&& pat
== lcd_cursor
.hw_char
)
165 lcd_cursor
.hw_char
= lcd_cursor
.subst_char
;
167 lcd_patterns
[pat
].count
= 0;
171 static int lcd_get_free_pat(int xchar
)
173 static int last_used_pat
= 0;
175 int pat
= last_used_pat
; /* start from last used pattern */
176 int least_pat
= pat
; /* pattern with least priority */
177 int least_priority
= lcd_patterns
[pat
].priority
;
180 for (i
= 0; i
< lcd_pattern_count
; i
++)
182 if (++pat
>= lcd_pattern_count
) /* Keep 'pat' within limits */
185 if (lcd_patterns
[pat
].count
== 0)
190 if (lcd_patterns
[pat
].priority
< least_priority
)
192 least_priority
= lcd_patterns
[pat
].priority
;
196 if (xchar_info
[xchar
].priority
> least_priority
) /* prioritized char */
198 lcd_free_pat(least_pat
);
199 last_used_pat
= least_pat
;
205 static const unsigned char *glyph_to_pattern(unsigned glyph
)
208 return xfont_variable
[glyph
& 0x7fff];
210 return xfont_fixed
[glyph
];
213 static int map_xchar(int xchar
, unsigned char *substitute
)
218 if (xchar_info
[xchar
].priority
> 0) /* soft char */
220 glyph
= xchar_info
[xchar
].glyph
;
221 pat
= glyph_to_pat(glyph
);
223 if (pat
== NO_PATTERN
) /* not yet mapped */
225 pat
= lcd_get_free_pat(xchar
); /* try to map */
227 if (pat
== NO_PATTERN
) /* failed: just use substitute */
228 return xchar_info
[xchar
].hw_char
;
230 { /* define pattern */
231 lcd_patterns
[pat
].priority
= xchar_info
[xchar
].priority
;
232 lcd_patterns
[pat
].glyph
= glyph
;
233 memcpy(lcd_patterns
[pat
].pattern
, glyph_to_pattern(glyph
),
237 lcd_patterns
[pat
].count
++; /* increase reference count */
238 *substitute
= xchar_info
[xchar
].hw_char
;
241 else /* hardware char */
242 return xchar_info
[xchar
].hw_char
;
245 static void lcd_putxchar(int x
, int y
, int xchar
)
249 /* Adjust for viewport */
253 lcd_char
= lcd_charbuffer
[y
][x
];
255 if (lcd_char
< lcd_pattern_count
) /* old char was soft */
256 lcd_patterns
[lcd_char
].count
--; /* decrease old reference count */
258 lcd_charbuffer
[y
][x
] = map_xchar(xchar
, &lcd_substbuffer
[y
][x
]);
261 /** user-definable pattern handling **/
263 unsigned long lcd_get_locked_pattern(void)
267 for (i
= 0; i
< VARIABLE_XCHARS
; i
++)
269 if (!xfont_variable_locked
[i
])
271 xfont_variable_locked
[i
] = true;
272 return 0xe000 + i
; /* hard-coded */
278 void lcd_unlock_pattern(unsigned long ucs
)
280 int xchar
= find_xchar(ucs
);
281 unsigned glyph
= xchar_info
[xchar
].glyph
;
283 if (glyph
& 0x8000) /* variable extended char */
285 lcd_free_pat(glyph_to_pat(glyph
));
286 xfont_variable_locked
[glyph
& 0x7fff] = false;
290 void lcd_define_pattern(unsigned long ucs
, const char *pattern
)
292 int xchar
= find_xchar(ucs
);
293 unsigned glyph
= xchar_info
[xchar
].glyph
;
296 if (glyph
& 0x8000) /* variable extended char */
298 memcpy(xfont_variable
[glyph
& 0x7fff], pattern
, HW_PATTERN_SIZE
);
299 pat
= glyph_to_pat(glyph
);
300 if (pat
!= NO_PATTERN
)
301 memcpy(lcd_patterns
[pat
].pattern
, pattern
, HW_PATTERN_SIZE
);
305 /** output functions **/
307 /* Clear the whole display */
308 void lcd_clear_display(void)
311 struct viewport
* old_vp
= current_vp
;
313 lcd_scroll_info
.lines
= 0;
316 /* Set the default viewport - required for lcd_putxchar */
317 current_vp
= &default_vp
;
319 for (x
= 0; x
< LCD_WIDTH
; x
++)
320 for (y
= 0; y
< LCD_HEIGHT
; y
++)
321 lcd_putxchar(x
, y
, xspace
);
326 /* Clear the current viewport */
327 void lcd_clear_viewport(void)
331 if (current_vp
== &default_vp
)
337 /* Remove the cursor if it is within the current viewport */
338 if (lcd_cursor
.enabled
&&
339 (lcd_cursor
.x
>= current_vp
->x
) &&
340 (lcd_cursor
.x
<= current_vp
->x
+ current_vp
->width
) &&
341 (lcd_cursor
.y
>= current_vp
->y
) &&
342 (lcd_cursor
.y
<= current_vp
->y
+ current_vp
->height
))
347 for (x
= 0; x
< current_vp
->width
; x
++)
348 for (y
= 0; y
< current_vp
->height
; y
++)
349 lcd_putxchar(x
, y
, xspace
);
351 lcd_scroll_stop(current_vp
);
355 /* Put an unicode character at the given position */
356 void lcd_putc(int x
, int y
, unsigned long ucs
)
358 if ((unsigned)x
>= (unsigned)current_vp
->width
||
359 (unsigned)y
>= (unsigned)current_vp
->height
)
362 lcd_putxchar(x
, y
, find_xchar(ucs
));
365 /* Show cursor (alternating with existing character) at the given position */
366 void lcd_put_cursor(int x
, int y
, unsigned long cursor_ucs
)
368 if ((unsigned)x
>= (unsigned)current_vp
->width
||
369 (unsigned)y
>= (unsigned)current_vp
->height
||
373 lcd_cursor
.enabled
= true;
374 lcd_cursor
.visible
= false;
375 lcd_cursor
.hw_char
= map_xchar(find_xchar(cursor_ucs
), &lcd_cursor
.subst_char
);
376 lcd_cursor
.x
= current_vp
->x
+ x
;
377 lcd_cursor
.y
= current_vp
->y
+ y
;
378 lcd_cursor
.downcount
= 0;
379 lcd_cursor
.divider
= MAX((HZ
/2) / lcd_scroll_info
.ticks
, 1);
382 /* Remove the cursor */
383 void lcd_remove_cursor(void)
385 if (lcd_cursor
.enabled
)
387 if (lcd_cursor
.hw_char
< lcd_pattern_count
) /* soft char, unmap */
388 lcd_patterns
[lcd_cursor
.hw_char
].count
--;
390 lcd_cursor
.enabled
= lcd_cursor
.visible
= false;
394 /* Put a string at a given position, skipping first ofs chars */
395 static int lcd_putsxyofs(int x
, int y
, int ofs
, const unsigned char *str
)
398 const unsigned char *utf8
= str
;
400 while (*utf8
&& x
< current_vp
->width
)
402 utf8
= utf8decode(utf8
, &ucs
);
409 lcd_putxchar(x
++, y
, find_xchar(ucs
));
414 /* Put a string at a given position */
415 void lcd_putsxy(int x
, int y
, const unsigned char *str
)
417 if ((unsigned)y
>= (unsigned)current_vp
->height
)
420 lcd_putsxyofs(x
, y
, 0, str
);
423 /*** Line oriented text output ***/
425 /* Put a string at a given char position */
426 void lcd_puts(int x
, int y
, const unsigned char *str
)
428 lcd_puts_offset(x
, y
, str
, 0);
431 /* Formatting version of lcd_puts */
432 void lcd_putsf(int x
, int y
, const unsigned char *fmt
, ...)
437 vsnprintf(buf
, sizeof (buf
), fmt
, ap
);
442 /* Put a string at a given char position, skipping first offset chars */
443 void lcd_puts_offset(int x
, int y
, const unsigned char *str
, int offset
)
445 if ((unsigned)y
>= (unsigned)current_vp
->height
)
448 /* make sure scrolling is turned off on the line we are updating */
449 lcd_scroll_stop_line(current_vp
, y
);
451 x
= lcd_putsxyofs(x
, y
, offset
, str
);
452 while (x
< current_vp
->width
)
453 lcd_putxchar(x
++, y
, xspace
);
457 void lcd_puts_scroll(int x
, int y
, const unsigned char *string
)
459 lcd_puts_scroll_offset(x
, y
, string
, 0);
462 void lcd_puts_scroll_offset(int x
, int y
, const unsigned char *string
,
465 struct scrollinfo
* s
;
468 if ((unsigned)y
>= (unsigned)current_vp
->height
)
471 /* remove any previously scrolling line at the same location */
472 lcd_scroll_stop_line(current_vp
, y
);
474 if (lcd_scroll_info
.lines
>= LCD_SCROLLABLE_LINES
) return;
476 s
= &lcd_scroll_info
.scroll
[lcd_scroll_info
.lines
];
478 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
;
480 lcd_puts_offset(x
, y
, string
, offset
);
481 len
= utf8length(string
);
483 if (current_vp
->width
- x
< len
)
485 /* prepare scroll line */
488 memset(s
->line
, 0, sizeof s
->line
);
489 strcpy(s
->line
, string
);
492 s
->len
= utf8length(s
->line
);
494 /* scroll bidirectional or forward only depending on the string width */
495 if (lcd_scroll_info
.bidir_limit
)
497 s
->bidir
= s
->len
< (current_vp
->width
) *
498 (100 + lcd_scroll_info
.bidir_limit
) / 100;
503 if (!s
->bidir
) /* add spaces if scrolling in the round */
505 strcat(s
->line
, " ");
506 /* get new width incl. spaces */
507 s
->len
+= SCROLL_SPACING
;
510 end
= strchr(s
->line
, '\0');
511 strlcpy(end
, string
, utf8seek(s
->line
, current_vp
->width
));
518 lcd_scroll_info
.lines
++;
522 void lcd_scroll_fn(void)
524 struct scrollinfo
* s
;
528 struct viewport
* old_vp
= current_vp
;
531 for ( index
= 0; index
< lcd_scroll_info
.lines
; index
++ ) {
532 s
= &lcd_scroll_info
.scroll
[index
];
535 if (TIME_BEFORE(current_tick
, s
->start_tick
))
538 lcd_set_viewport(s
->vp
);
548 if (s
->bidir
) /* scroll bidirectional */
550 if (s
->offset
<= 0) {
551 /* at beginning of line */
554 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
* 2;
556 if (s
->offset
>= s
->len
- (current_vp
->width
- xpos
)) {
558 s
->offset
= s
->len
- (current_vp
->width
- xpos
);
560 s
->start_tick
= current_tick
+ lcd_scroll_info
.delay
* 2;
563 else /* scroll forward the whole time */
565 if (s
->offset
>= s
->len
)
569 lcd_putsxyofs(xpos
, ypos
, s
->offset
, s
->line
);
573 lcd_set_viewport(old_vp
);
575 if (lcd_cursor
.enabled
)
577 if (--lcd_cursor
.downcount
<= 0)
579 lcd_cursor
.downcount
= lcd_cursor
.divider
;
580 lcd_cursor
.visible
= !lcd_cursor
.visible
;