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 3baa2617 2022-02-16 op playlist_enqueue(const char *path)
34 3baa2617 2022-02-16 op {
35 3baa2617 2022-02-16 op size_t newcap;
36 3baa2617 2022-02-16 op
37 3baa2617 2022-02-16 op if (playlist.len == playlist.cap) {
38 3baa2617 2022-02-16 op newcap = MAX(16, playlist.cap * 1.5);
39 3baa2617 2022-02-16 op playlist.songs = xrecallocarray(playlist.songs, playlist.cap,
40 3baa2617 2022-02-16 op newcap, sizeof(*playlist.songs));
41 3baa2617 2022-02-16 op playlist.cap = newcap;
42 3baa2617 2022-02-16 op }
43 3baa2617 2022-02-16 op
44 3baa2617 2022-02-16 op playlist.songs[playlist.len++] = xstrdup(path);
45 3baa2617 2022-02-16 op }
46 3baa2617 2022-02-16 op
47 3baa2617 2022-02-16 op const char *
48 3baa2617 2022-02-16 op playlist_current(void)
49 3baa2617 2022-02-16 op {
50 8891f624 2022-02-16 op if (playlist.len == 0 || play_off == -1) {
51 8891f624 2022-02-16 op play_state = STATE_STOPPED;
52 3baa2617 2022-02-16 op return NULL;
53 8891f624 2022-02-16 op }
54 3baa2617 2022-02-16 op
55 3baa2617 2022-02-16 op return playlist.songs[play_off];
56 3baa2617 2022-02-16 op }
57 3baa2617 2022-02-16 op
58 3baa2617 2022-02-16 op const char *
59 3baa2617 2022-02-16 op playlist_advance(void)
60 3baa2617 2022-02-16 op {
61 8891f624 2022-02-16 op if (playlist.len == 0) {
62 8891f624 2022-02-16 op play_state = STATE_STOPPED;
63 3baa2617 2022-02-16 op return NULL;
64 8891f624 2022-02-16 op }
65 3baa2617 2022-02-16 op
66 3baa2617 2022-02-16 op play_off++;
67 3baa2617 2022-02-16 op if (play_off == playlist.len) {
68 3baa2617 2022-02-16 op if (repeat_all)
69 3baa2617 2022-02-16 op play_off = 0;
70 3baa2617 2022-02-16 op else {
71 3baa2617 2022-02-16 op play_state = STATE_STOPPED;
72 3baa2617 2022-02-16 op play_off = -1;
73 3baa2617 2022-02-16 op return NULL;
74 3baa2617 2022-02-16 op }
75 3baa2617 2022-02-16 op }
76 3baa2617 2022-02-16 op
77 3baa2617 2022-02-16 op play_state = STATE_PLAYING;
78 3baa2617 2022-02-16 op return playlist.songs[play_off];
79 3baa2617 2022-02-16 op }
80 3baa2617 2022-02-16 op
81 af27e631 2022-02-17 op const char *
82 af27e631 2022-02-17 op playlist_previous(void)
83 af27e631 2022-02-17 op {
84 af27e631 2022-02-17 op if (playlist.len == 0) {
85 af27e631 2022-02-17 op play_state = STATE_STOPPED;
86 af27e631 2022-02-17 op return NULL;
87 af27e631 2022-02-17 op }
88 af27e631 2022-02-17 op
89 af27e631 2022-02-17 op play_off--;
90 af27e631 2022-02-17 op if (play_off < 0) {
91 af27e631 2022-02-17 op if (repeat_all)
92 af27e631 2022-02-17 op play_off = playlist.len - 1;
93 af27e631 2022-02-17 op else {
94 af27e631 2022-02-17 op play_state = STATE_STOPPED;
95 af27e631 2022-02-17 op play_off = -1;
96 af27e631 2022-02-17 op return NULL;
97 af27e631 2022-02-17 op }
98 af27e631 2022-02-17 op }
99 af27e631 2022-02-17 op
100 af27e631 2022-02-17 op play_state = STATE_PLAYING;
101 af27e631 2022-02-17 op return playlist.songs[play_off];
102 af27e631 2022-02-17 op }
103 af27e631 2022-02-17 op
104 3baa2617 2022-02-16 op void
105 3baa2617 2022-02-16 op playlist_reset(void)
106 3baa2617 2022-02-16 op {
107 3baa2617 2022-02-16 op play_off = -1;
108 3baa2617 2022-02-16 op }
109 3baa2617 2022-02-16 op
110 3baa2617 2022-02-16 op void
111 3baa2617 2022-02-16 op playlist_truncate(void)
112 3baa2617 2022-02-16 op {
113 3baa2617 2022-02-16 op size_t i;
114 3baa2617 2022-02-16 op
115 3baa2617 2022-02-16 op for (i = 0; i < playlist.len; ++i)
116 3baa2617 2022-02-16 op free(playlist.songs[i]);
117 3baa2617 2022-02-16 op free(playlist.songs);
118 3baa2617 2022-02-16 op playlist.songs = NULL;
119 3baa2617 2022-02-16 op
120 3baa2617 2022-02-16 op playlist.len = 0;
121 3baa2617 2022-02-16 op playlist.cap = 0;
122 3baa2617 2022-02-16 op play_off = -1;
123 3baa2617 2022-02-16 op }
124 13b83883 2022-02-16 op
125 13b83883 2022-02-16 op void
126 13b83883 2022-02-16 op playlist_dropcurrent(void)
127 13b83883 2022-02-16 op {
128 13b83883 2022-02-16 op size_t i;
129 13b83883 2022-02-16 op
130 13b83883 2022-02-16 op if (play_off == -1 || playlist.len == 0)
131 13b83883 2022-02-16 op return;
132 13b83883 2022-02-16 op
133 13b83883 2022-02-16 op free(playlist.songs[play_off]);
134 13b83883 2022-02-16 op
135 13b83883 2022-02-16 op playlist.len--;
136 13b83883 2022-02-16 op for (i = play_off; i < playlist.len; ++i)
137 13b83883 2022-02-16 op playlist.songs[i] = playlist.songs[i+1];
138 13b83883 2022-02-16 op
139 13b83883 2022-02-16 op playlist.songs[playlist.len] = NULL;
140 13b83883 2022-02-16 op }