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

/*****************************************************************************
 * Preamble
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
29
30
31
32
33
34
#include "config.h"

#ifdef HAVE_MOZILLA_CONFIG_H
#   include <mozilla-config.h>
#endif

35
#include "vlcplugin.h"
damienf's avatar
damienf committed
36
#include "control/npolibvlc.h"
37

damienf's avatar
damienf committed
38
39
#include <ctype.h>

40
/*****************************************************************************
41
 * VlcPlugin constructor and destructor
42
 *****************************************************************************/
damienf's avatar
damienf committed
43
44
45
VlcPlugin::VlcPlugin( NPP instance, uint16 mode ) :
    i_npmode(mode),
    b_stream(0),
damienf's avatar
damienf committed
46
    b_autoplay(1),
Jean-Paul Saman's avatar
Jean-Paul Saman committed
47
    b_toolbar(0),
48
    psz_text(NULL),
damienf's avatar
damienf committed
49
    psz_target(NULL),
50
    playlist_index(-1),
damienf's avatar
damienf committed
51
    libvlc_instance(NULL),
52
53
    libvlc_media_list(NULL),
    libvlc_media_player(NULL),
damienf's avatar
damienf committed
54
    p_scriptClass(NULL),
damienf's avatar
damienf committed
55
56
57
58
59
60
61
62
    p_browser(instance),
    psz_baseURL(NULL)
#if XP_WIN
    ,pf_wndproc(NULL)
#endif
#if XP_UNIX
    ,i_width((unsigned)-1)
    ,i_height((unsigned)-1)
Jean-Paul Saman's avatar
Jean-Paul Saman committed
63
64
    ,i_tb_width(0)
    ,i_tb_height(0)
65
    ,i_last_position(0)
66
67
68
69
70
71
72
73
    ,p_btnPlay(NULL)
    ,p_btnPause(NULL)
    ,p_btnStop(NULL)
    ,p_btnMute(NULL)
    ,p_btnUnmute(NULL)
    ,p_btnFullscreen(NULL)
    ,p_btnTime(NULL)
    ,p_timeline(NULL)
damienf's avatar
damienf committed
74
#endif
75
{
damienf's avatar
damienf committed
76
    memset(&npwindow, 0, sizeof(NPWindow));
77
#if XP_UNIX
78
79
    memset(&npvideo, 0, sizeof(Window));
    memset(&npcontrol, 0, sizeof(Window));
80
#endif
81
82
}

damienf's avatar
damienf committed
83
static bool boolValue(const char *value) {
84
    return ( !strcmp(value, "1") ||
damienf's avatar
damienf committed
85
86
87
             !strcasecmp(value, "true") ||
             !strcasecmp(value, "yes") );
}
88

damienf's avatar
damienf committed
89
NPError VlcPlugin::init(int argc, char* const argn[], char* const argv[])
90
{
damienf's avatar
damienf committed
91
    /* prepare VLC command line */
92
    const char *ppsz_argv[32];
93
    int ppsz_argc = 0;
damienf's avatar
damienf committed
94

95
96
97
98
#ifndef NDEBUG
    ppsz_argv[ppsz_argc++] = "--no-plugins-cache";
#endif

damienf's avatar
damienf committed
99
100
    /* locate VLC module path */
#ifdef XP_MACOSX
101
    ppsz_argv[ppsz_argc++] = "--plugin-path=/Library/Internet\\ Plug-Ins/VLC\\ Plugin.plugin/Contents/MacOS/modules";
102
    ppsz_argv[ppsz_argc++] = "--vout=minimal_macosx";
damienf's avatar
damienf committed
103
104
105
106
107
108
#elif defined(XP_WIN)
    HKEY h_key;
    DWORD i_type, i_data = MAX_PATH + 1;
    char p_data[MAX_PATH + 1];
    if( RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\VideoLAN\\VLC",
                      0, KEY_READ, &h_key ) == ERROR_SUCCESS )
