vlcshell.cpp 29.8 KB
Newer Older
1
/*****************************************************************************
2
 * vlcshell.cpp: a VLC plugin for Mozilla
3
 *****************************************************************************
Jean-Paul Saman's avatar
Jean-Paul Saman committed
4
 * Copyright (C) 2002-2009 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
#include "vlcshell.h"
46

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

50
#define WINDOW_TEXT "Waiting for video"
51

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

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

61
62
#endif

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

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

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

gbazin's avatar
   
gbazin committed
76
#endif
77
78
79
80
81
82

/******************************************************************************
 * UNIX-only API calls
 *****************************************************************************/
char * NPP_GetMIMEDescription( void )
{
83
84
    static char mimetype[] = PLUGIN_MIMETYPES;
    return mimetype;
85
86
87
88
}

NPError NPP_GetValue( NPP instance, NPPVariable variable, void *value )
{
89
    static char psz_name[] = PLUGIN_NAME;
Sam Hocevar's avatar
Sam Hocevar committed
90
    static char psz_desc[1000];
91

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

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

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

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

damienf's avatar
damienf committed
115
    /* plugin instance variables */
116

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

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

139
        default:
140
            ;
141
    }
142
    return NPERR_GENERIC_ERROR;
143
144
}

145
146
147
148
149
150
151
152
153
154
/*
 * 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;
}

155
156
157
/******************************************************************************
 * Mac-only API calls
 *****************************************************************************/
158
#ifdef XP_MACOSX
159
160
int16 NPP_HandleEvent( NPP instance, void * event )
{
damienf's avatar
damienf committed
161
    static UInt32 lastMouseUp = 0;
162
163
    libvlc_exception_t ex;
    libvlc_exception_init(&ex);
damienf's avatar
damienf committed
164

165
166
167
168
169
    if( instance == NULL )
    {
        return false;
    }

170
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
171
172
173
174
175
    if( p_plugin == NULL )
    {
        return false;
    }

176
    EventRecord *myEvent = (EventRecord*)event;
177

178
    switch( myEvent->what )
179
    {
180
        case nullEvent:
181
            return true;
182
        case mouseDown:
damienf's avatar
damienf committed
183
184
185
186
        {
            if( (myEvent->when - lastMouseUp) < GetDblTime() )
            {
                /* double click */
187
188
                p_plugin->toggle_fullscreen(&ex);
                libvlc_exception_clear(&ex);
damienf's avatar
damienf committed
189
190
191
            }
            return true;
        }
192
        case mouseUp:
damienf's avatar
damienf committed
193
            lastMouseUp = myEvent->when;
194
195
196
197
198
199
200
            return true;
        case keyUp:
        case keyDown:
        case autoKey:
            return true;
        case updateEvt:
        {
201
202
            const NPWindow& npwindow = p_plugin->getWindow();
            if( npwindow.window )
damienf's avatar
damienf committed
203
            {
204
205
                int hasVout = FALSE;

206
                if( p_plugin->playlist_isplaying(&ex) )
damienf's avatar
damienf committed
207
                {
208
209
                    hasVout = p_plugin->player_has_vout(NULL);
                    if( hasVout )
damienf's avatar
damienf committed
210
                    {
211
212
213
214
215
                        libvlc_rectangle_t area;
                        area.left = 0;
                        area.top = 0;
                        area.right = npwindow.width;
                        area.bottom = npwindow.height;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
216
                        libvlc_video_redraw_rectangle(p_plugin->getMD(&ex), &area, NULL);
damienf's avatar
damienf committed
217
218
                    }
                }
219
                libvlc_exception_clear(&ex);
220

221
222
223
                if( ! hasVout )
                {
                    /* draw the beautiful "No Picture" */
damienf's avatar
damienf committed
224

225
226
                    ForeColor(blackColor);
                    PenMode( patCopy );
damienf's avatar
damienf committed
227

228
229
                    /* seems that firefox forgets to set the following
                     * on occasion (reload) */
230
231
                    SetOrigin(((NP_Port *)npwindow.window)->portx,
                              ((NP_Port *)npwindow.window)->porty);
232

233
234
235
236
237
238
                    Rect rect;
                    rect.left = 0;
                    rect.top = 0;
                    rect.right = npwindow.width;
                    rect.bottom = npwindow.height;
                    PaintRect( &rect );
damienf's avatar
damienf committed
239

240
241
242
243
                    ForeColor(whiteColor);
                    MoveTo( (npwindow.width-80)/ 2  , npwindow.height / 2 );
                    DrawText( WINDOW_TEXT , 0 , strlen(WINDOW_TEXT) );
                }
damienf's avatar
damienf committed
244
            }
245
246
247
248
249
250
251
252
253
            return true;
        }
        case activateEvt:
            return false;
        case NPEventType_GetFocusEvent:
        case NPEventType_LoseFocusEvent:
            return true;
        case NPEventType_AdjustCursorEvent:
            return false;
254
255
256
257
258
        case NPEventType_MenuCommandEvent:
            return false;
        case NPEventType_ClippingChangedEvent:
            return false;
        case NPEventType_ScrollingBeginsEvent:
259
            return true;
260
        case NPEventType_ScrollingEndsEvent:
261
262
263
264
265
            return true;
        default:
            ;
    }
    return false;
266
}
267
#endif /* XP_MACOSX */
268

