map-pointer: allow running arbitrary commands

This commit is contained in:
Alexander Courtis 2022-09-13 12:05:21 +10:00 committed by Isaac Freund
parent e2a27b3a4e
commit cacc986166
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11
8 changed files with 89 additions and 25 deletions

View File

@ -44,7 +44,7 @@ complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'send-to-previous
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'declare-mode' -d 'Create a new mode' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'declare-mode' -d 'Create a new mode'
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'enter-mode' -d 'Switch to given mode if it exists' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'enter-mode' -d 'Switch to given mode if it exists'
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'map' -d 'Run command when key is pressed while modifiers are held down and in the specified mode' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'map' -d 'Run command when key is pressed while modifiers are held down and in the specified mode'
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'map-pointer' -d 'Move or resize views when button and modifers are held down while in the specified mode' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'map-pointer' -d 'Move or resize views or run command when button and modifers are held down while in the specified mode'
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'map-switch ' -d 'Run command when river receives a switch event in the specified mode' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'map-switch ' -d 'Run command when river receives a switch event in the specified mode'
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'unmap' -d 'Remove the mapping defined by the arguments' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'unmap' -d 'Remove the mapping defined by the arguments'
complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'unmap-pointer' -d 'Remove the pointer mapping defined by the arguments' complete -c riverctl -x -n '__fish_riverctl_complete_arg 1' -a 'unmap-pointer' -d 'Remove the pointer mapping defined by the arguments'

View File

@ -38,7 +38,7 @@ _riverctl_subcommands()
'declare-mode:Create a new mode' 'declare-mode:Create a new mode'
'enter-mode:Switch to given mode if it exists' 'enter-mode:Switch to given mode if it exists'
'map:Run command when key is pressed while modifiers are held down and in the specified mode' 'map:Run command when key is pressed while modifiers are held down and in the specified mode'
'map-pointer:Move or resize views when button and modifiers are held down while in the specified mode' 'map-pointer:Move or resize views or run command when button and modifiers are held down while in the specified mode'
'map-switch:Run command when river receives a switch event in the specified mode' 'map-switch:Run command when river receives a switch event in the specified mode'
'unmap:Remove the mapping defined by the arguments' 'unmap:Remove the mapping defined by the arguments'
'unmap-pointer:Remove the pointer mapping defined by the arguments' 'unmap-pointer:Remove the pointer mapping defined by the arguments'

View File

@ -212,9 +212,10 @@ A complete list may be found in _/usr/include/linux/input-event-codes.h_
- _key_: an XKB keysym name as described above - _key_: an XKB keysym name as described above
- _command_: any command that may be run with riverctl - _command_: any command that may be run with riverctl
*map-pointer* _mode_ _modifiers_ _button_ _action_ *map-pointer* _mode_ _modifiers_ _button_ _action_|_command_
Move or resize views when _button_ and _modifiers_ are held down Move or resize views or run _command_ when _button_ and _modifiers_ are held
while in the specified _mode_. down while in the specified _mode_. The view under the cursor will be
focused.
- _mode_: name of the mode for which to create the mapping - _mode_: name of the mode for which to create the mapping
- _modifiers_: one or more of the modifiers listed above, separated - _modifiers_: one or more of the modifiers listed above, separated
@ -223,6 +224,7 @@ A complete list may be found in _/usr/include/linux/input-event-codes.h_
- _action_: one of the following values: - _action_: one of the following values:
- move-view - move-view
- resize-view - resize-view
- _command_: any command that may be run with riverctl
*map-switch* _mode_ *lid*|*tablet* _state_ _command_ *map-switch* _mode_ *lid*|*tablet* _state_ _command_
Run _command_ when river receives a certain switch event. Run _command_ when river receives a certain switch event.

View File

