]> git.sesse.net Git - vlc/blob - modules/control/showintf.c
c93fa3f567c04b02e1057baabb8247d85ad27c4c
[vlc] / 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
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_vout.h>
36 #include <vlc_playlist.h>
37
38 #ifdef HAVE_UNISTD_H
39 #    include <unistd.h>
40 #endif
41
42 /*****************************************************************************
43  * intf_sys_t: description and status of interface
44  *****************************************************************************/
45 struct intf_sys_t
46 {
47     vlc_mutex_t lock;
48     vlc_object_t * p_vout;
49     bool     b_button_pressed;
50     bool     b_triggered;
51     int            i_threshold;
52 };
53
54 /*****************************************************************************
55  * Local prototypes.
56  *****************************************************************************/
57 int  Open ( vlc_object_t * );
58 void Close( vlc_object_t * );
59 static void RunIntf( intf_thread_t *p_intf );
60 static int  InitThread( intf_thread_t *p_intf );
61 static int  MouseEvent( vlc_object_t *, char const *,
62                         vlc_value_t, vlc_value_t, void * );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 #define THRESHOLD_TEXT N_( "Threshold" )
68 #define THRESHOLD_LONGTEXT N_( "Height of the zone triggering the interface." )
69
70 vlc_module_begin ()
71     set_shortname( "Showintf" )
72     add_integer( "showintf-threshold", 10, NULL, THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
73     set_description( N_("Show interface with mouse") )
74
75     set_capability( "interface", 0 )
76     set_callbacks( Open, Close )
77 vlc_module_end ()
78
79 /*****************************************************************************
80  * Open: initialize interface
81  *****************************************************************************/
82 int Open( vlc_object_t *p_this )
83 {
84     intf_thread_t *p_intf = (intf_thread_t *)p_this;
85
86     /* Allocate instance and initialize some members */
87     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
88     if( p_intf->p_sys == NULL )
89     {
90         return( 1 );
91     };
92
93     vlc_mutex_init( &p_intf->p_sys->lock );
94     p_intf->pf_run = RunIntf;
95
96     return( 0 );
97 }
98
99 /*****************************************************************************
100  * Close: destroy interface
101  *****************************************************************************/
102 void Close( vlc_object_t *p_this )
103 {
104     intf_thread_t *p_intf = (intf_thread_t *)p_this;
105
106     /* Destroy structure */
107     vlc_mutex_destroy( &p_intf->p_sys->lock );
108     free( p_intf->p_sys );
109 }
110
111
112 /*****************************************************************************
113  * RunIntf: main loop
114  *****************************************************************************/
115 static void RunIntf( intf_thread_t *p_intf )
116 {
117     int canc = vlc_savecancel( );
118     p_intf->p_sys->p_vout = NULL;
119
120     if( InitThread( p_intf ) < 0 )
121     {
122         msg_Err( p_intf, "cannot initialize interface" );
123         return;
124     }
125
126     /* Main loop */
127     while( vlc_object_alive( p_intf ) )
128     {
129         vlc_mutex_lock( &p_intf->p_sys->lock );
130
131         /* Notify the interfaces */
132         if( p_intf->p_sys->b_triggered )
133         {
134             var_SetBool( p_intf->p_libvlc, "intf-show", true );
135             p_intf->p_sys->b_triggered = false;
136         }
137
138         vlc_mutex_unlock( &p_intf->p_sys->lock );
139
140
141         /* Take care of the video output */
142         if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
143         {
144             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
145                              MouseEvent, p_intf );
146             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
147                              MouseEvent, p_intf );
148             vlc_object_release( p_intf->p_sys->p_vout );
149             p_intf->p_sys->p_vout = NULL;
150         }
151
152         if( p_intf->p_sys->p_vout == NULL )
153         {
154             p_intf->p_sys->p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
155                                                      FIND_ANYWHERE );
156             if( p_intf->p_sys->p_vout )
157             {
158                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
159                                  MouseEvent, p_intf );
160                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
161                                  MouseEvent, p_intf );
162             }
163         }
164
165         /* Wait a bit */
166         msleep( INTF_IDLE_SLEEP );
167     }
168
169     if( p_intf->p_sys->p_vout )
170     {
171         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
172                          MouseEvent, p_intf );
173         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
174                          MouseEvent, p_intf );
175         vlc_object_release( p_intf->p_sys->p_vout );
176     }
177     vlc_restorecancel( canc );
178 }
179
180 /*****************************************************************************
181  * InitThread:
182  *****************************************************************************/
183 static int InitThread( intf_thread_t * p_intf )
184 {
185     if( vlc_object_alive( p_intf ) )
186     {
187         vlc_mutex_lock( &p_intf->p_sys->lock );
188
189         p_intf->p_sys->b_triggered = false;
190         p_intf->p_sys->b_button_pressed = false;
191         p_intf->p_sys->i_threshold =
192             config_GetInt( p_intf, "showintf-threshold" );
193
194         vlc_mutex_unlock( &p_intf->p_sys->lock );
195
196         return 0;
197     }
198     else
199     {
200         return -1;
201     }
202 }
203
204 /*****************************************************************************
205  * MouseEvent: callback for mouse events
206  *****************************************************************************/
207 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
208                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
209 {
210     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(newval);
211     vlc_value_t val;
212
213     int i_mouse_x, i_mouse_y;
214     intf_thread_t *p_intf = (intf_thread_t *)p_data;
215
216     /* Do nothing when the interface is already requested */
217     if( p_intf->p_sys->b_triggered )
218         return VLC_SUCCESS;
219
220     /* Nothing to do when not in fullscreen mode */
221     var_Get( p_intf->p_sys->p_vout, "fullscreen", &val );
222     if( !val.i_int )
223         return VLC_SUCCESS;
224
225     vlc_mutex_lock( &p_intf->p_sys->lock );
226     if( !strcmp( psz_var, "mouse-moved" ) && !p_intf->p_sys->b_button_pressed )
227     {
228         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
229         i_mouse_x = val.i_int;
230         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
231         i_mouse_y = val.i_int;
232
233         /* Very basic test, we even ignore the x value :) */
234         if ( i_mouse_y < p_intf->p_sys->i_threshold )
235         {
236             msg_Dbg( p_intf, "interface showing requested" );
237             p_intf->p_sys->b_triggered = true;
238         }
239     }
240
241     /* We keep track of the button state to avoid interferences with the
242      * gestures plugin */
243     if( !p_intf->p_sys->b_button_pressed &&
244         !strcmp( psz_var, "mouse-button-down" ) )
245     {
246         p_intf->p_sys->b_button_pressed = true;
247     }
248     if( p_intf->p_sys->b_button_pressed &&
249         !strcmp( psz_var, "mouse-button-down" ) )
250     {
251         p_intf->p_sys->b_button_pressed = false;
252     }
253
254     vlc_mutex_unlock( &p_intf->p_sys->lock );
255
256     return VLC_SUCCESS;
257 }