Skip to content

Commit

Permalink
Add multiple playlists feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mahkoh committed Jul 31, 2016
1 parent 61d625d commit cd01837
Show file tree
Hide file tree
Showing 10 changed files with 1,150 additions and 37 deletions.
50 changes: 29 additions & 21 deletions command_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void view_add(int view, char *arg, int prepend)
cmus_add(lib_add_track, name, ft, JOB_TYPE_LIB, 0, NULL);
break;
case PLAYLIST_VIEW:
cmus_add(pl_add_track, name, ft, JOB_TYPE_PL, 0, NULL);
pl_add_file_to_marked_pl(name);
break;
case QUEUE_VIEW:
if (prepend) {
Expand Down Expand Up @@ -213,10 +213,7 @@ void view_save(int view, char *arg, int to_stdout, int filtered, int extended)
do_save(lib_for_each_ti, arg, dest, save_ti);
break;
case PLAYLIST_VIEW:
if (worker_has_job_by_type(JOB_TYPE_PL))
goto worker_running;
dest = extended ? &pl_ext_filename : &pl_filename;
do_save(pl_for_each, arg, dest, save_ti);
pl_save();
break;
case QUEUE_VIEW:
if (worker_has_job_by_type(JOB_TYPE_QUEUE))
Expand All @@ -225,7 +222,7 @@ void view_save(int view, char *arg, int to_stdout, int filtered, int extended)
do_save(play_queue_for_each, arg, dest, save_ti);
break;
default:
info_msg(":save only works in views 1 & 2 (library) and 3 (playlist)");
info_msg(":save only works in views 1 - 4");
}
free(arg);
return;
Expand Down Expand Up @@ -430,6 +427,8 @@ static void cmd_set(char *arg)
if (cur_view == TREE_VIEW) {
lib_track_win->changed = 1;
lib_tree_win->changed = 1;
} else if (cur_view == PLAYLIST_VIEW) {
pl_mark_for_redraw();
} else {
current_win()->changed = 1;
}
Expand Down Expand Up @@ -468,6 +467,8 @@ static void cmd_toggle(char *arg)
if (cur_view == TREE_VIEW) {
lib_track_win->changed = 1;
lib_tree_win->changed = 1;
} else if (cur_view == PLAYLIST_VIEW) {
pl_mark_for_redraw();
} else {
current_win()->changed = 1;
}
Expand Down Expand Up @@ -1255,6 +1256,16 @@ static void cmd_right_view(char *arg)
}
}

static void cmd_pl_create(char *arg)
{
pl_create(arg);
}

static void cmd_pl_rename(char *arg)
{
pl_rename_selected_pl(arg);
}

static void cmd_view(char *arg)
{
int view;
Expand Down Expand Up @@ -1354,11 +1365,6 @@ static int sorted_for_each_sel(track_info_cb cb, void *data, int reverse)
return editable_for_each_sel(&lib_editable, cb, data, reverse);
}

static int pl_for_each_sel(track_info_cb cb, void *data, int reverse)
{
return editable_for_each_sel(&pl_editable, cb, data, reverse);
}

static int pq_for_each_sel(track_info_cb cb, void *data, int reverse)
{
return editable_for_each_sel(&pq_editable, cb, data, reverse);
Expand Down Expand Up @@ -1436,10 +1442,14 @@ static void cmd_win_add_p(char *arg)
return;

if (cur_view <= QUEUE_VIEW) {
struct wrapper_cb_data add = { pl_add_track };
struct wrapper_cb_data add = { pl_add_track_to_marked_pl2 };
view_for_each_sel[cur_view](wrapper_cb, &add, 0);
} else if (cur_view == BROWSER_VIEW) {
add_from_browser(pl_add_track, JOB_TYPE_PL);
char *sel = get_browser_add_file();
if (sel) {
pl_add_file_to_marked_pl(sel);
free(sel);
}
}
}

