Blame
Date:
Thu Feb 17 15:41:02 2022 UTC
Message:
add `jump' subcommand to play the first matching
001
2022-02-16
op
/*
002
2022-02-16
op
* Copyright (c) 2022 Omar Polo <op@openbsd.org>
003
2022-02-16
op
*
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.
007
2022-02-16
op
*
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.
015
2022-02-16
op
*/
016
2022-02-16
op
017
2022-02-17
op
#include <sys/types.h>
018
2022-02-17
op
019
2022-02-17
op
#include <regex.h>
020
2022-02-16
op
#include <stdlib.h>
021
2022-02-16
op
#include <syslog.h>
022
2022-02-16
op
023
2022-02-16
op
#include "log.h"
024
2022-02-16
op
#include "xmalloc.h"
025
2022-02-16
op
#include "playlist.h"
026
2022-02-16
op
027
2022-02-16
op
#define MAX(a, b) ((a) > (b) ? (a) : (b))
028
2022-02-16
op
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;
034
2022-02-16
op
035
2022-02-16
op
void
036
2022-02-17
op
playlist_swap(struct playlist *p)
037
2022-02-17
op
{
038
2022-02-17
op
playlist_truncate();
039
2022-02-17
op
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;
043
2022-02-17
op
}
044
2022-02-17
op
045
2022-02-17
op
void
046
2022-02-17
op
playlist_push(struct playlist *playlist, const char *path)
047
2022-02-16
op
{
048
2022-02-16
op
size_t newcap;
049
2022-02-16
op
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;
055
2022-02-16
op
}
056
2022-02-16
op
057
2022-02-17
op
playlist->songs[playlist->len++] = xstrdup(path);
058
2022-02-16
op
}
059
2022-02-16
op
060
2022-02-17
op
void
061
2022-02-17
op
playlist_enqueue(const char *path)
062
2022-02-17
op
{
063
2022-02-17
op
playlist_push(&playlist, path);
064
2022-02-17
op
}
065
2022-02-17
op
066
2022-02-16
op
const char *
067
2022-02-16
op
playlist_current(void)
068
2022-02-16
op
{
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;
072
2022-02-16
op
}
073
2022-02-16
op
074
2022-02-16
op
return playlist.songs[play_off];
075
2022-02-16
op
}
076
2022-02-16
op
077
2022-02-16
op
const char *
078
2022-02-16
op
playlist_advance(void)
079
2022-02-16
op
{
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;
083
2022-02-16
op
}
084
2022-02-16
op
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;
089
2022-02-16
op
else {
090
2022-02-16
op
play_state = STATE_STOPPED;
091
2022-02-16
op
play_off = -1;
092
2022-02-16
op
return NULL;
093
2022-02-16
op
}
094
2022-02-16
op
}
095
2022-02-16
op
096
2022-02-16
op
play_state = STATE_PLAYING;
097
2022-02-16
op
return playlist.songs[play_off];
098
2022-02-16
op
}
099
2022-02-16
op
100
2022-02-17
op
const char *
101
2022-02-17
op
playlist_previous(void)
102
2022-02-17
op
{
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;
106
2022-02-17
op
}
107
2022-02-17
op
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;
112
2022-02-17
op
else {
113
2022-02-17
op
play_state = STATE_STOPPED;
114
2022-02-17
op
play_off = -1;
115
2022-02-17
op
return NULL;
116
2022-02-17
op
}
117
2022-02-17
op
}
118
2022-02-17
op
119
2022-02-17
op
play_state = STATE_PLAYING;
120
2022-02-17
op
return playlist.songs[play_off];
121
2022-02-17
op
}
122
2022-02-17
op
123
2022-02-16
op
void
124
2022-02-16
op
playlist_reset(void)
125
2022-02-16
op
{
126
2022-02-16
op
play_off = -1;
127
2022-02-16
op
}
128
2022-02-16
op
129
2022-02-16
op
void
130
2022-02-17
op
playlist_free(struct playlist *playlist)
131
2022-02-16
op
{
132
2022-02-16
op
size_t i;
133
2022-02-16
op
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;
138
2022-02-16
op
139
2022-02-17
op
playlist->len = 0;
140
2022-02-17
op
playlist->cap = 0;
141
2022-02-17
op
}
142
2022-02-17
op
143
2022-02-17
op
void
144
2022-02-17
op
playlist_truncate(void)
145
2022-02-17
op
{
146
2022-02-17
op
playlist_free(&playlist);
147
2022-02-16
op
play_off = -1;
148
2022-02-16
op
}
149
2022-02-16
op
150
2022-02-16
op
void
151
2022-02-16
op
playlist_dropcurrent(void)
152
2022-02-16
op
{
153
2022-02-16
op
size_t i;
154
2022-02-16
op
155
2022-02-16
op
if (play_off == -1 || playlist.len == 0)
156
2022-02-16
op
return;
157
2022-02-16
op
158
2022-02-16
op
free(playlist.songs[play_off]);
159
2022-02-16
op
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];
163
2022-02-16
op
164
2022-02-16
op
playlist.songs[playlist.len] = NULL;
165
2022-02-16
op
}
166
2022-02-17
op
167
2022-02-17
op
const char *
168
2022-02-17
op
playlist_jump(const char *arg)
169
2022-02-17
op
{
170
2022-02-17
op
size_t i;
171
2022-02-17
op
regex_t re;
172
2022-02-17
op
173
2022-02-17
op
if (regcomp(&re, arg, REG_ICASE | REG_NOSUB) != 0)
174
2022-02-17
op
return NULL;
175
2022-02-17
op
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)
178
2022-02-17
op
break;
179
2022-02-17
op
}
180
2022-02-17
op
regfree(&re);
181
2022-02-17
op
182
2022-02-17
op
if (i == playlist.len)
183
2022-02-17
op
return NULL;
184
2022-02-17
op
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];
188
2022-02-17
op
}
Omar Polo