chiark / gitweb /
Trivial resampler fixes
[disorder] / disobedience / queue.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
fb009628 3 * Copyright (C) 2006-2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 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
460b9539 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 *
460b9539 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/>.
460b9539 17 */
132a5a4a
RK
18/** @file disobedience/queue.c
19 * @brief Disobedience queue widget
20 */
717ba987 21#include "disobedience.h"
6982880f 22#include "popup.h"
c133bd3c 23#include "queue-generic.h"
460b9539 24
c133bd3c
RK
25/** @brief The actual queue */
26static struct queue_entry *actual_queue;
27static struct queue_entry *actual_playing_track;
460b9539 28
c133bd3c
RK
29/** @brief The playing track */
30struct queue_entry *playing_track;
4eb1f430 31
3900d6d6
RK
32/** @brief When we last got the playing track
33 *
34 * Set to 0 if the timings are currently off due to having just unpaused.
35 */
c133bd3c 36time_t last_playing;
460b9539 37
83fb99f9 38static void queue_completed(void *v,
e2717131 39 const char *err,
83fb99f9
RK
40 struct queue_entry *q);
41static void playing_completed(void *v,
e2717131 42 const char *err,
83fb99f9
RK
43 struct queue_entry *q);
44
c133bd3c
RK
45/** @brief Called when either the actual queue or the playing track change */
46static void queue_playing_changed(void) {
83fb99f9
RK
47 /* Check that the playing track isn't in the queue. There's a race here due
48 * to the fact that we issue the two commands at slightly different times.
49 * If it goes wrong we re-issue and try again, so that we never offer up an
50 * inconsistent state. */
51 if(actual_playing_track) {
52 struct queue_entry *q;
53 for(q = actual_queue; q; q = q->next)
54 if(!strcmp(q->id, actual_playing_track->id))
55 break;
56 if(q) {
57 disorder_eclient_playing(client, playing_completed, 0);
58 disorder_eclient_queue(client, queue_completed, 0);
59 return;
60 }
61 }
62
63 struct queue_entry *q = xmalloc(sizeof *q);
ee7552f8
RK
64 if(actual_playing_track) {
65 *q = *actual_playing_track;
66 q->next = actual_queue;
67 playing_track = q;
68 } else {
69 playing_track = NULL;
70 q = actual_queue;
71 }
c133bd3c
RK
72 ql_new_queue(&ql_queue, q);
73 /* Tell anyone who cares */
74 event_raise("queue-list-changed", q);
75 event_raise("playing-track-changed", q);
460b9539 76}
77
c133bd3c
RK
78/** @brief Update the queue itself */
79static void queue_completed(void attribute((unused)) *v,
abf99697 80 const char *err,
c133bd3c 81 struct queue_entry *q) {
abf99697
RK
82 if(err) {
83 popup_protocol_error(0, err);
c133bd3c 84 return;
3035257f 85 }
c133bd3c
RK
86 actual_queue = q;
87 queue_playing_changed();
460b9539 88}
89
c133bd3c 90/** @brief Update the playing track */
460b9539 91static void playing_completed(void attribute((unused)) *v,
abf99697 92 const char *err,
460b9539 93 struct queue_entry *q) {
abf99697
RK
94 if(err) {
95 popup_protocol_error(0, err);
c133bd3c 96 return;
460b9539 97 }
c133bd3c
RK
98 actual_playing_track = q;
99 queue_playing_changed();
4265e5d3 100 xtime(&last_playing);
460b9539 101}
102
c133bd3c 103/** @brief Schedule an update to the queue
717ba987 104 *
c133bd3c
RK
105 * Called whenever a track is added to it or removed from it.
106 */
107static void queue_changed(const char attribute((unused)) *event,
108 void attribute((unused)) *eventdata,
109 void attribute((unused)) *callbackdata) {
110 D(("queue_changed"));
111 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
112 disorder_eclient_queue(client, queue_completed, 0);
460b9539 113}
114
c133bd3c 115/** @brief Schedule an update to the playing track
717ba987 116 *
c133bd3c 117 * Called whenever it changes
3ffa2d15 118 */
a8cd6f84 119static void playing_changed(const char attribute((unused)) *event,
c133bd3c
RK
120 void attribute((unused)) *eventdata,
121 void attribute((unused)) *callbackdata) {
a8cd6f84 122 D(("playing_changed"));
3ffa2d15 123 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
3900d6d6
RK
124 /* Setting last_playing=0 means that we don't know what the correct value
125 * is right now, e.g. because things have been deranged by a pause. */
126 last_playing = 0;
3ffa2d15
RK
127 disorder_eclient_playing(client, playing_completed, 0);
128}
129
c133bd3c 130/** @brief Called regularly
717ba987 131 *
c133bd3c 132 * Updates the played-so-far field
717ba987 133 */
c133bd3c
RK
134static gboolean playing_periodic(gpointer attribute((unused)) data) {
135 /* If there's a track playing, update its row */
136 if(playing_track)
137 ql_update_row(playing_track, 0);
138 return TRUE;
460b9539 139}
140
c133bd3c 141/** @brief Called at startup */
a8c0e917 142static void queue_init(struct queuelike attribute((unused)) *ql) {
c133bd3c
RK
143 /* Arrange a callback whenever the playing state changes */
144 event_register("playing-changed", playing_changed, 0);
15837f6a
RK
145 /* We reget both playing track and queue at pause/resume so that start times
146 * can be computed correctly */
c133bd3c 147 event_register("pause-changed", playing_changed, 0);
15837f6a
RK
148 event_register("pause-changed", queue_changed, 0);
149 /* Reget the queue whenever it changes */
c133bd3c
RK
150 event_register("queue-changed", queue_changed, 0);
151 /* ...and once a second anyway */
152 g_timeout_add(1000/*ms*/, playing_periodic, 0);
460b9539 153}
154
4b51f265 155static void queue_drop_completed(void attribute((unused)) *v,
82db9336
RK
156 const char *err) {
157 if(err) {
158 popup_protocol_error(0, err);
159 return;
160 }
161 /* The log should tell us the queue changed so we do no more here */
162}
163
164/** @brief Called when drag+drop completes */
a8c0e917 165static void queue_drop(struct queuelike attribute((unused)) *ql,
0beb320e 166 int ntracks,
4b51f265 167 char **tracks, char **ids,
0beb320e 168 struct queue_entry *after_me) {
82db9336 169 int n;
4b51f265
RK
170
171 if(ids) {
172 /* Rearrangement */
173 if(playing_track) {
174 /* If there's a playing track then you can't drag it anywhere */
175 for(n = 0; n < ntracks; ++n) {
176 if(!strcmp(playing_track->id, ids[n])) {
177 fprintf(stderr, "cannot drag playing track\n");
178 return;
179 }
0beb320e 180 }
4b51f265
RK
181 /* You can't tell the server to drag after the playing track by ID, you
182 * have to send "". */
183 if(after_me == playing_track)
184 after_me = NULL;
185 /* If you try to drag before the playing track (i.e. after_me=NULL on
186 * input) then the effect is just to drag after it, although there's no
187 * longer code to explicitly implement this. */
82db9336 188 }
4b51f265
RK
189 /* Tell the server to move them. The log will tell us about the change (if
190 * indeed it succeeds!), so no need to rearrange the model now. */
191 disorder_eclient_moveafter(client,
192 after_me ? after_me->id : "",
193 ntracks, (const char **)ids,
194 queue_drop_completed, NULL);
195 } else {
196 /* You can't tell the server to insert after the playing track by ID, you
0beb320e
RK
197 * have to send "". */
198 if(after_me == playing_track)
199 after_me = NULL;
4b51f265
RK
200 /* Play the tracks */
201 disorder_eclient_playafter(client,
202 after_me ? after_me->id : "",
203 ntracks, (const char **)tracks,
204 queue_drop_completed, NULL);
82db9336 205 }
82db9336
RK
206}
207
c133bd3c
RK
208/** @brief Columns for the queue */
209static const struct queue_column queue_columns[] = {
b0b15b7c 210 { "When", column_when, 0, COL_RIGHT },
c133bd3c 211 { "Who", column_who, 0, 0 },
b0b15b7c
RK
212 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
213 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
214 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
215 { "Length", column_length, 0, COL_RIGHT }
460b9539 216};
217
c133bd3c 218/** @brief Pop-up menu for queue */
6982880f 219static struct menuitem queue_menuitems[] = {
c133bd3c
RK
220 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
221 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
222 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
c9fb35f4 223 { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
c133bd3c 224 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
a49c2c0f 225 { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 },
4eb1f430
RK
226};
227
c133bd3c 228struct queuelike ql_queue = {
ee7552f8 229 .name = "queue",
c133bd3c
RK
230 .init = queue_init,
231 .columns = queue_columns,
232 .ncolumns = sizeof queue_columns / sizeof *queue_columns,
233 .menuitems = queue_menuitems,
82db9336
RK
234 .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems,
235 .drop = queue_drop
460b9539 236};
237
c10dd9af
RK
238/** @brief Called when a key is pressed in the queue tree view */
239static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
240 GdkEventKey *event,
241 gpointer user_data) {
242 /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
243 event->type, event->state, event->keyval);*/
244 switch(event->keyval) {
245 case GDK_BackSpace:
246 case GDK_Delete:
247 if(event->state)
248 break; /* Only take unmodified DEL/<-- */
249 ql_remove_activate(0, user_data);
250 return TRUE; /* Do not propagate */
251 }
252 return FALSE; /* Propagate */
253}
254
c133bd3c 255GtkWidget *queue_widget(void) {
1b5f38c4
RK
256 GtkWidget *const w = init_queuelike(&ql_queue);
257
c10dd9af
RK
258 /* Catch keypresses */
259 g_signal_connect(ql_queue.view, "key-press-event",
260 G_CALLBACK(queue_key_press), &ql_queue);
1b5f38c4 261 return w;
c133bd3c 262}
460b9539 263
717ba987 264/** @brief Return nonzero if @p track is in the queue */
460b9539 265int queued(const char *track) {
266 struct queue_entry *q;
267
268 D(("queued %s", track));
68110302
RK
269 /* Queue will contain resolved name */
270 track = namepart_resolve(track);
460b9539 271 for(q = ql_queue.q; q; q = q->next)
272 if(!strcmp(q->track, track))
273 return 1;
274 return 0;
275}
276
277/*
278Local Variables:
279c-basic-offset:2
280comment-column:40
281fill-column:79
282indent-tabs-mode:nil
283End:
284*/