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_push(struct playlist *playlist, const char *path)
34 {
35 size_t newcap;
37 if (playlist->len == playlist->cap) {
38 newcap = MAX(16, playlist->cap * 1.5);
39 playlist->songs = xrecallocarray(playlist->songs,
40 playlist->cap, newcap, sizeof(*playlist->songs));
41 playlist->cap = newcap;
42 }
44 playlist->songs[playlist->len++] = xstrdup(path);
45 }
47 void
48 playlist_enqueue(const char *path)
49 {
50 playlist_push(&playlist, path);
51 }
53 const char *
54 playlist_current(void)
55 {
56 if (playlist.len == 0 || play_off == -1) {
57 play_state = STATE_STOPPED;
58 return NULL;
59 }
61 return playlist.songs[play_off];
62 }
64 const char *
65 playlist_advance(void)
66 {
67 if (playlist.len == 0) {
68 play_state = STATE_STOPPED;
69 return NULL;
70 }
72 play_off++;
73 if (play_off == playlist.len) {
74 if (repeat_all)
75 play_off = 0;
76 else {
77 play_state = STATE_STOPPED;
78 play_off = -1;
79 return NULL;
80 }
81 }
83 play_state = STATE_PLAYING;
84 return playlist.songs[play_off];
85 }
87 const char *
88 playlist_previous(void)
89 {
90 if (playlist.len == 0) {
91 play_state = STATE_STOPPED;
92 return NULL;
93 }
95 play_off--;
96 if (play_off < 0) {
97 if (repeat_all)
98 play_off = playlist.len - 1;
99 else {
100 play_state = STATE_STOPPED;
101 play_off = -1;
102 return NULL;
106 play_state = STATE_PLAYING;
107 return playlist.songs[play_off];
110 void
111 playlist_reset(void)
113 play_off = -1;
116 void
117 playlist_truncate(void)
119 size_t i;
121 for (i = 0; i < playlist.len; ++i)
122 free(playlist.songs[i]);
123 free(playlist.songs);
124 playlist.songs = NULL;
126 playlist.len = 0;
127 playlist.cap = 0;
128 play_off = -1;
131 void
132 playlist_dropcurrent(void)
134 size_t i;
136 if (play_off == -1 || playlist.len == 0)
137 return;
139 free(playlist.songs[play_off]);
141 playlist.len--;
142 for (i = play_off; i < playlist.len; ++i)
143 playlist.songs[i] = playlist.songs[i+1];
145 playlist.songs[playlist.len] = NULL;