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>
47 #include <dbus/dbus.h>
49 #define GS_SERVICE "org.gnome.ScreenSaver"
50 #define GS_PATH "/org/gnome/ScreenSaver"
51 #define GS_INTERFACE "org.gnome.ScreenSaver"
53 #define FDS_SERVICE "org.freedesktop.ScreenSaver"
54 #define FDS_PATH "/ScreenSaver"
55 #define FDS_INTERFACE "org.freedesktop.ScreenSaver"
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_service,
76 const char *psz_interface,
77 const char *psz_name );
78 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service );
83 DBusConnection *p_connection;
88 /*****************************************************************************
90 *****************************************************************************/
92 set_description( N_("X Screensaver disabler") )
93 set_capability( "interface", 0 )
94 set_callbacks( Activate, Deactivate )
97 /*****************************************************************************
98 * Activate: initialize and create stuff
99 *****************************************************************************/
100 static int Activate( vlc_object_t *p_this )
102 intf_thread_t *p_intf = (intf_thread_t*)p_this;
104 p_intf->pf_run = Run;
107 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
108 if( !p_intf->p_sys ) return VLC_ENOMEM;
114 /*****************************************************************************
115 * Deactivate: uninitialize and cleanup
116 *****************************************************************************/
117 static void Deactivate( vlc_object_t *p_this )
120 intf_thread_t *p_intf = (intf_thread_t*)p_this;
122 if( p_intf->p_sys->p_connection )
124 dbus_connection_unref( p_intf->p_sys->p_connection );
127 free( p_intf->p_sys );
128 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 )
140 case 0: /* we're the child */
144 pthread_sigmask (SIG_SETMASK, &set, NULL);
146 /* We don't want output */
147 if( ( freopen( "/dev/null", "w", stdout ) != NULL )
148 && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
149 execv( ppsz_args[0] , (char *const *)ppsz_args );
150 /* If the file we want to execute doesn't exist we exit() */
151 exit( EXIT_FAILURE );
153 case -1: /* we're the error */
154 msg_Dbg( p_this, "Couldn't fork() while launching %s",
157 default: /* we're the parent */
158 /* Wait for the child to exit.
159 * We will not deadlock because we ran "/bin/sh &" */
160 while( waitpid( pid, NULL, 0 ) != pid);
165 /*****************************************************************************
167 *****************************************************************************
168 * This part of the module is in a separate thread so that we do not have
169 * too much system() overhead.
170 *****************************************************************************/
171 static void Run( intf_thread_t *p_intf )
173 int canc = vlc_savecancel();
175 p_intf->p_sys->p_connection = dbus_init( p_intf );
180 vlc_object_t *p_vout;
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 == var_GetInteger( p_input, "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_restorecancel( canc );
216 /* Check screensaver every 30 seconds */
217 msleep( 30 * CLOCK_FREQ );
218 canc = vlc_savecancel( );
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, GS_SERVICE ) )
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, GS_SERVICE, GS_PATH,
252 GS_INTERFACE, "Poke" );
253 screensaver_send_message_void( p_intf, p_connection, GS_SERVICE, GS_PATH,
254 GS_INTERFACE, "SimulateUserActivity" );
256 else if( screensaver_is_running( p_connection, FDS_SERVICE ) )
258 # ifdef SCREENSAVER_DEBUG
259 msg_Dbg( p_intf, "found a running freedesktop-screensaver instance" );
261 screensaver_send_message_void( p_intf, p_connection, FDS_SERVICE, FDS_PATH,
262 FDS_INTERFACE, "SimulateUserActivity" );
264 # ifdef SCREENSAVER_DEBUG
267 msg_Dbg( p_intf, "found no running (gnome|freedesktop)-screensaver instance" );
273 static void screensaver_send_message_void ( intf_thread_t *p_intf,
274 DBusConnection *p_connection,
275 const char *psz_service,
276 const char *psz_path,
277 const char *psz_interface,
278 const char *psz_name )
280 DBusMessage *p_message;
282 if( !p_connection || !psz_name ) return;
284 p_message = dbus_message_new_method_call( psz_service, psz_path,
285 psz_interface, psz_name );
286 if( p_message == NULL )
288 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
292 if( !dbus_connection_send( p_connection, p_message, NULL ) )
294 msg_Err( p_intf, "DBUS communication failed" );
297 dbus_connection_flush( p_connection );
299 dbus_message_unref( p_message );
302 static bool screensaver_is_running( DBusConnection *p_connection, const char *psz_service )
307 if( !p_connection ) return false;
309 dbus_error_init( &error );
310 b_return = dbus_bus_name_has_owner( p_connection, psz_service, &error );
311 if( dbus_error_is_set( &error ) ) dbus_error_free (&error);