1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Alan Korr
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
33 #include "rbunicode.h"
35 #include "scroll_engine.h"
37 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
38 #define LCDFN(fn) lcd_ ## fn
39 #define FBFN(fn) fb_ ## fn
40 #define LCDM(ma) LCD_ ## ma
41 #define LCDNAME "lcd_"
47 FBFN(data
) LCDFN(framebuffer
)[LCDM(FBHEIGHT
)][LCDM(FBWIDTH
)]
48 #if CONFIG_CPU != SH7034
53 static struct viewport default_vp
=
58 .height
= LCDM(HEIGHT
),
59 .font
= FONT_SYSFIXED
,
60 .drawmode
= DRMODE_SOLID
,
63 static struct viewport
* current_vp
= &default_vp
;
67 void LCDFN(set_viewport
)(struct viewport
* vp
)
70 current_vp
= &default_vp
;
75 void LCDFN(update_viewport
)(void)
77 LCDFN(update_rect
)(current_vp
->x
, current_vp
->y
,
78 current_vp
->width
, current_vp
->height
);
81 void LCDFN(update_viewport_rect
)(int x
, int y
, int width
, int height
)
83 LCDFN(update_rect
)(current_vp
->x
+ x
, current_vp
->y
+ y
, width
, height
);
87 void LCDFN(init
)(void)
89 LCDFN(clear_display
)();
98 /*** parameter handling ***/
100 void LCDFN(set_drawmode
)(int mode
)
102 current_vp
->drawmode
= mode
& (DRMODE_SOLID
|DRMODE_INVERSEVID
);
105 int LCDFN(get_drawmode
)(void)
107 return current_vp
->drawmode
;
110 int LCDFN(getwidth
)(void)
112 return current_vp
->width
;
115 int LCDFN(getheight
)(void)
117 return current_vp
->height
;
120 void LCDFN(setfont
)(int newfont
)
122 current_vp
->font
= newfont
;
125 int LCDFN(getfont
)(void)
127 return current_vp
->font
;
130 int LCDFN(getstringsize
)(const unsigned char *str
, int *w
, int *h
)
132 return font_getstringsize(str
, w
, h
, current_vp
->font
);
135 /*** low-level drawing functions ***/
137 static void setpixel(int x
, int y
)
139 LCDFN(framebuffer
)[y
>>3][x
] |= 1 << (y
& 7);
142 static void clearpixel(int x
, int y
)
144 LCDFN(framebuffer
)[y
>>3][x
] &= ~(1 << (y
& 7));
147 static void flippixel(int x
, int y
)
149 LCDFN(framebuffer
)[y
>>3][x
] ^= 1 << (y
& 7);
152 static void nopixel(int x
, int y
)
158 LCDFN(pixelfunc_type
)* const LCDFN(pixelfuncs
)[8] = {
159 flippixel
, nopixel
, setpixel
, setpixel
,
160 nopixel
, clearpixel
, nopixel
, clearpixel
163 static void ICODE_ATTR
flipblock(FBFN(data
) *address
, unsigned mask
,
166 *address
^= bits
& mask
;
169 static void ICODE_ATTR
bgblock(FBFN(data
) *address
, unsigned mask
,
172 *address
&= bits
| ~mask
;
175 static void ICODE_ATTR
fgblock(FBFN(data
) *address
, unsigned mask
,
178 *address
|= bits
& mask
;
181 static void ICODE_ATTR
solidblock(FBFN(data
) *address
, unsigned mask
,
184 unsigned data
= *(char*)address
;
187 *address
= data
^ (bits
& mask
);
190 static void ICODE_ATTR
flipinvblock(FBFN(data
) *address
, unsigned mask
,
193 *address
^= ~bits
& mask
;
196 static void ICODE_ATTR
bginvblock(FBFN(data
) *address
, unsigned mask
,
199 *address
&= ~(bits
& mask
);
202 static void ICODE_ATTR
fginvblock(FBFN(data
) *address
, unsigned mask
,
205 *address
|= ~bits
& mask
;
208 static void ICODE_ATTR
solidinvblock(FBFN(data
) *address
, unsigned mask
,
211 unsigned data
= *(char *)address
;
214 *address
= data
^ (bits
& mask
);
217 LCDFN(blockfunc_type
)* const LCDFN(blockfuncs
)[8] = {
218 flipblock
, bgblock
, fgblock
, solidblock
,
219 flipinvblock
, bginvblock
, fginvblock
, solidinvblock
222 /*** drawing functions ***/
224 /* Clear the whole display */
225 void LCDFN(clear_display
)(void)
227 unsigned bits
= (current_vp
->drawmode
& DRMODE_INVERSEVID
) ? 0xFFu
: 0;
229 memset(LCDFN(framebuffer
), bits
, sizeof LCDFN(framebuffer
));
230 LCDFN(scroll_info
).lines
= 0;
233 /* Clear the current viewport */
234 void LCDFN(clear_viewport
)(void)
238 if (current_vp
== &default_vp
)
240 LCDFN(clear_display
)();
244 oldmode
= current_vp
->drawmode
;
246 /* Invert the INVERSEVID bit and set basic mode to SOLID */
247 current_vp
->drawmode
= (~current_vp
->drawmode
& DRMODE_INVERSEVID
) |
250 LCDFN(fillrect
)(0, 0, current_vp
->width
, current_vp
->height
);
252 current_vp
->drawmode
= oldmode
;
254 LCDFN(scroll_stop
)(current_vp
);
258 /* Set a single pixel */
259 void LCDFN(drawpixel
)(int x
, int y
)
261 if (((unsigned)x
< (unsigned)current_vp
->width
) &&
262 ((unsigned)y
< (unsigned)current_vp
->height
))
263 LCDFN(pixelfuncs
)[current_vp
->drawmode
](current_vp
->x
+ x
, current_vp
->y
+ y
);
267 void LCDFN(drawline
)(int x1
, int y1
, int x2
, int y2
)
275 LCDFN(pixelfunc_type
) *pfunc
= LCDFN(pixelfuncs
)[current_vp
->drawmode
];
277 deltax
= abs(x2
- x1
);
280 DEBUGF(LCDNAME
"drawline() called for vertical line - optimisation.\n");
281 LCDFN(vline
)(x1
, y1
, y2
);
284 deltay
= abs(y2
- y1
);
287 DEBUGF(LCDNAME
"drawline() called for horizontal line - optimisation.\n");
288 LCDFN(hline
)(x1
, x2
, y1
);
294 if (deltax
>= deltay
)
297 d
= 2 * deltay
- deltax
;
299 dinc2
= (deltay
- deltax
) * 2;
306 d
= 2 * deltax
- deltay
;
308 dinc2
= (deltax
- deltay
) * 2;
312 numpixels
++; /* include endpoints */
329 for (i
= 0; i
< numpixels
; i
++)
331 if (((unsigned)x
< (unsigned)current_vp
->width
)
332 && ((unsigned)y
< (unsigned)current_vp
->height
))
333 pfunc(current_vp
->x
+ x
, current_vp
->y
+ y
);
350 /* Draw a horizontal line (optimised) */
351 void LCDFN(hline
)(int x1
, int x2
, int y
)
354 unsigned char *dst
, *dst_end
;
356 LCDFN(blockfunc_type
) *bfunc
;
366 /* nothing to draw? */
367 if (((unsigned)y
>= (unsigned)current_vp
->height
) || (x1
>= current_vp
->width
)
374 if (x2
>= current_vp
->width
)
375 x2
= current_vp
->width
-1;
379 /* adjust to viewport */
383 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
384 dst
= &LCDFN(framebuffer
)[y
>>3][x1
];
387 dst_end
= dst
+ width
;
389 bfunc(dst
++, mask
, 0xFFu
);
390 while (dst
< dst_end
);
393 /* Draw a vertical line (optimised) */
394 void LCDFN(vline
)(int x
, int y1
, int y2
)
398 unsigned mask
, mask_bottom
;
399 LCDFN(blockfunc_type
) *bfunc
;
409 /* nothing to draw? */
410 if (((unsigned)x
>= (unsigned)current_vp
->width
) || (y1
>= current_vp
->height
)
417 if (y2
>= current_vp
->height
)
418 y2
= current_vp
->height
-1;
420 /* adjust for viewport */
425 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
426 dst
= &LCDFN(framebuffer
)[y1
>>3][x
];
428 mask
= 0xFFu
<< (y1
& 7);
429 mask_bottom
= 0xFFu
>> (~ny
& 7);
431 for (; ny
>= 8; ny
-= 8)
433 bfunc(dst
, mask
, 0xFFu
);
438 bfunc(dst
, mask
, 0xFFu
);
441 /* Draw a rectangular box */
442 void LCDFN(drawrect
)(int x
, int y
, int width
, int height
)
444 if ((width
<= 0) || (height
<= 0))
447 int x2
= x
+ width
- 1;
448 int y2
= y
+ height
- 1;
450 LCDFN(vline
)(x
, y
, y2
);
451 LCDFN(vline
)(x2
, y
, y2
);
452 LCDFN(hline
)(x
, x2
, y
);
453 LCDFN(hline
)(x
, x2
, y2
);
456 /* Fill a rectangular area */
457 void LCDFN(fillrect
)(int x
, int y
, int width
, int height
)
460 FBFN(data
) *dst
, *dst_end
;
461 unsigned mask
, mask_bottom
;
463 LCDFN(blockfunc_type
) *bfunc
;
464 bool fillopt
= false;
466 /* nothing to draw? */
467 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
468 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
482 if (x
+ width
> current_vp
->width
)
483 width
= current_vp
->width
- x
;
484 if (y
+ height
> current_vp
->height
)
485 height
= current_vp
->height
- y
;
487 /* adjust for viewport */
491 if (current_vp
->drawmode
& DRMODE_INVERSEVID
)
493 if (current_vp
->drawmode
& DRMODE_BG
)
500 if (current_vp
->drawmode
& DRMODE_FG
)
506 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
507 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
508 ny
= height
- 1 + (y
& 7);
509 mask
= 0xFFu
<< (y
& 7);
510 mask_bottom
= 0xFFu
>> (~ny
& 7);
512 for (; ny
>= 8; ny
-= 8)
514 if (fillopt
&& (mask
== 0xFFu
))
515 memset(dst
, bits
, width
);
518 FBFN(data
) *dst_row
= dst
;
520 dst_end
= dst_row
+ width
;
522 bfunc(dst_row
++, mask
, 0xFFu
);
523 while (dst_row
< dst_end
);
531 if (fillopt
&& (mask
== 0xFFu
))
532 memset(dst
, bits
, width
);
535 dst_end
= dst
+ width
;
537 bfunc(dst
++, mask
, 0xFFu
);
538 while (dst
< dst_end
);
542 /* About Rockbox' internal bitmap format:
544 * A bitmap contains one bit for every pixel that defines if that pixel is
545 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
547 * The bytes are stored in row-major order, with byte 0 being top left,
548 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
549 * 0..7, the second row defines pixel row 8..15 etc.
551 * This is the same as the internal lcd hw format. */
553 /* Draw a partial bitmap */
554 void ICODE_ATTR
LCDFN(bitmap_part
)(const unsigned char *src
, int src_x
,
555 int src_y
, int stride
, int x
, int y
,
556 int width
, int height
)
559 FBFN(data
) *dst
, *dst_end
;
560 unsigned mask
, mask_bottom
;
561 LCDFN(blockfunc_type
) *bfunc
;
563 /* nothing to draw? */
564 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
565 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
581 if (x
+ width
> current_vp
->width
)
582 width
= current_vp
->width
- x
;
583 if (y
+ height
> current_vp
->height
)
584 height
= current_vp
->height
- y
;
586 /* adjust for viewport */
590 src
+= stride
* (src_y
>> 3) + src_x
; /* move starting point */
593 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
595 ny
= height
- 1 + shift
+ src_y
;
597 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
598 mask
= 0xFFu
<< (shift
+ src_y
);
599 mask_bottom
= 0xFFu
>> (~ny
& 7);
603 bool copyopt
= (current_vp
->drawmode
== DRMODE_SOLID
);
605 for (; ny
>= 8; ny
-= 8)
607 if (copyopt
&& (mask
== 0xFFu
))
608 memcpy(dst
, src
, width
);
611 const unsigned char *src_row
= src
;
612 FBFN(data
) *dst_row
= dst
;
614 dst_end
= dst_row
+ width
;
616 bfunc(dst_row
++, mask
, *src_row
++);
617 while (dst_row
< dst_end
);
626 if (copyopt
&& (mask
== 0xFFu
))
627 memcpy(dst
, src
, width
);
630 dst_end
= dst
+ width
;
632 bfunc(dst
++, mask
, *src
++);
633 while (dst
< dst_end
);
638 dst_end
= dst
+ width
;
641 const unsigned char *src_col
= src
++;
642 FBFN(data
) *dst_col
= dst
++;
643 unsigned mask_col
= mask
;
646 for (y
= ny
; y
>= 8; y
-= 8)
648 data
|= *src_col
<< shift
;
650 if (mask_col
& 0xFFu
)
652 bfunc(dst_col
, mask_col
, data
);
659 dst_col
+= LCDM(WIDTH
);
662 data
|= *src_col
<< shift
;
663 bfunc(dst_col
, mask_col
& mask_bottom
, data
);
665 while (dst
< dst_end
);
669 /* Draw a full bitmap */
670 void LCDFN(bitmap
)(const unsigned char *src
, int x
, int y
, int width
,
673 LCDFN(bitmap_part
)(src
, 0, 0, width
, x
, y
, width
, height
);
676 /* put a string at a given pixel position, skipping first ofs pixel columns */
677 static void LCDFN(putsxyofs
)(int x
, int y
, int ofs
, const unsigned char *str
)
681 struct font
* pf
= font_get(current_vp
->font
);
683 ucs
= bidi_l2v(str
, 1);
685 while ((ch
= *ucs
++) != 0 && x
< current_vp
->width
)
688 const unsigned char *bits
;
690 /* get proportional width and glyph bits */
691 width
= font_get_width(pf
, ch
);
699 bits
= font_get_bits(pf
, ch
);
701 LCDFN(mono_bitmap_part
)(bits
, ofs
, 0, width
, x
, y
, width
- ofs
,
708 /* put a string at a given pixel position */
709 void LCDFN(putsxy
)(int x
, int y
, const unsigned char *str
)
711 LCDFN(putsxyofs
)(x
, y
, 0, str
);
714 /*** Line oriented text output ***/
716 /* put a string at a given char position */
717 void LCDFN(puts
)(int x
, int y
, const unsigned char *str
)
719 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, 0);
722 void LCDFN(puts_style
)(int x
, int y
, const unsigned char *str
, int style
)
724 LCDFN(puts_style_offset
)(x
, y
, str
, style
, 0);
727 void LCDFN(puts_offset
)(int x
, int y
, const unsigned char *str
, int offset
)
729 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, offset
);
732 /* put a string at a given char position, style, and pixel position,
733 * skipping first offset pixel columns */
734 void LCDFN(puts_style_offset
)(int x
, int y
, const unsigned char *str
,
735 int style
, int offset
)
737 int xpos
,ypos
,w
,h
,xrect
;
738 int lastmode
= current_vp
->drawmode
;
740 /* make sure scrolling is turned off on the line we are updating */
741 LCDFN(scroll_stop_line
)(current_vp
, y
);
746 LCDFN(getstringsize
)(str
, &w
, &h
);
747 xpos
= x
*w
/ utf8length(str
);
749 current_vp
->drawmode
= (style
& STYLE_INVERT
) ?
750 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
751 LCDFN(putsxyofs
)(xpos
, ypos
, offset
, str
);
752 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
753 xrect
= xpos
+ MAX(w
- offset
, 0);
754 LCDFN(fillrect
)(xrect
, ypos
, current_vp
->width
- xrect
, h
);
755 current_vp
->drawmode
= lastmode
;
760 void LCDFN(puts_scroll
)(int x
, int y
, const unsigned char *string
)
762 LCDFN(puts_scroll_style
)(x
, y
, string
, STYLE_DEFAULT
);
765 void LCDFN(puts_scroll_style
)(int x
, int y
, const unsigned char *string
,
768 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, style
, 0);
771 void LCDFN(puts_scroll_offset
)(int x
, int y
, const unsigned char *string
,
774 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, STYLE_DEFAULT
, offset
);
777 void LCDFN(puts_scroll_style_offset
)(int x
, int y
, const unsigned char *string
,
778 int style
, int offset
)
780 struct scrollinfo
* s
;
783 if ((unsigned)y
>= (unsigned)current_vp
->height
)
786 /* remove any previously scrolling line at the same location */
787 LCDFN(scroll_stop_line
)(current_vp
, y
);
789 if (LCDFN(scroll_info
.lines
) >= LCDM(SCROLLABLE_LINES
)) return;
791 s
= &LCDFN(scroll_info
).scroll
[LCDFN(scroll_info
).lines
];
793 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
;
795 if (style
& STYLE_INVERT
) {
796 LCDFN(puts_style_offset
)(x
,y
,string
,STYLE_INVERT
,offset
);
799 LCDFN(puts_offset
)(x
,y
,string
,offset
);
801 LCDFN(getstringsize
)(string
, &w
, &h
);
803 if (current_vp
->width
- x
* 8 < w
) {
804 /* prepare scroll line */
807 memset(s
->line
, 0, sizeof s
->line
);
808 strcpy(s
->line
, string
);
811 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
813 /* scroll bidirectional or forward only depending on the string
815 if ( LCDFN(scroll_info
).bidir_limit
) {
816 s
->bidir
= s
->width
< (current_vp
->width
) *
817 (100 + LCDFN(scroll_info
).bidir_limit
) / 100;
822 if (!s
->bidir
) { /* add spaces if scrolling in the round */
823 strcat(s
->line
, " ");
824 /* get new width incl. spaces */
825 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
828 end
= strchr(s
->line
, '\0');
829 strncpy(end
, string
, current_vp
->width
/2);
833 s
->len
= utf8length(string
);
835 s
->startx
= x
* s
->width
/ s
->len
;;
838 LCDFN(scroll_info
).lines
++;
842 void LCDFN(scroll_fn
)(void)
845 struct scrollinfo
* s
;
849 struct viewport
* old_vp
= current_vp
;
851 for ( index
= 0; index
< LCDFN(scroll_info
).lines
; index
++ ) {
852 s
= &LCDFN(scroll_info
).scroll
[index
];
855 if (TIME_BEFORE(current_tick
, s
->start_tick
))
858 LCDFN(set_viewport
)(s
->vp
);
861 s
->offset
-= LCDFN(scroll_info
).step
;
863 s
->offset
+= LCDFN(scroll_info
).step
;
865 pf
= font_get(current_vp
->font
);
867 ypos
= s
->y
* pf
->height
;
869 if (s
->bidir
) { /* scroll bidirectional */
870 if (s
->offset
<= 0) {
871 /* at beginning of line */
874 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
876 if (s
->offset
>= s
->width
- (current_vp
->width
- xpos
)) {
878 s
->offset
= s
->width
- (current_vp
->width
- xpos
);
880 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
884 /* scroll forward the whole time */
885 if (s
->offset
>= s
->width
)
886 s
->offset
%= s
->width
;
889 lastmode
= current_vp
->drawmode
;
890 current_vp
->drawmode
= (s
->style
&STYLE_INVERT
) ?
891 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
892 LCDFN(putsxyofs
)(xpos
, ypos
, s
->offset
, s
->line
);
893 current_vp
->drawmode
= lastmode
;
894 LCDFN(update_viewport_rect
)(xpos
, ypos
, current_vp
->width
- xpos
, pf
->height
);
897 LCDFN(set_viewport
)(old_vp
);