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_error_init( &error );
111 /* connect to the session bus */
112 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
115 msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
117 dbus_error_free( &error );
118 free( p_intf->p_sys );
121 p_intf->p_sys->p_conn = p_conn;
125 /*****************************************************************************
126 * Close: destroy interface stuff
127 *****************************************************************************/
128 static void Close( vlc_object_t *p_this )
130 intf_thread_t *p_intf = (intf_thread_t *)p_this;
131 playlist_t *p_playlist = pl_Yield( p_this );
133 /* Clears the Presence message ... else it looks like we're still playing
134 * something although VLC (or the Telepathy plugin) is closed */
136 /* Do not check for VLC_ENOMEM as we're closing */
137 SendToTelepathy( p_intf, "" );
139 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
140 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
141 pl_Release( p_this );
143 /* we won't use the DBus connection anymore */
144 dbus_connection_unref( p_intf->p_sys->p_conn );
146 /* Destroy structure */
147 free( p_intf->p_sys->psz_format );
148 free( p_intf->p_sys );
151 /*****************************************************************************
152 * ItemChange: Playlist item change callback
153 *****************************************************************************/
154 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
155 vlc_value_t oldval, vlc_value_t newval, void *param )
157 intf_thread_t *p_intf = (intf_thread_t *)param;
158 char *psz_buf = NULL;
159 input_thread_t *p_input;
161 /* Don't update Telepathy presence each time an item has been preparsed */
162 if( !strncmp( "playlist-current", psz_var, 16 ) )
163 { /* stores the current input item id */
164 p_intf->p_sys->i_id = newval.i_int;
166 else if( newval.i_int != p_intf->p_sys->i_id ) /* "item-change" */
170 playlist_t *p_playlist = pl_Yield( p_this );
172 p_input = p_playlist->p_input;
173 pl_Release( p_this );
175 if( !p_input ) return VLC_SUCCESS;
176 vlc_object_yield( p_input );
178 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
180 vlc_object_release( p_input );
181 /* Not playing anything ... */
182 switch( SendToTelepathy( p_intf, "" ) )
192 /* We format the string to be displayed */
193 psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
194 /* We don't need the input anymore */
195 vlc_object_release( p_input );
197 if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
207 /*****************************************************************************
209 *****************************************************************************/
210 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
212 DBusConnection *p_conn;
214 DBusMessage *p_reply;
215 DBusMessageIter args;
217 dbus_error_init( &error );
218 dbus_uint32_t i_status;
220 p_conn = p_intf->p_sys->p_conn;
222 /* first we need to get the actual status */
223 p_msg = dbus_message_new_method_call(
224 "org.freedesktop.Telepathy.MissionControl",
225 "/org/freedesktop/Telepathy/MissionControl",
226 "org.freedesktop.Telepathy.MissionControl",
231 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
232 50, &error ); /* blocks 50ms maximum */
234 dbus_message_unref( p_msg );
235 if( p_reply == NULL )
236 { /* MC is not active, or too slow. Better luck next time? */
240 /* extract the status from the reply */
241 if( dbus_message_get_args( p_reply, &error,
242 DBUS_TYPE_UINT32, &i_status,
243 DBUS_TYPE_INVALID ) == FALSE )
248 p_msg = dbus_message_new_method_call(
249 "org.freedesktop.Telepathy.MissionControl",
250 "/org/freedesktop/Telepathy/MissionControl",
251 "org.freedesktop.Telepathy.MissionControl",
256 dbus_message_iter_init_append( p_msg, &args );
258 /* first argument is the status */
259 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
261 dbus_message_unref( p_msg );
264 /* second argument is the message */
265 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
267 dbus_message_unref( p_msg );
272 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
275 dbus_connection_flush( p_conn );
276 dbus_message_unref( p_msg );