@kmamal/sdl 0.9.5 → 0.9.6-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +34 -15
  2. package/binding.gyp +25 -5
  3. package/package.json +7 -4
  4. package/src/javascript/audio/audio-instance.js +9 -6
  5. package/src/javascript/cleanup.js +1 -1
  6. package/src/javascript/clipboard/index.js +2 -1
  7. package/src/javascript/controller/controller-instance.js +6 -0
  8. package/src/javascript/enums.js +1 -1
  9. package/src/javascript/events/index.js +302 -305
  10. package/src/javascript/index.js +1 -1
  11. package/src/javascript/joystick/joystick-instance.js +7 -0
  12. package/src/javascript/keyboard/index.js +3 -3
  13. package/src/javascript/keyboard/key-mapping.js +14 -6
  14. package/src/javascript/mouse/index.js +13 -12
  15. package/src/javascript/power/index.js +0 -2
  16. package/src/javascript/sensor/sensor-instance.js +1 -5
  17. package/src/javascript/video/window.js +17 -16
  18. package/src/native/audio.cpp +178 -0
  19. package/src/native/audio.h +21 -0
  20. package/src/native/clipboard.cpp +45 -0
  21. package/src/native/clipboard.h +13 -0
  22. package/src/native/cocoa-global.h +8 -0
  23. package/src/native/cocoa-global.mm +8 -0
  24. package/src/native/cocoa-window.h +9 -4
  25. package/src/native/cocoa-window.mm +6 -9
  26. package/src/native/controller.cpp +154 -0
  27. package/src/native/controller.h +17 -0
  28. package/src/native/enums.cpp +477 -0
  29. package/src/native/enums.h +67 -0
  30. package/src/native/events.cpp +339 -0
  31. package/src/native/events.h +15 -0
  32. package/src/native/global.cpp +187 -0
  33. package/src/native/global.h +15 -0
  34. package/src/native/joystick.cpp +388 -0
  35. package/src/native/joystick.h +22 -0
  36. package/src/native/keyboard.cpp +55 -0
  37. package/src/native/keyboard.h +18 -0
  38. package/src/native/module.cpp +98 -0
  39. package/src/native/mouse.cpp +186 -0
  40. package/src/native/mouse.h +21 -0
  41. package/src/native/power.cpp +18 -0
  42. package/src/native/power.h +12 -0
  43. package/src/native/sensor.cpp +117 -0
  44. package/src/native/sensor.h +15 -0
  45. package/src/native/video.cpp +90 -0
  46. package/src/native/video.h +12 -0
  47. package/src/native/window.cpp +755 -0
  48. package/src/native/window.h +29 -0
  49. package/src/types/index.d.ts +7 -4
  50. package/scripts/test.mjs +0 -12
  51. package/src/native/asprintf.c +0 -47
  52. package/src/native/asprintf.h +0 -37
  53. package/src/native/binding.cpp +0 -1680
  54. package/src/native/sdl-helpers.cpp +0 -2617
  55. package/src/native/sdl-helpers.h +0 -182
