VideoOutput.cpp 47.8 KB
Newer Older
1
/*****************************************************************************
2
 * vout_beos.cpp: beos video output display method
3 4
 *****************************************************************************
 * Copyright (C) 2000, 2001 VideoLAN
5
 * $Id$
6 7 8 9 10
 *
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Tony Castley <tcastley@mail.powerup.com.au>
 *          Richard Shepherd <richard@rshepherd.demon.co.uk>
11
 *          Stephan Aßmus <stippi@yellowbites.com>
12 13 14 15 16
 *
 * 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.
Sam Hocevar's avatar
Sam Hocevar committed
17
 *
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */
#include <stdlib.h>                                                /* free() */
#include <stdio.h>
#include <string.h>                                            /* strerror() */
35

36
#include <Application.h>
37
#include <BitmapStream.h>
38
#include <Bitmap.h>
Stephan Assmus's avatar
 
Stephan Assmus committed
39
#include <Directory.h>
40 41 42 43 44 45
#include <DirectWindow.h>
#include <File.h>
#include <InterfaceKit.h>
#include <NodeInfo.h>
#include <String.h>
#include <TranslatorRoster.h>
46
#include <WindowScreen.h>
47

48
/* VLC headers */
49 50 51
#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <vlc/vout.h>
52
#include <vlc_keys.h>
53

54
#include "InterfaceWindow.h"    // for load/save_settings()
55 56 57
#include "DrawingTidbits.h"
#include "MsgVals.h"

58 59
#include "VideoWindow.h"

60 61 62 63 64 65 66 67 68 69
/*****************************************************************************
 * vout_sys_t: BeOS video output method descriptor
 *****************************************************************************
 * This structure is part of the video output thread descriptor.
 * It describes the BeOS specific properties of an output thread.
 *****************************************************************************/
struct vout_sys_t
{
    VideoWindow *  p_window;

Sam Hocevar's avatar
Sam Hocevar committed
70 71
    int32_t i_width;
    int32_t i_height;
72

Sam Hocevar's avatar
Sam Hocevar committed
73 74
//    uint8_t *pp_buffer[3];
    uint32_t source_chroma;
75
    int i_index;
76

77 78
};

79 80
#define MOUSE_IDLE_TIMEOUT 2000000    // two seconds
#define MIN_AUTO_VSYNC_REFRESH 61    // Hz
81

82 83 84
/*****************************************************************************
 * beos_GetAppWindow : retrieve a BWindow pointer from the window name
 *****************************************************************************/
85 86
BWindow*
beos_GetAppWindow(char *name)
87
{
Sam Hocevar's avatar
Sam Hocevar committed
88
    int32_t     index;
89
    BWindow     *window;
Sam Hocevar's avatar
Sam Hocevar committed
90

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    for (index = 0 ; ; index++)
    {
        window = be_app->WindowAt(index);
        if (window == NULL)
            break;
        if (window->LockWithTimeout(20000) == B_OK)
        {
            if (strcmp(window->Name(), name) == 0)
            {
                window->Unlock();
                break;
            }
            window->Unlock();
        }
    }
Sam Hocevar's avatar
Sam Hocevar committed
106
    return window;
107 108
}

109
static const int beos_keys[][2] =
110
{
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    { B_LEFT_ARROW,  KEY_LEFT },
    { B_RIGHT_ARROW, KEY_RIGHT },
    { B_UP_ARROW,    KEY_UP },
    { B_DOWN_ARROW,  KEY_DOWN },
    { B_SPACE,       KEY_SPACE },
    { B_ENTER,       KEY_ENTER },
    { B_F1_KEY,      KEY_F1 },
    { B_F2_KEY,      KEY_F2 },
    { B_F3_KEY,      KEY_F3 },
    { B_F4_KEY,      KEY_F4 },
    { B_F5_KEY,      KEY_F5 },
    { B_F6_KEY,      KEY_F6 },
    { B_F7_KEY,      KEY_F7 },
    { B_F8_KEY,      KEY_F8 },
    { B_F9_KEY,      KEY_F9 },
    { B_F10_KEY,     KEY_F10 },
    { B_F11_KEY,     KEY_F11 },
    { B_F12_KEY,     KEY_F12 },
    { B_HOME,        KEY_HOME },
    { B_END,         KEY_END },
    { B_ESCAPE,      KEY_ESC },
    { B_PAGE_UP,     KEY_PAGEUP },
    { B_PAGE_DOWN,   KEY_PAGEDOWN },
    { B_TAB,         KEY_TAB },
    { B_BACKSPACE,   KEY_BACKSPACE }
};

static int ConvertKeyFromVLC( int key )
{
    for( unsigned i = 0; i < sizeof( beos_keys ) / sizeof( int ) / 2; i++ )
    {
        if( beos_keys[i][1] == key )
        {
            return beos_keys[i][0];
        }
    }
    return key;
}

static int ConvertKeyToVLC( int key )
{
    for( unsigned i = 0; i < sizeof( beos_keys ) / sizeof( int ) / 2; i++ )
153
    {
154 155 156 157
        if( beos_keys[i][0] == key )
        {
            return beos_keys[i][1];
        }
158 159 160 161
    }
    return key;
}

162 163 164 165 166 167
/*****************************************************************************
 * get_interface_window
 *****************************************************************************/
BWindow*
get_interface_window()
{
168
    return beos_GetAppWindow( "VLC " PACKAGE_VERSION );
169 170 171 172 173
}

