]> git.sesse.net Git - vlc/blob - modules/misc/inhibit.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / misc / inhibit.c
1 /*****************************************************************************
2  * inhibit.c : prevents the computer from suspending when VLC is playing
3  *****************************************************************************
4  * Copyright © 2007 Rafaël Carré
5  * $Id$
6  *
7  * Author: Rafaël Carré <funman@videolanorg>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*
25  * Based on freedesktop Power Management Specification version 0.2
26  * http://people.freedesktop.org/~hughsient/temp/power-management-spec-0.2.html
27  */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc/vlc.h>
38 #include <vlc_input.h>
39 #include <vlc_interface.h>
40
41 #include <dbus/dbus.h>
42
43 #define PM_SERVICE   "org.freedesktop.PowerManagement"
44 #define PM_PATH     "/org/freedesktop/PowerManagement/Inhibit"
45 #define PM_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static int  Activate     ( vlc_object_t * );
51 static void Deactivate   ( vlc_object_t * );
52
53 static void Run          ( intf_thread_t *p_intf );
54
55 struct intf_sys_t
56 {
57     DBusConnection  *p_conn;
58     dbus_uint32_t   i_cookie;
59 };
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_description( _("Power Management Inhibitor") );
66     set_capability( "interface", 0 );
67     set_callbacks( Activate, Deactivate );
68 vlc_module_end();
69
70 /*****************************************************************************
71  * Activate: initialize and create stuff
72  *****************************************************************************/
73 static int Activate( vlc_object_t *p_this )
74 {
75     intf_thread_t *p_intf = (intf_thread_t*)p_this;
76     DBusError     error;
77
78
79     p_intf->pf_run = Run;
80
81     p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
82
83     if( !p_intf->p_sys )
84         return VLC_ENOMEM;
85
86     p_intf->p_sys->i_cookie = 0;
87
88     dbus_error_init( &error );
89     p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
90     if( !p_intf->p_sys->p_conn )
91     {
92         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
93                 error.message );
94         dbus_error_free( &error );
95         free( p_intf->p_sys );
96         return VLC_EGENERIC;
97     }
98
99     return VLC_SUCCESS;
100 }
101
102 /*****************************************************************************
103  * Deactivate: uninitialize and cleanup
104  *****************************************************************************/
105 static void Deactivate( vlc_object_t *p_this )
106 {
107     intf_thread_t *p_intf = (intf_thread_t*)p_this;
108     dbus_connection_unref( p_intf->p_sys->p_conn );
109     free( p_intf->p_sys );
110 }
111
112 /*****************************************************************************
113  * Inhibit: Notify the power management daemon that it shouldn't suspend
114  * the computer because of inactivity
115  *
116  * returns false if Out of memory, else true
117  *****************************************************************************/
118 static int Inhibit( intf_thread_t *p_intf )
119 {
120     DBusConnection *p_conn;
121     DBusMessage *p_msg;
122     DBusMessageIter args;
123     DBusMessage *p_reply;
124     DBusError error;
125     dbus_error_init( &error );
126     dbus_uint32_t i_cookie;
127
128     p_conn = p_intf->p_sys->p_conn;
129
130     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
131                                           "Inhibit" );
132     if( !p_msg )
133         return false;
134
135     dbus_message_iter_init_append( p_msg, &args );
136
137     char *psz_app = strdup( PACKAGE );
138     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
139     {
140         free( psz_app );
141         dbus_message_unref( p_msg );
142         return false;
143     }
144     free( psz_app );
145
146     char *psz_inhibit_reason = strdup( "Playing some media." );
147     if( !psz_inhibit_reason )
148     {
149         dbus_message_unref( p_msg );
150         return false;
151     }
152     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
153                                          &psz_inhibit_reason ) )
154     {
155         free( psz_inhibit_reason );
156         dbus_message_unref( p_msg );
157         return false;
158     }
159     free( psz_inhibit_reason );
160
161     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
162         50, &error ); /* blocks 50ms maximum */
163
164     dbus_message_unref( p_msg );
165     if( p_reply == NULL )
166     {   /* g-p-m is not active, or too slow. Better luck next time? */
167         return true;
168     }
169
170     /* extract the cookie from the reply */
171     if( dbus_message_get_args( p_reply, &error,
172             DBUS_TYPE_UINT32, &i_cookie,
173             DBUS_TYPE_INVALID ) == FALSE )
174     {
175         return false;
176     }
177
178     /* Save the cookie */
179     p_intf->p_sys->i_cookie = i_cookie;
180     return true;
181 }
182
183 /*****************************************************************************
184  * UnInhibit: Notify the power management daemon that we aren't active anymore
185  *
186  * returns false if Out of memory, else true
187  *****************************************************************************/
188 static int UnInhibit( intf_thread_t *p_intf )
189 {
190     DBusConnection *p_conn;
191     DBusMessage *p_msg;
192     DBusMessageIter args;
193     DBusError error;
194     dbus_error_init( &error );
195     dbus_uint32_t i_cookie;
196
197     p_conn = p_intf->p_sys->p_conn;
198
199     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
200                                           "UnInhibit" );
201     if( !p_msg )
202         return false;
203
204     dbus_message_iter_init_append( p_msg, &args );
205
206     i_cookie = p_intf->p_sys->i_cookie;
207     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
208     {
209         dbus_message_unref( p_msg );
210         return false;
211     }
212
213     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
214         return false;
215     dbus_connection_flush( p_conn );
216
217     dbus_message_unref( p_msg );
218
219     p_intf->p_sys->i_cookie = 0;
220     return true;
221 }
222
223 /*****************************************************************************
224  * Run: main thread
225  *****************************************************************************/
226 static void Run( intf_thread_t *p_intf )
227 {
228     vlc_object_lock( p_intf );
229     for(;;)
230     {
231         input_thread_t *p_input;
232
233         /* Check playing state every 30 seconds */
234         if( vlc_object_timedwait( p_intf, mdate() + 30000000 ) < 0 )
235             break;
236
237         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
238         if( p_input )
239         {
240             if( PLAYING_S == p_input->i_state && !p_intf->p_sys->i_cookie )
241             {
242                 if( !Inhibit( p_intf ) )
243                 {
244                     vlc_object_release( p_input );
245                     goto end;
246                 }
247             }
248             else if( p_intf->p_sys->i_cookie )
249             {
250                 if( !UnInhibit( p_intf ) )
251                 {
252                     vlc_object_release( p_input );
253                     goto end;
254                 }
255             }
256             vlc_object_release( p_input );
257         }
258         else if( p_intf->p_sys->i_cookie )
259         {
260             if( !UnInhibit( p_intf ) )
261                 goto end;
262         }
263     }
264
265 end:
266     vlc_object_unlock( p_intf );
267 }