19 play_tree_is_valid(play_tree_t
* pt
);
23 play_tree_t
* r
= calloc(1,sizeof(play_tree_t
));
25 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_t
));
28 r
->entry_type
= PLAY_TREE_ENTRY_NODE
;
33 play_tree_free(play_tree_t
* pt
, int children
) {
41 for(iter
= pt
->child
; iter
!= NULL
; ) {
42 play_tree_t
* nxt
=iter
->next
;
43 play_tree_free(iter
,1);
49 play_tree_remove(pt
,0,0);
51 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
54 //if(pt->params) free(pt->params);
57 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++)
66 play_tree_free_list(play_tree_t
* pt
, int children
) {
73 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
77 play_tree_t
* nxt
= iter
->next
;
78 play_tree_free(iter
, children
);
86 play_tree_append_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
91 assert(entry
!= NULL
);
97 for(iter
= pt
; iter
->next
!= NULL
; iter
= iter
->next
)
100 entry
->parent
= iter
->parent
;
107 play_tree_prepend_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
112 assert(entry
!= NULL
);
115 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
120 entry
->parent
= iter
->parent
;
125 assert(entry
->parent
->child
== iter
);
127 entry
->parent
->child
= entry
;
132 play_tree_insert_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
136 assert(entry
!= NULL
);
139 entry
->parent
= pt
->parent
;
143 assert(pt
->next
->prev
== pt
);
145 entry
->next
= pt
->next
;
146 entry
->next
->prev
= entry
;
154 play_tree_remove(play_tree_t
* pt
, int free_it
, int with_children
) {
161 if(pt
->prev
&& pt
->next
) {
163 assert(pt
->prev
->next
== pt
);
164 assert(pt
->next
->prev
== pt
);
166 pt
->prev
->next
= pt
->next
;
167 pt
->next
->prev
= pt
->prev
;
171 assert(pt
->prev
->next
== pt
);
173 pt
->prev
->next
= NULL
;
174 } // Beginning of list
177 assert(pt
->next
->prev
== pt
);
179 pt
->next
->prev
= NULL
;
182 assert(pt
->parent
->child
== pt
);
184 pt
->parent
->child
= pt
->next
;
187 else if(pt
->parent
) {
189 assert(pt
->parent
->child
== pt
);
191 pt
->parent
->child
= NULL
;
194 pt
->prev
= pt
->next
= pt
->parent
= NULL
;
196 play_tree_free(pt
,with_children
);
201 play_tree_set_child(play_tree_t
* pt
, play_tree_t
* child
) {
206 assert(pt
->entry_type
== PLAY_TREE_ENTRY_NODE
);
209 //DEBUG_FF: Where are the children freed?
210 // Attention in using this function!
211 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
214 // Go back to first one
215 for(iter
= child
; iter
->prev
!= NULL
; iter
= iter
->prev
)
220 for( ; iter
!= NULL
; iter
= iter
->next
)
226 play_tree_set_parent(play_tree_t
* pt
, play_tree_t
* parent
) {
234 pt
->parent
->child
= NULL
;
236 for(iter
= pt
; iter
!= NULL
; iter
= iter
->next
)
237 iter
->parent
= parent
;
240 for(iter
= pt
->prev
; iter
->prev
!= NULL
; iter
= iter
->prev
)
241 iter
->parent
= parent
;
242 iter
->parent
= parent
;
243 parent
->child
= iter
;
251 play_tree_add_file(play_tree_t
* pt
,char* file
) {
257 assert(pt
->child
== NULL
);
258 assert(file
!= NULL
);
261 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
&&
262 pt
->entry_type
!= PLAY_TREE_ENTRY_FILE
)
266 for(n
= 0 ; pt
->files
[n
] != NULL
; n
++)
269 pt
->files
= (char**)realloc(pt
->files
,(n
+2)*sizeof(char*));
270 if(pt
->files
==NULL
) {
271 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*(int)sizeof(char*));
275 e
= pt
->files
[n
] = strdup(file
);
276 pt
->files
[n
+1] = NULL
;
278 pt
->entry_type
= PLAY_TREE_ENTRY_FILE
;
283 play_tree_remove_file(play_tree_t
* pt
,char* file
) {
288 assert(file
!= NULL
);
289 assert(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
);
292 for(n
=0 ; pt
->files
[n
] != NULL
; n
++) {
293 if(strcmp(file
,pt
->files
[n
]) == 0)
297 if(f
< 0) // Not found
307 memmove(&pt
->files
[f
],&pt
->files
[f
+1],(n
-f
)*sizeof(char*));
308 pt
->files
= (char**)realloc(pt
->files
,n
*sizeof(char*));
309 if(pt
->files
== NULL
) {
310 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*(int)sizeof(char*));
322 play_tree_set_param(play_tree_t
* pt
, char* name
, char* val
) {
327 assert(name
!= NULL
);
331 for ( ; pt
->params
[n
].name
!= NULL
; n
++ ) { }
333 pt
->params
= (play_tree_param_t
*)realloc(pt
->params
,(n
+2)*sizeof(play_tree_param_t
));
334 if(pt
->params
== NULL
) {
335 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't realloc params (%d bytes of memory)\n",(n
+2)*(int)sizeof(play_tree_param_t
));
338 pt
->params
[n
].name
= strdup(name
);
339 pt
->params
[n
].value
= val
!= NULL
? strdup(val
) : NULL
;
340 memset(&pt
->params
[n
+1],0,sizeof(play_tree_param_t
));
346 play_tree_unset_param(play_tree_t
* pt
, char* name
) {
351 assert(name
!= NULL
);
352 assert(pt
->params
!= NULL
);
355 for(n
= 0 ; pt
->params
[n
].name
!= NULL
; n
++) {
356 if(strcasecmp(pt
->params
[n
].name
,name
) == 0)
363 if(pt
->params
[ni
].name
) free(pt
->params
[ni
].name
);
364 if(pt
->params
[ni
].value
) free(pt
->params
[ni
].value
);
367 memmove(&pt
->params
[ni
],&pt
->params
[ni
+1],(n
-ni
)*sizeof(play_tree_param_t
));
368 pt
->params
= (play_tree_param_t
*)realloc(pt
->params
,n
*sizeof(play_tree_param_t
));
369 if(pt
->params
== NULL
) {
370 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",n
*(int)sizeof(play_tree_param_t
));
382 play_tree_set_params_from(play_tree_t
* dest
,play_tree_t
* src
) {
386 assert(dest
!= NULL
);
393 for(i
= 0; src
->params
[i
].name
!= NULL
; i
++)
394 play_tree_set_param(dest
,src
->params
[i
].name
,src
->params
[i
].value
);
395 if(src
->flags
& PLAY_TREE_RND
) // pass the random flag too
396 dest
->flags
|= PLAY_TREE_RND
;
400 // all children if deep < 0
402 play_tree_set_flag(play_tree_t
* pt
, int flags
, int deep
) {
407 if(deep
&& pt
->child
) {
409 for(i
= pt
->child
; i
; i
= i
->next
)
410 play_tree_set_flag(i
,flags
,deep
);
415 play_tree_unset_flag(play_tree_t
* pt
, int flags
, int deep
) {
420 if(deep
&& pt
->child
) {
422 for(i
= pt
->child
; i
; i
= i
->next
)
423 play_tree_unset_flag(i
,flags
,deep
);
428 //////////////////////////////////// ITERATOR //////////////////////////////////////
431 play_tree_iter_push_params(play_tree_iter_t
* iter
) {
435 assert(iter
!= NULL
);
436 assert(iter
->config
!= NULL
);
437 assert(iter
->tree
!= NULL
);
442 // We always push a config because we can set some option
444 m_config_push(iter
->config
);
446 if(pt
->params
== NULL
)
450 for(n
= 0; pt
->params
[n
].name
!= NULL
; n
++) {
452 if((e
= m_config_set_option(iter
->config
,pt
->params
[n
].name
,pt
->params
[n
].value
)) < 0) {
453 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error %d while setting option '%s' with value '%s'\n",e
,
454 pt
->params
[n
].name
,pt
->params
[n
].value
);
459 iter
->entry_pushed
= 1;
464 play_tree_iter_new(play_tree_t
* pt
,m_config_t
* config
) {
465 play_tree_iter_t
* iter
;
469 assert(config
!= NULL
);
472 if( ! play_tree_is_valid(pt
))
475 iter
= calloc(1,sizeof(play_tree_iter_t
));
477 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate new iterator (%d bytes of memory)\n",(int)sizeof(play_tree_iter_t
));
482 iter
->config
= config
;
485 iter
->loop
= pt
->parent
->loop
;
491 play_tree_iter_free(play_tree_iter_t
* iter
) {
494 assert(iter
!= NULL
);
497 if(iter
->status_stack
) {
499 assert(iter
->stack_size
> 0);
501 free(iter
->status_stack
);
508 play_tree_rnd_step(play_tree_t
* pt
) {
511 play_tree_t
*i
,*head
;
513 // Count how many free choice we have
514 for(i
= pt
; i
->prev
; i
= i
->prev
)
515 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
517 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
518 for(i
= pt
->next
; i
; i
= i
->next
)
519 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
521 if(!count
) return NULL
;
523 r
= (int)((float)(count
) * rand() / (RAND_MAX
+ 1.0));
525 for(i
= head
; i
; i
=i
->next
) {
526 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) r
--;
530 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Random stepping error\n");
536 play_tree_iter_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
539 if ( !iter
) return PLAY_TREE_ITER_ENTRY
;
540 if ( !iter
->root
) return PLAY_TREE_ITER_ENTRY
;
543 assert(iter
!= NULL
);
544 assert(iter
->root
!= NULL
);
545 //printf("PT : Stepping = %d\n",d);
548 if(iter
->tree
== NULL
) {
549 iter
->tree
= iter
->root
;
550 return play_tree_iter_step(iter
,0,with_nodes
);
553 if(iter
->config
&& iter
->entry_pushed
> 0) {
554 iter
->entry_pushed
= 0;
555 m_config_pop(iter
->config
);
558 if(iter
->tree
->parent
&& (iter
->tree
->parent
->flags
& PLAY_TREE_RND
))
559 iter
->mode
= PLAY_TREE_ITER_RND
;
561 iter
->mode
= PLAY_TREE_ITER_NORMAL
;
564 if(iter
->mode
== PLAY_TREE_ITER_RND
)
565 pt
= play_tree_rnd_step(iter
->tree
);
569 for(i
= d
; i
> 0 && pt
; i
--)
575 for(i
= d
; i
< 0 && pt
; i
++)
581 if(pt
== NULL
) { // No next
583 if (iter
->mode
== PLAY_TREE_ITER_RND
) {
584 if (iter
->root
->loop
== 0)
585 return PLAY_TREE_ITER_END
;
586 play_tree_unset_flag(iter
->root
, PLAY_TREE_RND_PLAYED
, -1);
587 if (iter
->root
->loop
> 0) iter
->root
->loop
--;
589 return play_tree_iter_step(iter
, 0, with_nodes
);
591 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
) ) ) ) {
592 if(d
> 0) { // Go back to the first one
593 for(pt
= iter
->tree
; pt
->prev
!= NULL
; pt
= pt
->prev
)
595 if(iter
->loop
> 0) iter
->loop
--;
596 } else if( d
< 0 ) { // Or the last one
597 for(pt
= iter
->tree
; pt
->next
!= NULL
; pt
= pt
->next
)
599 if(iter
->loop
>= 0 && iter
->loop
< iter
->tree
->parent
->loop
) iter
->loop
++;
602 return play_tree_iter_step(iter
,0,with_nodes
);
605 return play_tree_iter_up_step(iter
,d
,with_nodes
);
609 // Is there any valid child?
610 if(pt
->child
&& play_tree_is_valid(pt
->child
)) {
612 if(with_nodes
) { // Stop on the node
613 return PLAY_TREE_ITER_NODE
;
614 } else // Or follow it
615 return play_tree_iter_down_step(iter
,d
,with_nodes
);
618 // Is it a valid entry?
619 if(! play_tree_is_valid(pt
)) {
620 if(d
== 0) { // Can this happen ? FF: Yes!
621 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"What to do now ???? Infinite loop if we continue\n");
622 return PLAY_TREE_ITER_ERROR
;
623 } // Not a valid entry : go to next one
624 return play_tree_iter_step(iter
,d
,with_nodes
);
628 assert(pt
->files
!= NULL
);
633 for(d
= 0 ; iter
->tree
->files
[d
] != NULL
; d
++)
638 play_tree_iter_push_params(iter
);
639 iter
->entry_pushed
= 1;
640 if(iter
->mode
== PLAY_TREE_ITER_RND
)
641 pt
->flags
|= PLAY_TREE_RND_PLAYED
;
644 return PLAY_TREE_ITER_ENTRY
;
649 play_tree_is_valid(play_tree_t
* pt
) {
656 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
) {
658 assert(pt
->child
== NULL
);
662 else if (pt
->child
!= NULL
) {
663 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
) {
664 if(play_tree_is_valid(iter
))
672 play_tree_iter_up_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
675 assert(iter
!= NULL
);
676 assert(iter
->tree
!= NULL
);
677 //printf("PT : Go UP\n");
681 if(iter
->tree
->parent
== iter
->root
->parent
)
682 return PLAY_TREE_ITER_END
;
685 assert(iter
->tree
->parent
!= NULL
);
686 assert(iter
->stack_size
> 0);
687 assert(iter
->status_stack
!= NULL
);
691 iter
->loop
= iter
->status_stack
[iter
->stack_size
];
692 if(iter
->stack_size
> 0)
693 iter
->status_stack
= (int*)realloc(iter
->status_stack
,iter
->stack_size
*sizeof(int));
695 free(iter
->status_stack
);
696 iter
->status_stack
= NULL
;
698 if(iter
->stack_size
> 0 && iter
->status_stack
== NULL
) {
699 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*(int)sizeof(char*));
700 return PLAY_TREE_ITER_ERROR
;
702 iter
->tree
= iter
->tree
->parent
;
704 // Pop subtree params
706 m_config_pop(iter
->config
);
707 if(iter
->mode
== PLAY_TREE_ITER_RND
)
708 iter
->tree
->flags
|= PLAY_TREE_RND_PLAYED
;
711 return play_tree_iter_step(iter
,d
,with_nodes
);
715 play_tree_iter_down_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
718 assert(iter
->tree
->files
== NULL
);
719 assert(iter
->tree
->child
!= NULL
);
720 assert(iter
->tree
->child
->parent
== iter
->tree
);
721 //printf("PT : Go DOWN\n");
726 // Push subtree params
728 play_tree_iter_push_params(iter
);
731 iter
->status_stack
= (int*)realloc(iter
->status_stack
,iter
->stack_size
*sizeof(int));
732 if(iter
->status_stack
== NULL
) {
733 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*(int)sizeof(int));
734 return PLAY_TREE_ITER_ERROR
;
736 iter
->status_stack
[iter
->stack_size
-1] = iter
->loop
;
738 iter
->loop
= iter
->tree
->loop
-1;
740 iter
->tree
= iter
->tree
->child
;
743 for(pt
= iter
->tree
->child
; pt
->next
!= NULL
; pt
= pt
->next
)
748 return play_tree_iter_step(iter
,0,with_nodes
);
752 play_tree_iter_get_file(play_tree_iter_t
* iter
, int d
) {
754 assert(iter
!= NULL
);
755 assert(iter
->tree
->child
== NULL
);
758 if(iter
->tree
->files
== NULL
)
762 assert(iter
->num_files
> 0);
765 if(iter
->file
>= iter
->num_files
-1 || iter
->file
< -1)
769 if(iter
->file
>= iter
->num_files
- 1)
775 iter
->file
= iter
->num_files
- 1;
779 return iter
->tree
->files
[iter
->file
];
783 play_tree_cleanup(play_tree_t
* pt
) {
784 play_tree_t
* iter
, *tmp
, *first
;
790 if( ! play_tree_is_valid(pt
)) {
791 play_tree_remove(pt
,1,1);
797 for(iter
= pt
->child
; iter
!= NULL
; ) {
800 if(! play_tree_is_valid(tmp
)) {
801 play_tree_remove(tmp
,1,1);
802 if(tmp
== first
) first
= iter
;
806 for(iter
= first
; iter
!= NULL
; ) {
809 play_tree_cleanup(tmp
);
817 play_tree_iter_new_copy(play_tree_iter_t
* old
) {
818 play_tree_iter_t
* iter
;
824 iter
= malloc(sizeof(play_tree_iter_t
));
826 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_iter_t
));
830 memcpy(iter
,old
,sizeof(play_tree_iter_t
));
831 if(old
->status_stack
) {
832 iter
->status_stack
= malloc(old
->stack_size
* sizeof(int));
833 if(iter
->status_stack
== NULL
) {
834 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",old
->stack_size
* (int)sizeof(int));
838 memcpy(iter
->status_stack
,old
->status_stack
,iter
->stack_size
*sizeof(int));
845 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
847 play_tree_iter_t
* pt_iter_create(play_tree_t
** ppt
, m_config_t
* config
)
849 play_tree_iter_t
* r
=NULL
;
854 *ppt
=play_tree_cleanup(*ppt
);
857 r
= play_tree_iter_new(*ppt
,config
);
858 if (r
&& play_tree_iter_step(r
,0,0) != PLAY_TREE_ITER_ENTRY
)
860 play_tree_iter_free(r
);
868 void pt_iter_destroy(play_tree_iter_t
** iter
)
877 char* pt_iter_get_file(play_tree_iter_t
* iter
, int d
)
885 r
= play_tree_iter_get_file(iter
,d
);
889 if (play_tree_iter_step(iter
,d
,0) != PLAY_TREE_ITER_ENTRY
)
891 r
=play_tree_iter_get_file(iter
,d
);
898 void pt_iter_insert_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
900 play_tree_t
*pt
= iter
->tree
;
907 play_tree_insert_entry(pt
, entry
);
908 play_tree_set_params_from(entry
,pt
);
911 void pt_iter_replace_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
913 play_tree_t
*pt
= iter
->tree
;
915 pt_iter_insert_entry(iter
, entry
);
916 play_tree_remove(pt
, 1, 1);
920 //Add a new file as a new entry
921 void pt_add_file(play_tree_t
** ppt
, char* filename
)
923 play_tree_t
*pt
= *ppt
, *entry
= play_tree_new();
928 play_tree_add_file(entry
, filename
);
930 play_tree_append_entry(pt
, entry
);
936 play_tree_set_params_from(entry
,pt
);
939 void pt_add_gui_file(play_tree_t
** ppt
, char* path
, char* file
)
941 char* wholename
= malloc(strlen(path
)+strlen(file
)+2);
945 strcpy(wholename
, path
);
946 strcat(wholename
, "/");
947 strcat(wholename
, file
);
948 pt_add_file(ppt
, wholename
);
949 free(wholename
); // As pt_add_file strdups it anyway!
953 void pt_iter_goto_head(play_tree_iter_t
* iter
)
955 iter
->tree
=iter
->root
;
956 play_tree_iter_step(iter
, 0, 0);