~novakane/rivercarro

ref: 67e0d63423c94424dfe523fee0928a5a2a466a7a rivercarro/src/main.zig -rw-r--r-- 18.2 KiB
67e0d634Hugo Machet README: Improve contributing section 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Layout generator for river <https://github.com/ifreund/river>
//
// Copyright 2021 Hugo Machet
//
// 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 <https://www.gnu.org/licenses/>.

const build_options = @import("build_options");

const std = @import("std");
const assert = std.debug.assert;
const mem = std.mem;
const math = std.math;
const os = std.os;

const wayland = @import("wayland");
const wl = wayland.client.wl;
const river = wayland.client.river;

const flags = @import("flags.zig");

const log = std.log.scoped(.rivercarro);

const usage =
    \\Usage: rivercarro [options]
    \\
    \\  -h              Print this help message and exit.
    \\  -version        Print the version number and exit.
    \\  -view-padding   Set the padding around views in pixels. (Default 6)
    \\  -outer-padding  Set the padding around the edge of the layout area in
    \\                  pixels. (Default 6)
    \\
    \\  The following commands may also be sent to rivercarro at runtime:
    \\
    \\  -main-location  Set the initial location of the main area in the
    \\                  layout. (Default left)
    \\  -main-count     Set the initial number of views in the main area of the
    \\                  layout. (Default 1)
    \\  -main-ratio     Set the initial ratio of main area to total layout
    \\                  area. (Default: 0.6)
    \\  -no-smart-gaps  Disable smart gaps
    \\
;

const Command = enum {
    @"main-location",
    @"main-count",
    @"main-ratio",
};

const Location = enum {
    top,
    right,
    bottom,
    left,
    monocle,
};

// Configured through command line options
var default_view_padding: u32 = 6;
var default_outer_padding: u32 = 6;
var default_main_location: Location = .left;
var default_main_count: u32 = 1;
var default_main_ratio: f64 = 0.6;
var smart_gaps: bool = true;

var only_one_view: bool = false;

/// We don't free resources on exit, only when output globals are removed.
const gpa = std.heap.c_allocator;

const Context = struct {
    initialized: bool = false,
    layout_manager: ?*river.LayoutManagerV3 = null,
    outputs: std.TailQueue(Output) = .{},

    fn addOutput(context: *Context, registry: *wl.Registry, name: u32) !void {
        const wl_output = try registry.bind(name, wl.Output, 3);
        errdefer wl_output.release();
        const node = try gpa.create(std.TailQueue(Output).Node);
        errdefer gpa.destroy(node);
        try node.data.init(context, wl_output, name);
        context.outputs.append(node);
    }
};

