]> git.sesse.net Git - vlc/blobdiff - projects/mozilla/vlcshell.cpp
mozilla: cleanup
[vlc] / projects / mozilla / vlcshell.cpp
index ecb40d8ebe8337c1f46ae17ccaefcc9e49f354aa..ab4cf514d5762cc04027900911c456ec7cbacff7 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * vlcshell.cpp: a VLC plugin for Mozilla
  *****************************************************************************
- * Copyright (C) 2002-2005 the VideoLAN team
+ * Copyright (C) 2002-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Samuel Hocevar <sam@zoy.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
 #endif
 
 #include "vlcplugin.h"
+#include "vlcshell.h"
 
 /* Enable/disable debugging printf's for X11 resizing */
 #undef X11_RESIZE_DEBUG
 
-#define WINDOW_TEXT "Video is loading..."
+#define WINDOW_TEXT "Waiting for video"
 
 /*****************************************************************************
  * Unix-only declarations
@@ -78,24 +80,25 @@ static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpa
  *****************************************************************************/
 char * NPP_GetMIMEDescription( void )
 {
-    return PLUGIN_MIMETYPES;
+    static char mimetype[] = PLUGIN_MIMETYPES;
+    return mimetype;
 }
 
 NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
 {
-
+    static char psz_name[] = PLUGIN_NAME;
     static char psz_desc[1000];
 
     /* plugin class variables */
     switch( variable )
     {
         case NPPVpluginNameString:
-            *((char **)value) = PLUGIN_NAME;
+            *((char **)value) = psz_name;
             return NPERR_NO_ERROR;
 
         case NPPVpluginDescriptionString:
             snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
-                      VLC_Version() );
+                      libvlc_get_version() );
             *((char **)value) = psz_desc;
             return NPERR_NO_ERROR;
 
@@ -156,6 +159,8 @@ NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
 int16 NPP_HandleEvent( NPP instance, void * event )
 {
     static UInt32 lastMouseUp = 0;
+    libvlc_exception_t ex;
+    libvlc_exception_init(&ex);
 
     if( instance == NULL )
     {
@@ -180,21 +185,8 @@ int16 NPP_HandleEvent( NPP instance, void * event )
             if( (myEvent->when - lastMouseUp) < GetDblTime() )
             {
                 /* double click */
-                libvlc_instance_t *p_vlc = p_plugin->getVLC();
-
-                if( p_vlc )
-                {
-                    if( libvlc_playlist_isplaying(p_vlc, NULL) )
-                    {
-                        libvlc_media_player_t *p_md =
-                            libvlc_playlist_get_media_player(p_vlc, NULL);
-                        if( p_md )
-                        {
-                            libvlc_toggle_fullscreen(p_md, NULL);
-                            libvlc_media_player_release(p_md);
-                        }
-                    }
-                }
+                p_plugin->toggle_fullscreen(&ex);
+                libvlc_exception_clear(&ex);
             }
             return true;
         }
@@ -211,32 +203,21 @@ int16 NPP_HandleEvent( NPP instance, void * event )
             if( npwindow.window )
             {
                 int hasVout = FALSE;
-                libvlc_instance_t *p_vlc = p_plugin->getVLC();
 
-                if( p_vlc )
+                if( p_plugin->playlist_isplaying(&ex) )
                 {
-                    if( libvlc_playlist_isplaying(p_vlc, NULL) )
+                    hasVout = p_plugin->player_has_vout(NULL);
+                    if( hasVout )
                     {
-                        libvlc_media_player_t *p_md =
-                            libvlc_playlist_get_media_player(p_vlc, NULL);
-                        if( p_md )
-                        {
-                            hasVout = libvlc_media_player_has_vout(p_md,
-                                                                     NULL);
-                            if( hasVout )
-                            {
-                                libvlc_rectangle_t area;
-                                area.left = 0;
-                                area.top = 0;
-                                area.right = npwindow.width;
-                                area.bottom = npwindow.height;
-                                libvlc_video_redraw_rectangle(p_md, &area,
-                                                              NULL);
-                            }
-                            libvlc_media_player_release(p_md);
-                        }
+                        libvlc_rectangle_t area;
+                        area.left = 0;
+                        area.top = 0;
+                        area.right = npwindow.width;
+                        area.bottom = npwindow.height;
+                        libvlc_video_redraw_rectangle(p_plugin->getMD(&ex), &area, NULL);
                     }
                 }
+                libvlc_exception_clear(&ex);
 
                 if( ! hasVout )
                 {
@@ -364,7 +345,7 @@ NPError NPP_Destroy( NPP instance, NPSavedData** save )
 
 NPError NPP_SetWindow( NPP instance, NPWindow* window )
 {
-#ifdef XP_UNIX && !defined(__APPLE__)
+#if defined(XP_UNIX) && !defined(__APPLE__)
     Window control;
     unsigned int i_control_height = 0, i_control_width = 0;
 #endif
@@ -381,9 +362,14 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
         /* we should probably show a splash screen here */
         return NPERR_NO_ERROR;
     }
-#ifdef XP_UNIX && !defined(__APPLE__)
+
+#if defined(XP_UNIX) && !defined(__APPLE__)
     control = p_plugin->getControlWindow();
 #endif
+
+    libvlc_exception_t ex;
+    libvlc_exception_init(&ex);
+
     libvlc_instance_t *p_vlc = p_plugin->getVLC();
 
     /*
@@ -405,7 +391,10 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
         if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
         {
             /* set/change parent window */
-            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
+            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, &ex);
+            if( libvlc_exception_raised(&ex) )
+                fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+            libvlc_exception_clear(&ex);
         }
 
         /* as MacOS X video output is windowless, set viewport */
@@ -420,20 +409,30 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
         view.left    = ((NP_Port*) (window->window))->portx;
         view.bottom  = window->height+view.top;
         view.right   = window->width+view.left;
+
         /* clipRect coordinates are also relative to GrafPort */
         clip.top     = window->clipRect.top;
         clip.left    = window->clipRect.left;
         clip.bottom  = window->clipRect.bottom;
         clip.right   = window->clipRect.right;
 
-        libvlc_video_set_viewport(p_vlc, &view, &clip, NULL);
+        libvlc_video_set_viewport(p_vlc, &view, &clip, &ex);
+        if( libvlc_exception_raised(&ex) )
+            fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+        libvlc_exception_clear(&ex);
 
         /* remember new window */
         p_plugin->setWindow(*window);
     }
