vlcshell.cpp 28.2 KB
Newer Older
1
/*****************************************************************************
2
 * vlcshell.cpp: a VLC plugin for Mozilla
3
 *****************************************************************************
4
 * Copyright (C) 2002-2005 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23
 *****************************************************************************/

24 25 26
/* XXX: disable VLC here */
#define USE_LIBVLC 1

27 28 29
/*****************************************************************************
 * Preamble
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
30 31
#include "config.h"

32 33
#include <stdio.h>
#include <string.h>
34
#include <stdlib.h>
35 36

/* vlc stuff */
37 38 39
#ifdef USE_LIBVLC
#   include <vlc/vlc.h>
#endif
40

41
/* Mozilla stuff */
Sam Hocevar's avatar
Sam Hocevar committed
42 43 44
#ifdef HAVE_MOZILLA_CONFIG_H
#   include <mozilla-config.h>
#endif
45 46
#include <nsISupports.h>
#include <nsMemory.h>
47
#include <npapi.h>
48 49

/* This is from mozilla java, do we really need it? */
50
#if 0
Sam Hocevar's avatar
Sam Hocevar committed
51
#include <jri.h>
52
#endif
Sam Hocevar's avatar
Sam Hocevar committed
53 54 55 56 57 58

#if !defined(XP_MACOSX) && !defined(XP_UNIX) && !defined(XP_WIN)
#define XP_UNIX 1
#elif defined(XP_MACOSX)
#undef XP_UNIX
#endif
59

60 61 62
#ifdef XP_WIN
    /* Windows stuff */
#endif
63

64
#ifdef XP_MACOSX
65
    /* Mac OS X stuff */
66
#   include <Quickdraw.h>
67 68
#endif

Sam Hocevar's avatar
Sam Hocevar committed
69 70 71 72 73 74 75
#ifdef XP_UNIX
    /* X11 stuff */
#   include <X11/Xlib.h>
#   include <X11/Intrinsic.h>
#   include <X11/StringDefs.h>
#endif

76 77 78
#include "vlcpeer.h"
#include "vlcplugin.h"

79
#if USE_LIBVLC
Gildas Bazin's avatar
 
Gildas Bazin committed
80
#   define WINDOW_TEXT "(no picture)"
81
#else
Gildas Bazin's avatar
 
Gildas Bazin committed
82
#   define WINDOW_TEXT "(no libvlc)"
83 84
#endif

85 86 87
/* Enable/disable debugging printf's for X11 resizing */
#undef X11_RESIZE_DEBUG

88
/*****************************************************************************
89
 * Unix-only declarations
90
******************************************************************************/
91 92
#ifdef XP_UNIX
#   define VOUT_PLUGINS "xvideo,x11,dummy"
93
#   define AOUT_PLUGINS "alsa,oss,dummy"
94

Clément Stenac's avatar
Clément Stenac committed
95 96
static unsigned int i_previous_height = 100000;
static unsigned int i_previous_width = 100000;
97

98
static void Redraw( Widget w, XtPointer closure, XEvent *event );
99
static void Resize( Widget w, XtPointer closure, XEvent *event );
100 101
#endif

102 103 104 105 106 107 108 109 110
/*****************************************************************************
 * MacOS-only declarations
******************************************************************************/
#ifdef XP_MACOSX
#   define VOUT_PLUGINS "macosx"
#   define AOUT_PLUGINS "macosx"

#endif

111
/*****************************************************************************
112 113
 * Windows-only declarations
 *****************************************************************************/
114
#ifdef XP_WIN
115 116
#   define VOUT_PLUGINS "directx,wingdi,dummy"
#   define AOUT_PLUGINS "directx,waveout,dummy"
117

Gildas Bazin's avatar
 
Gildas Bazin committed
118
#if defined(XP_WIN) && !USE_LIBVLC
119
LRESULT CALLBACK Manage( HWND, UINT, WPARAM, LPARAM );
120
#endif
Gildas Bazin's avatar
 
Gildas Bazin committed
121
#endif
122 123 124 125 126 127 128 129 130 131 132

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

NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
{
133

134
    static nsIID nsid = VLCINTF_IID;
Sam Hocevar's avatar
Sam Hocevar committed
135
    static char psz_desc[1000];
136 137 138 139 140 141 142 143

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

        case NPPVpluginDescriptionString:
144
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
145
            snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, VLC_Version() );
146
#else /* USE_LIBVLC */
Gildas Bazin's avatar
 
Gildas Bazin committed
147
            snprintf( psz_desc, 1000-1, PLUGIN_DESCRIPTION, "(disabled)" );
148
#endif /* USE_LIBVLC */
Sam Hocevar's avatar
Sam Hocevar committed
149 150
            psz_desc[1000-1] = 0;
            *((char **)value) = psz_desc;
151
            return NPERR_NO_ERROR;
Sam Hocevar's avatar
Sam Hocevar committed
152 153 154 155

        default:
            /* go on... */
            break;
156 157 158 159 160 161 162 163 164 165 166 167
    }

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

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

    switch( variable )
    {
        case NPPVpluginScriptableInstance:
168
            *(nsISupports**)value = p_plugin->GetPeer();
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            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;
}

191 192 193
/******************************************************************************
 * Mac-only API calls
 *****************************************************************************/
194
#ifdef XP_MACOSX
195 196
int16 NPP_HandleEvent( NPP instance, void * event )
{
197 198 199
    VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
    vlc_value_t value;

200 201 202 203 204
    if( instance == NULL )
    {
        return false;
    }

205
    EventRecord *pouetEvent = (EventRecord*)event;
206

207
    if (pouetEvent->what == 6)
208
    {
209
        value.i_int = 1;
210
        VLC_VariableSet( p_plugin->i_vlc, "drawableredraw", value );
211
        return true;
212
    }
213 214

    Boolean eventHandled = false;
215 216 217

    return eventHandled;
}
218
#endif /* XP_MACOSX */
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
/******************************************************************************
 * 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 )
{
241
    int i;
Gildas Bazin's avatar
 
Gildas Bazin committed
242

243
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
244
    vlc_value_t value;
245 246
    int i_ret;

247
#endif /* USE_LIBVLC */
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

    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;

263 264 265
#ifdef XP_WIN
    p_plugin->p_hwnd = NULL;
    p_plugin->pf_wndproc = NULL;
266
#endif /* XP_WIN */
267

268
#ifdef XP_UNIX
269
    p_plugin->window = 0;
270
    p_plugin->p_display = NULL;
271
#endif /* XP_UNIX */
272

273 274 275 276 277 278
    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
279 280
    p_plugin->i_vlc = VLC_Create();
    if( p_plugin->i_vlc < 0 )
