vlcshell.cpp 32.5 KB
Newer Older
1
/*****************************************************************************
2
 * vlcshell.cpp: a VLC plugin for Mozilla
3
 *****************************************************************************
4
 * Copyright (C) 2002-2008 the VideoLAN team
5
 * $Id$
6
7
 *
 * Authors: Samuel Hocevar <sam@zoy.org>
8
 *          Jean-Paul Saman <jpsaman@videolan.org>
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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
dionoea's avatar
dionoea committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
25
26
27
 *****************************************************************************/

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

30
31
#include <stdio.h>
#include <string.h>
32
#include <stdlib.h>
33

34
35
36
37
38
/* Mozilla stuff */
#ifdef HAVE_MOZILLA_CONFIG_H
#   include <mozilla-config.h>
#endif

39
/* This is from mozilla java, do we really need it? */
40
#if 0
Sam Hocevar's avatar
Sam Hocevar committed
41
#include <jri.h>
42
#endif
Sam Hocevar's avatar
Sam Hocevar committed
43

44
#include "vlcplugin.h"
45

46
47
48
/* Enable/disable debugging printf's for X11 resizing */
#undef X11_RESIZE_DEBUG

49
#define WINDOW_TEXT "Video is loading..."
50

51
/*****************************************************************************
52
 * Unix-only declarations
53
******************************************************************************/
54
55
#ifdef XP_UNIX

56
static void Redraw( Widget w, XtPointer closure, XEvent *event );
57
static void ControlHandler( Widget w, XtPointer closure, XEvent *event );
58
static void Resize( Widget w, XtPointer closure, XEvent *event );
damienf's avatar
damienf committed
59

60
61
#endif

62
63
64
65
66
67
/*****************************************************************************
 * MacOS-only declarations
******************************************************************************/
#ifdef XP_MACOSX
#endif

68
/*****************************************************************************
69
70
 * Windows-only declarations
 *****************************************************************************/
71
72
#ifdef XP_WIN

damienf's avatar
damienf committed
73
74
static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar );

gbazin's avatar
   
gbazin committed
75
#endif
76
77
78
79
80
81
82
83
84
85
86

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

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

Sam Hocevar's avatar
Sam Hocevar committed
88
    static char psz_desc[1000];
89

damienf's avatar
damienf committed
90
    /* plugin class variables */
91
92
93
94
95
96
97
    switch( variable )
    {
        case NPPVpluginNameString:
            *((char **)value) = PLUGIN_NAME;
            return NPERR_NO_ERROR;

        case NPPVpluginDescriptionString:
98
            snprintf( psz_desc, sizeof(psz_desc), PLUGIN_DESCRIPTION,
99
                      libvlc_get_version() );
Sam Hocevar's avatar
Sam Hocevar committed
100
            *((char **)value) = psz_desc;
101
            return NPERR_NO_ERROR;
Sam Hocevar's avatar
Sam Hocevar committed
102
103

        default:
damienf's avatar
damienf committed
104
            /* move on to instance variables ... */
105
            ;
106
107
108
109
110
111
112
    }

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

damienf's avatar
damienf committed
113
    /* plugin instance variables */
114

damienf's avatar
damienf committed
115
116
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
    if( NULL == p_plugin )
117
    {
damienf's avatar
damienf committed
118
119
120
        // plugin has not been initialized yet !
        return NPERR_INVALID_INSTANCE_ERROR;
    }
121

damienf's avatar
damienf committed
122
123
    switch( variable )
    {
124
        case NPPVpluginScriptableNPObject:
damienf's avatar
damienf committed
125
        {
126
127
128
            /* retrieve plugin root class */
            NPClass *scriptClass = p_plugin->getScriptClass();
            if( scriptClass )
129
            {
130
131
132
                /* create an instance and return it */
                *(NPObject**)value = NPN_CreateObject(instance, scriptClass);
                return NPERR_NO_ERROR;
133
134
            }
            break;
damienf's avatar
damienf committed
135
        }
136

137
        default:
138
            ;
139
    }
140
    return NPERR_GENERIC_ERROR;
141
142
}

143
144
145
146
147
148
149
150
151
152
/*
 * there is some confusion in gecko headers regarding definition of this API
 * NPPVariable is wrongly defined as NPNVariable, which sounds incorrect.
 */

