]> git.sesse.net Git - vlc/blobdiff - projects/mozilla/vlcplugin.h
Mozilla plugin event listeners.
[vlc] / projects / mozilla / vlcplugin.h
index 06116750edc6bf0a72b93b03e15702979cdd4c46..7cc0519641464e530bdd93c797c14b54a88a6dc5 100644 (file)
@@ -1,11 +1,12 @@
 /*****************************************************************************
  * vlcplugin.h: a VLC plugin for Mozilla
  *****************************************************************************
- * Copyright (C) 2002-2006 the VideoLAN team
+ * Copyright (C) 2002-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.org>
-            Damien Fouilleul <damienf@videolan.org>
+ *          Damien Fouilleul <damienf@videolan.org>
+ *          Jean-Paul Saman <jpsaman@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -28,8 +29,9 @@
 #ifndef __VLCPLUGIN_H__
 #define __VLCPLUGIN_H__
 
-#include <vlc/libvlc.h>
+#include <vlc/vlc.h>
 #include <npapi.h>
+#include <vector>
 #include "control/nporuntime.h"
 
 #if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
 #   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
 #endif
 
+typedef enum vlc_toolbar_clicked_e {
+    clicked_Unknown = 0,
+    clicked_Play,
+    clicked_Pause,
+    clicked_Stop,
+    clicked_timeline,
+    clicked_Time,
+    clicked_Fullscreen,
+    clicked_Mute,
+    clicked_Unmute
+} vlc_toolbar_clicked_t;
+
+
+// Note that the accessor functions are unsafe, but this is handled in
+// the next layer up. 64bit uints can be substituted to taste (shift=6).
+template<size_t M> class bitmap
+{
+private:
+    typedef uint32_t bitu_t; enum { shift=5 };
+    enum { bmax=M, bpu=1<<shift, mask=bpu-1, units=(bmax+bpu-1)/bpu };
+    bitu_t bits[units];
+public:
+    bool get(size_t idx) const
+    {
+        return bits[idx>>shift]&(1<<(idx&mask));
+    }
+
+    void set(size_t idx)       { bits[idx>>shift]|=  1<<(idx&mask);  }
+    void reset(size_t idx)     { bits[idx>>shift]&=~(1<<(idx&mask)); }
+    void toggle(size_t idx)    { bits[idx>>shift]^=  1<<(idx&mask);  }
+    size_t maxbit() const      { return bmax; }
+    void clear()               { memset(bits,0,sizeof(bits)); }
+    bitmap() { clear(); }
+    ~bitmap() { }
+};
+
+typedef bitmap<libvlc_num_event_types> parent;
+
+class eventtypes_bitmap_t: private bitmap<libvlc_num_event_types> {
+private:
+    typedef libvlc_event_type_t event_t;
+    event_t find_event(const char *s) const
+    {
+        event_t i;
+        for(i=0;i<maxbit();++i)
+            if(!strcmp(s,libvlc_event_type_name(i)))
+                break;
+        return i;
+    }
+public:
+    bool add_event(const eventtypes_bitmap_t &eventBitmap)
+    {
+        event_t i;
+        for(i=0;i<maxbit();++i)
+            if (eventBitmap.have_event(i))
+                set(i);
+    }
+    bool add_event(const char *s)
+    {
+        if (!strcmp(s, "all"))
+        {
+            set_all_events();
+            return true;
+        }
+        if (!strcmp(s, "none"))
+        {
+            clear();
+            return true;
+        }
+
+        event_t event = find_event(s);
+        bool b = event<maxbit();
+        if(b) set(event);
+        return b;
+    }
+    bool del_event(const char *s)
+    {
+        event_t event=find_event(s);
+        bool b=event<maxbit();
+        if(b) reset(event);
+        return b;
+    }
+    bool have_event(libvlc_event_type_t event) const
+    {
+        return event<maxbit()?get(event):false;
+    }
+    void clear()
+    {
+        parent::clear();
+    }
+    void set_all_events()
+    {
+        event_t i;
+        for(i=0;i<maxbit();++i)
+            set(i);
+    }
+};
+
+
+// Structure used to represent an EventListener.
+// It contains the listener object that will be invoked,
+// An Id given by the addEventListener function and sent
+// when invoking the listener. Can be anything or nothing.
+// The profile associated with the listener used to invoke
+// the listener only to some events.
+typedef struct s_EventListener
+{
+    NPObject *listener;
+    NPVariant id;
+    eventtypes_bitmap_t eventMap;
+    
+} EventListener;
+
+void event_callback(const libvlc_event_t* event, void *param);
+
 class VlcPlugin
 {
 public:
@@ -75,6 +192,15 @@ public:
     NPError             init(int argc, char* const argn[], char* const argv[]);
     libvlc_instance_t*  getVLC()
                             { return libvlc_instance; };
+    libvlc_media_player_t* getMD(libvlc_exception_t *ex)
+    {
+        if( !libvlc_media_player )
+        {
+             libvlc_exception_raise(ex);
+             libvlc_printerr("no mediaplayer");
+        }
+        return libvlc_media_player;
+    }
     NPP                 getBrowser()
                             { return p_browser; };
     char*               getAbsoluteURL(const char *url);
@@ -86,18 +212,14 @@ public:
     NPClass*            getScriptClass()
                             { return p_scriptClass; };
 
-    void                setLog(libvlc_log_t *log)
-                            { libvlc_log = log; };
-    libvlc_log_t*       getLog()
-                            { return libvlc_log; };
-#if XP_WIN
+#if defined(XP_WIN)
     WNDPROC             getWindowProc()
                             { return pf_wndproc; };
     void                setWindowProc(WNDPROC wndproc)
                             { pf_wndproc = wndproc; };
 #endif
 
-#if XP_UNIX
+#if defined(XP_UNIX)
     int                 setSize(unsigned width, unsigned height);
     Window              getVideoWindow()
                             { return npvideo; };
@@ -111,6 +233,11 @@ public:
     void                showToolbar();
     void                hideToolbar();
     void                redrawToolbar();
+    void                getToolbarSize(unsigned int *width, unsigned int *height)
+                            { *width = i_tb_width; *height = i_tb_height; };
+    int                 setToolbarSize(unsigned int width, unsigned int height)
+                            { i_tb_width = width; i_tb_height = height; return 1; };
+    vlc_toolbar_clicked_t getToolbarButtonClicked( int i_xpos, int i_ypos );
 #endif
 
     uint16    i_npmode; /* either NP_EMBED or NP_FULL */
