Skip to content

Commit

Permalink
Add "player-next-album" and "player-prev-album" commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gavtroy committed May 30, 2021
1 parent edc5a07 commit 8355bd6
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 8 deletions.
11 changes: 11 additions & 0 deletions Doc/cmus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ x player-play
z player-prev
v player-stop

B player-next-album
Z player-prev-album

] vol +0 +1%
[ vol +1% +0
+ vol +10%
Expand Down Expand Up @@ -513,6 +516,10 @@ pl-rename <name>
player-next (*b*)
Skips to the next track.

player-next-album (*B*)
Skips to the next album. If *shuffle*=`tracks` or a playlist is active,
skips to the next track.

player-pause (*c*)
Toggles pause.

Expand All @@ -526,6 +533,10 @@ player-play [filename] (*x*)
player-prev (*z*)
Skips to the previous track.

player-prev-album (*Z*)
Skips to the previous album. If *shuffle*=`tracks` or a playlist is active,
skips to the previous track.

player-stop (*v*)
Stops playback.

Expand Down
29 changes: 29 additions & 0 deletions cmus.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "utils.h"
#include "path.h"
#include "options.h"
#include "command_mode.h"
#include "xmalloc.h"
#include "debug.h"
#include "load_dir.h"
Expand Down Expand Up @@ -104,6 +105,34 @@ void cmus_prev(void)
player_set_file(info);
}

void cmus_next_album(void)
{
struct track_info *info;

if (play_library) {
info = lib_goto_next_album();
} else {
info = pl_goto_next();
}

if (info)
player_set_file(info);
}

void cmus_prev_album(void)
{
struct track_info *info;

if (play_library) {
info = lib_goto_prev_album();
} else {
info = pl_goto_prev();
}

if (info)
player_set_file(info);
}

void cmus_play_file(const char *filename)
{
struct track_info *ti;
Expand Down
2 changes: 2 additions & 0 deletions cmus.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ int cmus_playlist_for_each(const char *buf, int size, int reverse,

void cmus_next(void);
void cmus_prev(void);
void cmus_next_album(void);
void cmus_prev_album(void);

extern int cmus_next_track_request_fd;
struct track_info *cmus_get_next_track(void);
Expand Down
12 changes: 12 additions & 0 deletions command_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,16 @@ static void cmd_p_prev(char *arg)
}
}

static void cmd_p_next_album(char *arg)
{
cmus_next_album();
}

static void cmd_p_prev_album(char *arg)
{
cmus_prev_album();
}

