static int rebuild_shuffle_data(void) { if (g_list.cycle_mode != conf_cycle_random) return 0; g_shuffle.index = 0; g_shuffle.size = music_maxindex(); dbg_printf(d, "%s: rebuild shuffle data", __func__); if (g_shuffle.table != NULL) { free(g_shuffle.table); g_shuffle.table = NULL; } g_shuffle.table = build_array(music_maxindex()); shuffle(g_shuffle.table, g_shuffle.size); if (g_shuffle.first_time) { unsigned i; for (i = 0; i < g_shuffle.size; ++i) { if (g_shuffle.table[i] == 0) { swap(&g_shuffle.table[i], &g_shuffle.table[0]); break; } } g_shuffle.index = 1; g_shuffle.first_time = false; } return 0; }
int music_prev(void) { int ret = 0; music_lock(); switch (g_list.cycle_mode) { case conf_cycle_single: { if (g_list.curr_pos == 0) g_list.curr_pos = music_maxindex() - 1; else g_list.curr_pos--; break; } case conf_cycle_repeat: { if (g_list.curr_pos == 0) g_list.curr_pos = music_maxindex() - 1; else g_list.curr_pos--; break; } break; case conf_cycle_repeat_one: { if (g_list.curr_pos == 0) g_list.curr_pos = music_maxindex() - 1; else g_list.curr_pos--; break; } break; case conf_cycle_random: { if (shuffle_prev() != 0) { ret = music_stop(); g_list.is_list_playing = false; music_unlock(); return ret; } break; } break; } if (!g_list.is_list_playing) g_list.is_list_playing = true; if (g_list.is_list_playing) ret = music_play(g_list.curr_pos); music_unlock(); return ret; }
int music_movedown(int i) { struct music_file **tmp; struct music_file *a, *b, *c; int n; if (i < 0 || i >= music_maxindex() - 1) return -EINVAL; tmp = &g_music_files; n = i; while (*tmp && n > 0) { n--; tmp = &(*tmp)->next; } b = (*tmp); a = b->next; c = a->next; a->next = b; b->next = c; *tmp = a; music_lock(); rebuild_shuffle_data(); music_unlock(); return i + 1; }
int music_moveup(int i) { struct music_file **tmp; struct music_file *a, *b, *c; int n; if (i < 1 || i >= music_maxindex()) return -EINVAL; tmp = &g_music_files; n = i - 1; while (*tmp && n > 0) { n--; tmp = &(*tmp)->next; } a = *tmp; b = a->next; c = b->next; b->next = a; a->next = c; *tmp = b; music_lock(); rebuild_shuffle_data(); music_unlock(); return i - 1; }
int music_del(int i) { int n; struct music_file **tmp; struct music_file *p; tmp = &g_music_files; n = i; while (*tmp && n > 0) { n--; tmp = &(*tmp)->next; } p = (*tmp); *tmp = p->next; free_music_file(p); music_lock(); rebuild_shuffle_data(); n = music_maxindex(); if (n == 0) { music_list_refresh(); } music_unlock(); return n; }
static int music_list_refresh(void) { music_lock(); g_list.curr_pos = 0; if (music_maxindex() > 0) { if (g_list.is_list_playing) { if (music_play(g_list.curr_pos) < 0) { g_list.is_list_playing = false; } } else { // only load information if (music_load(g_list.curr_pos) >= 0) { musicdrv_end(); } } } else { musicdrv_end(); set_musicdrv(NULL); } g_list.first_time = true; g_shuffle.first_time = true; music_unlock(); return 0; }
int music_list_save(const char *path) { FILE *fp; int i; int fid; if (path == NULL) return -EINVAL; fp = fopen(path, "wt"); if (fp == NULL) return -EBADFD; fid = freq_enter_hotzone(); for (i = 0; i < music_maxindex(); i++) { struct music_file *file; file = music_get(i); if (file == NULL) continue; fprintf(fp, "%s\n", file->shortpath->ptr); fprintf(fp, "%s\n", file->longpath->ptr); } fclose(fp); freq_leave(fid); return true; }
int music_list_save(const char *path) { FILE *fp; int i; int fid; if (path == NULL) return -EINVAL; fp = fopen(path, "wt"); if (fp == NULL) return -EBADFD; fid = freq_enter_hotzone(); for (i = 0; i < music_maxindex(); i++) { MusicListEntry *file; file = musiclist_get(&g_music_list, i); if (file == NULL) continue; fprintf(fp, "%s\n", file->spath); fprintf(fp, "%s\n", file->lpath); } fclose(fp); freq_leave(fid); return true; }
int music_next(void) { int ret; music_lock(); switch (g_list.cycle_mode) { case conf_cycle_single: { if (g_list.curr_pos == music_maxindex() - 1) g_list.curr_pos = 0; else g_list.curr_pos++; break; } case conf_cycle_repeat: { if (g_list.curr_pos == music_maxindex() - 1) g_list.curr_pos = 0; else g_list.curr_pos++; break; } case conf_cycle_repeat_one: { if (g_list.curr_pos == music_maxindex() - 1) g_list.curr_pos = 0; else g_list.curr_pos++; break; } case conf_cycle_random: { shuffle_next(); break; } } if (!g_list.is_list_playing) g_list.is_list_playing = true; ret = music_play(g_list.curr_pos); music_unlock(); return ret; }
static int get_next_music(void) { switch (g_list.cycle_mode) { case conf_cycle_single: { if (g_list.curr_pos == music_maxindex() - 1) { g_list.curr_pos = 0; g_list.is_list_playing = false; g_list.first_time = true; } else { g_list.curr_pos++; } break; } case conf_cycle_repeat: { if (g_list.curr_pos == music_maxindex() - 1) { g_list.curr_pos = 0; } else { g_list.curr_pos++; } break; } case conf_cycle_repeat_one: { break; } case conf_cycle_random: shuffle_next(); break; default: break; } return 0; }
static int shuffle_next(void) { if (g_list.cycle_mode == conf_cycle_random) { stack_push(&played, g_list.curr_pos); if (g_shuffle.index == g_shuffle.size || g_shuffle.size != music_maxindex() ) { rebuild_shuffle_data(); } dbg_printf(d, "%s: g_shuffle index %d, pos %d", __func__, g_shuffle.index, g_shuffle.table[g_shuffle.index]); g_list.curr_pos = g_shuffle.table[g_shuffle.index++]; } return 0; }