vlcplugin.cpp 28.1 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
Antoine Cellerier's avatar
Antoine Cellerier 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"
36
#include "control/npolibvlc.h"
37

38 39
#include <ctype.h>

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

82
static bool boolValue(const char *value) {
83
    return ( !strcmp(value, "1") ||
84 85 86
             !strcasecmp(value, "true") ||
             !strcasecmp(value, "yes") );
}
87

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

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

98 99
    /* locate VLC module path */
#ifdef XP_MACOSX
100 101
    ppsz_argv[ppsz_argc++] = "--plugin-path=/Library/Internet\\ Plug-Ins/VLC\\ Plugin.plugin/Contents/MacOS/modules";
    ppsz_argv[ppsz_argc++] = "--vout=macosx";
102 103 104 105 106 107
#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 )
108
    {
109 110 111 112 113
         if( RegQueryValueEx( h_key, "InstallDir", 0, &i_type,
                              (LPBYTE)p_data, &i_data ) == ERROR_SUCCESS )
         {
             if( i_type == REG_SZ )
             {
114
                 strcat( p_data, "\\plugins" );
115 116
                 ppsz_argv[ppsz_argc++] = "--plugin-path";
                 ppsz_argv[ppsz_argc++] = p_data;
117 118 119
             }
         }
         RegCloseKey( h_key );
120
    }
121
    ppsz_argv[ppsz_argc++] = "--no-one-instance";
122

123
#endif /* XP_MACOSX */
124

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

133
    const char *progid = NULL;
134 135 136 137

    /* parse plugin arguments */
    for( int i = 0; i < argc ; i++ )
    {
138
       /* fprintf(stderr, "argn=%s, argv=%s\n", argn[i], argv[i]); */
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

        if( !strcmp( argn[i], "target" )
         || !strcmp( argn[i], "mrl")
         || !strcmp( argn[i], "filename")
         || !strcmp( argn[i], "src") )
        {
            psz_target = argv[i];
        }
        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]) )
            {
167
                ppsz_argv[ppsz_argc++] = "--volume=0";
168 169 170 171 172 173 174 175 176
            }
        }
        else if( !strcmp( argn[i], "loop")
              || !strcmp( argn[i], "autoloop") )
        {
            if( boolValue(argv[i]) )
            {
                ppsz_argv[ppsz_argc++] = "--loop";
            }
177 178
            else
            {
179 180 181
                ppsz_argv[ppsz_argc++] = "--no-loop";
            }
        }
182 183
        else if( !strcmp( argn[i], "version")
              || !strcmp( argn[i], "progid") )
Damien Fouilleul's avatar
Damien Fouilleul committed
184
        {
185
            progid = argv[i];
Damien Fouilleul's avatar
Damien Fouilleul committed
186
        }
Jean-Paul Saman's avatar
Jean-Paul Saman committed
187
        else if( !strcmp( argn[i], "toolbar" ) )
188
        {
189
/* FIXME: Remove this when toolbar functionality has been implemented on
190 191
 * MacOS X and Win32 for Firefox/Mozilla/Safari. */
#ifdef XP_UNIX
Jean-Paul Saman's avatar
Jean-Paul Saman committed
192
            b_toolbar = boolValue(argv[i]);
193
#endif
194
        }
195 196
    }

197 198
    libvlc_exception_t ex;
    libvlc_exception_init(&ex);
199

200
    libvlc_instance = libvlc_new(ppsz_argc, ppsz_argv, &ex);
201 202 203 204 205 206 207
    if( libvlc_exception_raised(&ex) )
    {
        libvlc_exception_clear(&ex);
        return NPERR_GENERIC_ERROR;
    }

    libvlc_media_list = libvlc_media_list_new(libvlc_instance,&ex);
208
    if( libvlc_exception_raised(&ex) )
209
    {
210
        libvlc_exception_clear(&ex);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
        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
    */
    NPObject *plugin;

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

239
                psz_baseURL = static_cast<char*>(malloc(location.utf8length+1));
240 241 242 243 244 245 246 247 248 249 250 251 252 253
                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
254 255
        char *psz_absurl = getAbsoluteURL(psz_target);
        psz_target = psz_absurl ? psz_absurl : strdup(psz_target);
256 257 258
    }

    /* assign plugin script root class */
