]> git.sesse.net Git - vlc/blob - modules/access/dvdplay/intf.c
* ALL: improved hotkeys support.
[vlc] / modules / access / dvdplay / intf.c
1 /*****************************************************************************
2  * intf.c: interface for DVD video manager
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: intf.c,v 1.9 2003/10/29 01:33:27 gbazin Exp $
6  *
7  * Authors: Stéphane Borel <stef@via.ecp.fr>
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 <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <vlc/vlc.h>
32 #include <vlc/intf.h>
33
34 #include "stream_control.h"
35 #include "input_ext-intf.h"
36 #include "input_ext-dec.h"
37
38 #include "vlc_keys.h"
39
40 #include "dvd.h"
41
42 /*****************************************************************************
43  * intf_sys_t: description and status of interface
44  *****************************************************************************/
45 struct intf_sys_t
46 {
47     input_thread_t *    p_input;
48     dvd_data_t *        p_dvd;
49
50     vlc_bool_t          b_still;
51     vlc_bool_t          b_inf_still;
52     mtime_t             m_still_time;
53
54     dvdplay_ctrl_t      control;
55     vlc_bool_t          b_click, b_move, b_key_pressed;
56 };
57
58 /*****************************************************************************
59  * Local prototypes.
60  *****************************************************************************/
61 static int  InitThread     ( intf_thread_t *p_intf );
62 static int  MouseEvent     ( vlc_object_t *, char const *,
63                              vlc_value_t, vlc_value_t, void * );
64 static int  KeyEvent       ( vlc_object_t *, char const *,
65                              vlc_value_t, vlc_value_t, void * );
66
67 /* Exported functions */
68 static void RunIntf        ( intf_thread_t *p_intf );
69
70 /*****************************************************************************
71  * OpenIntf: initialize dummy interface
72  *****************************************************************************/
73 int E_(OpenIntf) ( vlc_object_t *p_this )
74 {
75     intf_thread_t *p_intf = (intf_thread_t *)p_this;
76
77     /* Allocate instance and initialize some members */
78     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
79     if( p_intf->p_sys == NULL )
80     {
81         return( 1 );
82     };
83
84     p_intf->pf_run = RunIntf;
85     
86     var_AddCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
87     p_intf->p_sys->m_still_time = 0;
88     p_intf->p_sys->b_inf_still = 0;
89     p_intf->p_sys->b_still = 0;
90
91     return( 0 );
92 }
93
94 /*****************************************************************************
95  * CloseIntf: destroy dummy interface
96  *****************************************************************************/
97 void E_(CloseIntf) ( vlc_object_t *p_this )
98 {
99     intf_thread_t *p_intf = (intf_thread_t *)p_this;
100     var_DelCallback( p_intf->p_vlc, "key-pressed", KeyEvent, p_intf );
101     /* Destroy structure */
102     free( p_intf->p_sys );
103 }
104
105
106 /*****************************************************************************
107  * RunIntf: main loop
108  *****************************************************************************/
109 static void RunIntf( intf_thread_t *p_intf )
110 {
111     vlc_object_t *      p_vout = NULL;
112     mtime_t             mtime = 0;
113     mtime_t             mlast = 0;
114
115     if( InitThread( p_intf ) < 0 )
116     {
117         msg_Err( p_intf, "can't initialize intf" );
118         return;
119     }
120     msg_Dbg( p_intf, "intf initialized" );
121
122     /* Main loop */
123     while( !p_intf->b_die )
124     {
125         vlc_mutex_lock( &p_intf->change_lock );
126
127         /*
128          * still images
129          */
130 #if 1
131         if( p_intf->p_sys->b_still && !p_intf->p_sys->b_inf_still )
132         {
133             if( p_intf->p_sys->m_still_time > 0 )
134             {
135                 /* update remaining still time */
136                 mtime = mdate();
137                 if( mlast )
138                 {
139                     p_intf->p_sys->m_still_time -= mtime - mlast;
140                 }
141
142                 mlast = mtime;
143             }
144             else
145             {
146                 /* still time elasped */
147                 input_SetStatus( p_intf->p_sys->p_input,
148                                  INPUT_STATUS_PLAY );
149                 p_intf->p_sys->m_still_time = 0;
150                 p_intf->p_sys->b_still = 0;
151                 mlast = 0;
152             }
153         }
154 #else
155         if( p_intf->p_sys->m_still_time != (mtime_t)(-1) )
156         {
157             if( p_intf->p_sys->m_still_time )
158             {
159                 mtime = mdate();
160                 if( mlast )
161                 {
162                     p_intf->p_sys->m_still_time -= mtime - mlast;
163                 }
164                 if( !p_intf->p_sys->m_still_time )
165                 {
166                     input_SetStatus( p_intf->p_sys->p_input,
167                                      INPUT_STATUS_PLAY );
168                 }
169                 mlast = mtime;
170             }
171
172         }
173 #endif
174
175         /* 
176          * mouse cursor
177          */
178         if( p_vout && ( p_intf->p_sys->b_click || p_intf->p_sys->b_move ) )
179         {
180             vlc_value_t val;
181             int i_activate;
182
183             var_Get( p_vout, "mouse-x", &val );
184             p_intf->p_sys->control.mouse.i_x = val.i_int;
185             var_Get( p_vout, "mouse-y", &val );
186             p_intf->p_sys->control.mouse.i_y = val.i_int;
187
188             if( p_intf->p_sys->b_click )
189             {
190                 p_intf->p_sys->control.type = DVDCtrlMouseActivate;
191                 p_intf->p_sys->b_click = VLC_FALSE;
192             }
193             else
194             {
195                 p_intf->p_sys->control.type = DVDCtrlMouseSelect;
196                 p_intf->p_sys->b_move = VLC_FALSE;
197             }
198
199             /* we can safely interact with libdvdplay
200              * with the stream lock */
201             vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
202
203             i_activate = dvdplay_button( p_intf->p_sys->p_dvd->vmg,
204                                          &p_intf->p_sys->control );
205
206             vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
207
208             if( i_activate && p_intf->p_sys->b_still )
209             {
210                 input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
211                 p_intf->p_sys->b_still = 0;
212                 p_intf->p_sys->b_inf_still = 0;
213                 p_intf->p_sys->m_still_time = 0;
214             }
215         }
216
217         /*
218          * keyboard event
219          */
220         if( p_intf->p_sys->b_key_pressed )
221         {
222             vlc_value_t val;
223             int i, i_activate, i_action = -1;
224             struct hotkey *p_hotkeys = p_intf->p_vlc->p_hotkeys;
225
226             p_intf->p_sys->b_key_pressed = VLC_FALSE;
227             
228             /* Find action triggered by hotkey (if any) */
229             var_Get( p_intf->p_vlc, "key-pressed", &val );
230             for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
231             {
232                 if( p_hotkeys[i].i_key == val.i_int )
233                 {
234                     i_action = p_hotkeys[i].i_action;
235                 }
236             }
237
238             if( i_action )
239             {
240                 if( i_action == ACTIONID_NAV_LEFT )
241                 {
242                     p_intf->p_sys->control.type = DVDCtrlLeftButtonSelect;
243                 }
244                 else if( i_action == ACTIONID_NAV_RIGHT )
245                 {
246                     p_intf->p_sys->control.type = DVDCtrlRightButtonSelect;
247                 }
248                 else if( i_action == ACTIONID_NAV_UP )
249                 {
250                     p_intf->p_sys->control.type = DVDCtrlUpperButtonSelect;
251                 }
252                 else if( i_action == ACTIONID_NAV_DOWN )
253                 {
254                     p_intf->p_sys->control.type = DVDCtrlLowerButtonSelect;
255                 }
256                 else if( i_action == ACTIONID_NAV_ACTIVATE )
257                 {
258                     p_intf->p_sys->control.type = DVDCtrlButtonActivate;
259                 }
260                 /* we can safely interact with libdvdplay
261                  * with the stream lock */
262                 vlc_mutex_lock( &p_intf->p_sys->p_input->stream.stream_lock );
263                 
264                 i_activate = dvdplay_button( p_intf->p_sys->p_dvd->vmg,
265                                              &p_intf->p_sys->control );
266                 
267                 vlc_mutex_unlock( &p_intf->p_sys->p_input->stream.stream_lock );
268                 
269                 if( i_activate && p_intf->p_sys->b_still )
270                 {
271                     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
272                     p_intf->p_sys->b_still = 0;
273                     p_intf->p_sys->b_inf_still = 0;
274                     p_intf->p_sys->m_still_time = 0;
275                 }
276             }
277         }
278                 
279
280         vlc_mutex_unlock( &p_intf->change_lock );
281
282         /* 
283          * video output
284          */
285         if( p_vout && p_vout->b_die )
286         {
287             var_DelCallback( p_vout, "mouse-moved", MouseEvent, p_intf );
288             var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
289             vlc_object_release( p_vout );
290             p_vout = NULL;
291         }
292
293         if( p_vout == NULL )
294         {
295             p_vout = vlc_object_find( p_intf->p_sys->p_input,
296                                       VLC_OBJECT_VOUT, FIND_CHILD );
297             if( p_vout )
298             {
299                 var_AddCallback( p_vout, "mouse-moved", MouseEvent, p_intf );
300                 var_AddCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
301             }
302         }
303
304         /* Wait a bit */
305         msleep( INTF_IDLE_SLEEP );
306     }
307
308     if( p_vout )
309     {
310         var_DelCallback( p_vout, "mouse-moved", MouseEvent, p_intf );
311         var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
312         vlc_object_release( p_vout );
313     }
314
315     vlc_object_release( p_intf->p_sys->p_input );
316 }
317
318 /*****************************************************************************
319  * InitThread:
320  *****************************************************************************/
321 static int InitThread( intf_thread_t * p_intf )
322 {
323     /* We might need some locking here */
324     if( !p_intf->b_die )
325     {
326         input_thread_t * p_input;
327         dvd_data_t * p_dvd;
328
329         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
330
331         /* Maybe the input just died */
332         if( p_input == NULL )
333         {
334             return VLC_EGENERIC;
335         }
336
337         p_dvd = (dvd_data_t*)p_input->p_access_data;
338         p_dvd->p_intf = p_intf;
339
340         vlc_mutex_lock( &p_intf->change_lock );
341
342         p_intf->p_sys->p_input = p_input;
343         p_intf->p_sys->p_dvd = p_dvd;
344
345         p_intf->p_sys->b_move = VLC_FALSE;
346         p_intf->p_sys->b_click = VLC_FALSE;
347         p_intf->p_sys->b_key_pressed = VLC_FALSE;
348
349         vlc_mutex_unlock( &p_intf->change_lock );
350
351         return VLC_SUCCESS;
352     }
353     else
354     {
355         return VLC_EGENERIC;
356     }
357 }
358
359 /*****************************************************************************
360  * MouseEvent: callback for mouse events
361  *****************************************************************************/
362 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
363                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
364 {
365     intf_thread_t *p_intf = (intf_thread_t *)p_data;
366
367     vlc_mutex_lock( &p_intf->change_lock );
368
369     if( psz_var[6] == 'c' ) /* "mouse-clicked" */
370     {
371         p_intf->p_sys->b_click = VLC_TRUE;
372     }
373     else if( psz_var[6] == 'm' ) /* "mouse-moved" */
374     {
375         p_intf->p_sys->b_move = VLC_TRUE;
376     }
377
378     vlc_mutex_unlock( &p_intf->change_lock );
379
380     return VLC_SUCCESS;
381 }
382
383 /*****************************************************************************
384  * KeyEvent: callback for keyboard events
385  *****************************************************************************/
386 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
387                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
388 {
389     intf_thread_t *p_intf = (intf_thread_t *)p_data;
390     vlc_mutex_lock( &p_intf->change_lock );
391
392     p_intf->p_sys->b_key_pressed = VLC_TRUE;
393     
394     vlc_mutex_unlock( &p_intf->change_lock );
395
396     return VLC_SUCCESS;
397 }
398
399 /*****************************************************************************
400  * dvdIntfStillTime: function provided to demux plugin to request
401  * still images
402  *****************************************************************************/
403 int dvdIntfStillTime( intf_thread_t *p_intf, int i_sec )
404 {
405     vlc_mutex_lock( &p_intf->change_lock );
406 #if 1
407
408     if( i_sec == 0xff )
409     {
410         p_intf->p_sys->b_still = 1;
411         p_intf->p_sys->b_inf_still = 1;
412     }
413     else if( i_sec > 0 )
414     {
415         p_intf->p_sys->b_still = 1;
416         p_intf->p_sys->m_still_time = 1000000 * i_sec;
417     }
418 #else
419     if( i_sec > 0 )
420     {
421         if( i_sec == 0xff )
422         {
423             p_intf->p_sys->m_still_time = (mtime_t)(-1);
424             msg_Warn( p_intf, I64Fd, p_intf->p_sys->m_still_time );
425         }
426         else
427         {
428             p_intf->p_sys->m_still_time = 1000000 * i_sec;
429         }
430     }
431 #endif
432     vlc_mutex_unlock( &p_intf->change_lock );
433
434     return VLC_SUCCESS;
435 }
436
437 /*****************************************************************************
438  * dvdIntfStillTime: function provided to reset still image
439  *****************************************************************************/
440 int dvdIntfResetStillTime( intf_thread_t *p_intf )
441 {
442     vlc_mutex_lock( &p_intf->change_lock );
443     p_intf->p_sys->m_still_time = 0;
444     input_SetStatus( p_intf->p_sys->p_input, INPUT_STATUS_PLAY );
445     vlc_mutex_unlock( &p_intf->change_lock );
446
447     return VLC_SUCCESS;
448 }