002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
004
2022-02-16
op
* Permission to use, copy, modify, and distribute this software for any
005
2022-02-16
op
* purpose with or without fee is hereby granted, provided that the above
006
2022-02-16
op
* copyright notice and this permission notice appear in all copies.
008
2022-02-16
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
009
2022-02-16
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
010
2022-02-16
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
011
2022-02-16
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
012
2022-02-16
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
013
2022-02-16
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
014
2022-02-16
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
017
2022-02-17
op
#include <sys/types.h>
019
2022-02-17
op
#include <regex.h>
020
2022-02-16
op
#include <stdlib.h>
021
2022-02-19
op
#include <string.h>
022
2022-02-16
op
#include <syslog.h>
024
2022-02-16
op
#include "log.h"
025
2022-02-16
op
#include "xmalloc.h"
026
2022-02-16
op
#include "playlist.h"
028
2022-02-16
op
#define MAX(a, b) ((a) > (b) ? (a) : (b))
030
2022-02-19
op
struct playlist playlist;
031
2022-02-19
op
enum play_state play_state;
032
2022-02-19
op
int repeat_one;
033
2022-02-19
op
int repeat_all = 1;
034
2022-02-19
op
ssize_t play_off = -1;
035
2022-02-19
op
const char *current_song;
037
2022-02-19
op
static void
038
2022-02-19
op
setsong(ssize_t i)
040
2022-02-19
op
free((char *)current_song);
041
2022-02-19
op
if (i == -1)
042
2022-02-19
op
current_song = NULL;
044
2022-02-19
op
current_song = xstrdup(playlist.songs[i]);
048
2022-02-17
op
playlist_swap(struct playlist *p)
050
2022-02-19
op
ssize_t i = -1;
052
2022-02-19
op
if (current_song != NULL) {
053
2022-02-19
op
/* try to adjust play_off to match the same song */
054
2022-02-19
op
for (i = 0; i < p->len; ++i) {
055
2022-02-19
op
if (!strcmp(current_song, p->songs[i]))
058
2022-02-19
op
/* try to match one song before */
059
2022-02-19
op
if (i == p->len && play_off >= 1)
060
2022-02-19
op
for (i = 0; i < p->len; ++i)
061
2022-02-19
op
if (!strcmp(current_song, p->songs[i]))
063
2022-02-19
op
/* or one song after */
064
2022-02-19
op
if (i == p->len && play_off < playlist.len-1)
065
2022-02-19
op
for (i = 0; i < p->len; ++i)
066
2022-02-19
op
if (!strcmp(current_song, p->songs[i]))
068
2022-02-19
op
if (i == p->len)
072
2022-02-17
op
playlist_truncate();
074
2022-02-19
op
if (i != -1)
075
2022-02-19
op
play_off = i;
077
2022-02-17
op
playlist.len = p->len;
078
2022-02-17
op
playlist.cap = p->cap;
079
2022-02-17
op
playlist.songs = p->songs;
083
2022-02-17
op
playlist_push(struct playlist *playlist, const char *path)
085
2022-02-16
op
size_t newcap;
087
2022-02-17
op
if (playlist->len == playlist->cap) {
088
2022-02-17
op
newcap = MAX(16, playlist->cap * 1.5);
089
2022-02-17
op
playlist->songs = xrecallocarray(playlist->songs,
090
2022-02-17
op
playlist->cap, newcap, sizeof(*playlist->songs));
091
2022-02-17
op
playlist->cap = newcap;
094
2022-02-17
op
playlist->songs[playlist->len++] = xstrdup(path);
098
2022-02-17
op
playlist_enqueue(const char *path)
100
2022-02-17
op
playlist_push(&playlist, path);
103
2022-02-16
op
const char *
104
2022-02-16
op
playlist_advance(void)
106
2022-02-16
op
if (playlist.len == 0) {
107
2022-02-16
op
play_state = STATE_STOPPED;
108
2022-02-16
op
return NULL;
111
2022-02-16
op
play_off++;
112
2022-02-16
op
if (play_off == playlist.len) {
113
2022-02-16
op
if (repeat_all)
114
2022-02-16
op
play_off = 0;
116
2022-02-16
op
play_state = STATE_STOPPED;
117
2022-02-16
op
play_off = -1;
118
2022-02-19
op
setsong(play_off);
119
2022-02-16
op
return NULL;
123
2022-02-19
op
setsong(play_off);
124
2022-02-16
op
play_state = STATE_PLAYING;
125
2022-02-16
op
return playlist.songs[play_off];
128
2022-02-17
op
const char *
129
2022-02-17
op
playlist_previous(void)
131
2022-02-17
op
if (playlist.len == 0) {
132
2022-02-17
op
play_state = STATE_STOPPED;
133
2022-02-17
op
return NULL;
136
2022-02-17
op
play_off--;
137
2022-02-17
op
if (play_off < 0) {
138
2022-02-17
op
if (repeat_all)
139
2022-02-17
op
play_off = playlist.len - 1;
141
2022-02-17
op
play_state = STATE_STOPPED;
142
2022-02-17
op
play_off = -1;
143
2022-02-19
op
setsong(play_off);
144
2022-02-17
op
return NULL;
148
2022-02-19
op
setsong(play_off);
149
2022-02-17
op
play_state = STATE_PLAYING;
150
2022-02-17
op
return playlist.songs[play_off];
154
2022-02-16
op
playlist_reset(void)
156
2022-02-16
op
play_off = -1;
160
2022-02-17
op
playlist_free(struct playlist *playlist)
162
2022-02-16
op
size_t i;
164
2022-02-17
op
for (i = 0; i < playlist->len; ++i)
165
2022-02-17
op
free(playlist->songs[i]);
166
2022-02-17
op
free(playlist->songs);
167
2022-02-17
op
playlist->songs = NULL;
169
2022-02-17
op
playlist->len = 0;
170
2022-02-17
op
playlist->cap = 0;
174
2022-02-17
op
playlist_truncate(void)
176
2022-02-17
op
playlist_free(&playlist);
177
2022-02-16
op
play_off = -1;
181
2022-02-16
op
playlist_dropcurrent(void)
183
2022-02-16
op
size_t i;
185
2022-02-16
op
if (play_off == -1 || playlist.len == 0)
188
2022-02-16
op
free(playlist.songs[play_off]);
190
2022-02-16
op
playlist.len--;
191
2022-02-16
op
for (i = play_off; i < playlist.len; ++i)
192
2022-02-16
op
playlist.songs[i] = playlist.songs[i+1];
194
2022-02-16
op
playlist.songs[playlist.len] = NULL;
197
2022-02-17
op
const char *
198
2022-02-17
op
playlist_jump(const char *arg)
200
2022-02-17
op
size_t i;
201
2022-02-17
op
regex_t re;
203
2022-02-17
op
if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
204
2022-02-17
op
return NULL;
206
2022-02-17
op
for (i = 0; i < playlist.len; ++i) {
207
2022-02-17
op
if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
210
2022-02-17
op
regfree(&re);
212
2022-02-17
op
if (i == playlist.len)
213
2022-02-17
op
return NULL;
215
2022-02-17
op
play_state = STATE_PLAYING;
216
2022-02-17
op
play_off = i;
217
2022-02-19
op
setsong(play_off);
218
2022-02-17
op
return playlist.songs[i];