269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/******************************************************************************
 * 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
290
    NPError status;
291
292
293
294
295
296

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

damienf's avatar
damienf committed
297
298
    VlcPlugin * p_plugin = new VlcPlugin( instance, mode );
    if( NULL == p_plugin )
299
300
301
302
    {
        return NPERR_OUT_OF_MEMORY_ERROR;
    }

damienf's avatar
damienf committed
303
    status = p_plugin->init(argc, argn, argv);
304
305
    if( NPERR_NO_ERROR == status )
    {
damienf's avatar
damienf committed
306
        instance->pdata = reinterpret_cast<void*>(p_plugin);
307
308
#if 0
        NPN_SetValue(instance, NPPVpluginWindowBool, (void *)false);
309
        NPN_SetValue(instance, NPPVpluginTransparentBool, (void *)false);
310
#endif
gbazin's avatar
   
gbazin committed
311
    }
312
313
    else
    {
314
315
        delete p_plugin;
    }
damienf's avatar
damienf committed
316
    return status;
317
318
}

319
320
NPError NPP_Destroy( NPP instance, NPSavedData** save )
{
damienf's avatar
damienf committed
321
    if( NULL == instance )
322
323
        return NPERR_INVALID_INSTANCE_ERROR;

damienf's avatar
damienf committed
324
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
damienf's avatar
damienf committed
325
326
327
328
    if( NULL == p_plugin )
        return NPERR_NO_ERROR;

    instance->pdata = NULL;
329

330
#if XP_WIN
331
    HWND win = (HWND)p_plugin->getWindow().window;
332
333
334
335
336
337
338
339
    WNDPROC winproc = p_plugin->getWindowProc();
    if( winproc )
    {
        /* reset WNDPROC */
        SetWindowLong( win, GWL_WNDPROC, (LONG)winproc );
    }
#endif

340
    delete p_plugin;
341
342
343
344
345
346

    return NPERR_NO_ERROR;
}

NPError NPP_SetWindow( NPP instance, NPWindow* window )
{
347
#if defined(XP_UNIX) && !defined(__APPLE__)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
348
349
    Window control;
    unsigned int i_control_height = 0, i_control_width = 0;
350
#endif
351

damienf's avatar
damienf committed
352
    if( ! instance )
353
354
355
356
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }

damienf's avatar
damienf committed
357
358
    /* NPP_SetWindow may be called before NPP_New (Opera) */
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(instance->pdata);
359
    if( NULL == p_plugin )
