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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
* Copyright 2008-2013 Various Authors
* Copyright 2004 Timo Hirvonen
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#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 "debug.h"
#include "load_dir.h"
#include "ui_curses.h"
#include "cache.h"
#include "gbuf.h"
#include "discid.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <ctype.h>
#include <strings.h>
/* save_playlist_cb, save_ext_playlist_cb */
typedef int (*save_tracks_cb)(void *data, struct track_info *ti);
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_goto_next();
} else {
info = pl_goto_next();
}
}
editable_unlock();
if (info)
player_set_file(info);
}
void cmus_prev(void)
{
struct track_info *info;
editable_lock();
if (play_library) {
info = lib_goto_prev();
} else {
info = pl_goto_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, 0);
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_http_url(name) || is_cue_url(name)) {
*ret = xstrdup(name);
return FILE_TYPE_URL;
}
if (is_cdda_url(name)) {
*ret = complete_cdda_url(cdda_device, name);
return FILE_TYPE_CDDA;
}
*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, int force)
{
struct add_data *data = xnew(struct add_data, 1);
data->add = add;
data->name = xstrdup(name);
data->type = ft;
data->force = force;
worker_add_job(jt, do_add_job, free_add_job, data);
}
static int save_ext_playlist_cb(void *data, struct track_info *ti)
{
GBUF(buf);
int fd = *(int *)data;
int i, rc;
gbuf_addf(&buf, "file %s\n", escape(ti->filename));
gbuf_addf(&buf, "duration %d\n", ti->duration);
gbuf_addf(&buf, "codec %s\n", ti->codec);
gbuf_addf(&buf, "bitrate %ld\n", ti->bitrate);
for (i = 0; ti->comments[i].key; i++)
gbuf_addf(&buf, "tag %s %s\n",
ti->comments[i].key,
escape(ti->comments[i].val));
rc = write_all(fd, buf.buffer, buf.len);
gbuf_free(&buf);
if (rc == -1)
return -1;
return 0;
}
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;
}
static int do_cmus_save(for_each_ti_cb for_each_ti, const char *filename, save_tracks_cb save_tracks)
{
int fd, rc;
if (strcmp(filename, "-") == 0) {
if (get_client_fd() == -1) {
error_msg("saving to stdout works only remotely");
return 0;
}
fd = dup(get_client_fd());
} else
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (fd == -1)
return -1;
rc = for_each_ti(save_tracks, &fd);
close(fd);
return rc;
}
int cmus_save(for_each_ti_cb for_each_ti, const char *filename)
{
return do_cmus_save(for_each_ti, filename, save_playlist_cb);
}
int cmus_save_ext(for_each_ti_cb for_each_ti, const char *filename)
{
return do_cmus_save(for_each_ti, filename, save_ext_playlist_cb);
}
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(int force)
{
struct update_cache_data *data;
data = xnew(struct update_cache_data, 1);
data->force = force;
worker_add_job(JOB_TYPE_LIB, do_update_cache_job, free_update_cache_job, data);
}
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, int force)
{
struct update_data *data;
data = xnew(struct update_data, 1);
data->size = nr;
data->used = nr;
data->ti = tis;
data->force = force;
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;
}
|