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 committed Jul 23, 2019
1 parent 743d948 commit 3927eb6
Show file tree
Hide file tree
Showing 12 changed files with 72 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 @@ -489,7 +489,8 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
}
else
{
g_controller_interface.ChangeWindow(wsi.render_surface);
g_controller_interface.ChangeWindow(wsi.render_surface, wsi.render_surface_width,
wsi.render_surface_height);
Pad::LoadConfig();
Keyboard::LoadConfig();
}
Expand Down
3 changes: 3 additions & 0 deletions 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 @@ -130,6 +131,8 @@ void PlatformWayland::TopLevelConfigure(void* data, struct xdg_toplevel* xdg_top
platform->m_surface_height = height;
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 @@ -11,6 +11,7 @@
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "Core/State.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

#include <climits>
#include <cstdio>
Expand Down Expand Up @@ -52,8 +53,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 @@ -172,8 +173,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 @@ -263,6 +265,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
4 changes: 3 additions & 1 deletion Source/Core/DolphinQt/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void Host::SetRenderHandle(void* handle, int width, int height)
{
g_renderer->ChangeSurface(handle, width, height);
if (g_controller_interface.IsInit())
g_controller_interface.ChangeWindow(handle);
g_controller_interface.ChangeWindow(handle, width, height);
}
}

Expand Down Expand Up @@ -85,6 +85,8 @@ void Host::ResizeSurface(int new_width, int new_height)
{
if (g_renderer)
g_renderer->ResizeSurface(new_width, new_height);
if (g_controller_interface.IsInit())
g_controller_interface.OnWindowResized(new_width, new_height);
}

void Host_Message(HostMessageID id)
Expand Down
7 changes: 6 additions & 1 deletion Source/Core/DolphinQt/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,12 @@ void MainWindow::HideRenderWidget(bool reinit)
// 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);
if (g_controller_interface.IsInit())
{
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 @@ -41,7 +41,10 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi)
if (m_is_init)
return;

// 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);

// Allow backends to add devices as soon as they are initialized.
m_is_init = true;
Expand Down Expand Up @@ -78,15 +81,23 @@ void ControllerInterface::Initialize(const WindowSystemInfo& wsi)
RefreshDevices();
}

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

m_wsi.render_surface = hwnd;
m_wsi.render_surface_width = std::max(width, 1);
m_wsi.render_surface_height = std::max(height, 1);
RefreshDevices();
}

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<std::mutex> lk(m_devices_mutex);
for (const auto& d : m_devices)
d->OnWindowResized(width, height);
}

void ControllerInterface::RefreshDevices()
{
if (!m_is_init)
Expand All @@ -107,7 +118,7 @@ void ControllerInterface::RefreshDevices()
#endif
#ifdef CIFACE_USE_XLIB
if (m_wsi.type == WindowSystemType::X11)
ciface::XInput2::PopulateDevices(m_wsi.render_surface);
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 @@ -46,7 +46,8 @@ class ControllerInterface : public ciface::Core::DeviceContainer

ControllerInterface() : m_is_init(false) {}
void Initialize(const WindowSystemInfo& wsi);
void ChangeWindow(void* hwnd);
void ChangeWindow(void* hwnd, int width, int height);
void OnWindowResized(int width, int height);
void RefreshDevices();
void Shutdown();
void AddDevice(std::shared_ptr<ciface::Core::Device> device);
Expand Down
3 changes: 3 additions & 0 deletions Source/Core/InputCommon/ControllerInterface/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class Device
// (e.g. Xbox 360 controllers have controller number LEDs which should match the ID we use.)
virtual std::optional<int> GetPreferredId() const;

// 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
21 changes: 15 additions & 6 deletions Source/Core/InputCommon/ControllerInterface/Xlib/XInput2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,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 @@ -83,7 +83,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 @@ -126,8 +126,9 @@ void KeyboardMouse::SelectEventsForDevice(Window window, XIEventMask* mask, int
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_surface)), xi_opcode(opcode),
pointer_deviceid(pointer), keyboard_deviceid(keyboard)
{
memset(&m_state, 0, sizeof(m_state));

Expand All @@ -137,6 +138,8 @@ KeyboardMouse::KeyboardMouse(Window window, int opcode, int pointer, int keyboar
// 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;

int min_keycode, max_keycode;
XDisplayKeycodes(m_display, &min_keycode, &max_keycode);
Expand Down Expand Up @@ -211,8 +214,8 @@ void KeyboardMouse::UpdateCursor()
XGetWindowAttributes(m_display, m_window, &win_attribs);

// the mouse position as a range from -1 to 1
m_state.cursor.x = win_x / (float)win_attribs.width * 2 - 1;
m_state.cursor.y = win_y / (float)win_attribs.height * 2 - 1;
m_state.cursor.x = static_cast<float>((win_x / static_cast<double>(m_window_width) * 2.0) - 1.0);
m_state.cursor.y = static_cast<float>((win_y / static_cast<double>(m_window_height) * 2.0) - 1.0);
}

void KeyboardMouse::UpdateInput()
Expand Down Expand Up @@ -295,6 +298,12 @@ void KeyboardMouse::UpdateInput()
UpdateCursor();
}

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 @@ -12,11 +12,12 @@ extern "C" {
#include <X11/keysym.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 @@ -96,8 +97,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 @@ -110,5 +113,7 @@ class KeyboardMouse : public Core::Device
int xi_opcode;
const int pointer_deviceid, keyboard_deviceid;
std::string name;
int m_window_width;
int m_window_height;
};
} // namespace ciface::XInput2

0 comments on commit 3927eb6

Please sign in to comment.