notify.c 10.4 KB
Newer Older
Christophe Mutricy's avatar
Christophe Mutricy committed
1 2 3
/*****************************************************************************
 * notify.c : libnotify notification plugin
 *****************************************************************************
Rafaël Carré's avatar
Rafaël Carré committed
4
 * Copyright (C) 2006-2007 the VideoLAN team
Christophe Mutricy's avatar
Christophe Mutricy committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * $Id$
 *
 * Authors: Christophe Mutricy <xtophe -at- videolan -dot- org>
 *
 * 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
 *****************************************************************************/
27 28 29 30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
33 34
#include <vlc_interface.h>
#include <vlc_playlist.h>
Christophe Mutricy's avatar
Christophe Mutricy committed
35 36
#include <vlc_meta.h>

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
37 38 39 40
#include <errno.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <libnotify/notify.h>

Christophe Mutricy's avatar
Christophe Mutricy committed
41 42 43 44 45 46 47 48
/*****************************************************************************
 * 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 * );
49
static int Notify( vlc_object_t *, const char *, GdkPixbuf *, intf_thread_t * );
Christophe Mutricy's avatar
Christophe Mutricy committed
50 51
#define MAX_LENGTH 256

52 53
struct intf_sys_t
{
54
    NotifyNotification *notification;
55 56 57
    vlc_mutex_t     lock;
};

Christophe Mutricy's avatar
Christophe Mutricy committed
58 59 60 61 62 63 64 65 66
/*****************************************************************************
 * Module descriptor
 ****************************************************************************/

#define APPLICATION_NAME "VLC media player"

#define TIMEOUT_TEXT N_("Timeout (ms)")
#define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")

67 68 69 70 71
vlc_module_begin ()
    set_category( CAT_INTERFACE )
    set_subcategory( SUBCAT_INTERFACE_CONTROL )
    set_shortname( N_( "Notify" ) )
    set_description( N_("LibNotify Notification Plugin") )
Christophe Mutricy's avatar
Christophe Mutricy committed
72 73

    add_integer( "notify-timeout", 4000,NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
74
                TIMEOUT_TEXT, TIMEOUT_LONGTEXT, true )
Christophe Mutricy's avatar
Christophe Mutricy committed
75

76 77 78
    set_capability( "interface", 0 )
    set_callbacks( Open, Close )
vlc_module_end ()
Christophe Mutricy's avatar
Christophe Mutricy committed
79 80 81 82 83 84

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
85 86 87
    intf_thread_t   *p_intf = (intf_thread_t *)p_this;
    playlist_t      *p_playlist;
    intf_sys_t      *p_sys  = malloc( sizeof( intf_sys_t ) );
88

89 90
    if( !p_sys )
        return VLC_ENOMEM;
Christophe Mutricy's avatar
Christophe Mutricy committed
91 92 93

    if( !notify_init( APPLICATION_NAME ) )
    {
94
        free( p_sys );
Christophe Mutricy's avatar
Christophe Mutricy committed
95 96 97
        msg_Err( p_intf, "can't find notification daemon" );
        return VLC_EGENERIC;
    }
98

99
    vlc_mutex_init( &p_sys->lock );
100 101 102

    p_intf->p_sys = p_sys;

103 104
    p_intf->p_sys->notification = NULL;

Antoine Cellerier's avatar
Antoine Cellerier committed
105
    p_playlist = pl_Hold( p_intf );
106
    var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
107 108
    pl_Release( p_intf );

Christophe Mutricy's avatar
Christophe Mutricy committed
109 110 111 112 113 114 115 116
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
117 118 119
    intf_thread_t   *p_intf = ( intf_thread_t* ) p_this;
    intf_sys_t      *p_sys  = p_intf->p_sys;

Antoine Cellerier's avatar
Antoine Cellerier committed
120
    playlist_t *p_playlist = pl_Hold( p_this );
121
    var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
Clément Stenac's avatar
Fix  
Clément Stenac committed
122
    pl_Release( p_this );
