Make sure to set the current lcd backdrop id so the buflib move callback can reset...
[maemo-rb.git] / apps / gui / skin_engine / skin_backdrops.c
blob4701f51b19207976b7b844aef7be237722311b29
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 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 ****************************************************************************/
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "core_alloc.h"
26 #include "string-extra.h"
27 #include "settings.h"
28 #include "wps_internals.h"
29 #include "skin_engine.h"
31 #if !defined(__PCTOOL__) && \
32 ((LCD_DEPTH > 1) || (defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)))
34 #define NB_BDROPS SKINNABLE_SCREENS_COUNT*NB_SCREENS
35 static struct skin_backdrop {
36 char name[MAX_PATH];
37 char *buffer;
38 enum screen_type screen;
39 bool loaded;
40 int buflib_handle;
41 } backdrops[NB_BDROPS];
43 #define NB_BDROPS SKINNABLE_SCREENS_COUNT*NB_SCREENS
44 static int handle_being_loaded;
45 static int current_lcd_backdrop[NB_SCREENS];
47 static int buflib_move_callback(int handle, void* current, void* new)
49 int i;
50 if (handle == handle_being_loaded)
51 return BUFLIB_CB_CANNOT_MOVE;
52 for (i=0; i<NB_BDROPS; i++)
54 if (backdrops[i].buffer == current)
56 backdrops[i].buffer = new;
57 break;
60 FOR_NB_SCREENS(i)
61 skin_backdrop_show(current_lcd_backdrop[i]);
62 return BUFLIB_CB_OK;
64 static struct buflib_callbacks buflib_ops = {buflib_move_callback, NULL};
65 static bool first_go = true;
66 void skin_backdrop_init(void)
68 int i;
70 for (i=0; i<NB_BDROPS; i++)
72 if (first_go)
73 backdrops[i].buflib_handle = -1;
74 else
75 skin_backdrop_unload(i);
76 backdrops[i].name[0] = '\0';
77 backdrops[i].buffer = NULL;
78 backdrops[i].loaded = false;
81 first_go = false;
82 FOR_NB_SCREENS(i)
83 current_lcd_backdrop[i] = -1;
84 handle_being_loaded = -1;
87 int skin_backdrop_assign(char* backdrop, char *bmpdir,
88 enum screen_type screen)
90 char filename[MAX_PATH];
91 int i, free = -1;
92 if (!backdrop)
93 return -1;
94 if (backdrop[0] == '-')
96 filename[0] = '-';
97 filename[1] = '\0';
98 filename[2] = '\0'; /* we check this later to see if we actually have an
99 image to load. != '\0' means display the image */
101 else
103 get_image_filename(backdrop, bmpdir, filename, sizeof(filename));
105 for (i=0; i<NB_BDROPS; i++)
107 if (!backdrops[i].name[0] && free < 0)
108 free = i;
109 if (!strcmp(backdrops[i].name, filename) && backdrops[i].screen == screen)
111 break;
114 if (i < NB_BDROPS)
115 return i;
116 else if (free >= 0)
118 strlcpy(backdrops[free].name, filename,
119 sizeof (backdrops[free].name));
120 backdrops[free].buffer = NULL;
121 backdrops[free].screen = screen;
122 return free;
124 return -1;
127 bool skin_backdrops_preload(void)
129 bool retval = true;
130 int i;
131 char *filename;
132 for (i=0; i<NB_BDROPS; i++)
134 if (backdrops[i].name[0] && !backdrops[i].buffer)
136 size_t buf_size;
137 enum screen_type screen = backdrops[i].screen;
138 #if defined(HAVE_REMOTE_LCD) && (LCD_REMOTE_DEPTH > 1)
139 if (screen == SCREEN_REMOTE)
140 buf_size = REMOTE_LCD_BACKDROP_BYTES;
141 else
142 #endif
143 buf_size = LCD_BACKDROP_BYTES;
145 filename = backdrops[i].name;
146 if (screen == SCREEN_MAIN && global_settings.backdrop_file[0] &&
147 global_settings.backdrop_file[0] != '-' && filename[0] == '-')
149 filename = global_settings.backdrop_file;
151 if (*filename && *filename != '-')
153 backdrops[i].buflib_handle = core_alloc_ex(filename, buf_size, &buflib_ops);
154 if (backdrops[i].buflib_handle > 0)
156 backdrops[i].buffer = core_get_data(backdrops[i].buflib_handle);
157 handle_being_loaded = backdrops[i].buflib_handle;
158 backdrops[i].loaded =
159 screens[screen].backdrop_load(filename, backdrops[i].buffer);
160 handle_being_loaded = -1;
161 if (!backdrops[i].loaded)
162 retval = false;
164 else
165 retval = false;
167 if (backdrops[i].name[0] == '-' && backdrops[i].loaded)
168 backdrops[i].name[2] = '.';
171 return retval;
174 void skin_backdrop_show(int backdrop_id)
176 if (backdrop_id < 0)
177 return;
178 enum screen_type screen = backdrops[backdrop_id].screen;
179 if ((backdrops[backdrop_id].loaded == false) ||
180 (backdrops[backdrop_id].name[0] == '-' &&
181 backdrops[backdrop_id].name[2] == '\0'))
183 screens[screen].backdrop_show(NULL);
184 current_lcd_backdrop[screen] = -1;
186 else if (backdrops[backdrop_id].buffer)
188 screens[screen].backdrop_show(backdrops[backdrop_id].buffer);
189 current_lcd_backdrop[screen] = backdrop_id;
193 void skin_backdrop_unload(int backdrop_id)
195 if (backdrops[backdrop_id].buflib_handle > 0)
196 core_free(backdrops[backdrop_id].buflib_handle);
197 backdrops[backdrop_id].buffer = NULL;
198 backdrops[backdrop_id].buflib_handle = -1;
201 void skin_backdrop_load_setting(void)
203 int i;
204 for(i=0;i<SKINNABLE_SCREENS_COUNT*NB_SCREENS;i++)
206 if (backdrops[i].name[0] == '-' && backdrops[i].screen == SCREEN_MAIN)
208 if (global_settings.backdrop_file[0] &&
209 global_settings.backdrop_file[0] != '-')
211 if (!backdrops[i].buffer)
213 bool loaded;
214 backdrops[i].buflib_handle =
215 core_alloc_ex(global_settings.backdrop_file,
216 LCD_BACKDROP_BYTES, &buflib_ops);
217 if (backdrops[i].buflib_handle < 0)
218 return;
219 backdrops[i].buffer = core_get_data(backdrops[i].buflib_handle);
220 handle_being_loaded = backdrops[i].buflib_handle;
221 loaded = screens[SCREEN_MAIN].backdrop_load(
222 global_settings.backdrop_file,
223 backdrops[i].buffer);
224 handle_being_loaded = -1;
225 backdrops[i].name[2] = loaded ? '.' : '\0';
227 return;
229 else
230 backdrops[i].name[2] = '\0';
232 #if NB_SCREENS > 1
233 else if (backdrops[i].name[0] == '-')
235 backdrops[i].name[2] = '\0';
236 return;
238 #endif
241 #elif defined(__PCTOOL__)
243 int skin_backdrop_assign(char* backdrop, char *bmpdir,
244 enum screen_type screen)
246 (void)backdrop;
247 (void)bmpdir;
248 (void)screen;
249 return 0;
251 void skin_backdrop_unload(int backdrop_id)
253 (void)backdrop_id;
255 #else
257 void skin_backdrop_init(void)
260 #endif