259 260
    /* new APIs */
    p_scriptClass = RuntimeNPClass<LibvlcRootNPObject>::getClass();
261 262

    return NPERR_NO_ERROR;
263 264
}

265
VlcPlugin::~VlcPlugin()
266
{
267 268
    free(psz_baseURL);
    free(psz_target);
269 270 271 272
    if( libvlc_media_player )
        libvlc_media_player_release( libvlc_media_player );
    if( libvlc_media_list )
        libvlc_media_list_release( libvlc_media_list );
273
    if( libvlc_instance )
274
        libvlc_release(libvlc_instance);
275 276
}

277 278 279 280 281 282 283
/*****************************************************************************
 * VlcPlugin playlist replacement methods
 *****************************************************************************/
void VlcPlugin::set_player_window( libvlc_exception_t *ex )
{
#ifdef XP_UNIX
    libvlc_media_player_set_xwindow(libvlc_media_player,
284
                                    (libvlc_drawable_t)getVideoWindow(),
285 286 287 288 289 290 291
                                    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
292
                                 getWindow().window,
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
                                 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 )
    {
325
        libvlc_media_add_option_flag(p_m, optv[i], libvlc_media_option_unique, ex);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 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;
}

int  VlcPlugin::player_has_vout( libvlc_exception_t *ex )
{
    int r = 0;
    if( playlist_isplaying(ex) )
        r = libvlc_media_player_has_vout(libvlc_media_player, ex);
    return r;
}

431 432 433
/*****************************************************************************
 * VlcPlugin methods
 *****************************************************************************/
434

435
char *VlcPlugin::getAbsoluteURL(const char *url)
436
{
437
    if( NULL != url )
438
    {
439 440 441
        // check whether URL is already absolute
        const char *end=strchr(url, ':');
        if( (NULL != end) && (end != url) )
442
        {
443 444
            // validate protocol header
            const char *start = url;
445 446 447
            char c = *start;
            if( isalpha(c) )
            {
448
                ++start;
449 450 451 452 453 454 455 456 457 458 459 460 461 462
                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);
463
            }
464
            // not a valid protocol header, assume relative URL
465
        }
466

467 468
relativeurl:

469 470 471
        if( psz_baseURL )
        {
            size_t baseLen = strlen(psz_baseURL);
472
            char *href = static_cast<char*>(malloc(baseLen+strlen(url)+1));
473 474 475
            if( href )
            {
                /* prepend base URL */
JP Dinger's avatar
JP Dinger committed
476
                memcpy(href, psz_baseURL, baseLen+1);
477 478 479 480 481 482 483 484 485 486 487 488 489 490

                /*
                ** 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
491
                char *pathend = href+baseLen;
492
                if( pathstart )
493 494 495
                {
                    if( '/' == *(++pathstart) )
                    {
496 497 498 499 500 501 502 503 504 505 506 507
                        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 = '/';
508 509
                    }
                }
510
                else
511
                {
512 513 514 515
                    /* baseURL is just a UNIX path */
                    if( '/' != *href )
                    {
                        /* baseURL is not an absolute path */
516
		        free(href);
517 518 519
                        return NULL;
                    }
                    pathstart = href;
520 521 522 523 524 525 526 527 528 529
                }

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

530
                /* find last path component and replace it */
531 532
                while( '/' != *pathend)
                    --pathend;
533 534 535 536 537 538 539 540 541 542 543 544

                /*
                ** 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;
545 546 547 548
                    if( '\0' == *p  )
                    {
                        /* relative url is just '.' */
                        url = p;
549
                        break;
550 551 552 553 554 555 556
                    }
                    if( '/' == *p  )
                    {
                        /* relative url starts with './' */
                        url = ++p;
                        continue;
                    }
557
                    if( '.' != *p )
558 559
                        break;
                    ++p;
560 561 562 563 564 565
                    if( '\0' == *p )
                    {
                        /* relative url is '..' */
                    }
                    else
                    {
566
                        if( '/' != *p )
567 568 569 570
                            break;
                        /* relative url starts with '../' */
                        ++p;
                    }
571
                    url = p;
572 573 574 575 576
                    do
                    {
                        --pathend;
                    }
                    while( '/' != *pathend );
577
                }
578 579
                /* skip over '/' separator */
                ++pathend;
580
                /* concatenate remaining base URL and relative URL */
581
                strcpy(pathend, url);
582 583 584
            }
            return href;
        }
