]> git.sesse.net Git - vlc/blob - dynamicoverlay_list.c
1eca32d64eca893b884caee2eaed9845e58e5025
[vlc] / dynamicoverlay_list.c
1 /*****************************************************************************
2  * dynamicoverlay_list.c : dynamic overlay list
3  *****************************************************************************
4  * Copyright (C) 2008-2009 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Author: Søren Bøg <avacore@videolan.org>
8  *         Jean-Paul Saman <jpsaman@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <vlc_common.h>
30 #include <vlc_memory.h>
31
32 #include "dynamicoverlay.h"
33
34 /*****************************************************************************
35  * list_t: Command queue
36  *****************************************************************************/
37
38 int do_ListInit( list_t *p_list )
39 {
40     p_list->pp_head = calloc( 16, sizeof( overlay_t * ) );
41     if( p_list->pp_head == NULL )
42         return VLC_ENOMEM;
43
44     p_list->pp_tail = p_list->pp_head + 16;
45     return VLC_SUCCESS;
46 }
47
48 int do_ListDestroy( list_t *p_list )
49 {
50     for( overlay_t **pp_cur = p_list->pp_head;
51          pp_cur < p_list->pp_tail;
52          ++pp_cur )
53     {
54         if( *pp_cur != NULL )
55         {
56             OverlayDestroy( *pp_cur );
57             free( *pp_cur );
58         }
59     }
60     free( p_list->pp_head );
61
62     return VLC_SUCCESS;
63 }
64
65 ssize_t ListAdd( list_t *p_list, overlay_t *p_new )
66 {
67     /* Find an available slot */
68     for( overlay_t **pp_cur = p_list->pp_head;
69          pp_cur < p_list->pp_tail;
70          ++pp_cur )
71     {
72         if( *pp_cur == NULL )
73         {
74             *pp_cur = p_new;
75             return pp_cur - p_list->pp_head;
76         }
77     }
78
79     /* Have to expand */
80     size_t i_size = p_list->pp_tail - p_list->pp_head;
81     size_t i_newsize = i_size * 2;
82     p_list->pp_head = realloc_or_free( p_list->pp_head,
83                                        i_newsize * sizeof( overlay_t * ) );
84     if( p_list->pp_head == NULL )
85         return VLC_ENOMEM;
86
87     p_list->pp_tail = p_list->pp_head + i_newsize;
88     memset( p_list->pp_head + i_size, 0, i_size * sizeof( overlay_t * ) );
89     p_list->pp_head[i_size] = p_new;
90     return i_size;
91 }
92
93 int ListRemove( list_t *p_list, size_t i_idx )
94 {
95     int ret;
96
97     if( ( i_idx >= (size_t)( p_list->pp_tail - p_list->pp_head ) ) ||
98         ( p_list->pp_head[i_idx] == NULL ) )
99     {
100         return VLC_EGENERIC;
101     }
102
103     ret = OverlayDestroy( p_list->pp_head[i_idx] );
104     free( p_list->pp_head[i_idx] );
105     p_list->pp_head[i_idx] = NULL;
106
107     return ret;
108 }
109
110 overlay_t *ListGet( list_t *p_list, size_t i_idx )
111 {
112     if( ( i_idx >= (size_t)( p_list->pp_tail - p_list->pp_head ) ) ||
113         ( p_list->pp_head[i_idx] == NULL ) )
114     {
115         return NULL;
116     }
117     return p_list->pp_head[i_idx];
118 }
119
120 overlay_t *ListWalk( list_t *p_list )
121 {
122     static overlay_t **pp_cur = NULL;
123
124     if( pp_cur == NULL )
125         pp_cur = p_list->pp_head;
126     else
127         pp_cur = pp_cur + 1;
128
129     for( ; pp_cur < p_list->pp_tail; ++pp_cur )
130     {
131         if( ( *pp_cur != NULL ) &&
132             ( (*pp_cur)->b_active )&&
133             ( (*pp_cur)->format.i_chroma != VLC_FOURCC( '\0','\0','\0','\0') ) )
134         {
135             return *pp_cur;
136         }
137     }
138     pp_cur = NULL;
139     return NULL;
140 }