commit 90122a37e6f55f08fd979f7b07ba20a49952faf8 from: Omar Polo date: Sat May 14 18:54:20 2022 UTC amused monitor: allow to pass a list of event as filter it's easier / simpler for scripts to do $ amused monitor next,prev,jump rather than $ amused monitor | egrep --line-buffered 'next|prev|jump' commit - 26caf091b4db3116bc2cb0205f4fa1e8a5fdbcae commit + 90122a37e6f55f08fd979f7b07ba20a49952faf8 blob - 60282c1842797ac51a9266a5b8a7a208ff64fcf8 blob + 0feee4124de8465c39d5a4a9673f521e7e94e716 --- amused.1 +++ amused.1 @@ -78,12 +78,21 @@ If the list was generated by restores also the position in the playlist, otherwise if already playing something tries to match the currently playing song in the new list. Failing that, the playlist will be played from the first track onwards. -.It Cm monitor -Stop indefinitely and print the events as they happen one per line. -The events are triggered either by other instances of +.It Cm monitor Op Ar events +Stop indefinitely and print when an event in the comma-separated list of +.Ar events +happen. +By default logs every event. +The +.Ar events +are triggered either by other instances of .Nm issuing commands or the player itself anvancing through the playing queue. +The events name take after the command name that generates it, e.g.\& +.Ar play , +.Ar toggle , +.Ar ... .It Cm next Play the next song. .It Cm pause blob - afcac0d1af9ff25dc42ad7919c5fe922857ddd3e blob + 81634a091f11596ad1fb2cad0f4c6b4298ea0600 --- amused.h +++ amused.h @@ -53,6 +53,7 @@ enum imsg_type { IMSG_CTL_MONITOR, IMSG_CTL_ERR, + IMSG__LAST, }; struct imsgev { @@ -99,6 +100,7 @@ struct parse_result { char **files; const char *file; int pretty; + int monitor[IMSG__LAST]; struct player_repeat rep; struct ctl_command *ctl; }; blob - a07182ead118ab9b1427cb615811de32cd0d896d blob + e25201404c849f9f177eca8e796465a6095e4b8c --- ctl.c +++ ctl.c @@ -45,6 +45,7 @@ int ctl_show(struct parse_result *, int, char **); int ctl_load(struct parse_result *, int, char **); int ctl_jump(struct parse_result *, int, char **); int ctl_repeat(struct parse_result *, int, char **); +int ctl_monitor(struct parse_result *, int, char **); struct ctl_command ctl_commands[] = { { "play", PLAY, ctl_noarg, "" }, @@ -61,7 +62,7 @@ struct ctl_command ctl_commands[] = { { "load", LOAD, ctl_load, "[file]", 1 }, { "jump", JUMP, ctl_jump, "pattern" }, { "repeat", REPEAT, ctl_repeat, "one|all on|off" }, - { "monitor", MONITOR, ctl_noarg, "" }, + { "monitor", MONITOR, ctl_monitor, "[events]" }, { NULL }, }; @@ -349,7 +350,7 @@ show_load(struct parse_result *res, struct imsg *imsg, } static int -show_monitor(struct imsg *imsg, int *ret) +show_monitor(struct parse_result *res, struct imsg *imsg, int *ret) { int type; @@ -367,6 +368,15 @@ show_monitor(struct imsg *imsg, int *ret) } memcpy(&type, imsg->data, sizeof(type)); + if (type < 0 || type > IMSG__LAST) { + log_warnx("wrong monitor type received"); + *ret = 1; + return 1; + } + + if (!res->monitor[type]) + return 0; + switch (type) { case IMSG_CTL_PLAY: puts("play"); @@ -544,7 +554,7 @@ ctlaction(struct parse_result *res) done = show_load(res, &imsg, &ret); break; case MONITOR: - done = show_monitor(&imsg, &ret); + done = show_monitor(res, &imsg, &ret); break; default: done = 1; @@ -681,7 +691,65 @@ ctl_repeat(struct parse_result *res, int argc, char ** res->rep.repeat_all = b; else ctl_usage(res->ctl); + + return ctlaction(res); +} + +int +ctl_monitor(struct parse_result *res, int argc, char **argv) +{ + int ch; + const char *events; + char *dup, *tmp, *tok; + + while ((ch = getopt(argc, argv, "")) != -1) + ctl_usage(res->ctl); + argc -= optind; + argv += optind; + + if (argc > 1) + ctl_usage(res->ctl); + + if (argc == 1) + events = *argv; + else + events = "play,toggle,pause,stop,restart,flush,next,prev," + "jump,repeat,add,load"; + tmp = dup = xstrdup(events); + while ((tok = strsep(&tmp, ",")) != NULL) { + if (*tok == '\0') + continue; + + if (!strcmp(tok, "play")) + res->monitor[IMSG_CTL_PLAY] = 1; + else if (!strcmp(tok, "toggle")) + res->monitor[IMSG_CTL_TOGGLE_PLAY] = 1; + else if (!strcmp(tok, "pause")) + res->monitor[IMSG_CTL_PAUSE] = 1; + else if (!strcmp(tok, "stop")) + res->monitor[IMSG_CTL_STOP] = 1; + else if (!strcmp(tok, "restart")) + res->monitor[IMSG_CTL_RESTART] = 1; + else if (!strcmp(tok, "flush")) + res->monitor[IMSG_CTL_FLUSH] = 1; + else if (!strcmp(tok, "next")) + res->monitor[IMSG_CTL_NEXT] = 1; + else if (!strcmp(tok, "prev")) + res->monitor[IMSG_CTL_PREV] = 1; + else if (!strcmp(tok, "jump")) + res->monitor[IMSG_CTL_JUMP] = 1; + else if (!strcmp(tok, "repeat")) + res->monitor[IMSG_CTL_REPEAT] = 1; + else if (!strcmp(tok, "add")) + res->monitor[IMSG_CTL_ADD] = 1; + else if (!strcmp(tok, "load")) + res->monitor[IMSG_CTL_COMMIT] = 1; + else + fatalx("unknown event \"%s\"", tok); + } + + free(dup); return ctlaction(res); }