chiark / gitweb /
Quieten over-pick compiler.
[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 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 "random.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 */
44 struct queue_entry qhead = { &qhead, &qhead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
45
46 /* the head of the recent list is the oldest thing, the tail the most recently
47  * played */
48 struct queue_entry phead = { &phead, &phead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
49
50 static long pcount;
51
52 /* add new entry @n@ to a doubly linked list just after @b@ */
53 static 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 */
61 static void l_remove(struct queue_entry *node) {
62   node->next->prev = node->prev;
63   node->prev->next = node->next;
64 }
65
66 void 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
90 static void queue_read_error(const char *msg,
91                              void *u) {
92   fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
93 }
94
95 static 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
119 void queue_read(void) {
120   queue_do_read(&qhead, config_get_file("queue"));
121 }
122
123 void 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
136 static 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
150 void queue_write(void) {
151   queue_do_write(&qhead, config_get_file("queue"));
152 }
153
154 void recent_write(void) {
155   queue_do_write(&phead, config_get_file("recent"));
156 }
157
158 static int id_in_use(const char *id) {
159   struct queue_entry *q;
160
161   for(q = qhead.next; q != &qhead; q = q->next)
162     if(!strcmp(id, q->id))
163       return 1;
164   return 0;
165 }
166
167 static void queue_id(struct queue_entry *q) {
168   const char *id;
169
170   id = random_id();
171   while(id_in_use(id))
172     id = random_id();
173   q->id = id;
174 }
175
176 struct queue_entry *queue_add(const char *track, const char *submitter,
177                               int where) {
178   struct queue_entry *q, *beforeme;
179
180   q = xmalloc(sizeof *q);
181   q->track = xstrdup(track);
182   q->submitter = submitter ? xstrdup(submitter) : 0;
183   q->state = playing_unplayed;
184   queue_id(q);
185   time(&q->when);
186   switch(where) {
187   case WHERE_START:
188     l_add(&qhead, q);
189     break;
190   case WHERE_END:
191     l_add(qhead.prev, q);
192     break;
193   case WHERE_BEFORE_RANDOM:
194     /* We want to find the point in the queue before the block of random tracks
195      * at the end. */
196     beforeme = &qhead;
197     while(beforeme->prev != &qhead
198           && beforeme->prev->state == playing_random)
199       beforeme = beforeme->prev;
200     l_add(beforeme->prev, q);
201     break;
202   }
203   /* submitter will be a null pointer for a scratch */
204   if(submitter)
205     notify_queue(track, submitter);
206   eventlog_raw("queue", queue_marshall(q), (const char *)0);
207   return q;
208 }
209
210 int queue_move(struct queue_entry *q, int delta, const char *who) {
211   int moved = 0;
212   char buffer[20];
213
214   /* not the most efficient approach but hopefuly relatively comprehensible:
215    * the idea is that for each step we determine which nodes are affected, and
216    * fill in all the links starting at the 'prev' end and moving towards the
217    * 'next' end. */
218   
219   while(delta > 0 && q->prev != &qhead) {
220     struct queue_entry *n, *p, *pp;
221
222     n = q->next;
223     p = q->prev;
224     pp = p->prev;
225     pp->next = q;
226     q->prev = pp;
227     q->next = p;
228     p->prev = q;
229     p->next = n;
230     n->prev = p;
231     --delta;
232     ++moved;
233   }
234
235   while(delta < 0 && q->next != &qhead) {
236     struct queue_entry *n, *p, *nn;
237
238     p = q->prev;
239     n = q->next;
240     nn = n->next;
241     p->next = n;
242     n->prev = p;
243     n->next = q;
244     q->prev = n;
245     q->next = nn;
246     nn->prev = q;
247     ++delta;
248     --moved;
249   }
250
251   if(moved) {
252     info("user %s moved %s", who, q->id);
253     notify_queue_move(q->track, who);
254     sprintf(buffer, "%d", moved);
255     eventlog("moved", who, (char *)0);
256   }
257   
258   return delta;
259 }
260
261 static int find_in_list(struct queue_entry *needle,
262                         int nqs, struct queue_entry **qs) {
263   int n;
264
265   for(n = 0; n < nqs; ++n)
266     if(qs[n] == needle)
267       return 1;
268   return 0;
269 }
270
271 void queue_moveafter(struct queue_entry *target,
272                      int nqs, struct queue_entry **qs,
273                      const char *who) {
274   struct queue_entry *q;
275   int n;
276
277   /* Normalize */
278   if(!target)
279     target = &qhead;
280   else
281     while(find_in_list(target, nqs, qs))
282       target = target->prev;
283   /* Do the move */
284   for(n = 0; n < nqs; ++n) {
285     q = qs[n];
286     l_remove(q);
287     l_add(target, q);
288     target = q;
289     /* Log the individual tracks */
290     info("user %s moved %s", who, q->id);
291     notify_queue_move(q->track, who);
292   }
293   /* Report that the queue changed to the event log */
294   eventlog("moved", who, (char *)0);
295 }
296
297 void queue_remove(struct queue_entry *which, const char *who) {
298   if(who) {
299     info("user %s removed %s", who, which->id);
300     notify_queue_move(which->track, who);
301   }
302   eventlog("removed", which->id, who, (const char *)0);
303   l_remove(which);
304 }
305
306 struct queue_entry *queue_find(const char *key) {
307   struct queue_entry *q;
308
309   for(q = qhead.next;
310       q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
311       q = q->next)
312     ;
313   return q != &qhead ? q : 0;
314 }
315
316 void queue_played(struct queue_entry *q) {
317   while(pcount && pcount >= config->history) {
318     eventlog("recent_removed", phead.next->id, (char *)0);
319     l_remove(phead.next);
320     pcount--;
321   }
322   if(config->history) {
323     eventlog_raw("recent_added", queue_marshall(q), (char *)0);
324     l_add(phead.prev, q);
325     ++pcount;
326   }
327 }
328
329 /*
330 Local Variables:
331 c-basic-offset:2
332 comment-column:40
333 fill-column:79
334 indent-tabs-mode:nil
335 End:
336 */