shadPS4/src/sdl_window.cpp

280 lines
8.9 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2024-06-10 14:24:34 +02:00
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_properties.h>
#include <SDL3/SDL_video.h>
#include "common/assert.h"
#include "common/config.h"
#include "common/version.h"
#include "core/libraries/pad/pad.h"
#include "input/controller.h"
#include "sdl_window.h"
#include "video_core/renderdoc.h"
2024-07-09 11:18:34 +02:00
#ifdef __APPLE__
#include <SDL3/SDL_metal.h>
#endif
namespace Frontend {
WindowSDL::WindowSDL(s32 width_, s32 height_, Input::GameController* controller_,
2024-07-31 23:56:10 +02:00
std::string_view window_title)
2024-07-25 09:15:44 +02:00
: width{width_}, height{height_}, controller{controller_} {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
UNREACHABLE_MSG("Failed to initialize SDL video subsystem: {}", SDL_GetError());
}
SDL_InitSubSystem(SDL_INIT_AUDIO);
SDL_PropertiesID props = SDL_CreateProperties();
2024-08-01 00:11:58 +02:00
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING,
std::string(window_title).c_str());
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SDL_WINDOWPOS_CENTERED);
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SDL_WINDOWPOS_CENTERED);
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, width);
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, height);
SDL_SetNumberProperty(props, "flags", SDL_WINDOW_VULKAN);
window = SDL_CreateWindowWithProperties(props);
SDL_DestroyProperties(props);
if (window == nullptr) {
UNREACHABLE_MSG("Failed to create window handle: {}", SDL_GetError());
}
SDL_SetWindowFullscreen(window, Config::isFullscreenMode());
#if defined(SDL_PLATFORM_WIN32)
window_info.type = WindowSystemType::Windows;
window_info.render_surface = SDL_GetPointerProperty(SDL_GetWindowProperties(window),
SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL);
#elif defined(SDL_PLATFORM_LINUX)
if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) {
window_info.type = WindowSystemType::X11;
2024-08-05 18:52:30 +02:00
window_info.display_connection = SDL_GetPointerProperty(
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL);
window_info.render_surface = (void*)SDL_GetNumberProperty(
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0);
} else if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) {
window_info.type = WindowSystemType::Wayland;
2024-08-05 21:36:25 +02:00
window_info.display_connection = SDL_GetPointerProperty(
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL);
2024-08-05 21:36:25 +02:00
window_info.render_surface = SDL_GetPointerProperty(
SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL);
}
2024-07-09 11:18:34 +02:00
#elif defined(SDL_PLATFORM_MACOS)
window_info.type = WindowSystemType::Metal;
window_info.render_surface = SDL_Metal_GetLayer(SDL_Metal_CreateView(window));
#endif
}
WindowSDL::~WindowSDL() = default;
void WindowSDL::waitEvent() {
// Called on main thread
SDL_Event event;
if (!SDL_WaitEvent(&event)) {
return;
}
switch (event.type) {
case SDL_EVENT_WINDOW_RESIZED:
case SDL_EVENT_WINDOW_MAXIMIZED:
case SDL_EVENT_WINDOW_RESTORED:
onResize();
break;
case SDL_EVENT_WINDOW_MINIMIZED:
case SDL_EVENT_WINDOW_EXPOSED:
is_shown = event.type == SDL_EVENT_WINDOW_EXPOSED;
onResize();
break;
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
onKeyPress(&event);
break;
case SDL_EVENT_QUIT:
is_open = false;
break;
default:
break;
}
}
void WindowSDL::onResize() {
SDL_GetWindowSizeInPixels(window, &width, &height);
}
void WindowSDL::onKeyPress(const SDL_Event* event) {
using Libraries::Pad::OrbisPadButtonDataOffset;
#ifdef __APPLE__
// Use keys that are more friendly for keyboards without a keypad.
// Once there are key binding options this won't be necessary.
constexpr SDL_Keycode CrossKey = SDLK_N;
constexpr SDL_Keycode CircleKey = SDLK_B;
constexpr SDL_Keycode SquareKey = SDLK_V;
constexpr SDL_Keycode TriangleKey = SDLK_C;
#else
constexpr SDL_Keycode CrossKey = SDLK_KP_2;
constexpr SDL_Keycode CircleKey = SDLK_KP_6;
constexpr SDL_Keycode SquareKey = SDLK_KP_4;
constexpr SDL_Keycode TriangleKey = SDLK_KP_8;
#endif
u32 button = 0;
2024-06-17 12:42:39 +02:00
Input::Axis axis = Input::Axis::AxisMax;
int axisvalue = 0;
int ax = 0;
switch (event->key.key) {
case SDLK_UP:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_UP;
break;
case SDLK_DOWN:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_DOWN;
break;
case SDLK_LEFT:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_LEFT;
break;
case SDLK_RIGHT:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_RIGHT;
break;
case TriangleKey:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TRIANGLE;
break;
case CircleKey:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CIRCLE;
break;
case CrossKey:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_CROSS;
break;
case SquareKey:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_SQUARE;
break;
case SDLK_RETURN:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_OPTIONS;
break;
2024-07-05 14:08:33 +02:00
case SDLK_A:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::LeftX;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += -127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_D:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::LeftX;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += 127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_W:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::LeftY;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += -127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_S:
if (event->key.mod == SDL_KMOD_LCTRL) {
// Trigger rdoc capture
VideoCore::TriggerCapture();
break;
}
2024-06-17 12:42:39 +02:00
axis = Input::Axis::LeftY;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += 127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_J:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::RightX;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += -127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_L:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::RightX;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += 127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_I:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::RightY;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += -127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_K:
2024-06-17 12:42:39 +02:00
axis = Input::Axis::RightY;
2024-06-17 13:33:03 +02:00
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += 127;
} else {
axisvalue = 0;
2024-06-20 17:09:40 +02:00
}
2024-06-17 12:42:39 +02:00
ax = Input::GetAxis(-0x80, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_X:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L3;
break;
2024-07-05 14:08:33 +02:00
case SDLK_M:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R3;
break;
2024-07-05 14:08:33 +02:00
case SDLK_Q:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L1;
break;
2024-07-05 14:08:33 +02:00
case SDLK_U:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R1;
break;
2024-07-05 14:08:33 +02:00
case SDLK_E:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L2;
axis = Input::Axis::TriggerLeft;
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += 255;
} else {
axisvalue = 0;
}
ax = Input::GetAxis(0, 0x80, axisvalue);
break;
2024-07-05 14:08:33 +02:00
case SDLK_O:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R2;
axis = Input::Axis::TriggerRight;
if (event->type == SDL_EVENT_KEY_DOWN) {
axisvalue += 255;
} else {
axisvalue = 0;
}
ax = Input::GetAxis(0, 0x80, axisvalue);
break;
case SDLK_SPACE:
button = OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_TOUCH_PAD;
break;
default:
break;
}
if (button != 0) {
2024-06-17 11:52:25 +02:00
controller->CheckButton(0, button, event->type == SDL_EVENT_KEY_DOWN);
}
2024-06-17 13:33:03 +02:00
if (axis != Input::Axis::AxisMax) {
2024-06-17 12:42:39 +02:00
controller->Axis(0, axis, ax);
}
}
} // namespace Frontend