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>
42 #include <dbus/dbus.h>
44 #define PM_SERVICE "org.freedesktop.PowerManagement"
45 #define PM_PATH "/org/freedesktop/PowerManagement/Inhibit"
46 #define PM_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Activate ( vlc_object_t * );
52 static void Deactivate ( vlc_object_t * );
54 static void Run ( intf_thread_t *p_intf );
58 DBusConnection *p_conn;
59 dbus_uint32_t i_cookie;
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( N_("Power Management Inhibitor") );
67 set_capability( "interface", 0 );
68 set_callbacks( Activate, Deactivate );
71 /*****************************************************************************
72 * Activate: initialize and create stuff
73 *****************************************************************************/
74 static int Activate( vlc_object_t *p_this )
76 intf_thread_t *p_intf = (intf_thread_t*)p_this;
80 p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
84 p_intf->p_sys->i_cookie = 0;
86 dbus_error_init( &error );
87 p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
88 if( !p_intf->p_sys->p_conn )
90 msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
92 dbus_error_free( &error );
93 free( p_intf->p_sys );
100 /*****************************************************************************
101 * Deactivate: uninitialize and cleanup
102 *****************************************************************************/
103 static void Deactivate( vlc_object_t *p_this )
105 intf_thread_t *p_intf = (intf_thread_t*)p_this;
106 dbus_connection_unref( p_intf->p_sys->p_conn );
107 free( p_intf->p_sys );
110 /*****************************************************************************
111 * Inhibit: Notify the power management daemon that it shouldn't suspend
112 * the computer because of inactivity
114 * returns false if Out of memory, else true
115 *****************************************************************************/
116 static int Inhibit( intf_thread_t *p_intf )
118 DBusConnection *p_conn;
120 DBusMessageIter args;
121 DBusMessage *p_reply;
122 dbus_uint32_t i_cookie;
124 p_conn = p_intf->p_sys->p_conn;
126 p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
131 dbus_message_iter_init_append( p_msg, &args );
133 char *psz_app = strdup( PACKAGE );
135 !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
138 dbus_message_unref( p_msg );
143 char *psz_inhibit_reason = strdup( "Playing some media." );
144 if( !psz_inhibit_reason )
146 dbus_message_unref( p_msg );
149 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
150 &psz_inhibit_reason ) )
152 free( psz_inhibit_reason );
153 dbus_message_unref( p_msg );
156 free( psz_inhibit_reason );
158 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
159 50, NULL ); /* blocks 50ms maximum */
160 dbus_message_unref( p_msg );
161 if( p_reply == NULL )
162 { /* g-p-m is not active, or too slow. Better luck next time? */
166 /* extract the cookie from the reply */
167 if( dbus_message_get_args( p_reply, NULL,
168 DBUS_TYPE_UINT32, &i_cookie,
169 DBUS_TYPE_INVALID ) == FALSE )
174 /* Save the cookie */
175 p_intf->p_sys->i_cookie = i_cookie;
179 /*****************************************************************************
180 * UnInhibit: Notify the power management daemon that we aren't active anymore
182 * returns false if Out of memory, else true
183 *****************************************************************************/
184 static int UnInhibit( intf_thread_t *p_intf )
186 DBusConnection *p_conn;
188 DBusMessageIter args;
189 dbus_uint32_t i_cookie;
191 p_conn = p_intf->p_sys->p_conn;
193 p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
198 dbus_message_iter_init_append( p_msg, &args );
200 i_cookie = p_intf->p_sys->i_cookie;
201 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
203 dbus_message_unref( p_msg );
207 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
209 dbus_connection_flush( p_conn );
211 dbus_message_unref( p_msg );
213 p_intf->p_sys->i_cookie = 0;
217 /*****************************************************************************
219 *****************************************************************************/
220 static void Run( intf_thread_t *p_intf )
222 vlc_object_lock( p_intf );
223 while( vlc_object_alive( p_intf ) )
225 input_thread_t *p_input;
227 /* Check playing state every 30 seconds */
228 vlc_object_timedwait( p_intf, mdate() + 30000000 );
230 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
233 if( PLAYING_S == p_input->i_state && !p_intf->p_sys->i_cookie )
235 if( !Inhibit( p_intf ) )
237 vlc_object_release( p_input );
241 else if( p_intf->p_sys->i_cookie )
243 if( !UnInhibit( p_intf ) )
245 vlc_object_release( p_input );
249 vlc_object_release( p_input );
251 else if( p_intf->p_sys->i_cookie )
253 if( !UnInhibit( p_intf ) )
259 vlc_object_unlock( p_intf );