1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) Jonathan Gordon (2006)
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 ****************************************************************************/
27 #include "screen_access.h"
32 #include "scrollbar.h"
37 #include "color_picker.h"
40 /* structure for color info */
43 unsigned color
; /* native color value */
46 unsigned char rgb_val
[6]; /* access to components as array */
49 unsigned char r
; /* native red value */
50 unsigned char g
; /* native green value */
51 unsigned char b
; /* native blue value */
52 unsigned char red
; /* 8 bit red value */
53 unsigned char green
; /* 8 bit green value */
54 unsigned char blue
; /* 8 bit blue value */
55 } __attribute__ ((__packed__
)); /* assume byte packing */
60 /* list of primary colors */
63 static const fb_data prim_rgb
[][3] =
65 /* Foreground colors for sliders */
67 LCD_RGBPACK(255, 0, 0),
68 LCD_RGBPACK( 0, 255, 0),
69 LCD_RGBPACK( 0, 0, 255),
71 /* Fill colors for sliders */
73 LCD_RGBPACK( 85, 0, 0),
74 LCD_RGBPACK( 0, 85, 0),
75 LCD_RGBPACK( 0, 0, 85),
79 /* maximum values for components */
80 static const unsigned char rgb_max
[3] =
87 /* Unpacks the color value into native rgb values and 24 bit rgb values */
88 static void unpack_rgb(struct rgb_pick
*rgb
)
90 unsigned color
= _LCD_UNSWAP_COLOR(rgb
->color
);
91 rgb
->red
= _RGB_UNPACK_RED(color
);
92 rgb
->green
= _RGB_UNPACK_GREEN(color
);
93 rgb
->blue
= _RGB_UNPACK_BLUE(color
);
94 rgb
->r
= _RGB_UNPACK_RED_LCD(color
);
95 rgb
->g
= _RGB_UNPACK_GREEN_LCD(color
);
96 rgb
->b
= _RGB_UNPACK_BLUE_LCD(color
);
99 /* Packs the native rgb colors into a color value */
100 static inline void pack_rgb(struct rgb_pick
*rgb
)
102 rgb
->color
= LCD_RGBPACK_LCD(rgb
->r
, rgb
->g
, rgb
->b
);
105 /* Returns LCD_BLACK if the color is above a threshold brightness
106 else return LCD_WHITE */
107 static inline unsigned get_black_or_white(const struct rgb_pick
*rgb
)
109 return (2*rgb
->red
+ 5*rgb
->green
+ rgb
->blue
) >= 1024 ?
110 LCD_BLACK
: LCD_WHITE
;
113 #define MARGIN_TOP 2 /* Top margin of screen */
114 #define MARGIN_BOTTOM 6 /* Bottom margin of screen */
115 #define SLIDER_TEXT_MARGIN 2 /* Gap between text and sliders */
116 #define TITLE_MARGIN_BOTTOM 4 /* Space below title bar */
117 #define SELECTOR_TB_MARGIN 1 /* Margin on top and bottom of selector */
118 #define SWATCH_TOP_MARGIN 4 /* Space between last slider and swatch */
119 #define SELECTOR_WIDTH get_icon_width(display->screen_type)
120 #define SELECTOR_HEIGHT 8 /* Height of > and < bitmaps */
122 /* dunno why lcd_set_drawinfo should be left out of struct screen */
123 static void set_drawinfo(struct screen
*display
, int mode
,
124 unsigned foreground
, unsigned background
)
126 display
->set_drawmode(mode
);
127 if (display
->depth
> 1)
129 display
->set_foreground(foreground
);
130 display
->set_background(background
);
134 /* Figure out widest label character in case they vary -
135 this function assumes labels are one character */
136 static int label_get_max_width(struct screen
*display
)
140 for (i
= 0, max_width
= 0; i
< 3; i
++)
143 buf
[0] = str(LANG_COLOR_RGB_LABELS
)[i
];
145 width
= display
->getstringsize(buf
, NULL
, NULL
);
146 if (width
> max_width
)
152 static void draw_screen(struct screen
*display
, char *title
,
153 struct rgb_pick
*rgb
, int row
)
155 unsigned text_color
= LCD_BLACK
;
156 unsigned background_color
= LCD_WHITE
;
158 int i
, char_height
, line_height
;
160 int text_x
, text_top
;
161 int slider_x
, slider_width
;
162 bool display_three_rows
;
165 viewport_set_defaults(&vp
, display
->screen_type
);
166 display
->set_viewport(&vp
);
168 display
->clear_viewport();
170 if (display
->depth
> 1)
172 text_color
= display
->get_foreground();
173 background_color
= display
->get_background();
176 /* Draw title string */
177 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
178 vp
.flags
|= VP_FLAG_ALIGN_CENTER
;
179 display
->putsxy(0, MARGIN_TOP
, title
);
181 /* Get slider positions and top starting position */
182 max_label_width
= label_get_max_width(display
);
183 char_height
= display
->getcharheight();
184 text_top
= MARGIN_TOP
+ char_height
+
185 TITLE_MARGIN_BOTTOM
+ SELECTOR_TB_MARGIN
;
186 text_x
= SELECTOR_WIDTH
;
187 slider_x
= text_x
+ max_label_width
+ SLIDER_TEXT_MARGIN
;
188 slider_width
= vp
.width
- slider_x
*2 - max_label_width
;
189 line_height
= char_height
+ 2*SELECTOR_TB_MARGIN
;
191 /* Find out if there's enough room for three sliders or just
192 enough to display the selected slider - calculate total height
193 of display with three sliders present */
196 text_top
+ line_height
*3 + /* Title + 3 sliders */
197 SWATCH_TOP_MARGIN
+ /* at least 2 lines */
198 char_height
*2 + /* + margins for bottom */
199 MARGIN_BOTTOM
; /* colored rectangle */
201 for (i
= 0; i
< 3; i
++)
203 unsigned sb_flags
= HORIZONTAL
;
204 int mode
= DRMODE_SOLID
;
205 unsigned fg
= text_color
;
206 unsigned bg
= background_color
;
210 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
212 if (global_settings
.cursor_style
!= 0)
214 /* Draw solid bar selection bar */
216 text_top
- SELECTOR_TB_MARGIN
,
218 char_height
+ SELECTOR_TB_MARGIN
*2);
220 if (display
->depth
< 16)
222 sb_flags
|= FOREGROUND
| INNER_FILL
;
223 mode
|= DRMODE_INVERSEVID
;
226 else if (display_three_rows
)
228 /* Draw "> <" around sliders */
229 int top
= text_top
+ (char_height
- SELECTOR_HEIGHT
) / 2;
230 screen_put_iconxy(display
, 0, top
, Icon_Cursor
);
231 screen_put_iconxy(display
,
232 vp
.width
- SELECTOR_WIDTH
,
236 if (display
->depth
>= 16)
238 sb_flags
|= FOREGROUND
| INNER_BGFILL
;
240 fg
= prim_rgb
[SB_PRIM
][i
];
241 bg
= prim_rgb
[SB_FILL
][i
];
244 else if (!display_three_rows
)
247 set_drawinfo(display
, mode
, fg
, bg
);
250 buf
[0] = str(LANG_COLOR_RGB_LABELS
)[i
];
252 vp
.flags
&= ~VP_FLAG_ALIGNMENT_MASK
;
253 display
->putsxy(text_x
, text_top
, buf
);
254 /* Draw color value */
255 snprintf(buf
, 3, "%02d", rgb
->rgb_val
[i
]);
256 vp
.flags
|= VP_FLAG_ALIGN_RIGHT
;
257 display
->putsxy(text_x
, text_top
, buf
);
260 gui_scrollbar_draw(display
, /* screen */
262 text_top
+ char_height
/ 4, /* y */
263 slider_width
, /* width */
264 char_height
/ 2, /* height */
265 rgb_max
[i
], /* items */
267 rgb
->rgb_val
[i
], /* max_shown */
268 sb_flags
); /* flags */
270 /* Advance to next line */
271 text_top
+= line_height
;
274 /* Format RGB: #rrggbb */
275 snprintf(buf
, sizeof(buf
), str(LANG_COLOR_RGB_VALUE
),
276 rgb
->red
, rgb
->green
, rgb
->blue
);
277 vp
.flags
|= VP_FLAG_ALIGN_CENTER
;
278 if (display
->depth
>= 16)
280 /* Display color swatch on color screens only */
281 int top
= text_top
+ SWATCH_TOP_MARGIN
;
282 int width
= vp
.width
- text_x
*2;
283 int height
= vp
.height
- top
- MARGIN_BOTTOM
;
285 /* Only draw if room */
286 if (height
>= char_height
+ 2)
288 /* draw the big rectangle */
289 display
->set_foreground(rgb
->color
);
290 display
->fillrect(text_x
, top
, width
, height
);
292 /* Draw RGB: #rrggbb in middle of swatch */
293 set_drawinfo(display
, DRMODE_FG
, get_black_or_white(rgb
),
296 display
->putsxy(0, top
+ (height
- char_height
) / 2, buf
);
298 /* Draw border around the rect */
299 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
300 display
->drawrect(text_x
, top
, width
, height
);
305 /* Display RGB value only centered on remaining display if room */
306 int top
= text_top
+ SWATCH_TOP_MARGIN
;
307 int height
= vp
.height
- top
- MARGIN_BOTTOM
;
309 if (height
>= char_height
)
311 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
312 display
->putsxy(0, top
+ (height
- char_height
) / 2, buf
);
316 display
->update_viewport();
317 display
->set_viewport(NULL
);
320 #ifdef HAVE_TOUCHSCREEN
321 static int touchscreen_slider(struct screen
*display
,
322 struct rgb_pick
*rgb
,
323 int *selected_slider
)
326 int char_height
, line_height
;
328 int text_top
, slider_x
, slider_width
;
329 bool display_three_rows
;
334 viewport_set_defaults(&vp
, display
->screen_type
);
335 display
->set_viewport(&vp
);
337 button
= action_get_touchscreen_press_in_vp(&x
, &y
, &vp
);
338 if (button
== ACTION_UNKNOWN
|| button
== BUTTON_NONE
)
340 /* Get slider positions and top starting position
341 * need vp.y here, because of the statusbar, since touchscreen
342 * coordinates are absolute */
343 max_label_width
= label_get_max_width(display
);
344 char_height
= display
->getcharheight();
345 text_top
= MARGIN_TOP
+ char_height
+
346 TITLE_MARGIN_BOTTOM
+ SELECTOR_TB_MARGIN
;
347 slider_x
= SELECTOR_WIDTH
+ max_label_width
+ SLIDER_TEXT_MARGIN
;
348 slider_width
= vp
.width
- slider_x
*2 - max_label_width
;
349 line_height
= char_height
+ 2*SELECTOR_TB_MARGIN
;
351 /* same logic as in draw_screen */
352 /* Find out if there's enough room for three sliders or just
353 enough to display the selected slider - calculate total height
354 of display with three sliders present */
357 text_top
+ line_height
*3 + /* Title + 3 sliders */
358 SWATCH_TOP_MARGIN
+ /* at least 2 lines */
359 char_height
*2 + /* + margins for bottom */
360 MARGIN_BOTTOM
; /* colored rectangle */
362 display
->set_viewport(NULL
);
366 if (button
== BUTTON_REL
)
367 return ACTION_STD_CANCEL
;
372 if (y
>= text_top
+ line_height
* (display_three_rows
? 3:1))
373 { /* touching the color area means accept */
374 if (button
== BUTTON_REL
)
375 return ACTION_STD_OK
;
379 /* y is relative to the original viewport */
380 pressed_slider
= (y
- text_top
)/line_height
;
381 if (pressed_slider
!= *selected_slider
)
382 *selected_slider
= pressed_slider
;
383 /* add max_label_width to overcome integer division limits,
384 * cap value later since that may lead to an overflow */
385 if (x
< slider_x
+ (slider_width
+max_label_width
) && x
> slider_x
)
389 computed_val
= (x
*rgb_max
[pressed_slider
]/(slider_width
));
390 rgb
->rgb_val
[pressed_slider
] =
391 MIN(computed_val
,rgb_max
[pressed_slider
]);
399 returns true if USB was inserted, false otherwise
400 color is a pointer to the colour (in native format) to modify
401 set banned_color to -1 to allow all
403 bool set_color(struct screen
*display
, char *title
,
404 unsigned *color
, unsigned banned_color
)
406 int exit
= 0, slider
= 0;
419 draw_screen(display
, title
, &rgb
, slider
);
425 draw_screen(&screens
[i
], title
, &rgb
, slider
);
428 button
= get_action(CONTEXT_SETTINGS_COLOURCHOOSER
, TIMEOUT_BLOCK
);
429 #ifdef HAVE_TOUCHSCREEN
430 if (button
== ACTION_TOUCHSCREEN
431 && display
->screen_type
== SCREEN_MAIN
)
432 button
= touchscreen_slider(display
, &rgb
, &slider
);
437 case ACTION_STD_PREV
:
438 case ACTION_STD_PREVREPEAT
:
439 slider
= (slider
+ 2) % 3;
442 case ACTION_STD_NEXT
:
443 case ACTION_STD_NEXTREPEAT
:
444 slider
= (slider
+ 1) % 3;
447 case ACTION_SETTINGS_INC
:
448 case ACTION_SETTINGS_INCREPEAT
:
449 if (rgb
.rgb_val
[slider
] < rgb_max
[slider
])
450 rgb
.rgb_val
[slider
]++;
454 case ACTION_SETTINGS_DEC
:
455 case ACTION_SETTINGS_DECREPEAT
:
456 if (rgb
.rgb_val
[slider
] > 0)
457 rgb
.rgb_val
[slider
]--;
462 if (banned_color
!= (unsigned)-1 &&
463 banned_color
== rgb
.color
)
465 splash(HZ
*2, ID2P(LANG_COLOR_UNACCEPTABLE
));
472 case ACTION_STD_CANCEL
:
477 if (default_event_handler(button
) == SYS_USB_CONNECTED
)