-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_instance_test.c
More file actions
428 lines (359 loc) · 10.5 KB
/
native_instance_test.c
File metadata and controls
428 lines (359 loc) · 10.5 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "pal/pal_video.h"
#include "tests.h"
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#endif // NOMINMAX
// set unicode
#ifndef UNICODE
#define UNICODE
#endif // UNICODE
#include <windows.h>
#elif defined(__linux__)
#include <X11/Xlib.h>
#include <dlfcn.h>
#include <stdlib.h>
typedef Display* (*XOpenDisplayFn)(const char*);
typedef int (*XCloseDisplayFn)(Display*);
static void* s_LibX;
static XOpenDisplayFn s_XOpenDisplay;
static XCloseDisplayFn s_XCloseDisplay;
struct wl_display;
struct wl_registry;
struct wl_proxy;
struct wl_interface;
typedef struct wl_display* (*wl_display_connect_fn)(const char*);
typedef void (*wl_display_disconnect_fn)(struct wl_display*);
typedef uint32_t (*wl_proxy_get_version_fn)(struct wl_proxy*);
typedef int (*wl_display_roundtrip_fn)(struct wl_display*);
typedef void (*wl_proxy_destroy_fn)(struct wl_proxy*);
typedef struct wl_proxy* (*wl_proxy_marshal_flags_fn)(
struct wl_proxy*,
uint32_t,
const struct wl_interface*,
uint32_t,
uint32_t,
...);
typedef int (*wl_proxy_add_listener_fn)(
struct wl_proxy*,
void (**)(void),
void*);
struct wl_interface {
const char* name;
int version;
int method_count;
const struct wl_message* methods;
int event_count;
const struct wl_message* events;
};
struct wl_registry_listener {
void (*global)(
void*,
struct wl_registry*,
uint32_t,
const char*,
uint32_t);
void (*global_remove)(
void*,
struct wl_registry*,
uint32_t);
};
static void* s_LibWayland;
static wl_display_connect_fn s_wl_display_connect;
static wl_display_disconnect_fn s_wl_display_disconnect;
static wl_display_roundtrip_fn s_wl_display_roundtrip;
static wl_proxy_get_version_fn s_wl_proxy_get_version;
static wl_proxy_marshal_flags_fn s_wl_proxy_marshal_flags;
static wl_proxy_add_listener_fn s_wl_proxy_add_listener;
static wl_proxy_destroy_fn s_wl_proxy_destroy;
static struct wl_registry* s_Registry;
static const struct wl_interface* registryInterface;
static inline void* wlRegistryBind(
struct wl_registry* wl_registry,
uint32_t name,
const struct wl_interface* interface,
uint32_t version)
{
struct wl_proxy* id;
id = s_wl_proxy_marshal_flags(
(struct wl_proxy*)wl_registry,
0, // WL_REGISTRY_BIND
interface,
version,
0,
name,
interface->name,
version,
NULL);
return (void*)id;
}
static inline int wlRegistryAddListener(
struct wl_registry* wl_registry,
const struct wl_registry_listener* listener,
void* data)
{
return s_wl_proxy_add_listener(
(struct wl_proxy*)wl_registry,
(void (**)(void))listener,
data);
}
static inline struct wl_registry*
wlDisplayGetRegistry(struct wl_display* wl_display)
{
struct wl_proxy* registry;
registry = s_wl_proxy_marshal_flags(
(struct wl_proxy*)wl_display,
1, // WL_DISPLAY_GET_REGISTRY
registryInterface,
s_wl_proxy_get_version((struct wl_proxy*)wl_display),
0,
NULL);
return (struct wl_registry*)registry;
}
static bool s_Logged = false;
static void globalHandle(
void* data,
struct wl_registry* registry,
uint32_t name,
const char* interface,
uint32_t version)
{
if (!s_Logged) {
palLog(nullptr, "Registry global handle working");
s_Logged = true;
}
}
static void globalRemove(
void* data,
struct wl_registry* registry,
uint32_t name)
{
if (s_Logged) {
palLog(nullptr, "Registry global remove working");
s_Logged = false;
}
}
static const struct wl_registry_listener s_RegistryListener = {
.global = globalHandle,
.global_remove = globalRemove};
static bool s_OnWayland = false;
#endif // _WIN32
void* openDisplayX11()
{
#ifdef __linux__
// load the procs
s_LibX = dlopen("libX11.so", RTLD_LAZY);
if (!s_LibX) {
return nullptr;
}
// clang-format off
s_XOpenDisplay = (XOpenDisplayFn)dlsym(
s_LibX,
"XOpenDisplay");
s_XCloseDisplay = (XCloseDisplayFn)dlsym(
s_LibX,
"XCloseDisplay");
return s_XOpenDisplay(nullptr);
#endif // __linux__
}
void closeDisplayX11(void* instance)
{
#ifdef __linux__
s_XCloseDisplay((Display*)instance);
dlclose(s_LibX);
#endif // __linux__
}
void* openDisplayWayland()
{
#ifdef __linux__
// load the procs
s_LibWayland = dlopen("libwayland-client.so.0", RTLD_LAZY);
if (!s_LibWayland) {
return nullptr;
}
// clang-format off
s_wl_display_connect = (wl_display_connect_fn)dlsym(
s_LibWayland,
"wl_display_connect");
s_wl_display_disconnect = (wl_display_disconnect_fn)dlsym(
s_LibWayland,
"wl_display_disconnect");
s_wl_display_roundtrip = (wl_display_roundtrip_fn)dlsym(
s_LibWayland,
"wl_display_roundtrip");
s_wl_proxy_marshal_flags = (wl_proxy_marshal_flags_fn)dlsym(
s_LibWayland,
"wl_proxy_marshal_flags");
s_wl_proxy_get_version = (wl_proxy_get_version_fn)dlsym(
s_LibWayland,
"wl_proxy_get_version");
s_wl_proxy_add_listener = (wl_proxy_add_listener_fn)dlsym(
s_LibWayland,
"wl_proxy_add_listener");
s_wl_proxy_destroy = (wl_proxy_destroy_fn)dlsym(
s_LibWayland,
"wl_proxy_destroy");
registryInterface = dlsym(s_LibWayland, "wl_registry_interface");
struct wl_display* display = s_wl_display_connect(nullptr);
if (display) {
s_Registry = wlDisplayGetRegistry(display);
wlRegistryAddListener(s_Registry, &s_RegistryListener, nullptr);
s_wl_display_roundtrip(display);
}
return display;
#endif // __linux__
}
void closeDisplayWayland(void* instance)
{
#ifdef __linux__
s_wl_proxy_destroy((struct wl_proxy*)s_Registry);
s_wl_display_disconnect((struct wl_display*)instance);
dlclose(s_LibWayland);
#endif // __linux__
}
void* openDisplayWin32()
{
#ifdef __WIN32
return GetModuleHandleW(nullptr);
#endif // __WIN32
}
void closeDisplayWin32(void* instance)
{
// this does nothing
}
void* openInstance()
{
#ifdef _WIN32
return openDisplayWin32();
#elif defined(__linux__)
// get the active session
const char* session = getenv("XDG_SESSION_TYPE");
if (session) {
if (strcmp(session, "wayland") == 0) {
s_OnWayland = true;
} else {
s_OnWayland = false;
}
}
if (s_OnWayland) {
return openDisplayWayland();
} else {
return openDisplayX11();
}
#endif // _WIN32
}
void closeInstance(void* instance)
{
#ifdef _WIN32
closeDisplayWin32(instance);
#elif defined(__linux__)
if (s_OnWayland) {
closeDisplayWayland(instance);
} else {
closeDisplayX11(instance);
}
#endif // _WIN32
}
bool nativeInstanceTest()
{
palLog(nullptr, "");
palLog(nullptr, "===========================================");
palLog(nullptr, "Native Instance Test");
palLog(nullptr, "Press Escape or click close button to close Test");
palLog(nullptr, "===========================================");
palLog(nullptr, "");
PalResult result;
// event driver
PalEventDriver* eventDriver = nullptr;
PalEventDriverCreateInfo eventDriverCreateInfo = {0};
// create the event driver
result = palCreateEventDriver(&eventDriverCreateInfo, &eventDriver);
if (result != PAL_RESULT_SUCCESS) {
const char* error = palFormatResult(result);
palLog(nullptr, "Failed to create event driver: %s", error);
return false;
}
// open our own display or instance
void* instance = openInstance();
if (!instance) {
palLog(nullptr, "Failed to open instance");
return false;
}
// tell the video system to use out instance rather
// than creating a new one
// this can be set to the opengl system as well
palSetPreferredInstance(instance);
// initialize the video system. We pass the event driver to recieve video
// related events the video system does not copy the event driver, it must
// be valid till the video system is shutdown
result = palInitVideo(nullptr, eventDriver);
if (result != PAL_RESULT_SUCCESS) {
const char* error = palFormatResult(result);
palLog(nullptr, "Failed to initialize video: %s", error);
return false;
}
PalWindow* window = nullptr;
PalWindowCreateInfo createInfo = {0};
createInfo.monitor = nullptr; // use default monitor
createInfo.height = 480;
createInfo.width = 640;
createInfo.show = true;
createInfo.style = PAL_WINDOW_STYLE_RESIZABLE;
createInfo.title = "Native Instance Test";
// check if we support decorated windows (title bar, close etc)
PalVideoFeatures64 features = palGetVideoFeaturesEx();
if (!(features & PAL_VIDEO_FEATURE64_DECORATED_WINDOW)) {
// if we dont support, we need to create a borderless window
// and create the decorations ourselves
createInfo.style |= PAL_WINDOW_STYLE_BORDERLESS;
}
result = palCreateWindow(&createInfo, &window);
if (result != PAL_RESULT_SUCCESS) {
const char* error = palFormatResult(result);
palLog(nullptr, "Failed to create window: %s", error);
return false;
}
// we set window close to poll
palSetEventDispatchMode(
eventDriver,
PAL_EVENT_WINDOW_CLOSE,
PAL_DISPATCH_POLL);
palSetEventDispatchMode(
eventDriver,
PAL_EVENT_KEYDOWN,
PAL_DISPATCH_POLL);
bool running = true;
while (running) {
// update the video system to push video events
palUpdateVideo();
PalEvent event;
while (palPollEvent(eventDriver, &event)) {
switch (event.type) {
case PAL_EVENT_WINDOW_CLOSE: {
running = false;
break;
}
case PAL_EVENT_KEYDOWN: {
PalKeycode keycode = 0;
palUnpackUint32(event.data, &keycode, nullptr);
if (keycode == PAL_KEYCODE_ESCAPE) {
running = false;
}
break;
}
}
}
}
// destroy the window
palDestroyWindow(window);
// shutdown the video system
palShutdownVideo();
// destroy the event driver
palDestroyEventDriver(eventDriver);
// the video system does not destroy or close the handle provided
closeInstance(instance);
return true;
}