]> git.sesse.net Git - vlc/blob - modules/misc/screensaver.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / misc / screensaver.c
1 /*****************************************************************************
2  * screensaver.c : disable screen savers when VLC is playing
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Benjamin Pracht <bigben AT videolan DOT org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_interface.h>
37 #include <vlc_aout.h>
38 #include <vlc_vout.h>
39
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <unistd.h>
43 #include <signal.h>
44
45 #ifdef HAVE_DBUS
46
47 #include <dbus/dbus.h>
48
49 #define GS_SERVICE   "org.gnome.ScreenSaver"
50 #define GS_PATH      "/org/gnome/ScreenSaver"
51 #define GS_INTERFACE "org.gnome.ScreenSaver"
52
53 #define FDS_SERVICE   "org.freedesktop.ScreenSaver"
54 #define FDS_PATH      "/ScreenSaver"
55 #define FDS_INTERFACE "org.freedesktop.ScreenSaver"
56
57 #endif
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int  Activate     ( vlc_object_t * );
63 static void  Deactivate   ( vlc_object_t * );
64
65 static void Run          ( intf_thread_t *p_intf );
66
67 #ifdef HAVE_DBUS
68
69 static DBusConnection * dbus_init( intf_thread_t *p_intf );
70 static void poke_screensaver( intf_thread_t *p_intf,
71                               DBusConnection *p_connection );
72 static void screensaver_send_message_void ( intf_thread_t *p_intf,
73                                        DBusConnection *p_connection,
74                                        const char *psz_service,
75                                        const char *psz_path,
76                                        const char *psz_interface,
77                                        const char *psz_name );
78 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service );
79
80
81 struct intf_sys_t
82 {
83     DBusConnection *p_connection;
84 };
85
86 #endif
87
88 /*****************************************************************************
89  * Module descriptor
90  *****************************************************************************/
91 vlc_module_begin ()
92     set_description( N_("X Screensaver disabler") )
93     set_capability( "interface", 0 )
94     set_callbacks( Activate, Deactivate )
95 vlc_module_end ()
96
97 /*****************************************************************************
98  * Activate: initialize and create stuff
99  *****************************************************************************/
100 static int Activate( vlc_object_t *p_this )
101 {
102     intf_thread_t *p_intf = (intf_thread_t*)p_this;
103
104     p_intf->pf_run = Run;
105
106 #ifdef HAVE_DBUS
107     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
108     if( !p_intf->p_sys ) return VLC_ENOMEM;
109 #endif
110
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * Deactivate: uninitialize and cleanup
116  *****************************************************************************/
117 static void Deactivate( vlc_object_t *p_this )
118 {
119 #ifdef HAVE_DBUS
120     intf_thread_t *p_intf = (intf_thread_t*)p_this;
121
122     if( p_intf->p_sys->p_connection )
123     {
124         dbus_connection_unref( p_intf->p_sys->p_connection );
125     }
126
127     free( p_intf->p_sys );
128     p_intf->p_sys = NULL;
129 #endif
130 }
131
132 /*****************************************************************************
133  * Execute: Spawns a process using execv()
134  *****************************************************************************/
135 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
136 {
137     pid_t pid = fork();
138     switch( pid )
139     {
140         case 0:     /* we're the child */
141         {
142             sigset_t set;
143             sigemptyset (&set);
144             pthread_sigmask (SIG_SETMASK, &set, NULL);
145
146             /* We don't want output */
147             if( ( freopen( "/dev/null", "w", stdout ) != NULL )
148              && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
149                 execv( ppsz_args[0] , (char *const *)ppsz_args );
150             /* If the file we want to execute doesn't exist we exit() */
151             exit( EXIT_FAILURE );
152         }
153         case -1:    /* we're the error */
154             msg_Dbg( p_this, "Couldn't fork() while launching %s",
155                      ppsz_args[0] );
156             break;
157         default:    /* we're the parent */
158             /* Wait for the child to exit.
159              * We will not deadlock because we ran "/bin/sh &" */
160             while( waitpid( pid, NULL, 0 ) != pid);
161             break;
162     }
163 }
164
165 /*****************************************************************************
166  * Run: main thread
167  *****************************************************************************
168  * This part of the module is in a separate thread so that we do not have
169  * too much system() overhead.
170  *****************************************************************************/
171 static void Run( intf_thread_t *p_intf )
172 {
173     int canc = vlc_savecancel();
174 #ifdef HAVE_DBUS
175     p_intf->p_sys->p_connection = dbus_init( p_intf );
176 #endif
177
178     for( ;; )
179     {
180         vlc_object_t *p_vout;
181
182         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
183
184         /* If there is a video output, disable xscreensaver */
185         if( p_vout )
186         {
187             input_thread_t *p_input;
188             p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
189             vlc_object_release( p_vout );
190             if( p_input )
191             {
192                 if( PLAYING_S == var_GetInteger( p_input, "state" ) )
193                 {
194                     /* http://www.jwz.org/xscreensaver/faq.html#dvd */
195                     const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
196                             "xscreensaver-command -deactivate &", (char*)NULL };
197                     Execute( p_intf, ppsz_xsargs );
198
199                     /* If we have dbus support, let's communicate directly
200                        with gnome-screensave else, run
201                        gnome-screensaver-command */
202 #ifdef HAVE_DBUS
203                     poke_screensaver( p_intf, p_intf->p_sys->p_connection );
204 #else
205                     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
206                             "gnome-screensaver-command --poke &", (char*)NULL };
207                     Execute( p_intf, ppsz_gsargs );
208 #endif
209                     /* FIXME: add support for other screensavers */
210                 }
211                 vlc_object_release( p_input );
212             }
213         }
214
215         vlc_restorecancel( canc );
216         /* Check screensaver every 30 seconds */
217         msleep( 30 * CLOCK_FREQ );
218         canc = vlc_savecancel( );
219     }
220 }
221
222 #ifdef HAVE_DBUS
223
224 static DBusConnection * dbus_init( intf_thread_t *p_intf )
225 {
226     DBusError dbus_error;
227
228     dbus_error_init (&dbus_error);
229     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
230
231     if ( !p_connection )
232     {
233         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
234                           dbus_error.message);
235         dbus_error_free( &dbus_error );
236         return NULL;
237     }
238
239     return p_connection;
240 }
241
242 static void poke_screensaver( intf_thread_t *p_intf,
243                               DBusConnection *p_connection )
244 {
245     if( screensaver_is_running( p_connection, GS_SERVICE ) )
246     {
247 #   ifdef SCREENSAVER_DEBUG
248         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
249 #   endif
250         /* gnome-screensaver changed it's D-Bus interface, so we need both */
251         screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
252                                        GS_INTERFACE, "Poke" );
253         screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
254                                        GS_INTERFACE, "SimulateUserActivity" );
255     }
256     else if( screensaver_is_running( p_connection, FDS_SERVICE ) )
257     {
258 #   ifdef SCREENSAVER_DEBUG
259         msg_Dbg( p_intf, "found a running freedesktop-screensaver instance" );
260 #   endif
261         screensaver_send_message_void( p_intf, p_connection, FDS_SERVICE, FDS_PATH,
262                                        FDS_INTERFACE, "SimulateUserActivity" );
263     }
264 #   ifdef SCREENSAVER_DEBUG
265     else
266     {
267         msg_Dbg( p_intf, "found no running (gnome|freedesktop)-screensaver instance" );
268     }
269 #   endif
270
271 }
272
273 static void screensaver_send_message_void ( intf_thread_t *p_intf,
274                                        DBusConnection *p_connection,
275                                        const char *psz_service,
276                                        const char *psz_path,
277                                        const char *psz_interface,
278                                        const char *psz_name )
279 {
280     DBusMessage *p_message;
281
282     if( !p_connection || !psz_name ) return;
283
284     p_message = dbus_message_new_method_call( psz_service, psz_path,
285                                               psz_interface, psz_name );
286     if( p_message == NULL )
287     {
288         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
289         return;
290     }
291
292     if( !dbus_connection_send( p_connection, p_message, NULL ) )
293     {
294         msg_Err( p_intf, "DBUS communication failed" );
295     }
296
297     dbus_connection_flush( p_connection );
298
299     dbus_message_unref( p_message );
300 }
301
302 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service )
303 {
304     DBusError error;
305     bool b_return;
306
307     if( !p_connection ) return false;
308
309     dbus_error_init( &error );
310     b_return = dbus_bus_name_has_owner( p_connection, psz_service, &error );
311     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
312
313     return b_return;
314 }
315
316 #endif
317