-    else if( curwin.window ) {
+    else if( curwin.window )
+    {
         /* change/set parent */
-        libvlc_video_set_parent(p_vlc, 0, NULL);
+        libvlc_video_set_parent(p_vlc, 0, &ex);
+
+        if( libvlc_exception_raised(&ex) )
+            fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+        libvlc_exception_clear(&ex);
+
         curwin.window = NULL;
     }
 #endif /* XP_MACOSX */
@@ -467,7 +466,10 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
             SetWindowLong((HWND)drawable, GWL_STYLE, style);
 
             /* change/set parent */
-            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
+            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, &ex);
+            if( libvlc_exception_raised(&ex) )
+                fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+            libvlc_exception_clear(&ex);
 
             /* remember new window */
             p_plugin->setWindow(*window);
@@ -477,31 +479,33 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
             UpdateWindow( (HWND)drawable );
         }
     }
-    else if ( curwin.window )
+    else if( curwin.window )
     {
         /* reset WNDPROC */
         HWND oldwin = (HWND)curwin.window;
         SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
         p_plugin->setWindowProc(NULL);
+
         /* change/set parent */
-        libvlc_video_set_parent(p_vlc, 0, NULL);
+        libvlc_video_set_parent(p_vlc, 0, &ex);
+        if( libvlc_exception_raised(&ex) )
+            fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+        libvlc_exception_clear(&ex);
+
         curwin.window = NULL;
     }
 #endif /* XP_WIN */
 
 #ifdef XP_UNIX
-    if( p_plugin->b_toolbar )
-    {
-        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
-    }
-    else
-    {
-        i_control_height = i_control_width = 0;
-    }
+    /* default to hidden toolbar, shown at the end of this method if asked *
+     * developers note : getToolbarSize need to wait the end of this method
+     */
+    i_control_height = 0;
+    i_control_width = window->width;
 
     if( window && window->window )
     {
-        Window  parent   = (Window) window->window;
+        Window  parent  = (Window) window->window;
         if( !curwin.window || (parent != (Window)curwin.window) )
         {
             Display *p_display = ( (NPSetWindowCallbackStruct *)
@@ -516,13 +520,10 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
                            window->width, window->height - i_control_height,
                            0, i_blackColor, i_blackColor );
             Window controls = (Window) NULL;
-            if( p_plugin->b_toolbar )
-            {
-                controls = XCreateSimpleWindow( p_display, parent,
-                                0, window->height - i_control_height-1,
-                                window->width, i_control_height-1,
-                                0, i_blackColor, i_blackColor );
-            }
+            controls = XCreateSimpleWindow( p_display, parent,
+                            0, window->height - i_control_height-1,
+                            window->width, i_control_height-1,
+                            0, i_blackColor, i_blackColor );
 
             XMapWindow( p_display, parent );
             XMapWindow( p_display, video );
@@ -544,29 +545,43 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
 /*
             libvlc_media_player_t *p_md;
 
-            libvlc_exception_t ex;
-            libvlc_exception_init(& ex );
-            p_md = libvlc_playlist_get_media_player( p_plugin->getVLC(), &ex );
-            libvlc_exception_init( &ex );
+            p_md = p_plugin->getMD( &ex );
+            libvlc_exception_clear( &ex );
             libvlc_event_attach( libvlc_media_player_event_manager( p_md, &ex ),
                                  libvlc_MediaPlayerPositionChanged, Redraw, NULL, &ex );
 */
 
             /* set/change parent window */
-            libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, NULL );
+            libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, &ex );
+            if( libvlc_exception_raised(&ex) )
+                fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+            libvlc_exception_clear(&ex);
 
             /* remember window */
             p_plugin->setWindow( *window );
             p_plugin->setVideoWindow( video );