class BackgroundView : public BView
{
 public:
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
                            BackgroundView(BRect frame, VLCView* view)
                            : BView(frame, "background",
                                    B_FOLLOW_ALL, B_FULL_UPDATE_ON_RESIZE),
                              fVideoView(view)
                            {
                                SetViewColor(kBlack);
                            }
    virtual                    ~BackgroundView() {}

    virtual    void            MouseDown(BPoint where)
                            {
                                // convert coordinates
                                where = fVideoView->ConvertFromParent(where);
                                // let him handle it
                                fVideoView->MouseDown(where);
                            }
    virtual    void            MouseMoved(BPoint where, uint32_t transit,
                                       const BMessage* dragMessage)
                            {
                                // convert coordinates
                                where = fVideoView->ConvertFromParent(where);
                                // let him handle it
                                fVideoView->MouseMoved(where, transit, dragMessage);
                                // notice: It might look like transit should be
                                // B_OUTSIDE_VIEW regardless, but leave it like this,
                                // otherwise, unwanted things will happen!
                            }
201 202

 private:
203
    VLCView*                fVideoView;
204 205
};

206 207 208 209 210

/*****************************************************************************
 * VideoSettings constructor and destructor
 *****************************************************************************/
VideoSettings::VideoSettings()
211 212 213
    : fVideoSize( SIZE_100 ),
      fFlags( FLAG_CORRECT_RATIO ),
      fSettings( new BMessage( 'sett' ) )
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 239
    // read settings from disk
    status_t ret = load_settings( fSettings, "video_settings", "VideoLAN Client" );
    if ( ret == B_OK )
    {
        uint32_t flags;
        if ( fSettings->FindInt32( "flags", (int32*)&flags ) == B_OK )
            SetFlags( flags );
        uint32_t size;
        if ( fSettings->FindInt32( "video size", (int32*)&size ) == B_OK )
            SetVideoSize( size );
    }
    else
    {
        // figure out if we should use vertical sync by default
        BScreen screen(B_MAIN_SCREEN_ID);
        if (screen.IsValid())
        {
            display_mode mode;
            screen.GetMode(&mode);
            float refresh = (mode.timing.pixel_clock * 1000)
                            / ((mode.timing.h_total)* (mode.timing.v_total));
            if (refresh < MIN_AUTO_VSYNC_REFRESH)
                AddFlags(FLAG_SYNC_RETRACE);
        }
    }
240 241 242
}

VideoSettings::VideoSettings( const VideoSettings& clone )
243 244 245
    : fVideoSize( clone.VideoSize() ),
      fFlags( clone.Flags() ),
      fSettings( NULL )
246 247 248 249 250 251
{
}


VideoSettings::~VideoSettings()
{
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    if ( fSettings )
    {
        // we are the default settings
        // and write our settings to disk
        if (fSettings->ReplaceInt32( "video size", VideoSize() ) != B_OK)
            fSettings->AddInt32( "video size", VideoSize() );
        if (fSettings->ReplaceInt32( "flags", Flags() ) != B_OK)
            fSettings->AddInt32( "flags", Flags() );

        save_settings( fSettings, "video_settings", "VideoLAN Client" );
        delete fSettings;
    }
    else
    {
        // we are just a clone of the default settings
        fDefaultSettings.SetVideoSize( VideoSize() );
        fDefaultSettings.SetFlags( Flags() );
    }
270 271 272 273 274 275 276 277
}

/*****************************************************************************
 * VideoSettings::DefaultSettings
 *****************************************************************************/
VideoSettings*
VideoSettings::DefaultSettings()
{
278
    return &fDefaultSettings;
279 280 281 282 283 284
}

/*****************************************************************************
 * VideoSettings::SetVideoSize
 *****************************************************************************/
void
Sam Hocevar's avatar
Sam Hocevar committed
285
VideoSettings::SetVideoSize( uint32_t mode )
286
{
287
    fVideoSize = mode;
288 289 290 291 292 293 294
}

// static variable initialization
VideoSettings
VideoSettings::fDefaultSettings;


295 296 297
/*****************************************************************************
 * VideoWindow constructor and destructor
 *****************************************************************************/
298 299
VideoWindow::VideoWindow(int v_width, int v_height, BRect frame,
                         vout_thread_t *p_videoout)
300 301 302 303 304 305 306 307 308 309 310 311
    : BWindow(frame, NULL, B_TITLED_WINDOW, B_NOT_CLOSABLE | B_NOT_MINIMIZABLE),
      i_width(frame.IntegerWidth()),
      i_height(frame.IntegerHeight()),
      winSize(frame),
      i_buffer(0),
      teardownwindow(false),
      fTrueWidth(v_width),
      fTrueHeight(v_height),
      fCachedFeel(B_NORMAL_WINDOW_FEEL),
      fInterfaceShowing(false),
      fInitStatus(B_ERROR),
      fSettings(new VideoSettings(*VideoSettings::DefaultSettings()))
