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