585
    }
586 587
    return NULL;
}
588

589 590 591 592 593 594 595 596 597 598
#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;
599 600
}

601
#define BTN_SPACE ((unsigned int)4)
602 603 604 605
void VlcPlugin::showToolbar()
{
    const NPWindow& window = getWindow();
    Window control = getControlWindow();
606
    Window video = getVideoWindow();
607
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;
608
    unsigned int i_height = 0, i_width = BTN_SPACE;
609 610

    /* load icons */
611 612 613
    if( !p_btnPlay )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/play.xpm",
                            &p_btnPlay, NULL, NULL);
614
    if( p_btnPlay )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
615 616 617
    {
        i_height = __MAX( i_height, p_btnPlay->height );
    }
618 619 620
    if( !p_btnPause )
        XpmReadFileToImage( p_display, DATA_PATH "/mozilla/pause.xpm",
                            &p_btnPause, NULL, NULL);
621
    if( p_btnPause )
Jean-Paul Saman's avatar
Jean-Paul Saman committed
622 623 624
    {
        i_height = __MAX( i_height, p_btnPause->height );
    }
625 626
    i_width += __MAX( p_btnPause->width, p_btnPlay->width );

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

Jean-Paul Saman's avatar
Jean-Paul Saman committed
675
    setToolbarSize( i_width, i_height );
676 677 678 679

    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 );
680 681 682 683 684 685 686 687 688

    /* 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();
689 690 691 692
}

void VlcPlugin::hideToolbar()
{
693 694 695 696 697
    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
698 699
    i_tb_width = i_tb_height = 0;

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    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;
717 718 719 720 721 722 723 724 725

    /* 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();
726 727 728 729 730
}

void VlcPlugin::redrawToolbar()
{
    libvlc_exception_t ex;
731
    int is_playing = 0;
732
    bool b_mute = false;
Jean-Paul Saman's avatar
Jean-Paul Saman committed
733
    unsigned int dst_x, dst_y;
734 735
    GC gc;
    XGCValues gcv;
736
    unsigned int i_tb_width, i_tb_height;
737

738 739 740 741
    /* This method does nothing if toolbar is hidden. */
    if( !b_toolbar )
        return;

742 743 744 745
    const NPWindow& window = getWindow();
    Window control = getControlWindow();
    Display *p_display = ((NPSetWindowCallbackStruct *)window.ws_info)->display;

746 747
    getToolbarSize( &i_tb_width, &i_tb_height );

748 749 750 751 752 753 754 755 756 757
    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
758
                    0, 0, window.width, i_tb_height );
759 760 761 762
    gcv.foreground = WhitePixel( p_display, 0 );
    XChangeGC( p_display, gc, GCForeground, &gcv );

    /* position icons */
763 764
    dst_x = BTN_SPACE;
    dst_y = i_tb_height >> 1; /* baseline = vertical middle */
Jean-Paul Saman's avatar
Jean-Paul Saman committed
765

766
    if( p_btnPause && (is_playing == 1) )
767
    {
768 769
        XPutImage( p_display, control, gc, p_btnPause, 0, 0, dst_x,
                   dst_y - (p_btnPause->height >> 1),
770
                   p_btnPause->width, p_btnPause->height );
771
        dst_x += BTN_SPACE + p_btnPause->width;
772
    }
773
    else if( p_btnPlay )
774
    {
775 776
        XPutImage( p_display, control, gc, p_btnPlay, 0, 0, dst_x,
                   dst_y - (p_btnPlay->height >> 1),
777
                   p_btnPlay->width, p_btnPlay->height );
778
        dst_x += BTN_SPACE + p_btnPlay->width;
779 780 781
    }

    if( p_btnStop )
782 783
        XPutImage( p_display, control, gc, p_btnStop, 0, 0, dst_x,
                   dst_y - (p_btnStop->height >> 1),
784
                   p_btnStop->width, p_btnStop->height );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
785 786 787

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

788
    if( p_btnFullscreen )
789 790
        XPutImage( p_display, control, gc, p_btnFullscreen, 0, 0, dst_x,
                   dst_y - (p_btnFullscreen->height >> 1),
791 792
                   p_btnFullscreen->width, p_btnFullscreen->height );

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

