]> git.sesse.net Git - vlc/blob - modules/misc/inhibit.c
Fix logic
[vlc] / modules / misc / inhibit.c
1 /*****************************************************************************
2  * inhibit.c : prevents the computer from suspending when VLC is playing
3  *****************************************************************************
4  * Copyright © 2007 Rafaël Carré
5  * $Id$
6  *
7  * Author: Rafaël Carré <funman@videolanorg>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*
25  * Based on freedesktop Power Management Specification version 0.2
26  * http://people.freedesktop.org/~hughsient/temp/power-management-spec-0.2.html
27  */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_input.h>
40 #include <vlc_interface.h>
41
42 #include <dbus/dbus.h>
43
44 #define PM_SERVICE   "org.freedesktop.PowerManagement"
45 #define PM_PATH      "/org/freedesktop/PowerManagement/Inhibit"
46 #define PM_INTERFACE "org.freedesktop.PowerManagement.Inhibit"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static int  Activate     ( vlc_object_t * );
52 static void Deactivate   ( vlc_object_t * );
53
54 static void Run          ( intf_thread_t *p_intf );
55
56 static int Inhibit( intf_thread_t *p_intf );
57 static int UnInhibit( intf_thread_t *p_intf );
58
59 struct intf_sys_t
60 {
61     DBusConnection  *p_conn;
62     dbus_uint32_t   i_cookie;
63 };
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 vlc_module_begin ()
69     set_description( N_("Power Management Inhibitor") )
70     set_capability( "interface", 0 )
71     set_callbacks( Activate, Deactivate )
72 vlc_module_end ()
73
74 /*****************************************************************************
75  * Activate: initialize and create stuff
76  *****************************************************************************/
77 static int Activate( vlc_object_t *p_this )
78 {
79     intf_thread_t *p_intf = (intf_thread_t*)p_this;
80     DBusError     error;
81
82     p_intf->pf_run = Run;
83     p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
84     if( !p_intf->p_sys )
85         return VLC_ENOMEM;
86
87     p_intf->p_sys->i_cookie = 0;
88
89     dbus_error_init( &error );
90     p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
91     if( !p_intf->p_sys->p_conn )
92     {
93         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
94                 error.message );
95         dbus_error_free( &error );
96         free( p_intf->p_sys );
97         return VLC_EGENERIC;
98     }
99
100     return VLC_SUCCESS;
101 }
102
103 /*****************************************************************************
104  * Deactivate: uninitialize and cleanup
105  *****************************************************************************/
106 static void Deactivate( vlc_object_t *p_this )
107 {
108     intf_thread_t *p_intf = (intf_thread_t*)p_this;
109     if( p_intf->p_sys->i_cookie )
110         UnInhibit( p_intf );
111
112     dbus_connection_unref( p_intf->p_sys->p_conn );
113     free( p_intf->p_sys );
114 }
115
116 /*****************************************************************************
117  * Inhibit: Notify the power management daemon that it shouldn't suspend
118  * the computer because of inactivity
119  *
120  * returns false if Out of memory, else true
121  *****************************************************************************/
122 static int Inhibit( intf_thread_t *p_intf )
123 {
124     DBusConnection *p_conn;
125     DBusMessage *p_msg;
126     DBusMessageIter args;
127     DBusMessage *p_reply;
128     dbus_uint32_t i_cookie;
129
130     p_conn = p_intf->p_sys->p_conn;
131
132     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
133                                           "Inhibit" );
134     if( !p_msg )
135         return false;
136
137     dbus_message_iter_init_append( p_msg, &args );
138
139     char *psz_app = strdup( PACKAGE );
140     if( !psz_app ||
141         !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_app ) )
142     {
143         free( psz_app );
144         dbus_message_unref( p_msg );
145         return false;
146     }
147     free( psz_app );
148
149     char *psz_inhibit_reason = strdup( "Playing some media." );
150     if( !psz_inhibit_reason )
151     {
152         dbus_message_unref( p_msg );
153         return false;
154     }
155     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING,
156                                          &psz_inhibit_reason ) )
157     {
158         free( psz_inhibit_reason );
159         dbus_message_unref( p_msg );
160         return false;
161     }
162     free( psz_inhibit_reason );
163
164     p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
165         50, NULL ); /* blocks 50ms maximum */
166     dbus_message_unref( p_msg );
167     if( p_reply == NULL )
168     {   /* g-p-m is not active, or too slow. Better luck next time? */
169         return true;
170     }
171
172     /* extract the cookie from the reply */
173     if( dbus_message_get_args( p_reply, NULL,
174             DBUS_TYPE_UINT32, &i_cookie,
175             DBUS_TYPE_INVALID ) == FALSE )
176     {
177         return false;
178     }
179
180     /* Save the cookie */
181     p_intf->p_sys->i_cookie = i_cookie;
182     return true;
183 }
184
185 /*****************************************************************************
186  * UnInhibit: Notify the power management daemon that we aren't active anymore
187  *
188  * returns false if Out of memory, else true
189  *****************************************************************************/
190 static int UnInhibit( intf_thread_t *p_intf )
191 {
192     DBusConnection *p_conn;
193     DBusMessage *p_msg;
194     DBusMessageIter args;
195     dbus_uint32_t i_cookie;
196
197     p_conn = p_intf->p_sys->p_conn;
198
199     p_msg = dbus_message_new_method_call( PM_SERVICE, PM_PATH, PM_INTERFACE,
200                                           "UnInhibit" );
201     if( !p_msg )
202         return false;
203
204     dbus_message_iter_init_append( p_msg, &args );
205
206     i_cookie = p_intf->p_sys->i_cookie;
207     if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_cookie ) )
208     {
209         dbus_message_unref( p_msg );
210         return false;
211     }
212
213     if( !dbus_connection_send( p_conn, p_msg, NULL ) )
214         return false;
215     dbus_connection_flush( p_conn );
216
217     dbus_message_unref( p_msg );
218
219     p_intf->p_sys->i_cookie = 0;
220     return true;
221 }
222
223 /*****************************************************************************
224  * Run: main thread
225  *****************************************************************************/
226 static void Run( intf_thread_t *p_intf )
227 {
228     for( ;; )
229     {
230         input_thread_t *p_input;
231
232         /* Check playing state every 30 seconds */
233         msleep( 30 * CLOCK_FREQ );
234
235         p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_ANYWHERE );
236         if( p_input )
237         {
238             const int i_state = var_GetInteger( p_input, "state" );
239             vlc_object_release( p_input );
240
241             if( PLAYING_S == i_state )
242             {
243                if( !p_intf->p_sys->i_cookie )
244                {
245                    if( !Inhibit( p_intf ) )
246                        break;
247                }
248             }
249             else if( p_intf->p_sys->i_cookie )
250             {
251                 if( !UnInhibit( p_intf ) )
252                     break;
253             }
254         }
255         else if( p_intf->p_sys->i_cookie )
256         {
257             if( !UnInhibit( p_intf ) )
258                 break;
259         }
260     }
261 }