]> git.sesse.net Git - vlc/blob - modules/misc/screensaver.c
Remove useless check for (C89) <signal.h>
[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
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_input.h>
36 #include <vlc_interface.h>
37 #include <vlc_aout.h>
38 #include <vlc_vout.h>
39
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <unistd.h>
43 #include <signal.h>
44
45 #ifdef HAVE_DBUS
46
47 #include <dbus/dbus.h>
48
49 #define GS_SERVICE   "org.gnome.ScreenSaver"
50 #define GS_PATH      "/org/gnome/ScreenSaver"
51 #define GS_INTERFACE "org.gnome.ScreenSaver"
52
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 bool 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( N_("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         dbus_connection_unref( p_intf->p_sys->p_connection );
118     }
119
120     free( p_intf->p_sys );
121     p_intf->p_sys = NULL;
122 #endif
123 }
124
125 /*****************************************************************************
126  * Execute: Spawns a process using execv()
127  *****************************************************************************/
128 static void Execute( intf_thread_t *p_this, const char *const *ppsz_args )
129 {
130     pid_t pid = fork();
131     switch( pid )
132     {
133         case 0:     /* we're the child */
134         {
135             sigset_t set;
136             sigemptyset (&set);
137             pthread_sigmask (SIG_SETMASK, &set, NULL);
138
139             /* We don't want output */
140             if( ( freopen( "/dev/null", "w", stdout ) != NULL )
141              && ( freopen( "/dev/null", "w", stderr ) != NULL ) )
142                 execv( ppsz_args[0] , (char *const *)ppsz_args );
143             /* If the file we want to execute doesn't exist we exit() */
144             exit( EXIT_FAILURE );
145         }
146         case -1:    /* we're the error */
147             msg_Dbg( p_this, "Couldn't fork() while launching %s",
148                      ppsz_args[0] );
149             break;
150         default:    /* we're the parent */
151             /* Wait for the child to exit.
152              * We will not deadlock because we ran "/bin/sh &" */
153             while( waitpid( pid, NULL, 0 ) != pid);
154             break;
155     }
156 }
157
158 /*****************************************************************************
159  * Run: main thread
160  *****************************************************************************
161  * This part of the module is in a separate thread so that we do not have
162  * too much system() overhead.
163  *****************************************************************************/
164 static void Run( intf_thread_t *p_intf )
165 {
166     int canc = vlc_savecancel();
167 #ifdef HAVE_DBUS
168     p_intf->p_sys->p_connection = dbus_init( p_intf );
169 #endif
170
171     for( ;; )
172     {
173         vlc_object_t *p_vout;
174
175         p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
176
177         /* If there is a video output, disable xscreensaver */
178         if( p_vout )
179         {
180             input_thread_t *p_input;
181             p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
182             vlc_object_release( p_vout );
183             if( p_input )
184             {
185                 if( PLAYING_S == var_GetInteger( p_input, "state" ) )
186                 {
187                     /* http://www.jwz.org/xscreensaver/faq.html#dvd */
188                     const char *const ppsz_xsargs[] = { "/bin/sh", "-c",
189                             "xscreensaver-command -deactivate &", (char*)NULL };
190                     Execute( p_intf, ppsz_xsargs );
191
192                     /* If we have dbus support, let's communicate directly
193                        with gnome-screensave else, run
194                        gnome-screensaver-command */
195 #ifdef HAVE_DBUS
196                     poke_screensaver( p_intf, p_intf->p_sys->p_connection );
197 #else
198                     const char *const ppsz_gsargs[] = { "/bin/sh", "-c",
199                             "gnome-screensaver-command --poke &", (char*)NULL };
200                     Execute( p_intf, ppsz_gsargs );
201 #endif
202                     /* FIXME: add support for other screensavers */
203                 }
204                 vlc_object_release( p_input );
205             }
206         }
207
208         vlc_restorecancel( canc );
209         /* Check screensaver every 30 seconds */
210         msleep( 30 * CLOCK_FREQ );
211         canc = vlc_savecancel( );
212     }
213 }
214
215 #ifdef HAVE_DBUS
216
217 static DBusConnection * dbus_init( intf_thread_t *p_intf )
218 {
219     DBusError dbus_error;
220
221     dbus_error_init (&dbus_error);
222     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
223
224     if ( !p_connection )
225     {
226         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
227                           dbus_error.message);
228         dbus_error_free( &dbus_error );
229         return NULL;
230     }
231
232     return p_connection;
233 }
234
235 static void poke_screensaver( intf_thread_t *p_intf,
236                               DBusConnection *p_connection )
237 {
238     if( screensaver_is_running( p_connection ) )
239     {
240 #   ifdef SCREENSAVER_DEBUG
241         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
242 #   endif
243         /* gnome-screensaver changed it's D-Bus interface, so we need both */
244         screensaver_send_message_void( p_intf, p_connection, "Poke" );
245         screensaver_send_message_void( p_intf, p_connection,
246                 "SimulateUserActivity" );
247     }
248 #   ifdef SCREENSAVER_DEBUG
249     else
250     {
251         msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
252     }
253 #   endif
254 }
255
256 static void screensaver_send_message_void ( intf_thread_t *p_intf,
257                                        DBusConnection *p_connection,
258                                        const char *psz_name )
259 {
260     DBusMessage *p_message;
261
262     if( !p_connection || !psz_name ) return;
263
264     p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
265                                               GS_INTERFACE, psz_name );
266     if( p_message == NULL )
267     {
268         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
269         return;
270     }
271
272     if( !dbus_connection_send( p_connection, p_message, NULL ) )
273     {
274         msg_Err( p_intf, "DBUS communication failed" );
275     }
276
277     dbus_connection_flush( p_connection );
278
279     dbus_message_unref( p_message );
280 }
281
282 static bool screensaver_is_running( DBusConnection *p_connection )
283 {
284     DBusError error;
285     bool b_return;
286
287     if( !p_connection ) return false;
288
289     dbus_error_init( &error );
290     b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
291     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
292
293     return b_return;
294 }
295
296 #endif
297