Christophe Mutricy's avatar
Oups  
Christophe Mutricy committed
123

124 125 126
    if( p_intf->p_sys->notification )
        g_object_unref( p_intf->p_sys->notification );

127 128
    vlc_mutex_destroy( &p_sys->lock );
    free( p_sys );
Christophe Mutricy's avatar
Christophe Mutricy committed
129 130 131 132 133 134 135 136 137
    notify_uninit();
}

/*****************************************************************************
 * 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 )
{
Rafaël Carré's avatar
Rafaël Carré committed
138
    VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
139
    char                psz_tmp[MAX_LENGTH];
140
    char                psz_notify[MAX_LENGTH];
141 142 143
    char                *psz_title      = NULL;
    char                *psz_artist     = NULL;
    char                *psz_album      = NULL;
144
    char                *psz_arturl     = NULL;
145 146
    input_thread_t      *p_input        =  playlist_CurrentInput(
                                                    (playlist_t*) p_this );
147 148
    intf_thread_t       *p_intf         = ( intf_thread_t* ) param;
    intf_sys_t          *p_sys          = p_intf->p_sys;
149

Christophe Mutricy's avatar
Christophe Mutricy committed
150 151
    if( !p_input ) return VLC_SUCCESS;

152
    if( p_input->b_dead )
Christophe Mutricy's avatar
Christophe Mutricy committed
153 154 155 156 157
    {
        /* Not playing anything ... */
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
158 159
    /*Wait a tad so the meta has been fetched*/
    msleep( 1000*4 );
Christophe Mutricy's avatar
Christophe Mutricy committed
160 161

    /* Playing something ... */
Rafaël Carré's avatar
Rafaël Carré committed
162 163
    psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
    psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
164
    psz_title = input_item_GetTitle( input_GetItem( p_input ) );
165 166 167
    if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
    {
        free( psz_title );
168
        psz_title = input_item_GetName( input_GetItem( p_input ) );
169 170
    }
    if( ( psz_title == NULL ) || EMPTY_STR( psz_title ) )
171
    {  /* Not enough metadata ... */
172
        free( psz_title );
173 174 175 176 177
        free( psz_artist );
        free( psz_album );
        vlc_object_release( p_input );
        return VLC_SUCCESS;
    }
178 179 180 181 182 183 184 185 186 187
    if( EMPTY_STR( psz_artist ) )
    {
        free( psz_artist );
        psz_artist = NULL;
    }
    if( EMPTY_STR( psz_album ) )
    {
        free( psz_album );
        psz_album = NULL;
    }
188 189 190

    vlc_object_release( p_input );

191
    if( psz_artist && psz_album )
192
        snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s\n[%s]",
193 194
                  psz_title, psz_artist, psz_album );
    else if( psz_artist )
195
        snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s",
196 197 198
                  psz_title, psz_artist );
    else
        snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>", psz_title );
199

Christophe Mutricy's avatar
Christophe Mutricy committed
200 201 202 203
    free( psz_title );
    free( psz_artist );
    free( psz_album );

204 205 206 207 208 209 210 211 212 213 214 215 216
    GdkPixbuf *pix = NULL;
    GError *p_error = NULL;

    psz_arturl = input_item_GetArtURL( input_GetItem( p_input ) );
    if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) &&
                strlen( psz_arturl ) > 7 )
    { /* scale the art to show it in notify popup */
        gboolean b = TRUE;
        pix = gdk_pixbuf_new_from_file_at_scale(
                (psz_arturl + 7), 72, 72, b, &p_error );
        free( psz_arturl );
    }
    else /* else we show state-of-the art logo */
217 218 219 220 221 222 223
    {
        const char *data_path = config_GetDataDir ();
        char buf[strlen (data_path) + sizeof ("/vlc48x48.png")];

        snprintf (buf, sizeof (buf), "%s/vlc48x48.png", data_path);
        pix = gdk_pixbuf_new_from_file( buf, &p_error );
    }
224

