RmlUi_Backend_X11_GL2.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "RmlUi_Backend.h"
  29. #include "RmlUi_Include_Xlib.h"
  30. #include "RmlUi_Platform_X11.h"
  31. #include "RmlUi_Renderer_GL2.h"
  32. #include <RmlUi/Core/Context.h>
  33. #include <GL/gl.h>
  34. #include <GL/glext.h>
  35. #include <GL/glu.h>
  36. #include <GL/glx.h>
  37. #include <X11/Xlib.h>
  38. #include <X11/cursorfont.h>
  39. #include <X11/extensions/xf86vmode.h>
  40. #include <limits.h>
  41. #include <stdarg.h>
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/stat.h>
  45. #include <sys/time.h>
  46. #include <sys/types.h>
  47. #include <time.h>
  48. #include <unistd.h>
  49. // Attach the OpenGL context to the window.
  50. static bool AttachToNative(GLXContext& out_gl_context, Display* display, Window window, XVisualInfo* visual_info)
  51. {
  52. GLXContext gl_context = glXCreateContext(display, visual_info, nullptr, GL_TRUE);
  53. if (!gl_context)
  54. return false;
  55. if (!glXMakeCurrent(display, window, gl_context))
  56. return false;
  57. if (!glXIsDirect(display, gl_context))
  58. puts("OpenGL context does not support direct rendering; performance is likely to be poor.");
  59. out_gl_context = gl_context;
  60. Window root_window;
  61. int x, y;
  62. unsigned int width, height;
  63. unsigned int border_width, depth;
  64. XGetGeometry(display, window, &root_window, &x, &y, &width, &height, &border_width, &depth);
  65. return true;
  66. }
  67. // Shutdown the OpenGL context.
  68. static void DetachFromNative(GLXContext gl_context, Display* display)
  69. {
  70. glXMakeCurrent(display, 0L, nullptr);
  71. glXDestroyContext(display, gl_context);
  72. }
  73. /**
  74. Global data used by this backend.
  75. Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
  76. */
  77. struct BackendData {
  78. BackendData(Display* display) : system_interface(display) {}
  79. SystemInterface_X11 system_interface;
  80. RenderInterface_GL2 render_interface;
  81. Display* display = nullptr;
  82. Window window = 0;
  83. GLXContext gl_context = nullptr;
  84. bool running = true;
  85. };
  86. static Rml::UniquePtr<BackendData> data;
  87. bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
  88. {
  89. RMLUI_ASSERT(!data);
  90. Display* display = XOpenDisplay(0);
  91. if (!display)
  92. return false;
  93. int screen = XDefaultScreen(display);
  94. // Fetch an appropriate 32-bit visual interface.
  95. int attribute_list[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, GLX_DEPTH_SIZE, 24, GLX_STENCIL_SIZE, 8,
  96. 0L};
  97. XVisualInfo* visual_info = glXChooseVisual(display, screen, attribute_list);
  98. if (!visual_info)
  99. return false;
  100. // Build up our window attributes.
  101. XSetWindowAttributes window_attributes;
  102. window_attributes.colormap = XCreateColormap(display, RootWindow(display, visual_info->screen), visual_info->visual, AllocNone);
  103. window_attributes.border_pixel = 0;
  104. window_attributes.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
  105. // Create the window.
  106. Window window = XCreateWindow(display, RootWindow(display, visual_info->screen), 0, 0, width, height, 0, visual_info->depth, InputOutput,
  107. visual_info->visual, CWBorderPixel | CWColormap | CWEventMask, &window_attributes);
  108. // Handle delete events in windowed mode.
  109. Atom delete_atom = XInternAtom(display, "WM_DELETE_WINDOW", True);
  110. XSetWMProtocols(display, window, &delete_atom, 1);
  111. // Capture the events we're interested in.
  112. XSelectInput(display, window,
  113. KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | LeaveWindowMask | PointerMotionMask | StructureNotifyMask);
  114. if (!allow_resize)
  115. {
  116. // Force the window to remain at the fixed size by asking the window manager nicely, it may choose to ignore us
  117. XSizeHints* win_size_hints = XAllocSizeHints(); // Allocate a size hint structure
  118. if (!win_size_hints)
  119. {
  120. fprintf(stderr, "XAllocSizeHints - out of memory\n");
  121. }
  122. else
  123. {
  124. // Initialize the structure and specify which hints will be providing
  125. win_size_hints->flags = PSize | PMinSize | PMaxSize;
  126. // Set the sizes we want the window manager to use
  127. win_size_hints->base_width = width;
  128. win_size_hints->base_height = height;
  129. win_size_hints->min_width = width;
  130. win_size_hints->min_height = height;
  131. win_size_hints->max_width = width;
  132. win_size_hints->max_height = height;
  133. // Pass the size hints to the window manager.
  134. XSetWMNormalHints(display, window, win_size_hints);
  135. // Free the size buffer
  136. XFree(win_size_hints);
  137. }
  138. }
  139. // Set the window title and show the window.
  140. XSetStandardProperties(display, window, window_name, "", 0L, nullptr, 0, nullptr);
  141. XMapRaised(display, window);
  142. GLXContext gl_context = {};
  143. if (!AttachToNative(gl_context, display, window, visual_info))
  144. return false;
  145. data = Rml::MakeUnique<BackendData>(display);
  146. data->display = display;
  147. data->window = window;
  148. data->gl_context = gl_context;
  149. data->system_interface.SetWindow(window);
  150. data->render_interface.SetViewport(width, height);
  151. return true;
  152. }
  153. void Backend::Shutdown()
  154. {
  155. RMLUI_ASSERT(data);
  156. DetachFromNative(data->gl_context, data->display);
  157. XCloseDisplay(data->display);
  158. data.reset();
  159. }
  160. Rml::SystemInterface* Backend::GetSystemInterface()
  161. {
  162. RMLUI_ASSERT(data);
  163. return &data->system_interface;
  164. }
  165. Rml::RenderInterface* Backend::GetRenderInterface()
  166. {
  167. RMLUI_ASSERT(data);
  168. return &data->render_interface;
  169. }
  170. bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback)
  171. {
  172. RMLUI_ASSERT(data && context);
  173. Display* display = data->display;
  174. bool result = data->running;
  175. data->running = true;
  176. while (XPending(display) > 0)
  177. {
  178. XEvent ev;
  179. XNextEvent(display, &ev);
  180. switch (ev.type)
  181. {
  182. case ClientMessage:
  183. {
  184. // The only message we register for is WM_DELETE_WINDOW, so if we receive a client message then the window has been closed.
  185. char* event_type = XGetAtomName(display, ev.xclient.message_type);
  186. if (strcmp(event_type, "WM_PROTOCOLS") == 0)
  187. data->running = false;
  188. XFree(event_type);
  189. event_type = nullptr;
  190. }
  191. break;
  192. case ConfigureNotify:
  193. {
  194. int x = ev.xconfigure.width;
  195. int y = ev.xconfigure.height;
  196. context->SetDimensions({x, y});
  197. data->render_interface.SetViewport(x, y);
  198. }
  199. break;
  200. case KeyPress:
  201. {
  202. Rml::Input::KeyIdentifier key = RmlX11::ConvertKey(display, ev.xkey.keycode);
  203. const int key_modifier = RmlX11::GetKeyModifierState(ev.xkey.state);
  204. const float native_dp_ratio = 1.f;
  205. // See if we have any global shortcuts that take priority over the context.
  206. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, true))
  207. break;
  208. // Otherwise, hand the event over to the context by calling the input handler as normal.
  209. if (!RmlX11::HandleInputEvent(context, display, ev))
  210. break;
  211. // The key was not consumed by the context either, try keyboard shortcuts of lower priority.
  212. if (key_down_callback && !key_down_callback(context, key, key_modifier, native_dp_ratio, false))
  213. break;
  214. }
  215. break;
  216. case SelectionRequest:
  217. {
  218. data->system_interface.HandleSelectionRequest(ev);
  219. }
  220. break;
  221. default:
  222. {
  223. // Pass unhandled events to the platform layer's input handler.
  224. RmlX11::HandleInputEvent(context, display, ev);
  225. }
  226. break;
  227. }
  228. }
  229. return result;
  230. }
  231. void Backend::RequestExit()
  232. {
  233. RMLUI_ASSERT(data);
  234. data->running = false;
  235. }
  236. void Backend::BeginFrame()
  237. {
  238. RMLUI_ASSERT(data);
  239. data->render_interface.BeginFrame();
  240. data->render_interface.Clear();
  241. }
  242. void Backend::PresentFrame()
  243. {
  244. RMLUI_ASSERT(data);
  245. data->render_interface.EndFrame();
  246. // Flips the OpenGL buffers.
  247. glXSwapBuffers(data->display, data->window);
  248. }