chiark / gitweb /
7421ef8d48945e313e0bf0eb0825a7b44085c2e1
[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  *
34  * Set to 0 if the timings are currently off due to having just unpaused.
35  */
36 time_t last_playing;
37
38 static void queue_completed(void *v,
39                             const char *err,
40                             struct queue_entry *q);
41 static void playing_completed(void *v,
42                               const char *err,
43                               struct queue_entry *q);
44
45 /** @brief Called when either the actual queue or the playing track change */
46 static void queue_playing_changed(void) {
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);
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   }
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);
76 }
77
78 /** @brief Update the queue itself */
79 static void queue_completed(void attribute((unused)) *v,
80                             const char *err,
81                             struct queue_entry *q) {
82   if(err) {
83     popup_protocol_error(0, err);
84     return;
85   }
86   actual_queue = q;
87   queue_playing_changed();
88 }
89
90 /** @brief Update the playing track */
91 static void playing_completed(void attribute((unused)) *v,
92                               const char *err,
93                               struct queue_entry *q) {
94   if(err) {
95     popup_protocol_error(0, err);
96     return;
97   }
98   actual_playing_track = q;
99   queue_playing_changed();
100   xtime(&last_playing);
101 }
102
103 /** @brief Schedule an update to the queue
104  *
105  * Called whenever a track is added to it or removed from it.
106  */
107 static 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);
113 }
114
115 /** @brief Schedule an update to the playing track
116  *
117  * Called whenever it changes
118  */
119 static void playing_changed(const char attribute((unused)) *event,
120                             void  attribute((unused)) *eventdata,
121                             void  attribute((unused)) *callbackdata) {
122   D(("playing_changed"));
123   gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
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;
127   disorder_eclient_playing(client, playing_completed, 0);
128 }
129
130 /** @brief Called regularly
131  *
132  * Updates the played-so-far field
133  */
134 static 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;
139 }
140
141 /** @brief Called at startup */
142 static void queue_init(struct queuelike attribute((unused)) *ql) {
143   /* Arrange a callback whenever the playing state changes */ 
144   event_register("playing-changed", playing_changed, 0);
145   /* We reget both playing track and queue at pause/resume so that start times
146    * can be computed correctly */
147   event_register("pause-changed", playing_changed, 0);
148   event_register("pause-changed", queue_changed, 0);
149   /* Reget the queue whenever it changes */
150   event_register("queue-changed", queue_changed, 0);
151   /* ...and once a second anyway */
152   g_timeout_add(1000/*ms*/, playing_periodic, 0);
153 }
154
155 static void queue_move_completed(void attribute((unused)) *v,
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 */
165 static void queue_drop(struct queuelike attribute((unused)) *ql,
166                        int src, int dst) {
167   struct queue_entry *sq, *dq;
168   int n;
169
170   //fprintf(stderr, "queue_drop %d -> %d\n", src, dst);
171   if(playing_track) {
172     /* If there's a playing track then you can't drag it anywhere  */
173     if(src == 0) {
174       //fprintf(stderr, "cannot drag playing track\n");
175       queue_playing_changed();
176       return;
177     }
178     /* If you try to drop before the playing track we assume you missed and
179      * mean after instead */
180     if(!dst)
181       dst = 1;
182     //fprintf(stderr, "...adjusted to %d -> %d\n\n", src, dst);
183   }
184   /* Find the entry to move */
185   for(n = 0, sq = ql_queue.q; n < src; ++n)
186     sq = sq->next;
187   /*fprintf(stderr, "source=%s (%s)\n",
188           sq->id, sq->track);*/
189   const int after = dst - 1;
190   if(after == -1)
191     dq = 0;
192   else
193     /* Find the entry to insert after */
194     for(n = 0, dq = ql_queue.q; n < after; ++n)
195       dq = dq->next;
196   if(dq == playing_track)
197     dq = 0;
198 #if 0
199   if(dq)
200     fprintf(stderr, "after=%s (%s)\n",
201             dq->id, dq->track);
202   else
203     fprintf(stderr, "after=NULL\n");
204 #endif
205   disorder_eclient_moveafter(client,
206                              dq ? dq->id : "",
207                              1, &sq->id,
208                              queue_move_completed, NULL);
209 }
210
211 /** @brief Columns for the queue */
212 static const struct queue_column queue_columns[] = {
213   { "When",   column_when,     0,        COL_RIGHT },
214   { "Who",    column_who,      0,        0 },
215   { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
216   { "Album",  column_namepart, "album",  COL_EXPAND|COL_ELLIPSIZE },
217   { "Title",  column_namepart, "title",  COL_EXPAND|COL_ELLIPSIZE },
218   { "Length", column_length,   0,        COL_RIGHT }
219 };
220
221 /** @brief Pop-up menu for queue */
222 static struct menuitem queue_menuitems[] = {
223   { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
224   { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
225   { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
226   { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
227   { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
228   { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 },
229 };
230
231 struct queuelike ql_queue = {
232   .name = "queue",
233   .init = queue_init,
234   .columns = queue_columns,
235   .ncolumns = sizeof queue_columns / sizeof *queue_columns,
236   .menuitems = queue_menuitems,
237   .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems,
238   .drop = queue_drop
239 };
240
241 /** @brief Called when a key is pressed in the queue tree view */
242 static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
243                                 GdkEventKey *event,
244                                 gpointer user_data) {
245   /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
246           event->type, event->state, event->keyval);*/
247   switch(event->keyval) {
248   case GDK_BackSpace:
249   case GDK_Delete:
250     if(event->state)
251       break;                            /* Only take unmodified DEL/<-- */
252     ql_remove_activate(0, user_data);
253     return TRUE;                        /* Do not propagate */
254   }
255   return FALSE;                         /* Propagate */
256 }
257
258 GtkWidget *queue_widget(void) {
259   GtkWidget *const w = init_queuelike(&ql_queue);
260
261   /* Catch keypresses */
262   g_signal_connect(ql_queue.view, "key-press-event",
263                    G_CALLBACK(queue_key_press), &ql_queue);
264   return w;
265 }
266
267 /** @brief Return nonzero if @p track is in the queue */
268 int queued(const char *track) {
269   struct queue_entry *q;
270
271   D(("queued %s", track));
272   /* Queue will contain resolved name */
273   track = namepart_resolve(track);
274   for(q = ql_queue.q; q; q = q->next)
275     if(!strcmp(q->track, track))
276       return 1;
277   return 0;
278 }
279
280 /*
281 Local Variables:
282 c-basic-offset:2
283 comment-column:40
284 fill-column:79
285 indent-tabs-mode:nil
286 End:
287 */