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-16
op
#include <syslog.h>
023
2022-02-16
op
#include "log.h"
024
2022-02-16
op
#include "xmalloc.h"
025
2022-02-16
op
#include "playlist.h"
027
2022-02-16
op
#define MAX(a, b) ((a) > (b) ? (a) : (b))
029
2022-02-16
op
struct playlist playlist;
030
2022-02-16
op
enum play_state play_state;
031
2022-02-16
op
int repeat_one;
032
2022-02-16
op
int repeat_all = 1;
033
2022-02-16
op
ssize_t play_off = -1;
036
2022-02-17
op
playlist_swap(struct playlist *p)
038
2022-02-17
op
playlist_truncate();
040
2022-02-17
op
playlist.len = p->len;
041
2022-02-17
op
playlist.cap = p->cap;
042
2022-02-17
op
playlist.songs = p->songs;
046
2022-02-17
op
playlist_push(struct playlist *playlist, const char *path)
048
2022-02-16
op
size_t newcap;
050
2022-02-17
op
if (playlist->len == playlist->cap) {
051
2022-02-17
op
newcap = MAX(16, playlist->cap * 1.5);
052
2022-02-17
op
playlist->songs = xrecallocarray(playlist->songs,
053
2022-02-17
op
playlist->cap, newcap, sizeof(*playlist->songs));
054
2022-02-17
op
playlist->cap = newcap;
057
2022-02-17
op
playlist->songs[playlist->len++] = xstrdup(path);
061
2022-02-17
op
playlist_enqueue(const char *path)
063
2022-02-17
op
playlist_push(&playlist, path);
066
2022-02-16
op
const char *
067
2022-02-16
op
playlist_current(void)
069
2022-02-16
op
if (playlist.len == 0 || play_off == -1) {
070
2022-02-16
op
play_state = STATE_STOPPED;
071
2022-02-16
op
return NULL;
074
2022-02-16
op
return playlist.songs[play_off];
077
2022-02-16
op
const char *
078
2022-02-16
op
playlist_advance(void)
080
2022-02-16
op
if (playlist.len == 0) {
081
2022-02-16
op
play_state = STATE_STOPPED;
082
2022-02-16
op
return NULL;
085
2022-02-16
op
play_off++;
086
2022-02-16
op
if (play_off == playlist.len) {
087
2022-02-16
op
if (repeat_all)
088
2022-02-16
op
play_off = 0;
090
2022-02-16
op
play_state = STATE_STOPPED;
091
2022-02-16
op
play_off = -1;
092
2022-02-16
op
return NULL;
096
2022-02-16
op
play_state = STATE_PLAYING;
097
2022-02-16
op
return playlist.songs[play_off];
100
2022-02-17
op
const char *
101
2022-02-17
op
playlist_previous(void)
103
2022-02-17
op
if (playlist.len == 0) {
104
2022-02-17
op
play_state = STATE_STOPPED;
105
2022-02-17
op
return NULL;
108
2022-02-17
op
play_off--;
109
2022-02-17
op
if (play_off < 0) {
110
2022-02-17
op
if (repeat_all)
111
2022-02-17
op
play_off = playlist.len - 1;
113
2022-02-17
op
play_state = STATE_STOPPED;
114
2022-02-17
op
play_off = -1;
115
2022-02-17
op
return NULL;
119
2022-02-17
op
play_state = STATE_PLAYING;
120
2022-02-17
op
return playlist.songs[play_off];
124
2022-02-16
op
playlist_reset(void)
126
2022-02-16
op
play_off = -1;
130
2022-02-17
op
playlist_free(struct playlist *playlist)
132
2022-02-16
op
size_t i;
134
2022-02-17
op
for (i = 0; i < playlist->len; ++i)
135
2022-02-17
op
free(playlist->songs[i]);
136
2022-02-17
op
free(playlist->songs);
137
2022-02-17
op
playlist->songs = NULL;
139
2022-02-17
op
playlist->len = 0;
140
2022-02-17
op
playlist->cap = 0;
144
2022-02-17
op
playlist_truncate(void)
146
2022-02-17
op
playlist_free(&playlist);
147
2022-02-16
op
play_off = -1;
151
2022-02-16
op
playlist_dropcurrent(void)
153
2022-02-16
op
size_t i;
155
2022-02-16
op
if (play_off == -1 || playlist.len == 0)
158
2022-02-16
op
free(playlist.songs[play_off]);
160
2022-02-16
op
playlist.len--;
161
2022-02-16
op
for (i = play_off; i < playlist.len; ++i)
162
2022-02-16
op
playlist.songs[i] = playlist.songs[i+1];
164
2022-02-16
op
playlist.songs[playlist.len] = NULL;
167
2022-02-17
op
const char *
168
2022-02-17
op
playlist_jump(const char *arg)
170
2022-02-17
op
size_t i;
171
2022-02-17
op
regex_t re;
173
2022-02-17
op
if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
174
2022-02-17
op
return NULL;
176
2022-02-17
op
for (i = 0; i < playlist.len; ++i) {
177
2022-02-17
op
if (regexec(&re, playlist.songs[i], 0, NULL, 0) == 0)
180
2022-02-17
op
regfree(&re);
182
2022-02-17
op
if (i == playlist.len)
183
2022-02-17
op
return NULL;
185
2022-02-17
op
play_state = STATE_PLAYING;
186
2022-02-17
op
play_off = i;
187
2022-02-17
op
return playlist.songs[i];