]> git.sesse.net Git - vlc/blob - modules/misc/screensaver.c
90eb44da0d85ce1d04cdbb460d37e57990d82cfe
[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 #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 #ifdef HAVE_DBUS
36
37 #define DBUS_API_SUBJECT_TO_CHANGE
38 #include <dbus/dbus.h>
39
40 #define GS_SERVICE   "org.gnome.ScreenSaver"
41 #define GS_PATH      "/org/gnome/ScreenSaver"
42 #define GS_INTERFACE "org.gnome.ScreenSaver"
43
44 #endif
45
46 /* this is for dbus < 0.3 */
47 #ifndef HAVE_DBUS_1
48 #define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
49 #endif
50
51 /*****************************************************************************
52  * Local prototypes
53  *****************************************************************************/
54 static int  Activate     ( vlc_object_t * );
55 static void  Deactivate   ( vlc_object_t * );
56
57 static void Run          ( intf_thread_t *p_intf );
58
59 #ifdef HAVE_DBUS
60
61 static DBusConnection * dbus_init( intf_thread_t *p_intf );
62 static void poke_screensaver( intf_thread_t *p_intf,
63                               DBusConnection *p_connection );
64 static void screensaver_send_message_void ( intf_thread_t *p_intf,
65                                        DBusConnection *p_connection,
66                                        const char *psz_name );
67 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection );
68
69
70 struct intf_sys_t
71 {
72     DBusConnection *p_connection;
73 };
74
75 #endif
76
77 /*****************************************************************************
78  * Module descriptor
79  *****************************************************************************/
80 vlc_module_begin();
81     set_description( _("X Screensaver disabler") );
82     set_capability( "interface", 0 );
83     set_callbacks( Activate, Deactivate );
84 vlc_module_end();
85
86 /*****************************************************************************
87  * Activate: initialize and create stuff
88  *****************************************************************************/
89 static int Activate( vlc_object_t *p_this )
90 {
91     intf_thread_t *p_intf = (intf_thread_t*)p_this;
92
93     p_intf->pf_run = Run;
94
95 #ifdef HAVE_DBUS
96     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
97     if( !p_intf->p_sys ) return VLC_ENOMEM;
98 #endif
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Deactivate: uninitialize and cleanup
105  *****************************************************************************/
106 static void Deactivate( vlc_object_t *p_this )
107 {
108 #ifdef HAVE_DBUS
109     intf_thread_t *p_intf = (intf_thread_t*)p_this;
110
111     if( p_intf->p_sys->p_connection )
112     {
113 #  ifdef HAVE_DBUS_2
114         dbus_connection_unref( p_intf->p_sys->p_connection );
115 #  else
116         dbus_connection_disconnect( p_intf->p_sys->p_connection );
117 #  endif
118     }
119
120     if( p_intf->p_sys )
121     {
122         free( p_intf->p_sys );
123         p_intf->p_sys = NULL;
124     }
125 #endif
126 }
127
128
129 /*****************************************************************************
130  * Run: main thread
131  *****************************************************************************
132  * This part of the module is in a separate thread so that we do not have
133  * too much system() overhead.
134  *****************************************************************************/
135 static void Run( intf_thread_t *p_intf )
136 {
137     int i_lastcall = 0;
138
139 #ifdef HAVE_DBUS
140     p_intf->p_sys->p_connection = dbus_init( p_intf );
141 #endif
142
143     while( !p_intf->b_die )
144     {
145         msleep( INTF_IDLE_SLEEP*5 ); // 250ms
146
147         /* Check screensaver every 30 seconds */
148         if( ++i_lastcall > 120 )
149         {
150             vlc_object_t *p_vout;
151             p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
152             /* If there is a video output, disable xscreensaver */
153             if( p_vout )
154             {
155                 input_thread_t *p_input = vlc_object_find( p_vout, VLC_OBJECT_INPUT, FIND_PARENT );
156                 vlc_object_release( p_vout );
157                 if( p_input )
158                 {
159                     if( PLAYING_S == p_input->i_state )
160                     {
161                         /* http://www.jwz.org/xscreensaver/faq.html#dvd */
162
163                         system( "xscreensaver-command -deactivate >&- 2>&- &" );
164
165                         /* If we have dbus support, let's communicate directly
166                            with gnome-screensave else, run
167                            gnome-screensaver-command */
168 #ifdef HAVE_DBUS
169                         poke_screensaver( p_intf, p_intf->p_sys->p_connection );
170 #else
171                         system( "gnome-screensaver-command --poke >&- 2>&- &" );
172 #endif
173                         /* FIXME: add support for other screensavers */
174                     }
175                     vlc_object_release( p_input );
176                 }
177             }
178             i_lastcall = 0;
179         }
180     }
181 }
182
183 #ifdef HAVE_DBUS
184
185 static DBusConnection * dbus_init( intf_thread_t *p_intf )
186 {
187     DBusError dbus_error;
188
189     dbus_error_init (&dbus_error);
190     DBusConnection * p_connection = dbus_bus_get( DBUS_BUS_SESSION, &dbus_error );
191
192     if ( !p_connection )
193     {
194         msg_Warn( p_intf, "failed to connect to the D-BUS daemon: %s",
195                           dbus_error.message);
196         dbus_error_free( &dbus_error );
197         return NULL;
198     }
199
200     return p_connection;
201 }
202
203 static void poke_screensaver( intf_thread_t *p_intf,
204                               DBusConnection *p_connection )
205 {
206     if( screensaver_is_running( p_connection ) )
207     {
208 #   ifdef SCREENSAVER_DEBUG
209         msg_Dbg( p_intf, "found a running gnome-screensaver instance" );
210 #   endif
211         /* gnome-screensaver changed it's D-Bus interface, so we need both */
212         screensaver_send_message_void( p_intf, p_connection, "Poke" );
213         screensaver_send_message_void( p_intf, p_connection, 
214                 "SimulateUserActivity" );
215     }
216 #   ifdef SCREENSAVER_DEBUG
217     else
218     {
219         msg_Dbg( p_intf, "found no running gnome-screensaver instance" );
220     }
221 #   endif
222 }
223
224 static void screensaver_send_message_void ( intf_thread_t *p_intf,
225                                        DBusConnection *p_connection,
226                                        const char *psz_name )
227 {
228     DBusMessage *p_message;
229
230     if( !p_connection || !psz_name ) return;
231
232     p_message = dbus_message_new_method_call( GS_SERVICE, GS_PATH,
233                                               GS_INTERFACE, psz_name );
234     if( p_message == NULL )
235     {
236         msg_Err( p_intf, "DBUS initialization failed: message initialization" );
237         return;
238     }
239
240     if( !dbus_connection_send( p_connection, p_message, NULL ) )
241     {
242         msg_Err( p_intf, "DBUS communication failed" );
243     }
244
245     dbus_connection_flush( p_connection );
246
247     dbus_message_unref( p_message );
248 }
249
250 static vlc_bool_t screensaver_is_running( DBusConnection *p_connection )
251 {
252     DBusError error;
253     vlc_bool_t b_return;
254
255     if( !p_connection ) return VLC_FALSE;
256
257     dbus_error_init( &error );
258     b_return = dbus_bus_name_has_owner( p_connection, GS_SERVICE, &error );
259     if( dbus_error_is_set( &error ) ) dbus_error_free (&error);
260
261     return b_return;
262 }
263
264 #endif
265