summaryrefslogtreecommitdiffstats
path: root/apps/root_menu.c
blob: abee02dbf781b47da6acf0e252509a2f95cc6d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: main.c 12101 2007-01-24 02:19:22Z jdgordon $
 *
 * Copyright (C) 2007 Jonathan Gordon
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include "config.h"
#include "menu.h"
#include "root_menu.h"
#include "lang.h"
#include "settings.h"
#include "kernel.h"
#include "debug.h"
#include "misc.h"
#include "rolo.h"
#include "powermgmt.h"

#if LCD_DEPTH > 1
#include "backdrop.h"
#endif
#include "talk.h"
#include "audio.h"

/* gui api */
#include "list.h"
#include "statusbar.h"
#include "splash.h"
#include "buttonbar.h"
#include "textarea.h"
#include "action.h"
#include "yesno.h"

#include "main_menu.h"
#include "tree.h"
#if CONFIG_TUNER
#include "radio.h"
#endif
#ifdef HAVE_RECORDING
#include "recording.h"
#endif
#include "gwps-common.h"
#include "bookmark.h"
#include "tagtree.h"
#include "menus/exported_menus.h"
#ifdef HAVE_RTC_ALARM
#include "rtc.h"
#endif

struct root_items {
    int (*function)(void* param);
    void* param;
};
static int last_screen = GO_TO_ROOT; /* unfortunatly needed so we can resume
                                        or goto current track based on previous
                                        screen */
static int browser(void* param)
{
    int ret_val;
#ifdef HAVE_TAGCACHE
    struct tree_context* tc = tree_get_context();
#endif
    int filter = SHOW_SUPPORTED;
    char folder[MAX_PATH] = "/";
    /* stuff needed to remember position in file browser */
    static char last_folder[MAX_PATH] = "/";
    /* and stuff for the database browser */
#ifdef HAVE_TAGCACHE
    static int last_db_dirlevel = 0;
#endif
    
    switch ((intptr_t)param)
    {
        case GO_TO_FILEBROWSER:
            filter = global_settings.dirfilter;
            if (global_settings.browse_current && 
                    last_screen == GO_TO_WPS && audio_status() &&
                    wps_state.current_track_path[0] != '\0')
            {
                strcpy(folder, wps_state.current_track_path);
            }
            else
                strcpy(folder, last_folder);
        break;
#ifdef HAVE_TAGCACHE
        case GO_TO_DBBROWSER:
            if ((last_screen != GO_TO_ROOT) && !tagcache_is_usable())
            {
                gui_syncsplash(HZ, true, str(LANG_TAGCACHE_BUSY));
                return GO_TO_PREVIOUS;
            }
            filter = SHOW_ID3DB;
            tc->dirlevel = last_db_dirlevel;
        break;
#endif
        case GO_TO_BROWSEPLUGINS:
            filter = SHOW_PLUGINS;
            snprintf(folder, MAX_PATH, "%s/", PLUGIN_DIR);
        break;
    }
    ret_val = rockbox_browse(folder, filter);
    switch ((intptr_t)param)
    {
        case GO_TO_FILEBROWSER:
            get_current_file(last_folder, MAX_PATH);
        break;
#ifdef HAVE_TAGCACHE
        case GO_TO_DBBROWSER:
            last_db_dirlevel = tc->dirlevel;
        break;
#endif
    }
    /* hopefully only happens trying to go back into the WPS
       from plugins, if music is stopped... */
    if ((ret_val == GO_TO_PREVIOUS) && (last_screen == (intptr_t)param))
        ret_val = GO_TO_ROOT;

    return ret_val;
}  

static int menu(void* param)
{
    (void)param;
    return main_menu();
    
}
#ifdef HAVE_RECORDING
static int recscrn(void* param)
{
    (void)param;
    recording_screen(false);
    return GO_TO_ROOT;
}
#endif
static int wpsscrn(void* param)
{
    int ret_val = GO_TO_PREVIOUS;
    (void)param;
    if (audio_status())
    {
        ret_val = gui_wps_show();
    }
    else if ( global_status.resume_index != -1 )
    {
        DEBUGF("Resume index %X offset %X\n",
               global_status.resume_index,
               global_status.resume_offset);
        if (playlist_resume() != -1)
        {
            playlist_start(global_status.resume_index,
                global_status.resume_offset);
            ret_val = gui_wps_show();
        }
    }
    else 
    {
        gui_syncsplash(HZ*2, true, str(LANG_NOTHING_TO_RESUME));
    }
#if LCD_DEPTH > 1
    show_main_backdrop();
#endif
    return ret_val;
}
#if CONFIG_TUNER
static int radio(void* param)
{
    (void)param;
    radio_screen();
    return GO_TO_ROOT;
}
#endif

