events.md 6.5 KB

Event Handling

ui_event_t *ui_event(ui_t *ctx, ui_form_t *form);

This is the heart of SMGUI, this function queries the events and also adds an UI layer on top with the desired form [layout] to the window.

Parameter Description
ctx Pointer to UI context
form Form [layout]

Returns NULL if the main loop should exit, an event otherwise.

If you change one of the variables that the passed form references, then you must call ui_refresh() before calling ui_event() to force a [redrawing].

The returned event should be parsed with a switch on evt->type, because most fields depend on the type.

No Event

Nothing happened.

Parameter Description
evt->type UI_EVT_NONE (0)

Mouse

This is generated whenever a mouse button is pressed or released.

Parameter Description
evt->type UI_EVT_MOUSE
evt->btn Current button's state
evt->x Current mouse X position
evt->y Current mouse Y position

The btn field is a bitfield with the following values:

Define Description
UI_BTN_L Left mouse button
UI_BTN_M Middle mouse button
UI_BTN_R Right mouse button
UI_BTN_U Wheel scrolled up
UI_BTN_D Wheel scrolled down
UI_BTN_A Wheel scrolled left
UI_BTN_B Wheel scrolled right
UI_BTN_RELEASE set if this is a release event

If you want to know the mouse's position without an event, then you can use

int ui_getmouse(ui_t *ctx, int *x, int *y);
Parameter Description
ctx Pointer to UI context
x Pointer to store X position
y Pointer to store Y position

Returns 0 on success, error code otherwise.

Gamepad

This is generated whenever the gamepad state changes.

Parameter Description
evt->type UI_EVT_GAMEPAD
evt->btn Current button's state
evt->x Left joystick X position
evt->y Left joystick Y position
evt->rx Right joystick X position
evt->ry Right joystick Y position
evt->key[0] Gamepad's index if there's more than one

Joystick coordinates are signed, and in the range -32768 .. +32768. The btn field is a bitfield with the following values:

Define Description
UI_BTN_A The 'A'/cross button state
UI_BTN_B The 'B'/circle button state
UI_BTN_X The 'X'/square button state
UI_BTN_Y The 'Y'/triangle button state
UI_BTN_L DPad left button state
UI_BTN_R DPad right button state
UI_BTN_U DPad up button state
UI_BTN_D DPad down button state
UI_BTN_BA Back button state
UI_BTN_ST Start button state
UI_BTN_GU Guide button state
UI_BTN_LT Left thumb button state
UI_BTN_RT Right thumb button state
UI_BTN_LS Left shoulder button state
UI_BTN_RS Right shoulder button state

Keyboard

This is generated whenever a key is pressed on the keyboard.

Parameter Description
evt->type UI_EVT_KEY
evt->btn Modifier key's state
evt->key Pressed key

The returned key is an UTF-8 character, or (in case of special keys) an invalid UTF-8 sequence with the key's name. To distinguish, check if key[1] is non-zero and the most significant bit in key[0] is set (valid UTF-8) or clear (a key name).

HINT: There's no mapping with magic defines, to figure out what code a certain key generates, just print evt->key.

The btn field is a bitfield with the following values:

Define Description
UI_BTN_SHIFT One of the Shift keys is pressed
UI_BTN_CONTROL One of the Control keys is pressed
UI_BTN_ALT One of the Alt keys is pressed (right key is often called AltGr)
UI_BTN_GUI One of the GUI keys is pressed

For the modifier keys (LShift, RShift, LCtrl, RCtrl, LAlt, RAlt, LGui and RGui) you'll also receive a key release event, with UI_BTN_RELEASE being set. Other keys only generate a key press event.

Dropped File

This is generated whenever a file is dropped on the window.

Parameter Description
evt->type UI_EVT_DROP
evt->x Current mouse X position
evt->y Current mouse Y position
evt->fn Path of the file

Window Resize

This is generated whenever the window is resized.

Parameter Description
evt->type UI_EVT_RESIZE
evt->x New window width
evt->y New window height

Clipboard

Paste

To query the clipboard (after you have received a [keyboard] event with Paste key), call

char *ui_getclipboard(ui_t *ctx);
Parameter Description
ctx Pointer to UI context

Returns NULL if the clipboard is empty, otherwise the content in a newly allocated buffer. It is the caller's duty to free this buffer after finished using it.

Copy

To set the clipboard (after you have received Cut or Copy key), call

char *ui_setclipboard(ui_t *ctx, char *str);
Parameter Description
ctx Pointer to UI context
str String to copy to clipboard

Returns 0 on success, error code otherwise.