281
    {
Sam Hocevar's avatar
Sam Hocevar committed
282
        p_plugin->i_vlc = 0;
283 284 285 286 287
        delete p_plugin;
        p_plugin = NULL;
        return NPERR_GENERIC_ERROR;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
288 289 290 291 292 293 294 295 296 297
    {
#ifdef XP_MACOSX
        char *home_user;
        char *directory;
        char *plugin_path;
        char *ppsz_argv[] = { "vlc", "--plugin-path", NULL };

        home_user = strdup( getenv("HOME") );
        directory = strdup( "/Library/Internet Plug-Ins/VLC Plugin.plugin/"
                            "Contents/MacOS/modules" );
298
        plugin_path = (char *)malloc( strlen( directory ) + strlen( home_user ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
299 300 301 302 303 304 305
        memcpy( plugin_path , home_user , strlen(home_user) );
        memcpy( plugin_path + strlen( home_user ) , directory ,
                strlen( directory ) );

        ppsz_argv[2] = plugin_path;

#elif defined(XP_WIN)
Gildas Bazin's avatar
 
Gildas Bazin committed
306
        char *ppsz_argv[] = { NULL, "-vv" };
Gildas Bazin's avatar
 
Gildas Bazin committed
307
        HKEY h_key;
Gildas Bazin's avatar
 
Gildas Bazin committed
308
        DWORD i_type, i_data = MAX_PATH + 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
309 310 311 312 313 314 315 316 317
        char p_data[MAX_PATH + 1];
        if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
                          0, KEY_READ, &h_key ) == ERROR_SUCCESS )
        {
             if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
                                  (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
             {
                 if( i_type == REG_SZ )
                 {
Gildas Bazin's avatar
 
Gildas Bazin committed
318 319
                     strcat( p_data, "\\vlc" );
                     ppsz_argv[0] = p_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
320 321 322 323 324
                 }
             }
             RegCloseKey( h_key );
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
325
        if( !ppsz_argv[0] ) ppsz_argv[0] = "vlc";
Gildas Bazin's avatar
 
Gildas Bazin committed
326

327
#else /* XP_MACOSX */
Gildas Bazin's avatar
 
Gildas Bazin committed
328 329 330 331 332 333
        char *ppsz_argv[] =
        {
            "vlc"
            /*, "--plugin-path", "/home/sam/videolan/vlc_MAIN/plugins"*/
        };

334
#endif /* XP_MACOSX */
Gildas Bazin's avatar
 
Gildas Bazin committed
335

336 337 338 339 340 341
        /* HACK: special case for loop, to have it set before playlist startup
         */
        for( i = 0; i < argc ; i++ )
        {
            if( !strcmp( argn[i], "loop" ) )
            {
Antoine Cellerier's avatar
Antoine Cellerier committed
342
                if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "yes" ) )
343 344 345 346 347 348 349
                {
                    value.b_bool = VLC_TRUE;
                    VLC_VariableSet( p_plugin->i_vlc, "conf::loop", value );
                }
            }
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
350 351 352 353 354 355 356
        i_ret = VLC_Init( p_plugin->i_vlc, sizeof(ppsz_argv)/sizeof(char*),
                          ppsz_argv );

#ifdef XP_MACOSX
        free( home_user );
        free( directory );
        free( plugin_path );
357
#endif /* XP_MACOSX */
Gildas Bazin's avatar
 
Gildas Bazin committed
358 359
    }

360 361
    if( i_ret )
    {
Sam Hocevar's avatar
Sam Hocevar committed
362 363
        VLC_Destroy( p_plugin->i_vlc );
        p_plugin->i_vlc = 0;
364 365 366 367 368
        delete p_plugin;
        p_plugin = NULL;
        return NPERR_GENERIC_ERROR;
    }

Sam Hocevar's avatar
Sam Hocevar committed
369
    value.psz_string = "dummy";
370
    VLC_VariableSet( p_plugin->i_vlc, "conf::intf", value );
371
    value.psz_string = VOUT_PLUGINS;
372
    VLC_VariableSet( p_plugin->i_vlc, "conf::vout", value );
373
    value.psz_string = AOUT_PLUGINS;
374
    VLC_VariableSet( p_plugin->i_vlc, "conf::aout", value );
375

376
#else /* USE_LIBVLC */
377 378 379 380 381 382
    p_plugin->i_vlc = 1;

#endif /* USE_LIBVLC */

    p_plugin->b_stream = VLC_FALSE;
    p_plugin->b_autoplay = VLC_FALSE;
383 384 385 386
    p_plugin->psz_target = NULL;

    for( i = 0; i < argc ; i++ )
    {
387
        if( !strcmp( argn[i], "target" ) )
388
        {
389
            p_plugin->psz_target = argv[i];
390
        }
391
        else if( !strcmp( argn[i], "autoplay" ) )
392
        {
Antoine Cellerier's avatar
Antoine Cellerier committed
393
            if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "yes" ) )
394 395 396 397
            {
                p_plugin->b_autoplay = 1;
            }
        }
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        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];
        }
413

414
#if USE_LIBVLC
415 416
        else if( !strcmp( argn[i], "fullscreen" ) )
        {
Antoine Cellerier's avatar
Antoine Cellerier committed
417
            if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "yes" ) )
418 419
            {
                value.b_bool = VLC_TRUE;
420
                VLC_VariableSet( p_plugin->i_vlc, "conf::fullscreen", value );
421 422
            }
        }
