1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#include "cmus.h"
#include "job.h"
#include "lib.h"
#include "pl.h"
#include "player.h"
#include "input.h"
#include "play_queue.h"
#include "worker.h"
#include "cache.h"
#include "misc.h"
#include "file.h"
#include "utils.h"
#include "path.h"
#include "options.h"
#include "xmalloc.h"
#include "xstrjoin.h"
#include "debug.h"
#include "load_dir.h"
#include "ui_curses.h"
#include "cache.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <ctype.h>
static char **playable_exts;
static const char * const playlist_exts[] = { "m3u", "pl", "pls", NULL };
int cmus_init(void)
{
playable_exts = ip_get_supported_extensions();
cache_init();
worker_init();
play_queue_init();
return 0;
}
void cmus_exit(void)
{
worker_remove_jobs(JOB_TYPE_ANY);
worker_exit();
if (cache_close())
d_print("error: %s\n", strerror(errno));
}
void cmus_next(void)
{
struct track_info *info;
editable_lock();
info = play_queue_remove();
if (info == NULL) {
if (play_library) {
info = lib_set_next();
} else {
info = pl_set_next();
}
}
editable_unlock();
if (info)
player_set_file(info);
}
void cmus_prev(void)
{
struct track_info *info;
editable_lock();
if (play_library) {
info = lib_set_prev();
} else {
info = pl_set_prev();
}
editable_unlock();
if (info)
player_set_file(info);
}
void cmus_play_file(const char *filename)
{
struct track_info *ti;
cache_lock();
ti = cache_get_ti(filename);
cache_unlock();
if (!ti) {
error_msg("Couldn't get file information for %s\n", filename);
return;
}
player_play_file(ti);
}
enum file_type cmus_detect_ft(const char *name, char **ret)
{
char *absolute;
struct stat st;
if (is_url(name)) {
*ret = xstrdup(name);
return FILE_TYPE_URL;
}
*ret = NULL;
absolute = path_absolute(name);
if (absolute == NULL)
return FILE_TYPE_INVALID;
/* stat follows symlinks, lstat does not */
if (stat(absolute, &st) == -1) {
free(absolute);
return FILE_TYPE_INVALID;
}
if (S_ISDIR(st.st_mode)) {
*ret = absolute;
return FILE_TYPE_DIR;
}
if (!S_ISREG(st.st_mode)) {
free(absolute);
errno = EINVAL;
return FILE_TYPE_INVALID;
}
*ret = absolute;
if (cmus_is_playlist(absolute))
return FILE_TYPE_PL;
/* NOTE: it could be FILE_TYPE_PL too! */
return FILE_TYPE_FILE;
}
void cmus_add(add_ti_cb add, const char *name, enum file_type ft, int jt)
{
struct add_data *data = xnew(struct add_data, 1);
data->add = add;
data->name = xstrdup(name);
data->type = ft;
worker_add_job(jt, do_add_job, free_add_job, data);
}
static int save_playlist_cb(void *data, struct track_info *ti)
{
int fd = *(int *)data;
const char nl = '\n';
int rc;
rc = write_all(fd, ti->filename, strlen(ti->filename));
if (rc == -1)
return -1;
rc = write_all(fd, &nl, 1);
if (rc == -1)
return -1;
return 0;
}
int cmus_save(for_each_ti_cb for_each_ti, const char *filename)
{
int fd, rc;
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (fd == -1)
return -1;
rc = for_each_ti(save_playlist_cb, &fd);
close(fd);
return rc;
}
static int update_cb(void *data, struct track_info *ti)
{
struct update_data *d = data;
if (d->size == d->used) {
if (d->size == 0)
d->size = 16;
d->size *= 2;
d->ti = xrealloc(d->ti, d->size * sizeof(struct track_info *));
}
track_info_ref(ti);
d->ti[d->used++] = ti;
return 0;
}
void cmus_update_cache(void)
{
worker_add_job(JOB_TYPE_LIB, do_update_cache_job, free_update_cache_job, NULL);
}
void cmus_update_lib(void)
{
struct update_data *data;
data = xnew(struct update_data, 1);
data->size = 0;
data->used = 0;
data->ti = NULL;
editable_lock();
lib_for_each(update_cb, data);
editable_unlock();
worker_add_job(JOB_TYPE_LIB, do_update_job, free_update_job, data);
}
void cmus_update_tis(struct track_info **tis, int nr)
{
struct update_data *data;
data = xnew(struct update_data, 1);
data->size = nr;
data->used = nr;
data->ti = tis;
worker_add_job(JOB_TYPE_LIB, do_update_job, free_update_job, data);
}
static const char *get_ext(const char *filename)
{
const char *ext = strrchr(filename, '.');
if (ext)
ext++;
return ext;
}
static int str_in_array(const char *str, const char * const *array)
{
int i;
for (i = 0; array[i]; i++) {
if (strcasecmp(str, array[i]) == 0)
return 1;
}
return 0;
}
int cmus_is_playlist(const char *filename)
{
const char *ext = get_ext(filename);
return ext && str_in_array(ext, playlist_exts);
}
int cmus_is_playable(const char *filename)
{
const char *ext = get_ext(filename);
return ext && str_in_array(ext, (const char * const *)playable_exts);
}
int cmus_is_supported(const char *filename)
{
const char *ext = get_ext(filename);
return ext && (str_in_array(ext, (const char * const *)playable_exts) ||
str_in_array(ext, playlist_exts));
}
struct pl_data {
int (*cb)(void *data, const char *line);
void *data;
};
static int pl_handle_line(void *data, const char *line)
{
struct pl_data *d = data;
int i = 0;
while (isspace((unsigned char)line[i]))
i++;
if (line[i] == 0)
return 0;
if (line[i] == '#')
return 0;
return d->cb(d->data, line);
}
static int pls_handle_line(void *data, const char *line)
{
struct pl_data *d = data;
if (strncasecmp(line, "file", 4))
return 0;
line = strchr(line, '=');
if (line == NULL)
return 0;
return d->cb(d->data, line + 1);
}
int cmus_playlist_for_each(const char *buf, int size, int reverse,
int (*cb)(void *data, const char *line),
void *data)
{
struct pl_data d = { cb, data };
int (*handler)(void *, const char *);
handler = pl_handle_line;
if (size >= 10 && strncasecmp(buf, "[playlist]", 10) == 0)
handler = pls_handle_line;
if (reverse) {
buffer_for_each_line_reverse(buf, size, handler, &d);
} else {
buffer_for_each_line(buf, size, handler, &d);
}
return 0;
}
|