comparison src/events/SDL_events.c @ 5149:3052772b59db

Added functions to watch events as they go through the event queue.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 01 Feb 2011 19:15:42 -0800
parents dc0dfdd58f27
children fb424691cfc7
comparison
equal deleted inserted replaced
5148:2f44e6969a59 5149:3052772b59db
36 36
37 /* Public data -- the event filter */ 37 /* Public data -- the event filter */
38 SDL_EventFilter SDL_EventOK = NULL; 38 SDL_EventFilter SDL_EventOK = NULL;
39 void *SDL_EventOKParam; 39 void *SDL_EventOKParam;
40 40
41 typedef struct SDL_EventWatcher {
42 SDL_EventFilter callback;
43 void *userdata;
44 struct SDL_EventWatcher *next;
45 } SDL_EventWatcher;
46
47 static SDL_EventWatcher *SDL_event_watchers = NULL;
48
41 typedef struct { 49 typedef struct {
42 Uint32 bits[8]; 50 Uint32 bits[8];
43 } SDL_DisabledEventBlock; 51 } SDL_DisabledEventBlock;
44 52
45 static SDL_DisabledEventBlock *SDL_disabled_events[256]; 53 static SDL_DisabledEventBlock *SDL_disabled_events[256];
93 for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) { 101 for (i = 0; i < SDL_arraysize(SDL_disabled_events); ++i) {
94 if (SDL_disabled_events[i]) { 102 if (SDL_disabled_events[i]) {
95 SDL_free(SDL_disabled_events[i]); 103 SDL_free(SDL_disabled_events[i]);
96 SDL_disabled_events[i] = NULL; 104 SDL_disabled_events[i] = NULL;
97 } 105 }
106 }
107
108 while (SDL_event_watchers) {
109 SDL_EventWatcher *tmp = SDL_event_watchers;
110 SDL_event_watchers = tmp->next;
111 SDL_free(tmp);
98 } 112 }
99 } 113 }
100 114
101 /* This function (and associated calls) may be called more than once */ 115 /* This function (and associated calls) may be called more than once */
102 int 116 int
338 } 352 }
339 353
340 int 354 int
341 SDL_PushEvent(SDL_Event * event) 355 SDL_PushEvent(SDL_Event * event)
342 { 356 {
357 SDL_EventWatcher *curr;
358
343 if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) { 359 if (SDL_EventOK && !SDL_EventOK(SDL_EventOKParam, event)) {
344 return 0; 360 return 0;
345 } 361 }
362
363 for (curr = SDL_event_watchers; curr; curr = curr->next) {
364 curr->callback(curr->userdata, event);
365 }
366
346 if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) { 367 if (SDL_PeepEvents(event, 1, SDL_ADDEVENT, 0, 0) <= 0) {
347 return -1; 368 return -1;
348 } 369 }
349 370
350 SDL_GestureProcessEvent(event); 371 SDL_GestureProcessEvent(event);
372 } 393 }
373 if (userdata) { 394 if (userdata) {
374 *userdata = SDL_EventOKParam; 395 *userdata = SDL_EventOKParam;
375 } 396 }
376 return SDL_EventOK ? SDL_TRUE : SDL_FALSE; 397 return SDL_EventOK ? SDL_TRUE : SDL_FALSE;
398 }
399
400 /* FIXME: This is not thread-safe yet */
401 void
402 SDL_AddEventWatch(SDL_EventFilter filter, void *userdata)
403 {
404 SDL_EventWatcher *watcher;
405
406 watcher = (SDL_EventWatcher *)SDL_malloc(sizeof(*watcher));
407 if (!watcher) {
408 /* Uh oh... */
409 return;
410 }
411 watcher->callback = filter;
412 watcher->userdata = userdata;
413 watcher->next = SDL_event_watchers;
414 SDL_event_watchers = watcher;
415 }
416
417 /* FIXME: This is not thread-safe yet */
418 void
419 SDL_DelEventWatch(SDL_EventFilter filter, void *userdata)
420 {
421 SDL_EventWatcher *prev = NULL;
422 SDL_EventWatcher *curr;
423
424 for (curr = SDL_event_watchers; curr; prev = curr, curr = curr->next) {
425 if (curr->callback == filter && curr->userdata == userdata) {
426 if (prev) {
427 prev->next = curr->next;
428 } else {
429 SDL_event_watchers = curr->next;
430 }
431 SDL_free(curr);
432 break;
433 }
434 }
377 } 435 }
378 436
379 void 437 void
380 SDL_FilterEvents(SDL_EventFilter filter, void *userdata) 438 SDL_FilterEvents(SDL_EventFilter filter, void *userdata)
381 { 439 {