vlcshell.cpp 18.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * vlcshell.c: a VideoLAN Client plugin for Mozilla
 *****************************************************************************
 * Copyright (C) 2002 VideoLAN
5
 * $Id: vlcshell.cpp,v 1.9 2003/02/01 18:54:10 sam Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Authors: Samuel Hocevar <sam@zoy.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
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdio.h>
#include <string.h>

/* vlc stuff */
#include <vlc/vlc.h>

33 34 35
/* Mozilla stuff */
#include <npapi.h>

36 37 38
#ifdef XP_WIN
    /* Windows stuff */
#endif
39

40
#ifdef XP_UNIX
41 42 43 44 45 46
    /* X11 stuff */
#   include <X11/Xlib.h>
#   include <X11/Intrinsic.h>
#   include <X11/StringDefs.h>
#endif

47 48 49
#include "vlcpeer.h"
#include "vlcplugin.h"

50 51 52 53 54 55 56 57 58
/* XXX: disable VLC */
#define USE_LIBVLC 1

#if USE_LIBVLC
#   define WINDOW_TEXT "(no picture)"
#else
#   define WINDOW_TEXT "(no libvlc)"
#endif

59
/*****************************************************************************
60
 * Unix-only declarations
61
******************************************************************************/
62 63
#ifdef XP_UNIX
#   define VOUT_PLUGINS "xvideo,x11,dummy"
64
#   define AOUT_PLUGINS "oss,dummy"
65

66 67 68
static void Redraw( Widget w, XtPointer closure, XEvent *event );
#endif

69
/*****************************************************************************
70 71
 * Windows-only declarations
 *****************************************************************************/
72 73 74 75
#ifdef XP_WIN
#   define VOUT_PLUGINS "directx,dummy"
#   define AOUT_PLUGINS "none" /* "directx,waveout,dummy" */

76 77 78 79 80 81 82
HINSTANCE g_hDllInstance = NULL;

BOOL WINAPI
DllMain( HINSTANCE  hinstDLL,                   // handle of DLL module
                    DWORD  fdwReason,       // reason for calling function
                    LPVOID  lpvReserved)
{
83 84 85 86 87 88 89 90 91 92 93
    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
            g_hDllInstance = hinstDLL;
            break;
        case DLL_THREAD_ATTACH:
        case DLL_PROCESS_DETACH:
        case DLL_THREAD_DETACH:
            break;
    }
    return TRUE;
94
}
95 96

LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
97 98 99 100 101 102 103 104 105 106 107 108 109
#endif

/******************************************************************************
 * UNIX-only API calls
 *****************************************************************************/
char * NPP_GetMIMEDescription( void )
{
    return PLUGIN_MIMETYPES;
}

NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
{
    static nsIID nsid = VLCINTF_IID;
Sam Hocevar's avatar
Sam Hocevar committed
110
    static char psz_desc[1000];
111 112 113 114 115 116 117 118

    switch( variable )
    {
        case NPPVpluginNameString:
            *((char **)value) = PLUGIN_NAME;
            return NPERR_NO_ERROR;

        case NPPVpluginDescriptionString:
119
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
120
            snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
121 122 123
#else
            snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
#endif
Sam Hocevar's avatar
Sam Hocevar committed
124 125
            psz_desc[1000-1] = 0;
            *((char **)value) = psz_desc;
126
            return NPERR_NO_ERROR;
Sam Hocevar's avatar
Sam Hocevar committed
127 128 129 130

        default:
            /* go on... */
            break;
131 132 133 134 135 136 137 138 139 140 141 142
    }

    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

    VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;

    switch( variable )
    {
        case NPPVpluginScriptableInstance:
143
            *(nsISupports**)value = p_plugin->GetPeer();
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
            if( *(nsISupports**)value == NULL )
            {
                return NPERR_OUT_OF_MEMORY_ERROR;
            }
            break;

        case NPPVpluginScriptableIID:
            *(nsIID**)value = (nsIID*)NPN_MemAlloc( sizeof(nsIID) );
            if( *(nsIID**)value == NULL )
            {
                return NPERR_OUT_OF_MEMORY_ERROR;
            }
            **(nsIID**)value = nsid;
            break;

        default:
            return NPERR_GENERIC_ERROR;
    }

    return NPERR_NO_ERROR;
}

/******************************************************************************
 * General Plug-in Calls
 *****************************************************************************/
NPError NPP_Initialize( void )
{
    return NPERR_NO_ERROR;
}

jref NPP_GetJavaClass( void )
{
    return NULL;
}

void NPP_Shutdown( void )
{
    ;
}

