72 lines
1.9 KiB
C
72 lines
1.9 KiB
C
#include "lib_input.h"
|
|
|
|
#include "memory.h"
|
|
|
|
//TODO: native input calls
|
|
|
|
//call the hook
|
|
typedef struct Natives {
|
|
char* name;
|
|
NativeFn fn;
|
|
} Natives;
|
|
|
|
int hookInput(Interpreter* interpreter, Literal identifier, Literal alias) {
|
|
//build the natives list
|
|
Natives natives[] = {
|
|
// {"mapInputEventToKey", nativeMapInputEventToKey},
|
|
// {"mapInputEventToMouse", nativeMapInputEventToMouse},
|
|
// {"mapInputEventToSpecial", nativeMapInputEventToSpecial},
|
|
{NULL, NULL}
|
|
};
|
|
|
|
//store the library in an aliased dictionary
|
|
if (!IS_NULL(alias)) {
|
|
//make sure the name isn't taken
|
|
if (isDelcaredScopeVariable(interpreter->scope, alias)) {
|
|
interpreter->errorOutput("Can't override an existing variable\n");
|
|
freeLiteral(alias);
|
|
return false;
|
|
}
|
|
|
|
//create the dictionary to load up with functions
|
|
LiteralDictionary* dictionary = ALLOCATE(LiteralDictionary, 1);
|
|
initLiteralDictionary(dictionary);
|
|
|
|
//load the dict with functions
|
|
for (int i = 0; natives[i].name; i++) {
|
|
Literal name = TO_STRING_LITERAL(copyString(natives[i].name, strlen(natives[i].name)), strlen(natives[i].name));
|
|
Literal func = TO_FUNCTION_LITERAL((void*)natives[i].fn, 0);
|
|
func.type = LITERAL_FUNCTION_NATIVE;
|
|
|
|
setLiteralDictionary(dictionary, name, func);
|
|
|
|
freeLiteral(name);
|
|
freeLiteral(func);
|
|
}
|
|
|
|
//build the type
|
|
Literal type = TO_TYPE_LITERAL(LITERAL_DICTIONARY, true);
|
|
Literal strType = TO_TYPE_LITERAL(LITERAL_STRING, true);
|
|
Literal fnType = TO_TYPE_LITERAL(LITERAL_FUNCTION_NATIVE, true);
|
|
TYPE_PUSH_SUBTYPE(&type, strType);
|
|
TYPE_PUSH_SUBTYPE(&type, fnType);
|
|
|
|
//set scope
|
|
Literal dict = TO_DICTIONARY_LITERAL(dictionary);
|
|
declareScopeVariable(interpreter->scope, alias, type);
|
|
setScopeVariable(interpreter->scope, alias, dict, false);
|
|
|
|
//cleanup
|
|
freeLiteral(dict);
|
|
freeLiteral(type);
|
|
return 0;
|
|
}
|
|
|
|
//default
|
|
for (int i = 0; natives[i].name; i++) {
|
|
injectNativeFn(interpreter, natives[i].name, natives[i].fn);
|
|
}
|
|
|
|
return 0;
|
|
}
|