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 *****************************************************************************/
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
35 #include <sys/types.h>
45 #define DBUS_API_SUBJECT_TO_CHANGE
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"
54 /* this is for dbus < 0.3 */
56 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
59 /*****************************************************************************
61 *****************************************************************************/
62 static int Activate ( vlc_object_t * );
63 static void Deactivate ( vlc_object_t * );
65 static void Run ( intf_thread_t *p_intf );
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_name );
75 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection );
80 DBusConnection *p_connection;
85 /*****************************************************************************
87 *****************************************************************************/
89 set_description( _("X Screensaver disabler") );
90 set_capability( "interface", 0 );
91 set_callbacks( Activate, Deactivate );
94 /*****************************************************************************
95 * Activate: initialize and create stuff
96 *****************************************************************************/
97 static int Activate( vlc_object_t *p_this )
99 intf_thread_t *p_intf = (intf_thread_t*)p_this;
101 p_intf->pf_run = Run;
104 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
105 if( !p_intf->p_sys ) return VLC_ENOMEM;
111 /*****************************************************************************
112 * Deactivate: uninitialize and cleanup
113 *****************************************************************************/
114 static void Deactivate( vlc_object_t *p_this )
117 intf_thread_t *p_intf = (intf_thread_t*)p_this;
119 if( p_intf->p_sys->p_connection )
122 dbus_connection_unref( p_intf->p_sys->p_connection );
124 dbus_connection_disconnect( p_intf->p_sys->p_connection );
130 free( p_intf->p_sys );
131 p_intf->p_sys = NULL;
136 /*****************************************************************************
137 * Execute: Spawns a process using execv()
138 *****************************************************************************/
139 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
144 case 0: /* we're the child */
148 pthread_sigmask (SIG_SETMASK, &set, NULL);
150 /* We don't want output */
151 freopen( "/dev/null", "w", stdout );
152 freopen( "/dev/null", "w", stderr );
153 execv( ppsz_args[0] , (char *const *)ppsz_args );
154 /* If the file we want to execute doesn't exist we exit() */
155 exit( EXIT_FAILURE );
157 case -1: /* we're the error */
158 msg_Dbg( p_this, "Couldn't fork() while launching %s",
161 default: /* we're the parent */
162 /* Wait for the child to exit.
163 * We will not deadlock because we ran "/bin/sh &" */
164 while( waitpid( pid, NULL, 0 ) != pid);
169 /*****************************************************************************
171 *****************************************************************************
172 * This part of the module is in a separate thread so that we do not have
173 * too much system() overhead.
174 *****************************************************************************/
175 static void Run( intf_thread_t *p_intf )
177 vlc_object_lock( p_intf );
180 p_intf->p_sys->p_connection = dbus_init( p_intf );
183 while( vlc_object_alive( p_intf ) )
185 vlc_object_t *p_vout;
187 /* Check screensaver every 30 seconds */
188 if( vlc_object_timedwait( p_intf, mdate() + 30000000 ) < 0 )
191 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
193 /* If there is a video output, disable xscreensaver */
196 input_thread_t *p_input;
197 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
198 vlc_object_release( p_vout );
201 if( PLAYING_S == p_input->i_state )
203 /* http://www.jwz.org/xscreensaver/faq.html#dvd */
204 const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
205 "xscreensaver-command -deactivate &", (char*)NULL };
206 Execute( p_intf, ppsz_xsargs );
208 /* If we have dbus support, let's communicate directly
209 with gnome-screensave else, run
210 gnome-screensaver-command */
212 poke_screensaver( p_intf, p_intf->p_sys->p_connection );
214 const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
215 "gnome-screensaver-command --poke &", (char*)NULL };
216 Execute( p_intf, ppsz_gsargs );
218 /* FIXME: add support for other screensavers */
220 vlc_object_release( p_input );
224 vlc_object_unlock( p_intf );
229 static DBusConnection * dbus_init( intf_thread_t *p_intf )
231 DBusError dbus_error;
233 dbus_error_init (&dbus_error);
234 DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
238 msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
240 dbus_error_free( &dbus_error );
247 static void poke_screensaver( intf_thread_t *p_intf,
248 DBusConnection *p_connection )
250 if( screensaver_is_running( p_connection ) )
252 # ifdef SCREENSAVER_DEBUG
253 msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
255 /* gnome-screensaver changed it's D-Bus interface, so we need both */
256 screensaver_send_message_void( p_intf, p_connection, "Poke" );
257 screensaver_send_message_void( p_intf, p_connection,
258 "SimulateUserActivity" );
260 # ifdef SCREENSAVER_DEBUG
263 msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
268 static void screensaver_send_message_void ( intf_thread_t *p_intf,
269 DBusConnection *p_connection,
270 const char *psz_name )
272 DBusMessage *p_message;
274 if( !p_connection || !psz_name ) return;
276 p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
277 GS_INTERFACE, psz_name );
278 if( p_message == NULL )
280 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
284 if( !dbus_connection_send( p_connection, p_message, NULL ) )
286 msg_Err( p_intf, "DBUS communication failed" );
289 dbus_connection_flush( p_connection );
291 dbus_message_unref( p_message );
294 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
299 if( !p_connection ) return VLC_FALSE;
301 dbus_error_init( &error );
302 b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
303 if( dbus_error_is_set( &error ) ) dbus_error_free (&error);