VideoOutput.cpp 47.3 KB
Newer Older
1
/*****************************************************************************
2
 * vout_beos.cpp: beos video output display method
3
 *****************************************************************************
4
 * Copyright (C) 2000, 2001 the VideoLAN team
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
 * 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
25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 27 28 29 30 31
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <Application.h>
32
#include <BitmapStream.h>
33
#include <Bitmap.h>
Stephan Assmus's avatar
 
Stephan Assmus committed
34
#include <Directory.h>
35 36 37 38 39 40
#include <DirectWindow.h>
#include <File.h>
#include <InterfaceKit.h>
#include <NodeInfo.h>
#include <String.h>
#include <TranslatorRoster.h>
41
#include <WindowScreen.h>
42

43
/* VLC headers */
44 45 46 47
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

48
#include <vlc_common.h>
zorglub's avatar
zorglub committed
49 50
#include <vlc_interface.h>
#include <vlc_vout.h>
51
#include <vlc_keys.h>
52

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

57 58
#include "VideoWindow.h"

59 60 61 62 63 64 65 66 67 68
/*****************************************************************************
 * 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
69 70
    int32_t i_width;
    int32_t i_height;
71

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

76 77
};

78
#define MIN_AUTO_VSYNC_REFRESH 61    // Hz
79

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

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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
104
    return window;
105 106
}

107
static const int beos_keys[][2] =
108
{
109 110 111 112
    { B_LEFT_ARROW,  KEY_LEFT },
    { B_RIGHT_ARROW, KEY_RIGHT },
    { B_UP_ARROW,    KEY_UP },
    { B_DOWN_ARROW,  KEY_DOWN },
113
    { B_SPACE,       ' ' },
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
    { 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++ )
151
    {
152 153 154 155
        if( beos_keys[i][0] == key )
        {
            return beos_keys[i][1];
        }
156 157 158 159
    }
    return key;
}

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

class BackgroundView : public BView
{
 public:
172 173 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
                            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!
                            }
199 200

 private:
201
    VLCView*                fVideoView;
202 203
};

204 205 206 207 208

/*****************************************************************************
 * VideoSettings constructor and destructor
 *****************************************************************************/
VideoSettings::VideoSettings()
209 210 211
    : fVideoSize( SIZE_100 ),
      fFlags( FLAG_CORRECT_RATIO ),
      fSettings( new BMessage( 'sett' ) )
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
    // 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);
        }
    }
238 239 240
}

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


VideoSettings::~VideoSettings()
{
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    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() );
    }
268 269 270 271 272 273 274 275
}

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

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

// static variable initialization
VideoSettings
VideoSettings::fDefaultSettings;


293 294 295
/*****************************************************************************
 * VideoWindow constructor and destructor
 *****************************************************************************/
296 297
VideoWindow::VideoWindow(int v_width, int v_height, BRect frame,
                         vout_thread_t *p_videoout)
298 299 300 301 302 303 304 305 306 307 308 309
    : 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()))
310
{
311
    p_vout = p_videoout;
Sam Hocevar's avatar
Sam Hocevar committed
312

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

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

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

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

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

333
       bitmap[0]->GetOverlayRestrictions(&r);
334 335
       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);
336
    }
Sam Hocevar's avatar
Sam Hocevar committed
337

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

    _SetToSettings();
343 344 345 346
}

VideoWindow::~VideoWindow()
{
Eric Petit's avatar
Eric Petit committed
347
    int32 result;
348

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

/*****************************************************************************
 * VideoWindow::MessageReceived
 *****************************************************************************/
void
VideoWindow::MessageReceived( BMessage *p_message )
{
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
    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;
396 397 398 399 400 401 402 403 404 405 406 407

        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 )
408
            {
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
                /* 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 );
433
                else
434 435 436
                    PostMessage( RESIZE_200 );

                break;
437
            }
438 439 440 441 442 443 444 445 446 447 448 449

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

450
            vlc_value_t val;
451 452 453 454 455 456 457 458 459 460 461 462 463 464
            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;
            }
465
            var_Set( p_vout->p_libvlc, "key-pressed", val );
466 467 468

            free( keys );
            free( chars );
469 470
            break;
        }
471

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

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

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

/*****************************************************************************
 * VideoWindow::FrameResized
 *****************************************************************************/
void
VideoWindow::FrameResized( float width, float height )
503
{
Sam Hocevar's avatar
Sam Hocevar committed
504 505
    int32_t useWidth = CorrectAspectRatio() ? i_width : fTrueWidth;
    int32_t useHeight = CorrectAspectRatio() ? i_height : fTrueHeight;
506 507 508 509 510 511
    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)
512
    {
513 514
        out_width = (useWidth * width_scale);
        out_height = (useHeight * width_scale);
Sam Hocevar's avatar
Sam Hocevar committed
515
        out_left = 0;
516
        out_top = (height - out_height) / 2;
517
    }
518 519 520 521 522 523 524 525 526 527
    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);

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

/*****************************************************************************
 * VideoWindow::ScreenChanged
 *****************************************************************************/
void
VideoWindow::ScreenChanged(BRect frame, color_space format)
{
538 539 540 541 542 543
    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);
544 545
}

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

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

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

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

