2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009 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 /** @file server/server-queue.c
19 * @brief Server-specific track queue support
21 #include "disorder-server.h"
23 /* the head of the queue is played next, so normally we add to the tail */
24 struct queue_entry qhead
= {
29 /* the head of the recent list is the oldest thing, the tail the most recently
31 struct queue_entry phead
= {
38 void queue_fix_sofar(struct queue_entry
*q
) {
41 /* Fake up SOFAR field for currently-playing tracks that don't have it filled
42 * in by the speaker process. XXX this horrible bodge should go away when we
43 * have a more general implementation of pausing as that field will always
44 * have to be right for the playing track. */
45 if((q
->state
== playing_started
46 || q
->state
== playing_paused
)
47 && q
->type
& DISORDER_PLAYER_PAUSES
48 && (q
->type
& DISORDER_PLAYER_TYPEMASK
) != DISORDER_PLAYER_RAW
) {
50 if(q
->uptopause
== -1) /* Don't know how far thru. */
52 else if(q
->lastresumed
) /* Has been paused and resumed. */
53 sofar
= q
->uptopause
+ xtime(0) - q
->lastresumed
;
54 else /* Currently paused. */
56 } else /* Never been paused. */
57 sofar
= xtime(0) - q
->played
;
62 static void queue_read_error(const char *msg
,
64 disorder_fatal(0, "error parsing queue %s: %s", (const char *)u
, msg
);
67 static void queue_do_read(struct queue_entry
*head
, const char *path
) {
70 struct queue_entry
*q
;
73 if(!(fp
= fopen(path
, "r"))) {
75 return; /* no queue */
76 disorder_fatal(errno
, "error opening %s", path
);
78 head
->next
= head
->prev
= head
;
79 while(!inputline(path
, fp
, &buffer
, '\n')) {
80 if(buffer
[0] == '#') {
81 /* Version indicator */
82 ver
= atoi(buffer
+ 1);
85 q
= xmalloc(sizeof *q
);
86 queue_unmarshall(q
, buffer
, queue_read_error
, (void *)path
);
88 /* Fix up origin field as best we can; will be wrong in some cases but
89 * hopefully not too horribly so. */
90 q
->origin
= q
->submitter ? origin_picked
: origin_random
;
91 /* Eliminated obsolete states, since they are assumed elsewhere not to be
94 case playing_isscratch
:
95 q
->origin
= origin_scratch
;
96 q
->state
= playing_unplayed
;
99 q
->state
= playing_unplayed
;
108 disorder_fatal(0, "incomplete queue entry in %s", path
);
109 queue_insert_entry(head
->prev
, q
);
112 disorder_fatal(errno
, "error reading %s", path
);
116 void queue_read(void) {
117 queue_do_read(&qhead
, config_get_file("queue"));
120 void recent_read(void) {
121 struct queue_entry
*q
;
123 queue_do_read(&phead
, config_get_file("recent"));
124 /* reset pcount after loading */
133 static void queue_do_write(const struct queue_entry
*head
, const char *path
) {
136 struct queue_entry
*q
;
138 byte_xasprintf(&tmp
, "%s.new", path
);
139 if(!(fp
= fopen(tmp
, "w"))) disorder_fatal(errno
, "error opening %s", tmp
);
140 /* Save version indicator */
141 if(fprintf(fp
, "#1\n") < 0)
142 disorder_fatal(errno
, "error writing %s", tmp
);
143 for(q
= head
->next
; q
!= head
; q
= q
->next
)
144 if(fprintf(fp
, "%s\n", queue_marshall(q
)) < 0)
145 disorder_fatal(errno
, "error writing %s", tmp
);
146 if(fclose(fp
) < 0) disorder_fatal(errno
, "error closing %s", tmp
);
147 if(rename(tmp
, path
) < 0) disorder_fatal(errno
, "error replacing %s", path
);
150 void queue_write(void) {
151 queue_do_write(&qhead
, config_get_file("queue"));
154 void recent_write(void) {
155 queue_do_write(&phead
, config_get_file("recent"));
158 struct queue_entry
*queue_find(const char *key
) {
159 struct queue_entry
*q
;
162 q
!= &qhead
&& strcmp(q
->track
, key
) && strcmp(q
->id
, key
);
165 return q
!= &qhead ? q
: 0;