static int load_bmarks(void* param)
{
    (void)param;
    bookmark_mrb_load();
    return GO_TO_PREVIOUS;
}

static const struct root_items items[] = {
    [GO_TO_FILEBROWSER] =   { browser, (void*)GO_TO_FILEBROWSER },
    [GO_TO_DBBROWSER] =     { browser, (void*)GO_TO_DBBROWSER },
    [GO_TO_WPS] =           { wpsscrn, NULL },
    [GO_TO_MAINMENU] =      { menu, NULL },
    
#ifdef HAVE_RECORDING
    [GO_TO_RECSCREEN] =     {  recscrn, NULL },
#endif
    
#if CONFIG_TUNER
    [GO_TO_FM] =            { radio, NULL },
#endif
    
    [GO_TO_RECENTBMARKS] =  { load_bmarks, NULL }, 
    [GO_TO_BROWSEPLUGINS] = { browser, (void*)GO_TO_BROWSEPLUGINS }, 
    
};
static const int nb_items = sizeof(items)/sizeof(*items);

#ifdef BOOTFILE
extern bool boot_changed; /* from tree.c */
static void check_boot(void)
{
    if (boot_changed) {
        char *lines[]={str(LANG_BOOT_CHANGED), str(LANG_REBOOT_NOW)};
        struct text_message message={lines, 2};
        if(gui_syncyesno_run(&message, NULL, NULL)==YESNO_YES)
            rolo_load("/" BOOTFILE);
        boot_changed = false;
    }
}
#else
# define check_boot()
#endif
int item_callback(int action, const struct menu_item_ex *this_item) ;

MENUITEM_RETURNVALUE(file_browser, ID2P(LANG_DIR_BROWSER), GO_TO_FILEBROWSER,
                        NULL, Icon_file_view_menu);
#ifdef HAVE_TAGCACHE
MENUITEM_RETURNVALUE(db_browser, ID2P(LANG_TAGCACHE), GO_TO_DBBROWSER, 
                        NULL, Icon_Audio);
#endif
MENUITEM_RETURNVALUE(rocks_browser, ID2P(LANG_PLUGINS), GO_TO_BROWSEPLUGINS, 
                        NULL, Icon_Plugin);
char *get_wps_item_name(int selected_item, void * data, char *buffer)
{
    (void)selected_item; (void)data; (void)buffer;
    if (audio_status())
        return ID2P(LANG_NOW_PLAYING);
    return ID2P(LANG_RESUME_PLAYBACK);
}
MENUITEM_RETURNVALUE_DYNTEXT(wps_item, GO_TO_WPS, NULL, get_wps_item_name, 
                                NULL, Icon_Playback_menu);
#ifdef HAVE_RECORDING
MENUITEM_RETURNVALUE(rec, ID2P(LANG_RECORDING_MENU), GO_TO_RECSCREEN,  
                        NULL, Icon_Recording);
#endif
#if CONFIG_TUNER
MENUITEM_RETURNVALUE(fm, ID2P(LANG_FM_RADIO), GO_TO_FM,  
                        item_callback, Icon_Radio_screen);
#endif
MENUITEM_RETURNVALUE(menu_, ID2P(LANG_SETTINGS_MENU), GO_TO_MAINMENU,  
                        NULL, Icon_Submenu_Entered);
MENUITEM_RETURNVALUE(bookmarks, ID2P(LANG_BOOKMARK_MENU_RECENT_BOOKMARKS),
                        GO_TO_RECENTBMARKS,  item_callback, 
                        Icon_Bookmark);