@@ -118,17 +245,80 @@ public:
     /* plugin properties */
     int      b_stream;
     int      b_autoplay;
-    int      b_show_toolbar;
+    int      b_toolbar;
+    char *   psz_text;
     char *   psz_target;
 
-#if XP_UNIX
-    /* toolbar */
-    int     i_control_height;
-#endif
+    void playlist_play(libvlc_exception_t *ex)
+    {
+        if( libvlc_media_player||playlist_select(0,ex) )
+            libvlc_media_player_play(libvlc_media_player,ex);
+    }
+    void playlist_play_item(int idx,libvlc_exception_t *ex)
+    {
+        if( playlist_select(idx,ex) )
+            libvlc_media_player_play(libvlc_media_player,ex);
+    }
+    void playlist_stop()
+    {
+        if( libvlc_media_player )
+            libvlc_media_player_stop(libvlc_media_player);
+    }
+    void playlist_next(libvlc_exception_t *ex)
+    {
+        if( playlist_select(playlist_index+1,ex) )
+            libvlc_media_player_play(libvlc_media_player,ex);
+    }
+    void playlist_prev(libvlc_exception_t *ex)
+    {
+        if( playlist_select(playlist_index-1,ex) )
+            libvlc_media_player_play(libvlc_media_player,ex);
+    }
+    void playlist_pause(libvlc_exception_t *ex)
+    {
+        if( libvlc_media_player )
+            libvlc_media_player_pause(libvlc_media_player,ex);
+    }
+    int playlist_isplaying()
+    {
+        int is_playing = 0;
+        if( libvlc_media_player )
+            is_playing = libvlc_media_player_is_playing(
+                                libvlc_media_player );
+        return is_playing;
+    }
+
+    int playlist_add( const char *, libvlc_exception_t * );
+    int playlist_add_extended_untrusted( const char *, const char *, int,
+                                const char **, libvlc_exception_t * );
+    void playlist_delete_item( int, libvlc_exception_t * );
+    void playlist_clear( libvlc_exception_t * );
+    int  playlist_count();
+
+    void toggle_fullscreen( libvlc_exception_t * );
+    void set_fullscreen( int, libvlc_exception_t * );
+    int  get_fullscreen( libvlc_exception_t * );
+
+    bool  player_has_vout( libvlc_exception_t * );
+
+
+    // Events related members
+    std::vector<EventListener*> eventListeners; // List of registered listerners.
+    std::vector<libvlc_event_type_t> eventList; // List of event sent by VLC that must be returned to JS.
+    eventtypes_bitmap_t eventToCatch;
+    pthread_mutex_t mutex;
+
+    static bool canUseEventListener();
+
 private:
