Esempio n. 1
0
File: fifo.c Progetto: hsgg/quark
static void
fifo_execute (const char *command)
{
    if (!g_ascii_strncasecmp (command, "quit", 4))
        main_quit ();
    else if (!g_ascii_strncasecmp (command, "play", 4))
        music_play ();
    else if (!g_ascii_strncasecmp (command, "pause", 5))
        if (music_playing)
            music_pause ();
        else
            music_play ();
    else if (!g_ascii_strncasecmp (command, "stop", 4))
        music_stop ();
    else if (!g_ascii_strncasecmp (command, "next", 4)) {
        playlist_advance (1, TRUE);
    }
    else if (!g_ascii_strncasecmp (command, "prev", 4)) {
        playlist_advance (-1, TRUE);
    }
    else if (!g_ascii_strncasecmp (command, "append", 6)) {
        if (strlen (command) > 7) {
            gchar *u;
            const gchar *path;

            path = (command + 7);
            if (!(u = g_filename_to_utf8 (path, -1, NULL, NULL, NULL))) {
                g_warning (_("Skipping '%s'. Could not convert to UTF-8. "
                             "See the README for a possible solution."), path);
            } else {
                playlist_append_single (u);
                g_free (u);
            }
        }
    }
    else if (!g_ascii_strncasecmp (command, "track", 5)) {
        if (strlen (command) > 6) {
            int i = atoi (command + 6);
            playlist_seek (i - 1); /* first track is '1' */
        }
    }
    else if (!g_ascii_strncasecmp (command, "remove", 6)) {
        if (strlen (command) > 7) {
            int i = atoi (command + 7);
            playlist_remove (i - 1); /* first track is '1' */
        }
    }
    else if (!g_ascii_strncasecmp (command, "clear", 5)) {
        playlist_clear ();
    }
    else if (!g_ascii_strncasecmp (command, "move", 4)) {
        if (strlen (command) > 5) {
            char *p;
            int i = strtol (command + 5, &p, 10);
            if (strlen (command) > (unsigned)(p - command)) {
                int before = atoi (p);
                playlist_move (i - 1, before - 1); /* first track is '1' */
            }
        }
    }
    else if (!g_ascii_strncasecmp (command, "loop", 4)) {
        main_set_loop_at_end(!main_loop_at_end);
    }
    else if (!g_ascii_strncasecmp (command, "random", 6)) {
        main_set_random_order(!main_random_order);
    }
    else if (!g_ascii_strncasecmp (command, "dump", 4)) {
        playlist_dump ();
    }
    else if (!g_ascii_strncasecmp (command, "connect", 7)) {
        if (strlen (command) > 8) {
            fifo_add_notify (command + 8);
        }
    }

}
/* Main viewer function.  Filename identifies playlist to be viewed.  If NULL,
   view current playlist. */
enum playlist_viewer_result playlist_viewer_ex(const char* filename)
{
    enum playlist_viewer_result ret = PLAYLIST_VIEWER_OK;
    bool exit = false;        /* exit viewer */
    int button;
    bool dirty = false;
    struct gui_synclist playlist_lists;
    if (!playlist_viewer_init(&viewer, filename, false))
        goto exit;

