]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.cpp
Remove deadcode.
[vlc] / 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
176     libvlc_exception_t ex;
177     libvlc_exception_init(&ex);
178
179     libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
180     if( libvlc_exception_raised(&ex) )
181     {
182         libvlc_exception_clear(&ex);
183         return NPERR_GENERIC_ERROR;
184     }
185
186     /*
187     ** fetch plugin base URL, which is the URL of the page containing the plugin
188     ** this URL is used for making absolute URL from relative URL that may be
189     ** passed as an MRL argument
190     */
191     NPObject *plugin;
192
193     if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
194     {
195         /*
196         ** is there a better way to get that info ?
197         */
198         static const char docLocHref[] = "document.location.href";
199         NPString script;
200         NPVariant result;
201
202         script.utf8characters = docLocHref;
203         script.utf8length = sizeof(docLocHref)-1;
204
205         if( NPN_Evaluate(p_browser, plugin, &script, &result) )
206         {
207             if( NPVARIANT_IS_STRING(result) )
208             {
209                 NPString &location = NPVARIANT_TO_STRING(result);
210
211                 psz_baseURL = new char[location.utf8length+1];
212                 if( psz_baseURL )
213                 {
214                     strncpy(psz_baseURL, location.utf8characters, location.utf8length);
215                     psz_baseURL[location.utf8length] = '\0';
216                 }
217             }
218             NPN_ReleaseVariantValue(&result);
219         }
220         NPN_ReleaseObject(plugin);
221     }
222
223     if( psz_target )
224     {
225         // get absolute URL from src
226         char *psz_absurl = getAbsoluteURL(psz_target);
227         psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
228     }
229
230     /* assign plugin script root class */
231     /* new APIs */
232     p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
233
234     return NPERR_NO_ERROR;
235 }
236
237 VlcPlugin::~VlcPlugin()
238 {
239     delete psz_baseURL;
240     delete psz_target;
241     if( libvlc_log )
242         libvlc_log_close(libvlc_log, NULL);
243     if( libvlc_instance )
244         libvlc_release(libvlc_instance);
245 }
246
247 /*****************************************************************************
248  * VlcPlugin methods
249  *****************************************************************************/
250
251 char *VlcPlugin::getAbsoluteURL(const char *url)
252 {
253     if( NULL != url )
254     {
255         // check whether URL is already absolute
256         const char *end=strchr(url, ':');
257         if( (NULL != end) && (end != url) )
258         {
259             // validate protocol header
260             const char *start = url;
261             char c = *start;
262             if( isalpha(c) )
263             {
264                 ++start;
265                 while( start != end )
266                 {
267                     c  = *start;
268                     if( ! (isalnum(c)
269                        || ('-' == c)
270                        || ('+' == c)
271                        || ('.' == c)
272                        || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
273                         // not valid protocol header, assume relative URL
274                         goto relativeurl;
275                     ++start;
276                 }
277                 /* we have a protocol header, therefore URL is absolute */
278                 return strdup(url);
279             }
280             // not a valid protocol header, assume relative URL
281         }
282
283 relativeurl:
284
285         if( psz_baseURL )
286         {
287             size_t baseLen = strlen(psz_baseURL);
288             char *href = new char[baseLen+strlen(url)+1];
289             if( href )
290             {
291                 /* prepend base URL */
292                 strcpy(href, psz_baseURL);
293
294                 /*
295                 ** relative url could be empty,
296                 ** in which case return base URL
297                 */
298                 if( '\0' == *url )
299                     return href;
300
301                 /*
302                 ** locate pathname part of base URL
303                 */
304
305                 /* skip over protocol part  */
306                 char *pathstart = strchr(href, ':');
307                 char *pathend;
308                 if( pathstart )
309                 {
310                     if( '/' == *(++pathstart) )
311                     {
312                         if( '/' == *(++pathstart) )
313                         {
314                             ++pathstart;
315                         }
316                     }
317                     /* skip over host part */
318                     pathstart = strchr(pathstart, '/');
319                     pathend = href+baseLen;
320                     if( ! pathstart )
321                     {
322                         // no path, add a / past end of url (over '\0')
323                         pathstart = pathend;
324                         *pathstart = '/';
325                     }
326                 }
327                 else
328                 {
329                     /* baseURL is just a UNIX path */
330                     if( '/' != *href )
331                     {
332                         /* baseURL is not an absolute path */
333                         return NULL;
334                     }
335                     pathstart = href;
336                     pathend = href+baseLen;
337                 }
338
339                 /* relative URL made of an absolute path ? */
340                 if( '/' == *url )
341                 {
342                     /* replace path completely */
343                     strcpy(pathstart, url);
344                     return href;
345                 }
346
347                 /* find last path component and replace it */
348                 while( '/' != *pathend)
349                     --pathend;
350
351                 /*
352                 ** if relative url path starts with one or more '../',
353                 ** factor them out of href so that we return a
354                 ** normalized URL
355                 */
356                 while( pathend != pathstart )
357                 {
358                     const char *p = url;
359                     if( '.' != *p )
360                         break;
361                     ++p;
362                     if( '\0' == *p  )
363                     {
364                         /* relative url is just '.' */
365                         url = p;
366                         break;
367                     }
368                     if( '/' == *p  )
369                     {
370                         /* relative url starts with './' */
371                         url = ++p;
372                         continue;
373                     }
374                     if( '.' != *p )
375                         break;
376                     ++p;
377                     if( '\0' == *p )
378                     {
379                         /* relative url is '..' */
380                     }
381                     else
382                     {
383                         if( '/' != *p )
384                             break;
385                         /* relative url starts with '../' */
386                         ++p;
387                     }
388                     url = p;
389                     do
390                     {
391                         --pathend;
392                     }
393                     while( '/' != *pathend );
394                 }
395                 /* skip over '/' separator */
396                 ++pathend;
397                 /* concatenate remaining base URL and relative URL */
398                 strcpy(pathend, url);
399             }
400             return href;
401         }
402     }
403     return NULL;
404 }
405
406 #if XP_UNIX
407 int  VlcPlugin::setSize(unsigned width, unsigned height)
408 {
409     int diff = (width != i_width) || (height != i_height);
410
411     i_width = width;
412     i_height = height;
413
414     /* return size */
415     return diff;
416 }
417
418 void VlcPlugin::showToolbar()
419 {
420     const NPWindow& window = getWindow();
421     Window control = getControlWindow();
422     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
423     unsigned int i_height = 0, i_width = 0;
424
425     /* load icons */
426     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
427                         &p_btnPlay, NULL, NULL);
428     if( p_btnPlay )
429     {
430         i_height = __MAX( i_height, p_btnPlay->height );
431         i_width  = __MAX( i_width,  p_btnPlay->width );
432     }
433     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
434                         &p_btnPause, NULL, NULL);
435     if( p_btnPause )
436     {
437         i_height = __MAX( i_height, p_btnPause->height );
438         i_width  = __MAX( i_width,  p_btnPause->width );
439     }
440     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
441                         &p_btnStop, NULL, NULL );
442     if( p_btnStop )
443     {
444         i_height = __MAX( i_height, p_btnStop->height );
445         i_width  = __MAX( i_width,  p_btnStop->width );
446     }
447     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
448                         &p_timeline, NULL, NULL);
449     if( p_timeline )
450     {
451         i_height = __MAX( i_height, p_timeline->height );
452         i_width  = __MAX( i_width,  p_timeline->width );
453     }
454     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
455                         &p_btnTime, NULL, NULL);
456     if( p_btnTime )
457     {
458         i_height = __MAX( i_height, p_btnTime->height );
459         i_width  = __MAX( i_width,  p_btnTime->width );
460     }
461     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
462                         &p_btnFullscreen, NULL, NULL);
463     if( p_btnFullscreen )
464     {
465         i_height = __MAX( i_height, p_btnFullscreen->height );
466         i_width  = __MAX( i_width,  p_btnFullscreen->width );
467     }
468     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
469                         &p_btnMute, NULL, NULL);
470     if( p_btnMute )
471     {
472         i_height = __MAX( i_height, p_btnMute->height );
473         i_width  = __MAX( i_width,  p_btnMute->width );
474     }
475     XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
476                         &p_btnUnmute, NULL, NULL);
477     if( p_btnUnmute )
478     {
479         i_height = __MAX( i_height, p_btnUnmute->height );
480         i_width  = __MAX( i_width,  p_btnUnmute->width );
481     }
482     setToolbarSize( i_width, i_height );
483
484     if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
485         !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
486         fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
487 }
488
489 void VlcPlugin::hideToolbar()
490 {
491     i_tb_width = i_tb_height = 0;
492
493     if( p_btnPlay )  XDestroyImage( p_btnPlay );
494     if( p_btnPause ) XDestroyImage( p_btnPause );
495     if( p_btnStop )  XDestroyImage( p_btnStop );
496     if( p_timeline ) XDestroyImage( p_timeline );
497     if( p_btnTime )  XDestroyImage( p_btnTime );
498     if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
499     if( p_btnMute )  XDestroyImage( p_btnMute );
500     if( p_btnUnmute ) XDestroyImage( p_btnUnmute );
501
502     p_btnPlay = NULL;
503     p_btnPause = NULL;
504     p_btnStop = NULL;
505     p_timeline = NULL;
506     p_btnTime = NULL;
507     p_btnFullscreen = NULL;
508     p_btnMute = NULL;
509     p_btnUnmute = NULL;
510 }
511
512 void VlcPlugin::redrawToolbar()
513 {
514     libvlc_media_player_t *p_md = NULL;
515     libvlc_exception_t ex;
516     float f_position = 0.0;
517     int i_playing = 0;
518     bool b_mute = false;
519     unsigned int dst_x, dst_y;
520     GC gc;
521     XGCValues gcv;
522 #define BTN_SPACE ((unsigned int)4)
523
524     const NPWindow& window = getWindow();
525     Window control = getControlWindow();
526     Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
527
528     /* get media instance */
529     libvlc_exception_init( &ex );
530     p_md = libvlc_playlist_get_media_player( getVLC(), &ex );
531     libvlc_exception_clear( &ex );
532
533     /* get isplaying */
534     libvlc_exception_init( &ex );
535     i_playing = libvlc_playlist_isplaying( getVLC(), &ex );
536     libvlc_exception_clear( &ex );
537
538     /* get mute info */
539     libvlc_exception_init(&ex);
540     b_mute = libvlc_audio_get_mute( getVLC(), &ex );
541     libvlc_exception_clear( &ex );
542
543     /* get movie position in % */
544     if( i_playing == 1 )
545     {
546         libvlc_exception_init( &ex );
547         f_position = libvlc_media_player_get_position( p_md, &ex ) * 100;
548         libvlc_exception_clear( &ex );
549     }
550     libvlc_media_player_release( p_md );
551
552     gcv.foreground = BlackPixel( p_display, 0 );
553     gc = XCreateGC( p_display, control, GCForeground, &gcv );
554
555     XFillRectangle( p_display, control, gc,
556                     0, 0, window.width, i_tb_height );
557     gcv.foreground = WhitePixel( p_display, 0 );
558     XChangeGC( p_display, gc, GCForeground, &gcv );
559
560     /* position icons */
561     dst_x = 4; dst_y = 4;
562
563     fprintf( stderr, ">>>>>> is playing = %d\n", i_playing );
564     if( p_btnPause && (i_playing == 1) )
565     {
566         XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x, dst_y,
567                    p_btnPause->width, p_btnPause->height );
568     }
569     else if( p_btnPlay )
570     {
571         XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x, dst_y,
572                    p_btnPlay->width, p_btnPlay->height );
573     }
574
575     dst_x += BTN_SPACE + ( p_btnPlay ? p_btnPlay->width : 0 );
576     dst_y = 4;
577
578     if( p_btnStop )
579         XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x, dst_y,
580                    p_btnStop->width, p_btnStop->height );
581
582     dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );
583     dst_y = 4;
584
585     if( p_btnFullscreen )
586         XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x, dst_y,
587                    p_btnFullscreen->width, p_btnFullscreen->height );
588
589     dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );
590     dst_y = 4;
591
592     if( p_btnUnmute && b_mute )
593     {
594         XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x, dst_y,
595                    p_btnUnmute->width, p_btnUnmute->height );
596
597         dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
598         dst_y = 4;
599     }
600     else if( p_btnMute )
601     {
602         XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x, dst_y,
603                    p_btnMute->width, p_btnMute->height );
604
605         dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
606         dst_y = 4;
607     }
608
609     if( p_timeline )
610         XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x, dst_y,
611                    (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
612
613     if( f_position > 0 )
614         i_last_position = (((float)window.width-8.0)/100.0)*f_position;
615
616     if( p_btnTime )
617         XPutImage( p_display, control, gc, p_btnTime,
618                    0, 0, (dst_x+i_last_position), dst_y,
619                    p_btnTime->width, p_btnTime->height );
620
621     XFreeGC( p_display, gc );
622 }
623 #endif