damienf's avatar
damienf committed
360
361
362
363
    {
        /* we should probably show a splash screen here */
        return NPERR_NO_ERROR;
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
364

365
#if defined(XP_UNIX) && !defined(__APPLE__)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
366
    control = p_plugin->getControlWindow();
367
#endif
Jean-Paul Saman's avatar
Jean-Paul Saman committed
368
369
370
371

    libvlc_exception_t ex;
    libvlc_exception_init(&ex);

damienf's avatar
damienf committed
372
    libvlc_instance_t *p_vlc = p_plugin->getVLC();
373

374
375
376
377
378
379
380
381
    /*
     * 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.
     */

382
383
    /* retrieve current window */
    NPWindow& curwin = p_plugin->getWindow();
damienf's avatar
damienf committed
384
385
386

#ifdef XP_MACOSX
    if( window && window->window )
387
    {
damienf's avatar
damienf committed
388
        /* check if plugin has a new parent window */
389
        CGrafPtr drawable = (((NP_Port*) (window->window))->port);
390
        if( !curwin.window || drawable != (((NP_Port*) (curwin.window))->port) )
391
        {
damienf's avatar
damienf committed
392
            /* set/change parent window */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
393
394
            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, &ex);
            libvlc_exception_clear(&ex);
395
        }
396

damienf's avatar
damienf committed
397
398
399
400
        /* as MacOS X video output is windowless, set viewport */
        libvlc_rectangle_t view, clip;

        /*
401
402
403
        ** 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
404
405
406
407
408
        */
        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;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
409

damienf's avatar
damienf committed
410
411
412
413
414
415
        /* 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;

416
        libvlc_video_set_viewport(p_vlc, p_plugin->getMD(&ex), &view, &clip, &ex);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
417
        libvlc_exception_clear(&ex);
damienf's avatar
damienf committed
418

419
420
421
        /* remember new window */
        p_plugin->setWindow(*window);
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
422
423
    else if( curwin.window )
    {
424
        /* change/set parent */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
425
426
427
        libvlc_video_set_parent(p_vlc, 0, &ex);
        libvlc_exception_clear(&ex);

428
        curwin.window = NULL;
429
    }
damienf's avatar
damienf committed
430
#endif /* XP_MACOSX */
431

damienf's avatar
damienf committed
432
433
#ifdef XP_WIN
    if( window && window->window )
434
    {
damienf's avatar
damienf committed
435
        /* check if plugin has a new parent window */
436
        HWND drawable = (HWND) (window->window);
437
        if( !curwin.window || drawable != curwin.window )
438
        {
damienf's avatar
damienf committed
439
            /* reset previous window settings */
440
            HWND oldwin = (HWND)p_plugin->getWindow().window;
damienf's avatar
damienf committed
441
442
443
444
445
446
            WNDPROC oldproc = p_plugin->getWindowProc();
            if( oldproc )
            {
                /* reset WNDPROC */
                SetWindowLong( oldwin, GWL_WNDPROC, (LONG)oldproc );
            }
447
            /* attach our plugin object */
448
449
            SetWindowLongPtr((HWND)drawable, GWLP_USERDATA,
                             reinterpret_cast<LONG_PTR>(p_plugin));
450

damienf's avatar
damienf committed
451
            /* install our WNDPROC */
452
            p_plugin->setWindowProc( (WNDPROC)SetWindowLong( drawable,
453
                                             GWL_WNDPROC, (LONG)Manage ) );
damienf's avatar
damienf committed
454
455
456
457
458
459
460

            /* 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 */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
461
462
            libvlc_video_set_parent(p_vlc, (libvlc_drawable_t)drawable, &ex);
            libvlc_exception_clear(&ex);
463

464
465
            /* remember new window */
            p_plugin->setWindow(*window);
damienf's avatar
damienf committed
466

467
468
469
470
            /* Redraw window */
            InvalidateRect( (HWND)drawable, NULL, TRUE );
            UpdateWindow( (HWND)drawable );
        }
damienf's avatar
damienf committed
471
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
472
    else if( curwin.window )
damienf's avatar
damienf committed
473
474
    {
        /* reset WNDPROC */
475
        HWND oldwin = (HWND)curwin.window;
damienf's avatar
damienf committed
476
477
        SetWindowLong( oldwin, GWL_WNDPROC, (LONG)(p_plugin->getWindowProc()) );
        p_plugin->setWindowProc(NULL);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
478

damienf's avatar
damienf committed
479
        /* change/set parent */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
480
481
482
        libvlc_video_set_parent(p_vlc, 0, &ex);
        libvlc_exception_clear(&ex);

damienf's avatar
damienf committed
483
        curwin.window = NULL;
484
    }
damienf's avatar
damienf committed
485
486
487
#endif /* XP_WIN */

#ifdef XP_UNIX
488
489
490
491
492
    /* 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
493

damienf's avatar
damienf committed
494
495
    if( window && window->window )
    {
496
        Window  parent  = (Window) window->window;
497
        if( !curwin.window || (parent != (Window)curwin.window) )
damienf's avatar
damienf committed
498
        {
499
500
            Display *p_display = ( (NPSetWindowCallbackStruct *)
                                   window->ws_info )->display;
501

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

504
505
            int i_blackColor = BlackPixel(p_display, DefaultScreen(p_display));

506
            /* create windows */
507
            Window video = XCreateSimpleWindow( p_display, parent, 0, 0,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
508
                           window->width, window->height - i_control_height,
509
510
                           0, i_blackColor, i_blackColor );
            Window controls = (Window) NULL;
511
512
513
514
            controls = XCreateSimpleWindow( p_display, parent,
                            0, window->height - i_control_height-1,
                            window->width, i_control_height-1,
                            0, i_blackColor, i_blackColor );
515
516
517

            XMapWindow( p_display, parent );
            XMapWindow( p_display, video );
518
            if( controls ) { XMapWindow( p_display, controls ); }
519
520
521

            XFlush(p_display);

522
            /* bind events */
523
524
525
526
527
528
529
530
531
            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 );

damienf's avatar
damienf committed
532
            /* set/change parent window */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
533
534
            libvlc_video_set_parent( p_vlc, (libvlc_drawable_t) video, &ex );
            libvlc_exception_clear(&ex);
535

damienf's avatar
damienf committed
536
            /* remember window */
537
538
            p_plugin->setWindow( *window );
            p_plugin->setVideoWindow( video );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
539
540
541
542
543

            if( controls )
            {
                p_plugin->setControlWindow( controls );
            }
damienf's avatar
damienf committed
544
545

            Redraw( w, (XtPointer)p_plugin, NULL );
546
547
548
549
550
551

            /* now display toolbar if asked through parameters */
            if( p_plugin->b_toolbar )
            {
                p_plugin->showToolbar();
            }
damienf's avatar
damienf committed
552
        }
