1 /*****************************************************************************
2 * screensaver.c : disable screen savers when VLC is playing
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Sam Hocevar <sam@zoy.org>
8 * Benjamin Pracht <bigben AT videolan DOT org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_interface.h>
38 #include <sys/types.h>
45 #include <dbus/dbus.h>
47 #define GS_SERVICE "org.gnome.ScreenSaver"
48 #define GS_PATH "/org/gnome/ScreenSaver"
49 #define GS_INTERFACE "org.gnome.ScreenSaver"
51 #define FDS_SERVICE "org.freedesktop.ScreenSaver"
52 #define FDS_PATH "/ScreenSaver"
53 #define FDS_INTERFACE "org.freedesktop.ScreenSaver"
57 /*****************************************************************************
59 *****************************************************************************/
60 static int Activate ( vlc_object_t * );
61 static void Deactivate ( vlc_object_t * );
63 static void Run ( intf_thread_t *p_intf );
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,
74 const char *psz_interface,
75 const char *psz_name );
76 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service );
81 DBusConnection *p_connection;
86 /*****************************************************************************
88 *****************************************************************************/
90 set_description( N_("X Screensaver disabler") )
91 set_capability( "interface", 0 )
92 set_callbacks( Activate, Deactivate )
95 /*****************************************************************************
96 * Activate: initialize and create stuff
97 *****************************************************************************/
98 static int Activate( vlc_object_t *p_this )
100 intf_thread_t *p_intf = (intf_thread_t*)p_this;
102 p_intf->pf_run = Run;
105 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
106 if( !p_intf->p_sys ) return VLC_ENOMEM;
112 /*****************************************************************************
113 * Deactivate: uninitialize and cleanup
114 *****************************************************************************/
115 static void Deactivate( vlc_object_t *p_this )
118 intf_thread_t *p_intf = (intf_thread_t*)p_this;
120 if( p_intf->p_sys->p_connection )
122 dbus_connection_unref( p_intf->p_sys->p_connection );
125 free( p_intf->p_sys );
126 p_intf->p_sys = NULL;
130 /*****************************************************************************
131 * Execute: Spawns a process using execv()
132 *****************************************************************************/
133 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
138 case 0: /* we're the child */
142 pthread_sigmask (SIG_SETMASK, &set, NULL);
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 );
151 case -1: /* we're the error */
152 msg_Dbg( p_this, "Couldn't fork() while launching %s",
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);
163 /*****************************************************************************
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 )
171 int canc = vlc_savecancel();
173 p_intf->p_sys->p_connection = dbus_init( p_intf );
178 vlc_object_t *p_vout;
180 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
182 /* If there is a video output, disable xscreensaver */
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 );
190 if( PLAYING_S == var_GetInteger( p_input, "state" ) )
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 );
197 /* If we have dbus support, let's communicate directly
198 with gnome-screensave else, run
199 gnome-screensaver-command */
201 poke_screensaver( p_intf, p_intf->p_sys->p_connection );
203 const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
204 "gnome-screensaver-command --poke &", (char*)NULL };
205 Execute( p_intf, ppsz_gsargs );
207 /* FIXME: add support for other screensavers */
209 vlc_object_release( p_input );
213 vlc_restorecancel( canc );
214 /* Check screensaver every 30 seconds */
215 msleep( 30 * CLOCK_FREQ );
216 canc = vlc_savecancel( );
222 static DBusConnection * dbus_init( intf_thread_t *p_intf )
224 DBusError dbus_error;
226 dbus_error_init (&dbus_error);
227 DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
231 msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
233 dbus_error_free( &dbus_error );
240 static void poke_screensaver( intf_thread_t *p_intf,
241 DBusConnection *p_connection )
243 if( screensaver_is_running( p_connection, GS_SERVICE ) )
245 # ifdef SCREENSAVER_DEBUG
246 msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
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" );
254 else if( screensaver_is_running( p_connection, FDS_SERVICE ) )
256 # ifdef SCREENSAVER_DEBUG
257 msg_Dbg( p_intf, "found a running freedesktop-screensaver instance" );
259 screensaver_send_message_void( p_intf, p_connection, FDS_SERVICE, FDS_PATH,
260 FDS_INTERFACE, "SimulateUserActivity" );
262 # ifdef SCREENSAVER_DEBUG
265 msg_Dbg( p_intf, "found no running (gnome|freedesktop)-screensaver instance" );
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 )
278 DBusMessage *p_message;
280 if( !p_connection || !psz_name ) return;
282 p_message = dbus_message_new_method_call( psz_service, psz_path,
283 psz_interface, psz_name );
284 if( p_message == NULL )
286 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
290 if( !dbus_connection_send( p_connection, p_message, NULL ) )
292 msg_Err( p_intf, "DBUS communication failed" );
295 dbus_connection_flush( p_connection );
297 dbus_message_unref( p_message );
300 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service )
305 if( !p_connection ) return false;
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);