]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.h
Mozilla plugin: try to fix some errors with XulRunner 1.9.2
[vlc] / projects / mozilla / vlcplugin.h
1 /*****************************************************************************
2  * vlcplugin.h: a VLC plugin for Mozilla
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *          Damien Fouilleul <damienf@videolan.org>
9  *          Jean-Paul Saman <jpsaman@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*******************************************************************************
27  * Instance state information about the plugin.
28  ******************************************************************************/
29 #ifndef __VLCPLUGIN_H__
30 #define __VLCPLUGIN_H__
31
32 #include <vlc/vlc.h>
33 #include <npapi.h>
34 #include <vector>
35
36 #include "control/nporuntime.h"
37
38 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
39 #define XP_UNIX 1
40 #elif defined(XP_MACOSX)
41 #undef XP_UNIX
42 #endif
43
44 #ifdef XP_WIN
45     /* Windows stuff */
46 #   include <winbase.h>
47 #   include <windows.h>
48 #endif
49
50 #ifdef XP_MACOSX
51     /* Mac OS X stuff */
52 #   include <Quickdraw.h>
53 #endif
54
55 #ifdef XP_UNIX
56 #   include <pthread.h>
57     /* X11 stuff */
58 #   include <X11/Xlib.h>
59 #   include <X11/Intrinsic.h>
60 #   include <X11/StringDefs.h>
61 #   include <X11/X.h>
62
63 #   ifndef __APPLE__
64 #       include <X11/xpm.h>
65 #   endif
66 #endif
67
68 #ifndef __MAX
69 #   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
70 #endif
71 #ifndef __MIN
72 #   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
73 #endif
74
75 typedef struct {
76 #if defined(XP_UNIX)
77     pthread_mutex_t mutex;
78 #elif defined(XP_WIN)
79     CRITICAL_SECTION cs;
80 #else
81 #warning "locking not implemented in this platform"
82 #endif
83 } plugin_lock_t;
84
85
86 typedef enum vlc_toolbar_clicked_e {
87     clicked_Unknown = 0,
88     clicked_Play,
89     clicked_Pause,
90     clicked_Stop,
91     clicked_timeline,
92     clicked_Time,
93     clicked_Fullscreen,
94     clicked_Mute,
95     clicked_Unmute
96 } vlc_toolbar_clicked_t;
97
98
99 // Note that the accessor functions are unsafe, but this is handled in
100 // the next layer up. 64bit uints can be substituted to taste (shift=6).
101 template<size_t M> class bitmap
102 {
103 private:
104     typedef uint32_t bitu_t; enum { shift=5 };
105     enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
106     bitu_t bits[units];
107 public:
108     bool get(size_t idx) const { return bits[idx>>shift]&(1<<(idx&mask)); }
109     void set(size_t idx)       { bits[idx>>shift]|=  1<<(idx&mask);  }
110     void reset(size_t idx)     { bits[idx>>shift]&=~(1<<(idx&mask)); }
111     void toggle(size_t idx)    { bits[idx>>shift]^=  1<<(idx&mask);  }
112     size_t maxbit() const      { return bmax; }
113     void clear()               { memset(bits,0,sizeof(bits)); }
114     bitmap() { clear(); }
115     ~bitmap() { }
116     bool empty() const { // naive invert() will break this
117         for(size_t i=0;i<units;++i)
118             if(bits[i]) return false;
119         return true;
120     }
121 };
122
123 typedef bitmap<libvlc_VlmMediaInstanceStatusError+1> eventtypes_bitmap_t;
124
125
126 class EventObj: private eventtypes_bitmap_t
127 {
128 private:
129     typedef libvlc_event_type_t event_t;
130     bool have_event(event_t e) const { return e<maxbit()?get(e):false; }
131
132     class Listener: public eventtypes_bitmap_t
133     {
134     public:
135         Listener(event_t e,NPObject *o,bool b): _l(o), _b(b)
136             { NPN_RetainObject(o); set(e); }
137         Listener(): _l(NULL), _b(false) { }
138         ~Listener() { if(_l) NPN_ReleaseObject(_l); }
139         NPObject *listener() const { return _l; }
140         bool bubble() const { return _b; }
141     private:
142         NPObject *_l;
143         bool _b;
144     };
145
146     libvlc_event_manager_t *_em;
147     libvlc_callback_t _cb;
148     void *_ud;
149 public:
150     EventObj(): _em(NULL)  { /* deferred to init() */ }
151     bool init();
152     ~EventObj();
153
154     void deliver(NPP browser);
155     void callback(const libvlc_event_t*);
156     bool insert(const NPString &, NPObject *, bool);
157     bool remove(const NPString &, NPObject *, bool);
158     void unhook_manager();
159     void hook_manager(libvlc_event_manager_t *,libvlc_callback_t, void *);
160 private:
161     event_t find_event(const char *s) const;
162     typedef std::vector<Listener> lr_l;
163     typedef std::vector<libvlc_event_type_t> ev_l;
164     lr_l _llist;
165     ev_l _elist;
166
167     plugin_lock_t lock;
168
169     bool ask_for_event(event_t e);
170     void unask_for_event(event_t e);
171 };
172
173
174 class VlcPlugin
175 {
176 public:
177 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
178              VlcPlugin( NPP, uint16 );
179 #else
180              VlcPlugin( NPP, uint16_t );
181 #endif
182     virtual ~VlcPlugin();
183
184     NPError             init(int argc, char* const argn[], char* const argv[]);
185     libvlc_instance_t*  getVLC()
186                             { return libvlc_instance; };
187     libvlc_media_player_t* getMD()
188     {
189         if( !libvlc_media_player )
190         {
191              libvlc_printerr("no mediaplayer");
192         }
193         return libvlc_media_player;
194     }
195     NPP                 getBrowser()
196                             { return p_browser; };
197     char*               getAbsoluteURL(const char *url);
198     NPWindow&           getWindow()
199                             { return npwindow; };
200     void                setWindow(const NPWindow &window)
201                             { npwindow = window; };
202
203     NPClass*            getScriptClass()
204                             { return p_scriptClass; };
205
206 #if defined(XP_WIN)
207     WNDPROC             getWindowProc()
208                             { return pf_wndproc; };
209     void                setWindowProc(WNDPROC wndproc)
210                             { pf_wndproc = wndproc; };
211 #endif
212
213 #if defined(XP_UNIX)
214     int                 setSize(unsigned width, unsigned height);
215     Window              getVideoWindow()
216                             { return npvideo; };
217     void                setVideoWindow(Window window)
218                             { npvideo = window; };
219     Window              getControlWindow()
220                             { return npcontrol; };
221     void                setControlWindow(Window window)
222                             { npcontrol = window; };
223
224     void                showToolbar();
225     void                hideToolbar();
226     void                redrawToolbar();
227     void                getToolbarSize(unsigned int *width, unsigned int *height)
228                             { *width = i_tb_width; *height = i_tb_height; };
229     int                 setToolbarSize(unsigned int width, unsigned int height)
230                             { i_tb_width = width; i_tb_height = height; return 1; };
231     vlc_toolbar_clicked_t getToolbarButtonClicked( int i_xpos, int i_ypos );
232 #endif
233
234 #if (((NP_VERSION_MAJOR << 8) + NP_VERSION_MINOR) < 20)
235     uint16    i_npmode; /* either NP_EMBED or NP_FULL */
236 #else
237     uint16_t  i_npmode; /* either NP_EMBED or NP_FULL */
238 #endif
239
240     /* plugin properties */
241     int      b_stream;
242     int      b_autoplay;
243     int      b_toolbar;
244     char *   psz_text;
245     char *   psz_target;
246
247     void playlist_play()
248     {
249         if( playlist_isplaying() )
250             playlist_stop();
251         if( libvlc_media_player||playlist_select(0) )
252             libvlc_media_player_play(libvlc_media_player);
253     }
254     void playlist_play_item(int idx)
255     {
256         if( playlist_select(idx) )
257             libvlc_media_player_play(libvlc_media_player);
258     }
259     void playlist_stop()
260     {
261         if( libvlc_media_player )
262             libvlc_media_player_stop(libvlc_media_player);
263     }
264     void playlist_next()
265     {
266         if( playlist_select(playlist_index+1) )
267             libvlc_media_player_play(libvlc_media_player);
268     }
269     void playlist_prev()
270     {
271         if( playlist_select(playlist_index-1) )
272             libvlc_media_player_play(libvlc_media_player);
273     }
274     void playlist_pause()
275     {
276         if( libvlc_media_player )
277             libvlc_media_player_pause(libvlc_media_player);
278     }
279     int playlist_isplaying()
280     {
281         int is_playing = 0;
282         if( libvlc_media_player )
283             is_playing = libvlc_media_player_is_playing(
284                                 libvlc_media_player );
285         return is_playing;
286     }
287
288     int playlist_add( const char * );
289     int playlist_add_extended_untrusted( const char *, const char *, int,
290                                 const char ** );
291     int playlist_delete_item( int );
292     void playlist_clear();
293     int  playlist_count();
294
295     void toggle_fullscreen();
296     void set_fullscreen( int );
297     int  get_fullscreen();
298
299     bool  player_has_vout();
300
301
302     static bool canUseEventListener();
303
304     EventObj events;
305 private:
306     bool playlist_select(int);
307     void set_player_window();
308
309     /* VLC reference */
310     int                 playlist_index;
311     libvlc_instance_t   *libvlc_instance;
312     libvlc_media_list_t *libvlc_media_list;
313     libvlc_media_player_t *libvlc_media_player;
314     NPClass             *p_scriptClass;
315
316     /* browser reference */
317     NPP     p_browser;
318     char*   psz_baseURL;
319
320     /* display settings */
321     NPWindow  npwindow;
322 #if defined(XP_WIN)
323     WNDPROC   pf_wndproc;
324 #endif
325 #if defined(XP_UNIX)
326     unsigned int     i_width, i_height;
327     unsigned int     i_tb_width, i_tb_height;
328     Window           npvideo, npcontrol;
329
330     XImage *p_btnPlay;
331     XImage *p_btnPause;
332     XImage *p_btnStop;
333     XImage *p_timeline;
334     XImage *p_btnTime;
335     XImage *p_btnFullscreen;
336     XImage *p_btnMute;
337     XImage *p_btnUnmute;
338
339     int i_last_position;
340 #endif
341
342     static void eventAsync(void *);
343     static void event_callback(const libvlc_event_t *, void *);
344 };
345
346 /*******************************************************************************
347  * Plugin properties.
348  ******************************************************************************/
349 #define PLUGIN_NAME         "VLC Multimedia Plug-in"
350 #define PLUGIN_DESCRIPTION \
351     "Version %s, copyright 1996-2007 The VideoLAN Team" \
352     "<br><a href=\"http://www.videolan.org/\">http://www.videolan.org/</a>"
353
354 #define PLUGIN_MIMETYPES \
355     /* MPEG-1 and MPEG-2 */ \
356     "audio/mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
357     "audio/x-mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
358     "video/mpeg:mpg,mpeg,mpe:MPEG video;" \
359     "video/x-mpeg:mpg,mpeg,mpe:MPEG video;" \
360     "video/mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
361     "video/x-mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
362     /* M3U */ \
363     "audio/x-mpegurl:m3u:MPEG audio;" \
364     /* MPEG-4 */ \
365     "video/mp4:mp4,mpg4:MPEG-4 video;" \
366     "audio/mp4:mp4,mpg4:MPEG-4 audio;" \
367     "audio/x-m4a:m4a:MPEG-4 audio;" \
368     "application/mpeg4-iod:mp4,mpg4:MPEG-4 video;" \
369     "application/mpeg4-muxcodetable:mp4,mpg4:MPEG-4 video;" \
370     /* AVI */ \
371     "video/x-msvideo:avi:AVI video;" \
372     /* QuickTime */ \
373     "video/quicktime:mov,qt:QuickTime video;" \
374     /* OGG */ \
375     "application/x-ogg:ogg:Ogg stream;" \
376     "application/ogg:ogg:Ogg stream;" \
377     /* VLC */ \
378     "application/x-vlc-plugin:vlc:VLC plug-in;" \
379     /* Windows Media */ \
380     "video/x-ms-asf-plugin:asf,asx:Windows Media Video;" \
381     "video/x-ms-asf:asf,asx:Windows Media Video;" \
382     "application/x-mplayer2::Windows Media;" \
383     "video/x-ms-wmv:wmv:Windows Media;" \
384     "video/x-ms-wvx:wvx:Windows Media Video;" \
385     "audio/x-ms-wma:wma:Windows Media Audio;" \
386     /* Google VLC */ \
387     "application/x-google-vlc-plugin::Google VLC plug-in;" \
388     /* WAV audio */ \
389     "audio/wav:wav:WAV audio;" \
390     "audio/x-wav:wav:WAV audio;" \
391     /* 3GPP */ \
392     "audio/3gpp:3gp,3gpp:3GPP audio;" \
393     "video/3gpp:3gp,3gpp:3GPP video;" \
394     /* 3GPP2 */ \
395     "audio/3gpp2:3g2,3gpp2:3GPP2 audio;" \
396     "video/3gpp2:3g2,3gpp2:3GPP2 video;" \
397     /* DIVX */ \
398     "video/divx:divx:DivX video;" \
399     /* FLV */ \
400     "video/flv:flv:FLV video;" \
401     "video/x-flv:flv:FLV video;" \
402     /* Matroska */ \
403     "video/x-matroska:mkv:Matroska video;" \
404     "audio/x-matroska:mka:Matroska audio;" \
405     /* XSPF */ \
406     "application/xspf+xml:xspf:Playlist xspf;"
407
408 #endif