225
    /* we need to replace '&' with '&amp;' because '&' is a keyword of
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
     * notification-daemon parser */
    int i_notify, i_len, i;
    i_len = strlen( psz_tmp );
    i_notify = 0;
    for( i = 0; ( ( i < i_len ) && ( i_notify < ( MAX_LENGTH - 5 ) ) ); i++ )
    { /* we use MAX_LENGTH - 5 because if the last char of psz_tmp is '&'
       * we will need 5 more characters: 'amp;\0' .
       * however that's unlikely to happen because the last char is '\0' */
        if( psz_tmp[i] != '&' )
            psz_notify[i_notify] = psz_tmp[i];
        else
        {
            snprintf( psz_notify + i_notify, 6, "&amp;" );
            i_notify += 4;
        }
        i_notify++;
    }
    psz_notify[i_notify] = '\0';

245 246
    vlc_mutex_lock( &p_sys->lock );

247
    Notify( p_this, psz_notify, pix, p_intf );
Christophe Mutricy's avatar
Christophe Mutricy committed
248

249 250
    vlc_mutex_unlock( &p_sys->lock );

Christophe Mutricy's avatar
Christophe Mutricy committed
251 252 253
    return VLC_SUCCESS;
}

254 255
static void Next( NotifyNotification *notification, gchar *psz, gpointer p )
{ /* libnotify callback, called when the "Next" button is pressed */
Rafaël Carré's avatar
Rafaël Carré committed
256
    VLC_UNUSED(psz);
257
    notify_notification_close (notification, NULL);
Antoine Cellerier's avatar
Antoine Cellerier committed
258
    playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p) );
259 260 261 262
    playlist_Next( p_playlist );
    pl_Release( ((vlc_object_t*) p) );
}

263 264
static void Prev( NotifyNotification *notification, gchar *psz, gpointer p )
{ /* libnotify callback, called when the "Previous" button is pressed */
Rafaël Carré's avatar
Rafaël Carré committed
265
    VLC_UNUSED(psz);
266
    notify_notification_close (notification, NULL);
Antoine Cellerier's avatar
Antoine Cellerier committed
267
    playlist_t *p_playlist = pl_Hold( ((vlc_object_t*) p) );
268 269 270 271
    playlist_Prev( p_playlist );
    pl_Release( ((vlc_object_t*) p) );
}

272
static int Notify( vlc_object_t *p_this, const char *psz_temp, GdkPixbuf *pix,
273
                   intf_thread_t *p_intf )
Christophe Mutricy's avatar
Christophe Mutricy committed
274 275
{
    NotifyNotification * notification;
276 277 278 279
    GError *p_error = NULL;

    /* Close previous notification if still active */
    if( p_intf->p_sys->notification )
280
    {
281
        notify_notification_close( p_intf->p_sys->notification, &p_error );
282 283
        g_object_unref( p_intf->p_sys->notification );
    }
284

Christophe Mutricy's avatar
Christophe Mutricy committed
285
    notification = notify_notification_new( _("Now Playing"),
286
            psz_temp, NULL, NULL);
Christophe Mutricy's avatar
Christophe Mutricy committed
287 288 289
    notify_notification_set_timeout( notification,
                                     config_GetInt(p_this, "notify-timeout") );
    notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
290
    if( pix )
291
    {
292
        notify_notification_set_icon_from_pixbuf( notification, pix );
293 294 295
        gdk_pixbuf_unref( pix );
    }

296
    /* Adds previous and next buttons in the notification */
297
    notify_notification_add_action( notification, "previous", _("Previous"), Prev,
298 299
                                    (gpointer*) p_intf, NULL );
    notify_notification_add_action( notification, "next", _("Next"), Next,
300 301
                                    (gpointer*) p_intf, NULL );

Christophe Mutricy's avatar
Christophe Mutricy committed
302
    notify_notification_show( notification, NULL);
303 304 305

    /* Stores the notification to be able to close it */
    p_intf->p_sys->notification = notification;
Christophe Mutricy's avatar
Christophe Mutricy committed
306 307 308
    return VLC_SUCCESS;
}