    push_current_activity(ACTIVITY_PLAYLISTVIEWER);
    gui_synclist_init(&playlist_lists, playlist_callback_name,
                      &viewer, false, 1, NULL);
    gui_synclist_set_voice_callback(&playlist_lists, playlist_callback_voice);
    gui_synclist_set_icon_callback(&playlist_lists,
                  global_settings.playlist_viewer_icons?
                  &playlist_callback_icons:NULL);
    gui_synclist_set_nb_items(&playlist_lists, viewer.num_tracks);
    gui_synclist_set_title(&playlist_lists, str(LANG_PLAYLIST), Icon_Playlist);
    gui_synclist_select_item(&playlist_lists, viewer.selected_track);
    gui_synclist_draw(&playlist_lists);
    gui_synclist_speak_item(&playlist_lists);
    while (!exit)
    {
        int track;

        if (global_status.resume_index != -1 && !viewer.playlist)
            playlist_get_resume_info(&track);
        else
            track = -1;

        if (track != viewer.current_playing_track ||
            playlist_amount_ex(viewer.playlist) != viewer.num_tracks)
        {
            /* Playlist has changed (new track started?) */
            if (!update_playlist(false))
                goto exit;
            /*Needed because update_playlist gives wrong value when
                                                            playing is stopped*/
            viewer.current_playing_track = track;
            gui_synclist_set_nb_items(&playlist_lists, viewer.num_tracks);

            gui_synclist_draw(&playlist_lists);
        }

        /* Timeout so we can determine if play status has changed */
        bool res = list_do_action(CONTEXT_TREE, HZ/2,
                            &playlist_lists, &button, LIST_WRAP_UNLESS_HELD);
        /* during moving, another redraw is going to be needed,
         * since viewer.selected_track is updated too late (after the first draw)
         * drawing the moving item needs it */
        viewer.selected_track=gui_synclist_get_sel_pos(&playlist_lists);
        if (res)
        {
            bool reload = playlist_buffer_needs_reload(&viewer.buffer,
                    viewer.selected_track);
            if (reload)
                playlist_buffer_load_entries_screen(&viewer.buffer,
                    button == ACTION_STD_NEXT ? FORWARD : BACKWARD);
            if (reload || viewer.moving_track >= 0)
                gui_synclist_draw(&playlist_lists);
        }
        switch (button)
        {
            case ACTION_TREE_WPS:
            case ACTION_STD_CANCEL:
            {
                if (viewer.moving_track >= 0)
                {
                    viewer.selected_track = viewer.moving_track;
                    gui_synclist_select_item(&playlist_lists, viewer.moving_track);
                    viewer.moving_track = -1;
                    viewer.moving_playlist_index = -1;
                    gui_synclist_draw(&playlist_lists);
                }
                else
                {
                    exit = true;
                    ret = PLAYLIST_VIEWER_CANCEL;
                }
                break;
            }
            case ACTION_STD_OK:
            {
                struct playlist_entry * current_track =
                            playlist_buffer_get_track(&viewer.buffer,
                                                      viewer.selected_track);

                if (viewer.moving_track >= 0)
                {
                    /* Move track */
                    int ret_val;

                    ret_val = playlist_move(viewer.playlist,
                                            viewer.moving_playlist_index,
                                            current_track->index);
                    if (ret_val < 0)
                         splashf(HZ, (unsigned char *)"%s %s", str(LANG_MOVE),
                                                               str(LANG_FAILED));
                    update_playlist(true);
                    viewer.moving_track = -1;
                    viewer.moving_playlist_index = -1;
                    dirty = true;
                }
                else if (!viewer.playlist)
                {
                    /* play new track */
                    if (!global_settings.party_mode)
                    {
                        playlist_start(current_track->index, 0);
                        update_playlist(false);
                    }
                }
                else if (!global_settings.party_mode)
                {
                    int start_index = current_track->index;
                    if (!warn_on_pl_erase())
                    {
                        gui_synclist_draw(&playlist_lists);
                        break;
                    }
                    /* New playlist */
                    if (playlist_set_current(viewer.playlist) < 0)
                        goto exit;
                    if (global_settings.playlist_shuffle)
                        start_index = playlist_shuffle(current_tick, start_index);
                    playlist_start(start_index, 0);

                    /* Our playlist is now the current list */
                    if (!playlist_viewer_init(&viewer, NULL, true))
                        goto exit;
                    exit = true;
                }
                gui_synclist_draw(&playlist_lists);

                break;
            }
            case ACTION_STD_CONTEXT:
            {
                /* ON+PLAY menu */
                int ret_val;

                ret_val = onplay_menu(viewer.selected_track);

                if (ret_val < 0)
                {
                    ret = PLAYLIST_VIEWER_USB;
                    goto exit;
                }
                else if (ret_val > 0)
                {
                    /* Playlist changed */
                    gui_synclist_del_item(&playlist_lists);
                    update_playlist(true);
                    if (viewer.num_tracks <= 0)
                        exit = true;
                    if (viewer.selected_track >= viewer.num_tracks)
                        viewer.selected_track = viewer.num_tracks-1;
                    dirty = true;
                }
                gui_synclist_draw(&playlist_lists);
                break;
            }
            case ACTION_STD_MENU:
                ret = PLAYLIST_VIEWER_MAINMENU;
                goto exit;
            default:
                if(default_event_handler(button) == SYS_USB_CONNECTED)
                {
                    ret = PLAYLIST_VIEWER_USB;
                    goto exit;
                }
                break;
        }
    }

exit:
    pop_current_activity();
    if (viewer.playlist)
    {
        if(dirty && yesno_pop(ID2P(LANG_SAVE_CHANGES)))
            save_playlist_screen(viewer.playlist);
        playlist_close(viewer.playlist);
    }
    return ret;
}