]> git.sesse.net Git - vlc/blob - projects/mozilla/vlcplugin.h
Mozilla plugin event listeners.
[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 #include "control/nporuntime.h"
36
37 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
38 #define XP_UNIX 1
39 #elif defined(XP_MACOSX)
40 #undef XP_UNIX
41 #endif
42
43 #ifdef XP_WIN
44     /* Windows stuff */
45 #endif
46
47 #ifdef XP_MACOSX
48     /* Mac OS X stuff */
49 #   include <Quickdraw.h>
50 #endif
51
52 #ifdef XP_UNIX
53     /* X11 stuff */
54 #   include <X11/Xlib.h>
55 #   include <X11/Intrinsic.h>
56 #   include <X11/StringDefs.h>
57 #   include <X11/X.h>
58
59 #   ifndef __APPLE__
60 #       include <X11/xpm.h>
61 #   endif
62 #endif
63
64 #ifndef __MAX
65 #   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
66 #endif
67 #ifndef __MIN
68 #   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
69 #endif
70
71 typedef enum vlc_toolbar_clicked_e {
72     clicked_Unknown = 0,
73     clicked_Play,
74     clicked_Pause,
75     clicked_Stop,
76     clicked_timeline,
77     clicked_Time,
78     clicked_Fullscreen,
79     clicked_Mute,
80     clicked_Unmute
81 } vlc_toolbar_clicked_t;
82
83
84 // Note that the accessor functions are unsafe, but this is handled in
85 // the next layer up. 64bit uints can be substituted to taste (shift=6).
86 template<size_t M> class bitmap
87 {
88 private:
89     typedef uint32_t bitu_t; enum { shift=5 };
90     enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
91     bitu_t bits[units];
92 public:
93     bool get(size_t idx) const
94     {
95         return bits[idx>>shift]&(1<<(idx&mask));
96     }
97
98     void set(size_t idx)       { bits[idx>>shift]|=  1<<(idx&mask);  }
99     void reset(size_t idx)     { bits[idx>>shift]&=~(1<<(idx&mask)); }
100     void toggle(size_t idx)    { bits[idx>>shift]^=  1<<(idx&mask);  }
101     size_t maxbit() const      { return bmax; }
102     void clear()               { memset(bits,0,sizeof(bits)); }
103     bitmap() { clear(); }
104     ~bitmap() { }
105 };
106
107 typedef bitmap<libvlc_num_event_types> parent;
108
109 class eventtypes_bitmap_t: private bitmap<libvlc_num_event_types> {
110 private:
111     typedef libvlc_event_type_t event_t;
112     event_t find_event(const char *s) const
113     {
114         event_t i;
115         for(i=0;i<maxbit();++i)
116             if(!strcmp(s,libvlc_event_type_name(i)))
117                 break;
118         return i;
119     }
120 public:
121     bool add_event(const eventtypes_bitmap_t &eventBitmap)
122     {
123         event_t i;
124         for(i=0;i<maxbit();++i)
125             if (eventBitmap.have_event(i))
126                 set(i);
127     }
128     bool add_event(const char *s)
129     {
130         if (!strcmp(s, "all"))
131         {
132             set_all_events();
133             return true;
134         }
135         if (!strcmp(s, "none"))
136         {
137             clear();
138             return true;
139         }
140
141         event_t event = find_event(s);
142         bool b = event<maxbit();
143         if(b) set(event);
144         return b;
145     }
146     bool del_event(const char *s)
147     {
148         event_t event=find_event(s);
149         bool b=event<maxbit();
150         if(b) reset(event);
151         return b;
152     }
153     bool have_event(libvlc_event_type_t event) const
154     {
155         return event<maxbit()?get(event):false;
156     }
157     void clear()
158     {
159         parent::clear();
160     }
161     void set_all_events()
162     {
163         event_t i;
164         for(i=0;i<maxbit();++i)
165             set(i);
166     }
167 };
168
169
170 // Structure used to represent an EventListener.
171 // It contains the listener object that will be invoked,
172 // An Id given by the addEventListener function and sent
173 // when invoking the listener. Can be anything or nothing.
174 // The profile associated with the listener used to invoke
175 // the listener only to some events.
176 typedef struct s_EventListener
177 {
178     NPObject *listener;
179     NPVariant id;
180     eventtypes_bitmap_t eventMap;
181     
182 } EventListener;
183
184 void event_callback(const libvlc_event_t* event, void *param);
185
186 class VlcPlugin
187 {
188 public:
189              VlcPlugin( NPP, uint16 );
190     virtual ~VlcPlugin();
191
192     NPError             init(int argc, char* const argn[], char* const argv[]);
193     libvlc_instance_t*  getVLC()
194                             { return libvlc_instance; };
195     libvlc_media_player_t* getMD(libvlc_exception_t *ex)
196     {
197         if( !libvlc_media_player )
198         {
199              libvlc_exception_raise(ex);
200              libvlc_printerr("no mediaplayer");
201         }
202         return libvlc_media_player;
203     }
204     NPP                 getBrowser()
205                             { return p_browser; };
206     char*               getAbsoluteURL(const char *url);
207     NPWindow&           getWindow()
208                             { return npwindow; };
209     void                setWindow(const NPWindow &window)
210                             { npwindow = window; };
211
212     NPClass*            getScriptClass()
213                             { return p_scriptClass; };
214
215 #if defined(XP_WIN)
216     WNDPROC             getWindowProc()
217                             { return pf_wndproc; };
218     void                setWindowProc(WNDPROC wndproc)
219                             { pf_wndproc = wndproc; };
220 #endif
221
222 #if defined(XP_UNIX)
223     int                 setSize(unsigned width, unsigned height);
224     Window              getVideoWindow()
225                             { return npvideo; };
226     void                setVideoWindow(Window window)
227                             { npvideo = window; };
228     Window              getControlWindow()
229                             { return npcontrol; };
230     void                setControlWindow(Window window)
231                             { npcontrol = window; };
232
233     void                showToolbar();
234     void                hideToolbar();
235     void                redrawToolbar();
236     void                getToolbarSize(unsigned int *width, unsigned int *height)
237                             { *width = i_tb_width; *height = i_tb_height; };
238     int                 setToolbarSize(unsigned int width, unsigned int height)
239                             { i_tb_width = width; i_tb_height = height; return 1; };
240     vlc_toolbar_clicked_t getToolbarButtonClicked( int i_xpos, int i_ypos );
241 #endif
242
243     uint16    i_npmode; /* either NP_EMBED or NP_FULL */
244
245     /* plugin properties */
246     int      b_stream;
247     int      b_autoplay;
248     int      b_toolbar;
249     char *   psz_text;
250     char *   psz_target;
251
252     void playlist_play(libvlc_exception_t *ex)
253     {
254         if( libvlc_media_player||playlist_select(0,ex) )
255             libvlc_media_player_play(libvlc_media_player,ex);
256     }
257     void playlist_play_item(int idx,libvlc_exception_t *ex)
258     {
259         if( playlist_select(idx,ex) )
260             libvlc_media_player_play(libvlc_media_player,ex);
261     }
262     void playlist_stop()
263     {
264         if( libvlc_media_player )
265             libvlc_media_player_stop(libvlc_media_player);
266     }
267     void playlist_next(libvlc_exception_t *ex)
268     {
269         if( playlist_select(playlist_index+1,ex) )
270             libvlc_media_player_play(libvlc_media_player,ex);
271     }
272     void playlist_prev(libvlc_exception_t *ex)
273     {
274         if( playlist_select(playlist_index-1,ex) )
275             libvlc_media_player_play(libvlc_media_player,ex);
276     }
277     void playlist_pause(libvlc_exception_t *ex)
278     {
279         if( libvlc_media_player )
280             libvlc_media_player_pause(libvlc_media_player,ex);
281     }
282     int playlist_isplaying()
283     {
284         int is_playing = 0;
285         if( libvlc_media_player )
286             is_playing = libvlc_media_player_is_playing(
287                                 libvlc_media_player );
288         return is_playing;
289     }
290
291     int playlist_add( const char *, libvlc_exception_t * );
292     int playlist_add_extended_untrusted( const char *, const char *, int,
293                                 const char **, libvlc_exception_t * );
294     void playlist_delete_item( int, libvlc_exception_t * );
295     void playlist_clear( libvlc_exception_t * );
296     int  playlist_count();
297
298     void toggle_fullscreen( libvlc_exception_t * );
299     void set_fullscreen( int, libvlc_exception_t * );
300     int  get_fullscreen( libvlc_exception_t * );
301
302     bool  player_has_vout( libvlc_exception_t * );
303
304
305     // Events related members
306     std::vector<EventListener*> eventListeners; // List of registered listerners.
307     std::vector<libvlc_event_type_t> eventList; // List of event sent by VLC that must be returned to JS.
308     eventtypes_bitmap_t eventToCatch;
309     pthread_mutex_t mutex;
310
311     static bool canUseEventListener();
312
313 private:
314     bool playlist_select(int,libvlc_exception_t *);
315     void set_player_window();
316
317     /* VLC reference */
318     int                 playlist_index;
319     libvlc_instance_t   *libvlc_instance;
320     libvlc_media_list_t *libvlc_media_list;
321     libvlc_media_player_t *libvlc_media_player;
322     NPClass             *p_scriptClass;
323
324     /* browser reference */
325     NPP     p_browser;
326     char*   psz_baseURL;
327
328     /* display settings */
329     NPWindow  npwindow;
330 #if defined(XP_WIN)
331     WNDPROC   pf_wndproc;
332 #endif
333 #if defined(XP_UNIX)
334     unsigned int     i_width, i_height;
335     unsigned int     i_tb_width, i_tb_height;
336     Window           npvideo, npcontrol;
337
338     XImage *p_btnPlay;
339     XImage *p_btnPause;
340     XImage *p_btnStop;
341     XImage *p_timeline;
342     XImage *p_btnTime;
343     XImage *p_btnFullscreen;
344     XImage *p_btnMute;
345     XImage *p_btnUnmute;
346
347     int i_last_position;
348 #endif
349 };
350
351 /*******************************************************************************
352  * Plugin properties.
353  ******************************************************************************/
354 #define PLUGIN_NAME         "VLC Multimedia Plug-in"
355 #define PLUGIN_DESCRIPTION \
356     "Version %s, copyright 1996-2007 The VideoLAN Team" \
357     "<br><a href=\"http://www.videolan.org/\">http://www.videolan.org/</a>"
358
359 #define PLUGIN_MIMETYPES \
360     /* MPEG-1 and MPEG-2 */ \
361     "audio/mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
362     "audio/x-mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
363     "video/mpeg:mpg,mpeg,mpe:MPEG video;" \
364     "video/x-mpeg:mpg,mpeg,mpe:MPEG video;" \
365     "video/mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
366     "video/x-mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
367     /* M3U */ \
368     "audio/x-mpegurl:m3u:MPEG audio;" \
369     /* MPEG-4 */ \
370     "video/mp4:mp4,mpg4:MPEG-4 video;" \
371     "audio/mp4:mp4,mpg4:MPEG-4 audio;" \
372     "audio/x-m4a:m4a:MPEG-4 audio;" \
373     "application/mpeg4-iod:mp4,mpg4:MPEG-4 video;" \
374     "application/mpeg4-muxcodetable:mp4,mpg4:MPEG-4 video;" \
375     /* AVI */ \
376     "video/x-msvideo:avi:AVI video;" \
377     /* QuickTime */ \
378     "video/quicktime:mov,qt:QuickTime video;" \
379     /* OGG */ \
380     "application/x-ogg:ogg:Ogg stream;" \
381     "application/ogg:ogg:Ogg stream;" \
382     /* VLC */ \
383     "application/x-vlc-plugin:vlc:VLC plug-in;" \
384     /* Windows Media */ \
385     "video/x-ms-asf-plugin:asf,asx:Windows Media Video;" \
386     "video/x-ms-asf:asf,asx:Windows Media Video;" \
387     "application/x-mplayer2::Windows Media;" \
388     "video/x-ms-wmv:wmv:Windows Media;" \
389     "video/x-ms-wvx:wvx:Windows Media Video;" \
390     "audio/x-ms-wma:wma:Windows Media Audio;" \
391     /* Google VLC */ \
392     "application/x-google-vlc-plugin::Google VLC plug-in;" \
393     /* WAV audio */ \
394     "audio/wav:wav:WAV audio;" \
395     "audio/x-wav:wav:WAV audio;" \
396     /* 3GPP */ \
397     "audio/3gpp:3gp,3gpp:3GPP audio;" \
398     "video/3gpp:3gp,3gpp:3GPP video;" \
399     /* 3GPP2 */ \
400     "audio/3gpp2:3g2,3gpp2:3GPP2 audio;" \
401     "video/3gpp2:3g2,3gpp2:3GPP2 video;" \
402     /* DIVX */ \
403     "video/divx:divx:DivX video;" \
404     /* FLV */ \
405     "video/flv:flv:FLV video;" \
406     "video/x-flv:flv:FLV video;" \
407     /* Matroska */ \
408     "video/x-matroska:mkv:Matroska video;" \
409     "audio/x-matroska:mka:Matroska audio;" \
410     /* XSPF */ \
411     "application/xspf+xml:xspf:Playlist xspf;"
412
413 #endif