795 796
    if( p_btnUnmute && b_mute )
    {
797 798
        XPutImage( p_display, control, gc, p_btnUnmute, 0, 0, dst_x,
                   dst_y - (p_btnUnmute->height >> 1),
799
                   p_btnUnmute->width, p_btnUnmute->height );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
800 801

        dst_x += BTN_SPACE + ( p_btnUnmute ? p_btnUnmute->width : 0 );
802 803 804
    }
    else if( p_btnMute )
    {
805 806
        XPutImage( p_display, control, gc, p_btnMute, 0, 0, dst_x,
                   dst_y - (p_btnMute->height >> 1),
807
                   p_btnMute->width, p_btnMute->height );
Jean-Paul Saman's avatar
Jean-Paul Saman committed
808 809

        dst_x += BTN_SPACE + ( p_btnMute ? p_btnMute->width : 0 );
810 811 812
    }

    if( p_timeline )
813 814
        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
815
                   (window.width-(dst_x+BTN_SPACE)), p_timeline->height );
816

817 818 819 820 821 822 823
    /* 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
824

825 826
    if( p_btnTime )
        XPutImage( p_display, control, gc, p_btnTime,
827 828
                   0, 0, (dst_x+i_last_position),
                   dst_y - (p_btnTime->height >> 1),
829 830 831 832
                   p_btnTime->width, p_btnTime->height );

    XFreeGC( p_display, gc );
}
833 834 835

vlc_toolbar_clicked_t VlcPlugin::getToolbarButtonClicked( int i_xpos, int i_ypos )
{
Jean-Paul Saman's avatar
Jean-Paul Saman committed
836
    unsigned int i_dest = BTN_SPACE;
837
    int is_playing = 0;
838 839 840
    bool b_mute = false;
    libvlc_exception_t ex;

Jean-Paul Saman's avatar
Jean-Paul Saman committed
841
#ifndef NDEBUG
842 843 844
    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
845
#endif
846
    if( i_ypos >= i_tb_width )
847 848 849 850 851 852 853 854 855
        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 );
856
    is_playing = playlist_isplaying( &ex );
857 858 859 860 861 862 863
    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 */
864
    if( (is_playing != 1) &&
865 866
        (i_xpos >= (BTN_SPACE>>1)) &&
        (i_xpos <= i_dest + p_btnPlay->width + (BTN_SPACE>>1)) )
867
        return clicked_Play;
868 869
    else if( (i_xpos >= (BTN_SPACE>>1))  &&
             (i_xpos <= i_dest + p_btnPause->width) )
870 871 872
        return clicked_Pause;

    /* is Stop button clicked */
873
    if( is_playing != 1 )
874
        i_dest += (p_btnPlay->width + (BTN_SPACE>>1));
875
    else
876 877 878 879
        i_dest += (p_btnPause->width + (BTN_SPACE>>1));

    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnStop->width + (BTN_SPACE>>1)) )
880 881 882 883
        return clicked_Stop;

    /* is Fullscreen button clicked */
    i_dest += (p_btnStop->width + (BTN_SPACE>>1));
884 885
    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnFullscreen->width + (BTN_SPACE>>1)) )
886 887 888 889
        return clicked_Fullscreen;

    /* is Mute or Unmute button clicked */
    i_dest += (p_btnFullscreen->width + (BTN_SPACE>>1));
890 891
    if( !b_mute && (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnMute->width + (BTN_SPACE>>1)) )
892
        return clicked_Mute;
893 894
    else if( (i_xpos >= i_dest) &&
             (i_xpos <= i_dest + p_btnUnmute->width + (BTN_SPACE>>1)) )
895 896 897 898 899 900 901
        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));
902 903
    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_timeline->width + (BTN_SPACE>>1)) )
904 905 906 907
        return clicked_timeline;

    /* is time button clicked */
    i_dest += (p_timeline->width + (BTN_SPACE>>1));
908 909
    if( (i_xpos >= i_dest) &&
        (i_xpos <= i_dest + p_btnTime->width + (BTN_SPACE>>1)) )
910 911 912 913 914
        return clicked_Time;

    return clicked_Unknown;
}
#undef BTN_SPACE
915
#endif