Make it possible to force a distro.mak to use. This is helpfull in cross-compile...
[vlc.git] / modules / control / showintf.c
1 /*****************************************************************************
2  * showintf.c: control the display of the interface in fullscreen mode
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Olivier Teuliere <ipkiss@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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_interface.h>
32 #include <vlc_vout.h>
33 #include <vlc_playlist.h>
34
35 #ifdef HAVE_UNISTD_H
36 #    include <unistd.h>
37 #endif
38
39 /*****************************************************************************
40  * intf_sys_t: description and status of interface
41  *****************************************************************************/
42 struct intf_sys_t
43 {
44     vlc_object_t * p_vout;
45     vlc_bool_t     b_button_pressed;
46     vlc_bool_t     b_triggered;
47     int            i_threshold;
48 };
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 int  E_(Open) ( vlc_object_t * );
54 void E_(Close)( vlc_object_t * );
55 static void RunIntf( intf_thread_t *p_intf );
56 static int  InitThread( intf_thread_t *p_intf );
57 static int  MouseEvent( vlc_object_t *, char const *,
58                         vlc_value_t, vlc_value_t, void * );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 #define THRESHOLD_TEXT N_( "Threshold" )
64 #define THRESHOLD_LONGTEXT N_( "Height of the zone triggering the interface." )
65
66 vlc_module_begin();
67     set_shortname( "Showintf" );
68     add_integer( "showintf-threshold", 10, NULL, THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
69     set_description( _("Show interface with mouse") );
70
71     set_capability( "interface", 0 );
72     set_callbacks( E_(Open), E_(Close) );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * Open: initialize interface
77  *****************************************************************************/
78 int E_(Open)( vlc_object_t *p_this )
79 {
80     intf_thread_t *p_intf = (intf_thread_t *)p_this;
81
82     /* Allocate instance and initialize some members */
83     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
84     if( p_intf->p_sys == NULL )
85     {
86         return( 1 );
87     };
88
89     p_intf->pf_run = RunIntf;
90
91     return( 0 );
92 }
93
94 /*****************************************************************************
95  * Close: destroy interface
96  *****************************************************************************/
97 void E_(Close)( vlc_object_t *p_this )
98 {
99     intf_thread_t *p_intf = (intf_thread_t *)p_this;
100
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     p_intf->p_sys->p_vout = NULL;
112
113     if( InitThread( p_intf ) < 0 )
114     {
115         msg_Err( p_intf, "cannot initialize interface" );
116         return;
117     }
118
119     /* Main loop */
120     while( !intf_ShouldDie( p_intf ) )
121     {
122         vlc_mutex_lock( &p_intf->change_lock );
123
124         /* Notify the interfaces */
125         if( p_intf->p_sys->b_triggered )
126         {
127             playlist_t *p_playlist = pl_Yield( p_intf );
128             var_SetBool( p_playlist, "intf-show", VLC_TRUE );
129             vlc_object_release( p_playlist );
130             p_intf->p_sys->b_triggered = VLC_FALSE;
131         }
132
133         vlc_mutex_unlock( &p_intf->change_lock );
134
135
136         /* Take care of the video output */
137         if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
138         {
139             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
140                              MouseEvent, p_intf );
141             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
142                              MouseEvent, p_intf );
143             vlc_object_release( p_intf->p_sys->p_vout );
144             p_intf->p_sys->p_vout = NULL;
145         }
146
147         if( p_intf->p_sys->p_vout == NULL )
148         {
149             p_intf->p_sys->p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
150                                                      FIND_ANYWHERE );
151             if( p_intf->p_sys->p_vout )
152             {
153                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
154                                  MouseEvent, p_intf );
155                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
156                                  MouseEvent, p_intf );
157             }
158         }
159
160         /* Wait a bit */
161         msleep( INTF_IDLE_SLEEP );
162     }
163
164     if( p_intf->p_sys->p_vout )
165     {
166         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
167                          MouseEvent, p_intf );
168         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
169                          MouseEvent, p_intf );
170         vlc_object_release( p_intf->p_sys->p_vout );
171     }
172 }
173
174 /*****************************************************************************
175  * InitThread:
176  *****************************************************************************/
177 static int InitThread( intf_thread_t * p_intf )
178 {
179     if( !intf_ShouldDie( p_intf ) )
180     {
181         vlc_mutex_lock( &p_intf->change_lock );
182
183         p_intf->p_sys->b_triggered = VLC_FALSE;
184         p_intf->p_sys->b_button_pressed = VLC_FALSE;
185         p_intf->p_sys->i_threshold =
186             config_GetInt( p_intf, "showintf-threshold" );
187
188         vlc_mutex_unlock( &p_intf->change_lock );
189
190         return 0;
191     }
192     else
193     {
194         return -1;
195     }
196 }
197
198 /*****************************************************************************
199  * MouseEvent: callback for mouse events
200  *****************************************************************************/
201 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
202                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
203 {
204     vlc_value_t val;
205
206     int i_mouse_x, i_mouse_y;
207     intf_thread_t *p_intf = (intf_thread_t *)p_data;
208
209     /* Do nothing when the interface is already requested */
210     if( p_intf->p_sys->b_triggered )
211         return VLC_SUCCESS;
212
213     /* Nothing to do when not in fullscreen mode */
214     var_Get( p_intf->p_sys->p_vout, "fullscreen", &val );
215     if( !val.i_int )
216         return VLC_SUCCESS;
217
218     vlc_mutex_lock( &p_intf->change_lock );
219     if( !strcmp( psz_var, "mouse-moved" ) && !p_intf->p_sys->b_button_pressed )
220     {
221         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
222         i_mouse_x = val.i_int;
223         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
224         i_mouse_y = val.i_int;
225
226         /* Very basic test, we even ignore the x value :) */
227         if ( i_mouse_y < p_intf->p_sys->i_threshold )
228         {
229             msg_Dbg( p_intf, "interface showing requested" );
230             p_intf->p_sys->b_triggered = VLC_TRUE;
231         }
232     }
233
234     /* We keep track of the button state to avoid interferences with the
235      * gestures plugin */
236     if( !p_intf->p_sys->b_button_pressed &&
237         !strcmp( psz_var, "mouse-button-down" ) )
238     {
239         p_intf->p_sys->b_button_pressed = VLC_TRUE;
240     }
241     if( p_intf->p_sys->b_button_pressed &&
242         !strcmp( psz_var, "mouse-button-down" ) )
243     {
244         p_intf->p_sys->b_button_pressed = VLC_FALSE;
245     }
246
247     vlc_mutex_unlock( &p_intf->change_lock );
248
249     return VLC_SUCCESS;
250 }