109
    {
damienf's avatar
damienf committed
110
111
112
113
114
         if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
                              (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
         {
             if( i_type == REG_SZ )
             {
115
                 strcat( p_data, "\\plugins" );
116
117
                 ppsz_argv[ppsz_argc++] = "--plugin-path";
                 ppsz_argv[ppsz_argc++] = p_data;
damienf's avatar
damienf committed
118
119
120
             }
         }
         RegCloseKey( h_key );
121
    }
damienf's avatar
damienf committed
122
    ppsz_argv[ppsz_argc++] = "--no-one-instance";
123

damienf's avatar
damienf committed
124
#endif /* XP_MACOSX */
125

126
127
128
129
    /* common settings */
    ppsz_argv[ppsz_argc++] = "-vv";
    ppsz_argv[ppsz_argc++] = "--no-stats";
    ppsz_argv[ppsz_argc++] = "--no-media-library";
130
    ppsz_argv[ppsz_argc++] = "--ignore-config";
131
    ppsz_argv[ppsz_argc++] = "--intf=dummy";
132
133
    ppsz_argv[ppsz_argc++] = "--no-video-title-show";

damienf's avatar
damienf committed
134
    const char *progid = NULL;
damienf's avatar
damienf committed
135
136
137
138

    /* parse plugin arguments */
    for( int i = 0; i < argc ; i++ )
    {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
139
       /* fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]); */
damienf's avatar
damienf committed
140
141
142
143
144
145
146
147

        if( !strcmp( argn[i], "target" )
         || !strcmp( argn[i], "mrl")
         || !strcmp( argn[i], "filename")
         || !strcmp( argn[i], "src") )
        {
            psz_target = argv[i];
        }
148
149
        else if( !strcmp( argn[i], "text" ) )
        {
150
            free( psz_text );
151
152
            psz_text = strdup( argv[i] );
        }
damienf's avatar
damienf committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        else if( !strcmp( argn[i], "autoplay")
              || !strcmp( argn[i], "autostart") )
        {
            b_autoplay = boolValue(argv[i]);
        }
        else if( !strcmp( argn[i], "fullscreen" ) )
        {
            if( boolValue(argv[i]) )
            {
                ppsz_argv[ppsz_argc++] = "--fullscreen";
            }
            else
            {
                ppsz_argv[ppsz_argc++] = "--no-fullscreen";
            }
        }
        else if( !strcmp( argn[i], "mute" ) )
        {
            if( boolValue(argv[i]) )
            {
173
                ppsz_argv[ppsz_argc++] = "--volume=0";
damienf's avatar
damienf committed
174
175
176
177
178
179
180
181
182
            }
        }
        else if( !strcmp( argn[i], "loop")
              || !strcmp( argn[i], "autoloop") )
        {
            if( boolValue(argv[i]) )
            {
                ppsz_argv[ppsz_argc++] = "--loop";
            }
183
184
            else
            {
damienf's avatar
damienf committed
185
186
187
                ppsz_argv[ppsz_argc++] = "--no-loop";
            }
        }
damienf's avatar
damienf committed
188
189
        else if( !strcmp( argn[i], "version")
              || !strcmp( argn[i], "progid") )
damienf's avatar
damienf committed
190
        {
damienf's avatar
damienf committed
191
            progid = argv[i];
damienf's avatar
damienf committed
192
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
193
        else if( !strcmp( argn[i], "toolbar" ) )
194
        {
Jean-Paul Saman's avatar
Jean-Paul Saman committed
195
/* FIXME: Remove this when toolbar functionality has been implemented on
196
197
 * MacOS X and Win32 for Firefox/Mozilla/Safari. */
#ifdef XP_UNIX
Jean-Paul Saman's avatar
Jean-Paul Saman committed
198
            b_toolbar = boolValue(argv[i]);
199
#endif
200
        }
damienf's avatar
damienf committed
201
202
    }

203
204
    libvlc_exception_t ex;
    libvlc_exception_init(&ex);
205

206
    libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
207
208
209
210
211
212
213
    if( libvlc_exception_raised(&ex) )
    {
        libvlc_exception_clear(&ex);
        return NPERR_GENERIC_ERROR;
    }

    libvlc_media_list = libvlc_media_list_new(libvlc_instance,&ex);
214
    if( libvlc_exception_raised(&ex) )
damienf's avatar
damienf committed
215
    {
216
        libvlc_exception_clear(&ex);
damienf's avatar
damienf committed
217
218
219
220
221
222
223
224
        return NPERR_GENERIC_ERROR;
    }

    /*
    ** fetch plugin base URL, which is the URL of the page containing the plugin
    ** this URL is used for making absolute URL from relative URL that may be
    ** passed as an MRL argument
    */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
225
    NPObject *plugin = NULL;
damienf's avatar
damienf committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

    if( NPERR_NO_ERROR == NPN_GetValue(p_browser, NPNVWindowNPObject, &plugin) )
    {
        /*
        ** is there a better way to get that info ?
        */
        static const char docLocHref[] = "document.location.href";
        NPString script;
        NPVariant result;

        script.utf8characters = docLocHref;
        script.utf8length = sizeof(docLocHref)-1;

        if( NPN_Evaluate(p_browser, plugin, &script, &result) )
        {
            if( NPVARIANT_IS_STRING(result) )
            {
                NPString &location = NPVARIANT_TO_STRING(result);

Jean-Paul Saman's avatar
Jean-Paul Saman committed
245
                psz_baseURL = (char *) malloc(location.utf8length+1);
damienf's avatar
damienf committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
                if( psz_baseURL )
                {
                    strncpy(psz_baseURL, location.utf8characters, location.utf8length);
                    psz_baseURL[location.utf8length] = '\0';
                }
            }
            NPN_ReleaseVariantValue(&result);
        }
        NPN_ReleaseObject(plugin);
    }

    if( psz_target )
    {
        // get absolute URL from src
damienf's avatar
damienf committed
260
261
        char *psz_absurl = getAbsoluteURL(psz_target);
        psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
damienf's avatar
damienf committed
262
263
264
    }

    /* assign plugin script root class */
265
266
    /* new APIs */
    p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
damienf's avatar
damienf committed
267
268

    return NPERR_NO_ERROR;
269
270
}

damienf's avatar
damienf committed
271
VlcPlugin::~VlcPlugin()
272
{
273
274
    free(psz_baseURL);
    free(psz_target);
275
    free(psz_text);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
276

277
278
279
280
    if( libvlc_media_player )
        libvlc_media_player_release( libvlc_media_player );
    if( libvlc_media_list )
        libvlc_media_list_release( libvlc_media_list );
damienf's avatar
damienf committed
281
    if( libvlc_instance )
282
        libvlc_release(libvlc_instance);
283
284
}

285
286
287
288
289
290
291
/*****************************************************************************
 * VlcPlugin playlist replacement methods
 *****************************************************************************/
void VlcPlugin::set_player_window( libvlc_exception_t *ex )
{
#ifdef XP_UNIX
    libvlc_media_player_set_xwindow(libvlc_media_player,
292
                                    (libvlc_drawable_t)getVideoWindow(),
293
294
295
296
297
298
299
                                    ex);
#endif
#ifdef XP_MACOSX
    // XXX FIXME insert appropriate call here
#endif
#ifdef XP_WIN
    libvlc_media_player_set_hwnd(libvlc_media_player,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
300
                                 getWindow().window,
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
                                 ex);
#endif
}

int VlcPlugin::playlist_add( const char *mrl, libvlc_exception_t *ex )
{
    int item = -1;
    libvlc_media_t *p_m = libvlc_media_new(libvlc_instance,mrl,ex);
    if( libvlc_exception_raised(ex) )
        return -1;

    libvlc_media_list_lock(libvlc_media_list);
    libvlc_media_list_add_media(libvlc_media_list,p_m,ex);
    if( !libvlc_exception_raised(ex) )
        item = libvlc_media_list_count(libvlc_media_list,ex)-1;
    libvlc_media_list_unlock(libvlc_media_list);

    libvlc_media_release(p_m);

    return item;
}

int VlcPlugin::playlist_add_extended_untrusted( const char *mrl, const char *name,
                    int optc, const char **optv, libvlc_exception_t *ex )
{
    libvlc_media_t *p_m = libvlc_media_new(libvlc_instance, mrl,ex);
    int item = -1;
    if( libvlc_exception_raised(ex) )
        return -1;

    for( int i = 0; i < optc; ++i )
    {
333
        libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique, ex);
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
        if( libvlc_exception_raised(ex) )
        {
            libvlc_media_release(p_m);
            return -1;
        }
    }

    libvlc_media_list_lock(libvlc_media_list);
    libvlc_media_list_add_media(libvlc_media_list,p_m,ex);
    if( !libvlc_exception_raised(ex) )
        item = libvlc_media_list_count(libvlc_media_list,ex)-1;
    libvlc_media_list_unlock(libvlc_media_list);
    libvlc_media_release(p_m);

    return item;
}

