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 *****************************************************************************/
37 #define DBUS_API_SUBJECT_TO_CHANGE
38 #include <dbus/dbus.h>
40 #define GS_SERVICE "org.gnome.ScreenSaver"
41 #define GS_PATH "/org/gnome/ScreenSaver"
42 #define GS_INTERFACE "org.gnome.ScreenSaver"
46 /* this is for dbus < 0.3 */
48 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Activate ( vlc_object_t * );
55 static void Deactivate ( vlc_object_t * );
57 static void Run ( intf_thread_t *p_intf );
61 static DBusConnection * dbus_init( intf_thread_t *p_intf );
62 static void poke_screensaver( intf_thread_t *p_intf,
63 DBusConnection *p_connection );
64 static void screensaver_send_message_void ( intf_thread_t *p_intf,
65 DBusConnection *p_connection,
66 const char *psz_name );
67 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection );
72 DBusConnection *p_connection;
77 /*****************************************************************************
79 *****************************************************************************/
81 set_description( _("X Screensaver disabler") );
82 set_capability( "interface", 0 );
83 set_callbacks( Activate, Deactivate );
86 /*****************************************************************************
87 * Activate: initialize and create stuff
88 *****************************************************************************/
89 static int Activate( vlc_object_t *p_this )
91 intf_thread_t *p_intf = (intf_thread_t*)p_this;
96 p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
97 if( !p_intf->p_sys ) return VLC_ENOMEM;
103 /*****************************************************************************
104 * Deactivate: uninitialize and cleanup
105 *****************************************************************************/
106 static void Deactivate( vlc_object_t *p_this )
109 intf_thread_t *p_intf = (intf_thread_t*)p_this;
111 if( p_intf->p_sys->p_connection )
114 dbus_connection_close( p_intf->p_sys->p_connection );
116 dbus_connection_disconnect( p_intf->p_sys->p_connection );
122 free( p_intf->p_sys );
123 p_intf->p_sys = NULL;
129 /*****************************************************************************
131 *****************************************************************************
132 * This part of the module is in a separate thread so that we do not have
133 * too much system() overhead.
134 *****************************************************************************/
135 static void Run( intf_thread_t *p_intf )
140 p_intf->p_sys->p_connection = dbus_init( p_intf );
143 while( !p_intf->b_die )
145 msleep( INTF_IDLE_SLEEP*5 ); // 250ms
147 /* Check screensaver every 30 seconds */
148 if( ++i_lastcall > 300 )
150 vlc_object_t *p_vout;
151 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
152 /* If there is a video output, disable xscreensaver */
155 vlc_object_release( p_vout );
157 /* http://www.jwz.org/xscreensaver/faq.html#dvd */
158 system( "xscreensaver-command -deactivate >&- 2>&- &" );
160 /* If we have dbug support, let's communicate directly with gnome-screensaver
161 else, run gnome-screensaver-command */
163 poke_screensaver( p_intf, p_intf->p_sys->p_connection );
165 system( "gnome-screensaver-command --poke >&- 2>&- &" );
167 /* FIXME: add support for other screensavers */
177 static DBusConnection * dbus_init( intf_thread_t *p_intf )
179 DBusError dbus_error;
181 dbus_error_init (&dbus_error);
182 DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
186 msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
188 dbus_error_free( &dbus_error );
195 static void poke_screensaver( intf_thread_t *p_intf,
196 DBusConnection *p_connection )
198 if( screensaver_is_running( p_connection ) )
200 # ifdef SCREENSAVER_DEBUG
201 msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
203 screensaver_send_message_void( p_intf, p_connection, "Poke" );
205 # ifdef SCREENSAVER_DEBUG
208 msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
213 static void screensaver_send_message_void ( intf_thread_t *p_intf,
214 DBusConnection *p_connection,
215 const char *psz_name )
217 DBusMessage *p_message;
219 if( !p_connection || !psz_name ) return;
221 p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
222 GS_INTERFACE, psz_name );
223 if( p_message == NULL )
225 msg_Err( p_intf, "DBUS initialization failed: message initialization" );
229 if( !dbus_connection_send( p_connection, p_message, NULL ) )
231 msg_Err( p_intf, "DBUS communication failed" );
234 dbus_connection_flush( p_connection );
236 dbus_message_unref( p_message );
239 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
244 if( !p_connection ) return VLC_FALSE;
246 dbus_error_init( &error );
247 b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
248 if( dbus_error_is_set( &error ) ) dbus_error_free (&error);