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;
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close ( vlc_object_t * );
52 static int ItemChange( vlc_object_t *, const char *,
53 vlc_value_t, vlc_value_t, void * );
54 static int SendToTelepathy( intf_thread_t *, const char * );
56 /*****************************************************************************
58 *****************************************************************************/
59 #define FORMAT_DEFAULT "$a - $t"
60 #define FORMAT_TEXT N_("Title format string")
61 #define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
62 "Defaults to \"Artist - Title\" ($a - $t). " \
63 "You can use the following substitutions: " \
64 "$a Artist, $b Album, $c Copyright, $d Description, $e Encoder, $g Genre, " \
65 "$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
66 "$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
67 "$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
68 "$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")
71 set_category( CAT_INTERFACE );
72 set_subcategory( SUBCAT_INTERFACE_CONTROL );
73 set_shortname( "Telepathy" );
74 set_description( _("Telepathy \"Now Playing\" using MissionControl") );
76 add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
77 FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );
79 set_capability( "interface", 0 );
80 set_callbacks( Open, Close );
83 /*****************************************************************************
84 * Open: initialize and create stuff
85 *****************************************************************************/
86 static int Open( vlc_object_t *p_this )
88 intf_thread_t *p_intf = (intf_thread_t *)p_this;
89 playlist_t *p_playlist;
90 DBusConnection *p_conn;
93 MALLOC_ERR( p_intf->p_sys, intf_sys_t );
95 p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
96 if( !p_intf->p_sys->psz_format )
98 msg_Dbg( p_intf, "no format provided" );
99 p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
101 msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );
103 p_intf->p_sys->i_id = -1;
105 p_playlist = pl_Yield( p_intf );
106 var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
107 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
108 pl_Release( p_intf );
110 dbus_error_init( &error );
112 /* connect to the session bus */
113 p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
116 msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
118 dbus_error_free( &error );
119 free( p_intf->p_sys );
122 p_intf->p_sys->p_conn = p_conn;
126 /*****************************************************************************
127 * Close: destroy interface stuff
128 *****************************************************************************/
129 static void Close( vlc_object_t *p_this )
131 intf_thread_t *p_intf = (intf_thread_t *)p_this;
132 playlist_t *p_playlist = pl_Yield( p_this );
134 /* Clears the Presence message ... else it looks like we're still playing
135 * something although VLC (or the Telepathy plugin) is closed */
137 /* Do not check for VLC_ENOMEM as we're closing */
138 SendToTelepathy( p_intf, "" );
140 var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
141 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
142 pl_Release( p_this );
144 /* we won't use the DBus connection anymore */
145 dbus_connection_unref( p_intf->p_sys->p_conn );
147 /* Destroy structure */
148 free( p_intf->p_sys->psz_format );
149 free( p_intf->p_sys );
152 /*****************************************************************************
153 * ItemChange: Playlist item change callback
154 *****************************************************************************/
155 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
156 vlc_value_t oldval, vlc_value_t newval, void *param )
158 intf_thread_t *p_intf = (intf_thread_t *)param;
159 char *psz_buf = NULL;
160 input_thread_t *p_input;
162 /* Don't update Telepathy presence each time an item has been preparsed */
163 if( !strncmp( "playlist-current", psz_var, 16 ) )
164 { /* stores the current input item id */
165 p_intf->p_sys->i_id = newval.i_int;
166 p_intf->p_sys->i_item_changes = 0;
170 if( newval.i_int != p_intf->p_sys->i_id ) /* "item-change" */
172 /* Some variable bitrate inputs call "item-change callbacks each time
173 * their length is updated, that is several times per second.
174 * We'll limit the number of changes to 10 per input. */
175 if( p_intf->p_sys->i_item_changes > 10 )
177 p_intf->p_sys->i_item_changes++;
181 playlist_t *p_playlist = pl_Yield( p_this );
183 p_input = p_playlist->p_input;
184 pl_Release( p_this );
186 if( !p_input ) return VLC_SUCCESS;
187 vlc_object_yield( p_input );
189 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
191 vlc_object_release( p_input );
192 /* Not playing anything ... */
193 switch( SendToTelepathy( p_intf, "" ) )
203 /* We format the string to be displayed */
204 psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
205 /* We don't need the input anymore */
206 vlc_object_release( p_input );
208 if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
218 /*****************************************************************************
220 *****************************************************************************/
221 static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
223 DBusConnection *p_conn;
225 DBusMessage *p_reply;
226 DBusMessageIter args;
228 dbus_error_init( &error );
229 dbus_uint32_t i_status;
231 p_conn = p_intf->p_sys->p_conn;
233 /* first we need to get the actual status */
234 p_msg = dbus_message_new_method_call(
235 "org.freedesktop.Telepathy.MissionControl",
236 "/org/freedesktop/Telepathy/MissionControl",
237 "org.freedesktop.Telepathy.MissionControl",
242 p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
243 50, &error ); /* blocks 50ms maximum */
245 dbus_message_unref( p_msg );
246 if( p_reply == NULL )
247 { /* MC is not active, or too slow. Better luck next time? */
251 /* extract the status from the reply */
252 if( dbus_message_get_args( p_reply, &error,
253 DBUS_TYPE_UINT32, &i_status,
254 DBUS_TYPE_INVALID ) == FALSE )
259 p_msg = dbus_message_new_method_call(
260 "org.freedesktop.Telepathy.MissionControl",
261 "/org/freedesktop/Telepathy/MissionControl",
262 "org.freedesktop.Telepathy.MissionControl",
267 dbus_message_iter_init_append( p_msg, &args );
269 /* first argument is the status */
270 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
272 dbus_message_unref( p_msg );
275 /* second argument is the message */
276 if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
278 dbus_message_unref( p_msg );
283 if( !dbus_connection_send( p_conn, p_msg, NULL ) )
286 dbus_connection_flush( p_conn );
287 dbus_message_unref( p_msg );