sad

simple audio daemon
git clone git://git.2f30.org/sad
Log | Files | Refs | LICENSE

commit a780d71d012c31e573f2efb68d3fd0bd1529f58e
parent 3c996b019c84314d1535fd13b7a3baa4fb6b0f92
Author: sin <sin@2f30.org>
Date:   Thu, 21 Jan 2021 10:03:31 +0000

Re-format code

Diffstat:
Malsa.c | 181++++++++++++++++++++++++++++++++++++++-----------------------------------------
Marg.h | 95+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mcmd.c | 809+++++++++++++++++++++++++++++++++++++------------------------------------------
Mcompat/reallocarray.c | 23+++++++++++------------
Mcompat/strlcat.c | 44+++++++++++++++++++++-----------------------
Mcompat/strlcpy.c | 40+++++++++++++++++++---------------------
Mcompat/strtonum.c | 74++++++++++++++++++++++++++++++++++++--------------------------------------
Mconfig.def.h | 42++++++++++++------------------------------
Mdecoder.c | 34++++++++++++++++------------------
Mfifo.c | 88++++++++++++++++++++++++++++++++++++-------------------------------------------
Mmp3.c | 146+++++++++++++++++++++++++++++++++++++------------------------------------------
Mnotify.c | 139++++++++++++++++++++++++++++++++++++-------------------------------------------
Moutput.c | 484+++++++++++++++++++++++++++++++++++++------------------------------------------
Mpcm.c | 46+++++++++++++++++++---------------------------
Mplaylist.c | 343+++++++++++++++++++++++++++++++++++--------------------------------------------
Mqueue.h | 972++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msad.c | 287++++++++++++++++++++++++++++++++++++++-----------------------------------------
Msad.h | 73++++++++++++++++++++++++++++++++-----------------------------------------
Msndio.c | 108++++++++++++++++++++++++++++++++++++-------------------------------------------
Mvorbis.c | 89+++++++++++++++++++++++++++++++++++--------------------------------------------
Mwav.c | 117++++++++++++++++++++++++++++++++++++-------------------------------------------
21 files changed, 1994 insertions(+), 2240 deletions(-)

