Merge pull request #35 from tonybanters/lua-config-test
Browse files
Lua Config Conversion
+154 −1 Cargo.lock
| @@ -38,6 +38,16 @@ dependencies = [ | ||
| 38 | 38 | "serde_core", |
| 39 | 39 | ] |
| 40 | 40 | |
| 41 | +[[package]] | |
| 42 | +name = "bstr" | |
| 43 | +version = "1.12.1" | |
| 44 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 45 | +checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab" | |
| 46 | +dependencies = [ | |
| 47 | + "memchr", | |
| 48 | + "serde", | |
| 49 | +] | |
| 50 | + | |
| 41 | 51 | [[package]] |
| 42 | 52 | name = "bumpalo" |
| 43 | 53 | version = "3.19.0" |
| @@ -100,6 +110,18 @@ dependencies = [ | ||
| 100 | 110 | "windows-sys 0.48.0", |
| 101 | 111 | ] |
| 102 | 112 | |
| 113 | +[[package]] | |
| 114 | +name = "either" | |
| 115 | +version = "1.15.0" | |
| 116 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 117 | +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" | |
| 118 | + | |
| 119 | +[[package]] | |
| 120 | +name = "env_home" | |
| 121 | +version = "0.1.0" | |
| 122 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 123 | +checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" | |
| 124 | + | |
| 103 | 125 | [[package]] |
| 104 | 126 | name = "errno" |
| 105 | 127 | version = "0.3.14" |
| @@ -193,12 +215,74 @@ version = "0.11.0" | ||
| 193 | 215 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 194 | 216 | checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" |
| 195 | 217 | |
| 218 | +[[package]] | |
| 219 | +name = "lock_api" | |
| 220 | +version = "0.4.14" | |
| 221 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 222 | +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" | |
| 223 | +dependencies = [ | |
| 224 | + "scopeguard", | |
| 225 | +] | |
| 226 | + | |
| 196 | 227 | [[package]] |
| 197 | 228 | name = "log" |
| 198 | 229 | version = "0.4.28" |
| 199 | 230 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 200 | 231 | checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" |
| 201 | 232 | |
| 233 | +[[package]] | |
| 234 | +name = "lua-src" | |
| 235 | +version = "547.0.0" | |
| 236 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 237 | +checksum = "1edaf29e3517b49b8b746701e5648ccb5785cde1c119062cbabbc5d5cd115e42" | |
| 238 | +dependencies = [ | |
| 239 | + "cc", | |
| 240 | +] | |
| 241 | + | |
| 242 | +[[package]] | |
| 243 | +name = "luajit-src" | |
| 244 | +version = "210.5.12+a4f56a4" | |
| 245 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 246 | +checksum = "b3a8e7962a5368d5f264d045a5a255e90f9aa3fc1941ae15a8d2940d42cac671" | |
| 247 | +dependencies = [ | |
| 248 | + "cc", | |
| 249 | + "which", | |
| 250 | +] | |
| 251 | + | |
| 252 | +[[package]] | |
| 253 | +name = "memchr" | |
| 254 | +version = "2.7.6" | |
| 255 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 256 | +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" | |
| 257 | + | |
| 258 | +[[package]] | |
| 259 | +name = "mlua" | |
| 260 | +version = "0.10.5" | |
| 261 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 262 | +checksum = "c1f5f8fbebc7db5f671671134b9321c4b9aa9adeafccfd9a8c020ae45c6a35d0" | |
| 263 | +dependencies = [ | |
| 264 | + "bstr", | |
| 265 | + "either", | |
| 266 | + "mlua-sys", | |
| 267 | + "num-traits", | |
| 268 | + "parking_lot", | |
| 269 | + "rustc-hash", | |
| 270 | + "rustversion", | |
| 271 | +] | |
| 272 | + | |
| 273 | +[[package]] | |
| 274 | +name = "mlua-sys" | |
| 275 | +version = "0.6.8" | |
| 276 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 277 | +checksum = "380c1f7e2099cafcf40e51d3a9f20a346977587aa4d012eae1f043149a728a93" | |
| 278 | +dependencies = [ | |
| 279 | + "cc", | |
| 280 | + "cfg-if", | |
| 281 | + "lua-src", | |
| 282 | + "luajit-src", | |
| 283 | + "pkg-config", | |
| 284 | +] | |
| 285 | + | |
| 202 | 286 | [[package]] |
| 203 | 287 | name = "num-traits" |
| 204 | 288 | version = "0.2.19" |
| @@ -222,17 +306,41 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" | ||
| 222 | 306 | |
| 223 | 307 | [[package]] |
| 224 | 308 | name = "oxwm" |
| 225 | -version = "0.5.0" | |
| 309 | +version = "0.6.0" | |
| 226 | 310 | dependencies = [ |
| 227 | 311 | "anyhow", |
| 228 | 312 | "chrono", |
| 229 | 313 | "dirs", |
| 314 | + "mlua", | |
| 230 | 315 | "ron", |
| 231 | 316 | "serde", |
| 232 | 317 | "x11", |
| 233 | 318 | "x11rb", |
| 234 | 319 | ] |
| 235 | 320 | |
| 321 | +[[package]] | |
| 322 | +name = "parking_lot" | |
| 323 | +version = "0.12.5" | |
| 324 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 325 | +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" | |
| 326 | +dependencies = [ | |
| 327 | + "lock_api", | |
| 328 | + "parking_lot_core", | |
| 329 | +] | |
| 330 | + | |
| 331 | +[[package]] | |
| 332 | +name = "parking_lot_core" | |
| 333 | +version = "0.9.12" | |
| 334 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 335 | +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" | |
| 336 | +dependencies = [ | |
| 337 | + "cfg-if", | |
| 338 | + "libc", | |
| 339 | + "redox_syscall", | |
| 340 | + "smallvec", | |
| 341 | + "windows-link", | |
| 342 | +] | |
| 343 | + | |
| 236 | 344 | [[package]] |
| 237 | 345 | name = "pkg-config" |
| 238 | 346 | version = "0.3.32" |
| @@ -257,6 +365,15 @@ dependencies = [ | ||
| 257 | 365 | "proc-macro2", |
| 258 | 366 | ] |
| 259 | 367 | |
| 368 | +[[package]] | |
| 369 | +name = "redox_syscall" | |
| 370 | +version = "0.5.18" | |
| 371 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 372 | +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" | |
| 373 | +dependencies = [ | |
| 374 | + "bitflags", | |
| 375 | +] | |
| 376 | + | |
| 260 | 377 | [[package]] |
| 261 | 378 | name = "redox_users" |
| 262 | 379 | version = "0.4.6" |
| @@ -280,6 +397,12 @@ dependencies = [ | ||
| 280 | 397 | "serde_derive", |
| 281 | 398 | ] |
| 282 | 399 | |
| 400 | +[[package]] | |
| 401 | +name = "rustc-hash" | |
| 402 | +version = "2.1.1" | |
| 403 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 404 | +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" | |
| 405 | + | |
| 283 | 406 | [[package]] |
| 284 | 407 | name = "rustix" |
| 285 | 408 | version = "1.1.2" |
| @@ -299,6 +422,12 @@ version = "1.0.22" | ||
| 299 | 422 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 300 | 423 | checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" |
| 301 | 424 | |
| 425 | +[[package]] | |
| 426 | +name = "scopeguard" | |
| 427 | +version = "1.2.0" | |
| 428 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 429 | +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" | |
| 430 | + | |
| 302 | 431 | [[package]] |
| 303 | 432 | name = "serde" |
| 304 | 433 | version = "1.0.228" |
| @@ -335,6 +464,12 @@ version = "1.3.0" | ||
| 335 | 464 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 336 | 465 | checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" |
| 337 | 466 | |
| 467 | +[[package]] | |
| 468 | +name = "smallvec" | |
| 469 | +version = "1.15.1" | |
| 470 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 471 | +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" | |
| 472 | + | |
| 338 | 473 | [[package]] |
| 339 | 474 | name = "syn" |
| 340 | 475 | version = "2.0.107" |
| @@ -437,6 +572,18 @@ dependencies = [ | ||
| 437 | 572 | "unicode-ident", |
| 438 | 573 | ] |
| 439 | 574 | |
| 575 | +[[package]] | |
| 576 | +name = "which" | |
| 577 | +version = "7.0.3" | |
| 578 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 579 | +checksum = "24d643ce3fd3e5b54854602a080f34fb10ab75e0b813ee32d00ca2b44fa74762" | |
| 580 | +dependencies = [ | |
| 581 | + "either", | |
| 582 | + "env_home", | |
| 583 | + "rustix", | |
| 584 | + "winsafe", | |
| 585 | +] | |
| 586 | + | |
| 440 | 587 | [[package]] |
| 441 | 588 | name = "windows-core" |
| 442 | 589 | version = "0.62.2" |
| @@ -571,6 +718,12 @@ version = "0.48.5" | ||
| 571 | 718 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 572 | 719 | checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" |
| 573 | 720 | |
| 721 | +[[package]] | |
| 722 | +name = "winsafe" | |
| 723 | +version = "0.0.19" | |
| 724 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 725 | +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" | |
| 726 | + | |
| 574 | 727 | [[package]] |
| 575 | 728 | name = "x11" |
| 576 | 729 | version = "2.21.0" |
+2 −1 Cargo.toml
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | [package] |
| 2 | 2 | name = "oxwm" |
| 3 | -version = "0.5.0" | |
| 3 | +version = "0.6.0" | |
| 4 | 4 | edition = "2024" |
| 5 | 5 | |
| 6 | 6 | [lib] |
| @@ -19,3 +19,4 @@ chrono = "0.4" | ||
| 19 | 19 | dirs = "5.0" |
| 20 | 20 | serde = { version = "1.0", features = ["derive"] } |
| 21 | 21 | ron = "0.8" |
| 22 | +mlua = { version = "0.10", features = ["lua54", "vendored"] } |
+5 −5 justfile
| @@ -10,7 +10,7 @@ install: build | ||
| 10 | 10 | uninstall: |
| 11 | 11 | rm -f /usr/bin/oxwm |
| 12 | 12 | @echo "✓ oxwm uninstalled" |
| 13 | - @echo " Your config at ~/.config/oxwm/config.ron is preserved" | |
| 13 | + @echo " Your config at ~/.config/oxwm/ is preserved" | |
| 14 | 14 | |
| 15 | 15 | clean: |
| 16 | 16 | cargo clean |
| @@ -19,23 +19,23 @@ test-clean: | ||
| 19 | 19 | pkill Xephyr || true |
| 20 | 20 | rm -rf ~/.config/oxwm |
| 21 | 21 | Xephyr -screen 1280x800 :1 & sleep 1 |
| 22 | - DISPLAY=:1 cargo run --release -- --config resources/test-config.ron | |
| 22 | + DISPLAY=:1 cargo run --release -- --config resources/test-config.lua | |
| 23 | 23 | |
| 24 | 24 | test: |
| 25 | 25 | pkill Xephyr || true |
| 26 | 26 | Xephyr -screen 1280x800 :1 & sleep 1 |
| 27 | - DISPLAY=:1 cargo run --release -- --config resources/test-config.ron | |
| 27 | + DISPLAY=:1 cargo run --release -- --config resources/test-config.lua | |
| 28 | 28 | |
| 29 | 29 | test-multimon: |
| 30 | 30 | pkill Xephyr || true |
| 31 | 31 | Xephyr +xinerama -screen 640x480 -screen 640x480 :1 & sleep 1 |
| 32 | - DISPLAY=:1 cargo run --release -- --config resources/test-config.ron | |
| 32 | + DISPLAY=:1 cargo run --release -- --config resources/test-config.lua | |
| 33 | 33 | |
| 34 | 34 | init: |
| 35 | 35 | cargo run --release -- --init |
| 36 | 36 | |
| 37 | 37 | edit: |
| 38 | - $EDITOR ~/.config/oxwm/config.ron | |
| 38 | + $EDITOR ~/.config/oxwm/config.lua | |
| 39 | 39 | |
| 40 | 40 | check: |
| 41 | 41 | cargo clippy -- -W clippy::all |
+17 −6 readme.org
| @@ -79,7 +79,7 @@ Add this to your =configuration.nix=: | ||
| 79 | 79 | *** Initialize your config |
| 80 | 80 | After rebuilding your system with =sudo nixos-rebuild switch=, log in via your display manager. |
| 81 | 81 | |
| 82 | -On first launch, your initial config file will be automatically created and placed in =~/.config/oxwm/config.ron=. Edit it and reload with =Mod+Shift+R=. | |
| 82 | +On first launch, your initial config file will be automatically created and placed in =~/.config/oxwm/config.lua=. Edit it and reload with =Mod+Shift+R=. | |
| 83 | 83 | |
| 84 | 84 | *** Advanced: Using a specific oxwm version |
| 85 | 85 | If you want to pin or customize the oxwm package: |
| @@ -154,22 +154,33 @@ startx | ||
| 154 | 154 | If using a display manager (LightDM, GDM, SDDM), OXWM should appear in the session list after installation. |
| 155 | 155 | |
| 156 | 156 | * Configuration |
| 157 | -OXWM was inspired by dwm, but ditched the suckless philosophy. This philosophy quite literally discourages users from using the software for the sake of 'elitism'. I find that quite nonsensical, so I went ahead and created this project to be user friendly. The configuration is done by editing =~/.config/oxwm/config.ron= and the binary can be reloaded with a hotkey (Super+Shift+R by default). | |
| 157 | +OXWM was inspired by dwm, but ditched the suckless philosophy. This philosophy quite literally discourages users from using the software for the sake of 'elitism'. I find that quite nonsensical, so I went ahead and created this project to be user friendly. The configuration is done by editing =~/.config/oxwm/config.lua= and the binary can be reloaded with a hotkey (Super+Shift+R by default). | |
| 158 | 158 | |
| 159 | -Edit =~/.config/oxwm/config.ron= to customize: | |
| 159 | +Edit =~/.config/oxwm/config.lua= to customize: | |
| 160 | 160 | - Keybindings |
| 161 | 161 | - Colors and appearance |
| 162 | 162 | - Status bar blocks |
| 163 | 163 | - Gaps and borders |
| 164 | 164 | - Terminal and applications |
| 165 | 165 | |
| 166 | -After making changes, reload OXWM with =Mod+Shift+R= | |
| 166 | +After making changes, reload OXWM with =Mod+Shift+R= | |
| 167 | + | |
| 168 | +** Migrating from RON | |
| 169 | +If you have an old RON config (=config.ron=), use the migration tool to automatically convert it: | |
| 170 | + | |
| 171 | +#+begin_src sh | |
| 172 | +oxwm --migrate | |
| 173 | +# Or specify a path: | |
| 174 | +oxwm --migrate ~/.config/oxwm/config.ron | |
| 175 | +#+end_src | |
| 176 | + | |
| 177 | +This will convert your =config.ron= to =config.lua=, preserving all your settings. Your old config will remain intact for backup. | |
| 167 | 178 | |
| 168 | 179 | * Contributing |
| 169 | 180 | When contributing to OXWM: |
| 170 | 181 | |
| 171 | -1. Never commit your personal =~/.config/oxwm/config.ron= | |
| 172 | -2. Only modify =templates/config.ron= if adding new configuration options | |
| 182 | +1. Never commit your personal =~/.config/oxwm/config.lua= | |
| 183 | +2. Only modify =templates/config.lua= if adding new configuration options | |
| 173 | 184 | 3. Test your changes with =just test= using Xephyr/Xwayland |
| 174 | 185 | 4. Document any new features or keybindings |
| 175 | 186 |
+180 −0 resources/test-config.lua new file mode 100644
| @@ -0,0 +1,180 @@ | ||
| 1 | +-- OXWM Configuration File (Lua) | |
| 2 | +-- Migrated from config.ron | |
| 3 | +-- Edit this file and reload with Mod+Shift+R (no compilation needed!) | |
| 4 | + | |
| 5 | +local terminal = "st" | |
| 6 | +local modkey = "Mod4" | |
| 7 | + | |
| 8 | +-- Color palette | |
| 9 | +local colors = { | |
| 10 | + lavender = "#a9b1d6", | |
| 11 | + light_blue = "#7aa2f7", | |
| 12 | + grey = "#bbbbbb", | |
| 13 | + purple = "#ad8ee6", | |
| 14 | + cyan = "#0db9d7", | |
| 15 | + bg = "#1a1b26", | |
| 16 | + green = "#9ece6a", | |
| 17 | + red = "#f7768e", | |
| 18 | + fg = "#bbbbbb", | |
| 19 | + blue = "#6dade3", | |
| 20 | +} | |
| 21 | + | |
| 22 | +-- Main configuration table | |
| 23 | +return { | |
| 24 | + -- Appearance | |
| 25 | + border_width = 2, | |
| 26 | + border_focused = colors.blue, | |
| 27 | + border_unfocused = colors.grey, | |
| 28 | + font = "JetBrainsMono Nerd Font:style=Bold:size=12", | |
| 29 | + | |
| 30 | + -- Window gaps | |
| 31 | + gaps_enabled = true, | |
| 32 | + gap_inner_horizontal = 5, | |
| 33 | + gap_inner_vertical = 5, | |
| 34 | + gap_outer_horizontal = 5, | |
| 35 | + gap_outer_vertical = 5, | |
| 36 | + | |
| 37 | + -- Basics | |
| 38 | + modkey = "Mod1", | |
| 39 | + terminal = "st", | |
| 40 | + | |
| 41 | + -- Workspace tags | |
| 42 | + tags = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }, | |
| 43 | + | |
| 44 | + -- Layout symbol overrides | |
| 45 | + layout_symbols = { | |
| 46 | + { name = "tiling", symbol = "[T]" }, | |
| 47 | + { name = "normie", symbol = "[F]" }, | |
| 48 | + }, | |
| 49 | + | |
| 50 | + -- Keybindings | |
| 51 | + keybindings = { | |
| 52 | + { | |
| 53 | + keys = { | |
| 54 | + { modifiers = { "Mod1" }, key = "Space" }, | |
| 55 | + { modifiers = { }, key = "T" }, | |
| 56 | + }, | |
| 57 | + action = "Spawn", | |
| 58 | + arg = "st" | |
| 59 | + }, | |
| 60 | + { modifiers = { "Mod1" }, key = "Return", action = "Spawn", arg = "st" }, | |
| 61 | + { modifiers = { "Mod1" }, key = "D", action = "Spawn", arg = { "sh", "-c", "dmenu_run -l 10" } }, | |
| 62 | + { modifiers = { "Mod1" }, key = "S", action = "Spawn", arg = { "sh", "-c", "maim -s | xclip -selection clipboard -t image/png" } }, | |
| 63 | + { modifiers = { "Mod1" }, key = "Q", action = "KillClient" }, | |
| 64 | + { modifiers = { "Mod1", "Shift" }, key = "Slash", action = "ShowKeybindOverlay" }, | |
| 65 | + { modifiers = { "Mod1", "Shift" }, key = "F", action = "ToggleFullScreen" }, | |
| 66 | + { modifiers = { "Mod1", "Shift" }, key = "Space", action = "ToggleFloating" }, | |
| 67 | + { modifiers = { "Mod1" }, key = "F", action = "ChangeLayout", arg = "normie" }, | |
| 68 | + { modifiers = { "Mod1" }, key = "C", action = "ChangeLayout", arg = "tiling" }, | |
| 69 | + { modifiers = { "Mod1" }, key = "N", action = "CycleLayout" }, | |
| 70 | + { modifiers = { "Mod1" }, key = "A", action = "ToggleGaps" }, | |
| 71 | + { modifiers = { "Mod1", "Shift" }, key = "Q", action = "Quit" }, | |
| 72 | + { modifiers = { "Mod1", "Shift" }, key = "R", action = "Restart" }, | |
| 73 | + { modifiers = { "Mod1" }, key = "H", action = "FocusDirection", arg = 2 }, | |
| 74 | + { modifiers = { "Mod1" }, key = "J", action = "FocusDirection", arg = 1 }, | |
| 75 | + { modifiers = { "Mod1" }, key = "K", action = "FocusDirection", arg = 0 }, | |
| 76 | + { modifiers = { "Mod1" }, key = "L", action = "FocusDirection", arg = 3 }, | |
| 77 | + { modifiers = { "Mod1", "Shift" }, key = "H", action = "SwapDirection", arg = 2 }, | |
| 78 | + { modifiers = { "Mod1", "Shift" }, key = "J", action = "SwapDirection", arg = 1 }, | |
| 79 | + { modifiers = { "Mod1", "Shift" }, key = "K", action = "SwapDirection", arg = 0 }, | |
| 80 | + { modifiers = { "Mod1", "Shift" }, key = "L", action = "SwapDirection", arg = 3 }, | |
| 81 | + { modifiers = { "Mod1" }, key = "1", action = "ViewTag", arg = 0 }, | |
| 82 | + { modifiers = { "Mod1" }, key = "2", action = "ViewTag", arg = 1 }, | |
| 83 | + { modifiers = { "Mod1" }, key = "3", action = "ViewTag", arg = 2 }, | |
| 84 | + { modifiers = { "Mod1" }, key = "4", action = "ViewTag", arg = 3 }, | |
| 85 | + { modifiers = { "Mod1" }, key = "5", action = "ViewTag", arg = 4 }, | |
| 86 | + { modifiers = { "Mod1" }, key = "6", action = "ViewTag", arg = 5 }, | |
| 87 | + { modifiers = { "Mod1" }, key = "7", action = "ViewTag", arg = 6 }, | |
| 88 | + { modifiers = { "Mod1" }, key = "8", action = "ViewTag", arg = 7 }, | |
| 89 | + { modifiers = { "Mod1" }, key = "9", action = "ViewTag", arg = 8 }, | |
| 90 | + { modifiers = { "Mod1", "Shift" }, key = "1", action = "MoveToTag", arg = 0 }, | |
| 91 | + { modifiers = { "Mod1", "Shift" }, key = "2", action = "MoveToTag", arg = 1 }, | |
| 92 | + { modifiers = { "Mod1", "Shift" }, key = "3", action = "MoveToTag", arg = 2 }, | |
| 93 | + { modifiers = { "Mod1", "Shift" }, key = "4", action = "MoveToTag", arg = 3 }, | |
| 94 | + { modifiers = { "Mod1", "Shift" }, key = "5", action = "MoveToTag", arg = 4 }, | |
| 95 | + { modifiers = { "Mod1", "Shift" }, key = "6", action = "MoveToTag", arg = 5 }, | |
| 96 | + { modifiers = { "Mod1", "Shift" }, key = "7", action = "MoveToTag", arg = 6 }, | |
| 97 | + { modifiers = { "Mod1", "Shift" }, key = "8", action = "MoveToTag", arg = 7 }, | |
| 98 | + { modifiers = { "Mod1", "Shift" }, key = "9", action = "MoveToTag", arg = 8 }, | |
| 99 | + }, | |
| 100 | + | |
| 101 | + -- Status bar blocks | |
| 102 | + status_blocks = { | |
| 103 | + { | |
| 104 | + format = "", | |
| 105 | + command = "Battery", | |
| 106 | + battery_formats = { | |
| 107 | + charging = " Bat: {}%", | |
| 108 | + discharging = " Bat:{}%", | |
| 109 | + full = " Bat: {}%" | |
| 110 | + }, | |
| 111 | + interval_secs = 30, | |
| 112 | + color = colors.green, | |
| 113 | + underline = true | |
| 114 | + }, | |
| 115 | + { | |
| 116 | + format = " │ ", | |
| 117 | + command = "Static", | |
| 118 | + interval_secs = 999999999, | |
| 119 | + color = colors.lavender, | |
| 120 | + underline = false | |
| 121 | + }, | |
| 122 | + { | |
| 123 | + format = " {used}/{total} GB", | |
| 124 | + command = "Ram", | |
| 125 | + interval_secs = 5, | |
| 126 | + color = colors.light_blue, | |
| 127 | + underline = true | |
| 128 | + }, | |
| 129 | + { | |
| 130 | + format = " │ ", | |
| 131 | + command = "Static", | |
| 132 | + interval_secs = 999999999, | |
| 133 | + color = colors.lavender, | |
| 134 | + underline = false | |
| 135 | + }, | |
| 136 | + { | |
| 137 | + format = " {}", | |
| 138 | + command = "Shell", | |
| 139 | + command_arg = "uname -r", | |
| 140 | + interval_secs = 999999999, | |
| 141 | + color = colors.red, | |
| 142 | + underline = true | |
| 143 | + }, | |
| 144 | + { | |
| 145 | + format = " │ ", | |
| 146 | + command = "Static", | |
| 147 | + interval_secs = 999999999, | |
| 148 | + color = colors.lavender, | |
| 149 | + underline = false | |
| 150 | + }, | |
| 151 | + { | |
| 152 | + format = " {}", | |
| 153 | + command = "DateTime", | |
| 154 | + command_arg = "%a, %b %d - %-I:%M %P", | |
| 155 | + interval_secs = 1, | |
| 156 | + color = colors.cyan, | |
| 157 | + underline = true | |
| 158 | + }, | |
| 159 | + }, | |
| 160 | + | |
| 161 | + -- Color schemes for bar | |
| 162 | + scheme_normal = { | |
| 163 | + foreground = colors.fg, | |
| 164 | + background = colors.bg, | |
| 165 | + underline = "#444444" | |
| 166 | + }, | |
| 167 | + scheme_occupied = { | |
| 168 | + foreground = colors.cyan, | |
| 169 | + background = colors.bg, | |
| 170 | + underline = colors.cyan | |
| 171 | + }, | |
| 172 | + scheme_selected = { | |
| 173 | + foreground = colors.cyan, | |
| 174 | + background = colors.bg, | |
| 175 | + underline = colors.purple | |
| 176 | + }, | |
| 177 | + | |
| 178 | + -- Autostart commands | |
| 179 | + autostart = {}, | |
| 180 | +} |
+5 −5 src/bar/blocks/battery.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use std::fs; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -30,22 +30,22 @@ impl Battery { | ||
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - fn read_file(&self, filename: &str) -> Result<String> { | |
| 33 | + fn read_file(&self, filename: &str) -> Result<String, BlockError> { | |
| 34 | 34 | let path = format!("{}/{}", self.battery_path, filename); |
| 35 | 35 | Ok(fs::read_to_string(path)?.trim().to_string()) |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - fn get_capacity(&self) -> Result<u32> { | |
| 38 | + fn get_capacity(&self) -> Result<u32, BlockError> { | |
| 39 | 39 | Ok(self.read_file("capacity")?.parse()?) |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - fn get_status(&self) -> Result<String> { | |
| 42 | + fn get_status(&self) -> Result<String, BlockError> { | |
| 43 | 43 | self.read_file("status") |
| 44 | 44 | } |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | impl Block for Battery { |
| 48 | - fn content(&mut self) -> Result<String> { | |
| 48 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 49 | 49 | let capacity = self.get_capacity()?; |
| 50 | 50 | let status = self.get_status()?; |
| 51 | 51 |
+2 −2 src/bar/blocks/datetime.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use chrono::Local; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -22,7 +22,7 @@ impl DateTime { | ||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | impl Block for DateTime { |
| 25 | - fn content(&mut self) -> Result<String> { | |
| 25 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 26 | 26 | let now = Local::now(); |
| 27 | 27 | let time_str = now.format(&self.time_format).to_string(); |
| 28 | 28 | Ok(self.format_template.replace("{}", &time_str)) |
+3 −3 src/bar/blocks/mod.rs
| @@ -1,4 +1,4 @@ | ||
| 1 | -use anyhow::Result; | |
| 1 | +use crate::errors::BlockError; | |
| 2 | 2 | use std::time::Duration; |
| 3 | 3 | |
| 4 | 4 | mod battery; |
| @@ -12,7 +12,7 @@ use ram::Ram; | ||
| 12 | 12 | use shell::ShellBlock; |
| 13 | 13 | |
| 14 | 14 | pub trait Block { |
| 15 | - fn content(&mut self) -> Result<String>; | |
| 15 | + fn content(&mut self) -> Result<String, BlockError>; | |
| 16 | 16 | fn interval(&self) -> Duration; |
| 17 | 17 | fn color(&self) -> u32; |
| 18 | 18 | } |
| @@ -89,7 +89,7 @@ impl StaticBlock { | ||
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | impl Block for StaticBlock { |
| 92 | - fn content(&mut self) -> Result<String> { | |
| 92 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 93 | 93 | Ok(self.text.clone()) |
| 94 | 94 | } |
| 95 | 95 |
+3 −3 src/bar/blocks/ram.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use std::fs; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -18,7 +18,7 @@ impl Ram { | ||
| 18 | 18 | } |
| 19 | 19 | } |
| 20 | 20 | |
| 21 | - fn get_memory_info(&self) -> Result<(u64, u64, f32)> { | |
| 21 | + fn get_memory_info(&self) -> Result<(u64, u64, f32), BlockError> { | |
| 22 | 22 | let meminfo = fs::read_to_string("/proc/meminfo")?; |
| 23 | 23 | let mut total: u64 = 0; |
| 24 | 24 | let mut available: u64 = 0; |
| @@ -51,7 +51,7 @@ impl Ram { | ||
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | impl Block for Ram { |
| 54 | - fn content(&mut self) -> Result<String> { | |
| 54 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 55 | 55 | let (used, total, percentage) = self.get_memory_info()?; |
| 56 | 56 | |
| 57 | 57 | let used_gb = used as f32 / 1024.0 / 1024.0; |
+14 −3 src/bar/blocks/shell.rs
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | use super::Block; |
| 2 | -use anyhow::Result; | |
| 2 | +use crate::errors::BlockError; | |
| 3 | 3 | use std::process::Command; |
| 4 | 4 | use std::time::Duration; |
| 5 | 5 | |
| @@ -22,8 +22,19 @@ impl ShellBlock { | ||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | impl Block for ShellBlock { |
| 25 | - fn content(&mut self) -> Result<String> { | |
| 26 | - let output = Command::new("sh").arg("-c").arg(&self.command).output()?; | |
| 25 | + fn content(&mut self) -> Result<String, BlockError> { | |
| 26 | + let output = Command::new("sh") | |
| 27 | + .arg("-c") | |
| 28 | + .arg(&self.command) | |
| 29 | + .output() | |
| 30 | + .map_err(|e| BlockError::CommandFailed(format!("Failed to execute command: {}", e)))?; | |
| 31 | + | |
| 32 | + if !output.status.success() { | |
| 33 | + return Err(BlockError::CommandFailed(format!( | |
| 34 | + "Command exited with status: {}", | |
| 35 | + output.status | |
| 36 | + ))); | |
| 37 | + } | |
| 27 | 38 | |
| 28 | 39 | let result = String::from_utf8_lossy(&output.stdout).trim().to_string(); |
| 29 | 40 | Ok(self.format.replace("{}", &result)) |
+7 −0 src/bar/font.rs
| @@ -124,6 +124,13 @@ impl FontDraw { | ||
| 124 | 124 | ); |
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | + | |
| 128 | + pub fn flush(&self) { | |
| 129 | + unsafe { | |
| 130 | + let display = x11::xft::XftDrawDisplay(self.xft_draw); | |
| 131 | + x11::xlib::XFlush(display); | |
| 132 | + } | |
| 133 | + } | |
| 127 | 134 | } |
| 128 | 135 | |
| 129 | 136 | impl Drop for FontDraw { |
+78 −11 src/bin/main.rs
| @@ -19,6 +19,11 @@ fn main() -> Result<()> { | ||
| 19 | 19 | init_config()?; |
| 20 | 20 | return Ok(()); |
| 21 | 21 | } |
| 22 | + Some("--migrate") => { | |
| 23 | + let path = args.get(2).map(PathBuf::from); | |
| 24 | + migrate_config(path)?; | |
| 25 | + return Ok(()); | |
| 26 | + } | |
| 22 | 27 | Some("--config") => { |
| 23 | 28 | if let Some(path) = args.get(2) { |
| 24 | 29 | custom_config_path = Some(PathBuf::from(path)); |
| @@ -50,27 +55,46 @@ fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> { | ||
| 50 | 55 | let config_path = if let Some(path) = custom_path { |
| 51 | 56 | path |
| 52 | 57 | } else { |
| 53 | - let default_path = get_config_path().join("config.ron"); | |
| 54 | - if !default_path.exists() { | |
| 55 | - println!("No config found at {:?}", default_path); | |
| 56 | - println!("Creating default config..."); | |
| 58 | + let config_dir = get_config_path(); | |
| 59 | + let lua_path = config_dir.join("config.lua"); | |
| 60 | + let ron_path = config_dir.join("config.ron"); | |
| 61 | + | |
| 62 | + if lua_path.exists() { | |
| 63 | + lua_path | |
| 64 | + } else if ron_path.exists() { | |
| 65 | + ron_path | |
| 66 | + } else { | |
| 67 | + println!("No config found at {:?}", config_dir); | |
| 68 | + println!("Creating default Lua config..."); | |
| 57 | 69 | init_config()?; |
| 70 | + config_dir.join("config.lua") | |
| 58 | 71 | } |
| 59 | - default_path | |
| 60 | 72 | }; |
| 61 | 73 | |
| 62 | 74 | let config_str = |
| 63 | 75 | std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?; |
| 64 | 76 | |
| 65 | - oxwm::config::parse_config(&config_str).with_context(|| "Failed to parse config") | |
| 77 | + let is_lua = config_path | |
| 78 | + .extension() | |
| 79 | + .and_then(|s| s.to_str()) | |
| 80 | + .map(|s| s == "lua") | |
| 81 | + .unwrap_or(false); | |
| 82 | + | |
| 83 | + if is_lua { | |
| 84 | + let config_dir = config_path.parent(); | |
| 85 | + oxwm::config::parse_lua_config(&config_str, config_dir) | |
| 86 | + .with_context(|| "Failed to parse Lua config") | |
| 87 | + } else { | |
| 88 | + oxwm::config::parse_config(&config_str).with_context(|| "Failed to parse RON config") | |
| 89 | + } | |
| 66 | 90 | } |
| 67 | 91 | |
| 68 | 92 | fn init_config() -> Result<()> { |
| 69 | 93 | let config_dir = get_config_path(); |
| 70 | 94 | std::fs::create_dir_all(&config_dir)?; |
| 71 | 95 | |
| 72 | - let config_template = include_str!("../../templates/config.ron"); | |
| 73 | - let config_path = config_dir.join("config.ron"); | |
| 96 | + let config_template = include_str!("../../templates/config.lua"); | |
| 97 | + let config_path = config_dir.join("config.lua"); | |
| 74 | 98 | |
| 75 | 99 | std::fs::write(&config_path, config_template)?; |
| 76 | 100 | |
| @@ -87,19 +111,62 @@ fn get_config_path() -> PathBuf { | ||
| 87 | 111 | .join("oxwm") |
| 88 | 112 | } |
| 89 | 113 | |
| 114 | +fn migrate_config(custom_path: Option<PathBuf>) -> Result<()> { | |
| 115 | + let ron_path = if let Some(path) = custom_path { | |
| 116 | + path | |
| 117 | + } else { | |
| 118 | + get_config_path().join("config.ron") | |
| 119 | + }; | |
| 120 | + | |
| 121 | + if !ron_path.exists() { | |
| 122 | + eprintln!("Error: RON config file not found at {:?}", ron_path); | |
| 123 | + eprintln!("Please specify a path: oxwm --migrate <path/to/config.ron>"); | |
| 124 | + std::process::exit(1); | |
| 125 | + } | |
| 126 | + | |
| 127 | + println!("Migrating RON config to Lua..."); | |
| 128 | + println!(" Reading: {:?}", ron_path); | |
| 129 | + | |
| 130 | + let ron_content = std::fs::read_to_string(&ron_path) | |
| 131 | + .with_context(|| format!("Failed to read RON config from {:?}", ron_path))?; | |
| 132 | + | |
| 133 | + let lua_content = oxwm::config::migrate::ron_to_lua(&ron_content) | |
| 134 | + .with_context(|| "Failed to migrate RON config to Lua")?; | |
| 135 | + | |
| 136 | + let lua_path = ron_path.with_extension("lua"); | |
| 137 | + | |
| 138 | + std::fs::write(&lua_path, lua_content) | |
| 139 | + .with_context(|| format!("Failed to write Lua config to {:?}", lua_path))?; | |
| 140 | + | |
| 141 | + println!("✓ Migration complete!"); | |
| 142 | + println!(" Output: {:?}", lua_path); | |
| 143 | + println!("\nYour old config.ron is still intact."); | |
| 144 | + println!( | |
| 145 | + "Review the new config.lua and then you can delete config.ron if everything looks good." | |
| 146 | + ); | |
| 147 | + | |
| 148 | + Ok(()) | |
| 149 | +} | |
| 150 | + | |
| 90 | 151 | fn print_help() { |
| 91 | 152 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 92 | 153 | println!("USAGE:"); |
| 93 | 154 | println!(" oxwm [OPTIONS]\n"); |
| 94 | 155 | println!("OPTIONS:"); |
| 95 | - println!(" --init Create default config in ~/.config/oxwm/config.ron"); | |
| 96 | - println!(" --config <PATH> Use custom config file"); | |
| 156 | + println!(" --init Create default config in ~/.config/oxwm/config.lua"); | |
| 157 | + println!( | |
| 158 | + " --migrate [PATH] Convert RON config to Lua (default: ~/.config/oxwm/config.ron)" | |
| 159 | + ); | |
| 160 | + println!(" --config <PATH> Use custom config file (.lua or .ron)"); | |
| 97 | 161 | println!(" --version Print version information"); |
| 98 | 162 | println!(" --help Print this help message\n"); |
| 99 | 163 | println!("CONFIG:"); |
| 100 | - println!(" Location: ~/.config/oxwm/config.ron"); | |
| 164 | + println!(" Location: ~/.config/oxwm/config.lua (or config.ron for legacy)"); | |
| 101 | 165 | println!(" Edit the config file and use Mod+Shift+R to reload"); |
| 102 | 166 | println!(" No compilation needed - instant hot-reload!\n"); |
| 167 | + println!("MIGRATION:"); | |
| 168 | + println!(" To migrate from RON to Lua: oxwm --migrate"); | |
| 169 | + println!(" Or specify a custom path: oxwm --migrate /path/to/config.ron\n"); | |
| 103 | 170 | println!("FIRST RUN:"); |
| 104 | 171 | println!(" Run 'oxwm --init' to create a config file"); |
| 105 | 172 | println!(" Or just start oxwm and it will create one automatically\n"); |
+586 −0 src/config/lua.rs new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,586 @@ | ||
| 1 | +use crate::bar::{BlockCommand, BlockConfig}; | |
| 2 | +use crate::errors::ConfigError; | |
| 3 | +use crate::keyboard::handlers::{KeyBinding, KeyPress}; | |
| 4 | +use crate::keyboard::keysyms::{self, Keysym}; | |
| 5 | +use crate::keyboard::{Arg, KeyAction}; | |
| 6 | +use crate::{ColorScheme, LayoutSymbolOverride}; | |
| 7 | +use mlua::{Lua, Table, Value}; | |
| 8 | +use x11rb::protocol::xproto::KeyButMask; | |
| 9 | + | |
| 10 | +pub fn parse_lua_config( | |
| 11 | + input: &str, | |
| 12 | + config_dir: Option<&std::path::Path>, | |
| 13 | +) -> Result<crate::Config, ConfigError> { | |
| 14 | + let lua = Lua::new(); | |
| 15 | + | |
| 16 | + if let Some(dir) = config_dir { | |
| 17 | + if let Some(dir_str) = dir.to_str() { | |
| 18 | + let setup_code = format!("package.path = '{}/?.lua;' .. package.path", dir_str); | |
| 19 | + lua.load(&setup_code) | |
| 20 | + .exec() | |
| 21 | + .map_err(|e| ConfigError::LuaError(format!("Failed to set package.path: {}", e)))?; | |
| 22 | + } | |
| 23 | + } | |
| 24 | + | |
| 25 | + let config: Table = lua | |
| 26 | + .load(input) | |
| 27 | + .eval() | |
| 28 | + .map_err(|e| ConfigError::LuaError(format!("Failed to execute Lua config: {}", e)))?; | |
| 29 | + let border_width: u32 = get_table_field(&config, "border_width")?; | |
| 30 | + let border_focused: u32 = parse_color(&config, "border_focused")?; | |
| 31 | + let border_unfocused: u32 = parse_color(&config, "border_unfocused")?; | |
| 32 | + let font: String = get_table_field(&config, "font")?; | |
| 33 | + | |
| 34 | + let gaps_enabled: bool = get_table_field(&config, "gaps_enabled")?; | |
| 35 | + let gap_inner_horizontal: u32 = get_table_field(&config, "gap_inner_horizontal")?; | |
| 36 | + let gap_inner_vertical: u32 = get_table_field(&config, "gap_inner_vertical")?; | |
| 37 | + let gap_outer_horizontal: u32 = get_table_field(&config, "gap_outer_horizontal")?; | |
| 38 | + let gap_outer_vertical: u32 = get_table_field(&config, "gap_outer_vertical")?; | |
| 39 | + | |
| 40 | + let terminal: String = get_table_field(&config, "terminal")?; | |
| 41 | + let modkey = parse_modkey(&config)?; | |
| 42 | + | |
| 43 | + let tags = parse_tags(&config)?; | |
| 44 | + let layout_symbols = parse_layout_symbols(&config)?; | |
| 45 | + let keybindings = parse_keybindings(&config, modkey)?; | |
| 46 | + let status_blocks = parse_status_blocks(&config)?; | |
| 47 | + | |
| 48 | + let scheme_normal = parse_color_scheme(&config, "scheme_normal")?; | |
| 49 | + let scheme_occupied = parse_color_scheme(&config, "scheme_occupied")?; | |
| 50 | + let scheme_selected = parse_color_scheme(&config, "scheme_selected")?; | |
| 51 | + | |
| 52 | + let autostart = parse_autostart(&config)?; | |
| 53 | + | |
| 54 | + Ok(crate::Config { | |
| 55 | + border_width, | |
| 56 | + border_focused, | |
| 57 | + border_unfocused, | |
| 58 | + font, | |
| 59 | + gaps_enabled, | |
| 60 | + gap_inner_horizontal, | |
| 61 | + gap_inner_vertical, | |
| 62 | + gap_outer_horizontal, | |
| 63 | + gap_outer_vertical, | |
| 64 | + terminal, | |
| 65 | + modkey, | |
| 66 | + tags, | |
| 67 | + layout_symbols, | |
| 68 | + keybindings, | |
| 69 | + status_blocks, | |
| 70 | + scheme_normal, | |
| 71 | + scheme_occupied, | |
| 72 | + scheme_selected, | |
| 73 | + autostart, | |
| 74 | + }) | |
| 75 | +} | |
| 76 | + | |
| 77 | +fn get_table_field<T>(table: &Table, field: &str) -> Result<T, ConfigError> | |
| 78 | +where | |
| 79 | + T: mlua::FromLua, | |
| 80 | +{ | |
| 81 | + table | |
| 82 | + .get::<T>(field) | |
| 83 | + .map_err(|e| ConfigError::LuaError(format!("Failed to get field '{}': {}", field, e))) | |
| 84 | +} | |
| 85 | + | |
| 86 | +fn parse_color(table: &Table, field: &str) -> Result<u32, ConfigError> { | |
| 87 | + let value: Value = table.get(field).map_err(|e| { | |
| 88 | + ConfigError::LuaError(format!("Failed to get color field '{}': {}", field, e)) | |
| 89 | + })?; | |
| 90 | + | |
| 91 | + match value { | |
| 92 | + Value::String(s) => { | |
| 93 | + let s = s.to_str().map_err(|e| { | |
| 94 | + ConfigError::LuaError(format!("Invalid UTF-8 in color string: {}", e)) | |
| 95 | + })?; | |
| 96 | + parse_color_string(&s) | |
| 97 | + } | |
| 98 | + Value::Integer(i) => Ok(i as u32), | |
| 99 | + Value::Number(n) => Ok(n as u32), | |
| 100 | + _ => Err(ConfigError::LuaError(format!( | |
| 101 | + "Color field '{}' must be a string or number", | |
| 102 | + field | |
| 103 | + ))), | |
| 104 | + } | |
| 105 | +} | |
| 106 | + | |
| 107 | +fn parse_color_string(s: &str) -> Result<u32, ConfigError> { | |
| 108 | + let s = s.trim(); | |
| 109 | + if s.starts_with('#') { | |
| 110 | + u32::from_str_radix(&s[1..], 16) | |
| 111 | + .map_err(|e| ConfigError::LuaError(format!("Invalid hex color '{}': {}", s, e))) | |
| 112 | + } else if s.starts_with("0x") { | |
| 113 | + u32::from_str_radix(&s[2..], 16) | |
| 114 | + .map_err(|e| ConfigError::LuaError(format!("Invalid hex color '{}': {}", s, e))) | |
| 115 | + } else { | |
| 116 | + s.parse::<u32>() | |
| 117 | + .map_err(|e| ConfigError::LuaError(format!("Invalid color '{}': {}", s, e))) | |
| 118 | + } | |
| 119 | +} | |
| 120 | + | |
| 121 | +fn parse_modkey(config: &Table) -> Result<KeyButMask, ConfigError> { | |
| 122 | + let modkey_str: String = get_table_field(config, "modkey")?; | |
| 123 | + parse_modkey_string(&modkey_str) | |
| 124 | +} | |
| 125 | + | |
| 126 | +fn parse_modkey_string(s: &str) -> Result<KeyButMask, ConfigError> { | |
| 127 | + match s { | |
| 128 | + "Mod1" => Ok(KeyButMask::MOD1), | |
| 129 | + "Mod2" => Ok(KeyButMask::MOD2), | |
| 130 | + "Mod3" => Ok(KeyButMask::MOD3), | |
| 131 | + "Mod4" => Ok(KeyButMask::MOD4), | |
| 132 | + "Mod5" => Ok(KeyButMask::MOD5), | |
| 133 | + "Shift" => Ok(KeyButMask::SHIFT), | |
| 134 | + "Control" => Ok(KeyButMask::CONTROL), | |
| 135 | + _ => Err(ConfigError::InvalidModkey(s.to_string())), | |
| 136 | + } | |
| 137 | +} | |
| 138 | + | |
| 139 | +fn parse_tags(config: &Table) -> Result<Vec<String>, ConfigError> { | |
| 140 | + let tags_table: Table = get_table_field(config, "tags")?; | |
| 141 | + let mut tags = Vec::new(); | |
| 142 | + | |
| 143 | + for i in 1..=tags_table | |
| 144 | + .len() | |
| 145 | + .map_err(|e| ConfigError::LuaError(format!("Failed to get tags length: {}", e)))? | |
| 146 | + { | |
| 147 | + let tag: String = tags_table.get(i).map_err(|e| { | |
| 148 | + ConfigError::LuaError(format!("Failed to get tag at index {}: {}", i, e)) | |
| 149 | + })?; | |
| 150 | + tags.push(tag); | |
| 151 | + } | |
| 152 | + | |
| 153 | + Ok(tags) | |
| 154 | +} | |
| 155 | + | |
| 156 | +fn parse_layout_symbols(config: &Table) -> Result<Vec<LayoutSymbolOverride>, ConfigError> { | |
| 157 | + let layout_symbols_result: Result<Table, _> = config.get("layout_symbols"); | |
| 158 | + | |
| 159 | + match layout_symbols_result { | |
| 160 | + Ok(layout_symbols_table) => { | |
| 161 | + let mut layout_symbols = Vec::new(); | |
| 162 | + | |
| 163 | + for i in 1..=layout_symbols_table.len().map_err(|e| { | |
| 164 | + ConfigError::LuaError(format!("Failed to get layout_symbols length: {}", e)) | |
| 165 | + })? { | |
| 166 | + let entry: Table = layout_symbols_table.get(i).map_err(|e| { | |
| 167 | + ConfigError::LuaError(format!( | |
| 168 | + "Failed to get layout_symbol at index {}: {}", | |
| 169 | + i, e | |
| 170 | + )) | |
| 171 | + })?; | |
| 172 | + | |
| 173 | + let name: String = get_table_field(&entry, "name")?; | |
| 174 | + let symbol: String = get_table_field(&entry, "symbol")?; | |
| 175 | + | |
| 176 | + layout_symbols.push(LayoutSymbolOverride { name, symbol }); | |
| 177 | + } | |
| 178 | + | |
| 179 | + Ok(layout_symbols) | |
| 180 | + } | |
| 181 | + Err(_) => Ok(Vec::new()), | |
| 182 | + } | |
| 183 | +} | |
| 184 | + | |
| 185 | +fn parse_keybindings(config: &Table, modkey: KeyButMask) -> Result<Vec<KeyBinding>, ConfigError> { | |
| 186 | + let keybindings_table: Table = get_table_field(config, "keybindings")?; | |
| 187 | + let mut keybindings = Vec::new(); | |
| 188 | + | |
| 189 | + for i in 1..=keybindings_table | |
| 190 | + .len() | |
| 191 | + .map_err(|e| ConfigError::LuaError(format!("Failed to get keybindings length: {}", e)))? | |
| 192 | + { | |
| 193 | + let kb_table: Table = keybindings_table.get(i).map_err(|e| { | |
| 194 | + ConfigError::LuaError(format!("Failed to get keybinding at index {}: {}", i, e)) | |
| 195 | + })?; | |
| 196 | + | |
| 197 | + let keys = parse_keypress_list(&kb_table, modkey)?; | |
| 198 | + let action = parse_key_action(&kb_table)?; | |
| 199 | + let arg = parse_arg(&kb_table)?; | |
| 200 | + | |
| 201 | + keybindings.push(KeyBinding::new(keys, action, arg)); | |
| 202 | + } | |
| 203 | + | |
| 204 | + Ok(keybindings) | |
| 205 | +} | |
| 206 | + | |
| 207 | +fn parse_keypress_list(kb_table: &Table, modkey: KeyButMask) -> Result<Vec<KeyPress>, ConfigError> { | |
| 208 | + let keys_result: Result<Table, _> = kb_table.get("keys"); | |
| 209 | + | |
| 210 | + if let Ok(keys_table) = keys_result { | |
| 211 | + let mut keys = Vec::new(); | |
| 212 | + for i in 1..=keys_table | |
| 213 | + .len() | |
| 214 | + .map_err(|e| ConfigError::LuaError(format!("Failed to get keys length: {}", e)))? | |
| 215 | + { | |
| 216 | + let key_entry: Table = keys_table.get(i).map_err(|e| { | |
| 217 | + ConfigError::LuaError(format!("Failed to get key at index {}: {}", i, e)) | |
| 218 | + })?; | |
| 219 | + | |
| 220 | + let modifiers = parse_modifiers(&key_entry, "modifiers", modkey)?; | |
| 221 | + let keysym = parse_keysym(&key_entry, "key")?; | |
| 222 | + | |
| 223 | + keys.push(KeyPress { modifiers, keysym }); | |
| 224 | + } | |
| 225 | + Ok(keys) | |
| 226 | + } else { | |
| 227 | + let modifiers = parse_modifiers(kb_table, "modifiers", modkey)?; | |
| 228 | + let keysym = parse_keysym(kb_table, "key")?; | |
| 229 | + | |
| 230 | + Ok(vec![KeyPress { modifiers, keysym }]) | |
| 231 | + } | |
| 232 | +} | |
| 233 | + | |
| 234 | +fn parse_modifiers( | |
| 235 | + table: &Table, | |
| 236 | + field: &str, | |
| 237 | + modkey: KeyButMask, | |
| 238 | +) -> Result<Vec<KeyButMask>, ConfigError> { | |
| 239 | + let mods_table: Table = get_table_field(table, field)?; | |
| 240 | + let mut modifiers = Vec::new(); | |
| 241 | + | |
| 242 | + for i in 1..=mods_table | |
| 243 | + .len() | |
| 244 | + .map_err(|e| ConfigError::LuaError(format!("Failed to get modifiers length: {}", e)))? | |
| 245 | + { | |
| 246 | + let mod_str: String = mods_table.get(i).map_err(|e| { | |
| 247 | + ConfigError::LuaError(format!("Failed to get modifier at index {}: {}", i, e)) | |
| 248 | + })?; | |
| 249 | + | |
| 250 | + let modifier = if mod_str == "Mod" { | |
| 251 | + modkey | |
| 252 | + } else { | |
| 253 | + parse_modkey_string(&mod_str)? | |
| 254 | + }; | |
| 255 | + | |
| 256 | + modifiers.push(modifier); | |
| 257 | + } | |
| 258 | + | |
| 259 | + Ok(modifiers) | |
| 260 | +} | |
| 261 | + | |
| 262 | +fn parse_keysym(table: &Table, field: &str) -> Result<Keysym, ConfigError> { | |
| 263 | + let key_str: String = get_table_field(table, field)?; | |
| 264 | + string_to_keysym(&key_str) | |
| 265 | +} | |
| 266 | + | |
| 267 | +fn string_to_keysym(s: &str) -> Result<Keysym, ConfigError> { | |
| 268 | + let keysym = match s { | |
| 269 | + "Return" => keysyms::XK_RETURN, | |
| 270 | + "Q" => keysyms::XK_Q, | |
| 271 | + "Escape" => keysyms::XK_ESCAPE, | |
| 272 | + "Space" => keysyms::XK_SPACE, | |
| 273 | + "Tab" => keysyms::XK_TAB, | |
| 274 | + "Backspace" => keysyms::XK_BACKSPACE, | |
| 275 | + "Delete" => keysyms::XK_DELETE, | |
| 276 | + "F1" => keysyms::XK_F1, | |
| 277 | + "F2" => keysyms::XK_F2, | |
| 278 | + "F3" => keysyms::XK_F3, | |
| 279 | + "F4" => keysyms::XK_F4, | |
| 280 | + "F5" => keysyms::XK_F5, | |
| 281 | + "F6" => keysyms::XK_F6, | |
| 282 | + "F7" => keysyms::XK_F7, | |
| 283 | + "F8" => keysyms::XK_F8, | |
| 284 | + "F9" => keysyms::XK_F9, | |
| 285 | + "F10" => keysyms::XK_F10, | |
| 286 | + "F11" => keysyms::XK_F11, | |
| 287 | + "F12" => keysyms::XK_F12, | |
| 288 | + "A" => keysyms::XK_A, | |
| 289 | + "B" => keysyms::XK_B, | |
| 290 | + "C" => keysyms::XK_C, | |
| 291 | + "D" => keysyms::XK_D, | |
| 292 | + "E" => keysyms::XK_E, | |
| 293 | + "F" => keysyms::XK_F, | |
| 294 | + "G" => keysyms::XK_G, | |
| 295 | + "H" => keysyms::XK_H, | |
| 296 | + "I" => keysyms::XK_I, | |
| 297 | + "J" => keysyms::XK_J, | |
| 298 | + "K" => keysyms::XK_K, | |
| 299 | + "L" => keysyms::XK_L, | |
| 300 | + "M" => keysyms::XK_M, | |
| 301 | + "N" => keysyms::XK_N, | |
| 302 | + "O" => keysyms::XK_O, | |
| 303 | + "P" => keysyms::XK_P, | |
| 304 | + "R" => keysyms::XK_R, | |
| 305 | + "S" => keysyms::XK_S, | |
| 306 | + "T" => keysyms::XK_T, | |
| 307 | + "U" => keysyms::XK_U, | |
| 308 | + "V" => keysyms::XK_V, | |
| 309 | + "W" => keysyms::XK_W, | |
| 310 | + "X" => keysyms::XK_X, | |
| 311 | + "Y" => keysyms::XK_Y, | |
| 312 | + "Z" => keysyms::XK_Z, | |
| 313 | + "0" => keysyms::XK_0, | |
| 314 | + "1" => keysyms::XK_1, | |
| 315 | + "2" => keysyms::XK_2, | |
| 316 | + "3" => keysyms::XK_3, | |
| 317 | + "4" => keysyms::XK_4, | |
| 318 | + "5" => keysyms::XK_5, | |
| 319 | + "6" => keysyms::XK_6, | |
| 320 | + "7" => keysyms::XK_7, | |
| 321 | + "8" => keysyms::XK_8, | |
| 322 | + "9" => keysyms::XK_9, | |
| 323 | + "Left" => keysyms::XK_LEFT, | |
| 324 | + "Right" => keysyms::XK_RIGHT, | |
| 325 | + "Up" => keysyms::XK_UP, | |
| 326 | + "Down" => keysyms::XK_DOWN, | |
| 327 | + "Home" => keysyms::XK_HOME, | |
| 328 | + "End" => keysyms::XK_END, | |
| 329 | + "PageUp" => keysyms::XK_PAGE_UP, | |
| 330 | + "PageDown" => keysyms::XK_PAGE_DOWN, | |
| 331 | + "Insert" => keysyms::XK_INSERT, | |
| 332 | + "Minus" => keysyms::XK_MINUS, | |
| 333 | + "Equal" => keysyms::XK_EQUAL, | |
| 334 | + "BracketLeft" => keysyms::XK_LEFT_BRACKET, | |
| 335 | + "BracketRight" => keysyms::XK_RIGHT_BRACKET, | |
| 336 | + "Semicolon" => keysyms::XK_SEMICOLON, | |
| 337 | + "Apostrophe" => keysyms::XK_APOSTROPHE, | |
| 338 | + "Grave" => keysyms::XK_GRAVE, | |
| 339 | + "Backslash" => keysyms::XK_BACKSLASH, | |
| 340 | + "Comma" => keysyms::XK_COMMA, | |
| 341 | + "Period" => keysyms::XK_PERIOD, | |
| 342 | + "Slash" => keysyms::XK_SLASH, | |
| 343 | + "AudioRaiseVolume" => keysyms::XF86_AUDIO_RAISE_VOLUME, | |
| 344 | + "AudioLowerVolume" => keysyms::XF86_AUDIO_LOWER_VOLUME, | |
| 345 | + "AudioMute" => keysyms::XF86_AUDIO_MUTE, | |
| 346 | + "MonBrightnessUp" => keysyms::XF86_MON_BRIGHTNESS_UP, | |
| 347 | + "MonBrightnessDown" => keysyms::XF86_MON_BRIGHTNESS_DOWN, | |
| 348 | + _ => return Err(ConfigError::UnknownKey(s.to_string())), | |
| 349 | + }; | |
| 350 | + | |
| 351 | + Ok(keysym) | |
| 352 | +} | |
| 353 | + | |
| 354 | +fn parse_key_action(kb_table: &Table) -> Result<KeyAction, ConfigError> { | |
| 355 | + let action_str: String = get_table_field(kb_table, "action")?; | |
| 356 | + string_to_key_action(&action_str) | |
| 357 | +} | |
| 358 | + | |
| 359 | +fn string_to_key_action(s: &str) -> Result<KeyAction, ConfigError> { | |
| 360 | + let action = match s { | |
| 361 | + "Spawn" => KeyAction::Spawn, | |
| 362 | + "KillClient" => KeyAction::KillClient, | |
| 363 | + "FocusStack" => KeyAction::FocusStack, | |
| 364 | + "FocusDirection" => KeyAction::FocusDirection, | |
| 365 | + "SwapDirection" => KeyAction::SwapDirection, | |
| 366 | + "Quit" => KeyAction::Quit, | |
| 367 | + "Restart" => KeyAction::Restart, | |
| 368 | + "Recompile" => KeyAction::Recompile, | |
| 369 | + "ViewTag" => KeyAction::ViewTag, | |
| 370 | + "ToggleGaps" => KeyAction::ToggleGaps, | |
| 371 | + "ToggleFullScreen" => KeyAction::ToggleFullScreen, | |
| 372 | + "ToggleFloating" => KeyAction::ToggleFloating, | |
| 373 | + "ChangeLayout" => KeyAction::ChangeLayout, | |
| 374 | + "CycleLayout" => KeyAction::CycleLayout, | |
| 375 | + "MoveToTag" => KeyAction::MoveToTag, | |
| 376 | + "FocusMonitor" => KeyAction::FocusMonitor, | |
| 377 | + "SmartMoveWin" => KeyAction::SmartMoveWin, | |
| 378 | + "ExchangeClient" => KeyAction::ExchangeClient, | |
| 379 | + "ShowKeybindOverlay" => KeyAction::ShowKeybindOverlay, | |
| 380 | + "None" => KeyAction::None, | |
| 381 | + _ => return Err(ConfigError::UnknownAction(s.to_string())), | |
| 382 | + }; | |
| 383 | + | |
| 384 | + Ok(action) | |
| 385 | +} | |
| 386 | + | |
| 387 | +fn parse_arg(kb_table: &Table) -> Result<Arg, ConfigError> { | |
| 388 | + let arg_result: Result<Value, _> = kb_table.get("arg"); | |
| 389 | + | |
| 390 | + match arg_result { | |
| 391 | + Ok(Value::Nil) | Err(_) => Ok(Arg::None), | |
| 392 | + Ok(Value::String(s)) => { | |
| 393 | + let s = s | |
| 394 | + .to_str() | |
| 395 | + .map_err(|e| ConfigError::LuaError(format!("Invalid UTF-8 in arg: {}", e)))?; | |
| 396 | + Ok(Arg::Str(s.to_string())) | |
| 397 | + } | |
| 398 | + Ok(Value::Integer(i)) => Ok(Arg::Int(i as i32)), | |
| 399 | + Ok(Value::Number(n)) => Ok(Arg::Int(n as i32)), | |
| 400 | + Ok(Value::Table(t)) => { | |
| 401 | + let mut arr = Vec::new(); | |
| 402 | + for i in 1..=t.len().map_err(|e| { | |
| 403 | + ConfigError::LuaError(format!("Failed to get arg array length: {}", e)) | |
| 404 | + })? { | |
| 405 | + let item: String = t.get(i).map_err(|e| { | |
| 406 | + ConfigError::LuaError(format!( | |
| 407 | + "Failed to get arg array item at index {}: {}", | |
| 408 | + i, e | |
| 409 | + )) | |
| 410 | + })?; | |
| 411 | + arr.push(item); | |
| 412 | + } | |
| 413 | + Ok(Arg::Array(arr)) | |
| 414 | + } | |
| 415 | + Ok(_) => Err(ConfigError::LuaError( | |
| 416 | + "Arg must be nil, string, number, or array".to_string(), | |
| 417 | + )), | |
| 418 | + } | |
| 419 | +} | |
| 420 | + | |
| 421 | +fn parse_status_blocks(config: &Table) -> Result<Vec<BlockConfig>, ConfigError> { | |
| 422 | + let blocks_table: Table = get_table_field(config, "status_blocks")?; | |
| 423 | + let mut blocks = Vec::new(); | |
| 424 | + | |
| 425 | + for i in 1..=blocks_table | |
| 426 | + .len() | |
| 427 | + .map_err(|e| ConfigError::LuaError(format!("Failed to get status_blocks length: {}", e)))? | |
| 428 | + { | |
| 429 | + let block_table: Table = blocks_table.get(i).map_err(|e| { | |
| 430 | + ConfigError::LuaError(format!("Failed to get status_block at index {}: {}", i, e)) | |
| 431 | + })?; | |
| 432 | + | |
| 433 | + let format: String = get_table_field(&block_table, "format")?; | |
| 434 | + let command_str: String = get_table_field(&block_table, "command")?; | |
| 435 | + | |
| 436 | + let interval_secs: u64 = { | |
| 437 | + let value: Value = block_table.get("interval_secs").map_err(|e| { | |
| 438 | + ConfigError::LuaError(format!("Failed to get interval_secs: {}", e)) | |
| 439 | + })?; | |
| 440 | + match value { | |
| 441 | + Value::Integer(i) => i as u64, | |
| 442 | + Value::Number(n) => n as u64, | |
| 443 | + _ => { | |
| 444 | + return Err(ConfigError::LuaError( | |
| 445 | + "interval_secs must be a number".to_string(), | |
| 446 | + )); | |
| 447 | + } | |
| 448 | + } | |
| 449 | + }; | |
| 450 | + | |
| 451 | + let color: u32 = parse_color(&block_table, "color")?; | |
| 452 | + let underline: bool = get_table_field(&block_table, "underline")?; | |
| 453 | + | |
| 454 | + let command = match command_str.as_str() { | |
| 455 | + "DateTime" => { | |
| 456 | + let fmt: String = get_table_field(&block_table, "command_arg")?; | |
| 457 | + BlockCommand::DateTime(fmt) | |
| 458 | + } | |
| 459 | + "Shell" => { | |
| 460 | + let cmd: String = get_table_field(&block_table, "command_arg")?; | |
| 461 | + BlockCommand::Shell(cmd) | |
| 462 | + } | |
| 463 | + "Ram" => BlockCommand::Ram, | |
| 464 | + "Static" => { | |
| 465 | + let text_result: Result<String, _> = block_table.get("command_arg"); | |
| 466 | + let text = text_result.unwrap_or_default(); | |
| 467 | + BlockCommand::Static(text) | |
| 468 | + } | |
| 469 | + "Battery" => { | |
| 470 | + let formats_table: Table = get_table_field(&block_table, "battery_formats")?; | |
| 471 | + let format_charging: String = get_table_field(&formats_table, "charging")?; | |
| 472 | + let format_discharging: String = get_table_field(&formats_table, "discharging")?; | |
| 473 | + let format_full: String = get_table_field(&formats_table, "full")?; | |
| 474 | + | |
| 475 | + BlockCommand::Battery { | |
| 476 | + format_charging, | |
| 477 | + format_discharging, | |
| 478 | + format_full, | |
| 479 | + } | |
| 480 | + } | |
| 481 | + _ => return Err(ConfigError::UnknownBlockCommand(command_str)), | |
| 482 | + }; | |
| 483 | + | |
| 484 | + blocks.push(BlockConfig { | |
| 485 | + format, | |
| 486 | + command, | |
| 487 | + interval_secs, | |
| 488 | + color, | |
| 489 | + underline, | |
| 490 | + }); | |
| 491 | + } | |
| 492 | + | |
| 493 | + Ok(blocks) | |
| 494 | +} | |
| 495 | + | |
| 496 | +fn parse_color_scheme(config: &Table, field: &str) -> Result<ColorScheme, ConfigError> { | |
| 497 | + let scheme_table: Table = get_table_field(config, field)?; | |
| 498 | + | |
| 499 | + let foreground = parse_color(&scheme_table, "foreground")?; | |
| 500 | + let background = parse_color(&scheme_table, "background")?; | |
| 501 | + let underline = parse_color(&scheme_table, "underline")?; | |
| 502 | + | |
| 503 | + Ok(ColorScheme { | |
| 504 | + foreground, | |
| 505 | + background, | |
| 506 | + underline, | |
| 507 | + }) | |
| 508 | +} | |
| 509 | + | |
| 510 | +fn parse_autostart(config: &Table) -> Result<Vec<String>, ConfigError> { | |
| 511 | + let autostart_result: Result<Table, _> = config.get("autostart"); | |
| 512 | + | |
| 513 | + match autostart_result { | |
| 514 | + Ok(autostart_table) => { | |
| 515 | + let mut autostart = Vec::new(); | |
| 516 | + | |
| 517 | + for i in 1..=autostart_table.len().map_err(|e| { | |
| 518 | + ConfigError::LuaError(format!("Failed to get autostart length: {}", e)) | |
| 519 | + })? { | |
| 520 | + let cmd: String = autostart_table.get(i).map_err(|e| { | |
| 521 | + ConfigError::LuaError(format!( | |
| 522 | + "Failed to get autostart command at index {}: {}", | |
| 523 | + i, e | |
| 524 | + )) | |
| 525 | + })?; | |
| 526 | + autostart.push(cmd); | |
| 527 | + } | |
| 528 | + | |
| 529 | + Ok(autostart) | |
| 530 | + } | |
| 531 | + Err(_) => Ok(Vec::new()), | |
| 532 | + } | |
| 533 | +} | |
| 534 | + | |
| 535 | +#[cfg(test)] | |
| 536 | +mod tests { | |
| 537 | + use super::*; | |
| 538 | + | |
| 539 | + #[test] | |
| 540 | + fn test_parse_minimal_lua_config() { | |
| 541 | + let config_str = r#" | |
| 542 | +return { | |
| 543 | + border_width = 2, | |
| 544 | + border_focused = 0x6dade3, | |
| 545 | + border_unfocused = 0xbbbbbb, | |
| 546 | + font = "monospace:style=Bold:size=10", | |
| 547 | + | |
| 548 | + gaps_enabled = true, | |
| 549 | + gap_inner_horizontal = 5, | |
| 550 | + gap_inner_vertical = 5, | |
| 551 | + gap_outer_horizontal = 5, | |
| 552 | + gap_outer_vertical = 5, | |
| 553 | + | |
| 554 | + modkey = "Mod4", | |
| 555 | + terminal = "st", | |
| 556 | + | |
| 557 | + tags = {"1", "2", "3"}, | |
| 558 | + | |
| 559 | + keybindings = { | |
| 560 | + {modifiers = {"Mod4"}, key = "Return", action = "Spawn", arg = "st"}, | |
| 561 | + {modifiers = {"Mod4"}, key = "Q", action = "KillClient"}, | |
| 562 | + }, | |
| 563 | + | |
| 564 | + status_blocks = { | |
| 565 | + {format = "{}", command = "DateTime", command_arg = "%H:%M", interval_secs = 1, color = 0xffffff, underline = true}, | |
| 566 | + }, | |
| 567 | + | |
| 568 | + scheme_normal = {foreground = 0xffffff, background = 0x000000, underline = 0x444444}, | |
| 569 | + scheme_occupied = {foreground = 0xffffff, background = 0x000000, underline = 0x444444}, | |
| 570 | + scheme_selected = {foreground = 0xffffff, background = 0x000000, underline = 0x444444}, | |
| 571 | + | |
| 572 | + autostart = {}, | |
| 573 | +} | |
| 574 | +"#; | |
| 575 | + | |
| 576 | + let config = parse_lua_config(config_str, None).expect("Failed to parse config"); | |
| 577 | + | |
| 578 | + assert_eq!(config.border_width, 2); | |
| 579 | + assert_eq!(config.border_focused, 0x6dade3); | |
| 580 | + assert_eq!(config.terminal, "st"); | |
| 581 | + assert_eq!(config.tags.len(), 3); | |
| 582 | + assert_eq!(config.keybindings.len(), 2); | |
| 583 | + assert_eq!(config.status_blocks.len(), 1); | |
| 584 | + assert!(config.gaps_enabled); | |
| 585 | + } | |
| 586 | +} |
+553 −0 src/config/migrate.rs new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,553 @@ | ||
| 1 | +use crate::errors::ConfigError; | |
| 2 | +use std::collections::HashMap; | |
| 3 | + | |
| 4 | +pub fn ron_to_lua(ron_content: &str) -> Result<String, ConfigError> { | |
| 5 | + let mut lua_output = String::new(); | |
| 6 | + let defines = extract_defines(ron_content); | |
| 7 | + | |
| 8 | + lua_output.push_str("-- OXWM Configuration File (Lua)\n"); | |
| 9 | + lua_output.push_str("-- Migrated from config.ron\n"); | |
| 10 | + lua_output.push_str("-- Edit this file and reload with Mod+Shift+R (no compilation needed!)\n\n"); | |
| 11 | + | |
| 12 | + let terminal = resolve_value(&defines.get("$terminal").cloned().unwrap_or_else(|| "\"st\"".to_string()), &defines); | |
| 13 | + let modkey = resolve_value(&defines.get("$modkey").cloned().unwrap_or_else(|| "Mod4".to_string()), &defines); | |
| 14 | + let secondary_modkey = defines.get("$secondary_modkey").map(|v| resolve_value(v, &defines)); | |
| 15 | + | |
| 16 | + lua_output.push_str(&format!("local terminal = {}\n", terminal)); | |
| 17 | + lua_output.push_str(&format!("local modkey = \"{}\"\n", modkey.trim_matches('"'))); | |
| 18 | + if let Some(sec_mod) = secondary_modkey { | |
| 19 | + lua_output.push_str(&format!("local secondary_modkey = \"{}\"\n", sec_mod.trim_matches('"'))); | |
| 20 | + } | |
| 21 | + lua_output.push_str("\n"); | |
| 22 | + | |
| 23 | + lua_output.push_str("-- Color palette\n"); | |
| 24 | + lua_output.push_str("local colors = {\n"); | |
| 25 | + for (key, value) in &defines { | |
| 26 | + if key.starts_with("$color_") { | |
| 27 | + let color_name = &key[7..]; | |
| 28 | + let color_value = if value.starts_with("0x") { | |
| 29 | + format!("\"#{}\"", &value[2..]) | |
| 30 | + } else { | |
| 31 | + value.clone() | |
| 32 | + }; | |
| 33 | + lua_output.push_str(&format!(" {} = {},\n", color_name, color_value)); | |
| 34 | + } | |
| 35 | + } | |
| 36 | + lua_output.push_str("}\n\n"); | |
| 37 | + | |
| 38 | + lua_output.push_str("-- Main configuration table\n"); | |
| 39 | + lua_output.push_str("return {\n"); | |
| 40 | + | |
| 41 | + if let Some(config_start) = ron_content.find('(') { | |
| 42 | + let config_content = &ron_content[config_start + 1..]; | |
| 43 | + | |
| 44 | + lua_output.push_str(" -- Appearance\n"); | |
| 45 | + if let Some(val) = extract_field(config_content, "border_width") { | |
| 46 | + lua_output.push_str(&format!(" border_width = {},\n", val)); | |
| 47 | + } | |
| 48 | + if let Some(val) = extract_field(config_content, "border_focused") { | |
| 49 | + lua_output.push_str(&format!(" border_focused = {},\n", resolve_color_value(&val, &defines))); | |
| 50 | + } | |
| 51 | + if let Some(val) = extract_field(config_content, "border_unfocused") { | |
| 52 | + lua_output.push_str(&format!(" border_unfocused = {},\n", resolve_color_value(&val, &defines))); | |
| 53 | + } | |
| 54 | + if let Some(val) = extract_field(config_content, "font") { | |
| 55 | + lua_output.push_str(&format!(" font = {},\n", val)); | |
| 56 | + } | |
| 57 | + | |
| 58 | + lua_output.push_str("\n -- Window gaps\n"); | |
| 59 | + if let Some(val) = extract_field(config_content, "gaps_enabled") { | |
| 60 | + lua_output.push_str(&format!(" gaps_enabled = {},\n", val)); | |
| 61 | + } | |
| 62 | + if let Some(val) = extract_field(config_content, "gap_inner_horizontal") { | |
| 63 | + lua_output.push_str(&format!(" gap_inner_horizontal = {},\n", val)); | |
| 64 | + } | |
| 65 | + if let Some(val) = extract_field(config_content, "gap_inner_vertical") { | |
| 66 | + lua_output.push_str(&format!(" gap_inner_vertical = {},\n", val)); | |
| 67 | + } | |
| 68 | + if let Some(val) = extract_field(config_content, "gap_outer_horizontal") { | |
| 69 | + lua_output.push_str(&format!(" gap_outer_horizontal = {},\n", val)); | |
| 70 | + } | |
| 71 | + if let Some(val) = extract_field(config_content, "gap_outer_vertical") { | |
| 72 | + lua_output.push_str(&format!(" gap_outer_vertical = {},\n", val)); | |
| 73 | + } | |
| 74 | + | |
| 75 | + lua_output.push_str("\n -- Basics\n"); | |
| 76 | + if let Some(val) = extract_field(config_content, "modkey") { | |
| 77 | + let resolved = resolve_value(&val, &defines).trim_matches('"').to_string(); | |
| 78 | + if resolved == "modkey" { | |
| 79 | + lua_output.push_str(" modkey = modkey,\n"); | |
| 80 | + } else { | |
| 81 | + lua_output.push_str(&format!(" modkey = \"{}\",\n", resolved)); | |
| 82 | + } | |
| 83 | + } | |
| 84 | + if let Some(val) = extract_field(config_content, "terminal") { | |
| 85 | + let resolved = resolve_value(&val, &defines); | |
| 86 | + if resolved == "terminal" { | |
| 87 | + lua_output.push_str(" terminal = terminal,\n"); | |
| 88 | + } else { | |
| 89 | + lua_output.push_str(&format!(" terminal = {},\n", resolved)); | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + lua_output.push_str("\n -- Workspace tags\n"); | |
| 94 | + if let Some(val) = extract_field(config_content, "tags") { | |
| 95 | + lua_output.push_str(&format!(" tags = {},\n", convert_array_to_lua(&val))); | |
| 96 | + } | |
| 97 | + | |
| 98 | + lua_output.push_str("\n -- Layout symbol overrides\n"); | |
| 99 | + if let Some(val) = extract_field(config_content, "layout_symbols") { | |
| 100 | + lua_output.push_str(" layout_symbols = "); | |
| 101 | + lua_output.push_str(&convert_layout_symbols(&val)); | |
| 102 | + lua_output.push_str(",\n"); | |
| 103 | + } | |
| 104 | + | |
| 105 | + lua_output.push_str("\n -- Keybindings\n"); | |
| 106 | + if let Some(val) = extract_field(config_content, "keybindings") { | |
| 107 | + lua_output.push_str(" keybindings = "); | |
| 108 | + lua_output.push_str(&convert_keybindings(&val, &defines)); | |
| 109 | + lua_output.push_str(",\n"); | |
| 110 | + } | |
| 111 | + | |
| 112 | + lua_output.push_str("\n -- Status bar blocks\n"); | |
| 113 | + if let Some(val) = extract_field(config_content, "status_blocks") { | |
| 114 | + lua_output.push_str(" status_blocks = "); | |
| 115 | + lua_output.push_str(&convert_status_blocks(&val, &defines)); | |
| 116 | + lua_output.push_str(",\n"); | |
| 117 | + } | |
| 118 | + | |
| 119 | + lua_output.push_str("\n -- Color schemes for bar\n"); | |
| 120 | + if let Some(val) = extract_field(config_content, "scheme_normal") { | |
| 121 | + lua_output.push_str(" scheme_normal = "); | |
| 122 | + lua_output.push_str(&convert_color_scheme(&val, &defines)); | |
| 123 | + lua_output.push_str(",\n"); | |
| 124 | + } | |
| 125 | + if let Some(val) = extract_field(config_content, "scheme_occupied") { | |
| 126 | + lua_output.push_str(" scheme_occupied = "); | |
| 127 | + lua_output.push_str(&convert_color_scheme(&val, &defines)); | |
| 128 | + lua_output.push_str(",\n"); | |
| 129 | + } | |
| 130 | + if let Some(val) = extract_field(config_content, "scheme_selected") { | |
| 131 | + lua_output.push_str(" scheme_selected = "); | |
| 132 | + lua_output.push_str(&convert_color_scheme(&val, &defines)); | |
| 133 | + lua_output.push_str(",\n"); | |
| 134 | + } | |
| 135 | + | |
| 136 | + lua_output.push_str("\n -- Autostart commands\n"); | |
| 137 | + if let Some(val) = extract_field(config_content, "autostart") { | |
| 138 | + let converted = convert_array_to_lua(&val); | |
| 139 | + lua_output.push_str(" autostart = "); | |
| 140 | + lua_output.push_str(&converted); | |
| 141 | + lua_output.push_str(",\n"); | |
| 142 | + } else { | |
| 143 | + lua_output.push_str(" autostart = {},\n"); | |
| 144 | + } | |
| 145 | + } | |
| 146 | + | |
| 147 | + lua_output.push_str("}\n"); | |
| 148 | + | |
| 149 | + Ok(lua_output) | |
| 150 | +} | |
| 151 | + | |
| 152 | +fn extract_defines(content: &str) -> HashMap<String, String> { | |
| 153 | + let mut defines = HashMap::new(); | |
| 154 | + for line in content.lines() { | |
| 155 | + let trimmed = line.trim(); | |
| 156 | + if trimmed.starts_with("#DEFINE") { | |
| 157 | + if let Some(rest) = trimmed.strip_prefix("#DEFINE") { | |
| 158 | + if let Some(eq_pos) = rest.find('=') { | |
| 159 | + let var_name = rest[..eq_pos].trim().to_string(); | |
| 160 | + let value = rest[eq_pos + 1..].trim().trim_end_matches(',').to_string(); | |
| 161 | + defines.insert(var_name, value); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + } | |
| 165 | + } | |
| 166 | + defines | |
| 167 | +} | |
| 168 | + | |
| 169 | +fn resolve_value(value: &str, defines: &HashMap<String, String>) -> String { | |
| 170 | + if let Some(resolved) = defines.get(value) { | |
| 171 | + resolved.clone() | |
| 172 | + } else { | |
| 173 | + value.to_string() | |
| 174 | + } | |
| 175 | +} | |
| 176 | + | |
| 177 | +fn resolve_color_value(value: &str, defines: &HashMap<String, String>) -> String { | |
| 178 | + let resolved = resolve_value(value, defines); | |
| 179 | + if resolved.starts_with("$color_") { | |
| 180 | + format!("colors.{}", &resolved[7..]) | |
| 181 | + } else if value.starts_with("$color_") { | |
| 182 | + format!("colors.{}", &value[7..]) | |
| 183 | + } else if resolved.starts_with("0x") { | |
| 184 | + format!("\"#{}\"", &resolved[2..]) | |
| 185 | + } else { | |
| 186 | + resolved | |
| 187 | + } | |
| 188 | +} | |
| 189 | + | |
| 190 | +fn extract_field(content: &str, field_name: &str) -> Option<String> { | |
| 191 | + let pattern = format!("{}:", field_name); | |
| 192 | + let cleaned_content = remove_comments(content); | |
| 193 | + | |
| 194 | + if let Some(start) = cleaned_content.find(&pattern) { | |
| 195 | + let after_colon = &cleaned_content[start + pattern.len()..]; | |
| 196 | + let value_start = after_colon.trim_start(); | |
| 197 | + | |
| 198 | + if value_start.starts_with('[') { | |
| 199 | + extract_bracketed(value_start, '[', ']') | |
| 200 | + } else if value_start.starts_with('(') { | |
| 201 | + extract_bracketed(value_start, '(', ')') | |
| 202 | + } else if value_start.starts_with('"') { | |
| 203 | + if let Some(end) = value_start[1..].find('"') { | |
| 204 | + Some(value_start[..end + 2].to_string()) | |
| 205 | + } else { | |
| 206 | + None | |
| 207 | + } | |
| 208 | + } else { | |
| 209 | + let end = value_start | |
| 210 | + .find(|c: char| c == ',' || c == '\n' || c == ')') | |
| 211 | + .unwrap_or(value_start.len()); | |
| 212 | + Some(value_start[..end].trim().to_string()) | |
| 213 | + } | |
| 214 | + } else { | |
| 215 | + None | |
| 216 | + } | |
| 217 | +} | |
| 218 | + | |
| 219 | +fn extract_bracketed(s: &str, open: char, close: char) -> Option<String> { | |
| 220 | + if !s.starts_with(open) { | |
| 221 | + return None; | |
| 222 | + } | |
| 223 | + let mut depth = 0; | |
| 224 | + let mut end = 0; | |
| 225 | + for (i, c) in s.char_indices() { | |
| 226 | + if c == open { | |
| 227 | + depth += 1; | |
| 228 | + } else if c == close { | |
| 229 | + depth -= 1; | |
| 230 | + if depth == 0 { | |
| 231 | + end = i + 1; | |
| 232 | + break; | |
| 233 | + } | |
| 234 | + } | |
| 235 | + } | |
| 236 | + if end > 0 { | |
| 237 | + Some(s[..end].to_string()) | |
| 238 | + } else { | |
| 239 | + None | |
| 240 | + } | |
| 241 | +} | |
| 242 | + | |
| 243 | +fn convert_array_to_lua(ron_array: &str) -> String { | |
| 244 | + let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); | |
| 245 | + let items: Vec<&str> = inner.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()).collect(); | |
| 246 | + format!("{{ {} }}", items.join(", ")) | |
| 247 | +} | |
| 248 | + | |
| 249 | +fn convert_layout_symbols(ron_array: &str) -> String { | |
| 250 | + let mut result = String::from("{\n"); | |
| 251 | + let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); | |
| 252 | + | |
| 253 | + let items = extract_all_bracketed(inner, '(', ')'); | |
| 254 | + for item in items { | |
| 255 | + let item_inner = item.trim_start_matches('(').trim_end_matches(')'); | |
| 256 | + if let (Some(name), Some(symbol)) = (extract_quoted_value(item_inner, "name"), extract_quoted_value(item_inner, "symbol")) { | |
| 257 | + result.push_str(&format!(" {{ name = \"{}\", symbol = \"{}\" }},\n", name, symbol)); | |
| 258 | + } | |
| 259 | + } | |
| 260 | + | |
| 261 | + result.push_str(" }"); | |
| 262 | + result | |
| 263 | +} | |
| 264 | + | |
| 265 | +fn convert_keybindings(ron_array: &str, defines: &HashMap<String, String>) -> String { | |
| 266 | + let mut result = String::from("{\n"); | |
| 267 | + let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); | |
| 268 | + | |
| 269 | + let items = extract_all_bracketed(inner, '(', ')'); | |
| 270 | + for item in items { | |
| 271 | + let binding = convert_keybinding(&item, defines); | |
| 272 | + result.push_str(&binding); | |
| 273 | + result.push_str(",\n"); | |
| 274 | + } | |
| 275 | + | |
| 276 | + result.push_str(" }"); | |
| 277 | + result | |
| 278 | +} | |
| 279 | + | |
| 280 | +fn convert_keybinding(ron_binding: &str, defines: &HashMap<String, String>) -> String { | |
| 281 | + let inner = ron_binding.trim_start_matches('(').trim_end_matches(')'); | |
| 282 | + | |
| 283 | + if inner.contains("keys:") { | |
| 284 | + convert_keychord(inner, defines) | |
| 285 | + } else { | |
| 286 | + convert_single_key(inner, defines) | |
| 287 | + } | |
| 288 | +} | |
| 289 | + | |
| 290 | +fn convert_keychord(inner: &str, defines: &HashMap<String, String>) -> String { | |
| 291 | + let mut result = String::from(" {\n keys = {\n"); | |
| 292 | + | |
| 293 | + if let Some(keys_str) = extract_field(inner, "keys") { | |
| 294 | + let keys = extract_all_bracketed(&keys_str, '(', ')'); | |
| 295 | + for key in keys { | |
| 296 | + let key_inner = key.trim_start_matches('(').trim_end_matches(')'); | |
| 297 | + let mods = extract_modifiers(key_inner, defines); | |
| 298 | + let key_name = extract_key(key_inner); | |
| 299 | + result.push_str(&format!(" {{ modifiers = {{ {} }}, key = \"{}\" }},\n", mods, key_name)); | |
| 300 | + } | |
| 301 | + } | |
| 302 | + | |
| 303 | + result.push_str(" },\n"); | |
| 304 | + | |
| 305 | + if let Some(action) = extract_identifier(inner, "action") { | |
| 306 | + result.push_str(&format!(" action = \"{}\",\n", action)); | |
| 307 | + } | |
| 308 | + | |
| 309 | + if let Some(arg) = extract_arg(inner, defines) { | |
| 310 | + result.push_str(&format!(" arg = {}\n", arg)); | |
| 311 | + } | |
| 312 | + | |
| 313 | + result.push_str(" }"); | |
| 314 | + result | |
| 315 | +} | |
| 316 | + | |
| 317 | +fn convert_single_key(inner: &str, defines: &HashMap<String, String>) -> String { | |
| 318 | + let mods = extract_modifiers(inner, defines); | |
| 319 | + let key = extract_key(inner); | |
| 320 | + let action = extract_identifier(inner, "action").unwrap_or_default(); | |
| 321 | + | |
| 322 | + let mut result = format!(" {{ modifiers = {{ {} }}, key = \"{}\", action = \"{}\"", mods, key, action); | |
| 323 | + | |
| 324 | + if let Some(arg) = extract_arg(inner, defines) { | |
| 325 | + result.push_str(&format!(", arg = {}", arg)); | |
| 326 | + } | |
| 327 | + | |
| 328 | + result.push_str(" }"); | |
| 329 | + result | |
| 330 | +} | |
| 331 | + | |
| 332 | +fn extract_modifiers(content: &str, defines: &HashMap<String, String>) -> String { | |
| 333 | + if let Some(mods_str) = extract_field(content, "modifiers") { | |
| 334 | + let inner = mods_str.trim_start_matches('[').trim_end_matches(']').trim(); | |
| 335 | + if inner.is_empty() { | |
| 336 | + return String::new(); | |
| 337 | + } | |
| 338 | + let mods: Vec<String> = inner | |
| 339 | + .split(',') | |
| 340 | + .map(|s| { | |
| 341 | + let trimmed = s.trim(); | |
| 342 | + if !trimmed.is_empty() { | |
| 343 | + let resolved = resolve_value(trimmed, defines); | |
| 344 | + format!("\"{}\"", resolved) | |
| 345 | + } else { | |
| 346 | + String::new() | |
| 347 | + } | |
| 348 | + }) | |
| 349 | + .filter(|s| !s.is_empty()) | |
| 350 | + .collect(); | |
| 351 | + mods.join(", ") | |
| 352 | + } else { | |
| 353 | + String::new() | |
| 354 | + } | |
| 355 | +} | |
| 356 | + | |
| 357 | +fn extract_key(content: &str) -> String { | |
| 358 | + if let Some(key_str) = extract_identifier(content, "key") { | |
| 359 | + if key_str.starts_with("Key") && key_str.len() == 4 { | |
| 360 | + if let Some(digit) = key_str.chars().nth(3) { | |
| 361 | + if digit.is_ascii_digit() { | |
| 362 | + return digit.to_string(); | |
| 363 | + } | |
| 364 | + } | |
| 365 | + } | |
| 366 | + key_str | |
| 367 | + } else { | |
| 368 | + String::from("Return") | |
| 369 | + } | |
| 370 | +} | |
| 371 | + | |
| 372 | +fn extract_identifier(content: &str, field_name: &str) -> Option<String> { | |
| 373 | + let pattern = format!("{}:", field_name); | |
| 374 | + if let Some(start) = content.find(&pattern) { | |
| 375 | + let after_colon = &content[start + pattern.len()..]; | |
| 376 | + let value_start = after_colon.trim_start(); | |
| 377 | + let end = value_start | |
| 378 | + .find(|c: char| c == ',' || c == ')' || c == '\n') | |
| 379 | + .unwrap_or(value_start.len()); | |
| 380 | + Some(value_start[..end].trim().to_string()) | |
| 381 | + } else { | |
| 382 | + None | |
| 383 | + } | |
| 384 | +} | |
| 385 | + | |
| 386 | +fn extract_arg(content: &str, defines: &HashMap<String, String>) -> Option<String> { | |
| 387 | + if let Some(arg_str) = extract_field(content, "arg") { | |
| 388 | + let resolved = resolve_value(&arg_str, defines); | |
| 389 | + if resolved.starts_with('[') { | |
| 390 | + Some(convert_array_to_lua(&resolved)) | |
| 391 | + } else if resolved.starts_with('"') || resolved.parse::<i32>().is_ok() || resolved.starts_with("0x") { | |
| 392 | + Some(resolved) | |
| 393 | + } else { | |
| 394 | + Some(format!("\"{}\"", resolved)) | |
| 395 | + } | |
| 396 | + } else { | |
| 397 | + None | |
| 398 | + } | |
| 399 | +} | |
| 400 | + | |
| 401 | +fn convert_status_blocks(ron_array: &str, defines: &HashMap<String, String>) -> String { | |
| 402 | + let mut result = String::from("{\n"); | |
| 403 | + let inner = ron_array.trim_start_matches('[').trim_end_matches(']'); | |
| 404 | + | |
| 405 | + let items = extract_all_bracketed(inner, '(', ')'); | |
| 406 | + for item in items { | |
| 407 | + let block = convert_status_block(&item, defines); | |
| 408 | + if !block.trim().ends_with("{\n }") { | |
| 409 | + result.push_str(&block); | |
| 410 | + result.push_str(",\n"); | |
| 411 | + } | |
| 412 | + } | |
| 413 | + | |
| 414 | + result.push_str(" }"); | |
| 415 | + result | |
| 416 | +} | |
| 417 | + | |
| 418 | +fn convert_status_block(ron_block: &str, defines: &HashMap<String, String>) -> String { | |
| 419 | + let mut result = String::from(" {\n"); | |
| 420 | + let inner = ron_block.trim_start_matches('(').trim_end_matches(')'); | |
| 421 | + | |
| 422 | + if let Some(format) = extract_field(inner, "format") { | |
| 423 | + result.push_str(&format!(" format = {},\n", format)); | |
| 424 | + } | |
| 425 | + if let Some(command) = extract_field(inner, "command") { | |
| 426 | + result.push_str(&format!(" command = {},\n", command)); | |
| 427 | + } | |
| 428 | + if let Some(command_arg) = extract_field(inner, "command_arg") { | |
| 429 | + result.push_str(&format!(" command_arg = {},\n", command_arg)); | |
| 430 | + } | |
| 431 | + if inner.contains("battery_formats:") { | |
| 432 | + if let Some(battery_str) = extract_field(inner, "battery_formats") { | |
| 433 | + result.push_str(" battery_formats = {\n"); | |
| 434 | + let battery_inner = battery_str.trim_start_matches('(').trim_end_matches(')'); | |
| 435 | + if let Some(charging) = extract_quoted_value(battery_inner, "charging") { | |
| 436 | + result.push_str(&format!(" charging = \"{}\",\n", charging)); | |
| 437 | + } | |
| 438 | + if let Some(discharging) = extract_quoted_value(battery_inner, "discharging") { | |
| 439 | + result.push_str(&format!(" discharging = \"{}\",\n", discharging)); | |
| 440 | + } | |
| 441 | + if let Some(full) = extract_quoted_value(battery_inner, "full") { | |
| 442 | + result.push_str(&format!(" full = \"{}\"\n", full)); | |
| 443 | + } | |
| 444 | + result.push_str(" },\n"); | |
| 445 | + } | |
| 446 | + } | |
| 447 | + if let Some(interval) = extract_field(inner, "interval_secs") { | |
| 448 | + let interval_val = if interval.len() > 10 { | |
| 449 | + "999999999".to_string() | |
| 450 | + } else { | |
| 451 | + interval | |
| 452 | + }; | |
| 453 | + result.push_str(&format!(" interval_secs = {},\n", interval_val)); | |
| 454 | + } | |
| 455 | + if let Some(color) = extract_field(inner, "color") { | |
| 456 | + let resolved = resolve_color_value(&color, defines); | |
| 457 | + result.push_str(&format!(" color = {},\n", resolved)); | |
| 458 | + } | |
| 459 | + if let Some(underline) = extract_field(inner, "underline") { | |
| 460 | + result.push_str(&format!(" underline = {}\n", underline)); | |
| 461 | + } | |
| 462 | + | |
| 463 | + result.push_str(" }"); | |
| 464 | + result | |
| 465 | +} | |
| 466 | + | |
| 467 | +fn convert_color_scheme(ron_scheme: &str, defines: &HashMap<String, String>) -> String { | |
| 468 | + let mut result = String::from("{\n"); | |
| 469 | + let inner = ron_scheme.trim_start_matches('(').trim_end_matches(')'); | |
| 470 | + | |
| 471 | + if let Some(fg) = extract_field(inner, "foreground") { | |
| 472 | + let resolved = resolve_color_value(&fg, defines); | |
| 473 | + result.push_str(&format!(" foreground = {},\n", resolved)); | |
| 474 | + } | |
| 475 | + if let Some(bg) = extract_field(inner, "background") { | |
| 476 | + let resolved = resolve_color_value(&bg, defines); | |
| 477 | + result.push_str(&format!(" background = {},\n", resolved)); | |
| 478 | + } | |
| 479 | + if let Some(ul) = extract_field(inner, "underline") { | |
| 480 | + let resolved = resolve_color_value(&ul, defines); | |
| 481 | + result.push_str(&format!(" underline = {}\n", resolved)); | |
| 482 | + } | |
| 483 | + | |
| 484 | + result.push_str(" }"); | |
| 485 | + result | |
| 486 | +} | |
| 487 | + | |
| 488 | +fn extract_all_bracketed(s: &str, open: char, close: char) -> Vec<String> { | |
| 489 | + let mut results = Vec::new(); | |
| 490 | + let mut depth = 0; | |
| 491 | + let mut start = None; | |
| 492 | + | |
| 493 | + let cleaned = remove_comments(s); | |
| 494 | + | |
| 495 | + for (i, c) in cleaned.char_indices() { | |
| 496 | + if c == open { | |
| 497 | + if depth == 0 { | |
| 498 | + start = Some(i); | |
| 499 | + } | |
| 500 | + depth += 1; | |
| 501 | + } else if c == close { | |
| 502 | + depth -= 1; | |
| 503 | + if depth == 0 { | |
| 504 | + if let Some(start_idx) = start { | |
| 505 | + results.push(cleaned[start_idx..=i].to_string()); | |
| 506 | + start = None; | |
| 507 | + } | |
| 508 | + } | |
| 509 | + } | |
| 510 | + } | |
| 511 | + | |
| 512 | + results | |
| 513 | +} | |
| 514 | + | |
| 515 | +fn remove_comments(s: &str) -> String { | |
| 516 | + let mut result = String::new(); | |
| 517 | + for line in s.lines() { | |
| 518 | + let mut in_string = false; | |
| 519 | + let mut comment_start = None; | |
| 520 | + | |
| 521 | + for (i, c) in line.char_indices() { | |
| 522 | + if c == '"' && (i == 0 || line.chars().nth(i - 1) != Some('\\')) { | |
| 523 | + in_string = !in_string; | |
| 524 | + } | |
| 525 | + if !in_string && i + 1 < line.len() && &line[i..i + 2] == "//" { | |
| 526 | + comment_start = Some(i); | |
| 527 | + break; | |
| 528 | + } | |
| 529 | + } | |
| 530 | + | |
| 531 | + if let Some(pos) = comment_start { | |
| 532 | + result.push_str(&line[..pos]); | |
| 533 | + } else { | |
| 534 | + result.push_str(line); | |
| 535 | + } | |
| 536 | + result.push('\n'); | |
| 537 | + } | |
| 538 | + result | |
| 539 | +} | |
| 540 | + | |
| 541 | +fn extract_quoted_value(content: &str, field_name: &str) -> Option<String> { | |
| 542 | + let pattern = format!("{}:", field_name); | |
| 543 | + if let Some(start) = content.find(&pattern) { | |
| 544 | + let after_colon = &content[start + pattern.len()..]; | |
| 545 | + let trimmed = after_colon.trim_start(); | |
| 546 | + if trimmed.starts_with('"') { | |
| 547 | + if let Some(end) = trimmed[1..].find('"') { | |
| 548 | + return Some(trimmed[1..end + 1].to_string()); | |
| 549 | + } | |
| 550 | + } | |
| 551 | + } | |
| 552 | + None | |
| 553 | +} |
+5 −0 src/config/mod.rs
| @@ -1,3 +1,6 @@ | ||
| 1 | +mod lua; | |
| 2 | +pub mod migrate; | |
| 3 | + | |
| 1 | 4 | use crate::bar::{BlockCommand, BlockConfig}; |
| 2 | 5 | use crate::errors::ConfigError; |
| 3 | 6 | use crate::keyboard::handlers::{KeyBinding, KeyPress}; |
| @@ -8,6 +11,8 @@ use serde::Deserialize; | ||
| 8 | 11 | use std::collections::HashMap; |
| 9 | 12 | use x11rb::protocol::xproto::KeyButMask; |
| 10 | 13 | |
| 14 | +pub use lua::parse_lua_config; | |
| 15 | + | |
| 11 | 16 | #[derive(Debug, Deserialize)] |
| 12 | 17 | pub enum ModKey { |
| 13 | 18 | Mod, |
+47 −8 src/errors.rs
| @@ -4,8 +4,8 @@ use std::io; | ||
| 4 | 4 | pub enum WmError { |
| 5 | 5 | X11(X11Error), |
| 6 | 6 | Io(io::Error), |
| 7 | - Anyhow(anyhow::Error), | |
| 8 | 7 | Config(ConfigError), |
| 8 | + Block(BlockError), | |
| 9 | 9 | Autostart(String, io::Error), |
| 10 | 10 | } |
| 11 | 11 | |
| @@ -23,6 +23,7 @@ pub enum X11Error { | ||
| 23 | 23 | #[derive(Debug)] |
| 24 | 24 | pub enum ConfigError { |
| 25 | 25 | ParseError(ron::error::SpannedError), |
| 26 | + LuaError(String), | |
| 26 | 27 | InvalidModkey(String), |
| 27 | 28 | UnknownKey(String), |
| 28 | 29 | UnknownAction(String), |
| @@ -32,6 +33,16 @@ pub enum ConfigError { | ||
| 32 | 33 | InvalidVariableName(String), |
| 33 | 34 | InvalidDefine(String), |
| 34 | 35 | UndefinedVariable(String), |
| 36 | + MigrationError(String), | |
| 37 | +} | |
| 38 | + | |
| 39 | +#[derive(Debug)] | |
| 40 | +pub enum BlockError { | |
| 41 | + Io(io::Error), | |
| 42 | + ParseInt(std::num::ParseIntError), | |
| 43 | + MissingFile(String), | |
| 44 | + InvalidData(String), | |
| 45 | + CommandFailed(String), | |
| 35 | 46 | } |
| 36 | 47 | |
| 37 | 48 | impl std::fmt::Display for WmError { |
| @@ -39,8 +50,8 @@ impl std::fmt::Display for WmError { | ||
| 39 | 50 | match self { |
| 40 | 51 | Self::X11(error) => write!(f, "{}", error), |
| 41 | 52 | Self::Io(error) => write!(f, "{}", error), |
| 42 | - Self::Anyhow(error) => write!(f, "{}", error), | |
| 43 | 53 | Self::Config(error) => write!(f, "{}", error), |
| 54 | + Self::Block(error) => write!(f, "{}", error), | |
| 44 | 55 | Self::Autostart(command, error) => write!(f, "Failed to spawn autostart command '{}': {}", command, error), |
| 45 | 56 | } |
| 46 | 57 | } |
| @@ -68,6 +79,7 @@ impl std::fmt::Display for ConfigError { | ||
| 68 | 79 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 69 | 80 | match self { |
| 70 | 81 | Self::ParseError(err) => write!(f, "Failed to parse RON config: {}", err), |
| 82 | + Self::LuaError(msg) => write!(f, "Lua config error: {}", msg), | |
| 71 | 83 | Self::InvalidModkey(key) => write!(f, "Invalid modkey: {}", key), |
| 72 | 84 | Self::UnknownKey(key) => write!(f, "Unknown key: {}", key), |
| 73 | 85 | Self::UnknownAction(action) => write!(f, "Unknown action: {}", action), |
| @@ -93,12 +105,27 @@ impl std::fmt::Display for ConfigError { | ||
| 93 | 105 | var |
| 94 | 106 | ) |
| 95 | 107 | } |
| 108 | + Self::MigrationError(msg) => write!(f, "Migration error: {}", msg), | |
| 96 | 109 | } |
| 97 | 110 | } |
| 98 | 111 | } |
| 99 | 112 | |
| 100 | 113 | impl std::error::Error for ConfigError {} |
| 101 | 114 | |
| 115 | +impl std::fmt::Display for BlockError { | |
| 116 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | |
| 117 | + match self { | |
| 118 | + Self::Io(err) => write!(f, "Block I/O error: {}", err), | |
| 119 | + Self::ParseInt(err) => write!(f, "Block parse error: {}", err), | |
| 120 | + Self::MissingFile(path) => write!(f, "Block missing file: {}", path), | |
| 121 | + Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg), | |
| 122 | + Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg), | |
| 123 | + } | |
| 124 | + } | |
| 125 | +} | |
| 126 | + | |
| 127 | +impl std::error::Error for BlockError {} | |
| 128 | + | |
| 102 | 129 | impl<T: Into<X11Error>> From<T> for WmError { |
| 103 | 130 | fn from(value: T) -> Self { |
| 104 | 131 | Self::X11(value.into()) |
| @@ -111,18 +138,30 @@ impl From<io::Error> for WmError { | ||
| 111 | 138 | } |
| 112 | 139 | } |
| 113 | 140 | |
| 114 | -impl From<anyhow::Error> for WmError { | |
| 115 | - fn from(value: anyhow::Error) -> Self { | |
| 116 | - Self::Anyhow(value) | |
| 117 | - } | |
| 118 | -} | |
| 119 | - | |
| 120 | 141 | impl From<ConfigError> for WmError { |
| 121 | 142 | fn from(value: ConfigError) -> Self { |
| 122 | 143 | Self::Config(value) |
| 123 | 144 | } |
| 124 | 145 | } |
| 125 | 146 | |
| 147 | +impl From<BlockError> for WmError { | |
| 148 | + fn from(value: BlockError) -> Self { | |
| 149 | + Self::Block(value) | |
| 150 | + } | |
| 151 | +} | |
| 152 | + | |
| 153 | +impl From<io::Error> for BlockError { | |
| 154 | + fn from(value: io::Error) -> Self { | |
| 155 | + BlockError::Io(value) | |
| 156 | + } | |
| 157 | +} | |
| 158 | + | |
| 159 | +impl From<std::num::ParseIntError> for BlockError { | |
| 160 | + fn from(value: std::num::ParseIntError) -> Self { | |
| 161 | + BlockError::ParseInt(value) | |
| 162 | + } | |
| 163 | +} | |
| 164 | + | |
| 126 | 165 | impl From<ron::error::SpannedError> for ConfigError { |
| 127 | 166 | fn from(value: ron::error::SpannedError) -> Self { |
| 128 | 167 | ConfigError::ParseError(value) |
+2 −1 src/keyboard/handlers.rs
| @@ -9,7 +9,7 @@ use x11rb::protocol::xproto::*; | ||
| 9 | 9 | use crate::errors::X11Error; |
| 10 | 10 | use crate::keyboard::keysyms::{self, Keysym}; |
| 11 | 11 | |
| 12 | -#[derive(Debug, Copy, Clone, Deserialize)] | |
| 12 | +#[derive(Debug, Copy, Clone, Deserialize, PartialEq)] | |
| 13 | 13 | pub enum KeyAction { |
| 14 | 14 | Spawn, |
| 15 | 15 | KillClient, |
| @@ -29,6 +29,7 @@ pub enum KeyAction { | ||
| 29 | 29 | FocusMonitor, |
| 30 | 30 | SmartMoveWin, |
| 31 | 31 | ExchangeClient, |
| 32 | + ShowKeybindOverlay, | |
| 32 | 33 | None, |
| 33 | 34 | } |
| 34 | 35 |
+58 −0 src/keyboard/keysyms.rs
| @@ -82,3 +82,61 @@ pub const XF86_AUDIO_LOWER_VOLUME: Keysym = 0x1008ff11; | ||
| 82 | 82 | pub const XF86_AUDIO_MUTE: Keysym = 0x1008ff12; |
| 83 | 83 | pub const XF86_MON_BRIGHTNESS_UP: Keysym = 0x1008ff02; |
| 84 | 84 | pub const XF86_MON_BRIGHTNESS_DOWN: Keysym = 0x1008ff03; |
| 85 | + | |
| 86 | +pub fn format_keysym(keysym: Keysym) -> String { | |
| 87 | + match keysym { | |
| 88 | + XK_RETURN => "Return".to_string(), | |
| 89 | + XK_ESCAPE => "Esc".to_string(), | |
| 90 | + XK_SPACE => "Space".to_string(), | |
| 91 | + XK_TAB => "Tab".to_string(), | |
| 92 | + XK_BACKSPACE => "Backspace".to_string(), | |
| 93 | + XK_DELETE => "Del".to_string(), | |
| 94 | + XK_LEFT => "Left".to_string(), | |
| 95 | + XK_RIGHT => "Right".to_string(), | |
| 96 | + XK_UP => "Up".to_string(), | |
| 97 | + XK_DOWN => "Down".to_string(), | |
| 98 | + XK_HOME => "Home".to_string(), | |
| 99 | + XK_END => "End".to_string(), | |
| 100 | + XK_PAGE_UP => "PgUp".to_string(), | |
| 101 | + XK_PAGE_DOWN => "PgDn".to_string(), | |
| 102 | + XK_INSERT => "Ins".to_string(), | |
| 103 | + XK_F1 => "F1".to_string(), | |
| 104 | + XK_F2 => "F2".to_string(), | |
| 105 | + XK_F3 => "F3".to_string(), | |
| 106 | + XK_F4 => "F4".to_string(), | |
| 107 | + XK_F5 => "F5".to_string(), | |
| 108 | + XK_F6 => "F6".to_string(), | |
| 109 | + XK_F7 => "F7".to_string(), | |
| 110 | + XK_F8 => "F8".to_string(), | |
| 111 | + XK_F9 => "F9".to_string(), | |
| 112 | + XK_F10 => "F10".to_string(), | |
| 113 | + XK_F11 => "F11".to_string(), | |
| 114 | + XK_F12 => "F12".to_string(), | |
| 115 | + XK_SLASH => "/".to_string(), | |
| 116 | + XK_COMMA => ",".to_string(), | |
| 117 | + XK_PERIOD => ".".to_string(), | |
| 118 | + XK_MINUS => "-".to_string(), | |
| 119 | + XK_EQUAL => "=".to_string(), | |
| 120 | + XK_GRAVE => "`".to_string(), | |
| 121 | + XK_LEFT_BRACKET => "[".to_string(), | |
| 122 | + XK_RIGHT_BRACKET => "]".to_string(), | |
| 123 | + XK_SEMICOLON => ";".to_string(), | |
| 124 | + XK_APOSTROPHE => "'".to_string(), | |
| 125 | + XK_BACKSLASH => "\\".to_string(), | |
| 126 | + XK_PRINT => "Print".to_string(), | |
| 127 | + XF86_AUDIO_RAISE_VOLUME => "Vol+".to_string(), | |
| 128 | + XF86_AUDIO_LOWER_VOLUME => "Vol-".to_string(), | |
| 129 | + XF86_AUDIO_MUTE => "Mute".to_string(), | |
| 130 | + XF86_MON_BRIGHTNESS_UP => "Bright+".to_string(), | |
| 131 | + XF86_MON_BRIGHTNESS_DOWN => "Bright-".to_string(), | |
| 132 | + XK_A..=XK_Z => { | |
| 133 | + let ch = (keysym - XK_A + b'A' as u32) as u8 as char; | |
| 134 | + ch.to_string() | |
| 135 | + } | |
| 136 | + XK_0..=XK_9 => { | |
| 137 | + let ch = (keysym - XK_0 + b'0' as u32) as u8 as char; | |
| 138 | + ch.to_string() | |
| 139 | + } | |
| 140 | + _ => format!("0x{:x}", keysym), | |
| 141 | + } | |
| 142 | +} |
+1 −0 src/lib.rs
| @@ -4,6 +4,7 @@ pub mod errors; | ||
| 4 | 4 | pub mod keyboard; |
| 5 | 5 | pub mod layout; |
| 6 | 6 | pub mod monitor; |
| 7 | +pub mod overlay; | |
| 7 | 8 | pub mod window_manager; |
| 8 | 9 | |
| 9 | 10 | pub mod prelude { |
+157 −0 src/overlay/error.rs new file mode 100644
| @@ -0,0 +1,157 @@ | ||
| 1 | +use super::{Overlay, OverlayBase}; | |
| 2 | +use crate::bar::font::Font; | |
| 3 | +use crate::errors::X11Error; | |
| 4 | +use std::time::Instant; | |
| 5 | +use x11rb::connection::Connection; | |
| 6 | +use x11rb::protocol::xproto::*; | |
| 7 | +use x11rb::rust_connection::RustConnection; | |
| 8 | + | |
| 9 | +const PADDING: i16 = 20; | |
| 10 | +const LINE_SPACING: i16 = 5; | |
| 11 | +const BORDER_WIDTH: u16 = 2; | |
| 12 | +const BORDER_COLOR: u32 = 0xff5555; | |
| 13 | +const AUTO_DISMISS_SECONDS: u64 = 10; | |
| 14 | + | |
| 15 | +pub struct ErrorOverlay { | |
| 16 | + base: OverlayBase, | |
| 17 | + created_at: Option<Instant>, | |
| 18 | + lines: Vec<String>, | |
| 19 | +} | |
| 20 | + | |
| 21 | +impl ErrorOverlay { | |
| 22 | + pub fn new( | |
| 23 | + connection: &RustConnection, | |
| 24 | + screen: &Screen, | |
| 25 | + screen_num: usize, | |
| 26 | + display: *mut x11::xlib::Display, | |
| 27 | + _font: &Font, | |
| 28 | + _max_width: u16, | |
| 29 | + ) -> Result<Self, X11Error> { | |
| 30 | + let base = OverlayBase::new( | |
| 31 | + connection, | |
| 32 | + screen, | |
| 33 | + screen_num, | |
| 34 | + display, | |
| 35 | + 400, | |
| 36 | + 200, | |
| 37 | + BORDER_WIDTH, | |
| 38 | + BORDER_COLOR, | |
| 39 | + 0x1a1a1a, | |
| 40 | + 0xffffff, | |
| 41 | + )?; | |
| 42 | + | |
| 43 | + Ok(ErrorOverlay { | |
| 44 | + base, | |
| 45 | + created_at: None, | |
| 46 | + lines: Vec::new(), | |
| 47 | + }) | |
| 48 | + } | |
| 49 | + | |
| 50 | + pub fn show_error( | |
| 51 | + &mut self, | |
| 52 | + connection: &RustConnection, | |
| 53 | + font: &Font, | |
| 54 | + error_text: &str, | |
| 55 | + screen_width: u16, | |
| 56 | + screen_height: u16, | |
| 57 | + ) -> Result<(), X11Error> { | |
| 58 | + let max_line_width = (screen_width as i16 - PADDING * 4).max(300) as u16; | |
| 59 | + self.lines = self.wrap_text(error_text, font, max_line_width); | |
| 60 | + | |
| 61 | + let mut content_width = 0u16; | |
| 62 | + for line in &self.lines { | |
| 63 | + let line_width = font.text_width(line); | |
| 64 | + if line_width > content_width { | |
| 65 | + content_width = line_width; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + let width = content_width + (PADDING as u16 * 2); | |
| 70 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 71 | + let height = (self.lines.len() as u16 * line_height) + (PADDING as u16 * 2); | |
| 72 | + | |
| 73 | + let x = ((screen_width - width) / 2) as i16; | |
| 74 | + let y = ((screen_height - height) / 2) as i16; | |
| 75 | + | |
| 76 | + self.base.configure(connection, x, y, width, height)?; | |
| 77 | + self.base.show(connection)?; | |
| 78 | + self.created_at = Some(Instant::now()); | |
| 79 | + self.draw(connection, font)?; | |
| 80 | + Ok(()) | |
| 81 | + } | |
| 82 | + | |
| 83 | + pub fn should_auto_dismiss(&self) -> bool { | |
| 84 | + if let Some(created_at) = self.created_at { | |
| 85 | + created_at.elapsed().as_secs() >= AUTO_DISMISS_SECONDS | |
| 86 | + } else { | |
| 87 | + false | |
| 88 | + } | |
| 89 | + } | |
| 90 | + | |
| 91 | + fn wrap_text(&self, text: &str, font: &Font, max_width: u16) -> Vec<String> { | |
| 92 | + let mut lines = Vec::new(); | |
| 93 | + for paragraph in text.lines() { | |
| 94 | + if paragraph.trim().is_empty() { | |
| 95 | + lines.push(String::new()); | |
| 96 | + continue; | |
| 97 | + } | |
| 98 | + | |
| 99 | + let words: Vec<&str> = paragraph.split_whitespace().collect(); | |
| 100 | + let mut current_line = String::new(); | |
| 101 | + | |
| 102 | + for word in words { | |
| 103 | + let test_line = if current_line.is_empty() { | |
| 104 | + word.to_string() | |
| 105 | + } else { | |
| 106 | + format!("{} {}", current_line, word) | |
| 107 | + }; | |
| 108 | + if font.text_width(&test_line) <= max_width { | |
| 109 | + current_line = test_line; | |
| 110 | + } else { | |
| 111 | + if !current_line.is_empty() { | |
| 112 | + lines.push(current_line); | |
| 113 | + } | |
| 114 | + current_line = word.to_string(); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + if !current_line.is_empty() { | |
| 118 | + lines.push(current_line); | |
| 119 | + } | |
| 120 | + } | |
| 121 | + lines | |
| 122 | + } | |
| 123 | +} | |
| 124 | + | |
| 125 | +impl Overlay for ErrorOverlay { | |
| 126 | + fn window(&self) -> Window { | |
| 127 | + self.base.window | |
| 128 | + } | |
| 129 | + | |
| 130 | + fn is_visible(&self) -> bool { | |
| 131 | + self.base.is_visible | |
| 132 | + } | |
| 133 | + | |
| 134 | + fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 135 | + self.base.hide(connection)?; | |
| 136 | + self.created_at = None; | |
| 137 | + self.lines.clear(); | |
| 138 | + Ok(()) | |
| 139 | + } | |
| 140 | + | |
| 141 | + fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> { | |
| 142 | + if !self.base.is_visible { | |
| 143 | + return Ok(()); | |
| 144 | + } | |
| 145 | + self.base.draw_background(connection)?; | |
| 146 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 147 | + let mut y = PADDING + font.ascent(); | |
| 148 | + for line in &self.lines { | |
| 149 | + self.base | |
| 150 | + .font_draw | |
| 151 | + .draw_text(font, self.base.foreground_color, PADDING, y, line); | |
| 152 | + y += line_height as i16; | |
| 153 | + } | |
| 154 | + connection.flush()?; | |
| 155 | + Ok(()) | |
| 156 | + } | |
| 157 | +} |
+296 −0 src/overlay/keybind.rs new file mode 100644
| @@ -0,0 +1,296 @@ | ||
| 1 | +use super::{Overlay, OverlayBase}; | |
| 2 | +use crate::bar::font::Font; | |
| 3 | +use crate::errors::X11Error; | |
| 4 | +use crate::keyboard::KeyAction; | |
| 5 | +use crate::keyboard::handlers::{KeyBinding, KeyPress}; | |
| 6 | +use std::time::Instant; | |
| 7 | +use x11rb::connection::Connection; | |
| 8 | +use x11rb::protocol::xproto::*; | |
| 9 | +use x11rb::rust_connection::RustConnection; | |
| 10 | + | |
| 11 | +const PADDING: i16 = 24; | |
| 12 | +const KEY_ACTION_SPACING: i16 = 20; | |
| 13 | +const LINE_SPACING: i16 = 8; | |
| 14 | +const BORDER_WIDTH: u16 = 4; | |
| 15 | +const BORDER_COLOR: u32 = 0x7fccff; | |
| 16 | +const TITLE_BOTTOM_MARGIN: i16 = 20; | |
| 17 | +const INPUT_SUPPRESS_MS: u128 = 200; | |
| 18 | + | |
| 19 | +pub struct KeybindOverlay { | |
| 20 | + base: OverlayBase, | |
| 21 | + keybindings: Vec<(String, String)>, | |
| 22 | + key_bg_color: u32, | |
| 23 | + modkey: KeyButMask, | |
| 24 | + last_shown_at: Option<Instant>, | |
| 25 | + max_key_width: u16, | |
| 26 | +} | |
| 27 | + | |
| 28 | +impl KeybindOverlay { | |
| 29 | + pub fn new( | |
| 30 | + connection: &RustConnection, | |
| 31 | + screen: &Screen, | |
| 32 | + screen_num: usize, | |
| 33 | + display: *mut x11::xlib::Display, | |
| 34 | + modkey: KeyButMask, | |
| 35 | + ) -> Result<Self, X11Error> { | |
| 36 | + let base = OverlayBase::new( | |
| 37 | + connection, | |
| 38 | + screen, | |
| 39 | + screen_num, | |
| 40 | + display, | |
| 41 | + 800, | |
| 42 | + 600, | |
| 43 | + BORDER_WIDTH, | |
| 44 | + BORDER_COLOR, | |
| 45 | + 0x1a1a1a, | |
| 46 | + 0xffffff, | |
| 47 | + )?; | |
| 48 | + | |
| 49 | + Ok(KeybindOverlay { | |
| 50 | + base, | |
| 51 | + keybindings: Vec::new(), | |
| 52 | + key_bg_color: 0x2a2a2a, | |
| 53 | + modkey, | |
| 54 | + last_shown_at: None, | |
| 55 | + max_key_width: 0, | |
| 56 | + }) | |
| 57 | + } | |
| 58 | + | |
| 59 | + pub fn show( | |
| 60 | + &mut self, | |
| 61 | + connection: &RustConnection, | |
| 62 | + font: &Font, | |
| 63 | + keybindings: &[KeyBinding], | |
| 64 | + screen_width: u16, | |
| 65 | + screen_height: u16, | |
| 66 | + ) -> Result<(), X11Error> { | |
| 67 | + self.keybindings = self.collect_keybindings(keybindings); | |
| 68 | + | |
| 69 | + let title = "Important Keybindings"; | |
| 70 | + let title_width = font.text_width(title); | |
| 71 | + | |
| 72 | + let mut max_key_width = 0u16; | |
| 73 | + let mut max_action_width = 0u16; | |
| 74 | + | |
| 75 | + for (key, action) in &self.keybindings { | |
| 76 | + let key_width = font.text_width(key); | |
| 77 | + let action_width = font.text_width(action); | |
| 78 | + if key_width > max_key_width { | |
| 79 | + max_key_width = key_width; | |
| 80 | + } | |
| 81 | + if action_width > max_action_width { | |
| 82 | + max_action_width = action_width; | |
| 83 | + } | |
| 84 | + } | |
| 85 | + | |
| 86 | + let content_width = max_key_width + KEY_ACTION_SPACING as u16 + max_action_width; | |
| 87 | + let min_width = title_width.max(content_width); | |
| 88 | + | |
| 89 | + let width = min_width + (PADDING as u16 * 2); | |
| 90 | + | |
| 91 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 92 | + let title_height = font.height() + TITLE_BOTTOM_MARGIN as u16; | |
| 93 | + let height = | |
| 94 | + title_height + (self.keybindings.len() as u16 * line_height) + (PADDING as u16 * 2); | |
| 95 | + | |
| 96 | + let x = ((screen_width - width) / 2) as i16; | |
| 97 | + let y = ((screen_height - height) / 2) as i16; | |
| 98 | + | |
| 99 | + self.base.configure(connection, x, y, width, height)?; | |
| 100 | + | |
| 101 | + self.last_shown_at = Some(Instant::now()); | |
| 102 | + self.max_key_width = max_key_width; | |
| 103 | + | |
| 104 | + self.base.show(connection)?; | |
| 105 | + | |
| 106 | + self.draw(connection, font)?; | |
| 107 | + | |
| 108 | + Ok(()) | |
| 109 | + } | |
| 110 | + | |
| 111 | + pub fn toggle( | |
| 112 | + &mut self, | |
| 113 | + connection: &RustConnection, | |
| 114 | + font: &Font, | |
| 115 | + keybindings: &[KeyBinding], | |
| 116 | + screen_width: u16, | |
| 117 | + screen_height: u16, | |
| 118 | + ) -> Result<(), X11Error> { | |
| 119 | + if self.base.is_visible { | |
| 120 | + self.hide(connection)?; | |
| 121 | + } else { | |
| 122 | + self.show(connection, font, keybindings, screen_width, screen_height)?; | |
| 123 | + } | |
| 124 | + Ok(()) | |
| 125 | + } | |
| 126 | + | |
| 127 | + pub fn should_suppress_input(&self) -> bool { | |
| 128 | + if let Some(shown_at) = self.last_shown_at { | |
| 129 | + shown_at.elapsed().as_millis() < INPUT_SUPPRESS_MS | |
| 130 | + } else { | |
| 131 | + false | |
| 132 | + } | |
| 133 | + } | |
| 134 | + | |
| 135 | + fn collect_keybindings(&self, keybindings: &[KeyBinding]) -> Vec<(String, String)> { | |
| 136 | + let mut result = Vec::new(); | |
| 137 | + | |
| 138 | + let priority_actions = [ | |
| 139 | + KeyAction::ShowKeybindOverlay, | |
| 140 | + KeyAction::Quit, | |
| 141 | + KeyAction::Restart, | |
| 142 | + KeyAction::KillClient, | |
| 143 | + KeyAction::Spawn, | |
| 144 | + KeyAction::ToggleFullScreen, | |
| 145 | + KeyAction::ToggleFloating, | |
| 146 | + KeyAction::CycleLayout, | |
| 147 | + KeyAction::FocusStack, | |
| 148 | + KeyAction::ViewTag, | |
| 149 | + ]; | |
| 150 | + | |
| 151 | + for &action in &priority_actions { | |
| 152 | + let binding = keybindings | |
| 153 | + .iter() | |
| 154 | + .filter(|kb| kb.func == action) | |
| 155 | + .min_by_key(|kb| kb.keys.len()); | |
| 156 | + | |
| 157 | + if let Some(binding) = binding { | |
| 158 | + if !binding.keys.is_empty() { | |
| 159 | + let key_str = self.format_key_combo(&binding.keys[0]); | |
| 160 | + let action_str = self.action_description(binding); | |
| 161 | + result.push((key_str, action_str)); | |
| 162 | + } | |
| 163 | + } | |
| 164 | + } | |
| 165 | + | |
| 166 | + result | |
| 167 | + } | |
| 168 | + | |
| 169 | + fn format_key_combo(&self, key: &KeyPress) -> String { | |
| 170 | + let mut parts = Vec::new(); | |
| 171 | + | |
| 172 | + for modifier in &key.modifiers { | |
| 173 | + let mod_str = match *modifier { | |
| 174 | + m if m == self.modkey => "Mod", | |
| 175 | + KeyButMask::SHIFT => "Shift", | |
| 176 | + KeyButMask::CONTROL => "Ctrl", | |
| 177 | + KeyButMask::MOD1 => "Alt", | |
| 178 | + KeyButMask::MOD4 => "Super", | |
| 179 | + _ => continue, | |
| 180 | + }; | |
| 181 | + parts.push(mod_str.to_string()); | |
| 182 | + } | |
| 183 | + | |
| 184 | + parts.push(crate::keyboard::keysyms::format_keysym(key.keysym)); | |
| 185 | + | |
| 186 | + parts.join(" + ") | |
| 187 | + } | |
| 188 | + | |
| 189 | + fn action_description(&self, binding: &KeyBinding) -> String { | |
| 190 | + use crate::keyboard::Arg; | |
| 191 | + | |
| 192 | + match binding.func { | |
| 193 | + KeyAction::ShowKeybindOverlay => "Show This Keybind Help".to_string(), | |
| 194 | + KeyAction::Quit => "Quit Window Manager".to_string(), | |
| 195 | + KeyAction::Restart => "Restart Window Manager".to_string(), | |
| 196 | + KeyAction::Recompile => "Recompile Window Manager".to_string(), | |
| 197 | + KeyAction::KillClient => "Close Focused Window".to_string(), | |
| 198 | + KeyAction::Spawn => match &binding.arg { | |
| 199 | + Arg::Str(cmd) => format!("Launch: {}", cmd), | |
| 200 | + Arg::Array(arr) if !arr.is_empty() => format!("Launch: {}", arr[0]), | |
| 201 | + _ => "Launch Program".to_string(), | |
| 202 | + }, | |
| 203 | + KeyAction::FocusStack => "Focus Next/Previous Window".to_string(), | |
| 204 | + KeyAction::FocusDirection => "Focus Window in Direction".to_string(), | |
| 205 | + KeyAction::SwapDirection => "Swap Window in Direction".to_string(), | |
| 206 | + KeyAction::ViewTag => match &binding.arg { | |
| 207 | + Arg::Int(n) => format!("View Workspace {}", n), | |
| 208 | + _ => "View Workspace".to_string(), | |
| 209 | + }, | |
| 210 | + KeyAction::MoveToTag => "Move Window to Workspace".to_string(), | |
| 211 | + KeyAction::ToggleGaps => "Toggle Window Gaps".to_string(), | |
| 212 | + KeyAction::ToggleFullScreen => "Toggle Fullscreen".to_string(), | |
| 213 | + KeyAction::ToggleFloating => "Toggle Floating Mode".to_string(), | |
| 214 | + KeyAction::ChangeLayout => "Change Layout".to_string(), | |
| 215 | + KeyAction::CycleLayout => "Cycle Through Layouts".to_string(), | |
| 216 | + KeyAction::FocusMonitor => "Focus Next Monitor".to_string(), | |
| 217 | + KeyAction::SmartMoveWin => "Smart Move Window".to_string(), | |
| 218 | + KeyAction::ExchangeClient => "Exchange Client Windows".to_string(), | |
| 219 | + KeyAction::None => "No Action".to_string(), | |
| 220 | + } | |
| 221 | + } | |
| 222 | +} | |
| 223 | + | |
| 224 | +impl Overlay for KeybindOverlay { | |
| 225 | + fn window(&self) -> Window { | |
| 226 | + self.base.window | |
| 227 | + } | |
| 228 | + | |
| 229 | + fn is_visible(&self) -> bool { | |
| 230 | + self.base.is_visible | |
| 231 | + } | |
| 232 | + | |
| 233 | + fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 234 | + self.base.hide(connection)?; | |
| 235 | + self.last_shown_at = None; | |
| 236 | + self.keybindings.clear(); | |
| 237 | + Ok(()) | |
| 238 | + } | |
| 239 | + | |
| 240 | + fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> { | |
| 241 | + if !self.base.is_visible { | |
| 242 | + return Ok(()); | |
| 243 | + } | |
| 244 | + | |
| 245 | + self.base.draw_background(connection)?; | |
| 246 | + | |
| 247 | + let title = "Important Keybindings"; | |
| 248 | + let title_width = font.text_width(title); | |
| 249 | + let title_x = ((self.base.width - title_width) / 2) as i16; | |
| 250 | + let title_y = PADDING + font.ascent(); | |
| 251 | + | |
| 252 | + self.base | |
| 253 | + .font_draw | |
| 254 | + .draw_text(font, self.base.foreground_color, title_x, title_y, title); | |
| 255 | + | |
| 256 | + let line_height = font.height() + LINE_SPACING as u16; | |
| 257 | + let mut y = PADDING + font.height() as i16 + TITLE_BOTTOM_MARGIN + font.ascent(); | |
| 258 | + | |
| 259 | + for (key, action) in &self.keybindings { | |
| 260 | + let key_width = font.text_width(key); | |
| 261 | + let key_x = PADDING; | |
| 262 | + | |
| 263 | + connection.change_gc( | |
| 264 | + self.base.graphics_context, | |
| 265 | + &ChangeGCAux::new().foreground(self.key_bg_color), | |
| 266 | + )?; | |
| 267 | + connection.poly_fill_rectangle( | |
| 268 | + self.base.window, | |
| 269 | + self.base.graphics_context, | |
| 270 | + &[Rectangle { | |
| 271 | + x: key_x - 4, | |
| 272 | + y: y - font.ascent() - 2, | |
| 273 | + width: key_width + 8, | |
| 274 | + height: font.height() + 4, | |
| 275 | + }], | |
| 276 | + )?; | |
| 277 | + | |
| 278 | + self.base | |
| 279 | + .font_draw | |
| 280 | + .draw_text(font, self.base.foreground_color, key_x, y, key); | |
| 281 | + | |
| 282 | + let action_x = PADDING + self.max_key_width as i16 + KEY_ACTION_SPACING; | |
| 283 | + self.base | |
| 284 | + .font_draw | |
| 285 | + .draw_text(font, self.base.foreground_color, action_x, y, action); | |
| 286 | + | |
| 287 | + y += line_height as i16; | |
| 288 | + } | |
| 289 | + | |
| 290 | + self.base.font_draw.flush(); | |
| 291 | + | |
| 292 | + connection.flush()?; | |
| 293 | + | |
| 294 | + Ok(()) | |
| 295 | + } | |
| 296 | +} |
+156 −0 src/overlay/mod.rs new file mode 100644
| @@ -0,0 +1,156 @@ | ||
| 1 | +use crate::bar::font::{Font, FontDraw}; | |
| 2 | +use crate::errors::X11Error; | |
| 3 | +use x11rb::COPY_DEPTH_FROM_PARENT; | |
| 4 | +use x11rb::connection::Connection; | |
| 5 | +use x11rb::protocol::xproto::*; | |
| 6 | +use x11rb::rust_connection::RustConnection; | |
| 7 | + | |
| 8 | +pub mod error; | |
| 9 | +pub mod keybind; | |
| 10 | + | |
| 11 | +pub use error::ErrorOverlay; | |
| 12 | +pub use keybind::KeybindOverlay; | |
| 13 | + | |
| 14 | +pub trait Overlay { | |
| 15 | + fn window(&self) -> Window; | |
| 16 | + fn is_visible(&self) -> bool; | |
| 17 | + fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error>; | |
| 18 | + fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error>; | |
| 19 | +} | |
| 20 | + | |
| 21 | +pub struct OverlayBase { | |
| 22 | + pub window: Window, | |
| 23 | + pub width: u16, | |
| 24 | + pub height: u16, | |
| 25 | + pub graphics_context: Gcontext, | |
| 26 | + pub font_draw: FontDraw, | |
| 27 | + pub is_visible: bool, | |
| 28 | + pub background_color: u32, | |
| 29 | + pub foreground_color: u32, | |
| 30 | +} | |
| 31 | + | |
| 32 | +impl OverlayBase { | |
| 33 | + pub fn new( | |
| 34 | + connection: &RustConnection, | |
| 35 | + screen: &Screen, | |
| 36 | + screen_num: usize, | |
| 37 | + display: *mut x11::xlib::Display, | |
| 38 | + width: u16, | |
| 39 | + height: u16, | |
| 40 | + border_width: u16, | |
| 41 | + border_color: u32, | |
| 42 | + background_color: u32, | |
| 43 | + foreground_color: u32, | |
| 44 | + ) -> Result<Self, X11Error> { | |
| 45 | + let window = connection.generate_id()?; | |
| 46 | + let graphics_context = connection.generate_id()?; | |
| 47 | + | |
| 48 | + connection.create_window( | |
| 49 | + COPY_DEPTH_FROM_PARENT, | |
| 50 | + window, | |
| 51 | + screen.root, | |
| 52 | + 0, | |
| 53 | + 0, | |
| 54 | + width, | |
| 55 | + height, | |
| 56 | + border_width, | |
| 57 | + WindowClass::INPUT_OUTPUT, | |
| 58 | + screen.root_visual, | |
| 59 | + &CreateWindowAux::new() | |
| 60 | + .background_pixel(background_color) | |
| 61 | + .border_pixel(border_color) | |
| 62 | + .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS | EventMask::KEY_PRESS) | |
| 63 | + .override_redirect(1), | |
| 64 | + )?; | |
| 65 | + | |
| 66 | + connection.create_gc( | |
| 67 | + graphics_context, | |
| 68 | + window, | |
| 69 | + &CreateGCAux::new() | |
| 70 | + .foreground(foreground_color) | |
| 71 | + .background(background_color), | |
| 72 | + )?; | |
| 73 | + | |
| 74 | + connection.flush()?; | |
| 75 | + | |
| 76 | + let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; | |
| 77 | + let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; | |
| 78 | + | |
| 79 | + let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?; | |
| 80 | + | |
| 81 | + Ok(OverlayBase { | |
| 82 | + window, | |
| 83 | + width, | |
| 84 | + height, | |
| 85 | + graphics_context, | |
| 86 | + font_draw, | |
| 87 | + is_visible: false, | |
| 88 | + background_color, | |
| 89 | + foreground_color, | |
| 90 | + }) | |
| 91 | + } | |
| 92 | + | |
| 93 | + pub fn configure( | |
| 94 | + &mut self, | |
| 95 | + connection: &RustConnection, | |
| 96 | + x: i16, | |
| 97 | + y: i16, | |
| 98 | + width: u16, | |
| 99 | + height: u16, | |
| 100 | + ) -> Result<(), X11Error> { | |
| 101 | + self.width = width; | |
| 102 | + self.height = height; | |
| 103 | + | |
| 104 | + connection.configure_window( | |
| 105 | + self.window, | |
| 106 | + &ConfigureWindowAux::new() | |
| 107 | + .x(x as i32) | |
| 108 | + .y(y as i32) | |
| 109 | + .width(width as u32) | |
| 110 | + .height(height as u32), | |
| 111 | + )?; | |
| 112 | + | |
| 113 | + Ok(()) | |
| 114 | + } | |
| 115 | + | |
| 116 | + pub fn show(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 117 | + connection.configure_window( | |
| 118 | + self.window, | |
| 119 | + &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | |
| 120 | + )?; | |
| 121 | + | |
| 122 | + connection.map_window(self.window)?; | |
| 123 | + connection.flush()?; | |
| 124 | + | |
| 125 | + self.is_visible = true; | |
| 126 | + | |
| 127 | + Ok(()) | |
| 128 | + } | |
| 129 | + | |
| 130 | + pub fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 131 | + if self.is_visible { | |
| 132 | + connection.unmap_window(self.window)?; | |
| 133 | + connection.flush()?; | |
| 134 | + self.is_visible = false; | |
| 135 | + } | |
| 136 | + Ok(()) | |
| 137 | + } | |
| 138 | + | |
| 139 | + pub fn draw_background(&self, connection: &RustConnection) -> Result<(), X11Error> { | |
| 140 | + connection.change_gc( | |
| 141 | + self.graphics_context, | |
| 142 | + &ChangeGCAux::new().foreground(self.background_color), | |
| 143 | + )?; | |
| 144 | + connection.poly_fill_rectangle( | |
| 145 | + self.window, | |
| 146 | + self.graphics_context, | |
| 147 | + &[Rectangle { | |
| 148 | + x: 0, | |
| 149 | + y: 0, | |
| 150 | + width: self.width, | |
| 151 | + height: self.height, | |
| 152 | + }], | |
| 153 | + )?; | |
| 154 | + Ok(()) | |
| 155 | + } | |
| 156 | +} |
+151 −56 src/window_manager.rs
| @@ -6,6 +6,7 @@ use crate::layout::GapConfig; | ||
| 6 | 6 | use crate::layout::tiling::TilingLayout; |
| 7 | 7 | use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}; |
| 8 | 8 | use crate::monitor::{Monitor, detect_monitors}; |
| 9 | +use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; | |
| 9 | 10 | use std::collections::HashSet; |
| 10 | 11 | use std::process::Command; |
| 11 | 12 | use x11rb::cursor::Handle as CursorHandle; |
| @@ -69,6 +70,9 @@ pub struct WindowManager { | ||
| 69 | 70 | display: *mut x11::xlib::Display, |
| 70 | 71 | font: crate::bar::font::Font, |
| 71 | 72 | keychord_state: keyboard::handlers::KeychordState, |
| 73 | + error_message: Option<String>, | |
| 74 | + overlay: ErrorOverlay, | |
| 75 | + keybind_overlay: KeybindOverlay, | |
| 72 | 76 | } |
| 73 | 77 | |
| 74 | 78 | type WmResult<T> = Result<T, WmError>; |
| @@ -156,6 +160,18 @@ impl WindowManager { | ||
| 156 | 160 | |
| 157 | 161 | let atoms = AtomCache::new(&connection)?; |
| 158 | 162 | |
| 163 | + let overlay = ErrorOverlay::new( | |
| 164 | + &connection, | |
| 165 | + &screen, | |
| 166 | + screen_number, | |
| 167 | + display, | |
| 168 | + &font, | |
| 169 | + screen.width_in_pixels, | |
| 170 | + )?; | |
| 171 | + | |
| 172 | + let keybind_overlay = | |
| 173 | + KeybindOverlay::new(&connection, &screen, screen_number, display, config.modkey)?; | |
| 174 | + | |
| 159 | 175 | let mut window_manager = Self { |
| 160 | 176 | config, |
| 161 | 177 | connection, |
| @@ -177,6 +193,9 @@ impl WindowManager { | ||
| 177 | 193 | display, |
| 178 | 194 | font, |
| 179 | 195 | keychord_state: keyboard::handlers::KeychordState::Idle, |
| 196 | + error_message: None, | |
| 197 | + overlay, | |
| 198 | + keybind_overlay, | |
| 180 | 199 | }; |
| 181 | 200 | |
| 182 | 201 | window_manager.scan_existing_windows()?; |
| @@ -225,6 +244,48 @@ impl WindowManager { | ||
| 225 | 244 | Ok(tag_mask(0)) |
| 226 | 245 | } |
| 227 | 246 | |
| 247 | + fn try_reload_config(&mut self) -> Result<(), String> { | |
| 248 | + let config_dir = if let Some(xdg_config) = std::env::var_os("XDG_CONFIG_HOME") { | |
| 249 | + std::path::PathBuf::from(xdg_config).join("oxwm") | |
| 250 | + } else if let Some(home) = std::env::var_os("HOME") { | |
| 251 | + std::path::PathBuf::from(home).join(".config").join("oxwm") | |
| 252 | + } else { | |
| 253 | + return Err("Could not find config directory".to_string()); | |
| 254 | + }; | |
| 255 | + | |
| 256 | + let lua_path = config_dir.join("config.lua"); | |
| 257 | + let ron_path = config_dir.join("config.ron"); | |
| 258 | + | |
| 259 | + let config_path = if lua_path.exists() { | |
| 260 | + lua_path | |
| 261 | + } else if ron_path.exists() { | |
| 262 | + ron_path | |
| 263 | + } else { | |
| 264 | + return Err("No config file found".to_string()); | |
| 265 | + }; | |
| 266 | + | |
| 267 | + let config_str = std::fs::read_to_string(&config_path) | |
| 268 | + .map_err(|e| format!("Failed to read config: {}", e))?; | |
| 269 | + | |
| 270 | + let is_lua = config_path | |
| 271 | + .extension() | |
| 272 | + .and_then(|s| s.to_str()) | |
| 273 | + .map(|s| s == "lua") | |
| 274 | + .unwrap_or(false); | |
| 275 | + | |
| 276 | + let new_config = if is_lua { | |
| 277 | + let config_dir = config_path.parent(); | |
| 278 | + crate::config::parse_lua_config(&config_str, config_dir) | |
| 279 | + .map_err(|e| format!("Config error: {}", e))? | |
| 280 | + } else { | |
| 281 | + crate::config::parse_config(&config_str).map_err(|e| format!("Config error: {}", e))? | |
| 282 | + }; | |
| 283 | + | |
| 284 | + self.config = new_config; | |
| 285 | + self.error_message = None; | |
| 286 | + Ok(()) | |
| 287 | + } | |
| 288 | + | |
| 228 | 289 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 229 | 290 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 230 | 291 | let net_client_info = self.atoms.net_client_info; |
| @@ -377,6 +438,10 @@ impl WindowManager { | ||
| 377 | 438 | self.update_bar()?; |
| 378 | 439 | } |
| 379 | 440 | |
| 441 | + if self.overlay.is_visible() && self.overlay.should_auto_dismiss() { | |
| 442 | + let _ = self.overlay.hide(&self.connection); | |
| 443 | + } | |
| 444 | + | |
| 380 | 445 | std::thread::sleep(std::time::Duration::from_millis(100)); |
| 381 | 446 | } |
| 382 | 447 | } |
| @@ -808,7 +873,7 @@ impl WindowManager { | ||
| 808 | 873 | indicator.push('+'); |
| 809 | 874 | } |
| 810 | 875 | |
| 811 | - indicator.push_str(&self.format_keysym(key_press.keysym)); | |
| 876 | + indicator.push_str(&keyboard::keysyms::format_keysym(key_press.keysym)); | |
| 812 | 877 | } |
| 813 | 878 | |
| 814 | 879 | indicator.push('-'); |
| @@ -827,60 +892,6 @@ impl WindowManager { | ||
| 827 | 892 | } |
| 828 | 893 | } |
| 829 | 894 | |
| 830 | - fn format_keysym(&self, keysym: keyboard::keysyms::Keysym) -> String { | |
| 831 | - use keyboard::keysyms::*; | |
| 832 | - | |
| 833 | - match keysym { | |
| 834 | - XK_RETURN => "Return", | |
| 835 | - XK_ESCAPE => "Esc", | |
| 836 | - XK_SPACE => "Space", | |
| 837 | - XK_TAB => "Tab", | |
| 838 | - XK_BACKSPACE => "Backspace", | |
| 839 | - XK_DELETE => "Del", | |
| 840 | - XK_F1 => "F1", | |
| 841 | - XK_F2 => "F2", | |
| 842 | - XK_F3 => "F3", | |
| 843 | - XK_F4 => "F4", | |
| 844 | - XK_F5 => "F5", | |
| 845 | - XK_F6 => "F6", | |
| 846 | - XK_F7 => "F7", | |
| 847 | - XK_F8 => "F8", | |
| 848 | - XK_F9 => "F9", | |
| 849 | - XK_F10 => "F10", | |
| 850 | - XK_F11 => "F11", | |
| 851 | - XK_F12 => "F12", | |
| 852 | - XK_A..=XK_Z | XK_0..=XK_9 => { | |
| 853 | - return char::from_u32(keysym).unwrap_or('?').to_string(); | |
| 854 | - } | |
| 855 | - XK_LEFT => "Left", | |
| 856 | - XK_RIGHT => "Right", | |
| 857 | - XK_UP => "Up", | |
| 858 | - XK_DOWN => "Down", | |
| 859 | - XK_HOME => "Home", | |
| 860 | - XK_END => "End", | |
| 861 | - XK_PAGE_UP => "PgUp", | |
| 862 | - XK_PAGE_DOWN => "PgDn", | |
| 863 | - XK_INSERT => "Ins", | |
| 864 | - XK_MINUS => "-", | |
| 865 | - XK_EQUAL => "=", | |
| 866 | - XK_LEFT_BRACKET => "[", | |
| 867 | - XK_RIGHT_BRACKET => "]", | |
| 868 | - XK_SEMICOLON => ";", | |
| 869 | - XK_APOSTROPHE => "'", | |
| 870 | - XK_GRAVE => "`", | |
| 871 | - XK_BACKSLASH => "\\", | |
| 872 | - XK_COMMA => ",", | |
| 873 | - XK_PERIOD => ".", | |
| 874 | - XK_SLASH => "/", | |
| 875 | - XF86_AUDIO_RAISE_VOLUME => "Vol+", | |
| 876 | - XF86_AUDIO_LOWER_VOLUME => "Vol-", | |
| 877 | - XF86_AUDIO_MUTE => "Mute", | |
| 878 | - XF86_MON_BRIGHTNESS_UP => "Bri+", | |
| 879 | - XF86_MON_BRIGHTNESS_DOWN => "Bri-", | |
| 880 | - _ => "?", | |
| 881 | - } | |
| 882 | - .to_string() | |
| 883 | - } | |
| 884 | 895 | |
| 885 | 896 | fn update_bar(&mut self) -> WmResult<()> { |
| 886 | 897 | let layout_symbol = self.get_layout_symbol(); |
| @@ -1020,6 +1031,16 @@ impl WindowManager { | ||
| 1020 | 1031 | self.focus_monitor(*direction)?; |
| 1021 | 1032 | } |
| 1022 | 1033 | } |
| 1034 | + KeyAction::ShowKeybindOverlay => { | |
| 1035 | + let monitor = &self.monitors[self.selected_monitor]; | |
| 1036 | + self.keybind_overlay.toggle( | |
| 1037 | + &self.connection, | |
| 1038 | + &self.font, | |
| 1039 | + &self.config.keybindings, | |
| 1040 | + monitor.width as u16, | |
| 1041 | + monitor.height as u16, | |
| 1042 | + )?; | |
| 1043 | + } | |
| 1023 | 1044 | KeyAction::None => {} |
| 1024 | 1045 | } |
| 1025 | 1046 | Ok(()) |
| @@ -1610,6 +1631,60 @@ impl WindowManager { | ||
| 1610 | 1631 | |
| 1611 | 1632 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 1612 | 1633 | match event { |
| 1634 | + Event::KeyPress(ref e) if e.event == self.overlay.window() => { | |
| 1635 | + if self.overlay.is_visible() { | |
| 1636 | + let _ = self.overlay.hide(&self.connection); | |
| 1637 | + } | |
| 1638 | + return Ok(None); | |
| 1639 | + } | |
| 1640 | + Event::ButtonPress(ref e) if e.event == self.overlay.window() => { | |
| 1641 | + if self.overlay.is_visible() { | |
| 1642 | + let _ = self.overlay.hide(&self.connection); | |
| 1643 | + } | |
| 1644 | + return Ok(None); | |
| 1645 | + } | |
| 1646 | + Event::Expose(ref e) if e.window == self.overlay.window() => { | |
| 1647 | + if self.overlay.is_visible() { | |
| 1648 | + let _ = self.overlay.draw(&self.connection, &self.font); | |
| 1649 | + } | |
| 1650 | + return Ok(None); | |
| 1651 | + } | |
| 1652 | + Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => { | |
| 1653 | + if self.keybind_overlay.is_visible() | |
| 1654 | + && !self.keybind_overlay.should_suppress_input() | |
| 1655 | + { | |
| 1656 | + use crate::keyboard::keysyms; | |
| 1657 | + let keyboard_mapping = self | |
| 1658 | + .connection | |
| 1659 | + .get_keyboard_mapping( | |
| 1660 | + self.connection.setup().min_keycode, | |
| 1661 | + self.connection.setup().max_keycode | |
| 1662 | + - self.connection.setup().min_keycode | |
| 1663 | + + 1, | |
| 1664 | + )? | |
| 1665 | + .reply()?; | |
| 1666 | + | |
| 1667 | + let min_keycode = self.connection.setup().min_keycode; | |
| 1668 | + let keysyms_per_keycode = keyboard_mapping.keysyms_per_keycode; | |
| 1669 | + let index = (e.detail - min_keycode) as usize * keysyms_per_keycode as usize; | |
| 1670 | + | |
| 1671 | + if let Some(&keysym) = keyboard_mapping.keysyms.get(index) { | |
| 1672 | + if keysym == keysyms::XK_ESCAPE || keysym == keysyms::XK_Q { | |
| 1673 | + let _ = self.keybind_overlay.hide(&self.connection); | |
| 1674 | + } | |
| 1675 | + } | |
| 1676 | + } | |
| 1677 | + return Ok(None); | |
| 1678 | + } | |
| 1679 | + Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => { | |
| 1680 | + return Ok(None); | |
| 1681 | + } | |
| 1682 | + Event::Expose(ref e) if e.window == self.keybind_overlay.window() => { | |
| 1683 | + if self.keybind_overlay.is_visible() { | |
| 1684 | + let _ = self.keybind_overlay.draw(&self.connection, &self.font); | |
| 1685 | + } | |
| 1686 | + return Ok(None); | |
| 1687 | + } | |
| 1613 | 1688 | Event::MapRequest(event) => { |
| 1614 | 1689 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 1615 | 1690 | Ok(attrs) => attrs, |
| @@ -1705,7 +1780,27 @@ impl WindowManager { | ||
| 1705 | 1780 | |
| 1706 | 1781 | match action { |
| 1707 | 1782 | KeyAction::Quit => return Ok(Some(false)), |
| 1708 | - KeyAction::Restart => return Ok(Some(true)), | |
| 1783 | + KeyAction::Restart => match self.try_reload_config() { | |
| 1784 | + Ok(()) => { | |
| 1785 | + self.gaps_enabled = self.config.gaps_enabled; | |
| 1786 | + self.error_message = None; | |
| 1787 | + let _ = self.overlay.hide(&self.connection); | |
| 1788 | + self.apply_layout()?; | |
| 1789 | + self.update_bar()?; | |
| 1790 | + } | |
| 1791 | + Err(err) => { | |
| 1792 | + self.error_message = Some(err.clone()); | |
| 1793 | + let screen_width = self.screen.width_in_pixels; | |
| 1794 | + let screen_height = self.screen.height_in_pixels; | |
| 1795 | + let _ = self.overlay.show_error( | |
| 1796 | + &self.connection, | |
| 1797 | + &self.font, | |
| 1798 | + &err, | |
| 1799 | + screen_width, | |
| 1800 | + screen_height, | |
| 1801 | + ); | |
| 1802 | + } | |
| 1803 | + }, | |
| 1709 | 1804 | _ => self.handle_key_action(action, &arg)?, |
| 1710 | 1805 | } |
| 1711 | 1806 | } |
+164 −0 templates/config.lua new file mode 100644
| @@ -0,0 +1,164 @@ | ||
| 1 | +-- OXWM Configuration File (Lua) | |
| 2 | +-- Migrated from config.ron | |
| 3 | +-- Edit this file and reload with Mod+Shift+R (no compilation needed!) | |
| 4 | + | |
| 5 | +local terminal = "st" | |
| 6 | +local modkey = "Mod4" | |
| 7 | +local secondary_modkey = "Mod1" | |
| 8 | + | |
| 9 | +-- Color palette | |
| 10 | +local colors = { | |
| 11 | + fg = "#bbbbbb", | |
| 12 | + red = "#f7768e", | |
| 13 | + bg = "#1a1b26", | |
| 14 | + cyan = "#0db9d7", | |
| 15 | + green = "#9ece6a", | |
| 16 | + lavender = "#a9b1d6", | |
| 17 | + light_blue = "#7aa2f7", | |
| 18 | + grey = "#bbbbbb", | |
| 19 | + blue = "#6dade3", | |
| 20 | + purple = "#ad8ee6", | |
| 21 | +} | |
| 22 | + | |
| 23 | +-- Main configuration table | |
| 24 | +return { | |
| 25 | + -- Appearance | |
| 26 | + border_width = 2, | |
| 27 | + border_focused = colors.blue, | |
| 28 | + border_unfocused = colors.grey, | |
| 29 | + font = "monospace:style=Bold:size=10", | |
| 30 | + | |
| 31 | + -- Window gaps | |
| 32 | + gaps_enabled = true, | |
| 33 | + gap_inner_horizontal = 5, | |
| 34 | + gap_inner_vertical = 5, | |
| 35 | + gap_outer_horizontal = 5, | |
| 36 | + gap_outer_vertical = 5, | |
| 37 | + | |
| 38 | + -- Basics | |
| 39 | + modkey = "Mod4", | |
| 40 | + terminal = "st", | |
| 41 | + | |
| 42 | + -- Workspace tags | |
| 43 | + tags = { "1", "2", "3", "4", "5", "6", "7", "8", "9" }, | |
| 44 | + | |
| 45 | + -- Layout symbol overrides | |
| 46 | + layout_symbols = { | |
| 47 | + { name = "tiling", symbol = "[T]" }, | |
| 48 | + { name = "normie", symbol = "[F]" }, | |
| 49 | + }, | |
| 50 | + | |
| 51 | + -- Keybindings | |
| 52 | + keybindings = { | |
| 53 | + { modifiers = { "Mod4" }, key = "Return", action = "Spawn", arg = "st" }, | |
| 54 | + { modifiers = { "Mod4" }, key = "D", action = "Spawn", arg = { "sh", "-c", "dmenu_run -l 10" } }, | |
| 55 | + { modifiers = { "Mod4" }, key = "S", action = "Spawn", arg = { "sh", "-c", "maim -s | xclip -selection clipboard -t image/png" } }, | |
| 56 | + { modifiers = { "Mod4" }, key = "Q", action = "KillClient" }, | |
| 57 | + { modifiers = { "Mod4", "Shift" }, key = "Slash", action = "ShowKeybindOverlay" }, | |
| 58 | + { modifiers = { "Mod4", "Shift" }, key = "F", action = "ToggleFullScreen" }, | |
| 59 | + { modifiers = { "Mod4", "Shift" }, key = "Space", action = "ToggleFloating" }, | |
| 60 | + { modifiers = { "Mod4" }, key = "F", action = "ChangeLayout", arg = "normie" }, | |
| 61 | + { modifiers = { "Mod4" }, key = "C", action = "ChangeLayout", arg = "tiling" }, | |
| 62 | + { modifiers = { "Mod1" }, key = "N", action = "CycleLayout" }, | |
| 63 | + { modifiers = { "Mod4" }, key = "A", action = "ToggleGaps" }, | |
| 64 | + { modifiers = { "Mod4", "Shift" }, key = "Q", action = "Quit" }, | |
| 65 | + { modifiers = { "Mod4", "Shift" }, key = "R", action = "Restart" }, | |
| 66 | + { modifiers = { "Mod4" }, key = "H", action = "FocusDirection", arg = 2 }, | |
| 67 | + { modifiers = { "Mod4" }, key = "J", action = "FocusDirection", arg = 1 }, | |
| 68 | + { modifiers = { "Mod4" }, key = "K", action = "FocusDirection", arg = 0 }, | |
| 69 | + { modifiers = { "Mod4" }, key = "L", action = "FocusDirection", arg = 3 }, | |
| 70 | + { modifiers = { "Mod4" }, key = "Comma", action = "FocusMonitor", arg = -1 }, | |
| 71 | + { modifiers = { "Mod4" }, key = "Period", action = "FocusMonitor", arg = 1 }, | |
| 72 | + { modifiers = { "Mod4" }, key = "1", action = "ViewTag", arg = 0 }, | |
| 73 | + { modifiers = { "Mod4" }, key = "2", action = "ViewTag", arg = 1 }, | |
| 74 | + { modifiers = { "Mod4" }, key = "3", action = "ViewTag", arg = 2 }, | |
| 75 | + { modifiers = { "Mod4" }, key = "4", action = "ViewTag", arg = 3 }, | |
| 76 | + { modifiers = { "Mod4" }, key = "5", action = "ViewTag", arg = 4 }, | |
| 77 | + { modifiers = { "Mod4" }, key = "6", action = "ViewTag", arg = 5 }, | |
| 78 | + { modifiers = { "Mod4" }, key = "7", action = "ViewTag", arg = 6 }, | |
| 79 | + { modifiers = { "Mod4" }, key = "8", action = "ViewTag", arg = 7 }, | |
| 80 | + { modifiers = { "Mod4" }, key = "9", action = "ViewTag", arg = 8 }, | |
| 81 | + { modifiers = { "Mod4", "Shift" }, key = "1", action = "MoveToTag", arg = 0 }, | |
| 82 | + { modifiers = { "Mod4", "Shift" }, key = "2", action = "MoveToTag", arg = 1 }, | |
| 83 | + { modifiers = { "Mod4", "Shift" }, key = "3", action = "MoveToTag", arg = 2 }, | |
| 84 | + { modifiers = { "Mod4", "Shift" }, key = "4", action = "MoveToTag", arg = 3 }, | |
| 85 | + { modifiers = { "Mod4", "Shift" }, key = "5", action = "MoveToTag", arg = 4 }, | |
| 86 | + { modifiers = { "Mod4", "Shift" }, key = "6", action = "MoveToTag", arg = 5 }, | |
| 87 | + { modifiers = { "Mod4", "Shift" }, key = "7", action = "MoveToTag", arg = 6 }, | |
| 88 | + { modifiers = { "Mod4", "Shift" }, key = "8", action = "MoveToTag", arg = 7 }, | |
| 89 | + { modifiers = { "Mod4", "Shift" }, key = "9", action = "MoveToTag", arg = 8 }, | |
| 90 | + { modifiers = { "Mod4", "Shift" }, key = "H", action = "SwapDirection", arg = 2 }, | |
| 91 | + { modifiers = { "Mod4", "Shift" }, key = "J", action = "SwapDirection", arg = 1 }, | |
| 92 | + { modifiers = { "Mod4", "Shift" }, key = "K", action = "SwapDirection", arg = 0 }, | |
| 93 | + { modifiers = { "Mod4", "Shift" }, key = "L", action = "SwapDirection", arg = 3 }, | |
| 94 | + { | |
| 95 | + keys = { | |
| 96 | + { modifiers = { "Mod4" }, key = "Space" }, | |
| 97 | + { modifiers = { }, key = "T" }, | |
| 98 | + }, | |
| 99 | + action = "Spawn", | |
| 100 | + arg = "st" | |
| 101 | + }, | |
| 102 | + }, | |
| 103 | + | |
| 104 | + -- Status bar blocks | |
| 105 | + status_blocks = { | |
| 106 | + { | |
| 107 | + format = "Ram: {used}/{total} GB", | |
| 108 | + command = "Ram", | |
| 109 | + interval_secs = 5, | |
| 110 | + color = colors.light_blue, | |
| 111 | + underline = true | |
| 112 | + }, | |
| 113 | + { | |
| 114 | + format = " │ ", | |
| 115 | + command = "Static", | |
| 116 | + interval_secs = 999999999, | |
| 117 | + color = colors.lavender, | |
| 118 | + underline = false | |
| 119 | + }, | |
| 120 | + { | |
| 121 | + format = "Kernel: {}", | |
| 122 | + command = "Shell", | |
| 123 | + command_arg = "uname -r", | |
| 124 | + interval_secs = 999999999, | |
| 125 | + color = colors.red, | |
| 126 | + underline = true | |
| 127 | + }, | |
| 128 | + { | |
| 129 | + format = " │ ", | |
| 130 | + command = "Static", | |
| 131 | + interval_secs = 999999999, | |
| 132 | + color = colors.lavender, | |
| 133 | + underline = false | |
| 134 | + }, | |
| 135 | + { | |
| 136 | + format = "{}", | |
| 137 | + command = "DateTime", | |
| 138 | + command_arg = "%a, %b %d - %-I:%M %P", | |
| 139 | + interval_secs = 1, | |
| 140 | + color = colors.cyan, | |
| 141 | + underline = true | |
| 142 | + }, | |
| 143 | + }, | |
| 144 | + | |
| 145 | + -- Color schemes for bar | |
| 146 | + scheme_normal = { | |
| 147 | + foreground = colors.fg, | |
| 148 | + background = colors.bg, | |
| 149 | + underline = "#444444" | |
| 150 | + }, | |
| 151 | + scheme_occupied = { | |
| 152 | + foreground = colors.cyan, | |
| 153 | + background = colors.bg, | |
| 154 | + underline = colors.cyan | |
| 155 | + }, | |
| 156 | + scheme_selected = { | |
| 157 | + foreground = colors.cyan, | |
| 158 | + background = colors.bg, | |
| 159 | + underline = colors.purple | |
| 160 | + }, | |
| 161 | + | |
| 162 | + -- Autostart commands | |
| 163 | + autostart = { }, | |
| 164 | +} |