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