bool VlcPlugin::playlist_select( int idx, libvlc_exception_t *ex )
{
    libvlc_media_t *p_m = NULL;

    libvlc_media_list_lock(libvlc_media_list);

    int count = libvlc_media_list_count(libvlc_media_list,ex);
    if( libvlc_exception_raised(ex) )
        goto bad_unlock;

    if( idx<0||idx>=count )
        goto bad_unlock;

    playlist_index = idx;

    p_m = libvlc_media_list_item_at_index(libvlc_media_list,playlist_index,ex);
    libvlc_media_list_unlock(libvlc_media_list);

    if( libvlc_exception_raised(ex) )
        return false;

    if( libvlc_media_player )
    {
        libvlc_media_player_release( libvlc_media_player );
        libvlc_media_player = NULL;
    }

    libvlc_media_player = libvlc_media_player_new_from_media(p_m,ex);
    if( libvlc_media_player )
        set_player_window(ex);

    libvlc_media_release( p_m );
    return !libvlc_exception_raised(ex);

bad_unlock:
    libvlc_media_list_unlock(libvlc_media_list);
    return false;
}

void VlcPlugin::playlist_delete_item( int idx, libvlc_exception_t *ex )
{
    libvlc_media_list_lock(libvlc_media_list);
    libvlc_media_list_remove_index(libvlc_media_list,idx,ex);
    libvlc_media_list_unlock(libvlc_media_list);
}

