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