2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 play_tree_is_valid(play_tree_t
* pt
);
40 play_tree_t
* r
= calloc(1,sizeof(play_tree_t
));
42 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_t
));
45 r
->entry_type
= PLAY_TREE_ENTRY_NODE
;
50 play_tree_free(play_tree_t
* pt
, int children
) {
58 for(iter
= pt
->child
; iter
!= NULL
; ) {
59 play_tree_t
* nxt
=iter
->next
;
60 play_tree_free(iter
,1);
66 play_tree_remove(pt
,0,0);
68 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
73 for(i
= 0 ; pt
->params
[i
].name
!= NULL
; i
++) {
74 free(pt
->params
[i
].name
);
75 free(pt
->params
[i
].value
);
81 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++)
90 play_tree_free_list(play_tree_t
* pt
, int children
) {
97 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
101 play_tree_t
* nxt
= iter
->next
;
102 play_tree_free(iter
, children
);
110 play_tree_append_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
115 assert(entry
!= NULL
);
121 for(iter
= pt
; iter
->next
!= NULL
; iter
= iter
->next
)
124 entry
->parent
= iter
->parent
;
131 play_tree_prepend_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
136 assert(entry
!= NULL
);
139 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
144 entry
->parent
= iter
->parent
;
149 assert(entry
->parent
->child
== iter
);
151 entry
->parent
->child
= entry
;
156 play_tree_insert_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
160 assert(entry
!= NULL
);
163 entry
->parent
= pt
->parent
;
167 assert(pt
->next
->prev
== pt
);
169 entry
->next
= pt
->next
;
170 entry
->next
->prev
= entry
;
178 play_tree_remove(play_tree_t
* pt
, int free_it
, int with_children
) {
185 if(pt
->prev
&& pt
->next
) {
187 assert(pt
->prev
->next
== pt
);
188 assert(pt
->next
->prev
== pt
);
190 pt
->prev
->next
= pt
->next
;
191 pt
->next
->prev
= pt
->prev
;
195 assert(pt
->prev
->next
== pt
);
197 pt
->prev
->next
= NULL
;
198 } // Beginning of list
201 assert(pt
->next
->prev
== pt
);
203 pt
->next
->prev
= NULL
;
206 assert(pt
->parent
->child
== pt
);
208 pt
->parent
->child
= pt
->next
;
211 else if(pt
->parent
) {
213 assert(pt
->parent
->child
== pt
);
215 pt
->parent
->child
= NULL
;
218 pt
->prev
= pt
->next
= pt
->parent
= NULL
;
220 play_tree_free(pt
,with_children
);
225 play_tree_set_child(play_tree_t
* pt
, play_tree_t
* child
) {
228 /* Roughly validate input data. Both, pt and child are going to be
229 * dereferenced, hence assure they're not NULL.
232 mp_msg(MSGT_PLAYTREE
, MSGL_ERR
, "Internal error, attempt to add an empty child or use empty playlist\n");
237 assert(pt
->entry_type
== PLAY_TREE_ENTRY_NODE
);
240 //DEBUG_FF: Where are the children freed?
241 // Attention in using this function!
242 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
245 // Go back to first one
246 for(iter
= child
; iter
->prev
!= NULL
; iter
= iter
->prev
)
251 for( ; iter
!= NULL
; iter
= iter
->next
)
257 play_tree_set_parent(play_tree_t
* pt
, play_tree_t
* parent
) {
265 pt
->parent
->child
= NULL
;
267 for(iter
= pt
; iter
!= NULL
; iter
= iter
->next
)
268 iter
->parent
= parent
;
271 for(iter
= pt
->prev
; iter
->prev
!= NULL
; iter
= iter
->prev
)
272 iter
->parent
= parent
;
273 iter
->parent
= parent
;
274 parent
->child
= iter
;
282 play_tree_add_file(play_tree_t
* pt
,char* file
) {
287 assert(pt
->child
== NULL
);
288 assert(file
!= NULL
);
291 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
&&
292 pt
->entry_type
!= PLAY_TREE_ENTRY_FILE
)
296 for(n
= 0 ; pt
->files
[n
] != NULL
; n
++)
299 pt
->files
= realloc(pt
->files
, (n
+ 2) * sizeof(char*));
300 if(pt
->files
==NULL
) {
301 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*(int)sizeof(char*));
305 pt
->files
[n
] = strdup(file
);
306 pt
->files
[n
+1] = NULL
;
308 pt
->entry_type
= PLAY_TREE_ENTRY_FILE
;
313 play_tree_remove_file(play_tree_t
* pt
,char* file
) {
318 assert(file
!= NULL
);
319 assert(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
);
322 for(n
=0 ; pt
->files
[n
] != NULL
; n
++) {
323 if(strcmp(file
,pt
->files
[n
]) == 0)
327 if(f
< 0) // Not found
337 memmove(&pt
->files
[f
],&pt
->files
[f
+1],(n
-f
)*sizeof(char*));
338 pt
->files
= realloc(pt
->files
, n
* sizeof(char*));
339 if(pt
->files
== NULL
) {
340 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*(int)sizeof(char*));
352 play_tree_set_param(play_tree_t
* pt
, char* name
, char* val
) {
357 assert(name
!= NULL
);
361 for ( ; pt
->params
[n
].name
!= NULL
; n
++ ) { }
363 pt
->params
= realloc(pt
->params
, (n
+ 2) * sizeof(play_tree_param_t
));
364 if(pt
->params
== NULL
) {
365 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't realloc params (%d bytes of memory)\n",(n
+2)*(int)sizeof(play_tree_param_t
));
368 pt
->params
[n
].name
= strdup(name
);
369 pt
->params
[n
].value
= val
!= NULL
? strdup(val
) : NULL
;
370 memset(&pt
->params
[n
+1],0,sizeof(play_tree_param_t
));
376 play_tree_unset_param(play_tree_t
* pt
, char* name
) {
381 assert(name
!= NULL
);
382 assert(pt
->params
!= NULL
);
385 for(n
= 0 ; pt
->params
[n
].name
!= NULL
; n
++) {
386 if(strcasecmp(pt
->params
[n
].name
,name
) == 0)
393 free(pt
->params
[ni
].name
);
394 free(pt
->params
[ni
].value
);
397 memmove(&pt
->params
[ni
],&pt
->params
[ni
+1],(n
-ni
)*sizeof(play_tree_param_t
));
398 pt
->params
= realloc(pt
->params
, n
* sizeof(play_tree_param_t
));
399 if(pt
->params
== NULL
) {
400 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",n
*(int)sizeof(play_tree_param_t
));
412 play_tree_set_params_from(play_tree_t
* dest
,play_tree_t
* src
) {
416 assert(dest
!= NULL
);
423 for(i
= 0; src
->params
[i
].name
!= NULL
; i
++)
424 play_tree_set_param(dest
,src
->params
[i
].name
,src
->params
[i
].value
);
425 if(src
->flags
& PLAY_TREE_RND
) // pass the random flag too
426 dest
->flags
|= PLAY_TREE_RND
;
431 play_tree_unset_flag(play_tree_t
* pt
, int flags
, int deep
) {
436 if(deep
&& pt
->child
) {
438 for(i
= pt
->child
; i
; i
= i
->next
)
439 play_tree_unset_flag(i
,flags
,deep
);
444 //////////////////////////////////// ITERATOR //////////////////////////////////////
447 play_tree_iter_push_params(play_tree_iter_t
* iter
) {
451 assert(iter
!= NULL
);
452 assert(iter
->config
!= NULL
);
453 assert(iter
->tree
!= NULL
);
458 // We always push a config because we can set some option
460 m_config_push(iter
->config
);
462 if(pt
->params
== NULL
)
466 for(n
= 0; pt
->params
[n
].name
!= NULL
; n
++) {
468 if((e
= m_config_set_option(iter
->config
,pt
->params
[n
].name
,pt
->params
[n
].value
)) < 0) {
469 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error %d while setting option '%s' with value '%s'\n",e
,
470 pt
->params
[n
].name
,pt
->params
[n
].value
);
475 iter
->entry_pushed
= 1;
480 play_tree_iter_new(play_tree_t
* pt
,m_config_t
* config
) {
481 play_tree_iter_t
* iter
;
485 assert(config
!= NULL
);
488 if( ! play_tree_is_valid(pt
))
491 iter
= calloc(1,sizeof(play_tree_iter_t
));
493 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate new iterator (%d bytes of memory)\n",(int)sizeof(play_tree_iter_t
));
498 iter
->config
= config
;
501 iter
->loop
= pt
->parent
->loop
;
507 play_tree_iter_free(play_tree_iter_t
* iter
) {
510 assert(iter
!= NULL
);
513 if(iter
->status_stack
) {
515 assert(iter
->stack_size
> 0);
517 free(iter
->status_stack
);
524 play_tree_rnd_step(play_tree_t
* pt
) {
527 play_tree_t
*i
,*head
;
529 // Count how many free choice we have
530 for(i
= pt
; i
->prev
; i
= i
->prev
)
531 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
533 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
534 for(i
= pt
->next
; i
; i
= i
->next
)
535 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
537 if(!count
) return NULL
;
539 r
= (int)((float)(count
) * rand() / (RAND_MAX
+ 1.0));
541 for(i
= head
; i
; i
=i
->next
) {
542 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) r
--;
546 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Random stepping error\n");
552 play_tree_iter_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
555 if ( !iter
) return PLAY_TREE_ITER_ENTRY
;
556 if ( !iter
->root
) return PLAY_TREE_ITER_ENTRY
;
559 assert(iter
!= NULL
);
560 assert(iter
->root
!= NULL
);
561 //printf("PT : Stepping = %d\n",d);
564 if(iter
->tree
== NULL
) {
565 iter
->tree
= iter
->root
;
566 return play_tree_iter_step(iter
,0,with_nodes
);
569 if(iter
->config
&& iter
->entry_pushed
> 0) {
570 iter
->entry_pushed
= 0;
571 m_config_pop(iter
->config
);
574 if(iter
->tree
->parent
&& (iter
->tree
->parent
->flags
& PLAY_TREE_RND
))
575 iter
->mode
= PLAY_TREE_ITER_RND
;
577 iter
->mode
= PLAY_TREE_ITER_NORMAL
;
580 if(iter
->mode
== PLAY_TREE_ITER_RND
)
581 pt
= play_tree_rnd_step(iter
->tree
);
585 for(i
= d
; i
> 0 && pt
; i
--)
591 for(i
= d
; i
< 0 && pt
; i
++)
597 if(pt
== NULL
) { // No next
599 if (iter
->mode
== PLAY_TREE_ITER_RND
) {
600 if (iter
->root
->loop
== 0)
601 return PLAY_TREE_ITER_END
;
602 play_tree_unset_flag(iter
->root
, PLAY_TREE_RND_PLAYED
, -1);
603 if (iter
->root
->loop
> 0) iter
->root
->loop
--;
605 return play_tree_iter_step(iter
, 0, with_nodes
);
607 if(iter
->tree
->parent
&& iter
->tree
->parent
->loop
!= 0 && ((d
> 0 && iter
->loop
!= 0) || ( d
< 0 && (iter
->loop
< 0 || iter
->loop
< iter
->tree
->parent
->loop
) ) ) ) {
608 if(d
> 0) { // Go back to the first one
609 for(pt
= iter
->tree
; pt
->prev
!= NULL
; pt
= pt
->prev
)
611 if(iter
->loop
> 0) iter
->loop
--;
612 } else if( d
< 0 ) { // Or the last one
613 for(pt
= iter
->tree
; pt
->next
!= NULL
; pt
= pt
->next
)
615 if(iter
->loop
>= 0 && iter
->loop
< iter
->tree
->parent
->loop
) iter
->loop
++;
618 return play_tree_iter_step(iter
,0,with_nodes
);
621 return play_tree_iter_up_step(iter
,d
,with_nodes
);
625 // Is there any valid child?
626 if(pt
->child
&& play_tree_is_valid(pt
->child
)) {
628 if(with_nodes
) { // Stop on the node
629 return PLAY_TREE_ITER_NODE
;
630 } else // Or follow it
631 return play_tree_iter_down_step(iter
,d
,with_nodes
);
634 // Is it a valid entry?
635 if(! play_tree_is_valid(pt
)) {
636 if(d
== 0) { // Can this happen ? FF: Yes!
637 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"What to do now ???? Infinite loop if we continue\n");
638 return PLAY_TREE_ITER_ERROR
;
639 } // Not a valid entry : go to next one
640 return play_tree_iter_step(iter
,d
,with_nodes
);
644 assert(pt
->files
!= NULL
);
649 for(d
= 0 ; iter
->tree
->files
[d
] != NULL
; d
++)
654 play_tree_iter_push_params(iter
);
655 iter
->entry_pushed
= 1;
656 if(iter
->mode
== PLAY_TREE_ITER_RND
)
657 pt
->flags
|= PLAY_TREE_RND_PLAYED
;
660 return PLAY_TREE_ITER_ENTRY
;
665 play_tree_is_valid(play_tree_t
* pt
) {
672 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
) {
674 assert(pt
->child
== NULL
);
678 else if (pt
->child
!= NULL
) {
679 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
) {
680 if(play_tree_is_valid(iter
))
688 play_tree_iter_up_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
691 assert(iter
!= NULL
);
692 assert(iter
->tree
!= NULL
);
693 //printf("PT : Go UP\n");
697 if(iter
->tree
->parent
== iter
->root
->parent
)
698 return PLAY_TREE_ITER_END
;
701 assert(iter
->tree
->parent
!= NULL
);
702 assert(iter
->stack_size
> 0);
703 assert(iter
->status_stack
!= NULL
);
707 iter
->loop
= iter
->status_stack
[iter
->stack_size
];
708 if(iter
->stack_size
> 0)
709 iter
->status_stack
= realloc(iter
->status_stack
, iter
->stack_size
* sizeof(int));
711 free(iter
->status_stack
);
712 iter
->status_stack
= NULL
;
714 if(iter
->stack_size
> 0 && iter
->status_stack
== NULL
) {
715 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*(int)sizeof(char*));
716 return PLAY_TREE_ITER_ERROR
;
718 iter
->tree
= iter
->tree
->parent
;
720 // Pop subtree params
722 m_config_pop(iter
->config
);
723 if(iter
->mode
== PLAY_TREE_ITER_RND
)
724 iter
->tree
->flags
|= PLAY_TREE_RND_PLAYED
;
727 return play_tree_iter_step(iter
,d
,with_nodes
);
731 play_tree_iter_down_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
734 assert(iter
->tree
->files
== NULL
);
735 assert(iter
->tree
->child
!= NULL
);
736 assert(iter
->tree
->child
->parent
== iter
->tree
);
737 //printf("PT : Go DOWN\n");
742 // Push subtree params
744 play_tree_iter_push_params(iter
);
747 iter
->status_stack
= realloc(iter
->status_stack
, iter
->stack_size
* sizeof(int));
748 if(iter
->status_stack
== NULL
) {
749 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*(int)sizeof(int));
750 return PLAY_TREE_ITER_ERROR
;
752 iter
->status_stack
[iter
->stack_size
-1] = iter
->loop
;
754 iter
->loop
= iter
->tree
->loop
-1;
756 iter
->tree
= iter
->tree
->child
;
759 for(pt
= iter
->tree
->child
; pt
->next
!= NULL
; pt
= pt
->next
)
764 return play_tree_iter_step(iter
,0,with_nodes
);
768 play_tree_iter_get_file(play_tree_iter_t
* iter
, int d
) {
770 assert(iter
!= NULL
);
771 assert(iter
->tree
->child
== NULL
);
774 if(iter
->tree
->files
== NULL
)
778 assert(iter
->num_files
> 0);
781 if(iter
->file
>= iter
->num_files
-1 || iter
->file
< -1)
785 if(iter
->file
>= iter
->num_files
- 1)
791 iter
->file
= iter
->num_files
- 1;
795 return iter
->tree
->files
[iter
->file
];
799 play_tree_cleanup(play_tree_t
* pt
) {
800 play_tree_t
* iter
, *tmp
, *first
;
806 if( ! play_tree_is_valid(pt
)) {
807 play_tree_remove(pt
,1,1);
813 for(iter
= pt
->child
; iter
!= NULL
; ) {
816 if(! play_tree_is_valid(tmp
)) {
817 play_tree_remove(tmp
,1,1);
818 if(tmp
== first
) first
= iter
;
822 for(iter
= first
; iter
!= NULL
; ) {
825 play_tree_cleanup(tmp
);
833 play_tree_iter_new_copy(play_tree_iter_t
* old
) {
834 play_tree_iter_t
* iter
;
840 iter
= malloc(sizeof(play_tree_iter_t
));
842 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_iter_t
));
846 memcpy(iter
,old
,sizeof(play_tree_iter_t
));
847 if(old
->status_stack
) {
848 iter
->status_stack
= malloc(old
->stack_size
* sizeof(int));
849 if(iter
->status_stack
== NULL
) {
850 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",old
->stack_size
* (int)sizeof(int));
854 memcpy(iter
->status_stack
,old
->status_stack
,iter
->stack_size
*sizeof(int));
861 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
863 play_tree_iter_t
* pt_iter_create(play_tree_t
** ppt
, m_config_t
* config
)
865 play_tree_iter_t
* r
=NULL
;
870 *ppt
=play_tree_cleanup(*ppt
);
873 r
= play_tree_iter_new(*ppt
,config
);
874 if (r
&& play_tree_iter_step(r
,0,0) != PLAY_TREE_ITER_ENTRY
)
876 play_tree_iter_free(r
);
884 void pt_iter_destroy(play_tree_iter_t
** iter
)
893 char* pt_iter_get_file(play_tree_iter_t
* iter
, int d
)
901 r
= play_tree_iter_get_file(iter
,d
);
905 if (play_tree_iter_step(iter
,d
,0) != PLAY_TREE_ITER_ENTRY
)
907 r
=play_tree_iter_get_file(iter
,d
);
914 void pt_iter_insert_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
916 play_tree_t
*pt
= iter
->tree
;
923 play_tree_insert_entry(pt
, entry
);
924 play_tree_set_params_from(entry
,pt
);
927 void pt_iter_replace_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
929 play_tree_t
*pt
= iter
->tree
;
931 pt_iter_insert_entry(iter
, entry
);
932 play_tree_remove(pt
, 1, 1);
936 //Add a new file as a new entry
937 void pt_add_file(play_tree_t
** ppt
, char* filename
)
939 play_tree_t
*pt
= *ppt
, *entry
= play_tree_new();
944 play_tree_add_file(entry
, filename
);
946 play_tree_append_entry(pt
, entry
);
952 play_tree_set_params_from(entry
,pt
);
955 void pt_iter_goto_head(play_tree_iter_t
* iter
)
957 iter
->tree
=iter
->root
;
958 play_tree_iter_step(iter
, 0, 0);