@@ -1,2617 +0,0 @@
1
- #include "sdl-helpers.h"
2
- #include <SDL.h>
3
- #include <SDL_syswm.h>
4
- #include <cstdio>
5
- #include <cstdlib>
6
- #include <cstring>
7
-
8
- #if defined(_WIN32)
9
- #include "asprintf.h"
10
- #endif
11
-
12
-
13
- #if defined(__LINUX__)
14
- #define GL_NativeWindow Window
15
- struct GPU_NativeData {
16
- Display *display;
17
- Window window;
18
- };
19
- #define GPU_WINDOW_FLAG SDL_WINDOW_VULKAN
20
- #elif defined(__WIN32__)
21
- #define GL_NativeWindow HWND
22
- struct GPU_NativeData {
23
- HWND hwnd;
24
- HINSTANCE hinstance;
25
- };
26
- #define GPU_WINDOW_FLAG 0
27
- #elif defined(__MACOSX__)
28
- #include "cocoa-window.h"
29
- #define GL_NativeWindow CALayer *
30
- struct GPU_NativeData {
31
- CALayer *layer;
32
- };
33
- #define GPU_WINDOW_FLAG SDL_WINDOW_METAL
34
- #endif
35
-
36
-
37
- #define RETURN_ERROR(...) do { \
38
- char * error; \
39
- int result = asprintf(&error, __VA_ARGS__); \
40
- return result == -1 ? new ErrorMessage("asprintf failed", false) : new ErrorMessage(error); \
41
- } while (0)
42
-
43
-
44
- static const char * no_error = nullptr;
45
-
46
- static char * allocated_drop_file = nullptr ;
47
- static char * allocated_clipboard = nullptr ;
48
- static SDL_Cursor * allocated_cursor = nullptr;
49
-
50
- struct AllocString {
51
- char * buffer;
52
- AllocString * next;
53
- };
54
- static AllocString * allocated_guid_strings = nullptr;
55
- char * _allocateString (int size) {
56
- AllocString * node = (AllocString*) malloc(sizeof(AllocString));
57
- node->buffer = (char *) malloc(size);
58
- AllocString * head = allocated_guid_strings;
59
- allocated_guid_strings = node;
60
- node->next = head;
61
- return node->buffer;
62
- }
63
-
64
- static const Uint8 * keys;
65
- static int num_keys;
66
-
67
- static char text_input[32];
68
-
69
- const static int SDL_JOYSTICK_AXIS_RANGE = SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN;
70
-
71
- double _joystickMapAxis (int value) {
72
- return (value - SDL_JOYSTICK_AXIS_MIN) / (SDL_JOYSTICK_AXIS_RANGE / 2.0) - 1;
73
- }
74
-
75
-
76
- namespace SdlHelpers {
77
-
78
-
79
- #define MAKE_VAR(target, type, value) do { \
80
- target.emplace<type>(value); \
81
- } while (false)
82
-
83
- #define MAKE_MAP(target) MAKE_VAR(target, 1, VariantMap())
84
-
85
- static Variant var;
86
-
87
- #define SET_PROP(object, prop, type, value) do { \
88
- MAKE_VAR(var, type, value); \
89
- std::get<VariantMap>(object).insert({ prop, var }); \
90
- } while (false)
91
-
92
- #define SET_NULL(object, prop) SET_PROP(object, prop, 0, std::monostate())
93
- #define SET_MAP(object, prop, val) SET_PROP(object, prop, 1, std::get<VariantMap>(val))
94
- #define SET_LIST(object, prop, val) SET_PROP(object, prop, 2, std::get<VariantList>(val))
95
- #define SET_NUM(object, prop, val) SET_PROP(object, prop, 3, val)
96
- #define SET_STRING(object, prop, val) SET_PROP(object, prop, 4, val)
97
- #define SET_BOOL(object, prop, val) SET_PROP(object, prop, 5, val)
98
-
99
- #define MAKE_LIST(target) MAKE_VAR(target, 2, VariantList())
100
-
101
- #define APPEND(list, item) do { \
102
- std::get<VariantList>(list).push_back(item); \
103
- } while (false)
104
-
105
- #define APPEND_VALUE(list, type, value) do { \
106
- MAKE_VAR(var, type, value); \
107
- APPEND(list, var); \
108
- } while (false)
109
-
110
- #define MAKE_NUM(target, value) MAKE_VAR(target, 3, value)
111
-
112
- #define MAKE_STRING(target, value) MAKE_VAR(target, 4, value)
113
-
114
- #define MAKE_BOOL(target, value) MAKE_VAR(target, 5, value)
115
-
116
-
117
- ErrorMessage::
118
- ErrorMessage(const char * _message, bool _should_dispose)
119
- : message(_message)
120
- , should_dispose(_should_dispose)
121
- {}
122
-
123
- ErrorMessage::
124
- ~ErrorMessage()
125
- {
126
- if (!this->should_dispose) { return; }
127
- free((void *) this->message);
128
- }
129
-
130
-
131
- enum class EventFamily {
132
- DISPLAY,
133
- WINDOW,
134
- KEYBOARD,
135
- MOUSE,
136
- TEXT,
137
- DROP,
138
- JOYSTICK_DEVICE,
139
- JOYSTICK,
140
- CONTROLLER,
141
- SENSOR,
142
- AUDIO_DEVICE,
143
- CLIPBOARD,
144
- };
145
-
146
- ErrorMessage *
147
- enum_getEventFamilies (Variant & families)
148
- {
149
- MAKE_MAP(families);
150
-
151
- SET_NUM(families, "display", (int) EventFamily::DISPLAY);
152
- SET_NUM(families, "window", (int) EventFamily::WINDOW);
153
- SET_NUM(families, "keyboard", (int) EventFamily::KEYBOARD);
154
- SET_NUM(families, "mouse", (int) EventFamily::MOUSE);
155
- SET_NUM(families, "text", (int) EventFamily::TEXT);
156
- SET_NUM(families, "drop", (int) EventFamily::DROP);
157
- SET_NUM(families, "joystickDevice", (int) EventFamily::JOYSTICK_DEVICE);
158
- SET_NUM(families, "joystick", (int) EventFamily::JOYSTICK);
159
- SET_NUM(families, "controller", (int) EventFamily::CONTROLLER);
160
- SET_NUM(families, "audioDevice", (int) EventFamily::AUDIO_DEVICE);
161
- SET_NUM(families, "clipboard", (int) EventFamily::CLIPBOARD);
162
-
163
- return nullptr;
164
- }
165
-
166
-
167
- enum class EventType {
168
- QUIT,
169
- DISPLAY_ADD,
170
- DISPLAY_REMOVE,
171
- DISPLAY_ORIENT,
172
- SHOW,
173
- HIDE,
174
- EXPOSE,
175
- MOVE,
176
- RESIZE,
177
- MINIMIZE,
178
- MAXIMIZE,
179
- RESTORE,
180
- FOCUS,
181
- BLUR,
182
- HOVER,
183
- LEAVE,
184
- CLOSE,
185
- DROP_BEGIN,
186
- DROP_COMPLETE,
187
- DROP_FILE,
188
- DROP_TEXT,
189
- KEY_DOWN,
190
- KEY_UP,
191
- TEXT_INPUT,
192
- MOUSE_MOVE,
193
- MOUSE_BUTTON_DOWN,
194
- MOUSE_BUTTON_UP,
195
- MOUSE_WHEEL,
196
- DEVICE_ADD,
197
- DEVICE_REMOVE,
198
- AXIS_MOTION,
199
- BALL_MOTION,
200
- BUTTON_DOWN,
201
- BUTTON_UP,
202
- HAT_MOTION,
203
- REMAP,
204
- UPDATE,
205
- };
206
-
207
- ErrorMessage *
208
- enum_getEventTypes (Variant & types)
209
- {
210
- MAKE_MAP(types);
211
-
212
- SET_NUM(types, "quit", (int) EventType::QUIT);
213
- SET_NUM(types, "displayAdd", (int) EventType::DISPLAY_ADD);
214
- SET_NUM(types, "displayRemove", (int) EventType::DISPLAY_REMOVE);
215
- SET_NUM(types, "displayOrient", (int) EventType::DISPLAY_ORIENT);
216
- SET_NUM(types, "show", (int) EventType::SHOW);
217
- SET_NUM(types, "hide", (int) EventType::HIDE);
218
- SET_NUM(types, "expose", (int) EventType::EXPOSE);
219
- SET_NUM(types, "move", (int) EventType::MOVE);
220
- SET_NUM(types, "resize", (int) EventType::RESIZE);
221
- SET_NUM(types, "minimize", (int) EventType::MINIMIZE);
222
- SET_NUM(types, "maximize", (int) EventType::MAXIMIZE);
223
- SET_NUM(types, "restore", (int) EventType::RESTORE);
224
- SET_NUM(types, "focus", (int) EventType::FOCUS);
225
- SET_NUM(types, "blur", (int) EventType::BLUR);
226
- SET_NUM(types, "hover", (int) EventType::HOVER);
227
- SET_NUM(types, "leave", (int) EventType::LEAVE);
228
- SET_NUM(types, "close", (int) EventType::CLOSE);
229
- SET_NUM(types, "dropBegin", (int) EventType::DROP_BEGIN);
230
- SET_NUM(types, "dropComplete", (int) EventType::DROP_COMPLETE);
231
- SET_NUM(types, "dropFile", (int) EventType::DROP_FILE);
232
- SET_NUM(types, "dropText", (int) EventType::DROP_TEXT);
233
- SET_NUM(types, "keyDown", (int) EventType::KEY_DOWN);
234
- SET_NUM(types, "keyUp", (int) EventType::KEY_UP);
235
- SET_NUM(types, "textInput", (int) EventType::TEXT_INPUT);
236
- SET_NUM(types, "mouseMove", (int) EventType::MOUSE_MOVE);
237
- SET_NUM(types, "mouseButtonDown", (int) EventType::MOUSE_BUTTON_DOWN);
238
- SET_NUM(types, "mouseButtonUp", (int) EventType::MOUSE_BUTTON_UP);
239
- SET_NUM(types, "mouseWheel", (int) EventType::MOUSE_WHEEL);
240
- SET_NUM(types, "deviceAdd", (int) EventType::DEVICE_ADD);
241
- SET_NUM(types, "deviceRemove", (int) EventType::DEVICE_REMOVE);
242
- SET_NUM(types, "axisMotion", (int) EventType::AXIS_MOTION);
243
- SET_NUM(types, "ballMotion", (int) EventType::BALL_MOTION);
244
- SET_NUM(types, "buttonDown", (int) EventType::BUTTON_DOWN);
245
- SET_NUM(types, "buttonUp", (int) EventType::BUTTON_UP);
246
- SET_NUM(types, "hatMotion", (int) EventType::HAT_MOTION);
247
- SET_NUM(types, "remap", (int) EventType::REMAP);
248
- SET_NUM(types, "update", (int) EventType::UPDATE);
249
-
250
- return nullptr;
251
- }
252
-
253
-
254
- ErrorMessage *
255
- enum_getPixelFormats (Variant & formats)
256
- {
257
- MAKE_MAP(formats);
258
-
259
- SET_NUM(formats, "rgb332", SDL_PIXELFORMAT_RGB332);
260
- SET_NUM(formats, "rgb444", SDL_PIXELFORMAT_RGB444);
261
- SET_NUM(formats, "rgb555", SDL_PIXELFORMAT_RGB555);
262
- SET_NUM(formats, "bgr555", SDL_PIXELFORMAT_BGR555);
263
- SET_NUM(formats, "argb4444", SDL_PIXELFORMAT_ARGB4444);
264
- SET_NUM(formats, "rgba4444", SDL_PIXELFORMAT_RGBA4444);
265
- SET_NUM(formats, "abgr4444", SDL_PIXELFORMAT_ABGR4444);
266
- SET_NUM(formats, "bgra4444", SDL_PIXELFORMAT_BGRA4444);
267
- SET_NUM(formats, "argb1555", SDL_PIXELFORMAT_ARGB1555);
268
- SET_NUM(formats, "rgba5551", SDL_PIXELFORMAT_RGBA5551);
269
- SET_NUM(formats, "abgr1555", SDL_PIXELFORMAT_ABGR1555);
270
- SET_NUM(formats, "bgra5551", SDL_PIXELFORMAT_BGRA5551);
271
- SET_NUM(formats, "rgb565", SDL_PIXELFORMAT_RGB565);
272
- SET_NUM(formats, "bgr565", SDL_PIXELFORMAT_BGR565);
273
- SET_NUM(formats, "rgb24", SDL_PIXELFORMAT_RGB24);
274
- SET_NUM(formats, "bgr24", SDL_PIXELFORMAT_BGR24);
275
- SET_NUM(formats, "rgb888", SDL_PIXELFORMAT_RGB888);
276
- SET_NUM(formats, "rgbx8888", SDL_PIXELFORMAT_RGBX8888);
277
- SET_NUM(formats, "bgr888", SDL_PIXELFORMAT_BGR888);
278
- SET_NUM(formats, "bgrx8888", SDL_PIXELFORMAT_BGRX8888);
279
- SET_NUM(formats, "argb8888", SDL_PIXELFORMAT_ARGB8888);
280
- SET_NUM(formats, "rgba8888", SDL_PIXELFORMAT_RGBA8888);
281
- SET_NUM(formats, "abgr8888", SDL_PIXELFORMAT_ABGR8888);
282
- SET_NUM(formats, "bgra8888", SDL_PIXELFORMAT_BGRA8888);
283
- SET_NUM(formats, "argb2101010", SDL_PIXELFORMAT_ARGB2101010);
284
- SET_NUM(formats, "rgba32", SDL_PIXELFORMAT_RGBA32);
285
- SET_NUM(formats, "argb32", SDL_PIXELFORMAT_ARGB32);
286
- SET_NUM(formats, "bgra32", SDL_PIXELFORMAT_BGRA32);
287
- SET_NUM(formats, "abgr32", SDL_PIXELFORMAT_ABGR32);
288
- SET_NUM(formats, "yv12", SDL_PIXELFORMAT_YV12);
289
- SET_NUM(formats, "iyuv", SDL_PIXELFORMAT_IYUV);
290
- SET_NUM(formats, "yuy2", SDL_PIXELFORMAT_YUY2);
291
- SET_NUM(formats, "uyvy", SDL_PIXELFORMAT_UYVY);
292
- SET_NUM(formats, "yvyu", SDL_PIXELFORMAT_YVYU);
293
- SET_NUM(formats, "nv12", SDL_PIXELFORMAT_NV12);
294
- SET_NUM(formats, "nv21", SDL_PIXELFORMAT_NV21);
295
-
296
- return nullptr;
297
- }
298
-
299
- ErrorMessage *
300
- enum_getAudioFormats (Variant & formats)
301
- {
302
- MAKE_MAP(formats);
303
-
304
- SET_NUM(formats, "s8", AUDIO_S8);
305
- SET_NUM(formats, "u8", AUDIO_U8);
306
- SET_NUM(formats, "s16lsb", AUDIO_S16LSB);
307
- SET_NUM(formats, "s16msb", AUDIO_S16MSB);
308
- SET_NUM(formats, "s16sys", AUDIO_S16SYS);
309
- SET_NUM(formats, "s16", AUDIO_S16);
310
- SET_NUM(formats, "u16lsb", AUDIO_U16LSB);
311
- SET_NUM(formats, "u16msb", AUDIO_U16MSB);
312
- SET_NUM(formats, "u16sys", AUDIO_U16SYS);
313
- SET_NUM(formats, "u16", AUDIO_U16);
314
- SET_NUM(formats, "s32lsb", AUDIO_S32LSB);
315
- SET_NUM(formats, "s32msb", AUDIO_S32MSB);
316
- SET_NUM(formats, "s32sys", AUDIO_S32SYS);
317
- SET_NUM(formats, "s32", AUDIO_S32);
318
- SET_NUM(formats, "f32lsb", AUDIO_F32LSB);
319
- SET_NUM(formats, "f32msb", AUDIO_F32MSB);
320
- SET_NUM(formats, "f32sys", AUDIO_F32SYS);
321
- SET_NUM(formats, "f32", AUDIO_F32);
322
-
323
- return nullptr;
324
- }
325
-
326
- ErrorMessage *
327
- enum_getScancodes (Variant & scancodes)
328
- {
329
- MAKE_MAP(scancodes);
330
-
331
- SET_NUM(scancodes, "UNKNOWN", SDL_SCANCODE_UNKNOWN);
332
- SET_NUM(scancodes, "A", SDL_SCANCODE_A);
333
- SET_NUM(scancodes, "B", SDL_SCANCODE_B);
334
- SET_NUM(scancodes, "C", SDL_SCANCODE_C);
335
- SET_NUM(scancodes, "D", SDL_SCANCODE_D);
336
- SET_NUM(scancodes, "E", SDL_SCANCODE_E);
337
- SET_NUM(scancodes, "F", SDL_SCANCODE_F);
338
- SET_NUM(scancodes, "G", SDL_SCANCODE_G);
339
- SET_NUM(scancodes, "H", SDL_SCANCODE_H);
340
- SET_NUM(scancodes, "I", SDL_SCANCODE_I);
341
- SET_NUM(scancodes, "J", SDL_SCANCODE_J);
342
- SET_NUM(scancodes, "K", SDL_SCANCODE_K);
343
- SET_NUM(scancodes, "L", SDL_SCANCODE_L);
344
- SET_NUM(scancodes, "M", SDL_SCANCODE_M);
345
- SET_NUM(scancodes, "N", SDL_SCANCODE_N);
346
- SET_NUM(scancodes, "O", SDL_SCANCODE_O);
347
- SET_NUM(scancodes, "P", SDL_SCANCODE_P);
348
- SET_NUM(scancodes, "Q", SDL_SCANCODE_Q);
349
- SET_NUM(scancodes, "R", SDL_SCANCODE_R);
350
- SET_NUM(scancodes, "S", SDL_SCANCODE_S);
351
- SET_NUM(scancodes, "T", SDL_SCANCODE_T);
352
- SET_NUM(scancodes, "U", SDL_SCANCODE_U);
353
- SET_NUM(scancodes, "V", SDL_SCANCODE_V);
354
- SET_NUM(scancodes, "W", SDL_SCANCODE_W);
355
- SET_NUM(scancodes, "X", SDL_SCANCODE_X);
356
- SET_NUM(scancodes, "Y", SDL_SCANCODE_Y);
357
- SET_NUM(scancodes, "Z", SDL_SCANCODE_Z);
358
- SET_NUM(scancodes, "1", SDL_SCANCODE_1);
359
- SET_NUM(scancodes, "2", SDL_SCANCODE_2);
360
- SET_NUM(scancodes, "3", SDL_SCANCODE_3);
361
- SET_NUM(scancodes, "4", SDL_SCANCODE_4);
362
- SET_NUM(scancodes, "5", SDL_SCANCODE_5);
363
- SET_NUM(scancodes, "6", SDL_SCANCODE_6);
364
- SET_NUM(scancodes, "7", SDL_SCANCODE_7);
365
- SET_NUM(scancodes, "8", SDL_SCANCODE_8);
366
- SET_NUM(scancodes, "9", SDL_SCANCODE_9);
367
- SET_NUM(scancodes, "0", SDL_SCANCODE_0);
368
- SET_NUM(scancodes, "RETURN", SDL_SCANCODE_RETURN);
369
- SET_NUM(scancodes, "ESCAPE", SDL_SCANCODE_ESCAPE);
370
- SET_NUM(scancodes, "BACKSPACE", SDL_SCANCODE_BACKSPACE);
371
- SET_NUM(scancodes, "TAB", SDL_SCANCODE_TAB);
372
- SET_NUM(scancodes, "SPACE", SDL_SCANCODE_SPACE);
373
- SET_NUM(scancodes, "MINUS", SDL_SCANCODE_MINUS);
374
- SET_NUM(scancodes, "EQUALS", SDL_SCANCODE_EQUALS);
375
- SET_NUM(scancodes, "LEFTBRACKET", SDL_SCANCODE_LEFTBRACKET);
376
- SET_NUM(scancodes, "RIGHTBRACKET", SDL_SCANCODE_RIGHTBRACKET);
377
- SET_NUM(scancodes, "BACKSLASH", SDL_SCANCODE_BACKSLASH);
378
- SET_NUM(scancodes, "NONUSHASH", SDL_SCANCODE_NONUSHASH);
379
- SET_NUM(scancodes, "SEMICOLON", SDL_SCANCODE_SEMICOLON);
380
- SET_NUM(scancodes, "APOSTROPHE", SDL_SCANCODE_APOSTROPHE);
381
- SET_NUM(scancodes, "GRAVE", SDL_SCANCODE_GRAVE);
382
- SET_NUM(scancodes, "COMMA", SDL_SCANCODE_COMMA);
383
- SET_NUM(scancodes, "PERIOD", SDL_SCANCODE_PERIOD);
384
- SET_NUM(scancodes, "SLASH", SDL_SCANCODE_SLASH);
385
- SET_NUM(scancodes, "CAPSLOCK", SDL_SCANCODE_CAPSLOCK);
386
- SET_NUM(scancodes, "F1", SDL_SCANCODE_F1);
387
- SET_NUM(scancodes, "F2", SDL_SCANCODE_F2);
388
- SET_NUM(scancodes, "F3", SDL_SCANCODE_F3);
389
- SET_NUM(scancodes, "F4", SDL_SCANCODE_F4);
390
- SET_NUM(scancodes, "F5", SDL_SCANCODE_F5);
391
- SET_NUM(scancodes, "F6", SDL_SCANCODE_F6);
392
- SET_NUM(scancodes, "F7", SDL_SCANCODE_F7);
393
- SET_NUM(scancodes, "F8", SDL_SCANCODE_F8);
394
- SET_NUM(scancodes, "F9", SDL_SCANCODE_F9);
395
- SET_NUM(scancodes, "F10", SDL_SCANCODE_F10);
396
- SET_NUM(scancodes, "F11", SDL_SCANCODE_F11);
397
- SET_NUM(scancodes, "F12", SDL_SCANCODE_F12);
398
- SET_NUM(scancodes, "PRINTSCREEN", SDL_SCANCODE_PRINTSCREEN);
399
- SET_NUM(scancodes, "SCROLLLOCK", SDL_SCANCODE_SCROLLLOCK);
400
- SET_NUM(scancodes, "PAUSE", SDL_SCANCODE_PAUSE);
401
- SET_NUM(scancodes, "INSERT", SDL_SCANCODE_INSERT);
402
- SET_NUM(scancodes, "HOME", SDL_SCANCODE_HOME);
403
- SET_NUM(scancodes, "PAGEUP", SDL_SCANCODE_PAGEUP);
404
- SET_NUM(scancodes, "DELETE", SDL_SCANCODE_DELETE);
405
- SET_NUM(scancodes, "END", SDL_SCANCODE_END);
406
- SET_NUM(scancodes, "PAGEDOWN", SDL_SCANCODE_PAGEDOWN);
407
- SET_NUM(scancodes, "RIGHT", SDL_SCANCODE_RIGHT);
408
- SET_NUM(scancodes, "LEFT", SDL_SCANCODE_LEFT);
409
- SET_NUM(scancodes, "DOWN", SDL_SCANCODE_DOWN);
410
- SET_NUM(scancodes, "UP", SDL_SCANCODE_UP);
411
- SET_NUM(scancodes, "NUMLOCKCLEAR", SDL_SCANCODE_NUMLOCKCLEAR);
412
- SET_NUM(scancodes, "KP_DIVIDE", SDL_SCANCODE_KP_DIVIDE);
413
- SET_NUM(scancodes, "KP_MULTIPLY", SDL_SCANCODE_KP_MULTIPLY);
414
- SET_NUM(scancodes, "KP_MINUS", SDL_SCANCODE_KP_MINUS);
415
- SET_NUM(scancodes, "KP_PLUS", SDL_SCANCODE_KP_PLUS);
416
- SET_NUM(scancodes, "KP_ENTER", SDL_SCANCODE_KP_ENTER);
417
- SET_NUM(scancodes, "KP_1", SDL_SCANCODE_KP_1);
418
- SET_NUM(scancodes, "KP_2", SDL_SCANCODE_KP_2);
419
- SET_NUM(scancodes, "KP_3", SDL_SCANCODE_KP_3);
420
- SET_NUM(scancodes, "KP_4", SDL_SCANCODE_KP_4);
421
- SET_NUM(scancodes, "KP_5", SDL_SCANCODE_KP_5);
422
- SET_NUM(scancodes, "KP_6", SDL_SCANCODE_KP_6);
423
- SET_NUM(scancodes, "KP_7", SDL_SCANCODE_KP_7);
424
- SET_NUM(scancodes, "KP_8", SDL_SCANCODE_KP_8);
425
- SET_NUM(scancodes, "KP_9", SDL_SCANCODE_KP_9);
426
- SET_NUM(scancodes, "KP_0", SDL_SCANCODE_KP_0);
427
- SET_NUM(scancodes, "KP_PERIOD", SDL_SCANCODE_KP_PERIOD);
428
- SET_NUM(scancodes, "NONUSBACKSLASH", SDL_SCANCODE_NONUSBACKSLASH);
429
- SET_NUM(scancodes, "APPLICATION", SDL_SCANCODE_APPLICATION);
430
- SET_NUM(scancodes, "POWER", SDL_SCANCODE_POWER);
431
- SET_NUM(scancodes, "KP_EQUALS", SDL_SCANCODE_KP_EQUALS);
432
- SET_NUM(scancodes, "F13", SDL_SCANCODE_F13);
433
- SET_NUM(scancodes, "F14", SDL_SCANCODE_F14);
434
- SET_NUM(scancodes, "F15", SDL_SCANCODE_F15);
435
- SET_NUM(scancodes, "F16", SDL_SCANCODE_F16);
436
- SET_NUM(scancodes, "F17", SDL_SCANCODE_F17);
437
- SET_NUM(scancodes, "F18", SDL_SCANCODE_F18);
438
- SET_NUM(scancodes, "F19", SDL_SCANCODE_F19);
439
- SET_NUM(scancodes, "F20", SDL_SCANCODE_F20);
440
- SET_NUM(scancodes, "F21", SDL_SCANCODE_F21);
441
- SET_NUM(scancodes, "F22", SDL_SCANCODE_F22);
442
- SET_NUM(scancodes, "F23", SDL_SCANCODE_F23);
443
- SET_NUM(scancodes, "F24", SDL_SCANCODE_F24);
444
- SET_NUM(scancodes, "EXECUTE", SDL_SCANCODE_EXECUTE);
445
- SET_NUM(scancodes, "HELP", SDL_SCANCODE_HELP);
446
- SET_NUM(scancodes, "MENU", SDL_SCANCODE_MENU);
447
- SET_NUM(scancodes, "SELECT", SDL_SCANCODE_SELECT);
448
- SET_NUM(scancodes, "STOP", SDL_SCANCODE_STOP);
449
- SET_NUM(scancodes, "AGAIN", SDL_SCANCODE_AGAIN);
450
- SET_NUM(scancodes, "UNDO", SDL_SCANCODE_UNDO);
451
- SET_NUM(scancodes, "CUT", SDL_SCANCODE_CUT);
452
- SET_NUM(scancodes, "COPY", SDL_SCANCODE_COPY);
453
- SET_NUM(scancodes, "PASTE", SDL_SCANCODE_PASTE);
454
- SET_NUM(scancodes, "FIND", SDL_SCANCODE_FIND);
455
- SET_NUM(scancodes, "MUTE", SDL_SCANCODE_MUTE);
456
- SET_NUM(scancodes, "VOLUMEUP", SDL_SCANCODE_VOLUMEUP);
457
- SET_NUM(scancodes, "VOLUMEDOWN", SDL_SCANCODE_VOLUMEDOWN);
458
- SET_NUM(scancodes, "KP_COMMA", SDL_SCANCODE_KP_COMMA);
459
- SET_NUM(scancodes, "KP_EQUALSAS400", SDL_SCANCODE_KP_EQUALSAS400);
460
- SET_NUM(scancodes, "INTERNATIONAL1", SDL_SCANCODE_INTERNATIONAL1);
461
- SET_NUM(scancodes, "INTERNATIONAL2", SDL_SCANCODE_INTERNATIONAL2);
462
- SET_NUM(scancodes, "INTERNATIONAL3", SDL_SCANCODE_INTERNATIONAL3);
463
- SET_NUM(scancodes, "INTERNATIONAL4", SDL_SCANCODE_INTERNATIONAL4);
464
- SET_NUM(scancodes, "INTERNATIONAL5", SDL_SCANCODE_INTERNATIONAL5);
465
- SET_NUM(scancodes, "INTERNATIONAL6", SDL_SCANCODE_INTERNATIONAL6);
466
- SET_NUM(scancodes, "INTERNATIONAL7", SDL_SCANCODE_INTERNATIONAL7);
467
- SET_NUM(scancodes, "INTERNATIONAL8", SDL_SCANCODE_INTERNATIONAL8);
468
- SET_NUM(scancodes, "INTERNATIONAL9", SDL_SCANCODE_INTERNATIONAL9);
469
- SET_NUM(scancodes, "LANG1", SDL_SCANCODE_LANG1);
470
- SET_NUM(scancodes, "LANG2", SDL_SCANCODE_LANG2);
471
- SET_NUM(scancodes, "LANG3", SDL_SCANCODE_LANG3);
472
- SET_NUM(scancodes, "LANG4", SDL_SCANCODE_LANG4);
473
- SET_NUM(scancodes, "LANG5", SDL_SCANCODE_LANG5);
474
- SET_NUM(scancodes, "LANG6", SDL_SCANCODE_LANG6);
475
- SET_NUM(scancodes, "LANG7", SDL_SCANCODE_LANG7);
476
- SET_NUM(scancodes, "LANG8", SDL_SCANCODE_LANG8);
477
- SET_NUM(scancodes, "LANG9", SDL_SCANCODE_LANG9);
478
- SET_NUM(scancodes, "ALTERASE", SDL_SCANCODE_ALTERASE);
479
- SET_NUM(scancodes, "SYSREQ", SDL_SCANCODE_SYSREQ);
480
- SET_NUM(scancodes, "CANCEL", SDL_SCANCODE_CANCEL);
481
- SET_NUM(scancodes, "CLEAR", SDL_SCANCODE_CLEAR);
482
- SET_NUM(scancodes, "PRIOR", SDL_SCANCODE_PRIOR);
483
- SET_NUM(scancodes, "RETURN2", SDL_SCANCODE_RETURN2);
484
- SET_NUM(scancodes, "SEPARATOR", SDL_SCANCODE_SEPARATOR);
485
- SET_NUM(scancodes, "OUT", SDL_SCANCODE_OUT);
486
- SET_NUM(scancodes, "OPER", SDL_SCANCODE_OPER);
487
- SET_NUM(scancodes, "CLEARAGAIN", SDL_SCANCODE_CLEARAGAIN);
488
- SET_NUM(scancodes, "CRSEL", SDL_SCANCODE_CRSEL);
489
- SET_NUM(scancodes, "EXSEL", SDL_SCANCODE_EXSEL);
490
- SET_NUM(scancodes, "KP_00", SDL_SCANCODE_KP_00);
491
- SET_NUM(scancodes, "KP_000", SDL_SCANCODE_KP_000);
492
- SET_NUM(scancodes, "THOUSANDSSEPARATOR", SDL_SCANCODE_THOUSANDSSEPARATOR);
493
- SET_NUM(scancodes, "DECIMALSEPARATOR", SDL_SCANCODE_DECIMALSEPARATOR);
494
- SET_NUM(scancodes, "CURRENCYUNIT", SDL_SCANCODE_CURRENCYUNIT);
495
- SET_NUM(scancodes, "CURRENCYSUBUNIT", SDL_SCANCODE_CURRENCYSUBUNIT);
496
- SET_NUM(scancodes, "KP_LEFTPAREN", SDL_SCANCODE_KP_LEFTPAREN);
497
- SET_NUM(scancodes, "KP_RIGHTPAREN", SDL_SCANCODE_KP_RIGHTPAREN);
498
- SET_NUM(scancodes, "KP_LEFTBRACE", SDL_SCANCODE_KP_LEFTBRACE);
499
- SET_NUM(scancodes, "KP_RIGHTBRACE", SDL_SCANCODE_KP_RIGHTBRACE);
500
- SET_NUM(scancodes, "KP_TAB", SDL_SCANCODE_KP_TAB);
501
- SET_NUM(scancodes, "KP_BACKSPACE", SDL_SCANCODE_KP_BACKSPACE);
502
- SET_NUM(scancodes, "KP_A", SDL_SCANCODE_KP_A);
503
- SET_NUM(scancodes, "KP_B", SDL_SCANCODE_KP_B);
504
- SET_NUM(scancodes, "KP_C", SDL_SCANCODE_KP_C);
505
- SET_NUM(scancodes, "KP_D", SDL_SCANCODE_KP_D);
506
- SET_NUM(scancodes, "KP_E", SDL_SCANCODE_KP_E);
507
- SET_NUM(scancodes, "KP_F", SDL_SCANCODE_KP_F);
508
- SET_NUM(scancodes, "KP_XOR", SDL_SCANCODE_KP_XOR);
509
- SET_NUM(scancodes, "KP_POWER", SDL_SCANCODE_KP_POWER);
510
- SET_NUM(scancodes, "KP_PERCENT", SDL_SCANCODE_KP_PERCENT);
511
- SET_NUM(scancodes, "KP_LESS", SDL_SCANCODE_KP_LESS);
512
- SET_NUM(scancodes, "KP_GREATER", SDL_SCANCODE_KP_GREATER);
513
- SET_NUM(scancodes, "KP_AMPERSAND", SDL_SCANCODE_KP_AMPERSAND);
514
- SET_NUM(scancodes, "KP_DBLAMPERSAND", SDL_SCANCODE_KP_DBLAMPERSAND);
515
- SET_NUM(scancodes, "KP_VERTICALBAR", SDL_SCANCODE_KP_VERTICALBAR);
516
- SET_NUM(scancodes, "KP_DBLVERTICALBAR", SDL_SCANCODE_KP_DBLVERTICALBAR);
517
- SET_NUM(scancodes, "KP_COLON", SDL_SCANCODE_KP_COLON);
518
- SET_NUM(scancodes, "KP_HASH", SDL_SCANCODE_KP_HASH);
519
- SET_NUM(scancodes, "KP_SPACE", SDL_SCANCODE_KP_SPACE);
520
- SET_NUM(scancodes, "KP_AT", SDL_SCANCODE_KP_AT);
521
- SET_NUM(scancodes, "KP_EXCLAM", SDL_SCANCODE_KP_EXCLAM);
522
- SET_NUM(scancodes, "KP_MEMSTORE", SDL_SCANCODE_KP_MEMSTORE);
523
- SET_NUM(scancodes, "KP_MEMRECALL", SDL_SCANCODE_KP_MEMRECALL);
524
- SET_NUM(scancodes, "KP_MEMCLEAR", SDL_SCANCODE_KP_MEMCLEAR);
525
- SET_NUM(scancodes, "KP_MEMADD", SDL_SCANCODE_KP_MEMADD);
526
- SET_NUM(scancodes, "KP_MEMSUBTRACT", SDL_SCANCODE_KP_MEMSUBTRACT);
527
- SET_NUM(scancodes, "KP_MEMMULTIPLY", SDL_SCANCODE_KP_MEMMULTIPLY);
528
- SET_NUM(scancodes, "KP_MEMDIVIDE", SDL_SCANCODE_KP_MEMDIVIDE);
529
- SET_NUM(scancodes, "KP_PLUSMINUS", SDL_SCANCODE_KP_PLUSMINUS);
530
- SET_NUM(scancodes, "KP_CLEAR", SDL_SCANCODE_KP_CLEAR);
531
- SET_NUM(scancodes, "KP_CLEARENTRY", SDL_SCANCODE_KP_CLEARENTRY);
532
- SET_NUM(scancodes, "KP_BINARY", SDL_SCANCODE_KP_BINARY);
533
- SET_NUM(scancodes, "KP_OCTAL", SDL_SCANCODE_KP_OCTAL);
534
- SET_NUM(scancodes, "KP_DECIMAL", SDL_SCANCODE_KP_DECIMAL);
535
- SET_NUM(scancodes, "KP_HEXADECIMAL", SDL_SCANCODE_KP_HEXADECIMAL);
536
- SET_NUM(scancodes, "LCTRL", SDL_SCANCODE_LCTRL);
537
- SET_NUM(scancodes, "LSHIFT", SDL_SCANCODE_LSHIFT);
538
- SET_NUM(scancodes, "LALT", SDL_SCANCODE_LALT);
539
- SET_NUM(scancodes, "LGUI", SDL_SCANCODE_LGUI);
540
- SET_NUM(scancodes, "RCTRL", SDL_SCANCODE_RCTRL);
541
- SET_NUM(scancodes, "RSHIFT", SDL_SCANCODE_RSHIFT);
542
- SET_NUM(scancodes, "RALT", SDL_SCANCODE_RALT);
543
- SET_NUM(scancodes, "RGUI", SDL_SCANCODE_RGUI);
544
- SET_NUM(scancodes, "MODE", SDL_SCANCODE_MODE);
545
- SET_NUM(scancodes, "AUDIONEXT", SDL_SCANCODE_AUDIONEXT);
546
- SET_NUM(scancodes, "AUDIOPREV", SDL_SCANCODE_AUDIOPREV);
547
- SET_NUM(scancodes, "AUDIOSTOP", SDL_SCANCODE_AUDIOSTOP);
548
- SET_NUM(scancodes, "AUDIOPLAY", SDL_SCANCODE_AUDIOPLAY);
549
- SET_NUM(scancodes, "AUDIOMUTE", SDL_SCANCODE_AUDIOMUTE);
550
- SET_NUM(scancodes, "MEDIASELECT", SDL_SCANCODE_MEDIASELECT);
551
- SET_NUM(scancodes, "WWW", SDL_SCANCODE_WWW);
552
- SET_NUM(scancodes, "MAIL", SDL_SCANCODE_MAIL);
553
- SET_NUM(scancodes, "CALCULATOR", SDL_SCANCODE_CALCULATOR);
554
- SET_NUM(scancodes, "COMPUTER", SDL_SCANCODE_COMPUTER);
555
- SET_NUM(scancodes, "AC_SEARCH", SDL_SCANCODE_AC_SEARCH);
556
- SET_NUM(scancodes, "AC_HOME", SDL_SCANCODE_AC_HOME);
557
- SET_NUM(scancodes, "AC_BACK", SDL_SCANCODE_AC_BACK);
558
- SET_NUM(scancodes, "AC_FORWARD", SDL_SCANCODE_AC_FORWARD);
559
- SET_NUM(scancodes, "AC_STOP", SDL_SCANCODE_AC_STOP);
560
- SET_NUM(scancodes, "AC_REFRESH", SDL_SCANCODE_AC_REFRESH);
561
- SET_NUM(scancodes, "AC_BOOKMARKS", SDL_SCANCODE_AC_BOOKMARKS);
562
- SET_NUM(scancodes, "BRIGHTNESSDOWN", SDL_SCANCODE_BRIGHTNESSDOWN);
563
- SET_NUM(scancodes, "BRIGHTNESSUP", SDL_SCANCODE_BRIGHTNESSUP);
564
- SET_NUM(scancodes, "DISPLAYSWITCH", SDL_SCANCODE_DISPLAYSWITCH);
565
- SET_NUM(scancodes, "KBDILLUMTOGGLE", SDL_SCANCODE_KBDILLUMTOGGLE);
566
- SET_NUM(scancodes, "KBDILLUMDOWN", SDL_SCANCODE_KBDILLUMDOWN);
567
- SET_NUM(scancodes, "KBDILLUMUP", SDL_SCANCODE_KBDILLUMUP);
568
- SET_NUM(scancodes, "EJECT", SDL_SCANCODE_EJECT);
569
- SET_NUM(scancodes, "SLEEP", SDL_SCANCODE_SLEEP);
570
- SET_NUM(scancodes, "APP1", SDL_SCANCODE_APP1);
571
- SET_NUM(scancodes, "APP2", SDL_SCANCODE_APP2);
572
- SET_NUM(scancodes, "AUDIOREWIND", SDL_SCANCODE_AUDIOREWIND);
573
- SET_NUM(scancodes, "AUDIOFASTFORWARD", SDL_SCANCODE_AUDIOFASTFORWARD);
574
-
575
- return nullptr;
576
- }
577
-
578
- ErrorMessage *
579
- enum_getMouseButtons (Variant & buttons)
580
- {
581
- MAKE_MAP(buttons);
582
-
583
- SET_NUM(buttons, "LEFT", SDL_BUTTON_LEFT);
584
- SET_NUM(buttons, "MIDDLE", SDL_BUTTON_MIDDLE);
585
- SET_NUM(buttons, "RIGHT", SDL_BUTTON_RIGHT);
586
-
587
- return nullptr;
588
- }
589
-
590
- ErrorMessage *
591
- enum_getCursors (Variant & cursors)
592
- {
593
- MAKE_MAP(cursors);
594
-
595
- SET_NUM(cursors, "arrow", SDL_SYSTEM_CURSOR_ARROW);
596
- SET_NUM(cursors, "ibeam", SDL_SYSTEM_CURSOR_IBEAM);
597
- SET_NUM(cursors, "wait", SDL_SYSTEM_CURSOR_WAIT);
598
- SET_NUM(cursors, "crosshair", SDL_SYSTEM_CURSOR_CROSSHAIR);
599
- SET_NUM(cursors, "waitarrow", SDL_SYSTEM_CURSOR_WAITARROW);
600
- SET_NUM(cursors, "sizenwse", SDL_SYSTEM_CURSOR_SIZENWSE);
601
- SET_NUM(cursors, "sizenesw", SDL_SYSTEM_CURSOR_SIZENESW);
602
- SET_NUM(cursors, "sizewe", SDL_SYSTEM_CURSOR_SIZEWE);
603
- SET_NUM(cursors, "sizens", SDL_SYSTEM_CURSOR_SIZENS);
604
- SET_NUM(cursors, "sizeall", SDL_SYSTEM_CURSOR_SIZEALL);
605
- SET_NUM(cursors, "no", SDL_SYSTEM_CURSOR_NO);
606
- SET_NUM(cursors, "hand", SDL_SYSTEM_CURSOR_HAND);
607
-
608
- return nullptr;
609
- }
610
-
611
- ErrorMessage *
612
- enum_getHatPositions (Variant & positions)
613
- {
614
- MAKE_MAP(positions);
615
-
616
- SET_NUM(positions, "centered", SDL_HAT_CENTERED);
617
- SET_NUM(positions, "up", SDL_HAT_UP);
618
- SET_NUM(positions, "right", SDL_HAT_RIGHT);
619
- SET_NUM(positions, "down", SDL_HAT_DOWN);
620
- SET_NUM(positions, "left", SDL_HAT_LEFT);
621
- SET_NUM(positions, "rightup", SDL_HAT_RIGHTUP);
622
- SET_NUM(positions, "rightdown", SDL_HAT_RIGHTDOWN);
623
- SET_NUM(positions, "leftup", SDL_HAT_LEFTUP);
624
- SET_NUM(positions, "leftdown", SDL_HAT_LEFTDOWN);
625
-
626
- return nullptr;
627
- }
628
-
629
- ErrorMessage *
630
- enum_getPowerLevels (Variant & levels)
631
- {
632
- MAKE_MAP(levels);
633
-
634
- SET_NUM(levels, "unknown", SDL_JOYSTICK_POWER_UNKNOWN);
635
- SET_NUM(levels, "empty", SDL_JOYSTICK_POWER_EMPTY);
636
- SET_NUM(levels, "low", SDL_JOYSTICK_POWER_LOW);
637
- SET_NUM(levels, "medium", SDL_JOYSTICK_POWER_MEDIUM);
638
- SET_NUM(levels, "full", SDL_JOYSTICK_POWER_FULL);
639
- SET_NUM(levels, "wired", SDL_JOYSTICK_POWER_WIRED);
640
- SET_NUM(levels, "max", SDL_JOYSTICK_POWER_MAX);
641
-
642
- return nullptr;
643
- }
644
-
645
- ErrorMessage *
646
- enum_getJoystickTypes (Variant & types)
647
- {
648
- MAKE_MAP(types);
649
-
650
- SET_NUM(types, "unknown", SDL_JOYSTICK_TYPE_UNKNOWN);
651
- SET_NUM(types, "gamecontroller", SDL_JOYSTICK_TYPE_GAMECONTROLLER);
652
- SET_NUM(types, "wheel", SDL_JOYSTICK_TYPE_WHEEL);
653
- SET_NUM(types, "arcadestick", SDL_JOYSTICK_TYPE_ARCADE_STICK);
654
- SET_NUM(types, "flightstick", SDL_JOYSTICK_TYPE_FLIGHT_STICK);
655
- SET_NUM(types, "dancepad", SDL_JOYSTICK_TYPE_DANCE_PAD);
656
- SET_NUM(types, "guitar", SDL_JOYSTICK_TYPE_GUITAR);
657
- SET_NUM(types, "drumkit", SDL_JOYSTICK_TYPE_DRUM_KIT);
658
- SET_NUM(types, "arcadepad", SDL_JOYSTICK_TYPE_ARCADE_PAD);
659
- SET_NUM(types, "throttle", SDL_JOYSTICK_TYPE_THROTTLE);
660
-
661
- return nullptr;
662
- }
663
-
664
- ErrorMessage *
665
- enum_getControllerAxes (Variant & axes)
666
- {
667
- MAKE_MAP(axes);
668
-
669
- SET_NUM(axes, "leftStickX", SDL_CONTROLLER_AXIS_LEFTX);
670
- SET_NUM(axes, "leftStickY", SDL_CONTROLLER_AXIS_LEFTY);
671
- SET_NUM(axes, "rightStickX", SDL_CONTROLLER_AXIS_RIGHTX);
672
- SET_NUM(axes, "rightStickY", SDL_CONTROLLER_AXIS_RIGHTY);
673
- SET_NUM(axes, "leftTrigger", SDL_CONTROLLER_AXIS_TRIGGERLEFT);
674
- SET_NUM(axes, "rightTrigger", SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
675
-
676
- return nullptr;
677
- }
678
-
679
- ErrorMessage *
680
- enum_getControllerButtons (Variant & buttons)
681
- {
682
- MAKE_MAP(buttons);
683
-
684
- SET_NUM(buttons, "dpadLeft", SDL_CONTROLLER_BUTTON_DPAD_LEFT);
685
- SET_NUM(buttons, "dpadRight", SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
686
- SET_NUM(buttons, "dpadUp", SDL_CONTROLLER_BUTTON_DPAD_UP);
687
- SET_NUM(buttons, "dpadDown", SDL_CONTROLLER_BUTTON_DPAD_DOWN);
688
- SET_NUM(buttons, "a", SDL_CONTROLLER_BUTTON_A);
689
- SET_NUM(buttons, "b", SDL_CONTROLLER_BUTTON_B);
690
- SET_NUM(buttons, "x", SDL_CONTROLLER_BUTTON_X);
691
- SET_NUM(buttons, "y", SDL_CONTROLLER_BUTTON_Y);
692
- SET_NUM(buttons, "guide", SDL_CONTROLLER_BUTTON_GUIDE);
693
- SET_NUM(buttons, "back", SDL_CONTROLLER_BUTTON_BACK);
694
- SET_NUM(buttons, "start", SDL_CONTROLLER_BUTTON_START);
695
- SET_NUM(buttons, "leftStick", SDL_CONTROLLER_BUTTON_LEFTSTICK);
696
- SET_NUM(buttons, "rightStick", SDL_CONTROLLER_BUTTON_RIGHTSTICK);
697
- SET_NUM(buttons, "leftShoulder", SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
698
- SET_NUM(buttons, "rightShoulder", SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
699
- SET_NUM(buttons, "paddle1", SDL_CONTROLLER_BUTTON_PADDLE1);
700
- SET_NUM(buttons, "paddle2", SDL_CONTROLLER_BUTTON_PADDLE2);
701
- SET_NUM(buttons, "paddle3", SDL_CONTROLLER_BUTTON_PADDLE3);
702
- SET_NUM(buttons, "paddle4", SDL_CONTROLLER_BUTTON_PADDLE4);
703
-
704
- return nullptr;
705
- }
706
-
707
- ErrorMessage *
708
- enum_getSensorTypes (Variant & types)
709
- {
710
- MAKE_MAP(types);
711
-
712
- SET_NUM(types, "unknown", SDL_SENSOR_UNKNOWN);
713
- SET_NUM(types, "accelerometer", SDL_SENSOR_ACCEL);
714
- SET_NUM(types, "gyroscope", SDL_SENSOR_GYRO);
715
- SET_NUM(types, "accelerometerLeft", SDL_SENSOR_ACCEL_L);
716
- SET_NUM(types, "gyroscopeLeft", SDL_SENSOR_GYRO_L);
717
- SET_NUM(types, "accelerometerRight", SDL_SENSOR_ACCEL_R);
718
- SET_NUM(types, "gyroscopeRight", SDL_SENSOR_GYRO_R);
719
-
720
- return nullptr;
721
- }
722
-
723
- ErrorMessage *
724
- enum_getPowerStates (Variant & states)
725
- {
726
- MAKE_MAP(states);
727
-
728
- SET_NUM(states, "unknown", SDL_POWERSTATE_UNKNOWN);
729
- SET_NUM(states, "noBattery", SDL_POWERSTATE_NO_BATTERY);
730
- SET_NUM(states, "battery", SDL_POWERSTATE_ON_BATTERY);
731
- SET_NUM(states, "charging", SDL_POWERSTATE_CHARGING);
732
- SET_NUM(states, "charged", SDL_POWERSTATE_CHARGED);
733
-
734
- return nullptr;
735
- }
736
-
737
-
738
-
739
- ErrorMessage *
740
- initialize (Variant & object)
741
- {
742
- // Hints
743
- {
744
- SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
745
- }
746
-
747
- MAKE_MAP(object);
748
-
749
- // Init
750
- {
751
- int subsystems = 0
752
- | SDL_INIT_EVENTS
753
- | SDL_INIT_JOYSTICK
754
- | SDL_INIT_GAMECONTROLLER
755
- | SDL_INIT_HAPTIC
756
- | SDL_INIT_SENSOR;
757
- if (SDL_Init(subsystems) != 0) {
758
- RETURN_ERROR("SDL_Init() error: %s\n", SDL_GetError());
759
- }
760
- }
761
-
762
- // Version
763
- {
764
- Variant version, compile, runtime;
765
- MAKE_MAP(version);
766
- MAKE_MAP(compile);
767
- MAKE_MAP(runtime);
768
-
769
- SDL_version compile_version;
770
- SDL_VERSION(&compile_version);
771
- SET_NUM(compile, "major", compile_version.major);
772
- SET_NUM(compile, "minor", compile_version.minor);
773
- SET_NUM(compile, "patch", compile_version.patch);
774
- SET_MAP(version, "compile", compile);
775
-
776
- SDL_version runtime_version;
777
- SDL_GetVersion(&runtime_version);
778
- SET_NUM(runtime, "major", runtime_version.major);
779
- SET_NUM(runtime, "minor", runtime_version.minor);
780
- SET_NUM(runtime, "patch", runtime_version.patch);
781
- SET_MAP(version, "runtime", runtime);
782
-
783
- SET_MAP(object, "version", version);
784
- }
785
-
786
- {
787
- const char * platform_name = SDL_GetPlatform();
788
- SET_STRING(object, "platform", platform_name);
789
- }
790
-
791
- Variant drivers, video, audio;
792
- MAKE_MAP(drivers);
793
- MAKE_MAP(video);
794
- MAKE_MAP(audio);
795
-
796
- // Available Video Drivers
797
- {
798
- Variant list;
799
- MAKE_LIST(list);
800
-
801
- int num_drivers = SDL_GetNumVideoDrivers();
802
- if (num_drivers < 0) {
803
- RETURN_ERROR("SDL_GetNumVideoDrivers() error: %s\n", SDL_GetError());
804
- }
805
-
806
- const char * driver_name;
807
- Variant name;
808
-
809
- for (int i = 0; i < num_drivers; i ++) {
810
- driver_name = SDL_GetVideoDriver(i);
811
- if(driver_name == nullptr) {
812
- RETURN_ERROR("SDL_GetVideoDriver(%d) error: %s\n", i, SDL_GetError());
813
- }
814
- MAKE_STRING(name, driver_name);
815
- APPEND(list, name);
816
- }
817
-
818
- SET_LIST(video, "all", list);
819
- }
820
-
821
- // Selected Video Driver
822
- {
823
- if (SDL_InitSubSystem(SDL_INIT_VIDEO) == 0) {
824
- const char * driver_name = SDL_GetCurrentVideoDriver();
825
- if(driver_name == nullptr) {
826
- RETURN_ERROR("SDL_GetCurrentVideoDriver() error: %s\n", SDL_GetError());
827
- }
828
-
829
- SET_STRING(video, "current", driver_name);
830
- } else {
831
- SET_NULL(video, "current");
832
- }
833
- }
834
-
835
- // Available Audio Drivers
836
- {
837
- int num_drivers = SDL_GetNumAudioDrivers();
838
- if (num_drivers < 0) {
839
- RETURN_ERROR("SDL_GetNumAudioDrivers() error: %s\n", SDL_GetError());
840
- }
841
-
842
- Variant list;
843
- MAKE_LIST(list);
844
-
845
- const char * driver_name;
846
- Variant name;
847
-
848
- for (int i = 0; i < num_drivers; i ++) {
849
- driver_name = SDL_GetAudioDriver(i);
850
- if(driver_name == nullptr) {
851
- RETURN_ERROR("SDL_GetAudioDriver(%d) error: %s\n", i, SDL_GetError());
852
- }
853
-
854
- MAKE_STRING(name, driver_name);
855
- APPEND(list, name);
856
- }
857
-
858
- SET_LIST(audio, "all", list);
859
- }
860
-
861
- // Selected Audio Driver
862
- if (SDL_InitSubSystem(SDL_INIT_AUDIO) == 0) {
863
- const char * driver_name = SDL_GetCurrentAudioDriver();
864
- if(driver_name == nullptr) {
865
- RETURN_ERROR("SDL_GetCurrentAudioDriver() error: %s\n", SDL_GetError());
866
- }
867
-
868
- SET_STRING(audio, "current", driver_name);
869
- } else {
870
- SET_NULL(audio, "current");
871
- }
872
-
873
- SET_MAP(drivers, "video", video);
874
- SET_MAP(drivers, "audio", audio);
875
- SET_MAP(object, "drivers", drivers);
876
-
877
- SDL_ClearError();
878
- no_error = SDL_GetError();
879
-
880
- keys = SDL_GetKeyboardState(&num_keys);
881
- SDL_StartTextInput();
882
-
883
-
884
- #if defined(__MACOSX__)
885
- reenableInertialScrolling();
886
- #endif
887
-
888
- return nullptr;
889
- }
890
-
891
-
892
- ErrorMessage *
893
- _windowUpdateTexture(
894
- SDL_Window * window,
895
- SDL_Renderer * renderer,
896
- int pixel_width, int pixel_height,
897
- SDL_Texture ** texture_dst
898
- ) {
899
- int window_id = SDL_GetWindowID(window);
900
-
901
- int format = SDL_GetWindowPixelFormat(window);
902
- SDL_Texture * texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, pixel_width, pixel_height);
903
- if (texture == nullptr) {
904
- RETURN_ERROR("SDL_CreateTexture(%d) error: %s\n", window_id, SDL_GetError());
905
- }
906
-
907
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
908
- RETURN_ERROR("SDL_SetTextureBlendMode(%d) error: %s\n", window_id, SDL_GetError());
909
- }
910
-
911
- SDL_Texture * old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
912
- if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
913
-
914
- if (texture_dst != nullptr) { *texture_dst = texture; }
915
-
916
- return nullptr;
917
- }
918
-
919
- ErrorMessage *
920
- _windowUpdateRenderer(
921
- SDL_Window * window,
922
- bool * accelerated,
923
- bool * vsync
924
- ) {
925
- int window_id = SDL_GetWindowID(window);
926
-
927
- SDL_Renderer * old_renderer = SDL_GetRenderer(window);
928
- if (old_renderer != nullptr) {
929
- SDL_DestroyRenderer(old_renderer);
930
- SDL_SetWindowData(window, "texture", nullptr);
931
- }
932
-
933
- int renderer_flags = 0
934
- | (*accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
935
- | (*accelerated && *vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
936
-
937
- SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, renderer_flags);
938
- if (renderer == nullptr) {
939
- RETURN_ERROR("SDL_CreateRenderer(%d, %d) error: %s\n", window_id, renderer_flags, SDL_GetError());
940
- }
941
-
942
- SDL_RendererInfo info;
943
- SDL_GetRendererInfo(renderer, &info);
944
-
945
- *accelerated = info.flags & SDL_RENDERER_ACCELERATED;
946
- *vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
947
-
948
- int pixel_width, pixel_height;
949
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
950
-
951
- ErrorMessage * error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, nullptr);
952
- if (error != nullptr) { return error; }
953
-
954
- return nullptr;
955
- }
956
-
957
-
958
- ErrorMessage *
959
- _controllerGetState (SDL_GameController * controller, Variant & object)
960
- {
961
- {
962
- Variant axes;
963
- MAKE_MAP(axes);
964
-
965
- SET_NUM(axes, "leftStickX", _joystickMapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX)));
966
- SET_NUM(axes, "leftStickY", _joystickMapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTY)));
967
- SET_NUM(axes, "rightStickX", _joystickMapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTX)));
968
- SET_NUM(axes, "rightStickY", _joystickMapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_RIGHTY)));
969
- SET_NUM(axes, "leftTrigger", _joystickMapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT)));
970
- SET_NUM(axes, "rightTrigger", _joystickMapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT)));
971
-
972
- SET_MAP(object, "axes", axes);
973
- }
974
-
975
- {
976
- Variant buttons;
977
- MAKE_MAP(buttons);
978
-
979
- SET_BOOL(buttons, "dpadLeft", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_LEFT));
980
- SET_BOOL(buttons, "dpadRight", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
981
- SET_BOOL(buttons, "dpadUp", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_UP));
982
- SET_BOOL(buttons, "dpadDown", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_DPAD_DOWN));
983
- SET_BOOL(buttons, "a", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_A));
984
- SET_BOOL(buttons, "b", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_B));
985
- SET_BOOL(buttons, "x", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_X));
986
- SET_BOOL(buttons, "y", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_Y));
987
- SET_BOOL(buttons, "guide", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_GUIDE));
988
- SET_BOOL(buttons, "back", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_BACK));
989
- SET_BOOL(buttons, "start", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_START));
990
- SET_BOOL(buttons, "leftStick", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSTICK));
991
- SET_BOOL(buttons, "rightStick", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSTICK));
992
- SET_BOOL(buttons, "leftShoulder", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
993
- SET_BOOL(buttons, "rightShoulder", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER));
994
- SET_BOOL(buttons, "paddle1", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE1));
995
- SET_BOOL(buttons, "paddle2", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE2));
996
- SET_BOOL(buttons, "paddle3", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE3));
997
- SET_BOOL(buttons, "paddle4", SDL_GameControllerGetButton(controller, SDL_CONTROLLER_BUTTON_PADDLE4));
998
-
999
- SET_MAP(object, "buttons", buttons);
1000
- }
1001
-
1002
- return nullptr;
1003
- }
1004
-
1005
-
1006
- ErrorMessage *
1007
- packageEvent (const SDL_Event & event, Variant & object)
1008
- {
1009
- if (allocated_drop_file != nullptr) {
1010
- SDL_free(allocated_drop_file);
1011
- allocated_drop_file = nullptr;
1012
- }
1013
- if (allocated_clipboard != nullptr) {
1014
- SDL_free(allocated_clipboard);
1015
- allocated_clipboard = nullptr;
1016
- }
1017
- while (allocated_guid_strings != nullptr) {
1018
- AllocString * tmp = allocated_guid_strings;
1019
- allocated_guid_strings = allocated_guid_strings->next;
1020
- free(tmp->buffer);
1021
- free(tmp);
1022
- }
1023
-
1024
- MAKE_MAP(object);
1025
-
1026
- Variant value;
1027
- switch (event.type) {
1028
- case SDL_DISPLAYEVENT: {
1029
- SET_NUM(object, "family", (int) EventFamily::DISPLAY);
1030
- SET_NUM(object, "displayIndex", event.display.display);
1031
-
1032
- switch (event.display.event) {
1033
- case SDL_DISPLAYEVENT_CONNECTED: { SET_NUM(object, "type", (int) EventType::DISPLAY_ADD); break; }
1034
- case SDL_DISPLAYEVENT_DISCONNECTED: { SET_NUM(object, "type", (int) EventType::DISPLAY_REMOVE); break; }
1035
- case SDL_DISPLAYEVENT_ORIENTATION: { SET_NUM(object, "type", (int) EventType::DISPLAY_ORIENT); break; }
1036
- }
1037
- break;
1038
- }
1039
-
1040
- case SDL_WINDOWEVENT: {
1041
- SET_NUM(object, "family", (int) EventFamily::WINDOW);
1042
- SET_NUM(object, "windowId", event.window.windowID);
1043
-
1044
- switch (event.window.event) {
1045
- case SDL_WINDOWEVENT_SHOWN: { SET_NUM(object, "type", (int) EventType::SHOW); break; }
1046
- case SDL_WINDOWEVENT_HIDDEN: { SET_NUM(object, "type", (int) EventType::HIDE); break; }
1047
- case SDL_WINDOWEVENT_EXPOSED: { SET_NUM(object, "type", (int) EventType::EXPOSE); break; }
1048
- case SDL_WINDOWEVENT_MOVED: {
1049
- SET_NUM(object, "type", (int) EventType::MOVE);
1050
- SET_NUM(object, "x", event.window.data1);
1051
- SET_NUM(object, "y", event.window.data2);
1052
- break;
1053
- }
1054
- case SDL_WINDOWEVENT_SIZE_CHANGED: {
1055
- SDL_Window * window = SDL_GetWindowFromID(event.window.windowID);
1056
- if (window == nullptr) {
1057
- RETURN_ERROR("SDL_CreateWindow() error: %s\n", SDL_GetError());
1058
- }
1059
-
1060
- int pixel_width, pixel_height;
1061
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
1062
-
1063
- SET_NUM(object, "type", (int) EventType::RESIZE);
1064
- SET_NUM(object, "width", event.window.data1);
1065
- SET_NUM(object, "height", event.window.data2);
1066
- SET_NUM(object, "pixelWidth", pixel_width);
1067
- SET_NUM(object, "pixelHeight", pixel_height);
1068
- break;
1069
- }
1070
- case SDL_WINDOWEVENT_MINIMIZED: { SET_NUM(object, "type", (int) EventType::MINIMIZE); break; }
1071
- case SDL_WINDOWEVENT_MAXIMIZED: { SET_NUM(object, "type", (int) EventType::MAXIMIZE); break; }
1072
- case SDL_WINDOWEVENT_RESTORED: { SET_NUM(object, "type", (int) EventType::RESTORE); break; }
1073
- case SDL_WINDOWEVENT_FOCUS_GAINED: { SET_NUM(object, "type", (int) EventType::FOCUS); break; }
1074
- case SDL_WINDOWEVENT_FOCUS_LOST: { SET_NUM(object, "type", (int) EventType::BLUR); break; }
1075
- case SDL_WINDOWEVENT_ENTER: { SET_NUM(object, "type", (int) EventType::HOVER); break; }
1076
- case SDL_WINDOWEVENT_LEAVE: { SET_NUM(object, "type", (int) EventType::LEAVE); break; }
1077
- case SDL_WINDOWEVENT_CLOSE: { SET_NUM(object, "type", (int) EventType::CLOSE); break; }
1078
- }
1079
- break;
1080
- }
1081
-
1082
- case SDL_DROPBEGIN:
1083
- case SDL_DROPCOMPLETE: {
1084
- SET_NUM(object, "family", (int) EventFamily::DROP);
1085
- SET_NUM(object, "type", (int) (event.type == SDL_DROPBEGIN
1086
- ? EventType::DROP_BEGIN
1087
- : EventType::DROP_COMPLETE));
1088
- SET_NUM(object, "windowId", event.drop.windowID);
1089
- break;
1090
- }
1091
- case SDL_DROPFILE:
1092
- case SDL_DROPTEXT: {
1093
- SET_NUM(object, "family", (int) EventFamily::DROP);
1094
- bool is_file = event.type == SDL_DROPFILE;
1095
- SET_NUM(object, "type", (int) (is_file
1096
- ? EventType::DROP_FILE
1097
- : EventType::DROP_TEXT));
1098
- SET_NUM(object, "windowId", event.drop.windowID);
1099
-
1100
- allocated_drop_file = event.drop.file;
1101
- SET_STRING(object, is_file ? "file" : "text", allocated_drop_file);
1102
- break;
1103
- }
1104
-
1105
- case SDL_KEYDOWN:
1106
- case SDL_KEYUP: {
1107
- SET_NUM(object, "family", (int) EventFamily::KEYBOARD);
1108
- SET_NUM(object, "type", (int) (event.type == SDL_KEYDOWN
1109
- ? EventType::KEY_DOWN
1110
- : EventType::KEY_UP));
1111
- SET_NUM(object, "windowId", event.key.windowID);
1112
-
1113
- SDL_Keysym symbol = event.key.keysym;
1114
- SET_NUM(object, "scancode", symbol.scancode);
1115
-
1116
- const char * name = SDL_GetKeyName(symbol.sym);
1117
- if (name[0] != '\0') {
1118
- SET_STRING(object, "key", name);
1119
- }
1120
- else {
1121
- SET_NULL(object, "key");
1122
- }
1123
-
1124
- if (event.type == SDL_KEYDOWN) {
1125
- SET_BOOL(object, "repeat", !!event.key.repeat);
1126
- }
1127
-
1128
- SET_BOOL(object, "alt", !!(symbol.mod & KMOD_ALT));
1129
- SET_BOOL(object, "ctrl", !!(symbol.mod & KMOD_CTRL));
1130
- SET_BOOL(object, "shift", !!(symbol.mod & KMOD_SHIFT));
1131
- SET_BOOL(object, "super", !!(symbol.mod & KMOD_GUI));
1132
- SET_BOOL(object, "altgr", !!(symbol.mod & KMOD_MODE));
1133
- SET_BOOL(object, "numlock", !!(symbol.mod & KMOD_NUM));
1134
- SET_BOOL(object, "capslock", !!(symbol.mod & KMOD_CAPS));
1135
- break;
1136
- }
1137
-
1138
- case SDL_TEXTINPUT: {
1139
- SET_NUM(object, "family", (int) EventFamily::TEXT);
1140
- SET_NUM(object, "type", (int) EventType::TEXT_INPUT);
1141
- SET_NUM(object, "windowId", event.text.windowID);
1142
- strcpy(text_input, event.text.text);
1143
- SET_STRING(object, "text", text_input);
1144
- break ;
1145
- }
1146
-
1147
- case SDL_MOUSEMOTION: {
1148
- SET_NUM(object, "family", (int) EventFamily::MOUSE);
1149
- SET_NUM(object, "type", (int) EventType::MOUSE_MOVE);
1150
- SET_NUM(object, "windowId", event.motion.windowID);
1151
- SET_BOOL(object, "touch", event.motion.which == SDL_TOUCH_MOUSEID);
1152
- SET_NUM(object, "x", event.motion.x);
1153
- SET_NUM(object, "y", event.motion.y);
1154
- break;
1155
- }
1156
- case SDL_MOUSEBUTTONDOWN:
1157
- case SDL_MOUSEBUTTONUP: {
1158
- SET_NUM(object, "family", (int) EventFamily::MOUSE);
1159
- SET_NUM(object, "type", (int) (event.type == SDL_MOUSEBUTTONDOWN
1160
- ? EventType::MOUSE_BUTTON_DOWN
1161
- : EventType::MOUSE_BUTTON_UP));
1162
- SET_NUM(object, "windowId", event.button.windowID);
1163
- SET_BOOL(object, "touch", event.button.which == SDL_TOUCH_MOUSEID);
1164
- SET_NUM(object, "button", event.button.button);
1165
- SET_NUM(object, "x", event.button.x);
1166
- SET_NUM(object, "y", event.button.y);
1167
- break;
1168
- }
1169
- case SDL_MOUSEWHEEL: {
1170
- SET_NUM(object, "family", (int) EventFamily::MOUSE);
1171
- SET_NUM(object, "type", (int) EventType::MOUSE_WHEEL);
1172
- SET_NUM(object, "windowId", event.wheel.windowID);
1173
- SET_BOOL(object, "touch", event.wheel.which == SDL_TOUCH_MOUSEID);
1174
-
1175
- int x, y;
1176
- SDL_GetMouseState(&x, &y);
1177
- SET_NUM(object, "x", x);
1178
- SET_NUM(object, "y", y);
1179
-
1180
- SET_NUM(object, "dx", event.wheel.x);
1181
- SET_NUM(object, "dy", event.wheel.y);
1182
- SET_BOOL(object, "flipped", event.wheel.direction);
1183
- break;
1184
- }
1185
-
1186
- case SDL_JOYDEVICEADDED: {
1187
- SET_NUM(object, "family", (int) EventFamily::JOYSTICK_DEVICE);
1188
- SET_NUM(object, "type", (int) EventType::DEVICE_ADD);
1189
-
1190
- Variant devices;
1191
- ErrorMessage * error = joystick_getDevices(devices);
1192
- if (error != nullptr) { return error; }
1193
- SET_LIST(object, "devices", devices);
1194
- break;
1195
- }
1196
- case SDL_JOYDEVICEREMOVED: {
1197
- SET_NUM(object, "family", (int) EventFamily::JOYSTICK_DEVICE);
1198
- SET_NUM(object, "type", (int) EventType::DEVICE_REMOVE);
1199
- SET_NUM(object, "joystickId", event.jdevice.which);
1200
-
1201
- Variant devices;
1202
- ErrorMessage * error = joystick_getDevices(devices);
1203
- if (error != nullptr) { return error; }
1204
- SET_LIST(object, "devices", devices);
1205
- break;
1206
- }
1207
-
1208
- case SDL_JOYAXISMOTION: {
1209
- SET_NUM(object, "family", (int) EventFamily::JOYSTICK);
1210
- SET_NUM(object, "type", (int) EventType::AXIS_MOTION);
1211
- SET_NUM(object, "joystickId", event.jaxis.which);
1212
- SET_NUM(object, "axis", event.jaxis.axis);
1213
- SET_NUM(object, "value", _joystickMapAxis(event.jaxis.value));
1214
- break ;
1215
- }
1216
- case SDL_JOYBALLMOTION: {
1217
- SET_NUM(object, "family", (int) EventFamily::JOYSTICK);
1218
- SET_NUM(object, "type", (int) EventType::BALL_MOTION);
1219
- SET_NUM(object, "joystickId", event.jball.which);
1220
- SET_NUM(object, "ball", event.jball.ball);
1221
- SET_NUM(object, "x", event.jball.xrel);
1222
- SET_NUM(object, "y", event.jball.yrel);
1223
- break ;
1224
- }
1225
- case SDL_JOYBUTTONDOWN:
1226
- case SDL_JOYBUTTONUP: {
1227
- SET_NUM(object, "family", (int) EventFamily::JOYSTICK);
1228
- SET_NUM(object, "type", (int) (event.type == SDL_JOYBUTTONDOWN
1229
- ? EventType::BUTTON_DOWN
1230
- : EventType::BUTTON_UP));
1231
- SET_NUM(object, "joystickId", event.jbutton.which);
1232
- SET_NUM(object, "button", event.jbutton.button);
1233
- break ;
1234
- }
1235
- case SDL_JOYHATMOTION: {
1236
- SET_NUM(object, "family", (int) EventFamily::JOYSTICK);
1237
- SET_NUM(object, "type", (int) EventType::HAT_MOTION);
1238
- SET_NUM(object, "joystickId", event.jhat.which);
1239
- SET_NUM(object, "hat", event.jhat.hat);
1240
- SET_NUM(object, "value", event.jhat.value);
1241
- break ;
1242
- }
1243
-
1244
- case SDL_CONTROLLERDEVICEREMAPPED: {
1245
- SET_NUM(object, "family", (int) EventFamily::CONTROLLER);
1246
- SET_NUM(object, "type", (int) EventType::REMAP);
1247
- SDL_JoystickID controllerId = event.cdevice.which;
1248
- SET_NUM(object, "controllerId", controllerId);
1249
-
1250
- SDL_GameController * controller = SDL_GameControllerFromInstanceID(controllerId);
1251
- if (controller != nullptr) {
1252
- ErrorMessage * error = _controllerGetState(controller, object);
1253
- if (error != nullptr) { return error ; }
1254
- }
1255
- break;
1256
- }
1257
-
1258
- case SDL_CONTROLLERAXISMOTION: {
1259
- SET_NUM(object, "family", (int) EventFamily::CONTROLLER);
1260
- SET_NUM(object, "type", (int) EventType::AXIS_MOTION);
1261
- SET_NUM(object, "controllerId", event.caxis.which);
1262
- SET_NUM(object, "axis", event.caxis.axis);
1263
- SET_NUM(object, "value", _joystickMapAxis(event.caxis.value));
1264
- break;
1265
- }
1266
- case SDL_CONTROLLERBUTTONDOWN:
1267
- case SDL_CONTROLLERBUTTONUP: {
1268
- SET_NUM(object, "family", (int) EventFamily::CONTROLLER);
1269
- SET_NUM(object, "type", (int) (event.type == SDL_CONTROLLERBUTTONDOWN
1270
- ? EventType::BUTTON_DOWN
1271
- : EventType::BUTTON_UP));
1272
- SET_NUM(object, "controllerId", event.cbutton.which);
1273
- SET_NUM(object, "button", event.cbutton.button);
1274
- break;
1275
- }
1276
-
1277
- case SDL_SENSORUPDATE: {
1278
- SET_NUM(object, "family", (int) EventFamily::SENSOR);
1279
- SET_NUM(object, "type", (int) EventType::UPDATE);
1280
- SET_NUM(object, "sensorId", event.sensor.which);
1281
- break;
1282
- }
1283
-
1284
- case SDL_AUDIODEVICEADDED: {
1285
- SET_NUM(object, "family", (int) EventFamily::AUDIO_DEVICE);
1286
- SET_NUM(object, "type", (int) EventType::DEVICE_ADD);
1287
- bool is_capture = event.adevice.iscapture;
1288
- SET_BOOL(object, "isRecorder", is_capture);
1289
-
1290
- Variant devices;
1291
- ErrorMessage * error = audio_getDevices(is_capture, devices);
1292
- if (error != nullptr) { return error; }
1293
- SET_LIST(object, "devices", devices);
1294
- break;
1295
- }
1296
- case SDL_AUDIODEVICEREMOVED: {
1297
- SET_NUM(object, "family", (int) EventFamily::AUDIO_DEVICE);
1298
- SET_NUM(object, "type", (int) EventType::DEVICE_REMOVE);
1299
- SET_NUM(object, "audioId", event.adevice.which);
1300
- bool is_capture = event.adevice.iscapture;
1301
- SET_BOOL(object, "isRecorder", is_capture);
1302
-
1303
- Variant devices;
1304
- ErrorMessage * error = audio_getDevices(is_capture, devices);
1305
- if (error != nullptr) { return error; }
1306
- SET_LIST(object, "devices", devices);
1307
- break;
1308
- }
1309
-
1310
- case SDL_CLIPBOARDUPDATE: {
1311
- SET_NUM(object, "family", (int) EventFamily::CLIPBOARD);
1312
- SET_NUM(object, "type", (int) EventType::UPDATE);
1313
- break;
1314
- }
1315
-
1316
- // case SDL_FINGERUP:
1317
- // case SDL_FINGERDOWN: {
1318
- // SET_STRING(object, "type", event.type == SDL_FINGERUP
1319
- // ? "fingerUp"
1320
- // : "fingerDown");
1321
- // SET_NUM(object, "touch_id", event.tfinger.touchId);
1322
- // SET_NUM(object, "finger_id", event.tfinger.fingerId);
1323
- // SET_NUM(object, "pressure", event.tfinger.pressure);
1324
- // SET_NUM(object, "x", event.tfinger.x);
1325
- // SET_NUM(object, "y", event.tfinger.y);
1326
- // break;
1327
- // }
1328
- // case SDL_FINGERMOTION: {
1329
- // SET_STRING(object, "type", "fingerMotion");
1330
- // SET_NUM(object, "touch_id", event.tfinger.touchId);
1331
- // SET_NUM(object, "finger_id", event.tfinger.fingerId);
1332
- // SET_NUM(object, "pressure", event.tfinger.pressure);
1333
- // SET_NUM(object, "x", event.tfinger.x);
1334
- // SET_NUM(object, "y", event.tfinger.y);
1335
- // SET_NUM(object, "dx", event.tfinger.dx);
1336
- // SET_NUM(object, "dy", event.tfinger.dy);
1337
- // break;
1338
- // }
1339
- }
1340
- return nullptr;
1341
- }
1342
-
1343
- ErrorMessage *
1344
- events_poll (Variant & object)
1345
- {
1346
- SDL_Event event;
1347
- bool got_event = SDL_PollEvent(&event);
1348
-
1349
- if (!got_event) { return nullptr; }
1350
-
1351
- ErrorMessage * error = packageEvent(event, object);
1352
- if (error != nullptr) { return error; }
1353
-
1354
- return nullptr;
1355
- }
1356
-
1357
- ErrorMessage *
1358
- events_wait (int timeout, Variant & object)
1359
- {
1360
- SDL_Event event;
1361
-
1362
- bool got_event = SDL_WaitEventTimeout(&event, timeout);
1363
- SDL_ClearError();
1364
-
1365
- if (!got_event) { return nullptr; }
1366
-
1367
- ErrorMessage * error = packageEvent(event, object);
1368
- if (error != nullptr) { return error; }
1369
-
1370
- return nullptr;
1371
- }
1372
-
1373
-
1374
- ErrorMessage *
1375
- video_getDisplays(Variant & list)
1376
- {
1377
- int num_displays = SDL_GetNumVideoDisplays();
1378
- if (num_displays < 0) {
1379
- RETURN_ERROR("SDL_GetNumVideoDisplays() error: %s\n", SDL_GetError());
1380
- }
1381
-
1382
- const char * name;
1383
- SDL_Rect rect;
1384
- SDL_DisplayMode mode;
1385
- float ddpi;
1386
- float hdpi;
1387
- float vdpi;
1388
-
1389
- MAKE_LIST(list);
1390
-
1391
- for (int i = 0; i < num_displays; i++) {
1392
- Variant display;
1393
- MAKE_MAP(display);
1394
-
1395
- name = SDL_GetDisplayName(i);
1396
- if(name == nullptr) {
1397
- RETURN_ERROR("SDL_GetDisplayName(%d) error: %s\n", i, SDL_GetError());
1398
- }
1399
- SET_STRING(display, "name", name);
1400
-
1401
- if (SDL_GetCurrentDisplayMode(i, &mode) != 0) {
1402
- RETURN_ERROR("SDL_GetCurrentDisplayMode(%d) error: %s\n", i, SDL_GetError());
1403
- }
1404
- SET_NUM(display, "format", mode.format);
1405
- SET_NUM(display, "frequency", mode.refresh_rate);
1406
-
1407
- {
1408
- Variant geometry;
1409
- MAKE_MAP(geometry);
1410
-
1411
- if(SDL_GetDisplayBounds(i, &rect) != 0) {
1412
- RETURN_ERROR("SDL_GetDisplayBounds(%d) error: %s\n", i, SDL_GetError());
1413
- }
1414
- SET_NUM(geometry, "x", rect.x);
1415
- SET_NUM(geometry, "y", rect.y);
1416
- SET_NUM(geometry, "width", rect.w);
1417
- SET_NUM(geometry, "height", rect.h);
1418
-
1419
- SET_MAP(display, "geometry", geometry);
1420
- }
1421
-
1422
- {
1423
- Variant usable;
1424
- MAKE_MAP(usable);
1425
-
1426
- if(SDL_GetDisplayUsableBounds(i, &rect) != 0) {
1427
- RETURN_ERROR("SDL_GetDisplayUsableBounds(%d) error: %s\n", i, SDL_GetError());
1428
- }
1429
- SET_NUM(usable, "x", rect.x);
1430
- SET_NUM(usable, "y", rect.y);
1431
- SET_NUM(usable, "width", rect.w);
1432
- SET_NUM(usable, "height", rect.h);
1433
-
1434
- SET_MAP(display, "usable", usable);
1435
- }
1436
-
1437
- {
1438
- Variant dpi;
1439
- MAKE_MAP(dpi);
1440
-
1441
- if (SDL_GetDisplayDPI(i, &ddpi, &hdpi, &vdpi) != 0) {
1442
- RETURN_ERROR("SDL_GetDisplayDPI(%d) error: %s\n", i, SDL_GetError());
1443
- }
1444
- SET_NUM(dpi, "diagonal", ddpi);
1445
- SET_NUM(dpi, "horizontal", hdpi);
1446
- SET_NUM(dpi, "vertical", vdpi);
1447
-
1448
- SET_MAP(display, "dpi", dpi);
1449
- }
1450
-
1451
- APPEND(list, display);
1452
- }
1453
-
1454
- return nullptr;
1455
- }
1456
-
1457
-
1458
- ErrorMessage *
1459
- window_create (
1460
- const char * title, int display,
1461
- int ** x, int ** y, int ** width, int ** height,
1462
- int * pixel_width, int * pixel_height,
1463
- bool visible,
1464
- bool * fullscreen,
1465
- bool * resizable,
1466
- bool * borderless,
1467
- bool * always_on_top,
1468
- bool * accelerated,
1469
- bool * vsync,
1470
- bool opengl,
1471
- bool webgpu,
1472
- bool * skip_taskbar,
1473
- bool * popup_menu,
1474
- bool * tooltip,
1475
- bool * utility,
1476
- int * window_id, void ** native_pointer, int * native_size
1477
- ) {
1478
- if (*x == nullptr) {
1479
- *x = (int *) malloc(sizeof(int));
1480
- **x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
1481
- }
1482
- if (*y == nullptr) {
1483
- *y = (int *) malloc(sizeof(int));
1484
- **y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
1485
- }
1486
- if (*width == nullptr) {
1487
- *width = (int *) malloc(sizeof(int));
1488
- **width = 640;
1489
- }
1490
- if (*height == nullptr) {
1491
- *height = (int *) malloc(sizeof(int));
1492
- **height = 480;
1493
- }
1494
- int desired_flags = 0
1495
- | SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI
1496
- | (*fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0)
1497
- | (*resizable ? SDL_WINDOW_RESIZABLE : 0)
1498
- | (*borderless ? SDL_WINDOW_BORDERLESS : 0)
1499
- | (*always_on_top ? SDL_WINDOW_ALWAYS_ON_TOP : 0)
1500
- | (opengl ? SDL_WINDOW_OPENGL : 0)
1501
- | (webgpu ? GPU_WINDOW_FLAG : 0)
1502
- | (*skip_taskbar ? SDL_WINDOW_SKIP_TASKBAR : 0)
1503
- | (*popup_menu ? SDL_WINDOW_POPUP_MENU : 0)
1504
- | (*tooltip ? SDL_WINDOW_TOOLTIP : 0)
1505
- | (*utility ? SDL_WINDOW_UTILITY : 0)
1506
- ;
1507
-
1508
- SDL_Window * window = SDL_CreateWindow(title, **x, **y, **width, **height, desired_flags);
1509
- if (window == nullptr) {
1510
- RETURN_ERROR("SDL_CreateWindow() error: %s\n", SDL_GetError());
1511
- }
1512
-
1513
- *window_id = SDL_GetWindowID(window);
1514
- if (*window_id == 0) {
1515
- RETURN_ERROR("SDL_GetWindowID() error: %s\n", SDL_GetError());
1516
- }
1517
-
1518
- int actual_flags = SDL_GetWindowFlags(window);
1519
- *fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
1520
- *resizable = actual_flags & SDL_WINDOW_RESIZABLE;
1521
- *borderless = actual_flags & SDL_WINDOW_BORDERLESS;
1522
- *always_on_top = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
1523
- *skip_taskbar = actual_flags & SDL_WINDOW_SKIP_TASKBAR;
1524
- *popup_menu = actual_flags & SDL_WINDOW_POPUP_MENU;
1525
- *tooltip = actual_flags & SDL_WINDOW_TOOLTIP;
1526
- *utility = actual_flags & SDL_WINDOW_UTILITY;
1527
-
1528
- SDL_GetWindowPosition(window, *x, *y);
1529
- SDL_GetWindowSizeInPixels(window, pixel_width, pixel_height);
1530
-
1531
- if (opengl || webgpu) {
1532
- SDL_SysWMinfo info;
1533
- SDL_VERSION(&(info.version));
1534
- bool got_info = SDL_GetWindowWMInfo(window, &info);
1535
- if (!got_info) {
1536
- RETURN_ERROR("SDL_GetWindowWMInfo(%d) error: %s\n", *window_id, SDL_GetError());
1537
- }
1538
-
1539
- if (opengl) {
1540
- int size = sizeof(GL_NativeWindow);
1541
- GL_NativeWindow *pointer = (GL_NativeWindow *) malloc(size);
1542
- #if defined(__LINUX__)
1543
- *pointer = info.info.x11.window;
1544
- #elif defined(__WIN32__)
1545
- *pointer = info.info.win.window;
1546
- #elif defined(__MACOSX__)
1547
- *pointer = getCocoaGlView(info.info.cocoa.window);
1548
- #endif
1549
-
1550
- *native_pointer = pointer;
1551
- *native_size = size;
1552
- } else if (webgpu) {
1553
- int size = sizeof(GPU_NativeData);
1554
- GPU_NativeData *pointer = (GPU_NativeData *) malloc(size);
1555
-
1556
- #if defined(__LINUX__)
1557
- pointer->display = info.info.x11.display;
1558
- pointer->window = info.info.x11.window;
1559
- #elif defined(__WIN32__)
1560
- pointer->hwnd = info.info.win.window;
1561
- pointer->hinstance = info.info.win.hinstance;
1562
- #elif defined(__MACOSX__)
1563
- pointer->layer = getCocoaGpuView(info.info.cocoa.window);
1564
- #endif
1565
-
1566
- *native_pointer = pointer;
1567
- *native_size = size;
1568
- }
1569
- }
1570
- else {
1571
- ErrorMessage * error = _windowUpdateRenderer(window, accelerated, vsync);
1572
- if (error != nullptr) { return error; }
1573
- }
1574
-
1575
- if (visible) { SDL_ShowWindow(window); }
1576
-
1577
- return nullptr;
1578
- }
1579
-
1580
- ErrorMessage *
1581
- window_setTitle (int window_id, const char * title)
1582
- {
1583
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1584
- if (window == nullptr) {
1585
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1586
- }
1587
-
1588
- SDL_SetWindowTitle(window, title);
1589
-
1590
- return nullptr;
1591
- }
1592
-
1593
- ErrorMessage *
1594
- window_setPosition (int window_id, int x, int y)
1595
- {
1596
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1597
- if (window == nullptr) {
1598
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1599
- }
1600
-
1601
- SDL_SetWindowPosition(window, x, y);
1602
-
1603
- return nullptr;
1604
- }
1605
-
1606
- ErrorMessage *
1607
- window_setSize (int window_id, int width, int height, int * pixel_width, int * pixel_height)
1608
- {
1609
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1610
- if (window == nullptr) {
1611
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1612
- }
1613
-
1614
- SDL_SetWindowSize(window, width, height);
1615
-
1616
- SDL_GetWindowSizeInPixels(window, pixel_width, pixel_height);
1617
-
1618
- return nullptr;
1619
- }
1620
-
1621
- ErrorMessage *
1622
- window_setFullscreen (int window_id, bool * fullscreen)
1623
- {
1624
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1625
- if (window == nullptr) {
1626
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1627
- }
1628
-
1629
- if (SDL_SetWindowFullscreen(window, *fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) != 0) {
1630
- RETURN_ERROR("SDL_SetWindowFullscreen(%d, %d) error: %s\n", window_id, *fullscreen, SDL_GetError());
1631
- }
1632
-
1633
- int actual_flags = SDL_GetWindowFlags(window);
1634
- *fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
1635
-
1636
- return nullptr;
1637
- }
1638
-
1639
- ErrorMessage *
1640
- window_setResizable (int window_id, bool * resizable)
1641
- {
1642
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1643
- if (window == nullptr) {
1644
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1645
- }
1646
-
1647
- SDL_SetWindowResizable(window, *resizable ? SDL_TRUE : SDL_FALSE);
1648
-
1649
- int actual_flags = SDL_GetWindowFlags(window);
1650
- *resizable = actual_flags & SDL_WINDOW_RESIZABLE;
1651
-
1652
- return nullptr;
1653
- }
1654
-
1655
- ErrorMessage *
1656
- window_setBorderless (int window_id, bool * borderless)
1657
- {
1658
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1659
- if (window == nullptr) {
1660
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1661
- }
1662
-
1663
- SDL_SetWindowBordered(window, *borderless ? SDL_FALSE : SDL_TRUE);
1664
-
1665
- int actual_flags = SDL_GetWindowFlags(window);
1666
- *borderless = actual_flags & SDL_WINDOW_BORDERLESS;
1667
-
1668
- return nullptr;
1669
- }
1670
-
1671
- ErrorMessage *
1672
- window_setAcceleratedAndVsync (int window_id, bool * accelerated, bool * vsync)
1673
- {
1674
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1675
- if (window == nullptr) {
1676
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1677
- }
1678
-
1679
- ErrorMessage * error = _windowUpdateRenderer(window, accelerated, vsync);
1680
- if (error != nullptr) { return error; }
1681
-
1682
- return nullptr;
1683
- }
1684
-
1685
- ErrorMessage *
1686
- window_focus (int window_id)
1687
- {
1688
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1689
- if (window == nullptr) {
1690
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1691
- }
1692
-
1693
- SDL_RaiseWindow(window);
1694
-
1695
- return nullptr;
1696
- }
1697
-
1698
- ErrorMessage *
1699
- window_show (int window_id)
1700
- {
1701
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1702
- if (window == nullptr) {
1703
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1704
- }
1705
-
1706
- SDL_ShowWindow(window);
1707
-
1708
- return nullptr;
1709
- }
1710
-
1711
- ErrorMessage *
1712
- window_hide (int window_id)
1713
- {
1714
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1715
- if (window == nullptr) {
1716
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1717
- }
1718
-
1719
- SDL_HideWindow(window);
1720
-
1721
- return nullptr;
1722
- }
1723
-
1724
- ErrorMessage *
1725
- window_maximize (int window_id)
1726
- {
1727
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1728
- if (window == nullptr) {
1729
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1730
- }
1731
-
1732
- SDL_MaximizeWindow(window);
1733
-
1734
- return nullptr;
1735
- }
1736
-
1737
- ErrorMessage *
1738
- window_minimize (int window_id)
1739
- {
1740
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1741
- if (window == nullptr) {
1742
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1743
- }
1744
-
1745
- SDL_MinimizeWindow(window);
1746
-
1747
- return nullptr;
1748
- }
1749
-
1750
- ErrorMessage *
1751
- window_restore (int window_id)
1752
- {
1753
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1754
- if (window == nullptr) {
1755
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1756
- }
1757
-
1758
- SDL_RestoreWindow(window);
1759
-
1760
- return nullptr;
1761
- }
1762
-
1763
- ErrorMessage *
1764
- window_render (
1765
- int window_id,
1766
- int w, int h, int stride,
1767
- unsigned int format,
1768
- void * pixels
1769
- ) {
1770
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1771
- if (window == nullptr) {
1772
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1773
- }
1774
-
1775
- SDL_Renderer * renderer = SDL_GetRenderer(window);
1776
- if (renderer == nullptr) {
1777
- RETURN_ERROR("SDL_GetRenderer(%d) error: %s\n", window_id, SDL_GetError());
1778
- }
1779
-
1780
- SDL_Texture * texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
1781
- if (texture == nullptr) {
1782
- RETURN_ERROR("SDL_GetWindowData(%d) error: %s\n", window_id, SDL_GetError());
1783
- }
1784
-
1785
- unsigned int texture_format;
1786
- int texture_width, texture_height;
1787
- if (SDL_QueryTexture(texture, &texture_format, nullptr, &texture_width, &texture_height)) {
1788
- RETURN_ERROR("SDL_QueryTexture(%d) error: %s\n", window_id, SDL_GetError());
1789
- }
1790
-
1791
- int pixel_width, pixel_height;
1792
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
1793
-
1794
- if (pixel_width != texture_width || pixel_height != texture_height) {
1795
- ErrorMessage * error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, &texture);
1796
- if (error != nullptr) { return error; }
1797
- texture_width = pixel_width;
1798
- texture_height = pixel_height;
1799
- }
1800
-
1801
- SDL_Surface * src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
1802
- if (src_surface == nullptr) {
1803
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d, %d) error: %s\n", window_id, w, h, stride, format, SDL_GetError());
1804
- }
1805
-
1806
- if (SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE) != 0) {
1807
- RETURN_ERROR("SDL_SetSurfaceBlendMode(%d) error: %s\n", window_id, SDL_GetError());
1808
- }
1809
-
1810
- void *texture_pixels;
1811
- int texture_stride;
1812
- SDL_LockTexture(texture, nullptr, &texture_pixels, &texture_stride);
1813
-
1814
- SDL_Surface * dst_surface = SDL_CreateRGBSurfaceWithFormatFrom(texture_pixels, texture_width, texture_height, SDL_BITSPERPIXEL(texture_format), texture_stride, texture_format);
1815
- if (dst_surface == nullptr) {
1816
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d, %d) error: %s\n", window_id, texture_width, texture_height, texture_stride, texture_format, SDL_GetError());
1817
- }
1818
-
1819
- if (SDL_BlitScaled(src_surface, NULL, dst_surface, NULL) != 0) {
1820
- RETURN_ERROR("SDL_BlitSurface(%d) error: %s\n", window_id, SDL_GetError());
1821
- }
1822
-
1823
- SDL_UnlockTexture(texture);
1824
-
1825
- if (SDL_RenderCopy(renderer, texture, nullptr, nullptr) != 0) {
1826
- RETURN_ERROR("SDL_RenderCopy(%d) error: %s\n", window_id, SDL_GetError());
1827
- }
1828
-
1829
- SDL_RenderPresent(renderer);
1830
-
1831
- SDL_FreeSurface(src_surface);
1832
- SDL_FreeSurface(dst_surface);
1833
-
1834
- return nullptr;
1835
- }
1836
-
1837
- ErrorMessage *
1838
- window_setIcon (
1839
- int window_id,
1840
- int w, int h, int stride,
1841
- unsigned int format,
1842
- void * pixels
1843
- ) {
1844
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1845
- if (window == nullptr) {
1846
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1847
- }
1848
-
1849
- SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
1850
- if (surface == nullptr) {
1851
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d) error: %s\n", window_id, w, h, format, SDL_GetError());
1852
- }
1853
-
1854
- SDL_SetWindowIcon(window, surface);
1855
-
1856
- SDL_FreeSurface(surface);
1857
-
1858
- return nullptr;
1859
- }
1860
-
1861
- ErrorMessage *
1862
- window_flash (int window_id, int type)
1863
- {
1864
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1865
- if (window == nullptr) {
1866
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1867
- }
1868
-
1869
- SDL_FlashOperation op;
1870
- switch (type) {
1871
- case 2: op = SDL_FLASH_UNTIL_FOCUSED; break;
1872
- case 1: op = SDL_FLASH_BRIEFLY; break;
1873
- default: op = SDL_FLASH_CANCEL; break;
1874
- }
1875
-
1876
- if (SDL_FlashWindow(window, op) != 0) {
1877
- RETURN_ERROR("SDL_FlashWindow(%d, %d) error: %s\n", window_id, type, SDL_GetError());
1878
- }
1879
-
1880
- return nullptr;
1881
- }
1882
-
1883
- ErrorMessage *
1884
- window_destroy (int window_id)
1885
- {
1886
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1887
- if (window == nullptr) {
1888
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1889
- }
1890
-
1891
- SDL_Renderer * renderer = SDL_GetRenderer(window);
1892
- if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
1893
-
1894
- SDL_DestroyWindow(window);
1895
-
1896
- return nullptr;
1897
- }
1898
-
1899
-
1900
- ErrorMessage *
1901
- keyboard_getKey (int scancode, const char ** key)
1902
- {
1903
- int keycode = SDL_GetKeyFromScancode((SDL_Scancode) scancode);
1904
- *key = SDL_GetKeyName(keycode);
1905
- return nullptr;
1906
- }
1907
-
1908
- ErrorMessage *
1909
- keyboard_getScancode (const char * key, int * scancode)
1910
- {
1911
- int keycode = SDL_GetKeyFromName(key);
1912
- *scancode = keycode == SDLK_UNKNOWN ? 0 : SDL_GetScancodeFromKey((SDL_Keycode) keycode);
1913
- return nullptr;
1914
- }
1915
-
1916
- ErrorMessage *
1917
- keyboard_getState (Variant & list)
1918
- {
1919
- MAKE_LIST(list);
1920
-
1921
- Variant key;
1922
- for (int i = 0 ; i < num_keys; i++) {
1923
- MAKE_BOOL(key, keys[i]);
1924
- APPEND(list, key);
1925
- }
1926
-
1927
- return nullptr;
1928
- }
1929
-
1930
-
1931
- ErrorMessage *
1932
- mouse_getButton (int button, int * state)
1933
- {
1934
- int buttons = SDL_GetMouseState(nullptr, nullptr);
1935
- *state = buttons & SDL_BUTTON(button);
1936
-
1937
- return nullptr;
1938
- }
1939
-
1940
- ErrorMessage *
1941
- mouse_getPosition (Variant & position)
1942
- {
1943
- MAKE_MAP(position);
1944
-
1945
- int x, y;
1946
- SDL_GetGlobalMouseState(&x, &y);
1947
-
1948
- SET_NUM(position, "x", x);
1949
- SET_NUM(position, "y", y);
1950
-
1951
- return nullptr;
1952
- }
1953
-
1954
- ErrorMessage *
1955
- mouse_setPosition (int x, int y)
1956
- {
1957
- if (SDL_WarpMouseGlobal(x, y) != 0) {
1958
- RETURN_ERROR("SDL_WarpMouseGlobal(%d, %d) error: %s\n", x, y, SDL_GetError());
1959
- }
1960
-
1961
- return nullptr;
1962
- }
1963
-
1964
- ErrorMessage *
1965
- mouse_capture (bool capture)
1966
- {
1967
- SDL_CaptureMouse(capture ? SDL_TRUE : SDL_FALSE);
1968
-
1969
- return nullptr;
1970
- }
1971
-
1972
- ErrorMessage *
1973
- mouse_setCursor (int cursor_id)
1974
- {
1975
- if (allocated_cursor != nullptr) {
1976
- SDL_FreeCursor(allocated_cursor);
1977
- allocated_cursor = nullptr;
1978
- }
1979
-
1980
- allocated_cursor = SDL_CreateSystemCursor((SDL_SystemCursor) cursor_id);
1981
- if (allocated_cursor == nullptr) {
1982
- RETURN_ERROR("SDL_CreateSystemCursor(%d) error: %s\n", cursor_id, SDL_GetError());
1983
- }
1984
-
1985
- SDL_SetCursor(allocated_cursor);
1986
-
1987
- return nullptr;
1988
- }
1989
-
1990
- ErrorMessage *
1991
- mouse_setCursorImage (
1992
- int w, int h, int stride,
1993
- unsigned int format,
1994
- void * pixels,
1995
- int x, int y
1996
- ) {
1997
- SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
1998
- if (surface == nullptr) {
1999
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d) error: %s\n", w, h, format, SDL_GetError());
2000
- }
2001
-
2002
- allocated_cursor = SDL_CreateColorCursor(surface, x, y);
2003
- if (allocated_cursor == nullptr) {
2004
- RETURN_ERROR("SDL_CreateColorCursor(%d, %d) error: %s\n", x, y, SDL_GetError());
2005
- }
2006
-
2007
- SDL_FreeSurface(surface);
2008
-
2009
- SDL_SetCursor(allocated_cursor);
2010
-
2011
- return nullptr;
2012
- }
2013
-
2014
- ErrorMessage *
2015
- mouse_showCursor (bool show)
2016
- {
2017
- SDL_ShowCursor(show ? SDL_ENABLE : SDL_DISABLE);
2018
-
2019
- return nullptr;
2020
- }
2021
-
2022
-
2023
- ErrorMessage *
2024
- joystick_getDevices(Variant & list)
2025
- {
2026
- int num_devices = SDL_NumJoysticks();
2027
- if (num_devices < 0) {
2028
- RETURN_ERROR("SDL_NumJoysticks() error: %s\n", SDL_GetError());
2029
- }
2030
-
2031
- MAKE_LIST(list);
2032
-
2033
- for (int i = 0; i < num_devices; i++) {
2034
- Variant device;
2035
- MAKE_MAP(device);
2036
-
2037
- SET_NUM(device, "_index", i);
2038
-
2039
- int id = SDL_JoystickGetDeviceInstanceID(i);
2040
- SET_NUM(device, "id", id);
2041
-
2042
- int type = SDL_JoystickGetDeviceType(i);
2043
- SET_NUM(device, "type", type);
2044
-
2045
- const char * name = SDL_JoystickNameForIndex(i);
2046
- if (name == nullptr) {
2047
- RETURN_ERROR("SDL_JoystickNameForIndex(%d) error: %s\n", i, SDL_GetError());
2048
- }
2049
- SET_STRING(device, "name", name);
2050
-
2051
- const char * path = SDL_JoystickPathForIndex(i);
2052
- if (path == nullptr) {
2053
- RETURN_ERROR("SDL_JoystickPathForIndex(%d) error: %s\n", i, SDL_GetError());
2054
- }
2055
- SET_STRING(device, "path", path);
2056
-
2057
- SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(i);
2058
- char * guid_string = _allocateString(33);
2059
- SDL_JoystickGetGUIDString(guid, guid_string, 33);
2060
- SET_STRING(device, "guid", guid_string);
2061
-
2062
- int vendor = SDL_JoystickGetDeviceVendor(i);
2063
- SET_NUM(device, "vendor", vendor);
2064
-
2065
- int product = SDL_JoystickGetDeviceProduct(i);
2066
- SET_NUM(device, "product", product);
2067
-
2068
- int version = SDL_JoystickGetDeviceProductVersion(i);
2069
- SET_NUM(device, "version", version);
2070
-
2071
- int player = SDL_JoystickGetDevicePlayerIndex(i);
2072
- SET_NUM(device, "player", player);
2073
-
2074
- bool is_controller = SDL_IsGameController(i);
2075
- SET_BOOL(device, "isController", is_controller);
2076
- if (is_controller) {
2077
- const char * mapping = SDL_GameControllerMappingForDeviceIndex(i);
2078
- if (mapping != nullptr) {
2079
- SET_STRING(device, "mapping", mapping);
2080
- } else {
2081
- SET_NULL(device, "mapping");
2082
- }
2083
- }
2084
-
2085
- APPEND(list, device);
2086
- }
2087
-
2088
- return nullptr;
2089
- }
2090
-
2091
- ErrorMessage *
2092
- joystick_open (int index, Variant & object)
2093
- {
2094
- SDL_Joystick * joystick = SDL_JoystickOpen(index);
2095
- if (joystick == nullptr) {
2096
- RETURN_ERROR("SDL_JoystickOpen(%d) error: %s\n", index, SDL_GetError());
2097
- }
2098
-
2099
- MAKE_MAP(object);
2100
-
2101
- {
2102
- SDL_JoystickID joystick_id = SDL_JoystickInstanceID(joystick);
2103
- if (joystick_id < 0) {
2104
- RETURN_ERROR("SDL_JoystickInstanceID(%d) error: %s\n", index, SDL_GetError());
2105
- }
2106
- SET_NUM(object, "id", joystick_id);
2107
-
2108
- SET_NUM(object, "firmwareVersion", SDL_JoystickGetFirmwareVersion(joystick));
2109
-
2110
- const char * serial = SDL_JoystickGetSerial(joystick);
2111
- if (serial != nullptr) {
2112
- SET_STRING(object, "serialNumber", serial);
2113
- } else {
2114
- SET_NULL(object, "serialNumber");
2115
- }
2116
-
2117
- SET_BOOL(object, "hasLed", SDL_JoystickHasLED(joystick));
2118
- SET_BOOL(object, "hasRumble", SDL_JoystickHasRumble(joystick));
2119
- SET_BOOL(object, "hasRumbleTriggers", SDL_JoystickHasRumbleTriggers(joystick));
2120
- }
2121
-
2122
- {
2123
- int num_axes = SDL_JoystickNumAxes(joystick);
2124
- if (num_axes < 0) {
2125
- RETURN_ERROR("SDL_JoystickNumAxes(%d) error: %s\n", index, SDL_GetError());
2126
- }
2127
-
2128
- Variant axes;
2129
- MAKE_LIST(axes);
2130
-
2131
- for (int i = 0; i < num_axes; i++) {
2132
- Sint16 value = SDL_JoystickGetAxis(joystick, i);
2133
-
2134
- Variant axis;
2135
- MAKE_NUM(axis, _joystickMapAxis(value));
2136
- APPEND(axes, axis);
2137
- }
2138
-
2139
- SET_LIST(object, "axes", axes);
2140
- }
2141
-
2142
- {
2143
- int num_balls = SDL_JoystickNumBalls(joystick);
2144
- if (num_balls < 0) {
2145
- RETURN_ERROR("SDL_JoystickNumBalls(%d) error: %s\n", index, SDL_GetError());
2146
- }
2147
-
2148
- Variant balls;
2149
- MAKE_LIST(balls);
2150
-
2151
- for (int i = 0; i < num_balls; i++) {
2152
- int dx, dy;
2153
- if (SDL_JoystickGetBall(joystick, i, &dx, &dy) != 0) {
2154
- RETURN_ERROR("SDL_JoystickGetBall(%d, %d) error: %s\n", index, i, SDL_GetError());
2155
- }
2156
-
2157
- Variant ball;
2158
- MAKE_MAP(ball);
2159
- SET_NUM(ball, "x", dx);
2160
- SET_NUM(ball, "y", dy);
2161
- APPEND(balls, ball);
2162
- }
2163
-
2164
- SET_LIST(object, "balls", balls);
2165
- }
2166
-
2167
- {
2168
- int num_buttons = SDL_JoystickNumButtons(joystick);
2169
- if (num_buttons < 0) {
2170
- RETURN_ERROR("SDL_JoystickNumButtons(%d) error: %s\n", index, SDL_GetError());
2171
- }
2172
-
2173
- Variant buttons;
2174
- MAKE_LIST(buttons);
2175
-
2176
- for (int i = 0; i < num_buttons; i++) {
2177
- int pressed = SDL_JoystickGetButton(joystick, i);
2178
-
2179
- Variant button;
2180
- MAKE_BOOL(button, pressed);
2181
- APPEND(buttons, button);
2182
- }
2183
-
2184
- SET_LIST(object, "buttons", buttons);
2185
- }
2186
-
2187
- {
2188
- int num_hats = SDL_JoystickNumHats(joystick);
2189
- if (num_hats < 0) {
2190
- RETURN_ERROR("SDL_JoystickNumHats(%d) error: %s\n", index, SDL_GetError());
2191
- }
2192
-
2193
- Variant hats;
2194
- MAKE_LIST(hats);
2195
-
2196
- for (int i = 0; i < num_hats; i++) {
2197
- int position = SDL_JoystickGetHat(joystick, i);
2198
-
2199
- Variant hat;
2200
- MAKE_NUM(hat, position);
2201
- APPEND(hats, hat);
2202
- }
2203
-
2204
- SET_LIST(object, "hats", hats);
2205
- }
2206
-
2207
- return nullptr;
2208
- }
2209
-
2210
- ErrorMessage *
2211
- joystick_close (int joystick_id)
2212
- {
2213
- SDL_Joystick * joystick = SDL_JoystickFromInstanceID(joystick_id);
2214
- if (joystick == nullptr) {
2215
- RETURN_ERROR("SDL_JoystickFromInstanceID(%d) error: %s\n", joystick_id, SDL_GetError());
2216
- }
2217
-
2218
- SDL_JoystickClose(joystick);
2219
-
2220
- return nullptr;
2221
- }
2222
-
2223
- ErrorMessage *
2224
- joystick_getPower (int joystick_id, int * power)
2225
- {
2226
- SDL_Joystick * joystick = SDL_JoystickFromInstanceID(joystick_id);
2227
- if (joystick == nullptr) {
2228
- RETURN_ERROR("SDL_JoystickFromInstanceID(%d) error: %s\n", joystick_id, SDL_GetError());
2229
- }
2230
-
2231
- *power = SDL_JoystickCurrentPowerLevel(joystick);
2232
-
2233
- return nullptr;
2234
- }
2235
-
2236
- ErrorMessage *
2237
- joystick_rumble (
2238
- int joystick_id,
2239
- double low_freq_rumble,
2240
- double high_freq_rumble,
2241
- int duration
2242
- ) {
2243
- SDL_Joystick * joystick = SDL_JoystickFromInstanceID(joystick_id);
2244
- if (joystick == nullptr) {
2245
- RETURN_ERROR("SDL_JoystickFromInstanceID(%d) error: %s\n", joystick_id, SDL_GetError());
2246
- }
2247
-
2248
- if (SDL_JoystickRumble(joystick, low_freq_rumble * 0xFFFF, high_freq_rumble * 0xFFFF, duration) == -1) {
2249
- RETURN_ERROR("SDL_JoystickRumble(%d) error: %s\n", joystick_id, SDL_GetError());
2250
- }
2251
-
2252
- return nullptr;
2253
- }
2254
-
2255
- ErrorMessage *
2256
- joystick_setLed (int joystick_id, double red, double green, double blue)
2257
- {
2258
- SDL_Joystick * joystick = SDL_JoystickFromInstanceID(joystick_id);
2259
- if (joystick == nullptr) {
2260
- RETURN_ERROR("SDL_JoystickFromInstanceID(%d) error: %s\n", joystick_id, SDL_GetError());
2261
- }
2262
-
2263
- if (SDL_JoystickSetLED(joystick, red * 0xFF, green * 0xFF, blue * 0xFF) == -1) {
2264
- RETURN_ERROR("SDL_JoystickSetLED(%d) error: %s\n", joystick_id, SDL_GetError());
2265
- }
2266
-
2267
- return nullptr;
2268
- }
2269
-
2270
- ErrorMessage *
2271
- joystick_setPlayer (int joystick_id, int player)
2272
- {
2273
- SDL_Joystick * joystick = SDL_JoystickFromInstanceID(joystick_id);
2274
- if (joystick == nullptr) {
2275
- RETURN_ERROR("SDL_JoystickFromInstanceID(%d) error: %s\n", joystick_id, SDL_GetError());
2276
- }
2277
-
2278
- SDL_JoystickSetPlayerIndex(joystick, player);
2279
-
2280
- return nullptr;
2281
- }
2282
-
2283
- ErrorMessage *
2284
- joystick_rumbleTriggers (
2285
- int joystick_id,
2286
- double left_rumble,
2287
- double right_rumble,
2288
- int duration
2289
- ) {
2290
- SDL_Joystick * joystick = SDL_JoystickFromInstanceID(joystick_id);
2291
- if (joystick == nullptr) {
2292
- RETURN_ERROR("SDL_JoystickFromInstanceID(%d) error: %s\n", joystick_id, SDL_GetError());
2293
- }
2294
-
2295
- if (SDL_JoystickRumbleTriggers(joystick, left_rumble * 0xFFFF, right_rumble * 0xFFFF, duration) == -1) {
2296
- RETURN_ERROR("SDL_JoystickRumbleTriggers(%d) error: %s\n", joystick_id, SDL_GetError());
2297
- }
2298
-
2299
- return nullptr;
2300
- }
2301
-
2302
- ErrorMessage *
2303
- controller_open (int index, Variant & object)
2304
- {
2305
- SDL_GameController * controller = SDL_GameControllerOpen(index);
2306
- if (controller == nullptr) {
2307
- RETURN_ERROR("SDL_GameControllerOpen(%d) error: %s\n", index, SDL_GetError());
2308
- }
2309
-
2310
- MAKE_MAP(object);
2311
-
2312
- {
2313
- SDL_Joystick * joystick = SDL_GameControllerGetJoystick(controller);
2314
- if (joystick == nullptr) {
2315
- RETURN_ERROR("SDL_GameControllerGetJoystick(%d) error: %s\n", index, SDL_GetError());
2316
- }
2317
-
2318
- SDL_JoystickID joystick_id = SDL_JoystickInstanceID(joystick);
2319
- if (joystick_id < 0) {
2320
- RETURN_ERROR("SDL_JoystickInstanceID(%d) error: %s\n", index, SDL_GetError());
2321
- }
2322
- SET_NUM(object, "id", joystick_id);
2323
-
2324
- SET_NUM(object, "firmwareVersion", SDL_JoystickGetFirmwareVersion(joystick));
2325
-
2326
- const char * serial = SDL_JoystickGetSerial(joystick);
2327
- if (serial != nullptr) {
2328
- SET_STRING(object, "serialNumber", serial);
2329
- } else {
2330
- SET_NULL(object, "serialNumber");
2331
- }
2332
-
2333
- SET_BOOL(object, "hasLed", SDL_JoystickHasLED(joystick));
2334
- SET_BOOL(object, "hasRumble", SDL_JoystickHasRumble(joystick));
2335
- SET_BOOL(object, "hasRumbleTriggers", SDL_JoystickHasRumbleTriggers(joystick));
2336
- }
2337
-
2338
- ErrorMessage * error = _controllerGetState(controller, object);
2339
- if (error != nullptr) { return error; }
2340
-
2341
- return nullptr;
2342
- }
2343
-
2344
- ErrorMessage *
2345
- controller_close (int controller_id)
2346
- {
2347
- SDL_GameController * controller = SDL_GameControllerFromInstanceID(controller_id);
2348
- if (controller == nullptr) {
2349
- RETURN_ERROR("SDL_GameControllerFromInstanceID(%d) error: %s\n", controller_id, SDL_GetError());
2350
- }
2351
-
2352
- SDL_GameControllerClose(controller);
2353
-
2354
- return nullptr;
2355
- }
2356
-
2357
- ErrorMessage *
2358
- controller_addMappings (char ** mappings, int length)
2359
- {
2360
- for (int i = 0; i < length; i++) {
2361
- if (SDL_GameControllerAddMapping(mappings[i]) == -1) {
2362
- RETURN_ERROR("SDL_GameControllerAddMapping(%s) error: %s\n", mappings[i], SDL_GetError());
2363
- }
2364
- }
2365
- return nullptr;
2366
- }
2367
-
2368
-
2369
- ErrorMessage *
2370
- sensor_getDevices (Variant & list)
2371
- {
2372
- int num_devices = SDL_NumSensors();
2373
- if (num_devices < 0) {
2374
- RETURN_ERROR("SDL_NumSensors() error: %s\n", SDL_GetError());
2375
- }
2376
-
2377
- MAKE_LIST(list);
2378
-
2379
- for (int i = 0; i < num_devices; i++) {
2380
- Variant device;
2381
- MAKE_MAP(device);
2382
-
2383
- SET_NUM(device, "_index", i);
2384
-
2385
- int id = SDL_SensorGetDeviceInstanceID(i);
2386
- SET_NUM(device, "id", id);
2387
-
2388
- int type = SDL_SensorGetDeviceType(i);
2389
- SET_NUM(device, "type", type);
2390
-
2391
- const char * name = SDL_SensorGetDeviceName(i);
2392
- if (name == nullptr) {
2393
- RETURN_ERROR("SDL_SensorGetDeviceName(%d) error: %s\n", i, SDL_GetError());
2394
- }
2395
- SET_STRING(device, "name", name);
2396
-
2397
- APPEND(list, device);
2398
- }
2399
-
2400
- return nullptr;
2401
- }
2402
-
2403
- ErrorMessage *
2404
- sensor_open (int index)
2405
- {
2406
- SDL_Sensor * sensor = SDL_SensorOpen(index);
2407
- if (sensor == nullptr) {
2408
- RETURN_ERROR("SDL_SensorOpen(%d) error: %s\n", index, SDL_GetError());
2409
- }
2410
-
2411
- return nullptr;
2412
- }
2413
-
2414
- ErrorMessage *
2415
- sensor_close (int sensor_id)
2416
- {
2417
- SDL_Sensor * sensor = SDL_SensorFromInstanceID(sensor_id);
2418
- if (sensor == nullptr) {
2419
- RETURN_ERROR("SDL_SensorFromInstanceID(%d) error: %s\n", sensor_id, SDL_GetError());
2420
- }
2421
-
2422
- SDL_SensorClose(sensor);
2423
-
2424
- return nullptr;
2425
- }
2426
-
2427
- ErrorMessage *
2428
- sensor_getData (int sensor_id, Variant & object)
2429
- {
2430
- SDL_Sensor * sensor = SDL_SensorFromInstanceID(sensor_id);
2431
- if (sensor == nullptr) {
2432
- RETURN_ERROR("SDL_SensorFromInstanceID(%d) error: %s\n", sensor_id, SDL_GetError());
2433
- }
2434
-
2435
- Uint64 timestamp;
2436
- float data[3];
2437
- if (SDL_SensorGetDataWithTimestamp(sensor, &timestamp, data, 3) == -1) {
2438
- RETURN_ERROR("SDL_SensorGetDataWithTimestamp(%d) error: %s\n", sensor_id, SDL_GetError());
2439
- }
2440
-
2441
- MAKE_MAP(object);
2442
- SET_NUM(object, "timestamp", timestamp);
2443
- SET_NUM(object, "x", data[0]);
2444
- SET_NUM(object, "y", data[1]);
2445
- SET_NUM(object, "z", data[2]);
2446
-
2447
- return nullptr;
2448
- }
2449
-
2450
-
2451
-
2452
- ErrorMessage *
2453
- audio_getDevices(bool capture, Variant & list)
2454
- {
2455
- MAKE_LIST(list);
2456
-
2457
- int num_devices = SDL_GetNumAudioDevices(capture ? 1 : 0);
2458
- if (num_devices == -1) {
2459
- const char * error = SDL_GetError();
2460
- if (error != no_error) {
2461
- RETURN_ERROR("SDL_GetNumAudioDevices(%d) error: %s\n", capture, error);
2462
- }
2463
- return nullptr;
2464
- }
2465
-
2466
- for (int i = 0; i < num_devices; i++) {
2467
- Variant device;
2468
- MAKE_MAP(device);
2469
-
2470
- SET_BOOL(device, "isRecorder", capture);
2471
-
2472
- const char * name = SDL_GetAudioDeviceName(i, capture);
2473
- if (name == nullptr) {
2474
- RETURN_ERROR("SDL_GetAudioDeviceName(%d, %d) error: %s\n", i, capture, SDL_GetError());
2475
- }
2476
- SET_STRING(device, "name", name);
2477
-
2478
- APPEND(list, device);
2479
- }
2480
-
2481
- return nullptr;
2482
- }
2483
-
2484
- ErrorMessage *
2485
- audio_openDevice (
2486
- const char * name, bool capture,
2487
- int freq, int format, int channels, int samples,
2488
- int * audio_id
2489
- ) {
2490
- SDL_AudioSpec desired;
2491
- SDL_memset(&desired, 0, sizeof(desired));
2492
- desired.freq = freq;
2493
- desired.format = format;
2494
- desired.channels = channels;
2495
- desired.samples = samples;
2496
-
2497
- SDL_AudioDeviceID audio_device = SDL_OpenAudioDevice(name, capture ? 1 : 0, &desired, nullptr, 0);
2498
- if (audio_device == 0) {
2499
- RETURN_ERROR("SDL_OpenAudioDevice() error: %s\n", SDL_GetError());
2500
- }
2501
-
2502
- *audio_id = audio_device;
2503
-
2504
- return nullptr;
2505
- }
2506
-
2507
- ErrorMessage *
2508
- audio_close (int audio_id)
2509
- {
2510
- SDL_PauseAudioDevice(audio_id, 1);
2511
- SDL_CloseAudioDevice(audio_id);
2512
-
2513
- return nullptr;
2514
- }
2515
-
2516
- ErrorMessage *
2517
- audio_play (int audio_id, bool play)
2518
- {
2519
- SDL_PauseAudioDevice(audio_id, play ? 0 : 1);
2520
-
2521
- return nullptr;
2522
- }
2523
-
2524
- ErrorMessage *
2525
- audio_getQueueSize (int audio_id, unsigned int * size)
2526
- {
2527
- *size = SDL_GetQueuedAudioSize(audio_id);
2528
-
2529
- return nullptr;
2530
- }
2531
-
2532
- ErrorMessage *
2533
- audio_clearQueue (int audio_id)
2534
- {
2535
- SDL_ClearQueuedAudio(audio_id);
2536
-
2537
- return nullptr;
2538
- }
2539
-
2540
- ErrorMessage *
2541
- audio_enqueue (int audio_id, void * src, int size)
2542
- {
2543
- if (SDL_QueueAudio(audio_id, src, size) != 0) {
2544
- RETURN_ERROR("SDL_QueueAudio() error: %s\n", SDL_GetError());
2545
- }
2546
-
2547
- return nullptr;
2548
- }
2549
-
2550
- ErrorMessage *
2551
- audio_dequeue (int audio_id, void * dst, int size, int * num)
2552
- {
2553
- *num = SDL_DequeueAudio(audio_id, dst, size);
2554
-
2555
- return nullptr;
2556
- }
2557
-
2558
-
2559
- ErrorMessage *
2560
- clipboard_getText (const char ** text)
2561
- {
2562
- if (!SDL_HasClipboardText()) {
2563
- return nullptr;
2564
- }
2565
-
2566
- if (allocated_clipboard) {
2567
- SDL_free(allocated_clipboard);
2568
- allocated_clipboard = nullptr;
2569
- }
2570
-
2571
- allocated_clipboard = SDL_GetClipboardText();
2572
- if (!allocated_clipboard) {
2573
- RETURN_ERROR("SDL_GetClipboardText(NULL) error: %s\n", SDL_GetError());
2574
- }
2575
-
2576
- *text = allocated_clipboard;
2577
-
2578
- return nullptr;
2579
- }
2580
-
2581
- ErrorMessage *
2582
- clipboard_setText (const char * text)
2583
- {
2584
- if (SDL_SetClipboardText(text) != 0) {
2585
- RETURN_ERROR("SDL_SetClipboardText() error: %s\n", SDL_GetError());
2586
- }
2587
-
2588
- return nullptr;
2589
- }
2590
-
2591
-
2592
- ErrorMessage *
2593
- power_getInfo (Variant & info)
2594
- {
2595
- int seconds, percent;
2596
- SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
2597
-
2598
- MAKE_MAP(info);
2599
-
2600
- SET_NUM(info, "state", (int) state);
2601
- SET_NUM(info, "seconds", seconds);
2602
- SET_NUM(info, "percent", percent);
2603
-
2604
- return nullptr;
2605
- }
2606
-
2607
-
2608
- ErrorMessage *
2609
- cleanup ()
2610
- {
2611
- SDL_Quit();
2612
-
2613
- return nullptr;
2614
- }
2615
-
2616
-
2617
- } // namespace