const Output = struct {
    wl_output: *wl.Output,
    name: u32,

    main_location: Location,
    main_count: u32,
    main_ratio: f64,

    outer_padding: u32,
    view_padding: u32,

    layout: *river.LayoutV3 = undefined,

    fn init(output: *Output, context: *Context, wl_output: *wl.Output, name: u32) !void {
        output.* = .{
            .wl_output = wl_output,
            .name = name,
            .main_location = default_main_location,
            .main_count = default_main_count,
            .main_ratio = default_main_ratio,
            .outer_padding = default_outer_padding,
            .view_padding = default_view_padding,
        };
        if (context.initialized) try output.getLayout(context);
    }

    fn getLayout(output: *Output, context: *Context) !void {
        assert(context.initialized);
        output.layout = try context.layout_manager.?.getLayout(output.wl_output, "rivercarro");
        output.layout.setListener(*Output, layoutListener, output);
    }

    fn deinit(output: *Output) void {
        output.wl_output.release();
        output.layout.destroy();
    }

    fn layoutListener(layout: *river.LayoutV3, event: river.LayoutV3.Event, output: *Output) void {
        switch (event) {
            .namespace_in_use => fatal("namespace 'rivercarro' already in use.", .{}),

            .user_command => |ev| {
                var it = mem.tokenize(mem.span(ev.command), " ");
                const raw_cmd = it.next() orelse {
                    log.err("not enough arguments", .{});
                    return;
                };
                const raw_arg = it.next() orelse {
                    log.err("not enough arguments", .{});
                    return;
                };
                if (it.next() != null) {
                    log.err("too many arguments", .{});
                    return;
                }
                const cmd = std.meta.stringToEnum(Command, raw_cmd) orelse {
                    log.err("unknown command: {s}", .{raw_cmd});
                    return;
                };
                switch (cmd) {
                    .@"main-location" => {
                        output.main_location = std.meta.stringToEnum(Location, raw_arg) orelse {
                            log.err("unknown location: {s}", .{raw_arg});
                            return;
                        };
                    },
                    .@"main-count" => {
                        const arg = std.fmt.parseInt(i32, raw_arg, 10) catch |err| {
                            log.err("failed to parse argument: {}", .{err});
                            return;
                        };
                        switch (raw_arg[0]) {
                            '+' => output.main_count = math.add(
                                u32,
                                output.main_count,
                                @intCast(u32, arg),
                            ) catch math.maxInt(u32),
                            '-' => {
                                const result = @as(i33, output.main_count) + arg;
                                if (result >= 0) output.main_count = @intCast(u32, result);
                            },
                            else => output.main_count = @intCast(u32, arg),
                        }
                    },
                    .@"main-ratio" => {
                        const arg = std.fmt.parseFloat(f64, raw_arg) catch |err| {
                            log.err("failed to parse argument: {}", .{err});
                            return;
                        };
                        switch (raw_arg[0]) {
                            '+', '-' => {
                                output.main_ratio = math.clamp(output.main_ratio + arg, 0.1, 0.9);
                            },
                            else => output.main_ratio = math.clamp(arg, 0.1, 0.9),
                        }
                    },
                }
            },

            .layout_demand => |ev| {
                const main_count = math.clamp(output.main_count, 1, ev.view_count);
                const secondary_count = if (ev.view_count > main_count)
                    ev.view_count - main_count
                else
                    0;

                only_one_view = if (ev.view_count == 1 or output.main_location == .monocle)
                    true
                else
                    false;

                // Don't add gaps if there is only one view
                if (only_one_view and smart_gaps) {
                    default_outer_padding = 0;
                    default_view_padding = 0;
                } else {
                    default_outer_padding = output.outer_padding;
                    default_view_padding = output.view_padding;
                }

                const usable_width = switch (output.main_location) {
                    .left, .right, .monocle => ev.usable_width - 2 * default_outer_padding,
                    .top, .bottom => ev.usable_height - 2 * default_outer_padding,
                };
                const usable_height = switch (output.main_location) {
                    .left, .right, .monocle => ev.usable_height - 2 * default_outer_padding,
                    .top, .bottom => ev.usable_width - 2 * default_outer_padding,
                };

                // to make things pixel-perfect, we make the first main and first secondary
                // view slightly larger if the height is not evenly divisible
                var main_width: u32 = undefined;
                var main_height: u32 = undefined;
                var main_height_rem: u32 = undefined;

                var secondary_width: u32 = undefined;
                var secondary_height: u32 = undefined;
                var secondary_height_rem: u32 = undefined;

                if (output.main_location == .monocle) {
                    main_width = usable_width;
                    main_height = usable_height;

                    secondary_width = usable_width;
                    secondary_height = usable_height;
                } else {
                    if (main_count > 0 and secondary_count > 0) {
                        main_width = @floatToInt(u32, output.main_ratio * @intToFloat(f64, usable_width));
                        main_height = usable_height / main_count;
                        main_height_rem = usable_height % main_count;

                        secondary_width = usable_width - main_width;
                        secondary_height = usable_height / secondary_count;
                        secondary_height_rem = usable_height % secondary_count;
                    } else if (main_count > 0) {
                        main_width = usable_width;
                        main_height = usable_height / main_count;
                        main_height_rem = usable_height % main_count;
                    } else if (secondary_width > 0) {
                        main_width = 0;
                        secondary_width = usable_width;
                        secondary_height = usable_height / secondary_count;
                        secondary_height_rem = usable_height % secondary_count;
                    }
                }

                var i: u32 = 0;
                while (i < ev.view_count) : (i += 1) {
                    var x: i32 = undefined;
                    var y: i32 = undefined;
                    var width: u32 = undefined;
                    var height: u32 = undefined;

                    if (output.main_location == .monocle) {
                        x = 0;
                        y = 0;
                        width = main_width;
                        height = main_height;
                    } else {
                        if (i < main_count) {
                            x = 0;
                            y = @intCast(i32, (i * main_height) +
                                if (i > 0) default_view_padding else 0 +
                                if (i > 0) main_height_rem else 0);
                            width = main_width - default_view_padding / 2;
                            height = main_height -
                                if (i > 0) default_view_padding else 0 +
                                if (i == 0) main_height_rem else 0;
                        } else {
                            x = @intCast(i32, (main_width - default_view_padding / 2) + default_view_padding);
                            y = @intCast(i32, ((i - main_count) * secondary_height) +
                                if (i > main_count) default_view_padding else 0 +
                                if (i > main_count) secondary_height_rem else 0);
                            width = secondary_width - default_view_padding / 2;
                            height = secondary_height -
                                if (i > main_count) default_view_padding else 0 +
                                if (i == main_count) secondary_height_rem else 0;
                        }
                    }

                    switch (output.main_location) {
                        .left => layout.pushViewDimensions(
                            x + @intCast(i32, default_outer_padding),
                            y + @intCast(i32, default_outer_padding),
                            width,
                            height,
                            ev.serial,
                        ),
                        .right => layout.pushViewDimensions(
                            @intCast(i32, usable_width - width) - x + @intCast(i32, default_outer_padding),
                            y + @intCast(i32, default_outer_padding),
                            width,
                            height,
                            ev.serial,
                        ),
                        .top => layout.pushViewDimensions(
                            y + @intCast(i32, default_outer_padding),
                            x + @intCast(i32, default_outer_padding),
                            height,
                            width,
                            ev.serial,
                        ),
                        .bottom => layout.pushViewDimensions(
                            y + @intCast(i32, default_outer_padding),
                            @intCast(i32, usable_width - width) - x + @intCast(i32, default_outer_padding),
                            height,
                            width,
                            ev.serial,
                        ),
                        .monocle => layout.pushViewDimensions(
                            x + @intCast(i32, default_outer_padding),
                            y + @intCast(i32, default_outer_padding),
                            width,
                            height,
                            ev.serial,
                        ),
                    }
                }

                switch (output.main_location) {
                    .left => layout.commit("left", ev.serial),
                    .right => layout.commit("right", ev.serial),
                    .top => layout.commit("top", ev.serial),
                    .bottom => layout.commit("bottom", ev.serial),
                    .monocle => layout.commit("monocle", ev.serial),
                }
            },
        }
    }
};

