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