Fix up configure and buildzip/wpsbuild.pl a bit
[kugel-rb.git] / apps / radio / presets.c
blobe900afe734dcb08561e967c0c2af820393652e92
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Linus Nielsen Feltzing
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 "general.h"
28 #include "radio.h"
29 #include "tuner.h"
30 #include "file.h"
31 #include "string-extra.h"
32 #include "misc.h"
33 #include "filefuncs.h"
34 #include "lang.h"
35 #include "action.h"
36 #include "list.h"
37 #include "splash.h"
38 #include "menu.h"
39 #include "yesno.h"
40 #include "keyboard.h"
41 #include "talk.h"
42 #include "filetree.h"
43 #include "dir.h"
44 #include "presets.h"
46 static int curr_preset = -1;
48 extern int curr_freq; /* from radio.c.. naughty but meh */
49 extern int radio_mode;
50 int snap_freq_to_grid(int freq);
51 void remember_frequency(void);
52 void talk_freq(int freq, bool enqueue);
54 #define MAX_PRESETS 64
55 static bool presets_loaded = false;
56 static bool presets_changed = false;
57 static struct fmstation presets[MAX_PRESETS];
59 static char filepreset[MAX_PATH]; /* preset filename variable */
61 static int num_presets = 0; /* The number of presets in the preset list */
63 bool yesno_pop(const char* text); /* radio.c */
65 int radio_current_preset(void)
67 return curr_preset;
69 int radio_preset_count(void)
71 return num_presets;
73 const struct fmstation *radio_get_preset(int preset)
75 return &presets[preset];
78 bool presets_have_changed(void)
80 return presets_changed;
84 /* Find a matching preset to freq */
85 int preset_find(int freq)
87 int i;
88 if(num_presets < 1)
89 return -1;
90 for(i = 0;i < MAX_PRESETS;i++)
92 if(freq == presets[i].frequency)
93 return i;
96 return -1;
99 /* Return the closest preset encountered in the search direction with
100 wraparound. */
101 static int find_closest_preset(int freq, int direction)
103 int i;
104 int lowpreset = 0;
105 int highpreset = 0;
106 int closest = -1;
108 if (direction == 0) /* direction == 0 isn't really used */
109 return 0;
111 for (i = 0; i < num_presets; i++)
113 int f = presets[i].frequency;
114 if (f == freq)
115 return i; /* Exact match = stop */
117 /* remember the highest and lowest presets for wraparound */
118 if (f < presets[lowpreset].frequency)
119 lowpreset = i;
120 if (f > presets[highpreset].frequency)
121 highpreset = i;
123 /* find the closest preset in the given direction */
124 if (direction > 0 && f > freq)
126 if (closest < 0 || f < presets[closest].frequency)
127 closest = i;
129 else if (direction < 0 && f < freq)
131 if (closest < 0 || f > presets[closest].frequency)
132 closest = i;
136 if (closest < 0)
138 /* no presets in the given direction */
139 /* wrap around depending on direction */
140 if (direction < 0)
141 closest = highpreset;
142 else
143 closest = lowpreset;
146 return closest;
149 void preset_next(int direction)
151 if (num_presets < 1)
152 return;
154 if (curr_preset == -1)
155 curr_preset = find_closest_preset(curr_freq, direction);
156 else
157 curr_preset = (curr_preset + direction + num_presets) % num_presets;
159 /* Must stay on the current grid for the region */
160 curr_freq = snap_freq_to_grid(presets[curr_preset].frequency);
162 tuner_set(RADIO_FREQUENCY, curr_freq);
163 remember_frequency();
166 void preset_set_current(int preset)
168 curr_preset = preset;
171 /* Speak a preset by number or by spelling its name, depending on settings. */
172 void preset_talk(int preset, bool fallback, bool enqueue)
174 if (global_settings.talk_file == 1) /* number */
175 talk_number(preset + 1, enqueue);
176 else
177 { /* spell */
178 if(presets[preset].name[0])
179 talk_spell(presets[preset].name, enqueue);
180 else if(fallback)
181 talk_freq(presets[preset].frequency, enqueue);
186 void radio_save_presets(void)
188 int fd;
189 int i;
191 fd = creat(filepreset, 0666);
192 if(fd >= 0)
194 for(i = 0;i < num_presets;i++)
196 fdprintf(fd, "%d:%s\n", presets[i].frequency, presets[i].name);
198 close(fd);
200 if(!strncasecmp(FMPRESET_PATH, filepreset, strlen(FMPRESET_PATH)))
201 set_file(filepreset, global_settings.fmr_file, MAX_FILENAME);
202 presets_changed = false;
204 else
206 splash(HZ, ID2P(LANG_FM_PRESET_SAVE_FAILED));
210 void radio_load_presets(char *filename)
212 int fd;
213 int rc;
214 char buf[128];
215 char *freq;
216 char *name;
217 bool done = false;
218 int f;
220 memset(presets, 0, sizeof(presets));
221 num_presets = 0;
223 /* No Preset in configuration. */
224 if(filename[0] == '\0')
226 filepreset[0] = '\0';
227 return;
229 /* Temporary preset, loaded until player shuts down. */
230 else if(filename[0] == '/')
231 strlcpy(filepreset, filename, sizeof(filepreset));
232 /* Preset from default directory. */
233 else
234 snprintf(filepreset, sizeof(filepreset), "%s/%s.fmr",
235 FMPRESET_PATH, filename);
237 fd = open_utf8(filepreset, O_RDONLY);
238 if(fd >= 0)
240 while(!done && num_presets < MAX_PRESETS)
242 rc = read_line(fd, buf, 128);
243 if(rc > 0)
245 if(settings_parseline(buf, &freq, &name))
247 f = atoi(freq);
248 if(f) /* For backwards compatibility */
250 struct fmstation * const fms = &presets[num_presets];
251 fms->frequency = f;
252 strlcpy(fms->name, name, MAX_FMPRESET_LEN+1);
253 num_presets++;
257 else
258 done = true;
260 close(fd);
262 else /* invalid file name? */
263 filepreset[0] = '\0';
265 presets_loaded = num_presets > 0;
266 presets_changed = false;
269 const char* radio_get_preset_name(int preset)
271 if (preset < num_presets)
272 return presets[preset].name;
273 return NULL;
276 int handle_radio_add_preset(void)
278 char buf[MAX_FMPRESET_LEN + 1];
280 if(num_presets < MAX_PRESETS)
282 buf[0] = '\0';
284 if (!kbd_input(buf, MAX_FMPRESET_LEN + 1))
286 struct fmstation * const fms = &presets[num_presets];
287 strcpy(fms->name, buf);
288 fms->frequency = curr_freq;
289 num_presets++;
290 presets_changed = true;
291 presets_loaded = num_presets > 0;
292 return true;
295 else
297 splash(HZ, ID2P(LANG_FM_NO_FREE_PRESETS));
299 return false;
302 /* needed to know which preset we are edit/delete-ing */
303 static int selected_preset = -1;
304 static int radio_edit_preset(void)
306 char buf[MAX_FMPRESET_LEN + 1];
308 if (num_presets > 0)
310 struct fmstation * const fms = &presets[selected_preset];
312 strcpy(buf, fms->name);
314 if (!kbd_input(buf, MAX_FMPRESET_LEN + 1))
316 strcpy(fms->name, buf);
317 presets_changed = true;
321 return 1;
324 static int radio_delete_preset(void)
326 if (num_presets > 0)
328 struct fmstation * const fms = &presets[selected_preset];
330 if (selected_preset >= --num_presets)
331 selected_preset = num_presets - 1;
333 memmove(fms, fms + 1, (uintptr_t)(fms + num_presets) -
334 (uintptr_t)fms);
336 if (curr_preset >= num_presets)
337 --curr_preset;
340 /* Don't ask to save when all presets are deleted. */
341 presets_changed = num_presets > 0;
343 if (!presets_changed)
345 /* The preset list will be cleared, switch to Scan Mode. */
346 radio_mode = RADIO_SCAN_MODE;
347 curr_preset = -1;
348 presets_loaded = false;
351 return 1;
354 int preset_list_load(void)
356 return !rockbox_browse(FMPRESET_PATH, SHOW_FMR);
359 int preset_list_save(void)
361 if(num_presets > 0)
363 bool bad_file_name = true;
365 if(!dir_exists(FMPRESET_PATH)) /* Check if there is preset folder */
366 mkdir(FMPRESET_PATH);
368 create_numbered_filename(filepreset, FMPRESET_PATH, "preset",
369 ".fmr", 2 IF_CNFN_NUM_(, NULL));
371 while(bad_file_name)
373 if(!kbd_input(filepreset, sizeof(filepreset)))
375 /* check the name: max MAX_FILENAME (20) chars */
376 char* p2;
377 char* p1;
378 int len;
379 p1 = strrchr(filepreset, '/');
380 p2 = p1;
381 while((p1) && (*p2) && (*p2 != '.'))
382 p2++;
383 len = (int)(p2-p1) - 1;
384 if((!p1) || (len > MAX_FILENAME) || (len == 0))
386 /* no slash, too long or too short */
387 splash(HZ, ID2P(LANG_INVALID_FILENAME));
389 else
391 /* add correct extension (easier to always write)
392 at this point, p2 points to 0 or the extension dot */
393 *p2 = '\0';
394 strcat(filepreset,".fmr");
395 bad_file_name = false;
396 radio_save_presets();
399 else
401 /* user aborted */
402 return false;
406 else
407 splash(HZ, ID2P(LANG_FM_NO_PRESETS));
409 return true;
412 int preset_list_clear(void)
414 /* Clear all the preset entries */
415 memset(presets, 0, sizeof (presets));
417 num_presets = 0;
418 presets_loaded = false;
419 /* The preset list will be cleared switch to Scan Mode. */
420 radio_mode = RADIO_SCAN_MODE;
421 curr_preset = -1;
422 presets_changed = false; /* Don't ask to save when clearing the list. */
424 return true;
427 MENUITEM_FUNCTION(radio_edit_preset_item, MENU_FUNC_CHECK_RETVAL,
428 ID2P(LANG_FM_EDIT_PRESET),
429 radio_edit_preset, NULL, NULL, Icon_NOICON);
430 MENUITEM_FUNCTION(radio_delete_preset_item, MENU_FUNC_CHECK_RETVAL,
431 ID2P(LANG_FM_DELETE_PRESET),
432 radio_delete_preset, NULL, NULL, Icon_NOICON);
433 static int radio_preset_callback(int action,
434 const struct menu_item_ex *this_item)
436 if (action == ACTION_STD_OK)
437 action = ACTION_EXIT_AFTER_THIS_MENUITEM;
438 return action;
439 (void)this_item;
441 MAKE_MENU(handle_radio_preset_menu, ID2P(LANG_PRESET),
442 radio_preset_callback, Icon_NOICON, &radio_edit_preset_item,
443 &radio_delete_preset_item);
444 /* present a list of preset stations */
445 static const char* presets_get_name(int selected_item, void *data,
446 char *buffer, size_t buffer_len)
448 (void)data;
449 struct fmstation *p = &presets[selected_item];
450 if(p->name[0])
451 return p->name;
452 int freq = p->frequency / 10000;
453 int frac = freq % 100;
454 freq /= 100;
455 snprintf(buffer, buffer_len,
456 str(LANG_FM_DEFAULT_PRESET_NAME), freq, frac);
457 return buffer;
460 static int presets_speak_name(int selected_item, void * data)
462 (void)data;
463 preset_talk(selected_item, true, false);
464 return 0;
467 int handle_radio_presets(void)
469 struct gui_synclist lists;
470 int result = 0;
471 int action = ACTION_NONE;
472 #ifdef HAVE_BUTTONBAR
473 struct gui_buttonbar buttonbar;
474 #endif
476 if(presets_loaded == false)
477 return result;
479 #ifdef HAVE_BUTTONBAR
480 gui_buttonbar_init(&buttonbar);
481 gui_buttonbar_set_display(&buttonbar, &(screens[SCREEN_MAIN]) );
482 gui_buttonbar_set(&buttonbar, str(LANG_FM_BUTTONBAR_ADD),
483 str(LANG_FM_BUTTONBAR_EXIT),
484 str(LANG_FM_BUTTONBAR_ACTION));
485 gui_buttonbar_draw(&buttonbar);
486 #endif
487 gui_synclist_init(&lists, presets_get_name, NULL, false, 1, NULL);
488 gui_synclist_set_title(&lists, str(LANG_PRESET), NOICON);
489 gui_synclist_set_icon_callback(&lists, NULL);
490 if(global_settings.talk_file)
491 gui_synclist_set_voice_callback(&lists, presets_speak_name);
492 gui_synclist_set_nb_items(&lists, num_presets);
493 gui_synclist_select_item(&lists, curr_preset<0 ? 0 : curr_preset);
494 gui_synclist_speak_item(&lists);
496 while (result == 0)
498 gui_synclist_draw(&lists);
499 list_do_action(CONTEXT_STD, TIMEOUT_BLOCK,
500 &lists, &action, LIST_WRAP_UNLESS_HELD);
501 switch (action)
503 case ACTION_STD_MENU:
504 if (handle_radio_add_preset())
506 gui_synclist_set_nb_items(&lists, num_presets);
507 gui_synclist_select_item(&lists, num_presets - 1);
509 break;
510 case ACTION_STD_CANCEL:
511 result = 1;
512 break;
513 case ACTION_STD_OK:
514 curr_preset = gui_synclist_get_sel_pos(&lists);
515 curr_freq = presets[curr_preset].frequency;
516 next_station(0);
517 remember_frequency();
518 result = 1;
519 break;
520 case ACTION_F3:
521 case ACTION_STD_CONTEXT:
522 selected_preset = gui_synclist_get_sel_pos(&lists);
523 do_menu(&handle_radio_preset_menu, NULL, NULL, false);
524 gui_synclist_set_nb_items(&lists, num_presets);
525 gui_synclist_select_item(&lists, selected_preset);
526 gui_synclist_speak_item(&lists);
527 break;
528 default:
529 if(default_event_handler(action) == SYS_USB_CONNECTED)
530 result = 2;
533 return result - 1;
537 int presets_scan(void *viewports)
539 bool do_scan = true;
540 int i;
541 struct viewport *vp = (struct viewport *)viewports;
543 FOR_NB_SCREENS(i)
544 screens[i].set_viewport(vp?&vp[i]:NULL);
545 if(num_presets > 0) /* Do that to avoid 2 questions. */
546 do_scan = yesno_pop(ID2P(LANG_FM_CLEAR_PRESETS));
548 if(do_scan)
550 const struct fm_region_data * const fmr =
551 &fm_region_data[global_settings.fm_region];
553 curr_freq = fmr->freq_min;
554 num_presets = 0;
555 memset(presets, 0, sizeof(presets));
557 tuner_set(RADIO_MUTE, 1);
559 while(curr_freq <= fmr->freq_max)
561 int freq, frac;
562 if(num_presets >= MAX_PRESETS || action_userabort(TIMEOUT_NOBLOCK))
563 break;
565 freq = curr_freq / 10000;
566 frac = freq % 100;
567 freq /= 100;
569 splashf(0, str(LANG_FM_SCANNING), freq, frac);
571 if(tuner_set(RADIO_SCAN_FREQUENCY, curr_freq))
573 /* add preset */
574 presets[num_presets].name[0] = '\0';
575 presets[num_presets].frequency = curr_freq;
576 num_presets++;
579 curr_freq += fmr->freq_step;
582 if (get_radio_status() == FMRADIO_PLAYING)
583 tuner_set(RADIO_MUTE, 0);
585 presets_changed = true;
587 FOR_NB_SCREENS(i)
589 screens[i].clear_viewport();
590 screens[i].update_viewport();
593 if(num_presets > 0)
595 curr_freq = presets[0].frequency;
596 radio_mode = RADIO_PRESET_MODE;
597 presets_loaded = true;
598 next_station(0);
600 else
602 /* Wrap it to beginning or we'll be past end of band */
603 presets_loaded = false;
604 next_station(1);
607 return true;
611 void presets_save(void)
613 if(filepreset[0] == '\0')
614 preset_list_save();
615 else
616 radio_save_presets();
619 #ifdef HAVE_LCD_BITMAP
620 static inline void draw_vertical_line_mark(struct screen * screen,
621 int x, int y, int h)
623 screen->set_drawmode(DRMODE_COMPLEMENT);
624 screen->vline(x, y, y+h-1);
627 /* draw the preset markers for a track of length "tracklen",
628 between (x,y) and (x+w,y) */
629 void presets_draw_markers(struct screen *screen,
630 int x, int y, int w, int h)
632 int i,xi;
633 const struct fm_region_data *region_data =
634 &(fm_region_data[global_settings.fm_region]);
635 int len = region_data->freq_max - region_data->freq_min;
636 for (i=0; i < radio_preset_count(); i++)
638 int freq = radio_get_preset(i)->frequency;
639 int diff = freq - region_data->freq_min;
640 xi = x + (w * diff)/len;
641 draw_vertical_line_mark(screen, xi, y, h);
644 #endif