Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <stdlib.h>
18 #include <syslog.h>
20 #include "log.h"
21 #include "xmalloc.h"
22 #include "playlist.h"
24 #define MAX(a, b) ((a) > (b) ? (a) : (b))
26 struct playlist playlist;
27 enum play_state play_state;
28 int repeat_one;
29 int repeat_all = 1;
30 ssize_t play_off = -1;
32 void
33 playlist_swap(struct playlist *p)
34 {
35 playlist_truncate();
37 playlist.len = p->len;
38 playlist.cap = p->cap;
39 playlist.songs = p->songs;
40 }
42 void
43 playlist_push(struct playlist *playlist, const char *path)
44 {
45 size_t newcap;
47 if (playlist->len == playlist->cap) {
48 newcap = MAX(16, playlist->cap * 1.5);
49 playlist->songs = xrecallocarray(playlist->songs,
50 playlist->cap, newcap, sizeof(*playlist->songs));
51 playlist->cap = newcap;
52 }
54 playlist->songs[playlist->len++] = xstrdup(path);
55 }
57 void
58 playlist_enqueue(const char *path)
59 {
60 playlist_push(&playlist, path);
61 }
63 const char *
64 playlist_current(void)
65 {
66 if (playlist.len == 0 || play_off == -1) {
67 play_state = STATE_STOPPED;
68 return NULL;
69 }
71 return playlist.songs[play_off];
72 }
74 const char *
75 playlist_advance(void)
76 {
77 if (playlist.len == 0) {
78 play_state = STATE_STOPPED;
79 return NULL;
80 }
82 play_off++;
83 if (play_off == playlist.len) {
84 if (repeat_all)
85 play_off = 0;
86 else {
87 play_state = STATE_STOPPED;
88 play_off = -1;
89 return NULL;
90 }
91 }
93 play_state = STATE_PLAYING;
94 return playlist.songs[play_off];
95 }
97 const char *
98 playlist_previous(void)
99 {
100 if (playlist.len == 0) {
101 play_state = STATE_STOPPED;
102 return NULL;
105 play_off--;
106 if (play_off < 0) {
107 if (repeat_all)
108 play_off = playlist.len - 1;
109 else {
110 play_state = STATE_STOPPED;
111 play_off = -1;
112 return NULL;
116 play_state = STATE_PLAYING;
117 return playlist.songs[play_off];
120 void
121 playlist_reset(void)
123 play_off = -1;
126 void
127 playlist_free(struct playlist *playlist)
129 size_t i;
131 for (i = 0; i < playlist->len; ++i)
132 free(playlist->songs[i]);
133 free(playlist->songs);
134 playlist->songs = NULL;
136 playlist->len = 0;
137 playlist->cap = 0;
140 void
141 playlist_truncate(void)
143 playlist_free(&playlist);
144 play_off = -1;
147 void
148 playlist_dropcurrent(void)
150 size_t i;
152 if (play_off == -1 || playlist.len == 0)
153 return;
155 free(playlist.songs[play_off]);
157 playlist.len--;
158 for (i = play_off; i < playlist.len; ++i)
159 playlist.songs[i] = playlist.songs[i+1];
161 playlist.songs[playlist.len] = NULL;