Expand Down Expand Up @@ -1479,9 +1489,6 @@ static void cmd_win_activate(char *arg)
if (lib_cur_track)
previous = &lib_cur_track->shuffle_track;
shuffle_root = &lib_shuffle_root;
} else if (cur_view == PLAYLIST_VIEW) {
previous = (struct shuffle_track *)pl_cur_track;
shuffle_root = &pl_shuffle_root;
}

switch (cur_view) {
Expand All @@ -1494,8 +1501,7 @@ static void cmd_win_activate(char *arg)
next = &lib_cur_track->shuffle_track;
break;
case PLAYLIST_VIEW:
info = pl_activate_selected();
next = (struct shuffle_track *)pl_cur_track;
info = pl_play_selected_row();
break;
case QUEUE_VIEW:
break;
Expand All @@ -1511,7 +1517,7 @@ static void cmd_win_activate(char *arg)
}

if (info) {
if (shuffle)
if (shuffle && next)
shuffle_insert(shuffle_root, previous, next);
/* update lib/pl mode */
if (cur_view < 2)
Expand Down Expand Up @@ -1590,7 +1596,7 @@ static void cmd_win_sel_cur(char *arg)
sorted_sel_current();
break;
case PLAYLIST_VIEW:
pl_sel_current();
pl_select_playing_track();
break;
}
}
Expand Down Expand Up @@ -1653,7 +1659,7 @@ static void cmd_win_next(char *arg)
{
if (cur_view == TREE_VIEW)
tree_toggle_active_window();
if (cur_view == PLAYLIST_VIEW)
else if (cur_view == PLAYLIST_VIEW)
pl_win_next();
}

Expand Down Expand Up @@ -2495,6 +2501,8 @@ struct command commands[] = {
{ "prev-view", cmd_prev_view, 0, 0, NULL, 0, 0 },
{ "left-view", cmd_left_view, 0, 0, NULL, 0, 0 },
{ "right-view", cmd_right_view, 0, 0, NULL, 0, 0 },
{ "pl-create", cmd_pl_create, 1, -1,NULL, 0, 0 },
{ "pl-rename", cmd_pl_rename, 1, -1,NULL, 0, 0 },
{ "push", cmd_push, 1,-1, expand_commands, 0, 0 },
{ "pwd", cmd_pwd, 0, 0, NULL, 0, 0 },
{ "rand", cmd_rand, 0, 0, NULL, 0, 0 },
Expand Down
43 changes: 42 additions & 1 deletion job.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum job_result_var {
JOB_RES_ADD,
JOB_RES_UPDATE,
JOB_RES_UPDATE_CACHE,
JOB_RES_PL_DELETE,
};

enum update_kind {
Expand Down Expand Up @@ -79,6 +80,10 @@ struct job_result {
size_t update_cache_num;
struct track_info **update_cache_ti;
};
struct {
void (*pl_delete_cb)(struct playlist *);
struct playlist *pl_delete_pl;
};
};
};

Expand Down Expand Up @@ -550,7 +555,7 @@ static void job_handle_update_cache_result(struct job_result *res)
new = old->next;
if (lib_remove(old) && new)
lib_add_track(new, NULL);
editable_update_track(&pl_editable, old, new);
pl_update_track(old, new);
editable_update_track(&pq_editable, old, new);
if (player_info.ti == old && new) {
track_info_ref(new);
Expand All @@ -570,6 +575,39 @@ void job_schedule_update_cache(int type, struct update_cache_data *data)
free_update_cache_job, data);
}

static void do_pl_delete_job(void *data)
{
/*
* If PL jobs are canceled this function won't run. Hence we push the
* result in the free function.
*/
}

static void free_pl_delete_job(void *data)
{
struct pl_delete_data *pdd = data;
struct job_result *res;

res = xnew(struct job_result, 1);
res->var = JOB_RES_PL_DELETE;
res->pl_delete_cb = pdd->cb;
res->pl_delete_pl = pdd->pl;
job_push_result(res);

free(pdd);
}

static void job_handle_pl_delete_result(struct job_result *res)
{
res->pl_delete_cb(res->pl_delete_pl);
}

