Makefile overhaul. All generated bitmaps are now explicitly in OBJDIR/bitmaps and...
[kugel-rb.git] / apps / gui / icon.c
blobe11c21cbeac372f7472a0538b6f7bd6e3ffd1a3a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jonathan Gordon
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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "inttypes.h"
25 #include "config.h"
26 #include "icon.h"
27 #include "screen_access.h"
28 #include "icons.h"
29 #include "settings.h"
30 #include "bmp.h"
31 #include "filetypes.h"
33 #include "bitmaps/default_icons.h"
34 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
35 #include "bitmaps/remote_default_icons.h"
36 #endif
38 /* These are just the file names, the full path is snprint'ed when used */
39 #define DEFAULT_VIEWER_BMP "viewers"
40 #define DEFAULT_REMOTE_VIEWER_BMP "remote_viewers"
42 /* These should probably be moved to config-<target>.h */
43 #define MAX_ICON_HEIGHT 24
44 #define MAX_ICON_WIDTH 24
47 /* We dont actually do anything with these pointers,
48 but they need to be grouped like this to save code
49 so storing them as void* is ok. (stops compile warning) */
50 static const void * inbuilt_icons[NB_SCREENS] = {
51 (void*)default_icons
52 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
53 , (void*)remote_default_icons
54 #endif
57 static const int default_width[NB_SCREENS] = {
58 BMPWIDTH_default_icons
59 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
60 , BMPWIDTH_remote_default_icons
61 #endif
64 /* height of whole file */
65 static const int default_height[NB_SCREENS] = {
66 BMPHEIGHT_default_icons
67 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
68 , BMPHEIGHT_remote_default_icons
69 #endif
72 #define IMG_BUFSIZE (MAX_ICON_HEIGHT * MAX_ICON_WIDTH * \
73 Icon_Last_Themeable *LCD_DEPTH/8)
74 static unsigned char icon_buffer[NB_SCREENS][IMG_BUFSIZE];
75 static bool custom_icons_loaded[NB_SCREENS] = {false};
76 static struct bitmap user_iconset[NB_SCREENS];
78 static unsigned char viewer_icon_buffer[NB_SCREENS][IMG_BUFSIZE];
79 static bool viewer_icons_loaded[NB_SCREENS] = {false};
80 static struct bitmap viewer_iconset[NB_SCREENS];
83 #define ICON_HEIGHT(screen) (!custom_icons_loaded[screen]? \
84 default_height[screen] : \
85 user_iconset[screen].height) \
86 / Icon_Last_Themeable
88 #define ICON_WIDTH(screen) (!custom_icons_loaded[screen]? \
89 default_width[screen] : \
90 user_iconset[screen].width)
92 /* x,y in letters, not pixles */
93 void screen_put_icon(struct screen * display,
94 int x, int y, enum themable_icons icon)
96 screen_put_icon_with_offset(display, x, y, 0, 0, icon);
99 void screen_put_icon_with_offset(struct screen * display,
100 int x, int y, int off_x, int off_y,
101 enum themable_icons icon)
103 int xpos, ypos;
104 int width, height;
105 int screen = display->screen_type;
106 display->getstringsize((unsigned char *)"M", &width, &height);
107 xpos = x*ICON_WIDTH(screen) + off_x;
108 ypos = y*height + off_y;
110 if ( height > ICON_HEIGHT(screen) )/* center the cursor */
111 ypos += (height - ICON_HEIGHT(screen)) / 2;
112 screen_put_iconxy(display, xpos, ypos, icon);
115 /* x,y in pixels */
116 void screen_put_iconxy(struct screen * display,
117 int xpos, int ypos, enum themable_icons icon)
119 const void *data;
120 int screen = display->screen_type;
121 int width = ICON_WIDTH(screen);
122 int height = ICON_HEIGHT(screen);
123 screen_bitmap_part_func *draw_func = NULL;
125 if (icon == Icon_NOICON)
127 screen_clear_area(display, xpos, ypos, width, height);
128 return;
130 else if (icon >= Icon_Last_Themeable)
132 icon -= Icon_Last_Themeable;
133 if (!viewer_icons_loaded[screen] ||
134 (global_status.viewer_icon_count*height
135 > viewer_iconset[screen].height) ||
136 (icon * height > viewer_iconset[screen].height))
138 screen_put_iconxy(display, xpos, ypos, Icon_Questionmark);
139 return;
141 data = viewer_iconset[screen].data;
143 else if (custom_icons_loaded[screen])
145 data = user_iconset[screen].data;
147 else
149 data = inbuilt_icons[screen];
151 /* add some left padding to the icons if they are on the edge */
152 if (xpos == 0)
153 xpos++;
155 #if (LCD_DEPTH == 16) || defined(LCD_REMOTE_DEPTH) && (LCD_REMOTE_DEPTH == 16)
156 if (display->depth == 16)
157 draw_func = display->transparent_bitmap_part;
158 else
159 #endif
160 draw_func = display->bitmap_part;
162 draw_func(data, 0, height * icon, width, xpos, ypos, width, height);
165 void screen_put_cursorxy(struct screen * display, int x, int y, bool on)
167 #ifdef HAVE_LCD_BITMAP
168 screen_put_icon(display, x, y, on?Icon_Cursor:0);
169 #else
170 screen_put_icon(display, x, y, on?CURSOR_CHAR:-1);
171 #endif
174 enum Iconset {
175 Iconset_Mainscreen,
176 Iconset_Mainscreen_viewers,
177 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
178 Iconset_Remotescreen,
179 Iconset_Remotescreen_viewers,
180 #endif
183 static void load_icons(const char* filename, enum Iconset iconset,
184 bool allow_disable)
186 int size_read;
187 bool *loaded_ok = NULL;
188 struct bitmap *bmp = NULL;
189 int bmpformat = (FORMAT_NATIVE|FORMAT_DITHER);
191 switch (iconset)
193 case Iconset_Mainscreen:
194 loaded_ok = &custom_icons_loaded[SCREEN_MAIN];
195 bmp = &user_iconset[SCREEN_MAIN];
196 bmp->data = icon_buffer[SCREEN_MAIN];
197 break;
198 case Iconset_Mainscreen_viewers:
199 loaded_ok = &viewer_icons_loaded[SCREEN_MAIN];
200 bmp = &viewer_iconset[SCREEN_MAIN];
201 bmp->data = viewer_icon_buffer[SCREEN_MAIN];
202 break;
203 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
204 case Iconset_Remotescreen:
205 loaded_ok = &custom_icons_loaded[SCREEN_REMOTE];
206 bmp = &user_iconset[SCREEN_REMOTE];
207 bmp->data = icon_buffer[SCREEN_REMOTE];
208 bmpformat |= FORMAT_REMOTE;
209 break;
210 case Iconset_Remotescreen_viewers:
211 loaded_ok = &viewer_icons_loaded[SCREEN_REMOTE];
212 bmp = &viewer_iconset[SCREEN_REMOTE];
213 bmp->data = viewer_icon_buffer[SCREEN_REMOTE];
214 bmpformat |= FORMAT_REMOTE;
215 break;
216 #endif
219 *loaded_ok = false;
220 if (!allow_disable || (filename[0] && filename[0] != '-'))
222 char path[MAX_PATH];
224 snprintf(path, sizeof(path), "%s/%s.bmp", ICON_DIR, filename);
225 size_read = read_bmp_file(path, bmp, IMG_BUFSIZE, bmpformat);
226 if (size_read > 0)
228 *loaded_ok = true;
234 void icons_init(void)
236 load_icons(global_settings.icon_file, Iconset_Mainscreen, true);
238 if (*global_settings.viewers_icon_file)
240 load_icons(global_settings.viewers_icon_file,
241 Iconset_Mainscreen_viewers, true);
242 read_viewer_theme_file();
244 else
246 load_icons(DEFAULT_VIEWER_BMP, Iconset_Mainscreen_viewers, false);
249 #if defined(HAVE_REMOTE_LCD) && (NB_SCREENS > 1)
250 load_icons(global_settings.remote_icon_file,
251 Iconset_Remotescreen, true);
253 if (*global_settings.remote_viewers_icon_file)
255 load_icons(global_settings.remote_viewers_icon_file,
256 Iconset_Remotescreen_viewers, true);
258 else
260 load_icons(DEFAULT_REMOTE_VIEWER_BMP,
261 Iconset_Remotescreen_viewers, false);
263 #endif
266 int get_icon_width(enum screen_type screen_type)
268 return ICON_WIDTH(screen_type);