chiark / gitweb /
Add 'adopt' command. This adopts a randomly picked track by changing
[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 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
f08c0859 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
f08c0859
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
f08c0859 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f08c0859 17 */
05b75f8d 18#include "disorder-server.h"
f08c0859
RK
19
20/* the head of the queue is played next, so normally we add to the tail */
2dc2f478
RK
21struct queue_entry qhead = {
22 .next = &qhead,
23 .prev = &qhead
24};
f08c0859
RK
25
26/* the head of the recent list is the oldest thing, the tail the most recently
27 * played */
2dc2f478
RK
28struct queue_entry phead = {
29 .next = &phead,
30 .prev = &phead
31};
f08c0859 32
05b75f8d 33long pcount;
f08c0859
RK
34
35void queue_fix_sofar(struct queue_entry *q) {
36 long sofar;
37
38 /* Fake up SOFAR field for currently-playing tracks that don't have it filled
39 * in by the speaker process. XXX this horrible bodge should go away when we
40 * have a more general implementation of pausing as that field will always
41 * have to be right for the playing track. */
42 if((q->state == playing_started
43 || q->state == playing_paused)
44 && q->type & DISORDER_PLAYER_PAUSES
45 && (q->type & DISORDER_PLAYER_TYPEMASK) != DISORDER_PLAYER_RAW) {
46 if(q->lastpaused) {
47 if(q->uptopause == -1) /* Don't know how far thru. */
48 sofar = -1;
49 else if(q->lastresumed) /* Has been paused and resumed. */
50 sofar = q->uptopause + time(0) - q->lastresumed;
51 else /* Currently paused. */
52 sofar = q->uptopause;
53 } else /* Never been paused. */
54 sofar = time(0) - q->played;
55 q->sofar = sofar;
56 }
57}
58
59static void queue_read_error(const char *msg,
60 void *u) {
61 fatal(0, "error parsing queue %s: %s", (const char *)u, msg);
62}
63
64static void queue_do_read(struct queue_entry *head, const char *path) {
65 char *buffer;
66 FILE *fp;
67 struct queue_entry *q;
af4b13ca 68 int ver = 0;
f08c0859
RK
69
70 if(!(fp = fopen(path, "r"))) {
71 if(errno == ENOENT)
72 return; /* no queue */
73 fatal(errno, "error opening %s", path);
74 }
75 head->next = head->prev = head;
76 while(!inputline(path, fp, &buffer, '\n')) {
d296136c
RK
77 if(buffer[0] == '#') {
78 /* Version indicator */
af4b13ca 79 ver = atoi(buffer + 1);
d296136c
RK
80 continue;
81 }
f08c0859
RK
82 q = xmalloc(sizeof *q);
83 queue_unmarshall(q, buffer, queue_read_error, (void *)path);
af4b13ca 84 if(ver < 1) {
d296136c
RK
85 /* Fix up origin field as best we can; will be wrong in some cases but
86 * hopefully not too horribly so. */
87 q->origin = q->submitter ? origin_picked : origin_random;
6a5964b7
RK
88 /* Eliminated obsolete states, since they are assumed elsewhere not to be
89 * set. */
3867fa20
RK
90 switch(q->state) {
91 case playing_isscratch:
d296136c 92 q->origin = origin_scratch;
6a5964b7 93 q->state = playing_unplayed;
3867fa20
RK
94 break;
95 case playing_random:
96 q->state = playing_unplayed;
97 break;
98 default:
99 break;
100 }
d296136c 101 }
f08c0859
RK
102 if(head == &qhead
103 && (!q->track
104 || !q->when))
105 fatal(0, "incomplete queue entry in %s", path);
05b75f8d 106 queue_insert_entry(head->prev, q);
f08c0859
RK
107 }
108 if(ferror(fp)) fatal(errno, "error reading %s", path);
109 fclose(fp);
110}
111
112void queue_read(void) {
113 queue_do_read(&qhead, config_get_file("queue"));
114}
115
116void recent_read(void) {
117 struct queue_entry *q;
118
119 queue_do_read(&phead, config_get_file("recent"));
120 /* reset pcount after loading */
121 pcount = 0;
122 q = phead.next;
123 while(q != &phead) {
124 ++pcount;
125 q = q->next;
126 }
127}
128
129static void queue_do_write(const struct queue_entry *head, const char *path) {
130 char *tmp;
131 FILE *fp;
132 struct queue_entry *q;
133
134 byte_xasprintf(&tmp, "%s.new", path);
135 if(!(fp = fopen(tmp, "w"))) fatal(errno, "error opening %s", tmp);
d296136c
RK
136 /* Save version indicator */
137 if(fprintf(fp, "#1\n") < 0)
138 fatal(errno, "error writing %s", tmp);
f08c0859
RK
139 for(q = head->next; q != head; q = q->next)
140 if(fprintf(fp, "%s\n", queue_marshall(q)) < 0)
141 fatal(errno, "error writing %s", tmp);
142 if(fclose(fp) < 0) fatal(errno, "error closing %s", tmp);
143 if(rename(tmp, path) < 0) fatal(errno, "error replacing %s", path);
144}
145
146void queue_write(void) {
147 queue_do_write(&qhead, config_get_file("queue"));
148}
149
150void recent_write(void) {
151 queue_do_write(&phead, config_get_file("recent"));
152}
153
f08c0859
RK
154struct queue_entry *queue_find(const char *key) {
155 struct queue_entry *q;
156
157 for(q = qhead.next;
158 q != &qhead && strcmp(q->track, key) && strcmp(q->id, key);
159 q = q->next)
160 ;
161 return q != &qhead ? q : 0;
162}
163
f08c0859
RK
164/*
165Local Variables:
166c-basic-offset:2
167comment-column:40
168fill-column:79
169indent-tabs-mode:nil
170End:
171*/