]> git.sesse.net Git - vlc/blob - modules/misc/growl.c
ec69de0f0285a22e4d168d4ff6c8bd2fd100b189
[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 #define SERVER_DEFAULT "127.0.0.1"
56 #define SERVER_TEXT N_("Growl server")
57 #define SERVER_LONGTEXT N_("This is the host to which Growl notifications " \
58    "will be sent. By default, notifications are sent locally." )
59 #define PASS_DEFAULT ""
60 #define PASS_TEXT N_("Growl password")
61 #define PASS_LONGTEXT N_("Growl password on the server.")
62 #define PORT_TEXT N_("Growl UDP port")
63 #define PORT_LONGTEXT N_("Growl UDP port on the server.")
64
65 vlc_module_begin();
66     set_category( CAT_INTERFACE );
67     set_subcategory( SUBCAT_INTERFACE_CONTROL );
68     set_shortname( N_( "Growl" ) );
69     set_description( _("Growl Notification Plugin") );
70
71     add_string( "growl-server", SERVER_DEFAULT, NULL,
72                 SERVER_TEXT, SERVER_LONGTEXT, VLC_FALSE );
73     add_string( "growl-password", PASS_DEFAULT, NULL,
74                 PASS_TEXT, PASS_LONGTEXT, VLC_FALSE );
75     add_integer( "growl-port", 9887, NULL,
76                 PORT_TEXT, PORT_LONGTEXT, VLC_TRUE );
77
78     set_capability( "interface", 0 );
79     set_callbacks( Open, Close );
80 vlc_module_end();
81
82 /*****************************************************************************
83  * Open: initialize and create stuff
84  *****************************************************************************/
85 static int Open( vlc_object_t *p_this )
86 {
87     intf_thread_t *p_intf = (intf_thread_t *)p_this;
88
89     playlist_t *p_playlist = (playlist_t *)vlc_object_find(
90         p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
91
92     if( !p_playlist )
93     {
94         msg_Err( p_intf, "could not find playlist object" );
95         return VLC_ENOOBJ;
96     }
97
98     var_AddCallback( p_playlist, "playlist-current", ItemChange, p_intf );
99     vlc_object_release( p_playlist );
100
101     RegisterToGrowl( p_this );
102     p_intf->pf_run = Run;
103
104     return VLC_SUCCESS;
105 }
106
107 /*****************************************************************************
108  * Close: destroy interface stuff
109  *****************************************************************************/
110 static void Close( vlc_object_t *p_this )
111 {
112     playlist_t *p_playlist = (playlist_t *)vlc_object_find(
113         p_this, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
114
115     if( p_playlist )
116     {
117         var_DelCallback( p_playlist, "playlist-current", ItemChange, p_this );
118         vlc_object_release( p_playlist );
119     }
120 }
121
122 /*****************************************************************************
123  * Run
124  *****************************************************************************/
125 static void Run( intf_thread_t *p_intf )
126 {
127     msleep( INTF_IDLE_SLEEP );
128 }
129
130 /*****************************************************************************
131  * ItemChange: Playlist item change callback
132  *****************************************************************************/
133 static int ItemChange( vlc_object_t *p_this, const char *psz_var,
134                        vlc_value_t oldval, vlc_value_t newval, void *param )
135 {
136     char psz_tmp[GROWL_MAX_LENGTH];
137     playlist_t *p_playlist;
138     char *psz_title = NULL;
139     char *psz_artist = NULL;
140     char *psz_album = NULL;
141     input_thread_t *p_input;
142
143     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_INPUT,
144                                                 FIND_ANYWHERE );
145     if( !p_playlist ) return VLC_EGENERIC;
146
147     p_input = p_playlist->p_input;
148     vlc_object_release( p_playlist );
149     if( !p_input ) return VLC_SUCCESS;
150     vlc_object_yield( p_input );
151
152     if( p_input->b_dead || !p_input->input.p_item->psz_name )
153     {
154         /* Not playing anything ... */
155         vlc_object_release( p_input );
156         return VLC_SUCCESS;
157     }
158
159     /* Playing something ... */
160     psz_artist = vlc_input_item_GetInfo( p_input->input.p_item,
161                                          _("Meta-information"),
162                                          _(VLC_META_ARTIST) );
163     psz_album = vlc_input_item_GetInfo( p_input->input.p_item,
164                                          _("Meta-information"),
165                                          _("Album/movie/show title" ) );
166     psz_title = strdup( p_input->input.p_item->psz_name );
167     if( psz_title == NULL ) psz_title = strdup( N_("(no title)") );
168     if( psz_artist == NULL ) psz_artist = strdup( N_("(no artist)") );
169     if( psz_album == NULL ) psz_album = strdup( N_("(no album)") );
170     snprintf( psz_tmp, GROWL_MAX_LENGTH, "%s %s %s",
171               psz_title, psz_artist, psz_album );
172     free( psz_title );
173     free( psz_artist );
174     free( psz_album );
175
176     NotifyToGrowl( p_this, "Now Playing", "Now Playing", psz_tmp );
177
178     vlc_object_release( p_input );
179     return VLC_SUCCESS;
180 }
181
182 /*****************************************************************************
183  * Growl specific functions
184  *****************************************************************************/
185 #define GROWL_PROTOCOL_VERSION (1)
186 #define GROWL_TYPE_REGISTRATION (0)
187 #define GROWL_TYPE_NOTIFICATION (1)
188 #define APPLICATION_NAME "VLC media player"
189
190 #define insertstrlen( psz ) \
191 { \
192     uint16_t i_size = strlen( psz ); \
193     psz_encoded[i++] = (i_size>>8)&0xFF; \
194     psz_encoded[i++] = i_size&0xFF; \
195 }
196 /*****************************************************************************
197  * RegisterToGrowl
198  *****************************************************************************/
199 static int RegisterToGrowl( vlc_object_t *p_this )
200 {
201     uint8_t *psz_encoded = malloc(100);
202     uint8_t i_defaults = 0;
203     char *psz_notifications[] = {"Now Playing", NULL};
204     vlc_bool_t pb_defaults[] = {VLC_TRUE, VLC_FALSE};
205     int i = 0, j;
206     if( psz_encoded == NULL )
207         return VLC_FALSE;
208
209     memset( psz_encoded, 0, sizeof(psz_encoded) );
210     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
211     psz_encoded[i++] = GROWL_TYPE_REGISTRATION;
212     insertstrlen(APPLICATION_NAME);
213     i+=2;
214     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
215     i += strlen(APPLICATION_NAME);
216     for( j = 0 ; psz_notifications[j] != NULL ; j++)
217     {
218         insertstrlen(psz_notifications[j]);
219         strcpy( (char*)(psz_encoded+i), psz_notifications[j] );
220         i += strlen(psz_notifications[j]);
221     }
222     psz_encoded[4] = j;
223     for( j = 0 ; psz_notifications[j] != NULL ; j++)
224         if(pb_defaults[j] == VLC_TRUE)
225         {
226             psz_encoded[i++] = (uint8_t)j;
227             i_defaults++;
228         }
229     psz_encoded[5] = i_defaults;
230
231     CheckAndSend(p_this, psz_encoded, i);
232     free( psz_encoded );
233     return VLC_SUCCESS;
234 }
235
236 static int NotifyToGrowl( vlc_object_t *p_this, char *psz_type,
237                             char *psz_title, char *psz_desc )
238 {
239     uint8_t *psz_encoded = malloc(GROWL_MAX_LENGTH + 42);
240     uint16_t flags;
241     int i = 0;
242     if( psz_encoded == NULL )
243         return VLC_FALSE;
244
245     memset( psz_encoded, 0, sizeof(psz_encoded) );
246     psz_encoded[i++] = GROWL_PROTOCOL_VERSION;
247     psz_encoded[i++] = GROWL_TYPE_NOTIFICATION;
248     flags = 0;
249     psz_encoded[i++] = (flags>>8)&0xFF;
250     psz_encoded[i++] = flags&0xFF;
251     insertstrlen(psz_type);
252     insertstrlen(psz_title);
253     insertstrlen(psz_desc);
254     insertstrlen(APPLICATION_NAME);
255     strcpy( (char*)(psz_encoded+i), psz_type );
256     i += strlen(psz_type);
257     strcpy( (char*)(psz_encoded+i), psz_title );
258     i += strlen(psz_title);
259     strcpy( (char*)(psz_encoded+i), psz_desc );
260     i += strlen(psz_desc);
261     strcpy( (char*)(psz_encoded+i), APPLICATION_NAME );
262     i += strlen(APPLICATION_NAME);
263
264     CheckAndSend(p_this, psz_encoded, i);
265     free( psz_encoded );
266     return VLC_SUCCESS;
267 }
268
269 static int CheckAndSend( vlc_object_t *p_this, uint8_t* p_data, int i_offset )
270 {
271     int i, i_handle;
272     struct md5_s md5;
273     intf_thread_t *p_intf = (intf_thread_t *)p_this;
274     char *psz_password = config_GetPsz( p_intf, "growl-password" );
275     char *psz_server = config_GetPsz( p_intf, "growl-server" );
276     int i_port = config_GetInt( p_intf, "growl-port" );
277     strcpy( (char*)(p_data+i_offset), psz_password );
278     i = i_offset + strlen(psz_password);
279
280     InitMD5( &md5 );
281     AddMD5( &md5, p_data, i );
282     EndMD5( &md5 );
283
284     for( i = 0 ; i < 4 ; i++ )
285     {
286         md5.p_digest[i] = md5.p_digest[i];
287         p_data[i_offset++] =  md5.p_digest[i]     &0xFF;
288         p_data[i_offset++] = (md5.p_digest[i]>> 8)&0xFF;
289         p_data[i_offset++] = (md5.p_digest[i]>>16)&0xFF;
290         p_data[i_offset++] = (md5.p_digest[i]>>24)&0xFF;
291     }
292
293     i_handle = net_ConnectUDP( p_this, psz_server, i_port, 0 );
294     if( i_handle == -1 )
295     {
296          msg_Err( p_this, "failed to open a connection (udp)" );
297          free( psz_password);
298          free( psz_server);
299          return VLC_EGENERIC;
300     }
301
302     net_StopRecv( i_handle );
303     if( send( i_handle, p_data, i_offset, 0 )
304           == -1 )
305     {
306         msg_Warn( p_this, "send error: %s", strerror(errno) );
307     }
308     net_Close( i_handle );
309
310     free( psz_password);
311     free( psz_server);
312     return VLC_SUCCESS;
313 }
314
315 #undef GROWL_PROTOCOL_VERSION
316 #undef GROWL_TYPE_REGISTRATION
317 #undef GROWL_TYPE_NOTIFICATION
318 #undef APPLICATION_NAME
319 #undef insertstrlen