16 play_tree_is_valid(play_tree_t
* pt
);
20 play_tree_t
* r
= (play_tree_t
*)calloc(1,sizeof(play_tree_t
));
22 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",sizeof(play_tree_t
));
23 r
->entry_type
= PLAY_TREE_ENTRY_NODE
;
28 play_tree_free(play_tree_t
* pt
, int childs
) {
36 for(iter
= pt
->child
; iter
!= NULL
; ) {
37 play_tree_t
* nxt
=iter
->next
;
38 play_tree_free(iter
,1);
44 play_tree_remove(pt
,0,0);
46 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
49 //if(pt->params) free(pt->params);
52 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++)
61 play_tree_free_list(play_tree_t
* pt
, int childs
) {
68 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
72 play_tree_t
* nxt
= iter
->next
;
73 play_tree_free(iter
,childs
);
81 play_tree_append_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
86 assert(entry
!= NULL
);
92 for(iter
= pt
; iter
->next
!= NULL
; iter
= iter
->next
)
95 entry
->parent
= iter
->parent
;
102 play_tree_prepend_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
107 assert(entry
!= NULL
);
110 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
115 entry
->parent
= iter
->parent
;
120 assert(entry
->parent
->child
== iter
);
122 entry
->parent
->child
= entry
;
127 play_tree_insert_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
131 assert(entry
!= NULL
);
134 entry
->parent
= pt
->parent
;
138 assert(pt
->next
->prev
== pt
);
140 entry
->next
= pt
->next
;
141 entry
->next
->prev
= entry
;
149 play_tree_remove(play_tree_t
* pt
, int free_it
,int with_childs
) {
156 if(pt
->prev
&& pt
->next
) {
158 assert(pt
->prev
->next
== pt
);
159 assert(pt
->next
->prev
== pt
);
161 pt
->prev
->next
= pt
->next
;
162 pt
->next
->prev
= pt
->prev
;
166 assert(pt
->prev
->next
== pt
);
168 pt
->prev
->next
= NULL
;
169 } // Begining of list
172 assert(pt
->next
->prev
== pt
);
174 pt
->next
->prev
= NULL
;
177 assert(pt
->parent
->child
== pt
);
179 pt
->parent
->child
= pt
->next
;
182 else if(pt
->parent
) {
184 assert(pt
->parent
->child
== pt
);
186 pt
->parent
->child
= NULL
;
189 pt
->prev
= pt
->next
= pt
->parent
= NULL
;
191 play_tree_free(pt
,with_childs
);
196 play_tree_set_child(play_tree_t
* pt
, play_tree_t
* child
) {
201 assert(pt
->entry_type
== PLAY_TREE_ENTRY_NODE
);
204 //DEBUG_FF: Where are the childs freed ?
205 // Attention in using this function!
206 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
209 // Go back to first one
210 for(iter
= child
; iter
->prev
!= NULL
; iter
= iter
->prev
)
215 for( ; iter
!= NULL
; iter
= iter
->next
)
221 play_tree_set_parent(play_tree_t
* pt
, play_tree_t
* parent
) {
229 pt
->parent
->child
= NULL
;
231 for(iter
= pt
; iter
!= NULL
; iter
= iter
->next
)
232 iter
->parent
= parent
;
235 for(iter
= pt
->prev
; iter
->prev
!= NULL
; iter
= iter
->prev
)
236 iter
->parent
= parent
;
237 iter
->parent
= parent
;
238 parent
->child
= iter
;
246 play_tree_add_file(play_tree_t
* pt
,char* file
) {
252 assert(pt
->child
== NULL
);
253 assert(file
!= NULL
);
256 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
&&
257 pt
->entry_type
!= PLAY_TREE_ENTRY_FILE
)
261 for(n
= 0 ; pt
->files
[n
] != NULL
; n
++)
264 pt
->files
= (char**)realloc(pt
->files
,(n
+2)*sizeof(char*));
265 if(pt
->files
==NULL
) {
266 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*sizeof(char*));
270 e
= pt
->files
[n
] = strdup(file
);
271 pt
->files
[n
+1] = NULL
;
273 pt
->entry_type
= PLAY_TREE_ENTRY_FILE
;
278 play_tree_remove_file(play_tree_t
* pt
,char* file
) {
283 assert(file
!= NULL
);
284 assert(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
);
287 for(n
=0 ; pt
->files
[n
] != NULL
; n
++) {
288 if(strcmp(file
,pt
->files
[n
]) == 0)
292 if(f
< 0) // Not found
302 memmove(&pt
->files
[f
],&pt
->files
[f
+1],(n
-f
)*sizeof(char*));
303 pt
->files
= (char**)realloc(pt
->files
,n
*sizeof(char*));
304 if(pt
->files
== NULL
) {
305 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*sizeof(char*));
317 play_tree_set_param(play_tree_t
* pt
, char* name
, char* val
) {
322 assert(name
!= NULL
);
326 for( ; pt
->params
[n
].name
!= NULL
; n
++) {
327 if(strcasecmp(pt
->params
[n
].name
,name
) == 0)
333 if(pt
->params
[n
].value
!= NULL
) free(pt
->params
[n
].value
);
334 pt
->params
[n
].value
= val
!= NULL
? strdup(val
) : NULL
;
338 pt
->params
= (play_tree_param_t
*)realloc(pt
->params
,(n
+2)*sizeof(play_tree_param_t
));
339 if(pt
->params
== NULL
) {
340 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't realloc params (%d bytes of memory)\n",(n
+2)*sizeof(play_tree_param_t
));
343 pt
->params
[n
].name
= strdup(name
);
344 pt
->params
[n
].value
= val
!= NULL
? strdup(val
) : NULL
;
345 memset(&pt
->params
[n
+1],0,sizeof(play_tree_param_t
));
351 play_tree_unset_param(play_tree_t
* pt
, char* name
) {
356 assert(name
!= NULL
);
357 assert(pt
->params
!= NULL
);
360 for(n
= 0 ; pt
->params
[n
].name
!= NULL
; n
++) {
361 if(strcasecmp(pt
->params
[n
].name
,name
) == 0)
368 if(pt
->params
[ni
].name
) free(pt
->params
[ni
].name
);
369 if(pt
->params
[ni
].value
) free(pt
->params
[ni
].value
);
372 memmove(&pt
->params
[ni
],&pt
->params
[ni
+1],(n
-ni
)*sizeof(play_tree_param_t
));
373 pt
->params
= (play_tree_param_t
*)realloc(pt
->params
,n
*sizeof(play_tree_param_t
));
374 if(pt
->params
== NULL
) {
375 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",n
*sizeof(play_tree_param_t
));
387 play_tree_set_params_from(play_tree_t
* dest
,play_tree_t
* src
) {
391 assert(dest
!= NULL
);
398 for(i
= 0; src
->params
[i
].name
!= NULL
; i
++)
399 play_tree_set_param(dest
,src
->params
[i
].name
,src
->params
[i
].value
);
400 if(src
->flags
& PLAY_TREE_RND
) // pass the random flag too
401 dest
->flags
|= PLAY_TREE_RND
;
405 // all childs if deep < 0
407 play_tree_set_flag(play_tree_t
* pt
, int flags
, int deep
) {
412 if(deep
&& pt
->child
) {
414 for(i
= pt
->child
; i
; i
= i
->next
)
415 play_tree_set_flag(i
,flags
,deep
);
420 play_tree_unset_flag(play_tree_t
* pt
, int flags
, int deep
) {
425 if(deep
&& pt
->child
) {
427 for(i
= pt
->child
; i
; i
= i
->next
)
428 play_tree_unset_flag(i
,flags
,deep
);
433 //////////////////////////////////// ITERATOR //////////////////////////////////////
436 play_tree_iter_push_params(play_tree_iter_t
* iter
) {
440 assert(iter
!= NULL
);
441 assert(iter
->config
!= NULL
);
442 assert(iter
->tree
!= NULL
);
447 // We always push a config because we can set some option
449 m_config_push(iter
->config
);
451 if(pt
->params
== NULL
)
455 for(n
= 0; pt
->params
[n
].name
!= NULL
; n
++) {
457 if((e
= m_config_set_option(iter
->config
,pt
->params
[n
].name
,pt
->params
[n
].value
)) < 0) {
458 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error %d while setting option '%s' with value '%s'\n",e
,
459 pt
->params
[n
].name
,pt
->params
[n
].value
);
464 iter
->entry_pushed
= 1;
469 play_tree_iter_new(play_tree_t
* pt
,m_config_t
* config
) {
470 play_tree_iter_t
* iter
;
474 assert(config
!= NULL
);
477 if( ! play_tree_is_valid(pt
))
480 iter
= (play_tree_iter_t
*)calloc(1,sizeof(play_tree_iter_t
));
482 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate new iterator (%d bytes of memory)\n",sizeof(play_tree_iter_t
));
487 iter
->config
= config
;
490 iter
->loop
= pt
->parent
->loop
;
496 play_tree_iter_free(play_tree_iter_t
* iter
) {
499 assert(iter
!= NULL
);
502 if(iter
->status_stack
) {
504 assert(iter
->stack_size
> 0);
506 free(iter
->status_stack
);
513 play_tree_rnd_step(play_tree_t
* pt
) {
516 play_tree_t
*i
,*head
;
518 // Count how many free choice we have
519 for(i
= pt
; i
->prev
; i
= i
->prev
)
520 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
522 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
523 for(i
= pt
->next
; i
; i
= i
->next
)
524 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
526 if(!count
) return NULL
;
528 r
= (int)((count
-1.0) * rand() / RAND_MAX
);
530 for(i
= head
; i
; i
=i
->next
) {
531 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) r
--;
535 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Random stepping error\n");
541 play_tree_iter_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
544 if ( !iter
) return PLAY_TREE_ITER_ENTRY
;
545 if ( !iter
->root
) return PLAY_TREE_ITER_ENTRY
;
548 assert(iter
!= NULL
);
549 assert(iter
->root
!= NULL
);
550 //printf("PT : Stepping = %d\n",d);
553 if(iter
->tree
== NULL
) {
554 iter
->tree
= iter
->root
;
555 return play_tree_iter_step(iter
,0,with_nodes
);
558 if(iter
->config
&& iter
->entry_pushed
> 0) {
559 iter
->entry_pushed
= 0;
560 m_config_pop(iter
->config
);
563 if(iter
->tree
->parent
&& (iter
->tree
->parent
->flags
& PLAY_TREE_RND
))
564 iter
->mode
= PLAY_TREE_ITER_RND
;
566 iter
->mode
= PLAY_TREE_ITER_NORMAL
;
569 if(iter
->mode
== PLAY_TREE_ITER_RND
)
570 pt
= play_tree_rnd_step(iter
->tree
);
574 for(i
= d
; i
> 0 && pt
; i
--)
580 for(i
= d
; i
< 0 && pt
; i
++)
586 if(pt
== NULL
) { // No next
588 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
) ) ) ) {
589 if(d
> 0) { // Go back to the first one
590 for(pt
= iter
->tree
; pt
->prev
!= NULL
; pt
= pt
->prev
)
592 if(iter
->loop
> 0) iter
->loop
--;
593 } else if( d
< 0 ) { // Or the last one
594 for(pt
= iter
->tree
; pt
->next
!= NULL
; pt
= pt
->next
)
596 if(iter
->loop
>= 0 && iter
->loop
< iter
->tree
->parent
->loop
) iter
->loop
++;
599 return play_tree_iter_step(iter
,0,with_nodes
);
602 return play_tree_iter_up_step(iter
,d
,with_nodes
);
606 // Is there any valid childs ?
607 if(pt
->child
&& play_tree_is_valid(pt
->child
)) {
609 if(with_nodes
) { // Stop on the node
610 return PLAY_TREE_ITER_NODE
;
611 } else // Or follow it
612 return play_tree_iter_down_step(iter
,d
,with_nodes
);
615 // Is it a valid enty ?
616 if(! play_tree_is_valid(pt
)) {
617 if(d
== 0) { // Can this happen ? FF: Yes!
618 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"What to do now ???? Infinite loop if we continue\n");
619 return PLAY_TREE_ITER_ERROR
;
620 } // Not a valid entry : go to next one
621 return play_tree_iter_step(iter
,d
,with_nodes
);
625 assert(pt
->files
!= NULL
);
630 for(d
= 0 ; iter
->tree
->files
[d
] != NULL
; d
++)
635 play_tree_iter_push_params(iter
);
636 iter
->entry_pushed
= 1;
637 if(iter
->mode
== PLAY_TREE_ITER_RND
)
638 pt
->flags
|= PLAY_TREE_RND_PLAYED
;
641 return PLAY_TREE_ITER_ENTRY
;
646 play_tree_is_valid(play_tree_t
* pt
) {
653 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
) {
655 assert(pt
->child
== NULL
);
659 else if (pt
->child
!= NULL
) {
660 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
) {
661 if(play_tree_is_valid(iter
))
669 play_tree_iter_up_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
672 assert(iter
!= NULL
);
673 assert(iter
->tree
!= NULL
);
674 //printf("PT : Go UP\n");
678 if(iter
->tree
->parent
== iter
->root
->parent
)
679 return PLAY_TREE_ITER_END
;
682 assert(iter
->tree
->parent
!= NULL
);
683 assert(iter
->stack_size
> 0);
684 assert(iter
->status_stack
!= NULL
);
688 iter
->loop
= iter
->status_stack
[iter
->stack_size
];
689 if(iter
->stack_size
> 0)
690 iter
->status_stack
= (int*)realloc(iter
->status_stack
,iter
->stack_size
*sizeof(int));
692 free(iter
->status_stack
);
693 iter
->status_stack
= NULL
;
695 if(iter
->stack_size
> 0 && iter
->status_stack
== NULL
) {
696 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*sizeof(char*));
697 return PLAY_TREE_ITER_ERROR
;
699 iter
->tree
= iter
->tree
->parent
;
701 // Pop subtree params
703 m_config_pop(iter
->config
);
704 if(iter
->mode
== PLAY_TREE_ITER_RND
)
705 iter
->tree
->flags
|= PLAY_TREE_RND_PLAYED
;
708 return play_tree_iter_step(iter
,d
,with_nodes
);
712 play_tree_iter_down_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
715 assert(iter
->tree
->files
== NULL
);
716 assert(iter
->tree
->child
!= NULL
);
717 assert(iter
->tree
->child
->parent
== iter
->tree
);
718 //printf("PT : Go DOWN\n");
723 // Push subtree params
725 play_tree_iter_push_params(iter
);
728 iter
->status_stack
= (int*)realloc(iter
->status_stack
,iter
->stack_size
*sizeof(int));
729 if(iter
->status_stack
== NULL
) {
730 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*sizeof(int));
731 return PLAY_TREE_ITER_ERROR
;
733 iter
->status_stack
[iter
->stack_size
-1] = iter
->loop
;
735 iter
->loop
= iter
->tree
->loop
-1;
737 iter
->tree
= iter
->tree
->child
;
740 for(pt
= iter
->tree
->child
; pt
->next
!= NULL
; pt
= pt
->next
)
745 return play_tree_iter_step(iter
,0,with_nodes
);
749 play_tree_iter_get_file(play_tree_iter_t
* iter
, int d
) {
752 assert(iter
!= NULL
);
753 assert(iter
->tree
->child
== NULL
);
756 if(iter
->tree
->files
== NULL
)
760 assert(iter
->num_files
> 0);
763 if(iter
->file
>= iter
->num_files
-1 || iter
->file
< -1)
767 if(iter
->file
>= iter
->num_files
- 1)
773 iter
->file
= iter
->num_files
- 1;
777 return iter
->tree
->files
[iter
->file
];
781 play_tree_cleanup(play_tree_t
* pt
) {
782 play_tree_t
* iter
, *tmp
, *first
;
788 if( ! play_tree_is_valid(pt
)) {
789 play_tree_remove(pt
,1,1);
795 for(iter
= pt
->child
; iter
!= NULL
; ) {
798 if(! play_tree_is_valid(tmp
)) {
799 play_tree_remove(tmp
,1,1);
800 if(tmp
== first
) first
= iter
;
804 for(iter
= first
; iter
!= NULL
; ) {
807 play_tree_cleanup(tmp
);
815 play_tree_iter_new_copy(play_tree_iter_t
* old
) {
816 play_tree_iter_t
* iter
;
822 iter
= (play_tree_iter_t
*)malloc(sizeof(play_tree_iter_t
));
824 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",sizeof(play_tree_iter_t
));
828 memcpy(iter
,old
,sizeof(play_tree_iter_t
));
829 if(old
->status_stack
) {
830 iter
->status_stack
= (int*)malloc(old
->stack_size
* sizeof(int));
831 if(iter
->status_stack
== NULL
) {
832 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",old
->stack_size
* sizeof(int));
836 memcpy(iter
->status_stack
,old
->status_stack
,iter
->stack_size
*sizeof(int));
843 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
845 play_tree_iter_t
* pt_iter_create(play_tree_t
** ppt
, m_config_t
* config
)
847 play_tree_iter_t
* r
=NULL
;
852 *ppt
=play_tree_cleanup(*ppt
);
855 r
= play_tree_iter_new(*ppt
,config
);
856 if (r
&& play_tree_iter_step(r
,0,0) != PLAY_TREE_ITER_ENTRY
)
858 play_tree_iter_free(r
);
866 void pt_iter_destroy(play_tree_iter_t
** iter
)
875 char* pt_iter_get_file(play_tree_iter_t
* iter
, int d
)
883 r
= play_tree_iter_get_file(iter
,d
);
887 if (play_tree_iter_step(iter
,d
,0) != PLAY_TREE_ITER_ENTRY
)
889 r
=play_tree_iter_get_file(iter
,d
);
896 void pt_iter_insert_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
898 play_tree_t
*pt
= iter
->tree
;
905 play_tree_insert_entry(pt
, entry
);
906 play_tree_set_params_from(entry
,pt
);
909 void pt_iter_replace_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
911 play_tree_t
*pt
= iter
->tree
;
913 pt_iter_insert_entry(iter
, entry
);
914 play_tree_remove(pt
, 1, 1);
918 //Add a new file as a new entry
919 void pt_add_file(play_tree_t
** ppt
, char* filename
)
921 play_tree_t
*pt
= *ppt
, *entry
= play_tree_new();
926 play_tree_add_file(entry
, filename
);
928 play_tree_append_entry(pt
, entry
);
934 play_tree_set_params_from(entry
,pt
);
937 void pt_add_gui_file(play_tree_t
** ppt
, char* path
, char* file
)
939 char* wholename
= malloc(strlen(path
)+strlen(file
)+2);
943 strcpy(wholename
, path
);
944 strcat(wholename
, "/");
945 strcat(wholename
, file
);
946 pt_add_file(ppt
, wholename
);
947 free(wholename
); // As pt_add_file strdups it anyway!
951 void pt_iter_goto_head(play_tree_iter_t
* iter
)
953 iter
->tree
=iter
->root
;
954 play_tree_iter_step(iter
, 0, 0);