damienf's avatar
damienf committed
553
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
554
    else if( curwin.window )
555
556
    {
        /* change/set parent */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
557
558
        libvlc_video_set_parent(p_vlc, 0, &ex);
        libvlc_exception_clear(&ex);
damienf's avatar
damienf committed
559
        curwin.window = NULL;
560
    }
561
#endif /* XP_UNIX */
562
563
564
565
566

    if( !p_plugin->b_stream )
    {
        if( p_plugin->psz_target )
        {
567
            if( p_plugin->playlist_add( p_plugin->psz_target, NULL ) != -1 )
damienf's avatar
damienf committed
568
569
570
            {
                if( p_plugin->b_autoplay )
                {
571
                    p_plugin->playlist_play(NULL);
damienf's avatar
damienf committed
572
573
                }
            }
574
            p_plugin->b_stream = true;
575
576
577
578
579
580
581
582
        }
    }
    return NPERR_NO_ERROR;
}

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

588
    VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
589
590
591
592
    if( NULL == p_plugin )
    {
        return NPERR_INVALID_INSTANCE_ERROR;
    }
593

594
595
596
597
598
599
600
601
   /*
   ** 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) )
602
    {
603
604
605
        /* TODO: use pipes !!!! */
        *stype = NP_ASFILEONLY;
        return NPERR_NO_ERROR;
606
    }
607
    return NPERR_GENERIC_ERROR;
608
609
610
611
}

int32 NPP_WriteReady( NPP instance, NPStream *stream )
{
612
613
    /* TODO */
    return 8*1024;
614
615
616
617
618
}

int32 NPP_Write( NPP instance, NPStream *stream, int32 offset,
                 int32 len, void *buffer )
{
619
620
    /* TODO */
    return len;
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
}

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

639
    VlcPlugin *p_plugin = reinterpret_cast<VlcPlugin *>(instance->pdata);
640
641
642
643
    if( NULL == p_plugin )
    {
        return;
    }
644

645
    if( p_plugin->playlist_add( stream->url, NULL ) != -1 )
646
647
648
    {
        if( p_plugin->b_autoplay )
        {
649
            p_plugin->playlist_play(NULL);
650
651
        }
    }