NPError NPP_SetValue( NPP instance, NPNVariable variable, void *value )
{
    return NPERR_GENERIC_ERROR;
}

153
154
155
/******************************************************************************
 * Mac-only API calls
 *****************************************************************************/
156
#ifdef XP_MACOSX
157
158
int16 NPP_HandleEvent( NPP instance, void * event )
{
damienf's avatar
damienf committed
159
160
    static UInt32 lastMouseUp = 0;

161
162
163
164
165
    if( instance == NULL )
    {
        return false;
    }

166
    VlcPlugin *p_plugin = (VlcPlugin*)instance->pdata;
167
168
169
170
171
172

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

173
    EventRecord *myEvent = (EventRecord*)event;
174

175
    switch( myEvent->what )
176
    {
177
        case nullEvent:
178
            return true;
179
        case mouseDown:
damienf's avatar
damienf committed
180
181
182
183
184
185
186
        {
            if( (myEvent->when - lastMouseUp) < GetDblTime() )
            {
                /* double click */
                libvlc_instance_t *p_vlc = p_plugin->getVLC();
                if( p_vlc )
                {
187
188
189
190
191
                    int is_playing;
                    libvlc_playlist_lock(p_vlc);
                    is_playing = libvlc_playlist_isplaying(p_vlc, NULL);
                    libvlc_playlist_lunock(p_vlc);
                    if( is_playing )
damienf's avatar
damienf committed
192
                    {
193
194
                        libvlc_media_player_t *p_md =
                            libvlc_playlist_get_media_player(p_vlc, NULL);
195
                        if( p_md )
damienf's avatar
damienf committed
196
                        {
197
                            libvlc_toggle_fullscreen(p_md, NULL);
198
                            libvlc_media_player_release(p_md);
damienf's avatar
damienf committed
199
200
201
202
203
204
                        }
                    }
                }
            }
            return true;
        }
205
        case mouseUp:
damienf's avatar
damienf committed
206
            lastMouseUp = myEvent->when;
207
208
209
210
211
212
213
            return true;
        case keyUp:
        case keyDown:
        case autoKey:
            return true;
        case updateEvt:
        {
214
215
            const NPWindow& npwindow = p_plugin->getWindow();
            if( npwindow.window )
damienf's avatar
damienf committed
216
            {
217
218
219
220
                int hasVout = FALSE;
                libvlc_instance_t *p_vlc = p_plugin->getVLC();

                if( p_vlc )
damienf's avatar
damienf committed
221
                {
222
223
224
225
226
                    int is_playing;
                    libvlc_playlist_lock(p_vlc);
                    is_playing = libvlc_playlist_isplaying(p_vlc, NULL);
                    libvlc_playlist_unlock(p_vlc);
                    if( is_playing )
damienf's avatar
damienf committed
227
                    {
228
229
                        libvlc_media_player_t *p_md =
                            libvlc_playlist_get_media_player(p_vlc, NULL);
230
                        if( p_md )
231
                        {
232
                            hasVout = libvlc_media_player_has_vout(p_md,
233
                                                                     NULL);
234
235
236
237
238
239
240
                            if( hasVout )
                            {
                                libvlc_rectangle_t area;
                                area.left = 0;
                                area.top = 0;
                                area.right = npwindow.width;
                                area.bottom = npwindow.height;
241
242
                                libvlc_video_redraw_rectangle(p_md, &area,
                                                              NULL);
243
                            }
244
                            libvlc_media_player_release(p_md);
245
                        }
damienf's avatar
damienf committed
246
247
                    }
                }
248

249
250
251
                if( ! hasVout )
                {
                    /* draw the beautiful "No Picture" */
damienf's avatar
damienf committed
252

253
254
                    ForeColor(blackColor);
                    PenMode( patCopy );
damienf's avatar
damienf committed
255

256
257
                    /* seems that firefox forgets to set the following
                     * on occasion (reload) */
258
259
                    SetOrigin(((NP_Port *)npwindow.window)->portx,
                              ((NP_Port *)npwindow.window)->porty);
260

261
262
263
264
265
266
                    Rect rect;
                    rect.left = 0;
                    rect.top = 0;
                    rect.right = npwindow.width;
                    rect.bottom = npwindow.height;
                    PaintRect( &rect );
damienf's avatar
damienf committed
267

268
269
270
271
                    ForeColor(whiteColor);
                    MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
                    DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
                }
damienf's avatar
damienf committed
272
            }
273
274
275
276
277
278
279
280
281
            return true;
        }
        case activateEvt:
            return false;
        case NPEventType_GetFocusEvent:
        case NPEventType_LoseFocusEvent:
            return true;
        case NPEventType_AdjustCursorEvent:
            return false;
282
283
284
285
286
        case NPEventType_MenuCommandEvent:
            return false;
        case NPEventType_ClippingChangedEvent:
            return false;
        case NPEventType_ScrollingBeginsEvent:
287
            return true;
288
        case NPEventType_ScrollingEndsEvent:
289
290
291
292
293
            return true;
        default:
            ;
    }
    return false;
294
}
295
#endif /* XP_MACOSX */
296

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/******************************************************************************
 * 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 )
{
damienf's avatar
damienf committed
318
    NPError status;
319
320
321
322
323
324

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

damienf's avatar
damienf committed
325
326
    VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
    if( NULL == p_plugin )
327
328
329
330
    {
        return NPERR_OUT_OF_MEMORY_ERROR;
    }

damienf's avatar
damienf committed
331
    status = p_plugin->init(argc, argn, argv);
332
333
    if( NPERR_NO_ERROR == status )
    {
damienf's avatar
damienf committed
334
        instance->pdata = reinterpret_cast<void*>(p_plugin);
335
336
#if 0
        NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
337
        NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
338
#endif
gbazin's avatar
   
gbazin committed
339
    }
340
341
    else
    {
342
343
        delete p_plugin;
    }
damienf's avatar
damienf committed
344
    return status;
345
346
}

347
348
NPError NPP_Destroy( NPP instance, NPSavedData** save )
{
damienf's avatar
damienf committed
349
    if( NULL == instance )
350
351
        return NPERR_INVALID_INSTANCE_ERROR;

damienf's avatar
damienf committed
352
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
damienf's avatar
damienf committed
353
354
355
356
    if( NULL == p_plugin )
        return NPERR_NO_ERROR;

    instance->pdata = NULL;
357

358
#if XP_WIN
359
    HWND win = (HWND)p_plugin->getWindow().window;
360
361
362
363
364
365
366
367
    WNDPROC winproc = p_plugin->getWindowProc();
    if( winproc )
    {
        /* reset WNDPROC */
        SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
    }
