2019-05-15 21:40:06 -05:00
|
|
|
// 15 may 2019
|
|
|
|
#include <stdlib.h>
|
2019-05-16 22:02:03 -05:00
|
|
|
#include <string.h>
|
2019-05-15 21:40:06 -05:00
|
|
|
#include "ui.h"
|
|
|
|
#include "uipriv.h"
|
|
|
|
|
|
|
|
struct handler {
|
|
|
|
int id;
|
2019-05-16 22:02:03 -05:00
|
|
|
uiEventHandler f;
|
2019-05-15 21:40:06 -05:00
|
|
|
void *sender;
|
|
|
|
void *data;
|
|
|
|
bool blocked;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int handlerCmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct handler *ha = (const struct handler *) a;
|
|
|
|
const struct handler *hb = (const struct handler *) b;
|
|
|
|
|
|
|
|
// This could be ha->id - hb->id, but let's do it the explicit way to avoid integer overflow/underflow.
|
|
|
|
if (ha->id < hb->id)
|
|
|
|
return -1;
|
|
|
|
if (ha->id > hb->id)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct uiEvent {
|
|
|
|
uiEventOptions opts;
|
2019-05-19 13:37:32 -05:00
|
|
|
uiprivArray handlers;
|
2019-05-19 14:46:09 -05:00
|
|
|
uiprivArray unusedIDs;
|
2019-05-15 21:40:06 -05:00
|
|
|
bool firing;
|
|
|
|
};
|
|
|
|
|
2019-05-16 11:27:04 -05:00
|
|
|
uiEvent *uiNewEvent(const uiEventOptions *options)
|
2019-05-15 21:40:06 -05:00
|
|
|
{
|
2019-05-16 22:02:03 -05:00
|
|
|
uiEvent *e;
|
|
|
|
|
2019-05-16 11:27:04 -05:00
|
|
|
if (options == NULL) {
|
2019-05-27 10:02:23 -05:00
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEventOptions", uiprivFunc);
|
2019-05-16 11:27:04 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-05-16 22:02:03 -05:00
|
|
|
if (options->Size != sizeof (uiEventOptions)) {
|
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorWrongStructSize, options->Size, "uiEventOptions");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
e = (uiEvent *) uiprivAlloc(sizeof (uiEvent), "uiEvent");
|
|
|
|
e->opts = *options;
|
2019-05-19 13:37:32 -05:00
|
|
|
uiprivArrayInit(e->handlers, struct handler, 32, "uiEvent handlers");
|
2019-05-19 14:46:09 -05:00
|
|
|
uiprivArrayInit(e->unusedIDs, int, 32, "uiEvent handler unused IDs");
|
2019-05-16 22:02:03 -05:00
|
|
|
return e;
|
2019-05-15 21:40:06 -05:00
|
|
|
}
|
|
|
|
|
2019-05-16 22:34:33 -05:00
|
|
|
#define checkEventNonnull(e, ret) if ((e) == NULL) { \
|
2019-05-27 10:02:23 -05:00
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEvent", uiprivFunc); \
|
2019-05-16 22:34:33 -05:00
|
|
|
return ret; \
|
2019-05-15 22:11:21 -05:00
|
|
|
}
|
2019-05-16 22:34:33 -05:00
|
|
|
#define checkEventNotFiring(e, ret) if ((e)->firing) { \
|
2019-05-27 10:02:23 -05:00
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorChangingEventDuringFire, uiprivFunc); \
|
2019-05-16 22:34:33 -05:00
|
|
|
return ret; \
|
2019-05-15 22:11:21 -05:00
|
|
|
}
|
|
|
|
|
2019-05-16 11:27:04 -05:00
|
|
|
static bool checkEventSender(const uiEvent *e, void *sender, const char *func)
|
|
|
|
{
|
|
|
|
if (e->opts.Global && sender != NULL) {
|
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorBadSenderForEvent, "non-NULL", "global", func);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!e->opts.Global && sender == NULL) {
|
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorBadSenderForEvent, "NULL", "non-global", func);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-15 21:40:06 -05:00
|
|
|
int uiEventAddHandler(uiEvent *e, uiEventHandler handler, void *sender, void *data)
|
|
|
|
{
|
2019-05-17 21:40:29 -05:00
|
|
|
struct handler *h;
|
2019-05-19 13:37:32 -05:00
|
|
|
int id;
|
2019-05-17 21:40:29 -05:00
|
|
|
|
2019-05-15 22:11:21 -05:00
|
|
|
checkEventNonnull(e, 0);
|
|
|
|
checkEventNotFiring(e, 0);
|
2019-05-16 11:27:04 -05:00
|
|
|
if (handler == NULL) {
|
2019-05-27 10:02:23 -05:00
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorNullPointer, "uiEventHandler", uiprivFunc);
|
2019-05-16 11:27:04 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2019-05-27 10:02:23 -05:00
|
|
|
if (!checkEventSender(e, sender, uiprivFunc))
|
2019-05-16 11:27:04 -05:00
|
|
|
return 0;
|
2019-05-17 21:40:29 -05:00
|
|
|
|
2019-05-19 13:37:32 -05:00
|
|
|
id = 0;
|
2019-05-19 14:46:09 -05:00
|
|
|
if (e->unusedIDs.len > 0) {
|
|
|
|
int *p;
|
|
|
|
|
|
|
|
p = uiprivArrayAt(e->unusedIDs, int, e->unusedIDs.len - 1);
|
|
|
|
id = *p;
|
|
|
|
uiprivArrayDeleteItem(&(e->unusedIDs), p, 1);
|
2019-05-19 13:37:32 -05:00
|
|
|
} else if (e->handlers.len != 0)
|
|
|
|
id = uiprivArrayAt(e->handlers, struct handler, e->handlers.len - 1)->id + 1;
|
|
|
|
|
|
|
|
h = (struct handler *) uiprivArrayAppend(&(e->handlers), 1);
|
|
|
|
h->id = id;
|
2019-05-17 21:40:29 -05:00
|
|
|
h->f = handler;
|
|
|
|
h->sender = sender;
|
|
|
|
h->data = data;
|
2019-05-19 13:37:32 -05:00
|
|
|
uiprivArrayQsort(&(e->handlers), handlerCmp);
|
|
|
|
return id;
|
2019-05-16 11:27:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct handler *findHandler(const uiEvent *e, int id, const char *func)
|
|
|
|
{
|
|
|
|
struct handler key;
|
|
|
|
struct handler *ret;
|
|
|
|
|
|
|
|
memset(&key, 0, sizeof (struct handler));
|
|
|
|
key.id = id;
|
2019-05-19 13:37:32 -05:00
|
|
|
ret = (struct handler *) uiprivArrayBsearch(&(e->handlers), &key, handlerCmp);
|
|
|
|
if (ret == NULL)
|
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorIntIDNotFound, "uiEvent handler", id, func);
|
|
|
|
return ret;
|
2019-05-15 21:40:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiEventDeleteHandler(uiEvent *e, int id)
|
|
|
|
{
|
|
|
|
struct handler *h;
|
|
|
|
|
2019-05-16 22:34:33 -05:00
|
|
|
checkEventNonnull(e, /* nothing */);
|
|
|
|
checkEventNotFiring(e, /* nothing */);
|
2019-05-27 10:02:23 -05:00
|
|
|
h = findHandler(e, id, uiprivFunc);
|
2019-05-15 21:40:06 -05:00
|
|
|
if (h == NULL)
|
2019-05-16 11:27:04 -05:00
|
|
|
return;
|
2019-05-17 21:40:29 -05:00
|
|
|
|
2019-05-19 13:37:32 -05:00
|
|
|
uiprivArrayDeleteItem(&(e->handlers), h, 1);
|
2019-05-19 14:46:09 -05:00
|
|
|
*((int *) uiprivArrayAppend(&(e->unusedIDs), 1)) = id;
|
2019-05-15 21:40:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiEventFire(uiEvent *e, void *sender, void *args)
|
|
|
|
{
|
|
|
|
struct handler *h;
|
|
|
|
size_t i;
|
|
|
|
|
2019-05-16 22:34:33 -05:00
|
|
|
checkEventNonnull(e, /* nothing */);
|
2019-05-15 22:11:21 -05:00
|
|
|
if (e->firing) {
|
|
|
|
uiprivProgrammerError(uiprivProgrammerErrorRecursiveEventFire);
|
|
|
|
return;
|
|
|
|
}
|
2019-05-27 10:02:23 -05:00
|
|
|
if (!checkEventSender(e, sender, uiprivFunc))
|
2019-05-16 11:27:04 -05:00
|
|
|
return;
|
|
|
|
|
2019-05-15 21:40:06 -05:00
|
|
|
e->firing = true;
|
2019-05-19 13:37:32 -05:00
|
|
|
h = (struct handler *) (e->handlers.buf);
|
|
|
|
for (i = 0; i < e->handlers.len; i++) {
|
2019-05-15 21:40:06 -05:00
|
|
|
if (h->sender == sender && !h->blocked)
|
|
|
|
(*(h->f))(sender, args, h->data);
|
|
|
|
h++;
|
|
|
|
}
|
|
|
|
e->firing = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool uiEventHandlerBlocked(const uiEvent *e, int id)
|
|
|
|
{
|
2019-05-16 11:27:04 -05:00
|
|
|
struct handler *h;
|
|
|
|
|
2019-05-15 22:11:21 -05:00
|
|
|
checkEventNonnull(e, false);
|
2019-05-27 10:02:23 -05:00
|
|
|
h = findHandler(e, id, uiprivFunc);
|
2019-05-16 11:27:04 -05:00
|
|
|
if (h == NULL)
|
|
|
|
return false;
|
|
|
|
return h->blocked;
|
2019-05-15 21:40:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiEventSetHandlerBlocked(uiEvent *e, int id, bool blocked)
|
|
|
|
{
|
2019-05-16 11:27:04 -05:00
|
|
|
struct handler *h;
|
|
|
|
|
2019-05-16 22:34:33 -05:00
|
|
|
checkEventNonnull(e, /* nothing */);
|
|
|
|
checkEventNotFiring(e, /* nothing */);
|
2019-05-27 10:02:23 -05:00
|
|
|
h = findHandler(e, id, uiprivFunc);
|
2019-05-16 11:27:04 -05:00
|
|
|
if (h == NULL)
|
|
|
|
return;
|
|
|
|
h->blocked = blocked;
|
2019-05-15 21:40:06 -05:00
|
|
|
}
|