commit 87f575c3c6293c5cdff2b424a9f182f1857248f3 from: Omar Polo date: Mon Feb 21 21:03:07 2022 UTC add a monitor functionality this pings all the "interesting" events to clients that issued a IMSG_CTL_MONITOR, except to the one that generated such event. commit - 821e4c101be89279ecdc5216b12da2da8e54152d commit + 87f575c3c6293c5cdff2b424a9f182f1857248f3 blob - a9b3a93129e6d8632418e9f76cc92eef389cbdac blob + 7d3c534b2d025f38cc7579a3627cb96a9303e666 --- amused.c +++ amused.c @@ -162,12 +162,20 @@ main_dispatch_player(int sig, short event, void *d) case IMSG_ERR: playlist_dropcurrent(); main_playlist_advance(); + if (play_state == STATE_PLAYING) + control_notify(NULL, IMSG_CTL_NEXT); + else + control_notify(NULL, IMSG_CTL_STOP); break; case IMSG_EOF: if (repeat_one && current_song != NULL) if (main_play_song(current_song)) break; main_playlist_advance(); + if (play_state == STATE_PLAYING) + control_notify(NULL, IMSG_CTL_NEXT); + else + control_notify(NULL, IMSG_CTL_STOP); break; default: blob - f01329e0d66edab94097e8f83217870ca699193a blob + afaf2d8374fa674a3f9052182ad18d97c9ba1c41 --- amused.h +++ amused.h @@ -51,6 +51,8 @@ enum imsg_type { IMSG_CTL_ADD, /* path to a file */ IMSG_CTL_COMMIT, + IMSG_CTL_MONITOR, + IMSG_CTL_ERR, }; blob - 8a1263255b91f3fda656a08f0eb16ff40921810d blob + 05357b0c0efba4aa25477ee8ff32265c2202307a --- control.c +++ control.c @@ -51,6 +51,7 @@ struct { struct ctl_conn { TAILQ_ENTRY(ctl_conn) entry; + int monitor; /* 1 if client is in monitor mode */ struct imsgev iev; }; @@ -224,6 +225,20 @@ control_close(int fd) } free(c); +} + +void +control_notify(struct imsgev *iev, int type) +{ + struct ctl_conn *c; + + TAILQ_FOREACH(c, &ctl_conns, entry) { + if (&c->iev == iev || !c->monitor) + continue; + + imsg_compose_event(&c->iev, IMSG_CTL_MONITOR, 0, 0, + -1, &type, sizeof(type)); + } } void @@ -275,6 +290,7 @@ control_dispatch_imsg(int fd, short event, void *bula) main_send_player(IMSG_RESUME, -1, NULL, 0); break; } + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_TOGGLE_PLAY: switch (play_state) { @@ -290,25 +306,30 @@ control_dispatch_imsg(int fd, short event, void *bula) main_send_player(IMSG_RESUME, -1, NULL, 0); break; } + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_PAUSE: if (play_state != STATE_PLAYING) break; play_state = STATE_PAUSED; main_send_player(IMSG_PAUSE, -1, NULL, 0); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_STOP: if (play_state == STATE_STOPPED) break; play_state = STATE_STOPPED; main_send_player(IMSG_STOP, -1, NULL, 0); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_RESTART: main_send_player(IMSG_STOP, -1, NULL, 0); main_restart_track(); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_FLUSH: playlist_truncate(); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_SHOW: main_send_playlist(&c->iev); @@ -319,13 +340,16 @@ control_dispatch_imsg(int fd, short event, void *bula) case IMSG_CTL_NEXT: main_send_player(IMSG_STOP, -1, NULL, 0); main_playlist_advance(); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_PREV: main_send_player(IMSG_STOP, -1, NULL, 0); main_playlist_previous(); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_JUMP: main_playlist_jump(&c->iev, &imsg); + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_REPEAT: if (IMSG_DATA_SIZE(imsg) != sizeof(rp)) { @@ -337,6 +361,7 @@ control_dispatch_imsg(int fd, short event, void *bula) repeat_all = rp.repeat_all; if (rp.repeat_one != -1) repeat_one = rp.repeat_one; + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_BEGIN: if (control_state.tx != -1) { @@ -355,6 +380,8 @@ control_dispatch_imsg(int fd, short event, void *bula) } main_enqueue(control_state.tx != -1, &control_state.play,&c->iev, &imsg); + if (control_state.tx == -1) + control_notify(&c->iev, imsg.hdr.type); break; case IMSG_CTL_COMMIT: if (control_state.tx != c->iev.ibuf.fd) { @@ -367,7 +394,11 @@ control_dispatch_imsg(int fd, short event, void *bula) control_state.tx = -1; imsg_compose_event(&c->iev, IMSG_CTL_COMMIT, 0, 0, -1, NULL, 0); + control_notify(&c->iev, imsg.hdr.type); break; + case IMSG_CTL_MONITOR: + c->monitor = 1; + break; default: log_debug("%s: error handling imsg %d", __func__, imsg.hdr.type); blob - e75b3c1d5a53c935ef71364bbaabaa5c054b9acd blob + 198a03208d06b4478dc845c107f3789c5cd22482 --- control.h +++ control.h @@ -19,5 +19,6 @@ int control_init(char *); int control_listen(int fd); void control_accept(int, short, void *); +void control_notify(struct imsgev *, int); void control_dispatch_imsg(int, short, void *); int control_imsg_relay(struct imsg *);