bsnes/hiro/qt/window.cpp
Tim Allen ed5ec58595 Update to v103r13 release.
byuu says:

Changelog:

  - gb/interface: fix Game Boy Color extension to be "gbc" and not "gb"
    [hex\_usr]
  - ms/interface: move Master System hardware controls below controller
    ports
  - sfc/ppu: improve latching behavior of BGnHOFS registers (not
    hardware verified) [AWJ]
  - tomoko/input: rework port/device mapping to support non-sequential
    ports and devices¹
      - todo: should add move() to inputDevice.mappings.append and
        inputPort.devices.append
      - note: there's a weird GCC 4.9 bug with brace initialization of
        InputEmulator; have to assign each field separately
  - tomoko: all windows sans the main presentation window can be
    dismissed with the escape key
  - icarus: the single file selection dialog ("Load ROM Image...") can
    be dismissed with the escape key
  - tomoko: do not pause emulation when FocusLoss/Pause is set during
    exclusive fullscreen mode
  - hiro/(windows,gtk,qt): implemented Window::setDismissable() function
    (missing from cocoa port, sorry)
  - nall/string: fixed printing of largest possible negative numbers (eg
    `INT_MIN`) [Sintendo]
      - only took eight months! :D

¹: When I tried to move the Master System hardware port below the
controller ports, I ran into a world of pain.

The input settings list expects every item in the
`InputEmulator<InputPort<InputDevice<InputMapping>>>>` arrays to be
populated with valid results. But these would be sparsely populated
based on the port and device IDs from inside higan. And that is done so
that the Interface::inputPoll can have O(1) lookup of ports and devices.
This worked because all the port and device IDs were sequential (they
left no gaps in the maps upon creating the lists.)

Unfortunately by changing the expectation of port ID to how it appears
in the list, inputs would not poll correctly. By leaving them alone and
just moving Hardware to the third position, the Game Gear would be
missing port IDs of 0 and 1 (the controller ports of the Master System).
Even by trying to make separate MasterSystemHardware and
GameGearHardware ports, things still fractured when the devices were no
longer contigious.

I got pretty sick of this and just decided to give up on O(1)
port/device lookup, and moved to O(n) lookup. It only knocked the
framerate down by maybe one frame per second, enough to be in the margin
of error. Inputs aren't polled *that* often for loops that usually
terminate after 1-2 cycles to be too detrimental to performance.

So the new input system now allows non-sequential port and device IDs.

Remember that I killed input IDs a while back. There's never any reason
for those to need IDs ... it was easier to just order the inputs in the
order you want to see them in the user interface. So the input lookup is
still O(1). Only now, everything's safer and I return a
maybe<InputMapping&>, and won't crash out the program trying to use a
mapping that isn't found for some reason.

Errata: the escape key isn't working on the browser/message dialogs on
Windows, because of course nothing can ever just be easy and work for
me. If anyone else wouldn't mind looking into that, I'd greatly
appreciate it.

Having the `WM_KEYDOWN` test inside the main `Application_sharedProc`, it
seems to not respond to the escape key on modal dialogs. If I put the
`WM_KEYDOWN` test in the main window proc, then it doesn't seem to get
called for `VK_ESCAPE` at all, and doesn't get called period for modal
windows. So I'm at a loss and it's past 4AM here >_>
2017-07-12 18:24:27 +10:00

304 lines
8.8 KiB
C++

