aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsaac Freund <ifreund@ifreund.xyz>2021-02-19 19:58:44 +0100
committerIsaac Freund <ifreund@ifreund.xyz>2021-02-19 19:58:44 +0100
commit4beb39920a591d65287ad82de65483747f68f0c2 (patch)
treea0e4730c6b712599b52cf0828e560f6aacc4e0f3
parent3d66cbd2d4f58cf7c8953f6746b25b1731f4e80e (diff)
downloadriver-4beb39920a591d65287ad82de65483747f68f0c2.tar.gz
river-4beb39920a591d65287ad82de65483747f68f0c2.tar.xz
river-control: fix various bugs
It kinda shows that this was the first protocol I ever implemented server-side: - Use client as well as ID for keys in the hashmap as IDs might (and will) be the same between clients. - Clear saved args after running a command.
-rw-r--r--river/Control.zig28
1 files changed, 19 insertions, 9 deletions
diff --git a/river/Control.zig b/river/Control.zig
index 8357d22..5b0fbd9 100644
--- a/river/Control.zig
+++ b/river/Control.zig
@@ -32,16 +32,18 @@ const util = @import("util.zig");
const Seat = @import("Seat.zig");
const Server = @import("Server.zig");
+const ArgMap = std.AutoHashMap(struct { client: *wl.Client, id: u32 }, std.ArrayListUnmanaged([]const u8));
+
global: *wl.Global,
-args_map: std.AutoHashMap(u32, std.ArrayList([]const u8)),
+args_map: ArgMap,
server_destroy: wl.Listener(*wl.Server) = wl.Listener(*wl.Server).init(handleServerDestroy),
pub fn init(self: *Self, server: *Server) !void {
self.* = .{
.global = try wl.Global.create(server.wl_server, zriver.ControlV1, 1, *Self, self, bind),
- .args_map = std.AutoHashMap(u32, std.ArrayList([]const u8)).init(util.gpa),
+ .args_map = ArgMap.init(util.gpa),
};
server.wl_server.addDestroyListener(&self.server_destroy);
@@ -59,7 +61,7 @@ fn bind(client: *wl.Client, self: *Self, version: u32, id: u32) callconv(.C) voi
client.postNoMemory();
return;
};
- self.args_map.putNoClobber(id, std.ArrayList([]const u8).init(util.gpa)) catch {
+ self.args_map.putNoClobber(.{ .client = client, .id = id }, .{}) catch {
control.destroy();
client.postNoMemory();
return;
@@ -76,7 +78,8 @@ fn handleRequest(control: *zriver.ControlV1, request: zriver.ControlV1.Request,
return;
};
- self.args_map.getEntry(control.getId()).?.value.append(owned_slice) catch {
+ const entry = self.args_map.getEntry(.{ .client = control.getClient(), .id = control.getId() }).?;
+ entry.value.append(util.gpa, owned_slice) catch {
control.getClient().postNoMemory();
util.gpa.free(owned_slice);
return;
@@ -94,7 +97,12 @@ fn handleRequest(control: *zriver.ControlV1, request: zriver.ControlV1.Request,
return;
};
- const args = self.args_map.get(control.getId()).?.items;
+ const entry = self.args_map.getEntry(.{ .client = control.getClient(), .id = control.getId() }).?;
+ defer {
+ for (entry.value.items) |arg| util.gpa.free(arg);
+ entry.value.items.len = 0;
+ }
+ const args = entry.value.items;
var out: ?[]const u8 = null;
defer if (out) |s| util.gpa.free(s);
@@ -116,7 +124,7 @@ fn handleRequest(control: *zriver.ControlV1, request: zriver.ControlV1.Request,
};
const success_message = if (out) |s|
- std.cstr.addNullByte(util.gpa, s) catch {
+ util.gpa.dupeZ(u8, s) catch {
callback.getClient().postNoMemory();
return;
}
@@ -130,7 +138,9 @@ fn handleRequest(control: *zriver.ControlV1, request: zriver.ControlV1.Request,
/// Remove the resource from the hash map and free all stored args
fn handleDestroy(control: *zriver.ControlV1, self: *Self) void {
- const list = self.args_map.remove(control.getId()).?.value;
- for (list.items) |arg| list.allocator.free(arg);
- list.deinit();
+ var list = self.args_map.remove(
+ .{ .client = control.getClient(), .id = control.getId() },
+ ).?.value;
+ for (list.items) |arg| util.gpa.free(arg);
+ list.deinit(util.gpa);
}