Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions runtime/share/mezzaluna/master.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mez.input.add_keymap("alt", "g", {
press = function ()
for _, id in ipairs(mez.view.get_all_ids()) do
print(id)
end
mez.input.send_pointer_button("press")
end
})

Expand Down Expand Up @@ -312,6 +310,12 @@ local master = function()
end
end

mez.input.add_keymap("alt", "x", {
press = function()
mez.input.send_key("a", "press")
end
})

master()

function print_table(tbl, indent, seen)
Expand Down
71 changes: 71 additions & 0 deletions src/lua/Input.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const Input = @This();
const std = @import("std");
const zlua = @import("zlua");
const xkb = @import("xkbcommon");
const wl = @import("wayland").server.wl;
const wlr = @import("wlroots");

const Keymap = @import("../types/Keymap.zig");
const Utils = @import("../Utils.zig");
const LuaUtils = @import("LuaUtils.zig");
const c = @import("../C.zig").c;

const server = &@import("../main.zig").server;

Expand Down Expand Up @@ -110,3 +112,72 @@ pub fn set_repeat_info(L: *zlua.Lua) i32 {
server.seat.keyboard_group.keyboard.setRepeatInfo(rate, delay);
return 0;
}

/// FIXME: this doesn't work just yet, and I'm not sure how we can get the
/// correct time.
fn getTimeMs() u32 {
const now = std.posix.clock_gettime(.MONOTONIC) catch unreachable;
return @intCast(now.sec * std.time.ms_per_s + @divTrunc(now.nsec, std.time.ns_per_ms));
}

pub fn send_key(L: *zlua.Lua) i32 {
const key = L.checkString(1);
const state = L.checkString(2);

var pressed: u32 = 1;
if (std.mem.eql(u8, state, "press")) {
pressed = 1;
} if (std.mem.eql(u8, state, "release")) {
pressed = 0;
}

const keysym = xkb.Keysym.fromName(key, .no_flags);

server.seat.wlr_seat.keyboardNotifyKey(
getTimeMs(),
keysym.toUTF32(),
.pressed,
);

return 0;
}

pub fn send_pointer_motion(L: *zlua.Lua) i32 {
const sx = L.checkNumber(1);
const sy = L.checkNumber(2);

server.seat.wlr_seat.pointerNotifyMotion(getTimeMs(), sx, sy);
server.seat.wlr_seat.pointerNotifyFrame();

return 0;
}

pub fn send_pointer_button(L: *zlua.Lua) i32 {
const button = L.checkString(1);
const state = L.checkString(2);

var pressed: wl.Pointer.ButtonState = .pressed;
if (std.mem.eql(u8, state, "press")) {
pressed = .pressed;
} if (std.mem.eql(u8, state, "release")) {
pressed = .released;
} else {
return 0;
}

const mousesym = c.libevdev_event_code_from_name(c.EV_KEY, button);
_ = server.seat.wlr_seat.pointerNotifyButton(getTimeMs(), @intCast(mousesym), pressed);

return 0;
}

// ---@param x number x position of cursor
// ---@param y number y position of cursor
pub fn warp_pointer(L: *zlua.Lua) i32 {
const x = L.checkNumber(1);
const y = L.checkNumber(2);

server.seat.wlr_seat.pointerWarp(x, y);
server.seat.wlr_seat.pointerNotifyFrame();
return 0;
}