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 void UnInhibit( intf_thread_t *p_intf );
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 * );
64 playlist_t *p_playlist;
65 vlc_object_t *p_input;
66 DBusConnection *p_conn;
67 dbus_uint32_t i_cookie;
70 /*****************************************************************************
72 *****************************************************************************/
74 set_description( N_("Power Management Inhibitor") )
75 set_capability( "interface", 0 )
76 set_callbacks( Activate, Deactivate )
79 /*****************************************************************************
80 * Activate: initialize and create stuff
81 *****************************************************************************/
82 static int Activate( vlc_object_t *p_this )
84 intf_thread_t *p_intf = (intf_thread_t*)p_this;
88 p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
93 p_sys->p_input = NULL;
95 dbus_error_init( &error );
96 p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
99 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
101 dbus_error_free( &error );
106 p_sys->p_playlist = pl_Get( p_intf );
107 var_AddCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
111 /*****************************************************************************
112 * Deactivate: uninitialize and cleanup
113 *****************************************************************************/
114 static void Deactivate( vlc_object_t *p_this )
116 intf_thread_t *p_intf = (intf_thread_t*)p_this;
117 intf_sys_t *p_sys = p_intf->p_sys;
119 var_DelCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
121 if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
123 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
124 vlc_object_release( p_sys->p_input );
127 if( p_sys->i_cookie )
129 dbus_connection_unref( p_sys->p_conn );
134 /*****************************************************************************
135 * Inhibit: Notify the power management daemon that it shouldn't suspend
136 * the computer because of inactivity
138 * returns false if Out of memory, else true
139 *****************************************************************************/
140 static void Inhibit( intf_thread_t *p_intf )
142 intf_sys_t *p_sys = p_intf->p_sys;
144 DBusMessage *msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH,
145 PM_INTERFACE, "Inhibit" );
146 if( unlikely(msg == NULL) )
149 const char *app = PACKAGE;
150 const char *reason = _("Playing some media.");
154 if( !dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
155 DBUS_TYPE_STRING, &reason,
156 DBUS_TYPE_INVALID ) )
158 dbus_message_unref( msg );
162 /* blocks 50ms maximum */
165 reply = dbus_connection_send_with_reply_and_block( p_sys->p_conn, msg,
167 dbus_message_unref( msg );
169 /* g-p-m is not active, or too slow. Better luck next time? */
172 /* extract the cookie from the reply */
173 dbus_uint32_t i_cookie;
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;
180 dbus_message_unref( reply );
183 /*****************************************************************************
184 * UnInhibit: Notify the power management daemon that we aren't active anymore
186 * returns false if Out of memory, else true
187 *****************************************************************************/
188 static void UnInhibit( intf_thread_t *p_intf )
190 intf_sys_t *p_sys = p_intf->p_sys;
192 DBusMessage *msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH,
193 PM_INTERFACE, "UnInhibit" );
194 if( unlikely(msg == NULL) )
197 dbus_uint32_t i_cookie = p_sys->i_cookie;
198 if( dbus_message_append_args( msg, DBUS_TYPE_UINT32, &i_cookie,
200 && dbus_connection_send( p_sys->p_conn, msg, NULL ) )
202 dbus_connection_flush( p_sys->p_conn );
205 dbus_message_unref( msg );
209 static int StateChange( vlc_object_t *p_input, const char *var,
210 vlc_value_t prev, vlc_value_t value, void *data )
212 intf_thread_t *p_intf = data;
213 const int old = prev.i_int, cur = value.i_int;
215 if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
216 return VLC_SUCCESS; /* No interesting change */
218 if( ( p_intf->p_sys->i_cookie != 0 ) == ( cur == PLAYING_S ) )
219 return VLC_SUCCESS; /* Already in correct state */
221 if( cur == PLAYING_S )
226 (void)p_input; (void)var; (void)prev;
230 static int InputChange( vlc_object_t *p_playlist, const char *var,
231 vlc_value_t prev, vlc_value_t value, void *data )
233 intf_thread_t *p_intf = data;
234 intf_sys_t *p_sys = p_intf->p_sys;
238 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
239 vlc_object_release( p_sys->p_input );
241 p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
243 var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
245 (void)var; (void)prev; (void)value; (void)p_playlist;