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