void job_schedule_pl_delete(struct pl_delete_data *data)
{
worker_add_job(JOB_TYPE_PL | JOB_TYPE_DELETE, do_pl_delete_job,
free_pl_delete_job, data);
}

static void job_handle_result(struct job_result *res)
{
switch (res->var) {
Expand All @@ -582,6 +620,9 @@ static void job_handle_result(struct job_result *res)
case JOB_RES_UPDATE_CACHE:
job_handle_update_cache_result(res);
break;
case JOB_RES_PL_DELETE:
job_handle_pl_delete_result(res);
break;
}
free(res);
}
Expand Down
7 changes: 7 additions & 0 deletions job.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define JOB_TYPE_ADD 1 << 16
#define JOB_TYPE_UPDATE 1 << 17
#define JOB_TYPE_UPDATE_CACHE 1 << 18
#define JOB_TYPE_DELETE 1 << 19

struct add_data {
enum file_type type;
Expand All @@ -48,13 +49,19 @@ struct update_cache_data {
unsigned int force : 1;
};

struct pl_delete_data {
struct playlist *pl;
void (*cb)(struct playlist *);
};

extern int job_fd;

void job_init(void);
void job_exit(void);
void job_schedule_add(int type, struct add_data *data);
void job_schedule_update(struct update_data *data);
void job_schedule_update_cache(int type, struct update_cache_data *data);
void job_schedule_pl_delete(struct pl_delete_data *data);
void job_handle(void);

#endif
32 changes: 32 additions & 0 deletions misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "prog.h"
#include "xmalloc.h"
#include "xstrjoin.h"
#include "ui_curses.h"

#include <string.h>
#include <stdlib.h>
Expand All @@ -33,6 +34,7 @@
#include <pwd.h>

const char *cmus_config_dir = NULL;
const char *cmus_playlist_dir = NULL;
const char *cmus_socket_path = NULL;
const char *home_dir = NULL;
const char *user_name = NULL;
Expand Down Expand Up @@ -182,6 +184,30 @@ static char *get_non_empty_env(const char *name)
return xstrdup(val);
}

const char *get_filename(const char *path)
{
const char *file = strrchr(path, '/');
if (!file)
file = path;
else
file++;
if (!*file)
return NULL;
return file;
}

static void move_old_playlist(void)
{
char *default_playlist = xstrjoin(cmus_playlist_dir, "/default");
char *old_playlist = xstrjoin(cmus_config_dir, "/playlist.pl");
int rc = rename(old_playlist, default_playlist);
if (rc && errno != ENOENT)
die_errno("error: unable to move %s to playlist directory",
old_playlist);
free(default_playlist);
free(old_playlist);
}

int misc_init(void)
{
char *xdg_runtime_dir = get_non_empty_env("XDG_RUNTIME_DIR");
Expand Down Expand Up @@ -222,6 +248,12 @@ int misc_init(void)
}
make_dir(cmus_config_dir);

cmus_playlist_dir = xstrjoin(cmus_config_dir, "/playlists");
int playlist_dir_is_new = dir_exists(cmus_playlist_dir) == 0;
make_dir(cmus_playlist_dir);
if (playlist_dir_is_new)
move_old_playlist();

cmus_socket_path = get_non_empty_env("CMUS_SOCKET");
if (cmus_socket_path == NULL) {
if (xdg_runtime_dir == NULL) {
Expand Down
2 changes: 2 additions & 0 deletions misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <stddef.h>

extern const char *cmus_config_dir;
extern const char *cmus_playlist_dir;
extern const char *cmus_socket_path;
extern const char *home_dir;
extern const char *user_name;
Expand All @@ -32,6 +33,7 @@ int strptrcoll(const void *a, const void *b);
int misc_init(void);
const char *escape(const char *str);
const char *unescape(const char *str);
const char *get_filename(const char *path);

/*
* @field contains Replay Gain data format in bit representation
Expand Down
Loading

0 comments on commit cd01837

Please sign in to comment.