File: event.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (447 lines) | stat: -rw-r--r-- 10,253 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2010,2011,2012 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MLV_event.h"
#include "MLV_mouse.h"

#include "platform.h"

#ifndef MEMORY_DEBUG
#if defined( OS_WINDOWS )
#	include <SDL/SDL.h>
#	include <SDL/SDL_framerate.h>
#elif defined( OS_APPLE )
#   include <SDL/SDL.h>
#	include <SDL/SDL_framerate.h>
#else
#	include <SDL/SDL.h>
#	include <SDL/SDL_framerate.h>
#endif
#else
#include "memory_debug.h"
#endif

#include <string.h>

#include "warning_error.h"

#include "memory_management.h"

#include "MLV_time.h"

#include "data_structure.h"

extern DataMLV* MLV_data;


MLV_Event MLV_get_event( 
	MLV_Keyboard_button* key_sym, MLV_Keyboard_modifier* key_mod, int* unicode,
	char** texte, MLV_Input_box** input_box,
	int* mouse_x, int* mouse_y, MLV_Mouse_button* mouse_button,
	MLV_Button_state* state
){
	SDL_Event event;
	int invalidEvent = 1;
	while( invalidEvent ){
		if( SDL_PollEvent( &event ) ){
			switch( event.type ){
				case SDL_KEYDOWN: {
					if( key_sym || key_mod || state ){
						if( key_sym ){
							(*key_sym) = (MLV_Keyboard_button) 
								event.key.keysym.sym;
						}
						if( key_mod ){
							(*key_mod) = (MLV_Keyboard_modifier) 
								event.key.keysym.mod;
						}
						if( unicode ){
							(*unicode) = event.key.keysym.unicode;
						}
						if( state ){
							(*state) = MLV_PRESSED;
						}
						return MLV_KEY;
					}
				};
				break;
				case SDL_KEYUP:{
					if( key_sym || key_mod || state ){
						if( key_sym ){
							(*key_sym) = (MLV_Keyboard_button) 
								event.key.keysym.sym;
						}
						if( key_mod ){
							(*key_mod) = (MLV_Keyboard_modifier) 
								event.key.keysym.mod;
						}
						if( unicode ){
							(*unicode) = event.key.keysym.unicode;
						}
						if( state ){
							(*state) = MLV_RELEASED;
						}
						return MLV_KEY;
					}
				};
				break;
				case SDL_MOUSEMOTION:{
					if( mouse_x || mouse_y ){
						if( mouse_x ){
							(*mouse_x) = event.motion.x;
						}
						if( mouse_y ){
							(*mouse_y) = event.motion.y;
						}
						return MLV_MOUSE_MOTION;
					}
				}
				break;
				case SDL_MOUSEBUTTONUP: {
					if(  mouse_x || mouse_y || mouse_button  || state ){
						if( mouse_x ){
							(*mouse_x) = event.button.x;
						}
						if( mouse_y ){
							(*mouse_y) = event.button.y;
						}
						if( mouse_button ){
							(*mouse_button) = (MLV_Mouse_button) 
								event.button.button;
						}
						if( state ){
							(*state) = MLV_RELEASED;
						}
						return MLV_MOUSE_BUTTON;
					}
				}
				break;
				case SDL_MOUSEBUTTONDOWN: {
					if(  mouse_x || mouse_y || mouse_button  || state ){
						if( mouse_x ){
							(*mouse_x) = event.button.x;
						}
						if( mouse_y ){
							(*mouse_y) = event.button.y;
						}
						if( mouse_button ){
							(*mouse_button) = (MLV_Mouse_button) 
								event.button.button;
						}
						if( state ){
							(*state) = MLV_PRESSED;
						}
						return MLV_MOUSE_BUTTON;
					}
				}
				break;
				case SDL_QUIT:{
				}
				break;
				case SDL_USEREVENT: {
					if( event.user.code == MLV_INPUT_BOX ){
						if( texte || input_box ){
							if( input_box ){
								(*input_box) = event.user.data1;
							}
							if( texte ){
								int size = strlen(event.user.data2)+1;
								(*texte) = MLV_MALLOC( size, char );
								memcpy(
									(*texte), 
									event.user.data2,
									size
								);
							}
							return MLV_INPUT_BOX;
						}
					}
                }
            	break;

				default:;
			}
		}else{
			invalidEvent = 0;
		}
	}
	return MLV_NONE;
}