312
{
313
    p_vout = p_videoout;
Sam Hocevar's avatar
Sam Hocevar committed
314

315
    // create the view to do the display
316
    view = new VLCView( Bounds(), p_vout );
317

318
    // create background view
319 320 321
    BView *mainView =  new BackgroundView( Bounds(), view );
    AddChild(mainView);
    mainView->AddChild(view);
322

323 324 325 326
    // allocate bitmap buffers
    for (int32_t i = 0; i < 3; i++)
        bitmap[i] = NULL;
    fInitStatus = _AllocateBuffers(v_width, v_height, &mode);
327

328
    // make sure we layout the view correctly
329
    FrameResized(i_width, i_height);
330

331
    if (fInitStatus >= B_OK && mode == OVERLAY)
332 333 334
    {
       overlay_restrictions r;

335
       bitmap[0]->GetOverlayRestrictions(&r);
336 337
       SetSizeLimits((i_width * r.min_width_scale), i_width * r.max_width_scale,
                     (i_height * r.min_height_scale), i_height * r.max_height_scale);
338
    }
Sam Hocevar's avatar
Sam Hocevar committed
339

340 341 342
    // vlc settings override settings from disk
    if (config_GetInt(p_vout, "fullscreen"))
        fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
343 344

    _SetToSettings();
345 346 347 348
}

VideoWindow::~VideoWindow()
{
Eric Petit's avatar
Eric Petit committed
349
    int32 result;
350

351
    teardownwindow = true;
352 353
    wait_for_thread(fDrawThreadID, &result);
    _FreeBuffers();
354
    delete fSettings;
355 356 357 358 359 360 361 362
}

/*****************************************************************************
 * VideoWindow::MessageReceived
 *****************************************************************************/
void
VideoWindow::MessageReceived( BMessage *p_message )
{
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
    switch( p_message->what )
    {
        case SHOW_INTERFACE:
            SetInterfaceShowing( true );
            break;
        case TOGGLE_FULL_SCREEN:
            BWindow::Zoom();
            break;
        case RESIZE_50:
        case RESIZE_100:
        case RESIZE_200:
            if (IsFullScreen())
                BWindow::Zoom();
            _SetVideoSize(p_message->what);
            break;
        case VERT_SYNC:
            SetSyncToRetrace(!IsSyncedToRetrace());
            break;
        case WINDOW_FEEL:
            {
                window_feel winFeel;
                if (p_message->FindInt32("WinFeel", (int32*)&winFeel) == B_OK)
                {
                    SetFeel(winFeel);
                    fCachedFeel = winFeel;
                    if (winFeel == B_FLOATING_ALL_WINDOW_FEEL)
                        fSettings->AddFlags(VideoSettings::FLAG_ON_TOP_ALL);
                    else
                        fSettings->ClearFlags(VideoSettings::FLAG_ON_TOP_ALL);
                }
            }
            break;
        case ASPECT_CORRECT:
            SetCorrectAspectRatio(!CorrectAspectRatio());
            break;
398 399 400 401 402 403 404 405 406 407 408 409

        case B_KEY_DOWN:
        case B_UNMAPPED_KEY_DOWN:
        case B_KEY_UP:
        case B_UNMAPPED_KEY_UP:
        {
            key_map * keys;
            char    * chars;
            int32     key, modifiers;

            if( p_message->FindInt32( "key", &key ) != B_OK ||
                p_message->FindInt32( "modifiers", &modifiers ) != B_OK )
410
            {
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
                /* Shouldn't happen */
                break;
            }

            if( ( p_message->what == B_KEY_UP ||
                  p_message->what == B_UNMAPPED_KEY_UP ) &&
                !( modifiers & B_COMMAND_KEY ) )
            {
                /* We only use the KEY_UP messages to detect Alt+X
                   shortcuts (because the KEY_DOWN messages aren't
                   sent when Alt is pressed) */
                break;
            }

            /* Special case for Alt+1, Alt+2 and Alt+3 shortcuts: since
               the character depends on the keymap, we use the key codes
               directly (18, 19, 20) */
            if( ( modifiers & B_COMMAND_KEY ) &&
                key >= 18 && key <= 20 )
            {
                if( key == 18 )
                    PostMessage( RESIZE_50 );
                else if( key == 19 )
                    PostMessage( RESIZE_100 );
435
                else
436 437 438
                    PostMessage( RESIZE_200 );

                break;
439
            }
440 441 442 443 444 445 446 447 448 449 450 451

            /* Get the current keymap */
            get_key_map( &keys, &chars );

            if( key >= 128 || chars[keys->normal_map[key]] != 1 )
            {
                /* Weird key or Unicode character */
                free( keys );
                free( chars );
                break;
            }

452
            vlc_value_t val;
453 454 455 456 457 458 459 460 461 462 463 464 465 466
            val.i_int = ConvertKeyToVLC( chars[keys->normal_map[key]+1] );

            if( modifiers & B_COMMAND_KEY )
            {
                val.i_int |= KEY_MODIFIER_ALT;
            }
            if( modifiers & B_SHIFT_KEY )
            {
                val.i_int |= KEY_MODIFIER_SHIFT;
            }
            if( modifiers & B_CONTROL_KEY )
            {
                val.i_int |= KEY_MODIFIER_CTRL;
            }
467
            var_Set( p_vout->p_vlc, "key-pressed", val );
468 469 470

            free( keys );
            free( chars );
471 472
            break;
        }
473

474 475 476 477
        default:
            BWindow::MessageReceived( p_message );
            break;
    }
478 479 480 481 482 483 484 485
}

/*****************************************************************************
 * VideoWindow::Zoom
 *****************************************************************************/
void
VideoWindow::Zoom(BPoint origin, float width, float height )
{
486
    ToggleFullScreen();
487 488
}

489 490 491 492
/*****************************************************************************
 * VideoWindow::FrameMoved
 *****************************************************************************/
