Mercurial > audlegacy
annotate src/audacious/playlist.c @ 2480:bb2f191895ce trunk
[svn] - fixed crash when skipping to next song with an empty playlist
author | marvin |
---|---|
date | Tue, 06 Feb 2007 06:06:58 -0800 |
parents | 5e8526a4f554 |
children | c5075a79f1aa |
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; | |
977 } | |
978 | |
979 PLAYLIST_UNLOCK(playlist->mutex); | |
980 | |
981 playlist_recalc_total_time(playlist); | |
982 | |
983 mainwin_set_song_info(rate, freq, nch); | |
2460 | 984 |
985 if ( playlist->position ) | |
986 hook_call( "playlist set info" , playlist->position ); | |
2313 | 987 } |
988 | |
989 void | |
990 playlist_set_info(Playlist * playlist, const gchar * title, gint length, gint rate, | |
991 gint freq, gint nch) | |
992 { | |
993 PLAYLIST_LOCK(playlist->mutex); | |
994 | |
995 g_return_if_fail(playlist != NULL); | |
996 | |
997 if (playlist->position) { | |
998 g_free(playlist->position->title); | |
999 playlist->position->title = g_strdup(title); | |
1000 playlist->position->length = length; | |
1001 } | |
1002 | |
1003 PLAYLIST_UNLOCK(playlist->mutex); | |
1004 | |
1005 playlist_recalc_total_time(playlist); | |
1006 | |
1007 mainwin_set_song_info(rate, freq, nch); | |
2460 | 1008 |
1009 if ( playlist->position ) | |
1010 hook_call( "playlist set info" , playlist->position ); | |
2313 | 1011 } |
1012 | |
1013 void | |
1014 playlist_check_pos_current(Playlist *playlist) | |
1015 { | |
1016 gint pos, row, bottom; | |
1017 | |
1018 if (!playlist) | |
1019 return; | |
1020 | |
1021 PLAYLIST_LOCK(playlist->mutex); | |
1022 if (!playlist->position || !playlistwin_list) { | |
1023 PLAYLIST_UNLOCK(playlist->mutex); | |
1024 return; | |
1025 } | |
1026 | |
1027 pos = g_list_index(playlist->entries, playlist->position); | |
1028 | |
1029 if (playlistwin_item_visible(pos)) { | |
1030 PLAYLIST_UNLOCK(playlist->mutex); | |
1031 return; | |
1032 } | |
1033 | |
1034 bottom = MAX(0, playlist_get_length_nolock(playlist) - | |
1035 playlistwin_list->pl_num_visible); | |
1036 row = CLAMP(pos - playlistwin_list->pl_num_visible / 2, 0, bottom); | |
1037 PLAYLIST_UNLOCK(playlist->mutex); | |
1038 playlistwin_set_toprow(row); | |
1039 g_cond_signal(cond_scan); | |
1040 } | |
1041 | |
1042 void | |
1043 playlist_next(Playlist *playlist) | |
1044 { | |
1045 GList *plist_pos_list; | |
1046 gboolean restart_playing = FALSE; | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1047 if (!playlist_get_length(playlist)) |
2313 | 1048 return; |
1049 | |
1050 PLAYLIST_LOCK(playlist->mutex); | |
1051 | |
1052 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1053 { | |
1054 playlist->position = playlist_position_before_jump; | |
1055 playlist_position_before_jump = NULL; | |
1056 } | |
1057 | |
1058 plist_pos_list = find_playlist_position_list(playlist); | |
1059 | |
1060 if (!cfg.repeat && !g_list_next(plist_pos_list) && playlist->queue == NULL) { | |
1061 PLAYLIST_UNLOCK(playlist->mutex); | |
1062 return; | |
1063 } | |
1064 | |
1065 if (playback_get_playing()) { | |
1066 /* We need to stop before changing playlist_position */ | |
1067 PLAYLIST_UNLOCK(playlist->mutex); | |
1068 ip_data.stop = TRUE; | |
1069 playback_stop(); | |
1070 ip_data.stop = FALSE; | |
1071 PLAYLIST_LOCK(playlist->mutex); | |
1072 restart_playing = TRUE; | |
1073 } | |
1074 | |
1075 plist_pos_list = find_playlist_position_list(playlist); | |
1076 if (playlist->queue != NULL) | |
1077 play_queued(playlist); | |
1078 else if (g_list_next(plist_pos_list)) | |
1079 playlist->position = g_list_next(plist_pos_list)->data; | |
1080 else if (cfg.repeat) { | |
1081 playlist->position = NULL; | |
1082 playlist_generate_shuffle_list_nolock(playlist); | |
1083 if (cfg.shuffle) | |
1084 playlist->position = playlist->shuffle->data; | |
1085 else | |
1086 playlist->position = playlist->entries->data; | |
1087 } | |
1088 PLAYLIST_UNLOCK(playlist->mutex); | |
1089 playlist_check_pos_current(playlist); | |
1090 | |
1091 if (restart_playing) | |
1092 playback_initiate(); | |
1093 else { | |
1094 mainwin_set_info_text(); | |
1095 playlistwin_update_list(playlist); | |
1096 } | |
1097 } | |
1098 | |
1099 void | |
1100 playlist_prev(Playlist *playlist) | |
1101 { | |
1102 GList *plist_pos_list; | |
1103 gboolean restart_playing = FALSE; | |
1104 | |
2480
bb2f191895ce
[svn] - fixed crash when skipping to next song with an empty playlist
marvin
parents:
2460
diff
changeset
|
1105 if (!playlist_get_length(playlist)) |
2313 | 1106 return; |
1107 | |
1108 PLAYLIST_LOCK(playlist->mutex); | |
1109 | |
1110 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1111 { | |
1112 playlist->position = playlist_position_before_jump; | |
1113 playlist_position_before_jump = NULL; | |
1114 } | |
1115 | |
1116 plist_pos_list = find_playlist_position_list(playlist); | |
1117 | |
1118 if (!cfg.repeat && !g_list_previous(plist_pos_list)) { | |
1119 PLAYLIST_UNLOCK(playlist->mutex); | |
1120 return; | |
1121 } | |
1122 | |
1123 if (playback_get_playing()) { | |
1124 /* We need to stop before changing playlist_position */ | |
1125 PLAYLIST_UNLOCK(playlist->mutex); | |
1126 ip_data.stop = TRUE; | |
1127 playback_stop(); | |
1128 ip_data.stop = FALSE; | |
1129 PLAYLIST_LOCK(playlist->mutex); | |
1130 restart_playing = TRUE; | |
1131 } | |
1132 | |
1133 plist_pos_list = find_playlist_position_list(playlist); | |
1134 if (g_list_previous(plist_pos_list)) { | |
1135 playlist->position = g_list_previous(plist_pos_list)->data; | |
1136 } | |
1137 else if (cfg.repeat) { | |
1138 GList *node; | |
1139 playlist->position = NULL; | |
1140 playlist_generate_shuffle_list_nolock(playlist); | |
1141 if (cfg.shuffle) | |
1142 node = g_list_last(playlist->shuffle); | |
1143 else | |
1144 node = g_list_last(playlist->entries); | |
1145 if (node) | |
1146 playlist->position = node->data; | |
1147 } | |
1148 | |
1149 PLAYLIST_UNLOCK(playlist->mutex); | |
1150 | |
1151 playlist_check_pos_current(playlist); | |
1152 | |
1153 if (restart_playing) | |
1154 playback_initiate(); | |
1155 else { | |
1156 mainwin_set_info_text(); | |
1157 playlistwin_update_list(playlist); | |
1158 } | |
1159 } | |
1160 | |
1161 void | |
1162 playlist_queue(Playlist *playlist) | |
1163 { | |
1164 GList *list = playlist_get_selected(playlist); | |
1165 GList *it = list; | |
1166 | |
1167 PLAYLIST_LOCK(playlist->mutex); | |
1168 | |
1169 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1170 { | |
1171 /* Shuffling and this is our first manual jump. */ | |
1172 playlist_position_before_jump = playlist->position; | |
1173 } | |
1174 | |
1175 while (it) { | |
1176 GList *next = g_list_next(it); | |
1177 GList *tmp; | |
1178 | |
1179 /* XXX: WTF? --nenolod */ | |
1180 it->data = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(it->data)); | |
1181 if ((tmp = g_list_find(playlist->queue, it->data))) { | |
1182 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1183 g_list_free_1(tmp); | |
1184 list = g_list_remove_link(list, it); | |
1185 g_list_free_1(it); | |
1186 } | |
1187 | |
1188 it = next; | |
1189 } | |
1190 | |
1191 playlist->queue = g_list_concat(playlist->queue, list); | |
1192 | |
1193 PLAYLIST_UNLOCK(playlist->mutex); | |
1194 | |
1195 playlist_recalc_total_time(playlist); | |
1196 playlistwin_update_list(playlist); | |
1197 } | |
1198 | |
1199 void | |
1200 playlist_queue_position(Playlist *playlist, guint pos) | |
1201 { | |
1202 GList *tmp; | |
1203 PlaylistEntry *entry; | |
1204 | |
1205 PLAYLIST_LOCK(playlist->mutex); | |
1206 | |
1207 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1208 { | |
1209 /* Shuffling and this is our first manual jump. */ | |
1210 playlist_position_before_jump = playlist->position; | |
1211 } | |
1212 | |
1213 entry = g_list_nth_data(playlist->entries, pos); | |
1214 if ((tmp = g_list_find(playlist->queue, entry))) { | |
1215 playlist->queue = g_list_remove_link(playlist->queue, tmp); | |
1216 g_list_free_1(tmp); | |
1217 } | |
1218 else | |
1219 playlist->queue = g_list_append(playlist->queue, entry); | |
1220 PLAYLIST_UNLOCK(playlist->mutex); | |
1221 | |
1222 playlist_recalc_total_time(playlist); | |
1223 playlistwin_update_list(playlist); | |
1224 } | |
1225 | |
1226 gboolean | |
1227 playlist_is_position_queued(Playlist *playlist, guint pos) | |
1228 { | |
1229 PlaylistEntry *entry; | |
1230 GList *tmp; | |
1231 | |
1232 PLAYLIST_LOCK(playlist->mutex); | |
1233 entry = g_list_nth_data(playlist->entries, pos); | |
1234 tmp = g_list_find(playlist->queue, entry); | |
1235 PLAYLIST_UNLOCK(playlist->mutex); | |
1236 | |
1237 return tmp != NULL; | |
1238 } | |
1239 | |
1240 gint | |
1241 playlist_get_queue_position_number(Playlist *playlist, guint pos) | |
1242 { | |
1243 PlaylistEntry *entry; | |
1244 gint tmp; | |
1245 | |
1246 PLAYLIST_LOCK(playlist->mutex); | |
1247 entry = g_list_nth_data(playlist->entries, pos); | |
1248 tmp = g_list_index(playlist->queue, entry); | |
1249 PLAYLIST_UNLOCK(playlist->mutex); | |
1250 | |
1251 return tmp; | |
1252 } | |
1253 | |
1254 gint | |
1255 playlist_get_queue_qposition_number(Playlist *playlist, guint pos) | |
1256 { | |
1257 PlaylistEntry *entry; | |
1258 gint tmp; | |
1259 | |
1260 PLAYLIST_LOCK(playlist->mutex); | |
1261 entry = g_list_nth_data(playlist->queue, pos); | |
1262 tmp = g_list_index(playlist->entries, entry); | |
1263 PLAYLIST_UNLOCK(playlist->mutex); | |
1264 | |
1265 return tmp; | |
1266 } | |
1267 | |
1268 void | |
1269 playlist_clear_queue(Playlist *playlist) | |
1270 { | |
1271 PLAYLIST_LOCK(playlist->mutex); | |
1272 g_list_free(playlist->queue); | |
1273 playlist->queue = NULL; | |
1274 PLAYLIST_UNLOCK(playlist->mutex); | |
1275 | |
1276 playlist_recalc_total_time(playlist); | |
1277 playlistwin_update_list(playlist); | |
1278 } | |
1279 | |
1280 void | |
1281 playlist_queue_remove(Playlist *playlist, guint pos) | |
1282 { | |
1283 void *entry; | |
1284 | |
1285 PLAYLIST_LOCK(playlist->mutex); | |
1286 entry = g_list_nth_data(playlist->entries, pos); | |
1287 playlist->queue = g_list_remove(playlist->queue, entry); | |
1288 PLAYLIST_UNLOCK(playlist->mutex); | |
1289 | |
1290 playlistwin_update_list(playlist); | |
1291 } | |
1292 | |
1293 gint | |
1294 playlist_get_queue_position(Playlist *playlist, PlaylistEntry * entry) | |
1295 { | |
1296 return g_list_index(playlist->queue, entry); | |
1297 } | |
1298 | |
1299 void | |
1300 playlist_set_position(Playlist *playlist, guint pos) | |
1301 { | |
1302 GList *node; | |
1303 gboolean restart_playing = FALSE; | |
1304 | |
1305 if (!playlist) | |
1306 return; | |
1307 | |
1308 PLAYLIST_LOCK(playlist->mutex); | |
1309 | |
1310 node = g_list_nth(playlist->entries, pos); | |
1311 if (!node) { | |
1312 PLAYLIST_UNLOCK(playlist->mutex); | |
1313 return; | |
1314 } | |
1315 | |
1316 if (playback_get_playing()) { | |
1317 /* We need to stop before changing playlist_position */ | |
1318 PLAYLIST_UNLOCK(playlist->mutex); | |
1319 ip_data.stop = TRUE; | |
1320 playback_stop(); | |
1321 ip_data.stop = FALSE; | |
1322 PLAYLIST_LOCK(playlist->mutex); | |
1323 restart_playing = TRUE; | |
1324 } | |
1325 | |
1326 if ((cfg.shuffle) && (playlist_position_before_jump == NULL)) | |
1327 { | |
1328 /* Shuffling and this is our first manual jump. */ | |
1329 playlist_position_before_jump = playlist->position; | |
1330 } | |
1331 | |
1332 playlist->position = node->data; | |
1333 PLAYLIST_UNLOCK(playlist->mutex); | |
1334 playlist_check_pos_current(playlist); | |
1335 | |
1336 if (restart_playing) | |
1337 playback_initiate(); | |
1338 else { | |
1339 mainwin_set_info_text(); | |
1340 playlistwin_update_list(playlist); | |
1341 } | |
1342 } | |
1343 | |
1344 void | |
1345 playlist_eof_reached(Playlist *playlist) | |
1346 { | |
1347 GList *plist_pos_list; | |
1348 | |
1349 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1350 ip_data.stop = TRUE; | |
1351 playback_stop(); | |
1352 if ((cfg.no_playlist_advance && !cfg.repeat) || cfg.stopaftersong) | |
1353 ip_data.stop = FALSE; | |
1354 | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1355 hook_call("playback end", playlist->position); |
2407
1dc1d36d0347
[svn] - add hooks: playback begin, playback end, playlist reached end
nenolod
parents:
2380
diff
changeset
|
1356 |
2313 | 1357 PLAYLIST_LOCK(playlist->mutex); |
1358 | |
1359 if ((playlist_position_before_jump != NULL) && playlist->queue == NULL) | |
1360 { | |
1361 playlist->position = playlist_position_before_jump; | |
1362 playlist_position_before_jump = NULL; | |
1363 } | |
1364 | |
1365 plist_pos_list = find_playlist_position_list(playlist); | |
1366 | |
1367 if (cfg.no_playlist_advance) { | |
1368 PLAYLIST_UNLOCK(playlist->mutex); | |
1369 mainwin_clear_song_info(); | |
1370 if (cfg.repeat) | |
1371 playback_initiate(); | |
1372 return; | |
1373 } | |
1374 | |
1375 if (cfg.stopaftersong) { | |
1376 PLAYLIST_UNLOCK(playlist->mutex); | |
1377 mainwin_clear_song_info(); | |
1378 mainwin_set_stopaftersong(FALSE); | |
1379 return; | |
1380 } | |
1381 | |
1382 if (playlist->queue != NULL) { | |
1383 play_queued(playlist); | |
1384 } | |
1385 else if (!g_list_next(plist_pos_list)) { | |
1386 if (cfg.shuffle) { | |
1387 playlist->position = NULL; | |
1388 playlist_generate_shuffle_list_nolock(playlist); | |
1389 } | |
2380 | 1390 else if (playlist->entries != NULL) |
2313 | 1391 playlist->position = playlist->entries->data; |
1392 | |
1393 if (!cfg.repeat) { | |
1394 PLAYLIST_UNLOCK(playlist->mutex); | |
2408
b380e84148bb
[svn] - rename some hooks so that they are more logical
nenolod
parents:
2407
diff
changeset
|
1395 hook_call("playlist end reached", playlist->position); |
2313 | 1396 mainwin_clear_song_info(); |
1397 mainwin_set_info_text(); | |
1398 return; | |
1399 } | |
1400 } | |
1401 else | |
1402 playlist->position = g_list_next(plist_pos_list)->data; | |
1403 | |
1404 PLAYLIST_UNLOCK(playlist->mutex); | |
1405 | |
1406 playlist_check_pos_current(playlist); | |
1407 playback_initiate(); | |
1408 mainwin_set_info_text(); | |
1409 playlistwin_update_list(playlist); | |
1410 } | |
1411 | |
1412 gint | |
1413 playlist_get_length(Playlist *playlist) | |
1414 { | |
1415 gint retval; | |
1416 | |
1417 PLAYLIST_LOCK(playlist->mutex); | |
1418 retval = playlist_get_length_nolock(playlist); | |
1419 PLAYLIST_UNLOCK(playlist->mutex); | |
1420 | |
1421 return retval; | |
1422 } | |
1423 | |
1424 gint | |
1425 playlist_queue_get_length(Playlist *playlist) | |
1426 { | |
1427 gint length; | |
1428 | |
1429 PLAYLIST_LOCK(playlist->mutex); | |
1430 length = g_list_length(playlist->queue); | |
1431 PLAYLIST_UNLOCK(playlist->mutex); | |
1432 | |
1433 return length; | |
1434 } | |
1435 | |
1436 gint | |
1437 playlist_get_length_nolock(Playlist *playlist) | |
1438 { | |
1439 return g_list_length(playlist->entries); | |
1440 } | |
1441 | |
1442 gchar * | |
1443 playlist_get_info_text(Playlist *playlist) | |
1444 { | |
1445 gchar *text, *title, *numbers, *length; | |
1446 | |
1447 g_return_val_if_fail(playlist != NULL, NULL); | |
1448 | |
1449 PLAYLIST_LOCK(playlist->mutex); | |
1450 if (!playlist->position) { | |
1451 PLAYLIST_UNLOCK(playlist->mutex); | |
1452 return NULL; | |
1453 } | |
1454 | |
1455 /* FIXME: there should not be a need to do additional conversion, | |
1456 * if playlist is properly maintained */ | |
1457 if (playlist->position->title) { | |
1458 title = str_to_utf8(playlist->position->title); | |
1459 } | |
1460 else { | |
1461 gchar *basename = g_path_get_basename(playlist->position->filename); | |
1462 title = filename_to_utf8(basename); | |
1463 g_free(basename); | |
1464 } | |
1465 | |
1466 /* | |
1467 * If the user don't want numbers in the playlist, don't | |
1468 * display them in other parts of XMMS | |
1469 */ | |
1470 | |
1471 if (cfg.show_numbers_in_pl) | |
1472 numbers = g_strdup_printf("%d. ", playlist_get_position_nolock(playlist) + 1); | |
1473 else | |
1474 numbers = g_strdup(""); | |
1475 | |
1476 if (playlist->position->length != -1) | |
1477 length = g_strdup_printf(" (%d:%-2.2d)", | |
1478 playlist->position->length / 60000, | |
1479 (playlist->position->length / 1000) % 60); | |
1480 else | |
1481 length = g_strdup(""); | |
1482 | |
1483 PLAYLIST_UNLOCK(playlist->mutex); | |
1484 | |
1485 text = convert_title_text(g_strconcat(numbers, title, length, NULL)); | |
1486 | |
1487 g_free(numbers); | |
1488 g_free(title); | |
1489 g_free(length); | |
1490 | |
1491 return text; | |
1492 } | |
1493 | |
1494 gint | |
1495 playlist_get_current_length(Playlist * playlist) | |
1496 { | |
1497 gint len = 0; | |
1498 | |
1499 if (!playlist) | |
1500 return 0; | |
1501 | |
1502 PLAYLIST_LOCK(playlist->mutex); | |
1503 if (playlist->position) | |
1504 len = playlist->position->length; | |
1505 PLAYLIST_UNLOCK(playlist->mutex); | |
1506 | |
1507 return len; | |
1508 } | |
1509 | |
1510 gboolean | |
1511 playlist_save(Playlist * playlist, const gchar * filename) | |
1512 { | |
1513 PlaylistContainer *plc = NULL; | |
1514 gchar *ext; | |
1515 | |
1516 g_return_val_if_fail(playlist != NULL, FALSE); | |
1517 g_return_val_if_fail(filename != NULL, FALSE); | |
1518 | |
1519 ext = strrchr(filename, '.') + 1; | |
1520 | |
1521 playlist_set_current_name(playlist, filename); | |
1522 | |
1523 if ((plc = playlist_container_find(ext)) == NULL) | |
1524 return FALSE; | |
1525 | |
1526 if (plc->plc_write == NULL) | |
1527 return FALSE; | |
1528 | |
1529 plc->plc_write(filename, 0); | |
1530 | |
1531 return TRUE; | |
1532 } | |
1533 | |
1534 gboolean | |
1535 playlist_load(Playlist * playlist, const gchar * filename) | |
1536 { | |
1537 gboolean ret = FALSE; | |
1538 g_return_val_if_fail(playlist != NULL, FALSE); | |
1539 | |
1540 playlist->loading_playlist = TRUE; | |
1541 ret = playlist_load_ins(playlist, filename, -1); | |
1542 playlist->loading_playlist = FALSE; | |
1543 | |
1544 return ret; | |
1545 } | |
1546 | |
1547 void | |
1548 playlist_load_ins_file(Playlist *playlist, | |
1549 const gchar * filename_p, | |
1550 const gchar * playlist_name, gint pos, | |
1551 const gchar * title, gint len) | |
1552 { | |
1553 gchar *filename; | |
1554 gchar *tmp, *path; | |
1555 InputPlugin *dec; /* for decoder cache */ | |
1556 | |
1557 g_return_if_fail(filename_p != NULL); | |
1558 g_return_if_fail(playlist != NULL); | |
1559 g_return_if_fail(playlist_name != NULL); | |
1560 | |
1561 filename = g_strchug(g_strdup(filename_p)); | |
1562 | |
1563 if(cfg.convert_slash) | |
1564 while ((tmp = strchr(filename, '\\')) != NULL) | |
1565 *tmp = '/'; | |
1566 | |
1567 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1568 path = g_strdup(playlist_name); | |
1569 if ((tmp = strrchr(path, '/'))) | |
1570 *tmp = '\0'; | |
1571 else { | |
1572 if (playlist->loading_playlist != TRUE || cfg.playlist_detect == FALSE) | |
1573 dec = input_check_file(filename, FALSE); | |
1574 else | |
1575 dec = NULL; | |
1576 | |
1577 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1578 return; | |
1579 } | |
1580 tmp = g_build_filename(path, filename, NULL); | |
1581 | |
1582 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1583 dec = input_check_file(tmp, FALSE); | |
1584 else | |
1585 dec = NULL; | |
1586 | |
1587 __playlist_ins_with_info(playlist, tmp, pos, title, len, dec); | |
1588 g_free(tmp); | |
1589 g_free(path); | |
1590 } | |
1591 else | |
1592 { | |
1593 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1594 dec = input_check_file(filename, FALSE); | |
1595 else | |
1596 dec = NULL; | |
1597 | |
1598 __playlist_ins_with_info(playlist, filename, pos, title, len, dec); | |
1599 } | |
1600 | |
1601 g_free(filename); | |
1602 } | |
1603 | |
1604 void | |
1605 playlist_load_ins_file_tuple(Playlist * playlist, | |
1606 const gchar * filename_p, | |
1607 const gchar * playlist_name, | |
1608 gint pos, | |
1609 TitleInput *tuple) | |
1610 { | |
1611 gchar *filename; | |
1612 gchar *tmp, *path; | |
1613 InputPlugin *dec; /* for decoder cache */ | |
1614 | |
1615 g_return_if_fail(filename_p != NULL); | |
1616 g_return_if_fail(playlist_name != NULL); | |
1617 g_return_if_fail(playlist != NULL); | |
1618 | |
1619 filename = g_strchug(g_strdup(filename_p)); | |
1620 | |
1621 while ((tmp = strchr(filename, '\\')) != NULL) | |
1622 *tmp = '/'; | |
1623 | |
1624 if (filename[0] != '/' && !strstr(filename, "://")) { | |
1625 path = g_strdup(playlist_name); | |
1626 if ((tmp = strrchr(path, '/'))) | |
1627 *tmp = '\0'; | |
1628 else { | |
1629 if (playlist->loading_playlist != TRUE || cfg.playlist_detect == FALSE) | |
1630 dec = input_check_file(filename, FALSE); | |
1631 else | |
1632 dec = NULL; | |
1633 | |
1634 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1635 return; | |
1636 } | |
1637 tmp = g_build_filename(path, filename, NULL); | |
1638 | |
1639 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1640 dec = input_check_file(tmp, FALSE); | |
1641 else | |
1642 dec = NULL; | |
1643 | |
1644 __playlist_ins_with_info_tuple(playlist, tmp, pos, tuple, dec); | |
1645 g_free(tmp); | |
1646 g_free(path); | |
1647 } | |
1648 else | |
1649 { | |
1650 if (playlist->loading_playlist != TRUE && cfg.playlist_detect != TRUE) | |
1651 dec = input_check_file(filename, FALSE); | |
1652 else | |
1653 dec = NULL; | |
1654 | |
1655 __playlist_ins_with_info_tuple(playlist, filename, pos, tuple, dec); | |
1656 } | |
1657 | |
1658 g_free(filename); | |
1659 } | |
1660 | |
1661 static guint | |
1662 playlist_load_ins(Playlist * playlist, const gchar * filename, gint pos) | |
1663 { | |
1664 PlaylistContainer *plc; | |
1665 gchar *ext; | |
1666 | |
1667 g_return_val_if_fail(playlist != NULL, 0); | |
1668 g_return_val_if_fail(filename != NULL, 0); | |
1669 | |
1670 ext = strrchr(filename, '.') + 1; | |
1671 plc = playlist_container_find(ext); | |
1672 | |
1673 g_return_val_if_fail(plc != NULL, 0); | |
1674 g_return_val_if_fail(plc->plc_read != NULL, 0); | |
1675 | |
1676 plc->plc_read(filename, pos); | |
1677 | |
1678 playlist_generate_shuffle_list(playlist); | |
1679 playlistwin_update_list(playlist); | |
1680 | |
1681 return 1; | |
1682 } | |
1683 | |
1684 GList * | |
1685 get_playlist_nth(Playlist *playlist, guint nth) | |
1686 { | |
1687 g_warning("deprecated function get_playlist_nth() was called"); | |
1688 REQUIRE_LOCK(playlist->mutex); | |
1689 return g_list_nth(playlist->entries, nth); | |
1690 } | |
1691 | |
1692 gint | |
1693 playlist_get_position_nolock(Playlist *playlist) | |
1694 { | |
1695 if (playlist && playlist->position) | |
1696 return g_list_index(playlist->entries, playlist->position); | |
1697 return 0; | |
1698 } | |
1699 | |
1700 gint | |
1701 playlist_get_position(Playlist *playlist) | |
1702 { | |
1703 gint pos; | |
1704 | |
1705 PLAYLIST_LOCK(playlist->mutex); | |
1706 pos = playlist_get_position_nolock(playlist); | |
1707 PLAYLIST_UNLOCK(playlist->mutex); | |
1708 | |
1709 return pos; | |
1710 } | |
1711 | |
1712 gchar * | |
1713 playlist_get_filename(Playlist *playlist, guint pos) | |
1714 { | |
1715 gchar *filename; | |
1716 PlaylistEntry *entry; | |
1717 GList *node; | |
1718 | |
1719 if (!playlist) | |
1720 return NULL; | |
1721 | |
1722 PLAYLIST_LOCK(playlist->mutex); | |
1723 node = g_list_nth(playlist->entries, pos); | |
1724 if (!node) { | |
1725 PLAYLIST_UNLOCK(playlist->mutex); | |
1726 return NULL; | |
1727 } | |
1728 entry = node->data; | |
1729 | |
1730 filename = g_strdup(entry->filename); | |
1731 PLAYLIST_UNLOCK(playlist->mutex); | |
1732 | |
1733 return filename; | |
1734 } | |
1735 | |
1736 gchar * | |
1737 playlist_get_songtitle(Playlist *playlist, guint pos) | |
1738 { | |
1739 gchar *title = NULL; | |
1740 PlaylistEntry *entry; | |
1741 GList *node; | |
1742 | |
1743 if (!playlist) | |
1744 return NULL; | |
1745 | |
1746 PLAYLIST_LOCK(playlist->mutex); | |
1747 | |
1748 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1749 PLAYLIST_UNLOCK(playlist->mutex); | |
1750 return NULL; | |
1751 } | |
1752 | |
1753 entry = node->data; | |
1754 | |
1755 /* FIXME: simplify this logic */ | |
1756 if ((entry->title == NULL && entry->length == -1) || | |
1757 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1758 { | |
1759 if (playlist_entry_get_info(entry)) | |
1760 title = entry->title; | |
1761 } | |
1762 else { | |
1763 title = entry->title; | |
1764 } | |
1765 | |
1766 PLAYLIST_UNLOCK(playlist->mutex); | |
1767 | |
1768 if (!title) { | |
1769 title = g_path_get_basename(entry->filename); | |
1770 return str_replace(title, filename_to_utf8(title)); | |
1771 } | |
1772 | |
1773 return str_to_utf8(title); | |
1774 } | |
1775 | |
1776 TitleInput * | |
1777 playlist_get_tuple(Playlist *playlist, guint pos) | |
1778 { | |
1779 PlaylistEntry *entry; | |
1780 TitleInput *tuple = NULL; | |
1781 GList *node; | |
1782 | |
1783 if (!playlist) | |
1784 return NULL; | |
1785 | |
1786 PLAYLIST_LOCK(playlist->mutex); | |
1787 | |
1788 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1789 PLAYLIST_UNLOCK(playlist->mutex); | |
1790 return NULL; | |
1791 } | |
1792 | |
1793 entry = (PlaylistEntry *) node->data; | |
1794 | |
1795 tuple = entry->tuple; | |
1796 | |
1797 // if no tuple or tuple with old mtime, get new one. | |
1798 if (tuple == NULL || | |
1799 (entry->tuple && entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) | |
1800 { | |
1801 playlist_entry_get_info(entry); | |
1802 tuple = entry->tuple; | |
1803 } | |
1804 | |
1805 PLAYLIST_UNLOCK(playlist->mutex); | |
1806 | |
1807 return tuple; | |
1808 } | |
1809 | |
1810 gint | |
1811 playlist_get_songtime(Playlist *playlist, guint pos) | |
1812 { | |
1813 gint song_time = -1; | |
1814 PlaylistEntry *entry; | |
1815 GList *node; | |
1816 | |
1817 if (!playlist) | |
1818 return -1; | |
1819 | |
1820 PLAYLIST_LOCK(playlist->mutex); | |
1821 | |
1822 if (!(node = g_list_nth(playlist->entries, pos))) { | |
1823 PLAYLIST_UNLOCK(playlist->mutex); | |
1824 return -1; | |
1825 } | |
1826 | |
1827 entry = node->data; | |
1828 if (entry->tuple == NULL || | |
1829 (entry->tuple->mtime != 0 && (entry->tuple->mtime == -1 || entry->tuple->mtime != playlist_get_mtime(entry->filename)))) { | |
1830 | |
1831 if (playlist_entry_get_info(entry)) | |
1832 song_time = entry->length; | |
1833 | |
1834 PLAYLIST_UNLOCK(playlist->mutex); | |
1835 } | |
1836 else { | |
1837 song_time = entry->length; | |
1838 PLAYLIST_UNLOCK(playlist->mutex); | |
1839 } | |
1840 | |
1841 return song_time; | |
1842 } | |
1843 | |
1844 static gint | |
1845 playlist_compare_track(PlaylistEntry * a, | |
1846 PlaylistEntry * b) | |
1847 { | |
1848 g_return_val_if_fail(a != NULL, 0); | |
1849 g_return_val_if_fail(b != NULL, 0); | |
1850 | |
1851 g_return_val_if_fail(a->tuple != NULL, 0); | |
1852 g_return_val_if_fail(b->tuple != NULL, 0); | |
1853 | |
1854 return (a->tuple->track_number - b->tuple->track_number); | |
1855 } | |
1856 | |
1857 static gint | |
1858 playlist_compare_playlist(PlaylistEntry * a, | |
1859 PlaylistEntry * b) | |
1860 { | |
1861 const gchar *a_title = NULL, *b_title = NULL; | |
1862 | |
1863 g_return_val_if_fail(a != NULL, 0); | |
1864 g_return_val_if_fail(b != NULL, 0); | |
1865 | |
1866 if (a->title != NULL) | |
1867 a_title = a->title; | |
1868 else { | |
1869 if (strrchr(a->filename, '/')) | |
1870 a_title = strrchr(a->filename, '/') + 1; | |
1871 else | |
1872 a_title = a->filename; | |
1873 } | |
1874 | |
1875 if (b->title != NULL) | |
1876 b_title = b->title; | |
1877 else { | |
1878 if (strrchr(a->filename, '/')) | |
1879 b_title = strrchr(b->filename, '/') + 1; | |
1880 else | |
1881 b_title = b->filename; | |
1882 } | |
1883 | |
1884 return strcasecmp(a_title, b_title); | |
1885 } | |
1886 | |
1887 static gint | |
1888 playlist_compare_title(PlaylistEntry * a, | |
1889 PlaylistEntry * b) | |
1890 { | |
1891 const gchar *a_title = NULL, *b_title = NULL; | |
1892 | |
1893 g_return_val_if_fail(a != NULL, 0); | |
1894 g_return_val_if_fail(b != NULL, 0); | |
1895 | |
1896 if (a->tuple != NULL && a->tuple->track_name != NULL) | |
1897 a_title = a->tuple->track_name; | |
1898 if (b->tuple != NULL && b->tuple->track_name != NULL) | |
1899 b_title = b->tuple->track_name; | |
1900 | |
1901 if (a_title != NULL && b_title != NULL) | |
1902 return strcasecmp(a_title, b_title); | |
1903 | |
1904 if (a->title != NULL) | |
1905 a_title = a->title; | |
1906 else { | |
1907 if (strrchr(a->filename, '/')) | |
1908 a_title = strrchr(a->filename, '/') + 1; | |
1909 else | |
1910 a_title = a->filename; | |
1911 } | |
1912 | |
1913 if (b->title != NULL) | |
1914 b_title = b->title; | |
1915 else { | |
1916 if (strrchr(a->filename, '/')) | |
1917 b_title = strrchr(b->filename, '/') + 1; | |
1918 else | |
1919 b_title = b->filename; | |
1920 } | |
1921 | |
1922 return strcasecmp(a_title, b_title); | |
1923 } | |
1924 | |
1925 static gint | |
1926 playlist_compare_artist(PlaylistEntry * a, | |
1927 PlaylistEntry * b) | |
1928 { | |
1929 const gchar *a_artist = NULL, *b_artist = NULL; | |
1930 | |
1931 g_return_val_if_fail(a != NULL, 0); | |
1932 g_return_val_if_fail(b != NULL, 0); | |
1933 | |
1934 if (a->tuple != NULL) | |
1935 playlist_entry_get_info(a); | |
1936 | |
1937 if (b->tuple != NULL) | |
1938 playlist_entry_get_info(b); | |
1939 | |
1940 if (a->tuple != NULL && a->tuple->performer != NULL) | |
1941 a_artist = a->tuple->performer; | |
1942 if (b->tuple != NULL && b->tuple->performer != NULL) | |
1943 b_artist = b->tuple->performer; | |
1944 | |
1945 if (a_artist != NULL && b_artist != NULL) | |
1946 return strcasecmp(a_artist, b_artist); | |
1947 | |
1948 return 0; | |
1949 } | |
1950 | |
1951 static gint | |
1952 playlist_compare_filename(PlaylistEntry * a, | |
1953 PlaylistEntry * b) | |
1954 { | |
1955 gchar *a_filename, *b_filename; | |
1956 | |
1957 g_return_val_if_fail(a != NULL, 0); | |
1958 g_return_val_if_fail(b != NULL, 0); | |
1959 | |
1960 if (strrchr(a->filename, '/')) | |
1961 a_filename = strrchr(a->filename, '/') + 1; | |
1962 else | |
1963 a_filename = a->filename; | |
1964 | |
1965 if (strrchr(b->filename, '/')) | |
1966 b_filename = strrchr(b->filename, '/') + 1; | |
1967 else | |
1968 b_filename = b->filename; | |
1969 | |
1970 | |
1971 return strcasecmp(a_filename, b_filename); | |
1972 } | |
1973 | |
1974 static gint | |
1975 path_compare(const gchar * a, const gchar * b) | |
1976 { | |
1977 gchar *posa, *posb; | |
1978 gint len, ret; | |
1979 | |
1980 posa = strrchr(a, '/'); | |
1981 posb = strrchr(b, '/'); | |
1982 | |
1983 /* | |
1984 * Sort directories before files | |
1985 */ | |
1986 if (posa && posb && (posa - a != posb - b)) { | |
1987 if (posa - a > posb - b) { | |
1988 len = posb - b; | |
1989 ret = -1; | |
1990 } | |
1991 else { | |
1992 len = posa - a; | |
1993 ret = 1; | |
1994 } | |
1995 if (!strncasecmp(a, b, len)) | |
1996 return ret; | |
1997 } | |
1998 return strcasecmp(a, b); | |
1999 } | |
2000 | |
2001 static gint | |
2002 playlist_compare_path(PlaylistEntry * a, | |
2003 PlaylistEntry * b) | |
2004 { | |
2005 return path_compare(a->filename, b->filename); | |
2006 } | |
2007 | |
2008 | |
2009 static time_t | |
2010 playlist_get_mtime(const gchar *filename) | |
2011 { | |
2012 struct stat buf; | |
2013 gint rv; | |
2014 | |
2015 rv = stat(filename, &buf); | |
2016 | |
2017 if (rv == 0) { | |
2018 return buf.st_mtime; | |
2019 } else { | |
2020 return 0; //error | |
2021 } | |
2022 } | |
2023 | |
2024 | |
2025 static gint | |
2026 playlist_compare_date(PlaylistEntry * a, | |
2027 PlaylistEntry * b) | |
2028 { | |
2029 struct stat buf; | |
2030 time_t modtime; | |
2031 | |
2032 gint rv; | |
2033 | |
2034 | |
2035 rv = stat(a->filename, &buf); | |
2036 | |
2037 if (rv == 0) { | |
2038 modtime = buf.st_mtime; | |
2039 rv = stat(b->filename, &buf); | |
2040 | |
2041 if (stat(b->filename, &buf) == 0) { | |
2042 if (buf.st_mtime == modtime) | |
2043 return 0; | |
2044 else | |
2045 return (buf.st_mtime - modtime) > 0 ? -1 : 1; | |
2046 } | |
2047 else | |
2048 return -1; | |
2049 } | |
2050 else if (!lstat(b->filename, &buf)) | |
2051 return 1; | |
2052 else | |
2053 return playlist_compare_filename(a, b); | |
2054 } | |
2055 | |
2056 | |
2057 void | |
2058 playlist_sort(Playlist *playlist, PlaylistSortType type) | |
2059 { | |
2060 playlist_remove_dead_files(playlist); | |
2061 PLAYLIST_LOCK(playlist->mutex); | |
2062 playlist->entries = | |
2063 g_list_sort(playlist->entries, | |
2064 (GCompareFunc) playlist_compare_func_table[type]); | |
2065 PLAYLIST_UNLOCK(playlist->mutex); | |
2066 } | |
2067 | |
2068 static GList * | |
2069 playlist_sort_selected_generic(GList * list, GCompareFunc cmpfunc) | |
2070 { | |
2071 GList *list1, *list2; | |
2072 GList *tmp_list = NULL; | |
2073 GList *index_list = NULL; | |
2074 | |
2075 /* | |
2076 * We take all the selected entries out of the playlist, | |
2077 * sorts them, and then put them back in again. | |
2078 */ | |
2079 | |
2080 list1 = g_list_last(list); | |
2081 | |
2082 while (list1) { | |
2083 list2 = g_list_previous(list1); | |
2084 if (PLAYLIST_ENTRY(list1->data)->selected) { | |
2085 gpointer idx; | |
2086 idx = GINT_TO_POINTER(g_list_position(list, list1)); | |
2087 index_list = g_list_prepend(index_list, idx); | |
2088 list = g_list_remove_link(list, list1); | |
2089 tmp_list = g_list_concat(list1, tmp_list); | |
2090 } | |
2091 list1 = list2; | |
2092 } | |
2093 | |
2094 tmp_list = g_list_sort(tmp_list, cmpfunc); | |
2095 list1 = tmp_list; | |
2096 list2 = index_list; | |
2097 | |
2098 while (list2) { | |
2099 if (!list1) { | |
2100 g_critical(G_STRLOC ": Error during list sorting. " | |
2101 "Possibly dropped some playlist-entries."); | |
2102 break; | |
2103 } | |
2104 | |
2105 list = g_list_insert(list, list1->data, GPOINTER_TO_INT(list2->data)); | |
2106 | |
2107 list2 = g_list_next(list2); | |
2108 list1 = g_list_next(list1); | |
2109 } | |
2110 | |
2111 g_list_free(index_list); | |
2112 g_list_free(tmp_list); | |
2113 | |
2114 return list; | |
2115 } | |
2116 | |
2117 void | |
2118 playlist_sort_selected(Playlist *playlist, PlaylistSortType type) | |
2119 { | |
2120 PLAYLIST_LOCK(playlist->mutex); | |
2121 playlist->entries = playlist_sort_selected_generic(playlist->entries, (GCompareFunc) | |
2122 playlist_compare_func_table | |
2123 [type]); | |
2124 PLAYLIST_UNLOCK(playlist->mutex); | |
2125 } | |
2126 | |
2127 void | |
2128 playlist_reverse(Playlist *playlist) | |
2129 { | |
2130 PLAYLIST_LOCK(playlist->mutex); | |
2131 playlist->entries = g_list_reverse(playlist->entries); | |
2132 PLAYLIST_UNLOCK(playlist->mutex); | |
2133 } | |
2134 | |
2135 static GList * | |
2136 playlist_shuffle_list(Playlist *playlist, GList * list) | |
2137 { | |
2138 /* | |
2139 * Note that this doesn't make a copy of the original list. | |
2140 * The pointer to the original list is not valid after this | |
2141 * fuction is run. | |
2142 */ | |
2143 gint len = g_list_length(list); | |
2144 gint i, j; | |
2145 GList *node, **ptrs; | |
2146 | |
2147 if (!playlist) | |
2148 return NULL; | |
2149 | |
2150 REQUIRE_LOCK(playlist->mutex); | |
2151 | |
2152 if (!len) | |
2153 return NULL; | |
2154 | |
2155 ptrs = g_new(GList *, len); | |
2156 | |
2157 for (node = list, i = 0; i < len; node = g_list_next(node), i++) | |
2158 ptrs[i] = node; | |
2159 | |
2160 j = g_random_int_range(0, len); | |
2161 list = ptrs[j]; | |
2162 ptrs[j]->next = NULL; | |
2163 ptrs[j] = ptrs[0]; | |
2164 | |
2165 for (i = 1; i < len; i++) { | |
2166 j = g_random_int_range(0, len - i); | |
2167 list->prev = ptrs[i + j]; | |
2168 ptrs[i + j]->next = list; | |
2169 list = ptrs[i + j]; | |
2170 ptrs[i + j] = ptrs[i]; | |
2171 } | |
2172 list->prev = NULL; | |
2173 | |
2174 g_free(ptrs); | |
2175 | |
2176 return list; | |
2177 } | |
2178 | |
2179 void | |
2180 playlist_random(Playlist *playlist) | |
2181 { | |
2182 PLAYLIST_LOCK(playlist->mutex); | |
2183 playlist->entries = playlist_shuffle_list(playlist, playlist->entries); | |
2184 PLAYLIST_UNLOCK(playlist->mutex); | |
2185 } | |
2186 | |
2187 GList * | |
2188 playlist_get_selected(Playlist *playlist) | |
2189 { | |
2190 GList *node, *list = NULL; | |
2191 gint i = 0; | |
2192 | |
2193 PLAYLIST_LOCK(playlist->mutex); | |
2194 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2195 PlaylistEntry *entry = node->data; | |
2196 if (entry->selected) | |
2197 list = g_list_prepend(list, GINT_TO_POINTER(i)); | |
2198 } | |
2199 PLAYLIST_UNLOCK(playlist->mutex); | |
2200 return g_list_reverse(list); | |
2201 } | |
2202 | |
2203 void | |
2204 playlist_clear_selected(Playlist *playlist) | |
2205 { | |
2206 GList *node = NULL; | |
2207 gint i = 0; | |
2208 | |
2209 PLAYLIST_LOCK(playlist->mutex); | |
2210 for (node = playlist->entries; node; node = g_list_next(node), i++) { | |
2211 PLAYLIST_ENTRY(node->data)->selected = FALSE; | |
2212 } | |
2213 PLAYLIST_UNLOCK(playlist->mutex); | |
2214 playlist_recalc_total_time(playlist); | |
2215 playlist_manager_update(); | |
2216 } | |
2217 | |
2218 gint | |
2219 playlist_get_num_selected(Playlist *playlist) | |
2220 { | |
2221 GList *node; | |
2222 gint num = 0; | |
2223 | |
2224 PLAYLIST_LOCK(playlist->mutex); | |
2225 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2226 PlaylistEntry *entry = node->data; | |
2227 if (entry->selected) | |
2228 num++; | |
2229 } | |
2230 PLAYLIST_UNLOCK(playlist->mutex); | |
2231 return num; | |
2232 } | |
2233 | |
2234 | |
2235 static void | |
2236 playlist_generate_shuffle_list(Playlist *playlist) | |
2237 { | |
2238 PLAYLIST_LOCK(playlist->mutex); | |
2239 playlist_generate_shuffle_list_nolock(playlist); | |
2240 PLAYLIST_UNLOCK(playlist->mutex); | |
2241 } | |
2242 | |
2243 static void | |
2244 playlist_generate_shuffle_list_nolock(Playlist *playlist) | |
2245 { | |
2246 GList *node; | |
2247 gint numsongs; | |
2248 | |
2249 if (!cfg.shuffle || !playlist) | |
2250 return; | |
2251 | |
2252 REQUIRE_LOCK(playlist->mutex); | |
2253 | |
2254 if (playlist->shuffle) { | |
2255 g_list_free(playlist->shuffle); | |
2256 playlist->shuffle = NULL; | |
2257 } | |
2258 | |
2259 playlist->shuffle = playlist_shuffle_list(playlist, g_list_copy(playlist->entries)); | |
2260 numsongs = g_list_length(playlist->shuffle); | |
2261 | |
2262 if (playlist->position) { | |
2263 gint i = g_list_index(playlist->shuffle, playlist->position); | |
2264 node = g_list_nth(playlist->shuffle, i); | |
2265 playlist->shuffle = g_list_remove_link(playlist->shuffle, node); | |
2266 playlist->shuffle = g_list_prepend(playlist->shuffle, node->data); | |
2267 } | |
2268 } | |
2269 | |
2270 void | |
2271 playlist_fileinfo(Playlist *playlist, guint pos) | |
2272 { | |
2273 gchar *path = NULL; | |
2274 GList *node; | |
2275 PlaylistEntry *entry = NULL; | |
2276 TitleInput *tuple = NULL; | |
2277 | |
2278 PLAYLIST_LOCK(playlist->mutex); | |
2279 | |
2280 if ((node = g_list_nth(playlist->entries, pos))) | |
2281 { | |
2282 entry = node->data; | |
2283 tuple = entry->tuple; | |
2284 path = g_strdup(entry->filename); | |
2285 } | |
2286 | |
2287 PLAYLIST_UNLOCK(playlist->mutex); | |
2288 | |
2289 /* No tuple? Try to set this entry up properly. --nenolod */ | |
2290 if (entry->tuple == NULL || entry->tuple->mtime == -1 || | |
2291 entry->tuple->mtime == 0 || entry->tuple->mtime != playlist_get_mtime(entry->filename)) | |
2292 { | |
2293 playlist_entry_get_info(entry); | |
2294 tuple = entry->tuple; | |
2295 } | |
2296 | |
2297 if (tuple != NULL) | |
2298 { | |
2299 if (entry->decoder == NULL) | |
2300 entry->decoder = input_check_file(entry->filename, FALSE); /* try to find a decoder */ | |
2301 | |
2302 if (entry->decoder != NULL && entry->decoder->file_info_box == NULL) | |
2303 fileinfo_show_for_tuple(tuple); | |
2304 else if (entry->decoder != NULL && entry->decoder->file_info_box != NULL) | |
2305 entry->decoder->file_info_box(path); | |
2306 else | |
2307 fileinfo_show_for_path(path); | |
2308 g_free(path); | |
2309 } | |
2310 else if (path != NULL) | |
2311 { | |
2312 if (entry != NULL && 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 } | |
2319 | |
2320 void | |
2321 playlist_fileinfo_current(Playlist *playlist) | |
2322 { | |
2323 gchar *path = NULL; | |
2324 TitleInput *tuple = NULL; | |
2325 | |
2326 PLAYLIST_LOCK(playlist->mutex); | |
2327 | |
2328 if (playlist->entries && playlist->position) | |
2329 { | |
2330 path = g_strdup(playlist->position->filename); | |
2331 if (( playlist->position->tuple == NULL ) || ( playlist->position->decoder == NULL )) | |
2332 playlist_entry_get_info(playlist->position); | |
2333 tuple = playlist->position->tuple; | |
2334 } | |
2335 | |
2336 PLAYLIST_UNLOCK(playlist->mutex); | |
2337 | |
2338 if (tuple != NULL) | |
2339 { | |
2340 if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box == NULL) | |
2341 fileinfo_show_for_tuple(tuple); | |
2342 else if (playlist->position->decoder != NULL && playlist->position->decoder->file_info_box != NULL) | |
2343 playlist->position->decoder->file_info_box(path); | |
2344 else | |
2345 fileinfo_show_for_path(path); | |
2346 g_free(path); | |
2347 } | |
2348 else if (path != NULL) | |
2349 { | |
2350 if (playlist->position != NULL && 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 } | |
2357 | |
2358 | |
2359 static gboolean | |
2360 playlist_get_info_is_going(void) | |
2361 { | |
2362 gboolean result; | |
2363 | |
2364 G_LOCK(playlist_get_info_going); | |
2365 result = playlist_get_info_going; | |
2366 G_UNLOCK(playlist_get_info_going); | |
2367 | |
2368 return result; | |
2369 } | |
2370 | |
2371 static gpointer | |
2372 playlist_get_info_func(gpointer arg) | |
2373 { | |
2374 GList *node; | |
2375 gboolean update_playlistwin = FALSE; | |
2376 gboolean update_mainwin = FALSE; | |
2377 | |
2378 while (playlist_get_info_is_going()) { | |
2379 PlaylistEntry *entry; | |
2380 Playlist *playlist = playlist_get_active(); | |
2381 | |
2382 // on_load | |
2383 if (cfg.use_pl_metadata && | |
2384 cfg.get_info_on_load && | |
2385 playlist_get_info_scan_active) { | |
2386 | |
2387 PLAYLIST_LOCK(playlist->mutex); | |
2388 for (node = playlist->entries; node; node = g_list_next(node)) { | |
2389 entry = node->data; | |
2390 | |
2391 if(entry->tuple && (entry->tuple->length > -1)) { | |
2392 update_playlistwin = TRUE; | |
2393 continue; | |
2394 } | |
2395 | |
2396 if (!playlist_entry_get_info(entry)) { | |
2397 if (g_list_index(playlist->entries, entry) == -1) | |
2398 /* Entry disappeared while we looked it up. | |
2399 Restart. */ | |
2400 node = playlist->entries; | |
2401 } | |
2402 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { | |
2403 update_playlistwin = TRUE; | |
2404 if (entry == playlist->position) | |
2405 update_mainwin = TRUE; | |
2406 break; | |
2407 } | |
2408 } | |
2409 PLAYLIST_UNLOCK(playlist->mutex); | |
2410 | |
2411 if (!node) { | |
2412 g_mutex_lock(mutex_scan); | |
2413 playlist_get_info_scan_active = FALSE; | |
2414 g_mutex_unlock(mutex_scan); | |
2415 } | |
2416 } // on_load | |
2417 | |
2418 // on_demand | |
2419 else if (!cfg.get_info_on_load && | |
2420 cfg.get_info_on_demand && | |
2421 cfg.playlist_visible && | |
2422 !cfg.playlist_shaded && | |
2423 cfg.use_pl_metadata) { | |
2424 | |
2425 g_mutex_lock(mutex_scan); | |
2426 playlist_get_info_scan_active = FALSE; | |
2427 g_mutex_unlock(mutex_scan); | |
2428 | |
2429 PLAYLIST_LOCK(playlist->mutex); | |
2430 | |
2431 if (!playlist->entries) { | |
2432 PLAYLIST_UNLOCK(playlist->mutex); | |
2433 } | |
2434 else { | |
2435 for (node = g_list_nth(playlist->entries, playlistwin_get_toprow()); | |
2436 node && playlistwin_item_visible(g_list_position(playlist->entries, node)); | |
2437 node = g_list_next(node)) { | |
2438 | |
2439 entry = node->data; | |
2440 | |
2441 if(entry->tuple && (entry->tuple->length > -1)) { | |
2442 update_playlistwin = TRUE; | |
2443 continue; | |
2444 } | |
2445 | |
2446 if (!playlist_entry_get_info(entry)) { | |
2447 if (g_list_index(playlist->entries, entry) == -1) | |
2448 /* Entry disapeared while we | |
2449 looked it up. Restart. */ | |
2450 node = | |
2451 g_list_nth(playlist->entries, | |
2452 playlistwin_get_toprow()); | |
2453 } | |
2454 else if ((entry->tuple != NULL || entry->title != NULL) && entry->length != -1) { | |
2455 update_playlistwin = TRUE; | |
2456 if (entry == playlist->position) | |
2457 update_mainwin = TRUE; | |
2458 // no need for break here since this iteration is very short. | |
2459 } | |
2460 } | |
2461 PLAYLIST_UNLOCK(playlist->mutex); | |
2462 } | |
2463 } // on_demand | |
2464 else if (cfg.get_info_on_demand && | |
2465 (!cfg.playlist_visible || cfg.playlist_shaded | |
2466 || !cfg.use_pl_metadata)) | |
2467 { | |
2468 g_mutex_lock(mutex_scan); | |
2469 playlist_get_info_scan_active = FALSE; | |
2470 g_mutex_unlock(mutex_scan); | |
2471 } | |
2472 else /* not on_demand and not on_load... | |
2473 NOTE: this shouldn't happen anymore, sanity check in bmp_config_load now */ | |
2474 { | |
2475 g_mutex_lock(mutex_scan); | |
2476 playlist_get_info_scan_active = FALSE; | |
2477 g_mutex_unlock(mutex_scan); | |
2478 } | |
2479 | |
2480 if (update_playlistwin) { | |
2481 playlistwin_update_list(playlist); | |
2482 update_playlistwin = FALSE; | |
2483 } | |
2484 | |
2485 if (update_mainwin) { | |
2486 mainwin_set_info_text(); | |
2487 update_mainwin = FALSE; | |
2488 } | |
2489 | |
2490 if (playlist_get_info_scan_active) { | |
2491 continue; | |
2492 } | |
2493 | |
2494 g_mutex_lock(mutex_scan); | |
2495 g_cond_wait(cond_scan, mutex_scan); | |
2496 g_mutex_unlock(mutex_scan); | |
2497 | |
2498 } // while | |
2499 | |
2500 g_thread_exit(NULL); | |
2501 return NULL; | |
2502 } | |
2503 | |
2504 void | |
2505 playlist_start_get_info_thread(void) | |
2506 { | |
2507 G_LOCK(playlist_get_info_going); | |
2508 playlist_get_info_going = TRUE; | |
2509 G_UNLOCK(playlist_get_info_going); | |
2510 | |
2511 playlist_get_info_thread = g_thread_create(playlist_get_info_func, | |
2512 NULL, TRUE, NULL); | |
2513 } | |
2514 | |
2515 void | |
2516 playlist_stop_get_info_thread(void) | |
2517 { | |
2518 G_LOCK(playlist_get_info_going); | |
2519 playlist_get_info_going = FALSE; | |
2520 G_UNLOCK(playlist_get_info_going); | |
2521 | |
2522 g_cond_broadcast(cond_scan); | |
2523 g_thread_join(playlist_get_info_thread); | |
2524 } | |
2525 | |
2526 void | |
2527 playlist_start_get_info_scan(void) | |
2528 { | |
2529 g_mutex_lock(mutex_scan); | |
2530 playlist_get_info_scan_active = TRUE; | |
2531 g_mutex_unlock(mutex_scan); | |
2532 g_cond_signal(cond_scan); | |
2533 } | |
2534 | |
2535 void | |
2536 playlist_remove_dead_files(Playlist *playlist) | |
2537 { | |
2538 GList *node, *next_node; | |
2539 | |
2540 PLAYLIST_LOCK(playlist->mutex); | |
2541 | |
2542 for (node = playlist->entries; node; node = next_node) { | |
2543 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2544 next_node = g_list_next(node); | |
2545 | |
2546 if (!entry || !entry->filename) { | |
2547 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2548 continue; | |
2549 } | |
2550 | |
2551 /* FIXME: What about 'file:///'? */ | |
2552 /* Don't kill URLs */ | |
2553 if (strstr(entry->filename, "://")) | |
2554 continue; | |
2555 | |
2556 /* FIXME: Should test for readability */ | |
2557 if (vfs_file_test(entry->filename, G_FILE_TEST_EXISTS)) | |
2558 continue; | |
2559 | |
2560 if (entry == playlist->position) { | |
2561 /* Don't remove the currently playing song */ | |
2562 if (playback_get_playing()) | |
2563 continue; | |
2564 | |
2565 if (next_node) | |
2566 playlist->position = PLAYLIST_ENTRY(next_node->data); | |
2567 else | |
2568 playlist->position = NULL; | |
2569 } | |
2570 | |
2571 playlist_entry_free(entry); | |
2572 playlist->entries = g_list_delete_link(playlist->entries, node); | |
2573 } | |
2574 | |
2575 PLAYLIST_UNLOCK(playlist->mutex); | |
2576 | |
2577 playlist_generate_shuffle_list(playlist); | |
2578 playlistwin_update_list(playlist); | |
2579 playlist_recalc_total_time(playlist); | |
2580 playlist_manager_update(); | |
2581 } | |
2582 | |
2583 | |
2584 static gint | |
2585 playlist_dupscmp_title(PlaylistEntry * a, | |
2586 PlaylistEntry * b) | |
2587 { | |
2588 const gchar *a_title, *b_title; | |
2589 | |
2590 g_return_val_if_fail(a != NULL, 0); | |
2591 g_return_val_if_fail(b != NULL, 0); | |
2592 | |
2593 if (a->title) | |
2594 a_title = a->title; | |
2595 else { | |
2596 if (strrchr(a->filename, '/')) | |
2597 a_title = strrchr(a->filename, '/') + 1; | |
2598 else | |
2599 a_title = a->filename; | |
2600 } | |
2601 | |
2602 if (b->title) | |
2603 b_title = b->title; | |
2604 else { | |
2605 if (strrchr(a->filename, '/')) | |
2606 b_title = strrchr(b->filename, '/') + 1; | |
2607 else | |
2608 b_title = b->filename; | |
2609 } | |
2610 | |
2611 return strcmp(a_title, b_title); | |
2612 } | |
2613 | |
2614 static gint | |
2615 playlist_dupscmp_filename(PlaylistEntry * a, | |
2616 PlaylistEntry * b ) | |
2617 { | |
2618 gchar *a_filename, *b_filename; | |
2619 | |
2620 g_return_val_if_fail(a != NULL, 0); | |
2621 g_return_val_if_fail(b != NULL, 0); | |
2622 | |
2623 if (strrchr(a->filename, '/')) | |
2624 a_filename = strrchr(a->filename, '/') + 1; | |
2625 else | |
2626 a_filename = a->filename; | |
2627 | |
2628 if (strrchr(b->filename, '/')) | |
2629 b_filename = strrchr(b->filename, '/') + 1; | |
2630 else | |
2631 b_filename = b->filename; | |
2632 | |
2633 return strcmp(a_filename, b_filename); | |
2634 } | |
2635 | |
2636 static gint | |
2637 playlist_dupscmp_path(PlaylistEntry * a, | |
2638 PlaylistEntry * b) | |
2639 { | |
2640 /* simply compare the entire filename string */ | |
2641 return strcmp(a->filename, b->filename); | |
2642 } | |
2643 | |
2644 void | |
2645 playlist_remove_duplicates(Playlist *playlist, PlaylistDupsType type) | |
2646 { | |
2647 GList *node, *next_node; | |
2648 GList *node_cmp, *next_node_cmp; | |
2649 gint (*dups_compare_func)(PlaylistEntry * , PlaylistEntry *); | |
2650 | |
2651 switch ( type ) | |
2652 { | |
2653 case PLAYLIST_DUPS_TITLE: | |
2654 dups_compare_func = playlist_dupscmp_title; | |
2655 break; | |
2656 case PLAYLIST_DUPS_PATH: | |
2657 dups_compare_func = playlist_dupscmp_path; | |
2658 break; | |
2659 case PLAYLIST_DUPS_FILENAME: | |
2660 default: | |
2661 dups_compare_func = playlist_dupscmp_filename; | |
2662 break; | |
2663 } | |
2664 | |
2665 PLAYLIST_LOCK(playlist->mutex); | |
2666 | |
2667 for (node = playlist->entries; node; node = next_node) { | |
2668 PlaylistEntry *entry = PLAYLIST_ENTRY(node->data); | |
2669 next_node = g_list_next(node); | |
2670 | |
2671 if (!entry || !entry->filename) { | |
2672 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2673 continue; | |
2674 } | |
2675 | |
2676 for (node_cmp = next_node; node_cmp; node_cmp = next_node_cmp) { | |
2677 PlaylistEntry *entry_cmp = PLAYLIST_ENTRY(node_cmp->data); | |
2678 next_node_cmp = g_list_next(node_cmp); | |
2679 | |
2680 if (!entry_cmp || !entry_cmp->filename) { | |
2681 g_message(G_STRLOC ": Playlist entry is invalid!"); | |
2682 continue; | |
2683 } | |
2684 | |
2685 /* compare using the chosen dups_compare_func */ | |
2686 if ( !dups_compare_func( entry , entry_cmp ) ) { | |
2687 | |
2688 if (entry_cmp == playlist->position) { | |
2689 /* Don't remove the currently playing song */ | |
2690 if (playback_get_playing()) | |
2691 continue; | |
2692 | |
2693 if (next_node_cmp) | |
2694 playlist->position = PLAYLIST_ENTRY(next_node_cmp->data); | |
2695 else | |
2696 playlist->position = NULL; | |
2697 } | |
2698 | |
2699 /* check if this was the next item of the external | |
2700 loop; if true, replace it with the next of the next*/ | |
2701 if ( node_cmp == next_node ) | |
2702 next_node = g_list_next(next_node); | |
2703 | |
2704 playlist_entry_free(entry_cmp); | |
2705 playlist->entries = g_list_delete_link(playlist->entries, node_cmp); | |
2706 } | |
2707 } | |
2708 } | |
2709 | |
2710 PLAYLIST_UNLOCK(playlist->mutex); | |
2711 | |
2712 playlistwin_update_list(playlist); | |
2713 playlist_recalc_total_time(playlist); | |
2714 | |
2715 playlist_manager_update(); | |
2716 } | |
2717 | |
2718 void | |
2719 playlist_get_total_time(Playlist * playlist, | |
2720 gulong * total_time, | |
2721 gulong * selection_time, | |
2722 gboolean * total_more, | |
2723 gboolean * selection_more) | |
2724 { | |
2725 PLAYLIST_LOCK(playlist->mutex); | |
2726 *total_time = playlist->pl_total_time; | |
2727 *selection_time = playlist->pl_selection_time; | |
2728 *total_more = playlist->pl_total_more; | |
2729 *selection_more = playlist->pl_selection_more; | |
2730 PLAYLIST_UNLOCK(playlist->mutex); | |
2731 } | |
2732 | |
2733 | |
2734 static void | |
2735 playlist_recalc_total_time_nolock(Playlist *playlist) | |
2736 { | |
2737 GList *list; | |
2738 PlaylistEntry *entry; | |
2739 | |
2740 REQUIRE_LOCK(playlist->mutex); | |
2741 | |
2742 playlist->pl_total_time = 0; | |
2743 playlist->pl_selection_time = 0; | |
2744 playlist->pl_total_more = FALSE; | |
2745 playlist->pl_selection_more = FALSE; | |
2746 | |
2747 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2748 entry = list->data; | |
2749 | |
2750 if (entry->length != -1) | |
2751 playlist->pl_total_time += entry->length / 1000; | |
2752 else | |
2753 playlist->pl_total_more = TRUE; | |
2754 | |
2755 if (entry->selected) { | |
2756 if (entry->length != -1) | |
2757 playlist->pl_selection_time += entry->length / 1000; | |
2758 else | |
2759 playlist->pl_selection_more = TRUE; | |
2760 } | |
2761 } | |
2762 } | |
2763 | |
2764 static void | |
2765 playlist_recalc_total_time(Playlist *playlist) | |
2766 { | |
2767 PLAYLIST_LOCK(playlist->mutex); | |
2768 playlist_recalc_total_time_nolock(playlist); | |
2769 PLAYLIST_UNLOCK(playlist->mutex); | |
2770 } | |
2771 | |
2772 gint | |
2773 playlist_select_search( Playlist *playlist , TitleInput *tuple , gint action ) | |
2774 { | |
2775 GList *entry_list = NULL, *found_list = NULL, *sel_list = NULL; | |
2776 gboolean is_first_search = TRUE; | |
2777 gint num_of_entries_found = 0; | |
2778 | |
2779 #if defined(USE_REGEX_ONIGURUMA) | |
2780 /* set encoding for Oniguruma regex to UTF-8 */ | |
2781 reg_set_encoding( REG_POSIX_ENCODING_UTF8 ); | |
2782 onig_set_default_syntax( ONIG_SYNTAX_POSIX_BASIC ); | |
2783 #endif | |
2784 | |
2785 PLAYLIST_LOCK(playlist->mutex); | |
2786 | |
2787 if ( tuple->track_name != NULL ) | |
2788 { | |
2789 /* match by track_name */ | |
2790 const gchar *regex_pattern = tuple->track_name; | |
2791 regex_t regex; | |
2792 #if defined(USE_REGEX_PCRE) | |
2793 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2794 #else | |
2795 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2796 #endif | |
2797 { | |
2798 GList *tfound_list = NULL; | |
2799 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2800 else entry_list = found_list; /* use found_list */ | |
2801 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2802 { | |
2803 PlaylistEntry *entry = entry_list->data; | |
2804 if ( ( entry->tuple != NULL ) && ( entry->tuple->track_name != NULL ) && | |
2805 ( regexec( ®ex , entry->tuple->track_name , 0 , NULL , 0 ) == 0 ) ) | |
2806 { | |
2807 tfound_list = g_list_append( tfound_list , entry ); | |
2808 } | |
2809 } | |
2810 g_list_free( found_list ); /* wipe old found_list */ | |
2811 found_list = tfound_list; /* move tfound_list in found_list */ | |
2812 regfree( ®ex ); | |
2813 } | |
2814 is_first_search = FALSE; | |
2815 } | |
2816 | |
2817 if ( tuple->album_name != NULL ) | |
2818 { | |
2819 /* match by album_name */ | |
2820 const gchar *regex_pattern = tuple->album_name; | |
2821 regex_t regex; | |
2822 #if defined(USE_REGEX_PCRE) | |
2823 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2824 #else | |
2825 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2826 #endif | |
2827 { | |
2828 GList *tfound_list = NULL; | |
2829 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2830 else entry_list = found_list; /* use found_list */ | |
2831 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2832 { | |
2833 PlaylistEntry *entry = entry_list->data; | |
2834 if ( ( entry->tuple != NULL ) && ( entry->tuple->album_name != NULL ) && | |
2835 ( regexec( ®ex , entry->tuple->album_name , 0 , NULL , 0 ) == 0 ) ) | |
2836 { | |
2837 tfound_list = g_list_append( tfound_list , entry ); | |
2838 } | |
2839 } | |
2840 g_list_free( found_list ); /* wipe old found_list */ | |
2841 found_list = tfound_list; /* move tfound_list in found_list */ | |
2842 regfree( ®ex ); | |
2843 } | |
2844 is_first_search = FALSE; | |
2845 } | |
2846 | |
2847 if ( tuple->performer != NULL ) | |
2848 { | |
2849 /* match by performer */ | |
2850 const gchar *regex_pattern = tuple->performer; | |
2851 regex_t regex; | |
2852 #if defined(USE_REGEX_PCRE) | |
2853 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2854 #else | |
2855 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2856 #endif | |
2857 { | |
2858 GList *tfound_list = NULL; | |
2859 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2860 else entry_list = found_list; /* use found_list */ | |
2861 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2862 { | |
2863 PlaylistEntry *entry = entry_list->data; | |
2864 if ( ( entry->tuple != NULL ) && ( entry->tuple->performer != NULL ) && | |
2865 ( regexec( ®ex , entry->tuple->performer , 0 , NULL , 0 ) == 0 ) ) | |
2866 { | |
2867 tfound_list = g_list_append( tfound_list , entry ); | |
2868 } | |
2869 } | |
2870 g_list_free( found_list ); /* wipe old found_list */ | |
2871 found_list = tfound_list; /* move tfound_list in found_list */ | |
2872 regfree( ®ex ); | |
2873 } | |
2874 is_first_search = FALSE; | |
2875 } | |
2876 | |
2877 if ( tuple->file_name != NULL ) | |
2878 { | |
2879 /* match by file_name */ | |
2880 const gchar *regex_pattern = tuple->file_name; | |
2881 regex_t regex; | |
2882 #if defined(USE_REGEX_PCRE) | |
2883 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE | REG_UTF8 ) == 0 ) | |
2884 #else | |
2885 if ( regcomp( ®ex , regex_pattern , REG_NOSUB | REG_ICASE ) == 0 ) | |
2886 #endif | |
2887 { | |
2888 GList *tfound_list = NULL; | |
2889 if ( is_first_search == TRUE ) entry_list = playlist->entries; | |
2890 else entry_list = found_list; /* use found_list */ | |
2891 for ( ; entry_list ; entry_list = g_list_next(entry_list) ) | |
2892 { | |
2893 PlaylistEntry *entry = entry_list->data; | |
2894 if ( ( entry->tuple != NULL ) && ( entry->tuple->file_name != NULL ) && | |
2895 ( regexec( ®ex , entry->tuple->file_name , 0 , NULL , 0 ) == 0 ) ) | |
2896 { | |
2897 tfound_list = g_list_append( tfound_list , entry ); | |
2898 } | |
2899 } | |
2900 g_list_free( found_list ); /* wipe old found_list */ | |
2901 found_list = tfound_list; /* move tfound_list in found_list */ | |
2902 regfree( ®ex ); | |
2903 } | |
2904 is_first_search = FALSE; | |
2905 } | |
2906 | |
2907 /* NOTE: action = 0 -> default behaviour, select all matching entries */ | |
2908 /* if some entries are still in found_list, those | |
2909 are what the user is searching for; select them */ | |
2910 for ( sel_list = found_list ; sel_list ; sel_list = g_list_next(sel_list) ) | |
2911 { | |
2912 PlaylistEntry *entry = sel_list->data; | |
2913 entry->selected = TRUE; | |
2914 num_of_entries_found++; | |
2915 } | |
2916 | |
2917 g_list_free( found_list ); | |
2918 | |
2919 PLAYLIST_UNLOCK(playlist->mutex); | |
2920 playlist_recalc_total_time(playlist); | |
2921 | |
2922 return num_of_entries_found; | |
2923 } | |
2924 | |
2925 void | |
2926 playlist_select_all(Playlist *playlist, gboolean set) | |
2927 { | |
2928 GList *list; | |
2929 | |
2930 PLAYLIST_LOCK(playlist->mutex); | |
2931 | |
2932 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2933 PlaylistEntry *entry = list->data; | |
2934 entry->selected = set; | |
2935 } | |
2936 | |
2937 PLAYLIST_UNLOCK(playlist->mutex); | |
2938 playlist_recalc_total_time(playlist); | |
2939 } | |
2940 | |
2941 void | |
2942 playlist_select_invert_all(Playlist *playlist) | |
2943 { | |
2944 GList *list; | |
2945 | |
2946 PLAYLIST_LOCK(playlist->mutex); | |
2947 | |
2948 for (list = playlist->entries; list; list = g_list_next(list)) { | |
2949 PlaylistEntry *entry = list->data; | |
2950 entry->selected = !entry->selected; | |
2951 } | |
2952 | |
2953 PLAYLIST_UNLOCK(playlist->mutex); | |
2954 playlist_recalc_total_time(playlist); | |
2955 } | |
2956 | |
2957 gboolean | |
2958 playlist_select_invert(Playlist *playlist, guint pos) | |
2959 { | |
2960 GList *list; | |
2961 gboolean invert_ok = FALSE; | |
2962 | |
2963 PLAYLIST_LOCK(playlist->mutex); | |
2964 | |
2965 if ((list = g_list_nth(playlist->entries, pos))) { | |
2966 PlaylistEntry *entry = list->data; | |
2967 entry->selected = !entry->selected; | |
2968 invert_ok = TRUE; | |
2969 } | |
2970 | |
2971 PLAYLIST_UNLOCK(playlist->mutex); | |
2972 playlist_recalc_total_time(playlist); | |
2973 | |
2974 return invert_ok; | |
2975 } | |
2976 | |
2977 | |
2978 void | |
2979 playlist_select_range(Playlist *playlist, gint min_pos, gint max_pos, gboolean select) | |
2980 { | |
2981 GList *list; | |
2982 gint i; | |
2983 | |
2984 if (min_pos > max_pos) | |
2985 SWAP(min_pos, max_pos); | |
2986 | |
2987 PLAYLIST_LOCK(playlist->mutex); | |
2988 | |
2989 list = g_list_nth(playlist->entries, min_pos); | |
2990 for (i = min_pos; i <= max_pos && list; i++) { | |
2991 PlaylistEntry *entry = list->data; | |
2992 entry->selected = select; | |
2993 list = g_list_next(list); | |
2994 } | |
2995 | |
2996 PLAYLIST_UNLOCK(playlist->mutex); | |
2997 | |
2998 playlist_recalc_total_time(playlist); | |
2999 } | |
3000 | |
3001 gboolean | |
3002 playlist_read_info_selection(Playlist *playlist) | |
3003 { | |
3004 GList *node; | |
3005 gboolean retval = FALSE; | |
3006 | |
3007 PLAYLIST_LOCK(playlist->mutex); | |
3008 | |
3009 for (node = playlist->entries; node; node = g_list_next(node)) { | |
3010 PlaylistEntry *entry = node->data; | |
3011 if (!entry->selected) | |
3012 continue; | |
3013 | |
3014 retval = TRUE; | |
3015 | |
3016 str_replace_in(&entry->title, NULL); | |
3017 entry->length = -1; | |
3018 | |
3019 /* invalidate mtime to reread */ | |
3020 if (entry->tuple != NULL) | |
3021 entry->tuple->mtime = -1; /* -1 denotes "non-initialized". now 0 is for stream etc. yaz */ | |
3022 | |
3023 if (!playlist_entry_get_info(entry)) { | |
3024 if (g_list_index(playlist->entries, entry) == -1) | |
3025 /* Entry disappeared while we looked it up. Restart. */ | |
3026 node = playlist->entries; | |
3027 } | |
3028 } | |
3029 | |
3030 PLAYLIST_UNLOCK(playlist->mutex); | |
3031 | |
3032 playlistwin_update_list(playlist); | |
3033 playlist_recalc_total_time(playlist); | |
3034 | |
3035 return retval; | |
3036 } | |
3037 | |
3038 void | |
3039 playlist_read_info(Playlist *playlist, guint pos) | |
3040 { | |
3041 GList *node; | |
3042 | |
3043 PLAYLIST_LOCK(playlist->mutex); | |
3044 | |
3045 if ((node = g_list_nth(playlist->entries, pos))) { | |
3046 PlaylistEntry *entry = node->data; | |
3047 str_replace_in(&entry->title, NULL); | |
3048 entry->length = -1; | |
3049 playlist_entry_get_info(entry); | |
3050 } | |
3051 | |
3052 PLAYLIST_UNLOCK(playlist->mutex); | |
3053 | |
3054 playlistwin_update_list(playlist); | |
3055 playlist_recalc_total_time(playlist); | |
3056 } | |
3057 | |
3058 Playlist * | |
3059 playlist_get_active(void) | |
3060 { | |
3061 if (playlists_iter != NULL) | |
3062 return (Playlist *) playlists_iter->data; | |
3063 | |
3064 return (Playlist *) playlists->data; | |
3065 } | |
3066 | |
3067 void | |
3068 playlist_set_shuffle(gboolean shuffle) | |
3069 { | |
3070 Playlist *playlist = playlist_get_active(); | |
3071 if (!playlist) | |
3072 return; | |
3073 | |
3074 PLAYLIST_LOCK(playlist->mutex); | |
3075 | |
3076 playlist_position_before_jump = NULL; | |
3077 | |
3078 cfg.shuffle = shuffle; | |
3079 playlist_generate_shuffle_list_nolock(playlist); | |
3080 | |
3081 PLAYLIST_UNLOCK(playlist->mutex); | |
3082 } | |
3083 | |
3084 Playlist * | |
3085 playlist_new(void) | |
3086 { | |
3087 Playlist *playlist = g_new0(Playlist, 1); | |
3088 playlist->mutex = g_mutex_new(); | |
3089 playlist->loading_playlist = FALSE; | |
3090 | |
3091 playlist_set_current_name(playlist, NULL); | |
3092 playlist_clear(playlist); | |
3093 | |
3094 return playlist; | |
3095 } | |
3096 | |
3097 void | |
3098 playlist_free(Playlist *playlist) | |
3099 { | |
3100 g_mutex_free( playlist->mutex ); | |
3101 g_free( playlist ); | |
3102 return; | |
3103 } | |
3104 | |
3105 Playlist * | |
3106 playlist_new_from_selected(void) | |
3107 { | |
3108 Playlist *newpl = playlist_new(); | |
3109 Playlist *playlist = playlist_get_active(); | |
3110 GList *list = playlist_get_selected(playlist); | |
3111 | |
3112 playlist_add_playlist( newpl ); | |
3113 | |
3114 PLAYLIST_LOCK(playlist->mutex); | |
3115 | |
3116 while ( list != NULL ) | |
3117 { | |
3118 PlaylistEntry *entry = g_list_nth_data(playlist->entries, GPOINTER_TO_INT(list->data)); | |
3119 if ( entry->filename != NULL ) /* paranoid? oh well... */ | |
3120 playlist_add( newpl , entry->filename ); | |
3121 list = g_list_next(list); | |
3122 } | |
3123 | |
3124 PLAYLIST_UNLOCK(playlist->mutex); | |
3125 | |
3126 playlist_recalc_total_time(newpl); | |
3127 playlistwin_update_list(playlist); | |
3128 | |
3129 return newpl; | |
3130 } | |
3131 | |
3132 const gchar * | |
3133 playlist_get_filename_to_play(Playlist *playlist) | |
3134 { | |
3135 const gchar *filename = NULL; | |
3136 | |
3137 if (!playlist) | |
3138 return NULL; | |
3139 | |
3140 PLAYLIST_LOCK(playlist->mutex); | |
3141 | |
3142 if (!playlist->position) { | |
3143 if (cfg.shuffle) | |
3144 playlist->position = playlist->shuffle->data; | |
3145 else | |
3146 playlist->position = playlist->entries->data; | |
3147 } | |
3148 | |
3149 filename = playlist->position->filename; | |
3150 | |
3151 PLAYLIST_UNLOCK(playlist->mutex); | |
3152 | |
3153 return filename; | |
3154 } | |
3155 | |
3156 PlaylistEntry * | |
3157 playlist_get_entry_to_play(Playlist *playlist) | |
3158 { | |
3159 if (!playlist) | |
3160 return NULL; | |
3161 | |
3162 PLAYLIST_LOCK(playlist->mutex); | |
3163 | |
3164 if (!playlist->position) { | |
3165 if (cfg.shuffle) | |
3166 playlist->position = playlist->shuffle->data; | |
3167 else | |
3168 playlist->position = playlist->entries->data; | |
3169 } | |
3170 | |
3171 PLAYLIST_UNLOCK(playlist->mutex); | |
3172 | |
3173 return playlist->position; | |
3174 } |