#endif

368
    delete p_plugin;
369
370
371
372
373
374

    return NPERR_NO_ERROR;
}

NPError NPP_SetWindow( NPP instance, NPWindow* window )
{
375
#if defined(XP_UNIX) && !defined(__APPLE__)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
376
377
    Window control;
    unsigned int i_control_height = 0, i_control_width = 0;
378
#endif
379

damienf's avatar
damienf committed
380
    if( ! instance )
381
382
383
384
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

damienf's avatar
damienf committed
385
386
    /* NPP_SetWindow may be called before NPP_New (Opera) */
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
387
    if( NULL == p_plugin )
damienf's avatar
damienf committed
388
389
390
391
    {
        /* we should probably show a splash screen here */
        return NPERR_NO_ERROR;
    }
392
#if defined(XP_UNIX) && !defined(__APPLE__)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
393
    control = p_plugin->getControlWindow();
394
#endif
damienf's avatar
damienf committed
395
    libvlc_instance_t *p_vlc = p_plugin->getVLC();
396

397
398
399
400
401
402
403
404
    /*
     * 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.
     */

405
406
    /* retrieve current window */
    NPWindow& curwin = p_plugin->getWindow();
damienf's avatar
damienf committed
407
408
409

#ifdef XP_MACOSX
    if( window && window->window )
410
    {
damienf's avatar
damienf committed
411
        /* check if plugin has a new parent window */
412
        CGrafPtr drawable = (((NP_Port*) (window->window))->port);
413
        if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
414
        {
damienf's avatar
damienf committed
415
            /* set/change parent window */
416
            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
417
        }
418

damienf's avatar
damienf committed
419
420
421
422
        /* as MacOS X video output is windowless, set viewport */
        libvlc_rectangle_t view, clip;

        /*
423
424
425
        ** browser sets port origin to top-left location of plugin
        ** relative to GrafPort window origin is set relative to document,
        ** which of little use for drawing
damienf's avatar
damienf committed
426
427
428
429
430
431
432
433
434
435
436
437
438
        */
        view.top     = ((NP_Port*) (window->window))->porty;
        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);

439
440
441
442
443
444
445
        /* remember new window */
        p_plugin->setWindow(*window);
    }
    else if( curwin.window ) {
        /* change/set parent */
        libvlc_video_set_parent(p_vlc, 0, NULL);
        curwin.window = NULL;
446
    }
