]> git.sesse.net Git - vlc/blob - modules/access/cdda/intf.c
Split out cdda to facilitate later changes.
[vlc] / modules / access / cdda / intf.c
1 /*****************************************************************************
2  * intf.c: Video CD interface to handle user interaction and still time
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: intf.c,v 1.1 2003/11/26 03:35:26 rocky Exp $
6  *
7  * Authors: Rocky Bernstein <rocky@panix.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/intf.h>
29
30 #include "stream_control.h"
31 #include "input_ext-intf.h"
32 #include "input_ext-dec.h"
33 #include "vlc_keys.h"
34
35 #include "cdda.h"
36
37 /*****************************************************************************
38  * intf_sys_t: description and status of interface
39  *****************************************************************************/
40 struct intf_sys_t
41 {
42     input_thread_t    * p_input;
43     cdda_data_t       * p_cdda;
44     vlc_bool_t          b_click, b_move, b_key_pressed;
45 };
46
47 /*****************************************************************************
48  * Local prototypes.
49  *****************************************************************************/
50 static int  InitThread     ( intf_thread_t *p_intf );
51 static int  KeyEvent       ( vlc_object_t *, char const *,
52                              vlc_value_t, vlc_value_t, void * );
53
54 /* Exported functions */
55 static void RunIntf        ( intf_thread_t *p_intf );
56
57 /*****************************************************************************
58  * OpenIntf: initialize dummy interface
59  *****************************************************************************/
60 int E_(OpenIntf) ( vlc_object_t *p_this )
61 {
62     intf_thread_t *p_intf = (intf_thread_t *)p_this;
63
64     /* Allocate instance and initialize some members */
65     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
66     if( p_intf->p_sys == NULL )
67     {
68         return( 1 );
69     };
70
71     p_intf->pf_run = RunIntf;
72
73     var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
74
75     return( 0 );
76 }
77
78 /*****************************************************************************
79  * CloseIntf: destroy dummy interface
80  *****************************************************************************/
81 void E_(CloseIntf) ( vlc_object_t *p_this )
82 {
83     intf_thread_t *p_intf = (intf_thread_t *)p_this;
84
85     /* Destroy structure */
86     free( p_intf->p_sys );
87 }
88
89 /*****************************************************************************
90  * RunIntf: main loop
91  *****************************************************************************/
92 static void RunIntf( intf_thread_t *p_intf )
93 {
94     vlc_object_t      * p_vout = NULL;
95     cdda_data_t       * p_cdda;
96     input_thread_t    * p_input;
97     
98     /* What you add to the last input number entry. It accumulates all of
99        the 10_ADD keypresses */
100     int number_addend = 0; 
101     
102     if( InitThread( p_intf ) < 0 )
103     {
104         msg_Err( p_intf, "can't initialize intf" );
105         return;
106     }
107
108     p_input = p_intf->p_sys->p_input;
109     p_cdda   = p_intf->p_sys->p_cdda = 
110       (cdda_data_t *) p_input->p_access_data;
111
112     dbg_print( INPUT_DBG_CALL, "intf initialized" );
113
114     /* Main loop */
115     while( !p_intf->b_die )
116     {
117       vlc_mutex_lock( &p_intf->change_lock );
118
119       /*
120        * keyboard event
121        */
122       if( p_vout && p_intf->p_sys->b_key_pressed )
123         {
124           vlc_value_t val;
125           int i, i_action = -1;
126           struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
127
128           p_intf->p_sys->b_key_pressed = VLC_FALSE;
129           
130           /* Find action triggered by hotkey (if any) */
131           var_Get( p_intf->p_vlc, "key-pressed", &val );
132
133           dbg_print( INPUT_DBG_EVENT, "Key pressed %d", val.i_int );
134
135           for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
136             {
137               if( p_hotkeys[i].i_key == val.i_int )
138                 {
139                   i_action = p_hotkeys[i].i_action;
140                 }
141             }
142           
143           if( i_action != -1) {
144             switch (i_action) {
145               
146             case ACTIONID_NAV_LEFT: 
147               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_LEFT (%d)", 
148                          number_addend );
149               do {
150                 if ( CDDAPlay( p_input, p_cdda->i_track-1 ) ) {
151                   p_cdda->i_track--;
152                 } else {
153                   break;
154                 }
155               } while (number_addend-- > 0);
156               break;
157
158             case ACTIONID_NAV_RIGHT:
159               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_RIGHT (%d)",
160                          number_addend );
161               do {
162                 if ( CDDAPlay( p_input, p_cdda->i_track+1 ) ) {
163                   p_cdda->i_track++;
164                 } else {
165                   break;
166                 }
167               } while (number_addend-- > 0);
168               break;
169
170             case ACTIONID_NAV_UP:
171               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_UP" );
172               do {
173                 ;
174               } while (number_addend-- > 0);
175               break;
176
177             case ACTIONID_NAV_DOWN:
178               dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_DOWN"  );
179               break;
180
181             case ACTIONID_NAV_ACTIVATE: 
182               {
183                 dbg_print( INPUT_DBG_EVENT, "ACTIONID_NAV_ACTIVATE" );
184                 if ( CDDAPlay( p_input, number_addend ) ) {
185                   p_cdda->i_track = number_addend;
186                 } else {
187                   break;
188                 }
189                 break;
190               }
191             }
192             number_addend = 0;
193           } else {
194             unsigned int digit_entered=0;
195
196             switch (val.i_int) {
197             case '9':
198               digit_entered++;
199             case '8':
200               digit_entered++;
201             case '7':
202               digit_entered++;
203             case '6':
204               digit_entered++;
205             case '5':
206               digit_entered++;
207             case '4':
208               digit_entered++;
209             case '3':
210               digit_entered++;
211             case '2':
212               digit_entered++;
213             case '1':
214               digit_entered++;
215             case '0':
216               {
217                 number_addend *= 10;
218                 number_addend += digit_entered;
219                 dbg_print( INPUT_DBG_EVENT, 
220                            "Added %d. Number is now: %d\n", 
221                            digit_entered, number_addend);
222                 break;
223               }
224             }
225           }
226         }
227
228       
229       vlc_mutex_unlock( &p_intf->change_lock );
230       
231       if( p_vout == NULL )
232         {
233           p_vout = vlc_object_find( p_intf->p_sys->p_input,
234                                     VLC_OBJECT_VOUT, FIND_ANYWHERE );
235           if( p_vout )
236             {
237               var_AddCallback( p_vout, "key-pressed", KeyEvent, p_intf );
238             }
239         }
240       
241       
242       /* Wait a bit */
243       msleep( INTF_IDLE_SLEEP );
244     }
245
246     if( p_vout )
247     {
248         var_DelCallback( p_vout, "key-pressed", KeyEvent, p_intf );
249         vlc_object_release( p_vout );
250     }
251
252     vlc_object_release( p_intf->p_sys->p_input );
253 }
254
255 /*****************************************************************************
256  * InitThread:
257  *****************************************************************************/
258 static int InitThread( intf_thread_t * p_intf )
259 {
260     /* We might need some locking here */
261     if( !p_intf->b_die )
262     {
263         input_thread_t * p_input;
264
265         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
266
267         /* Maybe the input just died */
268         if( p_input == NULL )
269         {
270             return VLC_EGENERIC;
271         }
272
273         vlc_mutex_lock( &p_intf->change_lock );
274
275         p_intf->p_sys->p_input = p_input;
276
277         p_intf->p_sys->b_move = VLC_FALSE;
278         p_intf->p_sys->b_click = VLC_FALSE;
279         p_intf->p_sys->b_key_pressed = VLC_FALSE;
280
281         vlc_mutex_unlock( &p_intf->change_lock );
282
283         return VLC_SUCCESS;
284     }
285     else
286     {
287         return VLC_EGENERIC;
288     }
289 }
290
291 /*****************************************************************************
292  * KeyEvent: callback for keyboard events
293  *****************************************************************************/
294 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
295                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
296 {
297     intf_thread_t *p_intf = (intf_thread_t *)p_data;
298     vlc_mutex_lock( &p_intf->change_lock );
299
300     p_intf->p_sys->b_key_pressed = VLC_TRUE;
301
302     vlc_mutex_unlock( &p_intf->change_lock );
303
304     return VLC_SUCCESS;
305 }