static void cmd_p_stop(char *arg)
{
player_stop();
Expand Down Expand Up @@ -2604,10 +2614,12 @@ struct command commands[] = {
{ "mark", cmd_mark, 0, 1, NULL, 0, 0 },
{ "mute", cmd_mute, 0, 0, NULL, 0, 0 },
{ "player-next", cmd_p_next, 0, 0, NULL, 0, 0 },
{ "player-next-album", cmd_p_next_album, 0, 0, NULL, 0, 0 },
{ "player-pause", cmd_p_pause, 0, 0, NULL, 0, 0 },
{ "player-pause-playback", cmd_p_pause_playback, 0, 0, NULL, 0, 0 },
{ "player-play", cmd_p_play, 0, 1, expand_playable, 0, 0 },
{ "player-prev", cmd_p_prev, 0, 0, NULL, 0, 0 },
{ "player-prev-album", cmd_p_prev_album, 0, 0, NULL, 0, 0 },
{ "player-stop", cmd_p_stop, 0, 0, NULL, 0, 0 },
{ "prev-view", cmd_prev_view, 0, 0, NULL, 0, 0 },
{ "left-view", cmd_left_view, 0, 0, NULL, 0, 0 },
Expand Down
2 changes: 2 additions & 0 deletions data/rc
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Playback

bind common b player-next
bind common B player-next-album
bind common c player-pause
bind common x player-play
bind common z player-prev
bind common Z player-prev-album
bind common v player-stop

bind common ] vol +0 +1%
Expand Down
71 changes: 63 additions & 8 deletions lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static int cur_album_filter(const struct album *album)

/* set next/prev (tree) {{{ */

static struct tree_track *normal_get_next(enum aaa_mode aaa, bool allow_repeat)
static struct tree_track *normal_get_next(enum aaa_mode aaa, bool allow_repeat, bool skip_album)
{
if (lib_cur_track == NULL) {
if (!allow_repeat)
Expand All @@ -244,7 +244,7 @@ static struct tree_track *normal_get_next(enum aaa_mode aaa, bool allow_repeat)
}

/* not last track of the album? */
if (rb_next(&lib_cur_track->tree_node)) {
if (!skip_album && rb_next(&lib_cur_track->tree_node)) {
/* next track of the current album */
return to_tree_track(rb_next(&lib_cur_track->tree_node));
}
Expand Down Expand Up @@ -282,7 +282,7 @@ static struct tree_track *normal_get_next(enum aaa_mode aaa, bool allow_repeat)
return normal_get_first();
}

static struct tree_track *normal_get_prev(enum aaa_mode aaa, bool allow_repeat)
static struct tree_track *normal_get_prev(enum aaa_mode aaa, bool allow_repeat, bool skip_album)
{
if (lib_cur_track == NULL) {
if (!allow_repeat)
Expand All @@ -291,7 +291,7 @@ static struct tree_track *normal_get_prev(enum aaa_mode aaa, bool allow_repeat)
}

/* not first track of the album? */
if (rb_prev(&lib_cur_track->tree_node)) {
if (!skip_album && rb_prev(&lib_cur_track->tree_node)) {
/* prev track of the album */
return to_tree_track(rb_prev(&lib_cur_track->tree_node));
}
Expand Down Expand Up @@ -456,7 +456,7 @@ struct track_info *lib_goto_next(void)
track = (struct tree_track *)simple_list_get_next(&lib_editable.head,
(struct simple_track *)lib_cur_track, cur_album_filter, false);
else
track = normal_get_next(AAA_MODE_ALBUM, false);
track = normal_get_next(AAA_MODE_ALBUM, false, false);
if (track == NULL) {
track = shuffle_album_get_next();
if (play_sorted)
Expand All @@ -466,7 +466,7 @@ struct track_info *lib_goto_next(void)
track = (struct tree_track *)simple_list_get_next(&lib_editable.head,
(struct simple_track *)lib_cur_track, aaa_mode_filter, true);
} else {
track = normal_get_next(aaa_mode, true);
track = normal_get_next(aaa_mode, true, false);
}
return lib_set_track(track);
}
Expand All @@ -487,7 +487,7 @@ struct track_info *lib_goto_prev(void)
track = (struct tree_track *)simple_list_get_prev(&lib_editable.head,
(struct simple_track *)lib_cur_track, cur_album_filter, false);
else
track = normal_get_prev(AAA_MODE_ALBUM, false);
track = normal_get_prev(AAA_MODE_ALBUM, false, false);
if (track == NULL) {
track = shuffle_album_get_prev();
if (play_sorted)
Expand All @@ -497,11 +497,66 @@ struct track_info *lib_goto_prev(void)
track = (struct tree_track *)simple_list_get_prev(&lib_editable.head,
(struct simple_track *)lib_cur_track, aaa_mode_filter, true);
} else {
track = normal_get_prev(aaa_mode, true);
track = normal_get_prev(aaa_mode, true, false);
}
return lib_set_track(track);
}

struct track_info *lib_goto_next_album(void)
{
struct tree_track *track = NULL;

if (rb_root_empty(&lib_artist_root)) {
BUG_ON(lib_cur_track != NULL);
return NULL;
}
if (shuffle == SHUFFLE_TRACKS) {
return lib_goto_next();
} else if (shuffle == SHUFFLE_ALBUMS) {
track = shuffle_album_get_next();
if (play_sorted)
track = sorted_album_first_track(track);
} else if (play_sorted) {
track = sorted_album_last_track(track);
track = (struct tree_track *)simple_list_get_next(&lib_editable.head,
(struct simple_track *)track, aaa_mode_filter, true);
} else {
track = normal_get_next(aaa_mode, true, true);
}

return lib_set_track(track);
}

struct track_info *lib_goto_prev_album(void)
{
struct tree_track *track = NULL;

if (rb_root_empty(&lib_artist_root)) {
BUG_ON(lib_cur_track != NULL);
return NULL;
}
if (shuffle == SHUFFLE_TRACKS) {
return lib_goto_prev();
} else if (shuffle == SHUFFLE_ALBUMS) {
track = shuffle_album_get_prev();
if (play_sorted)
track = sorted_album_first_track(track);
else if (track)
track = album_first_track(track->album);
} else if (play_sorted) {
track = sorted_album_first_track(track);
track = (struct tree_track *)simple_list_get_prev(&lib_editable.head,
(struct simple_track *)track, aaa_mode_filter, true);
track = sorted_album_first_track(track);
} else {
track = normal_get_prev(aaa_mode, true, true);
if (track)
track = album_first_track(track->album);
}

return lib_set_track(track);
}

static struct tree_track *sorted_get_selected(void)
{
struct iter sel;
Expand Down
2 changes: 2 additions & 0 deletions lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ void lib_init(void);
void tree_init(void);
struct track_info *lib_goto_next(void);
struct track_info *lib_goto_prev(void);
struct track_info *lib_goto_next_album(void);
struct track_info *lib_goto_prev_album(void);
void lib_add_track(struct track_info *track_info, void *opaque);
void lib_set_filter(struct expr *expr);
void lib_set_live_filter(const char *str);
Expand Down

0 comments on commit 8355bd6

Please sign in to comment.