]> git.sesse.net Git - vlc/blob - modules/misc/inhibit.c
Use var_Inherit* instead of var_CreateGet*.
[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_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_input.h>
40 #include <vlc_interface.h>
41 #include <vlc_playlist.h>
42
43 #include <dbus/dbus.h>
44
45 #define PM_SERVICE   "org.freedesktop.PowerManagement"
46 #define PM_PATH      "/org/freedesktop/PowerManagement/Inhibit"
47 #define PM_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
48
49 /*****************************************************************************
50  * Local prototypes
51  *****************************************************************************/
52 static int  Activate     ( vlc_object_t * );
53 static void Deactivate   ( vlc_object_t * );
54
55 static int Inhibit( intf_thread_t *p_intf );
56 static int UnInhibit( intf_thread_t *p_intf );
57
58 static int InputChange( vlc_object_t *, const char *,
59                         vlc_value_t, vlc_value_t, void * );
60 static int StateChange( vlc_object_t *, const char *,
61                         vlc_value_t, vlc_value_t, void * );
62
63 struct intf_sys_t
64 {
65     playlist_t      *p_playlist;
66     vlc_object_t    *p_input;
67     DBusConnection  *p_conn;
68     dbus_uint32_t   i_cookie;
69 };
70
71 /*****************************************************************************
72  * Module descriptor
73  *****************************************************************************/
74 vlc_module_begin ()
75     set_description( N_("Power Management Inhibitor") )
76     set_capability( "interface", 0 )
77     set_callbacks( Activate, Deactivate )
78 vlc_module_end ()
79
80 /*****************************************************************************
81  * Activate: initialize and create stuff
82  *****************************************************************************/
83 static int Activate( vlc_object_t *p_this )
84 {
85     intf_thread_t *p_intf = (intf_thread_t*)p_this;
86     intf_sys_t *p_sys;
87     DBusError     error;
88
89     p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
90     if( !p_sys )
91         return VLC_ENOMEM;
92
93     p_sys->i_cookie = 0;
94     p_sys->p_input = NULL;
95
96     dbus_error_init( &error );
97     p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
98     if( !p_sys->p_conn )
99     {
100         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
101                 error.message );
102         dbus_error_free( &error );
103         free( p_sys );
104         return VLC_EGENERIC;
105     }
106
107     p_sys->p_playlist = pl_Get( p_intf );
108     var_AddCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
109     return VLC_SUCCESS;
110 }
111
112 /*****************************************************************************
113  * Deactivate: uninitialize and cleanup
114  *****************************************************************************/
115 static void Deactivate( vlc_object_t *p_this )
116 {
117     intf_thread_t *p_intf = (intf_thread_t*)p_this;
118     intf_sys_t *p_sys = p_intf->p_sys;
119
120     var_DelCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
121
122     if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
123     {
124         var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
125         vlc_object_release( p_sys->p_input );
126     }
127
128     if( p_sys->i_cookie )
129         UnInhibit( p_intf );
130     dbus_connection_unref( p_sys->p_conn );
131
132     free( p_sys );
133 }
134
135 /*****************************************************************************
136  * Inhibit: Notify the power management daemon that it shouldn't suspend
137  * the computer because of inactivity
138  *
139  * returns false if Out of memory, else true
140  *****************************************************************************/
141 static int Inhibit( intf_thread_t *p_intf )
142 {
143     DBusConnection *p_conn;
144     DBusMessage *p_msg;
145     DBusMessageIter args;
146     DBusMessage *p_reply;
147     dbus_uint32_t i_cookie;
148
149     p_conn = p_intf->p_sys->p_conn;
150
151     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
152                                           "Inhibit" );
153     if( !p_msg )
154         return false;
155
156     dbus_message_iter_init_append( p_msg, &args );
157
158     char *psz_app = strdup( PACKAGE );
159     if( !psz_app ||
160         !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
161     {
162         free( psz_app );
163         dbus_message_unref( p_msg );
164         return false;
165     }
166     free( psz_app );
167
168     char *psz_inhibit_reason = strdup( _("Playing some media.") );
169     if( !psz_inhibit_reason )
170     {
171         dbus_message_unref( p_msg );
172         return false;
173     }
174     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
175                                          &psz_inhibit_reason ) )
176     {
177         free( psz_inhibit_reason );
178         dbus_message_unref( p_msg );
179         return false;
180     }
181     free( psz_inhibit_reason );
182
183     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
184         50, NULL ); /* blocks 50ms maximum */
185     dbus_message_unref( p_msg );
186     if( p_reply == NULL )
187     {   /* g-p-m is not active, or too slow. Better luck next time? */
188         return true;
189     }
190
191     /* extract the cookie from the reply */
192     if( dbus_message_get_args( p_reply, NULL,
193             DBUS_TYPE_UINT32, &i_cookie,
194             DBUS_TYPE_INVALID ) == FALSE )
195     {
196         return false;
197     }
198
199     /* Save the cookie */
200     p_intf->p_sys->i_cookie = i_cookie;
201     return true;
202 }
203
204 /*****************************************************************************
205  * UnInhibit: Notify the power management daemon that we aren't active anymore
206  *
207  * returns false if Out of memory, else true
208  *****************************************************************************/
209 static int UnInhibit( intf_thread_t *p_intf )
210 {
211     DBusConnection *p_conn;
212     DBusMessage *p_msg;
213     DBusMessageIter args;
214     dbus_uint32_t i_cookie;
215
216     p_conn = p_intf->p_sys->p_conn;
217
218     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
219                                           "UnInhibit" );
220     if( !p_msg )
221         return false;
222
223     dbus_message_iter_init_append( p_msg, &args );
224
225     i_cookie = p_intf->p_sys->i_cookie;
226     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
227     {
228         dbus_message_unref( p_msg );
229         return false;
230     }
231
232     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
233         return false;
234     dbus_connection_flush( p_conn );
235
236     dbus_message_unref( p_msg );
237
238     p_intf->p_sys->i_cookie = 0;
239     return true;
240 }
241
242
243 static int StateChange( vlc_object_t *p_input, const char *var,
244                         vlc_value_t prev, vlc_value_t value, void *data )
245 {
246     intf_thread_t *p_intf = data;
247     const int old = prev.i_int, cur = value.i_int;
248
249     if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
250         return VLC_SUCCESS; /* No interesting change */
251
252     if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
253         return VLC_SUCCESS; /* Already in correct state */
254
255     if( cur == PLAYING_S )
256         Inhibit( p_intf );
257     else
258         UnInhibit( p_intf );
259
260     (void)p_input; (void)var; (void)prev;
261     return VLC_SUCCESS;
262 }
263
264 static int InputChange( vlc_object_t *p_playlist, const char *var,
265                         vlc_value_t prev, vlc_value_t value, void *data )
266 {
267     intf_thread_t *p_intf = data;
268     intf_sys_t *p_sys = p_intf->p_sys;
269
270     if( p_sys->p_input )
271     {
272         var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
273         vlc_object_release( p_sys->p_input );
274     }
275     p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
276     if( p_sys->p_input )
277         var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
278
279     (void)var; (void)prev; (void)value; (void)p_playlist;
280     return VLC_SUCCESS;
281 }