]> git.sesse.net Git - vlc/blob - modules/control/dbus.c
Use intf_ShouldDie()
[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  *  extract:
31     "If you use this low-level API directly, you're signing up for some pain."
32  * MPRIS Specification (still drafting on June, 25 of 2007):
33  *      http://wiki.xmms2.xmms.se/index.php/Media_Player_Interfaces
34  */
35
36 /*****************************************************************************
37  * Preamble
38  *****************************************************************************/
39
40 #include <dbus/dbus.h>
41
42 #include "dbus.h"
43
44 #include <vlc/vlc.h>
45 #include <vlc_aout.h>
46 #include <vlc_interface.h>
47 #include <vlc_meta.h>
48 #include <vlc_input.h>
49 #include <vlc_playlist.h>
50
51 /*****************************************************************************
52  * Local prototypes.
53  *****************************************************************************/
54
55 static int  Open    ( vlc_object_t * );
56 static void Close   ( vlc_object_t * );
57 static void Run     ( intf_thread_t * );
58
59 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
60                     vlc_value_t oldval, vlc_value_t newval, void *p_data );
61
62 static int GetInputMeta ( input_item_t *p_input,
63                     DBusMessageIter *args);
64
65 struct intf_sys_t
66 {
67     DBusConnection *p_conn;
68 };
69
70 /*****************************************************************************
71  * Module descriptor
72  *****************************************************************************/
73
74 vlc_module_begin();
75     set_shortname( _("dbus"));
76     set_category( CAT_INTERFACE );
77     set_subcategory( SUBCAT_INTERFACE_CONTROL );
78     set_description( _("D-Bus control interface") );
79     set_capability( "interface", 0 );
80     set_callbacks( Open, Close );
81 vlc_module_end();
82
83 /*****************************************************************************
84  * Methods
85  *****************************************************************************/
86
87 /* Player */
88
89 DBUS_METHOD( Quit )
90 { /* exits vlc */
91     REPLY_INIT;
92     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
93     playlist_Stop( p_playlist );
94     pl_Release( ((vlc_object_t*) p_this) );
95     vlc_object_kill(((vlc_object_t*)p_this)->p_libvlc);
96     REPLY_SEND;
97 }
98
99 DBUS_METHOD( PositionGet )
100 { /* returns position in milliseconds */
101     REPLY_INIT;
102     OUT_ARGUMENTS;
103     vlc_value_t position;
104     dbus_int32_t i_pos;
105
106     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
107     input_thread_t *p_input = p_playlist->p_input;
108
109     if( !p_input )
110         i_pos = 0;
111     else
112     {
113         var_Get( p_input, "time", &position );
114         i_pos = position.i_time / 1000;
115     }
116     pl_Release( ((vlc_object_t*) p_this) );
117     ADD_INT32( &i_pos );
118     REPLY_SEND;
119 }
120
121 DBUS_METHOD( PositionSet )
122 { /* set position in milliseconds */
123
124     REPLY_INIT;
125     vlc_value_t position;
126     playlist_t* p_playlist = NULL;
127     dbus_int32_t i_pos;
128
129     DBusError error;
130     dbus_error_init( &error );
131
132     dbus_message_get_args( p_from, &error,
133             DBUS_TYPE_INT32, &i_pos,
134             DBUS_TYPE_INVALID );
135
136     if( dbus_error_is_set( &error ) )
137     {
138         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
139                 error.message );
140         dbus_error_free( &error );
141         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
142     }
143     p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
144     input_thread_t *p_input = p_playlist->p_input;
145
146     if( p_input )
147     {
148         position.i_time = i_pos * 1000;
149         var_Set( p_input, "time", position );
150     }
151     pl_Release( ((vlc_object_t*) p_this) );
152     REPLY_SEND;
153 }
154
155 DBUS_METHOD( VolumeGet )
156 { /* returns volume in percentage */
157     REPLY_INIT;
158     OUT_ARGUMENTS;
159     dbus_int32_t i_dbus_vol;
160     audio_volume_t i_vol;
161     /* 2nd argument of aout_VolumeGet is int32 */
162     aout_VolumeGet( (vlc_object_t*) p_this, &i_vol );
163     i_dbus_vol = ( 100 * i_vol ) / AOUT_VOLUME_MAX;
164     ADD_INT32( &i_dbus_vol );
165     REPLY_SEND;
166 }
167
168 DBUS_METHOD( VolumeSet )
169 { /* set volume in percentage */
170     REPLY_INIT;
171
172     DBusError error;
173     dbus_error_init( &error );
174
175     dbus_int32_t i_dbus_vol;
176     audio_volume_t i_vol;
177
178     dbus_message_get_args( p_from, &error,
179             DBUS_TYPE_INT32, &i_dbus_vol,
180             DBUS_TYPE_INVALID );
181
182     if( dbus_error_is_set( &error ) )
183     {
184         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
185                 error.message );
186         dbus_error_free( &error );
187         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
188     }
189
190     i_vol = ( AOUT_VOLUME_MAX / 100 ) *i_dbus_vol;
191     aout_VolumeSet( (vlc_object_t*) p_this, i_vol );
192
193     REPLY_SEND;
194 }
195
196 DBUS_METHOD( Next )
197 { /* next playlist item */
198     REPLY_INIT;
199     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
200     playlist_Next( p_playlist );
201     pl_Release( ((vlc_object_t*) p_this) );
202     REPLY_SEND;
203 }
204
205 DBUS_METHOD( Prev )
206 { /* previous playlist item */
207     REPLY_INIT;
208     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
209     playlist_Prev( p_playlist );
210     pl_Release( ((vlc_object_t*) p_this) );
211     REPLY_SEND;
212 }
213
214 DBUS_METHOD( Stop )
215 { /* stop playing */
216     REPLY_INIT;
217     playlist_t *p_playlist = pl_Yield( ((vlc_object_t*) p_this) );
218     playlist_Stop( p_playlist );
219     pl_Release( ((vlc_object_t*) p_this) );
220     REPLY_SEND;
221 }
222
223 DBUS_METHOD( GetStatus )
224 { /* returns an int: 0=playing 1=paused 2=stopped */
225     REPLY_INIT;
226     OUT_ARGUMENTS;
227
228     dbus_int32_t i_status;
229     vlc_value_t val;
230
231     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
232     input_thread_t *p_input = p_playlist->p_input;
233
234     i_status = 2;
235     if( p_input )
236     {
237         var_Get( p_input, "state", &val );
238         if( val.i_int == PAUSE_S )
239             i_status = 1;
240         else if( val.i_int == PLAYING_S )
241             i_status = 0;
242     }
243
244     pl_Release( p_playlist );
245
246     ADD_INT32( &i_status );
247     REPLY_SEND;
248 }
249
250 DBUS_METHOD( Pause )
251 {
252     REPLY_INIT;
253     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
254     playlist_Pause( p_playlist );
255     pl_Release( p_playlist );
256     REPLY_SEND;
257 }
258
259 DBUS_METHOD( Play )
260 {
261     REPLY_INIT;
262     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
263     playlist_Play( p_playlist );
264     pl_Release( p_playlist );
265     REPLY_SEND;
266 }
267
268 DBUS_METHOD( GetCurrentMetadata )
269 {
270     REPLY_INIT;
271     OUT_ARGUMENTS;
272     playlist_t* p_playlist = pl_Yield( (vlc_object_t*) p_this );
273
274     if( p_playlist->status.p_item )
275         GetInputMeta( p_playlist->status.p_item->p_input, &args );
276
277     pl_Release( p_playlist );
278     REPLY_SEND;
279 }
280
281 /* Media Player information */
282
283 DBUS_METHOD( Identity )
284 {
285     REPLY_INIT;
286     OUT_ARGUMENTS;
287     char *psz_identity = malloc( strlen( PACKAGE ) + strlen( VERSION ) + 2 );
288     sprintf( psz_identity, "%s %s", PACKAGE, VERSION );
289     ADD_STRING( &psz_identity );
290     free( psz_identity );
291     REPLY_SEND;
292 }
293
294 /* TrackList */
295
296 DBUS_METHOD( AddTrack )
297 { /* add the string to the playlist, and play it if the boolean is true */
298     REPLY_INIT;
299
300     DBusError error;
301     dbus_error_init( &error );
302     playlist_t* p_playlist = NULL;
303
304     char *psz_mrl;
305     dbus_bool_t b_play;
306
307     dbus_message_get_args( p_from, &error,
308             DBUS_TYPE_STRING, &psz_mrl,
309             DBUS_TYPE_BOOLEAN, &b_play,
310             DBUS_TYPE_INVALID );
311
312     if( dbus_error_is_set( &error ) )
313     {
314         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
315                 error.message );
316         dbus_error_free( &error );
317         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
318     }
319
320     p_playlist = pl_Yield( (vlc_object_t*) p_this );
321     playlist_Add( p_playlist, psz_mrl, NULL, PLAYLIST_APPEND |
322             ( ( b_play == TRUE ) ? PLAYLIST_GO : 0 ) ,
323             PLAYLIST_END, VLC_TRUE, VLC_FALSE );
324     pl_Release( p_playlist );
325
326     REPLY_SEND;
327 }
328
329 DBUS_METHOD( GetCurrentTrack )
330 {
331     REPLY_INIT;
332     OUT_ARGUMENTS;
333     /* FIXME 0 indicates the first item,
334      * what to do if we're stopped, or empty ? */
335     dbus_int32_t i_position = 0;
336     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
337     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
338
339     if( p_playlist->status.p_item )
340         while ( p_tested_item && p_tested_item->p_input->i_id !=
341                 p_playlist->status.p_item->p_input->i_id )
342         {
343             i_position++;
344             TEST_NEXT_ITEM;
345         }
346     /* FIXME if p_tested_item is NULL at that point, what do we do ? */
347     pl_Release( p_playlist );
348
349     ADD_INT32( &i_position );
350     REPLY_SEND;
351 }
352
353 DBUS_METHOD( GetMetadata )
354 {
355     REPLY_INIT;
356     OUT_ARGUMENTS;
357     DBusError error;
358     dbus_error_init( &error );
359
360     dbus_int32_t i_position, i_count = 0;
361
362     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
363     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
364
365     dbus_message_get_args( p_from, &error,
366            DBUS_TYPE_INT32, &i_position,
367            DBUS_TYPE_INVALID );
368
369     if( dbus_error_is_set( &error ) )
370     {
371         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
372                 error.message );
373         dbus_error_free( &error );
374         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
375     }
376
377     while ( p_tested_item && ( i_count < i_position ) )
378     {
379         i_count++;
380         TEST_NEXT_ITEM;
381     }
382
383     if( p_tested_item )
384         GetInputMeta ( p_tested_item->p_input, &args );
385
386     pl_Release( p_playlist );
387     REPLY_SEND;
388 }
389
390 DBUS_METHOD( GetLength )
391 {
392     REPLY_INIT;
393     OUT_ARGUMENTS;
394
395     dbus_int32_t i_elements = 0;
396     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
397     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
398     playlist_item_t* p_last_item = playlist_GetLastLeaf( p_playlist,
399                     p_playlist->p_root_onelevel );
400
401     while ( p_tested_item &&
402                ( p_tested_item->p_input->i_id != p_last_item->p_input->i_id ) )
403     {
404         i_elements++;
405         TEST_NEXT_ITEM;
406     }
407
408     pl_Release( p_playlist );
409
410     ADD_INT32( &i_elements );
411     REPLY_SEND;
412 }
413
414 DBUS_METHOD( DelTrack )
415 {
416     REPLY_INIT;
417
418     DBusError error;
419     dbus_error_init( &error );
420
421     dbus_int32_t i_position, i_count = 0;
422     playlist_t *p_playlist = pl_Yield( (vlc_object_t*) p_this );
423     playlist_item_t* p_tested_item = p_playlist->p_root_onelevel;
424
425     dbus_message_get_args( p_from, &error,
426             DBUS_TYPE_INT32, &i_position,
427             DBUS_TYPE_INVALID );
428
429     if( dbus_error_is_set( &error ) )
430     {
431         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
432                 error.message );
433         dbus_error_free( &error );
434         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
435     }
436
437     while ( p_tested_item && ( i_count < i_position ) )
438     {
439         i_count++;
440         TEST_NEXT_ITEM;
441     }
442
443     if( p_tested_item )
444     {
445         PL_LOCK;
446         playlist_DeleteFromInput( p_playlist,
447             p_tested_item->p_input->i_id,
448             VLC_TRUE );
449         PL_UNLOCK;
450     }
451
452     pl_Release( p_playlist );
453
454     REPLY_SEND;
455 }
456
457 DBUS_METHOD( Loop )
458 {
459     REPLY_INIT;
460     OUT_ARGUMENTS;
461
462     DBusError error;
463     dbus_bool_t b_loop;
464     vlc_value_t val;
465     playlist_t* p_playlist = NULL;
466
467     dbus_error_init( &error );
468     dbus_message_get_args( p_from, &error,
469             DBUS_TYPE_BOOLEAN, &b_loop,
470             DBUS_TYPE_INVALID );
471
472     if( dbus_error_is_set( &error ) )
473     {
474         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
475                 error.message );
476         dbus_error_free( &error );
477         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
478     }
479
480     val.b_bool = ( b_loop == TRUE ) ? VLC_TRUE : VLC_FALSE ;
481     p_playlist = pl_Yield( (vlc_object_t*) p_this );
482     var_Set ( p_playlist, "loop", val );
483     pl_Release( ((vlc_object_t*) p_this) );
484
485     REPLY_SEND;
486 }
487
488 DBUS_METHOD( Repeat )
489 {
490     REPLY_INIT;
491     OUT_ARGUMENTS;
492
493     DBusError error;
494     dbus_bool_t b_repeat;
495     vlc_value_t val;
496     playlist_t* p_playlist = NULL;
497
498     dbus_error_init( &error );
499     dbus_message_get_args( p_from, &error,
500             DBUS_TYPE_BOOLEAN, &b_repeat,
501             DBUS_TYPE_INVALID );
502
503     if( dbus_error_is_set( &error ) )
504     {
505         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
506                 error.message );
507         dbus_error_free( &error );
508         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
509     }
510
511     val.b_bool = ( b_repeat == TRUE ) ? VLC_TRUE : VLC_FALSE ;
512
513     p_playlist = pl_Yield( (vlc_object_t*) p_this );
514     var_Set ( p_playlist, "repeat", val );
515     pl_Release( ((vlc_object_t*) p_this) );
516
517     REPLY_SEND;
518 }
519
520 DBUS_METHOD( Random )
521 {
522     REPLY_INIT;
523     OUT_ARGUMENTS;
524
525     DBusError error;
526     dbus_bool_t b_random;
527     vlc_value_t val;
528     playlist_t* p_playlist = NULL;
529  
530     dbus_error_init( &error );
531     dbus_message_get_args( p_from, &error,
532             DBUS_TYPE_BOOLEAN, &b_random,
533             DBUS_TYPE_INVALID );
534  
535     if( dbus_error_is_set( &error ) )
536     {
537         msg_Err( (vlc_object_t*) p_this, "D-Bus message reading : %s\n",
538                 error.message );
539         dbus_error_free( &error );
540         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
541     }
542
543     val.b_bool = ( b_random == TRUE ) ? VLC_TRUE : VLC_FALSE ;
544  
545     p_playlist = pl_Yield( (vlc_object_t*) p_this );
546     var_Set ( p_playlist, "random", val );
547     pl_Release( ((vlc_object_t*) p_this) );
548
549     REPLY_SEND;
550 }
551 /*****************************************************************************
552  * Introspection method
553  *****************************************************************************/
554
555 DBUS_METHOD( handle_introspect_root )
556 { /* handles introspection of root object */
557     REPLY_INIT;
558     OUT_ARGUMENTS;
559     ADD_STRING( &psz_introspection_xml_data_root );
560     REPLY_SEND;
561 }
562
563 DBUS_METHOD( handle_introspect_player )
564 {
565     REPLY_INIT;
566     OUT_ARGUMENTS;
567     ADD_STRING( &psz_introspection_xml_data_player );
568     REPLY_SEND;
569 }
570
571 DBUS_METHOD( handle_introspect_tracklist )
572 {
573     REPLY_INIT;
574     OUT_ARGUMENTS;
575     ADD_STRING( &psz_introspection_xml_data_tracklist );
576     REPLY_SEND;
577 }
578
579 /*****************************************************************************
580  * handle_*: answer to incoming messages
581  *****************************************************************************/
582
583 #define METHOD_FUNC( method, function ) \
584     else if( dbus_message_is_method_call( p_from, MPRIS_DBUS_INTERFACE, method ) )\
585         return function( p_conn, p_from, p_this )
586
587 DBUS_METHOD( handle_root )
588 {
589
590     if( dbus_message_is_method_call( p_from,
591                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
592         return handle_introspect_root( p_conn, p_from, p_this );
593
594     /* here D-Bus method's names are associated to an handler */
595
596     METHOD_FUNC( "Identity",                Identity );
597
598     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
599 }
600
601
602 DBUS_METHOD( handle_player )
603 {
604     if( dbus_message_is_method_call( p_from,
605                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
606     return handle_introspect_player( p_conn, p_from, p_this );
607
608     /* here D-Bus method's names are associated to an handler */
609
610     METHOD_FUNC( "Prev",                    Prev );
611     METHOD_FUNC( "Next",                    Next );
612     METHOD_FUNC( "Quit",                    Quit );
613     METHOD_FUNC( "Stop",                    Stop );
614     METHOD_FUNC( "Play",                    Play );
615     METHOD_FUNC( "Pause",                   Pause );
616     METHOD_FUNC( "Repeat",                  Repeat );
617     METHOD_FUNC( "VolumeSet",               VolumeSet );
618     METHOD_FUNC( "VolumeGet",               VolumeGet );
619     METHOD_FUNC( "PositionSet",             PositionSet );
620     METHOD_FUNC( "PositionGet",             PositionGet );
621     METHOD_FUNC( "GetStatus",               GetStatus );
622     METHOD_FUNC( "GetMetadata",             GetCurrentMetadata );
623
624     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
625 }
626
627 DBUS_METHOD( handle_tracklist )
628 {
629     if( dbus_message_is_method_call( p_from,
630                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect" ) )
631     return handle_introspect_tracklist( p_conn, p_from, p_this );
632
633     /* here D-Bus method's names are associated to an handler */
634
635     METHOD_FUNC( "GetMetadata",             GetMetadata );
636     METHOD_FUNC( "GetCurrentTrack",         GetCurrentTrack );
637     METHOD_FUNC( "GetLength",               GetLength );
638     METHOD_FUNC( "AddTrack",                AddTrack );
639     METHOD_FUNC( "DelTrack",                DelTrack );
640     METHOD_FUNC( "Loop",                    Loop );
641     METHOD_FUNC( "Random",                  Random );
642
643     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
644 }
645
646 /*****************************************************************************
647  * Open: initialize interface
648  *****************************************************************************/
649
650 static int Open( vlc_object_t *p_this )
651 { /* initialisation of the connection */
652     intf_thread_t   *p_intf = (intf_thread_t*)p_this;
653     intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
654     playlist_t      *p_playlist;
655     DBusConnection  *p_conn;
656     DBusError       error;
657
658     if( !p_sys )
659         return VLC_ENOMEM;
660
661     dbus_threads_init_default();
662
663     dbus_error_init( &error );
664
665     /* connect to the session bus */
666     p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
667     if( !p_conn )
668     {
669         msg_Err( p_this, "Failed to connect to the D-Bus session daemon: %s",
670                 error.message );
671         dbus_error_free( &error );
672         free( p_sys );
673         return VLC_EGENERIC;
674     }
675
676     /* register a well-known name on the bus */
677     dbus_bus_request_name( p_conn, VLC_MPRIS_DBUS_SERVICE, 0, &error );
678     if( dbus_error_is_set( &error ) )
679     {
680         msg_Err( p_this, "Error requesting service " VLC_MPRIS_DBUS_SERVICE
681                  ": %s", error.message );
682         dbus_error_free( &error );
683         free( p_sys );
684         return VLC_EGENERIC;
685     }
686
687     /* we register the objects */
688     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_ROOT_PATH,
689             &vlc_dbus_root_vtable, p_this );
690     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_PLAYER_PATH,
691             &vlc_dbus_player_vtable, p_this );
692     dbus_connection_register_object_path( p_conn, MPRIS_DBUS_TRACKLIST_PATH,
693             &vlc_dbus_tracklist_vtable, p_this );
694
695     dbus_connection_flush( p_conn );
696
697     p_playlist = pl_Yield( p_intf );
698     PL_LOCK;
699     var_AddCallback( p_playlist, "playlist-current", TrackChange, p_intf );
700     PL_UNLOCK;
701     pl_Release( p_playlist );
702
703     p_intf->pf_run = Run;
704     p_intf->p_sys = p_sys;
705     p_sys->p_conn = p_conn;
706
707     return VLC_SUCCESS;
708 }
709
710 /*****************************************************************************
711  * Close: destroy interface
712  *****************************************************************************/
713
714 static void Close   ( vlc_object_t *p_this )
715 {
716     intf_thread_t   *p_intf     = (intf_thread_t*) p_this;
717     playlist_t      *p_playlist = pl_Yield( p_intf );;
718
719     PL_LOCK;
720     var_DelCallback( p_playlist, "playlist-current", TrackChange, p_intf );
721     PL_UNLOCK;
722     pl_Release( p_playlist );
723
724     dbus_connection_unref( p_intf->p_sys->p_conn );
725
726     free( p_intf->p_sys );
727 }
728
729 /*****************************************************************************
730  * Run: main loop
731  *****************************************************************************/
732
733 static void Run          ( intf_thread_t *p_intf )
734 {
735     while( !intf_ShouldDie( p_intf ) )
736     {
737         msleep( INTF_IDLE_SLEEP );
738         dbus_connection_read_write_dispatch( p_intf->p_sys->p_conn, 0 );
739     }
740 }
741
742 /*****************************************************************************
743  * TrackChange: Playlist item change callback
744  *****************************************************************************/
745
746 DBUS_SIGNAL( TrackChangeSignal )
747 { /* emit the metadata of the new item */
748     SIGNAL_INIT( "TrackChange" );
749     OUT_ARGUMENTS;
750
751     input_thread_t *p_input = (input_thread_t*) p_data;
752     GetInputMeta ( input_GetItem(p_input), &args );
753
754     SIGNAL_SEND;
755 }
756
757 static int TrackChange( vlc_object_t *p_this, const char *psz_var,
758             vlc_value_t oldval, vlc_value_t newval, void *p_data )
759 {
760     intf_thread_t       *p_intf     = ( intf_thread_t* ) p_data;
761     intf_sys_t          *p_sys      = p_intf->p_sys;
762     playlist_t          *p_playlist;
763     input_thread_t      *p_input    = NULL;
764     (void)p_this; (void)psz_var; (void)oldval; (void)newval;
765
766     p_playlist = pl_Yield( p_intf );
767     PL_LOCK;
768     p_input = p_playlist->p_input;
769
770     if( !p_input )
771     {
772         PL_UNLOCK;
773         pl_Release( p_playlist );
774         return VLC_SUCCESS;
775     }
776
777     vlc_object_yield( p_input );
778     PL_UNLOCK;
779     pl_Release( p_playlist );
780
781     TrackChangeSignal( p_sys->p_conn, p_input );
782
783     vlc_object_release( p_input );
784     return VLC_SUCCESS;
785 }
786
787 /*****************************************************************************
788  * GetInputMeta: Fill a DBusMessage with the given input item metadata
789  *****************************************************************************/
790
791 #define ADD_META( entry, type, data ) \
792     if( data ) { \
793         dbus_message_iter_open_container( &dict, DBUS_TYPE_DICT_ENTRY, \
794                 NULL, &dict_entry ); \
795         dbus_message_iter_append_basic( &dict_entry, DBUS_TYPE_STRING, \
796                 &ppsz_meta_items[entry] ); \
797         dbus_message_iter_open_container( &dict_entry, DBUS_TYPE_VARIANT, \
798                 type##_AS_STRING, &variant ); \
799         dbus_message_iter_append_basic( &variant, \
800                 type, \
801                 & data ); \
802         dbus_message_iter_close_container( &dict_entry, &variant ); \
803         dbus_message_iter_close_container( &dict, &dict_entry ); }
804
805 #define ADD_VLC_META_STRING( entry, item ) \
806     { \
807         char * psz = input_item_Get##item( p_input );\
808         ADD_META( entry, DBUS_TYPE_STRING, \
809                   psz ); \
810         free( psz ); \
811     }
812
813 static int GetInputMeta( input_item_t* p_input,
814                         DBusMessageIter *args )
815 { /*FIXME: Works only for already read metadata. */
816
817     DBusMessageIter dict, dict_entry, variant;
818     /* We need the track length to be expressed in seconds
819      * instead of milliseconds */
820     dbus_int64_t i_length = ( input_item_GetDuration( p_input ) / 1000 );
821
822     const char* ppsz_meta_items[] =
823     {
824     "title", "artist", "genre", "copyright", "album", "tracknum",
825     "description", "rating", "date", "setting", "url", "language",
826     "nowplaying", "publisher", "encodedby", "arturl", "trackid",
827     "status", "URI", "length", "video-codec", "audio-codec",
828     "video-bitrate", "audio-bitrate", "audio-samplerate"
829     };
830
831     dbus_message_iter_open_container( args, DBUS_TYPE_ARRAY, "{sv}", &dict );
832
833     ADD_VLC_META_STRING( 0,  Title );
834     ADD_VLC_META_STRING( 1,  Artist );
835     ADD_VLC_META_STRING( 2,  Genre );
836     ADD_VLC_META_STRING( 3,  Copyright );
837     ADD_VLC_META_STRING( 4,  Album );
838     ADD_VLC_META_STRING( 5,  TrackNum );
839     ADD_VLC_META_STRING( 6,  Description );
840     ADD_VLC_META_STRING( 7,  Rating );
841     ADD_VLC_META_STRING( 8,  Date );
842     ADD_VLC_META_STRING( 9,  Setting );
843     ADD_VLC_META_STRING( 10, URL );
844     ADD_VLC_META_STRING( 11, Language );
845     ADD_VLC_META_STRING( 12, NowPlaying );
846     ADD_VLC_META_STRING( 13, Publisher );
847     ADD_VLC_META_STRING( 14, EncodedBy );
848     ADD_VLC_META_STRING( 15, ArtURL );
849     ADD_VLC_META_STRING( 16, TrackID );
850
851     vlc_mutex_lock( &p_input->lock );
852     if( p_input->p_meta )
853         ADD_META( 17, DBUS_TYPE_INT32, p_input->p_meta->i_status );
854     vlc_mutex_unlock( &p_input->lock );
855
856     ADD_VLC_META_STRING( 18, URI );
857     ADD_META( 19, DBUS_TYPE_INT64, i_length );
858
859     dbus_message_iter_close_container( args, &dict );
860     return VLC_SUCCESS;
861 }
862
863 #undef ADD_META
864 #undef ADD_VLC_META_STRING