d9e631cb1e9b5c8646f506690aeec302dfaa1a53
[vlc.git] / projects / mozilla / vlcplugin.cpp
1 /*****************************************************************************
2  * vlcplugin.cpp: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf.fouilleul@laposte.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "config.h"
29
30 #ifdef HAVE_MOZILLA_CONFIG_H
31 #   include <mozilla-config.h>
32 #endif
33
34 #include "vlcplugin.h"
35 #include "control/npolibvlc.h"
36
37 #include <ctype.h>
38
39 /*****************************************************************************
40  * VlcPlugin constructor and destructor
41  *****************************************************************************/
42 VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
43     i_npmode(mode),
44     b_stream(0),
45     b_autoplay(1),
46     b_toolbar(0),
47     psz_target(NULL),
48     libvlc_instance(NULL),
49     libvlc_log(NULL),
50     p_scriptClass(NULL),
51     p_browser(instance),
52     psz_baseURL(NULL)
53 #if XP_WIN
54     ,pf_wndproc(NULL)
55 #endif
56 #if XP_UNIX
57     ,i_width((unsigned)-1)
58     ,i_height((unsigned)-1)
59     ,i_tb_width(0)
60     ,i_tb_height(0)
61     ,i_last_position(0)
62 #endif
63 {
64     memset(&npwindow, 0, sizeof(NPWindow));
65 }
66
67 static bool boolValue(const char *value) {
68     return ( !strcmp(value, "1") ||
69              !strcasecmp(value, "true") ||
70              !strcasecmp(value, "yes") );
71 }
72
73 NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
74 {
75     /* prepare VLC command line */
76     char *ppsz_argv[32];
77     int ppsz_argc = 0;
78
79     /* locate VLC module path */
80 #ifdef XP_MACOSX
81     ppsz_argv[ppsz_argc++] = "--plugin-path";
82     ppsz_argv[ppsz_argc++] = "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
83                              "Contents/MacOS/modules";
84 #elif defined(XP_WIN)
85     HKEY h_key;
86     DWORD i_type, i_data = MAX_PATH + 1;
87     char p_data[MAX_PATH + 1];
88     if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
89                       0, KEY_READ, &h_key ) == ERROR_SUCCESS )
90     {
91          if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
92                               (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
93          {
94              if( i_type == REG_SZ )
95              {
96                  strcat( p_data, "\\plugins" );
97                  ppsz_argv[ppsz_argc++] = "--plugin-path";
98                  ppsz_argv[ppsz_argc++] = p_data;
99              }
100          }
101          RegCloseKey( h_key );
102     }
103     ppsz_argv[ppsz_argc++] = "--no-one-instance";
104
105 #endif /* XP_MACOSX */
106
107     /* common settings */
108     ppsz_argv[ppsz_argc++] = "-vv";
109     ppsz_argv[ppsz_argc++] = "--no-stats";
110     ppsz_argv[ppsz_argc++] = "--no-media-library";
111     ppsz_argv[ppsz_argc++] = "--ignore-config";
112     ppsz_argv[ppsz_argc++] = "--intf";
113     ppsz_argv[ppsz_argc++] = "dummy";
114
115     const char *progid = NULL;
116
117     /* parse plugin arguments */
118     for( int i = 0; i < argc ; i++ )
119     {
120         fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]);
121
122         if( !strcmp( argn[i], "target" )
123          || !strcmp( argn[i], "mrl")
124          || !strcmp( argn[i], "filename")
125          || !strcmp( argn[i], "src") )
126         {
127             psz_target = argv[i];
128         }
129         else if( !strcmp( argn[i], "autoplay")
130               || !strcmp( argn[i], "autostart") )
131         {
132             b_autoplay = boolValue(argv[i]);
133         }
134         else if( !strcmp( argn[i], "fullscreen" ) )
135         {
136             if( boolValue(argv[i]) )
137             {
138                 ppsz_argv[ppsz_argc++] = "--fullscreen";
139             }
140             else
141             {
142                 ppsz_argv[ppsz_argc++] = "--no-fullscreen";
143             }
144         }
145         else if( !strcmp( argn[i], "mute" ) )
146         {
147             if( boolValue(argv[i]) )
148             {
149                 ppsz_argv[ppsz_argc++] = "--volume";
150                 ppsz_argv[ppsz_argc++] = "0";
151             }
152         }
153         else if( !strcmp( argn[i], "loop")
154               || !strcmp( argn[i], "autoloop") )
155         {
156             if( boolValue(argv[i]) )
157             {
158                 ppsz_argv[ppsz_argc++] = "--loop";
159             }
160             else {
161                 ppsz_argv[ppsz_argc++] = "--no-loop";
162             }
163         }
164         else if( !strcmp( argn[i], "version")
165               || !strcmp( argn[i], "progid") )
166         {
167             progid = argv[i];
168         }
169         else if( !strcmp( argn[i], "toolbar" ) )
170         {
171             b_toolbar = boolValue(argv[i]);
172         }
173     }
174
175     libvlc_exception_t ex;
176     libvlc_exception_init(&ex);
177
178     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
179     if( libvlc_exception_raised(&ex) )
180     {
181         libvlc_exception_clear(&ex);
182         return NPERR_GENERIC_ERROR;
183     }
184
185     /*
186     ** fetch plugin base URL, which is the URL of the page containing the plugin
187     ** this URL is used for making absolute URL from relative URL that may be
188     ** passed as an MRL argument
189     */
190     NPObject *plugin;
191
192     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
193     {
194         /*
195         ** is there a better way to get that info ?
196         */
197         static const char docLocHref[] = "document.location.href";
198         NPString script;
199         NPVariant result;
200
201         script.utf8characters = docLocHref;
202         script.utf8length = sizeof(docLocHref)-1;
203
204         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
205         {
206             if( NPVARIANT_IS_STRING(result) )
207             {
208                 NPString &location = NPVARIANT_TO_STRING(result);
209
210                 psz_baseURL = new char[location.utf8length+1];
211                 if( psz_baseURL )
212                 {
213                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
214                     psz_baseURL[location.utf8length] = '\0';
215                 }
216             }
217             NPN_ReleaseVariantValue(&result);
218         }
219         NPN_ReleaseObject(plugin);
220     }
221
222     if( psz_target )
223     {
224         // get absolute URL from src
225         char *psz_absurl = getAbsoluteURL(psz_target);
226         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
227     }
228
229     /* assign plugin script root class */
230     /* new APIs */
231     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
232
233     return NPERR_NO_ERROR;
234 }
235
236 VlcPlugin::~VlcPlugin()
237 {
238     delete psz_baseURL;
239     delete psz_target;
240     if( libvlc_log )
241         libvlc_log_close(libvlc_log, NULL);
242     if( libvlc_instance )
243         libvlc_release(libvlc_instance);
244 }
245
246 /*****************************************************************************
247  * VlcPlugin methods
248  *****************************************************************************/
249
250 char *VlcPlugin::getAbsoluteURL(const char *url)
251 {
252     if( NULL != url )
253     {
254         // check whether URL is already absolute
255         const char *end=strchr(url, ':');
256         if( (NULL != end) && (end != url) )
257         {
258             // validate protocol header
259             const char *start = url;
260             char c = *start;
261             if( isalpha(c) )
262             {
263                 ++start;
264                 while( start != end )
265                 {
266                     c  = *start;
267                     if( ! (isalnum(c)
268                        || ('-' == c)
269                        || ('+' == c)
270                        || ('.' == c)
271                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
272                         // not valid protocol header, assume relative URL
273                         goto relativeurl;
274                     ++start;
275                 }
276                 /* we have a protocol header, therefore URL is absolute */
277                 return strdup(url);
278             }
279             // not a valid protocol header, assume relative URL
280         }
281
282 relativeurl:
283
284         if( psz_baseURL )
285         {
286             size_t baseLen = strlen(psz_baseURL);
287             char *href = new char[baseLen+strlen(url)+1];
288             if( href )
289             {
290                 /* prepend base URL */
291                 strcpy(href, psz_baseURL);
292
293                 /*
294                 ** relative url could be empty,
295                 ** in which case return base URL
296                 */
297                 if( '\0' == *url )
298                     return href;
299
300                 /*
301                 ** locate pathname part of base URL
302                 */
303
304                 /* skip over protocol part  */
305                 char *pathstart = strchr(href, ':');
306                 char *pathend;
307                 if( pathstart )
308                 {
309                     if( '/' == *(++pathstart) )
310                     {
311                         if( '/' == *(++pathstart) )
312                         {
313                             ++pathstart;
314                         }
315                     }
316                     /* skip over host part */
317                     pathstart = strchr(pathstart, '/');
318                     pathend = href+baseLen;
319                     if( ! pathstart )
320                     {
321                         // no path, add a / past end of url (over '\0')
322                         pathstart = pathend;
323                         *pathstart = '/';
324                     }
325                 }
326                 else
327                 {
328                     /* baseURL is just a UNIX path */
329                     if( '/' != *href )
330                     {
331                         /* baseURL is not an absolute path */
332                         return NULL;
333                     }
334                     pathstart = href;
335                     pathend = href+baseLen;
336                 }
337
338                 /* relative URL made of an absolute path ? */
339                 if( '/' == *url )
340                 {
341                     /* replace path completely */
342                     strcpy(pathstart, url);
343                     return href;
344                 }
345
346                 /* find last path component and replace it */
347                 while( '/' != *pathend)
348                     --pathend;
349
350                 /*
351                 ** if relative url path starts with one or more '../',
352                 ** factor them out of href so that we return a
353                 ** normalized URL
354                 */
355                 while( pathend != pathstart )
356                 {
357                     const char *p = url;
358                     if( '.' != *p )
359                         break;
360                     ++p;
361                     if( '\0' == *p  )
362                     {
363                         /* relative url is just '.' */
364                         url = p;
365                         break;
366                     }
367                     if( '/' == *p  )
368                     {
369                         /* relative url starts with './' */
370                         url = ++p;
371                         continue;
372                     }
373                     if( '.' != *p )
374                         break;
375                     ++p;
376                     if( '\0' == *p )
377                     {
378                         /* relative url is '..' */
379                     }
380                     else
381                     {
382                         if( '/' != *p )
383                             break;
384                         /* relative url starts with '../' */
385                         ++p;
386                     }
387                     url = p;
388                     do
389                     {
390                         --pathend;
391                     }
392                     while( '/' != *pathend );
393                 }
394                 /* skip over '/' separator */
395                 ++pathend;
396                 /* concatenate remaining base URL and relative URL */
397                 strcpy(pathend, url);
398             }
399             return href;
400         }
401     }
402     return NULL;
403 }
404
405 #if XP_UNIX
406 int  VlcPlugin::setSize(unsigned width, unsigned height)
407 {
408     int diff = (width != i_width) || (height != i_height);
409
410     i_width = width;
411     i_height = height;
412
413     /* return size */
414     return diff;
415 }
416
417 void VlcPlugin::showToolbar()
418 {
419     const NPWindow& window = getWindow();
420     Window control = getControlWindow();
421     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
422     unsigned int i_height = 0, i_width = 0;
423
424     /* load icons */
425     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
426                         &p_btnPlay, NULL, NULL);
427     if( p_btnPlay )
428     {
429         i_height = __MAX( i_height, p_btnPlay->height );
430         i_width += p_btnPlay->width;
431     }
432     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
433                         &p_btnPause, NULL, NULL);
434     if( p_btnPause )
435     {
436         i_height = __MAX( i_height, p_btnPause->height );
437         i_width += p_btnPause->width;
438     }
439     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
440                         &p_btnStop, NULL, NULL );
441     if( p_btnStop )
442     {
443         i_height = __MAX( i_height, p_btnStop->height );
444         i_width += p_btnStop->width;
445     }
446     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
447                         &p_timeline, NULL, NULL);
448     if( p_timeline )
449     {
450         i_height = __MAX( i_height, p_timeline->height );
451         i_width += p_timeline->width;
452     }
453     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
454                         &p_btnTime, NULL, NULL);
455     if( p_btnTime )
456     {
457         i_height = __MAX( i_height, p_btnTime->height );
458         i_width += p_btnTime->width;
459     }
460     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
461                         &p_btnFullscreen, NULL, NULL);
462     if( p_btnFullscreen )
463     {
464         i_height = __MAX( i_height, p_btnFullscreen->height );
465         i_width += p_btnFullscreen->width;
466     }
467     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
468                         &p_btnMute, NULL, NULL);
469     if( p_btnMute )
470     {
471         i_height = __MAX( i_height, p_btnMute->height );
472         i_width += p_btnMute->width;
473     }
474     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
475                         &p_btnUnmute, NULL, NULL);
476     if( p_btnUnmute )
477     {
478         i_height = __MAX( i_height, p_btnUnmute->height );
479         i_width += p_btnUnmute->width;
480     }
481     setToolbarSize( i_width, i_height );
482
483     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
484         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
485         fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
486 }
487
488 void VlcPlugin::hideToolbar()
489 {
490     i_tb_width = i_tb_height = 0;
491
492     if( p_btnPlay )  XDestroyImage( p_btnPlay );
493     if( p_btnPause ) XDestroyImage( p_btnPause );
494     if( p_btnStop )  XDestroyImage( p_btnStop );
495     if( p_timeline ) XDestroyImage( p_timeline );
496     if( p_btnTime )  XDestroyImage( p_btnTime );
497     if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
498     if( p_btnMute )  XDestroyImage( p_btnMute );
499     if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
500
501     p_btnPlay = NULL;
502     p_btnPause = NULL;
503     p_btnStop = NULL;
504     p_timeline = NULL;
505     p_btnTime = NULL;
506     p_btnFullscreen = NULL;
507     p_btnMute = NULL;
508     p_btnUnmute = NULL;
509 }
510
511 void VlcPlugin::redrawToolbar()
512 {
513     libvlc_media_player_t *p_md = NULL;
514     libvlc_exception_t ex;
515     float f_position = 0.0;
516     int i_playing = 0;
517     bool b_mute = false;
518     unsigned int dst_x, dst_y;
519     GC gc;
520     XGCValues gcv;
521 #define BTN_SPACE ((unsigned int)4)
522
523     const NPWindow& window = getWindow();
524     Window control = getControlWindow();
525     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
526
527     /* get media instance */
528     libvlc_exception_init( &ex );
529     p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
530     libvlc_exception_clear( &ex );
531
532     /* get isplaying */
533     libvlc_exception_init( &ex );
534     i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
535     libvlc_exception_clear( &ex );
536
537     /* get mute info */
538     libvlc_exception_init(&ex);
539     b_mute = libvlc_audio_get_mute( getVLC(), &ex );
540     libvlc_exception_clear( &ex );
541
542     /* get movie position in % */
543     if( i_playing == 1 )
544     {
545         libvlc_exception_init( &ex );
546         f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
547         libvlc_exception_clear( &ex );
548     }
549     libvlc_media_player_release( p_md );
550
551     gcv.foreground = BlackPixel( p_display, 0 );
552     gc = XCreateGC( p_display, control, GCForeground, &gcv );
553
554     XFillRectangle( p_display, control, gc,
555                     0, 0, window.width, i_tb_height );
556     gcv.foreground = WhitePixel( p_display, 0 );
557     XChangeGC( p_display, gc, GCForeground, &gcv );
558
559     /* position icons */
560     dst_x = 4; dst_y = 4;
561
562     fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
563     if( p_btnPause && (i_playing == 1) )
564     {
565         XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x, dst_y,
566                    p_btnPause->width, p_btnPause->height );
567     }
568     else if( p_btnPlay )
569     {
570         XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x, dst_y,
571                    p_btnPlay->width, p_btnPlay->height );
572     }
573
574     dst_x += BTN_SPACE + ( p_btnPlay ? p_btnPlay->width : 0 );
575     dst_y = 4;
576
577     if( p_btnStop )
578         XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x, dst_y,
579                    p_btnStop->width, p_btnStop->height );
580
581     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
582     dst_y = 4;
583
584     if( p_btnFullscreen )
585         XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x, dst_y,
586                    p_btnFullscreen->width, p_btnFullscreen->height );
587
588     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
589     dst_y = 4;
590
591     if( p_btnUnmute && b_mute )
592     {
593         XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x, dst_y,
594                    p_btnUnmute->width, p_btnUnmute->height );
595
596         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
597         dst_y = 4;
598     }
599     else if( p_btnMute )
600     {
601         XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x, dst_y,
602                    p_btnMute->width, p_btnMute->height );
603
604         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
605         dst_y = 4;
606     }
607
608     if( p_timeline )
609         XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x, dst_y,
610                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
611
612     if( f_position > 0 )
613         i_last_position = (((float)window.width-8.0)/100.0)*f_position;
614
615     if( p_btnTime )
616         XPutImage( p_display, control, gc, p_btnTime,
617                    0, 0, (dst_x+i_last_position), dst_y,
618                    p_btnTime->width, p_btnTime->height );
619
620     XFreeGC( p_display, gc );
621 }
622 #endif