2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3baa2617 2022-02-16 op #include <stdlib.h>
18 3baa2617 2022-02-16 op #include <syslog.h>
20 3baa2617 2022-02-16 op #include "log.h"
21 3baa2617 2022-02-16 op #include "xmalloc.h"
22 3baa2617 2022-02-16 op #include "playlist.h"
24 3baa2617 2022-02-16 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
26 3baa2617 2022-02-16 op struct playlist playlist;
27 3baa2617 2022-02-16 op enum play_state play_state;
28 3baa2617 2022-02-16 op int repeat_one;
29 3baa2617 2022-02-16 op int repeat_all = 1;
30 3baa2617 2022-02-16 op ssize_t play_off = -1;
33 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
35 3baa2617 2022-02-16 op size_t newcap;
37 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
38 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
39 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
40 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
41 532ca63c 2022-02-17 op playlist->cap = newcap;
44 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
48 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
50 532ca63c 2022-02-17 op playlist_push(&playlist, path);
54 3baa2617 2022-02-16 op playlist_current(void)
56 8891f624 2022-02-16 op if (playlist.len == 0 || play_off == -1) {
57 8891f624 2022-02-16 op play_state = STATE_STOPPED;
61 3baa2617 2022-02-16 op return playlist.songs[play_off];
65 3baa2617 2022-02-16 op playlist_advance(void)
67 8891f624 2022-02-16 op if (playlist.len == 0) {
68 8891f624 2022-02-16 op play_state = STATE_STOPPED;
73 3baa2617 2022-02-16 op if (play_off == playlist.len) {
74 3baa2617 2022-02-16 op if (repeat_all)
77 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
78 3baa2617 2022-02-16 op play_off = -1;
83 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
84 3baa2617 2022-02-16 op return playlist.songs[play_off];
88 af27e631 2022-02-17 op playlist_previous(void)
90 af27e631 2022-02-17 op if (playlist.len == 0) {
91 af27e631 2022-02-17 op play_state = STATE_STOPPED;
96 af27e631 2022-02-17 op if (play_off < 0) {
97 af27e631 2022-02-17 op if (repeat_all)
98 af27e631 2022-02-17 op play_off = playlist.len - 1;
100 af27e631 2022-02-17 op play_state = STATE_STOPPED;
101 af27e631 2022-02-17 op play_off = -1;
106 af27e631 2022-02-17 op play_state = STATE_PLAYING;
107 af27e631 2022-02-17 op return playlist.songs[play_off];
111 3baa2617 2022-02-16 op playlist_reset(void)
113 3baa2617 2022-02-16 op play_off = -1;
117 3baa2617 2022-02-16 op playlist_truncate(void)
121 3baa2617 2022-02-16 op for (i = 0; i < playlist.len; ++i)
122 3baa2617 2022-02-16 op free(playlist.songs[i]);
123 3baa2617 2022-02-16 op free(playlist.songs);
124 3baa2617 2022-02-16 op playlist.songs = NULL;
126 3baa2617 2022-02-16 op playlist.len = 0;
127 3baa2617 2022-02-16 op playlist.cap = 0;
128 3baa2617 2022-02-16 op play_off = -1;
132 13b83883 2022-02-16 op playlist_dropcurrent(void)
136 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
139 13b83883 2022-02-16 op free(playlist.songs[play_off]);
141 13b83883 2022-02-16 op playlist.len--;
142 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
143 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
145 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;