chiark / gitweb /
More careful about compact mode transition detection
[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
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 36time_t last_playing;
460b9539 37
83fb99f9 38static void queue_completed(void *v,
e2717131 39 const char *err,
83fb99f9
RK
40 struct queue_entry *q);
41static 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 */
46static 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);
01237e47
RK
58 if(full_mode)
59 disorder_eclient_queue(client, queue_completed, 0);
83fb99f9
RK
60 return;
61 }
62 }
63
64 struct queue_entry *q = xmalloc(sizeof *q);
ee7552f8
RK
65 if(actual_playing_track) {
66 *q = *actual_playing_track;
67 q->next = actual_queue;
68 playing_track = q;
69 } else {
70 playing_track = NULL;
71 q = actual_queue;
72 }
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 }
01237e47
RK
87 if(full_mode)
88 actual_queue = q;
89 else
90 actual_queue = NULL;
c133bd3c 91 queue_playing_changed();
460b9539 92}
93
c133bd3c 94/** @brief Update the playing track */
460b9539 95static void playing_completed(void attribute((unused)) *v,
abf99697 96 const char *err,
460b9539 97 struct queue_entry *q) {
abf99697
RK
98 if(err) {
99 popup_protocol_error(0, err);
c133bd3c 100 return;
460b9539 101 }
c133bd3c
RK
102 actual_playing_track = q;
103 queue_playing_changed();
4265e5d3 104 xtime(&last_playing);
460b9539 105}
106
c133bd3c 107/** @brief Schedule an update to the queue
717ba987 108 *
c133bd3c
RK
109 * Called whenever a track is added to it or removed from it.
110 */
111static void queue_changed(const char attribute((unused)) *event,
112 void attribute((unused)) *eventdata,
113 void attribute((unused)) *callbackdata) {
114 D(("queue_changed"));
01237e47
RK
115 if(!full_mode)
116 return;
c133bd3c
RK
117 gtk_label_set_text(GTK_LABEL(report_label), "updating queue");
118 disorder_eclient_queue(client, queue_completed, 0);
460b9539 119}
120
c133bd3c 121/** @brief Schedule an update to the playing track
717ba987 122 *
c133bd3c 123 * Called whenever it changes
3ffa2d15 124 */
a8cd6f84 125static void playing_changed(const char attribute((unused)) *event,
c133bd3c
RK
126 void attribute((unused)) *eventdata,
127 void attribute((unused)) *callbackdata) {
a8cd6f84 128 D(("playing_changed"));
3ffa2d15 129 gtk_label_set_text(GTK_LABEL(report_label), "updating playing track");
3900d6d6
RK
130 /* Setting last_playing=0 means that we don't know what the correct value
131 * is right now, e.g. because things have been deranged by a pause. */
132 last_playing = 0;
3ffa2d15
RK
133 disorder_eclient_playing(client, playing_completed, 0);
134}
135
c133bd3c 136/** @brief Called regularly
717ba987 137 *
c133bd3c 138 * Updates the played-so-far field
717ba987 139 */
c133bd3c
RK
140static gboolean playing_periodic(gpointer attribute((unused)) data) {
141 /* If there's a track playing, update its row */
142 if(playing_track)
143 ql_update_row(playing_track, 0);
e03df0a5
RK
144 /* If the first (nonplaying) track starts in the past, update the queue to
145 * get new expected start times; but rate limit this checking. (If we only
146 * do it once a minute then the rest of the queue can get out of date too
147 * easily.) */
148 struct queue_entry *q = ql_queue.q;
149 if(q) {
150 if(q == playing_track)
151 q = q->next;
152 if(q) {
153 time_t now;
154 time(&now);
155 if(q->expected / 15 < now / 15)
156 queue_changed(0,0,0);
157 }
158 }
c133bd3c 159 return TRUE;
460b9539 160}
161
c133bd3c 162/** @brief Called at startup */
a8c0e917 163static void queue_init(struct queuelike attribute((unused)) *ql) {
c133bd3c
RK
164 /* Arrange a callback whenever the playing state changes */
165 event_register("playing-changed", playing_changed, 0);
15837f6a
RK
166 /* We reget both playing track and queue at pause/resume so that start times
167 * can be computed correctly */
c133bd3c 168 event_register("pause-changed", playing_changed, 0);
15837f6a
RK
169 event_register("pause-changed", queue_changed, 0);
170 /* Reget the queue whenever it changes */
c133bd3c
RK
171 event_register("queue-changed", queue_changed, 0);
172 /* ...and once a second anyway */
173 g_timeout_add(1000/*ms*/, playing_periodic, 0);
460b9539 174}
175
4b51f265 176static void queue_drop_completed(void attribute((unused)) *v,
82db9336
RK
177 const char *err) {
178 if(err) {
179 popup_protocol_error(0, err);
180 return;
181 }
182 /* The log should tell us the queue changed so we do no more here */
183}
184
185/** @brief Called when drag+drop completes */
a8c0e917 186static void queue_drop(struct queuelike attribute((unused)) *ql,
0beb320e 187 int ntracks,
4b51f265 188 char **tracks, char **ids,
0beb320e 189 struct queue_entry *after_me) {
82db9336 190 int n;
4b51f265
RK
191
192 if(ids) {
193 /* Rearrangement */
194 if(playing_track) {
195 /* If there's a playing track then you can't drag it anywhere */
196 for(n = 0; n < ntracks; ++n) {
197 if(!strcmp(playing_track->id, ids[n])) {
198 fprintf(stderr, "cannot drag playing track\n");
199 return;
200 }
0beb320e 201 }
4b51f265
RK
202 /* You can't tell the server to drag after the playing track by ID, you
203 * have to send "". */
204 if(after_me == playing_track)
205 after_me = NULL;
206 /* If you try to drag before the playing track (i.e. after_me=NULL on
207 * input) then the effect is just to drag after it, although there's no
208 * longer code to explicitly implement this. */
82db9336 209 }
4b51f265
RK
210 /* Tell the server to move them. The log will tell us about the change (if
211 * indeed it succeeds!), so no need to rearrange the model now. */
212 disorder_eclient_moveafter(client,
213 after_me ? after_me->id : "",
214 ntracks, (const char **)ids,
215 queue_drop_completed, NULL);
216 } else {
217 /* You can't tell the server to insert after the playing track by ID, you
0beb320e
RK
218 * have to send "". */
219 if(after_me == playing_track)
220 after_me = NULL;
4b51f265
RK
221 /* Play the tracks */
222 disorder_eclient_playafter(client,
223 after_me ? after_me->id : "",
224 ntracks, (const char **)tracks,
225 queue_drop_completed, NULL);
82db9336 226 }
82db9336
RK
227}
228
c133bd3c
RK
229/** @brief Columns for the queue */
230static const struct queue_column queue_columns[] = {
b0b15b7c 231 { "When", column_when, 0, COL_RIGHT },
c133bd3c 232 { "Who", column_who, 0, 0 },
b0b15b7c
RK
233 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
234 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
235 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
236 { "Length", column_length, 0, COL_RIGHT }
460b9539 237};
238
c133bd3c 239/** @brief Pop-up menu for queue */
6982880f 240static struct menuitem queue_menuitems[] = {
c133bd3c
RK
241 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
242 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
243 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
c9fb35f4 244 { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
c133bd3c 245 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
a49c2c0f 246 { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 },
4eb1f430
RK
247};
248
a6712ea8
RK
249static const GtkTargetEntry queue_targets[] = {
250 {
251 QUEUED_TRACKS, /* drag type */
252 GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */
253 QUEUED_TRACKS_ID /* ID value */
254 },
255 {
256 PLAYABLE_TRACKS, /* drag type */
257 GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */
258 PLAYABLE_TRACKS_ID, /* ID value */
259 },
260 {
261 .target = NULL
262 }
263};
264
c133bd3c 265struct queuelike ql_queue = {
ee7552f8 266 .name = "queue",
c133bd3c
RK
267 .init = queue_init,
268 .columns = queue_columns,
269 .ncolumns = sizeof queue_columns / sizeof *queue_columns,
270 .menuitems = queue_menuitems,
82db9336 271 .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems,
a6712ea8
RK
272 .drop = queue_drop,
273 .drag_source_targets = queue_targets,
274 .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY,
275 .drag_dest_targets = queue_targets,
276 .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY,
460b9539 277};
278
c10dd9af
RK
279/** @brief Called when a key is pressed in the queue tree view */
280static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
281 GdkEventKey *event,
282 gpointer user_data) {
283 /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
284 event->type, event->state, event->keyval);*/
285 switch(event->keyval) {
286 case GDK_BackSpace:
287 case GDK_Delete:
288 if(event->state)
289 break; /* Only take unmodified DEL/<-- */
290 ql_remove_activate(0, user_data);
291 return TRUE; /* Do not propagate */
292 }
293 return FALSE; /* Propagate */
294}
295
01237e47
RK
296static void queue_minimode(const char attribute((unused)) *event,
297 void attribute((unused)) *evendata,
298 void attribute((unused)) *callbackdata) {
299 if(full_mode) {
300 /* We will need to refetch the queue */
301 disorder_eclient_queue(client, queue_completed, 0);
302 } else {
303 /* We will need to hide the queue */
304 if(actual_queue)
305 queue_completed(NULL, NULL, NULL);
306 }
307}
308
c133bd3c 309GtkWidget *queue_widget(void) {
1b5f38c4
RK
310 GtkWidget *const w = init_queuelike(&ql_queue);
311
c10dd9af
RK
312 /* Catch keypresses */
313 g_signal_connect(ql_queue.view, "key-press-event",
314 G_CALLBACK(queue_key_press), &ql_queue);
01237e47
RK
315
316 event_register("mini-mode-changed", queue_minimode, 0);
1b5f38c4 317 return w;
c133bd3c 318}
460b9539 319
717ba987 320/** @brief Return nonzero if @p track is in the queue */
460b9539 321int queued(const char *track) {
322 struct queue_entry *q;
323
324 D(("queued %s", track));
68110302
RK
325 /* Queue will contain resolved name */
326 track = namepart_resolve(track);
460b9539 327 for(q = ql_queue.q; q; q = q->next)
328 if(!strcmp(q->track, track))
329 return 1;
330 return 0;
331}
332
333/*
334Local Variables:
335c-basic-offset:2
336comment-column:40
337fill-column:79
338indent-tabs-mode:nil
339End:
340*/