NPError NPP_New( NPMIMEType pluginType, NPP instance, uint16 mode, int16 argc,
                 char* argn[], char* argv[], NPSavedData* saved )
{
187 188
    int i;
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
189
    vlc_value_t value;
190 191 192 193 194 195 196
    int i_ret;

    char *ppsz_foo[] =
    {
        "vlc"
        /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
    };
197
#endif
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

    VlcPlugin * p_plugin = new VlcPlugin( instance );

    if( p_plugin == NULL )
    {
        return NPERR_OUT_OF_MEMORY_ERROR;
    }

    instance->pdata = p_plugin;

213 214 215 216
#ifdef XP_WIN
    p_plugin->p_hwnd = NULL;
    p_plugin->pf_wndproc = NULL;
#endif
217

218
#ifdef XP_UNIX
219
    p_plugin->window = 0;
220
    p_plugin->p_display = NULL;
221
#endif
222

223 224 225 226 227 228
    p_plugin->p_npwin = NULL;
    p_plugin->i_npmode = mode;
    p_plugin->i_width = 0;
    p_plugin->i_height = 0;

#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
229 230
    p_plugin->i_vlc = VLC_Create();
    if( p_plugin->i_vlc < 0 )
231
    {
Sam Hocevar's avatar
Sam Hocevar committed
232
        p_plugin->i_vlc = 0;
233 234 235 236 237
        delete p_plugin;
        p_plugin = NULL;
        return NPERR_GENERIC_ERROR;
    }

Sam Hocevar's avatar
Sam Hocevar committed
238
    i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_foo)/sizeof(char*), ppsz_foo );
239 240
    if( i_ret )
    {
Sam Hocevar's avatar
Sam Hocevar committed
241 242
        VLC_Destroy( p_plugin->i_vlc );
        p_plugin->i_vlc = 0;
243 244 245 246 247
        delete p_plugin;
        p_plugin = NULL;
        return NPERR_GENERIC_ERROR;
    }

Sam Hocevar's avatar
Sam Hocevar committed
248 249
    value.psz_string = "dummy";
    VLC_Set( p_plugin->i_vlc, "conf::intf", value );
250
    value.psz_string = VOUT_PLUGINS;
251
    VLC_Set( p_plugin->i_vlc, "conf::vout", value );
252
    value.psz_string = AOUT_PLUGINS;
253
    VLC_Set( p_plugin->i_vlc, "conf::aout", value );
254

255 256 257 258 259 260 261
#else
    p_plugin->i_vlc = 1;

#endif /* USE_LIBVLC */

    p_plugin->b_stream = VLC_FALSE;
    p_plugin->b_autoplay = VLC_FALSE;
262 263 264 265
    p_plugin->psz_target = NULL;

    for( i = 0; i < argc ; i++ )
    {
266
        if( !strcmp( argn[i], "target" ) )
267
        {
268
            p_plugin->psz_target = argv[i];
269
        }
270
        else if( !strcmp( argn[i], "autoplay" ) )
271
        {
272 273 274 275 276
            if( !strcmp( argv[i], "yes" ) )
            {
                p_plugin->b_autoplay = 1;
            }
        }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        else if( !strcmp( argn[i], "autostart" ) )
        {
            if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "true" ) )
            {
                p_plugin->b_autoplay = 1;
            }
        }
        else if( !strcmp( argn[i], "filename" ) )
        {
            p_plugin->psz_target = argv[i];
        }
        else if( !strcmp( argn[i], "src" ) )
        {
            p_plugin->psz_target = argv[i];
        }
292

293
#if USE_LIBVLC
294 295 296 297
        else if( !strcmp( argn[i], "loop" ) )
        {
            if( !strcmp( argv[i], "yes" ) )
            {
Sam Hocevar's avatar
Sam Hocevar committed
298 299
                value.b_bool = VLC_TRUE;
                VLC_Set( p_plugin->i_vlc, "conf::loop", value );
300
            }
301
        }
302
#endif
303 304
    }

305 306 307 308 309
    if( p_plugin->psz_target )
    {
        p_plugin->psz_target = strdup( p_plugin->psz_target );
    }

310 311 312 313 314 315 316 317 318 319 320 321 322 323
    return NPERR_NO_ERROR;
}

NPError NPP_Destroy( NPP instance, NPSavedData** save )
{
    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

    VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;

    if( p_plugin != NULL )
    {
Sam Hocevar's avatar
Sam Hocevar committed
324
        if( p_plugin->i_vlc )
325
        {
326
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
327 328
            VLC_Stop( p_plugin->i_vlc );
            VLC_Destroy( p_plugin->i_vlc );
329
#endif
Sam Hocevar's avatar
Sam Hocevar committed
330
            p_plugin->i_vlc = 0;
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        }

        if( p_plugin->psz_target )
        {
            free( p_plugin->psz_target );
            p_plugin->psz_target = NULL;
        }

        delete p_plugin;
    }

    instance->pdata = NULL;

    return NPERR_NO_ERROR;
}