#if defined(Hiro_Window)
namespace hiro {
auto pWindow::construct() -> void {
qtWindow = new QtWindow(*this);
qtWindow->setWindowTitle(" ");
//if program was given a name, try and set the window taskbar icon to a matching pixmap image
if(auto name = Application::state.name) {
if(file::exists({Path::user(), ".local/share/icons/", name, ".png"})) {
qtWindow->setWindowIcon(QIcon(QString::fromUtf8(string{Path::user(), ".local/share/icons/", name, ".png"})));
} else if(file::exists({"/usr/local/share/pixmaps/", name, ".png"})) {
qtWindow->setWindowIcon(QIcon(QString::fromUtf8(string{"/usr/local/share/pixmaps/", name, ".png"})));
} else if(file::exists({"/usr/share/pixmaps/", name, ".png"})) {
qtWindow->setWindowIcon(QIcon(QString::fromUtf8(string{"/usr/share/pixmaps/", name, ".png"})));
}
}
qtLayout = new QVBoxLayout(qtWindow);
qtLayout->setMargin(0);
qtLayout->setSpacing(0);
qtWindow->setLayout(qtLayout);
qtMenuBar = new QMenuBar(qtWindow);
qtMenuBar->setVisible(false);
qtLayout->addWidget(qtMenuBar);
qtContainer = new QWidget(qtWindow);
qtContainer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
qtContainer->setVisible(true);
qtLayout->addWidget(qtContainer);
qtStatusBar = new QStatusBar(qtWindow);
qtStatusBar->setSizeGripEnabled(true);
qtStatusBar->setVisible(false);
qtLayout->addWidget(qtStatusBar);
setBackgroundColor(state().backgroundColor);
setDroppable(state().droppable);
setGeometry(state().geometry);
setResizable(state().resizable);
setTitle(state().title);
}
auto pWindow::destruct() -> void {
delete qtStatusBar;
delete qtContainer;
delete qtMenuBar;
delete qtLayout;
delete qtWindow;
}
auto pWindow::append(sLayout layout) -> void {
}
auto pWindow::append(sMenuBar menuBar) -> void {
}
auto pWindow::append(sStatusBar statusBar) -> void {
}
auto pWindow::focused() const -> bool {
return qtWindow->isActiveWindow() && !qtWindow->isMinimized();
}
auto pWindow::frameMargin() const -> Geometry {
if(state().fullScreen) return {
0, _menuHeight(),
0, _menuHeight() + _statusHeight()
};
return {
settings.geometry.frameX,
settings.geometry.frameY + _menuHeight(),
settings.geometry.frameWidth,
settings.geometry.frameHeight + _menuHeight() + _statusHeight()
};
}
auto pWindow::remove(sLayout layout) -> void {
}
auto pWindow::remove(sMenuBar menuBar) -> void {
//QMenuBar::removeMenu() does not exist
//qtMenu->clear();
//for(auto& menu : window.state.menu) append(menu);
}
auto pWindow::remove(sStatusBar statusBar) -> void {
}
auto pWindow::setBackgroundColor(Color color) -> void {
if(color) {
QPalette palette;
palette.setColor(QPalette::Background, QColor(color.red(), color.green(), color.blue() /*, color.alpha() */));
qtContainer->setPalette(palette);
qtContainer->setAutoFillBackground(true);
//translucency results are very unpleasant without a compositor; so disable for now
//qtWindow->setAttribute(Qt::WA_TranslucentBackground, color.alpha() != 255);
}
}
auto pWindow::setDismissable(bool dismissable) -> void {
}
auto pWindow::setDroppable(bool droppable) -> void {
qtWindow->setAcceptDrops(droppable);
}
auto pWindow::setEnabled(bool enabled) -> void {
}
auto pWindow::setFocused() -> void {
qtWindow->raise();
qtWindow->activateWindow();
}
auto pWindow::setFullScreen(bool fullScreen) -> void {
lock();
if(fullScreen) {
windowedGeometry = state().geometry;
qtLayout->setSizeConstraint(QLayout::SetDefaultConstraint);
qtContainer->setFixedSize(Desktop::size().width() - frameMargin().width(), Desktop::size().height() - frameMargin().height());
qtWindow->showFullScreen();
state().geometry = Monitor::geometry(Monitor::primary());
} else {
setResizable(state().resizable);
qtWindow->showNormal();
qtWindow->adjustSize();
self().setGeometry(windowedGeometry);
}
unlock();
}
auto pWindow::setGeometry(Geometry geometry) -> void {
lock();
Application::processEvents();
QApplication::syncX();
setResizable(state().resizable);
qtWindow->move(geometry.x() - frameMargin().x(), geometry.y() - frameMargin().y());
//qtWindow->adjustSize() fails if larger than 2/3rds screen size
qtWindow->resize(qtWindow->sizeHint());
if(state().resizable) {
//required to allow shrinking window from default size
qtWindow->setMinimumSize(1, 1);
qtContainer->setMinimumSize(1, 1);
}
unlock();
}
auto pWindow::setModal(bool modal) -> void {
if(modal) {
//windowModality can only be enabled while window is invisible
setVisible(false);
qtWindow->setWindowModality(Qt::ApplicationModal);
setVisible(true);
while(!Application::state.quit && state().modal) {
if(Application::state.onMain) {
Application::doMain();
} else {
usleep(20 * 1000);
}
Application::processEvents();
}
qtWindow->setWindowModality(Qt::NonModal);
}
}
auto pWindow::setResizable(bool resizable) -> void {
if(resizable) {
qtLayout->setSizeConstraint(QLayout::SetDefaultConstraint);
qtContainer->setMinimumSize(state().geometry.width(), state().geometry.height());
} else {
qtLayout->setSizeConstraint(QLayout::SetFixedSize);
qtContainer->setFixedSize(state().geometry.width(), state().geometry.height());
}
qtStatusBar->setSizeGripEnabled(resizable);
}
auto pWindow::setTitle(const string& text) -> void {
qtWindow->setWindowTitle(QString::fromUtf8(text));
}
auto pWindow::setVisible(bool visible) -> void {
lock();
qtWindow->setVisible(visible);
if(visible) {
_updateFrameGeometry();
setGeometry(state().geometry);
}
unlock();
}
auto pWindow::_append(mWidget& widget) -> void {
if(auto self = widget.self()) {
self->qtWidget->setParent(qtContainer);
}
}
auto pWindow::_menuHeight() const -> signed {
return qtMenuBar->isVisible() ? settings.geometry.menuHeight : 0;
}
auto pWindow::_statusHeight() const -> signed {
return qtStatusBar->isVisible() ? settings.geometry.statusHeight : 0;
}
auto pWindow::_updateFrameGeometry() -> void {
pApplication::syncX();
QRect border = qtWindow->frameGeometry();
QRect client = qtWindow->geometry();
settings.geometry.frameX = client.x() - border.x();
settings.geometry.frameY = client.y() - border.y();
settings.geometry.frameWidth = border.width() - client.width();
settings.geometry.frameHeight = border.height() - client.height();
if(qtMenuBar->isVisible()) {
pApplication::syncX();
settings.geometry.menuHeight = qtMenuBar->height();
}
if(qtStatusBar->isVisible()) {
pApplication::syncX();
settings.geometry.statusHeight = qtStatusBar->height();
}
}
auto QtWindow::closeEvent(QCloseEvent* event) -> void {
event->ignore();
if(p.state().onClose) p.self().doClose();
else p.self().setVisible(false);
if(p.state().modal && !p.self().visible()) p.self().setModal(false);
}
auto QtWindow::moveEvent(QMoveEvent* event) -> void {
if(!p.locked() && !p.state().fullScreen && p.qtWindow->isVisible()) {
p.state().geometry.setPosition({
p.state().geometry.x() + event->pos().x() - event->oldPos().x(),
p.state().geometry.y() + event->pos().y() - event->oldPos().y()
});
}
if(!p.locked()) {
p.self().doMove();
}
}
auto QtWindow::dragEnterEvent(QDragEnterEvent* event) -> void {
if(event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
auto QtWindow::dropEvent(QDropEvent* event) -> void {
if(auto paths = DropPaths(event)) p.self().doDrop(paths);
}
auto QtWindow::keyPressEvent(QKeyEvent* event) -> void {
//Keyboard::Keycode sym = Keysym(event->nativeVirtualKey());
//if(sym != Keyboard::Keycode::None && self.window.onKeyPress) self.window.onKeyPress(sym);
if(p.state().dismissable && event->key() == Qt::Key_Escape) {
if(p.state().onClose) p.self().doClose();
else p.self().setVisible(false);
if(p.state().modal && !p.self().visible()) p.self().setModal(false);
}
}
auto QtWindow::keyReleaseEvent(QKeyEvent* event) -> void {
//Keyboard::Keycode sym = Keysym(event->nativeVirtualKey());
//if(sym != Keyboard::Keycode::None && self.window.onKeyRelease) self.window.onKeyRelease(sym);
}
auto QtWindow::resizeEvent(QResizeEvent*) -> void {
if(!p.locked() && !p.state().fullScreen && p.qtWindow->isVisible()) {
p.state().geometry.setSize({
p.qtContainer->geometry().width(),
p.qtContainer->geometry().height()
});
}
if(auto& layout = p.state().layout) {
layout->setGeometry(p.self().geometry().setPosition(0, 0));
}
if(!p.locked()) {
p.self().doSize();
}
}
auto QtWindow::sizeHint() const -> QSize {
unsigned width = p.state().geometry.width();
unsigned height = p.state().geometry.height();
if(p.qtMenuBar->isVisible()) height += settings.geometry.menuHeight;
if(p.qtStatusBar->isVisible()) height += settings.geometry.statusHeight;
return QSize(width, height);
}
}
#endif