void VlcPlugin::playlist_clear( libvlc_exception_t *ex )
{
    if( libvlc_media_list )
        libvlc_media_list_release(libvlc_media_list);
    libvlc_media_list = libvlc_media_list_new(getVLC(),ex);
}

int VlcPlugin::playlist_count( libvlc_exception_t *ex )
{
    int items_count = 0;
    libvlc_media_list_lock(libvlc_media_list);
    items_count = libvlc_media_list_count(libvlc_media_list,ex);
    libvlc_media_list_unlock(libvlc_media_list);
    return items_count;
}

void VlcPlugin::toggle_fullscreen( libvlc_exception_t *ex )
{
    if( playlist_isplaying(ex) )
        libvlc_toggle_fullscreen(libvlc_media_player,ex);
}
void VlcPlugin::set_fullscreen( int yes, libvlc_exception_t *ex )
{
    if( playlist_isplaying(ex) )
        libvlc_set_fullscreen(libvlc_media_player,yes,ex);
}
int  VlcPlugin::get_fullscreen( libvlc_exception_t *ex )
{
    int r = 0;
    if( playlist_isplaying(ex) )
        r = libvlc_get_fullscreen(libvlc_media_player,ex);
    return r;
}

Jean-Paul Saman's avatar
Jean-Paul Saman committed
431
bool  VlcPlugin::player_has_vout( libvlc_exception_t *ex )
432
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
433
    bool r = false;
434
435
436
437
438
    if( playlist_isplaying(ex) )
        r = libvlc_media_player_has_vout(libvlc_media_player, ex);
    return r;
}

damienf's avatar
damienf committed
439
440
441
/*****************************************************************************
 * VlcPlugin methods
 *****************************************************************************/
442