NPError NPP_SetWindow( NPP instance, NPWindow* window )
{
    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

    VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;

    /* Write the window ID for vlc */
357 358 359
#if USE_LIBVLC
    vlc_value_t value;

360
    /* FIXME: this cast sucks */
361
    value.i_int = (int) (ptrdiff_t) (void *) window->window;
362
    VLC_Set( p_plugin->i_vlc, "drawable", value );
363 364
#endif

365 366 367 368 369 370 371 372
    /*
     * PLUGIN DEVELOPERS:
     *  Before setting window to point to the
     *  new window, you may wish to compare the new window
     *  info to the previous window (if any) to note window
     *  size changes, etc.
     */

373 374 375 376 377 378 379 380 381 382 383
#ifdef XP_WIN
    if( !window || !window->window )
    {
        /* Window was destroyed. Invalidate everything. */
        if( p_plugin->p_npwin )
        {
            SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
                           (LONG)p_plugin->pf_wndproc );
            p_plugin->pf_wndproc = NULL;
            p_plugin->p_hwnd = NULL;
        }
384

385 386 387
        p_plugin->p_npwin = window;
        return NPERR_NO_ERROR;
    }
388

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    if( p_plugin->p_npwin )
    {
        if( p_plugin->p_hwnd == (HWND)window->window )
        {
            /* Same window, but something may have changed. First we
             * update the plugin structure, then we redraw the window */
            InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
            p_plugin->i_width = window->width;
            p_plugin->i_height = window->height;
            p_plugin->p_npwin = window;
            UpdateWindow( p_plugin->p_hwnd );
            return NPERR_NO_ERROR;
        }

        /* Window has changed. Destroy the one we have, and go
         * on as if it was a real initialization. */
        SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
                       (LONG)p_plugin->pf_wndproc );
        p_plugin->pf_wndproc = NULL;
        p_plugin->p_hwnd = NULL;
    }

    p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
                                                   GWL_WNDPROC, (LONG)Manage );
    p_plugin->p_hwnd = (HWND)window->window;
    SetProp( p_plugin->p_hwnd, "w00t", (HANDLE)p_plugin );
    InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
    UpdateWindow( p_plugin->p_hwnd );
#endif

#ifdef XP_UNIX
420
    p_plugin->window = (Window) window->window;
421 422 423 424 425 426
    p_plugin->p_display = ((NPSetWindowCallbackStruct *)window->ws_info)->display;

    Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
    XtAddEventHandler( w, ExposureMask, FALSE,
                       (XtEventHandler)Redraw, p_plugin );
    Redraw( w, (XtPointer)p_plugin, NULL );
427
#endif
428

429 430 431 432
    p_plugin->p_npwin = window;

    p_plugin->i_width = window->width;
    p_plugin->i_height = window->height;
433 434 435

    if( !p_plugin->b_stream )
    {
436 437 438 439 440 441 442
        int i_mode = PLAYLIST_APPEND;

        if( p_plugin->b_autoplay )
        {
            i_mode |= PLAYLIST_GO;
        }

443 444
        if( p_plugin->psz_target )
        {
445
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
446 447
            VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
                           i_mode, PLAYLIST_END );
448 449
#endif
            p_plugin->b_stream = VLC_TRUE;
450 451 452 453 454 455 456 457 458 459 460 461 462 463
        }
    }

    return NPERR_NO_ERROR;
}

NPError NPP_NewStream( NPP instance, NPMIMEType type, NPStream *stream,
                       NPBool seekable, uint16 *stype )
{
    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

464
#if 0
465
    VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
466
#endif
467

468
    /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
469 470 471 472 473

    /* We want a *filename* ! */
    *stype = NP_ASFILE;

#if 0
474
    if( !p_plugin->b_stream )
475 476
    {
        p_plugin->psz_target = strdup( stream->url );
477
        p_plugin->b_stream = VLC_TRUE;
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    }
#endif

    return NPERR_NO_ERROR;
}

int32 STREAMBUFSIZE = 0X0FFFFFFF; /* If we are reading from a file in NPAsFile
                   * mode so we can take any size stream in our
                   * write call (since we ignore it) */

#define SARASS_SIZE (1024*1024)

int32 NPP_WriteReady( NPP instance, NPStream *stream )
{
    VlcPlugin* p_plugin;

494
    /* fprintf(stderr, "NPP_WriteReady\n"); */
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

    if (instance != NULL)
    {
        p_plugin = (VlcPlugin*) instance->pdata;
        /* Muahahahahahahaha */
        return STREAMBUFSIZE;
        /*return SARASS_SIZE;*/
    }

    /* Number of bytes ready to accept in NPP_Write() */
    return STREAMBUFSIZE;
    /*return 0;*/
}


int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
                 int32 len, void *buffer )
{
513
    /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
514

515
    if( instance != NULL )
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
    {
        /*VlcPlugin* p_plugin = (VlcPlugin*) instance->pdata;*/
    }

    return len;         /* The number of bytes accepted */
}