MLV_Event MLV_wait_event( 
	MLV_Keyboard_button* key_sym, MLV_Keyboard_modifier* key_mod, int* unicode, 
	char** texte, MLV_Input_box** input_box,
	int* mouse_x, int* mouse_y, MLV_Mouse_button* mouse_button,
	MLV_Button_state* state
){
	MLV_Event resultat;
	while( 
		(
			resultat = MLV_get_event( 
				key_sym, key_mod, unicode,
				texte, input_box,
				mouse_x, mouse_y, mouse_button,
				state
			)
		) == MLV_NONE
	) SDL_framerateDelay( &(MLV_data->frame_rate_manager_for_MLV_wait_event) );
	return resultat;
}

MLV_Event MLV_wait_event_or_milliseconds( 
	MLV_Keyboard_button* key_sym, MLV_Keyboard_modifier* key_mod, int* unicode, 
	char** texte, MLV_Input_box** input_box,
	int* mouse_x, int* mouse_y, MLV_Mouse_button* mouse_button,
	MLV_Button_state* state, int milliseconds
){
	MLV_Event resultat;
	int time = MLV_get_time();
	while( 
		(
			(
				resultat = MLV_get_event( 
					key_sym, key_mod, unicode,
					texte, input_box,
					mouse_x, mouse_y, mouse_button,
					state
				)
			) == MLV_NONE
		) && (
			MLV_get_time() - time < milliseconds
		)
	) SDL_framerateDelay( &(MLV_data->frame_rate_manager_for_MLV_wait_event) );
	return resultat;
}

MLV_Event MLV_wait_event_or_seconds( 
	MLV_Keyboard_button* key_sym, MLV_Keyboard_modifier* key_mod, int* unicode, 
	char** texte, MLV_Input_box** input_box,
	int* mouse_x, int* mouse_y, MLV_Mouse_button* mouse_button,
	MLV_Button_state* state, int seconds
){
	return MLV_wait_event_or_milliseconds( 
		key_sym, key_mod, unicode, texte, input_box, mouse_x, mouse_y, 
		mouse_button, state, seconds*1000
	);
}

void MLV_flush_event_queue(){
	MLV_get_event( 
		NULL, NULL, NULL,
		NULL, NULL,
		NULL, NULL, NULL,
		NULL
	);
}

MLV_Event MLV_wait_keyboard_or_mouse(
	MLV_Keyboard_button* sym, MLV_Keyboard_modifier* mod, int* unicode,
	int* mouse_x, int* mouse_y
){
	MLV_Event resultat;
	MLV_Button_state state;
	MLV_Mouse_button mouse_button;

	// We remove all existing event from the queue
	MLV_flush_event_queue();

	MLV_Keyboard_button tmp_sym;
	MLV_Keyboard_modifier tmp_mod;
	int tmp_unicode;
	int tmp_mouse_x;
	int tmp_mouse_y;

	//We wait for a new keyboard or mouse event
	while( 
		(
			(
				(
					resultat = MLV_wait_event( 
						&tmp_sym, &tmp_mod, &tmp_unicode,
						NULL, NULL,
						&tmp_mouse_x, &tmp_mouse_y, &mouse_button,
						&state
					)
				) != MLV_KEY
			) && ( 
				( resultat != MLV_MOUSE_BUTTON ) ||
				( mouse_button != MLV_BUTTON_LEFT ) 
			)
		) || (
			state != MLV_PRESSED 
		)
	);

	switch( resultat ){
		case MLV_KEY:
			if( sym ) *sym = tmp_sym;
			if( mod ) *mod = tmp_mod;
			if( unicode ) *unicode = tmp_unicode;
			break;
		case MLV_MOUSE_BUTTON:
			if( mouse_x ) *mouse_x = tmp_mouse_x;
			if( mouse_y ) *mouse_y = tmp_mouse_y;
			break;
		default:
			ERROR("Valeur d'evenements innatendu.");
	}

	return resultat;
}


