]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Use vlc_object_kill(). Needs triple checking.
[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  * Authors:    Rafaël Carré <funman at videolanorg>
8  *             Mirsal Ennaime <mirsal dot ennaime at gmail dot com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*
26  * D-Bus Specification:
27  *      http://dbus.freedesktop.org/doc/dbus-specification.html
28  * D-Bus low-level C API (libdbus)
29  *      http://dbus.freedesktop.org/doc/dbus/api/html/index.html
30  */
31
32 /*
33  * TODO:
34  *  properties ?
35  *
36  *  macros to read incoming arguments
37  *
38  *  explore different possible types (arrays..)
39  *
40  *  what must we do if org.videolan.vlc already exist on the bus ?
41  *  ( there is more than one vlc instance )
42  */
43
44 /*****************************************************************************
45  * Preamble
46  *****************************************************************************/
47
48 #include <dbus/dbus.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #include "dbus.h"
54
55 #include <vlc/vlc.h>
56 #include <vlc_aout.h>
57 #include <vlc_interface.h>
58 #include <vlc_meta.h>
59 #include <vlc_input.h>
60 #include <vlc_playlist.h>
61
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 TrackChange( vlc_object_t *p_this, const char *psz_var,
73                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
74
75 static int GetInputMeta ( input_item_t *p_input, 
76                     DBusMessageIter *args);
77
78 struct intf_sys_t
79 {
80     DBusConnection *p_conn;
81 };
82
83 /*****************************************************************************
84  * Module descriptor
85  *****************************************************************************/
86
87 vlc_module_begin();
88     set_shortname( _("dbus"));
89     set_category( CAT_INTERFACE );
90     set_subcategory( SUBCAT_INTERFACE_CONTROL );
91     set_description( _("D-Bus control interface") );
92     set_capability( "interface", 0 );
93     set_callbacks( Open, Close );
94 vlc_module_end();
95
96 /*****************************************************************************
97  * Methods
98  *****************************************************************************/
99 #if 0
100 DBUS_METHOD( PlaylistExport_XSPF )
101 { /*export playlist to an xspf file */
102
103   /* reads the filename to export to */
104   /* returns the status as int32:
105    *    0 : success
106    *    1 : error
107    *    2 : playlist empty
108    */
109     REPLY_INIT;
110     OUT_ARGUMENTS;
111
112     DBusError error; 
113     dbus_error_init( &error );
114
115     char *psz_file;
116     dbus_int32_t i_ret;
117
118     dbus_message_get_args( p_from, &error,
119             DBUS_TYPE_STRING, &psz_file,
120             DBUS_TYPE_INVALID );
121
122     if( dbus_error_is_set( &error ) )
123     {
124         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
125                 error.message );
126         dbus_error_free( &error );
127         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
128     }
129
130     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
131
132     if( ( !playlist_IsEmpty( p_playlist ) ) &&
133             ( p_playlist->p_root_category->i_children > 0 ) )
134     {
135         if( playlist_Export( p_playlist, psz_file,
136                          p_playlist->p_root_category->pp_children[0],
137                          "export-xspf" ) == VLC_SUCCESS )
138             i_ret = 0;
139         else
140             i_ret = 1;
141     }
142     else
143         i_ret = 2;
144
145     pl_Release( ((vlc_object_t*) p_this ) );
146
147     ADD_INT32( &i_ret );
148     REPLY_SEND;
149 }
150 #endif
151
152 /* Player */
153
154 DBUS_METHOD( Quit )
155 { /* exits vlc */
156     REPLY_INIT;
157     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
158     playlist_Stop( p_playlist );
159     pl_Release( ((vlc_object_t*) p_this) );
160     vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
161     REPLY_SEND;
162 }
163
164 DBUS_METHOD( PositionGet )
165 { /* returns position as an int in the range [0;1000] */
166     REPLY_INIT;
167     OUT_ARGUMENTS;
168     vlc_value_t position;
169     dbus_int32_t i_pos;
170
171     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
172     input_thread_t *p_input = p_playlist->p_input;
173
174     if( !p_input )
175         i_pos = 0;
176     else
177     {
178         var_Get( p_input, "position", &position );
179         i_pos = position.f_float * 1000 ;
180     }
181     ADD_INT32( &i_pos );
182     pl_Release( ((vlc_object_t*) p_this) );
183     REPLY_SEND;
184 }
185
186 DBUS_METHOD( PositionSet )
187 { /* set position from an int in the range [0;1000] */
188
189     REPLY_INIT;
190     vlc_value_t position;
191     dbus_int32_t i_pos;
192
193     DBusError error;
194     dbus_error_init( &error );
195
196     dbus_message_get_args( p_from, &error,
197             DBUS_TYPE_INT32, &i_pos,
198             DBUS_TYPE_INVALID );
199
200     if( dbus_error_is_set( &error ) )
201     {
202         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
203                 error.message );
204         dbus_error_free( &error );
205         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
206     }
207     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
208     input_thread_t *p_input = p_playlist->p_input;
209
210     if( p_input )
211     {
212         position.f_float = ((float)i_pos) / 1000;
213         var_Set( p_input, "position", position );
214     }
215     pl_Release( ((vlc_object_t*) p_this) );
216     REPLY_SEND;
217 }
218
219 DBUS_METHOD( VolumeGet )
220 { /* returns volume in percentage */
221     REPLY_INIT;
222     OUT_ARGUMENTS;
223     dbus_int32_t i_dbus_vol;
224     audio_volume_t i_vol;
225     /* 2nd argument of aout_VolumeGet is int32 */
226     aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
227     i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
228     ADD_INT32( &i_dbus_vol );
229     REPLY_SEND;
230 }
231
232 DBUS_METHOD( VolumeSet )
233 { /* set volume in percentage */
234     REPLY_INIT;
235
236     DBusError error;
237     dbus_error_init( &error );
238
239     dbus_int32_t i_dbus_vol;
240     audio_volume_t i_vol;
241
242     dbus_message_get_args( p_from, &error,
243             DBUS_TYPE_INT32, &i_dbus_vol,
244             DBUS_TYPE_INVALID );
245
246     if( dbus_error_is_set( &error ) )
247     {
248         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
249                 error.message );
250         dbus_error_free( &error );
251         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
252     }
253
254     i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
255     aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
256
257     REPLY_SEND;
258 }
259
260 DBUS_METHOD( Next )
261 { /* next playlist item */
262     REPLY_INIT;
263     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
264     playlist_Next( p_playlist );
265     pl_Release( ((vlc_object_t*) p_this) );
266     REPLY_SEND;
267 }
268
269 DBUS_METHOD( Prev )
270 { /* previous playlist item */
271     REPLY_INIT;
272     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
273     playlist_Prev( p_playlist );
274     pl_Release( ((vlc_object_t*) p_this) );
275     REPLY_SEND;
276 }
277
278 DBUS_METHOD( Stop )
279 { /* stop playing */
280     REPLY_INIT;
281     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
282     playlist_Stop( p_playlist );
283     pl_Release( ((vlc_object_t*) p_this) );
284     REPLY_SEND;
285 }
286
287 DBUS_METHOD( GetStatus )
288 { /* returns an int: 0=playing 1=paused 2=stopped */
289     REPLY_INIT;
290     OUT_ARGUMENTS;
291
292     dbus_int32_t i_status;
293     vlc_value_t val;
294
295     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
296     input_thread_t *p_input = p_playlist->p_input;
297
298     i_status = 2;
299     if( p_input )
300     {
301         var_Get( p_input, "state", &val );
302         if( val.i_int == PAUSE_S )
303             i_status = 1;
304         else if( val.i_int == PLAYING_S )
305             i_status = 0;
306     }
307
308     pl_Release( p_playlist );
309
310     ADD_INT32( &i_status );
311     REPLY_SEND;
312 }
313
314 DBUS_METHOD( Pause )
315 {
316     REPLY_INIT;
317     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
318     playlist_Pause( p_playlist );
319     pl_Release( p_playlist );
320     REPLY_SEND;
321 }
322
323 DBUS_METHOD( Play )
324 {
325     REPLY_INIT;
326     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
327     playlist_Play( p_playlist );
328     pl_Release( p_playlist );
329     REPLY_SEND;
330 }
331
332 /* Media Player information */
333
334 DBUS_METHOD( Identity )
335 {
336     REPLY_INIT;
337     OUT_ARGUMENTS;
338     char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 1 );
339     sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
340     ADD_STRING( &psz_identity );
341     free( psz_identity );
342     REPLY_SEND;
343 }
344
345 /* TrackList */
346
347 DBUS_METHOD( AddTrack )
348 { /* add the string to the playlist, and play it if the boolean is true */
349     REPLY_INIT;
350
351     DBusError error;
352     dbus_error_init( &error );
353
354     char *psz_mrl;
355     dbus_bool_t b_play;
356
357     dbus_message_get_args( p_from, &error,
358             DBUS_TYPE_STRING, &psz_mrl,
359             DBUS_TYPE_BOOLEAN, &b_play,
360             DBUS_TYPE_INVALID );
361
362     if( dbus_error_is_set( &error ) )
363     {
364         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
365                 error.message );
366         dbus_error_free( &error );
367         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
368     }
369
370     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
371     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
372             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) , 
373             PLAYLIST_END, VLC_TRUE, VLC_FALSE );
374     pl_Release( p_playlist );
375
376     REPLY_SEND;
377 }
378
379 DBUS_METHOD( GetCurrentTrack )
380 {
381     REPLY_INIT;
382     OUT_ARGUMENTS;
383     dbus_int32_t i_position = 0;
384     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
385     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
386     
387     while ( p_tested_item->p_input->i_id != 
388                     p_playlist->status.p_item->p_input->i_id )
389     {
390         i_position++;
391         TEST_NEXT;
392     }
393
394     pl_Release( p_playlist );
395
396     ADD_INT32( &i_position );
397     REPLY_SEND;
398 }
399
400 DBUS_METHOD( GetMetadata )
401
402     REPLY_INIT;
403     OUT_ARGUMENTS;
404     DBusError error;
405     dbus_error_init( &error );
406
407     dbus_int32_t i_position, i_count = 0;
408     
409     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
410     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
411
412     dbus_message_get_args( p_from, &error,
413             DBUS_TYPE_INT32, &i_position,
414             DBUS_TYPE_INVALID );
415
416     if( dbus_error_is_set( &error ) )
417     {
418         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
419                 error.message );
420         dbus_error_free( &error );
421         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
422     }
423     
424     while ( i_count < i_position ) 
425     {
426         i_count++;
427             TEST_NEXT;
428     }
429
430     GetInputMeta ( p_tested_item->p_input, &args );
431
432     pl_Release( p_playlist );
433     REPLY_SEND;
434 }
435
436 DBUS_METHOD( GetLength )
437
438     REPLY_INIT;
439     OUT_ARGUMENTS;
440
441     dbus_int32_t i_elements = 0;
442     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
443     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
444     playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist, 
445                     p_playlist->p_root_onelevel ); 
446
447     while ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id )
448     {
449         i_elements++;
450         TEST_NEXT;
451     }
452
453     pl_Release( p_playlist );
454     
455     ADD_INT32( &i_elements );
456     REPLY_SEND;
457 }
458
459 DBUS_METHOD( DelTrack )
460 {
461     REPLY_INIT;
462
463     DBusError error;
464     dbus_error_init( &error );
465
466     dbus_int32_t i_position, i_count = 0;
467     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
468     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
469
470     dbus_message_get_args( p_from, &error,
471             DBUS_TYPE_INT32, &i_position,
472             DBUS_TYPE_INVALID );
473
474     if( dbus_error_is_set( &error ) )
475     {
476         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
477                 error.message );
478         dbus_error_free( &error );
479         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
480     }
481     
482     while ( i_count < i_position ) 
483     {
484         i_count++;
485         TEST_NEXT;
486     }
487
488     PL_LOCK; 
489     playlist_DeleteFromInput( p_playlist, 
490                     p_tested_item->p_input->i_id, 
491                     VLC_TRUE );
492     PL_UNLOCK;
493
494     pl_Release( p_playlist );
495     
496     REPLY_SEND;
497 }
498
499 /*****************************************************************************
500  * Introspection method
501  *****************************************************************************/
502
503 DBUS_METHOD( handle_introspect_root )
504 { /* handles introspection of /org/videolan/vlc */
505     REPLY_INIT;
506     OUT_ARGUMENTS;
507     ADD_STRING( &psz_introspection_xml_data_root );
508     REPLY_SEND;
509 }
510
511 DBUS_METHOD( handle_introspect_player )
512 {
513     REPLY_INIT;
514     OUT_ARGUMENTS;
515     ADD_STRING( &psz_introspection_xml_data_player );
516     REPLY_SEND;
517 }
518
519 DBUS_METHOD( handle_introspect_tracklist )
520 {
521     REPLY_INIT;
522     OUT_ARGUMENTS;
523     ADD_STRING( &psz_introspection_xml_data_tracklist );
524     REPLY_SEND;
525 }
526
527 /*****************************************************************************
528  * handle_*: answer to incoming messages
529  *****************************************************************************/
530
531 #define METHOD_FUNC( method, function ) \
532     else if( dbus_message_is_method_call( p_from, VLC_DBUS_INTERFACE, method ) )\
533         return function( p_conn, p_from, p_this )
534
535 DBUS_METHOD( handle_root )
536 {
537
538     if( dbus_message_is_method_call( p_from,
539                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
540         return handle_introspect_root( p_conn, p_from, p_this );
541
542     /* here D-Bus method's names are associated to an handler */
543
544     METHOD_FUNC( "Identity",                Identity );
545
546     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
547 }
548
549
550 DBUS_METHOD( handle_player )
551 {
552     if( dbus_message_is_method_call( p_from,
553                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
554     return handle_introspect_player( p_conn, p_from, p_this );
555
556     /* here D-Bus method's names are associated to an handler */
557
558     METHOD_FUNC( "Prev",                    Prev );
559     METHOD_FUNC( "Next",                    Next );
560     METHOD_FUNC( "Quit",                    Quit );
561     METHOD_FUNC( "Stop",                    Stop );
562     METHOD_FUNC( "Play",                    Play );
563     METHOD_FUNC( "Pause",                   Pause );
564     METHOD_FUNC( "VolumeSet",               VolumeSet );
565     METHOD_FUNC( "VolumeGet",               VolumeGet );
566     METHOD_FUNC( "PositionSet",             PositionSet );
567     METHOD_FUNC( "PositionGet",             PositionGet );
568     METHOD_FUNC( "GetStatus",               GetStatus );
569
570     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
571 }
572
573 DBUS_METHOD( handle_tracklist )
574 {
575     if( dbus_message_is_method_call( p_from,
576                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
577     return handle_introspect_tracklist( p_conn, p_from, p_this );
578
579     /* here D-Bus method's names are associated to an handler */
580
581     METHOD_FUNC( "GetMetadata",             GetMetadata );
582     METHOD_FUNC( "GetCurrentTrack",         GetCurrentTrack );
583     METHOD_FUNC( "GetLength",               GetLength );
584     METHOD_FUNC( "AddTrack",                AddTrack );
585     METHOD_FUNC( "DelTrack",                DelTrack );
586
587     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
588 }
589
590 /*****************************************************************************
591  * Open: initialize interface
592  *****************************************************************************/
593
594 static int Open( vlc_object_t *p_this )
595 { /* initialisation of the connection */
596     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
597     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
598     playlist_t      *p_playlist;
599     DBusConnection  *p_conn;
600     DBusError       error;
601
602     if( !p_sys )
603         return VLC_ENOMEM;
604
605     dbus_threads_init_default();
606
607     dbus_error_init( &error );
608
609     /* connect to the session bus */
610     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
611     if( !p_conn )
612     {
613         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
614                 error.message );
615         dbus_error_free( &error );
616         free( p_sys );
617         return VLC_EGENERIC;
618     }
619
620     /* register a well-known name on the bus */
621     dbus_bus_request_name( p_conn, "org.freedesktop.MediaPlayer", 0, &error );
622     if( dbus_error_is_set( &error ) )
623     {
624         msg_Err( p_this, "Error requesting org.freedesktop.MediaPlayer service:"                " %s\n", error.message );
625         dbus_error_free( &error );
626         free( p_sys );
627         return VLC_EGENERIC;
628     }
629
630     /* we register the objects */
631     dbus_connection_register_object_path( p_conn, VLC_DBUS_ROOT_PATH,
632             &vlc_dbus_root_vtable, p_this );
633     dbus_connection_register_object_path( p_conn, VLC_DBUS_PLAYER_PATH,
634             &vlc_dbus_player_vtable, p_this );
635     dbus_connection_register_object_path( p_conn, VLC_DBUS_TRACKLIST_PATH,
636             &vlc_dbus_tracklist_vtable, p_this );
637
638     dbus_connection_flush( p_conn );
639
640     p_playlist = pl_Yield( p_intf );
641     PL_LOCK;
642     var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
643     PL_UNLOCK;
644     pl_Release( p_playlist );
645
646     p_intf->pf_run = Run;
647     p_intf->p_sys = p_sys;
648     p_sys->p_conn = p_conn;
649
650     return VLC_SUCCESS;
651 }
652
653 /*****************************************************************************
654  * Close: destroy interface
655  *****************************************************************************/
656
657 static void Close   ( vlc_object_t *p_this )
658 {
659     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
660     playlist_t      *p_playlist = pl_Yield( p_intf );;
661
662     PL_LOCK;
663     var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
664     PL_UNLOCK;
665     pl_Release( p_playlist );
666
667     dbus_connection_unref( p_intf->p_sys->p_conn );
668
669     free( p_intf->p_sys );
670 }
671
672 /*****************************************************************************
673  * Run: main loop
674  *****************************************************************************/
675
676 static void Run          ( intf_thread_t *p_intf )
677 {
678     while( !p_intf->b_die )
679     {
680         msleep( INTF_IDLE_SLEEP );
681         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
682     }
683 }
684
685 /*****************************************************************************
686  * TrackChange: Playlist item change callback
687  *****************************************************************************/
688
689 DBUS_SIGNAL( TrackChangeSignal )
690 { /* emit the metadata of the new item */
691     SIGNAL_INIT( "TrackChange" );
692     OUT_ARGUMENTS;
693
694     input_thread_t *p_input = (input_thread_t*) p_data;
695     GetInputMeta ( input_GetItem(p_input), &args );
696
697     SIGNAL_SEND;
698 }
699
700 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
701             vlc_value_t oldval, vlc_value_t newval, void *p_data )
702 {
703     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
704     intf_sys_t          *p_sys      = p_intf->p_sys;
705     playlist_t          *p_playlist;
706     input_thread_t      *p_input    = NULL;
707     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
708
709     p_playlist = pl_Yield( p_intf );
710     PL_LOCK;
711     p_input = p_playlist->p_input;
712
713     if( !p_input )
714     {
715         PL_UNLOCK;
716         pl_Release( p_playlist );
717         return VLC_SUCCESS;
718     }
719
720     vlc_object_yield( p_input );
721     PL_UNLOCK;
722     pl_Release( p_playlist );
723
724     TrackChangeSignal( p_sys->p_conn, p_input );
725
726     vlc_object_release( p_input );
727     return VLC_SUCCESS;
728 }
729
730 /*****************************************************************************
731  * GetInputMeta: Fill a DBusMessage with the given input item metadata
732  *****************************************************************************/
733
734 #define ADD_META( entry, type, data ) \
735     if( data ) { \
736         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
737                 NULL, &dict_entry ); \
738         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
739                 &ppsz_meta_items[entry] ); \
740         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
741                 type##_AS_STRING, &variant ); \
742         dbus_message_iter_append_basic( &variant, \
743                 type, \
744                 & data ); \
745         dbus_message_iter_close_container( &dict_entry, &variant ); \
746         dbus_message_iter_close_container( &dict, &dict_entry ); }\
747
748 #define ADD_VLC_META_STRING( entry, item ) \
749         ADD_META( entry, DBUS_TYPE_STRING, \
750                 p_input->p_meta->psz_##item );
751
752 static int GetInputMeta( input_item_t* p_input, 
753                         DBusMessageIter *args )
754 { /*FIXME: Works only for already read metadata. */ 
755   /*FIXME: Should return the length in seconds rather than milliseconds */
756   
757     DBusMessageIter dict, dict_entry, variant;
758
759     const char* ppsz_meta_items[] = 
760     {
761     "title", "artist", "genre", "copyright", "album", "tracknum",
762     "description", "rating", "date", "setting", "url", "language",
763     "nowplaying", "publisher", "encodedby", "arturl", "trackid",
764     "status", "URI", "length", "video-codec", "audio-codec",
765     "video-bitrate", "audio-bitrate", "audio-samplerate"
766     };
767     
768     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
769
770     ADD_VLC_META_STRING( 0, title );
771     ADD_VLC_META_STRING( 1, artist );
772     ADD_VLC_META_STRING( 2, genre );
773     ADD_VLC_META_STRING( 3, copyright );
774     ADD_VLC_META_STRING( 4, album );
775     ADD_VLC_META_STRING( 5, tracknum );
776     ADD_VLC_META_STRING( 6, description );
777     ADD_VLC_META_STRING( 7, rating );
778     ADD_VLC_META_STRING( 8, date );
779     ADD_VLC_META_STRING( 9, setting );
780     ADD_VLC_META_STRING( 10, url );
781     ADD_VLC_META_STRING( 11, language );
782     ADD_VLC_META_STRING( 12, nowplaying );
783     ADD_VLC_META_STRING( 13, publisher );
784     ADD_VLC_META_STRING( 14, encodedby );
785     ADD_VLC_META_STRING( 15, arturl );
786     ADD_VLC_META_STRING( 16, trackid ); 
787
788     ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
789     ADD_META( 18, DBUS_TYPE_STRING, p_input->psz_uri ); 
790     ADD_META( 19, DBUS_TYPE_INT64, p_input->i_duration ); 
791
792     dbus_message_iter_close_container( args, &dict );
793     return VLC_SUCCESS;
794 }
795
796 #undef ADD_META
797 #undef ADD_VLC_META_STRING