void
Sam Hocevar's avatar
Sam Hocevar committed
493
VideoWindow::FrameMoved(BPoint origin)
494
{
495 496 497
    if (IsFullScreen())
        return ;
    winSize = Frame();
498 499 500 501 502 503 504
}

/*****************************************************************************
 * VideoWindow::FrameResized
 *****************************************************************************/
void
VideoWindow::FrameResized( float width, float height )
505
{
Sam Hocevar's avatar
Sam Hocevar committed
506 507
    int32_t useWidth = CorrectAspectRatio() ? i_width : fTrueWidth;
    int32_t useHeight = CorrectAspectRatio() ? i_height : fTrueHeight;
508 509 510 511 512 513
    float out_width, out_height;
    float out_left, out_top;
    float width_scale = width / useWidth;
    float height_scale = height / useHeight;

    if (width_scale <= height_scale)
514
    {
515 516
        out_width = (useWidth * width_scale);
        out_height = (useHeight * width_scale);
Sam Hocevar's avatar
Sam Hocevar committed
517
        out_left = 0;
518
        out_top = (height - out_height) / 2;
519
    }
520 521 522 523 524 525 526 527 528 529
    else   /* if the height is proportionally smaller */
    {
        out_width = (useWidth * height_scale);
        out_height = (useHeight * height_scale);
        out_top = 0;
        out_left = (width - out_width) / 2;
    }
    view->MoveTo(out_left,out_top);
    view->ResizeTo(out_width, out_height);

530
    if (!IsFullScreen())
531 532 533 534 535 536 537 538 539
        winSize = Frame();
}

/*****************************************************************************
 * VideoWindow::ScreenChanged
 *****************************************************************************/
void
VideoWindow::ScreenChanged(BRect frame, color_space format)
{
540 541 542 543 544 545
    BScreen screen(this);
    display_mode mode;
    screen.GetMode(&mode);
    float refresh = (mode.timing.pixel_clock * 1000)
                    / ((mode.timing.h_total) * (mode.timing.v_total));
    SetSyncToRetrace(refresh < MIN_AUTO_VSYNC_REFRESH);
546 547
}

548 549 550 551 552 553 554 555 556 557 558 559 560
/*****************************************************************************
 * VideoWindow::Activate
 *****************************************************************************/
void
VideoWindow::WindowActivated(bool active)
{
}

/*****************************************************************************
 * VideoWindow::drawBuffer
 *****************************************************************************/
void
VideoWindow::drawBuffer(int bufferIndex)
561 562 563 564
{
    i_buffer = bufferIndex;

    // sync to the screen if required
565
    if (IsSyncedToRetrace())
566
    {
567 568
        BScreen screen(this);
        screen.WaitForRetrace(22000);
569
    }
570
    if (fInitStatus >= B_OK && LockLooper())
571 572 573 574 575
    {
       // switch the overlay bitmap
       if (mode == OVERLAY)
       {
          rgb_color key;
Sam Hocevar's avatar
Sam Hocevar committed
576
          view->SetViewOverlay(bitmap[i_buffer],
577 578 579
                            bitmap[i_buffer]->Bounds() ,
                            view->Bounds(),
                            &key, B_FOLLOW_ALL,
580 581 582 583
                            B_OVERLAY_FILTER_HORIZONTAL|B_OVERLAY_FILTER_VERTICAL|
                            B_OVERLAY_TRANSFER_CHANNEL);
           view->SetViewColor(key);
       }
584 585 586
       else
       {
         // switch the bitmap
587
         view->DrawBitmap(bitmap[i_buffer], view->Bounds() );
588 589 590 591 592
       }
       UnlockLooper();
    }
}

593 594 595 596 597
/*****************************************************************************
 * VideoWindow::SetInterfaceShowing
 *****************************************************************************/
void
VideoWindow::ToggleInterfaceShowing()
598
{
599
    SetInterfaceShowing(!fInterfaceShowing);
600 601
}

602 603 604 605 606
/*****************************************************************************
 * VideoWindow::SetInterfaceShowing
 *****************************************************************************/
void
VideoWindow::SetInterfaceShowing(bool showIt)
607
{
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
    BWindow* window = get_interface_window();
    if (window)
    {
        if (showIt)
        {
            if (fCachedFeel != B_NORMAL_WINDOW_FEEL)
                SetFeel(B_NORMAL_WINDOW_FEEL);
            window->Activate(true);
            SendBehind(window);
        }
        else
        {
            SetFeel(fCachedFeel);
            Activate(true);
            window->SendBehind(this);
        }
        fInterfaceShowing = showIt;
    }
626 627
}

628 629 630 631 632
/*****************************************************************************
 * VideoWindow::SetCorrectAspectRatio
 *****************************************************************************/
void
VideoWindow::SetCorrectAspectRatio(bool doIt)
633
{
634 635 636 637 638 639 640 641
    if (CorrectAspectRatio() != doIt)
    {
        if (doIt)
            fSettings->AddFlags(VideoSettings::FLAG_CORRECT_RATIO);
        else
            fSettings->ClearFlags(VideoSettings::FLAG_CORRECT_RATIO);
        FrameResized(Bounds().Width(), Bounds().Height());
    }
642 643
}

644 645 646 647 648 649
/*****************************************************************************
 * VideoWindow::CorrectAspectRatio
 *****************************************************************************/
bool
VideoWindow::CorrectAspectRatio() const
{
650
    return fSettings->HasFlags(VideoSettings::FLAG_CORRECT_RATIO);
651 652 653 654 655 656 657 658
}