423 424
        else if( !strcmp( argn[i], "mute" ) )
        {
Antoine Cellerier's avatar
Antoine Cellerier committed
425
            if( !strcmp( argv[i], "1" ) || !strcmp( argv[i], "yes" ) )
426 427 428 429
            {
                VLC_VolumeMute( p_plugin->i_vlc );
            }
        }
430
#endif /* USE_LIBVLC */
431 432
    }

433 434 435 436 437
    if( p_plugin->psz_target )
    {
        p_plugin->psz_target = strdup( p_plugin->psz_target );
    }

438 439 440
    return NPERR_NO_ERROR;
}

441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
#ifdef XP_WIN
/* This is really ugly but there is a deadlock when stopping a stream
 * (in VLC_CleanUp()) because the video output is a child of the drawable but
 * is in a different thread. */
static void HackStopVout( VlcPlugin* p_plugin )
{
    MSG msg;
    HWND hwnd;
    vlc_value_t value;

    VLC_VariableGet( p_plugin->i_vlc, "drawable", &value );

    hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 );
    if( !hwnd ) return;

    PostMessage( hwnd, WM_CLOSE, 0, 0 );

    do
    {
        while( PeekMessage( &msg, (HWND)value.i_int, 0, 0, PM_REMOVE ) )
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        if( FindWindowEx( (HWND)value.i_int, 0, 0, 0 ) ) Sleep( 10 );
    }
    while( (hwnd = FindWindowEx( (HWND)value.i_int, 0, 0, 0 )) );
}
469
#endif /* XP_WIN */
470

471 472 473 474 475 476 477 478 479 480 481
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
482
        if( p_plugin->i_vlc )
483
        {
484
#if USE_LIBVLC
485 486
#   ifdef XP_WIN
            HackStopVout( p_plugin );
487
#   endif /* XP_WIN */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
488
            VLC_CleanUp( p_plugin->i_vlc );
Sam Hocevar's avatar
Sam Hocevar committed
489
            VLC_Destroy( p_plugin->i_vlc );
490
#endif /* USE_LIBVLC */
Sam Hocevar's avatar
Sam Hocevar committed
491
            p_plugin->i_vlc = 0;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        }

        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 )
{
510
    vlc_value_t value;
Simon Latapie's avatar
Simon Latapie committed
511
#ifdef XP_MACOSX
512 513 514 515 516 517 518 519 520 521
    vlc_value_t valuex;
    vlc_value_t valuey;
    vlc_value_t valuew;
    vlc_value_t valueh;
    vlc_value_t valuet;
    vlc_value_t valuel;
    vlc_value_t valueb;
    vlc_value_t valuer;
    vlc_value_t valueportx;
    vlc_value_t valueporty;
522 523
    Rect black_rect;
    char * text;
524
#endif /* XP_MACOSX */
525

526 527 528 529 530 531 532 533
    if( instance == NULL )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

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

    /* Write the window ID for vlc */
534 535
#if USE_LIBVLC

536
#ifdef XP_MACOSX
537
    value.i_int = (int)(((NP_Port*) (window->window))->port);
538
    VLC_VariableSet( p_plugin->i_vlc, "drawable", value );
539 540 541

    valueportx.i_int = ((NP_Port*) (window->window))->portx;
    valueporty.i_int = ((NP_Port*) (window->window))->porty;
542 543
    VLC_VariableSet( p_plugin->i_vlc, "drawableportx", valueportx );
    VLC_VariableSet( p_plugin->i_vlc, "drawableporty", valueporty );
544 545 546 547 548 549 550 551 552 553

    valuex.i_int = window->x;
    valuey.i_int = window->y;
    valuew.i_int = window->width;
    valueh.i_int = window->height;
    valuet.i_int = window->clipRect.top;
    valuel.i_int = window->clipRect.left;
    valueb.i_int = window->clipRect.bottom;
    valuer.i_int = window->clipRect.right;

554 555 556
    VLC_VariableSet( p_plugin->i_vlc, "drawablet", valuet );
    VLC_VariableSet( p_plugin->i_vlc, "drawablel", valuel );
    VLC_VariableSet( p_plugin->i_vlc, "drawableb", valueb );
557
    VLC_VariableSet( p_plugin->i_vlc, "drawabler", valuer );
558 559 560 561
    VLC_VariableSet( p_plugin->i_vlc, "drawablex", valuex );
    VLC_VariableSet( p_plugin->i_vlc, "drawabley", valuey );
    VLC_VariableSet( p_plugin->i_vlc, "drawablew", valuew );
    VLC_VariableSet( p_plugin->i_vlc, "drawableh", valueh );
562 563 564

    p_plugin->window = window;

565 566 567 568 569 570 571
    /* draw the beautiful "No Picture" */

    black_rect.top = valuet.i_int - valuey.i_int;
    black_rect.left = valuel.i_int - valuex.i_int;
    black_rect.bottom = valueb.i_int - valuey.i_int;
    black_rect.right = valuer.i_int - valuex.i_int;

572
    SetPort( (GrafPtr)value.i_int );
573 574 575 576 577 578 579 580 581
    SetOrigin( valueportx.i_int , valueporty.i_int );
    ForeColor(blackColor);
    PenMode( patCopy );
    PaintRect( &black_rect );

    ForeColor(whiteColor);
    text = strdup( WINDOW_TEXT );
    MoveTo( valuew.i_int / 2 - 40 , valueh.i_int / 2 );
    DrawText( text , 0 , strlen(text) );
582
    free(text);
583

584
#else /* XP_MACOSX */
585
    /* FIXME: this cast sucks */
586
    value.i_int = (int) (ptrdiff_t) (void *) window->window;
587
    VLC_VariableSet( p_plugin->i_vlc, "drawable", value );
588
#endif /* XP_MACOSX */
589

590
#endif /* USE_LIBVLC */
591

592 593 594 595 596 597 598 599
    /*
     * 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.
     */

600 601 602 603 604 605
#ifdef XP_WIN
    if( !window || !window->window )
    {
        /* Window was destroyed. Invalidate everything. */
        if( p_plugin->p_npwin )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
606
#if !USE_LIBVLC
607 608
            SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
                           (LONG)p_plugin->pf_wndproc );
609
#endif /* !USE_LIBVLC */
610 611 612
            p_plugin->pf_wndproc = NULL;
            p_plugin->p_hwnd = NULL;
        }
