summaryrefslogtreecommitdiff
path: root/apps/radio/radioart.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/radio/radioart.c')
-rw-r--r--apps/radio/radioart.c180
1 files changed, 180 insertions, 0 deletions
diff --git a/apps/radio/radioart.c b/apps/radio/radioart.c
new file mode 100644
index 0000000000..319f254144
--- /dev/null
+++ b/apps/radio/radioart.c
@@ -0,0 +1,180 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: radio.c -1 $
9 *
10 * Copyright (C) 2010 Jonathan Gordon
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
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
34#define MAX_RADIOART_IMAGES 10
35struct radioart {
36 int handle;
37 long last_tick;
38 struct dim dim;
39 char name[MAX_FMPRESET_LEN+1];
40};
41
42static struct radioart radioart[MAX_RADIOART_IMAGES];
43#ifdef HAVE_RECORDING
44static bool allow_buffer_access = true; /* If we are recording dont touch the buffers! */
45#endif
46static int find_oldest_image(void)
47{
48 int i;
49 long oldest_tick = radioart[0].last_tick;
50 int oldest_idx = 0;
51 for(i=1;i<MAX_RADIOART_IMAGES;i++)
52 {
53 if (radioart[i].last_tick < oldest_tick)
54 {
55 oldest_tick = radioart[i].last_tick;
56 oldest_idx = i;
57 }
58 }
59 return oldest_idx;
60}
61static int load_radioart_image(struct radioart *ra, const char* preset_name,
62 struct dim *dim)
63{
64 char path[MAX_PATH];
65#ifndef HAVE_NOISY_IDLE_MODE
66 cpu_idle_mode(false);
67#endif
68 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.bmp",preset_name);
69 if (!file_exists(path))
70 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.jpg",preset_name);
71 if (!file_exists(path))
72 {
73#ifndef HAVE_NOISY_IDLE_MODE
74 cpu_idle_mode(true);
75#endif
76 return -1;
77 }
78 strlcpy(ra->name, preset_name, MAX_FMPRESET_LEN+1);
79 ra->dim.height = dim->height;
80 ra->dim.width = dim->width;
81 ra->last_tick = current_tick;
82 ra->handle = bufopen(path, 0, TYPE_BITMAP, &ra->dim);
83 if (ra->handle == ERR_BUFFER_FULL)
84 {
85 int i = find_oldest_image();
86 bufclose(i);
87 ra->handle = bufopen(path, 0, TYPE_BITMAP, &ra->dim);
88 }
89#ifndef HAVE_NOISY_IDLE_MODE
90 cpu_idle_mode(true);
91#endif
92 return ra->handle;
93}
94int radio_get_art_hid(struct dim *requested_dim)
95{
96 int preset = radio_current_preset();
97 int i, free_idx = -1;
98 const char* preset_name;
99 if (radio_scan_mode() || preset < 0)
100 return -1;
101#ifdef HAVE_RECORDING
102 if (!allow_buffer_access)
103 return -1;
104#endif
105 preset_name = radio_get_preset_name(preset);
106 for(i=0;i<MAX_RADIOART_IMAGES;i++)
107 {
108 if (radioart[i].handle < 0)
109 {
110 free_idx = i;
111 }
112 else if (!strcmp(radioart[i].name, preset_name) &&
113 radioart[i].dim.width == requested_dim->width &&
114 radioart[i].dim.height == requested_dim->height)
115 {
116 radioart[i].last_tick = current_tick;
117 return radioart[i].handle;
118 }
119 }
120 if (free_idx >= 0)
121 {
122 return load_radioart_image(&radioart[free_idx],
123 preset_name, requested_dim);
124 }
125 else
126 {
127 int i = find_oldest_image();
128 bufclose(radioart[i].handle);
129 return load_radioart_image(&radioart[i],
130 preset_name, requested_dim);
131 }
132
133 return -1;
134}
135static void playback_restarting_handler(void *data)
136{
137 (void)data;
138 int i;
139 for(i=0;i<MAX_RADIOART_IMAGES;i++)
140 {
141 if (radioart[i].handle >= 0)
142 bufclose(radioart[i].handle);
143 radioart[i].handle = -1;
144 radioart[i].name[0] = '\0';
145 }
146}
147#ifdef HAVE_RECORDING
148static void recording_started_handler(void *data)
149{
150 (void)data;
151 allow_buffer_access = false;
152 playback_restarting_handler(NULL);
153}
154static void recording_stopped_handler(void *data)
155{
156 (void)data;
157 allow_buffer_access = true;
158}
159#endif
160
161void radioart_init(bool entering_screen)
162{
163 int i;
164 if (entering_screen)
165 {
166 for(i=0;i<MAX_RADIOART_IMAGES;i++)
167 {
168 radioart[i].handle = -1;
169 radioart[i].name[0] = '\0';
170 }
171 add_event(PLAYBACK_EVENT_START_PLAYBACK, true, playback_restarting_handler);
172 }
173 else
174 {
175#if defined(HAVE_RECORDING)
176 add_event(RECORDING_EVENT_START, false, recording_started_handler);
177 add_event(RECORDING_EVENT_STOP, false, recording_stopped_handler);
178#endif
179 }
180}