pub fn main() !void {
    // https://github.com/ziglang/zig/issues/7807
    const argv: [][*:0]const u8 = os.argv;
    const result = flags.parse(argv[1..], &[_]flags.Flag{
        .{ .name = "-h", .kind = .boolean },
        .{ .name = "-version", .kind = .boolean },
        .{ .name = "-view-padding", .kind = .arg },
        .{ .name = "-outer-padding", .kind = .arg },
        .{ .name = "-main-location", .kind = .arg },
        .{ .name = "-main-count", .kind = .arg },
        .{ .name = "-main-ratio", .kind = .arg },
        .{ .name = "-no-smart-gaps", .kind = .boolean },
    }) catch {
        try std.io.getStdErr().writeAll(usage);
        os.exit(1);
    };
    if (result.args.len != 0) fatalPrintUsage("unknown option '{s}'", .{result.args[0]});

    if (result.boolFlag("-h")) {
        try std.io.getStdOut().writeAll(usage);
        os.exit(0);
    }
    if (result.boolFlag("-version")) {
        try std.io.getStdOut().writeAll(build_options.version ++ "\n");
        os.exit(0);
    }
    if (result.argFlag("-view-padding")) |raw| {
        default_view_padding = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch
            fatalPrintUsage("invalid value '{s}' provided to -view-padding", .{raw});
    }
    if (result.argFlag("-outer-padding")) |raw| {
        default_outer_padding = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch
            fatalPrintUsage("invalid value '{s}' provided to -outer-padding", .{raw});
    }
    if (result.argFlag("-main-location")) |raw| {
        default_main_location = std.meta.stringToEnum(Location, mem.span(raw)) orelse
            fatalPrintUsage("invalid value '{s}' provided to -main-location", .{raw});
    }
    if (result.argFlag("-main-count")) |raw| {
        default_main_count = std.fmt.parseUnsigned(u32, mem.span(raw), 10) catch
            fatalPrintUsage("invalid value '{s}' provided to -main-count", .{raw});
    }
    if (result.argFlag("-main-ratio")) |raw| {
        default_main_ratio = std.fmt.parseFloat(f64, mem.span(raw)) catch
            fatalPrintUsage("invalid value '{s}' provided to -main-ratio", .{raw});
    }
    if (result.boolFlag("-no-smart-gaps")) {
        smart_gaps = false;
    }

    const display = wl.Display.connect(null) catch {
        std.debug.warn("Unable to connect to Wayland server.\n", .{});
        os.exit(1);
    };
    defer display.disconnect();

    var context: Context = .{};

    const registry = try display.getRegistry();
    registry.setListener(*Context, registryListener, &context);
    _ = try display.roundtrip();

    if (context.layout_manager == null) {
        fatal("Wayland compositor does not support river_layout_v3.\n", .{});
    }

    context.initialized = true;

    var it = context.outputs.first;
    while (it) |node| : (it = node.next) {
        const output = &node.data;
        try output.getLayout(&context);
    }

    while (true) _ = try display.dispatch();
}

fn registryListener(registry: *wl.Registry, event: wl.Registry.Event, context: *Context) void {
    switch (event) {
        .global => |global| {
            if (std.cstr.cmp(global.interface, river.LayoutManagerV3.getInterface().name) == 0) {
                context.layout_manager = registry.bind(global.name, river.LayoutManagerV3, 1) catch return;
            } else if (std.cstr.cmp(global.interface, wl.Output.getInterface().name) == 0) {
                context.addOutput(registry, global.name) catch |err|
                    fatal("failed to bind output: {}", .{err});
            }
        },
        .global_remove => |ev| {
            var it = context.outputs.first;
            while (it) |node| : (it = node.next) {
                const output = &node.data;
                if (output.name == ev.name) {
                    context.outputs.remove(node);
                    output.deinit();
                    gpa.destroy(node);
                    break;
                }
            }
        },
    }
}

fn fatal(comptime format: []const u8, args: anytype) noreturn {
    log.err(format, args);
    os.exit(1);
}

fn fatalPrintUsage(comptime format: []const u8, args: anytype) noreturn {
    log.err(format, args);
    std.io.getStdErr().writeAll(usage) catch {};
    os.exit(1);
}