damienf's avatar
damienf committed
443
char *VlcPlugin::getAbsoluteURL(const char *url)
444
{
damienf's avatar
damienf committed
445
    if( NULL != url )
446
    {
damienf's avatar
damienf committed
447
448
449
        // check whether URL is already absolute
        const char *end=strchr(url, ':');
        if( (NULL != end) && (end != url) )
450
        {
damienf's avatar
damienf committed
451
452
            // validate protocol header
            const char *start = url;
453
454
455
            char c = *start;
            if( isalpha(c) )
            {
damienf's avatar
damienf committed
456
                ++start;
457
458
459
460
461
462
463
464
465
466
467
468
469
470
                while( start != end )
                {
                    c  = *start;
                    if( ! (isalnum(c)
                       || ('-' == c)
                       || ('+' == c)
                       || ('.' == c)
                       || ('/' == c)) ) /* VLC uses / to allow user to specify a demuxer */
                        // not valid protocol header, assume relative URL
                        goto relativeurl;
                    ++start;
                }
                /* we have a protocol header, therefore URL is absolute */
                return strdup(url);
damienf's avatar
damienf committed
471
            }
472
            // not a valid protocol header, assume relative URL
473
        }
474

damienf's avatar
damienf committed
475
476
relativeurl:

damienf's avatar
damienf committed
477
478
479
        if( psz_baseURL )
        {
            size_t baseLen = strlen(psz_baseURL);
Jean-Paul Saman's avatar
Jean-Paul Saman committed
480
            char *href = (char *) malloc(baseLen+strlen(url)+1);
damienf's avatar
damienf committed
481
482
483
            if( href )
            {
                /* prepend base URL */
JP Dinger's avatar
JP Dinger committed
484
                memcpy(href, psz_baseURL, baseLen+1);
damienf's avatar
damienf committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498

                /*
                ** relative url could be empty,
                ** in which case return base URL
                */
                if( '\0' == *url )
                    return href;

                /*
                ** locate pathname part of base URL
                */

                /* skip over protocol part  */
                char *pathstart = strchr(href, ':');
JP Dinger's avatar
JP Dinger committed
499
                char *pathend = href+baseLen;
damienf's avatar
damienf committed
500
                if( pathstart )
damienf's avatar
damienf committed
501
502
503
                {
                    if( '/' == *(++pathstart) )
                    {
damienf's avatar
damienf committed
504
505
506
507
508
509
510
511
512
513
514
515
                        if( '/' == *(++pathstart) )
                        {
                            ++pathstart;
                        }
                    }
                    /* skip over host part */
                    pathstart = strchr(pathstart, '/');
                    if( ! pathstart )
                    {
                        // no path, add a / past end of url (over '\0')
                        pathstart = pathend;
                        *pathstart = '/';
damienf's avatar
damienf committed
516
517
                    }
                }
damienf's avatar
damienf committed
518
                else
damienf's avatar
damienf committed
519
                {
damienf's avatar
damienf committed
520
521
522
523
                    /* baseURL is just a UNIX path */
                    if( '/' != *href )
                    {
                        /* baseURL is not an absolute path */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
524
                        free(href);
damienf's avatar
damienf committed
525
526
527
                        return NULL;
                    }
                    pathstart = href;
damienf's avatar
damienf committed
528
529
530
531
532
533
534
535
536
537
                }

                /* relative URL made of an absolute path ? */
                if( '/' == *url )
                {
                    /* replace path completely */
                    strcpy(pathstart, url);
                    return href;
                }

538
                /* find last path component and replace it */
damienf's avatar
damienf committed
539
540
                while( '/' != *pathend)
                    --pathend;
damienf's avatar
damienf committed
541
542
543
544
545
546
547
548
549
550
551
552

                /*
                ** if relative url path starts with one or more '../',
                ** factor them out of href so that we return a
                ** normalized URL
                */
                while( pathend != pathstart )
                {
                    const char *p = url;
                    if( '.' != *p )
                        break;
                    ++p;
damienf's avatar
damienf committed
553
554
555
556
                    if( '\0' == *p  )
                    {
                        /* relative url is just '.' */
                        url = p;
damienf's avatar
damienf committed
557
                        break;
damienf's avatar
damienf committed
558
559
560
561
562
563
564
                    }
                    if( '/' == *p  )
                    {
                        /* relative url starts with './' */
                        url = ++p;
                        continue;
                    }
565
                    if( '.' != *p )
damienf's avatar
damienf committed
566
567
                        break;
                    ++p;
damienf's avatar
damienf committed
568
569
570
571
572
573
                    if( '\0' == *p )
                    {
                        /* relative url is '..' */
                    }
                    else
                    {
574
                        if( '/' != *p )
damienf's avatar
damienf committed
575
576
577
578
                            break;
                        /* relative url starts with '../' */
                        ++p;
                    }
damienf's avatar
damienf committed
579
                    url = p;
damienf's avatar
damienf committed
580
581
582
583
584
                    do
                    {
                        --pathend;
                    }
                    while( '/' != *pathend );
damienf's avatar
damienf committed
585
                }
damienf's avatar
damienf committed
586
587
                /* skip over '/' separator */
                ++pathend;
damienf's avatar
damienf committed
588
                /* concatenate remaining base URL and relative URL */
damienf's avatar
damienf committed
589
                strcpy(pathend, url);
damienf's avatar
damienf committed
590
591
592
            }
            return href;
        }
593
    }
damienf's avatar
damienf committed
594
595
    return NULL;
}
596

