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