Build doom on clipv2 and clip+
[kugel-rb.git] / apps / gui / color_picker.c
blob044edd7789726fa92f8973b9e530c224acc846c5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "config.h"
22 #include "stdarg.h"
23 #include "string.h"
24 #include "stdio.h"
25 #include "kernel.h"
26 #include "system.h"
27 #include "screen_access.h"
28 #include "font.h"
29 #include "debug.h"
30 #include "misc.h"
31 #include "settings.h"
32 #include "scrollbar.h"
33 #include "lang.h"
34 #include "splash.h"
35 #include "action.h"
36 #include "icon.h"
37 #include "color_picker.h"
38 #include "viewport.h"
40 /* structure for color info */
41 struct rgb_pick
43 unsigned color; /* native color value */
44 union
46 unsigned char rgb_val[6]; /* access to components as array */
47 struct
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 */
61 #define SB_PRIM 0
62 #define SB_FILL 1
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] =
82 LCD_MAX_RED,
83 LCD_MAX_GREEN,
84 LCD_MAX_BLUE
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)
138 int max_width, i;
139 char buf[4];
140 for (i = 0, max_width = 0; i < 3; i++)
142 int width;
143 buf[0] = str(LANG_COLOR_RGB_LABELS)[i];
144 buf[1] = '\0';
145 width = display->getstringsize(buf, NULL, NULL);
146 if (width > max_width)
147 max_width = width;
149 return 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;
157 char buf[32];
158 int i, char_height, line_height;
159 int max_label_width;
160 int text_x, text_top;
161 int slider_x, slider_width;
162 bool display_three_rows;
163 struct viewport vp;
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 */
194 display_three_rows =
195 vp.height >=
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;
208 if (i == row)
210 set_drawinfo(display, DRMODE_SOLID, text_color, background_color);
212 if (global_settings.cursor_style != 0)
214 /* Draw solid bar selection bar */
215 display->fillrect(0,
216 text_top - SELECTOR_TB_MARGIN,
217 vp.width,
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,
233 top, Icon_Cursor);
236 if (display->depth >= 16)
238 sb_flags |= FOREGROUND | INNER_BGFILL;
239 mode = DRMODE_FG;
240 fg = prim_rgb[SB_PRIM][i];
241 bg = prim_rgb[SB_FILL][i];
244 else if (!display_three_rows)
245 continue;
247 set_drawinfo(display, mode, fg, bg);
249 /* Draw label */
250 buf[0] = str(LANG_COLOR_RGB_LABELS)[i];
251 buf[1] = '\0';
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);
259 /* Draw scrollbar */
260 gui_scrollbar_draw(display, /* screen */
261 slider_x, /* x */
262 text_top + char_height / 4, /* y */
263 slider_width, /* width */
264 char_height / 2, /* height */
265 rgb_max[i], /* items */
266 0, /* min_shown */
267 rgb->rgb_val[i], /* max_shown */
268 sb_flags); /* flags */
270 /* Advance to next line */
271 text_top += line_height;
272 } /* end for */
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),
294 background_color);
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);
303 else
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)
325 short x, y;
326 int char_height, line_height;
327 int max_label_width;
328 int text_top, slider_x, slider_width;
329 bool display_three_rows;
330 int button;
331 int pressed_slider;
332 struct viewport vp;
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)
339 return ACTION_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 */
355 display_three_rows =
356 vp.height >=
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);
364 if (y < text_top)
366 if (button == BUTTON_REL)
367 return ACTION_STD_CANCEL;
368 else
369 return ACTION_NONE;
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;
376 else
377 return ACTION_NONE;
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)
387 char computed_val;
388 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]);
392 pack_rgb(rgb);
394 return ACTION_NONE;
396 #endif
397 /***********
398 set_color
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
402 ***********/
403 bool set_color(struct screen *display, char *title,
404 unsigned *color, unsigned banned_color)
406 int exit = 0, slider = 0;
407 struct rgb_pick rgb;
409 rgb.color = *color;
411 while (!exit)
413 int button;
415 unpack_rgb(&rgb);
417 if (display != NULL)
419 draw_screen(display, title, &rgb, slider);
421 else
423 int i;
424 FOR_NB_SCREENS(i)
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);
433 #endif
435 switch (button)
437 case ACTION_STD_PREV:
438 case ACTION_STD_PREVREPEAT:
439 slider = (slider + 2) % 3;
440 break;
442 case ACTION_STD_NEXT:
443 case ACTION_STD_NEXTREPEAT:
444 slider = (slider + 1) % 3;
445 break;
447 case ACTION_SETTINGS_INC:
448 case ACTION_SETTINGS_INCREPEAT:
449 if (rgb.rgb_val[slider] < rgb_max[slider])
450 rgb.rgb_val[slider]++;
451 pack_rgb(&rgb);
452 break;
454 case ACTION_SETTINGS_DEC:
455 case ACTION_SETTINGS_DECREPEAT:
456 if (rgb.rgb_val[slider] > 0)
457 rgb.rgb_val[slider]--;
458 pack_rgb(&rgb);
459 break;
461 case ACTION_STD_OK:
462 if (banned_color != (unsigned)-1 &&
463 banned_color == rgb.color)
465 splash(HZ*2, ID2P(LANG_COLOR_UNACCEPTABLE));
466 break;
468 *color = rgb.color;
469 exit = 1;
470 break;
472 case ACTION_STD_CANCEL:
473 exit = 1;
474 break;
476 default:
477 if (default_event_handler(button) == SYS_USB_CONNECTED)
478 return true;
479 break;
483 return false;