/*****************************************************************************
 * VideoWindow::ToggleFullScreen
 *****************************************************************************/
void
VideoWindow::ToggleFullScreen()
{
659
    SetFullScreen(!IsFullScreen());
660 661 662 663 664 665 666 667
}

/*****************************************************************************
 * VideoWindow::SetFullScreen
 *****************************************************************************/
void
VideoWindow::SetFullScreen(bool doIt)
{
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
    if (doIt)
    {
        SetLook( B_NO_BORDER_WINDOW_LOOK );
        BScreen screen( this );
        BRect rect = screen.Frame();
        Activate();
        MoveTo(0.0, 0.0);
        ResizeTo(rect.IntegerWidth(), rect.IntegerHeight());
        be_app->ObscureCursor();
        fInterfaceShowing = false;
        fSettings->AddFlags(VideoSettings::FLAG_FULL_SCREEN);
    }
    else
    {
        SetLook( B_TITLED_WINDOW_LOOK );
        MoveTo(winSize.left, winSize.top);
        ResizeTo(winSize.IntegerWidth(), winSize.IntegerHeight());
        be_app->ShowCursor();
        fInterfaceShowing = true;
        fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
    }
689 690 691 692 693 694 695 696
}

/*****************************************************************************
 * VideoWindow::IsFullScreen
 *****************************************************************************/
bool
VideoWindow::IsFullScreen() const
{
697
    return fSettings->HasFlags(VideoSettings::FLAG_FULL_SCREEN);
698 699 700 701 702 703 704 705
}

/*****************************************************************************
 * VideoWindow::SetSyncToRetrace
 *****************************************************************************/
void
VideoWindow::SetSyncToRetrace(bool doIt)
{
706 707 708 709
    if (doIt)
        fSettings->AddFlags(VideoSettings::FLAG_SYNC_RETRACE);
    else
        fSettings->ClearFlags(VideoSettings::FLAG_SYNC_RETRACE);
710 711 712 713 714 715 716 717
}

/*****************************************************************************
 * VideoWindow::IsSyncedToRetrace
 *****************************************************************************/
bool
VideoWindow::IsSyncedToRetrace() const
{
718
    return fSettings->HasFlags(VideoSettings::FLAG_SYNC_RETRACE);
719 720 721
}


722 723 724 725 726
/*****************************************************************************
 * VideoWindow::_AllocateBuffers
 *****************************************************************************/
status_t
VideoWindow::_AllocateBuffers(int width, int height, int* mode)
727
{
728 729 730 731
    // clear any old buffers
    _FreeBuffers();
    // set default mode
    *mode = BITMAP;
732
    bitmap_count = 3;
733

734 735
    BRect bitmapFrame( 0, 0, width, height );
    // read from config, if we are supposed to use overlay at all
736
    int noOverlay = !config_GetInt( p_vout, "overlay" );
737 738

    /* Test for overlay capability: for every chroma in colspace,
739 740 741
       we try to do double-buffered overlay, single-buffered overlay
       or basic overlay. If nothing worked, we then have to work with
       a non-overlay BBitmap. */
742
    for( int i = 0; i < COLOR_COUNT; i++ )
743
    {
744 745
        if( noOverlay )
            break;
746

747 748 749 750 751
        bitmap[0] = new BBitmap( bitmapFrame,
                                 B_BITMAP_WILL_OVERLAY |
                                 B_BITMAP_RESERVE_OVERLAY_CHANNEL,
                                 colspace[i].colspace );
        if( bitmap[0] && bitmap[0]->InitCheck() == B_OK )
752 753 754
        {
            colspace_index = i;

755 756 757 758 759 760 761 762 763
            *mode = OVERLAY;
            rgb_color key;
            view->SetViewOverlay( bitmap[0], bitmap[0]->Bounds(),
                                  view->Bounds(), &key, B_FOLLOW_ALL,
                                  B_OVERLAY_FILTER_HORIZONTAL |
                                  B_OVERLAY_FILTER_VERTICAL );
            view->SetViewColor( key );
            SetTitle( "VLC " PACKAGE_VERSION " (Overlay)" );

764
            bitmap[1] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
765
                                     colspace[colspace_index].colspace);
766
            if( bitmap[1] && bitmap[1]->InitCheck() == B_OK )
767
            {
768 769 770 771 772 773 774 775 776 777 778

                bitmap[2] = new BBitmap( bitmapFrame, B_BITMAP_WILL_OVERLAY,
                                         colspace[colspace_index].colspace);
                if( bitmap[2] && bitmap[2]->InitCheck() == B_OK )
                {
                    msg_Dbg( p_vout, "using double-buffered overlay" );
                }
                else
                {
                    msg_Dbg( p_vout, "using single-buffered overlay" );
                    bitmap_count = 2;
779
                    if( bitmap[2] ) { delete bitmap[2]; bitmap[2] = NULL; }
780
                }
781 782 783
            }
            else
            {
784 785 786
                msg_Dbg( p_vout, "using simple overlay" );
                bitmap_count = 1;
                if( bitmap[1] ) { delete bitmap[1]; bitmap[1] = NULL; }
787
            }
788
            break;
789 790
        }
        else
791
        {
792
            if( bitmap[0] ) { delete bitmap[0]; bitmap[0] = NULL; }
793
        }
794
    }
795

796
    if (*mode == BITMAP)
