oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git
10,797 bytes raw
1
const std = @import("std");
2
const client_mod = @import("../client.zig");
3
const monitor_mod = @import("../monitor.zig");
4
const xlib = @import("../x11/xlib.zig");
5
6
const Client = client_mod.Client;
7
const Monitor = monitor_mod.Monitor;
8
9
pub const layout = monitor_mod.Layout{
10
    .symbol = "[]=",
11
    .arrange_fn = tile,
12
};
13
14
pub var display_handle: ?*xlib.Display = null;
15
pub var screen_width: i32 = 0;
16
pub var screen_height: i32 = 0;
17
pub var bar_height: i32 = 0;
18
19
pub fn set_display(display: *xlib.Display) void {
20
    display_handle = display;
21
}
22
23
pub fn set_screen_size(width: i32, height: i32) void {
24
    screen_width = width;
25
    screen_height = height;
26
}
27
28
pub fn set_bar_height(height: i32) void {
29
    bar_height = height;
30
}
31
32
pub fn tile(monitor: *Monitor) void {
33
    var gap_outer_h: i32 = 0;
34
    var gap_outer_v: i32 = 0;
35
    var gap_inner_h: i32 = 0;
36
    var gap_inner_v: i32 = 0;
37
    var client_count: u32 = 0;
38
39
    get_gaps(monitor, &gap_outer_h, &gap_outer_v, &gap_inner_h, &gap_inner_v, &client_count);
40
    if (client_count == 0) return;
41
42
    const nmaster: i32 = monitor.nmaster;
43
    const nmaster_count: u32 = @intCast(@max(0, nmaster));
44
45
    const master_x: i32 = monitor.win_x + gap_outer_v;
46
    var master_y: i32 = monitor.win_y + gap_outer_h;
47
    const master_height: i32 = monitor.win_h - 2 * gap_outer_h - gap_inner_h * (@as(i32, @intCast(@min(client_count, nmaster_count))) - 1);
48
    var master_width: i32 = monitor.win_w - 2 * gap_outer_v;
49
50
    var stack_x: i32 = master_x;
51
    var stack_y: i32 = monitor.win_y + gap_outer_h;
52
    const stack_height: i32 = monitor.win_h - 2 * gap_outer_h - gap_inner_h * (@as(i32, @intCast(client_count)) - nmaster - 1);
53
    var stack_width: i32 = master_width;
54
55
    if (nmaster > 0 and client_count > nmaster_count) {
56
        stack_width = @intFromFloat(@as(f32, @floatFromInt(master_width - gap_inner_v)) * (1.0 - monitor.mfact));
57
        master_width = master_width - gap_inner_v - stack_width;
58
        stack_x = master_x + master_width + gap_inner_v;
59
    }
60
61
    var master_facts: f32 = 0;
62
    var stack_facts: f32 = 0;
63
    var master_rest: i32 = 0;
64
    var stack_rest: i32 = 0;
65
    get_facts(monitor, master_height, stack_height, &master_facts, &stack_facts, &master_rest, &stack_rest);
66
67
    var index: u32 = 0;
68
    var current = client_mod.next_tiled(monitor.clients);
69
    while (current) |client| : (current = client_mod.next_tiled(client.next)) {
70
        if (index < nmaster_count) {
71
            const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_height)) / master_facts)) + (if (index < @as(u32, @intCast(master_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width;
72
            resize(client, master_x, master_y, master_width - 2 * client.border_width, height, false);
73
            master_y += get_client_height(client) + gap_inner_h;
74
        } else {
75
            const stack_index = index - nmaster_count;
76
            const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_height)) / stack_facts)) + (if (stack_index < @as(u32, @intCast(stack_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width;
77
            resize(client, stack_x, stack_y, stack_width - 2 * client.border_width, height, false);
78
            stack_y += get_client_height(client) + gap_inner_h;
79
        }
80
        index += 1;
81
    }
82
}
83
84
fn get_gaps(monitor: *Monitor, gap_outer_h: *i32, gap_outer_v: *i32, gap_inner_h: *i32, gap_inner_v: *i32, client_count: *u32) void {
85
    var count: u32 = 0;
86
    var current = client_mod.next_tiled(monitor.clients);
87
    while (current) |client| : (current = client_mod.next_tiled(client.next)) {
88
        count += 1;
89
    }
90
91
    gap_outer_h.* = monitor.gap_outer_h;
92
    gap_outer_v.* = monitor.gap_outer_v;
93
    gap_inner_h.* = monitor.gap_inner_h;
94
    gap_inner_v.* = monitor.gap_inner_v;
95
    client_count.* = count;
96
}
97
98
fn get_facts(monitor: *Monitor, master_size: i32, stack_size: i32, master_factor: *f32, stack_factor: *f32, master_rest: *i32, stack_rest: *i32) void {
99
    var count: u32 = 0;
100
    var current = client_mod.next_tiled(monitor.clients);
101
    while (current) |client| : (current = client_mod.next_tiled(client.next)) {
102
        count += 1;
103
    }
104
105
    const nmaster_count: u32 = @intCast(@max(0, monitor.nmaster));
106
    const master_facts: f32 = @floatFromInt(@min(count, nmaster_count));
107
    const stack_facts: f32 = @floatFromInt(if (count > nmaster_count) count - nmaster_count else 0);
108
109
    var master_total: i32 = 0;
110
    var stack_total: i32 = 0;
111
112
    if (master_facts > 0) {
113
        master_total = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_size)) / master_facts)) * @as(i32, @intFromFloat(master_facts));
