2018-10-24 05:50:54 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-10-24 05:50:54 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-02-12 06:48:46 +00:00
|
|
|
// X.h defines None to be 0L, but other parts of Dolphin undef that so that
|
|
|
|
// None can be used in enums. Work around that here by copying the definition
|
|
|
|
// before it is undefined.
|
|
|
|
#include <X11/X.h>
|
|
|
|
static constexpr auto X_None = None;
|
|
|
|
|
2018-10-24 05:50:54 +00:00
|
|
|
#include "DolphinNoGUI/Platform.h"
|
|
|
|
|
|
|
|
#include "Common/MsgHandler.h"
|
2019-03-03 04:41:50 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2018-10-24 05:50:54 +00:00
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/State.h"
|
2024-03-18 08:35:42 +00:00
|
|
|
#include "Core/System.h"
|
2018-10-24 05:50:54 +00:00
|
|
|
|
|
|
|
#include <climits>
|
|
|
|
#include <cstdio>
|
2019-06-07 22:25:32 +00:00
|
|
|
#include <cstring>
|
2023-01-29 14:09:40 +00:00
|
|
|
#include <thread>
|
2018-10-24 05:50:54 +00:00
|
|
|
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/Xlib.h>
|
2021-02-03 00:25:06 +00:00
|
|
|
#include <X11/Xutil.h>
|
2018-10-24 05:50:54 +00:00
|
|
|
#include <X11/keysym.h>
|
|
|
|
#include "UICommon/X11Utils.h"
|
2023-01-27 04:03:15 +00:00
|
|
|
#include "VideoCommon/Present.h"
|
2018-10-24 05:50:54 +00:00
|
|
|
|
|
|
|
#ifndef HOST_NAME_MAX
|
|
|
|
#define HOST_NAME_MAX _POSIX_HOST_NAME_MAX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class PlatformX11 : public Platform
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~PlatformX11() override;
|
|
|
|
|
|
|
|
bool Init() override;
|
|
|
|
void SetTitle(const std::string& string) override;
|
|
|
|
void MainLoop() override;
|
|
|
|
|
2020-03-23 08:33:55 +00:00
|
|
|
WindowSystemInfo GetWindowSystemInfo() const override;
|
2018-10-24 05:50:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void CloseDisplay();
|
|
|
|
void UpdateWindowPosition();
|
|
|
|
void ProcessEvents();
|
|
|
|
|
|
|
|
Display* m_display = nullptr;
|
|
|
|
Window m_window = {};
|
2021-02-12 06:48:46 +00:00
|
|
|
Cursor m_blank_cursor = X_None;
|
2021-02-22 13:32:53 +00:00
|
|
|
#ifdef HAVE_XRANDR
|
2018-10-24 05:50:54 +00:00
|
|
|
X11Utils::XRRConfiguration* m_xrr_config = nullptr;
|
|
|
|
#endif
|
2019-03-03 04:41:50 +00:00
|
|
|
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);
|
2018-10-24 05:50:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
PlatformX11::~PlatformX11()
|
|
|
|
{
|
2021-02-22 13:32:53 +00:00
|
|
|
#ifdef HAVE_XRANDR
|
2018-10-24 05:50:54 +00:00
|
|
|
delete m_xrr_config;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (m_display)
|
|
|
|
{
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never)
|
2018-10-24 05:50:54 +00:00
|
|
|
XFreeCursor(m_display, m_blank_cursor);
|
|
|
|
|
|
|
|
XCloseDisplay(m_display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PlatformX11::Init()
|
|
|
|
{
|
|
|
|
XInitThreads();
|
|
|
|
m_display = XOpenDisplay(nullptr);
|
|
|
|
if (!m_display)
|
|
|
|
{
|
2021-11-11 01:43:24 +00:00
|
|
|
PanicAlertFmt("No X11 display found");
|
2018-10-24 05:50:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-03 04:41:50 +00:00
|
|
|
m_window = XCreateSimpleWindow(m_display, DefaultRootWindow(m_display), m_window_x, m_window_y,
|
|
|
|
m_window_width, m_window_height, 0, 0, BlackPixel(m_display, 0));
|
2018-10-24 05:50:54 +00:00
|
|
|
XSelectInput(m_display, m_window, StructureNotifyMask | KeyPressMask | FocusChangeMask);
|
|
|
|
Atom wmProtocols[1];
|
|
|
|
wmProtocols[0] = XInternAtom(m_display, "WM_DELETE_WINDOW", True);
|
|
|
|
XSetWMProtocols(m_display, m_window, wmProtocols, 1);
|
|
|
|
pid_t pid = getpid();
|
|
|
|
XChangeProperty(m_display, m_window, XInternAtom(m_display, "_NET_WM_PID", False), XA_CARDINAL,
|
|
|
|
32, PropModeReplace, reinterpret_cast<unsigned char*>(&pid), 1);
|
|
|
|
char host_name[HOST_NAME_MAX] = "";
|
|
|
|
if (!gethostname(host_name, sizeof(host_name)))
|
|
|
|
{
|
|
|
|
XTextProperty wmClientMachine = {reinterpret_cast<unsigned char*>(host_name), XA_STRING, 8,
|
|
|
|
strlen(host_name)};
|
|
|
|
XSetWMClientMachine(m_display, m_window, &wmClientMachine);
|
|
|
|
}
|
|
|
|
XMapRaised(m_display, m_window);
|
|
|
|
XFlush(m_display);
|
|
|
|
XSync(m_display, True);
|
|
|
|
ProcessEvents();
|
|
|
|
|
2019-03-03 04:41:50 +00:00
|
|
|
if (Config::Get(Config::MAIN_DISABLE_SCREENSAVER))
|
2018-10-24 05:50:54 +00:00
|
|
|
X11Utils::InhibitScreensaver(m_window, true);
|
|
|
|
|
2021-02-22 13:32:53 +00:00
|
|
|
#ifdef HAVE_XRANDR
|
2018-10-24 05:50:54 +00:00
|
|
|
m_xrr_config = new X11Utils::XRRConfiguration(m_display, m_window);
|
|
|
|
#endif
|
|
|
|
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never)
|
2018-10-24 05:50:54 +00:00
|
|
|
{
|
|
|
|
// make a blank cursor
|
|
|
|
Pixmap Blank;
|
|
|
|
XColor DummyColor;
|
|
|
|
char ZeroData[1] = {0};
|
|
|
|
Blank = XCreateBitmapFromData(m_display, m_window, ZeroData, 1, 1);
|
|
|
|
m_blank_cursor = XCreatePixmapCursor(m_display, Blank, Blank, &DummyColor, &DummyColor, 0, 0);
|
|
|
|
XFreePixmap(m_display, Blank);
|
|
|
|
XDefineCursor(m_display, m_window, m_blank_cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter fullscreen if enabled.
|
2019-03-03 04:41:50 +00:00
|
|
|
if (Config::Get(Config::MAIN_FULLSCREEN))
|
2018-10-24 05:50:54 +00:00
|
|
|
{
|
|
|
|
m_window_fullscreen = X11Utils::ToggleFullscreen(m_display, m_window);
|
2021-02-22 13:32:53 +00:00
|
|
|
#ifdef HAVE_XRANDR
|
2018-10-24 05:50:54 +00:00
|
|
|
m_xrr_config->ToggleDisplayMode(True);
|
|
|
|
#endif
|
|
|
|
ProcessEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateWindowPosition();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformX11::SetTitle(const std::string& string)
|
|
|
|
{
|
|
|
|
XStoreName(m_display, m_window, string.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformX11::MainLoop()
|
|
|
|
{
|
|
|
|
while (IsRunning())
|
|
|
|
{
|
|
|
|
UpdateRunningFlag();
|
2024-03-18 08:35:42 +00:00
|
|
|
Core::HostDispatchJobs(Core::System::GetInstance());
|
2018-10-24 05:50:54 +00:00
|
|
|
ProcessEvents();
|
|
|
|
UpdateWindowPosition();
|
|
|
|
|
|
|
|
// TODO: Is this sleep appropriate?
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WindowSystemInfo PlatformX11::GetWindowSystemInfo() const
|
|
|
|
{
|
|
|
|
WindowSystemInfo wsi;
|
|
|
|
wsi.type = WindowSystemType::X11;
|
|
|
|
wsi.display_connection = static_cast<void*>(m_display);
|
2020-03-11 13:09:28 +00:00
|
|
|
wsi.render_window = reinterpret_cast<void*>(m_window);
|
2018-10-24 05:50:54 +00:00
|
|
|
wsi.render_surface = reinterpret_cast<void*>(m_window);
|
|
|
|
return wsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformX11::UpdateWindowPosition()
|
|
|
|
{
|
|
|
|
if (m_window_fullscreen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Window winDummy;
|
|
|
|
unsigned int borderDummy, depthDummy;
|
2019-03-03 04:41:50 +00:00
|
|
|
XGetGeometry(m_display, m_window, &winDummy, &m_window_x, &m_window_y, &m_window_width,
|
|
|
|
&m_window_height, &borderDummy, &depthDummy);
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PlatformX11::ProcessEvents()
|
|
|
|
{
|
|
|
|
XEvent event;
|
|
|
|
KeySym key;
|
|
|
|
for (int num_events = XPending(m_display); num_events > 0; num_events--)
|
|
|
|
{
|
|
|
|
XNextEvent(m_display, &event);
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case KeyPress:
|
|
|
|
key = XLookupKeysym((XKeyEvent*)&event, 0);
|
|
|
|
if (key == XK_Escape)
|
|
|
|
{
|
|
|
|
RequestShutdown();
|
|
|
|
}
|
|
|
|
else if (key == XK_F10)
|
|
|
|
{
|
2024-03-28 18:35:13 +00:00
|
|
|
if (Core::GetState(Core::System::GetInstance()) == Core::State::Running)
|
2018-10-24 05:50:54 +00:00
|
|
|
{
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never)
|
2018-10-24 05:50:54 +00:00
|
|
|
XUndefineCursor(m_display, m_window);
|
2024-05-04 03:49:48 +00:00
|
|
|
Core::SetState(Core::System::GetInstance(), Core::State::Paused);
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never)
|
2018-10-24 05:50:54 +00:00
|
|
|
XDefineCursor(m_display, m_window, m_blank_cursor);
|
2024-05-04 03:49:48 +00:00
|
|
|
Core::SetState(Core::System::GetInstance(), Core::State::Running);
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((key == XK_Return) && (event.xkey.state & Mod1Mask))
|
|
|
|
{
|
|
|
|
m_window_fullscreen = !m_window_fullscreen;
|
|
|
|
X11Utils::ToggleFullscreen(m_display, m_window);
|
2021-02-22 13:32:53 +00:00
|
|
|
#ifdef HAVE_XRANDR
|
2018-10-24 05:50:54 +00:00
|
|
|
m_xrr_config->ToggleDisplayMode(m_window_fullscreen);
|
|
|
|
#endif
|
|
|
|
UpdateWindowPosition();
|
|
|
|
}
|
|
|
|
else if (key >= XK_F1 && key <= XK_F8)
|
|
|
|
{
|
|
|
|
int slot_number = key - XK_F1 + 1;
|
|
|
|
if (event.xkey.state & ShiftMask)
|
2024-03-01 18:41:48 +00:00
|
|
|
State::Save(Core::System::GetInstance(), slot_number);
|
2018-10-24 05:50:54 +00:00
|
|
|
else
|
2024-03-01 18:41:48 +00:00
|
|
|
State::Load(Core::System::GetInstance(), slot_number);
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
else if (key == XK_F9)
|
|
|
|
Core::SaveScreenShot();
|
|
|
|
else if (key == XK_F11)
|
2024-03-01 18:41:48 +00:00
|
|
|
State::LoadLastSaved(Core::System::GetInstance());
|
2018-10-24 05:50:54 +00:00
|
|
|
else if (key == XK_F12)
|
|
|
|
{
|
|
|
|
if (event.xkey.state & ShiftMask)
|
2024-03-01 18:41:48 +00:00
|
|
|
State::UndoLoadState(Core::System::GetInstance());
|
2018-10-24 05:50:54 +00:00
|
|
|
else
|
2024-03-01 18:41:48 +00:00
|
|
|
State::UndoSaveState(Core::System::GetInstance());
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FocusIn:
|
|
|
|
{
|
|
|
|
m_window_focus = true;
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never &&
|
2024-03-28 18:35:13 +00:00
|
|
|
Core::GetState(Core::System::GetInstance()) != Core::State::Paused)
|
|
|
|
{
|
2018-10-24 05:50:54 +00:00
|
|
|
XDefineCursor(m_display, m_window, m_blank_cursor);
|
2024-03-28 18:35:13 +00:00
|
|
|
}
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FocusOut:
|
|
|
|
{
|
|
|
|
m_window_focus = false;
|
2021-12-31 02:00:39 +00:00
|
|
|
if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never)
|
2018-10-24 05:50:54 +00:00
|
|
|
XUndefineCursor(m_display, m_window);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ClientMessage:
|
|
|
|
{
|
|
|
|
if ((unsigned long)event.xclient.data.l[0] ==
|
|
|
|
XInternAtom(m_display, "WM_DELETE_WINDOW", False))
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ConfigureNotify:
|
|
|
|
{
|
2023-01-27 04:03:15 +00:00
|
|
|
if (g_presenter)
|
|
|
|
g_presenter->ResizeSurface();
|
2018-10-24 05:50:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<Platform> Platform::CreateX11Platform()
|
|
|
|
{
|
|
|
|
return std::make_unique<PlatformX11>();
|
|
|
|
}
|