NPError NPP_DestroyStream( NPP instance, NPStream *stream, NPError reason )
{
    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

    return NPERR_NO_ERROR;
}


void NPP_StreamAsFile( NPP instance, NPStream *stream, const char* fname )
{
    if( instance == NULL )
    {
        return;
    }

542
    /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
543 544

#if USE_LIBVLC
545
    VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
546

547 548
    VLC_AddTarget( p_plugin->i_vlc, fname,
                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
549
#endif
550
}
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565


void NPP_URLNotify( NPP instance, const char* url,
                    NPReason reason, void* notifyData )
{
    /***** Insert NPP_URLNotify code here *****\
    PluginInstance* p_plugin;
    if (instance != NULL)
        p_plugin = (PluginInstance*) instance->pdata;
    \*********************************************/
}


void NPP_Print( NPP instance, NPPrint* printInfo )
{
566 567
    if( printInfo == NULL )
    {
568
        return;
569
    }
570

571 572 573
    if( instance != NULL )
    {
        /***** Insert NPP_Print code here *****\
574
        PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
575
        \**************************************/
576

577 578
        if( printInfo->mode == NP_FULL )
        {
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
            /*
             * PLUGIN DEVELOPERS:
             *  If your plugin would like to take over
             *  printing completely when it is in full-screen mode,
             *  set printInfo->pluginPrinted to TRUE and print your
             *  plugin as you see fit.  If your plugin wants Netscape
             *  to handle printing in this case, set
             *  printInfo->pluginPrinted to FALSE (the default) and
             *  do nothing.  If you do want to handle printing
             *  yourself, printOne is true if the print button
             *  (as opposed to the print menu) was clicked.
             *  On the Macintosh, platformPrint is a THPrint; on
             *  Windows, platformPrint is a structure
             *  (defined in npapi.h) containing the printer name, port,
             *  etc.
             */

596
            /***** Insert NPP_Print code here *****\
597 598 599 600
            void* platformPrint =
                printInfo->print.fullPrint.platformPrint;
            NPBool printOne =
                printInfo->print.fullPrint.printOne;
601
            \**************************************/
602 603 604 605

            /* Do the default*/
            printInfo->print.fullPrint.pluginPrinted = FALSE;
        }
606 607 608
        else
        {
            /* If not fullscreen, we must be embedded */
609 610 611 612 613 614 615 616 617 618 619 620
            /*
             * PLUGIN DEVELOPERS:
             *  If your plugin is embedded, or is full-screen
             *  but you returned false in pluginPrinted above, NPP_Print
             *  will be called with mode == NP_EMBED.  The NPWindow
             *  in the printInfo gives the location and dimensions of
             *  the embedded plugin on the printed page.  On the
             *  Macintosh, platformPrint is the printer port; on
             *  Windows, platformPrint is the handle to the printing
             *  device context.
             */

621
            /***** Insert NPP_Print code here *****\
622 623 624 625
            NPWindow* printWindow =
                &(printInfo->print.embedPrint.window);
            void* platformPrint =
                printInfo->print.embedPrint.platformPrint;
626
            \**************************************/
627 628 629 630
        }
    }
}

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
/******************************************************************************
 * Windows-only methods
 *****************************************************************************/
#ifdef XP_WIN
LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
{
    VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );

    switch( i_msg )
    {
#if !USE_LIBVLC
        case WM_PAINT:
        {
            PAINTSTRUCT paintstruct;
            HDC hdc;
            RECT rect;

            hdc = BeginPaint( p_hwnd, &paintstruct );

            GetClientRect( p_hwnd, &rect );
            FillRect( hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH) );
            TextOut( hdc, p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
                     WINDOW_TEXT, strlen(WINDOW_TEXT) );

            EndPaint( p_hwnd, &paintstruct );
            break;
        }
#endif
        default:
            p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
            break;
    }
    return 0;
}
#endif

667 668 669
/******************************************************************************
 * UNIX-only methods
 *****************************************************************************/
670
#ifdef XP_UNIX
671 672 673 674 675
static void Redraw( Widget w, XtPointer closure, XEvent *event )
{
    VlcPlugin* p_plugin = (VlcPlugin*)closure;
    GC gc;
    XGCValues gcv;
676

677 678
    gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
    gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
679

680 681
    XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
                    0, 0, p_plugin->i_width, p_plugin->i_height );
682

683 684
    gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
    XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
685

686 687 688
    XDrawString( p_plugin->p_display, p_plugin->window, gc,
                 p_plugin->i_width / 2 - 40, p_plugin->i_height / 2,
                 WINDOW_TEXT, strlen(WINDOW_TEXT) );
689

690
    XFreeGC( p_plugin->p_display, gc );
691 692 693
}
#endif