Skip to content

Commit

Permalink
Adding keyboard support items
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhat committed Dec 26, 2023
1 parent 9a70f51 commit c2f8f47
Show file tree
Hide file tree
Showing 8 changed files with 724 additions and 792 deletions.
32 changes: 27 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,32 @@ endif()
if(UNIX AND NOT APPLE)
find_package(PkgConfig REQUIRED)

pkg_check_modules(EVDEV REQUIRED libevdev)
target_include_directories(uiohook PRIVATE "${EVDEV_INCLUDE_DIRS}")
target_link_libraries(uiohook "${EVDEV_LDFLAGS}")

#pkg_check_modules(XCB REQUIRED xcb)
#target_include_directories(uiohook PRIVATE "${XCB_INCLUDE_DIRS}")
#target_link_libraries(uiohook "${XCB_LDFLAGS}")

pkg_check_modules(XCB_X11 REQUIRED x11-xcb)
target_include_directories(uiohook PRIVATE "${XCB_X11_INCLUDE_DIRS}")
target_link_libraries(uiohook "${XCB_X11_LDFLAGS}")

pkg_check_modules(XKB REQUIRED xkbcommon)
target_include_directories(uiohook PRIVATE "${XKB_INCLUDE_DIRS}")
target_link_libraries(uiohook "${XKB_LDFLAGS}")

pkg_check_modules(XKB_X11 REQUIRED xkbcommon-x11)
target_include_directories(uiohook PRIVATE "${XKB_X11_INCLUDE_DIRS}")
target_link_libraries(uiohook "${XKB_X11_LDFLAGS}")

# FIXME Check for header X11/extensions/XKB.h
include(CheckIncludeFile)
check_include_file(X11/extensions/XKB.h HAVE_XKB_H)



pkg_check_modules(X11 REQUIRED x11)
target_include_directories(uiohook PRIVATE "${X11_INCLUDE_DIRS}")
target_link_libraries(uiohook "${X11_LDFLAGS}")
Expand All @@ -151,18 +177,14 @@ if(UNIX AND NOT APPLE)
target_include_directories(uiohook PRIVATE "${XINPUT_INCLUDE_DIRS}")
target_link_libraries(uiohook "${XINPUT_LDFLAGS}")

pkg_check_modules(EVDEV REQUIRED libevdev)
target_include_directories(uiohook PRIVATE "${EVDEV_INCLUDE_DIRS}")
target_link_libraries(uiohook "${EVDEV_LDFLAGS}")


include(CMakePrintHelpers)
cmake_print_variables(EVDEV_INCLUDE_DIRS)

include(CheckLibraryExists)
check_library_exists(Xtst XRecordQueryVersion "" HAVE_XRECORD)

include(CheckIncludeFile)
check_include_file(X11/extensions/record.h HAVE_RECORD_H "-include X11/Xlib.h")

option(USE_XT "X Toolkit Extension (default: ON)" ON)
if(USE_XT)
Expand Down
77 changes: 25 additions & 52 deletions src/evdev/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,41 +96,20 @@ void dispatch_hook_disabled(XAnyEvent * const x_event) {
unload_input_helper();
}