797
    {
798 799
        msg_Warn( p_vout, "no possible overlay" );

800
        // fallback to RGB
801
        colspace_index = DEFAULT_COL;    // B_RGB32
802 803 804
        bitmap[0] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
        bitmap[1] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
        bitmap[2] = new BBitmap( bitmapFrame, colspace[colspace_index].colspace );
805
        SetTitle( "VLC " PACKAGE_VERSION " (Bitmap)" );
806 807 808
    }
    // see if everything went well
    status_t status = B_ERROR;
809
    for (int32_t i = 0; i < bitmap_count; i++)
810
    {
811 812 813 814
        if (bitmap[i])
            status = bitmap[i]->InitCheck();
        if (status < B_OK)
            break;
815
    }
816 817
    if (status >= B_OK)
    {
818
        // clear bitmaps to black
819
        for (int32_t i = 0; i < bitmap_count; i++)
820
            _BlankBitmap(bitmap[i]);
821 822
    }
    return status;
823 824 825
}

/*****************************************************************************
826 827 828 829 830
 * VideoWindow::_FreeBuffers
 *****************************************************************************/
void
VideoWindow::_FreeBuffers()
{
831 832 833
    if( bitmap[0] ) { delete bitmap[0]; bitmap[0] = NULL; }
    if( bitmap[1] ) { delete bitmap[1]; bitmap[1] = NULL; }
    if( bitmap[2] ) { delete bitmap[2]; bitmap[2] = NULL; }
834
    fInitStatus = B_ERROR;
835 836 837 838 839 840 841 842
}

/*****************************************************************************
 * VideoWindow::_BlankBitmap
 *****************************************************************************/
void
VideoWindow::_BlankBitmap(BBitmap* bitmap) const
{
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
    // no error checking (we do that earlier on and since it's a private function...

    // YCbCr:
    // Loss/Saturation points are Y 16-235 (absoulte); Cb/Cr 16-240 (center 128)

    // YUV:
    // Extrema points are Y 0 - 207 (absolute) U -91 - 91 (offset 128) V -127 - 127 (offset 128)

    // we only handle weird colorspaces with special care
    switch (bitmap->ColorSpace()) {
        case B_YCbCr422: {
            // Y0[7:0]  Cb0[7:0]  Y1[7:0]  Cr0[7:0]  Y2[7:0]  Cb2[7:0]  Y3[7:0]  Cr2[7:0]
            int32_t height = bitmap->Bounds().IntegerHeight() + 1;
            uint8_t* bits = (uint8_t*)bitmap->Bits();
            int32_t bpr = bitmap->BytesPerRow();
            for (int32_t y = 0; y < height; y++) {
                // handle 2 bytes at a time
                for (int32_t i = 0; i < bpr; i += 2) {
                    // offset into line
                    bits[i] = 16;
                    bits[i + 1] = 128;
                }
                // next line
                bits += bpr;
            }
            break;
        }
        case B_YCbCr420: {
871
// TODO: untested!!
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
            // Non-interlaced only, Cb0  Y0  Y1  Cb2 Y2  Y3  on even scan lines ...
            // Cr0  Y0  Y1  Cr2 Y2  Y3  on odd scan lines
            int32_t height = bitmap->Bounds().IntegerHeight() + 1;
            uint8_t* bits = (uint8_t*)bitmap->Bits();
            int32_t bpr = bitmap->BytesPerRow();
            for (int32_t y = 0; y < height; y += 1) {
                // handle 3 bytes at a time
                for (int32_t i = 0; i < bpr; i += 3) {
                    // offset into line
                    bits[i] = 128;
                    bits[i + 1] = 16;
                    bits[i + 2] = 16;
                }
                // next line
                bits += bpr;
            }
            break;
        }
        case B_YUV422: {
891
// TODO: untested!!
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
            // U0[7:0]  Y0[7:0]   V0[7:0]  Y1[7:0]  U2[7:0]  Y2[7:0]   V2[7:0]  Y3[7:0]
            int32_t height = bitmap->Bounds().IntegerHeight() + 1;
            uint8_t* bits = (uint8_t*)bitmap->Bits();
            int32_t bpr = bitmap->BytesPerRow();
            for (int32_t y = 0; y < height; y += 1) {
                // handle 2 bytes at a time
                for (int32_t i = 0; i < bpr; i += 2) {
                    // offset into line
                    bits[i] = 128;
                    bits[i + 1] = 0;
                }
                // next line
                bits += bpr;
            }
            break;
        }
        default:
            memset(bitmap->Bits(), 0, bitmap->BitsLength());
            break;
    }
912 913 914 915 916 917
}

/*****************************************************************************
 * VideoWindow::_SetVideoSize
 *****************************************************************************/
void
Sam Hocevar's avatar
Sam Hocevar committed
918
VideoWindow::_SetVideoSize(uint32_t mode)
919
{
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
    // let size depend on aspect correction
    int32_t width = CorrectAspectRatio() ? i_width : fTrueWidth;
    int32_t height = CorrectAspectRatio() ? i_height : fTrueHeight;
    switch (mode)
    {
        case RESIZE_50:
            width /= 2;
            height /= 2;
            break;
        case RESIZE_200:
            width *= 2;
            height *= 2;
            break;
        case RESIZE_100:
        default:
            break;
    }
    fSettings->ClearFlags(VideoSettings::FLAG_FULL_SCREEN);
    ResizeTo(width, height);
939 940 941 942 943 944 945 946
}

