* fixed a crash when quitting VLC while scrolling the credits in the about window...
[vlc.git] / 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
29 #include <vlc/vlc.h>
30 #include <vlc_input.h>
31 #include <vlc_interface.h>
32 #include <vlc_aout.h>
33 #include <vlc_vout.h>
34
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <unistd.h>
38
39 #ifdef HAVE_SIGNAL_H
40 #   include <signal.h>
41 #endif
42
43 #ifdef HAVE_DBUS
44
45 #define DBUS_API_SUBJECT_TO_CHANGE
46 #include <dbus/dbus.h>
47
48 #define GS_SERVICE   "org.gnome.ScreenSaver"
49 #define GS_PATH      "/org/gnome/ScreenSaver"
50 #define GS_INTERFACE "org.gnome.ScreenSaver"
51
52 #endif
53
54 /* this is for dbus < 0.3 */
55 #ifndef HAVE_DBUS_1
56 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
57 #endif
58
59 /*****************************************************************************
60  * Local prototypes
61  *****************************************************************************/
62 static int  Activate     ( vlc_object_t * );
63 static void  Deactivate   ( vlc_object_t * );
64
65 static void Run          ( intf_thread_t *p_intf );
66
67 #ifdef HAVE_DBUS
68
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 );
76
77
78 struct intf_sys_t
79 {
80     DBusConnection *p_connection;
81 };
82
83 #endif
84
85 /*****************************************************************************
86  * Module descriptor
87  *****************************************************************************/
88 vlc_module_begin();
89     set_description( _("X Screensaver disabler") );
90     set_capability( "interface", 0 );
91     set_callbacks( Activate, Deactivate );
92 vlc_module_end();
93
94 /*****************************************************************************
95  * Activate: initialize and create stuff
96  *****************************************************************************/
97 static int Activate( vlc_object_t *p_this )
98 {
99     intf_thread_t *p_intf = (intf_thread_t*)p_this;
100
101     p_intf->pf_run = Run;
102
103 #ifdef HAVE_DBUS
104     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
105     if( !p_intf->p_sys ) return VLC_ENOMEM;
106 #endif
107
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * Deactivate: uninitialize and cleanup
113  *****************************************************************************/
114 static void Deactivate( vlc_object_t *p_this )
115 {
116 #ifdef HAVE_DBUS
117     intf_thread_t *p_intf = (intf_thread_t*)p_this;
118
119     if( p_intf->p_sys->p_connection )
120     {
121 #  ifdef HAVE_DBUS_2
122         dbus_connection_unref( p_intf->p_sys->p_connection );
123 #  else
124         dbus_connection_disconnect( p_intf->p_sys->p_connection );
125 #  endif
126     }
127
128     if( p_intf->p_sys )
129     {
130         free( p_intf->p_sys );
131         p_intf->p_sys = NULL;
132     }
133 #endif
134 }
135
136 /*****************************************************************************
137  * Execute: Spawns a process using execv()
138  *****************************************************************************/
139 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
140 {
141     pid_t pid = fork();
142     switch( pid )
143     {
144         case 0:     /* we're the child */
145         {
146             sigset_t set;
147             sigemptyset (&set);
148             pthread_sigmask (SIG_SETMASK, &set, NULL);
149
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 );
156         }
157         case -1:    /* we're the error */
158             msg_Dbg( p_this, "Couldn't fork() while launching %s",
159                      ppsz_args[0] );
160             break;
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);
165             break;
166     }
167 }
168
169 /*****************************************************************************
170  * Run: main thread
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 )
176 {
177     vlc_object_lock( p_intf );
178
179 #ifdef HAVE_DBUS
180     p_intf->p_sys->p_connection = dbus_init( p_intf );
181 #endif
182
183     while( vlc_object_alive( p_intf ) )
184     {
185         vlc_object_t *p_vout;
186
187         /* Check screensaver every 30 seconds */
188         if( vlc_object_timedwait( p_intf, mdate() + 30000000 ) < 0 )
189             continue;
190
191         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
192
193         /* If there is a video output, disable xscreensaver */
194         if( p_vout )
195         {
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 );
199             if( p_input )
200             {
201                 if( PLAYING_S == p_input->i_state )
202                 {
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 );
207
208                     /* If we have dbus support, let's communicate directly
209                        with gnome-screensave else, run
210                        gnome-screensaver-command */
211 #ifdef HAVE_DBUS
212                     poke_screensaver( p_intf, p_intf->p_sys->p_connection );
213 #else
214                     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
215                             "gnome-screensaver-command --poke &", (char*)NULL };
216                     Execute( p_intf, ppsz_gsargs );
217 #endif
218                     /* FIXME: add support for other screensavers */
219                 }
220                 vlc_object_release( p_input );
221             }
222         }
223     }
224     vlc_object_unlock( p_intf );
225 }
226
227 #ifdef HAVE_DBUS
228
229 static DBusConnection * dbus_init( intf_thread_t *p_intf )
230 {
231     DBusError dbus_error;
232
233     dbus_error_init (&dbus_error);
234     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
235
236     if ( !p_connection )
237     {
238         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
239                           dbus_error.message);
240         dbus_error_free( &dbus_error );
241         return NULL;
242     }
243
244     return p_connection;
245 }
246
247 static void poke_screensaver( intf_thread_t *p_intf,
248                               DBusConnection *p_connection )
249 {
250     if( screensaver_is_running( p_connection ) )
251     {
252 #   ifdef SCREENSAVER_DEBUG
253         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
254 #   endif
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" );
259     }
260 #   ifdef SCREENSAVER_DEBUG
261     else
262     {
263         msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
264     }
265 #   endif
266 }
267
268 static void screensaver_send_message_void ( intf_thread_t *p_intf,
269                                        DBusConnection *p_connection,
270                                        const char *psz_name )
271 {
272     DBusMessage *p_message;
273
274     if( !p_connection || !psz_name ) return;
275
276     p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
277                                               GS_INTERFACE, psz_name );
278     if( p_message == NULL )
279     {
280         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
281         return;
282     }
283
284     if( !dbus_connection_send( p_connection, p_message, NULL ) )
285     {
286         msg_Err( p_intf, "DBUS communication failed" );
287     }
288
289     dbus_connection_flush( p_connection );
290
291     dbus_message_unref( p_message );
292 }
293
294 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
295 {
296     DBusError error;
297     vlc_bool_t b_return;
298
299     if( !p_connection ) return VLC_FALSE;
300
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);
304
305     return b_return;
306 }
307
308 #endif
309