damienf's avatar
damienf committed
597
598
599
600
601
602
603
604
605
606
#if XP_UNIX
int  VlcPlugin::setSize(unsigned width, unsigned height)
{
    int diff = (width != i_width) || (height != i_height);

    i_width = width;
    i_height = height;

    /* return size */
    return diff;
607
608
}

609
#define BTN_SPACE ((unsigned int)4)
610
611
612
613
void VlcPlugin::showToolbar()
{
    const NPWindow& window = getWindow();
    Window control = getControlWindow();
614
    Window video = getVideoWindow();
615
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
616
    unsigned int i_height = 0, i_width = BTN_SPACE;
617
618

    /* load icons */
619
620
621
    if( !p_btnPlay )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
                            &p_btnPlay, NULL, NULL);
622
    if( p_btnPlay )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
623
624
625
    {
        i_height = __MAX( i_height, p_btnPlay->height );
    }
626
627
628
    if( !p_btnPause )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
                            &p_btnPause, NULL, NULL);
629
    if( p_btnPause )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
630
631
632
    {
        i_height = __MAX( i_height, p_btnPause->height );
    }
633
634
    i_width += __MAX( p_btnPause->width, p_btnPlay->width );

635
636
637
    if( !p_btnStop )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/stop.xpm",
                            &p_btnStop, NULL, NULL );
638
    if( p_btnStop )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
639
640
    {
        i_height = __MAX( i_height, p_btnStop->height );
641
        i_width += BTN_SPACE + p_btnStop->width;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
642
    }
643
644
645
    if( !p_timeline )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_line.xpm",
                            &p_timeline, NULL, NULL);
646
    if( p_timeline )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
647
648
    {
        i_height = __MAX( i_height, p_timeline->height );
649
        i_width += BTN_SPACE + p_timeline->width;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
650
    }
651
652
653
    if( !p_btnTime )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/time_icon.xpm",
                            &p_btnTime, NULL, NULL);
654
    if( p_btnTime )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
655
656
    {
        i_height = __MAX( i_height, p_btnTime->height );
657
        i_width += BTN_SPACE + p_btnTime->width;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
658
    }
659
660
661
    if( !p_btnFullscreen )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/fullscreen.xpm",
                            &p_btnFullscreen, NULL, NULL);
662
    if( p_btnFullscreen )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
663
664
    {
        i_height = __MAX( i_height, p_btnFullscreen->height );
665
        i_width += BTN_SPACE + p_btnFullscreen->width;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
666
    }
667
668
669
    if( !p_btnMute )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_max.xpm",
                            &p_btnMute, NULL, NULL);
670
    if( p_btnMute )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
671
672
673
    {
        i_height = __MAX( i_height, p_btnMute->height );
    }
674
675
676
    if( !p_btnUnmute )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/volume_mute.xpm",
                            &p_btnUnmute, NULL, NULL);
677
    if( p_btnUnmute )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
678
679
680
    {
        i_height = __MAX( i_height, p_btnUnmute->height );
    }
681
682
    i_width += BTN_SPACE + __MAX( p_btnUnmute->width, p_btnMute->width );

Jean-Paul Saman's avatar
Jean-Paul Saman committed
683
    setToolbarSize( i_width, i_height );
684
685
686
687

    if( !p_btnPlay || !p_btnPause || !p_btnStop || !p_timeline ||
        !p_btnTime || !p_btnFullscreen || !p_btnMute || !p_btnUnmute )
        fprintf(stderr, "Error: some button images not found in %s\n", DATA_PATH );
688
689
690
691
692
693
694
695
696

    /* reset panels position and size */
    /* XXX  use i_width */
    XResizeWindow( p_display, video, window.width, window.height - i_height);
    XMoveWindow( p_display, control, 0, window.height - i_height );
    XResizeWindow( p_display, control, window.width, i_height -1);

    b_toolbar = 1; /* says toolbar is now shown */
    redrawToolbar();
