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> /* str_format_meta */
38 #include <dbus/dbus.h>
40 #define FORMAT_DEFAULT "$a - $t"
41 /*****************************************************************************
42 * intf_sys_t: description and status of log interface
43 *****************************************************************************/
47 DBusConnection *p_conn;
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close ( vlc_object_t * );
58 static int ItemChange( vlc_object_t *, const char *,
59 vlc_value_t, vlc_value_t, void * );
60 static int StateChange( vlc_object_t *, const char *,
61 vlc_value_t, vlc_value_t, void * );
62 static int SendToTelepathy( intf_thread_t *, const char * );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_category( CAT_INTERFACE )
69 set_subcategory( SUBCAT_INTERFACE_CONTROL )
70 set_shortname( "Telepathy" )
71 set_description( N_("Telepathy \"Now Playing\" (MissionControl)") )
73 add_obsolete_string( "telepathy-format")
75 set_capability( "interface", 0 )
76 set_callbacks( Open, Close )
79 /*****************************************************************************
80 * Open: initialize and create stuff
81 *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
84 intf_thread_t *p_intf = (intf_thread_t *)p_this;
85 playlist_t *p_playlist;
88 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
92 dbus_error_init( &error );
94 /* connect privately to the session bus
95 * the connection will not be shared with other vlc modules which use dbus,
96 * thus avoiding a whole class of concurrency issues */
97 p_intf->p_sys->p_conn = dbus_bus_get_private( DBUS_BUS_SESSION, &error );
98 if( !p_intf->p_sys->p_conn )
100 msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
102 dbus_error_free( &error );
103 free( p_intf->p_sys );
107 p_intf->p_sys->psz_format = var_InheritString( p_intf, "input-title-format" );
108 if( !p_intf->p_sys->psz_format )
110 p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
112 msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
114 p_intf->p_sys->i_id = -1;
116 p_playlist = pl_Get( p_intf );
117 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
118 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
123 /*****************************************************************************
124 * Close: destroy interface stuff
125 *****************************************************************************/
126 static void Close( vlc_object_t *p_this )
128 intf_thread_t *p_intf = (intf_thread_t *)p_this;
129 playlist_t *p_playlist = pl_Get( p_this );
130 input_thread_t *p_input = NULL;
132 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
133 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
134 if( (p_input = playlist_CurrentInput( p_playlist )) )
136 var_DelCallback( p_input, "state", StateChange, p_intf );
137 vlc_object_release( p_input );
140 /* Clears the Presence message ... else it looks like we're still playing
141 * something although VLC (or the Telepathy plugin) is closed */
143 /* Do not check for VLC_ENOMEM as we're closing */
144 SendToTelepathy( p_intf, "" );
146 /* The dbus connection is private,
147 * so we are responsible for closing it */
148 dbus_connection_close( p_intf->p_sys->p_conn );
149 dbus_connection_unref( p_intf->p_sys->p_conn );
151 /* Destroy structure */
152 free( p_intf->p_sys->psz_format );
153 free( p_intf->p_sys );
156 /*****************************************************************************
157 * ItemChange: Playlist item change callback
158 *****************************************************************************/
159 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
160 vlc_value_t oldval, vlc_value_t newval, void *param )
163 intf_thread_t *p_intf = (intf_thread_t *)param;
164 playlist_t* p_playlist = (playlist_t*) p_this;
165 char *psz_buf = NULL;
166 input_thread_t *p_input;
167 input_item_t *p_item = newval.p_address;
168 bool b_is_item_current = !strcmp( "item-current", psz_var );
170 /* Don't update Telepathy presence each time an item has been preparsed */
171 if( b_is_item_current )
172 { /* stores the current input item id */
173 p_intf->p_sys->i_id = p_item->i_id;
174 p_intf->p_sys->i_item_changes = 0;
179 if( p_item->i_id != p_intf->p_sys->i_id ) /* "item-change" */
181 /* Some variable bitrate inputs call "item-change callbacks each time
182 * their length is updated, that is several times per second.
183 * We'll limit the number of changes to 10 per input. */
184 if( p_intf->p_sys->i_item_changes > 10 )
186 p_intf->p_sys->i_item_changes++;
189 p_input = playlist_CurrentInput( p_playlist );
191 if( !p_input ) return VLC_SUCCESS;
193 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
195 vlc_object_release( p_input );
196 /* Not playing anything ... */
197 switch( SendToTelepathy( p_intf, "" ) )
206 if( b_is_item_current )
207 var_AddCallback( p_input, "state", StateChange, p_intf );
209 /* We format the string to be displayed */
210 psz_buf = str_format_meta( (vlc_object_t*) p_intf,
211 p_intf->p_sys->psz_format );
213 /* We don't need the input anymore */
214 vlc_object_release( p_input );
216 if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
225 /*****************************************************************************
226 * StateChange: State change callback
227 *****************************************************************************/
228 static int StateChange( vlc_object_t *p_this, const char *psz_var,
229 vlc_value_t oldval, vlc_value_t newval, void *param )
231 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
232 intf_thread_t *p_intf = (intf_thread_t *)param;
233 if( newval.i_int >= END_S )
234 return SendToTelepathy( p_intf, "" );
238 /*****************************************************************************
240 *****************************************************************************/
241 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
243 DBusConnection *p_conn;
245 DBusMessage *p_reply;
246 DBusMessageIter args;
248 dbus_error_init( &error );
249 dbus_uint32_t i_status;
251 p_conn = p_intf->p_sys->p_conn;
253 /* first we need to get the actual status */
254 p_msg = dbus_message_new_method_call(
255 "org.freedesktop.Telepathy.MissionControl",
256 "/org/freedesktop/Telepathy/MissionControl",
257 "org.freedesktop.Telepathy.MissionControl",
262 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
263 50, &error ); /* blocks 50ms maximum */
265 if( dbus_error_is_set( &error ) )
266 dbus_error_free( &error );
268 dbus_message_unref( p_msg );
269 if( p_reply == NULL )
270 { /* MC is not active, or too slow. Better luck next time? */
274 /* extract the status from the reply */
275 if( dbus_message_get_args( p_reply, &error,
276 DBUS_TYPE_UINT32, &i_status,
277 DBUS_TYPE_INVALID ) == FALSE )
282 p_msg = dbus_message_new_method_call(
283 "org.freedesktop.Telepathy.MissionControl",
284 "/org/freedesktop/Telepathy/MissionControl",
285 "org.freedesktop.Telepathy.MissionControl",
290 dbus_message_iter_init_append( p_msg, &args );
292 /* first argument is the status */
293 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
295 dbus_message_unref( p_msg );
298 /* second argument is the message */
299 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
301 dbus_message_unref( p_msg );
306 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
309 dbus_connection_flush( p_conn );
310 dbus_message_unref( p_msg );