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_playlist.h>
37 #include <vlc_interface.h>
39 #include <sys/types.h>
46 #include <dbus/dbus.h>
48 #define GS_SERVICE "org.gnome.ScreenSaver"
49 #define GS_PATH "/org/gnome/ScreenSaver"
50 #define GS_INTERFACE "org.gnome.ScreenSaver"
52 #define FDS_SERVICE "org.freedesktop.ScreenSaver"
53 #define FDS_PATH "/ScreenSaver"
54 #define FDS_INTERFACE "org.freedesktop.ScreenSaver"
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Activate ( vlc_object_t * );
62 static void Deactivate ( vlc_object_t * );
64 static void Timer( vlc_timer_t *, void * );
68 static DBusConnection * dbus_init( intf_thread_t *p_intf );
69 static void poke_screensaver( intf_thread_t *p_intf,
70 DBusConnection *p_connection );
71 static void screensaver_send_message_void ( intf_thread_t *p_intf,
72 DBusConnection *p_connection,
73 const char *psz_service,
75 const char *psz_interface,
76 const char *psz_name );
77 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service );
83 DBusConnection *p_connection;
89 /*****************************************************************************
91 *****************************************************************************/
93 set_description( N_("X Screensaver disabler") )
94 set_capability( "interface", 0 )
95 set_callbacks( Activate, Deactivate )
98 /*****************************************************************************
99 * Activate: initialize and create stuff
100 *****************************************************************************/
101 static int Activate( vlc_object_t *p_this )
103 intf_thread_t *p_intf = (intf_thread_t*)p_this;
106 p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
110 if( vlc_timer_create( &p_sys->timer, Timer, p_intf ) )
115 vlc_timer_schedule( &p_sys->timer, false, 30*CLOCK_FREQ, 30*CLOCK_FREQ );
118 p_sys->p_connection = dbus_init( p_intf );
123 /*****************************************************************************
124 * Deactivate: uninitialize and cleanup
125 *****************************************************************************/
126 static void Deactivate( vlc_object_t *p_this )
128 intf_thread_t *p_intf = (intf_thread_t*)p_this;
129 intf_sys_t *p_sys = p_intf->p_sys;
131 vlc_timer_destroy( &p_sys->timer );
133 if( p_sys->p_connection )
134 dbus_connection_unref( p_sys->p_connection );
140 /*****************************************************************************
141 * Execute: Spawns a process using execv()
142 *****************************************************************************/
143 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
148 case 0: /* we're the child */
152 pthread_sigmask (SIG_SETMASK, &set, NULL);
154 /* We don't want output */
155 if( ( freopen( "/dev/null", "w", stdout ) != NULL )
156 && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
157 execv( ppsz_args[0] , (char *const *)ppsz_args );
158 /* If the file we want to execute doesn't exist we exit() */
159 exit( EXIT_FAILURE );
161 case -1: /* we're the error */
162 msg_Dbg( p_this, "Couldn't fork() while launching %s",
165 default: /* we're the parent */
166 /* Wait for the child to exit.
167 * We will not deadlock because we ran "/bin/sh &" */
168 while( waitpid( pid, NULL, 0 ) != pid);
173 /*****************************************************************************
175 *****************************************************************************
176 * This part of the module is in a separate thread so that we do not have
177 * too much system() overhead.
178 *****************************************************************************/
179 static void Timer( vlc_timer_t *id, void *data )
181 intf_thread_t *p_intf = data;
182 playlist_t *p_playlist = pl_Hold( p_intf );
183 input_thread_t *p_input = playlist_CurrentInput( p_playlist );
184 pl_Release( p_intf );
188 vlc_object_t *p_vout;
189 if( var_GetInteger( p_input, "state" ) == PLAYING_S )
190 p_vout = (vlc_object_t *)input_GetVout( p_input );
193 vlc_object_release( p_input );
196 vlc_object_release( p_vout );
198 /* If there is a playing video output, disable xscreensaver */
199 /* http://www.jwz.org/xscreensaver/faq.html#dvd */
200 const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
201 "xscreensaver-command -deactivate &", (char*)NULL };
202 Execute( p_intf, ppsz_xsargs );
204 /* If we have dbus support, let's communicate directly with
205 * gnome-screensave else, run gnome-screensaver-command */
207 poke_screensaver( p_intf, p_intf->p_sys->p_connection );
209 const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
210 "gnome-screensaver-command --poke &", (char*)NULL };
211 Execute( p_intf, ppsz_gsargs );
213 /* FIXME: add support for other screensavers */
220 static DBusConnection * dbus_init( intf_thread_t *p_intf )
222 DBusError dbus_error;
224 dbus_error_init (&dbus_error);
225 DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
229 msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
231 dbus_error_free( &dbus_error );
238 static void poke_screensaver( intf_thread_t *p_intf,
239 DBusConnection *p_connection )
241 if( screensaver_is_running( p_connection, GS_SERVICE ) )
243 # ifdef SCREENSAVER_DEBUG
244 msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
246 /* gnome-screensaver changed it's D-Bus interface, so we need both */
247 screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
248 GS_INTERFACE, "Poke" );
249 screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
250 GS_INTERFACE, "SimulateUserActivity" );
252 else if( screensaver_is_running( p_connection, FDS_SERVICE ) )
254 # ifdef SCREENSAVER_DEBUG
255 msg_Dbg( p_intf, "found a running freedesktop-screensaver instance" );
257 screensaver_send_message_void( p_intf, p_connection, FDS_SERVICE, FDS_PATH,
258 FDS_INTERFACE, "SimulateUserActivity" );
260 # ifdef SCREENSAVER_DEBUG
263 msg_Dbg( p_intf, "found no running (gnome|freedesktop)-screensaver instance" );
269 static void screensaver_send_message_void ( intf_thread_t *p_intf,
270 DBusConnection *p_connection,
271 const char *psz_service,
272 const char *psz_path,
273 const char *psz_interface,
274 const char *psz_name )
276 DBusMessage *p_message;
278 if( !p_connection || !psz_name ) return;
280 p_message = dbus_message_new_method_call( psz_service, psz_path,
281 psz_interface, psz_name );
282 if( p_message == NULL )
284 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
288 if( !dbus_connection_send( p_connection, p_message, NULL ) )
290 msg_Err( p_intf, "DBUS communication failed" );
293 dbus_connection_flush( p_connection );
295 dbus_message_unref( p_message );
298 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service )
303 if( !p_connection ) return false;
305 dbus_error_init( &error );
306 b_return = dbus_bus_name_has_owner( p_connection, psz_service, &error );
307 if( dbus_error_is_set( &error ) ) dbus_error_free (&error);