1 /+
2 +            Copyright 2022 – 2024 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 static if(sdlSupport >= SDLSupport.v2_30)
110 enum: SDL_EventType{
111 	SDL_CONTROLLERUPDATECOMPLETE_RESERVED_FOR_SDL3 = 0x65A,
112 	SDL_CONTROLLERSTEAMHANDLEUPDATED = 0x65B,
113 }
114 
115 struct SDL_CommonEvent{
116 	SDL_EventType type;
117 	uint timestamp;
118 }
119 
120 static if(sdlSupport >= SDLSupport.v2_0_9){
121 	struct SDL_DisplayEvent{
122 		SDL_EventType type;
123 		uint timestamp;
124 		uint display;
125 		ubyte event;
126 		ubyte padding1;
127 		ubyte padding2;
128 		ubyte padding3;
129 		int data1;
130 	}
131 }
132 
133 struct SDL_WindowEvent{
134 	SDL_EventType type;
135 	uint timestamp;
136 	uint windowID;
137 	SDL_WindowEventID event;
138 	ubyte padding1;
139 	ubyte padding2;
140 	ubyte padding3;
141 	int data1;
142 	int data2;
143 }
144 
145 struct SDL_KeyboardEvent{
146 	SDL_EventType type;
147 	uint timestamp;
148 	uint windowID;
149 	ubyte state;
150 	ubyte repeat;
151 	ubyte padding2;
152 	ubyte padding3;
153 	SDL_Keysym keysym;
154 }
155 
156 enum SDL_TEXTEDITINGEVENT_TEXT_SIZE = 32;
157 struct SDL_TextEditingEvent{
158 	SDL_EventType type;
159 	uint timestamp;
160 	uint windowID;
161 	char[SDL_TEXTEDITINGEVENT_TEXT_SIZE] text;
162 	int start;
163 	int length;
164 }
165 
166 static if(sdlSupport >= SDLSupport.v2_0_22){
167 	struct SDL_TextEditingExtEvent{
168 		SDL_EventType type;
169 		uint timestamp;
170 		uint windowID;
171 		char* text;
172 		int start;
173 		int length;
174 	}
175 }
176 
177 enum SDL_TEXTINPUTEVENT_TEXT_SIZE = 32;
178 struct SDL_TextInputEvent{
179 	SDL_EventType type;
180 	uint timestamp;
181 	uint windowID;
182 	char[SDL_TEXTINPUTEVENT_TEXT_SIZE] text;
183 }
184 
185 struct SDL_MouseMotionEvent{
186 	SDL_EventType type;
187 	uint timestamp;
188 	uint windowID;
189 	uint which;
190 	uint state;
191 	int x;
192 	int y;
193 	int xrel;
194 	int yrel;
195 }
196 
197 struct SDL_MouseButtonEvent{
198 	SDL_EventType type;
199 	uint timestamp;
200 	uint windowID;
201 	uint which;
202 	ubyte button;
203 	ubyte state;
204 	static if(sdlSupport <= SDLSupport.v2_0_0){
205 		ubyte padding1;
206 		ubyte padding2;
207 	}else{
208 		ubyte clicks;
209 		ubyte padding1;
210 	}
211 	int x;
212 	int y;
213 }
214 
215 struct SDL_MouseWheelEvent{
216 	SDL_EventType type;
217 	uint timestamp;
218 	uint windowID;
219 	uint which;
220 	int x;
221 	int y;
222 	static if(sdlSupport >= SDLSupport.v2_0_4){
223 		uint direction;
224 	}
225 	static if(sdlSupport >= SDLSupport.v2_0_18){
226 		float preciseX;
227 		float preciseY;
228 	}
229 	static if(sdlSupport >= SDLSupport.v2_26){
230 		int mouseX;
231 		int mouseY;
232 	}
233 }
234 
235 struct SDL_JoyAxisEvent{
236 	SDL_EventType type;
237 	uint timestamp;
238 	SDL_JoystickID which;
239 	ubyte axis;
240 	ubyte padding1;
241 	ubyte padding2;
242 	ubyte padding3;
243 	short value;
244 	ushort padding4;
245 }
246 
247 struct SDL_JoyBallEvent{
248 	SDL_EventType type;
249 	uint timestamp;
250 	SDL_JoystickID which;
251 	ubyte ball;
252 	ubyte padding1;
253 	ubyte padding2;
254 	ubyte padding3;
255 	short xrel;
256 	short yrel;
257 }
258 
259 struct SDL_JoyHatEvent{
260 	SDL_EventType type;
261 	uint timestamp;
262 	SDL_JoystickID which;
263 	ubyte hat;
264 	ubyte value;
265 	ubyte padding1;
266 	ubyte padding2;
267 }
268 
269 struct SDL_JoyButtonEvent{
270 	SDL_EventType type;
271 	uint timestamp;
272 	SDL_JoystickID which;
273 	ubyte button;
274 	ubyte state;
275 	ubyte padding1;
276 	ubyte padding2;
277 }
278 
279 struct SDL_JoyDeviceEvent{
280 	SDL_EventType type;
281 	uint timestamp;
282 	int which;
283 }
284 
285 static if(sdlSupport >= SDLSupport.v2_24){
286 	struct SDL_JoyBatteryEvent{
287 		SDL_EventType type;
288 		uint timestamp;
289 		SDL_JoystickID which;
290 		SDL_JoystickPowerLevel level;
291 	}
292 }
293 
294 struct SDL_ControllerAxisEvent{
295 	SDL_EventType type;
296 	uint timestamp;
297 	SDL_JoystickID which;
298 	ubyte axis;
299 	ubyte padding1;
300 	ubyte padding2;
301 	ubyte padding3;
302 	short value;
303 	ushort padding4;
304 }
305 
306 struct SDL_ControllerButtonEvent{
307 	SDL_EventType type;
308 	uint timestamp;
309 	SDL_JoystickID which;
310 	ubyte button;
311 	ubyte state;
312 	ubyte padding1;
313 	ubyte padding2;
314 }
315 
316 struct SDL_ControllerDeviceEvent{
317 	SDL_EventType type;
318 	uint timestamp;
319 	int which;
320 }
321 
322 static if(sdlSupport >= SDLSupport.v2_0_14){
323 	struct SDL_ControllerTouchpadEvent{
324 		SDL_EventType type;
325 		uint timestamp;
326 		SDL_JoystickID which;
327 		int touchpad;
328 		int finger;
329 		float x;
330 		float y;
331 		float pressure;
332 	}
333 	
334 	struct SDL_ControllerSensorEvent{
335 		SDL_EventType type;
336 		uint timestamp;
337 		SDL_JoystickID which;
338 		int sensor;
339 		float[3] data;
340 		static if(sdlSupport >= SDLSupport.v2_26){
341 			ulong timestamp_us;
342 		}
343 	}
344 }
345 
346 static if(sdlSupport >= SDLSupport.v2_0_4){
347 	struct SDL_AudioDeviceEvent{
348 		SDL_EventType type;
349 		uint timestamp;
350 		uint which;
351 		ubyte iscapture;
352 		ubyte padding1;
353 		ubyte padding2;
354 		ubyte padding3;
355 	}
356 }
357 
358 struct SDL_TouchFingerEvent{
359 	SDL_EventType type;
360 	uint timestamp;
361 	SDL_TouchID touchId;
362 	SDL_FingerID fingerId;
363 	float x;
364 	float y;
365 	float dx;
366 	float dy;
367 	float pressure;
368 	static if(sdlSupport >= SDLSupport.v2_0_12){
369 		uint windowID;
370 	}
371 }
372 
373 struct SDL_MultiGestureEvent{
374 	SDL_EventType type;
375 	uint timestamp;
376 	SDL_TouchID touchId;
377 	float dTheta;
378 	float dDist;
379 	float x;
380 	float y;
381 	ushort numFingers;
382 	ushort padding;
383 }
384 
385 struct SDL_DollarGestureEvent{
386 	SDL_EventType type;
387 	uint timestamp;
388 	SDL_TouchID touchId;
389 	SDL_GestureID gestureId;
390 	uint numFingers;
391 	float error;
392 	float x;
393 	float y;
394 }
395 
396 struct SDL_DropEvent{
397 	SDL_EventType type;
398 	uint timestamp;
399 	char* file;
400 	static if(sdlSupport >= SDLSupport.v2_0_5){
401 		uint windowID;
402 	}
403 }
404 
405 struct SDL_SensorEvent{
406 	SDL_EventType type;
407 	uint timestamp;
408 	int which;
409 	float[6] data;
410 	static if(sdlSupport >= SDLSupport.v2_26){
411 		ulong timestamp_us;
412 	}
413 }
414 
415 struct SDL_QuitEvent{
416 	SDL_EventType type;
417 	uint timestamp;
418 }
419 
420 struct SDL_OSEvent{
421 	SDL_EventType type;
422 	uint timestamp;
423 }
424 
425 struct SDL_UserEvent{
426 	SDL_EventType type;
427 	uint timestamp;
428 	uint windowID;
429 	int code;
430 	void* data1;
431 	void* data2;
432 }
433 
434 struct SDL_SysWMEvent{
435 	SDL_EventType type;
436 	uint timestamp;
437 	SDL_SysWMmsg* msg;
438 }
439 
440 union SDL_Event{
441 	SDL_EventType type;
442 	SDL_CommonEvent common;
443 	static if(sdlSupport >= SDLSupport.v2_0_9){
444 		SDL_DisplayEvent display;
445 	}
446 	SDL_WindowEvent window;
447 	SDL_KeyboardEvent key;
448 	SDL_TextEditingEvent edit;
449 	static if(sdlSupport >= SDLSupport.v2_0_22){
450 		SDL_TextEditingExtEvent editExt;
451 	}
452 	SDL_TextInputEvent text;
453 	SDL_MouseMotionEvent motion;
454 	SDL_MouseButtonEvent button;
455 	SDL_MouseWheelEvent wheel;
456 	SDL_JoyAxisEvent jaxis;
457 	SDL_JoyBallEvent jball;
458 	SDL_JoyHatEvent jhat;
459 	SDL_JoyButtonEvent jbutton;
460 	SDL_JoyDeviceEvent jdevice;
461 	static if(sdlSupport >= SDLSupport.v2_24){
462 		SDL_JoyBatteryEvent jbattery;
463 	}
464 	SDL_ControllerAxisEvent caxis;
465 	SDL_ControllerButtonEvent cbutton;
466 	SDL_ControllerDeviceEvent cdevice;
467 	static if(sdlSupport >= SDLSupport.v2_0_14){
468 		SDL_ControllerTouchpadEvent ctouchpad;
469 		SDL_ControllerSensorEvent csensor;
470 	}
471 	static if(sdlSupport >= SDLSupport.v2_0_4){
472 		SDL_AudioDeviceEvent adevice;
473 	}
474 	static if(sdlSupport >= SDLSupport.v2_0_9){
475 		SDL_SensorEvent sensor;
476 	}
477 	SDL_QuitEvent quit;
478 	SDL_UserEvent user;
479 	SDL_SysWMEvent syswm;
480 	SDL_TouchFingerEvent tfinger;
481 	SDL_MultiGestureEvent mgesture;
482 	SDL_DollarGestureEvent dgesture;
483 	SDL_DropEvent drop;
484 	
485 	ubyte[56] padding;
486 }
487 
488 alias SDL_eventaction = uint;
489 enum: SDL_eventaction{
490 	SDL_ADDEVENT,
491 	SDL_PEEKEVENT,
492 	SDL_GETEVENT,
493 }
494 alias SDL_EventAction = SDL_eventaction;
495 
496 alias SDL_EventFilter = extern(C) int function(void* userData, SDL_Event* event) nothrow;
497 
498 enum{
499 	SDL_QUERY    = -1,
500 	SDL_IGNORE   = 0,
501 	SDL_DISABLE  = 0,
502 	SDL_ENABLE   = 1,
503 }
504 
505 pragma(inline, true) nothrow @nogc{
506 	int SDL_GetEventState(SDL_EventType type){
507 		return SDL_EventState(type, SDL_QUERY);
508 	}
509 	
510 	// This is implemented in SDL_quit.h, but works better here.
511 	bool SDL_QuitRequested(){
512 		SDL_PumpEvents();
513 		return SDL_PeepEvents(null, 0, SDL_PEEKEVENT, SDL_QUIT, SDL_QUIT) > 0;
514 	}
515 }
516 
517 mixin(joinFnBinds((){
518 	FnBind[] ret = [
519 		{q{void}, q{SDL_PumpEvents}, q{}},
520 		{q{int}, q{SDL_PeepEvents}, q{SDL_Event* events, int numEvents, SDL_eventaction action, SDL_EventType minType, SDL_EventType maxType}},
521 		{q{SDL_bool}, q{SDL_HasEvent}, q{SDL_EventType type}},
522 		{q{SDL_bool}, q{SDL_HasEvents}, q{SDL_EventType minType, SDL_EventType maxType}},
523 		{q{void}, q{SDL_FlushEvent}, q{SDL_EventType type}},
524 		{q{void}, q{SDL_FlushEvents}, q{SDL_EventType minType, SDL_EventType maxType}},
525 		{q{int}, q{SDL_PollEvent}, q{SDL_Event* event}},
526 		{q{int}, q{SDL_WaitEvent}, q{SDL_Event* event}},
527 		{q{int}, q{SDL_WaitEventTimeout}, q{SDL_Event* event, int timeout}},
528 		{q{int}, q{SDL_PushEvent}, q{SDL_Event* event}},
529 		{q{void}, q{SDL_SetEventFilter}, q{SDL_EventFilter filter, void* userData}},
530 		{q{SDL_bool}, q{SDL_GetEventFilter}, q{SDL_EventFilter* filter, void** userData}},
531 		{q{void}, q{SDL_AddEventWatch}, q{SDL_EventFilter filter, void* userData}},
532 		{q{void}, q{SDL_DelEventWatch}, q{SDL_EventFilter filter, void* userData}},
533 		{q{void}, q{SDL_FilterEvents}, q{SDL_EventFilter filter, void* userData}},
534 		{q{ubyte}, q{SDL_EventState}, q{SDL_EventType type, int state}},
535 		{q{SDL_EventType}, q{SDL_RegisterEvents}, q{int numEvents}},
536 	];
537 	return ret;
538 }()));
539