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-03-02
op
playlist_swap(struct playlist *p, ssize_t off)
050
2022-02-19
op
ssize_t i = -1;
052
2022-03-02
op
if (off > p->len)
053
2022-03-02
op
off = -1;
055
2022-03-02
op
if (current_song != NULL && off < 0) {
056
2022-03-10
op
/* try to match the currently played song */
057
2022-02-19
op
for (i = 0; i < p->len; ++i) {
058
2022-02-19
op
if (!strcmp(current_song, p->songs[i]))
061
2022-02-19
op
if (i == p->len)
065
2022-02-17
op
playlist_truncate();
067
2022-02-19
op
if (i != -1)
068
2022-02-19
op
play_off = i;
069
2022-03-02
op
else if (off >= 0)
070
2022-03-02
op
play_off = off;
072
2022-02-17
op
playlist.len = p->len;
073
2022-02-17
op
playlist.cap = p->cap;
074
2022-02-17
op
playlist.songs = p->songs;
076
2022-03-02
op
if (play_state == STATE_STOPPED)
077
2022-03-02
op
setsong(play_off);
081
2022-02-17
op
playlist_push(struct playlist *playlist, const char *path)
083
2022-02-16
op
size_t newcap;
085
2022-02-17
op
if (playlist->len == playlist->cap) {
086
2022-02-17
op
newcap = MAX(16, playlist->cap * 1.5);
087
2022-02-17
op
playlist->songs = xrecallocarray(playlist->songs,
088
2022-02-17
op
playlist->cap, newcap, sizeof(*playlist->songs));
089
2022-02-17
op
playlist->cap = newcap;
092
2022-02-17
op
playlist->songs[playlist->len++] = xstrdup(path);
096
2022-02-17
op
playlist_enqueue(const char *path)
098
2022-02-17
op
playlist_push(&playlist, path);
101
2022-02-16
op
const char *
102
2022-02-16
op
playlist_advance(void)
104
2022-02-16
op
if (playlist.len == 0) {
105
2022-02-16
op
play_state = STATE_STOPPED;
106
2022-02-16
op
return NULL;
109
2022-02-16
op
play_off++;
110
2022-02-16
op
if (play_off == playlist.len) {
111
2022-02-16
op
if (repeat_all)
112
2022-02-16
op
play_off = 0;
114
2022-02-16
op
play_state = STATE_STOPPED;
115
2022-02-16
op
play_off = -1;
116
2022-02-19
op
setsong(play_off);
117
2022-02-16
op
return NULL;
121
2022-02-19
op
setsong(play_off);
122
2022-02-16
op
play_state = STATE_PLAYING;
123
2022-02-16
op
return playlist.songs[play_off];
126
2022-02-17
op
const char *
127
2022-02-17
op
playlist_previous(void)
129
2022-02-17
op
if (playlist.len == 0) {
130
2022-02-17
op
play_state = STATE_STOPPED;
131
2022-02-17
op
return NULL;
134
2022-02-17
op
play_off--;
135
2022-02-17
op
if (play_off < 0) {
136
2022-02-17
op
if (repeat_all)
137
2022-02-17
op
play_off = playlist.len - 1;
139
2022-02-17
op
play_state = STATE_STOPPED;
140
2022-02-17
op
play_off = -1;
141
2022-02-19
op
setsong(play_off);
142
2022-02-17
op
return NULL;
146
2022-02-19
op
setsong(play_off);
147
2022-02-17
op
play_state = STATE_PLAYING;
148
2022-02-17
op
return playlist.songs[play_off];
152
2022-02-16
op
playlist_reset(void)
154
2022-02-16
op
play_off = -1;
158
2022-02-17
op
playlist_free(struct playlist *playlist)
160
2022-02-16
op
size_t i;
162
2022-02-17
op
for (i = 0; i < playlist->len; ++i)
163
2022-02-17
op
free(playlist->songs[i]);
164
2022-02-17
op
free(playlist->songs);
165
2022-02-17
op
playlist->songs = NULL;
167
2022-02-17
op
playlist->len = 0;
168
2022-02-17
op
playlist->cap = 0;
172
2022-02-17
op
playlist_truncate(void)
174
2022-02-17
op
playlist_free(&playlist);
175
2022-02-16
op
play_off = -1;
179
2022-02-16
op
playlist_dropcurrent(void)
181
2022-02-16
op
size_t i;
183
2022-02-16
op
if (play_off == -1 || playlist.len == 0)
186
2022-02-16
op
free(playlist.songs[play_off]);
187
2022-02-22
op
setsong(-1);
189
2022-02-16
op
playlist.len--;
190
2022-02-16
op
for (i = play_off; i < playlist.len; ++i)
191
2022-02-16
op
playlist.songs[i] = playlist.songs[i+1];
192
2022-02-22
op
play_off--;
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];