1 /*****************************************************************************
2 * telepathy.c : changes Telepathy Presence information using MissionControl
3 *****************************************************************************
4 * Copyright © 2007-2009 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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
36 #include <vlc_strings.h>
38 #include <dbus/dbus.h>
40 /*****************************************************************************
41 * intf_sys_t: description and status of log interface
42 *****************************************************************************/
46 DBusConnection *p_conn;
51 /*****************************************************************************
53 *****************************************************************************/
54 static int Open ( vlc_object_t * );
55 static void Close ( vlc_object_t * );
57 static int ItemChange( vlc_object_t *, const char *,
58 vlc_value_t, vlc_value_t, void * );
59 static int StateChange( vlc_object_t *, const char *,
60 vlc_value_t, vlc_value_t, void * );
61 static int SendToTelepathy( intf_thread_t *, const char * );
63 /*****************************************************************************
65 *****************************************************************************/
66 #define FORMAT_DEFAULT "$a - $t"
67 #define FORMAT_TEXT N_("Title format string")
68 #define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
69 "Defaults to \"Artist - Title\" ($a - $t). " \
70 "You can use the following substitutions: " \
71 "$a Artist, $b Album, $c Copyright, $d Description, $e Encoder, $g Genre, " \
72 "$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
73 "$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
74 "$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
75 "$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")
78 set_category( CAT_INTERFACE )
79 set_subcategory( SUBCAT_INTERFACE_CONTROL )
80 set_shortname( "Telepathy" )
81 set_description( N_("Telepathy \"Now Playing\" (MissionControl)") )
83 add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
84 FORMAT_TEXT, FORMAT_LONGTEXT, false )
86 set_capability( "interface", 0 )
87 set_callbacks( Open, Close )
90 /*****************************************************************************
91 * Open: initialize and create stuff
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 intf_thread_t *p_intf = (intf_thread_t *)p_this;
96 playlist_t *p_playlist;
99 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
103 /* connect to the session bus */
104 dbus_error_init( &error );
105 p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
106 if( !p_intf->p_sys->p_conn )
108 msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
110 dbus_error_free( &error );
111 free( p_intf->p_sys );
115 p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
116 if( !p_intf->p_sys->psz_format )
118 msg_Dbg( p_intf, "no format provided" );
119 p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
121 msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
123 p_intf->p_sys->i_id = -1;
125 p_playlist = pl_Hold( p_intf );
126 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
127 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
128 pl_Release( p_intf );
133 /*****************************************************************************
134 * Close: destroy interface stuff
135 *****************************************************************************/
136 static void Close( vlc_object_t *p_this )
138 intf_thread_t *p_intf = (intf_thread_t *)p_this;
139 playlist_t *p_playlist = pl_Hold( p_this );
140 input_thread_t *p_input = NULL;
142 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
143 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
144 if( (p_input = playlist_CurrentInput( p_playlist )) )
146 var_DelCallback( p_input, "state", StateChange, p_intf );
147 vlc_object_release( p_input );
149 pl_Release( p_this );
151 /* Clears the Presence message ... else it looks like we're still playing
152 * something although VLC (or the Telepathy plugin) is closed */
154 /* Do not check for VLC_ENOMEM as we're closing */
155 SendToTelepathy( p_intf, "" );
157 /* we won't use the DBus connection anymore */
158 dbus_connection_unref( p_intf->p_sys->p_conn );
160 /* Destroy structure */
161 free( p_intf->p_sys->psz_format );
162 free( p_intf->p_sys );
165 /*****************************************************************************
166 * ItemChange: Playlist item change callback
167 *****************************************************************************/
168 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
169 vlc_value_t oldval, vlc_value_t newval, void *param )
172 intf_thread_t *p_intf = (intf_thread_t *)param;
173 playlist_t* p_playlist = (playlist_t*) p_this;
174 char *psz_buf = NULL;
175 input_thread_t *p_input;
176 input_item_t *p_item = newval.p_address;
177 bool b_is_item_current = !strcmp( "item-current", psz_var );
179 /* Don't update Telepathy presence each time an item has been preparsed */
180 if( b_is_item_current )
181 { /* stores the current input item id */
182 p_intf->p_sys->i_id = p_item->i_id;
183 p_intf->p_sys->i_item_changes = 0;
188 if( p_item->i_id != p_intf->p_sys->i_id ) /* "item-change" */
190 /* Some variable bitrate inputs call "item-change callbacks each time
191 * their length is updated, that is several times per second.
192 * We'll limit the number of changes to 10 per input. */
193 if( p_intf->p_sys->i_item_changes > 10 )
195 p_intf->p_sys->i_item_changes++;
198 p_input = playlist_CurrentInput( p_playlist );
200 if( !p_input ) return VLC_SUCCESS;
202 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
204 vlc_object_release( p_input );
205 /* Not playing anything ... */
206 switch( SendToTelepathy( p_intf, "" ) )
215 if( b_is_item_current )
216 var_AddCallback( p_input, "state", StateChange, p_intf );
218 /* We format the string to be displayed */
219 psz_buf = str_format_meta( (vlc_object_t*) p_intf,
220 p_intf->p_sys->psz_format );
222 /* We don't need the input anymore */
223 vlc_object_release( p_input );
225 if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
234 /*****************************************************************************
235 * StateChange: State change callback
236 *****************************************************************************/
237 static int StateChange( vlc_object_t *p_this, const char *psz_var,
238 vlc_value_t oldval, vlc_value_t newval, void *param )
240 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
241 intf_thread_t *p_intf = (intf_thread_t *)param;
242 if( newval.i_int >= END_S )
243 return SendToTelepathy( p_intf, "" );
247 /*****************************************************************************
249 *****************************************************************************/
250 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
252 DBusConnection *p_conn;
254 DBusMessage *p_reply;
255 DBusMessageIter args;
257 dbus_error_init( &error );
258 dbus_uint32_t i_status;
260 p_conn = p_intf->p_sys->p_conn;
262 /* first we need to get the actual status */
263 p_msg = dbus_message_new_method_call(
264 "org.freedesktop.Telepathy.MissionControl",
265 "/org/freedesktop/Telepathy/MissionControl",
266 "org.freedesktop.Telepathy.MissionControl",
271 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
272 50, &error ); /* blocks 50ms maximum */
274 dbus_message_unref( p_msg );
275 if( p_reply == NULL )
276 { /* MC is not active, or too slow. Better luck next time? */
280 /* extract the status from the reply */
281 if( dbus_message_get_args( p_reply, &error,
282 DBUS_TYPE_UINT32, &i_status,
283 DBUS_TYPE_INVALID ) == FALSE )
288 p_msg = dbus_message_new_method_call(
289 "org.freedesktop.Telepathy.MissionControl",
290 "/org/freedesktop/Telepathy/MissionControl",
291 "org.freedesktop.Telepathy.MissionControl",
296 dbus_message_iter_init_append( p_msg, &args );
298 /* first argument is the status */
299 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
301 dbus_message_unref( p_msg );
304 /* second argument is the message */
305 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
307 dbus_message_unref( p_msg );
312 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
315 dbus_connection_flush( p_conn );
316 dbus_message_unref( p_msg );