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 /* connect to the session bus */
93 dbus_error_init( &error );
94 p_intf->p_sys->p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
95 if( !p_intf->p_sys->p_conn )
97 msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
99 dbus_error_free( &error );
100 free( p_intf->p_sys );
104 p_intf->p_sys->psz_format = var_InheritString( p_intf, "input-title-format" );
105 if( !p_intf->p_sys->psz_format )
107 p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
109 msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
111 p_intf->p_sys->i_id = -1;
113 p_playlist = pl_Get( p_intf );
114 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
115 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
120 /*****************************************************************************
121 * Close: destroy interface stuff
122 *****************************************************************************/
123 static void Close( vlc_object_t *p_this )
125 intf_thread_t *p_intf = (intf_thread_t *)p_this;
126 playlist_t *p_playlist = pl_Get( p_this );
127 input_thread_t *p_input = NULL;
129 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
130 var_DelCallback( p_playlist, "item-current", ItemChange, p_intf );
131 if( (p_input = playlist_CurrentInput( p_playlist )) )
133 var_DelCallback( p_input, "state", StateChange, p_intf );
134 vlc_object_release( p_input );
137 /* Clears the Presence message ... else it looks like we're still playing
138 * something although VLC (or the Telepathy plugin) is closed */
140 /* Do not check for VLC_ENOMEM as we're closing */
141 SendToTelepathy( p_intf, "" );
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 )
158 intf_thread_t *p_intf = (intf_thread_t *)param;
159 playlist_t* p_playlist = (playlist_t*) p_this;
160 char *psz_buf = NULL;
161 input_thread_t *p_input;
162 input_item_t *p_item = newval.p_address;
163 bool b_is_item_current = !strcmp( "item-current", psz_var );
165 /* Don't update Telepathy presence each time an item has been preparsed */
166 if( b_is_item_current )
167 { /* stores the current input item id */
168 p_intf->p_sys->i_id = p_item->i_id;
169 p_intf->p_sys->i_item_changes = 0;
174 if( p_item->i_id != p_intf->p_sys->i_id ) /* "item-change" */
176 /* Some variable bitrate inputs call "item-change callbacks each time
177 * their length is updated, that is several times per second.
178 * We'll limit the number of changes to 10 per input. */
179 if( p_intf->p_sys->i_item_changes > 10 )
181 p_intf->p_sys->i_item_changes++;
184 p_input = playlist_CurrentInput( p_playlist );
186 if( !p_input ) return VLC_SUCCESS;
188 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
190 vlc_object_release( p_input );
191 /* Not playing anything ... */
192 switch( SendToTelepathy( p_intf, "" ) )
201 if( b_is_item_current )
202 var_AddCallback( p_input, "state", StateChange, p_intf );
204 /* We format the string to be displayed */
205 psz_buf = str_format_meta( (vlc_object_t*) p_intf,
206 p_intf->p_sys->psz_format );
208 /* We don't need the input anymore */
209 vlc_object_release( p_input );
211 if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
220 /*****************************************************************************
221 * StateChange: State change callback
222 *****************************************************************************/
223 static int StateChange( vlc_object_t *p_this, const char *psz_var,
224 vlc_value_t oldval, vlc_value_t newval, void *param )
226 VLC_UNUSED(p_this); VLC_UNUSED(psz_var); VLC_UNUSED(oldval);
227 intf_thread_t *p_intf = (intf_thread_t *)param;
228 if( newval.i_int >= END_S )
229 return SendToTelepathy( p_intf, "" );
233 /*****************************************************************************
235 *****************************************************************************/
236 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
238 DBusConnection *p_conn;
240 DBusMessage *p_reply;
241 DBusMessageIter args;
243 dbus_error_init( &error );
244 dbus_uint32_t i_status;
246 p_conn = p_intf->p_sys->p_conn;
248 /* first we need to get the actual status */
249 p_msg = dbus_message_new_method_call(
250 "org.freedesktop.Telepathy.MissionControl",
251 "/org/freedesktop/Telepathy/MissionControl",
252 "org.freedesktop.Telepathy.MissionControl",
257 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
258 50, &error ); /* blocks 50ms maximum */
260 if( dbus_error_is_set( &error ) )
261 dbus_error_free( &error );
263 dbus_message_unref( p_msg );
264 if( p_reply == NULL )
265 { /* MC is not active, or too slow. Better luck next time? */
269 /* extract the status from the reply */
270 if( dbus_message_get_args( p_reply, &error,
271 DBUS_TYPE_UINT32, &i_status,
272 DBUS_TYPE_INVALID ) == FALSE )
277 p_msg = dbus_message_new_method_call(
278 "org.freedesktop.Telepathy.MissionControl",
279 "/org/freedesktop/Telepathy/MissionControl",
280 "org.freedesktop.Telepathy.MissionControl",
285 dbus_message_iter_init_append( p_msg, &args );
287 /* first argument is the status */
288 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
290 dbus_message_unref( p_msg );
293 /* second argument is the message */
294 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
296 dbus_message_unref( p_msg );
301 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
304 dbus_connection_flush( p_conn );
305 dbus_message_unref( p_msg );