damienf's avatar
damienf committed
447
#endif /* XP_MACOSX */
448

damienf's avatar
damienf committed
449
450
#ifdef XP_WIN
    if( window && window->window )
451
    {
damienf's avatar
damienf committed
452
        /* check if plugin has a new parent window */
453
        HWND drawable = (HWND) (window->window);
454
        if( !curwin.window || drawable != curwin.window )
455
        {
damienf's avatar
damienf committed
456
            /* reset previous window settings */
457
            HWND oldwin = (HWND)p_plugin->getWindow().window;
damienf's avatar
damienf committed
458
459
460
461
462
463
            WNDPROC oldproc = p_plugin->getWindowProc();
            if( oldproc )
            {
                /* reset WNDPROC */
                SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
            }
464
            /* attach our plugin object */
465
466
            SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
                             reinterpret_cast<LONG_PTR>(p_plugin));
467

damienf's avatar
damienf committed
468
            /* install our WNDPROC */
469
            p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
470
                                             GWL_WNDPROC, (LONG)Manage ) );
damienf's avatar
damienf committed
471
472
473
474
475
476
477

            /* change window style to our liking */
            LONG style = GetWindowLong((HWND)drawable, GWL_STYLE);
            style |= WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
            SetWindowLong((HWND)drawable, GWL_STYLE, style);

            /* change/set parent */
478
            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, NULL);
479

480
481
            /* remember new window */
            p_plugin->setWindow(*window);
damienf's avatar
damienf committed
482

483
484
485
486
            /* Redraw window */
            InvalidateRect( (HWND)drawable, NULL, TRUE );
            UpdateWindow( (HWND)drawable );
        }
damienf's avatar
damienf committed
487
    }
damienf's avatar
damienf committed
488
    else if ( curwin.window )
damienf's avatar
damienf committed
489
490
    {
        /* reset WNDPROC */
491
        HWND oldwin = (HWND)curwin.window;
damienf's avatar
damienf committed
492
493
494
495
        SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
        p_plugin->setWindowProc(NULL);
        /* change/set parent */
        libvlc_video_set_parent(p_vlc, 0, NULL);
damienf's avatar
damienf committed
496
        curwin.window = NULL;
497
    }
damienf's avatar
damienf committed
498
499
500
#endif /* XP_WIN */

#ifdef XP_UNIX
501
502
503
504
505
    /* 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;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
506

damienf's avatar
damienf committed
507
508
    if( window && window->window )
    {
509
        Window  parent  = (Window) window->window;
510
        if( !curwin.window || (parent != (Window)curwin.window) )
damienf's avatar
damienf committed
511
        {
512
513
            Display *p_display = ( (NPSetWindowCallbackStruct *)
                                   window->ws_info )->display;
514

515
            XResizeWindow( p_display, parent, window->width, window->height );
gbazin's avatar
   
gbazin committed
516

517
518
            int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));

519
            /* create windows */
520
            Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
521
                           window->width, window->height - i_control_height,
522
523
                           0, i_blackColor, i_blackColor );
            Window controls = (Window) NULL;
524
525
526
527
            controls = XCreateSimpleWindow( p_display, parent,
                            0, window->height - i_control_height-1,
                            window->width, i_control_height-1,
                            0, i_blackColor, i_blackColor );
528
529
530

            XMapWindow( p_display, parent );
            XMapWindow( p_display, video );
531
            if( controls ) { XMapWindow( p_display, controls ); }
532
533
534

            XFlush(p_display);

535
            /* bind events */
