code: Cleanup use of std library for consistancy

This commit is contained in:
Hugo Machet
2022-02-07 22:07:27 +01:00
committed by Isaac Freund
parent 995ae99be5
commit da59632cea
25 changed files with 103 additions and 97 deletions

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const mem = std.mem;
const util = @import("../util.zig");
@ -24,7 +25,7 @@ const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig");
pub fn attachMode(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -32,9 +33,9 @@ pub fn attachMode(
if (args.len < 2) return Error.NotEnoughArguments;
if (args.len > 2) return Error.TooManyArguments;
if (std.mem.eql(u8, "top", args[1])) {
if (mem.eql(u8, "top", args[1])) {
server.config.attach_mode = .top;
} else if (std.mem.eql(u8, "bottom", args[1])) {
} else if (mem.eql(u8, "bottom", args[1])) {
server.config.attach_mode = .bottom;
} else {
return Error.UnknownOption;

View File

@ -16,6 +16,7 @@
const std = @import("std");
const fmt = std.fmt;
const mem = std.mem;
const server = &@import("../main.zig").server;
@ -24,7 +25,7 @@ const Seat = @import("../Seat.zig");
const Config = @import("../Config.zig");
pub fn borderWidth(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -38,7 +39,7 @@ pub fn borderWidth(
}
pub fn backgroundColor(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -53,7 +54,7 @@ pub fn backgroundColor(
}
pub fn borderColorFocused(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -68,7 +69,7 @@ pub fn borderColorFocused(
}
pub fn borderColorUnfocused(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -83,7 +84,7 @@ pub fn borderColorUnfocused(
}
pub fn borderColorUrgent(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -98,7 +99,7 @@ pub fn borderColorUrgent(
}
pub fn setCursorWarp(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,

View File

@ -16,6 +16,7 @@
const std = @import("std");
const mem = std.mem;
const meta = std.meta;
const server = &@import("../main.zig").server;
const util = @import("../util.zig");
@ -156,37 +157,37 @@ pub fn input(
}
if (mem.eql(u8, "events", args[2])) {
input_config.event_state = std.meta.stringToEnum(InputConfig.EventState, args[3]) orelse
input_config.event_state = meta.stringToEnum(InputConfig.EventState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "accel-profile", args[2])) {
input_config.accel_profile = std.meta.stringToEnum(InputConfig.AccelProfile, args[3]) orelse
input_config.accel_profile = meta.stringToEnum(InputConfig.AccelProfile, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "click-method", args[2])) {
input_config.click_method = std.meta.stringToEnum(InputConfig.ClickMethod, args[3]) orelse
input_config.click_method = meta.stringToEnum(InputConfig.ClickMethod, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "drag", args[2])) {
input_config.drag_state = std.meta.stringToEnum(InputConfig.DragState, args[3]) orelse
input_config.drag_state = meta.stringToEnum(InputConfig.DragState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "drag-lock", args[2])) {
input_config.drag_lock = std.meta.stringToEnum(InputConfig.DragLock, args[3]) orelse
input_config.drag_lock = meta.stringToEnum(InputConfig.DragLock, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "disable-while-typing", args[2])) {
input_config.dwt_state = std.meta.stringToEnum(InputConfig.DwtState, args[3]) orelse
input_config.dwt_state = meta.stringToEnum(InputConfig.DwtState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "middle-emulation", args[2])) {
input_config.middle_emulation = std.meta.stringToEnum(InputConfig.MiddleEmulation, args[3]) orelse
input_config.middle_emulation = meta.stringToEnum(InputConfig.MiddleEmulation, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "natural-scroll", args[2])) {
input_config.natural_scroll = std.meta.stringToEnum(InputConfig.NaturalScroll, args[3]) orelse
input_config.natural_scroll = meta.stringToEnum(InputConfig.NaturalScroll, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "left-handed", args[2])) {
input_config.left_handed = std.meta.stringToEnum(InputConfig.LeftHanded, args[3]) orelse
input_config.left_handed = meta.stringToEnum(InputConfig.LeftHanded, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "tap", args[2])) {
input_config.tap_state = std.meta.stringToEnum(InputConfig.TapState, args[3]) orelse
input_config.tap_state = meta.stringToEnum(InputConfig.TapState, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "tap-button-map", args[2])) {
input_config.tap_button_map = std.meta.stringToEnum(InputConfig.TapButtonMap, args[3]) orelse
input_config.tap_button_map = meta.stringToEnum(InputConfig.TapButtonMap, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "pointer-accel", args[2])) {
input_config.pointer_accel = InputConfig.PointerAccel{
@ -197,7 +198,7 @@ pub fn input(
),
};
} else if (mem.eql(u8, "scroll-method", args[2])) {
input_config.scroll_method = std.meta.stringToEnum(InputConfig.ScrollMethod, args[3]) orelse
input_config.scroll_method = meta.stringToEnum(InputConfig.ScrollMethod, args[3]) orelse
return Error.UnknownOption;
} else if (mem.eql(u8, "scroll-button", args[2])) {
const ret = c.libevdev_event_code_from_name(c.EV_KEY, args[3]);

View File

@ -25,7 +25,7 @@ const Error = @import("../command.zig").Error;
const Seat = @import("../Seat.zig");
pub fn outputLayout(
_: std.mem.Allocator,
_: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -39,7 +39,7 @@ pub fn outputLayout(
}
pub fn defaultLayout(
_: std.mem.Allocator,
_: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -62,7 +62,7 @@ pub fn defaultLayout(
/// riverctl send-layout-cmd rivertile "mod-main-factor -0.1"
/// riverctl send-layout-cmd rivertile "main-location top"
pub fn sendLayoutCmd(
_: std.mem.Allocator,
_: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const fmt = std.fmt;
const mem = std.mem;
const wlr = @import("wlroots");
const xkb = @import("xkbcommon");
@ -33,7 +34,7 @@ const Seat = @import("../Seat.zig");
/// Example:
/// map normal Mod4+Shift Return spawn foot
pub fn map(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -63,7 +64,7 @@ pub fn map(
mode_mappings.items[current] = new;
// Warn user if they overwrote an existing keybinding using riverctl.
const opts = if (optionals.release) "-release " else "";
out.* = try std.fmt.allocPrint(
out.* = try fmt.allocPrint(
allocator,
"overwrote an existing keybinding: {s} {s}{s} {s}",
.{ mode_raw, opts, modifiers_raw, keysym_raw },
@ -82,7 +83,7 @@ pub fn map(
/// Example:
/// map-pointer normal Mod4 BTN_LEFT move-view
pub fn mapPointer(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -94,12 +95,12 @@ pub fn mapPointer(
const modifiers = try parseModifiers(allocator, args[2], out);
const event_code = try parseEventCode(allocator, args[3], out);
const action = if (std.mem.eql(u8, args[4], "move-view"))
const action = if (mem.eql(u8, args[4], "move-view"))
PointerMapping.Action.move
else if (std.mem.eql(u8, args[4], "resize-view"))
else if (mem.eql(u8, args[4], "resize-view"))
PointerMapping.Action.resize
else {
out.* = try std.fmt.allocPrint(
out.* = try fmt.allocPrint(
allocator,
"invalid pointer action {s}, must be move-view or resize-view",
.{args[4]},
@ -121,10 +122,10 @@ pub fn mapPointer(
}
}
fn modeNameToId(allocator: std.mem.Allocator, mode_name: []const u8, out: *?[]const u8) !usize {
fn modeNameToId(allocator: mem.Allocator, mode_name: []const u8, out: *?[]const u8) !usize {
const config = &server.config;
return config.mode_to_id.get(mode_name) orelse {
out.* = try std.fmt.allocPrint(
out.* = try fmt.allocPrint(
allocator,
"cannot add/remove mapping to/from non-existant mode '{s}'",
.{mode_name},
@ -164,31 +165,31 @@ fn pointerMappingExists(
return null;
}
fn parseEventCode(allocator: std.mem.Allocator, name: [:0]const u8, out: *?[]const u8) !u32 {
fn parseEventCode(allocator: mem.Allocator, name: [:0]const u8, out: *?[]const u8) !u32 {
const event_code = c.libevdev_event_code_from_name(c.EV_KEY, name);
if (event_code < 1) {
out.* = try std.fmt.allocPrint(allocator, "unknown button {s}", .{name});
out.* = try fmt.allocPrint(allocator, "unknown button {s}", .{name});
return Error.Other;
}
return @intCast(u32, event_code);
}
fn parseKeysym(allocator: std.mem.Allocator, name: [:0]const u8, out: *?[]const u8) !xkb.Keysym {
fn parseKeysym(allocator: mem.Allocator, name: [:0]const u8, out: *?[]const u8) !xkb.Keysym {
const keysym = xkb.Keysym.fromName(name, .case_insensitive);
if (keysym == .NoSymbol) {
out.* = try std.fmt.allocPrint(allocator, "invalid keysym '{s}'", .{name});
out.* = try fmt.allocPrint(allocator, "invalid keysym '{s}'", .{name});
return Error.Other;
}
return keysym;
}
fn parseModifiers(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
modifiers_str: []const u8,
out: *?[]const u8,
) !wlr.Keyboard.ModifierMask {
var it = std.mem.split(u8, modifiers_str, "+");
var it = mem.split(u8, modifiers_str, "+");
var modifiers = wlr.Keyboard.ModifierMask{};
outer: while (it.next()) |mod_name| {
if (mem.eql(u8, mod_name, "None")) continue;
@ -204,12 +205,12 @@ fn parseModifiers(
.{ .name = "Super", .field_name = "logo" },
.{ .name = "Mod5", .field_name = "mod5" },
}) |def| {
if (std.mem.eql(u8, def.name, mod_name)) {
if (mem.eql(u8, def.name, mod_name)) {
@field(modifiers, def.field_name) = true;
continue :outer;
}
}
out.* = try std.fmt.allocPrint(allocator, "invalid modifier '{s}'", .{mod_name});
out.* = try fmt.allocPrint(allocator, "invalid modifier '{s}'", .{mod_name});
return Error.Other;
}
return modifiers;
@ -236,10 +237,10 @@ fn parseOptionalArgs(args: []const []const u8) OptionalArgsContainer {
var i: usize = 0;
for (args) |arg| {
if (std.mem.eql(u8, arg, "-release")) {
if (mem.eql(u8, arg, "-release")) {
parsed.release = true;
i += 1;
} else if (std.mem.eql(u8, arg, "-repeat")) {
} else if (mem.eql(u8, arg, "-repeat")) {
parsed.repeat = true;
i += 1;
} else {
@ -257,7 +258,7 @@ fn parseOptionalArgs(args: []const []const u8) OptionalArgsContainer {
/// Example:
/// unmap normal Mod4+Shift Return
pub fn unmap(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -288,7 +289,7 @@ pub fn unmap(
/// Example:
/// unmap-pointer normal Mod4 BTN_LEFT
pub fn unmapPointer(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
_: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const math = std.math;
const server = &@import("../main.zig").server;
@ -100,11 +101,11 @@ pub fn resize(
} else {
// Prevent underflow
view.pending.box.width -=
std.math.min(view.pending.box.width, @intCast(u32, -1 * delta));
math.min(view.pending.box.width, @intCast(u32, -1 * delta));
}
view.applyConstraints();
// Do not grow bigger than the output
view.pending.box.width = std.math.min(
view.pending.box.width = math.min(
view.pending.box.width,
output_box.width - @intCast(u32, 2 * border_width),
);
@ -118,11 +119,11 @@ pub fn resize(
} else {
// Prevent underflow
view.pending.box.height -=
std.math.min(view.pending.box.height, @intCast(u32, -1 * delta));
math.min(view.pending.box.height, @intCast(u32, -1 * delta));
}
view.applyConstraints();
// Do not grow bigger than the output
view.pending.box.height = std.math.min(
view.pending.box.height = math.min(
view.pending.box.height,
output_box.height - @intCast(u32, 2 * border_width),
);

View File

@ -15,6 +15,8 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const assert = std.debug.assert;
const mem = std.mem;
const wlr = @import("wlroots");
@ -27,7 +29,7 @@ const Output = @import("../Output.zig");
const Seat = @import("../Seat.zig");
pub fn focusOutput(
_: std.mem.Allocator,
_: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -37,7 +39,7 @@ pub fn focusOutput(
// If the noop output is focused, there are no other outputs to switch to
if (seat.focused_output == &server.root.noop_output) {
std.debug.assert(server.root.outputs.len == 0);
assert(server.root.outputs.len == 0);
return;
}
@ -47,7 +49,7 @@ pub fn focusOutput(
}
pub fn sendToOutput(
_: std.mem.Allocator,
_: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
_: *?[]const u8,
@ -57,7 +59,7 @@ pub fn sendToOutput(
// If the noop output is focused, there is nowhere to send the view
if (seat.focused_output == &server.root.noop_output) {
std.debug.assert(server.root.outputs.len == 0);
assert(server.root.outputs.len == 0);
return;
}
@ -98,7 +100,7 @@ fn getOutput(seat: *Seat, str: []const u8) !?*Output {
// Check if an output matches by name
var it = server.root.outputs.first;
while (it) |node| : (it = node.next) {
if (std.mem.eql(u8, std.mem.span(node.data.wlr_output.name), str)) {
if (mem.eql(u8, mem.span(node.data.wlr_output.name), str)) {
return &node.data;
}
}

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const os = std.os;
const c = @import("../c.zig");
@ -33,7 +34,7 @@ pub fn spawn(
const child_args = [_:null]?[*:0]const u8{ "/bin/sh", "-c", args[1], null };
const pid = std.os.fork() catch {
const pid = os.fork() catch {
out.* = try std.fmt.allocPrint(allocator, "fork/execve failed", .{});
return Error.Other;
};
@ -41,18 +42,18 @@ pub fn spawn(
if (pid == 0) {
// Clean things up for the child in an intermediate fork
if (c.setsid() < 0) unreachable;
if (std.os.system.sigprocmask(std.os.SIG.SETMASK, &std.os.empty_sigset, null) < 0) unreachable;
if (os.system.sigprocmask(os.SIG.SETMASK, &os.empty_sigset, null) < 0) unreachable;
const pid2 = std.os.fork() catch c._exit(1);
if (pid2 == 0) std.os.execveZ("/bin/sh", &child_args, std.c.environ) catch c._exit(1);
const pid2 = os.fork() catch c._exit(1);
if (pid2 == 0) os.execveZ("/bin/sh", &child_args, std.c.environ) catch c._exit(1);
c._exit(0);
}
// Wait the intermediate child.
const ret = std.os.waitpid(pid, 0);
if (!std.os.W.IFEXITED(ret.status) or
(std.os.W.IFEXITED(ret.status) and std.os.W.EXITSTATUS(ret.status) != 0))
const ret = os.waitpid(pid, 0);
if (!os.W.IFEXITED(ret.status) or
(os.W.IFEXITED(ret.status) and os.W.EXITSTATUS(ret.status) != 0))
{
out.* = try std.fmt.allocPrint(allocator, "fork/execve failed", .{});
return Error.Other;

View File

@ -15,6 +15,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const mem = std.mem;
const server = &@import("../main.zig").server;
@ -23,7 +24,7 @@ const Seat = @import("../Seat.zig");
/// Switch focus to the passed tags.
pub fn setFocusedTags(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -40,7 +41,7 @@ pub fn setFocusedTags(
/// Set the spawn tagmask
pub fn spawnTagmask(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -51,7 +52,7 @@ pub fn spawnTagmask(
/// Set the tags of the focused view.
pub fn setViewTags(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -67,7 +68,7 @@ pub fn setViewTags(
/// Toggle focus of the passsed tags.
pub fn toggleFocusedTags(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -86,7 +87,7 @@ pub fn toggleFocusedTags(
/// Toggle the passed tags of the focused view
pub fn toggleViewTags(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
seat: *Seat,
args: []const [:0]const u8,
out: *?[]const u8,
@ -105,7 +106,7 @@ pub fn toggleViewTags(
/// Switch focus to tags that were selected previously
pub fn focusPreviousTags(
_: std.mem.Allocator,
_: mem.Allocator,
seat: *Seat,
args: []const []const u8,
_: *?[]const u8,
@ -123,7 +124,7 @@ pub fn focusPreviousTags(
/// Set the tags of the focused view to the tags that were selected previously
pub fn sendToPreviousTags(
_: std.mem.Allocator,
_: mem.Allocator,
seat: *Seat,
args: []const []const u8,
_: *?[]const u8,
@ -139,7 +140,7 @@ pub fn sendToPreviousTags(
}
fn parseTags(
allocator: std.mem.Allocator,
allocator: mem.Allocator,
args: []const [:0]const u8,
out: *?[]const u8,
) Error!u32 {