Clear signal mask _between_ fork() and exec*().
[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_DBUS
40
41 #define DBUS_API_SUBJECT_TO_CHANGE
42 #include <dbus/dbus.h>
43
44 #define GS_SERVICE   "org.gnome.ScreenSaver"
45 #define GS_PATH      "/org/gnome/ScreenSaver"
46 #define GS_INTERFACE "org.gnome.ScreenSaver"
47
48 #endif
49
50 /* this is for dbus < 0.3 */
51 #ifndef HAVE_DBUS_1
52 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
53 #endif
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Activate     ( vlc_object_t * );
59 static void  Deactivate   ( vlc_object_t * );
60
61 static void Run          ( intf_thread_t *p_intf );
62
63 #ifdef HAVE_DBUS
64
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 );
72
73
74 struct intf_sys_t
75 {
76     DBusConnection *p_connection;
77 };
78
79 #endif
80
81 /*****************************************************************************
82  * Module descriptor
83  *****************************************************************************/
84 vlc_module_begin();
85     set_description( _("X Screensaver disabler") );
86     set_capability( "interface", 0 );
87     set_callbacks( Activate, Deactivate );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Activate: initialize and create stuff
92  *****************************************************************************/
93 static int Activate( vlc_object_t *p_this )
94 {
95     intf_thread_t *p_intf = (intf_thread_t*)p_this;
96
97     p_intf->pf_run = Run;
98
99 #ifdef HAVE_DBUS
100     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
101     if( !p_intf->p_sys ) return VLC_ENOMEM;
102 #endif
103
104     return VLC_SUCCESS;
105 }
106
107 /*****************************************************************************
108  * Deactivate: uninitialize and cleanup
109  *****************************************************************************/
110 static void Deactivate( vlc_object_t *p_this )
111 {
112 #ifdef HAVE_DBUS
113     intf_thread_t *p_intf = (intf_thread_t*)p_this;
114
115     if( p_intf->p_sys->p_connection )
116     {
117 #  ifdef HAVE_DBUS_2
118         dbus_connection_unref( p_intf->p_sys->p_connection );
119 #  else
120         dbus_connection_disconnect( p_intf->p_sys->p_connection );
121 #  endif
122     }
123
124     if( p_intf->p_sys )
125     {
126         free( p_intf->p_sys );
127         p_intf->p_sys = NULL;
128     }
129 #endif
130 }
131
132 /*****************************************************************************
133  * Execute: Spawns a process using execv()
134  *****************************************************************************/
135 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
136 {
137     pid_t pid = fork();
138     switch( pid )
139     {
140         case 0:     /* we're the child */
141         {
142             sigset_t set;
143             sigemptyset (&set);
144             pthread_sigmask (SIG_SETMASK, &set, NULL);
145
146             /* We don't want output */
147             freopen( "/dev/null", "w", stdout );
148             freopen( "/dev/null", "w", stderr );
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 );
152         }
153         case -1:    /* we're the error */
154             msg_Dbg( p_this, "Couldn't fork() while launching %s",
155                      ppsz_args[0] );
156             break;
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);
161             break;
162     }
163 }
164
165 /*****************************************************************************
166  * Run: main thread
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 )
172 {
173     vlc_object_lock( p_intf );
174
175 #ifdef HAVE_DBUS
176     p_intf->p_sys->p_connection = dbus_init( p_intf );
177 #endif
178
179     while( vlc_object_alive( p_intf ) )
180     {
181         vlc_object_t *p_vout;
182
183         /* Check screensaver every 30 seconds */
184         if( vlc_object_timedwait( p_intf, mdate() + 30000000 ) < 0 )
185             continue;
186
187         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
188
189         /* If there is a video output, disable xscreensaver */
190         if( p_vout )
191         {
192             input_thread_t *p_input;
193             p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
194             vlc_object_release( p_vout );
195             if( p_input )
196             {
197                 if( PLAYING_S == p_input->i_state )
198                 {
199                     /* http://www.jwz.org/xscreensaver/faq.html#dvd */
200                     const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
201                             "xscreensaver-command -deactivate &", (char*)NULL };
202                     Execute( p_intf, ppsz_xsargs );
203
204                     /* If we have dbus support, let's communicate directly
205                        with gnome-screensave else, run
206                        gnome-screensaver-command */
207 #ifdef HAVE_DBUS
208                     poke_screensaver( p_intf, p_intf->p_sys->p_connection );
209 #else
210                     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
211                             "gnome-screensaver-command --poke &", (char*)NULL };
212                     Execute( p_intf, ppsz_gsargs );
213 #endif
214                     /* FIXME: add support for other screensavers */
215                 }
216                 vlc_object_release( p_input );
217             }
218         }
219     }
220     vlc_object_unlock( p_intf );
221 }
222
223 #ifdef HAVE_DBUS
224
225 static DBusConnection * dbus_init( intf_thread_t *p_intf )
226 {
227     DBusError dbus_error;
228
229     dbus_error_init (&dbus_error);
230     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
231
232     if ( !p_connection )
233     {
234         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
235                           dbus_error.message);
236         dbus_error_free( &dbus_error );
237         return NULL;
238     }
239
240     return p_connection;
241 }
242
243 static void poke_screensaver( intf_thread_t *p_intf,
244                               DBusConnection *p_connection )
245 {
246     if( screensaver_is_running( p_connection ) )
247     {
248 #   ifdef SCREENSAVER_DEBUG
249         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
250 #   endif
251         /* gnome-screensaver changed it's D-Bus interface, so we need both */
252         screensaver_send_message_void( p_intf, p_connection, "Poke" );
253         screensaver_send_message_void( p_intf, p_connection,
254                 "SimulateUserActivity" );
255     }
256 #   ifdef SCREENSAVER_DEBUG
257     else
258     {
259         msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
260     }
261 #   endif
262 }
263
264 static void screensaver_send_message_void ( intf_thread_t *p_intf,
265                                        DBusConnection *p_connection,
266                                        const char *psz_name )
267 {
268     DBusMessage *p_message;
269
270     if( !p_connection || !psz_name ) return;
271
272     p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
273                                               GS_INTERFACE, psz_name );
274     if( p_message == NULL )
275     {
276         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
277         return;
278     }
279
280     if( !dbus_connection_send( p_connection, p_message, NULL ) )
281     {
282         msg_Err( p_intf, "DBUS communication failed" );
283     }
284
285     dbus_connection_flush( p_connection );
286
287     dbus_message_unref( p_message );
288 }
289
290 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
291 {
292     DBusError error;
293     vlc_bool_t b_return;
294
295     if( !p_connection ) return VLC_FALSE;
296
297     dbus_error_init( &error );
298     b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
299     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
300
301     return b_return;
302 }
303
304 #endif
305