2020-05-19 04:25:16 -07:00
|
|
|
// This file is part of river, a dynamic tiling wayland compositor.
|
|
|
|
//
|
2020-11-11 11:30:21 -08:00
|
|
|
// Copyright 2020 The River Developers
|
2020-05-19 04:25:16 -07:00
|
|
|
//
|
|
|
|
// 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
|
2022-01-31 10:33:22 -08:00
|
|
|
// the Free Software Foundation, version 3.
|
2020-05-19 04:25:16 -07:00
|
|
|
//
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
const Self = @This();
|
|
|
|
|
|
|
|
const std = @import("std");
|
2020-11-03 15:23:21 -08:00
|
|
|
const mem = std.mem;
|
2021-05-13 06:06:00 -07:00
|
|
|
const wlr = @import("wlroots");
|
2020-11-03 15:23:21 -08:00
|
|
|
const wayland = @import("wayland");
|
|
|
|
const wl = wayland.server.wl;
|
|
|
|
const zriver = wayland.server.zriver;
|
|
|
|
|
2020-05-26 13:55:07 -07:00
|
|
|
const command = @import("command.zig");
|
2021-05-13 06:06:00 -07:00
|
|
|
const server = &@import("main.zig").server;
|
2020-06-16 11:54:05 -07:00
|
|
|
const util = @import("util.zig");
|
2020-05-19 04:25:16 -07:00
|
|
|
|
2020-06-15 13:24:54 -07:00
|
|
|
const Seat = @import("Seat.zig");
|
2020-05-19 04:25:16 -07:00
|
|
|
const Server = @import("Server.zig");
|
|
|
|
|
2021-07-14 15:13:16 -07:00
|
|
|
const ArgMap = std.AutoHashMap(struct { client: *wl.Client, id: u32 }, std.ArrayListUnmanaged([:0]const u8));
|
2021-02-19 10:58:44 -08:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
global: *wl.Global,
|
2020-05-19 04:25:16 -07:00
|
|
|
|
2021-02-19 10:58:44 -08:00
|
|
|
args_map: ArgMap,
|
2020-06-15 13:24:54 -07:00
|
|
|
|
2020-12-31 06:35:35 -08:00
|
|
|
server_destroy: wl.Listener(*wl.Server) = wl.Listener(*wl.Server).init(handleServerDestroy),
|
2020-05-19 04:25:16 -07:00
|
|
|
|
2021-05-13 06:06:00 -07:00
|
|
|
pub fn init(self: *Self) !void {
|
2020-08-21 10:47:57 -07:00
|
|
|
self.* = .{
|
2020-11-03 15:23:21 -08:00
|
|
|
.global = try wl.Global.create(server.wl_server, zriver.ControlV1, 1, *Self, self, bind),
|
2021-02-19 10:58:44 -08:00
|
|
|
.args_map = ArgMap.init(util.gpa),
|
2020-08-21 10:47:57 -07:00
|
|
|
};
|
2020-06-15 13:24:54 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
server.wl_server.addDestroyListener(&self.server_destroy);
|
2020-05-19 04:25:16 -07:00
|
|
|
}
|
|
|
|
|
2021-10-11 03:44:46 -07:00
|
|
|
fn handleServerDestroy(listener: *wl.Listener(*wl.Server), _: *wl.Server) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
const self = @fieldParentPtr(Self, "server_destroy", listener);
|
|
|
|
self.global.destroy();
|
2020-06-15 13:24:54 -07:00
|
|
|
self.args_map.deinit();
|
2020-05-19 04:25:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Called when a client binds our global
|
2022-11-04 16:29:51 -07:00
|
|
|
fn bind(client: *wl.Client, self: *Self, version: u32, id: u32) void {
|
2020-11-03 15:23:21 -08:00
|
|
|
const control = zriver.ControlV1.create(client, version, id) catch {
|
|
|
|
client.postNoMemory();
|
2020-05-19 04:25:16 -07:00
|
|
|
return;
|
|
|
|
};
|
2021-02-19 10:58:44 -08:00
|
|
|
self.args_map.putNoClobber(.{ .client = client, .id = id }, .{}) catch {
|
2020-11-03 15:23:21 -08:00
|
|
|
control.destroy();
|
|
|
|
client.postNoMemory();
|
2020-06-15 13:24:54 -07:00
|
|
|
return;
|
|
|
|
};
|
2020-11-03 15:23:21 -08:00
|
|
|
control.setHandler(*Self, handleRequest, handleDestroy, self);
|
2020-05-19 04:25:16 -07:00
|
|
|
}
|
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
fn handleRequest(control: *zriver.ControlV1, request: zriver.ControlV1.Request, self: *Self) void {
|
|
|
|
switch (request) {
|
|
|
|
.destroy => control.destroy(),
|
|
|
|
.add_argument => |add_argument| {
|
2021-07-14 15:13:16 -07:00
|
|
|
const owned_slice = util.gpa.dupeZ(u8, mem.span(add_argument.argument)) catch {
|
2020-11-03 15:23:21 -08:00
|
|
|
control.getClient().postNoMemory();
|
|
|
|
return;
|
|
|
|
};
|
2020-06-15 13:24:54 -07:00
|
|
|
|
2021-05-23 04:35:37 -07:00
|
|
|
const args = self.args_map.getPtr(.{ .client = control.getClient(), .id = control.getId() }).?;
|
|
|
|
args.append(util.gpa, owned_slice) catch {
|
2020-11-03 15:23:21 -08:00
|
|
|
control.getClient().postNoMemory();
|
|
|
|
util.gpa.free(owned_slice);
|
2020-06-01 05:41:44 -07:00
|
|
|
return;
|
2020-11-03 15:23:21 -08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
.run_command => |run_command| {
|
|
|
|
const seat = @intToPtr(*Seat, wlr.Seat.Client.fromWlSeat(run_command.seat).?.seat.data);
|
|
|
|
|
|
|
|
const callback = zriver.CommandCallbackV1.create(
|
|
|
|
control.getClient(),
|
|
|
|
control.getVersion(),
|
|
|
|
run_command.callback,
|
|
|
|
) catch {
|
|
|
|
control.getClient().postNoMemory();
|
2020-06-26 08:57:03 -07:00
|
|
|
return;
|
2020-11-03 15:23:21 -08:00
|
|
|
};
|
|
|
|
|
2021-05-23 04:35:37 -07:00
|
|
|
const args = self.args_map.getPtr(.{ .client = control.getClient(), .id = control.getId() }).?;
|
2021-02-19 10:58:44 -08:00
|
|
|
defer {
|
2021-05-23 04:35:37 -07:00
|
|
|
for (args.items) |arg| util.gpa.free(arg);
|
|
|
|
args.items.len = 0;
|
2021-02-19 10:58:44 -08:00
|
|
|
}
|
2020-11-03 15:23:21 -08:00
|
|
|
|
|
|
|
var out: ?[]const u8 = null;
|
|
|
|
defer if (out) |s| util.gpa.free(s);
|
2022-02-08 03:27:32 -08:00
|
|
|
command.run(seat, args.items, &out) catch |err| {
|
2020-11-03 15:23:21 -08:00
|
|
|
const failure_message = switch (err) {
|
|
|
|
command.Error.OutOfMemory => {
|
|
|
|
callback.getClient().postNoMemory();
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
command.Error.Other => std.cstr.addNullByte(util.gpa, out.?) catch {
|
|
|
|
callback.getClient().postNoMemory();
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
else => command.errToMsg(err),
|
|
|
|
};
|
|
|
|
defer if (err == command.Error.Other) util.gpa.free(failure_message);
|
2022-12-31 14:19:18 -08:00
|
|
|
callback.destroySendFailure(failure_message);
|
2020-11-03 15:23:21 -08:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
const success_message = if (out) |s|
|
2021-02-19 10:58:44 -08:00
|
|
|
util.gpa.dupeZ(u8, s) catch {
|
2020-11-03 15:23:21 -08:00
|
|
|
callback.getClient().postNoMemory();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
"";
|
|
|
|
defer if (out != null) util.gpa.free(success_message);
|
2022-12-31 14:19:18 -08:00
|
|
|
callback.destroySendSuccess(success_message);
|
2020-11-03 15:23:21 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 08:57:03 -07:00
|
|
|
|
2020-11-03 15:23:21 -08:00
|
|
|
/// Remove the resource from the hash map and free all stored args
|
|
|
|
fn handleDestroy(control: *zriver.ControlV1, self: *Self) void {
|
2021-05-23 04:35:37 -07:00
|
|
|
var args = self.args_map.fetchRemove(
|
2021-02-19 10:58:44 -08:00
|
|
|
.{ .client = control.getClient(), .id = control.getId() },
|
|
|
|
).?.value;
|
2021-05-23 04:35:37 -07:00
|
|
|
for (args.items) |arg| util.gpa.free(arg);
|
|
|
|
args.deinit(util.gpa);
|
2020-05-19 04:25:16 -07:00
|
|
|
}
|