Add support for key press and release events.

This commit is contained in:
Roz K 2022-12-23 10:18:26 +01:00
parent ffffffaa79
commit 61cfdbccf3
Signed by: roz
GPG Key ID: 51FBF4E483E1C822
5 changed files with 147 additions and 4 deletions

View File

@ -1,4 +1,24 @@
SOURCES = cpp/opengl/render_context_glx.cpp cpp/opengl/render_opengles.cpp cpp/math.cpp
# Copyright (C) 2022 RozK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
SOURCES = \
cpp/opengl/render_context_glx.cpp \
cpp/opengl/render_opengles.cpp \
cpp/events/events_x11.cpp \
cpp/math.cpp
OUTPUTFILE = engine.so
CXXFLAGS = -fpic -Wall -Werror -O2 -flto -fomit-frame-pointer -ffast-math -funroll-loops -fno-rtti -fno-exceptions

View File

@ -344,3 +344,27 @@ destroy_shader.argtypes = (
ctypes.c_void_p,) # shader
terminate = _engine.rk_terminate
EVENT_KEY_PRESS = 0
EVENT_KEY_RELEASE = 1
class _EventKey(ctypes.Structure):
_fields_ = (
('keycode', ctypes.c_uint),
)
class _AllEvents(ctypes.Union):
_fields_ = (
('key', _EventKey),
)
class Event(ctypes.Structure):
_fields_ = ('type', ctypes.c_uint), ('data', _AllEvents)
consume_events = _engine.rk_consume_events
consume_events.restype = ctypes.c_uint
consume_events.argtypes = (
ctypes.POINTER(Event), # events
ctypes.c_uint) # max_events
flush_events = _engine.rk_flush_events

43
cpp/events.hpp Normal file
View File

@ -0,0 +1,43 @@
// Copyright (C) 2022 RozK
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef _RK_ENGINE_EVENTS_H
#define _RK_ENGINE_EVENTS_H
#include "types.hpp"
enum rk_event_type : rk_uint {
RK_EVENT_KEY_PRESS = 0,
RK_EVENT_KEY_RELEASE = 1
};
struct rk_event_key {
rk_uint keycode;
};
struct rk_event {
rk_event_type type;
union {
rk_event_key key;
};
};
RK_EXPORT rk_uint rk_consume_events(
rk_event * events,
rk_uint max_events);
RK_EXPORT void rk_flush_events();
#endif // _RK_ENGINE_EVENTS_H

55
cpp/events/events_x11.cpp Normal file
View File

@ -0,0 +1,55 @@
// Copyright (C) 2022 RozK
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "../events.hpp"
#include <X11/Xlib.h>
unsigned rk_events_mask = KeyPressMask | KeyReleaseMask;
extern Display * rk_display;
extern Window rk_window;
rk_uint rk_consume_events(
rk_event * events,
rk_uint max_events) {
if (!events || !max_events) {
return 0;
}
XEvent x11_event;
unsigned nevents = 0;
for ( ; nevents < max_events ; ++nevents) {
if (XCheckWindowEvent(rk_display, rk_window, rk_events_mask, &x11_event)) {
rk_event & event = events[nevents];
switch (x11_event.type) {
case KeyPress:
event.type = RK_EVENT_KEY_PRESS;
event.key.keycode = x11_event.xkey.keycode;
break;
case KeyRelease:
event.type = RK_EVENT_KEY_RELEASE;
event.key.keycode = x11_event.xkey.keycode;
break;
}
} else {
break;
}
}
return nevents;
}
void rk_flush_events() {
XEvent x11_event;
while (XCheckWindowEvent(rk_display, rk_window, rk_events_mask, &x11_event)) {
}
}

View File

@ -21,9 +21,10 @@
#include <GLES3/gl32.h>
#include <X11/Xlib.h>
static Display * rk_display = nullptr;
Display * rk_display = nullptr;
Window rk_window = 0;
extern unsigned rk_events_mask;
static Colormap rk_colormap = 0;
static Window rk_window = 0;
static GLXContext rk_context = nullptr;
static bool rk_error_occured = false;
@ -146,7 +147,7 @@ rk_window_t rk_create_context(
swa.colormap = rk_colormap;
swa.background_pixmap = None;
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
swa.event_mask = rk_events_mask;
rk_window = XCreateWindow(rk_display, RootWindow(rk_display, vi->screen),
0, 0, width, height, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask, &swa);