chiark / gitweb /
Read user.tmpl after macros.tmpl
[disorder] / server / server-queue.c
CommitLineData
f08c0859
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 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 2 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, 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.
14 *
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
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <string.h>
25#include <unistd.h>
26#include <stdio.h>
27#include <errno.h>
28#include <time.h>
29
30#include "mem.h"
31#include "log.h"
32#include "split.h"
33#include "printf.h"
34#include "queue.h"
35#include "server-queue.h"
36#include "eventlog.h"
37#include "plugin.h"
38#include "basen.h"
39#include "configuration.h"
40#include "inputline.h"
41#include "disorder.h"
42
43/* the head of the queue is played next, so normally we add to the tail */
66bb2e02 44struct queue_entry qhead = { &qhead, &qhead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
f08c0859
RK
45
46/* the head of the recent list is the oldest thing, the tail the most recently
47 * played */
66bb2e02 48struct queue_entry phead = { &phead, &phead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
f08c0859
RK
49
50static long pcount;
51
52/* add new entry @n@ to a doubly linked list just after @b@ */
53static void l_add(struct queue_entry *b, struct queue_entry *n) {
54 n->prev = b;
55 n->next = b->next;
56 n->next->prev = n;
57 n->prev->next = n;
58}
59
60/* remove an entry from a doubly-linked list */
61static void l_remove(struct queue_entry *node) {
62 node->next->prev = node->prev;
63 node->prev->next = node->next;
64}
65
66void queue_fix_sofar(struct queue_entry *q) {
67 long sofar;
68
69 /* Fake up SOFAR field for currently-playing tracks that don't have it filled
70 * in by the speaker process. XXX this horrible bodge should go away when we
71 * have a more general implementation of pausing as that field will always
72 * have to be right for the playing track. */
73 if((q->state == playing_started
74 || q->state == playing_paused)
75 && q->type & DISORDER_PLAYER_PAUSES
76 && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) {
77 if(q->lastpaused) {
78 if(q->uptopause == -1) /* Don't know how far thru. */
79 sofar = -1;
80 else if(q->lastresumed) /* Has been paused and resumed. */
81 sofar = q->uptopause + time(0) - q->lastresumed;
82 else /* Currently paused. */
83 sofar = q->uptopause;
84 } else /* Never been paused. */
85 sofar = time(0) - q->played;
86 q->sofar = sofar;
87 }
88}
89
90static void queue_read_error(const char *msg,
91 void *u) {
92 fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
93}
94
95static void queue_do_read(struct queue_entry *head, const char *path) {
96 char *buffer;
97 FILE *fp;
98 struct queue_entry *q;
99
100 if(!(fp = fopen(path, "r"))) {
101 if(errno == ENOENT)
102 return; /* no queue */
103 fatal(errno, "error opening %s", path);
104 }
105 head->next = head->prev = head;
106 while(!inputline(path, fp, &buffer, '\n')) {
107 q = xmalloc(sizeof *q);
108 queue_unmarshall(q, buffer, queue_read_error, (void *)path);
109 if(head == &qhead
110 && (!q->track
111 || !q->when))
112 fatal(0, "incomplete queue entry in %s", path);
113 l_add(head->prev, q);
114 }
115 if(ferror(fp)) fatal(errno, "error reading %s", path);
116 fclose(fp);
117}
118
119void queue_read(void) {
120 queue_do_read(&qhead, config_get_file("queue"));
121}
122
123void recent_read(void) {
124 struct queue_entry *q;
125
126 queue_do_read(&phead, config_get_file("recent"));
127 /* reset pcount after loading */
128 pcount = 0;
129 q = phead.next;
130 while(q != &phead) {
131 ++pcount;
132 q = q->next;
133 }
134}
135
136static void queue_do_write(const struct queue_entry *head, const char *path) {
137 char *tmp;
138 FILE *fp;
139 struct queue_entry *q;
140
141 byte_xasprintf(&tmp, "%s.new", path);
142 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
143 for(q = head->next; q != head; q = q->next)
144 if(fprintf(fp, "%s\n", queue_marshall(q)) < 0)
145 fatal(errno, "error writing %s", tmp);
146 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
147 if(rename(tmp, path) < 0) fatal(errno, "error replacing %s", path);
148}
149
150void queue_write(void) {
151 queue_do_write(&qhead, config_get_file("queue"));
152}
153
154void recent_write(void) {
155 queue_do_write(&phead, config_get_file("recent"));
156}
157
158static void queue_id(struct queue_entry *q) {
159 static unsigned long serial;
160 unsigned long a[3];
161 char buffer[128];
162
163 a[0] = serial++ & 0xFFFFFFFFUL;
164 a[1] = time(0) & 0xFFFFFFFFUL;
165 a[2] = getpid() & 0xFFFFFFFFUL;
166 basen(a, 3, buffer, sizeof buffer, 62);
167 q->id = xstrdup(buffer);
168}
169
170struct queue_entry *queue_add(const char *track, const char *submitter,
171 int where) {
172 struct queue_entry *q, *beforeme;
173
174 q = xmalloc(sizeof *q);
175 q->track = xstrdup(track);
176 q->submitter = submitter ? xstrdup(submitter) : 0;
177 q->state = playing_unplayed;
178 queue_id(q);
179 time(&q->when);
180 switch(where) {
181 case WHERE_START:
182 l_add(&qhead, q);
183 break;
184 case WHERE_END:
185 l_add(qhead.prev, q);
186 break;
187 case WHERE_BEFORE_RANDOM:
188 /* We want to find the point in the queue before the block of random tracks
189 * at the end. */
190 beforeme = &qhead;
191 while(beforeme->prev != &qhead
192 && beforeme->prev->state == playing_random)
193 beforeme = beforeme->prev;
194 l_add(beforeme->prev, q);
195 break;
196 }
197 /* submitter will be a null pointer for a scratch */
198 if(submitter)
199 notify_queue(track, submitter);
200 eventlog_raw("queue", queue_marshall(q), (const char *)0);
201 return q;
202}
203
204int queue_move(struct queue_entry *q, int delta, const char *who) {
205 int moved = 0;
206 char buffer[20];
207
208 /* not the most efficient approach but hopefuly relatively comprehensible:
209 * the idea is that for each step we determine which nodes are affected, and
210 * fill in all the links starting at the 'prev' end and moving towards the
211 * 'next' end. */
212
213 while(delta > 0 && q->prev != &qhead) {
214 struct queue_entry *n, *p, *pp;
215
216 n = q->next;
217 p = q->prev;
218 pp = p->prev;
219 pp->next = q;
220 q->prev = pp;
221 q->next = p;
222 p->prev = q;
223 p->next = n;
224 n->prev = p;
225 --delta;
226 ++moved;
227 }
228
229 while(delta < 0 && q->next != &qhead) {
230 struct queue_entry *n, *p, *nn;
231
232 p = q->prev;
233 n = q->next;
234 nn = n->next;
235 p->next = n;
236 n->prev = p;
237 n->next = q;
238 q->prev = n;
239 q->next = nn;
240 nn->prev = q;
241 ++delta;
242 --moved;
243 }
244
245 if(moved) {
246 info("user %s moved %s", who, q->id);
247 notify_queue_move(q->track, who);
248 sprintf(buffer, "%d", moved);
249 eventlog("moved", who, (char *)0);
250 }
251
252 return delta;
253}
254
255static int find_in_list(struct queue_entry *needle,
256 int nqs, struct queue_entry **qs) {
257 int n;
258
259 for(n = 0; n < nqs; ++n)
260 if(qs[n] == needle)
261 return 1;
262 return 0;
263}
264
265void queue_moveafter(struct queue_entry *target,
266 int nqs, struct queue_entry **qs,
267 const char *who) {
268 struct queue_entry *q;
269 int n;
270
271 /* Normalize */
272 if(!target)
273 target = &qhead;
274 else
275 while(find_in_list(target, nqs, qs))
276 target = target->prev;
277 /* Do the move */
278 for(n = 0; n < nqs; ++n) {
279 q = qs[n];
280 l_remove(q);
281 l_add(target, q);
282 target = q;
283 /* Log the individual tracks */
284 info("user %s moved %s", who, q->id);
285 notify_queue_move(q->track, who);
286 }
287 /* Report that the queue changed to the event log */
288 eventlog("moved", who, (char *)0);
289}
290
291void queue_remove(struct queue_entry *which, const char *who) {
292 if(who) {
293 info("user %s removed %s", who, which->id);
294 notify_queue_move(which->track, who);
295 }
296 eventlog("removed", which->id, who, (const char *)0);
297 l_remove(which);
298}
299
300struct queue_entry *queue_find(const char *key) {
301 struct queue_entry *q;
302
303 for(q = qhead.next;
304 q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
305 q = q->next)
306 ;
307 return q != &qhead ? q : 0;
308}
309
310void queue_played(struct queue_entry *q) {
311 while(pcount && pcount >= config->history) {
312 eventlog("recent_removed", phead.next->id, (char *)0);
313 l_remove(phead.next);
314 pcount--;
315 }
316 if(config->history) {
317 eventlog_raw("recent_added", queue_marshall(q), (char *)0);
318 l_add(phead.prev, q);
319 ++pcount;
320 }
321}
322
323/*
324Local Variables:
325c-basic-offset:2
326comment-column:40
327fill-column:79
328indent-tabs-mode:nil
329End:
330*/