697
698
699
700
}

void VlcPlugin::hideToolbar()
{
701
702
703
704
705
    const NPWindow& window = getWindow();
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
    Window control = getControlWindow();
    Window video = getVideoWindow();

Jean-Paul Saman's avatar
Jean-Paul Saman committed
706
707
    i_tb_width = i_tb_height = 0;

708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    if( p_btnPlay )  XDestroyImage( p_btnPlay );
    if( p_btnPause ) XDestroyImage( p_btnPause );
    if( p_btnStop )  XDestroyImage( p_btnStop );
    if( p_timeline ) XDestroyImage( p_timeline );
    if( p_btnTime )  XDestroyImage( p_btnTime );
    if( p_btnFullscreen ) XDestroyImage( p_btnFullscreen );
    if( p_btnMute )  XDestroyImage( p_btnMute );
    if( p_btnUnmute ) XDestroyImage( p_btnUnmute );

    p_btnPlay = NULL;
    p_btnPause = NULL;
    p_btnStop = NULL;
    p_timeline = NULL;
    p_btnTime = NULL;
    p_btnFullscreen = NULL;
    p_btnMute = NULL;
    p_btnUnmute = NULL;
725
726
727
728
729
730
731
732
733

    /* reset panels position and size */
    /* XXX  use i_width */
    XResizeWindow( p_display, video, window.width, window.height );
    XMoveWindow( p_display, control, 0, window.height-1 );
    XResizeWindow( p_display, control, window.width, 1 );

    b_toolbar = 0; /* says toolbar is now hidden */
    redrawToolbar();
734
735
736
737
738
}

void VlcPlugin::redrawToolbar()
{
    libvlc_exception_t ex;
739
    int is_playing = 0;
740
    bool b_mute = false;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
741
    unsigned int dst_x, dst_y;
742
743
    GC gc;
    XGCValues gcv;
744
    unsigned int i_tb_width, i_tb_height;
745

746
747
748
749
    /* This method does nothing if toolbar is hidden. */
    if( !b_toolbar )
        return;

750
751
752
753
    const NPWindow& window = getWindow();
    Window control = getControlWindow();
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;

754
755
    getToolbarSize( &i_tb_width, &i_tb_height );

756
757
758
759
760
761
762
763
764
765
    libvlc_exception_init( &ex );

    /* get mute info */
    b_mute = libvlc_audio_get_mute( getVLC(), &ex );
    libvlc_exception_clear( &ex );

    gcv.foreground = BlackPixel( p_display, 0 );
    gc = XCreateGC( p_display, control, GCForeground, &gcv );

    XFillRectangle( p_display, control, gc,
Jean-Paul Saman's avatar
Jean-Paul Saman committed
766
                    0, 0, window.width, i_tb_height );
767
768
769
770
    gcv.foreground = WhitePixel( p_display, 0 );
    XChangeGC( p_display, gc, GCForeground, &gcv );

    /* position icons */
771
772
    dst_x = BTN_SPACE;
    dst_y = i_tb_height >> 1; /* baseline = vertical middle */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
773

774
    if( p_btnPause && (is_playing == 1) )
775
    {
776
777
        XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
                   dst_y - (p_btnPause->height >> 1),
778
                   p_btnPause->width, p_btnPause->height );
779
        dst_x += BTN_SPACE + p_btnPause->width;
780
    }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
781
    else if( p_btnPlay )
782
    {
783
784
        XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
                   dst_y - (p_btnPlay->height >> 1),
785
                   p_btnPlay->width, p_btnPlay->height );
786
        dst_x += BTN_SPACE + p_btnPlay->width;
787
788
789
    }

    if( p_btnStop )
790
791
        XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
                   dst_y - (p_btnStop->height >> 1),
792
                   p_btnStop->width, p_btnStop->height );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
793
794
795

    dst_x += BTN_SPACE + ( p_btnStop ? p_btnStop->width : 0 );

796
    if( p_btnFullscreen )
797
798
        XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
                   dst_y - (p_btnFullscreen->height >> 1),
799
800
                   p_btnFullscreen->width, p_btnFullscreen->height );

