Skip to content

Commit

Permalink
ControllerInterface: Get window size from frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and LaserEyess committed Nov 6, 2022
1 parent 5d306cc commit 1ef113c
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 21 deletions.
3 changes: 2 additions & 1 deletion Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
// Switch the window used for inputs to the render window. This way, the cursor position
// is relative to the render window, instead of the main window.
ASSERT(g_controller_interface.IsInit());
g_controller_interface.ChangeWindow(wsi.render_window);
g_controller_interface.ChangeWindow(wsi.render_surface, wsi.render_surface_width,
wsi.render_surface_height);

Pad::LoadConfig();
Pad::LoadGBAConfig();
Expand Down
6 changes: 5 additions & 1 deletion Source/Core/DolphinNoGUI/PlatformWayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "Core/State.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

#include <climits>
#include <cstdio>
Expand Down Expand Up @@ -128,8 +129,11 @@ void PlatformWayland::TopLevelConfigure(void* data, struct xdg_toplevel* xdg_top
PlatformWayland* platform = static_cast<PlatformWayland*>(data);
platform->m_surface_width = width;
platform->m_surface_height = height;
if (g_renderer)
if (g_renderer) {
g_renderer->ResizeSurface(width, height);
if (g_controller_interface.IsInit())
g_controller_interface.OnWindowResized(width, height);
}
}

void PlatformWayland::TopLevelClose(void* data, struct xdg_toplevel* xdg_toplevel)
Expand Down
12 changes: 8 additions & 4 deletions Source/Core/DolphinNoGUI/PlatformX11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static constexpr auto X_None = None;
#include "Core/Config/MainSettings.h"
#include "Core/Core.h"
#include "Core/State.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

#include <climits>
#include <cstdio>
Expand Down Expand Up @@ -57,8 +58,8 @@ class PlatformX11 : public Platform
#endif
int m_window_x = Config::Get(Config::MAIN_RENDER_WINDOW_XPOS);
int m_window_y = Config::Get(Config::MAIN_RENDER_WINDOW_YPOS);
unsigned int m_window_width = Config::Get(Config::MAIN_RENDER_WINDOW_WIDTH);
unsigned int m_window_height = Config::Get(Config::MAIN_RENDER_WINDOW_HEIGHT);
int m_window_width = Config::Get(Config::MAIN_RENDER_WINDOW_WIDTH);
int m_window_height = Config::Get(Config::MAIN_RENDER_WINDOW_HEIGHT);
};

PlatformX11::~PlatformX11()
Expand Down Expand Up @@ -178,8 +179,9 @@ void PlatformX11::UpdateWindowPosition()

Window winDummy;
unsigned int borderDummy, depthDummy;
XGetGeometry(m_display, m_window, &winDummy, &m_window_x, &m_window_y, &m_window_width,
&m_window_height, &borderDummy, &depthDummy);
XGetGeometry(m_display, m_window, &winDummy, &m_window_x, &m_window_y,
reinterpret_cast<unsigned int*>(&m_window_width),
reinterpret_cast<unsigned int*>(&m_window_height), &borderDummy, &depthDummy);
}

void PlatformX11::ProcessEvents()
Expand Down Expand Up @@ -270,6 +272,8 @@ void PlatformX11::ProcessEvents()
UpdateWindowPosition();
g_renderer->ResizeSurface(m_window_width, m_window_height);
}
if (g_controller_interface.IsInit())
g_controller_interface.OnWindowResized(m_window_width, m_window_height);
}
break;
}
Expand Down
8 changes: 6 additions & 2 deletions Source/Core/DolphinQt/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ void Host::SetRenderHandle(void* handle, int width, int height)
if (g_renderer)
{
g_renderer->ChangeSurface(handle, width, height);
g_controller_interface.ChangeWindow(handle, width, height);
if (g_controller_interface.IsInit())
g_controller_interface.ChangeWindow(handle, width, height);
}
}

Expand Down Expand Up @@ -188,8 +189,11 @@ void Host::SetRenderFullscreen(bool fullscreen)

