Skip to content

Commit

Permalink
Seems we just need to remove min_keycode from press.detail
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmstill committed Mar 4, 2024
1 parent 6da6278 commit abd08e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions src/backend/x11.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const c = @cImport({
@cInclude("GLES3/gl3.h");
@cInclude("X11/Xlib-xcb.h");
@cInclude("xkbcommon/xkbcommon.h");
@cInclude("xkbcommon/xkbcommon-x11.h");
// @cInclude("xkbcommon/xkbcommon-x11.h");
});

const Event = @import("../subsystem.zig").Event;
Expand All @@ -32,20 +32,20 @@ pub const X11 = struct {
fd: i32,
display: *c.Display,
outputs: std.ArrayList(X11Output),
xkb_context: *c.xkb_context,
device_id: i32,
keymap: *c.xkb_keymap,
state: *c.xkb_state,
// xkb_context: *c.xkb_context,
// device_id: i32,
// keymap: *c.xkb_keymap,
// state: *c.xkb_state,
min_keycode: u8,

pub fn init(allocator: mem.Allocator) !X11 {
const display: *c.Display = c.XOpenDisplay(null) orelse return error.FailedToOpenDisplay;
const conn = c.XGetXCBConnection(display) orelse return error.FailedToGetXcbConnection;

const xkb_context = c.xkb_context_new(c.XKB_CONTEXT_NO_FLAGS) orelse return error.X11XkbContextFailed;
const device_id = c.xkb_x11_get_core_keyboard_device_id(conn);
const keymap = c.xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, c.XKB_KEYMAP_COMPILE_NO_FLAGS) orelse return error.X11XkbKeymapFailed;
const state = c.xkb_x11_state_new_from_device(keymap, conn, device_id) orelse return error.X11XkbStateFailed;
// const xkb_context = c.xkb_context_new(c.XKB_CONTEXT_NO_FLAGS) orelse return error.X11XkbContextFailed;
// const device_id = c.xkb_x11_get_core_keyboard_device_id(conn);
// const keymap = c.xkb_x11_keymap_new_from_device(xkb_context, conn, device_id, c.XKB_KEYMAP_COMPILE_NO_FLAGS) orelse return error.X11XkbKeymapFailed;
// const state = c.xkb_x11_state_new_from_device(keymap, conn, device_id) orelse return error.X11XkbStateFailed;

const setup = c.xcb_get_setup(conn);

Expand All @@ -54,10 +54,10 @@ pub const X11 = struct {
.fd = c.xcb_get_file_descriptor(conn),
.display = display,
.outputs = std.ArrayList(X11Output).init(allocator),
.xkb_context = xkb_context,
.device_id = device_id,
.keymap = keymap,
.state = state,
// .xkb_context = xkb_context,
// .device_id = device_id,
// .keymap = keymap,
// .state = state,
.min_keycode = setup.*.min_keycode,
};
}
Expand Down Expand Up @@ -88,10 +88,11 @@ pub const X11 = struct {
c.XCB_KEY_PRESS => {
const press: *c.xcb_key_press_event_t = @ptrCast(ev);

_ = c.xkb_state_update_key(it.x11.state, press.detail, c.XKB_KEY_DOWN);
const keysym = c.xkb_state_key_get_one_sym(it.x11.state, press.detail);
const adjusted_keycode = press.detail - it.x11.min_keycode;
// _ = c.xkb_state_update_key(it.x11.state, adjusted_keycode, c.XKB_KEY_DOWN);
// const keysym = c.xkb_state_key_get_one_sym(it.x11.state, adjusted_keycode);

std.log.info("key press = {} keysym = {}", .{ press.detail, keysym });
// std.log.info("key press = {} keysym = {}", .{ press.detail, keysym });

return .{
.backend = .{
Expand All @@ -100,7 +101,7 @@ pub const X11 = struct {
.event = .{
.key_press = .{
.time = press.time,
.button = keysym - 8,
.button = adjusted_keycode,
.state = 1,
},
},
Expand All @@ -111,8 +112,9 @@ pub const X11 = struct {
const press: *c.xcb_key_release_event_t = @ptrCast(ev);
// std.log.info("button press = {}x{} 0x{x}", .{ press.event_x, press.event_y, press.state });

_ = c.xkb_state_update_key(it.x11.state, press.detail, c.XKB_KEY_UP);
const keysym = c.xkb_state_key_get_one_sym(it.x11.state, press.detail);
const adjusted_keycode = press.detail - it.x11.min_keycode;
// _ = c.xkb_state_update_key(it.x11.state, adjusted_keycode, c.XKB_KEY_UP);
// const keysym = c.xkb_state_key_get_one_sym(it.x11.state, adjusted_keycode);

return .{
.backend = .{
Expand All @@ -121,7 +123,7 @@ pub const X11 = struct {
.event = .{
.key_press = .{
.time = press.time,
.button = keysym - 8,
.button = adjusted_keycode,
.state = 0,
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub const Server = struct {
}

pub fn keyboard(server: *Self, time: u32, button: u32, action: u32) !void {
if (button == 224 or button == 25) server.running = false;
if (button == 224) server.running = false;

server.xkb.updateKey(button, action);
server.mods_depressed = server.xkb.serializeDepressed();
Expand Down

0 comments on commit abd08e4

Please sign in to comment.