river-control: implement protocol changes

This commit is contained in:
Isaac Freund 2020-06-15 22:24:54 +02:00
parent e7bf1940da
commit f0c3aa3744
No known key found for this signature in database
GPG key ID: 86DED400DDFD7A11
2 changed files with 77 additions and 38 deletions

View file

@ -23,17 +23,22 @@ const c = @import("c.zig");
const command = @import("command.zig");
const Log = @import("log.zig").Log;
const Seat = @import("Seat.zig");
const Server = @import("Server.zig");
const protocol_version = 1;
const implementation = c.struct_zriver_control_v1_interface{
.destroy = destroy,
.add_argument = addArgument,
.run_command = runCommand,
};
server: *Server,
wl_global: *c.wl_global,
args_map: std.AutoHashMap(u32, std.ArrayList([]const u8)),
listen_display_destroy: c.wl_listener,
pub fn init(self: *Self, server: *Server) !void {
@ -46,6 +51,8 @@ pub fn init(self: *Self, server: *Server) !void {
bind,
) orelse return error.CantCreateWlGlobal;
self.args_map = std.AutoHashMap(u32, std.ArrayList([]const u8)).init(server.allocator);
self.listen_display_destroy.notify = handleDisplayDestroy;
c.wl_display_add_destroy_listener(server.wl_display, &self.listen_display_destroy);
}
@ -53,6 +60,7 @@ pub fn init(self: *Self, server: *Server) !void {
fn handleDisplayDestroy(wl_listener: ?*c.wl_listener, data: ?*c_void) callconv(.C) void {
const self = @fieldParentPtr(Self, "listen_display_destroy", wl_listener.?);
c.wl_global_destroy(self.wl_global);
self.args_map.deinit();
}
/// Called when a client binds our global
@ -67,30 +75,55 @@ fn bind(wl_client: ?*c.wl_client, data: ?*c_void, version: u32, id: u32) callcon
c.wl_client_post_no_memory(wl_client);
return;
};
c.wl_resource_set_implementation(wl_resource, &implementation, self, null);
self.args_map.putNoClobber(id, std.ArrayList([]const u8).init(self.server.allocator)) catch {
c.wl_resource_destroy(wl_resource);
c.wl_client_post_no_memory(wl_client);
return;
};
c.wl_resource_set_implementation(wl_resource, &implementation, self, handleResourceDestroy);
}
/// Remove the resource from the hash map and free all stored args
fn handleResourceDestroy(wl_resource: ?*c.wl_resource) callconv(.C) void {
const self = @ptrCast(*Self, @alignCast(@alignOf(*Self), c.wl_resource_get_user_data(wl_resource)));
const id = c.wl_resource_get_id(wl_resource);
const list = self.args_map.remove(id).?.value;
for (list.items) |arg| list.allocator.free(arg);
list.deinit();
}
fn destroy(wl_client: ?*c.wl_client, wl_resource: ?*c.wl_resource) callconv(.C) void {
c.wl_resource_destroy(wl_resource);
}
fn addArgument(wl_client: ?*c.wl_client, wl_resource: ?*c.wl_resource, arg: ?[*:0]const u8) callconv(.C) void {
const self = @ptrCast(*Self, @alignCast(@alignOf(*Self), c.wl_resource_get_user_data(wl_resource)));
const id = c.wl_resource_get_id(wl_resource);
const allocator = self.server.allocator;
const owned_slice = std.mem.dupe(allocator, u8, std.mem.span(arg.?)) catch {
c.wl_client_post_no_memory(wl_client);
return;
};
self.args_map.get(id).?.value.append(owned_slice) catch {
c.wl_client_post_no_memory(wl_client);
allocator.free(owned_slice);
return;
};
}
fn runCommand(
wl_client: ?*c.wl_client,
wl_resource: ?*c.wl_resource,
wl_array: ?*c.wl_array,
seat_wl_resource: ?*c.wl_resource,
callback_id: u32,
) callconv(.C) void {
const self = @ptrCast(*Self, @alignCast(@alignOf(*Self), c.wl_resource_get_user_data(wl_resource)));
// This can be null if the seat is inert, in which case we ignore the request
const wlr_seat_client = c.wlr_seat_client_from_resource(seat_wl_resource) orelse return;
const seat = @ptrCast(*Seat, @alignCast(@alignOf(*Seat), wlr_seat_client.*.seat.*.data));
const allocator = self.server.allocator;
const seat = self.server.input_manager.default_seat;
var args = std.ArrayList([]const u8).init(allocator);
defer args.deinit();
var i: usize = 0;
const data = @ptrCast([*]const u8, wl_array.?.data);
while (i < wl_array.?.size) {
const slice = std.mem.spanZ(@ptrCast([*:0]const u8, &data[i]));
args.append(slice) catch unreachable;
i += slice.len + 1;
}
const callback_resource = c.wl_resource_create(
wl_client,
@ -101,11 +134,12 @@ fn runCommand(
c.wl_client_post_no_memory(wl_client);
return;
};
c.wl_resource_set_implementation(callback_resource, null, null, null);
const args = self.args_map.get(c.wl_resource_get_id(wl_resource)).?.value.items;
var failure_message: []const u8 = undefined;
command.run(allocator, seat, args.items, &failure_message) catch |err| {
command.run(allocator, seat, args, &failure_message) catch |err| {
if (err == command.Error.CommandFailed) {
defer allocator.free(failure_message);
const out = std.cstr.addNullByte(allocator, failure_message) catch {
@ -134,5 +168,5 @@ fn runCommand(
}
return;
};
c.zriver_command_callback_v1_send_success(callback_resource);
c.zriver_command_callback_v1_send_success(callback_resource, "");
}

View file

@ -33,6 +33,7 @@ const command_callback_listener = c.zriver_command_callback_v1_listener{
};
var river_control_optional: ?*c.zriver_control_v1 = null;
var wl_seat_optional: ?*c.wl_seat = null;
pub fn main() !void {
const wl_display = c.wl_display_connect(null) orelse return error.CantConnectToDisplay;
@ -43,21 +44,14 @@ pub fn main() !void {
if (c.wl_display_roundtrip(wl_display) < 0) return error.RoundtripFailed;
const river_control = river_control_optional orelse return error.RiverControlNotAdvertised;
const wl_seat = wl_seat_optional orelse return error.SeatNotAdverstised;
var command: c.wl_array = undefined;
c.wl_array_init(&command);
var it = std.process.args();
// Skip our name
_ = it.nextPosix();
while (it.nextPosix()) |arg| {
// Add one as we need to copy the null terminators as well
var ptr = @ptrCast([*]u8, c.wl_array_add(&command, arg.len + 1) orelse
return error.OutOfMemory);
for (arg) |ch, i| ptr[i] = ch;
ptr[arg.len] = 0;
}
// Skip our name, send all other args
// This next line is needed cause of https://github.com/ziglang/zig/issues/2622
const args = std.os.argv;
for (args[1..]) |arg| c.zriver_control_v1_add_argument(river_control, arg);
const command_callback = c.zriver_control_v1_run_command(river_control, &command);
const command_callback = c.zriver_control_v1_run_command(river_control, wl_seat);
if (c.zriver_command_callback_v1_add_listener(
command_callback,
&command_callback_listener,
@ -76,23 +70,34 @@ fn handleGlobal(
version: u32,
) callconv(.C) void {
// We only care about the river_control global
if (std.mem.eql(
u8,
std.mem.spanZ(interface.?),
std.mem.spanZ(@ptrCast([*:0]const u8, c.zriver_control_v1_interface.name.?)),
)) {
if (std.cstr.cmp(interface.?, @ptrCast([*:0]const u8, c.zriver_control_v1_interface.name.?)) == 0) {
river_control_optional = @ptrCast(
*c.zriver_control_v1,
c.wl_registry_bind(wl_registry, name, &c.zriver_control_v1_interface, 1),
);
} else if (std.cstr.cmp(interface.?, @ptrCast([*:0]const u8, c.wl_seat_interface.name.?)) == 0) {
// river does not yet support multi-seat, so just use the first
// (and only) seat advertised
wl_seat_optional = @ptrCast(
*c.wl_seat,
c.wl_registry_bind(wl_registry, name, &c.wl_seat_interface, 1),
);
}
}
/// Ignore the event
fn handleGlobalRemove(data: ?*c_void, wl_registry: ?*c.wl_registry, name: u32) callconv(.C) void {}
/// On success we simply exit with a clean exit code
fn handleSuccess(data: ?*c_void, callback: ?*c.zriver_command_callback_v1) callconv(.C) void {
/// Print the output of the command if any and exit
fn handleSuccess(
data: ?*c_void,
callback: ?*c.zriver_command_callback_v1,
output: ?[*:0]const u8,
) callconv(.C) void {
if (std.mem.len(output.?) > 0) {
const stdout = std.io.getStdOut().outStream();
stdout.print("{}\n", .{output}) catch @panic("failed to write to stdout");
}
std.os.exit(0);
}