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>
41 #define DBUS_API_SUBJECT_TO_CHANGE
42 #include <dbus/dbus.h>
44 #define GS_SERVICE "org.gnome.ScreenSaver"
45 #define GS_PATH "/org/gnome/ScreenSaver"
46 #define GS_INTERFACE "org.gnome.ScreenSaver"
50 /* this is for dbus < 0.3 */
52 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Activate ( vlc_object_t * );
59 static void Deactivate ( vlc_object_t * );
61 static void Run ( intf_thread_t *p_intf );
65 static DBusConnection * dbus_init( intf_thread_t *p_intf );
66 static void poke_screensaver( intf_thread_t *p_intf,
67 DBusConnection *p_connection );
68 static void screensaver_send_message_void ( intf_thread_t *p_intf,
69 DBusConnection *p_connection,
70 const char *psz_name );
71 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection );
76 DBusConnection *p_connection;
81 /*****************************************************************************
83 *****************************************************************************/
85 set_description( _("X Screensaver disabler") );
86 set_capability( "interface", 0 );
87 set_callbacks( Activate, Deactivate );
90 /*****************************************************************************
91 * Activate: initialize and create stuff
92 *****************************************************************************/
93 static int Activate( vlc_object_t *p_this )
95 intf_thread_t *p_intf = (intf_thread_t*)p_this;
100 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
101 if( !p_intf->p_sys ) return VLC_ENOMEM;
107 /*****************************************************************************
108 * Deactivate: uninitialize and cleanup
109 *****************************************************************************/
110 static void Deactivate( vlc_object_t *p_this )
113 intf_thread_t *p_intf = (intf_thread_t*)p_this;
115 if( p_intf->p_sys->p_connection )
118 dbus_connection_unref( p_intf->p_sys->p_connection );
120 dbus_connection_disconnect( p_intf->p_sys->p_connection );
126 free( p_intf->p_sys );
127 p_intf->p_sys = NULL;
132 /*****************************************************************************
133 * Execute: Spawns a process using execv()
134 *****************************************************************************/
135 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
138 switch( pid = fork() )
140 case 0: /* we're the child */
141 /* We don't want output */
142 freopen( "/dev/null", "w", stdout );
143 freopen( "/dev/null", "w", stderr );
144 execv( ppsz_args[0] , (char *const *)ppsz_args );
145 /* If the file we want to execute doesn't exist we exit() */
148 case -1: /* we're the error */
149 msg_Dbg( p_this, "Couldn't fork() while launching %s",
152 default: /* we're the parent */
153 /* Wait for the child to exit.
154 * We will not deadlock because we ran "/bin/sh &" */
155 while( waitpid( pid, NULL, 0 ) != pid);
160 /*****************************************************************************
162 *****************************************************************************
163 * This part of the module is in a separate thread so that we do not have
164 * too much system() overhead.
165 *****************************************************************************/
166 static void Run( intf_thread_t *p_intf )
168 vlc_object_lock( p_intf );
171 p_intf->p_sys->p_connection = dbus_init( p_intf );
174 while( vlc_object_alive( p_intf ) )
176 vlc_object_t *p_vout;
178 /* Check screensaver every 30 seconds */
179 if( vlc_object_timedwait( p_intf, mdate() + 30000000 ) < 0 )
182 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
184 /* If there is a video output, disable xscreensaver */
187 input_thread_t *p_input;
188 p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
189 vlc_object_release( p_vout );
192 if( PLAYING_S == p_input->i_state )
194 /* http://www.jwz.org/xscreensaver/faq.html#dvd */
195 const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
196 "xscreensaver-command -deactivate &", (char*)NULL };
197 Execute( p_intf, ppsz_xsargs );
199 /* If we have dbus support, let's communicate directly
200 with gnome-screensave else, run
201 gnome-screensaver-command */
203 poke_screensaver( p_intf, p_intf->p_sys->p_connection );
205 const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
206 "gnome-screensaver-command --poke &", (char*)NULL };
207 Execute( p_intf, ppsz_gsargs );
209 /* FIXME: add support for other screensavers */
211 vlc_object_release( p_input );
215 vlc_object_unlock( p_intf );
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 ) )
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, "Poke" );
248 screensaver_send_message_void( p_intf, p_connection,
249 "SimulateUserActivity" );
251 # ifdef SCREENSAVER_DEBUG
254 msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
259 static void screensaver_send_message_void ( intf_thread_t *p_intf,
260 DBusConnection *p_connection,
261 const char *psz_name )
263 DBusMessage *p_message;
265 if( !p_connection || !psz_name ) return;
267 p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
268 GS_INTERFACE, psz_name );
269 if( p_message == NULL )
271 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
275 if( !dbus_connection_send( p_connection, p_message, NULL ) )
277 msg_Err( p_intf, "DBUS communication failed" );
280 dbus_connection_flush( p_connection );
282 dbus_message_unref( p_message );
285 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
290 if( !p_connection ) return VLC_FALSE;
292 dbus_error_init( &error );
293 b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
294 if( dbus_error_is_set( &error ) ) dbus_error_free (&error);