mozilla: factorize.
[vlc/solaris.git] / projects / mozilla / vlcplugin.h
blobbc1c314a8f63fa421e2b0e79e4c24c1af21e58f3
1 /*****************************************************************************
2 * vlcplugin.h: a VLC plugin for Mozilla
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf@videolan.org>
9 * Jean-Paul Saman <jpsaman@videolan.org>
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.
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.
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 *****************************************************************************/
26 /*******************************************************************************
27 * Instance state information about the plugin.
28 ******************************************************************************/
29 #ifndef __VLCPLUGIN_H__
30 #define __VLCPLUGIN_H__
32 #include <vlc/vlc.h>
33 #include <pthread.h>
34 #include <npapi.h>
35 #include <vector>
37 #include "control/nporuntime.h"
39 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
40 #define XP_UNIX 1
41 #elif defined(XP_MACOSX)
42 #undef XP_UNIX
43 #endif
45 #ifdef XP_WIN
46 /* Windows stuff */
47 #endif
49 #ifdef XP_MACOSX
50 /* Mac OS X stuff */
51 # include <Quickdraw.h>
52 #endif
54 #ifdef XP_UNIX
55 /* X11 stuff */
56 # include <X11/Xlib.h>
57 # include <X11/Intrinsic.h>
58 # include <X11/StringDefs.h>
59 # include <X11/X.h>
61 # ifndef __APPLE__
62 # include <X11/xpm.h>
63 # endif
64 #endif
66 #ifndef __MAX
67 # define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
68 #endif
69 #ifndef __MIN
70 # define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
71 #endif
73 typedef enum vlc_toolbar_clicked_e {
74 clicked_Unknown = 0,
75 clicked_Play,
76 clicked_Pause,
77 clicked_Stop,
78 clicked_timeline,
79 clicked_Time,
80 clicked_Fullscreen,
81 clicked_Mute,
82 clicked_Unmute
83 } vlc_toolbar_clicked_t;
86 // Note that the accessor functions are unsafe, but this is handled in
87 // the next layer up. 64bit uints can be substituted to taste (shift=6).
88 template<size_t M> class bitmap
90 private:
91 typedef uint32_t bitu_t; enum { shift=5 };
92 enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
93 bitu_t bits[units];
94 public:
95 bool get(size_t idx) const { return bits[idx>>shift]&(1<<(idx&mask)); }
96 void set(size_t idx) { bits[idx>>shift]|= 1<<(idx&mask); }
97 void reset(size_t idx) { bits[idx>>shift]&=~(1<<(idx&mask)); }
98 void toggle(size_t idx) { bits[idx>>shift]^= 1<<(idx&mask); }
99 size_t maxbit() const { return bmax; }
100 void clear() { memset(bits,0,sizeof(bits)); }
101 bitmap() { clear(); }
102 ~bitmap() { }
103 bool empty() const { // naive invert() will break this
104 for(size_t i=0;i<units;++i)
105 if(bits[i]) return false;
106 return true;
110 typedef bitmap<libvlc_num_event_types> eventtypes_bitmap_t;
113 class EventObj: private eventtypes_bitmap_t
115 private:
116 typedef libvlc_event_type_t event_t;
117 bool have_event(event_t e) const { return e<maxbit()?get(e):false; }
119 class Listener: public eventtypes_bitmap_t
121 public:
122 Listener(event_t e,NPObject *o,bool b): _l(o), _b(b)
123 { NPN_RetainObject(o); set(e); }
124 Listener(): _l(NULL), _b(false) { }
125 ~Listener() { if(_l) NPN_ReleaseObject(_l); }
126 NPObject *listener() const { return _l; }
127 bool bubble() const { return _b; }
128 private:
129 NPObject *_l;
130 bool _b;
133 libvlc_event_manager_t *_em;
134 libvlc_callback_t _cb;
135 void *_ud;
136 public:
137 EventObj(): _em(NULL) { /* deferred to init() */ }
138 bool init() { return pthread_mutex_init(&mutex, NULL) == 0; }
139 ~EventObj() { pthread_mutex_destroy(&mutex); }
141 void deliver(NPP browser);
142 void callback(const libvlc_event_t*);
143 bool insert(const NPString &, NPObject *, bool);
144 bool remove(const NPString &, NPObject *, bool);
145 void unhook_manager();
146 void hook_manager(libvlc_event_manager_t *,libvlc_callback_t, void *);
147 private:
148 event_t find_event(const char *s) const;
149 typedef std::vector<Listener> lr_l;
150 typedef std::vector<libvlc_event_type_t> ev_l;
151 lr_l _llist;
152 ev_l _elist;
154 pthread_mutex_t mutex;
156 bool ask_for_event(event_t e);
157 void unask_for_event(event_t e);
161 class VlcPlugin
163 public:
164 VlcPlugin( NPP, uint16 );
165 virtual ~VlcPlugin();
167 NPError init(int argc, char* const argn[], char* const argv[]);
168 libvlc_instance_t* getVLC()
169 { return libvlc_instance; };
170 libvlc_media_player_t* getMD(libvlc_exception_t *ex)
172 if( !libvlc_media_player )
174 libvlc_exception_raise(ex);
175 libvlc_printerr("no mediaplayer");
177 return libvlc_media_player;
179 NPP getBrowser()
180 { return p_browser; };
181 char* getAbsoluteURL(const char *url);
182 NPWindow& getWindow()
183 { return npwindow; };
184 void setWindow(const NPWindow &window)
185 { npwindow = window; };
187 NPClass* getScriptClass()
188 { return p_scriptClass; };
190 #if defined(XP_WIN)
191 WNDPROC getWindowProc()
192 { return pf_wndproc; };
193 void setWindowProc(WNDPROC wndproc)
194 { pf_wndproc = wndproc; };
195 #endif
197 #if defined(XP_UNIX)
198 int setSize(unsigned width, unsigned height);
199 Window getVideoWindow()
200 { return npvideo; };
201 void setVideoWindow(Window window)
202 { npvideo = window; };
203 Window getControlWindow()
204 { return npcontrol; };
205 void setControlWindow(Window window)
206 { npcontrol = window; };
208 void showToolbar();
209 void hideToolbar();
210 void redrawToolbar();
211 void getToolbarSize(unsigned int *width, unsigned int *height)
212 { *width = i_tb_width; *height = i_tb_height; };
213 int setToolbarSize(unsigned int width, unsigned int height)
214 { i_tb_width = width; i_tb_height = height; return 1; };
215 vlc_toolbar_clicked_t getToolbarButtonClicked( int i_xpos, int i_ypos );
216 #endif
218 uint16 i_npmode; /* either NP_EMBED or NP_FULL */
220 /* plugin properties */
221 int b_stream;
222 int b_autoplay;
223 int b_toolbar;
224 char * psz_text;
225 char * psz_target;
227 void playlist_play(libvlc_exception_t *ex)
229 if( libvlc_media_player||playlist_select(0,ex) )
230 libvlc_media_player_play(libvlc_media_player);
232 void playlist_play_item(int idx,libvlc_exception_t *ex)
234 if( playlist_select(idx,ex) )
235 libvlc_media_player_play(libvlc_media_player);
237 void playlist_stop()
239 if( libvlc_media_player )
240 libvlc_media_player_stop(libvlc_media_player);
242 void playlist_next(libvlc_exception_t *ex)
244 if( playlist_select(playlist_index+1,ex) )
245 libvlc_media_player_play(libvlc_media_player);
247 void playlist_prev(libvlc_exception_t *ex)
249 if( playlist_select(playlist_index-1,ex) )
250 libvlc_media_player_play(libvlc_media_player);
252 void playlist_pause()
254 if( libvlc_media_player )
255 libvlc_media_player_pause(libvlc_media_player);
257 int playlist_isplaying()
259 int is_playing = 0;
260 if( libvlc_media_player )
261 is_playing = libvlc_media_player_is_playing(
262 libvlc_media_player );
263 return is_playing;
266 int playlist_add( const char *, libvlc_exception_t * );
267 int playlist_add_extended_untrusted( const char *, const char *, int,
268 const char **, libvlc_exception_t * );
269 void playlist_delete_item( int, libvlc_exception_t * );
270 void playlist_clear( libvlc_exception_t * );
271 int playlist_count();
273 void toggle_fullscreen( libvlc_exception_t * );
274 void set_fullscreen( int, libvlc_exception_t * );
275 int get_fullscreen( libvlc_exception_t * );
277 bool player_has_vout( libvlc_exception_t * );
280 static bool canUseEventListener();
282 EventObj events;
283 private:
284 bool playlist_select(int,libvlc_exception_t *);
285 void set_player_window();
287 /* VLC reference */
288 int playlist_index;
289 libvlc_instance_t *libvlc_instance;
290 libvlc_media_list_t *libvlc_media_list;
291 libvlc_media_player_t *libvlc_media_player;
292 NPClass *p_scriptClass;
294 /* browser reference */
295 NPP p_browser;
296 char* psz_baseURL;
298 /* display settings */
299 NPWindow npwindow;
300 #if defined(XP_WIN)
301 WNDPROC pf_wndproc;
302 #endif
303 #if defined(XP_UNIX)
304 unsigned int i_width, i_height;
305 unsigned int i_tb_width, i_tb_height;
306 Window npvideo, npcontrol;
308 XImage *p_btnPlay;
309 XImage *p_btnPause;
310 XImage *p_btnStop;
311 XImage *p_timeline;
312 XImage *p_btnTime;
313 XImage *p_btnFullscreen;
314 XImage *p_btnMute;
315 XImage *p_btnUnmute;
317 int i_last_position;
318 #endif
320 static void eventAsync(void *);
321 static void event_callback(const libvlc_event_t *, void *);
324 /*******************************************************************************
325 * Plugin properties.
326 ******************************************************************************/
327 #define PLUGIN_NAME "VLC Multimedia Plug-in"
328 #define PLUGIN_DESCRIPTION \
329 "Version %s, copyright 1996-2007 The VideoLAN Team" \
330 "<br><a href=\"http://www.videolan.org/\">http://www.videolan.org/</a>"
332 #define PLUGIN_MIMETYPES \
333 /* MPEG-1 and MPEG-2 */ \
334 "audio/mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
335 "audio/x-mpeg:mp2,mp3,mpga,mpega:MPEG audio;" \
336 "video/mpeg:mpg,mpeg,mpe:MPEG video;" \
337 "video/x-mpeg:mpg,mpeg,mpe:MPEG video;" \
338 "video/mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
339 "video/x-mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
340 /* M3U */ \
341 "audio/x-mpegurl:m3u:MPEG audio;" \
342 /* MPEG-4 */ \
343 "video/mp4:mp4,mpg4:MPEG-4 video;" \
344 "audio/mp4:mp4,mpg4:MPEG-4 audio;" \
345 "audio/x-m4a:m4a:MPEG-4 audio;" \
346 "application/mpeg4-iod:mp4,mpg4:MPEG-4 video;" \
347 "application/mpeg4-muxcodetable:mp4,mpg4:MPEG-4 video;" \
348 /* AVI */ \
349 "video/x-msvideo:avi:AVI video;" \
350 /* QuickTime */ \
351 "video/quicktime:mov,qt:QuickTime video;" \
352 /* OGG */ \
353 "application/x-ogg:ogg:Ogg stream;" \
354 "application/ogg:ogg:Ogg stream;" \
355 /* VLC */ \
356 "application/x-vlc-plugin:vlc:VLC plug-in;" \
357 /* Windows Media */ \
358 "video/x-ms-asf-plugin:asf,asx:Windows Media Video;" \
359 "video/x-ms-asf:asf,asx:Windows Media Video;" \
360 "application/x-mplayer2::Windows Media;" \
361 "video/x-ms-wmv:wmv:Windows Media;" \
362 "video/x-ms-wvx:wvx:Windows Media Video;" \
363 "audio/x-ms-wma:wma:Windows Media Audio;" \
364 /* Google VLC */ \
365 "application/x-google-vlc-plugin::Google VLC plug-in;" \
366 /* WAV audio */ \
367 "audio/wav:wav:WAV audio;" \
368 "audio/x-wav:wav:WAV audio;" \
369 /* 3GPP */ \
370 "audio/3gpp:3gp,3gpp:3GPP audio;" \
371 "video/3gpp:3gp,3gpp:3GPP video;" \
372 /* 3GPP2 */ \
373 "audio/3gpp2:3g2,3gpp2:3GPP2 audio;" \
374 "video/3gpp2:3g2,3gpp2:3GPP2 video;" \
375 /* DIVX */ \
376 "video/divx:divx:DivX video;" \
377 /* FLV */ \
378 "video/flv:flv:FLV video;" \
379 "video/x-flv:flv:FLV video;" \
380 /* Matroska */ \
381 "video/x-matroska:mkv:Matroska video;" \
382 "audio/x-matroska:mka:Matroska audio;" \
383 /* XSPF */ \
384 "application/xspf+xml:xspf:Playlist xspf;"
386 #endif