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>
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
47 static int ItemChange( vlc_object_t *, const char *,
48 vlc_value_t, vlc_value_t, void * );
50 static int RegisterToGrowl( vlc_object_t *p_this );
51 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc );
52 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
53 #define GROWL_MAX_LENGTH 256
55 /*****************************************************************************
57 ****************************************************************************/
59 #define SERVER_DEFAULT "127.0.0.1"
60 #define SERVER_TEXT N_("Server")
61 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
62 "will be sent. By default, notifications are sent locally." )
63 #define PASS_DEFAULT ""
64 #define PASS_TEXT N_("Password")
65 #define PASS_LONGTEXT N_("Growl password on the Growl server.")
66 #define PORT_TEXT N_("UDP port")
67 #define PORT_LONGTEXT N_("Growl UDP port on the Growl server.")
70 set_category( CAT_INTERFACE )
71 set_subcategory( SUBCAT_INTERFACE_CONTROL )
72 set_shortname( "Growl-UDP" )
73 set_description( N_("Growl UDP Notification Plugin") )
75 add_string( "growl-server", SERVER_DEFAULT, NULL,
76 SERVER_TEXT, SERVER_LONGTEXT, false )
77 add_password( "growl-password", PASS_DEFAULT, NULL,
78 PASS_TEXT, PASS_LONGTEXT, false )
79 add_integer( "growl-port", 9887, NULL,
80 PORT_TEXT, PORT_LONGTEXT, true )
82 set_capability( "interface", 0 )
83 set_callbacks( Open, Close )
86 /*****************************************************************************
87 * Open: initialize and create stuff
88 *****************************************************************************/
89 static int Open( vlc_object_t *p_this )
91 intf_thread_t *p_intf = (intf_thread_t *)p_this;
93 playlist_t *p_playlist = pl_Hold( p_intf );
94 var_AddCallback( p_playlist, "item-current", ItemChange, p_intf );
97 RegisterToGrowl( p_this );
101 /*****************************************************************************
102 * Close: destroy interface stuff
103 *****************************************************************************/
104 static void Close( vlc_object_t *p_this )
106 playlist_t *p_playlist = pl_Hold( p_this );
107 var_DelCallback( p_playlist, "item-current", ItemChange, p_this );
108 pl_Release( p_this );
111 /*****************************************************************************
112 * ItemChange: Playlist item change callback
113 *****************************************************************************/
114 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
115 vlc_value_t oldval, vlc_value_t newval, void *param )
117 VLC_UNUSED(psz_var); VLC_UNUSED(oldval); VLC_UNUSED(newval);
119 char psz_tmp[GROWL_MAX_LENGTH];
120 char *psz_title = NULL;
121 char *psz_artist = NULL;
122 char *psz_album = NULL;
123 input_thread_t *p_input;
124 p_input = playlist_CurrentInput( (playlist_t*)p_this );
126 if( !p_input ) return VLC_SUCCESS;
128 char *psz_name = input_item_GetName( input_GetItem( p_input ) );
129 if( p_input->b_dead || !psz_name )
131 /* Not playing anything ... */
133 vlc_object_release( p_input );
138 /* Playing something ... */
139 input_item_t *p_item = input_GetItem( p_input );
141 psz_title = input_item_GetTitleFbName( p_item );
142 if( EMPTY_STR( psz_title ) )
145 vlc_object_release( p_input );
149 psz_artist = input_item_GetArtist( p_item );
150 if( EMPTY_STR( psz_artist ) ) FREENULL( psz_artist );
151 psz_album = input_item_GetAlbum( p_item ) ;
152 if( EMPTY_STR( psz_album ) ) FREENULL( psz_album );
154 if( psz_artist && psz_album )
155 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s [%s]",
156 psz_title, psz_artist, psz_album );
157 else if( psz_artist )
158 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s\n%s", psz_title, psz_artist );
160 snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s", psz_title );
166 NotifyToGrowl( p_this, psz_tmp );
168 vlc_object_release( p_input );
172 /*****************************************************************************
173 * Growl specific functions
174 *****************************************************************************/
175 #define GROWL_PROTOCOL_VERSION (1)
176 #define GROWL_TYPE_REGISTRATION (0)
177 #define GROWL_TYPE_NOTIFICATION (1)
178 #define APPLICATION_NAME "VLC media player"
180 #define insertstrlen( psz ) \
182 uint16_t i_size = strlen( psz ); \
183 psz_encoded[i++] = (i_size>>8)&0xFF; \
184 psz_encoded[i++] = i_size&0xFF; \
186 /*****************************************************************************
188 *****************************************************************************/
189 static int RegisterToGrowl( vlc_object_t *p_this )
191 uint8_t *psz_encoded = malloc(100);
192 uint8_t i_defaults = 0;
193 static const char *psz_notifications[] = {"Now Playing", NULL};
194 bool pb_defaults[] = {true, false};
196 if( psz_encoded == NULL )
199 memset( psz_encoded, 0, sizeof(psz_encoded) );
200 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
201 psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
202 insertstrlen(APPLICATION_NAME);
204 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
205 i += strlen(APPLICATION_NAME);
206 for( j = 0 ; psz_notifications[j] != NULL ; j++)
208 insertstrlen(psz_notifications[j]);
209 strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
210 i += strlen(psz_notifications[j]);
213 for( j = 0 ; psz_notifications[j] != NULL ; j++)
214 if(pb_defaults[j] == true)
216 psz_encoded[i++] = (uint8_t)j;
219 psz_encoded[5] = i_defaults;
221 CheckAndSend(p_this, psz_encoded, i);
226 static int NotifyToGrowl( vlc_object_t *p_this, const char *psz_desc )
228 const char *psz_type = "Now Playing", *psz_title = "Now Playing";
229 uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
232 if( psz_encoded == NULL )
235 memset( psz_encoded, 0, sizeof(psz_encoded) );
236 psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
237 psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
239 psz_encoded[i++] = (flags>>8)&0xFF;
240 psz_encoded[i++] = flags&0xFF;
241 insertstrlen(psz_type);
242 insertstrlen(psz_title);
243 insertstrlen(psz_desc);
244 insertstrlen(APPLICATION_NAME);
245 strcpy( (char*)(psz_encoded+i), psz_type );
246 i += strlen(psz_type);
247 strcpy( (char*)(psz_encoded+i), psz_title );
248 i += strlen(psz_title);
249 strcpy( (char*)(psz_encoded+i), psz_desc );
250 i += strlen(psz_desc);
251 strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
252 i += strlen(APPLICATION_NAME);
254 CheckAndSend(p_this, psz_encoded, i);
259 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
263 intf_thread_t *p_intf = (intf_thread_t *)p_this;
264 char *psz_password = config_GetPsz( p_intf, "growl-password" );
265 char *psz_server = config_GetPsz( p_intf, "growl-server" );
266 int i_port = config_GetInt( p_intf, "growl-port" );
267 strcpy( (char*)(p_data+i_offset), psz_password );
268 i = i_offset + strlen(psz_password);
271 AddMD5( &md5, p_data, i );
274 for( i = 0 ; i < 4 ; i++ )
276 md5.p_digest[i] = md5.p_digest[i];
277 p_data[i_offset++] = md5.p_digest[i] &0xFF;
278 p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
279 p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
280 p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
283 i_handle = net_ConnectUDP( p_this, psz_server, i_port, -1 );
286 msg_Err( p_this, "failed to open a connection (udp)" );
292 shutdown( i_handle, SHUT_RD );
293 if( send( i_handle, p_data, i_offset, 0 )
296 msg_Warn( p_this, "send error: %m" );
298 net_Close( i_handle );
305 #undef GROWL_PROTOCOL_VERSION
306 #undef GROWL_TYPE_REGISTRATION
307 #undef GROWL_TYPE_NOTIFICATION
308 #undef APPLICATION_NAME