Commit Diff
Commit:
3af9396383944c20699d5340bd000f7a5dafb387
Date:
Wed Mar 2 17:54:37 2022
UTC
Message:
keep the current song if load input was generated by show -p
`amused show -p' generates a listing in the form of
song
> current song
song
...
This adds an heuristic to `amused load' so that the current song can be
set if it's prefixed by "> ". It's particularly useful when
re-importing the state from a previous run.
--- amused.h
+++ amused.h
@@ -49,7 +49,7 @@ enum imsg_type {
IMSG_CTL_BEGIN,
IMSG_CTL_ADD, /* path to a file */
- IMSG_CTL_COMMIT,
+ IMSG_CTL_COMMIT, /* offset of the track to jump to */
IMSG_CTL_MONITOR,
--- control.c
+++ control.c
@@ -247,7 +247,7 @@ control_dispatch_imsg(int fd, short event, void *bula)
struct ctl_conn *c;
struct imsg imsg;
struct player_repeat rp;
- ssize_t n;
+ ssize_t n, off;
if ((c = control_connbyfd(fd)) == NULL) {
log_warnx("%s: fd %d: not found", __func__, fd);
@@ -388,7 +388,12 @@ control_dispatch_imsg(int fd, short event, void *bula)
main_senderr(&c->iev, "locked");
break;
}
- playlist_swap(&control_state.play);
+ if (IMSG_DATA_SIZE(imsg) != sizeof(off)) {
+ main_senderr(&c->iev, "wrong size");
+ break;
+ }
+ memcpy(&off, imsg.data, sizeof(off));
+ playlist_swap(&control_state.play, off);
memset(&control_state.play, 0,
sizeof(control_state.play));
control_state.tx = -1;
--- ctl.c
+++ ctl.c
@@ -269,9 +269,8 @@ show_load(struct parse_result *res, struct imsg *imsg,
const char *file;
char *line = NULL;
char path[PATH_MAX];
- size_t linesize = 0;
- ssize_t linelen;
- int any = 0;
+ size_t linesize = 0, i = 0;
+ ssize_t linelen, curr = -1;
if (imsg->hdr.type == IMSG_CTL_ERR) {
print_error_message("load failed", imsg);
@@ -301,8 +300,10 @@ show_load(struct parse_result *res, struct imsg *imsg,
continue;
line[linelen-1] = '\0';
file = line;
- if (file[0] == '>' && file[1] == ' ')
+ if (file[0] == '>' && file[1] == ' ') {
file += 2;
+ curr = i;
+ }
if (file[0] == ' ' && file[1] == ' ')
file += 2;
@@ -312,7 +313,7 @@ show_load(struct parse_result *res, struct imsg *imsg,
continue;
}
- any++;
+ i++;
imsg_compose(ibuf, IMSG_CTL_ADD, 0, 0, -1,
path, sizeof(path));
}
@@ -322,13 +323,13 @@ show_load(struct parse_result *res, struct imsg *imsg,
fatal("getline");
fclose(f);
- if (!any) {
+ if (i == 0) {
*ret = 1;
return 1;
}
imsg_compose(ibuf, IMSG_CTL_COMMIT, 0, 0, -1,
- NULL, 0);
+ &curr, sizeof(curr));
imsg_flush(ibuf);
return 0;
}
--- playlist.c
+++ playlist.c
@@ -45,11 +45,14 @@ playlist_swap(struct playlist *p)
}
void
-playlist_swap(struct playlist *p)
+playlist_swap(struct playlist *p, ssize_t off)
{
ssize_t i = -1;
- if (current_song != NULL) {
+ if (off > p->len)
+ off = -1;
+
+ if (current_song != NULL && off < 0) {
/* try to adjust play_off to match the same song */
for (i = 0; i < p->len; ++i) {
if (!strcmp(current_song, p->songs[i]))
@@ -73,6 +76,8 @@ playlist_swap(struct playlist *p)
if (i != -1)
play_off = i;
+ else if (off >= 0)
+ play_off = off;
playlist.len = p->len;
playlist.cap = p->cap;
--- playlist.h
+++ playlist.h
@@ -37,7 +37,7 @@ void playlist_swap(struct playlist *);
extern ssize_t play_off;
extern const char *current_song;
-void playlist_swap(struct playlist *);
+void playlist_swap(struct playlist *, ssize_t);
void playlist_push(struct playlist *, const char *);
void playlist_enqueue(const char *);
const char *playlist_advance(void);
Omar Polo