2 * This file is part of DisOrder
3 * Copyright (C) 2006-2008 Richard Kettlewell
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.
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.
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
20 #include "disobedience.h"
22 #include "queue-generic.h"
24 /** @brief The actual queue */
25 static struct queue_entry
*actual_queue
;
26 static struct queue_entry
*actual_playing_track
;
28 /** @brief The playing track */
29 struct queue_entry
*playing_track
;
31 /** @brief When we last got the playing track */
34 static void queue_completed(void *v
,
36 struct queue_entry
*q
);
37 static void playing_completed(void *v
,
39 struct queue_entry
*q
);
41 /** @brief Called when either the actual queue or the playing track change */
42 static void queue_playing_changed(void) {
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
))
54 disorder_eclient_playing(client
, playing_completed
, 0);
55 disorder_eclient_queue(client
, queue_completed
, 0);
60 struct queue_entry
*q
= xmalloc(sizeof *q
);
61 if(actual_playing_track
) {
62 *q
= *actual_playing_track
;
63 q
->next
= actual_queue
;
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
);
76 /** @brief Update the queue itself */
77 static void queue_completed(void attribute((unused
)) *v
,
79 struct queue_entry
*q
) {
81 popup_protocol_error(0, err
);
85 queue_playing_changed();
88 /** @brief Update the playing track */
89 static void playing_completed(void attribute((unused
)) *v
,
91 struct queue_entry
*q
) {
93 popup_protocol_error(0, err
);
96 actual_playing_track
= q
;
97 queue_playing_changed();
100 /** @brief Schedule an update to the queue
102 * Called whenever a track is added to it or removed from it.
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);
112 /** @brief Schedule an update to the playing track
114 * Called whenever it changes
116 static void playing_changed(const char attribute((unused
)) *event
,
117 void attribute((unused
)) *eventdata
,
118 void attribute((unused
)) *callbackdata
) {
119 D(("playing_changed"));
120 gtk_label_set_text(GTK_LABEL(report_label
), "updating playing track");
121 disorder_eclient_playing(client
, playing_completed
, 0);
124 /** @brief Called regularly
126 * Updates the played-so-far field
128 static gboolean
playing_periodic(gpointer
attribute((unused
)) data
) {
129 /* If there's a track playing, update its row */
131 ql_update_row(playing_track
, 0);
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);
139 /* We reget both playing track and queue at pause/resume so that start times
140 * can be computed correctly */
141 event_register("pause-changed", playing_changed
, 0);
142 event_register("pause-changed", queue_changed
, 0);
143 /* Reget the queue whenever it changes */
144 event_register("queue-changed", queue_changed
, 0);
145 /* ...and once a second anyway */
146 g_timeout_add(1000/*ms*/, playing_periodic
, 0);
149 static void queue_move_completed(void attribute((unused
)) *v
,
152 popup_protocol_error(0, err
);
155 /* The log should tell us the queue changed so we do no more here */
158 /** @brief Called when drag+drop completes */
159 static void queue_drop(int src
, int dst
) {
160 struct queue_entry
*sq
, *dq
;
163 //fprintf(stderr, "queue_drop %d -> %d\n", src, dst);
165 /* If there's a playing track then you can't drag it anywhere */
167 //fprintf(stderr, "cannot drag playing track\n");
168 queue_playing_changed();
171 /* If you try to drop before the playing track we assume you missed and
172 * mean after instead */
175 //fprintf(stderr, "...adjusted to %d -> %d\n\n", src, dst);
177 /* Find the entry to move */
178 for(n
= 0, sq
= ql_queue
.q
; n
< src
; ++n
)
180 /*fprintf(stderr, "source=%s (%s)\n",
181 sq->id, sq->track);*/
182 const int after
= dst
- 1;
186 /* Find the entry to insert after */
187 for(n
= 0, dq
= ql_queue
.q
; n
< after
; ++n
)
189 if(dq
== playing_track
)
193 fprintf(stderr
, "after=%s (%s)\n",
196 fprintf(stderr
, "after=NULL\n");
198 disorder_eclient_moveafter(client
,
201 queue_move_completed
, NULL
);
204 /** @brief Columns for the queue */
205 static const struct queue_column queue_columns
[] = {
206 { "When", column_when
, 0, COL_RIGHT
},
207 { "Who", column_who
, 0, 0 },
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
}
214 /** @brief Pop-up menu for queue */
215 static struct menuitem queue_menuitems
[] = {
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 },
219 { "Scratch playing track", ql_scratch_activate
, ql_scratch_sensitive
, 0, 0 },
220 { "Remove track from queue", ql_remove_activate
, ql_remove_sensitive
, 0, 0 },
223 struct queuelike ql_queue
= {
226 .columns
= queue_columns
,
227 .ncolumns
= sizeof queue_columns
/ sizeof *queue_columns
,
228 .menuitems
= queue_menuitems
,
229 .nmenuitems
= sizeof queue_menuitems
/ sizeof *queue_menuitems
,
233 /** @brief Called when a key is pressed in the queue tree view */
234 static gboolean
queue_key_press(GtkWidget
attribute((unused
)) *widget
,
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
) {
243 break; /* Only take unmodified DEL/<-- */
244 ql_remove_activate(0, user_data
);
245 return TRUE
; /* Do not propagate */
247 return FALSE
; /* Propagate */
250 GtkWidget
*queue_widget(void) {
251 GtkWidget
*const w
= init_queuelike(&ql_queue
);
253 /* Catch keypresses */
254 g_signal_connect(ql_queue
.view
, "key-press-event",
255 G_CALLBACK(queue_key_press
), &ql_queue
);
259 /** @brief Return nonzero if @p track is in the queue */
260 int queued(const char *track
) {
261 struct queue_entry
*q
;
263 D(("queued %s", track
));
264 /* Queue will contain resolved name */
265 track
= namepart_resolve(track
);
266 for(q
= ql_queue
.q
; q
; q
= q
->next
)
267 if(!strcmp(q
->track
, track
))