Mercurial > audlegacy
annotate src/audacious/playlist.c @ 2489:e56a55d07935 trunk
[svn] - revise r3986 to only override tuple->track_name on lengthless streams
author | nenolod |
---|---|
date | Thu, 08 Feb 2007 08:42:01 -0800 |
parents | c5075a79f1aa |
children | b7f48f00a342 |
rev | line source |
---|---|
2313 | 1 /* Audacious |
2 * Copyright (C) 2005-2007 Audacious team. | |
3 * | |
4 * BMP (C) GPL 2003 $top_src_dir/AUTHORS | |
5 * | |
6 * based on: | |
7 * | |
8 * XMMS - Cross-platform multimedia player | |
9 * Copyright (C) 1998-2003 Peter Alm, Mikael Alm, Olle Hallnas, | |
10 * Thomas Nilsson and 4Front Technologies | |
11 * Copyright (C) 1999-2003 Haavard Kvaalen | |
12 * | |
13 * This program is free software; you can redistribute it and/or modify | |
14 * it under the terms of the GNU General Public License as published by | |
15 * the Free Software Foundation; under version 2 of the License. | |
16 * | |
17 * This program is distributed in the hope that it will be useful, | |
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 * GNU General Public License for more details. | |
21 * | |
22 * You should have received a copy of the GNU General Public License | |
23 * along with this program; if not, write to the Free Software | |
24 * Foundation, Inc., 59 Tmple Place - Suite 330, Boston, MA 02110-1301, USA. | |
25 */ | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 # include "config.h" | |
29 #endif | |
30 | |
31 #include "playlist.h" | |
32 | |
33 #include <glib.h> | |
34 #include <glib/gprintf.h> | |
35 #include <stdlib.h> | |
36 #include <string.h> | |
37 #include <time.h> | |
38 | |
39 #include <unistd.h> | |
40 #include <sys/types.h> | |
41 #include <sys/stat.h> | |
42 #include <sys/errno.h> | |
43 | |
44 #if defined(USE_REGEX_ONIGURUMA) | |
45 #include <onigposix.h> | |
46 #elif defined(USE_REGEX_PCRE) | |
47 #include <pcreposix.h> | |
48 #else | |
49 #include <regex.h> | |
50 #endif | |
51 | |
52 #include "input.h" | |
53 #include "main.h" | |
54 #include "ui_main.h" | |
2416
0fd7f4f969ad
[svn] integrated urldecode.* from libaudacious into audacious directory, made separate ui_fileopener.*
mf0102
parents:
2408
diff
changeset
|
55 #include "util.h" |
2313 | 56 #include "libaudacious/configdb.h" |
57 #include "vfs.h" | |
2416
0fd7f4f969ad
[svn] integrated urldecode.* from libaudacious into audacious directory, made separate ui_fileopener.*
mf0102
parents:
2408
diff
changeset
|
58 #include "urldecode.h" |
2313 | 59 #include "ui_equalizer.h" |
60 #include "playback.h" | |
61 #include "playlist.h" | |
62 #include "playlist_container.h" | |
63 #include "playlist_manager.h" | |
64 #include "ui_playlist.h" | |
2373
ad1d7687814c
[svn] made strings.h for existing strings.c, cleanups
mf0102
parents:
2328
diff
changeset
|
65 #include "strings.h" |
2313 | 66 #include "ui_fileinfo.h" |
67 | |
68 #include "debug.h" | |
69 | |
2407
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
70 #include "hook.h" |
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
71 |
2313 | 72 typedef gint (*PlaylistCompareFunc) (PlaylistEntry * a, PlaylistEntry * b); |
73 typedef void (*PlaylistSaveFunc) (FILE * file); | |
74 | |
75 /* If we manually change the song, p_p_b_j will show us where to go back to */ | |
76 PlaylistEntry *playlist_position_before_jump = NULL; | |
77 | |
78 static GList *playlists = NULL; | |
79 static GList *playlists_iter; | |
80 | |
81 /* If this is set to TRUE, we do not probe upon playlist add. | |
82 * | |
83 * Under Audacious 0.1.x, this was not a big deal because we used | |
84 * file extension introspection instead of looking for file format magic | |
85 * strings. | |
86 * | |
87 * Because we use file magic strings, we have to fstat a file being added | |
88 * to a playlist up to 1 * <number of input plugins installed> times. | |
89 * | |
90 * This can get really slow now that we're looking for files to add to a | |
91 * playlist. (Up to 5 minutes for 5000 songs, etcetera.) | |
92 * | |
93 * So, we obviously don't want to probe while opening a large playlist | |
94 * up. Hince the boolean below. | |
95 * | |
96 * January 7, 2006, William Pitcock <nenolod@nenolod.net> | |
97 */ | |
98 | |
99 G_LOCK_DEFINE(playlist_get_info_going); | |
100 | |
2328 | 101 //static gchar *playlist_current_name = NULL; |
2313 | 102 |
103 static gboolean playlist_get_info_scan_active = FALSE; | |
104 static gboolean playlist_get_info_going = FALSE; | |
105 static GThread *playlist_get_info_thread; | |
106 | |
107 static gint path_compare(const gchar * a, const gchar * b); | |
108 static gint playlist_compare_path(PlaylistEntry * a, PlaylistEntry * b); | |
109 static gint playlist_compare_filename(PlaylistEntry * a, PlaylistEntry * b); | |
110 static gint playlist_compare_title(PlaylistEntry * a, PlaylistEntry * b); | |
111 static gint playlist_compare_artist(PlaylistEntry * a, PlaylistEntry * b); | |
112 static time_t playlist_get_mtime(const gchar *filename); | |
113 static gint playlist_compare_date(PlaylistEntry * a, PlaylistEntry * b); | |
114 static gint playlist_compare_track(PlaylistEntry * a, PlaylistEntry * b); | |
115 static gint playlist_compare_playlist(PlaylistEntry * a, PlaylistEntry * b); | |
116 | |
117 static gint playlist_dupscmp_path(PlaylistEntry * a, PlaylistEntry * b); | |
118 static gint playlist_dupscmp_filename(PlaylistEntry * a, PlaylistEntry * b); | |
119 static gint playlist_dupscmp_title(PlaylistEntry * a, PlaylistEntry * b); | |
120 | |
121 static PlaylistCompareFunc playlist_compare_func_table[] = { | |
122 playlist_compare_path, | |
123 playlist_compare_filename, | |
124 playlist_compare_title, | |
125 playlist_compare_artist, | |
126 playlist_compare_date, | |
127 playlist_compare_track, | |
128 playlist_compare_playlist | |
129 }; | |
130 | |
131 static guint playlist_load_ins(Playlist * playlist, const gchar * filename, gint pos); | |
132 | |
133 static void playlist_generate_shuffle_list(Playlist *); | |
134 static void playlist_generate_shuffle_list_nolock(Playlist *); | |
135 | |
136 static void playlist_recalc_total_time_nolock(Playlist *); | |
137 static void playlist_recalc_total_time(Playlist *); | |
138 static gboolean playlist_entry_get_info(PlaylistEntry * entry); | |
139 | |
140 /* *********************** playlist entry code ********************** */ | |
141 | |
142 PlaylistEntry * | |
143 playlist_entry_new(const gchar * filename, | |
144 const gchar * title, | |
145 const gint length, | |
146 InputPlugin * dec) | |
147 { | |
148 PlaylistEntry *entry; | |
149 | |
150 entry = g_new0(PlaylistEntry, 1); | |
151 entry->filename = g_strdup(filename); | |
152 entry->title = str_to_utf8(title); | |
153 entry->length = length; | |
154 entry->selected = FALSE; | |
155 entry->decoder = dec; | |
156 | |
157 /* only do this if we have a decoder, otherwise it just takes too long */ | |
158 if (entry->decoder) | |
159 playlist_entry_get_info(entry); | |
160 | |
161 return entry; | |
162 } | |
163 | |
164 void | |
165 playlist_entry_free(PlaylistEntry * entry) | |
166 { | |
167 if (!entry) | |
168 return; | |
169 | |
170 if (entry->tuple != NULL) { | |
171 bmp_title_input_free(entry->tuple); | |
172 entry->tuple = NULL; | |
173 } | |
174 | |
175 if (entry->filename != NULL) | |
176 g_free(entry->filename); | |
177 | |
178 if (entry->title != NULL) | |
179 g_free(entry->title); | |
180 | |
181 g_free(entry); | |
182 } | |
183 | |
184 static gboolean | |
185 playlist_entry_get_info(PlaylistEntry * entry) | |
186 { | |
187 TitleInput *tuple; | |
188 time_t modtime; | |
189 | |
190 g_return_val_if_fail(entry != NULL, FALSE); | |
191 | |
2433
6cdccf094e51
[svn] - Why are we probing streams so unnecessarily often?
nhjm449
parents:
2416
diff
changeset
|
192 // Let's not randomly probe streams, mmkay? |
6cdccf094e51
[svn] - Why are we probing streams so unnecessarily often?
nhjm449
parents:
2416
diff
changeset
|
193 if (entry->decoder == NULL && (str_has_prefix_nocase(entry->filename, "http://") || str_has_prefix_nocase(entry->filename, "https://"))) |
6cdccf094e51
[svn] - Why are we probing streams so unnecessarily often?
nhjm449
parents:
2416
diff
changeset
|
194 return TRUE; |
6cdccf094e51
[svn] - Why are we probing streams so unnecessarily often?
nhjm449
parents:
2416
diff
changeset
|
195 |
2313 | 196 if (entry->tuple == NULL || entry->tuple->mtime > 0 || entry->tuple->mtime == -1) |
197 modtime = playlist_get_mtime(entry->filename); | |
198 else | |
199 modtime = 0; /* URI -nenolod */ | |
200 | |
201 if (entry->decoder == NULL) | |
202 entry->decoder = input_check_file(entry->filename, FALSE); | |
203 | |
204 /* renew tuple if file mtime is newer than tuple mtime. */ | |
205 if(entry->tuple){ | |
206 if(entry->tuple->mtime == modtime) | |
207 return TRUE; | |
208 else { | |
209 bmp_title_input_free(entry->tuple); | |
210 entry->tuple = NULL; | |
211 } | |
212 } | |
213 | |
214 if (entry->decoder == NULL || entry->decoder->get_song_tuple == NULL) | |
215 tuple = input_get_song_tuple(entry->filename); | |
216 else | |
217 tuple = entry->decoder->get_song_tuple(entry->filename); | |
218 | |
219 if (tuple == NULL) | |
220 return FALSE; | |
221 | |
222 /* attach mtime */ | |
223 tuple->mtime = modtime; | |
224 | |
225 /* entry is still around */ | |
226 entry->title = xmms_get_titlestring(tuple->formatter != NULL ? tuple->formatter : xmms_get_gentitle_format(), tuple); | |
227 entry->length = tuple->length; | |
228 entry->tuple = tuple; | |
229 | |
230 return TRUE; | |
231 } | |
232 | |
233 /* *********************** playlist selector code ************************* */ | |
234 | |
235 void | |
236 playlist_init(void) | |
237 { | |
238 Playlist *initial_pl; | |
239 | |
240 /* FIXME: is this really necessary? REQUIRE_STATIC_LOCK(playlists); */ | |
241 | |
242 initial_pl = playlist_new(); | |
243 | |
244 playlist_add_playlist(initial_pl); | |
245 } | |
246 | |
247 void | |
248 playlist_add_playlist(Playlist *playlist) | |
249 { | |
250 playlists = g_list_append(playlists, playlist); | |
251 | |
252 if (playlists_iter == NULL) | |
253 playlists_iter = playlists; | |
254 | |
255 playlist_manager_update(); | |
256 } | |
257 | |
258 void | |
259 playlist_remove_playlist(Playlist *playlist) | |
260 { | |
261 /* trying to free the last playlist simply clears and resets it */ | |
262 if (g_list_length(playlists) < 2) { | |
263 playlist_clear(playlist); | |
264 playlist_set_current_name(playlist, NULL); | |
265 return; | |
266 } | |
267 | |
268 if (playlist == playlist_get_active()) | |
269 playlist_select_next(); | |
270 | |
271 /* upon removal, a playlist should be cleared and freed */ | |
272 playlists = g_list_remove(playlists, playlist); | |
273 playlist_clear(playlist); | |
274 playlist_free(playlist); | |
275 | |
276 if (playlists_iter == NULL) | |
277 playlists_iter = playlists; | |
278 | |
279 playlist_manager_update(); | |
280 } | |
281 | |
282 GList * | |
283 playlist_get_playlists(void) | |
284 { | |
285 return playlists; | |
286 } | |
287 | |
288 void | |
289 playlist_select_next(void) | |
290 { | |
291 if (playlists_iter == NULL) | |
292 playlists_iter = playlists; | |
293 | |
294 playlists_iter = g_list_next(playlists_iter); | |
295 | |
296 if (playlists_iter == NULL) | |
297 playlists_iter = playlists; | |
298 | |
299 playlistwin_update_list(playlist_get_active()); | |
300 } | |
301 | |
302 void | |
303 playlist_select_prev(void) | |
304 { | |
305 if (playlists_iter == NULL) | |
306 playlists_iter = playlists; | |
307 | |
308 playlists_iter = g_list_previous(playlists_iter); | |
309 | |
310 if (playlists_iter == NULL) | |
311 playlists_iter = playlists; | |
312 | |
313 playlistwin_update_list(playlist_get_active()); | |
314 } | |
315 | |
316 void | |
317 playlist_select_playlist(Playlist *playlist) | |
318 { | |
319 if (playlists_iter == NULL) | |
320 playlists_iter = playlists; | |
321 | |
322 playlists_iter = g_list_find(playlists, playlist); | |
323 | |
324 if (playlists_iter == NULL) | |
325 playlists_iter = playlists; | |
326 | |
327 playlistwin_update_list(playlist); | |
328 } | |
329 | |
330 /* *********************** playlist code ********************** */ | |
331 | |
332 const gchar * | |
333 playlist_get_current_name(Playlist *playlist) | |
334 { | |
335 return playlist->title; | |
336 } | |
337 | |
338 gboolean | |
339 playlist_set_current_name(Playlist *playlist, const gchar * filename) | |
340 { | |
341 if (playlist->title) | |
342 g_free(playlist->title); | |
343 | |
344 if (!filename) { | |
345 playlist->title = NULL; | |
346 return FALSE; | |
347 } | |
348 | |
349 playlist->title = g_strdup(filename); | |
350 return TRUE; | |
351 } | |
352 | |
353 static GList * | |
354 find_playlist_position_list(Playlist *playlist) | |
355 { | |
356 REQUIRE_LOCK(playlist->mutex); | |
357 | |
358 if (!playlist->position) { | |
359 if (cfg.shuffle) | |
360 return playlist->shuffle; | |
361 else | |
362 return playlist->entries; | |
363 } | |
364 | |
365 if (cfg.shuffle) | |
366 return g_list_find(playlist->shuffle, playlist->position); | |
367 else | |
368 return g_list_find(playlist->entries, playlist->position); | |
369 } | |
370 | |
371 static void | |
372 play_queued(Playlist *playlist) | |
373 { | |
374 GList *tmp = playlist->queue; | |
375 | |
376 REQUIRE_LOCK( playlist->mutex ); | |
377 | |
378 playlist->position = playlist->queue->data; | |
379 playlist->queue = g_list_remove_link(playlist->queue, playlist->queue); | |
380 g_list_free_1(tmp); | |
381 } | |
382 | |
383 void | |
384 playlist_clear(Playlist *playlist) | |
385 { | |
386 if (!playlist) | |
387 return; | |
388 | |
389 PLAYLIST_LOCK( playlist->mutex ); | |
390 | |
391 g_list_foreach(playlist->entries, (GFunc) playlist_entry_free, NULL); | |
392 g_list_free(playlist->entries); | |
393 playlist->position = NULL; | |
394 playlist->entries = NULL; | |
395 | |
396 PLAYLIST_UNLOCK( playlist->mutex ); | |
397 | |
398 playlist_generate_shuffle_list(playlist); | |
399 playlistwin_update_list(playlist); | |
400 playlist_recalc_total_time(playlist); | |
401 playlist_manager_update(); | |
402 } | |
403 | |
404 static void | |
405 playlist_delete_node(Playlist * playlist, GList * node, gboolean * set_info_text, | |
406 gboolean * restart_playing) | |
407 { | |
408 PlaylistEntry *entry; | |
409 GList *playing_song = NULL; | |
410 | |
411 REQUIRE_LOCK(playlist->mutex); | |
412 | |
413 /* We call g_list_find manually here because we don't want an item | |
414 * in the shuffle_list */ | |
415 | |
416 if (playlist->position) | |
417 playing_song = g_list_find(playlist->entries, playlist->position); | |
418 | |
419 entry = PLAYLIST_ENTRY(node->data); | |
420 | |
421 if (playing_song == node) { | |
422 *set_info_text = TRUE; | |
423 | |
424 if (playback_get_playing()) { | |
425 PLAYLIST_UNLOCK(playlist->mutex); | |
426 ip_data.stop = TRUE; | |
427 playback_stop(); | |
428 ip_data.stop = FALSE; | |
429 PLAYLIST_LOCK(playlist->mutex); | |
430 *restart_playing = TRUE; | |
431 } | |
432 | |
433 playing_song = find_playlist_position_list(playlist); | |
434 | |
435 if (g_list_next(playing_song)) | |
436 playlist->position = g_list_next(playing_song)->data; | |
437 else if (g_list_previous(playing_song)) | |
438 playlist->position = g_list_previous(playing_song)->data; | |
439 else | |
440 playlist->position = NULL; | |
441 | |
442 /* Make sure the entry did not disappear under us */ | |
443 if (g_list_index(playlist->entries, entry) == -1) | |
444 return; | |
445 | |
446 } | |
447 else if (g_list_position(playlist->entries, playing_song) > | |
448 g_list_position(playlist->entries, node)) { | |
449 *set_info_text = TRUE; | |
450 } | |
451 | |
452 playlist->shuffle = g_list_remove(playlist->shuffle, entry); | |
453 playlist->queue = g_list_remove(playlist->queue, entry); | |
454 playlist->entries = g_list_remove_link(playlist->entries, node); | |
455 playlist_entry_free(entry); | |
456 g_list_free_1(node); | |
457 | |
458 playlist_recalc_total_time_nolock(playlist); | |
459 } | |
460 | |
461 void | |
462 playlist_delete_index(Playlist *playlist, guint pos) | |
463 { | |
464 gboolean restart_playing = FALSE, set_info_text = FALSE; | |
465 GList *node; | |
466 | |
467 if (!playlist) | |
468 return; | |
469 | |
470 PLAYLIST_LOCK(playlist->mutex); | |
471 | |
472 node = g_list_nth(playlist->entries, pos); | |
473 | |
474 if (!node) { | |
475 PLAYLIST_UNLOCK(playlist->mutex); | |
476 return; | |
477 } | |
478 | |
479 playlist_delete_node(playlist, node, &set_info_text, &restart_playing); | |
480 | |
481 PLAYLIST_UNLOCK(playlist->mutex); | |
482 | |
483 playlist_recalc_total_time(playlist); | |
484 | |
485 playlistwin_update_list(playlist); | |
486 if (restart_playing) { | |
487 if (playlist->position) { | |
488 playback_initiate(); | |
489 } | |
490 else { | |
491 mainwin_clear_song_info(); | |
492 } | |
493 } | |
494 else if (set_info_text) { | |
495 mainwin_set_info_text(); | |
496 } | |
497 | |
498 playlist_manager_update(); | |
499 } | |
500 | |
501 void | |
502 playlist_delete_filenames(Playlist * playlist, GList * filenames) | |
503 { | |
504 GList *node, *fnode; | |
505 gboolean set_info_text = FALSE, restart_playing = FALSE; | |
506 | |
507 PLAYLIST_LOCK(playlist->mutex); | |
508 | |
509 for (fnode = filenames; fnode; fnode = g_list_next(fnode)) { | |
510 node = playlist->entries; | |
511 | |
512 while (node) { | |
513 GList *next = g_list_next(node); | |
514 PlaylistEntry *entry = node->data; | |
515 | |
516 if (!strcmp(entry->filename, fnode->data)) | |
517 playlist_delete_node(playlist, node, &set_info_text, &restart_playing); | |
518 | |
519 node = next; | |
520 } | |
521 } | |
522 | |
523 playlist_recalc_total_time(playlist); | |
524 PLAYLIST_UNLOCK(playlist->mutex); | |
525 | |
526 playlistwin_update_list(playlist); | |
527 | |
528 if (restart_playing) { | |
529 if (playlist->position) { | |
530 playback_initiate(); | |
531 } | |
532 else { | |
533 mainwin_clear_song_info(); | |
534 } | |
535 } | |
536 else if (set_info_text) { | |
537 mainwin_set_info_text(); | |
538 } | |
539 | |
540 playlist_manager_update(); | |
541 } | |
542 | |
543 void | |
544 playlist_delete(Playlist * playlist, gboolean crop) | |
545 { | |
546 gboolean restart_playing = FALSE, set_info_text = FALSE; | |
547 GList *node, *next_node; | |
548 PlaylistEntry *entry; | |
549 | |
550 g_return_if_fail(playlist != NULL); | |
551 | |
552 PLAYLIST_LOCK(playlist->mutex); | |
553 | |
554 node = playlist->entries; | |
555 | |
556 while (node) { | |
557 entry = PLAYLIST_ENTRY(node->data); | |
558 | |
559 next_node = g_list_next(node); | |
560 | |
561 if ((entry->selected && !crop) || (!entry->selected && crop)) { | |
562 playlist_delete_node(playlist, node, &set_info_text, &restart_playing); | |
563 } | |
564 | |
565 node = next_node; | |
566 } | |
567 | |
568 PLAYLIST_UNLOCK(playlist->mutex); | |
569 | |
570 playlist_recalc_total_time(playlist); | |
571 | |
572 if (set_info_text) { | |
573 mainwin_set_info_text(); | |
574 } | |
575 | |
576 if (restart_playing) { | |
577 if (playlist->position) { | |
578 playback_initiate(); | |
579 } | |
580 else { | |
581 mainwin_clear_song_info(); | |
582 } | |
583 } | |
584 | |
585 playlistwin_update_list(playlist); | |
586 playlist_manager_update(); | |
587 } | |
588 | |
589 static void | |
590 __playlist_ins_with_info(Playlist * playlist, | |
591 const gchar * filename, | |
592 gint pos, | |
593 const gchar * title, | |
594 gint len, | |
595 InputPlugin * dec) | |
596 { | |
597 g_return_if_fail(filename != NULL); | |
598 | |
599 PLAYLIST_LOCK(playlist->mutex); | |
600 playlist->entries = g_list_insert(playlist->entries, | |
601 playlist_entry_new(filename, title, len, dec), | |
602 pos); | |
603 PLAYLIST_UNLOCK(playlist->mutex); | |
604 | |
605 g_mutex_lock(mutex_scan); | |
606 playlist_get_info_scan_active = TRUE; | |
607 g_mutex_unlock(mutex_scan); | |
608 g_cond_signal(cond_scan); | |
609 } | |
610 | |
611 static void | |
612 __playlist_ins_with_info_tuple(Playlist * playlist, | |
613 const gchar * filename, | |
614 gint pos, | |
615 TitleInput *tuple, | |
616 InputPlugin * dec) | |
617 { | |
618 GList *node; | |
619 PlaylistEntry *entry; | |
620 | |
621 g_return_if_fail(playlist != NULL); | |
622 g_return_if_fail(filename != NULL); | |
623 | |
624 PLAYLIST_LOCK(playlist->mutex); | |
625 playlist->entries = g_list_insert(playlist->entries, | |
626 playlist_entry_new(filename, tuple->track_name, tuple->length, dec), | |
627 pos); | |
628 | |
629 if (pos < 0) | |
630 pos = g_list_length(playlist->entries) - 1; /* last element. */ | |
631 | |
632 node = g_list_nth(playlist->entries, pos); | |
633 entry = PLAYLIST_ENTRY(node->data); | |
634 | |
635 if (tuple != NULL) { | |
636 entry->title = xmms_get_titlestring(tuple->formatter != NULL ? tuple->formatter : xmms_get_gentitle_format(), tuple); | |
637 entry->length = tuple->length; | |
638 entry->tuple = tuple; | |
639 } | |
640 | |
641 PLAYLIST_UNLOCK(playlist->mutex); | |
642 | |
643 g_mutex_lock(mutex_scan); | |
644 playlist_get_info_scan_active = TRUE; | |
645 g_mutex_unlock(mutex_scan); | |
646 g_cond_signal(cond_scan); | |
647 } | |
648 | |
649 static void | |
650 __playlist_ins(Playlist * playlist, const gchar * filename, gint pos, InputPlugin *dec) | |
651 { | |
652 __playlist_ins_with_info(playlist, filename, pos, NULL, -1, dec); | |
653 playlist_recalc_total_time(playlist); | |
654 playlist_manager_update(); | |
655 } | |
656 | |
657 gboolean | |
658 playlist_ins(Playlist * playlist, const gchar * filename, gint pos) | |
659 { | |
660 gchar buf[64], *p; | |
661 gint r; | |
662 VFSFile *file; | |
663 InputPlugin *dec; | |
664 | |
665 g_return_val_if_fail(playlist != NULL, FALSE); | |
666 g_return_val_if_fail(filename != NULL, FALSE); | |
667 | |
668 if (is_playlist_name(filename)) { | |
669 playlist->loading_playlist = TRUE; | |
670 playlist_load_ins(playlist, filename, pos); | |
671 playlist->loading_playlist = FALSE; | |
672 return TRUE; | |
673 } | |
674 | |
675 if (playlist->loading_playlist == TRUE || cfg.playlist_detect == TRUE) | |
676 dec = NULL; | |
677 else | |
678 dec = input_check_file(filename, TRUE); | |
679 | |
680 if (cfg.playlist_detect == TRUE || playlist->loading_playlist == TRUE || (playlist->loading_playlist == FALSE && dec != NULL)) | |
681 { | |
682 __playlist_ins(playlist, filename, pos, dec); | |
683 playlist_generate_shuffle_list(playlist); | |
684 playlistwin_update_list(playlist); | |
685 return TRUE; | |
686 } | |
687 | |
688 /* Some files (typically produced by some cgi-scripts) don't have | |
689 * the correct extension. Try to recognize these files by looking | |
690 * at their content. We only check for http entries since it does | |
691 * not make sense to have file entries in a playlist fetched from | |
692 * the net. */ | |
693 | |
694 /* Some strange people put fifo's with the .mp3 extension, so we | |
695 * need to make sure it's a real file (otherwise fread() may block | |
696 * and stall the entire program) */ | |
697 | |
698 /* FIXME: bah, FIFOs actually pass this regular file test */ | |
699 if (!vfs_file_test(filename, G_FILE_TEST_IS_REGULAR)) | |
700 return FALSE; | |
701 | |
702 if (!(file = vfs_fopen(filename, "rb"))) | |
703 return FALSE; | |
704 | |
705 r = vfs_fread(buf, 1, sizeof(buf), file); | |
706 vfs_fclose(file); | |
707 | |
708 for (p = buf; r-- > 0 && (*p == '\r' || *p == '\n'); p++); | |
709 | |
710 if (r > 5 && str_has_prefix_nocase(p, "http:")) { | |
711 playlist_load_ins(playlist, filename, pos); | |
712 return TRUE; | |
713 } | |
714 | |
715 return FALSE; | |
716 } | |
717 | |
718 /* FIXME: The next few functions are specific to Unix | |
719 * filesystems. Either abstract it away, or don't even bother checking | |
720 * at such low level */ | |
721 | |
722 typedef struct { | |
723 dev_t dev; | |
724 ino_t ino; | |
725 } DeviceInode; | |
726 | |
727 static DeviceInode * | |
728 devino_new(dev_t device, | |
729 ino_t inode) | |
730 { | |
731 DeviceInode *devino = g_new0(DeviceInode, 1); | |
732 | |
733 if (devino) | |
734 { | |
735 devino->dev = device; | |
736 devino->ino = inode; | |
737 } | |
738 | |
739 return devino; | |
740 } | |
741 | |
742 static guint | |
743 devino_hash(gconstpointer key) | |
744 { | |
745 const DeviceInode *d = key; | |
746 return d->ino; | |
747 } | |
748 | |
749 static gint | |
750 devino_compare(gconstpointer a, | |
751 gconstpointer b) | |
752 { | |
753 const DeviceInode *da = a, *db = b; | |
754 return (da->dev == db->dev && da->ino == db->ino); | |
755 } | |
756 | |
757 static gboolean | |
758 devino_destroy(gpointer key, | |
759 gpointer value, | |
760 gpointer data) | |
761 { | |
762 g_free(key); | |
763 return TRUE; | |
764 } | |
765 | |
766 static gboolean | |
767 file_is_hidden(const gchar * filename) | |
768 { | |
769 // FIXME: remove the const cast | |
770 g_return_val_if_fail(filename != NULL, FALSE); | |
771 return (g_basename((gchar *) filename)[0] == '.'); | |
772 } | |
773 | |
774 static GList * | |
775 playlist_dir_find_files(const gchar * path, | |
776 gboolean background, | |
777 GHashTable * htab) | |
778 { | |
779 GDir *dir; | |
780 GList *list = NULL, *ilist; | |
781 const gchar *dir_entry; | |
782 | |
783 struct stat statbuf; | |
784 DeviceInode *devino; | |
785 | |
786 if (!g_file_test(path, G_FILE_TEST_IS_DIR)) | |
787 return NULL; | |
788 | |
789 stat(path, &statbuf); | |
790 devino = devino_new(statbuf.st_dev, statbuf.st_ino); | |
791 | |
792 if (g_hash_table_lookup(htab, devino)) { | |
793 g_free(devino); | |
794 return NULL; | |
795 } | |
796 | |
797 g_hash_table_insert(htab, devino, GINT_TO_POINTER(1)); | |
798 | |
799 if ((ilist = input_scan_dir(path))) { | |
800 GList *node; | |
801 for (node = ilist; node; node = g_list_next(node)) { | |
802 gchar *name = g_build_filename(path, node->data, NULL); | |
803 list = g_list_prepend(list, name); | |
804 g_free(node->data); | |
805 } | |
806 g_list_free(ilist); | |
807 return list; | |
808 } | |
809 | |
810 if (!(dir = g_dir_open(path, 0, NULL))) | |
811 return NULL; | |
812 | |
813 while ((dir_entry = g_dir_read_name(dir))) { | |
814 gchar *filename; | |
815 | |
816 if (file_is_hidden(dir_entry)) | |
817 continue; | |
818 | |
819 filename = g_build_filename(path, dir_entry, NULL); | |
820 | |
821 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) { | |
822 GList *sub; | |
823 sub = playlist_dir_find_files(filename, background, htab); | |
824 g_free(filename); | |
825 list = g_list_concat(list, sub); | |
826 } | |
827 else if (cfg.playlist_detect == TRUE) | |
828 list = g_list_prepend(list, filename); | |
829 else if (input_check_file(filename, TRUE)) | |
830 list = g_list_prepend(list, filename); | |
831 else | |
832 g_free(filename); | |
833 | |
834 while (background && gtk_events_pending()) | |
835 gtk_main_iteration(); | |
836 } | |
837 g_dir_close(dir); | |
838 | |
839 return list; | |
840 } | |
841 | |
842 gboolean | |
843 playlist_add(Playlist * playlist, const gchar * filename) | |
844 { | |
845 return playlist_ins(playlist, filename, -1); | |
846 } | |
847 | |
848 guint | |
849 playlist_add_dir(Playlist * playlist, const gchar * directory) | |
850 { | |
851 return playlist_ins_dir(playlist, directory, -1, TRUE); | |
852 } | |
853 | |
854 guint | |
855 playlist_add_url(Playlist * playlist, const gchar * url) | |
856 { | |
857 return playlist_ins_url(playlist, url, -1); | |
858 } | |
859 | |
860 guint | |
861 playlist_ins_dir(Playlist * playlist, const gchar * path, | |
862 gint pos, | |
863 gboolean background) | |
864 { | |
865 guint entries = 0; | |
866 GList *list, *node; | |
867 GHashTable *htab; | |
868 | |
869 htab = g_hash_table_new(devino_hash, devino_compare); | |
870 | |
871 list = playlist_dir_find_files(path, background, htab); | |
872 list = g_list_sort(list, (GCompareFunc) path_compare); | |
873 | |
874 g_hash_table_foreach_remove(htab, devino_destroy, NULL); | |
875 | |
876 for (node = list; node; node = g_list_next(node)) { | |
877 __playlist_ins(playlist, node->data, pos, NULL); | |
878 g_free(node->data); | |
879 entries++; | |
880 if (pos >= 0) | |
881 pos++; | |
882 } | |
883 | |
884 g_list_free(list); | |
885 | |
886 playlist_recalc_total_time(playlist); | |
887 playlist_generate_shuffle_list(playlist); | |
888 playlistwin_update_list(playlist); | |
889 playlist_manager_update(); | |
890 return entries; | |
891 } | |
892 | |
893 guint | |
894 playlist_ins_url(Playlist * playlist, const gchar * string, | |
895 gint pos) | |
896 { | |
897 gchar *tmp; | |
898 gint i = 1, entries = 0; | |
899 gboolean first = TRUE; | |
900 guint firstpos = 0; | |
901 gboolean success = FALSE; | |
902 gchar *decoded = NULL; | |
903 | |
904 g_return_val_if_fail(playlist != NULL, 0); | |
905 g_return_val_if_fail(string != NULL, 0); | |
906 | |
907 playlistwin_update_list(playlist); | |
908 | |
909 while (*string) { | |
910 GList *node; | |
911 tmp = strchr(string, '\n'); | |
912 if (tmp) { | |
913 if (*(tmp - 1) == '\r') | |
914 *(tmp - 1) = '\0'; | |
915 *tmp = '\0'; | |
916 } | |
917 | |
918 decoded = g_strdup(string); | |
919 | |
920 if (g_file_test(decoded, G_FILE_TEST_IS_DIR)) { | |
921 i = playlist_ins_dir(playlist, decoded, pos, FALSE); | |
922 } | |
923 else { | |
924 if (is_playlist_name(decoded)) { | |
925 i = playlist_load_ins(playlist, decoded, pos); | |
926 } | |
927 else { | |
928 success = playlist_ins(playlist, decoded, pos); | |
929 i = 1; | |
930 } | |
931 } | |
932 | |
933 g_free(decoded); | |
934 | |
935 PLAYLIST_LOCK(playlist->mutex); | |
936 node = g_list_nth(playlist->entries, pos); | |
937 PLAYLIST_UNLOCK(playlist->mutex); | |
938 | |
939 entries += i; | |
940 | |
941 if (first) { | |
942 first = FALSE; | |
943 firstpos = pos; | |
944 } | |
945 | |
946 if (pos >= 0) | |
947 pos += i; | |
948 if (!tmp) | |
949 break; | |
950 | |
951 string = tmp + 1; | |
952 } | |
953 | |
954 playlist_recalc_total_time(playlist); | |
955 playlist_generate_shuffle_list(playlist); | |
956 playlistwin_update_list(playlist); | |
957 | |
958 playlist_manager_update(); | |
959 | |
960 return entries; | |
961 } | |
962 | |
963 void | |
964 playlist_set_info_old_abi(const gchar * title, gint length, gint rate, | |
965 gint freq, gint nch) | |
966 { | |
967 Playlist *playlist = playlist_get_active(); | |
968 | |
969 PLAYLIST_LOCK(playlist->mutex); | |
970 | |
971 g_return_if_fail(playlist != NULL); | |
972 | |
973 if (playlist->position) { | |
974 g_free(playlist->position->title); | |
975 playlist->position->title = g_strdup(title); | |
976 playlist->position->length = length; | |
2488
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
977 |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
978 // overwrite tuple->track_name, mainly for streaming. it may incur side effects. --yaz |
2489
e56a55d07935
[svn] - revise r3986 to only override tuple->track_name on lengthless streams
nenolod
parents:
2488
diff
changeset
|
979 if(playlist->position->tuple && length == -1){ |
2488
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
980 if(playlist->position->tuple->track_name){ |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
981 g_free(playlist->position->tuple->track_name); |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
982 } |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
983 playlist->position->tuple->track_name = g_strdup(title); |
c5075a79f1aa
[svn] make input->set_info overwrite tuple->track_name. it allows input
yaz
parents:
2480
diff
changeset
|
984 } |
2313 | 985 } |
986 | |
987 PLAYLIST_UNLOCK(playlist->mutex); | |
988 | |
989 playlist_recalc_total_time(playlist); | |
990 | |
991 mainwin_set_song_info(rate, freq, nch); | |
2460 | 992 |
993 if ( playlist->position ) | |
994 hook_call( "playlist set info" , playlist->position ); | |
2313 | 995 } |
996 | |
997 void | |
998 playlist_set_info(Playlist * playlist, const gchar * title, gint length, gint rate, | |
999 gint freq, gint nch) | |
1000 { | |
1001 PLAYLIST_LOCK(playlist->mutex); | |
1002 | |
1003 g_return_if_fail(playlist != NULL); | |
1004 | |
1005 if (playlist->position) { | |
1006 g_free(playlist->position->title); | |
1007 playlist->position->title = g_strdup(title); | |
1008 playlist->position->length = length; | |
1009 } | |
1010 | |
1011 PLAYLIST_UNLOCK(playlist->mutex); | |
1012 | |
1013 playlist_recalc_total_time(playlist); | |
1014 | |
1015 mainwin_set_song_info(rate, freq, nch); | |
2460 | 1016 |
1017 if ( playlist->position ) | |
1018 hook_call( "playlist set info" , playlist->position ); | |
2313 | 1019 } |
1020 | |
1021 void | |
1022 playlist_check_pos_current(Playlist *playlist) | |
1023 { | |
1024 gint pos, row, bottom; | |
1025 | |
1026 if (!playlist) | |
1027 return; | |
1028 | |
1029 PLAYLIST_LOCK(playlist->mutex); | |
1030 if (!playlist->position || !playlistwin_list) { | |
1031 PLAYLIST_UNLOCK(playlist->mutex); | |
1032 return; | |
1033 } | |
1034 | |
1035 pos = g_list_index(playlist->entries, playlist->position); | |
1036 | |
1037 if (playlistwin_item_visible(pos)) { | |
1038 PLAYLIST_UNLOCK(playlist->mutex); | |
1039 return; | |
1040 } | |
1041 | |
1042 bottom = MAX(0, playlist_get_length_nolock(playlist) - | |
1043 playlistwin_list->pl_num_visible); | |
1044 row = CLAMP(pos - playlistwin_list->pl_num_visible / 2, 0, bottom); | |
1045 PLAYLIST_UNLOCK(playlist->mutex); | |
1046 playlistwin_set_toprow(row); | |
1047 g_cond_signal(cond_scan); | |
1048 } | |
1049 | |
1050 void | |
1051 playlist_next(Playlist *playlist) | |
1052 { | |
1053 GList *plist_pos_list; | |
1054 gboolean restart_playing = FALSE; | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1055 if (!playlist_get_length(playlist)) |
2313 | 1056 return; |
1057 | |
1058 PLAYLIST_LOCK(playlist->mutex); | |
1059 | |
1060 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1061 { | |
1062 playlist->position = playlist_position_before_jump; | |
1063 playlist_position_before_jump = NULL; | |
1064 } | |
1065 | |
1066 plist_pos_list = find_playlist_position_list(playlist); | |
1067 | |
1068 if (!cfg.repeat && !g_list_next(plist_pos_list) && playlist->queue == NULL) { | |
1069 PLAYLIST_UNLOCK(playlist->mutex); | |
1070 return; | |
1071 } | |
1072 | |
1073 if (playback_get_playing()) { | |
1074 /* We need to stop before changing playlist_position */ | |
1075 PLAYLIST_UNLOCK(playlist->mutex); | |
1076 ip_data.stop = TRUE; | |
1077 playback_stop(); | |
1078 ip_data.stop = FALSE; | |
1079 PLAYLIST_LOCK(playlist->mutex); | |
1080 restart_playing = TRUE; | |
1081 } | |
1082 | |
1083 plist_pos_list = find_playlist_position_list(playlist); | |
1084 if (playlist->queue != NULL) | |
1085 play_queued(playlist); | |
1086 else if (g_list_next(plist_pos_list)) | |
1087 playlist->position = g_list_next(plist_pos_list)->data; | |
1088 else if (cfg.repeat) { | |
1089 playlist->position = NULL; | |
1090 playlist_generate_shuffle_list_nolock(playlist); | |
1091 if (cfg.shuffle) | |
1092 playlist->position = playlist->shuffle->data; | |
1093 else | |
1094 playlist->position = playlist->entries->data; | |
1095 } | |
1096 PLAYLIST_UNLOCK(playlist->mutex); | |
1097 playlist_check_pos_current(playlist); | |
1098 | |
1099 if (restart_playing) | |
1100 playback_initiate(); | |
1101 else { | |
1102 mainwin_set_info_text(); | |
1103 playlistwin_update_list(playlist); | |
1104 } | |
1105 } | |
1106 | |
1107 void | |
1108 playlist_prev(Playlist *playlist) | |
1109 { | |
1110 GList *plist_pos_list; | |
1111 gboolean restart_playing = FALSE; | |
1112 | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1113 if (!playlist_get_length(playlist)) |
2313 | 1114 return; |
1115 | |
1116 PLAYLIST_LOCK(playlist->mutex); | |
1117 | |
1118 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1119 { | |
1120 playlist->position = playlist_position_before_jump; | |
1121 playlist_position_before_jump = NULL; | |
1122 } | |
1123 | |
1124 plist_pos_list = find_playlist_position_list(playlist); | |
1125 | |
1126 if (!cfg.repeat && !g_list_previous(plist_pos_list)) { | |
1127 PLAYLIST_UNLOCK(playlist->mutex); | |
1128 return; | |
1129 } | |
1130 | |
1131 if (playback_get_playing()) { | |
1132 /* We need to stop before changing playlist_position */ | |
1133 PLAYLIST_UNLOCK(playlist->mutex); | |
1134 ip_data.stop = TRUE; | |
1135 playback_stop(); | |
1136 ip_data.stop = FALSE; | |
1137 PLAYLIST_LOCK(playlist->mutex); | |
1138 restart_playing = TRUE; | |
1139 } | |
1140 | |
1141 plist_pos_list = find_playlist_position_list(playlist); | |
1142 if (g_list_previous(plist_pos_list)) { | |
1143 playlist->position = g_list_previous(plist_pos_list)->data; | |
1144 } | |
1145 else if (cfg.repeat) { | |
1146 GList *node; | |
1147 playlist->position = NULL; | |
1148 playlist_generate_shuffle_list_nolock(playlist); | |
1149 if (cfg.shuffle) | |
1150 node = g_list_last(playlist->shuffle); | |
1151 else | |
1152 node = g_list_last(playlist->entries); | |
1153 if (node) | |
1154 playlist->position = node->data; | |
1155 } | |
1156 | |
1157 PLAYLIST_UNLOCK(playlist->mutex); | |
1158 | |
1159 playlist_check_pos_current(playlist); | |
1160 | |
1161 if (restart_playing) | |
1162 playback_initiate(); | |
1163 else { | |
1164 mainwin_set_info_text(); | |
1165 playlistwin_update_list(playlist); | |
1166 } | |
1167 } | |
1168 | |
1169 void | |
1170 playlist_queue(Playlist *playlist) | |
1171 { | |
1172 GList *list = playlist_get_selected(playlist); | |
1173 GList *it = list; | |
1174 | |
1175 PLAYLIST_LOCK(playlist->mutex); | |
1176 | |
1177 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1178 { | |
1179 /* Shuffling and this is our first manual jump. */ | |
1180 playlist_position_before_jump = playlist->position; | |
1181 } | |
1182 | |
1183 while (it) { | |
1184 GList *next = g_list_next(it); | |
1185 GList *tmp; | |
1186 | |
1187 /* XXX: WTF? --nenolod */ | |
1188 it->data = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(it->data)); | |
1189 if ((tmp = g_list_find(playlist->queue, it->data))) { | |
1190 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1191 g_list_free_1(tmp); | |
1192 list = g_list_remove_link(list, it); | |
1193 g_list_free_1(it); | |
1194 } | |
1195 | |
1196 it = next; | |
1197 } | |
1198 | |
1199 playlist->queue = g_list_concat(playlist->queue, list); | |
1200 | |
1201 PLAYLIST_UNLOCK(playlist->mutex); | |
1202 | |
1203 playlist_recalc_total_time(playlist); | |
1204 playlistwin_update_list(playlist); | |
1205 } | |
1206 | |
1207 void | |
1208 playlist_queue_position(Playlist *playlist, guint pos) | |
1209 { | |
1210 GList *tmp; | |
1211 PlaylistEntry *entry; | |
1212 | |
1213 PLAYLIST_LOCK(playlist->mutex); | |
1214 | |
1215 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1216 { | |
1217 /* Shuffling and this is our first manual jump. */ | |
1218 playlist_position_before_jump = playlist->position; | |
1219 } | |
1220 | |
1221 entry = g_list_nth_data(playlist->entries, pos); | |
1222 if ((tmp = g_list_find(playlist->queue, entry))) { | |
1223 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1224 g_list_free_1(tmp); | |
1225 } | |
1226 else | |
1227 playlist->queue = g_list_append(playlist->queue, entry); | |
1228 PLAYLIST_UNLOCK(playlist->mutex); | |
1229 | |
1230 playlist_recalc_total_time(playlist); | |
1231 playlistwin_update_list(playlist); | |
1232 } | |
1233 | |
1234 gboolean | |
1235 playlist_is_position_queued(Playlist *playlist, guint pos) | |
1236 { | |
1237 PlaylistEntry *entry; | |
1238 GList *tmp; | |
1239 | |
1240 PLAYLIST_LOCK(playlist->mutex); | |
1241 entry = g_list_nth_data(playlist->entries, pos); | |
1242 tmp = g_list_find(playlist->queue, entry); | |
1243 PLAYLIST_UNLOCK(playlist->mutex); | |
1244 | |
1245 return tmp != NULL; | |
1246 } | |
1247 | |
1248 gint | |
1249 playlist_get_queue_position_number(Playlist *playlist, guint pos) | |
1250 { | |
1251 PlaylistEntry *entry; | |
1252 gint tmp; | |
1253 | |
1254 PLAYLIST_LOCK(playlist->mutex); | |
1255 entry = g_list_nth_data(playlist->entries, pos); | |
1256 tmp = g_list_index(playlist->queue, entry); | |
1257 PLAYLIST_UNLOCK(playlist->mutex); | |
1258 | |
1259 return tmp; | |
1260 } | |
1261 | |
1262 gint | |
1263 playlist_get_queue_qposition_number(Playlist *playlist, guint pos) | |
1264 { | |
1265 PlaylistEntry *entry; | |
1266 gint tmp; | |
1267 | |
1268 PLAYLIST_LOCK(playlist->mutex); | |
1269 entry = g_list_nth_data(playlist->queue, pos); | |
1270 tmp = g_list_index(playlist->entries, entry); | |
1271 PLAYLIST_UNLOCK(playlist->mutex); | |
1272 | |
1273 return tmp; | |
1274 } | |
1275 | |
1276 void | |
1277 playlist_clear_queue(Playlist *playlist) | |
1278 { | |
1279 PLAYLIST_LOCK(playlist->mutex); | |
1280 g_list_free(playlist->queue); | |
1281 playlist->queue = NULL; | |
1282 PLAYLIST_UNLOCK(playlist->mutex); | |
1283 | |
1284 playlist_recalc_total_time(playlist); | |
1285 playlistwin_update_list(playlist); | |
1286 } | |
1287 | |
1288 void | |
1289 playlist_queue_remove(Playlist *playlist, guint pos) | |
1290 { | |
1291 void *entry; | |
1292 | |
1293 PLAYLIST_LOCK(playlist->mutex); | |
1294 entry = g_list_nth_data(playlist->entries, pos); | |
1295 playlist->queue = g_list_remove(playlist->queue, entry); | |
1296 PLAYLIST_UNLOCK(playlist->mutex); | |
1297 | |
1298 playlistwin_update_list(playlist); | |
1299 } | |
1300 | |
1301 gint | |
1302 playlist_get_queue_position(Playlist *playlist, PlaylistEntry * entry) | |
1303 { | |
1304 return g_list_index(playlist->queue, entry); | |
1305 } | |
1306 | |
1307 void | |
1308 playlist_set_position(Playlist *playlist, guint pos) | |
1309 { | |
1310 GList *node; | |
1311 gboolean restart_playing = FALSE; | |
1312 | |
1313 if (!playlist) | |
1314 return; | |
1315 | |
1316 PLAYLIST_LOCK(playlist->mutex); | |
1317 | |
1318 node = g_list_nth(playlist->entries, pos); | |
1319 if (!node) { | |
1320 PLAYLIST_UNLOCK(playlist->mutex); | |
1321 return; | |
1322 } | |
1323 | |
1324 if (playback_get_playing()) { | |
1325 /* We need to stop before changing playlist_position */ | |
1326 PLAYLIST_UNLOCK(playlist->mutex); | |
1327 ip_data.stop = TRUE; | |
1328 playback_stop(); | |
1329 ip_data.stop = FALSE; | |
1330 PLAYLIST_LOCK(playlist->mutex); | |
1331 restart_playing = TRUE; | |
1332 } | |
1333 | |
1334 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1335 { | |
1336 /* Shuffling and this is our first manual jump. */ | |
1337 playlist_position_before_jump = playlist->position; | |
1338 } | |
1339 | |
1340 playlist->position = node->data; | |
1341 PLAYLIST_UNLOCK(playlist->mutex); | |
1342 playlist_check_pos_current(playlist); | |
1343 | |
1344 if (restart_playing) | |
1345 playback_initiate(); | |
1346 else { | |
1347 mainwin_set_info_text(); | |
1348 playlistwin_update_list(playlist); | |
1349 } | |
1350 } | |
1351 | |
1352 void | |
1353 playlist_eof_reached(Playlist *playlist) | |
1354 { | |
1355 GList *plist_pos_list; | |
1356 | |
1357 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1358 ip_data.stop = TRUE; | |
1359 playback_stop(); | |
1360 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1361 ip_data.stop = FALSE; | |
1362 | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1363 hook_call("playback end", playlist->position); |
2407
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
1364 |
2313 | 1365 PLAYLIST_LOCK(playlist->mutex); |
1366 | |
1367 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1368 { | |
1369 playlist->position = playlist_position_before_jump; | |
1370 playlist_position_before_jump = NULL; | |
1371 } | |
1372 | |
1373 plist_pos_list = find_playlist_position_list(playlist); | |
1374 | |
1375 if (cfg.no_playlist_advance) { | |
1376 PLAYLIST_UNLOCK(playlist->mutex); | |
1377 mainwin_clear_song_info(); | |
1378 if (cfg.repeat) | |
1379 playback_initiate(); | |
1380 return; | |
1381 } | |
1382 | |
1383 if (cfg.stopaftersong) { | |
1384 PLAYLIST_UNLOCK(playlist->mutex); | |
1385 mainwin_clear_song_info(); | |
1386 mainwin_set_stopaftersong(FALSE); | |
1387 return; | |
1388 } | |
1389 | |
1390 if (playlist->queue != NULL) { | |
1391 play_queued(playlist); | |
1392 } | |
1393 else if (!g_list_next(plist_pos_list)) { | |
1394 if (cfg.shuffle) { | |
1395 playlist->position = NULL; | |
1396 playlist_generate_shuffle_list_nolock(playlist); | |
1397 } | |
2380 | 1398 else if (playlist->entries != NULL) |
2313 | 1399 playlist->position = playlist->entries->data; |
1400 | |
1401 if (!cfg.repeat) { | |
1402 PLAYLIST_UNLOCK(playlist->mutex); | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1403 hook_call("playlist end reached", playlist->position); |
2313 | 1404 mainwin_clear_song_info(); |
1405 mainwin_set_info_text(); | |
1406 return; | |
1407 } | |
1408 } | |
1409 else | |
1410 playlist->position = g_list_next(plist_pos_list)->data; | |
1411 | |
1412 PLAYLIST_UNLOCK(playlist->mutex); | |
1413 | |
1414 playlist_check_pos_current(playlist); | |
1415 playback_initiate(); | |
1416 mainwin_set_info_text(); | |
1417 playlistwin_update_list(playlist); | |
1418 } | |
1419 | |
1420 gint | |
1421 playlist_get_length(Playlist *playlist) | |
1422 { | |
1423 gint retval; | |
1424 | |
1425 PLAYLIST_LOCK(playlist->mutex); | |
1426 retval = playlist_get_length_nolock(playlist); | |
1427 PLAYLIST_UNLOCK(playlist->mutex); | |
1428 | |
1429 return retval; | |
1430 } | |
1431 | |
1432 gint | |
1433 playlist_queue_get_length(Playlist *playlist) | |
1434 { | |
1435 gint length; | |
1436 | |
1437 PLAYLIST_LOCK(playlist->mutex); | |
1438 length = g_list_length(playlist->queue); | |
1439 PLAYLIST_UNLOCK(playlist->mutex); | |
1440 | |
1441 return length; | |
1442 } | |
1443 | |
1444 gint | |
1445 playlist_get_length_nolock(Playlist *playlist) | |
1446 { | |
1447 return g_list_length(playlist->entries); | |
1448 } | |
1449 | |
1450 gchar * | |
1451 playlist_get_info_text(Playlist *playlist) | |
1452 { | |
1453 gchar *text, *title, *numbers, *length; | |
1454 | |
1455 g_return_val_if_fail(playlist != NULL, NULL); | |
1456 | |
1457 PLAYLIST_LOCK(playlist->mutex); | |
1458 if (!playlist->position) { | |
1459 PLAYLIST_UNLOCK(playlist->mutex); | |
1460 return NULL; | |
1461 } | |
1462 | |
1463 /* FIXME: there should not be a need to do additional conversion, | |
1464 * if playlist is properly maintained */ | |
1465 if (playlist->position->title) { | |
1466 title = str_to_utf8(playlist->position->title); | |
1467 } | |
1468 else { | |
1469 gchar *basename = g_path_get_basename(playlist->position->filename); | |
1470 title = filename_to_utf8(basename); | |
1471 g_free(basename); | |
1472 } | |
1473 | |
1474 /* | |
1475 * If the user don't want numbers in the playlist, don't | |
1476 * display them in other parts of XMMS | |
1477 */ | |
1478 | |
1479 if (cfg.show_numbers_in_pl) | |
1480 numbers = g_strdup_printf("%d. ", playlist_get_position_nolock(playlist) + 1); | |
1481 else | |
1482 numbers = g_strdup(""); | |
1483 | |
1484 if (playlist->position->length != -1) | |
1485 length = g_strdup_printf(" (%d:%-2.2d)", | |
1486 playlist->position->length / 60000, | |
1487 (playlist->position->length / 1000) % 60); | |
1488 else | |
1489 length = g_strdup(""); | |
1490 | |
1491 PLAYLIST_UNLOCK(playlist->mutex); | |
1492 | |
1493 text = convert_title_text(g_strconcat(numbers, title, length, NULL)); | |
1494 | |
1495 g_free(numbers); | |
1496 g_free(title); | |
1497 g_free(length); | |
1498 | |
1499 return text; | |
1500 } | |
1501 | |
1502 gint | |
1503 playlist_get_current_length(Playlist * playlist) | |
1504 { | |
1505 gint len = 0; | |
1506 | |
1507 if (!playlist) | |
1508 return 0; | |
1509 | |
1510 PLAYLIST_LOCK(playlist->mutex); | |
1511 if (playlist->position) | |
1512 len = playlist->position->length; | |
1513 PLAYLIST_UNLOCK(playlist->mutex); | |
1514 | |
1515 return len; | |
1516 } | |
1517 | |
1518 gboolean | |
1519 playlist_save(Playlist * playlist, const gchar * filename) | |
1520 { | |
1521 PlaylistContainer *plc = NULL; | |
1522 gchar *ext; | |
1523 | |
1524 g_return_val_if_fail(playlist != NULL, FALSE); | |
1525 g_return_val_if_fail(filename != NULL, FALSE); | |
1526 | |
1527 ext = strrchr(filename, '.') + 1; | |
1528 | |
1529 playlist_set_current_name(playlist, filename); | |
1530 | |
1531 if ((plc = playlist_container_find(ext)) == NULL) | |
1532 return FALSE; | |
1533 | |
1534 if (plc->plc_write == NULL) | |
1535 return FALSE; | |
1536 | |
1537 plc->plc_write(filename, 0); | |
1538 | |
1539 return TRUE; | |
1540 } | |
1541 | |
1542 gboolean | |
1543 playlist_load(Playlist * playlist, const gchar * filename) | |
1544 { | |
1545 gboolean ret = FALSE; | |
1546 g_return_val_if_fail(playlist != NULL, FALSE); | |
1547 | |
1548 playlist->loading_playlist = TRUE; | |
1549 ret = playlist_load_ins(playlist, filename, -1); | |
1550 playlist->loading_playlist = FALSE; | |
1551 | |
1552 return ret; | |
1553 } | |
1554 | |
1555 void | |
1556 playlist_load_ins_file(Playlist *playlist, | |
1557 const gchar * filename_p, | |
1558 const gchar * playlist_name, gint pos, | |
1559 const gchar * title, gint len) | |
1560 { | |
1561 gchar *filename; | |
1562 gchar *tmp, *path; | |
1563 InputPlugin *dec; /* for decoder cache */ | |
1564 | |
1565 g_return_if_fail(filename_p != NULL); | |
1566 g_return_if_fail(playlist != NULL); | |
1567 g_return_if_fail(playlist_name != NULL); | |
1568 | |
1569 filename = g_strchug(g_strdup(filename_p)); | |
1570 | |
1571 if(cfg.convert_slash) | |
1572 while ((tmp = strchr(filename, '\\')) != NULL) | |
1573 *tmp = '/'; | |
1574 | |
1575 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1576 path = g_strdup(playlist_name); | |
1577 if ((tmp = strrchr(path, '/'))) | |
1578 *tmp = '\0'; | |
1579 else { | |
1580 if (playlist->loading_playlist != TRUE || cfg.playlist_detect == FALSE) | |
1581 dec = input_check_file(filename, FALSE); | |
1582 else | |
1583 dec = NULL; | |
1584 | |
1585 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1586 return; | |
1587 } | |
1588 tmp = g_build_filename(path, filename, NULL); | |
1589 | |
1590 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1591 dec = input_check_file(tmp, FALSE); | |
1592 else | |
1593 dec = NULL; | |
1594 | |
1595 __playlist_ins_with_info(playlist, tmp, pos, title, len, dec); | |
1596 g_free(tmp); | |
1597 g_free(path); | |
1598 } | |
1599 else | |
1600 { | |
1601 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1602 dec = input_check_file(filename, FALSE); | |
1603 else | |
1604 dec = NULL; | |
1605 | |
1606 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1607 } | |
1608 | |
1609 g_free(filename); | |
1610 } | |
1611 | |
1612 void | |
1613 playlist_load_ins_file_tuple(Playlist * playlist, | |
1614 const gchar * filename_p, | |
1615 const gchar * playlist_name, | |
1616 gint pos, | |
1617 TitleInput *tuple) | |
1618 { | |
1619 gchar *filename; | |
1620 gchar *tmp, *path; | |
1621 InputPlugin *dec; /* for decoder cache */ | |
1622 | |
1623 g_return_if_fail(filename_p != NULL); | |
1624 g_return_if_fail(playlist_name != NULL); | |
1625 g_return_if_fail(playlist != NULL); | |
1626 | |
1627 filename = g_strchug(g_strdup(filename_p)); | |
1628 | |
1629 while ((tmp = strchr(filename, '\\')) != NULL) | |
1630 *tmp = '/'; | |
1631 | |
1632 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1633 path = g_strdup(playlist_name); | |
1634 if ((tmp = strrchr(path, '/'))) | |
1635 *tmp = '\0'; | |
1636 else { | |
1637 if (playlist->loading_playlist != TRUE || cfg.playlist_detect == FALSE) | |
1638 dec = input_check_file(filename, FALSE); | |
1639 else | |
1640 dec = NULL; | |
1641 | |
1642 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1643 return; | |
1644 } | |
1645 tmp = g_build_filename(path, filename, NULL); | |
1646 | |
1647 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1648 dec = input_check_file(tmp, FALSE); | |
1649 else | |
1650 dec = NULL; | |
1651 | |
1652 __playlist_ins_with_info_tuple(playlist, tmp, pos, tuple, dec); | |
1653 g_free(tmp); | |
1654 g_free(path); | |
1655 } | |
1656 else | |
1657 { | |
1658 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1659 dec = input_check_file(filename, FALSE); | |
1660 else | |
1661 dec = NULL; | |
1662 | |
1663 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1664 } | |
1665 | |
1666 g_free(filename); | |
1667 } | |
1668 | |
1669 static guint | |
1670 playlist_load_ins(Playlist * playlist, const gchar * filename, gint pos) | |
1671 { | |
1672 PlaylistContainer *plc; | |
1673 gchar *ext; | |
1674 | |
1675 g_return_val_if_fail(playlist != NULL, 0); | |
1676 g_return_val_if_fail(filename != NULL, 0); | |
1677 | |
1678 ext = strrchr(filename, '.') + 1; | |
1679 plc = playlist_container_find(ext); | |
1680 | |
1681 g_return_val_if_fail(plc != NULL, 0); | |
1682 g_return_val_if_fail(plc->plc_read != NULL, 0); | |
1683 | |
1684 plc->plc_read(filename, pos); | |
1685 | |
1686 playlist_generate_shuffle_list(playlist); | |
1687 playlistwin_update_list(playlist); | |
1688 | |
1689 return 1; | |
1690 } | |
1691 | |
1692 GList * | |
1693 get_playlist_nth(Playlist *playlist, guint nth) | |
1694 { | |
1695 g_warning("deprecated function get_playlist_nth() was called"); | |
1696 REQUIRE_LOCK(playlist->mutex); | |
1697 return g_list_nth(playlist->entries, nth); | |
1698 } | |
1699 | |
1700 gint | |
1701 playlist_get_position_nolock(Playlist *playlist) | |
1702 { | |
1703 if (playlist && playlist->position) | |
1704 return g_list_index(playlist->entries, playlist->position); | |
1705 return 0; | |
1706 } | |
1707 | |
1708 gint | |
1709 playlist_get_position(Playlist *playlist) | |
1710 { | |
1711 gint pos; | |
1712 | |
1713 PLAYLIST_LOCK(playlist->mutex); | |
1714 pos = playlist_get_position_nolock(playlist); | |
1715 PLAYLIST_UNLOCK(playlist->mutex); | |
1716 | |
1717 return pos; | |
1718 } | |
1719 | |
1720 gchar * | |
1721 playlist_get_filename(Playlist *playlist, guint pos) | |
1722 { | |
1723 gchar *filename; | |
1724 PlaylistEntry *entry; | |
1725 GList *node; | |
1726 | |
1727 if (!playlist) | |
1728 return NULL; | |
1729 | |
1730 PLAYLIST_LOCK(playlist->mutex); | |
1731 node = g_list_nth(playlist->entries, pos); | |
1732 if (!node) { | |
1733 PLAYLIST_UNLOCK(playlist->mutex); | |
1734 return NULL; | |
1735 } | |
1736 entry = node->data; | |
1737 | |
1738 filename = g_strdup(entry->filename); | |
1739 PLAYLIST_UNLOCK(playlist->mutex); | |
1740 | |
1741 return filename; | |
1742 } | |
1743 | |
1744 gchar * | |
1745 playlist_get_songtitle(Playlist *playlist, guint pos) | |
1746 { | |
1747 gchar *title = NULL; | |
1748 PlaylistEntry *entry; | |
1749 GList *node; | |
1750 | |
1751 if (!playlist) | |
1752 return NULL; | |
1753 | |
1754 PLAYLIST_LOCK(playlist->mutex); | |
1755 | |
1756 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1757 PLAYLIST_UNLOCK(playlist->mutex); | |
1758 return NULL; | |
1759 } | |
1760 | |
1761 entry = node->data; | |
1762 | |
1763 /* FIXME: simplify this logic */ | |
1764 if ((entry->title == NULL && entry->length == -1) || | |
1765 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1766 { | |
1767 if (playlist_entry_get_info(entry)) | |
1768 title = entry->title; | |
1769 } | |
1770 else { | |
1771 title = entry->title; | |
1772 } | |
1773 | |
1774 PLAYLIST_UNLOCK(playlist->mutex); | |
1775 | |
1776 if (!title) { | |
1777 title = g_path_get_basename(entry->filename); | |
1778 return str_replace(title, filename_to_utf8(title)); | |
1779 } | |
1780 | |
1781 return str_to_utf8(title); | |
1782 } | |
1783 | |
1784 TitleInput * | |
1785 playlist_get_tuple(Playlist *playlist, guint pos) | |
1786 { | |
1787 PlaylistEntry *entry; | |
1788 TitleInput *tuple = NULL; | |
1789 GList *node; | |
1790 | |
1791 if (!playlist) | |
1792 return NULL; | |
1793 | |
1794 PLAYLIST_LOCK(playlist->mutex); | |
1795 | |
1796 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1797 PLAYLIST_UNLOCK(playlist->mutex); | |
1798 return NULL; | |
1799 } | |
1800 | |
1801 entry = (PlaylistEntry *) node->data; | |
1802 | |
1803 tuple = entry->tuple; | |
1804 | |
1805 // if no tuple or tuple with old mtime, get new one. | |
1806 if (tuple == NULL || | |
1807 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1808 { | |
1809 playlist_entry_get_info(entry); | |
1810 tuple = entry->tuple; | |
1811 } | |
1812 | |
1813 PLAYLIST_UNLOCK(playlist->mutex); | |
1814 | |
1815 return tuple; | |
1816 } | |
1817 | |
1818 gint | |
1819 playlist_get_songtime(Playlist *playlist, guint pos) | |
1820 { | |
1821 gint song_time = -1; | |
1822 PlaylistEntry *entry; | |
1823 GList *node; | |
1824 | |
1825 if (!playlist) | |
1826 return -1; | |
1827 | |
1828 PLAYLIST_LOCK(playlist->mutex); | |
1829 | |
1830 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1831 PLAYLIST_UNLOCK(playlist->mutex); | |
1832 return -1; | |
1833 } | |
1834 | |
1835 entry = node->data; | |
1836 if (entry->tuple == NULL || | |
1837 (entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) { | |
1838 | |
1839 if (playlist_entry_get_info(entry)) | |
1840 song_time = entry->length; | |
1841 | |
1842 PLAYLIST_UNLOCK(playlist->mutex); | |
1843 } | |
1844 else { | |
1845 song_time = entry->length; | |
1846 PLAYLIST_UNLOCK(playlist->mutex); | |
1847 } | |
1848 | |
1849 return song_time; | |
1850 } | |
1851 | |
1852 static gint | |
1853 playlist_compare_track(PlaylistEntry * a, | |
1854 PlaylistEntry * b) | |
1855 { | |
1856 g_return_val_if_fail(a != NULL, 0); | |
1857 g_return_val_if_fail(b != NULL, 0); | |
1858 | |
1859 g_return_val_if_fail(a->tuple != NULL, 0); | |
1860 g_return_val_if_fail(b->tuple != NULL, 0); | |
1861 | |
1862 return (a->tuple->track_number - b->tuple->track_number); | |
1863 } | |
1864 | |
1865 static gint | |
1866 playlist_compare_playlist(PlaylistEntry * a, | |
1867 PlaylistEntry * b) | |
1868 { | |
1869 const gchar *a_title = NULL, *b_title = NULL; | |
1870 | |
1871 g_return_val_if_fail(a != NULL, 0); | |
1872 g_return_val_if_fail(b != NULL, 0); | |
1873 | |
1874 if (a->title != NULL) | |
1875 a_title = a->title; | |
1876 else { | |
1877 if (strrchr(a->filename, '/')) | |
1878 a_title = strrchr(a->filename, '/') + 1; | |
1879 else | |
1880 a_title = a->filename; | |
1881 } | |
1882 | |
1883 if (b->title != NULL) | |
1884 b_title = b->title; | |
1885 else { | |
1886 if (strrchr(a->filename, '/')) | |
1887 b_title = strrchr(b->filename, '/') + 1; | |
1888 else | |
1889 b_title = b->filename; | |
1890 } | |
1891 | |
1892 return strcasecmp(a_title, b_title); | |
1893 } | |
1894 | |
1895 static gint | |
1896 playlist_compare_title(PlaylistEntry * a, | |
1897 PlaylistEntry * b) | |
1898 { | |
1899 const gchar *a_title = NULL, *b_title = NULL; | |
1900 | |
1901 g_return_val_if_fail(a != NULL, 0); | |
1902 g_return_val_if_fail(b != NULL, 0); | |
1903 | |
1904 if (a->tuple != NULL && a->tuple->track_name != NULL) | |
1905 a_title = a->tuple->track_name; | |
1906 if (b->tuple != NULL && b->tuple->track_name != NULL) | |
1907 b_title = b->tuple->track_name; | |
1908 | |
1909 if (a_title != NULL && b_title != NULL) | |
1910 return strcasecmp(a_title, b_title); | |
1911 | |
1912 if (a->title != NULL) | |
1913 a_title = a->title; | |
1914 else { | |
1915 if (strrchr(a->filename, '/')) | |
1916 a_title = strrchr(a->filename, '/') + 1; | |
1917 else | |
1918 a_title = a->filename; | |
1919 } | |
1920 | |
1921 if (b->title != NULL) | |
1922 b_title = b->title; | |
1923 else { | |
1924 if (strrchr(a->filename, '/')) | |
1925 b_title = strrchr(b->filename, '/') + 1; | |
1926 else | |
1927 b_title = b->filename; | |
1928 } | |
1929 | |
1930 return strcasecmp(a_title, b_title); | |
1931 } | |
1932 | |
1933 static gint | |
1934 playlist_compare_artist(PlaylistEntry * a, | |
1935 PlaylistEntry * b) | |
1936 { | |
1937 const gchar *a_artist = NULL, *b_artist = NULL; | |
1938 | |
1939 g_return_val_if_fail(a != NULL, 0); | |
1940 g_return_val_if_fail(b != NULL, 0); | |
1941 | |
1942 if (a->tuple != NULL) | |
1943 playlist_entry_get_info(a); | |
1944 | |
1945 if (b->tuple != NULL) | |
1946 playlist_entry_get_info(b); | |
1947 | |
1948 if (a->tuple != NULL && a->tuple->performer != NULL) | |
1949 a_artist = a->tuple->performer; | |
1950 if (b->tuple != NULL && b->tuple->performer != NULL) | |
1951 b_artist = b->tuple->performer; | |
1952 | |
1953 if (a_artist != NULL && b_artist != NULL) | |
1954 return strcasecmp(a_artist, b_artist); | |
1955 | |
1956 return 0; | |
1957 } | |
1958 | |
1959 static gint | |
1960 playlist_compare_filename(PlaylistEntry * a, | |
1961 PlaylistEntry * b) | |
1962 { | |
1963 gchar *a_filename, *b_filename; | |
1964 | |
1965 g_return_val_if_fail(a != NULL, 0); | |
1966 g_return_val_if_fail(b != NULL, 0); | |
1967 | |
1968 if (strrchr(a->filename, '/')) | |
1969 a_filename = strrchr(a->filename, '/') + 1; | |
1970 else | |
1971 a_filename = a->filename; | |
1972 | |
1973 if (strrchr(b->filename, '/')) | |
1974 b_filename = strrchr(b->filename, '/') + 1; | |
1975 else | |
1976 b_filename = b->filename; | |
1977 | |
1978 | |
1979 return strcasecmp(a_filename, b_filename); | |
1980 } | |
1981 | |
1982 static gint | |
1983 path_compare(const gchar * a, const gchar * b) | |
1984 { | |
1985 gchar *posa, *posb; | |
1986 gint len, ret; | |
1987 | |
1988 posa = strrchr(a, '/'); | |
1989 posb = strrchr(b, '/'); | |
1990 | |
1991 /* | |
1992 * Sort directories before files | |
1993 */ | |
1994 if (posa && posb && (posa - a != posb - b)) { | |
1995 if (posa - a > posb - b) { | |
1996 len = posb - b; | |
1997 ret = -1; | |
1998 } | |
1999 else { | |
2000 len = posa - a; | |
2001 ret = 1; | |
2002 } | |
2003 if (!strncasecmp(a, b, len)) | |
2004 return ret; | |
2005 } | |
2006 return strcasecmp(a, b); | |
2007 } | |
2008 | |
2009 static gint | |
2010 playlist_compare_path(PlaylistEntry * a, | |
2011 PlaylistEntry * b) | |
2012 { | |
2013 return path_compare(a->filename, b->filename); | |
2014 } | |
2015 | |
2016 | |
2017 static time_t | |
2018 playlist_get_mtime(const gchar *filename) | |
2019 { | |
2020 struct stat buf; | |
2021 gint rv; | |
2022 | |
2023 rv = stat(filename, &buf); | |
2024 | |
2025 if (rv == 0) { | |
2026 return buf.st_mtime; | |
2027 } else { | |
2028 return 0; //error | |
2029 } | |
2030 } | |
2031 | |
2032 | |
2033 static gint | |
2034 playlist_compare_date(PlaylistEntry * a, | |
2035 PlaylistEntry * b) | |
2036 { | |
2037 struct stat buf; | |
2038 time_t modtime; | |
2039 | |
2040 gint rv; | |
2041 | |
2042 | |
2043 rv = stat(a->filename, &buf); | |
2044 | |
2045 if (rv == 0) { | |
2046 modtime = buf.st_mtime; | |
2047 rv = stat(b->filename, &buf); | |
2048 | |
2049 if (stat(b->filename, &buf) == 0) { | |
2050 if (buf.st_mtime == modtime) | |
2051 return 0; | |
2052 else | |
2053 return (buf.st_mtime - modtime) > 0 ? -1 : 1; | |
2054 } | |
2055 else | |
2056 return -1; | |
2057 } | |
2058 else if (!lstat(b->filename, &buf)) | |
2059 return 1; | |
2060 else | |
2061 return playlist_compare_filename(a, b); | |
2062 } | |
2063 | |
2064 | |
2065 void | |
2066 playlist_sort(Playlist *playlist, PlaylistSortType type) | |
2067 { | |
2068 playlist_remove_dead_files(playlist); | |
2069 PLAYLIST_LOCK(playlist->mutex); | |
2070 playlist->entries = | |
2071 g_list_sort(playlist->entries, | |
2072 (GCompareFunc) playlist_compare_func_table[type]); | |
2073 PLAYLIST_UNLOCK(playlist->mutex); | |
2074 } | |
2075 | |
2076 static GList * | |
2077 playlist_sort_selected_generic(GList * list, GCompareFunc cmpfunc) | |
2078 { | |
2079 GList *list1, *list2; | |
2080 GList *tmp_list = NULL; | |
2081 GList *index_list = NULL; | |
2082 | |
2083 /* | |
2084 * We take all the selected entries out of the playlist, | |
2085 * sorts them, and then put them back in again. | |
2086 */ | |
2087 | |
2088 list1 = g_list_last(list); | |
2089 | |
2090 while (list1) { | |
2091 list2 = g_list_previous(list1); | |
2092 if (PLAYLIST_ENTRY(list1->data)->selected) { | |
2093 gpointer idx; | |
2094 idx = GINT_TO_POINTER(g_list_position(list, list1)); | |
2095 index_list = g_list_prepend(index_list, idx); | |
2096 list = g_list_remove_link(list, list1); | |
2097 tmp_list = g_list_concat(list1, tmp_list); | |
2098 } | |
2099 list1 = list2; | |
2100 } | |
2101 | |
2102 tmp_list = g_list_sort(tmp_list, cmpfunc); | |
2103 list1 = tmp_list; | |
2104 list2 = index_list; | |
2105 | |
2106 while (list2) { | |
2107 if (!list1) { | |
2108 g_critical(G_STRLOC ": Error during list sorting. " | |
2109 "Possibly dropped some playlist-entries."); | |
2110 break; | |
2111 } | |
2112 | |
2113 list = g_list_insert(list, list1->data, GPOINTER_TO_INT(list2->data)); | |
2114 | |
2115 list2 = g_list_next(list2); | |
2116 list1 = g_list_next(list1); | |
2117 } | |
2118 | |
2119 g_list_free(index_list); | |
2120 g_list_free(tmp_list); | |
2121 | |
2122 return list; | |
2123 } | |
2124 | |
2125 void | |
2126 playlist_sort_selected(Playlist *playlist, PlaylistSortType type) | |
2127 { | |
2128 PLAYLIST_LOCK(playlist->mutex); | |
2129 playlist->entries = playlist_sort_selected_generic(playlist->entries, (GCompareFunc) | |
2130 playlist_compare_func_table | |
2131 [type]); | |
2132 PLAYLIST_UNLOCK(playlist->mutex); | |
2133 } | |
2134 | |
2135 void | |
2136 playlist_reverse(Playlist *playlist) | |
2137 { | |
2138 PLAYLIST_LOCK(playlist->mutex); | |
2139 playlist->entries = g_list_reverse(playlist->entries); | |
2140 PLAYLIST_UNLOCK(playlist->mutex); | |
2141 } | |
2142 | |
2143 static GList * | |
2144 playlist_shuffle_list(Playlist *playlist, GList * list) | |
2145 { | |
2146 /* | |
2147 * Note that this doesn't make a copy of the original list. | |
2148 * The pointer to the original list is not valid after this | |
2149 * fuction is run. | |
2150 */ | |
2151 gint len = g_list_length(list); | |
2152 gint i, j; | |
2153 GList *node, **ptrs; | |
2154 | |
2155 if (!playlist) | |
2156 return NULL; | |
2157 | |
2158 REQUIRE_LOCK(playlist->mutex); | |
2159 | |
2160 if (!len) | |
2161 return NULL; | |
2162 | |
2163 ptrs = g_new(GList *, len); | |
2164 | |
2165 for (node = list, i = 0; i < len; node = g_list_next(node), i++) | |
2166 ptrs[i] = node; | |
2167 | |
2168 j = g_random_int_range(0, len); | |
2169 list = ptrs[j]; | |
2170 ptrs[j]->next = NULL; | |
2171 ptrs[j] = ptrs[0]; | |
2172 | |
2173 for (i = 1; i < len; i++) { | |
2174 j = g_random_int_range(0, len - i); | |
2175 list->prev = ptrs[i + j]; | |
2176 ptrs[i + j]->next = list; | |
2177 list = ptrs[i + j]; | |
2178 ptrs[i + j] = ptrs[i]; | |
2179 } | |
2180 list->prev = NULL; | |
2181 | |
2182 g_free(ptrs); | |
2183 | |
2184 return list; | |
2185 } | |
2186 | |
2187 void | |
2188 playlist_random(Playlist *playlist) | |
2189 { | |
2190 PLAYLIST_LOCK(playlist->mutex); | |
2191 playlist->entries = playlist_shuffle_list(playlist, playlist->entries); | |
2192 PLAYLIST_UNLOCK(playlist->mutex); | |
2193 } | |
2194 | |
2195 GList * | |
2196 playlist_get_selected(Playlist *playlist) | |
2197 { | |
2198 GList *node, *list = NULL; | |
2199 gint i = 0; | |
2200 | |
2201 PLAYLIST_LOCK(playlist->mutex); | |
2202 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2203 PlaylistEntry *entry = node->data; | |
2204 if (entry->selected) | |
2205 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
2206 } | |
2207 PLAYLIST_UNLOCK(playlist->mutex); | |
2208 return g_list_reverse(list); | |
2209 } | |
2210 | |
2211 void | |
2212 playlist_clear_selected(Playlist *playlist) | |
2213 { | |
2214 GList *node = NULL; | |
2215 gint i = 0; | |
2216 | |
2217 PLAYLIST_LOCK(playlist->mutex); | |
2218 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2219 PLAYLIST_ENTRY(node->data)->selected = FALSE; | |
2220 } | |
2221 PLAYLIST_UNLOCK(playlist->mutex); | |
2222 playlist_recalc_total_time(playlist); | |
2223 playlist_manager_update(); | |
2224 } | |
2225 | |
2226 gint | |
2227 playlist_get_num_selected(Playlist *playlist) | |
2228 { | |
2229 GList *node; | |
2230 gint num = 0; | |
2231 | |
2232 PLAYLIST_LOCK(playlist->mutex); | |
2233 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2234 PlaylistEntry *entry = node->data; | |
2235 if (entry->selected) | |
2236 num++; | |
2237 } | |
2238 PLAYLIST_UNLOCK(playlist->mutex); | |
2239 return num; | |
2240 } | |
2241 | |
2242 | |
2243 static void | |
2244 playlist_generate_shuffle_list(Playlist *playlist) | |
2245 { | |
2246 PLAYLIST_LOCK(playlist->mutex); | |
2247 playlist_generate_shuffle_list_nolock(playlist); | |
2248 PLAYLIST_UNLOCK(playlist->mutex); | |
2249 } | |
2250 | |
2251 static void | |
2252 playlist_generate_shuffle_list_nolock(Playlist *playlist) | |
2253 { | |
2254 GList *node; | |
2255 gint numsongs; | |
2256 | |
2257 if (!cfg.shuffle || !playlist) | |
2258 return; | |
2259 | |
2260 REQUIRE_LOCK(playlist->mutex); | |
2261 | |
2262 if (playlist->shuffle) { | |
2263 g_list_free(playlist->shuffle); | |
2264 playlist->shuffle = NULL; | |
2265 } | |
2266 | |
2267 playlist->shuffle = playlist_shuffle_list(playlist, g_list_copy(playlist->entries)); | |
2268 numsongs = g_list_length(playlist->shuffle); | |
2269 | |
2270 if (playlist->position) { | |
2271 gint i = g_list_index(playlist->shuffle, playlist->position); | |
2272 node = g_list_nth(playlist->shuffle, i); | |
2273 playlist->shuffle = g_list_remove_link(playlist->shuffle, node); | |
2274 playlist->shuffle = g_list_prepend(playlist->shuffle, node->data); | |
2275 } | |
2276 } | |
2277 | |
2278 void | |
2279 playlist_fileinfo(Playlist *playlist, guint pos) | |
2280 { | |
2281 gchar *path = NULL; | |
2282 GList *node; | |
2283 PlaylistEntry *entry = NULL; | |
2284 TitleInput *tuple = NULL; | |
2285 | |
2286 PLAYLIST_LOCK(playlist->mutex); | |
2287 | |
2288 if ((node = g_list_nth(playlist->entries, pos))) | |
2289 { | |
2290 entry = node->data; | |
2291 tuple = entry->tuple; | |
2292 path = g_strdup(entry->filename); | |
2293 } | |
2294 | |
2295 PLAYLIST_UNLOCK(playlist->mutex); | |
2296 | |
2297 /* No tuple? Try to set this entry up properly. --nenolod */ | |
2298 if (entry->tuple == NULL || entry->tuple->mtime == -1 || | |
2299 entry->tuple->mtime == 0 || entry->tuple->mtime != playlist_get_mtime(entry->filename)) | |
2300 { | |
2301 playlist_entry_get_info(entry); | |
2302 tuple = entry->tuple; | |
2303 } | |
2304 | |
2305 if (tuple != NULL) | |
2306 { | |
2307 if (entry->decoder == NULL) | |
2308 entry->decoder = input_check_file(entry->filename, FALSE); /* try to find a decoder */ | |
2309 | |
2310 if (entry->decoder != NULL && entry->decoder->file_info_box == NULL) | |
2311 fileinfo_show_for_tuple(tuple); | |
2312 else if (entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2313 entry->decoder->file_info_box(path); | |
2314 else | |
2315 fileinfo_show_for_path(path); | |
2316 g_free(path); | |
2317 } | |
2318 else if (path != NULL) | |
2319 { | |
2320 if (entry != NULL && entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2321 entry->decoder->file_info_box(path); | |
2322 else | |
2323 fileinfo_show_for_path(path); | |
2324 g_free(path); | |
2325 } | |
2326 } | |
2327 | |
2328 void | |
2329 playlist_fileinfo_current(Playlist *playlist) | |
2330 { | |
2331 gchar *path = NULL; | |
2332 TitleInput *tuple = NULL; | |
2333 | |
2334 PLAYLIST_LOCK(playlist->mutex); | |
2335 | |
2336 if (playlist->entries && playlist->position) | |
2337 { | |
2338 path = g_strdup(playlist->position->filename); | |
2339 if (( playlist->position->tuple == NULL ) || ( playlist->position->decoder == NULL )) | |
2340 playlist_entry_get_info(playlist->position); | |
2341 tuple = playlist->position->tuple; | |
2342 } | |
2343 | |
2344 PLAYLIST_UNLOCK(playlist->mutex); | |
2345 | |
2346 if (tuple != NULL) | |
2347 { | |
2348 if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box == NULL) | |
2349 fileinfo_show_for_tuple(tuple); | |
2350 else if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2351 playlist->position->decoder->file_info_box(path); | |
2352 else | |
2353 fileinfo_show_for_path(path); | |
2354 g_free(path); | |
2355 } | |
2356 else if (path != NULL) | |
2357 { | |
2358 if (playlist->position != NULL && playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2359 playlist->position->decoder->file_info_box(path); | |
2360 else | |
2361 fileinfo_show_for_path(path); | |
2362 g_free(path); | |
2363 } | |
2364 } | |
2365 | |
2366 | |
2367 static gboolean | |
2368 playlist_get_info_is_going(void) | |
2369 { | |
2370 gboolean result; | |
2371 | |
2372 G_LOCK(playlist_get_info_going); | |
2373 result = playlist_get_info_going; | |
2374 G_UNLOCK(playlist_get_info_going); | |
2375 | |
2376 return result; | |
2377 } | |
2378 | |
2379 static gpointer | |
2380 playlist_get_info_func(gpointer arg) | |
2381 { | |
2382 GList *node; | |
2383 gboolean update_playlistwin = FALSE; | |
2384 gboolean update_mainwin = FALSE; | |
2385 | |
2386 while (playlist_get_info_is_going()) { | |
2387 PlaylistEntry *entry; | |
2388 Playlist *playlist = playlist_get_active(); | |
2389 | |
2390 // on_load | |
2391 if (cfg.use_pl_metadata && | |
2392 cfg.get_info_on_load && | |
2393 playlist_get_info_scan_active) { | |
2394 | |
2395 PLAYLIST_LOCK(playlist->mutex); | |
2396 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2397 entry = node->data; | |
2398 | |
2399 if(entry->tuple && (entry->tuple->length > -1)) { | |
2400 update_playlistwin = TRUE; | |
2401 continue; | |
2402 } | |
2403 | |
2404 if (!playlist_entry_get_info(entry)) { | |
2405 if (g_list_index(playlist->entries, entry) == -1) | |
2406 /* Entry disappeared while we looked it up. | |
2407 Restart. */ | |
2408 node = playlist->entries; | |
2409 } | |
2410 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { | |
2411 update_playlistwin = TRUE; | |
2412 if (entry == playlist->position) | |
2413 update_mainwin = TRUE; | |
2414 break; | |
2415 } | |
2416 } | |
2417 PLAYLIST_UNLOCK(playlist->mutex); | |
2418 | |
2419 if (!node) { | |
2420 g_mutex_lock(mutex_scan); | |
2421 playlist_get_info_scan_active = FALSE; | |
2422 g_mutex_unlock(mutex_scan); | |
2423 } | |
2424 } // on_load | |
2425 | |
2426 // on_demand | |
2427 else if (!cfg.get_info_on_load && | |
2428 cfg.get_info_on_demand && | |
2429 cfg.playlist_visible && | |
2430 !cfg.playlist_shaded && | |
2431 cfg.use_pl_metadata) { | |
2432 | |
2433 g_mutex_lock(mutex_scan); | |
2434 playlist_get_info_scan_active = FALSE; | |
2435 g_mutex_unlock(mutex_scan); | |
2436 | |
2437 PLAYLIST_LOCK(playlist->mutex); | |
2438 | |
2439 if (!playlist->entries) { | |
2440 PLAYLIST_UNLOCK(playlist->mutex); | |
2441 } | |
2442 else { | |
2443 for (node = g_list_nth(playlist->entries, playlistwin_get_toprow()); | |
2444 node && playlistwin_item_visible(g_list_position(playlist->entries, node)); | |
2445 node = g_list_next(node)) { | |
2446 | |
2447 entry = node->data; | |
2448 | |
2449 if(entry->tuple && (entry->tuple->length > -1)) { | |
2450 update_playlistwin = TRUE; | |
2451 continue; | |
2452 } | |
2453 | |
2454 if (!playlist_entry_get_info(entry)) { | |
2455 if (g_list_index(playlist->entries, entry) == -1) | |
2456 /* Entry disapeared while we | |
2457 looked it up. Restart. */ | |
2458 node = | |
2459 g_list_nth(playlist->entries, | |
2460 playlistwin_get_toprow()); | |
2461 } | |
2462 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { | |
2463 update_playlistwin = TRUE; | |
2464 if (entry == playlist->position) | |
2465 update_mainwin = TRUE; | |
2466 // no need for break here since this iteration is very short. | |
2467 } | |
2468 } | |
2469 PLAYLIST_UNLOCK(playlist->mutex); | |
2470 } | |
2471 } // on_demand | |
2472 else if (cfg.get_info_on_demand && | |
2473 (!cfg.playlist_visible || cfg.playlist_shaded | |
2474 || !cfg.use_pl_metadata)) | |
2475 { | |
2476 g_mutex_lock(mutex_scan); | |
2477 playlist_get_info_scan_active = FALSE; | |
2478 g_mutex_unlock(mutex_scan); | |
2479 } | |
2480 else /* not on_demand and not on_load... | |
2481 NOTE: this shouldn't happen anymore, sanity check in bmp_config_load now */ | |
2482 { | |
2483 g_mutex_lock(mutex_scan); | |
2484 playlist_get_info_scan_active = FALSE; | |
2485 g_mutex_unlock(mutex_scan); | |
2486 } | |
2487 | |
2488 if (update_playlistwin) { | |
2489 playlistwin_update_list(playlist); | |
2490 update_playlistwin = FALSE; | |
2491 } | |
2492 | |
2493 if (update_mainwin) { | |
2494 mainwin_set_info_text(); | |
2495 update_mainwin = FALSE; | |
2496 } | |
2497 | |
2498 if (playlist_get_info_scan_active) { | |
2499 continue; | |
2500 } | |
2501 | |
2502 g_mutex_lock(mutex_scan); | |
2503 g_cond_wait(cond_scan, mutex_scan); | |
2504 g_mutex_unlock(mutex_scan); | |
2505 | |
2506 } // while | |
2507 | |
2508 g_thread_exit(NULL); | |
2509 return NULL; | |
2510 } | |
2511 | |
2512 void | |
2513 playlist_start_get_info_thread(void) | |
2514 { | |
2515 G_LOCK(playlist_get_info_going); | |
2516 playlist_get_info_going = TRUE; | |
2517 G_UNLOCK(playlist_get_info_going); | |
2518 | |
2519 playlist_get_info_thread = g_thread_create(playlist_get_info_func, | |
2520 NULL, TRUE, NULL); | |
2521 } | |
2522 | |
2523 void | |
2524 playlist_stop_get_info_thread(void) | |
2525 { | |
2526 G_LOCK(playlist_get_info_going); | |
2527 playlist_get_info_going = FALSE; | |
2528 G_UNLOCK(playlist_get_info_going); | |
2529 | |
2530 g_cond_broadcast(cond_scan); | |
2531 g_thread_join(playlist_get_info_thread); | |
2532 } | |
2533 | |
2534 void | |
2535 playlist_start_get_info_scan(void) | |
2536 { | |
2537 g_mutex_lock(mutex_scan); | |
2538 playlist_get_info_scan_active = TRUE; | |
2539 g_mutex_unlock(mutex_scan); | |
2540 g_cond_signal(cond_scan); | |
2541 } | |
2542 | |
2543 void | |
2544 playlist_remove_dead_files(Playlist *playlist) | |
2545 { | |
2546 GList *node, *next_node; | |
2547 | |
2548 PLAYLIST_LOCK(playlist->mutex); | |
2549 | |
2550 for (node = playlist->entries; node; node = next_node) { | |
2551 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2552 next_node = g_list_next(node); | |
2553 | |
2554 if (!entry || !entry->filename) { | |
2555 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2556 continue; | |
2557 } | |
2558 | |
2559 /* FIXME: What about 'file:///'? */ | |
2560 /* Don't kill URLs */ | |
2561 if (strstr(entry->filename, "://")) | |
2562 continue; | |
2563 | |
2564 /* FIXME: Should test for readability */ | |
2565 if (vfs_file_test(entry->filename, G_FILE_TEST_EXISTS)) | |
2566 continue; | |
2567 | |
2568 if (entry == playlist->position) { | |
2569 /* Don't remove the currently playing song */ | |
2570 if (playback_get_playing()) | |
2571 continue; | |
2572 | |
2573 if (next_node) | |
2574 playlist->position = PLAYLIST_ENTRY(next_node->data); | |
2575 else | |
2576 playlist->position = NULL; | |
2577 } | |
2578 | |
2579 playlist_entry_free(entry); | |
2580 playlist->entries = g_list_delete_link(playlist->entries, node); | |
2581 } | |
2582 | |
2583 PLAYLIST_UNLOCK(playlist->mutex); | |
2584 | |
2585 playlist_generate_shuffle_list(playlist); | |
2586 playlistwin_update_list(playlist); | |
2587 playlist_recalc_total_time(playlist); | |
2588 playlist_manager_update(); | |
2589 } | |
2590 | |
2591 | |
2592 static gint | |
2593 playlist_dupscmp_title(PlaylistEntry * a, | |
2594 PlaylistEntry * b) | |
2595 { | |
2596 const gchar *a_title, *b_title; | |
2597 | |
2598 g_return_val_if_fail(a != NULL, 0); | |
2599 g_return_val_if_fail(b != NULL, 0); | |
2600 | |
2601 if (a->title) | |
2602 a_title = a->title; | |
2603 else { | |
2604 if (strrchr(a->filename, '/')) | |
2605 a_title = strrchr(a->filename, '/') + 1; | |
2606 else | |
2607 a_title = a->filename; | |
2608 } | |
2609 | |
2610 if (b->title) | |
2611 b_title = b->title; | |
2612 else { | |
2613 if (strrchr(a->filename, '/')) | |
2614 b_title = strrchr(b->filename, '/') + 1; | |
2615 else | |
2616 b_title = b->filename; | |
2617 } | |
2618 | |
2619 return strcmp(a_title, b_title); | |
2620 } | |
2621 | |
2622 static gint | |
2623 playlist_dupscmp_filename(PlaylistEntry * a, | |
2624 PlaylistEntry * b ) | |
2625 { | |
2626 gchar *a_filename, *b_filename; | |
2627 | |
2628 g_return_val_if_fail(a != NULL, 0); | |
2629 g_return_val_if_fail(b != NULL, 0); | |
2630 | |
2631 if (strrchr(a->filename, '/')) | |
2632 a_filename = strrchr(a->filename, '/') + 1; | |
2633 else | |
2634 a_filename = a->filename; | |
2635 | |
2636 if (strrchr(b->filename, '/')) | |
2637 b_filename = strrchr(b->filename, '/') + 1; | |
2638 else | |
2639 b_filename = b->filename; | |
2640 | |
2641 return strcmp(a_filename, b_filename); | |
2642 } | |
2643 | |
2644 static gint | |
2645 playlist_dupscmp_path(PlaylistEntry * a, | |
2646 PlaylistEntry * b) | |
2647 { | |
2648 /* simply compare the entire filename string */ | |
2649 return strcmp(a->filename, b->filename); | |
2650 } | |
2651 | |
2652 void | |
2653 playlist_remove_duplicates(Playlist *playlist, PlaylistDupsType type) | |
2654 { | |
2655 GList *node, *next_node; | |
2656 GList *node_cmp, *next_node_cmp; | |
2657 gint (*dups_compare_func)(PlaylistEntry * , PlaylistEntry *); | |
2658 | |
2659 switch ( type ) | |
2660 { | |
2661 case PLAYLIST_DUPS_TITLE: | |
2662 dups_compare_func = playlist_dupscmp_title; | |
2663 break; | |
2664 case PLAYLIST_DUPS_PATH: | |
2665 dups_compare_func = playlist_dupscmp_path; | |
2666 break; | |
2667 case PLAYLIST_DUPS_FILENAME: | |
2668 default: | |
2669 dups_compare_func = playlist_dupscmp_filename; | |
2670 break; | |
2671 } | |
2672 | |
2673 PLAYLIST_LOCK(playlist->mutex); | |
2674 | |
2675 for (node = playlist->entries; node; node = next_node) { | |
2676 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2677 next_node = g_list_next(node); | |
2678 | |
2679 if (!entry || !entry->filename) { | |
2680 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2681 continue; | |
2682 } | |
2683 | |
2684 for (node_cmp = next_node; node_cmp; node_cmp = next_node_cmp) { | |
2685 PlaylistEntry *entry_cmp = PLAYLIST_ENTRY(node_cmp->data); | |
2686 next_node_cmp = g_list_next(node_cmp); | |
2687 | |
2688 if (!entry_cmp || !entry_cmp->filename) { | |
2689 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2690 continue; | |
2691 } | |
2692 | |
2693 /* compare using the chosen dups_compare_func */ | |
2694 if ( !dups_compare_func( entry , entry_cmp ) ) { | |
2695 | |
2696 if (entry_cmp == playlist->position) { | |
2697 /* Don't remove the currently playing song */ | |
2698 if (playback_get_playing()) | |
2699 continue; | |
2700 | |
2701 if (next_node_cmp) | |
2702 playlist->position = PLAYLIST_ENTRY(next_node_cmp->data); | |
2703 else | |
2704 playlist->position = NULL; | |
2705 } | |
2706 | |
2707 /* check if this was the next item of the external | |
2708 loop; if true, replace it with the next of the next*/ | |
2709 if ( node_cmp == next_node ) | |
2710 next_node = g_list_next(next_node); | |
2711 | |
2712 playlist_entry_free(entry_cmp); | |
2713 playlist->entries = g_list_delete_link(playlist->entries, node_cmp); | |
2714 } | |
2715 } | |
2716 } | |
2717 | |
2718 PLAYLIST_UNLOCK(playlist->mutex); | |
2719 | |
2720 playlistwin_update_list(playlist); | |
2721 playlist_recalc_total_time(playlist); | |
2722 | |
2723 playlist_manager_update(); | |
2724 } | |
2725 | |
2726 void | |
2727 playlist_get_total_time(Playlist * playlist, | |
2728 gulong * total_time, | |
2729 gulong * selection_time, | |
2730 gboolean * total_more, | |
2731 gboolean * selection_more) | |
2732 { | |
2733 PLAYLIST_LOCK(playlist->mutex); | |
2734 *total_time = playlist->pl_total_time; | |
2735 *selection_time = playlist->pl_selection_time; | |
2736 *total_more = playlist->pl_total_more; | |
2737 *selection_more = playlist->pl_selection_more; | |
2738 PLAYLIST_UNLOCK(playlist->mutex); | |
2739 } | |
2740 | |
2741 | |
2742 static void | |
2743 playlist_recalc_total_time_nolock(Playlist *playlist) | |
2744 { | |
2745 GList *list; | |
2746 PlaylistEntry *entry; | |
2747 | |
2748 REQUIRE_LOCK(playlist->mutex); | |
2749 | |
2750 playlist->pl_total_time = 0; | |
2751 playlist->pl_selection_time = 0; | |
2752 playlist->pl_total_more = FALSE; | |
2753 playlist->pl_selection_more = FALSE; | |
2754 | |
2755 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2756 entry = list->data; | |
2757 | |
2758 if (entry->length != -1) | |
2759 playlist->pl_total_time += entry->length / 1000; | |
2760 else | |
2761 playlist->pl_total_more = TRUE; | |
2762 | |
2763 if (entry->selected) { | |
2764 if (entry->length != -1) | |
2765 playlist->pl_selection_time += entry->length / 1000; | |
2766 else | |
2767 playlist->pl_selection_more = TRUE; | |
2768 } | |
2769 } | |
2770 } | |
2771 | |
2772 static void | |
2773 playlist_recalc_total_time(Playlist *playlist) | |
2774 { | |
2775 PLAYLIST_LOCK(playlist->mutex); | |
2776 playlist_recalc_total_time_nolock(playlist); | |
2777 PLAYLIST_UNLOCK(playlist->mutex); | |
2778 } | |
2779 | |
2780 gint | |
2781 playlist_select_search( Playlist *playlist , TitleInput *tuple , gint action ) | |
2782 { | |
2783 GList *entry_list = NULL, *found_list = NULL, *sel_list = NULL; | |
2784 gboolean is_first_search = TRUE; | |
2785 gint num_of_entries_found = 0; | |
2786 | |
2787 #if defined(USE_REGEX_ONIGURUMA) | |
2788 /* set encoding for Oniguruma regex to UTF-8 */ | |
2789 reg_set_encoding( REG_POSIX_ENCODING_UTF8 ); | |
2790 onig_set_default_syntax( ONIG_SYNTAX_POSIX_BASIC ); | |
2791 #endif | |
2792 | |
2793 PLAYLIST_LOCK(playlist->mutex); | |
2794 | |
2795 if ( tuple->track_name != NULL ) | |
2796 { | |
2797 /* match by track_name */ | |
2798 const gchar *regex_pattern = tuple->track_name; | |
2799 regex_t regex; | |
2800 #if defined(USE_REGEX_PCRE) | |
2801 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2802 #else | |
2803 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2804 #endif | |
2805 { | |
2806 GList *tfound_list = NULL; | |
2807 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2808 else entry_list = found_list; /* use found_list */ | |
2809 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2810 { | |
2811 PlaylistEntry *entry = entry_list->data; | |
2812 if ( ( entry->tuple != NULL ) && ( entry->tuple->track_name != NULL ) && | |
2813 ( regexec( ®ex , entry->tuple->track_name , 0 , NULL , 0 ) == 0 ) ) | |
2814 { | |
2815 tfound_list = g_list_append( tfound_list , entry ); | |
2816 } | |
2817 } | |
2818 g_list_free( found_list ); /* wipe old found_list */ | |
2819 found_list = tfound_list; /* move tfound_list in found_list */ | |
2820 regfree( ®ex ); | |
2821 } | |
2822 is_first_search = FALSE; | |
2823 } | |
2824 | |
2825 if ( tuple->album_name != NULL ) | |
2826 { | |
2827 /* match by album_name */ | |
2828 const gchar *regex_pattern = tuple->album_name; | |
2829 regex_t regex; | |
2830 #if defined(USE_REGEX_PCRE) | |
2831 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2832 #else | |
2833 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2834 #endif | |
2835 { | |
2836 GList *tfound_list = NULL; | |
2837 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2838 else entry_list = found_list; /* use found_list */ | |
2839 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2840 { | |
2841 PlaylistEntry *entry = entry_list->data; | |
2842 if ( ( entry->tuple != NULL ) && ( entry->tuple->album_name != NULL ) && | |
2843 ( regexec( ®ex , entry->tuple->album_name , 0 , NULL , 0 ) == 0 ) ) | |
2844 { | |
2845 tfound_list = g_list_append( tfound_list , entry ); | |
2846 } | |
2847 } | |
2848 g_list_free( found_list ); /* wipe old found_list */ | |
2849 found_list = tfound_list; /* move tfound_list in found_list */ | |
2850 regfree( ®ex ); | |
2851 } | |
2852 is_first_search = FALSE; | |
2853 } | |
2854 | |
2855 if ( tuple->performer != NULL ) | |
2856 { | |
2857 /* match by performer */ | |
2858 const gchar *regex_pattern = tuple->performer; | |
2859 regex_t regex; | |
2860 #if defined(USE_REGEX_PCRE) | |
2861 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2862 #else | |
2863 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2864 #endif | |
2865 { | |
2866 GList *tfound_list = NULL; | |
2867 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2868 else entry_list = found_list; /* use found_list */ | |
2869 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2870 { | |
2871 PlaylistEntry *entry = entry_list->data; | |
2872 if ( ( entry->tuple != NULL ) && ( entry->tuple->performer != NULL ) && | |
2873 ( regexec( ®ex , entry->tuple->performer , 0 , NULL , 0 ) == 0 ) ) | |
2874 { | |
2875 tfound_list = g_list_append( tfound_list , entry ); | |
2876 } | |
2877 } | |
2878 g_list_free( found_list ); /* wipe old found_list */ | |
2879 found_list = tfound_list; /* move tfound_list in found_list */ | |
2880 regfree( ®ex ); | |
2881 } | |
2882 is_first_search = FALSE; | |
2883 } | |
2884 | |
2885 if ( tuple->file_name != NULL ) | |
2886 { | |
2887 /* match by file_name */ | |
2888 const gchar *regex_pattern = tuple->file_name; | |
2889 regex_t regex; | |
2890 #if defined(USE_REGEX_PCRE) | |
2891 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2892 #else | |
2893 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2894 #endif | |
2895 { | |
2896 GList *tfound_list = NULL; | |
2897 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2898 else entry_list = found_list; /* use found_list */ | |
2899 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2900 { | |
2901 PlaylistEntry *entry = entry_list->data; | |
2902 if ( ( entry->tuple != NULL ) && ( entry->tuple->file_name != NULL ) && | |
2903 ( regexec( ®ex , entry->tuple->file_name , 0 , NULL , 0 ) == 0 ) ) | |
2904 { | |
2905 tfound_list = g_list_append( tfound_list , entry ); | |
2906 } | |
2907 } | |
2908 g_list_free( found_list ); /* wipe old found_list */ | |
2909 found_list = tfound_list; /* move tfound_list in found_list */ | |
2910 regfree( ®ex ); | |
2911 } | |
2912 is_first_search = FALSE; | |
2913 } | |
2914 | |
2915 /* NOTE: action = 0 -> default behaviour, select all matching entries */ | |
2916 /* if some entries are still in found_list, those | |
2917 are what the user is searching for; select them */ | |
2918 for ( sel_list = found_list ; sel_list ; sel_list = g_list_next(sel_list) ) | |
2919 { | |
2920 PlaylistEntry *entry = sel_list->data; | |
2921 entry->selected = TRUE; | |
2922 num_of_entries_found++; | |
2923 } | |
2924 | |
2925 g_list_free( found_list ); | |
2926 | |
2927 PLAYLIST_UNLOCK(playlist->mutex); | |
2928 playlist_recalc_total_time(playlist); | |
2929 | |
2930 return num_of_entries_found; | |
2931 } | |
2932 | |
2933 void | |
2934 playlist_select_all(Playlist *playlist, gboolean set) | |
2935 { | |
2936 GList *list; | |
2937 | |
2938 PLAYLIST_LOCK(playlist->mutex); | |
2939 | |
2940 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2941 PlaylistEntry *entry = list->data; | |
2942 entry->selected = set; | |
2943 } | |
2944 | |
2945 PLAYLIST_UNLOCK(playlist->mutex); | |
2946 playlist_recalc_total_time(playlist); | |
2947 } | |
2948 | |
2949 void | |
2950 playlist_select_invert_all(Playlist *playlist) | |
2951 { | |
2952 GList *list; | |
2953 | |
2954 PLAYLIST_LOCK(playlist->mutex); | |
2955 | |
2956 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2957 PlaylistEntry *entry = list->data; | |
2958 entry->selected = !entry->selected; | |
2959 } | |
2960 | |
2961 PLAYLIST_UNLOCK(playlist->mutex); | |
2962 playlist_recalc_total_time(playlist); | |
2963 } | |
2964 | |
2965 gboolean | |
2966 playlist_select_invert(Playlist *playlist, guint pos) | |
2967 { | |
2968 GList *list; | |
2969 gboolean invert_ok = FALSE; | |
2970 | |
2971 PLAYLIST_LOCK(playlist->mutex); | |
2972 | |
2973 if ((list = g_list_nth(playlist->entries, pos))) { | |
2974 PlaylistEntry *entry = list->data; | |
2975 entry->selected = !entry->selected; | |
2976 invert_ok = TRUE; | |
2977 } | |
2978 | |
2979 PLAYLIST_UNLOCK(playlist->mutex); | |
2980 playlist_recalc_total_time(playlist); | |
2981 | |
2982 return invert_ok; | |
2983 } | |
2984 | |
2985 | |
2986 void | |
2987 playlist_select_range(Playlist *playlist, gint min_pos, gint max_pos, gboolean select) | |
2988 { | |
2989 GList *list; | |
2990 gint i; | |
2991 | |
2992 if (min_pos > max_pos) | |
2993 SWAP(min_pos, max_pos); | |
2994 | |
2995 PLAYLIST_LOCK(playlist->mutex); | |
2996 | |
2997 list = g_list_nth(playlist->entries, min_pos); | |
2998 for (i = min_pos; i <= max_pos && list; i++) { | |
2999 PlaylistEntry *entry = list->data; | |
3000 entry->selected = select; | |
3001 list = g_list_next(list); | |
3002 } | |
3003 | |
3004 PLAYLIST_UNLOCK(playlist->mutex); | |
3005 | |
3006 playlist_recalc_total_time(playlist); | |
3007 } | |
3008 | |
3009 gboolean | |
3010 playlist_read_info_selection(Playlist *playlist) | |
3011 { | |
3012 GList *node; | |
3013 gboolean retval = FALSE; | |
3014 | |
3015 PLAYLIST_LOCK(playlist->mutex); | |
3016 | |
3017 for (node = playlist->entries; node; node = g_list_next(node)) { | |
3018 PlaylistEntry *entry = node->data; | |
3019 if (!entry->selected) | |
3020 continue; | |
3021 | |
3022 retval = TRUE; | |
3023 | |
3024 str_replace_in(&entry->title, NULL); | |
3025 entry->length = -1; | |
3026 | |
3027 /* invalidate mtime to reread */ | |
3028 if (entry->tuple != NULL) | |
3029 entry->tuple->mtime = -1; /* -1 denotes "non-initialized". now 0 is for stream etc. yaz */ | |
3030 | |
3031 if (!playlist_entry_get_info(entry)) { | |
3032 if (g_list_index(playlist->entries, entry) == -1) | |
3033 /* Entry disappeared while we looked it up. Restart. */ | |
3034 node = playlist->entries; | |
3035 } | |
3036 } | |
3037 | |
3038 PLAYLIST_UNLOCK(playlist->mutex); | |
3039 | |
3040 playlistwin_update_list(playlist); | |
3041 playlist_recalc_total_time(playlist); | |
3042 | |
3043 return retval; | |
3044 } | |
3045 | |
3046 void | |
3047 playlist_read_info(Playlist *playlist, guint pos) | |
3048 { | |
3049 GList *node; | |
3050 | |
3051 PLAYLIST_LOCK(playlist->mutex); | |
3052 | |
3053 if ((node = g_list_nth(playlist->entries, pos))) { | |
3054 PlaylistEntry *entry = node->data; | |
3055 str_replace_in(&entry->title, NULL); | |
3056 entry->length = -1; | |
3057 playlist_entry_get_info(entry); | |
3058 } | |
3059 | |
3060 PLAYLIST_UNLOCK(playlist->mutex); | |
3061 | |
3062 playlistwin_update_list(playlist); | |
3063 playlist_recalc_total_time(playlist); | |
3064 } | |
3065 | |
3066 Playlist * | |
3067 playlist_get_active(void) | |
3068 { | |
3069 if (playlists_iter != NULL) | |
3070 return (Playlist *) playlists_iter->data; | |
3071 | |
3072 return (Playlist *) playlists->data; | |
3073 } | |
3074 | |
3075 void | |
3076 playlist_set_shuffle(gboolean shuffle) | |
3077 { | |
3078 Playlist *playlist = playlist_get_active(); | |
3079 if (!playlist) | |
3080 return; | |
3081 | |
3082 PLAYLIST_LOCK(playlist->mutex); | |
3083 | |
3084 playlist_position_before_jump = NULL; | |
3085 | |
3086 cfg.shuffle = shuffle; | |
3087 playlist_generate_shuffle_list_nolock(playlist); | |
3088 | |
3089 PLAYLIST_UNLOCK(playlist->mutex); | |
3090 } | |
3091 | |
3092 Playlist * | |
3093 playlist_new(void) | |
3094 { | |
3095 Playlist *playlist = g_new0(Playlist, 1); | |
3096 playlist->mutex = g_mutex_new(); | |
3097 playlist->loading_playlist = FALSE; | |
3098 | |
3099 playlist_set_current_name(playlist, NULL); | |
3100 playlist_clear(playlist); | |
3101 | |
3102 return playlist; | |
3103 } | |
3104 | |
3105 void | |
3106 playlist_free(Playlist *playlist) | |
3107 { | |
3108 g_mutex_free( playlist->mutex ); | |
3109 g_free( playlist ); | |
3110 return; | |
3111 } | |
3112 | |
3113 Playlist * | |
3114 playlist_new_from_selected(void) | |
3115 { | |
3116 Playlist *newpl = playlist_new(); | |
3117 Playlist *playlist = playlist_get_active(); | |
3118 GList *list = playlist_get_selected(playlist); | |
3119 | |
3120 playlist_add_playlist( newpl ); | |
3121 | |
3122 PLAYLIST_LOCK(playlist->mutex); | |
3123 | |
3124 while ( list != NULL ) | |
3125 { | |
3126 PlaylistEntry *entry = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(list->data)); | |
3127 if ( entry->filename != NULL ) /* paranoid? oh well... */ | |
3128 playlist_add( newpl , entry->filename ); | |
3129 list = g_list_next(list); | |
3130 } | |
3131 | |
3132 PLAYLIST_UNLOCK(playlist->mutex); | |
3133 | |
3134 playlist_recalc_total_time(newpl); | |
3135 playlistwin_update_list(playlist); | |
3136 | |
3137 return newpl; | |
3138 } | |
3139 | |
3140 const gchar * | |
3141 playlist_get_filename_to_play(Playlist *playlist) | |
3142 { | |
3143 const gchar *filename = NULL; | |
3144 | |
3145 if (!playlist) | |
3146 return NULL; | |
3147 | |
3148 PLAYLIST_LOCK(playlist->mutex); | |
3149 | |
3150 if (!playlist->position) { | |
3151 if (cfg.shuffle) | |
3152 playlist->position = playlist->shuffle->data; | |
3153 else | |
3154 playlist->position = playlist->entries->data; | |
3155 } | |
3156 | |
3157 filename = playlist->position->filename; | |
3158 | |
3159 PLAYLIST_UNLOCK(playlist->mutex); | |
3160 | |
3161 return filename; | |
3162 } | |
3163 | |
3164 PlaylistEntry * | |
3165 playlist_get_entry_to_play(Playlist *playlist) | |
3166 { | |
3167 if (!playlist) | |
3168 return NULL; | |
3169 | |
3170 PLAYLIST_LOCK(playlist->mutex); | |
3171 | |
3172 if (!playlist->position) { | |
3173 if (cfg.shuffle) | |
3174 playlist->position = playlist->shuffle->data; | |
3175 else | |
3176 playlist->position = playlist->entries->data; | |
3177 } | |
3178 | |
3179 PLAYLIST_UNLOCK(playlist->mutex); | |
3180 | |
3181 return playlist->position; | |
3182 } |