chiark / gitweb /
More compact mode support. The notebook will disappear and is
[disorder] / disobedience / queue.c
... / ...
CommitLineData
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 */
26static struct queue_entry *actual_queue;
27static struct queue_entry *actual_playing_track;
28
29/** @brief The playing track */
30struct 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 */
36time_t last_playing;
37
38static void queue_completed(void *v,
39 const char *err,
40 struct queue_entry *q);
41static 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 */
46static 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 */
79static 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 */
91static 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 */
107static 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 */
119static 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 */
134static 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 /* 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 }
153 return TRUE;
154}
155
156/** @brief Called at startup */
157static void queue_init(struct queuelike attribute((unused)) *ql) {
158 /* Arrange a callback whenever the playing state changes */
159 event_register("playing-changed", playing_changed, 0);
160 /* We reget both playing track and queue at pause/resume so that start times
161 * can be computed correctly */
162 event_register("pause-changed", playing_changed, 0);
163 event_register("pause-changed", queue_changed, 0);
164 /* Reget the queue whenever it changes */
165 event_register("queue-changed", queue_changed, 0);
166 /* ...and once a second anyway */
167 g_timeout_add(1000/*ms*/, playing_periodic, 0);
168}
169
170static void queue_drop_completed(void attribute((unused)) *v,
171 const char *err) {
172 if(err) {
173 popup_protocol_error(0, err);
174 return;
175 }
176 /* The log should tell us the queue changed so we do no more here */
177}
178
179/** @brief Called when drag+drop completes */
180static void queue_drop(struct queuelike attribute((unused)) *ql,
181 int ntracks,
182 char **tracks, char **ids,
183 struct queue_entry *after_me) {
184 int n;
185
186 if(ids) {
187 /* Rearrangement */
188 if(playing_track) {
189 /* If there's a playing track then you can't drag it anywhere */
190 for(n = 0; n < ntracks; ++n) {
191 if(!strcmp(playing_track->id, ids[n])) {
192 fprintf(stderr, "cannot drag playing track\n");
193 return;
194 }
195 }
196 /* You can't tell the server to drag after the playing track by ID, you
197 * have to send "". */
198 if(after_me == playing_track)
199 after_me = NULL;
200 /* If you try to drag before the playing track (i.e. after_me=NULL on
201 * input) then the effect is just to drag after it, although there's no
202 * longer code to explicitly implement this. */
203 }
204 /* Tell the server to move them. The log will tell us about the change (if
205 * indeed it succeeds!), so no need to rearrange the model now. */
206 disorder_eclient_moveafter(client,
207 after_me ? after_me->id : "",
208 ntracks, (const char **)ids,
209 queue_drop_completed, NULL);
210 } else {
211 /* You can't tell the server to insert after the playing track by ID, you
212 * have to send "". */
213 if(after_me == playing_track)
214 after_me = NULL;
215 /* Play the tracks */
216 disorder_eclient_playafter(client,
217 after_me ? after_me->id : "",
218 ntracks, (const char **)tracks,
219 queue_drop_completed, NULL);
220 }
221}
222
223/** @brief Columns for the queue */
224static const struct queue_column queue_columns[] = {
225 { "When", column_when, 0, COL_RIGHT },
226 { "Who", column_who, 0, 0 },
227 { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE },
228 { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE },
229 { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE },
230 { "Length", column_length, 0, COL_RIGHT }
231};
232
233/** @brief Pop-up menu for queue */
234static struct menuitem queue_menuitems[] = {
235 { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 },
236 { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 },
237 { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 },
238 { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 },
239 { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 },
240 { "Adopt track", ql_adopt_activate, ql_adopt_sensitive, 0, 0 },
241};
242
243static const GtkTargetEntry queue_targets[] = {
244 {
245 QUEUED_TRACKS, /* drag type */
246 GTK_TARGET_SAME_WIDGET, /* rearrangement within a widget */
247 QUEUED_TRACKS_ID /* ID value */
248 },
249 {
250 PLAYABLE_TRACKS, /* drag type */
251 GTK_TARGET_SAME_APP|GTK_TARGET_OTHER_WIDGET, /* copying between widgets */
252 PLAYABLE_TRACKS_ID, /* ID value */
253 },
254 {
255 .target = NULL
256 }
257};
258
259struct queuelike ql_queue = {
260 .name = "queue",
261 .init = queue_init,
262 .columns = queue_columns,
263 .ncolumns = sizeof queue_columns / sizeof *queue_columns,
264 .menuitems = queue_menuitems,
265 .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems,
266 .drop = queue_drop,
267 .drag_source_targets = queue_targets,
268 .drag_source_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY,
269 .drag_dest_targets = queue_targets,
270 .drag_dest_actions = GDK_ACTION_MOVE|GDK_ACTION_COPY,
271};
272
273/** @brief Called when a key is pressed in the queue tree view */
274static gboolean queue_key_press(GtkWidget attribute((unused)) *widget,
275 GdkEventKey *event,
276 gpointer user_data) {
277 /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
278 event->type, event->state, event->keyval);*/
279 switch(event->keyval) {
280 case GDK_BackSpace:
281 case GDK_Delete:
282 if(event->state)
283 break; /* Only take unmodified DEL/<-- */
284 ql_remove_activate(0, user_data);
285 return TRUE; /* Do not propagate */
286 }
287 return FALSE; /* Propagate */
288}
289
290GtkWidget *queue_widget(void) {
291 GtkWidget *const w = init_queuelike(&ql_queue);
292
293 /* Catch keypresses */
294 g_signal_connect(ql_queue.view, "key-press-event",
295 G_CALLBACK(queue_key_press), &ql_queue);
296 return w;
297}
298
299/** @brief Return nonzero if @p track is in the queue */
300int queued(const char *track) {
301 struct queue_entry *q;
302
303 D(("queued %s", track));
304 /* Queue will contain resolved name */
305 track = namepart_resolve(track);
306 for(q = ql_queue.q; q; q = q->next)
307 if(!strcmp(q->track, track))
308 return 1;
309 return 0;
310}
311
312/*
313Local Variables:
314c-basic-offset:2
315comment-column:40
316fill-column:79
317indent-tabs-mode:nil
318End:
319*/