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_Get( 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
);
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
)
130 dbus_connection_unref( p_sys
->p_conn
);
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
;
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
,
156 dbus_message_iter_init_append( p_msg
, &args
);
158 char *psz_app
= strdup( PACKAGE
);
160 !dbus_message_iter_append_basic( &args
, DBUS_TYPE_STRING
, &psz_app
) )
163 dbus_message_unref( p_msg
);
168 char *psz_inhibit_reason
= strdup( _("Playing some media.") );
169 if( !psz_inhibit_reason
)
171 dbus_message_unref( p_msg
);
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
);
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? */
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
)
199 /* Save the cookie */
200 p_intf
->p_sys
->i_cookie
= i_cookie
;
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
;
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
,
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
);
232 if( !dbus_connection_send( p_conn
, p_msg
, NULL
) )
234 dbus_connection_flush( p_conn
);
236 dbus_message_unref( p_msg
);
238 p_intf
->p_sys
->i_cookie
= 0;
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
)
260 (void)p_input
; (void)var
; (void)prev
;
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
;
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
));
277 var_AddCallback( p_sys
->p_input
, "state", StateChange
, p_intf
);
279 (void)var
; (void)prev
; (void)value
; (void)p_playlist
;