1 /*****************************************************************************
2 * inhibit.c : prevents the computer from suspending when VLC is playing
3 *****************************************************************************
4 * Copyright © 2007 Rafaël Carré
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 /*****************************************************************************
31 *****************************************************************************/
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 /*****************************************************************************
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 * );
65 playlist_t *p_playlist;
66 vlc_object_t *p_input;
67 DBusConnection *p_conn;
68 dbus_uint32_t i_cookie;
71 /*****************************************************************************
73 *****************************************************************************/
75 set_description( N_("Power Management Inhibitor") )
76 set_capability( "interface", 0 )
77 set_callbacks( Activate, Deactivate )
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;
89 p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
94 p_sys->p_input = NULL;
96 dbus_error_init( &error );
97 p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
100 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
102 dbus_error_free( &error );
107 p_sys->p_playlist = pl_Hold( p_intf );
108 var_AddCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
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 );
121 pl_Release( p_intf );
123 if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
125 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
126 vlc_object_release( p_sys->p_input );
129 if( p_sys->i_cookie )
131 dbus_connection_unref( p_sys->p_conn );
136 /*****************************************************************************
137 * Inhibit: Notify the power management daemon that it shouldn't suspend
138 * the computer because of inactivity
140 * returns false if Out of memory, else true
141 *****************************************************************************/
142 static int Inhibit( intf_thread_t *p_intf )
144 DBusConnection *p_conn;
146 DBusMessageIter args;
147 DBusMessage *p_reply;
148 dbus_uint32_t i_cookie;
150 p_conn = p_intf->p_sys->p_conn;
152 p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
157 dbus_message_iter_init_append( p_msg, &args );
159 char *psz_app = strdup( PACKAGE );
161 !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
164 dbus_message_unref( p_msg );
169 char *psz_inhibit_reason = strdup( _("Playing some media.") );
170 if( !psz_inhibit_reason )
172 dbus_message_unref( p_msg );
175 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
176 &psz_inhibit_reason ) )
178 free( psz_inhibit_reason );
179 dbus_message_unref( p_msg );
182 free( psz_inhibit_reason );
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? */
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 )
200 /* Save the cookie */
201 p_intf->p_sys->i_cookie = i_cookie;
205 /*****************************************************************************
206 * UnInhibit: Notify the power management daemon that we aren't active anymore
208 * returns false if Out of memory, else true
209 *****************************************************************************/
210 static int UnInhibit( intf_thread_t *p_intf )
212 DBusConnection *p_conn;
214 DBusMessageIter args;
215 dbus_uint32_t i_cookie;
217 p_conn = p_intf->p_sys->p_conn;
219 p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
224 dbus_message_iter_init_append( p_msg, &args );
226 i_cookie = p_intf->p_sys->i_cookie;
227 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
229 dbus_message_unref( p_msg );
233 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
235 dbus_connection_flush( p_conn );
237 dbus_message_unref( p_msg );
239 p_intf->p_sys->i_cookie = 0;
244 static int StateChange( vlc_object_t *p_input, const char *var,
245 vlc_value_t prev, vlc_value_t value, void *data )
247 intf_thread_t *p_intf = data;
248 const int old = prev.i_int, cur = value.i_int;
250 if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
251 return VLC_SUCCESS; /* No interesting change */
253 if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
254 return VLC_SUCCESS; /* Already in correct state */
256 if( cur == PLAYING_S )
261 (void)p_input; (void)var; (void)prev;
265 static int InputChange( vlc_object_t *p_playlist, const char *var,
266 vlc_value_t prev, vlc_value_t value, void *data )
268 intf_thread_t *p_intf = data;
269 intf_sys_t *p_sys = p_intf->p_sys;
273 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
274 vlc_object_release( p_sys->p_input );
276 p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
278 var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
280 (void)var; (void)prev; (void)value; (void)p_playlist;