void Host::ResizeSurface(int new_width, int new_height)
{
if (g_renderer)
if (g_renderer) {
g_renderer->ResizeSurface(new_width, new_height);
if (g_controller_interface.IsInit())
g_controller_interface.OnWindowResized(new_width, new_height);
}
}

std::vector<std::string> Host_GetPreferredLocales()
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/DolphinQt/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1172,9 +1172,9 @@ void MainWindow::HideRenderWidget(bool reinit, bool is_exit)
// The controller interface will still be registered to the old render widget, if the core
// has booted. Therefore, we should re-bind it to the main window for now. When the core
// is next started, it will be swapped back to the new render widget.
g_controller_interface.ChangeWindow(GetWindowSystemInfo(this).render_surface,
is_exit ? ControllerInterface::WindowChangeReason::Exit :
ControllerInterface::WindowChangeReason::Other);
const WindowSystemInfo wsi = GetWindowSystemInfo(this);
g_controller_interface.ChangeWindow(wsi.render_surface, wsi.render_surface_width,
wsi.render_surface_height);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi)

std::lock_guard lk_population(m_devices_population_mutex);

// Prevent divide by zero if we somehow end up with a 0x0 window.
m_wsi = wsi;
m_wsi.render_surface_width = std::max(m_wsi.render_surface_width, 1);
m_wsi.render_surface_height = std::max(m_wsi.render_surface_height, 1);

m_populating_devices_counter = 1;

Expand Down Expand Up @@ -93,20 +96,22 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi)

// Devices writes are already protected by m_devices_population_mutex but this won't hurt
m_devices_mutex.lock();
const bool devices_empty = m_devices.empty();
const bool devices_empty = m_devices.empty();
m_devices_mutex.unlock();

if (m_populating_devices_counter.fetch_sub(1) == 1 && !devices_empty)
InvokeDevicesChangedCallbacks();
}

void ControllerInterface::ChangeWindow(void* hwnd, WindowChangeReason reason)
void ControllerInterface::ChangeWindow(void* hwnd, int width, int height, WindowChangeReason reason)
{
if (!m_is_init)
return;

// This shouldn't use render_surface so no need to update it.
m_wsi.render_window = hwnd;
m_wsi.render_surface_width = std::max(width, 1);
m_wsi.render_surface_height = std::max(height, 1);

// No need to re-add devices if this is an application exit request
if (reason == WindowChangeReason::Exit)
Expand All @@ -115,6 +120,15 @@ void ControllerInterface::ChangeWindow(void* hwnd, WindowChangeReason reason)
RefreshDevices(RefreshReason::WindowChangeOnly);
}

void ControllerInterface::OnWindowResized(int width, int height)
{
m_wsi.render_surface_width = std::max(width, 1);
m_wsi.render_surface_height = std::max(height, 1);
std::lock_guard lk(m_devices_mutex);
for (const auto& d : m_devices)
d->OnWindowResized(width, height);
}

