| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | // X.h defines None to be 0L, but other parts of Dolphin undef that so that |
| 7 | // None can be used in enums. Work around that here by copying the definition |
| 8 | // before it is undefined. |
| 9 | #include <X11/X.h> |
| 10 | static constexpr auto X_None = None; |
| 11 | |
| 12 | #include "DolphinNoGUI/Platform.h" |
| 13 | |
| 14 | #include "Common/MsgHandler.h" |
| 15 | #include "Core/Config/MainSettings.h" |
| 16 | #include "Core/Core.h" |
| 17 | #include "Core/State.h" |
| 18 | #include "Core/System.h" |
| 19 | |
| 20 | #include <cstring> |
| 21 | #include <thread> |
| 22 | |
| 23 | #include <X11/Xatom.h> |
| 24 | #include <X11/Xlib.h> |
| 25 | #include <X11/Xutil.h> |
| 26 | #include <X11/keysym.h> |
| 27 | #include "UICommon/UICommon.h" |
| 28 | #include "UICommon/X11Utils.h" |
| 29 | #include "VideoCommon/Present.h" |
| 30 | |
| 31 | #ifndef HOST_NAME_MAX |
| 32 | #define HOST_NAME_MAX _POSIX_HOST_NAME_MAX |
| 33 | #endif |
| 34 | |
| 35 | namespace |
| 36 | { |
| 37 | class PlatformX11 : public Platform |
| 38 | { |
| 39 | public: |
| 40 | ~PlatformX11() override; |
| 41 | |
| 42 | bool Init() override; |
| 43 | void SetTitle(const std::string& string) override; |
| 44 | void MainLoop() override; |
| 45 | |
| 46 | WindowSystemInfo GetWindowSystemInfo() const override; |
| 47 | |
| 48 | private: |
| 49 | void CloseDisplay(); |
| 50 | void UpdateWindowPosition(); |
| 51 | void ProcessEvents(); |
| 52 | |
| 53 | Display* m_display = nullptr; |
| 54 | Window m_window = {}; |
| 55 | Cursor m_blank_cursor = X_None; |
| 56 | #ifdef HAVE_XRANDR |
| 57 | X11Utils::XRRConfiguration* m_xrr_config = nullptr; |
| 58 | #endif |
| 59 | int m_window_x = Config::Get(Config::MAIN_RENDER_WINDOW_XPOS); |
| 60 | int m_window_y = Config::Get(Config::MAIN_RENDER_WINDOW_YPOS); |
| 61 | unsigned int m_window_width = Config::Get(Config::MAIN_RENDER_WINDOW_WIDTH); |
| 62 | unsigned int m_window_height = Config::Get(Config::MAIN_RENDER_WINDOW_HEIGHT); |
| 63 | }; |
| 64 | |
| 65 | PlatformX11::~PlatformX11() |
| 66 | { |
| 67 | #ifdef HAVE_XRANDR |
| 68 | delete m_xrr_config; |
| 69 | #endif |
| 70 | |
| 71 | if (m_display) |
| 72 | { |
| 73 | if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 74 | XFreeCursor(m_display, m_blank_cursor); |
| 75 | |
| 76 | XCloseDisplay(m_display); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | bool PlatformX11::Init() |
| 81 | { |
| 82 | XInitThreads(); |
| 83 | m_display = XOpenDisplay(nullptr); |
| 84 | if (!m_display) |
| 85 | { |
| 86 | PanicAlertFmt("No X11 display found"); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | m_window = XCreateSimpleWindow(m_display, DefaultRootWindow(m_display), m_window_x, m_window_y, |
| 91 | m_window_width, m_window_height, 0, 0, BlackPixel(m_display, 0)); |
| 92 | XSelectInput(m_display, m_window, StructureNotifyMask | KeyPressMask | FocusChangeMask); |
| 93 | Atom wmProtocols[1]; |
| 94 | wmProtocols[0] = XInternAtom(m_display, "WM_DELETE_WINDOW", True); |
| 95 | XSetWMProtocols(m_display, m_window, wmProtocols, 1); |
| 96 | pid_t pid = getpid(); |
| 97 | XChangeProperty(m_display, m_window, XInternAtom(m_display, "_NET_WM_PID", False), XA_CARDINAL, |
| 98 | 32, PropModeReplace, reinterpret_cast<unsigned char*>(&pid), 1); |
| 99 | char host_name[HOST_NAME_MAX] = ""; |
| 100 | if (!gethostname(host_name, sizeof(host_name))) |
| 101 | { |
| 102 | XTextProperty wmClientMachine = {reinterpret_cast<unsigned char*>(host_name), XA_STRING, 8, |
| 103 | strlen(host_name)}; |
| 104 | XSetWMClientMachine(m_display, m_window, &wmClientMachine); |
| 105 | } |
| 106 | XMapRaised(m_display, m_window); |
| 107 | XFlush(m_display); |
| 108 | XSync(m_display, True); |
| 109 | ProcessEvents(); |
| 110 | |
| 111 | if (Config::Get(Config::MAIN_DISABLE_SCREENSAVER)) |
| 112 | UICommon::InhibitScreenSaver(true); |
| 113 | |
| 114 | #ifdef HAVE_XRANDR |
| 115 | m_xrr_config = new X11Utils::XRRConfiguration(m_display, m_window); |
| 116 | #endif |
| 117 | |
| 118 | if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 119 | { |
| 120 | // make a blank cursor |
| 121 | Pixmap Blank; |
| 122 | XColor DummyColor; |
| 123 | char ZeroData[1] = {0}; |
| 124 | Blank = XCreateBitmapFromData(m_display, m_window, ZeroData, 1, 1); |
| 125 | m_blank_cursor = XCreatePixmapCursor(m_display, Blank, Blank, &DummyColor, &DummyColor, 0, 0); |
| 126 | XFreePixmap(m_display, Blank); |
| 127 | XDefineCursor(m_display, m_window, m_blank_cursor); |
| 128 | } |
| 129 | |
| 130 | // Enter fullscreen if enabled. |
| 131 | if (Config::Get(Config::MAIN_FULLSCREEN)) |
| 132 | { |
| 133 | m_window_fullscreen = X11Utils::ToggleFullscreen(m_display, m_window); |
| 134 | #ifdef HAVE_XRANDR |
| 135 | m_xrr_config->ToggleDisplayMode(True); |
| 136 | #endif |
| 137 | ProcessEvents(); |
| 138 | } |
| 139 | |
| 140 | UpdateWindowPosition(); |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | void PlatformX11::SetTitle(const std::string& string) |
| 145 | { |
| 146 | XStoreName(m_display, m_window, string.c_str()); |
| 147 | } |
| 148 | |
| 149 | void PlatformX11::MainLoop() |
| 150 | { |
| 151 | while (IsRunning()) |
| 152 | { |
| 153 | UpdateRunningFlag(); |
| 154 | Core::HostDispatchJobs(Core::System::GetInstance()); |
| 155 | ProcessEvents(); |
| 156 | UpdateWindowPosition(); |
| 157 | |
| 158 | // TODO: Is this sleep appropriate? |
| 159 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | WindowSystemInfo PlatformX11::GetWindowSystemInfo() const |
| 164 | { |
| 165 | WindowSystemInfo wsi; |
| 166 | wsi.type = WindowSystemType::X11; |
| 167 | wsi.display_connection = static_cast<void*>(m_display); |
| 168 | wsi.render_window = reinterpret_cast<void*>(m_window); |
| 169 | wsi.render_surface = reinterpret_cast<void*>(m_window); |
| 170 | return wsi; |
| 171 | } |
| 172 | |
| 173 | void PlatformX11::UpdateWindowPosition() |
| 174 | { |
| 175 | if (m_window_fullscreen) |
| 176 | return; |
| 177 | |
| 178 | Window winDummy; |
| 179 | unsigned int borderDummy, depthDummy; |
| 180 | XGetGeometry(m_display, m_window, &winDummy, &m_window_x, &m_window_y, &m_window_width, |
| 181 | &m_window_height, &borderDummy, &depthDummy); |
| 182 | } |
| 183 | |
| 184 | void PlatformX11::ProcessEvents() |
| 185 | { |
| 186 | XEvent event; |
| 187 | KeySym key; |
| 188 | for (int num_events = XPending(m_display); num_events > 0; num_events--) |
| 189 | { |
| 190 | XNextEvent(m_display, &event); |
| 191 | switch (event.type) |
| 192 | { |
| 193 | case KeyPress: |
| 194 | key = XLookupKeysym((XKeyEvent*)&event, 0); |
| 195 | if (key == XK_Escape) |
| 196 | { |
| 197 | RequestShutdown(); |
| 198 | } |
| 199 | else if (key == XK_F10) |
| 200 | { |
| 201 | if (Core::GetState(Core::System::GetInstance()) == Core::State::Running) |
| 202 | { |
| 203 | if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 204 | XUndefineCursor(m_display, m_window); |
| 205 | Core::SetState(Core::System::GetInstance(), Core::State::Paused); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 210 | XDefineCursor(m_display, m_window, m_blank_cursor); |
| 211 | Core::SetState(Core::System::GetInstance(), Core::State::Running); |
| 212 | } |
| 213 | } |
| 214 | else if ((key == XK_Return) && (event.xkey.state & Mod1Mask)) |
| 215 | { |
| 216 | m_window_fullscreen = !m_window_fullscreen; |
| 217 | X11Utils::ToggleFullscreen(m_display, m_window); |
| 218 | #ifdef HAVE_XRANDR |
| 219 | m_xrr_config->ToggleDisplayMode(m_window_fullscreen); |
| 220 | #endif |
| 221 | UpdateWindowPosition(); |
| 222 | } |
| 223 | else if (key >= XK_F1 && key <= XK_F8) |
| 224 | { |
| 225 | int slot_number = key - XK_F1 + 1; |
| 226 | if (event.xkey.state & ShiftMask) |
| 227 | State::Save(Core::System::GetInstance(), slot_number); |
| 228 | else |
| 229 | State::Load(Core::System::GetInstance(), slot_number); |
| 230 | } |
| 231 | else if (key == XK_F9) |
| 232 | Core::SaveScreenShot(); |
| 233 | else if (key == XK_F11) |
| 234 | State::LoadLastSaved(Core::System::GetInstance()); |
| 235 | else if (key == XK_F12) |
| 236 | { |
| 237 | if (event.xkey.state & ShiftMask) |
| 238 | State::UndoLoadState(Core::System::GetInstance()); |
| 239 | else |
| 240 | State::UndoSaveState(Core::System::GetInstance()); |
| 241 | } |
| 242 | break; |
| 243 | case FocusIn: |
| 244 | { |
| 245 | m_window_focus = true; |
| 246 | if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never && |
| 247 | Core::GetState(Core::System::GetInstance()) != Core::State::Paused) |
| 248 | { |
| 249 | XDefineCursor(m_display, m_window, m_blank_cursor); |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | case FocusOut: |
| 254 | { |
| 255 | m_window_focus = false; |
| 256 | if (Config::Get(Config::MAIN_SHOW_CURSOR) == Config::ShowCursor::Never) |
| 257 | XUndefineCursor(m_display, m_window); |
| 258 | } |
| 259 | break; |
| 260 | case ClientMessage: |
| 261 | { |
| 262 | if ((unsigned long)event.xclient.data.l[0] == |
| 263 | XInternAtom(m_display, "WM_DELETE_WINDOW", False)) |
| 264 | Stop(); |
| 265 | } |
| 266 | break; |
| 267 | case ConfigureNotify: |
| 268 | { |
| 269 | if (g_presenter) |
| 270 | g_presenter->ResizeSurface(); |
| 271 | } |
| 272 | break; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } // namespace |
| 277 | |
| 278 | std::unique_ptr<Platform> Platform::CreateX11Platform() |
| 279 | { |
| 280 | return std::make_unique<PlatformX11>(); |
| 281 | } |