chiark / gitweb /
Switch to GPL v3
[disorder] / server / server-queue.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2008 Richard Kettlewell
4  *
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.
9  *
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.
14  * 
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/>.
17  */
18 #include "disorder-server.h"
19
20 /* the head of the queue is played next, so normally we add to the tail */
21 struct queue_entry qhead = { &qhead, &qhead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
22
23 /* the head of the recent list is the oldest thing, the tail the most recently
24  * played */
25 struct queue_entry phead = { &phead, &phead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
26
27 long pcount;
28
29 void queue_fix_sofar(struct queue_entry *q) {
30   long sofar;
31   
32   /* Fake up SOFAR field for currently-playing tracks that don't have it filled
33    * in by the speaker process.  XXX this horrible bodge should go away when we
34    * have a more general implementation of pausing as that field will always
35    * have to be right for the playing track. */
36   if((q->state == playing_started
37       || q->state == playing_paused)
38      && q->type & DISORDER_PLAYER_PAUSES
39      && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) {
40     if(q->lastpaused) {
41       if(q->uptopause == -1)            /* Don't know how far thru. */
42         sofar = -1;
43       else if(q->lastresumed)           /* Has been paused and resumed. */
44         sofar = q->uptopause + time(0) - q->lastresumed;
45       else                              /* Currently paused. */
46         sofar = q->uptopause;
47     } else                              /* Never been paused. */
48       sofar = time(0) - q->played;
49     q->sofar = sofar;
50   }
51 }
52
53 static void queue_read_error(const char *msg,
54                              void *u) {
55   fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
56 }
57
58 static void queue_do_read(struct queue_entry *head, const char *path) {
59   char *buffer;
60   FILE *fp;
61   struct queue_entry *q;
62
63   if(!(fp = fopen(path, "r"))) {
64     if(errno == ENOENT)
65       return;                   /* no queue */
66     fatal(errno, "error opening %s", path);
67   }
68   head->next = head->prev = head;
69   while(!inputline(path, fp, &buffer, '\n')) {
70     q = xmalloc(sizeof *q);
71     queue_unmarshall(q, buffer, queue_read_error, (void *)path);
72     if(head == &qhead
73        && (!q->track
74            || !q->when))
75       fatal(0, "incomplete queue entry in %s", path);
76     queue_insert_entry(head->prev, q);
77   }
78   if(ferror(fp)) fatal(errno, "error reading %s", path);
79   fclose(fp);
80 }
81
82 void queue_read(void) {
83   queue_do_read(&qhead, config_get_file("queue"));
84 }
85
86 void recent_read(void) {
87   struct queue_entry *q;
88
89   queue_do_read(&phead, config_get_file("recent"));
90   /* reset pcount after loading */
91   pcount = 0;
92   q = phead.next;
93   while(q != &phead) {
94     ++pcount;
95     q = q->next;
96   }
97 }
98
99 static void queue_do_write(const struct queue_entry *head, const char *path) {
100   char *tmp;
101   FILE *fp;
102   struct queue_entry *q;
103
104   byte_xasprintf(&tmp, "%s.new", path);
105   if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
106   for(q = head->next; q != head; q = q->next)
107     if(fprintf(fp, "%s\n", queue_marshall(q)) < 0)
108       fatal(errno, "error writing %s", tmp);
109   if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
110   if(rename(tmp, path) < 0) fatal(errno, "error replacing %s", path);
111 }
112
113 void queue_write(void) {
114   queue_do_write(&qhead, config_get_file("queue"));
115 }
116
117 void recent_write(void) {
118   queue_do_write(&phead, config_get_file("recent"));
119 }
120
121 struct queue_entry *queue_find(const char *key) {
122   struct queue_entry *q;
123
124   for(q = qhead.next;
125       q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
126       q = q->next)
127     ;
128   return q != &qhead ? q : 0;
129 }
130
131 /*
132 Local Variables:
133 c-basic-offset:2
134 comment-column:40
135 fill-column:79
136 indent-tabs-mode:nil
137 End:
138 */