void ControllerInterface::RefreshDevices(RefreshReason reason)
{
if (!m_is_init)
Expand Down Expand Up @@ -175,7 +189,7 @@ void ControllerInterface::RefreshDevices(RefreshReason reason)
#endif
#ifdef CIFACE_USE_XLIB
if (m_wsi.type == WindowSystemType::X11)
ciface::XInput2::PopulateDevices(m_wsi.render_window);
ciface::XInput2::PopulateDevices(m_wsi);
#endif
#ifdef CIFACE_USE_WAYLAND
if (m_wsi.type == WindowSystemType::Wayland)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class ControllerInterface : public ciface::Core::DeviceContainer
ControllerInterface() : m_is_init(false) {}
void Initialize(const WindowSystemInfo& wsi);
// Only call from one thread at a time.
void ChangeWindow(void* hwnd, WindowChangeReason reason = WindowChangeReason::Other);
void ChangeWindow(void* hwnd, int width, int height,
WindowChangeReason reason = WindowChangeReason::Other);
void OnWindowResized(int width, int height);
// Can be called by any thread at any time (when initialized).
void RefreshDevices(RefreshReason reason = RefreshReason::Other);
void Shutdown();
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/InputCommon/ControllerInterface/CoreDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class Device
// Every platform should have at least one device with priority >= 0.
virtual int GetSortPriority() const { return 0; }

// Called when the window the device is bound to is resized.
virtual void OnWindowResized(int width, int height) {}

const std::vector<Input*>& Inputs() const { return m_inputs; }
const std::vector<Output*>& Outputs() const { return m_outputs; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ void KeyboardMouse::UpdateInput()
wl_display_dispatch_queue_pending(m_display, m_event_queue);
}

void KeyboardMouse::OnWindowResized(int width, int height)
{
m_window_width = width;
m_window_height = height;
}

void KeyboardMouse::PointerEnter(void* data, wl_pointer* pointer, uint32_t serial,
wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class KeyboardMouse : public Core::Device
std::string GetName() const override;
std::string GetSource() const override;
void UpdateInput() override;
void OnWindowResized(int width, int height) override;

private:
void AddKeyInputs();
Expand Down
17 changes: 13 additions & 4 deletions Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
namespace ciface::XInput2
{
// This function will add zero or more KeyboardMouse objects to devices.
void PopulateDevices(void* const hwnd)
void PopulateDevices(const WindowSystemInfo& wsi)
{
Display* dpy = XOpenDisplay(nullptr);

Expand Down Expand Up @@ -86,7 +86,7 @@ void PopulateDevices(void* const hwnd)
// Since current_master is a master pointer, its attachment must
// be a master keyboard.
g_controller_interface.AddDevice(std::make_shared<KeyboardMouse>(
(Window)hwnd, xi_opcode, current_master->deviceid, current_master->attachment));
wsi, xi_opcode, current_master->deviceid, current_master->attachment));
}
}

Expand Down Expand Up @@ -128,15 +128,18 @@ void KeyboardMouse::SelectEventsForDevice(XIEventMask* mask, int deviceid)
XIFreeDeviceInfo(all_slaves);
}

KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboard)
: m_window(window), xi_opcode(opcode), pointer_deviceid(pointer), keyboard_deviceid(keyboard)
KeyboardMouse::KeyboardMouse(const WindowSystemInfo& wsi, int opcode, int pointer, int keyboard)
: m_window(reinterpret_cast<Window>(wsi.render_window)), xi_opcode(opcode),
pointer_deviceid(pointer), keyboard_deviceid(keyboard)
{
// The cool thing about each KeyboardMouse object having its own Display
// is that each one gets its own separate copy of the X11 event stream,
// which it can individually filter to get just the events it's interested
// in. So be aware that each KeyboardMouse object actually has its own X11
// "context."
m_display = XOpenDisplay(nullptr);
m_window_width = wsi.render_surface_width;
m_window_height = wsi.render_surface_height;

// should always be 1
int unused;
Expand Down Expand Up @@ -348,6 +351,12 @@ void KeyboardMouse::UpdateInput()
m_state.keyboard[i] &= keyboard[i];
}

void KeyboardMouse::OnWindowResized(int width, int height)
{
m_window_width = width;
m_window_height = height;
}

std::string KeyboardMouse::GetName() const
{
// This is the name string we got from the X server for this master
Expand Down
9 changes: 7 additions & 2 deletions Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ extern "C" {
}

#include "Common/Matrix.h"
#include "Common/WindowSystemInfo.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

namespace ciface::XInput2
{
void PopulateDevices(void* const hwnd);
void PopulateDevices(const WindowSystemInfo& wsi);

class KeyboardMouse : public Core::Device
{
Expand Down Expand Up @@ -112,8 +113,10 @@ class KeyboardMouse : public Core::Device

public:
void UpdateInput() override;
void OnWindowResized(int width, int height) override;

KeyboardMouse(Window window, int opcode, int pointer_deviceid, int keyboard_deviceid);
KeyboardMouse(const WindowSystemInfo& wsi, int opcode, int pointer_deviceid,
int keyboard_deviceid);
~KeyboardMouse();

std::string GetName() const override;
Expand All @@ -127,5 +130,7 @@ class KeyboardMouse : public Core::Device
const int pointer_deviceid;
const int keyboard_deviceid;
std::string name;
int m_window_width;
int m_window_height;
};
} // namespace ciface::XInput2

0 comments on commit 1ef113c

Please sign in to comment.