]> git.sesse.net Git - vlc/blob - modules/video_filter/dynamicoverlay/dynamicoverlay_list.c
06d4170c8798d351fbdaa672e18f8a25a2d58d7d
[vlc] / modules / video_filter / dynamicoverlay / dynamicoverlay_list.c
1 /*****************************************************************************
2  * dynamicoverlay_list.h : dynamic overlay list
3  *****************************************************************************
4  * Copyright (C) 2008-2009 the VideoLAN team
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
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 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 General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, 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_osd.h>
31 #include <vlc_memory.h>
32
33 #include <fcntl.h>
34 #include "dynamicoverlay.h"
35
36 /*****************************************************************************
37  * list_t: Command queue
38  *****************************************************************************/
39
40 int ListInit( list_t *p_list )
41 {
42     p_list->pp_head = calloc( 16, sizeof( overlay_t * ) );
43     if( p_list->pp_head == NULL )
44         return VLC_ENOMEM;
45
46     p_list->pp_tail = p_list->pp_head + 16;
47     return VLC_SUCCESS;
48 }
49
50 int ListDestroy( list_t *p_list )
51 {
52     for( overlay_t **pp_cur = p_list->pp_head;
53          pp_cur < p_list->pp_tail;
54          ++pp_cur )
55     {
56         if( *pp_cur != NULL )
57         {
58             OverlayDestroy( *pp_cur );
59             free( *pp_cur );
60         }
61     }
62     free( p_list->pp_head );
63
64     return VLC_SUCCESS;
65 }
66
67 ssize_t ListAdd( list_t *p_list, overlay_t *p_new )
68 {
69     /* Find an available slot */
70     for( overlay_t **pp_cur = p_list->pp_head;
71          pp_cur < p_list->pp_tail;
72          ++pp_cur )
73     {
74         if( *pp_cur == NULL )
75         {
76             *pp_cur = p_new;
77             return pp_cur - p_list->pp_head;
78         }
79     }
80
81     /* Have to expand */
82     size_t i_size = p_list->pp_tail - p_list->pp_head;
83     size_t i_newsize = i_size * 2;
84     p_list->pp_head = realloc_or_free( p_list->pp_head,
85                                        i_newsize * sizeof( overlay_t * ) );
86     if( p_list->pp_head == NULL )
87         return VLC_ENOMEM;
88
89     p_list->pp_tail = p_list->pp_head + i_newsize;
90     memset( p_list->pp_head + i_size, 0, i_size * sizeof( overlay_t * ) );
91     p_list->pp_head[i_size] = p_new;
92     return i_size;
93 }
94
95 int ListRemove( list_t *p_list, size_t i_idx )
96 {
97     int ret;
98
99     if( ( i_idx >= (size_t)( p_list->pp_tail - p_list->pp_head ) ) ||
100         ( p_list->pp_head[i_idx] == NULL ) )
101     {
102         return VLC_EGENERIC;
103     }
104
105     ret = OverlayDestroy( p_list->pp_head[i_idx] );
106     free( p_list->pp_head[i_idx] );
107     p_list->pp_head[i_idx] = NULL;
108
109     return ret;
110 }
111
112 overlay_t *ListGet( list_t *p_list, size_t i_idx )
113 {
114     if( ( i_idx >= (size_t)( p_list->pp_tail - p_list->pp_head ) ) ||
115         ( p_list->pp_head[i_idx] == NULL ) )
116     {
117         return NULL;
118     }
119     return p_list->pp_head[i_idx];
120 }
121
122 overlay_t *ListWalk( list_t *p_list )
123 {
124     static overlay_t **pp_cur = NULL;
125
126     if( pp_cur == NULL )
127         pp_cur = p_list->pp_head;
128     else
129         pp_cur = pp_cur + 1;
130
131     for( ; pp_cur < p_list->pp_tail; ++pp_cur )
132     {
133         if( ( *pp_cur != NULL ) &&
134             ( (*pp_cur)->b_active == true )&&
135             ( (*pp_cur)->format.i_chroma != VLC_FOURCC( '\0','\0','\0','\0') ) )
136         {
137             return *pp_cur;
138         }
139     }
140     pp_cur = NULL;
141     return NULL;
142 }