1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Alan Korr
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
31 #include "rbunicode.h"
33 #include "scroll_engine.h"
35 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
36 #define LCDFN(fn) lcd_ ## fn
37 #define FBFN(fn) fb_ ## fn
38 #define LCDM(ma) LCD_ ## ma
39 #define LCDNAME "lcd_"
45 FBFN(data
) LCDFN(framebuffer
)[LCDM(FBHEIGHT
)][LCDM(FBWIDTH
)]
46 #if CONFIG_CPU != SH7034
51 static struct viewport default_vp
=
56 .height
= LCDM(HEIGHT
),
57 .font
= FONT_SYSFIXED
,
58 .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 void LCDFN(setmargins
)(int x
, int y
)
112 current_vp
->xmargin
= x
;
113 current_vp
->ymargin
= y
;
116 int LCDFN(getxmargin
)(void)
118 return current_vp
->xmargin
;
121 int LCDFN(getymargin
)(void)
123 return current_vp
->ymargin
;
126 int LCDFN(getwidth
)(void)
128 return current_vp
->width
;
131 int LCDFN(getheight
)(void)
133 return current_vp
->height
;
136 void LCDFN(setfont
)(int newfont
)
138 current_vp
->font
= newfont
;
141 int LCDFN(getfont
)(void)
143 return current_vp
->font
;
146 int LCDFN(getstringsize
)(const unsigned char *str
, int *w
, int *h
)
148 return font_getstringsize(str
, w
, h
, current_vp
->font
);
151 /*** low-level drawing functions ***/
153 static void setpixel(int x
, int y
)
155 LCDFN(framebuffer
)[y
>>3][x
] |= 1 << (y
& 7);
158 static void clearpixel(int x
, int y
)
160 LCDFN(framebuffer
)[y
>>3][x
] &= ~(1 << (y
& 7));
163 static void flippixel(int x
, int y
)
165 LCDFN(framebuffer
)[y
>>3][x
] ^= 1 << (y
& 7);
168 static void nopixel(int x
, int y
)
174 LCDFN(pixelfunc_type
)* const LCDFN(pixelfuncs
)[8] = {
175 flippixel
, nopixel
, setpixel
, setpixel
,
176 nopixel
, clearpixel
, nopixel
, clearpixel
179 static void ICODE_ATTR
flipblock(FBFN(data
) *address
, unsigned mask
,
182 *address
^= bits
& mask
;
185 static void ICODE_ATTR
bgblock(FBFN(data
) *address
, unsigned mask
,
188 *address
&= bits
| ~mask
;
191 static void ICODE_ATTR
fgblock(FBFN(data
) *address
, unsigned mask
,
194 *address
|= bits
& mask
;
197 static void ICODE_ATTR
solidblock(FBFN(data
) *address
, unsigned mask
,
200 unsigned data
= *(char*)address
;
203 *address
= data
^ (bits
& mask
);
206 static void ICODE_ATTR
flipinvblock(FBFN(data
) *address
, unsigned mask
,
209 *address
^= ~bits
& mask
;
212 static void ICODE_ATTR
bginvblock(FBFN(data
) *address
, unsigned mask
,
215 *address
&= ~(bits
& mask
);
218 static void ICODE_ATTR
fginvblock(FBFN(data
) *address
, unsigned mask
,
221 *address
|= ~bits
& mask
;
224 static void ICODE_ATTR
solidinvblock(FBFN(data
) *address
, unsigned mask
,
227 unsigned data
= *(char *)address
;
230 *address
= data
^ (bits
& mask
);
233 LCDFN(blockfunc_type
)* const LCDFN(blockfuncs
)[8] = {
234 flipblock
, bgblock
, fgblock
, solidblock
,
235 flipinvblock
, bginvblock
, fginvblock
, solidinvblock
238 /*** drawing functions ***/
240 /* Clear the whole display */
241 void LCDFN(clear_display
)(void)
243 unsigned bits
= (current_vp
->drawmode
& DRMODE_INVERSEVID
) ? 0xFFu
: 0;
245 memset(LCDFN(framebuffer
), bits
, sizeof LCDFN(framebuffer
));
246 LCDFN(scroll_info
).lines
= 0;
249 /* Clear the current viewport */
250 void LCDFN(clear_viewport
)(void)
254 if (current_vp
== &default_vp
)
256 LCDFN(clear_display
)();
260 oldmode
= current_vp
->drawmode
;
262 /* Invert the INVERSEVID bit and set basic mode to SOLID */
263 current_vp
->drawmode
= (~current_vp
->drawmode
& DRMODE_INVERSEVID
) |
266 LCDFN(fillrect
)(0, 0, current_vp
->width
, current_vp
->height
);
268 current_vp
->drawmode
= oldmode
;
270 LCDFN(scroll_stop
)(current_vp
);
274 /* Set a single pixel */
275 void LCDFN(drawpixel
)(int x
, int y
)
277 if (((unsigned)x
< (unsigned)current_vp
->width
) &&
278 ((unsigned)y
< (unsigned)current_vp
->height
))
279 LCDFN(pixelfuncs
)[current_vp
->drawmode
](current_vp
->x
+ x
, current_vp
->y
+ y
);
283 void LCDFN(drawline
)(int x1
, int y1
, int x2
, int y2
)
291 LCDFN(pixelfunc_type
) *pfunc
= LCDFN(pixelfuncs
)[current_vp
->drawmode
];
293 deltax
= abs(x2
- x1
);
296 DEBUGF(LCDNAME
"drawline() called for vertical line - optimisation.\n");
297 LCDFN(vline
)(x1
, y1
, y2
);
300 deltay
= abs(y2
- y1
);
303 DEBUGF(LCDNAME
"drawline() called for horizontal line - optimisation.\n");
304 LCDFN(hline
)(x1
, x2
, y1
);
310 if (deltax
>= deltay
)
313 d
= 2 * deltay
- deltax
;
315 dinc2
= (deltay
- deltax
) * 2;
322 d
= 2 * deltax
- deltay
;
324 dinc2
= (deltax
- deltay
) * 2;
328 numpixels
++; /* include endpoints */
345 for (i
= 0; i
< numpixels
; i
++)
347 if (((unsigned)x
< (unsigned)current_vp
->width
)
348 && ((unsigned)y
< (unsigned)current_vp
->height
))
349 pfunc(current_vp
->x
+ x
, current_vp
->y
+ y
);
366 /* Draw a horizontal line (optimised) */
367 void LCDFN(hline
)(int x1
, int x2
, int y
)
370 unsigned char *dst
, *dst_end
;
372 LCDFN(blockfunc_type
) *bfunc
;
382 /* nothing to draw? */
383 if (((unsigned)y
>= (unsigned)current_vp
->height
) || (x1
>= current_vp
->width
)
390 if (x2
>= current_vp
->width
)
391 x2
= current_vp
->width
-1;
395 /* adjust to viewport */
399 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
400 dst
= &LCDFN(framebuffer
)[y
>>3][x1
];
403 dst_end
= dst
+ width
;
405 bfunc(dst
++, mask
, 0xFFu
);
406 while (dst
< dst_end
);
409 /* Draw a vertical line (optimised) */
410 void LCDFN(vline
)(int x
, int y1
, int y2
)
414 unsigned mask
, mask_bottom
;
415 LCDFN(blockfunc_type
) *bfunc
;
425 /* nothing to draw? */
426 if (((unsigned)x
>= (unsigned)current_vp
->width
) || (y1
>= current_vp
->height
)
433 if (y2
>= current_vp
->height
)
434 y2
= current_vp
->height
-1;
436 /* adjust for viewport */
441 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
442 dst
= &LCDFN(framebuffer
)[y1
>>3][x
];
444 mask
= 0xFFu
<< (y1
& 7);
445 mask_bottom
= 0xFFu
>> (~ny
& 7);
447 for (; ny
>= 8; ny
-= 8)
449 bfunc(dst
, mask
, 0xFFu
);
454 bfunc(dst
, mask
, 0xFFu
);
457 /* Draw a rectangular box */
458 void LCDFN(drawrect
)(int x
, int y
, int width
, int height
)
460 if ((width
<= 0) || (height
<= 0))
463 int x2
= x
+ width
- 1;
464 int y2
= y
+ height
- 1;
466 LCDFN(vline
)(x
, y
, y2
);
467 LCDFN(vline
)(x2
, y
, y2
);
468 LCDFN(hline
)(x
, x2
, y
);
469 LCDFN(hline
)(x
, x2
, y2
);
472 /* Fill a rectangular area */
473 void LCDFN(fillrect
)(int x
, int y
, int width
, int height
)
476 FBFN(data
) *dst
, *dst_end
;
477 unsigned mask
, mask_bottom
;
479 LCDFN(blockfunc_type
) *bfunc
;
480 bool fillopt
= false;
482 /* nothing to draw? */
483 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
484 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
498 if (x
+ width
> current_vp
->width
)
499 width
= current_vp
->width
- x
;
500 if (y
+ height
> current_vp
->height
)
501 height
= current_vp
->height
- y
;
503 /* adjust for viewport */
507 if (current_vp
->drawmode
& DRMODE_INVERSEVID
)
509 if (current_vp
->drawmode
& DRMODE_BG
)
516 if (current_vp
->drawmode
& DRMODE_FG
)
522 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
523 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
524 ny
= height
- 1 + (y
& 7);
525 mask
= 0xFFu
<< (y
& 7);
526 mask_bottom
= 0xFFu
>> (~ny
& 7);
528 for (; ny
>= 8; ny
-= 8)
530 if (fillopt
&& (mask
== 0xFFu
))
531 memset(dst
, bits
, width
);
534 FBFN(data
) *dst_row
= dst
;
536 dst_end
= dst_row
+ width
;
538 bfunc(dst_row
++, mask
, 0xFFu
);
539 while (dst_row
< dst_end
);
547 if (fillopt
&& (mask
== 0xFFu
))
548 memset(dst
, bits
, width
);
551 dst_end
= dst
+ width
;
553 bfunc(dst
++, mask
, 0xFFu
);
554 while (dst
< dst_end
);
558 /* About Rockbox' internal bitmap format:
560 * A bitmap contains one bit for every pixel that defines if that pixel is
561 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
563 * The bytes are stored in row-major order, with byte 0 being top left,
564 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
565 * 0..7, the second row defines pixel row 8..15 etc.
567 * This is the same as the internal lcd hw format. */
569 /* Draw a partial bitmap */
570 void ICODE_ATTR
LCDFN(bitmap_part
)(const unsigned char *src
, int src_x
,
571 int src_y
, int stride
, int x
, int y
,
572 int width
, int height
)
575 FBFN(data
) *dst
, *dst_end
;
576 unsigned mask
, mask_bottom
;
577 LCDFN(blockfunc_type
) *bfunc
;
579 /* nothing to draw? */
580 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
581 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
597 if (x
+ width
> current_vp
->width
)
598 width
= current_vp
->width
- x
;
599 if (y
+ height
> current_vp
->height
)
600 height
= current_vp
->height
- y
;
602 /* adjust for viewport */
606 src
+= stride
* (src_y
>> 3) + src_x
; /* move starting point */
609 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
611 ny
= height
- 1 + shift
+ src_y
;
613 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
614 mask
= 0xFFu
<< (shift
+ src_y
);
615 mask_bottom
= 0xFFu
>> (~ny
& 7);
619 bool copyopt
= (current_vp
->drawmode
== DRMODE_SOLID
);
621 for (; ny
>= 8; ny
-= 8)
623 if (copyopt
&& (mask
== 0xFFu
))
624 memcpy(dst
, src
, width
);
627 const unsigned char *src_row
= src
;
628 FBFN(data
) *dst_row
= dst
;
630 dst_end
= dst_row
+ width
;
632 bfunc(dst_row
++, mask
, *src_row
++);
633 while (dst_row
< dst_end
);
642 if (copyopt
&& (mask
== 0xFFu
))
643 memcpy(dst
, src
, width
);
646 dst_end
= dst
+ width
;
648 bfunc(dst
++, mask
, *src
++);
649 while (dst
< dst_end
);
654 dst_end
= dst
+ width
;
657 const unsigned char *src_col
= src
++;
658 FBFN(data
) *dst_col
= dst
++;
659 unsigned mask_col
= mask
;
662 for (y
= ny
; y
>= 8; y
-= 8)
664 data
|= *src_col
<< shift
;
666 if (mask_col
& 0xFFu
)
668 bfunc(dst_col
, mask_col
, data
);
675 dst_col
+= LCDM(WIDTH
);
678 data
|= *src_col
<< shift
;
679 bfunc(dst_col
, mask_col
& mask_bottom
, data
);
681 while (dst
< dst_end
);
685 /* Draw a full bitmap */
686 void LCDFN(bitmap
)(const unsigned char *src
, int x
, int y
, int width
,
689 LCDFN(bitmap_part
)(src
, 0, 0, width
, x
, y
, width
, height
);
692 /* put a string at a given pixel position, skipping first ofs pixel columns */
693 static void LCDFN(putsxyofs
)(int x
, int y
, int ofs
, const unsigned char *str
)
697 struct font
* pf
= font_get(current_vp
->font
);
699 ucs
= bidi_l2v(str
, 1);
701 while ((ch
= *ucs
++) != 0 && x
< current_vp
->width
)
704 const unsigned char *bits
;
706 /* get proportional width and glyph bits */
707 width
= font_get_width(pf
, ch
);
715 bits
= font_get_bits(pf
, ch
);
717 LCDFN(mono_bitmap_part
)(bits
, ofs
, 0, width
, x
, y
, width
- ofs
,
724 /* put a string at a given pixel position */
725 void LCDFN(putsxy
)(int x
, int y
, const unsigned char *str
)
727 LCDFN(putsxyofs
)(x
, y
, 0, str
);
730 /*** Line oriented text output ***/
732 /* put a string at a given char position */
733 void LCDFN(puts
)(int x
, int y
, const unsigned char *str
)
735 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, 0);
738 void LCDFN(puts_style
)(int x
, int y
, const unsigned char *str
, int style
)
740 LCDFN(puts_style_offset
)(x
, y
, str
, style
, 0);
743 void LCDFN(puts_offset
)(int x
, int y
, const unsigned char *str
, int offset
)
745 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, offset
);
748 /* put a string at a given char position, style, and pixel position,
749 * skipping first offset pixel columns */
750 void LCDFN(puts_style_offset
)(int x
, int y
, const unsigned char *str
,
751 int style
, int offset
)
753 int xpos
,ypos
,w
,h
,xrect
;
754 int lastmode
= current_vp
->drawmode
;
756 /* make sure scrolling is turned off on the line we are updating */
757 LCDFN(scroll_stop_line
)(current_vp
, y
);
762 LCDFN(getstringsize
)(str
, &w
, &h
);
763 xpos
= current_vp
->xmargin
+ x
*w
/ utf8length(str
);
764 ypos
= current_vp
->ymargin
+ y
*h
;
765 current_vp
->drawmode
= (style
& STYLE_INVERT
) ?
766 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
767 LCDFN(putsxyofs
)(xpos
, ypos
, offset
, str
);
768 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
769 xrect
= xpos
+ MAX(w
- offset
, 0);
770 LCDFN(fillrect
)(xrect
, ypos
, current_vp
->width
- xrect
, h
);
771 current_vp
->drawmode
= lastmode
;
776 void LCDFN(puts_scroll
)(int x
, int y
, const unsigned char *string
)
778 LCDFN(puts_scroll_style
)(x
, y
, string
, STYLE_DEFAULT
);
781 void LCDFN(puts_scroll_style
)(int x
, int y
, const unsigned char *string
,
784 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, style
, 0);
787 void LCDFN(puts_scroll_offset
)(int x
, int y
, const unsigned char *string
,
790 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, STYLE_DEFAULT
, offset
);
793 void LCDFN(puts_scroll_style_offset
)(int x
, int y
, const unsigned char *string
,
794 int style
, int offset
)
796 struct scrollinfo
* s
;
799 if ((unsigned)y
>= (unsigned)current_vp
->height
)
802 /* remove any previously scrolling line at the same location */
803 LCDFN(scroll_stop_line
)(current_vp
, y
);
805 if (LCDFN(scroll_info
.lines
) >= LCDM(SCROLLABLE_LINES
)) return;
807 s
= &LCDFN(scroll_info
).scroll
[LCDFN(scroll_info
).lines
];
809 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
;
811 if (style
& STYLE_INVERT
) {
812 LCDFN(puts_style_offset
)(x
,y
,string
,STYLE_INVERT
,offset
);
815 LCDFN(puts_offset
)(x
,y
,string
,offset
);
817 LCDFN(getstringsize
)(string
, &w
, &h
);
819 if (current_vp
->width
- x
* 8 - current_vp
->xmargin
< w
) {
820 /* prepare scroll line */
823 memset(s
->line
, 0, sizeof s
->line
);
824 strcpy(s
->line
, string
);
827 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
829 /* scroll bidirectional or forward only depending on the string
831 if ( LCDFN(scroll_info
).bidir_limit
) {
832 s
->bidir
= s
->width
< (current_vp
->width
- current_vp
->xmargin
) *
833 (100 + LCDFN(scroll_info
).bidir_limit
) / 100;
838 if (!s
->bidir
) { /* add spaces if scrolling in the round */
839 strcat(s
->line
, " ");
840 /* get new width incl. spaces */
841 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
844 end
= strchr(s
->line
, '\0');
845 strncpy(end
, string
, current_vp
->width
/2);
849 s
->len
= utf8length(string
);
851 s
->startx
= current_vp
->xmargin
+ x
* s
->width
/ s
->len
;;
854 LCDFN(scroll_info
).lines
++;
858 void LCDFN(scroll_fn
)(void)
861 struct scrollinfo
* s
;
865 struct viewport
* old_vp
= current_vp
;
867 for ( index
= 0; index
< LCDFN(scroll_info
).lines
; index
++ ) {
868 s
= &LCDFN(scroll_info
).scroll
[index
];
871 if (TIME_BEFORE(current_tick
, s
->start_tick
))
874 LCDFN(set_viewport
)(s
->vp
);
877 s
->offset
-= LCDFN(scroll_info
).step
;
879 s
->offset
+= LCDFN(scroll_info
).step
;
881 pf
= font_get(current_vp
->font
);
883 ypos
= current_vp
->ymargin
+ s
->y
* pf
->height
;
885 if (s
->bidir
) { /* scroll bidirectional */
886 if (s
->offset
<= 0) {
887 /* at beginning of line */
890 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
892 if (s
->offset
>= s
->width
- (current_vp
->width
- xpos
)) {
894 s
->offset
= s
->width
- (current_vp
->width
- xpos
);
896 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
900 /* scroll forward the whole time */
901 if (s
->offset
>= s
->width
)
902 s
->offset
%= s
->width
;
905 lastmode
= current_vp
->drawmode
;
906 current_vp
->drawmode
= (s
->style
&STYLE_INVERT
) ?
907 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
908 LCDFN(putsxyofs
)(xpos
, ypos
, s
->offset
, s
->line
);
909 current_vp
->drawmode
= lastmode
;
910 LCDFN(update_viewport_rect
)(xpos
, ypos
, current_vp
->width
- xpos
, pf
->height
);
913 LCDFN(set_viewport
)(old_vp
);