MPEGPlayer: Skip to next file when there is a problem with a video file in all-play...
[kugel-rb.git] / apps / radio / radioart.c
blob85397c16b6b02d01d560badcf9818559b2d3ecd6
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 "settings.h"
27 #include "radio.h"
28 #include "buffering.h"
29 #include "file.h"
30 #include "kernel.h"
31 #include "string-extra.h"
32 #include "misc.h"
33 #include "filefuncs.h"
35 #define MAX_RADIOART_IMAGES 10
36 struct radioart {
37 int handle;
38 long last_tick;
39 struct dim dim;
40 char name[MAX_FMPRESET_LEN+1];
43 static struct radioart radioart[MAX_RADIOART_IMAGES];
44 #ifdef HAVE_RECORDING
45 static bool allow_buffer_access = true; /* If we are recording dont touch the buffers! */
46 #endif
47 static int find_oldest_image(void)
49 int i;
50 long oldest_tick = radioart[0].last_tick;
51 int oldest_idx = 0;
52 for(i=1;i<MAX_RADIOART_IMAGES;i++)
54 if (radioart[i].last_tick < oldest_tick)
56 oldest_tick = radioart[i].last_tick;
57 oldest_idx = i;
60 return oldest_idx;
62 static int load_radioart_image(struct radioart *ra, const char* preset_name,
63 struct dim *dim)
65 char path[MAX_PATH];
66 #ifndef HAVE_NOISY_IDLE_MODE
67 cpu_idle_mode(false);
68 #endif
69 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.bmp",preset_name);
70 if (!file_exists(path))
71 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.jpg",preset_name);
72 if (!file_exists(path))
74 #ifndef HAVE_NOISY_IDLE_MODE
75 cpu_idle_mode(true);
76 #endif
77 return -1;
79 strlcpy(ra->name, preset_name, MAX_FMPRESET_LEN+1);
80 ra->dim.height = dim->height;
81 ra->dim.width = dim->width;
82 ra->last_tick = current_tick;
83 ra->handle = bufopen(path, 0, TYPE_BITMAP, &ra->dim);
84 if (ra->handle == ERR_BUFFER_FULL)
86 int i = find_oldest_image();
87 bufclose(i);
88 ra->handle = bufopen(path, 0, TYPE_BITMAP, &ra->dim);
90 #ifndef HAVE_NOISY_IDLE_MODE
91 cpu_idle_mode(true);
92 #endif
93 return ra->handle;
95 int radio_get_art_hid(struct dim *requested_dim)
97 int preset = radio_current_preset();
98 int i, free_idx = -1;
99 const char* preset_name;
100 if (radio_scan_mode() || preset < 0)
101 return -1;
102 #ifdef HAVE_RECORDING
103 if (!allow_buffer_access)
104 return -1;
105 #endif
106 preset_name = radio_get_preset_name(preset);
107 for(i=0;i<MAX_RADIOART_IMAGES;i++)
109 if (radioart[i].handle < 0)
111 free_idx = i;
113 else if (!strcmp(radioart[i].name, preset_name) &&
114 radioart[i].dim.width == requested_dim->width &&
115 radioart[i].dim.height == requested_dim->height)
117 radioart[i].last_tick = current_tick;
118 return radioart[i].handle;
121 if (free_idx >= 0)
123 return load_radioart_image(&radioart[free_idx],
124 preset_name, requested_dim);
126 else
128 int i = find_oldest_image();
129 bufclose(radioart[i].handle);
130 return load_radioart_image(&radioart[i],
131 preset_name, requested_dim);
134 return -1;
136 static void playback_restarting_handler(void *data)
138 (void)data;
139 int i;
140 for(i=0;i<MAX_RADIOART_IMAGES;i++)
142 if (radioart[i].handle >= 0)
143 bufclose(radioart[i].handle);
144 radioart[i].handle = -1;
145 radioart[i].name[0] = '\0';
148 #ifdef HAVE_RECORDING
149 static void recording_started_handler(void *data)
151 (void)data;
152 allow_buffer_access = false;
153 playback_restarting_handler(NULL);
155 static void recording_stopped_handler(void *data)
157 (void)data;
158 allow_buffer_access = true;
160 #endif
162 void radioart_init(bool entering_screen)
164 int i;
165 if (entering_screen)
167 for(i=0;i<MAX_RADIOART_IMAGES;i++)
169 radioart[i].handle = -1;
170 radioart[i].name[0] = '\0';
172 add_event(PLAYBACK_EVENT_START_PLAYBACK, true, playback_restarting_handler);
174 else
176 #if defined(HAVE_RECORDING)
177 add_event(RECORDING_EVENT_START, false, recording_started_handler);
178 add_event(RECORDING_EVENT_STOP, false, recording_stopped_handler);
179 #endif