]> git.sesse.net Git - vlc/blob - modules/misc/screensaver.c
Merge branch 1.0-bugfix
[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
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <unistd.h>
41 #include <signal.h>
42
43 #ifdef HAVE_DBUS
44
45 #include <dbus/dbus.h>
46
47 #define GS_SERVICE   "org.gnome.ScreenSaver"
48 #define GS_PATH      "/org/gnome/ScreenSaver"
49 #define GS_INTERFACE "org.gnome.ScreenSaver"
50
51 #define FDS_SERVICE   "org.freedesktop.ScreenSaver"
52 #define FDS_PATH      "/ScreenSaver"
53 #define FDS_INTERFACE "org.freedesktop.ScreenSaver"
54
55 #endif
56
57 /*****************************************************************************
58  * Local prototypes
59  *****************************************************************************/
60 static int  Activate     ( vlc_object_t * );
61 static void  Deactivate   ( vlc_object_t * );
62
63 static void Run          ( intf_thread_t *p_intf );
64
65 #ifdef HAVE_DBUS
66
67 static DBusConnection * dbus_init( intf_thread_t *p_intf );
68 static void poke_screensaver( intf_thread_t *p_intf,
69                               DBusConnection *p_connection );
70 static void screensaver_send_message_void ( intf_thread_t *p_intf,
71                                        DBusConnection *p_connection,
72                                        const char *psz_service,
73                                        const char *psz_path,
74                                        const char *psz_interface,
75                                        const char *psz_name );
76 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service );
77
78
79 struct intf_sys_t
80 {
81     DBusConnection *p_connection;
82 };
83
84 #endif
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 vlc_module_begin ()
90     set_description( N_("X Screensaver disabler") )
91     set_capability( "interface", 0 )
92     set_callbacks( Activate, Deactivate )
93 vlc_module_end ()
94
95 /*****************************************************************************
96  * Activate: initialize and create stuff
97  *****************************************************************************/
98 static int Activate( vlc_object_t *p_this )
99 {
100     intf_thread_t *p_intf = (intf_thread_t*)p_this;
101
102     p_intf->pf_run = Run;
103
104 #ifdef HAVE_DBUS
105     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
106     if( !p_intf->p_sys ) return VLC_ENOMEM;
107 #endif
108
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * Deactivate: uninitialize and cleanup
114  *****************************************************************************/
115 static void Deactivate( vlc_object_t *p_this )
116 {
117 #ifdef HAVE_DBUS
118     intf_thread_t *p_intf = (intf_thread_t*)p_this;
119
120     if( p_intf->p_sys->p_connection )
121     {
122         dbus_connection_unref( p_intf->p_sys->p_connection );
123     }
124
125     free( p_intf->p_sys );
126     p_intf->p_sys = NULL;
127 #endif
128 }
129
130 /*****************************************************************************
131  * Execute: Spawns a process using execv()
132  *****************************************************************************/
133 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
134 {
135     pid_t pid = fork();
136     switch( pid )
137     {
138         case 0:     /* we're the child */
139         {
140             sigset_t set;
141             sigemptyset (&set);
142             pthread_sigmask (SIG_SETMASK, &set, NULL);
143
144             /* We don't want output */
145             if( ( freopen( "/dev/null", "w", stdout ) != NULL )
146              && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
147                 execv( ppsz_args[0] , (char *const *)ppsz_args );
148             /* If the file we want to execute doesn't exist we exit() */
149             exit( EXIT_FAILURE );
150         }
151         case -1:    /* we're the error */
152             msg_Dbg( p_this, "Couldn't fork() while launching %s",
153                      ppsz_args[0] );
154             break;
155         default:    /* we're the parent */
156             /* Wait for the child to exit.
157              * We will not deadlock because we ran "/bin/sh &" */
158             while( waitpid( pid, NULL, 0 ) != pid);
159             break;
160     }
161 }
162
163 /*****************************************************************************
164  * Run: main thread
165  *****************************************************************************
166  * This part of the module is in a separate thread so that we do not have
167  * too much system() overhead.
168  *****************************************************************************/
169 static void Run( intf_thread_t *p_intf )
170 {
171     int canc = vlc_savecancel();
172 #ifdef HAVE_DBUS
173     p_intf->p_sys->p_connection = dbus_init( p_intf );
174 #endif
175
176     for( ;; )
177     {
178         vlc_object_t *p_vout;
179
180         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
181
182         /* If there is a video output, disable xscreensaver */
183         if( p_vout )
184         {
185             input_thread_t *p_input;
186             p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
187             vlc_object_release( p_vout );
188             if( p_input )
189             {
190                 if( PLAYING_S == var_GetInteger( p_input, "state" ) )
191                 {
192                     /* http://www.jwz.org/xscreensaver/faq.html#dvd */
193                     const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
194                             "xscreensaver-command -deactivate &", (char*)NULL };
195                     Execute( p_intf, ppsz_xsargs );
196
197                     /* If we have dbus support, let's communicate directly
198                        with gnome-screensave else, run
199                        gnome-screensaver-command */
200 #ifdef HAVE_DBUS
201                     poke_screensaver( p_intf, p_intf->p_sys->p_connection );
202 #else
203                     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
204                             "gnome-screensaver-command --poke &", (char*)NULL };
205                     Execute( p_intf, ppsz_gsargs );
206 #endif
207                     /* FIXME: add support for other screensavers */
208                 }
209                 vlc_object_release( p_input );
210             }
211         }
212
213         vlc_restorecancel( canc );
214         /* Check screensaver every 30 seconds */
215         msleep( 30 * CLOCK_FREQ );
216         canc = vlc_savecancel( );
217     }
218 }
219
220 #ifdef HAVE_DBUS
221
222 static DBusConnection * dbus_init( intf_thread_t *p_intf )
223 {
224     DBusError dbus_error;
225
226     dbus_error_init (&dbus_error);
227     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
228
229     if ( !p_connection )
230     {
231         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
232                           dbus_error.message);
233         dbus_error_free( &dbus_error );
234         return NULL;
235     }
236
237     return p_connection;
238 }
239
240 static void poke_screensaver( intf_thread_t *p_intf,
241                               DBusConnection *p_connection )
242 {
243     if( screensaver_is_running( p_connection, GS_SERVICE ) )
244     {
245 #   ifdef SCREENSAVER_DEBUG
246         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
247 #   endif
248         /* gnome-screensaver changed it's D-Bus interface, so we need both */
249         screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
250                                        GS_INTERFACE, "Poke" );
251         screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
252                                        GS_INTERFACE, "SimulateUserActivity" );
253     }
254     else if( screensaver_is_running( p_connection, FDS_SERVICE ) )
255     {
256 #   ifdef SCREENSAVER_DEBUG
257         msg_Dbg( p_intf, "found a running freedesktop-screensaver instance" );
258 #   endif
259         screensaver_send_message_void( p_intf, p_connection, FDS_SERVICE, FDS_PATH,
260                                        FDS_INTERFACE, "SimulateUserActivity" );
261     }
262 #   ifdef SCREENSAVER_DEBUG
263     else
264     {
265         msg_Dbg( p_intf, "found no running (gnome|freedesktop)-screensaver instance" );
266     }
267 #   endif
268
269 }
270
271 static void screensaver_send_message_void ( intf_thread_t *p_intf,
272                                        DBusConnection *p_connection,
273                                        const char *psz_service,
274                                        const char *psz_path,
275                                        const char *psz_interface,
276                                        const char *psz_name )
277 {
278     DBusMessage *p_message;
279
280     if( !p_connection || !psz_name ) return;
281
282     p_message = dbus_message_new_method_call( psz_service, psz_path,
283                                               psz_interface, psz_name );
284     if( p_message == NULL )
285     {
286         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
287         return;
288     }
289
290     if( !dbus_connection_send( p_connection, p_message, NULL ) )
291     {
292         msg_Err( p_intf, "DBUS communication failed" );
293     }
294
295     dbus_connection_flush( p_connection );
296
297     dbus_message_unref( p_message );
298 }
299
300 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service )
301 {
302     DBusError error;
303     bool b_return;
304
305     if( !p_connection ) return false;
306
307     dbus_error_init( &error );
308     b_return = dbus_bus_name_has_owner( p_connection, psz_service, &error );
309     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
310
311     return b_return;
312 }
313
314 #endif
315