]> git.sesse.net Git - vlc/blob - modules/access/vcdx/intf.c
Don't include config.h from the headers - refs #297.
[vlc] / modules / access / vcdx / intf.c
1 /*****************************************************************************
2  * intf.c: Video CD interface to handle user interaction and still time
3  *****************************************************************************
4  * Copyright (C) 2002,2003 the VideoLAN team
5  * $Id$
6  *
7  * Author: Rocky Bernstein <rocky@panix.com>
8  *   from DVD code by Stéphane Borel <stef@via.ecp.fr>
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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <vlc_interface.h>
34 #include <vlc_access.h>
35
36 #include "vlc_keys.h"
37
38 #include "vcd.h"
39 #include "vcdplayer.h"
40 #include "intf.h"
41
42 /*****************************************************************************
43  * Local prototypes.
44  *****************************************************************************/
45 static int  InitThread     ( intf_thread_t *p_intf );
46 static int  KeyEvent       ( vlc_object_t *, char const *,
47                              vlc_value_t, vlc_value_t, void * );
48
49 /* Exported functions */
50 static void RunIntf        ( intf_thread_t *p_intf );
51
52 /*****************************************************************************
53  * OpenIntf: initialize dummy interface
54  *****************************************************************************/
55 int VCDOpenIntf ( vlc_object_t *p_this )
56 {
57     intf_thread_t *p_intf = (intf_thread_t *)p_this;
58
59     msg_Dbg( p_intf, "VCDOpenIntf" );
60
61     /* Allocate instance and initialize some members */
62     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
63     if( p_intf->p_sys == NULL )
64     {
65         return( VLC_EGENERIC );
66     };
67
68     p_intf->pf_run = RunIntf;
69
70     var_AddCallback( p_intf->p_libvlc, "key-pressed", KeyEvent, p_intf );
71     p_intf->p_sys->m_still_time = 0;
72     p_intf->p_sys->b_infinite_still = 0;
73     p_intf->p_sys->b_still = 0;
74
75     return( VLC_SUCCESS );
76 }
77
78 /*****************************************************************************
79  * CloseIntf: destroy dummy interface
80  *****************************************************************************/
81 void VCDCloseIntf ( vlc_object_t *p_this )
82 {
83     intf_thread_t *p_intf = (intf_thread_t *)p_this;
84     var_DelCallback( p_intf->p_libvlc, "key-pressed", KeyEvent, p_intf );
85
86     /* Destroy structure */
87     free( p_intf->p_sys );
88 }
89
90
91 /*****************************************************************************
92  * RunIntf: main loop
93  *****************************************************************************/
94 static void
95 RunIntf( intf_thread_t *p_intf )
96 {
97     vlc_object_t      * p_vout = NULL;
98     mtime_t             mtime = 0;
99     mtime_t             mlast = 0;
100     vcdplayer_t       * p_vcdplayer;
101     input_thread_t    * p_input;
102     access_t          * p_access;
103
104     /* What you add to the last input number entry. It accumulates all of
105        the 10_ADD keypresses */
106     int number_addend = 0;
107
108     if( InitThread( p_intf ) < 0 )
109     {
110         msg_Err( p_intf, "can't initialize intf" );
111         return;
112     }
113
114     p_input = p_intf->p_sys->p_input;
115
116     while ( !p_intf->p_sys->p_vcdplayer )
117     {
118         msleep( INTF_IDLE_SLEEP );
119     }
120  
121     p_vcdplayer = p_intf->p_sys->p_vcdplayer;
122     p_access    = p_vcdplayer->p_access;
123
124     dbg_print( INPUT_DBG_CALL, "intf initialized" );
125
126     /* Main loop */
127     while( !p_intf->b_die )
128     {
129       vlc_mutex_lock( &p_intf->change_lock );
130
131         /*
132          * Have we timed-out in showing a still frame?
133          */
134         if( p_intf->p_sys->b_still && !p_intf->p_sys->b_infinite_still )
135         {
136             if( p_intf->p_sys->m_still_time > 0 )
137             {
138                 /* Update remaining still time */
139                 dbg_print(INPUT_DBG_STILL, "updating still time");
140                 mtime = mdate();
141                 if( mlast )
142                 {
143                     p_intf->p_sys->m_still_time -= mtime - mlast;
144                 }
145
146                 mlast = mtime;
147             }
148             else
149             {
150                 /* Still time has elapsed; set to continue playing. */
151                 dbg_print(INPUT_DBG_STILL, "wait time done - setting play");
152                 var_SetInteger( p_intf->p_sys->p_input, "state", PLAYING_S );
153                 p_intf->p_sys->m_still_time = 0;
154                 p_intf->p_sys->b_still = 0;
155                 mlast = 0;
156             }
157         }
158
159       /*
160        * Do we have a keyboard event?
161        */
162       if( p_vout && p_intf->p_sys->b_key_pressed )
163         {
164           vlc_value_t val;
165           int i, i_action = -1;
166           struct hotkey *p_hotkeys = p_intf->p_libvlc->p_hotkeys;
167
168           p_intf->p_sys->b_key_pressed = VLC_FALSE;
169
170           /* Find action triggered by hotkey (if any) */
171           var_Get( p_intf->p_libvlc, "key-pressed", &val );
172
173           dbg_print( INPUT_DBG_EVENT, "Key pressed %d", val.i_int );
174
175           for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
176             {
177               if( p_hotkeys[i].i_key == val.i_int )
178                 {
179                   i_action = p_hotkeys[i].i_action;
180                 }
181             }
182
183           if( i_action != -1) {
184             switch (i_action) {
185
186             case ACTIONID_NAV_LEFT:
187               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_LEFT - prev (%d)",
188                          number_addend );
189               do {
190                 vcdplayer_play_prev( p_access );
191               }        while (number_addend-- > 0);
192               break;
193
194             case ACTIONID_NAV_RIGHT:
195               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_RIGHT - next (%d)",
196                          number_addend );
197               do {
198                 vcdplayer_play_next( p_access );
199               } while (number_addend-- > 0);
200               break;
201
202             case ACTIONID_NAV_UP:
203               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_UP - return" );
204               do {
205                 vcdplayer_play_return( p_access );
206               } while (number_addend-- > 0);
207               break;
208
209             case ACTIONID_NAV_DOWN:
210               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_DOWN - default"  );
211               vcdplayer_play_default( p_access );
212               break;
213
214             case ACTIONID_NAV_ACTIVATE:
215               {
216                 vcdinfo_itemid_t itemid;
217                 itemid.type=p_vcdplayer->play_item.type;
218
219                 dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_ACTIVATE" );
220
221                 if ( vcdplayer_pbc_is_on( p_vcdplayer )
222              && number_addend != 0 ) {
223                   lid_t next_num=vcdinfo_selection_get_lid(p_vcdplayer->vcd,
224                                                            p_vcdplayer->i_lid,
225                                                            number_addend);
226                   if (VCDINFO_INVALID_LID != next_num) {
227                     itemid.num  = next_num;
228                     itemid.type = VCDINFO_ITEM_TYPE_LID;
229                     vcdplayer_play( p_access, itemid );
230                   }
231                 } else {
232                   itemid.num = number_addend;
233                   vcdplayer_play( p_access, itemid );
234                 }
235                 break;
236               }
237             }
238             number_addend = 0;
239
240             /* Any keypress gets rid of still frame waiting.
241                FIXME - should handle just the ones that cause an action.
242             */
243             if( p_intf->p_sys->b_still )
244               {
245                 dbg_print(INPUT_DBG_STILL, "Playing still after activate");
246                 var_SetInteger( p_intf->p_sys->p_input, "state", PLAYING_S );
247                 p_intf->p_sys->b_still = 0;
248                 p_intf->p_sys->b_infinite_still = 0;
249                 p_intf->p_sys->m_still_time = 0;
250               }
251
252           } else {
253             unsigned int digit_entered=0;
254
255             switch (val.i_int) {
256             case '9':
257               digit_entered++;
258             case '8':
259               digit_entered++;
260             case '7':
261               digit_entered++;
262             case '6':
263               digit_entered++;
264             case '5':
265               digit_entered++;
266             case '4':
267               digit_entered++;
268             case '3':
269               digit_entered++;
270             case '2':
271               digit_entered++;
272             case '1':
273               digit_entered++;
274             case '0':
275               {
276                 number_addend *= 10;
277                 number_addend += digit_entered;
278                 dbg_print( INPUT_DBG_EVENT,
279                            "Added %d. Number is now: %d\n",
280                            digit_entered, number_addend);
281                 break;
282               }
283             }
284           }
285         }
286
287
288       vlc_mutex_unlock( &p_intf->change_lock );
289
290       if( p_vout == NULL )
291         {
292           p_vout = vlc_object_find( p_intf->p_sys->p_input,
293                                     VLC_OBJECT_VOUT, FIND_CHILD );
294           if( p_vout )
295             {
296               var_AddCallback( p_vout, "key-pressed", KeyEvent, p_intf );
297             }
298         }
299
300
301       /* Wait a bit */
302       msleep( INTF_IDLE_SLEEP );
303     }
304
305     if( p_vout )
306     {
307         var_DelCallback( p_vout, "key-pressed", KeyEvent, p_intf );
308         vlc_object_release( p_vout );
309     }
310
311     vlc_object_release( p_intf->p_sys->p_input );
312 }
313
314 /*****************************************************************************
315  * InitThread:
316  *****************************************************************************/
317 static int InitThread( intf_thread_t * p_intf )
318 {
319     /* We might need some locking here */
320     if( !p_intf->b_die )
321     {
322         input_thread_t * p_input;
323
324         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
325
326         /* Maybe the input just died */
327         if( p_input == NULL )
328         {
329             return VLC_EGENERIC;
330         }
331
332         vlc_mutex_lock( &p_intf->change_lock );
333
334         p_intf->p_sys->p_input     = p_input;
335         p_intf->p_sys->p_vcdplayer = NULL;
336
337         p_intf->p_sys->b_move  = VLC_FALSE;
338         p_intf->p_sys->b_click = VLC_FALSE;
339         p_intf->p_sys->b_key_pressed = VLC_FALSE;
340
341         vlc_mutex_unlock( &p_intf->change_lock );
342
343         return VLC_SUCCESS;
344     }
345     else
346     {
347         return VLC_EGENERIC;
348     }
349 }
350
351 /*****************************************************************************
352  * KeyEvent: callback for keyboard events
353  *****************************************************************************/
354 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
355                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
356 {
357     intf_thread_t *p_intf = (intf_thread_t *)p_data;
358     vlc_mutex_lock( &p_intf->change_lock );
359
360     p_intf->p_sys->b_key_pressed = VLC_TRUE;
361
362     vlc_mutex_unlock( &p_intf->change_lock );
363
364     return VLC_SUCCESS;
365 }
366
367 /*****************************************************************************
368  * vcdIntfStillTime: function provided to demux plugin to request
369  * still images
370  *****************************************************************************/
371 int vcdIntfStillTime( intf_thread_t *p_intf, uint8_t i_sec )
372 {
373     vlc_mutex_lock( &p_intf->change_lock );
374
375     p_intf->p_sys->b_still = 1;
376     if( 255 == i_sec )
377     {
378         p_intf->p_sys->b_infinite_still = VLC_TRUE;
379     }
380     else
381     {
382         p_intf->p_sys->m_still_time = MILLISECONDS_PER_SEC * i_sec;
383     }
384     vlc_mutex_unlock( &p_intf->change_lock );
385
386     return VLC_SUCCESS;
387 }
388
389 /*****************************************************************************
390  * vcdIntfStillTime: function provided to reset still image
391  *****************************************************************************/
392 int vcdIntfResetStillTime( intf_thread_t *p_intf )
393 {
394     vlc_mutex_lock( &p_intf->change_lock );
395     p_intf->p_sys->m_still_time = 0;
396     var_SetInteger( p_intf->p_sys->p_input, "state", PLAYING_S );
397     vlc_mutex_unlock( &p_intf->change_lock );
398
399     return VLC_SUCCESS;
400 }