tony / oxwm

Branch: 15a6a02
Switch branches/tags
master
Iziram feature : click handler on bar blocks 15a6a02 2026-03-07
274 lines · 8.6 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 };
39
40 pub const KeyPress = struct {
41 mod_mask: u32 = 0,
42 keysym: u64 = 0,
43 };
44
45 pub const Keybind = struct {
46 keys: [4]KeyPress = [_]KeyPress{.{}} ** 4,
47 key_count: u8 = 1,
48 action: Action,
49 int_arg: i32 = 0,
50 str_arg: ?[]const u8 = null,
51 };
52
53 pub const Rule = struct {
54 class: ?[]const u8,
55 instance: ?[]const u8,
56 title: ?[]const u8,
57 tags: u32,
58 is_floating: bool,
59 monitor: i32,
60 focus: bool,
61 };
62
63 pub const BlockType = enum {
64 static,
65 datetime,
66 ram,
67 shell,
68 battery,
69 cpu_temp,
70 };
71
72 pub const ClickTarget = enum {
73 client_win,
74 root_win,
75 tag_bar,
76 };
77
78 pub const MouseAction = enum {
79 move_mouse,
80 resize_mouse,
81 toggle_floating,
82 };
83
84 pub const Layouts = enum(u32) {
85 tiling,
86 monocle,
87 floating,
88 scrolling,
89 grid,
90
91 pub fn fromString(name: []const u8) ?Layouts {
92 if (std.meta.stringToEnum(Layouts, name)) |v| return v;
93 if (std.mem.eql(u8, name, "tile")) return .tiling;
94 if (std.mem.eql(u8, name, "normie")) return .floating;
95 if (std.mem.eql(u8, name, "float")) return .floating;
96 if (std.mem.eql(u8, name, "scroll")) return .scrolling;
97 return null;
98 }
99 };
100
101 pub const MouseButton = struct {
102 click: ClickTarget,
103 mod_mask: u32,
104 button: u32,
105 action: MouseAction,
106 };
107
108 pub const ClickAction = struct {
109 command: []const u8,
110 floating: bool = false,
111 };
112
113 pub const Block = struct {
114 block_type: BlockType,
115 format: []const u8,
116 command: ?[]const u8 = null,
117 interval: u32,
118 color: u32,
119 underline: bool = true,
120 datetime_format: ?[]const u8 = null,
121 format_charging: ?[]const u8 = null,
122 format_discharging: ?[]const u8 = null,
123 format_full: ?[]const u8 = null,
124 battery_name: ?[]const u8 = null,
125 thermal_zone: ?[]const u8 = null,
126 click: ?ClickAction = null,
127 };
128
129 pub const ColorScheme = struct {
130 foreground: u32 = 0xbbbbbb,
131 background: u32 = 0x1a1b26,
132 border: u32 = 0x444444,
133 };
134
135 pub const Config = struct {
136 allocator: std.mem.Allocator,
137 string_arena: std.heap.ArenaAllocator,
138
139 terminal: []const u8 = "st",
140 font: []const u8 = "monospace:size=10",
141 bar_position: []const u8 = "top",
142 tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" },
143 layout: []const u8 = "tiling",
144 tag_layouts: [9]?[]const u8 = .{null} ** 9,
145
146 border_width: i32 = 2,
147 border_focused: u32 = 0x6dade3,
148 border_unfocused: u32 = 0x444444,
149
150 gaps_enabled: bool = true,
151 smartgaps_enabled: bool = false,
152 gap_inner_h: i32 = 5,
153 gap_inner_v: i32 = 5,
154 gap_outer_h: i32 = 5,
155 gap_outer_v: i32 = 5,
156
157 modkey: u32 = (1 << 6),
158 auto_tile: bool = false,
159 tag_back_and_forth: bool = false,
160 hide_vacant_tags: bool = false,
161
162 layout_tile_symbol: []const u8 = "[]=",
163 layout_monocle_symbol: []const u8 = "[M]",
164 layout_floating_symbol: []const u8 = "><>",
165 layout_scrolling_symbol: []const u8 = "[S]",
166 layout_grid_symbol: []const u8 = "[#]",
167
168 scheme_normal: ColorScheme = .{ .foreground = 0xbbbbbb, .background = 0x1a1b26, .border = 0x444444 },
169 scheme_selected: ColorScheme = .{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0xad8ee6 },
170 scheme_occupied: ColorScheme = .{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0x0db9d7 },
171 scheme_urgent: ColorScheme = .{ .foreground = 0xf7768e, .background = 0x1a1b26, .border = 0xf7768e },
172
173 keybinds: std.ArrayListUnmanaged(Keybind) = .{},
174 rules: std.ArrayListUnmanaged(Rule) = .{},
175 blocks: std.ArrayListUnmanaged(Block) = .{},
176 buttons: std.ArrayListUnmanaged(MouseButton) = .{},
177 autostart: std.ArrayListUnmanaged([]const u8) = .{},
178
179 pub fn init(allocator: std.mem.Allocator) Config {
180 return Config{
181 .allocator = allocator,
182 .string_arena = std.heap.ArenaAllocator.init(allocator),
183 };
184 }
185
186 pub fn deinit(self: *Config) void {
187 self.string_arena.deinit();
188 self.keybinds.deinit(self.allocator);
189 self.rules.deinit(self.allocator);
190 self.blocks.deinit(self.allocator);
191 self.buttons.deinit(self.allocator);
192 self.autostart.deinit(self.allocator);
193 }
194
195 pub fn addKeybind(self: *Config, keybind: Keybind) !void {
196 try self.keybinds.append(self.allocator, keybind);
197 }
198
199 pub fn addRule(self: *Config, rule: Rule) !void {
200 try self.rules.append(self.allocator, rule);
201 }
202
203 pub fn addBlock(self: *Config, block: Block) !void {
204 try self.blocks.append(self.allocator, block);
205 }
206
207 pub fn addButton(self: *Config, button: MouseButton) !void {
208 try self.buttons.append(self.allocator, button);
209 }
210
211 pub fn addAutostart(self: *Config, cmd: []const u8) !void {
212 try self.autostart.append(self.allocator, cmd);
213 }
214 };
215
216 pub fn initializeDefaultConfig(cfg: *Config) void {
217 const mod_key: u32 = 1 << 6;
218 const shift_key: u32 = 1 << 0;
219 const control_key: u32 = 1 << 2;
220
221 cfg.addKeybind(makeKeybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
222 cfg.addKeybind(makeKeybindStr(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
223 cfg.addKeybind(makeKeybindStr(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
224 cfg.addKeybind(makeKeybind(mod_key, 'q', .kill_client)) catch {};
225 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'q', .quit)) catch {};
226 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'r', .reload_config)) catch {};
227 cfg.addKeybind(makeKeybind(mod_key, 'j', .focus_next)) catch {};
228 cfg.addKeybind(makeKeybind(mod_key, 'k', .focus_prev)) catch {};
229 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'j', .move_next)) catch {};
230 cfg.addKeybind(makeKeybind(mod_key | shift_key, 'k', .move_prev)) catch {};
231 cfg.addKeybind(makeKeybindInt(mod_key, 'h', .resize_master, -50)) catch {};
232 cfg.addKeybind(makeKeybindInt(mod_key, 'l', .resize_master, 50)) catch {};
233 cfg.addKeybind(makeKeybind(mod_key, 'i', .inc_master)) catch {};
234 cfg.addKeybind(makeKeybind(mod_key, 'p', .dec_master)) catch {};
235 cfg.addKeybind(makeKeybind(mod_key, 'a', .toggle_gaps)) catch {};
236 cfg.addKeybind(makeKeybind(mod_key, 'f', .toggle_fullscreen)) catch {};
237 cfg.addKeybind(makeKeybind(mod_key, 0x0020, .toggle_floating)) catch {};
238 cfg.addKeybind(makeKeybind(mod_key, 'n', .cycle_layout)) catch {};
239 cfg.addKeybind(makeKeybindInt(mod_key, 0x002c, .focus_monitor, -1)) catch {};
240 cfg.addKeybind(makeKeybindInt(mod_key, 0x002e, .focus_monitor, 1)) catch {};
241 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
242 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
243
244 var tag_index: i32 = 0;
245 while (tag_index < 9) : (tag_index += 1) {
246 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
247 cfg.addKeybind(makeKeybindInt(mod_key, keysym, .view_tag, tag_index)) catch {};
248 cfg.addKeybind(makeKeybindInt(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
249 cfg.addKeybind(makeKeybindInt(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
250 cfg.addKeybind(makeKeybindInt(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
251 }
252
253 cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
254 cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
255 }
256
257 fn makeKeybind(mod: u32, key: u64, action: Action) Keybind {
258 var kb: Keybind = .{ .action = action };
259 kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
260 kb.key_count = 1;
261 return kb;
262 }
263
264 fn makeKeybindInt(mod: u32, key: u64, action: Action, int_arg: i32) Keybind {
265 var kb = makeKeybind(mod, key, action);
266 kb.int_arg = int_arg;
267 return kb;
268 }
269
270 fn makeKeybindStr(mod: u32, key: u64, action: Action, str_arg: []const u8) Keybind {
271 var kb = makeKeybind(mod, key, action);
272 kb.str_arg = str_arg;
273 return kb;
274 }