536
537
538
539
540
541
542
543
544
545
546
            Widget w = XtWindowToWidget( p_display, parent );

            XtAddEventHandler( w, ExposureMask, FALSE,
                               (XtEventHandler)Redraw, p_plugin );
            XtAddEventHandler( w, StructureNotifyMask, FALSE,
                               (XtEventHandler)Resize, p_plugin );
            XtAddEventHandler( w, ButtonReleaseMask, FALSE,
                               (XtEventHandler)ControlHandler, p_plugin );

            /* callback */
/*
547
            libvlc_media_player_t *p_md;
548
549
550

            libvlc_exception_t ex;
            libvlc_exception_init(& ex );
551
            p_md = libvlc_playlist_get_media_player( p_plugin->getVLC(), &ex );
552
            libvlc_exception_init( &ex );
553
            libvlc_event_attach( libvlc_media_player_event_manager( p_md, &ex ),
554
                                 libvlc_MediaPlayerPositionChanged, Redraw, NULL, &ex );
555
*/
556

damienf's avatar
damienf committed
557
            /* set/change parent window */
558
            libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, NULL );
559

damienf's avatar
damienf committed
560
            /* remember window */
561
562
            p_plugin->setWindow( *window );
            p_plugin->setVideoWindow( video );
563
            if( controls ) { p_plugin->setControlWindow( controls ); }
damienf's avatar
damienf committed
564
565

            Redraw( w, (XtPointer)p_plugin, NULL );
566
567
568
569
570
571

            /* now display toolbar if asked through parameters */
            if( p_plugin->b_toolbar )
            {
                p_plugin->showToolbar();
            }
damienf's avatar
damienf committed
572
        }
damienf's avatar
damienf committed
573
    }
damienf's avatar
damienf committed
574
    else if ( curwin.window )
575
576
577
    {
        /* change/set parent */
        libvlc_video_set_parent(p_vlc, 0, NULL);
damienf's avatar
damienf committed
578
        curwin.window = NULL;
579
    }
580
#endif /* XP_UNIX */
581
582
583
584
585

    if( !p_plugin->b_stream )
    {
        if( p_plugin->psz_target )
        {
586
587
            if( libvlc_playlist_add( p_vlc, p_plugin->psz_target,
                                     NULL, NULL ) != -1 )
damienf's avatar
damienf committed
588
589
590
591
592
593
            {
                if( p_plugin->b_autoplay )
                {
                    libvlc_playlist_play(p_vlc, 0, 0, NULL, NULL);
                }
            }
594
            p_plugin->b_stream = true;
595
596
597
598
599
600
601
602
        }
    }
    return NPERR_NO_ERROR;
}

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

608
    VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
609
610
611
612
    if( NULL == p_plugin )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }
613

614
615
616
617
618
619
620
621
   /*
   ** Firefox/Mozilla may decide to open a stream from the URL specified
   ** in the SRC parameter of the EMBED tag and pass it to us
   **
   ** since VLC will open the SRC URL as well, we're not interested in
   ** that stream. Otherwise, we'll take it and queue it up in the playlist
   */
    if( !p_plugin->psz_target || strcmp(stream->url, p_plugin->psz_target) )
622
    {
623
624
625
        /* TODO: use pipes !!!! */
        *stype = NP_ASFILEONLY;
        return NPERR_NO_ERROR;
626
    }
627
    return NPERR_GENERIC_ERROR;
628
629
630
631
}

int32 NPP_WriteReady( NPP instance, NPStream *stream )
{
632
633
    /* TODO */
    return 8*1024;
634
635
636
637
638
639
}


int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
                 int32 len, void *buffer )
{
640
641
    /* TODO */
    return len;
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
}


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

662
    VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
663
664
665
666
    if( NULL == p_plugin )
    {
        return;
    }
667

668
669
    if( libvlc_playlist_add( p_plugin->getVLC(), fname, stream->url, NULL )
        != -1 )
670
671
672
673
674
675
    {
        if( p_plugin->b_autoplay )
        {
            libvlc_playlist_play( p_plugin->getVLC(), 0, 0, NULL, NULL);
        }
    }
