1 /+
2 +            Copyright 2022 – 2023 Aya Partridge
3 +          Copyright 2018 - 2022 Michael D. Parker
4 + Distributed under the Boost Software License, Version 1.0.
5 +     (See accompanying file LICENSE_1_0.txt or copy at
6 +           http://www.boost.org/LICENSE_1_0.txt)
7 +/
8 module sdl.events;
9 
10 import bindbc.sdl.config;
11 import bindbc.sdl.codegen;
12 
13 import sdl.gesture;
14 import sdl.joystick;
15 import sdl.keyboard;
16 import sdl.keycode;
17 import sdl.stdinc;
18 import sdl.syswm;
19 import sdl.touch;
20 import sdl.video;
21 
22 enum: ubyte{
23 	SDL_RELEASED  = 0,
24 	SDL_PRESSED   = 1,
25 }
26 
27 alias SDL_EventType = uint;
28 enum: SDL_EventType{
29 	SDL_FIRSTEVENT                = 0,
30 	SDL_QUIT                      = 0x100,
31 	SDL_APP_TERMINATING           = 0x101,
32 	SDL_APP_LOWMEMORY             = 0x102,
33 	SDL_APP_WILLENTERBACKGROUND   = 0x103,
34 	SDL_APP_DIDENTERBACKGROUND    = 0x104,
35 	SDL_APP_WILLENTERFOREGROUND   = 0x105,
36 	SDL_APP_DIDENTERFOREGROUND    = 0x106,
37 	SDL_WINDOWEVENT               = 0x200,
38 	SDL_SYSWMEVENT                = 0x201,
39 	SDL_KEYDOWN                   = 0x300,
40 	SDL_KEYUP                     = 0x301,
41 	SDL_TEXTEDITING               = 0x302,
42 	SDL_TEXTINPUT                 = 0x303,
43 	SDL_MOUSEMOTION               = 0x400,
44 	SDL_MOUSEBUTTONDOWN           = 0x401,
45 	SDL_MOUSEBUTTONUP             = 0x402,
46 	SDL_MOUSEWHEEL                = 0x403,
47 	SDL_JOYAXISMOTION             = 0x600,
48 	SDL_JOYBALLMOTION             = 0x601,
49 	SDL_JOYHATMOTION              = 0x602,
50 	SDL_JOYBUTTONDOWN             = 0x603,
51 	SDL_JOYBUTTONUP               = 0x604,
52 	SDL_JOYDEVICEADDED            = 0x605,
53 	SDL_JOYDEVICEREMOVED          = 0x606,
54 	SDL_CONTROLLERAXISMOTION      = 0x650,
55 	SDL_CONTROLLERBUTTONDOWN      = 0x651,
56 	SDL_CONTROLLERBUTTONUP        = 0x652,
57 	SDL_CONTROLLERDEVICEADDED     = 0x653,
58 	SDL_CONTROLLERDEVICEREMOVED   = 0x654,
59 	SDL_CONTROLLERDEVICEREMAPPED  = 0x655,
60 	SDL_FINGERDOWN                = 0x700,
61 	SDL_FINGERUP                  = 0x701,
62 	SDL_FINGERMOTION              = 0x702,
63 	SDL_DOLLARGESTURE             = 0x800,
64 	SDL_DOLLARRECORD              = 0x801,
65 	SDL_MULTIGESTURE              = 0x802,
66 	SDL_CLIPBOARDUPDATE           = 0x900,
67 	SDL_DROPFILE                  = 0x1000,
68 	SDL_USEREVENT                 = 0x8000,
69 	SDL_LASTEVENT                 = 0xFFFF,
70 }
71 static if(sdlSupport >= SDLSupport.v2_0_1)
72 enum: SDL_EventType{
73 	SDL_RENDER_TARGETS_RESET      = 0x2000,
74 }
75 static if(sdlSupport >= SDLSupport.v2_0_4)
76 enum: SDL_EventType{
77 	SDL_KEYMAPCHANGED             = 0x304,
78 	SDL_AUDIODEVICEADDED          = 0x1100,
79 	SDL_AUDIODEVICEREMOVED        = 0x1101,
80 	SDL_RENDER_DEVICE_RESET       = 0x2001,
81 }
82 static if(sdlSupport >= SDLSupport.v2_0_5)
83 enum: SDL_EventType{
84 	SDL_DROPTEXT                  = 0x1001,
85 	SDL_DROPBEGIN                 = 0x1002,
86 	SDL_DROPCOMPLETE              = 0x1003,
87 }
88 static if(sdlSupport >= SDLSupport.v2_0_9)
89 enum: SDL_EventType{
90 	SDL_DISPLAYEVENT              = 0x150,
91 	SDL_SENSORUPDATE              = 0x1200,
92 }
93 static if(sdlSupport >= SDLSupport.v2_0_14)
94 enum: SDL_EventType{
95 	SDL_LOCALECHANGED             = 0x107,
96 	SDL_CONTROLLERTOUCHPADDOWN    = 0x656,
97 	SDL_CONTROLLERTOUCHPADMOTION  = 0x657,
98 	SDL_CONTROLLERTOUCHPADUP      = 0x658,
99 	SDL_CONTROLLERSENSORUPDATE    = 0x659,
100 }
101 static if(sdlSupport >= SDLSupport.v2_0_22)
102 enum: SDL_EventType{
103 	SDL_TEXTEDITING_EXT           = 0x305,
104 }
105 static if(sdlSupport >= SDLSupport.v2_24)
106 enum: SDL_EventType{
107 	SDL_JOYBATTERYUPDATED         = 0x607,
108 }
109 
110 struct SDL_CommonEvent{
111 	SDL_EventType type;
112 	uint timestamp;
113 }
114 
115 static if(sdlSupport >= SDLSupport.v2_0_9){
116 	struct SDL_DisplayEvent{
117 		SDL_EventType type;
118 		uint timestamp;
119 		uint display;
120 		ubyte event;
121 		ubyte padding1;
122 		ubyte padding2;
123 		ubyte padding3;
124 		int data1;
125 	}
126 }
127 
128 struct SDL_WindowEvent{
129 	SDL_EventType type;
130 	uint timestamp;
131 	uint windowID;
132 	SDL_WindowEventID event;
133 	ubyte padding1;
134 	ubyte padding2;
135 	ubyte padding3;
136 	int data1;
137 	int data2;
138 }
139 
140 struct SDL_KeyboardEvent{
141 	SDL_EventType type;
142 	uint timestamp;
143 	uint windowID;
144 	ubyte state;
145 	ubyte repeat;
146 	ubyte padding2;
147 	ubyte padding3;
148 	SDL_Keysym keysym;
149 }
150 
151 enum SDL_TEXTEDITINGEVENT_TEXT_SIZE = 32;
152 struct SDL_TextEditingEvent{
153 	SDL_EventType type;
154 	uint timestamp;
155 	uint windowID;
156 	char[SDL_TEXTEDITINGEVENT_TEXT_SIZE] text;
157 	int start;
158 	int length;
159 }
160 
161 static if(sdlSupport >= SDLSupport.v2_0_22){
162 	struct SDL_TextEditingExtEvent{
163 		SDL_EventType type;
164 		uint timestamp;
165 		uint windowID;
166 		char* text;
167 		int start;
168 		int length;
169 	}
170 }
171 
172 enum SDL_TEXTINPUTEVENT_TEXT_SIZE = 32;
173 struct SDL_TextInputEvent{
174 	SDL_EventType type;
175 	uint timestamp;
176 	uint windowID;
177 	char[SDL_TEXTINPUTEVENT_TEXT_SIZE] text;
178 }
179 
180 struct SDL_MouseMotionEvent{
181 	SDL_EventType type;
182 	uint timestamp;
183 	uint windowID;
184 	uint which;
185 	uint state;
186 	int x;
187 	int y;
188 	int xrel;
189 	int yrel;
190 }
191 
192 struct SDL_MouseButtonEvent{
193 	SDL_EventType type;
194 	uint timestamp;
195 	uint windowID;
196 	uint which;
197 	ubyte button;
198 	ubyte state;
199 	static if(sdlSupport <= SDLSupport.v2_0_0){
200 		ubyte padding1;
201 		ubyte padding2;
202 	}else{
203 		ubyte clicks;
204 		ubyte padding1;
205 	}
206 	int x;
207 	int y;
208 }
209 
210 struct SDL_MouseWheelEvent{
211 	SDL_EventType type;
212 	uint timestamp;
213 	uint windowID;
214 	uint which;
215 	int x;
216 	int y;
217 	static if(sdlSupport >= SDLSupport.v2_0_4){
218 		uint direction;
219 	}
220 	static if(sdlSupport >= SDLSupport.v2_0_18){
221 		float preciseX;
222 		float preciseY;
223 	}
224 	static if(sdlSupport >= SDLSupport.v2_26){
225 		int mouseX;
226 		int mouseY;
227 	}
228 }
229 
230 struct SDL_JoyAxisEvent{
231 	SDL_EventType type;
232 	uint timestamp;
233 	SDL_JoystickID which;
234 	ubyte axis;
235 	ubyte padding1;
236 	ubyte padding2;
237 	ubyte padding3;
238 	short value;
239 	ushort padding4;
240 }
241 
242 struct SDL_JoyBallEvent{
243 	SDL_EventType type;
244 	uint timestamp;
245 	SDL_JoystickID which;
246 	ubyte ball;
247 	ubyte padding1;
248 	ubyte padding2;
249 	ubyte padding3;
250 	short xrel;
251 	short yrel;
252 }
253 
254 struct SDL_JoyHatEvent{
255 	SDL_EventType type;
256 	uint timestamp;
257 	SDL_JoystickID which;
258 	ubyte hat;
259 	ubyte value;
260 	ubyte padding1;
261 	ubyte padding2;
262 }
263 
264 struct SDL_JoyButtonEvent{
265 	SDL_EventType type;
266 	uint timestamp;
267 	SDL_JoystickID which;
268 	ubyte button;
269 	ubyte state;
270 	ubyte padding1;
271 	ubyte padding2;
272 }
273 
274 struct SDL_JoyDeviceEvent{
275 	SDL_EventType type;
276 	uint timestamp;
277 	int which;
278 }
279 
280 static if(sdlSupport >= SDLSupport.v2_24){
281 	struct SDL_JoyBatteryEvent{
282 		SDL_EventType type;
283 		uint timestamp;
284 		SDL_JoystickID which;
285 		SDL_JoystickPowerLevel level;
286 	}
287 }
288 
289 struct SDL_ControllerAxisEvent{
290 	SDL_EventType type;
291 	uint timestamp;
292 	SDL_JoystickID which;
293 	ubyte axis;
294 	ubyte padding1;
295 	ubyte padding2;
296 	ubyte padding3;
297 	short value;
298 	ushort padding4;
299 }
300 
301 struct SDL_ControllerButtonEvent{
302 	SDL_EventType type;
303 	uint timestamp;
304 	SDL_JoystickID which;
305 	ubyte button;
306 	ubyte state;
307 	ubyte padding1;
308 	ubyte padding2;
309 }
310 
311 struct SDL_ControllerDeviceEvent{
312 	SDL_EventType type;
313 	uint timestamp;
314 	int which;
315 }
316 
317 static if(sdlSupport >= SDLSupport.v2_0_14){
318 	struct SDL_ControllerTouchpadEvent{
319 		uint type;
320 		uint timestamp;
321 		SDL_JoystickID which;
322 		int touchpad;
323 		int finger;
324 		float x;
325 		float y;
326 		float pressure;
327 	}
328 	
329 	struct SDL_ControllerSensorEvent{
330 		uint type;
331 		uint timestamp;
332 		SDL_JoystickID which;
333 		int sensor;
334 		float[3] data;
335 		static if(sdlSupport >= SDLSupport.v2_26){
336 			ulong timestamp_us;
337 		}
338 	}
339 }
340 
341 static if(sdlSupport >= SDLSupport.v2_0_4){
342 	struct SDL_AudioDeviceEvent{
343 		uint type;
344 		uint timestamp;
345 		uint which;
346 		ubyte iscapture;
347 		ubyte padding1;
348 		ubyte padding2;
349 		ubyte padding3;
350 	}
351 }
352 
353 struct SDL_TouchFingerEvent{
354 	SDL_EventType type;
355 	uint timestamp;
356 	SDL_TouchID touchId;
357 	SDL_FingerID fingerId;
358 	float x;
359 	float y;
360 	float dx;
361 	float dy;
362 	float pressure;
363 	static if(sdlSupport >= SDLSupport.v2_0_12){
364 		uint windowID;
365 	}
366 }
367 
368 struct SDL_MultiGestureEvent{
369 	SDL_EventType type;
370 	uint timestamp;
371 	SDL_TouchID touchId;
372 	float dTheta;
373 	float dDist;
374 	float x;
375 	float y;
376 	ushort numFingers;
377 	ushort padding;
378 }
379 
380 struct SDL_DollarGestureEvent{
381 	SDL_EventType type;
382 	uint timestamp;
383 	SDL_TouchID touchId;
384 	SDL_GestureID gestureId;
385 	uint numFingers;
386 	float error;
387 	float x;
388 	float y;
389 }
390 
391 struct SDL_DropEvent{
392 	SDL_EventType type;
393 	uint timestamp;
394 	char* file;
395 	static if(sdlSupport >= SDLSupport.v2_0_5){
396 		uint windowID;
397 	}
398 }
399 
400 struct SDL_SensorEvent{
401 	SDL_EventType type;
402 	uint timestamp;
403 	int which;
404 	float[6] data;
405 	static if(sdlSupport >= SDLSupport.v2_26){
406 		ulong timestamp_us;
407 	}
408 }
409 
410 struct SDL_QuitEvent{
411 	SDL_EventType type;
412 	uint timestamp;
413 }
414 
415 struct SDL_OSEvent{
416 	SDL_EventType type;
417 	uint timestamp;
418 }
419 
420 struct SDL_UserEvent{
421 	SDL_EventType type;
422 	uint timestamp;
423 	uint windowID;
424 	int code;
425 	void* data1;
426 	void* data2;
427 }
428 
429 struct SDL_SysWMEvent{
430 	SDL_EventType type;
431 	uint timestamp;
432 	SDL_SysWMmsg* msg;
433 }
434 
435 union SDL_Event{
436 	SDL_EventType type;
437 	SDL_CommonEvent common;
438 	static if(sdlSupport >= SDLSupport.v2_0_9){
439 		SDL_DisplayEvent display;
440 	}
441 	SDL_WindowEvent window;
442 	SDL_KeyboardEvent key;
443 	SDL_TextEditingEvent edit;
444 	static if(sdlSupport >= SDLSupport.v2_0_22){
445 		SDL_TextEditingExtEvent editExt;
446 	}
447 	SDL_TextInputEvent text;
448 	SDL_MouseMotionEvent motion;
449 	SDL_MouseButtonEvent button;
450 	SDL_MouseWheelEvent wheel;
451 	SDL_JoyAxisEvent jaxis;
452 	SDL_JoyBallEvent jball;
453 	SDL_JoyHatEvent jhat;
454 	SDL_JoyButtonEvent jbutton;
455 	SDL_JoyDeviceEvent jdevice;
456 	static if(sdlSupport >= SDLSupport.v2_24){
457 		SDL_JoyBatteryEvent jbattery;
458 	}
459 	SDL_ControllerAxisEvent caxis;
460 	SDL_ControllerButtonEvent cbutton;
461 	SDL_ControllerDeviceEvent cdevice;
462 	static if(sdlSupport >= SDLSupport.v2_0_14){
463 		SDL_ControllerTouchpadEvent ctouchpad;
464 		SDL_ControllerSensorEvent csensor;
465 	}
466 	static if(sdlSupport >= SDLSupport.v2_0_4){
467 		SDL_AudioDeviceEvent adevice;
468 	}
469 	static if(sdlSupport >= SDLSupport.v2_0_9){
470 		SDL_SensorEvent sensor;
471 	}
472 	SDL_QuitEvent quit;
473 	SDL_UserEvent user;
474 	SDL_SysWMEvent syswm;
475 	SDL_TouchFingerEvent tfinger;
476 	SDL_MultiGestureEvent mgesture;
477 	SDL_DollarGestureEvent dgesture;
478 	SDL_DropEvent drop;
479 	
480 	ubyte[56] padding;
481 }
482 
483 alias SDL_eventaction = uint;
484 enum: SDL_eventaction{
485 	SDL_ADDEVENT,
486 	SDL_PEEKEVENT,
487 	SDL_GETEVENT,
488 }
489 alias SDL_EventAction = SDL_eventaction;
490 
491 alias SDL_EventFilter = extern(C) int function(void* userdata, SDL_Event* event) nothrow;
492 
493 enum{
494 	SDL_QUERY    = -1,
495 	SDL_IGNORE   = 0,
496 	SDL_DISABLE  = 0,
497 	SDL_ENABLE   = 1,
498 }
499 
500 pragma(inline, true) @nogc nothrow{
501 	int SDL_GetEventState(SDL_EventType type){
502 		return SDL_EventState(type, SDL_QUERY);
503 	}
504 	
505 	// This is implemented in SDL_quit.h, but works better here.
506 	bool SDL_QuitRequested(){
507 		SDL_PumpEvents();
508 		return SDL_PeepEvents(null, 0, SDL_PEEKEVENT, SDL_QUIT, SDL_QUIT) > 0;
509 	}
510 }
511 
512 mixin(joinFnBinds((){
513 	string[][] ret;
514 	ret ~= makeFnBinds([
515 		[q{void}, q{SDL_PumpEvents}, q{}],
516 		[q{int}, q{SDL_PeepEvents}, q{SDL_Event* events, int numevents, SDL_eventaction action, uint minType, uint maxType}],
517 		[q{SDL_bool}, q{SDL_HasEvent}, q{uint type}],
518 		[q{SDL_bool}, q{SDL_HasEvents}, q{uint minType, uint maxType}],
519 		[q{void}, q{SDL_FlushEvent}, q{uint type}],
520 		[q{void}, q{SDL_FlushEvents}, q{uint minType, uint maxType}],
521 		[q{int}, q{SDL_PollEvent}, q{SDL_Event* event}],
522 		[q{int}, q{SDL_WaitEvent}, q{SDL_Event* event}],
523 		[q{int}, q{SDL_WaitEventTimeout}, q{SDL_Event* event, int timeout}],
524 		[q{int}, q{SDL_PushEvent}, q{SDL_Event* event}],
525 		[q{void}, q{SDL_SetEventFilter}, q{SDL_EventFilter filter, void* userdata}],
526 		[q{SDL_bool}, q{SDL_GetEventFilter}, q{SDL_EventFilter* filter, void** userdata}],
527 		[q{void}, q{SDL_AddEventWatch}, q{SDL_EventFilter filter, void* userdata}],
528 		[q{void}, q{SDL_DelEventWatch}, q{SDL_EventFilter filter, void* userdata}],
529 		[q{void}, q{SDL_FilterEvents}, q{SDL_EventFilter filter, void* userdata}],
530 		[q{ubyte}, q{SDL_EventState}, q{uint type, int state}],
531 		[q{uint}, q{SDL_RegisterEvents}, q{int numevents}],
532 	]);
533 	return ret;
534 }()));
535