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
,
61 static struct viewport
* current_vp
= &default_vp
;
65 void LCDFN(set_viewport
)(struct viewport
* vp
)
68 current_vp
= &default_vp
;
73 void LCDFN(update_viewport
)(void)
75 LCDFN(update_rect
)(current_vp
->x
, current_vp
->y
,
76 current_vp
->width
, current_vp
->height
);
79 void LCDFN(update_viewport_rect
)(int x
, int y
, int width
, int height
)
81 LCDFN(update_rect
)(current_vp
->x
+ x
, current_vp
->y
+ y
, width
, height
);
85 void LCDFN(init
)(void)
87 LCDFN(clear_display
)();
96 /*** parameter handling ***/
98 void LCDFN(set_drawmode
)(int mode
)
100 current_vp
->drawmode
= mode
& (DRMODE_SOLID
|DRMODE_INVERSEVID
);
103 int LCDFN(get_drawmode
)(void)
105 return current_vp
->drawmode
;
108 int LCDFN(getwidth
)(void)
110 return current_vp
->width
;
113 int LCDFN(getheight
)(void)
115 return current_vp
->height
;
118 void LCDFN(setfont
)(int newfont
)
120 current_vp
->font
= newfont
;
123 int LCDFN(getfont
)(void)
125 return current_vp
->font
;
128 int LCDFN(getstringsize
)(const unsigned char *str
, int *w
, int *h
)
130 return font_getstringsize(str
, w
, h
, current_vp
->font
);
133 /*** low-level drawing functions ***/
135 static void setpixel(int x
, int y
)
137 LCDFN(framebuffer
)[y
>>3][x
] |= 1 << (y
& 7);
140 static void clearpixel(int x
, int y
)
142 LCDFN(framebuffer
)[y
>>3][x
] &= ~(1 << (y
& 7));
145 static void flippixel(int x
, int y
)
147 LCDFN(framebuffer
)[y
>>3][x
] ^= 1 << (y
& 7);
150 static void nopixel(int x
, int y
)
156 LCDFN(pixelfunc_type
)* const LCDFN(pixelfuncs
)[8] = {
157 flippixel
, nopixel
, setpixel
, setpixel
,
158 nopixel
, clearpixel
, nopixel
, clearpixel
161 static void ICODE_ATTR
flipblock(FBFN(data
) *address
, unsigned mask
,
164 *address
^= bits
& mask
;
167 static void ICODE_ATTR
bgblock(FBFN(data
) *address
, unsigned mask
,
170 *address
&= bits
| ~mask
;
173 static void ICODE_ATTR
fgblock(FBFN(data
) *address
, unsigned mask
,
176 *address
|= bits
& mask
;
179 static void ICODE_ATTR
solidblock(FBFN(data
) *address
, unsigned mask
,
182 unsigned data
= *(char*)address
;
185 *address
= data
^ (bits
& mask
);
188 static void ICODE_ATTR
flipinvblock(FBFN(data
) *address
, unsigned mask
,
191 *address
^= ~bits
& mask
;
194 static void ICODE_ATTR
bginvblock(FBFN(data
) *address
, unsigned mask
,
197 *address
&= ~(bits
& mask
);
200 static void ICODE_ATTR
fginvblock(FBFN(data
) *address
, unsigned mask
,
203 *address
|= ~bits
& mask
;
206 static void ICODE_ATTR
solidinvblock(FBFN(data
) *address
, unsigned mask
,
209 unsigned data
= *(char *)address
;
212 *address
= data
^ (bits
& mask
);
215 LCDFN(blockfunc_type
)* const LCDFN(blockfuncs
)[8] = {
216 flipblock
, bgblock
, fgblock
, solidblock
,
217 flipinvblock
, bginvblock
, fginvblock
, solidinvblock
220 /*** drawing functions ***/
222 /* Clear the whole display */
223 void LCDFN(clear_display
)(void)
225 unsigned bits
= (current_vp
->drawmode
& DRMODE_INVERSEVID
) ? 0xFFu
: 0;
227 memset(LCDFN(framebuffer
), bits
, sizeof LCDFN(framebuffer
));
228 LCDFN(scroll_info
).lines
= 0;
231 /* Clear the current viewport */
232 void LCDFN(clear_viewport
)(void)
236 if (current_vp
== &default_vp
)
238 LCDFN(clear_display
)();
242 oldmode
= current_vp
->drawmode
;
244 /* Invert the INVERSEVID bit and set basic mode to SOLID */
245 current_vp
->drawmode
= (~current_vp
->drawmode
& DRMODE_INVERSEVID
) |
248 LCDFN(fillrect
)(0, 0, current_vp
->width
, current_vp
->height
);
250 current_vp
->drawmode
= oldmode
;
252 LCDFN(scroll_stop
)(current_vp
);
256 /* Set a single pixel */
257 void LCDFN(drawpixel
)(int x
, int y
)
259 if (((unsigned)x
< (unsigned)current_vp
->width
) &&
260 ((unsigned)y
< (unsigned)current_vp
->height
))
261 LCDFN(pixelfuncs
)[current_vp
->drawmode
](current_vp
->x
+ x
, current_vp
->y
+ y
);
265 void LCDFN(drawline
)(int x1
, int y1
, int x2
, int y2
)
273 LCDFN(pixelfunc_type
) *pfunc
= LCDFN(pixelfuncs
)[current_vp
->drawmode
];
275 deltax
= abs(x2
- x1
);
278 DEBUGF(LCDNAME
"drawline() called for vertical line - optimisation.\n");
279 LCDFN(vline
)(x1
, y1
, y2
);
282 deltay
= abs(y2
- y1
);
285 DEBUGF(LCDNAME
"drawline() called for horizontal line - optimisation.\n");
286 LCDFN(hline
)(x1
, x2
, y1
);
292 if (deltax
>= deltay
)
295 d
= 2 * deltay
- deltax
;
297 dinc2
= (deltay
- deltax
) * 2;
304 d
= 2 * deltax
- deltay
;
306 dinc2
= (deltax
- deltay
) * 2;
310 numpixels
++; /* include endpoints */
327 for (i
= 0; i
< numpixels
; i
++)
329 if (((unsigned)x
< (unsigned)current_vp
->width
)
330 && ((unsigned)y
< (unsigned)current_vp
->height
))
331 pfunc(current_vp
->x
+ x
, current_vp
->y
+ y
);
348 /* Draw a horizontal line (optimised) */
349 void LCDFN(hline
)(int x1
, int x2
, int y
)
352 unsigned char *dst
, *dst_end
;
354 LCDFN(blockfunc_type
) *bfunc
;
364 /* nothing to draw? */
365 if (((unsigned)y
>= (unsigned)current_vp
->height
) || (x1
>= current_vp
->width
)
372 if (x2
>= current_vp
->width
)
373 x2
= current_vp
->width
-1;
377 /* adjust to viewport */
381 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
382 dst
= &LCDFN(framebuffer
)[y
>>3][x1
];
385 dst_end
= dst
+ width
;
387 bfunc(dst
++, mask
, 0xFFu
);
388 while (dst
< dst_end
);
391 /* Draw a vertical line (optimised) */
392 void LCDFN(vline
)(int x
, int y1
, int y2
)
396 unsigned mask
, mask_bottom
;
397 LCDFN(blockfunc_type
) *bfunc
;
407 /* nothing to draw? */
408 if (((unsigned)x
>= (unsigned)current_vp
->width
) || (y1
>= current_vp
->height
)
415 if (y2
>= current_vp
->height
)
416 y2
= current_vp
->height
-1;
418 /* adjust for viewport */
423 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
424 dst
= &LCDFN(framebuffer
)[y1
>>3][x
];
426 mask
= 0xFFu
<< (y1
& 7);
427 mask_bottom
= 0xFFu
>> (~ny
& 7);
429 for (; ny
>= 8; ny
-= 8)
431 bfunc(dst
, mask
, 0xFFu
);
436 bfunc(dst
, mask
, 0xFFu
);
439 /* Draw a rectangular box */
440 void LCDFN(drawrect
)(int x
, int y
, int width
, int height
)
442 if ((width
<= 0) || (height
<= 0))
445 int x2
= x
+ width
- 1;
446 int y2
= y
+ height
- 1;
448 LCDFN(vline
)(x
, y
, y2
);
449 LCDFN(vline
)(x2
, y
, y2
);
450 LCDFN(hline
)(x
, x2
, y
);
451 LCDFN(hline
)(x
, x2
, y2
);
454 /* Fill a rectangular area */
455 void LCDFN(fillrect
)(int x
, int y
, int width
, int height
)
458 FBFN(data
) *dst
, *dst_end
;
459 unsigned mask
, mask_bottom
;
461 LCDFN(blockfunc_type
) *bfunc
;
462 bool fillopt
= false;
464 /* nothing to draw? */
465 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
466 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
480 if (x
+ width
> current_vp
->width
)
481 width
= current_vp
->width
- x
;
482 if (y
+ height
> current_vp
->height
)
483 height
= current_vp
->height
- y
;
485 /* adjust for viewport */
489 if (current_vp
->drawmode
& DRMODE_INVERSEVID
)
491 if (current_vp
->drawmode
& DRMODE_BG
)
498 if (current_vp
->drawmode
& DRMODE_FG
)
504 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
505 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
506 ny
= height
- 1 + (y
& 7);
507 mask
= 0xFFu
<< (y
& 7);
508 mask_bottom
= 0xFFu
>> (~ny
& 7);
510 for (; ny
>= 8; ny
-= 8)
512 if (fillopt
&& (mask
== 0xFFu
))
513 memset(dst
, bits
, width
);
516 FBFN(data
) *dst_row
= dst
;
518 dst_end
= dst_row
+ width
;
520 bfunc(dst_row
++, mask
, 0xFFu
);
521 while (dst_row
< dst_end
);
529 if (fillopt
&& (mask
== 0xFFu
))
530 memset(dst
, bits
, width
);
533 dst_end
= dst
+ width
;
535 bfunc(dst
++, mask
, 0xFFu
);
536 while (dst
< dst_end
);
540 /* About Rockbox' internal bitmap format:
542 * A bitmap contains one bit for every pixel that defines if that pixel is
543 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
545 * The bytes are stored in row-major order, with byte 0 being top left,
546 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
547 * 0..7, the second row defines pixel row 8..15 etc.
549 * This is the same as the internal lcd hw format. */
551 /* Draw a partial bitmap */
552 void ICODE_ATTR
LCDFN(bitmap_part
)(const unsigned char *src
, int src_x
,
553 int src_y
, int stride
, int x
, int y
,
554 int width
, int height
)
557 FBFN(data
) *dst
, *dst_end
;
558 unsigned mask
, mask_bottom
;
559 LCDFN(blockfunc_type
) *bfunc
;
561 /* nothing to draw? */
562 if ((width
<= 0) || (height
<= 0) || (x
>= current_vp
->width
)
563 || (y
>= current_vp
->height
) || (x
+ width
<= 0) || (y
+ height
<= 0))
579 if (x
+ width
> current_vp
->width
)
580 width
= current_vp
->width
- x
;
581 if (y
+ height
> current_vp
->height
)
582 height
= current_vp
->height
- y
;
584 /* adjust for viewport */
588 src
+= stride
* (src_y
>> 3) + src_x
; /* move starting point */
591 dst
= &LCDFN(framebuffer
)[y
>>3][x
];
593 ny
= height
- 1 + shift
+ src_y
;
595 bfunc
= LCDFN(blockfuncs
)[current_vp
->drawmode
];
596 mask
= 0xFFu
<< (shift
+ src_y
);
597 mask_bottom
= 0xFFu
>> (~ny
& 7);
601 bool copyopt
= (current_vp
->drawmode
== DRMODE_SOLID
);
603 for (; ny
>= 8; ny
-= 8)
605 if (copyopt
&& (mask
== 0xFFu
))
606 memcpy(dst
, src
, width
);
609 const unsigned char *src_row
= src
;
610 FBFN(data
) *dst_row
= dst
;
612 dst_end
= dst_row
+ width
;
614 bfunc(dst_row
++, mask
, *src_row
++);
615 while (dst_row
< dst_end
);
624 if (copyopt
&& (mask
== 0xFFu
))
625 memcpy(dst
, src
, width
);
628 dst_end
= dst
+ width
;
630 bfunc(dst
++, mask
, *src
++);
631 while (dst
< dst_end
);
636 dst_end
= dst
+ width
;
639 const unsigned char *src_col
= src
++;
640 FBFN(data
) *dst_col
= dst
++;
641 unsigned mask_col
= mask
;
644 for (y
= ny
; y
>= 8; y
-= 8)
646 data
|= *src_col
<< shift
;
648 if (mask_col
& 0xFFu
)
650 bfunc(dst_col
, mask_col
, data
);
657 dst_col
+= LCDM(WIDTH
);
660 data
|= *src_col
<< shift
;
661 bfunc(dst_col
, mask_col
& mask_bottom
, data
);
663 while (dst
< dst_end
);
667 /* Draw a full bitmap */
668 void LCDFN(bitmap
)(const unsigned char *src
, int x
, int y
, int width
,
671 LCDFN(bitmap_part
)(src
, 0, 0, width
, x
, y
, width
, height
);
674 /* put a string at a given pixel position, skipping first ofs pixel columns */
675 static void LCDFN(putsxyofs
)(int x
, int y
, int ofs
, const unsigned char *str
)
679 struct font
* pf
= font_get(current_vp
->font
);
681 ucs
= bidi_l2v(str
, 1);
683 while ((ch
= *ucs
++) != 0 && x
< current_vp
->width
)
686 const unsigned char *bits
;
688 /* get proportional width and glyph bits */
689 width
= font_get_width(pf
, ch
);
697 bits
= font_get_bits(pf
, ch
);
699 LCDFN(mono_bitmap_part
)(bits
, ofs
, 0, width
, x
, y
, width
- ofs
,
706 /* put a string at a given pixel position */
707 void LCDFN(putsxy
)(int x
, int y
, const unsigned char *str
)
709 LCDFN(putsxyofs
)(x
, y
, 0, str
);
712 /*** Line oriented text output ***/
714 /* put a string at a given char position */
715 void LCDFN(puts
)(int x
, int y
, const unsigned char *str
)
717 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, 0);
720 void LCDFN(puts_style
)(int x
, int y
, const unsigned char *str
, int style
)
722 LCDFN(puts_style_offset
)(x
, y
, str
, style
, 0);
725 void LCDFN(puts_offset
)(int x
, int y
, const unsigned char *str
, int offset
)
727 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, offset
);
730 /* put a string at a given char position, style, and pixel position,
731 * skipping first offset pixel columns */
732 void LCDFN(puts_style_offset
)(int x
, int y
, const unsigned char *str
,
733 int style
, int offset
)
735 int xpos
,ypos
,w
,h
,xrect
;
736 int lastmode
= current_vp
->drawmode
;
738 /* make sure scrolling is turned off on the line we are updating */
739 LCDFN(scroll_stop_line
)(current_vp
, y
);
744 LCDFN(getstringsize
)(str
, &w
, &h
);
745 xpos
= x
*w
/ utf8length(str
);
747 current_vp
->drawmode
= (style
& STYLE_INVERT
) ?
748 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
749 LCDFN(putsxyofs
)(xpos
, ypos
, offset
, str
);
750 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
751 xrect
= xpos
+ MAX(w
- offset
, 0);
752 LCDFN(fillrect
)(xrect
, ypos
, current_vp
->width
- xrect
, h
);
753 current_vp
->drawmode
= lastmode
;
758 void LCDFN(puts_scroll
)(int x
, int y
, const unsigned char *string
)
760 LCDFN(puts_scroll_style
)(x
, y
, string
, STYLE_DEFAULT
);
763 void LCDFN(puts_scroll_style
)(int x
, int y
, const unsigned char *string
,
766 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, style
, 0);
769 void LCDFN(puts_scroll_offset
)(int x
, int y
, const unsigned char *string
,
772 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, STYLE_DEFAULT
, offset
);
775 void LCDFN(puts_scroll_style_offset
)(int x
, int y
, const unsigned char *string
,
776 int style
, int offset
)
778 struct scrollinfo
* s
;
781 if ((unsigned)y
>= (unsigned)current_vp
->height
)
784 /* remove any previously scrolling line at the same location */
785 LCDFN(scroll_stop_line
)(current_vp
, y
);
787 if (LCDFN(scroll_info
.lines
) >= LCDM(SCROLLABLE_LINES
)) return;
789 s
= &LCDFN(scroll_info
).scroll
[LCDFN(scroll_info
).lines
];
791 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
;
793 if (style
& STYLE_INVERT
) {
794 LCDFN(puts_style_offset
)(x
,y
,string
,STYLE_INVERT
,offset
);
797 LCDFN(puts_offset
)(x
,y
,string
,offset
);
799 LCDFN(getstringsize
)(string
, &w
, &h
);
801 if (current_vp
->width
- x
* 8 < w
) {
802 /* prepare scroll line */
805 memset(s
->line
, 0, sizeof s
->line
);
806 strcpy(s
->line
, string
);
809 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
811 /* scroll bidirectional or forward only depending on the string
813 if ( LCDFN(scroll_info
).bidir_limit
) {
814 s
->bidir
= s
->width
< (current_vp
->width
) *
815 (100 + LCDFN(scroll_info
).bidir_limit
) / 100;
820 if (!s
->bidir
) { /* add spaces if scrolling in the round */
821 strcat(s
->line
, " ");
822 /* get new width incl. spaces */
823 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
826 end
= strchr(s
->line
, '\0');
827 strncpy(end
, string
, current_vp
->width
/2);
831 s
->len
= utf8length(string
);
833 s
->startx
= x
* s
->width
/ s
->len
;;
836 LCDFN(scroll_info
).lines
++;
840 void LCDFN(scroll_fn
)(void)
843 struct scrollinfo
* s
;
847 struct viewport
* old_vp
= current_vp
;
849 for ( index
= 0; index
< LCDFN(scroll_info
).lines
; index
++ ) {
850 s
= &LCDFN(scroll_info
).scroll
[index
];
853 if (TIME_BEFORE(current_tick
, s
->start_tick
))
856 LCDFN(set_viewport
)(s
->vp
);
859 s
->offset
-= LCDFN(scroll_info
).step
;
861 s
->offset
+= LCDFN(scroll_info
).step
;
863 pf
= font_get(current_vp
->font
);
865 ypos
= s
->y
* pf
->height
;
867 if (s
->bidir
) { /* scroll bidirectional */
868 if (s
->offset
<= 0) {
869 /* at beginning of line */
872 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
874 if (s
->offset
>= s
->width
- (current_vp
->width
- xpos
)) {
876 s
->offset
= s
->width
- (current_vp
->width
- xpos
);
878 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
882 /* scroll forward the whole time */
883 if (s
->offset
>= s
->width
)
884 s
->offset
%= s
->width
;
887 lastmode
= current_vp
->drawmode
;
888 current_vp
->drawmode
= (s
->style
&STYLE_INVERT
) ?
889 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
890 LCDFN(putsxyofs
)(xpos
, ypos
, s
->offset
, s
->line
);
891 current_vp
->drawmode
= lastmode
;
892 LCDFN(update_viewport_rect
)(xpos
, ypos
, current_vp
->width
- xpos
, pf
->height
);
895 LCDFN(set_viewport
)(old_vp
);