613

614 615 616
        p_plugin->p_npwin = window;
        return NPERR_NO_ERROR;
    }
617

618 619 620 621 622 623 624 625 626
    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 */
            p_plugin->i_width = window->width;
            p_plugin->i_height = window->height;
            p_plugin->p_npwin = window;
Gildas Bazin's avatar
 
Gildas Bazin committed
627 628
#if !USE_LIBVLC
            InvalidateRect( p_plugin->p_hwnd, NULL, TRUE );
629
            UpdateWindow( p_plugin->p_hwnd );
630
#endif /* !USE_LIBVLC */
631 632 633 634 635
            return NPERR_NO_ERROR;
        }

        /* Window has changed. Destroy the one we have, and go
         * on as if it was a real initialization. */
Gildas Bazin's avatar
 
Gildas Bazin committed
636
#if !USE_LIBVLC
637 638
        SetWindowLong( p_plugin->p_hwnd, GWL_WNDPROC,
                       (LONG)p_plugin->pf_wndproc );
639
#endif /* !USE_LIBVLC */
640 641 642 643
        p_plugin->pf_wndproc = NULL;
        p_plugin->p_hwnd = NULL;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
644
#if !USE_LIBVLC
645 646
    p_plugin->pf_wndproc = (WNDPROC)SetWindowLong( (HWND)window->window,
                                                   GWL_WNDPROC, (LONG)Manage );
647
#endif /* !USE_LIBVLC */
Gildas Bazin's avatar
 
Gildas Bazin committed
648

649 650 651 652
    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 );
653 654 655 656 657
#endif /* XP_WIN */

    p_plugin->i_width = window->width;
    p_plugin->i_height = window->height;
    p_plugin->p_npwin = window;
658 659

#ifdef XP_UNIX
660
    p_plugin->window = (Window) window->window;
661 662
    p_plugin->p_display =
        ((NPSetWindowCallbackStruct *)window->ws_info)->display;
