rk_engine/cpp/events/events_x11.cpp

81 lines
2.8 KiB
C++

// 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>
extern Display * rk_display;
extern Window rk_window;
extern XIC rk_input_context;
unsigned rk_events_mask = KeyPressMask | KeyReleaseMask;
rk_uint rk_consume_events(
rk_event * events,
rk_uint max_events) {
if (!events || !max_events) {
return 0;
}
XEvent x11_event;
unsigned nevents = 0;
wchar_t string[256];
KeySym keysym;
Status status;
while (nevents < max_events && XCheckWindowEvent(rk_display, rk_window, rk_events_mask, &x11_event)) {
if (XFilterEvent(&x11_event, 0)) {
continue;
}
rk_event & event = events[nevents++];
switch (x11_event.type) {
case KeyPress:
event.type = RK_EVENT_KEY_PRESS;
event.key.code = x11_event.xkey.keycode;
XwcLookupString(rk_input_context, &x11_event.xkey, string, 256, &keysym, &status);
switch (status) {
case XLookupChars:
event.key.symbol = XLookupKeysym(&x11_event.xkey, 0);
event.key.character = string[0];
break;
case XLookupKeySym:
event.key.symbol = keysym;
event.key.character = 0;
break;
case XLookupBoth:
event.key.symbol = keysym;
event.key.character = string[0];
break;
default:
event.key.symbol = 0;
event.key.character = 0;
break;
}
break;
case KeyRelease:
event.type = RK_EVENT_KEY_RELEASE;
event.key.code = x11_event.xkey.keycode;
event.key.symbol = XLookupKeysym(&x11_event.xkey, 0);
event.key.character = 0;
break;
}
}
return nevents;
}
void rk_flush_events() {
XEvent x11_event;
while (XCheckWindowEvent(rk_display, rk_window, rk_events_mask, &x11_event)) {
}
}