1 /*****************************************************************************
2 * notify.c : libnotify notification plugin
3 *****************************************************************************
4 * Copyright (C) 2006 the VideoLAN team
7 * Authors: Christophe Mutricy <xtophe -at- videolan -dot- org>
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 <libnotify/notify.h>
32 #include <vlc_interface.h>
33 #include <vlc_playlist.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close ( vlc_object_t * );
42 static int ItemChange( vlc_object_t *, const char *,
43 vlc_value_t, vlc_value_t, void * );
44 static int Notify( vlc_object_t *, const char * );
45 #define MAX_LENGTH 256
52 /*****************************************************************************
54 ****************************************************************************/
56 #define APPLICATION_NAME "VLC media player"
58 #define TIMEOUT_TEXT N_("Timeout (ms)")
59 #define TIMEOUT_LONGTEXT N_("How long the notification will be displayed ")
62 set_category( CAT_INTERFACE );
63 set_subcategory( SUBCAT_INTERFACE_CONTROL );
64 set_shortname( _( "Notify" ) );
65 set_description( _("LibNotify Notification Plugin") );
67 add_integer( "notify-timeout", 4000,NULL,
68 TIMEOUT_TEXT, TIMEOUT_LONGTEXT, VLC_TRUE );
70 set_capability( "interface", 0 );
71 set_callbacks( Open, Close );
74 /*****************************************************************************
75 * Open: initialize and create stuff
76 *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
79 intf_thread_t *p_intf = (intf_thread_t *)p_this;
80 playlist_t *p_playlist;
81 intf_sys_t *p_sys = malloc( sizeof( intf_sys_t ) );
85 msg_Err( p_intf, "Out of memory" );
89 if( !notify_init( APPLICATION_NAME ) )
91 msg_Err( p_intf, "can't find notification daemon" );
95 vlc_mutex_init( p_this, &p_sys->lock );
97 p_intf->p_sys = p_sys;
99 p_playlist = pl_Yield( p_intf );
100 var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
101 pl_Release( p_intf );
106 /*****************************************************************************
107 * Close: destroy interface stuff
108 *****************************************************************************/
109 static void Close( vlc_object_t *p_this )
111 intf_thread_t *p_intf = ( intf_thread_t* ) p_this;
112 intf_sys_t *p_sys = p_intf->p_sys;
114 playlist_t *p_playlist = pl_Yield( p_this );
115 var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
116 pl_Release( p_this );
118 vlc_mutex_destroy( &p_sys->lock );
123 /*****************************************************************************
124 * ItemChange: Playlist item change callback
125 *****************************************************************************/
126 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
127 vlc_value_t oldval, vlc_value_t newval, void *param )
129 char psz_tmp[MAX_LENGTH];
130 char *psz_title = NULL;
131 char *psz_artist = NULL;
132 char *psz_album = NULL;
133 input_thread_t *p_input = NULL;
134 playlist_t * p_playlist = pl_Yield( p_this );
135 intf_thread_t *p_intf = ( intf_thread_t* ) param;
136 intf_sys_t *p_sys = p_intf->p_sys;
138 p_input = p_playlist->p_input;
139 vlc_object_release( p_playlist );
141 if( !p_input ) return VLC_SUCCESS;
142 vlc_object_yield( p_input );
144 if( p_input->b_dead || !input_GetItem(p_input)->psz_name )
146 /* Not playing anything ... */
147 vlc_object_release( p_input );
151 /* Playing something ... */
152 psz_artist = input_item_GetArtist( input_GetItem( p_input ) );
153 if( psz_artist == NULL ) psz_artist = strdup( _("no artist") );
154 psz_album = input_item_GetAlbum( input_GetItem( p_input ) ) ;
155 if( psz_album == NULL ) psz_album = strdup( _("no album") );
156 psz_title = input_item_GetName( input_GetItem( p_input ) );
158 vlc_object_release( p_input );
160 if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
161 snprintf( psz_tmp, MAX_LENGTH, "<b>%s</b>\n%s - %s",
162 psz_title, psz_artist, psz_album );
167 vlc_mutex_lock( &p_sys->lock );
169 Notify( p_this, psz_tmp );
171 vlc_mutex_unlock( &p_sys->lock );
176 static int Notify( vlc_object_t *p_this, const char *psz_temp )
178 NotifyNotification * notification;
179 notification = notify_notification_new( _("Now Playing"),
181 DATA_PATH "/vlc48x48.png",NULL);
182 notify_notification_set_timeout( notification,
183 config_GetInt(p_this, "notify-timeout") );
184 notify_notification_set_urgency( notification, NOTIFY_URGENCY_LOW );
185 notify_notification_show( notification, NULL);