663

664 665
    XResizeWindow( p_plugin->p_display, p_plugin->window,
                   p_plugin->i_width, p_plugin->i_height );
666
    Widget w = XtWindowToWidget( p_plugin->p_display, p_plugin->window );
667

668 669
    XtAddEventHandler( w, ExposureMask, FALSE,
                       (XtEventHandler)Redraw, p_plugin );
670 671
    XtAddEventHandler( w, StructureNotifyMask, FALSE,
                       (XtEventHandler)Resize, p_plugin );
672
    Redraw( w, (XtPointer)p_plugin, NULL );
673
#endif /* XP_UNIX */
674 675 676

    if( !p_plugin->b_stream )
    {
677 678 679 680 681 682 683
        int i_mode = PLAYLIST_APPEND;

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

684 685
        if( p_plugin->psz_target )
        {
686
#if USE_LIBVLC
Sam Hocevar's avatar
Sam Hocevar committed
687
            VLC_AddTarget( p_plugin->i_vlc, p_plugin->psz_target,
688
                           0, 0, PLAYLIST_INSERT, 0 );
689 690
#endif
            p_plugin->b_stream = VLC_TRUE;
691 692 693 694 695 696 697 698 699 700 701 702 703 704
        }
    }

    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;
    }

705
#if 0
706
    VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
707
#endif
708

709
    /* fprintf(stderr, "NPP_NewStream - FILE mode !!\n"); */
710 711 712 713 714

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

#if 0
715
    if( !p_plugin->b_stream )
716 717
    {
        p_plugin->psz_target = strdup( stream->url );
718
        p_plugin->b_stream = VLC_TRUE;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    }
#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;

735
    /* fprintf(stderr, "NPP_WriteReady\n"); */
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

    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 )
{
754
    /* fprintf(stderr, "NPP_Write %i\n", (int)len); */
755

756
    if( instance != NULL )
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
    {
        /*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;
    }

783
    /* fprintf(stderr, "NPP_StreamAsFile %s\n", fname); */
784 785

#if USE_LIBVLC
786
    VlcPlugin* p_plugin = (VlcPlugin*)instance->pdata;
787

Gildas Bazin's avatar
 
Gildas Bazin committed
788
    VLC_AddTarget( p_plugin->i_vlc, fname, 0, 0,
789
                   PLAYLIST_APPEND | PLAYLIST_GO, PLAYLIST_END );
790
#endif /* USE_LIBVLC */
791
}
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806


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 )
{
807 808
    if( printInfo == NULL )
    {
809
        return;
810
    }
811

812 813 814
    if( instance != NULL )
    {
        /***** Insert NPP_Print code here *****\
815
        PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
816
        \**************************************/
817

818 819
        if( printInfo->mode == NP_FULL )
        {
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
            /*
             * 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.
             */

837
            /***** Insert NPP_Print code here *****\
838 839 840 841
            void* platformPrint =
                printInfo->print.fullPrint.platformPrint;
            NPBool printOne =
                printInfo->print.fullPrint.printOne;
842
            \**************************************/
843 844 845 846

            /* Do the default*/
            printInfo->print.fullPrint.pluginPrinted = FALSE;
        }
847 848 849
        else
        {
            /* If not fullscreen, we must be embedded */
850 851 852 853 854 855 856 857 858 859 860 861
            /*
             * 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.
             */

862
            /***** Insert NPP_Print code here *****\
863 864 865 866
            NPWindow* printWindow =
                &(printInfo->print.embedPrint.window);
            void* platformPrint =
                printInfo->print.embedPrint.platformPrint;
867
            \**************************************/
868 869 870 871
        }
    }
}

872 873 874
/******************************************************************************
 * Windows-only methods
 *****************************************************************************/
Gildas Bazin's avatar
 
Gildas Bazin committed
875
#if defined(XP_WIN) && !USE_LIBVLC
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
{
    VlcPlugin* p_plugin = (VlcPlugin*) GetProp( p_hwnd, "w00t" );

    switch( i_msg )
    {
        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;
        }
        default:
            p_plugin->pf_wndproc( p_hwnd, i_msg, wpar, lpar );
            break;
    }
    return 0;
}
904
#endif /* XP_WIN */
905

