Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
fb009628 | 3 | * Copyright (C) 2006-2008 Richard Kettlewell |
460b9539 | 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 2 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, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * 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, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
717ba987 | 20 | #include "disobedience.h" |
6982880f | 21 | #include "popup.h" |
c133bd3c | 22 | #include "queue-generic.h" |
460b9539 | 23 | |
c133bd3c RK |
24 | /** @brief The actual queue */ |
25 | static struct queue_entry *actual_queue; | |
26 | static struct queue_entry *actual_playing_track; | |
460b9539 | 27 | |
c133bd3c RK |
28 | /** @brief The playing track */ |
29 | struct queue_entry *playing_track; | |
4eb1f430 | 30 | |
c133bd3c RK |
31 | /** @brief When we last got the playing track */ |
32 | time_t last_playing; | |
460b9539 | 33 | |
83fb99f9 | 34 | static void queue_completed(void *v, |
e2717131 | 35 | const char *err, |
83fb99f9 RK |
36 | struct queue_entry *q); |
37 | static void playing_completed(void *v, | |
e2717131 | 38 | const char *err, |
83fb99f9 RK |
39 | struct queue_entry *q); |
40 | ||
c133bd3c RK |
41 | /** @brief Called when either the actual queue or the playing track change */ |
42 | static void queue_playing_changed(void) { | |
460b9539 | 43 | |
83fb99f9 RK |
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); | |
ee7552f8 RK |
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 | } | |
c133bd3c RK |
69 | time(&last_playing); /* for column_length() */ |
70 | ql_new_queue(&ql_queue, q); | |
71 | /* Tell anyone who cares */ | |
72 | event_raise("queue-list-changed", q); | |
73 | event_raise("playing-track-changed", q); | |
460b9539 | 74 | } |
75 | ||
c133bd3c RK |
76 | /** @brief Update the queue itself */ |
77 | static void queue_completed(void attribute((unused)) *v, | |
abf99697 | 78 | const char *err, |
c133bd3c | 79 | struct queue_entry *q) { |
abf99697 RK |
80 | if(err) { |
81 | popup_protocol_error(0, err); | |
c133bd3c | 82 | return; |
3035257f | 83 | } |
c133bd3c RK |
84 | actual_queue = q; |
85 | queue_playing_changed(); | |
460b9539 | 86 | } |
87 | ||
c133bd3c | 88 | /** @brief Update the playing track */ |
460b9539 | 89 | static void playing_completed(void attribute((unused)) *v, |
abf99697 | 90 | const char *err, |
460b9539 | 91 | struct queue_entry *q) { |
abf99697 RK |
92 | if(err) { |
93 | popup_protocol_error(0, err); | |
c133bd3c | 94 | return; |
460b9539 | 95 | } |
c133bd3c RK |
96 | actual_playing_track = q; |
97 | queue_playing_changed(); | |
460b9539 | 98 | } |
99 | ||
c133bd3c | 100 | /** @brief Schedule an update to the queue |
717ba987 | 101 | * |
c133bd3c RK |
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); | |
460b9539 | 110 | } |
111 | ||
c133bd3c | 112 | /** @brief Schedule an update to the playing track |
717ba987 | 113 | * |
c133bd3c | 114 | * Called whenever it changes |
3ffa2d15 | 115 | */ |
a8cd6f84 | 116 | static void playing_changed(const char attribute((unused)) *event, |
c133bd3c RK |
117 | void attribute((unused)) *eventdata, |
118 | void attribute((unused)) *callbackdata) { | |
a8cd6f84 | 119 | D(("playing_changed")); |
3ffa2d15 RK |
120 | gtk_label_set_text(GTK_LABEL(report_label), "updating playing track"); |
121 | disorder_eclient_playing(client, playing_completed, 0); | |
122 | } | |
123 | ||
c133bd3c | 124 | /** @brief Called regularly |
717ba987 | 125 | * |
c133bd3c | 126 | * Updates the played-so-far field |
717ba987 | 127 | */ |
c133bd3c RK |
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; | |
460b9539 | 133 | } |
134 | ||
c133bd3c RK |
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); | |
15837f6a RK |
139 | /* We reget both playing track and queue at pause/resume so that start times |
140 | * can be computed correctly */ | |
c133bd3c | 141 | event_register("pause-changed", playing_changed, 0); |
15837f6a RK |
142 | event_register("pause-changed", queue_changed, 0); |
143 | /* Reget the queue whenever it changes */ | |
c133bd3c RK |
144 | event_register("queue-changed", queue_changed, 0); |
145 | /* ...and once a second anyway */ | |
146 | g_timeout_add(1000/*ms*/, playing_periodic, 0); | |
460b9539 | 147 | } |
148 | ||
82db9336 RK |
149 | static void queue_move_completed(void attribute((unused)) *v, |
150 | const char *err) { | |
151 | if(err) { | |
152 | popup_protocol_error(0, err); | |
153 | return; | |
154 | } | |
155 | /* The log should tell us the queue changed so we do no more here */ | |
156 | } | |
157 | ||
158 | /** @brief Called when drag+drop completes */ | |
159 | static void queue_drop(int src, int dst) { | |
160 | struct queue_entry *sq, *dq; | |
161 | int n; | |
162 | ||
163 | //fprintf(stderr, "queue_drop %d -> %d\n", src, dst); | |
164 | if(playing_track) { | |
165 | /* If there's a playing track then you can't drag it anywhere */ | |
166 | if(src == 0) { | |
167 | //fprintf(stderr, "cannot drag playing track\n"); | |
168 | queue_playing_changed(); | |
169 | return; | |
170 | } | |
171 | /* If you try to drop before the playing track we assume you missed and | |
172 | * mean after instead */ | |
173 | if(!dst) | |
174 | dst = 1; | |
175 | //fprintf(stderr, "...adjusted to %d -> %d\n\n", src, dst); | |
176 | } | |
177 | /* Find the entry to move */ | |
178 | for(n = 0, sq = ql_queue.q; n < src; ++n) | |
179 | sq = sq->next; | |
180 | /*fprintf(stderr, "source=%s (%s)\n", | |
181 | sq->id, sq->track);*/ | |
182 | const int after = dst - 1; | |
183 | if(after == -1) | |
184 | dq = 0; | |
185 | else | |
186 | /* Find the entry to insert after */ | |
187 | for(n = 0, dq = ql_queue.q; n < after; ++n) | |
188 | dq = dq->next; | |
189 | if(dq == playing_track) | |
190 | dq = 0; | |
191 | #if 0 | |
192 | if(dq) | |
193 | fprintf(stderr, "after=%s (%s)\n", | |
194 | dq->id, dq->track); | |
195 | else | |
196 | fprintf(stderr, "after=NULL\n"); | |
197 | #endif | |
198 | disorder_eclient_moveafter(client, | |
199 | dq ? dq->id : "", | |
200 | 1, &sq->id, | |
201 | queue_move_completed, NULL); | |
202 | } | |
203 | ||
c133bd3c RK |
204 | /** @brief Columns for the queue */ |
205 | static const struct queue_column queue_columns[] = { | |
b0b15b7c | 206 | { "When", column_when, 0, COL_RIGHT }, |
c133bd3c | 207 | { "Who", column_who, 0, 0 }, |
b0b15b7c RK |
208 | { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, |
209 | { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, | |
210 | { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, | |
211 | { "Length", column_length, 0, COL_RIGHT } | |
460b9539 | 212 | }; |
213 | ||
c133bd3c | 214 | /** @brief Pop-up menu for queue */ |
6982880f | 215 | static struct menuitem queue_menuitems[] = { |
c133bd3c RK |
216 | { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 }, |
217 | { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, | |
218 | { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, | |
c9fb35f4 | 219 | { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 }, |
c133bd3c | 220 | { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 }, |
4eb1f430 RK |
221 | }; |
222 | ||
c133bd3c | 223 | struct queuelike ql_queue = { |
ee7552f8 | 224 | .name = "queue", |
c133bd3c RK |
225 | .init = queue_init, |
226 | .columns = queue_columns, | |
227 | .ncolumns = sizeof queue_columns / sizeof *queue_columns, | |
228 | .menuitems = queue_menuitems, | |
82db9336 RK |
229 | .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems, |
230 | .drop = queue_drop | |
460b9539 | 231 | }; |
232 | ||
c10dd9af RK |
233 | /** @brief Called when a key is pressed in the queue tree view */ |
234 | static gboolean queue_key_press(GtkWidget attribute((unused)) *widget, | |
235 | GdkEventKey *event, | |
236 | gpointer user_data) { | |
237 | /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n", | |
238 | event->type, event->state, event->keyval);*/ | |
239 | switch(event->keyval) { | |
240 | case GDK_BackSpace: | |
241 | case GDK_Delete: | |
242 | if(event->state) | |
243 | break; /* Only take unmodified DEL/<-- */ | |
244 | ql_remove_activate(0, user_data); | |
245 | return TRUE; /* Do not propagate */ | |
246 | } | |
247 | return FALSE; /* Propagate */ | |
248 | } | |
249 | ||
c133bd3c | 250 | GtkWidget *queue_widget(void) { |
1b5f38c4 RK |
251 | GtkWidget *const w = init_queuelike(&ql_queue); |
252 | ||
c10dd9af RK |
253 | /* Catch keypresses */ |
254 | g_signal_connect(ql_queue.view, "key-press-event", | |
255 | G_CALLBACK(queue_key_press), &ql_queue); | |
1b5f38c4 | 256 | return w; |
c133bd3c | 257 | } |
460b9539 | 258 | |
717ba987 | 259 | /** @brief Return nonzero if @p track is in the queue */ |
460b9539 | 260 | int queued(const char *track) { |
261 | struct queue_entry *q; | |
262 | ||
263 | D(("queued %s", track)); | |
68110302 RK |
264 | /* Queue will contain resolved name */ |
265 | track = namepart_resolve(track); | |
460b9539 | 266 | for(q = ql_queue.q; q; q = q->next) |
267 | if(!strcmp(q->track, track)) | |
268 | return 1; | |
269 | return 0; | |
270 | } | |
271 | ||
272 | /* | |
273 | Local Variables: | |
274 | c-basic-offset:2 | |
275 | comment-column:40 | |
276 | fill-column:79 | |
277 | indent-tabs-mode:nil | |
278 | End: | |
279 | */ |