diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/gui.zig | 168 |
1 files changed, 74 insertions, 94 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 9fd22e3f..3ed97f10 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2808,112 +2808,92 @@ const StdinFeed = struct { var seq_start: usize = 0; while (seq_start < len) { - // ESC]777;mouse;<down|up|motion>;<SDL button, 0 for none>;<x>;<y>[;<mods>]BEL - // ESC]777;mouse;wheel;<amount>;<x>;<y>[;<mods>]BEL - // A mouse event at window pixels, built as SDL would and run - // through dispatch, so a driver exercises the real pointer path - // (pixel to cell, tag and body hits). SGR mouse carries only cells. - // ESC]777;view;<k>BEL: what follows is window k's (k from 1), - // as an event of that window's makes its view live (route). - const view_prefix = "\x1b]777;view;"; - if (std.mem.startsWith(u8, f.buf[seq_start..len], view_prefix)) { - const body = f.buf[seq_start + view_prefix.len .. len]; - const end = std.mem.indexOfScalar(u8, body, 0x07) orelse { - std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); - f.fill = len - seq_start; + // ESC]777;<event>;<fields>BEL, the driver's own: + // view;<k>: what follows is window k's (k from 1), as an event + // of that window's makes its view live (route). + // mouse;<down|up|motion>;<SDL button, 0 for none>;<x>;<y>[;<mods>] + // mouse;wheel;<amount>;<x>;<y>[;<mods>]: a mouse event at + // window pixels, built as SDL would and run through dispatch, + // so a driver exercises the real pointer path (pixel to cell, + // tag and body hits). SGR mouse carries only cells. + // finger;<kind>;<id>;<x>;<y>;<pressure> + // One the read cut short, or what may yet begin one, waits for + // the next read. + const rest = f.buf[seq_start..len]; + const osc = "\x1b]777;"; + if (std.mem.startsWith(u8, rest, osc) or std.mem.startsWith(u8, osc, rest)) { + const bel = std.mem.indexOfScalar(u8, rest, 0x07) orelse { + std.mem.copyForwards(u8, f.buf[0..rest.len], rest); + f.fill = rest.len; break; }; - const k = std.fmt.parseInt(u8, body[0..end], 10) catch 0; - if (in.shell) |sh| if (k >= 1 and pardes.views.isOpen(sh.core, k - 1)) { - sh.core.toView(k - 1); - sh.follow(); - }; - seq_start += view_prefix.len + end + 1; - continue; - } - const mouse_prefix = "\x1b]777;mouse;"; - if (std.mem.startsWith(u8, f.buf[seq_start..len], mouse_prefix)) { - const body = f.buf[seq_start + mouse_prefix.len .. len]; - const end = std.mem.indexOfScalar(u8, body, 0x07) orelse { - std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); - f.fill = len - seq_start; - break; - }; - if (g) |gp| { - var parts = std.mem.splitScalar(u8, body[0..end], ';'); - const kind = parts.next() orelse ""; - const second = parts.next() orelse ""; - const button = std.fmt.parseInt(u8, second, 10) catch 0; - const x = std.fmt.parseFloat(f32, parts.next() orelse "") catch 0; - const y = std.fmt.parseFloat(f32, parts.next() orelse "") catch 0; - // An optional fifth field holds modifiers as xterm - // counts them: shift 4, meta (alt) 8, ctrl 16. - const held = std.fmt.parseInt(u8, parts.next() orelse "0", 10) catch 0; - var sev = std.mem.zeroes(c.SDL_Event); - if (std.mem.eql(u8, kind, "wheel")) { - // ESC]777;mouse;wheel;<y, SDL's sign: up positive>;<x>;<y>BEL: - // a whole number is a mouse's notches, else a touchpad's. - const amount = std.fmt.parseFloat(f32, second) catch 0; - sev.type = c.SDL_EVENT_MOUSE_WHEEL; - sev.wheel.y = amount; - sev.wheel.integer_y = @intFromFloat(@trunc(amount)); - sev.wheel.mouse_x = x; - sev.wheel.mouse_y = y; - } else if (std.mem.eql(u8, kind, "focus")) { - // ESC]777;mouse;focus;<1 gained, 0 lost>;0;0BEL - sev.type = if (button != 0) c.SDL_EVENT_WINDOW_FOCUS_GAINED else c.SDL_EVENT_WINDOW_FOCUS_LOST; - } else if (std.mem.eql(u8, kind, "motion")) { - sev.type = c.SDL_EVENT_MOUSE_MOTION; - sev.motion.x = x; - sev.motion.y = y; - sev.motion.state = if (button == 0 or button > 5) 0 else @as(u32, 1) << @intCast(button - 1); - } else { - sev.type = if (std.mem.eql(u8, kind, "down")) c.SDL_EVENT_MOUSE_BUTTON_DOWN else c.SDL_EVENT_MOUSE_BUTTON_UP; - sev.button.button = button; - sev.button.down = std.mem.eql(u8, kind, "down"); - sev.button.x = x; - sev.button.y = y; - } - const before = c.SDL_GetModState(); - var mods: c.SDL_Keymod = 0; - if (held & 4 != 0) mods |= c.SDL_KMOD_LSHIFT; - if (held & 8 != 0) mods |= c.SDL_KMOD_LALT; - if (held & 16 != 0) mods |= c.SDL_KMOD_LCTRL; - c.SDL_SetModState(mods); - dispatch(gp, in, &sev); - c.SDL_SetModState(before); - } - seq_start += mouse_prefix.len + end + 1; - continue; - } - const prefix = "\x1b]777;finger;"; - if (std.mem.startsWith(u8, f.buf[seq_start..len], prefix)) { - const body = f.buf[seq_start + prefix.len .. len]; - const end = std.mem.indexOfScalar(u8, body, 0x07) orelse { - std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); - f.fill = len - seq_start; - break; - }; - if (parseFinger(body[0..end])) |finger| { + seq_start += bel + 1; + const event = rest[osc.len..bel]; + const semi = std.mem.indexOfScalar(u8, event, ';') orelse event.len; + const fields = event[@min(semi + 1, event.len)..]; + if (std.mem.eql(u8, event[0..semi], "view")) { + const k = std.fmt.parseInt(u8, fields, 10) catch 0; + if (in.shell) |sh| if (k >= 1 and pardes.views.isOpen(sh.core, k - 1)) { + sh.core.toView(k - 1); + sh.follow(); + }; + } else if (std.mem.eql(u8, event[0..semi], "mouse")) { if (g) |gp| { + var parts = std.mem.splitScalar(u8, fields, ';'); + const kind = parts.next() orelse ""; + const second = parts.next() orelse ""; + const button = std.fmt.parseInt(u8, second, 10) catch 0; + const x = std.fmt.parseFloat(f32, parts.next() orelse "") catch 0; + const y = std.fmt.parseFloat(f32, parts.next() orelse "") catch 0; + // An optional fifth field holds modifiers as xterm + // counts them: shift 4, meta (alt) 8, ctrl 16. + const held = std.fmt.parseInt(u8, parts.next() orelse "0", 10) catch 0; + var sev = std.mem.zeroes(c.SDL_Event); + if (std.mem.eql(u8, kind, "wheel")) { + // ESC]777;mouse;wheel;<y, SDL's sign: up positive>;<x>;<y>BEL: + // a whole number is a mouse's notches, else a touchpad's. + const amount = std.fmt.parseFloat(f32, second) catch 0; + sev.type = c.SDL_EVENT_MOUSE_WHEEL; + sev.wheel.y = amount; + sev.wheel.integer_y = @intFromFloat(@trunc(amount)); + sev.wheel.mouse_x = x; + sev.wheel.mouse_y = y; + } else if (std.mem.eql(u8, kind, "focus")) { + // ESC]777;mouse;focus;<1 gained, 0 lost>;0;0BEL + sev.type = if (button != 0) c.SDL_EVENT_WINDOW_FOCUS_GAINED else c.SDL_EVENT_WINDOW_FOCUS_LOST; + } else if (std.mem.eql(u8, kind, "motion")) { + sev.type = c.SDL_EVENT_MOUSE_MOTION; + sev.motion.x = x; + sev.motion.y = y; + sev.motion.state = if (button == 0 or button > 5) 0 else @as(u32, 1) << @intCast(button - 1); + } else { + sev.type = if (std.mem.eql(u8, kind, "down")) c.SDL_EVENT_MOUSE_BUTTON_DOWN else c.SDL_EVENT_MOUSE_BUTTON_UP; + sev.button.button = button; + sev.button.down = std.mem.eql(u8, kind, "down"); + sev.button.x = x; + sev.button.y = y; + } + const before = c.SDL_GetModState(); + var mods: c.SDL_Keymod = 0; + if (held & 4 != 0) mods |= c.SDL_KMOD_LSHIFT; + if (held & 8 != 0) mods |= c.SDL_KMOD_LALT; + if (held & 16 != 0) mods |= c.SDL_KMOD_LCTRL; + c.SDL_SetModState(mods); + dispatch(gp, in, &sev); + c.SDL_SetModState(before); + } + } else if (std.mem.eql(u8, event[0..semi], "finger")) { + if (parseFinger(fields)) |finger| if (g) |gp| { var wpw: c_int = 0; var wph: c_int = 0; _ = c.SDL_GetWindowSizeInPixels(gp.window, &wpw, &wph); const win_w: f32 = @floatFromInt(@max(wpw, 1)); const win_h: f32 = @floatFromInt(@max(wph, 1)); handleFinger(&gp.touch, in, finger, win_w, win_h, @floatFromInt(gp.cell_w), @floatFromInt(gp.cell_h), @floatFromInt(gp.tagline_width)); - } + }; } - seq_start += prefix.len + end + 1; continue; } - if ((len - seq_start < prefix.len and std.mem.startsWith(u8, prefix, f.buf[seq_start..len])) or - (len - seq_start < mouse_prefix.len and std.mem.startsWith(u8, mouse_prefix, f.buf[seq_start..len]))) - { - std.mem.copyForwards(u8, f.buf[0 .. len - seq_start], f.buf[seq_start..len]); - f.fill = len - seq_start; - break; - } const result = f.parser.parse(f.buf[seq_start..len], gpa) catch break; if (result.n == 0) { // partial sequence: shift to front, read more |
