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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 /** @file disobedience/queue.c
19 * @brief Disobedience queue widget
21 #include "disobedience.h"
23 #include "queue-generic.h"
25 /** @brief The actual queue */
26 static struct queue_entry
*actual_queue
;
27 static struct queue_entry
*actual_playing_track
;
29 /** @brief The playing track */
30 struct queue_entry
*playing_track
;
32 /** @brief When we last got the playing track
34 * Set to 0 if the timings are currently off due to having just unpaused.
38 static void queue_completed(void *v
,
40 struct queue_entry
*q
);
41 static void playing_completed(void *v
,
43 struct queue_entry
*q
);
45 /** @brief Called when either the actual queue or the playing track change */
46 static 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
))
57 disorder_eclient_playing(client
, playing_completed
, 0);
58 disorder_eclient_queue(client
, queue_completed
, 0);
63 struct queue_entry
*q
= xmalloc(sizeof *q
);
64 if(actual_playing_track
) {
65 *q
= *actual_playing_track
;
66 q
->next
= actual_queue
;
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
);
78 /** @brief Update the queue itself */
79 static void queue_completed(void attribute((unused
)) *v
,
81 struct queue_entry
*q
) {
83 popup_protocol_error(0, err
);
87 queue_playing_changed();
90 /** @brief Update the playing track */
91 static void playing_completed(void attribute((unused
)) *v
,
93 struct queue_entry
*q
) {
95 popup_protocol_error(0, err
);
98 actual_playing_track
= q
;
99 queue_playing_changed();
100 xtime(&last_playing
);
103 /** @brief Schedule an update to the queue
105 * Called whenever a track is added to it or removed from it.
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);
115 /** @brief Schedule an update to the playing track
117 * Called whenever it changes
119 static 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. */
127 disorder_eclient_playing(client
, playing_completed
, 0);
130 /** @brief Called regularly
132 * Updates the played-so-far field
134 static gboolean
playing_periodic(gpointer
attribute((unused
)) data
) {
135 /* If there's a track playing, update its row */
137 ql_update_row(playing_track
, 0);
141 /** @brief Called at startup */
142 static void queue_init(struct queuelike
attribute((unused
)) *ql
) {
143 /* Arrange a callback whenever the playing state changes */
144 event_register("playing-changed", playing_changed
, 0);
145 /* We reget both playing track and queue at pause/resume so that start times
146 * can be computed correctly */
147 event_register("pause-changed", playing_changed
, 0);
148 event_register("pause-changed", queue_changed
, 0);
149 /* Reget the queue whenever it changes */
150 event_register("queue-changed", queue_changed
, 0);
151 /* ...and once a second anyway */
152 g_timeout_add(1000/*ms*/, playing_periodic
, 0);
155 static void queue_drop_completed(void attribute((unused
)) *v
,
158 popup_protocol_error(0, err
);
161 /* The log should tell us the queue changed so we do no more here */
164 /** @brief Called when drag+drop completes */
165 static void queue_drop(struct queuelike
attribute((unused
)) *ql
,
167 char **tracks
, char **ids
,
168 struct queue_entry
*after_me
) {
174 /* If there's a playing track then you can't drag it anywhere */
175 for(n
= 0; n
< ntracks
; ++n
) {
176 if(!strcmp(playing_track
->id
, ids
[n
])) {
177 fprintf(stderr
, "cannot drag playing track\n");
181 /* You can't tell the server to drag after the playing track by ID, you
182 * have to send "". */
183 if(after_me
== playing_track
)
185 /* If you try to drag before the playing track (i.e. after_me=NULL on
186 * input) then the effect is just to drag after it, although there's no
187 * longer code to explicitly implement this. */
189 /* Tell the server to move them. The log will tell us about the change (if
190 * indeed it succeeds!), so no need to rearrange the model now. */
191 disorder_eclient_moveafter(client
,
192 after_me ? after_me
->id
: "",
193 ntracks
, (const char **)ids
,
194 queue_drop_completed
, NULL
);
196 /* You can't tell the server to insert after the playing track by ID, you
197 * have to send "". */
198 if(after_me
== playing_track
)
200 /* Play the tracks */
201 disorder_eclient_playafter(client
,
202 after_me ? after_me
->id
: "",
203 ntracks
, (const char **)tracks
,
204 queue_drop_completed
, NULL
);
208 /** @brief Columns for the queue */
209 static const struct queue_column queue_columns
[] = {
210 { "When", column_when
, 0, COL_RIGHT
},
211 { "Who", column_who
, 0, 0 },
212 { "Artist", column_namepart
, "artist", COL_EXPAND
|COL_ELLIPSIZE
},
213 { "Album", column_namepart
, "album", COL_EXPAND
|COL_ELLIPSIZE
},
214 { "Title", column_namepart
, "title", COL_EXPAND
|COL_ELLIPSIZE
},
215 { "Length", column_length
, 0, COL_RIGHT
}
218 /** @brief Pop-up menu for queue */
219 static struct menuitem queue_menuitems
[] = {
220 { "Track properties", ql_properties_activate
, ql_properties_sensitive
, 0, 0 },
221 { "Select all tracks", ql_selectall_activate
, ql_selectall_sensitive
, 0, 0 },
222 { "Deselect all tracks", ql_selectnone_activate
, ql_selectnone_sensitive
, 0, 0 },
223 { "Scratch playing track", ql_scratch_activate
, ql_scratch_sensitive
, 0, 0 },
224 { "Remove track from queue", ql_remove_activate
, ql_remove_sensitive
, 0, 0 },
225 { "Adopt track", ql_adopt_activate
, ql_adopt_sensitive
, 0, 0 },
228 static const GtkTargetEntry queue_targets
[] = {
230 QUEUED_TRACKS
, /* drag type */
231 GTK_TARGET_SAME_WIDGET
, /* rearrangement within a widget */
232 QUEUED_TRACKS_ID
/* ID value */
235 PLAYABLE_TRACKS
, /* drag type */
236 GTK_TARGET_SAME_APP
|GTK_TARGET_OTHER_WIDGET
, /* copying between widgets */
237 PLAYABLE_TRACKS_ID
, /* ID value */
244 struct queuelike ql_queue
= {
247 .columns
= queue_columns
,
248 .ncolumns
= sizeof queue_columns
/ sizeof *queue_columns
,
249 .menuitems
= queue_menuitems
,
250 .nmenuitems
= sizeof queue_menuitems
/ sizeof *queue_menuitems
,
252 .drag_source_targets
= queue_targets
,
253 .drag_source_actions
= GDK_ACTION_MOVE
|GDK_ACTION_COPY
,
254 .drag_dest_targets
= queue_targets
,
255 .drag_dest_actions
= GDK_ACTION_MOVE
|GDK_ACTION_COPY
,
258 /** @brief Called when a key is pressed in the queue tree view */
259 static gboolean
queue_key_press(GtkWidget
attribute((unused
)) *widget
,
261 gpointer user_data
) {
262 /*fprintf(stderr, "queue_key_press type=%d state=%#x keyval=%#x\n",
263 event->type, event->state, event->keyval);*/
264 switch(event
->keyval
) {
268 break; /* Only take unmodified DEL/<-- */
269 ql_remove_activate(0, user_data
);
270 return TRUE
; /* Do not propagate */
272 return FALSE
; /* Propagate */
275 GtkWidget
*queue_widget(void) {
276 GtkWidget
*const w
= init_queuelike(&ql_queue
);
278 /* Catch keypresses */
279 g_signal_connect(ql_queue
.view
, "key-press-event",
280 G_CALLBACK(queue_key_press
), &ql_queue
);
284 /** @brief Return nonzero if @p track is in the queue */
285 int queued(const char *track
) {
286 struct queue_entry
*q
;
288 D(("queued %s", track
));
289 /* Queue will contain resolved name */
290 track
= namepart_resolve(track
);
291 for(q
= ql_queue
.q
; q
; q
= q
->next
)
292 if(!strcmp(q
->track
, track
))