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