1 /*****************************************************************************
2 * telepathy.c : changes Telepathy Presence information using MissionControl
3 *****************************************************************************
4 * Copyright © 2007 the VideoLAN team
7 * Author: Rafaël Carré <funman@videoanorg>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
29 #include <vlc_interface.h>
31 #include <vlc_playlist.h>
32 #include <vlc_strings.h>
33 #include <dbus/dbus.h>
35 /*****************************************************************************
36 * intf_sys_t: description and status of log interface
37 *****************************************************************************/
41 DBusConnection *p_conn;
45 /*****************************************************************************
47 *****************************************************************************/
48 static int Open ( vlc_object_t * );
49 static void Close ( vlc_object_t * );
51 static int ItemChange( vlc_object_t *, const char *,
52 vlc_value_t, vlc_value_t, void * );
53 static int SendToTelepathy( intf_thread_t *, const char * );
55 /*****************************************************************************
57 *****************************************************************************/
58 #define FORMAT_DEFAULT "$a - $t"
59 #define FORMAT_TEXT N_("Title format string")
60 #define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
61 "Defaults to \"Artist - Title\" ($a - $t). " \
62 "You can use the following substitutions: " \
63 "$a Artist, $b Album, $c Copyright, $d Description, $e Encoder, $g Genre, " \
64 "$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
65 "$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
66 "$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
67 "$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")
70 set_category( CAT_INTERFACE );
71 set_subcategory( SUBCAT_INTERFACE_CONTROL );
72 set_shortname( "Telepathy" );
73 set_description( _("Telepathy \"Now Playing\" using MissionControl") );
75 add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
76 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
78 set_capability( "interface", 0 );
79 set_callbacks( Open, Close );
82 /*****************************************************************************
83 * Open: initialize and create stuff
84 *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
87 intf_thread_t *p_intf = (intf_thread_t *)p_this;
88 playlist_t *p_playlist;
89 DBusConnection *p_conn;
92 MALLOC_ERR( p_intf->p_sys, intf_sys_t );
94 p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
95 if( !p_intf->p_sys->psz_format )
97 msg_Dbg( p_intf, "no format provided" );
98 p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
100 msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
102 p_intf->p_sys->i_id = -1;
104 p_playlist = pl_Yield( p_intf );
105 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
106 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
107 pl_Release( p_intf );
109 dbus_threads_init_default();
111 dbus_error_init( &error );
113 /* connect to the session bus */
114 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
117 msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
119 dbus_error_free( &error );
120 free( p_intf->p_sys );
123 p_intf->p_sys->p_conn = p_conn;
127 /*****************************************************************************
128 * Close: destroy interface stuff
129 *****************************************************************************/
130 static void Close( vlc_object_t *p_this )
132 intf_thread_t *p_intf = (intf_thread_t *)p_this;
133 playlist_t *p_playlist = pl_Yield( p_this );
135 /* Clears the Presence message ... else it looks like we're still playing
136 * something although VLC (or the Telepathy plugin) is closed */
138 /* Do not check for VLC_ENOMEM as we're closing */
139 SendToTelepathy( p_intf, "" );
141 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
142 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
143 pl_Release( p_this );
145 /* we won't use the DBus connection anymore */
146 dbus_connection_unref( p_intf->p_sys->p_conn );
148 /* Destroy structure */
149 free( p_intf->p_sys->psz_format );
150 free( p_intf->p_sys );
153 /*****************************************************************************
154 * ItemChange: Playlist item change callback
155 *****************************************************************************/
156 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
157 vlc_value_t oldval, vlc_value_t newval, void *param )
159 intf_thread_t *p_intf = (intf_thread_t *)param;
160 char *psz_buf = NULL;
161 input_thread_t *p_input;
163 /* Don't update Telepathy presence each time an item has been preparsed */
164 if( !strncmp( "playlist-current", psz_var, 16 ) )
165 { /* stores the current input item id */
166 p_intf->p_sys->i_id = newval.i_int;
168 else if( newval.i_int != p_intf->p_sys->i_id ) /* "item-change" */
172 playlist_t *p_playlist = pl_Yield( p_this );
174 p_input = p_playlist->p_input;
175 pl_Release( p_this );
177 if( !p_input ) return VLC_SUCCESS;
178 vlc_object_yield( p_input );
180 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
182 vlc_object_release( p_input );
183 /* Not playing anything ... */
184 switch( SendToTelepathy( p_intf, "" ) )
194 /* We format the string to be displayed */
195 psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
196 /* We don't need the input anymore */
197 vlc_object_release( p_input );
199 if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
209 /*****************************************************************************
211 *****************************************************************************/
212 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
214 DBusConnection *p_conn;
216 DBusMessage *p_reply;
217 DBusMessageIter args;
219 dbus_error_init( &error );
220 dbus_uint32_t i_status;
222 p_conn = p_intf->p_sys->p_conn;
224 /* first we need to get the actual status */
225 p_msg = dbus_message_new_method_call(
226 "org.freedesktop.Telepathy.MissionControl",
227 "/org/freedesktop/Telepathy/MissionControl",
228 "org.freedesktop.Telepathy.MissionControl",
233 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
234 50, &error ); /* blocks 50ms maximum */
236 dbus_message_unref( p_msg );
237 if( p_reply == NULL )
238 { /* MC is not active, or too slow. Better luck next time? */
242 /* extract the status from the reply */
243 if( dbus_message_get_args( p_reply, &error,
244 DBUS_TYPE_UINT32, &i_status,
245 DBUS_TYPE_INVALID ) == FALSE )
250 p_msg = dbus_message_new_method_call(
251 "org.freedesktop.Telepathy.MissionControl",
252 "/org/freedesktop/Telepathy/MissionControl",
253 "org.freedesktop.Telepathy.MissionControl",
258 dbus_message_iter_init_append( p_msg, &args );
260 /* first argument is the status */
261 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
263 dbus_message_unref( p_msg );
266 /* second argument is the message */
267 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
269 dbus_message_unref( p_msg );
274 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
277 dbus_connection_flush( p_conn );
278 dbus_message_unref( p_msg );