Add backdrop functions to the multiscreen api and add a enum backdrop_type parameter...
[kugel-rb.git] / firmware / drivers / lcd-16bit.c
blob882bfa0854e49a06ff6712eeb85857b1a4eac34a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Dave Chapman
12 * Rockbox driver for 16-bit colour LCDs
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
23 #include "config.h"
25 #include "cpu.h"
26 #include "lcd.h"
27 #include "kernel.h"
28 #include "thread.h"
29 #include <string.h>
30 #include <stdlib.h>
31 #include "memory.h"
32 #include "file.h"
33 #include "debug.h"
34 #include "system.h"
35 #include "font.h"
36 #include "rbunicode.h"
37 #include "bidi.h"
38 #include "scroll_engine.h"
40 enum fill_opt {
41 OPT_NONE = 0,
42 OPT_SET,
43 OPT_COPY
46 /*** globals ***/
47 fb_data lcd_framebuffer[LCD_FBHEIGHT][LCD_FBWIDTH]
48 IRAM_LCDFRAMEBUFFER CACHEALIGN_AT_LEAST_ATTR(16);
51 static fb_data* lcd_backdrop = NULL;
52 static long lcd_backdrop_offset IDATA_ATTR = 0;
54 static struct viewport default_vp =
56 .x = 0,
57 .y = 0,
58 .width = LCD_WIDTH,
59 .height = LCD_HEIGHT,
60 .font = FONT_SYSFIXED,
61 .drawmode = DRMODE_SOLID,
62 .fg_pattern = LCD_DEFAULT_FG,
63 .bg_pattern = LCD_DEFAULT_BG,
64 .lss_pattern = LCD_DEFAULT_BG,
65 .lse_pattern = LCD_DEFAULT_BG,
66 .lst_pattern = LCD_DEFAULT_BG,
69 /* The Gigabeat target build requires access to the current fg_pattern
70 in lcd-meg-fx.c */
71 #if (!defined(TOSHIBA_GIGABEAT_F)&& !defined(TOSHIBA_GIGABEAT_S)) || defined(SIMULATOR)
72 static struct viewport* current_vp IDATA_ATTR = &default_vp;
73 #else
74 struct viewport* current_vp IDATA_ATTR = &default_vp;
75 #endif
78 /*** Helpers - consolidate optional code ***/
79 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
80 static void (*lcd_activation_hook)(void) = NULL;
82 void lcd_activation_set_hook(void (*func)(void))
84 lcd_activation_hook = func;
87 /* To be called by target driver after enabling display and refreshing it */
88 void lcd_activation_call_hook(void)
90 void (*func)(void) = lcd_activation_hook;
92 if (func != NULL)
93 func();
96 #endif
98 /* LCD init */
99 void lcd_init(void)
101 lcd_clear_display();
103 /* Call device specific init */
104 lcd_init_device();
105 scroll_init();
107 /*** Viewports ***/
109 void lcd_set_viewport(struct viewport* vp)
111 if (vp == NULL)
112 current_vp = &default_vp;
113 else
114 current_vp = vp;
117 void lcd_update_viewport(void)
119 lcd_update_rect(current_vp->x, current_vp->y,
120 current_vp->width, current_vp->height);
123 void lcd_update_viewport_rect(int x, int y, int width, int height)
125 lcd_update_rect(current_vp->x + x, current_vp->y + y, width, height);
128 /*** parameter handling ***/
130 void lcd_set_drawmode(int mode)
132 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
135 int lcd_get_drawmode(void)
137 return current_vp->drawmode;
140 void lcd_set_foreground(unsigned color)
142 current_vp->fg_pattern = color;
145 unsigned lcd_get_foreground(void)
147 return current_vp->fg_pattern;
150 void lcd_set_background(unsigned color)
152 current_vp->bg_pattern = color;
155 unsigned lcd_get_background(void)
157 return current_vp->bg_pattern;
160 void lcd_set_selector_start(unsigned color)
162 current_vp->lss_pattern = color;
165 void lcd_set_selector_end(unsigned color)
167 current_vp->lse_pattern = color;
170 void lcd_set_selector_text(unsigned color)
172 current_vp->lst_pattern = color;
175 void lcd_set_drawinfo(int mode, unsigned fg_color, unsigned bg_color)
177 lcd_set_drawmode(mode);
178 current_vp->fg_pattern = fg_color;
179 current_vp->bg_pattern = bg_color;
182 int lcd_getwidth(void)
184 return current_vp->width;
187 int lcd_getheight(void)
189 return current_vp->height;
192 void lcd_setfont(int newfont)
194 current_vp->font = newfont;
197 int lcd_getfont(void)
199 return current_vp->font;
202 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
204 return font_getstringsize(str, w, h, current_vp->font);
207 /*** low-level drawing functions ***/
209 #define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
211 static void ICODE_ATTR setpixel(fb_data *address)
213 *address = current_vp->fg_pattern;
216 static void ICODE_ATTR clearpixel(fb_data *address)
218 *address = current_vp->bg_pattern;
221 static void ICODE_ATTR clearimgpixel(fb_data *address)
223 *address = *(fb_data *)((long)address + lcd_backdrop_offset);
226 static void ICODE_ATTR flippixel(fb_data *address)
228 *address = ~(*address);
231 static void ICODE_ATTR nopixel(fb_data *address)
233 (void)address;
236 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_bgcolor[8] = {
237 flippixel, nopixel, setpixel, setpixel,
238 nopixel, clearpixel, nopixel, clearpixel
241 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_backdrop[8] = {
242 flippixel, nopixel, setpixel, setpixel,
243 nopixel, clearimgpixel, nopixel, clearimgpixel
246 lcd_fastpixelfunc_type* const * lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
248 void lcd_set_backdrop(fb_data* backdrop)
250 lcd_backdrop = backdrop;
251 if (backdrop)
253 lcd_backdrop_offset = (long)backdrop - (long)&lcd_framebuffer[0][0];
254 lcd_fastpixelfuncs = lcd_fastpixelfuncs_backdrop;
256 else
258 lcd_backdrop_offset = 0;
259 lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
263 fb_data* lcd_get_backdrop(void)
265 return lcd_backdrop;
268 /*** drawing functions ***/
270 /* Clear the current viewport */
271 void lcd_clear_viewport(void)
273 fb_data *dst, *dst_end;
275 dst = LCDADDR(current_vp->x, current_vp->y);
276 dst_end = dst + current_vp->height * LCD_WIDTH;
278 if (current_vp->drawmode & DRMODE_INVERSEVID)
282 memset16(dst, current_vp->fg_pattern, current_vp->width);
283 dst += LCD_WIDTH;
285 while (dst < dst_end);
287 else
289 if (!lcd_backdrop)
293 memset16(dst, current_vp->bg_pattern, current_vp->width);
294 dst += LCD_WIDTH;
296 while (dst < dst_end);
298 else
302 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
303 current_vp->width * sizeof(fb_data));
304 dst += LCD_WIDTH;
306 while (dst < dst_end);
310 if (current_vp == &default_vp)
312 lcd_scroll_info.lines = 0;
314 else
316 lcd_scroll_stop(current_vp);
320 /* Clear the whole display */
321 void lcd_clear_display(void)
323 struct viewport* old_vp = current_vp;
325 current_vp = &default_vp;
327 lcd_clear_viewport();
329 current_vp = old_vp;
332 /* Set a single pixel */
333 void lcd_drawpixel(int x, int y)
335 if (((unsigned)x < (unsigned)current_vp->width) &&
336 ((unsigned)y < (unsigned)current_vp->height))
337 lcd_fastpixelfuncs[current_vp->drawmode](LCDADDR(current_vp->x+x, current_vp->y+y));
340 /* Draw a line */
341 void lcd_drawline(int x1, int y1, int x2, int y2)
343 int numpixels;
344 int i;
345 int deltax, deltay;
346 int d, dinc1, dinc2;
347 int x, xinc1, xinc2;
348 int y, yinc1, yinc2;
349 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
351 deltay = abs(y2 - y1);
352 if (deltay == 0)
354 DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n");
355 lcd_hline(x1, x2, y1);
356 return;
358 deltax = abs(x2 - x1);
359 if (deltax == 0)
361 DEBUGF("lcd_drawline() called for vertical line - optimisation.\n");
362 lcd_vline(x1, y1, y2);
363 return;
365 xinc2 = 1;
366 yinc2 = 1;
368 if (deltax >= deltay)
370 numpixels = deltax;
371 d = 2 * deltay - deltax;
372 dinc1 = deltay * 2;
373 dinc2 = (deltay - deltax) * 2;
374 xinc1 = 1;
375 yinc1 = 0;
377 else
379 numpixels = deltay;
380 d = 2 * deltax - deltay;
381 dinc1 = deltax * 2;
382 dinc2 = (deltax - deltay) * 2;
383 xinc1 = 0;
384 yinc1 = 1;
386 numpixels++; /* include endpoints */
388 if (x1 > x2)
390 xinc1 = -xinc1;
391 xinc2 = -xinc2;
394 if (y1 > y2)
396 yinc1 = -yinc1;
397 yinc2 = -yinc2;
400 x = x1;
401 y = y1;
403 for (i = 0; i < numpixels; i++)
405 if (((unsigned)x < (unsigned)current_vp->width) && ((unsigned)y < (unsigned)current_vp->height))
406 pfunc(LCDADDR(x + current_vp->x, y + current_vp->y));
408 if (d < 0)
410 d += dinc1;
411 x += xinc1;
412 y += yinc1;
414 else
416 d += dinc2;
417 x += xinc2;
418 y += yinc2;
423 /* Draw a horizontal line (optimised) */
424 void lcd_hline(int x1, int x2, int y)
426 int x, width;
427 unsigned bits = 0;
428 enum fill_opt fillopt = OPT_NONE;
429 fb_data *dst, *dst_end;
431 /* direction flip */
432 if (x2 < x1)
434 x = x1;
435 x1 = x2;
436 x2 = x;
439 /* nothing to draw? */
440 if (((unsigned)y >= (unsigned)current_vp->height) ||
441 (x1 >= current_vp->width) ||
442 (x2 < 0))
443 return;
445 /* drawmode and optimisation */
446 if (current_vp->drawmode & DRMODE_INVERSEVID)
448 if (current_vp->drawmode & DRMODE_BG)
450 if (!lcd_backdrop)
452 fillopt = OPT_SET;
453 bits = current_vp->bg_pattern;
455 else
456 fillopt = OPT_COPY;
459 else
461 if (current_vp->drawmode & DRMODE_FG)
463 fillopt = OPT_SET;
464 bits = current_vp->fg_pattern;
467 if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT)
468 return;
470 /* clipping */
471 if (x1 < 0)
472 x1 = 0;
473 if (x2 >= current_vp->width)
474 x2 = current_vp->width-1;
476 width = x2 - x1 + 1;
478 /* Adjust x1 and y to viewport */
479 x1 += current_vp->x;
480 y += current_vp->y;
482 dst = LCDADDR(x1, y);
484 switch (fillopt)
486 case OPT_SET:
487 memset16(dst, bits, width);
488 break;
490 case OPT_COPY:
491 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
492 width * sizeof(fb_data));
493 break;
495 case OPT_NONE: /* DRMODE_COMPLEMENT */
496 dst_end = dst + width;
498 *dst = ~(*dst);
499 while (++dst < dst_end);
500 break;
504 /* Draw a vertical line (optimised) */
505 void lcd_vline(int x, int y1, int y2)
507 int y;
508 fb_data *dst, *dst_end;
509 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
511 /* direction flip */
512 if (y2 < y1)
514 y = y1;
515 y1 = y2;
516 y2 = y;
519 /* nothing to draw? */
520 if (((unsigned)x >= (unsigned)current_vp->width) ||
521 (y1 >= current_vp->height) ||
522 (y2 < 0))
523 return;
525 /* clipping */
526 if (y1 < 0)
527 y1 = 0;
528 if (y2 >= current_vp->height)
529 y2 = current_vp->height-1;
531 dst = LCDADDR(x + current_vp->x, y1 + current_vp->y);
532 dst_end = dst + (y2 - y1) * LCD_WIDTH;
536 pfunc(dst);
537 dst += LCD_WIDTH;
539 while (dst <= dst_end);
542 /* Draw a rectangular box */
543 void lcd_drawrect(int x, int y, int width, int height)
545 if ((width <= 0) || (height <= 0))
546 return;
548 int x2 = x + width - 1;
549 int y2 = y + height - 1;
551 lcd_vline(x, y, y2);
552 lcd_vline(x2, y, y2);
553 lcd_hline(x, x2, y);
554 lcd_hline(x, x2, y2);
557 /* Fill a rectangular area */
558 void lcd_fillrect(int x, int y, int width, int height)
560 unsigned bits = 0;
561 enum fill_opt fillopt = OPT_NONE;
562 fb_data *dst, *dst_end;
564 /* nothing to draw? */
565 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
566 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
567 return;
569 /* drawmode and optimisation */
570 if (current_vp->drawmode & DRMODE_INVERSEVID)
572 if (current_vp->drawmode & DRMODE_BG)
574 if (!lcd_backdrop)
576 fillopt = OPT_SET;
577 bits = current_vp->bg_pattern;
579 else
580 fillopt = OPT_COPY;
583 else
585 if (current_vp->drawmode & DRMODE_FG)
587 fillopt = OPT_SET;
588 bits = current_vp->fg_pattern;
591 if (fillopt == OPT_NONE && current_vp->drawmode != DRMODE_COMPLEMENT)
592 return;
594 /* clipping */
595 if (x < 0)
597 width += x;
598 x = 0;
600 if (y < 0)
602 height += y;
603 y = 0;
605 if (x + width > current_vp->width)
606 width = current_vp->width - x;
607 if (y + height > current_vp->height)
608 height = current_vp->height - y;
610 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
611 dst_end = dst + height * LCD_WIDTH;
615 fb_data *dst_row, *row_end;
617 switch (fillopt)
619 case OPT_SET:
620 memset16(dst, bits, width);
621 break;
623 case OPT_COPY:
624 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
625 width * sizeof(fb_data));
626 break;
628 case OPT_NONE: /* DRMODE_COMPLEMENT */
629 dst_row = dst;
630 row_end = dst_row + width;
632 *dst_row = ~(*dst_row);
633 while (++dst_row < row_end);
634 break;
636 dst += LCD_WIDTH;
638 while (dst < dst_end);
641 /* Fill a rectangle with a gradient */
642 static void lcd_gradient_rect(int x1, int x2, int y, int h)
644 int old_pattern = current_vp->fg_pattern;
646 if (h == 0) return;
648 int h_r = RGB_UNPACK_RED(current_vp->lss_pattern) << 16;
649 int h_b = RGB_UNPACK_BLUE(current_vp->lss_pattern) << 16;
650 int h_g = RGB_UNPACK_GREEN(current_vp->lss_pattern) << 16;
651 int rstep = (h_r - ((signed)RGB_UNPACK_RED(current_vp->lse_pattern) << 16)) / h;
652 int gstep = (h_g - ((signed)RGB_UNPACK_GREEN(current_vp->lse_pattern) << 16)) / h;
653 int bstep = (h_b - ((signed)RGB_UNPACK_BLUE(current_vp->lse_pattern) << 16)) / h;
654 int count;
656 current_vp->fg_pattern = current_vp->lss_pattern;
657 for(count = 0; count < h; count++) {
658 lcd_hline(x1, x2, y + count);
659 h_r -= rstep;
660 h_g -= gstep;
661 h_b -= bstep;
662 current_vp->fg_pattern = LCD_RGBPACK(h_r >> 16, h_g >> 16, h_b >> 16);
665 current_vp->fg_pattern = old_pattern;
668 #define H_COLOR(lss, lse, cur_line, max_line) \
669 (((lse) - (lss)) * (cur_line) / (max_line) + (lss))
671 /* Fill a rectangle with a gradient for scrolling line. To draw a gradient that
672 covers several lines, we need to know how many lines will be covered
673 (the num_lines arg), and which one is the current line within the selection
674 (the cur_line arg). */
675 static void lcd_gradient_rect_scroll(int x1, int x2, int y, int h,
676 unsigned char num_lines, unsigned char cur_line)
678 if (h == 0 || num_lines == 0) return;
680 unsigned tmp_lss = current_vp->lss_pattern;
681 unsigned tmp_lse = current_vp->lse_pattern;
682 int lss_r = (signed)RGB_UNPACK_RED(current_vp->lss_pattern);
683 int lss_b = (signed)RGB_UNPACK_BLUE(current_vp->lss_pattern);
684 int lss_g = (signed)RGB_UNPACK_GREEN(current_vp->lss_pattern);
685 int lse_r = (signed)RGB_UNPACK_RED(current_vp->lse_pattern);
686 int lse_b = (signed)RGB_UNPACK_BLUE(current_vp->lse_pattern);
687 int lse_g = (signed)RGB_UNPACK_GREEN(current_vp->lse_pattern);
689 int h_r = H_COLOR(lss_r, lse_r, cur_line, num_lines);
690 int h_g = H_COLOR(lss_g, lse_g, cur_line, num_lines);
691 int h_b = H_COLOR(lss_b, lse_b, cur_line, num_lines);
692 lcd_set_selector_start(LCD_RGBPACK(h_r, h_g, h_b));
694 int l_r = H_COLOR(lss_r, lse_r, cur_line+1, num_lines);
695 int l_g = H_COLOR(lss_g, lse_g, cur_line+1, num_lines);
696 int l_b = H_COLOR(lss_b, lse_b, cur_line+1, num_lines);
697 lcd_set_selector_end(LCD_RGBPACK(l_r, l_g, l_b));
699 lcd_gradient_rect(x1, x2, y, h);
701 current_vp->lss_pattern = tmp_lss;
702 current_vp->lse_pattern = tmp_lse;
705 /* About Rockbox' internal monochrome bitmap format:
707 * A bitmap contains one bit for every pixel that defines if that pixel is
708 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
709 * at top.
710 * The bytes are stored in row-major order, with byte 0 being top left,
711 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
712 * 0..7, the second row defines pixel row 8..15 etc.
714 * This is the mono bitmap format used on all other targets so far; the
715 * pixel packing doesn't really matter on a 8bit+ target. */
717 /* Draw a partial monochrome bitmap */
719 void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x,
720 int src_y, int stride, int x, int y,
721 int width, int height)
723 const unsigned char *src_end;
724 fb_data *dst, *dst_end;
725 unsigned dmask = 0x100; /* bit 8 == sentinel */
726 int drmode = current_vp->drawmode;
728 /* nothing to draw? */
729 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
730 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
731 return;
733 /* clipping */
734 if (x < 0)
736 width += x;
737 src_x -= x;
738 x = 0;
740 if (y < 0)
742 height += y;
743 src_y -= y;
744 y = 0;
746 if (x + width > current_vp->width)
747 width = current_vp->width - x;
748 if (y + height > current_vp->height)
749 height = current_vp->height - y;
751 src += stride * (src_y >> 3) + src_x; /* move starting point */
752 src_y &= 7;
753 src_end = src + width;
754 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
755 dst_end = dst + height * LCD_WIDTH;
757 if (drmode & DRMODE_INVERSEVID)
759 dmask = 0x1ff; /* bit 8 == sentinel */
760 drmode &= DRMODE_SOLID; /* mask out inversevid */
765 const unsigned char *src_col = src++;
766 unsigned data = (*src_col ^ dmask) >> src_y;
767 fb_data *dst_col = dst++;
768 int fg, bg;
769 long bo;
771 #define UPDATE_SRC do { \
772 data >>= 1; \
773 if (data == 0x001) { \
774 src_col += stride; \
775 data = *src_col ^ dmask; \
777 } while (0)
779 switch (drmode)
781 case DRMODE_COMPLEMENT:
784 if (data & 0x01)
785 *dst_col = ~(*dst_col);
787 dst_col += LCD_WIDTH;
788 UPDATE_SRC;
790 while (dst_col < dst_end);
791 break;
793 case DRMODE_BG:
794 if (lcd_backdrop)
796 bo = lcd_backdrop_offset;
799 if (!(data & 0x01))
800 *dst_col = *(fb_data *)((long)dst_col + bo);
802 dst_col += LCD_WIDTH;
803 UPDATE_SRC;
805 while (dst_col < dst_end);
807 else
809 bg = current_vp->bg_pattern;
812 if (!(data & 0x01))
813 *dst_col = bg;
815 dst_col += LCD_WIDTH;
816 UPDATE_SRC;
818 while (dst_col < dst_end);
820 break;
822 case DRMODE_FG:
823 fg = current_vp->fg_pattern;
826 if (data & 0x01)
827 *dst_col = fg;
829 dst_col += LCD_WIDTH;
830 UPDATE_SRC;
832 while (dst_col < dst_end);
833 break;
835 case DRMODE_SOLID:
836 fg = current_vp->fg_pattern;
837 if (lcd_backdrop)
839 bo = lcd_backdrop_offset;
842 *dst_col = (data & 0x01) ? fg
843 : *(fb_data *)((long)dst_col + bo);
844 dst_col += LCD_WIDTH;
845 UPDATE_SRC;
847 while (dst_col < dst_end);
849 else
851 bg = current_vp->bg_pattern;
854 *dst_col = (data & 0x01) ? fg : bg;
855 dst_col += LCD_WIDTH;
856 UPDATE_SRC;
858 while (dst_col < dst_end);
860 break;
863 while (src < src_end);
865 /* Draw a full monochrome bitmap */
866 void lcd_mono_bitmap(const unsigned char *src, int x, int y, int width, int height)
868 lcd_mono_bitmap_part(src, 0, 0, width, x, y, width, height);
871 /* Draw a partial native bitmap */
872 void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y,
873 int stride, int x, int y, int width,
874 int height)
876 fb_data *dst, *dst_end;
878 /* nothing to draw? */
879 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
880 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
881 return;
883 /* clipping */
884 if (x < 0)
886 width += x;
887 src_x -= x;
888 x = 0;
890 if (y < 0)
892 height += y;
893 src_y -= y;
894 y = 0;
896 if (x + width > current_vp->width)
897 width = current_vp->width - x;
898 if (y + height > current_vp->height)
899 height = current_vp->height - y;
901 src += stride * src_y + src_x; /* move starting point */
902 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
903 dst_end = dst + height * LCD_WIDTH;
907 memcpy(dst, src, width * sizeof(fb_data));
908 src += stride;
909 dst += LCD_WIDTH;
911 while (dst < dst_end);
914 /* Draw a full native bitmap */
915 void lcd_bitmap(const fb_data *src, int x, int y, int width, int height)
917 lcd_bitmap_part(src, 0, 0, width, x, y, width, height);
920 #if !defined(TOSHIBA_GIGABEAT_F) && !defined(TOSHIBA_GIGABEAT_S) \
921 || defined(SIMULATOR)
922 /* Draw a partial native bitmap */
923 void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x,
924 int src_y, int stride, int x,
925 int y, int width, int height)
927 fb_data *dst, *dst_end;
929 /* nothing to draw? */
930 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
931 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
932 return;
934 /* clipping */
935 if (x < 0)
937 width += x;
938 src_x -= x;
939 x = 0;
941 if (y < 0)
943 height += y;
944 src_y -= y;
945 y = 0;
947 if (x + width > current_vp->width)
948 width = current_vp->width - x;
949 if (y + height > current_vp->height)
950 height = current_vp->height - y;
952 src += stride * src_y + src_x; /* move starting point */
953 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
954 dst_end = dst + height * LCD_WIDTH;
958 int i;
959 for(i = 0;i < width;i++)
961 if (src[i] == REPLACEWITHFG_COLOR)
962 dst[i] = current_vp->fg_pattern;
963 else if(src[i] != TRANSPARENT_COLOR)
964 dst[i] = src[i];
966 src += stride;
967 dst += LCD_WIDTH;
969 while (dst < dst_end);
971 #endif /* !defined(TOSHIBA_GIGABEAT_F) || defined(SIMULATOR) */
973 /* Draw a full native bitmap with a transparent color */
974 void lcd_bitmap_transparent(const fb_data *src, int x, int y,
975 int width, int height)
977 lcd_bitmap_transparent_part(src, 0, 0, width, x, y, width, height);
980 /* put a string at a given pixel position, skipping first ofs pixel columns */
981 static void lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
983 unsigned short ch;
984 unsigned short *ucs;
985 struct font* pf = font_get(current_vp->font);
987 ucs = bidi_l2v(str, 1);
989 while ((ch = *ucs++) != 0 && x < current_vp->width)
991 int width;
992 const unsigned char *bits;
994 /* get proportional width and glyph bits */
995 width = font_get_width(pf,ch);
997 if (ofs > width)
999 ofs -= width;
1000 continue;
1003 bits = font_get_bits(pf, ch);
1005 lcd_mono_bitmap_part(bits, ofs, 0, width, x, y, width - ofs, pf->height);
1007 x += width - ofs;
1008 ofs = 0;
1012 /* put a string at a given pixel position */
1013 void lcd_putsxy(int x, int y, const unsigned char *str)
1015 lcd_putsxyofs(x, y, 0, str);
1018 /*** line oriented text output ***/
1020 /* put a string at a given char position */
1021 void lcd_puts(int x, int y, const unsigned char *str)
1023 lcd_puts_style_offset(x, y, str, STYLE_DEFAULT, 0);
1026 void lcd_puts_style(int x, int y, const unsigned char *str, int style)
1028 lcd_puts_style_offset(x, y, str, style, 0);
1031 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
1033 lcd_puts_style_offset(x, y, str, STYLE_DEFAULT, offset);
1036 /* put a string at a given char position, style, and pixel position,
1037 * skipping first offset pixel columns */
1038 void lcd_puts_style_offset(int x, int y, const unsigned char *str, int style,
1039 int offset)
1041 int xpos,ypos,w,h,xrect;
1042 int lastmode = current_vp->drawmode;
1043 int oldfgcolor = current_vp->fg_pattern;
1044 int oldbgcolor = current_vp->bg_pattern;
1046 /* make sure scrolling is turned off on the line we are updating */
1047 lcd_scroll_stop_line(current_vp, y);
1049 if(!str || !str[0])
1050 return;
1052 lcd_getstringsize(str, &w, &h);
1053 xpos = x*w / utf8length(str);
1054 ypos = y*h;
1055 current_vp->drawmode = (style & STYLE_INVERT) ?
1056 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1057 if (style & STYLE_COLORED) {
1058 if (current_vp->drawmode == DRMODE_SOLID)
1059 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
1060 else
1061 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
1063 current_vp->drawmode ^= DRMODE_INVERSEVID;
1064 xrect = xpos + MAX(w - offset, 0);
1066 if (style & STYLE_GRADIENT) {
1067 current_vp->drawmode = DRMODE_FG;
1068 if (CURLN_UNPACK(style) == 0)
1069 lcd_gradient_rect(xpos, current_vp->width, ypos, h*NUMLN_UNPACK(style));
1070 current_vp->fg_pattern = current_vp->lst_pattern;
1072 else if (style & STYLE_COLORBAR) {
1073 current_vp->drawmode = DRMODE_FG;
1074 current_vp->fg_pattern = current_vp->lss_pattern;
1075 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
1076 current_vp->fg_pattern = current_vp->lst_pattern;
1078 else {
1079 lcd_fillrect(xrect, ypos, current_vp->width - xrect, h);
1080 current_vp->drawmode = (style & STYLE_INVERT) ?
1081 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1083 lcd_putsxyofs(xpos, ypos, offset, str);
1084 current_vp->drawmode = lastmode;
1085 current_vp->fg_pattern = oldfgcolor;
1086 current_vp->bg_pattern = oldbgcolor;
1089 /*** scrolling ***/
1090 void lcd_puts_scroll(int x, int y, const unsigned char *string)
1092 lcd_puts_scroll_style(x, y, string, STYLE_DEFAULT);
1095 void lcd_puts_scroll_style(int x, int y, const unsigned char *string, int style)
1097 lcd_puts_scroll_style_offset(x, y, string, style, 0);
1100 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string, int offset)
1102 lcd_puts_scroll_style_offset(x, y, string, STYLE_DEFAULT, offset);
1105 /* Initialise a scrolling line at (x,y) in current viewport */
1107 void lcd_puts_scroll_style_offset(int x, int y, const unsigned char *string,
1108 int style, int offset)
1110 struct scrollinfo* s;
1111 int w, h;
1113 if ((unsigned)y >= (unsigned)current_vp->height)
1114 return;
1116 /* remove any previously scrolling line at the same location */
1117 lcd_scroll_stop_line(current_vp, y);
1119 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
1121 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
1123 s->start_tick = current_tick + lcd_scroll_info.delay;
1124 s->style = style;
1125 lcd_puts_style_offset(x,y,string,style,offset);
1127 lcd_getstringsize(string, &w, &h);
1129 if (current_vp->width - x * 8 < w) {
1130 /* prepare scroll line */
1131 char *end;
1133 memset(s->line, 0, sizeof s->line);
1134 strcpy(s->line, string);
1136 /* get width */
1137 s->width = lcd_getstringsize(s->line, &w, &h);
1139 /* scroll bidirectional or forward only depending on the string
1140 width */
1141 if ( lcd_scroll_info.bidir_limit ) {
1142 s->bidir = s->width < (current_vp->width) *
1143 (100 + lcd_scroll_info.bidir_limit) / 100;
1145 else
1146 s->bidir = false;
1148 if (!s->bidir) { /* add spaces if scrolling in the round */
1149 strcat(s->line, " ");
1150 /* get new width incl. spaces */
1151 s->width = lcd_getstringsize(s->line, &w, &h);
1154 end = strchr(s->line, '\0');
1155 strlcpy(end, string, current_vp->width/2);
1157 s->vp = current_vp;
1158 s->y = y;
1159 s->len = utf8length(string);
1160 s->offset = offset;
1161 s->startx = x * s->width / s->len;
1162 s->backward = false;
1163 lcd_scroll_info.lines++;
1167 void lcd_scroll_fn(void)
1169 struct font* pf;
1170 struct scrollinfo* s;
1171 int index;
1172 int xpos, ypos;
1173 int lastmode;
1174 unsigned old_fgcolor;
1175 unsigned old_bgcolor;
1176 struct viewport* old_vp = current_vp;
1178 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
1179 s = &lcd_scroll_info.scroll[index];
1181 /* check pause */
1182 if (TIME_BEFORE(current_tick, s->start_tick))
1183 continue;
1185 lcd_set_viewport(s->vp);
1186 old_fgcolor = current_vp->fg_pattern;
1187 old_bgcolor = current_vp->bg_pattern;
1189 if (s->style&STYLE_COLORED) {
1190 if (s->style&STYLE_MODE_MASK) {
1191 current_vp->fg_pattern = old_fgcolor;
1192 current_vp->bg_pattern = s->style&STYLE_COLOR_MASK;
1194 else {
1195 current_vp->fg_pattern = s->style&STYLE_COLOR_MASK;
1196 current_vp->bg_pattern = old_bgcolor;
1200 if (s->backward)
1201 s->offset -= lcd_scroll_info.step;
1202 else
1203 s->offset += lcd_scroll_info.step;
1205 pf = font_get(current_vp->font);
1206 xpos = s->startx;
1207 ypos = s->y * pf->height;
1209 if (s->bidir) { /* scroll bidirectional */
1210 if (s->offset <= 0) {
1211 /* at beginning of line */
1212 s->offset = 0;
1213 s->backward = false;
1214 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
1216 if (s->offset >= s->width - (current_vp->width - xpos)) {
1217 /* at end of line */
1218 s->offset = s->width - (current_vp->width - xpos);
1219 s->backward = true;
1220 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
1223 else {
1224 /* scroll forward the whole time */
1225 if (s->offset >= s->width)
1226 s->offset %= s->width;
1229 lastmode = current_vp->drawmode;
1230 switch (s->style&STYLE_MODE_MASK) {
1231 case STYLE_INVERT:
1232 current_vp->drawmode = DRMODE_SOLID|DRMODE_INVERSEVID;
1233 break;
1234 case STYLE_COLORBAR:
1235 /* Solid colour line selector */
1236 current_vp->drawmode = DRMODE_FG;
1237 current_vp->fg_pattern = current_vp->lss_pattern;
1238 lcd_fillrect(xpos, ypos, current_vp->width - xpos, pf->height);
1239 current_vp->fg_pattern = current_vp->lst_pattern;
1240 break;
1241 case STYLE_GRADIENT:
1242 /* Gradient line selector */
1243 current_vp->drawmode = DRMODE_FG;
1244 lcd_gradient_rect_scroll(xpos, current_vp->width, ypos, (signed)pf->height,
1245 NUMLN_UNPACK(s->style),
1246 CURLN_UNPACK(s->style));
1247 current_vp->fg_pattern = current_vp->lst_pattern;
1248 break;
1249 default:
1250 current_vp->drawmode = DRMODE_SOLID;
1251 break;
1253 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
1254 current_vp->drawmode = lastmode;
1255 current_vp->fg_pattern = old_fgcolor;
1256 current_vp->bg_pattern = old_bgcolor;
1257 lcd_update_viewport_rect(xpos, ypos, current_vp->width - xpos, pf->height);
1260 lcd_set_viewport(old_vp);