PlatformX11.cpp 7.8 KB

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