Clarify live555 version error
[vlc/asuraparaju-public.git] / modules / misc / inhibit.c
blob31d1f41959d3b02afab309d306953c6690d213df
1 /*****************************************************************************
2 * inhibit.c : prevents the computer from suspending when VLC is playing
3 *****************************************************************************
4 * Copyright © 2007 Rafaël Carré
5 * $Id$
7 * Author: Rafaël Carré <funman@videolanorg>
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.
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.
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 *****************************************************************************/
25 * Based on freedesktop Power Management Specification version 0.2
26 * http://people.freedesktop.org/~hughsient/temp/power-management-spec-0.2.html
29 /*****************************************************************************
30 * Preamble
31 *****************************************************************************/
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
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>
43 #include <dbus/dbus.h>
45 #define PM_SERVICE "org.freedesktop.PowerManagement"
46 #define PM_PATH "/org/freedesktop/PowerManagement/Inhibit"
47 #define PM_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
49 /*****************************************************************************
50 * Local prototypes
51 *****************************************************************************/
52 static int Activate ( vlc_object_t * );
53 static void Deactivate ( vlc_object_t * );
55 static int Inhibit( intf_thread_t *p_intf );
56 static int UnInhibit( intf_thread_t *p_intf );
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 * );
63 struct intf_sys_t
65 playlist_t *p_playlist;
66 vlc_object_t *p_input;
67 DBusConnection *p_conn;
68 dbus_uint32_t i_cookie;
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 ()
80 /*****************************************************************************
81 * Activate: initialize and create stuff
82 *****************************************************************************/
83 static int Activate( vlc_object_t *p_this )
85 intf_thread_t *p_intf = (intf_thread_t*)p_this;
86 intf_sys_t *p_sys;
87 DBusError error;
89 p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
90 if( !p_sys )
91 return VLC_ENOMEM;
93 p_sys->i_cookie = 0;
94 p_sys->p_input = NULL;
96 dbus_error_init( &error );
97 p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
98 if( !p_sys->p_conn )
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;
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;
112 /*****************************************************************************
113 * Deactivate: uninitialize and cleanup
114 *****************************************************************************/
115 static void Deactivate( vlc_object_t *p_this )
117 intf_thread_t *p_intf = (intf_thread_t*)p_this;
118 intf_sys_t *p_sys = p_intf->p_sys;
120 var_DelCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
122 if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
124 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
125 vlc_object_release( p_sys->p_input );
128 if( p_sys->i_cookie )
129 UnInhibit( p_intf );
130 dbus_connection_unref( p_sys->p_conn );
132 free( p_sys );
135 /*****************************************************************************
136 * Inhibit: Notify the power management daemon that it shouldn't suspend
137 * the computer because of inactivity
139 * returns false if Out of memory, else true
140 *****************************************************************************/
141 static int Inhibit( intf_thread_t *p_intf )
143 DBusConnection *p_conn;
144 DBusMessage *p_msg;
145 DBusMessageIter args;
146 DBusMessage *p_reply;
147 dbus_uint32_t i_cookie;
149 p_conn = p_intf->p_sys->p_conn;
151 p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
152 "Inhibit" );
153 if( !p_msg )
154 return false;
156 dbus_message_iter_init_append( p_msg, &args );
158 char *psz_app = strdup( PACKAGE );
159 if( !psz_app ||
160 !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
162 free( psz_app );
163 dbus_message_unref( p_msg );
164 return false;
166 free( psz_app );
168 char *psz_inhibit_reason = strdup( _("Playing some media.") );
169 if( !psz_inhibit_reason )
171 dbus_message_unref( p_msg );
172 return false;
174 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
175 &psz_inhibit_reason ) )
177 free( psz_inhibit_reason );
178 dbus_message_unref( p_msg );
179 return false;
181 free( psz_inhibit_reason );
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;
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 )
196 return false;
199 /* Save the cookie */
200 p_intf->p_sys->i_cookie = i_cookie;
201 return true;
204 /*****************************************************************************
205 * UnInhibit: Notify the power management daemon that we aren't active anymore
207 * returns false if Out of memory, else true
208 *****************************************************************************/
209 static int UnInhibit( intf_thread_t *p_intf )
211 DBusConnection *p_conn;
212 DBusMessage *p_msg;
213 DBusMessageIter args;
214 dbus_uint32_t i_cookie;
216 p_conn = p_intf->p_sys->p_conn;
218 p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
219 "UnInhibit" );
220 if( !p_msg )
221 return false;
223 dbus_message_iter_init_append( p_msg, &args );
225 i_cookie = p_intf->p_sys->i_cookie;
226 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
228 dbus_message_unref( p_msg );
229 return false;
232 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
233 return false;
234 dbus_connection_flush( p_conn );
236 dbus_message_unref( p_msg );
238 p_intf->p_sys->i_cookie = 0;
239 return true;
243 static int StateChange( vlc_object_t *p_input, const char *var,
244 vlc_value_t prev, vlc_value_t value, void *data )
246 intf_thread_t *p_intf = data;
247 const int old = prev.i_int, cur = value.i_int;
249 if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
250 return VLC_SUCCESS; /* No interesting change */
252 if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
253 return VLC_SUCCESS; /* Already in correct state */
255 if( cur == PLAYING_S )
256 Inhibit( p_intf );
257 else
258 UnInhibit( p_intf );
260 (void)p_input; (void)var; (void)prev;
261 return VLC_SUCCESS;
264 static int InputChange( vlc_object_t *p_playlist, const char *var,
265 vlc_value_t prev, vlc_value_t value, void *data )
267 intf_thread_t *p_intf = data;
268 intf_sys_t *p_sys = p_intf->p_sys;
270 if( p_sys->p_input )
272 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
273 vlc_object_release( p_sys->p_input );
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 );
279 (void)var; (void)prev; (void)value; (void)p_playlist;
280 return VLC_SUCCESS;