void dispatch_key_press(XKeyPressedEvent * const x_event) {
void dispatch_key_press(uint64_t time, xkb_keycode_t keycode) {
KeySym keysym = 0x00;

wchar_t surrogate[2] = {};
size_t count = x_key_event_lookup(x_event, surrogate, sizeof(surrogate) - 1, &keysym);


uint16_t uiocode = keysym_to_vcode(keysym);

// FIXME This can happen inside of keysym_to_vcode()
// TODO VC_ALT_GRAPH MASK?
if (uiocode == VC_SHIFT_L) { set_modifier_mask(MASK_SHIFT_L); }
else if (uiocode == VC_SHIFT_R) { set_modifier_mask(MASK_SHIFT_R); }
else if (uiocode == VC_CONTROL_L) { set_modifier_mask(MASK_CTRL_L); }
else if (uiocode == VC_CONTROL_R) { set_modifier_mask(MASK_CTRL_R); }
else if (uiocode == VC_ALT_L) { set_modifier_mask(MASK_ALT_L); }
else if (uiocode == VC_ALT_R) { set_modifier_mask(MASK_ALT_R); }
else if (uiocode == VC_META_L) { set_modifier_mask(MASK_META_L); }
else if (uiocode == VC_META_R) { set_modifier_mask(MASK_META_R); }

// FIXME We shouldn't be doing this on each key press, do something similar to above.
//initialize_locks();



uint16_t uiocode = keycode_to_uiocode(keycode);

// Populate key pressed event.
uio_event.time = x_event->serial;
uio_event.time = time;
uio_event.reserved = 0x00;

uio_event.type = EVENT_KEY_PRESSED;
uio_event.mask = get_modifiers();

uio_event.data.keyboard.keycode = uiocode;
uio_event.data.keyboard.rawcode = keysym;
uio_event.data.keyboard.rawcode = keycode;
uio_event.data.keyboard.keychar = CHAR_UNDEFINED;

logger(LOG_LEVEL_DEBUG, "%s [%u]: Key %#X pressed. (%#X)\n",
Expand All @@ -142,9 +121,11 @@ void dispatch_key_press(XKeyPressedEvent * const x_event) {

// If the pressed event was not consumed and we got a char in the buffer.
if (uio_event.reserved ^ 0x01) {
wchar_t surrogate[4] = {};
size_t count = keycode_to_utf8(keycode, surrogate, sizeof(surrogate));
for (unsigned int i = 0; i < count; i++) {
// Populate key typed event.
uio_event.time = x_event->serial;
uio_event.time = time;
uio_event.reserved = 0x00;

uio_event.type = EVENT_KEY_TYPED;
Expand All @@ -165,37 +146,22 @@ void dispatch_key_press(XKeyPressedEvent * const x_event) {
}
}

void dispatch_key_release(XKeyReleasedEvent * const x_event) {
void dispatch_key_release(uint64_t time, xkb_keycode_t keycode) {
// The X11 KeyCode associated with this event.
KeySym keysym = 0x00;

x_key_event_lookup(x_event, NULL, 0, &keysym);

uint16_t uiocode = keysym_to_vcode(keysym);

// FIXME This can happen inside of keycode_to_scancode()
if (uiocode == VC_SHIFT_L) { unset_modifier_mask(MASK_SHIFT_L); }
else if (uiocode == VC_SHIFT_R) { unset_modifier_mask(MASK_SHIFT_R); }
else if (uiocode == VC_CONTROL_L) { unset_modifier_mask(MASK_CTRL_L); }
else if (uiocode == VC_CONTROL_R) { unset_modifier_mask(MASK_CTRL_R); }
else if (uiocode == VC_ALT_L) { unset_modifier_mask(MASK_ALT_L); }
else if (uiocode == VC_ALT_R) { unset_modifier_mask(MASK_ALT_R); }
else if (uiocode == VC_META_L) { unset_modifier_mask(MASK_META_L); }
else if (uiocode == VC_META_R) { unset_modifier_mask(MASK_META_R); }

// FIXME We shouldn't be doing this on each key release.
//initialize_locks();
uint16_t uiocode = keycode_to_uiocode(keycode);


// Populate key released event.
uio_event.time = x_event->serial;
uio_event.time = time;
uio_event.reserved = 0x00;

uio_event.type = EVENT_KEY_RELEASED;
uio_event.mask = get_modifiers();

uio_event.data.keyboard.keycode = uiocode;
uio_event.data.keyboard.rawcode = keysym;
uio_event.data.keyboard.rawcode = keycode;
uio_event.data.keyboard.keychar = CHAR_UNDEFINED;

logger(LOG_LEVEL_DEBUG, "%s [%u]: Key %#X released. (%#X)\n",
Expand Down Expand Up @@ -242,6 +208,8 @@ static void dispatch_mouse_wheel_rotated(XButtonEvent * const x_event) {
/* Some scroll wheel properties are available via the new XInput2 (XI2) extension. Unfortunately the extension is
* not available on my development platform at this time. For the time being we will just use the Windows default
* value of 3. */

/*
uio_event.data.wheel.delta = 100;
if (x_event->button == WheelDown || x_event->button == WheelLeft) {
// Wheel Rotated Up and Away.
Expand All @@ -258,7 +226,7 @@ static void dispatch_mouse_wheel_rotated(XButtonEvent * const x_event) {
// Wheel Rotated Left or Right.
uio_event.data.wheel.direction = WHEEL_HORIZONTAL_DIRECTION;
}

*/
logger(LOG_LEVEL_DEBUG, "%s [%u]: Mouse wheel %i / %u of type %u in the %u direction at %u, %u.\n",
__FUNCTION__, __LINE__,
uio_event.data.wheel.rotation, uio_event.data.wheel.delta,
Expand All @@ -270,6 +238,7 @@ static void dispatch_mouse_wheel_rotated(XButtonEvent * const x_event) {
}

static void dispatch_mouse_button_pressed(XButtonPressedEvent * const x_event) {
/*
switch (x_event->button) {
case Button1:
x_event->button = MOUSE_BUTTON1;
Expand Down Expand Up @@ -305,7 +274,7 @@ static void dispatch_mouse_button_pressed(XButtonPressedEvent * const x_event) {
x_event->button = MOUSE_NOBUTTON;
}
}

*/
// Track the number of clicks, the button must match the previous button.
if (x_event->button == click.button && x_event->serial - click.time <= hook_get_multi_click_time()) {
if (click.count < UINT16_MAX) {
Expand Down Expand Up @@ -361,8 +330,8 @@ static void dispatch_mouse_button_pressed(XButtonPressedEvent * const x_event) {
}

void dispatch_mouse_press(XButtonEvent * const x_event) {
x_event->button = button_map_lookup(x_event->button);

x_event->button = 0; // FIXME button_map_lookup(x_event->button);
/*
switch (x_event->button) {
case WheelUp:
case WheelDown:
Expand All @@ -375,10 +344,12 @@ void dispatch_mouse_press(XButtonEvent * const x_event) {
dispatch_mouse_button_pressed((XButtonPressedEvent *) x_event);
break;
}
*/
}

static void dispatch_mouse_button_released(XButtonReleasedEvent * const x_event) {
switch (button_map_lookup(x_event->button)) {
switch (0 /* FIXME button_map_lookup(x_event->button) */) {
/*
case Button1:
x_event->button = MOUSE_BUTTON1;
unset_modifier_mask(MASK_BUTTON1);
Expand Down Expand Up @@ -412,6 +383,7 @@ static void dispatch_mouse_button_released(XButtonReleasedEvent * const x_event)
// Something screwed up, default to MOUSE_NOBUTTON
x_event->button = MOUSE_NOBUTTON;
}
*/
}

// Populate mouse released event.
Expand Down Expand Up @@ -484,16 +456,17 @@ static void dispatch_mouse_button_clicked(XButtonEvent * const x_event) {
}

void dispatch_mouse_release(XButtonEvent * const x_event) {
x_event->button = button_map_lookup(x_event->button);
x_event->button = 0; // FIXME button_map_lookup(x_event->button);

/*
switch (x_event->button) {
case WheelUp:
case WheelDown:
case WheelLeft:
case WheelRight:
return;
}

*/
dispatch_mouse_button_released((XButtonReleasedEvent *) x_event);

// If the pressed event was not consumed...
Expand Down
6 changes: 4 additions & 2 deletions src/evdev/dispatch_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <xkbcommon/xkbcommon.h>
#include <X11/Xlib.h>

// FIXME Shouldn't be extern, remove
#include <uiohook.h>

extern void dispatch_event(uiohook_event *const uio_event);

extern void dispatch_hook_enabled();

extern void dispatch_hook_disabled();

extern void dispatch_key_press(XKeyPressedEvent * const x_event);
extern void dispatch_key_press(uint64_t time, xkb_keycode_t keycode);

extern void dispatch_key_release(XKeyReleasedEvent * const x_event);
extern void dispatch_key_release(uint64_t time, xkb_keycode_t keycode);

extern void dispatch_mouse_press(XButtonEvent * const x_event);

Expand Down
Loading

0 comments on commit c2f8f47

Please sign in to comment.