2021-11-30 21:29:18 -06:00
|
|
|
#include "input.h"
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2023-11-03 22:01:30 -05:00
|
|
|
#include "jsffi.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include "font.h"
|
|
|
|
#include "log.h"
|
2022-12-14 13:01:42 -06:00
|
|
|
#include "script.h"
|
2022-08-26 09:19:17 -05:00
|
|
|
#include "stb_ds.h"
|
2023-04-25 16:59:12 -05:00
|
|
|
#include "time.h"
|
2023-05-12 13:22:05 -05:00
|
|
|
#include <stdio.h>
|
2023-08-31 17:23:24 -05:00
|
|
|
#include <ctype.h>
|
2023-09-21 19:51:38 -05:00
|
|
|
#include <wchar.h>
|
2023-09-04 09:48:44 -05:00
|
|
|
#include "resources.h"
|
2021-11-30 21:29:18 -06:00
|
|
|
|
2023-08-31 03:10:30 -05:00
|
|
|
static int mouse_states[3] = {INPUT_UP};
|
|
|
|
static int key_states[512] = {INPUT_UP};
|
2023-08-31 02:05:06 -05:00
|
|
|
|
2023-11-14 09:20:09 -06:00
|
|
|
HMM_Vec2 mousewheel = {0,0};
|
|
|
|
HMM_Vec2 mouse_pos = {0, 0};
|
|
|
|
HMM_Vec2 mouse_delta = {0, 0};
|
2022-02-06 10:14:57 -06:00
|
|
|
|
2023-04-25 14:59:26 -05:00
|
|
|
struct joystick {
|
|
|
|
int id;
|
2023-08-31 02:05:06 -05:00
|
|
|
//GLFWgamepadstate state;
|
2023-04-25 14:59:26 -05:00
|
|
|
};
|
|
|
|
|
2022-08-26 09:19:17 -05:00
|
|
|
static int *downkeys = NULL;
|
2023-04-25 14:59:26 -05:00
|
|
|
static struct joystick *joysticks = NULL;
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2022-12-16 11:54:05 -06:00
|
|
|
static int mquit = 0;
|
|
|
|
|
2023-04-19 15:16:35 -05:00
|
|
|
static struct callee pawn_callee;
|
2023-04-25 16:59:12 -05:00
|
|
|
static struct callee gamepad_callee;
|
2022-12-19 18:15:38 -06:00
|
|
|
|
2023-02-03 13:41:53 -06:00
|
|
|
void add_downkey(int key) {
|
|
|
|
for (int i = 0; i < arrlen(downkeys); i++)
|
|
|
|
if (downkeys[i] == key) return;
|
|
|
|
|
|
|
|
arrput(downkeys, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rm_downkey(int key) {
|
|
|
|
for (int i = 0; i < arrlen(downkeys); i++)
|
|
|
|
if (downkeys[i] == key) {
|
|
|
|
arrdelswap(downkeys, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
char *mb2str(int btn)
|
|
|
|
{
|
|
|
|
switch(btn) {
|
|
|
|
case 0:
|
2023-09-12 17:19:46 -05:00
|
|
|
return "lm";
|
2023-08-31 02:05:06 -05:00
|
|
|
break;
|
|
|
|
case 1:
|
2023-09-12 17:19:46 -05:00
|
|
|
return "rm";
|
2023-08-31 02:05:06 -05:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-09-12 17:19:46 -05:00
|
|
|
return "mm";
|
2023-08-31 02:05:06 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return "NULLMOUSE";
|
2023-02-13 08:30:35 -06:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
JSValue input2js(int state)
|
|
|
|
{
|
|
|
|
switch(state) {
|
|
|
|
case INPUT_UP: return jstr("released");
|
|
|
|
case INPUT_REPEAT: return jstr("rep");
|
|
|
|
case INPUT_DOWN: return jstr("pressed");
|
|
|
|
case 3: return jstr("pressrep");
|
|
|
|
case 4: return jstr("down");
|
|
|
|
}
|
2023-11-29 12:40:13 -06:00
|
|
|
return JS_UNDEFINED;
|
2023-10-16 09:40:43 -05:00
|
|
|
}
|
|
|
|
|
2023-09-12 17:19:46 -05:00
|
|
|
void input_mouse(int btn, int state, uint32_t mod)
|
2023-08-31 02:05:06 -05:00
|
|
|
{
|
2023-09-12 17:19:46 -05:00
|
|
|
char out[16] = {0};
|
|
|
|
snprintf(out, 16, "%s%s%s%s",
|
|
|
|
mod & SAPP_MODIFIER_CTRL ? "C-" : "",
|
|
|
|
mod & SAPP_MODIFIER_ALT ? "M-" : "",
|
|
|
|
mod & SAPP_MODIFIER_SUPER ? "S-" : "",
|
|
|
|
mb2str(btn)
|
|
|
|
);
|
|
|
|
|
2023-09-11 17:09:21 -05:00
|
|
|
JSValue argv[3];
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("emacs");
|
|
|
|
argv[1] = jstr(out);
|
|
|
|
argv[2] = input2js(state);
|
2023-09-12 17:19:46 -05:00
|
|
|
script_callee(pawn_callee, 3, argv);
|
2022-02-06 10:14:57 -06:00
|
|
|
}
|
|
|
|
|
2023-09-15 22:40:19 -05:00
|
|
|
void input_mouse_move(float x, float y, float dx, float dy, uint32_t mod)
|
2023-08-31 02:05:06 -05:00
|
|
|
{
|
|
|
|
mouse_pos.x = x;
|
2023-10-05 13:33:43 -05:00
|
|
|
mouse_pos.y = mainwin.height - y;
|
2023-08-31 02:05:06 -05:00
|
|
|
mouse_delta.x = dx;
|
2023-09-14 12:49:29 -05:00
|
|
|
mouse_delta.y = -dy;
|
2023-08-31 02:05:06 -05:00
|
|
|
|
2023-09-15 03:37:07 -05:00
|
|
|
JSValue argv[4];
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("mouse");
|
|
|
|
argv[1] = jstr("move");
|
2023-08-31 02:05:06 -05:00
|
|
|
argv[2] = vec2js(mouse_pos);
|
2023-09-12 17:19:46 -05:00
|
|
|
argv[3] = vec2js(mouse_delta);
|
|
|
|
script_callee(pawn_callee, 4, argv);
|
2023-08-31 02:05:06 -05:00
|
|
|
JS_FreeValue(js, argv[2]);
|
2023-09-12 17:19:46 -05:00
|
|
|
JS_FreeValue(js, argv[3]);
|
2023-02-02 17:52:15 -06:00
|
|
|
}
|
|
|
|
|
2023-09-15 22:40:19 -05:00
|
|
|
void input_mouse_scroll(float x, float y, uint32_t mod)
|
2023-08-31 02:05:06 -05:00
|
|
|
{
|
2023-09-15 22:40:19 -05:00
|
|
|
mousewheel.x = x;
|
|
|
|
mousewheel.y = y;
|
|
|
|
|
|
|
|
JSValue argv[4];
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("mouse");
|
2023-09-15 22:40:19 -05:00
|
|
|
char out[16] = {0};
|
|
|
|
snprintf(out, 16, "%s%s%sscroll",
|
|
|
|
mod & SAPP_MODIFIER_CTRL ? "C-" : "",
|
|
|
|
mod & SAPP_MODIFIER_ALT ? "M-" : "",
|
|
|
|
mod & SAPP_MODIFIER_SUPER ? "S-" : ""
|
|
|
|
);
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[1] = jstr(out);
|
2023-09-15 22:40:19 -05:00
|
|
|
argv[2] = vec2js(mousewheel);
|
|
|
|
script_callee(pawn_callee, 3, argv);
|
|
|
|
JS_FreeValue(js, argv[2]);
|
2023-02-04 22:53:54 -06:00
|
|
|
}
|
|
|
|
|
2023-09-02 06:53:52 -05:00
|
|
|
void input_btn(int btn, int state, uint32_t mod)
|
2023-08-31 02:05:06 -05:00
|
|
|
{
|
2023-10-16 09:40:43 -05:00
|
|
|
char keystr[16] = {0};
|
|
|
|
strncat(keystr,keyname_extd(btn),16);
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-09-02 06:53:52 -05:00
|
|
|
if (strlen(keystr) == 1 && mod & SAPP_MODIFIER_SHIFT)
|
|
|
|
keystr[0] = toupper(keystr[0]);
|
|
|
|
|
|
|
|
char out[16] = {0};
|
|
|
|
snprintf(out, 16, "%s%s%s%s",
|
|
|
|
mod & SAPP_MODIFIER_CTRL ? "C-" : "",
|
|
|
|
mod & SAPP_MODIFIER_ALT ? "M-" : "",
|
|
|
|
mod & SAPP_MODIFIER_SUPER ? "S-" : "",
|
|
|
|
keystr
|
|
|
|
);
|
|
|
|
|
|
|
|
JSValue argv[3];
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[1] = jstr(out);
|
|
|
|
argv[2] = input2js(state);
|
2023-09-02 06:53:52 -05:00
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("emacs");
|
2023-09-02 06:53:52 -05:00
|
|
|
script_callee(pawn_callee, 3, argv);
|
2023-08-22 23:19:09 -05:00
|
|
|
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("action");
|
2023-09-02 06:53:52 -05:00
|
|
|
script_callee(pawn_callee, 3, argv);
|
2023-10-03 17:16:38 -05:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
if (state == INPUT_DOWN) {
|
|
|
|
key_states[btn] = INPUT_DOWN;
|
|
|
|
add_downkey(btn);
|
|
|
|
}
|
|
|
|
else if (state == INPUT_UP) {
|
|
|
|
key_states[btn] = INPUT_UP;
|
|
|
|
rm_downkey(btn);
|
|
|
|
}
|
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-09-21 19:51:38 -05:00
|
|
|
static const uint32_t UNCHAR_FLAGS = SAPP_MODIFIER_CTRL | SAPP_MODIFIER_ALT | SAPP_MODIFIER_SUPER;
|
|
|
|
|
|
|
|
void input_key(uint32_t key, uint32_t mod)
|
2023-08-31 02:05:06 -05:00
|
|
|
{
|
2023-09-21 19:51:38 -05:00
|
|
|
if (mod & UNCHAR_FLAGS) return;
|
|
|
|
if (key <= 31 || key >= 127) return;
|
|
|
|
|
2023-09-02 06:53:52 -05:00
|
|
|
JSValue argv[2];
|
2023-09-21 19:51:38 -05:00
|
|
|
char s[2] = {key, '\0'};
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("char");
|
|
|
|
argv[1] = jstr(s);
|
2023-04-19 15:16:35 -05:00
|
|
|
script_callee(pawn_callee, 2, argv);
|
2023-02-13 13:35:01 -06:00
|
|
|
}
|
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
void register_pawn(struct callee c) {
|
|
|
|
pawn_callee = c;
|
|
|
|
}
|
2023-03-10 13:13:48 -06:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
void register_gamepad(struct callee c) {
|
|
|
|
gamepad_callee = c;
|
2023-04-25 14:59:26 -05:00
|
|
|
}
|
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
static void pawn_call_keydown(int key) {
|
|
|
|
JSValue argv[4];
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("input");
|
|
|
|
argv[1] = jstr("num");
|
|
|
|
argv[2] = input2js(INPUT_DOWN);
|
2023-08-31 02:05:06 -05:00
|
|
|
/* TODO: Could cache */
|
|
|
|
argv[3] = JS_NewInt32(js, key);
|
|
|
|
script_callee(pawn_callee, 4, argv);
|
|
|
|
JS_FreeValue(js, argv[3]);
|
2023-04-25 14:59:26 -05:00
|
|
|
}
|
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
/*
|
|
|
|
0 free
|
|
|
|
1 lock
|
|
|
|
*/
|
|
|
|
void set_mouse_mode(int mousemode) { sapp_lock_mouse(mousemode); }
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
void input_init() {
|
2023-08-31 03:10:30 -05:00
|
|
|
for (int i = 0; i < 512; i++)
|
|
|
|
key_states[i] = INPUT_UP;
|
2022-08-12 14:03:56 -05:00
|
|
|
|
2023-08-31 03:10:30 -05:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
mouse_states[i] = INPUT_UP;
|
2022-12-20 08:16:26 -06:00
|
|
|
}
|
|
|
|
|
2023-04-28 20:55:24 -05:00
|
|
|
char keybuf[50];
|
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
const char *keyname_extd(int key) {
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
if (key > 289 && key < 302) {
|
|
|
|
int num = key - 289;
|
|
|
|
sprintf(keybuf, "f%d", num);
|
|
|
|
return keybuf;
|
2023-08-31 02:05:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key >= 320 && key <= 329) {
|
2023-05-12 13:22:05 -05:00
|
|
|
int num = key - 320;
|
|
|
|
sprintf(keybuf, "kp%d", num);
|
|
|
|
return keybuf;
|
2023-08-31 02:05:06 -05:00
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
switch (key) {
|
|
|
|
case SAPP_KEYCODE_ENTER:
|
|
|
|
return "enter";
|
|
|
|
case SAPP_KEYCODE_ESCAPE:
|
|
|
|
return "escape";
|
|
|
|
case SAPP_KEYCODE_DELETE:
|
|
|
|
return "delete";
|
|
|
|
case SAPP_KEYCODE_INSERT:
|
|
|
|
return "insert";
|
|
|
|
case SAPP_KEYCODE_TAB:
|
|
|
|
return "tab";
|
|
|
|
case SAPP_KEYCODE_RIGHT:
|
|
|
|
return "right";
|
|
|
|
case SAPP_KEYCODE_LEFT:
|
|
|
|
return "left";
|
|
|
|
case SAPP_KEYCODE_UP:
|
|
|
|
return "up";
|
|
|
|
case SAPP_KEYCODE_DOWN:
|
|
|
|
return "down";
|
|
|
|
case SAPP_KEYCODE_LEFT_SHIFT:
|
|
|
|
return "lshift";
|
|
|
|
case SAPP_KEYCODE_RIGHT_SHIFT:
|
|
|
|
return "rshift";
|
|
|
|
case SAPP_KEYCODE_LEFT_CONTROL:
|
|
|
|
return "lctrl";
|
|
|
|
case SAPP_KEYCODE_LEFT_ALT:
|
|
|
|
return "lalt";
|
|
|
|
case SAPP_KEYCODE_RIGHT_CONTROL:
|
|
|
|
return "rctrl";
|
|
|
|
case SAPP_KEYCODE_RIGHT_ALT:
|
|
|
|
return "ralt";
|
|
|
|
case SAPP_KEYCODE_SPACE:
|
|
|
|
return "space";
|
|
|
|
case SAPP_KEYCODE_KP_ADD:
|
|
|
|
return "plus";
|
2023-10-29 16:39:45 -05:00
|
|
|
case '=':
|
|
|
|
return "plus";
|
|
|
|
case '-':
|
|
|
|
return "minus";
|
2023-08-31 02:05:06 -05:00
|
|
|
case SAPP_KEYCODE_KP_SUBTRACT:
|
|
|
|
return "minus";
|
|
|
|
case SAPP_KEYCODE_GRAVE_ACCENT:
|
2023-09-13 16:49:22 -05:00
|
|
|
return "`";
|
2023-08-31 02:05:06 -05:00
|
|
|
case SAPP_KEYCODE_LEFT_BRACKET:
|
|
|
|
return "lbracket";
|
|
|
|
case SAPP_KEYCODE_RIGHT_BRACKET:
|
|
|
|
return "rbracket";
|
|
|
|
case SAPP_KEYCODE_BACKSPACE:
|
|
|
|
return "backspace";
|
2023-10-09 13:03:12 -05:00
|
|
|
case SAPP_KEYCODE_PAGE_UP:
|
|
|
|
return "pgup";
|
|
|
|
case SAPP_KEYCODE_PAGE_DOWN:
|
|
|
|
return "pgdown";
|
2023-05-12 13:22:05 -05:00
|
|
|
}
|
|
|
|
|
2023-08-31 03:10:30 -05:00
|
|
|
if (key >= 32 && key <=90) {
|
|
|
|
keybuf[0] = tolower(key);
|
|
|
|
keybuf[1] = '\0';
|
|
|
|
|
|
|
|
return keybuf;
|
|
|
|
}
|
2023-05-12 13:22:05 -05:00
|
|
|
|
|
|
|
return "NULL";
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
2022-12-16 11:54:05 -06:00
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
void call_input_down(int *key) {
|
2023-09-06 17:48:08 -05:00
|
|
|
JSValue argv[3];
|
2023-10-16 09:40:43 -05:00
|
|
|
argv[0] = jstr("emacs");
|
|
|
|
argv[1] = jstr(keyname_extd(*key));
|
|
|
|
argv[2] = input2js(4);
|
2023-09-06 17:48:08 -05:00
|
|
|
script_callee(pawn_callee, 3, argv);
|
2023-04-25 14:59:26 -05:00
|
|
|
}
|
|
|
|
|
2022-12-22 16:58:06 -06:00
|
|
|
/* This is called once every frame - or more if we want it more! */
|
2023-05-12 13:22:05 -05:00
|
|
|
void input_poll(double wait) {
|
2023-09-06 17:48:08 -05:00
|
|
|
for (int i = 0; i < arrlen(downkeys); i++)
|
|
|
|
call_input_down(&downkeys[i]);
|
2022-12-22 16:58:06 -06:00
|
|
|
}
|
|
|
|
|
2023-02-13 08:30:35 -06:00
|
|
|
int key_is_num(int key) {
|
|
|
|
return key <= 57 && key >= 48;
|
|
|
|
}
|
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
void cursor_hide() { sapp_show_mouse(0); }
|
|
|
|
void cursor_show() { sapp_show_mouse(1); }
|
2022-08-26 09:19:17 -05:00
|
|
|
|
2023-08-31 02:05:06 -05:00
|
|
|
int action_down(int key) { return key_states[key] == INPUT_DOWN; }
|
|
|
|
int action_up(int key) { return key_states[key] == INPUT_UP; }
|
2022-12-16 11:54:05 -06:00
|
|
|
|
|
|
|
void quit() {
|
2023-09-12 17:19:46 -05:00
|
|
|
sapp_quit();
|
2022-12-16 13:29:50 -06:00
|
|
|
}
|