Add Input/InputBind/InputButton JS bindings, move default binds to JS
Adds a new script module exposing Input.bind/value/lastValue/isDown/
wasDown/pressed/released, InputBind.* (the inputbind_t action enum),
and InputButton.* (every named button in the compiled platform's
INPUT_BUTTON_DATA table, e.g. InputButton.SELECT on PSP).
Renames the PLATFORM global to Platform.current for consistency with
Time/Console/Input.
Uppercases every INPUT_BUTTON_DATA .name across inputlinux.c/
inputpsp.c/inputdolphin.c to match the InputButton.* JS constants
(inputButtonGetByName was already case-insensitive, so this doesn't
change matching behavior).
Moves the default key/button-to-action bindings out of
src/duskrpg/input/inputbindmap.h (deleted) and into
assets/scripts/input.js, branching on Platform.current instead of the
old per-platform #ifdefs. Wired in via require('./input.js') from
init.js.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,3 +10,12 @@ dusktest(test_modulerequire.c)
|
||||
dusktest(test_scriptdef.c)
|
||||
|
||||
dusktest(test_moduleconsole.c)
|
||||
|
||||
dusktest(test_moduleplatform.c)
|
||||
|
||||
dusktest(test_moduleinput.c)
|
||||
|
||||
dusktest(test_scriptinput.c)
|
||||
target_compile_definitions(test_scriptinput PRIVATE
|
||||
DUSK_ASSETS_DIR="${DUSK_ASSETS_DIR}"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "script/scriptmanager.h"
|
||||
#include "script/module/modulebase.h"
|
||||
#include "input/input.h"
|
||||
#include "input/inputbutton.h"
|
||||
#include "input/inputbind.h"
|
||||
|
||||
static int input_setup(void **state) {
|
||||
errorret_t ret = inputInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
ret = scriptManagerInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int input_teardown(void **state) {
|
||||
errorret_t ret = scriptManagerDispose();
|
||||
if(errorIsNotOk(ret)) errorCatch(ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_inputbind_exposes_action_ordinals(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec("InputBind.UP;", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_true(jerry_value_is_number(result));
|
||||
assert_int_equal(moduleBaseValueInt(result), (int32_t)INPUT_BIND_UP);
|
||||
jerry_value_free(result);
|
||||
|
||||
ret = scriptManagerExec("InputBind.ACCEPT;", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_int_equal(moduleBaseValueInt(result), (int32_t)INPUT_BIND_ACCEPT);
|
||||
jerry_value_free(result);
|
||||
}
|
||||
|
||||
static void test_inputbutton_exposes_platform_button_names(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec("InputButton.UP;", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_true(jerry_value_is_string(result));
|
||||
|
||||
char_t buf[16];
|
||||
moduleBaseToString(result, buf, sizeof(buf));
|
||||
assert_string_equal(buf, "UP");
|
||||
|
||||
jerry_value_free(result);
|
||||
}
|
||||
|
||||
static void test_input_bind_updates_button_action(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec(
|
||||
"Input.bind(InputButton.UP, InputBind.ACCEPT);", &result
|
||||
);
|
||||
assert_true(errorIsOk(ret));
|
||||
jerry_value_free(result);
|
||||
|
||||
inputbuttondata_t *data = inputButtonGetData(inputButtonGetByName("UP"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_ACCEPT);
|
||||
}
|
||||
|
||||
static void test_input_bind_accepts_plain_string(void **state) {
|
||||
// InputButton.* values are just the button's own name string, so a bare
|
||||
// string works identically -- confirms Input.bind() doesn't require the
|
||||
// constant object.
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec(
|
||||
"Input.bind('down', InputBind.CANCEL);", &result
|
||||
);
|
||||
assert_true(errorIsOk(ret));
|
||||
jerry_value_free(result);
|
||||
|
||||
inputbuttondata_t *data = inputButtonGetData(inputButtonGetByName("DOWN"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_CANCEL);
|
||||
}
|
||||
|
||||
static void test_input_query_functions_default_to_zero(void **state) {
|
||||
// No inputUpdate() has run (it would touch real SDL/platform input state,
|
||||
// unavailable in this headless test), so every action still reads its
|
||||
// inputInit()-zeroed default.
|
||||
jerry_value_t result;
|
||||
|
||||
errorret_t ret = scriptManagerExec("Input.isDown(InputBind.UP);", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_false(jerry_value_is_true(result));
|
||||
jerry_value_free(result);
|
||||
|
||||
ret = scriptManagerExec("Input.wasDown(InputBind.UP);", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_false(jerry_value_is_true(result));
|
||||
jerry_value_free(result);
|
||||
|
||||
ret = scriptManagerExec("Input.pressed(InputBind.UP);", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_false(jerry_value_is_true(result));
|
||||
jerry_value_free(result);
|
||||
|
||||
ret = scriptManagerExec("Input.released(InputBind.UP);", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_false(jerry_value_is_true(result));
|
||||
jerry_value_free(result);
|
||||
|
||||
ret = scriptManagerExec("Input.value(InputBind.UP);", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_true(jerry_value_is_number(result));
|
||||
assert_float_equal((float_t)jerry_value_as_number(result), 0.0f, 0.0001f);
|
||||
jerry_value_free(result);
|
||||
|
||||
ret = scriptManagerExec("Input.lastValue(InputBind.UP);", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_float_equal((float_t)jerry_value_as_number(result), 0.0f, 0.0001f);
|
||||
jerry_value_free(result);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
assertInit();
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_inputbind_exposes_action_ordinals, input_setup, input_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_inputbutton_exposes_platform_button_names,
|
||||
input_setup, input_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_input_bind_updates_button_action, input_setup, input_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_input_bind_accepts_plain_string, input_setup, input_teardown
|
||||
),
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_input_query_functions_default_to_zero, input_setup, input_teardown
|
||||
),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "script/scriptmanager.h"
|
||||
#include "script/module/modulebase.h"
|
||||
|
||||
static int platform_setup(void **state) {
|
||||
errorret_t ret = scriptManagerInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int platform_teardown(void **state) {
|
||||
errorret_t ret = scriptManagerDispose();
|
||||
if(errorIsNotOk(ret)) errorCatch(ret);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_platform_current_matches_target_system(void **state) {
|
||||
jerry_value_t result;
|
||||
errorret_t ret = scriptManagerExec("Platform.current;", &result);
|
||||
assert_true(errorIsOk(ret));
|
||||
assert_true(jerry_value_is_string(result));
|
||||
|
||||
char_t buf[32];
|
||||
moduleBaseToString(result, buf, sizeof(buf));
|
||||
assert_string_equal(buf, DUSK_TARGET_SYSTEM);
|
||||
|
||||
jerry_value_free(result);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
assertInit();
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_platform_current_matches_target_system,
|
||||
platform_setup, platform_teardown
|
||||
),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "dusktest.h"
|
||||
#include "util/memory.h"
|
||||
#include "asset/asset.h"
|
||||
#include "input/input.h"
|
||||
#include "input/inputbutton.h"
|
||||
#include "input/inputbind.h"
|
||||
#include "script/scriptmanager.h"
|
||||
#include <zip.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef DUSK_ASSETS_DIR
|
||||
#error "DUSK_ASSETS_DIR must be defined"
|
||||
#endif
|
||||
|
||||
// Runs the real, shipped assets/scripts/input.js (read straight off disk,
|
||||
// not a copy embedded in this test) through the real require()/asset-zip
|
||||
// path, so this actually verifies what ships -- on whichever platform this
|
||||
// test binary itself was built for (Platform.current === "linux" here).
|
||||
|
||||
static char_t *readFile(const char_t *relativePath) {
|
||||
char_t path[512];
|
||||
snprintf(path, sizeof(path), "%s/%s", DUSK_ASSETS_DIR, relativePath);
|
||||
|
||||
FILE *f = fopen(path, "rb");
|
||||
assert_non_null(f);
|
||||
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
|
||||
char_t *buf = (char_t *)memoryAllocate((size_t)size + 1);
|
||||
size_t read = fread(buf, 1, (size_t)size, f);
|
||||
fclose(f);
|
||||
buf[read] = '\0';
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
static zip_t *g_zip = NULL;
|
||||
|
||||
static int input_setup(void **state) {
|
||||
errorret_t ret = inputInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
ret = scriptManagerInit();
|
||||
if(errorIsNotOk(ret)) { errorCatch(ret); return -1; }
|
||||
|
||||
zip_error_t err;
|
||||
zip_error_init(&err);
|
||||
|
||||
zip_source_t *write_src = zip_source_buffer_create(NULL, 0, 1, &err);
|
||||
if(!write_src) return -1;
|
||||
|
||||
zip_t *za = zip_open_from_source(write_src, ZIP_TRUNCATE, &err);
|
||||
if(!za) { zip_source_free(write_src); return -1; }
|
||||
|
||||
char_t *inputJsSrc = readFile("scripts/input.js");
|
||||
zip_source_t *s = zip_source_buffer(za, inputJsSrc, strlen(inputJsSrc), 0);
|
||||
if(zip_file_add(za, "scripts/input.js", s, ZIP_FL_OVERWRITE) < 0) {
|
||||
zip_close(za);
|
||||
return -1;
|
||||
}
|
||||
|
||||
zip_source_keep(write_src);
|
||||
if(zip_close(za) != 0) { zip_source_free(write_src); return -1; }
|
||||
memoryFree(inputJsSrc);
|
||||
|
||||
zip_stat_t zs;
|
||||
memset(&zs, 0, sizeof(zs));
|
||||
if(zip_source_stat(write_src, &zs) != 0 || !(zs.valid & ZIP_STAT_SIZE)) {
|
||||
zip_source_free(write_src); return -1;
|
||||
}
|
||||
|
||||
void *zipbuf = malloc((size_t)zs.size);
|
||||
if(!zipbuf) { zip_source_free(write_src); return -1; }
|
||||
|
||||
if(zip_source_open(write_src) != 0) {
|
||||
free(zipbuf); zip_source_free(write_src); return -1;
|
||||
}
|
||||
zip_source_read(write_src, zipbuf, (zip_uint64_t)zs.size);
|
||||
zip_source_close(write_src);
|
||||
zip_source_free(write_src);
|
||||
|
||||
zip_error_init(&err);
|
||||
zip_source_t *read_src = zip_source_buffer_create(
|
||||
zipbuf, (zip_uint64_t)zs.size, 1, &err
|
||||
);
|
||||
if(!read_src) { free(zipbuf); return -1; }
|
||||
|
||||
g_zip = zip_open_from_source(read_src, 0, &err);
|
||||
if(!g_zip) { zip_source_free(read_src); return -1; }
|
||||
|
||||
ASSET.zip = g_zip;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int input_teardown(void **state) {
|
||||
errorret_t ret = scriptManagerDispose();
|
||||
if(errorIsNotOk(ret)) errorCatch(ret);
|
||||
|
||||
if(g_zip) { zip_close(g_zip); g_zip = NULL; }
|
||||
ASSET.zip = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_real_input_js_binds_linux_defaults(void **state) {
|
||||
errorret_t ret = scriptManagerExecFile("scripts/input.js", NULL);
|
||||
assert_true(errorIsOk(ret));
|
||||
|
||||
inputbuttondata_t *data;
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("W"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_UP);
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("ESCAPE"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_RAGEQUIT);
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("`"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_CONSOLE);
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("GAMEPAD_A"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_ACCEPT);
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("GAMEPAD_LSTICK_UP"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_UP);
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("MOUSE_X"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_POINTERX);
|
||||
|
||||
data = inputButtonGetData(inputButtonGetByName("MOUSE_Y"));
|
||||
assert_non_null(data);
|
||||
assert_int_equal(data->action, INPUT_BIND_POINTERY);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
assertInit();
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test_setup_teardown(
|
||||
test_real_input_js_binds_linux_defaults, input_setup, input_teardown
|
||||
),
|
||||
};
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user