forgot to add gesture.zig
Browse files
+127 −0 src/input/gesture.zig new file mode 100644
| @@ -0,0 +1,127 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const build_options = @import("build_options"); | ||
| 3 | + | ||
| 4 | +pub const enabled = build_options.gestures; | ||
| 5 | + | ||
| 6 | +const c = if (enabled) @cImport({ | ||
| 7 | + @cInclude("libinput.h"); | ||
| 8 | + @cInclude("libudev.h"); | ||
| 9 | +}) else struct {}; | ||
| 10 | + | ||
| 11 | +extern "c" fn open(path: [*:0]const u8, flags: c_int, ...) c_int; | ||
| 12 | +extern "c" fn close(fd: c_int) c_int; | ||
| 13 | + | ||
| 14 | +pub const Kind = enum { | ||
| 15 | + begin, | ||
| 16 | + update, | ||
| 17 | + end, | ||
| 18 | +}; | ||
| 19 | + | ||
| 20 | +pub const Event = struct { | ||
| 21 | + kind: Kind, | ||
| 22 | + fingers: u32, | ||
| 23 | + dx: f64 = 0, | ||
| 24 | + dy: f64 = 0, | ||
| 25 | + cancelled: bool = false, | ||
| 26 | +}; | ||
| 27 | + | ||
| 28 | +pub const GestureError = error{ | ||
| 29 | + Disabled, | ||
| 30 | + UdevFailed, | ||
| 31 | + ContextFailed, | ||
| 32 | + SeatFailed, | ||
| 33 | +}; | ||
| 34 | + | ||
| 35 | +pub const Gestures = if (enabled) LibinputGestures else StubGestures; | ||
| 36 | + | ||
| 37 | +fn openRestricted(path: [*c]const u8, flags: c_int, user_data: ?*anyopaque) callconv(.c) c_int { | ||
| 38 | + _ = user_data; | ||
| 39 | + const fd = open(path, flags); | ||
| 40 | + if (fd < 0) return -@as(c_int, @intCast(@intFromEnum(std.posix.errno(fd)))); | ||
| 41 | + return fd; | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +fn closeRestricted(fd: c_int, user_data: ?*anyopaque) callconv(.c) void { | ||
| 45 | + _ = user_data; | ||
| 46 | + _ = close(fd); | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +var interface = if (enabled) c.struct_libinput_interface{ | ||
| 50 | + .open_restricted = openRestricted, | ||
| 51 | + .close_restricted = closeRestricted, | ||
| 52 | +} else {}; | ||
| 53 | + | ||
| 54 | +const LibinputGestures = struct { | ||
| 55 | + udev: *c.struct_udev, | ||
| 56 | + li: *c.struct_libinput, | ||
| 57 | + fd: c_int, | ||
| 58 | + | ||
| 59 | + pub fn init() GestureError!LibinputGestures { | ||
| 60 | + const udev = c.udev_new() orelse return GestureError.UdevFailed; | ||
| 61 | + const li = c.libinput_udev_create_context(&interface, null, udev) orelse { | ||
| 62 | + _ = c.udev_unref(udev); | ||
| 63 | + return GestureError.ContextFailed; | ||
| 64 | + }; | ||
| 65 | + c.libinput_log_set_priority(li, c.LIBINPUT_LOG_PRIORITY_ERROR); | ||
| 66 | + if (c.libinput_udev_assign_seat(li, "seat0") != 0) { | ||
| 67 | + _ = c.libinput_unref(li); | ||
| 68 | + _ = c.udev_unref(udev); | ||
| 69 | + return GestureError.SeatFailed; | ||
| 70 | + } | ||
| 71 | + return LibinputGestures{ | ||
| 72 | + .udev = udev, | ||
| 73 | + .li = li, | ||
| 74 | + .fd = c.libinput_get_fd(li), | ||
| 75 | + }; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + pub fn deinit(self: *LibinputGestures) void { | ||
| 79 | + _ = c.libinput_unref(self.li); | ||
| 80 | + _ = c.udev_unref(self.udev); | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + /// Drains pending libinput events and calls `callback` for every swipe. | ||
| 84 | + pub fn dispatch(self: *LibinputGestures, context: anytype, comptime callback: fn (@TypeOf(context), Event) void) void { | ||
| 85 | + if (c.libinput_dispatch(self.li) != 0) return; | ||
| 86 | + while (c.libinput_get_event(self.li)) |event| { | ||
| 87 | + defer c.libinput_event_destroy(event); | ||
| 88 | + const kind: Kind = switch (c.libinput_event_get_type(event)) { | ||
| 89 | + c.LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN => .begin, | ||
| 90 | + c.LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE => .update, | ||
| 91 | + c.LIBINPUT_EVENT_GESTURE_SWIPE_END => .end, | ||
| 92 | + else => continue, | ||
| 93 | + }; | ||
| 94 | + const gesture = c.libinput_event_get_gesture_event(event) orelse continue; | ||
| 95 | + var ev = Event{ | ||
| 96 | + .kind = kind, | ||
| 97 | + .fingers = @intCast(@max(0, c.libinput_event_gesture_get_finger_count(gesture))), | ||
| 98 | + }; | ||
| 99 | + switch (kind) { | ||
| 100 | + .update => { | ||
| 101 | + ev.dx = c.libinput_event_gesture_get_dx(gesture); | ||
| 102 | + ev.dy = c.libinput_event_gesture_get_dy(gesture); | ||
| 103 | + }, | ||
| 104 | + .end => ev.cancelled = c.libinput_event_gesture_get_cancelled(gesture) != 0, | ||
| 105 | + .begin => {}, | ||
| 106 | + } | ||
| 107 | + callback(context, ev); | ||
| 108 | + } | ||
| 109 | + } | ||
| 110 | +}; | ||
| 111 | + | ||
| 112 | +const StubGestures = struct { | ||
| 113 | + fd: c_int = -1, | ||
| 114 | + | ||
| 115 | + pub fn init() GestureError!StubGestures { | ||
| 116 | + return GestureError.Disabled; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + pub fn deinit(self: *StubGestures) void { | ||
| 120 | + _ = self; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + pub fn dispatch(self: *StubGestures, context: anytype, comptime callback: fn (@TypeOf(context), Event) void) void { | ||
| 124 | + _ = self; | ||
| 125 | + _ = callback; | ||
| 126 | + } | ||
| 127 | +}; | ||