]> git.sesse.net Git - vlc/blob - modules/misc/inhibit.c
Remove useless parameters
[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 void UnInhibit( intf_thread_t *p_intf );
56
57 static int InputChange( vlc_object_t *, const char *,
58                         vlc_value_t, vlc_value_t, void * );
59 static int StateChange( vlc_object_t *, const char *,
60                         vlc_value_t, vlc_value_t, void * );
61
62 struct intf_sys_t
63 {
64     playlist_t      *p_playlist;
65     vlc_object_t    *p_input;
66     DBusConnection  *p_conn;
67     dbus_uint32_t   i_cookie;
68 };
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73 vlc_module_begin ()
74     set_description( N_("Power Management Inhibitor") )
75     set_capability( "interface", 0 )
76     set_callbacks( Activate, Deactivate )
77 vlc_module_end ()
78
79 /*****************************************************************************
80  * Activate: initialize and create stuff
81  *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
83 {
84     intf_thread_t *p_intf = (intf_thread_t*)p_this;
85     intf_sys_t *p_sys;
86     DBusError     error;
87
88     p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
89     if( !p_sys )
90         return VLC_ENOMEM;
91
92     p_sys->i_cookie = 0;
93     p_sys->p_input = NULL;
94
95     dbus_error_init( &error );
96     p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
97     if( !p_sys->p_conn )
98     {
99         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
100                 error.message );
101         dbus_error_free( &error );
102         free( p_sys );
103         return VLC_EGENERIC;
104     }
105
106     p_sys->p_playlist = pl_Get( p_intf );
107     var_AddCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
108     return VLC_SUCCESS;
109 }
110
111 /*****************************************************************************
112  * Deactivate: uninitialize and cleanup
113  *****************************************************************************/
114 static void Deactivate( vlc_object_t *p_this )
115 {
116     intf_thread_t *p_intf = (intf_thread_t*)p_this;
117     intf_sys_t *p_sys = p_intf->p_sys;
118
119     var_DelCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
120
121     if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
122     {
123         var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
124         vlc_object_release( p_sys->p_input );
125     }
126
127     if( p_sys->i_cookie )
128         UnInhibit( p_intf );
129     dbus_connection_unref( p_sys->p_conn );
130
131     free( p_sys );
132 }
133
134 /*****************************************************************************
135  * Inhibit: Notify the power management daemon that it shouldn't suspend
136  * the computer because of inactivity
137  *
138  * returns false if Out of memory, else true
139  *****************************************************************************/
140 static void Inhibit( intf_thread_t *p_intf )
141 {
142     intf_sys_t *p_sys = p_intf->p_sys;
143
144     DBusMessage *msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH,
145                                                      PM_INTERFACE, "Inhibit" );
146     if( unlikely(msg == NULL) )
147         return;
148
149     const char *app = PACKAGE;
150     const char *reason = _("Playing some media.");
151
152     p_sys->i_cookie = 0;
153
154     if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
155                                         DBUS_TYPE_STRING, &reason,
156                                         DBUS_TYPE_INVALID ) )
157     {
158         dbus_message_unref( msg );
159         return;
160     }
161
162     /* blocks 50ms maximum */
163     DBusMessage *reply;
164
165     reply = dbus_connection_send_with_reply_and_block( p_sys->p_conn, msg,
166                                                        50, NULL );
167     dbus_message_unref( msg );
168     if( reply == NULL )
169         /* g-p-m is not active, or too slow. Better luck next time? */
170         return;
171
172     /* extract the cookie from the reply */
173     dbus_uint32_t i_cookie;
174
175     if( dbus_message_get_args( reply, NULL,
176                                DBUS_TYPE_UINT32, &i_cookie,
177                                DBUS_TYPE_INVALID ) )
178         p_sys->i_cookie = i_cookie;
179
180     dbus_message_unref( reply );
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 void UnInhibit( intf_thread_t *p_intf )
189 {
190     intf_sys_t *p_sys = p_intf->p_sys;
191
192     DBusMessage *msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH,
193                                                    PM_INTERFACE, "UnInhibit" );
194     if( unlikely(msg == NULL) )
195         return;
196
197     dbus_uint32_t i_cookie = p_sys->i_cookie;
198     if( dbus_message_append_args( msg, DBUS_TYPE_UINT32, &i_cookie,
199                                        DBUS_TYPE_INVALID )
200      && dbus_connection_send( p_sys->p_conn, msg, NULL ) )
201     {
202         dbus_connection_flush( p_sys->p_conn );
203         p_sys->i_cookie = 0;
204     }
205     dbus_message_unref( msg );
206 }
207
208
209 static int StateChange( vlc_object_t *p_input, const char *var,
210                         vlc_value_t prev, vlc_value_t value, void *data )
211 {
212     intf_thread_t *p_intf = data;
213     const int old = prev.i_int, cur = value.i_int;
214
215     if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
216         return VLC_SUCCESS; /* No interesting change */
217
218     if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
219         return VLC_SUCCESS; /* Already in correct state */
220
221     if( cur == PLAYING_S )
222         Inhibit( p_intf );
223     else
224         UnInhibit( p_intf );
225
226     (void)p_input; (void)var; (void)prev;
227     return VLC_SUCCESS;
228 }
229
230 static int InputChange( vlc_object_t *p_playlist, const char *var,
231                         vlc_value_t prev, vlc_value_t value, void *data )
232 {
233     intf_thread_t *p_intf = data;
234     intf_sys_t *p_sys = p_intf->p_sys;
235
236     if( p_sys->p_input )
237     {
238         var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
239         vlc_object_release( p_sys->p_input );
240     }
241     p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
242     if( p_sys->p_input )
243         var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
244
245     (void)var; (void)prev; (void)value; (void)p_playlist;
246     return VLC_SUCCESS;
247 }