]> git.sesse.net Git - vlc/blob - modules/misc/inhibit.c
vod: removing more unused stuff
[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_Hold( 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     pl_Release( p_intf );
122
123     if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
124     {
125         var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
126         vlc_object_release( p_sys->p_input );
127     }
128
129     if( p_sys->i_cookie )
130         UnInhibit( p_intf );
131     dbus_connection_unref( p_sys->p_conn );
132
133     free( p_sys );
134 }
135
136 /*****************************************************************************
137  * Inhibit: Notify the power management daemon that it shouldn't suspend
138  * the computer because of inactivity
139  *
140  * returns false if Out of memory, else true
141  *****************************************************************************/
142 static int Inhibit( intf_thread_t *p_intf )
143 {
144     DBusConnection *p_conn;
145     DBusMessage *p_msg;
146     DBusMessageIter args;
147     DBusMessage *p_reply;
148     dbus_uint32_t i_cookie;
149
150     p_conn = p_intf->p_sys->p_conn;
151
152     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
153                                           "Inhibit" );
154     if( !p_msg )
155         return false;
156
157     dbus_message_iter_init_append( p_msg, &args );
158
159     char *psz_app = strdup( PACKAGE );
160     if( !psz_app ||
161         !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
162     {
163         free( psz_app );
164         dbus_message_unref( p_msg );
165         return false;
166     }
167     free( psz_app );
168
169     char *psz_inhibit_reason = strdup( _("Playing some media.") );
170     if( !psz_inhibit_reason )
171     {
172         dbus_message_unref( p_msg );
173         return false;
174     }
175     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
176                                          &psz_inhibit_reason ) )
177     {
178         free( psz_inhibit_reason );
179         dbus_message_unref( p_msg );
180         return false;
181     }
182     free( psz_inhibit_reason );
183
184     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
185         50, NULL ); /* blocks 50ms maximum */
186     dbus_message_unref( p_msg );
187     if( p_reply == NULL )
188     {   /* g-p-m is not active, or too slow. Better luck next time? */
189         return true;
190     }
191
192     /* extract the cookie from the reply */
193     if( dbus_message_get_args( p_reply, NULL,
194             DBUS_TYPE_UINT32, &i_cookie,
195             DBUS_TYPE_INVALID ) == FALSE )
196     {
197         return false;
198     }
199
200     /* Save the cookie */
201     p_intf->p_sys->i_cookie = i_cookie;
202     return true;
203 }
204
205 /*****************************************************************************
206  * UnInhibit: Notify the power management daemon that we aren't active anymore
207  *
208  * returns false if Out of memory, else true
209  *****************************************************************************/
210 static int UnInhibit( intf_thread_t *p_intf )
211 {
212     DBusConnection *p_conn;
213     DBusMessage *p_msg;
214     DBusMessageIter args;
215     dbus_uint32_t i_cookie;
216
217     p_conn = p_intf->p_sys->p_conn;
218
219     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
220                                           "UnInhibit" );
221     if( !p_msg )
222         return false;
223
224     dbus_message_iter_init_append( p_msg, &args );
225
226     i_cookie = p_intf->p_sys->i_cookie;
227     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
228     {
229         dbus_message_unref( p_msg );
230         return false;
231     }
232
233     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
234         return false;
235     dbus_connection_flush( p_conn );
236
237     dbus_message_unref( p_msg );
238
239     p_intf->p_sys->i_cookie = 0;
240     return true;
241 }
242
243
244 static int StateChange( vlc_object_t *p_input, const char *var,
245                         vlc_value_t prev, vlc_value_t value, void *data )
246 {
247     intf_thread_t *p_intf = data;
248     const int old = prev.i_int, cur = value.i_int;
249
250     if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
251         return VLC_SUCCESS; /* No interesting change */
252
253     if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
254         return VLC_SUCCESS; /* Already in correct state */
255
256     if( cur == PLAYING_S )
257         Inhibit( p_intf );
258     else
259         UnInhibit( p_intf );
260
261     (void)p_input; (void)var; (void)prev;
262     return VLC_SUCCESS;
263 }
264
265 static int InputChange( vlc_object_t *p_playlist, const char *var,
266                         vlc_value_t prev, vlc_value_t value, void *data )
267 {
268     intf_thread_t *p_intf = data;
269     intf_sys_t *p_sys = p_intf->p_sys;
270
271     if( p_sys->p_input )
272     {
273         var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
274         vlc_object_release( p_sys->p_input );
275     }
276     p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
277     if( p_sys->p_input )
278         var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
279
280     (void)var; (void)prev; (void)value; (void)p_playlist;
281     return VLC_SUCCESS;
282 }