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