| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 50 |
# include "config.h" |
|---|
| 51 |
#endif |
|---|
| 52 |
|
|---|
| 53 |
#import <Foundation/Foundation.h> |
|---|
| 54 |
#import <Growl/GrowlDefines.h> |
|---|
| 55 |
|
|---|
| 56 |
#include <vlc_common.h> |
|---|
| 57 |
#include <vlc_plugin.h> |
|---|
| 58 |
#include <vlc_playlist.h> |
|---|
| 59 |
#include <vlc_meta.h> |
|---|
| 60 |
#include <vlc_interface.h> |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
struct intf_sys_t |
|---|
| 67 |
{ |
|---|
| 68 |
CFDataRef default_icon; |
|---|
| 69 |
NSAutoreleasePool *p_pool; |
|---|
| 70 |
CFStringRef app_name; |
|---|
| 71 |
CFStringRef notification_type; |
|---|
| 72 |
}; |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
static int Open ( vlc_object_t * ); |
|---|
| 78 |
static void Close ( vlc_object_t * ); |
|---|
| 79 |
|
|---|
| 80 |
static int ItemChange( vlc_object_t *, const char *, |
|---|
| 81 |
vlc_value_t, vlc_value_t, void * ); |
|---|
| 82 |
|
|---|
| 83 |
static void RegisterToGrowl( vlc_object_t * ); |
|---|
| 84 |
static void NotifyToGrowl( intf_thread_t *, const char *, CFDataRef ); |
|---|
| 85 |
|
|---|
| 86 |
static CFDataRef readFile(const char *); |
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
vlc_module_begin(); |
|---|
| 93 |
set_category( CAT_INTERFACE ); |
|---|
| 94 |
set_subcategory( SUBCAT_INTERFACE_CONTROL ); |
|---|
| 95 |
set_shortname( "Growl" ); |
|---|
| 96 |
set_description( N_("Growl Notification Plugin") ); |
|---|
| 97 |
set_capability( "interface", 0 ); |
|---|
| 98 |
set_callbacks( Open, Close ); |
|---|
| 99 |
vlc_module_end(); |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
static int Open( vlc_object_t *p_this ) |
|---|
| 105 |
{ |
|---|
| 106 |
intf_thread_t *p_intf = (intf_thread_t *)p_this; |
|---|
| 107 |
intf_sys_t *p_sys; |
|---|
| 108 |
|
|---|
| 109 |
p_sys = p_intf->p_sys = calloc( sizeof(intf_sys_t), 1); |
|---|
| 110 |
if( !p_sys ) |
|---|
| 111 |
return VLC_ENOMEM; |
|---|
| 112 |
|
|---|
| 113 |
p_sys->p_pool = [[NSAutoreleasePool alloc] init]; |
|---|
| 114 |
p_sys->app_name = CFSTR( "VLC media player" ); |
|---|
| 115 |
p_sys->notification_type = CFSTR( "New input playing" ); |
|---|
| 116 |
|
|---|
| 117 |
const char *data_path = config_GetDataDir (); |
|---|
| 118 |
char buf[strlen (data_path) + sizeof ("/vlc48x48.png")]; |
|---|
| 119 |
snprintf (buf, sizeof (buf), "%s/vlc48x48.png", data_path); |
|---|
| 120 |
p_sys->default_icon = (CFDataRef) readFile( buf ); |
|---|
| 121 |
|
|---|
| 122 |
playlist_t *p_playlist = pl_Hold( p_intf ); |
|---|
| 123 |
var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf ); |
|---|
| 124 |
pl_Release( p_intf ); |
|---|
| 125 |
|
|---|
| 126 |
RegisterToGrowl( p_this ); |
|---|
| 127 |
return VLC_SUCCESS; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
static void Close( vlc_object_t *p_this ) |
|---|
| 134 |
{ |
|---|
| 135 |
intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys; |
|---|
| 136 |
|
|---|
| 137 |
CFRelease( p_sys->default_icon ); |
|---|
| 138 |
CFRelease( p_sys->app_name ); |
|---|
| 139 |
CFRelease( p_sys->notification_type ); |
|---|
| 140 |
[p_sys->p_pool release]; |
|---|
| 141 |
free( p_sys ); |
|---|
| 142 |
|
|---|
| 143 |
playlist_t *p_playlist = pl_Hold( p_this ); |
|---|
| 144 |
var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this ); |
|---|
| 145 |
pl_Release( p_this ); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
static int ItemChange( vlc_object_t *p_this, const char *psz_var, |
|---|
| 152 |
vlc_value_t oldval, vlc_value_t newval, void *param ) |
|---|
| 153 |
{ |
|---|
| 154 |
VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval); |
|---|
| 155 |
|
|---|
| 156 |
intf_thread_t *p_intf = (intf_thread_t*)param; |
|---|
| 157 |
char *psz_tmp = NULL; |
|---|
| 158 |
char *psz_title = NULL; |
|---|
| 159 |
char *psz_artist = NULL; |
|---|
| 160 |
char *psz_album = NULL; |
|---|
| 161 |
input_thread_t *p_input; |
|---|
| 162 |
playlist_t *p_playlist = pl_Hold( p_this ); |
|---|
| 163 |
|
|---|
| 164 |
p_input = p_playlist->p_input; |
|---|
| 165 |
pl_Release( p_this ); |
|---|
| 166 |
|
|---|
| 167 |
if( !p_input ) return VLC_SUCCESS; |
|---|
| 168 |
vlc_object_hold( p_input ); |
|---|
| 169 |
|
|---|
| 170 |
char *psz_name = input_item_GetName( input_GetItem( p_input ) ); |
|---|
| 171 |
if( p_input->b_dead || !psz_name ) |
|---|
| 172 |
{ |
|---|
| 173 |
|
|---|
| 174 |
free( psz_name ); |
|---|
| 175 |
vlc_object_release( p_input ); |
|---|
| 176 |
return VLC_SUCCESS; |
|---|
| 177 |
} |
|---|
| 178 |
free( psz_name ); |
|---|
| 179 |
|
|---|
| 180 |
|
|---|
| 181 |
input_item_t *p_item = input_GetItem( p_input ); |
|---|
| 182 |
|
|---|
| 183 |
psz_title = input_item_GetTitle( p_item ); |
|---|
| 184 |
if( psz_title == NULL || EMPTY_STR( psz_title ) ) |
|---|
| 185 |
{ |
|---|
| 186 |
free( psz_title ); |
|---|
| 187 |
psz_title = input_item_GetName( input_GetItem( p_input ) ); |
|---|
| 188 |
if( psz_title == NULL || EMPTY_STR( psz_title ) ) |
|---|
| 189 |
{ |
|---|
| 190 |
free( psz_title ); |
|---|
| 191 |
vlc_object_release( p_input ); |
|---|
| 192 |
return VLC_SUCCESS; |
|---|
| 193 |
} |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
psz_artist = input_item_GetArtist( p_item ); |
|---|
| 197 |
if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist ); |
|---|
| 198 |
psz_album = input_item_GetAlbum( p_item ) ; |
|---|
| 199 |
if( EMPTY_STR( psz_album ) ) FREENULL( psz_album ); |
|---|
| 200 |
|
|---|
| 201 |
int i_ret; |
|---|
| 202 |
if( psz_artist && psz_album ) |
|---|
| 203 |
i_ret = asprintf( &psz_tmp, "%s\n%s [%s]", |
|---|
| 204 |
psz_title, psz_artist, psz_album ); |
|---|
| 205 |
else if( psz_artist ) |
|---|
| 206 |
i_ret = asprintf( &psz_tmp, "%s\n%s", psz_title, psz_artist ); |
|---|
| 207 |
else |
|---|
| 208 |
i_ret = asprintf( &psz_tmp, "%s", psz_title ); |
|---|
| 209 |
|
|---|
| 210 |
if( i_ret == -1 ) |
|---|
| 211 |
{ |
|---|
| 212 |
free( psz_title ); |
|---|
| 213 |
free( psz_artist ); |
|---|
| 214 |
free( psz_album ); |
|---|
| 215 |
vlc_object_release( p_input ); |
|---|
| 216 |
return VLC_ENOMEM; |
|---|
| 217 |
} |
|---|
| 218 |
|
|---|
| 219 |
char *psz_arturl = input_item_GetArtURL( p_item ); |
|---|
| 220 |
CFDataRef art = NULL; |
|---|
| 221 |
if( psz_arturl && !strncmp( psz_arturl, "file://", 7 ) && |
|---|
| 222 |
strlen( psz_arturl ) > 7 ) |
|---|
| 223 |
art = (CFDataRef) readFile( psz_arturl + 7 ); |
|---|
| 224 |
|
|---|
| 225 |
free( psz_title ); |
|---|
| 226 |
free( psz_artist ); |
|---|
| 227 |
free( psz_album ); |
|---|
| 228 |
free( psz_arturl ); |
|---|
| 229 |
|
|---|
| 230 |
NotifyToGrowl( p_intf, psz_tmp, art ); |
|---|
| 231 |
|
|---|
| 232 |
if( art ) CFRelease( art ); |
|---|
| 233 |
|
|---|
| 234 |
vlc_object_release( p_input ); |
|---|
| 235 |
return VLC_SUCCESS; |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
static void RegisterToGrowl( vlc_object_t *p_this ) |
|---|
| 242 |
{ |
|---|
| 243 |
intf_sys_t *p_sys = ((intf_thread_t *)p_this)->p_sys; |
|---|
| 244 |
|
|---|
| 245 |
CFArrayRef defaultAndAllNotifications = CFArrayCreate( |
|---|
| 246 |
kCFAllocatorDefault, (const void **)&(p_sys->notification_type), 1, |
|---|
| 247 |
&kCFTypeArrayCallBacks ); |
|---|
| 248 |
|
|---|
| 249 |
CFTypeRef registerKeys[4] = { |
|---|
| 250 |
GROWL_APP_NAME, |
|---|
| 251 |
GROWL_NOTIFICATIONS_ALL, |
|---|
| 252 |
GROWL_NOTIFICATIONS_DEFAULT, |
|---|
| 253 |
GROWL_APP_ICON |
|---|
| 254 |
}; |
|---|
| 255 |
|
|---|
| 256 |
CFTypeRef registerValues[4] = { |
|---|
| 257 |
p_sys->app_name, |
|---|
| 258 |
defaultAndAllNotifications, |
|---|
| 259 |
defaultAndAllNotifications, |
|---|
| 260 |
p_sys->default_icon |
|---|
| 261 |
}; |
|---|
| 262 |
|
|---|
| 263 |
CFDictionaryRef registerInfo = CFDictionaryCreate( |
|---|
| 264 |
kCFAllocatorDefault, registerKeys, registerValues, 4, |
|---|
| 265 |
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); |
|---|
| 266 |
|
|---|
| 267 |
CFRelease( defaultAndAllNotifications ); |
|---|
| 268 |
|
|---|
| 269 |
CFNotificationCenterPostNotificationWithOptions( |
|---|
| 270 |
CFNotificationCenterGetDistributedCenter(), |
|---|
| 271 |
(CFStringRef)GROWL_APP_REGISTRATION, NULL, registerInfo, |
|---|
| 272 |
kCFNotificationPostToAllSessions ); |
|---|
| 273 |
CFRelease( registerInfo ); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
static void NotifyToGrowl( intf_thread_t *p_intf, const char *psz_desc, CFDataRef art ) |
|---|
| 277 |
{ |
|---|
| 278 |
intf_sys_t *p_sys = p_intf->p_sys; |
|---|
| 279 |
|
|---|
| 280 |
CFStringRef title = CFStringCreateWithCString( kCFAllocatorDefault, _("Now playing"), kCFStringEncodingUTF8 ); |
|---|
| 281 |
CFStringRef desc = CFStringCreateWithCString( kCFAllocatorDefault, psz_desc, kCFStringEncodingUTF8 ); |
|---|
| 282 |
|
|---|
| 283 |
CFMutableDictionaryRef notificationInfo = CFDictionaryCreateMutable( |
|---|
| 284 |
kCFAllocatorDefault, 5, &kCFTypeDictionaryKeyCallBacks, |
|---|
| 285 |
&kCFTypeDictionaryValueCallBacks); |
|---|
| 286 |
|
|---|
| 287 |
CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_NAME, p_sys->notification_type ); |
|---|
| 288 |
CFDictionarySetValue( notificationInfo, GROWL_APP_NAME, p_sys->app_name ); |
|---|
| 289 |
CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_TITLE, title ); |
|---|
| 290 |
CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_DESCRIPTION, desc ); |
|---|
| 291 |
|
|---|
| 292 |
CFDictionarySetValue( notificationInfo, GROWL_NOTIFICATION_ICON, |
|---|
| 293 |
art ? art : p_sys->default_icon ); |
|---|
| 294 |
|
|---|
| 295 |
CFRelease( title ); |
|---|
| 296 |
CFRelease( desc ); |
|---|
| 297 |
|
|---|
| 298 |
CFNotificationCenterPostNotificationWithOptions( |
|---|
| 299 |
CFNotificationCenterGetDistributedCenter(), |
|---|
| 300 |
(CFStringRef)GROWL_NOTIFICATION, NULL, notificationInfo, |
|---|
| 301 |
kCFNotificationPostToAllSessions ); |
|---|
| 302 |
|
|---|
| 303 |
CFRelease( notificationInfo ); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
static CFDataRef readFile(const char *filename) |
|---|
| 310 |
{ |
|---|
| 311 |
CFDataRef data; |
|---|
| 312 |
|
|---|
| 313 |
FILE *fp = fopen(filename, "r"); |
|---|
| 314 |
if( !fp ) |
|---|
| 315 |
return NULL; |
|---|
| 316 |
|
|---|
| 317 |
fseek(fp, 0, SEEK_END); |
|---|
| 318 |
long dataLength = ftell(fp); |
|---|
| 319 |
fseek(fp, 0, SEEK_SET); |
|---|
| 320 |
unsigned char *fileData = malloc(dataLength); |
|---|
| 321 |
fread(fileData, 1, dataLength, fp); |
|---|
| 322 |
fclose(fp); |
|---|
| 323 |
return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, fileData, |
|---|
| 324 |
dataLength, kCFAllocatorMalloc); |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|