telepathy.c 9.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/*****************************************************************************
 * telepathy.c : changes Telepathy Presence information using MissionControl
 *****************************************************************************
 * Copyright © 2007 the VideoLAN team
 * $Id$
 *
 * Author: Rafaël Carré <funman@videoanorg>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#include <vlc/vlc.h>
#include <vlc_interface.h>
#include <vlc_meta.h>
#include <vlc_playlist.h>
#include <vlc_strings.h>
#include <dbus/dbus.h>

/*****************************************************************************
 * intf_sys_t: description and status of log interface
 *****************************************************************************/
struct intf_sys_t
{
    char *psz_format;
    DBusConnection  *p_conn;
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open    ( vlc_object_t * );
static void Close   ( vlc_object_t * );

static int ItemChange( vlc_object_t *, const char *,
                       vlc_value_t, vlc_value_t, void * );
static int SendToTelepathy( intf_thread_t *, const char * );

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define FORMAT_DEFAULT "$a - $t"
#define FORMAT_TEXT N_("Title format string")
#define FORMAT_LONGTEXT N_("Format of the string to send to Telepathy." \
"Defaults to \"Artist - Title\" ($a - $t)." \
"You can use the following substitutions: " \
"$a Artist, $b Album, $c, Copyright, $d Description, $e Encoder, $g Genre, " \
"$l Language, $n number, $p Now Playing, $r Rating, $s Subtitles language, " \
"$t Title, $u URL, $A Date, $B Bitrate, $C Chapter, $D Duration, $F URI, " \
"$I Video Title, $L Time Remaining, $N Name, $O Audio language, $P Position, " \
"$R Rate, $S Sample rate, $T Time elapsed, $U Publisher, $V Volume")

vlc_module_begin();
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_CONTROL );
    set_shortname( "Telepathy" );
    set_description( _("Telepathy « Now Playing » using MissionControl") );

    add_string( "telepathy-format", FORMAT_DEFAULT, NULL,
                FORMAT_TEXT, FORMAT_LONGTEXT, VLC_FALSE );

    set_capability( "interface", 0 );
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    playlist_t *p_playlist;
    DBusConnection  *p_conn;
    DBusError       error;

    MALLOC_ERR( p_intf->p_sys, intf_sys_t );

    p_intf->p_sys->psz_format = config_GetPsz( p_intf, "telepathy-format" );
    if( !p_intf->p_sys->psz_format )
    {
        msg_Dbg( p_intf, "no format provided" );
        p_intf->p_sys->psz_format = strdup( FORMAT_DEFAULT );
    }
    msg_Dbg( p_intf, "using format: %s", p_intf->p_sys->psz_format );

    p_playlist = pl_Yield( p_intf );
    var_AddCallback( p_playlist, "item-change", ItemChange, p_intf );
    var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
    pl_Release( p_intf );

    dbus_threads_init_default();

    dbus_error_init( &error );

    /* connect to the session bus */
    p_conn = dbus_bus_get( DBUS_BUS_SESSION, &error );
    if( !p_conn )
    {
        msg_Err( p_this, "Failed to connect to the DBus session daemon: %s",
                error.message );
        dbus_error_free( &error );
        free( p_intf->p_sys );
        return VLC_EGENERIC;
    }
    p_intf->p_sys->p_conn = p_conn;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
    playlist_t *p_playlist = pl_Yield( p_this );

    /* Clears the Presence message ... else it looks like we're still playing
     * something although VLC (or the Telepathy plugin) is closed */

    /* Do not check for VLC_ENOMEM as we're closing */
    SendToTelepathy( p_intf, "" );

    var_DelCallback( p_playlist, "item-change", ItemChange, p_intf );
    var_DelCallback( p_playlist, "playlist-current", ItemChange, p_intf );
    pl_Release( p_this );

    /* we won't use the DBus connection anymore */
    dbus_connection_unref( p_intf->p_sys->p_conn );

    /* Destroy structure */
    free( p_intf->p_sys->psz_format );
    free( p_intf->p_sys );
}

/*****************************************************************************
 * ItemChange: Playlist item change callback
 *****************************************************************************/
static int ItemChange( vlc_object_t *p_this, const char *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *param )
{
    intf_thread_t *p_intf = (intf_thread_t *)param;
    char *psz_buf = NULL;
    input_thread_t *p_input;
    playlist_t *p_playlist = pl_Yield( p_this );

    p_input = p_playlist->p_input;
    pl_Release( p_this );

    if( !p_input ) return VLC_SUCCESS;
    vlc_object_yield( p_input );

    if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
    {
        vlc_object_release( p_input );
        /* Not playing anything ... */
        switch( SendToTelepathy( p_intf, "" ) )
        {
            case VLC_ENOMEM:
                Close( p_this );
                return VLC_ENOMEM;
            default:
                return VLC_SUCCESS;
        }
    }

    /* We format the string to be displayed */
    psz_buf = str_format_meta( p_this, p_intf->p_sys->psz_format );
    /* We don't need the input anymore */
    vlc_object_release( p_input );

    if( SendToTelepathy( p_intf, psz_buf ) == VLC_ENOMEM )
    {
        free( psz_buf );
        Close( p_this );
        return VLC_ENOMEM;
    }
    free( psz_buf );
    return VLC_SUCCESS;
}

/*****************************************************************************
 * SendToTelepathy
 *****************************************************************************/
static int SendToTelepathy( intf_thread_t *p_intf, const char *psz_msg )
{
    DBusConnection *p_conn;
    DBusMessage *p_msg;
    DBusMessage *p_reply;
    DBusMessageIter args;
    DBusError error;
    dbus_error_init( &error );
    dbus_uint32_t i_status;

    p_conn = p_intf->p_sys->p_conn;

    /* first we need to get the actual status */
    p_msg = dbus_message_new_method_call(
            "org.freedesktop.Telepathy.MissionControl",
           "/org/freedesktop/Telepathy/MissionControl",
            "org.freedesktop.Telepathy.MissionControl",
            "GetPresence" );
    if( !p_msg )
        return VLC_ENOMEM;

    p_reply = dbus_connection_send_with_reply_and_block( p_conn, p_msg,
        1000, &error );

    dbus_message_unref( p_msg );
    if( p_reply == NULL )
    {   /* MC is not active, or too slow. Better luck next time? */
        return VLC_SUCCESS;
    }

    /* extract the status from the reply */
    if( dbus_message_get_args( p_reply, &error,
            DBUS_TYPE_UINT32, &i_status,
            DBUS_TYPE_INVALID ) == FALSE )
    {
        return VLC_ENOMEM;
    }

    p_msg = dbus_message_new_method_call(
            "org.freedesktop.Telepathy.MissionControl",
           "/org/freedesktop/Telepathy/MissionControl",
            "org.freedesktop.Telepathy.MissionControl",
            "SetPresence" );
    if( !p_msg )
        return VLC_ENOMEM;

    dbus_message_iter_init_append( p_msg, &args );

    /* first argument is the status */
    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_UINT32, &i_status ) )
    {
        dbus_message_unref( p_msg );
        return VLC_ENOMEM;
    }
    /* second argument is the message */
    if( !dbus_message_iter_append_basic( &args, DBUS_TYPE_STRING, &psz_msg ) )
    {
        dbus_message_unref( p_msg );
        return VLC_ENOMEM;
    }


    if( !dbus_connection_send( p_conn, p_msg, NULL ) )
        return VLC_ENOMEM;

    dbus_connection_flush( p_conn );
    dbus_message_unref( p_msg );

    return VLC_SUCCESS;
}