/*****************************************************************************
 * VideoWindow::_SetToSettings
 *****************************************************************************/
void
VideoWindow::_SetToSettings()
{
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
    // adjust dimensions
    uint32_t mode = RESIZE_100;
    switch (fSettings->VideoSize())
    {
        case VideoSettings::SIZE_50:
            mode = RESIZE_50;
            break;
        case VideoSettings::SIZE_200:
            mode = RESIZE_200;
            break;
        case VideoSettings::SIZE_100:
        case VideoSettings::SIZE_OTHER:
        default:
            break;
    }
    bool fullscreen = IsFullScreen();    // remember settings
    _SetVideoSize(mode);                // because this will reset settings
    // the fullscreen status is reflected in the settings,
    // but not yet in the windows state
    if (fullscreen)
        SetFullScreen(true);
    if (fSettings->HasFlags(VideoSettings::FLAG_ON_TOP_ALL))
        fCachedFeel = B_FLOATING_ALL_WINDOW_FEEL;
    else
        fCachedFeel = B_NORMAL_WINDOW_FEEL;
    SetFeel(fCachedFeel);
973 974 975 976 977
}

/*****************************************************************************
 * VLCView::VLCView
 *****************************************************************************/
978
VLCView::VLCView(BRect bounds, vout_thread_t *p_vout_instance )
979 980 981 982 983
    : BView(bounds, "video view", B_FOLLOW_NONE, B_WILL_DRAW | B_PULSE_NEEDED),
      fLastMouseMovedTime(mdate()),
      fCursorHidden(false),
      fCursorInside(false),
      fIgnoreDoubleClick(false)
984
{
985
    p_vout = p_vout_instance;
986 987 988 989 990 991 992 993 994 995
    SetViewColor(B_TRANSPARENT_32_BIT);
}

/*****************************************************************************
 * VLCView::~VLCView
 *****************************************************************************/
VLCView::~VLCView()
{
}

996 997 998 999 1000 1001
/*****************************************************************************
 * VLCVIew::AttachedToWindow
 *****************************************************************************/
void
VLCView::AttachedToWindow()
{
1002 1003
    // periodically check if we want to hide the pointer
    Window()->SetPulseRate(1000000);
1004 1005
}

1006 1007 1008
/*****************************************************************************
 * VLCVIew::MouseDown
 *****************************************************************************/
1009 1010 1011
void
VLCView::MouseDown(BPoint where)
{
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
    VideoWindow* videoWindow = dynamic_cast<VideoWindow*>(Window());
    BMessage* msg = Window()->CurrentMessage();
    int32 clicks;
    uint32_t buttons;
    msg->FindInt32("clicks", &clicks);
    msg->FindInt32("buttons", (int32*)&buttons);

    if (videoWindow)
    {
        if (buttons & B_PRIMARY_MOUSE_BUTTON)
        {
            if (clicks == 2 && !fIgnoreDoubleClick)
                Window()->Zoom();
            /* else
                videoWindow->ToggleInterfaceShowing(); */
            fIgnoreDoubleClick = false;
        }
        else
        {
            if (buttons & B_SECONDARY_MOUSE_BUTTON)
            {
                // clicks will be 2 next time (if interval short enough)
                // even if the first click and the second
                // have not been made with the same mouse button
                fIgnoreDoubleClick = true;
                // launch popup menu
                BPopUpMenu *menu = new BPopUpMenu("context menu");
                menu->SetRadioMode(false);
                // In full screen, add an item to show/hide the interface
                if( videoWindow->IsFullScreen() )
                {
                    BMenuItem *intfItem =
                        new BMenuItem( _("Show Interface"), new BMessage(SHOW_INTERFACE) );
                    menu->AddItem( intfItem );
                }
                // Resize to 50%
                BMenuItem *halfItem = new BMenuItem(_("50%"), new BMessage(RESIZE_50));
                menu->AddItem(halfItem);
                // Resize to 100%
                BMenuItem *origItem = new BMenuItem(_("100%"), new BMessage(RESIZE_100));
                menu->AddItem(origItem);
                // Resize to 200%
                BMenuItem *doubleItem = new BMenuItem(_("200%"), new BMessage(RESIZE_200));
                menu->AddItem(doubleItem);
                // Toggle FullScreen
                BMenuItem *zoomItem = new BMenuItem(_("Fullscreen"), new BMessage(TOGGLE_FULL_SCREEN));
                zoomItem->SetMarked(videoWindow->IsFullScreen());
                menu->AddItem(zoomItem);
    
                menu->AddSeparatorItem();
    
                // Toggle vSync
                BMenuItem *vsyncItem = new BMenuItem(_("Vertical Sync"), new BMessage(VERT_SYNC));
                vsyncItem->SetMarked(videoWindow->IsSyncedToRetrace());
                menu->AddItem(vsyncItem);
                // Correct Aspect Ratio
                BMenuItem *aspectItem = new BMenuItem(_("Correct Aspect Ratio"), new BMessage(ASPECT_CORRECT));
                aspectItem->SetMarked(videoWindow->CorrectAspectRatio());
                menu->AddItem(aspectItem);
    
                menu->AddSeparatorItem();
    
                // Window Feel Items
/*                BMessage *winNormFeel = new BMessage(WINDOW_FEEL);
                winNormFeel->AddInt32("WinFeel", (int32_t)B_NORMAL_WINDOW_FEEL);
                BMenuItem *normWindItem = new BMenuItem("Normal Window", winNormFeel);
                normWindItem->SetMarked(videoWindow->Feel() == B_NORMAL_WINDOW_FEEL);
                menu->AddItem(normWindItem);
                
                BMessage *winFloatFeel = new BMessage(WINDOW_FEEL);
                winFloatFeel->AddInt32("WinFeel", (int32_t)B_FLOATING_APP_WINDOW_FEEL);
                BMenuItem *onTopWindItem = new BMenuItem("App Top", winFloatFeel);
                onTopWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_APP_WINDOW_FEEL);
                menu->AddItem(onTopWindItem);
                
                BMessage *winAllFeel = new BMessage(WINDOW_FEEL);
                winAllFeel->AddInt32("WinFeel", (int32_t)B_FLOATING_ALL_WINDOW_FEEL);
                BMenuItem *allSpacesWindItem = new BMenuItem("On Top All Workspaces", winAllFeel);
                allSpacesWindItem->SetMarked(videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL);
                menu->AddItem(allSpacesWindItem);*/

                BMessage *windowFeelMsg = new BMessage( WINDOW_FEEL );
                bool onTop = videoWindow->Feel() == B_FLOATING_ALL_WINDOW_FEEL;
                window_feel feel = onTop ? B_NORMAL_WINDOW_FEEL : B_FLOATING_ALL_WINDOW_FEEL;
                windowFeelMsg->AddInt32( "WinFeel", (int32_t)feel );
                BMenuItem *windowFeelItem = new BMenuItem( _("Stay On Top"), windowFeelMsg );
                windowFeelItem->SetMarked( onTop );
                menu->AddItem( windowFeelItem );

                menu->AddSeparatorItem();

                BMenuItem* screenShotItem = new BMenuItem( _("Take Screen Shot"),
                                                           new BMessage( SCREEN_SHOT ) );
                menu->AddItem( screenShotItem );

                menu->SetTargetForItems( this );
                ConvertToScreen( &where );
                BRect mouseRect( where.x - 5, where.y - 5,
                                 where.x + 5, where.y + 5 );
                menu->Go( where, true, false, mouseRect, true );
            }
        }
    }
    fLastMouseMovedTime = mdate();
    fCursorHidden = false;
1117 1118
}

