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