chiark / gitweb /
Support DEB_BUILD_OPTIONS=parallel=N
[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
c133bd3c
RK
32/** @brief When we last got the playing track */
33time_t last_playing;
460b9539 34
83fb99f9 35static void queue_completed(void *v,
e2717131 36 const char *err,
83fb99f9
RK
37 struct queue_entry *q);
38static void playing_completed(void *v,
e2717131 39 const char *err,
83fb99f9
RK
40 struct queue_entry *q);
41
c133bd3c
RK
42/** @brief Called when either the actual queue or the playing track change */
43static void queue_playing_changed(void) {
f69359f8
RK
44 const char *old_id = playing_track ? playing_track->id : 0;
45
83fb99f9
RK
46 /* Check that the playing track isn't in the queue. There's a race here due
47 * to the fact that we issue the two commands at slightly different times.
48 * If it goes wrong we re-issue and try again, so that we never offer up an
49 * inconsistent state. */
50 if(actual_playing_track) {
51 struct queue_entry *q;
52 for(q = actual_queue; q; q = q->next)
53 if(!strcmp(q->id, actual_playing_track->id))
54 break;
55 if(q) {
56 disorder_eclient_playing(client, playing_completed, 0);
57 disorder_eclient_queue(client, queue_completed, 0);
58 return;
59 }
60 }
61
62 struct queue_entry *q = xmalloc(sizeof *q);
ee7552f8
RK
63 if(actual_playing_track) {
64 *q = *actual_playing_track;
65 q->next = actual_queue;
66 playing_track = q;
67 } else {
68 playing_track = NULL;
69 q = actual_queue;
70 }
f69359f8
RK
71 if(!old_id || !playing_track || strcmp(old_id, playing_track->id))
72 time(&last_playing); /* for column_length() */
c133bd3c
RK
73 ql_new_queue(&ql_queue, q);
74 /* Tell anyone who cares */
75 event_raise("queue-list-changed", q);
76 event_raise("playing-track-changed", q);
460b9539 77}
78
c133bd3c
RK
79/** @brief Update the queue itself */
80static void queue_completed(void attribute((unused)) *v,
abf99697 81 const char *err,
c133bd3c 82 struct queue_entry *q) {
abf99697
RK
83 if(err) {
84 popup_protocol_error(0, err);
c133bd3c 85 return;
3035257f 86 }
c133bd3c
RK
87 actual_queue = q;
88 queue_playing_changed();
460b9539 89}
90
c133bd3c 91/** @brief Update the playing track */
460b9539 92static void playing_completed(void attribute((unused)) *v,
abf99697 93 const char *err,
460b9539 94 struct queue_entry *q) {
abf99697
RK
95 if(err) {
96 popup_protocol_error(0, err);
c133bd3c 97 return;
460b9539 98 }
c133bd3c
RK
99 actual_playing_track = q;
100 queue_playing_changed();
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
RK
123 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
124 disorder_eclient_playing(client, playing_completed, 0);
125}
126
c133bd3c 127/** @brief Called regularly
717ba987 128 *
c133bd3c 129 * Updates the played-so-far field
717ba987 130 */
c133bd3c
RK
131static gboolean playing_periodic(gpointer attribute((unused)) data) {
132 /* If there's a track playing, update its row */
133 if(playing_track)
134 ql_update_row(playing_track, 0);
135 return TRUE;
460b9539 136}
137
c133bd3c
RK
138/** @brief Called at startup */
139static void queue_init(void) {
140 /* Arrange a callback whenever the playing state changes */
141 event_register("playing-changed", playing_changed, 0);
15837f6a
RK
142 /* We reget both playing track and queue at pause/resume so that start times
143 * can be computed correctly */
c133bd3c 144 event_register("pause-changed", playing_changed, 0);
15837f6a
RK
145 event_register("pause-changed", queue_changed, 0);
146 /* Reget the queue whenever it changes */
c133bd3c
RK
147 event_register("queue-changed", queue_changed, 0);
148 /* ...and once a second anyway */
149 g_timeout_add(1000/*ms*/, playing_periodic, 0);
460b9539 150}
151
c133bd3c
RK
152/** @brief Columns for the queue */
153static const struct queue_column queue_columns[] = {
b0b15b7c 154 { "When", column_when, 0, COL_RIGHT },
c133bd3c 155 { "Who", column_who, 0, 0 },
b0b15b7c
RK
156 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
157 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
158 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
159 { "Length", column_length, 0, COL_RIGHT }
460b9539 160};
161
c133bd3c 162/** @brief Pop-up menu for queue */
6982880f 163static struct menuitem queue_menuitems[] = {
c133bd3c
RK
164 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
165 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
166 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
c9fb35f4 167 { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
c133bd3c 168 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
a49c2c0f 169 { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 },
4eb1f430
RK
170};
171
c133bd3c 172struct queuelike ql_queue = {
ee7552f8 173 .name = "queue",
c133bd3c
RK
174 .init = queue_init,
175 .columns = queue_columns,
176 .ncolumns = sizeof queue_columns / sizeof *queue_columns,
177 .menuitems = queue_menuitems,
83fb99f9 178 .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems
460b9539 179};
180
1b5f38c4
RK
181/* Drag and drop has to be figured out experimentally, because it is not well
182 * documented.
183 *
184 * First you get a row-inserted. The path argument points to the destination
185 * row but this will not yet have had its values set. The source row is still
186 * present. AFAICT the iter argument points to the same place.
187 *
188 * Then you get a row-deleted. The path argument identifies the row that was
189 * deleted. By this stage the row inserted above has acquired its values.
190 *
191 * A complication is that the deletion will move the inserted row. For
192 * instance, if you do a drag that moves row 1 down to after the track that was
193 * formerly on row 9, in the row-inserted call it will show up as row 10, but
194 * in the row-deleted call, row 1 will have been deleted thus making the
195 * inserted row be row 9.
196 *
197 * So when we see the row-inserted we have no idea what track to move.
198 * Therefore we stash it until we see a row-deleted.
199 */
200
201/** @brief Target row for drag */
202static int queue_drag_target = -1;
203
204static void queue_move_completed(void attribute((unused)) *v,
abf99697
RK
205 const char *err) {
206 if(err) {
207 popup_protocol_error(0, err);
1b5f38c4
RK
208 return;
209 }
210 /* The log should tell us the queue changed so we do no more here */
211}
212
213static void queue_row_deleted(GtkTreeModel *treemodel,
214 GtkTreePath *path,
215 gpointer attribute((unused)) user_data) {
216 if(!suppress_actions) {
217#if 0
218 char *ps = gtk_tree_path_to_string(path);
219 fprintf(stderr, "row-deleted path=%s queue_drag_target=%d\n",
220 ps, queue_drag_target);
221 GtkTreeIter j[1];
222 gboolean jt = gtk_tree_model_get_iter_first(treemodel, j);
223 int row = 0;
224 while(jt) {
225 struct queue_entry *q = ql_iter_to_q(treemodel, j);
226 fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)");
227 jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_queue.store), j);
228 }
229 g_free(ps);
230#endif
231 if(queue_drag_target < 0) {
232 error(0, "unsuppressed row-deleted with no row-inserted");
233 return;
234 }
235 int drag_source = gtk_tree_path_get_indices(path)[0];
236
237 /* If the drag is downwards (=towards higher row numbers) then the target
238 * will have been moved upwards (=towards lower row numbers) by one row. */
239 if(drag_source < queue_drag_target)
240 --queue_drag_target;
241
242 /* Find the track to move */
243 GtkTreeIter src[1];
244 gboolean srcv = gtk_tree_model_iter_nth_child(treemodel, src, NULL,
245 queue_drag_target);
246 if(!srcv) {
247 error(0, "cannot get iterator to drag target %d", queue_drag_target);
248 queue_playing_changed();
249 queue_drag_target = -1;
250 return;
251 }
252 struct queue_entry *srcq = ql_iter_to_q(treemodel, src);
253 assert(srcq);
254 //fprintf(stderr, "move %s %s\n", srcq->id, srcq->track);
255
256 /* Don't allow the currently playing track to be moved. As above, we put
257 * the queue back into the right order straight away. */
258 if(srcq == playing_track) {
259 //fprintf(stderr, "cannot move currently playing track\n");
260 queue_playing_changed();
261 queue_drag_target = -1;
262 return;
263 }
264
265 /* Find the destination */
266 struct queue_entry *dstq;
267 if(queue_drag_target) {
268 GtkTreeIter dst[1];
269 gboolean dstv = gtk_tree_model_iter_nth_child(treemodel, dst, NULL,
270 queue_drag_target - 1);
271 if(!dstv) {
272 error(0, "cannot get iterator to drag target predecessor %d",
273 queue_drag_target - 1);
274 queue_playing_changed();
275 queue_drag_target = -1;
276 return;
277 }
278 dstq = ql_iter_to_q(treemodel, dst);
279 assert(dstq);
280 if(dstq == playing_track)
281 dstq = 0;
282 } else
283 dstq = 0;
284 /* NB if the user attempts to move a queued track before the currently
285 * playing track we assume they just missed a bit, and put it after. */
286 //fprintf(stderr, " target %s %s\n", dstq ? dstq->id : "(none)", dstq ? dstq->track : "(none)");
287 /* Now we know what is to be moved. We need to know the preceding queue
288 * entry so we can move it. */
289 disorder_eclient_moveafter(client,
290 dstq ? dstq->id : "",
291 1, &srcq->id,
292 queue_move_completed, NULL);
293 queue_drag_target = -1;
294 }
295}
296
297static void queue_row_inserted(GtkTreeModel attribute((unused)) *treemodel,
298 GtkTreePath *path,
299 GtkTreeIter attribute((unused)) *iter,
300 gpointer attribute((unused)) user_data) {
301 if(!suppress_actions) {
302#if 0
303 char *ps = gtk_tree_path_to_string(path);
304 GtkTreeIter piter[1];
305 gboolean pi = gtk_tree_model_get_iter(treemodel, piter, path);
306 struct queue_entry *pq = pi ? ql_iter_to_q(treemodel, piter) : 0;
307 struct queue_entry *iq = ql_iter_to_q(treemodel, iter);
308
309 fprintf(stderr, "row-inserted path=%s pi=%d pq=%p path=%s iq=%p iter=%s\n",
310 ps,
311 pi,
312 pq,
313 (pi
314 ? (pq ? pq->track : "(pq=0)")
315 : "(pi=FALSE)"),
316 iq,
317 iq ? iq->track : "(iq=0)");
318
319 GtkTreeIter j[1];
320 gboolean jt = gtk_tree_model_get_iter_first(treemodel, j);
321 int row = 0;
322 while(jt) {
323 struct queue_entry *q = ql_iter_to_q(treemodel, j);
324 fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)");
325 jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_queue.store), j);
326 }
327 g_free(ps);
328#endif
329 queue_drag_target = gtk_tree_path_get_indices(path)[0];
330 }
331}
332
c10dd9af
RK
333/** @brief Called when a key is pressed in the queue tree view */
334static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
335 GdkEventKey *event,
336 gpointer user_data) {
337 /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
338 event->type, event->state, event->keyval);*/
339 switch(event->keyval) {
340 case GDK_BackSpace:
341 case GDK_Delete:
342 if(event->state)
343 break; /* Only take unmodified DEL/<-- */
344 ql_remove_activate(0, user_data);
345 return TRUE; /* Do not propagate */
346 }
347 return FALSE; /* Propagate */
348}
349
c133bd3c 350GtkWidget *queue_widget(void) {
1b5f38c4
RK
351 GtkWidget *const w = init_queuelike(&ql_queue);
352
353 /* Enable drag+drop */
354 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql_queue.view), TRUE);
355 g_signal_connect(ql_queue.store,
356 "row-inserted",
357 G_CALLBACK(queue_row_inserted), &ql_queue);
358 g_signal_connect(ql_queue.store,
359 "row-deleted",
360 G_CALLBACK(queue_row_deleted), &ql_queue);
c10dd9af
RK
361 /* Catch keypresses */
362 g_signal_connect(ql_queue.view, "key-press-event",
363 G_CALLBACK(queue_key_press), &ql_queue);
1b5f38c4 364 return w;
c133bd3c 365}
460b9539 366
717ba987 367/** @brief Return nonzero if @p track is in the queue */
460b9539 368int queued(const char *track) {
369 struct queue_entry *q;
370
371 D(("queued %s", track));
68110302
RK
372 /* Queue will contain resolved name */
373 track = namepart_resolve(track);
460b9539 374 for(q = ql_queue.q; q; q = q->next)
375 if(!strcmp(q->track, track))
376 return 1;
377 return 0;
378}
379
380/*
381Local Variables:
382c-basic-offset:2
383comment-column:40
384fill-column:79
385indent-tabs-mode:nil
386End:
387*/