1 /* 2 * MainWin.cpp - Media Player for the Haiku Operating System 3 * 4 * Copyright (C) 2006 Marcus Overhagen <marcus@overhagen.de> 5 * Copyright (C) 2007-2010 Stephan Aßmus <superstippi@gmx.de> (GPL->MIT ok) 6 * Copyright (C) 2007-2009 Fredrik Modéen <[FirstName]@[LastName].se> (MIT ok) 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * version 2 as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20 * USA. 21 */ 22 23 24 #include "MainWin.h" 25 26 #include <math.h> 27 #include <stdio.h> 28 #include <string.h> 29 30 #include <Alert.h> 31 #include <Application.h> 32 #include <Autolock.h> 33 #include <Debug.h> 34 #include <fs_attr.h> 35 #include <Language.h> 36 #include <Menu.h> 37 #include <MenuBar.h> 38 #include <MenuItem.h> 39 #include <MessageRunner.h> 40 #include <Messenger.h> 41 #include <PopUpMenu.h> 42 #include <PropertyInfo.h> 43 #include <RecentItems.h> 44 #include <Roster.h> 45 #include <Screen.h> 46 #include <String.h> 47 #include <TypeConstants.h> 48 #include <View.h> 49 50 #include "AudioProducer.h" 51 #include "ControllerObserver.h" 52 #include "DurationToString.h" 53 #include "FilePlaylistItem.h" 54 #include "MainApp.h" 55 #include "PeakView.h" 56 #include "PlaylistItem.h" 57 #include "PlaylistObserver.h" 58 #include "PlaylistWindow.h" 59 #include "Settings.h" 60 61 62 #define MIN_WIDTH 250 63 64 65 int MainWin::sNoVideoWidth = MIN_WIDTH; 66 67 68 // XXX TODO: why is lround not defined? 69 #define lround(a) ((int)(0.99999 + (a))) 70 71 enum { 72 M_DUMMY = 0x100, 73 M_FILE_OPEN = 0x1000, 74 M_FILE_INFO, 75 M_FILE_PLAYLIST, 76 M_FILE_CLOSE, 77 M_FILE_QUIT, 78 M_VIEW_SIZE, 79 M_TOGGLE_FULLSCREEN, 80 M_TOGGLE_ALWAYS_ON_TOP, 81 M_TOGGLE_NO_INTERFACE, 82 M_VOLUME_UP, 83 M_VOLUME_DOWN, 84 M_SKIP_NEXT, 85 M_SKIP_PREV, 86 M_WIND, 87 88 // The common display aspect ratios 89 M_ASPECT_SAME_AS_SOURCE, 90 M_ASPECT_NO_DISTORTION, 91 M_ASPECT_4_3, 92 M_ASPECT_16_9, 93 M_ASPECT_83_50, 94 M_ASPECT_7_4, 95 M_ASPECT_37_20, 96 M_ASPECT_47_20, 97 98 M_SELECT_AUDIO_TRACK = 0x00000800, 99 M_SELECT_AUDIO_TRACK_END = 0x00000fff, 100 M_SELECT_VIDEO_TRACK = 0x00010000, 101 M_SELECT_VIDEO_TRACK_END = 0x00010fff, 102 M_SELECT_SUB_TITLE_TRACK = 0x00020000, 103 M_SELECT_SUB_TITLE_TRACK_END = 0x00020fff, 104 105 M_SET_RATING, 106 107 M_SET_PLAYLIST_POSITION, 108 109 M_FILE_DELETE, 110 111 M_SHOW_IF_NEEDED, 112 113 M_SLIDE_CONTROLS, 114 M_FINISH_SLIDING_CONTROLS 115 }; 116 117 118 static property_info sPropertyInfo[] = { 119 { "Next", { B_EXECUTE_PROPERTY }, 120 { B_DIRECT_SPECIFIER, 0 }, "Skip to the next track.", 0 121 }, 122 { "Prev", { B_EXECUTE_PROPERTY }, 123 { B_DIRECT_SPECIFIER, 0 }, "Skip to the previous track.", 0 124 }, 125 { "Play", { B_EXECUTE_PROPERTY }, 126 { B_DIRECT_SPECIFIER, 0 }, "Start playing.", 0 127 }, 128 { "Stop", { B_EXECUTE_PROPERTY }, 129 { B_DIRECT_SPECIFIER, 0 }, "Stop playing.", 0 130 }, 131 { "Pause", { B_EXECUTE_PROPERTY }, 132 { B_DIRECT_SPECIFIER, 0 }, "Pause playback.", 0 133 }, 134 { "TogglePlaying", { B_EXECUTE_PROPERTY }, 135 { B_DIRECT_SPECIFIER, 0 }, "Toggle pause/play.", 0 136 }, 137 { "Mute", { B_EXECUTE_PROPERTY }, 138 { B_DIRECT_SPECIFIER, 0 }, "Toggle mute.", 0 139 }, 140 { "Volume", { B_GET_PROPERTY, B_SET_PROPERTY, 0 }, 141 { B_DIRECT_SPECIFIER, 0 }, "Gets/sets the volume (0.0-2.0).", 0, 142 { B_FLOAT_TYPE } 143 }, 144 { "URI", { B_GET_PROPERTY, 0 }, 145 { B_DIRECT_SPECIFIER, 0 }, 146 "Gets the URI of the currently playing item.", 0, { B_STRING_TYPE } 147 }, 148 { 0, { 0 }, { 0 }, 0, 0 } 149 }; 150 151 152 static const char* kRatingAttrName = "Media:Rating"; 153 154 static const char* kDisabledSeekMessage = "Drop files to play"; 155 156 157 //#define printf(a...) 158 159 160 MainWin::MainWin(bool isFirstWindow, BMessage* message) 161 : 162 BWindow(BRect(100, 100, 400, 300), NAME, B_TITLED_WINDOW, 163 B_ASYNCHRONOUS_CONTROLS /* | B_WILL_ACCEPT_FIRST_CLICK */), 164 fCreationTime(system_time()), 165 fInfoWin(NULL), 166 fPlaylistWindow(NULL), 167 fHasFile(false), 168 fHasVideo(false), 169 fHasAudio(false), 170 fPlaylist(new Playlist), 171 fPlaylistObserver(new PlaylistObserver(this)), 172 fController(new Controller), 173 fControllerObserver(new ControllerObserver(this, 174 OBSERVE_FILE_CHANGES | OBSERVE_TRACK_CHANGES 175 | OBSERVE_PLAYBACK_STATE_CHANGES | OBSERVE_POSITION_CHANGES 176 | OBSERVE_VOLUME_CHANGES)), 177 fIsFullscreen(false), 178 fAlwaysOnTop(false), 179 fNoInterface(false), 180 fShowsFullscreenControls(false), 181 fSourceWidth(-1), 182 fSourceHeight(-1), 183 fWidthAspect(0), 184 fHeightAspect(0), 185 fSavedFrame(), 186 fNoVideoFrame(), 187 188 fMouseDownTracking(false), 189 fLastMousePos(0, 0), 190 fLastMouseMovedTime(system_time()), 191 fMouseMoveDist(0), 192 193 fGlobalSettingsListener(this), 194 fInitialSeekPosition(0), 195 fAllowWinding(true) 196 { 197 // Handle window position and size depending on whether this is the 198 // first window or not. Use the window size from the window that was 199 // last resized by the user. 200 static int pos = 0; 201 MoveBy(pos * 25, pos * 25); 202 pos = (pos + 1) % 15; 203 204 BRect frame = Settings::Default()->CurrentSettings() 205 .audioPlayerWindowFrame; 206 if (frame.IsValid()) { 207 if (isFirstWindow) { 208 if (message == NULL) { 209 MoveTo(frame.LeftTop()); 210 ResizeTo(frame.Width(), frame.Height()); 211 } else { 212 // Delay moving to the initial position, since we don't 213 // know if we will be playing audio at all. 214 message->AddRect("window frame", frame); 215 } 216 } 217 if (sNoVideoWidth == MIN_WIDTH) 218 sNoVideoWidth = frame.IntegerWidth(); 219 } else if (sNoVideoWidth > MIN_WIDTH) { 220 ResizeTo(sNoVideoWidth, Bounds().Height()); 221 } 222 fNoVideoWidth = sNoVideoWidth; 223 224 BRect rect = Bounds(); 225 226 // background 227 fBackground = new BView(rect, "background", B_FOLLOW_ALL, 228 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE); 229 fBackground->SetViewColor(0, 0, 0); 230 AddChild(fBackground); 231 232 // menu 233 fMenuBar = new BMenuBar(fBackground->Bounds(), "menu"); 234 _CreateMenu(); 235 fBackground->AddChild(fMenuBar); 236 fMenuBar->SetResizingMode(B_FOLLOW_NONE); 237 fMenuBar->ResizeToPreferred(); 238 fMenuBarWidth = (int)fMenuBar->Frame().Width() + 1; 239 fMenuBarHeight = (int)fMenuBar->Frame().Height() + 1; 240 241 // video view 242 rect = BRect(0, fMenuBarHeight, fBackground->Bounds().right, 243 fMenuBarHeight + 10); 244 fVideoView = new VideoView(rect, "video display", B_FOLLOW_NONE); 245 fBackground->AddChild(fVideoView); 246 247 // controls 248 rect = BRect(0, fMenuBarHeight + 11, fBackground->Bounds().right, 249 fBackground->Bounds().bottom); 250 fControls = new ControllerView(rect, fController, fPlaylist); 251 fBackground->AddChild(fControls); 252 fControls->ResizeToPreferred(); 253 fControlsHeight = (int)fControls->Frame().Height() + 1; 254 fControlsWidth = (int)fControls->Frame().Width() + 1; 255 fControls->SetResizingMode(B_FOLLOW_BOTTOM | B_FOLLOW_LEFT_RIGHT); 256 fControls->SetDisabledString(kDisabledSeekMessage); 257 258 fPlaylist->AddListener(fPlaylistObserver); 259 fController->SetVideoView(fVideoView); 260 fController->AddListener(fControllerObserver); 261 PeakView* peakView = fControls->GetPeakView(); 262 peakView->SetPeakNotificationWhat(MSG_PEAK_NOTIFICATION); 263 fController->SetPeakListener(peakView); 264 265 _SetupWindow(); 266 267 // setup the playlist window now, we need to have it 268 // running for the undo/redo playlist editing 269 fPlaylistWindow = new PlaylistWindow(BRect(150, 150, 500, 600), fPlaylist, 270 fController); 271 fPlaylistWindow->Hide(); 272 fPlaylistWindow->Show(); 273 // this makes sure the window thread is running without 274 // showing the window just yet 275 276 Settings::Default()->AddListener(&fGlobalSettingsListener); 277 _AdoptGlobalSettings(); 278 279 AddShortcut('z', B_COMMAND_KEY, new BMessage(B_UNDO)); 280 AddShortcut('y', B_COMMAND_KEY, new BMessage(B_UNDO)); 281 AddShortcut('z', B_COMMAND_KEY | B_SHIFT_KEY, new BMessage(B_REDO)); 282 AddShortcut('y', B_COMMAND_KEY | B_SHIFT_KEY, new BMessage(B_REDO)); 283 284 Hide(); 285 Show(); 286 287 if (message != NULL) 288 PostMessage(message); 289 } 290 291 292 MainWin::~MainWin() 293 { 294 // printf("MainWin::~MainWin\n"); 295 296 Settings::Default()->RemoveListener(&fGlobalSettingsListener); 297 fPlaylist->RemoveListener(fPlaylistObserver); 298 fController->Lock(); 299 fController->RemoveListener(fControllerObserver); 300 fController->SetPeakListener(NULL); 301 fController->SetVideoTarget(NULL); 302 fController->Unlock(); 303 304 // give the views a chance to detach from any notifiers 305 // before we delete them 306 fBackground->RemoveSelf(); 307 delete fBackground; 308 309 if (fInfoWin && fInfoWin->Lock()) 310 fInfoWin->Quit(); 311 312 if (fPlaylistWindow && fPlaylistWindow->Lock()) 313 fPlaylistWindow->Quit(); 314 315 delete fPlaylist; 316 fPlaylist = NULL; 317 318 // quit the Controller looper thread 319 thread_id controllerThread = fController->Thread(); 320 fController->PostMessage(B_QUIT_REQUESTED); 321 status_t exitValue; 322 wait_for_thread(controllerThread, &exitValue); 323 } 324 325 326 // #pragma mark - 327 328 329 void 330 MainWin::FrameResized(float newWidth, float newHeight) 331 { 332 if (newWidth != Bounds().Width() || newHeight != Bounds().Height()) { 333 debugger("size wrong\n"); 334 } 335 336 bool noMenu = fNoInterface || fIsFullscreen; 337 bool noControls = fNoInterface || fIsFullscreen; 338 339 // printf("FrameResized enter: newWidth %.0f, newHeight %.0f\n", 340 // newWidth, newHeight); 341 342 if (!fHasVideo) 343 sNoVideoWidth = fNoVideoWidth = (int)newWidth; 344 345 int maxVideoWidth = int(newWidth) + 1; 346 int maxVideoHeight = int(newHeight) + 1 347 - (noMenu ? 0 : fMenuBarHeight) 348 - (noControls ? 0 : fControlsHeight); 349 350 ASSERT(maxVideoHeight >= 0); 351 352 int y = 0; 353 354 if (noMenu) { 355 if (!fMenuBar->IsHidden(fMenuBar)) 356 fMenuBar->Hide(); 357 } else { 358 fMenuBar->MoveTo(0, y); 359 fMenuBar->ResizeTo(newWidth, fMenuBarHeight - 1); 360 if (fMenuBar->IsHidden(fMenuBar)) 361 fMenuBar->Show(); 362 y += fMenuBarHeight; 363 } 364 365 if (maxVideoHeight == 0) { 366 if (!fVideoView->IsHidden(fVideoView)) 367 fVideoView->Hide(); 368 } else { 369 _ResizeVideoView(0, y, maxVideoWidth, maxVideoHeight); 370 if (fVideoView->IsHidden(fVideoView)) 371 fVideoView->Show(); 372 y += maxVideoHeight; 373 } 374 375 if (noControls) { 376 if (!fControls->IsHidden(fControls)) 377 fControls->Hide(); 378 } else { 379 fControls->MoveTo(0, y); 380 fControls->ResizeTo(newWidth, fControlsHeight - 1); 381 if (fControls->IsHidden(fControls)) 382 fControls->Show(); 383 // y += fControlsHeight; 384 } 385 386 // printf("FrameResized leave\n"); 387 } 388 389 390 void 391 MainWin::Zoom(BPoint /*position*/, float /*width*/, float /*height*/) 392 { 393 PostMessage(M_TOGGLE_FULLSCREEN); 394 } 395 396 397 void 398 MainWin::DispatchMessage(BMessage* msg, BHandler* handler) 399 { 400 if ((msg->what == B_MOUSE_DOWN) 401 && (handler == fBackground || handler == fVideoView 402 || handler == fControls)) { 403 _MouseDown(msg, dynamic_cast<BView*>(handler)); 404 } 405 406 if ((msg->what == B_MOUSE_MOVED) 407 && (handler == fBackground || handler == fVideoView 408 || handler == fControls)) { 409 _MouseMoved(msg, dynamic_cast<BView*>(handler)); 410 } 411 412 if ((msg->what == B_MOUSE_UP) 413 && (handler == fBackground || handler == fVideoView)) { 414 _MouseUp(msg); 415 } 416 417 if ((msg->what == B_KEY_DOWN) 418 && (handler == fBackground || handler == fVideoView)) { 419 // special case for PrintScreen key 420 if (msg->FindInt32("key") == B_PRINT_KEY) { 421 fVideoView->OverlayScreenshotPrepare(); 422 BWindow::DispatchMessage(msg, handler); 423 fVideoView->OverlayScreenshotCleanup(); 424 return; 425 } 426 427 // every other key gets dispatched to our _KeyDown first 428 if (_KeyDown(msg)) { 429 // it got handled, don't pass it on 430 return; 431 } 432 } 433 434 BWindow::DispatchMessage(msg, handler); 435 } 436 437 438 void 439 MainWin::MessageReceived(BMessage* msg) 440 { 441 // msg->PrintToStream(); 442 switch (msg->what) { 443 case B_EXECUTE_PROPERTY: 444 case B_GET_PROPERTY: 445 case B_SET_PROPERTY: 446 { 447 BMessage reply(B_REPLY); 448 status_t result = B_BAD_SCRIPT_SYNTAX; 449 int32 index; 450 BMessage specifier; 451 int32 what; 452 const char* property; 453 454 if (msg->GetCurrentSpecifier(&index, &specifier, &what, 455 &property) != B_OK) { 456 return BWindow::MessageReceived(msg); 457 } 458 459 BPropertyInfo propertyInfo(sPropertyInfo); 460 switch (propertyInfo.FindMatch(msg, index, &specifier, what, 461 property)) { 462 case 0: 463 fControls->SkipForward(); 464 result = B_OK; 465 break; 466 467 case 1: 468 fControls->SkipBackward(); 469 result = B_OK; 470 break; 471 472 case 2: 473 fController->Play(); 474 result = B_OK; 475 break; 476 477 case 3: 478 fController->Stop(); 479 result = B_OK; 480 break; 481 482 case 4: 483 fController->Pause(); 484 result = B_OK; 485 break; 486 487 case 5: 488 fController->TogglePlaying(); 489 result = B_OK; 490 break; 491 492 case 6: 493 fController->ToggleMute(); 494 result = B_OK; 495 break; 496 497 case 7: 498 { 499 if (msg->what == B_GET_PROPERTY) { 500 result = reply.AddFloat("result", 501 fController->Volume()); 502 } else if (msg->what == B_SET_PROPERTY) { 503 float newVolume; 504 result = msg->FindFloat("data", &newVolume); 505 if (result == B_OK) 506 fController->SetVolume(newVolume); 507 } 508 break; 509 } 510 511 case 8: 512 { 513 if (msg->what == B_GET_PROPERTY) { 514 BAutolock _(fPlaylist); 515 const PlaylistItem* item = fController->Item(); 516 if (item == NULL) { 517 result = B_NO_INIT; 518 break; 519 } 520 521 result = reply.AddString("result", item->LocationURI()); 522 } 523 break; 524 } 525 526 default: 527 return BWindow::MessageReceived(msg); 528 } 529 530 if (result != B_OK) { 531 reply.what = B_MESSAGE_NOT_UNDERSTOOD; 532 reply.AddString("message", strerror(result)); 533 reply.AddInt32("error", result); 534 } 535 536 msg->SendReply(&reply); 537 break; 538 } 539 540 case B_REFS_RECEIVED: 541 _RefsReceived(msg); 542 break; 543 case B_SIMPLE_DATA: 544 if (msg->HasRef("refs")) 545 _RefsReceived(msg); 546 break; 547 case M_OPEN_PREVIOUS_PLAYLIST: 548 OpenPlaylist(msg); 549 break; 550 551 case B_UNDO: 552 case B_REDO: 553 fPlaylistWindow->PostMessage(msg); 554 break; 555 556 case M_MEDIA_SERVER_STARTED: 557 { 558 printf("TODO: implement M_MEDIA_SERVER_STARTED\n"); 559 // 560 // BAutolock _(fPlaylist); 561 // BMessage fakePlaylistMessage(MSG_PLAYLIST_CURRENT_ITEM_CHANGED); 562 // fakePlaylistMessage.AddInt32("index", 563 // fPlaylist->CurrentItemIndex()); 564 // PostMessage(&fakePlaylistMessage); 565 break; 566 } 567 568 case M_MEDIA_SERVER_QUIT: 569 printf("TODO: implement M_MEDIA_SERVER_QUIT\n"); 570 // if (fController->Lock()) { 571 // fController->CleanupNodes(); 572 // fController->Unlock(); 573 // } 574 break; 575 576 // PlaylistObserver messages 577 case MSG_PLAYLIST_ITEM_ADDED: 578 { 579 PlaylistItem* item; 580 int32 index; 581 if (msg->FindPointer("item", (void**)&item) == B_OK 582 && msg->FindInt32("index", &index) == B_OK) { 583 _AddPlaylistItem(item, index); 584 } 585 break; 586 } 587 case MSG_PLAYLIST_ITEM_REMOVED: 588 { 589 int32 index; 590 if (msg->FindInt32("index", &index) == B_OK) 591 _RemovePlaylistItem(index); 592 break; 593 } 594 case MSG_PLAYLIST_CURRENT_ITEM_CHANGED: 595 { 596 BAutolock _(fPlaylist); 597 598 int32 index; 599 if (msg->FindInt32("index", &index) < B_OK 600 || index != fPlaylist->CurrentItemIndex()) 601 break; 602 PlaylistItemRef item(fPlaylist->ItemAt(index)); 603 if (item.Get() != NULL) { 604 printf("open playlist item: %s\n", item->Name().String()); 605 OpenPlaylistItem(item); 606 _MarkPlaylistItem(index); 607 } 608 break; 609 } 610 case MSG_PLAYLIST_IMPORT_FAILED: 611 { 612 BAlert* alert = new BAlert("Nothing to Play", "None of the files " 613 "you wanted to play appear to be media files.", "OK"); 614 alert->Go(); 615 fControls->SetDisabledString(kDisabledSeekMessage); 616 break; 617 } 618 619 // ControllerObserver messages 620 case MSG_CONTROLLER_FILE_FINISHED: 621 { 622 BAutolock _(fPlaylist); 623 624 bool hadNext = fPlaylist->SetCurrentItemIndex( 625 fPlaylist->CurrentItemIndex() + 1); 626 if (!hadNext) { 627 // Reached end of playlist 628 // Handle "quit when done" settings 629 if ((fHasVideo && fCloseWhenDonePlayingMovie) 630 || (!fHasVideo && fCloseWhenDonePlayingSound)) 631 PostMessage(B_QUIT_REQUESTED); 632 // Handle "loop by default" settings 633 if ((fHasVideo && fLoopMovies) 634 || (!fHasVideo && fLoopSounds)) { 635 if (fPlaylist->CountItems() > 1) 636 fPlaylist->SetCurrentItemIndex(0); 637 else 638 fController->Play(); 639 } 640 } 641 break; 642 } 643 case MSG_CONTROLLER_FILE_CHANGED: 644 { 645 status_t result = B_ERROR; 646 msg->FindInt32("result", &result); 647 PlaylistItemRef itemRef; 648 PlaylistItem* item; 649 if (msg->FindPointer("item", (void**)&item) == B_OK) { 650 itemRef.SetTo(item, true); 651 // The reference was passed along with the message. 652 } else { 653 BAutolock _(fPlaylist); 654 itemRef.SetTo(fPlaylist->ItemAt( 655 fPlaylist->CurrentItemIndex())); 656 } 657 _PlaylistItemOpened(itemRef, result); 658 break; 659 } 660 case MSG_CONTROLLER_VIDEO_TRACK_CHANGED: 661 { 662 int32 index; 663 if (msg->FindInt32("index", &index) == B_OK) { 664 int32 i = 0; 665 while (BMenuItem* item = fVideoTrackMenu->ItemAt(i)) { 666 item->SetMarked(i == index); 667 i++; 668 } 669 } 670 break; 671 } 672 case MSG_CONTROLLER_AUDIO_TRACK_CHANGED: 673 { 674 int32 index; 675 if (msg->FindInt32("index", &index) == B_OK) { 676 int32 i = 0; 677 while (BMenuItem* item = fAudioTrackMenu->ItemAt(i)) { 678 item->SetMarked(i == index); 679 i++; 680 } 681 _UpdateAudioChannelCount(index); 682 } 683 break; 684 } 685 case MSG_CONTROLLER_SUB_TITLE_TRACK_CHANGED: 686 { 687 int32 index; 688 if (msg->FindInt32("index", &index) == B_OK) { 689 int32 i = 0; 690 while (BMenuItem* item = fSubTitleTrackMenu->ItemAt(i)) { 691 BMessage* message = item->Message(); 692 if (message != NULL) { 693 item->SetMarked((int32)message->what 694 - M_SELECT_SUB_TITLE_TRACK == index); 695 } 696 i++; 697 } 698 } 699 break; 700 } 701 case MSG_CONTROLLER_PLAYBACK_STATE_CHANGED: 702 { 703 uint32 state; 704 if (msg->FindInt32("state", (int32*)&state) == B_OK) 705 fControls->SetPlaybackState(state); 706 break; 707 } 708 case MSG_CONTROLLER_POSITION_CHANGED: 709 { 710 float position; 711 if (msg->FindFloat("position", &position) == B_OK) { 712 fControls->SetPosition(position, fController->TimePosition(), 713 fController->TimeDuration()); 714 fAllowWinding = true; 715 } 716 break; 717 } 718 case MSG_CONTROLLER_SEEK_HANDLED: 719 break; 720 721 case MSG_CONTROLLER_VOLUME_CHANGED: 722 { 723 float volume; 724 if (msg->FindFloat("volume", &volume) == B_OK) 725 fControls->SetVolume(volume); 726 break; 727 } 728 case MSG_CONTROLLER_MUTED_CHANGED: 729 { 730 bool muted; 731 if (msg->FindBool("muted", &muted) == B_OK) 732 fControls->SetMuted(muted); 733 break; 734 } 735 736 // menu item messages 737 case M_FILE_OPEN: 738 { 739 BMessenger target(this); 740 BMessage result(B_REFS_RECEIVED); 741 BMessage appMessage(M_SHOW_OPEN_PANEL); 742 appMessage.AddMessenger("target", target); 743 appMessage.AddMessage("message", &result); 744 appMessage.AddString("title", "Open Clips"); 745 appMessage.AddString("label", "Open"); 746 be_app->PostMessage(&appMessage); 747 break; 748 } 749 case M_FILE_INFO: 750 ShowFileInfo(); 751 break; 752 case M_FILE_PLAYLIST: 753 ShowPlaylistWindow(); 754 break; 755 case M_FILE_CLOSE: 756 PostMessage(B_QUIT_REQUESTED); 757 break; 758 case M_FILE_QUIT: 759 be_app->PostMessage(B_QUIT_REQUESTED); 760 break; 761 762 case M_TOGGLE_FULLSCREEN: 763 _ToggleFullscreen(); 764 break; 765 766 case M_TOGGLE_ALWAYS_ON_TOP: 767 _ToggleAlwaysOnTop(); 768 break; 769 770 case M_TOGGLE_NO_INTERFACE: 771 _ToggleNoInterface(); 772 break; 773 774 case M_VIEW_SIZE: 775 { 776 int32 size; 777 if (msg->FindInt32("size", &size) == B_OK) { 778 if (!fHasVideo) 779 break; 780 if (fIsFullscreen) 781 _ToggleFullscreen(); 782 _ResizeWindow(size); 783 } 784 break; 785 } 786 787 /* 788 case B_ACQUIRE_OVERLAY_LOCK: 789 printf("B_ACQUIRE_OVERLAY_LOCK\n"); 790 fVideoView->OverlayLockAcquire(); 791 break; 792 793 case B_RELEASE_OVERLAY_LOCK: 794 printf("B_RELEASE_OVERLAY_LOCK\n"); 795 fVideoView->OverlayLockRelease(); 796 break; 797 */ 798 case B_MOUSE_WHEEL_CHANGED: 799 { 800 float dx = msg->FindFloat("be:wheel_delta_x"); 801 float dy = msg->FindFloat("be:wheel_delta_y"); 802 bool inv = modifiers() & B_COMMAND_KEY; 803 if (dx > 0.1) 804 PostMessage(inv ? M_VOLUME_DOWN : M_SKIP_PREV); 805 if (dx < -0.1) 806 PostMessage(inv ? M_VOLUME_UP : M_SKIP_NEXT); 807 if (dy > 0.1) 808 PostMessage(inv ? M_SKIP_PREV : M_VOLUME_DOWN); 809 if (dy < -0.1) 810 PostMessage(inv ? M_SKIP_NEXT : M_VOLUME_UP); 811 break; 812 } 813 814 case M_SKIP_NEXT: 815 fControls->SkipForward(); 816 break; 817 818 case M_SKIP_PREV: 819 fControls->SkipBackward(); 820 break; 821 822 case M_WIND: 823 { 824 if (!fAllowWinding) 825 break; 826 827 bigtime_t howMuch; 828 int64 frames; 829 if (msg->FindInt64("how much", &howMuch) != B_OK 830 || msg->FindInt64("frames", &frames) != B_OK) { 831 break; 832 } 833 834 if (fController->Lock()) { 835 if (fHasVideo && !fController->IsPlaying()) { 836 int64 newFrame = fController->CurrentFrame() + frames; 837 fController->SetFramePosition(newFrame); 838 } else { 839 bigtime_t seekTime = fController->TimePosition() + howMuch; 840 if (seekTime < 0) { 841 fInitialSeekPosition = seekTime; 842 PostMessage(M_SKIP_PREV); 843 } else if (seekTime > fController->TimeDuration()) { 844 fInitialSeekPosition = 0; 845 PostMessage(M_SKIP_NEXT); 846 } else 847 fController->SetTimePosition(seekTime); 848 } 849 fController->Unlock(); 850 851 fAllowWinding = false; 852 } 853 break; 854 } 855 856 case M_VOLUME_UP: 857 fController->VolumeUp(); 858 break; 859 860 case M_VOLUME_DOWN: 861 fController->VolumeDown(); 862 break; 863 864 case M_ASPECT_SAME_AS_SOURCE: 865 if (fHasVideo) { 866 int width; 867 int height; 868 int widthAspect; 869 int heightAspect; 870 fController->GetSize(&width, &height, 871 &widthAspect, &heightAspect); 872 VideoFormatChange(width, height, widthAspect, heightAspect); 873 } 874 break; 875 876 case M_ASPECT_NO_DISTORTION: 877 if (fHasVideo) { 878 int width; 879 int height; 880 fController->GetSize(&width, &height); 881 VideoFormatChange(width, height, width, height); 882 } 883 break; 884 885 case M_ASPECT_4_3: 886 VideoAspectChange(4, 3); 887 break; 888 889 case M_ASPECT_16_9: // 1.77 : 1 890 VideoAspectChange(16, 9); 891 break; 892 893 case M_ASPECT_83_50: // 1.66 : 1 894 VideoAspectChange(83, 50); 895 break; 896 897 case M_ASPECT_7_4: // 1.75 : 1 898 VideoAspectChange(7, 4); 899 break; 900 901 case M_ASPECT_37_20: // 1.85 : 1 902 VideoAspectChange(37, 20); 903 break; 904 905 case M_ASPECT_47_20: // 2.35 : 1 906 VideoAspectChange(47, 20); 907 break; 908 909 case M_SET_PLAYLIST_POSITION: 910 { 911 BAutolock _(fPlaylist); 912 913 int32 index; 914 if (msg->FindInt32("index", &index) == B_OK) 915 fPlaylist->SetCurrentItemIndex(index); 916 break; 917 } 918 919 case MSG_OBJECT_CHANGED: 920 // received from fGlobalSettingsListener 921 // TODO: find out which object, if we ever watch more than 922 // the global settings instance... 923 _AdoptGlobalSettings(); 924 break; 925 926 case M_SHOW_IF_NEEDED: 927 _ShowIfNeeded(); 928 break; 929 930 case M_SLIDE_CONTROLS: 931 { 932 float offset; 933 if (msg->FindFloat("offset", &offset) == B_OK) { 934 fControls->MoveBy(0, offset); 935 UpdateIfNeeded(); 936 snooze(15000); 937 } 938 break; 939 } 940 case M_FINISH_SLIDING_CONTROLS: 941 { 942 float offset; 943 bool show; 944 if (msg->FindFloat("offset", &offset) == B_OK 945 && msg->FindBool("show", &show) == B_OK) { 946 if (show) 947 fControls->MoveTo(fControls->Frame().left, offset); 948 else { 949 fControls->RemoveSelf(); 950 fControls->MoveTo(fVideoView->Frame().left, 951 fVideoView->Frame().bottom + 1); 952 fBackground->AddChild(fControls); 953 fControls->SetSymbolScale(1.0f); 954 while (!fControls->IsHidden()) 955 fControls->Hide(); 956 } 957 } 958 break; 959 } 960 case M_HIDE_FULL_SCREEN_CONTROLS: 961 if (fIsFullscreen) { 962 BPoint videoViewWhere; 963 if (msg->FindPoint("where", &videoViewWhere) == B_OK) { 964 if (!fControls->Frame().Contains(videoViewWhere)) { 965 _ShowFullscreenControls(false); 966 // hide the mouse cursor until the user moves it 967 be_app->ObscureCursor(); 968 } 969 } 970 } 971 break; 972 973 case M_SET_RATING: 974 { 975 int32 rating; 976 if (msg->FindInt32("rating", &rating) == B_OK) 977 _SetRating(rating); 978 break; 979 } 980 981 default: 982 if (msg->what >= M_SELECT_AUDIO_TRACK 983 && msg->what <= M_SELECT_AUDIO_TRACK_END) { 984 fController->SelectAudioTrack(msg->what - M_SELECT_AUDIO_TRACK); 985 break; 986 } 987 if (msg->what >= M_SELECT_VIDEO_TRACK 988 && msg->what <= M_SELECT_VIDEO_TRACK_END) { 989 fController->SelectVideoTrack(msg->what - M_SELECT_VIDEO_TRACK); 990 break; 991 } 992 if ((int32)msg->what >= M_SELECT_SUB_TITLE_TRACK - 1 993 && msg->what <= M_SELECT_SUB_TITLE_TRACK_END) { 994 fController->SelectSubTitleTrack((int32)msg->what 995 - M_SELECT_SUB_TITLE_TRACK); 996 break; 997 } 998 // let BWindow handle the rest 999 BWindow::MessageReceived(msg); 1000 } 1001 } 1002 1003 1004 void 1005 MainWin::WindowActivated(bool active) 1006 { 1007 fController->PlayerActivated(active); 1008 } 1009 1010 1011 bool 1012 MainWin::QuitRequested() 1013 { 1014 BMessage message(M_PLAYER_QUIT); 1015 GetQuitMessage(&message); 1016 be_app->PostMessage(&message); 1017 return true; 1018 } 1019 1020 1021 void 1022 MainWin::MenusBeginning() 1023 { 1024 _SetupVideoAspectItems(fVideoAspectMenu); 1025 } 1026 1027 1028 // #pragma mark - 1029 1030 1031 void 1032 MainWin::OpenPlaylist(const BMessage* playlistArchive) 1033 { 1034 if (playlistArchive == NULL) 1035 return; 1036 1037 BAutolock _(this); 1038 BAutolock playlistLocker(fPlaylist); 1039 1040 if (fPlaylist->Unarchive(playlistArchive) != B_OK) 1041 return; 1042 1043 int32 currentIndex; 1044 if (playlistArchive->FindInt32("index", ¤tIndex) != B_OK) 1045 currentIndex = 0; 1046 fPlaylist->SetCurrentItemIndex(currentIndex); 1047 1048 playlistLocker.Unlock(); 1049 1050 if (currentIndex != -1) { 1051 // Restore the current play position only if we have something to play 1052 playlistArchive->FindInt64("position", (int64*)&fInitialSeekPosition); 1053 } 1054 1055 if (IsHidden()) 1056 Show(); 1057 } 1058 1059 1060 void 1061 MainWin::OpenPlaylistItem(const PlaylistItemRef& item) 1062 { 1063 status_t ret = fController->SetToAsync(item); 1064 if (ret != B_OK) { 1065 fprintf(stderr, "MainWin::OpenPlaylistItem() - Failed to send message " 1066 "to Controller.\n"); 1067 (new BAlert("error", NAME" encountered an internal error. " 1068 "The file could not be opened.", "OK"))->Go(); 1069 _PlaylistItemOpened(item, ret); 1070 } else { 1071 BString string; 1072 string << "Opening '" << item->Name() << "'."; 1073 fControls->SetDisabledString(string.String()); 1074 1075 if (IsHidden()) { 1076 BMessage showMessage(M_SHOW_IF_NEEDED); 1077 BMessageRunner::StartSending(BMessenger(this), &showMessage, 1078 150000, 1); 1079 } 1080 } 1081 } 1082 1083 1084 void 1085 MainWin::ShowFileInfo() 1086 { 1087 if (!fInfoWin) 1088 fInfoWin = new InfoWin(Frame().LeftTop(), fController); 1089 1090 if (fInfoWin->Lock()) { 1091 if (fInfoWin->IsHidden()) 1092 fInfoWin->Show(); 1093 else 1094 fInfoWin->Activate(); 1095 fInfoWin->Unlock(); 1096 } 1097 } 1098 1099 1100 void 1101 MainWin::ShowPlaylistWindow() 1102 { 1103 if (fPlaylistWindow->Lock()) { 1104 // make sure the window shows on the same workspace as ourself 1105 uint32 workspaces = Workspaces(); 1106 if (fPlaylistWindow->Workspaces() != workspaces) 1107 fPlaylistWindow->SetWorkspaces(workspaces); 1108 1109 // show or activate 1110 if (fPlaylistWindow->IsHidden()) 1111 fPlaylistWindow->Show(); 1112 else 1113 fPlaylistWindow->Activate(); 1114 1115 fPlaylistWindow->Unlock(); 1116 } 1117 } 1118 1119 1120 void 1121 MainWin::VideoAspectChange(int forcedWidth, int forcedHeight, float widthScale) 1122 { 1123 // Force specific source size and pixel width scale. 1124 if (fHasVideo) { 1125 int width; 1126 int height; 1127 fController->GetSize(&width, &height); 1128 VideoFormatChange(forcedWidth, forcedHeight, 1129 lround(width * widthScale), height); 1130 } 1131 } 1132 1133 1134 void 1135 MainWin::VideoAspectChange(float widthScale) 1136 { 1137 // Called when video aspect ratio changes and the original 1138 // width/height should be restored too, display aspect is not known, 1139 // only pixel width scale. 1140 if (fHasVideo) { 1141 int width; 1142 int height; 1143 fController->GetSize(&width, &height); 1144 VideoFormatChange(width, height, lround(width * widthScale), height); 1145 } 1146 } 1147 1148 1149 void 1150 MainWin::VideoAspectChange(int widthAspect, int heightAspect) 1151 { 1152 // Called when video aspect ratio changes and the original 1153 // width/height should be restored too. 1154 if (fHasVideo) { 1155 int width; 1156 int height; 1157 fController->GetSize(&width, &height); 1158 VideoFormatChange(width, height, widthAspect, heightAspect); 1159 } 1160 } 1161 1162 1163 void 1164 MainWin::VideoFormatChange(int width, int height, int widthAspect, 1165 int heightAspect) 1166 { 1167 // Called when video format or aspect ratio changes. 1168 1169 printf("VideoFormatChange enter: width %d, height %d, " 1170 "aspect ratio: %d:%d\n", width, height, widthAspect, heightAspect); 1171 1172 // remember current view scale 1173 int percent = _CurrentVideoSizeInPercent(); 1174 1175 fSourceWidth = width; 1176 fSourceHeight = height; 1177 fWidthAspect = widthAspect; 1178 fHeightAspect = heightAspect; 1179 1180 if (percent == 100) 1181 _ResizeWindow(100); 1182 else 1183 FrameResized(Bounds().Width(), Bounds().Height()); 1184 1185 printf("VideoFormatChange leave\n"); 1186 } 1187 1188 1189 void 1190 MainWin::GetQuitMessage(BMessage* message) 1191 { 1192 message->AddPointer("instance", this); 1193 message->AddRect("window frame", Frame()); 1194 message->AddBool("audio only", !fHasVideo); 1195 message->AddInt64("creation time", fCreationTime); 1196 1197 if (!fHasVideo && fHasAudio) { 1198 // store playlist, current index and position if this is audio 1199 BMessage playlistArchive; 1200 1201 BAutolock controllerLocker(fController); 1202 playlistArchive.AddInt64("position", fController->TimePosition()); 1203 controllerLocker.Unlock(); 1204 1205 if (!fPlaylist) 1206 return; 1207 1208 BAutolock playlistLocker(fPlaylist); 1209 if (fPlaylist->Archive(&playlistArchive) != B_OK 1210 || playlistArchive.AddInt32("index", 1211 fPlaylist->CurrentItemIndex()) != B_OK 1212 || message->AddMessage("playlist", &playlistArchive) != B_OK) { 1213 fprintf(stderr, "Failed to store current playlist.\n"); 1214 } 1215 } 1216 } 1217 1218 1219 BHandler* 1220 MainWin::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier, 1221 int32 what, const char* property) 1222 { 1223 BPropertyInfo propertyInfo(sPropertyInfo); 1224 switch (propertyInfo.FindMatch(message, index, specifier, what, property)) { 1225 case 0: 1226 case 1: 1227 case 2: 1228 case 3: 1229 case 4: 1230 case 5: 1231 case 6: 1232 case 7: 1233 case 8: 1234 return this; 1235 } 1236 1237 return BWindow::ResolveSpecifier(message, index, specifier, what, property); 1238 } 1239 1240 1241 status_t 1242 MainWin::GetSupportedSuites(BMessage* data) 1243 { 1244 if (data == NULL) 1245 return B_BAD_VALUE; 1246 1247 status_t status = data->AddString("suites", "suite/vnd.Haiku-MediaPlayer"); 1248 if (status != B_OK) 1249 return status; 1250 1251 BPropertyInfo propertyInfo(sPropertyInfo); 1252 status = data->AddFlat("messages", &propertyInfo); 1253 if (status != B_OK) 1254 return status; 1255 1256 return BWindow::GetSupportedSuites(data); 1257 } 1258 1259 1260 // #pragma mark - 1261 1262 1263 void 1264 MainWin::_RefsReceived(BMessage* message) 1265 { 1266 // the playlist is replaced by dropped files 1267 // or the dropped files are appended to the end 1268 // of the existing playlist if <shift> is pressed 1269 bool append = false; 1270 if (message->FindBool("append to playlist", &append) != B_OK) 1271 append = modifiers() & B_SHIFT_KEY; 1272 1273 BAutolock _(fPlaylist); 1274 int32 appendIndex = append ? APPEND_INDEX_APPEND_LAST 1275 : APPEND_INDEX_REPLACE_PLAYLIST; 1276 message->AddInt32("append_index", appendIndex); 1277 1278 // forward the message to the playlist window, 1279 // so that undo/redo is used for modifying the playlist 1280 fPlaylistWindow->PostMessage(message); 1281 1282 if (message->FindRect("window frame", &fNoVideoFrame) != B_OK) 1283 fNoVideoFrame = BRect(); 1284 _ShowIfNeeded(); 1285 } 1286 1287 1288 void 1289 MainWin::_PlaylistItemOpened(const PlaylistItemRef& item, status_t result) 1290 { 1291 if (result != B_OK) { 1292 BAutolock _(fPlaylist); 1293 1294 item->SetPlaybackFailed(); 1295 bool allItemsFailed = true; 1296 int32 count = fPlaylist->CountItems(); 1297 for (int32 i = 0; i < count; i++) { 1298 if (!fPlaylist->ItemAtFast(i)->PlaybackFailed()) { 1299 allItemsFailed = false; 1300 break; 1301 } 1302 } 1303 1304 if (allItemsFailed) { 1305 // Display error if all files failed to play. 1306 BString message; 1307 message << "The file '"; 1308 message << item->Name(); 1309 message << "' could not be opened.\n\n"; 1310 1311 if (result == B_MEDIA_NO_HANDLER) { 1312 // give a more detailed message for the most likely of all 1313 // errors 1314 message << "There is no decoder installed to handle the " 1315 "file format, or the decoder has trouble with the " 1316 "specific version of the format."; 1317 } else { 1318 message << "Error: " << strerror(result); 1319 } 1320 (new BAlert("error", message.String(), "OK"))->Go(); 1321 fControls->SetDisabledString(kDisabledSeekMessage); 1322 } else { 1323 // Just go to the next file and don't bother user (yet) 1324 fPlaylist->SetCurrentItemIndex(fPlaylist->CurrentItemIndex() + 1); 1325 } 1326 1327 fHasFile = false; 1328 fHasVideo = false; 1329 fHasAudio = false; 1330 SetTitle(NAME); 1331 } else { 1332 fHasFile = true; 1333 fHasVideo = fController->VideoTrackCount() != 0; 1334 fHasAudio = fController->AudioTrackCount() != 0; 1335 SetTitle(item->Name().String()); 1336 1337 if (fInitialSeekPosition < 0) { 1338 fInitialSeekPosition 1339 = fController->TimeDuration() + fInitialSeekPosition; 1340 } 1341 fController->SetTimePosition(fInitialSeekPosition); 1342 fInitialSeekPosition = 0; 1343 } 1344 _SetupWindow(); 1345 1346 if (result == B_OK) 1347 _UpdatePlaylistItemFile(); 1348 } 1349 1350 1351 void 1352 MainWin::_SetupWindow() 1353 { 1354 // printf("MainWin::_SetupWindow\n"); 1355 // Populate the track menus 1356 _SetupTrackMenus(fAudioTrackMenu, fVideoTrackMenu, fSubTitleTrackMenu); 1357 _UpdateAudioChannelCount(fController->CurrentAudioTrack()); 1358 1359 fVideoMenu->SetEnabled(fHasVideo); 1360 fAudioMenu->SetEnabled(fHasAudio); 1361 int previousSourceWidth = fSourceWidth; 1362 int previousSourceHeight = fSourceHeight; 1363 int previousWidthAspect = fWidthAspect; 1364 int previousHeightAspect = fHeightAspect; 1365 if (fHasVideo) { 1366 fController->GetSize(&fSourceWidth, &fSourceHeight, 1367 &fWidthAspect, &fHeightAspect); 1368 } else { 1369 fSourceWidth = 0; 1370 fSourceHeight = 0; 1371 fWidthAspect = 1; 1372 fHeightAspect = 1; 1373 } 1374 _UpdateControlsEnabledStatus(); 1375 1376 _ShowIfNeeded(); 1377 1378 // Adopt the size and window layout if necessary 1379 if (previousSourceWidth != fSourceWidth 1380 || previousSourceHeight != fSourceHeight 1381 || previousWidthAspect != fWidthAspect 1382 || previousHeightAspect != fHeightAspect) { 1383 1384 _SetWindowSizeLimits(); 1385 1386 if (!fIsFullscreen) { 1387 // Resize to 100% but stay on screen 1388 _ResizeWindow(100, !fHasVideo, true); 1389 } else { 1390 // Make sure we relayout the video view when in full screen mode 1391 FrameResized(Frame().Width(), Frame().Height()); 1392 } 1393 } 1394 1395 fVideoView->MakeFocus(); 1396 } 1397 1398 1399 void 1400 MainWin::_CreateMenu() 1401 { 1402 fFileMenu = new BMenu(NAME); 1403 fPlaylistMenu = new BMenu("Playlist"B_UTF8_ELLIPSIS); 1404 fAudioMenu = new BMenu("Audio"); 1405 fVideoMenu = new BMenu("Video"); 1406 fVideoAspectMenu = new BMenu("Aspect ratio"); 1407 fAudioTrackMenu = new BMenu("Track"); 1408 fVideoTrackMenu = new BMenu("Track"); 1409 fSubTitleTrackMenu = new BMenu("Subtitles"); 1410 fAttributesMenu = new BMenu("Attributes"); 1411 1412 fMenuBar->AddItem(fFileMenu); 1413 fMenuBar->AddItem(fAudioMenu); 1414 fMenuBar->AddItem(fVideoMenu); 1415 fMenuBar->AddItem(fAttributesMenu); 1416 1417 BMenuItem* item = new BMenuItem("New player"B_UTF8_ELLIPSIS, 1418 new BMessage(M_NEW_PLAYER), 'N'); 1419 fFileMenu->AddItem(item); 1420 item->SetTarget(be_app); 1421 1422 #if 0 1423 // Plain "Open File" entry 1424 fFileMenu->AddItem(new BMenuItem("Open File"B_UTF8_ELLIPSIS, 1425 new BMessage(M_FILE_OPEN), 'O')); 1426 #else 1427 // Add recent files to "Open File" entry as sub-menu. 1428 BRecentFilesList recentFiles(10, false, NULL, kAppSig); 1429 item = new BMenuItem(recentFiles.NewFileListMenu( 1430 "Open file"B_UTF8_ELLIPSIS, new BMessage(B_REFS_RECEIVED), 1431 NULL, this, 10, false, NULL, 0, kAppSig), new BMessage(M_FILE_OPEN)); 1432 item->SetShortcut('O', 0); 1433 fFileMenu->AddItem(item); 1434 #endif 1435 1436 fFileMenu->AddSeparatorItem(); 1437 1438 fFileMenu->AddItem(new BMenuItem("File info"B_UTF8_ELLIPSIS, 1439 new BMessage(M_FILE_INFO), 'I')); 1440 fFileMenu->AddItem(fPlaylistMenu); 1441 fPlaylistMenu->Superitem()->SetShortcut('P', B_COMMAND_KEY); 1442 fPlaylistMenu->Superitem()->SetMessage(new BMessage(M_FILE_PLAYLIST)); 1443 1444 fFileMenu->AddSeparatorItem(); 1445 1446 fNoInterfaceMenuItem = new BMenuItem("Hide interface", 1447 new BMessage(M_TOGGLE_NO_INTERFACE), 'H'); 1448 fFileMenu->AddItem(fNoInterfaceMenuItem); 1449 fFileMenu->AddItem(new BMenuItem("Always on top", 1450 new BMessage(M_TOGGLE_ALWAYS_ON_TOP), 'A')); 1451 1452 item = new BMenuItem("Settings"B_UTF8_ELLIPSIS, 1453 new BMessage(M_SETTINGS), 'S'); 1454 fFileMenu->AddItem(item); 1455 item->SetTarget(be_app); 1456 1457 fFileMenu->AddSeparatorItem(); 1458 1459 item = new BMenuItem("About " NAME B_UTF8_ELLIPSIS, 1460 new BMessage(B_ABOUT_REQUESTED)); 1461 fFileMenu->AddItem(item); 1462 item->SetTarget(be_app); 1463 1464 fFileMenu->AddSeparatorItem(); 1465 1466 fFileMenu->AddItem(new BMenuItem("Close", new BMessage(M_FILE_CLOSE), 'W')); 1467 fFileMenu->AddItem(new BMenuItem("Quit", new BMessage(M_FILE_QUIT), 'Q')); 1468 1469 fPlaylistMenu->SetRadioMode(true); 1470 1471 fAudioMenu->AddItem(fAudioTrackMenu); 1472 1473 fVideoMenu->AddItem(fVideoTrackMenu); 1474 fVideoMenu->AddItem(fSubTitleTrackMenu); 1475 fVideoMenu->AddSeparatorItem(); 1476 BMessage* resizeMessage = new BMessage(M_VIEW_SIZE); 1477 resizeMessage->AddInt32("size", 50); 1478 fVideoMenu->AddItem(new BMenuItem("50% scale", resizeMessage, '0')); 1479 1480 resizeMessage = new BMessage(M_VIEW_SIZE); 1481 resizeMessage->AddInt32("size", 100); 1482 fVideoMenu->AddItem(new BMenuItem("100% scale", resizeMessage, '1')); 1483 1484 resizeMessage = new BMessage(M_VIEW_SIZE); 1485 resizeMessage->AddInt32("size", 200); 1486 fVideoMenu->AddItem(new BMenuItem("200% scale", resizeMessage, '2')); 1487 1488 resizeMessage = new BMessage(M_VIEW_SIZE); 1489 resizeMessage->AddInt32("size", 300); 1490 fVideoMenu->AddItem(new BMenuItem("300% scale", resizeMessage, '3')); 1491 1492 resizeMessage = new BMessage(M_VIEW_SIZE); 1493 resizeMessage->AddInt32("size", 400); 1494 fVideoMenu->AddItem(new BMenuItem("400% scale", resizeMessage, '4')); 1495 1496 fVideoMenu->AddSeparatorItem(); 1497 1498 fVideoMenu->AddItem(new BMenuItem("Full screen", 1499 new BMessage(M_TOGGLE_FULLSCREEN), 'F')); 1500 1501 fVideoMenu->AddSeparatorItem(); 1502 1503 _SetupVideoAspectItems(fVideoAspectMenu); 1504 fVideoMenu->AddItem(fVideoAspectMenu); 1505 1506 fRatingMenu = new BMenu("Rating"); 1507 fAttributesMenu->AddItem(fRatingMenu); 1508 for (int32 i = 1; i <= 10; i++) { 1509 char label[16]; 1510 snprintf(label, sizeof(label), "%ld", i); 1511 BMessage* setRatingMsg = new BMessage(M_SET_RATING); 1512 setRatingMsg->AddInt32("rating", i); 1513 fRatingMenu->AddItem(new BMenuItem(label, setRatingMsg)); 1514 } 1515 } 1516 1517 1518 void 1519 MainWin::_SetupVideoAspectItems(BMenu* menu) 1520 { 1521 BMenuItem* item; 1522 while ((item = menu->RemoveItem(0L)) != NULL) 1523 delete item; 1524 1525 int width; 1526 int height; 1527 int widthAspect; 1528 int heightAspect; 1529 fController->GetSize(&width, &height, &widthAspect, &heightAspect); 1530 // We don't care if there is a video track at all. In that 1531 // case we should end up not marking any item. 1532 1533 // NOTE: The item marking may end up marking for example both 1534 // "Stream Settings" and "16 : 9" if the stream settings happen to 1535 // be "16 : 9". 1536 1537 menu->AddItem(item = new BMenuItem("Stream settings", 1538 new BMessage(M_ASPECT_SAME_AS_SOURCE))); 1539 item->SetMarked(widthAspect == fWidthAspect 1540 && heightAspect == fHeightAspect); 1541 1542 menu->AddItem(item = new BMenuItem("No aspect correction", 1543 new BMessage(M_ASPECT_NO_DISTORTION))); 1544 item->SetMarked(width == fWidthAspect && height == fHeightAspect); 1545 1546 menu->AddSeparatorItem(); 1547 1548 menu->AddItem(item = new BMenuItem("4 : 3", 1549 new BMessage(M_ASPECT_4_3))); 1550 item->SetMarked(fWidthAspect == 4 && fHeightAspect == 3); 1551 menu->AddItem(item = new BMenuItem("16 : 9", 1552 new BMessage(M_ASPECT_16_9))); 1553 item->SetMarked(fWidthAspect == 16 && fHeightAspect == 9); 1554 1555 menu->AddSeparatorItem(); 1556 1557 menu->AddItem(item = new BMenuItem("1.66 : 1", 1558 new BMessage(M_ASPECT_83_50))); 1559 item->SetMarked(fWidthAspect == 83 && fHeightAspect == 50); 1560 menu->AddItem(item = new BMenuItem("1.75 : 1", 1561 new BMessage(M_ASPECT_7_4))); 1562 item->SetMarked(fWidthAspect == 7 && fHeightAspect == 4); 1563 menu->AddItem(item = new BMenuItem("1.85 : 1 (American)", 1564 new BMessage(M_ASPECT_37_20))); 1565 item->SetMarked(fWidthAspect == 37 && fHeightAspect == 20); 1566 menu->AddItem(item = new BMenuItem("2.35 : 1 (Cinemascope)", 1567 new BMessage(M_ASPECT_47_20))); 1568 item->SetMarked(fWidthAspect == 47 && fHeightAspect == 20); 1569 } 1570 1571 1572 void 1573 MainWin::_SetupTrackMenus(BMenu* audioTrackMenu, BMenu* videoTrackMenu, 1574 BMenu* subTitleTrackMenu) 1575 { 1576 audioTrackMenu->RemoveItems(0, audioTrackMenu->CountItems(), true); 1577 videoTrackMenu->RemoveItems(0, videoTrackMenu->CountItems(), true); 1578 subTitleTrackMenu->RemoveItems(0, subTitleTrackMenu->CountItems(), true); 1579 1580 char s[100]; 1581 1582 int count = fController->AudioTrackCount(); 1583 int current = fController->CurrentAudioTrack(); 1584 for (int i = 0; i < count; i++) { 1585 BMessage metaData; 1586 const char* languageString = NULL; 1587 if (fController->GetAudioMetaData(i, &metaData) == B_OK) 1588 metaData.FindString("language", &languageString); 1589 if (languageString != NULL) { 1590 BLanguage language(languageString); 1591 BString languageName; 1592 if (language.GetTranslatedName(languageName) == B_OK) 1593 languageString = languageName.String(); 1594 snprintf(s, sizeof(s), "%s", languageString); 1595 } else 1596 snprintf(s, sizeof(s), "Track %d", i + 1); 1597 BMenuItem* item = new BMenuItem(s, 1598 new BMessage(M_SELECT_AUDIO_TRACK + i)); 1599 item->SetMarked(i == current); 1600 audioTrackMenu->AddItem(item); 1601 } 1602 if (count == 0) { 1603 audioTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY))); 1604 audioTrackMenu->ItemAt(0)->SetMarked(true); 1605 } 1606 1607 1608 count = fController->VideoTrackCount(); 1609 current = fController->CurrentVideoTrack(); 1610 for (int i = 0; i < count; i++) { 1611 snprintf(s, sizeof(s), "Track %d", i + 1); 1612 BMenuItem* item = new BMenuItem(s, 1613 new BMessage(M_SELECT_VIDEO_TRACK + i)); 1614 item->SetMarked(i == current); 1615 videoTrackMenu->AddItem(item); 1616 } 1617 if (count == 0) { 1618 videoTrackMenu->AddItem(new BMenuItem("none", new BMessage(M_DUMMY))); 1619 videoTrackMenu->ItemAt(0)->SetMarked(true); 1620 } 1621 1622 count = fController->SubTitleTrackCount(); 1623 if (count > 0) { 1624 current = fController->CurrentSubTitleTrack(); 1625 BMenuItem* item = new BMenuItem("Off", 1626 new BMessage(M_SELECT_SUB_TITLE_TRACK - 1)); 1627 subTitleTrackMenu->AddItem(item); 1628 item->SetMarked(current == -1); 1629 1630 subTitleTrackMenu->AddSeparatorItem(); 1631 1632 for (int i = 0; i < count; i++) { 1633 const char* name = fController->SubTitleTrackName(i); 1634 if (name != NULL) 1635 snprintf(s, sizeof(s), "%s", name); 1636 else 1637 snprintf(s, sizeof(s), "Track %d", i + 1); 1638 item = new BMenuItem(s, 1639 new BMessage(M_SELECT_SUB_TITLE_TRACK + i)); 1640 item->SetMarked(i == current); 1641 subTitleTrackMenu->AddItem(item); 1642 } 1643 } else { 1644 subTitleTrackMenu->AddItem(new BMenuItem("none", 1645 new BMessage(M_DUMMY))); 1646 subTitleTrackMenu->ItemAt(0)->SetMarked(true); 1647 } 1648 } 1649 1650 1651 void 1652 MainWin::_UpdateAudioChannelCount(int32 audioTrackIndex) 1653 { 1654 fControls->SetAudioChannelCount(fController->AudioTrackChannelCount()); 1655 } 1656 1657 1658 void 1659 MainWin::_GetMinimumWindowSize(int& width, int& height) const 1660 { 1661 width = MIN_WIDTH; 1662 height = 0; 1663 if (!fNoInterface) { 1664 width = max_c(width, fMenuBarWidth); 1665 width = max_c(width, fControlsWidth); 1666 height = fMenuBarHeight + fControlsHeight; 1667 } 1668 } 1669 1670 1671 void 1672 MainWin::_GetUnscaledVideoSize(int& videoWidth, int& videoHeight) const 1673 { 1674 if (fWidthAspect != 0 && fHeightAspect != 0) { 1675 videoWidth = fSourceHeight * fWidthAspect / fHeightAspect; 1676 videoHeight = fSourceWidth * fHeightAspect / fWidthAspect; 1677 // Use the scaling which produces an enlarged view. 1678 if (videoWidth > fSourceWidth) { 1679 // Enlarge width 1680 videoHeight = fSourceHeight; 1681 } else { 1682 // Enlarge height 1683 videoWidth = fSourceWidth; 1684 } 1685 } else { 1686 videoWidth = fSourceWidth; 1687 videoHeight = fSourceHeight; 1688 } 1689 } 1690 1691 1692 void 1693 MainWin::_SetWindowSizeLimits() 1694 { 1695 int minWidth; 1696 int minHeight; 1697 _GetMinimumWindowSize(minWidth, minHeight); 1698 SetSizeLimits(minWidth - 1, 32000, minHeight - 1, 1699 fHasVideo ? 32000 : minHeight - 1); 1700 } 1701 1702 1703 int 1704 MainWin::_CurrentVideoSizeInPercent() const 1705 { 1706 if (!fHasVideo) 1707 return 0; 1708 1709 int videoWidth; 1710 int videoHeight; 1711 _GetUnscaledVideoSize(videoWidth, videoHeight); 1712 1713 int viewWidth = fVideoView->Bounds().IntegerWidth() + 1; 1714 int viewHeight = fVideoView->Bounds().IntegerHeight() + 1; 1715 1716 int widthPercent = viewWidth * 100 / videoWidth; 1717 int heightPercent = viewHeight * 100 / videoHeight; 1718 1719 if (widthPercent > heightPercent) 1720 return widthPercent; 1721 return heightPercent; 1722 } 1723 1724 1725 void 1726 MainWin::_ZoomVideoView(int percentDiff) 1727 { 1728 if (!fHasVideo) 1729 return; 1730 1731 int percent = _CurrentVideoSizeInPercent(); 1732 int newSize = percent * (100 + percentDiff) / 100; 1733 1734 if (newSize < 25) 1735 newSize = 25; 1736 if (newSize > 400) 1737 newSize = 400; 1738 if (newSize != percent) { 1739 BMessage message(M_VIEW_SIZE); 1740 message.AddInt32("size", newSize); 1741 PostMessage(&message); 1742 } 1743 } 1744 1745 1746 void 1747 MainWin::_ResizeWindow(int percent, bool useNoVideoWidth, bool stayOnScreen) 1748 { 1749 // Get required window size 1750 int videoWidth; 1751 int videoHeight; 1752 _GetUnscaledVideoSize(videoWidth, videoHeight); 1753 1754 videoWidth = (videoWidth * percent) / 100; 1755 videoHeight = (videoHeight * percent) / 100; 1756 1757 // Calculate and set the minimum window size 1758 int width; 1759 int height; 1760 _GetMinimumWindowSize(width, height); 1761 1762 width = max_c(width, videoWidth) - 1; 1763 if (useNoVideoWidth) 1764 width = max_c(width, fNoVideoWidth); 1765 height = height + videoHeight - 1; 1766 1767 if (stayOnScreen) { 1768 BRect screenFrame(BScreen(this).Frame()); 1769 BRect frame(Frame()); 1770 BRect decoratorFrame(DecoratorFrame()); 1771 1772 // Shrink the screen frame by the window border size 1773 screenFrame.top += frame.top - decoratorFrame.top; 1774 screenFrame.left += frame.left - decoratorFrame.left; 1775 screenFrame.right += frame.right - decoratorFrame.right; 1776 screenFrame.bottom += frame.bottom - decoratorFrame.bottom; 1777 1778 // Update frame to what the new size would be 1779 frame.right = frame.left + width; 1780 frame.bottom = frame.top + height; 1781 1782 if (!screenFrame.Contains(frame)) { 1783 // Resize the window so it doesn't extend outside the current 1784 // screen frame. 1785 if (frame.Width() > screenFrame.Width() 1786 || frame.Height() > screenFrame.Height()) { 1787 // too large 1788 int widthDiff 1789 = frame.IntegerWidth() - screenFrame.IntegerWidth(); 1790 int heightDiff 1791 = frame.IntegerHeight() - screenFrame.IntegerHeight(); 1792 1793 float shrinkScale; 1794 if (widthDiff > heightDiff) 1795 shrinkScale = (float)(width - widthDiff) / width; 1796 else 1797 shrinkScale = (float)(height - heightDiff) / height; 1798 1799 // Resize width/height and center window 1800 width = lround(width * shrinkScale); 1801 height = lround(height * shrinkScale); 1802 MoveTo((screenFrame.left + screenFrame.right - width) / 2, 1803 (screenFrame.top + screenFrame.bottom - height) / 2); 1804 } else { 1805 // just off-screen on one or more sides 1806 int offsetX = 0; 1807 int offsetY = 0; 1808 if (frame.left < screenFrame.left) 1809 offsetX = (int)(screenFrame.left - frame.left); 1810 else if (frame.right > screenFrame.right) 1811 offsetX = (int)(screenFrame.right - frame.right); 1812 if (frame.top < screenFrame.top) 1813 offsetY = (int)(screenFrame.top - frame.top); 1814 else if (frame.bottom > screenFrame.bottom) 1815 offsetY = (int)(screenFrame.bottom - frame.bottom); 1816 MoveBy(offsetX, offsetY); 1817 } 1818 } 1819 } 1820 1821 ResizeTo(width, height); 1822 } 1823 1824 1825 void 1826 MainWin::_ResizeVideoView(int x, int y, int width, int height) 1827 { 1828 // Keep aspect ratio, place video view inside 1829 // the background area (may create black bars). 1830 int videoWidth; 1831 int videoHeight; 1832 _GetUnscaledVideoSize(videoWidth, videoHeight); 1833 float scaledWidth = videoWidth; 1834 float scaledHeight = videoHeight; 1835 float factor = min_c(width / scaledWidth, height / scaledHeight); 1836 int renderWidth = lround(scaledWidth * factor); 1837 int renderHeight = lround(scaledHeight * factor); 1838 if (renderWidth > width) 1839 renderWidth = width; 1840 if (renderHeight > height) 1841 renderHeight = height; 1842 1843 int xOffset = (width - renderWidth) / 2; 1844 int yOffset = (height - renderHeight) / 2; 1845 1846 fVideoView->MoveTo(x, y); 1847 fVideoView->ResizeTo(width - 1, height - 1); 1848 1849 BRect videoFrame(xOffset, yOffset, 1850 xOffset + renderWidth - 1, yOffset + renderHeight - 1); 1851 1852 fVideoView->SetVideoFrame(videoFrame); 1853 } 1854 1855 1856 // #pragma mark - 1857 1858 1859 void 1860 MainWin::_MouseDown(BMessage* msg, BView* originalHandler) 1861 { 1862 uint32 buttons = msg->FindInt32("buttons"); 1863 1864 // On Zeta, only "screen_where" is reliable, "where" and "be:view_where" 1865 // seem to be broken 1866 BPoint screenWhere; 1867 if (msg->FindPoint("screen_where", &screenWhere) != B_OK) { 1868 // TODO: remove 1869 // Workaround for BeOS R5, it has no "screen_where" 1870 if (!originalHandler || msg->FindPoint("where", &screenWhere) < B_OK) 1871 return; 1872 originalHandler->ConvertToScreen(&screenWhere); 1873 } 1874 1875 // double click handling 1876 1877 if (msg->FindInt32("clicks") % 2 == 0) { 1878 BRect rect(screenWhere.x - 1, screenWhere.y - 1, screenWhere.x + 1, 1879 screenWhere.y + 1); 1880 if (rect.Contains(fMouseDownMousePos)) { 1881 if (buttons == B_PRIMARY_MOUSE_BUTTON) 1882 PostMessage(M_TOGGLE_FULLSCREEN); 1883 else if (buttons == B_SECONDARY_MOUSE_BUTTON) 1884 PostMessage(M_TOGGLE_NO_INTERFACE); 1885 1886 return; 1887 } 1888 } 1889 1890 fMouseDownMousePos = screenWhere; 1891 fMouseDownWindowPos = Frame().LeftTop(); 1892 1893 if (buttons == B_PRIMARY_MOUSE_BUTTON && !fIsFullscreen) { 1894 // start mouse tracking 1895 fVideoView->SetMouseEventMask(B_POINTER_EVENTS | B_NO_POINTER_HISTORY 1896 /* | B_LOCK_WINDOW_FOCUS */); 1897 fMouseDownTracking = true; 1898 } 1899 1900 // pop up a context menu if right mouse button is down for 200 ms 1901 1902 if ((buttons & B_SECONDARY_MOUSE_BUTTON) == 0) 1903 return; 1904 1905 bigtime_t start = system_time(); 1906 bigtime_t delay = 200000; 1907 BPoint location; 1908 do { 1909 fVideoView->GetMouse(&location, &buttons); 1910 if ((buttons & B_SECONDARY_MOUSE_BUTTON) == 0) 1911 break; 1912 snooze(1000); 1913 } while (system_time() - start < delay); 1914 1915 if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) 1916 _ShowContextMenu(screenWhere); 1917 } 1918 1919 1920 void 1921 MainWin::_MouseMoved(BMessage* msg, BView* originalHandler) 1922 { 1923 // msg->PrintToStream(); 1924 1925 BPoint mousePos; 1926 uint32 buttons = msg->FindInt32("buttons"); 1927 // On Zeta, only "screen_where" is reliable, "where" 1928 // and "be:view_where" seem to be broken 1929 if (msg->FindPoint("screen_where", &mousePos) != B_OK) { 1930 // TODO: remove 1931 // Workaround for BeOS R5, it has no "screen_where" 1932 if (!originalHandler || msg->FindPoint("where", &mousePos) < B_OK) 1933 return; 1934 originalHandler->ConvertToScreen(&mousePos); 1935 } 1936 1937 if (buttons == B_PRIMARY_MOUSE_BUTTON && fMouseDownTracking 1938 && !fIsFullscreen) { 1939 // printf("screen where: %.0f, %.0f => ", mousePos.x, mousePos.y); 1940 float delta_x = mousePos.x - fMouseDownMousePos.x; 1941 float delta_y = mousePos.y - fMouseDownMousePos.y; 1942 float x = fMouseDownWindowPos.x + delta_x; 1943 float y = fMouseDownWindowPos.y + delta_y; 1944 // printf("move window to %.0f, %.0f\n", x, y); 1945 MoveTo(x, y); 1946 } 1947 1948 bigtime_t eventTime; 1949 if (msg->FindInt64("when", &eventTime) != B_OK) 1950 eventTime = system_time(); 1951 1952 if (buttons == 0 && fIsFullscreen) { 1953 BPoint moveDelta = mousePos - fLastMousePos; 1954 float moveDeltaDist 1955 = sqrtf(moveDelta.x * moveDelta.x + moveDelta.y * moveDelta.y); 1956 if (eventTime - fLastMouseMovedTime < 200000) 1957 fMouseMoveDist += moveDeltaDist; 1958 else 1959 fMouseMoveDist = moveDeltaDist; 1960 if (fMouseMoveDist > 5) 1961 _ShowFullscreenControls(true); 1962 } 1963 1964 fLastMousePos = mousePos; 1965 fLastMouseMovedTime =eventTime; 1966 } 1967 1968 1969 void 1970 MainWin::_MouseUp(BMessage* msg) 1971 { 1972 fMouseDownTracking = false; 1973 } 1974 1975 1976 void 1977 MainWin::_ShowContextMenu(const BPoint& screenPoint) 1978 { 1979 printf("Show context menu\n"); 1980 BPopUpMenu* menu = new BPopUpMenu("context menu", false, false); 1981 BMenuItem* item; 1982 menu->AddItem(item = new BMenuItem("Full screen", 1983 new BMessage(M_TOGGLE_FULLSCREEN), 'F')); 1984 item->SetMarked(fIsFullscreen); 1985 item->SetEnabled(fHasVideo); 1986 1987 BMenu* aspectSubMenu = new BMenu("Aspect ratio"); 1988 _SetupVideoAspectItems(aspectSubMenu); 1989 aspectSubMenu->SetTargetForItems(this); 1990 menu->AddItem(item = new BMenuItem(aspectSubMenu)); 1991 item->SetEnabled(fHasVideo); 1992 1993 menu->AddItem(item = new BMenuItem("No interface", 1994 new BMessage(M_TOGGLE_NO_INTERFACE), 'B')); 1995 item->SetMarked(fNoInterface); 1996 item->SetEnabled(fHasVideo); 1997 1998 menu->AddSeparatorItem(); 1999 2000 // Add track selector menus 2001 BMenu* audioTrackMenu = new BMenu("Audio track"); 2002 BMenu* videoTrackMenu = new BMenu("Video track"); 2003 BMenu* subTitleTrackMenu = new BMenu("Subtitles"); 2004 _SetupTrackMenus(audioTrackMenu, videoTrackMenu, subTitleTrackMenu); 2005 2006 audioTrackMenu->SetTargetForItems(this); 2007 videoTrackMenu->SetTargetForItems(this); 2008 subTitleTrackMenu->SetTargetForItems(this); 2009 2010 menu->AddItem(item = new BMenuItem(audioTrackMenu)); 2011 item->SetEnabled(fHasAudio); 2012 2013 menu->AddItem(item = new BMenuItem(videoTrackMenu)); 2014 item->SetEnabled(fHasVideo); 2015 2016 menu->AddItem(item = new BMenuItem(subTitleTrackMenu)); 2017 item->SetEnabled(fHasVideo); 2018 2019 menu->AddSeparatorItem(); 2020 menu->AddItem(new BMenuItem("Quit", new BMessage(M_FILE_QUIT), 'Q')); 2021 2022 menu->SetTargetForItems(this); 2023 BRect rect(screenPoint.x - 5, screenPoint.y - 5, screenPoint.x + 5, 2024 screenPoint.y + 5); 2025 menu->Go(screenPoint, true, true, rect, true); 2026 } 2027 2028 2029 /*! Trap keys that are about to be send to background or renderer view. 2030 Return true if it shouldn't be passed to the view. 2031 */ 2032 bool 2033 MainWin::_KeyDown(BMessage* msg) 2034 { 2035 uint32 key = msg->FindInt32("key"); 2036 uint32 rawChar = msg->FindInt32("raw_char"); 2037 uint32 modifier = msg->FindInt32("modifiers"); 2038 2039 // printf("key 0x%lx, rawChar 0x%lx, modifiers 0x%lx\n", key, rawChar, 2040 // modifier); 2041 2042 // ignore the system modifier namespace 2043 if ((modifier & (B_CONTROL_KEY | B_COMMAND_KEY)) 2044 == (B_CONTROL_KEY | B_COMMAND_KEY)) 2045 return false; 2046 2047 switch (rawChar) { 2048 case B_SPACE: 2049 fController->TogglePlaying(); 2050 return true; 2051 2052 case 'm': 2053 fController->ToggleMute(); 2054 return true; 2055 2056 case B_ESCAPE: 2057 if (!fIsFullscreen) 2058 break; 2059 2060 PostMessage(M_TOGGLE_FULLSCREEN); 2061 return true; 2062 2063 case B_ENTER: // Enter / Return 2064 if ((modifier & B_COMMAND_KEY) != 0) { 2065 PostMessage(M_TOGGLE_FULLSCREEN); 2066 return true; 2067 } 2068 break; 2069 2070 case B_TAB: 2071 if ((modifier & (B_COMMAND_KEY | B_CONTROL_KEY | B_OPTION_KEY 2072 | B_MENU_KEY)) == 0) { 2073 PostMessage(M_TOGGLE_FULLSCREEN); 2074 return true; 2075 } 2076 break; 2077 2078 case B_UP_ARROW: 2079 if ((modifier & B_COMMAND_KEY) != 0) 2080 PostMessage(M_SKIP_NEXT); 2081 else 2082 PostMessage(M_VOLUME_UP); 2083 return true; 2084 2085 case B_DOWN_ARROW: 2086 if ((modifier & B_COMMAND_KEY) != 0) 2087 PostMessage(M_SKIP_PREV); 2088 else 2089 PostMessage(M_VOLUME_DOWN); 2090 return true; 2091 2092 case B_RIGHT_ARROW: 2093 if ((modifier & B_COMMAND_KEY) != 0) 2094 PostMessage(M_SKIP_NEXT); 2095 else if (fAllowWinding) { 2096 BMessage windMessage(M_WIND); 2097 if ((modifier & B_SHIFT_KEY) != 0) { 2098 windMessage.AddInt64("how much", 30000000LL); 2099 windMessage.AddInt64("frames", 5); 2100 } else { 2101 windMessage.AddInt64("how much", 5000000LL); 2102 windMessage.AddInt64("frames", 1); 2103 } 2104 PostMessage(&windMessage); 2105 } 2106 return true; 2107 2108 case B_LEFT_ARROW: 2109 if ((modifier & B_COMMAND_KEY) != 0) 2110 PostMessage(M_SKIP_PREV); 2111 else if (fAllowWinding) { 2112 BMessage windMessage(M_WIND); 2113 if ((modifier & B_SHIFT_KEY) != 0) { 2114 windMessage.AddInt64("how much", -30000000LL); 2115 windMessage.AddInt64("frames", -5); 2116 } else { 2117 windMessage.AddInt64("how much", -5000000LL); 2118 windMessage.AddInt64("frames", -1); 2119 } 2120 PostMessage(&windMessage); 2121 } 2122 return true; 2123 2124 case B_PAGE_UP: 2125 PostMessage(M_SKIP_NEXT); 2126 return true; 2127 2128 case B_PAGE_DOWN: 2129 PostMessage(M_SKIP_PREV); 2130 return true; 2131 2132 case '+': 2133 if ((modifier & B_COMMAND_KEY) == 0) { 2134 _ZoomVideoView(10); 2135 return true; 2136 } 2137 break; 2138 2139 case '-': 2140 if ((modifier & B_COMMAND_KEY) == 0) { 2141 _ZoomVideoView(-10); 2142 return true; 2143 } 2144 break; 2145 2146 case B_DELETE: 2147 case 'd': // d for delete 2148 case 't': // t for Trash 2149 if ((modifiers() & B_COMMAND_KEY) != 0) { 2150 BAutolock _(fPlaylist); 2151 BMessage removeMessage(M_PLAYLIST_REMOVE_AND_PUT_INTO_TRASH); 2152 removeMessage.AddInt32("playlist index", 2153 fPlaylist->CurrentItemIndex()); 2154 fPlaylistWindow->PostMessage(&removeMessage); 2155 return true; 2156 } 2157 break; 2158 } 2159 2160 switch (key) { 2161 case 0x3a: // numeric keypad + 2162 if ((modifier & B_COMMAND_KEY) == 0) { 2163 _ZoomVideoView(10); 2164 return true; 2165 } 2166 break; 2167 2168 case 0x25: // numeric keypad - 2169 if ((modifier & B_COMMAND_KEY) == 0) { 2170 _ZoomVideoView(-10); 2171 return true; 2172 } 2173 break; 2174 2175 case 0x38: // numeric keypad up arrow 2176 PostMessage(M_VOLUME_UP); 2177 return true; 2178 2179 case 0x59: // numeric keypad down arrow 2180 PostMessage(M_VOLUME_DOWN); 2181 return true; 2182 2183 case 0x39: // numeric keypad page up 2184 case 0x4a: // numeric keypad right arrow 2185 PostMessage(M_SKIP_NEXT); 2186 return true; 2187 2188 case 0x5a: // numeric keypad page down 2189 case 0x48: // numeric keypad left arrow 2190 PostMessage(M_SKIP_PREV); 2191 return true; 2192 2193 // Playback controls along the bottom of the keyboard: 2194 // Z X C V B for US International 2195 case 0x4c: 2196 PostMessage(M_SKIP_PREV); 2197 return true; 2198 case 0x4d: 2199 fController->Play(); 2200 return true; 2201 case 0x4e: 2202 fController->Pause(); 2203 return true; 2204 case 0x4f: 2205 fController->Stop(); 2206 return true; 2207 case 0x50: 2208 PostMessage(M_SKIP_NEXT); 2209 return true; 2210 } 2211 2212 return false; 2213 } 2214 2215 2216 // #pragma mark - 2217 2218 2219 void 2220 MainWin::_ToggleFullscreen() 2221 { 2222 printf("_ToggleFullscreen enter\n"); 2223 2224 if (!fHasVideo) { 2225 printf("_ToggleFullscreen - ignoring, as we don't have a video\n"); 2226 return; 2227 } 2228 2229 fIsFullscreen = !fIsFullscreen; 2230 2231 if (fIsFullscreen) { 2232 // switch to fullscreen 2233 2234 fSavedFrame = Frame(); 2235 printf("saving current frame: %d %d %d %d\n", int(fSavedFrame.left), 2236 int(fSavedFrame.top), int(fSavedFrame.right), 2237 int(fSavedFrame.bottom)); 2238 BScreen screen(this); 2239 BRect rect(screen.Frame()); 2240 2241 Hide(); 2242 MoveTo(rect.left, rect.top); 2243 ResizeTo(rect.Width(), rect.Height()); 2244 Show(); 2245 2246 } else { 2247 // switch back from full screen mode 2248 _ShowFullscreenControls(false, false); 2249 2250 Hide(); 2251 MoveTo(fSavedFrame.left, fSavedFrame.top); 2252 ResizeTo(fSavedFrame.Width(), fSavedFrame.Height()); 2253 Show(); 2254 } 2255 2256 fVideoView->SetFullscreen(fIsFullscreen); 2257 2258 _MarkItem(fFileMenu, M_TOGGLE_FULLSCREEN, fIsFullscreen); 2259 2260 printf("_ToggleFullscreen leave\n"); 2261 } 2262 2263 void 2264 MainWin::_ToggleAlwaysOnTop() 2265 { 2266 fAlwaysOnTop = !fAlwaysOnTop; 2267 SetFeel(fAlwaysOnTop ? B_FLOATING_ALL_WINDOW_FEEL : B_NORMAL_WINDOW_FEEL); 2268 2269 _MarkItem(fFileMenu, M_TOGGLE_ALWAYS_ON_TOP, fAlwaysOnTop); 2270 } 2271 2272 2273 void 2274 MainWin::_ToggleNoInterface() 2275 { 2276 printf("_ToggleNoInterface enter\n"); 2277 2278 if (fIsFullscreen || !fHasVideo) { 2279 // Fullscreen playback is always without interface and 2280 // audio playback is always with interface. So we ignore these 2281 // two states here. 2282 printf("_ToggleNoControls leave, doing nothing, we are fullscreen\n"); 2283 return; 2284 } 2285 2286 fNoInterface = !fNoInterface; 2287 _SetWindowSizeLimits(); 2288 2289 if (fNoInterface) { 2290 MoveBy(0, fMenuBarHeight); 2291 ResizeBy(0, -(fControlsHeight + fMenuBarHeight)); 2292 SetLook(B_BORDERED_WINDOW_LOOK); 2293 } else { 2294 MoveBy(0, -fMenuBarHeight); 2295 ResizeBy(0, fControlsHeight + fMenuBarHeight); 2296 SetLook(B_TITLED_WINDOW_LOOK); 2297 } 2298 2299 _MarkItem(fFileMenu, M_TOGGLE_NO_INTERFACE, fNoInterface); 2300 2301 printf("_ToggleNoInterface leave\n"); 2302 } 2303 2304 2305 void 2306 MainWin::_ShowIfNeeded() 2307 { 2308 if (find_thread(NULL) != Thread()) 2309 return; 2310 2311 if (!fHasVideo && fNoVideoFrame.IsValid()) { 2312 MoveTo(fNoVideoFrame.LeftTop()); 2313 ResizeTo(fNoVideoFrame.Width(), fNoVideoFrame.Height()); 2314 } 2315 fNoVideoFrame = BRect(); 2316 2317 if (IsHidden()) { 2318 Show(); 2319 UpdateIfNeeded(); 2320 } 2321 } 2322 2323 2324 void 2325 MainWin::_ShowFullscreenControls(bool show, bool animate) 2326 { 2327 if (fShowsFullscreenControls == show) 2328 return; 2329 2330 fShowsFullscreenControls = show; 2331 2332 if (show) { 2333 fControls->RemoveSelf(); 2334 fControls->MoveTo(fVideoView->Bounds().left, 2335 fVideoView->Bounds().bottom + 1); 2336 fVideoView->AddChild(fControls); 2337 if (fScaleFullscreenControls) 2338 fControls->SetSymbolScale(1.5f); 2339 while (fControls->IsHidden()) 2340 fControls->Show(); 2341 } 2342 2343 if (animate) { 2344 // Slide the controls into view. We need to do this with 2345 // messages, otherwise we block the video playback for the 2346 // time of the animation. 2347 const float kAnimationOffsets[] = { 0.05, 0.2, 0.5, 0.2, 0.05 }; 2348 const int32 steps = sizeof(kAnimationOffsets) / sizeof(float); 2349 float height = fControls->Bounds().Height(); 2350 float moveDist = show ? -height : height; 2351 float originalY = fControls->Frame().top; 2352 for (int32 i = 0; i < steps; i++) { 2353 BMessage message(M_SLIDE_CONTROLS); 2354 message.AddFloat("offset", 2355 floorf(moveDist * kAnimationOffsets[i])); 2356 PostMessage(&message, this); 2357 } 2358 BMessage finalMessage(M_FINISH_SLIDING_CONTROLS); 2359 finalMessage.AddFloat("offset", originalY + moveDist); 2360 finalMessage.AddBool("show", show); 2361 PostMessage(&finalMessage, this); 2362 } else { 2363 if (!show) { 2364 fControls->RemoveSelf(); 2365 fControls->MoveTo(fVideoView->Frame().left, 2366 fVideoView->Frame().bottom + 1); 2367 fBackground->AddChild(fControls); 2368 fControls->SetSymbolScale(1.0f); 2369 while (!fControls->IsHidden()) 2370 fControls->Hide(); 2371 } 2372 } 2373 } 2374 2375 2376 // #pragma mark - 2377 2378 2379 void 2380 MainWin::_UpdatePlaylistItemFile() 2381 { 2382 BAutolock locker(fPlaylist); 2383 const FilePlaylistItem* item 2384 = dynamic_cast<const FilePlaylistItem*>(fController->Item()); 2385 if (item == NULL) 2386 return; 2387 2388 if (!fHasVideo && !fHasAudio) 2389 return; 2390 2391 BNode node(&item->Ref()); 2392 if (node.InitCheck()) 2393 return; 2394 2395 // Add to recent documents 2396 be_roster->AddToRecentDocuments(&item->Ref(), kAppSig); 2397 2398 locker.Unlock(); 2399 2400 // Set some standard attributes of the currently played file. 2401 // This should only be a temporary solution. 2402 2403 // Write duration 2404 const char* kDurationAttrName = "Media:Length"; 2405 attr_info info; 2406 status_t status = node.GetAttrInfo(kDurationAttrName, &info); 2407 if (status != B_OK || info.size == 0) { 2408 time_t duration = fController->TimeDuration() / 1000000L; 2409 2410 char text[256]; 2411 duration_to_string(duration, text, sizeof(text)); 2412 node.WriteAttr(kDurationAttrName, B_STRING_TYPE, 0, text, 2413 strlen(text) + 1); 2414 } 2415 2416 // Write audio bitrate 2417 if (fHasAudio) { 2418 status = node.GetAttrInfo("Audio:Bitrate", &info); 2419 if (status != B_OK || info.size == 0) { 2420 media_format format; 2421 if (fController->GetEncodedAudioFormat(&format) == B_OK 2422 && format.type == B_MEDIA_ENCODED_AUDIO) { 2423 int32 bitrate = (int32)(format.u.encoded_audio.bit_rate 2424 / 1000); 2425 char text[256]; 2426 snprintf(text, sizeof(text), "%ld kbit", bitrate); 2427 node.WriteAttr("Audio:Bitrate", B_STRING_TYPE, 0, text, 2428 strlen(text) + 1); 2429 } 2430 } 2431 } 2432 2433 // Write video bitrate 2434 if (fHasVideo) { 2435 status = node.GetAttrInfo("Video:Bitrate", &info); 2436 if (status != B_OK || info.size == 0) { 2437 media_format format; 2438 if (fController->GetEncodedVideoFormat(&format) == B_OK 2439 && format.type == B_MEDIA_ENCODED_VIDEO) { 2440 int32 bitrate = (int32)(format.u.encoded_video.avg_bit_rate 2441 / 1000); 2442 char text[256]; 2443 snprintf(text, sizeof(text), "%ld kbit", bitrate); 2444 node.WriteAttr("Video:Bitrate", B_STRING_TYPE, 0, text, 2445 strlen(text) + 1); 2446 } 2447 } 2448 } 2449 2450 _UpdateAttributesMenu(node); 2451 } 2452 2453 2454 void 2455 MainWin::_UpdateAttributesMenu(const BNode& node) 2456 { 2457 int32 rating = -1; 2458 2459 attr_info info; 2460 status_t status = node.GetAttrInfo(kRatingAttrName, &info); 2461 if (status == B_OK && info.type == B_INT32_TYPE) { 2462 // Node has the Rating attribute. 2463 node.ReadAttr(kRatingAttrName, B_INT32_TYPE, 0, &rating, 2464 sizeof(rating)); 2465 } 2466 2467 for (int32 i = 0; BMenuItem* item = fRatingMenu->ItemAt(i); i++) 2468 item->SetMarked(i + 1 == rating); 2469 } 2470 2471 2472 void 2473 MainWin::_SetRating(int32 rating) 2474 { 2475 BAutolock locker(fPlaylist); 2476 const FilePlaylistItem* item 2477 = dynamic_cast<const FilePlaylistItem*>(fController->Item()); 2478 if (item == NULL) 2479 return; 2480 2481 BNode node(&item->Ref()); 2482 if (node.InitCheck()) 2483 return; 2484 2485 locker.Unlock(); 2486 2487 node.WriteAttr(kRatingAttrName, B_INT32_TYPE, 0, &rating, sizeof(rating)); 2488 2489 // TODO: The whole mechnism should work like this: 2490 // * There is already an attribute API for PlaylistItem, flesh it out! 2491 // * FilePlaylistItem node-monitors it's file somehow. 2492 // * FilePlaylistItem keeps attributes in sync and sends notications. 2493 // * MainWin updates the menu according to FilePlaylistItem notifications. 2494 // * PlaylistWin shows columns with attribute and other info. 2495 // * PlaylistWin updates also upon FilePlaylistItem notifications. 2496 // * This keeps attributes in sync when another app changes them. 2497 2498 _UpdateAttributesMenu(node); 2499 } 2500 2501 2502 void 2503 MainWin::_UpdateControlsEnabledStatus() 2504 { 2505 uint32 enabledButtons = 0; 2506 if (fHasVideo || fHasAudio) { 2507 enabledButtons |= PLAYBACK_ENABLED | SEEK_ENABLED 2508 | SEEK_BACK_ENABLED | SEEK_FORWARD_ENABLED; 2509 } 2510 if (fHasAudio) 2511 enabledButtons |= VOLUME_ENABLED; 2512 2513 BAutolock _(fPlaylist); 2514 bool canSkipPrevious, canSkipNext; 2515 fPlaylist->GetSkipInfo(&canSkipPrevious, &canSkipNext); 2516 if (canSkipPrevious) 2517 enabledButtons |= SKIP_BACK_ENABLED; 2518 if (canSkipNext) 2519 enabledButtons |= SKIP_FORWARD_ENABLED; 2520 2521 fControls->SetEnabled(enabledButtons); 2522 2523 fNoInterfaceMenuItem->SetEnabled(fHasVideo); 2524 fAttributesMenu->SetEnabled(fHasAudio || fHasVideo); 2525 } 2526 2527 2528 void 2529 MainWin::_UpdatePlaylistMenu() 2530 { 2531 if (!fPlaylist->Lock()) 2532 return; 2533 2534 fPlaylistMenu->RemoveItems(0, fPlaylistMenu->CountItems(), true); 2535 2536 int32 count = fPlaylist->CountItems(); 2537 for (int32 i = 0; i < count; i++) { 2538 PlaylistItem* item = fPlaylist->ItemAtFast(i); 2539 _AddPlaylistItem(item, i); 2540 } 2541 fPlaylistMenu->SetTargetForItems(this); 2542 2543 _MarkPlaylistItem(fPlaylist->CurrentItemIndex()); 2544 2545 fPlaylist->Unlock(); 2546 } 2547 2548 2549 void 2550 MainWin::_AddPlaylistItem(PlaylistItem* item, int32 index) 2551 { 2552 BMessage* message = new BMessage(M_SET_PLAYLIST_POSITION); 2553 message->AddInt32("index", index); 2554 BMenuItem* menuItem = new BMenuItem(item->Name().String(), message); 2555 fPlaylistMenu->AddItem(menuItem, index); 2556 } 2557 2558 2559 void 2560 MainWin::_RemovePlaylistItem(int32 index) 2561 { 2562 delete fPlaylistMenu->RemoveItem(index); 2563 } 2564 2565 2566 void 2567 MainWin::_MarkPlaylistItem(int32 index) 2568 { 2569 if (BMenuItem* item = fPlaylistMenu->ItemAt(index)) { 2570 item->SetMarked(true); 2571 // ... and in case the menu is currently on screen: 2572 if (fPlaylistMenu->LockLooper()) { 2573 fPlaylistMenu->Invalidate(); 2574 fPlaylistMenu->UnlockLooper(); 2575 } 2576 } 2577 } 2578 2579 2580 void 2581 MainWin::_MarkItem(BMenu* menu, uint32 command, bool mark) 2582 { 2583 if (BMenuItem* item = menu->FindItem(command)) 2584 item->SetMarked(mark); 2585 } 2586 2587 2588 void 2589 MainWin::_AdoptGlobalSettings() 2590 { 2591 mpSettings settings = Settings::CurrentSettings(); 2592 // thread safe 2593 2594 fCloseWhenDonePlayingMovie = settings.closeWhenDonePlayingMovie; 2595 fCloseWhenDonePlayingSound = settings.closeWhenDonePlayingSound; 2596 fLoopMovies = settings.loopMovie; 2597 fLoopSounds = settings.loopSound; 2598 fScaleFullscreenControls = settings.scaleFullscreenControls; 2599 } 2600 2601 2602