key() / keyp() works in JS
This commit is contained in:
		
							
								
								
									
										85
									
								
								src/jsapi.c
									
									
									
									
									
								
							
							
						
						
									
										85
									
								
								src/jsapi.c
									
									
									
									
									
								
							@@ -195,45 +195,37 @@ 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))
 | 
			
		||||
	{
 | 
			
		||||
		duk_push_uint(duk, machine->memory.ram.input.gamepads.data);
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
			s32 index = duk_to_int(duk, 0) & 0xf;
 | 
			
		||||
		s32 index = duk_to_int(duk, 0) & 0x1f;
 | 
			
		||||
		duk_push_boolean(duk, machine->memory.ram.input.gamepads.data & (1 << index));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
	else duk_error(duk, DUK_ERR_ERROR, "gamepad input not declared in metadata\n");
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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))
 | 
			
		||||
	{
 | 
			
		||||
		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;
 | 
			
		||||
		s32 index = duk_to_int(duk, 0) & 0x1f;
 | 
			
		||||
 | 
			
		||||
		duk_push_boolean(duk, memory->api.btnp(memory, index, -1, -1));
 | 
			
		||||
	}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
			s32 index = duk_to_int(duk, 0) & 0xf;
 | 
			
		||||
		s32 index = duk_to_int(duk, 0) & 0x1f;
 | 
			
		||||
		u32 hold = duk_to_int(duk, 1);
 | 
			
		||||
		u32 period = duk_to_int(duk, 2);
 | 
			
		||||
 | 
			
		||||
@@ -242,7 +234,66 @@ static duk_ret_t duk_btnp(duk_context* duk)
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
	else duk_error(duk, DUK_ERR_ERROR, "gamepad input not declared in metadata\n");
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
		{
 | 
			
		||||
			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, tic->api.keyp(tic, key, hold, period));
 | 
			
		||||
				return 1;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
@@ -580,8 +631,6 @@ 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;
 | 
			
		||||
 | 
			
		||||
	duk_idx_t idx = duk_push_array(duk);
 | 
			
		||||
@@ -598,10 +647,6 @@ static duk_ret_t duk_mouse(duk_context* duk)
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
}
 | 
			
		||||
	else duk_error(duk, DUK_ERR_ERROR, "mouse input not declared in metadata\n");
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										28
									
								
								src/luaapi.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/luaapi.c
									
									
									
									
									
								
							@@ -795,7 +795,13 @@ static s32 lua_key(lua_State* lua)
 | 
			
		||||
	{
 | 
			
		||||
		tic_key key = getLuaNumber(lua, 1) + TIC_KEY_START_INDEX;
 | 
			
		||||
 | 
			
		||||
		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)
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		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)
 | 
			
		||||
			{
 | 
			
		||||
		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));
 | 
			
		||||
				return 1;
 | 
			
		||||
			}
 | 
			
		||||
			else luaL_error(lua, "invalid params, keyp [ code [ hold period ] ]\n");
 | 
			
		||||
		}
 | 
			
		||||
	else
 | 
			
		||||
	{
 | 
			
		||||
		luaL_error(lua, "invalid params, keyp [ code [ hold period ] ]\n");
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return 1;
 | 
			
		||||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static s32 lua_memcpy(lua_State* lua)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user