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>
40 #include <sys/types.h>
50 #include <dbus/dbus.h>
52 #define GS_SERVICE "org.gnome.ScreenSaver"
53 #define GS_PATH "/org/gnome/ScreenSaver"
54 #define GS_INTERFACE "org.gnome.ScreenSaver"
58 /*****************************************************************************
60 *****************************************************************************/
61 static int Activate ( vlc_object_t * );
62 static void Deactivate ( vlc_object_t * );
64 static void Run ( intf_thread_t *p_intf );
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_name );
74 static bool screensaver_is_running( DBusConnection *p_connection );
79 DBusConnection *p_connection;
84 /*****************************************************************************
86 *****************************************************************************/
88 set_description( N_("X Screensaver disabler") );
89 set_capability( "interface", 0 );
90 set_callbacks( Activate, Deactivate );
93 /*****************************************************************************
94 * Activate: initialize and create stuff
95 *****************************************************************************/
96 static int Activate( vlc_object_t *p_this )
98 intf_thread_t *p_intf = (intf_thread_t*)p_this;
100 p_intf->pf_run = Run;
103 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
104 if( !p_intf->p_sys ) return VLC_ENOMEM;
110 /*****************************************************************************
111 * Deactivate: uninitialize and cleanup
112 *****************************************************************************/
113 static void Deactivate( vlc_object_t *p_this )
116 intf_thread_t *p_intf = (intf_thread_t*)p_this;
118 if( p_intf->p_sys->p_connection )
120 dbus_connection_unref( p_intf->p_sys->p_connection );
123 free( p_intf->p_sys );
124 p_intf->p_sys = NULL;
128 /*****************************************************************************
129 * Execute: Spawns a process using execv()
130 *****************************************************************************/
131 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
136 case 0: /* we're the child */
140 pthread_sigmask (SIG_SETMASK, &set, NULL);
142 /* We don't want output */
143 if( ( freopen( "/dev/null", "w", stdout ) != NULL )
144 && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
145 execv( ppsz_args[0] , (char *const *)ppsz_args );
146 /* If the file we want to execute doesn't exist we exit() */
147 exit( EXIT_FAILURE );
149 case -1: /* we're the error */
150 msg_Dbg( p_this, "Couldn't fork() while launching %s",
153 default: /* we're the parent */
154 /* Wait for the child to exit.
155 * We will not deadlock because we ran "/bin/sh &" */
156 while( waitpid( pid, NULL, 0 ) != pid);
161 /*****************************************************************************
163 *****************************************************************************
164 * This part of the module is in a separate thread so that we do not have
165 * too much system() overhead.
166 *****************************************************************************/
167 static void Run( intf_thread_t *p_intf )
169 mtime_t deadline = mdate();
171 vlc_object_lock( p_intf );
173 p_intf->p_sys->p_connection = dbus_init( p_intf );
176 while( vlc_object_alive( p_intf ) )
178 vlc_object_t *p_vout;
180 if( vlc_object_timedwait( p_intf, deadline ) == 0 )
183 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
185 /* If there is a video output, disable xscreensaver */
188 input_thread_t *p_input;
189 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
190 vlc_object_release( p_vout );
193 if( PLAYING_S == p_input->i_state )
195 /* http://www.jwz.org/xscreensaver/faq.html#dvd */
196 const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
197 "xscreensaver-command -deactivate &", (char*)NULL };
198 Execute( p_intf, ppsz_xsargs );
200 /* If we have dbus support, let's communicate directly
201 with gnome-screensave else, run
202 gnome-screensaver-command */
204 poke_screensaver( p_intf, p_intf->p_sys->p_connection );
206 const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
207 "gnome-screensaver-command --poke &", (char*)NULL };
208 Execute( p_intf, ppsz_gsargs );
210 /* FIXME: add support for other screensavers */
212 vlc_object_release( p_input );
216 /* Check screensaver every 30 seconds */
217 deadline = mdate() + 30000000;
219 vlc_object_unlock( p_intf );
224 static DBusConnection * dbus_init( intf_thread_t *p_intf )
226 DBusError dbus_error;
228 dbus_error_init (&dbus_error);
229 DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
233 msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
235 dbus_error_free( &dbus_error );
242 static void poke_screensaver( intf_thread_t *p_intf,
243 DBusConnection *p_connection )
245 if( screensaver_is_running( p_connection ) )
247 # ifdef SCREENSAVER_DEBUG
248 msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
250 /* gnome-screensaver changed it's D-Bus interface, so we need both */
251 screensaver_send_message_void( p_intf, p_connection, "Poke" );
252 screensaver_send_message_void( p_intf, p_connection,
253 "SimulateUserActivity" );
255 # ifdef SCREENSAVER_DEBUG
258 msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
263 static void screensaver_send_message_void ( intf_thread_t *p_intf,
264 DBusConnection *p_connection,
265 const char *psz_name )
267 DBusMessage *p_message;
269 if( !p_connection || !psz_name ) return;
271 p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
272 GS_INTERFACE, psz_name );
273 if( p_message == NULL )
275 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
279 if( !dbus_connection_send( p_connection, p_message, NULL ) )
281 msg_Err( p_intf, "DBUS communication failed" );
284 dbus_connection_flush( p_connection );
286 dbus_message_unref( p_message );
289 static bool screensaver_is_running( DBusConnection *p_connection )
294 if( !p_connection ) return false;
296 dbus_error_init( &error );
297 b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
298 if( dbus_error_is_set( &error ) ) dbus_error_free (&error);