676
}
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691


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 )
{
692
693
    if( printInfo == NULL )
    {
694
        return;
695
    }
696

697
698
699
    if( instance != NULL )
    {
        /***** Insert NPP_Print code here *****\
700
        PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
701
        \**************************************/
702

703
704
        if( printInfo->mode == NP_FULL )
        {
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
            /*
             * 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.
             */

722
            /***** Insert NPP_Print code here *****\
723
724
725
726
            void* platformPrint =
                printInfo->print.fullPrint.platformPrint;
            NPBool printOne =
                printInfo->print.fullPrint.printOne;
727
            \**************************************/
728
729
730
731

            /* Do the default*/
            printInfo->print.fullPrint.pluginPrinted = FALSE;
        }
732
733
734
        else
        {
            /* If not fullscreen, we must be embedded */
735
736
737
738
739
740
741
742
743
744
745
746
            /*
             * 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.
             */

747
            /***** Insert NPP_Print code here *****\
748
749
750
751
            NPWindow* printWindow =
                &(printInfo->print.embedPrint.window);
            void* platformPrint =
                printInfo->print.embedPrint.platformPrint;
752
            \**************************************/
753
754
755
756
        }
    }
}

757
758
759
/******************************************************************************
 * Windows-only methods
 *****************************************************************************/
damienf's avatar
damienf committed
760
761
#if XP_WIN
static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
762
{
damienf's avatar
damienf committed
763
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
764
765
766

    switch( i_msg )
    {
damienf's avatar
damienf committed
767
768
769
        case WM_ERASEBKGND:
            return 1L;

770
771
772
773
774
775
776
777
778
        case WM_PAINT:
        {
            PAINTSTRUCT paintstruct;
            HDC hdc;
            RECT rect;

            hdc = BeginPaint( p_hwnd, &paintstruct );

            GetClientRect( p_hwnd, &rect );
damienf's avatar
damienf committed
779
780
781
782

            FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
            SetTextColor(hdc, RGB(255, 255, 255));
            SetBkColor(hdc, RGB(0, 0, 0));
783
784
            DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect,
                      DT_CENTER|DT_VCENTER|DT_SINGLELINE);
785
786

            EndPaint( p_hwnd, &paintstruct );
damienf's avatar
damienf committed
787
            return 0L;
788
789
        }
        default:
damienf's avatar
damienf committed
790
            /* delegate to default handler */
791
792
            return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
                                   i_msg, wpar, lpar );
793
794
    }
}
795
#endif /* XP_WIN */
796

797
798
799
/******************************************************************************
 * UNIX-only methods
 *****************************************************************************/
800
#ifdef XP_UNIX
801
802
static void Redraw( Widget w, XtPointer closure, XEvent *event )
{
damienf's avatar
damienf committed
803
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
804
    Window control = p_plugin->getControlWindow();
805
    const NPWindow& window = p_plugin->getWindow();
806
807
    GC gc;
    XGCValues gcv;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
808
    unsigned int i_control_height, i_control_width;
809

Jean-Paul Saman's avatar
Jean-Paul Saman committed
810
811
    if( p_plugin->b_toolbar )
        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
812
    else
Jean-Paul Saman's avatar
Jean-Paul Saman committed
813
        i_control_height = i_control_width = 0;
814

815
    Window video = p_plugin->getVideoWindow();
damienf's avatar
damienf committed
816
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
damienf's avatar
damienf committed
817
818

    gcv.foreground = BlackPixel( p_display, 0 );
819
    gc = XCreateGC( p_display, video, GCForeground, &gcv );
820

821
    XFillRectangle( p_display, video, gc,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
822
                    0, 0, window.width, window.height - i_control_height);
823

damienf's avatar
damienf committed
824
825
    gcv.foreground = WhitePixel( p_display, 0 );
    XChangeGC( p_display, gc, GCForeground, &gcv );
826

827
    XDrawString( p_display, video, gc,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
828
                 window.width / 2 - 40, (window.height - i_control_height) / 2,
829
                 WINDOW_TEXT, strlen(WINDOW_TEXT) );
830
    XFreeGC( p_display, gc );
831

832
    p_plugin->redrawToolbar();
833
}
834

