chiark / gitweb /
Assume initial digits in a track name are a sort key even without the
[disorder] / server / server-queue.c
CommitLineData
f08c0859
RK
1/*
2 * This file is part of DisOrder.
5ee84006 3 * Copyright (C) 2004-2008 Richard Kettlewell
f08c0859
RK
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 */
05b75f8d 20#include "disorder-server.h"
f08c0859
RK
21
22/* the head of the queue is played next, so normally we add to the tail */
66bb2e02 23struct 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
24
25/* the head of the recent list is the oldest thing, the tail the most recently
26 * played */
66bb2e02 27struct queue_entry phead = { &phead, &phead, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
f08c0859 28
05b75f8d 29long pcount;
f08c0859
RK
30
31void queue_fix_sofar(struct queue_entry *q) {
32 long sofar;
33
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) {
42 if(q->lastpaused) {
43 if(q->uptopause == -1) /* Don't know how far thru. */
44 sofar = -1;
45 else if(q->lastresumed) /* Has been paused and resumed. */
46 sofar = q->uptopause + time(0) - q->lastresumed;
47 else /* Currently paused. */
48 sofar = q->uptopause;
49 } else /* Never been paused. */
50 sofar = time(0) - q->played;
51 q->sofar = sofar;
52 }
53}
54
55static void queue_read_error(const char *msg,
56 void *u) {
57 fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
58}
59
60static void queue_do_read(struct queue_entry *head, const char *path) {
61 char *buffer;
62 FILE *fp;
63 struct queue_entry *q;
64
65 if(!(fp = fopen(path, "r"))) {
66 if(errno == ENOENT)
67 return; /* no queue */
68 fatal(errno, "error opening %s", path);
69 }
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);
74 if(head == &qhead
75 && (!q->track
76 || !q->when))
77 fatal(0, "incomplete queue entry in %s", path);
05b75f8d 78 queue_insert_entry(head->prev, q);
f08c0859
RK
79 }
80 if(ferror(fp)) fatal(errno, "error reading %s", path);
81 fclose(fp);
82}
83
84void queue_read(void) {
85 queue_do_read(&qhead, config_get_file("queue"));
86}
87
88void recent_read(void) {
89 struct queue_entry *q;
90
91 queue_do_read(&phead, config_get_file("recent"));
92 /* reset pcount after loading */
93 pcount = 0;
94 q = phead.next;
95 while(q != &phead) {
96 ++pcount;
97 q = q->next;
98 }
99}
100
101static void queue_do_write(const struct queue_entry *head, const char *path) {
102 char *tmp;
103 FILE *fp;
104 struct queue_entry *q;
105
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);
113}
114
115void queue_write(void) {
116 queue_do_write(&qhead, config_get_file("queue"));
117}
118
119void recent_write(void) {
120 queue_do_write(&phead, config_get_file("recent"));
121}
122
f08c0859
RK
123struct queue_entry *queue_find(const char *key) {
124 struct queue_entry *q;
125
126 for(q = qhead.next;
127 q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
128 q = q->next)
129 ;
130 return q != &qhead ? q : 0;
131}
132
f08c0859
RK
133/*
134Local Variables:
135c-basic-offset:2
136comment-column:40
137fill-column:79
138indent-tabs-mode:nil
139End:
140*/