Revert 1207c7b and fix the actual warnings.
[maemo-rb.git] / apps / radio / radioart.c
blob07fab814ddbb06d89f94082fcbec83a6c23890f6
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 <stdbool.h>
25 #include <stdlib.h>
26 #include "system.h"
27 #include "settings.h"
28 #include "radio.h"
29 #include "buffering.h"
30 #include "playback.h" /* bufopen_user_data */
31 #include "file.h"
32 #include "kernel.h"
33 #include "string-extra.h"
34 #include "filefuncs.h"
36 #define MAX_RADIOART_IMAGES 10
37 struct radioart {
38 int handle;
39 long last_tick;
40 struct dim dim;
41 char name[MAX_FMPRESET_LEN+1];
44 static struct radioart radioart[MAX_RADIOART_IMAGES];
45 #ifdef HAVE_RECORDING
46 static bool allow_buffer_access = true; /* If we are recording dont touch the buffers! */
47 #endif
48 static int find_oldest_image(void)
50 int i;
51 long oldest_tick = radioart[0].last_tick;
52 int oldest_idx = 0;
53 for(i=1;i<MAX_RADIOART_IMAGES;i++)
55 if (radioart[i].last_tick < oldest_tick)
57 oldest_tick = radioart[i].last_tick;
58 oldest_idx = i;
61 return oldest_idx;
63 static int load_radioart_image(struct radioart *ra, const char* preset_name,
64 struct dim *dim)
66 char path[MAX_PATH];
67 struct bufopen_bitmap_data user_data;
68 #ifndef HAVE_NOISY_IDLE_MODE
69 cpu_idle_mode(false);
70 #endif
71 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.bmp",preset_name);
72 if (!file_exists(path))
73 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.jpg",preset_name);
74 if (!file_exists(path))
76 #ifndef HAVE_NOISY_IDLE_MODE
77 cpu_idle_mode(true);
78 #endif
79 return -1;
81 strlcpy(ra->name, preset_name, MAX_FMPRESET_LEN+1);
82 ra->dim.height = dim->height;
83 ra->dim.width = dim->width;
84 ra->last_tick = current_tick;
85 user_data.embedded_albumart = NULL;
86 user_data.dim = &ra->dim;
87 ra->handle = bufopen(path, 0, TYPE_BITMAP, &user_data);
88 if (ra->handle == ERR_BUFFER_FULL)
90 int i = find_oldest_image();
91 bufclose(i);
92 ra->handle = bufopen(path, 0, TYPE_BITMAP, &ra->dim);
94 #ifndef HAVE_NOISY_IDLE_MODE
95 cpu_idle_mode(true);
96 #endif
97 return ra->handle;
99 int radio_get_art_hid(struct dim *requested_dim)
101 int preset = radio_current_preset();
102 int free_idx = -1;
103 const char* preset_name;
104 if (radio_scan_mode() || preset < 0)
105 return -1;
106 #ifdef HAVE_RECORDING
107 if (!allow_buffer_access)
108 return -1;
109 #endif
110 preset_name = radio_get_preset_name(preset);
111 for (int i=0; i<MAX_RADIOART_IMAGES; i++)
113 if (radioart[i].handle < 0)
115 free_idx = i;
117 else if (!strcmp(radioart[i].name, preset_name) &&
118 radioart[i].dim.width == requested_dim->width &&
119 radioart[i].dim.height == requested_dim->height)
121 radioart[i].last_tick = current_tick;
122 return radioart[i].handle;
125 if (free_idx >= 0)
127 return load_radioart_image(&radioart[free_idx],
128 preset_name, requested_dim);
130 else
132 int i = find_oldest_image();
133 bufclose(radioart[i].handle);
134 return load_radioart_image(&radioart[i],
135 preset_name, requested_dim);
138 return -1;
140 static void playback_restarting_handler(void *data)
142 (void)data;
143 int i;
144 for(i=0;i<MAX_RADIOART_IMAGES;i++)
146 if (radioart[i].handle >= 0)
147 bufclose(radioart[i].handle);
148 radioart[i].handle = -1;
149 radioart[i].name[0] = '\0';
152 #ifdef HAVE_RECORDING
153 static void recording_started_handler(void *data)
155 (void)data;
156 allow_buffer_access = false;
157 playback_restarting_handler(NULL);
159 static void recording_stopped_handler(void *data)
161 (void)data;
162 allow_buffer_access = true;
164 #endif
166 void radioart_init(bool entering_screen)
168 int i;
169 if (entering_screen)
171 for(i=0;i<MAX_RADIOART_IMAGES;i++)
173 radioart[i].handle = -1;
174 radioart[i].name[0] = '\0';
176 add_event(PLAYBACK_EVENT_START_PLAYBACK, true, playback_restarting_handler);
178 /* grab control over buffering */
179 char* buf;
180 size_t bufsize;
181 buf = audio_get_buffer(false, &bufsize);
182 buffering_reset(buf, bufsize);
184 else
186 #if defined(HAVE_RECORDING)
187 add_event(RECORDING_EVENT_START, false, recording_started_handler);
188 add_event(RECORDING_EVENT_STOP, false, recording_stopped_handler);
189 #endif