diff --git a/alsa.c b/alsa.c @@ -1,9 +1,9 @@ #include <sys/select.h> +#include <alsa/asoundlib.h> #include <err.h> #include <limits.h> #include <stdio.h> -#include <alsa/asoundlib.h> #include "sad.h" @@ -12,115 +12,106 @@ static int framesize; static const char *device = "default"; /* TODO: make configurable? */ int alsavolstatus = DEFAULTVOL; -static int -alsavol(int vol) -{ - long min, max; - snd_mixer_t *mixerhdl; - snd_mixer_elem_t *elem; - snd_mixer_selem_id_t *sid; - const char *selem_name = "Master"; - - if (snd_mixer_open(&mixerhdl, 0) < 0) { - warnx("snd_mixer_open: failed"); - return -1; - } - - if (snd_mixer_attach(mixerhdl, device) < 0) { - warnx("snd_mixer_attach: failed"); - goto err0; - } - - if (snd_mixer_selem_register(mixerhdl, NULL, NULL) < 0) { - warnx("snd_mixer_selem_register: failed"); - goto err0; - } - - if (snd_mixer_load(mixerhdl) < 0) { - warnx("snd_mixer_load: failed"); - goto err0; - } - - snd_mixer_selem_id_alloca(&sid); - snd_mixer_selem_id_set_index(sid, 0); - snd_mixer_selem_id_set_name(sid, selem_name); - elem = snd_mixer_find_selem(mixerhdl, sid); - if (!elem) { - warnx("snd_mixer_find_selem: failed"); - goto err0; - } - - snd_mixer_selem_get_playback_volume_range(elem, &min, &max); - snd_mixer_selem_set_playback_volume_all(elem, vol * max / 100); - alsavolstatus = vol; - - snd_mixer_close(mixerhdl); - return 0; +static int alsavol(int vol) { + long min, max; + snd_mixer_t *mixerhdl; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + const char *selem_name = "Master"; + + if (snd_mixer_open(&mixerhdl, 0) < 0) { + warnx("snd_mixer_open: failed"); + return -1; + } + + if (snd_mixer_attach(mixerhdl, device) < 0) { + warnx("snd_mixer_attach: failed"); + goto err0; + } + + if (snd_mixer_selem_register(mixerhdl, NULL, NULL) < 0) { + warnx("snd_mixer_selem_register: failed"); + goto err0; + } + + if (snd_mixer_load(mixerhdl) < 0) { + warnx("snd_mixer_load: failed"); + goto err0; + } + + snd_mixer_selem_id_alloca(&sid); + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, selem_name); + elem = snd_mixer_find_selem(mixerhdl, sid); + if (!elem) { + warnx("snd_mixer_find_selem: failed"); + goto err0; + } + + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_set_playback_volume_all(elem, vol * max / 100); + alsavolstatus = vol; + + snd_mixer_close(mixerhdl); + return 0; err0: - snd_mixer_close(mixerhdl); - return -1; + snd_mixer_close(mixerhdl); + return -1; } -static int -alsaopen(Format *fmt) -{ - snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; - int r; +static int alsaopen(Format *fmt) { + snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; + int r; - if (fmt->bits != 16) { - warnx("unsupported number of bits"); - return -1; - } + if (fmt->bits != 16) { + warnx("unsupported number of bits"); + return -1; + } - if ((r = snd_pcm_open(&hdl, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { - warnx("snd_pcm_open: %s\n", snd_strerror(r)); - return -1; - } + if ((r = snd_pcm_open(&hdl, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) { + warnx("snd_pcm_open: %s\n", snd_strerror(r)); + return -1; + } - if ((r = snd_pcm_set_params(hdl, format, SND_PCM_ACCESS_RW_INTERLEAVED, - fmt->channels, fmt->rate, 0, 500000)) < 0) { - warnx("send_pcm_set_params: %s\n", snd_strerror(r)); - goto err0; - } + if ((r = snd_pcm_set_params(hdl, format, SND_PCM_ACCESS_RW_INTERLEAVED, + fmt->channels, fmt->rate, 0, 500000)) < 0) { + warnx("send_pcm_set_params: %s\n", snd_strerror(r)); + goto err0; + } - framesize = (fmt->bits + 7) / 8 * fmt->channels; - return 0; + framesize = (fmt->bits + 7) / 8 * fmt->channels; + return 0; err0: - snd_pcm_close(hdl); - hdl = NULL; - return -1; + snd_pcm_close(hdl); + hdl = NULL; + return -1; } -static int -alsaplay(void *buf, size_t nbytes) -{ - snd_pcm_sframes_t frames; - - frames = snd_pcm_writei(hdl, buf, nbytes / framesize); - if (frames < 0) - frames = snd_pcm_recover(hdl, frames, 0); - if (frames < 0) { - warnx("snd_pcm_writei failed: %s\n", snd_strerror(frames)); - return -1; - } - return frames; +static int alsaplay(void *buf, size_t nbytes) { + snd_pcm_sframes_t frames; + + frames = snd_pcm_writei(hdl, buf, nbytes / framesize); + if (frames < 0) + frames = snd_pcm_recover(hdl, frames, 0); + if (frames < 0) { + warnx("snd_pcm_writei failed: %s\n", snd_strerror(frames)); + return -1; + } + return frames; } -static int -alsaclose(void) -{ - if (hdl) - snd_pcm_close(hdl); - hdl = NULL; - return 0; +static int alsaclose(void) { + if (hdl) + snd_pcm_close(hdl); + hdl = NULL; + return 0; } Output alsaoutput = { .volstatus = &alsavolstatus, - .vol = alsavol, - .open = alsaopen, - .play = alsaplay, - .close = alsaclose, + .vol = alsavol, + .open = alsaopen, + .play = alsaplay, + .close = alsaclose, }; - diff --git a/arg.h b/arg.h @@ -9,55 +9,54 @@ extern char *argv0; /* use main(int argc, char *argv[]) */ -#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ - argv[0] && argv[0][1]\ - && argv[0][0] == '-';\ - argc--, argv++) {\ - char argc_;\ - char **argv_;\ - int brk_;\ - if (argv[0][1] == '-' && argv[0][2] == '\0') {\ - argv++;\ - argc--;\ - break;\ - }\ - for (brk_ = 0, argv[0]++, argv_ = argv;\ - argv[0][0] && !brk_;\ - argv[0]++) {\ - if (argv_ != argv)\ - break;\ - argc_ = argv[0][0];\ - switch (argc_) +#define ARGBEGIN \ + for (argv0 = *argv, argv++, argc--; \ + argv[0] && argv[0][1] && argv[0][0] == '-'; argc--, argv++) { \ + char argc_; \ + char **argv_; \ + int brk_; \ + if (argv[0][1] == '-' && argv[0][2] == '\0') { \ + argv++; \ + argc--; \ + break; \ + } \ + for (brk_ = 0, argv[0]++, argv_ = argv; argv[0][0] && !brk_; argv[0]++) { \ + if (argv_ != argv) \ + break; \ + argc_ = argv[0][0]; \ + switch (argc_) /* Handles obsolete -NUM syntax */ -#define ARGNUM case '0':\ - case '1':\ - case '2':\ - case '3':\ - case '4':\ - case '5':\ - case '6':\ - case '7':\ - case '8':\ - case '9' - -#define ARGEND }\ - } - -#define ARGC() argc_ - -#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) - -#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ - ((x), abort(), (char *)0) :\ - (brk_ = 1, (argv[0][1] != '\0')?\ - (&argv[0][1]) :\ - (argc--, argv++, argv[0]))) - -#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ - (char *)0 :\ - (brk_ = 1, (argv[0][1] != '\0')?\ - (&argv[0][1]) :\ - (argc--, argv++, argv[0]))) +#define ARGNUM \ + case '0': \ + case '1': \ + case '2': \ + case '3': \ + case '4': \ + case '5': \ + case '6': \ + case '7': \ + case '8': \ + case '9' + +#define ARGEND \ + } \ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) \ + ((argv[0][1] == '\0' && argv[1] == NULL) \ + ? ((x), abort(), (char *)0) \ + : (brk_ = 1, \ + (argv[0][1] != '\0') ? (&argv[0][1]) : (argc--, argv++, argv[0]))) + +#define ARGF() \ + ((argv[0][1] == '\0' && argv[1] == NULL) \ + ? (char *)0 \ + : (brk_ = 1, \ + (argv[0][1] != '\0') ? (&argv[0][1]) : (argc--, argv++, argv[0]))) #endif diff --git a/cmd.c b/cmd.c @@ -12,497 +12,440 @@ #include "sad.h" -static void -cmdstatus(int fd, char *arg) -{ - Song *s; - int r = 0; - - if (!arg[0]) { - dprintf(fd, "ERR expected argument\n"); - return; - } - - if (!strncmp(arg, "random", 7)) { - r = 1; - dprintf(fd, "random: %d\n", (getplaylistmode() & RANDOM) == 1); - } else if (!strncmp(arg, "repeat", 7)) { - r = 1; - dprintf(fd, "repeat: %d\n", (getplaylistmode() & REPEAT) == 1); - } else if (!strncmp(arg, "single", 7)) { - r = 1; - dprintf(fd, "single: %d\n", (getplaylistmode() & SINGLE) == 1); - } else if (!strncmp(arg, "volume", 7)) { - r = 1; - dprintf(fd, "volume: %d\n", getvol()); - } else if (!strncmp(arg, "songid", 7)) { - r = 1; - s = getcursong(); - - if (!s) { - dprintf(fd, "ERR no song is active\n"); - return; - } - - dprintf(fd, "songid: %d\n", s->id); - } else if (!strncmp(arg, "playback", 9)) { - r = 1; - s = getcursong(); - - dprintf(fd, "playback: "); - - if (s) - switch (s->state) { - case PLAYING: - dprintf(fd, "play\n"); - break; - case PAUSED: - dprintf(fd, "pause\n"); - break; - case NONE: - dprintf(fd, "stop\n"); - break; - } - else - dprintf(fd, "stop\n"); - } - - if (r) { - dprintf(fd, "OK\n"); - return; - } - - dprintf(fd, "ERR unknown command\n"); +static void cmdstatus(int fd, char *arg) { + Song *s; + int r = 0; + + if (!arg[0]) { + dprintf(fd, "ERR expected argument\n"); + return; + } + + if (!strncmp(arg, "random", 7)) { + r = 1; + dprintf(fd, "random: %d\n", (getplaylistmode() & RANDOM) == 1); + } else if (!strncmp(arg, "repeat", 7)) { + r = 1; + dprintf(fd, "repeat: %d\n", (getplaylistmode() & REPEAT) == 1); + } else if (!strncmp(arg, "single", 7)) { + r = 1; + dprintf(fd, "single: %d\n", (getplaylistmode() & SINGLE) == 1); + } else if (!strncmp(arg, "volume", 7)) { + r = 1; + dprintf(fd, "volume: %d\n", getvol()); + } else if (!strncmp(arg, "songid", 7)) { + r = 1; + s = getcursong(); + + if (!s) { + dprintf(fd, "ERR no song is active\n"); + return; + } + + dprintf(fd, "songid: %d\n", s->id); + } else if (!strncmp(arg, "playback", 9)) { + r = 1; + s = getcursong(); + + dprintf(fd, "playback: "); + + if (s) + switch (s->state) { + case PLAYING: + dprintf(fd, "play\n"); + break; + case PAUSED: + dprintf(fd, "pause\n"); + break; + case NONE: + dprintf(fd, "stop\n"); + break; + } + else + dprintf(fd, "stop\n"); + } + + if (r) { + dprintf(fd, "OK\n"); + return; + } + + dprintf(fd, "ERR unknown command\n"); } -static void -cmdrepeat(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } +static void cmdrepeat(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } - playlistmode(REPEAT); - dprintf(fd, "OK\n"); + playlistmode(REPEAT); + dprintf(fd, "OK\n"); } -static void -cmdrandom(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } +static void cmdrandom(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } - playlistmode(RANDOM); - dprintf(fd, "OK\n"); + playlistmode(RANDOM); + dprintf(fd, "OK\n"); } -static void -cmdvolume(int fd, char *arg) -{ - int vol; - const char *errstr; - - if (!arg[0]) { - dprintf(fd, "ERR expected volume\n"); - return; - } - - vol = strtonum(arg, 0, 100, &errstr); - if (errstr) { - dprintf(fd, "ERR volume should be between [0, 100]\n"); - return; - } - if (setvol(vol) < 0) { - dprintf(fd, "ERR failed to change volume\n"); - return; - } - dprintf(fd, "OK\n"); +static void cmdvolume(int fd, char *arg) { + int vol; + const char *errstr; + + if (!arg[0]) { + dprintf(fd, "ERR expected volume\n"); + return; + } + + vol = strtonum(arg, 0, 100, &errstr); + if (errstr) { + dprintf(fd, "ERR volume should be between [0, 100]\n"); + return; + } + if (setvol(vol) < 0) { + dprintf(fd, "ERR failed to change volume\n"); + return; + } + dprintf(fd, "OK\n"); } -static void -cmdnext(int fd, char *arg) -{ - Song *s; +static void cmdnext(int fd, char *arg) { + Song *s; - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } - s = getcursong(); - if (!s) { - dprintf(fd, "ERR playlist is empty\n"); - return; - } + s = getcursong(); + if (!s) { + dprintf(fd, "ERR playlist is empty\n"); + return; + } - playsong(getnextsong()); - dprintf(fd, "OK\n"); + playsong(getnextsong()); + dprintf(fd, "OK\n"); } -static void -cmdpause(int fd, char *arg) -{ - Song *s; - int pause; - const char *errstr; - - if (!arg[0]) { - s = getcursong(); - if (s->state == PAUSED) - s->state = PLAYING; - else - s->state = PAUSED; - } else { - - pause = strtonum(arg, 0, 1, &errstr); - if (errstr) { - dprintf(fd, "ERR argument should be 0 or 1, or no argument\n"); - return; - } +static void cmdpause(int fd, char *arg) { + Song *s; + int pause; + const char *errstr; + + if (!arg[0]) { + s = getcursong(); + if (s->state == PAUSED) + s->state = PLAYING; + else + s->state = PAUSED; + } else { + + pause = strtonum(arg, 0, 1, &errstr); + if (errstr) { + dprintf(fd, "ERR argument should be 0 or 1, or no argument\n"); + return; + } - s = getcursong(); - if (!s) { - dprintf(fd, "ERR playlist is empty\n"); - return; - } + s = getcursong(); + if (!s) { + dprintf(fd, "ERR playlist is empty\n"); + return; + } - switch (s->state) { - case PLAYING: - if (pause == 1) - s->state = PAUSED; - break; - case PAUSED: - if (pause == 0) - s->state = PLAYING; - break; - case NONE: - dprintf(fd, "ERR no song is active\n"); - return; - } + switch (s->state) { + case PLAYING: + if (pause == 1) + s->state = PAUSED; + break; + case PAUSED: + if (pause == 0) + s->state = PLAYING; + break; + case NONE: + dprintf(fd, "ERR no song is active\n"); + return; } - dprintf(fd, "OK\n"); + } + dprintf(fd, "OK\n"); } -static void -cmdplay(int fd, char *arg) -{ - Song *s, *cur; - int id; - const char *errstr; - - cur = getcursong(); - if (!cur) { - dprintf(fd, "ERR playlist is empty\n"); - return; - } - - if (arg[0]) { - id = strtonum(arg, 0, INT_MAX, &errstr); - if (errstr) { - dprintf(fd, "ERR invalid song id\n"); - return; - } - - s = findsongid(id); - if (!s) { - dprintf(fd, "ERR cannot find song with given id\n"); - return; - } - } else { - s = cur; - } - - playsong(s); - dprintf(fd, "OK\n"); +static void cmdplay(int fd, char *arg) { + Song *s, *cur; + int id; + const char *errstr; + + cur = getcursong(); + if (!cur) { + dprintf(fd, "ERR playlist is empty\n"); + return; + } + + if (arg[0]) { + id = strtonum(arg, 0, INT_MAX, &errstr); + if (errstr) { + dprintf(fd, "ERR invalid song id\n"); + return; + } + + s = findsongid(id); + if (!s) { + dprintf(fd, "ERR cannot find song with given id\n"); + return; + } + } else { + s = cur; + } + + playsong(s); + dprintf(fd, "OK\n"); } -static void -cmdprev(int fd, char *arg) -{ - Song *s; +static void cmdprev(int fd, char *arg) { + Song *s; - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } - s = getcursong(); - if (!s) { - dprintf(fd, "ERR playlist is empty\n"); - return; - } + s = getcursong(); + if (!s) { + dprintf(fd, "ERR playlist is empty\n"); + return; + } - playsong(getprevsong()); - dprintf(fd, "OK\n"); + playsong(getprevsong()); + dprintf(fd, "OK\n"); } -static void -cmdstop(int fd, char *arg) -{ - Song *s; +static void cmdstop(int fd, char *arg) { + Song *s; - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } - s = getcursong(); - if (!s) { - dprintf(fd, "ERR playlist is empty\n"); - return; - } + s = getcursong(); + if (!s) { + dprintf(fd, "ERR playlist is empty\n"); + return; + } - stopsong(s); - dprintf(fd, "OK\n"); + stopsong(s); + dprintf(fd, "OK\n"); } -static void -cmdadd(int fd, char *arg) -{ - if (!arg[0]) { - dprintf(fd, "ERR expected file path\n"); - return; - } +static void cmdadd(int fd, char *arg) { + if (!arg[0]) { + dprintf(fd, "ERR expected file path\n"); + return; + } - if (!addplaylist(arg)) { - dprintf(fd, "ERR cannot add song to playlist\n"); - return; - } + if (!addplaylist(arg)) { + dprintf(fd, "ERR cannot add song to playlist\n"); + return; + } - dprintf(fd, "OK\n"); + dprintf(fd, "OK\n"); } -static void -cmdclear(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } - - stopsong(getcursong()); - clearplaylist(); - dprintf(fd, "OK\n"); -} +static void cmdclear(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } -static void -cmdremove(int fd, char *arg) -{ - Song *s; - const char *errstr; - int id; - - if (arg[0]) { - id = strtonum(arg, 0, INT_MAX, &errstr); - if (errstr) { - dprintf(fd, "ERR invalid song id\n"); - return; - } - s = findsongid(id); - if (!s) { - dprintf(fd, "ERR cannot find song with given id\n"); - return; - } - } else { - s = getcursong(); - stopsong(s); - } - - if (rmplaylist(s->id) < 0) { - dprintf(fd, "ERR failed to remove song\n"); - return; - } - dprintf(fd, "OK\n"); + stopsong(getcursong()); + clearplaylist(); + dprintf(fd, "OK\n"); } -static void -cmdplaylist(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } - dumpplaylist(fd); - dprintf(fd, "OK\n"); +static void cmdremove(int fd, char *arg) { + Song *s; + const char *errstr; + int id; + + if (arg[0]) { + id = strtonum(arg, 0, INT_MAX, &errstr); + if (errstr) { + dprintf(fd, "ERR invalid song id\n"); + return; + } + s = findsongid(id); + if (!s) { + dprintf(fd, "ERR cannot find song with given id\n"); + return; + } + } else { + s = getcursong(); + stopsong(s); + } + + if (rmplaylist(s->id) < 0) { + dprintf(fd, "ERR failed to remove song\n"); + return; + } + dprintf(fd, "OK\n"); } -static void -cmdclose(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } - - dprintf(fd, "OK\n"); - FD_CLR(fd, &master); - close(fd); +static void cmdplaylist(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } + dumpplaylist(fd); + dprintf(fd, "OK\n"); } -static void -cmdkill(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } - dprintf(fd, "OK\n"); - raise(SIGTERM); +static void cmdclose(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } + + dprintf(fd, "OK\n"); + FD_CLR(fd, &master); + close(fd); } -static void -cmdping(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } - dprintf(fd, "pong\nOK\n"); +static void cmdkill(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } + dprintf(fd, "OK\n"); + raise(SIGTERM); } -static void -cmdversion(int fd, char *arg) -{ - if (arg[0]) { - dprintf(fd, "ERR unexpected argument\n"); - return; - } - dprintf(fd, "version 0.0\nOK\n"); +static void cmdping(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } + dprintf(fd, "pong\nOK\n"); } -static void -cmdenable(int fd, char *arg) -{ - if (!arg[0]) { - dprintf(fd, "ERR expected output name\n"); - return; - } - if (enableoutput(arg) < 0) { - dprintf(fd, "ERR failed to enable output %s\n", arg); - return; - } - dprintf(fd, "OK\n"); +static void cmdversion(int fd, char *arg) { + if (arg[0]) { + dprintf(fd, "ERR unexpected argument\n"); + return; + } + dprintf(fd, "version 0.0\nOK\n"); } +static void cmdenable(int fd, char *arg) { + if (!arg[0]) { + dprintf(fd, "ERR expected output name\n"); + return; + } + if (enableoutput(arg) < 0) { + dprintf(fd, "ERR failed to enable output %s\n", arg); + return; + } + dprintf(fd, "OK\n"); +} -static void -cmddisable(int fd, char *arg) -{ - if (!arg[0]) { - dprintf(fd, "ERR expected output name\n"); - return; - } - if (disableoutput(arg) < 0) { - dprintf(fd, "ERR failed to disable output %s\n", arg); - return; - } - dprintf(fd, "OK\n"); +static void cmddisable(int fd, char *arg) { + if (!arg[0]) { + dprintf(fd, "ERR expected output name\n"); + return; + } + if (disableoutput(arg) < 0) { + dprintf(fd, "ERR failed to disable output %s\n", arg); + return; + } + dprintf(fd, "OK\n"); } -static void -cmdwait(int fd, char *arg) -{ - if (!arg[0]) { - dprintf(fd, "ERR expected event name\n"); - return; - } - - if (addsubscribername(fd, arg) < 0) { - dprintf(fd, "ERR unknown event type\n"); - return; - } - dprintf(fd, "OK\n"); +static void cmdwait(int fd, char *arg) { + if (!arg[0]) { + dprintf(fd, "ERR expected event name\n"); + return; + } + + if (addsubscribername(fd, arg) < 0) { + dprintf(fd, "ERR unknown event type\n"); + return; + } + dprintf(fd, "OK\n"); } static Cmd cmds[] = { - { "repeat", cmdrepeat }, - { "random", cmdrandom }, - { "status", cmdstatus }, - { "volume", cmdvolume }, - { "next", cmdnext }, - { "pause", cmdpause }, - { "play", cmdplay }, - { "prev", cmdprev }, - { "stop", cmdstop }, - { "add", cmdadd }, - { "clear", cmdclear }, - { "remove", cmdremove }, - { "playlist", cmdplaylist }, - { "close", cmdclose }, - { "kill", cmdkill }, - { "ping", cmdping }, - { "version", cmdversion }, - { "enable", cmdenable }, - { "disable", cmddisable }, - { "wait", cmdwait }, + {"repeat", cmdrepeat}, {"random", cmdrandom}, {"status", cmdstatus}, + {"volume", cmdvolume}, {"next", cmdnext}, {"pause", cmdpause}, + {"play", cmdplay}, {"prev", cmdprev}, {"stop", cmdstop}, + {"add", cmdadd}, {"clear", cmdclear}, {"remove", cmdremove}, + {"playlist", cmdplaylist}, {"close", cmdclose}, {"kill", cmdkill}, + {"ping", cmdping}, {"version", cmdversion}, {"enable", cmdenable}, + {"disable", cmddisable}, {"wait", cmdwait}, }; /* shamelessly taken from isakmpd ui.c */ -int -docmd(int clifd) -{ - static char *buf = 0; - static char *p; - static size_t sz; - static size_t resid; - ssize_t n; - size_t cmdlen, i; - char *new_buf; - int c; - - /* If no buffer, set it up. */ - if (!buf) { - sz = BUFSIZ; - buf = malloc(sz); - if (!buf) - err(1, "malloc"); - p = buf; - resid = sz; - } - /* If no place left in the buffer reallocate twice as large. */ - if (!resid) { - new_buf = realloc(buf, sz * 2); - if (!new_buf) - err(1, "realloc"); - buf = new_buf; - p = buf + sz; - resid = sz; - sz *= 2; - } - n = read(clifd, p, resid); - if (n <= 0) - return -1; - resid -= n; - while (n--) { - /* - * When we find a newline, cut off the line and feed it to the - * command processor. Then move the rest up-front. - */ - if (*p == '\n') { - *p = '\0'; - for (i = 0; i < LEN(cmds); i++) { - cmdlen = strlen(cmds[i].name); - if (!strncmp(cmds[i].name, buf, cmdlen) && - (buf[cmdlen] == '\0' || isspace(buf[cmdlen]))) { - /* strip leading whitespace */ - for (c = cmdlen; buf[c] && isspace(buf[c]); c++) - ; - cmds[i].fn(clifd, &buf[c]); - break; - } - } - if (i == LEN(cmds)) - dprintf(clifd, "ERR unknown command\n"); - memmove(buf, p + 1, n); - p = buf; - resid = sz - n; - continue; - } - p++; - } - return 0; +int docmd(int clifd) { + static char *buf = 0; + static char *p; + static size_t sz; + static size_t resid; + ssize_t n; + size_t cmdlen, i; + char *new_buf; + int c; + + /* If no buffer, set it up. */ + if (!buf) { + sz = BUFSIZ; + buf = malloc(sz); + if (!buf) + err(1, "malloc"); + p = buf; + resid = sz; + } + /* If no place left in the buffer reallocate twice as large. */ + if (!resid) { + new_buf = realloc(buf, sz * 2); + if (!new_buf) + err(1, "realloc"); + buf = new_buf; + p = buf + sz; + resid = sz; + sz *= 2; + } + n = read(clifd, p, resid); + if (n <= 0) + return -1; + resid -= n; + while (n--) { + /* + * When we find a newline, cut off the line and feed it to the + * command processor. Then move the rest up-front. + */ + if (*p == '\n') { + *p = '\0'; + for (i = 0; i < LEN(cmds); i++) { + cmdlen = strlen(cmds[i].name); + if (!strncmp(cmds[i].name, buf, cmdlen) && + (buf[cmdlen] == '\0' || isspace(buf[cmdlen]))) { + /* strip leading whitespace */ + for (c = cmdlen; buf[c] && isspace(buf[c]); c++) + ; + cmds[i].fn(clifd, &buf[c]); + break; + } + } + if (i == LEN(cmds)) + dprintf(clifd, "ERR unknown command\n"); + memmove(buf, p + 1, n); + p = buf; + resid = sz - n; + continue; + } + p++; + } + return 0; } - diff --git a/compat/reallocarray.c b/compat/reallocarray.c @@ -1,4 +1,5 @@ -/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */ +/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ + */ /* * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> * @@ -15,10 +16,10 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include <sys/types.h> #include <errno.h> #include <stdint.h> #include <stdlib.h> +#include <sys/types.h> #include "../compat.h" @@ -26,15 +27,13 @@ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */ -#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) +#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) -void * -reallocarray(void *optr, size_t nmemb, size_t size) -{ - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && - nmemb > 0 && SIZE_MAX / nmemb < size) { - errno = ENOMEM; - return NULL; - } - return realloc(optr, size * nmemb); +void *reallocarray(void *optr, size_t nmemb, size_t size) { + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && nmemb > 0 && + SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); } diff --git a/compat/strlcat.c b/compat/strlcat.c @@ -26,27 +26,25 @@ * Returns strlen(src) + MIN(siz, strlen(initial dst)). * If retval >= siz, truncation occurred. */ -size_t -strlcat(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - size_t dlen; - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; - return(dlen + (s - src)); /* count does not include NUL */ +size_t strlcat(char *dst, const char *src, size_t siz) { + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + if (n == 0) + return (dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + return (dlen + (s - src)); /* count does not include NUL */ } diff --git a/compat/strlcpy.c b/compat/strlcpy.c @@ -24,25 +24,23 @@ * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ -size_t -strlcpy(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - /* Copy as many bytes as will fit */ - if (n != 0) { - while (--n != 0) { - if ((*d++ = *s++) == '\0') - break; - } - } - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - return(s - src - 1); /* count does not include NUL */ +size_t strlcpy(char *dst, const char *src, size_t siz) { + char *d = dst; + const char *s = src; + size_t n = siz; + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + return (s - src - 1); /* count does not include NUL */ } diff --git a/compat/strtonum.c b/compat/strtonum.c @@ -23,45 +23,43 @@ #include "../compat.h" -#define INVALID 1 -#define TOOSMALL 2 -#define TOOLARGE 3 +#define INVALID 1 +#define TOOSMALL 2 +#define TOOLARGE 3 -long long -strtonum(const char *numstr, long long minval, long long maxval, - const char **errstrp) -{ - long long ll = 0; - char *ep; - int error = 0; - struct errval { - const char *errstr; - int err; - } ev[4] = { - { NULL, 0 }, - { "invalid", EINVAL }, - { "too small", ERANGE }, - { "too large", ERANGE }, - }; +long long strtonum(const char *numstr, long long minval, long long maxval, + const char **errstrp) { + long long ll = 0; + char *ep; + int error = 0; + struct errval { + const char *errstr; + int err; + } ev[4] = { + {NULL, 0}, + {"invalid", EINVAL}, + {"too small", ERANGE}, + {"too large", ERANGE}, + }; - ev[0].err = errno; - errno = 0; - if (minval > maxval) - error = INVALID; - else { - ll = strtoll(numstr, &ep, 10); - if (errno == EINVAL || numstr == ep || *ep != '\0') - error = INVALID; - else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) - error = TOOSMALL; - else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) - error = TOOLARGE; - } - if (errstrp != NULL) - *errstrp = ev[error].errstr; - errno = ev[error].err; - if (error) - ll = 0; + ev[0].err = errno; + errno = 0; + if (minval > maxval) + error = INVALID; + else { + ll = strtoll(numstr, &ep, 10); + if (errno == EINVAL || numstr == ep || *ep != '\0') + error = INVALID; + else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) + error = TOOSMALL; + else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) + error = TOOLARGE; + } + if (errstrp != NULL) + *errstrp = ev[error].errstr; + errno = ev[error].err; + if (error) + ll = 0; - return (ll); + return (ll); } diff --git a/config.def.h b/config.def.h @@ -1,34 +1,16 @@ #define RESAMPLEQUALITY SOXR_QQ Outputcfg outputcfgs[] = { - { - .name = "sndio", - .fmt = { - .bits = 16, - .rate = 48000, - .channels = 2 - }, - .enabled = 0, - .output = &sndiooutput - }, - { - .name = "alsa", - .fmt = { - .bits = 16, - .rate = 48000, - .channels = 2 - }, - .enabled = 1, - .output = &alsaoutput - }, - { - .name = "fifo", - .fmt = { - .bits = 16, - .rate = 48000, - .channels = 2 - }, - .enabled = 0, - .output = &fifooutput - }, + {.name = "sndio", + .fmt = {.bits = 16, .rate = 48000, .channels = 2}, + .enabled = 0, + .output = &sndiooutput}, + {.name = "alsa", + .fmt = {.bits = 16, .rate = 48000, .channels = 2}, + .enabled = 1, + .output = &alsaoutput}, + {.name = "fifo", + .fmt = {.bits = 16, .rate = 48000, .channels = 2}, + .enabled = 0, + .output = &fifooutput}, }; diff --git a/decoder.c b/decoder.c @@ -9,26 +9,24 @@ #include "sad.h" static struct { - char *ext; - Decoder *decoder; + char *ext; + Decoder *decoder; } decodermap[] = { - { ".wav", &wavdecoder }, - { ".flac", &wavdecoder }, - { ".mp3", &mp3decoder }, - { ".ogg", &vorbisdecoder }, + {".wav", &wavdecoder}, + {".flac", &wavdecoder}, + {".mp3", &mp3decoder}, + {".ogg", &vorbisdecoder}, }; -Decoder * -matchdecoder(const char *name) -{ - char *ext; - size_t i; +Decoder *matchdecoder(const char *name) { + char *ext; + size_t i; - ext = strrchr(name, '.'); - if (!ext) - return NULL; - for (i = 0; i < LEN(decodermap); i++) - if (!strcasecmp(decodermap[i].ext, ext)) - return decodermap[i].decoder; - return NULL; + ext = strrchr(name, '.'); + if (!ext) + return NULL; + for (i = 0; i < LEN(decodermap); i++) + if (!strcasecmp(decodermap[i].ext, ext)) + return decodermap[i].decoder; + return NULL; } diff --git a/fifo.c b/fifo.c @@ -14,66 +14,58 @@ static int fifofd = -1; int fifovolstatus = DEFAULTVOL; -static int -fifovol(int vol) -{ - (void)vol; +static int fifovol(int vol) { + (void)vol; - return 0; + return 0; } -static int -fifoopen(Format *fmt) -{ - (void)fmt; +static int fifoopen(Format *fmt) { + (void)fmt; - unlink("/tmp/sad-fifo"); - if (mkfifo("/tmp/sad-fifo", 0644) < 0) { - warn("mkfifo /tmp/sad-fifo"); - return -1; - } + unlink("/tmp/sad-fifo"); + if (mkfifo("/tmp/sad-fifo", 0644) < 0) { + warn("mkfifo /tmp/sad-fifo"); + return -1; + } - fifofd = open("/tmp/sad-fifo", O_RDWR | O_NONBLOCK); - if (fifofd < 0) { - warn("open /tmp/sad-fifo"); - return -1; - } - return 0; + fifofd = open("/tmp/sad-fifo", O_RDWR | O_NONBLOCK); + if (fifofd < 0) { + warn("open /tmp/sad-fifo"); + return -1; + } + return 0; } -static int -fifoplay(void *buf, size_t nbytes) -{ - ssize_t n, wrote; - char *p = buf; +static int fifoplay(void *buf, size_t nbytes) { + ssize_t n, wrote; + char *p = buf; - wrote = 0; - while (nbytes > 0) { - n = write(fifofd, &p[wrote], nbytes); - if (n < 0) - return nbytes; /* ignore errors */ - else if (n == 0) - break; - wrote += n; - nbytes -= n; - } - return wrote; + wrote = 0; + while (nbytes > 0) { + n = write(fifofd, &p[wrote], nbytes); + if (n < 0) + return nbytes; /* ignore errors */ + else if (n == 0) + break; + wrote += n; + nbytes -= n; + } + return wrote; } -static int -fifoclose(void) -{ - if (fifofd != -1) { - close(fifofd); - fifofd = -1; - } - return 0; +static int fifoclose(void) { + if (fifofd != -1) { + close(fifofd); + fifofd = -1; + } + return 0; } Output fifooutput = { .volstatus = &fifovolstatus, - .vol = fifovol, - .open = fifoopen, - .play = fifoplay, - .close = fifoclose, + .vol = fifovol, + .open = fifoopen, + .play = fifoplay, + .close = fifoclose, }; diff --git a/mp3.c b/mp3.c @@ -2,97 +2,87 @@ #include <err.h> #include <limits.h> -#include <stdio.h> #include <mpg123.h> +#include <stdio.h> #include "sad.h" static mpg123_handle *hdl; -static int -mp3open(Format *fmt, const char *name) -{ - int r; - long rate; - int channels, encoding; - - if (mpg123_init() != MPG123_OK) { - warnx("mpg123_init: failed"); - return -1; - } - - hdl = mpg123_new(NULL, NULL); - if (!hdl) { - warnx("mpg123_new: failed"); - goto err0; - } - - r = mpg123_open(hdl, name); - if (r != MPG123_OK) { - warnx("mpg123_open: failed"); - goto err1; - } - - r = mpg123_getformat(hdl, &rate, &channels, &encoding); - if (r != MPG123_OK) { - warnx("mpg123_getformat: failed"); - goto err2; - } - - fmt->bits = mpg123_encsize(encoding) * 8; - fmt->rate = rate; - fmt->channels = channels; - - if (initresamplers(fmt) < 0) - goto err2; - - return 0; +static int mp3open(Format *fmt, const char *name) { + int r; + long rate; + int channels, encoding; + + if (mpg123_init() != MPG123_OK) { + warnx("mpg123_init: failed"); + return -1; + } + + hdl = mpg123_new(NULL, NULL); + if (!hdl) { + warnx("mpg123_new: failed"); + goto err0; + } + + r = mpg123_open(hdl, name); + if (r != MPG123_OK) { + warnx("mpg123_open: failed"); + goto err1; + } + + r = mpg123_getformat(hdl, &rate, &channels, &encoding); + if (r != MPG123_OK) { + warnx("mpg123_getformat: failed"); + goto err2; + } + + fmt->bits = mpg123_encsize(encoding) * 8; + fmt->rate = rate; + fmt->channels = channels; + + if (initresamplers(fmt) < 0) + goto err2; + + return 0; err2: - mpg123_close(hdl); + mpg123_close(hdl); err1: - mpg123_delete(hdl); + mpg123_delete(hdl); err0: - mpg123_exit(); - hdl = NULL; - return -1; + mpg123_exit(); + hdl = NULL; + return -1; } -static int -mp3decode(void *buf, int nbytes) -{ - size_t actual; - int r; - - r = mpg123_read(hdl, buf, nbytes, &actual); - if (r == MPG123_DONE) - return 0; - else if (r != MPG123_OK) { - warnx("mpg123_read: failed"); - return -1; - } - return actual; +static int mp3decode(void *buf, int nbytes) { + size_t actual; + int r; + + r = mpg123_read(hdl, buf, nbytes, &actual); + if (r == MPG123_DONE) + return 0; + else if (r != MPG123_OK) { + warnx("mpg123_read: failed"); + return -1; + } + return actual; } -static int -mp3close(void) -{ - int r = 0; - - if (hdl) { - if (mpg123_close(hdl) != MPG123_OK) { - warnx("mpg123_close: failed"); - r = -1; - } - mpg123_delete(hdl); - mpg123_exit(); - } - hdl = NULL; - return r; +static int mp3close(void) { + int r = 0; + + if (hdl) { + if (mpg123_close(hdl) != MPG123_OK) { + warnx("mpg123_close: failed"); + r = -1; + } + mpg123_delete(hdl); + mpg123_exit(); + } + hdl = NULL; + return r; } -Decoder mp3decoder = { - .open = mp3open, - .decode = mp3decode, - .close = mp3close -}; +Decoder mp3decoder = {.open = mp3open, .decode = mp3decode, .close = mp3close}; diff --git a/notify.c b/notify.c @@ -10,100 +10,89 @@ #include "sad.h" Eventdesc Eventmap[] = { - { EVSONGFINISHED, "songfinished" }, + {EVSONGFINISHED, "songfinished"}, }; struct subscriber { - int clifd; - int event; - TAILQ_ENTRY(subscriber) entry; + int clifd; + int event; + TAILQ_ENTRY(subscriber) entry; }; static TAILQ_HEAD(subscribers, subscriber) subscribers; -int -initnotifier(void) -{ - TAILQ_INIT(&subscribers); - return 0; +int initnotifier(void) { + TAILQ_INIT(&subscribers); + return 0; } -int -addsubscriber(int clifd, int event) -{ - struct subscriber *s; - size_t i; +int addsubscriber(int clifd, int event) { + struct subscriber *s; + size_t i; - for (i = 0; i < LEN(Eventmap); i++) - if (Eventmap[i].event == event) - break; - if (i == LEN(Eventmap)) - return -1; + for (i = 0; i < LEN(Eventmap); i++) + if (Eventmap[i].event == event) + break; + if (i == LEN(Eventmap)) + return -1; - TAILQ_FOREACH(s, &subscribers, entry) - if (s->clifd == clifd && - s->event == event) - return 0; /* do not queue events */ + TAILQ_FOREACH(s, &subscribers, entry) + if (s->clifd == clifd && s->event == event) + return 0; /* do not queue events */ - s = malloc(sizeof(*s)); - if (!s) - err(1, "malloc"); + s = malloc(sizeof(*s)); + if (!s) + err(1, "malloc"); - s->clifd = clifd; - s->event = event; - TAILQ_INSERT_TAIL(&subscribers, s, entry); + s->clifd = clifd; + s->event = event; + TAILQ_INSERT_TAIL(&subscribers, s, entry); - return 0; + return 0; } -int -addsubscribername(int clifd, const char *name) -{ - size_t i; +int addsubscribername(int clifd, const char *name) { + size_t i; - for (i = 0; i < LEN(Eventmap); i++) - if (!strcmp(Eventmap[i].name, name)) - return addsubscriber(clifd, Eventmap[i].event); - return -1; + for (i = 0; i < LEN(Eventmap); i++) + if (!strcmp(Eventmap[i].name, name)) + return addsubscriber(clifd, Eventmap[i].event); + return -1; } -int -notify(int event) -{ - struct subscriber *s, *tmp; - size_t i; - - for (i = 0; i < LEN(Eventmap); i++) - if (Eventmap[i].event == event) - break; - if (i == LEN(Eventmap)) - return -1; - - TAILQ_FOREACH_SAFE(s, &subscribers, entry, tmp) { - if (s->event != event) - continue; - for (i = 0; i < LEN(Eventmap); i++) { - if (Eventmap[i].event == s->event) { - dprintf(s->clifd, "event: %s\n", Eventmap[i].name); - dprintf(s->clifd, "OK\n"); - TAILQ_REMOVE(&subscribers, s, entry); - free(s); - break; - } - } - } - return 0; +int notify(int event) { + struct subscriber *s, *tmp; + size_t i; + + for (i = 0; i < LEN(Eventmap); i++) + if (Eventmap[i].event == event) + break; + if (i == LEN(Eventmap)) + return -1; + + TAILQ_FOREACH_SAFE(s, &subscribers, entry, tmp) { + if (s->event != event) + continue; + for (i = 0; i < LEN(Eventmap); i++) { + if (Eventmap[i].event == s->event) { + dprintf(s->clifd, "event: %s\n", Eventmap[i].name); + dprintf(s->clifd, "OK\n"); + TAILQ_REMOVE(&subscribers, s, entry); + free(s); + break; + } + } + } + return 0; } -void -removesubscriber(int clifd) -{ - struct subscriber *s, *tmp; - - TAILQ_FOREACH_SAFE(s, &subscribers, entry, tmp) { - if (s->clifd == clifd) { - TAILQ_REMOVE(&subscribers, s, entry); - free(s); - } - } +void removesubscriber(int clifd) { + struct subscriber *s, *tmp; + + TAILQ_FOREACH_SAFE(s, &subscribers, entry, tmp) { + if (s->clifd == clifd) { + TAILQ_REMOVE(&subscribers, s, entry); + free(s); + } + } } diff --git a/output.c b/output.c @@ -2,305 +2,273 @@ #include <err.h> #include <limits.h> +#include <soxr.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <soxr.h> #include "sad.h" typedef struct { - char *name; - Format fmt; - int enabled; - int active; - Output *output; - soxr_t resampler; + char *name; + Format fmt; + int enabled; + int active; + Output *output; + soxr_t resampler; } Outputdesc; typedef struct { - char *name; - Format fmt; - int enabled; - Output *output; + char *name; + Format fmt; + int enabled; + Output *output; } Outputcfg; #include "config.h" static Outputdesc outputdescs[LEN(outputcfgs)]; -int -initoutputs(void) -{ - size_t i; - - for (i = 0; i < LEN(outputcfgs); i++) { - outputdescs[i].name = outputcfgs[i].name; - outputdescs[i].fmt = outputcfgs[i].fmt; - outputdescs[i].enabled = outputcfgs[i].enabled; - outputdescs[i].output = outputcfgs[i].output; - outputdescs[i].active = 0; - outputdescs[i].resampler = NULL; - } - return 0; +int initoutputs(void) { + size_t i; + + for (i = 0; i < LEN(outputcfgs); i++) { + outputdescs[i].name = outputcfgs[i].name; + outputdescs[i].fmt = outputcfgs[i].fmt; + outputdescs[i].enabled = outputcfgs[i].enabled; + outputdescs[i].output = outputcfgs[i].output; + outputdescs[i].active = 0; + outputdescs[i].resampler = NULL; + } + return 0; } -static int -initresampler(Format *fmt, Outputdesc *desc) -{ - soxr_quality_spec_t quality; - soxr_io_spec_t iospec; - - quality = soxr_quality_spec(RESAMPLEQUALITY, 0); - iospec = soxr_io_spec(SOXR_INT16_I, SOXR_INT16_I); - - if (desc->resampler) - soxr_delete(desc->resampler); - desc->resampler = soxr_create(fmt->rate, desc->fmt.rate, - desc->fmt.channels, - NULL, - &iospec, - &quality, - NULL); - if (!desc->resampler) { - warnx("soxr_create: failed to initialize resampler"); - return -1; - } - return 0; +static int initresampler(Format *fmt, Outputdesc *desc) { + soxr_quality_spec_t quality; + soxr_io_spec_t iospec; + + quality = soxr_quality_spec(RESAMPLEQUALITY, 0); + iospec = soxr_io_spec(SOXR_INT16_I, SOXR_INT16_I); + + if (desc->resampler) + soxr_delete(desc->resampler); + desc->resampler = soxr_create(fmt->rate, desc->fmt.rate, desc->fmt.channels, + NULL, &iospec, &quality, NULL); + if (!desc->resampler) { + warnx("soxr_create: failed to initialize resampler"); + return -1; + } + return 0; } -int -initresamplers(Format *fmt) -{ - Outputdesc *desc; - size_t i; - int r = 0; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (!desc->enabled) - continue; - if (initresampler(fmt, desc) < 0) - r = -1; - } - return r; +int initresamplers(Format *fmt) { + Outputdesc *desc; + size_t i; + int r = 0; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (!desc->enabled) + continue; + if (initresampler(fmt, desc) < 0) + r = -1; + } + return r; } -static int -openoutput(Outputdesc *desc) -{ - if (desc->active) - return 0; +static int openoutput(Outputdesc *desc) { + if (desc->active) + return 0; - if (desc->output->open(&desc->fmt) < 0) { - desc->active = 0; - return -1; - } + if (desc->output->open(&desc->fmt) < 0) { + desc->active = 0; + return -1; + } - printf("Opened %s output\n", desc->name); - desc->active = 1; - return 0; + printf("Opened %s output\n", desc->name); + desc->active = 1; + return 0; } -int -openoutputs(void) -{ - Outputdesc *desc; - size_t i; - int r = 0; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (!desc->enabled) - continue; - if (openoutput(desc) < 0) - r = -1; - } - return r; +int openoutputs(void) { + Outputdesc *desc; + size_t i; + int r = 0; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (!desc->enabled) + continue; + if (openoutput(desc) < 0) + r = -1; + } + return r; } -int -getvol(void) -{ - Outputdesc *desc; - size_t i; - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (!desc->enabled) - continue; - return *desc->output->volstatus; - } - return -1; +int getvol(void) { + Outputdesc *desc; + size_t i; + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (!desc->enabled) + continue; + return *desc->output->volstatus; + } + return -1; } -static int -closeoutput(Outputdesc *desc) -{ - if (!desc->active) - return 0; +static int closeoutput(Outputdesc *desc) { + if (!desc->active) + return 0; - printf("Closed %s output\n", desc->name); - desc->active = 0; - return desc->output->close(); + printf("Closed %s output\n", desc->name); + desc->active = 0; + return desc->output->close(); } -int -closeoutputs(void) -{ - Outputdesc *desc; - size_t i; - int r = 0; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (!desc->enabled) - continue; - if (closeoutput(desc) < 0) - r = -1; - } - return r; +int closeoutputs(void) { + Outputdesc *desc; + size_t i; + int r = 0; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (!desc->enabled) + continue; + if (closeoutput(desc) < 0) + r = -1; + } + return r; } -static int -playoutput(Format *fmt, Outputdesc *desc, void *buf, size_t nbytes) -{ - soxr_error_t e; - size_t inframes, outframes; - size_t framesize; - size_t idone, odone; - void *inbuf; - void *outbuf; - float ratio; - - if (!desc->active) - return 0; - - if (fmt->channels == 1 && desc->fmt.channels == 2) { - /* perform mono to stereo conversion */ - inbuf = malloc(nbytes * 2); - if (!inbuf) - err(1, "malloc"); - s16monotostereo(buf, inbuf, nbytes / 2); - nbytes *= 2; - } else if (fmt->channels == 2 && desc->fmt.channels == 1) { - /* perform stereo to mono conversion */ - inbuf = malloc(nbytes / 2); - if (!inbuf) - err(1, "malloc"); - s16stereotomono(buf, inbuf, nbytes / 4); - nbytes /= 2; - } else { - inbuf = malloc(nbytes); - if (!inbuf) - err(1, "malloc"); - memcpy(inbuf, buf, nbytes); - } - - if (fmt->rate == desc->fmt.rate) { - if (desc->output->play(inbuf, nbytes) < 0) { - free(inbuf); - return -1; - } - free(inbuf); - return 0; - } - - /* perform SRC */ - framesize = (desc->fmt.bits + 7) / 8 * desc->fmt.channels; - inframes = nbytes / framesize; - ratio = (float)desc->fmt.rate / fmt->rate; - outframes = inframes * ratio + 1; - outbuf = malloc(outframes * framesize); - if (!outbuf) - err(1, "malloc"); - - e = soxr_process(desc->resampler, inbuf, inframes, - &idone, outbuf, outframes, - &odone); - if (!e) { - if (desc->output->play(outbuf, odone * framesize) < 0) - return -1; - free(inbuf); - free(outbuf); - return 0; - } - - warnx("soxr_process: failed"); - free(inbuf); - free(outbuf); - return -1; +static int playoutput(Format *fmt, Outputdesc *desc, void *buf, size_t nbytes) { + soxr_error_t e; + size_t inframes, outframes; + size_t framesize; + size_t idone, odone; + void *inbuf; + void *outbuf; + float ratio; + + if (!desc->active) + return 0; + + if (fmt->channels == 1 && desc->fmt.channels == 2) { + /* perform mono to stereo conversion */ + inbuf = malloc(nbytes * 2); + if (!inbuf) + err(1, "malloc"); + s16monotostereo(buf, inbuf, nbytes / 2); + nbytes *= 2; + } else if (fmt->channels == 2 && desc->fmt.channels == 1) { + /* perform stereo to mono conversion */ + inbuf = malloc(nbytes / 2); + if (!inbuf) + err(1, "malloc"); + s16stereotomono(buf, inbuf, nbytes / 4); + nbytes /= 2; + } else { + inbuf = malloc(nbytes); + if (!inbuf) + err(1, "malloc"); + memcpy(inbuf, buf, nbytes); + } + + if (fmt->rate == desc->fmt.rate) { + if (desc->output->play(inbuf, nbytes) < 0) { + free(inbuf); + return -1; + } + free(inbuf); + return 0; + } + + /* perform SRC */ + framesize = (desc->fmt.bits + 7) / 8 * desc->fmt.channels; + inframes = nbytes / framesize; + ratio = (float)desc->fmt.rate / fmt->rate; + outframes = inframes * ratio + 1; + outbuf = malloc(outframes * framesize); + if (!outbuf) + err(1, "malloc"); + + e = soxr_process(desc->resampler, inbuf, inframes, &idone, outbuf, outframes, + &odone); + if (!e) { + if (desc->output->play(outbuf, odone * framesize) < 0) + return -1; + free(inbuf); + free(outbuf); + return 0; + } + + warnx("soxr_process: failed"); + free(inbuf); + free(outbuf); + return -1; } -int -playoutputs(Format *fmt, void *inbuf, size_t nbytes) -{ - Outputdesc *desc; - size_t i; - int r = 0; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (!desc->enabled) - continue; - if (playoutput(fmt, desc, inbuf, nbytes) < 0) - r = -1; - } - return r; +int playoutputs(Format *fmt, void *inbuf, size_t nbytes) { + Outputdesc *desc; + size_t i; + int r = 0; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (!desc->enabled) + continue; + if (playoutput(fmt, desc, inbuf, nbytes) < 0) + r = -1; + } + return r; } -int -setvol(int vol) -{ - Outputdesc *desc; - size_t i; - int r = 0; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (!desc->enabled) - continue; - if (desc->output->vol(vol) < 0) { - r = -1; - } - *desc->output->volstatus = vol; - } - return r; +int setvol(int vol) { + Outputdesc *desc; + size_t i; + int r = 0; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (!desc->enabled) + continue; + if (desc->output->vol(vol) < 0) { + r = -1; + } + *desc->output->volstatus = vol; + } + return r; } -int -enableoutput(const char *name) -{ - Outputdesc *desc; - size_t i; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (strcmp(desc->name, name)) - continue; - if (openoutput(desc) < 0) { - desc->enabled = 0; - return -1; - } else { - desc->enabled = 1; - return 0; - } - } - return -1; +int enableoutput(const char *name) { + Outputdesc *desc; + size_t i; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (strcmp(desc->name, name)) + continue; + if (openoutput(desc) < 0) { + desc->enabled = 0; + return -1; + } else { + desc->enabled = 1; + return 0; + } + } + return -1; } -int -disableoutput(const char *name) -{ - Outputdesc *desc; - size_t i; - - for (i = 0; i < LEN(outputdescs); i++) { - desc = &outputdescs[i]; - if (strcmp(desc->name, name)) - continue; - desc->enabled = 0; - return closeoutput(desc); - } - return -1; +int disableoutput(const char *name) { + Outputdesc *desc; + size_t i; + + for (i = 0; i < LEN(outputdescs); i++) { + desc = &outputdescs[i]; + if (strcmp(desc->name, name)) + continue; + desc->enabled = 0; + return closeoutput(desc); + } + return -1; } - diff --git a/pcm.c b/pcm.c @@ -5,40 +5,32 @@ #include "sad.h" -void -s16monotostereo(short *in, short *out, size_t nsamples) -{ - size_t i; - - for (i = 0; i < nsamples; i++) { - out[i * 2] = in[i]; - out[i * 2 + 1] = in[i]; - } +void s16monotostereo(short *in, short *out, size_t nsamples) { + size_t i; + + for (i = 0; i < nsamples; i++) { + out[i * 2] = in[i]; + out[i * 2 + 1] = in[i]; + } } -void -s16stereotomono(short *in, short *out, size_t nsamples) -{ - size_t i; +void s16stereotomono(short *in, short *out, size_t nsamples) { + size_t i; - for (i = 0; i < nsamples; i++) - out[i] = in[i * 2] / 2 + in[i * 2 + 1] / 2; + for (i = 0; i < nsamples; i++) + out[i] = in[i * 2] / 2 + in[i * 2 + 1] / 2; } -void -s16tofloat(short *in, float *out, size_t nsamples) -{ - size_t i; +void s16tofloat(short *in, float *out, size_t nsamples) { + size_t i; - for (i = 0; i < nsamples; i++) - out[i] = in[i] / 32768.f; + for (i = 0; i < nsamples; i++) + out[i] = in[i] / 32768.f; } -void -floattos16(float *in, short *out, size_t nsamples) -{ - size_t i; +void floattos16(float *in, short *out, size_t nsamples) { + size_t i; - for (i = 0; i < nsamples; i++) - out[i] = in[i] * 32768.f; + for (i = 0; i < nsamples; i++) + out[i] = in[i] * 32768.f; } diff --git a/playlist.c b/playlist.c @@ -12,219 +12,182 @@ #include "sad.h" static Playlist playlist; -static int rollingid; - -Song * -addplaylist(const char *path) -{ - Song *s; - Decoder *d; - - if (access(path, F_OK) < 0) - return NULL; - - d = matchdecoder(path); - if (!d) - return NULL; - - if (!playlist.nsongs || playlist.nsongs + 1 > playlist.maxsongs) { - playlist.maxsongs += 4096; - playlist.songs = reallocarray(playlist.songs, playlist.maxsongs, sizeof(Song *)); - if (!playlist.songs) - err(1, "reallocarray"); - } - - s = malloc(sizeof(*s)); - if (!s) - err(1, "malloc"); - - playlist.songs[playlist.nsongs] = s; - strlcpy(s->path, path, sizeof(s->path)); - s->id = rollingid++; - s->state = NONE; - s->decoder = d; - if (!playlist.nsongs) - playlist.cursong = s; - playlist.nsongs++; - return s; +static int rollingid; + +Song *addplaylist(const char *path) { + Song *s; + Decoder *d; + + if (access(path, F_OK) < 0) + return NULL; + + d = matchdecoder(path); + if (!d) + return NULL; + + if (!playlist.nsongs || playlist.nsongs + 1 > playlist.maxsongs) { + playlist.maxsongs += 4096; + playlist.songs = + reallocarray(playlist.songs, playlist.maxsongs, sizeof(Song *)); + if (!playlist.songs) + err(1, "reallocarray"); + } + + s = malloc(sizeof(*s)); + if (!s) + err(1, "malloc"); + + playlist.songs[playlist.nsongs] = s; + strlcpy(s->path, path, sizeof(s->path)); + s->id = rollingid++; + s->state = NONE; + s->decoder = d; + if (!playlist.nsongs) + playlist.cursong = s; + playlist.nsongs++; + return s; } -int -rmplaylist(int id) -{ - Song *s; - size_t i; - - for (i = 0; i < playlist.nsongs; i++) { - s = playlist.songs[i]; - if (s->id == id) - break; - } - if (i == playlist.nsongs) - return -1; - - free(s); - s = getnextsong(); - memmove(&playlist.songs[i], &playlist.songs[i + 1], - (playlist.nsongs - i - 1) * sizeof(playlist.songs[i])); - putcursong(s); - playlist.nsongs--; - return 0; +int rmplaylist(int id) { + Song *s; + size_t i; + + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + if (s->id == id) + break; + } + if (i == playlist.nsongs) + return -1; + + free(s); + s = getnextsong(); + memmove(&playlist.songs[i], &playlist.songs[i + 1], + (playlist.nsongs - i - 1) * sizeof(playlist.songs[i])); + putcursong(s); + playlist.nsongs--; + return 0; } -Song * -findsong(const char *path) -{ - Song *s; - size_t i; - - for (i = 0; i < playlist.nsongs; i++) { - s = playlist.songs[i]; - if (!strcmp(s->path, path)) - return s; - } - return NULL; -} +Song *findsong(const char *path) { + Song *s; + size_t i; -Song * -findsongid(int id) -{ - Song *s; - size_t i; - - for (i = 0; i < playlist.nsongs; i++) { - s = playlist.songs[i]; - if (s->id == id) - return s; - } - return NULL; + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + if (!strcmp(s->path, path)) + return s; + } + return NULL; } -Song * -getnextsong(void) -{ - Song *s, *cur; - size_t i; - - cur = playlist.cursong; - for (i = 0; i < playlist.nsongs; i++) { - s = playlist.songs[i]; - if (s->id == cur->id) - break; - } - if (i == playlist.nsongs) - return NULL; - if (i == playlist.nsongs - 1) - s = playlist.songs[0]; - else - s = playlist.songs[i + 1]; - return s; -} +Song *findsongid(int id) { + Song *s; + size_t i; -Song * -getprevsong(void) -{ - Song *s, *cur; - size_t i; - - cur = playlist.cursong; - for (i = 0; i < playlist.nsongs; i++) { - s = playlist.songs[i]; - if (s->id == cur->id) - break; - } - if (i == playlist.nsongs) - return NULL; - if (i == 0) - s = playlist.songs[playlist.nsongs - 1]; - else - s = playlist.songs[i - 1]; - return s; + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + if (s->id == id) + return s; + } + return NULL; } -Song * -getcursong(void) -{ - return playlist.cursong; +Song *getnextsong(void) { + Song *s, *cur; + size_t i; + + cur = playlist.cursong; + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + if (s->id == cur->id) + break; + } + if (i == playlist.nsongs) + return NULL; + if (i == playlist.nsongs - 1) + s = playlist.songs[0]; + else + s = playlist.songs[i + 1]; + return s; } -void -putcursong(Song *s) -{ - playlist.cursong = s; +Song *getprevsong(void) { + Song *s, *cur; + size_t i; + + cur = playlist.cursong; + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + if (s->id == cur->id) + break; + } + if (i == playlist.nsongs) + return NULL; + if (i == 0) + s = playlist.songs[playlist.nsongs - 1]; + else + s = playlist.songs[i - 1]; + return s; } -void -dumpplaylist(int fd) -{ - Song *s; - size_t i; +Song *getcursong(void) { return playlist.cursong; } - for (i = 0; i < playlist.nsongs; i++) { - s = playlist.songs[i]; - dprintf(fd, "%d: %s\n", s->id, s->path); - } -} +void putcursong(Song *s) { playlist.cursong = s; } -void -clearplaylist(void) -{ - size_t i; - - for (i = 0; i < playlist.nsongs; i++) { - free(playlist.songs[i]); - playlist.songs[i] = NULL; - } - playlist.nsongs = 0; - rollingid = 0; - playlist.cursong = NULL; -} +void dumpplaylist(int fd) { + Song *s; + size_t i; -Song * -picknextsong(void) -{ - Song *s; - - switch (playlist.mode) { - case REPEAT: - s = getnextsong(); - break; - case RANDOM: - srand(time(NULL)); - s = playlist.songs[rand() % playlist.nsongs]; - break; - default: - s = NULL; - break; - } - return s; + for (i = 0; i < playlist.nsongs; i++) { + s = playlist.songs[i]; + dprintf(fd, "%d: %s\n", s->id, s->path); + } } -void -playsong(Song *new) -{ - stopsong(playlist.cursong); - new->state = PREPARE; - playlist.cursong = new; +void clearplaylist(void) { + size_t i; + + for (i = 0; i < playlist.nsongs; i++) { + free(playlist.songs[i]); + playlist.songs[i] = NULL; + } + playlist.nsongs = 0; + rollingid = 0; + playlist.cursong = NULL; } -void -stopsong(Song *s) -{ - if (s && s->state != NONE) { - s->decoder->close(); - s->state = NONE; - } +Song *picknextsong(void) { + Song *s; + + switch (playlist.mode) { + case REPEAT: + s = getnextsong(); + break; + case RANDOM: + srand(time(NULL)); + s = playlist.songs[rand() % playlist.nsongs]; + break; + default: + s = NULL; + break; + } + return s; } -void -playlistmode(int mode) -{ - playlist.mode = mode; +void playsong(Song *new) { + stopsong(playlist.cursong); + new->state = PREPARE; + playlist.cursong = new; } -int -getplaylistmode(void) -{ - return playlist.mode; +void stopsong(Song *s) { + if (s && s->state != NONE) { + s->decoder->close(); + s->state = NONE; + } } + +void playlistmode(int mode) { playlist.mode = mode; } + +int getplaylistmode(void) { return playlist.mode; } diff --git a/queue.h b/queue.h @@ -32,11 +32,11 @@ * @(#)queue.h 8.5 (Berkeley) 8/20/94 */ -#ifndef _SYS_QUEUE_H_ -#define _SYS_QUEUE_H_ +#ifndef _SYS_QUEUE_H_ +#define _SYS_QUEUE_H_ /* - * This file defines five types of data structures: singly-linked lists, + * This file defines five types of data structures: singly-linked lists, * lists, simple queues, tail queues, and circular queues. * * @@ -91,558 +91,564 @@ /* * Singly-linked List definitions. */ -#define SLIST_HEAD(name, type) \ -struct name { \ - struct type *slh_first; /* first element */ \ -} - -#define SLIST_HEAD_INITIALIZER(head) \ - { NULL } - -#define SLIST_ENTRY(type) \ -struct { \ - struct type *sle_next; /* next element */ \ -} - +#define SLIST_HEAD(name, type) \ + struct name { \ + struct type *slh_first; /* first element */ \ + } + +#define SLIST_HEAD_INITIALIZER(head) \ + { NULL } + +#define SLIST_ENTRY(type) \ + struct { \ + struct type *sle_next; /* next element */ \ + } + /* * Singly-linked List access methods. */ -#define SLIST_FIRST(head) ((head)->slh_first) -#define SLIST_END(head) NULL -#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) -#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) +#define SLIST_FIRST(head) ((head)->slh_first) +#define SLIST_END(head) NULL +#define SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head)) +#define SLIST_NEXT(elm, field) ((elm)->field.sle_next) -#define SLIST_FOREACH(var, head, field) \ - for((var) = SLIST_FIRST(head); \ - (var) != SLIST_END(head); \ - (var) = SLIST_NEXT(var, field)) +#define SLIST_FOREACH(var, head, field) \ + for ((var) = SLIST_FIRST(head); (var) != SLIST_END(head); \ + (var) = SLIST_NEXT(var, field)) -#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = SLIST_FIRST(head); \ - (var) && ((tvar) = SLIST_NEXT(var, field), 1); \ - (var) = (tvar)) +#define SLIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SLIST_FIRST(head); \ + (var) && ((tvar) = SLIST_NEXT(var, field), 1); (var) = (tvar)) /* * Singly-linked List functions. */ -#define SLIST_INIT(head) { \ - SLIST_FIRST(head) = SLIST_END(head); \ -} - -#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \ - (elm)->field.sle_next = (slistelm)->field.sle_next; \ - (slistelm)->field.sle_next = (elm); \ -} while (0) - -#define SLIST_INSERT_HEAD(head, elm, field) do { \ - (elm)->field.sle_next = (head)->slh_first; \ - (head)->slh_first = (elm); \ -} while (0) - -#define SLIST_REMOVE_AFTER(elm, field) do { \ - (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ -} while (0) - -#define SLIST_REMOVE_HEAD(head, field) do { \ - (head)->slh_first = (head)->slh_first->field.sle_next; \ -} while (0) - -#define SLIST_REMOVE(head, elm, type, field) do { \ - if ((head)->slh_first == (elm)) { \ - SLIST_REMOVE_HEAD((head), field); \ - } else { \ - struct type *curelm = (head)->slh_first; \ - \ - while (curelm->field.sle_next != (elm)) \ - curelm = curelm->field.sle_next; \ - curelm->field.sle_next = \ - curelm->field.sle_next->field.sle_next; \ - _Q_INVALIDATE((elm)->field.sle_next); \ - } \ -} while (0) +#define SLIST_INIT(head) \ + { SLIST_FIRST(head) = SLIST_END(head); } + +#define SLIST_INSERT_AFTER(slistelm, elm, field) \ + do { \ + (elm)->field.sle_next = (slistelm)->field.sle_next; \ + (slistelm)->field.sle_next = (elm); \ + } while (0) + +#define SLIST_INSERT_HEAD(head, elm, field) \ + do { \ + (elm)->field.sle_next = (head)->slh_first; \ + (head)->slh_first = (elm); \ + } while (0) + +#define SLIST_REMOVE_AFTER(elm, field) \ + do { \ + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \ + } while (0) + +#define SLIST_REMOVE_HEAD(head, field) \ + do { \ + (head)->slh_first = (head)->slh_first->field.sle_next; \ + } while (0) + +#define SLIST_REMOVE(head, elm, type, field) \ + do { \ + if ((head)->slh_first == (elm)) { \ + SLIST_REMOVE_HEAD((head), field); \ + } else { \ + struct type *curelm = (head)->slh_first; \ + \ + while (curelm->field.sle_next != (elm)) \ + curelm = curelm->field.sle_next; \ + curelm->field.sle_next = curelm->field.sle_next->field.sle_next; \ + _Q_INVALIDATE((elm)->field.sle_next); \ + } \ + } while (0) /* * List definitions. */ -#define LIST_HEAD(name, type) \ -struct name { \ - struct type *lh_first; /* first element */ \ -} +#define LIST_HEAD(name, type) \ + struct name { \ + struct type *lh_first; /* first element */ \ + } -#define LIST_HEAD_INITIALIZER(head) \ - { NULL } +#define LIST_HEAD_INITIALIZER(head) \ + { NULL } -#define LIST_ENTRY(type) \ -struct { \ - struct type *le_next; /* next element */ \ - struct type **le_prev; /* address of previous next element */ \ -} +#define LIST_ENTRY(type) \ + struct { \ + struct type *le_next; /* next element */ \ + struct type **le_prev; /* address of previous next element */ \ + } /* * List access methods */ -#define LIST_FIRST(head) ((head)->lh_first) -#define LIST_END(head) NULL -#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) -#define LIST_NEXT(elm, field) ((elm)->field.le_next) +#define LIST_FIRST(head) ((head)->lh_first) +#define LIST_END(head) NULL +#define LIST_EMPTY(head) (LIST_FIRST(head) == LIST_END(head)) +#define LIST_NEXT(elm, field) ((elm)->field.le_next) -#define LIST_FOREACH(var, head, field) \ - for((var) = LIST_FIRST(head); \ - (var)!= LIST_END(head); \ - (var) = LIST_NEXT(var, field)) +#define LIST_FOREACH(var, head, field) \ + for ((var) = LIST_FIRST(head); (var) != LIST_END(head); \ + (var) = LIST_NEXT(var, field)) -#define LIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = LIST_FIRST(head); \ - (var) && ((tvar) = LIST_NEXT(var, field), 1); \ - (var) = (tvar)) +#define LIST_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = LIST_FIRST(head); (var) && ((tvar) = LIST_NEXT(var, field), 1); \ + (var) = (tvar)) /* * List functions. */ -#define LIST_INIT(head) do { \ - LIST_FIRST(head) = LIST_END(head); \ -} while (0) - -#define LIST_INSERT_AFTER(listelm, elm, field) do { \ - if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ - (listelm)->field.le_next->field.le_prev = \ - &(elm)->field.le_next; \ - (listelm)->field.le_next = (elm); \ - (elm)->field.le_prev = &(listelm)->field.le_next; \ -} while (0) - -#define LIST_INSERT_BEFORE(listelm, elm, field) do { \ - (elm)->field.le_prev = (listelm)->field.le_prev; \ - (elm)->field.le_next = (listelm); \ - *(listelm)->field.le_prev = (elm); \ - (listelm)->field.le_prev = &(elm)->field.le_next; \ -} while (0) - -#define LIST_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.le_next = (head)->lh_first) != NULL) \ - (head)->lh_first->field.le_prev = &(elm)->field.le_next;\ - (head)->lh_first = (elm); \ - (elm)->field.le_prev = &(head)->lh_first; \ -} while (0) - -#define LIST_REMOVE(elm, field) do { \ - if ((elm)->field.le_next != NULL) \ - (elm)->field.le_next->field.le_prev = \ - (elm)->field.le_prev; \ - *(elm)->field.le_prev = (elm)->field.le_next; \ - _Q_INVALIDATE((elm)->field.le_prev); \ - _Q_INVALIDATE((elm)->field.le_next); \ -} while (0) - -#define LIST_REPLACE(elm, elm2, field) do { \ - if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ - (elm2)->field.le_next->field.le_prev = \ - &(elm2)->field.le_next; \ - (elm2)->field.le_prev = (elm)->field.le_prev; \ - *(elm2)->field.le_prev = (elm2); \ - _Q_INVALIDATE((elm)->field.le_prev); \ - _Q_INVALIDATE((elm)->field.le_next); \ -} while (0) +#define LIST_INIT(head) \ + do { \ + LIST_FIRST(head) = LIST_END(head); \ + } while (0) + +#define LIST_INSERT_AFTER(listelm, elm, field) \ + do { \ + if (((elm)->field.le_next = (listelm)->field.le_next) != NULL) \ + (listelm)->field.le_next->field.le_prev = &(elm)->field.le_next; \ + (listelm)->field.le_next = (elm); \ + (elm)->field.le_prev = &(listelm)->field.le_next; \ + } while (0) + +#define LIST_INSERT_BEFORE(listelm, elm, field) \ + do { \ + (elm)->field.le_prev = (listelm)->field.le_prev; \ + (elm)->field.le_next = (listelm); \ + *(listelm)->field.le_prev = (elm); \ + (listelm)->field.le_prev = &(elm)->field.le_next; \ + } while (0) + +#define LIST_INSERT_HEAD(head, elm, field) \ + do { \ + if (((elm)->field.le_next = (head)->lh_first) != NULL) \ + (head)->lh_first->field.le_prev = &(elm)->field.le_next; \ + (head)->lh_first = (elm); \ + (elm)->field.le_prev = &(head)->lh_first; \ + } while (0) + +#define LIST_REMOVE(elm, field) \ + do { \ + if ((elm)->field.le_next != NULL) \ + (elm)->field.le_next->field.le_prev = (elm)->field.le_prev; \ + *(elm)->field.le_prev = (elm)->field.le_next; \ + _Q_INVALIDATE((elm)->field.le_prev); \ + _Q_INVALIDATE((elm)->field.le_next); \ + } while (0) + +#define LIST_REPLACE(elm, elm2, field) \ + do { \ + if (((elm2)->field.le_next = (elm)->field.le_next) != NULL) \ + (elm2)->field.le_next->field.le_prev = &(elm2)->field.le_next; \ + (elm2)->field.le_prev = (elm)->field.le_prev; \ + *(elm2)->field.le_prev = (elm2); \ + _Q_INVALIDATE((elm)->field.le_prev); \ + _Q_INVALIDATE((elm)->field.le_next); \ + } while (0) /* * Simple queue definitions. */ -#define SIMPLEQ_HEAD(name, type) \ -struct name { \ - struct type *sqh_first; /* first element */ \ - struct type **sqh_last; /* addr of last next element */ \ -} +#define SIMPLEQ_HEAD(name, type) \ + struct name { \ + struct type *sqh_first; /* first element */ \ + struct type **sqh_last; /* addr of last next element */ \ + } -#define SIMPLEQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).sqh_first } +#define SIMPLEQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).sqh_first } -#define SIMPLEQ_ENTRY(type) \ -struct { \ - struct type *sqe_next; /* next element */ \ -} +#define SIMPLEQ_ENTRY(type) \ + struct { \ + struct type *sqe_next; /* next element */ \ + } /* * Simple queue access methods. */ -#define SIMPLEQ_FIRST(head) ((head)->sqh_first) -#define SIMPLEQ_END(head) NULL -#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) -#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) +#define SIMPLEQ_FIRST(head) ((head)->sqh_first) +#define SIMPLEQ_END(head) NULL +#define SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head)) +#define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) -#define SIMPLEQ_FOREACH(var, head, field) \ - for((var) = SIMPLEQ_FIRST(head); \ - (var) != SIMPLEQ_END(head); \ - (var) = SIMPLEQ_NEXT(var, field)) +#define SIMPLEQ_FOREACH(var, head, field) \ + for ((var) = SIMPLEQ_FIRST(head); (var) != SIMPLEQ_END(head); \ + (var) = SIMPLEQ_NEXT(var, field)) -#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = SIMPLEQ_FIRST(head); \ - (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); \ - (var) = (tvar)) +#define SIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = SIMPLEQ_FIRST(head); \ + (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1); (var) = (tvar)) /* * Simple queue functions. */ -#define SIMPLEQ_INIT(head) do { \ - (head)->sqh_first = NULL; \ - (head)->sqh_last = &(head)->sqh_first; \ -} while (0) - -#define SIMPLEQ_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ - (head)->sqh_last = &(elm)->field.sqe_next; \ - (head)->sqh_first = (elm); \ -} while (0) - -#define SIMPLEQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.sqe_next = NULL; \ - *(head)->sqh_last = (elm); \ - (head)->sqh_last = &(elm)->field.sqe_next; \ -} while (0) - -#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\ - (head)->sqh_last = &(elm)->field.sqe_next; \ - (listelm)->field.sqe_next = (elm); \ -} while (0) - -#define SIMPLEQ_REMOVE_HEAD(head, field) do { \ - if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ - (head)->sqh_last = &(head)->sqh_first; \ -} while (0) - -#define SIMPLEQ_REMOVE_AFTER(head, elm, field) do { \ - if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \ - == NULL) \ - (head)->sqh_last = &(elm)->field.sqe_next; \ -} while (0) +#define SIMPLEQ_INIT(head) \ + do { \ + (head)->sqh_first = NULL; \ + (head)->sqh_last = &(head)->sqh_first; \ + } while (0) + +#define SIMPLEQ_INSERT_HEAD(head, elm, field) \ + do { \ + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (head)->sqh_first = (elm); \ + } while (0) + +#define SIMPLEQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.sqe_next = NULL; \ + *(head)->sqh_last = (elm); \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + } while (0) + +#define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + (listelm)->field.sqe_next = (elm); \ + } while (0) + +#define SIMPLEQ_REMOVE_HEAD(head, field) \ + do { \ + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \ + (head)->sqh_last = &(head)->sqh_first; \ + } while (0) + +#define SIMPLEQ_REMOVE_AFTER(head, elm, field) \ + do { \ + if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) == \ + NULL) \ + (head)->sqh_last = &(elm)->field.sqe_next; \ + } while (0) /* * XOR Simple queue definitions. */ -#define XSIMPLEQ_HEAD(name, type) \ -struct name { \ - struct type *sqx_first; /* first element */ \ - struct type **sqx_last; /* addr of last next element */ \ - unsigned long sqx_cookie; \ -} - -#define XSIMPLEQ_ENTRY(type) \ -struct { \ - struct type *sqx_next; /* next element */ \ -} +#define XSIMPLEQ_HEAD(name, type) \ + struct name { \ + struct type *sqx_first; /* first element */ \ + struct type **sqx_last; /* addr of last next element */ \ + unsigned long sqx_cookie; \ + } + +#define XSIMPLEQ_ENTRY(type) \ + struct { \ + struct type *sqx_next; /* next element */ \ + } /* * XOR Simple queue access methods. */ -#define XSIMPLEQ_XOR(head, ptr) ((__typeof(ptr))((head)->sqx_cookie ^ \ - (unsigned long)(ptr))) -#define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first)) -#define XSIMPLEQ_END(head) NULL -#define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head)) -#define XSIMPLEQ_NEXT(head, elm, field) XSIMPLEQ_XOR(head, ((elm)->field.sqx_next)) - - -#define XSIMPLEQ_FOREACH(var, head, field) \ - for ((var) = XSIMPLEQ_FIRST(head); \ - (var) != XSIMPLEQ_END(head); \ - (var) = XSIMPLEQ_NEXT(head, var, field)) - -#define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = XSIMPLEQ_FIRST(head); \ - (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); \ - (var) = (tvar)) +#define XSIMPLEQ_XOR(head, ptr) \ + ((__typeof(ptr))((head)->sqx_cookie ^ (unsigned long)(ptr))) +#define XSIMPLEQ_FIRST(head) XSIMPLEQ_XOR(head, ((head)->sqx_first)) +#define XSIMPLEQ_END(head) NULL +#define XSIMPLEQ_EMPTY(head) (XSIMPLEQ_FIRST(head) == XSIMPLEQ_END(head)) +#define XSIMPLEQ_NEXT(head, elm, field) \ + XSIMPLEQ_XOR(head, ((elm)->field.sqx_next)) + +#define XSIMPLEQ_FOREACH(var, head, field) \ + for ((var) = XSIMPLEQ_FIRST(head); (var) != XSIMPLEQ_END(head); \ + (var) = XSIMPLEQ_NEXT(head, var, field)) + +#define XSIMPLEQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = XSIMPLEQ_FIRST(head); \ + (var) && ((tvar) = XSIMPLEQ_NEXT(head, var, field), 1); (var) = (tvar)) /* * XOR Simple queue functions. */ -#define XSIMPLEQ_INIT(head) do { \ - arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \ - (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \ - (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ -} while (0) - -#define XSIMPLEQ_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.sqx_next = (head)->sqx_first) == \ - XSIMPLEQ_XOR(head, NULL)) \ - (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ - (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \ -} while (0) - -#define XSIMPLEQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \ - *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \ - (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ -} while (0) - -#define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \ - XSIMPLEQ_XOR(head, NULL)) \ - (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ - (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \ -} while (0) - -#define XSIMPLEQ_REMOVE_HEAD(head, field) do { \ - if (((head)->sqx_first = XSIMPLEQ_XOR(head, \ - (head)->sqx_first)->field.sqx_next) == XSIMPLEQ_XOR(head, NULL)) \ - (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ -} while (0) - -#define XSIMPLEQ_REMOVE_AFTER(head, elm, field) do { \ - if (((elm)->field.sqx_next = XSIMPLEQ_XOR(head, \ - (elm)->field.sqx_next)->field.sqx_next) \ - == XSIMPLEQ_XOR(head, NULL)) \ - (head)->sqx_last = \ - XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ -} while (0) - - +#define XSIMPLEQ_INIT(head) \ + do { \ + arc4random_buf(&(head)->sqx_cookie, sizeof((head)->sqx_cookie)); \ + (head)->sqx_first = XSIMPLEQ_XOR(head, NULL); \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ + } while (0) + +#define XSIMPLEQ_INSERT_HEAD(head, elm, field) \ + do { \ + if (((elm)->field.sqx_next = (head)->sqx_first) == \ + XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ + (head)->sqx_first = XSIMPLEQ_XOR(head, (elm)); \ + } while (0) + +#define XSIMPLEQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.sqx_next = XSIMPLEQ_XOR(head, NULL); \ + *(XSIMPLEQ_XOR(head, (head)->sqx_last)) = XSIMPLEQ_XOR(head, (elm)); \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ + } while (0) + +#define XSIMPLEQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + if (((elm)->field.sqx_next = (listelm)->field.sqx_next) == \ + XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ + (listelm)->field.sqx_next = XSIMPLEQ_XOR(head, (elm)); \ + } while (0) + +#define XSIMPLEQ_REMOVE_HEAD(head, field) \ + do { \ + if (((head)->sqx_first = \ + XSIMPLEQ_XOR(head, (head)->sqx_first)->field.sqx_next) == \ + XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(head)->sqx_first); \ + } while (0) + +#define XSIMPLEQ_REMOVE_AFTER(head, elm, field) \ + do { \ + if (((elm)->field.sqx_next = \ + XSIMPLEQ_XOR(head, (elm)->field.sqx_next)->field.sqx_next) == \ + XSIMPLEQ_XOR(head, NULL)) \ + (head)->sqx_last = XSIMPLEQ_XOR(head, &(elm)->field.sqx_next); \ + } while (0) + /* * Tail queue definitions. */ -#define TAILQ_HEAD(name, type) \ -struct name { \ - struct type *tqh_first; /* first element */ \ - struct type **tqh_last; /* addr of last next element */ \ -} - -#define TAILQ_HEAD_INITIALIZER(head) \ - { NULL, &(head).tqh_first } - -#define TAILQ_ENTRY(type) \ -struct { \ - struct type *tqe_next; /* next element */ \ - struct type **tqe_prev; /* address of previous next element */ \ -} - -/* - * tail queue access methods +#define TAILQ_HEAD(name, type) \ + struct name { \ + struct type *tqh_first; /* first element */ \ + struct type **tqh_last; /* addr of last next element */ \ + } + +#define TAILQ_HEAD_INITIALIZER(head) \ + { NULL, &(head).tqh_first } + +#define TAILQ_ENTRY(type) \ + struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ + } + +/* + * tail queue access methods */ -#define TAILQ_FIRST(head) ((head)->tqh_first) -#define TAILQ_END(head) NULL -#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) -#define TAILQ_LAST(head, headname) \ - (*(((struct headname *)((head)->tqh_last))->tqh_last)) +#define TAILQ_FIRST(head) ((head)->tqh_first) +#define TAILQ_END(head) NULL +#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next) +#define TAILQ_LAST(head, headname) \ + (*(((struct headname *)((head)->tqh_last))->tqh_last)) /* XXX */ -#define TAILQ_PREV(elm, headname, field) \ - (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) -#define TAILQ_EMPTY(head) \ - (TAILQ_FIRST(head) == TAILQ_END(head)) - -#define TAILQ_FOREACH(var, head, field) \ - for((var) = TAILQ_FIRST(head); \ - (var) != TAILQ_END(head); \ - (var) = TAILQ_NEXT(var, field)) - -#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = TAILQ_FIRST(head); \ - (var) != TAILQ_END(head) && \ - ((tvar) = TAILQ_NEXT(var, field), 1); \ - (var) = (tvar)) - - -#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ - for((var) = TAILQ_LAST(head, headname); \ - (var) != TAILQ_END(head); \ - (var) = TAILQ_PREV(var, headname, field)) - -#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ - for ((var) = TAILQ_LAST(head, headname); \ - (var) != TAILQ_END(head) && \ - ((tvar) = TAILQ_PREV(var, headname, field), 1); \ - (var) = (tvar)) +#define TAILQ_PREV(elm, headname, field) \ + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last)) +#define TAILQ_EMPTY(head) (TAILQ_FIRST(head) == TAILQ_END(head)) + +#define TAILQ_FOREACH(var, head, field) \ + for ((var) = TAILQ_FIRST(head); (var) != TAILQ_END(head); \ + (var) = TAILQ_NEXT(var, field)) + +#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = TAILQ_FIRST(head); \ + (var) != TAILQ_END(head) && ((tvar) = TAILQ_NEXT(var, field), 1); \ + (var) = (tvar)) + +#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \ + for ((var) = TAILQ_LAST(head, headname); (var) != TAILQ_END(head); \ + (var) = TAILQ_PREV(var, headname, field)) + +#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = TAILQ_LAST(head, headname); \ + (var) != TAILQ_END(head) && \ + ((tvar) = TAILQ_PREV(var, headname, field), 1); \ + (var) = (tvar)) /* * Tail queue functions. */ -#define TAILQ_INIT(head) do { \ - (head)->tqh_first = NULL; \ - (head)->tqh_last = &(head)->tqh_first; \ -} while (0) - -#define TAILQ_INSERT_HEAD(head, elm, field) do { \ - if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ - (head)->tqh_first->field.tqe_prev = \ - &(elm)->field.tqe_next; \ - else \ - (head)->tqh_last = &(elm)->field.tqe_next; \ - (head)->tqh_first = (elm); \ - (elm)->field.tqe_prev = &(head)->tqh_first; \ -} while (0) - -#define TAILQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.tqe_next = NULL; \ - (elm)->field.tqe_prev = (head)->tqh_last; \ - *(head)->tqh_last = (elm); \ - (head)->tqh_last = &(elm)->field.tqe_next; \ -} while (0) - -#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \ - if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\ - (elm)->field.tqe_next->field.tqe_prev = \ - &(elm)->field.tqe_next; \ - else \ - (head)->tqh_last = &(elm)->field.tqe_next; \ - (listelm)->field.tqe_next = (elm); \ - (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ -} while (0) - -#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \ - (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ - (elm)->field.tqe_next = (listelm); \ - *(listelm)->field.tqe_prev = (elm); \ - (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ -} while (0) - -#define TAILQ_REMOVE(head, elm, field) do { \ - if (((elm)->field.tqe_next) != NULL) \ - (elm)->field.tqe_next->field.tqe_prev = \ - (elm)->field.tqe_prev; \ - else \ - (head)->tqh_last = (elm)->field.tqe_prev; \ - *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ - _Q_INVALIDATE((elm)->field.tqe_prev); \ - _Q_INVALIDATE((elm)->field.tqe_next); \ -} while (0) - -#define TAILQ_REPLACE(head, elm, elm2, field) do { \ - if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ - (elm2)->field.tqe_next->field.tqe_prev = \ - &(elm2)->field.tqe_next; \ - else \ - (head)->tqh_last = &(elm2)->field.tqe_next; \ - (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ - *(elm2)->field.tqe_prev = (elm2); \ - _Q_INVALIDATE((elm)->field.tqe_prev); \ - _Q_INVALIDATE((elm)->field.tqe_next); \ -} while (0) +#define TAILQ_INIT(head) \ + do { \ + (head)->tqh_first = NULL; \ + (head)->tqh_last = &(head)->tqh_first; \ + } while (0) + +#define TAILQ_INSERT_HEAD(head, elm, field) \ + do { \ + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \ + (head)->tqh_first->field.tqe_prev = &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (head)->tqh_first = (elm); \ + (elm)->field.tqe_prev = &(head)->tqh_first; \ + } while (0) + +#define TAILQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.tqe_next = NULL; \ + (elm)->field.tqe_prev = (head)->tqh_last; \ + *(head)->tqh_last = (elm); \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + } while (0) + +#define TAILQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL) \ + (elm)->field.tqe_next->field.tqe_prev = &(elm)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm)->field.tqe_next; \ + (listelm)->field.tqe_next = (elm); \ + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \ + } while (0) + +#define TAILQ_INSERT_BEFORE(listelm, elm, field) \ + do { \ + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \ + (elm)->field.tqe_next = (listelm); \ + *(listelm)->field.tqe_prev = (elm); \ + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \ + } while (0) + +#define TAILQ_REMOVE(head, elm, field) \ + do { \ + if (((elm)->field.tqe_next) != NULL) \ + (elm)->field.tqe_next->field.tqe_prev = (elm)->field.tqe_prev; \ + else \ + (head)->tqh_last = (elm)->field.tqe_prev; \ + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \ + _Q_INVALIDATE((elm)->field.tqe_prev); \ + _Q_INVALIDATE((elm)->field.tqe_next); \ + } while (0) + +#define TAILQ_REPLACE(head, elm, elm2, field) \ + do { \ + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \ + (elm2)->field.tqe_next->field.tqe_prev = &(elm2)->field.tqe_next; \ + else \ + (head)->tqh_last = &(elm2)->field.tqe_next; \ + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \ + *(elm2)->field.tqe_prev = (elm2); \ + _Q_INVALIDATE((elm)->field.tqe_prev); \ + _Q_INVALIDATE((elm)->field.tqe_next); \ + } while (0) /* * Circular queue definitions. */ -#define CIRCLEQ_HEAD(name, type) \ -struct name { \ - struct type *cqh_first; /* first element */ \ - struct type *cqh_last; /* last element */ \ -} +#define CIRCLEQ_HEAD(name, type) \ + struct name { \ + struct type *cqh_first; /* first element */ \ + struct type *cqh_last; /* last element */ \ + } -#define CIRCLEQ_HEAD_INITIALIZER(head) \ - { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } +#define CIRCLEQ_HEAD_INITIALIZER(head) \ + { CIRCLEQ_END(&head), CIRCLEQ_END(&head) } -#define CIRCLEQ_ENTRY(type) \ -struct { \ - struct type *cqe_next; /* next element */ \ - struct type *cqe_prev; /* previous element */ \ -} +#define CIRCLEQ_ENTRY(type) \ + struct { \ + struct type *cqe_next; /* next element */ \ + struct type *cqe_prev; /* previous element */ \ + } /* - * Circular queue access methods + * Circular queue access methods */ -#define CIRCLEQ_FIRST(head) ((head)->cqh_first) -#define CIRCLEQ_LAST(head) ((head)->cqh_last) -#define CIRCLEQ_END(head) ((void *)(head)) -#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) -#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) -#define CIRCLEQ_EMPTY(head) \ - (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head)) - -#define CIRCLEQ_FOREACH(var, head, field) \ - for((var) = CIRCLEQ_FIRST(head); \ - (var) != CIRCLEQ_END(head); \ - (var) = CIRCLEQ_NEXT(var, field)) - -#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = CIRCLEQ_FIRST(head); \ - (var) != CIRCLEQ_END(head) && \ - ((tvar) = CIRCLEQ_NEXT(var, field), 1); \ - (var) = (tvar)) - -#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ - for((var) = CIRCLEQ_LAST(head); \ - (var) != CIRCLEQ_END(head); \ - (var) = CIRCLEQ_PREV(var, field)) - -#define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ - for ((var) = CIRCLEQ_LAST(head, headname); \ - (var) != CIRCLEQ_END(head) && \ - ((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \ - (var) = (tvar)) +#define CIRCLEQ_FIRST(head) ((head)->cqh_first) +#define CIRCLEQ_LAST(head) ((head)->cqh_last) +#define CIRCLEQ_END(head) ((void *)(head)) +#define CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next) +#define CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev) +#define CIRCLEQ_EMPTY(head) (CIRCLEQ_FIRST(head) == CIRCLEQ_END(head)) + +#define CIRCLEQ_FOREACH(var, head, field) \ + for ((var) = CIRCLEQ_FIRST(head); (var) != CIRCLEQ_END(head); \ + (var) = CIRCLEQ_NEXT(var, field)) + +#define CIRCLEQ_FOREACH_SAFE(var, head, field, tvar) \ + for ((var) = CIRCLEQ_FIRST(head); \ + (var) != CIRCLEQ_END(head) && ((tvar) = CIRCLEQ_NEXT(var, field), 1); \ + (var) = (tvar)) + +#define CIRCLEQ_FOREACH_REVERSE(var, head, field) \ + for ((var) = CIRCLEQ_LAST(head); (var) != CIRCLEQ_END(head); \ + (var) = CIRCLEQ_PREV(var, field)) + +#define CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \ + for ((var) = CIRCLEQ_LAST(head, headname); \ + (var) != CIRCLEQ_END(head) && \ + ((tvar) = CIRCLEQ_PREV(var, headname, field), 1); \ + (var) = (tvar)) /* * Circular queue functions. */ -#define CIRCLEQ_INIT(head) do { \ - (head)->cqh_first = CIRCLEQ_END(head); \ - (head)->cqh_last = CIRCLEQ_END(head); \ -} while (0) - -#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \ - (elm)->field.cqe_next = (listelm)->field.cqe_next; \ - (elm)->field.cqe_prev = (listelm); \ - if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \ - (head)->cqh_last = (elm); \ - else \ - (listelm)->field.cqe_next->field.cqe_prev = (elm); \ - (listelm)->field.cqe_next = (elm); \ -} while (0) - -#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \ - (elm)->field.cqe_next = (listelm); \ - (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ - if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \ - (head)->cqh_first = (elm); \ - else \ - (listelm)->field.cqe_prev->field.cqe_next = (elm); \ - (listelm)->field.cqe_prev = (elm); \ -} while (0) - -#define CIRCLEQ_INSERT_HEAD(head, elm, field) do { \ - (elm)->field.cqe_next = (head)->cqh_first; \ - (elm)->field.cqe_prev = CIRCLEQ_END(head); \ - if ((head)->cqh_last == CIRCLEQ_END(head)) \ - (head)->cqh_last = (elm); \ - else \ - (head)->cqh_first->field.cqe_prev = (elm); \ - (head)->cqh_first = (elm); \ -} while (0) - -#define CIRCLEQ_INSERT_TAIL(head, elm, field) do { \ - (elm)->field.cqe_next = CIRCLEQ_END(head); \ - (elm)->field.cqe_prev = (head)->cqh_last; \ - if ((head)->cqh_first == CIRCLEQ_END(head)) \ - (head)->cqh_first = (elm); \ - else \ - (head)->cqh_last->field.cqe_next = (elm); \ - (head)->cqh_last = (elm); \ -} while (0) - -#define CIRCLEQ_REMOVE(head, elm, field) do { \ - if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \ - (head)->cqh_last = (elm)->field.cqe_prev; \ - else \ - (elm)->field.cqe_next->field.cqe_prev = \ - (elm)->field.cqe_prev; \ - if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \ - (head)->cqh_first = (elm)->field.cqe_next; \ - else \ - (elm)->field.cqe_prev->field.cqe_next = \ - (elm)->field.cqe_next; \ - _Q_INVALIDATE((elm)->field.cqe_prev); \ - _Q_INVALIDATE((elm)->field.cqe_next); \ -} while (0) - -#define CIRCLEQ_REPLACE(head, elm, elm2, field) do { \ - if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \ - CIRCLEQ_END(head)) \ - (head)->cqh_last = (elm2); \ - else \ - (elm2)->field.cqe_next->field.cqe_prev = (elm2); \ - if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \ - CIRCLEQ_END(head)) \ - (head)->cqh_first = (elm2); \ - else \ - (elm2)->field.cqe_prev->field.cqe_next = (elm2); \ - _Q_INVALIDATE((elm)->field.cqe_prev); \ - _Q_INVALIDATE((elm)->field.cqe_next); \ -} while (0) - -#endif /* !_SYS_QUEUE_H_ */ +#define CIRCLEQ_INIT(head) \ + do { \ + (head)->cqh_first = CIRCLEQ_END(head); \ + (head)->cqh_last = CIRCLEQ_END(head); \ + } while (0) + +#define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) \ + do { \ + (elm)->field.cqe_next = (listelm)->field.cqe_next; \ + (elm)->field.cqe_prev = (listelm); \ + if ((listelm)->field.cqe_next == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm); \ + else \ + (listelm)->field.cqe_next->field.cqe_prev = (elm); \ + (listelm)->field.cqe_next = (elm); \ + } while (0) + +#define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) \ + do { \ + (elm)->field.cqe_next = (listelm); \ + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \ + if ((listelm)->field.cqe_prev == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm); \ + else \ + (listelm)->field.cqe_prev->field.cqe_next = (elm); \ + (listelm)->field.cqe_prev = (elm); \ + } while (0) + +#define CIRCLEQ_INSERT_HEAD(head, elm, field) \ + do { \ + (elm)->field.cqe_next = (head)->cqh_first; \ + (elm)->field.cqe_prev = CIRCLEQ_END(head); \ + if ((head)->cqh_last == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm); \ + else \ + (head)->cqh_first->field.cqe_prev = (elm); \ + (head)->cqh_first = (elm); \ + } while (0) + +#define CIRCLEQ_INSERT_TAIL(head, elm, field) \ + do { \ + (elm)->field.cqe_next = CIRCLEQ_END(head); \ + (elm)->field.cqe_prev = (head)->cqh_last; \ + if ((head)->cqh_first == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm); \ + else \ + (head)->cqh_last->field.cqe_next = (elm); \ + (head)->cqh_last = (elm); \ + } while (0) + +#define CIRCLEQ_REMOVE(head, elm, field) \ + do { \ + if ((elm)->field.cqe_next == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm)->field.cqe_prev; \ + else \ + (elm)->field.cqe_next->field.cqe_prev = (elm)->field.cqe_prev; \ + if ((elm)->field.cqe_prev == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm)->field.cqe_next; \ + else \ + (elm)->field.cqe_prev->field.cqe_next = (elm)->field.cqe_next; \ + _Q_INVALIDATE((elm)->field.cqe_prev); \ + _Q_INVALIDATE((elm)->field.cqe_next); \ + } while (0) + +#define CIRCLEQ_REPLACE(head, elm, elm2, field) \ + do { \ + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == CIRCLEQ_END(head)) \ + (head)->cqh_last = (elm2); \ + else \ + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \ + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == CIRCLEQ_END(head)) \ + (head)->cqh_first = (elm2); \ + else \ + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \ + _Q_INVALIDATE((elm)->field.cqe_prev); \ + _Q_INVALIDATE((elm)->field.cqe_next); \ + } while (0) + +#endif /* !_SYS_QUEUE_H_ */ diff --git a/sad.c b/sad.c @@ -16,175 +16,164 @@ #include "arg.h" #include "sad.h" -fd_set master; -fd_set rfds; -int fdmax; -char *argv0; +fd_set master; +fd_set rfds; +int fdmax; +char *argv0; static int listenfd = -1; static volatile sig_atomic_t isrunning = 1; static char *socketpath = "/tmp/sad-sock"; -static int -servlisten(const char *name) -{ - struct sockaddr_un sun; - int fd, r; - socklen_t len; +static int servlisten(const char *name) { + struct sockaddr_un sun; + int fd, r; + socklen_t len; - fd = socket(AF_UNIX, SOCK_STREAM, 0); - if (fd < 0) - err(1, "socket"); + fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd < 0) + err(1, "socket"); - unlink(name); + unlink(name); - memset(&sun, 0, sizeof(sun)); - sun.sun_family = AF_UNIX; - strlcpy(sun.sun_path, name, sizeof(sun.sun_path)); + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, name, sizeof(sun.sun_path)); - len = sizeof(sun); - r = bind(fd, (struct sockaddr *)&sun, len); - if (r < 0) - err(1, "bind"); + len = sizeof(sun); + r = bind(fd, (struct sockaddr *)&sun, len); + if (r < 0) + err(1, "bind"); - r = listen(fd, 5); - if (r < 0) - err(1, "listen"); + r = listen(fd, 5); + if (r < 0) + err(1, "listen"); - return fd; + return fd; } -static int -servaccept(int listenfd) -{ - struct sockaddr_un sun; - int clifd; - socklen_t len; - - len = sizeof(sun); - clifd = accept(listenfd, (struct sockaddr *)&sun, &len); - if (clifd < 0) - err(1, "accept"); - return clifd; +static int servaccept(int listenfd) { + struct sockaddr_un sun; + int clifd; + socklen_t len; + + len = sizeof(sun); + clifd = accept(listenfd, (struct sockaddr *)&sun, &len); + if (clifd < 0) + err(1, "accept"); + return clifd; } -static void -playaudio(void) -{ - Song *s; - unsigned char buf[4096]; - int nbytes; - - s = getcursong(); - if (!s) - return; - - switch (s->state) { - case PREPARE: - if (s->decoder->open(&s->fmt, s->path) < 0) { - s->state = NONE; - return; - } - s->state = PLAYING; - break; - case PLAYING: - if ((nbytes = s->decoder->decode(buf, sizeof(buf))) <= 0) { - playsong(picknextsong()); - notify(EVSONGFINISHED); - } else { - playoutputs(&s->fmt, buf, nbytes); - } - break; - } +static void playaudio(void) { + Song *s; + unsigned char buf[4096]; + int nbytes; + + s = getcursong(); + if (!s) + return; + + switch (s->state) { + case PREPARE: + if (s->decoder->open(&s->fmt, s->path) < 0) { + s->state = NONE; + return; + } + s->state = PLAYING; + break; + case PLAYING: + if ((nbytes = s->decoder->decode(buf, sizeof(buf))) <= 0) { + playsong(picknextsong()); + notify(EVSONGFINISHED); + } else { + playoutputs(&s->fmt, buf, nbytes); + } + break; + } } -static void -sighandler(int signum) -{ - if (signum == SIGINT || signum == SIGTERM) - isrunning = 0; +static void sighandler(int signum) { + if (signum == SIGINT || signum == SIGTERM) + isrunning = 0; } -static void -usage(void) -{ - fprintf(stderr, "usage: %s [-f sock]\n", argv0); - exit(1); +static void usage(void) { + fprintf(stderr, "usage: %s [-f sock]\n", argv0); + exit(1); } -int -main(int argc, char *argv[]) -{ - struct sigaction sa; - struct timeval tv; - int clifd, n, i; - - ARGBEGIN { - case 'f': - socketpath = EARGF(usage()); - break; - default: - usage(); - } ARGEND; - - memset(&sa, 0, sizeof(sa)); - sa.sa_flags = SA_RESTART; - sa.sa_handler = sighandler; - sigaction(SIGTERM, &sa, NULL); - sigaction(SIGINT, &sa, NULL); - - FD_ZERO(&master); - FD_ZERO(&rfds); - - listenfd = servlisten(socketpath); - FD_SET(listenfd, &master); - fdmax = listenfd; - - initoutputs(); - openoutputs(); - initnotifier(); - - playlistmode(REPEAT); - - while (isrunning) { - rfds = master; - tv.tv_sec = 0; - tv.tv_usec = 10000; - n = select(fdmax + 1, &rfds, NULL, NULL, &tv); - if (n < 0) { - if (errno == EINTR) - goto fini; - err(1, "select"); - } - - playaudio(); - - for (i = 0; i <= fdmax; i++) { - if (!FD_ISSET(i, &rfds)) - continue; - if (i == listenfd) { - clifd = servaccept(listenfd); - FD_SET(clifd, &master); - if (clifd > fdmax) - fdmax = clifd; - } else { - if (docmd(i) < 0) { - close(i); - FD_CLR(i, &master); - removesubscriber(i); - } - } - } - } +int main(int argc, char *argv[]) { + struct sigaction sa; + struct timeval tv; + int clifd, n, i; + + ARGBEGIN { + case 'f': + socketpath = EARGF(usage()); + break; + default: + usage(); + } + ARGEND; + + memset(&sa, 0, sizeof(sa)); + sa.sa_flags = SA_RESTART; + sa.sa_handler = sighandler; + sigaction(SIGTERM, &sa, NULL); + sigaction(SIGINT, &sa, NULL); + + FD_ZERO(&master); + FD_ZERO(&rfds); + + listenfd = servlisten(socketpath); + FD_SET(listenfd, &master); + fdmax = listenfd; + + initoutputs(); + openoutputs(); + initnotifier(); + + playlistmode(REPEAT); + + while (isrunning) { + rfds = master; + tv.tv_sec = 0; + tv.tv_usec = 10000; + n = select(fdmax + 1, &rfds, NULL, NULL, &tv); + if (n < 0) { + if (errno == EINTR) + goto fini; + err(1, "select"); + } + + playaudio(); + + for (i = 0; i <= fdmax; i++) { + if (!FD_ISSET(i, &rfds)) + continue; + if (i == listenfd) { + clifd = servaccept(listenfd); + FD_SET(clifd, &master); + if (clifd > fdmax) + fdmax = clifd; + } else { + if (docmd(i) < 0) { + close(i); + FD_CLR(i, &master); + removesubscriber(i); + } + } + } + } fini: - for (i = 0; i <= fdmax; i++) { - if (FD_ISSET(i, &master)) { - close(i); - FD_CLR(i, &master); - removesubscriber(i); - } - } - if (listenfd != -1) - unlink(socketpath); - return 0; + for (i = 0; i <= fdmax; i++) { + if (FD_ISSET(i, &master)) { + close(i); + FD_CLR(i, &master); + removesubscriber(i); + } + } + if (listenfd != -1) + unlink(socketpath); + return 0; } diff --git a/sad.h b/sad.h @@ -2,77 +2,68 @@ #include "compat.h" #endif -#define LEN(x) (sizeof (x) / sizeof *(x)) +#define LEN(x) (sizeof(x) / sizeof *(x)) #define PROTOCOLVERSION "0.0" #define DEFAULTVOL 100 typedef struct { - char *name; - void (*fn)(int, char *); + char *name; + void (*fn)(int, char *); } Cmd; -enum { - NONE, - PREPARE, - PLAYING, - PAUSED -}; +enum { NONE, PREPARE, PLAYING, PAUSED }; -enum { - REPEAT = 1 << 0, - RANDOM = 1 << 1, - SINGLE = 1 << 2 -}; +enum { REPEAT = 1 << 0, RANDOM = 1 << 1, SINGLE = 1 << 2 }; enum { - EVSONGFINISHED, + EVSONGFINISHED, }; typedef struct { - unsigned int bits; - unsigned int rate; - unsigned int channels; + unsigned int bits; + unsigned int rate; + unsigned int channels; } Format; typedef struct { - int (*open)(Format *, const char *); - int (*decode)(void *, int); - int (*close)(void); + int (*open)(Format *, const char *); + int (*decode)(void *, int); + int (*close)(void); } Decoder; typedef struct { - int *volstatus; - int (*vol)(int); - int (*open)(Format *); - int (*play)(void *, size_t); - int (*close)(void); + int *volstatus; + int (*vol)(int); + int (*open)(Format *); + int (*play)(void *, size_t); + int (*close)(void); } Output; typedef struct { - char path[PATH_MAX]; - int id; - int state; - Decoder *decoder; - Format fmt; + char path[PATH_MAX]; + int id; + int state; + Decoder *decoder; + Format fmt; } Song; typedef struct { - Song **songs; - Song *cursong; - size_t nsongs; - size_t maxsongs; - int mode; + Song **songs; + Song *cursong; + size_t nsongs; + size_t maxsongs; + int mode; } Playlist; typedef struct { - int event; - char *name; + int event; + char *name; } Eventdesc; /* sad.c */ -extern fd_set master; -extern fd_set rfds; -extern int fdmax; +extern fd_set master; +extern fd_set rfds; +extern int fdmax; /* cmd.c */ int docmd(int); diff --git a/sndio.c b/sndio.c @@ -2,88 +2,78 @@ #include <err.h> #include <limits.h> -#include <stdio.h> #include <sndio.h> +#include <stdio.h> #include "sad.h" static struct sio_hdl *hdl; int sndiovolstatus = DEFAULTVOL; -static int -sndiovol(int vol) -{ - if (!sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) { - warnx("sio_setvol: failed"); - return -1; - } - sndiovolstatus = vol; - return 0; +static int sndiovol(int vol) { + if (!sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) { + warnx("sio_setvol: failed"); + return -1; + } + sndiovolstatus = vol; + return 0; } -static int -sndioopen(Format *fmt) -{ - struct sio_par par; +static int sndioopen(Format *fmt) { + struct sio_par par; - hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); - if (!hdl) { - warnx("sio_open: failed"); - return -1; - } + hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); + if (!hdl) { + warnx("sio_open: failed"); + return -1; + } - sio_initpar(&par); - par.bits = fmt->bits; - par.rate = fmt->rate; - par.pchan = fmt->channels; - par.sig = 1; - par.le = SIO_LE_NATIVE; + sio_initpar(&par); + par.bits = fmt->bits; + par.rate = fmt->rate; + par.pchan = fmt->channels; + par.sig = 1; + par.le = SIO_LE_NATIVE; - if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) { - warnx("sio_{set,get}par: failed"); - goto err0; - } + if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) { + warnx("sio_{set,get}par: failed"); + goto err0; + } - if (par.bits != fmt->bits || par.rate != fmt->rate || - par.pchan != fmt->channels || par.le != SIO_LE_NATIVE || - par.sig != 1) { - warnx("unsupported audio params"); - goto err0; - } + if (par.bits != fmt->bits || par.rate != fmt->rate || + par.pchan != fmt->channels || par.le != SIO_LE_NATIVE || par.sig != 1) { + warnx("unsupported audio params"); + goto err0; + } - if (!sio_start(hdl)) { - warnx("sio_start: failed"); - goto err0; - } + if (!sio_start(hdl)) { + warnx("sio_start: failed"); + goto err0; + } - return 0; + return 0; err0: - sio_close(hdl); - hdl = NULL; - return -1; + sio_close(hdl); + hdl = NULL; + return -1; } -static int -sndioplay(void *buf, size_t nbytes) -{ - return sio_write(hdl, buf, nbytes); +static int sndioplay(void *buf, size_t nbytes) { + return sio_write(hdl, buf, nbytes); } -static int -sndioclose(void) -{ - if (hdl) - sio_close(hdl); - hdl = NULL; - return 0; +static int sndioclose(void) { + if (hdl) + sio_close(hdl); + hdl = NULL; + return 0; } Output sndiooutput = { .volstatus = &sndiovolstatus, - .vol = sndiovol, - .open = sndioopen, - .play = sndioplay, - .close = sndioclose, + .vol = sndiovol, + .open = sndioopen, + .play = sndioplay, + .close = sndioclose, }; - diff --git a/vorbis.c b/vorbis.c @@ -10,67 +10,56 @@ static OggVorbis_File vf; static int cursect; -static int -vorbisopen(Format *fmt, const char *name) -{ - int r; - vorbis_info *vi; +static int vorbisopen(Format *fmt, const char *name) { + int r; + vorbis_info *vi; - r = ov_fopen(name, &vf); - if (r < 0) { - warnx("ov_fopen: failed"); - return -1; - } + r = ov_fopen(name, &vf); + if (r < 0) { + warnx("ov_fopen: failed"); + return -1; + } - vi = ov_info(&vf, -1); - if (!vi) { - warnx("ov_info: failed"); - goto err0; - } + vi = ov_info(&vf, -1); + if (!vi) { + warnx("ov_info: failed"); + goto err0; + } - fmt->bits = 16; - fmt->rate = vi->rate; - fmt->channels = vi->channels; + fmt->bits = 16; + fmt->rate = vi->rate; + fmt->channels = vi->channels; - if (initresamplers(fmt) < 0) - goto err0; + if (initresamplers(fmt) < 0) + goto err0; - return 0; + return 0; err0: - ov_clear(&vf); - return -1; + ov_clear(&vf); + return -1; } -static int -vorbisdecode(void *buf, int nbytes) -{ - int r, decoded; - char *p = buf; +static int vorbisdecode(void *buf, int nbytes) { + int r, decoded; + char *p = buf; - decoded = 0; - while (nbytes > 0) { - r = ov_read(&vf, &p[decoded], nbytes, 0, 2, 1, &cursect); - if (r < 0) { - warnx("ov_read: failed"); - return -1; - } else if (r == 0) { - break; - } - decoded += r; - nbytes -= r; - } - return decoded; + decoded = 0; + while (nbytes > 0) { + r = ov_read(&vf, &p[decoded], nbytes, 0, 2, 1, &cursect); + if (r < 0) { + warnx("ov_read: failed"); + return -1; + } else if (r == 0) { + break; + } + decoded += r; + nbytes -= r; + } + return decoded; } -static int -vorbisclose(void) -{ - return ov_clear(&vf); -} +static int vorbisclose(void) { return ov_clear(&vf); } Decoder vorbisdecoder = { - .open = vorbisopen, - .decode = vorbisdecode, - .close = vorbisclose -}; + .open = vorbisopen, .decode = vorbisdecode, .close = vorbisclose}; diff --git a/wav.c b/wav.c @@ -2,87 +2,76 @@ #include <err.h> #include <limits.h> -#include <stdio.h> #include <sndfile.h> +#include <stdio.h> #include "sad.h" static SNDFILE *sf; -static int -wavopen(Format *fmt, const char *name) -{ - SF_INFO sfinfo; - int bits; +static int wavopen(Format *fmt, const char *name) { + SF_INFO sfinfo; + int bits; - sf = sf_open(name, SFM_READ, &sfinfo); - if (!sf) { - warnx("sf_open_fd: failed"); - return -1; - } + sf = sf_open(name, SFM_READ, &sfinfo); + if (!sf) { + warnx("sf_open_fd: failed"); + return -1; + } - if (sfinfo.channels < 1 || sfinfo.channels > 2) { - warnx("sfinfo: unsupported number of channels %d", - sfinfo.channels); - goto err0; - } + if (sfinfo.channels < 1 || sfinfo.channels > 2) { + warnx("sfinfo: unsupported number of channels %d", sfinfo.channels); + goto err0; + } - switch (sfinfo.format & 0xff) { - case SF_FORMAT_PCM_S8: - bits = 8; - break; - case SF_FORMAT_PCM_16: - bits = 16; - break; - case SF_FORMAT_PCM_24: - bits = 24; - break; - case SF_FORMAT_PCM_32: - bits = 32; - break; - default: - warnx("sfinfo: unsupported format"); - goto err0; - } + switch (sfinfo.format & 0xff) { + case SF_FORMAT_PCM_S8: + bits = 8; + break; + case SF_FORMAT_PCM_16: + bits = 16; + break; + case SF_FORMAT_PCM_24: + bits = 24; + break; + case SF_FORMAT_PCM_32: + bits = 32; + break; + default: + warnx("sfinfo: unsupported format"); + goto err0; + } - fmt->bits = bits; - fmt->rate = sfinfo.samplerate; - fmt->channels = sfinfo.channels; + fmt->bits = bits; + fmt->rate = sfinfo.samplerate; + fmt->channels = sfinfo.channels; - if (initresamplers(fmt) < 0) - goto err0; + if (initresamplers(fmt) < 0) + goto err0; - return 0; + return 0; err0: - sf_close(sf); - sf = NULL; - return -1; + sf_close(sf); + sf = NULL; + return -1; } -static int -wavdecode(void *buf, int nbytes) -{ - return sf_read_short(sf, buf, nbytes / sizeof(short)) * sizeof(short); +static int wavdecode(void *buf, int nbytes) { + return sf_read_short(sf, buf, nbytes / sizeof(short)) * sizeof(short); } -static int -wavclose(void) -{ - int r = 0; +static int wavclose(void) { + int r = 0; - if (sf) { - r = sf_close(sf); - if (r != 0) { - warnx("sf_close: failed"); - r = -1; - } - } - sf = NULL; - return r; + if (sf) { + r = sf_close(sf); + if (r != 0) { + warnx("sf_close: failed"); + r = -1; + } + } + sf = NULL; + return r; } -Decoder wavdecoder = { - .open = wavopen, - .decode = wavdecode, - .close = wavclose -}; +Decoder wavdecoder = {.open = wavopen, .decode = wavdecode, .close = wavclose};