906 907 908
/******************************************************************************
 * UNIX-only methods
 *****************************************************************************/
909
#ifdef XP_UNIX
910 911 912 913 914
static void Redraw( Widget w, XtPointer closure, XEvent *event )
{
    VlcPlugin* p_plugin = (VlcPlugin*)closure;
    GC gc;
    XGCValues gcv;
915

916 917
    gcv.foreground = BlackPixel( p_plugin->p_display, 0 );
    gc = XCreateGC( p_plugin->p_display, p_plugin->window, GCForeground, &gcv );
918

919 920
    XFillRectangle( p_plugin->p_display, p_plugin->window, gc,
                    0, 0, p_plugin->i_width, p_plugin->i_height );
921

922 923
    gcv.foreground = WhitePixel( p_plugin->p_display, 0 );
    XChangeGC( p_plugin->p_display, gc, GCForeground, &gcv );
924

925 926 927
    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) );
928

929
    XFreeGC( p_plugin->p_display, gc );
930
}
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

static void Resize ( Widget w, XtPointer closure, XEvent *event )
{
    VlcPlugin* p_plugin = (VlcPlugin*)closure;
    int i_ret;
    Window root_return, parent_return, * children_return;
    Window base_window;
    unsigned int i_nchildren;

#ifdef X11_RESIZE_DEBUG
    XWindowAttributes attr;

    if( event && event->type == ConfigureNotify )
    {
        fprintf( stderr, "vlcshell::Resize() ConfigureNotify %d x %d, "
                 "send_event ? %s\n", event->xconfigure.width,
                 event->xconfigure.height,
                 event->xconfigure.send_event ? "TRUE" : "FALSE" );
    }
950 951
#endif /* X11_RESIZE_DEBUG */

952 953 954 955 956 957 958 959
    if( p_plugin->i_height == i_previous_height &&
        p_plugin->i_width == i_previous_width )
    {
        return;
    }
    i_previous_height = p_plugin->i_height;
    i_previous_width  = p_plugin->i_width;

960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

    i_ret = XResizeWindow( p_plugin->p_display, p_plugin->window,
            p_plugin->i_width, p_plugin->i_height );

#ifdef X11_RESIZE_DEBUG
    fprintf( stderr,
             "vlcshell::Resize() XResizeWindow(owner) returned %d\n", i_ret );

    XGetWindowAttributes ( p_plugin->p_display, p_plugin->window, &attr );

    /* X is asynchronous, so the current size reported here is not
       necessarily the requested size as the Resize request may not
       yet have been handled by the plugin host */
    fprintf( stderr, "vlcshell::Resize() current (owner) size %d x %d\n",
             attr.width, attr.height );
#endif /* X11_RESIZE_DEBUG */

    XQueryTree( p_plugin->p_display, p_plugin->window,
                &root_return, &parent_return, &children_return,
                &i_nchildren );

    if( i_nchildren > 0 )
    {
        /* XXX: Make assumptions related to the window parenting structure in
           vlc/modules/video_output/x11/xcommon.c */
        base_window = children_return[i_nchildren - 1];

#ifdef X11_RESIZE_DEBUG
        fprintf( stderr, "vlcshell::Resize() got %d children\n", i_nchildren );
        fprintf( stderr, "vlcshell::Resize() got base_window %p\n",
                 base_window );
#endif /* X11_RESIZE_DEBUG */

        i_ret = XResizeWindow( p_plugin->p_display, base_window,
                p_plugin->i_width, p_plugin->i_height );

#ifdef X11_RESIZE_DEBUG
        fprintf( stderr,
                 "vlcshell::Resize() XResizeWindow(base) returned %d\n",
                 i_ret );

        XGetWindowAttributes( p_plugin->p_display, base_window, &attr );

        fprintf( stderr, "vlcshell::Resize() new size %d x %d\n",
                 attr.width, attr.height );
#endif /* X11_RESIZE_DEBUG */
    }
}

#endif /* XP_UNIX */
1010