-            if( controls ) { p_plugin->setControlWindow( controls ); }
+
+            if( controls )
+            {
+                p_plugin->setControlWindow( controls );
+            }
 
             Redraw( w, (XtPointer)p_plugin, NULL );
+
+            /* now display toolbar if asked through parameters */
+            if( p_plugin->b_toolbar )
+            {
+                p_plugin->showToolbar();
+            }
         }
     }
-    else if ( curwin.window )
+    else if( curwin.window )
     {
         /* change/set parent */
-        libvlc_video_set_parent(p_vlc, 0, NULL);
+        libvlc_video_set_parent(p_vlc, 0, &ex);
+        if( libvlc_exception_raised(&ex) )
+            fprintf( stderr, "Exception: %s\n", libvlc_exception_get_message(&ex) );
+        libvlc_exception_clear(&ex);
         curwin.window = NULL;
     }
 #endif /* XP_UNIX */
@@ -575,12 +590,11 @@ NPError NPP_SetWindow( NPP instance, NPWindow* window )
     {
         if( p_plugin->psz_target )
         {
-            if( libvlc_playlist_add( p_vlc, p_plugin->psz_target,
-                                     NULL, NULL ) != -1 )
+            if( p_plugin->playlist_add( p_plugin->psz_target, NULL ) != -1 )
             {
                 if( p_plugin->b_autoplay )
                 {
-                    libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
+                    p_plugin->playlist_play(NULL);
                 }
             }
             p_plugin->b_stream = true;
@@ -625,7 +639,6 @@ int32 NPP_WriteReady( NPP instance, NPStream *stream )
     return 8*1024;
 }
 
-
 int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
                  int32 len, void *buffer )
 {
@@ -633,7 +646,6 @@ int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
     return len;
 }
 
-
 NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
 {
     if( instance == NULL )
@@ -643,7 +655,6 @@ NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
     return NPERR_NO_ERROR;
 }
 
-
 void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
 {
     if( instance == NULL )
@@ -657,17 +668,15 @@ void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
         return;
     }
 
-    if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL )
-        != -1 )
+    if( p_plugin->playlist_add( stream->url, NULL ) != -1 )
     {
         if( p_plugin->b_autoplay )
         {
-            libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
+            p_plugin->playlist_play(NULL);
         }
     }
 }
 
-
 void NPP_URLNotify( NPP instance, const char* url,
                     NPReason reason, void* notifyData )
 {
@@ -678,7 +687,6 @@ void NPP_URLNotify( NPP instance, const char* url,
     \*********************************************/
 }
 
-
 void NPP_Print( NPP instance, NPPrint* printInfo )
 {
     if( printInfo == NULL )
@@ -800,10 +808,7 @@ static void Redraw( Widget w, XtPointer closure, XEvent *event )
     unsigned int i_control_height, i_control_width;
 
     if( p_plugin->b_toolbar )
-    {
-        p_plugin->showToolbar();
         p_plugin->getToolbarSize( &i_control_width, &i_control_height );
-    }
     else
         i_control_height = i_control_width = 0;
 
@@ -824,11 +829,7 @@ static void Redraw( Widget w, XtPointer closure, XEvent *event )
                  WINDOW_TEXT, strlen(WINDOW_TEXT) );
     XFreeGC( p_display, gc );
 
-    if( p_plugin->b_toolbar )
-    {
-        p_plugin->redrawToolbar();
-        p_plugin->hideToolbar();
-    }
+    p_plugin->redrawToolbar();
 }
 
 static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
