1 /*****************************************************************************
2 * growl_udp.c : growl UDP notification plugin
3 *****************************************************************************
4 * Copyright (C) 2006 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 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close ( vlc_object_t * );
46 static int ItemChange( vlc_object_t *, const char *,
47 vlc_value_t, vlc_value_t, void * );
49 static int RegisterToGrowl( vlc_object_t *p_this );
50 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc );
51 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
52 #define GROWL_MAX_LENGTH 256
54 /*****************************************************************************
56 ****************************************************************************/
58 #define SERVER_DEFAULT "127.0.0.1"
59 #define SERVER_TEXT N_("Server")
60 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
61 "will be sent. By default, notifications are sent locally." )
62 #define PASS_DEFAULT ""
63 #define PASS_TEXT N_("Password")
64 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
65 #define PORT_TEXT N_("UDP port")
66 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
69 set_category( CAT_INTERFACE )
70 set_subcategory( SUBCAT_INTERFACE_CONTROL )
71 set_shortname( "Growl-UDP" )
72 set_description( N_("Growl UDP Notification Plugin") )
74 add_string( "growl-server", SERVER_DEFAULT, NULL,
75 SERVER_TEXT, SERVER_LONGTEXT, false )
76 add_password( "growl-password", PASS_DEFAULT, NULL,
77 PASS_TEXT, PASS_LONGTEXT, false )
78 add_integer( "growl-port", 9887, NULL,
79 PORT_TEXT, PORT_LONGTEXT, true )
81 set_capability( "interface", 0 )
82 set_callbacks( Open, Close )
85 /*****************************************************************************
86 * Open: initialize and create stuff
87 *****************************************************************************/
88 static int Open( vlc_object_t *p_this )
90 intf_thread_t *p_intf = (intf_thread_t *)p_this;
92 playlist_t *p_playlist = pl_Hold( p_intf );
93 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
96 RegisterToGrowl( p_this );
100 /*****************************************************************************
101 * Close: destroy interface stuff
102 *****************************************************************************/
103 static void Close( vlc_object_t *p_this )
105 playlist_t *p_playlist = pl_Hold( p_this );
106 var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
107 pl_Release( p_this );
110 /*****************************************************************************
111 * ItemChange: Playlist item change callback
112 *****************************************************************************/
113 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
114 vlc_value_t oldval, vlc_value_t newval, void *param )
116 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
118 char psz_tmp[GROWL_MAX_LENGTH];
119 char *psz_title = NULL;
120 char *psz_artist = NULL;
121 char *psz_album = NULL;
122 input_thread_t *p_input;
123 p_input = playlist_CurrentInput( (playlist_t*)p_this );
125 if( !p_input ) return VLC_SUCCESS;
127 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
128 if( p_input->b_dead || !psz_name )
130 /* Not playing anything ... */
132 vlc_object_release( p_input );
137 /* Playing something ... */
138 input_item_t *p_item = input_GetItem( p_input );
140 psz_title = input_item_GetTitleFbName( p_item );
141 if( EMPTY_STR( psz_title ) )
144 vlc_object_release( p_input );
148 psz_artist = input_item_GetArtist( p_item );
149 if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
150 psz_album = input_item_GetAlbum( p_item ) ;
151 if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
153 if( psz_artist && psz_album )
154 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
155 psz_title, psz_artist, psz_album );
156 else if( psz_artist )
157 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
159 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );
165 NotifyToGrowl( p_this, psz_tmp );
167 vlc_object_release( p_input );
171 /*****************************************************************************
172 * Growl specific functions
173 *****************************************************************************/
174 #define GROWL_PROTOCOL_VERSION (1)
175 #define GROWL_TYPE_REGISTRATION (0)
176 #define GROWL_TYPE_NOTIFICATION (1)
177 #define APPLICATION_NAME "VLC media player"
179 #define insertstrlen( psz ) \
181 uint16_t i_size = strlen( psz ); \
182 psz_encoded[i++] = (i_size>>8)&0xFF; \
183 psz_encoded[i++] = i_size&0xFF; \
185 /*****************************************************************************
187 *****************************************************************************/
188 static int RegisterToGrowl( vlc_object_t *p_this )
190 uint8_t *psz_encoded = malloc(100);
191 uint8_t i_defaults = 0;
192 static const char *psz_notifications[] = {"Now Playing", NULL};
193 bool pb_defaults[] = {true, false};
195 if( psz_encoded == NULL )
198 memset( psz_encoded, 0, sizeof(psz_encoded) );
199 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
200 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
201 insertstrlen(APPLICATION_NAME);
203 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
204 i += strlen(APPLICATION_NAME);
205 for( j = 0 ; psz_notifications[j] != NULL ; j++)
207 insertstrlen(psz_notifications[j]);
208 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
209 i += strlen(psz_notifications[j]);
212 for( j = 0 ; psz_notifications[j] != NULL ; j++)
213 if(pb_defaults[j] == true)
215 psz_encoded[i++] = (uint8_t)j;
218 psz_encoded[5] = i_defaults;
220 CheckAndSend(p_this, psz_encoded, i);
225 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
227 const char *psz_type = "Now Playing", *psz_title = "Now Playing";
228 uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
231 if( psz_encoded == NULL )
234 memset( psz_encoded, 0, sizeof(psz_encoded) );
235 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
236 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
238 psz_encoded[i++] = (flags>>8)&0xFF;
239 psz_encoded[i++] = flags&0xFF;
240 insertstrlen(psz_type);
241 insertstrlen(psz_title);
242 insertstrlen(psz_desc);
243 insertstrlen(APPLICATION_NAME);
244 strcpy( (char*)(psz_encoded+i), psz_type );
245 i += strlen(psz_type);
246 strcpy( (char*)(psz_encoded+i), psz_title );
247 i += strlen(psz_title);
248 strcpy( (char*)(psz_encoded+i), psz_desc );
249 i += strlen(psz_desc);
250 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
251 i += strlen(APPLICATION_NAME);
253 CheckAndSend(p_this, psz_encoded, i);
258 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
262 intf_thread_t *p_intf = (intf_thread_t *)p_this;
263 char *psz_password = var_InheritString( p_intf, "growl-password" );
264 char *psz_server = var_InheritString( p_intf, "growl-server" );
265 int i_port = var_InheritInteger( p_intf, "growl-port" );
266 strcpy( (char*)(p_data+i_offset), psz_password );
267 i = i_offset + strlen(psz_password);
270 AddMD5( &md5, p_data, i );
273 for( i = 0 ; i < 4 ; i++ )
275 md5.p_digest[i] = md5.p_digest[i];
276 p_data[i_offset++] = md5.p_digest[i] &0xFF;
277 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
278 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
279 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
282 i_handle = net_ConnectUDP( p_this, psz_server, i_port, -1 );
285 msg_Err( p_this, "failed to open a connection (udp)" );
291 shutdown( i_handle, SHUT_RD );
292 if( send( i_handle, p_data, i_offset, 0 )
295 msg_Warn( p_this, "send error: %m" );
297 net_Close( i_handle );
304 #undef GROWL_PROTOCOL_VERSION
305 #undef GROWL_TYPE_REGISTRATION
306 #undef GROWL_TYPE_NOTIFICATION
307 #undef APPLICATION_NAME