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