add sokol version of default input module.

This commit is contained in:
Tuomas Katajisto 2025-05-01 13:33:31 +03:00
parent bc0fd22cf8
commit b33e491293
2 changed files with 683 additions and 0 deletions

266
modules/Input/module.jai Normal file
View File

@ -0,0 +1,266 @@
// Input handler routines, platform-independent.
// The Jai Input module ported to use sokol events.
#load "sokol.jai";
Event_Type :: enum u32 { // If we set this to u8, our packing will stop matching C's.
UNINITIALIZED :: 0;
KEYBOARD :: 1;
TEXT_INPUT :: 2;
WINDOW :: 3;
MOUSE_WHEEL :: 4;
QUIT :: 5;
DRAG_AND_DROP_FILES :: 6;
TOUCH :: 7;
}
Key_Current_State :: enum_flags u32 {
NONE :: 0x0;
DOWN :: 0x1;
START :: 0x4;
END :: 0x8;
}
// We reserve 32 buttons for each gamepad.
GAMEPAD_BUTTON_COUNT :: 32;
Key_Code :: enum u32 {
UNKNOWN :: 0;
// Non-textual keys that have placements in the ASCII table
// (and thus in Unicode):
BACKSPACE :: 8;
TAB :: 9;
LINEFEED :: 10;
ENTER :: 13;
ESCAPE :: 27;
SPACEBAR :: 32;
// The letters A-Z live in here as well and may be returned
// by keyboard events.
DELETE :: 127;
ARROW_UP :: 128;
ARROW_DOWN :: 129;
ARROW_LEFT :: 130;
ARROW_RIGHT :: 131;
PAGE_UP :: 132;
PAGE_DOWN :: 133;
HOME :: 134;
END :: 135;
INSERT :: 136;
PAUSE :: 137;
SCROLL_LOCK :: 138;
ALT;
CTRL;
SHIFT;
CMD;
META :: CMD;
F1;
F2;
F3;
F4;
F5;
F6;
F7;
F8;
F9;
F10;
F11;
F12;
F13;
F14;
F15;
F16;
F17;
F18;
F19;
F20;
F21;
F22;
F23;
F24;
PRINT_SCREEN;
MOUSE_BUTTON_LEFT;
MOUSE_BUTTON_MIDDLE;
MOUSE_BUTTON_RIGHT;
MOUSE_WHEEL_UP;
MOUSE_WHEEL_DOWN;
// We reserve button codes for up to 4 gamepads.
GAMEPAD_0_BEGIN;
GAMEPAD_0_END :: GAMEPAD_0_BEGIN + xx GAMEPAD_BUTTON_COUNT;
GAMEPAD_1_BEGIN;
GAMEPAD_1_END :: GAMEPAD_1_BEGIN + xx GAMEPAD_BUTTON_COUNT;
GAMEPAD_2_BEGIN;
GAMEPAD_2_END :: GAMEPAD_2_BEGIN + xx GAMEPAD_BUTTON_COUNT;
GAMEPAD_3_BEGIN;
GAMEPAD_3_END :: GAMEPAD_3_BEGIN + xx GAMEPAD_BUTTON_COUNT;
TOUCH;
// WARNING!
//
// We make an array whose size is controlled
// by the last enum value in this array, so if you make
// really big values to match Unicode code points, our
// memory usage will become quite sorry.
//
// -jblow, 19 March 2017
//
}
// Modifier_Flags used to use #place, but I rewrote it to use a union
// instead .... it is not necessarily clearer though! So I am switching
// it back for now...
Event :: struct {
Modifier_Flags :: union {
// Eventually we'd like the *_pressed modifiers to be 1 bit each,
// but still be nameable as booleans. But for now they're 1 byte each.
// You can compare them as a u32 using the 'packed' member.
struct {
// @@ This is confusing. Below key_pressed means the key was just pressed, here _pressed means the key is held down.
shift_pressed := false;
ctrl_pressed := false;
alt_pressed := false;
cmd_meta_pressed := false; // Cmd on macOS, Meta on Linux
}
packed: u32 = 0;
}
type: Event_Type = Event_Type.UNINITIALIZED;
// If keyboard event:
key_pressed: u32; // If not pressed, it's a key release.
key_code := Key_Code.UNKNOWN;
using modifier_flags: Modifier_Flags; // Only set for Event_Type.KEYBOARD.
utf32: u32; // If TEXT_INPUT.
repeat := false; // If KEYBOARD event.
text_input_count: u16; // If KEYBOARD event that also generated TEXT_INPUT events, this will tell you how many TEXT_INPUT events after this KEYBOARD event were generated.
typical_wheel_delta: s32; // Used only for mouse events.
wheel_delta: s32; // Used only for mouse events.
files: [..] string; // Used only for drag and drop events. Both the array and its contents are heap-allocated, lives until events are reset for the next frame.
touch_type: enum u8 { // Used only for touch events.
MOVED :: 0;
PRESSED :: 1;
RELEASED :: 2;
}
touch_index: s32; // Used only for touch events. Index of which touch this is.
}
// Per-frame mouse deltas:
mouse_delta_x: int;
mouse_delta_y: int;
mouse_delta_z: int;
events_this_frame: [..] Event;
input_button_states: [NUM_BUTTON_STATES] Key_Current_State;
input_application_has_focus := false;
NUM_BUTTON_STATES :: #run enum_highest_value(Key_Code) + 1;
Window_Resize_Record :: struct {
window: Window_Type;
width: s32;
height: s32;
}
Window_Move_Record :: struct {
window: Window_Type;
x: s32;
y: s32;
}
get_window_resizes :: () -> [] Window_Resize_Record {
// The return value here will stick around in memory until the next call
// to get_window_resizes (from any thread. Actually this whole module does
// not deal with threading, so don't do that!)
if resizes_to_free array_reset(*resizes_to_free);
if !pending_resizes return .[];
array_copy(*resizes_to_free, pending_resizes);
this_allocation_is_not_a_leak(resizes_to_free.data);
pending_resizes.count = 0;
return resizes_to_free;
}
get_window_moves :: () -> [] Window_Move_Record {
// See notes on get_window_resizes. This works the same way.
if moves_to_free array_reset(*moves_to_free);
if !pending_moves return .[];
array_copy(*moves_to_free, pending_moves);
pending_moves.count = 0;
return moves_to_free;
}
input_per_frame_event_and_flag_update :: () {
// Called once per frame, probably.
for events_this_frame {
allocator := it.files.allocator; // Same allocator for filenames and the array itself.
for file: it.files {
free(file,, allocator);
}
array_free(it.files,, allocator);
}
array_reset(*events_this_frame);
mask := ~Key_Current_State.START;
end_mask := ~(Key_Current_State.END | .DOWN | .START);
// @Speed: Could just keep a list of who is not currently set.
for * input_button_states {
if it.* & .END {
it.* &= end_mask;
} else {
it.* &= mask;
}
}
mouse_delta_x = 0;
mouse_delta_y = 0;
mouse_delta_z = 0;
}
#scope_module
pending_moves: [..] Window_Move_Record;
moves_to_free: [..] Window_Move_Record;
pending_resizes: [..] Window_Resize_Record;
resizes_to_free: [..] Window_Resize_Record;
#import "Basic";
#import "Window_Type";