@ -71,6 +71,9 @@ riverctl map-pointer normal Super BTN_LEFT move-view
# Super + Right Mouse Button to resize views # Super + Right Mouse Button to resize views
riverctl map-pointer normal Super BTN_RIGHT resize-view riverctl map-pointer normal Super BTN_RIGHT resize-view
# Super + Middle Mouse Button to toggle float
riverctl map-pointer normal Super BTN_MIDDLE toggle-float
for i in $(seq 1 9) for i in $(seq 1 9)
do do
tags=$((1 << ($i - 1))) tags=$((1 << ($i - 1)))

View File

@ -528,6 +528,10 @@ fn handlePointerMapping(self: *Self, event: *wlr.Pointer.event.Button, view: *Vi
switch (mapping.action) { switch (mapping.action) {
.move => if (!fullscreen) self.enterMode(.move, view), .move => if (!fullscreen) self.enterMode(.move, view),
.resize => if (!fullscreen) self.enterMode(.resize, view), .resize => if (!fullscreen) self.enterMode(.resize, view),
.command => |args| {
self.seat.focus(view);
self.seat.runCommand(args);
},
} }
break true; break true;
} }

View File

@ -14,13 +14,64 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const Self = @This();
const std = @import("std");
const wlr = @import("wlroots"); const wlr = @import("wlroots");
pub const Action = enum { const util = @import("util.zig");
pub const ActionType = enum {
move, move,
resize, resize,
command,
};
pub const Action = union(ActionType) {
move: void,
resize: void,
command: []const [:0]const u8,
}; };
event_code: u32, event_code: u32,
modifiers: wlr.Keyboard.ModifierMask, modifiers: wlr.Keyboard.ModifierMask,
action: Action, 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();
}

View File

@ -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; var out: ?[]const u8 = null;
defer if (out) |s| util.gpa.free(s); defer if (out) |s| util.gpa.free(s);
command.run(self, args, &out) catch |err| { 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.?, command.Error.Other => out.?,
else => command.errToMsg(err), 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; return;
}; };
if (out) |s| { if (out) |s| {

View File

@ -130,33 +130,31 @@ pub fn mapPointer(
out: *?[]const u8, out: *?[]const u8,
) Error!void { ) Error!void {
if (args.len < 5) return Error.NotEnoughArguments; if (args.len < 5) return Error.NotEnoughArguments;
if (args.len > 5) return Error.TooManyArguments;
const mode_id = try modeNameToId(args[1], out); const mode_id = try modeNameToId(args[1], out);
const modifiers = try parseModifiers(args[2], out); const modifiers = try parseModifiers(args[2], out);
const event_code = try parseEventCode(args[3], out); const event_code = try parseEventCode(args[3], out);
const action = if (mem.eql(u8, args[4], "move-view")) 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")) else if (mem.eql(u8, args[4], "resize-view"))
PointerMapping.Action.resize PointerMapping.ActionType.resize
else { else
out.* = try fmt.allocPrint( PointerMapping.ActionType.command;
util.gpa,
"invalid pointer action {s}, must be move-view or resize-view",
.{args[4]},
);
return Error.Other;
};
const new = PointerMapping{ if (action != PointerMapping.ActionType.command and args.len > 5) return Error.TooManyArguments;
.event_code = event_code,
.modifiers = modifiers, var new = try PointerMapping.init(
.action = action, event_code,
}; modifiers,
action,
args[4..],
);
errdefer new.deinit();
const mode_pointer_mappings = &server.config.modes.items[mode_id].pointer_mappings; const mode_pointer_mappings = &server.config.modes.items[mode_id].pointer_mappings;
if (pointerMappingExists(mode_pointer_mappings, modifiers, event_code)) |current| { if (pointerMappingExists(mode_pointer_mappings, modifiers, event_code)) |current| {
mode_pointer_mappings.items[current].deinit();
mode_pointer_mappings.items[current] = new; mode_pointer_mappings.items[current] = new;
} else { } else {
try mode_pointer_mappings.append(util.gpa, new); 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 mode_pointer_mappings = &server.config.modes.items[mode_id].pointer_mappings;
const mapping_idx = pointerMappingExists(mode_pointer_mappings, modifiers, event_code) orelse return; 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();
} }