+    bool playlist_select(int,libvlc_exception_t *);
+    void set_player_window();
+
     /* VLC reference */
+    int                 playlist_index;
     libvlc_instance_t   *libvlc_instance;
-    libvlc_log_t        *libvlc_log;
+    libvlc_media_list_t *libvlc_media_list;
+    libvlc_media_player_t *libvlc_media_player;
     NPClass             *p_scriptClass;
 
     /* browser reference */
@@ -137,11 +327,12 @@ private:
 
     /* display settings */
     NPWindow  npwindow;
-#if XP_WIN
+#if defined(XP_WIN)
     WNDPROC   pf_wndproc;
 #endif
-#if XP_UNIX
+#if defined(XP_UNIX)
     unsigned int     i_width, i_height;
+    unsigned int     i_tb_width, i_tb_height;
     Window           npvideo, npcontrol;
 
     XImage *p_btnPlay;
@@ -173,9 +364,12 @@ private:
     "video/x-mpeg:mpg,mpeg,mpe:MPEG video;" \
     "video/mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
     "video/x-mpeg-system:mpg,mpeg,mpe,vob:MPEG video;" \
+    /* M3U */ \
+    "audio/x-mpegurl:m3u:MPEG audio;" \
     /* MPEG-4 */ \
-    "video/mpeg4:mp4,mpg4:MPEG-4 video;" \
-    "audio/mpeg4:mp4,mpg4:MPEG-4 audio;" \
+    "video/mp4:mp4,mpg4:MPEG-4 video;" \
+    "audio/mp4:mp4,mpg4:MPEG-4 audio;" \
+    "audio/x-m4a:m4a:MPEG-4 audio;" \
     "application/mpeg4-iod:mp4,mpg4:MPEG-4 video;" \
     "application/mpeg4-muxcodetable:mp4,mpg4:MPEG-4 video;" \
     /* AVI */ \
@@ -193,6 +387,7 @@ private:
     "application/x-mplayer2::Windows Media;" \
     "video/x-ms-wmv:wmv:Windows Media;" \
     "video/x-ms-wvx:wvx:Windows Media Video;" \
+    "audio/x-ms-wma:wma:Windows Media Audio;" \
     /* Google VLC */ \
     "application/x-google-vlc-plugin::Google VLC plug-in;" \
     /* WAV audio */ \
@@ -208,8 +403,11 @@ private:
     "video/divx:divx:DivX video;" \
     /* FLV */ \
     "video/flv:flv:FLV video;" \
-    "video/x-flv:flv:FLV video;"
-
-
+    "video/x-flv:flv:FLV video;" \
+    /* Matroska */ \
+    "video/x-matroska:mkv:Matroska video;" \
+    "audio/x-matroska:mka:Matroska audio;" \
+    /* XSPF */ \
+    "application/xspf+xml:xspf:Playlist xspf;"
 
 #endif