417
modules/Input/sokol.jai Normal file
View File

@ -0,0 +1,417 @@
#import "String"; // @Cleanup: Remove this dependency on String?
#import,dir "../sokol-jai/sokol/app";
input_mouse_x : float;
input_mouse_y : float;
set_custom_cursor_handling :: (is_custom: bool) {
}
get_key_code :: (key: sapp_keycode) -> Key_Code {
using Key_Code;
// non-ascii ordered as in ./module.jai/Key_Code
if key == .BACKSPACE
return BACKSPACE;
if key == .TAB
return TAB;
if key == .ENTER
return ENTER;
if key == .ESCAPE
return ESCAPE;
if key == .DELETE
return DELETE;
if key == .SPACE
return SPACEBAR;
if key == .UP
return ARROW_UP;
if key == .DOWN
return ARROW_DOWN;
if key == .LEFT
return ARROW_LEFT;
if key == .RIGHT
return ARROW_RIGHT;
if key == .PAGE_UP
return PAGE_UP;
if key == .PAGE_DOWN
return PAGE_DOWN;
if key == .HOME
return HOME;
if key == .END
return END;
if key == .INSERT
return INSERT;
if key == .PAUSE
return PAUSE;
if key == .SCROLL_LOCK
return SCROLL_LOCK;
if (key == .LEFT_ALT) || (key == .RIGHT_ALT)
return ALT;
if (key == .LEFT_CONTROL) || (key == .RIGHT_CONTROL)
return CTRL;
if (key == .LEFT_SHIFT) || (key == .RIGHT_SHIFT)
return SHIFT;
if (key == .LEFT_SUPER) || (key == .RIGHT_SUPER)
return META;
if key >= sapp_keycode.F1 && key <= sapp_keycode.F25
return (cast(Key_Code) (key - sapp_keycode.F1))+F1;
if key == .PRINT_SCREEN
return PRINT_SCREEN;
if (key >= #char "a") && (key <= #char "z") return cast(Key_Code) (key - 0x20);
if key > 32 && key < 127 return cast(Key_Code) key;
return UNKNOWN;
}
get_key_code_for_mouse :: (mb: sapp_mousebutton) -> Key_Code {
if mb == .LEFT
return .MOUSE_BUTTON_LEFT;
if mb == .RIGHT
return .MOUSE_BUTTON_RIGHT;
if mb == .MIDDLE
return .MOUSE_BUTTON_MIDDLE;
return .UNKNOWN;
}
#scope_export
handle_sokol_event :: (event: *sapp_event) {
new_event: Event;
if event.type == {
case .KEY_DOWN;
mapped := get_key_code(event.key_code);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 1;
input_button_states[mapped] = .START | .DOWN;
case .KEY_UP;
mapped := get_key_code(event.key_code);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 0;
input_button_states[mapped] = .END;
case .MOUSE_DOWN;
mapped := get_key_code_for_mouse(event.mouse_button);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 1;
input_button_states[mapped] = .START | .DOWN;
case .MOUSE_UP;
mapped := get_key_code_for_mouse(event.mouse_button);
new_event.type = .KEYBOARD;
new_event.key_code = mapped;
new_event.key_pressed = 0;
input_button_states[mapped] = .END;
case .CHAR;
new_event.type = .TEXT_INPUT;
new_event.utf32 = event.char_code;
case .FOCUSED;
input_application_has_focus = true;
return;
case .UNFOCUSED;
input_application_has_focus = false;
return;
case .MOUSE_MOVE;
input_mouse_x = event.mouse_x;
input_mouse_y = event.mouse_y;
return;
}
array_add(*events_this_frame, new_event);
}
// using Key_Code;
// input_per_frame_event_and_flag_update();
// display := x_global_display;
// XLockDisplay(display);
// key_press_repeat := false;
// while XPending(display) {
// xev: XEvent;
// XNextEvent(display, *xev);
// if XFilterEvent(*xev, None) continue;
// if xev.type == {
// case ClientMessage;
// message_type := xev.xclient.message_type;
// if message_type == XdndEnter {
// drop_handled := maybe_handle_xdnd_drop_events(display, *xev, *drop_info);
// if drop_handled continue;
// } else if message_type == x_global_wm_protocols {
// message0 := cast(Atom) xev.xclient.data.l[0];
// // This can't be a switch, because the values are not constant!
// // Except come on guys, every single implementation of X11 in the universe
// // is going to agree on these values. Why are we pretending they are not constant?
// // How about if we just look them up once and then hardcode them into this program?
// // We'd save startup time...
// if message0 == x_global_wm_delete_window {
// event: Event;
// event.type = .QUIT;
// array_add(*events_this_frame, event);
// }
// }
// case KeyPress;
// event: Event;
// shift: u32;
// if xev.xkey.state & ShiftMask
// shift = 1;
// if xev.xkey.state & ControlMask
// event.ctrl_pressed = true;
// if xev.xkey.state & Mod1Mask
// event.alt_pressed = true;
// status: Status;
// keysym: KeySym;
// text: [16]u32;
// lookup_count := XwcLookupString(x_global_input_context, *xev.xkey, xx text.data, xx text.count, *keysym, *status);
// has_keysym := (status == XLookupKeySym || status == XLookupBoth);
// has_text := (status == XLookupChars || status == XLookupBoth);
// if has_keysym {
// if xev.xkey.state & ShiftMask event.shift_pressed = true;
// if xev.xkey.state & ControlMask event.ctrl_pressed = true;
// if xev.xkey.state & Mod1Mask event.alt_pressed = true;
// if xev.xkey.state & Mod4Mask event.cmd_meta_pressed = true;
// event.type = .KEYBOARD;
// event.key_pressed = 1;
// event.key_code = get_key_code(keysym);
// for 0..lookup_count - 1 {
// utf32 := text[it];
// if utf32 >= 32 && utf32 != 127 event.text_input_count += 1;
// }
// if event.key_code == {
// case .SHIFT; event.shift_pressed = true;
// case .CTRL; event.ctrl_pressed = true;
// case .ALT; event.alt_pressed = true;
// case .META; event.cmd_meta_pressed = true;
// }
// event.repeat = key_press_repeat;
// key_press_repeat = false;
// array_add(*events_this_frame, event);
// input_button_states[event.key_code] = .START | .DOWN;
// }
// event.shift_pressed = cast(bool) shift;
// if has_text {
// for 0..lookup_count - 1 {
// utf32 := text[it];
// if utf32 < 32 || utf32 == 127 continue;
// char_event: Event;
// char_event.type = .TEXT_INPUT;
// char_event.key_pressed = 1;
// char_event.key_code = get_key_code(keysym);
// char_event.utf32 = utf32;
// array_add(*events_this_frame, char_event);
// }
// }
// case KeyRelease;
// // For some odd reason X11 generates KeyRelease followed by a near identical KeyPress to simulate repeat events so we have to filter that out
// if XEventsQueued(display, QueuedAfterReading) {
// nev: XEvent;
// XPeekEvent(display, *nev);
// if nev.type == KeyPress
// && nev.xkey.time == xev.xkey.time
// && nev.xkey.keycode == xev.xkey.keycode {
// // This is a repeat, so we ignore the KeyRelease and set the repeat flag.
// key_press_repeat = true;
// continue;
// }
// }
// event: Event;
// shift: u32;
// if xev.xkey.state & ShiftMask
// shift = 1;
// if xev.xkey.state & ControlMask
// event.ctrl_pressed = true;
// if xev.xkey.state & Mod1Mask
// event.alt_pressed = true;
// keysym := XkbKeycodeToKeysym(display, xx xev.xkey.keycode, 0, 0 /* English lowercase group*/);
// event.type = .KEYBOARD;
// event.key_pressed = 0;
// event.shift_pressed = cast(bool) shift;
// event.key_code = get_key_code(keysym);
// input_button_states[event.key_code] = .END;
// array_add(*events_this_frame, event);
// case ButtonPress;
// event: Event;
// event.type = .KEYBOARD;
// event.key_pressed = 1;
// button := xev.xbutton.button;
// // 6/7 is horizontal/secondary wheel, don't handle it
// if (button == 6) || (button == 7) continue;
// // button 4/5 is mwheel up/down
// if (button == 4) || (button == 5) {
// event.type = .MOUSE_WHEEL;
// event.typical_wheel_delta = WHEEL_DELTA;
// event.wheel_delta = event.typical_wheel_delta * ifx button & 1 then cast(s32) - 1 else 1;
// array_add(*events_this_frame, event);
// mouse_delta_z += event.wheel_delta;
// continue;
// }
// if button == Button1 {
// event.key_code = MOUSE_BUTTON_LEFT;
// } else if button == Button2 {
// event.key_code = MOUSE_BUTTON_MIDDLE;
// } else if button == Button3 {
// event.key_code = MOUSE_BUTTON_RIGHT;
// }
// input_button_states[event.key_code] = .START | .DOWN;
// array_add(*events_this_frame, event);
// case ButtonRelease;
// // it seems that mouse input doesnt generate repeat events so we dont have to peek the queue
// event: Event;
// event.type = .KEYBOARD;
// event.key_pressed = 0;
// button := xev.xbutton.button;
// if (button >= 4) && (button <= 7) continue; // No action on button release of mouse wheels.
// if button == Button1 {
// event.key_code = MOUSE_BUTTON_LEFT;
// } else if button == Button2 {
// event.key_code = MOUSE_BUTTON_MIDDLE;
// } else if button == Button3 {
// event.key_code = MOUSE_BUTTON_RIGHT;
// }
// input_button_states[event.key_code] = .END;
// array_add(*events_this_frame, event);
// case SelectionRequest;
// selreq := cast(*XSelectionRequestEvent) *xev;
// out: XEvent;
// selnot := cast(*XSelectionEvent) *out;
// selnot.type = SelectionNotify;
// selnot.requestor = selreq.requestor;
// selnot.selection = selreq.selection;
// selnot.target = selreq.target;
// selnot.time = selreq.time;
// selnot.property = None;
// if x_window_is_ours(x_global_display, selreq.owner) {
// if selreq.target == x_global_xa_utf8 {
// selnot.property = selreq.property;
// text_data := x_global_clipboard_buffer.text_data;
// XChangeProperty(selreq.display, selreq.requestor, selreq.property, selreq.target, 8, PropModeReplace,
// text_data.data, cast(s32) text_data.count);
// } else if selreq.target == x_global_xa_targets {
// selnot.property = selreq.property;
// atoms: [..] Atom;
// array_add(*atoms, x_global_xa_utf8);
// array_add(*atoms, x_global_xa_targets);
// array_add(*atoms, x_global_xa_multiple);
// if x_global_clipboard_buffer.rgb_data {
// array_add(*atoms, x_global_image_bmp);
// }
// XChangeProperty(selreq.display, selreq.requestor, selreq.property, x_global_xa_atom, 32, PropModeReplace,
// xx atoms.data, cast(s32) atoms.count);
// array_reset(*atoms);
// } else if selreq.target == x_global_image_bmp {
// #import "stb_image_write";
// Data :: struct {
// _context: *#Context;
// data: [..] u8;
// }
// write_func :: (data_pointer: *void, _data: *void, size: s32) #c_call {
// data := cast(*Data) data_pointer;
// push_context data._context.* {
// data8 := cast(*u8) _data;
// for 0..size-1 {
// array_add(*data.data, data8[it]);
// }
// }
// }
// data: Data;
// data._context = *context;
// w := x_global_clipboard_buffer.width;
// h := x_global_clipboard_buffer.height;
// comp: s32 = 3;
// stride := x_global_clipboard_buffer.pitch;
// stbi_write_bmp_to_func(write_func, *data, w, h, comp, x_global_clipboard_buffer.rgb_data);
// selnot.property = selreq.property;
// XChangeProperty(selreq.display, selreq.requestor, selreq.property, selreq.target, 8, PropModeReplace,
// xx data.data.data, cast(s32) data.data.count);
// array_reset(*data.data);
// } else {
// // print("GOT REQ: %\n", to_string(XGetAtomName(x_global_display, selreq.target)));
// }
// }
// XSendEvent(selreq.display, selreq.requestor, True, 0, *out);
// case ConfigureNotify;
// config := cast(*XConfigureEvent) *xev;
// add_resize_record(config.window, config.width, config.height);
// case FocusIn;
// input_application_has_focus = true;
// case FocusOut;
// input_application_has_focus = false;
// }
// }
// XUnlockDisplay(display);
WHEEL_DELTA :: 120;
/*
Copyright 2021, Thekla, Inc. Modified to work with Sokol by Tuomas Katajisto.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/