1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) Jonathan Gordon (2006)
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 ****************************************************************************/
25 #include "screen_access.h"
30 #include "scrollbar.h"
35 #include "color_picker.h"
37 /* structure for color info */
40 unsigned color
; /* native color value */
43 unsigned char rgb_val
[6]; /* access to components as array */
46 unsigned char r
; /* native red value */
47 unsigned char g
; /* native green value */
48 unsigned char b
; /* native blue value */
49 unsigned char red
; /* 8 bit red value */
50 unsigned char green
; /* 8 bit green value */
51 unsigned char blue
; /* 8 bit blue value */
52 } __attribute__ ((__packed__
)); /* assume byte packing */
57 /* list of primary colors */
60 static const fb_data prim_rgb
[][3] =
62 /* Foreground colors for sliders */
64 LCD_RGBPACK(255, 0, 0),
65 LCD_RGBPACK( 0, 255, 0),
66 LCD_RGBPACK( 0, 0, 255),
68 /* Fill colors for sliders */
70 LCD_RGBPACK( 85, 0, 0),
71 LCD_RGBPACK( 0, 85, 0),
72 LCD_RGBPACK( 0, 0, 85),
76 /* maximum values for components */
77 static const unsigned char rgb_max
[3] =
84 /* Unpacks the color value into native rgb values and 24 bit rgb values */
85 static void unpack_rgb(struct rgb_pick
*rgb
)
87 unsigned color
= _LCD_UNSWAP_COLOR(rgb
->color
);
88 rgb
->red
= _RGB_UNPACK_RED(color
);
89 rgb
->green
= _RGB_UNPACK_GREEN(color
);
90 rgb
->blue
= _RGB_UNPACK_BLUE(color
);
91 rgb
->r
= _RGB_UNPACK_RED_LCD(color
);
92 rgb
->g
= _RGB_UNPACK_GREEN_LCD(color
);
93 rgb
->b
= _RGB_UNPACK_BLUE_LCD(color
);
96 /* Packs the native rgb colors into a color value */
97 static inline void pack_rgb(struct rgb_pick
*rgb
)
99 rgb
->color
= LCD_RGBPACK_LCD(rgb
->r
, rgb
->g
, rgb
->b
);
102 /* Returns LCD_BLACK if the color is above a threshold brightness
103 else return LCD_WHITE */
104 static inline unsigned get_black_or_white(const struct rgb_pick
*rgb
)
106 return (2*rgb
->red
+ 5*rgb
->green
+ rgb
->blue
) >= 1024 ?
107 LCD_BLACK
: LCD_WHITE
;
110 #define MARGIN_LEFT 0 /* Left margin of screen */
111 #define MARGIN_TOP 2 /* Top margin of screen */
112 #define MARGIN_RIGHT 0 /* Right margin of screen */
113 #define MARGIN_BOTTOM 6 /* Bottom margin of screen */
114 #define SLIDER_MARGIN_LEFT 2 /* Gap to left of sliders */
115 #define SLIDER_MARGIN_RIGHT 2 /* Gap to right of sliders */
116 #define TITLE_MARGIN_BOTTOM 4 /* Space below title bar */
117 #define SELECTOR_LR_MARGIN 0 /* Margin between ">" and text */
118 #define SELECTOR_TB_MARGIN 1 /* Margin on top and bottom of selector */
119 #define SWATCH_TOP_MARGIN 4 /* Space between last slider and swatch */
120 #define SELECTOR_WIDTH get_icon_width(display->screen_type)
121 #define SELECTOR_HEIGHT 8 /* Height of > and < bitmaps */
123 /* dunno why lcd_set_drawinfo should be left out of struct screen */
124 static void set_drawinfo(struct screen
*display
, int mode
,
125 unsigned foreground
, unsigned background
)
127 display
->set_drawmode(mode
);
128 if (display
->depth
> 1)
130 display
->set_foreground(foreground
);
131 display
->set_background(background
);
135 static void draw_screen(struct screen
*display
, char *title
,
136 struct rgb_pick
*rgb
, int row
)
138 unsigned text_color
= LCD_BLACK
;
139 unsigned background_color
= LCD_WHITE
;
143 int slider_left
, slider_width
;
144 bool display_three_rows
;
147 display
->clear_display();
149 if (display
->depth
> 1)
151 text_color
= display
->get_foreground();
152 background_color
= display
->get_background();
155 /* Find out if there's enough room for three sliders or just
156 enough to display the selected slider - calculate total height
157 of display with three sliders present */
159 display
->height
>= MARGIN_TOP
+
160 display
->char_height
*4 + /* Title + 3 sliders */
161 TITLE_MARGIN_BOTTOM
+
162 SELECTOR_TB_MARGIN
*6 + /* 2 margins/slider */
165 /* Figure out widest label character in case they vary -
166 this function assumes labels are one character */
167 for (i
= 0, max_label_width
= 0; i
< 3; i
++)
169 buf
[0] = str(LANG_COLOR_RGB_LABELS
)[i
];
171 x
= display
->getstringsize(buf
, NULL
, NULL
);
172 if (x
> max_label_width
)
176 /* Draw title string */
177 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
178 display
->getstringsize(title
, &x
, &y
);
179 display
->putsxy((display
->width
- x
) / 2, MARGIN_TOP
, title
);
181 /* Get slider positions and top starting position */
182 text_top
= MARGIN_TOP
+ y
+ TITLE_MARGIN_BOTTOM
+ SELECTOR_TB_MARGIN
;
183 slider_left
= MARGIN_LEFT
+ SELECTOR_WIDTH
+ SELECTOR_LR_MARGIN
+
184 max_label_width
+ SLIDER_MARGIN_LEFT
;
185 slider_width
= display
->width
- slider_left
- SLIDER_MARGIN_RIGHT
-
186 display
->char_width
*2 - SELECTOR_LR_MARGIN
- SELECTOR_WIDTH
-
189 for (i
= 0; i
< 3; i
++)
191 unsigned sb_flags
= HORIZONTAL
;
192 int mode
= DRMODE_SOLID
;
193 unsigned fg
= text_color
;
194 unsigned bg
= background_color
;
196 if (!display_three_rows
)
201 set_drawinfo(display
, DRMODE_SOLID
, text_color
,
204 if (global_settings
.cursor_style
!= 0)
206 /* Draw solid bar selection bar */
208 text_top
- SELECTOR_TB_MARGIN
,
210 display
->char_height
+
211 SELECTOR_TB_MARGIN
*2);
213 if (display
->depth
< 16)
215 sb_flags
|= FOREGROUND
| INNER_FILL
;
216 mode
|= DRMODE_INVERSEVID
;
219 else if (display_three_rows
)
221 /* Draw "> <" around sliders */
222 int top
= text_top
+ (display
->char_height
-
223 SELECTOR_HEIGHT
) / 2;
224 screen_put_iconxy(display
, MARGIN_LEFT
, top
, Icon_Cursor
);
225 screen_put_iconxy(display
,
226 display
->width
- MARGIN_RIGHT
-
227 get_icon_width(display
->screen_type
),
231 if (display
->depth
>= 16)
233 sb_flags
|= FOREGROUND
| INNER_BGFILL
;
235 fg
= prim_rgb
[SB_PRIM
][i
];
236 bg
= prim_rgb
[SB_FILL
][i
];
240 set_drawinfo(display
, mode
, fg
, bg
);
243 buf
[0] = str(LANG_COLOR_RGB_LABELS
)[i
];
245 display
->putsxy(slider_left
- display
->char_width
-
246 SLIDER_MARGIN_LEFT
, text_top
, buf
);
248 /* Draw color value */
249 snprintf(buf
, 3, "%02d", rgb
->rgb_val
[i
]);
250 display
->putsxy(slider_left
+ slider_width
+ SLIDER_MARGIN_RIGHT
,
254 gui_scrollbar_draw(display
,
256 text_top
+ display
->char_height
/ 4,
258 display
->char_height
/ 2,
264 /* Advance to next line */
265 text_top
+= display
->char_height
+ 2*SELECTOR_TB_MARGIN
;
267 if (!display_three_rows
)
271 /* Draw color value in system font */
272 display
->setfont(FONT_SYSFIXED
);
274 /* Format RGB: #rrggbb */
275 snprintf(buf
, sizeof(buf
), str(LANG_COLOR_RGB_VALUE
),
276 rgb
->red
, rgb
->green
, rgb
->blue
);
278 if (display
->depth
>= 16)
280 /* Display color swatch on color screens only */
281 int left
= MARGIN_LEFT
+ SELECTOR_WIDTH
+ SELECTOR_LR_MARGIN
;
282 int top
= text_top
+ SWATCH_TOP_MARGIN
;
283 int width
= display
->width
- left
- SELECTOR_LR_MARGIN
-
284 SELECTOR_WIDTH
- MARGIN_RIGHT
;
285 int height
= display
->height
- top
- MARGIN_BOTTOM
;
287 /* Only draw if room */
288 if (height
>= display
->char_height
+ 2)
290 display
->set_foreground(rgb
->color
);
291 display
->fillrect(left
, top
, width
, height
);
293 /* Draw RGB: #rrggbb in middle of swatch */
294 display
->set_drawmode(DRMODE_FG
);
295 display
->getstringsize(buf
, &x
, &y
);
296 display
->set_foreground(get_black_or_white(rgb
));
298 x
= left
+ (width
- x
) / 2;
299 y
= top
+ (height
- y
) / 2;
301 display
->putsxy(x
, y
, buf
);
302 display
->set_drawmode(DRMODE_SOLID
);
305 display
->set_foreground(text_color
);
306 display
->drawrect(left
, top
, width
, height
);
311 /* Display RGB value only centered on remaining display if room */
312 display
->getstringsize(buf
, &x
, &y
);
313 i
= text_top
+ SWATCH_TOP_MARGIN
;
315 if (i
+ y
<= display
->height
- MARGIN_BOTTOM
)
317 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
318 x
= (display
->width
- x
) / 2;
319 y
= (i
+ display
->height
- MARGIN_BOTTOM
- y
) / 2;
320 display
->putsxy(x
, y
, buf
);
324 display
->setfont(FONT_UI
);
327 /* Be sure screen mode is reset */
328 set_drawinfo(display
, DRMODE_SOLID
, text_color
, background_color
);
332 static int touchpad_slider(struct rgb_pick
*rgb
, int *selected_slider
)
336 int slider_left
, slider_width
;
337 unsigned button
= action_get_touchpad_press(&x
, &y
);
338 bool display_three_rows
;
340 struct screen
*display
= &screens
[SCREEN_MAIN
];
344 if (button
== BUTTON_NONE
)
346 /* same logic as draw_screen */
347 /* Figure out widest label character in case they vary -
348 this function assumes labels are one character */
349 for (i
= 0, max_label_width
= 0; i
< 3; i
++)
351 buf
[0] = str(LANG_COLOR_RGB_LABELS
)[i
];
353 x1
= display
->getstringsize(buf
, NULL
, NULL
);
354 if (x1
> max_label_width
)
355 max_label_width
= x1
;
357 /* Get slider positions and top starting position */
358 text_top
= MARGIN_TOP
+ display
->char_height
+ TITLE_MARGIN_BOTTOM
+ SELECTOR_TB_MARGIN
;
359 slider_left
= MARGIN_LEFT
+ SELECTOR_WIDTH
+ SELECTOR_LR_MARGIN
+
360 max_label_width
+ SLIDER_MARGIN_LEFT
;
361 slider_width
= display
->width
- slider_left
- SLIDER_MARGIN_RIGHT
-
362 display
->char_width
*2 - SELECTOR_LR_MARGIN
- SELECTOR_WIDTH
-
365 display
->height
>= MARGIN_TOP
+
366 display
->char_height
*4 + /* Title + 3 sliders */
367 TITLE_MARGIN_BOTTOM
+
368 SELECTOR_TB_MARGIN
*6 + /* 2 margins/slider */
370 if (y
< MARGIN_TOP
+display
->char_height
)
372 if (button
== BUTTON_REL
)
373 return ACTION_STD_CANCEL
;
376 pressed_slider
= y
/display
->char_height
;
377 if (pressed_slider
> (display_three_rows
?2:0))
379 if (button
== BUTTON_REL
)
380 return ACTION_STD_OK
;
382 if (pressed_slider
!= *selected_slider
)
383 *selected_slider
= pressed_slider
;
384 if (x
< slider_left
+slider_width
&&
388 rgb
->rgb_val
[pressed_slider
] =
389 (x
*rgb_max
[pressed_slider
]/(slider_width
-slider_left
));
397 returns true if USB was inserted, false otherwise
398 color is a pointer to the colour (in native format) to modify
399 set banned_color to -1 to allow all
401 bool set_color(struct screen
*display
, char *title
, unsigned *color
,
402 unsigned banned_color
)
404 int exit
= 0, slider
= 0;
417 draw_screen(display
, title
, &rgb
, slider
);
423 draw_screen(&screens
[i
], title
, &rgb
, slider
);
426 button
= get_action(CONTEXT_SETTINGS_COLOURCHOOSER
, TIMEOUT_BLOCK
);
428 if (button
== ACTION_TOUCHPAD
)
429 button
= touchpad_slider(&rgb
, &slider
);
434 case ACTION_STD_PREV
:
435 case ACTION_STD_PREVREPEAT
:
436 slider
= (slider
+ 2) % 3;
439 case ACTION_STD_NEXT
:
440 case ACTION_STD_NEXTREPEAT
:
441 slider
= (slider
+ 1) % 3;
444 case ACTION_SETTINGS_INC
:
445 case ACTION_SETTINGS_INCREPEAT
:
446 if (rgb
.rgb_val
[slider
] < rgb_max
[slider
])
447 rgb
.rgb_val
[slider
]++;
451 case ACTION_SETTINGS_DEC
:
452 case ACTION_SETTINGS_DECREPEAT
:
453 if (rgb
.rgb_val
[slider
] > 0)
454 rgb
.rgb_val
[slider
]--;
459 if (banned_color
!= (unsigned)-1 &&
460 banned_color
== rgb
.color
)
462 gui_syncsplash(HZ
*2, ID2P(LANG_COLOR_UNACCEPTABLE
));
469 case ACTION_STD_CANCEL
:
474 if (default_event_handler(button
) == SYS_USB_CONNECTED
)