Files
nt-lite/code/user/tests/input_api_smoke/input_api_smoke.c
T

123 lines
4.0 KiB
C

// Native input API smoke test.
// Verifies status-returning keyboard and mouse polling contracts without requiring live input.
#include <stdint.h>
#include <stdbool.h>
#include <ntlite/syscall.h>
// Prints one NUL-terminated string to the kernel console.
static void print(const char* s) {
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromCstr(s) };
SysWriteConsole(&req);
}
// Prints one unsigned integer to the kernel console.
static void print_u64(uint64_t v) {
char buf[32];
int i = 0;
if (v == 0) {
print("0");
return;
}
while (v && i < (int)sizeof(buf)) {
buf[i++] = (char)('0' + (v % 10));
v /= 10;
}
while (i--) {
char c = buf[i];
SYS_WRITE_CONSOLE_REQUEST req = { SysSvFromParts(&c, 1) };
SysWriteConsole(&req);
}
}
// Terminates the current test process.
static void exit_now(int code) {
for (;;) SysExitProcess(code);
}
// Fails the test with one status value.
static void fail_status(const char* what, uint32_t status) {
print("input_api_smoke: ");
print(what);
print(" status=");
print_u64(status);
print("\n");
exit_now(1);
}
// Requires one exact status value.
static void expect_status(const char* what, uint32_t got, uint32_t expected) {
if (got == expected) return;
print("input_api_smoke: ");
print(what);
print(" got=");
print_u64(got);
print(" expected=");
print_u64(expected);
print("\n");
exit_now(1);
}
// Program entry point.
int main(void) {
print("input_api_smoke: start\n");
expect_status("poll keyboard null", SysPollKeyboard((INPUT_KEY*)0), NTLSTATUS_INVALID_PARAMETER);
expect_status("is key down null", SysIsKeyDown(INPUT_KEY_A, (bool*)0), NTLSTATUS_INVALID_PARAMETER);
expect_status("read key null", SysReadKey((INPUT_KEY*)0), NTLSTATUS_INVALID_PARAMETER);
expect_status("read char null", SysReadKeyboardChar((char*)0), NTLSTATUS_INVALID_PARAMETER);
bool down = true;
uint32_t status = SysIsKeyDown(INPUT_KEY_A, &down);
if (status != NTLSTATUS_SUCCESS)
fail_status("is key down", status);
INPUT_KEY key = INPUT_KEY_A;
status = SysPollKeyboard(&key);
if (status != NTLSTATUS_SUCCESS && status != NTLSTATUS_WOULD_BLOCK)
fail_status("poll keyboard", status);
if (status == NTLSTATUS_WOULD_BLOCK && key != INPUT_KEY_NONE)
fail_status("poll keyboard would-block key", key);
INPUT_KEY_RECORD input;
SYS_INPUT_EVENT_REQUEST inputReq;
inputReq.Size = sizeof(inputReq);
inputReq.QueueHandle = 0;
inputReq.Flags = SYS_INPUT_EVENT_FLAG_NONE;
inputReq.TimeoutMilliseconds = 0;
inputReq.RecordOut = &input;
expect_status("peek input null", SysPeekInputEvent((const SYS_INPUT_EVENT_REQUEST*)0), NTLSTATUS_INVALID_PARAMETER);
status = SysPeekInputEvent(&inputReq);
if (status != NTLSTATUS_SUCCESS && status != NTLSTATUS_WOULD_BLOCK)
fail_status("peek input", status);
INPUT_MOUSE_EVENT mouse;
expect_status("peek mouse null", SysPeekMouse((INPUT_MOUSE_EVENT*)0), NTLSTATUS_INVALID_PARAMETER);
expect_status("read mouse null", SysReadMouse((INPUT_MOUSE_EVENT*)0), NTLSTATUS_INVALID_PARAMETER);
status = SysPeekMouse(&mouse);
if (status != NTLSTATUS_SUCCESS && status != NTLSTATUS_WOULD_BLOCK)
fail_status("peek mouse", status);
uint32_t readStatus = SysReadMouse(&mouse);
if (status == NTLSTATUS_SUCCESS) {
if (readStatus != NTLSTATUS_SUCCESS)
fail_status("read mouse after peek", readStatus);
} else if (readStatus != NTLSTATUS_WOULD_BLOCK) {
fail_status("read mouse would-block", readStatus);
}
int x = 0;
int y = 0;
expect_status("get mouse pos null x", SysGetMousePos((int*)0, &y), NTLSTATUS_INVALID_PARAMETER);
expect_status("get mouse pos null y", SysGetMousePos(&x, (int*)0), NTLSTATUS_INVALID_PARAMETER);
status = SysGetMousePos(&x, &y);
if (status != NTLSTATUS_SUCCESS)
fail_status("get mouse pos", status);
print("input_api_smoke: PASS\n");
exit_now(0);
return 0;
}