]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Implement Volume control
[vlc] / modules / control / dbus.c
1 /*****************************************************************************
2  * dbus.c : D-Bus control interface
3  *****************************************************************************
4  * Copyright (C) 2006 Rafaël Carré
5  * $Id$
6  *
7  * Author:    Rafaël Carré <funman at 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  * D-Bus Specification: 
26  *      http://dbus.freedesktop.org/doc/dbus-specification.html
27  * D-Bus low-level C API (libdbus)
28  *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
29  */
30
31 /*
32  * TODO:
33  *  properties ?
34  *
35  *  macros to read incoming arguments
36  *
37  *  explore different possible types (arrays..)
38  *
39  *  what must we do if org.videolan.vlc already exist on the bus ?
40  *  ( there is more than one vlc instance )
41  */
42
43 /*****************************************************************************
44  * Preamble
45  *****************************************************************************/
46
47 #include <dbus/dbus.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51
52 #include <sys/types.h> /* getpid() */
53 #include <unistd.h> /* getpid() */
54
55 #include "dbus.h"
56
57 #include <vlc/vlc.h>
58 #include <vlc/aout.h>
59 #include <vlc/intf.h>
60 #include <vlc_meta.h>
61 #include <vlc_input.h>
62
63 /*****************************************************************************
64  * Local prototypes.
65  *****************************************************************************/
66
67 static int  Open    ( vlc_object_t * );
68 static void Close   ( vlc_object_t * );
69 static void Run        ( intf_thread_t * );
70
71
72 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
73                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
74
75 struct intf_sys_t
76 {
77     DBusConnection *p_conn;
78 };
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83
84 vlc_module_begin();
85     set_shortname( _("dbus"));
86     set_category( CAT_INTERFACE );
87     set_subcategory( SUBCAT_INTERFACE_CONTROL );
88     set_description( _("D-Bus control interface") );
89     set_capability( "interface", 0 );
90     set_callbacks( Open, Close );
91 vlc_module_end();
92
93 /*****************************************************************************
94  * Methods
95  *****************************************************************************/
96
97 DBUS_METHOD( Nothing )
98 { /* do nothing */
99     REPLY_INIT;
100     REPLY_SEND;
101 }
102
103 DBUS_METHOD( Quit )
104 { /* exits vlc */
105     REPLY_INIT;
106     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
107     playlist_Stop( p_playlist );
108     pl_Release( ((vlc_object_t*) p_this) );
109     ((vlc_object_t*)p_this)->p_libvlc->b_die = VLC_TRUE;
110     REPLY_SEND;
111 }
112
113 DBUS_METHOD( VolumeGet )
114 { /* get volume in percentage */
115     REPLY_INIT;
116     OUT_ARGUMENTS;
117     dbus_uint16_t i_vol;
118     /* 2nd argument of aout_VolumeGet is uint16 */
119     aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
120     i_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
121     ADD_UINT16( &i_vol );
122     REPLY_SEND;
123 }
124
125 DBUS_METHOD( VolumeSet )
126 { /* set volume in percentage */
127     REPLY_INIT;
128
129     DBusError error;
130     dbus_error_init( &error );
131
132     dbus_uint16_t i_vol;
133
134     dbus_message_get_args( p_from, &error,
135             DBUS_TYPE_UINT16, &i_vol,
136             DBUS_TYPE_INVALID );
137
138     if( dbus_error_is_set( &error ) )
139     {
140         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
141                 error.message );
142         dbus_error_free( &error );
143         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
144     }
145
146     aout_VolumeSet( (vlc_object_t*) p_this, ( AOUT_VOLUME_MAX / 100 ) * i_vol );
147
148     REPLY_SEND;
149 }
150
151 DBUS_METHOD( Next )
152 { /* next playlist item */
153     REPLY_INIT;
154     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
155     playlist_Next( p_playlist );
156     pl_Release( ((vlc_object_t*) p_this) );
157     REPLY_SEND;
158 }
159
160 DBUS_METHOD( Prev )
161 { /* previous playlist item */
162     REPLY_INIT;
163     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
164     playlist_Prev( p_playlist );
165     pl_Release( ((vlc_object_t*) p_this) );
166     REPLY_SEND;
167 }
168
169 DBUS_METHOD( Stop )
170 { /* stop playing */
171     REPLY_INIT;
172     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
173     playlist_Stop( p_playlist );
174     pl_Release( ((vlc_object_t*) p_this) );
175     REPLY_SEND;
176 }
177
178 DBUS_METHOD( GetPlayingItem )
179 { /* return the current item */
180     REPLY_INIT;
181     OUT_ARGUMENTS;
182     char psz_no_input = '\0';
183     char *p_psz_no_input = &psz_no_input;
184     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
185     input_thread_t *p_input = p_playlist->p_input;
186     ADD_STRING( ( p_input ) ? &p_input->input.p_item->psz_name :
187             &p_psz_no_input );
188     pl_Release( ((vlc_object_t*) p_this) );
189     REPLY_SEND;
190 }
191
192 DBUS_METHOD( GetPlayStatus )
193 { /* return a string */
194     REPLY_INIT;
195     OUT_ARGUMENTS;
196
197     char *psz_play;
198     vlc_value_t val;
199     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
200     input_thread_t *p_input = p_playlist->p_input;
201
202     if( !p_input )
203         psz_play = strdup( "stopped" );
204     else
205     {
206         var_Get( p_input, "state", &val );
207         if( val.i_int == PAUSE_S )
208             psz_play = strdup( "pause" );
209         else if( val.i_int == PLAYING_S )
210             psz_play = strdup( "playing" );
211         else psz_play = strdup( "unknown" );
212     }
213     
214     pl_Release( p_playlist );
215
216     ADD_STRING( &psz_play );
217     free( psz_play );
218     REPLY_SEND;
219 }
220
221 DBUS_METHOD( TogglePause )
222 { /* return a bool: true if playing */
223     REPLY_INIT;
224     OUT_ARGUMENTS;
225
226     vlc_value_t val;
227     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
228     input_thread_t *p_input = p_playlist->p_input;
229     if( p_input != NULL )
230     {
231         var_Get( p_input, "state", &val );
232         if( val.i_int != PAUSE_S )
233         {
234             val.i_int = PAUSE_S;
235             playlist_Pause( p_playlist );
236         }
237         else
238         {
239             val.i_int = PLAYING_S;
240             playlist_Play( p_playlist );
241         }
242     }
243     else
244     {
245         val.i_int = PLAYING_S;
246         playlist_Play( p_playlist );
247     }
248     pl_Release( p_playlist );
249
250     dbus_bool_t pause = ( val.i_int == PLAYING_S ) ? TRUE : FALSE;
251     ADD_BOOL( &pause );
252     REPLY_SEND;
253 }
254
255 DBUS_SIGNAL( NewInstance )
256 { /* emits a signal with vlc pid */
257     SIGNAL_INIT( "NewInstance" );
258     OUT_ARGUMENTS;
259     dbus_uint32_t i_pid = (dbus_uint32_t) getpid();
260     ADD_UINT32( &i_pid );
261     SIGNAL_SEND;
262 }
263
264 DBUS_METHOD( AddMRL )
265 { /* add the string to the playlist, and play it if the boolean is true */
266     REPLY_INIT;
267
268     DBusError error;
269     dbus_error_init( &error );
270
271     char *psz_mrl;
272     dbus_bool_t b_play;
273
274     dbus_message_get_args( p_from, &error,
275             DBUS_TYPE_STRING, &psz_mrl,
276             DBUS_TYPE_BOOLEAN, &b_play,
277             DBUS_TYPE_INVALID );
278
279     if( dbus_error_is_set( &error ) )
280     {
281         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
282                 error.message );
283         dbus_error_free( &error );
284         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
285     }
286
287     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
288     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
289             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , PLAYLIST_END, VLC_TRUE );
290     pl_Release( p_playlist );
291
292     REPLY_SEND;
293 }
294
295 /*****************************************************************************
296  * Introspection method
297  *****************************************************************************/
298
299 DBUS_METHOD( handle_introspect )
300 { /* handles introspection of /org/videolan/vlc */
301     REPLY_INIT;
302     OUT_ARGUMENTS;
303     ADD_STRING( &psz_introspection_xml_data );
304     REPLY_SEND;
305 }
306
307 /*****************************************************************************
308  * handle_messages: answer to incoming messages
309  *****************************************************************************/
310
311 #define METHOD_FUNC( method, function ) \
312     else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
313         return function( p_conn, p_from, p_this )
314
315 DBUS_METHOD( handle_messages )
316 { /* the main handler, that call methods */
317
318     if( dbus_message_is_method_call( p_from,
319                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
320         return handle_introspect( p_conn, p_from, p_this );
321
322     /* here D-Bus method's names are associated to an handler */
323
324     METHOD_FUNC( "GetPlayStatus",   GetPlayStatus );
325     METHOD_FUNC( "GetPlayingItem",  GetPlayingItem );
326     METHOD_FUNC( "AddMRL",          AddMRL );
327     METHOD_FUNC( "TogglePause",     TogglePause );
328     METHOD_FUNC( "Nothing",         Nothing );
329     METHOD_FUNC( "Prev",            Prev );
330     METHOD_FUNC( "Next",            Next );
331     METHOD_FUNC( "Quit",            Quit );
332     METHOD_FUNC( "Stop",            Stop );
333     METHOD_FUNC( "VolumeSet",       VolumeSet );
334     METHOD_FUNC( "VolumeGet",       VolumeGet );
335
336     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
337 }
338
339 /*****************************************************************************
340  * Open: initialize interface
341  *****************************************************************************/
342
343 static int Open( vlc_object_t *p_this )
344 { /* initialisation of the connection */
345     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
346     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
347     playlist_t      *p_playlist;
348     DBusConnection  *p_conn;
349     DBusError       error;
350
351     if( !p_sys )
352         return VLC_ENOMEM;
353
354     dbus_threads_init_default();
355     
356     dbus_error_init( &error );
357     
358     /* connect to the session bus */
359     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
360     if( !p_conn )
361     {
362         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
363                 error.message );
364         dbus_error_free( &error );
365         free( p_sys );
366         return VLC_EGENERIC;
367     }
368
369     /* we unregister the object /, registered by libvlc */
370     dbus_connection_unregister_object_path( p_conn, "/" );
371
372     /* we register the object /org/videolan/vlc */
373     dbus_connection_register_object_path( p_conn, VLC_DBUS_OBJECT_PATH,
374             &vlc_dbus_vtable, p_this );
375
376     dbus_connection_flush( p_conn );
377
378     p_playlist = pl_Yield( p_intf );
379     PL_LOCK;
380     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
381     PL_UNLOCK;
382     pl_Release( p_playlist );
383
384     p_intf->pf_run = Run;
385     p_intf->p_sys = p_sys;
386     p_sys->p_conn = p_conn;
387
388     return VLC_SUCCESS;
389 }
390
391 /*****************************************************************************
392  * Close: destroy interface
393  *****************************************************************************/
394
395 static void Close   ( vlc_object_t *p_this )
396 {
397     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
398     playlist_t      *p_playlist = pl_Yield( p_intf );;
399
400     PL_LOCK;
401     var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
402     PL_UNLOCK;
403     pl_Release( p_playlist );
404
405     free( p_intf->p_sys );
406 }
407
408 /*****************************************************************************
409  * Run: main loop    
410  *****************************************************************************/
411
412 static void Run          ( intf_thread_t *p_intf )
413 {
414     NewInstance( p_intf->p_sys->p_conn, NULL );
415
416     while( !p_intf->b_die )
417     {
418         msleep( INTF_IDLE_SLEEP );
419         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
420     }
421 }
422
423 /*****************************************************************************
424  * ItemChange: Playlist item change callback
425  *****************************************************************************/
426
427 DBUS_SIGNAL( ItemChangeSignal )
428 { /* emit the name of the new item */
429     SIGNAL_INIT( "ItemChange" );
430     OUT_ARGUMENTS;
431
432     input_thread_t *p_input = (input_thread_t*) p_data;
433     ADD_STRING( &p_input->input.p_item->psz_name );
434
435     SIGNAL_SEND;
436 }
437
438 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
439             vlc_value_t oldval, vlc_value_t newval, void *p_data )
440 {
441     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
442     intf_sys_t          *p_sys      = p_intf->p_sys;
443     playlist_t          *p_playlist;
444     input_thread_t      *p_input    = NULL;
445     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
446
447     p_playlist = pl_Yield( p_intf );
448     PL_LOCK;
449     p_input = p_playlist->p_input;
450
451     if( !p_input )
452     {
453         PL_UNLOCK;
454         pl_Release( p_playlist );
455         return VLC_SUCCESS;
456     }
457
458     vlc_object_yield( p_input );
459     PL_UNLOCK;
460     pl_Release( p_playlist );
461
462     ItemChangeSignal( p_sys->p_conn, p_input );
463
464     vlc_object_release( p_input );
465     return VLC_SUCCESS;
466 }
467