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
)
71 //if(pt->params) free(pt->params);
74 for(i
= 0 ; pt
->files
[i
] != NULL
; i
++)
83 play_tree_free_list(play_tree_t
* pt
, int children
) {
90 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
94 play_tree_t
* nxt
= iter
->next
;
95 play_tree_free(iter
, children
);
103 play_tree_append_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
108 assert(entry
!= NULL
);
114 for(iter
= pt
; iter
->next
!= NULL
; iter
= iter
->next
)
117 entry
->parent
= iter
->parent
;
124 play_tree_prepend_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
129 assert(entry
!= NULL
);
132 for(iter
= pt
; iter
->prev
!= NULL
; iter
= iter
->prev
)
137 entry
->parent
= iter
->parent
;
142 assert(entry
->parent
->child
== iter
);
144 entry
->parent
->child
= entry
;
149 play_tree_insert_entry(play_tree_t
* pt
, play_tree_t
* entry
) {
153 assert(entry
!= NULL
);
156 entry
->parent
= pt
->parent
;
160 assert(pt
->next
->prev
== pt
);
162 entry
->next
= pt
->next
;
163 entry
->next
->prev
= entry
;
171 play_tree_remove(play_tree_t
* pt
, int free_it
, int with_children
) {
178 if(pt
->prev
&& pt
->next
) {
180 assert(pt
->prev
->next
== pt
);
181 assert(pt
->next
->prev
== pt
);
183 pt
->prev
->next
= pt
->next
;
184 pt
->next
->prev
= pt
->prev
;
188 assert(pt
->prev
->next
== pt
);
190 pt
->prev
->next
= NULL
;
191 } // Beginning of list
194 assert(pt
->next
->prev
== pt
);
196 pt
->next
->prev
= NULL
;
199 assert(pt
->parent
->child
== pt
);
201 pt
->parent
->child
= pt
->next
;
204 else if(pt
->parent
) {
206 assert(pt
->parent
->child
== pt
);
208 pt
->parent
->child
= NULL
;
211 pt
->prev
= pt
->next
= pt
->parent
= NULL
;
213 play_tree_free(pt
,with_children
);
218 play_tree_set_child(play_tree_t
* pt
, play_tree_t
* child
) {
221 /* Roughly validate input data. Both, pt and child are going to be
222 * dereferenced, hence assure they're not NULL.
225 mp_msg(MSGT_PLAYTREE
, MSGL_ERR
, "Internal error, attempt to add an empty child or use empty playlist\n");
230 assert(pt
->entry_type
== PLAY_TREE_ENTRY_NODE
);
233 //DEBUG_FF: Where are the children freed?
234 // Attention in using this function!
235 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
)
238 // Go back to first one
239 for(iter
= child
; iter
->prev
!= NULL
; iter
= iter
->prev
)
244 for( ; iter
!= NULL
; iter
= iter
->next
)
250 play_tree_set_parent(play_tree_t
* pt
, play_tree_t
* parent
) {
258 pt
->parent
->child
= NULL
;
260 for(iter
= pt
; iter
!= NULL
; iter
= iter
->next
)
261 iter
->parent
= parent
;
264 for(iter
= pt
->prev
; iter
->prev
!= NULL
; iter
= iter
->prev
)
265 iter
->parent
= parent
;
266 iter
->parent
= parent
;
267 parent
->child
= iter
;
275 play_tree_add_file(play_tree_t
* pt
,char* file
) {
281 assert(pt
->child
== NULL
);
282 assert(file
!= NULL
);
285 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
&&
286 pt
->entry_type
!= PLAY_TREE_ENTRY_FILE
)
290 for(n
= 0 ; pt
->files
[n
] != NULL
; n
++)
293 pt
->files
= realloc(pt
->files
, (n
+ 2) * sizeof(char*));
294 if(pt
->files
==NULL
) {
295 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*(int)sizeof(char*));
299 e
= pt
->files
[n
] = strdup(file
);
300 pt
->files
[n
+1] = NULL
;
302 pt
->entry_type
= PLAY_TREE_ENTRY_FILE
;
307 play_tree_remove_file(play_tree_t
* pt
,char* file
) {
312 assert(file
!= NULL
);
313 assert(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
);
316 for(n
=0 ; pt
->files
[n
] != NULL
; n
++) {
317 if(strcmp(file
,pt
->files
[n
]) == 0)
321 if(f
< 0) // Not found
331 memmove(&pt
->files
[f
],&pt
->files
[f
+1],(n
-f
)*sizeof(char*));
332 pt
->files
= realloc(pt
->files
, n
* sizeof(char*));
333 if(pt
->files
== NULL
) {
334 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(n
+2)*(int)sizeof(char*));
346 play_tree_set_param(play_tree_t
* pt
, char* name
, char* val
) {
351 assert(name
!= NULL
);
355 for ( ; pt
->params
[n
].name
!= NULL
; n
++ ) { }
357 pt
->params
= realloc(pt
->params
, (n
+ 2) * sizeof(play_tree_param_t
));
358 if(pt
->params
== NULL
) {
359 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't realloc params (%d bytes of memory)\n",(n
+2)*(int)sizeof(play_tree_param_t
));
362 pt
->params
[n
].name
= strdup(name
);
363 pt
->params
[n
].value
= val
!= NULL
? strdup(val
) : NULL
;
364 memset(&pt
->params
[n
+1],0,sizeof(play_tree_param_t
));
370 play_tree_unset_param(play_tree_t
* pt
, char* name
) {
375 assert(name
!= NULL
);
376 assert(pt
->params
!= NULL
);
379 for(n
= 0 ; pt
->params
[n
].name
!= NULL
; n
++) {
380 if(strcasecmp(pt
->params
[n
].name
,name
) == 0)
387 if(pt
->params
[ni
].name
) free(pt
->params
[ni
].name
);
388 if(pt
->params
[ni
].value
) free(pt
->params
[ni
].value
);
391 memmove(&pt
->params
[ni
],&pt
->params
[ni
+1],(n
-ni
)*sizeof(play_tree_param_t
));
392 pt
->params
= realloc(pt
->params
, n
* sizeof(play_tree_param_t
));
393 if(pt
->params
== NULL
) {
394 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",n
*(int)sizeof(play_tree_param_t
));
406 play_tree_set_params_from(play_tree_t
* dest
,play_tree_t
* src
) {
410 assert(dest
!= NULL
);
417 for(i
= 0; src
->params
[i
].name
!= NULL
; i
++)
418 play_tree_set_param(dest
,src
->params
[i
].name
,src
->params
[i
].value
);
419 if(src
->flags
& PLAY_TREE_RND
) // pass the random flag too
420 dest
->flags
|= PLAY_TREE_RND
;
425 play_tree_unset_flag(play_tree_t
* pt
, int flags
, int deep
) {
430 if(deep
&& pt
->child
) {
432 for(i
= pt
->child
; i
; i
= i
->next
)
433 play_tree_unset_flag(i
,flags
,deep
);
438 //////////////////////////////////// ITERATOR //////////////////////////////////////
441 play_tree_iter_push_params(play_tree_iter_t
* iter
) {
445 assert(iter
!= NULL
);
446 assert(iter
->config
!= NULL
);
447 assert(iter
->tree
!= NULL
);
452 // We always push a config because we can set some option
454 m_config_push(iter
->config
);
456 if(pt
->params
== NULL
)
460 for(n
= 0; pt
->params
[n
].name
!= NULL
; n
++) {
462 if((e
= m_config_set_option(iter
->config
,pt
->params
[n
].name
,pt
->params
[n
].value
)) < 0) {
463 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Error %d while setting option '%s' with value '%s'\n",e
,
464 pt
->params
[n
].name
,pt
->params
[n
].value
);
469 iter
->entry_pushed
= 1;
474 play_tree_iter_new(play_tree_t
* pt
,m_config_t
* config
) {
475 play_tree_iter_t
* iter
;
479 assert(config
!= NULL
);
482 if( ! play_tree_is_valid(pt
))
485 iter
= calloc(1,sizeof(play_tree_iter_t
));
487 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate new iterator (%d bytes of memory)\n",(int)sizeof(play_tree_iter_t
));
492 iter
->config
= config
;
495 iter
->loop
= pt
->parent
->loop
;
501 play_tree_iter_free(play_tree_iter_t
* iter
) {
504 assert(iter
!= NULL
);
507 if(iter
->status_stack
) {
509 assert(iter
->stack_size
> 0);
511 free(iter
->status_stack
);
518 play_tree_rnd_step(play_tree_t
* pt
) {
521 play_tree_t
*i
,*head
;
523 // Count how many free choice we have
524 for(i
= pt
; i
->prev
; i
= i
->prev
)
525 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
527 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
528 for(i
= pt
->next
; i
; i
= i
->next
)
529 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) count
++;
531 if(!count
) return NULL
;
533 r
= (int)((float)(count
) * rand() / (RAND_MAX
+ 1.0));
535 for(i
= head
; i
; i
=i
->next
) {
536 if(!(i
->flags
& PLAY_TREE_RND_PLAYED
)) r
--;
540 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Random stepping error\n");
546 play_tree_iter_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
549 if ( !iter
) return PLAY_TREE_ITER_ENTRY
;
550 if ( !iter
->root
) return PLAY_TREE_ITER_ENTRY
;
553 assert(iter
!= NULL
);
554 assert(iter
->root
!= NULL
);
555 //printf("PT : Stepping = %d\n",d);
558 if(iter
->tree
== NULL
) {
559 iter
->tree
= iter
->root
;
560 return play_tree_iter_step(iter
,0,with_nodes
);
563 if(iter
->config
&& iter
->entry_pushed
> 0) {
564 iter
->entry_pushed
= 0;
565 m_config_pop(iter
->config
);
568 if(iter
->tree
->parent
&& (iter
->tree
->parent
->flags
& PLAY_TREE_RND
))
569 iter
->mode
= PLAY_TREE_ITER_RND
;
571 iter
->mode
= PLAY_TREE_ITER_NORMAL
;
574 if(iter
->mode
== PLAY_TREE_ITER_RND
)
575 pt
= play_tree_rnd_step(iter
->tree
);
579 for(i
= d
; i
> 0 && pt
; i
--)
585 for(i
= d
; i
< 0 && pt
; i
++)
591 if(pt
== NULL
) { // No next
593 if (iter
->mode
== PLAY_TREE_ITER_RND
) {
594 if (iter
->root
->loop
== 0)
595 return PLAY_TREE_ITER_END
;
596 play_tree_unset_flag(iter
->root
, PLAY_TREE_RND_PLAYED
, -1);
597 if (iter
->root
->loop
> 0) iter
->root
->loop
--;
599 return play_tree_iter_step(iter
, 0, with_nodes
);
601 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
) ) ) ) {
602 if(d
> 0) { // Go back to the first one
603 for(pt
= iter
->tree
; pt
->prev
!= NULL
; pt
= pt
->prev
)
605 if(iter
->loop
> 0) iter
->loop
--;
606 } else if( d
< 0 ) { // Or the last one
607 for(pt
= iter
->tree
; pt
->next
!= NULL
; pt
= pt
->next
)
609 if(iter
->loop
>= 0 && iter
->loop
< iter
->tree
->parent
->loop
) iter
->loop
++;
612 return play_tree_iter_step(iter
,0,with_nodes
);
615 return play_tree_iter_up_step(iter
,d
,with_nodes
);
619 // Is there any valid child?
620 if(pt
->child
&& play_tree_is_valid(pt
->child
)) {
622 if(with_nodes
) { // Stop on the node
623 return PLAY_TREE_ITER_NODE
;
624 } else // Or follow it
625 return play_tree_iter_down_step(iter
,d
,with_nodes
);
628 // Is it a valid entry?
629 if(! play_tree_is_valid(pt
)) {
630 if(d
== 0) { // Can this happen ? FF: Yes!
631 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"What to do now ???? Infinite loop if we continue\n");
632 return PLAY_TREE_ITER_ERROR
;
633 } // Not a valid entry : go to next one
634 return play_tree_iter_step(iter
,d
,with_nodes
);
638 assert(pt
->files
!= NULL
);
643 for(d
= 0 ; iter
->tree
->files
[d
] != NULL
; d
++)
648 play_tree_iter_push_params(iter
);
649 iter
->entry_pushed
= 1;
650 if(iter
->mode
== PLAY_TREE_ITER_RND
)
651 pt
->flags
|= PLAY_TREE_RND_PLAYED
;
654 return PLAY_TREE_ITER_ENTRY
;
659 play_tree_is_valid(play_tree_t
* pt
) {
666 if(pt
->entry_type
!= PLAY_TREE_ENTRY_NODE
) {
668 assert(pt
->child
== NULL
);
672 else if (pt
->child
!= NULL
) {
673 for(iter
= pt
->child
; iter
!= NULL
; iter
= iter
->next
) {
674 if(play_tree_is_valid(iter
))
682 play_tree_iter_up_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
685 assert(iter
!= NULL
);
686 assert(iter
->tree
!= NULL
);
687 //printf("PT : Go UP\n");
691 if(iter
->tree
->parent
== iter
->root
->parent
)
692 return PLAY_TREE_ITER_END
;
695 assert(iter
->tree
->parent
!= NULL
);
696 assert(iter
->stack_size
> 0);
697 assert(iter
->status_stack
!= NULL
);
701 iter
->loop
= iter
->status_stack
[iter
->stack_size
];
702 if(iter
->stack_size
> 0)
703 iter
->status_stack
= realloc(iter
->status_stack
, iter
->stack_size
* sizeof(int));
705 free(iter
->status_stack
);
706 iter
->status_stack
= NULL
;
708 if(iter
->stack_size
> 0 && iter
->status_stack
== NULL
) {
709 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*(int)sizeof(char*));
710 return PLAY_TREE_ITER_ERROR
;
712 iter
->tree
= iter
->tree
->parent
;
714 // Pop subtree params
716 m_config_pop(iter
->config
);
717 if(iter
->mode
== PLAY_TREE_ITER_RND
)
718 iter
->tree
->flags
|= PLAY_TREE_RND_PLAYED
;
721 return play_tree_iter_step(iter
,d
,with_nodes
);
725 play_tree_iter_down_step(play_tree_iter_t
* iter
, int d
,int with_nodes
) {
728 assert(iter
->tree
->files
== NULL
);
729 assert(iter
->tree
->child
!= NULL
);
730 assert(iter
->tree
->child
->parent
== iter
->tree
);
731 //printf("PT : Go DOWN\n");
736 // Push subtree params
738 play_tree_iter_push_params(iter
);
741 iter
->status_stack
= realloc(iter
->status_stack
, iter
->stack_size
* sizeof(int));
742 if(iter
->status_stack
== NULL
) {
743 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",iter
->stack_size
*(int)sizeof(int));
744 return PLAY_TREE_ITER_ERROR
;
746 iter
->status_stack
[iter
->stack_size
-1] = iter
->loop
;
748 iter
->loop
= iter
->tree
->loop
-1;
750 iter
->tree
= iter
->tree
->child
;
753 for(pt
= iter
->tree
->child
; pt
->next
!= NULL
; pt
= pt
->next
)
758 return play_tree_iter_step(iter
,0,with_nodes
);
762 play_tree_iter_get_file(play_tree_iter_t
* iter
, int d
) {
764 assert(iter
!= NULL
);
765 assert(iter
->tree
->child
== NULL
);
768 if(iter
->tree
->files
== NULL
)
772 assert(iter
->num_files
> 0);
775 if(iter
->file
>= iter
->num_files
-1 || iter
->file
< -1)
779 if(iter
->file
>= iter
->num_files
- 1)
785 iter
->file
= iter
->num_files
- 1;
789 return iter
->tree
->files
[iter
->file
];
793 play_tree_cleanup(play_tree_t
* pt
) {
794 play_tree_t
* iter
, *tmp
, *first
;
800 if( ! play_tree_is_valid(pt
)) {
801 play_tree_remove(pt
,1,1);
807 for(iter
= pt
->child
; iter
!= NULL
; ) {
810 if(! play_tree_is_valid(tmp
)) {
811 play_tree_remove(tmp
,1,1);
812 if(tmp
== first
) first
= iter
;
816 for(iter
= first
; iter
!= NULL
; ) {
819 play_tree_cleanup(tmp
);
827 play_tree_iter_new_copy(play_tree_iter_t
* old
) {
828 play_tree_iter_t
* iter
;
834 iter
= malloc(sizeof(play_tree_iter_t
));
836 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",(int)sizeof(play_tree_iter_t
));
840 memcpy(iter
,old
,sizeof(play_tree_iter_t
));
841 if(old
->status_stack
) {
842 iter
->status_stack
= malloc(old
->stack_size
* sizeof(int));
843 if(iter
->status_stack
== NULL
) {
844 mp_msg(MSGT_PLAYTREE
,MSGL_ERR
,"Can't allocate %d bytes of memory\n",old
->stack_size
* (int)sizeof(int));
848 memcpy(iter
->status_stack
,old
->status_stack
,iter
->stack_size
*sizeof(int));
855 // HIGH Level API, by Fabian Franz (mplayer@fabian-franz.de)
857 play_tree_iter_t
* pt_iter_create(play_tree_t
** ppt
, m_config_t
* config
)
859 play_tree_iter_t
* r
=NULL
;
864 *ppt
=play_tree_cleanup(*ppt
);
867 r
= play_tree_iter_new(*ppt
,config
);
868 if (r
&& play_tree_iter_step(r
,0,0) != PLAY_TREE_ITER_ENTRY
)
870 play_tree_iter_free(r
);
878 void pt_iter_destroy(play_tree_iter_t
** iter
)
887 char* pt_iter_get_file(play_tree_iter_t
* iter
, int d
)
895 r
= play_tree_iter_get_file(iter
,d
);
899 if (play_tree_iter_step(iter
,d
,0) != PLAY_TREE_ITER_ENTRY
)
901 r
=play_tree_iter_get_file(iter
,d
);
908 void pt_iter_insert_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
910 play_tree_t
*pt
= iter
->tree
;
917 play_tree_insert_entry(pt
, entry
);
918 play_tree_set_params_from(entry
,pt
);
921 void pt_iter_replace_entry(play_tree_iter_t
* iter
, play_tree_t
* entry
)
923 play_tree_t
*pt
= iter
->tree
;
925 pt_iter_insert_entry(iter
, entry
);
926 play_tree_remove(pt
, 1, 1);
930 //Add a new file as a new entry
931 void pt_add_file(play_tree_t
** ppt
, char* filename
)
933 play_tree_t
*pt
= *ppt
, *entry
= play_tree_new();
938 play_tree_add_file(entry
, filename
);
940 play_tree_append_entry(pt
, entry
);
946 play_tree_set_params_from(entry
,pt
);
949 void pt_iter_goto_head(play_tree_iter_t
* iter
)
951 iter
->tree
=iter
->root
;
952 play_tree_iter_step(iter
, 0, 0);