this repo has no description
13
fork

Configure Feed

Select the types of activity you want to include in your feed.

mouse: implement mouse parsing and events

Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>

+93 -2
+1 -1
build.zig
··· 20 20 21 21 const exe = b.addExecutable(.{ 22 22 .name = "vaxis", 23 - .root_source_file = .{ .path = "examples/image.zig" }, 23 + .root_source_file = .{ .path = "examples/text_input.zig" }, 24 24 .target = target, 25 25 .optimize = optimize, 26 26 });
+1
examples/text_input.zig
··· 11 11 // for a single event loop with exhaustive switching. Booya 12 12 const Event = union(enum) { 13 13 key_press: vaxis.Key, 14 + mouse: vaxis.Mouse, 14 15 winsize: vaxis.Winsize, 15 16 focus_in, 16 17 focus_out,
+31 -1
src/Mouse.zig
··· 13 13 cell, 14 14 }; 15 15 16 - // TODO: mouse support 16 + pub const Button = enum(u8) { 17 + left, 18 + middle, 19 + right, 20 + none, 21 + wheel_up = 64, 22 + wheel_down = 65, 23 + button_8 = 128, 24 + button_9 = 129, 25 + button_10 = 130, 26 + button_11 = 131, 27 + }; 28 + 29 + pub const Modifiers = packed struct(u3) { 30 + shift: bool = false, 31 + alt: bool = false, 32 + ctrl: bool = false, 33 + }; 34 + 35 + pub const Type = enum { 36 + press, 37 + release, 38 + motion, 39 + drag, 40 + }; 41 + 42 + col: usize, 43 + row: usize, 44 + button: Button, 45 + mods: Modifiers, 46 + type: Type,
+52
src/Parser.zig
··· 2 2 const testing = std.testing; 3 3 const Event = @import("event.zig").Event; 4 4 const Key = @import("Key.zig"); 5 + const Mouse = @import("Mouse.zig"); 5 6 const CodePointIterator = @import("ziglyph").CodePointIterator; 6 7 const graphemeBreak = @import("ziglyph").graphemeBreak; 7 8 ··· 30 31 param_buf_idx: usize = 0, 31 32 sub_state: std.StaticBitSet(16) = std.StaticBitSet(16).initEmpty(), 32 33 empty_state: std.StaticBitSet(16) = std.StaticBitSet(16).initEmpty(), 34 + }; 35 + 36 + const mouse_bits = struct { 37 + const motion: u8 = 0b00100000; 38 + const buttons: u8 = 0b11000011; 39 + const shift: u8 = 0b00000100; 40 + const alt: u8 = 0b00001000; 41 + const ctrl: u8 = 0b00010000; 33 42 }; 34 43 35 44 // the state of the parser ··· 226 235 'E' => Key.kp_begin, 227 236 'F' => Key.end, 228 237 'H' => Key.home, 238 + 'M', 'm' => { // mouse event 239 + const priv = seq.private_indicator orelse { 240 + log.warn("unhandled csi: CSI {s}", .{input[start + 1 .. i + 1]}); 241 + return .{ .event = null, .n = i + 1 }; 242 + }; 243 + if (priv != '<') { 244 + log.warn("unhandled csi: CSI {s}", .{input[start + 1 .. i + 1]}); 245 + return .{ .event = null, .n = i + 1 }; 246 + } 247 + if (seq.param_idx != 3) { 248 + log.warn("unhandled csi: CSI {s}", .{input[start + 1 .. i + 1]}); 249 + return .{ .event = null, .n = i + 1 }; 250 + } 251 + const button: Mouse.Button = @enumFromInt(seq.params[0] & mouse_bits.buttons); 252 + const motion = seq.params[0] & mouse_bits.motion > 0; 253 + const shift = seq.params[0] & mouse_bits.shift > 0; 254 + const alt = seq.params[0] & mouse_bits.alt > 0; 255 + const ctrl = seq.params[0] & mouse_bits.ctrl > 0; 256 + const col: usize = seq.params[1] - 1; 257 + const row: usize = seq.params[2] - 1; 258 + 259 + const mouse = Mouse{ 260 + .button = button, 261 + .mods = .{ 262 + .shift = shift, 263 + .alt = alt, 264 + .ctrl = ctrl, 265 + }, 266 + .col = col, 267 + .row = row, 268 + .type = blk: { 269 + if (motion and button != Mouse.Button.none) { 270 + break :blk .drag; 271 + } 272 + if (motion and button == Mouse.Button.none) { 273 + break :blk .motion; 274 + } 275 + if (b == 'm') break :blk .release; 276 + break :blk .press; 277 + }, 278 + }; 279 + return .{ .event = .{ .mouse = mouse }, .n = i + 1 }; 280 + }, 229 281 'P' => Key.f1, 230 282 'Q' => Key.f2, 231 283 'R' => Key.f3,
+5
src/Tty.zig
··· 157 157 vx.postEvent(.{ .key_press = mut_key }); 158 158 } 159 159 }, 160 + .mouse => |mouse| { 161 + if (@hasField(EventType, "mouse")) { 162 + vx.postEvent(.{ .mouse = mouse }); 163 + } 164 + }, 160 165 .focus_in => { 161 166 if (@hasField(EventType, "focus_in")) { 162 167 vx.postEvent(.focus_in);
+2
src/event.zig
··· 1 1 pub const Key = @import("Key.zig"); 2 + pub const Mouse = @import("Mouse.zig"); 2 3 3 4 /// The events that Vaxis emits internally 4 5 pub const Event = union(enum) { 5 6 key_press: Key, 7 + mouse: Mouse, 6 8 focus_in, 7 9 focus_out, 8 10 paste_start,
+1
src/main.zig
··· 6 6 pub const Style = cell.Style; 7 7 8 8 pub const Key = @import("Key.zig"); 9 + pub const Mouse = @import("Mouse.zig"); 9 10 pub const Winsize = @import("Tty.zig").Winsize; 10 11 11 12 pub const widgets = @import("widgets/main.zig");