]> git.sesse.net Git - vlc/blob - modules/misc/screensaver.c
Use new gnome-screensaver D-Bus interface
[vlc] / modules / misc / screensaver.c
1 /*****************************************************************************
2  * screensaver.c : disable screen savers when VLC is playing
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sam Hocevar <sam@zoy.org>
8  *          Benjamin Pracht <bigben AT videolan DOT org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc_input.h>
32 #include <vlc_interface.h>
33 #include <vlc_aout.h>
34 #include <vlc_vout.h>
35
36 #ifdef HAVE_DBUS
37
38 #define DBUS_API_SUBJECT_TO_CHANGE
39 #include <dbus/dbus.h>
40
41 #define GS_SERVICE   "org.gnome.ScreenSaver"
42 #define GS_PATH      "/org/gnome/ScreenSaver"
43 #define GS_INTERFACE "org.gnome.ScreenSaver"
44
45 #endif
46
47 /* this is for dbus < 0.3 */
48 #ifndef HAVE_DBUS_1
49 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
50 #endif
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static int  Activate     ( vlc_object_t * );
56 static void  Deactivate   ( vlc_object_t * );
57
58 static void Run          ( intf_thread_t *p_intf );
59
60 #ifdef HAVE_DBUS
61
62 static DBusConnection * dbus_init( intf_thread_t *p_intf );
63 static void poke_screensaver( intf_thread_t *p_intf,
64                               DBusConnection *p_connection );
65 static void screensaver_send_message_void ( intf_thread_t *p_intf,
66                                        DBusConnection *p_connection,
67                                        const char *psz_name );
68 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection );
69
70
71 struct intf_sys_t
72 {
73     DBusConnection *p_connection;
74 };
75
76 #endif
77
78 /*****************************************************************************
79  * Module descriptor
80  *****************************************************************************/
81 vlc_module_begin();
82     set_description( _("X Screensaver disabler") );
83     set_capability( "interface", 0 );
84     set_callbacks( Activate, Deactivate );
85 vlc_module_end();
86
87 /*****************************************************************************
88  * Activate: initialize and create stuff
89  *****************************************************************************/
90 static int Activate( vlc_object_t *p_this )
91 {
92     intf_thread_t *p_intf = (intf_thread_t*)p_this;
93
94     p_intf->pf_run = Run;
95
96 #ifdef HAVE_DBUS
97     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
98     if( !p_intf->p_sys ) return VLC_ENOMEM;
99 #endif
100
101     return VLC_SUCCESS;
102 }
103
104 /*****************************************************************************
105  * Deactivate: uninitialize and cleanup
106  *****************************************************************************/
107 static void Deactivate( vlc_object_t *p_this )
108 {
109 #ifdef HAVE_DBUS
110     intf_thread_t *p_intf = (intf_thread_t*)p_this;
111
112     if( p_intf->p_sys->p_connection )
113     {
114 #  ifdef HAVE_DBUS_2
115         dbus_connection_unref( p_intf->p_sys->p_connection );
116 #  else
117         dbus_connection_disconnect( p_intf->p_sys->p_connection );
118 #  endif
119     }
120
121     if( p_intf->p_sys )
122     {
123         free( p_intf->p_sys );
124         p_intf->p_sys = NULL;
125     }
126 #endif
127 }
128
129
130 /*****************************************************************************
131  * Run: main thread
132  *****************************************************************************
133  * This part of the module is in a separate thread so that we do not have
134  * too much system() overhead.
135  *****************************************************************************/
136 static void Run( intf_thread_t *p_intf )
137 {
138     int i_lastcall = 0;
139
140 #ifdef HAVE_DBUS
141     p_intf->p_sys->p_connection = dbus_init( p_intf );
142 #endif
143
144     while( !p_intf->b_die )
145     {
146         msleep( INTF_IDLE_SLEEP*5 ); // 250ms
147
148         /* Check screensaver every 30 seconds */
149         if( ++i_lastcall > 120 )
150         {
151             vlc_object_t *p_vout;
152             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
153             /* If there is a video output, disable xscreensaver */
154             if( p_vout )
155             {
156                 input_thread_t *p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
157                 vlc_object_release( p_vout );
158                 if( p_input )
159                 {
160                     if( PLAYING_S == p_input->i_state )
161                     {
162                         /* http://www.jwz.org/xscreensaver/faq.html#dvd */
163
164                         system( "xscreensaver-command -deactivate >&- 2>&- &" );
165
166                         /* If we have dbus support, let's communicate directly
167                            with gnome-screensave else, run
168                            gnome-screensaver-command */
169 #ifdef HAVE_DBUS
170                         poke_screensaver( p_intf, p_intf->p_sys->p_connection );
171 #else
172                         system( "gnome-screensaver-command --poke >&- 2>&- &" );
173 #endif
174                         /* FIXME: add support for other screensavers */
175                     }
176                     vlc_object_release( p_input );
177                 }
178             }
179             i_lastcall = 0;
180         }
181     }
182 }
183
184 #ifdef HAVE_DBUS
185
186 static DBusConnection * dbus_init( intf_thread_t *p_intf )
187 {
188     DBusError dbus_error;
189
190     dbus_error_init (&dbus_error);
191     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
192
193     if ( !p_connection )
194     {
195         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
196                           dbus_error.message);
197         dbus_error_free( &dbus_error );
198         return NULL;
199     }
200
201     return p_connection;
202 }
203
204 static void poke_screensaver( intf_thread_t *p_intf,
205                               DBusConnection *p_connection )
206 {
207     if( screensaver_is_running( p_connection ) )
208     {
209 #   ifdef SCREENSAVER_DEBUG
210         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
211 #   endif
212         /* gnome-screensaver changed it's D-Bus interface, so we need both */
213         screensaver_send_message_void( p_intf, p_connection, "Poke" );
214         screensaver_send_message_void( p_intf, p_connection, 
215                 "SimulateUserActivity" );
216     }
217 #   ifdef SCREENSAVER_DEBUG
218     else
219     {
220         msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
221     }
222 #   endif
223 }
224
225 static void screensaver_send_message_void ( intf_thread_t *p_intf,
226                                        DBusConnection *p_connection,
227                                        const char *psz_name )
228 {
229     DBusMessage *p_message;
230
231     if( !p_connection || !psz_name ) return;
232
233     p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
234                                               GS_INTERFACE, psz_name );
235     if( p_message == NULL )
236     {
237         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
238         return;
239     }
240
241     if( !dbus_connection_send( p_connection, p_message, NULL ) )
242     {
243         msg_Err( p_intf, "DBUS communication failed" );
244     }
245
246     dbus_connection_flush( p_connection );
247
248     dbus_message_unref( p_message );
249 }
250
251 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
252 {
253     DBusError error;
254     vlc_bool_t b_return;
255
256     if( !p_connection ) return VLC_FALSE;
257
258     dbus_error_init( &error );
259     b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
260     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
261
262     return b_return;
263 }
264
265 #endif
266