Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
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.
7 3baa2617 2022-02-16 op *
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.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 3baa2617 2022-02-16 op #include <stdlib.h>
18 3baa2617 2022-02-16 op #include <syslog.h>
19 3baa2617 2022-02-16 op
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"
23 3baa2617 2022-02-16 op
24 3baa2617 2022-02-16 op #define MAX(a, b) ((a) > (b) ? (a) : (b))
25 3baa2617 2022-02-16 op
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;
31 3baa2617 2022-02-16 op
32 3baa2617 2022-02-16 op void
33 0977e96a 2022-02-17 op playlist_swap(struct playlist *p)
34 0977e96a 2022-02-17 op {
35 0977e96a 2022-02-17 op playlist_truncate();
36 0977e96a 2022-02-17 op
37 0977e96a 2022-02-17 op playlist.len = p->len;
38 0977e96a 2022-02-17 op playlist.cap = p->cap;
39 0977e96a 2022-02-17 op playlist.songs = p->songs;
40 0977e96a 2022-02-17 op }
41 0977e96a 2022-02-17 op
42 0977e96a 2022-02-17 op void
43 532ca63c 2022-02-17 op playlist_push(struct playlist *playlist, const char *path)
44 3baa2617 2022-02-16 op {
45 3baa2617 2022-02-16 op size_t newcap;
46 3baa2617 2022-02-16 op
47 532ca63c 2022-02-17 op if (playlist->len == playlist->cap) {
48 532ca63c 2022-02-17 op newcap = MAX(16, playlist->cap * 1.5);
49 532ca63c 2022-02-17 op playlist->songs = xrecallocarray(playlist->songs,
50 532ca63c 2022-02-17 op playlist->cap, newcap, sizeof(*playlist->songs));
51 532ca63c 2022-02-17 op playlist->cap = newcap;
52 3baa2617 2022-02-16 op }
53 3baa2617 2022-02-16 op
54 532ca63c 2022-02-17 op playlist->songs[playlist->len++] = xstrdup(path);
55 3baa2617 2022-02-16 op }
56 3baa2617 2022-02-16 op
57 532ca63c 2022-02-17 op void
58 532ca63c 2022-02-17 op playlist_enqueue(const char *path)
59 532ca63c 2022-02-17 op {
60 532ca63c 2022-02-17 op playlist_push(&playlist, path);
61 532ca63c 2022-02-17 op }
62 532ca63c 2022-02-17 op
63 3baa2617 2022-02-16 op const char *
64 3baa2617 2022-02-16 op playlist_current(void)
65 3baa2617 2022-02-16 op {
66 8891f624 2022-02-16 op if (playlist.len == 0 || play_off == -1) {
67 8891f624 2022-02-16 op play_state = STATE_STOPPED;
68 3baa2617 2022-02-16 op return NULL;
69 8891f624 2022-02-16 op }
70 3baa2617 2022-02-16 op
71 3baa2617 2022-02-16 op return playlist.songs[play_off];
72 3baa2617 2022-02-16 op }
73 3baa2617 2022-02-16 op
74 3baa2617 2022-02-16 op const char *
75 3baa2617 2022-02-16 op playlist_advance(void)
76 3baa2617 2022-02-16 op {
77 8891f624 2022-02-16 op if (playlist.len == 0) {
78 8891f624 2022-02-16 op play_state = STATE_STOPPED;
79 3baa2617 2022-02-16 op return NULL;
80 8891f624 2022-02-16 op }
81 3baa2617 2022-02-16 op
82 3baa2617 2022-02-16 op play_off++;
83 3baa2617 2022-02-16 op if (play_off == playlist.len) {
84 3baa2617 2022-02-16 op if (repeat_all)
85 3baa2617 2022-02-16 op play_off = 0;
86 3baa2617 2022-02-16 op else {
87 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
88 3baa2617 2022-02-16 op play_off = -1;
89 3baa2617 2022-02-16 op return NULL;
90 3baa2617 2022-02-16 op }
91 3baa2617 2022-02-16 op }
92 3baa2617 2022-02-16 op
93 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
94 3baa2617 2022-02-16 op return playlist.songs[play_off];
95 3baa2617 2022-02-16 op }
96 3baa2617 2022-02-16 op
97 af27e631 2022-02-17 op const char *
98 af27e631 2022-02-17 op playlist_previous(void)
99 af27e631 2022-02-17 op {
100 af27e631 2022-02-17 op if (playlist.len == 0) {
101 af27e631 2022-02-17 op play_state = STATE_STOPPED;
102 af27e631 2022-02-17 op return NULL;
103 af27e631 2022-02-17 op }
104 af27e631 2022-02-17 op
105 af27e631 2022-02-17 op play_off--;
106 af27e631 2022-02-17 op if (play_off < 0) {
107 af27e631 2022-02-17 op if (repeat_all)
108 af27e631 2022-02-17 op play_off = playlist.len - 1;
109 af27e631 2022-02-17 op else {
110 af27e631 2022-02-17 op play_state = STATE_STOPPED;
111 af27e631 2022-02-17 op play_off = -1;
112 af27e631 2022-02-17 op return NULL;
113 af27e631 2022-02-17 op }
114 af27e631 2022-02-17 op }
115 af27e631 2022-02-17 op
116 af27e631 2022-02-17 op play_state = STATE_PLAYING;
117 af27e631 2022-02-17 op return playlist.songs[play_off];
118 af27e631 2022-02-17 op }
119 af27e631 2022-02-17 op
120 3baa2617 2022-02-16 op void
121 3baa2617 2022-02-16 op playlist_reset(void)
122 3baa2617 2022-02-16 op {
123 3baa2617 2022-02-16 op play_off = -1;
124 3baa2617 2022-02-16 op }
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op void
127 cd070aea 2022-02-17 op playlist_free(struct playlist *playlist)
128 3baa2617 2022-02-16 op {
129 3baa2617 2022-02-16 op size_t i;
130 3baa2617 2022-02-16 op
131 cd070aea 2022-02-17 op for (i = 0; i < playlist->len; ++i)
132 cd070aea 2022-02-17 op free(playlist->songs[i]);
133 cd070aea 2022-02-17 op free(playlist->songs);
134 cd070aea 2022-02-17 op playlist->songs = NULL;
135 3baa2617 2022-02-16 op
136 cd070aea 2022-02-17 op playlist->len = 0;
137 cd070aea 2022-02-17 op playlist->cap = 0;
138 cd070aea 2022-02-17 op }
139 cd070aea 2022-02-17 op
140 cd070aea 2022-02-17 op void
141 cd070aea 2022-02-17 op playlist_truncate(void)
142 cd070aea 2022-02-17 op {
143 cd070aea 2022-02-17 op playlist_free(&playlist);
144 3baa2617 2022-02-16 op play_off = -1;
145 3baa2617 2022-02-16 op }
146 13b83883 2022-02-16 op
147 13b83883 2022-02-16 op void
148 13b83883 2022-02-16 op playlist_dropcurrent(void)
149 13b83883 2022-02-16 op {
150 13b83883 2022-02-16 op size_t i;
151 13b83883 2022-02-16 op
152 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
153 13b83883 2022-02-16 op return;
154 13b83883 2022-02-16 op
155 13b83883 2022-02-16 op free(playlist.songs[play_off]);
156 13b83883 2022-02-16 op
157 13b83883 2022-02-16 op playlist.len--;
158 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
159 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
160 13b83883 2022-02-16 op
161 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
162 13b83883 2022-02-16 op }