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