a bit of code cleanup.. use a single function to get the statusbar height (or lack...
[Rockbox.git] / firmware / drivers / lcd-16bit.c
blobc91d22283021171d3fd03d8f99d77bf69007a786
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 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #include "cpu.h"
24 #include "lcd.h"
25 #include "kernel.h"
26 #include "thread.h"
27 #include <string.h>
28 #include <stdlib.h>
29 #include "memory.h"
30 #include "file.h"
31 #include "debug.h"
32 #include "system.h"
33 #include "font.h"
34 #include "rbunicode.h"
35 #include "bidi.h"
36 #include "scroll_engine.h"
38 enum fill_opt {
39 OPT_NONE = 0,
40 OPT_SET,
41 OPT_COPY
44 /*** globals ***/
45 fb_data lcd_framebuffer[LCD_FBHEIGHT][LCD_FBWIDTH]
46 IRAM_LCDFRAMEBUFFER CACHEALIGN_AT_LEAST_ATTR(16);
49 static fb_data* lcd_backdrop = NULL;
50 static long lcd_backdrop_offset IDATA_ATTR = 0;
52 static struct viewport default_vp =
54 .x = 0,
55 .y = 0,
56 .width = LCD_WIDTH,
57 .height = LCD_HEIGHT,
58 .font = FONT_SYSFIXED,
59 .drawmode = DRMODE_SOLID,
60 .xmargin = 0,
61 .ymargin = 0,
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
77 /* LCD init */
78 void lcd_init(void)
80 lcd_clear_display();
82 /* Call device specific init */
83 lcd_init_device();
84 scroll_init();
87 /*** Viewports ***/
89 void lcd_set_viewport(struct viewport* vp)
91 if (vp == NULL)
92 current_vp = &default_vp;
93 else
94 current_vp = vp;
97 void lcd_update_viewport(void)
99 lcd_update_rect(current_vp->x, current_vp->y,
100 current_vp->width, current_vp->height);
103 void lcd_update_viewport_rect(int x, int y, int width, int height)
105 lcd_update_rect(current_vp->x + x, current_vp->y + y, width, height);
108 /*** parameter handling ***/
110 void lcd_set_drawmode(int mode)
112 current_vp->drawmode = mode & (DRMODE_SOLID|DRMODE_INVERSEVID);
115 int lcd_get_drawmode(void)
117 return current_vp->drawmode;
120 void lcd_set_foreground(unsigned color)
122 current_vp->fg_pattern = color;
125 unsigned lcd_get_foreground(void)
127 return current_vp->fg_pattern;
130 void lcd_set_background(unsigned color)
132 current_vp->bg_pattern = color;
135 unsigned lcd_get_background(void)
137 return current_vp->bg_pattern;
140 void lcd_set_selector_start(unsigned color)
142 current_vp->lss_pattern = color;
145 void lcd_set_selector_end(unsigned color)
147 current_vp->lse_pattern = color;
150 void lcd_set_selector_text(unsigned color)
152 current_vp->lst_pattern = color;
155 void lcd_set_drawinfo(int mode, unsigned fg_color, unsigned bg_color)
157 lcd_set_drawmode(mode);
158 current_vp->fg_pattern = fg_color;
159 current_vp->bg_pattern = bg_color;
162 void lcd_setmargins(int x, int y)
164 current_vp->xmargin = x;
165 current_vp->ymargin = y;
168 int lcd_getwidth(void)
170 return current_vp->width;
173 int lcd_getheight(void)
175 return current_vp->height;
178 int lcd_getxmargin(void)
180 return current_vp->xmargin;
183 int lcd_getymargin(void)
185 return current_vp->ymargin;
188 void lcd_setfont(int newfont)
190 current_vp->font = newfont;
193 int lcd_getfont(void)
195 return current_vp->font;
198 int lcd_getstringsize(const unsigned char *str, int *w, int *h)
200 return font_getstringsize(str, w, h, current_vp->font);
203 /*** low-level drawing functions ***/
205 #define LCDADDR(x, y) (&lcd_framebuffer[(y)][(x)])
207 static void ICODE_ATTR setpixel(fb_data *address)
209 *address = current_vp->fg_pattern;
212 static void ICODE_ATTR clearpixel(fb_data *address)
214 *address = current_vp->bg_pattern;
217 static void ICODE_ATTR clearimgpixel(fb_data *address)
219 *address = *(fb_data *)((long)address + lcd_backdrop_offset);
222 static void ICODE_ATTR flippixel(fb_data *address)
224 *address = ~(*address);
227 static void ICODE_ATTR nopixel(fb_data *address)
229 (void)address;
232 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_bgcolor[8] = {
233 flippixel, nopixel, setpixel, setpixel,
234 nopixel, clearpixel, nopixel, clearpixel
237 lcd_fastpixelfunc_type* const lcd_fastpixelfuncs_backdrop[8] = {
238 flippixel, nopixel, setpixel, setpixel,
239 nopixel, clearimgpixel, nopixel, clearimgpixel
242 lcd_fastpixelfunc_type* const * lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
244 void lcd_set_backdrop(fb_data* backdrop)
246 lcd_backdrop = backdrop;
247 if (backdrop)
249 lcd_backdrop_offset = (long)backdrop - (long)&lcd_framebuffer[0][0];
250 lcd_fastpixelfuncs = lcd_fastpixelfuncs_backdrop;
252 else
254 lcd_backdrop_offset = 0;
255 lcd_fastpixelfuncs = lcd_fastpixelfuncs_bgcolor;
259 fb_data* lcd_get_backdrop(void)
261 return lcd_backdrop;
264 /*** drawing functions ***/
266 /* Clear the current viewport */
267 void lcd_clear_viewport(void)
269 fb_data *dst, *dst_end;
271 dst = LCDADDR(current_vp->x, current_vp->y);
272 dst_end = dst + current_vp->height * LCD_WIDTH;
274 if (current_vp->drawmode & DRMODE_INVERSEVID)
278 memset16(dst, current_vp->fg_pattern, current_vp->width);
279 dst += LCD_WIDTH;
281 while (dst < dst_end);
283 else
285 if (!lcd_backdrop)
289 memset16(dst, current_vp->bg_pattern, current_vp->width);
290 dst += LCD_WIDTH;
292 while (dst < dst_end);
294 else
298 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
299 current_vp->width * sizeof(fb_data));
300 dst += LCD_WIDTH;
302 while (dst < dst_end);
306 if (current_vp == &default_vp)
308 lcd_scroll_info.lines = 0;
310 else
312 lcd_scroll_stop(current_vp);
316 /* Clear the whole display */
317 void lcd_clear_display(void)
319 struct viewport* old_vp = current_vp;
321 current_vp = &default_vp;
323 lcd_clear_viewport();
325 current_vp = old_vp;
328 /* Set a single pixel */
329 void lcd_drawpixel(int x, int y)
331 if (((unsigned)x < (unsigned)current_vp->width) &&
332 ((unsigned)y < (unsigned)current_vp->height))
333 lcd_fastpixelfuncs[current_vp->drawmode](LCDADDR(current_vp->x+x, current_vp->y+y));
336 /* Draw a line */
337 void lcd_drawline(int x1, int y1, int x2, int y2)
339 int numpixels;
340 int i;
341 int deltax, deltay;
342 int d, dinc1, dinc2;
343 int x, xinc1, xinc2;
344 int y, yinc1, yinc2;
345 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
347 deltay = abs(y2 - y1);
348 if (deltay == 0)
350 DEBUGF("lcd_drawline() called for horizontal line - optimisation.\n");
351 lcd_hline(x1, x2, y1);
352 return;
354 deltax = abs(x2 - x1);
355 if (deltax == 0)
357 DEBUGF("lcd_drawline() called for vertical line - optimisation.\n");
358 lcd_vline(x1, y1, y2);
359 return;
361 xinc2 = 1;
362 yinc2 = 1;
364 if (deltax >= deltay)
366 numpixels = deltax;
367 d = 2 * deltay - deltax;
368 dinc1 = deltay * 2;
369 dinc2 = (deltay - deltax) * 2;
370 xinc1 = 1;
371 yinc1 = 0;
373 else
375 numpixels = deltay;
376 d = 2 * deltax - deltay;
377 dinc1 = deltax * 2;
378 dinc2 = (deltax - deltay) * 2;
379 xinc1 = 0;
380 yinc1 = 1;
382 numpixels++; /* include endpoints */
384 if (x1 > x2)
386 xinc1 = -xinc1;
387 xinc2 = -xinc2;
390 if (y1 > y2)
392 yinc1 = -yinc1;
393 yinc2 = -yinc2;
396 x = x1;
397 y = y1;
399 for (i = 0; i < numpixels; i++)
401 if (((unsigned)x < (unsigned)current_vp->width) && ((unsigned)y < (unsigned)current_vp->height))
402 pfunc(LCDADDR(x + current_vp->x, y + current_vp->y));
404 if (d < 0)
406 d += dinc1;
407 x += xinc1;
408 y += yinc1;
410 else
412 d += dinc2;
413 x += xinc2;
414 y += yinc2;
419 /* Draw a horizontal line (optimised) */
420 void lcd_hline(int x1, int x2, int y)
422 int x, width;
423 unsigned bits = 0;
424 enum fill_opt fillopt = OPT_NONE;
425 fb_data *dst, *dst_end;
426 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
428 /* direction flip */
429 if (x2 < x1)
431 x = x1;
432 x1 = x2;
433 x2 = x;
436 /* nothing to draw? */
437 if (((unsigned)y >= (unsigned)current_vp->height) ||
438 (x1 >= current_vp->width) ||
439 (x2 < 0))
440 return;
442 /* clipping */
443 if (x1 < 0)
444 x1 = 0;
445 if (x2 >= current_vp->width)
446 x2 = current_vp->width-1;
448 width = x2 - x1 + 1;
450 /* Adjust x1 and y to viewport */
451 x1 += current_vp->x;
452 y += current_vp->y;
454 if (current_vp->drawmode & DRMODE_INVERSEVID)
456 if (current_vp->drawmode & DRMODE_BG)
458 if (!lcd_backdrop)
460 fillopt = OPT_SET;
461 bits = current_vp->bg_pattern;
463 else
464 fillopt = OPT_COPY;
467 else
469 if (current_vp->drawmode & DRMODE_FG)
471 fillopt = OPT_SET;
472 bits = current_vp->fg_pattern;
475 dst = LCDADDR(x1, y);
477 switch (fillopt)
479 case OPT_SET:
480 memset16(dst, bits, width);
481 break;
483 case OPT_COPY:
484 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
485 width * sizeof(fb_data));
486 break;
488 case OPT_NONE:
489 dst_end = dst + width;
491 pfunc(dst++);
492 while (dst < dst_end);
493 break;
497 /* Draw a vertical line (optimised) */
498 void lcd_vline(int x, int y1, int y2)
500 int y;
501 fb_data *dst, *dst_end;
502 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
504 /* direction flip */
505 if (y2 < y1)
507 y = y1;
508 y1 = y2;
509 y2 = y;
512 /* nothing to draw? */
513 if ((x >= current_vp->width) ||
514 (y1 >= current_vp->height) ||
515 (y2 < 0))
516 return;
518 /* clipping */
519 if (y1 < 0)
520 y1 = 0;
521 if (y2 >= current_vp->height)
522 y2 = current_vp->height-1;
524 dst = LCDADDR(x + current_vp->x, y1 + current_vp->y);
525 dst_end = dst + (y2 - y1) * LCD_WIDTH;
529 pfunc(dst);
530 dst += LCD_WIDTH;
532 while (dst <= dst_end);
535 /* Draw a rectangular box */
536 void lcd_drawrect(int x, int y, int width, int height)
538 if ((width <= 0) || (height <= 0))
539 return;
541 int x2 = x + width - 1;
542 int y2 = y + height - 1;
544 lcd_vline(x, y, y2);
545 lcd_vline(x2, y, y2);
546 lcd_hline(x, x2, y);
547 lcd_hline(x, x2, y2);
550 /* Fill a rectangular area */
551 void lcd_fillrect(int x, int y, int width, int height)
553 unsigned bits = 0;
554 enum fill_opt fillopt = OPT_NONE;
555 fb_data *dst, *dst_end;
556 lcd_fastpixelfunc_type *pfunc = lcd_fastpixelfuncs[current_vp->drawmode];
558 /* nothing to draw? */
559 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
560 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
561 return;
563 /* clipping */
564 if (x < 0)
566 width += x;
567 x = 0;
569 if (y < 0)
571 height += y;
572 y = 0;
574 if (x + width > current_vp->width)
575 width = current_vp->width - x;
576 if (y + height > current_vp->height)
577 height = current_vp->height - y;
579 if (current_vp->drawmode & DRMODE_INVERSEVID)
581 if (current_vp->drawmode & DRMODE_BG)
583 if (!lcd_backdrop)
585 fillopt = OPT_SET;
586 bits = current_vp->bg_pattern;
588 else
589 fillopt = OPT_COPY;
592 else
594 if (current_vp->drawmode & DRMODE_FG)
596 fillopt = OPT_SET;
597 bits = current_vp->fg_pattern;
600 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
601 dst_end = dst + height * LCD_WIDTH;
605 fb_data *dst_row, *row_end;
607 switch (fillopt)
609 case OPT_SET:
610 memset16(dst, bits, width);
611 break;
613 case OPT_COPY:
614 memcpy(dst, (void *)((long)dst + lcd_backdrop_offset),
615 width * sizeof(fb_data));
616 break;
618 case OPT_NONE:
619 dst_row = dst;
620 row_end = dst_row + width;
622 pfunc(dst_row++);
623 while (dst_row < row_end);
624 break;
626 dst += LCD_WIDTH;
628 while (dst < dst_end);
631 /* Fill a rectangle with a gradient */
632 static void lcd_gradient_rect(int x1, int x2, int y, int h)
634 int old_pattern = current_vp->fg_pattern;
636 if (h == 0) return;
638 int h_r = RGB_UNPACK_RED(current_vp->lss_pattern) << 16;
639 int h_b = RGB_UNPACK_BLUE(current_vp->lss_pattern) << 16;
640 int h_g = RGB_UNPACK_GREEN(current_vp->lss_pattern) << 16;
641 int rstep = (h_r - ((signed)RGB_UNPACK_RED(current_vp->lse_pattern) << 16)) / h;
642 int gstep = (h_g - ((signed)RGB_UNPACK_GREEN(current_vp->lse_pattern) << 16)) / h;
643 int bstep = (h_b - ((signed)RGB_UNPACK_BLUE(current_vp->lse_pattern) << 16)) / h;
644 int count;
646 current_vp->fg_pattern = current_vp->lss_pattern;
647 for(count = 0; count < h; count++) {
648 lcd_hline(x1, x2, y + count);
649 h_r -= rstep;
650 h_g -= gstep;
651 h_b -= bstep;
652 current_vp->fg_pattern = LCD_RGBPACK(h_r >> 16, h_g >> 16, h_b >> 16);
655 current_vp->fg_pattern = old_pattern;
658 #define H_COLOR(lss, lse, cur_line, max_line) \
659 (((lse) - (lss)) * (cur_line) / (max_line) + (lss))
661 /* Fill a rectangle with a gradient for scrolling line. To draw a gradient that
662 covers several lines, we need to know how many lines will be covered
663 (the num_lines arg), and which one is the current line within the selection
664 (the cur_line arg). */
665 static void lcd_gradient_rect_scroll(int x1, int x2, int y, int h,
666 unsigned char num_lines, unsigned char cur_line)
668 if (h == 0 || num_lines == 0) return;
670 unsigned tmp_lss = current_vp->lss_pattern;
671 unsigned tmp_lse = current_vp->lse_pattern;
672 int lss_r = (signed)RGB_UNPACK_RED(current_vp->lss_pattern);
673 int lss_b = (signed)RGB_UNPACK_BLUE(current_vp->lss_pattern);
674 int lss_g = (signed)RGB_UNPACK_GREEN(current_vp->lss_pattern);
675 int lse_r = (signed)RGB_UNPACK_RED(current_vp->lse_pattern);
676 int lse_b = (signed)RGB_UNPACK_BLUE(current_vp->lse_pattern);
677 int lse_g = (signed)RGB_UNPACK_GREEN(current_vp->lse_pattern);
679 int h_r = H_COLOR(lss_r, lse_r, cur_line, num_lines);
680 int h_g = H_COLOR(lss_g, lse_g, cur_line, num_lines);
681 int h_b = H_COLOR(lss_b, lse_b, cur_line, num_lines);
682 lcd_set_selector_start(LCD_RGBPACK(h_r, h_g, h_b));
684 int l_r = H_COLOR(lss_r, lse_r, cur_line+1, num_lines);
685 int l_g = H_COLOR(lss_g, lse_g, cur_line+1, num_lines);
686 int l_b = H_COLOR(lss_b, lse_b, cur_line+1, num_lines);
687 lcd_set_selector_end(LCD_RGBPACK(l_r, l_g, l_b));
689 lcd_gradient_rect(x1, x2, y, h);
691 current_vp->lss_pattern = tmp_lss;
692 current_vp->lse_pattern = tmp_lse;
695 /* About Rockbox' internal monochrome bitmap format:
697 * A bitmap contains one bit for every pixel that defines if that pixel is
698 * black (1) or white (0). Bits within a byte are arranged vertically, LSB
699 * at top.
700 * The bytes are stored in row-major order, with byte 0 being top left,
701 * byte 1 2nd from left etc. The first row of bytes defines pixel rows
702 * 0..7, the second row defines pixel row 8..15 etc.
704 * This is the mono bitmap format used on all other targets so far; the
705 * pixel packing doesn't really matter on a 8bit+ target. */
707 /* Draw a partial monochrome bitmap */
709 void ICODE_ATTR lcd_mono_bitmap_part(const unsigned char *src, int src_x,
710 int src_y, int stride, int x, int y,
711 int width, int height)
713 const unsigned char *src_end;
714 bool has_backdrop;
715 fb_data *dst, *dst_end, *backdrop;
716 lcd_fastpixelfunc_type *fgfunc, *bgfunc;
718 /* nothing to draw? */
719 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
720 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
721 return;
723 /* clipping */
724 if (x < 0)
726 width += x;
727 src_x -= x;
728 x = 0;
730 if (y < 0)
732 height += y;
733 src_y -= y;
734 y = 0;
736 if (x + width > current_vp->width)
737 width = current_vp->width - x;
738 if (y + height > current_vp->height)
739 height = current_vp->height - y;
741 src += stride * (src_y >> 3) + src_x; /* move starting point */
742 src_y &= 7;
743 src_end = src + width;
745 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
746 has_backdrop = (lcd_backdrop != NULL);
747 backdrop = lcd_backdrop + (current_vp->y + y) * LCD_WIDTH + current_vp->x + x;
748 fgfunc = lcd_fastpixelfuncs[current_vp->drawmode];
749 bgfunc = lcd_fastpixelfuncs[current_vp->drawmode ^ DRMODE_INVERSEVID];
752 const unsigned char *src_col = src++;
753 unsigned data = *src_col >> src_y;
754 fb_data *dst_col = dst++;
755 int numbits = 8 - src_y;
756 fb_data *backdrop_col = backdrop++;
757 dst_end = dst_col + height * LCD_WIDTH;
760 switch (current_vp->drawmode)
762 case DRMODE_SOLID:
763 if (data & 0x01)
764 *dst_col = current_vp->fg_pattern;
765 else
766 *dst_col = has_backdrop ? *backdrop_col : current_vp->bg_pattern;
767 break;
768 case DRMODE_FG:
769 if (data & 0x01)
770 *dst_col = current_vp->fg_pattern;
771 break;
772 case (DRMODE_SOLID|DRMODE_INVERSEVID):
773 if (data & 0x01)
774 *dst_col = has_backdrop ? *backdrop_col : current_vp->bg_pattern;
775 else
776 *dst_col = current_vp->fg_pattern;
777 break;
778 default:
779 if (data & 0x01)
780 fgfunc(dst_col);
781 else
782 bgfunc(dst_col);
783 break;
785 dst_col += LCD_WIDTH;
786 backdrop_col += LCD_WIDTH;
787 data >>= 1;
788 if (--numbits == 0)
790 src_col += stride;
791 data = *src_col;
792 numbits = 8;
795 while (dst_col < dst_end);
797 while (src < src_end);
799 /* Draw a full monochrome bitmap */
800 void lcd_mono_bitmap(const unsigned char *src, int x, int y, int width, int height)
802 lcd_mono_bitmap_part(src, 0, 0, width, x, y, width, height);
805 /* Draw a partial native bitmap */
806 void ICODE_ATTR lcd_bitmap_part(const fb_data *src, int src_x, int src_y,
807 int stride, int x, int y, int width,
808 int height)
810 fb_data *dst, *dst_end;
812 /* nothing to draw? */
813 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
814 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
815 return;
817 /* clipping */
818 if (x < 0)
820 width += x;
821 src_x -= x;
822 x = 0;
824 if (y < 0)
826 height += y;
827 src_y -= y;
828 y = 0;
830 if (x + width > current_vp->width)
831 width = current_vp->width - x;
832 if (y + height > current_vp->height)
833 height = current_vp->height - y;
835 src += stride * src_y + src_x; /* move starting point */
836 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
837 dst_end = dst + height * LCD_WIDTH;
841 memcpy(dst, src, width * sizeof(fb_data));
842 src += stride;
843 dst += LCD_WIDTH;
845 while (dst < dst_end);
848 /* Draw a full native bitmap */
849 void lcd_bitmap(const fb_data *src, int x, int y, int width, int height)
851 lcd_bitmap_part(src, 0, 0, width, x, y, width, height);
854 #if !defined(TOSHIBA_GIGABEAT_F) && !defined(TOSHIBA_GIGABEAT_S) \
855 || defined(SIMULATOR)
856 /* Draw a partial native bitmap */
857 void ICODE_ATTR lcd_bitmap_transparent_part(const fb_data *src, int src_x,
858 int src_y, int stride, int x,
859 int y, int width, int height)
861 fb_data *dst, *dst_end;
863 /* nothing to draw? */
864 if ((width <= 0) || (height <= 0) || (x >= current_vp->width) ||
865 (y >= current_vp->height) || (x + width <= 0) || (y + height <= 0))
866 return;
868 /* clipping */
869 if (x < 0)
871 width += x;
872 src_x -= x;
873 x = 0;
875 if (y < 0)
877 height += y;
878 src_y -= y;
879 y = 0;
881 if (x + width > current_vp->width)
882 width = current_vp->width - x;
883 if (y + height > current_vp->height)
884 height = current_vp->height - y;
886 src += stride * src_y + src_x; /* move starting point */
887 dst = LCDADDR(current_vp->x + x, current_vp->y + y);
888 dst_end = dst + height * LCD_WIDTH;
892 int i;
893 for(i = 0;i < width;i++)
895 if (src[i] == REPLACEWITHFG_COLOR)
896 dst[i] = current_vp->fg_pattern;
897 else if(src[i] != TRANSPARENT_COLOR)
898 dst[i] = src[i];
900 src += stride;
901 dst += LCD_WIDTH;
903 while (dst < dst_end);
905 #endif /* !defined(TOSHIBA_GIGABEAT_F) || defined(SIMULATOR) */
907 /* Draw a full native bitmap with a transparent color */
908 void lcd_bitmap_transparent(const fb_data *src, int x, int y,
909 int width, int height)
911 lcd_bitmap_transparent_part(src, 0, 0, width, x, y, width, height);
914 /* put a string at a given pixel position, skipping first ofs pixel columns */
915 static void lcd_putsxyofs(int x, int y, int ofs, const unsigned char *str)
917 unsigned short ch;
918 unsigned short *ucs;
919 struct font* pf = font_get(current_vp->font);
921 ucs = bidi_l2v(str, 1);
923 while ((ch = *ucs++) != 0 && x < current_vp->width)
925 int width;
926 const unsigned char *bits;
928 /* get proportional width and glyph bits */
929 width = font_get_width(pf,ch);
931 if (ofs > width)
933 ofs -= width;
934 continue;
937 bits = font_get_bits(pf, ch);
939 lcd_mono_bitmap_part(bits, ofs, 0, width, x, y, width - ofs, pf->height);
941 x += width - ofs;
942 ofs = 0;
946 /* put a string at a given pixel position */
947 void lcd_putsxy(int x, int y, const unsigned char *str)
949 lcd_putsxyofs(x, y, 0, str);
952 /*** line oriented text output ***/
954 /* put a string at a given char position */
955 void lcd_puts(int x, int y, const unsigned char *str)
957 lcd_puts_style_offset(x, y, str, STYLE_DEFAULT, 0);
960 void lcd_puts_style(int x, int y, const unsigned char *str, int style)
962 lcd_puts_style_offset(x, y, str, style, 0);
965 void lcd_puts_offset(int x, int y, const unsigned char *str, int offset)
967 lcd_puts_style_offset(x, y, str, STYLE_DEFAULT, offset);
970 /* put a string at a given char position, style, and pixel position,
971 * skipping first offset pixel columns */
972 void lcd_puts_style_offset(int x, int y, const unsigned char *str, int style,
973 int offset)
975 int xpos,ypos,w,h,xrect;
976 int lastmode = current_vp->drawmode;
977 int oldfgcolor = current_vp->fg_pattern;
978 int oldbgcolor = current_vp->bg_pattern;
980 /* make sure scrolling is turned off on the line we are updating */
981 lcd_scroll_stop_line(current_vp, y);
983 if(!str || !str[0])
984 return;
986 lcd_getstringsize(str, &w, &h);
987 xpos = current_vp->xmargin + x*w / utf8length(str);
988 ypos = current_vp->ymargin + y*h;
989 current_vp->drawmode = (style & STYLE_INVERT) ?
990 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
991 if (style & STYLE_COLORED) {
992 if (current_vp->drawmode == DRMODE_SOLID)
993 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
994 else
995 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
997 current_vp->drawmode ^= DRMODE_INVERSEVID;
998 xrect = xpos + MAX(w - offset, 0);
1000 if (style & STYLE_GRADIENT) {
1001 current_vp->drawmode = DRMODE_FG;
1002 if (CURLN_UNPACK(style) == 0)
1003 lcd_gradient_rect(xpos, current_vp->width, ypos, h*NUMLN_UNPACK(style));
1004 current_vp->fg_pattern = current_vp->lst_pattern;
1006 else if (style & STYLE_COLORBAR) {
1007 current_vp->drawmode = DRMODE_FG;
1008 current_vp->fg_pattern = current_vp->lss_pattern;
1009 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
1010 current_vp->fg_pattern = current_vp->lst_pattern;
1012 else {
1013 lcd_fillrect(xrect, ypos, current_vp->width - xrect, h);
1014 current_vp->drawmode = (style & STYLE_INVERT) ?
1015 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
1017 lcd_putsxyofs(xpos, ypos, offset, str);
1018 current_vp->drawmode = lastmode;
1019 current_vp->fg_pattern = oldfgcolor;
1020 current_vp->bg_pattern = oldbgcolor;
1023 /*** scrolling ***/
1024 void lcd_puts_scroll(int x, int y, const unsigned char *string)
1026 lcd_puts_scroll_style(x, y, string, STYLE_DEFAULT);
1029 void lcd_puts_scroll_style(int x, int y, const unsigned char *string, int style)
1031 lcd_puts_scroll_style_offset(x, y, string, style, 0);
1034 void lcd_puts_scroll_offset(int x, int y, const unsigned char *string, int offset)
1036 lcd_puts_scroll_style_offset(x, y, string, STYLE_DEFAULT, offset);
1039 /* Initialise a scrolling line at (x,y) in current viewport */
1041 void lcd_puts_scroll_style_offset(int x, int y, const unsigned char *string,
1042 int style, int offset)
1044 struct scrollinfo* s;
1045 int w, h;
1047 if ((unsigned)y >= (unsigned)current_vp->height)
1048 return;
1050 /* remove any previously scrolling line at the same location */
1051 lcd_scroll_stop_line(current_vp, y);
1053 if (lcd_scroll_info.lines >= LCD_SCROLLABLE_LINES) return;
1055 s = &lcd_scroll_info.scroll[lcd_scroll_info.lines];
1057 s->start_tick = current_tick + lcd_scroll_info.delay;
1058 s->style = style;
1059 lcd_puts_style_offset(x,y,string,style,offset);
1061 lcd_getstringsize(string, &w, &h);
1063 if (current_vp->width - x * 8 - current_vp->xmargin < w) {
1064 /* prepare scroll line */
1065 char *end;
1067 memset(s->line, 0, sizeof s->line);
1068 strcpy(s->line, string);
1070 /* get width */
1071 s->width = lcd_getstringsize(s->line, &w, &h);
1073 /* scroll bidirectional or forward only depending on the string
1074 width */
1075 if ( lcd_scroll_info.bidir_limit ) {
1076 s->bidir = s->width < (current_vp->width - current_vp->xmargin) *
1077 (100 + lcd_scroll_info.bidir_limit) / 100;
1079 else
1080 s->bidir = false;
1082 if (!s->bidir) { /* add spaces if scrolling in the round */
1083 strcat(s->line, " ");
1084 /* get new width incl. spaces */
1085 s->width = lcd_getstringsize(s->line, &w, &h);
1088 end = strchr(s->line, '\0');
1089 strncpy(end, string, current_vp->width/2);
1091 s->vp = current_vp;
1092 s->y = y;
1093 s->len = utf8length(string);
1094 s->offset = offset;
1095 s->startx = current_vp->xmargin + x * s->width / s->len;
1096 s->backward = false;
1097 lcd_scroll_info.lines++;
1101 void lcd_scroll_fn(void)
1103 struct font* pf;
1104 struct scrollinfo* s;
1105 int index;
1106 int xpos, ypos;
1107 int lastmode;
1108 unsigned old_fgcolor;
1109 unsigned old_bgcolor;
1110 struct viewport* old_vp = current_vp;
1112 for ( index = 0; index < lcd_scroll_info.lines; index++ ) {
1113 s = &lcd_scroll_info.scroll[index];
1115 /* check pause */
1116 if (TIME_BEFORE(current_tick, s->start_tick))
1117 continue;
1119 lcd_set_viewport(s->vp);
1120 old_fgcolor = current_vp->fg_pattern;
1121 old_bgcolor = current_vp->bg_pattern;
1123 if (s->style&STYLE_COLORED) {
1124 if (s->style&STYLE_MODE_MASK) {
1125 current_vp->fg_pattern = old_fgcolor;
1126 current_vp->bg_pattern = s->style&STYLE_COLOR_MASK;
1128 else {
1129 current_vp->fg_pattern = s->style&STYLE_COLOR_MASK;
1130 current_vp->bg_pattern = old_bgcolor;
1134 if (s->backward)
1135 s->offset -= lcd_scroll_info.step;
1136 else
1137 s->offset += lcd_scroll_info.step;
1139 pf = font_get(current_vp->font);
1140 xpos = s->startx;
1141 ypos = current_vp->ymargin + s->y * pf->height;
1143 if (s->bidir) { /* scroll bidirectional */
1144 if (s->offset <= 0) {
1145 /* at beginning of line */
1146 s->offset = 0;
1147 s->backward = false;
1148 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
1150 if (s->offset >= s->width - (current_vp->width - xpos)) {
1151 /* at end of line */
1152 s->offset = s->width - (current_vp->width - xpos);
1153 s->backward = true;
1154 s->start_tick = current_tick + lcd_scroll_info.delay * 2;
1157 else {
1158 /* scroll forward the whole time */
1159 if (s->offset >= s->width)
1160 s->offset %= s->width;
1163 lastmode = current_vp->drawmode;
1164 switch (s->style&STYLE_MODE_MASK) {
1165 case STYLE_INVERT:
1166 current_vp->drawmode = DRMODE_SOLID|DRMODE_INVERSEVID;
1167 break;
1168 case STYLE_COLORBAR:
1169 /* Solid colour line selector */
1170 current_vp->drawmode = DRMODE_FG;
1171 current_vp->fg_pattern = current_vp->lss_pattern;
1172 lcd_fillrect(xpos, ypos, current_vp->width - xpos, pf->height);
1173 current_vp->fg_pattern = current_vp->lst_pattern;
1174 break;
1175 case STYLE_GRADIENT:
1176 /* Gradient line selector */
1177 current_vp->drawmode = DRMODE_FG;
1178 lcd_gradient_rect_scroll(xpos, current_vp->width, ypos, (signed)pf->height,
1179 NUMLN_UNPACK(s->style),
1180 CURLN_UNPACK(s->style));
1181 current_vp->fg_pattern = current_vp->lst_pattern;
1182 break;
1183 default:
1184 current_vp->drawmode = DRMODE_SOLID;
1185 break;
1187 lcd_putsxyofs(xpos, ypos, s->offset, s->line);
1188 current_vp->drawmode = lastmode;
1189 current_vp->fg_pattern = old_fgcolor;
1190 current_vp->bg_pattern = old_bgcolor;
1191 lcd_update_viewport_rect(xpos, ypos, current_vp->width - xpos, pf->height);
1194 lcd_set_viewport(old_vp);