X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=projects%2Fmozilla%2Fvlcplugin.cpp;h=bb5849ce2ddb6e70ef22f2c0c4e992d116192482;hb=396acde38ecc51964cc91df0776a2eae2905f643;hp=3a49e8e4c7f6e04aa98465ccf09b58ff874ad82f;hpb=50b952d9421372f1a1163f2accc38d4719e57afd;p=vlc diff --git a/projects/mozilla/vlcplugin.cpp b/projects/mozilla/vlcplugin.cpp index 3a49e8e4c7..bb5849ce2d 100644 --- a/projects/mozilla/vlcplugin.cpp +++ b/projects/mozilla/vlcplugin.cpp @@ -36,6 +36,8 @@ #include "control/npolibvlc.h" #include +#include +#include /***************************************************************************** * VlcPlugin constructor and destructor @@ -54,10 +56,10 @@ VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) : p_scriptClass(NULL), p_browser(instance), psz_baseURL(NULL) -#if XP_WIN +#if defined(XP_WIN) ,pf_wndproc(NULL) #endif -#if XP_UNIX +#if defined(XP_UNIX) ,i_width((unsigned)-1) ,i_height((unsigned)-1) ,i_tb_width(0) @@ -74,7 +76,7 @@ VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) : #endif { memset(&npwindow, 0, sizeof(NPWindow)); -#if XP_UNIX +#if defined(XP_UNIX) memset(&npvideo, 0, sizeof(Window)); memset(&npcontrol, 0, sizeof(Window)); #endif @@ -86,6 +88,160 @@ static bool boolValue(const char *value) { !strcasecmp(value, "yes") ); } + +void EventObj::deliver(NPP browser) +{ + NPVariant result; + NPVariant params[1]; + + pthread_mutex_lock(&mutex); + + for( ev_l::iterator i=_elist.begin();i!=_elist.end();++i ) + { + libvlc_event_type_t event = *i; + STRINGZ_TO_NPVARIANT(libvlc_event_type_name(event), params[0]); + + // Invalid events aren't supposed to be queued up. + // if( !have_event(event) ) continue; + + for( lr_l::iterator j=_llist.begin();j!=_llist.end();++j ) + { + if (j->get(event)) + { + NPN_InvokeDefault(browser, j->listener(), params, 1, &result); + NPN_ReleaseVariantValue(&result); + } + } + } + _elist.clear(); + + pthread_mutex_unlock(&mutex); +} + +void VlcPlugin::eventAsync(void *param) +{ + VlcPlugin *plugin = (VlcPlugin*)param; + plugin->events.deliver(plugin->getBrowser()); +} + +void EventObj::callback(const libvlc_event_t* event) +{ + pthread_mutex_lock(&mutex); + + if( have_event(event->type) ) + _elist.push_back(event->type); + + pthread_mutex_unlock(&mutex); +} + +void VlcPlugin::event_callback(const libvlc_event_t* event, void *param) +{ + VlcPlugin *plugin = (VlcPlugin*)param; +#ifdef XP_UNIX + plugin->events.callback(event); + NPN_PluginThreadAsyncCall(plugin->getBrowser(), eventAsync, plugin); +#else +#warning NPN_PluginThreadAsyncCall not implemented yet. + printf("%s","No NPN_PluginThreadAsyncCall(), doing nothing."); +#endif +} + +inline EventObj::event_t EventObj::find_event(const char *s) const +{ + event_t i; + for(i=0;i=maxbit() ) + return false; + + if( !have_event(e) && !ask_for_event(e) ) + return false; + + lr_l::iterator i; + for(i=_llist.begin();i!=_llist.end();++i) + if(i->listener()==l && i->bubble()==b) + break; + + if( i == _llist.end() ) { + _llist.push_back(Listener(e,l,b)); + } else { + if( i->get(e) ) + return false; + i->get(e); + } + return true; +} + + +bool EventObj::remove(const NPString &s, NPObject *l, bool b) +{ + event_t e = find_event(s.utf8characters); + if( e>=maxbit() || !get(e) ) + return false; + + bool any=false; + for(lr_l::iterator i=_llist.begin();i!=_llist.end();) + { + if(i->listener()!=l || i->bubble()!=b) + any|=i->get(e); + else + { + i->reset(e); + if(i->empty()) + { + i=_llist.erase(i); + continue; + } + } + ++i; + } + if(!any) + unask_for_event(e); + + return true; +} + + +void EventObj::hook_manager(libvlc_event_manager_t *em, + libvlc_callback_t cb, void *udata) +{ + _em = em; _cb = cb; _ud = udata; + if( !_em ) + return; + for(size_t i=0;i(malloc(location.utf8length+1)); + psz_baseURL = (char *) malloc(location.utf8length+1); if( psz_baseURL ) { strncpy(psz_baseURL, location.utf8characters, location.utf8length); @@ -264,6 +408,9 @@ NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[]) /* new APIs */ p_scriptClass = RuntimeNPClass::getClass(); + if( !events.init() ) + return NPERR_GENERIC_ERROR; + return NPERR_NO_ERROR; } @@ -272,8 +419,12 @@ VlcPlugin::~VlcPlugin() free(psz_baseURL); free(psz_target); free(psz_text); + if( libvlc_media_player ) + { + events.unhook_manager(); libvlc_media_player_release( libvlc_media_player ); + } if( libvlc_media_list ) libvlc_media_list_release( libvlc_media_list ); if( libvlc_instance ) @@ -283,34 +434,31 @@ VlcPlugin::~VlcPlugin() /***************************************************************************** * VlcPlugin playlist replacement methods *****************************************************************************/ -void VlcPlugin::set_player_window( libvlc_exception_t *ex ) +void VlcPlugin::set_player_window() { #ifdef XP_UNIX libvlc_media_player_set_xwindow(libvlc_media_player, - (libvlc_drawable_t)getVideoWindow(), - ex); + (libvlc_drawable_t)getVideoWindow()); #endif #ifdef XP_MACOSX // XXX FIXME insert appropriate call here #endif #ifdef XP_WIN libvlc_media_player_set_hwnd(libvlc_media_player, - getWindow().window, - ex); + getWindow().window); #endif } -int VlcPlugin::playlist_add( const char *mrl, libvlc_exception_t *ex ) +int VlcPlugin::playlist_add( const char *mrl ) { int item = -1; - libvlc_media_t *p_m = libvlc_media_new(libvlc_instance,mrl,ex); - if( libvlc_exception_raised(ex) ) + libvlc_media_t *p_m = libvlc_media_new(libvlc_instance,mrl); + if( !p_m ) return -1; libvlc_media_list_lock(libvlc_media_list); - libvlc_media_list_add_media(libvlc_media_list,p_m,ex); - if( !libvlc_exception_raised(ex) ) - item = libvlc_media_list_count(libvlc_media_list,ex)-1; + if( !libvlc_media_list_add_media(libvlc_media_list,p_m) ) + item = libvlc_media_list_count(libvlc_media_list)-1; libvlc_media_list_unlock(libvlc_media_list); libvlc_media_release(p_m); @@ -319,118 +467,115 @@ int VlcPlugin::playlist_add( const char *mrl, libvlc_exception_t *ex ) } int VlcPlugin::playlist_add_extended_untrusted( const char *mrl, const char *name, - int optc, const char **optv, libvlc_exception_t *ex ) + int optc, const char **optv ) { - libvlc_media_t *p_m = libvlc_media_new(libvlc_instance, mrl,ex); + libvlc_media_t *p_m = libvlc_media_new(libvlc_instance, mrl); int item = -1; - if( libvlc_exception_raised(ex) ) + if( !p_m ) return -1; for( int i = 0; i < optc; ++i ) - { - libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique, ex); - if( libvlc_exception_raised(ex) ) - { - libvlc_media_release(p_m); - return -1; - } - } + libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique); libvlc_media_list_lock(libvlc_media_list); - libvlc_media_list_add_media(libvlc_media_list,p_m,ex); - if( !libvlc_exception_raised(ex) ) - item = libvlc_media_list_count(libvlc_media_list,ex)-1; + if( !libvlc_media_list_add_media(libvlc_media_list,p_m) ) + item = libvlc_media_list_count(libvlc_media_list)-1; libvlc_media_list_unlock(libvlc_media_list); libvlc_media_release(p_m); return item; } -bool VlcPlugin::playlist_select( int idx, libvlc_exception_t *ex ) +bool VlcPlugin::playlist_select( int idx ) { libvlc_media_t *p_m = NULL; libvlc_media_list_lock(libvlc_media_list); - int count = libvlc_media_list_count(libvlc_media_list,ex); - if( libvlc_exception_raised(ex) ) - goto bad_unlock; + int count = libvlc_media_list_count(libvlc_media_list); if( idx<0||idx>=count ) goto bad_unlock; playlist_index = idx; - p_m = libvlc_media_list_item_at_index(libvlc_media_list,playlist_index,ex); + p_m = libvlc_media_list_item_at_index(libvlc_media_list,playlist_index); libvlc_media_list_unlock(libvlc_media_list); - if( libvlc_exception_raised(ex) ) + if( !p_m ) return false; if( libvlc_media_player ) { + events.unhook_manager(); libvlc_media_player_release( libvlc_media_player ); libvlc_media_player = NULL; } - libvlc_media_player = libvlc_media_player_new_from_media(p_m,ex); + libvlc_media_player = libvlc_media_player_new_from_media(p_m); if( libvlc_media_player ) - set_player_window(ex); + { + set_player_window(); + events.hook_manager( + libvlc_media_player_event_manager(libvlc_media_player), + event_callback, this); + } libvlc_media_release( p_m ); - return !libvlc_exception_raised(ex); + return true; bad_unlock: libvlc_media_list_unlock(libvlc_media_list); return false; } -void VlcPlugin::playlist_delete_item( int idx, libvlc_exception_t *ex ) +int VlcPlugin::playlist_delete_item( int idx ) { libvlc_media_list_lock(libvlc_media_list); - libvlc_media_list_remove_index(libvlc_media_list,idx,ex); + int ret = libvlc_media_list_remove_index(libvlc_media_list,idx); libvlc_media_list_unlock(libvlc_media_list); + return ret; } -void VlcPlugin::playlist_clear( libvlc_exception_t *ex ) +void VlcPlugin::playlist_clear() { if( libvlc_media_list ) libvlc_media_list_release(libvlc_media_list); - libvlc_media_list = libvlc_media_list_new(getVLC(),ex); + libvlc_media_list = libvlc_media_list_new(getVLC()); } -int VlcPlugin::playlist_count( libvlc_exception_t *ex ) +int VlcPlugin::playlist_count() { int items_count = 0; libvlc_media_list_lock(libvlc_media_list); - items_count = libvlc_media_list_count(libvlc_media_list,ex); + items_count = libvlc_media_list_count(libvlc_media_list); libvlc_media_list_unlock(libvlc_media_list); return items_count; } -void VlcPlugin::toggle_fullscreen( libvlc_exception_t *ex ) +void VlcPlugin::toggle_fullscreen() { - if( playlist_isplaying(ex) ) - libvlc_toggle_fullscreen(libvlc_media_player,ex); + if( playlist_isplaying() ) + libvlc_toggle_fullscreen(libvlc_media_player); } -void VlcPlugin::set_fullscreen( int yes, libvlc_exception_t *ex ) +void VlcPlugin::set_fullscreen( int yes) { - if( playlist_isplaying(ex) ) - libvlc_set_fullscreen(libvlc_media_player,yes,ex); + if( playlist_isplaying() ) + libvlc_set_fullscreen(libvlc_media_player,yes); } -int VlcPlugin::get_fullscreen( libvlc_exception_t *ex ) +int VlcPlugin::get_fullscreen() { int r = 0; - if( playlist_isplaying(ex) ) - r = libvlc_get_fullscreen(libvlc_media_player,ex); + if( playlist_isplaying() ) + r = libvlc_get_fullscreen(libvlc_media_player); return r; } -int VlcPlugin::player_has_vout( libvlc_exception_t *ex ) +bool VlcPlugin::player_has_vout() { - int r = 0; - if( playlist_isplaying(ex) ) - r = libvlc_media_player_has_vout(libvlc_media_player, ex); + bool r = false; + if( playlist_isplaying() ) + r = libvlc_media_player_has_vout(libvlc_media_player); return r; } @@ -475,7 +620,7 @@ relativeurl: if( psz_baseURL ) { size_t baseLen = strlen(psz_baseURL); - char *href = static_cast(malloc(baseLen+strlen(url)+1)); + char *href = (char *) malloc(baseLen+strlen(url)+1); if( href ) { /* prepend base URL */ @@ -519,7 +664,7 @@ relativeurl: if( '/' != *href ) { /* baseURL is not an absolute path */ - free(href); + free(href); return NULL; } pathstart = href; @@ -592,7 +737,7 @@ relativeurl: return NULL; } -#if XP_UNIX +#if defined(XP_UNIX) int VlcPlugin::setSize(unsigned width, unsigned height) { int diff = (width != i_width) || (height != i_height); @@ -733,7 +878,6 @@ void VlcPlugin::hideToolbar() void VlcPlugin::redrawToolbar() { - libvlc_exception_t ex; int is_playing = 0; bool b_mute = false; unsigned int dst_x, dst_y; @@ -751,11 +895,9 @@ void VlcPlugin::redrawToolbar() getToolbarSize( &i_tb_width, &i_tb_height ); - libvlc_exception_init( &ex ); /* get mute info */ - b_mute = libvlc_audio_get_mute( getVLC(), &ex ); - libvlc_exception_clear( &ex ); + b_mute = libvlc_audio_get_mute( libvlc_media_player ); gcv.foreground = BlackPixel( p_display, 0 ); gc = XCreateGC( p_display, control, GCForeground, &gcv ); @@ -821,12 +963,11 @@ void VlcPlugin::redrawToolbar() (window.width-(dst_x+BTN_SPACE)), p_timeline->height ); /* get movie position in % */ - if( playlist_isplaying(&ex) ) + if( playlist_isplaying() ) { i_last_position = (int)((window.width-(dst_x+BTN_SPACE))* - libvlc_media_player_get_position(libvlc_media_player,&ex)); + libvlc_media_player_get_position(libvlc_media_player)); } - libvlc_exception_clear( &ex ); if( p_btnTime ) XPutImage( p_display, control, gc, p_btnTime, @@ -842,7 +983,6 @@ vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos unsigned int i_dest = BTN_SPACE; int is_playing = 0; bool b_mute = false; - libvlc_exception_t ex; #ifndef NDEBUG fprintf( stderr, "ToolbarButtonClicked:: " @@ -858,13 +998,10 @@ vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos */ /* get isplaying */ - libvlc_exception_init( &ex ); - is_playing = playlist_isplaying( &ex ); - libvlc_exception_clear( &ex ); + is_playing = playlist_isplaying(); /* get mute info */ - b_mute = libvlc_audio_get_mute( getVLC(), &ex ); - libvlc_exception_clear( &ex ); + b_mute = libvlc_audio_get_mute( libvlc_media_player ); /* is Pause of Play button clicked */ if( (is_playing != 1) && @@ -919,3 +1056,20 @@ vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos } #undef BTN_SPACE #endif + +// Verifies the version of the NPAPI. +// The eventListeners use a NPAPI function available +// since Gecko 1.9. +bool VlcPlugin::canUseEventListener() +{ + int plugin_major, plugin_minor; + int browser_major, browser_minor; + + NPN_Version(&plugin_major, &plugin_minor, + &browser_major, &browser_minor); + + if (browser_minor >= 19 || browser_major > 0) + return true; + return false; +} +