600 601 602 603 604
/*****************************************************************************
 * VideoWindow::SetInterfaceShowing
 *****************************************************************************/
void
VideoWindow::SetInterfaceShowing(bool showIt)
605
{
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    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;
    }
624 625
}

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

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

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

/*****************************************************************************
 * VideoWindow::SetFullScreen
 *****************************************************************************/
void
VideoWindow::SetFullScreen(bool doIt)
{
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    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);
    }
687 688 689 690 691 692 693 694
}

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

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

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


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

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

    /* Test for overlay capability: for every chroma in colspace,
737 738 739
       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. */
740
    for( int i = 0; i < COLOR_COUNT; i++ )
741
    {
742 743
        if( noOverlay )
            break;
744

745 746 747 748 749
        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 )
750 751 752
        {
            colspace_index = i;

753 754 755 756 757 758 759 760 761
            *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)" );

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

                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;
ivoire's avatar
ivoire committed
777
                    delete bitmap[2]; bitmap[2] = NULL;
778
                }
779 780 781
            }
            else
            {
782 783
                msg_Dbg( p_vout, "using simple overlay" );
                bitmap_count = 1;
ivoire's avatar
ivoire committed
784
                delete bitmap[1]; bitmap[1] = NULL;
785
            }
786
            break;
787 788
        }
        else
789
        {
ivoire's avatar
ivoire committed
790
            delete bitmap[0]; bitmap[0] = NULL;
791
        }
792
    }
793

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

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

/*****************************************************************************
824 825 826 827 828
 * VideoWindow::_FreeBuffers
 *****************************************************************************/
void
VideoWindow::_FreeBuffers()
{
ivoire's avatar
ivoire committed
829 830 831
    delete bitmap[0]; bitmap[0] = NULL;
    delete bitmap[1]; bitmap[1] = NULL;
    delete bitmap[2]; bitmap[2] = NULL;
832
    fInitStatus = B_ERROR;
833 834 835 836 837 838 839 840
}

/*****************************************************************************
 * VideoWindow::_BlankBitmap
 *****************************************************************************/
void
VideoWindow::_BlankBitmap(BBitmap* bitmap) const
{
841 842 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
    // 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: {
869
// TODO: untested!!
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
            // 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: {
889
// TODO: untested!!
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
            // 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;
    }
910 911 912 913 914 915
}

/*****************************************************************************
 * VideoWindow::_SetVideoSize
 *****************************************************************************/
void
Sam Hocevar's avatar
Sam Hocevar committed
916
VideoWindow::_SetVideoSize(uint32_t mode)
917
{
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
    // 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);
937 938 939 940 941 942 943 944
}

/*****************************************************************************
 * VideoWindow::_SetToSettings
 *****************************************************************************/
void
VideoWindow::_SetToSettings()
{
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
    // 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);
971 972 973 974 975
}

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

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

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

1005 1006 1007
/*****************************************************************************
 * VLCVIew::MouseDown
 *****************************************************************************/
1008 1009 1010
void
VLCView::MouseDown(BPoint where)
{
1011 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
    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);
1059
 
1060
                menu->AddSeparatorItem();
1061
 
1062 1063 1064 1065 1066 1067 1068 1069
                // 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);
1070
 
1071
                menu->AddSeparatorItem();
1072
 
1073 1074 1075 1076 1077 1078
                // 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);
1079
 
1080 1081 1082 1083 1084
                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);
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
                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;
1116 1117
}

1118 1119 1120 1121 1122 1123
/*****************************************************************************
 * VLCVIew::MouseUp
 *****************************************************************************/
void
VLCView::MouseUp( BPoint where )
{
1124
    var_SetBool( p_vout, "mouse-clicked", true );
1125 1126
}

1127 1128 1129 1130
/*****************************************************************************
 * VLCVIew::MouseMoved
 *****************************************************************************/
void
1131
VLCView::MouseMoved(BPoint point, uint32 transit, const BMessage* dragMessage)
1132
{
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    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;
1144 1145 1146
    vout_PlacePicture( p_vout, (unsigned int)Bounds().Width(),
                       (unsigned int)Bounds().Height(),
                       &i_x, &i_y, &i_width, &i_height );
1147 1148 1149 1150
    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 );
1151
    val.b_bool = true;
1152
    var_Set( p_vout, "mouse-moved", val );
1153
}
1154

1155 1156 1157
/*****************************************************************************
 * VLCVIew::Pulse
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
1158
void
1159 1160
VLCView::Pulse()
{
1161 1162 1163 1164 1165 1166 1167
    // 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
1168
            && mdate() - fLastMouseMovedTime > fMouseHideTimeout)
1169 1170 1171
        {
            be_app->ObscureCursor();
            fCursorHidden = true;
1172
 
1173 1174 1175 1176 1177
            // hide the interface window as well if full screen
            if (videoWindow && videoWindow->IsFullScreen())
                videoWindow->SetInterfaceShowing(false);
        }
    }
1178 1179

    // Workaround to disable the screensaver in full screen:
1180
    // we simulate an activity every 29 seconds
Eric Petit's avatar