SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
input_x11.c
Go to the documentation of this file.
1#include "linux_internal.h"
2#include "sbgl_types.h"
3#include <X11/Xlib.h>
4#include <X11/keysym.h>
5#include <string.h>
6
7static SBGL_Scancode x11_keysym_to_scancode(KeySym keysym) {
8 if (keysym >= 'a' && keysym <= 'z') return (SBGL_Scancode)(SBGL_SCANCODE_A + (keysym - 'a'));
9 if (keysym >= 'A' && keysym <= 'Z') return (SBGL_Scancode)(SBGL_SCANCODE_A + (keysym - 'A'));
10 if (keysym == '0') return SBGL_SCANCODE_0;
11 if (keysym >= '1' && keysym <= '9') return (SBGL_Scancode)(SBGL_SCANCODE_1 + (keysym - '1'));
12
13 switch (keysym) {
14 case XK_minus: return SBGL_SCANCODE_MINUS;
15 case XK_equal: return SBGL_SCANCODE_EQUAL;
16 case XK_Escape: return SBGL_SCANCODE_ESCAPE;
17 case XK_Return: return SBGL_SCANCODE_RETURN;
18 case XK_BackSpace: return SBGL_SCANCODE_BACKSPACE;
19 case XK_Tab: return SBGL_SCANCODE_TAB;
20 case XK_space: return SBGL_SCANCODE_SPACE;
21 case XK_Shift_L: return SBGL_SCANCODE_LSHIFT;
22 case XK_Control_L: return SBGL_SCANCODE_LCTRL;
23 case XK_Alt_L: return SBGL_SCANCODE_LALT;
24 case XK_Up: return SBGL_SCANCODE_UP;
25 case XK_Down: return SBGL_SCANCODE_DOWN;
26 case XK_Left: return SBGL_SCANCODE_LEFT;
27 case XK_Right: return SBGL_SCANCODE_RIGHT;
28 default: return SBGL_SCANCODE_UNKNOWN;
29 }
30}
31
32void x11_internal_process_event(XEvent* event, sbgl_Window* window) {
33 sbgl_InputState* input = window->input;
34 switch (event->type) {
35 case KeyPress: {
36 KeySym keysym = XLookupKeysym(&event->xkey, 0);
38 if (code < SBGL_SCANCODE_MAX) {
39 if (!input->keysDown[code]) input->keysPressed[code] = true;
40 input->keysDown[code] = true;
41 }
42 break;
43 }
44 case KeyRelease: {
45 // X11 Auto-repeat handling
46 if (XEventsQueued(window->display, QueuedAfterReading)) {
47 XEvent next;
48 XPeekEvent(window->display, &next);
49 if (next.type == KeyPress && next.xkey.time == event->xkey.time && next.xkey.keycode == event->xkey.keycode) {
50 XNextEvent(window->display, event); // Consume repeat
51 break;
52 }
53 }
54 KeySym keysym = XLookupKeysym(&event->xkey, 0);
56 if (code < SBGL_SCANCODE_MAX) input->keysDown[code] = false;
57 break;
58 }
59 case MotionNotify: {
60 input->mouseX = event->xmotion.x;
61 input->mouseY = event->xmotion.y;
62 break;
63 }
64 case ButtonPress:
65 case ButtonRelease: {
66 bool down = (event->type == ButtonPress);
67 int btn = -1;
68 if (event->xbutton.button == Button1) btn = SBGL_MOUSE_BUTTON_LEFT;
69 else if (event->xbutton.button == Button3) btn = SBGL_MOUSE_BUTTON_RIGHT;
70 else if (event->xbutton.button == Button2) btn = SBGL_MOUSE_BUTTON_MIDDLE;
71 if (btn != -1 && btn < SBGL_MOUSE_BUTTON_MAX) input->mouseDown[btn] = down;
72 break;
73 }
74 case FocusIn: {
75 window->focused = true;
76 break;
77 }
78 case FocusOut: {
79 window->focused = false;
80 break;
81 }
82 }
83}
84
86 sbgl_InputState* input = window->input;
87 if (!input) return;
88 input->mouseDeltaX = input->mouseX - input->_internalMouseX;
89 input->mouseDeltaY = input->mouseY - input->_internalMouseY;
90 input->_internalMouseX = input->mouseX;
91 input->_internalMouseY = input->mouseY;
92}
SBGL_Scancode
OS-independent physical scancodes.
Definition sbgl_input.h:21
@ SBGL_SCANCODE_UNKNOWN
Definition sbgl_input.h:22
@ SBGL_SCANCODE_0
Definition sbgl_input.h:59
@ SBGL_SCANCODE_SPACE
Definition sbgl_input.h:65
@ SBGL_SCANCODE_RIGHT
Definition sbgl_input.h:70
@ SBGL_SCANCODE_A
Definition sbgl_input.h:23
@ SBGL_SCANCODE_LALT
Definition sbgl_input.h:77
@ SBGL_SCANCODE_LEFT
Definition sbgl_input.h:71
@ SBGL_SCANCODE_LSHIFT
Definition sbgl_input.h:75
@ SBGL_SCANCODE_ESCAPE
Definition sbgl_input.h:62
@ SBGL_SCANCODE_LCTRL
Definition sbgl_input.h:76
@ SBGL_SCANCODE_UP
Definition sbgl_input.h:73
@ SBGL_SCANCODE_DOWN
Definition sbgl_input.h:72
@ SBGL_SCANCODE_TAB
Definition sbgl_input.h:64
@ SBGL_SCANCODE_MINUS
Definition sbgl_input.h:67
@ SBGL_SCANCODE_RETURN
Definition sbgl_input.h:61
@ SBGL_SCANCODE_1
Definition sbgl_input.h:50
@ SBGL_SCANCODE_MAX
Definition sbgl_input.h:137
@ SBGL_SCANCODE_BACKSPACE
Definition sbgl_input.h:63
@ SBGL_SCANCODE_EQUAL
Definition sbgl_input.h:68
@ SBGL_MOUSE_BUTTON_MIDDLE
Definition sbgl_input.h:146
@ SBGL_MOUSE_BUTTON_MAX
Definition sbgl_input.h:147
@ SBGL_MOUSE_BUTTON_LEFT
Definition sbgl_input.h:144
@ SBGL_MOUSE_BUTTON_RIGHT
Definition sbgl_input.h:145
void x11_internal_process_event(XEvent *event, sbgl_Window *window)
Definition input_x11.c:32
void linux_internal_update_input_states(sbgl_Window *window)
Definition input_x11.c:85
static SBGL_Scancode x11_keysym_to_scancode(KeySym keysym)
Definition input_x11.c:7
Represents the real-time state of physical inputs.
Definition sbgl_input.h:165
bool mouseDown[SBGL_MOUSE_BUTTON_MAX]
Definition sbgl_input.h:168
bool keysPressed[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:167
bool keysDown[SBGL_SCANCODE_MAX]
Definition sbgl_input.h:166
Native X11 window state.
sbgl_InputState * input
Display * display