]> git.sesse.net Git - vlc/blob - modules/control/showintf.c
showintf: fix a variable type (bool instead of int) and do some cleanup.
[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  MouseEvent( vlc_object_t *, char const *,
61                         vlc_value_t, vlc_value_t, void * );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 #define THRESHOLD_TEXT N_( "Threshold" )
67 #define THRESHOLD_LONGTEXT N_( "Height of the zone triggering the interface." )
68
69 vlc_module_begin ()
70     set_shortname( "Showintf" )
71     add_integer( "showintf-threshold", 10, NULL, THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
72     set_description( N_("Show interface with mouse") )
73
74     set_capability( "interface", 0 )
75     set_callbacks( Open, Close )
76 vlc_module_end ()
77
78 /*****************************************************************************
79  * Open: initialize interface
80  *****************************************************************************/
81 int Open( vlc_object_t *p_this )
82 {
83     intf_thread_t *p_intf = (intf_thread_t *)p_this;
84
85     /* Allocate instance and initialize some members */
86     intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
87     if( p_sys == NULL )
88         return VLC_ENOMEM;
89
90     vlc_mutex_init( &p_sys->lock );
91     p_sys->p_vout = NULL;
92     p_sys->b_button_pressed = false;
93     p_sys->b_triggered = false;
94     p_sys->i_threshold = config_GetInt( p_intf, "showintf-threshold" );
95
96     p_intf->pf_run = RunIntf;
97
98     return VLC_SUCCESS;
99 }
100
101 /*****************************************************************************
102  * Close: destroy interface
103  *****************************************************************************/
104 void Close( vlc_object_t *p_this )
105 {
106     intf_thread_t *p_intf = (intf_thread_t *)p_this;
107
108     /* Destroy structure */
109     vlc_mutex_destroy( &p_intf->p_sys->lock );
110     free( p_intf->p_sys );
111 }
112
113
114 /*****************************************************************************
115  * RunIntf: main loop
116  *****************************************************************************/
117 static void RunIntf( intf_thread_t *p_intf )
118 {
119     int canc = vlc_savecancel( );
120
121     /* Main loop */
122     while( vlc_object_alive( p_intf ) )
123     {
124         vlc_mutex_lock( &p_intf->p_sys->lock );
125
126         /* Notify the interfaces */
127         if( p_intf->p_sys->b_triggered )
128         {
129             var_SetBool( p_intf->p_libvlc, "intf-show", true );
130             p_intf->p_sys->b_triggered = false;
131         }
132
133         vlc_mutex_unlock( &p_intf->p_sys->lock );
134
135
136         /* Take care of the video output */
137         if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
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     vlc_restorecancel( canc );
173 }
174
175 /*****************************************************************************
176  * MouseEvent: callback for mouse events
177  *****************************************************************************/
178 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
179                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
180 {
181     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(newval);
182
183     int i_mouse_x, i_mouse_y;
184     intf_thread_t *p_intf = (intf_thread_t *)p_data;
185
186     /* Do nothing when the interface is already requested */
187     if( p_intf->p_sys->b_triggered )
188         return VLC_SUCCESS;
189
190     /* Nothing to do when not in fullscreen mode */
191     if( !var_GetBool( p_intf->p_sys->p_vout, "fullscreen" ) )
192         return VLC_SUCCESS;
193
194     vlc_mutex_lock( &p_intf->p_sys->lock );
195     if( !strcmp( psz_var, "mouse-moved" ) && !p_intf->p_sys->b_button_pressed )
196     {
197         i_mouse_x = var_GetInteger( p_intf->p_sys->p_vout, "mouse-x" );
198         i_mouse_y = var_GetInteger( p_intf->p_sys->p_vout, "mouse-y" );
199
200         /* Very basic test, we even ignore the x value :) */
201         if ( i_mouse_y < p_intf->p_sys->i_threshold )
202         {
203             msg_Dbg( p_intf, "interface showing requested" );
204             p_intf->p_sys->b_triggered = true;
205         }
206     }
207
208     /* We keep track of the button state to avoid interferences with the
209      * gestures plugin */
210     if( !p_intf->p_sys->b_button_pressed &&
211         !strcmp( psz_var, "mouse-button-down" ) )
212     {
213         p_intf->p_sys->b_button_pressed = true;
214     }
215     if( p_intf->p_sys->b_button_pressed &&
216         !strcmp( psz_var, "mouse-button-down" ) )
217     {
218         p_intf->p_sys->b_button_pressed = false;
219     }
220
221     vlc_mutex_unlock( &p_intf->p_sys->lock );
222
223     return VLC_SUCCESS;
224 }