diff --git a/src/jsapi.c b/src/jsapi.c index c14837e..3fa311a 100644 --- a/src/jsapi.c +++ b/src/jsapi.c @@ -195,23 +195,17 @@ static duk_ret_t duk_btn(duk_context* duk) { tic_machine* machine = getDukMachine(duk); - if(machine->memory.input.gamepad) + if (duk_is_null_or_undefined(duk, 0)) { - if (duk_is_null_or_undefined(duk, 0)) - { - duk_push_uint(duk, machine->memory.ram.input.gamepads.data); - } - else - { - s32 index = duk_to_int(duk, 0) & 0xf; - duk_push_boolean(duk, machine->memory.ram.input.gamepads.data & (1 << index)); - } - - return 1; + duk_push_uint(duk, machine->memory.ram.input.gamepads.data); + } + else + { + s32 index = duk_to_int(duk, 0) & 0x1f; + duk_push_boolean(duk, machine->memory.ram.input.gamepads.data & (1 << index)); } - else duk_error(duk, DUK_ERR_ERROR, "gamepad input not declared in metadata\n"); - return 0; + return 1; } static duk_ret_t duk_btnp(duk_context* duk) @@ -219,30 +213,87 @@ static duk_ret_t duk_btnp(duk_context* duk) tic_machine* machine = getDukMachine(duk); tic_mem* memory = (tic_mem*)machine; - if(machine->memory.input.gamepad) + if (duk_is_null_or_undefined(duk, 0)) { - if (duk_is_null_or_undefined(duk, 0)) - { - duk_push_uint(duk, memory->api.btnp(memory, -1, -1, -1)); - } - else if(duk_is_null_or_undefined(duk, 1) && duk_is_null_or_undefined(duk, 2)) - { - s32 index = duk_to_int(duk, 0) & 0xf; + duk_push_uint(duk, memory->api.btnp(memory, -1, -1, -1)); + } + else if(duk_is_null_or_undefined(duk, 1) && duk_is_null_or_undefined(duk, 2)) + { + s32 index = duk_to_int(duk, 0) & 0x1f; - duk_push_boolean(duk, memory->api.btnp(memory, index, -1, -1)); + duk_push_boolean(duk, memory->api.btnp(memory, index, -1, -1)); + } + else + { + s32 index = duk_to_int(duk, 0) & 0x1f; + u32 hold = duk_to_int(duk, 1); + u32 period = duk_to_int(duk, 2); + + duk_push_boolean(duk, memory->api.btnp(memory, index, hold, period)); + } + + return 1; +} + +static s32 duk_key(duk_context* duk) +{ + tic_machine* machine = getDukMachine(duk); + tic_mem* tic = &machine->memory; + + if (duk_is_null_or_undefined(duk, 0)) + { + duk_push_uint(duk, tic->api.key(tic, tic_key_unknown)); + } + else + { + tic_key key = duk_to_int(duk, 0) + TIC_KEY_START_INDEX; + + if(key < TIC_KEYS_COUNT) + duk_push_boolean(duk, tic->api.key(tic, key)); + else + { + duk_error(duk, DUK_ERR_ERROR, "unknown keyboard code\n"); + return 0; + } + } + + return 1; +} + +static s32 duk_keyp(duk_context* duk) +{ + tic_machine* machine = getDukMachine(duk); + tic_mem* tic = &machine->memory; + + if (duk_is_null_or_undefined(duk, 0)) + { + duk_push_uint(duk, tic->api.keyp(tic, tic_key_unknown, -1, -1)); + } + else + { + tic_key key = duk_to_int(duk, 0) + TIC_KEY_START_INDEX; + + if(key >= TIC_KEYS_COUNT) + { + duk_error(duk, DUK_ERR_ERROR, "unknown keyboard code\n"); } else { - s32 index = duk_to_int(duk, 0) & 0xf; - u32 hold = duk_to_int(duk, 1); - u32 period = duk_to_int(duk, 2); + if(duk_is_null_or_undefined(duk, 1) && duk_is_null_or_undefined(duk, 2)) + { + duk_push_boolean(duk, tic->api.keyp(tic, key, -1, -1)); + return 1; + } + else + { + u32 hold = duk_to_int(duk, 1); + u32 period = duk_to_int(duk, 2); - duk_push_boolean(duk, memory->api.btnp(memory, index, hold, period)); + duk_push_boolean(duk, tic->api.keyp(tic, key, hold, period)); + return 1; + } } - - return 1; } - else duk_error(duk, DUK_ERR_ERROR, "gamepad input not declared in metadata\n"); return 0; } @@ -580,27 +631,21 @@ static duk_ret_t duk_mouse(duk_context* duk) { tic_machine* machine = getDukMachine(duk); - if(machine->memory.input.mouse) - { - const tic80_mouse* mouse = &machine->memory.ram.input.mouse; + const tic80_mouse* mouse = &machine->memory.ram.input.mouse; - duk_idx_t idx = duk_push_array(duk); - duk_push_int(duk, mouse->x); - duk_put_prop_index(duk, idx, 0); - duk_push_int(duk, mouse->y); - duk_put_prop_index(duk, idx, 1); - duk_push_boolean(duk, mouse->left); - duk_put_prop_index(duk, idx, 2); - duk_push_boolean(duk, mouse->middle); - duk_put_prop_index(duk, idx, 3); - duk_push_boolean(duk, mouse->right); - duk_put_prop_index(duk, idx, 4); + duk_idx_t idx = duk_push_array(duk); + duk_push_int(duk, mouse->x); + duk_put_prop_index(duk, idx, 0); + duk_push_int(duk, mouse->y); + duk_put_prop_index(duk, idx, 1); + duk_push_boolean(duk, mouse->left); + duk_put_prop_index(duk, idx, 2); + duk_push_boolean(duk, mouse->middle); + duk_put_prop_index(duk, idx, 3); + duk_push_boolean(duk, mouse->right); + duk_put_prop_index(duk, idx, 4); - return 1; - } - else duk_error(duk, DUK_ERR_ERROR, "mouse input not declared in metadata\n"); - - return 0; + return 1; } static duk_ret_t duk_circ(duk_context* duk) @@ -771,8 +816,12 @@ static const struct{duk_c_function func; s32 params;} ApiFunc[] = {duk_music, 4}, {duk_sync, 3}, {duk_reset, 0}, + {duk_key, 1}, + {duk_keyp, 3}, }; +STATIC_ASSERT(api_func, COUNT_OF(ApiKeywords) == COUNT_OF(ApiFunc)); + s32 duk_timeout_check(void* udata) { tic_machine* machine = (tic_machine*)udata; diff --git a/src/luaapi.c b/src/luaapi.c index ff4614c..761e8d6 100644 --- a/src/luaapi.c +++ b/src/luaapi.c @@ -795,7 +795,13 @@ static s32 lua_key(lua_State* lua) { tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX; - lua_pushboolean(lua, tic->api.key(tic, key)); + if(key < TIC_KEYS_COUNT) + lua_pushboolean(lua, tic->api.key(tic, key)); + else + { + luaL_error(lua, "unknown keyboard code\n"); + return 0; + } } else { @@ -817,27 +823,35 @@ static s32 lua_keyp(lua_State* lua) { lua_pushinteger(lua, tic->api.keyp(tic, tic_key_unknown, -1, -1)); } - else if(top == 1) - { - tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX; - - lua_pushboolean(lua, tic->api.keyp(tic, key, -1, -1)); - } - else if(top == 3) - { - tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX; - u32 hold = getLuaNumber(lua, 2); - u32 period = getLuaNumber(lua, 3); - - lua_pushboolean(lua, tic->api.keyp(tic, key, hold, period)); - } else { - luaL_error(lua, "invalid params, keyp [ code [ hold period ] ]\n"); - return 0; + tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX; + + if(key >= TIC_KEYS_COUNT) + { + luaL_error(lua, "unknown keyboard code\n"); + } + else + { + if(top == 1) + { + lua_pushboolean(lua, tic->api.keyp(tic, key, -1, -1)); + + return 1; + } + else if(top == 3) + { + u32 hold = getLuaNumber(lua, 2); + u32 period = getLuaNumber(lua, 3); + + lua_pushboolean(lua, tic->api.keyp(tic, key, hold, period)); + return 1; + } + else luaL_error(lua, "invalid params, keyp [ code [ hold period ] ]\n"); + } } - return 1; + return 0; } static s32 lua_memcpy(lua_State* lua)