1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
/*****************************************************************************
 * VLCVIew::MouseUp
 *****************************************************************************/
void
VLCView::MouseUp( BPoint where )
{
    vlc_value_t val;
    val.b_bool = VLC_TRUE;
    var_Set( p_vout, "mouse-clicked", val );
}

1130 1131 1132 1133
/*****************************************************************************
 * VLCVIew::MouseMoved
 *****************************************************************************/
void
1134
VLCView::MouseMoved(BPoint point, uint32 transit, const BMessage* dragMessage)
1135
{
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    fLastMouseMovedTime = mdate();
    fCursorHidden = false;
    fCursorInside = ( transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW );

    if( !fCursorInside )
    {
        return;
    }

    vlc_value_t val;
    unsigned int i_width, i_height, i_x, i_y;
1147 1148 1149
    vout_PlacePicture( p_vout, (unsigned int)Bounds().Width(),
                       (unsigned int)Bounds().Height(),
                       &i_x, &i_y, &i_width, &i_height );
1150 1151 1152 1153 1154
    val.i_int = ( (int)point.x - i_x ) * p_vout->render.i_width / i_width;
    var_Set( p_vout, "mouse-x", val );
    val.i_int = ( (int)point.y - i_y ) * p_vout->render.i_height / i_height;
    var_Set( p_vout, "mouse-y", val );
    val.b_bool = VLC_TRUE;
1155
    var_Set( p_vout, "mouse-moved", val );
1156
}
1157

1158 1159 1160
/*****************************************************************************
 * VLCVIew::Pulse
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
1161
void
1162 1163
VLCView::Pulse()
{
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    // We are getting the pulse messages no matter if the mouse is over
    // this view. If we are in full screen mode, we want to hide the cursor
    // even if it is not.
    VideoWindow *videoWindow = dynamic_cast<VideoWindow*>(Window());
    if (!fCursorHidden)
    {
        if (fCursorInside
            && mdate() - fLastMouseMovedTime > MOUSE_IDLE_TIMEOUT)
        {
            be_app->ObscureCursor();
            fCursorHidden = true;
            
            // hide the interface window as well if full screen
            if (videoWindow && videoWindow->IsFullScreen())
                videoWindow->SetInterfaceShowing(false);
        }
    }
1181 1182

    // Workaround to disable the screensaver in full screen:
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192
    // we simulate an activity every 29 seconds    
    if( videoWindow && videoWindow->IsFullScreen() &&
        mdate() - fLastMouseMovedTime > 29000000 )
    {
        BPoint where;
        uint32 buttons;
        GetMouse(&where, &buttons, false);
        ConvertToScreen(&where);
        set_mouse_position((int32_t) where.x, (int32_t) where.y);
    }
1193
}
1194 1195 1196 1197

/*****************************************************************************
 * VLCVIew::Draw
 *****************************************************************************/
1198
void
Sam Hocevar's avatar
Sam Hocevar committed
1199
VLCView::Draw(BRect updateRect)
1200
{
1201 1202 1203
    VideoWindow* window = dynamic_cast<VideoWindow*>( Window() );
    if ( window && window->mode == BITMAP )
        FillRect( updateRect );
1204 1205 1206