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