| 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 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 | */ |
| 20 | #include "disobedience.h" |
| 21 | #include "queue-generic.h" |
| 22 | |
| 23 | /** @brief The actual queue */ |
| 24 | static struct queue_entry *actual_queue; |
| 25 | static struct queue_entry *actual_playing_track; |
| 26 | |
| 27 | /** @brief The playing track */ |
| 28 | struct queue_entry *playing_track; |
| 29 | |
| 30 | /** @brief When we last got the playing track */ |
| 31 | time_t last_playing; |
| 32 | |
| 33 | static void queue_completed(void *v, |
| 34 | const char *error, |
| 35 | struct queue_entry *q); |
| 36 | static void playing_completed(void *v, |
| 37 | const char *error, |
| 38 | struct queue_entry *q); |
| 39 | |
| 40 | /** @brief Called when either the actual queue or the playing track change */ |
| 41 | static void queue_playing_changed(void) { |
| 42 | |
| 43 | /* Check that the playing track isn't in the queue. There's a race here due |
| 44 | * to the fact that we issue the two commands at slightly different times. |
| 45 | * If it goes wrong we re-issue and try again, so that we never offer up an |
| 46 | * inconsistent state. */ |
| 47 | if(actual_playing_track) { |
| 48 | struct queue_entry *q; |
| 49 | for(q = actual_queue; q; q = q->next) |
| 50 | if(!strcmp(q->id, actual_playing_track->id)) |
| 51 | break; |
| 52 | if(q) { |
| 53 | disorder_eclient_playing(client, playing_completed, 0); |
| 54 | disorder_eclient_queue(client, queue_completed, 0); |
| 55 | return; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | struct queue_entry *q = xmalloc(sizeof *q); |
| 60 | if(actual_playing_track) { |
| 61 | *q = *actual_playing_track; |
| 62 | q->next = actual_queue; |
| 63 | playing_track = q; |
| 64 | } else { |
| 65 | playing_track = NULL; |
| 66 | q = actual_queue; |
| 67 | } |
| 68 | time(&last_playing); /* for column_length() */ |
| 69 | ql_new_queue(&ql_queue, q); |
| 70 | /* Tell anyone who cares */ |
| 71 | event_raise("queue-list-changed", q); |
| 72 | event_raise("playing-track-changed", q); |
| 73 | } |
| 74 | |
| 75 | /** @brief Update the queue itself */ |
| 76 | static void queue_completed(void attribute((unused)) *v, |
| 77 | const char *error, |
| 78 | struct queue_entry *q) { |
| 79 | if(error) { |
| 80 | popup_protocol_error(0, error); |
| 81 | return; |
| 82 | } |
| 83 | actual_queue = q; |
| 84 | queue_playing_changed(); |
| 85 | } |
| 86 | |
| 87 | /** @brief Update the playing track */ |
| 88 | static void playing_completed(void attribute((unused)) *v, |
| 89 | const char *error, |
| 90 | struct queue_entry *q) { |
| 91 | if(error) { |
| 92 | popup_protocol_error(0, error); |
| 93 | return; |
| 94 | } |
| 95 | actual_playing_track = q; |
| 96 | queue_playing_changed(); |
| 97 | } |
| 98 | |
| 99 | /** @brief Schedule an update to the queue |
| 100 | * |
| 101 | * Called whenever a track is added to it or removed from it. |
| 102 | */ |
| 103 | static void queue_changed(const char attribute((unused)) *event, |
| 104 | void attribute((unused)) *eventdata, |
| 105 | void attribute((unused)) *callbackdata) { |
| 106 | D(("queue_changed")); |
| 107 | gtk_label_set_text(GTK_LABEL(report_label), "updating queue"); |
| 108 | disorder_eclient_queue(client, queue_completed, 0); |
| 109 | } |
| 110 | |
| 111 | /** @brief Schedule an update to the playing track |
| 112 | * |
| 113 | * Called whenever it changes |
| 114 | */ |
| 115 | static void playing_changed(const char attribute((unused)) *event, |
| 116 | void attribute((unused)) *eventdata, |
| 117 | void attribute((unused)) *callbackdata) { |
| 118 | D(("playing_changed")); |
| 119 | gtk_label_set_text(GTK_LABEL(report_label), "updating playing track"); |
| 120 | disorder_eclient_playing(client, playing_completed, 0); |
| 121 | } |
| 122 | |
| 123 | /** @brief Called regularly |
| 124 | * |
| 125 | * Updates the played-so-far field |
| 126 | */ |
| 127 | static gboolean playing_periodic(gpointer attribute((unused)) data) { |
| 128 | /* If there's a track playing, update its row */ |
| 129 | if(playing_track) |
| 130 | ql_update_row(playing_track, 0); |
| 131 | return TRUE; |
| 132 | } |
| 133 | |
| 134 | /** @brief Called at startup */ |
| 135 | static void queue_init(void) { |
| 136 | /* Arrange a callback whenever the playing state changes */ |
| 137 | event_register("playing-changed", playing_changed, 0); |
| 138 | /* We reget both playing track and queue at pause/resume so that start times |
| 139 | * can be computed correctly */ |
| 140 | event_register("pause-changed", playing_changed, 0); |
| 141 | event_register("pause-changed", queue_changed, 0); |
| 142 | /* Reget the queue whenever it changes */ |
| 143 | event_register("queue-changed", queue_changed, 0); |
| 144 | /* ...and once a second anyway */ |
| 145 | g_timeout_add(1000/*ms*/, playing_periodic, 0); |
| 146 | } |
| 147 | |
| 148 | /** @brief Columns for the queue */ |
| 149 | static const struct queue_column queue_columns[] = { |
| 150 | { "When", column_when, 0, COL_RIGHT }, |
| 151 | { "Who", column_who, 0, 0 }, |
| 152 | { "Artist", column_namepart, "artist", COL_EXPAND|COL_ELLIPSIZE }, |
| 153 | { "Album", column_namepart, "album", COL_EXPAND|COL_ELLIPSIZE }, |
| 154 | { "Title", column_namepart, "title", COL_EXPAND|COL_ELLIPSIZE }, |
| 155 | { "Length", column_length, 0, COL_RIGHT } |
| 156 | }; |
| 157 | |
| 158 | /** @brief Pop-up menu for queue */ |
| 159 | static struct queue_menuitem queue_menuitems[] = { |
| 160 | { "Track properties", ql_properties_activate, ql_properties_sensitive, 0, 0 }, |
| 161 | { "Select all tracks", ql_selectall_activate, ql_selectall_sensitive, 0, 0 }, |
| 162 | { "Deselect all tracks", ql_selectnone_activate, ql_selectnone_sensitive, 0, 0 }, |
| 163 | { "Scratch playing track", ql_scratch_activate, ql_scratch_sensitive, 0, 0 }, |
| 164 | { "Remove track from queue", ql_remove_activate, ql_remove_sensitive, 0, 0 }, |
| 165 | }; |
| 166 | |
| 167 | struct queuelike ql_queue = { |
| 168 | .name = "queue", |
| 169 | .init = queue_init, |
| 170 | .columns = queue_columns, |
| 171 | .ncolumns = sizeof queue_columns / sizeof *queue_columns, |
| 172 | .menuitems = queue_menuitems, |
| 173 | .nmenuitems = sizeof queue_menuitems / sizeof *queue_menuitems |
| 174 | }; |
| 175 | |
| 176 | /* Drag and drop has to be figured out experimentally, because it is not well |
| 177 | * documented. |
| 178 | * |
| 179 | * First you get a row-inserted. The path argument points to the destination |
| 180 | * row but this will not yet have had its values set. The source row is still |
| 181 | * present. AFAICT the iter argument points to the same place. |
| 182 | * |
| 183 | * Then you get a row-deleted. The path argument identifies the row that was |
| 184 | * deleted. By this stage the row inserted above has acquired its values. |
| 185 | * |
| 186 | * A complication is that the deletion will move the inserted row. For |
| 187 | * instance, if you do a drag that moves row 1 down to after the track that was |
| 188 | * formerly on row 9, in the row-inserted call it will show up as row 10, but |
| 189 | * in the row-deleted call, row 1 will have been deleted thus making the |
| 190 | * inserted row be row 9. |
| 191 | * |
| 192 | * So when we see the row-inserted we have no idea what track to move. |
| 193 | * Therefore we stash it until we see a row-deleted. |
| 194 | */ |
| 195 | |
| 196 | /** @brief Target row for drag */ |
| 197 | static int queue_drag_target = -1; |
| 198 | |
| 199 | static void queue_move_completed(void attribute((unused)) *v, |
| 200 | const char *error) { |
| 201 | if(error) { |
| 202 | popup_protocol_error(0, error); |
| 203 | return; |
| 204 | } |
| 205 | /* The log should tell us the queue changed so we do no more here */ |
| 206 | } |
| 207 | |
| 208 | static void queue_row_deleted(GtkTreeModel *treemodel, |
| 209 | GtkTreePath *path, |
| 210 | gpointer attribute((unused)) user_data) { |
| 211 | if(!suppress_actions) { |
| 212 | #if 0 |
| 213 | char *ps = gtk_tree_path_to_string(path); |
| 214 | fprintf(stderr, "row-deleted path=%s queue_drag_target=%d\n", |
| 215 | ps, queue_drag_target); |
| 216 | GtkTreeIter j[1]; |
| 217 | gboolean jt = gtk_tree_model_get_iter_first(treemodel, j); |
| 218 | int row = 0; |
| 219 | while(jt) { |
| 220 | struct queue_entry *q = ql_iter_to_q(treemodel, j); |
| 221 | fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)"); |
| 222 | jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_queue.store), j); |
| 223 | } |
| 224 | g_free(ps); |
| 225 | #endif |
| 226 | if(queue_drag_target < 0) { |
| 227 | error(0, "unsuppressed row-deleted with no row-inserted"); |
| 228 | return; |
| 229 | } |
| 230 | int drag_source = gtk_tree_path_get_indices(path)[0]; |
| 231 | |
| 232 | /* If the drag is downwards (=towards higher row numbers) then the target |
| 233 | * will have been moved upwards (=towards lower row numbers) by one row. */ |
| 234 | if(drag_source < queue_drag_target) |
| 235 | --queue_drag_target; |
| 236 | |
| 237 | /* Find the track to move */ |
| 238 | GtkTreeIter src[1]; |
| 239 | gboolean srcv = gtk_tree_model_iter_nth_child(treemodel, src, NULL, |
| 240 | queue_drag_target); |
| 241 | if(!srcv) { |
| 242 | error(0, "cannot get iterator to drag target %d", queue_drag_target); |
| 243 | queue_playing_changed(); |
| 244 | queue_drag_target = -1; |
| 245 | return; |
| 246 | } |
| 247 | struct queue_entry *srcq = ql_iter_to_q(treemodel, src); |
| 248 | assert(srcq); |
| 249 | //fprintf(stderr, "move %s %s\n", srcq->id, srcq->track); |
| 250 | |
| 251 | /* Don't allow the currently playing track to be moved. As above, we put |
| 252 | * the queue back into the right order straight away. */ |
| 253 | if(srcq == playing_track) { |
| 254 | //fprintf(stderr, "cannot move currently playing track\n"); |
| 255 | queue_playing_changed(); |
| 256 | queue_drag_target = -1; |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | /* Find the destination */ |
| 261 | struct queue_entry *dstq; |
| 262 | if(queue_drag_target) { |
| 263 | GtkTreeIter dst[1]; |
| 264 | gboolean dstv = gtk_tree_model_iter_nth_child(treemodel, dst, NULL, |
| 265 | queue_drag_target - 1); |
| 266 | if(!dstv) { |
| 267 | error(0, "cannot get iterator to drag target predecessor %d", |
| 268 | queue_drag_target - 1); |
| 269 | queue_playing_changed(); |
| 270 | queue_drag_target = -1; |
| 271 | return; |
| 272 | } |
| 273 | dstq = ql_iter_to_q(treemodel, dst); |
| 274 | assert(dstq); |
| 275 | if(dstq == playing_track) |
| 276 | dstq = 0; |
| 277 | } else |
| 278 | dstq = 0; |
| 279 | /* NB if the user attempts to move a queued track before the currently |
| 280 | * playing track we assume they just missed a bit, and put it after. */ |
| 281 | //fprintf(stderr, " target %s %s\n", dstq ? dstq->id : "(none)", dstq ? dstq->track : "(none)"); |
| 282 | /* Now we know what is to be moved. We need to know the preceding queue |
| 283 | * entry so we can move it. */ |
| 284 | disorder_eclient_moveafter(client, |
| 285 | dstq ? dstq->id : "", |
| 286 | 1, &srcq->id, |
| 287 | queue_move_completed, NULL); |
| 288 | queue_drag_target = -1; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | static void queue_row_inserted(GtkTreeModel attribute((unused)) *treemodel, |
| 293 | GtkTreePath *path, |
| 294 | GtkTreeIter attribute((unused)) *iter, |
| 295 | gpointer attribute((unused)) user_data) { |
| 296 | if(!suppress_actions) { |
| 297 | #if 0 |
| 298 | char *ps = gtk_tree_path_to_string(path); |
| 299 | GtkTreeIter piter[1]; |
| 300 | gboolean pi = gtk_tree_model_get_iter(treemodel, piter, path); |
| 301 | struct queue_entry *pq = pi ? ql_iter_to_q(treemodel, piter) : 0; |
| 302 | struct queue_entry *iq = ql_iter_to_q(treemodel, iter); |
| 303 | |
| 304 | fprintf(stderr, "row-inserted path=%s pi=%d pq=%p path=%s iq=%p iter=%s\n", |
| 305 | ps, |
| 306 | pi, |
| 307 | pq, |
| 308 | (pi |
| 309 | ? (pq ? pq->track : "(pq=0)") |
| 310 | : "(pi=FALSE)"), |
| 311 | iq, |
| 312 | iq ? iq->track : "(iq=0)"); |
| 313 | |
| 314 | GtkTreeIter j[1]; |
| 315 | gboolean jt = gtk_tree_model_get_iter_first(treemodel, j); |
| 316 | int row = 0; |
| 317 | while(jt) { |
| 318 | struct queue_entry *q = ql_iter_to_q(treemodel, j); |
| 319 | fprintf(stderr, " %2d %s\n", row++, q ? q->track : "(no q)"); |
| 320 | jt = gtk_tree_model_iter_next(GTK_TREE_MODEL(ql_queue.store), j); |
| 321 | } |
| 322 | g_free(ps); |
| 323 | #endif |
| 324 | queue_drag_target = gtk_tree_path_get_indices(path)[0]; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | GtkWidget *queue_widget(void) { |
| 329 | GtkWidget *const w = init_queuelike(&ql_queue); |
| 330 | |
| 331 | /* Enable drag+drop */ |
| 332 | gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ql_queue.view), TRUE); |
| 333 | g_signal_connect(ql_queue.store, |
| 334 | "row-inserted", |
| 335 | G_CALLBACK(queue_row_inserted), &ql_queue); |
| 336 | g_signal_connect(ql_queue.store, |
| 337 | "row-deleted", |
| 338 | G_CALLBACK(queue_row_deleted), &ql_queue); |
| 339 | return w; |
| 340 | } |
| 341 | |
| 342 | /** @brief Return nonzero if @p track is in the queue */ |
| 343 | int queued(const char *track) { |
| 344 | struct queue_entry *q; |
| 345 | |
| 346 | D(("queued %s", track)); |
| 347 | for(q = ql_queue.q; q; q = q->next) |
| 348 | if(!strcmp(q->track, track)) |
| 349 | return 1; |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | Local Variables: |
| 355 | c-basic-offset:2 |
| 356 | comment-column:40 |
| 357 | fill-column:79 |
| 358 | indent-tabs-mode:nil |
| 359 | End: |
| 360 | */ |