Commit | Line | Data |
---|---|---|
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 */ |
26 | static struct queue_entry *actual_queue; | |
27 | static struct queue_entry *actual_playing_track; | |
460b9539 | 28 | |
c133bd3c RK |
29 | /** @brief The playing track */ |
30 | struct 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 | 36 | time_t last_playing; |
460b9539 | 37 | |
83fb99f9 | 38 | static void queue_completed(void *v, |
e2717131 | 39 | const char *err, |
83fb99f9 RK |
40 | struct queue_entry *q); |
41 | static 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 */ |
46 | static 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); | |
68be70c0 | 58 | disorder_eclient_queue(client, queue_completed, 0); |
83fb99f9 RK |
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); | |
68be70c0 | 75 | event_raise("playing-track-changed", playing_track); |
460b9539 | 76 | } |
77 | ||
c133bd3c RK |
78 | /** @brief Update the queue itself */ |
79 | static 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 | } |
68be70c0 | 86 | actual_queue = q; |
c133bd3c | 87 | queue_playing_changed(); |
460b9539 | 88 | } |
89 | ||
c133bd3c | 90 | /** @brief Update the playing track */ |
460b9539 | 91 | static 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 | */ | |
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); | |
460b9539 | 113 | } |
114 | ||
c133bd3c | 115 | /** @brief Schedule an update to the playing track |
717ba987 | 116 | * |
c133bd3c | 117 | * Called whenever it changes |
3ffa2d15 | 118 | */ |
a8cd6f84 | 119 | static 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 |
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); | |
e03df0a5 RK |
138 | /* If the first (nonplaying) track starts in the past, update the queue to |
139 | * get new expected start times; but rate limit this checking. (If we only | |
140 | * do it once a minute then the rest of the queue can get out of date too | |
141 | * easily.) */ | |
142 | struct queue_entry *q = ql_queue.q; | |
143 | if(q) { | |
144 | if(q == playing_track) | |
145 | q = q->next; | |
146 | if(q) { | |
147 | time_t now; | |
148 | time(&now); | |
149 | if(q->expected / 15 < now / 15) | |
150 | queue_changed(0,0,0); | |
151 | } | |
152 | } | |
c133bd3c | 153 | return TRUE; |
460b9539 | 154 | } |
155 | ||
c133bd3c | 156 | /** @brief Called at startup */ |
a8c0e917 | 157 | static void queue_init(struct queuelike attribute((unused)) *ql) { |
c133bd3c RK |
158 | /* Arrange a callback whenever the playing state changes */ |
159 | event_register("playing-changed", playing_changed, 0); | |
443c003c | 160 | event_register("playing-started", playing_changed, 0); |
15837f6a RK |
161 | /* We reget both playing track and queue at pause/resume so that start times |
162 | * can be computed correctly */ | |
c133bd3c | 163 | event_register("pause-changed", playing_changed, 0); |
15837f6a RK |
164 | event_register("pause-changed", queue_changed, 0); |
165 | /* Reget the queue whenever it changes */ | |
c133bd3c RK |
166 | event_register("queue-changed", queue_changed, 0); |
167 | /* ...and once a second anyway */ | |
168 | g_timeout_add(1000/*ms*/, playing_periodic, 0); | |
460b9539 | 169 | } |
170 | ||
4b51f265 | 171 | static void queue_drop_completed(void attribute((unused)) *v, |
82db9336 RK |
172 | const char *err) { |
173 | if(err) { | |
174 | popup_protocol_error(0, err); | |
175 | return; | |
176 | } | |
177 | /* The log should tell us the queue changed so we do no more here */ | |
178 | } | |
179 | ||
180 | /** @brief Called when drag+drop completes */ | |
a8c0e917 | 181 | static void queue_drop(struct queuelike attribute((unused)) *ql, |
0beb320e | 182 | int ntracks, |
4b51f265 | 183 | char **tracks, char **ids, |
0beb320e | 184 | struct queue_entry *after_me) { |
82db9336 | 185 | int n; |
4b51f265 RK |
186 | |
187 | if(ids) { | |
188 | /* Rearrangement */ | |
189 | if(playing_track) { | |
190 | /* If there's a playing track then you can't drag it anywhere */ | |
191 | for(n = 0; n < ntracks; ++n) { | |
192 | if(!strcmp(playing_track->id, ids[n])) { | |
193 | fprintf(stderr, "cannot drag playing track\n"); | |
194 | return; | |
195 | } | |
0beb320e | 196 | } |
4b51f265 RK |
197 | /* You can't tell the server to drag after the playing track by ID, you |
198 | * have to send "". */ | |
199 | if(after_me == playing_track) | |
200 | after_me = NULL; | |
201 | /* If you try to drag before the playing track (i.e. after_me=NULL on | |
202 | * input) then the effect is just to drag after it, although there's no | |
203 | * longer code to explicitly implement this. */ | |
82db9336 | 204 | } |
4b51f265 RK |
205 | /* Tell the server to move them. The log will tell us about the change (if |
206 | * indeed it succeeds!), so no need to rearrange the model now. */ | |
207 | disorder_eclient_moveafter(client, | |
ad131c25 | 208 | queue_drop_completed, |
4b51f265 | 209 | after_me ? after_me->id : "", |
ad131c25 RK |
210 | (char **)ids, ntracks, |
211 | NULL); | |
4b51f265 RK |
212 | } else { |
213 | /* You can't tell the server to insert after the playing track by ID, you | |
0beb320e RK |
214 | * have to send "". */ |
215 | if(after_me == playing_track) | |
216 | after_me = NULL; | |
4b51f265 RK |
217 | /* Play the tracks */ |
218 | disorder_eclient_playafter(client, | |
ad131c25 | 219 | queue_drop_completed, |
4b51f265 | 220 | after_me ? after_me->id : "", |
ad131c25 RK |
221 | (char **)tracks, ntracks, |
222 | NULL); | |
82db9336 | 223 | } |
82db9336 RK |
224 | } |
225 | ||
c133bd3c RK |
226 | /** @brief Columns for the queue */ |
227 | static const struct queue_column queue_columns[] = { | |
b0b15b7c | 228 | { "When", column_when, 0, COL_RIGHT }, |
c133bd3c | 229 | { "Who", column_who, 0, 0 }, |
b0b15b7c RK |
230 | { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, |
231 | { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, | |
232 | { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, | |
233 | { "Length", column_length, 0, COL_RIGHT } | |
460b9539 | 234 | }; |
235 | ||
c133bd3c | 236 | /** @brief Pop-up menu for queue */ |
6982880f | 237 | static struct menuitem queue_menuitems[] = { |
ba6f3dd4 RK |
238 | { "Track properties", GTK_STOCK_PROPERTIES, ql_properties_activate, ql_properties_sensitive, 0, 0 }, |
239 | { "Select all tracks", GTK_STOCK_SELECT_ALL, ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, | |
240 | { "Deselect all tracks", NULL, ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, | |
241 | { "Scratch playing track", GTK_STOCK_STOP, ql_scratch_activate, ql_scratch_sensitive, 0, 0 }, | |
4750a901 | 242 | { "Remove track from queue", GTK_STOCK_DELETE, ql_remove_activate, ql_remove_sensitive, 0, 0 }, |
ba6f3dd4 | 243 | { "Adopt track", NULL, ql_adopt_activate, ql_adopt_sensitive, 0, 0 }, |
4eb1f430 RK |
244 | }; |
245 | ||
a6712ea8 RK |
246 | static const GtkTargetEntry queue_targets[] = { |
247 | { | |
248 | QUEUED_TRACKS, /* drag type */ | |
249 | GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */ | |
250 | QUEUED_TRACKS_ID /* ID value */ | |
251 | }, | |
252 | { | |
253 | PLAYABLE_TRACKS, /* drag type */ | |
254 | GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */ | |
255 | PLAYABLE_TRACKS_ID, /* ID value */ | |
256 | }, | |
257 | { | |
258 | .target = NULL | |
259 | } | |
260 | }; | |
261 | ||
c133bd3c | 262 | struct queuelike ql_queue = { |
ee7552f8 | 263 | .name = "queue", |
c133bd3c RK |
264 | .init = queue_init, |
265 | .columns = queue_columns, | |
266 | .ncolumns = sizeof queue_columns / sizeof *queue_columns, | |
267 | .menuitems = queue_menuitems, | |
82db9336 | 268 | .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems, |
a6712ea8 RK |
269 | .drop = queue_drop, |
270 | .drag_source_targets = queue_targets, | |
271 | .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, | |
272 | .drag_dest_targets = queue_targets, | |
273 | .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY, | |
460b9539 | 274 | }; |
275 | ||
c10dd9af RK |
276 | /** @brief Called when a key is pressed in the queue tree view */ |
277 | static gboolean queue_key_press(GtkWidget attribute((unused)) *widget, | |
278 | GdkEventKey *event, | |
279 | gpointer user_data) { | |
280 | /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n", | |
281 | event->type, event->state, event->keyval);*/ | |
282 | switch(event->keyval) { | |
283 | case GDK_BackSpace: | |
284 | case GDK_Delete: | |
285 | if(event->state) | |
286 | break; /* Only take unmodified DEL/<-- */ | |
287 | ql_remove_activate(0, user_data); | |
288 | return TRUE; /* Do not propagate */ | |
289 | } | |
290 | return FALSE; /* Propagate */ | |
291 | } | |
292 | ||
c133bd3c | 293 | GtkWidget *queue_widget(void) { |
1b5f38c4 RK |
294 | GtkWidget *const w = init_queuelike(&ql_queue); |
295 | ||
c10dd9af RK |
296 | /* Catch keypresses */ |
297 | g_signal_connect(ql_queue.view, "key-press-event", | |
298 | G_CALLBACK(queue_key_press), &ql_queue); | |
1b5f38c4 | 299 | return w; |
c133bd3c | 300 | } |
460b9539 | 301 | |
717ba987 | 302 | /** @brief Return nonzero if @p track is in the queue */ |
460b9539 | 303 | int queued(const char *track) { |
304 | struct queue_entry *q; | |
305 | ||
306 | D(("queued %s", track)); | |
68110302 RK |
307 | /* Queue will contain resolved name */ |
308 | track = namepart_resolve(track); | |
460b9539 | 309 | for(q = ql_queue.q; q; q = q->next) |
310 | if(!strcmp(q->track, track)) | |
311 | return 1; | |
312 | return 0; | |
313 | } | |
314 | ||
68be70c0 RK |
315 | /* Playing widget for mini-mode */ |
316 | ||
317 | static void queue_set_playing_widget(const char attribute((unused)) *event, | |
4d9a43ba | 318 | void attribute((unused)) *eventdata, |
68be70c0 RK |
319 | void *callbackdata) { |
320 | GtkLabel *w = callbackdata; | |
68be70c0 | 321 | |
4d9a43ba | 322 | if(playing_track) { |
bb33cc31 RK |
323 | const char *artist = namepart(playing_track->track, "display", "artist"); |
324 | const char *album = namepart(playing_track->track, "display", "album"); | |
4d9a43ba RK |
325 | const char *title = namepart(playing_track->track, "display", "title"); |
326 | const char *ldata = column_length(playing_track, NULL); | |
327 | if(!ldata) | |
328 | ldata = ""; | |
329 | char *text; | |
bb33cc31 | 330 | byte_xasprintf(&text, "%s/%s/%s %s", artist, album, title, ldata); |
4d9a43ba | 331 | gtk_label_set_text(w, text); |
68be70c0 RK |
332 | } else |
333 | gtk_label_set_text(w, ""); | |
334 | } | |
335 | ||
336 | GtkWidget *playing_widget(void) { | |
337 | GtkWidget *w = gtk_label_new(""); | |
bb33cc31 | 338 | gtk_misc_set_alignment(GTK_MISC(w), 1.0, 0); |
4d9a43ba | 339 | /* Spot changes to the playing track */ |
68be70c0 RK |
340 | event_register("playing-track-changed", |
341 | queue_set_playing_widget, | |
342 | w); | |
4d9a43ba RK |
343 | /* Use the best-known name for it */ |
344 | event_register("lookups-complete", | |
345 | queue_set_playing_widget, | |
346 | w); | |
347 | /* Keep the amount played so far up to date */ | |
348 | event_register("periodic-fast", | |
349 | queue_set_playing_widget, | |
350 | w); | |
bb33cc31 | 351 | return frame_widget(w, NULL); |
68be70c0 RK |
352 | } |
353 | ||
460b9539 | 354 | /* |
355 | Local Variables: | |
356 | c-basic-offset:2 | |
357 | comment-column:40 | |
358 | fill-column:79 | |
359 | indent-tabs-mode:nil | |
360 | End: | |
361 | */ |