chiark / gitweb /
Disobedience updates last_playing whenever a 'playing' command
[disorder] / disobedience / queue.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2006-2008 Richard Kettlewell
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 3 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,
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  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file disobedience/queue.c
19  * @brief Disobedience queue widget
20  */
21 #include "disobedience.h"
22 #include "popup.h"
23 #include "queue-generic.h"
24
25 /** @brief The actual queue */
26 static struct queue_entry *actual_queue;
27 static struct queue_entry *actual_playing_track;
28
29 /** @brief The playing track */
30 struct queue_entry *playing_track;
31
32 /** @brief When we last got the playing track */
33 time_t last_playing;
34
35 static void queue_completed(void *v,
36                             const char *err,
37                             struct queue_entry *q);
38 static void playing_completed(void *v,
39                               const char *err,
40                               struct queue_entry *q);
41
42 /** @brief Called when either the actual queue or the playing track change */
43 static void queue_playing_changed(void) {
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);
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   }
69   ql_new_queue(&ql_queue, q);
70   /* Tell anyone who cares */
71   event_raise("queue-list-changed", q);
72   event_raise("playing-track-changed", q);
73 }
74
75 /** @brief Update the queue itself */
76 static void queue_completed(void attribute((unused)) *v,
77                             const char *err,
78                             struct queue_entry *q) {
79   if(err) {
80     popup_protocol_error(0, err);
81     return;
82   }
83   actual_queue = q;
84   queue_playing_changed();
85 }
86
87 /** @brief Update the playing track */
88 static void playing_completed(void attribute((unused)) *v,
89                               const char *err,
90                               struct queue_entry *q) {
91   if(err) {
92     popup_protocol_error(0, err);
93     return;
94   }
95   actual_playing_track = q;
96   queue_playing_changed();
97   time(&last_playing);
98 }
99
100 /** @brief Schedule an update to the queue
101  *
102  * Called whenever a track is added to it or removed from it.
103  */
104 static 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);
110 }
111
112 /** @brief Schedule an update to the playing track
113  *
114  * Called whenever it changes
115  */
116 static void playing_changed(const char attribute((unused)) *event,
117                             void  attribute((unused)) *eventdata,
118                             void  attribute((unused)) *callbackdata) {
119   D(("playing_changed"));
120   gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
121   disorder_eclient_playing(client, playing_completed, 0);
122 }
123
124 /** @brief Called regularly
125  *
126  * Updates the played-so-far field
127  */
128 static 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;
133 }
134
135 /** @brief Called at startup */
136 static void queue_init(void) {
137   /* Arrange a callback whenever the playing state changes */ 
138   event_register("playing-changed", playing_changed, 0);
139   /* We reget both playing track and queue at pause/resume so that start times
140    * can be computed correctly */
141   event_register("pause-changed", playing_changed, 0);
142   event_register("pause-changed", queue_changed, 0);
143   /* Reget the queue whenever it changes */
144   event_register("queue-changed", queue_changed, 0);
145   /* ...and once a second anyway */
146   g_timeout_add(1000/*ms*/, playing_periodic, 0);
147 }
148
149 /** @brief Columns for the queue */
150 static const struct queue_column queue_columns[] = {
151   { "When",   column_when,     0,        COL_RIGHT },
152   { "Who",    column_who,      0,        0 },
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 }
157 };
158
159 /** @brief Pop-up menu for queue */
160 static struct menuitem queue_menuitems[] = {
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 },
164   { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
165   { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
166   { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 },
167 };
168
169 struct queuelike ql_queue = {
170   .name = "queue",
171   .init = queue_init,
172   .columns = queue_columns,
173   .ncolumns = sizeof queue_columns / sizeof *queue_columns,
174   .menuitems = queue_menuitems,
175   .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems
176 };
177
178 /* Drag and drop has to be figured out experimentally, because it is not well
179  * documented.
180  *
181  * First you get a row-inserted.  The path argument points to the destination
182  * row but this will not yet have had its values set.  The source row is still
183  * present.  AFAICT the iter argument points to the same place.
184  *
185  * Then you get a row-deleted.  The path argument identifies the row that was
186  * deleted.  By this stage the row inserted above has acquired its values.
187  *
188  * A complication is that the deletion will move the inserted row.  For
189  * instance, if you do a drag that moves row 1 down to after the track that was
190  * formerly on row 9, in the row-inserted call it will show up as row 10, but
191  * in the row-deleted call, row 1 will have been deleted thus making the
192  * inserted row be row 9.
193  *
194  * So when we see the row-inserted we have no idea what track to move.
195  * Therefore we stash it until we see a row-deleted.
196  */
197
198 /** @brief Target row for drag */
199 static int queue_drag_target = -1;
200
201 static void queue_move_completed(void attribute((unused)) *v,
202                                  const char *err) {
203   if(err) {
204     popup_protocol_error(0, err);
205     return;
206   }
207   /* The log should tell us the queue changed so we do no more here */
208 }
209
210 static void queue_row_deleted(GtkTreeModel *treemodel,
211                               GtkTreePath *path,
212                               gpointer attribute((unused)) user_data) {
213   if(!suppress_actions) {
214 #if 0
215     char *ps = gtk_tree_path_to_string(path);
216     fprintf(stderr, "row-deleted path=%s queue_drag_target=%d\n",
217             ps, queue_drag_target);
218     GtkTreeIter j[1];
219     gboolean jt = gtk_tree_model_get_iter_first(treemodel, j);
220     int row = 0;
221     while(jt) {
222       struct queue_entry *q = ql_iter_to_q(treemodel, j);
223       fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)");
224       jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_queue.store), j);
225     }
226     g_free(ps);
227 #endif
228     if(queue_drag_target < 0) {
229       error(0, "unsuppressed row-deleted with no row-inserted");
230       return;
231     }
232     int drag_source = gtk_tree_path_get_indices(path)[0];
233
234     /* If the drag is downwards (=towards higher row numbers) then the target
235      * will have been moved upwards (=towards lower row numbers) by one row. */
236     if(drag_source < queue_drag_target)
237       --queue_drag_target;
238     
239     /* Find the track to move */
240     GtkTreeIter src[1];
241     gboolean srcv = gtk_tree_model_iter_nth_child(treemodel, src, NULL,
242                                                   queue_drag_target);
243     if(!srcv) {
244       error(0, "cannot get iterator to drag target %d", queue_drag_target);
245       queue_playing_changed();
246       queue_drag_target = -1;
247       return;
248     }
249     struct queue_entry *srcq = ql_iter_to_q(treemodel, src);
250     assert(srcq);
251     //fprintf(stderr, "move %s %s\n", srcq->id, srcq->track);
252     
253     /* Don't allow the currently playing track to be moved.  As above, we put
254      * the queue back into the right order straight away. */
255     if(srcq == playing_track) {
256       //fprintf(stderr, "cannot move currently playing track\n");
257       queue_playing_changed();
258       queue_drag_target = -1;
259       return;
260     }
261
262     /* Find the destination */
263     struct queue_entry *dstq;
264     if(queue_drag_target) {
265       GtkTreeIter dst[1];
266       gboolean dstv = gtk_tree_model_iter_nth_child(treemodel, dst, NULL,
267                                                     queue_drag_target - 1);
268       if(!dstv) {
269         error(0, "cannot get iterator to drag target predecessor %d",
270               queue_drag_target - 1);
271         queue_playing_changed();
272         queue_drag_target = -1;
273         return;
274       }
275       dstq = ql_iter_to_q(treemodel, dst);
276       assert(dstq);
277       if(dstq == playing_track)
278         dstq = 0;
279     } else
280       dstq = 0;
281     /* NB if the user attempts to move a queued track before the currently
282      * playing track we assume they just missed a bit, and put it after. */
283     //fprintf(stderr, " target %s %s\n", dstq ? dstq->id : "(none)", dstq ? dstq->track : "(none)");
284     /* Now we know what is to be moved.  We need to know the preceding queue
285      * entry so we can move it. */
286     disorder_eclient_moveafter(client,
287                                dstq ? dstq->id : "",
288                                1, &srcq->id,
289                                queue_move_completed, NULL);
290     queue_drag_target = -1;
291   }
292 }
293
294 static void queue_row_inserted(GtkTreeModel attribute((unused)) *treemodel,
295                                GtkTreePath *path,
296                                GtkTreeIter attribute((unused)) *iter,
297                                gpointer attribute((unused)) user_data) {
298   if(!suppress_actions) {
299 #if 0
300     char *ps = gtk_tree_path_to_string(path);
301     GtkTreeIter piter[1];
302     gboolean pi = gtk_tree_model_get_iter(treemodel, piter, path);
303     struct queue_entry *pq = pi ? ql_iter_to_q(treemodel, piter) : 0;
304     struct queue_entry *iq = ql_iter_to_q(treemodel, iter);
305
306     fprintf(stderr, "row-inserted path=%s pi=%d pq=%p path=%s iq=%p iter=%s\n",
307             ps,
308             pi,
309             pq,
310             (pi
311              ? (pq ? pq->track : "(pq=0)")
312              : "(pi=FALSE)"),
313             iq,
314             iq ? iq->track : "(iq=0)");
315
316     GtkTreeIter j[1];
317     gboolean jt = gtk_tree_model_get_iter_first(treemodel, j);
318     int row = 0;
319     while(jt) {
320       struct queue_entry *q = ql_iter_to_q(treemodel, j);
321       fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)");
322       jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_queue.store), j);
323     }
324     g_free(ps);
325 #endif
326     queue_drag_target = gtk_tree_path_get_indices(path)[0];
327   }
328 }
329
330 /** @brief Called when a key is pressed in the queue tree view */
331 static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
332                                 GdkEventKey *event,
333                                 gpointer user_data) {
334   /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
335           event->type, event->state, event->keyval);*/
336   switch(event->keyval) {
337   case GDK_BackSpace:
338   case GDK_Delete:
339     if(event->state)
340       break;                            /* Only take unmodified DEL/<-- */
341     ql_remove_activate(0, user_data);
342     return TRUE;                        /* Do not propagate */
343   }
344   return FALSE;                         /* Propagate */
345 }
346
347 GtkWidget *queue_widget(void) {
348   GtkWidget *const w = init_queuelike(&ql_queue);
349
350   /* Enable drag+drop */
351   gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql_queue.view), TRUE);
352   g_signal_connect(ql_queue.store,
353                    "row-inserted",
354                    G_CALLBACK(queue_row_inserted), &ql_queue);
355   g_signal_connect(ql_queue.store,
356                    "row-deleted",
357                    G_CALLBACK(queue_row_deleted), &ql_queue);
358   /* Catch keypresses */
359   g_signal_connect(ql_queue.view, "key-press-event",
360                    G_CALLBACK(queue_key_press), &ql_queue);
361   return w;
362 }
363
364 /** @brief Return nonzero if @p track is in the queue */
365 int queued(const char *track) {
366   struct queue_entry *q;
367
368   D(("queued %s", track));
369   /* Queue will contain resolved name */
370   track = namepart_resolve(track);
371   for(q = ql_queue.q; q; q = q->next)
372     if(!strcmp(q->track, track))
373       return 1;
374   return 0;
375 }
376
377 /*
378 Local Variables:
379 c-basic-offset:2
380 comment-column:40
381 fill-column:79
382 indent-tabs-mode:nil
383 End:
384 */