Merge tag 'v3.13-final' into maemo-port
[maemo-rb.git] / apps / radio / radioart.c
blob53ed863b3b332138a4cf3f863baf9316c34a629c
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 char* buf;
45 static struct radioart radioart[MAX_RADIOART_IMAGES];
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 struct bufopen_bitmap_data user_data;
67 #ifndef HAVE_NOISY_IDLE_MODE
68 cpu_idle_mode(false);
69 #endif
70 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.bmp",preset_name);
71 if (!file_exists(path))
72 snprintf(path, sizeof(path), FMPRESET_PATH "/%s.jpg",preset_name);
73 if (!file_exists(path))
75 #ifndef HAVE_NOISY_IDLE_MODE
76 cpu_idle_mode(true);
77 #endif
78 return -1;
80 strlcpy(ra->name, preset_name, MAX_FMPRESET_LEN+1);
81 ra->dim.height = dim->height;
82 ra->dim.width = dim->width;
83 ra->last_tick = current_tick;
84 user_data.embedded_albumart = NULL;
85 user_data.dim = &ra->dim;
86 ra->handle = bufopen(path, 0, TYPE_BITMAP, &user_data);
87 if (ra->handle == ERR_BUFFER_FULL)
89 int i = find_oldest_image();
90 bufclose(i);
91 ra->handle = bufopen(path, 0, TYPE_BITMAP, &ra->dim);
93 #ifndef HAVE_NOISY_IDLE_MODE
94 cpu_idle_mode(true);
95 #endif
96 return ra->handle;
98 int radio_get_art_hid(struct dim *requested_dim)
100 int preset = radio_current_preset();
101 int free_idx = -1;
102 const char* preset_name;
104 if (!buf || radio_scan_mode() || preset < 0)
105 return -1;
107 preset_name = radio_get_preset_name(preset);
108 for (int i=0; i<MAX_RADIOART_IMAGES; i++)
110 if (radioart[i].handle < 0)
112 free_idx = i;
114 else if (!strcmp(radioart[i].name, preset_name) &&
115 radioart[i].dim.width == requested_dim->width &&
116 radioart[i].dim.height == requested_dim->height)
118 radioart[i].last_tick = current_tick;
119 return radioart[i].handle;
122 if (free_idx >= 0)
124 return load_radioart_image(&radioart[free_idx],
125 preset_name, requested_dim);
127 else
129 int i = find_oldest_image();
130 bufclose(radioart[i].handle);
131 return load_radioart_image(&radioart[i],
132 preset_name, requested_dim);
135 return -1;
138 static void buffer_reset_handler(void *data)
140 buf = NULL;
141 for(int i=0;i<MAX_RADIOART_IMAGES;i++)
143 if (radioart[i].handle >= 0)
144 bufclose(radioart[i].handle);
145 radioart[i].handle = -1;
146 radioart[i].name[0] = '\0';
149 (void)data;
152 void radioart_init(bool entering_screen)
154 if (entering_screen)
156 /* grab control over buffering */
157 size_t bufsize;
158 buf = audio_get_buffer(false, &bufsize);
159 buffering_reset(buf, bufsize);
160 /* one-shot */
161 add_event(BUFFER_EVENT_BUFFER_RESET, true, buffer_reset_handler);
163 else /* init at startup */
165 for(int i=0;i<MAX_RADIOART_IMAGES;i++)
167 radioart[i].handle = -1;
168 radioart[i].name[0] = '\0';