652
}
653
654
655
656
657
658
659
660
661
662
663
664
665

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 )
{
666
667
    if( printInfo == NULL )
    {
668
        return;
669
    }
670

671
672
673
    if( instance != NULL )
    {
        /***** Insert NPP_Print code here *****\
674
        PluginInstance* p_plugin = (PluginInstance*) instance->pdata;
675
        \**************************************/
676

677
678
        if( printInfo->mode == NP_FULL )
        {
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
            /*
             * 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.
             */

696
            /***** Insert NPP_Print code here *****\
697
698
699
700
            void* platformPrint =
                printInfo->print.fullPrint.platformPrint;
            NPBool printOne =
                printInfo->print.fullPrint.printOne;
701
            \**************************************/
702
703
704
705

            /* Do the default*/
            printInfo->print.fullPrint.pluginPrinted = FALSE;
        }
706
707
708
        else
        {
            /* If not fullscreen, we must be embedded */
709
710
711
712
713
714
715
716
717
718
719
720
            /*
             * 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.
             */

721
            /***** Insert NPP_Print code here *****\
722
723
724
725
            NPWindow* printWindow =
                &(printInfo->print.embedPrint.window);
            void* platformPrint =
                printInfo->print.embedPrint.platformPrint;
726
            \**************************************/
727
728
729
730
        }
    }
}

731
732
733
/******************************************************************************
 * Windows-only methods
 *****************************************************************************/
damienf's avatar
damienf committed
734
735
#if XP_WIN
static LRESULT CALLBACK Manage( HWND p_hwnd, UINT i_msg, WPARAM wpar, LPARAM lpar )
736
{
damienf's avatar
damienf committed
737
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(GetWindowLongPtr(p_hwnd, GWLP_USERDATA));
738
739
740

    switch( i_msg )
    {
damienf's avatar
damienf committed
741
742
743
        case WM_ERASEBKGND:
            return 1L;

744
745
746
747
748
749
750
751
752
        case WM_PAINT:
        {
            PAINTSTRUCT paintstruct;
            HDC hdc;
            RECT rect;

            hdc = BeginPaint( p_hwnd, &paintstruct );

            GetClientRect( p_hwnd, &rect );
damienf's avatar
damienf committed
753
754
755
756

            FillRect( hdc, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH) );
            SetTextColor(hdc, RGB(255, 255, 255));
            SetBkColor(hdc, RGB(0, 0, 0));
757
758
            DrawText( hdc, WINDOW_TEXT, strlen(WINDOW_TEXT), &rect,
                      DT_CENTER|DT_VCENTER|DT_SINGLELINE);
759
760

            EndPaint( p_hwnd, &paintstruct );
damienf's avatar
damienf committed
761
            return 0L;
762
763
        }
        default:
damienf's avatar
damienf committed
764
            /* delegate to default handler */
765
766
            return CallWindowProc( p_plugin->getWindowProc(), p_hwnd,
                                   i_msg, wpar, lpar );
767
768
    }
}
769
#endif /* XP_WIN */
770

771
772
773
/******************************************************************************
 * UNIX-only methods
 *****************************************************************************/
774
#ifdef XP_UNIX
775
776
static void Redraw( Widget w, XtPointer closure, XEvent *event )
{
damienf's avatar
damienf committed
777
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
778
    Window control = p_plugin->getControlWindow();
779
    const NPWindow& window = p_plugin->getWindow();
780
781
    GC gc;
    XGCValues gcv;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
782
    unsigned int i_control_height, i_control_width;
783

Jean-Paul Saman's avatar
Jean-Paul Saman committed
784
785
    if( p_plugin->b_toolbar )
        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
786
    else
Jean-Paul Saman's avatar
Jean-Paul Saman committed
787
        i_control_height = i_control_width = 0;
788

789
    Window video = p_plugin->getVideoWindow();
damienf's avatar
damienf committed
790
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
damienf's avatar
damienf committed
791
792

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

795
    XFillRectangle( p_display, video, gc,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
796
                    0, 0, window.width, window.height - i_control_height);
797

damienf's avatar
damienf committed
798
799
    gcv.foreground = WhitePixel( p_display, 0 );
    XChangeGC( p_display, gc, GCForeground, &gcv );
800

801
    XDrawString( p_display, video, gc,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
802
                 window.width / 2 - 40, (window.height - i_control_height) / 2,
803
                 WINDOW_TEXT, strlen(WINDOW_TEXT) );
804
    XFreeGC( p_display, gc );
805

806
    p_plugin->redrawToolbar();
807
}
808

