]> git.sesse.net Git - vlc/blob - modules/codec/cmml/history.c
Input access locking, part 3 (final).
[vlc] / modules / codec / cmml / history.c
1 /*****************************************************************************
2  * history.c: vlc_history_t (web-browser-like back/forward history) handling
3  *****************************************************************************
4  * Copyright (C) 2004 Commonwealth Scientific and Industrial Research
5  *                    Organisation (CSIRO) Australia
6  * Copyright (C) 2004 the VideoLAN team
7  *
8  * $Id$
9  *
10  * Authors: Andre Pang <Andre.Pang@csiro.au>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #include <vlc/vlc.h>
28
29 #include "history.h"
30
31 #include "xarray.h"
32
33 #ifdef HAVE_STDLIB_H
34 #   include <stdlib.h>                                          /* realloc() */
35 #endif
36
37 #undef HISTORY_DEBUG
38
39 /*****************************************************************************
40  * Local prototypes
41  *****************************************************************************/
42 static void history_Dump( history_t *p_history );
43
44 /*****************************************************************************
45  * Local structure lock
46  *****************************************************************************/
47
48 /*****************************************************************************
49  * Actual history code
50  *****************************************************************************/
51
52 history_t *history_New( void )
53 {
54    history_t *p_new_history;
55    
56    p_new_history = calloc( 1, sizeof( struct history_t ) );
57    if( p_new_history == NULL ) return NULL;
58
59    p_new_history->p_xarray = xarray_New( 0 );
60    if( p_new_history->p_xarray == NULL )
61    {
62        free( p_new_history );
63        return NULL;
64    }
65
66 #ifndef HISTORY_DEBUG
67    /* make dummy reference to history_Dump to avoid compiler warnings */
68    while (0)
69    {
70        void *p_tmp;
71
72        p_tmp = history_Dump;
73    }
74 #endif
75
76    return p_new_history;
77 }
78
79 vlc_bool_t history_GoBackSavingCurrentItem ( history_t *p_history,
80                                              history_item_t *p_item )
81 {
82     history_PruneAndInsert( p_history, p_item );
83
84     /* PruneAndInsert will increment the index, so we need to go
85      * back one position to reset the index to the place we were at
86      * before saving the current state, and then go back one more to
87      * actually go back */
88     p_history->i_index -= 2;
89
90 #ifdef HISTORY_DEBUG
91     history_Dump( p_history );
92 #endif
93     return VLC_TRUE;
94 }
95
96 static void history_Dump( history_t *p_history )
97 {
98     unsigned int i_count;
99     int i;
100
101     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
102         return;
103
104     for (i = 0; i < (int) i_count; i++)
105     {
106         history_item_t *p_item;
107         void *pv_item;
108
109         xarray_ObjectAtIndex( p_history->p_xarray, i, &pv_item );
110
111         p_item = (history_item_t *) pv_item;
112
113         if( p_item == NULL )
114             fprintf( stderr, "HISTORY: [%d] NULL\n", i );
115         else
116         {
117             char *psz_uri = input_item_GetURI( p_item );
118             fprintf( stderr, "HISTORY: [%d] %p (%p->%s)\n", i, p_item,
119                      psz_uri, psz_uri );
120             free( psz_uri );
121         }
122     }
123 }
124
125 vlc_bool_t history_GoForwardSavingCurrentItem ( history_t *p_history,
126                                                 history_item_t *p_item )
127 {
128 #ifdef HISTORY_DEBUG
129     history_Dump( p_history );
130 #endif
131
132     if( xarray_ReplaceObject( p_history->p_xarray, p_history->i_index, p_item )
133         == XARRAY_SUCCESS )
134     {
135         p_history->i_index++;
136         return VLC_TRUE;
137     }
138     else
139     {
140         return VLC_FALSE;
141     }
142 }
143
144 vlc_bool_t history_CanGoBack( history_t *p_history )
145 {
146     if( p_history->i_index > 0 )
147         return VLC_TRUE;
148     else
149         return VLC_FALSE;
150 }
151
152 vlc_bool_t history_CanGoForward( history_t *p_history )
153 {
154     unsigned int i_count;
155
156     if( xarray_Count( p_history->p_xarray, &i_count ) != XARRAY_SUCCESS )
157         return VLC_FALSE;
158
159     if( p_history->i_index < i_count )
160         return VLC_TRUE;
161     else
162         return VLC_FALSE;
163 }
164
165 history_item_t *history_Item( history_t *p_history )
166 {
167     history_item_t *p_item;
168     void *pv_item;
169
170     if( xarray_ObjectAtIndex( p_history->p_xarray, p_history->i_index,
171                               &pv_item )
172         == XARRAY_SUCCESS )
173     {
174         p_item = (history_item_t *) pv_item;
175         return p_item;
176     }
177     else
178     {
179         return NULL;
180     }
181 }
182
183 void history_Prune( history_t *p_history )
184 {
185     xarray_RemoveObjectsAfter( p_history->p_xarray, p_history->i_index );
186     xarray_RemoveObject( p_history->p_xarray, p_history->i_index );
187 }
188
189 void history_PruneAndInsert( history_t *p_history, history_item_t *p_item )
190 {
191     unsigned int i_count;
192
193     xarray_Count( p_history->p_xarray, &i_count );
194
195     if( i_count == 0 )
196     {
197         xarray_InsertObject( p_history->p_xarray, p_item, 0 );
198         p_history->i_index = 1;
199     }
200     else
201     {
202         history_Prune( p_history );
203         xarray_InsertObject( p_history->p_xarray, p_item, p_history->i_index );
204         p_history->i_index++;
205     }
206 }
207
208 unsigned int history_Count( history_t *p_history )
209 {
210     unsigned int i_count;
211     xarray_Count( p_history->p_xarray, &i_count );
212     return i_count;
213 }
214
215 unsigned int history_Index( history_t *p_history )
216 {
217     return p_history->i_index;
218 }
219
220 history_item_t * historyItem_New( char *psz_name, char *psz_uri )
221 {
222     history_item_t *p_history_item = NULL;
223
224     p_history_item = (history_item_t *) malloc( sizeof(history_item_t) );
225     if( !p_history_item ) return NULL;
226
227     p_history_item->psz_uri = strdup( psz_uri );
228     p_history_item->psz_name = strdup( psz_name );
229
230     return p_history_item;
231 }
232