Add support for key press and release events.
This commit is contained in:
55
cpp/events/events_x11.cpp
Normal file
55
cpp/events/events_x11.cpp
Normal 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)) {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user