114
    }
115
    if (stack_facts > 0) {
116
        stack_total = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_size)) / stack_facts)) * @as(i32, @intFromFloat(stack_facts));
117
    }
118
119
    master_factor.* = master_facts;
120
    stack_factor.* = stack_facts;
121
    master_rest.* = master_size - master_total;
122
    stack_rest.* = stack_size - stack_total;
123
}
124
125
fn get_client_width(client: *Client) i32 {
126
    return client.width + 2 * client.border_width;
127
}
128
129
fn get_client_height(client: *Client) i32 {
130
    return client.height + 2 * client.border_width;
131
}
132
133
pub fn apply_size_hints(client: *Client, target_x: *i32, target_y: *i32, target_width: *i32, target_height: *i32, interact: bool) bool {
134
    const monitor = client.monitor orelse return false;
135
136
    target_width.* = @max(1, target_width.*);
137
    target_height.* = @max(1, target_height.*);
138
139
    if (interact) {
140
        if (target_x.* > screen_width) {
141
            target_x.* = screen_width - get_client_width(client);
142
        }
143
        if (target_y.* > screen_height) {
144
            target_y.* = screen_height - get_client_height(client);
145
        }
146
        if (target_x.* + target_width.* + 2 * client.border_width < 0) {
147
            target_x.* = 0;
148
        }
149
        if (target_y.* + target_height.* + 2 * client.border_width < 0) {
150
            target_y.* = 0;
151
        }
152
    } else {
153
        if (target_x.* >= monitor.win_x + monitor.win_w) {
154
            target_x.* = monitor.win_x + monitor.win_w - get_client_width(client);
155
        }
156
        if (target_y.* >= monitor.win_y + monitor.win_h) {
157
            target_y.* = monitor.win_y + monitor.win_h - get_client_height(client);
158
        }
159
        if (target_x.* + target_width.* + 2 * client.border_width <= monitor.win_x) {
160
            target_x.* = monitor.win_x;
161
        }
162
        if (target_y.* + target_height.* + 2 * client.border_width <= monitor.win_y) {
163
            target_y.* = monitor.win_y;
164
        }
165
    }
166
167
    if (target_height.* < bar_height) {
168
        target_height.* = bar_height;
169
    }
170
    if (target_width.* < bar_height) {
171
        target_width.* = bar_height;
172
    }
173
174
    if (client.is_floating or monitor.lt[monitor.sel_lt] == null) {
175
        const base_is_min = client.base_width == client.min_width and client.base_height == client.min_height;
176
177
        var adjusted_width = target_width.*;
178
        var adjusted_height = target_height.*;
179
180
        if (!base_is_min) {
181
            adjusted_width -= client.base_width;
182
            adjusted_height -= client.base_height;
183
        }
184
185
        if (client.min_aspect > 0 and client.max_aspect > 0) {
186
            const width_float: f32 = @floatFromInt(adjusted_width);
187
            const height_float: f32 = @floatFromInt(adjusted_height);
188
            if (client.max_aspect < width_float / height_float) {
189
                adjusted_width = @intFromFloat(height_float * client.max_aspect + 0.5);
190
            } else if (client.min_aspect < height_float / width_float) {
191
                adjusted_height = @intFromFloat(width_float * client.min_aspect + 0.5);
192
            }
193
        }
194
195
        if (base_is_min) {
196
            adjusted_width -= client.base_width;
197
            adjusted_height -= client.base_height;
198
        }
199
200
        if (client.increment_width > 0) {
201
            adjusted_width -= @mod(adjusted_width, client.increment_width);
202
        }
203
        if (client.increment_height > 0) {
204
            adjusted_height -= @mod(adjusted_height, client.increment_height);
205
        }
206
207
        target_width.* = @max(adjusted_width + client.base_width, client.min_width);
208
        target_height.* = @max(adjusted_height + client.base_height, client.min_height);
209
210
        if (client.max_width > 0) {
211
            target_width.* = @min(target_width.*, client.max_width);
212
        }
213
        if (client.max_height > 0) {
214
            target_height.* = @min(target_height.*, client.max_height);
215
        }
216
    }
217
218
    return target_x.* != client.x or target_y.* != client.y or target_width.* != client.width or target_height.* != client.height;
219
}
220
221
pub fn resize(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32, interact: bool) void {
222
    var final_x = target_x;
223
    var final_y = target_y;
224
    var final_width = target_width;
225
    var final_height = target_height;
226
227
    if (apply_size_hints(client, &final_x, &final_y, &final_width, &final_height, interact)) {
228
        resize_client(client, final_x, final_y, final_width, final_height);
229
    }
230
}
231
232
pub fn resize_client(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32) void {
233
    client.old_x = client.x;
234
    client.old_y = client.y;
235
    client.old_width = client.width;
236
    client.old_height = client.height;
237
    client.x = target_x;
238
    client.y = target_y;
239
    client.width = target_width;
240
    client.height = target_height;
241
242
    const display = display_handle orelse return;
243
244
    var window_changes: xlib.c.XWindowChanges = undefined;
245
    window_changes.x = target_x;
246
    window_changes.y = target_y;
247
    window_changes.width = @intCast(@max(1, target_width));
248
    window_changes.height = @intCast(@max(1, target_height));
249
    window_changes.border_width = client.border_width;
250
251
    _ = xlib.c.XConfigureWindow(
252
        display,
253
        client.window,
254
        xlib.c.CWX | xlib.c.CWY | xlib.c.CWWidth | xlib.c.CWHeight | xlib.c.CWBorderWidth,
255
        &window_changes,
256
    );
257
258
    send_configure(client);
259
    _ = xlib.XSync(display, xlib.False);
260
}
261
262
pub fn send_configure(client: *Client) void {
263
    const display = display_handle orelse return;
264
265
    var configure_event: xlib.c.XConfigureEvent = undefined;
266
    configure_event.type = xlib.c.ConfigureNotify;
267
    configure_event.display = display;
268
    configure_event.event = client.window;
269
    configure_event.window = client.window;
270
    configure_event.x = client.x;
271
    configure_event.y = client.y;
272
    configure_event.width = client.width;
273
    configure_event.height = client.height;
274
    configure_event.border_width = client.border_width;
275
    configure_event.above = xlib.None;
276
    configure_event.override_redirect = xlib.False;
277
278
    _ = xlib.c.XSendEvent(display, client.window, xlib.False, xlib.StructureNotifyMask, @ptrCast(&configure_event));
279
}