@@ -843,81 +844,95 @@ static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
 
     if( p_plugin && p_plugin->b_toolbar )
     {
+        int i_playing;
         libvlc_exception_t ex;
+
         libvlc_exception_init( &ex );
-        libvlc_media_player_t *p_md =
-                libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
+        libvlc_media_player_t *p_md = p_plugin->getMD(&ex);
+        if( libvlc_exception_raised(&ex) )
+            fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
+        libvlc_exception_clear( &ex );
+
+        i_playing = p_plugin->playlist_isplaying( &ex );
+        if( libvlc_exception_raised(&ex) )
+            fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
         libvlc_exception_clear( &ex );
 
-        /* jump in the movie */
-        if( i_yPos <= (i_height-30) )
+        vlc_toolbar_clicked_t clicked;
+        clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
+        switch( clicked )
         {
-            /* if a movie is loaded */
-            if( p_md )
+            case clicked_Play:
+            case clicked_Pause:
             {
-                vlc_int64_t f_length;
-                libvlc_exception_init( &ex );
-                f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
+                if( i_playing == 1 )
+                    p_plugin->playlist_pause( &ex );
+                else
+                    p_plugin->playlist_play( &ex );
+
+                if( libvlc_exception_raised(&ex) )
+                    fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
                 libvlc_exception_clear( &ex );
+            }
+            break;
 
-                f_length = (float)f_length *
-                           ( ((float)i_xPos-4 ) / ( ((float)i_width-8)/100) );
+            case clicked_Stop:
+            {
+                p_plugin->playlist_stop(&ex);
+                if( libvlc_exception_raised(&ex) )
+                    fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
+                libvlc_exception_clear( &ex );
+            }
+            break;
 
-                libvlc_exception_init( &ex );
-                libvlc_media_player_set_time( p_md, f_length, &ex );
+            case clicked_Fullscreen:
+            {
+                p_plugin->set_fullscreen( 1, &ex );
+                if( libvlc_exception_raised(&ex) )
+                    fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
                 libvlc_exception_clear( &ex );
             }
-        }
+            break;
 
-        /* play/pause toggle */
-        if( (i_yPos > (i_height-30)) && (i_xPos > 4) && (i_xPos <= 39) )
-        {
-            int i_playing;
-            libvlc_exception_init( &ex );
-            i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
-            libvlc_exception_clear( &ex );
+            case clicked_Mute:
+            case clicked_Unmute:
+            {
+                libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
+                if( libvlc_exception_raised(&ex) )
+                    fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
+                libvlc_exception_clear( &ex );
+            }
+            break;
 
-            libvlc_exception_init( &ex );
-            if( i_playing == 1 )
-                libvlc_playlist_pause( p_plugin->getVLC(), &ex );
-            else
-                libvlc_playlist_play( p_plugin->getVLC(), -1, 0, NULL, &ex );
-            libvlc_exception_clear( &ex );
-        }
+            case clicked_timeline:
+            {
+                /* if a movie is loaded */
+                if( p_md )
+                {
+                    int64_t f_length;
+                    f_length = libvlc_media_player_get_length( p_md, &ex ) / 100;
+                    libvlc_exception_clear( &ex );
 
-        /* stop */
-        if( (i_yPos > (i_height-30)) && (i_xPos > 39) && (i_xPos < 67) )
-        {
-            libvlc_exception_init( &ex );
-            libvlc_playlist_stop( p_plugin->getVLC(), &ex );
-            libvlc_exception_clear( &ex );
-        }
+                    f_length = (float)f_length *
+                            ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
 
-        /* fullscreen */
-        if( (i_yPos > (i_height-30)) && (i_xPos >= 67) && (i_xPos < 94) )
-        {
-            int i_playing;
-            libvlc_exception_init( &ex );
-            i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
-            libvlc_exception_clear( &ex );
+                    libvlc_media_player_set_time( p_md, f_length, &ex );
+                    if( libvlc_exception_raised(&ex) )
+                        fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
+                    libvlc_exception_clear( &ex );
+                }
+            }
+            break;
 
-            if( (i_playing == 1) && p_md )
+            case clicked_Time:
             {
-                libvlc_exception_init( &ex );
-                libvlc_set_fullscreen( p_md, 1, &ex );
-                libvlc_exception_clear( &ex );
+                /* Not implemented yet*/
             }
-        }
+            break;
 
-        /* mute toggle */
-        if( (i_yPos > (i_height-30)) && (i_xPos >= 94) && (i_xPos < 109))
-        {
-            libvlc_exception_init( &ex );
-            libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
-            libvlc_exception_clear( &ex );
+            default: /* button_Unknown */
+            break;
         }
-
-        if( p_md ) libvlc_media_player_release( p_md );
     }
     Redraw( w, closure, event );
 }