]> git.sesse.net Git - vlc/blob - modules/control/showintf.c
2f1149b5f4db8361ccfa73a53e1eea326b752500
[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     set_description( N_("Show interface with mouse") )
72
73     set_capability( "interface", 0 )
74     set_callbacks( Open, Close )
75
76     set_category( CAT_INTERFACE )
77     set_subcategory( SUBCAT_INTERFACE_CONTROL )
78
79     add_integer( "showintf-threshold", 10, NULL, THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true )
80 vlc_module_end ()
81
82 /*****************************************************************************
83  * Open: initialize interface
84  *****************************************************************************/
85 int Open( vlc_object_t *p_this )
86 {
87     intf_thread_t *p_intf = (intf_thread_t *)p_this;
88
89     /* Allocate instance and initialize some members */
90     intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
91     if( p_sys == NULL )
92         return VLC_ENOMEM;
93
94     vlc_mutex_init( &p_sys->lock );
95     p_sys->p_vout = NULL;
96     p_sys->b_button_pressed = false;
97     p_sys->b_triggered = false;
98     p_sys->i_threshold = config_GetInt( p_intf, "showintf-threshold" );
99
100     p_intf->pf_run = RunIntf;
101
102     return VLC_SUCCESS;
103 }
104
105 /*****************************************************************************
106  * Close: destroy interface
107  *****************************************************************************/
108 void Close( vlc_object_t *p_this )
109 {
110     intf_thread_t *p_intf = (intf_thread_t *)p_this;
111
112     /* Destroy structure */
113     vlc_mutex_destroy( &p_intf->p_sys->lock );
114     free( p_intf->p_sys );
115 }
116
117
118 /*****************************************************************************
119  * RunIntf: main loop
120  *****************************************************************************/
121 static void RunIntf( intf_thread_t *p_intf )
122 {
123     int canc = vlc_savecancel( );
124
125     /* Main loop */
126     while( vlc_object_alive( p_intf ) )
127     {
128         vlc_mutex_lock( &p_intf->p_sys->lock );
129
130         /* Notify the interfaces */
131         if( p_intf->p_sys->b_triggered )
132         {
133             var_SetBool( p_intf->p_libvlc, "intf-show", true );
134             p_intf->p_sys->b_triggered = false;
135         }
136
137         vlc_mutex_unlock( &p_intf->p_sys->lock );
138
139
140         /* Take care of the video output */
141         if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
142         {
143             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
144                              MouseEvent, p_intf );
145             var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
146                              MouseEvent, p_intf );
147             vlc_object_release( p_intf->p_sys->p_vout );
148             p_intf->p_sys->p_vout = NULL;
149         }
150
151         if( p_intf->p_sys->p_vout == NULL )
152         {
153             p_intf->p_sys->p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT,
154                                                      FIND_ANYWHERE );
155             if( p_intf->p_sys->p_vout )
156             {
157                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-moved",
158                                  MouseEvent, p_intf );
159                 var_AddCallback( p_intf->p_sys->p_vout, "mouse-button-down",
160                                  MouseEvent, p_intf );
161             }
162         }
163
164         /* Wait a bit */
165         msleep( INTF_IDLE_SLEEP );
166     }
167
168     if( p_intf->p_sys->p_vout )
169     {
170         var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
171                          MouseEvent, p_intf );
172         var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
173                          MouseEvent, p_intf );
174         vlc_object_release( p_intf->p_sys->p_vout );
175     }
176     vlc_restorecancel( canc );
177 }
178
179 /*****************************************************************************
180  * MouseEvent: callback for mouse events
181  *****************************************************************************/
182 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
183                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
184 {
185     VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(newval);
186
187     int i_mouse_x, i_mouse_y;
188     intf_thread_t *p_intf = (intf_thread_t *)p_data;
189
190     /* Do nothing when the interface is already requested */
191     if( p_intf->p_sys->b_triggered )
192         return VLC_SUCCESS;
193
194     /* Nothing to do when not in fullscreen mode */
195     if( !var_GetBool( p_intf->p_sys->p_vout, "fullscreen" ) )
196         return VLC_SUCCESS;
197
198     vlc_mutex_lock( &p_intf->p_sys->lock );
199     if( !strcmp( psz_var, "mouse-moved" ) && !p_intf->p_sys->b_button_pressed )
200     {
201         i_mouse_x = var_GetInteger( p_intf->p_sys->p_vout, "mouse-x" );
202         i_mouse_y = var_GetInteger( p_intf->p_sys->p_vout, "mouse-y" );
203
204         /* Very basic test, we even ignore the x value :) */
205         if ( i_mouse_y < p_intf->p_sys->i_threshold )
206         {
207             msg_Dbg( p_intf, "interface showing requested" );
208             p_intf->p_sys->b_triggered = true;
209         }
210     }
211
212     /* We keep track of the button state to avoid interferences with the
213      * gestures plugin */
214     if( !p_intf->p_sys->b_button_pressed &&
215         !strcmp( psz_var, "mouse-button-down" ) )
216     {
217         p_intf->p_sys->b_button_pressed = true;
218     }
219     if( p_intf->p_sys->b_button_pressed &&
220         !strcmp( psz_var, "mouse-button-down" ) )
221     {
222         p_intf->p_sys->b_button_pressed = false;
223     }
224
225     vlc_mutex_unlock( &p_intf->p_sys->lock );
226
227     return VLC_SUCCESS;
228 }