]> git.sesse.net Git - vlc/blob - modules/misc/growl.c
* MainMenu.nib, playlist.m: use NSSearchField instead of NSTextField, so we don't...
[vlc] / modules / misc / growl.c
1 /*****************************************************************************
2  * growl.c : growl notification plugin
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jérôme Decoodt <djc -at- videolan -dot- org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31 #include <vlc_meta.h>
32 #include <network.h>
33 #include <errno.h>
34 #include <vlc_md5.h>
35
36 /*****************************************************************************
37  * Local prototypes
38  *****************************************************************************/
39 static int  Open    ( vlc_object_t * );
40 static void Close   ( vlc_object_t * );
41 static void Run     ( intf_thread_t * );
42
43 static int ItemChange( vlc_object_t *, const char *,
44                        vlc_value_t, vlc_value_t, void * );
45
46 static int RegisterToGrowl( vlc_object_t *p_this );
47 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
48                             char *psz_title, char *psz_desc );
49 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset );
50 #define GROWL_MAX_LENGTH 256
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************
55  * This module should be used on windows with MSN (i think that you need to
56  * have MSN 7 or newer) to "advertise" what you are playing in VLC.
57  * You need to enable the "What I'm Listening To" option in MSN.
58  *****************************************************************************/
59 #define SERVER_DEFAULT "127.0.0.1"
60 #define SERVER_TEXT N_("Growl server")
61 #define SERVER_LONGTEXT N_("Growl server receiving notifications.")
62 #define PASS_DEFAULT ""
63 #define PASS_TEXT N_("Growl password")
64 #define PASS_LONGTEXT N_("Growl password on the server.")
65 #define PORT_TEXT N_("Growl UDP port")
66 #define PORT_LONGTEXT N_("Growl UPD port on the server.")
67 #define TTL_TEXT N_("Growl TTL")
68 #define TTL_LONGTEXT N_("Growl TTL.")
69
70 vlc_module_begin();
71     set_category( CAT_INTERFACE );
72     set_subcategory( SUBCAT_INTERFACE_CONTROL );
73     set_shortname( N_( "growl" ) );
74     set_description( _("Growl Notification Plugin") );
75
76     add_string( "growl-server", SERVER_DEFAULT, NULL,
77                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
78     add_string( "growl-password", PASS_DEFAULT, NULL,
79                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
80     add_integer( "growl-port", 9887, NULL,
81                 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
82     add_integer( "growl-ttl", 12, NULL,
83                 TTL_TEXT, TTL_LONGTEXT, VLC_TRUE );
84
85     set_capability( "interface", 0 );
86     set_callbacks( Open, Close );
87 vlc_module_end();
88
89 /*****************************************************************************
90  * Open: initialize and create stuff
91  *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
93 {
94     intf_thread_t *p_intf = (intf_thread_t *)p_this;
95
96     playlist_t *p_playlist = (playlist_t *)vlc_object_find(
97         p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
98
99     if( !p_playlist )
100     {
101         msg_Err( p_intf, "could not find playlist object" );
102         return VLC_ENOOBJ;
103     }
104
105     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
106     vlc_object_release( p_playlist );
107
108     RegisterToGrowl( p_this );
109     p_intf->pf_run = Run;
110
111     return VLC_SUCCESS;
112 }
113
114 /*****************************************************************************
115  * Close: destroy interface stuff
116  *****************************************************************************/
117 static void Close( vlc_object_t *p_this )
118 {
119     playlist_t *p_playlist = (playlist_t *)vlc_object_find(
120         p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
121
122     if( p_playlist )
123     {
124         var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
125         vlc_object_release( p_playlist );
126     }
127 }
128
129 /*****************************************************************************
130  * Run
131  *****************************************************************************/
132 static void Run( intf_thread_t *p_intf )
133 {
134     msleep( INTF_IDLE_SLEEP );
135 }
136
137 /*****************************************************************************
138  * ItemChange: Playlist item change callback
139  *****************************************************************************/
140 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
141                        vlc_value_t oldval, vlc_value_t newval, void *param )
142 {
143     char psz_tmp[GROWL_MAX_LENGTH];
144     playlist_t *p_playlist;
145     char *psz_title = NULL;
146     char *psz_artist = NULL;
147     char *psz_album = NULL;
148     input_thread_t *p_input;
149
150     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
151                                                 FIND_ANYWHERE );
152     if( !p_playlist ) return VLC_EGENERIC;
153
154     p_input = p_playlist->p_input;
155     vlc_object_release( p_playlist );
156     if( !p_input ) return VLC_SUCCESS;
157     vlc_object_yield( p_input );
158
159     if( p_input->b_dead || !p_input->input.p_item->psz_name )
160     {
161         /* Not playing anything ... */
162         vlc_object_release( p_input );
163         return VLC_SUCCESS;
164     }
165
166     /* Playing something ... */
167     psz_artist = vlc_input_item_GetInfo( p_input->input.p_item,
168                                          _("Meta-information"),
169                                          _(VLC_META_ARTIST) );
170     psz_album = vlc_input_item_GetInfo( p_input->input.p_item,
171                                          _("Meta-information"),
172                                          _("Album/movie/show title" ) );
173     psz_title = strdup( p_input->input.p_item->psz_name );
174     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
175     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
176     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
177     snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
178               psz_title, psz_artist, psz_album );
179     free( psz_title );
180     free( psz_artist );
181     free( psz_album );
182
183     NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
184
185     vlc_object_release( p_input );
186     return VLC_SUCCESS;
187 }
188
189 /*****************************************************************************
190  * Growl specific functions
191  *****************************************************************************/
192 #define GROWL_PROTOCOL_VERSION (1)
193 #define GROWL_TYPE_REGISTRATION (0)
194 #define GROWL_TYPE_NOTIFICATION (1)
195 #define APPLICATION_NAME "VLC media player"
196
197 #define insertstrlen( psz ) \
198 { \
199     uint16_t i_size = strlen( psz ); \
200     psz_encoded[i++] = (i_size>>8)&0xFF; \
201     psz_encoded[i++] = i_size&0xFF; \
202 }
203 /*****************************************************************************
204  * RegisterToGrowl
205  *****************************************************************************/
206 static int RegisterToGrowl( vlc_object_t *p_this )
207 {
208     uint8_t *psz_encoded = malloc(100);
209     uint8_t i_defaults = 0;
210     char *psz_notifications[] = {"Now Playing", NULL};
211     vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
212     int i = 0, j;
213     if( psz_encoded == NULL )
214         return VLC_FALSE;
215
216     memset( psz_encoded, 0, sizeof(psz_encoded) );
217     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
218     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
219     insertstrlen(APPLICATION_NAME);
220     i+=2;
221     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
222     i += strlen(APPLICATION_NAME);
223     for( j = 0 ; psz_notifications[j] != NULL ; j++)
224     {
225         insertstrlen(psz_notifications[j]);
226         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
227         i += strlen(psz_notifications[j]);
228     }
229     psz_encoded[4] = j;
230     for( j = 0 ; psz_notifications[j] != NULL ; j++)
231         if(pb_defaults[j] == VLC_TRUE)
232         {
233             psz_encoded[i++] = (uint8_t)j;
234             i_defaults++;
235         }
236     psz_encoded[5] = i_defaults;
237
238     CheckAndSend(p_this, psz_encoded, i);
239     free( psz_encoded );
240     return VLC_SUCCESS;
241 }
242
243 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
244                             char *psz_title, char *psz_desc )
245 {
246     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
247     uint16_t flags;
248     int i = 0;
249     if( psz_encoded == NULL )
250         return VLC_FALSE;
251
252     memset( psz_encoded, 0, sizeof(psz_encoded) );
253     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
254     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
255     flags = 0;
256     psz_encoded[i++] = (flags>>8)&0xFF;
257     psz_encoded[i++] = flags&0xFF;
258     insertstrlen(psz_type);
259     insertstrlen(psz_title);
260     insertstrlen(psz_desc);
261     insertstrlen(APPLICATION_NAME);
262     strcpy( (char*)(psz_encoded+i), psz_type );
263     i += strlen(psz_type);
264     strcpy( (char*)(psz_encoded+i), psz_title );
265     i += strlen(psz_title);
266     strcpy( (char*)(psz_encoded+i), psz_desc );
267     i += strlen(psz_desc);
268     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
269     i += strlen(APPLICATION_NAME);
270
271     CheckAndSend(p_this, psz_encoded, i);
272     free( psz_encoded );
273     return VLC_SUCCESS;
274 }
275
276 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
277 {
278     int i, i_handle;
279     struct md5_s md5;
280     intf_thread_t *p_intf = (intf_thread_t *)p_this;
281     char *psz_password = config_GetPsz( p_intf, "growl-password" );
282     char *psz_server = config_GetPsz( p_intf, "growl-server" );
283     int i_port = config_GetInt( p_intf, "growl-port" );
284     int i_ttl = config_GetInt( p_intf, "growl-ttl" );
285     strcpy( (char*)(p_data+i_offset), psz_password );
286     i = i_offset + strlen(psz_password);
287
288     InitMD5( &md5 );
289     AddMD5( &md5, p_data, i );
290     EndMD5( &md5 );
291
292     for( i = 0 ; i < 4 ; i++ )
293     {
294         md5.p_digest[i] = md5.p_digest[i];
295         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
296         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
297         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
298         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
299     }
300
301     i_handle = net_ConnectUDP( p_this, psz_server, i_port, i_ttl );
302     if( i_handle == -1 )
303     {
304          msg_Err( p_this, "failed to open a connection (udp)" );
305          free( psz_password);
306          free( psz_server);
307          return VLC_EGENERIC;
308     }
309
310     net_StopRecv( i_handle );
311     if( send( i_handle, p_data, i_offset, 0 )
312           == -1 )
313     {
314         msg_Warn( p_this, "send error: %s", strerror(errno) );
315     }
316     net_Close( i_handle );
317
318     free( psz_password);
319     free( psz_server);
320     return VLC_SUCCESS;
321 }
322
323 #undef GROWL_PROTOCOL_VERSION
324 #undef GROWL_TYPE_REGISTRATION
325 #undef GROWL_TYPE_NOTIFICATION
326 #undef APPLICATION_NAME
327 #undef insertstrlen