MLV_Event MLV_wait_keyboard_or_mouse_or_milliseconds(
	MLV_Keyboard_button* sym, MLV_Keyboard_modifier* mod, int* unicode,
	int* mouse_x, int* mouse_y,
	int milliseconds
){
	MLV_Event resultat;
	MLV_Button_state state;
	MLV_Mouse_button mouse_button;

	// We remove all existing event from the queue
	MLV_flush_event_queue();

	int time = MLV_get_time();

	MLV_Keyboard_button tmp_sym;
	MLV_Keyboard_modifier tmp_mod;
	int tmp_unicode;
	int tmp_mouse_x;
	int tmp_mouse_y;

	int event_is_not_valid = 0;
	//We wait for a new keyboard or mouse event
	while(
		( 
			event_is_not_valid = (
				(
					(
						resultat = MLV_wait_event_or_milliseconds( 
							&tmp_sym, &tmp_mod, &tmp_unicode,
							NULL, NULL,
							&tmp_mouse_x, &tmp_mouse_y, &mouse_button,
							&state,
							milliseconds - (MLV_get_time() - time)
						)
					) != MLV_KEY
				) && ( 
					( resultat != MLV_MOUSE_BUTTON ) ||
					( mouse_button != MLV_BUTTON_LEFT ) 
				)
			) || (
				state != MLV_PRESSED 
			)
		) && (
			(MLV_get_time() - time) < milliseconds
		)
	);
	if(event_is_not_valid) return MLV_NONE;

	switch( resultat ){
		case MLV_KEY:
			if( sym ) *sym = tmp_sym;
			if( mod ) *mod = tmp_mod;
			if( unicode ) *unicode = tmp_unicode;
			break;
		case MLV_MOUSE_BUTTON:
			if( mouse_x ) *mouse_x = tmp_mouse_x;
			if( mouse_y ) *mouse_y = tmp_mouse_y;
			break;
		default:
			ERROR("Valeur d'evenements innatendu.");
	}
		
	return resultat;
}

MLV_Event MLV_wait_keyboard_or_mouse_or_seconds(
	MLV_Keyboard_button* sym, MLV_Keyboard_modifier* mod, int* unicode,
	int* mouse_x, int* mouse_y,
	int seconds
){
	return MLV_wait_keyboard_or_mouse_or_milliseconds(
		sym, mod, unicode, mouse_x, mouse_y, seconds*1000
	);
}


const char* MLV_convert_event_to_string( MLV_Event event_code ){
	switch( event_code ){
		case MLV_NONE:
			return "MLV_NONE";
		case MLV_KEY:
			return "MLV_KEY";
		case MLV_INPUT_BOX:
			return "MLV_INPUT_BOX";
		case MLV_MOUSE_BUTTON:
			return "MLV_MOUSE_BUTTON";
		case MLV_MOUSE_MOTION:
			return "MLV_MOUSE_MOTION";
		default:
			ERROR( "Event code unexpected." );
	}
	return NULL;
}

MLV_Event MLV_convert_string_to_event( const char* event_string ){
	if( strcmp( event_string, "MLV_NONE" )==0 ){
		return MLV_NONE;
	}
	if( strcmp( event_string, "MLV_KEY" )==0 ){
		return MLV_KEY;
	}
	if( strcmp( event_string, "MLV_INPUT_BOX" )==0 ){
		return MLV_INPUT_BOX;
	}
	if( strcmp( event_string, "MLV_MOUSE_BUTTON" )==0 ){
		return MLV_MOUSE_BUTTON;
	}
	if( strcmp( event_string, "MLV_MOUSE_MOTION" )==0 ){
		return MLV_MOUSE_MOTION;
	}
	ERROR( "Event name unknown." );
	return -1;
}

const char* MLV_convert_button_state_to_string( MLV_Button_state state_code ){
	switch( state_code ){
		case  MLV_PRESSED :
			return "MLV_PRESSED";
		case MLV_RELEASED :
			return "MLV_RELEASED";
		default:
			ERROR( "Unexpected state code." );
	}
	return NULL;
}

MLV_Button_state MLV_convert_string_to_button_state( const char* state_string ){
	if( strcmp( state_string, "MLV_PRESSED" ) ){
		return MLV_PRESSED;
	}
	if( strcmp( state_string, "MLV_RELEASED" ) ){
		return MLV_RELEASED;
	}
	ERROR( "Unexpected state name." );
	return -1;
}