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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 #include "disorder-server.h"
22 /* the head of the queue is played next, so normally we add to the tail */
23 struct queue_entry qhead
= { &qhead
, &qhead
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
25 /* the head of the recent list is the oldest thing, the tail the most recently
27 struct queue_entry phead
= { &phead
, &phead
, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
31 void queue_fix_sofar(struct queue_entry
*q
) {
34 /* Fake up SOFAR field for currently-playing tracks that don't have it filled
35 * in by the speaker process. XXX this horrible bodge should go away when we
36 * have a more general implementation of pausing as that field will always
37 * have to be right for the playing track. */
38 if((q
->state
== playing_started
39 || q
->state
== playing_paused
)
40 && q
->type
& DISORDER_PLAYER_PAUSES
41 && (q
->type
& DISORDER_PLAYER_TYPEMASK
) != DISORDER_PLAYER_RAW
) {
43 if(q
->uptopause
== -1) /* Don't know how far thru. */
45 else if(q
->lastresumed
) /* Has been paused and resumed. */
46 sofar
= q
->uptopause
+ time(0) - q
->lastresumed
;
47 else /* Currently paused. */
49 } else /* Never been paused. */
50 sofar
= time(0) - q
->played
;
55 static void queue_read_error(const char *msg
,
57 fatal(0, "error parsing queue %s: %s", (const char *)u
, msg
);
60 static void queue_do_read(struct queue_entry
*head
, const char *path
) {
63 struct queue_entry
*q
;
65 if(!(fp
= fopen(path
, "r"))) {
67 return; /* no queue */
68 fatal(errno
, "error opening %s", path
);
70 head
->next
= head
->prev
= head
;
71 while(!inputline(path
, fp
, &buffer
, '\n')) {
72 q
= xmalloc(sizeof *q
);
73 queue_unmarshall(q
, buffer
, queue_read_error
, (void *)path
);
77 fatal(0, "incomplete queue entry in %s", path
);
78 queue_insert_entry(head
->prev
, q
);
80 if(ferror(fp
)) fatal(errno
, "error reading %s", path
);
84 void queue_read(void) {
85 queue_do_read(&qhead
, config_get_file("queue"));
88 void recent_read(void) {
89 struct queue_entry
*q
;
91 queue_do_read(&phead
, config_get_file("recent"));
92 /* reset pcount after loading */
101 static void queue_do_write(const struct queue_entry
*head
, const char *path
) {
104 struct queue_entry
*q
;
106 byte_xasprintf(&tmp
, "%s.new", path
);
107 if(!(fp
= fopen(tmp
, "w"))) fatal(errno
, "error opening %s", tmp
);
108 for(q
= head
->next
; q
!= head
; q
= q
->next
)
109 if(fprintf(fp
, "%s\n", queue_marshall(q
)) < 0)
110 fatal(errno
, "error writing %s", tmp
);
111 if(fclose(fp
) < 0) fatal(errno
, "error closing %s", tmp
);
112 if(rename(tmp
, path
) < 0) fatal(errno
, "error replacing %s", path
);
115 void queue_write(void) {
116 queue_do_write(&qhead
, config_get_file("queue"));
119 void recent_write(void) {
120 queue_do_write(&phead
, config_get_file("recent"));
123 struct queue_entry
*queue_find(const char *key
) {
124 struct queue_entry
*q
;
127 q
!= &qhead
&& strcmp(q
->track
, key
) && strcmp(q
->id
, key
);
130 return q
!= &qhead ? q
: 0;