835
836
837
838
839
840
841
842
843
844
static void ControlHandler( Widget w, XtPointer closure, XEvent *event )
{
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
    const NPWindow& window = p_plugin->getWindow();

    int i_height = window.height;
    int i_width = window.width;
    int i_xPos = event->xbutton.x;
    int i_yPos = event->xbutton.y;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
845
    if( p_plugin && p_plugin->b_toolbar )
846
    {
847
        int i_playing;
848
        libvlc_exception_t ex;
849

850
        libvlc_exception_init( &ex );
851
852
        libvlc_media_player_t *p_md =
                libvlc_playlist_get_media_player(p_plugin->getVLC(), &ex);
853
854
855
856
        if( libvlc_exception_raised(&ex) )
            fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
        libvlc_exception_clear( &ex );

857
        libvlc_playlist_lock( p_plugin->getVLC() );
858
        i_playing = libvlc_playlist_isplaying( p_plugin->getVLC(), &ex );
859
        libvlc_playlist_unlock( p_plugin->getVLC() );
860
861
        if( libvlc_exception_raised(&ex) )
            fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
862
863
        libvlc_exception_clear( &ex );

864
865
866
        vlc_toolbar_clicked_t clicked;
        clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
        switch( clicked )
867
        {
868
869
            case clicked_Play:
            case clicked_Pause:
870
            {
871
872
873
874
                if( i_playing == 1 )
                    libvlc_playlist_pause( p_plugin->getVLC(), &ex );
                else
                    libvlc_playlist_play( p_plugin->getVLC(), -1, 0, NULL, &ex );
875
876
877

                if( libvlc_exception_raised(&ex) )
                    fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
878
879
880
                libvlc_exception_clear( &ex );
            }
            break;
881

882
883
884
            case clicked_Stop:
            {
                libvlc_playlist_stop( p_plugin->getVLC(), &ex );
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
                if( libvlc_exception_raised(&ex) )
                    fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
                libvlc_exception_clear( &ex );
            }
            break;

            case clicked_Fullscreen:
            {
                if( (i_playing == 1) && p_md )
                {
                    libvlc_set_fullscreen( p_md, 1, &ex );
                    if( libvlc_exception_raised(&ex) )
                        fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
                    libvlc_exception_clear( &ex );
                }
            }
            break;

            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));
909
910
                libvlc_exception_clear( &ex );
            }
911
            break;
912

913
914
915
916
917
918
919
920
            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 );
921

922
923
                    f_length = (float)f_length *
                            ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
924

925
                    libvlc_media_player_set_time( p_md, f_length, &ex );
926
927
                    if( libvlc_exception_raised(&ex) )
                        fprintf( stderr, "%s\n", libvlc_exception_get_message(&ex));
928
929
930
931
                    libvlc_exception_clear( &ex );
                }
            }
            break;
932

933
934
935
936
937
            case clicked_Time:
            {
                /* Not implemented yet*/
            }
            break;
938

939
940
941
            default: /* button_Unknown */
            break;
        }
942
        if( p_md ) libvlc_media_player_release( p_md );
943
    }
944
945
946
    Redraw( w, closure, event );
}

947
948
static void Resize ( Widget w, XtPointer closure, XEvent *event )
{
damienf's avatar
damienf committed
949
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
950
    Window control = p_plugin->getControlWindow();
951
    const NPWindow& window = p_plugin->getWindow();
952
    Window  drawable   = p_plugin->getVideoWindow();
damienf's avatar
damienf committed
953
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
damienf's avatar
damienf committed
954

955
956
957
958
    int i_ret;
    Window root_return, parent_return, * children_return;
    Window base_window;
    unsigned int i_nchildren;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
959
    unsigned int i_control_height, i_control_width;
960

Jean-Paul Saman's avatar
Jean-Paul Saman committed
961
    if( p_plugin->b_toolbar )
962
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
963
        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
964
965
966
    }
    else
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
967
        i_control_height = i_control_width = 0;
968
    }
969
970
971
972
973
974
975
976
977
978
979

#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" );
    }
980
981
#endif /* X11_RESIZE_DEBUG */

Jean-Paul Saman's avatar
Jean-Paul Saman committed
982
    if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
983
    {
damienf's avatar
damienf committed
984
        /* size already set */
985
986
987
        return;
    }

988
    i_ret = XResizeWindow( p_display, drawable,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
989
                           window.width, (window.height - i_control_height) );
990
991
992
993
994

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

995
    XGetWindowAttributes ( p_display, drawable, &attr );
996
997
998
999
1000
1001
1002
1003

    /* 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 */

1004
    XQueryTree( p_display, drawable,
1005
1