1 /*****************************************************************************
2 * growl_udp.c : growl UDP notification plugin
3 *****************************************************************************
4 * Copyright (C) 2006 - 2010 the VideoLAN team
7 * Authors: Jérôme Decoodt <djc -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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_interface.h>
35 #include <vlc_playlist.h>
37 #include <vlc_network.h>
40 /*****************************************************************************
41 * intf_sys_t: description
42 *****************************************************************************/
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close ( vlc_object_t * );
56 static int ItemChange( vlc_object_t *, const char *,
57 vlc_value_t, vlc_value_t, void * );
59 static int RegisterToGrowl( vlc_object_t *p_this );
60 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc );
61 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset, size_t is_ze );
62 #define GROWL_MAX_LENGTH 256
64 /*****************************************************************************
66 ****************************************************************************/
68 #define SERVER_DEFAULT "127.0.0.1"
69 #define SERVER_TEXT N_("Server")
70 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
71 "will be sent. By default, notifications are sent locally." )
72 #define PASS_DEFAULT ""
73 #define PASS_TEXT N_("Password")
74 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
75 #define PORT_TEXT N_("UDP port")
76 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
79 set_category( CAT_INTERFACE )
80 set_subcategory( SUBCAT_INTERFACE_CONTROL )
81 set_shortname( "Growl-UDP" )
82 set_description( N_("Growl UDP Notification Plugin") )
84 add_string( "growl-server", SERVER_DEFAULT, NULL,
85 SERVER_TEXT, SERVER_LONGTEXT, false )
86 add_password( "growl-password", PASS_DEFAULT, NULL,
87 PASS_TEXT, PASS_LONGTEXT, false )
88 add_integer( "growl-port", 9887, NULL,
89 PORT_TEXT, PORT_LONGTEXT, true )
91 set_capability( "interface", 0 )
92 set_callbacks( Open, Close )
95 /*****************************************************************************
96 * Open: initialize and create stuff
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 intf_thread_t *p_intf = (intf_thread_t *)p_this;
101 intf_sys_t *p_sys = p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
105 p_sys->psz_password = var_InheritString( p_this, "growl-password" );
106 p_sys->psz_server = var_InheritString( p_this, "growl-server" );
107 p_sys->i_port = var_InheritInteger( p_this, "growl-port" );
109 if( !p_sys->psz_password || !p_sys->psz_server )
111 free( p_sys->psz_password );
112 free( p_sys->psz_server );
117 var_AddCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );
118 RegisterToGrowl( p_this );
122 /*****************************************************************************
123 * Close: destroy interface stuff
124 *****************************************************************************/
125 static void Close( vlc_object_t *p_this )
127 intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
129 var_DelCallback( pl_Get( p_this ), "item-current", ItemChange, p_this );
131 free( p_sys->psz_password );
132 free( p_sys->psz_server );
136 /*****************************************************************************
137 * ItemChange: Playlist item change callback
138 *****************************************************************************/
139 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
140 vlc_value_t oldval, vlc_value_t newval, void *param )
142 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
145 char psz_tmp[GROWL_MAX_LENGTH];
149 input_thread_t *p_input = playlist_CurrentInput( (playlist_t*)p_this );
151 if( !p_input ) return VLC_SUCCESS;
153 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
154 if( p_input->b_dead || !psz_name )
156 /* Not playing anything ... */
158 vlc_object_release( p_input );
163 /* Playing something ... */
164 input_item_t *p_item = input_GetItem( p_input );
166 psz_title = input_item_GetTitleFbName( p_item );
167 if( EMPTY_STR( psz_title ) )
170 vlc_object_release( p_input );
174 psz_artist = input_item_GetArtist( p_item );
175 if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
176 psz_album = input_item_GetAlbum( p_item ) ;
177 if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
179 if( psz_artist && psz_album )
180 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
181 psz_title, psz_artist, psz_album );
182 else if( psz_artist )
183 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
185 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );
191 NotifyToGrowl( p_this, psz_tmp );
193 vlc_object_release( p_input );
197 /*****************************************************************************
198 * Growl specific functions
199 *****************************************************************************/
200 #define GROWL_PROTOCOL_VERSION (1)
201 #define GROWL_TYPE_REGISTRATION (0)
202 #define GROWL_TYPE_NOTIFICATION (1)
203 #define APPLICATION_NAME "VLC media player"
205 #define insertstrlen( psz ) \
207 uint16_t i_size = strlen( psz ); \
208 psz_encoded[i++] = (i_size>>8)&0xFF; \
209 psz_encoded[i++] = i_size&0xFF; \
211 /*****************************************************************************
213 *****************************************************************************/
214 static int RegisterToGrowl( vlc_object_t *p_this )
216 uint8_t *psz_encoded = calloc( 100, 1 );
217 uint8_t i_defaults = 0;
218 static const char *psz_notifications[] = {"Now Playing", NULL};
219 bool pb_defaults[] = {true, false};
221 if( psz_encoded == NULL )
224 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
225 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
226 insertstrlen(APPLICATION_NAME);
228 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
229 i += strlen(APPLICATION_NAME);
230 for( j = 0 ; psz_notifications[j] != NULL ; j++)
232 insertstrlen(psz_notifications[j]);
233 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
234 i += strlen(psz_notifications[j]);
237 for( j = 0 ; psz_notifications[j] != NULL ; j++)
239 if(pb_defaults[j] == true)
241 psz_encoded[i++] = (uint8_t)j;
245 psz_encoded[5] = i_defaults;
247 CheckAndSend(p_this, psz_encoded, i, 100);
252 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
254 const char *psz_type = "Now Playing", *psz_title = "Now Playing";
255 uint8_t *psz_encoded = calloc(GROWL_MAX_LENGTH + 42, 1);
258 if( psz_encoded == NULL )
261 // Check the size of the data
262 size_t i_type = strlen( psz_type );
263 size_t i_title = strlen( psz_title );
264 size_t i_app = strlen( APPLICATION_NAME );
265 size_t i_desc = strlen( psz_desc );
266 if( 12 + i_type + i_title + i_desc + i_app >= GROWL_MAX_LENGTH + 42 )
272 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
273 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
275 psz_encoded[i++] = (flags>>8)&0xFF;
276 psz_encoded[i++] = flags&0xFF;
277 insertstrlen(psz_type);
278 insertstrlen(psz_title);
279 insertstrlen(psz_desc);
280 insertstrlen(APPLICATION_NAME);
282 strcpy( (char*)(psz_encoded+i), psz_type );
284 strcpy( (char*)(psz_encoded+i), psz_title );
286 strcpy( (char*)(psz_encoded+i), psz_desc );
288 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
291 CheckAndSend(p_this, psz_encoded, i, GROWL_MAX_LENGTH + 42);
296 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset, size_t i_size )
298 intf_sys_t *p_sys = ((intf_thread_t*)p_this)->p_sys;
302 int i_password_length = strlen( p_sys->psz_password );
303 // Check that the buffer is larger enought for the string and the md5
304 if( i_offset + i_password_length + 4*4 >= i_size )
307 strcpy( (char*)(p_data+i_offset), p_sys->psz_password );
310 AddMD5( &md5, p_data, i_offset + i_password_length );
313 for( int i = 0 ; i < 4 ; i++ )
315 p_data[i_offset++] = md5.p_digest[i] &0xFF;
316 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
317 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
318 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
321 i_handle = net_ConnectUDP( p_this, p_sys->psz_server, p_sys->i_port, -1 );
324 msg_Err( p_this, "failed to open a connection (udp)" );
328 shutdown( i_handle, SHUT_RD );
329 if( send( i_handle, p_data, i_offset, 0 ) == -1 )
331 msg_Warn( p_this, "send error: %m" );
333 net_Close( i_handle );
338 #undef GROWL_PROTOCOL_VERSION
339 #undef GROWL_TYPE_REGISTRATION
340 #undef GROWL_TYPE_NOTIFICATION
341 #undef APPLICATION_NAME