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>
46 FREEDESKTOP = 0, /* as used by KDE and gnome <= 2.26 */
47 GNOME = 1, /* as used by gnome > 2.26 */
50 static const char *dbus_service[] = {
51 [FREEDESKTOP] = "org.freedesktop.PowerManagement",
52 [GNOME] = "org.gnome.SessionManager",
55 static const char *dbus_path[] = {
56 [FREEDESKTOP] = "/org/freedesktop/PowerManagement",
57 [GNOME] = "/org/gnome/SessionManager",
60 static const char *dbus_interface[] = {
61 [FREEDESKTOP] = "org.freedesktop.PowerManagement.Inhibit",
62 [GNOME] = "org.gnome.SessionManager",
65 static const char *dbus_uninhibit_method[] = {
66 [FREEDESKTOP] = "UnInhibit",
67 [GNOME] = "Uninhibit",
71 /*****************************************************************************
73 !*****************************************************************************/
74 static int Activate ( vlc_object_t * );
75 static void Deactivate ( vlc_object_t * );
77 static void UnInhibit( intf_thread_t *p_intf, int type );
79 static int InputChange( vlc_object_t *, const char *,
80 vlc_value_t, vlc_value_t, void * );
81 static int StateChange( vlc_object_t *, const char *,
82 vlc_value_t, vlc_value_t, void * );
86 playlist_t *p_playlist;
87 vlc_object_t *p_input;
88 DBusConnection *p_conn;
89 dbus_uint32_t i_cookie[2];
92 /*****************************************************************************
94 *****************************************************************************/
96 set_description( N_("Power Management Inhibitor") )
97 set_capability( "interface", 0 )
98 set_callbacks( Activate, Deactivate )
101 /*****************************************************************************
102 * Activate: initialize and create stuff
103 *****************************************************************************/
104 static int Activate( vlc_object_t *p_this )
106 intf_thread_t *p_intf = (intf_thread_t*)p_this;
110 p_sys = p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
114 p_sys->i_cookie[FREEDESKTOP] = 0;
115 p_sys->i_cookie[GNOME] = 0;
116 p_sys->p_input = NULL;
118 dbus_error_init( &error );
120 /* connect privately to the session bus
121 * the connection will not be shared with other vlc modules which use dbus,
122 * thus avoiding a whole class of concurrency issues */
123 p_sys->p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
126 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
128 dbus_error_free( &error );
133 p_sys->p_playlist = pl_Get( p_intf );
134 var_AddCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
138 /*****************************************************************************
139 * Deactivate: uninitialize and cleanup
140 *****************************************************************************/
141 static void Deactivate( vlc_object_t *p_this )
143 intf_thread_t *p_intf = (intf_thread_t*)p_this;
144 intf_sys_t *p_sys = p_intf->p_sys;
146 var_DelCallback( p_sys->p_playlist, "item-current", InputChange, p_intf );
148 if( p_sys->p_input ) /* Do delete "state" after "item-changed"! */
150 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
151 vlc_object_release( p_sys->p_input );
154 if( p_sys->i_cookie[FREEDESKTOP] )
155 UnInhibit( p_intf, FREEDESKTOP );
156 if( p_sys->i_cookie[GNOME] )
157 UnInhibit( p_intf, GNOME );
159 /* The dbus connection is private,
160 * so we are responsible for closing it */
161 dbus_connection_close( p_sys->p_conn );
162 dbus_connection_unref( p_sys->p_conn );
167 /*****************************************************************************
168 * Inhibit: Notify the power management daemon that it shouldn't suspend
169 * the computer because of inactivity
170 *****************************************************************************/
171 static void Inhibit( intf_thread_t *p_intf, int type )
173 intf_sys_t *p_sys = p_intf->p_sys;
175 DBusMessage *msg = dbus_message_new_method_call(
176 dbus_service[type], dbus_path[type], dbus_interface[type], "Inhibit" );
177 if( unlikely(msg == NULL) )
180 const char *app = PACKAGE;
181 const char *reason = _("Playing some media.");
183 p_sys->i_cookie[type] = 0;
186 dbus_uint32_t xid = 0; // FIXME?
187 dbus_uint32_t flags = 8 /* Inhibit suspending the session or computer */
188 | 4;/* Inhibit the session being marked as idle */
191 ret = dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
192 DBUS_TYPE_STRING, &reason,
197 ret = dbus_message_append_args( msg, DBUS_TYPE_STRING, &app,
198 DBUS_TYPE_UINT32, &xid,
199 DBUS_TYPE_STRING, &reason,
200 DBUS_TYPE_UINT32, &flags,
207 dbus_message_unref( msg );
211 /* blocks 50ms maximum */
214 reply = dbus_connection_send_with_reply_and_block( p_sys->p_conn, msg,
216 dbus_message_unref( msg );
218 /* g-p-m is not active, or too slow. Better luck next time? */
221 /* extract the cookie from the reply */
222 dbus_uint32_t i_cookie;
224 if( dbus_message_get_args( reply, NULL,
225 DBUS_TYPE_UINT32, &i_cookie,
226 DBUS_TYPE_INVALID ) )
227 p_sys->i_cookie[type] = i_cookie;
229 dbus_message_unref( reply );
232 /*****************************************************************************
233 * UnInhibit: Notify the power management daemon that we aren't active anymore
234 *****************************************************************************/
235 static void UnInhibit( intf_thread_t *p_intf, int type )
237 intf_sys_t *p_sys = p_intf->p_sys;
239 DBusMessage *msg = dbus_message_new_method_call( dbus_service[type],
240 dbus_path[type], dbus_interface[type], dbus_uninhibit_method[type] );
241 if( unlikely(msg == NULL) )
244 dbus_uint32_t i_cookie = p_sys->i_cookie[type];
245 if( dbus_message_append_args( msg, DBUS_TYPE_UINT32, &i_cookie,
247 && dbus_connection_send( p_sys->p_conn, msg, NULL ) )
249 dbus_connection_flush( p_sys->p_conn );
250 p_sys->i_cookie[type] = 0;
252 dbus_message_unref( msg );
256 static int StateChange( vlc_object_t *p_input, const char *var,
257 vlc_value_t prev, vlc_value_t value, void *data )
259 intf_thread_t *p_intf = data;
260 intf_sys_t *p_sys = p_intf->p_sys;
261 const int old = prev.i_int, cur = value.i_int;
263 if( ( old == PLAYING_S ) == ( cur == PLAYING_S ) )
264 return VLC_SUCCESS; /* No interesting change */
266 if( cur == PLAYING_S ) {
267 if (p_sys->i_cookie[FREEDESKTOP] == 0)
268 Inhibit( p_intf, FREEDESKTOP );
269 if (p_sys->i_cookie[GNOME] == 0)
270 Inhibit( p_intf, GNOME );
273 if (p_sys->i_cookie[FREEDESKTOP] != 0)
274 UnInhibit( p_intf, FREEDESKTOP );
275 if (p_sys->i_cookie[GNOME] != 0)
276 UnInhibit( p_intf, GNOME );
279 (void)p_input; (void)var; (void)prev;
283 static int InputChange( vlc_object_t *p_playlist, const char *var,
284 vlc_value_t prev, vlc_value_t value, void *data )
286 intf_thread_t *p_intf = data;
287 intf_sys_t *p_sys = p_intf->p_sys;
291 var_DelCallback( p_sys->p_input, "state", StateChange, p_intf );
292 vlc_object_release( p_sys->p_input );
294 p_sys->p_input = VLC_OBJECT(playlist_CurrentInput( p_sys->p_playlist ));
297 Inhibit( p_intf, FREEDESKTOP );
298 Inhibit( p_intf, GNOME );
300 var_AddCallback( p_sys->p_input, "state", StateChange, p_intf );
303 (void)var; (void)prev; (void)value; (void)p_playlist;