#ifdef HAVE_LCD_CHARCELLS
static int do_shutdown(void)
{
    sys_poweroff();
    return 0;
}
MENUITEM_FUNCTION(do_shutdown_item, ID2P(LANG_SHUTDOWN), do_shutdown, NULL, Icon_NOICON);
#endif
MAKE_MENU(root_menu_, ID2P(LANG_ROCKBOX_TITLE),
            NULL, Icon_Rockbox,
            &bookmarks, &file_browser, 
#ifdef HAVE_TAGCACHE
            &db_browser,
#endif
            &wps_item, &menu_, 
#ifdef HAVE_RECORDING
            &rec, 
#endif
#if CONFIG_TUNER
            &fm,
#endif
            &playlist_options, &rocks_browser,  &info_menu

#ifdef HAVE_LCD_CHARCELLS
            ,&do_shutdown_item
#endif
        );

int item_callback(int action, const struct menu_item_ex *this_item) 
{
    switch (action)
    {
        case ACTION_REQUEST_MENUITEM:
#if CONFIG_TUNER
            if (this_item == &fm)
            {
                if (radio_hardware_present() == 0)
                    return ACTION_EXIT_MENUITEM;
            }
            else 
#endif
                if (this_item == &bookmarks)
            {
                if (global_settings.usemrb == 0)
                    return ACTION_EXIT_MENUITEM;
            }
        break;
    }
    return action;
}
static int get_selection(int last_screen)
{
    unsigned int i;
    for(i=0; i< sizeof(root_menu__)/sizeof(*root_menu__); i++)
    {
        if ((root_menu__[i]->flags&MT_RETURN_VALUE) && 
            (root_menu__[i]->value == last_screen))
        {
            return i;
        }
    }
    return 0;
}

void root_menu(void)
{
    int previous_browser = GO_TO_FILEBROWSER;
    int previous_music = GO_TO_WPS;
    int ret_val = GO_TO_ROOT;
    int this_screen = GO_TO_ROOT;
    int selected = 0;

    if (global_settings.start_in_screen == 0)
        ret_val = (int)global_status.last_screen;
    else ret_val = global_settings.start_in_screen - 2;
    
#ifdef HAVE_RTC_ALARM
    if ( rtc_check_alarm_started(true) ) 
    {
        rtc_enable_alarm(false);
        ret_val = GO_TO_WPS;
#if CONFIG_TUNER
        if (global_settings.alarm_wake_up_screen == ALARM_START_FM)
            ret_val = GO_TO_FM;
#endif
#ifdef HAVE_RECORDING
        if (global_settings.alarm_wake_up_screen == ALARM_START_REC)
        {
            recording_start_automatic = true;
            ret_val = GO_TO_RECSCREEN;
        }
#endif
    }
#endif /* HAVE_RTC_ALARM */

    while (true)
    {
        switch (ret_val)
        {
            case GO_TO_ROOT:
                selected = get_selection(last_screen);
                ret_val = do_menu(&root_menu_, &selected);
                /* As long as MENU_ATTACHED_USB == GO_TO_ROOT this works */
                if (ret_val == MENU_ATTACHED_USB)
                {
                    check_boot();
                    continue;
                }
                else if (ret_val <= GO_TO_ROOT)
                    continue;
                last_screen = GO_TO_ROOT;
                break;

            case GO_TO_PREVIOUS:
                ret_val = last_screen;
                continue;
                break;

            case GO_TO_PREVIOUS_BROWSER:
#ifdef HAVE_TAGCACHE
                if ((previous_browser == GO_TO_DBBROWSER) && 
                    !tagcache_is_usable())
                    ret_val = GO_TO_FILEBROWSER;
                else 
#endif
                    ret_val = previous_browser;
                /* fall through */
            case GO_TO_FILEBROWSER:
#ifdef HAVE_TAGCACHE
            case GO_TO_DBBROWSER:
#endif
                previous_browser = ret_val;
                break;

            case GO_TO_PREVIOUS_MUSIC:
                ret_val = previous_music;
                /* fall through */
            case GO_TO_WPS:
#if CONFIG_TUNER
            case GO_TO_FM:
#endif
                previous_music = ret_val;
                break;
        }
        this_screen = ret_val;
        /* set the global_status.last_screen before entering,
           if we dont we will always return to the wrong screen on boot */
        global_status.last_screen = (char)this_screen;
        status_save();
        action_signalscreenchange();
        ret_val = items[this_screen].function(items[this_screen].param);
        if (ret_val != GO_TO_PREVIOUS)
            last_screen = this_screen;
    }
    return;
}