From 62abfc5ee508a4b4186b120619c7f428ed95fa43 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 1 Jun 2020 15:16:18 +0200 Subject: Rename bind -> map --- src/command/bind.zig | 109 ------------------------------------------ src/command/declare_mode.zig | 6 +-- src/command/map.zig | 110 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 112 deletions(-) delete mode 100644 src/command/bind.zig create mode 100644 src/command/map.zig (limited to 'src/command') diff --git a/src/command/bind.zig b/src/command/bind.zig deleted file mode 100644 index f8571bc..0000000 --- a/src/command/bind.zig +++ /dev/null @@ -1,109 +0,0 @@ -// This file is part of river, a dynamic tiling wayland compositor. -// -// Copyright 2020 Isaac Freund -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -const std = @import("std"); - -const c = @import("../c.zig"); - -const Error = @import("../command.zig").Error; -const Keybind = @import("../Keybind.zig"); -const Seat = @import("../Seat.zig"); - -const modifier_names = [_]struct { - name: []const u8, - modifier: u32, -}{ - .{ .name = "Shift", .modifier = c.WLR_MODIFIER_SHIFT }, - .{ .name = "Lock", .modifier = c.WLR_MODIFIER_CAPS }, - .{ .name = "Control", .modifier = c.WLR_MODIFIER_CTRL }, - .{ .name = "Mod1", .modifier = c.WLR_MODIFIER_ALT }, - .{ .name = "Mod2", .modifier = c.WLR_MODIFIER_MOD2 }, - .{ .name = "Mod3", .modifier = c.WLR_MODIFIER_MOD3 }, - .{ .name = "Mod4", .modifier = c.WLR_MODIFIER_LOGO }, - .{ .name = "Mod5", .modifier = c.WLR_MODIFIER_MOD5 }, -}; - -/// Create a new keybind for a given mode -/// -/// bind normal Control|Shift|Mod4 Comma spawn alacritty -pub fn bind( - allocator: *std.mem.Allocator, - seat: *Seat, - args: []const []const u8, - failure_message: *[]const u8, -) Error!void { - if (args.len < 4) return Error.NotEnoughArguments; - - // Parse the mode - const config = seat.input_manager.server.config; - const target_mode = args[1]; - const mode_id = config.mode_to_id.getValue(target_mode) orelse { - failure_message.* = try std.fmt.allocPrint( - allocator, - "cannot add keybind to non-existant mode '{}'", - .{target_mode}, - ); - return Error.CommandFailed; - }; - - // Parse the modifiers - var it = std.mem.split(args[2], "|"); - var modifiers: u32 = 0; - while (it.next()) |mod_name| { - for (modifier_names) |def| { - if (std.mem.eql(u8, def.name, mod_name)) { - modifiers |= def.modifier; - break; - } - } else { - failure_message.* = try std.fmt.allocPrint( - allocator, - "invalid modifier '{}'", - .{mod_name}, - ); - return Error.CommandFailed; - } - } - - // Parse the keysym - const keysym_name = try std.cstr.addNullByte(allocator, args[3]); - defer allocator.free(keysym_name); - const keysym = c.xkb_keysym_from_name(keysym_name, .XKB_KEYSYM_CASE_INSENSITIVE); - if (keysym == c.XKB_KEY_NoSymbol) { - failure_message.* = try std.fmt.allocPrint( - allocator, - "invalid keysym '{}'", - .{args[3]}, - ); - return Error.CommandFailed; - } - - // Check if the mapping already exists - const mode_mappings = &config.modes.items[mode_id]; - for (mode_mappings.items) |existant_mapping| { - if (existant_mapping.modifiers == modifiers and existant_mapping.keysym == keysym) { - failure_message.* = try std.fmt.allocPrint( - allocator, - "a mapping for modifiers '{}' and keysym '{}' already exists", - .{ args[2], args[3] }, - ); - return Error.CommandFailed; - } - } - - try mode_mappings.append(try Keybind.init(allocator, keysym, modifiers, args[4..])); -} diff --git a/src/command/declare_mode.zig b/src/command/declare_mode.zig index 6cc1489..943ede8 100644 --- a/src/command/declare_mode.zig +++ b/src/command/declare_mode.zig @@ -20,10 +20,10 @@ const std = @import("std"); const c = @import("../c.zig"); const Error = @import("../command.zig").Error; -const Keybind = @import("../Keybind.zig"); +const Mapping = @import("../Mapping.zig"); const Seat = @import("../Seat.zig"); -/// Declare a new keybind mode +/// Declare a new keymap mode pub fn declareMode( allocator: *std.mem.Allocator, seat: *Seat, @@ -47,5 +47,5 @@ pub fn declareMode( try config.mode_to_id.putNoClobber(new_mode_name, config.modes.items.len); errdefer _ = config.mode_to_id.remove(new_mode_name); - try config.modes.append(std.ArrayList(Keybind).init(allocator)); + try config.modes.append(std.ArrayList(Mapping).init(allocator)); } diff --git a/src/command/map.zig b/src/command/map.zig new file mode 100644 index 0000000..90c0b33 --- /dev/null +++ b/src/command/map.zig @@ -0,0 +1,110 @@ +// This file is part of river, a dynamic tiling wayland compositor. +// +// Copyright 2020 Isaac Freund +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +const std = @import("std"); + +const c = @import("../c.zig"); + +const Error = @import("../command.zig").Error; +const Mapping = @import("../Mapping.zig"); +const Seat = @import("../Seat.zig"); + +const modifier_names = [_]struct { + name: []const u8, + modifier: u32, +}{ + .{ .name = "Shift", .modifier = c.WLR_MODIFIER_SHIFT }, + .{ .name = "Lock", .modifier = c.WLR_MODIFIER_CAPS }, + .{ .name = "Control", .modifier = c.WLR_MODIFIER_CTRL }, + .{ .name = "Mod1", .modifier = c.WLR_MODIFIER_ALT }, + .{ .name = "Mod2", .modifier = c.WLR_MODIFIER_MOD2 }, + .{ .name = "Mod3", .modifier = c.WLR_MODIFIER_MOD3 }, + .{ .name = "Mod4", .modifier = c.WLR_MODIFIER_LOGO }, + .{ .name = "Mod5", .modifier = c.WLR_MODIFIER_MOD5 }, +}; + +/// Create a new mapping for a given mode +/// +/// Example: +/// map normal Mod4|Shift Return spawn alacritty +pub fn map( + allocator: *std.mem.Allocator, + seat: *Seat, + args: []const []const u8, + failure_message: *[]const u8, +) Error!void { + if (args.len < 4) return Error.NotEnoughArguments; + + // Parse the mode + const config = seat.input_manager.server.config; + const target_mode = args[1]; + const mode_id = config.mode_to_id.getValue(target_mode) orelse { + failure_message.* = try std.fmt.allocPrint( + allocator, + "cannot add mapping to non-existant mode '{}p'", + .{target_mode}, + ); + return Error.CommandFailed; + }; + + // Parse the modifiers + var it = std.mem.split(args[2], "|"); + var modifiers: u32 = 0; + while (it.next()) |mod_name| { + for (modifier_names) |def| { + if (std.mem.eql(u8, def.name, mod_name)) { + modifiers |= def.modifier; + break; + } + } else { + failure_message.* = try std.fmt.allocPrint( + allocator, + "invalid modifier '{}'", + .{mod_name}, + ); + return Error.CommandFailed; + } + } + + // Parse the keysym + const keysym_name = try std.cstr.addNullByte(allocator, args[3]); + defer allocator.free(keysym_name); + const keysym = c.xkb_keysym_from_name(keysym_name, .XKB_KEYSYM_CASE_INSENSITIVE); + if (keysym == c.XKB_KEY_NoSymbol) { + failure_message.* = try std.fmt.allocPrint( + allocator, + "invalid keysym '{}'", + .{args[3]}, + ); + return Error.CommandFailed; + } + + // Check if the mapping already exists + const mode_mappings = &config.modes.items[mode_id]; + for (mode_mappings.items) |existant_mapping| { + if (existant_mapping.modifiers == modifiers and existant_mapping.keysym == keysym) { + failure_message.* = try std.fmt.allocPrint( + allocator, + "a mapping for modifiers '{}' and keysym '{}' already exists", + .{ args[2], args[3] }, + ); + return Error.CommandFailed; + } + } + + try mode_mappings.append(try Mapping.init(allocator, keysym, modifiers, args[4..])); +} -- cgit v1.2.3