2019-06-08 12:00:30 -05:00
|
|
|
// 8 june 2019
|
|
|
|
#include "uipriv.h"
|
2019-06-16 16:26:03 -05:00
|
|
|
#include "testhooks.h"
|
2019-06-08 12:00:30 -05:00
|
|
|
|
|
|
|
struct controlType {
|
|
|
|
uint32_t id;
|
|
|
|
char *name;
|
|
|
|
uiControlVtable vtable;
|
|
|
|
uiControlOSVtable *osVtable;
|
|
|
|
size_t implDataSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int controlTypeCmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const struct controlType *ca = (const struct controlType *) a;
|
|
|
|
const struct controlType *cb = (const struct controlType *) b;
|
|
|
|
|
|
|
|
if (ca->id < cb->id)
|
|
|
|
return -1;
|
|
|
|
if (ca->id > cb->id)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct uiControl {
|
|
|
|
uint32_t controlID;
|
|
|
|
uint32_t typeID;
|
|
|
|
struct controlType *type;
|
|
|
|
void *implData;
|
2019-06-08 17:22:16 -05:00
|
|
|
uiControl *parent;
|
2019-06-08 12:00:30 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static uiprivArray controlTypes = uiprivArrayStaticInit(struct controlType, 32, "uiControl type information");
|
|
|
|
|
|
|
|
#define controlTypeID UINT32_C(0x1F2E3C4D)
|
|
|
|
|
|
|
|
uint32_t uiControlType(void)
|
|
|
|
{
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return 0;
|
|
|
|
return controlTypeID;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t nextControlID = UINT32_C(0x80000000);
|
|
|
|
|
2019-06-15 19:48:20 -05:00
|
|
|
uint32_t uiRegisterControlType(const char *name, const uiControlVtable *vtable, const uiControlOSVtable *osVtable, size_t implDataSize)
|
2019-06-08 12:00:30 -05:00
|
|
|
{
|
|
|
|
struct controlType *ct;
|
|
|
|
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return 0;
|
2019-06-15 20:38:21 -05:00
|
|
|
if (name == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("name", uiprivFunc);
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-08 12:00:30 -05:00
|
|
|
if (vtable == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("uiControlVtable", uiprivFunc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (vtable->Size != sizeof (uiControlVtable)) {
|
2019-06-09 07:50:12 -05:00
|
|
|
uiprivProgrammerErrorWrongStructSize(vtable->Size, "uiControlVtable", uiprivFunc);
|
2019-06-08 12:00:30 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#define checkMethod(method) \
|
|
|
|
if (vtable->method == NULL) { \
|
2019-06-09 12:32:31 -05:00
|
|
|
uiprivProgrammerErrorRequiredControlMethodMissing(name, "uiControlVtable", #method, uiprivFunc); \
|
2019-06-08 12:00:30 -05:00
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
checkMethod(Init)
|
|
|
|
checkMethod(Free)
|
|
|
|
#undef checkMethod
|
|
|
|
|
|
|
|
if (osVtable == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("uiControlOSVtable", uiprivFunc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!uiprivOSVtableValid(osVtable, uiprivFunc))
|
|
|
|
return 0;
|
|
|
|
|
2019-06-08 17:22:16 -05:00
|
|
|
ct = (struct controlType *) uiprivArrayAppend(&controlTypes, 1);
|
2019-06-08 12:00:30 -05:00
|
|
|
ct->id = nextControlID;
|
|
|
|
nextControlID++;
|
|
|
|
ct->name = uiprivStrdup(name);
|
|
|
|
ct->vtable = *vtable;
|
|
|
|
ct->osVtable = uiprivCloneOSVtable(osVtable);
|
|
|
|
ct->implDataSize = implDataSize;
|
2019-06-08 17:22:16 -05:00
|
|
|
return ct->id;
|
2019-06-08 12:00:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void *uiCheckControlType(void *c, uint32_t type)
|
|
|
|
{
|
2019-06-08 17:22:16 -05:00
|
|
|
uiControl *cc = (uiControl *) c;
|
2019-06-08 12:00:30 -05:00
|
|
|
struct controlType *got, *want;
|
|
|
|
struct controlType key;
|
|
|
|
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return NULL;
|
|
|
|
if (c == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("uiControl", uiprivFunc);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-06-08 17:22:16 -05:00
|
|
|
if (cc->controlID != controlTypeID) {
|
2019-06-08 12:00:30 -05:00
|
|
|
uiprivProgrammerErrorNotAControl(uiprivFunc);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now grab the type information for c itself
|
|
|
|
// do this even if we were asked if this is a uiControl; we want to make absolutely sure this is a *real* uiControl
|
|
|
|
memset(&key, 0, sizeof (struct controlType));
|
2019-06-08 17:22:16 -05:00
|
|
|
key.id = cc->typeID;
|
|
|
|
got = (struct controlType *) uiprivArrayBsearch(&controlTypes, &key, controlTypeCmp);
|
2019-06-08 12:00:30 -05:00
|
|
|
if (got == NULL) {
|
2019-06-09 12:32:31 -05:00
|
|
|
uiprivProgrammerErrorUnknownControlTypeUsed(cc->typeID, uiprivFunc);
|
2019-06-08 12:00:30 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == controlTypeID)
|
|
|
|
// this is a real uiControl; no need to check further
|
|
|
|
return c;
|
|
|
|
|
|
|
|
// type isn't uiControlType(); make sure it is valid too
|
|
|
|
memset(&key, 0, sizeof (struct controlType));
|
|
|
|
key.id = type;
|
2019-06-08 17:22:16 -05:00
|
|
|
want = (struct controlType *) uiprivArrayBsearch(&controlTypes, &key, controlTypeCmp);
|
2019-06-08 12:00:30 -05:00
|
|
|
if (want == NULL) {
|
2019-06-09 12:32:31 -05:00
|
|
|
uiprivProgrammerErrorUnknownControlTypeRequested(type, uiprivFunc);
|
2019-06-08 12:00:30 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-06-08 17:22:16 -05:00
|
|
|
if (cc->typeID != type) {
|
2019-06-09 12:32:31 -05:00
|
|
|
uiprivProgrammerErrorWrongControlType(got->name, want->name, uiprivFunc);
|
2019-06-08 12:00:30 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2019-06-08 14:31:40 -05:00
|
|
|
#define callVtable(method, ...) ((*(method))(__VA_ARGS__))
|
|
|
|
|
2019-06-08 12:00:30 -05:00
|
|
|
uiControl *uiNewControl(uint32_t type, void *initData)
|
|
|
|
{
|
|
|
|
uiControl *c;
|
|
|
|
struct controlType *ct;
|
|
|
|
struct controlType key;
|
|
|
|
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return NULL;
|
|
|
|
if (type == controlTypeID) {
|
2019-06-09 12:18:18 -05:00
|
|
|
uiprivProgrammerErrorCannotCreateBaseControl(uiprivFunc);
|
2019-06-08 12:00:30 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(&key, 0, sizeof (struct controlType));
|
|
|
|
key.id = type;
|
|
|
|
ct = (struct controlType *) uiprivArrayBsearch(&controlTypes, &key, controlTypeCmp);
|
|
|
|
if (ct == NULL) {
|
2019-06-09 12:32:31 -05:00
|
|
|
uiprivProgrammerErrorUnknownControlTypeRequested(type, uiprivFunc);
|
2019-06-08 12:00:30 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-06-08 14:31:40 -05:00
|
|
|
|
|
|
|
c = uiprivNew(uiControl);
|
|
|
|
c->controlID = controlTypeID;
|
|
|
|
c->typeID = type;
|
|
|
|
c->type = ct;
|
|
|
|
if (ct->implDataSize != 0)
|
|
|
|
c->implData = uiprivAlloc(ct->implDataSize, "uiControl implementation data");
|
|
|
|
if (!callVtable(c->type->vtable.Init, c, c->implData, initData)) {
|
|
|
|
uiprivProgrammerErrorInvalidControlInitData(ct->name, uiprivFunc);
|
|
|
|
uiprivFree(c->implData);
|
|
|
|
uiprivFree(c);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return c;
|
2019-06-08 12:00:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uiControlFree(uiControl *c)
|
|
|
|
{
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return;
|
2019-06-08 14:31:40 -05:00
|
|
|
if (c == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("uiControl", uiprivFunc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c->parent != NULL) {
|
|
|
|
uiprivProgrammerErrorControlHasParent(uiprivFunc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uiEventFire(uiControlOnFree(), c, NULL);
|
|
|
|
uiEventInvalidateSender(uiControlOnFree(), c);
|
|
|
|
|
|
|
|
callVtable(c->type->vtable.Free, c, c->implData);
|
|
|
|
|
|
|
|
uiprivFree(c->implData);
|
|
|
|
uiprivFree(c);
|
2019-06-08 12:00:30 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 18:52:09 -05:00
|
|
|
void uiControlSetParent(uiControl *c, uiControl *parent)
|
|
|
|
{
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return;
|
|
|
|
if (c == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("uiControl", uiprivFunc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c->parent == NULL && parent == NULL) {
|
|
|
|
uiprivProgrammerErrorReparenting("no", "no", uiprivFunc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (c->parent != NULL && parent != NULL) {
|
|
|
|
uiprivProgrammerErrorReparenting("a", "another", uiprivFunc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
c->parent = parent;
|
|
|
|
}
|
|
|
|
|
2019-06-08 12:00:30 -05:00
|
|
|
void *uiControlImplData(uiControl *c)
|
|
|
|
{
|
|
|
|
if (!uiprivCheckInitializedAndThread())
|
|
|
|
return NULL;
|
2019-06-08 14:31:40 -05:00
|
|
|
if (c == NULL) {
|
|
|
|
uiprivProgrammerErrorNullPointer("uiControl", uiprivFunc);
|
2019-06-08 17:22:16 -05:00
|
|
|
return NULL;
|
2019-06-08 14:31:40 -05:00
|
|
|
}
|
|
|
|
return c->implData;
|
2019-06-08 12:00:30 -05:00
|
|
|
}
|
2019-06-16 16:26:03 -05:00
|
|
|
|
2019-06-16 16:32:47 -05:00
|
|
|
static uiControl testHookControlWithInvalidControlMarker = {
|
|
|
|
.controlID = 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
uiControl *uiprivTestHookControlWithInvalidControlMarker(void)
|
|
|
|
{
|
|
|
|
return &testHookControlWithInvalidControlMarker;
|
|
|
|
}
|
|
|
|
|
2019-06-16 16:26:03 -05:00
|
|
|
static uiControl testHookControlWithInvalidType = {
|
|
|
|
.controlID = controlTypeID,
|
|
|
|
.typeID = 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
uiControl *uiprivTestHookControlWithInvalidType(void)
|
|
|
|
{
|
|
|
|
return &testHookControlWithInvalidType;
|
|
|
|
}
|