map-pointer: allow running arbitrary commands
This commit is contained in:
committed by
Isaac Freund
parent
e2a27b3a4e
commit
cacc986166
@ -528,6 +528,10 @@ fn handlePointerMapping(self: *Self, event: *wlr.Pointer.event.Button, view: *Vi
|
||||
switch (mapping.action) {
|
||||
.move => if (!fullscreen) self.enterMode(.move, view),
|
||||
.resize => if (!fullscreen) self.enterMode(.resize, view),
|
||||
.command => |args| {
|
||||
self.seat.focus(view);
|
||||
self.seat.runCommand(args);
|
||||
},
|
||||
}
|
||||
break true;
|
||||
}
|
||||
|
@ -14,13 +14,64 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
const Self = @This();
|
||||
|
||||
const std = @import("std");
|
||||
const wlr = @import("wlroots");
|
||||
|
||||
pub const Action = enum {
|
||||
const util = @import("util.zig");
|
||||
|
||||
pub const ActionType = enum {
|
||||
move,
|
||||
resize,
|
||||
command,
|
||||
};
|
||||
|
||||
pub const Action = union(ActionType) {
|
||||
move: void,
|
||||
resize: void,
|
||||
command: []const [:0]const u8,
|
||||
};
|
||||
|
||||
event_code: u32,
|
||||
modifiers: wlr.Keyboard.ModifierMask,
|
||||
action: Action,
|
||||
arena: std.heap.ArenaAllocator,
|
||||
|
||||
pub fn init(
|
||||
event_code: u32,
|
||||
modifiers: wlr.Keyboard.ModifierMask,
|
||||
action_type: ActionType,
|
||||
command_args: []const [:0]const u8,
|
||||
) !Self {
|
||||
var arena: std.heap.ArenaAllocator = std.heap.ArenaAllocator.init(util.gpa);
|
||||
errdefer arena.deinit();
|
||||
|
||||
const action: Action = switch (action_type) {
|
||||
ActionType.move => Action.move,
|
||||
ActionType.resize => Action.resize,
|
||||
ActionType.command => blk: {
|
||||
const allocator: std.mem.Allocator = arena.allocator();
|
||||
|
||||
var owned_args = try std.ArrayListUnmanaged([:0]const u8).initCapacity(allocator, command_args.len);
|
||||
|
||||
for (command_args) |arg| {
|
||||
const owned = try allocator.dupeZ(u8, arg);
|
||||
owned_args.appendAssumeCapacity(owned);
|
||||
}
|
||||
|
||||
break :blk Action{ .command = owned_args.toOwnedSlice(allocator) };
|
||||
},
|
||||
};
|
||||
|
||||
return Self{
|
||||
.event_code = event_code,
|
||||
.modifiers = modifiers,
|
||||
.action = action,
|
||||
.arena = arena,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.arena.deinit();
|
||||
}
|
||||
|
@ -434,7 +434,7 @@ pub fn handleSwitchMapping(
|
||||
}
|
||||
}
|
||||
|
||||
fn runCommand(self: *Self, args: []const [:0]const u8) void {
|
||||
pub fn runCommand(self: *Self, args: []const [:0]const u8) void {
|
||||
var out: ?[]const u8 = null;
|
||||
defer if (out) |s| util.gpa.free(s);
|
||||
command.run(self, args, &out) catch |err| {
|
||||
@ -442,7 +442,12 @@ fn runCommand(self: *Self, args: []const [:0]const u8) void {
|
||||
command.Error.Other => out.?,
|
||||
else => command.errToMsg(err),
|
||||
};
|
||||
std.log.scoped(.command).err("{s}: {s}", .{ args[0], failure_message });
|
||||
|
||||
if (args.len == 0) {
|
||||
std.log.scoped(.command).err("{s}", .{failure_message});
|
||||
} else {
|
||||
std.log.scoped(.command).err("{s}: {s}", .{ args[0], failure_message });
|
||||
}
|
||||
return;
|
||||
};
|
||||
if (out) |s| {
|
||||
|
@ -130,33 +130,31 @@ pub fn mapPointer(
|
||||
out: *?[]const u8,
|
||||
) Error!void {
|
||||
if (args.len < 5) return Error.NotEnoughArguments;
|
||||
if (args.len > 5) return Error.TooManyArguments;
|
||||
|
||||
const mode_id = try modeNameToId(args[1], out);
|
||||
const modifiers = try parseModifiers(args[2], out);
|
||||
const event_code = try parseEventCode(args[3], out);
|
||||
|
||||
const action = if (mem.eql(u8, args[4], "move-view"))
|
||||
PointerMapping.Action.move
|
||||
PointerMapping.ActionType.move
|
||||
else if (mem.eql(u8, args[4], "resize-view"))
|
||||
PointerMapping.Action.resize
|
||||
else {
|
||||
out.* = try fmt.allocPrint(
|
||||
util.gpa,
|
||||
"invalid pointer action {s}, must be move-view or resize-view",
|
||||
.{args[4]},
|
||||
);
|
||||
return Error.Other;
|
||||
};
|
||||
PointerMapping.ActionType.resize
|
||||
else
|
||||
PointerMapping.ActionType.command;
|
||||
|
||||
const new = PointerMapping{
|
||||
.event_code = event_code,
|
||||
.modifiers = modifiers,
|
||||
.action = action,
|
||||
};
|
||||
if (action != PointerMapping.ActionType.command and args.len > 5) return Error.TooManyArguments;
|
||||
|
||||
var new = try PointerMapping.init(
|
||||
event_code,
|
||||
modifiers,
|
||||
action,
|
||||
args[4..],
|
||||
);
|
||||
errdefer new.deinit();
|
||||
|
||||
const mode_pointer_mappings = &server.config.modes.items[mode_id].pointer_mappings;
|
||||
if (pointerMappingExists(mode_pointer_mappings, modifiers, event_code)) |current| {
|
||||
mode_pointer_mappings.items[current].deinit();
|
||||
mode_pointer_mappings.items[current] = new;
|
||||
} else {
|
||||
try mode_pointer_mappings.append(util.gpa, new);
|
||||
@ -428,5 +426,6 @@ pub fn unmapPointer(_: *Seat, args: []const [:0]const u8, out: *?[]const u8) Err
|
||||
const mode_pointer_mappings = &server.config.modes.items[mode_id].pointer_mappings;
|
||||
const mapping_idx = pointerMappingExists(mode_pointer_mappings, modifiers, event_code) orelse return;
|
||||
|
||||
_ = mode_pointer_mappings.swapRemove(mapping_idx);
|
||||
var mapping = mode_pointer_mappings.swapRemove(mapping_idx);
|
||||
mapping.deinit();
|
||||
}
|
||||
|
Reference in New Issue
Block a user