809
810
811
812
813
814
815
816
817
818
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
819
    if( p_plugin && p_plugin->b_toolbar )
820
    {
821
        int i_playing;
822
        libvlc_exception_t ex;
823

824
        libvlc_exception_init( &ex );
825
        libvlc_media_player_t *p_md = p_plugin->getMD(&ex);
826
827
        libvlc_exception_clear( &ex );

828
        i_playing = p_plugin->playlist_isplaying( &ex );
829
830
        libvlc_exception_clear( &ex );

831
832
833
        vlc_toolbar_clicked_t clicked;
        clicked = p_plugin->getToolbarButtonClicked( i_xPos, i_yPos );
        switch( clicked )
834
        {
835
836
            case clicked_Play:
            case clicked_Pause:
837
            {
838
                if( i_playing == 1 )
839
                    p_plugin->playlist_pause( &ex );
840
                else
841
                    p_plugin->playlist_play( &ex );
842

843
844
845
                libvlc_exception_clear( &ex );
            }
            break;
846

847
848
            case clicked_Stop:
            {
849
                p_plugin->playlist_stop(&ex);
850
851
852
853
854
855
                libvlc_exception_clear( &ex );
            }
            break;

            case clicked_Fullscreen:
            {
856
857
                p_plugin->set_fullscreen( 1, &ex );
                libvlc_exception_clear( &ex );
858
859
860
861
862
863
864
            }
            break;

            case clicked_Mute:
            case clicked_Unmute:
            {
                libvlc_audio_toggle_mute( p_plugin->getVLC(), &ex );
865
866
                libvlc_exception_clear( &ex );
            }
867
            break;
868

869
870
871
872
873
874
875
876
            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 );
877

878
879
                    f_length = (float)f_length *
                            ( ((float)i_xPos-4.0 ) / ( ((float)i_width-8.0)/100) );
880

881
882
883
884
885
                    libvlc_media_player_set_time( p_md, f_length, &ex );
                    libvlc_exception_clear( &ex );
                }
            }
            break;
886

887
888
889
890
891
            case clicked_Time:
            {
                /* Not implemented yet*/
            }
            break;
892

893
894
895
            default: /* button_Unknown */
            break;
        }
896
    }
897
898
899
    Redraw( w, closure, event );
}

900
901
static void Resize ( Widget w, XtPointer closure, XEvent *event )
{
damienf's avatar
damienf committed
902
    VlcPlugin* p_plugin = reinterpret_cast<VlcPlugin*>(closure);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
903
    Window control = p_plugin->getControlWindow();
904
    const NPWindow& window = p_plugin->getWindow();
905
    Window  drawable   = p_plugin->getVideoWindow();
damienf's avatar
damienf committed
906
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
damienf's avatar
damienf committed
907

908
909
910
911
    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
912
    unsigned int i_control_height, i_control_width;
913

Jean-Paul Saman's avatar
Jean-Paul Saman committed
914
    if( p_plugin->b_toolbar )
915
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
916
        p_plugin->getToolbarSize( &i_control_width, &i_control_height );
917
918
919
    }
    else
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
920
        i_control_height = i_control_width = 0;
921
    }
922
923
924
925
926
927
928
929
930
931
932

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

Jean-Paul Saman's avatar
Jean-Paul Saman committed
935
    if( ! p_plugin->setSize(window.width, (window.height - i_control_height)) )
936
    {
damienf's avatar
damienf committed
937
        /* size already set */
938
939
940
        return;
    }

941
    i_ret = XResizeWindow( p_display, drawable,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
942
                           window.width, (window.height - i_control_height) );
943
944
945
946
947

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

948
    XGetWindowAttributes ( p_display, drawable, &attr );
949
950
951
952
953
954
955
956

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

957
    XQueryTree( p_display, drawable,
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
                &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 */

damienf's avatar
damienf committed
973
        i_ret = XResizeWindow( p_display, base_window,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
974
                window.width, ( window.height - i_control_height ) );
975
976
977
978
979
980

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

981
        XGetWindowAttributes( p_display, base_window, &attr );
982
983
984
985
986
987
988
989

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

#endif /* XP_UNIX */