tony / oxwm

Branch: b419fcd
Switch branches/tags
master
tonybanters updated for scrolling with gestures, and size of windows in scrolling layout. b419fcd 2026-09-20
349 lines · 10.8 KB Raw Blame History
1 const std = @import("std");
2
3 pub const Action = enum {
4 spawn_terminal,
5 spawn,
6 kill_client,
7 quit,
8 reload_config,
9 restart,
10 show_keybinds,
11 focus_next,
12 focus_prev,
13 move_next,
14 move_prev,
15 resize_master,
16 inc_master,
17 dec_master,
18 toggle_floating,
19 toggle_fullscreen,
20 toggle_gaps,
21 toggle_bar,
22 cycle_layout,
23 set_layout,
24 set_layout_tiling,
25 set_layout_floating,
26 view_tag,
27 view_next_tag,
28 view_prev_tag,
29 view_next_nonempty_tag,
30 view_prev_nonempty_tag,
31 move_to_tag,
32 toggle_view_tag,
33 toggle_tag,
34 focus_monitor,
35 send_to_monitor,
36 scroll_left,
37 scroll_right,
38 cycle_width,
39 set_width,
40 };
41
42 pub const KeyPress = struct {
43 mod_mask: u32 = 0,
44 keysym: u64 = 0,
45 };
46
47 pub const Keybind = struct {
48 keys: [4]KeyPress = [_]KeyPress{.{}} ** 4,
49 key_count: u8 = 1,
50 action: Action,
51 int_arg: i32 = 0,
52 str_arg: ?[]const u8 = null,
53 };
54
55 pub const Rule = struct {
56 class: ?[]const u8,
57 instance: ?[]const u8,
58 title: ?[]const u8,
59 tags: u32,
60 is_floating: bool,
61 monitor: i32,
62 focus: bool,
63 width: f32 = 0,
64 };
65
66 pub const BlockType = enum {
67 static,
68 datetime,
69 ram,
70 shell,
71 battery,
72 cpu_temp,
73 systray,
74 };
75
76 pub const ClickTarget = enum {
77 client_win,
78 root_win,
79 tag_bar,
80 };
81
82 pub const MouseAction = enum {
83 move_mouse,
84 resize_mouse,
85 toggle_floating,
86 };
87
88 pub const Layouts = enum(u32) {
89 tiling,
90 monocle,
91 floating,
92 scrolling,
93 grid,
94 dwindle,
95
96 pub fn fromString(name: []const u8) ?Layouts {
97 if (std.meta.stringToEnum(Layouts, name)) |v| return v;
98 if (std.mem.eql(u8, name, "tile")) return .tiling;
99 if (std.mem.eql(u8, name, "normie")) return .floating;
100 if (std.mem.eql(u8, name, "float")) return .floating;
101 if (std.mem.eql(u8, name, "scroll")) return .scrolling;
102 if (std.mem.eql(u8, name, "fib")) return .dwindle;
103 if (std.mem.eql(u8, name, "fibonacci")) return .dwindle;
104 return null;
105 }
106 };
107
108 pub const AttachMethods = enum {
109 aside,
110 top,
111 bottom,
112 above,
113 below,
114
115 pub fn fromString(name: []const u8) ?AttachMethods {
116 return std.meta.stringToEnum(AttachMethods, name);
117 }
118 };
119
120 pub const MouseButton = struct {
121 click: ClickTarget,
122 mod_mask: u32,
123 button: u32,
124 action: MouseAction,
125 };
126
127 pub const FloatingPosition = enum {
128 top_left,
129 top_center,
130 top_right,
131 center_left,
132 center,
133 center_right,
134 bottom_left,
135 bottom_center,
136 bottom_right,
137
138 pub fn fromString(name: []const u8) ?FloatingPosition {
139 const map = .{
140 .{ "top-left", .top_left },
141 .{ "top_left", .top_left },
142 .{ "top-center", .top_center },
143 .{ "top_center", .top_center },
144 .{ "top-middle", .top_center },
145 .{ "top-right", .top_right },
146 .{ "top_right", .top_right },
147 .{ "center-left", .center_left },
148 .{ "center_left", .center_left },
149 .{ "center", .center },
150 .{ "center-right", .center_right },
151 .{ "center_right", .center_right },
152 .{ "bottom-left", .bottom_left },
153 .{ "bottom_left", .bottom_left },
154 .{ "bottom-center", .bottom_center },
155 .{ "bottom_center", .bottom_center },
156 .{ "bottom-middle", .bottom_center },
157 .{ "bottom-right", .bottom_right },
158 .{ "bottom_right", .bottom_right },
159 };
160 inline for (map) |entry| {
161 if (std.mem.eql(u8, name, entry[0])) return entry[1];
162 }
163 return null;
164 }
165 };
166
167 pub const ClickAction = struct {
168 command: []const u8,
169 floating: bool = false,
170 bypass_rules: bool = false,
171 };
172
173 pub const Block = struct {
174 block_type: BlockType,
175 format: []const u8,
176 command: ?[]const u8 = null,
177 interval: u32,
178 color: u32,
179 underline: bool = true,
180 datetime_format: ?[]const u8 = null,
181 format_charging: ?[]const u8 = null,
182 format_discharging: ?[]const u8 = null,
183 format_full: ?[]const u8 = null,
184 battery_name: ?[]const u8 = null,
185 thermal_zone: ?[]const u8 = null,
186 click: ?ClickAction = null,
187 };
188
189 pub const ColorScheme = struct {
190 foreground: u32 = 0xbbbbbb,
191 background: u32 = 0x1a1b26,
192 border: u32 = 0x444444,
193 };
194
195 pub const Config = struct {
196 allocator: std.mem.Allocator,
197 string_arena: std.heap.ArenaAllocator,
198
199 terminal: []const u8 = "st",
200 font: []const u8 = "monospace:size=10",
201 bar_position: []const u8 = "top",
202 tags: [12][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" },
203 tag_count: u32 = 9,
204 layout: []const u8 = "tiling",
205 attach_method: AttachMethods = .aside,
206 tag_layouts: [12]?[]const u8 = .{null} ** 12,
207
208 border_width: i32 = 2,
209 border_focused: u32 = 0x6dade3,
210 border_unfocused: u32 = 0x444444,
211
212 gaps_enabled: bool = true,
213 smartgaps_enabled: bool = false,
214 gap_inner_h: i32 = 5,
215 gap_inner_v: i32 = 5,
216 gap_outer_h: i32 = 5,
217 gap_outer_v: i32 = 5,
218
219 modkey: u32 = (1 << 6),
220 auto_tile: bool = false,
221 tag_back_and_forth: bool = false,
222 hide_vacant_tags: bool = false,
223 floating_position: FloatingPosition = .center,
224 tiled_resize_mode: bool = false,
225 warp_cursor_to_monitor: bool = false,
226 warp_cursor_on_send: bool = false,
227
228 scroll_default_width: f32 = 0.5,
229 scroll_width_presets: [8]f32 = .{ 1.0 / 3.0, 0.5, 2.0 / 3.0, 0, 0, 0, 0, 0 },
230 scroll_width_preset_count: u32 = 3,
231 gesture_enabled: bool = true,
232 gesture_fingers: u32 = 3,
233 gesture_speed: f32 = 2.0,
234 gesture_natural: bool = false,
235
236 layout_tile_symbol: []const u8 = "[]=",
237 layout_monocle_symbol: []const u8 = "[M]",
238 layout_floating_symbol: []const u8 = "><>",
239 layout_scrolling_symbol: []const u8 = "[S]",
240 layout_grid_symbol: []const u8 = "[#]",
241 layout_dwindle_symbol: []const u8 = "[\\]",
242
243 scheme_normal: ColorScheme = .{ .foreground = 0xbbbbbb, .background = 0x1a1b26, .border = 0x444444 },
244 scheme_selected: ColorScheme = .{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0xad8ee6 },
245 scheme_occupied: ColorScheme = .{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0x0db9d7 },
246 scheme_urgent: ColorScheme = .{ .foreground = 0xf7768e, .background = 0x1a1b26, .border = 0xf7768e },
247
248 keybinds: std.ArrayListUnmanaged(Keybind) = .empty,
249 rules: std.ArrayListUnmanaged(Rule) = .empty,
250 blocks: std.ArrayListUnmanaged(Block) = .empty,
251 buttons: std.ArrayListUnmanaged(MouseButton) = .empty,
252 autostart: std.ArrayListUnmanaged([]const u8) = .empty,
253
254 pub fn init(allocator: std.mem.Allocator) Config {
255 return Config{
256 .allocator = allocator,
257 .string_arena = std.heap.ArenaAllocator.init(allocator),
258 };
259 }
260
261 pub fn deinit(self: *Config) void {
262 self.string_arena.deinit();
263 self.keybinds.deinit(self.allocator);
264 self.rules.deinit(self.allocator);
265 self.blocks.deinit(self.allocator);
266 self.buttons.deinit(self.allocator);
267 self.autostart.deinit(self.allocator);
268 }
269
270 pub fn addKeybind(self: *Config, keybind: Keybind) !void {
271 try self.keybinds.append(self.allocator, keybind);
272 }
273
274 pub fn addRule(self: *Config, rule: Rule) !void {
275 try self.rules.append(self.allocator, rule);
276 }
277
278 pub fn addBlock(self: *Config, block: Block) !void {
279 try self.blocks.append(self.allocator, block);
280 }
281
282 pub fn addButton(self: *Config, button: MouseButton) !void {
283 try self.buttons.append(self.allocator, button);
284 }
285
286 pub fn addAutostart(self: *Config, cmd: []const u8) !void {
287 try self.autostart.append(self.allocator, cmd);
288 }
289 };
290
291 pub fn initializeDefaultConfig(cfg: *Config) void {
292 const mod_key: u32 = 1 << 6;
293 const shift_key: u32 = 1 << 0;
294 const control_key: u32 = 1 << 2;
295
296 cfg.addKeybind(makeKeybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
297 cfg.addKeybind(makeKeybindStr(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
298 cfg.addKeybind(makeKeybindStr(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
299 cfg.addKeybind(makeKeybind(mod_key, 'q', .kill_client)) catch {};
300 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'q', .quit)) catch {};
301 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'r', .reload_config)) catch {};
302 cfg.addKeybind(makeKeybind(mod_key, 'j', .focus_next)) catch {};
303 cfg.addKeybind(makeKeybind(mod_key, 'k', .focus_prev)) catch {};
304 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'j', .move_next)) catch {};
305 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'k', .move_prev)) catch {};
306 cfg.addKeybind(makeKeybindInt(mod_key, 'h', .resize_master, -50)) catch {};
307 cfg.addKeybind(makeKeybindInt(mod_key, 'l', .resize_master, 50)) catch {};
308 cfg.addKeybind(makeKeybind(mod_key, 'i', .inc_master)) catch {};
309 cfg.addKeybind(makeKeybind(mod_key, 'p', .dec_master)) catch {};
310 cfg.addKeybind(makeKeybind(mod_key, 'a', .toggle_gaps)) catch {};
311 cfg.addKeybind(makeKeybind(mod_key, 'f', .toggle_fullscreen)) catch {};
312 cfg.addKeybind(makeKeybind(mod_key, 0x0020, .toggle_floating)) catch {};
313 cfg.addKeybind(makeKeybind(mod_key, 'n', .cycle_layout)) catch {};
314 cfg.addKeybind(makeKeybindInt(mod_key, 0x002c, .focus_monitor, -1)) catch {};
315 cfg.addKeybind(makeKeybindInt(mod_key, 0x002e, .focus_monitor, 1)) catch {};
316 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
317 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
318
319 var tag_index: i32 = 0;
320 while (tag_index < 9) : (tag_index += 1) {
321 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
322 cfg.addKeybind(makeKeybindInt(mod_key, keysym, .view_tag, tag_index)) catch {};
323 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
324 cfg.addKeybind(makeKeybindInt(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
325 cfg.addKeybind(makeKeybindInt(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
326 }
327
328 cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
329 cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
330 }
331
332 fn makeKeybind(mod: u32, key: u64, action: Action) Keybind {
333 var kb: Keybind = .{ .action = action };
334 kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
335 kb.key_count = 1;
336 return kb;
337 }
338
339 fn makeKeybindInt(mod: u32, key: u64, action: Action, int_arg: i32) Keybind {
340 var kb = makeKeybind(mod, key, action);
341 kb.int_arg = int_arg;
342 return kb;
343 }
344
345 fn makeKeybindStr(mod: u32, key: u64, action: Action, str_arg: []const u8) Keybind {
346 var kb = makeKeybind(mod, key, action);
347 kb.str_arg = str_arg;
348 return kb;
349 }