Split display from render and rework init/terminate.
This commit is contained in:
@ -14,144 +14,168 @@
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "../events.hpp"
|
||||
#include <X11/Xlib.h>
|
||||
#include "events_x11.hpp"
|
||||
#include "../display/display_x11.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
extern Display * rk_display;
|
||||
extern Window rk_window;
|
||||
|
||||
unsigned rk_events_mask = FocusChangeMask |
|
||||
KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
|
||||
|
||||
static XIM rk_input_manager = nullptr;
|
||||
static XIC rk_input_context = nullptr;
|
||||
|
||||
static bool rk_focused = false;
|
||||
static bool rk_keyboard_autorepeat = false;
|
||||
static XKeyboardState rk_previous_keyboard_state;
|
||||
static int rk_previous_accel_numerator, rk_accel_numerator = 1;
|
||||
static int rk_previous_accel_denominator, rk_accel_denominator = 1;
|
||||
static int rk_previous_threshold, rk_accel_threshold = 0;
|
||||
|
||||
static void rk_backup_setup() {
|
||||
XGetKeyboardControl(rk_display, &rk_previous_keyboard_state);
|
||||
XGetPointerControl(rk_display,
|
||||
&rk_previous_accel_numerator, &rk_previous_accel_denominator, &rk_previous_threshold);
|
||||
static void rk_x11_printf(
|
||||
char const * messsage) {
|
||||
printf("[X11] %s\n", messsage);
|
||||
}
|
||||
|
||||
static void rk_restore_setup() {
|
||||
if (AutoRepeatModeOn == rk_previous_keyboard_state.global_auto_repeat) {
|
||||
XAutoRepeatOn(rk_display);
|
||||
static void rk_backup(rk_events_x11 & events) {
|
||||
XKeyboardState backup;
|
||||
XGetKeyboardControl(events.display, &backup);
|
||||
events.backup_autorepeat = (AutoRepeatModeOn == backup.global_auto_repeat);
|
||||
XGetPointerControl(
|
||||
events.display, &events.backup_numerator, &events.backup_denominator, &events.backup_threshold);
|
||||
}
|
||||
|
||||
static void rk_restore(rk_events_x11 & events) {
|
||||
if (events.backup_autorepeat) {
|
||||
XAutoRepeatOn(events.display);
|
||||
} else {
|
||||
XAutoRepeatOff(rk_display);
|
||||
XAutoRepeatOff(events.display);
|
||||
}
|
||||
XChangePointerControl(rk_display, True, True,
|
||||
rk_previous_accel_numerator, rk_previous_accel_denominator, rk_previous_threshold);
|
||||
XChangePointerControl(
|
||||
events.display, True, True, events.backup_numerator, events.backup_denominator, events.backup_threshold);
|
||||
}
|
||||
|
||||
static void rk_apply_setup() {
|
||||
if (rk_keyboard_autorepeat) {
|
||||
XAutoRepeatOn(rk_display);
|
||||
static void rk_apply(rk_events_x11 & events) {
|
||||
if (events.key_autorepeat) {
|
||||
XAutoRepeatOn(events.display);
|
||||
} else {
|
||||
XAutoRepeatOff(rk_display);
|
||||
XAutoRepeatOff(events.display);
|
||||
}
|
||||
XChangePointerControl(rk_display, True, True, rk_accel_numerator, rk_accel_denominator, rk_accel_threshold);
|
||||
XChangePointerControl(
|
||||
events.display, True, True, events.motion_numerator, events.motion_denominator, events.motion_threshold);
|
||||
}
|
||||
|
||||
bool rk_initialize_events() {
|
||||
rk_focused = false;
|
||||
rk_backup_setup();
|
||||
rk_keyboard_autorepeat = (AutoRepeatModeOn == rk_previous_keyboard_state.global_auto_repeat);
|
||||
rk_accel_numerator = rk_previous_accel_numerator;
|
||||
rk_accel_denominator = rk_previous_accel_denominator;
|
||||
rk_accel_threshold = rk_previous_threshold;
|
||||
rk_input_manager = XOpenIM(rk_display, nullptr, nullptr, nullptr);
|
||||
if (!rk_input_manager) {
|
||||
return false;
|
||||
}
|
||||
rk_input_context = XCreateIC(rk_input_manager,
|
||||
XNInputStyle, XIMPreeditNone | XIMStatusNone,
|
||||
XNClientWindow, rk_window,
|
||||
NULL);
|
||||
if (!rk_input_context) {
|
||||
return false;
|
||||
}
|
||||
XSetICFocus(rk_input_context);
|
||||
return true;
|
||||
}
|
||||
|
||||
void rk_terminate_events() {
|
||||
rk_restore_setup();
|
||||
if (rk_input_context) {
|
||||
XUnsetICFocus(rk_input_context);
|
||||
XDestroyIC(rk_input_context);
|
||||
rk_input_context = nullptr;
|
||||
}
|
||||
if (rk_input_manager) {
|
||||
XCloseIM(rk_input_manager);
|
||||
rk_input_manager = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static void rk_focus_in() {
|
||||
if (!rk_focused) {
|
||||
rk_backup_setup();
|
||||
rk_apply_setup();
|
||||
XGrabPointer(rk_display, rk_window, True,
|
||||
static void rk_focus_in(rk_events_x11 & events) {
|
||||
if (!events.focused) {
|
||||
rk_backup(events);
|
||||
rk_apply(events);
|
||||
XGrabPointer(events.display, events.window, True,
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
|
||||
GrabModeAsync, GrabModeAsync, rk_window, None, CurrentTime);
|
||||
rk_focused = true;
|
||||
GrabModeAsync, GrabModeAsync, events.window, None, CurrentTime);
|
||||
events.focused = true;
|
||||
}
|
||||
}
|
||||
|
||||
static void rk_focus_out() {
|
||||
if (rk_focused) {
|
||||
XUngrabPointer(rk_display, CurrentTime);
|
||||
rk_restore_setup();
|
||||
rk_focused = false;
|
||||
static void rk_focus_out(rk_events_x11 & events) {
|
||||
if (events.focused) {
|
||||
XUngrabPointer(events.display, CurrentTime);
|
||||
rk_restore(events);
|
||||
events.focused = false;
|
||||
}
|
||||
}
|
||||
|
||||
void rk_set_autorepeat(
|
||||
rk_events_t rk_create_events(
|
||||
rk_display_t _display) {
|
||||
rk_display_x11 const * const display = reinterpret_cast<rk_display_x11 const *>(_display);
|
||||
if (!display || !display->display || !display->window) {
|
||||
return nullptr;
|
||||
}
|
||||
rk_events_x11 * const events = new rk_events_x11;
|
||||
events->display = display->display;
|
||||
events->window = display->window;
|
||||
events->input_manager = XOpenIM(display->display, nullptr, nullptr, nullptr);
|
||||
if (!events->input_manager) {
|
||||
rk_x11_printf("Failed to open input manager.");
|
||||
delete events;
|
||||
return nullptr;
|
||||
}
|
||||
events->input_context = XCreateIC(events->input_manager,
|
||||
XNInputStyle, XIMPreeditNone | XIMStatusNone,
|
||||
XNClientWindow, display->window,
|
||||
NULL);
|
||||
if (!events->input_context) {
|
||||
rk_x11_printf("Failed to create input context.");
|
||||
XCloseIM(events->input_manager);
|
||||
delete events;
|
||||
return nullptr;
|
||||
}
|
||||
events->focused = false;
|
||||
rk_backup(*events);
|
||||
events->key_autorepeat = events->backup_autorepeat;
|
||||
events->motion_numerator = events->backup_numerator;
|
||||
events->motion_denominator = events->backup_denominator;
|
||||
events->motion_threshold = events->backup_threshold;
|
||||
XSetICFocus(events->input_context);
|
||||
return reinterpret_cast<rk_events_t>(events);
|
||||
}
|
||||
|
||||
void rk_destroy_events(
|
||||
rk_display_t _display,
|
||||
rk_events_t _events) {
|
||||
rk_display_x11 const * const display = reinterpret_cast<rk_display_x11 const *>(_display);
|
||||
rk_events_x11 * const events = reinterpret_cast<rk_events_x11 *>(_events);
|
||||
if (display && events && display->display && display->display == events->display) {
|
||||
rk_restore(*events);
|
||||
if (events->input_context) {
|
||||
XUnsetICFocus(events->input_context);
|
||||
XDestroyIC(events->input_context);
|
||||
}
|
||||
if (events->input_manager) {
|
||||
XCloseIM(events->input_manager);
|
||||
}
|
||||
}
|
||||
if (events) {
|
||||
delete events;
|
||||
}
|
||||
}
|
||||
|
||||
void rk_set_key_autorepeat(
|
||||
rk_events_t _events,
|
||||
rk_bool autorepeat) {
|
||||
rk_keyboard_autorepeat = autorepeat;
|
||||
if (rk_focused) {
|
||||
rk_apply_setup();
|
||||
rk_events_x11 * const events = reinterpret_cast<rk_events_x11 *>(_events);
|
||||
if (events) {
|
||||
events->key_autorepeat = autorepeat;
|
||||
if (events->focused) {
|
||||
rk_apply(*events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void rk_set_acceleration(
|
||||
void rk_set_motion_acceleration(
|
||||
rk_events_t _events,
|
||||
rk_uint numerator,
|
||||
rk_uint denominator,
|
||||
rk_uint threshold) {
|
||||
rk_accel_numerator = numerator;
|
||||
rk_accel_denominator = denominator;
|
||||
rk_accel_threshold = threshold;
|
||||
if (rk_focused) {
|
||||
rk_apply_setup();
|
||||
rk_events_x11 * const events = reinterpret_cast<rk_events_x11 *>(_events);
|
||||
if (events) {
|
||||
events->motion_numerator = numerator;
|
||||
events->motion_denominator = denominator;
|
||||
events->motion_threshold = threshold;
|
||||
if (events->focused) {
|
||||
rk_apply(*events);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rk_uint rk_consume_events(
|
||||
rk_event * events,
|
||||
rk_events_t _events,
|
||||
rk_event * buffer,
|
||||
rk_uint max_events) {
|
||||
static wchar_t string[256];
|
||||
if (!events || !max_events) {
|
||||
rk_events_x11 * const events = reinterpret_cast<rk_events_x11 *>(_events);
|
||||
if (!events || !buffer || !max_events) {
|
||||
return 0;
|
||||
}
|
||||
XEvent x11_event;
|
||||
KeySym keysym;
|
||||
Status status;
|
||||
unsigned nevents = 0;
|
||||
while (nevents < max_events && XCheckWindowEvent(rk_display, rk_window, rk_events_mask, &x11_event)) {
|
||||
while (nevents < max_events && XCheckWindowEvent(events->display, events->window, RK_EVENTS_MASK, &x11_event)) {
|
||||
if (XFilterEvent(&x11_event, 0)) {
|
||||
continue;
|
||||
}
|
||||
rk_event & event = events[nevents];
|
||||
rk_event & event = buffer[nevents];
|
||||
switch (x11_event.type) {
|
||||
|
||||
case FocusIn:
|
||||
if (NotifyNormal == x11_event.xfocus.mode) {
|
||||
rk_focus_in();
|
||||
rk_focus_in(*events);
|
||||
event.type = RK_EVENT_FOCUS_IN;
|
||||
++nevents;
|
||||
}
|
||||
@ -159,7 +183,7 @@ rk_uint rk_consume_events(
|
||||
|
||||
case FocusOut:
|
||||
if (NotifyNormal == x11_event.xfocus.mode) {
|
||||
rk_focus_out();
|
||||
rk_focus_out(*events);
|
||||
event.type = RK_EVENT_FOCUS_OUT;
|
||||
++nevents;
|
||||
}
|
||||
@ -168,7 +192,7 @@ rk_uint rk_consume_events(
|
||||
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);
|
||||
XwcLookupString(events->input_context, &x11_event.xkey, string, 256, &keysym, &status);
|
||||
switch (status) {
|
||||
case XLookupChars:
|
||||
event.key.symbol = XLookupKeysym(&x11_event.xkey, 0);
|
||||
|
44
cpp/events/events_x11.hpp
Normal file
44
cpp/events/events_x11.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
// 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_X11_H
|
||||
#define _RK_ENGINE_EVENTS_X11_H
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
enum : unsigned {
|
||||
RK_EVENTS_MASK = FocusChangeMask |
|
||||
KeyPressMask | KeyReleaseMask |
|
||||
ButtonPressMask | ButtonReleaseMask |
|
||||
PointerMotionMask
|
||||
};
|
||||
|
||||
struct rk_events_x11 {
|
||||
Display * display;
|
||||
Window window;
|
||||
XIM input_manager;
|
||||
XIC input_context;
|
||||
bool focused;
|
||||
bool key_autorepeat;
|
||||
bool backup_autorepeat;
|
||||
int motion_numerator;
|
||||
int backup_numerator;
|
||||
int motion_denominator;
|
||||
int backup_denominator;
|
||||
int motion_threshold;
|
||||
int backup_threshold;
|
||||
};
|
||||
|
||||
#endif // _RK_ENGINE_EVENTS_X11_H
|
Reference in New Issue
Block a user