2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "disorder-server.h"
20 /* the head of the queue is played next, so normally we add to the tail */
21 struct queue_entry qhead = {
26 /* the head of the recent list is the oldest thing, the tail the most recently
28 struct queue_entry phead = {
35 void queue_fix_sofar(struct queue_entry *q) {
38 /* Fake up SOFAR field for currently-playing tracks that don't have it filled
39 * in by the speaker process. XXX this horrible bodge should go away when we
40 * have a more general implementation of pausing as that field will always
41 * have to be right for the playing track. */
42 if((q->state == playing_started
43 || q->state == playing_paused)
44 && q->type & DISORDER_PLAYER_PAUSES
45 && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) {
47 if(q->uptopause == -1) /* Don't know how far thru. */
49 else if(q->lastresumed) /* Has been paused and resumed. */
50 sofar = q->uptopause + time(0) - q->lastresumed;
51 else /* Currently paused. */
53 } else /* Never been paused. */
54 sofar = time(0) - q->played;
59 static void queue_read_error(const char *msg,
61 fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
64 static void queue_do_read(struct queue_entry *head, const char *path) {
67 struct queue_entry *q;
69 if(!(fp = fopen(path, "r"))) {
71 return; /* no queue */
72 fatal(errno, "error opening %s", path);
74 head->next = head->prev = head;
75 while(!inputline(path, fp, &buffer, '\n')) {
76 q = xmalloc(sizeof *q);
77 queue_unmarshall(q, buffer, queue_read_error, (void *)path);
81 fatal(0, "incomplete queue entry in %s", path);
82 queue_insert_entry(head->prev, q);
84 if(ferror(fp)) fatal(errno, "error reading %s", path);
88 void queue_read(void) {
89 queue_do_read(&qhead, config_get_file("queue"));
92 void recent_read(void) {
93 struct queue_entry *q;
95 queue_do_read(&phead, config_get_file("recent"));
96 /* reset pcount after loading */
105 static void queue_do_write(const struct queue_entry *head, const char *path) {
108 struct queue_entry *q;
110 byte_xasprintf(&tmp, "%s.new", path);
111 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
112 for(q = head->next; q != head; q = q->next)
113 if(fprintf(fp, "%s\n", queue_marshall(q)) < 0)
114 fatal(errno, "error writing %s", tmp);
115 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
116 if(rename(tmp, path) < 0) fatal(errno, "error replacing %s", path);
119 void queue_write(void) {
120 queue_do_write(&qhead, config_get_file("queue"));
123 void recent_write(void) {
124 queue_do_write(&phead, config_get_file("recent"));
127 struct queue_entry *queue_find(const char *key) {
128 struct queue_entry *q;
131 q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
134 return q != &qhead ? q : 0;