Jean-Paul Saman's avatar
Jean-Paul Saman committed
801
802
    dst_x += BTN_SPACE + ( p_btnFullscreen ? p_btnFullscreen->width : 0 );

803
804
    if( p_btnUnmute && b_mute )
    {
805
806
        XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
                   dst_y - (p_btnUnmute->height >> 1),
807
                   p_btnUnmute->width, p_btnUnmute->height );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
808
809

        dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
810
811
812
    }
    else if( p_btnMute )
    {
813
814
        XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
                   dst_y - (p_btnMute->height >> 1),
815
                   p_btnMute->width, p_btnMute->height );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
816
817

        dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
818
819
820
    }

    if( p_timeline )
821
822
        XPutImage( p_display, control, gc, p_timeline, 0, 0, dst_x,
                   dst_y - (p_timeline->height >> 1),
Jean-Paul Saman's avatar
Jean-Paul Saman committed
823
                   (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
824

825
826
827
828
829
830
831
    /* get movie position in % */
    if( playlist_isplaying(&ex) )
    {
        i_last_position = (int)((window.width-(dst_x+BTN_SPACE))*
                   libvlc_media_player_get_position(libvlc_media_player,&ex));
    }
    libvlc_exception_clear( &ex );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
832

833
834
    if( p_btnTime )
        XPutImage( p_display, control, gc, p_btnTime,
835
836
                   0, 0, (dst_x+i_last_position),
                   dst_y - (p_btnTime->height >> 1),
837
838
839
840
                   p_btnTime->width, p_btnTime->height );

    XFreeGC( p_display, gc );
}
841
842
843

vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
844
    unsigned int i_dest = BTN_SPACE;
845
    int is_playing = 0;
846
847
848
    bool b_mute = false;
    libvlc_exception_t ex;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
849
#ifndef NDEBUG
850
851
852
    fprintf( stderr, "ToolbarButtonClicked:: "
                     "trying to match (%d,%d) (%d,%d)\n",
             i_xpos, i_ypos, i_tb_height, i_tb_width );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
853
#endif
854
    if( i_ypos >= i_tb_width )
855
856
857
858
859
860
861
862
863
        return clicked_Unknown;

    /* Note: the order of testing is dependend on the original
     * drawing positions of the icon buttons. Buttons are tested
     * left to right.
     */

    /* get isplaying */
    libvlc_exception_init( &ex );
864
    is_playing = playlist_isplaying( &ex );
865
866
867
868
869
870
871
    libvlc_exception_clear( &ex );

    /* get mute info */
    b_mute = libvlc_audio_get_mute( getVLC(), &ex );
    libvlc_exception_clear( &ex );

    /* is Pause of Play button clicked */
872
    if( (is_playing != 1) &&
873
874
        (i_xpos >= (BTN_SPACE>>1)) &&
        (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
875
        return clicked_Play;
876
877
    else if( (i_xpos >= (BTN_SPACE>>1))  &&
             (i_xpos <= i_dest + p_btnPause->width) )
878
879
880
        return clicked_Pause;

    /* is Stop button clicked */
881
    if( is_playing != 1 )
882
        i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
883
    else
884
885
886
887
        i_dest += (p_btnPause->width + (BTN_SPACE>>1));

    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
888
889
890
891
        return clicked_Stop;

    /* is Fullscreen button clicked */
    i_dest += (p_btnStop->width + (BTN_SPACE>>1));
892
893
    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
894
895
896
897
        return clicked_Fullscreen;

    /* is Mute or Unmute button clicked */
    i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
898
899
    if( !b_mute && (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
900
        return clicked_Mute;
901
902
    else if( (i_xpos >= i_dest) &&
             (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
903
904
905
906
907
908
909
        return clicked_Unmute;

    /* is timeline clicked */
    if( !b_mute )
        i_dest += (p_btnMute->width + (BTN_SPACE>>1));
    else
        i_dest += (p_btnUnmute->width + (BTN_SPACE>>1));
910
911
    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
912
913
914
915
        return clicked_timeline;

    /* is time button clicked */
    i_dest += (p_timeline->width + (BTN_SPACE>>1));
916
917
    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
918
919
920
921
922
        return clicked_Time;

    return clicked_Unknown;
}
#undef BTN_SPACE
923
#endif