Skip to main content

Event System (event)

The input event abstraction layer for the window subsystem, unifying keyboard, rotary encoder, mouse, touch, joystick, and system events, delivering events from input devices to windows via a FIFO queue.

Event Types

TypeValueDescription
EVENT_TYPE_KEY_DOWN0x0100Key pressed
EVENT_TYPE_KEY_UP0x0101Key released
EVENT_TYPE_ROTARY_STEP0x0200Rotary encoder step
EVENT_TYPE_MOUSE_DOWN0x0300Mouse button pressed
EVENT_TYPE_MOUSE_MOVE0x0301Mouse moved
EVENT_TYPE_MOUSE_UP0x0302Mouse button released
EVENT_TYPE_MOUSE_WHEEL0x0303Mouse wheel scrolled
EVENT_TYPE_TOUCH_BEGIN0x0400Touch started
EVENT_TYPE_TOUCH_MOVE0x0401Touch moved
EVENT_TYPE_TOUCH_END0x0402Touch ended
EVENT_TYPE_JOYSTICK_LEFTSTICK0x0500Joystick left stick
EVENT_TYPE_JOYSTICK_RIGHTSTICK0x0501Joystick right stick
EVENT_TYPE_JOYSTICK_LEFTTRIGGER0x0502Joystick left trigger
EVENT_TYPE_JOYSTICK_RIGHTTRIGGER0x0503Joystick right trigger
EVENT_TYPE_JOYSTICK_BUTTONDOWN0x0504Joystick button pressed
EVENT_TYPE_JOYSTICK_BUTTONUP0x0505Joystick button released
EVENT_TYPE_SYSTEM_EXIT0x1000System exit

Data Structure

struct event_t {
void * device; /* Source device */
enum event_type_t type; /* Event type */
ktime_t timestamp; /* Event timestamp */

union {
struct { uint32_t key; } key_down; /* Key pressed */
struct { uint32_t key; } key_up; /* Key released */
struct { int32_t delta; } rotary_step; /* Rotary step */
struct { int32_t x, y; uint32_t button; } mouse_down; /* Mouse pressed */
struct { int32_t x, y; } mouse_move; /* Mouse moved */
struct { int32_t x, y; uint32_t button; } mouse_up; /* Mouse released */
struct { int32_t dx, dy; } mouse_wheel; /* Mouse wheel */
struct { int32_t x, y; uint32_t id; } touch_begin; /* Touch started */
struct { int32_t x, y; uint32_t id; } touch_move; /* Touch moved */
struct { int32_t x, y; uint32_t id; } touch_end; /* Touch ended */
struct { int32_t x, y; } joystick_left_stick; /* Left stick */
struct { int32_t x, y; } joystick_right_stick; /* Right stick */
struct { int32_t v; } joystick_left_trigger; /* Left trigger */
struct { int32_t v; } joystick_right_trigger; /* Right trigger */
struct { uint32_t button; } joystick_button_down; /* Joystick pressed */
struct { uint32_t button; } joystick_button_up; /* Joystick released */
} e;
};

Key Definitions

Key codes are ASCII-compatible (32-127), with extensions for function keys (1-31) and Latin-1 supplement characters (160-255). Common function keys:

KeyValueDescription
KB_KEY_POWER1Power key
KB_KEY_UP2Direction up
KB_KEY_DOWN3Direction down
KB_KEY_LEFT4Direction left
KB_KEY_RIGHT5Direction right
KB_KEY_ENTER13Enter key
KB_KEY_BACK11Back key
KB_KEY_HOME10Home key
KB_KEY_MENU12Menu key
KB_KEY_DELETE127Delete key

Mouse Buttons

ConstantBitDescription
MOUSE_BUTTON_LEFTbit 0Left button
MOUSE_BUTTON_RIGHTbit 1Right button
MOUSE_BUTTON_MIDDLEbit 2Middle button
MOUSE_BUTTON_X1bit 3Side button 1
MOUSE_BUTTON_X2bit 4Side button 2

Joystick Buttons

Joystick buttons use a bitmask, supporting D-pad, A/B/X/Y, Back/Start/Guide, bumpers, and stick presses:

ConstantBitDescription
JOYSTICK_BUTTON_Abit 4A button
JOYSTICK_BUTTON_Bbit 5B button
JOYSTICK_BUTTON_Xbit 6X button
JOYSTICK_BUTTON_Ybit 7Y button
JOYSTICK_BUTTON_STARTbit 9Start button
JOYSTICK_BUTTON_GUIDEbit 10Guide button

Event Push API

Input device drivers push events to the window system via the following functions:

FunctionDescription
push_event_key_down(device, key)Push key down event
push_event_key_up(device, key)Push key up event
push_event_rotary_step(device, delta)Push rotary encoder step event
push_event_mouse_button_down(device, x, y, button)Push mouse down event
push_event_mouse_button_up(device, x, y, button)Push mouse up event
push_event_mouse_move(device, x, y)Push mouse move event
push_event_mouse_wheel(device, dx, dy)Push mouse wheel event
push_event_touch_begin(device, x, y, id)Push touch begin event
push_event_touch_move(device, x, y, id)Push touch move event
push_event_touch_end(device, x, y, id)Push touch end event
push_event_joystick_left_stick(device, x, y)Push left stick event
push_event_joystick_right_stick(device, x, y)Push right stick event
push_event_joystick_left_trigger(device, v)Push left trigger event
push_event_joystick_right_trigger(device, v)Push right trigger event
push_event_joystick_button_down(device, button)Push joystick button down event
push_event_joystick_button_up(device, button)Push joystick button up event

Event Consumption

Events are retrieved from the window's event FIFO queue via window_pump_event():

struct event_t e;
while(window_pump_event(w, &e))
{
switch(e.type)
{
case EVENT_TYPE_KEY_DOWN:
LOG("key down: %d\n", e.e.key_down.key);
break;
case EVENT_TYPE_TOUCH_BEGIN:
LOG("touch: (%d, %d) id=%u\n", e.e.touch_begin.x, e.e.touch_begin.y, e.e.touch_begin.id);
break;
default:
break;
}
}

Notes

  • All push_event_* functions internally construct an event_t structure and call push_event() to inject it into the window event queue
  • The device field identifies the source device, useful for distinguishing multiple input devices of the same type
  • Event timestamps are automatically filled via ktime_get()
  • Mouse and joystick buttons use bitmasks, supporting simultaneous button presses
  • Touch events use id to distinguish different touch points in multi-touch scenarios
  • Joystick coordinates and trigger values are raw ADC values; calibration and normalization are required before use