Zig port.
Browse files
+3 −5 .gitignore
| @@ -1,13 +1,11 @@ | |||
| 1 | -# Rust / Cargo | 1 | +# Zig |
| 2 | -target/ | 2 | +zig-out/ |
| 3 | +.zig-cache/ | ||
| 3 | .direnv/ | 4 | .direnv/ |
| 4 | 5 | ||
| 5 | # Nix | 6 | # Nix |
| 6 | result | 7 | result |
| 7 | 8 | ||
| 8 | -# User config | ||
| 9 | -src/config.rs | ||
| 10 | - | ||
| 11 | # Notes | 9 | # Notes |
| 12 | notes/ | 10 | notes/ |
| 13 | *.md | 11 | *.md |
+0 −730 Cargo.lock deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,730 +0,0 @@ | |||
| 1 | -# This file is automatically @generated by Cargo. | ||
| 2 | -# It is not intended for manual editing. | ||
| 3 | -version = 4 | ||
| 4 | - | ||
| 5 | -[[package]] | ||
| 6 | -name = "android_system_properties" | ||
| 7 | -version = "0.1.5" | ||
| 8 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 9 | -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" | ||
| 10 | -dependencies = [ | ||
| 11 | - "libc", | ||
| 12 | -] | ||
| 13 | - | ||
| 14 | -[[package]] | ||
| 15 | -name = "autocfg" | ||
| 16 | -version = "1.5.0" | ||
| 17 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 18 | -checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" | ||
| 19 | - | ||
| 20 | -[[package]] | ||
| 21 | -name = "bitflags" | ||
| 22 | -version = "2.10.0" | ||
| 23 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 24 | -checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" | ||
| 25 | - | ||
| 26 | -[[package]] | ||
| 27 | -name = "bstr" | ||
| 28 | -version = "1.12.1" | ||
| 29 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 30 | -checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab" | ||
| 31 | -dependencies = [ | ||
| 32 | - "memchr", | ||
| 33 | - "serde", | ||
| 34 | -] | ||
| 35 | - | ||
| 36 | -[[package]] | ||
| 37 | -name = "bumpalo" | ||
| 38 | -version = "3.19.0" | ||
| 39 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 40 | -checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" | ||
| 41 | - | ||
| 42 | -[[package]] | ||
| 43 | -name = "cc" | ||
| 44 | -version = "1.2.41" | ||
| 45 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 46 | -checksum = "ac9fe6cdbb24b6ade63616c0a0688e45bb56732262c158df3c0c4bea4ca47cb7" | ||
| 47 | -dependencies = [ | ||
| 48 | - "find-msvc-tools", | ||
| 49 | - "shlex", | ||
| 50 | -] | ||
| 51 | - | ||
| 52 | -[[package]] | ||
| 53 | -name = "cfg-if" | ||
| 54 | -version = "1.0.4" | ||
| 55 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 56 | -checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" | ||
| 57 | - | ||
| 58 | -[[package]] | ||
| 59 | -name = "chrono" | ||
| 60 | -version = "0.4.42" | ||
| 61 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 62 | -checksum = "145052bdd345b87320e369255277e3fb5152762ad123a901ef5c262dd38fe8d2" | ||
| 63 | -dependencies = [ | ||
| 64 | - "iana-time-zone", | ||
| 65 | - "js-sys", | ||
| 66 | - "num-traits", | ||
| 67 | - "wasm-bindgen", | ||
| 68 | - "windows-link", | ||
| 69 | -] | ||
| 70 | - | ||
| 71 | -[[package]] | ||
| 72 | -name = "core-foundation-sys" | ||
| 73 | -version = "0.8.7" | ||
| 74 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 75 | -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" | ||
| 76 | - | ||
| 77 | -[[package]] | ||
| 78 | -name = "dirs" | ||
| 79 | -version = "5.0.1" | ||
| 80 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 81 | -checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225" | ||
| 82 | -dependencies = [ | ||
| 83 | - "dirs-sys", | ||
| 84 | -] | ||
| 85 | - | ||
| 86 | -[[package]] | ||
| 87 | -name = "dirs-sys" | ||
| 88 | -version = "0.4.1" | ||
| 89 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 90 | -checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" | ||
| 91 | -dependencies = [ | ||
| 92 | - "libc", | ||
| 93 | - "option-ext", | ||
| 94 | - "redox_users", | ||
| 95 | - "windows-sys 0.48.0", | ||
| 96 | -] | ||
| 97 | - | ||
| 98 | -[[package]] | ||
| 99 | -name = "either" | ||
| 100 | -version = "1.15.0" | ||
| 101 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 102 | -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" | ||
| 103 | - | ||
| 104 | -[[package]] | ||
| 105 | -name = "env_home" | ||
| 106 | -version = "0.1.0" | ||
| 107 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 108 | -checksum = "c7f84e12ccf0a7ddc17a6c41c93326024c42920d7ee630d04950e6926645c0fe" | ||
| 109 | - | ||
| 110 | -[[package]] | ||
| 111 | -name = "errno" | ||
| 112 | -version = "0.3.14" | ||
| 113 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 114 | -checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" | ||
| 115 | -dependencies = [ | ||
| 116 | - "libc", | ||
| 117 | - "windows-sys 0.61.2", | ||
| 118 | -] | ||
| 119 | - | ||
| 120 | -[[package]] | ||
| 121 | -name = "find-msvc-tools" | ||
| 122 | -version = "0.1.4" | ||
| 123 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 124 | -checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127" | ||
| 125 | - | ||
| 126 | -[[package]] | ||
| 127 | -name = "gethostname" | ||
| 128 | -version = "1.1.0" | ||
| 129 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 130 | -checksum = "1bd49230192a3797a9a4d6abe9b3eed6f7fa4c8a8a4947977c6f80025f92cbd8" | ||
| 131 | -dependencies = [ | ||
| 132 | - "rustix", | ||
| 133 | - "windows-link", | ||
| 134 | -] | ||
| 135 | - | ||
| 136 | -[[package]] | ||
| 137 | -name = "getrandom" | ||
| 138 | -version = "0.2.16" | ||
| 139 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 140 | -checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" | ||
| 141 | -dependencies = [ | ||
| 142 | - "cfg-if", | ||
| 143 | - "libc", | ||
| 144 | - "wasi", | ||
| 145 | -] | ||
| 146 | - | ||
| 147 | -[[package]] | ||
| 148 | -name = "iana-time-zone" | ||
| 149 | -version = "0.1.64" | ||
| 150 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 151 | -checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" | ||
| 152 | -dependencies = [ | ||
| 153 | - "android_system_properties", | ||
| 154 | - "core-foundation-sys", | ||
| 155 | - "iana-time-zone-haiku", | ||
| 156 | - "js-sys", | ||
| 157 | - "log", | ||
| 158 | - "wasm-bindgen", | ||
| 159 | - "windows-core", | ||
| 160 | -] | ||
| 161 | - | ||
| 162 | -[[package]] | ||
| 163 | -name = "iana-time-zone-haiku" | ||
| 164 | -version = "0.1.2" | ||
| 165 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 166 | -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" | ||
| 167 | -dependencies = [ | ||
| 168 | - "cc", | ||
| 169 | -] | ||
| 170 | - | ||
| 171 | -[[package]] | ||
| 172 | -name = "js-sys" | ||
| 173 | -version = "0.3.81" | ||
| 174 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 175 | -checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305" | ||
| 176 | -dependencies = [ | ||
| 177 | - "once_cell", | ||
| 178 | - "wasm-bindgen", | ||
| 179 | -] | ||
| 180 | - | ||
| 181 | -[[package]] | ||
| 182 | -name = "libc" | ||
| 183 | -version = "0.2.177" | ||
| 184 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 185 | -checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" | ||
| 186 | - | ||
| 187 | -[[package]] | ||
| 188 | -name = "libredox" | ||
| 189 | -version = "0.1.10" | ||
| 190 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 191 | -checksum = "416f7e718bdb06000964960ffa43b4335ad4012ae8b99060261aa4a8088d5ccb" | ||
| 192 | -dependencies = [ | ||
| 193 | - "bitflags", | ||
| 194 | - "libc", | ||
| 195 | -] | ||
| 196 | - | ||
| 197 | -[[package]] | ||
| 198 | -name = "linux-raw-sys" | ||
| 199 | -version = "0.11.0" | ||
| 200 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 201 | -checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" | ||
| 202 | - | ||
| 203 | -[[package]] | ||
| 204 | -name = "lock_api" | ||
| 205 | -version = "0.4.14" | ||
| 206 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 207 | -checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" | ||
| 208 | -dependencies = [ | ||
| 209 | - "scopeguard", | ||
| 210 | -] | ||
| 211 | - | ||
| 212 | -[[package]] | ||
| 213 | -name = "log" | ||
| 214 | -version = "0.4.28" | ||
| 215 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 216 | -checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" | ||
| 217 | - | ||
| 218 | -[[package]] | ||
| 219 | -name = "lua-src" | ||
| 220 | -version = "547.0.0" | ||
| 221 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 222 | -checksum = "1edaf29e3517b49b8b746701e5648ccb5785cde1c119062cbabbc5d5cd115e42" | ||
| 223 | -dependencies = [ | ||
| 224 | - "cc", | ||
| 225 | -] | ||
| 226 | - | ||
| 227 | -[[package]] | ||
| 228 | -name = "luajit-src" | ||
| 229 | -version = "210.5.12+a4f56a4" | ||
| 230 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 231 | -checksum = "b3a8e7962a5368d5f264d045a5a255e90f9aa3fc1941ae15a8d2940d42cac671" | ||
| 232 | -dependencies = [ | ||
| 233 | - "cc", | ||
| 234 | - "which", | ||
| 235 | -] | ||
| 236 | - | ||
| 237 | -[[package]] | ||
| 238 | -name = "memchr" | ||
| 239 | -version = "2.7.6" | ||
| 240 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 241 | -checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" | ||
| 242 | - | ||
| 243 | -[[package]] | ||
| 244 | -name = "mlua" | ||
| 245 | -version = "0.10.5" | ||
| 246 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 247 | -checksum = "c1f5f8fbebc7db5f671671134b9321c4b9aa9adeafccfd9a8c020ae45c6a35d0" | ||
| 248 | -dependencies = [ | ||
| 249 | - "bstr", | ||
| 250 | - "either", | ||
| 251 | - "mlua-sys", | ||
| 252 | - "num-traits", | ||
| 253 | - "parking_lot", | ||
| 254 | - "rustc-hash", | ||
| 255 | - "rustversion", | ||
| 256 | -] | ||
| 257 | - | ||
| 258 | -[[package]] | ||
| 259 | -name = "mlua-sys" | ||
| 260 | -version = "0.6.8" | ||
| 261 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 262 | -checksum = "380c1f7e2099cafcf40e51d3a9f20a346977587aa4d012eae1f043149a728a93" | ||
| 263 | -dependencies = [ | ||
| 264 | - "cc", | ||
| 265 | - "cfg-if", | ||
| 266 | - "lua-src", | ||
| 267 | - "luajit-src", | ||
| 268 | - "pkg-config", | ||
| 269 | -] | ||
| 270 | - | ||
| 271 | -[[package]] | ||
| 272 | -name = "num-traits" | ||
| 273 | -version = "0.2.19" | ||
| 274 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 275 | -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" | ||
| 276 | -dependencies = [ | ||
| 277 | - "autocfg", | ||
| 278 | -] | ||
| 279 | - | ||
| 280 | -[[package]] | ||
| 281 | -name = "once_cell" | ||
| 282 | -version = "1.21.3" | ||
| 283 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 284 | -checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" | ||
| 285 | - | ||
| 286 | -[[package]] | ||
| 287 | -name = "option-ext" | ||
| 288 | -version = "0.2.0" | ||
| 289 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 290 | -checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" | ||
| 291 | - | ||
| 292 | -[[package]] | ||
| 293 | -name = "oxwm" | ||
| 294 | -version = "0.10.2" | ||
| 295 | -dependencies = [ | ||
| 296 | - "chrono", | ||
| 297 | - "dirs", | ||
| 298 | - "mlua", | ||
| 299 | - "serde", | ||
| 300 | - "x11", | ||
| 301 | - "x11rb", | ||
| 302 | -] | ||
| 303 | - | ||
| 304 | -[[package]] | ||
| 305 | -name = "parking_lot" | ||
| 306 | -version = "0.12.5" | ||
| 307 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 308 | -checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" | ||
| 309 | -dependencies = [ | ||
| 310 | - "lock_api", | ||
| 311 | - "parking_lot_core", | ||
| 312 | -] | ||
| 313 | - | ||
| 314 | -[[package]] | ||
| 315 | -name = "parking_lot_core" | ||
| 316 | -version = "0.9.12" | ||
| 317 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 318 | -checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" | ||
| 319 | -dependencies = [ | ||
| 320 | - "cfg-if", | ||
| 321 | - "libc", | ||
| 322 | - "redox_syscall", | ||
| 323 | - "smallvec", | ||
| 324 | - "windows-link", | ||
| 325 | -] | ||
| 326 | - | ||
| 327 | -[[package]] | ||
| 328 | -name = "pkg-config" | ||
| 329 | -version = "0.3.32" | ||
| 330 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 331 | -checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" | ||
| 332 | - | ||
| 333 | -[[package]] | ||
| 334 | -name = "proc-macro2" | ||
| 335 | -version = "1.0.101" | ||
| 336 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 337 | -checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" | ||
| 338 | -dependencies = [ | ||
| 339 | - "unicode-ident", | ||
| 340 | -] | ||
| 341 | - | ||
| 342 | -[[package]] | ||
| 343 | -name = "quote" | ||
| 344 | -version = "1.0.41" | ||
| 345 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 346 | -checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" | ||
| 347 | -dependencies = [ | ||
| 348 | - "proc-macro2", | ||
| 349 | -] | ||
| 350 | - | ||
| 351 | -[[package]] | ||
| 352 | -name = "redox_syscall" | ||
| 353 | -version = "0.5.18" | ||
| 354 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 355 | -checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" | ||
| 356 | -dependencies = [ | ||
| 357 | - "bitflags", | ||
| 358 | -] | ||
| 359 | - | ||
| 360 | -[[package]] | ||
| 361 | -name = "redox_users" | ||
| 362 | -version = "0.4.6" | ||
| 363 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 364 | -checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" | ||
| 365 | -dependencies = [ | ||
| 366 | - "getrandom", | ||
| 367 | - "libredox", | ||
| 368 | - "thiserror", | ||
| 369 | -] | ||
| 370 | - | ||
| 371 | -[[package]] | ||
| 372 | -name = "rustc-hash" | ||
| 373 | -version = "2.1.1" | ||
| 374 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 375 | -checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" | ||
| 376 | - | ||
| 377 | -[[package]] | ||
| 378 | -name = "rustix" | ||
| 379 | -version = "1.1.2" | ||
| 380 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 381 | -checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e" | ||
| 382 | -dependencies = [ | ||
| 383 | - "bitflags", | ||
| 384 | - "errno", | ||
| 385 | - "libc", | ||
| 386 | - "linux-raw-sys", | ||
| 387 | - "windows-sys 0.61.2", | ||
| 388 | -] | ||
| 389 | - | ||
| 390 | -[[package]] | ||
| 391 | -name = "rustversion" | ||
| 392 | -version = "1.0.22" | ||
| 393 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 394 | -checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" | ||
| 395 | - | ||
| 396 | -[[package]] | ||
| 397 | -name = "scopeguard" | ||
| 398 | -version = "1.2.0" | ||
| 399 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 400 | -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" | ||
| 401 | - | ||
| 402 | -[[package]] | ||
| 403 | -name = "serde" | ||
| 404 | -version = "1.0.228" | ||
| 405 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 406 | -checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" | ||
| 407 | -dependencies = [ | ||
| 408 | - "serde_core", | ||
| 409 | - "serde_derive", | ||
| 410 | -] | ||
| 411 | - | ||
| 412 | -[[package]] | ||
| 413 | -name = "serde_core" | ||
| 414 | -version = "1.0.228" | ||
| 415 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 416 | -checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" | ||
| 417 | -dependencies = [ | ||
| 418 | - "serde_derive", | ||
| 419 | -] | ||
| 420 | - | ||
| 421 | -[[package]] | ||
| 422 | -name = "serde_derive" | ||
| 423 | -version = "1.0.228" | ||
| 424 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 425 | -checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" | ||
| 426 | -dependencies = [ | ||
| 427 | - "proc-macro2", | ||
| 428 | - "quote", | ||
| 429 | - "syn", | ||
| 430 | -] | ||
| 431 | - | ||
| 432 | -[[package]] | ||
| 433 | -name = "shlex" | ||
| 434 | -version = "1.3.0" | ||
| 435 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 436 | -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" | ||
| 437 | - | ||
| 438 | -[[package]] | ||
| 439 | -name = "smallvec" | ||
| 440 | -version = "1.15.1" | ||
| 441 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 442 | -checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" | ||
| 443 | - | ||
| 444 | -[[package]] | ||
| 445 | -name = "syn" | ||
| 446 | -version = "2.0.107" | ||
| 447 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 448 | -checksum = "2a26dbd934e5451d21ef060c018dae56fc073894c5a7896f882928a76e6d081b" | ||
| 449 | -dependencies = [ | ||
| 450 | - "proc-macro2", | ||
| 451 | - "quote", | ||
| 452 | - "unicode-ident", | ||
| 453 | -] | ||
| 454 | - | ||
| 455 | -[[package]] | ||
| 456 | -name = "thiserror" | ||
| 457 | -version = "1.0.69" | ||
| 458 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 459 | -checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" | ||
| 460 | -dependencies = [ | ||
| 461 | - "thiserror-impl", | ||
| 462 | -] | ||
| 463 | - | ||
| 464 | -[[package]] | ||
| 465 | -name = "thiserror-impl" | ||
| 466 | -version = "1.0.69" | ||
| 467 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 468 | -checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" | ||
| 469 | -dependencies = [ | ||
| 470 | - "proc-macro2", | ||
| 471 | - "quote", | ||
| 472 | - "syn", | ||
| 473 | -] | ||
| 474 | - | ||
| 475 | -[[package]] | ||
| 476 | -name = "unicode-ident" | ||
| 477 | -version = "1.0.20" | ||
| 478 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 479 | -checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06" | ||
| 480 | - | ||
| 481 | -[[package]] | ||
| 482 | -name = "wasi" | ||
| 483 | -version = "0.11.1+wasi-snapshot-preview1" | ||
| 484 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 485 | -checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" | ||
| 486 | - | ||
| 487 | -[[package]] | ||
| 488 | -name = "wasm-bindgen" | ||
| 489 | -version = "0.2.104" | ||
| 490 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 491 | -checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d" | ||
| 492 | -dependencies = [ | ||
| 493 | - "cfg-if", | ||
| 494 | - "once_cell", | ||
| 495 | - "rustversion", | ||
| 496 | - "wasm-bindgen-macro", | ||
| 497 | - "wasm-bindgen-shared", | ||
| 498 | -] | ||
| 499 | - | ||
| 500 | -[[package]] | ||
| 501 | -name = "wasm-bindgen-backend" | ||
| 502 | -version = "0.2.104" | ||
| 503 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 504 | -checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19" | ||
| 505 | -dependencies = [ | ||
| 506 | - "bumpalo", | ||
| 507 | - "log", | ||
| 508 | - "proc-macro2", | ||
| 509 | - "quote", | ||
| 510 | - "syn", | ||
| 511 | - "wasm-bindgen-shared", | ||
| 512 | -] | ||
| 513 | - | ||
| 514 | -[[package]] | ||
| 515 | -name = "wasm-bindgen-macro" | ||
| 516 | -version = "0.2.104" | ||
| 517 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 518 | -checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119" | ||
| 519 | -dependencies = [ | ||
| 520 | - "quote", | ||
| 521 | - "wasm-bindgen-macro-support", | ||
| 522 | -] | ||
| 523 | - | ||
| 524 | -[[package]] | ||
| 525 | -name = "wasm-bindgen-macro-support" | ||
| 526 | -version = "0.2.104" | ||
| 527 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 528 | -checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7" | ||
| 529 | -dependencies = [ | ||
| 530 | - "proc-macro2", | ||
| 531 | - "quote", | ||
| 532 | - "syn", | ||
| 533 | - "wasm-bindgen-backend", | ||
| 534 | - "wasm-bindgen-shared", | ||
| 535 | -] | ||
| 536 | - | ||
| 537 | -[[package]] | ||
| 538 | -name = "wasm-bindgen-shared" | ||
| 539 | -version = "0.2.104" | ||
| 540 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 541 | -checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1" | ||
| 542 | -dependencies = [ | ||
| 543 | - "unicode-ident", | ||
| 544 | -] | ||
| 545 | - | ||
| 546 | -[[package]] | ||
| 547 | -name = "which" | ||
| 548 | -version = "7.0.3" | ||
| 549 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 550 | -checksum = "24d643ce3fd3e5b54854602a080f34fb10ab75e0b813ee32d00ca2b44fa74762" | ||
| 551 | -dependencies = [ | ||
| 552 | - "either", | ||
| 553 | - "env_home", | ||
| 554 | - "rustix", | ||
| 555 | - "winsafe", | ||
| 556 | -] | ||
| 557 | - | ||
| 558 | -[[package]] | ||
| 559 | -name = "windows-core" | ||
| 560 | -version = "0.62.2" | ||
| 561 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 562 | -checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" | ||
| 563 | -dependencies = [ | ||
| 564 | - "windows-implement", | ||
| 565 | - "windows-interface", | ||
| 566 | - "windows-link", | ||
| 567 | - "windows-result", | ||
| 568 | - "windows-strings", | ||
| 569 | -] | ||
| 570 | - | ||
| 571 | -[[package]] | ||
| 572 | -name = "windows-implement" | ||
| 573 | -version = "0.60.2" | ||
| 574 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 575 | -checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" | ||
| 576 | -dependencies = [ | ||
| 577 | - "proc-macro2", | ||
| 578 | - "quote", | ||
| 579 | - "syn", | ||
| 580 | -] | ||
| 581 | - | ||
| 582 | -[[package]] | ||
| 583 | -name = "windows-interface" | ||
| 584 | -version = "0.59.3" | ||
| 585 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 586 | -checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" | ||
| 587 | -dependencies = [ | ||
| 588 | - "proc-macro2", | ||
| 589 | - "quote", | ||
| 590 | - "syn", | ||
| 591 | -] | ||
| 592 | - | ||
| 593 | -[[package]] | ||
| 594 | -name = "windows-link" | ||
| 595 | -version = "0.2.1" | ||
| 596 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 597 | -checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" | ||
| 598 | - | ||
| 599 | -[[package]] | ||
| 600 | -name = "windows-result" | ||
| 601 | -version = "0.4.1" | ||
| 602 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 603 | -checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" | ||
| 604 | -dependencies = [ | ||
| 605 | - "windows-link", | ||
| 606 | -] | ||
| 607 | - | ||
| 608 | -[[package]] | ||
| 609 | -name = "windows-strings" | ||
| 610 | -version = "0.5.1" | ||
| 611 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 612 | -checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" | ||
| 613 | -dependencies = [ | ||
| 614 | - "windows-link", | ||
| 615 | -] | ||
| 616 | - | ||
| 617 | -[[package]] | ||
| 618 | -name = "windows-sys" | ||
| 619 | -version = "0.48.0" | ||
| 620 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 621 | -checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" | ||
| 622 | -dependencies = [ | ||
| 623 | - "windows-targets", | ||
| 624 | -] | ||
| 625 | - | ||
| 626 | -[[package]] | ||
| 627 | -name = "windows-sys" | ||
| 628 | -version = "0.61.2" | ||
| 629 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 630 | -checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" | ||
| 631 | -dependencies = [ | ||
| 632 | - "windows-link", | ||
| 633 | -] | ||
| 634 | - | ||
| 635 | -[[package]] | ||
| 636 | -name = "windows-targets" | ||
| 637 | -version = "0.48.5" | ||
| 638 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 639 | -checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" | ||
| 640 | -dependencies = [ | ||
| 641 | - "windows_aarch64_gnullvm", | ||
| 642 | - "windows_aarch64_msvc", | ||
| 643 | - "windows_i686_gnu", | ||
| 644 | - "windows_i686_msvc", | ||
| 645 | - "windows_x86_64_gnu", | ||
| 646 | - "windows_x86_64_gnullvm", | ||
| 647 | - "windows_x86_64_msvc", | ||
| 648 | -] | ||
| 649 | - | ||
| 650 | -[[package]] | ||
| 651 | -name = "windows_aarch64_gnullvm" | ||
| 652 | -version = "0.48.5" | ||
| 653 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 654 | -checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" | ||
| 655 | - | ||
| 656 | -[[package]] | ||
| 657 | -name = "windows_aarch64_msvc" | ||
| 658 | -version = "0.48.5" | ||
| 659 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 660 | -checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" | ||
| 661 | - | ||
| 662 | -[[package]] | ||
| 663 | -name = "windows_i686_gnu" | ||
| 664 | -version = "0.48.5" | ||
| 665 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 666 | -checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" | ||
| 667 | - | ||
| 668 | -[[package]] | ||
| 669 | -name = "windows_i686_msvc" | ||
| 670 | -version = "0.48.5" | ||
| 671 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 672 | -checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" | ||
| 673 | - | ||
| 674 | -[[package]] | ||
| 675 | -name = "windows_x86_64_gnu" | ||
| 676 | -version = "0.48.5" | ||
| 677 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 678 | -checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" | ||
| 679 | - | ||
| 680 | -[[package]] | ||
| 681 | -name = "windows_x86_64_gnullvm" | ||
| 682 | -version = "0.48.5" | ||
| 683 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 684 | -checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" | ||
| 685 | - | ||
| 686 | -[[package]] | ||
| 687 | -name = "windows_x86_64_msvc" | ||
| 688 | -version = "0.48.5" | ||
| 689 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 690 | -checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" | ||
| 691 | - | ||
| 692 | -[[package]] | ||
| 693 | -name = "winsafe" | ||
| 694 | -version = "0.0.19" | ||
| 695 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 696 | -checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" | ||
| 697 | - | ||
| 698 | -[[package]] | ||
| 699 | -name = "x11" | ||
| 700 | -version = "2.21.0" | ||
| 701 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 702 | -checksum = "502da5464ccd04011667b11c435cb992822c2c0dbde1770c988480d312a0db2e" | ||
| 703 | -dependencies = [ | ||
| 704 | - "libc", | ||
| 705 | - "pkg-config", | ||
| 706 | -] | ||
| 707 | - | ||
| 708 | -[[package]] | ||
| 709 | -name = "x11rb" | ||
| 710 | -version = "0.13.2" | ||
| 711 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 712 | -checksum = "9993aa5be5a26815fe2c3eacfc1fde061fc1a1f094bf1ad2a18bf9c495dd7414" | ||
| 713 | -dependencies = [ | ||
| 714 | - "gethostname", | ||
| 715 | - "rustix", | ||
| 716 | - "x11rb-protocol", | ||
| 717 | - "xcursor", | ||
| 718 | -] | ||
| 719 | - | ||
| 720 | -[[package]] | ||
| 721 | -name = "x11rb-protocol" | ||
| 722 | -version = "0.13.2" | ||
| 723 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 724 | -checksum = "ea6fc2961e4ef194dcbfe56bb845534d0dc8098940c7e5c012a258bfec6701bd" | ||
| 725 | - | ||
| 726 | -[[package]] | ||
| 727 | -name = "xcursor" | ||
| 728 | -version = "0.3.10" | ||
| 729 | -source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 730 | -checksum = "bec9e4a500ca8864c5b47b8b482a73d62e4237670e5b5f1d6b9e3cae50f28f2b" | ||
+0 −20 Cargo.toml deleted file mode 100644
| @@ -1,20 +0,0 @@ | |||
| 1 | -[package] | ||
| 2 | -name = "oxwm" | ||
| 3 | -version = "0.10.2" | ||
| 4 | -edition = "2024" | ||
| 5 | - | ||
| 6 | -[lib] | ||
| 7 | -name = "oxwm" | ||
| 8 | -path = "src/lib.rs" | ||
| 9 | - | ||
| 10 | -[[bin]] | ||
| 11 | -name = "oxwm" | ||
| 12 | -path = "src/bin/main.rs" | ||
| 13 | - | ||
| 14 | -[dependencies] | ||
| 15 | -x11 = { version = "2.21", features = ["xlib", "xft"] } | ||
| 16 | -x11rb = { version = "0.13", features = ["cursor", "xinerama"] } | ||
| 17 | -chrono = "0.4" | ||
| 18 | -dirs = "5.0" | ||
| 19 | -serde = { version = "1.0", features = ["derive"] } | ||
| 20 | -mlua = { version = "0.10", features = ["lua54", "vendored"] } | ||
+76 −0 build.zig new file mode 100644
| @@ -0,0 +1,76 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +pub fn build(b: *std.Build) void { | ||
| 4 | + const target = b.standardTargetOptions(.{}); | ||
| 5 | + const optimize = b.standardOptimizeOption(.{}); | ||
| 6 | + | ||
| 7 | + const exe = b.addExecutable(.{ | ||
| 8 | + .name = "oxwm", | ||
| 9 | + .root_module = b.createModule(.{ | ||
| 10 | + .root_source_file = b.path("src/main.zig"), | ||
| 11 | + .target = target, | ||
| 12 | + .optimize = optimize, | ||
| 13 | + }), | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + exe.linkSystemLibrary("lua"); | ||
| 17 | + exe.linkSystemLibrary("X11"); | ||
| 18 | + exe.linkSystemLibrary("Xinerama"); | ||
| 19 | + exe.linkSystemLibrary("Xft"); | ||
| 20 | + exe.linkSystemLibrary("fontconfig"); | ||
| 21 | + exe.linkLibC(); | ||
| 22 | + | ||
| 23 | + b.installArtifact(exe); | ||
| 24 | + | ||
| 25 | + const run_step = b.step("run", "Run oxwm"); | ||
| 26 | + const run_cmd = b.addRunArtifact(exe); | ||
| 27 | + run_step.dependOn(&run_cmd.step); | ||
| 28 | + run_cmd.step.dependOn(b.getInstallStep()); | ||
| 29 | + if (b.args) |args| { | ||
| 30 | + run_cmd.addArgs(args); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + const test_step = b.step("test", "Run unit tests"); | ||
| 34 | + const unit_tests = b.addTest(.{ | ||
| 35 | + .root_module = b.createModule(.{ | ||
| 36 | + .root_source_file = b.path("tests/main_tests.zig"), | ||
| 37 | + .target = target, | ||
| 38 | + .optimize = optimize, | ||
| 39 | + }), | ||
| 40 | + }); | ||
| 41 | + test_step.dependOn(&b.addRunArtifact(unit_tests).step); | ||
| 42 | + | ||
| 43 | + const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)"); | ||
| 44 | + xephyr_step.dependOn(&add_xephyr_run(b, exe, false).step); | ||
| 45 | + | ||
| 46 | + const xephyr_multi_step = b.step("xephyr-multi", "Run in Xephyr multi-monitor on :2"); | ||
| 47 | + xephyr_multi_step.dependOn(&add_xephyr_run(b, exe, true).step); | ||
| 48 | + | ||
| 49 | + const multimon_step = b.step("multimon", "Alias for xephyr-multi"); | ||
| 50 | + multimon_step.dependOn(&add_xephyr_run(b, exe, true).step); | ||
| 51 | + | ||
| 52 | + const kill_step = b.step("kill", "Kill Xephyr and oxwm"); | ||
| 53 | + kill_step.dependOn(&b.addSystemCommand(&.{ "sh", "-c", "pkill -9 Xephyr || true; pkill -9 oxwm || true" }).step); | ||
| 54 | + | ||
| 55 | + const fmt_step = b.step("fmt", "Format source files"); | ||
| 56 | + fmt_step.dependOn(&b.addFmt(.{ .paths = &.{"src/"} }).step); | ||
| 57 | + | ||
| 58 | + const clean_step = b.step("clean", "Remove build artifacts"); | ||
| 59 | + clean_step.dependOn(&b.addSystemCommand(&.{ "rm", "-rf", "zig-out", ".zig-cache" }).step); | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +fn add_xephyr_run(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run { | ||
| 63 | + const kill_cmd = if (multimon) | ||
| 64 | + "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1" | ||
| 65 | + else | ||
| 66 | + "pkill -9 Xephyr || true; Xephyr -screen 1280x800 :2 & sleep 1"; | ||
| 67 | + | ||
| 68 | + const setup = b.addSystemCommand(&.{ "sh", "-c", kill_cmd }); | ||
| 69 | + | ||
| 70 | + const run_wm = b.addRunArtifact(exe); | ||
| 71 | + run_wm.step.dependOn(&setup.step); | ||
| 72 | + run_wm.setEnvironmentVariable("DISPLAY", ":2"); | ||
| 73 | + run_wm.addArgs(&.{ "-c", "resources/config.lua" }); | ||
| 74 | + | ||
| 75 | + return run_wm; | ||
| 76 | +} | ||
+25 −16 default.nix
| @@ -1,45 +1,54 @@ | |||
| 1 | { | 1 | { |
| 2 | lib, | 2 | lib, |
| 3 | - rustPlatform, | 3 | + stdenv, |
| 4 | + zig, | ||
| 4 | pkg-config, | 5 | pkg-config, |
| 5 | - libX11, | 6 | + xorg, |
| 6 | - libXft, | 7 | + lua5_4, |
| 7 | - libXrender, | ||
| 8 | freetype, | 8 | freetype, |
| 9 | fontconfig, | 9 | fontconfig, |
| 10 | - gitRev ? "unkown", | 10 | + gitRev ? "unknown", |
| 11 | }: | 11 | }: |
| 12 | -rustPlatform.buildRustPackage (finalAttrs: { | 12 | +stdenv.mkDerivation (finalAttrs: { |
| 13 | pname = "oxwm"; | 13 | pname = "oxwm"; |
| 14 | version = "${lib.substring 0 8 gitRev}"; | 14 | version = "${lib.substring 0 8 gitRev}"; |
| 15 | 15 | ||
| 16 | src = ./.; | 16 | src = ./.; |
| 17 | 17 | ||
| 18 | - cargoLock.lockFile = ./Cargo.lock; | 18 | + nativeBuildInputs = [zig pkg-config]; |
| 19 | - | ||
| 20 | - nativeBuildInputs = [pkg-config]; | ||
| 21 | 19 | ||
| 22 | buildInputs = [ | 20 | buildInputs = [ |
| 23 | - libX11 | 21 | + xorg.libX11 |
| 24 | - libXft | 22 | + xorg.libXinerama |
| 25 | - libXrender | 23 | + xorg.libXft |
| 24 | + lua5_4 | ||
| 26 | freetype | 25 | freetype |
| 27 | fontconfig | 26 | fontconfig |
| 28 | ]; | 27 | ]; |
| 29 | 28 | ||
| 30 | - # tests require a running X server | 29 | + dontConfigure = true; |
| 31 | - doCheck = false; | 30 | + |
| 31 | + buildPhase = '' | ||
| 32 | + runHook preBuild | ||
| 33 | + zig build -Doptimize=ReleaseSafe --prefix $out | ||
| 34 | + runHook postBuild | ||
| 35 | + ''; | ||
| 32 | 36 | ||
| 33 | - postInstall = '' | 37 | + installPhase = '' |
| 38 | + runHook preInstall | ||
| 34 | install resources/oxwm.desktop -Dt $out/share/xsessions | 39 | install resources/oxwm.desktop -Dt $out/share/xsessions |
| 35 | install -Dm644 resources/oxwm.1 -t $out/share/man/man1 | 40 | install -Dm644 resources/oxwm.1 -t $out/share/man/man1 |
| 36 | install -Dm644 templates/oxwm.lua -t $out/share/oxwm | 41 | install -Dm644 templates/oxwm.lua -t $out/share/oxwm |
| 42 | + runHook postInstall | ||
| 37 | ''; | 43 | ''; |
| 38 | 44 | ||
| 45 | + # tests require a running X server | ||
| 46 | + doCheck = false; | ||
| 47 | + | ||
| 39 | passthru.providedSessions = ["oxwm"]; | 48 | passthru.providedSessions = ["oxwm"]; |
| 40 | 49 | ||
| 41 | meta = { | 50 | meta = { |
| 42 | - description = "Dynamic window manager written in Rust, inspired by dwm"; | 51 | + description = "Dynamic window manager written in Zig, inspired by dwm"; |
| 43 | homepage = "https://github.com/tonybanters/oxwm"; | 52 | homepage = "https://github.com/tonybanters/oxwm"; |
| 44 | license = lib.licenses.gpl3Only; | 53 | license = lib.licenses.gpl3Only; |
| 45 | platforms = lib.platforms.linux; | 54 | platforms = lib.platforms.linux; |
+2 −4 flake.nix
| @@ -23,9 +23,8 @@ | |||
| 23 | default = pkgs.mkShell { | 23 | default = pkgs.mkShell { |
| 24 | inputsFrom = [self.packages.${pkgs.stdenv.hostPlatform.system}.oxwm]; | 24 | inputsFrom = [self.packages.${pkgs.stdenv.hostPlatform.system}.oxwm]; |
| 25 | packages = [ | 25 | packages = [ |
| 26 | - pkgs.rustc | 26 | + pkgs.zig |
| 27 | - pkgs.cargo | 27 | + pkgs.zls |
| 28 | - pkgs.clippy | ||
| 29 | pkgs.alacritty | 28 | pkgs.alacritty |
| 30 | pkgs.just | 29 | pkgs.just |
| 31 | pkgs.xorg.xorgserver | 30 | pkgs.xorg.xorgserver |
| @@ -33,7 +32,6 @@ | |||
| 33 | shellHook = '' | 32 | shellHook = '' |
| 34 | export PS1="(oxwm-dev) $PS1" | 33 | export PS1="(oxwm-dev) $PS1" |
| 35 | ''; | 34 | ''; |
| 36 | - env.RUST_SRC_PATH = "${pkgs.rustPlatform.rustLibSrc}"; | ||
| 37 | }; | 35 | }; |
| 38 | }); | 36 | }); |
| 39 | 37 | ||
+18 −27 justfile
| @@ -1,52 +1,43 @@ | |||
| 1 | build: | 1 | build: |
| 2 | - cargo build --release | 2 | + zig build -Doptimize=ReleaseSafe |
| 3 | 3 | ||
| 4 | install: build | 4 | install: build |
| 5 | - cp target/release/oxwm /usr/bin/oxwm | 5 | + sudo cp zig-out/bin/oxwm /usr/bin/oxwm |
| 6 | - cp resources/oxwm.desktop /usr/share/xsessions/oxwm.desktop | 6 | + sudo cp resources/oxwm.desktop /usr/share/xsessions/oxwm.desktop |
| 7 | - chmod +x /usr/bin/oxwm | 7 | + sudo chmod +x /usr/bin/oxwm |
| 8 | - @echo "✓ oxwm installed to /usr/bin/oxwm" | 8 | + @echo "oxwm installed to /usr/bin/oxwm" |
| 9 | - @echo " Run 'oxwm --init' to create your config" | ||
| 10 | 9 | ||
| 11 | checkinstall: | 10 | checkinstall: |
| 12 | checkinstall --pkgname oxwm --exclude /root -y just install | 11 | checkinstall --pkgname oxwm --exclude /root -y just install |
| 13 | 12 | ||
| 14 | uninstall: | 13 | uninstall: |
| 15 | - rm -f /usr/bin/oxwm | 14 | + sudo rm -f /usr/bin/oxwm |
| 16 | - @echo "✓ oxwm uninstalled" | 15 | + @echo "oxwm uninstalled" |
| 17 | - @echo " Your config at ~/.config/oxwm/ is preserved" | 16 | + @echo "Your config at ~/.config/oxwm/ is preserved" |
| 18 | 17 | ||
| 19 | clean: | 18 | clean: |
| 20 | - cargo clean | 19 | + rm -rf zig-out .zig-cache |
| 21 | 20 | ||
| 22 | test-clean: | 21 | test-clean: |
| 23 | pkill Xephyr || true | 22 | pkill Xephyr || true |
| 24 | rm -rf ~/.config/oxwm | 23 | rm -rf ~/.config/oxwm |
| 25 | Xephyr -screen 1280x800 :1 & sleep 1 | 24 | Xephyr -screen 1280x800 :1 & sleep 1 |
| 26 | - DISPLAY=:1 cargo run --release -- --config resources/test-config.lua | 25 | + DISPLAY=:1 zig build run -- -c resources/config.lua |
| 27 | 26 | ||
| 28 | test: | 27 | test: |
| 29 | - pkill Xephyr || true | 28 | + zig build xephyr |
| 30 | - Xephyr -screen 1280x800 :2 & sleep 1 | ||
| 31 | - DISPLAY=:2 cargo run --release -- --config resources/test-config.lua | ||
| 32 | 29 | ||
| 33 | test-multimon: | 30 | test-multimon: |
| 34 | - pkill Xephyr || true | 31 | + zig build xephyr-multi |
| 35 | - Xephyr +xinerama -screen 640x480 -screen 640x480 :1 & sleep 1 | ||
| 36 | - DISPLAY=:1 cargo run --release -- --config resources/test-config.lua | ||
| 37 | - | ||
| 38 | -init: | ||
| 39 | - cargo run --release -- --init | ||
| 40 | 32 | ||
| 41 | edit: | 33 | edit: |
| 42 | $EDITOR ~/.config/oxwm/config.lua | 34 | $EDITOR ~/.config/oxwm/config.lua |
| 43 | 35 | ||
| 44 | -check: | ||
| 45 | - cargo clippy -- -W clippy::all | ||
| 46 | - cargo fmt -- --check | ||
| 47 | - | ||
| 48 | fmt: | 36 | fmt: |
| 49 | - cargo fmt | 37 | + zig build fmt |
| 38 | + | ||
| 39 | +pre-commit: fmt build | ||
| 40 | + @echo "All checks passed!" | ||
| 50 | 41 | ||
| 51 | -pre-commit: fmt check build | 42 | +run: |
| 52 | - @echo "✓ All checks passed!" | 43 | + zig build run |
+76 −0 src/animations.zig new file mode 100644
| @@ -0,0 +1,76 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +pub const Easing = enum { | ||
| 4 | + linear, | ||
| 5 | + ease_out, | ||
| 6 | + ease_in_out, | ||
| 7 | + | ||
| 8 | + pub fn apply(self: Easing, t: f64) f64 { | ||
| 9 | + return switch (self) { | ||
| 10 | + .linear => t, | ||
| 11 | + .ease_out => 1.0 - std.math.pow(f64, 1.0 - t, 3), | ||
| 12 | + .ease_in_out => if (t < 0.5) | ||
| 13 | + 4.0 * t * t * t | ||
| 14 | + else | ||
| 15 | + 1.0 - std.math.pow(f64, -2.0 * t + 2.0, 3) / 2.0, | ||
| 16 | + }; | ||
| 17 | + } | ||
| 18 | +}; | ||
| 19 | + | ||
| 20 | +pub const Animation_Config = struct { | ||
| 21 | + duration_ms: u64 = 150, | ||
| 22 | + easing: Easing = .ease_out, | ||
| 23 | +}; | ||
| 24 | + | ||
| 25 | +pub const Scroll_Animation = struct { | ||
| 26 | + start_value: i32 = 0, | ||
| 27 | + end_value: i32 = 0, | ||
| 28 | + start_time: i64 = 0, | ||
| 29 | + duration_ms: u64 = 150, | ||
| 30 | + easing: Easing = .ease_out, | ||
| 31 | + active: bool = false, | ||
| 32 | + | ||
| 33 | + pub fn start(self: *Scroll_Animation, from: i32, to: i32, config: Animation_Config) void { | ||
| 34 | + if (from == to) { | ||
| 35 | + self.active = false; | ||
| 36 | + return; | ||
| 37 | + } | ||
| 38 | + self.start_value = from; | ||
| 39 | + self.end_value = to; | ||
| 40 | + self.start_time = std.time.milliTimestamp(); | ||
| 41 | + self.duration_ms = config.duration_ms; | ||
| 42 | + self.easing = config.easing; | ||
| 43 | + self.active = true; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + pub fn update(self: *Scroll_Animation) ?i32 { | ||
| 47 | + if (!self.active) return null; | ||
| 48 | + | ||
| 49 | + const now = std.time.milliTimestamp(); | ||
| 50 | + const elapsed = now - self.start_time; | ||
| 51 | + | ||
| 52 | + if (elapsed >= @as(i64, @intCast(self.duration_ms))) { | ||
| 53 | + self.active = false; | ||
| 54 | + return self.end_value; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + const t = @as(f64, @floatFromInt(elapsed)) / @as(f64, @floatFromInt(self.duration_ms)); | ||
| 58 | + const eased = self.easing.apply(t); | ||
| 59 | + const diff = @as(f64, @floatFromInt(self.end_value - self.start_value)); | ||
| 60 | + const current = @as(f64, @floatFromInt(self.start_value)) + (diff * eased); | ||
| 61 | + | ||
| 62 | + return @intFromFloat(current); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + pub fn is_active(self: *const Scroll_Animation) bool { | ||
| 66 | + return self.active; | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + pub fn target(self: *const Scroll_Animation) i32 { | ||
| 70 | + return self.end_value; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + pub fn stop(self: *Scroll_Animation) void { | ||
| 74 | + self.active = false; | ||
| 75 | + } | ||
| 76 | +}; | ||
+0 −42 src/animations/mod.rs deleted file mode 100644
| @@ -1,42 +0,0 @@ | |||
| 1 | -mod scroll; | ||
| 2 | - | ||
| 3 | -pub use scroll::ScrollAnimation; | ||
| 4 | - | ||
| 5 | -use std::time::Duration; | ||
| 6 | - | ||
| 7 | -#[derive(Debug, Clone, Copy)] | ||
| 8 | -pub enum Easing { | ||
| 9 | - Linear, | ||
| 10 | - EaseOut, | ||
| 11 | - EaseInOut, | ||
| 12 | -} | ||
| 13 | - | ||
| 14 | -impl Easing { | ||
| 15 | - pub fn apply(&self, t: f64) -> f64 { | ||
| 16 | - match self { | ||
| 17 | - Easing::Linear => t, | ||
| 18 | - Easing::EaseOut => 1.0 - (1.0 - t).powi(3), | ||
| 19 | - Easing::EaseInOut => { | ||
| 20 | - if t < 0.5 { | ||
| 21 | - 4.0 * t * t * t | ||
| 22 | - } else { | ||
| 23 | - 1.0 - (-2.0 * t + 2.0).powi(3) / 2.0 | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | - } | ||
| 27 | - } | ||
| 28 | -} | ||
| 29 | - | ||
| 30 | -pub struct AnimationConfig { | ||
| 31 | - pub duration: Duration, | ||
| 32 | - pub easing: Easing, | ||
| 33 | -} | ||
| 34 | - | ||
| 35 | -impl Default for AnimationConfig { | ||
| 36 | - fn default() -> Self { | ||
| 37 | - Self { | ||
| 38 | - duration: Duration::from_millis(150), | ||
| 39 | - easing: Easing::EaseOut, | ||
| 40 | - } | ||
| 41 | - } | ||
| 42 | -} | ||
+0 −76 src/animations/scroll.rs deleted file mode 100644
| @@ -1,76 +0,0 @@ | |||
| 1 | -use std::time::Instant; | ||
| 2 | -use super::{AnimationConfig, Easing}; | ||
| 3 | - | ||
| 4 | -pub struct ScrollAnimation { | ||
| 5 | - start_value: i32, | ||
| 6 | - end_value: i32, | ||
| 7 | - start_time: Instant, | ||
| 8 | - duration_ms: u64, | ||
| 9 | - easing: Easing, | ||
| 10 | - active: bool, | ||
| 11 | -} | ||
| 12 | - | ||
| 13 | -impl ScrollAnimation { | ||
| 14 | - pub fn new() -> Self { | ||
| 15 | - Self { | ||
| 16 | - start_value: 0, | ||
| 17 | - end_value: 0, | ||
| 18 | - start_time: Instant::now(), | ||
| 19 | - duration_ms: 150, | ||
| 20 | - easing: Easing::EaseOut, | ||
| 21 | - active: false, | ||
| 22 | - } | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | - pub fn start(&mut self, from: i32, to: i32, config: &AnimationConfig) { | ||
| 26 | - if from == to { | ||
| 27 | - self.active = false; | ||
| 28 | - return; | ||
| 29 | - } | ||
| 30 | - self.start_value = from; | ||
| 31 | - self.end_value = to; | ||
| 32 | - self.start_time = Instant::now(); | ||
| 33 | - self.duration_ms = config.duration.as_millis() as u64; | ||
| 34 | - self.easing = config.easing; | ||
| 35 | - self.active = true; | ||
| 36 | - } | ||
| 37 | - | ||
| 38 | - pub fn update(&mut self) -> Option<i32> { | ||
| 39 | - if !self.active { | ||
| 40 | - return None; | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - let elapsed = self.start_time.elapsed().as_millis() as u64; | ||
| 44 | - | ||
| 45 | - if elapsed >= self.duration_ms { | ||
| 46 | - self.active = false; | ||
| 47 | - return Some(self.end_value); | ||
| 48 | - } | ||
| 49 | - | ||
| 50 | - let t = elapsed as f64 / self.duration_ms as f64; | ||
| 51 | - let eased_t = self.easing.apply(t); | ||
| 52 | - | ||
| 53 | - let delta = (self.end_value - self.start_value) as f64; | ||
| 54 | - let current = self.start_value as f64 + delta * eased_t; | ||
| 55 | - | ||
| 56 | - Some(current.round() as i32) | ||
| 57 | - } | ||
| 58 | - | ||
| 59 | - pub fn is_active(&self) -> bool { | ||
| 60 | - self.active | ||
| 61 | - } | ||
| 62 | - | ||
| 63 | - pub fn target(&self) -> i32 { | ||
| 64 | - self.end_value | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - pub fn cancel(&mut self) { | ||
| 68 | - self.active = false; | ||
| 69 | - } | ||
| 70 | -} | ||
| 71 | - | ||
| 72 | -impl Default for ScrollAnimation { | ||
| 73 | - fn default() -> Self { | ||
| 74 | - Self::new() | ||
| 75 | - } | ||
| 76 | -} | ||
+0 −453 src/bar/bar.rs deleted file mode 100644
| @@ -1,453 +0,0 @@ | |||
| 1 | -use super::blocks::Block; | ||
| 2 | -use super::font::{DrawingSurface, Font}; | ||
| 3 | -use crate::Config; | ||
| 4 | -use crate::errors::X11Error; | ||
| 5 | -use std::time::Instant; | ||
| 6 | -use x11rb::COPY_DEPTH_FROM_PARENT; | ||
| 7 | -use x11rb::connection::Connection; | ||
| 8 | -use x11rb::protocol::xproto::*; | ||
| 9 | -use x11rb::rust_connection::RustConnection; | ||
| 10 | - | ||
| 11 | -pub struct Bar { | ||
| 12 | - window: Window, | ||
| 13 | - width: u16, | ||
| 14 | - height: u16, | ||
| 15 | - graphics_context: Gcontext, | ||
| 16 | - surface: DrawingSurface, | ||
| 17 | - | ||
| 18 | - tag_widths: Vec<u16>, | ||
| 19 | - needs_redraw: bool, | ||
| 20 | - | ||
| 21 | - blocks: Vec<Box<dyn Block>>, | ||
| 22 | - block_last_updates: Vec<Instant>, | ||
| 23 | - block_underlines: Vec<bool>, | ||
| 24 | - status_text: String, | ||
| 25 | - | ||
| 26 | - tags: Vec<String>, | ||
| 27 | - scheme_normal: crate::ColorScheme, | ||
| 28 | - scheme_occupied: crate::ColorScheme, | ||
| 29 | - scheme_selected: crate::ColorScheme, | ||
| 30 | - scheme_urgent: crate::ColorScheme, | ||
| 31 | - hide_vacant_tags: bool, | ||
| 32 | - last_occupied_tags: u32, | ||
| 33 | - last_current_tags: u32, | ||
| 34 | -} | ||
| 35 | - | ||
| 36 | -impl Bar { | ||
| 37 | - pub fn new( | ||
| 38 | - connection: &RustConnection, | ||
| 39 | - screen: &Screen, | ||
| 40 | - screen_num: usize, | ||
| 41 | - config: &Config, | ||
| 42 | - display: *mut x11::xlib::Display, | ||
| 43 | - font: &Font, | ||
| 44 | - x: i16, | ||
| 45 | - y: i16, | ||
| 46 | - width: u16, | ||
| 47 | - cursor: u32, | ||
| 48 | - ) -> Result<Self, X11Error> { | ||
| 49 | - let window = connection.generate_id()?; | ||
| 50 | - let graphics_context = connection.generate_id()?; | ||
| 51 | - | ||
| 52 | - let height = (font.height() as f32 * 1.4) as u16; | ||
| 53 | - | ||
| 54 | - connection.create_window( | ||
| 55 | - COPY_DEPTH_FROM_PARENT, | ||
| 56 | - window, | ||
| 57 | - screen.root, | ||
| 58 | - x, | ||
| 59 | - y, | ||
| 60 | - width, | ||
| 61 | - height, | ||
| 62 | - 0, | ||
| 63 | - WindowClass::INPUT_OUTPUT, | ||
| 64 | - screen.root_visual, | ||
| 65 | - &CreateWindowAux::new() | ||
| 66 | - .background_pixel(config.scheme_normal.background) | ||
| 67 | - .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) | ||
| 68 | - .override_redirect(1), | ||
| 69 | - )?; | ||
| 70 | - | ||
| 71 | - connection.create_gc( | ||
| 72 | - graphics_context, | ||
| 73 | - window, | ||
| 74 | - &CreateGCAux::new() | ||
| 75 | - .foreground(config.scheme_normal.foreground) | ||
| 76 | - .background(config.scheme_normal.background), | ||
| 77 | - )?; | ||
| 78 | - | ||
| 79 | - unsafe { | ||
| 80 | - x11::xlib::XDefineCursor(display, window as u64, cursor as u64); | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - connection.map_window(window)?; | ||
| 84 | - connection.flush()?; | ||
| 85 | - | ||
| 86 | - let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; | ||
| 87 | - let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; | ||
| 88 | - | ||
| 89 | - let surface = DrawingSurface::new( | ||
| 90 | - display, | ||
| 91 | - window as x11::xlib::Drawable, | ||
| 92 | - width as u32, | ||
| 93 | - height as u32, | ||
| 94 | - visual, | ||
| 95 | - colormap, | ||
| 96 | - )?; | ||
| 97 | - | ||
| 98 | - let horizontal_padding = (font.height() as f32 * 0.4) as u16; | ||
| 99 | - | ||
| 100 | - let tag_widths = config | ||
| 101 | - .tags | ||
| 102 | - .iter() | ||
| 103 | - .map(|tag| { | ||
| 104 | - let text_width = font.text_width(tag); | ||
| 105 | - text_width + (horizontal_padding * 2) | ||
| 106 | - }) | ||
| 107 | - .collect(); | ||
| 108 | - | ||
| 109 | - let blocks: Vec<Box<dyn Block>> = config | ||
| 110 | - .status_blocks | ||
| 111 | - .iter() | ||
| 112 | - .map(|block_config| block_config.to_block()) | ||
| 113 | - .collect(); | ||
| 114 | - | ||
| 115 | - let block_underlines: Vec<bool> = config | ||
| 116 | - .status_blocks | ||
| 117 | - .iter() | ||
| 118 | - .map(|block_config| block_config.underline) | ||
| 119 | - .collect(); | ||
| 120 | - | ||
| 121 | - let block_last_updates = vec![Instant::now(); blocks.len()]; | ||
| 122 | - | ||
| 123 | - Ok(Bar { | ||
| 124 | - window, | ||
| 125 | - width, | ||
| 126 | - height, | ||
| 127 | - graphics_context, | ||
| 128 | - surface, | ||
| 129 | - tag_widths, | ||
| 130 | - needs_redraw: true, | ||
| 131 | - blocks, | ||
| 132 | - block_last_updates, | ||
| 133 | - block_underlines, | ||
| 134 | - status_text: String::new(), | ||
| 135 | - tags: config.tags.clone(), | ||
| 136 | - scheme_normal: config.scheme_normal, | ||
| 137 | - scheme_occupied: config.scheme_occupied, | ||
| 138 | - scheme_selected: config.scheme_selected, | ||
| 139 | - scheme_urgent: config.scheme_urgent, | ||
| 140 | - hide_vacant_tags: config.hide_vacant_tags, | ||
| 141 | - last_occupied_tags: 0, | ||
| 142 | - last_current_tags: 0, | ||
| 143 | - }) | ||
| 144 | - } | ||
| 145 | - | ||
| 146 | - pub fn window(&self) -> Window { | ||
| 147 | - self.window | ||
| 148 | - } | ||
| 149 | - | ||
| 150 | - pub fn height(&self) -> u16 { | ||
| 151 | - self.height | ||
| 152 | - } | ||
| 153 | - | ||
| 154 | - pub fn invalidate(&mut self) { | ||
| 155 | - self.needs_redraw = true; | ||
| 156 | - } | ||
| 157 | - | ||
| 158 | - pub fn update_blocks(&mut self) { | ||
| 159 | - let now = Instant::now(); | ||
| 160 | - let mut changed = false; | ||
| 161 | - | ||
| 162 | - for (i, block) in self.blocks.iter_mut().enumerate() { | ||
| 163 | - let elapsed = now.duration_since(self.block_last_updates[i]); | ||
| 164 | - | ||
| 165 | - if elapsed >= block.interval() && block.content().is_ok() { | ||
| 166 | - self.block_last_updates[i] = now; | ||
| 167 | - changed = true; | ||
| 168 | - } | ||
| 169 | - } | ||
| 170 | - | ||
| 171 | - if changed { | ||
| 172 | - let mut parts = Vec::new(); | ||
| 173 | - for block in &mut self.blocks { | ||
| 174 | - if let Ok(text) = block.content() { | ||
| 175 | - parts.push(text); | ||
| 176 | - } | ||
| 177 | - } | ||
| 178 | - self.status_text = parts.join(""); | ||
| 179 | - self.needs_redraw = true; | ||
| 180 | - } | ||
| 181 | - } | ||
| 182 | - | ||
| 183 | - pub fn draw( | ||
| 184 | - &mut self, | ||
| 185 | - connection: &RustConnection, | ||
| 186 | - font: &Font, | ||
| 187 | - display: *mut x11::xlib::Display, | ||
| 188 | - current_tags: u32, | ||
| 189 | - occupied_tags: u32, | ||
| 190 | - urgent_tags: u32, | ||
| 191 | - draw_blocks: bool, | ||
| 192 | - layout_symbol: &str, | ||
| 193 | - keychord_indicator: Option<&str>, | ||
| 194 | - ) -> Result<(), X11Error> { | ||
| 195 | - if !self.needs_redraw { | ||
| 196 | - return Ok(()); | ||
| 197 | - } | ||
| 198 | - | ||
| 199 | - connection.change_gc( | ||
| 200 | - self.graphics_context, | ||
| 201 | - &ChangeGCAux::new().foreground(self.scheme_normal.background), | ||
| 202 | - )?; | ||
| 203 | - connection.flush()?; | ||
| 204 | - | ||
| 205 | - unsafe { | ||
| 206 | - let gc = x11::xlib::XCreateGC(display, self.surface.pixmap(), 0, std::ptr::null_mut()); | ||
| 207 | - x11::xlib::XSetForeground(display, gc, self.scheme_normal.background as u64); | ||
| 208 | - x11::xlib::XFillRectangle( | ||
| 209 | - display, | ||
| 210 | - self.surface.pixmap(), | ||
| 211 | - gc, | ||
| 212 | - 0, | ||
| 213 | - 0, | ||
| 214 | - self.width as u32, | ||
| 215 | - self.height as u32, | ||
| 216 | - ); | ||
| 217 | - x11::xlib::XFreeGC(display, gc); | ||
| 218 | - } | ||
| 219 | - | ||
| 220 | - self.last_occupied_tags = occupied_tags; | ||
| 221 | - self.last_current_tags = current_tags; | ||
| 222 | - | ||
| 223 | - let mut x_position: i16 = 0; | ||
| 224 | - | ||
| 225 | - for (tag_index, tag) in self.tags.iter().enumerate() { | ||
| 226 | - let tag_mask = 1 << tag_index; | ||
| 227 | - let is_selected = (current_tags & tag_mask) != 0; | ||
| 228 | - let is_occupied = (occupied_tags & tag_mask) != 0; | ||
| 229 | - let is_urgent = (urgent_tags & tag_mask) != 0; | ||
| 230 | - | ||
| 231 | - if self.hide_vacant_tags && !is_occupied && !is_selected { | ||
| 232 | - continue; | ||
| 233 | - } | ||
| 234 | - | ||
| 235 | - let tag_width = self.tag_widths[tag_index]; | ||
| 236 | - | ||
| 237 | - let scheme = if is_selected { | ||
| 238 | - &self.scheme_selected | ||
| 239 | - } else if is_urgent { | ||
| 240 | - &self.scheme_urgent | ||
| 241 | - } else if is_occupied { | ||
| 242 | - &self.scheme_occupied | ||
| 243 | - } else { | ||
| 244 | - &self.scheme_normal | ||
| 245 | - }; | ||
| 246 | - | ||
| 247 | - let text_width = font.text_width(tag); | ||
| 248 | - let text_x = x_position + ((tag_width - text_width) / 2) as i16; | ||
| 249 | - | ||
| 250 | - let top_padding = 4; | ||
| 251 | - let text_y = top_padding + font.ascent(); | ||
| 252 | - | ||
| 253 | - self.surface | ||
| 254 | - .font_draw() | ||
| 255 | - .draw_text(font, scheme.foreground, text_x, text_y, tag); | ||
| 256 | - | ||
| 257 | - if is_selected || is_urgent { | ||
| 258 | - let font_height = font.height(); | ||
| 259 | - let underline_height = font_height / 8; | ||
| 260 | - let bottom_gap = 3; | ||
| 261 | - let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; | ||
| 262 | - | ||
| 263 | - let underline_padding = 4; | ||
| 264 | - let underline_width = tag_width - underline_padding; | ||
| 265 | - let underline_x = x_position + (underline_padding / 2) as i16; | ||
| 266 | - | ||
| 267 | - unsafe { | ||
| 268 | - let gc = x11::xlib::XCreateGC( | ||
| 269 | - display, | ||
| 270 | - self.surface.pixmap(), | ||
| 271 | - 0, | ||
| 272 | - std::ptr::null_mut(), | ||
| 273 | - ); | ||
| 274 | - x11::xlib::XSetForeground(display, gc, scheme.underline as u64); | ||
| 275 | - x11::xlib::XFillRectangle( | ||
| 276 | - display, | ||
| 277 | - self.surface.pixmap(), | ||
| 278 | - gc, | ||
| 279 | - underline_x as i32, | ||
| 280 | - underline_y as i32, | ||
| 281 | - underline_width as u32, | ||
| 282 | - underline_height as u32, | ||
| 283 | - ); | ||
| 284 | - x11::xlib::XFreeGC(display, gc); | ||
| 285 | - } | ||
| 286 | - } | ||
| 287 | - | ||
| 288 | - x_position += tag_width as i16; | ||
| 289 | - } | ||
| 290 | - | ||
| 291 | - x_position += 10; | ||
| 292 | - | ||
| 293 | - let text_x = x_position; | ||
| 294 | - let top_padding = 4; | ||
| 295 | - let text_y = top_padding + font.ascent(); | ||
| 296 | - | ||
| 297 | - self.surface.font_draw().draw_text( | ||
| 298 | - font, | ||
| 299 | - self.scheme_normal.foreground, | ||
| 300 | - text_x, | ||
| 301 | - text_y, | ||
| 302 | - layout_symbol, | ||
| 303 | - ); | ||
| 304 | - | ||
| 305 | - x_position += font.text_width(layout_symbol) as i16; | ||
| 306 | - | ||
| 307 | - if let Some(indicator) = keychord_indicator { | ||
| 308 | - x_position += 10; | ||
| 309 | - | ||
| 310 | - let text_x = x_position; | ||
| 311 | - let text_y = top_padding + font.ascent(); | ||
| 312 | - | ||
| 313 | - self.surface.font_draw().draw_text( | ||
| 314 | - font, | ||
| 315 | - self.scheme_selected.foreground, | ||
| 316 | - text_x, | ||
| 317 | - text_y, | ||
| 318 | - indicator, | ||
| 319 | - ); | ||
| 320 | - } | ||
| 321 | - | ||
| 322 | - if draw_blocks && !self.status_text.is_empty() { | ||
| 323 | - let padding = 10; | ||
| 324 | - let mut x_position = self.width as i16 - padding; | ||
| 325 | - | ||
| 326 | - for (i, block) in self.blocks.iter_mut().enumerate().rev() { | ||
| 327 | - if let Ok(text) = block.content() { | ||
| 328 | - let text_width = font.text_width(&text); | ||
| 329 | - x_position -= text_width as i16; | ||
| 330 | - | ||
| 331 | - let top_padding = 4; | ||
| 332 | - let text_y = top_padding + font.ascent(); | ||
| 333 | - | ||
| 334 | - self.surface.font_draw().draw_text( | ||
| 335 | - font, | ||
| 336 | - block.color(), | ||
| 337 | - x_position, | ||
| 338 | - text_y, | ||
| 339 | - &text, | ||
| 340 | - ); | ||
| 341 | - | ||
| 342 | - if self.block_underlines[i] { | ||
| 343 | - let font_height = font.height(); | ||
| 344 | - let underline_height = font_height / 8; | ||
| 345 | - let bottom_gap = 3; | ||
| 346 | - let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; | ||
| 347 | - | ||
| 348 | - let underline_padding = 8; | ||
| 349 | - let underline_width = text_width + underline_padding; | ||
| 350 | - let underline_x = x_position - (underline_padding / 2) as i16; | ||
| 351 | - | ||
| 352 | - unsafe { | ||
| 353 | - let gc = x11::xlib::XCreateGC( | ||
| 354 | - display, | ||
| 355 | - self.surface.pixmap(), | ||
| 356 | - 0, | ||
| 357 | - std::ptr::null_mut(), | ||
| 358 | - ); | ||
| 359 | - x11::xlib::XSetForeground(display, gc, block.color() as u64); | ||
| 360 | - x11::xlib::XFillRectangle( | ||
| 361 | - display, | ||
| 362 | - self.surface.pixmap(), | ||
| 363 | - gc, | ||
| 364 | - underline_x as i32, | ||
| 365 | - underline_y as i32, | ||
| 366 | - underline_width as u32, | ||
| 367 | - underline_height as u32, | ||
| 368 | - ); | ||
| 369 | - x11::xlib::XFreeGC(display, gc); | ||
| 370 | - } | ||
| 371 | - } | ||
| 372 | - } | ||
| 373 | - } | ||
| 374 | - } | ||
| 375 | - | ||
| 376 | - unsafe { | ||
| 377 | - let gc = x11::xlib::XCreateGC( | ||
| 378 | - display, | ||
| 379 | - self.window as x11::xlib::Drawable, | ||
| 380 | - 0, | ||
| 381 | - std::ptr::null_mut(), | ||
| 382 | - ); | ||
| 383 | - x11::xlib::XCopyArea( | ||
| 384 | - display, | ||
| 385 | - self.surface.pixmap(), | ||
| 386 | - self.window as x11::xlib::Drawable, | ||
| 387 | - gc, | ||
| 388 | - 0, | ||
| 389 | - 0, | ||
| 390 | - self.width as u32, | ||
| 391 | - self.height as u32, | ||
| 392 | - 0, | ||
| 393 | - 0, | ||
| 394 | - ); | ||
| 395 | - x11::xlib::XFreeGC(display, gc); | ||
| 396 | - x11::xlib::XSync(display, 1); | ||
| 397 | - } | ||
| 398 | - | ||
| 399 | - self.needs_redraw = false; | ||
| 400 | - | ||
| 401 | - Ok(()) | ||
| 402 | - } | ||
| 403 | - | ||
| 404 | - pub fn handle_click(&self, click_x: i16) -> Option<usize> { | ||
| 405 | - let mut current_x_position = 0; | ||
| 406 | - | ||
| 407 | - for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() { | ||
| 408 | - let tag_mask = 1 << tag_index; | ||
| 409 | - let is_selected = (self.last_current_tags & tag_mask) != 0; | ||
| 410 | - let is_occupied = (self.last_occupied_tags & tag_mask) != 0; | ||
| 411 | - | ||
| 412 | - if self.hide_vacant_tags && !is_occupied && !is_selected { | ||
| 413 | - continue; | ||
| 414 | - } | ||
| 415 | - | ||
| 416 | - if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 { | ||
| 417 | - return Some(tag_index); | ||
| 418 | - } | ||
| 419 | - current_x_position += tag_width as i16; | ||
| 420 | - } | ||
| 421 | - None | ||
| 422 | - } | ||
| 423 | - | ||
| 424 | - pub fn needs_redraw(&self) -> bool { | ||
| 425 | - self.needs_redraw | ||
| 426 | - } | ||
| 427 | - | ||
| 428 | - pub fn update_from_config(&mut self, config: &Config) { | ||
| 429 | - self.blocks = config | ||
| 430 | - .status_blocks | ||
| 431 | - .iter() | ||
| 432 | - .map(|block_config| block_config.to_block()) | ||
| 433 | - .collect(); | ||
| 434 | - | ||
| 435 | - self.block_underlines = config | ||
| 436 | - .status_blocks | ||
| 437 | - .iter() | ||
| 438 | - .map(|block_config| block_config.underline) | ||
| 439 | - .collect(); | ||
| 440 | - | ||
| 441 | - self.block_last_updates = vec![Instant::now(); self.blocks.len()]; | ||
| 442 | - | ||
| 443 | - self.tags = config.tags.clone(); | ||
| 444 | - self.scheme_normal = config.scheme_normal; | ||
| 445 | - self.scheme_occupied = config.scheme_occupied; | ||
| 446 | - self.scheme_selected = config.scheme_selected; | ||
| 447 | - self.scheme_urgent = config.scheme_urgent; | ||
| 448 | - self.hide_vacant_tags = config.hide_vacant_tags; | ||
| 449 | - | ||
| 450 | - self.status_text.clear(); | ||
| 451 | - self.needs_redraw = true; | ||
| 452 | - } | ||
| 453 | -} | ||
+359 −0 src/bar/bar.zig new file mode 100644
| @@ -0,0 +1,359 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const xlib = @import("../x11/xlib.zig"); | ||
| 3 | +const monitor_mod = @import("../monitor.zig"); | ||
| 4 | +const client_mod = @import("../client.zig"); | ||
| 5 | +const blocks_mod = @import("blocks/blocks.zig"); | ||
| 6 | +const config_mod = @import("../config/config.zig"); | ||
| 7 | + | ||
| 8 | +const Monitor = monitor_mod.Monitor; | ||
| 9 | +const Block = blocks_mod.Block; | ||
| 10 | + | ||
| 11 | +fn get_layout_symbol(layout_index: u32) []const u8 { | ||
| 12 | + const cfg = config_mod.get_config(); | ||
| 13 | + if (cfg) |conf| { | ||
| 14 | + return switch (layout_index) { | ||
| 15 | + 0 => conf.layout_tile_symbol, | ||
| 16 | + 1 => conf.layout_monocle_symbol, | ||
| 17 | + 2 => conf.layout_floating_symbol, | ||
| 18 | + 3 => "[S]", | ||
| 19 | + else => "[?]", | ||
| 20 | + }; | ||
| 21 | + } | ||
| 22 | + return switch (layout_index) { | ||
| 23 | + 0 => "[]=", | ||
| 24 | + 1 => "[M]", | ||
| 25 | + 2 => "><>", | ||
| 26 | + 3 => "[S]", | ||
| 27 | + else => "[?]", | ||
| 28 | + }; | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +pub const ColorScheme = struct { | ||
| 32 | + foreground: c_ulong, | ||
| 33 | + background: c_ulong, | ||
| 34 | + border: c_ulong, | ||
| 35 | +}; | ||
| 36 | + | ||
| 37 | +pub const Bar = struct { | ||
| 38 | + window: xlib.Window, | ||
| 39 | + pixmap: xlib.Pixmap, | ||
| 40 | + graphics_context: xlib.GC, | ||
| 41 | + xft_draw: ?*xlib.XftDraw, | ||
| 42 | + width: i32, | ||
| 43 | + height: i32, | ||
| 44 | + monitor: *Monitor, | ||
| 45 | + | ||
| 46 | + font: ?*xlib.XftFont, | ||
| 47 | + font_height: i32, | ||
| 48 | + | ||
| 49 | + scheme_normal: ColorScheme, | ||
| 50 | + scheme_selected: ColorScheme, | ||
| 51 | + scheme_occupied: ColorScheme, | ||
| 52 | + scheme_urgent: ColorScheme, | ||
| 53 | + | ||
| 54 | + allocator: std.mem.Allocator, | ||
| 55 | + blocks: std.ArrayList(Block), | ||
| 56 | + needs_redraw: bool, | ||
| 57 | + next: ?*Bar, | ||
| 58 | + | ||
| 59 | + pub fn create( | ||
| 60 | + allocator: std.mem.Allocator, | ||
| 61 | + display: *xlib.Display, | ||
| 62 | + screen: c_int, | ||
| 63 | + monitor: *Monitor, | ||
| 64 | + font_name: []const u8, | ||
| 65 | + ) ?*Bar { | ||
| 66 | + const bar = allocator.create(Bar) catch return null; | ||
| 67 | + | ||
| 68 | + const visual = xlib.XDefaultVisual(display, screen); | ||
| 69 | + const colormap = xlib.XDefaultColormap(display, screen); | ||
| 70 | + const depth = xlib.XDefaultDepth(display, screen); | ||
| 71 | + const root = xlib.XRootWindow(display, screen); | ||
| 72 | + | ||
| 73 | + const font_name_z = allocator.dupeZ(u8, font_name) catch return null; | ||
| 74 | + defer allocator.free(font_name_z); | ||
| 75 | + | ||
| 76 | + const font = xlib.XftFontOpenName(display, screen, font_name_z); | ||
| 77 | + if (font == null) { | ||
| 78 | + allocator.destroy(bar); | ||
| 79 | + return null; | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + const font_height = font.*.ascent + font.*.descent; | ||
| 83 | + const bar_height: i32 = @intCast(@as(i32, font_height) + 8); | ||
| 84 | + | ||
| 85 | + const window = xlib.c.XCreateSimpleWindow( | ||
| 86 | + display, | ||
| 87 | + root, | ||
| 88 | + monitor.mon_x, | ||
| 89 | + monitor.mon_y, | ||
| 90 | + @intCast(monitor.mon_w), | ||
| 91 | + @intCast(bar_height), | ||
| 92 | + 0, | ||
| 93 | + 0, | ||
| 94 | + 0x1a1b26, | ||
| 95 | + ); | ||
| 96 | + | ||
| 97 | + _ = xlib.c.XSetWindowAttributes{}; | ||
| 98 | + var attributes: xlib.c.XSetWindowAttributes = undefined; | ||
| 99 | + attributes.override_redirect = xlib.True; | ||
| 100 | + attributes.event_mask = xlib.c.ExposureMask | xlib.c.ButtonPressMask; | ||
| 101 | + _ = xlib.c.XChangeWindowAttributes(display, window, xlib.c.CWOverrideRedirect | xlib.c.CWEventMask, &attributes); | ||
| 102 | + | ||
| 103 | + const pixmap = xlib.XCreatePixmap( | ||
| 104 | + display, | ||
| 105 | + window, | ||
| 106 | + @intCast(monitor.mon_w), | ||
| 107 | + @intCast(bar_height), | ||
| 108 | + @intCast(depth), | ||
| 109 | + ); | ||
| 110 | + | ||
| 111 | + const graphics_context = xlib.XCreateGC(display, pixmap, 0, null); | ||
| 112 | + | ||
| 113 | + const xft_draw = xlib.XftDrawCreate(display, pixmap, visual, colormap); | ||
| 114 | + | ||
| 115 | + _ = xlib.XMapWindow(display, window); | ||
| 116 | + | ||
| 117 | + const cfg = config_mod.get_config(); | ||
| 118 | + const scheme_normal = if (cfg) |c| ColorScheme{ .foreground = c.scheme_normal.fg, .background = c.scheme_normal.bg, .border = c.scheme_normal.border } else ColorScheme{ .foreground = 0xbbbbbb, .background = 0x1a1b26, .border = 0x444444 }; | ||
| 119 | + const scheme_selected = if (cfg) |c| ColorScheme{ .foreground = c.scheme_selected.fg, .background = c.scheme_selected.bg, .border = c.scheme_selected.border } else ColorScheme{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0xad8ee6 }; | ||
| 120 | + const scheme_occupied = if (cfg) |c| ColorScheme{ .foreground = c.scheme_occupied.fg, .background = c.scheme_occupied.bg, .border = c.scheme_occupied.border } else ColorScheme{ .foreground = 0x0db9d7, .background = 0x1a1b26, .border = 0x0db9d7 }; | ||
| 121 | + const scheme_urgent = if (cfg) |c| ColorScheme{ .foreground = c.scheme_urgent.fg, .background = c.scheme_urgent.bg, .border = c.scheme_urgent.border } else ColorScheme{ .foreground = 0xf7768e, .background = 0x1a1b26, .border = 0xf7768e }; | ||
| 122 | + | ||
| 123 | + bar.* = Bar{ | ||
| 124 | + .window = window, | ||
| 125 | + .pixmap = pixmap, | ||
| 126 | + .graphics_context = graphics_context, | ||
| 127 | + .xft_draw = xft_draw, | ||
| 128 | + .width = monitor.mon_w, | ||
| 129 | + .height = bar_height, | ||
| 130 | + .monitor = monitor, | ||
| 131 | + .font = font, | ||
| 132 | + .font_height = font_height, | ||
| 133 | + .scheme_normal = scheme_normal, | ||
| 134 | + .scheme_selected = scheme_selected, | ||
| 135 | + .scheme_occupied = scheme_occupied, | ||
| 136 | + .scheme_urgent = scheme_urgent, | ||
| 137 | + .allocator = allocator, | ||
| 138 | + .blocks = .{}, | ||
| 139 | + .needs_redraw = true, | ||
| 140 | + .next = null, | ||
| 141 | + }; | ||
| 142 | + | ||
| 143 | + monitor.bar_win = window; | ||
| 144 | + monitor.win_y = monitor.mon_y + bar_height; | ||
| 145 | + monitor.win_h = monitor.mon_h - bar_height; | ||
| 146 | + | ||
| 147 | + return bar; | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + pub fn destroy(self: *Bar, allocator: std.mem.Allocator, display: *xlib.Display) void { | ||
| 151 | + if (self.xft_draw) |xft_draw| { | ||
| 152 | + xlib.XftDrawDestroy(xft_draw); | ||
| 153 | + } | ||
| 154 | + if (self.font) |font| { | ||
| 155 | + xlib.XftFontClose(display, font); | ||
| 156 | + } | ||
| 157 | + _ = xlib.XFreeGC(display, self.graphics_context); | ||
| 158 | + _ = xlib.XFreePixmap(display, self.pixmap); | ||
| 159 | + _ = xlib.c.XDestroyWindow(display, self.window); | ||
| 160 | + self.blocks.deinit(self.allocator); | ||
| 161 | + allocator.destroy(self); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + pub fn add_block(self: *Bar, block: Block) void { | ||
| 165 | + self.blocks.append(self.allocator, block) catch {}; | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + pub fn invalidate(self: *Bar) void { | ||
| 169 | + self.needs_redraw = true; | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + pub fn draw(self: *Bar, display: *xlib.Display, tags: []const []const u8) void { | ||
| 173 | + if (!self.needs_redraw) return; | ||
| 174 | + | ||
| 175 | + self.fill_rect(display, 0, 0, self.width, self.height, self.scheme_normal.background); | ||
| 176 | + | ||
| 177 | + var x_position: i32 = 0; | ||
| 178 | + const padding: i32 = 8; | ||
| 179 | + const monitor = self.monitor; | ||
| 180 | + const current_tags = monitor.tagset[monitor.sel_tags]; | ||
| 181 | + | ||
| 182 | + for (tags, 0..) |tag, index| { | ||
| 183 | + const tag_mask: u32 = @as(u32, 1) << @intCast(index); | ||
| 184 | + const is_selected = (current_tags & tag_mask) != 0; | ||
| 185 | + const is_occupied = has_clients_on_tag(monitor, tag_mask); | ||
| 186 | + | ||
| 187 | + const scheme = if (is_selected) self.scheme_selected else if (is_occupied) self.scheme_occupied else self.scheme_normal; | ||
| 188 | + | ||
| 189 | + const tag_text_width = self.text_width(display, tag); | ||
| 190 | + const tag_width = tag_text_width + padding * 2; | ||
| 191 | + | ||
| 192 | + if (is_selected) { | ||
| 193 | + self.fill_rect(display, x_position, self.height - 3, tag_width, 3, scheme.border); | ||
| 194 | + } | ||
| 195 | + | ||
| 196 | + const text_y = @divTrunc(self.height + self.font_height, 2) - 4; | ||
| 197 | + self.draw_text(display, x_position + padding, text_y, tag, scheme.foreground); | ||
| 198 | + | ||
| 199 | + x_position += tag_width; | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + x_position += padding; | ||
| 203 | + | ||
| 204 | + const layout_symbol = get_layout_symbol(monitor.sel_lt); | ||
| 205 | + self.draw_text(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground); | ||
| 206 | + x_position += self.text_width(display, layout_symbol) + padding; | ||
| 207 | + | ||
| 208 | + var block_x: i32 = self.width - padding; | ||
| 209 | + var block_index: usize = self.blocks.items.len; | ||
| 210 | + while (block_index > 0) { | ||
| 211 | + block_index -= 1; | ||
| 212 | + const block = &self.blocks.items[block_index]; | ||
| 213 | + const content = block.get_content(); | ||
| 214 | + const content_width = self.text_width(display, content); | ||
| 215 | + block_x -= content_width; | ||
| 216 | + self.draw_text(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color()); | ||
| 217 | + if (block.underline) { | ||
| 218 | + self.fill_rect(display, block_x, self.height - 2, content_width, 2, block.color()); | ||
| 219 | + } | ||
| 220 | + block_x -= padding; | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); | ||
| 224 | + _ = xlib.XSync(display, xlib.False); | ||
| 225 | + | ||
| 226 | + self.needs_redraw = false; | ||
| 227 | + } | ||
| 228 | + | ||
| 229 | + fn fill_rect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void { | ||
| 230 | + _ = xlib.XSetForeground(display, self.graphics_context, color); | ||
| 231 | + _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height)); | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + fn draw_text(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void { | ||
| 235 | + if (self.xft_draw == null or self.font == null) return; | ||
| 236 | + | ||
| 237 | + var xft_color: xlib.XftColor = undefined; | ||
| 238 | + var render_color: xlib.XRenderColor = undefined; | ||
| 239 | + render_color.red = @intCast((color >> 16 & 0xff) * 257); | ||
| 240 | + render_color.green = @intCast((color >> 8 & 0xff) * 257); | ||
| 241 | + render_color.blue = @intCast((color & 0xff) * 257); | ||
| 242 | + render_color.alpha = 0xffff; | ||
| 243 | + | ||
| 244 | + const visual = xlib.XDefaultVisual(display, 0); | ||
| 245 | + const colormap = xlib.XDefaultColormap(display, 0); | ||
| 246 | + | ||
| 247 | + _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color); | ||
| 248 | + | ||
| 249 | + xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len)); | ||
| 250 | + | ||
| 251 | + xlib.XftColorFree(display, visual, colormap, &xft_color); | ||
| 252 | + } | ||
| 253 | + | ||
| 254 | + fn text_width(self: *Bar, display: *xlib.Display, text: []const u8) i32 { | ||
| 255 | + if (self.font == null) return 0; | ||
| 256 | + | ||
| 257 | + var extents: xlib.XGlyphInfo = undefined; | ||
| 258 | + xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents); | ||
| 259 | + return extents.xOff; | ||
| 260 | + } | ||
| 261 | + | ||
| 262 | + pub fn handle_click(self: *Bar, click_x: i32, tags: []const []const u8) ?usize { | ||
| 263 | + var x_position: i32 = 0; | ||
| 264 | + const padding: i32 = 8; | ||
| 265 | + const display = xlib.c.XOpenDisplay(null) orelse return null; | ||
| 266 | + defer _ = xlib.XCloseDisplay(display); | ||
| 267 | + | ||
| 268 | + for (tags, 0..) |tag, index| { | ||
| 269 | + const tag_text_width = self.text_width(display, tag); | ||
| 270 | + const tag_width = tag_text_width + padding * 2; | ||
| 271 | + | ||
| 272 | + if (click_x >= x_position and click_x < x_position + tag_width) { | ||
| 273 | + return index; | ||
| 274 | + } | ||
| 275 | + x_position += tag_width; | ||
| 276 | + } | ||
| 277 | + return null; | ||
| 278 | + } | ||
| 279 | + | ||
| 280 | + pub fn update_blocks(self: *Bar) void { | ||
| 281 | + var changed = false; | ||
| 282 | + for (self.blocks.items) |*block| { | ||
| 283 | + if (block.update()) { | ||
| 284 | + changed = true; | ||
| 285 | + } | ||
| 286 | + } | ||
| 287 | + if (changed) { | ||
| 288 | + self.needs_redraw = true; | ||
| 289 | + } | ||
| 290 | + } | ||
| 291 | + | ||
| 292 | + pub fn clear_blocks(self: *Bar) void { | ||
| 293 | + self.blocks.clearRetainingCapacity(); | ||
| 294 | + } | ||
| 295 | +}; | ||
| 296 | + | ||
| 297 | +fn has_clients_on_tag(monitor: *Monitor, tag_mask: u32) bool { | ||
| 298 | + var current = monitor.clients; | ||
| 299 | + while (current) |client| { | ||
| 300 | + if ((client.tags & tag_mask) != 0) { | ||
| 301 | + return true; | ||
| 302 | + } | ||
| 303 | + current = client.next; | ||
| 304 | + } | ||
| 305 | + return false; | ||
| 306 | +} | ||
| 307 | + | ||
| 308 | +pub var bars: ?*Bar = null; | ||
| 309 | + | ||
| 310 | +pub fn create_bars(allocator: std.mem.Allocator, display: *xlib.Display, screen: c_int) void { | ||
| 311 | + var current_monitor = monitor_mod.monitors; | ||
| 312 | + while (current_monitor) |monitor| { | ||
| 313 | + const bar = Bar.create(allocator, display, screen, monitor, "monospace:size=10"); | ||
| 314 | + if (bar) |created_bar| { | ||
| 315 | + bars = created_bar; | ||
| 316 | + } | ||
| 317 | + current_monitor = monitor.next; | ||
| 318 | + } | ||
| 319 | +} | ||
| 320 | + | ||
| 321 | +pub fn draw_bars(display: *xlib.Display, tags: []const []const u8) void { | ||
| 322 | + var current_monitor = monitor_mod.monitors; | ||
| 323 | + while (current_monitor) |monitor| { | ||
| 324 | + _ = monitor; | ||
| 325 | + if (bars) |bar| { | ||
| 326 | + bar.draw(display, tags); | ||
| 327 | + } | ||
| 328 | + current_monitor = if (current_monitor) |m| m.next else null; | ||
| 329 | + } | ||
| 330 | +} | ||
| 331 | + | ||
| 332 | +pub fn invalidate_bars() void { | ||
| 333 | + var current = bars; | ||
| 334 | + while (current) |bar| { | ||
| 335 | + bar.invalidate(); | ||
| 336 | + current = bar.next; | ||
| 337 | + } | ||
| 338 | +} | ||
| 339 | + | ||
| 340 | +pub fn destroy_bars(allocator: std.mem.Allocator, display: *xlib.Display) void { | ||
| 341 | + var current = bars; | ||
| 342 | + while (current) |bar| { | ||
| 343 | + const next = bar.next; | ||
| 344 | + bar.destroy(allocator, display); | ||
| 345 | + current = next; | ||
| 346 | + } | ||
| 347 | + bars = null; | ||
| 348 | +} | ||
| 349 | + | ||
| 350 | +pub fn window_to_bar(win: xlib.Window) ?*Bar { | ||
| 351 | + var current = bars; | ||
| 352 | + while (current) |bar| { | ||
| 353 | + if (bar.window == win) { | ||
| 354 | + return bar; | ||
| 355 | + } | ||
| 356 | + current = bar.next; | ||
| 357 | + } | ||
| 358 | + return null; | ||
| 359 | +} | ||
+0 −107 src/bar/blocks/battery.rs deleted file mode 100644
| @@ -1,107 +0,0 @@ | |||
| 1 | -use super::Block; | ||
| 2 | -use crate::errors::BlockError; | ||
| 3 | -use std::fs; | ||
| 4 | -use std::path::{Path, PathBuf}; | ||
| 5 | -use std::time::Duration; | ||
| 6 | - | ||
| 7 | -pub struct Battery { | ||
| 8 | - format_charging: String, | ||
| 9 | - format_discharging: String, | ||
| 10 | - format_full: String, | ||
| 11 | - interval: Duration, | ||
| 12 | - color: u32, | ||
| 13 | - battery_path: String, | ||
| 14 | -} | ||
| 15 | - | ||
| 16 | -fn detect_battery_name() -> Option<String> { | ||
| 17 | - let base = Path::new("/sys/class/power_supply"); | ||
| 18 | - let entries = fs::read_dir(base).ok()?; | ||
| 19 | - | ||
| 20 | - for entry in entries.flatten() { | ||
| 21 | - let path: PathBuf = entry.path(); | ||
| 22 | - | ||
| 23 | - let type_path = path.join("type"); | ||
| 24 | - let present_path = path.join("present"); | ||
| 25 | - | ||
| 26 | - let is_battery = fs::read_to_string(&type_path) | ||
| 27 | - .map(|s| s.trim() == "Battery") | ||
| 28 | - .unwrap_or(false); | ||
| 29 | - | ||
| 30 | - let is_present = fs::read_to_string(&present_path) | ||
| 31 | - .map(|s| s.trim() == "1") | ||
| 32 | - .unwrap_or(true); | ||
| 33 | - | ||
| 34 | - let is_device_scope = fs::read_to_string(path.join("scope")) | ||
| 35 | - .map(|s| s.trim().eq_ignore_ascii_case("device")) | ||
| 36 | - .unwrap_or(false); | ||
| 37 | - | ||
| 38 | - if !is_battery || !is_present || is_device_scope { | ||
| 39 | - continue; | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - if let Some(name) = path.file_name().and_then(|n| n.to_str()) { | ||
| 43 | - return Some(name.to_string()); | ||
| 44 | - } | ||
| 45 | - } | ||
| 46 | - None | ||
| 47 | -} | ||
| 48 | - | ||
| 49 | -impl Battery { | ||
| 50 | - pub fn new( | ||
| 51 | - format_charging: &str, | ||
| 52 | - format_discharging: &str, | ||
| 53 | - format_full: &str, | ||
| 54 | - interval_secs: u64, | ||
| 55 | - color: u32, | ||
| 56 | - battery_name: Option<String>, | ||
| 57 | - ) -> Self { | ||
| 58 | - let name = battery_name | ||
| 59 | - .or_else(detect_battery_name) | ||
| 60 | - .unwrap_or_else(|| "BAT0".to_string()); | ||
| 61 | - | ||
| 62 | - Self { | ||
| 63 | - format_charging: format_charging.to_string(), | ||
| 64 | - format_discharging: format_discharging.to_string(), | ||
| 65 | - format_full: format_full.to_string(), | ||
| 66 | - interval: Duration::from_secs(interval_secs), | ||
| 67 | - color, | ||
| 68 | - battery_path: format!("/sys/class/power_supply/{}", name), | ||
| 69 | - } | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - fn read_file(&self, filename: &str) -> Result<String, BlockError> { | ||
| 73 | - let path = format!("{}/{}", self.battery_path, filename); | ||
| 74 | - Ok(fs::read_to_string(path)?.trim().to_string()) | ||
| 75 | - } | ||
| 76 | - | ||
| 77 | - fn get_capacity(&self) -> Result<u32, BlockError> { | ||
| 78 | - Ok(self.read_file("capacity")?.parse()?) | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - fn get_status(&self) -> Result<String, BlockError> { | ||
| 82 | - self.read_file("status") | ||
| 83 | - } | ||
| 84 | -} | ||
| 85 | - | ||
| 86 | -impl Block for Battery { | ||
| 87 | - fn content(&mut self) -> Result<String, BlockError> { | ||
| 88 | - let capacity = self.get_capacity()?; | ||
| 89 | - let status = self.get_status()?; | ||
| 90 | - | ||
| 91 | - let format = match status.as_str() { | ||
| 92 | - "Charging" => &self.format_charging, | ||
| 93 | - "Full" => &self.format_full, | ||
| 94 | - _ => &self.format_discharging, | ||
| 95 | - }; | ||
| 96 | - | ||
| 97 | - Ok(format.replace("{}", &capacity.to_string())) | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - fn interval(&self) -> Duration { | ||
| 101 | - self.interval | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - fn color(&self) -> u32 { | ||
| 105 | - self.color | ||
| 106 | - } | ||
| 107 | -} | ||
+87 −0 src/bar/blocks/battery.zig new file mode 100644
| @@ -0,0 +1,87 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const format_util = @import("format.zig"); | ||
| 3 | + | ||
| 4 | +pub const Battery = struct { | ||
| 5 | + format_charging: []const u8, | ||
| 6 | + format_discharging: []const u8, | ||
| 7 | + format_full: []const u8, | ||
| 8 | + battery_name: []const u8, | ||
| 9 | + interval_secs: u64, | ||
| 10 | + color: c_ulong, | ||
| 11 | + | ||
| 12 | + pub fn init( | ||
| 13 | + format_charging: []const u8, | ||
| 14 | + format_discharging: []const u8, | ||
| 15 | + format_full: []const u8, | ||
| 16 | + battery_name: []const u8, | ||
| 17 | + interval_secs: u64, | ||
| 18 | + color: c_ulong, | ||
| 19 | + ) Battery { | ||
| 20 | + return .{ | ||
| 21 | + .format_charging = format_charging, | ||
| 22 | + .format_discharging = format_discharging, | ||
| 23 | + .format_full = format_full, | ||
| 24 | + .battery_name = if (battery_name.len > 0) battery_name else "BAT0", | ||
| 25 | + .interval_secs = interval_secs, | ||
| 26 | + .color = color, | ||
| 27 | + }; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + pub fn content(self: *Battery, buffer: []u8) []const u8 { | ||
| 31 | + var path_buf: [128]u8 = undefined; | ||
| 32 | + | ||
| 33 | + const capacity = self.read_battery_file(&path_buf, "capacity") orelse return buffer[0..0]; | ||
| 34 | + const status = self.read_battery_status(&path_buf) orelse return buffer[0..0]; | ||
| 35 | + | ||
| 36 | + const format = switch (status) { | ||
| 37 | + .charging => self.format_charging, | ||
| 38 | + .discharging => self.format_discharging, | ||
| 39 | + .full => self.format_full, | ||
| 40 | + }; | ||
| 41 | + | ||
| 42 | + var cap_buf: [8]u8 = undefined; | ||
| 43 | + const cap_str = std.fmt.bufPrint(&cap_buf, "{d}", .{capacity}) catch return buffer[0..0]; | ||
| 44 | + | ||
| 45 | + return format_util.substitute(format, cap_str, buffer); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + const Status = enum { charging, discharging, full }; | ||
| 49 | + | ||
| 50 | + fn read_battery_status(self: *Battery, path_buf: *[128]u8) ?Status { | ||
| 51 | + const path = std.fmt.bufPrint(path_buf, "/sys/class/power_supply/{s}/status", .{self.battery_name}) catch return null; | ||
| 52 | + | ||
| 53 | + const file = std.fs.openFileAbsolute(path, .{}) catch return null; | ||
| 54 | + defer file.close(); | ||
| 55 | + | ||
| 56 | + var buf: [32]u8 = undefined; | ||
| 57 | + const len = file.read(&buf) catch return null; | ||
| 58 | + const status_str = std.mem.trim(u8, buf[0..len], " \n\r\t"); | ||
| 59 | + | ||
| 60 | + if (std.mem.eql(u8, status_str, "Charging")) return .charging; | ||
| 61 | + if (std.mem.eql(u8, status_str, "Discharging")) return .discharging; | ||
| 62 | + if (std.mem.eql(u8, status_str, "Full")) return .full; | ||
| 63 | + if (std.mem.eql(u8, status_str, "Not charging")) return .full; | ||
| 64 | + return .discharging; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + fn read_battery_file(self: *Battery, path_buf: *[128]u8, file_name: []const u8) ?u8 { | ||
| 68 | + const path = std.fmt.bufPrint(path_buf, "/sys/class/power_supply/{s}/{s}", .{ self.battery_name, file_name }) catch return null; | ||
| 69 | + | ||
| 70 | + const file = std.fs.openFileAbsolute(path, .{}) catch return null; | ||
| 71 | + defer file.close(); | ||
| 72 | + | ||
| 73 | + var buf: [16]u8 = undefined; | ||
| 74 | + const len = file.read(&buf) catch return null; | ||
| 75 | + const value_str = std.mem.trim(u8, buf[0..len], " \n\r\t"); | ||
| 76 | + | ||
| 77 | + return std.fmt.parseInt(u8, value_str, 10) catch null; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + pub fn interval(self: *Battery) u64 { | ||
| 81 | + return self.interval_secs; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + pub fn get_color(self: *Battery) c_ulong { | ||
| 85 | + return self.color; | ||
| 86 | + } | ||
| 87 | +}; | ||
+161 −0 src/bar/blocks/blocks.zig new file mode 100644
| @@ -0,0 +1,161 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +pub const Static = @import("static.zig").Static; | ||
| 4 | +pub const Date_Time = @import("datetime.zig").Date_Time; | ||
| 5 | +pub const Ram = @import("ram.zig").Ram; | ||
| 6 | +pub const Shell = @import("shell.zig").Shell; | ||
| 7 | +pub const Battery = @import("battery.zig").Battery; | ||
| 8 | +pub const Cpu_Temp = @import("cpu_temp.zig").Cpu_Temp; | ||
| 9 | + | ||
| 10 | +pub const Block_Type = enum { | ||
| 11 | + static, | ||
| 12 | + datetime, | ||
| 13 | + ram, | ||
| 14 | + shell, | ||
| 15 | + battery, | ||
| 16 | + cpu_temp, | ||
| 17 | +}; | ||
| 18 | + | ||
| 19 | +pub const Block = struct { | ||
| 20 | + data: Data, | ||
| 21 | + last_update: i64, | ||
| 22 | + cached_content: [256]u8, | ||
| 23 | + cached_len: usize, | ||
| 24 | + underline: bool, | ||
| 25 | + | ||
| 26 | + pub const Data = union(Block_Type) { | ||
| 27 | + static: Static, | ||
| 28 | + datetime: Date_Time, | ||
| 29 | + ram: Ram, | ||
| 30 | + shell: Shell, | ||
| 31 | + battery: Battery, | ||
| 32 | + cpu_temp: Cpu_Temp, | ||
| 33 | + }; | ||
| 34 | + | ||
| 35 | + pub fn init_static(text: []const u8, col: c_ulong, ul: bool) Block { | ||
| 36 | + var block = Block{ | ||
| 37 | + .data = .{ .static = Static.init(text, col) }, | ||
| 38 | + .last_update = 0, | ||
| 39 | + .cached_content = undefined, | ||
| 40 | + .cached_len = 0, | ||
| 41 | + .underline = ul, | ||
| 42 | + }; | ||
| 43 | + @memcpy(block.cached_content[0..text.len], text); | ||
| 44 | + block.cached_len = text.len; | ||
| 45 | + return block; | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + pub fn init_datetime(format: []const u8, datetime_format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block { | ||
| 49 | + return .{ | ||
| 50 | + .data = .{ .datetime = Date_Time.init(format, datetime_format, interval_secs, col) }, | ||
| 51 | + .last_update = 0, | ||
| 52 | + .cached_content = undefined, | ||
| 53 | + .cached_len = 0, | ||
| 54 | + .underline = ul, | ||
| 55 | + }; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + pub fn init_ram(format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block { | ||
| 59 | + return .{ | ||
| 60 | + .data = .{ .ram = Ram.init(format, interval_secs, col) }, | ||
| 61 | + .last_update = 0, | ||
| 62 | + .cached_content = undefined, | ||
| 63 | + .cached_len = 0, | ||
| 64 | + .underline = ul, | ||
| 65 | + }; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + pub fn init_shell(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block { | ||
| 69 | + return .{ | ||
| 70 | + .data = .{ .shell = Shell.init(format, command, interval_secs, col) }, | ||
| 71 | + .last_update = 0, | ||
| 72 | + .cached_content = undefined, | ||
| 73 | + .cached_len = 0, | ||
| 74 | + .underline = ul, | ||
| 75 | + }; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + pub fn init_battery( | ||
| 79 | + format_charging: []const u8, | ||
| 80 | + format_discharging: []const u8, | ||
| 81 | + format_full: []const u8, | ||
| 82 | + battery_name: []const u8, | ||
| 83 | + interval_secs: u64, | ||
| 84 | + col: c_ulong, | ||
| 85 | + ul: bool, | ||
| 86 | + ) Block { | ||
| 87 | + return .{ | ||
| 88 | + .data = .{ .battery = Battery.init(format_charging, format_discharging, format_full, battery_name, interval_secs, col) }, | ||
| 89 | + .last_update = 0, | ||
| 90 | + .cached_content = undefined, | ||
| 91 | + .cached_len = 0, | ||
| 92 | + .underline = ul, | ||
| 93 | + }; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + pub fn init_cpu_temp( | ||
| 97 | + format: []const u8, | ||
| 98 | + thermal_zone: []const u8, | ||
| 99 | + interval_secs: u64, | ||
| 100 | + col: c_ulong, | ||
| 101 | + ul: bool, | ||
| 102 | + ) Block { | ||
| 103 | + return .{ | ||
| 104 | + .data = .{ .cpu_temp = Cpu_Temp.init(format, thermal_zone, interval_secs, col) }, | ||
| 105 | + .last_update = 0, | ||
| 106 | + .cached_content = undefined, | ||
| 107 | + .cached_len = 0, | ||
| 108 | + .underline = ul, | ||
| 109 | + }; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + pub fn update(self: *Block) bool { | ||
| 113 | + const interval_secs = self.interval(); | ||
| 114 | + if (interval_secs == 0) return false; | ||
| 115 | + | ||
| 116 | + const now = std.time.timestamp(); | ||
| 117 | + if (now - self.last_update < @as(i64, @intCast(interval_secs))) { | ||
| 118 | + return false; | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | + self.last_update = now; | ||
| 122 | + | ||
| 123 | + const result = switch (self.data) { | ||
| 124 | + .static => |*s| s.content(&self.cached_content), | ||
| 125 | + .datetime => |*d| d.content(&self.cached_content), | ||
| 126 | + .ram => |*r| r.content(&self.cached_content), | ||
| 127 | + .shell => |*s| s.content(&self.cached_content), | ||
| 128 | + .battery => |*b| b.content(&self.cached_content), | ||
| 129 | + .cpu_temp => |*c| c.content(&self.cached_content), | ||
| 130 | + }; | ||
| 131 | + | ||
| 132 | + self.cached_len = result.len; | ||
| 133 | + return true; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + pub fn interval(self: *Block) u64 { | ||
| 137 | + return switch (self.data) { | ||
| 138 | + .static => |*s| s.interval(), | ||
| 139 | + .datetime => |*d| d.interval(), | ||
| 140 | + .ram => |*r| r.interval(), | ||
| 141 | + .shell => |*s| s.interval(), | ||
| 142 | + .battery => |*b| b.interval(), | ||
| 143 | + .cpu_temp => |*c| c.interval(), | ||
| 144 | + }; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + pub fn color(self: *const Block) c_ulong { | ||
| 148 | + return switch (self.data) { | ||
| 149 | + .static => |s| s.color, | ||
| 150 | + .datetime => |d| d.color, | ||
| 151 | + .ram => |r| r.color, | ||
| 152 | + .shell => |s| s.color, | ||
| 153 | + .battery => |b| b.color, | ||
| 154 | + .cpu_temp => |c| c.color, | ||
| 155 | + }; | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + pub fn get_content(self: *const Block) []const u8 { | ||
| 159 | + return self.cached_content[0..self.cached_len]; | ||
| 160 | + } | ||
| 161 | +}; | ||
+113 −0 src/bar/blocks/cpu_temp.zig new file mode 100644
| @@ -0,0 +1,113 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const format_util = @import("format.zig"); | ||
| 3 | + | ||
| 4 | +pub const Cpu_Temp = struct { | ||
| 5 | + format: []const u8, | ||
| 6 | + device: []const u8, | ||
| 7 | + interval_secs: u64, | ||
| 8 | + color: c_ulong, | ||
| 9 | + cached_path: [128]u8, | ||
| 10 | + cached_path_len: usize, | ||
| 11 | + | ||
| 12 | + pub fn init( | ||
| 13 | + format: []const u8, | ||
| 14 | + device: []const u8, | ||
| 15 | + interval_secs: u64, | ||
| 16 | + color: c_ulong, | ||
| 17 | + ) Cpu_Temp { | ||
| 18 | + var self = Cpu_Temp{ | ||
| 19 | + .format = format, | ||
| 20 | + .device = device, | ||
| 21 | + .interval_secs = interval_secs, | ||
| 22 | + .color = color, | ||
| 23 | + .cached_path = undefined, | ||
| 24 | + .cached_path_len = 0, | ||
| 25 | + }; | ||
| 26 | + self.detect_path(); | ||
| 27 | + return self; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + fn detect_path(self: *Cpu_Temp) void { | ||
| 31 | + if (self.device.len > 0 and self.device[0] == '/') { | ||
| 32 | + if (self.device.len <= self.cached_path.len) { | ||
| 33 | + @memcpy(self.cached_path[0..self.device.len], self.device); | ||
| 34 | + self.cached_path_len = self.device.len; | ||
| 35 | + } | ||
| 36 | + return; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + if (self.device.len > 0) { | ||
| 40 | + if (self.try_path("/sys/class/thermal/{s}/temp", self.device)) return; | ||
| 41 | + if (self.try_path("/sys/class/hwmon/{s}/temp1_input", self.device)) return; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + if (self.find_hwmon_cpu()) return; | ||
| 45 | + | ||
| 46 | + if (self.try_path("/sys/class/thermal/{s}/temp", "thermal_zone0")) return; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + fn try_path(self: *Cpu_Temp, comptime fmt: []const u8, device: []const u8) bool { | ||
| 50 | + const path = std.fmt.bufPrint(&self.cached_path, fmt, .{device}) catch return false; | ||
| 51 | + const file = std.fs.openFileAbsolute(path, .{}) catch return false; | ||
| 52 | + file.close(); | ||
| 53 | + self.cached_path_len = path.len; | ||
| 54 | + return true; | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + fn find_hwmon_cpu(self: *Cpu_Temp) bool { | ||
| 58 | + var dir = std.fs.openDirAbsolute("/sys/class/hwmon", .{ .iterate = true }) catch return false; | ||
| 59 | + defer dir.close(); | ||
| 60 | + | ||
| 61 | + var iter = dir.iterate(); | ||
| 62 | + while (iter.next() catch null) |entry| { | ||
| 63 | + if (entry.kind != .directory and entry.kind != .sym_link) continue; | ||
| 64 | + | ||
| 65 | + var name_path: [128]u8 = undefined; | ||
| 66 | + const name_path_str = std.fmt.bufPrint(&name_path, "/sys/class/hwmon/{s}/name", .{entry.name}) catch continue; | ||
| 67 | + | ||
| 68 | + const name_file = std.fs.openFileAbsolute(name_path_str, .{}) catch continue; | ||
| 69 | + defer name_file.close(); | ||
| 70 | + | ||
| 71 | + var name_buf: [32]u8 = undefined; | ||
| 72 | + const name_len = name_file.read(&name_buf) catch continue; | ||
| 73 | + const name = std.mem.trim(u8, name_buf[0..name_len], " \n\r\t"); | ||
| 74 | + | ||
| 75 | + if (std.mem.eql(u8, name, "coretemp") or std.mem.eql(u8, name, "k10temp")) { | ||
| 76 | + const temp_path = std.fmt.bufPrint(&self.cached_path, "/sys/class/hwmon/{s}/temp1_input", .{entry.name}) catch continue; | ||
| 77 | + const temp_file = std.fs.openFileAbsolute(temp_path, .{}) catch continue; | ||
| 78 | + temp_file.close(); | ||
| 79 | + self.cached_path_len = temp_path.len; | ||
| 80 | + return true; | ||
| 81 | + } | ||
| 82 | + } | ||
| 83 | + return false; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + pub fn content(self: *Cpu_Temp, buffer: []u8) []const u8 { | ||
| 87 | + if (self.cached_path_len == 0) return buffer[0..0]; | ||
| 88 | + | ||
| 89 | + const path = self.cached_path[0..self.cached_path_len]; | ||
| 90 | + const file = std.fs.openFileAbsolute(path, .{}) catch return buffer[0..0]; | ||
| 91 | + defer file.close(); | ||
| 92 | + | ||
| 93 | + var temp_buf: [16]u8 = undefined; | ||
| 94 | + const len = file.read(&temp_buf) catch return buffer[0..0]; | ||
| 95 | + const temp_str = std.mem.trim(u8, temp_buf[0..len], " \n\r\t"); | ||
| 96 | + | ||
| 97 | + const millidegrees = std.fmt.parseInt(i32, temp_str, 10) catch return buffer[0..0]; | ||
| 98 | + const degrees = @divTrunc(millidegrees, 1000); | ||
| 99 | + | ||
| 100 | + var deg_buf: [8]u8 = undefined; | ||
| 101 | + const deg_str = std.fmt.bufPrint(°_buf, "{d}", .{degrees}) catch return buffer[0..0]; | ||
| 102 | + | ||
| 103 | + return format_util.substitute(self.format, deg_str, buffer); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + pub fn interval(self: *Cpu_Temp) u64 { | ||
| 107 | + return self.interval_secs; | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + pub fn get_color(self: *Cpu_Temp) c_ulong { | ||
| 111 | + return self.color; | ||
| 112 | + } | ||
| 113 | +}; | ||
+0 −38 src/bar/blocks/datetime.rs deleted file mode 100644
| @@ -1,38 +0,0 @@ | |||
| 1 | -use super::Block; | ||
| 2 | -use crate::errors::BlockError; | ||
| 3 | -use chrono::Local; | ||
| 4 | -use std::time::Duration; | ||
| 5 | - | ||
| 6 | -pub struct DateTime { | ||
| 7 | - format_template: String, | ||
| 8 | - time_format: String, | ||
| 9 | - interval: Duration, | ||
| 10 | - color: u32, | ||
| 11 | -} | ||
| 12 | - | ||
| 13 | -impl DateTime { | ||
| 14 | - pub fn new(format_template: &str, time_format: &str, interval_secs: u64, color: u32) -> Self { | ||
| 15 | - Self { | ||
| 16 | - format_template: format_template.to_string(), | ||
| 17 | - time_format: time_format.to_string(), | ||
| 18 | - interval: Duration::from_secs(interval_secs), | ||
| 19 | - color, | ||
| 20 | - } | ||
| 21 | - } | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -impl Block for DateTime { | ||
| 25 | - fn content(&mut self) -> Result<String, BlockError> { | ||
| 26 | - let now = Local::now(); | ||
| 27 | - let time_str = now.format(&self.time_format).to_string(); | ||
| 28 | - Ok(self.format_template.replace("{}", &time_str)) | ||
| 29 | - } | ||
| 30 | - | ||
| 31 | - fn interval(&self) -> Duration { | ||
| 32 | - self.interval | ||
| 33 | - } | ||
| 34 | - | ||
| 35 | - fn color(&self) -> u32 { | ||
| 36 | - self.color | ||
| 37 | - } | ||
| 38 | -} | ||
+107 −0 src/bar/blocks/datetime.zig new file mode 100644
| @@ -0,0 +1,107 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const format_util = @import("format.zig"); | ||
| 3 | +const c = @cImport({ | ||
| 4 | + @cInclude("time.h"); | ||
| 5 | +}); | ||
| 6 | + | ||
| 7 | +pub const Date_Time = struct { | ||
| 8 | + format: []const u8, | ||
| 9 | + datetime_format: []const u8, | ||
| 10 | + interval_secs: u64, | ||
| 11 | + color: c_ulong, | ||
| 12 | + | ||
| 13 | + pub fn init(format: []const u8, datetime_format: []const u8, interval_secs: u64, color: c_ulong) Date_Time { | ||
| 14 | + return .{ | ||
| 15 | + .format = format, | ||
| 16 | + .datetime_format = datetime_format, | ||
| 17 | + .interval_secs = interval_secs, | ||
| 18 | + .color = color, | ||
| 19 | + }; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + pub fn content(self: *Date_Time, buffer: []u8) []const u8 { | ||
| 23 | + var now: c.time_t = c.time(null); | ||
| 24 | + const tm_ptr = c.localtime(&now); | ||
| 25 | + if (tm_ptr == null) return buffer[0..0]; | ||
| 26 | + const tm = tm_ptr.*; | ||
| 27 | + | ||
| 28 | + const hours: u32 = @intCast(tm.tm_hour); | ||
| 29 | + const minutes: u32 = @intCast(tm.tm_min); | ||
| 30 | + const seconds: u32 = @intCast(tm.tm_sec); | ||
| 31 | + const day: u8 = @intCast(tm.tm_mday); | ||
| 32 | + const month: u8 = @intCast(tm.tm_mon + 1); | ||
| 33 | + const year: i32 = tm.tm_year + 1900; | ||
| 34 | + const dow: i32 = tm.tm_wday; | ||
| 35 | + | ||
| 36 | + var datetime_buf: [64]u8 = undefined; | ||
| 37 | + var dt_len: usize = 0; | ||
| 38 | + | ||
| 39 | + var fmt_idx: usize = 0; | ||
| 40 | + while (fmt_idx < self.datetime_format.len and dt_len < datetime_buf.len - 10) { | ||
| 41 | + if (self.datetime_format[fmt_idx] == '%' and fmt_idx + 1 < self.datetime_format.len) { | ||
| 42 | + const next = self.datetime_format[fmt_idx + 1]; | ||
| 43 | + if (next == '-' and fmt_idx + 2 < self.datetime_format.len) { | ||
| 44 | + const spec = self.datetime_format[fmt_idx + 2]; | ||
| 45 | + dt_len += format_spec(spec, false, hours, minutes, seconds, day, month, year, dow, datetime_buf[dt_len..]); | ||
| 46 | + fmt_idx += 3; | ||
| 47 | + } else { | ||
| 48 | + dt_len += format_spec(next, true, hours, minutes, seconds, day, month, year, dow, datetime_buf[dt_len..]); | ||
| 49 | + fmt_idx += 2; | ||
| 50 | + } | ||
| 51 | + } else { | ||
| 52 | + datetime_buf[dt_len] = self.datetime_format[fmt_idx]; | ||
| 53 | + dt_len += 1; | ||
| 54 | + fmt_idx += 1; | ||
| 55 | + } | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + return format_util.substitute(self.format, datetime_buf[0..dt_len], buffer); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + fn format_spec(spec: u8, pad: bool, hours: u32, minutes: u32, seconds: u32, day: u8, month: u8, year: i32, dow: i32, buf: []u8) usize { | ||
| 62 | + const day_names = [_][]const u8{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; | ||
| 63 | + const month_names = [_][]const u8{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; | ||
| 64 | + | ||
| 65 | + return switch (spec) { | ||
| 66 | + 'Y' => (std.fmt.bufPrint(buf, "{d}", .{year}) catch return 0).len, | ||
| 67 | + 'm' => if (pad) (std.fmt.bufPrint(buf, "{d:0>2}", .{month}) catch return 0).len else (std.fmt.bufPrint(buf, "{d}", .{month}) catch return 0).len, | ||
| 68 | + 'd' => if (pad) (std.fmt.bufPrint(buf, "{d:0>2}", .{day}) catch return 0).len else (std.fmt.bufPrint(buf, "{d}", .{day}) catch return 0).len, | ||
| 69 | + 'H' => if (pad) (std.fmt.bufPrint(buf, "{d:0>2}", .{hours}) catch return 0).len else (std.fmt.bufPrint(buf, "{d}", .{hours}) catch return 0).len, | ||
| 70 | + 'M' => if (pad) (std.fmt.bufPrint(buf, "{d:0>2}", .{minutes}) catch return 0).len else (std.fmt.bufPrint(buf, "{d}", .{minutes}) catch return 0).len, | ||
| 71 | + 'S' => if (pad) (std.fmt.bufPrint(buf, "{d:0>2}", .{seconds}) catch return 0).len else (std.fmt.bufPrint(buf, "{d}", .{seconds}) catch return 0).len, | ||
| 72 | + 'I' => blk: { | ||
| 73 | + const h12 = if (hours == 0) 12 else if (hours > 12) hours - 12 else hours; | ||
| 74 | + break :blk if (pad) (std.fmt.bufPrint(buf, "{d:0>2}", .{h12}) catch return 0).len else (std.fmt.bufPrint(buf, "{d}", .{h12}) catch return 0).len; | ||
| 75 | + }, | ||
| 76 | + 'p' => blk: { | ||
| 77 | + const s: []const u8 = if (hours >= 12) "PM" else "AM"; | ||
| 78 | + @memcpy(buf[0..2], s); | ||
| 79 | + break :blk 2; | ||
| 80 | + }, | ||
| 81 | + 'P' => blk: { | ||
| 82 | + const s: []const u8 = if (hours >= 12) "pm" else "am"; | ||
| 83 | + @memcpy(buf[0..2], s); | ||
| 84 | + break :blk 2; | ||
| 85 | + }, | ||
| 86 | + 'a' => blk: { | ||
| 87 | + const name = day_names[@intCast(dow)]; | ||
| 88 | + @memcpy(buf[0..3], name); | ||
| 89 | + break :blk 3; | ||
| 90 | + }, | ||
| 91 | + 'b' => blk: { | ||
| 92 | + const name = month_names[month - 1]; | ||
| 93 | + @memcpy(buf[0..3], name); | ||
| 94 | + break :blk 3; | ||
| 95 | + }, | ||
| 96 | + else => 0, | ||
| 97 | + }; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + pub fn interval(self: *Date_Time) u64 { | ||
| 101 | + return self.interval_secs; | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + pub fn get_color(self: *Date_Time) c_ulong { | ||
| 105 | + return self.color; | ||
| 106 | + } | ||
| 107 | +}; | ||
+52 −0 src/bar/blocks/format.zig new file mode 100644
| @@ -0,0 +1,52 @@ | |||
| 1 | +pub fn substitute(format: []const u8, value: []const u8, buffer: []u8) []const u8 { | ||
| 2 | + if (format.len == 0) { | ||
| 3 | + const len = @min(value.len, buffer.len); | ||
| 4 | + @memcpy(buffer[0..len], value[0..len]); | ||
| 5 | + return buffer[0..len]; | ||
| 6 | + } | ||
| 7 | + | ||
| 8 | + var out_idx: usize = 0; | ||
| 9 | + var fmt_idx: usize = 0; | ||
| 10 | + while (fmt_idx < format.len and out_idx < buffer.len -| value.len) { | ||
| 11 | + if (fmt_idx + 1 < format.len and format[fmt_idx] == '{' and format[fmt_idx + 1] == '}') { | ||
| 12 | + @memcpy(buffer[out_idx .. out_idx + value.len], value); | ||
| 13 | + out_idx += value.len; | ||
| 14 | + fmt_idx += 2; | ||
| 15 | + } else { | ||
| 16 | + buffer[out_idx] = format[fmt_idx]; | ||
| 17 | + out_idx += 1; | ||
| 18 | + fmt_idx += 1; | ||
| 19 | + } | ||
| 20 | + } | ||
| 21 | + return buffer[0..out_idx]; | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +pub fn substitute_multi(format: []const u8, values: []const []const u8, buffer: []u8) []const u8 { | ||
| 25 | + if (format.len == 0 and values.len > 0) { | ||
| 26 | + const len = @min(values[0].len, buffer.len); | ||
| 27 | + @memcpy(buffer[0..len], values[0][0..len]); | ||
| 28 | + return buffer[0..len]; | ||
| 29 | + } | ||
| 30 | + | ||
| 31 | + var out_idx: usize = 0; | ||
| 32 | + var fmt_idx: usize = 0; | ||
| 33 | + var val_idx: usize = 0; | ||
| 34 | + while (fmt_idx < format.len and out_idx < buffer.len - 20) { | ||
| 35 | + if (fmt_idx + 1 < format.len and format[fmt_idx] == '{' and format[fmt_idx + 1] == '}') { | ||
| 36 | + if (val_idx < values.len) { | ||
| 37 | + const value = values[val_idx]; | ||
| 38 | + if (out_idx + value.len <= buffer.len) { | ||
| 39 | + @memcpy(buffer[out_idx .. out_idx + value.len], value); | ||
| 40 | + out_idx += value.len; | ||
| 41 | + } | ||
| 42 | + val_idx += 1; | ||
| 43 | + } | ||
| 44 | + fmt_idx += 2; | ||
| 45 | + } else { | ||
| 46 | + buffer[out_idx] = format[fmt_idx]; | ||
| 47 | + out_idx += 1; | ||
| 48 | + fmt_idx += 1; | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + return buffer[0..out_idx]; | ||
| 52 | +} | ||
+0 −106 src/bar/blocks/mod.rs deleted file mode 100644
| @@ -1,106 +0,0 @@ | |||
| 1 | -use crate::errors::BlockError; | ||
| 2 | -use std::time::Duration; | ||
| 3 | - | ||
| 4 | -mod battery; | ||
| 5 | -mod datetime; | ||
| 6 | -mod ram; | ||
| 7 | -mod shell; | ||
| 8 | - | ||
| 9 | -use battery::Battery; | ||
| 10 | -use datetime::DateTime; | ||
| 11 | -use ram::Ram; | ||
| 12 | -use shell::ShellBlock; | ||
| 13 | - | ||
| 14 | -pub trait Block { | ||
| 15 | - fn content(&mut self) -> Result<String, BlockError>; | ||
| 16 | - fn interval(&self) -> Duration; | ||
| 17 | - fn color(&self) -> u32; | ||
| 18 | -} | ||
| 19 | - | ||
| 20 | -#[derive(Debug, Clone)] | ||
| 21 | -pub struct BlockConfig { | ||
| 22 | - pub format: String, | ||
| 23 | - pub command: BlockCommand, | ||
| 24 | - pub interval_secs: u64, | ||
| 25 | - pub color: u32, | ||
| 26 | - pub underline: bool, | ||
| 27 | -} | ||
| 28 | - | ||
| 29 | -#[derive(Debug, Clone)] | ||
| 30 | -pub enum BlockCommand { | ||
| 31 | - Shell(String), | ||
| 32 | - DateTime(String), | ||
| 33 | - Battery { | ||
| 34 | - format_charging: String, | ||
| 35 | - format_discharging: String, | ||
| 36 | - format_full: String, | ||
| 37 | - battery_name: Option<String>, | ||
| 38 | - }, | ||
| 39 | - Ram, | ||
| 40 | - Static(String), | ||
| 41 | -} | ||
| 42 | - | ||
| 43 | -impl BlockConfig { | ||
| 44 | - pub fn to_block(&self) -> Box<dyn Block> { | ||
| 45 | - match &self.command { | ||
| 46 | - BlockCommand::Shell(cmd) => Box::new(ShellBlock::new( | ||
| 47 | - &self.format, | ||
| 48 | - cmd, | ||
| 49 | - self.interval_secs, | ||
| 50 | - self.color, | ||
| 51 | - )), | ||
| 52 | - BlockCommand::DateTime(fmt) => Box::new(DateTime::new( | ||
| 53 | - &self.format, | ||
| 54 | - fmt, | ||
| 55 | - self.interval_secs, | ||
| 56 | - self.color, | ||
| 57 | - )), | ||
| 58 | - BlockCommand::Battery { | ||
| 59 | - format_charging, | ||
| 60 | - format_discharging, | ||
| 61 | - format_full, | ||
| 62 | - battery_name, | ||
| 63 | - } => Box::new(Battery::new( | ||
| 64 | - format_charging, | ||
| 65 | - format_discharging, | ||
| 66 | - format_full, | ||
| 67 | - self.interval_secs, | ||
| 68 | - self.color, | ||
| 69 | - battery_name.clone(), | ||
| 70 | - )), | ||
| 71 | - BlockCommand::Ram => Box::new(Ram::new(&self.format, self.interval_secs, self.color)), | ||
| 72 | - BlockCommand::Static(text) => Box::new(StaticBlock::new( | ||
| 73 | - &format!("{}{}", self.format, text), | ||
| 74 | - self.color, | ||
| 75 | - )), | ||
| 76 | - } | ||
| 77 | - } | ||
| 78 | -} | ||
| 79 | - | ||
| 80 | -struct StaticBlock { | ||
| 81 | - text: String, | ||
| 82 | - color: u32, | ||
| 83 | -} | ||
| 84 | - | ||
| 85 | -impl StaticBlock { | ||
| 86 | - fn new(text: &str, color: u32) -> Self { | ||
| 87 | - Self { | ||
| 88 | - text: text.to_string(), | ||
| 89 | - color, | ||
| 90 | - } | ||
| 91 | - } | ||
| 92 | -} | ||
| 93 | - | ||
| 94 | -impl Block for StaticBlock { | ||
| 95 | - fn content(&mut self) -> Result<String, BlockError> { | ||
| 96 | - Ok(self.text.clone()) | ||
| 97 | - } | ||
| 98 | - | ||
| 99 | - fn interval(&self) -> Duration { | ||
| 100 | - Duration::from_secs(u64::MAX) | ||
| 101 | - } | ||
| 102 | - | ||
| 103 | - fn color(&self) -> u32 { | ||
| 104 | - self.color | ||
| 105 | - } | ||
| 106 | -} | ||
+0 −77 src/bar/blocks/ram.rs deleted file mode 100644
| @@ -1,77 +0,0 @@ | |||
| 1 | -use super::Block; | ||
| 2 | -use crate::errors::BlockError; | ||
| 3 | -use std::fs; | ||
| 4 | -use std::time::Duration; | ||
| 5 | - | ||
| 6 | -pub struct Ram { | ||
| 7 | - format: String, | ||
| 8 | - interval: Duration, | ||
| 9 | - color: u32, | ||
| 10 | -} | ||
| 11 | - | ||
| 12 | -impl Ram { | ||
| 13 | - pub fn new(format: &str, interval_secs: u64, color: u32) -> Self { | ||
| 14 | - Self { | ||
| 15 | - format: format.to_string(), | ||
| 16 | - interval: Duration::from_secs(interval_secs), | ||
| 17 | - color, | ||
| 18 | - } | ||
| 19 | - } | ||
| 20 | - | ||
| 21 | - fn get_memory_info(&self) -> Result<(u64, u64, f32), BlockError> { | ||
| 22 | - let meminfo = fs::read_to_string("/proc/meminfo")?; | ||
| 23 | - let mut total: u64 = 0; | ||
| 24 | - let mut available: u64 = 0; | ||
| 25 | - | ||
| 26 | - for line in meminfo.lines() { | ||
| 27 | - if line.starts_with("MemTotal:") { | ||
| 28 | - total = line | ||
| 29 | - .split_whitespace() | ||
| 30 | - .nth(1) | ||
| 31 | - .and_then(|s| s.parse().ok()) | ||
| 32 | - .unwrap_or(0) | ||
| 33 | - } else if line.starts_with("MemAvailable:") { | ||
| 34 | - available = line | ||
| 35 | - .split_whitespace() | ||
| 36 | - .nth(1) | ||
| 37 | - .and_then(|s| s.parse().ok()) | ||
| 38 | - .unwrap_or(0) | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - let used = total.saturating_sub(available); | ||
| 43 | - let percentage = if total > 0 { | ||
| 44 | - (used as f32 / total as f32) * 100.0 | ||
| 45 | - } else { | ||
| 46 | - 0.0 | ||
| 47 | - }; | ||
| 48 | - | ||
| 49 | - Ok((used, total, percentage)) | ||
| 50 | - } | ||
| 51 | -} | ||
| 52 | - | ||
| 53 | -impl Block for Ram { | ||
| 54 | - fn content(&mut self) -> Result<String, BlockError> { | ||
| 55 | - let (used, total, percentage) = self.get_memory_info()?; | ||
| 56 | - | ||
| 57 | - let used_gb = used as f32 / 1024.0 / 1024.0; | ||
| 58 | - let total_gb = total as f32 / 1024.0 / 1024.0; | ||
| 59 | - | ||
| 60 | - let result = self | ||
| 61 | - .format | ||
| 62 | - .replace("{used}", &format!("{:.1}", used_gb)) | ||
| 63 | - .replace("{total}", &format!("{:.1}", total_gb)) | ||
| 64 | - .replace("{percent}", &format!("{:.1}", percentage)) | ||
| 65 | - .replace("{}", &format!("{:.1}", used_gb)); | ||
| 66 | - | ||
| 67 | - Ok(result) | ||
| 68 | - } | ||
| 69 | - | ||
| 70 | - fn interval(&self) -> Duration { | ||
| 71 | - self.interval | ||
| 72 | - } | ||
| 73 | - | ||
| 74 | - fn color(&self) -> u32 { | ||
| 75 | - self.color | ||
| 76 | - } | ||
| 77 | -} | ||
+111 −0 src/bar/blocks/ram.zig new file mode 100644
| @@ -0,0 +1,111 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +pub const Ram = struct { | ||
| 4 | + format: []const u8, | ||
| 5 | + interval_secs: u64, | ||
| 6 | + color: c_ulong, | ||
| 7 | + | ||
| 8 | + pub fn init(format: []const u8, interval_secs: u64, color: c_ulong) Ram { | ||
| 9 | + return .{ | ||
| 10 | + .format = format, | ||
| 11 | + .interval_secs = interval_secs, | ||
| 12 | + .color = color, | ||
| 13 | + }; | ||
| 14 | + } | ||
| 15 | + | ||
| 16 | + pub fn content(self: *Ram, buffer: []u8) []const u8 { | ||
| 17 | + const file = std.fs.openFileAbsolute("/proc/meminfo", .{}) catch return buffer[0..0]; | ||
| 18 | + defer file.close(); | ||
| 19 | + | ||
| 20 | + var read_buffer: [512]u8 = undefined; | ||
| 21 | + const bytes_read = file.read(&read_buffer) catch return buffer[0..0]; | ||
| 22 | + const file_content = read_buffer[0..bytes_read]; | ||
| 23 | + | ||
| 24 | + var total: u64 = 0; | ||
| 25 | + var available: u64 = 0; | ||
| 26 | + | ||
| 27 | + var lines = std.mem.splitScalar(u8, file_content, '\n'); | ||
| 28 | + while (lines.next()) |line| { | ||
| 29 | + if (std.mem.startsWith(u8, line, "MemTotal:")) { | ||
| 30 | + total = parse_mem_value(line); | ||
| 31 | + } else if (std.mem.startsWith(u8, line, "MemAvailable:")) { | ||
| 32 | + available = parse_mem_value(line); | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + if (total == 0) return buffer[0..0]; | ||
| 37 | + | ||
| 38 | + const used = total - available; | ||
| 39 | + const used_gb = @as(f32, @floatFromInt(used)) / 1024.0 / 1024.0; | ||
| 40 | + const total_gb = @as(f32, @floatFromInt(total)) / 1024.0 / 1024.0; | ||
| 41 | + const percent = (@as(f32, @floatFromInt(used)) / @as(f32, @floatFromInt(total))) * 100.0; | ||
| 42 | + | ||
| 43 | + var val_bufs: [3][16]u8 = undefined; | ||
| 44 | + const used_str = std.fmt.bufPrint(&val_bufs[0], "{d:.1}", .{used_gb}) catch return buffer[0..0]; | ||
| 45 | + const total_str = std.fmt.bufPrint(&val_bufs[1], "{d:.1}", .{total_gb}) catch return buffer[0..0]; | ||
| 46 | + const percent_str = std.fmt.bufPrint(&val_bufs[2], "{d:.1}", .{percent}) catch return buffer[0..0]; | ||
| 47 | + | ||
| 48 | + return substitute(self.format, buffer, .{ | ||
| 49 | + .used = used_str, | ||
| 50 | + .total = total_str, | ||
| 51 | + .percent = percent_str, | ||
| 52 | + }); | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + pub fn interval(self: *Ram) u64 { | ||
| 56 | + return self.interval_secs; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + pub fn get_color(self: *Ram) c_ulong { | ||
| 60 | + return self.color; | ||
| 61 | + } | ||
| 62 | +}; | ||
| 63 | + | ||
| 64 | +fn parse_mem_value(line: []const u8) u64 { | ||
| 65 | + var iter = std.mem.tokenizeAny(u8, line, ": \tkB"); | ||
| 66 | + _ = iter.next(); | ||
| 67 | + if (iter.next()) |value_str| { | ||
| 68 | + return std.fmt.parseInt(u64, value_str, 10) catch 0; | ||
| 69 | + } | ||
| 70 | + return 0; | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +fn substitute(format: []const u8, buffer: []u8, values: struct { | ||
| 74 | + used: []const u8, | ||
| 75 | + total: []const u8, | ||
| 76 | + percent: []const u8, | ||
| 77 | +}) []const u8 { | ||
| 78 | + var pos: usize = 0; | ||
| 79 | + var i: usize = 0; | ||
| 80 | + | ||
| 81 | + while (i < format.len) { | ||
| 82 | + if (format[i] == '{' and i + 1 < format.len) { | ||
| 83 | + const rest = format[i..]; | ||
| 84 | + const repl = if (std.mem.startsWith(u8, rest, "{used}")) blk: { | ||
| 85 | + i += 6; | ||
| 86 | + break :blk values.used; | ||
| 87 | + } else if (std.mem.startsWith(u8, rest, "{total}")) blk: { | ||
| 88 | + i += 7; | ||
| 89 | + break :blk values.total; | ||
| 90 | + } else if (std.mem.startsWith(u8, rest, "{percent}")) blk: { | ||
| 91 | + i += 9; | ||
| 92 | + break :blk values.percent; | ||
| 93 | + } else if (std.mem.startsWith(u8, rest, "{}")) blk: { | ||
| 94 | + i += 2; | ||
| 95 | + break :blk values.used; | ||
| 96 | + } else null; | ||
| 97 | + | ||
| 98 | + if (repl) |r| { | ||
| 99 | + if (pos + r.len > buffer.len) break; | ||
| 100 | + @memcpy(buffer[pos..][0..r.len], r); | ||
| 101 | + pos += r.len; | ||
| 102 | + continue; | ||
| 103 | + } | ||
| 104 | + } | ||
| 105 | + if (pos >= buffer.len) break; | ||
| 106 | + buffer[pos] = format[i]; | ||
| 107 | + pos += 1; | ||
| 108 | + i += 1; | ||
| 109 | + } | ||
| 110 | + return buffer[0..pos]; | ||
| 111 | +} | ||
+0 −74 src/bar/blocks/shell.rs deleted file mode 100644
| @@ -1,74 +0,0 @@ | |||
| 1 | -use super::Block; | ||
| 2 | -use crate::errors::BlockError; | ||
| 3 | -use std::process::Command; | ||
| 4 | -use std::time::{Duration, Instant}; | ||
| 5 | - | ||
| 6 | -pub struct ShellBlock { | ||
| 7 | - format: String, | ||
| 8 | - command: String, | ||
| 9 | - interval: Duration, | ||
| 10 | - color: u32, | ||
| 11 | - cached_output: Option<String>, | ||
| 12 | - last_run: Option<Instant>, | ||
| 13 | -} | ||
| 14 | - | ||
| 15 | -impl ShellBlock { | ||
| 16 | - pub fn new(format: &str, command: &str, interval_secs: u64, color: u32) -> Self { | ||
| 17 | - Self { | ||
| 18 | - format: format.to_string(), | ||
| 19 | - command: command.to_string(), | ||
| 20 | - interval: Duration::from_secs(interval_secs), | ||
| 21 | - color, | ||
| 22 | - cached_output: None, | ||
| 23 | - last_run: None, | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - fn execute(&mut self) -> Result<String, BlockError> { | ||
| 28 | - let output = Command::new("sh") | ||
| 29 | - .arg("-c") | ||
| 30 | - .arg(&self.command) | ||
| 31 | - .output() | ||
| 32 | - .map_err(|e| BlockError::CommandFailed(format!("Failed to execute command: {}", e)))?; | ||
| 33 | - | ||
| 34 | - if !output.status.success() { | ||
| 35 | - return Err(BlockError::CommandFailed(format!( | ||
| 36 | - "Command exited with status: {}", | ||
| 37 | - output.status | ||
| 38 | - ))); | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - let result = String::from_utf8_lossy(&output.stdout).trim().to_string(); | ||
| 42 | - let formatted = self.format.replace("{}", &result); | ||
| 43 | - | ||
| 44 | - self.cached_output = Some(formatted.clone()); | ||
| 45 | - self.last_run = Some(Instant::now()); | ||
| 46 | - | ||
| 47 | - Ok(formatted) | ||
| 48 | - } | ||
| 49 | -} | ||
| 50 | - | ||
| 51 | -impl Block for ShellBlock { | ||
| 52 | - fn content(&mut self) -> Result<String, BlockError> { | ||
| 53 | - let should_refresh = match self.last_run { | ||
| 54 | - None => true, | ||
| 55 | - Some(last) => last.elapsed() >= self.interval, | ||
| 56 | - }; | ||
| 57 | - | ||
| 58 | - if should_refresh { | ||
| 59 | - return self.execute(); | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - self.cached_output | ||
| 63 | - .clone() | ||
| 64 | - .ok_or_else(|| BlockError::CommandFailed("No cached output".to_string())) | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - fn interval(&self) -> Duration { | ||
| 68 | - self.interval | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - fn color(&self) -> u32 { | ||
| 72 | - self.color | ||
| 73 | - } | ||
| 74 | -} | ||
+45 −0 src/bar/blocks/shell.zig new file mode 100644
| @@ -0,0 +1,45 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const format_util = @import("format.zig"); | ||
| 3 | + | ||
| 4 | +pub const Shell = struct { | ||
| 5 | + format: []const u8, | ||
| 6 | + command: []const u8, | ||
| 7 | + interval_secs: u64, | ||
| 8 | + color: c_ulong, | ||
| 9 | + | ||
| 10 | + pub fn init(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong) Shell { | ||
| 11 | + return .{ | ||
| 12 | + .format = format, | ||
| 13 | + .command = command, | ||
| 14 | + .interval_secs = interval_secs, | ||
| 15 | + .color = col, | ||
| 16 | + }; | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + pub fn content(self: *Shell, buffer: []u8) []const u8 { | ||
| 20 | + var cmd_output: [256]u8 = undefined; | ||
| 21 | + const result = std.process.Child.run(.{ | ||
| 22 | + .allocator = std.heap.page_allocator, | ||
| 23 | + .argv = &.{ "/bin/sh", "-c", self.command }, | ||
| 24 | + }) catch return buffer[0..0]; | ||
| 25 | + defer std.heap.page_allocator.free(result.stdout); | ||
| 26 | + defer std.heap.page_allocator.free(result.stderr); | ||
| 27 | + | ||
| 28 | + var cmd_len = @min(result.stdout.len, cmd_output.len); | ||
| 29 | + @memcpy(cmd_output[0..cmd_len], result.stdout[0..cmd_len]); | ||
| 30 | + | ||
| 31 | + while (cmd_len > 0 and (cmd_output[cmd_len - 1] == '\n' or cmd_output[cmd_len - 1] == '\r')) { | ||
| 32 | + cmd_len -= 1; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + return format_util.substitute(self.format, cmd_output[0..cmd_len], buffer); | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + pub fn interval(self: *Shell) u64 { | ||
| 39 | + return self.interval_secs; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + pub fn get_color(self: *Shell) c_ulong { | ||
| 43 | + return self.color; | ||
| 44 | + } | ||
| 45 | +}; | ||
+24 −0 src/bar/blocks/static.zig new file mode 100644
| @@ -0,0 +1,24 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +pub const Static = struct { | ||
| 4 | + text: []const u8, | ||
| 5 | + color: c_ulong, | ||
| 6 | + | ||
| 7 | + pub fn init(text: []const u8, color: c_ulong) Static { | ||
| 8 | + return .{ .text = text, .color = color }; | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + pub fn content(self: *Static, buffer: []u8) []const u8 { | ||
| 12 | + const len = @min(self.text.len, buffer.len); | ||
| 13 | + @memcpy(buffer[0..len], self.text[0..len]); | ||
| 14 | + return buffer[0..len]; | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + pub fn interval(_: *Static) u64 { | ||
| 18 | + return 0; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + pub fn get_color(self: *Static) c_ulong { | ||
| 22 | + return self.color; | ||
| 23 | + } | ||
| 24 | +}; | ||
+0 −199 src/bar/font.rs deleted file mode 100644
| @@ -1,199 +0,0 @@ | |||
| 1 | -use std::ffi::CString; | ||
| 2 | -use x11::xft::{XftColor, XftDraw, XftDrawStringUtf8, XftFont, XftFontOpenName}; | ||
| 3 | -use x11::xlib::{Colormap, Display, Drawable, Visual}; | ||
| 4 | -use x11::xrender::XRenderColor; | ||
| 5 | - | ||
| 6 | -use crate::errors::X11Error; | ||
| 7 | - | ||
| 8 | -pub struct Font { | ||
| 9 | - xft_font: *mut XftFont, | ||
| 10 | - display: *mut Display, | ||
| 11 | -} | ||
| 12 | - | ||
| 13 | -impl Font { | ||
| 14 | - pub fn new(display: *mut Display, screen: i32, font_name: &str) -> Result<Self, X11Error> { | ||
| 15 | - let font_name_cstr = | ||
| 16 | - CString::new(font_name).map_err(|_| X11Error::FontLoadFailed(font_name.to_string()))?; | ||
| 17 | - | ||
| 18 | - let xft_font = unsafe { XftFontOpenName(display, screen, font_name_cstr.as_ptr()) }; | ||
| 19 | - | ||
| 20 | - if xft_font.is_null() { | ||
| 21 | - return Err(X11Error::FontLoadFailed(font_name.to_string())); | ||
| 22 | - } | ||
| 23 | - | ||
| 24 | - Ok(Font { xft_font, display }) | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - pub fn height(&self) -> u16 { | ||
| 28 | - unsafe { | ||
| 29 | - let font = &*self.xft_font; | ||
| 30 | - font.height as u16 | ||
| 31 | - } | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | - pub fn ascent(&self) -> i16 { | ||
| 35 | - unsafe { | ||
| 36 | - let font = &*self.xft_font; | ||
| 37 | - font.ascent as i16 | ||
| 38 | - } | ||
| 39 | - } | ||
| 40 | - | ||
| 41 | - pub fn text_width(&self, text: &str) -> u16 { | ||
| 42 | - unsafe { | ||
| 43 | - let mut extents = std::mem::zeroed(); | ||
| 44 | - x11::xft::XftTextExtentsUtf8( | ||
| 45 | - self.display, | ||
| 46 | - self.xft_font, | ||
| 47 | - text.as_ptr(), | ||
| 48 | - text.len() as i32, | ||
| 49 | - &mut extents, | ||
| 50 | - ); | ||
| 51 | - extents.width | ||
| 52 | - } | ||
| 53 | - } | ||
| 54 | -} | ||
| 55 | - | ||
| 56 | -impl Drop for Font { | ||
| 57 | - fn drop(&mut self) { | ||
| 58 | - unsafe { | ||
| 59 | - if !self.xft_font.is_null() { | ||
| 60 | - x11::xft::XftFontClose(self.display, self.xft_font); | ||
| 61 | - } | ||
| 62 | - } | ||
| 63 | - } | ||
| 64 | -} | ||
| 65 | - | ||
| 66 | -pub struct FontDraw { | ||
| 67 | - xft_draw: *mut XftDraw, | ||
| 68 | -} | ||
| 69 | - | ||
| 70 | -impl FontDraw { | ||
| 71 | - pub fn new( | ||
| 72 | - display: *mut Display, | ||
| 73 | - drawable: Drawable, | ||
| 74 | - visual: *mut Visual, | ||
| 75 | - colormap: Colormap, | ||
| 76 | - ) -> Result<Self, X11Error> { | ||
| 77 | - let xft_draw = unsafe { x11::xft::XftDrawCreate(display, drawable, visual, colormap) }; | ||
| 78 | - | ||
| 79 | - if xft_draw.is_null() { | ||
| 80 | - return Err(X11Error::DrawCreateFailed); | ||
| 81 | - } | ||
| 82 | - | ||
| 83 | - Ok(FontDraw { xft_draw }) | ||
| 84 | - } | ||
| 85 | - | ||
| 86 | - pub fn draw_text(&self, font: &Font, color: u32, x: i16, y: i16, text: &str) { | ||
| 87 | - let red = ((color >> 16) & 0xFF) as u16; | ||
| 88 | - let green = ((color >> 8) & 0xFF) as u16; | ||
| 89 | - let blue = (color & 0xFF) as u16; | ||
| 90 | - | ||
| 91 | - let render_color = XRenderColor { | ||
| 92 | - red: red << 8 | red, | ||
| 93 | - green: green << 8 | green, | ||
| 94 | - blue: blue << 8 | blue, | ||
| 95 | - alpha: 0xFFFF, | ||
| 96 | - }; | ||
| 97 | - | ||
| 98 | - let mut xft_color: XftColor = unsafe { std::mem::zeroed() }; | ||
| 99 | - | ||
| 100 | - unsafe { | ||
| 101 | - x11::xft::XftColorAllocValue( | ||
| 102 | - x11::xft::XftDrawDisplay(self.xft_draw), | ||
| 103 | - x11::xft::XftDrawVisual(self.xft_draw), | ||
| 104 | - x11::xft::XftDrawColormap(self.xft_draw), | ||
| 105 | - &render_color, | ||
| 106 | - &mut xft_color, | ||
| 107 | - ); | ||
| 108 | - | ||
| 109 | - XftDrawStringUtf8( | ||
| 110 | - self.xft_draw, | ||
| 111 | - &xft_color, | ||
| 112 | - font.xft_font, | ||
| 113 | - x as i32, | ||
| 114 | - y as i32, | ||
| 115 | - text.as_ptr(), | ||
| 116 | - text.len() as i32, | ||
| 117 | - ); | ||
| 118 | - | ||
| 119 | - x11::xft::XftColorFree( | ||
| 120 | - x11::xft::XftDrawDisplay(self.xft_draw), | ||
| 121 | - x11::xft::XftDrawVisual(self.xft_draw), | ||
| 122 | - x11::xft::XftDrawColormap(self.xft_draw), | ||
| 123 | - &mut xft_color, | ||
| 124 | - ); | ||
| 125 | - } | ||
| 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 | - } | ||
| 134 | - | ||
| 135 | - pub fn sync(&self) { | ||
| 136 | - unsafe { | ||
| 137 | - let display = x11::xft::XftDrawDisplay(self.xft_draw); | ||
| 138 | - x11::xlib::XSync(display, 1); | ||
| 139 | - } | ||
| 140 | - } | ||
| 141 | -} | ||
| 142 | - | ||
| 143 | -impl Drop for FontDraw { | ||
| 144 | - fn drop(&mut self) { | ||
| 145 | - unsafe { | ||
| 146 | - if !self.xft_draw.is_null() { | ||
| 147 | - x11::xft::XftDrawDestroy(self.xft_draw); | ||
| 148 | - } | ||
| 149 | - } | ||
| 150 | - } | ||
| 151 | -} | ||
| 152 | - | ||
| 153 | -pub struct DrawingSurface { | ||
| 154 | - font_draw: FontDraw, | ||
| 155 | - pixmap: x11::xlib::Pixmap, | ||
| 156 | - display: *mut Display, | ||
| 157 | -} | ||
| 158 | - | ||
| 159 | -impl DrawingSurface { | ||
| 160 | - pub fn new( | ||
| 161 | - display: *mut Display, | ||
| 162 | - window: x11::xlib::Drawable, | ||
| 163 | - width: u32, | ||
| 164 | - height: u32, | ||
| 165 | - visual: *mut Visual, | ||
| 166 | - colormap: Colormap, | ||
| 167 | - ) -> Result<Self, crate::errors::X11Error> { | ||
| 168 | - let depth = unsafe { x11::xlib::XDefaultDepth(display, 0) }; | ||
| 169 | - let pixmap = unsafe { | ||
| 170 | - x11::xlib::XCreatePixmap(display, window, width, height, depth as u32) | ||
| 171 | - }; | ||
| 172 | - | ||
| 173 | - let font_draw = FontDraw::new(display, pixmap, visual, colormap)?; | ||
| 174 | - | ||
| 175 | - Ok(Self { | ||
| 176 | - font_draw, | ||
| 177 | - pixmap, | ||
| 178 | - display, | ||
| 179 | - }) | ||
| 180 | - } | ||
| 181 | - | ||
| 182 | - pub fn pixmap(&self) -> x11::xlib::Pixmap { | ||
| 183 | - self.pixmap | ||
| 184 | - } | ||
| 185 | - | ||
| 186 | - pub fn font_draw(&self) -> &FontDraw { | ||
| 187 | - &self.font_draw | ||
| 188 | - } | ||
| 189 | -} | ||
| 190 | - | ||
| 191 | -impl Drop for DrawingSurface { | ||
| 192 | - fn drop(&mut self) { | ||
| 193 | - unsafe { | ||
| 194 | - x11::xft::XftDrawDestroy(self.font_draw.xft_draw); | ||
| 195 | - self.font_draw.xft_draw = std::ptr::null_mut(); | ||
| 196 | - x11::xlib::XFreePixmap(self.display, self.pixmap); | ||
| 197 | - } | ||
| 198 | - } | ||
| 199 | -} | ||
+0 −13 src/bar/mod.rs deleted file mode 100644
| @@ -1,13 +0,0 @@ | |||
| 1 | -mod bar; | ||
| 2 | -mod blocks; | ||
| 3 | -pub mod font; | ||
| 4 | - | ||
| 5 | -pub use bar::Bar; | ||
| 6 | -pub use blocks::{BlockCommand, BlockConfig}; | ||
| 7 | - | ||
| 8 | -// Bar position (for future use) | ||
| 9 | -#[derive(Debug, Clone, Copy)] | ||
| 10 | -pub enum BarPosition { | ||
| 11 | - Top, | ||
| 12 | - Bottom, | ||
| 13 | -} | ||
+0 −192 src/bin/main.rs deleted file mode 100644
| @@ -1,192 +0,0 @@ | |||
| 1 | -use oxwm::errors::ConfigError; | ||
| 2 | -use oxwm::errors::MainError; | ||
| 3 | -use std::path::Path; | ||
| 4 | -use std::path::PathBuf; | ||
| 5 | - | ||
| 6 | -const CONFIG_FILE: &str = "config.lua"; | ||
| 7 | -const TEMPLATE: &str = include_str!("../../templates/config.lua"); | ||
| 8 | - | ||
| 9 | -enum Args { | ||
| 10 | - Exit, | ||
| 11 | - Arguments(Vec<String>), | ||
| 12 | - Error(MainError), | ||
| 13 | -} | ||
| 14 | - | ||
| 15 | -fn main() -> Result<(), MainError> { | ||
| 16 | - let arguments = match process_args() { | ||
| 17 | - Args::Exit => return Ok(()), | ||
| 18 | - Args::Arguments(v) => v, | ||
| 19 | - Args::Error(e) => return Err(e), | ||
| 20 | - }; | ||
| 21 | - | ||
| 22 | - let (config, config_warning) = load_config(arguments.get(2))?; | ||
| 23 | - | ||
| 24 | - let mut window_manager = match oxwm::window_manager::WindowManager::new(config) { | ||
| 25 | - Ok(wm) => wm, | ||
| 26 | - Err(e) => return Err(MainError::CouldNotStartWm(e)), | ||
| 27 | - }; | ||
| 28 | - | ||
| 29 | - if let Some(warning) = config_warning { | ||
| 30 | - window_manager.show_startup_config_error(warning); | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - if let Err(e) = window_manager.run() { | ||
| 34 | - return Err(MainError::WmError(e)); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - Ok(()) | ||
| 38 | -} | ||
| 39 | - | ||
| 40 | -fn load_config( | ||
| 41 | - config_path: Option<&String>, | ||
| 42 | -) -> Result<(oxwm::Config, Option<ConfigError>), MainError> { | ||
| 43 | - let path = match config_path { | ||
| 44 | - None => { | ||
| 45 | - let config_dir = get_config_path()?; | ||
| 46 | - let config_path = config_dir.join(CONFIG_FILE); | ||
| 47 | - check_convert(&config_path)?; | ||
| 48 | - config_path | ||
| 49 | - } | ||
| 50 | - Some(p) => PathBuf::from(p), | ||
| 51 | - }; | ||
| 52 | - | ||
| 53 | - let config_string = match std::fs::read_to_string(&path) { | ||
| 54 | - Ok(c) => c, | ||
| 55 | - Err(e) => return Err(MainError::FailedReadConfig(e)), | ||
| 56 | - }; | ||
| 57 | - | ||
| 58 | - let config_directory = path.parent(); | ||
| 59 | - | ||
| 60 | - let (mut config, config_warning) = | ||
| 61 | - match oxwm::config::parse_lua_config(&config_string, config_directory) { | ||
| 62 | - Ok(config) => (config, None), | ||
| 63 | - Err(warning) => { | ||
| 64 | - let config = match oxwm::config::parse_lua_config(TEMPLATE, None) { | ||
| 65 | - Ok(c) => c, | ||
| 66 | - Err(e) => return Err(MainError::FailedReadConfigTemplate(e)), | ||
| 67 | - }; | ||
| 68 | - (config, Some(warning)) | ||
| 69 | - } | ||
| 70 | - }; | ||
| 71 | - config.path = Some(path); | ||
| 72 | - Ok((config, config_warning)) | ||
| 73 | -} | ||
| 74 | - | ||
| 75 | -fn init_config() -> Result<(), MainError> { | ||
| 76 | - let config_directory = get_config_path()?; | ||
| 77 | - if let Err(e) = std::fs::create_dir_all(&config_directory) { | ||
| 78 | - return Err(MainError::CouldNotCreateConfigDir(e)); | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - let config_template = TEMPLATE; | ||
| 82 | - let config_path = config_directory.join(CONFIG_FILE); | ||
| 83 | - if let Err(e) = std::fs::write(&config_path, config_template) { | ||
| 84 | - return Err(MainError::CouldNotWriteConfig(e)); | ||
| 85 | - } | ||
| 86 | - | ||
| 87 | - println!("✓ Config created at {:?}", config_path); | ||
| 88 | - println!(" Edit the file and reload with Mod+Shift+R"); | ||
| 89 | - println!(" No compilation needed - changes take effect immediately!"); | ||
| 90 | - | ||
| 91 | - Ok(()) | ||
| 92 | -} | ||
| 93 | - | ||
| 94 | -fn get_config_path() -> Result<PathBuf, MainError> { | ||
| 95 | - match dirs::config_dir() { | ||
| 96 | - Some(p) => Ok(p.join("oxwm")), | ||
| 97 | - None => Err(MainError::NoConfigDir), | ||
| 98 | - } | ||
| 99 | -} | ||
| 100 | - | ||
| 101 | -fn print_help() { | ||
| 102 | - println!("OXWM - A dynamic window manager written in Rust\n"); | ||
| 103 | - println!("USAGE:"); | ||
| 104 | - println!(" oxwm [OPTIONS]\n"); | ||
| 105 | - println!("OPTIONS:"); | ||
| 106 | - println!(" --init Create default config in ~/.config/oxwm/config.lua"); | ||
| 107 | - println!(" --config <PATH> Use custom config file"); | ||
| 108 | - println!(" --version Print version information"); | ||
| 109 | - println!(" --help Print this help message\n"); | ||
| 110 | - println!("CONFIG:"); | ||
| 111 | - println!(" Location: ~/.config/oxwm/config.lua"); | ||
| 112 | - println!(" Edit the config file and use Mod+Shift+R to reload"); | ||
| 113 | - println!(" No compilation needed - instant hot-reload!"); | ||
| 114 | - println!(" LSP support included with oxwm.lua type definitions\n"); | ||
| 115 | - println!("FIRST RUN:"); | ||
| 116 | - println!(" Run 'oxwm --init' to create a config file"); | ||
| 117 | - println!(" Or just start oxwm and it will create one automatically\n"); | ||
| 118 | -} | ||
| 119 | - | ||
| 120 | -fn process_args() -> Args { | ||
| 121 | - let mut args = std::env::args(); | ||
| 122 | - let name = match args.next() { | ||
| 123 | - Some(n) => n, | ||
| 124 | - None => return Args::Error(MainError::NoProgramName), | ||
| 125 | - }; | ||
| 126 | - let switch = args.next(); | ||
| 127 | - let path = args.next(); | ||
| 128 | - | ||
| 129 | - let switch = match switch { | ||
| 130 | - Some(s) => s, | ||
| 131 | - None => return Args::Arguments(vec![name]), | ||
| 132 | - }; | ||
| 133 | - | ||
| 134 | - match switch.as_str() { | ||
| 135 | - "--version" => { | ||
| 136 | - println!("{name} {}", env!("CARGO_PKG_VERSION")); | ||
| 137 | - Args::Exit | ||
| 138 | - } | ||
| 139 | - "--help" => { | ||
| 140 | - print_help(); | ||
| 141 | - Args::Exit | ||
| 142 | - } | ||
| 143 | - "--init" => match init_config() { | ||
| 144 | - Ok(_) => Args::Exit, | ||
| 145 | - Err(e) => Args::Error(e), | ||
| 146 | - }, | ||
| 147 | - "--config" => match check_custom_config(path) { | ||
| 148 | - Ok(p) => Args::Arguments(vec![name, switch, p]), | ||
| 149 | - Err(e) => Args::Error(e), | ||
| 150 | - }, | ||
| 151 | - _ => Args::Error(MainError::InvalidArguments), | ||
| 152 | - } | ||
| 153 | -} | ||
| 154 | - | ||
| 155 | -fn check_custom_config(path: Option<String>) -> Result<String, MainError> { | ||
| 156 | - let path = match path { | ||
| 157 | - Some(p) => p, | ||
| 158 | - None => { | ||
| 159 | - return Err(MainError::NoConfigPath); | ||
| 160 | - } | ||
| 161 | - }; | ||
| 162 | - | ||
| 163 | - match std::fs::exists(&path) { | ||
| 164 | - Ok(b) => match b { | ||
| 165 | - true => Ok(path), | ||
| 166 | - false => Err(MainError::BadConfigPath), | ||
| 167 | - }, | ||
| 168 | - Err(e) => Err(MainError::FailedCheckExist(e)), | ||
| 169 | - } | ||
| 170 | -} | ||
| 171 | - | ||
| 172 | -fn check_convert(path: &Path) -> Result<(), MainError> { | ||
| 173 | - let config_directory = get_config_path()?; | ||
| 174 | - | ||
| 175 | - if !path.exists() { | ||
| 176 | - let ron_path = config_directory.join("config.ron"); | ||
| 177 | - let had_ron_config = ron_path.exists(); | ||
| 178 | - | ||
| 179 | - println!("No config found at {:?}", config_directory); | ||
| 180 | - println!("Creating default Lua config..."); | ||
| 181 | - init_config()?; | ||
| 182 | - | ||
| 183 | - if had_ron_config { | ||
| 184 | - println!("\n NOTICE: OXWM has migrated to Lua configuration."); | ||
| 185 | - println!(" Your old config.ron has been preserved, but is no longer used."); | ||
| 186 | - println!(" Your settings have been reset to defaults."); | ||
| 187 | - println!(" Please manually port your configuration to the new Lua format."); | ||
| 188 | - println!(" See the new config.lua template for examples.\n"); | ||
| 189 | - } | ||
| 190 | - } | ||
| 191 | - Ok(()) | ||
| 192 | -} | ||
+259 −0 src/client.zig new file mode 100644
| @@ -0,0 +1,259 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const xlib = @import("x11/xlib.zig"); | ||
| 3 | +const Monitor = @import("monitor.zig").Monitor; | ||
| 4 | + | ||
| 5 | +pub const Client = struct { | ||
| 6 | + name: [256]u8 = std.mem.zeroes([256]u8), | ||
| 7 | + min_aspect: f32 = 0, | ||
| 8 | + max_aspect: f32 = 0, | ||
| 9 | + x: i32 = 0, | ||
| 10 | + y: i32 = 0, | ||
| 11 | + width: i32 = 0, | ||
| 12 | + height: i32 = 0, | ||
| 13 | + old_x: i32 = 0, | ||
| 14 | + old_y: i32 = 0, | ||
| 15 | + old_width: i32 = 0, | ||
| 16 | + old_height: i32 = 0, | ||
| 17 | + base_width: i32 = 0, | ||
| 18 | + base_height: i32 = 0, | ||
| 19 | + increment_width: i32 = 0, | ||
| 20 | + increment_height: i32 = 0, | ||
| 21 | + max_width: i32 = 0, | ||
| 22 | + max_height: i32 = 0, | ||
| 23 | + min_width: i32 = 0, | ||
| 24 | + min_height: i32 = 0, | ||
| 25 | + hints_valid: bool = false, | ||
| 26 | + border_width: i32 = 0, | ||
| 27 | + old_border_width: i32 = 0, | ||
| 28 | + tags: u32 = 0, | ||
| 29 | + is_fixed: bool = false, | ||
| 30 | + is_floating: bool = false, | ||
| 31 | + is_urgent: bool = false, | ||
| 32 | + never_focus: bool = false, | ||
| 33 | + old_state: bool = false, | ||
| 34 | + is_fullscreen: bool = false, | ||
| 35 | + next: ?*Client = null, | ||
| 36 | + stack_next: ?*Client = null, | ||
| 37 | + monitor: ?*Monitor = null, | ||
| 38 | + window: xlib.Window = 0, | ||
| 39 | +}; | ||
| 40 | + | ||
| 41 | +var allocator: std.mem.Allocator = undefined; | ||
| 42 | + | ||
| 43 | +pub fn init(alloc: std.mem.Allocator) void { | ||
| 44 | + allocator = alloc; | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +pub fn create(window: xlib.Window) ?*Client { | ||
| 48 | + const client = allocator.create(Client) catch return null; | ||
| 49 | + client.* = Client{ .window = window }; | ||
| 50 | + return client; | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +pub fn destroy(client: *Client) void { | ||
| 54 | + allocator.destroy(client); | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +pub fn attach(client: *Client) void { | ||
| 58 | + if (client.monitor) |monitor| { | ||
| 59 | + client.next = monitor.clients; | ||
| 60 | + monitor.clients = client; | ||
| 61 | + } | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +pub fn detach(client: *Client) void { | ||
| 65 | + if (client.monitor) |monitor| { | ||
| 66 | + var current_ptr: *?*Client = &monitor.clients; | ||
| 67 | + while (current_ptr.*) |current| { | ||
| 68 | + if (current == client) { | ||
| 69 | + current_ptr.* = client.next; | ||
| 70 | + return; | ||
| 71 | + } | ||
| 72 | + current_ptr = ¤t.next; | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +pub fn attach_stack(client: *Client) void { | ||
| 78 | + if (client.monitor) |monitor| { | ||
| 79 | + client.stack_next = monitor.stack; | ||
| 80 | + monitor.stack = client; | ||
| 81 | + } | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +pub fn detach_stack(client: *Client) void { | ||
| 85 | + if (client.monitor) |monitor| { | ||
| 86 | + var current_ptr: *?*Client = &monitor.stack; | ||
| 87 | + while (current_ptr.*) |current| { | ||
| 88 | + if (current == client) { | ||
| 89 | + current_ptr.* = client.stack_next; | ||
| 90 | + return; | ||
| 91 | + } | ||
| 92 | + current_ptr = ¤t.stack_next; | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +pub fn window_to_client(window: xlib.Window) ?*Client { | ||
| 98 | + const monitor_mod = @import("monitor.zig"); | ||
| 99 | + var current_monitor = monitor_mod.monitors; | ||
| 100 | + while (current_monitor) |monitor| { | ||
| 101 | + var current_client = monitor.clients; | ||
| 102 | + while (current_client) |client| { | ||
| 103 | + if (client.window == window) { | ||
| 104 | + return client; | ||
| 105 | + } | ||
| 106 | + current_client = client.next; | ||
| 107 | + } | ||
| 108 | + current_monitor = monitor.next; | ||
| 109 | + } | ||
| 110 | + return null; | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +pub fn next_tiled(client: ?*Client) ?*Client { | ||
| 114 | + var current = client; | ||
| 115 | + while (current) |iter| { | ||
| 116 | + if (!iter.is_floating and is_visible(iter)) { | ||
| 117 | + return iter; | ||
| 118 | + } | ||
| 119 | + current = iter.next; | ||
| 120 | + } | ||
| 121 | + return null; | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +pub fn is_visible(client: *Client) bool { | ||
| 125 | + if (client.monitor) |monitor| { | ||
| 126 | + return (client.tags & monitor.tagset[monitor.sel_tags]) != 0; | ||
| 127 | + } | ||
| 128 | + return false; | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +pub fn is_visible_on_tag(client: *Client, tags: u32) bool { | ||
| 132 | + return (client.tags & tags) != 0; | ||
| 133 | +} | ||
| 134 | + | ||
| 135 | +pub fn next_tagged(client: *Client) ?*Client { | ||
| 136 | + const monitor = client.monitor orelse return null; | ||
| 137 | + var walked = monitor.clients; | ||
| 138 | + while (walked) |iter| { | ||
| 139 | + if (!iter.is_floating and is_visible_on_tag(iter, client.tags)) { | ||
| 140 | + return iter; | ||
| 141 | + } | ||
| 142 | + walked = iter.next; | ||
| 143 | + } | ||
| 144 | + return null; | ||
| 145 | +} | ||
| 146 | + | ||
| 147 | +pub fn attach_aside(client: *Client) void { | ||
| 148 | + const at = next_tagged(client); | ||
| 149 | + if (at == null) { | ||
| 150 | + attach(client); | ||
| 151 | + return; | ||
| 152 | + } | ||
| 153 | + client.next = at.?.next; | ||
| 154 | + at.?.next = client; | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +pub fn count_tiled(monitor: *Monitor) u32 { | ||
| 158 | + var count: u32 = 0; | ||
| 159 | + var current = next_tiled(monitor.clients); | ||
| 160 | + while (current) |client| { | ||
| 161 | + count += 1; | ||
| 162 | + current = next_tiled(client.next); | ||
| 163 | + } | ||
| 164 | + return count; | ||
| 165 | +} | ||
| 166 | + | ||
| 167 | +pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client { | ||
| 168 | + const tags = monitor.tagset[monitor.sel_tags]; | ||
| 169 | + var current = monitor.clients; | ||
| 170 | + | ||
| 171 | + while (current) |client| { | ||
| 172 | + if (client != exclude and !client.is_floating and (client.tags & tags) != 0) { | ||
| 173 | + const client_x = client.x; | ||
| 174 | + const client_y = client.y; | ||
| 175 | + const client_w = client.width + client.border_width * 2; | ||
| 176 | + const client_h = client.height + client.border_width * 2; | ||
| 177 | + | ||
| 178 | + if (point_x >= client_x and point_x < client_x + client_w and | ||
| 179 | + point_y >= client_y and point_y < client_y + client_h) | ||
| 180 | + { | ||
| 181 | + return client; | ||
| 182 | + } | ||
| 183 | + } | ||
| 184 | + current = client.next; | ||
| 185 | + } | ||
| 186 | + return null; | ||
| 187 | +} | ||
| 188 | + | ||
| 189 | +pub fn insert_before(client: *Client, target: *Client) void { | ||
| 190 | + const monitor = target.monitor orelse return; | ||
| 191 | + if (client.monitor != monitor) return; | ||
| 192 | + | ||
| 193 | + detach(client); | ||
| 194 | + | ||
| 195 | + if (monitor.clients == target) { | ||
| 196 | + client.next = target; | ||
| 197 | + monitor.clients = client; | ||
| 198 | + return; | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + var current = monitor.clients; | ||
| 202 | + while (current) |iter| { | ||
| 203 | + if (iter.next == target) { | ||
| 204 | + client.next = target; | ||
| 205 | + iter.next = client; | ||
| 206 | + return; | ||
| 207 | + } | ||
| 208 | + current = iter.next; | ||
| 209 | + } | ||
| 210 | +} | ||
| 211 | + | ||
| 212 | +pub fn swap_clients(client_a: *Client, client_b: *Client) void { | ||
| 213 | + const monitor = client_a.monitor orelse return; | ||
| 214 | + if (client_b.monitor != monitor) return; | ||
| 215 | + | ||
| 216 | + var prev_a: ?*Client = null; | ||
| 217 | + var prev_b: ?*Client = null; | ||
| 218 | + var iter = monitor.clients; | ||
| 219 | + | ||
| 220 | + while (iter) |client| { | ||
| 221 | + if (client.next == client_a) prev_a = client; | ||
| 222 | + if (client.next == client_b) prev_b = client; | ||
| 223 | + iter = client.next; | ||
| 224 | + } | ||
| 225 | + | ||
| 226 | + const next_a = client_a.next; | ||
| 227 | + const next_b = client_b.next; | ||
| 228 | + | ||
| 229 | + if (next_a == client_b) { | ||
| 230 | + client_a.next = next_b; | ||
| 231 | + client_b.next = client_a; | ||
| 232 | + if (prev_a) |prev| { | ||
| 233 | + prev.next = client_b; | ||
| 234 | + } else { | ||
| 235 | + monitor.clients = client_b; | ||
| 236 | + } | ||
| 237 | + } else if (next_b == client_a) { | ||
| 238 | + client_b.next = next_a; | ||
| 239 | + client_a.next = client_b; | ||
| 240 | + if (prev_b) |prev| { | ||
| 241 | + prev.next = client_a; | ||
| 242 | + } else { | ||
| 243 | + monitor.clients = client_a; | ||
| 244 | + } | ||
| 245 | + } else { | ||
| 246 | + client_a.next = next_b; | ||
| 247 | + client_b.next = next_a; | ||
| 248 | + if (prev_a) |prev| { | ||
| 249 | + prev.next = client_b; | ||
| 250 | + } else { | ||
| 251 | + monitor.clients = client_b; | ||
| 252 | + } | ||
| 253 | + if (prev_b) |prev| { | ||
| 254 | + prev.next = client_a; | ||
| 255 | + } else { | ||
| 256 | + monitor.clients = client_a; | ||
| 257 | + } | ||
| 258 | + } | ||
| 259 | +} | ||
+0 −88 src/client/mod.rs deleted file mode 100644
| @@ -1,88 +0,0 @@ | |||
| 1 | -use x11rb::protocol::xproto::Window; | ||
| 2 | - | ||
| 3 | -pub type TagMask = u32; | ||
| 4 | - | ||
| 5 | -#[derive(Debug, Clone)] | ||
| 6 | -pub struct Client { | ||
| 7 | - pub name: String, | ||
| 8 | - pub min_aspect: f32, | ||
| 9 | - pub max_aspect: f32, | ||
| 10 | - pub x_position: i16, | ||
| 11 | - pub y_position: i16, | ||
| 12 | - pub width: u16, | ||
| 13 | - pub height: u16, | ||
| 14 | - pub old_x_position: i16, | ||
| 15 | - pub old_y_position: i16, | ||
| 16 | - pub old_width: u16, | ||
| 17 | - pub old_height: u16, | ||
| 18 | - pub base_width: i32, | ||
| 19 | - pub base_height: i32, | ||
| 20 | - pub increment_width: i32, | ||
| 21 | - pub increment_height: i32, | ||
| 22 | - pub max_width: i32, | ||
| 23 | - pub max_height: i32, | ||
| 24 | - pub min_width: i32, | ||
| 25 | - pub min_height: i32, | ||
| 26 | - pub hints_valid: bool, | ||
| 27 | - pub border_width: u16, | ||
| 28 | - pub old_border_width: u16, | ||
| 29 | - pub tags: TagMask, | ||
| 30 | - pub is_fixed: bool, | ||
| 31 | - pub is_floating: bool, | ||
| 32 | - pub is_urgent: bool, | ||
| 33 | - pub never_focus: bool, | ||
| 34 | - pub old_state: bool, | ||
| 35 | - pub is_fullscreen: bool, | ||
| 36 | - pub next: Option<Window>, | ||
| 37 | - pub stack_next: Option<Window>, | ||
| 38 | - pub monitor_index: usize, | ||
| 39 | - pub window: Window, | ||
| 40 | -} | ||
| 41 | - | ||
| 42 | -impl Client { | ||
| 43 | - pub fn new(window: Window, monitor_index: usize, tags: TagMask) -> Self { | ||
| 44 | - Self { | ||
| 45 | - name: String::new(), | ||
| 46 | - min_aspect: 0.0, | ||
| 47 | - max_aspect: 0.0, | ||
| 48 | - x_position: 0, | ||
| 49 | - y_position: 0, | ||
| 50 | - width: 0, | ||
| 51 | - height: 0, | ||
| 52 | - old_x_position: 0, | ||
| 53 | - old_y_position: 0, | ||
| 54 | - old_width: 0, | ||
| 55 | - old_height: 0, | ||
| 56 | - base_width: 0, | ||
| 57 | - base_height: 0, | ||
| 58 | - increment_width: 0, | ||
| 59 | - increment_height: 0, | ||
| 60 | - max_width: 0, | ||
| 61 | - max_height: 0, | ||
| 62 | - min_width: 0, | ||
| 63 | - min_height: 0, | ||
| 64 | - hints_valid: false, | ||
| 65 | - border_width: 0, | ||
| 66 | - old_border_width: 0, | ||
| 67 | - tags, | ||
| 68 | - is_fixed: false, | ||
| 69 | - is_floating: false, | ||
| 70 | - is_urgent: false, | ||
| 71 | - never_focus: false, | ||
| 72 | - old_state: false, | ||
| 73 | - is_fullscreen: false, | ||
| 74 | - next: None, | ||
| 75 | - stack_next: None, | ||
| 76 | - monitor_index, | ||
| 77 | - window, | ||
| 78 | - } | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | - pub fn width_with_border(&self) -> u16 { | ||
| 82 | - self.width.saturating_add(2 * self.border_width) | ||
| 83 | - } | ||
| 84 | - | ||
| 85 | - pub fn height_with_border(&self) -> u16 { | ||
| 86 | - self.height.saturating_add(2 * self.border_width) | ||
| 87 | - } | ||
| 88 | -} | ||
+185 −0 src/config/config.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,185 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +pub const Action = enum { | ||
| 4 | + spawn_terminal, | ||
| 5 | + spawn, | ||
| 6 | + kill_client, | ||
| 7 | + quit, | ||
| 8 | + reload_config, | ||
| 9 | + restart, | ||
| 10 | + focus_next, | ||
| 11 | + focus_prev, | ||
| 12 | + move_next, | ||
| 13 | + move_prev, | ||
| 14 | + resize_master, | ||
| 15 | + inc_master, | ||
| 16 | + dec_master, | ||
| 17 | + toggle_floating, | ||
| 18 | + toggle_fullscreen, | ||
| 19 | + toggle_gaps, | ||
| 20 | + cycle_layout, | ||
| 21 | + set_layout, | ||
| 22 | + set_layout_tiling, | ||
| 23 | + set_layout_floating, | ||
| 24 | + view_tag, | ||
| 25 | + view_next_tag, | ||
| 26 | + view_prev_tag, | ||
| 27 | + view_next_nonempty_tag, | ||
| 28 | + view_prev_nonempty_tag, | ||
| 29 | + move_to_tag, | ||
| 30 | + toggle_view_tag, | ||
| 31 | + toggle_tag, | ||
| 32 | + focus_monitor, | ||
| 33 | + send_to_monitor, | ||
| 34 | + scroll_left, | ||
| 35 | + scroll_right, | ||
| 36 | +}; | ||
| 37 | + | ||
| 38 | +pub const Keybind = struct { | ||
| 39 | + mod_mask: u32, | ||
| 40 | + keysym: u64, | ||
| 41 | + action: Action, | ||
| 42 | + int_arg: i32 = 0, | ||
| 43 | + str_arg: ?[]const u8 = null, | ||
| 44 | +}; | ||
| 45 | + | ||
| 46 | +pub const Rule = struct { | ||
| 47 | + class: ?[]const u8, | ||
| 48 | + instance: ?[]const u8, | ||
| 49 | + title: ?[]const u8, | ||
| 50 | + tags: u32, | ||
| 51 | + is_floating: bool, | ||
| 52 | + monitor: i32, | ||
| 53 | +}; | ||
| 54 | + | ||
| 55 | +pub const Block_Type = enum { | ||
| 56 | + static, | ||
| 57 | + datetime, | ||
| 58 | + ram, | ||
| 59 | + shell, | ||
| 60 | + battery, | ||
| 61 | + cpu_temp, | ||
| 62 | +}; | ||
| 63 | + | ||
| 64 | +pub const Click_Target = enum { | ||
| 65 | + client_win, | ||
| 66 | + root_win, | ||
| 67 | + tag_bar, | ||
| 68 | +}; | ||
| 69 | + | ||
| 70 | +pub const Mouse_Action = enum { | ||
| 71 | + move_mouse, | ||
| 72 | + resize_mouse, | ||
| 73 | + toggle_floating, | ||
| 74 | +}; | ||
| 75 | + | ||
| 76 | +pub const Mouse_Button = struct { | ||
| 77 | + click: Click_Target, | ||
| 78 | + mod_mask: u32, | ||
| 79 | + button: u32, | ||
| 80 | + action: Mouse_Action, | ||
| 81 | +}; | ||
| 82 | + | ||
| 83 | +pub const Block = struct { | ||
| 84 | + block_type: Block_Type, | ||
| 85 | + format: []const u8, | ||
| 86 | + command: ?[]const u8 = null, | ||
| 87 | + interval: u32, | ||
| 88 | + color: u32, | ||
| 89 | + underline: bool = true, | ||
| 90 | + datetime_format: ?[]const u8 = null, | ||
| 91 | + format_charging: ?[]const u8 = null, | ||
| 92 | + format_discharging: ?[]const u8 = null, | ||
| 93 | + format_full: ?[]const u8 = null, | ||
| 94 | + battery_name: ?[]const u8 = null, | ||
| 95 | + thermal_zone: ?[]const u8 = null, | ||
| 96 | +}; | ||
| 97 | + | ||
| 98 | +pub const Color_Scheme = struct { | ||
| 99 | + fg: u32 = 0xbbbbbb, | ||
| 100 | + bg: u32 = 0x1a1b26, | ||
| 101 | + border: u32 = 0x444444, | ||
| 102 | +}; | ||
| 103 | + | ||
| 104 | +pub const Config = struct { | ||
| 105 | + allocator: std.mem.Allocator, | ||
| 106 | + | ||
| 107 | + terminal: []const u8 = "st", | ||
| 108 | + font: []const u8 = "monospace:size=10", | ||
| 109 | + tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" }, | ||
| 110 | + | ||
| 111 | + border_width: i32 = 2, | ||
| 112 | + border_focused: u32 = 0x6dade3, | ||
| 113 | + border_unfocused: u32 = 0x444444, | ||
| 114 | + | ||
| 115 | + gaps_enabled: bool = true, | ||
| 116 | + smartgaps_enabled: bool = false, | ||
| 117 | + gap_inner_h: i32 = 5, | ||
| 118 | + gap_inner_v: i32 = 5, | ||
| 119 | + gap_outer_h: i32 = 5, | ||
| 120 | + gap_outer_v: i32 = 5, | ||
| 121 | + | ||
| 122 | + modkey: u32 = (1 << 6), | ||
| 123 | + auto_tile: bool = false, | ||
| 124 | + tag_back_and_forth: bool = false, | ||
| 125 | + hide_vacant_tags: bool = false, | ||
| 126 | + | ||
| 127 | + layout_tile_symbol: []const u8 = "[]=", | ||
| 128 | + layout_monocle_symbol: []const u8 = "[M]", | ||
| 129 | + layout_floating_symbol: []const u8 = "><>", | ||
| 130 | + | ||
| 131 | + scheme_normal: Color_Scheme = .{ .fg = 0xbbbbbb, .bg = 0x1a1b26, .border = 0x444444 }, | ||
| 132 | + scheme_selected: Color_Scheme = .{ .fg = 0x0db9d7, .bg = 0x1a1b26, .border = 0xad8ee6 }, | ||
| 133 | + scheme_occupied: Color_Scheme = .{ .fg = 0x0db9d7, .bg = 0x1a1b26, .border = 0x0db9d7 }, | ||
| 134 | + scheme_urgent: Color_Scheme = .{ .fg = 0xf7768e, .bg = 0x1a1b26, .border = 0xf7768e }, | ||
| 135 | + | ||
| 136 | + keybinds: std.ArrayListUnmanaged(Keybind) = .{}, | ||
| 137 | + rules: std.ArrayListUnmanaged(Rule) = .{}, | ||
| 138 | + blocks: std.ArrayListUnmanaged(Block) = .{}, | ||
| 139 | + buttons: std.ArrayListUnmanaged(Mouse_Button) = .{}, | ||
| 140 | + autostart: std.ArrayListUnmanaged([]const u8) = .{}, | ||
| 141 | + | ||
| 142 | + pub fn init(allocator: std.mem.Allocator) Config { | ||
| 143 | + return Config{ | ||
| 144 | + .allocator = allocator, | ||
| 145 | + }; | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + pub fn deinit(self: *Config) void { | ||
| 149 | + self.keybinds.deinit(self.allocator); | ||
| 150 | + self.rules.deinit(self.allocator); | ||
| 151 | + self.blocks.deinit(self.allocator); | ||
| 152 | + self.buttons.deinit(self.allocator); | ||
| 153 | + self.autostart.deinit(self.allocator); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + pub fn add_keybind(self: *Config, keybind: Keybind) !void { | ||
| 157 | + try self.keybinds.append(self.allocator, keybind); | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + pub fn add_rule(self: *Config, rule: Rule) !void { | ||
| 161 | + try self.rules.append(self.allocator, rule); | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + pub fn add_block(self: *Config, block: Block) !void { | ||
| 165 | + try self.blocks.append(self.allocator, block); | ||
| 166 | + } | ||
| 167 | + | ||
| 168 | + pub fn add_button(self: *Config, button: Mouse_Button) !void { | ||
| 169 | + try self.buttons.append(self.allocator, button); | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + pub fn add_autostart(self: *Config, cmd: []const u8) !void { | ||
| 173 | + try self.autostart.append(self.allocator, cmd); | ||
| 174 | + } | ||
| 175 | +}; | ||
| 176 | + | ||
| 177 | +pub var global_config: ?*Config = null; | ||
| 178 | + | ||
| 179 | +pub fn get_config() ?*Config { | ||
| 180 | + return global_config; | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +pub fn set_config(cfg: *Config) void { | ||
| 184 | + global_config = cfg; | ||
| 185 | +} | ||
+0 −57 src/config/lua.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,57 +0,0 @@ | |||
| 1 | -use crate::errors::ConfigError; | ||
| 2 | -use mlua::Lua; | ||
| 3 | - | ||
| 4 | -use super::lua_api; | ||
| 5 | - | ||
| 6 | -pub fn parse_lua_config( | ||
| 7 | - input: &str, | ||
| 8 | - config_dir: Option<&std::path::Path>, | ||
| 9 | -) -> Result<crate::Config, ConfigError> { | ||
| 10 | - let lua = Lua::new(); | ||
| 11 | - | ||
| 12 | - if let Some(dir) = config_dir | ||
| 13 | - && let Some(dir_str) = dir.to_str() | ||
| 14 | - { | ||
| 15 | - let setup_code = format!("package.path = '{}/?.lua;' .. package.path", dir_str); | ||
| 16 | - lua.load(&setup_code) | ||
| 17 | - .exec() | ||
| 18 | - .map_err(|e| ConfigError::LuaError(format!("Failed to set package.path: {}", e)))?; | ||
| 19 | - } | ||
| 20 | - | ||
| 21 | - let builder = lua_api::register_api(&lua)?; | ||
| 22 | - | ||
| 23 | - lua.load(input) | ||
| 24 | - .exec() | ||
| 25 | - .map_err(|e| ConfigError::LuaError(format!("{}", e)))?; | ||
| 26 | - | ||
| 27 | - let builder_data = builder.borrow().clone(); | ||
| 28 | - | ||
| 29 | - Ok(crate::Config { | ||
| 30 | - border_width: builder_data.border_width, | ||
| 31 | - border_focused: builder_data.border_focused, | ||
| 32 | - border_unfocused: builder_data.border_unfocused, | ||
| 33 | - font: builder_data.font, | ||
| 34 | - gaps_enabled: builder_data.gaps_enabled, | ||
| 35 | - smartgaps_enabled: builder_data.smartgaps_enabled, | ||
| 36 | - gap_inner_horizontal: builder_data.gap_inner_horizontal, | ||
| 37 | - gap_inner_vertical: builder_data.gap_inner_vertical, | ||
| 38 | - gap_outer_horizontal: builder_data.gap_outer_horizontal, | ||
| 39 | - gap_outer_vertical: builder_data.gap_outer_vertical, | ||
| 40 | - terminal: builder_data.terminal, | ||
| 41 | - modkey: builder_data.modkey, | ||
| 42 | - tags: builder_data.tags, | ||
| 43 | - layout_symbols: builder_data.layout_symbols, | ||
| 44 | - keybindings: builder_data.keybindings, | ||
| 45 | - tag_back_and_forth: builder_data.tag_back_and_forth, | ||
| 46 | - window_rules: builder_data.window_rules, | ||
| 47 | - status_blocks: builder_data.status_blocks, | ||
| 48 | - scheme_normal: builder_data.scheme_normal, | ||
| 49 | - scheme_occupied: builder_data.scheme_occupied, | ||
| 50 | - scheme_selected: builder_data.scheme_selected, | ||
| 51 | - scheme_urgent: builder_data.scheme_urgent, | ||
| 52 | - autostart: builder_data.autostart, | ||
| 53 | - auto_tile: builder_data.auto_tile, | ||
| 54 | - hide_vacant_tags: builder_data.hide_vacant_tags, | ||
| 55 | - path: None, | ||
| 56 | - }) | ||
| 57 | -} | ||
+1172 −0 src/config/lua.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,1172 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const config_mod = @import("config.zig"); | ||
| 3 | +const Config = config_mod.Config; | ||
| 4 | +const Keybind = config_mod.Keybind; | ||
| 5 | +const Action = config_mod.Action; | ||
| 6 | +const Rule = config_mod.Rule; | ||
| 7 | +const Block = config_mod.Block; | ||
| 8 | +const Block_Type = config_mod.Block_Type; | ||
| 9 | +const Mouse_Button = config_mod.Mouse_Button; | ||
| 10 | +const Click_Target = config_mod.Click_Target; | ||
| 11 | +const Mouse_Action = config_mod.Mouse_Action; | ||
| 12 | +const Color_Scheme = config_mod.Color_Scheme; | ||
| 13 | + | ||
| 14 | +const c = @cImport({ | ||
| 15 | + @cInclude("lua.h"); | ||
| 16 | + @cInclude("lauxlib.h"); | ||
| 17 | + @cInclude("lualib.h"); | ||
| 18 | +}); | ||
| 19 | + | ||
| 20 | +var L: ?*c.lua_State = null; | ||
| 21 | +var config: ?*Config = null; | ||
| 22 | + | ||
| 23 | +pub fn init(cfg: *Config) bool { | ||
| 24 | + config = cfg; | ||
| 25 | + L = c.luaL_newstate(); | ||
| 26 | + if (L == null) return false; | ||
| 27 | + c.luaL_openlibs(L); | ||
| 28 | + register_api(); | ||
| 29 | + return true; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +pub fn deinit() void { | ||
| 33 | + if (L) |state| { | ||
| 34 | + c.lua_close(state); | ||
| 35 | + } | ||
| 36 | + L = null; | ||
| 37 | + config = null; | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +pub fn load_file(path: []const u8) bool { | ||
| 41 | + const state = L orelse return false; | ||
| 42 | + var path_buf: [512]u8 = undefined; | ||
| 43 | + if (path.len >= path_buf.len) return false; | ||
| 44 | + @memcpy(path_buf[0..path.len], path); | ||
| 45 | + path_buf[path.len] = 0; | ||
| 46 | + | ||
| 47 | + if (c.luaL_loadfilex(state, &path_buf, null) != 0) { | ||
| 48 | + const err = c.lua_tolstring(state, -1, null); | ||
| 49 | + if (err != null) { | ||
| 50 | + std.debug.print("lua load error: {s}\n", .{std.mem.span(err)}); | ||
| 51 | + } | ||
| 52 | + c.lua_settop(state, -2); | ||
| 53 | + return false; | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + if (c.lua_pcallk(state, 0, 0, 0, 0, null) != 0) { | ||
| 57 | + const err = c.lua_tolstring(state, -1, null); | ||
| 58 | + if (err != null) { | ||
| 59 | + std.debug.print("lua runtime error: {s}\n", .{std.mem.span(err)}); | ||
| 60 | + } | ||
| 61 | + c.lua_settop(state, -2); | ||
| 62 | + return false; | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + return true; | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +pub fn load_config() bool { | ||
| 69 | + const home = std.posix.getenv("HOME") orelse return false; | ||
| 70 | + var path_buf: [512]u8 = undefined; | ||
| 71 | + const path = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm/config.lua", .{home}) catch return false; | ||
| 72 | + return load_file(path); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +fn register_api() void { | ||
| 76 | + const state = L orelse return; | ||
| 77 | + | ||
| 78 | + c.lua_createtable(state, 0, 16); | ||
| 79 | + | ||
| 80 | + register_spawn_functions(state); | ||
| 81 | + register_key_module(state); | ||
| 82 | + register_gaps_module(state); | ||
| 83 | + register_border_module(state); | ||
| 84 | + register_client_module(state); | ||
| 85 | + register_layout_module(state); | ||
| 86 | + register_tag_module(state); | ||
| 87 | + register_monitor_module(state); | ||
| 88 | + register_rule_module(state); | ||
| 89 | + register_bar_module(state); | ||
| 90 | + register_misc_functions(state); | ||
| 91 | + | ||
| 92 | + c.lua_setglobal(state, "oxwm"); | ||
| 93 | +} | ||
| 94 | + | ||
| 95 | +fn register_spawn_functions(state: *c.lua_State) void { | ||
| 96 | + c.lua_pushcfunction(state, lua_spawn); | ||
| 97 | + c.lua_setfield(state, -2, "spawn"); | ||
| 98 | + | ||
| 99 | + c.lua_pushcfunction(state, lua_spawn_terminal); | ||
| 100 | + c.lua_setfield(state, -2, "spawn_terminal"); | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +fn register_key_module(state: *c.lua_State) void { | ||
| 104 | + c.lua_createtable(state, 0, 2); | ||
| 105 | + | ||
| 106 | + c.lua_pushcfunction(state, lua_key_bind); | ||
| 107 | + c.lua_setfield(state, -2, "bind"); | ||
| 108 | + | ||
| 109 | + c.lua_pushcfunction(state, lua_key_chord); | ||
| 110 | + c.lua_setfield(state, -2, "chord"); | ||
| 111 | + | ||
| 112 | + c.lua_setfield(state, -2, "key"); | ||
| 113 | +} | ||
| 114 | + | ||
| 115 | +fn register_gaps_module(state: *c.lua_State) void { | ||
| 116 | + c.lua_createtable(state, 0, 6); | ||
| 117 | + | ||
| 118 | + c.lua_pushcfunction(state, lua_gaps_set_enabled); | ||
| 119 | + c.lua_setfield(state, -2, "set_enabled"); | ||
| 120 | + | ||
| 121 | + c.lua_pushcfunction(state, lua_gaps_enable); | ||
| 122 | + c.lua_setfield(state, -2, "enable"); | ||
| 123 | + | ||
| 124 | + c.lua_pushcfunction(state, lua_gaps_disable); | ||
| 125 | + c.lua_setfield(state, -2, "disable"); | ||
| 126 | + | ||
| 127 | + c.lua_pushcfunction(state, lua_gaps_set_inner); | ||
| 128 | + c.lua_setfield(state, -2, "set_inner"); | ||
| 129 | + | ||
| 130 | + c.lua_pushcfunction(state, lua_gaps_set_outer); | ||
| 131 | + c.lua_setfield(state, -2, "set_outer"); | ||
| 132 | + | ||
| 133 | + c.lua_pushcfunction(state, lua_gaps_set_smart); | ||
| 134 | + c.lua_setfield(state, -2, "set_smart"); | ||
| 135 | + | ||
| 136 | + c.lua_setfield(state, -2, "gaps"); | ||
| 137 | +} | ||
| 138 | + | ||
| 139 | +fn register_border_module(state: *c.lua_State) void { | ||
| 140 | + c.lua_createtable(state, 0, 3); | ||
| 141 | + | ||
| 142 | + c.lua_pushcfunction(state, lua_border_set_width); | ||
| 143 | + c.lua_setfield(state, -2, "set_width"); | ||
| 144 | + | ||
| 145 | + c.lua_pushcfunction(state, lua_border_set_focused_color); | ||
| 146 | + c.lua_setfield(state, -2, "set_focused_color"); | ||
| 147 | + | ||
| 148 | + c.lua_pushcfunction(state, lua_border_set_unfocused_color); | ||
| 149 | + c.lua_setfield(state, -2, "set_unfocused_color"); | ||
| 150 | + | ||
| 151 | + c.lua_setfield(state, -2, "border"); | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +fn register_client_module(state: *c.lua_State) void { | ||
| 155 | + c.lua_createtable(state, 0, 5); | ||
| 156 | + | ||
| 157 | + c.lua_pushcfunction(state, lua_client_kill); | ||
| 158 | + c.lua_setfield(state, -2, "kill"); | ||
| 159 | + | ||
| 160 | + c.lua_pushcfunction(state, lua_client_toggle_fullscreen); | ||
| 161 | + c.lua_setfield(state, -2, "toggle_fullscreen"); | ||
| 162 | + | ||
| 163 | + c.lua_pushcfunction(state, lua_client_toggle_floating); | ||
| 164 | + c.lua_setfield(state, -2, "toggle_floating"); | ||
| 165 | + | ||
| 166 | + c.lua_pushcfunction(state, lua_client_focus_stack); | ||
| 167 | + c.lua_setfield(state, -2, "focus_stack"); | ||
| 168 | + | ||
| 169 | + c.lua_pushcfunction(state, lua_client_move_stack); | ||
| 170 | + c.lua_setfield(state, -2, "move_stack"); | ||
| 171 | + | ||
| 172 | + c.lua_setfield(state, -2, "client"); | ||
| 173 | +} | ||
| 174 | + | ||
| 175 | +fn register_layout_module(state: *c.lua_State) void { | ||
| 176 | + c.lua_createtable(state, 0, 4); | ||
| 177 | + | ||
| 178 | + c.lua_pushcfunction(state, lua_layout_cycle); | ||
| 179 | + c.lua_setfield(state, -2, "cycle"); | ||
| 180 | + | ||
| 181 | + c.lua_pushcfunction(state, lua_layout_set); | ||
| 182 | + c.lua_setfield(state, -2, "set"); | ||
| 183 | + | ||
| 184 | + c.lua_pushcfunction(state, lua_layout_scroll_left); | ||
| 185 | + c.lua_setfield(state, -2, "scroll_left"); | ||
| 186 | + | ||
| 187 | + c.lua_pushcfunction(state, lua_layout_scroll_right); | ||
| 188 | + c.lua_setfield(state, -2, "scroll_right"); | ||
| 189 | + | ||
| 190 | + c.lua_setfield(state, -2, "layout"); | ||
| 191 | +} | ||
| 192 | + | ||
| 193 | +fn register_tag_module(state: *c.lua_State) void { | ||
| 194 | + c.lua_createtable(state, 0, 10); | ||
| 195 | + | ||
| 196 | + c.lua_pushcfunction(state, lua_tag_view); | ||
| 197 | + c.lua_setfield(state, -2, "view"); | ||
| 198 | + | ||
| 199 | + c.lua_pushcfunction(state, lua_tag_view_next); | ||
| 200 | + c.lua_setfield(state, -2, "view_next"); | ||
| 201 | + | ||
| 202 | + c.lua_pushcfunction(state, lua_tag_view_previous); | ||
| 203 | + c.lua_setfield(state, -2, "view_previous"); | ||
| 204 | + | ||
| 205 | + c.lua_pushcfunction(state, lua_tag_view_next_nonempty); | ||
| 206 | + c.lua_setfield(state, -2, "view_next_nonempty"); | ||
| 207 | + | ||
| 208 | + c.lua_pushcfunction(state, lua_tag_view_previous_nonempty); | ||
| 209 | + c.lua_setfield(state, -2, "view_previous_nonempty"); | ||
| 210 | + | ||
| 211 | + c.lua_pushcfunction(state, lua_tag_toggleview); | ||
| 212 | + c.lua_setfield(state, -2, "toggleview"); | ||
| 213 | + | ||
| 214 | + c.lua_pushcfunction(state, lua_tag_move_to); | ||
| 215 | + c.lua_setfield(state, -2, "move_to"); | ||
| 216 | + | ||
| 217 | + c.lua_pushcfunction(state, lua_tag_toggletag); | ||
| 218 | + c.lua_setfield(state, -2, "toggletag"); | ||
| 219 | + | ||
| 220 | + c.lua_pushcfunction(state, lua_tag_set_back_and_forth); | ||
| 221 | + c.lua_setfield(state, -2, "set_back_and_forth"); | ||
| 222 | + | ||
| 223 | + c.lua_setfield(state, -2, "tag"); | ||
| 224 | +} | ||
| 225 | + | ||
| 226 | +fn register_monitor_module(state: *c.lua_State) void { | ||
| 227 | + c.lua_createtable(state, 0, 2); | ||
| 228 | + | ||
| 229 | + c.lua_pushcfunction(state, lua_monitor_focus); | ||
| 230 | + c.lua_setfield(state, -2, "focus"); | ||
| 231 | + | ||
| 232 | + c.lua_pushcfunction(state, lua_monitor_tag); | ||
| 233 | + c.lua_setfield(state, -2, "tag"); | ||
| 234 | + | ||
| 235 | + c.lua_setfield(state, -2, "monitor"); | ||
| 236 | +} | ||
| 237 | + | ||
| 238 | +fn register_rule_module(state: *c.lua_State) void { | ||
| 239 | + c.lua_createtable(state, 0, 1); | ||
| 240 | + | ||
| 241 | + c.lua_pushcfunction(state, lua_rule_add); | ||
| 242 | + c.lua_setfield(state, -2, "add"); | ||
| 243 | + | ||
| 244 | + c.lua_setfield(state, -2, "rule"); | ||
| 245 | +} | ||
| 246 | + | ||
| 247 | +fn register_bar_module(state: *c.lua_State) void { | ||
| 248 | + c.lua_createtable(state, 0, 10); | ||
| 249 | + | ||
| 250 | + c.lua_pushcfunction(state, lua_bar_set_font); | ||
| 251 | + c.lua_setfield(state, -2, "set_font"); | ||
| 252 | + | ||
| 253 | + c.lua_pushcfunction(state, lua_bar_set_blocks); | ||
| 254 | + c.lua_setfield(state, -2, "set_blocks"); | ||
| 255 | + | ||
| 256 | + c.lua_pushcfunction(state, lua_bar_set_scheme_normal); | ||
| 257 | + c.lua_setfield(state, -2, "set_scheme_normal"); | ||
| 258 | + | ||
| 259 | + c.lua_pushcfunction(state, lua_bar_set_scheme_selected); | ||
| 260 | + c.lua_setfield(state, -2, "set_scheme_selected"); | ||
| 261 | + | ||
| 262 | + c.lua_pushcfunction(state, lua_bar_set_scheme_occupied); | ||
| 263 | + c.lua_setfield(state, -2, "set_scheme_occupied"); | ||
| 264 | + | ||
| 265 | + c.lua_pushcfunction(state, lua_bar_set_scheme_urgent); | ||
| 266 | + c.lua_setfield(state, -2, "set_scheme_urgent"); | ||
| 267 | + | ||
| 268 | + c.lua_pushcfunction(state, lua_bar_set_hide_vacant_tags); | ||
| 269 | + c.lua_setfield(state, -2, "set_hide_vacant_tags"); | ||
| 270 | + | ||
| 271 | + c.lua_createtable(state, 0, 6); | ||
| 272 | + | ||
| 273 | + c.lua_pushcfunction(state, lua_bar_block_ram); | ||
| 274 | + c.lua_setfield(state, -2, "ram"); | ||
| 275 | + | ||
| 276 | + c.lua_pushcfunction(state, lua_bar_block_datetime); | ||
| 277 | + c.lua_setfield(state, -2, "datetime"); | ||
| 278 | + | ||
| 279 | + c.lua_pushcfunction(state, lua_bar_block_shell); | ||
| 280 | + c.lua_setfield(state, -2, "shell"); | ||
| 281 | + | ||
| 282 | + c.lua_pushcfunction(state, lua_bar_block_static); | ||
| 283 | + c.lua_setfield(state, -2, "static"); | ||
| 284 | + | ||
| 285 | + c.lua_pushcfunction(state, lua_bar_block_battery); | ||
| 286 | + c.lua_setfield(state, -2, "battery"); | ||
| 287 | + | ||
| 288 | + c.lua_setfield(state, -2, "block"); | ||
| 289 | + | ||
| 290 | + c.lua_setfield(state, -2, "bar"); | ||
| 291 | +} | ||
| 292 | + | ||
| 293 | +fn register_misc_functions(state: *c.lua_State) void { | ||
| 294 | + c.lua_pushcfunction(state, lua_set_terminal); | ||
| 295 | + c.lua_setfield(state, -2, "set_terminal"); | ||
| 296 | + | ||
| 297 | + c.lua_pushcfunction(state, lua_set_modkey); | ||
| 298 | + c.lua_setfield(state, -2, "set_modkey"); | ||
| 299 | + | ||
| 300 | + c.lua_pushcfunction(state, lua_set_tags); | ||
| 301 | + c.lua_setfield(state, -2, "set_tags"); | ||
| 302 | + | ||
| 303 | + c.lua_pushcfunction(state, lua_autostart); | ||
| 304 | + c.lua_setfield(state, -2, "autostart"); | ||
| 305 | + | ||
| 306 | + c.lua_pushcfunction(state, lua_auto_tile); | ||
| 307 | + c.lua_setfield(state, -2, "auto_tile"); | ||
| 308 | + | ||
| 309 | + c.lua_pushcfunction(state, lua_quit); | ||
| 310 | + c.lua_setfield(state, -2, "quit"); | ||
| 311 | + | ||
| 312 | + c.lua_pushcfunction(state, lua_restart); | ||
| 313 | + c.lua_setfield(state, -2, "restart"); | ||
| 314 | + | ||
| 315 | + c.lua_pushcfunction(state, lua_toggle_gaps); | ||
| 316 | + c.lua_setfield(state, -2, "toggle_gaps"); | ||
| 317 | + | ||
| 318 | + c.lua_pushcfunction(state, lua_set_master_factor); | ||
| 319 | + c.lua_setfield(state, -2, "set_master_factor"); | ||
| 320 | + | ||
| 321 | + c.lua_pushcfunction(state, lua_inc_num_master); | ||
| 322 | + c.lua_setfield(state, -2, "inc_num_master"); | ||
| 323 | +} | ||
| 324 | + | ||
| 325 | +fn create_action_table(state: *c.lua_State, action_name: [*:0]const u8) void { | ||
| 326 | + c.lua_createtable(state, 0, 2); | ||
| 327 | + _ = c.lua_pushstring(state, action_name); | ||
| 328 | + c.lua_setfield(state, -2, "__action"); | ||
| 329 | +} | ||
| 330 | + | ||
| 331 | +fn create_action_table_with_int(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void { | ||
| 332 | + c.lua_createtable(state, 0, 2); | ||
| 333 | + _ = c.lua_pushstring(state, action_name); | ||
| 334 | + c.lua_setfield(state, -2, "__action"); | ||
| 335 | + c.lua_pushinteger(state, arg); | ||
| 336 | + c.lua_setfield(state, -2, "__arg"); | ||
| 337 | +} | ||
| 338 | + | ||
| 339 | +fn create_action_table_with_string(state: *c.lua_State, action_name: [*:0]const u8) void { | ||
| 340 | + c.lua_createtable(state, 0, 2); | ||
| 341 | + _ = c.lua_pushstring(state, action_name); | ||
| 342 | + c.lua_setfield(state, -2, "__action"); | ||
| 343 | + c.lua_pushvalue(state, 1); | ||
| 344 | + c.lua_setfield(state, -2, "__arg"); | ||
| 345 | +} | ||
| 346 | + | ||
| 347 | +fn lua_spawn(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 348 | + const s = state orelse return 0; | ||
| 349 | + create_action_table_with_string(s, "Spawn"); | ||
| 350 | + return 1; | ||
| 351 | +} | ||
| 352 | + | ||
| 353 | +fn lua_spawn_terminal(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 354 | + const s = state orelse return 0; | ||
| 355 | + create_action_table(s, "SpawnTerminal"); | ||
| 356 | + return 1; | ||
| 357 | +} | ||
| 358 | + | ||
| 359 | +fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 360 | + const s = state orelse return 0; | ||
| 361 | + const cfg = config orelse return 0; | ||
| 362 | + | ||
| 363 | + const mod_mask = parse_modifiers(s, 1); | ||
| 364 | + const key_str = get_string_arg(s, 2) orelse return 0; | ||
| 365 | + const keysym = key_name_to_keysym(key_str) orelse return 0; | ||
| 366 | + | ||
| 367 | + if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0; | ||
| 368 | + | ||
| 369 | + _ = c.lua_getfield(s, 3, "__action"); | ||
| 370 | + const action_str = get_lua_string(s, -1) orelse { | ||
| 371 | + c.lua_settop(s, -2); | ||
| 372 | + return 0; | ||
| 373 | + }; | ||
| 374 | + c.lua_settop(s, -2); | ||
| 375 | + | ||
| 376 | + const action = parse_action(action_str) orelse return 0; | ||
| 377 | + | ||
| 378 | + var int_arg: i32 = 0; | ||
| 379 | + var str_arg: ?[]const u8 = null; | ||
| 380 | + | ||
| 381 | + _ = c.lua_getfield(s, 3, "__arg"); | ||
| 382 | + if (c.lua_type(s, -1) == c.LUA_TNUMBER) { | ||
| 383 | + int_arg = @intCast(c.lua_tointegerx(s, -1, null)); | ||
| 384 | + } else if (c.lua_type(s, -1) == c.LUA_TSTRING) { | ||
| 385 | + str_arg = get_lua_string(s, -1); | ||
| 386 | + } | ||
| 387 | + c.lua_settop(s, -2); | ||
| 388 | + | ||
| 389 | + cfg.add_keybind(.{ | ||
| 390 | + .mod_mask = mod_mask, | ||
| 391 | + .keysym = keysym, | ||
| 392 | + .action = action, | ||
| 393 | + .int_arg = int_arg, | ||
| 394 | + .str_arg = str_arg, | ||
| 395 | + }) catch return 0; | ||
| 396 | + | ||
| 397 | + return 0; | ||
| 398 | +} | ||
| 399 | + | ||
| 400 | +fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 401 | + _ = state; | ||
| 402 | + return 0; | ||
| 403 | +} | ||
| 404 | + | ||
| 405 | +fn lua_gaps_set_enabled(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 406 | + const cfg = config orelse return 0; | ||
| 407 | + const s = state orelse return 0; | ||
| 408 | + cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0; | ||
| 409 | + return 0; | ||
| 410 | +} | ||
| 411 | + | ||
| 412 | +fn lua_gaps_enable(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 413 | + _ = state; | ||
| 414 | + const cfg = config orelse return 0; | ||
| 415 | + cfg.gaps_enabled = true; | ||
| 416 | + return 0; | ||
| 417 | +} | ||
| 418 | + | ||
| 419 | +fn lua_gaps_disable(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 420 | + _ = state; | ||
| 421 | + const cfg = config orelse return 0; | ||
| 422 | + cfg.gaps_enabled = false; | ||
| 423 | + return 0; | ||
| 424 | +} | ||
| 425 | + | ||
| 426 | +fn lua_gaps_set_inner(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 427 | + const cfg = config orelse return 0; | ||
| 428 | + const s = state orelse return 0; | ||
| 429 | + cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 430 | + cfg.gap_inner_v = @intCast(c.lua_tointegerx(s, 2, null)); | ||
| 431 | + return 0; | ||
| 432 | +} | ||
| 433 | + | ||
| 434 | +fn lua_gaps_set_outer(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 435 | + const cfg = config orelse return 0; | ||
| 436 | + const s = state orelse return 0; | ||
| 437 | + cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 438 | + cfg.gap_outer_v = @intCast(c.lua_tointegerx(s, 2, null)); | ||
| 439 | + return 0; | ||
| 440 | +} | ||
| 441 | + | ||
| 442 | +fn lua_gaps_set_smart(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 443 | + const cfg = config orelse return 0; | ||
| 444 | + const s = state orelse return 0; | ||
| 445 | + cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0; | ||
| 446 | + return 0; | ||
| 447 | +} | ||
| 448 | + | ||
| 449 | +fn lua_border_set_width(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 450 | + const cfg = config orelse return 0; | ||
| 451 | + const s = state orelse return 0; | ||
| 452 | + cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 453 | + return 0; | ||
| 454 | +} | ||
| 455 | + | ||
| 456 | +fn lua_border_set_focused_color(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 457 | + const cfg = config orelse return 0; | ||
| 458 | + const s = state orelse return 0; | ||
| 459 | + cfg.border_focused = parse_color(s, 1); | ||
| 460 | + return 0; | ||
| 461 | +} | ||
| 462 | + | ||
| 463 | +fn lua_border_set_unfocused_color(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 464 | + const cfg = config orelse return 0; | ||
| 465 | + const s = state orelse return 0; | ||
| 466 | + cfg.border_unfocused = parse_color(s, 1); | ||
| 467 | + return 0; | ||
| 468 | +} | ||
| 469 | + | ||
| 470 | +fn lua_client_kill(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 471 | + const s = state orelse return 0; | ||
| 472 | + create_action_table(s, "KillClient"); | ||
| 473 | + return 1; | ||
| 474 | +} | ||
| 475 | + | ||
| 476 | +fn lua_client_toggle_fullscreen(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 477 | + const s = state orelse return 0; | ||
| 478 | + create_action_table(s, "ToggleFullScreen"); | ||
| 479 | + return 1; | ||
| 480 | +} | ||
| 481 | + | ||
| 482 | +fn lua_client_toggle_floating(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 483 | + const s = state orelse return 0; | ||
| 484 | + create_action_table(s, "ToggleFloating"); | ||
| 485 | + return 1; | ||
| 486 | +} | ||
| 487 | + | ||
| 488 | +fn lua_client_focus_stack(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 489 | + const s = state orelse return 0; | ||
| 490 | + const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 491 | + create_action_table_with_int(s, "FocusStack", dir); | ||
| 492 | + return 1; | ||
| 493 | +} | ||
| 494 | + | ||
| 495 | +fn lua_client_move_stack(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 496 | + const s = state orelse return 0; | ||
| 497 | + const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 498 | + create_action_table_with_int(s, "MoveStack", dir); | ||
| 499 | + return 1; | ||
| 500 | +} | ||
| 501 | + | ||
| 502 | +fn lua_layout_cycle(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 503 | + const s = state orelse return 0; | ||
| 504 | + create_action_table(s, "CycleLayout"); | ||
| 505 | + return 1; | ||
| 506 | +} | ||
| 507 | + | ||
| 508 | +fn lua_layout_set(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 509 | + const s = state orelse return 0; | ||
| 510 | + create_action_table_with_string(s, "ChangeLayout"); | ||
| 511 | + return 1; | ||
| 512 | +} | ||
| 513 | + | ||
| 514 | +fn lua_layout_scroll_left(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 515 | + const s = state orelse return 0; | ||
| 516 | + create_action_table(s, "ScrollLeft"); | ||
| 517 | + return 1; | ||
| 518 | +} | ||
| 519 | + | ||
| 520 | +fn lua_layout_scroll_right(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 521 | + const s = state orelse return 0; | ||
| 522 | + create_action_table(s, "ScrollRight"); | ||
| 523 | + return 1; | ||
| 524 | +} | ||
| 525 | + | ||
| 526 | +fn lua_tag_view(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 527 | + const s = state orelse return 0; | ||
| 528 | + const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 529 | + create_action_table_with_int(s, "ViewTag", idx - 1); | ||
| 530 | + return 1; | ||
| 531 | +} | ||
| 532 | + | ||
| 533 | +fn lua_tag_view_next(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 534 | + const s = state orelse return 0; | ||
| 535 | + create_action_table(s, "ViewNextTag"); | ||
| 536 | + return 1; | ||
| 537 | +} | ||
| 538 | + | ||
| 539 | +fn lua_tag_view_previous(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 540 | + const s = state orelse return 0; | ||
| 541 | + create_action_table(s, "ViewPreviousTag"); | ||
| 542 | + return 1; | ||
| 543 | +} | ||
| 544 | + | ||
| 545 | +fn lua_tag_view_next_nonempty(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 546 | + const s = state orelse return 0; | ||
| 547 | + create_action_table(s, "ViewNextNonEmptyTag"); | ||
| 548 | + return 1; | ||
| 549 | +} | ||
| 550 | + | ||
| 551 | +fn lua_tag_view_previous_nonempty(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 552 | + const s = state orelse return 0; | ||
| 553 | + create_action_table(s, "ViewPreviousNonEmptyTag"); | ||
| 554 | + return 1; | ||
| 555 | +} | ||
| 556 | + | ||
| 557 | +fn lua_tag_toggleview(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 558 | + const s = state orelse return 0; | ||
| 559 | + const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 560 | + create_action_table_with_int(s, "ToggleView", idx - 1); | ||
| 561 | + return 1; | ||
| 562 | +} | ||
| 563 | + | ||
| 564 | +fn lua_tag_move_to(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 565 | + const s = state orelse return 0; | ||
| 566 | + const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 567 | + create_action_table_with_int(s, "MoveToTag", idx - 1); | ||
| 568 | + return 1; | ||
| 569 | +} | ||
| 570 | + | ||
| 571 | +fn lua_tag_toggletag(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 572 | + const s = state orelse return 0; | ||
| 573 | + const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 574 | + create_action_table_with_int(s, "ToggleTag", idx - 1); | ||
| 575 | + return 1; | ||
| 576 | +} | ||
| 577 | + | ||
| 578 | +fn lua_tag_set_back_and_forth(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 579 | + const cfg = config orelse return 0; | ||
| 580 | + const s = state orelse return 0; | ||
| 581 | + cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0; | ||
| 582 | + return 0; | ||
| 583 | +} | ||
| 584 | + | ||
| 585 | +fn lua_monitor_focus(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 586 | + const s = state orelse return 0; | ||
| 587 | + const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 588 | + create_action_table_with_int(s, "FocusMonitor", dir); | ||
| 589 | + return 1; | ||
| 590 | +} | ||
| 591 | + | ||
| 592 | +fn lua_monitor_tag(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 593 | + const s = state orelse return 0; | ||
| 594 | + const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 595 | + create_action_table_with_int(s, "TagMonitor", dir); | ||
| 596 | + return 1; | ||
| 597 | +} | ||
| 598 | + | ||
| 599 | +fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 600 | + const cfg = config orelse return 0; | ||
| 601 | + const s = state orelse return 0; | ||
| 602 | + | ||
| 603 | + if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; | ||
| 604 | + | ||
| 605 | + var rule = Rule{ | ||
| 606 | + .class = null, | ||
| 607 | + .instance = null, | ||
| 608 | + .title = null, | ||
| 609 | + .tags = 0, | ||
| 610 | + .is_floating = false, | ||
| 611 | + .monitor = -1, | ||
| 612 | + }; | ||
| 613 | + | ||
| 614 | + _ = c.lua_getfield(s, 1, "class"); | ||
| 615 | + if (c.lua_type(s, -1) == c.LUA_TSTRING) { | ||
| 616 | + rule.class = get_lua_string(s, -1); | ||
| 617 | + } | ||
| 618 | + c.lua_settop(s, -2); | ||
| 619 | + | ||
| 620 | + _ = c.lua_getfield(s, 1, "instance"); | ||
| 621 | + if (c.lua_type(s, -1) == c.LUA_TSTRING) { | ||
| 622 | + rule.instance = get_lua_string(s, -1); | ||
| 623 | + } | ||
| 624 | + c.lua_settop(s, -2); | ||
| 625 | + | ||
| 626 | + _ = c.lua_getfield(s, 1, "title"); | ||
| 627 | + if (c.lua_type(s, -1) == c.LUA_TSTRING) { | ||
| 628 | + rule.title = get_lua_string(s, -1); | ||
| 629 | + } | ||
| 630 | + c.lua_settop(s, -2); | ||
| 631 | + | ||
| 632 | + _ = c.lua_getfield(s, 1, "tag"); | ||
| 633 | + if (c.lua_type(s, -1) == c.LUA_TNUMBER) { | ||
| 634 | + const tag_idx: i32 = @intCast(c.lua_tointegerx(s, -1, null)); | ||
| 635 | + if (tag_idx > 0) { | ||
| 636 | + rule.tags = @as(u32, 1) << @intCast(tag_idx - 1); | ||
| 637 | + } | ||
| 638 | + } | ||
| 639 | + c.lua_settop(s, -2); | ||
| 640 | + | ||
| 641 | + _ = c.lua_getfield(s, 1, "floating"); | ||
| 642 | + if (c.lua_type(s, -1) == c.LUA_TBOOLEAN) { | ||
| 643 | + rule.is_floating = c.lua_toboolean(s, -1) != 0; | ||
| 644 | + } | ||
| 645 | + c.lua_settop(s, -2); | ||
| 646 | + | ||
| 647 | + _ = c.lua_getfield(s, 1, "monitor"); | ||
| 648 | + if (c.lua_type(s, -1) == c.LUA_TNUMBER) { | ||
| 649 | + rule.monitor = @intCast(c.lua_tointegerx(s, -1, null)); | ||
| 650 | + } | ||
| 651 | + c.lua_settop(s, -2); | ||
| 652 | + | ||
| 653 | + cfg.add_rule(rule) catch return 0; | ||
| 654 | + return 0; | ||
| 655 | +} | ||
| 656 | + | ||
| 657 | +fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 658 | + const cfg = config orelse return 0; | ||
| 659 | + const s = state orelse return 0; | ||
| 660 | + if (get_string_arg(s, 1)) |font| { | ||
| 661 | + cfg.font = font; | ||
| 662 | + } | ||
| 663 | + return 0; | ||
| 664 | +} | ||
| 665 | + | ||
| 666 | +fn lua_bar_set_blocks(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 667 | + const cfg = config orelse return 0; | ||
| 668 | + const s = state orelse return 0; | ||
| 669 | + | ||
| 670 | + if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; | ||
| 671 | + | ||
| 672 | + const len = c.lua_rawlen(s, 1); | ||
| 673 | + var i: usize = 1; | ||
| 674 | + while (i <= len) : (i += 1) { | ||
| 675 | + _ = c.lua_rawgeti(s, 1, @intCast(i)); | ||
| 676 | + | ||
| 677 | + if (c.lua_type(s, -1) != c.LUA_TTABLE) { | ||
| 678 | + c.lua_settop(s, -2); | ||
| 679 | + continue; | ||
| 680 | + } | ||
| 681 | + | ||
| 682 | + if (parse_block_config(s, -1)) |block| { | ||
| 683 | + cfg.add_block(block) catch {}; | ||
| 684 | + } | ||
| 685 | + | ||
| 686 | + c.lua_settop(s, -2); | ||
| 687 | + } | ||
| 688 | + | ||
| 689 | + return 0; | ||
| 690 | +} | ||
| 691 | + | ||
| 692 | +fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block { | ||
| 693 | + _ = c.lua_getfield(state, idx, "__block_type"); | ||
| 694 | + const block_type_str = get_lua_string(state, -1) orelse { | ||
| 695 | + c.lua_settop(state, -2); | ||
| 696 | + return null; | ||
| 697 | + }; | ||
| 698 | + c.lua_settop(state, -2); | ||
| 699 | + | ||
| 700 | + _ = c.lua_getfield(state, idx, "format"); | ||
| 701 | + const format = get_lua_string(state, -1) orelse ""; | ||
| 702 | + c.lua_settop(state, -2); | ||
| 703 | + | ||
| 704 | + _ = c.lua_getfield(state, idx, "interval"); | ||
| 705 | + const interval: u32 = @intCast(c.lua_tointegerx(state, -1, null)); | ||
| 706 | + c.lua_settop(state, -2); | ||
| 707 | + | ||
| 708 | + _ = c.lua_getfield(state, idx, "color"); | ||
| 709 | + const color = parse_color(state, -1); | ||
| 710 | + c.lua_settop(state, -2); | ||
| 711 | + | ||
| 712 | + _ = c.lua_getfield(state, idx, "underline"); | ||
| 713 | + const underline = c.lua_toboolean(state, -1) != 0; | ||
| 714 | + c.lua_settop(state, -2); | ||
| 715 | + | ||
| 716 | + var block = Block{ | ||
| 717 | + .block_type = .static, | ||
| 718 | + .format = format, | ||
| 719 | + .interval = interval, | ||
| 720 | + .color = color, | ||
| 721 | + .underline = underline, | ||
| 722 | + }; | ||
| 723 | + | ||
| 724 | + if (std.mem.eql(u8, block_type_str, "Ram")) { | ||
| 725 | + block.block_type = .ram; | ||
| 726 | + } else if (std.mem.eql(u8, block_type_str, "DateTime")) { | ||
| 727 | + block.block_type = .datetime; | ||
| 728 | + _ = c.lua_getfield(state, idx, "__arg"); | ||
| 729 | + block.datetime_format = get_lua_string(state, -1); | ||
| 730 | + c.lua_settop(state, -2); | ||
| 731 | + } else if (std.mem.eql(u8, block_type_str, "Shell")) { | ||
| 732 | + block.block_type = .shell; | ||
| 733 | + _ = c.lua_getfield(state, idx, "__arg"); | ||
| 734 | + block.command = get_lua_string(state, -1); | ||
| 735 | + c.lua_settop(state, -2); | ||
| 736 | + } else if (std.mem.eql(u8, block_type_str, "Static")) { | ||
| 737 | + block.block_type = .static; | ||
| 738 | + _ = c.lua_getfield(state, idx, "__arg"); | ||
| 739 | + if (get_lua_string(state, -1)) |text| { | ||
| 740 | + block.format = text; | ||
| 741 | + } | ||
| 742 | + c.lua_settop(state, -2); | ||
| 743 | + } else if (std.mem.eql(u8, block_type_str, "Battery")) { | ||
| 744 | + block.block_type = .battery; | ||
| 745 | + _ = c.lua_getfield(state, idx, "__arg"); | ||
| 746 | + if (c.lua_type(state, -1) == c.LUA_TTABLE) { | ||
| 747 | + _ = c.lua_getfield(state, -1, "charging"); | ||
| 748 | + block.format_charging = get_lua_string(state, -1); | ||
| 749 | + c.lua_settop(state, -2); | ||
| 750 | + | ||
| 751 | + _ = c.lua_getfield(state, -1, "discharging"); | ||
| 752 | + block.format_discharging = get_lua_string(state, -1); | ||
| 753 | + c.lua_settop(state, -2); | ||
| 754 | + | ||
| 755 | + _ = c.lua_getfield(state, -1, "full"); | ||
| 756 | + block.format_full = get_lua_string(state, -1); | ||
| 757 | + c.lua_settop(state, -2); | ||
| 758 | + | ||
| 759 | + _ = c.lua_getfield(state, -1, "battery_name"); | ||
| 760 | + block.battery_name = get_lua_string(state, -1); | ||
| 761 | + c.lua_settop(state, -2); | ||
| 762 | + } | ||
| 763 | + c.lua_settop(state, -2); | ||
| 764 | + } else { | ||
| 765 | + return null; | ||
| 766 | + } | ||
| 767 | + | ||
| 768 | + return block; | ||
| 769 | +} | ||
| 770 | + | ||
| 771 | +fn lua_bar_set_scheme_normal(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 772 | + const cfg = config orelse return 0; | ||
| 773 | + const s = state orelse return 0; | ||
| 774 | + cfg.scheme_normal = parse_scheme(s); | ||
| 775 | + return 0; | ||
| 776 | +} | ||
| 777 | + | ||
| 778 | +fn lua_bar_set_scheme_selected(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 779 | + const cfg = config orelse return 0; | ||
| 780 | + const s = state orelse return 0; | ||
| 781 | + cfg.scheme_selected = parse_scheme(s); | ||
| 782 | + return 0; | ||
| 783 | +} | ||
| 784 | + | ||
| 785 | +fn lua_bar_set_scheme_occupied(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 786 | + const cfg = config orelse return 0; | ||
| 787 | + const s = state orelse return 0; | ||
| 788 | + cfg.scheme_occupied = parse_scheme(s); | ||
| 789 | + return 0; | ||
| 790 | +} | ||
| 791 | + | ||
| 792 | +fn lua_bar_set_scheme_urgent(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 793 | + const cfg = config orelse return 0; | ||
| 794 | + const s = state orelse return 0; | ||
| 795 | + cfg.scheme_urgent = parse_scheme(s); | ||
| 796 | + return 0; | ||
| 797 | +} | ||
| 798 | + | ||
| 799 | +fn lua_bar_set_hide_vacant_tags(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 800 | + const cfg = config orelse return 0; | ||
| 801 | + const s = state orelse return 0; | ||
| 802 | + cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0; | ||
| 803 | + return 0; | ||
| 804 | +} | ||
| 805 | + | ||
| 806 | +fn lua_bar_block_ram(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 807 | + const s = state orelse return 0; | ||
| 808 | + create_block_table(s, "Ram", null); | ||
| 809 | + return 1; | ||
| 810 | +} | ||
| 811 | + | ||
| 812 | +fn lua_bar_block_datetime(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 813 | + const s = state orelse return 0; | ||
| 814 | + _ = c.lua_getfield(s, 1, "date_format"); | ||
| 815 | + const date_format = get_lua_string(s, -1); | ||
| 816 | + c.lua_settop(s, -2); | ||
| 817 | + create_block_table(s, "DateTime", date_format); | ||
| 818 | + return 1; | ||
| 819 | +} | ||
| 820 | + | ||
| 821 | +fn lua_bar_block_shell(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 822 | + const s = state orelse return 0; | ||
| 823 | + _ = c.lua_getfield(s, 1, "command"); | ||
| 824 | + const command = get_lua_string(s, -1); | ||
| 825 | + c.lua_settop(s, -2); | ||
| 826 | + create_block_table(s, "Shell", command); | ||
| 827 | + return 1; | ||
| 828 | +} | ||
| 829 | + | ||
| 830 | +fn lua_bar_block_static(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 831 | + const s = state orelse return 0; | ||
| 832 | + _ = c.lua_getfield(s, 1, "text"); | ||
| 833 | + const text = get_lua_string(s, -1); | ||
| 834 | + c.lua_settop(s, -2); | ||
| 835 | + create_block_table(s, "Static", text); | ||
| 836 | + return 1; | ||
| 837 | +} | ||
| 838 | + | ||
| 839 | +fn lua_bar_block_battery(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 840 | + const s = state orelse return 0; | ||
| 841 | + | ||
| 842 | + c.lua_createtable(s, 0, 6); | ||
| 843 | + | ||
| 844 | + _ = c.lua_pushstring(s, "Battery"); | ||
| 845 | + c.lua_setfield(s, -2, "__block_type"); | ||
| 846 | + | ||
| 847 | + _ = c.lua_getfield(s, 1, "format"); | ||
| 848 | + c.lua_setfield(s, -2, "format"); | ||
| 849 | + | ||
| 850 | + _ = c.lua_getfield(s, 1, "interval"); | ||
| 851 | + c.lua_setfield(s, -2, "interval"); | ||
| 852 | + | ||
| 853 | + _ = c.lua_getfield(s, 1, "color"); | ||
| 854 | + c.lua_setfield(s, -2, "color"); | ||
| 855 | + | ||
| 856 | + _ = c.lua_getfield(s, 1, "underline"); | ||
| 857 | + c.lua_setfield(s, -2, "underline"); | ||
| 858 | + | ||
| 859 | + c.lua_createtable(s, 0, 4); | ||
| 860 | + _ = c.lua_getfield(s, 1, "charging"); | ||
| 861 | + c.lua_setfield(s, -2, "charging"); | ||
| 862 | + _ = c.lua_getfield(s, 1, "discharging"); | ||
| 863 | + c.lua_setfield(s, -2, "discharging"); | ||
| 864 | + _ = c.lua_getfield(s, 1, "full"); | ||
| 865 | + c.lua_setfield(s, -2, "full"); | ||
| 866 | + _ = c.lua_getfield(s, 1, "battery_name"); | ||
| 867 | + c.lua_setfield(s, -2, "battery_name"); | ||
| 868 | + c.lua_setfield(s, -2, "__arg"); | ||
| 869 | + | ||
| 870 | + return 1; | ||
| 871 | +} | ||
| 872 | + | ||
| 873 | +fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void { | ||
| 874 | + c.lua_createtable(state, 0, 6); | ||
| 875 | + | ||
| 876 | + _ = c.lua_pushstring(state, block_type); | ||
| 877 | + c.lua_setfield(state, -2, "__block_type"); | ||
| 878 | + | ||
| 879 | + _ = c.lua_getfield(state, 1, "format"); | ||
| 880 | + c.lua_setfield(state, -2, "format"); | ||
| 881 | + | ||
| 882 | + _ = c.lua_getfield(state, 1, "interval"); | ||
| 883 | + c.lua_setfield(state, -2, "interval"); | ||
| 884 | + | ||
| 885 | + _ = c.lua_getfield(state, 1, "color"); | ||
| 886 | + c.lua_setfield(state, -2, "color"); | ||
| 887 | + | ||
| 888 | + _ = c.lua_getfield(state, 1, "underline"); | ||
| 889 | + c.lua_setfield(state, -2, "underline"); | ||
| 890 | + | ||
| 891 | + if (arg) |a| { | ||
| 892 | + var buf: [256]u8 = undefined; | ||
| 893 | + if (a.len < buf.len) { | ||
| 894 | + @memcpy(buf[0..a.len], a); | ||
| 895 | + buf[a.len] = 0; | ||
| 896 | + _ = c.lua_pushstring(state, &buf); | ||
| 897 | + c.lua_setfield(state, -2, "__arg"); | ||
| 898 | + } | ||
| 899 | + } | ||
| 900 | +} | ||
| 901 | + | ||
| 902 | +fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 903 | + const cfg = config orelse return 0; | ||
| 904 | + const s = state orelse return 0; | ||
| 905 | + if (get_string_arg(s, 1)) |term| { | ||
| 906 | + cfg.terminal = term; | ||
| 907 | + } | ||
| 908 | + return 0; | ||
| 909 | +} | ||
| 910 | + | ||
| 911 | +fn lua_set_modkey(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 912 | + const cfg = config orelse return 0; | ||
| 913 | + const s = state orelse return 0; | ||
| 914 | + if (get_string_arg(s, 1)) |modkey_str| { | ||
| 915 | + cfg.modkey = parse_single_modifier(modkey_str); | ||
| 916 | + } | ||
| 917 | + return 0; | ||
| 918 | +} | ||
| 919 | + | ||
| 920 | +fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 921 | + const cfg = config orelse return 0; | ||
| 922 | + const s = state orelse return 0; | ||
| 923 | + | ||
| 924 | + if (c.lua_type(s, 1) != c.LUA_TTABLE) return 0; | ||
| 925 | + | ||
| 926 | + const len = c.lua_rawlen(s, 1); | ||
| 927 | + var i: usize = 0; | ||
| 928 | + while (i < len and i < 9) : (i += 1) { | ||
| 929 | + _ = c.lua_rawgeti(s, 1, @intCast(i + 1)); | ||
| 930 | + if (get_lua_string(s, -1)) |tag_str| { | ||
| 931 | + cfg.tags[i] = tag_str; | ||
| 932 | + } | ||
| 933 | + c.lua_settop(s, -2); | ||
| 934 | + } | ||
| 935 | + | ||
| 936 | + return 0; | ||
| 937 | +} | ||
| 938 | + | ||
| 939 | +fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 940 | + const cfg = config orelse return 0; | ||
| 941 | + const s = state orelse return 0; | ||
| 942 | + if (get_string_arg(s, 1)) |cmd| { | ||
| 943 | + cfg.add_autostart(cmd) catch return 0; | ||
| 944 | + } | ||
| 945 | + return 0; | ||
| 946 | +} | ||
| 947 | + | ||
| 948 | +fn lua_auto_tile(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 949 | + const cfg = config orelse return 0; | ||
| 950 | + const s = state orelse return 0; | ||
| 951 | + cfg.auto_tile = c.lua_toboolean(s, 1) != 0; | ||
| 952 | + return 0; | ||
| 953 | +} | ||
| 954 | + | ||
| 955 | +fn lua_quit(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 956 | + const s = state orelse return 0; | ||
| 957 | + create_action_table(s, "Quit"); | ||
| 958 | + return 1; | ||
| 959 | +} | ||
| 960 | + | ||
| 961 | +fn lua_restart(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 962 | + const s = state orelse return 0; | ||
| 963 | + create_action_table(s, "Restart"); | ||
| 964 | + return 1; | ||
| 965 | +} | ||
| 966 | + | ||
| 967 | +fn lua_toggle_gaps(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 968 | + const s = state orelse return 0; | ||
| 969 | + create_action_table(s, "ToggleGaps"); | ||
| 970 | + return 1; | ||
| 971 | +} | ||
| 972 | + | ||
| 973 | +fn lua_set_master_factor(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 974 | + const s = state orelse return 0; | ||
| 975 | + const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 976 | + create_action_table_with_int(s, "ResizeMaster", delta); | ||
| 977 | + return 1; | ||
| 978 | +} | ||
| 979 | + | ||
| 980 | +fn lua_inc_num_master(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 981 | + const s = state orelse return 0; | ||
| 982 | + const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null)); | ||
| 983 | + if (delta > 0) { | ||
| 984 | + create_action_table(s, "IncMaster"); | ||
| 985 | + } else { | ||
| 986 | + create_action_table(s, "DecMaster"); | ||
| 987 | + } | ||
| 988 | + return 1; | ||
| 989 | +} | ||
| 990 | + | ||
| 991 | +fn get_string_arg(state: *c.lua_State, idx: c_int) ?[]const u8 { | ||
| 992 | + if (c.lua_type(state, idx) != c.LUA_TSTRING) return null; | ||
| 993 | + return get_lua_string(state, idx); | ||
| 994 | +} | ||
| 995 | + | ||
| 996 | +fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 { | ||
| 997 | + const cstr = c.lua_tolstring(state, idx, null); | ||
| 998 | + if (cstr == null) return null; | ||
| 999 | + return std.mem.span(cstr); | ||
| 1000 | +} | ||
| 1001 | + | ||
| 1002 | +fn parse_color(state: *c.lua_State, idx: c_int) u32 { | ||
| 1003 | + const lua_type = c.lua_type(state, idx); | ||
| 1004 | + if (lua_type == c.LUA_TNUMBER) { | ||
| 1005 | + return @intCast(c.lua_tointegerx(state, idx, null)); | ||
| 1006 | + } | ||
| 1007 | + if (lua_type == c.LUA_TSTRING) { | ||
| 1008 | + const str = get_lua_string(state, idx) orelse return 0; | ||
| 1009 | + if (str.len > 0 and str[0] == '#') { | ||
| 1010 | + return std.fmt.parseInt(u32, str[1..], 16) catch return 0; | ||
| 1011 | + } | ||
| 1012 | + if (str.len > 2 and str[0] == '0' and str[1] == 'x') { | ||
| 1013 | + return std.fmt.parseInt(u32, str[2..], 16) catch return 0; | ||
| 1014 | + } | ||
| 1015 | + return std.fmt.parseInt(u32, str, 16) catch return 0; | ||
| 1016 | + } | ||
| 1017 | + return 0; | ||
| 1018 | +} | ||
| 1019 | + | ||
| 1020 | +fn parse_scheme(state: *c.lua_State) Color_Scheme { | ||
| 1021 | + return Color_Scheme{ | ||
| 1022 | + .fg = parse_color(state, 1), | ||
| 1023 | + .bg = parse_color(state, 2), | ||
| 1024 | + .border = parse_color(state, 3), | ||
| 1025 | + }; | ||
| 1026 | +} | ||
| 1027 | + | ||
| 1028 | +fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 { | ||
| 1029 | + var mod_mask: u32 = 0; | ||
| 1030 | + | ||
| 1031 | + if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask; | ||
| 1032 | + | ||
| 1033 | + const len = c.lua_rawlen(state, idx); | ||
| 1034 | + var i: usize = 1; | ||
| 1035 | + while (i <= len) : (i += 1) { | ||
| 1036 | + _ = c.lua_rawgeti(state, idx, @intCast(i)); | ||
| 1037 | + if (get_lua_string(state, -1)) |mod_str| { | ||
| 1038 | + mod_mask |= parse_single_modifier(mod_str); | ||
| 1039 | + } | ||
| 1040 | + c.lua_settop(state, -2); | ||
| 1041 | + } | ||
| 1042 | + | ||
| 1043 | + return mod_mask; | ||
| 1044 | +} | ||
| 1045 | + | ||
| 1046 | +fn parse_single_modifier(name: []const u8) u32 { | ||
| 1047 | + if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) { | ||
| 1048 | + return (1 << 6); | ||
| 1049 | + } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) { | ||
| 1050 | + return (1 << 3); | ||
| 1051 | + } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) { | ||
| 1052 | + return (1 << 0); | ||
| 1053 | + } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) { | ||
| 1054 | + return (1 << 2); | ||
| 1055 | + } | ||
| 1056 | + return 0; | ||
| 1057 | +} | ||
| 1058 | + | ||
| 1059 | +fn parse_action(name: []const u8) ?Action { | ||
| 1060 | + const action_map = .{ | ||
| 1061 | + .{ "Spawn", Action.spawn }, | ||
| 1062 | + .{ "SpawnTerminal", Action.spawn_terminal }, | ||
| 1063 | + .{ "KillClient", Action.kill_client }, | ||
| 1064 | + .{ "Quit", Action.quit }, | ||
| 1065 | + .{ "Restart", Action.restart }, | ||
| 1066 | + .{ "FocusStack", Action.focus_next }, | ||
| 1067 | + .{ "MoveStack", Action.move_next }, | ||
| 1068 | + .{ "ResizeMaster", Action.resize_master }, | ||
| 1069 | + .{ "IncMaster", Action.inc_master }, | ||
| 1070 | + .{ "DecMaster", Action.dec_master }, | ||
| 1071 | + .{ "ToggleFloating", Action.toggle_floating }, | ||
| 1072 | + .{ "ToggleFullScreen", Action.toggle_fullscreen }, | ||
| 1073 | + .{ "ToggleGaps", Action.toggle_gaps }, | ||
| 1074 | + .{ "CycleLayout", Action.cycle_layout }, | ||
| 1075 | + .{ "ChangeLayout", Action.set_layout }, | ||
| 1076 | + .{ "ViewTag", Action.view_tag }, | ||
| 1077 | + .{ "ViewNextTag", Action.view_next_tag }, | ||
| 1078 | + .{ "ViewPreviousTag", Action.view_prev_tag }, | ||
| 1079 | + .{ "ViewNextNonEmptyTag", Action.view_next_nonempty_tag }, | ||
| 1080 | + .{ "ViewPreviousNonEmptyTag", Action.view_prev_nonempty_tag }, | ||
| 1081 | + .{ "MoveToTag", Action.move_to_tag }, | ||
| 1082 | + .{ "ToggleView", Action.toggle_view_tag }, | ||
| 1083 | + .{ "ToggleTag", Action.toggle_tag }, | ||
| 1084 | + .{ "FocusMonitor", Action.focus_monitor }, | ||
| 1085 | + .{ "TagMonitor", Action.send_to_monitor }, | ||
| 1086 | + .{ "ScrollLeft", Action.scroll_left }, | ||
| 1087 | + .{ "ScrollRight", Action.scroll_right }, | ||
| 1088 | + }; | ||
| 1089 | + | ||
| 1090 | + inline for (action_map) |entry| { | ||
| 1091 | + if (std.mem.eql(u8, name, entry[0])) { | ||
| 1092 | + return entry[1]; | ||
| 1093 | + } | ||
| 1094 | + } | ||
| 1095 | + return null; | ||
| 1096 | +} | ||
| 1097 | + | ||
| 1098 | +fn key_name_to_keysym(name: []const u8) ?u64 { | ||
| 1099 | + const key_map = .{ | ||
| 1100 | + .{ "Return", 0xff0d }, | ||
| 1101 | + .{ "Enter", 0xff0d }, | ||
| 1102 | + .{ "Tab", 0xff09 }, | ||
| 1103 | + .{ "Escape", 0xff1b }, | ||
| 1104 | + .{ "BackSpace", 0xff08 }, | ||
| 1105 | + .{ "Delete", 0xffff }, | ||
| 1106 | + .{ "space", 0x0020 }, | ||
| 1107 | + .{ "Space", 0x0020 }, | ||
| 1108 | + .{ "comma", 0x002c }, | ||
| 1109 | + .{ "Comma", 0x002c }, | ||
| 1110 | + .{ "period", 0x002e }, | ||
| 1111 | + .{ "Period", 0x002e }, | ||
| 1112 | + .{ "slash", 0x002f }, | ||
| 1113 | + .{ "Slash", 0x002f }, | ||
| 1114 | + .{ "minus", 0x002d }, | ||
| 1115 | + .{ "Minus", 0x002d }, | ||
| 1116 | + .{ "equal", 0x003d }, | ||
| 1117 | + .{ "Equal", 0x003d }, | ||
| 1118 | + .{ "bracketleft", 0x005b }, | ||
| 1119 | + .{ "bracketright", 0x005d }, | ||
| 1120 | + .{ "backslash", 0x005c }, | ||
| 1121 | + .{ "semicolon", 0x003b }, | ||
| 1122 | + .{ "apostrophe", 0x0027 }, | ||
| 1123 | + .{ "grave", 0x0060 }, | ||
| 1124 | + .{ "Left", 0xff51 }, | ||
| 1125 | + .{ "Up", 0xff52 }, | ||
| 1126 | + .{ "Right", 0xff53 }, | ||
| 1127 | + .{ "Down", 0xff54 }, | ||
| 1128 | + .{ "F1", 0xffbe }, | ||
| 1129 | + .{ "F2", 0xffbf }, | ||
| 1130 | + .{ "F3", 0xffc0 }, | ||
| 1131 | + .{ "F4", 0xffc1 }, | ||
| 1132 | + .{ "F5", 0xffc2 }, | ||
| 1133 | + .{ "F6", 0xffc3 }, | ||
| 1134 | + .{ "F7", 0xffc4 }, | ||
| 1135 | + .{ "F8", 0xffc5 }, | ||
| 1136 | + .{ "F9", 0xffc6 }, | ||
| 1137 | + .{ "F10", 0xffc7 }, | ||
| 1138 | + .{ "F11", 0xffc8 }, | ||
| 1139 | + .{ "F12", 0xffc9 }, | ||
| 1140 | + .{ "Print", 0xff61 }, | ||
| 1141 | + .{ "XF86AudioRaiseVolume", 0x1008ff13 }, | ||
| 1142 | + .{ "XF86AudioLowerVolume", 0x1008ff11 }, | ||
| 1143 | + .{ "XF86AudioMute", 0x1008ff12 }, | ||
| 1144 | + .{ "XF86AudioPlay", 0x1008ff14 }, | ||
| 1145 | + .{ "XF86AudioPause", 0x1008ff31 }, | ||
| 1146 | + .{ "XF86AudioNext", 0x1008ff17 }, | ||
| 1147 | + .{ "XF86AudioPrev", 0x1008ff16 }, | ||
| 1148 | + .{ "XF86MonBrightnessUp", 0x1008ff02 }, | ||
| 1149 | + .{ "XF86MonBrightnessDown", 0x1008ff03 }, | ||
| 1150 | + }; | ||
| 1151 | + | ||
| 1152 | + inline for (key_map) |entry| { | ||
| 1153 | + if (std.mem.eql(u8, name, entry[0])) { | ||
| 1154 | + return entry[1]; | ||
| 1155 | + } | ||
| 1156 | + } | ||
| 1157 | + | ||
| 1158 | + if (name.len == 1) { | ||
| 1159 | + const char = name[0]; | ||
| 1160 | + if (char >= 'a' and char <= 'z') { | ||
| 1161 | + return char; | ||
| 1162 | + } | ||
| 1163 | + if (char >= 'A' and char <= 'Z') { | ||
| 1164 | + return char + 32; | ||
| 1165 | + } | ||
| 1166 | + if (char >= '0' and char <= '9') { | ||
| 1167 | + return char; | ||
| 1168 | + } | ||
| 1169 | + } | ||
| 1170 | + | ||
| 1171 | + return null; | ||
| 1172 | +} | ||
+0 −1033 src/config/lua_api.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,1033 +0,0 @@ | |||
| 1 | -use mlua::{Lua, Table, Value}; | ||
| 2 | -use std::cell::RefCell; | ||
| 3 | -use std::rc::Rc; | ||
| 4 | - | ||
| 5 | -use crate::ColorScheme; | ||
| 6 | -use crate::bar::BlockConfig; | ||
| 7 | -use crate::errors::ConfigError; | ||
| 8 | -use crate::keyboard::handlers::{Arg, KeyAction, KeyBinding, KeyPress}; | ||
| 9 | -use crate::keyboard::keysyms::{self, Keysym}; | ||
| 10 | -use x11rb::protocol::xproto::KeyButMask; | ||
| 11 | - | ||
| 12 | -#[derive(Clone)] | ||
| 13 | -pub struct ConfigBuilder { | ||
| 14 | - pub border_width: u32, | ||
| 15 | - pub border_focused: u32, | ||
| 16 | - pub border_unfocused: u32, | ||
| 17 | - pub font: String, | ||
| 18 | - pub gaps_enabled: bool, | ||
| 19 | - pub smartgaps_enabled: bool, | ||
| 20 | - pub gap_inner_horizontal: u32, | ||
| 21 | - pub gap_inner_vertical: u32, | ||
| 22 | - pub gap_outer_horizontal: u32, | ||
| 23 | - pub gap_outer_vertical: u32, | ||
| 24 | - pub terminal: String, | ||
| 25 | - pub modkey: KeyButMask, | ||
| 26 | - pub tags: Vec<String>, | ||
| 27 | - pub layout_symbols: Vec<crate::LayoutSymbolOverride>, | ||
| 28 | - pub keybindings: Vec<KeyBinding>, | ||
| 29 | - pub tag_back_and_forth: bool, | ||
| 30 | - pub window_rules: Vec<crate::WindowRule>, | ||
| 31 | - pub status_blocks: Vec<BlockConfig>, | ||
| 32 | - pub scheme_normal: ColorScheme, | ||
| 33 | - pub scheme_occupied: ColorScheme, | ||
| 34 | - pub scheme_selected: ColorScheme, | ||
| 35 | - pub scheme_urgent: ColorScheme, | ||
| 36 | - pub autostart: Vec<String>, | ||
| 37 | - pub auto_tile: bool, | ||
| 38 | - pub hide_vacant_tags: bool, | ||
| 39 | -} | ||
| 40 | - | ||
| 41 | -impl Default for ConfigBuilder { | ||
| 42 | - fn default() -> Self { | ||
| 43 | - Self { | ||
| 44 | - border_width: 2, | ||
| 45 | - border_focused: 0x6dade3, | ||
| 46 | - border_unfocused: 0xbbbbbb, | ||
| 47 | - font: "monospace:style=Bold:size=10".to_string(), | ||
| 48 | - gaps_enabled: true, | ||
| 49 | - smartgaps_enabled: true, | ||
| 50 | - gap_inner_horizontal: 5, | ||
| 51 | - gap_inner_vertical: 5, | ||
| 52 | - gap_outer_horizontal: 5, | ||
| 53 | - gap_outer_vertical: 5, | ||
| 54 | - terminal: "st".to_string(), | ||
| 55 | - modkey: KeyButMask::MOD4, | ||
| 56 | - tags: vec!["1".into(), "2".into(), "3".into()], | ||
| 57 | - layout_symbols: Vec::new(), | ||
| 58 | - keybindings: Vec::new(), | ||
| 59 | - tag_back_and_forth: false, | ||
| 60 | - window_rules: Vec::new(), | ||
| 61 | - status_blocks: Vec::new(), | ||
| 62 | - scheme_normal: ColorScheme { | ||
| 63 | - foreground: 0xffffff, | ||
| 64 | - background: 0x000000, | ||
| 65 | - underline: 0x444444, | ||
| 66 | - }, | ||
| 67 | - scheme_occupied: ColorScheme { | ||
| 68 | - foreground: 0xffffff, | ||
| 69 | - background: 0x000000, | ||
| 70 | - underline: 0x444444, | ||
| 71 | - }, | ||
| 72 | - scheme_selected: ColorScheme { | ||
| 73 | - foreground: 0xffffff, | ||
| 74 | - background: 0x000000, | ||
| 75 | - underline: 0x444444, | ||
| 76 | - }, | ||
| 77 | - scheme_urgent: ColorScheme { | ||
| 78 | - foreground: 0xff5555, | ||
| 79 | - background: 0x000000, | ||
| 80 | - underline: 0xff5555, | ||
| 81 | - }, | ||
| 82 | - autostart: Vec::new(), | ||
| 83 | - auto_tile: false, | ||
| 84 | - hide_vacant_tags: false, | ||
| 85 | - } | ||
| 86 | - } | ||
| 87 | -} | ||
| 88 | - | ||
| 89 | -type SharedBuilder = Rc<RefCell<ConfigBuilder>>; | ||
| 90 | - | ||
| 91 | -pub fn register_api(lua: &Lua) -> Result<SharedBuilder, ConfigError> { | ||
| 92 | - let builder = Rc::new(RefCell::new(ConfigBuilder::default())); | ||
| 93 | - | ||
| 94 | - let oxwm_table = lua.create_table()?; | ||
| 95 | - | ||
| 96 | - register_spawn(lua, &oxwm_table, builder.clone())?; | ||
| 97 | - register_key_module(lua, &oxwm_table, builder.clone())?; | ||
| 98 | - register_gaps_module(lua, &oxwm_table, builder.clone())?; | ||
| 99 | - register_border_module(lua, &oxwm_table, builder.clone())?; | ||
| 100 | - register_client_module(lua, &oxwm_table)?; | ||
| 101 | - register_layout_module(lua, &oxwm_table)?; | ||
| 102 | - register_tag_module(lua, &oxwm_table, builder.clone())?; | ||
| 103 | - register_monitor_module(lua, &oxwm_table)?; | ||
| 104 | - register_rule_module(lua, &oxwm_table, builder.clone())?; | ||
| 105 | - register_bar_module(lua, &oxwm_table, builder.clone())?; | ||
| 106 | - register_misc(lua, &oxwm_table, builder.clone())?; | ||
| 107 | - | ||
| 108 | - lua.globals().set("oxwm", oxwm_table)?; | ||
| 109 | - | ||
| 110 | - Ok(builder) | ||
| 111 | -} | ||
| 112 | - | ||
| 113 | -fn register_spawn(lua: &Lua, parent: &Table, _builder: SharedBuilder) -> Result<(), ConfigError> { | ||
| 114 | - let spawn = lua.create_function(|lua, cmd: Value| create_action_table(lua, "Spawn", cmd))?; | ||
| 115 | - let spawn_terminal = | ||
| 116 | - lua.create_function(|lua, ()| create_action_table(lua, "SpawnTerminal", Value::Nil))?; | ||
| 117 | - parent.set("spawn", spawn)?; | ||
| 118 | - parent.set("spawn_terminal", spawn_terminal)?; | ||
| 119 | - Ok(()) | ||
| 120 | -} | ||
| 121 | - | ||
| 122 | -fn register_key_module( | ||
| 123 | - lua: &Lua, | ||
| 124 | - parent: &Table, | ||
| 125 | - builder: SharedBuilder, | ||
| 126 | -) -> Result<(), ConfigError> { | ||
| 127 | - let key_table = lua.create_table()?; | ||
| 128 | - | ||
| 129 | - let builder_clone = builder.clone(); | ||
| 130 | - let bind = lua.create_function(move |lua, (mods, key, action): (Value, String, Value)| { | ||
| 131 | - let modifiers = parse_modifiers_value(lua, mods)?; | ||
| 132 | - let keysym = parse_keysym(&key)?; | ||
| 133 | - let (key_action, arg) = parse_action_value(lua, action)?; | ||
| 134 | - | ||
| 135 | - let binding = KeyBinding::single_key(modifiers, keysym, key_action, arg); | ||
| 136 | - builder_clone.borrow_mut().keybindings.push(binding); | ||
| 137 | - | ||
| 138 | - Ok(()) | ||
| 139 | - })?; | ||
| 140 | - | ||
| 141 | - let builder_clone = builder.clone(); | ||
| 142 | - let chord = lua.create_function(move |lua, (keys, action): (Table, Value)| { | ||
| 143 | - let mut key_presses = Vec::new(); | ||
| 144 | - | ||
| 145 | - for i in 1..=keys.len()? { | ||
| 146 | - let key_spec: Table = keys.get(i)?; | ||
| 147 | - let mods: Value = key_spec.get(1)?; | ||
| 148 | - let key: String = key_spec.get(2)?; | ||
| 149 | - | ||
| 150 | - let modifiers = parse_modifiers_value(lua, mods)?; | ||
| 151 | - let keysym = parse_keysym(&key)?; | ||
| 152 | - | ||
| 153 | - key_presses.push(KeyPress { modifiers, keysym }); | ||
| 154 | - } | ||
| 155 | - | ||
| 156 | - let (key_action, arg) = parse_action_value(lua, action)?; | ||
| 157 | - let binding = KeyBinding::new(key_presses, key_action, arg); | ||
| 158 | - builder_clone.borrow_mut().keybindings.push(binding); | ||
| 159 | - | ||
| 160 | - Ok(()) | ||
| 161 | - })?; | ||
| 162 | - | ||
| 163 | - key_table.set("bind", bind)?; | ||
| 164 | - key_table.set("chord", chord)?; | ||
| 165 | - parent.set("key", key_table)?; | ||
| 166 | - Ok(()) | ||
| 167 | -} | ||
| 168 | - | ||
| 169 | -fn register_gaps_module( | ||
| 170 | - lua: &Lua, | ||
| 171 | - parent: &Table, | ||
| 172 | - builder: SharedBuilder, | ||
| 173 | -) -> Result<(), ConfigError> { | ||
| 174 | - let gaps_table = lua.create_table()?; | ||
| 175 | - | ||
| 176 | - let builder_clone = builder.clone(); | ||
| 177 | - let set_enabled = lua.create_function(move |_, enabled: bool| { | ||
| 178 | - builder_clone.borrow_mut().gaps_enabled = enabled; | ||
| 179 | - Ok(()) | ||
| 180 | - })?; | ||
| 181 | - | ||
| 182 | - let builder_clone = builder.clone(); | ||
| 183 | - let enable = lua.create_function(move |_, ()| { | ||
| 184 | - builder_clone.borrow_mut().gaps_enabled = true; | ||
| 185 | - Ok(()) | ||
| 186 | - })?; | ||
| 187 | - | ||
| 188 | - let builder_clone = builder.clone(); | ||
| 189 | - let disable = lua.create_function(move |_, ()| { | ||
| 190 | - builder_clone.borrow_mut().gaps_enabled = false; | ||
| 191 | - Ok(()) | ||
| 192 | - })?; | ||
| 193 | - | ||
| 194 | - let builder_clone = builder.clone(); | ||
| 195 | - let set_inner = lua.create_function(move |_, (h, v): (u32, u32)| { | ||
| 196 | - let mut b = builder_clone.borrow_mut(); | ||
| 197 | - b.gap_inner_horizontal = h; | ||
| 198 | - b.gap_inner_vertical = v; | ||
| 199 | - Ok(()) | ||
| 200 | - })?; | ||
| 201 | - | ||
| 202 | - let builder_clone = builder.clone(); | ||
| 203 | - let set_outer = lua.create_function(move |_, (h, v): (u32, u32)| { | ||
| 204 | - let mut b = builder_clone.borrow_mut(); | ||
| 205 | - b.gap_outer_horizontal = h; | ||
| 206 | - b.gap_outer_vertical = v; | ||
| 207 | - Ok(()) | ||
| 208 | - })?; | ||
| 209 | - | ||
| 210 | - let builder_clone = builder.clone(); | ||
| 211 | - let set_smart = lua.create_function(move |_, enabled: bool| { | ||
| 212 | - builder_clone.borrow_mut().smartgaps_enabled = enabled; | ||
| 213 | - Ok(()) | ||
| 214 | - })?; | ||
| 215 | - | ||
| 216 | - gaps_table.set("set_enabled", set_enabled)?; | ||
| 217 | - gaps_table.set("enable", enable)?; | ||
| 218 | - gaps_table.set("disable", disable)?; | ||
| 219 | - gaps_table.set("set_inner", set_inner)?; | ||
| 220 | - gaps_table.set("set_outer", set_outer)?; | ||
| 221 | - gaps_table.set("set_smart", set_smart)?; | ||
| 222 | - parent.set("gaps", gaps_table)?; | ||
| 223 | - Ok(()) | ||
| 224 | -} | ||
| 225 | - | ||
| 226 | -fn register_border_module( | ||
| 227 | - lua: &Lua, | ||
| 228 | - parent: &Table, | ||
| 229 | - builder: SharedBuilder, | ||
| 230 | -) -> Result<(), ConfigError> { | ||
| 231 | - let border_table = lua.create_table()?; | ||
| 232 | - | ||
| 233 | - let builder_clone = builder.clone(); | ||
| 234 | - let set_width = lua.create_function(move |_, width: u32| { | ||
| 235 | - builder_clone.borrow_mut().border_width = width; | ||
| 236 | - Ok(()) | ||
| 237 | - })?; | ||
| 238 | - | ||
| 239 | - let builder_clone = builder.clone(); | ||
| 240 | - let set_focused_color = lua.create_function(move |_, color: Value| { | ||
| 241 | - let color_u32 = parse_color_value(color)?; | ||
| 242 | - builder_clone.borrow_mut().border_focused = color_u32; | ||
| 243 | - Ok(()) | ||
| 244 | - })?; | ||
| 245 | - | ||
| 246 | - let builder_clone = builder.clone(); | ||
| 247 | - let set_unfocused_color = lua.create_function(move |_, color: Value| { | ||
| 248 | - let color_u32 = parse_color_value(color)?; | ||
| 249 | - builder_clone.borrow_mut().border_unfocused = color_u32; | ||
| 250 | - Ok(()) | ||
| 251 | - })?; | ||
| 252 | - | ||
| 253 | - border_table.set("set_width", set_width)?; | ||
| 254 | - border_table.set("set_focused_color", set_focused_color)?; | ||
| 255 | - border_table.set("set_unfocused_color", set_unfocused_color)?; | ||
| 256 | - parent.set("border", border_table)?; | ||
| 257 | - Ok(()) | ||
| 258 | -} | ||
| 259 | - | ||
| 260 | -fn register_client_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { | ||
| 261 | - let client_table = lua.create_table()?; | ||
| 262 | - | ||
| 263 | - let kill = lua.create_function(|lua, ()| create_action_table(lua, "KillClient", Value::Nil))?; | ||
| 264 | - | ||
| 265 | - let toggle_fullscreen = | ||
| 266 | - lua.create_function(|lua, ()| create_action_table(lua, "ToggleFullScreen", Value::Nil))?; | ||
| 267 | - | ||
| 268 | - let toggle_floating = | ||
| 269 | - lua.create_function(|lua, ()| create_action_table(lua, "ToggleFloating", Value::Nil))?; | ||
| 270 | - | ||
| 271 | - let focus_stack = lua.create_function(|lua, dir: i32| { | ||
| 272 | - create_action_table(lua, "FocusStack", Value::Integer(dir as i64)) | ||
| 273 | - })?; | ||
| 274 | - | ||
| 275 | - let move_stack = lua.create_function(|lua, dir: i32| { | ||
| 276 | - create_action_table(lua, "MoveStack", Value::Integer(dir as i64)) | ||
| 277 | - })?; | ||
| 278 | - | ||
| 279 | - client_table.set("kill", kill)?; | ||
| 280 | - client_table.set("toggle_fullscreen", toggle_fullscreen)?; | ||
| 281 | - client_table.set("toggle_floating", toggle_floating)?; | ||
| 282 | - client_table.set("focus_stack", focus_stack)?; | ||
| 283 | - client_table.set("move_stack", move_stack)?; | ||
| 284 | - | ||
| 285 | - parent.set("client", client_table)?; | ||
| 286 | - Ok(()) | ||
| 287 | -} | ||
| 288 | - | ||
| 289 | -fn register_layout_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { | ||
| 290 | - let layout_table = lua.create_table()?; | ||
| 291 | - | ||
| 292 | - let cycle = | ||
| 293 | - lua.create_function(|lua, ()| create_action_table(lua, "CycleLayout", Value::Nil))?; | ||
| 294 | - | ||
| 295 | - let set = lua.create_function(|lua, name: String| { | ||
| 296 | - create_action_table( | ||
| 297 | - lua, | ||
| 298 | - "ChangeLayout", | ||
| 299 | - Value::String(lua.create_string(&name)?), | ||
| 300 | - ) | ||
| 301 | - })?; | ||
| 302 | - | ||
| 303 | - let scroll_left = | ||
| 304 | - lua.create_function(|lua, ()| create_action_table(lua, "ScrollLeft", Value::Nil))?; | ||
| 305 | - | ||
| 306 | - let scroll_right = | ||
| 307 | - lua.create_function(|lua, ()| create_action_table(lua, "ScrollRight", Value::Nil))?; | ||
| 308 | - | ||
| 309 | - layout_table.set("cycle", cycle)?; | ||
| 310 | - layout_table.set("set", set)?; | ||
| 311 | - layout_table.set("scroll_left", scroll_left)?; | ||
| 312 | - layout_table.set("scroll_right", scroll_right)?; | ||
| 313 | - parent.set("layout", layout_table)?; | ||
| 314 | - Ok(()) | ||
| 315 | -} | ||
| 316 | - | ||
| 317 | -fn register_tag_module( | ||
| 318 | - lua: &Lua, | ||
| 319 | - parent: &Table, | ||
| 320 | - builder: SharedBuilder, | ||
| 321 | -) -> Result<(), ConfigError> { | ||
| 322 | - let builder_clone = builder.clone(); | ||
| 323 | - let tag_table = lua.create_table()?; | ||
| 324 | - | ||
| 325 | - let view = lua.create_function(|lua, idx: i32| { | ||
| 326 | - create_action_table(lua, "ViewTag", Value::Integer(idx as i64)) | ||
| 327 | - })?; | ||
| 328 | - | ||
| 329 | - let view_next = | ||
| 330 | - lua.create_function(|lua, ()| create_action_table(lua, "ViewNextTag", Value::Nil))?; | ||
| 331 | - | ||
| 332 | - let view_previous = | ||
| 333 | - lua.create_function(|lua, ()| create_action_table(lua, "ViewPreviousTag", Value::Nil))?; | ||
| 334 | - | ||
| 335 | - let view_next_nonempty = | ||
| 336 | - lua.create_function(|lua, ()| create_action_table(lua, "ViewNextNonEmptyTag", Value::Nil))?; | ||
| 337 | - | ||
| 338 | - let view_previous_nonempty = lua.create_function(|lua, ()| { | ||
| 339 | - create_action_table(lua, "ViewPreviousNonEmptyTag", Value::Nil) | ||
| 340 | - })?; | ||
| 341 | - | ||
| 342 | - let toggleview = lua.create_function(|lua, idx: i32| { | ||
| 343 | - create_action_table(lua, "ToggleView", Value::Integer(idx as i64)) | ||
| 344 | - })?; | ||
| 345 | - | ||
| 346 | - let move_to = lua.create_function(|lua, idx: i32| { | ||
| 347 | - create_action_table(lua, "MoveToTag", Value::Integer(idx as i64)) | ||
| 348 | - })?; | ||
| 349 | - | ||
| 350 | - let toggletag = lua.create_function(|lua, idx: i32| { | ||
| 351 | - create_action_table(lua, "ToggleTag", Value::Integer(idx as i64)) | ||
| 352 | - })?; | ||
| 353 | - | ||
| 354 | - let set_back_and_forth = lua.create_function(move |_, enabled: bool| { | ||
| 355 | - builder_clone.borrow_mut().tag_back_and_forth = enabled; | ||
| 356 | - Ok(()) | ||
| 357 | - })?; | ||
| 358 | - | ||
| 359 | - tag_table.set("view", view)?; | ||
| 360 | - tag_table.set("view_next", view_next)?; | ||
| 361 | - tag_table.set("view_previous", view_previous)?; | ||
| 362 | - tag_table.set("view_next_nonempty", view_next_nonempty)?; | ||
| 363 | - tag_table.set("view_previous_nonempty", view_previous_nonempty)?; | ||
| 364 | - tag_table.set("toggleview", toggleview)?; | ||
| 365 | - tag_table.set("move_to", move_to)?; | ||
| 366 | - tag_table.set("toggletag", toggletag)?; | ||
| 367 | - tag_table.set("set_back_and_forth", set_back_and_forth)?; | ||
| 368 | - parent.set("tag", tag_table)?; | ||
| 369 | - Ok(()) | ||
| 370 | -} | ||
| 371 | - | ||
| 372 | -fn register_monitor_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> { | ||
| 373 | - let monitor_table = lua.create_table()?; | ||
| 374 | - | ||
| 375 | - let focus = lua.create_function(|lua, direction: i64| { | ||
| 376 | - create_action_table(lua, "FocusMonitor", Value::Integer(direction)) | ||
| 377 | - })?; | ||
| 378 | - | ||
| 379 | - let tag = lua.create_function(|lua, direction: i64| { | ||
| 380 | - create_action_table(lua, "TagMonitor", Value::Integer(direction)) | ||
| 381 | - })?; | ||
| 382 | - | ||
| 383 | - monitor_table.set("focus", focus)?; | ||
| 384 | - monitor_table.set("tag", tag)?; | ||
| 385 | - parent.set("monitor", monitor_table)?; | ||
| 386 | - Ok(()) | ||
| 387 | -} | ||
| 388 | - | ||
| 389 | -fn register_rule_module( | ||
| 390 | - lua: &Lua, | ||
| 391 | - parent: &Table, | ||
| 392 | - builder: SharedBuilder, | ||
| 393 | -) -> Result<(), ConfigError> { | ||
| 394 | - let rule_table = lua.create_table()?; | ||
| 395 | - | ||
| 396 | - let builder_clone = builder.clone(); | ||
| 397 | - let add = lua.create_function(move |_, config: Table| { | ||
| 398 | - let class: Option<String> = config.get("class").ok(); | ||
| 399 | - let instance: Option<String> = config.get("instance").ok(); | ||
| 400 | - let title: Option<String> = config.get("title").ok(); | ||
| 401 | - let is_floating: Option<bool> = config.get("floating").ok(); | ||
| 402 | - let monitor: Option<usize> = config.get("monitor").ok(); | ||
| 403 | - let focus: Option<bool> = config.get("focus").ok(); | ||
| 404 | - | ||
| 405 | - let tags: Option<u32> = if let Ok(tag_index) = config.get::<i32>("tag") { | ||
| 406 | - if tag_index > 0 { | ||
| 407 | - Some(1 << (tag_index - 1)) | ||
| 408 | - } else { | ||
| 409 | - None | ||
| 410 | - } | ||
| 411 | - } else { | ||
| 412 | - None | ||
| 413 | - }; | ||
| 414 | - | ||
| 415 | - let rule = crate::WindowRule { | ||
| 416 | - class, | ||
| 417 | - instance, | ||
| 418 | - title, | ||
| 419 | - tags, | ||
| 420 | - focus, | ||
| 421 | - is_floating, | ||
| 422 | - monitor, | ||
| 423 | - }; | ||
| 424 | - | ||
| 425 | - builder_clone.borrow_mut().window_rules.push(rule); | ||
| 426 | - Ok(()) | ||
| 427 | - })?; | ||
| 428 | - | ||
| 429 | - rule_table.set("add", add)?; | ||
| 430 | - parent.set("rule", rule_table)?; | ||
| 431 | - Ok(()) | ||
| 432 | -} | ||
| 433 | - | ||
| 434 | -fn register_bar_module( | ||
| 435 | - lua: &Lua, | ||
| 436 | - parent: &Table, | ||
| 437 | - builder: SharedBuilder, | ||
| 438 | -) -> Result<(), ConfigError> { | ||
| 439 | - let bar_table = lua.create_table()?; | ||
| 440 | - | ||
| 441 | - let builder_clone = builder.clone(); | ||
| 442 | - let set_font = lua.create_function(move |_, font: String| { | ||
| 443 | - builder_clone.borrow_mut().font = font; | ||
| 444 | - Ok(()) | ||
| 445 | - })?; | ||
| 446 | - | ||
| 447 | - let block_table = lua.create_table()?; | ||
| 448 | - | ||
| 449 | - let ram = | ||
| 450 | - lua.create_function(|lua, config: Table| create_block_config(lua, config, "Ram", None))?; | ||
| 451 | - | ||
| 452 | - let datetime = lua.create_function(|lua, config: Table| { | ||
| 453 | - let date_format: String = config.get("date_format").map_err(|_| { | ||
| 454 | - mlua::Error::RuntimeError( | ||
| 455 | - "oxwm.bar.block.datetime: 'date_format' field is required (e.g., '%H:%M')".into(), | ||
| 456 | - ) | ||
| 457 | - })?; | ||
| 458 | - create_block_config( | ||
| 459 | - lua, | ||
| 460 | - config, | ||
| 461 | - "DateTime", | ||
| 462 | - Some(Value::String(lua.create_string(&date_format)?)), | ||
| 463 | - ) | ||
| 464 | - })?; | ||
| 465 | - | ||
| 466 | - let shell = lua.create_function(|lua, config: Table| { | ||
| 467 | - let command: String = config.get("command").map_err(|_| { | ||
| 468 | - mlua::Error::RuntimeError("oxwm.bar.block.shell: 'command' field is required".into()) | ||
| 469 | - })?; | ||
| 470 | - create_block_config( | ||
| 471 | - lua, | ||
| 472 | - config, | ||
| 473 | - "Shell", | ||
| 474 | - Some(Value::String(lua.create_string(&command)?)), | ||
| 475 | - ) | ||
| 476 | - })?; | ||
| 477 | - | ||
| 478 | - let static_block = lua.create_function(|lua, config: Table| { | ||
| 479 | - let text: String = config.get("text").map_err(|_| { | ||
| 480 | - mlua::Error::RuntimeError("oxwm.bar.block.static: 'text' field is required".into()) | ||
| 481 | - })?; | ||
| 482 | - create_block_config( | ||
| 483 | - lua, | ||
| 484 | - config, | ||
| 485 | - "Static", | ||
| 486 | - Some(Value::String(lua.create_string(&text)?)), | ||
| 487 | - ) | ||
| 488 | - })?; | ||
| 489 | - | ||
| 490 | - let battery = lua.create_function(|lua, config: Table| { | ||
| 491 | - let charging: String = config.get("charging").map_err(|_| { | ||
| 492 | - mlua::Error::RuntimeError("oxwm.bar.block.battery: 'charging' field is required".into()) | ||
| 493 | - })?; | ||
| 494 | - let discharging: String = config.get("discharging").map_err(|_| { | ||
| 495 | - mlua::Error::RuntimeError( | ||
| 496 | - "oxwm.bar.block.battery: 'discharging' field is required".into(), | ||
| 497 | - ) | ||
| 498 | - })?; | ||
| 499 | - let full: String = config.get("full").map_err(|_| { | ||
| 500 | - mlua::Error::RuntimeError("oxwm.bar.block.battery: 'full' field is required".into()) | ||
| 501 | - })?; | ||
| 502 | - let battery_name: Option<String> = config.get("battery_name").unwrap_or(None); | ||
| 503 | - | ||
| 504 | - let formats_table = lua.create_table()?; | ||
| 505 | - formats_table.set("charging", charging)?; | ||
| 506 | - formats_table.set("discharging", discharging)?; | ||
| 507 | - formats_table.set("full", full)?; | ||
| 508 | - formats_table.set("battery_name", battery_name)?; | ||
| 509 | - | ||
| 510 | - create_block_config(lua, config, "Battery", Some(Value::Table(formats_table))) | ||
| 511 | - })?; | ||
| 512 | - | ||
| 513 | - block_table.set("ram", ram)?; | ||
| 514 | - block_table.set("datetime", datetime)?; | ||
| 515 | - block_table.set("shell", shell)?; | ||
| 516 | - block_table.set("static", static_block)?; | ||
| 517 | - block_table.set("battery", battery)?; | ||
| 518 | - | ||
| 519 | - // Deprecated add_block() function for backwards compatibility | ||
| 520 | - // This allows old configs to still work, but users should migrate to set_blocks() | ||
| 521 | - let builder_clone = builder.clone(); | ||
| 522 | - let add_block = lua.create_function(move |_, (format, block_type, arg, interval, color, underline): (String, String, Value, u64, Value, Option<bool>)| -> mlua::Result<()> { | ||
| 523 | - eprintln!("WARNING: oxwm.bar.add_block() is deprecated. Please migrate to oxwm.bar.set_blocks() with block constructors."); | ||
| 524 | - eprintln!("See the migration guide for details."); | ||
| 525 | - | ||
| 526 | - let cmd = match block_type.as_str() { | ||
| 527 | - "DateTime" => { | ||
| 528 | - let fmt = if let Value::String(s) = arg { | ||
| 529 | - s.to_str()?.to_string() | ||
| 530 | - } else { | ||
| 531 | - return Err(mlua::Error::RuntimeError("DateTime block requires format string as third argument".into())); | ||
| 532 | - }; | ||
| 533 | - crate::bar::BlockCommand::DateTime(fmt) | ||
| 534 | - } | ||
| 535 | - "Shell" => { | ||
| 536 | - let cmd_str = if let Value::String(s) = arg { | ||
| 537 | - s.to_str()?.to_string() | ||
| 538 | - } else { | ||
| 539 | - return Err(mlua::Error::RuntimeError("Shell block requires command string as third argument".into())); | ||
| 540 | - }; | ||
| 541 | - crate::bar::BlockCommand::Shell(cmd_str) | ||
| 542 | - } | ||
| 543 | - "Ram" => crate::bar::BlockCommand::Ram, | ||
| 544 | - "Static" => { | ||
| 545 | - let text = if let Value::String(s) = arg { | ||
| 546 | - s.to_str()?.to_string() | ||
| 547 | - } else { | ||
| 548 | - String::new() | ||
| 549 | - }; | ||
| 550 | - crate::bar::BlockCommand::Static(text) | ||
| 551 | - } | ||
| 552 | - "Battery" => { | ||
| 553 | - return Err(mlua::Error::RuntimeError( | ||
| 554 | - "Battery block is not supported with add_block(). Please use oxwm.bar.set_blocks() with oxwm.bar.block.battery()".into() | ||
| 555 | - )); | ||
| 556 | - } | ||
| 557 | - _ => return Err(mlua::Error::RuntimeError(format!("Unknown block type '{}'", block_type))), | ||
| 558 | - }; | ||
| 559 | - | ||
| 560 | - let color_u32 = parse_color_value(color)?; | ||
| 561 | - | ||
| 562 | - let block = crate::bar::BlockConfig { | ||
| 563 | - format, | ||
| 564 | - command: cmd, | ||
| 565 | - interval_secs: interval, | ||
| 566 | - color: color_u32, | ||
| 567 | - underline: underline.unwrap_or(false), | ||
| 568 | - }; | ||
| 569 | - | ||
| 570 | - builder_clone.borrow_mut().status_blocks.push(block); | ||
| 571 | - Ok(()) | ||
| 572 | - })?; | ||
| 573 | - | ||
| 574 | - let builder_clone = builder.clone(); | ||
| 575 | - let set_blocks = lua.create_function(move |_, blocks: Table| { | ||
| 576 | - use crate::bar::BlockCommand; | ||
| 577 | - | ||
| 578 | - let mut block_configs = Vec::new(); | ||
| 579 | - | ||
| 580 | - for i in 1..=blocks.len()? { | ||
| 581 | - let block_table: Table = blocks.get(i)?; | ||
| 582 | - let block_type: String = block_table.get("__block_type")?; | ||
| 583 | - let format: String = block_table.get("format").unwrap_or_default(); | ||
| 584 | - let interval: u64 = block_table.get("interval")?; | ||
| 585 | - let color_val: Value = block_table.get("color")?; | ||
| 586 | - let underline: bool = block_table.get("underline").unwrap_or(false); | ||
| 587 | - let arg: Option<Value> = block_table.get("__arg").ok(); | ||
| 588 | - | ||
| 589 | - let cmd = match block_type.as_str() { | ||
| 590 | - "DateTime" => { | ||
| 591 | - let fmt = arg | ||
| 592 | - .and_then(|v| { | ||
| 593 | - if let Value::String(s) = v { | ||
| 594 | - s.to_str().ok().map(|s| s.to_string()) | ||
| 595 | - } else { | ||
| 596 | - None | ||
| 597 | - } | ||
| 598 | - }) | ||
| 599 | - .ok_or_else(|| { | ||
| 600 | - mlua::Error::RuntimeError("DateTime block missing format".into()) | ||
| 601 | - })?; | ||
| 602 | - BlockCommand::DateTime(fmt) | ||
| 603 | - } | ||
| 604 | - "Shell" => { | ||
| 605 | - let cmd_str = arg | ||
| 606 | - .and_then(|v| { | ||
| 607 | - if let Value::String(s) = v { | ||
| 608 | - s.to_str().ok().map(|s| s.to_string()) | ||
| 609 | - } else { | ||
| 610 | - None | ||
| 611 | - } | ||
| 612 | - }) | ||
| 613 | - .ok_or_else(|| { | ||
| 614 | - mlua::Error::RuntimeError("Shell block missing command".into()) | ||
| 615 | - })?; | ||
| 616 | - BlockCommand::Shell(cmd_str) | ||
| 617 | - } | ||
| 618 | - "Ram" => BlockCommand::Ram, | ||
| 619 | - "Static" => { | ||
| 620 | - let text = arg | ||
| 621 | - .and_then(|v| { | ||
| 622 | - if let Value::String(s) = v { | ||
| 623 | - s.to_str().ok().map(|s| s.to_string()) | ||
| 624 | - } else { | ||
| 625 | - None | ||
| 626 | - } | ||
| 627 | - }) | ||
| 628 | - .unwrap_or_default(); | ||
| 629 | - BlockCommand::Static(text) | ||
| 630 | - } | ||
| 631 | - "Battery" => { | ||
| 632 | - let formats = arg | ||
| 633 | - .and_then(|v| { | ||
| 634 | - if let Value::Table(t) = v { | ||
| 635 | - Some(t) | ||
| 636 | - } else { | ||
| 637 | - None | ||
| 638 | - } | ||
| 639 | - }) | ||
| 640 | - .ok_or_else(|| { | ||
| 641 | - mlua::Error::RuntimeError("Battery block missing formats".into()) | ||
| 642 | - })?; | ||
| 643 | - | ||
| 644 | - let charging: String = formats.get("charging")?; | ||
| 645 | - let discharging: String = formats.get("discharging")?; | ||
| 646 | - let full: String = formats.get("full")?; | ||
| 647 | - let battery_name: Option<String> = formats.get("battery_name").unwrap_or(None); | ||
| 648 | - | ||
| 649 | - BlockCommand::Battery { | ||
| 650 | - format_charging: charging, | ||
| 651 | - format_discharging: discharging, | ||
| 652 | - format_full: full, | ||
| 653 | - battery_name, | ||
| 654 | - } | ||
| 655 | - } | ||
| 656 | - _ => { | ||
| 657 | - return Err(mlua::Error::RuntimeError(format!( | ||
| 658 | - "Unknown block type '{}'", | ||
| 659 | - block_type | ||
| 660 | - ))); | ||
| 661 | - } | ||
| 662 | - }; | ||
| 663 | - | ||
| 664 | - let color_u32 = parse_color_value(color_val)?; | ||
| 665 | - | ||
| 666 | - let block = crate::bar::BlockConfig { | ||
| 667 | - format, | ||
| 668 | - command: cmd, | ||
| 669 | - interval_secs: interval, | ||
| 670 | - color: color_u32, | ||
| 671 | - underline, | ||
| 672 | - }; | ||
| 673 | - | ||
| 674 | - block_configs.push(block); | ||
| 675 | - } | ||
| 676 | - | ||
| 677 | - builder_clone.borrow_mut().status_blocks = block_configs; | ||
| 678 | - Ok(()) | ||
| 679 | - })?; | ||
| 680 | - | ||
| 681 | - let builder_clone = builder.clone(); | ||
| 682 | - let set_scheme_normal = | ||
| 683 | - lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { | ||
| 684 | - let foreground = parse_color_value(fg)?; | ||
| 685 | - let background = parse_color_value(bg)?; | ||
| 686 | - let underline = parse_color_value(ul)?; | ||
| 687 | - | ||
| 688 | - builder_clone.borrow_mut().scheme_normal = ColorScheme { | ||
| 689 | - foreground, | ||
| 690 | - background, | ||
| 691 | - underline, | ||
| 692 | - }; | ||
| 693 | - Ok(()) | ||
| 694 | - })?; | ||
| 695 | - | ||
| 696 | - let builder_clone = builder.clone(); | ||
| 697 | - let set_scheme_occupied = | ||
| 698 | - lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { | ||
| 699 | - let foreground = parse_color_value(fg)?; | ||
| 700 | - let background = parse_color_value(bg)?; | ||
| 701 | - let underline = parse_color_value(ul)?; | ||
| 702 | - | ||
| 703 | - builder_clone.borrow_mut().scheme_occupied = ColorScheme { | ||
| 704 | - foreground, | ||
| 705 | - background, | ||
| 706 | - underline, | ||
| 707 | - }; | ||
| 708 | - Ok(()) | ||
| 709 | - })?; | ||
| 710 | - | ||
| 711 | - let builder_clone = builder.clone(); | ||
| 712 | - let set_scheme_selected = | ||
| 713 | - lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { | ||
| 714 | - let foreground = parse_color_value(fg)?; | ||
| 715 | - let background = parse_color_value(bg)?; | ||
| 716 | - let underline = parse_color_value(ul)?; | ||
| 717 | - | ||
| 718 | - builder_clone.borrow_mut().scheme_selected = ColorScheme { | ||
| 719 | - foreground, | ||
| 720 | - background, | ||
| 721 | - underline, | ||
| 722 | - }; | ||
| 723 | - Ok(()) | ||
| 724 | - })?; | ||
| 725 | - | ||
| 726 | - let builder_clone = builder.clone(); | ||
| 727 | - let set_scheme_urgent = | ||
| 728 | - lua.create_function(move |_, (fg, bg, ul): (Value, Value, Value)| { | ||
| 729 | - let foreground = parse_color_value(fg)?; | ||
| 730 | - let background = parse_color_value(bg)?; | ||
| 731 | - let underline = parse_color_value(ul)?; | ||
| 732 | - | ||
| 733 | - builder_clone.borrow_mut().scheme_urgent = ColorScheme { | ||
| 734 | - foreground, | ||
| 735 | - background, | ||
| 736 | - underline, | ||
| 737 | - }; | ||
| 738 | - Ok(()) | ||
| 739 | - })?; | ||
| 740 | - | ||
| 741 | - let builder_clone = builder.clone(); | ||
| 742 | - let set_hide_vacant_tags = lua.create_function(move |_, hide: bool| { | ||
| 743 | - builder_clone.borrow_mut().hide_vacant_tags = hide; | ||
| 744 | - Ok(()) | ||
| 745 | - })?; | ||
| 746 | - | ||
| 747 | - bar_table.set("set_font", set_font)?; | ||
| 748 | - bar_table.set("block", block_table)?; | ||
| 749 | - bar_table.set("add_block", add_block)?; // Deprecated, for backwards compatibility | ||
| 750 | - bar_table.set("set_blocks", set_blocks)?; | ||
| 751 | - bar_table.set("set_scheme_normal", set_scheme_normal)?; | ||
| 752 | - bar_table.set("set_scheme_occupied", set_scheme_occupied)?; | ||
| 753 | - bar_table.set("set_scheme_selected", set_scheme_selected)?; | ||
| 754 | - bar_table.set("set_scheme_urgent", set_scheme_urgent)?; | ||
| 755 | - bar_table.set("set_hide_vacant_tags", set_hide_vacant_tags)?; | ||
| 756 | - parent.set("bar", bar_table)?; | ||
| 757 | - Ok(()) | ||
| 758 | -} | ||
| 759 | - | ||
| 760 | -fn register_misc(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> { | ||
| 761 | - let builder_clone = builder.clone(); | ||
| 762 | - let set_terminal = lua.create_function(move |_, term: String| { | ||
| 763 | - builder_clone.borrow_mut().terminal = term; | ||
| 764 | - Ok(()) | ||
| 765 | - })?; | ||
| 766 | - | ||
| 767 | - let builder_clone = builder.clone(); | ||
| 768 | - let set_modkey = lua.create_function(move |_, modkey_str: String| { | ||
| 769 | - let modkey = parse_modkey_string(&modkey_str) | ||
| 770 | - .map_err(|e| mlua::Error::RuntimeError(format!("{}", e)))?; | ||
| 771 | - builder_clone.borrow_mut().modkey = modkey; | ||
| 772 | - Ok(()) | ||
| 773 | - })?; | ||
| 774 | - | ||
| 775 | - let builder_clone = builder.clone(); | ||
| 776 | - let set_tags = lua.create_function(move |_, tags: Vec<String>| { | ||
| 777 | - builder_clone.borrow_mut().tags = tags; | ||
| 778 | - Ok(()) | ||
| 779 | - })?; | ||
| 780 | - | ||
| 781 | - let quit = lua.create_function(|lua, ()| create_action_table(lua, "Quit", Value::Nil))?; | ||
| 782 | - | ||
| 783 | - let restart = lua.create_function(|lua, ()| create_action_table(lua, "Restart", Value::Nil))?; | ||
| 784 | - | ||
| 785 | - let toggle_gaps = | ||
| 786 | - lua.create_function(|lua, ()| create_action_table(lua, "ToggleGaps", Value::Nil))?; | ||
| 787 | - | ||
| 788 | - let set_master_factor = lua.create_function(|lua, delta: i32| { | ||
| 789 | - create_action_table(lua, "SetMasterFactor", Value::Integer(delta as i64)) | ||
| 790 | - })?; | ||
| 791 | - | ||
| 792 | - let inc_num_master = lua.create_function(|lua, delta: i32| { | ||
| 793 | - create_action_table(lua, "IncNumMaster", Value::Integer(delta as i64)) | ||
| 794 | - })?; | ||
| 795 | - | ||
| 796 | - let show_keybinds = | ||
| 797 | - lua.create_function(|lua, ()| create_action_table(lua, "ShowKeybindOverlay", Value::Nil))?; | ||
| 798 | - | ||
| 799 | - let focus_monitor = lua.create_function(|lua, idx: i32| { | ||
| 800 | - create_action_table(lua, "FocusMonitor", Value::Integer(idx as i64)) | ||
| 801 | - })?; | ||
| 802 | - | ||
| 803 | - let builder_clone = builder.clone(); | ||
| 804 | - let set_layout_symbol = lua.create_function(move |_, (name, symbol): (String, String)| { | ||
| 805 | - builder_clone | ||
| 806 | - .borrow_mut() | ||
| 807 | - .layout_symbols | ||
| 808 | - .push(crate::LayoutSymbolOverride { name, symbol }); | ||
| 809 | - Ok(()) | ||
| 810 | - })?; | ||
| 811 | - | ||
| 812 | - let builder_clone = builder.clone(); | ||
| 813 | - let autostart = lua.create_function(move |_, cmd: String| { | ||
| 814 | - builder_clone.borrow_mut().autostart.push(cmd); | ||
| 815 | - Ok(()) | ||
| 816 | - })?; | ||
| 817 | - | ||
| 818 | - let builder_clone = builder.clone(); | ||
| 819 | - let auto_tile = lua.create_function(move |_, enabled: bool| { | ||
| 820 | - builder_clone.borrow_mut().auto_tile = enabled; | ||
| 821 | - Ok(()) | ||
| 822 | - })?; | ||
| 823 | - | ||
| 824 | - parent.set("set_terminal", set_terminal)?; | ||
| 825 | - parent.set("set_modkey", set_modkey)?; | ||
| 826 | - parent.set("set_tags", set_tags)?; | ||
| 827 | - parent.set("set_layout_symbol", set_layout_symbol)?; | ||
| 828 | - parent.set("autostart", autostart)?; | ||
| 829 | - parent.set("quit", quit)?; | ||
| 830 | - parent.set("restart", restart)?; | ||
| 831 | - parent.set("toggle_gaps", toggle_gaps)?; | ||
| 832 | - parent.set("set_master_factor", set_master_factor)?; | ||
| 833 | - parent.set("inc_num_master", inc_num_master)?; | ||
| 834 | - parent.set("show_keybinds", show_keybinds)?; | ||
| 835 | - parent.set("focus_monitor", focus_monitor)?; | ||
| 836 | - parent.set("auto_tile", auto_tile)?; | ||
| 837 | - Ok(()) | ||
| 838 | -} | ||
| 839 | - | ||
| 840 | -fn parse_modifiers_value(_lua: &Lua, value: Value) -> mlua::Result<Vec<KeyButMask>> { | ||
| 841 | - match value { | ||
| 842 | - Value::Table(t) => { | ||
| 843 | - let mut mods = Vec::new(); | ||
| 844 | - for i in 1..=t.len()? { | ||
| 845 | - let mod_str: String = t.get(i)?; | ||
| 846 | - let mask = parse_modkey_string(&mod_str) | ||
| 847 | - .map_err(|e| mlua::Error::RuntimeError(format!("oxwm.key.bind: invalid modifier - {}", e)))?; | ||
| 848 | - mods.push(mask); | ||
| 849 | - } | ||
| 850 | - Ok(mods) | ||
| 851 | - } | ||
| 852 | - Value::String(s) => { | ||
| 853 | - let s_str = s.to_str()?; | ||
| 854 | - let mask = parse_modkey_string(&s_str) | ||
| 855 | - .map_err(|e| mlua::Error::RuntimeError(format!("oxwm.key.bind: invalid modifier - {}", e)))?; | ||
| 856 | - Ok(vec![mask]) | ||
| 857 | - } | ||
| 858 | - _ => Err(mlua::Error::RuntimeError( | ||
| 859 | - "oxwm.key.bind: first argument must be a table of modifiers like {\"Mod4\"} or {\"Mod4\", \"Shift\"}".into(), | ||
| 860 | - )), | ||
| 861 | - } | ||
| 862 | -} | ||
| 863 | - | ||
| 864 | -fn parse_modkey_string(s: &str) -> Result<KeyButMask, ConfigError> { | ||
| 865 | - match s { | ||
| 866 | - "Mod1" => Ok(KeyButMask::MOD1), | ||
| 867 | - "Mod2" => Ok(KeyButMask::MOD2), | ||
| 868 | - "Mod3" => Ok(KeyButMask::MOD3), | ||
| 869 | - "Mod4" => Ok(KeyButMask::MOD4), | ||
| 870 | - "Mod5" => Ok(KeyButMask::MOD5), | ||
| 871 | - "Shift" => Ok(KeyButMask::SHIFT), | ||
| 872 | - "Control" => Ok(KeyButMask::CONTROL), | ||
| 873 | - _ => Err(ConfigError::InvalidModkey(format!( | ||
| 874 | - "'{}' is not a valid modifier. Use one of: Mod1, Mod4, Shift, Control", | ||
| 875 | - s | ||
| 876 | - ))), | ||
| 877 | - } | ||
| 878 | -} | ||
| 879 | - | ||
| 880 | -fn parse_keysym(key: &str) -> mlua::Result<Keysym> { | ||
| 881 | - keysyms::keysym_from_str(key) | ||
| 882 | - .ok_or_else(|| mlua::Error::RuntimeError(format!("unknown key '{}'. valid keys include: Return, Space, A-Z, 0-9, F1-F12, Left, Right, Up, Down, etc. check oxwm.lua type definitions for the complete list", key))) | ||
| 883 | -} | ||
| 884 | - | ||
| 885 | -fn parse_action_value(_lua: &Lua, value: Value) -> mlua::Result<(KeyAction, Arg)> { | ||
| 886 | - match value { | ||
| 887 | - Value::Function(_) => { | ||
| 888 | - Err(mlua::Error::RuntimeError( | ||
| 889 | - "action must be a function call, not a function reference. did you forget ()? example: oxwm.spawn('st') not oxwm.spawn".into() | ||
| 890 | - )) | ||
| 891 | - } | ||
| 892 | - Value::Table(t) => { | ||
| 893 | - if let Ok(action_name) = t.get::<String>("__action") { | ||
| 894 | - let action = string_to_action(&action_name)?; | ||
| 895 | - let arg = if let Ok(arg_val) = t.get::<Value>("__arg") { | ||
| 896 | - value_to_arg(arg_val)? | ||
| 897 | - } else { | ||
| 898 | - Arg::None | ||
| 899 | - }; | ||
| 900 | - return Ok((action, arg)); | ||
| 901 | - } | ||
| 902 | - | ||
| 903 | - Err(mlua::Error::RuntimeError( | ||
| 904 | - "action must be a table returned by oxwm functions like oxwm.spawn(), oxwm.client.kill(), oxwm.quit(), etc.".into(), | ||
| 905 | - )) | ||
| 906 | - } | ||
| 907 | - _ => Err(mlua::Error::RuntimeError( | ||
| 908 | - "action must be a table returned by oxwm functions like oxwm.spawn(), oxwm.client.kill(), oxwm.quit(), etc.".into(), | ||
| 909 | - )), | ||
| 910 | - } | ||
| 911 | -} | ||
| 912 | - | ||
| 913 | -fn string_to_action(s: &str) -> mlua::Result<KeyAction> { | ||
| 914 | - match s { | ||
| 915 | - "Spawn" => Ok(KeyAction::Spawn), | ||
| 916 | - "SpawnTerminal" => Ok(KeyAction::SpawnTerminal), | ||
| 917 | - "KillClient" => Ok(KeyAction::KillClient), | ||
| 918 | - "FocusStack" => Ok(KeyAction::FocusStack), | ||
| 919 | - "MoveStack" => Ok(KeyAction::MoveStack), | ||
| 920 | - "Quit" => Ok(KeyAction::Quit), | ||
| 921 | - "Restart" => Ok(KeyAction::Restart), | ||
| 922 | - "ViewTag" => Ok(KeyAction::ViewTag), | ||
| 923 | - "ViewNextTag" => Ok(KeyAction::ViewNextTag), | ||
| 924 | - "ViewPreviousTag" => Ok(KeyAction::ViewPreviousTag), | ||
| 925 | - "ViewNextNonEmptyTag" => Ok(KeyAction::ViewNextNonEmptyTag), | ||
| 926 | - "ViewPreviousNonEmptyTag" => Ok(KeyAction::ViewPreviousNonEmptyTag), | ||
| 927 | - "ToggleView" => Ok(KeyAction::ToggleView), | ||
| 928 | - "MoveToTag" => Ok(KeyAction::MoveToTag), | ||
| 929 | - "ToggleTag" => Ok(KeyAction::ToggleTag), | ||
| 930 | - "ToggleGaps" => Ok(KeyAction::ToggleGaps), | ||
| 931 | - "SetMasterFactor" => Ok(KeyAction::SetMasterFactor), | ||
| 932 | - "IncNumMaster" => Ok(KeyAction::IncNumMaster), | ||
| 933 | - "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen), | ||
| 934 | - "ToggleFloating" => Ok(KeyAction::ToggleFloating), | ||
| 935 | - "ChangeLayout" => Ok(KeyAction::ChangeLayout), | ||
| 936 | - "CycleLayout" => Ok(KeyAction::CycleLayout), | ||
| 937 | - "FocusMonitor" => Ok(KeyAction::FocusMonitor), | ||
| 938 | - "TagMonitor" => Ok(KeyAction::TagMonitor), | ||
| 939 | - "ShowKeybindOverlay" => Ok(KeyAction::ShowKeybindOverlay), | ||
| 940 | - "ScrollLeft" => Ok(KeyAction::ScrollLeft), | ||
| 941 | - "ScrollRight" => Ok(KeyAction::ScrollRight), | ||
| 942 | - _ => Err(mlua::Error::RuntimeError(format!( | ||
| 943 | - "unknown action '{}'. this is an internal error, please report it", | ||
| 944 | - s | ||
| 945 | - ))), | ||
| 946 | - } | ||
| 947 | -} | ||
| 948 | - | ||
| 949 | -fn value_to_arg(value: Value) -> mlua::Result<Arg> { | ||
| 950 | - match value { | ||
| 951 | - Value::Nil => Ok(Arg::None), | ||
| 952 | - Value::String(s) => Ok(Arg::Str(s.to_str()?.to_string())), | ||
| 953 | - Value::Integer(i) => Ok(Arg::Int(i as i32)), | ||
| 954 | - Value::Number(n) => Ok(Arg::Int(n as i32)), | ||
| 955 | - Value::Table(t) => { | ||
| 956 | - let mut arr = Vec::new(); | ||
| 957 | - for i in 1..=t.len()? { | ||
| 958 | - let item: String = t.get(i)?; | ||
| 959 | - arr.push(item); | ||
| 960 | - } | ||
| 961 | - Ok(Arg::Array(arr)) | ||
| 962 | - } | ||
| 963 | - _ => Ok(Arg::None), | ||
| 964 | - } | ||
| 965 | -} | ||
| 966 | - | ||
| 967 | -fn create_action_table(lua: &Lua, action_name: &str, arg: Value) -> mlua::Result<Table> { | ||
| 968 | - let table = lua.create_table()?; | ||
| 969 | - table.set("__action", action_name)?; | ||
| 970 | - table.set("__arg", arg)?; | ||
| 971 | - Ok(table) | ||
| 972 | -} | ||
| 973 | - | ||
| 974 | -fn parse_color_value(value: Value) -> mlua::Result<u32> { | ||
| 975 | - match value { | ||
| 976 | - Value::Integer(i) => Ok(i as u32), | ||
| 977 | - Value::Number(n) => Ok(n as u32), | ||
| 978 | - Value::String(s) => { | ||
| 979 | - let s = s.to_str()?; | ||
| 980 | - if let Some(hex) = s.strip_prefix('#') { | ||
| 981 | - u32::from_str_radix(hex, 16).map_err(|e| { | ||
| 982 | - mlua::Error::RuntimeError(format!( | ||
| 983 | - "invalid hex color '{}': {}. use format like #ff0000 or 0xff0000", | ||
| 984 | - s, e | ||
| 985 | - )) | ||
| 986 | - }) | ||
| 987 | - } else if let Some(hex) = s.strip_prefix("0x") { | ||
| 988 | - u32::from_str_radix(hex, 16).map_err(|e| { | ||
| 989 | - mlua::Error::RuntimeError(format!( | ||
| 990 | - "invalid hex color '{}': {}. use format like 0xff0000 or #ff0000", | ||
| 991 | - s, e | ||
| 992 | - )) | ||
| 993 | - }) | ||
| 994 | - } else { | ||
| 995 | - s.parse::<u32>().map_err(|e| { | ||
| 996 | - mlua::Error::RuntimeError(format!( | ||
| 997 | - "invalid color '{}': {}. use hex format like 0xff0000 or #ff0000", | ||
| 998 | - s, e | ||
| 999 | - )) | ||
| 1000 | - }) | ||
| 1001 | - } | ||
| 1002 | - } | ||
| 1003 | - _ => Err(mlua::Error::RuntimeError( | ||
| 1004 | - "color must be a number (0xff0000) or string ('#ff0000' or '0xff0000')".into(), | ||
| 1005 | - )), | ||
| 1006 | - } | ||
| 1007 | -} | ||
| 1008 | - | ||
| 1009 | -fn create_block_config( | ||
| 1010 | - lua: &Lua, | ||
| 1011 | - config: Table, | ||
| 1012 | - block_type: &str, | ||
| 1013 | - arg: Option<Value>, | ||
| 1014 | -) -> mlua::Result<Table> { | ||
| 1015 | - let table = lua.create_table()?; | ||
| 1016 | - table.set("__block_type", block_type)?; | ||
| 1017 | - | ||
| 1018 | - let format: String = config.get("format").unwrap_or_default(); | ||
| 1019 | - let interval: u64 = config.get("interval")?; | ||
| 1020 | - let color: Value = config.get("color")?; | ||
| 1021 | - let underline: bool = config.get("underline").unwrap_or(false); | ||
| 1022 | - | ||
| 1023 | - table.set("format", format)?; | ||
| 1024 | - table.set("interval", interval)?; | ||
| 1025 | - table.set("color", color)?; | ||
| 1026 | - table.set("underline", underline)?; | ||
| 1027 | - | ||
| 1028 | - if let Some(arg_val) = arg { | ||
| 1029 | - table.set("__arg", arg_val)?; | ||
| 1030 | - } | ||
| 1031 | - | ||
| 1032 | - Ok(table) | ||
| 1033 | -} | ||
+0 −4 src/config/mod.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,4 +0,0 @@ | |||
| 1 | -mod lua; | ||
| 2 | -mod lua_api; | ||
| 3 | - | ||
| 4 | -pub use lua::parse_lua_config; | ||
+0 −221 src/errors.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,221 +0,0 @@ | |||
| 1 | -use std::io; | ||
| 2 | - | ||
| 3 | -#[derive(Debug)] | ||
| 4 | -pub enum WmError { | ||
| 5 | - X11(X11Error), | ||
| 6 | - Io(io::Error), | ||
| 7 | - Config(ConfigError), | ||
| 8 | - Block(BlockError), | ||
| 9 | - Autostart(String, io::Error), | ||
| 10 | -} | ||
| 11 | - | ||
| 12 | -#[derive(Debug)] | ||
| 13 | -pub enum X11Error { | ||
| 14 | - ConnectError(x11rb::errors::ConnectError), | ||
| 15 | - ConnectionError(x11rb::errors::ConnectionError), | ||
| 16 | - ReplyError(x11rb::errors::ReplyError), | ||
| 17 | - ReplyOrIdError(x11rb::errors::ReplyOrIdError), | ||
| 18 | - DisplayOpenFailed, | ||
| 19 | - FontLoadFailed(String), | ||
| 20 | - DrawCreateFailed, | ||
| 21 | -} | ||
| 22 | - | ||
| 23 | -#[derive(Debug)] | ||
| 24 | -pub enum ConfigError { | ||
| 25 | - LuaError(String), | ||
| 26 | - InvalidModkey(String), | ||
| 27 | - UnknownKey(String), | ||
| 28 | - UnknownAction(String), | ||
| 29 | - UnknownBlockCommand(String), | ||
| 30 | - MissingCommandArg { command: String, field: String }, | ||
| 31 | - ValidationError(String), | ||
| 32 | - NoConfigPathSet, | ||
| 33 | - NoConfigAtPath, | ||
| 34 | - CouldNotReadConfig(std::io::Error), | ||
| 35 | -} | ||
| 36 | - | ||
| 37 | -#[derive(Debug)] | ||
| 38 | -pub enum BlockError { | ||
| 39 | - Io(io::Error), | ||
| 40 | - ParseInt(std::num::ParseIntError), | ||
| 41 | - MissingFile(String), | ||
| 42 | - InvalidData(String), | ||
| 43 | - CommandFailed(String), | ||
| 44 | -} | ||
| 45 | - | ||
| 46 | -pub enum MainError { | ||
| 47 | - CouldNotCreateConfigDir(std::io::Error), | ||
| 48 | - CouldNotWriteConfig(std::io::Error), | ||
| 49 | - FailedCheckExist(std::io::Error), | ||
| 50 | - FailedReadConfig(std::io::Error), | ||
| 51 | - FailedReadConfigTemplate(ConfigError), | ||
| 52 | - CouldNotStartWm(WmError), | ||
| 53 | - WmError(WmError), | ||
| 54 | - BadConfigPath, | ||
| 55 | - NoConfigPath, | ||
| 56 | - InvalidArguments, | ||
| 57 | - NoProgramName, | ||
| 58 | - NoConfigDir, | ||
| 59 | -} | ||
| 60 | - | ||
| 61 | -impl std::fmt::Display for WmError { | ||
| 62 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 63 | - match self { | ||
| 64 | - Self::X11(error) => write!(f, "{}", error), | ||
| 65 | - Self::Io(error) => write!(f, "{}", error), | ||
| 66 | - Self::Config(error) => write!(f, "{}", error), | ||
| 67 | - Self::Block(error) => write!(f, "{}", error), | ||
| 68 | - Self::Autostart(command, error) => write!( | ||
| 69 | - f, | ||
| 70 | - "Failed to spawn autostart command '{}': {}", | ||
| 71 | - command, error | ||
| 72 | - ), | ||
| 73 | - } | ||
| 74 | - } | ||
| 75 | -} | ||
| 76 | - | ||
| 77 | -impl std::fmt::Display for X11Error { | ||
| 78 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 79 | - match self { | ||
| 80 | - Self::ConnectError(err) => write!(f, "{}", err), | ||
| 81 | - Self::ConnectionError(err) => write!(f, "{}", err), | ||
| 82 | - Self::ReplyError(err) => write!(f, "{}", err), | ||
| 83 | - Self::ReplyOrIdError(err) => write!(f, "{}", err), | ||
| 84 | - Self::DisplayOpenFailed => write!(f, "failed to open X11 display"), | ||
| 85 | - Self::FontLoadFailed(font_name) => write!(f, "failed to load Xft font: {}", font_name), | ||
| 86 | - Self::DrawCreateFailed => write!(f, "failed to create XftDraw"), | ||
| 87 | - } | ||
| 88 | - } | ||
| 89 | -} | ||
| 90 | - | ||
| 91 | -impl std::fmt::Display for ConfigError { | ||
| 92 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 93 | - match self { | ||
| 94 | - Self::LuaError(msg) => write!(f, "{}", msg), | ||
| 95 | - Self::InvalidModkey(msg) => write!(f, "{}", msg), | ||
| 96 | - Self::UnknownKey(msg) => write!(f, "{}", msg), | ||
| 97 | - Self::UnknownAction(msg) => write!(f, "{}", msg), | ||
| 98 | - Self::UnknownBlockCommand(msg) => write!(f, "{}", msg), | ||
| 99 | - Self::MissingCommandArg { command, field } => { | ||
| 100 | - write!(f, "{} command requires {}", command, field) | ||
| 101 | - } | ||
| 102 | - Self::ValidationError(msg) => write!(f, "{}", msg), | ||
| 103 | - Self::NoConfigPathSet => write!( | ||
| 104 | - f, | ||
| 105 | - "Could not find config file. Config path should've been set while loading" | ||
| 106 | - ), | ||
| 107 | - Self::NoConfigAtPath => write!(f, "Could not find config file, has it been moved?"), | ||
| 108 | - Self::CouldNotReadConfig(e) => write!(f, "Could not read config: {e}"), | ||
| 109 | - } | ||
| 110 | - } | ||
| 111 | -} | ||
| 112 | - | ||
| 113 | -impl std::fmt::Display for BlockError { | ||
| 114 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 115 | - match self { | ||
| 116 | - Self::Io(err) => write!(f, "Block I/O error: {}", err), | ||
| 117 | - Self::ParseInt(err) => write!(f, "Block parse error: {}", err), | ||
| 118 | - Self::MissingFile(path) => write!(f, "Block missing file: {}", path), | ||
| 119 | - Self::InvalidData(msg) => write!(f, "Block invalid data: {}", msg), | ||
| 120 | - Self::CommandFailed(msg) => write!(f, "Block command failed: {}", msg), | ||
| 121 | - } | ||
| 122 | - } | ||
| 123 | -} | ||
| 124 | - | ||
| 125 | -impl<T: Into<X11Error>> From<T> for WmError { | ||
| 126 | - fn from(value: T) -> Self { | ||
| 127 | - Self::X11(value.into()) | ||
| 128 | - } | ||
| 129 | -} | ||
| 130 | - | ||
| 131 | -impl From<io::Error> for WmError { | ||
| 132 | - fn from(value: io::Error) -> Self { | ||
| 133 | - Self::Io(value) | ||
| 134 | - } | ||
| 135 | -} | ||
| 136 | - | ||
| 137 | -impl From<ConfigError> for WmError { | ||
| 138 | - fn from(value: ConfigError) -> Self { | ||
| 139 | - Self::Config(value) | ||
| 140 | - } | ||
| 141 | -} | ||
| 142 | - | ||
| 143 | -impl From<BlockError> for WmError { | ||
| 144 | - fn from(value: BlockError) -> Self { | ||
| 145 | - Self::Block(value) | ||
| 146 | - } | ||
| 147 | -} | ||
| 148 | - | ||
| 149 | -impl From<io::Error> for BlockError { | ||
| 150 | - fn from(value: io::Error) -> Self { | ||
| 151 | - BlockError::Io(value) | ||
| 152 | - } | ||
| 153 | -} | ||
| 154 | - | ||
| 155 | -impl From<std::num::ParseIntError> for BlockError { | ||
| 156 | - fn from(value: std::num::ParseIntError) -> Self { | ||
| 157 | - BlockError::ParseInt(value) | ||
| 158 | - } | ||
| 159 | -} | ||
| 160 | - | ||
| 161 | -impl From<x11rb::errors::ConnectError> for X11Error { | ||
| 162 | - fn from(value: x11rb::errors::ConnectError) -> Self { | ||
| 163 | - X11Error::ConnectError(value) | ||
| 164 | - } | ||
| 165 | -} | ||
| 166 | - | ||
| 167 | -impl From<x11rb::errors::ConnectionError> for X11Error { | ||
| 168 | - fn from(value: x11rb::errors::ConnectionError) -> Self { | ||
| 169 | - X11Error::ConnectionError(value) | ||
| 170 | - } | ||
| 171 | -} | ||
| 172 | - | ||
| 173 | -impl From<x11rb::errors::ReplyError> for X11Error { | ||
| 174 | - fn from(value: x11rb::errors::ReplyError) -> Self { | ||
| 175 | - X11Error::ReplyError(value) | ||
| 176 | - } | ||
| 177 | -} | ||
| 178 | - | ||
| 179 | -impl From<x11rb::errors::ReplyOrIdError> for X11Error { | ||
| 180 | - fn from(value: x11rb::errors::ReplyOrIdError) -> Self { | ||
| 181 | - X11Error::ReplyOrIdError(value) | ||
| 182 | - } | ||
| 183 | -} | ||
| 184 | - | ||
| 185 | -impl From<mlua::Error> for ConfigError { | ||
| 186 | - fn from(err: mlua::Error) -> Self { | ||
| 187 | - ConfigError::LuaError(err.to_string()) | ||
| 188 | - } | ||
| 189 | -} | ||
| 190 | - | ||
| 191 | -pub trait LuaResultExt<T> { | ||
| 192 | - fn lua_context(self, context: &str) -> Result<T, ConfigError>; | ||
| 193 | -} | ||
| 194 | - | ||
| 195 | -impl<T> LuaResultExt<T> for Result<T, mlua::Error> { | ||
| 196 | - fn lua_context(self, context: &str) -> Result<T, ConfigError> { | ||
| 197 | - self.map_err(|e| ConfigError::LuaError(format!("{}: {}", context, e))) | ||
| 198 | - } | ||
| 199 | -} | ||
| 200 | - | ||
| 201 | -impl std::fmt::Debug for MainError { | ||
| 202 | - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
| 203 | - use MainError::*; | ||
| 204 | - | ||
| 205 | - match self { | ||
| 206 | - CouldNotCreateConfigDir(e) | ||
| 207 | - | CouldNotWriteConfig(e) | ||
| 208 | - | FailedCheckExist(e) | ||
| 209 | - | FailedReadConfig(e) => { | ||
| 210 | - write!(f, "{e}") | ||
| 211 | - } | ||
| 212 | - FailedReadConfigTemplate(e) => write!(f, "{e}"), | ||
| 213 | - CouldNotStartWm(e) | WmError(e) => write!(f, "{e}"), | ||
| 214 | - BadConfigPath => write!(f, "Given config path does not exist"), | ||
| 215 | - NoConfigPath => write!(f, "The --config switch requires a path value"), | ||
| 216 | - InvalidArguments => write!(f, "The arguments given are invalid try --help"), | ||
| 217 | - NoProgramName => write!(f, "Could not get the program name from the environment"), | ||
| 218 | - NoConfigDir => write!(f, "Could not get the config dir"), | ||
| 219 | - } | ||
| 220 | - } | ||
| 221 | -} | ||
+0 −370 src/keyboard/handlers.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,370 +0,0 @@ | |||
| 1 | -use std::io::Result; | ||
| 2 | - | ||
| 3 | -use serde::Deserialize; | ||
| 4 | -use x11rb::connection::Connection; | ||
| 5 | -use x11rb::protocol::xproto::*; | ||
| 6 | - | ||
| 7 | -use crate::errors::X11Error; | ||
| 8 | -use crate::keyboard::keysyms::{self, Keysym, format_keysym}; | ||
| 9 | - | ||
| 10 | -/// When adding a new action, update: | ||
| 11 | -/// 1. Add variant here | ||
| 12 | -/// 2. lua_api.rs: string_to_action() | ||
| 13 | -/// 3. lua_api.rs: register_*_module() | ||
| 14 | -/// 4. window_manager.rs: handle_key_action() | ||
| 15 | -/// 5. (optionally) overlay/keybind.rs: action_description() | ||
| 16 | -/// 6. templates/oxwm.lua | ||
| 17 | -#[derive(Debug, Copy, Clone, Deserialize, PartialEq)] | ||
| 18 | -pub enum KeyAction { | ||
| 19 | - Spawn, | ||
| 20 | - SpawnTerminal, | ||
| 21 | - KillClient, | ||
| 22 | - FocusStack, | ||
| 23 | - MoveStack, | ||
| 24 | - Quit, | ||
| 25 | - Restart, | ||
| 26 | - ViewTag, | ||
| 27 | - ViewNextTag, | ||
| 28 | - ViewPreviousTag, | ||
| 29 | - ViewNextNonEmptyTag, | ||
| 30 | - ViewPreviousNonEmptyTag, | ||
| 31 | - ToggleView, | ||
| 32 | - MoveToTag, | ||
| 33 | - ToggleTag, | ||
| 34 | - ToggleGaps, | ||
| 35 | - ToggleFullScreen, | ||
| 36 | - ToggleFloating, | ||
| 37 | - ChangeLayout, | ||
| 38 | - CycleLayout, | ||
| 39 | - FocusMonitor, | ||
| 40 | - TagMonitor, | ||
| 41 | - ShowKeybindOverlay, | ||
| 42 | - SetMasterFactor, | ||
| 43 | - IncNumMaster, | ||
| 44 | - ScrollLeft, | ||
| 45 | - ScrollRight, | ||
| 46 | - None, | ||
| 47 | -} | ||
| 48 | - | ||
| 49 | -#[derive(Debug, Clone)] | ||
| 50 | -pub enum Arg { | ||
| 51 | - None, | ||
| 52 | - Int(i32), | ||
| 53 | - Str(String), | ||
| 54 | - Array(Vec<String>), | ||
| 55 | -} | ||
| 56 | - | ||
| 57 | -impl Arg { | ||
| 58 | - pub const fn none() -> Self { | ||
| 59 | - Arg::None | ||
| 60 | - } | ||
| 61 | -} | ||
| 62 | - | ||
| 63 | -#[derive(Clone)] | ||
| 64 | -pub struct KeyPress { | ||
| 65 | - pub(crate) modifiers: Vec<KeyButMask>, | ||
| 66 | - pub(crate) keysym: Keysym, | ||
| 67 | -} | ||
| 68 | - | ||
| 69 | -impl std::fmt::Debug for KeyPress { | ||
| 70 | - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| 71 | - f.debug_struct("KeyPress") | ||
| 72 | - .field("modifiers", &self.modifiers) | ||
| 73 | - .field("keysym", &format_keysym(self.keysym)) | ||
| 74 | - .finish() | ||
| 75 | - } | ||
| 76 | -} | ||
| 77 | - | ||
| 78 | -#[derive(Debug, Clone)] | ||
| 79 | -pub struct KeyBinding { | ||
| 80 | - pub(crate) keys: Vec<KeyPress>, | ||
| 81 | - pub(crate) func: KeyAction, | ||
| 82 | - pub(crate) arg: Arg, | ||
| 83 | -} | ||
| 84 | - | ||
| 85 | -impl KeyBinding { | ||
| 86 | - pub fn new(keys: Vec<KeyPress>, func: KeyAction, arg: Arg) -> Self { | ||
| 87 | - Self { keys, func, arg } | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - pub fn single_key( | ||
| 91 | - modifiers: Vec<KeyButMask>, | ||
| 92 | - keysym: Keysym, | ||
| 93 | - func: KeyAction, | ||
| 94 | - arg: Arg, | ||
| 95 | - ) -> Self { | ||
| 96 | - Self { | ||
| 97 | - keys: vec![KeyPress { modifiers, keysym }], | ||
| 98 | - func, | ||
| 99 | - arg, | ||
| 100 | - } | ||
| 101 | - } | ||
| 102 | -} | ||
| 103 | - | ||
| 104 | -pub type Key = KeyBinding; | ||
| 105 | - | ||
| 106 | -#[derive(Debug, Clone)] | ||
| 107 | -pub enum KeychordState { | ||
| 108 | - Idle, | ||
| 109 | - InProgress { | ||
| 110 | - candidates: Vec<usize>, | ||
| 111 | - keys_pressed: usize, | ||
| 112 | - }, | ||
| 113 | -} | ||
| 114 | - | ||
| 115 | -pub enum KeychordResult { | ||
| 116 | - Completed(KeyAction, Arg), | ||
| 117 | - InProgress(Vec<usize>), | ||
| 118 | - None, | ||
| 119 | - Cancelled, | ||
| 120 | -} | ||
| 121 | - | ||
| 122 | -pub fn modifiers_to_mask(modifiers: &[KeyButMask]) -> u16 { | ||
| 123 | - modifiers | ||
| 124 | - .iter() | ||
| 125 | - .fold(0u16, |acc, &modifier| acc | u16::from(modifier)) | ||
| 126 | -} | ||
| 127 | - | ||
| 128 | -pub struct KeyboardMapping { | ||
| 129 | - pub syms: Vec<Keysym>, | ||
| 130 | - pub keysyms_per_keycode: u8, | ||
| 131 | - pub min_keycode: Keycode, | ||
| 132 | -} | ||
| 133 | - | ||
| 134 | -impl KeyboardMapping { | ||
| 135 | - pub fn keycode_to_keysym(&self, keycode: Keycode) -> Keysym { | ||
| 136 | - if keycode < self.min_keycode { | ||
| 137 | - return 0; | ||
| 138 | - } | ||
| 139 | - let index = (keycode - self.min_keycode) as usize * self.keysyms_per_keycode as usize; | ||
| 140 | - self.syms.get(index).copied().unwrap_or(0) | ||
| 141 | - } | ||
| 142 | - | ||
| 143 | - pub fn find_keycode( | ||
| 144 | - &self, | ||
| 145 | - keysym: Keysym, | ||
| 146 | - min_keycode: Keycode, | ||
| 147 | - max_keycode: Keycode, | ||
| 148 | - ) -> Option<Keycode> { | ||
| 149 | - for keycode in min_keycode..=max_keycode { | ||
| 150 | - let index = (keycode - self.min_keycode) as usize * self.keysyms_per_keycode as usize; | ||
| 151 | - if let Some(&sym) = self.syms.get(index) | ||
| 152 | - && sym == keysym | ||
| 153 | - { | ||
| 154 | - return Some(keycode); | ||
| 155 | - } | ||
| 156 | - } | ||
| 157 | - None | ||
| 158 | - } | ||
| 159 | -} | ||
| 160 | - | ||
| 161 | -pub fn get_keyboard_mapping( | ||
| 162 | - connection: &impl Connection, | ||
| 163 | -) -> std::result::Result<KeyboardMapping, X11Error> { | ||
| 164 | - let setup = connection.setup(); | ||
| 165 | - let min_keycode = setup.min_keycode; | ||
| 166 | - let max_keycode = setup.max_keycode; | ||
| 167 | - | ||
| 168 | - let mapping = connection | ||
| 169 | - .get_keyboard_mapping(min_keycode, max_keycode - min_keycode + 1)? | ||
| 170 | - .reply()?; | ||
| 171 | - | ||
| 172 | - Ok(KeyboardMapping { | ||
| 173 | - syms: mapping.keysyms, | ||
| 174 | - keysyms_per_keycode: mapping.keysyms_per_keycode, | ||
| 175 | - min_keycode, | ||
| 176 | - }) | ||
| 177 | -} | ||
| 178 | - | ||
| 179 | -pub fn grab_keys( | ||
| 180 | - connection: &impl Connection, | ||
| 181 | - root: Window, | ||
| 182 | - keybindings: &[KeyBinding], | ||
| 183 | - current_key: usize, | ||
| 184 | -) -> std::result::Result<KeyboardMapping, X11Error> { | ||
| 185 | - let setup = connection.setup(); | ||
| 186 | - let min_keycode = setup.min_keycode; | ||
| 187 | - let max_keycode = setup.max_keycode; | ||
| 188 | - | ||
| 189 | - let mapping = get_keyboard_mapping(connection)?; | ||
| 190 | - | ||
| 191 | - connection.ungrab_key(x11rb::protocol::xproto::Grab::ANY, root, ModMask::ANY)?; | ||
| 192 | - | ||
| 193 | - let modifiers = [ | ||
| 194 | - 0u16, | ||
| 195 | - u16::from(ModMask::LOCK), | ||
| 196 | - u16::from(ModMask::M2), | ||
| 197 | - u16::from(ModMask::LOCK | ModMask::M2), | ||
| 198 | - ]; | ||
| 199 | - | ||
| 200 | - for keycode in min_keycode..=max_keycode { | ||
| 201 | - for keybinding in keybindings { | ||
| 202 | - if current_key >= keybinding.keys.len() { | ||
| 203 | - continue; | ||
| 204 | - } | ||
| 205 | - | ||
| 206 | - let key = &keybinding.keys[current_key]; | ||
| 207 | - if key.keysym == mapping.keycode_to_keysym(keycode) { | ||
| 208 | - let modifier_mask = modifiers_to_mask(&key.modifiers); | ||
| 209 | - for &ignore_mask in &modifiers { | ||
| 210 | - connection.grab_key( | ||
| 211 | - true, | ||
| 212 | - root, | ||
| 213 | - (modifier_mask | ignore_mask).into(), | ||
| 214 | - keycode, | ||
| 215 | - GrabMode::ASYNC, | ||
| 216 | - GrabMode::ASYNC, | ||
| 217 | - )?; | ||
| 218 | - } | ||
| 219 | - } | ||
| 220 | - } | ||
| 221 | - } | ||
| 222 | - | ||
| 223 | - if current_key > 0 | ||
| 224 | - && let Some(escape_keycode) = | ||
| 225 | - mapping.find_keycode(keysyms::XK_ESCAPE, min_keycode, max_keycode) | ||
| 226 | - { | ||
| 227 | - connection.grab_key( | ||
| 228 | - true, | ||
| 229 | - root, | ||
| 230 | - ModMask::ANY, | ||
| 231 | - escape_keycode, | ||
| 232 | - GrabMode::ASYNC, | ||
| 233 | - GrabMode::ASYNC, | ||
| 234 | - )?; | ||
| 235 | - } | ||
| 236 | - | ||
| 237 | - connection.flush()?; | ||
| 238 | - Ok(mapping) | ||
| 239 | -} | ||
| 240 | - | ||
| 241 | -pub fn handle_key_press( | ||
| 242 | - event: KeyPressEvent, | ||
| 243 | - keybindings: &[KeyBinding], | ||
| 244 | - keychord_state: &KeychordState, | ||
| 245 | - mapping: &KeyboardMapping, | ||
| 246 | -) -> KeychordResult { | ||
| 247 | - let keysym = mapping.keycode_to_keysym(event.detail); | ||
| 248 | - | ||
| 249 | - if keysym == keysyms::XK_ESCAPE { | ||
| 250 | - return match keychord_state { | ||
| 251 | - KeychordState::InProgress { .. } => KeychordResult::Cancelled, | ||
| 252 | - KeychordState::Idle => KeychordResult::None, | ||
| 253 | - }; | ||
| 254 | - } | ||
| 255 | - | ||
| 256 | - match keychord_state { | ||
| 257 | - KeychordState::Idle => handle_first_key(event, keysym, keybindings), | ||
| 258 | - KeychordState::InProgress { | ||
| 259 | - candidates, | ||
| 260 | - keys_pressed, | ||
| 261 | - } => handle_next_key(event, keysym, keybindings, candidates, *keys_pressed), | ||
| 262 | - } | ||
| 263 | -} | ||
| 264 | - | ||
| 265 | -fn handle_first_key( | ||
| 266 | - event: KeyPressEvent, | ||
| 267 | - event_keysym: Keysym, | ||
| 268 | - keybindings: &[KeyBinding], | ||
| 269 | -) -> KeychordResult { | ||
| 270 | - let mut candidates = Vec::new(); | ||
| 271 | - | ||
| 272 | - let clean_state = event.state & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2)); | ||
| 273 | - | ||
| 274 | - for (keybinding_index, keybinding) in keybindings.iter().enumerate() { | ||
| 275 | - if keybinding.keys.is_empty() { | ||
| 276 | - continue; | ||
| 277 | - } | ||
| 278 | - | ||
| 279 | - let first_key = &keybinding.keys[0]; | ||
| 280 | - let modifier_mask = modifiers_to_mask(&first_key.modifiers); | ||
| 281 | - | ||
| 282 | - if event_keysym == first_key.keysym && clean_state == modifier_mask.into() { | ||
| 283 | - if keybinding.keys.len() == 1 { | ||
| 284 | - return KeychordResult::Completed(keybinding.func, keybinding.arg.clone()); | ||
| 285 | - } else { | ||
| 286 | - candidates.push(keybinding_index); | ||
| 287 | - } | ||
| 288 | - } | ||
| 289 | - } | ||
| 290 | - | ||
| 291 | - if candidates.is_empty() { | ||
| 292 | - KeychordResult::None | ||
| 293 | - } else { | ||
| 294 | - KeychordResult::InProgress(candidates) | ||
| 295 | - } | ||
| 296 | -} | ||
| 297 | - | ||
| 298 | -fn handle_next_key( | ||
| 299 | - event: KeyPressEvent, | ||
| 300 | - event_keysym: Keysym, | ||
| 301 | - keybindings: &[KeyBinding], | ||
| 302 | - candidates: &[usize], | ||
| 303 | - keys_pressed: usize, | ||
| 304 | -) -> KeychordResult { | ||
| 305 | - let mut new_candidates = Vec::new(); | ||
| 306 | - | ||
| 307 | - let clean_state = event.state & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2)); | ||
| 308 | - | ||
| 309 | - for &candidate_index in candidates { | ||
| 310 | - let keybinding = &keybindings[candidate_index]; | ||
| 311 | - | ||
| 312 | - if keys_pressed >= keybinding.keys.len() { | ||
| 313 | - continue; | ||
| 314 | - } | ||
| 315 | - | ||
| 316 | - let next_key = &keybinding.keys[keys_pressed]; | ||
| 317 | - let required_mask = modifiers_to_mask(&next_key.modifiers); | ||
| 318 | - | ||
| 319 | - let modifiers_match = if next_key.modifiers.is_empty() { | ||
| 320 | - true | ||
| 321 | - } else { | ||
| 322 | - (clean_state & required_mask) == required_mask.into() | ||
| 323 | - }; | ||
| 324 | - | ||
| 325 | - if event_keysym == next_key.keysym && modifiers_match { | ||
| 326 | - if keys_pressed + 1 == keybinding.keys.len() { | ||
| 327 | - return KeychordResult::Completed(keybinding.func, keybinding.arg.clone()); | ||
| 328 | - } else { | ||
| 329 | - new_candidates.push(candidate_index); | ||
| 330 | - } | ||
| 331 | - } | ||
| 332 | - } | ||
| 333 | - | ||
| 334 | - if new_candidates.is_empty() { | ||
| 335 | - KeychordResult::Cancelled | ||
| 336 | - } else { | ||
| 337 | - KeychordResult::InProgress(new_candidates) | ||
| 338 | - } | ||
| 339 | -} | ||
| 340 | - | ||
| 341 | -pub fn handle_spawn_action(action: KeyAction, arg: &Arg, selected_monitor: usize) -> Result<()> { | ||
| 342 | - if let KeyAction::Spawn = action { | ||
| 343 | - match arg { | ||
| 344 | - Arg::Str(command) => { | ||
| 345 | - crate::signal::spawn_detached(command); | ||
| 346 | - } | ||
| 347 | - Arg::Array(command) => { | ||
| 348 | - let Some((cmd, args)) = command.split_first() else { | ||
| 349 | - return Ok(()); | ||
| 350 | - }; | ||
| 351 | - | ||
| 352 | - let mut args_vec: Vec<String> = args.to_vec(); | ||
| 353 | - | ||
| 354 | - let is_dmenu = cmd.contains("dmenu"); | ||
| 355 | - let has_monitor_flag = args.iter().any(|arg| arg == "-m"); | ||
| 356 | - | ||
| 357 | - if is_dmenu && !has_monitor_flag { | ||
| 358 | - args_vec.insert(0, selected_monitor.to_string()); | ||
| 359 | - args_vec.insert(0, "-m".to_string()); | ||
| 360 | - } | ||
| 361 | - | ||
| 362 | - let args_str: Vec<&str> = args_vec.iter().map(|s| s.as_str()).collect(); | ||
| 363 | - crate::signal::spawn_detached_with_args(cmd, &args_str); | ||
| 364 | - } | ||
| 365 | - _ => {} | ||
| 366 | - } | ||
| 367 | - } | ||
| 368 | - | ||
| 369 | - Ok(()) | ||
| 370 | -} | ||
+0 −422 src/keyboard/keysyms.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,422 +0,0 @@ | |||
| 1 | -#![allow(dead_code)] | ||
| 2 | - | ||
| 3 | -pub type Keysym = u32; | ||
| 4 | -pub const XK_ESCAPE: Keysym = 0xff1b; | ||
| 5 | -pub const XK_RETURN: Keysym = 0xff0d; | ||
| 6 | -pub const XK_SPACE: Keysym = 0x0020; | ||
| 7 | -pub const XK_TAB: Keysym = 0xff09; | ||
| 8 | -pub const XK_BACKSPACE: Keysym = 0xff08; | ||
| 9 | -pub const XK_DELETE: Keysym = 0xffff; | ||
| 10 | -pub const XK_F1: Keysym = 0xffbe; | ||
| 11 | -pub const XK_F2: Keysym = 0xffbf; | ||
| 12 | -pub const XK_F3: Keysym = 0xffc0; | ||
| 13 | -pub const XK_F4: Keysym = 0xffc1; | ||
| 14 | -pub const XK_F5: Keysym = 0xffc2; | ||
| 15 | -pub const XK_F6: Keysym = 0xffc3; | ||
| 16 | -pub const XK_F7: Keysym = 0xffc4; | ||
| 17 | -pub const XK_F8: Keysym = 0xffc5; | ||
| 18 | -pub const XK_F9: Keysym = 0xffc6; | ||
| 19 | -pub const XK_F10: Keysym = 0xffc7; | ||
| 20 | -pub const XK_F11: Keysym = 0xffc8; | ||
| 21 | -pub const XK_F12: Keysym = 0xffc9; | ||
| 22 | -pub const XK_A: Keysym = 0x0061; | ||
| 23 | -pub const XK_B: Keysym = 0x0062; | ||
| 24 | -pub const XK_C: Keysym = 0x0063; | ||
| 25 | -pub const XK_D: Keysym = 0x0064; | ||
| 26 | -pub const XK_E: Keysym = 0x0065; | ||
| 27 | -pub const XK_F: Keysym = 0x0066; | ||
| 28 | -pub const XK_G: Keysym = 0x0067; | ||
| 29 | -pub const XK_H: Keysym = 0x0068; | ||
| 30 | -pub const XK_I: Keysym = 0x0069; | ||
| 31 | -pub const XK_J: Keysym = 0x006a; | ||
| 32 | -pub const XK_K: Keysym = 0x006b; | ||
| 33 | -pub const XK_L: Keysym = 0x006c; | ||
| 34 | -pub const XK_M: Keysym = 0x006d; | ||
| 35 | -pub const XK_N: Keysym = 0x006e; | ||
| 36 | -pub const XK_O: Keysym = 0x006f; | ||
| 37 | -pub const XK_P: Keysym = 0x0070; | ||
| 38 | -pub const XK_Q: Keysym = 0x0071; | ||
| 39 | -pub const XK_R: Keysym = 0x0072; | ||
| 40 | -pub const XK_S: Keysym = 0x0073; | ||
| 41 | -pub const XK_T: Keysym = 0x0074; | ||
| 42 | -pub const XK_U: Keysym = 0x0075; | ||
| 43 | -pub const XK_V: Keysym = 0x0076; | ||
| 44 | -pub const XK_W: Keysym = 0x0077; | ||
| 45 | -pub const XK_X: Keysym = 0x0078; | ||
| 46 | -pub const XK_Y: Keysym = 0x0079; | ||
| 47 | -pub const XK_Z: Keysym = 0x007a; | ||
| 48 | -pub const XK_0: Keysym = 0x0030; | ||
| 49 | -pub const XK_1: Keysym = 0x0031; | ||
| 50 | -pub const XK_2: Keysym = 0x0032; | ||
| 51 | -pub const XK_3: Keysym = 0x0033; | ||
| 52 | -pub const XK_4: Keysym = 0x0034; | ||
| 53 | -pub const XK_5: Keysym = 0x0035; | ||
| 54 | -pub const XK_6: Keysym = 0x0036; | ||
| 55 | -pub const XK_7: Keysym = 0x0037; | ||
| 56 | -pub const XK_8: Keysym = 0x0038; | ||
| 57 | -pub const XK_9: Keysym = 0x0039; | ||
| 58 | -pub const XK_LEFT: Keysym = 0xff51; | ||
| 59 | -pub const XK_UP: Keysym = 0xff52; | ||
| 60 | -pub const XK_RIGHT: Keysym = 0xff53; | ||
| 61 | -pub const XK_DOWN: Keysym = 0xff54; | ||
| 62 | -pub const XK_HOME: Keysym = 0xff50; | ||
| 63 | -pub const XK_END: Keysym = 0xff57; | ||
| 64 | -pub const XK_PAGE_UP: Keysym = 0xff55; | ||
| 65 | -pub const XK_PAGE_DOWN: Keysym = 0xff56; | ||
| 66 | -pub const XK_INSERT: Keysym = 0xff63; | ||
| 67 | -pub const XK_MINUS: Keysym = 0x002d; | ||
| 68 | -pub const XK_EQUAL: Keysym = 0x003d; | ||
| 69 | -pub const XK_LEFT_BRACKET: Keysym = 0x005b; | ||
| 70 | -pub const XK_RIGHT_BRACKET: Keysym = 0x005d; | ||
| 71 | -pub const XK_SEMICOLON: Keysym = 0x003b; | ||
| 72 | -pub const XK_QUESTION: Keysym = 0x003f; | ||
| 73 | -pub const XK_APOSTROPHE: Keysym = 0x0027; | ||
| 74 | -pub const XK_GRAVE: Keysym = 0x0060; | ||
| 75 | -pub const XK_BACKSLASH: Keysym = 0x005c; | ||
| 76 | -pub const XK_COMMA: Keysym = 0x002c; | ||
| 77 | -pub const XK_PERIOD: Keysym = 0x002e; | ||
| 78 | -pub const XK_SLASH: Keysym = 0x002f; | ||
| 79 | -pub const XK_PRINT: Keysym = 0xff61; | ||
| 80 | -pub const XK_AMPERSAND: Keysym = 0x26; | ||
| 81 | -pub const XK_AGRAVE: Keysym = 0xe0; | ||
| 82 | -pub const XK_CCEDILLA: Keysym = 0xe7; | ||
| 83 | -pub const XK_EGRAVE: Keysym = 0xe8; | ||
| 84 | -pub const XK_EACUTE: Keysym = 0xe9; | ||
| 85 | -pub const XK_PARENLEFT: Keysym = 0x28; | ||
| 86 | -pub const XK_PARENRIGHT: Keysym = 0x29; | ||
| 87 | -pub const XK_QUOTEDBL: Keysym = 0x22; | ||
| 88 | -pub const XK_UNDERSCORE: Keysym = 0x5f; | ||
| 89 | -pub const XK_HYPHEN: Keysym = 0xad; | ||
| 90 | -pub const XF86_AUDIO_MEDIA: Keysym = 0x1008ff32; | ||
| 91 | -pub const XF86_AUDIO_NEXT: Keysym = 0x1008ff17; | ||
| 92 | -pub const XF86_AUDIO_PAUSE: Keysym = 0x1008ff31; | ||
| 93 | -pub const XF86_AUDIO_PLAY: Keysym = 0x1008ff14; | ||
| 94 | -pub const XF86_AUDIO_PREV: Keysym = 0x1008ff16; | ||
| 95 | -pub const XF86_AUDIO_RAISE_VOLUME: Keysym = 0x1008ff13; | ||
| 96 | -pub const XF86_AUDIO_LOWER_VOLUME: Keysym = 0x1008ff11; | ||
| 97 | -pub const XF86_AUDIO_MUTE: Keysym = 0x1008ff12; | ||
| 98 | -pub const XF86_AUDIO_MIC_MUTE: Keysym = 0x1008ffb2; | ||
| 99 | -pub const XF86_AUDIO_RECORD: Keysym = 0x1008ff1c; | ||
| 100 | -pub const XF86_AUDIO_REWIND: Keysym = 0x1008ff3e; | ||
| 101 | -pub const XF86_AUDIO_STOP: Keysym = 0x1008ff15; | ||
| 102 | -pub const XF86_BACK: Keysym = 0x1008ff26; | ||
| 103 | -pub const XF86_CALCULATOR: Keysym = 0x1008ff1d; | ||
| 104 | -pub const XF86_CLOSE: Keysym = 0x1008ff56; | ||
| 105 | -pub const XF86_COPY: Keysym = 0x1008ff57; | ||
| 106 | -pub const XF86_CUT: Keysym = 0x1008ff58; | ||
| 107 | -pub const XF86_DOS: Keysym = 0x1008ff5a; | ||
| 108 | -pub const XF86_DISPLAY: Keysym = 0x1008ff59; | ||
| 109 | -pub const XF86_DOCUMENTS: Keysym = 0x1008ff5b; | ||
| 110 | -pub const XF86_EJECT: Keysym = 0x1008ff2c; | ||
| 111 | -pub const XF86_EXPLORER: Keysym = 0x1008ff5d; | ||
| 112 | -pub const XF86_FAVORITES: Keysym = 0x1008ff30; | ||
| 113 | -pub const XF86_FINANCE: Keysym = 0x1008ff3c; | ||
| 114 | -pub const XF86_FORWARD: Keysym = 0x1008ff27; | ||
| 115 | -pub const XF86_GAME: Keysym = 0x1008ff5e; | ||
| 116 | -pub const XF86_GO: Keysym = 0x1008ff5f; | ||
| 117 | -pub const XF86_HOME_PAGE: Keysym = 0x1008ff18; | ||
| 118 | -pub const XF86_MAIL: Keysym = 0x1008ff19; | ||
| 119 | -pub const XF86_MAIL_FORWARD: Keysym = 0x1008ff90; | ||
| 120 | -pub const XF86_MENU_KB: Keysym = 0x1008ff65; | ||
| 121 | -pub const XF86_MESSENGER: Keysym = 0x1008ff8e; | ||
| 122 | -pub const XF86_MON_BRIGHTNESS_UP: Keysym = 0x1008ff02; | ||
| 123 | -pub const XF86_MON_BRIGHTNESS_DOWN: Keysym = 0x1008ff03; | ||
| 124 | -pub const XF86_MY_COMPUTER: Keysym = 0x1008ff33; | ||
| 125 | -pub const XF86_NEW: Keysym = 0x1008ff68; | ||
| 126 | -pub const XF86_NEXT_VMODE: Keysym = 0x1008fe22; | ||
| 127 | -pub const XF86_PREV_VMODE: Keysym = 0x1008fe23; | ||
| 128 | -pub const XF86_OPEN: Keysym = 0x1008ff6b; | ||
| 129 | -pub const XF86_PASTE: Keysym = 0x1008ff6d; | ||
| 130 | -pub const XF86_PHONE: Keysym = 0x1008ff6e; | ||
| 131 | -pub const XF86_POWER_OFF: Keysym = 0x1008ff2a; | ||
| 132 | -pub const XF86_RELOAD: Keysym = 0x1008ff73; | ||
| 133 | -pub const XF86_REPLY: Keysym = 0x1008ff72; | ||
| 134 | -pub const XF86_ROTATE_WINDOWS: Keysym = 0x1008ff74; | ||
| 135 | -pub const XF86_SAVE: Keysym = 0x1008ff77; | ||
| 136 | -pub const XF86_SCREEN_SAVER: Keysym = 0x1008ff2d; | ||
| 137 | -pub const XF86_SCROLL_DOWN: Keysym = 0x1008ff79; | ||
| 138 | -pub const XF86_SCROLL_UP: Keysym = 0x1008ff78; | ||
| 139 | -pub const XF86_SEARCH: Keysym = 0x1008ff1b; | ||
| 140 | -pub const XF86_SEND: Keysym = 0x1008ff7b; | ||
| 141 | -pub const XF86_SHOP: Keysym = 0x1008ff36; | ||
| 142 | -pub const XF86_SLEEP: Keysym = 0x1008ff2f; | ||
| 143 | -pub const XF86_TASK_PANE: Keysym = 0x1008ff7f; | ||
| 144 | -pub const XF86_TOOLS: Keysym = 0x1008ff81; | ||
| 145 | -pub const XF86_WWW: Keysym = 0x1008ff2e; | ||
| 146 | -pub const XF86_WAKE_UP: Keysym = 0x1008ff2b; | ||
| 147 | -pub const XF86_WEBCAM: Keysym = 0x1008ff8f; | ||
| 148 | -pub const XF86_XFER: Keysym = 0x1008ff8a; | ||
| 149 | - | ||
| 150 | -pub fn keysym_from_str(s: &str) -> Option<Keysym> { | ||
| 151 | - match s { | ||
| 152 | - "Return" => Some(XK_RETURN), | ||
| 153 | - "Escape" => Some(XK_ESCAPE), | ||
| 154 | - "Space" => Some(XK_SPACE), | ||
| 155 | - "Tab" => Some(XK_TAB), | ||
| 156 | - "Backspace" => Some(XK_BACKSPACE), | ||
| 157 | - "Delete" => Some(XK_DELETE), | ||
| 158 | - "F1" => Some(XK_F1), | ||
| 159 | - "F2" => Some(XK_F2), | ||
| 160 | - "F3" => Some(XK_F3), | ||
| 161 | - "F4" => Some(XK_F4), | ||
| 162 | - "F5" => Some(XK_F5), | ||
| 163 | - "F6" => Some(XK_F6), | ||
| 164 | - "F7" => Some(XK_F7), | ||
| 165 | - "F8" => Some(XK_F8), | ||
| 166 | - "F9" => Some(XK_F9), | ||
| 167 | - "F10" => Some(XK_F10), | ||
| 168 | - "F11" => Some(XK_F11), | ||
| 169 | - "F12" => Some(XK_F12), | ||
| 170 | - "A" => Some(XK_A), | ||
| 171 | - "B" => Some(XK_B), | ||
| 172 | - "C" => Some(XK_C), | ||
| 173 | - "D" => Some(XK_D), | ||
| 174 | - "E" => Some(XK_E), | ||
| 175 | - "F" => Some(XK_F), | ||
| 176 | - "G" => Some(XK_G), | ||
| 177 | - "H" => Some(XK_H), | ||
| 178 | - "I" => Some(XK_I), | ||
| 179 | - "J" => Some(XK_J), | ||
| 180 | - "K" => Some(XK_K), | ||
| 181 | - "L" => Some(XK_L), | ||
| 182 | - "M" => Some(XK_M), | ||
| 183 | - "N" => Some(XK_N), | ||
| 184 | - "O" => Some(XK_O), | ||
| 185 | - "P" => Some(XK_P), | ||
| 186 | - "Q" => Some(XK_Q), | ||
| 187 | - "R" => Some(XK_R), | ||
| 188 | - "S" => Some(XK_S), | ||
| 189 | - "T" => Some(XK_T), | ||
| 190 | - "U" => Some(XK_U), | ||
| 191 | - "V" => Some(XK_V), | ||
| 192 | - "W" => Some(XK_W), | ||
| 193 | - "X" => Some(XK_X), | ||
| 194 | - "Y" => Some(XK_Y), | ||
| 195 | - "Z" => Some(XK_Z), | ||
| 196 | - "0" => Some(XK_0), | ||
| 197 | - "1" => Some(XK_1), | ||
| 198 | - "2" => Some(XK_2), | ||
| 199 | - "3" => Some(XK_3), | ||
| 200 | - "4" => Some(XK_4), | ||
| 201 | - "5" => Some(XK_5), | ||
| 202 | - "6" => Some(XK_6), | ||
| 203 | - "7" => Some(XK_7), | ||
| 204 | - "8" => Some(XK_8), | ||
| 205 | - "9" => Some(XK_9), | ||
| 206 | - "Left" => Some(XK_LEFT), | ||
| 207 | - "Right" => Some(XK_RIGHT), | ||
| 208 | - "Up" => Some(XK_UP), | ||
| 209 | - "Down" => Some(XK_DOWN), | ||
| 210 | - "Home" => Some(XK_HOME), | ||
| 211 | - "End" => Some(XK_END), | ||
| 212 | - "PageUp" => Some(XK_PAGE_UP), | ||
| 213 | - "PageDown" => Some(XK_PAGE_DOWN), | ||
| 214 | - "Insert" => Some(XK_INSERT), | ||
| 215 | - "Minus" => Some(XK_MINUS), | ||
| 216 | - "Equal" => Some(XK_EQUAL), | ||
| 217 | - "BracketLeft" => Some(XK_LEFT_BRACKET), | ||
| 218 | - "BracketRight" => Some(XK_RIGHT_BRACKET), | ||
| 219 | - "Semicolon" => Some(XK_SEMICOLON), | ||
| 220 | - "Question" => Some(XK_QUESTION), | ||
| 221 | - "Apostrophe" => Some(XK_APOSTROPHE), | ||
| 222 | - "Grave" => Some(XK_GRAVE), | ||
| 223 | - "Backslash" => Some(XK_BACKSLASH), | ||
| 224 | - "Comma" => Some(XK_COMMA), | ||
| 225 | - "Period" => Some(XK_PERIOD), | ||
| 226 | - "Slash" => Some(XK_SLASH), | ||
| 227 | - "Print" => Some(XK_PRINT), | ||
| 228 | - "Ampersand" => Some(XK_AMPERSAND), | ||
| 229 | - "Agrave" => Some(XK_AGRAVE), | ||
| 230 | - "Ccedilla" => Some(XK_CCEDILLA), | ||
| 231 | - "Egrave" => Some(XK_EGRAVE), | ||
| 232 | - "Eacute" => Some(XK_EACUTE), | ||
| 233 | - "ParenLeft" => Some(XK_PARENLEFT), | ||
| 234 | - "ParenRight" => Some(XK_PARENRIGHT), | ||
| 235 | - "QuoteDouble" => Some(XK_QUOTEDBL), | ||
| 236 | - "Underscore" => Some(XK_UNDERSCORE), | ||
| 237 | - "Hyphen" => Some(XK_HYPHEN), | ||
| 238 | - "AudioMedia" | "XF86AudioMedia" => Some(XF86_AUDIO_MEDIA), | ||
| 239 | - "XF86AudioNext" => Some(XF86_AUDIO_NEXT), | ||
| 240 | - "XF86AudioPause" => Some(XF86_AUDIO_PAUSE), | ||
| 241 | - "XF86AudioPlay" => Some(XF86_AUDIO_PLAY), | ||
| 242 | - "XF86AudioPrev" => Some(XF86_AUDIO_PREV), | ||
| 243 | - "AudioRaiseVolume" | "XF86AudioRaiseVolume" => Some(XF86_AUDIO_RAISE_VOLUME), | ||
| 244 | - "AudioLowerVolume" | "XF86AudioLowerVolume" => Some(XF86_AUDIO_LOWER_VOLUME), | ||
| 245 | - "AudioMute" | "XF86AudioMute" => Some(XF86_AUDIO_MUTE), | ||
| 246 | - "XF86AudioMicMute" => Some(XF86_AUDIO_MIC_MUTE), | ||
| 247 | - "XF86AudioRecord" => Some(XF86_AUDIO_RECORD), | ||
| 248 | - "XF86AudioRewind" => Some(XF86_AUDIO_REWIND), | ||
| 249 | - "XF86AudioStop" => Some(XF86_AUDIO_STOP), | ||
| 250 | - "XF86Back" => Some(XF86_BACK), | ||
| 251 | - "XF86Calculator" => Some(XF86_CALCULATOR), | ||
| 252 | - "XF86Close" => Some(XF86_CLOSE), | ||
| 253 | - "XF86Copy" => Some(XF86_COPY), | ||
| 254 | - "XF86Cut" => Some(XF86_CUT), | ||
| 255 | - "XF86DOS" => Some(XF86_DOS), | ||
| 256 | - "XF86Display" => Some(XF86_DISPLAY), | ||
| 257 | - "XF86Documents" => Some(XF86_DOCUMENTS), | ||
| 258 | - "XF86Eject" => Some(XF86_EJECT), | ||
| 259 | - "XF86Explorer" => Some(XF86_EXPLORER), | ||
| 260 | - "XF86Favorites" => Some(XF86_FAVORITES), | ||
| 261 | - "XF86Finance" => Some(XF86_FINANCE), | ||
| 262 | - "XF86Forward" => Some(XF86_FORWARD), | ||
| 263 | - "XF86Game" => Some(XF86_GAME), | ||
| 264 | - "XF86Go" => Some(XF86_GO), | ||
| 265 | - "XF86HomePage" => Some(XF86_HOME_PAGE), | ||
| 266 | - "XF86Mail" => Some(XF86_MAIL), | ||
| 267 | - "XF86MailForward" => Some(XF86_MAIL_FORWARD), | ||
| 268 | - "XF86MenuKB" => Some(XF86_MENU_KB), | ||
| 269 | - "XF86Messenger" => Some(XF86_MESSENGER), | ||
| 270 | - "MonBrightnessUp" | "XF86MonBrightnessUp" => Some(XF86_MON_BRIGHTNESS_UP), | ||
| 271 | - "MonBrightnessDown" | "XF86MonBrightnessDown" => Some(XF86_MON_BRIGHTNESS_DOWN), | ||
| 272 | - "XF86MyComputer" => Some(XF86_MY_COMPUTER), | ||
| 273 | - "XF86New" => Some(XF86_NEW), | ||
| 274 | - "XF86Next_VMode" => Some(XF86_NEXT_VMODE), | ||
| 275 | - "XF86Prev_VMode" => Some(XF86_PREV_VMODE), | ||
| 276 | - "XF86Open" => Some(XF86_OPEN), | ||
| 277 | - "XF86Paste" => Some(XF86_PASTE), | ||
| 278 | - "XF86Phone" => Some(XF86_PHONE), | ||
| 279 | - "XF86PowerOff" => Some(XF86_POWER_OFF), | ||
| 280 | - "XF86Reload" => Some(XF86_RELOAD), | ||
| 281 | - "XF86Reply" => Some(XF86_REPLY), | ||
| 282 | - "XF86RotateWindows" => Some(XF86_ROTATE_WINDOWS), | ||
| 283 | - "XF86Save" => Some(XF86_SAVE), | ||
| 284 | - "XF86ScreenSaver" => Some(XF86_SCREEN_SAVER), | ||
| 285 | - "XF86ScrollDown" => Some(XF86_SCROLL_DOWN), | ||
| 286 | - "XF86ScrollUp" => Some(XF86_SCROLL_UP), | ||
| 287 | - "XF86Search" => Some(XF86_SEARCH), | ||
| 288 | - "XF86Send" => Some(XF86_SEND), | ||
| 289 | - "XF86Shop" => Some(XF86_SHOP), | ||
| 290 | - "XF86Sleep" => Some(XF86_SLEEP), | ||
| 291 | - "XF86TaskPane" => Some(XF86_TASK_PANE), | ||
| 292 | - "XF86Tools" => Some(XF86_TOOLS), | ||
| 293 | - "XF86WWW" => Some(XF86_WWW), | ||
| 294 | - "XF86WakeUp" => Some(XF86_WAKE_UP), | ||
| 295 | - "XF86WebCam" => Some(XF86_WEBCAM), | ||
| 296 | - "XF86Xfer" => Some(XF86_XFER), | ||
| 297 | - _ => None, | ||
| 298 | - } | ||
| 299 | -} | ||
| 300 | - | ||
| 301 | -pub fn format_keysym(keysym: Keysym) -> String { | ||
| 302 | - match keysym { | ||
| 303 | - XK_RETURN => "Return".to_string(), | ||
| 304 | - XK_ESCAPE => "Esc".to_string(), | ||
| 305 | - XK_SPACE => "Space".to_string(), | ||
| 306 | - XK_TAB => "Tab".to_string(), | ||
| 307 | - XK_BACKSPACE => "Backspace".to_string(), | ||
| 308 | - XK_DELETE => "Del".to_string(), | ||
| 309 | - XK_LEFT => "Left".to_string(), | ||
| 310 | - XK_RIGHT => "Right".to_string(), | ||
| 311 | - XK_UP => "Up".to_string(), | ||
| 312 | - XK_DOWN => "Down".to_string(), | ||
| 313 | - XK_HOME => "Home".to_string(), | ||
| 314 | - XK_END => "End".to_string(), | ||
| 315 | - XK_PAGE_UP => "PgUp".to_string(), | ||
| 316 | - XK_PAGE_DOWN => "PgDn".to_string(), | ||
| 317 | - XK_INSERT => "Ins".to_string(), | ||
| 318 | - XK_F1 => "F1".to_string(), | ||
| 319 | - XK_F2 => "F2".to_string(), | ||
| 320 | - XK_F3 => "F3".to_string(), | ||
| 321 | - XK_F4 => "F4".to_string(), | ||
| 322 | - XK_F5 => "F5".to_string(), | ||
| 323 | - XK_F6 => "F6".to_string(), | ||
| 324 | - XK_F7 => "F7".to_string(), | ||
| 325 | - XK_F8 => "F8".to_string(), | ||
| 326 | - XK_F9 => "F9".to_string(), | ||
| 327 | - XK_F10 => "F10".to_string(), | ||
| 328 | - XK_F11 => "F11".to_string(), | ||
| 329 | - XK_F12 => "F12".to_string(), | ||
| 330 | - XK_SLASH => "/".to_string(), | ||
| 331 | - XK_COMMA => ",".to_string(), | ||
| 332 | - XK_PERIOD => ".".to_string(), | ||
| 333 | - XK_MINUS => "-".to_string(), | ||
| 334 | - XK_EQUAL => "=".to_string(), | ||
| 335 | - XK_GRAVE => "`".to_string(), | ||
| 336 | - XK_LEFT_BRACKET => "[".to_string(), | ||
| 337 | - XK_RIGHT_BRACKET => "]".to_string(), | ||
| 338 | - XK_SEMICOLON => ";".to_string(), | ||
| 339 | - XK_QUESTION => "?".to_string(), | ||
| 340 | - XK_APOSTROPHE => "'".to_string(), | ||
| 341 | - XK_BACKSLASH => "\\".to_string(), | ||
| 342 | - XK_PRINT => "Print".to_string(), | ||
| 343 | - XK_AMPERSAND => "&".to_string(), | ||
| 344 | - XK_AGRAVE => "Ã ".to_string(), | ||
| 345 | - XK_CCEDILLA => "ç".to_string(), | ||
| 346 | - XK_EGRAVE => "è".to_string(), | ||
| 347 | - XK_EACUTE => "é".to_string(), | ||
| 348 | - XK_PARENLEFT => "(".to_string(), | ||
| 349 | - XK_PARENRIGHT => ")".to_string(), | ||
| 350 | - XK_QUOTEDBL => "\"".to_string(), | ||
| 351 | - XK_UNDERSCORE => "_".to_string(), | ||
| 352 | - XK_HYPHEN => "-".to_string(), | ||
| 353 | - XF86_AUDIO_MEDIA => "Media".to_string(), | ||
| 354 | - XF86_AUDIO_NEXT => "Next".to_string(), | ||
| 355 | - XF86_AUDIO_PAUSE => "Pause".to_string(), | ||
| 356 | - XF86_AUDIO_PLAY => "Play".to_string(), | ||
| 357 | - XF86_AUDIO_PREV => "Prev".to_string(), | ||
| 358 | - XF86_AUDIO_RAISE_VOLUME => "Vol+".to_string(), | ||
| 359 | - XF86_AUDIO_LOWER_VOLUME => "Vol-".to_string(), | ||
| 360 | - XF86_AUDIO_MUTE => "Mute".to_string(), | ||
| 361 | - XF86_AUDIO_MIC_MUTE => "MicMute".to_string(), | ||
| 362 | - XF86_AUDIO_RECORD => "Record".to_string(), | ||
| 363 | - XF86_AUDIO_REWIND => "Rewind".to_string(), | ||
| 364 | - XF86_AUDIO_STOP => "Stop".to_string(), | ||
| 365 | - XF86_BACK => "Back".to_string(), | ||
| 366 | - XF86_CALCULATOR => "Calculator".to_string(), | ||
| 367 | - XF86_CLOSE => "Close".to_string(), | ||
| 368 | - XF86_COPY => "Copy".to_string(), | ||
| 369 | - XF86_CUT => "Cut".to_string(), | ||
| 370 | - XF86_DOS => "DOS".to_string(), | ||
| 371 | - XF86_DISPLAY => "Display".to_string(), | ||
| 372 | - XF86_DOCUMENTS => "Documents".to_string(), | ||
| 373 | - XF86_EJECT => "Eject".to_string(), | ||
| 374 | - XF86_EXPLORER => "Explorer".to_string(), | ||
| 375 | - XF86_FAVORITES => "Favorites".to_string(), | ||
| 376 | - XF86_FINANCE => "Finance".to_string(), | ||
| 377 | - XF86_FORWARD => "Forward".to_string(), | ||
| 378 | - XF86_GAME => "Game".to_string(), | ||
| 379 | - XF86_GO => "Go".to_string(), | ||
| 380 | - XF86_HOME_PAGE => "HomePage".to_string(), | ||
| 381 | - XF86_MAIL => "Mail".to_string(), | ||
| 382 | - XF86_MAIL_FORWARD => "MailForward".to_string(), | ||
| 383 | - XF86_MENU_KB => "MenuKB".to_string(), | ||
| 384 | - XF86_MESSENGER => "Messenger".to_string(), | ||
| 385 | - XF86_MON_BRIGHTNESS_UP => "Bright+".to_string(), | ||
| 386 | - XF86_MON_BRIGHTNESS_DOWN => "Bright-".to_string(), | ||
| 387 | - XF86_MY_COMPUTER => "MyComputer".to_string(), | ||
| 388 | - XF86_NEW => "New".to_string(), | ||
| 389 | - XF86_NEXT_VMODE => "NextVMode".to_string(), | ||
| 390 | - XF86_PREV_VMODE => "PrevVMode".to_string(), | ||
| 391 | - XF86_OPEN => "Open".to_string(), | ||
| 392 | - XF86_PASTE => "Paste".to_string(), | ||
| 393 | - XF86_PHONE => "Phone".to_string(), | ||
| 394 | - XF86_POWER_OFF => "PowerOff".to_string(), | ||
| 395 | - XF86_RELOAD => "Reload".to_string(), | ||
| 396 | - XF86_REPLY => "Reply".to_string(), | ||
| 397 | - XF86_ROTATE_WINDOWS => "RotateWindows".to_string(), | ||
| 398 | - XF86_SAVE => "Save".to_string(), | ||
| 399 | - XF86_SCREEN_SAVER => "ScreenSaver".to_string(), | ||
| 400 | - XF86_SCROLL_DOWN => "ScrollDown".to_string(), | ||
| 401 | - XF86_SCROLL_UP => "ScrollUp".to_string(), | ||
| 402 | - XF86_SEARCH => "Search".to_string(), | ||
| 403 | - XF86_SEND => "Send".to_string(), | ||
| 404 | - XF86_SHOP => "Shop".to_string(), | ||
| 405 | - XF86_SLEEP => "Sleep".to_string(), | ||
| 406 | - XF86_TASK_PANE => "TaskPane".to_string(), | ||
| 407 | - XF86_TOOLS => "Tools".to_string(), | ||
| 408 | - XF86_WWW => "WWW".to_string(), | ||
| 409 | - XF86_WAKE_UP => "WakeUp".to_string(), | ||
| 410 | - XF86_WEBCAM => "WebCam".to_string(), | ||
| 411 | - XF86_XFER => "Xfer".to_string(), | ||
| 412 | - XK_A..=XK_Z => { | ||
| 413 | - let ch = (keysym - XK_A + b'A' as u32) as u8 as char; | ||
| 414 | - ch.to_string() | ||
| 415 | - } | ||
| 416 | - XK_0..=XK_9 => { | ||
| 417 | - let ch = (keysym - XK_0 + b'0' as u32) as u8 as char; | ||
| 418 | - ch.to_string() | ||
| 419 | - } | ||
| 420 | - _ => format!("0x{:x}", keysym), | ||
| 421 | - } | ||
| 422 | -} | ||
+0 −5 src/keyboard/mod.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,5 +0,0 @@ | |||
| 1 | -pub mod handlers; | ||
| 2 | -pub mod keysyms; | ||
| 3 | - | ||
| 4 | -pub use handlers::{Arg, KeyAction, KeyboardMapping, grab_keys, handle_key_press}; | ||
| 5 | -pub use keysyms::*; | ||
+0 −92 src/layout/grid.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,92 +0,0 @@ | |||
| 1 | -use super::{GapConfig, Layout, WindowGeometry}; | ||
| 2 | -use x11rb::protocol::xproto::Window; | ||
| 3 | - | ||
| 4 | -pub struct GridLayout; | ||
| 5 | - | ||
| 6 | -impl Layout for GridLayout { | ||
| 7 | - fn name(&self) -> &'static str { | ||
| 8 | - super::LayoutType::Grid.as_str() | ||
| 9 | - } | ||
| 10 | - | ||
| 11 | - fn symbol(&self) -> &'static str { | ||
| 12 | - "[#]" | ||
| 13 | - } | ||
| 14 | - | ||
| 15 | - fn arrange( | ||
| 16 | - &self, | ||
| 17 | - windows: &[Window], | ||
| 18 | - screen_width: u32, | ||
| 19 | - screen_height: u32, | ||
| 20 | - gaps: &GapConfig, | ||
| 21 | - _master_factor: f32, | ||
| 22 | - _num_master: i32, | ||
| 23 | - _smartgaps_enabled: bool, | ||
| 24 | - ) -> Vec<WindowGeometry> { | ||
| 25 | - let window_count = windows.len(); | ||
| 26 | - if window_count == 0 { | ||
| 27 | - return Vec::new(); | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - if window_count == 1 { | ||
| 31 | - let x = gaps.outer_horizontal as i32; | ||
| 32 | - let y = gaps.outer_vertical as i32; | ||
| 33 | - let width = screen_width.saturating_sub(2 * gaps.outer_horizontal); | ||
| 34 | - let height = screen_height.saturating_sub(2 * gaps.outer_vertical); | ||
| 35 | - | ||
| 36 | - return vec![WindowGeometry { | ||
| 37 | - x_coordinate: x, | ||
| 38 | - y_coordinate: y, | ||
| 39 | - width, | ||
| 40 | - height, | ||
| 41 | - }]; | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - let cols = (window_count as f64).sqrt().ceil() as usize; | ||
| 45 | - let rows = (window_count as f64 / cols as f64).ceil() as usize; | ||
| 46 | - | ||
| 47 | - let mut geometries = Vec::new(); | ||
| 48 | - | ||
| 49 | - let total_horizontal_gaps = | ||
| 50 | - gaps.outer_horizontal * 2 + gaps.inner_horizontal * (cols as u32 - 1); | ||
| 51 | - let total_vertical_gaps = gaps.outer_vertical * 2 + gaps.inner_vertical * (rows as u32 - 1); | ||
| 52 | - | ||
| 53 | - let cell_width = screen_width.saturating_sub(total_horizontal_gaps) / cols as u32; | ||
| 54 | - let cell_height = screen_height.saturating_sub(total_vertical_gaps) / rows as u32; | ||
| 55 | - | ||
| 56 | - for (index, _window) in windows.iter().enumerate() { | ||
| 57 | - let row = index / cols; | ||
| 58 | - let col = index % cols; | ||
| 59 | - | ||
| 60 | - let is_last_row = row == rows - 1; | ||
| 61 | - let windows_in_last_row = window_count - (rows - 1) * cols; | ||
| 62 | - | ||
| 63 | - let (x, y, width, height) = if is_last_row && windows_in_last_row < cols { | ||
| 64 | - let last_row_col = index % cols; | ||
| 65 | - let last_row_cell_width = | ||
| 66 | - screen_width.saturating_sub(total_horizontal_gaps.saturating_sub( | ||
| 67 | - gaps.inner_horizontal * (cols as u32 - windows_in_last_row as u32), | ||
| 68 | - )) / windows_in_last_row as u32; | ||
| 69 | - | ||
| 70 | - let x = gaps.outer_horizontal | ||
| 71 | - + last_row_col as u32 * (last_row_cell_width + gaps.inner_horizontal); | ||
| 72 | - let y = gaps.outer_vertical + row as u32 * (cell_height + gaps.inner_vertical); | ||
| 73 | - | ||
| 74 | - (x as i32, y as i32, last_row_cell_width, cell_height) | ||
| 75 | - } else { | ||
| 76 | - let x = gaps.outer_horizontal + col as u32 * (cell_width + gaps.inner_horizontal); | ||
| 77 | - let y = gaps.outer_vertical + row as u32 * (cell_height + gaps.inner_vertical); | ||
| 78 | - | ||
| 79 | - (x as i32, y as i32, cell_width, cell_height) | ||
| 80 | - }; | ||
| 81 | - | ||
| 82 | - geometries.push(WindowGeometry { | ||
| 83 | - x_coordinate: x, | ||
| 84 | - y_coordinate: y, | ||
| 85 | - width, | ||
| 86 | - height, | ||
| 87 | - }); | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - geometries | ||
| 91 | - } | ||
| 92 | -} | ||
+0 −115 src/layout/mod.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,115 +0,0 @@ | |||
| 1 | -pub mod grid; | ||
| 2 | -pub mod monocle; | ||
| 3 | -pub mod normie; | ||
| 4 | -pub mod scrolling; | ||
| 5 | -pub mod tabbed; | ||
| 6 | -pub mod tiling; | ||
| 7 | - | ||
| 8 | -use std::str::FromStr; | ||
| 9 | - | ||
| 10 | -use x11rb::protocol::xproto::Window; | ||
| 11 | - | ||
| 12 | -pub type LayoutBox = Box<dyn Layout>; | ||
| 13 | - | ||
| 14 | -pub struct GapConfig { | ||
| 15 | - pub inner_horizontal: u32, | ||
| 16 | - pub inner_vertical: u32, | ||
| 17 | - pub outer_horizontal: u32, | ||
| 18 | - pub outer_vertical: u32, | ||
| 19 | -} | ||
| 20 | - | ||
| 21 | -pub enum LayoutType { | ||
| 22 | - Tiling, | ||
| 23 | - Normie, | ||
| 24 | - Grid, | ||
| 25 | - Monocle, | ||
| 26 | - Tabbed, | ||
| 27 | - Scrolling, | ||
| 28 | -} | ||
| 29 | - | ||
| 30 | -impl LayoutType { | ||
| 31 | - pub fn to_boxed_layout(&self) -> LayoutBox { | ||
| 32 | - match self { | ||
| 33 | - Self::Tiling => Box::new(tiling::TilingLayout), | ||
| 34 | - Self::Normie => Box::new(normie::NormieLayout), | ||
| 35 | - Self::Grid => Box::new(grid::GridLayout), | ||
| 36 | - Self::Monocle => Box::new(monocle::MonocleLayout), | ||
| 37 | - Self::Tabbed => Box::new(tabbed::TabbedLayout), | ||
| 38 | - Self::Scrolling => Box::new(scrolling::ScrollingLayout), | ||
| 39 | - } | ||
| 40 | - } | ||
| 41 | - | ||
| 42 | - pub fn next(&self) -> Self { | ||
| 43 | - match self { | ||
| 44 | - Self::Tiling => Self::Normie, | ||
| 45 | - Self::Normie => Self::Grid, | ||
| 46 | - Self::Grid => Self::Monocle, | ||
| 47 | - Self::Monocle => Self::Tabbed, | ||
| 48 | - Self::Tabbed => Self::Scrolling, | ||
| 49 | - Self::Scrolling => Self::Tiling, | ||
| 50 | - } | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - pub fn as_str(&self) -> &'static str { | ||
| 54 | - match self { | ||
| 55 | - Self::Tiling => "tiling", | ||
| 56 | - Self::Normie => "normie", | ||
| 57 | - Self::Grid => "grid", | ||
| 58 | - Self::Monocle => "monocle", | ||
| 59 | - Self::Tabbed => "tabbed", | ||
| 60 | - Self::Scrolling => "scrolling", | ||
| 61 | - } | ||
| 62 | - } | ||
| 63 | -} | ||
| 64 | - | ||
| 65 | -impl FromStr for LayoutType { | ||
| 66 | - type Err = String; | ||
| 67 | - | ||
| 68 | - fn from_str(s: &str) -> Result<Self, String> { | ||
| 69 | - match s.to_lowercase().as_str() { | ||
| 70 | - "tiling" => Ok(Self::Tiling), | ||
| 71 | - "normie" | "floating" => Ok(Self::Normie), | ||
| 72 | - "grid" => Ok(Self::Grid), | ||
| 73 | - "monocle" => Ok(Self::Monocle), | ||
| 74 | - "tabbed" => Ok(Self::Tabbed), | ||
| 75 | - "scrolling" => Ok(Self::Scrolling), | ||
| 76 | - _ => Err(format!("Invalid Layout Type: {}", s)), | ||
| 77 | - } | ||
| 78 | - } | ||
| 79 | -} | ||
| 80 | - | ||
| 81 | -pub fn layout_from_str(s: &str) -> Result<LayoutBox, String> { | ||
| 82 | - let layout_type = LayoutType::from_str(s)?; | ||
| 83 | - Ok(layout_type.to_boxed_layout()) | ||
| 84 | -} | ||
| 85 | - | ||
| 86 | -pub fn next_layout(current_name: &str) -> &'static str { | ||
| 87 | - LayoutType::from_str(current_name) | ||
| 88 | - .ok() | ||
| 89 | - .map(|layout_type| layout_type.next()) | ||
| 90 | - .unwrap_or(LayoutType::Tiling) | ||
| 91 | - .as_str() | ||
| 92 | -} | ||
| 93 | - | ||
| 94 | -pub trait Layout { | ||
| 95 | - fn arrange( | ||
| 96 | - &self, | ||
| 97 | - windows: &[Window], | ||
| 98 | - screen_width: u32, | ||
| 99 | - screen_height: u32, | ||
| 100 | - gaps: &GapConfig, | ||
| 101 | - master_factor: f32, | ||
| 102 | - num_master: i32, | ||
| 103 | - smartgaps_enabled: bool, | ||
| 104 | - ) -> Vec<WindowGeometry>; | ||
| 105 | - fn name(&self) -> &'static str; | ||
| 106 | - fn symbol(&self) -> &'static str; | ||
| 107 | -} | ||
| 108 | - | ||
| 109 | -#[derive(Clone)] | ||
| 110 | -pub struct WindowGeometry { | ||
| 111 | - pub x_coordinate: i32, | ||
| 112 | - pub y_coordinate: i32, | ||
| 113 | - pub width: u32, | ||
| 114 | - pub height: u32, | ||
| 115 | -} | ||
+0 −44 src/layout/monocle.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,44 +0,0 @@ | |||
| 1 | -use super::{GapConfig, Layout, WindowGeometry}; | ||
| 2 | -use x11rb::protocol::xproto::Window; | ||
| 3 | - | ||
| 4 | -pub struct MonocleLayout; | ||
| 5 | - | ||
| 6 | -impl Layout for MonocleLayout { | ||
| 7 | - fn name(&self) -> &'static str { | ||
| 8 | - super::LayoutType::Monocle.as_str() | ||
| 9 | - } | ||
| 10 | - | ||
| 11 | - fn symbol(&self) -> &'static str { | ||
| 12 | - "[M]" | ||
| 13 | - } | ||
| 14 | - | ||
| 15 | - fn arrange( | ||
| 16 | - &self, | ||
| 17 | - windows: &[Window], | ||
| 18 | - screen_width: u32, | ||
| 19 | - screen_height: u32, | ||
| 20 | - gaps: &GapConfig, | ||
| 21 | - _master_factor: f32, | ||
| 22 | - _num_master: i32, | ||
| 23 | - _smartgaps_enabled: bool, | ||
| 24 | - ) -> Vec<WindowGeometry> { | ||
| 25 | - let window_count = windows.len(); | ||
| 26 | - if window_count == 0 { | ||
| 27 | - return Vec::new(); | ||
| 28 | - } | ||
| 29 | - | ||
| 30 | - let x = gaps.outer_horizontal as i32; | ||
| 31 | - let y = gaps.outer_vertical as i32; | ||
| 32 | - let width = screen_width.saturating_sub(2 * gaps.outer_horizontal); | ||
| 33 | - let height = screen_height.saturating_sub(2 * gaps.outer_vertical); | ||
| 34 | - | ||
| 35 | - let geometry = WindowGeometry { | ||
| 36 | - x_coordinate: x, | ||
| 37 | - y_coordinate: y, | ||
| 38 | - width, | ||
| 39 | - height, | ||
| 40 | - }; | ||
| 41 | - | ||
| 42 | - vec![geometry; window_count] | ||
| 43 | - } | ||
| 44 | -} | ||
+0 −28 src/layout/normie.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,28 +0,0 @@ | |||
| 1 | -use super::{GapConfig, Layout, WindowGeometry}; | ||
| 2 | -use x11rb::protocol::xproto::Window; | ||
| 3 | - | ||
| 4 | -pub struct NormieLayout; | ||
| 5 | - | ||
| 6 | -// This layout should return a no-op similar to DWM.C's "null" mode. | ||
| 7 | -impl Layout for NormieLayout { | ||
| 8 | - fn name(&self) -> &'static str { | ||
| 9 | - super::LayoutType::Normie.as_str() | ||
| 10 | - } | ||
| 11 | - | ||
| 12 | - fn symbol(&self) -> &'static str { | ||
| 13 | - "><>" | ||
| 14 | - } | ||
| 15 | - | ||
| 16 | - fn arrange( | ||
| 17 | - &self, | ||
| 18 | - _windows: &[Window], | ||
| 19 | - _screen_width: u32, | ||
| 20 | - _screen_height: u32, | ||
| 21 | - _gaps: &GapConfig, | ||
| 22 | - _master_factor: f32, | ||
| 23 | - _num_master: i32, | ||
| 24 | - _smartgaps_enabled: bool, | ||
| 25 | - ) -> Vec<WindowGeometry> { | ||
| 26 | - Vec::new() | ||
| 27 | - } | ||
| 28 | -} | ||
+0 −100 src/layout/scrolling.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,100 +0,0 @@ | |||
| 1 | -use super::{GapConfig, Layout, WindowGeometry}; | ||
| 2 | -use x11rb::protocol::xproto::Window; | ||
| 3 | - | ||
| 4 | -pub struct ScrollingLayout; | ||
| 5 | - | ||
| 6 | -struct GapValues { | ||
| 7 | - outer_horizontal: u32, | ||
| 8 | - outer_vertical: u32, | ||
| 9 | - inner_vertical: u32, | ||
| 10 | -} | ||
| 11 | - | ||
| 12 | -impl ScrollingLayout { | ||
| 13 | - fn getgaps(gaps: &GapConfig, window_count: usize, smartgaps_enabled: bool) -> GapValues { | ||
| 14 | - let outer_enabled = if smartgaps_enabled && window_count == 1 { | ||
| 15 | - 0 | ||
| 16 | - } else { | ||
| 17 | - 1 | ||
| 18 | - }; | ||
| 19 | - | ||
| 20 | - GapValues { | ||
| 21 | - outer_horizontal: gaps.outer_horizontal * outer_enabled, | ||
| 22 | - outer_vertical: gaps.outer_vertical * outer_enabled, | ||
| 23 | - inner_vertical: gaps.inner_vertical, | ||
| 24 | - } | ||
| 25 | - } | ||
| 26 | -} | ||
| 27 | - | ||
| 28 | -impl Layout for ScrollingLayout { | ||
| 29 | - fn name(&self) -> &'static str { | ||
| 30 | - "scrolling" | ||
| 31 | - } | ||
| 32 | - | ||
| 33 | - fn symbol(&self) -> &'static str { | ||
| 34 | - "[>>]" | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - fn arrange( | ||
| 38 | - &self, | ||
| 39 | - windows: &[Window], | ||
| 40 | - screen_width: u32, | ||
| 41 | - screen_height: u32, | ||
| 42 | - gaps: &GapConfig, | ||
| 43 | - _master_factor: f32, | ||
| 44 | - num_master: i32, | ||
| 45 | - smartgaps_enabled: bool, | ||
| 46 | - ) -> Vec<WindowGeometry> { | ||
| 47 | - let window_count = windows.len(); | ||
| 48 | - if window_count == 0 { | ||
| 49 | - return Vec::new(); | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - let gap_values = Self::getgaps(gaps, window_count, smartgaps_enabled); | ||
| 53 | - | ||
| 54 | - let outer_horizontal = gap_values.outer_horizontal; | ||
| 55 | - let outer_vertical = gap_values.outer_vertical; | ||
| 56 | - let inner_vertical = gap_values.inner_vertical; | ||
| 57 | - | ||
| 58 | - let visible_count = if num_master > 0 { | ||
| 59 | - num_master as usize | ||
| 60 | - } else { | ||
| 61 | - 2 | ||
| 62 | - }; | ||
| 63 | - | ||
| 64 | - let available_width = screen_width.saturating_sub(2 * outer_vertical); | ||
| 65 | - let available_height = screen_height.saturating_sub(2 * outer_horizontal); | ||
| 66 | - | ||
| 67 | - let total_inner_gaps = if visible_count > 1 { | ||
| 68 | - inner_vertical * (visible_count.min(window_count) - 1) as u32 | ||
| 69 | - } else { | ||
| 70 | - 0 | ||
| 71 | - }; | ||
| 72 | - let window_width = if window_count <= visible_count { | ||
| 73 | - let num_windows = window_count as u32; | ||
| 74 | - let total_gaps = if num_windows > 1 { | ||
| 75 | - inner_vertical * (num_windows - 1) | ||
| 76 | - } else { | ||
| 77 | - 0 | ||
| 78 | - }; | ||
| 79 | - (available_width.saturating_sub(total_gaps)) / num_windows | ||
| 80 | - } else { | ||
| 81 | - (available_width.saturating_sub(total_inner_gaps)) / visible_count as u32 | ||
| 82 | - }; | ||
| 83 | - | ||
| 84 | - let mut geometries = Vec::with_capacity(window_count); | ||
| 85 | - let mut x = outer_vertical as i32; | ||
| 86 | - | ||
| 87 | - for _window in windows.iter() { | ||
| 88 | - geometries.push(WindowGeometry { | ||
| 89 | - x_coordinate: x, | ||
| 90 | - y_coordinate: outer_horizontal as i32, | ||
| 91 | - width: window_width, | ||
| 92 | - height: available_height, | ||
| 93 | - }); | ||
| 94 | - | ||
| 95 | - x += window_width as i32 + inner_vertical as i32; | ||
| 96 | - } | ||
| 97 | - | ||
| 98 | - geometries | ||
| 99 | - } | ||
| 100 | -} | ||
+0 −48 src/layout/tabbed.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,48 +0,0 @@ | |||
| 1 | -use super::{GapConfig, Layout, WindowGeometry}; | ||
| 2 | -use x11rb::protocol::xproto::Window; | ||
| 3 | - | ||
| 4 | -pub struct TabbedLayout; | ||
| 5 | - | ||
| 6 | -pub const TAB_BAR_HEIGHT: u32 = 28; | ||
| 7 | - | ||
| 8 | -impl Layout for TabbedLayout { | ||
| 9 | - fn name(&self) -> &'static str { | ||
| 10 | - super::LayoutType::Tabbed.as_str() | ||
| 11 | - } | ||
| 12 | - | ||
| 13 | - fn symbol(&self) -> &'static str { | ||
| 14 | - "[=]" | ||
| 15 | - } | ||
| 16 | - | ||
| 17 | - fn arrange( | ||
| 18 | - &self, | ||
| 19 | - windows: &[Window], | ||
| 20 | - screen_width: u32, | ||
| 21 | - screen_height: u32, | ||
| 22 | - gaps: &GapConfig, | ||
| 23 | - _master_factor: f32, | ||
| 24 | - _num_master: i32, | ||
| 25 | - _smartgaps_enabled: bool, | ||
| 26 | - ) -> Vec<WindowGeometry> { | ||
| 27 | - let window_count = windows.len(); | ||
| 28 | - if window_count == 0 { | ||
| 29 | - return Vec::new(); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - let x = gaps.outer_horizontal as i32; | ||
| 33 | - let y = (gaps.outer_vertical + TAB_BAR_HEIGHT) as i32; | ||
| 34 | - let width = screen_width.saturating_sub(2 * gaps.outer_horizontal); | ||
| 35 | - let height = screen_height | ||
| 36 | - .saturating_sub(2 * gaps.outer_vertical) | ||
| 37 | - .saturating_sub(TAB_BAR_HEIGHT); | ||
| 38 | - | ||
| 39 | - let geometry = WindowGeometry { | ||
| 40 | - x_coordinate: x, | ||
| 41 | - y_coordinate: y, | ||
| 42 | - width, | ||
| 43 | - height, | ||
| 44 | - }; | ||
| 45 | - | ||
| 46 | - vec![geometry; window_count] | ||
| 47 | - } | ||
| 48 | -} | ||
+0 −173 src/layout/tiling.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,173 +0,0 @@ | |||
| 1 | -use super::{GapConfig, Layout, WindowGeometry}; | ||
| 2 | -use x11rb::protocol::xproto::Window; | ||
| 3 | - | ||
| 4 | -pub struct TilingLayout; | ||
| 5 | - | ||
| 6 | -struct GapValues { | ||
| 7 | - outer_horizontal: u32, | ||
| 8 | - outer_vertical: u32, | ||
| 9 | - inner_horizontal: u32, | ||
| 10 | - inner_vertical: u32, | ||
| 11 | -} | ||
| 12 | - | ||
| 13 | -struct FactValues { | ||
| 14 | - master_facts: f32, | ||
| 15 | - stack_facts: f32, | ||
| 16 | - master_remainder: i32, | ||
| 17 | - stack_remainder: i32, | ||
| 18 | -} | ||
| 19 | - | ||
| 20 | -impl TilingLayout { | ||
| 21 | - fn getgaps(gaps: &GapConfig, window_count: usize, smartgaps_enabled: bool) -> GapValues { | ||
| 22 | - let outer_enabled = if smartgaps_enabled && window_count == 1 { | ||
| 23 | - 0 | ||
| 24 | - } else { | ||
| 25 | - 1 | ||
| 26 | - }; | ||
| 27 | - let inner_enabled = 1; | ||
| 28 | - | ||
| 29 | - GapValues { | ||
| 30 | - outer_horizontal: gaps.outer_horizontal * outer_enabled, | ||
| 31 | - outer_vertical: gaps.outer_vertical * outer_enabled, | ||
| 32 | - inner_horizontal: gaps.inner_horizontal * inner_enabled, | ||
| 33 | - inner_vertical: gaps.inner_vertical * inner_enabled, | ||
| 34 | - } | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - fn getfacts( | ||
| 38 | - window_count: usize, | ||
| 39 | - num_master: i32, | ||
| 40 | - master_size: i32, | ||
| 41 | - stack_size: i32, | ||
| 42 | - ) -> FactValues { | ||
| 43 | - let num_master = num_master.max(0) as usize; | ||
| 44 | - let master_facts = window_count.min(num_master) as f32; | ||
| 45 | - let stack_facts = if window_count > num_master { | ||
| 46 | - (window_count - num_master) as f32 | ||
| 47 | - } else { | ||
| 48 | - 0.0 | ||
| 49 | - }; | ||
| 50 | - | ||
| 51 | - let mut master_total = 0; | ||
| 52 | - let mut stack_total = 0; | ||
| 53 | - | ||
| 54 | - for i in 0..window_count { | ||
| 55 | - if i < num_master { | ||
| 56 | - master_total += (master_size as f32 / master_facts) as i32; | ||
| 57 | - } else if stack_facts > 0.0 { | ||
| 58 | - stack_total += (stack_size as f32 / stack_facts) as i32; | ||
| 59 | - } | ||
| 60 | - } | ||
| 61 | - | ||
| 62 | - FactValues { | ||
| 63 | - master_facts, | ||
| 64 | - stack_facts, | ||
| 65 | - master_remainder: master_size - master_total, | ||
| 66 | - stack_remainder: stack_size - stack_total, | ||
| 67 | - } | ||
| 68 | - } | ||
| 69 | -} | ||
| 70 | - | ||
| 71 | -impl Layout for TilingLayout { | ||
| 72 | - fn name(&self) -> &'static str { | ||
| 73 | - super::LayoutType::Tiling.as_str() | ||
| 74 | - } | ||
| 75 | - | ||
| 76 | - fn symbol(&self) -> &'static str { | ||
| 77 | - "[]=" | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - fn arrange( | ||
| 81 | - &self, | ||
| 82 | - windows: &[Window], | ||
| 83 | - screen_width: u32, | ||
| 84 | - screen_height: u32, | ||
| 85 | - gaps: &GapConfig, | ||
| 86 | - master_factor: f32, | ||
| 87 | - num_master: i32, | ||
| 88 | - smartgaps_enabled: bool, | ||
| 89 | - ) -> Vec<WindowGeometry> { | ||
| 90 | - let window_count = windows.len(); | ||
| 91 | - if window_count == 0 { | ||
| 92 | - return Vec::new(); | ||
| 93 | - } | ||
| 94 | - | ||
| 95 | - let gap_values = Self::getgaps(gaps, window_count, smartgaps_enabled); | ||
| 96 | - | ||
| 97 | - let outer_gap_horizontal = gap_values.outer_horizontal; | ||
| 98 | - let outer_gap_vertical = gap_values.outer_vertical; | ||
| 99 | - let inner_gap_horizontal = gap_values.inner_horizontal; | ||
| 100 | - let inner_gap_vertical = gap_values.inner_vertical; | ||
| 101 | - | ||
| 102 | - let mut stack_x = outer_gap_vertical as i32; | ||
| 103 | - let mut stack_y = outer_gap_horizontal as i32; | ||
| 104 | - let master_x = outer_gap_vertical as i32; | ||
| 105 | - let mut master_y = outer_gap_horizontal as i32; | ||
| 106 | - | ||
| 107 | - let num_master_usize = num_master.max(0) as usize; | ||
| 108 | - let master_count = window_count.min(num_master_usize); | ||
| 109 | - let stack_count = window_count.saturating_sub(num_master_usize); | ||
| 110 | - | ||
| 111 | - let master_height = (screen_height as i32) | ||
| 112 | - - (2 * outer_gap_horizontal) as i32 | ||
| 113 | - - (inner_gap_horizontal as i32 * (master_count.saturating_sub(1)) as i32); | ||
| 114 | - let stack_height = (screen_height as i32) | ||
| 115 | - - (2 * outer_gap_horizontal) as i32 | ||
| 116 | - - (inner_gap_horizontal as i32 * stack_count.saturating_sub(1) as i32); | ||
| 117 | - let mut stack_width = (screen_width as i32) - (2 * outer_gap_vertical) as i32; | ||
| 118 | - let mut master_width = stack_width; | ||
| 119 | - | ||
| 120 | - if num_master > 0 && window_count > num_master_usize { | ||
| 121 | - stack_width = | ||
| 122 | - ((master_width as f32 - inner_gap_vertical as f32) * (1.0 - master_factor)) as i32; | ||
| 123 | - master_width = master_width - inner_gap_vertical as i32 - stack_width; | ||
| 124 | - stack_x = master_x + master_width + inner_gap_vertical as i32; | ||
| 125 | - } | ||
| 126 | - | ||
| 127 | - let facts = Self::getfacts(window_count, num_master, master_height, stack_height); | ||
| 128 | - | ||
| 129 | - let mut geometries = Vec::new(); | ||
| 130 | - | ||
| 131 | - for (i, _window) in windows.iter().enumerate() { | ||
| 132 | - if i < num_master_usize { | ||
| 133 | - let window_height = (master_height as f32 / facts.master_facts) as i32 | ||
| 134 | - + if (i as i32) < facts.master_remainder { | ||
| 135 | - 1 | ||
| 136 | - } else { | ||
| 137 | - 0 | ||
| 138 | - }; | ||
| 139 | - | ||
| 140 | - geometries.push(WindowGeometry { | ||
| 141 | - x_coordinate: master_x, | ||
| 142 | - y_coordinate: master_y, | ||
| 143 | - width: master_width as u32, | ||
| 144 | - height: window_height as u32, | ||
| 145 | - }); | ||
| 146 | - | ||
| 147 | - master_y += window_height + inner_gap_horizontal as i32; | ||
| 148 | - } else { | ||
| 149 | - let window_height = if facts.stack_facts > 0.0 { | ||
| 150 | - (stack_height as f32 / facts.stack_facts) as i32 | ||
| 151 | - + if ((i - num_master_usize) as i32) < facts.stack_remainder { | ||
| 152 | - 1 | ||
| 153 | - } else { | ||
| 154 | - 0 | ||
| 155 | - } | ||
| 156 | - } else { | ||
| 157 | - stack_height | ||
| 158 | - }; | ||
| 159 | - | ||
| 160 | - geometries.push(WindowGeometry { | ||
| 161 | - x_coordinate: stack_x, | ||
| 162 | - y_coordinate: stack_y, | ||
| 163 | - width: stack_width as u32, | ||
| 164 | - height: window_height as u32, | ||
| 165 | - }); | ||
| 166 | - | ||
| 167 | - stack_y += window_height + inner_gap_horizontal as i32; | ||
| 168 | - } | ||
| 169 | - } | ||
| 170 | - | ||
| 171 | - geometries | ||
| 172 | - } | ||
| 173 | -} | ||
+6 −0 src/layouts/floating.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,6 @@ | |||
| 1 | +const monitor_mod = @import("../monitor.zig"); | ||
| 2 | + | ||
| 3 | +pub const layout = monitor_mod.Layout{ | ||
| 4 | + .symbol = "><>", | ||
| 5 | + .arrange_fn = null, | ||
| 6 | +}; | ||
+31 −0 src/layouts/monocle.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,31 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const client_mod = @import("../client.zig"); | ||
| 3 | +const monitor_mod = @import("../monitor.zig"); | ||
| 4 | +const xlib = @import("../x11/xlib.zig"); | ||
| 5 | +const tiling = @import("tiling.zig"); | ||
| 6 | + | ||
| 7 | +const Client = client_mod.Client; | ||
| 8 | +const Monitor = monitor_mod.Monitor; | ||
| 9 | + | ||
| 10 | +pub const layout = monitor_mod.Layout{ | ||
| 11 | + .symbol = "[M]", | ||
| 12 | + .arrange_fn = monocle, | ||
| 13 | +}; | ||
| 14 | + | ||
| 15 | +pub fn monocle(monitor: *Monitor) void { | ||
| 16 | + const gap_h = monitor.gap_outer_h; | ||
| 17 | + const gap_v = monitor.gap_outer_v; | ||
| 18 | + | ||
| 19 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 20 | + while (current) |client| { | ||
| 21 | + tiling.resize( | ||
| 22 | + client, | ||
| 23 | + monitor.win_x + gap_v, | ||
| 24 | + monitor.win_y + gap_h, | ||
| 25 | + monitor.win_w - 2 * gap_v - 2 * client.border_width, | ||
| 26 | + monitor.win_h - 2 * gap_h - 2 * client.border_width, | ||
| 27 | + false, | ||
| 28 | + ); | ||
| 29 | + current = client_mod.next_tiled(client.next); | ||
| 30 | + } | ||
| 31 | +} | ||
+114 −0 src/layouts/scrolling.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,114 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const client_mod = @import("../client.zig"); | ||
| 3 | +const monitor_mod = @import("../monitor.zig"); | ||
| 4 | +const xlib = @import("../x11/xlib.zig"); | ||
| 5 | +const tiling = @import("tiling.zig"); | ||
| 6 | + | ||
| 7 | +const Client = client_mod.Client; | ||
| 8 | +const Monitor = monitor_mod.Monitor; | ||
| 9 | + | ||
| 10 | +pub const layout = monitor_mod.Layout{ | ||
| 11 | + .symbol = "[S]", | ||
| 12 | + .arrange_fn = scroll, | ||
| 13 | +}; | ||
| 14 | + | ||
| 15 | +pub fn scroll(monitor: *Monitor) void { | ||
| 16 | + var client_count: u32 = 0; | ||
| 17 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 18 | + while (current) |_| : (current = client_mod.next_tiled(current.?.next)) { | ||
| 19 | + client_count += 1; | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + if (client_count == 0) return; | ||
| 23 | + | ||
| 24 | + const gap_outer_h = monitor.gap_outer_h; | ||
| 25 | + const gap_outer_v = monitor.gap_outer_v; | ||
| 26 | + const gap_inner_v = monitor.gap_inner_v; | ||
| 27 | + | ||
| 28 | + const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); | ||
| 29 | + const available_width = monitor.win_w - 2 * gap_outer_v; | ||
| 30 | + const available_height = monitor.win_h - 2 * gap_outer_h; | ||
| 31 | + | ||
| 32 | + const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0)); | ||
| 33 | + const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count))); | ||
| 34 | + | ||
| 35 | + var x_pos: i32 = monitor.win_x + gap_outer_v - monitor.scroll_offset; | ||
| 36 | + const y_pos: i32 = monitor.win_y + gap_outer_h; | ||
| 37 | + const height = available_height; | ||
| 38 | + | ||
| 39 | + var index: u32 = 0; | ||
| 40 | + current = client_mod.next_tiled(monitor.clients); | ||
| 41 | + while (current) |client| : (current = client_mod.next_tiled(client.next)) { | ||
| 42 | + const window_right = x_pos + window_width; | ||
| 43 | + const screen_left = monitor.win_x; | ||
| 44 | + const screen_right = monitor.win_x + monitor.win_w; | ||
| 45 | + const is_visible = window_right > screen_left and x_pos < screen_right; | ||
| 46 | + | ||
| 47 | + if (is_visible) { | ||
| 48 | + tiling.resize( | ||
| 49 | + client, | ||
| 50 | + x_pos, | ||
| 51 | + y_pos, | ||
| 52 | + window_width - 2 * client.border_width, | ||
| 53 | + height - 2 * client.border_width, | ||
| 54 | + false, | ||
| 55 | + ); | ||
| 56 | + } else { | ||
| 57 | + tiling.resize_client( | ||
| 58 | + client, | ||
| 59 | + -2 * window_width, | ||
| 60 | + y_pos, | ||
| 61 | + window_width - 2 * client.border_width, | ||
| 62 | + height - 2 * client.border_width, | ||
| 63 | + ); | ||
| 64 | + } | ||
| 65 | + x_pos += window_width + gap_inner_v; | ||
| 66 | + index += 1; | ||
| 67 | + } | ||
| 68 | +} | ||
| 69 | + | ||
| 70 | +pub fn get_scroll_step(monitor: *Monitor) i32 { | ||
| 71 | + const gap_outer_v = monitor.gap_outer_v; | ||
| 72 | + const gap_inner_v = monitor.gap_inner_v; | ||
| 73 | + const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); | ||
| 74 | + const available_width = monitor.win_w - 2 * gap_outer_v; | ||
| 75 | + const total_gaps = gap_inner_v * @as(i32, @intCast(if (visible_count > 1) visible_count - 1 else 0)); | ||
| 76 | + const window_width = @divTrunc(available_width - total_gaps, @as(i32, @intCast(visible_count))); | ||
| 77 | + return window_width + gap_inner_v; | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +pub fn get_max_scroll(monitor: *Monitor) i32 { | ||
| 81 | + var client_count: u32 = 0; | ||
| 82 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 83 | + while (current) |_| : (current = client_mod.next_tiled(current.?.next)) { | ||
| 84 | + client_count += 1; | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + const visible_count: u32 = @intCast(@max(1, monitor.nmaster)); | ||
| 88 | + if (client_count <= visible_count) return 0; | ||
| 89 | + | ||
| 90 | + const scroll_step = get_scroll_step(monitor); | ||
| 91 | + const scrollable = client_count - visible_count; | ||
| 92 | + return scroll_step * @as(i32, @intCast(scrollable)); | ||
| 93 | +} | ||
| 94 | + | ||
| 95 | +pub fn get_window_index(monitor: *Monitor, target: *Client) ?u32 { | ||
| 96 | + var index: u32 = 0; | ||
| 97 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 98 | + while (current) |client| : (current = client_mod.next_tiled(client.next)) { | ||
| 99 | + if (client == target) return index; | ||
| 100 | + index += 1; | ||
| 101 | + } | ||
| 102 | + return null; | ||
| 103 | +} | ||
| 104 | + | ||
| 105 | +pub fn get_target_scroll_for_window(monitor: *Monitor, target: *Client) i32 { | ||
| 106 | + const index = get_window_index(monitor, target) orelse return 0; | ||
| 107 | + if (index == 0) return 0; | ||
| 108 | + | ||
| 109 | + const scroll_step = get_scroll_step(monitor); | ||
| 110 | + const max_scroll = get_max_scroll(monitor); | ||
| 111 | + | ||
| 112 | + const target_scroll = scroll_step * @as(i32, @intCast(index)); | ||
| 113 | + return @min(target_scroll, max_scroll); | ||
| 114 | +} | ||
+279 −0 src/layouts/tiling.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,279 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const client_mod = @import("../client.zig"); | ||
| 3 | +const monitor_mod = @import("../monitor.zig"); | ||
| 4 | +const xlib = @import("../x11/xlib.zig"); | ||
| 5 | + | ||
| 6 | +const Client = client_mod.Client; | ||
| 7 | +const Monitor = monitor_mod.Monitor; | ||
| 8 | + | ||
| 9 | +pub const layout = monitor_mod.Layout{ | ||
| 10 | + .symbol = "[]=", | ||
| 11 | + .arrange_fn = tile, | ||
| 12 | +}; | ||
| 13 | + | ||
| 14 | +pub var display_handle: ?*xlib.Display = null; | ||
| 15 | +pub var screen_width: i32 = 0; | ||
| 16 | +pub var screen_height: i32 = 0; | ||
| 17 | +pub var bar_height: i32 = 0; | ||
| 18 | + | ||
| 19 | +pub fn set_display(display: *xlib.Display) void { | ||
| 20 | + display_handle = display; | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +pub fn set_screen_size(width: i32, height: i32) void { | ||
| 24 | + screen_width = width; | ||
| 25 | + screen_height = height; | ||
| 26 | +} | ||
| 27 | + | ||
| 28 | +pub fn set_bar_height(height: i32) void { | ||
| 29 | + bar_height = height; | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +pub fn tile(monitor: *Monitor) void { | ||
| 33 | + var gap_outer_h: i32 = 0; | ||
| 34 | + var gap_outer_v: i32 = 0; | ||
| 35 | + var gap_inner_h: i32 = 0; | ||
| 36 | + var gap_inner_v: i32 = 0; | ||
| 37 | + var client_count: u32 = 0; | ||
| 38 | + | ||
| 39 | + get_gaps(monitor, &gap_outer_h, &gap_outer_v, &gap_inner_h, &gap_inner_v, &client_count); | ||
| 40 | + if (client_count == 0) return; | ||
| 41 | + | ||
| 42 | + const nmaster: i32 = monitor.nmaster; | ||
| 43 | + const nmaster_count: u32 = @intCast(@max(0, nmaster)); | ||
| 44 | + | ||
| 45 | + const master_x: i32 = monitor.win_x + gap_outer_v; | ||
| 46 | + var master_y: i32 = monitor.win_y + gap_outer_h; | ||
| 47 | + const master_height: i32 = monitor.win_h - 2 * gap_outer_h - gap_inner_h * (@as(i32, @intCast(@min(client_count, nmaster_count))) - 1); | ||
| 48 | + var master_width: i32 = monitor.win_w - 2 * gap_outer_v; | ||
| 49 | + | ||
| 50 | + var stack_x: i32 = master_x; | ||
| 51 | + var stack_y: i32 = monitor.win_y + gap_outer_h; | ||
| 52 | + const stack_height: i32 = monitor.win_h - 2 * gap_outer_h - gap_inner_h * (@as(i32, @intCast(client_count)) - nmaster - 1); | ||
| 53 | + var stack_width: i32 = master_width; | ||
| 54 | + | ||
| 55 | + if (nmaster > 0 and client_count > nmaster_count) { | ||
| 56 | + stack_width = @intFromFloat(@as(f32, @floatFromInt(master_width - gap_inner_v)) * (1.0 - monitor.mfact)); | ||
| 57 | + master_width = master_width - gap_inner_v - stack_width; | ||
| 58 | + stack_x = master_x + master_width + gap_inner_v; | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + var master_facts: f32 = 0; | ||
| 62 | + var stack_facts: f32 = 0; | ||
| 63 | + var master_rest: i32 = 0; | ||
| 64 | + var stack_rest: i32 = 0; | ||
| 65 | + get_facts(monitor, master_height, stack_height, &master_facts, &stack_facts, &master_rest, &stack_rest); | ||
| 66 | + | ||
| 67 | + var index: u32 = 0; | ||
| 68 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 69 | + while (current) |client| : (current = client_mod.next_tiled(client.next)) { | ||
| 70 | + if (index < nmaster_count) { | ||
| 71 | + const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_height)) / master_facts)) + (if (index < @as(u32, @intCast(master_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width; | ||
| 72 | + resize(client, master_x, master_y, master_width - 2 * client.border_width, height, false); | ||
| 73 | + master_y += get_client_height(client) + gap_inner_h; | ||
| 74 | + } else { | ||
| 75 | + const stack_index = index - nmaster_count; | ||
| 76 | + const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_height)) / stack_facts)) + (if (stack_index < @as(u32, @intCast(stack_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width; | ||
| 77 | + resize(client, stack_x, stack_y, stack_width - 2 * client.border_width, height, false); | ||
| 78 | + stack_y += get_client_height(client) + gap_inner_h; | ||
| 79 | + } | ||
| 80 | + index += 1; | ||
| 81 | + } | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +fn get_gaps(monitor: *Monitor, gap_outer_h: *i32, gap_outer_v: *i32, gap_inner_h: *i32, gap_inner_v: *i32, client_count: *u32) void { | ||
| 85 | + var count: u32 = 0; | ||
| 86 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 87 | + while (current) |client| : (current = client_mod.next_tiled(client.next)) { | ||
| 88 | + count += 1; | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + gap_outer_h.* = monitor.gap_outer_h; | ||
| 92 | + gap_outer_v.* = monitor.gap_outer_v; | ||
| 93 | + gap_inner_h.* = monitor.gap_inner_h; | ||
| 94 | + gap_inner_v.* = monitor.gap_inner_v; | ||
| 95 | + client_count.* = count; | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +fn get_facts(monitor: *Monitor, master_size: i32, stack_size: i32, master_factor: *f32, stack_factor: *f32, master_rest: *i32, stack_rest: *i32) void { | ||
| 99 | + var count: u32 = 0; | ||
| 100 | + var current = client_mod.next_tiled(monitor.clients); | ||
| 101 | + while (current) |client| : (current = client_mod.next_tiled(client.next)) { | ||
| 102 | + count += 1; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + const nmaster_count: u32 = @intCast(@max(0, monitor.nmaster)); | ||
| 106 | + const master_facts: f32 = @floatFromInt(@min(count, nmaster_count)); | ||
| 107 | + const stack_facts: f32 = @floatFromInt(if (count > nmaster_count) count - nmaster_count else 0); | ||
| 108 | + | ||
| 109 | + var master_total: i32 = 0; | ||
| 110 | + var stack_total: i32 = 0; | ||
| 111 | + | ||
| 112 | + if (master_facts > 0) { | ||
| 113 | + master_total = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_size)) / master_facts)) * @as(i32, @intFromFloat(master_facts)); | ||
| 114 | + } | ||
| 115 | + if (stack_facts > 0) { | ||
| 116 | + stack_total = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_size)) / stack_facts)) * @as(i32, @intFromFloat(stack_facts)); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + master_factor.* = master_facts; | ||
| 120 | + stack_factor.* = stack_facts; | ||
| 121 | + master_rest.* = master_size - master_total; | ||
| 122 | + stack_rest.* = stack_size - stack_total; | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +fn get_client_width(client: *Client) i32 { | ||
| 126 | + return client.width + 2 * client.border_width; | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +fn get_client_height(client: *Client) i32 { | ||
| 130 | + return client.height + 2 * client.border_width; | ||
| 131 | +} | ||
| 132 | + | ||
| 133 | +pub fn apply_size_hints(client: *Client, target_x: *i32, target_y: *i32, target_width: *i32, target_height: *i32, interact: bool) bool { | ||
| 134 | + const monitor = client.monitor orelse return false; | ||
| 135 | + | ||
| 136 | + target_width.* = @max(1, target_width.*); | ||
| 137 | + target_height.* = @max(1, target_height.*); | ||
| 138 | + | ||
| 139 | + if (interact) { | ||
| 140 | + if (target_x.* > screen_width) { | ||
| 141 | + target_x.* = screen_width - get_client_width(client); | ||
| 142 | + } | ||
| 143 | + if (target_y.* > screen_height) { | ||
| 144 | + target_y.* = screen_height - get_client_height(client); | ||
| 145 | + } | ||
| 146 | + if (target_x.* + target_width.* + 2 * client.border_width < 0) { | ||
| 147 | + target_x.* = 0; | ||
| 148 | + } | ||
| 149 | + if (target_y.* + target_height.* + 2 * client.border_width < 0) { | ||
| 150 | + target_y.* = 0; | ||
| 151 | + } | ||
| 152 | + } else { | ||
| 153 | + if (target_x.* >= monitor.win_x + monitor.win_w) { | ||
| 154 | + target_x.* = monitor.win_x + monitor.win_w - get_client_width(client); | ||
| 155 | + } | ||
| 156 | + if (target_y.* >= monitor.win_y + monitor.win_h) { | ||
| 157 | + target_y.* = monitor.win_y + monitor.win_h - get_client_height(client); | ||
| 158 | + } | ||
| 159 | + if (target_x.* + target_width.* + 2 * client.border_width <= monitor.win_x) { | ||
| 160 | + target_x.* = monitor.win_x; | ||
| 161 | + } | ||
| 162 | + if (target_y.* + target_height.* + 2 * client.border_width <= monitor.win_y) { | ||
| 163 | + target_y.* = monitor.win_y; | ||
| 164 | + } | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + if (target_height.* < bar_height) { | ||
| 168 | + target_height.* = bar_height; | ||
| 169 | + } | ||
| 170 | + if (target_width.* < bar_height) { | ||
| 171 | + target_width.* = bar_height; | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + if (client.is_floating or monitor.lt[monitor.sel_lt] == null) { | ||
| 175 | + const base_is_min = client.base_width == client.min_width and client.base_height == client.min_height; | ||
| 176 | + | ||
| 177 | + var adjusted_width = target_width.*; | ||
| 178 | + var adjusted_height = target_height.*; | ||
| 179 | + | ||
| 180 | + if (!base_is_min) { | ||
| 181 | + adjusted_width -= client.base_width; | ||
| 182 | + adjusted_height -= client.base_height; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + if (client.min_aspect > 0 and client.max_aspect > 0) { | ||
| 186 | + const width_float: f32 = @floatFromInt(adjusted_width); | ||
| 187 | + const height_float: f32 = @floatFromInt(adjusted_height); | ||
| 188 | + if (client.max_aspect < width_float / height_float) { | ||
| 189 | + adjusted_width = @intFromFloat(height_float * client.max_aspect + 0.5); | ||
| 190 | + } else if (client.min_aspect < height_float / width_float) { | ||
| 191 | + adjusted_height = @intFromFloat(width_float * client.min_aspect + 0.5); | ||
| 192 | + } | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + if (base_is_min) { | ||
| 196 | + adjusted_width -= client.base_width; | ||
| 197 | + adjusted_height -= client.base_height; | ||
| 198 | + } | ||
| 199 | + | ||
| 200 | + if (client.increment_width > 0) { | ||
| 201 | + adjusted_width -= @mod(adjusted_width, client.increment_width); | ||
| 202 | + } | ||
| 203 | + if (client.increment_height > 0) { | ||
| 204 | + adjusted_height -= @mod(adjusted_height, client.increment_height); | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + target_width.* = @max(adjusted_width + client.base_width, client.min_width); | ||
| 208 | + target_height.* = @max(adjusted_height + client.base_height, client.min_height); | ||
| 209 | + | ||
| 210 | + if (client.max_width > 0) { | ||
| 211 | + target_width.* = @min(target_width.*, client.max_width); | ||
| 212 | + } | ||
| 213 | + if (client.max_height > 0) { | ||
| 214 | + target_height.* = @min(target_height.*, client.max_height); | ||
| 215 | + } | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + return target_x.* != client.x or target_y.* != client.y or target_width.* != client.width or target_height.* != client.height; | ||
| 219 | +} | ||
| 220 | + | ||
| 221 | +pub fn resize(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32, interact: bool) void { | ||
| 222 | + var final_x = target_x; | ||
| 223 | + var final_y = target_y; | ||
| 224 | + var final_width = target_width; | ||
| 225 | + var final_height = target_height; | ||
| 226 | + | ||
| 227 | + if (apply_size_hints(client, &final_x, &final_y, &final_width, &final_height, interact)) { | ||
| 228 | + resize_client(client, final_x, final_y, final_width, final_height); | ||
| 229 | + } | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +pub fn resize_client(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32) void { | ||
| 233 | + client.old_x = client.x; | ||
| 234 | + client.old_y = client.y; | ||
| 235 | + client.old_width = client.width; | ||
| 236 | + client.old_height = client.height; | ||
| 237 | + client.x = target_x; | ||
| 238 | + client.y = target_y; | ||
| 239 | + client.width = target_width; | ||
| 240 | + client.height = target_height; | ||
| 241 | + | ||
| 242 | + const display = display_handle orelse return; | ||
| 243 | + | ||
| 244 | + var window_changes: xlib.c.XWindowChanges = undefined; | ||
| 245 | + window_changes.x = target_x; | ||
| 246 | + window_changes.y = target_y; | ||
| 247 | + window_changes.width = @intCast(@max(1, target_width)); | ||
| 248 | + window_changes.height = @intCast(@max(1, target_height)); | ||
| 249 | + window_changes.border_width = client.border_width; | ||
| 250 | + | ||
| 251 | + _ = xlib.c.XConfigureWindow( | ||
| 252 | + display, | ||
| 253 | + client.window, | ||
| 254 | + xlib.c.CWX | xlib.c.CWY | xlib.c.CWWidth | xlib.c.CWHeight | xlib.c.CWBorderWidth, | ||
| 255 | + &window_changes, | ||
| 256 | + ); | ||
| 257 | + | ||
| 258 | + send_configure(client); | ||
| 259 | + _ = xlib.XSync(display, xlib.False); | ||
| 260 | +} | ||
| 261 | + | ||
| 262 | +pub fn send_configure(client: *Client) void { | ||
| 263 | + const display = display_handle orelse return; | ||
| 264 | + | ||
| 265 | + var configure_event: xlib.c.XConfigureEvent = undefined; | ||
| 266 | + configure_event.type = xlib.c.ConfigureNotify; | ||
| 267 | + configure_event.display = display; | ||
| 268 | + configure_event.event = client.window; | ||
| 269 | + configure_event.window = client.window; | ||
| 270 | + configure_event.x = client.x; | ||
| 271 | + configure_event.y = client.y; | ||
| 272 | + configure_event.width = client.width; | ||
| 273 | + configure_event.height = client.height; | ||
| 274 | + configure_event.border_width = client.border_width; | ||
| 275 | + configure_event.above = xlib.None; | ||
| 276 | + configure_event.override_redirect = xlib.False; | ||
| 277 | + | ||
| 278 | + _ = xlib.c.XSendEvent(display, client.window, xlib.False, xlib.StructureNotifyMask, @ptrCast(&configure_event)); | ||
| 279 | +} | ||
+0 −362 src/lib.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,362 +0,0 @@ | |||
| 1 | -use std::path::PathBuf; | ||
| 2 | - | ||
| 3 | -pub mod animations; | ||
| 4 | -pub mod bar; | ||
| 5 | -pub mod client; | ||
| 6 | -pub mod config; | ||
| 7 | -pub mod errors; | ||
| 8 | -pub mod keyboard; | ||
| 9 | -pub mod layout; | ||
| 10 | -pub mod monitor; | ||
| 11 | -pub mod overlay; | ||
| 12 | -pub mod signal; | ||
| 13 | -pub mod size_hints; | ||
| 14 | -pub mod tab_bar; | ||
| 15 | -pub mod window_manager; | ||
| 16 | - | ||
| 17 | -pub mod prelude { | ||
| 18 | - pub use crate::ColorScheme; | ||
| 19 | - pub use crate::LayoutSymbolOverride; | ||
| 20 | - pub use crate::WindowRule; | ||
| 21 | - pub use crate::bar::{BlockCommand, BlockConfig}; | ||
| 22 | - pub use crate::keyboard::{Arg, KeyAction, handlers::KeyBinding, keysyms}; | ||
| 23 | - pub use x11rb::protocol::xproto::KeyButMask; | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -#[derive(Debug, Clone)] | ||
| 27 | -pub struct LayoutSymbolOverride { | ||
| 28 | - pub name: String, | ||
| 29 | - pub symbol: String, | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -#[derive(Debug, Clone)] | ||
| 33 | -pub struct WindowRule { | ||
| 34 | - pub class: Option<String>, | ||
| 35 | - pub instance: Option<String>, | ||
| 36 | - pub title: Option<String>, | ||
| 37 | - pub tags: Option<u32>, | ||
| 38 | - pub focus: Option<bool>, | ||
| 39 | - pub is_floating: Option<bool>, | ||
| 40 | - pub monitor: Option<usize>, | ||
| 41 | -} | ||
| 42 | - | ||
| 43 | -impl WindowRule { | ||
| 44 | - pub fn matches(&self, class: &str, instance: &str, title: &str) -> bool { | ||
| 45 | - let class_matches = self | ||
| 46 | - .class | ||
| 47 | - .as_ref() | ||
| 48 | - .is_none_or(|c| class.contains(c.as_str())); | ||
| 49 | - let instance_matches = self | ||
| 50 | - .instance | ||
| 51 | - .as_ref() | ||
| 52 | - .is_none_or(|i| instance.contains(i.as_str())); | ||
| 53 | - let title_matches = self | ||
| 54 | - .title | ||
| 55 | - .as_ref() | ||
| 56 | - .is_none_or(|t| title.contains(t.as_str())); | ||
| 57 | - class_matches && instance_matches && title_matches | ||
| 58 | - } | ||
| 59 | -} | ||
| 60 | - | ||
| 61 | -#[derive(Debug, Clone)] | ||
| 62 | -pub struct Config { | ||
| 63 | - // Meta | ||
| 64 | - pub path: Option<PathBuf>, | ||
| 65 | - | ||
| 66 | - // Appearance | ||
| 67 | - pub border_width: u32, | ||
| 68 | - pub border_focused: u32, | ||
| 69 | - pub border_unfocused: u32, | ||
| 70 | - pub font: String, | ||
| 71 | - | ||
| 72 | - // Gaps | ||
| 73 | - pub gaps_enabled: bool, | ||
| 74 | - pub smartgaps_enabled: bool, | ||
| 75 | - pub gap_inner_horizontal: u32, | ||
| 76 | - pub gap_inner_vertical: u32, | ||
| 77 | - pub gap_outer_horizontal: u32, | ||
| 78 | - pub gap_outer_vertical: u32, | ||
| 79 | - | ||
| 80 | - // Basics | ||
| 81 | - pub terminal: String, | ||
| 82 | - pub modkey: x11rb::protocol::xproto::KeyButMask, | ||
| 83 | - | ||
| 84 | - // Tags | ||
| 85 | - pub tags: Vec<String>, | ||
| 86 | - | ||
| 87 | - // Layout symbol overrides | ||
| 88 | - pub layout_symbols: Vec<LayoutSymbolOverride>, | ||
| 89 | - | ||
| 90 | - // Keybindings | ||
| 91 | - pub keybindings: Vec<crate::keyboard::handlers::Key>, | ||
| 92 | - pub tag_back_and_forth: bool, | ||
| 93 | - | ||
| 94 | - // Window rules | ||
| 95 | - pub window_rules: Vec<WindowRule>, | ||
| 96 | - | ||
| 97 | - // Status bar | ||
| 98 | - pub status_blocks: Vec<crate::bar::BlockConfig>, | ||
| 99 | - | ||
| 100 | - // Bar color schemes | ||
| 101 | - pub scheme_normal: ColorScheme, | ||
| 102 | - pub scheme_occupied: ColorScheme, | ||
| 103 | - pub scheme_selected: ColorScheme, | ||
| 104 | - pub scheme_urgent: ColorScheme, | ||
| 105 | - | ||
| 106 | - pub autostart: Vec<String>, | ||
| 107 | - pub auto_tile: bool, | ||
| 108 | - pub hide_vacant_tags: bool, | ||
| 109 | -} | ||
| 110 | - | ||
| 111 | -#[derive(Debug, Clone, Copy)] | ||
| 112 | -pub struct ColorScheme { | ||
| 113 | - pub foreground: u32, | ||
| 114 | - pub background: u32, | ||
| 115 | - pub underline: u32, | ||
| 116 | -} | ||
| 117 | - | ||
| 118 | -impl Default for Config { | ||
| 119 | - fn default() -> Self { | ||
| 120 | - use crate::keyboard::handlers::KeyBinding; | ||
| 121 | - use crate::keyboard::{Arg, KeyAction, keysyms}; | ||
| 122 | - use x11rb::protocol::xproto::KeyButMask; | ||
| 123 | - | ||
| 124 | - const MODKEY: KeyButMask = KeyButMask::MOD4; | ||
| 125 | - const SHIFT: KeyButMask = KeyButMask::SHIFT; | ||
| 126 | - | ||
| 127 | - const TERMINAL: &str = "st"; | ||
| 128 | - | ||
| 129 | - Self { | ||
| 130 | - path: None, | ||
| 131 | - border_width: 2, | ||
| 132 | - border_focused: 0x6dade3, | ||
| 133 | - border_unfocused: 0xbbbbbb, | ||
| 134 | - font: "monospace:size=10".to_string(), | ||
| 135 | - gaps_enabled: false, | ||
| 136 | - smartgaps_enabled: true, | ||
| 137 | - gap_inner_horizontal: 0, | ||
| 138 | - gap_inner_vertical: 0, | ||
| 139 | - gap_outer_horizontal: 0, | ||
| 140 | - gap_outer_vertical: 0, | ||
| 141 | - terminal: TERMINAL.to_string(), | ||
| 142 | - modkey: MODKEY, | ||
| 143 | - tags: vec!["1", "2", "3", "4", "5", "6", "7", "8", "9"] | ||
| 144 | - .into_iter() | ||
| 145 | - .map(String::from) | ||
| 146 | - .collect(), | ||
| 147 | - layout_symbols: vec![], | ||
| 148 | - keybindings: vec![ | ||
| 149 | - KeyBinding::single_key( | ||
| 150 | - vec![MODKEY], | ||
| 151 | - keysyms::XK_RETURN, | ||
| 152 | - KeyAction::Spawn, | ||
| 153 | - Arg::Str(TERMINAL.to_string()), | ||
| 154 | - ), | ||
| 155 | - KeyBinding::single_key( | ||
| 156 | - vec![MODKEY], | ||
| 157 | - keysyms::XK_D, | ||
| 158 | - KeyAction::Spawn, | ||
| 159 | - Arg::Array(vec![ | ||
| 160 | - "sh".to_string(), | ||
| 161 | - "-c".to_string(), | ||
| 162 | - "dmenu_run -l 10".to_string(), | ||
| 163 | - ]), | ||
| 164 | - ), | ||
| 165 | - KeyBinding::single_key( | ||
| 166 | - vec![MODKEY], | ||
| 167 | - keysyms::XK_Q, | ||
| 168 | - KeyAction::KillClient, | ||
| 169 | - Arg::None, | ||
| 170 | - ), | ||
| 171 | - KeyBinding::single_key( | ||
| 172 | - vec![MODKEY], | ||
| 173 | - keysyms::XK_N, | ||
| 174 | - KeyAction::CycleLayout, | ||
| 175 | - Arg::None, | ||
| 176 | - ), | ||
| 177 | - KeyBinding::single_key( | ||
| 178 | - vec![MODKEY, SHIFT], | ||
| 179 | - keysyms::XK_F, | ||
| 180 | - KeyAction::ToggleFullScreen, | ||
| 181 | - Arg::None, | ||
| 182 | - ), | ||
| 183 | - KeyBinding::single_key( | ||
| 184 | - vec![MODKEY], | ||
| 185 | - keysyms::XK_A, | ||
| 186 | - KeyAction::ToggleGaps, | ||
| 187 | - Arg::None, | ||
| 188 | - ), | ||
| 189 | - KeyBinding::single_key( | ||
| 190 | - vec![MODKEY, SHIFT], | ||
| 191 | - keysyms::XK_Q, | ||
| 192 | - KeyAction::Quit, | ||
| 193 | - Arg::None, | ||
| 194 | - ), | ||
| 195 | - KeyBinding::single_key( | ||
| 196 | - vec![MODKEY, SHIFT], | ||
| 197 | - keysyms::XK_R, | ||
| 198 | - KeyAction::Restart, | ||
| 199 | - Arg::None, | ||
| 200 | - ), | ||
| 201 | - KeyBinding::single_key( | ||
| 202 | - vec![MODKEY], | ||
| 203 | - keysyms::XK_F, | ||
| 204 | - KeyAction::ToggleFloating, | ||
| 205 | - Arg::None, | ||
| 206 | - ), | ||
| 207 | - KeyBinding::single_key( | ||
| 208 | - vec![MODKEY], | ||
| 209 | - keysyms::XK_J, | ||
| 210 | - KeyAction::FocusStack, | ||
| 211 | - Arg::Int(-1), | ||
| 212 | - ), | ||
| 213 | - KeyBinding::single_key( | ||
| 214 | - vec![MODKEY], | ||
| 215 | - keysyms::XK_K, | ||
| 216 | - KeyAction::FocusStack, | ||
| 217 | - Arg::Int(1), | ||
| 218 | - ), | ||
| 219 | - KeyBinding::single_key( | ||
| 220 | - vec![MODKEY], | ||
| 221 | - keysyms::XK_1, | ||
| 222 | - KeyAction::ViewTag, | ||
| 223 | - Arg::Int(0), | ||
| 224 | - ), | ||
| 225 | - KeyBinding::single_key( | ||
| 226 | - vec![MODKEY], | ||
| 227 | - keysyms::XK_2, | ||
| 228 | - KeyAction::ViewTag, | ||
| 229 | - Arg::Int(1), | ||
| 230 | - ), | ||
| 231 | - KeyBinding::single_key( | ||
| 232 | - vec![MODKEY], | ||
| 233 | - keysyms::XK_3, | ||
| 234 | - KeyAction::ViewTag, | ||
| 235 | - Arg::Int(2), | ||
| 236 | - ), | ||
| 237 | - KeyBinding::single_key( | ||
| 238 | - vec![MODKEY], | ||
| 239 | - keysyms::XK_4, | ||
| 240 | - KeyAction::ViewTag, | ||
| 241 | - Arg::Int(3), | ||
| 242 | - ), | ||
| 243 | - KeyBinding::single_key( | ||
| 244 | - vec![MODKEY], | ||
| 245 | - keysyms::XK_5, | ||
| 246 | - KeyAction::ViewTag, | ||
| 247 | - Arg::Int(4), | ||
| 248 | - ), | ||
| 249 | - KeyBinding::single_key( | ||
| 250 | - vec![MODKEY], | ||
| 251 | - keysyms::XK_6, | ||
| 252 | - KeyAction::ViewTag, | ||
| 253 | - Arg::Int(5), | ||
| 254 | - ), | ||
| 255 | - KeyBinding::single_key( | ||
| 256 | - vec![MODKEY], | ||
| 257 | - keysyms::XK_7, | ||
| 258 | - KeyAction::ViewTag, | ||
| 259 | - Arg::Int(6), | ||
| 260 | - ), | ||
| 261 | - KeyBinding::single_key( | ||
| 262 | - vec![MODKEY], | ||
| 263 | - keysyms::XK_8, | ||
| 264 | - KeyAction::ViewTag, | ||
| 265 | - Arg::Int(7), | ||
| 266 | - ), | ||
| 267 | - KeyBinding::single_key( | ||
| 268 | - vec![MODKEY], | ||
| 269 | - keysyms::XK_9, | ||
| 270 | - KeyAction::ViewTag, | ||
| 271 | - Arg::Int(8), | ||
| 272 | - ), | ||
| 273 | - KeyBinding::single_key( | ||
| 274 | - vec![MODKEY, SHIFT], | ||
| 275 | - keysyms::XK_1, | ||
| 276 | - KeyAction::MoveToTag, | ||
| 277 | - Arg::Int(0), | ||
| 278 | - ), | ||
| 279 | - KeyBinding::single_key( | ||
| 280 | - vec![MODKEY, SHIFT], | ||
| 281 | - keysyms::XK_2, | ||
| 282 | - KeyAction::MoveToTag, | ||
| 283 | - Arg::Int(1), | ||
| 284 | - ), | ||
| 285 | - KeyBinding::single_key( | ||
| 286 | - vec![MODKEY, SHIFT], | ||
| 287 | - keysyms::XK_3, | ||
| 288 | - KeyAction::MoveToTag, | ||
| 289 | - Arg::Int(2), | ||
| 290 | - ), | ||
| 291 | - KeyBinding::single_key( | ||
| 292 | - vec![MODKEY, SHIFT], | ||
| 293 | - keysyms::XK_4, | ||
| 294 | - KeyAction::MoveToTag, | ||
| 295 | - Arg::Int(3), | ||
| 296 | - ), | ||
| 297 | - KeyBinding::single_key( | ||
| 298 | - vec![MODKEY, SHIFT], | ||
| 299 | - keysyms::XK_5, | ||
| 300 | - KeyAction::MoveToTag, | ||
| 301 | - Arg::Int(4), | ||
| 302 | - ), | ||
| 303 | - KeyBinding::single_key( | ||
| 304 | - vec![MODKEY, SHIFT], | ||
| 305 | - keysyms::XK_6, | ||
| 306 | - KeyAction::MoveToTag, | ||
| 307 | - Arg::Int(5), | ||
| 308 | - ), | ||
| 309 | - KeyBinding::single_key( | ||
| 310 | - vec![MODKEY, SHIFT], | ||
| 311 | - keysyms::XK_7, | ||
| 312 | - KeyAction::MoveToTag, | ||
| 313 | - Arg::Int(6), | ||
| 314 | - ), | ||
| 315 | - KeyBinding::single_key( | ||
| 316 | - vec![MODKEY, SHIFT], | ||
| 317 | - keysyms::XK_8, | ||
| 318 | - KeyAction::MoveToTag, | ||
| 319 | - Arg::Int(7), | ||
| 320 | - ), | ||
| 321 | - KeyBinding::single_key( | ||
| 322 | - vec![MODKEY, SHIFT], | ||
| 323 | - keysyms::XK_9, | ||
| 324 | - KeyAction::MoveToTag, | ||
| 325 | - Arg::Int(8), | ||
| 326 | - ), | ||
| 327 | - ], | ||
| 328 | - tag_back_and_forth: false, | ||
| 329 | - window_rules: vec![], | ||
| 330 | - status_blocks: vec![crate::bar::BlockConfig { | ||
| 331 | - format: "{}".to_string(), | ||
| 332 | - command: crate::bar::BlockCommand::DateTime("%a, %b %d - %-I:%M %P".to_string()), | ||
| 333 | - interval_secs: 1, | ||
| 334 | - color: 0x0db9d7, | ||
| 335 | - underline: true, | ||
| 336 | - }], | ||
| 337 | - scheme_normal: ColorScheme { | ||
| 338 | - foreground: 0xbbbbbb, | ||
| 339 | - background: 0x1a1b26, | ||
| 340 | - underline: 0x444444, | ||
| 341 | - }, | ||
| 342 | - scheme_occupied: ColorScheme { | ||
| 343 | - foreground: 0x0db9d7, | ||
| 344 | - background: 0x1a1b26, | ||
| 345 | - underline: 0x0db9d7, | ||
| 346 | - }, | ||
| 347 | - scheme_selected: ColorScheme { | ||
| 348 | - foreground: 0x0db9d7, | ||
| 349 | - background: 0x1a1b26, | ||
| 350 | - underline: 0xad8ee6, | ||
| 351 | - }, | ||
| 352 | - scheme_urgent: ColorScheme { | ||
| 353 | - foreground: 0xff5555, | ||
| 354 | - background: 0x1a1b26, | ||
| 355 | - underline: 0xff5555, | ||
| 356 | - }, | ||
| 357 | - autostart: vec![], | ||
| 358 | - auto_tile: false, | ||
| 359 | - hide_vacant_tags: false, | ||
| 360 | - } | ||
| 361 | - } | ||
| 362 | -} | ||
+2291 −0 src/main.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,2291 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const display_mod = @import("x11/display.zig"); | ||
| 3 | +const events = @import("x11/events.zig"); | ||
| 4 | +const xlib = @import("x11/xlib.zig"); | ||
| 5 | +const client_mod = @import("client.zig"); | ||
| 6 | +const monitor_mod = @import("monitor.zig"); | ||
| 7 | +const tiling = @import("layouts/tiling.zig"); | ||
| 8 | +const monocle = @import("layouts/monocle.zig"); | ||
| 9 | +const floating = @import("layouts/floating.zig"); | ||
| 10 | +const scrolling = @import("layouts/scrolling.zig"); | ||
| 11 | +const animations = @import("animations.zig"); | ||
| 12 | +const bar_mod = @import("bar/bar.zig"); | ||
| 13 | +const blocks_mod = @import("bar/blocks/blocks.zig"); | ||
| 14 | +const config_mod = @import("config/config.zig"); | ||
| 15 | +const lua = @import("config/lua.zig"); | ||
| 16 | + | ||
| 17 | +const Display = display_mod.Display; | ||
| 18 | +const Client = client_mod.Client; | ||
| 19 | +const Monitor = monitor_mod.Monitor; | ||
| 20 | + | ||
| 21 | +var running: bool = true; | ||
| 22 | +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 23 | + | ||
| 24 | +var wm_protocols: xlib.Atom = 0; | ||
| 25 | +var wm_delete: xlib.Atom = 0; | ||
| 26 | +var wm_state: xlib.Atom = 0; | ||
| 27 | +var wm_take_focus: xlib.Atom = 0; | ||
| 28 | + | ||
| 29 | +var net_supported: xlib.Atom = 0; | ||
| 30 | +var net_wm_name: xlib.Atom = 0; | ||
| 31 | +var net_wm_state: xlib.Atom = 0; | ||
| 32 | +var net_wm_check: xlib.Atom = 0; | ||
| 33 | +var net_wm_state_fullscreen: xlib.Atom = 0; | ||
| 34 | +var net_active_window: xlib.Atom = 0; | ||
| 35 | +var net_wm_window_type: xlib.Atom = 0; | ||
| 36 | +var net_wm_window_type_dialog: xlib.Atom = 0; | ||
| 37 | +var net_client_list: xlib.Atom = 0; | ||
| 38 | + | ||
| 39 | +var wm_check_window: xlib.Window = 0; | ||
| 40 | + | ||
| 41 | +var border_color_focused: c_ulong = 0x6dade3; | ||
| 42 | +var border_color_unfocused: c_ulong = 0x444444; | ||
| 43 | +var border_width: i32 = 2; | ||
| 44 | +var gap_outer_v: i32 = 5; | ||
| 45 | +var gap_outer_h: i32 = 5; | ||
| 46 | +var gap_inner_h: i32 = 5; | ||
| 47 | +var gap_inner_v: i32 = 5; | ||
| 48 | + | ||
| 49 | +var tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" }; | ||
| 50 | + | ||
| 51 | +var cursor_normal: xlib.Cursor = 0; | ||
| 52 | +var cursor_resize: xlib.Cursor = 0; | ||
| 53 | +var cursor_move: xlib.Cursor = 0; | ||
| 54 | + | ||
| 55 | +const NormalState: c_long = 1; | ||
| 56 | +const WithdrawnState: c_long = 0; | ||
| 57 | +const IconicState: c_long = 3; | ||
| 58 | +const IsViewable: c_int = 2; | ||
| 59 | +const snap_distance: i32 = 32; | ||
| 60 | + | ||
| 61 | +var numlock_mask: c_uint = 0; | ||
| 62 | + | ||
| 63 | +var config: config_mod.Config = undefined; | ||
| 64 | +var display_global: ?*Display = null; | ||
| 65 | +var config_path_global: ?[]const u8 = null; | ||
| 66 | + | ||
| 67 | +var scroll_animation: animations.Scroll_Animation = .{}; | ||
| 68 | +var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out }; | ||
| 69 | + | ||
| 70 | +pub fn main() !void { | ||
| 71 | + const allocator = gpa.allocator(); | ||
| 72 | + defer _ = gpa.deinit(); | ||
| 73 | + | ||
| 74 | + std.debug.print("oxwm starting\n", .{}); | ||
| 75 | + | ||
| 76 | + var config_path: ?[]const u8 = null; | ||
| 77 | + var args = std.process.args(); | ||
| 78 | + _ = args.skip(); | ||
| 79 | + while (args.next()) |arg| { | ||
| 80 | + if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) { | ||
| 81 | + config_path = args.next(); | ||
| 82 | + } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { | ||
| 83 | + std.debug.print("usage: oxwm [-c config.lua]\n", .{}); | ||
| 84 | + return; | ||
| 85 | + } | ||
| 86 | + } | ||
| 87 | + | ||
| 88 | + config = config_mod.Config.init(allocator); | ||
| 89 | + defer config.deinit(); | ||
| 90 | + config_mod.set_config(&config); | ||
| 91 | + | ||
| 92 | + if (lua.init(&config)) { | ||
| 93 | + const loaded = if (config_path) |path| | ||
| 94 | + lua.load_file(path) | ||
| 95 | + else | ||
| 96 | + lua.load_config(); | ||
| 97 | + | ||
| 98 | + if (loaded) { | ||
| 99 | + config_path_global = config_path; | ||
| 100 | + if (config_path) |path| { | ||
| 101 | + std.debug.print("loaded config from {s}\n", .{path}); | ||
| 102 | + } else { | ||
| 103 | + std.debug.print("loaded config from ~/.config/oxwm/config.lua\n", .{}); | ||
| 104 | + } | ||
| 105 | + apply_config_values(); | ||
| 106 | + } else { | ||
| 107 | + std.debug.print("no config found, using defaults\n", .{}); | ||
| 108 | + setup_default_keybinds(); | ||
| 109 | + } | ||
| 110 | + } else { | ||
| 111 | + std.debug.print("failed to init lua, using defaults\n", .{}); | ||
| 112 | + setup_default_keybinds(); | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + var display = Display.open() catch |err| { | ||
| 116 | + std.debug.print("failed to open display: {}\n", .{err}); | ||
| 117 | + return; | ||
| 118 | + }; | ||
| 119 | + defer display.close(); | ||
| 120 | + | ||
| 121 | + display_global = &display; | ||
| 122 | + | ||
| 123 | + std.debug.print("display opened: screen={d} root=0x{x}\n", .{ display.screen, display.root }); | ||
| 124 | + std.debug.print("screen size: {d}x{d}\n", .{ display.screen_width(), display.screen_height() }); | ||
| 125 | + | ||
| 126 | + display.become_window_manager() catch |err| { | ||
| 127 | + std.debug.print("failed to become window manager: {}\n", .{err}); | ||
| 128 | + return; | ||
| 129 | + }; | ||
| 130 | + | ||
| 131 | + std.debug.print("successfully became window manager\n", .{}); | ||
| 132 | + | ||
| 133 | + setup_atoms(&display); | ||
| 134 | + setup_cursors(&display); | ||
| 135 | + client_mod.init(allocator); | ||
| 136 | + monitor_mod.init(allocator); | ||
| 137 | + monitor_mod.set_root_window(display.root, display.handle); | ||
| 138 | + tiling.set_display(display.handle); | ||
| 139 | + tiling.set_screen_size(display.screen_width(), display.screen_height()); | ||
| 140 | + | ||
| 141 | + setup_monitors(&display); | ||
| 142 | + setup_bars(allocator, &display); | ||
| 143 | + grab_keybinds(&display); | ||
| 144 | + scan_existing_windows(&display); | ||
| 145 | + | ||
| 146 | + std.debug.print("entering event loop\n", .{}); | ||
| 147 | + run_event_loop(&display); | ||
| 148 | + | ||
| 149 | + lua.deinit(); | ||
| 150 | + std.debug.print("oxwm exiting\n", .{}); | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +fn setup_atoms(display: *Display) void { | ||
| 154 | + wm_protocols = xlib.XInternAtom(display.handle, "WM_PROTOCOLS", xlib.False); | ||
| 155 | + wm_delete = xlib.XInternAtom(display.handle, "WM_DELETE_WINDOW", xlib.False); | ||
| 156 | + wm_state = xlib.XInternAtom(display.handle, "WM_STATE", xlib.False); | ||
| 157 | + wm_take_focus = xlib.XInternAtom(display.handle, "WM_TAKE_FOCUS", xlib.False); | ||
| 158 | + | ||
| 159 | + net_active_window = xlib.XInternAtom(display.handle, "_NET_ACTIVE_WINDOW", xlib.False); | ||
| 160 | + net_supported = xlib.XInternAtom(display.handle, "_NET_SUPPORTED", xlib.False); | ||
| 161 | + net_wm_name = xlib.XInternAtom(display.handle, "_NET_WM_NAME", xlib.False); | ||
| 162 | + net_wm_state = xlib.XInternAtom(display.handle, "_NET_WM_STATE", xlib.False); | ||
| 163 | + net_wm_check = xlib.XInternAtom(display.handle, "_NET_SUPPORTING_WM_CHECK", xlib.False); | ||
| 164 | + net_wm_state_fullscreen = xlib.XInternAtom(display.handle, "_NET_WM_STATE_FULLSCREEN", xlib.False); | ||
| 165 | + net_wm_window_type = xlib.XInternAtom(display.handle, "_NET_WM_WINDOW_TYPE", xlib.False); | ||
| 166 | + net_wm_window_type_dialog = xlib.XInternAtom(display.handle, "_NET_WM_WINDOW_TYPE_DIALOG", xlib.False); | ||
| 167 | + net_client_list = xlib.XInternAtom(display.handle, "_NET_CLIENT_LIST", xlib.False); | ||
| 168 | + | ||
| 169 | + const utf8_string = xlib.XInternAtom(display.handle, "UTF8_STRING", xlib.False); | ||
| 170 | + | ||
| 171 | + wm_check_window = xlib.XCreateSimpleWindow(display.handle, display.root, 0, 0, 1, 1, 0, 0, 0); | ||
| 172 | + _ = xlib.XChangeProperty(display.handle, wm_check_window, net_wm_check, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&wm_check_window), 1); | ||
| 173 | + _ = xlib.XChangeProperty(display.handle, wm_check_window, net_wm_name, utf8_string, 8, xlib.PropModeReplace, "oxwm", 6); | ||
| 174 | + _ = xlib.XChangeProperty(display.handle, display.root, net_wm_check, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&wm_check_window), 1); | ||
| 175 | + | ||
| 176 | + var net_atoms = [_]xlib.Atom{ net_supported, net_wm_name, net_wm_state, net_wm_check, net_wm_state_fullscreen, net_active_window, net_wm_window_type, net_wm_window_type_dialog, net_client_list }; | ||
| 177 | + _ = xlib.XChangeProperty(display.handle, display.root, net_supported, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&net_atoms), net_atoms.len); | ||
| 178 | + | ||
| 179 | + _ = xlib.XDeleteProperty(display.handle, display.root, net_client_list); | ||
| 180 | + | ||
| 181 | + std.debug.print("atoms initialized with EWMH support\n", .{}); | ||
| 182 | +} | ||
| 183 | + | ||
| 184 | +fn setup_cursors(display: *Display) void { | ||
| 185 | + cursor_normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr); | ||
| 186 | + cursor_resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing); | ||
| 187 | + cursor_move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur); | ||
| 188 | + _ = xlib.XDefineCursor(display.handle, display.root, cursor_normal); | ||
| 189 | +} | ||
| 190 | + | ||
| 191 | +fn setup_bars(allocator: std.mem.Allocator, display: *Display) void { | ||
| 192 | + var current_monitor = monitor_mod.monitors; | ||
| 193 | + var last_bar: ?*bar_mod.Bar = null; | ||
| 194 | + | ||
| 195 | + while (current_monitor) |monitor| { | ||
| 196 | + const bar = bar_mod.Bar.create(allocator, display.handle, display.screen, monitor, config.font); | ||
| 197 | + if (bar) |created_bar| { | ||
| 198 | + if (tiling.bar_height == 0) { | ||
| 199 | + tiling.set_bar_height(created_bar.height); | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + if (config.blocks.items.len > 0) { | ||
| 203 | + for (config.blocks.items) |cfg_block| { | ||
| 204 | + const block = config_block_to_bar_block(cfg_block); | ||
| 205 | + created_bar.add_block(block); | ||
| 206 | + } | ||
| 207 | + } else { | ||
| 208 | + created_bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true)); | ||
| 209 | + created_bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false)); | ||
| 210 | + created_bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true)); | ||
| 211 | + } | ||
| 212 | + | ||
| 213 | + if (last_bar) |prev| { | ||
| 214 | + prev.next = created_bar; | ||
| 215 | + } else { | ||
| 216 | + bar_mod.bars = created_bar; | ||
| 217 | + } | ||
| 218 | + last_bar = created_bar; | ||
| 219 | + std.debug.print("bar created for monitor {d}\n", .{monitor.num}); | ||
| 220 | + } | ||
| 221 | + current_monitor = monitor.next; | ||
| 222 | + } | ||
| 223 | +} | ||
| 224 | + | ||
| 225 | +fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block { | ||
| 226 | + return switch (cfg.block_type) { | ||
| 227 | + .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline), | ||
| 228 | + .datetime => blocks_mod.Block.init_datetime(cfg.format, cfg.datetime_format orelse "%H:%M", cfg.interval, cfg.color, cfg.underline), | ||
| 229 | + .ram => blocks_mod.Block.init_ram(cfg.format, cfg.interval, cfg.color, cfg.underline), | ||
| 230 | + .shell => blocks_mod.Block.init_shell(cfg.format, cfg.command orelse "", cfg.interval, cfg.color, cfg.underline), | ||
| 231 | + .battery => blocks_mod.Block.init_battery( | ||
| 232 | + cfg.format_charging orelse "", | ||
| 233 | + cfg.format_discharging orelse "", | ||
| 234 | + cfg.format_full orelse "", | ||
| 235 | + cfg.battery_name orelse "BAT0", | ||
| 236 | + cfg.interval, | ||
| 237 | + cfg.color, | ||
| 238 | + cfg.underline, | ||
| 239 | + ), | ||
| 240 | + .cpu_temp => blocks_mod.Block.init_cpu_temp( | ||
| 241 | + cfg.format, | ||
| 242 | + cfg.thermal_zone orelse "thermal_zone0", | ||
| 243 | + cfg.interval, | ||
| 244 | + cfg.color, | ||
| 245 | + cfg.underline, | ||
| 246 | + ), | ||
| 247 | + }; | ||
| 248 | +} | ||
| 249 | + | ||
| 250 | +fn setup_monitors(display: *Display) void { | ||
| 251 | + std.debug.print("checking xinerama...\n", .{}); | ||
| 252 | + if (xlib.XineramaIsActive(display.handle) != 0) { | ||
| 253 | + std.debug.print("xinerama is active!\n", .{}); | ||
| 254 | + var screen_count: c_int = 0; | ||
| 255 | + const screens = xlib.XineramaQueryScreens(display.handle, &screen_count); | ||
| 256 | + | ||
| 257 | + if (screen_count > 0 and screens != null) { | ||
| 258 | + var prev_monitor: ?*Monitor = null; | ||
| 259 | + var index: usize = 0; | ||
| 260 | + | ||
| 261 | + while (index < @as(usize, @intCast(screen_count))) : (index += 1) { | ||
| 262 | + const screen = screens[index]; | ||
| 263 | + const mon = monitor_mod.create() orelse continue; | ||
| 264 | + | ||
| 265 | + mon.num = @intCast(index); | ||
| 266 | + mon.mon_x = screen.x_org; | ||
| 267 | + mon.mon_y = screen.y_org; | ||
| 268 | + mon.mon_w = screen.width; | ||
| 269 | + mon.mon_h = screen.height; | ||
| 270 | + mon.win_x = screen.x_org; | ||
| 271 | + mon.win_y = screen.y_org; | ||
| 272 | + mon.win_w = screen.width; | ||
| 273 | + mon.win_h = screen.height; | ||
| 274 | + mon.lt[0] = &tiling.layout; | ||
| 275 | + mon.lt[1] = &monocle.layout; | ||
| 276 | + mon.lt[2] = &floating.layout; | ||
| 277 | + mon.lt[3] = &scrolling.layout; | ||
| 278 | + for (0..10) |i| { | ||
| 279 | + mon.pertag.ltidxs[i][0] = mon.lt[0]; | ||
| 280 | + mon.pertag.ltidxs[i][1] = mon.lt[1]; | ||
| 281 | + mon.pertag.ltidxs[i][2] = mon.lt[2]; | ||
| 282 | + mon.pertag.ltidxs[i][3] = mon.lt[3]; | ||
| 283 | + } | ||
| 284 | + | ||
| 285 | + if (prev_monitor) |prev| { | ||
| 286 | + prev.next = mon; | ||
| 287 | + } else { | ||
| 288 | + monitor_mod.monitors = mon; | ||
| 289 | + } | ||
| 290 | + prev_monitor = mon; | ||
| 291 | + | ||
| 292 | + std.debug.print("monitor {d}: {d}x{d} at ({d},{d})\n", .{ index, mon.mon_w, mon.mon_h, mon.mon_x, mon.mon_y }); | ||
| 293 | + } | ||
| 294 | + | ||
| 295 | + monitor_mod.selected_monitor = monitor_mod.monitors; | ||
| 296 | + _ = xlib.XFree(@ptrCast(screens)); | ||
| 297 | + return; | ||
| 298 | + } | ||
| 299 | + } else { | ||
| 300 | + std.debug.print("xinerama not active, using single monitor\n", .{}); | ||
| 301 | + } | ||
| 302 | + | ||
| 303 | + const mon = monitor_mod.create() orelse return; | ||
| 304 | + mon.mon_x = 0; | ||
| 305 | + mon.mon_y = 0; | ||
| 306 | + mon.mon_w = display.screen_width(); | ||
| 307 | + mon.mon_h = display.screen_height(); | ||
| 308 | + mon.win_x = 0; | ||
| 309 | + mon.win_y = 0; | ||
| 310 | + mon.win_w = display.screen_width(); | ||
| 311 | + mon.win_h = display.screen_height(); | ||
| 312 | + mon.lt[0] = &tiling.layout; | ||
| 313 | + mon.lt[1] = &monocle.layout; | ||
| 314 | + mon.lt[2] = &floating.layout; | ||
| 315 | + mon.lt[3] = &scrolling.layout; | ||
| 316 | + for (0..10) |i| { | ||
| 317 | + mon.pertag.ltidxs[i][0] = mon.lt[0]; | ||
| 318 | + mon.pertag.ltidxs[i][1] = mon.lt[1]; | ||
| 319 | + mon.pertag.ltidxs[i][2] = mon.lt[2]; | ||
| 320 | + mon.pertag.ltidxs[i][3] = mon.lt[3]; | ||
| 321 | + } | ||
| 322 | + monitor_mod.monitors = mon; | ||
| 323 | + monitor_mod.selected_monitor = mon; | ||
| 324 | + std.debug.print("monitor created: {d}x{d}\n", .{ mon.mon_w, mon.mon_h }); | ||
| 325 | +} | ||
| 326 | + | ||
| 327 | +fn apply_config_values() void { | ||
| 328 | + border_color_focused = config.border_focused; | ||
| 329 | + border_color_unfocused = config.border_unfocused; | ||
| 330 | + border_width = config.border_width; | ||
| 331 | + gap_inner_h = config.gap_inner_h; | ||
| 332 | + gap_inner_v = config.gap_inner_v; | ||
| 333 | + gap_outer_h = config.gap_outer_h; | ||
| 334 | + gap_outer_v = config.gap_outer_v; | ||
| 335 | + tags = config.tags; | ||
| 336 | +} | ||
| 337 | + | ||
| 338 | +fn setup_default_keybinds() void { | ||
| 339 | + const mod_key: u32 = 1 << 6; | ||
| 340 | + const shift_key: u32 = 1 << 0; | ||
| 341 | + const control_key: u32 = 1 << 2; | ||
| 342 | + | ||
| 343 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0xff0d, .action = .spawn_terminal }) catch {}; | ||
| 344 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'd', .action = .spawn, .str_arg = "rofi -show drun" }) catch {}; | ||
| 345 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 's', .action = .spawn, .str_arg = "maim -s | xclip -selection clipboard -t image/png" }) catch {}; | ||
| 346 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'q', .action = .kill_client }) catch {}; | ||
| 347 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'q', .action = .quit }) catch {}; | ||
| 348 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'r', .action = .reload_config }) catch {}; | ||
| 349 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'j', .action = .focus_next }) catch {}; | ||
| 350 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'k', .action = .focus_prev }) catch {}; | ||
| 351 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'j', .action = .move_next }) catch {}; | ||
| 352 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 'k', .action = .move_prev }) catch {}; | ||
| 353 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'h', .action = .resize_master, .int_arg = -50 }) catch {}; | ||
| 354 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'l', .action = .resize_master, .int_arg = 50 }) catch {}; | ||
| 355 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'i', .action = .inc_master }) catch {}; | ||
| 356 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'p', .action = .dec_master }) catch {}; | ||
| 357 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'a', .action = .toggle_gaps }) catch {}; | ||
| 358 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'f', .action = .toggle_fullscreen }) catch {}; | ||
| 359 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0x0020, .action = .toggle_floating }) catch {}; | ||
| 360 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 'n', .action = .cycle_layout }) catch {}; | ||
| 361 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0x002c, .action = .focus_monitor, .int_arg = -1 }) catch {}; | ||
| 362 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = 0x002e, .action = .focus_monitor, .int_arg = 1 }) catch {}; | ||
| 363 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 0x002c, .action = .send_to_monitor, .int_arg = -1 }) catch {}; | ||
| 364 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = 0x002e, .action = .send_to_monitor, .int_arg = 1 }) catch {}; | ||
| 365 | + | ||
| 366 | + var tag_index: i32 = 0; | ||
| 367 | + while (tag_index < 9) : (tag_index += 1) { | ||
| 368 | + const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index)); | ||
| 369 | + config.add_keybind(.{ .mod_mask = mod_key, .keysym = keysym, .action = .view_tag, .int_arg = tag_index }) catch {}; | ||
| 370 | + config.add_keybind(.{ .mod_mask = mod_key | shift_key, .keysym = keysym, .action = .move_to_tag, .int_arg = tag_index }) catch {}; | ||
| 371 | + config.add_keybind(.{ .mod_mask = mod_key | control_key, .keysym = keysym, .action = .toggle_view_tag, .int_arg = tag_index }) catch {}; | ||
| 372 | + config.add_keybind(.{ .mod_mask = mod_key | control_key | shift_key, .keysym = keysym, .action = .toggle_tag, .int_arg = tag_index }) catch {}; | ||
| 373 | + } | ||
| 374 | +} | ||
| 375 | + | ||
| 376 | +fn grab_keybinds(display: *Display) void { | ||
| 377 | + update_numlock_mask(display); | ||
| 378 | + const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask }; | ||
| 379 | + | ||
| 380 | + _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root); | ||
| 381 | + | ||
| 382 | + for (config.keybinds.items) |keybind| { | ||
| 383 | + const keycode = xlib.XKeysymToKeycode(display.handle, @intCast(keybind.keysym)); | ||
| 384 | + if (keycode != 0) { | ||
| 385 | + for (modifiers) |modifier| { | ||
| 386 | + _ = xlib.XGrabKey( | ||
| 387 | + display.handle, | ||
| 388 | + keycode, | ||
| 389 | + keybind.mod_mask | modifier, | ||
| 390 | + display.root, | ||
| 391 | + xlib.True, | ||
| 392 | + xlib.GrabModeAsync, | ||
| 393 | + xlib.GrabModeAsync, | ||
| 394 | + ); | ||
| 395 | + } | ||
| 396 | + } | ||
| 397 | + } | ||
| 398 | + | ||
| 399 | + for (config.buttons.items) |button| { | ||
| 400 | + if (button.click == .client_win) { | ||
| 401 | + for (modifiers) |modifier| { | ||
| 402 | + _ = xlib.XGrabButton( | ||
| 403 | + display.handle, | ||
| 404 | + @intCast(button.button), | ||
| 405 | + button.mod_mask | modifier, | ||
| 406 | + display.root, | ||
| 407 | + xlib.True, | ||
| 408 | + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, | ||
| 409 | + xlib.GrabModeAsync, | ||
| 410 | + xlib.GrabModeAsync, | ||
| 411 | + xlib.None, | ||
| 412 | + xlib.None, | ||
| 413 | + ); | ||
| 414 | + } | ||
| 415 | + } | ||
| 416 | + } | ||
| 417 | + | ||
| 418 | + std.debug.print("grabbed {d} keybinds from config\n", .{config.keybinds.items.len}); | ||
| 419 | +} | ||
| 420 | + | ||
| 421 | +fn get_state(display: *Display, window: xlib.Window) c_long { | ||
| 422 | + var actual_type: xlib.Atom = 0; | ||
| 423 | + var actual_format: c_int = 0; | ||
| 424 | + var num_items: c_ulong = 0; | ||
| 425 | + var bytes_after: c_ulong = 0; | ||
| 426 | + var prop: [*c]u8 = null; | ||
| 427 | + | ||
| 428 | + const result = xlib.XGetWindowProperty( | ||
| 429 | + display.handle, | ||
| 430 | + window, | ||
| 431 | + wm_state, | ||
| 432 | + 0, | ||
| 433 | + 2, | ||
| 434 | + xlib.False, | ||
| 435 | + wm_state, | ||
| 436 | + &actual_type, | ||
| 437 | + &actual_format, | ||
| 438 | + &num_items, | ||
| 439 | + &bytes_after, | ||
| 440 | + &prop, | ||
| 441 | + ); | ||
| 442 | + | ||
| 443 | + if (result != 0 or actual_type != wm_state or num_items < 1) { | ||
| 444 | + if (prop != null) { | ||
| 445 | + _ = xlib.XFree(prop); | ||
| 446 | + } | ||
| 447 | + return WithdrawnState; | ||
| 448 | + } | ||
| 449 | + | ||
| 450 | + const state: c_long = @as(*c_long, @alignCast(@ptrCast(prop))).*; | ||
| 451 | + _ = xlib.XFree(prop); | ||
| 452 | + return state; | ||
| 453 | +} | ||
| 454 | + | ||
| 455 | +fn scan_existing_windows(display: *Display) void { | ||
| 456 | + var root_return: xlib.Window = undefined; | ||
| 457 | + var parent_return: xlib.Window = undefined; | ||
| 458 | + var children: [*c]xlib.Window = undefined; | ||
| 459 | + var num_children: c_uint = undefined; | ||
| 460 | + | ||
| 461 | + if (xlib.XQueryTree(display.handle, display.root, &root_return, &parent_return, &children, &num_children) == 0) { | ||
| 462 | + return; | ||
| 463 | + } | ||
| 464 | + | ||
| 465 | + var index: c_uint = 0; | ||
| 466 | + while (index < num_children) : (index += 1) { | ||
| 467 | + var window_attrs: xlib.XWindowAttributes = undefined; | ||
| 468 | + if (xlib.XGetWindowAttributes(display.handle, children[index], &window_attrs) == 0) { | ||
| 469 | + continue; | ||
| 470 | + } | ||
| 471 | + if (window_attrs.override_redirect != 0) { | ||
| 472 | + continue; | ||
| 473 | + } | ||
| 474 | + var trans: xlib.Window = 0; | ||
| 475 | + if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) { | ||
| 476 | + continue; | ||
| 477 | + } | ||
| 478 | + if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) { | ||
| 479 | + manage(display, children[index], &window_attrs); | ||
| 480 | + } | ||
| 481 | + } | ||
| 482 | + | ||
| 483 | + index = 0; | ||
| 484 | + while (index < num_children) : (index += 1) { | ||
| 485 | + var window_attrs: xlib.XWindowAttributes = undefined; | ||
| 486 | + if (xlib.XGetWindowAttributes(display.handle, children[index], &window_attrs) == 0) { | ||
| 487 | + continue; | ||
| 488 | + } | ||
| 489 | + var trans: xlib.Window = 0; | ||
| 490 | + if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) { | ||
| 491 | + if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) { | ||
| 492 | + manage(display, children[index], &window_attrs); | ||
| 493 | + } | ||
| 494 | + } | ||
| 495 | + } | ||
| 496 | + | ||
| 497 | + if (children != null) { | ||
| 498 | + _ = xlib.XFree(@ptrCast(children)); | ||
| 499 | + } | ||
| 500 | +} | ||
| 501 | + | ||
| 502 | +fn run_event_loop(display: *Display) void { | ||
| 503 | + const x11_fd = xlib.XConnectionNumber(display.handle); | ||
| 504 | + var fds = [_]std.posix.pollfd{ | ||
| 505 | + .{ .fd = x11_fd, .events = std.posix.POLL.IN, .revents = 0 }, | ||
| 506 | + }; | ||
| 507 | + | ||
| 508 | + _ = xlib.XSync(display.handle, xlib.False); | ||
| 509 | + | ||
| 510 | + while (running) { | ||
| 511 | + while (xlib.XPending(display.handle) > 0) { | ||
| 512 | + var event = display.next_event(); | ||
| 513 | + handle_event(display, &event); | ||
| 514 | + } | ||
| 515 | + | ||
| 516 | + tick_animations(); | ||
| 517 | + | ||
| 518 | + var current_bar = bar_mod.bars; | ||
| 519 | + while (current_bar) |bar| { | ||
| 520 | + bar.update_blocks(); | ||
| 521 | + bar.draw(display.handle, &tags); | ||
| 522 | + current_bar = bar.next; | ||
| 523 | + } | ||
| 524 | + | ||
| 525 | + const poll_timeout: i32 = if (scroll_animation.is_active()) 16 else 1000; | ||
| 526 | + _ = std.posix.poll(&fds, poll_timeout) catch 0; | ||
| 527 | + } | ||
| 528 | +} | ||
| 529 | + | ||
| 530 | +fn handle_event(display: *Display, event: *xlib.XEvent) void { | ||
| 531 | + const event_type = events.get_event_type(event); | ||
| 532 | + | ||
| 533 | + if (event_type == .button_press) { | ||
| 534 | + std.debug.print("EVENT: button_press received type={d}\n", .{event.type}); | ||
| 535 | + } | ||
| 536 | + | ||
| 537 | + switch (event_type) { | ||
| 538 | + .map_request => handle_map_request(display, &event.xmaprequest), | ||
| 539 | + .configure_request => handle_configure_request(display, &event.xconfigurerequest), | ||
| 540 | + .key_press => handle_key_press(display, &event.xkey), | ||
| 541 | + .destroy_notify => handle_destroy_notify(display, &event.xdestroywindow), | ||
| 542 | + .unmap_notify => handle_unmap_notify(display, &event.xunmap), | ||
| 543 | + .enter_notify => handle_enter_notify(display, &event.xcrossing), | ||
| 544 | + .focus_in => handle_focus_in(display, &event.xfocus), | ||
| 545 | + .motion_notify => handle_motion_notify(display, &event.xmotion), | ||
| 546 | + .client_message => handle_client_message(display, &event.xclient), | ||
| 547 | + .button_press => handle_button_press(display, &event.xbutton), | ||
| 548 | + .expose => handle_expose(display, &event.xexpose), | ||
| 549 | + .property_notify => handle_property_notify(display, &event.xproperty), | ||
| 550 | + else => {}, | ||
| 551 | + } | ||
| 552 | +} | ||
| 553 | + | ||
| 554 | +fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void { | ||
| 555 | + std.debug.print("map_request: window=0x{x}\n", .{event.window}); | ||
| 556 | + | ||
| 557 | + var window_attributes: xlib.XWindowAttributes = undefined; | ||
| 558 | + if (xlib.XGetWindowAttributes(display.handle, event.window, &window_attributes) == 0) { | ||
| 559 | + return; | ||
| 560 | + } | ||
| 561 | + if (window_attributes.override_redirect != 0) { | ||
| 562 | + return; | ||
| 563 | + } | ||
| 564 | + if (client_mod.window_to_client(event.window) != null) { | ||
| 565 | + return; | ||
| 566 | + } | ||
| 567 | + | ||
| 568 | + manage(display, event.window, &window_attributes); | ||
| 569 | +} | ||
| 570 | + | ||
| 571 | +fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes) void { | ||
| 572 | + const client = client_mod.create(win) orelse return; | ||
| 573 | + var trans: xlib.Window = 0; | ||
| 574 | + | ||
| 575 | + client.x = window_attrs.x; | ||
| 576 | + client.y = window_attrs.y; | ||
| 577 | + client.width = window_attrs.width; | ||
| 578 | + client.height = window_attrs.height; | ||
| 579 | + client.old_x = window_attrs.x; | ||
| 580 | + client.old_y = window_attrs.y; | ||
| 581 | + client.old_width = window_attrs.width; | ||
| 582 | + client.old_height = window_attrs.height; | ||
| 583 | + client.old_border_width = window_attrs.border_width; | ||
| 584 | + client.border_width = border_width; | ||
| 585 | + | ||
| 586 | + update_title(display, client); | ||
| 587 | + | ||
| 588 | + if (xlib.XGetTransientForHint(display.handle, win, &trans) != 0) { | ||
| 589 | + if (client_mod.window_to_client(trans)) |transient_client| { | ||
| 590 | + client.monitor = transient_client.monitor; | ||
| 591 | + client.tags = transient_client.tags; | ||
| 592 | + } | ||
| 593 | + } | ||
| 594 | + | ||
| 595 | + if (client.monitor == null) { | ||
| 596 | + client.monitor = monitor_mod.selected_monitor; | ||
| 597 | + apply_rules(display, client); | ||
| 598 | + } | ||
| 599 | + | ||
| 600 | + const monitor = client.monitor orelse return; | ||
| 601 | + | ||
| 602 | + if (client.x + client.width > monitor.win_x + monitor.win_w) { | ||
| 603 | + client.x = monitor.win_x + monitor.win_w - client.width - 2 * client.border_width; | ||
| 604 | + } | ||
| 605 | + if (client.y + client.height > monitor.win_y + monitor.win_h) { | ||
| 606 | + client.y = monitor.win_y + monitor.win_h - client.height - 2 * client.border_width; | ||
| 607 | + } | ||
| 608 | + client.x = @max(client.x, monitor.win_x); | ||
| 609 | + client.y = @max(client.y, monitor.win_y); | ||
| 610 | + | ||
| 611 | + _ = xlib.XSetWindowBorderWidth(display.handle, win, @intCast(client.border_width)); | ||
| 612 | + _ = xlib.XSetWindowBorder(display.handle, win, border_color_unfocused); | ||
| 613 | + tiling.send_configure(client); | ||
| 614 | + | ||
| 615 | + update_window_type(display, client); | ||
| 616 | + update_size_hints(display, client); | ||
| 617 | + update_wm_hints(display, client); | ||
| 618 | + | ||
| 619 | + _ = xlib.XSelectInput( | ||
| 620 | + display.handle, | ||
| 621 | + win, | ||
| 622 | + xlib.EnterWindowMask | xlib.FocusChangeMask | xlib.PropertyChangeMask | xlib.StructureNotifyMask, | ||
| 623 | + ); | ||
| 624 | + grabbuttons(display, client, false); | ||
| 625 | + | ||
| 626 | + if (!client.is_floating) { | ||
| 627 | + client.is_floating = trans != 0 or client.is_fixed; | ||
| 628 | + client.old_state = client.is_floating; | ||
| 629 | + } | ||
| 630 | + if (client.is_floating) { | ||
| 631 | + _ = xlib.XRaiseWindow(display.handle, client.window); | ||
| 632 | + } | ||
| 633 | + | ||
| 634 | + client_mod.attach_aside(client); | ||
| 635 | + client_mod.attach_stack(client); | ||
| 636 | + | ||
| 637 | + _ = xlib.XChangeProperty(display.handle, display.root, net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1); | ||
| 638 | + _ = xlib.XMoveResizeWindow(display.handle, client.window, client.x + 2 * display.screen_width(), client.y, @intCast(client.width), @intCast(client.height)); | ||
| 639 | + set_client_state(display, client, NormalState); | ||
| 640 | + | ||
| 641 | + if (client.monitor == monitor_mod.selected_monitor) { | ||
| 642 | + const selmon = monitor_mod.selected_monitor orelse return; | ||
| 643 | + unfocus_client(display, selmon.sel, false); | ||
| 644 | + } | ||
| 645 | + monitor.sel = client; | ||
| 646 | + | ||
| 647 | + if (is_scrolling_layout(monitor)) { | ||
| 648 | + monitor.scroll_offset = 0; | ||
| 649 | + } | ||
| 650 | + | ||
| 651 | + arrange(monitor); | ||
| 652 | + _ = xlib.XMapWindow(display.handle, win); | ||
| 653 | + focus(display, null); | ||
| 654 | +} | ||
| 655 | + | ||
| 656 | +fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent) void { | ||
| 657 | + const client = client_mod.window_to_client(event.window); | ||
| 658 | + | ||
| 659 | + if (client) |managed_client| { | ||
| 660 | + if ((event.value_mask & xlib.c.CWBorderWidth) != 0) { | ||
| 661 | + managed_client.border_width = event.border_width; | ||
| 662 | + } else if (managed_client.is_floating or (managed_client.monitor != null and managed_client.monitor.?.lt[managed_client.monitor.?.sel_lt] == null)) { | ||
| 663 | + const monitor = managed_client.monitor orelse return; | ||
| 664 | + if ((event.value_mask & xlib.c.CWX) != 0) { | ||
| 665 | + managed_client.old_x = managed_client.x; | ||
| 666 | + managed_client.x = monitor.mon_x + event.x; | ||
| 667 | + } | ||
| 668 | + if ((event.value_mask & xlib.c.CWY) != 0) { | ||
| 669 | + managed_client.old_y = managed_client.y; | ||
| 670 | + managed_client.y = monitor.mon_y + event.y; | ||
| 671 | + } | ||
| 672 | + if ((event.value_mask & xlib.c.CWWidth) != 0) { | ||
| 673 | + managed_client.old_width = managed_client.width; | ||
| 674 | + managed_client.width = event.width; | ||
| 675 | + } | ||
| 676 | + if ((event.value_mask & xlib.c.CWHeight) != 0) { | ||
| 677 | + managed_client.old_height = managed_client.height; | ||
| 678 | + managed_client.height = event.height; | ||
| 679 | + } | ||
| 680 | + const client_full_width = managed_client.width + managed_client.border_width * 2; | ||
| 681 | + const client_full_height = managed_client.height + managed_client.border_width * 2; | ||
| 682 | + if ((managed_client.x + managed_client.width) > monitor.mon_x + monitor.mon_w and managed_client.is_floating) { | ||
| 683 | + managed_client.x = monitor.mon_x + @divTrunc(monitor.mon_w, 2) - @divTrunc(client_full_width, 2); | ||
| 684 | + } | ||
| 685 | + if ((managed_client.y + managed_client.height) > monitor.mon_y + monitor.mon_h and managed_client.is_floating) { | ||
| 686 | + managed_client.y = monitor.mon_y + @divTrunc(monitor.mon_h, 2) - @divTrunc(client_full_height, 2); | ||
| 687 | + } | ||
| 688 | + if (((event.value_mask & (xlib.c.CWX | xlib.c.CWY)) != 0) and ((event.value_mask & (xlib.c.CWWidth | xlib.c.CWHeight)) == 0)) { | ||
| 689 | + tiling.send_configure(managed_client); | ||
| 690 | + } | ||
| 691 | + if (client_mod.is_visible(managed_client)) { | ||
| 692 | + _ = xlib.XMoveResizeWindow(display.handle, managed_client.window, managed_client.x, managed_client.y, @intCast(managed_client.width), @intCast(managed_client.height)); | ||
| 693 | + } | ||
| 694 | + } else { | ||
| 695 | + tiling.send_configure(managed_client); | ||
| 696 | + } | ||
| 697 | + } else { | ||
| 698 | + var changes: xlib.XWindowChanges = undefined; | ||
| 699 | + changes.x = event.x; | ||
| 700 | + changes.y = event.y; | ||
| 701 | + changes.width = event.width; | ||
| 702 | + changes.height = event.height; | ||
| 703 | + changes.border_width = event.border_width; | ||
| 704 | + changes.sibling = event.above; | ||
| 705 | + changes.stack_mode = event.detail; | ||
| 706 | + _ = xlib.XConfigureWindow(display.handle, event.window, @intCast(event.value_mask), &changes); | ||
| 707 | + } | ||
| 708 | + _ = xlib.XSync(display.handle, xlib.False); | ||
| 709 | +} | ||
| 710 | + | ||
| 711 | +fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void { | ||
| 712 | + const keysym = xlib.XKeycodeToKeysym(display.handle, @intCast(event.keycode), 0); | ||
| 713 | + const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask); | ||
| 714 | + | ||
| 715 | + for (config.keybinds.items) |keybind| { | ||
| 716 | + if (keysym == keybind.keysym and clean_state == keybind.mod_mask) { | ||
| 717 | + execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg); | ||
| 718 | + return; | ||
| 719 | + } | ||
| 720 | + } | ||
| 721 | +} | ||
| 722 | + | ||
| 723 | +fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8) void { | ||
| 724 | + switch (action) { | ||
| 725 | + .spawn_terminal => spawn_terminal(), | ||
| 726 | + .spawn => { | ||
| 727 | + if (str_arg) |cmd| { | ||
| 728 | + spawn_command(cmd); | ||
| 729 | + } | ||
| 730 | + }, | ||
| 731 | + .kill_client => kill_focused(display), | ||
| 732 | + .quit => { | ||
| 733 | + std.debug.print("quit keybind pressed\n", .{}); | ||
| 734 | + running = false; | ||
| 735 | + }, | ||
| 736 | + .reload_config => reload_config(display), | ||
| 737 | + .restart => {}, | ||
| 738 | + .focus_next => focusstack(display, 1), | ||
| 739 | + .focus_prev => focusstack(display, -1), | ||
| 740 | + .move_next => movestack(display, 1), | ||
| 741 | + .move_prev => movestack(display, -1), | ||
| 742 | + .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0), | ||
| 743 | + .inc_master => incnmaster(1), | ||
| 744 | + .dec_master => incnmaster(-1), | ||
| 745 | + .toggle_floating => toggle_floating(display), | ||
| 746 | + .toggle_fullscreen => toggle_fullscreen(display), | ||
| 747 | + .toggle_gaps => toggle_gaps(), | ||
| 748 | + .cycle_layout => cycle_layout(), | ||
| 749 | + .set_layout => set_layout(str_arg), | ||
| 750 | + .set_layout_tiling => {}, | ||
| 751 | + .set_layout_floating => {}, | ||
| 752 | + .view_tag => { | ||
| 753 | + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); | ||
| 754 | + view(display, tag_mask); | ||
| 755 | + }, | ||
| 756 | + .view_next_tag => view_adjacent_tag(display, 1), | ||
| 757 | + .view_prev_tag => view_adjacent_tag(display, -1), | ||
| 758 | + .view_next_nonempty_tag => view_adjacent_nonempty_tag(display, 1), | ||
| 759 | + .view_prev_nonempty_tag => view_adjacent_nonempty_tag(display, -1), | ||
| 760 | + .move_to_tag => { | ||
| 761 | + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); | ||
| 762 | + tag_client(display, tag_mask); | ||
| 763 | + }, | ||
| 764 | + .toggle_view_tag => { | ||
| 765 | + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); | ||
| 766 | + toggle_view(display, tag_mask); | ||
| 767 | + }, | ||
| 768 | + .toggle_tag => { | ||
| 769 | + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); | ||
| 770 | + toggle_client_tag(display, tag_mask); | ||
| 771 | + }, | ||
| 772 | + .focus_monitor => focusmon(display, int_arg), | ||
| 773 | + .send_to_monitor => sendmon(display, int_arg), | ||
| 774 | + .scroll_left => { | ||
| 775 | + scroll_layout(-1); | ||
| 776 | + }, | ||
| 777 | + .scroll_right => { | ||
| 778 | + scroll_layout(1); | ||
| 779 | + }, | ||
| 780 | + } | ||
| 781 | +} | ||
| 782 | + | ||
| 783 | +fn reload_config(display: *Display) void { | ||
| 784 | + std.debug.print("reloading config...\n", .{}); | ||
| 785 | + | ||
| 786 | + ungrab_keybinds(display); | ||
| 787 | + | ||
| 788 | + config.keybinds.clearRetainingCapacity(); | ||
| 789 | + config.rules.clearRetainingCapacity(); | ||
| 790 | + config.blocks.clearRetainingCapacity(); | ||
| 791 | + | ||
| 792 | + lua.deinit(); | ||
| 793 | + _ = lua.init(&config); | ||
| 794 | + | ||
| 795 | + const loaded = if (config_path_global) |path| | ||
| 796 | + lua.load_file(path) | ||
| 797 | + else | ||
| 798 | + lua.load_config(); | ||
| 799 | + | ||
| 800 | + if (loaded) { | ||
| 801 | + if (config_path_global) |path| { | ||
| 802 | + std.debug.print("reloaded config from {s}\n", .{path}); | ||
| 803 | + } else { | ||
| 804 | + std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{}); | ||
| 805 | + } | ||
| 806 | + apply_config_values(); | ||
| 807 | + } else { | ||
| 808 | + std.debug.print("reload failed, restoring defaults\n", .{}); | ||
| 809 | + setup_default_keybinds(); | ||
| 810 | + } | ||
| 811 | + | ||
| 812 | + bar_mod.destroy_bars(gpa.allocator(), display.handle); | ||
| 813 | + setup_bars(gpa.allocator(), display); | ||
| 814 | + rebuild_bar_blocks(); | ||
| 815 | + | ||
| 816 | + grab_keybinds(display); | ||
| 817 | +} | ||
| 818 | + | ||
| 819 | +fn rebuild_bar_blocks() void { | ||
| 820 | + var current_bar = bar_mod.bars; | ||
| 821 | + while (current_bar) |bar| { | ||
| 822 | + bar.clear_blocks(); | ||
| 823 | + if (config.blocks.items.len > 0) { | ||
| 824 | + for (config.blocks.items) |cfg_block| { | ||
| 825 | + const block = config_block_to_bar_block(cfg_block); | ||
| 826 | + bar.add_block(block); | ||
| 827 | + } | ||
| 828 | + } else { | ||
| 829 | + bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true)); | ||
| 830 | + bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false)); | ||
| 831 | + bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true)); | ||
| 832 | + } | ||
| 833 | + current_bar = bar.next; | ||
| 834 | + } | ||
| 835 | +} | ||
| 836 | + | ||
| 837 | +fn ungrab_keybinds(display: *Display) void { | ||
| 838 | + _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root); | ||
| 839 | +} | ||
| 840 | + | ||
| 841 | +fn spawn_child_setup() void { | ||
| 842 | + _ = std.c.setsid(); | ||
| 843 | + if (display_global) |display| { | ||
| 844 | + const display_fd = xlib.XConnectionNumber(display.handle); | ||
| 845 | + std.posix.close(@intCast(display_fd)); | ||
| 846 | + } | ||
| 847 | + const sigchld_handler = std.posix.Sigaction{ | ||
| 848 | + .handler = .{ .handler = std.posix.SIG.DFL }, | ||
| 849 | + .mask = std.mem.zeroes(std.posix.sigset_t), | ||
| 850 | + .flags = 0, | ||
| 851 | + }; | ||
| 852 | + std.posix.sigaction(std.posix.SIG.CHLD, &sigchld_handler, null); | ||
| 853 | +} | ||
| 854 | + | ||
| 855 | +fn spawn_command(cmd: []const u8) void { | ||
| 856 | + const pid = std.posix.fork() catch return; | ||
| 857 | + if (pid == 0) { | ||
| 858 | + spawn_child_setup(); | ||
| 859 | + var cmd_buf: [1024]u8 = undefined; | ||
| 860 | + if (cmd.len >= cmd_buf.len) { | ||
| 861 | + std.posix.exit(1); | ||
| 862 | + } | ||
| 863 | + @memcpy(cmd_buf[0..cmd.len], cmd); | ||
| 864 | + cmd_buf[cmd.len] = 0; | ||
| 865 | + const argv = [_:null]?[*:0]const u8{ "sh", "-c", @ptrCast(&cmd_buf) }; | ||
| 866 | + _ = std.posix.execvpeZ("sh", &argv, std.c.environ) catch {}; | ||
| 867 | + std.posix.exit(1); | ||
| 868 | + } | ||
| 869 | +} | ||
| 870 | + | ||
| 871 | +fn spawn_terminal() void { | ||
| 872 | + const pid = std.posix.fork() catch return; | ||
| 873 | + if (pid == 0) { | ||
| 874 | + spawn_child_setup(); | ||
| 875 | + var term_buf: [256]u8 = undefined; | ||
| 876 | + const terminal = config.terminal; | ||
| 877 | + if (terminal.len >= term_buf.len) { | ||
| 878 | + std.posix.exit(1); | ||
| 879 | + } | ||
| 880 | + @memcpy(term_buf[0..terminal.len], terminal); | ||
| 881 | + term_buf[terminal.len] = 0; | ||
| 882 | + const term_ptr: [*:0]const u8 = @ptrCast(&term_buf); | ||
| 883 | + const argv = [_:null]?[*:0]const u8{term_ptr}; | ||
| 884 | + _ = std.posix.execvpeZ(term_ptr, &argv, std.c.environ) catch {}; | ||
| 885 | + std.posix.exit(1); | ||
| 886 | + } | ||
| 887 | +} | ||
| 888 | + | ||
| 889 | +fn movestack(display: *Display, direction: i32) void { | ||
| 890 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 891 | + const current = monitor.sel orelse return; | ||
| 892 | + | ||
| 893 | + if (current.is_floating) { | ||
| 894 | + return; | ||
| 895 | + } | ||
| 896 | + | ||
| 897 | + var target: ?*Client = null; | ||
| 898 | + | ||
| 899 | + if (direction > 0) { | ||
| 900 | + target = current.next; | ||
| 901 | + while (target) |client| { | ||
| 902 | + if (client_mod.is_visible(client) and !client.is_floating) { | ||
| 903 | + break; | ||
| 904 | + } | ||
| 905 | + target = client.next; | ||
| 906 | + } | ||
| 907 | + if (target == null) { | ||
| 908 | + target = monitor.clients; | ||
| 909 | + while (target) |client| { | ||
| 910 | + if (client == current) { | ||
| 911 | + break; | ||
| 912 | + } | ||
| 913 | + if (client_mod.is_visible(client) and !client.is_floating) { | ||
| 914 | + break; | ||
| 915 | + } | ||
| 916 | + target = client.next; | ||
| 917 | + } | ||
| 918 | + } | ||
| 919 | + } else { | ||
| 920 | + var prev: ?*Client = null; | ||
| 921 | + var iter = monitor.clients; | ||
| 922 | + while (iter) |client| { | ||
| 923 | + if (client == current) { | ||
| 924 | + break; | ||
| 925 | + } | ||
| 926 | + if (client_mod.is_visible(client) and !client.is_floating) { | ||
| 927 | + prev = client; | ||
| 928 | + } | ||
| 929 | + iter = client.next; | ||
| 930 | + } | ||
| 931 | + if (prev == null) { | ||
| 932 | + iter = current.next; | ||
| 933 | + while (iter) |client| { | ||
| 934 | + if (client_mod.is_visible(client) and !client.is_floating) { | ||
| 935 | + prev = client; | ||
| 936 | + } | ||
| 937 | + iter = client.next; | ||
| 938 | + } | ||
| 939 | + } | ||
| 940 | + target = prev; | ||
| 941 | + } | ||
| 942 | + | ||
| 943 | + if (target) |swap_client| { | ||
| 944 | + if (swap_client != current) { | ||
| 945 | + client_mod.swap_clients(current, swap_client); | ||
| 946 | + arrange(monitor); | ||
| 947 | + focus(display, current); | ||
| 948 | + } | ||
| 949 | + } | ||
| 950 | +} | ||
| 951 | + | ||
| 952 | +fn toggle_view(display: *Display, tag_mask: u32) void { | ||
| 953 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 954 | + const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask; | ||
| 955 | + if (new_tags != 0) { | ||
| 956 | + monitor.tagset[monitor.sel_tags] = new_tags; | ||
| 957 | + | ||
| 958 | + if (new_tags == ~@as(u32, 0)) { | ||
| 959 | + monitor.pertag.prevtag = monitor.pertag.curtag; | ||
| 960 | + monitor.pertag.curtag = 0; | ||
| 961 | + } | ||
| 962 | + | ||
| 963 | + if ((new_tags & (@as(u32, 1) << @intCast(monitor.pertag.curtag -| 1))) == 0) { | ||
| 964 | + monitor.pertag.prevtag = monitor.pertag.curtag; | ||
| 965 | + var i: u32 = 0; | ||
| 966 | + while (i < 9) : (i += 1) { | ||
| 967 | + if ((new_tags & (@as(u32, 1) << @intCast(i))) != 0) break; | ||
| 968 | + } | ||
| 969 | + monitor.pertag.curtag = i + 1; | ||
| 970 | + } | ||
| 971 | + | ||
| 972 | + monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag]; | ||
| 973 | + monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag]; | ||
| 974 | + monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag]; | ||
| 975 | + | ||
| 976 | + focus_top_client(display, monitor); | ||
| 977 | + arrange(monitor); | ||
| 978 | + bar_mod.invalidate_bars(); | ||
| 979 | + } | ||
| 980 | +} | ||
| 981 | + | ||
| 982 | +fn toggle_client_tag(display: *Display, tag_mask: u32) void { | ||
| 983 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 984 | + const client = monitor.sel orelse return; | ||
| 985 | + const new_tags = client.tags ^ tag_mask; | ||
| 986 | + if (new_tags != 0) { | ||
| 987 | + client.tags = new_tags; | ||
| 988 | + focus_top_client(display, monitor); | ||
| 989 | + arrange(monitor); | ||
| 990 | + bar_mod.invalidate_bars(); | ||
| 991 | + } | ||
| 992 | +} | ||
| 993 | + | ||
| 994 | +fn toggle_gaps() void { | ||
| 995 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 996 | + if (monitor.gap_inner_h == 0) { | ||
| 997 | + monitor.gap_inner_h = gap_inner_v; | ||
| 998 | + monitor.gap_inner_v = gap_inner_v; | ||
| 999 | + monitor.gap_outer_h = gap_outer_h; | ||
| 1000 | + monitor.gap_outer_v = gap_outer_v; | ||
| 1001 | + } else { | ||
| 1002 | + monitor.gap_inner_h = 0; | ||
| 1003 | + monitor.gap_inner_v = 0; | ||
| 1004 | + monitor.gap_outer_h = 0; | ||
| 1005 | + monitor.gap_outer_v = 0; | ||
| 1006 | + } | ||
| 1007 | + arrange(monitor); | ||
| 1008 | +} | ||
| 1009 | + | ||
| 1010 | +fn kill_focused(display: *Display) void { | ||
| 1011 | + const selected = monitor_mod.selected_monitor orelse return; | ||
| 1012 | + const client = selected.sel orelse return; | ||
| 1013 | + std.debug.print("killing window: 0x{x}\n", .{client.window}); | ||
| 1014 | + | ||
| 1015 | + if (!send_event(display, client, wm_delete)) { | ||
| 1016 | + _ = xlib.XGrabServer(display.handle); | ||
| 1017 | + _ = xlib.XKillClient(display.handle, client.window); | ||
| 1018 | + _ = xlib.XSync(display.handle, xlib.False); | ||
| 1019 | + _ = xlib.XUngrabServer(display.handle); | ||
| 1020 | + } | ||
| 1021 | +} | ||
| 1022 | + | ||
| 1023 | +fn toggle_fullscreen(display: *Display) void { | ||
| 1024 | + const selected = monitor_mod.selected_monitor orelse return; | ||
| 1025 | + const client = selected.sel orelse return; | ||
| 1026 | + set_fullscreen(display, client, !client.is_fullscreen); | ||
| 1027 | +} | ||
| 1028 | + | ||
| 1029 | +fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void { | ||
| 1030 | + const monitor = client.monitor orelse return; | ||
| 1031 | + | ||
| 1032 | + if (fullscreen and !client.is_fullscreen) { | ||
| 1033 | + var fullscreen_atom = net_wm_state_fullscreen; | ||
| 1034 | + _ = xlib.XChangeProperty( | ||
| 1035 | + display.handle, | ||
| 1036 | + client.window, | ||
| 1037 | + net_wm_state, | ||
| 1038 | + xlib.XA_ATOM, | ||
| 1039 | + 32, | ||
| 1040 | + xlib.PropModeReplace, | ||
| 1041 | + @ptrCast(&fullscreen_atom), | ||
| 1042 | + 1, | ||
| 1043 | + ); | ||
| 1044 | + client.is_fullscreen = true; | ||
| 1045 | + client.old_state = client.is_floating; | ||
| 1046 | + client.old_border_width = client.border_width; | ||
| 1047 | + client.border_width = 0; | ||
| 1048 | + client.is_floating = true; | ||
| 1049 | + | ||
| 1050 | + _ = xlib.XSetWindowBorderWidth(display.handle, client.window, 0); | ||
| 1051 | + tiling.resize_client(client, monitor.mon_x, monitor.mon_y, monitor.mon_w, monitor.mon_h); | ||
| 1052 | + _ = xlib.XRaiseWindow(display.handle, client.window); | ||
| 1053 | + | ||
| 1054 | + std.debug.print("fullscreen enabled: window=0x{x}\n", .{client.window}); | ||
| 1055 | + } else if (!fullscreen and client.is_fullscreen) { | ||
| 1056 | + var no_atom: xlib.Atom = 0; | ||
| 1057 | + _ = xlib.XChangeProperty( | ||
| 1058 | + display.handle, | ||
| 1059 | + client.window, | ||
| 1060 | + net_wm_state, | ||
| 1061 | + xlib.XA_ATOM, | ||
| 1062 | + 32, | ||
| 1063 | + xlib.PropModeReplace, | ||
| 1064 | + @ptrCast(&no_atom), | ||
| 1065 | + 0, | ||
| 1066 | + ); | ||
| 1067 | + client.is_fullscreen = false; | ||
| 1068 | + client.is_floating = client.old_state; | ||
| 1069 | + client.border_width = client.old_border_width; | ||
| 1070 | + | ||
| 1071 | + client.x = client.old_x; | ||
| 1072 | + client.y = client.old_y; | ||
| 1073 | + client.width = client.old_width; | ||
| 1074 | + client.height = client.old_height; | ||
| 1075 | + | ||
| 1076 | + tiling.resize_client(client, client.x, client.y, client.width, client.height); | ||
| 1077 | + arrange(monitor); | ||
| 1078 | + | ||
| 1079 | + std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window}); | ||
| 1080 | + } | ||
| 1081 | +} | ||
| 1082 | + | ||
| 1083 | +fn view(display: *Display, tag_mask: u32) void { | ||
| 1084 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1085 | + if (tag_mask == monitor.tagset[monitor.sel_tags]) { | ||
| 1086 | + return; | ||
| 1087 | + } | ||
| 1088 | + monitor.sel_tags ^= 1; | ||
| 1089 | + if (tag_mask != 0) { | ||
| 1090 | + monitor.tagset[monitor.sel_tags] = tag_mask; | ||
| 1091 | + monitor.pertag.prevtag = monitor.pertag.curtag; | ||
| 1092 | + | ||
| 1093 | + if (tag_mask == ~@as(u32, 0)) { | ||
| 1094 | + monitor.pertag.curtag = 0; | ||
| 1095 | + } else { | ||
| 1096 | + var i: u32 = 0; | ||
| 1097 | + while (i < 9) : (i += 1) { | ||
| 1098 | + if ((tag_mask & (@as(u32, 1) << @intCast(i))) != 0) break; | ||
| 1099 | + } | ||
| 1100 | + monitor.pertag.curtag = i + 1; | ||
| 1101 | + } | ||
| 1102 | + } else { | ||
| 1103 | + const tmp = monitor.pertag.prevtag; | ||
| 1104 | + monitor.pertag.prevtag = monitor.pertag.curtag; | ||
| 1105 | + monitor.pertag.curtag = tmp; | ||
| 1106 | + } | ||
| 1107 | + | ||
| 1108 | + monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag]; | ||
| 1109 | + monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag]; | ||
| 1110 | + monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag]; | ||
| 1111 | + | ||
| 1112 | + focus_top_client(display, monitor); | ||
| 1113 | + arrange(monitor); | ||
| 1114 | + bar_mod.invalidate_bars(); | ||
| 1115 | + std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]}); | ||
| 1116 | +} | ||
| 1117 | + | ||
| 1118 | +fn view_adjacent_tag(display: *Display, direction: i32) void { | ||
| 1119 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1120 | + const current_tag = monitor.pertag.curtag; | ||
| 1121 | + var new_tag: i32 = @intCast(current_tag); | ||
| 1122 | + | ||
| 1123 | + new_tag += direction; | ||
| 1124 | + if (new_tag < 1) new_tag = 9; | ||
| 1125 | + if (new_tag > 9) new_tag = 1; | ||
| 1126 | + | ||
| 1127 | + const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1); | ||
| 1128 | + view(display, tag_mask); | ||
| 1129 | +} | ||
| 1130 | + | ||
| 1131 | +fn view_adjacent_nonempty_tag(display: *Display, direction: i32) void { | ||
| 1132 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1133 | + const current_tag = monitor.pertag.curtag; | ||
| 1134 | + var new_tag: i32 = @intCast(current_tag); | ||
| 1135 | + | ||
| 1136 | + var attempts: i32 = 0; | ||
| 1137 | + while (attempts < 9) : (attempts += 1) { | ||
| 1138 | + new_tag += direction; | ||
| 1139 | + if (new_tag < 1) new_tag = 9; | ||
| 1140 | + if (new_tag > 9) new_tag = 1; | ||
| 1141 | + | ||
| 1142 | + const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1); | ||
| 1143 | + if (has_clients_on_tag(monitor, tag_mask)) { | ||
| 1144 | + view(display, tag_mask); | ||
| 1145 | + return; | ||
| 1146 | + } | ||
| 1147 | + } | ||
| 1148 | +} | ||
| 1149 | + | ||
| 1150 | +fn has_clients_on_tag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool { | ||
| 1151 | + var client = monitor.clients; | ||
| 1152 | + while (client) |c| { | ||
| 1153 | + if ((c.tags & tag_mask) != 0) { | ||
| 1154 | + return true; | ||
| 1155 | + } | ||
| 1156 | + client = c.next; | ||
| 1157 | + } | ||
| 1158 | + return false; | ||
| 1159 | +} | ||
| 1160 | + | ||
| 1161 | +fn tag_client(display: *Display, tag_mask: u32) void { | ||
| 1162 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1163 | + const client = monitor.sel orelse return; | ||
| 1164 | + if (tag_mask == 0) { | ||
| 1165 | + return; | ||
| 1166 | + } | ||
| 1167 | + client.tags = tag_mask; | ||
| 1168 | + focus_top_client(display, monitor); | ||
| 1169 | + arrange(monitor); | ||
| 1170 | + bar_mod.invalidate_bars(); | ||
| 1171 | + std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask }); | ||
| 1172 | +} | ||
| 1173 | + | ||
| 1174 | +fn focus_top_client(display: *Display, monitor: *Monitor) void { | ||
| 1175 | + var visible_client = monitor.stack; | ||
| 1176 | + while (visible_client) |client| { | ||
| 1177 | + if (client_mod.is_visible(client)) { | ||
| 1178 | + focus(display, client); | ||
| 1179 | + return; | ||
| 1180 | + } | ||
| 1181 | + visible_client = client.stack_next; | ||
| 1182 | + } | ||
| 1183 | + monitor.sel = null; | ||
| 1184 | + _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); | ||
| 1185 | +} | ||
| 1186 | + | ||
| 1187 | +fn focusstack(display: *Display, direction: i32) void { | ||
| 1188 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1189 | + const current = monitor.sel orelse return; | ||
| 1190 | + | ||
| 1191 | + var next_client: ?*Client = null; | ||
| 1192 | + | ||
| 1193 | + if (direction > 0) { | ||
| 1194 | + next_client = current.next; | ||
| 1195 | + while (next_client) |client| { | ||
| 1196 | + if (client_mod.is_visible(client)) { | ||
| 1197 | + break; | ||
| 1198 | + } | ||
| 1199 | + next_client = client.next; | ||
| 1200 | + } | ||
| 1201 | + if (next_client == null) { | ||
| 1202 | + next_client = monitor.clients; | ||
| 1203 | + while (next_client) |client| { | ||
| 1204 | + if (client_mod.is_visible(client)) { | ||
| 1205 | + break; | ||
| 1206 | + } | ||
| 1207 | + next_client = client.next; | ||
| 1208 | + } | ||
| 1209 | + } | ||
| 1210 | + } else { | ||
| 1211 | + var prev: ?*Client = null; | ||
| 1212 | + var iter = monitor.clients; | ||
| 1213 | + while (iter) |client| { | ||
| 1214 | + if (client == current) { | ||
| 1215 | + break; | ||
| 1216 | + } | ||
| 1217 | + if (client_mod.is_visible(client)) { | ||
| 1218 | + prev = client; | ||
| 1219 | + } | ||
| 1220 | + iter = client.next; | ||
| 1221 | + } | ||
| 1222 | + if (prev == null) { | ||
| 1223 | + iter = current.next; | ||
| 1224 | + while (iter) |client| { | ||
| 1225 | + if (client_mod.is_visible(client)) { | ||
| 1226 | + prev = client; | ||
| 1227 | + } | ||
| 1228 | + iter = client.next; | ||
| 1229 | + } | ||
| 1230 | + } | ||
| 1231 | + next_client = prev; | ||
| 1232 | + } | ||
| 1233 | + | ||
| 1234 | + if (next_client) |client| { | ||
| 1235 | + focus(display, client); | ||
| 1236 | + if (client.monitor) |client_monitor| { | ||
| 1237 | + restack(display, client_monitor); | ||
| 1238 | + } | ||
| 1239 | + } | ||
| 1240 | +} | ||
| 1241 | + | ||
| 1242 | +fn toggle_floating(_: *Display) void { | ||
| 1243 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1244 | + const client = monitor.sel orelse return; | ||
| 1245 | + | ||
| 1246 | + if (client.is_fullscreen) { | ||
| 1247 | + return; | ||
| 1248 | + } | ||
| 1249 | + | ||
| 1250 | + client.is_floating = !client.is_floating; | ||
| 1251 | + | ||
| 1252 | + if (client.is_floating) { | ||
| 1253 | + tiling.resize(client, client.x, client.y, client.width, client.height, false); | ||
| 1254 | + } | ||
| 1255 | + | ||
| 1256 | + arrange(monitor); | ||
| 1257 | + std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating }); | ||
| 1258 | +} | ||
| 1259 | + | ||
| 1260 | +fn incnmaster(delta: i32) void { | ||
| 1261 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1262 | + const new_val = @max(0, monitor.nmaster + delta); | ||
| 1263 | + monitor.nmaster = new_val; | ||
| 1264 | + monitor.pertag.nmasters[monitor.pertag.curtag] = new_val; | ||
| 1265 | + arrange(monitor); | ||
| 1266 | + std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster}); | ||
| 1267 | +} | ||
| 1268 | + | ||
| 1269 | +fn setmfact(delta: f32) void { | ||
| 1270 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1271 | + const new_mfact = monitor.mfact + delta; | ||
| 1272 | + if (new_mfact < 0.05 or new_mfact > 0.95) { | ||
| 1273 | + return; | ||
| 1274 | + } | ||
| 1275 | + monitor.mfact = new_mfact; | ||
| 1276 | + monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact; | ||
| 1277 | + arrange(monitor); | ||
| 1278 | + std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact}); | ||
| 1279 | +} | ||
| 1280 | + | ||
| 1281 | +fn cycle_layout() void { | ||
| 1282 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1283 | + const new_lt = (monitor.sel_lt + 1) % 4; | ||
| 1284 | + monitor.sel_lt = new_lt; | ||
| 1285 | + monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; | ||
| 1286 | + if (new_lt != 3) { | ||
| 1287 | + monitor.scroll_offset = 0; | ||
| 1288 | + } | ||
| 1289 | + arrange(monitor); | ||
| 1290 | + bar_mod.invalidate_bars(); | ||
| 1291 | + if (monitor.lt[monitor.sel_lt]) |layout| { | ||
| 1292 | + std.debug.print("cycle_layout: {s}\n", .{layout.symbol}); | ||
| 1293 | + } | ||
| 1294 | +} | ||
| 1295 | + | ||
| 1296 | +fn set_layout(layout_name: ?[]const u8) void { | ||
| 1297 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1298 | + const name = layout_name orelse return; | ||
| 1299 | + | ||
| 1300 | + const new_lt: u32 = if (std.mem.eql(u8, name, "tiling") or std.mem.eql(u8, name, "[]=")) | ||
| 1301 | + 0 | ||
| 1302 | + else if (std.mem.eql(u8, name, "monocle") or std.mem.eql(u8, name, "[M]")) | ||
| 1303 | + 1 | ||
| 1304 | + else if (std.mem.eql(u8, name, "floating") or std.mem.eql(u8, name, "><>")) | ||
| 1305 | + 2 | ||
| 1306 | + else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]")) | ||
| 1307 | + 3 | ||
| 1308 | + else { | ||
| 1309 | + std.debug.print("set_layout: unknown layout '{s}'\n", .{name}); | ||
| 1310 | + return; | ||
| 1311 | + }; | ||
| 1312 | + | ||
| 1313 | + monitor.sel_lt = new_lt; | ||
| 1314 | + monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; | ||
| 1315 | + if (new_lt != 3) { | ||
| 1316 | + monitor.scroll_offset = 0; | ||
| 1317 | + } | ||
| 1318 | + arrange(monitor); | ||
| 1319 | + bar_mod.invalidate_bars(); | ||
| 1320 | + if (monitor.lt[monitor.sel_lt]) |layout| { | ||
| 1321 | + std.debug.print("set_layout: {s}\n", .{layout.symbol}); | ||
| 1322 | + } | ||
| 1323 | +} | ||
| 1324 | + | ||
| 1325 | +fn focusmon(display: *Display, direction: i32) void { | ||
| 1326 | + const selmon = monitor_mod.selected_monitor orelse return; | ||
| 1327 | + const target = monitor_mod.dir_to_monitor(direction) orelse return; | ||
| 1328 | + if (target == selmon) { | ||
| 1329 | + return; | ||
| 1330 | + } | ||
| 1331 | + unfocus_client(display, selmon.sel, false); | ||
| 1332 | + monitor_mod.selected_monitor = target; | ||
| 1333 | + focus(display, null); | ||
| 1334 | + std.debug.print("focusmon: monitor {d}\n", .{target.num}); | ||
| 1335 | +} | ||
| 1336 | + | ||
| 1337 | +fn sendmon(display: *Display, direction: i32) void { | ||
| 1338 | + const source_monitor = monitor_mod.selected_monitor orelse return; | ||
| 1339 | + const client = source_monitor.sel orelse return; | ||
| 1340 | + const target = monitor_mod.dir_to_monitor(direction) orelse return; | ||
| 1341 | + | ||
| 1342 | + if (target == source_monitor) { | ||
| 1343 | + return; | ||
| 1344 | + } | ||
| 1345 | + | ||
| 1346 | + client_mod.detach(client); | ||
| 1347 | + client_mod.detach_stack(client); | ||
| 1348 | + client.monitor = target; | ||
| 1349 | + client.tags = target.tagset[target.sel_tags]; | ||
| 1350 | + client_mod.attach_aside(client); | ||
| 1351 | + client_mod.attach_stack(client); | ||
| 1352 | + | ||
| 1353 | + focus_top_client(display, source_monitor); | ||
| 1354 | + arrange(source_monitor); | ||
| 1355 | + arrange(target); | ||
| 1356 | + | ||
| 1357 | + std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num }); | ||
| 1358 | +} | ||
| 1359 | + | ||
| 1360 | +fn snap_x(client: *Client, new_x: i32, monitor: *Monitor) i32 { | ||
| 1361 | + const client_width = client.width + 2 * client.border_width; | ||
| 1362 | + if (@abs(monitor.win_x - new_x) < snap_distance) { | ||
| 1363 | + return monitor.win_x; | ||
| 1364 | + } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) { | ||
| 1365 | + return monitor.win_x + monitor.win_w - client_width; | ||
| 1366 | + } | ||
| 1367 | + return new_x; | ||
| 1368 | +} | ||
| 1369 | + | ||
| 1370 | +fn snap_y(client: *Client, new_y: i32, monitor: *Monitor) i32 { | ||
| 1371 | + const client_height = client.height + 2 * client.border_width; | ||
| 1372 | + if (@abs(monitor.win_y - new_y) < snap_distance) { | ||
| 1373 | + return monitor.win_y; | ||
| 1374 | + } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) { | ||
| 1375 | + return monitor.win_y + monitor.win_h - client_height; | ||
| 1376 | + } | ||
| 1377 | + return new_y; | ||
| 1378 | +} | ||
| 1379 | + | ||
| 1380 | +fn movemouse(display: *Display) void { | ||
| 1381 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1382 | + const client = monitor.sel orelse return; | ||
| 1383 | + | ||
| 1384 | + if (client.is_fullscreen) { | ||
| 1385 | + return; | ||
| 1386 | + } | ||
| 1387 | + | ||
| 1388 | + restack(display, monitor); | ||
| 1389 | + | ||
| 1390 | + const was_floating = client.is_floating; | ||
| 1391 | + if (!client.is_floating) { | ||
| 1392 | + client.is_floating = true; | ||
| 1393 | + } | ||
| 1394 | + | ||
| 1395 | + var root_x: c_int = undefined; | ||
| 1396 | + var root_y: c_int = undefined; | ||
| 1397 | + var dummy_win: xlib.Window = undefined; | ||
| 1398 | + var dummy_int: c_int = undefined; | ||
| 1399 | + var dummy_uint: c_uint = undefined; | ||
| 1400 | + | ||
| 1401 | + _ = xlib.XQueryPointer(display.handle, display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint); | ||
| 1402 | + | ||
| 1403 | + const grab_result = xlib.XGrabPointer( | ||
| 1404 | + display.handle, | ||
| 1405 | + display.root, | ||
| 1406 | + xlib.False, | ||
| 1407 | + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, | ||
| 1408 | + xlib.GrabModeAsync, | ||
| 1409 | + xlib.GrabModeAsync, | ||
| 1410 | + xlib.None, | ||
| 1411 | + cursor_move, | ||
| 1412 | + xlib.CurrentTime, | ||
| 1413 | + ); | ||
| 1414 | + | ||
| 1415 | + if (grab_result != xlib.GrabSuccess) { | ||
| 1416 | + return; | ||
| 1417 | + } | ||
| 1418 | + | ||
| 1419 | + const start_x = client.x; | ||
| 1420 | + const start_y = client.y; | ||
| 1421 | + const pointer_start_x = root_x; | ||
| 1422 | + const pointer_start_y = root_y; | ||
| 1423 | + var last_time: c_ulong = 0; | ||
| 1424 | + | ||
| 1425 | + var event: xlib.XEvent = undefined; | ||
| 1426 | + var done = false; | ||
| 1427 | + | ||
| 1428 | + while (!done) { | ||
| 1429 | + _ = xlib.XNextEvent(display.handle, &event); | ||
| 1430 | + | ||
| 1431 | + switch (event.type) { | ||
| 1432 | + xlib.MotionNotify => { | ||
| 1433 | + const motion = &event.xmotion; | ||
| 1434 | + if ((motion.time - last_time) < (1000 / 60)) { | ||
| 1435 | + continue; | ||
| 1436 | + } | ||
| 1437 | + last_time = motion.time; | ||
| 1438 | + const delta_x = motion.x_root - pointer_start_x; | ||
| 1439 | + const delta_y = motion.y_root - pointer_start_y; | ||
| 1440 | + var new_x = start_x + delta_x; | ||
| 1441 | + var new_y = start_y + delta_y; | ||
| 1442 | + if (client.monitor) |client_monitor| { | ||
| 1443 | + new_x = snap_x(client, new_x, client_monitor); | ||
| 1444 | + new_y = snap_y(client, new_y, client_monitor); | ||
| 1445 | + } | ||
| 1446 | + tiling.resize(client, new_x, new_y, client.width, client.height, true); | ||
| 1447 | + }, | ||
| 1448 | + xlib.ButtonRelease => { | ||
| 1449 | + done = true; | ||
| 1450 | + }, | ||
| 1451 | + else => {}, | ||
| 1452 | + } | ||
| 1453 | + } | ||
| 1454 | + | ||
| 1455 | + _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime); | ||
| 1456 | + | ||
| 1457 | + const new_mon = monitor_mod.rect_to_monitor(client.x, client.y, client.width, client.height); | ||
| 1458 | + if (new_mon != null and new_mon != monitor) { | ||
| 1459 | + client_mod.detach(client); | ||
| 1460 | + client_mod.detach_stack(client); | ||
| 1461 | + client.monitor = new_mon; | ||
| 1462 | + client.tags = new_mon.?.tagset[new_mon.?.sel_tags]; | ||
| 1463 | + client_mod.attach_aside(client); | ||
| 1464 | + client_mod.attach_stack(client); | ||
| 1465 | + monitor_mod.selected_monitor = new_mon; | ||
| 1466 | + focus(display, client); | ||
| 1467 | + arrange(monitor); | ||
| 1468 | + arrange(new_mon.?); | ||
| 1469 | + } else { | ||
| 1470 | + arrange(monitor); | ||
| 1471 | + } | ||
| 1472 | + | ||
| 1473 | + if (config.auto_tile and !was_floating) { | ||
| 1474 | + const drop_monitor = client.monitor orelse return; | ||
| 1475 | + const center_x = client.x + @divTrunc(client.width, 2); | ||
| 1476 | + const center_y = client.y + @divTrunc(client.height, 2); | ||
| 1477 | + | ||
| 1478 | + if (client_mod.tiled_window_at(client, drop_monitor, center_x, center_y)) |target| { | ||
| 1479 | + client_mod.insert_before(client, target); | ||
| 1480 | + } | ||
| 1481 | + | ||
| 1482 | + client.is_floating = false; | ||
| 1483 | + arrange(drop_monitor); | ||
| 1484 | + } | ||
| 1485 | +} | ||
| 1486 | + | ||
| 1487 | +fn resizemouse(display: *Display) void { | ||
| 1488 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1489 | + const client = monitor.sel orelse return; | ||
| 1490 | + | ||
| 1491 | + if (client.is_fullscreen) { | ||
| 1492 | + return; | ||
| 1493 | + } | ||
| 1494 | + | ||
| 1495 | + restack(display, monitor); | ||
| 1496 | + | ||
| 1497 | + if (!client.is_floating) { | ||
| 1498 | + client.is_floating = true; | ||
| 1499 | + } | ||
| 1500 | + | ||
| 1501 | + const grab_result = xlib.XGrabPointer( | ||
| 1502 | + display.handle, | ||
| 1503 | + display.root, | ||
| 1504 | + xlib.False, | ||
| 1505 | + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, | ||
| 1506 | + xlib.GrabModeAsync, | ||
| 1507 | + xlib.GrabModeAsync, | ||
| 1508 | + xlib.None, | ||
| 1509 | + cursor_resize, | ||
| 1510 | + xlib.CurrentTime, | ||
| 1511 | + ); | ||
| 1512 | + | ||
| 1513 | + if (grab_result != xlib.GrabSuccess) { | ||
| 1514 | + return; | ||
| 1515 | + } | ||
| 1516 | + | ||
| 1517 | + _ = xlib.XWarpPointer(display.handle, xlib.None, client.window, 0, 0, 0, 0, client.width + client.border_width - 1, client.height + client.border_width - 1); | ||
| 1518 | + | ||
| 1519 | + var event: xlib.XEvent = undefined; | ||
| 1520 | + var done = false; | ||
| 1521 | + var last_time: c_ulong = 0; | ||
| 1522 | + | ||
| 1523 | + while (!done) { | ||
| 1524 | + _ = xlib.XNextEvent(display.handle, &event); | ||
| 1525 | + | ||
| 1526 | + switch (event.type) { | ||
| 1527 | + xlib.MotionNotify => { | ||
| 1528 | + const motion = &event.xmotion; | ||
| 1529 | + if ((motion.time - last_time) < (1000 / 60)) { | ||
| 1530 | + continue; | ||
| 1531 | + } | ||
| 1532 | + last_time = motion.time; | ||
| 1533 | + var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1); | ||
| 1534 | + var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1); | ||
| 1535 | + if (client.monitor) |client_monitor| { | ||
| 1536 | + const client_right = client.x + new_width + 2 * client.border_width; | ||
| 1537 | + const client_bottom = client.y + new_height + 2 * client.border_width; | ||
| 1538 | + const mon_right = client_monitor.win_x + client_monitor.win_w; | ||
| 1539 | + const mon_bottom = client_monitor.win_y + client_monitor.win_h; | ||
| 1540 | + if (@abs(mon_right - client_right) < snap_distance) { | ||
| 1541 | + new_width = @max(1, mon_right - client.x - 2 * client.border_width); | ||
| 1542 | + } | ||
| 1543 | + if (@abs(mon_bottom - client_bottom) < snap_distance) { | ||
| 1544 | + new_height = @max(1, mon_bottom - client.y - 2 * client.border_width); | ||
| 1545 | + } | ||
| 1546 | + } | ||
| 1547 | + tiling.resize(client, client.x, client.y, new_width, new_height, true); | ||
| 1548 | + }, | ||
| 1549 | + xlib.ButtonRelease => { | ||
| 1550 | + done = true; | ||
| 1551 | + }, | ||
| 1552 | + else => {}, | ||
| 1553 | + } | ||
| 1554 | + } | ||
| 1555 | + | ||
| 1556 | + _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime); | ||
| 1557 | + arrange(monitor); | ||
| 1558 | +} | ||
| 1559 | + | ||
| 1560 | +fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void { | ||
| 1561 | + if (event.count != 0) return; | ||
| 1562 | + | ||
| 1563 | + if (bar_mod.window_to_bar(event.window)) |bar| { | ||
| 1564 | + bar.invalidate(); | ||
| 1565 | + bar.draw(display.handle, &tags); | ||
| 1566 | + } | ||
| 1567 | +} | ||
| 1568 | + | ||
| 1569 | +fn clean_mask(mask: c_uint) c_uint { | ||
| 1570 | + const lock: c_uint = @intCast(xlib.LockMask); | ||
| 1571 | + const shift: c_uint = @intCast(xlib.ShiftMask); | ||
| 1572 | + const ctrl: c_uint = @intCast(xlib.ControlMask); | ||
| 1573 | + const mod1: c_uint = @intCast(xlib.Mod1Mask); | ||
| 1574 | + const mod2: c_uint = @intCast(xlib.Mod2Mask); | ||
| 1575 | + const mod3: c_uint = @intCast(xlib.Mod3Mask); | ||
| 1576 | + const mod4: c_uint = @intCast(xlib.Mod4Mask); | ||
| 1577 | + const mod5: c_uint = @intCast(xlib.Mod5Mask); | ||
| 1578 | + return mask & ~(lock | numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5); | ||
| 1579 | +} | ||
| 1580 | + | ||
| 1581 | +fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void { | ||
| 1582 | + std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow }); | ||
| 1583 | + | ||
| 1584 | + const clicked_monitor = monitor_mod.window_to_monitor(event.window); | ||
| 1585 | + if (clicked_monitor) |monitor| { | ||
| 1586 | + if (monitor != monitor_mod.selected_monitor) { | ||
| 1587 | + if (monitor_mod.selected_monitor) |selmon| { | ||
| 1588 | + unfocus_client(display, selmon.sel, true); | ||
| 1589 | + } | ||
| 1590 | + monitor_mod.selected_monitor = monitor; | ||
| 1591 | + focus(display, null); | ||
| 1592 | + } | ||
| 1593 | + } | ||
| 1594 | + | ||
| 1595 | + if (bar_mod.window_to_bar(event.window)) |bar| { | ||
| 1596 | + const clicked_tag = bar.handle_click(event.x, &tags); | ||
| 1597 | + if (clicked_tag) |tag_index| { | ||
| 1598 | + const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index); | ||
| 1599 | + view(display, tag_mask); | ||
| 1600 | + } | ||
| 1601 | + return; | ||
| 1602 | + } | ||
| 1603 | + | ||
| 1604 | + const click_client = client_mod.window_to_client(event.window); | ||
| 1605 | + if (click_client) |found_client| { | ||
| 1606 | + focus(display, found_client); | ||
| 1607 | + if (monitor_mod.selected_monitor) |selmon| { | ||
| 1608 | + restack(display, selmon); | ||
| 1609 | + } | ||
| 1610 | + _ = xlib.XAllowEvents(display.handle, xlib.ReplayPointer, xlib.CurrentTime); | ||
| 1611 | + } | ||
| 1612 | + | ||
| 1613 | + const clean_state = clean_mask(event.state); | ||
| 1614 | + for (config.buttons.items) |button| { | ||
| 1615 | + if (button.click != .client_win) continue; | ||
| 1616 | + const button_clean_mask = clean_mask(button.mod_mask); | ||
| 1617 | + if (clean_state == button_clean_mask and event.button == button.button) { | ||
| 1618 | + switch (button.action) { | ||
| 1619 | + .move_mouse => movemouse(display), | ||
| 1620 | + .resize_mouse => resizemouse(display), | ||
| 1621 | + .toggle_floating => { | ||
| 1622 | + if (click_client) |found_client| { | ||
| 1623 | + found_client.is_floating = !found_client.is_floating; | ||
| 1624 | + if (monitor_mod.selected_monitor) |monitor| { | ||
| 1625 | + arrange(monitor); | ||
| 1626 | + } | ||
| 1627 | + } | ||
| 1628 | + }, | ||
| 1629 | + } | ||
| 1630 | + return; | ||
| 1631 | + } | ||
| 1632 | + } | ||
| 1633 | +} | ||
| 1634 | + | ||
| 1635 | +fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void { | ||
| 1636 | + const client = client_mod.window_to_client(event.window) orelse return; | ||
| 1637 | + | ||
| 1638 | + if (event.message_type == net_wm_state) { | ||
| 1639 | + const action = event.data.l[0]; | ||
| 1640 | + const first_property = @as(xlib.Atom, @intCast(event.data.l[1])); | ||
| 1641 | + const second_property = @as(xlib.Atom, @intCast(event.data.l[2])); | ||
| 1642 | + | ||
| 1643 | + if (first_property == net_wm_state_fullscreen or second_property == net_wm_state_fullscreen) { | ||
| 1644 | + const net_wm_state_remove = 0; | ||
| 1645 | + const net_wm_state_add = 1; | ||
| 1646 | + const net_wm_state_toggle = 2; | ||
| 1647 | + | ||
| 1648 | + if (action == net_wm_state_add) { | ||
| 1649 | + set_fullscreen(display, client, true); | ||
| 1650 | + } else if (action == net_wm_state_remove) { | ||
| 1651 | + set_fullscreen(display, client, false); | ||
| 1652 | + } else if (action == net_wm_state_toggle) { | ||
| 1653 | + set_fullscreen(display, client, !client.is_fullscreen); | ||
| 1654 | + } | ||
| 1655 | + } | ||
| 1656 | + } else if (event.message_type == net_active_window) { | ||
| 1657 | + const selected = monitor_mod.selected_monitor orelse return; | ||
| 1658 | + if (client != selected.sel and !client.is_urgent) { | ||
| 1659 | + set_urgent(display, client, true); | ||
| 1660 | + } | ||
| 1661 | + } | ||
| 1662 | +} | ||
| 1663 | + | ||
| 1664 | +fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void { | ||
| 1665 | + const client = client_mod.window_to_client(event.window) orelse return; | ||
| 1666 | + std.debug.print("destroy_notify: window=0x{x}\n", .{event.window}); | ||
| 1667 | + unmanage(display, client); | ||
| 1668 | +} | ||
| 1669 | + | ||
| 1670 | +fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void { | ||
| 1671 | + const client = client_mod.window_to_client(event.window) orelse return; | ||
| 1672 | + std.debug.print("unmap_notify: window=0x{x}\n", .{event.window}); | ||
| 1673 | + unmanage(display, client); | ||
| 1674 | +} | ||
| 1675 | + | ||
| 1676 | +fn unmanage(display: *Display, client: *Client) void { | ||
| 1677 | + const client_monitor = client.monitor; | ||
| 1678 | + | ||
| 1679 | + var next_focus: ?*Client = null; | ||
| 1680 | + if (client_monitor) |monitor| { | ||
| 1681 | + if (monitor.sel == client and is_scrolling_layout(monitor)) { | ||
| 1682 | + next_focus = client.next; | ||
| 1683 | + if (next_focus == null) { | ||
| 1684 | + var prev: ?*Client = null; | ||
| 1685 | + var iter = monitor.clients; | ||
| 1686 | + while (iter) |c| { | ||
| 1687 | + if (c == client) break; | ||
| 1688 | + prev = c; | ||
| 1689 | + iter = c.next; | ||
| 1690 | + } | ||
| 1691 | + next_focus = prev; | ||
| 1692 | + } | ||
| 1693 | + } | ||
| 1694 | + } | ||
| 1695 | + | ||
| 1696 | + client_mod.detach(client); | ||
| 1697 | + client_mod.detach_stack(client); | ||
| 1698 | + | ||
| 1699 | + if (client_monitor) |monitor| { | ||
| 1700 | + if (monitor.sel == client) { | ||
| 1701 | + monitor.sel = if (next_focus) |nf| nf else monitor.stack; | ||
| 1702 | + } | ||
| 1703 | + if (is_scrolling_layout(monitor)) { | ||
| 1704 | + const target = if (monitor.sel) |sel| scrolling.get_target_scroll_for_window(monitor, sel) else 0; | ||
| 1705 | + if (target == 0) { | ||
| 1706 | + monitor.scroll_offset = scrolling.get_scroll_step(monitor); | ||
| 1707 | + } else { | ||
| 1708 | + monitor.scroll_offset = 0; | ||
| 1709 | + } | ||
| 1710 | + } | ||
| 1711 | + arrange(monitor); | ||
| 1712 | + } | ||
| 1713 | + | ||
| 1714 | + if (client_monitor) |monitor| { | ||
| 1715 | + if (monitor.sel) |selected| { | ||
| 1716 | + focus(display, selected); | ||
| 1717 | + } | ||
| 1718 | + } | ||
| 1719 | + | ||
| 1720 | + client_mod.destroy(client); | ||
| 1721 | + update_client_list(display); | ||
| 1722 | + bar_mod.invalidate_bars(); | ||
| 1723 | +} | ||
| 1724 | + | ||
| 1725 | +fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void { | ||
| 1726 | + if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != display.root) { | ||
| 1727 | + return; | ||
| 1728 | + } | ||
| 1729 | + | ||
| 1730 | + const client = client_mod.window_to_client(event.window); | ||
| 1731 | + const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(event.window); | ||
| 1732 | + const selmon = monitor_mod.selected_monitor; | ||
| 1733 | + | ||
| 1734 | + if (target_mon != selmon) { | ||
| 1735 | + if (selmon) |sel| { | ||
| 1736 | + unfocus_client(display, sel.sel, true); | ||
| 1737 | + } | ||
| 1738 | + monitor_mod.selected_monitor = target_mon; | ||
| 1739 | + } else if (client == null) { | ||
| 1740 | + return; | ||
| 1741 | + } else if (selmon) |sel| { | ||
| 1742 | + if (client.? == sel.sel) { | ||
| 1743 | + return; | ||
| 1744 | + } | ||
| 1745 | + } | ||
| 1746 | + | ||
| 1747 | + focus(display, client); | ||
| 1748 | +} | ||
| 1749 | + | ||
| 1750 | +fn handle_focus_in(display: *Display, event: *xlib.XFocusChangeEvent) void { | ||
| 1751 | + const selmon = monitor_mod.selected_monitor orelse return; | ||
| 1752 | + const selected = selmon.sel orelse return; | ||
| 1753 | + if (event.window != selected.window) { | ||
| 1754 | + set_focus(display, selected); | ||
| 1755 | + } | ||
| 1756 | +} | ||
| 1757 | + | ||
| 1758 | +fn set_focus(display: *Display, client: *Client) void { | ||
| 1759 | + if (!client.never_focus) { | ||
| 1760 | + _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime); | ||
| 1761 | + _ = xlib.XChangeProperty(display.handle, display.root, net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1); | ||
| 1762 | + } | ||
| 1763 | + _ = send_event(display, client, wm_take_focus); | ||
| 1764 | +} | ||
| 1765 | + | ||
| 1766 | +var last_motion_monitor: ?*Monitor = null; | ||
| 1767 | + | ||
| 1768 | +fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent) void { | ||
| 1769 | + if (event.window != display.root) { | ||
| 1770 | + return; | ||
| 1771 | + } | ||
| 1772 | + | ||
| 1773 | + const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1); | ||
| 1774 | + if (target_mon != last_motion_monitor and last_motion_monitor != null) { | ||
| 1775 | + if (monitor_mod.selected_monitor) |selmon| { | ||
| 1776 | + unfocus_client(display, selmon.sel, true); | ||
| 1777 | + } | ||
| 1778 | + monitor_mod.selected_monitor = target_mon; | ||
| 1779 | + focus(display, null); | ||
| 1780 | + } | ||
| 1781 | + last_motion_monitor = target_mon; | ||
| 1782 | +} | ||
| 1783 | + | ||
| 1784 | +fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void { | ||
| 1785 | + if (event.state == xlib.PropertyDelete) { | ||
| 1786 | + return; | ||
| 1787 | + } | ||
| 1788 | + | ||
| 1789 | + const client = client_mod.window_to_client(event.window) orelse return; | ||
| 1790 | + | ||
| 1791 | + if (event.atom == xlib.XA_WM_TRANSIENT_FOR) { | ||
| 1792 | + var trans: xlib.Window = 0; | ||
| 1793 | + if (!client.is_floating and xlib.XGetTransientForHint(display.handle, client.window, &trans) != 0) { | ||
| 1794 | + client.is_floating = client_mod.window_to_client(trans) != null; | ||
| 1795 | + if (client.is_floating) { | ||
| 1796 | + if (client.monitor) |monitor| { | ||
| 1797 | + arrange(monitor); | ||
| 1798 | + } | ||
| 1799 | + } | ||
| 1800 | + } | ||
| 1801 | + } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) { | ||
| 1802 | + client.hints_valid = false; | ||
| 1803 | + } else if (event.atom == xlib.XA_WM_HINTS) { | ||
| 1804 | + update_wm_hints(display, client); | ||
| 1805 | + bar_mod.invalidate_bars(); | ||
| 1806 | + } else if (event.atom == xlib.XA_WM_NAME or event.atom == net_wm_name) { | ||
| 1807 | + update_title(display, client); | ||
| 1808 | + } else if (event.atom == net_wm_window_type) { | ||
| 1809 | + update_window_type(display, client); | ||
| 1810 | + } | ||
| 1811 | +} | ||
| 1812 | + | ||
| 1813 | +fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool) void { | ||
| 1814 | + const unfocus_target = client orelse return; | ||
| 1815 | + grabbuttons(display, unfocus_target, false); | ||
| 1816 | + _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, border_color_unfocused); | ||
| 1817 | + if (reset_input_focus) { | ||
| 1818 | + _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); | ||
| 1819 | + _ = xlib.XDeleteProperty(display.handle, display.root, net_active_window); | ||
| 1820 | + } | ||
| 1821 | +} | ||
| 1822 | + | ||
| 1823 | +fn set_client_state(display: *Display, client: *Client, state: c_long) void { | ||
| 1824 | + var data: [2]c_long = .{ state, xlib.None }; | ||
| 1825 | + _ = xlib.c.XChangeProperty(display.handle, client.window, wm_state, wm_state, 32, xlib.PropModeReplace, @ptrCast(&data), 2); | ||
| 1826 | +} | ||
| 1827 | + | ||
| 1828 | +fn update_numlock_mask(display: *Display) void { | ||
| 1829 | + numlock_mask = 0; | ||
| 1830 | + const modmap = xlib.XGetModifierMapping(display.handle); | ||
| 1831 | + if (modmap == null) return; | ||
| 1832 | + defer _ = xlib.XFreeModifiermap(modmap); | ||
| 1833 | + | ||
| 1834 | + const numlock_keycode = xlib.XKeysymToKeycode(display.handle, xlib.XK_Num_Lock); | ||
| 1835 | + | ||
| 1836 | + var modifier_index: usize = 0; | ||
| 1837 | + while (modifier_index < 8) : (modifier_index += 1) { | ||
| 1838 | + var key_index: usize = 0; | ||
| 1839 | + while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) { | ||
| 1840 | + const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index]; | ||
| 1841 | + if (keycode == numlock_keycode) { | ||
| 1842 | + numlock_mask = @as(c_uint, 1) << @intCast(modifier_index); | ||
| 1843 | + } | ||
| 1844 | + } | ||
| 1845 | + } | ||
| 1846 | +} | ||
| 1847 | + | ||
| 1848 | +fn grabbuttons(display: *Display, client: *Client, focused: bool) void { | ||
| 1849 | + update_numlock_mask(display); | ||
| 1850 | + const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask }; | ||
| 1851 | + | ||
| 1852 | + _ = xlib.XUngrabButton(display.handle, xlib.AnyButton, xlib.AnyModifier, client.window); | ||
| 1853 | + if (!focused) { | ||
| 1854 | + _ = xlib.XGrabButton( | ||
| 1855 | + display.handle, | ||
| 1856 | + xlib.AnyButton, | ||
| 1857 | + xlib.AnyModifier, | ||
| 1858 | + client.window, | ||
| 1859 | + xlib.False, | ||
| 1860 | + xlib.ButtonPressMask | xlib.ButtonReleaseMask, | ||
| 1861 | + xlib.GrabModeSync, | ||
| 1862 | + xlib.GrabModeSync, | ||
| 1863 | + xlib.None, | ||
| 1864 | + xlib.None, | ||
| 1865 | + ); | ||
| 1866 | + } | ||
| 1867 | + for (config.buttons.items) |button| { | ||
| 1868 | + if (button.click == .client_win) { | ||
| 1869 | + for (modifiers) |modifier| { | ||
| 1870 | + _ = xlib.XGrabButton( | ||
| 1871 | + display.handle, | ||
| 1872 | + @intCast(button.button), | ||
| 1873 | + button.mod_mask | modifier, | ||
| 1874 | + client.window, | ||
| 1875 | + xlib.False, | ||
| 1876 | + xlib.ButtonPressMask | xlib.ButtonReleaseMask, | ||
| 1877 | + xlib.GrabModeAsync, | ||
| 1878 | + xlib.GrabModeSync, | ||
| 1879 | + xlib.None, | ||
| 1880 | + xlib.None, | ||
| 1881 | + ); | ||
| 1882 | + } | ||
| 1883 | + } | ||
| 1884 | + } | ||
| 1885 | +} | ||
| 1886 | + | ||
| 1887 | +fn focus(display: *Display, target_client: ?*Client) void { | ||
| 1888 | + const selmon = monitor_mod.selected_monitor orelse return; | ||
| 1889 | + | ||
| 1890 | + var focus_client = target_client; | ||
| 1891 | + if (focus_client == null or !client_mod.is_visible(focus_client.?)) { | ||
| 1892 | + focus_client = selmon.stack; | ||
| 1893 | + while (focus_client) |iter| { | ||
| 1894 | + if (client_mod.is_visible(iter)) break; | ||
| 1895 | + focus_client = iter.stack_next; | ||
| 1896 | + } | ||
| 1897 | + } | ||
| 1898 | + | ||
| 1899 | + if (selmon.sel != null and selmon.sel != focus_client) { | ||
| 1900 | + unfocus_client(display, selmon.sel, false); | ||
| 1901 | + } | ||
| 1902 | + | ||
| 1903 | + if (focus_client) |client| { | ||
| 1904 | + if (client.monitor != selmon) { | ||
| 1905 | + monitor_mod.selected_monitor = client.monitor; | ||
| 1906 | + } | ||
| 1907 | + if (client.is_urgent) { | ||
| 1908 | + set_urgent(display, client, false); | ||
| 1909 | + } | ||
| 1910 | + client_mod.detach_stack(client); | ||
| 1911 | + client_mod.attach_stack(client); | ||
| 1912 | + grabbuttons(display, client, true); | ||
| 1913 | + _ = xlib.XSetWindowBorder(display.handle, client.window, border_color_focused); | ||
| 1914 | + if (!client.never_focus) { | ||
| 1915 | + _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime); | ||
| 1916 | + _ = xlib.XChangeProperty(display.handle, display.root, net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1); | ||
| 1917 | + } | ||
| 1918 | + _ = send_event(display, client, wm_take_focus); | ||
| 1919 | + } else { | ||
| 1920 | + _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); | ||
| 1921 | + _ = xlib.XDeleteProperty(display.handle, display.root, net_active_window); | ||
| 1922 | + } | ||
| 1923 | + | ||
| 1924 | + const current_selmon = monitor_mod.selected_monitor orelse return; | ||
| 1925 | + current_selmon.sel = focus_client; | ||
| 1926 | + | ||
| 1927 | + if (focus_client) |client| { | ||
| 1928 | + if (is_scrolling_layout(current_selmon)) { | ||
| 1929 | + scroll_to_window(client, true); | ||
| 1930 | + } | ||
| 1931 | + } | ||
| 1932 | + | ||
| 1933 | + bar_mod.invalidate_bars(); | ||
| 1934 | +} | ||
| 1935 | + | ||
| 1936 | +fn restack(display: *Display, monitor: *Monitor) void { | ||
| 1937 | + bar_mod.invalidate_bars(); | ||
| 1938 | + const selected_client = monitor.sel orelse return; | ||
| 1939 | + | ||
| 1940 | + if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) { | ||
| 1941 | + _ = xlib.XRaiseWindow(display.handle, selected_client.window); | ||
| 1942 | + } | ||
| 1943 | + | ||
| 1944 | + if (monitor.lt[monitor.sel_lt] != null) { | ||
| 1945 | + var window_changes: xlib.c.XWindowChanges = undefined; | ||
| 1946 | + window_changes.stack_mode = xlib.c.Below; | ||
| 1947 | + window_changes.sibling = monitor.bar_win; | ||
| 1948 | + | ||
| 1949 | + var current = monitor.stack; | ||
| 1950 | + while (current) |client| { | ||
| 1951 | + if (!client.is_floating and client_mod.is_visible(client)) { | ||
| 1952 | + _ = xlib.c.XConfigureWindow(display.handle, client.window, xlib.c.CWSibling | xlib.c.CWStackMode, &window_changes); | ||
| 1953 | + window_changes.sibling = client.window; | ||
| 1954 | + } | ||
| 1955 | + current = client.stack_next; | ||
| 1956 | + } | ||
| 1957 | + } | ||
| 1958 | + | ||
| 1959 | + _ = xlib.XSync(display.handle, xlib.False); | ||
| 1960 | + | ||
| 1961 | + var discard_event: xlib.XEvent = undefined; | ||
| 1962 | + while (xlib.c.XCheckMaskEvent(display.handle, xlib.EnterWindowMask, &discard_event) != 0) {} | ||
| 1963 | +} | ||
| 1964 | + | ||
| 1965 | +fn arrange(monitor: *Monitor) void { | ||
| 1966 | + if (display_global) |display| { | ||
| 1967 | + showhide(display, monitor); | ||
| 1968 | + } | ||
| 1969 | + if (monitor.lt[monitor.sel_lt]) |layout| { | ||
| 1970 | + if (layout.arrange_fn) |arrange_fn| { | ||
| 1971 | + arrange_fn(monitor); | ||
| 1972 | + } | ||
| 1973 | + } | ||
| 1974 | + if (display_global) |display| { | ||
| 1975 | + restack(display, monitor); | ||
| 1976 | + } | ||
| 1977 | +} | ||
| 1978 | + | ||
| 1979 | +fn tick_animations() void { | ||
| 1980 | + if (!scroll_animation.is_active()) return; | ||
| 1981 | + | ||
| 1982 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1983 | + if (scroll_animation.update()) |new_offset| { | ||
| 1984 | + monitor.scroll_offset = new_offset; | ||
| 1985 | + arrange(monitor); | ||
| 1986 | + } | ||
| 1987 | +} | ||
| 1988 | + | ||
| 1989 | +fn is_scrolling_layout(monitor: *Monitor) bool { | ||
| 1990 | + if (monitor.lt[monitor.sel_lt]) |layout| { | ||
| 1991 | + return layout.arrange_fn == scrolling.layout.arrange_fn; | ||
| 1992 | + } | ||
| 1993 | + return false; | ||
| 1994 | +} | ||
| 1995 | + | ||
| 1996 | +fn scroll_layout(direction: i32) void { | ||
| 1997 | + const monitor = monitor_mod.selected_monitor orelse return; | ||
| 1998 | + if (!is_scrolling_layout(monitor)) return; | ||
| 1999 | + | ||
| 2000 | + const scroll_step = scrolling.get_scroll_step(monitor); | ||
| 2001 | + const max_scroll = scrolling.get_max_scroll(monitor); | ||
| 2002 | + | ||
| 2003 | + const current = if (scroll_animation.is_active()) | ||
| 2004 | + scroll_animation.target() | ||
| 2005 | + else | ||
| 2006 | + monitor.scroll_offset; | ||
| 2007 | + | ||
| 2008 | + var target = current + direction * scroll_step; | ||
| 2009 | + target = @max(0, @min(target, max_scroll)); | ||
| 2010 | + | ||
| 2011 | + scroll_animation.start(monitor.scroll_offset, target, animation_config); | ||
| 2012 | +} | ||
| 2013 | + | ||
| 2014 | +fn scroll_to_window(client: *Client, animate: bool) void { | ||
| 2015 | + const monitor = client.monitor orelse return; | ||
| 2016 | + if (!is_scrolling_layout(monitor)) return; | ||
| 2017 | + | ||
| 2018 | + const target = scrolling.get_target_scroll_for_window(monitor, client); | ||
| 2019 | + | ||
| 2020 | + if (animate) { | ||
| 2021 | + scroll_animation.start(monitor.scroll_offset, target, animation_config); | ||
| 2022 | + } else { | ||
| 2023 | + monitor.scroll_offset = target; | ||
| 2024 | + arrange(monitor); | ||
| 2025 | + } | ||
| 2026 | +} | ||
| 2027 | + | ||
| 2028 | +fn showhide_client(display: *Display, client: ?*Client) void { | ||
| 2029 | + const target = client orelse return; | ||
| 2030 | + if (client_mod.is_visible(target)) { | ||
| 2031 | + _ = xlib.XMoveWindow(display.handle, target.window, target.x, target.y); | ||
| 2032 | + const monitor = target.monitor orelse return; | ||
| 2033 | + if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) { | ||
| 2034 | + tiling.resize(target, target.x, target.y, target.width, target.height, false); | ||
| 2035 | + } | ||
| 2036 | + showhide_client(display, target.stack_next); | ||
| 2037 | + } else { | ||
| 2038 | + showhide_client(display, target.stack_next); | ||
| 2039 | + const client_width = target.width + 2 * target.border_width; | ||
| 2040 | + _ = xlib.XMoveWindow(display.handle, target.window, -2 * client_width, target.y); | ||
| 2041 | + } | ||
| 2042 | +} | ||
| 2043 | + | ||
| 2044 | +fn showhide(display: *Display, monitor: *Monitor) void { | ||
| 2045 | + showhide_client(display, monitor.stack); | ||
| 2046 | +} | ||
| 2047 | + | ||
| 2048 | +fn update_size_hints(display: *Display, client: *Client) void { | ||
| 2049 | + var size_hints: xlib.XSizeHints = undefined; | ||
| 2050 | + var msize: c_long = 0; | ||
| 2051 | + | ||
| 2052 | + if (xlib.XGetWMNormalHints(display.handle, client.window, &size_hints, &msize) == 0) { | ||
| 2053 | + size_hints.flags = xlib.PSize; | ||
| 2054 | + } | ||
| 2055 | + | ||
| 2056 | + if ((size_hints.flags & xlib.PBaseSize) != 0) { | ||
| 2057 | + client.base_width = size_hints.base_width; | ||
| 2058 | + client.base_height = size_hints.base_height; | ||
| 2059 | + } else if ((size_hints.flags & xlib.PMinSize) != 0) { | ||
| 2060 | + client.base_width = size_hints.min_width; | ||
| 2061 | + client.base_height = size_hints.min_height; | ||
| 2062 | + } else { | ||
| 2063 | + client.base_width = 0; | ||
| 2064 | + client.base_height = 0; | ||
| 2065 | + } | ||
| 2066 | + | ||
| 2067 | + if ((size_hints.flags & xlib.PResizeInc) != 0) { | ||
| 2068 | + client.increment_width = size_hints.width_inc; | ||
| 2069 | + client.increment_height = size_hints.height_inc; | ||
| 2070 | + } else { | ||
| 2071 | + client.increment_width = 0; | ||
| 2072 | + client.increment_height = 0; | ||
| 2073 | + } | ||
| 2074 | + | ||
| 2075 | + if ((size_hints.flags & xlib.PMaxSize) != 0) { | ||
| 2076 | + client.max_width = size_hints.max_width; | ||
| 2077 | + client.max_height = size_hints.max_height; | ||
| 2078 | + } else { | ||
| 2079 | + client.max_width = 0; | ||
| 2080 | + client.max_height = 0; | ||
| 2081 | + } | ||
| 2082 | + | ||
| 2083 | + if ((size_hints.flags & xlib.PMinSize) != 0) { | ||
| 2084 | + client.min_width = size_hints.min_width; | ||
| 2085 | + client.min_height = size_hints.min_height; | ||
| 2086 | + } else if ((size_hints.flags & xlib.PBaseSize) != 0) { | ||
| 2087 | + client.min_width = size_hints.base_width; | ||
| 2088 | + client.min_height = size_hints.base_height; | ||
| 2089 | + } else { | ||
| 2090 | + client.min_width = 0; | ||
| 2091 | + client.min_height = 0; | ||
| 2092 | + } | ||
| 2093 | + | ||
| 2094 | + if ((size_hints.flags & xlib.PAspect) != 0) { | ||
| 2095 | + client.min_aspect = @as(f32, @floatFromInt(size_hints.min_aspect.y)) / @as(f32, @floatFromInt(size_hints.min_aspect.x)); | ||
| 2096 | + client.max_aspect = @as(f32, @floatFromInt(size_hints.max_aspect.x)) / @as(f32, @floatFromInt(size_hints.max_aspect.y)); | ||
| 2097 | + } else { | ||
| 2098 | + client.min_aspect = 0.0; | ||
| 2099 | + client.max_aspect = 0.0; | ||
| 2100 | + } | ||
| 2101 | + | ||
| 2102 | + client.is_fixed = (client.max_width != 0 and client.max_height != 0 and client.max_width == client.min_width and client.max_height == client.min_height); | ||
| 2103 | + client.hints_valid = true; | ||
| 2104 | +} | ||
| 2105 | + | ||
| 2106 | +fn update_wm_hints(display: *Display, client: *Client) void { | ||
| 2107 | + const wmh = xlib.XGetWMHints(display.handle, client.window); | ||
| 2108 | + if (wmh) |hints| { | ||
| 2109 | + defer _ = xlib.XFree(@ptrCast(hints)); | ||
| 2110 | + | ||
| 2111 | + if (client == (monitor_mod.selected_monitor orelse return).sel and (hints.*.flags & xlib.XUrgencyHint) != 0) { | ||
| 2112 | + hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint); | ||
| 2113 | + _ = xlib.XSetWMHints(display.handle, client.window, hints); | ||
| 2114 | + } else { | ||
| 2115 | + client.is_urgent = (hints.*.flags & xlib.XUrgencyHint) != 0; | ||
| 2116 | + } | ||
| 2117 | + | ||
| 2118 | + if ((hints.*.flags & xlib.InputHint) != 0) { | ||
| 2119 | + client.never_focus = hints.*.input == 0; | ||
| 2120 | + } else { | ||
| 2121 | + client.never_focus = false; | ||
| 2122 | + } | ||
| 2123 | + } | ||
| 2124 | +} | ||
| 2125 | + | ||
| 2126 | +fn update_window_type(display: *Display, client: *Client) void { | ||
| 2127 | + const state = get_atom_prop(display, client, net_wm_state); | ||
| 2128 | + const window_type = get_atom_prop(display, client, net_wm_window_type); | ||
| 2129 | + | ||
| 2130 | + if (state == net_wm_state_fullscreen) { | ||
| 2131 | + set_fullscreen(display, client, true); | ||
| 2132 | + } | ||
| 2133 | + if (window_type == net_wm_window_type_dialog) { | ||
| 2134 | + client.is_floating = true; | ||
| 2135 | + } | ||
| 2136 | +} | ||
| 2137 | + | ||
| 2138 | +fn update_title(display: *Display, client: *Client) void { | ||
| 2139 | + if (!get_text_prop(display, client.window, net_wm_name, &client.name)) { | ||
| 2140 | + _ = get_text_prop(display, client.window, xlib.XA_WM_NAME, &client.name); | ||
| 2141 | + } | ||
| 2142 | + if (client.name[0] == 0) { | ||
| 2143 | + @memcpy(client.name[0..6], "broken"); | ||
| 2144 | + } | ||
| 2145 | +} | ||
| 2146 | + | ||
| 2147 | +fn get_atom_prop(display: *Display, client: *Client, prop: xlib.Atom) xlib.Atom { | ||
| 2148 | + var actual_type: xlib.Atom = undefined; | ||
| 2149 | + var actual_format: c_int = undefined; | ||
| 2150 | + var num_items: c_ulong = undefined; | ||
| 2151 | + var bytes_after: c_ulong = undefined; | ||
| 2152 | + var prop_data: [*c]u8 = undefined; | ||
| 2153 | + | ||
| 2154 | + if (xlib.XGetWindowProperty(display.handle, client.window, prop, 0, @sizeOf(xlib.Atom), xlib.False, xlib.XA_ATOM, &actual_type, &actual_format, &num_items, &bytes_after, &prop_data) == 0 and prop_data != null) { | ||
| 2155 | + const atom: xlib.Atom = @as(*xlib.Atom, @ptrCast(@alignCast(prop_data))).*; | ||
| 2156 | + _ = xlib.XFree(@ptrCast(prop_data)); | ||
| 2157 | + return atom; | ||
| 2158 | + } | ||
| 2159 | + return 0; | ||
| 2160 | +} | ||
| 2161 | + | ||
| 2162 | +fn get_text_prop(display: *Display, window: xlib.Window, atom: xlib.Atom, text: *[256]u8) bool { | ||
| 2163 | + var name: xlib.XTextProperty = undefined; | ||
| 2164 | + text[0] = 0; | ||
| 2165 | + | ||
| 2166 | + if (xlib.XGetTextProperty(display.handle, window, &name, atom) == 0 or name.nitems == 0) { | ||
| 2167 | + return false; | ||
| 2168 | + } | ||
| 2169 | + | ||
| 2170 | + if (name.encoding == xlib.XA_STRING) { | ||
| 2171 | + const len = @min(name.nitems, 255); | ||
| 2172 | + @memcpy(text[0..len], name.value[0..len]); | ||
| 2173 | + text[len] = 0; | ||
| 2174 | + } else { | ||
| 2175 | + var list: [*c][*c]u8 = undefined; | ||
| 2176 | + var count: c_int = undefined; | ||
| 2177 | + if (xlib.XmbTextPropertyToTextList(display.handle, &name, &list, &count) >= xlib.Success and count > 0 and list[0] != null) { | ||
| 2178 | + const str = std.mem.sliceTo(list[0], 0); | ||
| 2179 | + const copy_len = @min(str.len, 255); | ||
| 2180 | + @memcpy(text[0..copy_len], str[0..copy_len]); | ||
| 2181 | + text[copy_len] = 0; | ||
| 2182 | + xlib.XFreeStringList(list); | ||
| 2183 | + } | ||
| 2184 | + } | ||
| 2185 | + text[255] = 0; | ||
| 2186 | + _ = xlib.XFree(@ptrCast(name.value)); | ||
| 2187 | + return true; | ||
| 2188 | +} | ||
| 2189 | + | ||
| 2190 | +fn apply_rules(display: *Display, client: *Client) void { | ||
| 2191 | + var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null }; | ||
| 2192 | + _ = xlib.XGetClassHint(display.handle, client.window, &class_hint); | ||
| 2193 | + | ||
| 2194 | + const class_str: []const u8 = if (class_hint.res_class) |ptr| std.mem.sliceTo(ptr, 0) else ""; | ||
| 2195 | + const instance_str: []const u8 = if (class_hint.res_name) |ptr| std.mem.sliceTo(ptr, 0) else ""; | ||
| 2196 | + | ||
| 2197 | + client.is_floating = false; | ||
| 2198 | + client.tags = 0; | ||
| 2199 | + | ||
| 2200 | + for (config.rules.items) |rule| { | ||
| 2201 | + const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true; | ||
| 2202 | + const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true; | ||
| 2203 | + const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true; | ||
| 2204 | + | ||
| 2205 | + if (class_matches and instance_matches and title_matches) { | ||
| 2206 | + client.is_floating = rule.is_floating; | ||
| 2207 | + client.tags |= rule.tags; | ||
| 2208 | + if (rule.monitor >= 0) { | ||
| 2209 | + var target = monitor_mod.monitors; | ||
| 2210 | + var index: i32 = 0; | ||
| 2211 | + while (target) |mon| { | ||
| 2212 | + if (index == rule.monitor) { | ||
| 2213 | + client.monitor = mon; | ||
| 2214 | + break; | ||
| 2215 | + } | ||
| 2216 | + index += 1; | ||
| 2217 | + target = mon.next; | ||
| 2218 | + } | ||
| 2219 | + } | ||
| 2220 | + } | ||
| 2221 | + } | ||
| 2222 | + | ||
| 2223 | + if (class_hint.res_class) |ptr| { | ||
| 2224 | + _ = xlib.XFree(@ptrCast(ptr)); | ||
| 2225 | + } | ||
| 2226 | + if (class_hint.res_name) |ptr| { | ||
| 2227 | + _ = xlib.XFree(@ptrCast(ptr)); | ||
| 2228 | + } | ||
| 2229 | + | ||
| 2230 | + const monitor = client.monitor orelse return; | ||
| 2231 | + if (client.tags == 0) { | ||
| 2232 | + client.tags = monitor.tagset[monitor.sel_tags]; | ||
| 2233 | + } | ||
| 2234 | +} | ||
| 2235 | + | ||
| 2236 | +fn update_client_list(display: *Display) void { | ||
| 2237 | + _ = xlib.XDeleteProperty(display.handle, display.root, net_client_list); | ||
| 2238 | + | ||
| 2239 | + var current_monitor = monitor_mod.monitors; | ||
| 2240 | + while (current_monitor) |monitor| { | ||
| 2241 | + var current_client = monitor.clients; | ||
| 2242 | + while (current_client) |client| { | ||
| 2243 | + _ = xlib.XChangeProperty(display.handle, display.root, net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1); | ||
| 2244 | + current_client = client.next; | ||
| 2245 | + } | ||
| 2246 | + current_monitor = monitor.next; | ||
| 2247 | + } | ||
| 2248 | +} | ||
| 2249 | + | ||
| 2250 | +fn send_event(display: *Display, client: *Client, protocol: xlib.Atom) bool { | ||
| 2251 | + var protocols: [*c]xlib.Atom = undefined; | ||
| 2252 | + var num_protocols: c_int = 0; | ||
| 2253 | + var exists = false; | ||
| 2254 | + | ||
| 2255 | + if (xlib.XGetWMProtocols(display.handle, client.window, &protocols, &num_protocols) != 0) { | ||
| 2256 | + var index: usize = 0; | ||
| 2257 | + while (index < @as(usize, @intCast(num_protocols))) : (index += 1) { | ||
| 2258 | + if (protocols[index] == protocol) { | ||
| 2259 | + exists = true; | ||
| 2260 | + break; | ||
| 2261 | + } | ||
| 2262 | + } | ||
| 2263 | + _ = xlib.XFree(@ptrCast(protocols)); | ||
| 2264 | + } | ||
| 2265 | + | ||
| 2266 | + if (exists) { | ||
| 2267 | + var event: xlib.XEvent = undefined; | ||
| 2268 | + event.type = xlib.ClientMessage; | ||
| 2269 | + event.xclient.window = client.window; | ||
| 2270 | + event.xclient.message_type = wm_protocols; | ||
| 2271 | + event.xclient.format = 32; | ||
| 2272 | + event.xclient.data.l[0] = @intCast(protocol); | ||
| 2273 | + event.xclient.data.l[1] = xlib.CurrentTime; | ||
| 2274 | + _ = xlib.XSendEvent(display.handle, client.window, xlib.False, xlib.NoEventMask, &event); | ||
| 2275 | + } | ||
| 2276 | + return exists; | ||
| 2277 | +} | ||
| 2278 | + | ||
| 2279 | +fn set_urgent(display: *Display, client: *Client, urgent: bool) void { | ||
| 2280 | + client.is_urgent = urgent; | ||
| 2281 | + const wmh = xlib.XGetWMHints(display.handle, client.window); | ||
| 2282 | + if (wmh) |hints| { | ||
| 2283 | + if (urgent) { | ||
| 2284 | + hints.*.flags = hints.*.flags | xlib.XUrgencyHint; | ||
| 2285 | + } else { | ||
| 2286 | + hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint); | ||
| 2287 | + } | ||
| 2288 | + _ = xlib.XSetWMHints(display.handle, client.window, hints); | ||
| 2289 | + _ = xlib.XFree(@ptrCast(hints)); | ||
| 2290 | + } | ||
| 2291 | +} | ||
+0 −198 src/monitor.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,198 +0,0 @@ | |||
| 1 | -use crate::client::TagMask; | ||
| 2 | -use crate::errors::WmError; | ||
| 3 | -use x11rb::protocol::xinerama::ConnectionExt as _; | ||
| 4 | -use x11rb::protocol::xproto::{Screen, Window}; | ||
| 5 | -use x11rb::rust_connection::RustConnection; | ||
| 6 | - | ||
| 7 | -type WmResult<T> = Result<T, WmError>; | ||
| 8 | - | ||
| 9 | -#[derive(Debug, Clone)] | ||
| 10 | -pub struct Pertag { | ||
| 11 | - pub current_tag: usize, | ||
| 12 | - pub previous_tag: usize, | ||
| 13 | - pub num_masters: Vec<i32>, | ||
| 14 | - pub master_factors: Vec<f32>, | ||
| 15 | - pub layouts: Vec<String>, | ||
| 16 | - pub show_bars: Vec<bool>, | ||
| 17 | -} | ||
| 18 | - | ||
| 19 | -impl Pertag { | ||
| 20 | - pub fn new( | ||
| 21 | - num_tags: usize, | ||
| 22 | - default_num_master: i32, | ||
| 23 | - default_master_factor: f32, | ||
| 24 | - default_show_bar: bool, | ||
| 25 | - default_layout: &str, | ||
| 26 | - ) -> Self { | ||
| 27 | - let len = num_tags + 1; | ||
| 28 | - Self { | ||
| 29 | - current_tag: 1, | ||
| 30 | - previous_tag: 1, | ||
| 31 | - num_masters: vec![default_num_master; len], | ||
| 32 | - master_factors: vec![default_master_factor; len], | ||
| 33 | - layouts: vec![default_layout.to_string(); len], | ||
| 34 | - show_bars: vec![default_show_bar; len], | ||
| 35 | - } | ||
| 36 | - } | ||
| 37 | -} | ||
| 38 | - | ||
| 39 | -#[derive(Debug, Clone)] | ||
| 40 | -pub struct Monitor { | ||
| 41 | - pub layout_symbol: String, | ||
| 42 | - pub master_factor: f32, | ||
| 43 | - pub num_master: i32, | ||
| 44 | - pub monitor_number: usize, | ||
| 45 | - pub bar_y_position: i32, | ||
| 46 | - pub screen_x: i32, | ||
| 47 | - pub screen_y: i32, | ||
| 48 | - pub screen_width: i32, | ||
| 49 | - pub screen_height: i32, | ||
| 50 | - pub window_area_x: i32, | ||
| 51 | - pub window_area_y: i32, | ||
| 52 | - pub window_area_width: i32, | ||
| 53 | - pub window_area_height: i32, | ||
| 54 | - pub gap_inner_horizontal: i32, | ||
| 55 | - pub gap_inner_vertical: i32, | ||
| 56 | - pub gap_outer_horizontal: i32, | ||
| 57 | - pub gap_outer_vertical: i32, | ||
| 58 | - pub selected_tags_index: usize, | ||
| 59 | - pub selected_layout_index: usize, | ||
| 60 | - pub tagset: [u32; 2], | ||
| 61 | - pub show_bar: bool, | ||
| 62 | - pub top_bar: bool, | ||
| 63 | - pub clients_head: Option<Window>, | ||
| 64 | - pub selected_client: Option<Window>, | ||
| 65 | - pub stack_head: Option<Window>, | ||
| 66 | - pub bar_window: Option<Window>, | ||
| 67 | - pub layout_indices: [usize; 2], | ||
| 68 | - pub scroll_offset: i32, | ||
| 69 | - pub pertag: Option<Pertag>, | ||
| 70 | -} | ||
| 71 | - | ||
| 72 | -impl Monitor { | ||
| 73 | - pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self { | ||
| 74 | - Self { | ||
| 75 | - layout_symbol: String::from("[]"), | ||
| 76 | - master_factor: 0.55, | ||
| 77 | - num_master: 1, | ||
| 78 | - monitor_number: 0, | ||
| 79 | - bar_y_position: 0, | ||
| 80 | - screen_x: x, | ||
| 81 | - screen_y: y, | ||
| 82 | - screen_width: width as i32, | ||
| 83 | - screen_height: height as i32, | ||
| 84 | - window_area_x: x, | ||
| 85 | - window_area_y: y, | ||
| 86 | - window_area_width: width as i32, | ||
| 87 | - window_area_height: height as i32, | ||
| 88 | - gap_inner_horizontal: 3, | ||
| 89 | - gap_inner_vertical: 3, | ||
| 90 | - gap_outer_horizontal: 3, | ||
| 91 | - gap_outer_vertical: 3, | ||
| 92 | - selected_tags_index: 0, | ||
| 93 | - selected_layout_index: 0, | ||
| 94 | - tagset: [1, 1], | ||
| 95 | - show_bar: true, | ||
| 96 | - top_bar: true, | ||
| 97 | - clients_head: None, | ||
| 98 | - selected_client: None, | ||
| 99 | - stack_head: None, | ||
| 100 | - bar_window: None, | ||
| 101 | - layout_indices: [0, 1], | ||
| 102 | - scroll_offset: 0, | ||
| 103 | - pertag: None, | ||
| 104 | - } | ||
| 105 | - } | ||
| 106 | - | ||
| 107 | - pub fn init_pertag(&mut self, num_tags: usize, default_layout: &str) { | ||
| 108 | - self.pertag = Some(Pertag::new( | ||
| 109 | - num_tags, | ||
| 110 | - self.num_master, | ||
| 111 | - self.master_factor, | ||
| 112 | - self.show_bar, | ||
| 113 | - default_layout, | ||
| 114 | - )); | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - pub fn contains_point(&self, x: i32, y: i32) -> bool { | ||
| 118 | - x >= self.screen_x | ||
| 119 | - && x < self.screen_x + self.screen_width | ||
| 120 | - && y >= self.screen_y | ||
| 121 | - && y < self.screen_y + self.screen_height | ||
| 122 | - } | ||
| 123 | - | ||
| 124 | - pub fn get_selected_tag(&self) -> TagMask { | ||
| 125 | - self.tagset[self.selected_tags_index] | ||
| 126 | - } | ||
| 127 | -} | ||
| 128 | - | ||
| 129 | -pub fn detect_monitors( | ||
| 130 | - connection: &RustConnection, | ||
| 131 | - screen: &Screen, | ||
| 132 | - _root: Window, | ||
| 133 | -) -> WmResult<Vec<Monitor>> { | ||
| 134 | - let fallback_monitors = || { | ||
| 135 | - vec![Monitor::new( | ||
| 136 | - 0, | ||
| 137 | - 0, | ||
| 138 | - screen.width_in_pixels as u32, | ||
| 139 | - screen.height_in_pixels as u32, | ||
| 140 | - )] | ||
| 141 | - }; | ||
| 142 | - | ||
| 143 | - let mut monitors = Vec::<Monitor>::new(); | ||
| 144 | - | ||
| 145 | - let xinerama_active = connection | ||
| 146 | - .xinerama_is_active() | ||
| 147 | - .ok() | ||
| 148 | - .and_then(|cookie| cookie.reply().ok()) | ||
| 149 | - .is_some_and(|reply| reply.state != 0); | ||
| 150 | - | ||
| 151 | - if xinerama_active { | ||
| 152 | - let Ok(xinerama_cookie) = connection.xinerama_query_screens() else { | ||
| 153 | - return Ok(fallback_monitors()); | ||
| 154 | - }; | ||
| 155 | - let Ok(xinerama_reply) = xinerama_cookie.reply() else { | ||
| 156 | - return Ok(fallback_monitors()); | ||
| 157 | - }; | ||
| 158 | - | ||
| 159 | - for screen_info in &xinerama_reply.screen_info { | ||
| 160 | - let has_valid_dimensions = screen_info.width > 0 && screen_info.height > 0; | ||
| 161 | - if !has_valid_dimensions { | ||
| 162 | - continue; | ||
| 163 | - } | ||
| 164 | - | ||
| 165 | - let x_position = screen_info.x_org as i32; | ||
| 166 | - let y_position = screen_info.y_org as i32; | ||
| 167 | - let width_in_pixels = screen_info.width as u32; | ||
| 168 | - let height_in_pixels = screen_info.height as u32; | ||
| 169 | - | ||
| 170 | - let is_duplicate_monitor = monitors.iter().any(|monitor| { | ||
| 171 | - monitor.screen_x == x_position | ||
| 172 | - && monitor.screen_y == y_position | ||
| 173 | - && monitor.screen_width == width_in_pixels as i32 | ||
| 174 | - && monitor.screen_height == height_in_pixels as i32 | ||
| 175 | - }); | ||
| 176 | - | ||
| 177 | - if !is_duplicate_monitor { | ||
| 178 | - monitors.push(Monitor::new( | ||
| 179 | - x_position, | ||
| 180 | - y_position, | ||
| 181 | - width_in_pixels, | ||
| 182 | - height_in_pixels, | ||
| 183 | - )); | ||
| 184 | - } | ||
| 185 | - } | ||
| 186 | - } | ||
| 187 | - | ||
| 188 | - if monitors.is_empty() { | ||
| 189 | - monitors = fallback_monitors(); | ||
| 190 | - } | ||
| 191 | - | ||
| 192 | - monitors.sort_by(|a, b| match a.screen_y.cmp(&b.screen_y) { | ||
| 193 | - std::cmp::Ordering::Equal => a.screen_x.cmp(&b.screen_x), | ||
| 194 | - other => other, | ||
| 195 | - }); | ||
| 196 | - | ||
| 197 | - Ok(monitors) | ||
| 198 | -} | ||
+154 −0 src/monitor.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,154 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const xlib = @import("x11/xlib.zig"); | ||
| 3 | +const Client = @import("client.zig").Client; | ||
| 4 | + | ||
| 5 | +pub const Layout = struct { | ||
| 6 | + symbol: []const u8, | ||
| 7 | + arrange_fn: ?*const fn (*Monitor) void, | ||
| 8 | +}; | ||
| 9 | + | ||
| 10 | +pub const Pertag = struct { | ||
| 11 | + curtag: u32 = 1, | ||
| 12 | + prevtag: u32 = 1, | ||
| 13 | + nmasters: [10]i32 = [_]i32{1} ** 10, | ||
| 14 | + mfacts: [10]f32 = [_]f32{0.55} ** 10, | ||
| 15 | + sellts: [10]u32 = [_]u32{0} ** 10, | ||
| 16 | + ltidxs: [10][4]?*const Layout = [_][4]?*const Layout{.{ null, null, null, null }} ** 10, | ||
| 17 | + showbars: [10]bool = [_]bool{true} ** 10, | ||
| 18 | +}; | ||
| 19 | + | ||
| 20 | +pub const Monitor = struct { | ||
| 21 | + lt_symbol: [16]u8 = std.mem.zeroes([16]u8), | ||
| 22 | + mfact: f32 = 0.55, | ||
| 23 | + nmaster: i32 = 1, | ||
| 24 | + num: i32 = 0, | ||
| 25 | + bar_y: i32 = 0, | ||
| 26 | + mon_x: i32 = 0, | ||
| 27 | + mon_y: i32 = 0, | ||
| 28 | + mon_w: i32 = 0, | ||
| 29 | + mon_h: i32 = 0, | ||
| 30 | + win_x: i32 = 0, | ||
| 31 | + win_y: i32 = 0, | ||
| 32 | + win_w: i32 = 0, | ||
| 33 | + win_h: i32 = 0, | ||
| 34 | + gap_inner_h: i32 = 0, | ||
| 35 | + gap_inner_v: i32 = 0, | ||
| 36 | + gap_outer_h: i32 = 0, | ||
| 37 | + gap_outer_v: i32 = 0, | ||
| 38 | + scroll_offset: i32 = 0, | ||
| 39 | + sel_tags: u32 = 0, | ||
| 40 | + sel_lt: u32 = 0, | ||
| 41 | + tagset: [2]u32 = .{ 1, 1 }, | ||
| 42 | + show_bar: bool = true, | ||
| 43 | + top_bar: bool = true, | ||
| 44 | + clients: ?*Client = null, | ||
| 45 | + sel: ?*Client = null, | ||
| 46 | + stack: ?*Client = null, | ||
| 47 | + next: ?*Monitor = null, | ||
| 48 | + bar_win: xlib.Window = 0, | ||
| 49 | + lt: [4]?*const Layout = .{ null, null, null, null }, | ||
| 50 | + pertag: Pertag = Pertag{}, | ||
| 51 | +}; | ||
| 52 | + | ||
| 53 | +pub var monitors: ?*Monitor = null; | ||
| 54 | +pub var selected_monitor: ?*Monitor = null; | ||
| 55 | + | ||
| 56 | +var allocator: std.mem.Allocator = undefined; | ||
| 57 | + | ||
| 58 | +pub fn init(alloc: std.mem.Allocator) void { | ||
| 59 | + allocator = alloc; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +pub fn create() ?*Monitor { | ||
| 63 | + const mon = allocator.create(Monitor) catch return null; | ||
| 64 | + mon.* = Monitor{}; | ||
| 65 | + return mon; | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +pub fn destroy(mon: *Monitor) void { | ||
| 69 | + allocator.destroy(mon); | ||
| 70 | +} | ||
| 71 | + | ||
| 72 | +var root_window: xlib.Window = 0; | ||
| 73 | +var display_handle: ?*xlib.Display = null; | ||
| 74 | + | ||
| 75 | +pub fn set_root_window(root: xlib.Window, display: *xlib.Display) void { | ||
| 76 | + root_window = root; | ||
| 77 | + display_handle = display; | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +pub fn window_to_monitor(win: xlib.Window) ?*Monitor { | ||
| 81 | + if (win == root_window and display_handle != null) { | ||
| 82 | + var root_x: c_int = undefined; | ||
| 83 | + var root_y: c_int = undefined; | ||
| 84 | + var dummy_win: xlib.Window = undefined; | ||
| 85 | + var dummy_int: c_int = undefined; | ||
| 86 | + var dummy_uint: c_uint = undefined; | ||
| 87 | + if (xlib.XQueryPointer(display_handle.?, root_window, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) { | ||
| 88 | + return rect_to_monitor(root_x, root_y, 1, 1); | ||
| 89 | + } | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + var current = monitors; | ||
| 93 | + while (current) |monitor| { | ||
| 94 | + if (monitor.bar_win == win) { | ||
| 95 | + return monitor; | ||
| 96 | + } | ||
| 97 | + current = monitor.next; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + const client = @import("client.zig").window_to_client(win); | ||
| 101 | + if (client) |found_client| { | ||
| 102 | + return found_client.monitor; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + return selected_monitor; | ||
| 106 | +} | ||
| 107 | + | ||
| 108 | +pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor { | ||
| 109 | + var result = selected_monitor; | ||
| 110 | + var max_area: i32 = 0; | ||
| 111 | + | ||
| 112 | + var current = monitors; | ||
| 113 | + while (current) |monitor| { | ||
| 114 | + const intersect_x = @max(0, @min(x + width, monitor.win_x + monitor.win_w) - @max(x, monitor.win_x)); | ||
| 115 | + const intersect_y = @max(0, @min(y + height, monitor.win_y + monitor.win_h) - @max(y, monitor.win_y)); | ||
| 116 | + const area = intersect_x * intersect_y; | ||
| 117 | + if (area > max_area) { | ||
| 118 | + max_area = area; | ||
| 119 | + result = monitor; | ||
| 120 | + } | ||
| 121 | + current = monitor.next; | ||
| 122 | + } | ||
| 123 | + return result; | ||
| 124 | +} | ||
| 125 | + | ||
| 126 | +pub fn dir_to_monitor(direction: i32) ?*Monitor { | ||
| 127 | + var target: ?*Monitor = null; | ||
| 128 | + | ||
| 129 | + if (direction > 0) { | ||
| 130 | + target = if (selected_monitor) |current| current.next else null; | ||
| 131 | + if (target == null) { | ||
| 132 | + target = monitors; | ||
| 133 | + } | ||
| 134 | + } else if (selected_monitor == monitors) { | ||
| 135 | + var last = monitors; | ||
| 136 | + while (last) |iter| { | ||
| 137 | + if (iter.next == null) { | ||
| 138 | + target = iter; | ||
| 139 | + break; | ||
| 140 | + } | ||
| 141 | + last = iter.next; | ||
| 142 | + } | ||
| 143 | + } else { | ||
| 144 | + var previous = monitors; | ||
| 145 | + while (previous) |iter| { | ||
| 146 | + if (iter.next == selected_monitor) { | ||
| 147 | + target = iter; | ||
| 148 | + break; | ||
| 149 | + } | ||
| 150 | + previous = iter.next; | ||
| 151 | + } | ||
| 152 | + } | ||
| 153 | + return target; | ||
| 154 | +} | ||
+0 −148 src/overlay/error.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,148 +0,0 @@ | |||
| 1 | -use super::{Overlay, OverlayBase}; | ||
| 2 | -use crate::bar::font::Font; | ||
| 3 | -use crate::errors::{ConfigError, X11Error}; | ||
| 4 | -use x11rb::connection::Connection; | ||
| 5 | -use x11rb::protocol::xproto::*; | ||
| 6 | -use x11rb::rust_connection::RustConnection; | ||
| 7 | - | ||
| 8 | -const PADDING: i16 = 20; | ||
| 9 | -const LINE_SPACING: i16 = 5; | ||
| 10 | -const BORDER_WIDTH: u16 = 2; | ||
| 11 | -const BORDER_COLOR: u32 = 0xff5555; | ||
| 12 | - | ||
| 13 | -pub struct ErrorOverlay { | ||
| 14 | - base: OverlayBase, | ||
| 15 | - lines: Vec<String>, | ||
| 16 | -} | ||
| 17 | - | ||
| 18 | -impl ErrorOverlay { | ||
| 19 | - pub fn new( | ||
| 20 | - connection: &RustConnection, | ||
| 21 | - screen: &Screen, | ||
| 22 | - screen_num: usize, | ||
| 23 | - display: *mut x11::xlib::Display, | ||
| 24 | - _font: &Font, | ||
| 25 | - _max_width: u16, | ||
| 26 | - ) -> Result<Self, X11Error> { | ||
| 27 | - let base = OverlayBase::new( | ||
| 28 | - connection, | ||
| 29 | - screen, | ||
| 30 | - screen_num, | ||
| 31 | - display, | ||
| 32 | - 400, | ||
| 33 | - 200, | ||
| 34 | - BORDER_WIDTH, | ||
| 35 | - BORDER_COLOR, | ||
| 36 | - 0x1a1a1a, | ||
| 37 | - 0xffffff, | ||
| 38 | - )?; | ||
| 39 | - | ||
| 40 | - Ok(ErrorOverlay { | ||
| 41 | - base, | ||
| 42 | - lines: Vec::new(), | ||
| 43 | - }) | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - pub fn show_error( | ||
| 47 | - &mut self, | ||
| 48 | - connection: &RustConnection, | ||
| 49 | - font: &Font, | ||
| 50 | - error: ConfigError, | ||
| 51 | - monitor_x: i16, | ||
| 52 | - monitor_y: i16, | ||
| 53 | - screen_width: u16, | ||
| 54 | - screen_height: u16, | ||
| 55 | - ) -> Result<(), X11Error> { | ||
| 56 | - let max_line_width = (screen_width as i16 / 2 - PADDING * 4).max(300) as u16; | ||
| 57 | - let error_with_instruction = format!("{}\n\nFix the config file and reload.", error); | ||
| 58 | - self.lines = self.wrap_text(&error_with_instruction, font, max_line_width); | ||
| 59 | - | ||
| 60 | - let mut content_width = 0u16; | ||
| 61 | - for line in &self.lines { | ||
| 62 | - let line_width = font.text_width(line); | ||
| 63 | - if line_width > content_width { | ||
| 64 | - content_width = line_width; | ||
| 65 | - } | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - let width = content_width + (PADDING as u16 * 2); | ||
| 69 | - let line_height = font.height() + LINE_SPACING as u16; | ||
| 70 | - let height = (self.lines.len() as u16 * line_height) + (PADDING as u16 * 2); | ||
| 71 | - | ||
| 72 | - let x = monitor_x + ((screen_width - width) / 2) as i16; | ||
| 73 | - let y = monitor_y + ((screen_height - height) / 2) as i16; | ||
| 74 | - | ||
| 75 | - self.base.configure(connection, x, y, width, height)?; | ||
| 76 | - self.base.is_visible = true; | ||
| 77 | - self.draw(connection, font)?; | ||
| 78 | - self.base.show(connection)?; | ||
| 79 | - Ok(()) | ||
| 80 | - } | ||
| 81 | - | ||
| 82 | - fn wrap_text(&self, text: &str, font: &Font, max_width: u16) -> Vec<String> { | ||
| 83 | - let mut lines = Vec::new(); | ||
| 84 | - for paragraph in text.lines() { | ||
| 85 | - if paragraph.trim().is_empty() { | ||
| 86 | - lines.push(String::new()); | ||
| 87 | - continue; | ||
| 88 | - } | ||
| 89 | - | ||
| 90 | - let words: Vec<&str> = paragraph.split_whitespace().collect(); | ||
| 91 | - let mut current_line = String::new(); | ||
| 92 | - | ||
| 93 | - for word in words { | ||
| 94 | - let test_line = if current_line.is_empty() { | ||
| 95 | - word.to_string() | ||
| 96 | - } else { | ||
| 97 | - format!("{} {}", current_line, word) | ||
| 98 | - }; | ||
| 99 | - if font.text_width(&test_line) <= max_width { | ||
| 100 | - current_line = test_line; | ||
| 101 | - } else { | ||
| 102 | - if !current_line.is_empty() { | ||
| 103 | - lines.push(current_line); | ||
| 104 | - } | ||
| 105 | - current_line = word.to_string(); | ||
| 106 | - } | ||
| 107 | - } | ||
| 108 | - if !current_line.is_empty() { | ||
| 109 | - lines.push(current_line); | ||
| 110 | - } | ||
| 111 | - } | ||
| 112 | - lines | ||
| 113 | - } | ||
| 114 | -} | ||
| 115 | - | ||
| 116 | -impl Overlay for ErrorOverlay { | ||
| 117 | - fn window(&self) -> Window { | ||
| 118 | - self.base.window | ||
| 119 | - } | ||
| 120 | - | ||
| 121 | - fn is_visible(&self) -> bool { | ||
| 122 | - self.base.is_visible | ||
| 123 | - } | ||
| 124 | - | ||
| 125 | - fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | ||
| 126 | - self.base.hide(connection)?; | ||
| 127 | - self.lines.clear(); | ||
| 128 | - Ok(()) | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> { | ||
| 132 | - if !self.base.is_visible { | ||
| 133 | - return Ok(()); | ||
| 134 | - } | ||
| 135 | - self.base.draw_background(connection)?; | ||
| 136 | - let line_height = font.height() + LINE_SPACING as u16; | ||
| 137 | - let mut y = PADDING + font.ascent(); | ||
| 138 | - for line in &self.lines { | ||
| 139 | - self.base | ||
| 140 | - .font_draw | ||
| 141 | - .draw_text(font, self.base.foreground_color, PADDING, y, line); | ||
| 142 | - y += line_height as i16; | ||
| 143 | - } | ||
| 144 | - connection.flush()?; | ||
| 145 | - self.base.font_draw.sync(); | ||
| 146 | - Ok(()) | ||
| 147 | - } | ||
| 148 | -} | ||
+0 −320 src/overlay/keybind.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,320 +0,0 @@ | |||
| 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 | - monitor_x: i16, | ||
| 65 | - monitor_y: i16, | ||
| 66 | - screen_width: u16, | ||
| 67 | - screen_height: u16, | ||
| 68 | - ) -> Result<(), X11Error> { | ||
| 69 | - self.keybindings = self.collect_keybindings(keybindings); | ||
| 70 | - | ||
| 71 | - let title = "Important Keybindings"; | ||
| 72 | - let title_width = font.text_width(title); | ||
| 73 | - | ||
| 74 | - let mut max_key_width = 0u16; | ||
| 75 | - let mut max_action_width = 0u16; | ||
| 76 | - | ||
| 77 | - for (key, action) in &self.keybindings { | ||
| 78 | - let key_width = font.text_width(key); | ||
| 79 | - let action_width = font.text_width(action); | ||
| 80 | - if key_width > max_key_width { | ||
| 81 | - max_key_width = key_width; | ||
| 82 | - } | ||
| 83 | - if action_width > max_action_width { | ||
| 84 | - max_action_width = action_width; | ||
| 85 | - } | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - let content_width = max_key_width + KEY_ACTION_SPACING as u16 + max_action_width; | ||
| 89 | - let min_width = title_width.max(content_width); | ||
| 90 | - | ||
| 91 | - let width = min_width + (PADDING as u16 * 2); | ||
| 92 | - | ||
| 93 | - let line_height = font.height() + LINE_SPACING as u16; | ||
| 94 | - let title_height = font.height() + TITLE_BOTTOM_MARGIN as u16; | ||
| 95 | - let height = | ||
| 96 | - title_height + (self.keybindings.len() as u16 * line_height) + (PADDING as u16 * 2); | ||
| 97 | - | ||
| 98 | - let x = monitor_x + ((screen_width - width) / 2) as i16; | ||
| 99 | - let y = monitor_y + ((screen_height - height) / 2) as i16; | ||
| 100 | - | ||
| 101 | - self.base.configure(connection, x, y, width, height)?; | ||
| 102 | - | ||
| 103 | - self.last_shown_at = Some(Instant::now()); | ||
| 104 | - self.max_key_width = max_key_width; | ||
| 105 | - | ||
| 106 | - self.base.is_visible = true; | ||
| 107 | - self.draw(connection, font)?; | ||
| 108 | - | ||
| 109 | - self.base.show(connection)?; | ||
| 110 | - | ||
| 111 | - Ok(()) | ||
| 112 | - } | ||
| 113 | - | ||
| 114 | - pub fn toggle( | ||
| 115 | - &mut self, | ||
| 116 | - connection: &RustConnection, | ||
| 117 | - font: &Font, | ||
| 118 | - keybindings: &[KeyBinding], | ||
| 119 | - monitor_x: i16, | ||
| 120 | - monitor_y: i16, | ||
| 121 | - screen_width: u16, | ||
| 122 | - screen_height: u16, | ||
| 123 | - ) -> Result<(), X11Error> { | ||
| 124 | - if self.base.is_visible { | ||
| 125 | - self.hide(connection)?; | ||
| 126 | - } else { | ||
| 127 | - self.show( | ||
| 128 | - connection, | ||
| 129 | - font, | ||
| 130 | - keybindings, | ||
| 131 | - monitor_x, | ||
| 132 | - monitor_y, | ||
| 133 | - screen_width, | ||
| 134 | - screen_height, | ||
| 135 | - )?; | ||
| 136 | - } | ||
| 137 | - Ok(()) | ||
| 138 | - } | ||
| 139 | - | ||
| 140 | - pub fn should_suppress_input(&self) -> bool { | ||
| 141 | - if let Some(shown_at) = self.last_shown_at { | ||
| 142 | - shown_at.elapsed().as_millis() < INPUT_SUPPRESS_MS | ||
| 143 | - } else { | ||
| 144 | - false | ||
| 145 | - } | ||
| 146 | - } | ||
| 147 | - | ||
| 148 | - fn collect_keybindings(&self, keybindings: &[KeyBinding]) -> Vec<(String, String)> { | ||
| 149 | - let mut result = Vec::new(); | ||
| 150 | - | ||
| 151 | - let priority_actions = [ | ||
| 152 | - KeyAction::ShowKeybindOverlay, | ||
| 153 | - KeyAction::Quit, | ||
| 154 | - KeyAction::Restart, | ||
| 155 | - KeyAction::KillClient, | ||
| 156 | - KeyAction::Spawn, | ||
| 157 | - KeyAction::SpawnTerminal, | ||
| 158 | - KeyAction::ToggleFullScreen, | ||
| 159 | - KeyAction::ToggleFloating, | ||
| 160 | - KeyAction::CycleLayout, | ||
| 161 | - KeyAction::FocusStack, | ||
| 162 | - KeyAction::ViewTag, | ||
| 163 | - ]; | ||
| 164 | - | ||
| 165 | - for &action in &priority_actions { | ||
| 166 | - let binding = keybindings | ||
| 167 | - .iter() | ||
| 168 | - .filter(|kb| kb.func == action) | ||
| 169 | - .min_by_key(|kb| kb.keys.len()); | ||
| 170 | - | ||
| 171 | - if let Some(binding) = binding | ||
| 172 | - && !binding.keys.is_empty() | ||
| 173 | - { | ||
| 174 | - let key_str = self.format_key_combo(&binding.keys[0]); | ||
| 175 | - let action_str = self.action_description(binding); | ||
| 176 | - result.push((key_str, action_str)); | ||
| 177 | - } | ||
| 178 | - } | ||
| 179 | - | ||
| 180 | - result | ||
| 181 | - } | ||
| 182 | - | ||
| 183 | - fn format_key_combo(&self, key: &KeyPress) -> String { | ||
| 184 | - let mut parts = Vec::new(); | ||
| 185 | - | ||
| 186 | - for modifier in &key.modifiers { | ||
| 187 | - let mod_str = match *modifier { | ||
| 188 | - m if m == self.modkey => "Mod", | ||
| 189 | - KeyButMask::SHIFT => "Shift", | ||
| 190 | - KeyButMask::CONTROL => "Ctrl", | ||
| 191 | - KeyButMask::MOD1 => "Alt", | ||
| 192 | - KeyButMask::MOD4 => "Super", | ||
| 193 | - _ => continue, | ||
| 194 | - }; | ||
| 195 | - parts.push(mod_str.to_string()); | ||
| 196 | - } | ||
| 197 | - | ||
| 198 | - parts.push(crate::keyboard::keysyms::format_keysym(key.keysym)); | ||
| 199 | - | ||
| 200 | - parts.join(" + ") | ||
| 201 | - } | ||
| 202 | - | ||
| 203 | - fn action_description(&self, binding: &KeyBinding) -> String { | ||
| 204 | - use crate::keyboard::Arg; | ||
| 205 | - | ||
| 206 | - match binding.func { | ||
| 207 | - KeyAction::ShowKeybindOverlay => "Show This Keybind Help".to_string(), | ||
| 208 | - KeyAction::Quit => "Quit Window Manager".to_string(), | ||
| 209 | - KeyAction::Restart => "Restart Window Manager".to_string(), | ||
| 210 | - KeyAction::KillClient => "Close Focused Window".to_string(), | ||
| 211 | - KeyAction::Spawn => match &binding.arg { | ||
| 212 | - Arg::Str(cmd) => format!("Launch: {}", cmd), | ||
| 213 | - Arg::Array(arr) if !arr.is_empty() => format!("Launch: {}", arr[0]), | ||
| 214 | - _ => "Launch Program".to_string(), | ||
| 215 | - }, | ||
| 216 | - KeyAction::SpawnTerminal => "Launch Terminal".to_string(), | ||
| 217 | - KeyAction::FocusStack => "Focus Next/Previous Window".to_string(), | ||
| 218 | - KeyAction::MoveStack => "Move Window Up/Down Stack".to_string(), | ||
| 219 | - KeyAction::ViewTag => match &binding.arg { | ||
| 220 | - Arg::Int(n) => format!("View Workspace {}", n), | ||
| 221 | - _ => "View Workspace".to_string(), | ||
| 222 | - }, | ||
| 223 | - KeyAction::ViewNextTag => "View Next Workspace".to_string(), | ||
| 224 | - KeyAction::ViewPreviousTag => "View Previous Workspace".to_string(), | ||
| 225 | - KeyAction::ViewNextNonEmptyTag => "View Next Non-Empty Workspace".to_string(), | ||
| 226 | - KeyAction::ViewPreviousNonEmptyTag => "View Previous Non-Empty Workspace".to_string(), | ||
| 227 | - KeyAction::ToggleView => match &binding.arg { | ||
| 228 | - Arg::Int(n) => format!("Toggle View Workspace {}", n), | ||
| 229 | - _ => "Toggle View Workspace".to_string(), | ||
| 230 | - }, | ||
| 231 | - KeyAction::MoveToTag => "Move Window to Workspace".to_string(), | ||
| 232 | - KeyAction::ToggleTag => "Toggle Window on Workspace".to_string(), | ||
| 233 | - KeyAction::ToggleGaps => "Toggle Window Gaps".to_string(), | ||
| 234 | - KeyAction::ToggleFullScreen => "Toggle Fullscreen Mode".to_string(), | ||
| 235 | - KeyAction::ToggleFloating => "Toggle Floating Mode".to_string(), | ||
| 236 | - KeyAction::ChangeLayout => "Change Layout".to_string(), | ||
| 237 | - KeyAction::CycleLayout => "Cycle Through Layouts".to_string(), | ||
| 238 | - KeyAction::FocusMonitor => "Focus Next Monitor".to_string(), | ||
| 239 | - KeyAction::TagMonitor => "Send Window to Monitor".to_string(), | ||
| 240 | - KeyAction::SetMasterFactor => "Adjust Master Area Size".to_string(), | ||
| 241 | - KeyAction::IncNumMaster => "Adjust Number of Master Windows".to_string(), | ||
| 242 | - KeyAction::ScrollLeft => "Scroll Layout Left".to_string(), | ||
| 243 | - KeyAction::ScrollRight => "Scroll Layout Right".to_string(), | ||
| 244 | - KeyAction::None => "No Action".to_string(), | ||
| 245 | - } | ||
| 246 | - } | ||
| 247 | -} | ||
| 248 | - | ||
| 249 | -impl Overlay for KeybindOverlay { | ||
| 250 | - fn window(&self) -> Window { | ||
| 251 | - self.base.window | ||
| 252 | - } | ||
| 253 | - | ||
| 254 | - fn is_visible(&self) -> bool { | ||
| 255 | - self.base.is_visible | ||
| 256 | - } | ||
| 257 | - | ||
| 258 | - fn hide(&mut self, connection: &RustConnection) -> Result<(), X11Error> { | ||
| 259 | - self.base.hide(connection)?; | ||
| 260 | - self.last_shown_at = None; | ||
| 261 | - self.keybindings.clear(); | ||
| 262 | - Ok(()) | ||
| 263 | - } | ||
| 264 | - | ||
| 265 | - fn draw(&self, connection: &RustConnection, font: &Font) -> Result<(), X11Error> { | ||
| 266 | - if !self.base.is_visible { | ||
| 267 | - return Ok(()); | ||
| 268 | - } | ||
| 269 | - | ||
| 270 | - self.base.draw_background(connection)?; | ||
| 271 | - | ||
| 272 | - let title = "Important Keybindings"; | ||
| 273 | - let title_width = font.text_width(title); | ||
| 274 | - let title_x = ((self.base.width - title_width) / 2) as i16; | ||
| 275 | - let title_y = PADDING + font.ascent(); | ||
| 276 | - | ||
| 277 | - self.base | ||
| 278 | - .font_draw | ||
| 279 | - .draw_text(font, self.base.foreground_color, title_x, title_y, title); | ||
| 280 | - | ||
| 281 | - let line_height = font.height() + LINE_SPACING as u16; | ||
| 282 | - let mut y = PADDING + font.height() as i16 + TITLE_BOTTOM_MARGIN + font.ascent(); | ||
| 283 | - | ||
| 284 | - for (key, action) in &self.keybindings { | ||
| 285 | - let key_width = font.text_width(key); | ||
| 286 | - let key_x = PADDING; | ||
| 287 | - | ||
| 288 | - connection.change_gc( | ||
| 289 | - self.base.graphics_context, | ||
| 290 | - &ChangeGCAux::new().foreground(self.key_bg_color), | ||
| 291 | - )?; | ||
| 292 | - connection.poly_fill_rectangle( | ||
| 293 | - self.base.window, | ||
| 294 | - self.base.graphics_context, | ||
| 295 | - &[Rectangle { | ||
| 296 | - x: key_x - 4, | ||
| 297 | - y: y - font.ascent() - 2, | ||
| 298 | - width: key_width + 8, | ||
| 299 | - height: font.height() + 4, | ||
| 300 | - }], | ||
| 301 | - )?; | ||
| 302 | - | ||
| 303 | - self.base | ||
| 304 | - .font_draw | ||
| 305 | - .draw_text(font, self.base.foreground_color, key_x, y, key); | ||
| 306 | - | ||
| 307 | - let action_x = PADDING + self.max_key_width as i16 + KEY_ACTION_SPACING; | ||
| 308 | - self.base | ||
| 309 | - .font_draw | ||
| 310 | - .draw_text(font, self.base.foreground_color, action_x, y, action); | ||
| 311 | - | ||
| 312 | - y += line_height as i16; | ||
| 313 | - } | ||
| 314 | - | ||
| 315 | - connection.flush()?; | ||
| 316 | - self.base.font_draw.sync(); | ||
| 317 | - | ||
| 318 | - Ok(()) | ||
| 319 | - } | ||
| 320 | -} | ||
+0 −156 src/overlay/mod.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,156 +0,0 @@ | |||
| 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 | -} | ||
+0 −32 src/signal.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,32 +0,0 @@ | |||
| 1 | -use std::process::{Command, Stdio}; | ||
| 2 | - | ||
| 3 | -pub fn spawn_detached(cmd: &str) { | ||
| 4 | - if let Ok(mut child) = Command::new("sh") | ||
| 5 | - .arg("-c") | ||
| 6 | - .arg(format!("({}) &", cmd)) | ||
| 7 | - .stdin(Stdio::null()) | ||
| 8 | - .stdout(Stdio::null()) | ||
| 9 | - .stderr(Stdio::null()) | ||
| 10 | - .spawn() | ||
| 11 | - { | ||
| 12 | - let _ = child.wait(); | ||
| 13 | - } | ||
| 14 | -} | ||
| 15 | - | ||
| 16 | -pub fn spawn_detached_with_args(program: &str, args: &[&str]) { | ||
| 17 | - let escaped_args: Vec<String> = args.iter().map(|a| shell_escape(a)).collect(); | ||
| 18 | - let full_cmd = if escaped_args.is_empty() { | ||
| 19 | - program.to_string() | ||
| 20 | - } else { | ||
| 21 | - format!("{} {}", program, escaped_args.join(" ")) | ||
| 22 | - }; | ||
| 23 | - spawn_detached(&full_cmd) | ||
| 24 | -} | ||
| 25 | - | ||
| 26 | -fn shell_escape(s: &str) -> String { | ||
| 27 | - if s.contains(|c: char| c.is_whitespace() || c == '\'' || c == '"' || c == '\\') { | ||
| 28 | - format!("'{}'", s.replace('\'', "'\\''")) | ||
| 29 | - } else { | ||
| 30 | - s.to_string() | ||
| 31 | - } | ||
| 32 | -} | ||
+0 −25 src/size_hints.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,25 +0,0 @@ | |||
| 1 | -#![allow(dead_code)] | ||
| 2 | - | ||
| 3 | -pub mod flags { | ||
| 4 | - pub const P_MIN_SIZE: u32 = 1 << 4; | ||
| 5 | - pub const P_MAX_SIZE: u32 = 1 << 5; | ||
| 6 | - pub const P_RESIZE_INC: u32 = 1 << 6; | ||
| 7 | - pub const P_ASPECT: u32 = 1 << 7; | ||
| 8 | - pub const P_BASE_SIZE: u32 = 1 << 8; | ||
| 9 | -} | ||
| 10 | - | ||
| 11 | -pub mod offset { | ||
| 12 | - pub const FLAGS: usize = 0; | ||
| 13 | - pub const MIN_WIDTH: usize = 5; | ||
| 14 | - pub const MIN_HEIGHT: usize = 6; | ||
| 15 | - pub const MAX_WIDTH: usize = 7; | ||
| 16 | - pub const MAX_HEIGHT: usize = 8; | ||
| 17 | - pub const WIDTH_INC: usize = 9; | ||
| 18 | - pub const HEIGHT_INC: usize = 10; | ||
| 19 | - pub const MIN_ASPECT_X: usize = 11; | ||
| 20 | - pub const MIN_ASPECT_Y: usize = 12; | ||
| 21 | - pub const MAX_ASPECT_X: usize = 13; | ||
| 22 | - pub const MAX_ASPECT_Y: usize = 14; | ||
| 23 | - pub const BASE_WIDTH: usize = 15; | ||
| 24 | - pub const BASE_HEIGHT: usize = 16; | ||
| 25 | -} | ||
+0 −278 src/tab_bar.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,278 +0,0 @@ | |||
| 1 | -use crate::ColorScheme; | ||
| 2 | -use crate::bar::font::{DrawingSurface, Font}; | ||
| 3 | -use crate::errors::X11Error; | ||
| 4 | -use crate::layout::tabbed::TAB_BAR_HEIGHT; | ||
| 5 | -use x11rb::COPY_DEPTH_FROM_PARENT; | ||
| 6 | -use x11rb::connection::Connection; | ||
| 7 | -use x11rb::protocol::xproto::*; | ||
| 8 | -use x11rb::rust_connection::RustConnection; | ||
| 9 | - | ||
| 10 | -pub struct TabBar { | ||
| 11 | - window: Window, | ||
| 12 | - width: u16, | ||
| 13 | - height: u16, | ||
| 14 | - x_offset: i16, | ||
| 15 | - y_offset: i16, | ||
| 16 | - graphics_context: Gcontext, | ||
| 17 | - display: *mut x11::xlib::Display, | ||
| 18 | - surface: DrawingSurface, | ||
| 19 | - scheme_normal: ColorScheme, | ||
| 20 | - scheme_selected: ColorScheme, | ||
| 21 | -} | ||
| 22 | - | ||
| 23 | -impl TabBar { | ||
| 24 | - pub fn new( | ||
| 25 | - connection: &RustConnection, | ||
| 26 | - screen: &Screen, | ||
| 27 | - screen_num: usize, | ||
| 28 | - display: *mut x11::xlib::Display, | ||
| 29 | - _font: &Font, | ||
| 30 | - x: i16, | ||
| 31 | - y: i16, | ||
| 32 | - width: u16, | ||
| 33 | - scheme_normal: ColorScheme, | ||
| 34 | - scheme_selected: ColorScheme, | ||
| 35 | - cursor: u32, | ||
| 36 | - ) -> Result<Self, X11Error> { | ||
| 37 | - let window = connection.generate_id()?; | ||
| 38 | - let graphics_context = connection.generate_id()?; | ||
| 39 | - | ||
| 40 | - let height = TAB_BAR_HEIGHT as u16; | ||
| 41 | - | ||
| 42 | - connection.create_window( | ||
| 43 | - COPY_DEPTH_FROM_PARENT, | ||
| 44 | - window, | ||
| 45 | - screen.root, | ||
| 46 | - x, | ||
| 47 | - y, | ||
| 48 | - width, | ||
| 49 | - height, | ||
| 50 | - 0, | ||
| 51 | - WindowClass::INPUT_OUTPUT, | ||
| 52 | - screen.root_visual, | ||
| 53 | - &CreateWindowAux::new() | ||
| 54 | - .background_pixel(scheme_normal.background) | ||
| 55 | - .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) | ||
| 56 | - .override_redirect(1), | ||
| 57 | - )?; | ||
| 58 | - | ||
| 59 | - connection.create_gc( | ||
| 60 | - graphics_context, | ||
| 61 | - window, | ||
| 62 | - &CreateGCAux::new() | ||
| 63 | - .foreground(scheme_normal.foreground) | ||
| 64 | - .background(scheme_normal.background), | ||
| 65 | - )?; | ||
| 66 | - | ||
| 67 | - unsafe { | ||
| 68 | - x11::xlib::XDefineCursor(display, window as u64, cursor as u64); | ||
| 69 | - } | ||
| 70 | - | ||
| 71 | - connection.map_window(window)?; | ||
| 72 | - connection.flush()?; | ||
| 73 | - | ||
| 74 | - let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; | ||
| 75 | - let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; | ||
| 76 | - | ||
| 77 | - let surface = DrawingSurface::new( | ||
| 78 | - display, | ||
| 79 | - window as x11::xlib::Drawable, | ||
| 80 | - width as u32, | ||
| 81 | - height as u32, | ||
| 82 | - visual, | ||
| 83 | - colormap, | ||
| 84 | - )?; | ||
| 85 | - | ||
| 86 | - Ok(Self { | ||
| 87 | - window, | ||
| 88 | - width, | ||
| 89 | - height, | ||
| 90 | - x_offset: x, | ||
| 91 | - y_offset: y, | ||
| 92 | - graphics_context, | ||
| 93 | - display, | ||
| 94 | - surface, | ||
| 95 | - scheme_normal, | ||
| 96 | - scheme_selected, | ||
| 97 | - }) | ||
| 98 | - } | ||
| 99 | - | ||
| 100 | - pub fn window(&self) -> Window { | ||
| 101 | - self.window | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - pub fn draw( | ||
| 105 | - &mut self, | ||
| 106 | - connection: &RustConnection, | ||
| 107 | - font: &Font, | ||
| 108 | - windows: &[(Window, String)], | ||
| 109 | - focused_window: Option<Window>, | ||
| 110 | - ) -> Result<(), X11Error> { | ||
| 111 | - connection.change_gc( | ||
| 112 | - self.graphics_context, | ||
| 113 | - &ChangeGCAux::new().foreground(self.scheme_normal.background), | ||
| 114 | - )?; | ||
| 115 | - connection.flush()?; | ||
| 116 | - | ||
| 117 | - unsafe { | ||
| 118 | - let gc = | ||
| 119 | - x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut()); | ||
| 120 | - x11::xlib::XSetForeground(self.display, gc, self.scheme_normal.background as u64); | ||
| 121 | - x11::xlib::XFillRectangle( | ||
| 122 | - self.display, | ||
| 123 | - self.surface.pixmap(), | ||
| 124 | - gc, | ||
| 125 | - 0, | ||
| 126 | - 0, | ||
| 127 | - self.width as u32, | ||
| 128 | - self.height as u32, | ||
| 129 | - ); | ||
| 130 | - x11::xlib::XFreeGC(self.display, gc); | ||
| 131 | - } | ||
| 132 | - | ||
| 133 | - if windows.is_empty() { | ||
| 134 | - self.copy_pixmap_to_window(); | ||
| 135 | - return Ok(()); | ||
| 136 | - } | ||
| 137 | - | ||
| 138 | - let tab_width = self.width / windows.len() as u16; | ||
| 139 | - let mut x_position: i16 = 0; | ||
| 140 | - | ||
| 141 | - for (index, &(window, ref title)) in windows.iter().enumerate() { | ||
| 142 | - let is_focused = Some(window) == focused_window; | ||
| 143 | - let scheme = if is_focused { | ||
| 144 | - &self.scheme_selected | ||
| 145 | - } else { | ||
| 146 | - &self.scheme_normal | ||
| 147 | - }; | ||
| 148 | - | ||
| 149 | - let display_title = if title.is_empty() { | ||
| 150 | - format!("Window {}", index + 1) | ||
| 151 | - } else { | ||
| 152 | - title.clone() | ||
| 153 | - }; | ||
| 154 | - | ||
| 155 | - let text_width = font.text_width(&display_title); | ||
| 156 | - let text_x = x_position + ((tab_width.saturating_sub(text_width)) / 2) as i16; | ||
| 157 | - | ||
| 158 | - let top_padding = 6; | ||
| 159 | - let text_y = top_padding + font.ascent(); | ||
| 160 | - | ||
| 161 | - self.surface.font_draw().draw_text( | ||
| 162 | - font, | ||
| 163 | - scheme.foreground, | ||
| 164 | - text_x, | ||
| 165 | - text_y, | ||
| 166 | - &display_title, | ||
| 167 | - ); | ||
| 168 | - | ||
| 169 | - if is_focused { | ||
| 170 | - let underline_height = 3; | ||
| 171 | - let underline_y = self.height as i16 - underline_height; | ||
| 172 | - | ||
| 173 | - unsafe { | ||
| 174 | - let gc = x11::xlib::XCreateGC( | ||
| 175 | - self.display, | ||
| 176 | - self.surface.pixmap(), | ||
| 177 | - 0, | ||
| 178 | - std::ptr::null_mut(), | ||
| 179 | - ); | ||
| 180 | - x11::xlib::XSetForeground(self.display, gc, scheme.underline as u64); | ||
| 181 | - x11::xlib::XFillRectangle( | ||
| 182 | - self.display, | ||
| 183 | - self.surface.pixmap(), | ||
| 184 | - gc, | ||
| 185 | - x_position as i32, | ||
| 186 | - underline_y as i32, | ||
| 187 | - tab_width as u32, | ||
| 188 | - underline_height as u32, | ||
| 189 | - ); | ||
| 190 | - x11::xlib::XFreeGC(self.display, gc); | ||
| 191 | - } | ||
| 192 | - } | ||
| 193 | - | ||
| 194 | - x_position += tab_width as i16; | ||
| 195 | - } | ||
| 196 | - | ||
| 197 | - self.copy_pixmap_to_window(); | ||
| 198 | - Ok(()) | ||
| 199 | - } | ||
| 200 | - | ||
| 201 | - fn copy_pixmap_to_window(&self) { | ||
| 202 | - unsafe { | ||
| 203 | - let gc = | ||
| 204 | - x11::xlib::XCreateGC(self.display, self.window as u64, 0, std::ptr::null_mut()); | ||
| 205 | - x11::xlib::XCopyArea( | ||
| 206 | - self.display, | ||
| 207 | - self.surface.pixmap(), | ||
| 208 | - self.window as u64, | ||
| 209 | - gc, | ||
| 210 | - 0, | ||
| 211 | - 0, | ||
| 212 | - self.width as u32, | ||
| 213 | - self.height as u32, | ||
| 214 | - 0, | ||
| 215 | - 0, | ||
| 216 | - ); | ||
| 217 | - x11::xlib::XFreeGC(self.display, gc); | ||
| 218 | - } | ||
| 219 | - } | ||
| 220 | - | ||
| 221 | - pub fn get_clicked_window(&self, windows: &[(Window, String)], click_x: i16) -> Option<Window> { | ||
| 222 | - if windows.is_empty() { | ||
| 223 | - return None; | ||
| 224 | - } | ||
| 225 | - | ||
| 226 | - let tab_width = self.width / windows.len() as u16; | ||
| 227 | - let tab_index = (click_x as u16 / tab_width) as usize; | ||
| 228 | - | ||
| 229 | - windows.get(tab_index).map(|&(win, _)| win) | ||
| 230 | - } | ||
| 231 | - | ||
| 232 | - pub fn reposition( | ||
| 233 | - &mut self, | ||
| 234 | - connection: &RustConnection, | ||
| 235 | - x: i16, | ||
| 236 | - y: i16, | ||
| 237 | - width: u16, | ||
| 238 | - ) -> Result<(), X11Error> { | ||
| 239 | - self.x_offset = x; | ||
| 240 | - self.y_offset = y; | ||
| 241 | - self.width = width; | ||
| 242 | - | ||
| 243 | - connection.configure_window( | ||
| 244 | - self.window, | ||
| 245 | - &ConfigureWindowAux::new() | ||
| 246 | - .x(x as i32) | ||
| 247 | - .y(y as i32) | ||
| 248 | - .width(width as u32), | ||
| 249 | - )?; | ||
| 250 | - | ||
| 251 | - let visual = unsafe { x11::xlib::XDefaultVisual(self.display, 0) }; | ||
| 252 | - let colormap = unsafe { x11::xlib::XDefaultColormap(self.display, 0) }; | ||
| 253 | - | ||
| 254 | - self.surface = DrawingSurface::new( | ||
| 255 | - self.display, | ||
| 256 | - self.window as x11::xlib::Drawable, | ||
| 257 | - width as u32, | ||
| 258 | - self.height as u32, | ||
| 259 | - visual, | ||
| 260 | - colormap, | ||
| 261 | - )?; | ||
| 262 | - | ||
| 263 | - connection.flush()?; | ||
| 264 | - Ok(()) | ||
| 265 | - } | ||
| 266 | - | ||
| 267 | - pub fn hide(&self, connection: &RustConnection) -> Result<(), X11Error> { | ||
| 268 | - connection.unmap_window(self.window)?; | ||
| 269 | - connection.flush()?; | ||
| 270 | - Ok(()) | ||
| 271 | - } | ||
| 272 | - | ||
| 273 | - pub fn show(&self, connection: &RustConnection) -> Result<(), X11Error> { | ||
| 274 | - connection.map_window(self.window)?; | ||
| 275 | - connection.flush()?; | ||
| 276 | - Ok(()) | ||
| 277 | - } | ||
| 278 | -} | ||
+0 −4793 src/window_manager.rs deleted file mode 100644 Large diff collapsed, click to expand
| @@ -1,4793 +0,0 @@ | |||
| 1 | -use crate::Config; | ||
| 2 | -use crate::animations::{AnimationConfig, ScrollAnimation}; | ||
| 3 | -use crate::bar::Bar; | ||
| 4 | -use crate::client::{Client, TagMask}; | ||
| 5 | -use crate::errors::{ConfigError, WmError}; | ||
| 6 | -use crate::keyboard::{self, Arg, KeyAction, handlers}; | ||
| 7 | -use crate::layout::GapConfig; | ||
| 8 | -use crate::layout::tiling::TilingLayout; | ||
| 9 | -use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}; | ||
| 10 | -use crate::monitor::{Monitor, detect_monitors}; | ||
| 11 | -use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; | ||
| 12 | -use std::collections::{HashMap, HashSet}; | ||
| 13 | - | ||
| 14 | -use x11rb::connection::Connection; | ||
| 15 | -use x11rb::protocol::Event; | ||
| 16 | -use x11rb::protocol::xproto::*; | ||
| 17 | -use x11rb::rust_connection::RustConnection; | ||
| 18 | - | ||
| 19 | -enum Control { | ||
| 20 | - Continue, | ||
| 21 | - Quit, | ||
| 22 | -} | ||
| 23 | - | ||
| 24 | -pub fn tag_mask(tag: usize) -> TagMask { | ||
| 25 | - 1 << tag | ||
| 26 | -} | ||
| 27 | - | ||
| 28 | -pub fn unmask_tag(mask: TagMask) -> usize { | ||
| 29 | - mask.trailing_zeros() as usize | ||
| 30 | -} | ||
| 31 | - | ||
| 32 | -struct AtomCache { | ||
| 33 | - net_supported: Atom, | ||
| 34 | - net_supporting_wm_check: Atom, | ||
| 35 | - net_current_desktop: Atom, | ||
| 36 | - net_client_info: Atom, | ||
| 37 | - wm_state: Atom, | ||
| 38 | - wm_protocols: Atom, | ||
| 39 | - wm_delete_window: Atom, | ||
| 40 | - net_wm_state: Atom, | ||
| 41 | - net_wm_state_fullscreen: Atom, | ||
| 42 | - net_wm_window_type: Atom, | ||
| 43 | - net_wm_window_type_dialog: Atom, | ||
| 44 | - wm_name: Atom, | ||
| 45 | - net_wm_name: Atom, | ||
| 46 | - utf8_string: Atom, | ||
| 47 | - net_active_window: Atom, | ||
| 48 | - wm_take_focus: Atom, | ||
| 49 | - net_client_list: Atom, | ||
| 50 | -} | ||
| 51 | - | ||
| 52 | -impl AtomCache { | ||
| 53 | - fn new(connection: &RustConnection) -> WmResult<Self> { | ||
| 54 | - let net_supported = connection | ||
| 55 | - .intern_atom(false, b"_NET_SUPPORTED")? | ||
| 56 | - .reply()? | ||
| 57 | - .atom; | ||
| 58 | - | ||
| 59 | - let net_supporting_wm_check = connection | ||
| 60 | - .intern_atom(false, b"_NET_SUPPORTING_WM_CHECK")? | ||
| 61 | - .reply()? | ||
| 62 | - .atom; | ||
| 63 | - | ||
| 64 | - let net_current_desktop = connection | ||
| 65 | - .intern_atom(false, b"_NET_CURRENT_DESKTOP")? | ||
| 66 | - .reply()? | ||
| 67 | - .atom; | ||
| 68 | - | ||
| 69 | - let net_client_info = connection | ||
| 70 | - .intern_atom(false, b"_NET_CLIENT_INFO")? | ||
| 71 | - .reply()? | ||
| 72 | - .atom; | ||
| 73 | - | ||
| 74 | - let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; | ||
| 75 | - | ||
| 76 | - let wm_protocols = connection | ||
| 77 | - .intern_atom(false, b"WM_PROTOCOLS")? | ||
| 78 | - .reply()? | ||
| 79 | - .atom; | ||
| 80 | - | ||
| 81 | - let wm_delete_window = connection | ||
| 82 | - .intern_atom(false, b"WM_DELETE_WINDOW")? | ||
| 83 | - .reply()? | ||
| 84 | - .atom; | ||
| 85 | - | ||
| 86 | - let net_wm_state = connection | ||
| 87 | - .intern_atom(false, b"_NET_WM_STATE")? | ||
| 88 | - .reply()? | ||
| 89 | - .atom; | ||
| 90 | - | ||
| 91 | - let net_wm_state_fullscreen = connection | ||
| 92 | - .intern_atom(false, b"_NET_WM_STATE_FULLSCREEN")? | ||
| 93 | - .reply()? | ||
| 94 | - .atom; | ||
| 95 | - | ||
| 96 | - let net_wm_window_type = connection | ||
| 97 | - .intern_atom(false, b"_NET_WM_WINDOW_TYPE")? | ||
| 98 | - .reply()? | ||
| 99 | - .atom; | ||
| 100 | - | ||
| 101 | - let net_wm_window_type_dialog = connection | ||
| 102 | - .intern_atom(false, b"_NET_WM_WINDOW_TYPE_DIALOG")? | ||
| 103 | - .reply()? | ||
| 104 | - .atom; | ||
| 105 | - | ||
| 106 | - let wm_name = AtomEnum::WM_NAME.into(); | ||
| 107 | - let net_wm_name = connection | ||
| 108 | - .intern_atom(false, b"_NET_WM_NAME")? | ||
| 109 | - .reply()? | ||
| 110 | - .atom; | ||
| 111 | - let utf8_string = connection.intern_atom(false, b"UTF8_STRING")?.reply()?.atom; | ||
| 112 | - let net_active_window = connection | ||
| 113 | - .intern_atom(false, b"_NET_ACTIVE_WINDOW")? | ||
| 114 | - .reply()? | ||
| 115 | - .atom; | ||
| 116 | - | ||
| 117 | - let wm_take_focus = connection | ||
| 118 | - .intern_atom(false, b"WM_TAKE_FOCUS")? | ||
| 119 | - .reply()? | ||
| 120 | - .atom; | ||
| 121 | - | ||
| 122 | - let net_client_list = connection | ||
| 123 | - .intern_atom(false, b"_NET_CLIENT_LIST")? | ||
| 124 | - .reply()? | ||
| 125 | - .atom; | ||
| 126 | - | ||
| 127 | - Ok(Self { | ||
| 128 | - net_supported, | ||
| 129 | - net_supporting_wm_check, | ||
| 130 | - net_current_desktop, | ||
| 131 | - net_client_info, | ||
| 132 | - wm_state, | ||
| 133 | - wm_protocols, | ||
| 134 | - wm_delete_window, | ||
| 135 | - net_wm_state, | ||
| 136 | - net_wm_state_fullscreen, | ||
| 137 | - net_wm_window_type, | ||
| 138 | - net_wm_window_type_dialog, | ||
| 139 | - wm_name, | ||
| 140 | - net_wm_name, | ||
| 141 | - utf8_string, | ||
| 142 | - net_active_window, | ||
| 143 | - wm_take_focus, | ||
| 144 | - net_client_list, | ||
| 145 | - }) | ||
| 146 | - } | ||
| 147 | -} | ||
| 148 | - | ||
| 149 | -pub struct WindowManager { | ||
| 150 | - config: Config, | ||
| 151 | - connection: RustConnection, | ||
| 152 | - screen_number: usize, | ||
| 153 | - root: Window, | ||
| 154 | - _wm_check_window: Window, | ||
| 155 | - screen: Screen, | ||
| 156 | - windows: Vec<Window>, | ||
| 157 | - clients: HashMap<Window, Client>, | ||
| 158 | - layout: LayoutBox, | ||
| 159 | - gaps_enabled: bool, | ||
| 160 | - floating_windows: HashSet<Window>, | ||
| 161 | - fullscreen_windows: HashSet<Window>, | ||
| 162 | - bars: Vec<Bar>, | ||
| 163 | - tab_bars: Vec<crate::tab_bar::TabBar>, | ||
| 164 | - show_bar: bool, | ||
| 165 | - monitors: Vec<Monitor>, | ||
| 166 | - selected_monitor: usize, | ||
| 167 | - atoms: AtomCache, | ||
| 168 | - previous_focused: Option<Window>, | ||
| 169 | - display: *mut x11::xlib::Display, | ||
| 170 | - font: crate::bar::font::Font, | ||
| 171 | - keychord_state: keyboard::handlers::KeychordState, | ||
| 172 | - current_key: usize, | ||
| 173 | - keyboard_mapping: Option<keyboard::KeyboardMapping>, | ||
| 174 | - error_message: Option<String>, | ||
| 175 | - overlay: ErrorOverlay, | ||
| 176 | - keybind_overlay: KeybindOverlay, | ||
| 177 | - scroll_animation: ScrollAnimation, | ||
| 178 | - animation_config: AnimationConfig, | ||
| 179 | -} | ||
| 180 | - | ||
| 181 | -type WmResult<T> = Result<T, WmError>; | ||
| 182 | - | ||
| 183 | -impl WindowManager { | ||
| 184 | - pub fn new(config: Config) -> WmResult<Self> { | ||
| 185 | - let (connection, screen_number) = x11rb::connect(None)?; | ||
| 186 | - let root = connection.setup().roots[screen_number].root; | ||
| 187 | - let screen = connection.setup().roots[screen_number].clone(); | ||
| 188 | - | ||
| 189 | - connection | ||
| 190 | - .change_window_attributes( | ||
| 191 | - root, | ||
| 192 | - &ChangeWindowAttributesAux::new().event_mask( | ||
| 193 | - EventMask::SUBSTRUCTURE_REDIRECT | ||
| 194 | - | EventMask::SUBSTRUCTURE_NOTIFY | ||
| 195 | - | EventMask::PROPERTY_CHANGE | ||
| 196 | - | EventMask::KEY_PRESS | ||
| 197 | - | EventMask::BUTTON_PRESS | ||
| 198 | - | EventMask::POINTER_MOTION, | ||
| 199 | - ), | ||
| 200 | - )? | ||
| 201 | - .check()?; | ||
| 202 | - | ||
| 203 | - let ignore_modifiers = [ | ||
| 204 | - 0, | ||
| 205 | - u16::from(ModMask::LOCK), | ||
| 206 | - u16::from(ModMask::M2), | ||
| 207 | - u16::from(ModMask::LOCK | ModMask::M2), | ||
| 208 | - ]; | ||
| 209 | - | ||
| 210 | - for &ignore_mask in &ignore_modifiers { | ||
| 211 | - let grab_mask = u16::from(config.modkey) | ignore_mask; | ||
| 212 | - | ||
| 213 | - connection.grab_button( | ||
| 214 | - false, | ||
| 215 | - root, | ||
| 216 | - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | ||
| 217 | - GrabMode::SYNC, | ||
| 218 | - GrabMode::ASYNC, | ||
| 219 | - x11rb::NONE, | ||
| 220 | - x11rb::NONE, | ||
| 221 | - ButtonIndex::M1, | ||
| 222 | - grab_mask.into(), | ||
| 223 | - )?; | ||
| 224 | - | ||
| 225 | - connection.grab_button( | ||
| 226 | - false, | ||
| 227 | - root, | ||
| 228 | - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | ||
| 229 | - GrabMode::SYNC, | ||
| 230 | - GrabMode::ASYNC, | ||
| 231 | - x11rb::NONE, | ||
| 232 | - x11rb::NONE, | ||
| 233 | - ButtonIndex::M3, | ||
| 234 | - grab_mask.into(), | ||
| 235 | - )?; | ||
| 236 | - } | ||
| 237 | - | ||
| 238 | - let mut monitors = detect_monitors(&connection, &screen, root)?; | ||
| 239 | - for monitor in monitors.iter_mut() { | ||
| 240 | - monitor.init_pertag(config.tags.len(), "tiling"); | ||
| 241 | - } | ||
| 242 | - | ||
| 243 | - let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; | ||
| 244 | - if display.is_null() { | ||
| 245 | - return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); | ||
| 246 | - } | ||
| 247 | - | ||
| 248 | - // C has better C interop than rust. | ||
| 249 | - let normal_cursor = unsafe { x11::xlib::XCreateFontCursor(display, 68) }; | ||
| 250 | - | ||
| 251 | - unsafe { | ||
| 252 | - x11::xlib::XDefineCursor(display, root as u64, normal_cursor); | ||
| 253 | - } | ||
| 254 | - | ||
| 255 | - let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; | ||
| 256 | - | ||
| 257 | - let mut bars = Vec::new(); | ||
| 258 | - for monitor in monitors.iter() { | ||
| 259 | - let bar = Bar::new( | ||
| 260 | - &connection, | ||
| 261 | - &screen, | ||
| 262 | - screen_number, | ||
| 263 | - &config, | ||
| 264 | - display, | ||
| 265 | - &font, | ||
| 266 | - monitor.screen_x as i16, | ||
| 267 | - monitor.screen_y as i16, | ||
| 268 | - monitor.screen_width as u16, | ||
| 269 | - normal_cursor as u32, | ||
| 270 | - )?; | ||
| 271 | - bars.push(bar); | ||
| 272 | - } | ||
| 273 | - | ||
| 274 | - let bar_height = font.height() as f32 * 1.4; | ||
| 275 | - let mut tab_bars = Vec::new(); | ||
| 276 | - for monitor in monitors.iter() { | ||
| 277 | - let tab_bar = crate::tab_bar::TabBar::new( | ||
| 278 | - &connection, | ||
| 279 | - &screen, | ||
| 280 | - screen_number, | ||
| 281 | - display, | ||
| 282 | - &font, | ||
| 283 | - (monitor.screen_x + config.gap_outer_horizontal as i32) as i16, | ||
| 284 | - (monitor.screen_y as f32 + bar_height + config.gap_outer_vertical as f32) as i16, | ||
| 285 | - monitor | ||
| 286 | - .screen_width | ||
| 287 | - .saturating_sub(2 * config.gap_outer_horizontal as i32) as u16, | ||
| 288 | - config.scheme_occupied, | ||
| 289 | - config.scheme_selected, | ||
| 290 | - normal_cursor as u32, | ||
| 291 | - )?; | ||
| 292 | - tab_bars.push(tab_bar); | ||
| 293 | - } | ||
| 294 | - | ||
| 295 | - let gaps_enabled = config.gaps_enabled; | ||
| 296 | - | ||
| 297 | - let atoms = AtomCache::new(&connection)?; | ||
| 298 | - | ||
| 299 | - let supported_atoms: Vec<Atom> = vec![ | ||
| 300 | - atoms.net_supported, | ||
| 301 | - atoms.net_supporting_wm_check, | ||
| 302 | - atoms.net_wm_state, | ||
| 303 | - atoms.net_wm_state_fullscreen, | ||
| 304 | - atoms.net_wm_window_type, | ||
| 305 | - atoms.net_wm_window_type_dialog, | ||
| 306 | - atoms.net_active_window, | ||
| 307 | - atoms.net_wm_name, | ||
| 308 | - atoms.net_current_desktop, | ||
| 309 | - atoms.net_client_info, | ||
| 310 | - atoms.net_client_list, | ||
| 311 | - ]; | ||
| 312 | - let supported_bytes: Vec<u8> = supported_atoms | ||
| 313 | - .iter() | ||
| 314 | - .flat_map(|a| a.to_ne_bytes()) | ||
| 315 | - .collect(); | ||
| 316 | - connection.change_property( | ||
| 317 | - PropMode::REPLACE, | ||
| 318 | - root, | ||
| 319 | - atoms.net_supported, | ||
| 320 | - AtomEnum::ATOM, | ||
| 321 | - 32, | ||
| 322 | - supported_atoms.len() as u32, | ||
| 323 | - &supported_bytes, | ||
| 324 | - )?; | ||
| 325 | - | ||
| 326 | - let wm_check_window = connection.generate_id()?; | ||
| 327 | - connection.create_window( | ||
| 328 | - screen.root_depth, | ||
| 329 | - wm_check_window, | ||
| 330 | - root, | ||
| 331 | - 0, | ||
| 332 | - 0, | ||
| 333 | - 1, | ||
| 334 | - 1, | ||
| 335 | - 0, | ||
| 336 | - WindowClass::INPUT_OUTPUT, | ||
| 337 | - 0, | ||
| 338 | - &CreateWindowAux::new(), | ||
| 339 | - )?; | ||
| 340 | - | ||
| 341 | - connection.change_property( | ||
| 342 | - PropMode::REPLACE, | ||
| 343 | - wm_check_window, | ||
| 344 | - atoms.net_supporting_wm_check, | ||
| 345 | - AtomEnum::WINDOW, | ||
| 346 | - 32, | ||
| 347 | - 1, | ||
| 348 | - &wm_check_window.to_ne_bytes(), | ||
| 349 | - )?; | ||
| 350 | - | ||
| 351 | - connection.change_property( | ||
| 352 | - PropMode::REPLACE, | ||
| 353 | - wm_check_window, | ||
| 354 | - atoms.net_wm_name, | ||
| 355 | - atoms.utf8_string, | ||
| 356 | - 8, | ||
| 357 | - 4, | ||
| 358 | - b"oxwm", | ||
| 359 | - )?; | ||
| 360 | - | ||
| 361 | - connection.change_property( | ||
| 362 | - PropMode::REPLACE, | ||
| 363 | - root, | ||
| 364 | - atoms.net_supporting_wm_check, | ||
| 365 | - AtomEnum::WINDOW, | ||
| 366 | - 32, | ||
| 367 | - 1, | ||
| 368 | - &wm_check_window.to_ne_bytes(), | ||
| 369 | - )?; | ||
| 370 | - | ||
| 371 | - let overlay = ErrorOverlay::new( | ||
| 372 | - &connection, | ||
| 373 | - &screen, | ||
| 374 | - screen_number, | ||
| 375 | - display, | ||
| 376 | - &font, | ||
| 377 | - screen.width_in_pixels, | ||
| 378 | - )?; | ||
| 379 | - | ||
| 380 | - let keybind_overlay = | ||
| 381 | - KeybindOverlay::new(&connection, &screen, screen_number, display, config.modkey)?; | ||
| 382 | - | ||
| 383 | - let mut window_manager = Self { | ||
| 384 | - config, | ||
| 385 | - connection, | ||
| 386 | - screen_number, | ||
| 387 | - root, | ||
| 388 | - _wm_check_window: wm_check_window, | ||
| 389 | - screen, | ||
| 390 | - windows: Vec::new(), | ||
| 391 | - clients: HashMap::new(), | ||
| 392 | - layout: Box::new(TilingLayout), | ||
| 393 | - gaps_enabled, | ||
| 394 | - floating_windows: HashSet::new(), | ||
| 395 | - fullscreen_windows: HashSet::new(), | ||
| 396 | - bars, | ||
| 397 | - tab_bars, | ||
| 398 | - show_bar: true, | ||
| 399 | - monitors, | ||
| 400 | - selected_monitor: 0, | ||
| 401 | - atoms, | ||
| 402 | - previous_focused: None, | ||
| 403 | - display, | ||
| 404 | - font, | ||
| 405 | - keychord_state: keyboard::handlers::KeychordState::Idle, | ||
| 406 | - current_key: 0, | ||
| 407 | - keyboard_mapping: None, | ||
| 408 | - error_message: None, | ||
| 409 | - overlay, | ||
| 410 | - keybind_overlay, | ||
| 411 | - scroll_animation: ScrollAnimation::new(), | ||
| 412 | - animation_config: AnimationConfig::default(), | ||
| 413 | - }; | ||
| 414 | - | ||
| 415 | - for tab_bar in &window_manager.tab_bars { | ||
| 416 | - tab_bar.hide(&window_manager.connection)?; | ||
| 417 | - } | ||
| 418 | - | ||
| 419 | - window_manager.scan_existing_windows()?; | ||
| 420 | - window_manager.update_bar()?; | ||
| 421 | - window_manager.run_autostart_commands(); | ||
| 422 | - | ||
| 423 | - Ok(window_manager) | ||
| 424 | - } | ||
| 425 | - | ||
| 426 | - pub fn show_startup_config_error(&mut self, error: ConfigError) { | ||
| 427 | - let monitor = &self.monitors[self.selected_monitor]; | ||
| 428 | - let monitor_x = monitor.screen_x as i16; | ||
| 429 | - let monitor_y = monitor.screen_y as i16; | ||
| 430 | - let screen_width = monitor.screen_width as u16; | ||
| 431 | - let screen_height = monitor.screen_height as u16; | ||
| 432 | - | ||
| 433 | - if let Err(e) = self.overlay.show_error( | ||
| 434 | - &self.connection, | ||
| 435 | - &self.font, | ||
| 436 | - error, | ||
| 437 | - monitor_x, | ||
| 438 | - monitor_y, | ||
| 439 | - screen_width, | ||
| 440 | - screen_height, | ||
| 441 | - ) { | ||
| 442 | - eprintln!("Failed to show config error overlay: {:?}", e); | ||
| 443 | - } | ||
| 444 | - } | ||
| 445 | - | ||
| 446 | - fn try_reload_config(&mut self) -> Result<(), ConfigError> { | ||
| 447 | - let lua_path = self | ||
| 448 | - .config | ||
| 449 | - .path | ||
| 450 | - .as_ref() | ||
| 451 | - .ok_or(ConfigError::NoConfigPathSet)?; | ||
| 452 | - | ||
| 453 | - if !lua_path.exists() { | ||
| 454 | - return Err(ConfigError::NoConfigAtPath); | ||
| 455 | - } | ||
| 456 | - | ||
| 457 | - let config_str = | ||
| 458 | - std::fs::read_to_string(lua_path).map_err(|e| ConfigError::CouldNotReadConfig(e))?; | ||
| 459 | - | ||
| 460 | - let config_dir = lua_path.parent(); | ||
| 461 | - | ||
| 462 | - let new_config = crate::config::parse_lua_config(&config_str, config_dir)?; | ||
| 463 | - | ||
| 464 | - let lua_path = self.config.path.take(); | ||
| 465 | - | ||
| 466 | - self.config = new_config; | ||
| 467 | - self.config.path = lua_path; | ||
| 468 | - self.error_message = None; | ||
| 469 | - | ||
| 470 | - for bar in &mut self.bars { | ||
| 471 | - bar.update_from_config(&self.config); | ||
| 472 | - } | ||
| 473 | - | ||
| 474 | - Ok(()) | ||
| 475 | - } | ||
| 476 | - | ||
| 477 | - fn scan_existing_windows(&mut self) -> WmResult<()> { | ||
| 478 | - let tree = self.connection.query_tree(self.root)?.reply()?; | ||
| 479 | - let net_client_info = self.atoms.net_client_info; | ||
| 480 | - let wm_state_atom = self.atoms.wm_state; | ||
| 481 | - | ||
| 482 | - for &window in &tree.children { | ||
| 483 | - if self.bars.iter().any(|bar| bar.window() == window) { | ||
| 484 | - continue; | ||
| 485 | - } | ||
| 486 | - | ||
| 487 | - let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { | ||
| 488 | - continue; | ||
| 489 | - }; | ||
| 490 | - | ||
| 491 | - if attrs.override_redirect { | ||
| 492 | - continue; | ||
| 493 | - } | ||
| 494 | - | ||
| 495 | - if attrs.map_state == MapState::VIEWABLE { | ||
| 496 | - let _tag = self.get_saved_tag(window, net_client_info)?; | ||
| 497 | - self.windows.push(window); | ||
| 498 | - continue; | ||
| 499 | - } | ||
| 500 | - | ||
| 501 | - if attrs.map_state == MapState::UNMAPPED { | ||
| 502 | - let has_wm_state = self | ||
| 503 | - .connection | ||
| 504 | - .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? | ||
| 505 | - .reply() | ||
| 506 | - .is_ok_and(|prop| !prop.value.is_empty()); | ||
| 507 | - | ||
| 508 | - if !has_wm_state { | ||
| 509 | - continue; | ||
| 510 | - } | ||
| 511 | - | ||
| 512 | - let has_wm_class = self | ||
| 513 | - .connection | ||
| 514 | - .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? | ||
| 515 | - .reply() | ||
| 516 | - .is_ok_and(|prop| !prop.value.is_empty()); | ||
| 517 | - | ||
| 518 | - if has_wm_class { | ||
| 519 | - let _tag = self.get_saved_tag(window, net_client_info)?; | ||
| 520 | - self.connection.map_window(window)?; | ||
| 521 | - self.windows.push(window); | ||
| 522 | - } | ||
| 523 | - } | ||
| 524 | - } | ||
| 525 | - | ||
| 526 | - if let Some(&first) = self.windows.first() { | ||
| 527 | - self.focus(Some(first))?; | ||
| 528 | - } | ||
| 529 | - | ||
| 530 | - self.apply_layout()?; | ||
| 531 | - Ok(()) | ||
| 532 | - } | ||
| 533 | - | ||
| 534 | - fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { | ||
| 535 | - match self | ||
| 536 | - .connection | ||
| 537 | - .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? | ||
| 538 | - .reply() | ||
| 539 | - { | ||
| 540 | - Ok(prop) if prop.value.len() >= 4 => { | ||
| 541 | - let tags = u32::from_ne_bytes([ | ||
| 542 | - prop.value[0], | ||
| 543 | - prop.value[1], | ||
| 544 | - prop.value[2], | ||
| 545 | - prop.value[3], | ||
| 546 | - ]); | ||
| 547 | - | ||
| 548 | - if tags != 0 && tags < (1 << self.config.tags.len()) { | ||
| 549 | - return Ok(tags); | ||
| 550 | - } | ||
| 551 | - } | ||
| 552 | - Ok(_) => {} | ||
| 553 | - Err(e) => { | ||
| 554 | - eprintln!("No _NET_CLIENT_INFO property ({})", e); | ||
| 555 | - } | ||
| 556 | - } | ||
| 557 | - | ||
| 558 | - Ok(self | ||
| 559 | - .monitors | ||
| 560 | - .get(self.selected_monitor) | ||
| 561 | - .map(|m| m.tagset[m.selected_tags_index]) | ||
| 562 | - .unwrap_or(tag_mask(0))) | ||
| 563 | - } | ||
| 564 | - | ||
| 565 | - fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { | ||
| 566 | - let net_client_info = self.atoms.net_client_info; | ||
| 567 | - | ||
| 568 | - let bytes = tag.to_ne_bytes().to_vec(); | ||
| 569 | - | ||
| 570 | - self.connection.change_property( | ||
| 571 | - PropMode::REPLACE, | ||
| 572 | - window, | ||
| 573 | - net_client_info, | ||
| 574 | - AtomEnum::CARDINAL, | ||
| 575 | - 32, | ||
| 576 | - 1, | ||
| 577 | - &bytes, | ||
| 578 | - )?; | ||
| 579 | - | ||
| 580 | - self.connection.flush()?; | ||
| 581 | - Ok(()) | ||
| 582 | - } | ||
| 583 | - | ||
| 584 | - fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { | ||
| 585 | - let wm_state_atom = self.atoms.wm_state; | ||
| 586 | - | ||
| 587 | - let data = [state, 0u32]; | ||
| 588 | - let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); | ||
| 589 | - | ||
| 590 | - self.connection.change_property( | ||
| 591 | - PropMode::REPLACE, | ||
| 592 | - window, | ||
| 593 | - wm_state_atom, | ||
| 594 | - wm_state_atom, | ||
| 595 | - 32, | ||
| 596 | - 2, | ||
| 597 | - &bytes, | ||
| 598 | - )?; | ||
| 599 | - | ||
| 600 | - self.connection.flush()?; | ||
| 601 | - Ok(()) | ||
| 602 | - } | ||
| 603 | - | ||
| 604 | - fn update_client_list(&self) -> WmResult<()> { | ||
| 605 | - let window_bytes: Vec<u8> = self | ||
| 606 | - .windows | ||
| 607 | - .iter() | ||
| 608 | - .flat_map(|window| window.to_ne_bytes()) | ||
| 609 | - .collect(); | ||
| 610 | - | ||
| 611 | - self.connection.change_property( | ||
| 612 | - PropMode::REPLACE, | ||
| 613 | - self.root, | ||
| 614 | - self.atoms.net_client_list, | ||
| 615 | - AtomEnum::WINDOW, | ||
| 616 | - 32, | ||
| 617 | - self.windows.len() as u32, | ||
| 618 | - &window_bytes, | ||
| 619 | - )?; | ||
| 620 | - | ||
| 621 | - Ok(()) | ||
| 622 | - } | ||
| 623 | - | ||
| 624 | - pub fn run(&mut self) -> WmResult<()> { | ||
| 625 | - println!("oxwm started on display {}", self.screen_number); | ||
| 626 | - | ||
| 627 | - self.grab_keys()?; | ||
| 628 | - self.update_bar()?; | ||
| 629 | - | ||
| 630 | - let mut last_bar_update = std::time::Instant::now(); | ||
| 631 | - const BAR_UPDATE_INTERVAL_MS: u64 = 100; | ||
| 632 | - | ||
| 633 | - loop { | ||
| 634 | - match self.connection.poll_for_event_with_sequence()? { | ||
| 635 | - Some((event, _sequence)) => { | ||
| 636 | - if matches!(self.handle_event(event)?, Control::Quit) { | ||
| 637 | - return Ok(()); | ||
| 638 | - } | ||
| 639 | - } | ||
| 640 | - None => { | ||
| 641 | - if last_bar_update.elapsed().as_millis() >= BAR_UPDATE_INTERVAL_MS as u128 { | ||
| 642 | - if let Some(bar) = self.bars.get_mut(self.selected_monitor) { | ||
| 643 | - bar.update_blocks(); | ||
| 644 | - } | ||
| 645 | - if self.bars.iter().any(|bar| bar.needs_redraw()) { | ||
| 646 | - self.update_bar()?; | ||
| 647 | - } | ||
| 648 | - last_bar_update = std::time::Instant::now(); | ||
| 649 | - } | ||
| 650 | - | ||
| 651 | - self.tick_animations()?; | ||
| 652 | - | ||
| 653 | - self.connection.flush()?; | ||
| 654 | - std::thread::sleep(std::time::Duration::from_millis(16)); | ||
| 655 | - } | ||
| 656 | - } | ||
| 657 | - } | ||
| 658 | - } | ||
| 659 | - | ||
| 660 | - fn toggle_floating(&mut self) -> WmResult<()> { | ||
| 661 | - let focused = self | ||
| 662 | - .monitors | ||
| 663 | - .get(self.selected_monitor) | ||
| 664 | - .and_then(|m| m.selected_client); | ||
| 665 | - | ||
| 666 | - if focused.is_none() { | ||
| 667 | - return Ok(()); | ||
| 668 | - } | ||
| 669 | - let focused = focused.unwrap(); | ||
| 670 | - | ||
| 671 | - if let Some(client) = self.clients.get(&focused) | ||
| 672 | - && client.is_fullscreen | ||
| 673 | - { | ||
| 674 | - return Ok(()); | ||
| 675 | - } | ||
| 676 | - | ||
| 677 | - let (is_fixed, x, y, w, h) = if let Some(client) = self.clients.get(&focused) { | ||
| 678 | - ( | ||
| 679 | - client.is_fixed, | ||
| 680 | - client.x_position as i32, | ||
| 681 | - client.y_position as i32, | ||
| 682 | - client.width as u32, | ||
| 683 | - client.height as u32, | ||
| 684 | - ) | ||
| 685 | - } else { | ||
| 686 | - return Ok(()); | ||
| 687 | - }; | ||
| 688 | - | ||
| 689 | - let was_floating = self.floating_windows.contains(&focused); | ||
| 690 | - | ||
| 691 | - if was_floating { | ||
| 692 | - self.floating_windows.remove(&focused); | ||
| 693 | - if let Some(client) = self.clients.get_mut(&focused) { | ||
| 694 | - client.is_floating = false; | ||
| 695 | - } | ||
| 696 | - } else { | ||
| 697 | - self.floating_windows.insert(focused); | ||
| 698 | - if let Some(client) = self.clients.get_mut(&focused) { | ||
| 699 | - client.is_floating = is_fixed || !client.is_floating; | ||
| 700 | - } | ||
| 701 | - | ||
| 702 | - self.connection.configure_window( | ||
| 703 | - focused, | ||
| 704 | - &ConfigureWindowAux::new() | ||
| 705 | - .x(x) | ||
| 706 | - .y(y) | ||
| 707 | - .width(w) | ||
| 708 | - .height(h) | ||
| 709 | - .stack_mode(StackMode::ABOVE), | ||
| 710 | - )?; | ||
| 711 | - } | ||
| 712 | - | ||
| 713 | - self.apply_layout()?; | ||
| 714 | - Ok(()) | ||
| 715 | - } | ||
| 716 | - | ||
| 717 | - fn set_master_factor(&mut self, delta: f32) -> WmResult<()> { | ||
| 718 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 719 | - let new_mfact = (monitor.master_factor + delta).clamp(0.05, 0.95); | ||
| 720 | - monitor.master_factor = new_mfact; | ||
| 721 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 722 | - pertag.master_factors[pertag.current_tag] = new_mfact; | ||
| 723 | - } | ||
| 724 | - self.apply_layout()?; | ||
| 725 | - } | ||
| 726 | - Ok(()) | ||
| 727 | - } | ||
| 728 | - | ||
| 729 | - fn inc_num_master(&mut self, delta: i32) -> WmResult<()> { | ||
| 730 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 731 | - let new_nmaster = (monitor.num_master + delta).max(0); | ||
| 732 | - monitor.num_master = new_nmaster; | ||
| 733 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 734 | - pertag.num_masters[pertag.current_tag] = new_nmaster; | ||
| 735 | - } | ||
| 736 | - self.apply_layout()?; | ||
| 737 | - } | ||
| 738 | - Ok(()) | ||
| 739 | - } | ||
| 740 | - | ||
| 741 | - fn tick_animations(&mut self) -> WmResult<()> { | ||
| 742 | - if self.scroll_animation.is_active() { | ||
| 743 | - if let Some(new_offset) = self.scroll_animation.update() { | ||
| 744 | - if let Some(m) = self.monitors.get_mut(self.selected_monitor) { | ||
| 745 | - m.scroll_offset = new_offset; | ||
| 746 | - } | ||
| 747 | - self.apply_layout()?; | ||
| 748 | - self.update_bar()?; | ||
| 749 | - } | ||
| 750 | - } | ||
| 751 | - Ok(()) | ||
| 752 | - } | ||
| 753 | - | ||
| 754 | - fn scroll_layout(&mut self, direction: i32) -> WmResult<()> { | ||
| 755 | - if self.layout.name() != "scrolling" { | ||
| 756 | - return Ok(()); | ||
| 757 | - } | ||
| 758 | - | ||
| 759 | - let monitor_index = self.selected_monitor; | ||
| 760 | - let monitor = match self.monitors.get(monitor_index) { | ||
| 761 | - Some(m) => m.clone(), | ||
| 762 | - None => return Ok(()), | ||
| 763 | - }; | ||
| 764 | - | ||
| 765 | - let visible_count = if monitor.num_master > 0 { | ||
| 766 | - monitor.num_master as usize | ||
| 767 | - } else { | ||
| 768 | - 2 | ||
| 769 | - }; | ||
| 770 | - | ||
| 771 | - let mut tiled_count = 0; | ||
| 772 | - let mut current = self.next_tiled(monitor.clients_head, &monitor); | ||
| 773 | - while let Some(window) = current { | ||
| 774 | - tiled_count += 1; | ||
| 775 | - if let Some(client) = self.clients.get(&window) { | ||
| 776 | - current = self.next_tiled(client.next, &monitor); | ||
| 777 | - } else { | ||
| 778 | - break; | ||
| 779 | - } | ||
| 780 | - } | ||
| 781 | - | ||
| 782 | - if tiled_count <= visible_count { | ||
| 783 | - if let Some(m) = self.monitors.get_mut(monitor_index) { | ||
| 784 | - m.scroll_offset = 0; | ||
| 785 | - } | ||
| 786 | - return Ok(()); | ||
| 787 | - } | ||
| 788 | - | ||
| 789 | - let outer_gap = if self.gaps_enabled { | ||
| 790 | - self.config.gap_outer_vertical | ||
| 791 | - } else { | ||
| 792 | - 0 | ||
| 793 | - }; | ||
| 794 | - let inner_gap = if self.gaps_enabled { | ||
| 795 | - self.config.gap_inner_vertical | ||
| 796 | - } else { | ||
| 797 | - 0 | ||
| 798 | - }; | ||
| 799 | - | ||
| 800 | - let available_width = monitor.screen_width - 2 * outer_gap as i32; | ||
| 801 | - let total_inner_gaps = inner_gap as i32 * (visible_count - 1) as i32; | ||
| 802 | - let window_width = (available_width - total_inner_gaps) / visible_count as i32; | ||
| 803 | - let scroll_amount = window_width + inner_gap as i32; | ||
| 804 | - | ||
| 805 | - let total_width = | ||
| 806 | - tiled_count as i32 * window_width + (tiled_count - 1) as i32 * inner_gap as i32; | ||
| 807 | - let max_scroll = (total_width - available_width).max(0); | ||
| 808 | - | ||
| 809 | - let current_offset = monitor.scroll_offset; | ||
| 810 | - let target_offset = if self.scroll_animation.is_active() { | ||
| 811 | - self.scroll_animation.target() + direction * scroll_amount | ||
| 812 | - } else { | ||
| 813 | - current_offset + direction * scroll_amount | ||
| 814 | - }; | ||
| 815 | - let target_offset = target_offset.clamp(0, max_scroll); | ||
| 816 | - | ||
| 817 | - self.scroll_animation | ||
| 818 | - .start(current_offset, target_offset, &self.animation_config); | ||
| 819 | - | ||
| 820 | - Ok(()) | ||
| 821 | - } | ||
| 822 | - | ||
| 823 | - fn scroll_to_window(&mut self, target_window: Window, animate: bool) -> WmResult<()> { | ||
| 824 | - if self.layout.name() != "scrolling" { | ||
| 825 | - return Ok(()); | ||
| 826 | - } | ||
| 827 | - | ||
| 828 | - let monitor_index = self.selected_monitor; | ||
| 829 | - let monitor = match self.monitors.get(monitor_index) { | ||
| 830 | - Some(m) => m.clone(), | ||
| 831 | - None => return Ok(()), | ||
| 832 | - }; | ||
| 833 | - | ||
| 834 | - let visible_count = if monitor.num_master > 0 { | ||
| 835 | - monitor.num_master as usize | ||
| 836 | - } else { | ||
| 837 | - 2 | ||
| 838 | - }; | ||
| 839 | - | ||
| 840 | - let outer_gap = if self.gaps_enabled { | ||
| 841 | - self.config.gap_outer_vertical | ||
| 842 | - } else { | ||
| 843 | - 0 | ||
| 844 | - }; | ||
| 845 | - let inner_gap = if self.gaps_enabled { | ||
| 846 | - self.config.gap_inner_vertical | ||
| 847 | - } else { | ||
| 848 | - 0 | ||
| 849 | - }; | ||
| 850 | - | ||
| 851 | - let mut tiled_windows = Vec::new(); | ||
| 852 | - let mut current = self.next_tiled(monitor.clients_head, &monitor); | ||
| 853 | - while let Some(window) = current { | ||
| 854 | - tiled_windows.push(window); | ||
| 855 | - if let Some(client) = self.clients.get(&window) { | ||
| 856 | - current = self.next_tiled(client.next, &monitor); | ||
| 857 | - } else { | ||
| 858 | - break; | ||
| 859 | - } | ||
| 860 | - } | ||
| 861 | - | ||
| 862 | - let target_idx = tiled_windows.iter().position(|&w| w == target_window); | ||
| 863 | - let target_idx = match target_idx { | ||
| 864 | - Some(idx) => idx, | ||
| 865 | - None => return Ok(()), | ||
| 866 | - }; | ||
| 867 | - | ||
| 868 | - let tiled_count = tiled_windows.len(); | ||
| 869 | - if tiled_count <= visible_count { | ||
| 870 | - if animate && monitor.scroll_offset != 0 { | ||
| 871 | - self.scroll_animation | ||
| 872 | - .start(monitor.scroll_offset, 0, &self.animation_config); | ||
| 873 | - } else if let Some(m) = self.monitors.get_mut(monitor_index) { | ||
| 874 | - m.scroll_offset = 0; | ||
| 875 | - } | ||
| 876 | - return Ok(()); | ||
| 877 | - } | ||
| 878 | - | ||
| 879 | - let available_width = monitor.screen_width - 2 * outer_gap as i32; | ||
| 880 | - let total_inner_gaps = inner_gap as i32 * (visible_count - 1) as i32; | ||
| 881 | - let window_width = (available_width - total_inner_gaps) / visible_count as i32; | ||
| 882 | - let scroll_step = window_width + inner_gap as i32; | ||
| 883 | - | ||
| 884 | - let total_width = | ||
| 885 | - tiled_count as i32 * window_width + (tiled_count - 1) as i32 * inner_gap as i32; | ||
| 886 | - let max_scroll = (total_width - available_width).max(0); | ||
| 887 | - | ||
| 888 | - let target_scroll = (target_idx as i32) * scroll_step; | ||
| 889 | - let new_offset = target_scroll.clamp(0, max_scroll); | ||
| 890 | - | ||
| 891 | - let current_offset = monitor.scroll_offset; | ||
| 892 | - if current_offset != new_offset { | ||
| 893 | - if animate { | ||
| 894 | - self.scroll_animation | ||
| 895 | - .start(current_offset, new_offset, &self.animation_config); | ||
| 896 | - } else { | ||
| 897 | - if let Some(m) = self.monitors.get_mut(monitor_index) { | ||
| 898 | - m.scroll_offset = new_offset; | ||
| 899 | - } | ||
| 900 | - } | ||
| 901 | - } | ||
| 902 | - | ||
| 903 | - Ok(()) | ||
| 904 | - } | ||
| 905 | - | ||
| 906 | - fn toggle_bar(&mut self) -> WmResult<()> { | ||
| 907 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 908 | - monitor.show_bar = !monitor.show_bar; | ||
| 909 | - self.show_bar = monitor.show_bar; | ||
| 910 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 911 | - pertag.show_bars[pertag.current_tag] = monitor.show_bar; | ||
| 912 | - } | ||
| 913 | - } | ||
| 914 | - self.apply_layout()?; | ||
| 915 | - self.update_bar()?; | ||
| 916 | - Ok(()) | ||
| 917 | - } | ||
| 918 | - | ||
| 919 | - fn get_layout_symbol(&self) -> String { | ||
| 920 | - let layout_name = self.layout.name(); | ||
| 921 | - | ||
| 922 | - if layout_name == "scrolling" { | ||
| 923 | - if let Some(monitor) = self.monitors.get(self.selected_monitor) { | ||
| 924 | - let visible_count = if monitor.num_master > 0 { | ||
| 925 | - monitor.num_master as usize | ||
| 926 | - } else { | ||
| 927 | - 2 | ||
| 928 | - }; | ||
| 929 | - | ||
| 930 | - let mut tiled_count = 0; | ||
| 931 | - let mut current = self.next_tiled(monitor.clients_head, monitor); | ||
| 932 | - while let Some(window) = current { | ||
| 933 | - tiled_count += 1; | ||
| 934 | - if let Some(client) = self.clients.get(&window) { | ||
| 935 | - current = self.next_tiled(client.next, monitor); | ||
| 936 | - } else { | ||
| 937 | - break; | ||
| 938 | - } | ||
| 939 | - } | ||
| 940 | - | ||
| 941 | - if tiled_count > 0 { | ||
| 942 | - let outer_gap = if self.gaps_enabled { | ||
| 943 | - self.config.gap_outer_vertical | ||
| 944 | - } else { | ||
| 945 | - 0 | ||
| 946 | - }; | ||
| 947 | - let inner_gap = if self.gaps_enabled { | ||
| 948 | - self.config.gap_inner_vertical | ||
| 949 | - } else { | ||
| 950 | - 0 | ||
| 951 | - }; | ||
| 952 | - | ||
| 953 | - let available_width = monitor.screen_width - 2 * outer_gap as i32; | ||
| 954 | - let total_inner_gaps = | ||
| 955 | - inner_gap as i32 * (visible_count.min(tiled_count) - 1) as i32; | ||
| 956 | - let window_width = if tiled_count <= visible_count { | ||
| 957 | - (available_width - total_inner_gaps) / tiled_count as i32 | ||
| 958 | - } else { | ||
| 959 | - (available_width - inner_gap as i32 * (visible_count - 1) as i32) | ||
| 960 | - / visible_count as i32 | ||
| 961 | - }; | ||
| 962 | - | ||
| 963 | - let scroll_step = window_width + inner_gap as i32; | ||
| 964 | - let first_visible = if scroll_step > 0 { | ||
| 965 | - (monitor.scroll_offset / scroll_step) + 1 | ||
| 966 | - } else { | ||
| 967 | - 1 | ||
| 968 | - }; | ||
| 969 | - let last_visible = | ||
| 970 | - (first_visible + visible_count as i32 - 1).min(tiled_count as i32); | ||
| 971 | - | ||
| 972 | - return format!("[{}-{}/{}]", first_visible, last_visible, tiled_count); | ||
| 973 | - } | ||
| 974 | - } | ||
| 975 | - } | ||
| 976 | - | ||
| 977 | - self.config | ||
| 978 | - .layout_symbols | ||
| 979 | - .iter() | ||
| 980 | - .find(|l| l.name == layout_name) | ||
| 981 | - .map(|l| l.symbol.clone()) | ||
| 982 | - .unwrap_or_else(|| self.layout.symbol().to_string()) | ||
| 983 | - } | ||
| 984 | - | ||
| 985 | - fn get_keychord_indicator(&self) -> Option<String> { | ||
| 986 | - match &self.keychord_state { | ||
| 987 | - keyboard::handlers::KeychordState::Idle => None, | ||
| 988 | - keyboard::handlers::KeychordState::InProgress { | ||
| 989 | - candidates, | ||
| 990 | - keys_pressed, | ||
| 991 | - } => { | ||
| 992 | - if candidates.is_empty() { | ||
| 993 | - return None; | ||
| 994 | - } | ||
| 995 | - | ||
| 996 | - let binding = &self.config.keybindings[candidates[0]]; | ||
| 997 | - let mut indicator = String::new(); | ||
| 998 | - | ||
| 999 | - for (i, key_press) in binding.keys.iter().take(*keys_pressed).enumerate() { | ||
| 1000 | - if i > 0 { | ||
| 1001 | - indicator.push(' '); | ||
| 1002 | - } | ||
| 1003 | - | ||
| 1004 | - for modifier in &key_press.modifiers { | ||
| 1005 | - indicator.push_str(Self::format_modifier(*modifier)); | ||
| 1006 | - indicator.push('+'); | ||
| 1007 | - } | ||
| 1008 | - | ||
| 1009 | - indicator.push_str(&keyboard::keysyms::format_keysym(key_press.keysym)); | ||
| 1010 | - } | ||
| 1011 | - | ||
| 1012 | - indicator.push('-'); | ||
| 1013 | - Some(indicator) | ||
| 1014 | - } | ||
| 1015 | - } | ||
| 1016 | - } | ||
| 1017 | - | ||
| 1018 | - fn format_modifier(modifier: KeyButMask) -> &'static str { | ||
| 1019 | - match modifier { | ||
| 1020 | - KeyButMask::MOD1 => "Alt", | ||
| 1021 | - KeyButMask::MOD4 => "Super", | ||
| 1022 | - KeyButMask::SHIFT => "Shift", | ||
| 1023 | - KeyButMask::CONTROL => "Ctrl", | ||
| 1024 | - _ => "Mod", | ||
| 1025 | - } | ||
| 1026 | - } | ||
| 1027 | - | ||
| 1028 | - fn update_bar(&mut self) -> WmResult<()> { | ||
| 1029 | - let layout_symbol = self.get_layout_symbol(); | ||
| 1030 | - let keychord_indicator = self.get_keychord_indicator(); | ||
| 1031 | - | ||
| 1032 | - for (monitor_index, monitor) in self.monitors.iter().enumerate() { | ||
| 1033 | - if let Some(bar) = self.bars.get_mut(monitor_index) { | ||
| 1034 | - let mut occupied_tags: TagMask = 0; | ||
| 1035 | - let mut urgent_tags: TagMask = 0; | ||
| 1036 | - for client in self.clients.values() { | ||
| 1037 | - if client.monitor_index == monitor_index { | ||
| 1038 | - occupied_tags |= client.tags; | ||
| 1039 | - if client.is_urgent { | ||
| 1040 | - urgent_tags |= client.tags; | ||
| 1041 | - } | ||
| 1042 | - } | ||
| 1043 | - } | ||
| 1044 | - | ||
| 1045 | - let draw_blocks = monitor_index == self.selected_monitor; | ||
| 1046 | - bar.invalidate(); | ||
| 1047 | - bar.draw( | ||
| 1048 | - &self.connection, | ||
| 1049 | - &self.font, | ||
| 1050 | - self.display, | ||
| 1051 | - monitor.tagset[monitor.selected_tags_index], | ||
| 1052 | - occupied_tags, | ||
| 1053 | - urgent_tags, | ||
| 1054 | - draw_blocks, | ||
| 1055 | - &layout_symbol, | ||
| 1056 | - keychord_indicator.as_deref(), | ||
| 1057 | - )?; | ||
| 1058 | - } | ||
| 1059 | - } | ||
| 1060 | - Ok(()) | ||
| 1061 | - } | ||
| 1062 | - | ||
| 1063 | - fn update_tab_bars(&mut self) -> WmResult<()> { | ||
| 1064 | - for (monitor_index, monitor) in self.monitors.iter().enumerate() { | ||
| 1065 | - if let Some(tab_bar) = self.tab_bars.get_mut(monitor_index) { | ||
| 1066 | - let visible_windows: Vec<(Window, String)> = self | ||
| 1067 | - .windows | ||
| 1068 | - .iter() | ||
| 1069 | - .filter_map(|&window| { | ||
| 1070 | - if let Some(client) = self.clients.get(&window) { | ||
| 1071 | - if client.monitor_index != monitor_index | ||
| 1072 | - || self.floating_windows.contains(&window) | ||
| 1073 | - || self.fullscreen_windows.contains(&window) | ||
| 1074 | - { | ||
| 1075 | - return None; | ||
| 1076 | - } | ||
| 1077 | - if (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0 { | ||
| 1078 | - return Some((window, client.name.clone())); | ||
| 1079 | - } | ||
| 1080 | - } | ||
| 1081 | - None | ||
| 1082 | - }) | ||
| 1083 | - .collect(); | ||
| 1084 | - | ||
| 1085 | - let focused_window = monitor.selected_client; | ||
| 1086 | - | ||
| 1087 | - tab_bar.draw( | ||
| 1088 | - &self.connection, | ||
| 1089 | - &self.font, | ||
| 1090 | - &visible_windows, | ||
| 1091 | - focused_window, | ||
| 1092 | - )?; | ||
| 1093 | - } | ||
| 1094 | - } | ||
| 1095 | - Ok(()) | ||
| 1096 | - } | ||
| 1097 | - | ||
| 1098 | - fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { | ||
| 1099 | - match action { | ||
| 1100 | - KeyAction::Spawn => handlers::handle_spawn_action(action, arg, self.selected_monitor)?, | ||
| 1101 | - KeyAction::SpawnTerminal => { | ||
| 1102 | - crate::signal::spawn_detached(&self.config.terminal); | ||
| 1103 | - } | ||
| 1104 | - KeyAction::KillClient => { | ||
| 1105 | - if let Some(focused) = self | ||
| 1106 | - .monitors | ||
| 1107 | - .get(self.selected_monitor) | ||
| 1108 | - .and_then(|m| m.selected_client) | ||
| 1109 | - { | ||
| 1110 | - self.kill_client(focused)?; | ||
| 1111 | - } | ||
| 1112 | - } | ||
| 1113 | - KeyAction::ToggleFullScreen => { | ||
| 1114 | - self.fullscreen()?; | ||
| 1115 | - self.restack()?; | ||
| 1116 | - } | ||
| 1117 | - KeyAction::ChangeLayout => { | ||
| 1118 | - if let Arg::Str(layout_name) = arg { | ||
| 1119 | - match layout_from_str(layout_name) { | ||
| 1120 | - Ok(layout) => { | ||
| 1121 | - self.layout = layout; | ||
| 1122 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 1123 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 1124 | - pertag.layouts[pertag.current_tag] = layout_name.to_string(); | ||
| 1125 | - } | ||
| 1126 | - } | ||
| 1127 | - if layout_name != "normie" && layout_name != "floating" { | ||
| 1128 | - self.floating_windows.clear(); | ||
| 1129 | - } | ||
| 1130 | - self.apply_layout()?; | ||
| 1131 | - self.update_bar()?; | ||
| 1132 | - self.restack()?; | ||
| 1133 | - } | ||
| 1134 | - Err(e) => eprintln!("Failed to change layout: {}", e), | ||
| 1135 | - } | ||
| 1136 | - } | ||
| 1137 | - } | ||
| 1138 | - KeyAction::CycleLayout => { | ||
| 1139 | - let current_name = self.layout.name(); | ||
| 1140 | - let next_name = next_layout(current_name); | ||
| 1141 | - match layout_from_str(next_name) { | ||
| 1142 | - Ok(layout) => { | ||
| 1143 | - self.layout = layout; | ||
| 1144 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 1145 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 1146 | - pertag.layouts[pertag.current_tag] = next_name.to_string(); | ||
| 1147 | - } | ||
| 1148 | - } | ||
| 1149 | - if next_name != "normie" && next_name != "floating" { | ||
| 1150 | - self.floating_windows.clear(); | ||
| 1151 | - } | ||
| 1152 | - self.apply_layout()?; | ||
| 1153 | - self.update_bar()?; | ||
| 1154 | - self.restack()?; | ||
| 1155 | - } | ||
| 1156 | - Err(e) => eprintln!("Failed to cycle layout: {}", e), | ||
| 1157 | - } | ||
| 1158 | - } | ||
| 1159 | - KeyAction::ToggleFloating => { | ||
| 1160 | - self.toggle_floating()?; | ||
| 1161 | - self.restack()?; | ||
| 1162 | - } | ||
| 1163 | - | ||
| 1164 | - KeyAction::FocusStack => { | ||
| 1165 | - if let Arg::Int(direction) = arg { | ||
| 1166 | - self.focusstack(*direction)?; | ||
| 1167 | - self.restack()?; | ||
| 1168 | - } | ||
| 1169 | - } | ||
| 1170 | - KeyAction::MoveStack => { | ||
| 1171 | - if let Arg::Int(direction) = arg { | ||
| 1172 | - self.move_stack(*direction)?; | ||
| 1173 | - self.restack()?; | ||
| 1174 | - } | ||
| 1175 | - } | ||
| 1176 | - KeyAction::Quit | KeyAction::Restart => {} | ||
| 1177 | - KeyAction::ViewTag => { | ||
| 1178 | - if let Arg::Int(tag_index) = arg { | ||
| 1179 | - self.view_tag(*tag_index as usize)?; | ||
| 1180 | - } | ||
| 1181 | - } | ||
| 1182 | - KeyAction::ViewNextTag => { | ||
| 1183 | - let monitor = self.get_selected_monitor(); | ||
| 1184 | - let current_tag_index = unmask_tag(monitor.get_selected_tag()) as i32; | ||
| 1185 | - let len = self.config.tags.len() as i32; | ||
| 1186 | - self.view_tag((current_tag_index + 1).rem_euclid(len) as usize)?; | ||
| 1187 | - } | ||
| 1188 | - KeyAction::ViewPreviousTag => { | ||
| 1189 | - let monitor = self.get_selected_monitor(); | ||
| 1190 | - let current_tag_index = unmask_tag(monitor.get_selected_tag()) as i32; | ||
| 1191 | - let len = self.config.tags.len() as i32; | ||
| 1192 | - self.view_tag((current_tag_index - 1).rem_euclid(len) as usize)?; | ||
| 1193 | - } | ||
| 1194 | - KeyAction::ViewNextNonEmptyTag => { | ||
| 1195 | - let monitor = self.get_selected_monitor(); | ||
| 1196 | - let current = unmask_tag(monitor.get_selected_tag()) as i32; | ||
| 1197 | - let len = self.config.tags.len() as i32; | ||
| 1198 | - let mon_num = monitor.monitor_number; | ||
| 1199 | - | ||
| 1200 | - for offset in 1..len { | ||
| 1201 | - let next = (current + offset).rem_euclid(len) as usize; | ||
| 1202 | - if self.has_windows_on_tag(mon_num, next) { | ||
| 1203 | - self.view_tag(next)?; | ||
| 1204 | - break; | ||
| 1205 | - } | ||
| 1206 | - } | ||
| 1207 | - } | ||
| 1208 | - KeyAction::ViewPreviousNonEmptyTag => { | ||
| 1209 | - let monitor = self.get_selected_monitor(); | ||
| 1210 | - let current = unmask_tag(monitor.get_selected_tag()) as i32; | ||
| 1211 | - let len = self.config.tags.len() as i32; | ||
| 1212 | - let mon_num = monitor.monitor_number; | ||
| 1213 | - | ||
| 1214 | - for offset in 1..len { | ||
| 1215 | - let prev = (current - offset).rem_euclid(len) as usize; | ||
| 1216 | - if self.has_windows_on_tag(mon_num, prev) { | ||
| 1217 | - self.view_tag(prev)?; | ||
| 1218 | - break; | ||
| 1219 | - } | ||
| 1220 | - } | ||
| 1221 | - } | ||
| 1222 | - KeyAction::ToggleView => { | ||
| 1223 | - if let Arg::Int(tag_index) = arg { | ||
| 1224 | - self.toggleview(*tag_index as usize)?; | ||
| 1225 | - } | ||
| 1226 | - } | ||
| 1227 | - KeyAction::MoveToTag => { | ||
| 1228 | - if let Arg::Int(tag_index) = arg { | ||
| 1229 | - self.move_to_tag(*tag_index as usize)?; | ||
| 1230 | - } | ||
| 1231 | - } | ||
| 1232 | - KeyAction::ToggleTag => { | ||
| 1233 | - if let Arg::Int(tag_index) = arg { | ||
| 1234 | - self.toggletag(*tag_index as usize)?; | ||
| 1235 | - } | ||
| 1236 | - } | ||
| 1237 | - KeyAction::ToggleGaps => { | ||
| 1238 | - self.gaps_enabled = !self.gaps_enabled; | ||
| 1239 | - self.apply_layout()?; | ||
| 1240 | - self.restack()?; | ||
| 1241 | - } | ||
| 1242 | - KeyAction::FocusMonitor => { | ||
| 1243 | - if let Arg::Int(direction) = arg { | ||
| 1244 | - self.focus_monitor(*direction)?; | ||
| 1245 | - } | ||
| 1246 | - } | ||
| 1247 | - KeyAction::TagMonitor => { | ||
| 1248 | - if let Arg::Int(direction) = arg { | ||
| 1249 | - self.send_window_to_adjacent_monitor(*direction)?; | ||
| 1250 | - } | ||
| 1251 | - } | ||
| 1252 | - KeyAction::ShowKeybindOverlay => { | ||
| 1253 | - let monitor = &self.monitors[self.selected_monitor]; | ||
| 1254 | - self.keybind_overlay.toggle( | ||
| 1255 | - &self.connection, | ||
| 1256 | - &self.font, | ||
| 1257 | - &self.config.keybindings, | ||
| 1258 | - monitor.screen_x as i16, | ||
| 1259 | - monitor.screen_y as i16, | ||
| 1260 | - monitor.screen_width as u16, | ||
| 1261 | - monitor.screen_height as u16, | ||
| 1262 | - )?; | ||
| 1263 | - } | ||
| 1264 | - KeyAction::SetMasterFactor => { | ||
| 1265 | - if let Arg::Int(delta) = arg { | ||
| 1266 | - self.set_master_factor(*delta as f32 / 100.0)?; | ||
| 1267 | - } | ||
| 1268 | - } | ||
| 1269 | - KeyAction::IncNumMaster => { | ||
| 1270 | - if let Arg::Int(delta) = arg { | ||
| 1271 | - self.inc_num_master(*delta)?; | ||
| 1272 | - } | ||
| 1273 | - } | ||
| 1274 | - KeyAction::ScrollLeft => { | ||
| 1275 | - self.scroll_layout(-1)?; | ||
| 1276 | - } | ||
| 1277 | - KeyAction::ScrollRight => { | ||
| 1278 | - self.scroll_layout(1)?; | ||
| 1279 | - } | ||
| 1280 | - KeyAction::None => {} | ||
| 1281 | - } | ||
| 1282 | - Ok(()) | ||
| 1283 | - } | ||
| 1284 | - | ||
| 1285 | - fn is_window_visible(&self, window: Window) -> bool { | ||
| 1286 | - if let Some(client) = self.clients.get(&window) { | ||
| 1287 | - let monitor = self.monitors.get(client.monitor_index); | ||
| 1288 | - let selected_tags = monitor | ||
| 1289 | - .map(|m| m.tagset[m.selected_tags_index]) | ||
| 1290 | - .unwrap_or(0); | ||
| 1291 | - (client.tags & selected_tags) != 0 | ||
| 1292 | - } else { | ||
| 1293 | - false | ||
| 1294 | - } | ||
| 1295 | - } | ||
| 1296 | - | ||
| 1297 | - fn visible_windows(&self) -> Vec<Window> { | ||
| 1298 | - let mut result = Vec::new(); | ||
| 1299 | - for monitor in &self.monitors { | ||
| 1300 | - let mut current = monitor.clients_head; | ||
| 1301 | - while let Some(window) = current { | ||
| 1302 | - if let Some(client) = self.clients.get(&window) { | ||
| 1303 | - let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; | ||
| 1304 | - if visible_tags != 0 { | ||
| 1305 | - result.push(window); | ||
| 1306 | - } | ||
| 1307 | - current = client.next; | ||
| 1308 | - } else { | ||
| 1309 | - break; | ||
| 1310 | - } | ||
| 1311 | - } | ||
| 1312 | - } | ||
| 1313 | - result | ||
| 1314 | - } | ||
| 1315 | - | ||
| 1316 | - fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { | ||
| 1317 | - let mut result = Vec::new(); | ||
| 1318 | - if let Some(monitor) = self.monitors.get(monitor_index) { | ||
| 1319 | - let mut current = monitor.clients_head; | ||
| 1320 | - while let Some(window) = current { | ||
| 1321 | - if let Some(client) = self.clients.get(&window) { | ||
| 1322 | - let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; | ||
| 1323 | - if visible_tags != 0 { | ||
| 1324 | - result.push(window); | ||
| 1325 | - } | ||
| 1326 | - current = client.next; | ||
| 1327 | - } else { | ||
| 1328 | - break; | ||
| 1329 | - } | ||
| 1330 | - } | ||
| 1331 | - } | ||
| 1332 | - result | ||
| 1333 | - } | ||
| 1334 | - | ||
| 1335 | - fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { | ||
| 1336 | - self.monitors | ||
| 1337 | - .iter() | ||
| 1338 | - .position(|mon| mon.contains_point(x, y)) | ||
| 1339 | - } | ||
| 1340 | - | ||
| 1341 | - fn get_monitor_for_rect(&self, x: i32, y: i32, w: i32, h: i32) -> usize { | ||
| 1342 | - let mut best_monitor = self.selected_monitor; | ||
| 1343 | - let mut max_area = 0; | ||
| 1344 | - | ||
| 1345 | - for (idx, monitor) in self.monitors.iter().enumerate() { | ||
| 1346 | - let intersect_width = 0.max( | ||
| 1347 | - (x + w).min(monitor.window_area_x + monitor.window_area_width) | ||
| 1348 | - - x.max(monitor.window_area_x), | ||
| 1349 | - ); | ||
| 1350 | - let intersect_height = 0.max( | ||
| 1351 | - (y + h).min(monitor.window_area_y + monitor.window_area_height) | ||
| 1352 | - - y.max(monitor.window_area_y), | ||
| 1353 | - ); | ||
| 1354 | - let area = intersect_width * intersect_height; | ||
| 1355 | - | ||
| 1356 | - if area > max_area { | ||
| 1357 | - max_area = area; | ||
| 1358 | - best_monitor = idx; | ||
| 1359 | - } | ||
| 1360 | - } | ||
| 1361 | - | ||
| 1362 | - best_monitor | ||
| 1363 | - } | ||
| 1364 | - | ||
| 1365 | - fn move_window_to_monitor( | ||
| 1366 | - &mut self, | ||
| 1367 | - window: Window, | ||
| 1368 | - target_monitor_index: usize, | ||
| 1369 | - ) -> WmResult<()> { | ||
| 1370 | - let current_monitor_index = self.clients.get(&window).map(|c| c.monitor_index); | ||
| 1371 | - | ||
| 1372 | - if let Some(current_idx) = current_monitor_index | ||
| 1373 | - && current_idx == target_monitor_index | ||
| 1374 | - { | ||
| 1375 | - return Ok(()); | ||
| 1376 | - } | ||
| 1377 | - | ||
| 1378 | - self.unfocus(window, false)?; | ||
| 1379 | - self.detach(window); | ||
| 1380 | - self.detach_stack(window); | ||
| 1381 | - | ||
| 1382 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 1383 | - client.monitor_index = target_monitor_index; | ||
| 1384 | - if let Some(target_monitor) = self.monitors.get(target_monitor_index) { | ||
| 1385 | - client.tags = target_monitor.tagset[target_monitor.selected_tags_index]; | ||
| 1386 | - } | ||
| 1387 | - } | ||
| 1388 | - | ||
| 1389 | - self.attach_aside(window, target_monitor_index); | ||
| 1390 | - self.attach_stack(window, target_monitor_index); | ||
| 1391 | - | ||
| 1392 | - self.focus(None)?; | ||
| 1393 | - self.apply_layout()?; | ||
| 1394 | - | ||
| 1395 | - Ok(()) | ||
| 1396 | - } | ||
| 1397 | - | ||
| 1398 | - fn get_adjacent_monitor(&self, direction: i32) -> Option<usize> { | ||
| 1399 | - if self.monitors.len() <= 1 { | ||
| 1400 | - return None; | ||
| 1401 | - } | ||
| 1402 | - | ||
| 1403 | - if direction > 0 { | ||
| 1404 | - if self.selected_monitor + 1 < self.monitors.len() { | ||
| 1405 | - Some(self.selected_monitor + 1) | ||
| 1406 | - } else { | ||
| 1407 | - Some(0) | ||
| 1408 | - } | ||
| 1409 | - } else if self.selected_monitor == 0 { | ||
| 1410 | - Some(self.monitors.len() - 1) | ||
| 1411 | - } else { | ||
| 1412 | - Some(self.selected_monitor - 1) | ||
| 1413 | - } | ||
| 1414 | - } | ||
| 1415 | - | ||
| 1416 | - fn is_visible(&self, window: Window) -> bool { | ||
| 1417 | - let Some(client) = self.clients.get(&window) else { | ||
| 1418 | - return false; | ||
| 1419 | - }; | ||
| 1420 | - | ||
| 1421 | - let Some(monitor) = self.monitors.get(client.monitor_index) else { | ||
| 1422 | - return false; | ||
| 1423 | - }; | ||
| 1424 | - | ||
| 1425 | - (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0 | ||
| 1426 | - } | ||
| 1427 | - | ||
| 1428 | - fn showhide(&mut self, window: Option<Window>) -> WmResult<()> { | ||
| 1429 | - let Some(window) = window else { | ||
| 1430 | - return Ok(()); | ||
| 1431 | - }; | ||
| 1432 | - | ||
| 1433 | - let Some(client) = self.clients.get(&window).cloned() else { | ||
| 1434 | - return Ok(()); | ||
| 1435 | - }; | ||
| 1436 | - | ||
| 1437 | - let monitor = match self.monitors.get(client.monitor_index) { | ||
| 1438 | - Some(m) => m, | ||
| 1439 | - None => return Ok(()), | ||
| 1440 | - }; | ||
| 1441 | - | ||
| 1442 | - let is_visible = (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0; | ||
| 1443 | - | ||
| 1444 | - if is_visible { | ||
| 1445 | - self.connection.configure_window( | ||
| 1446 | - window, | ||
| 1447 | - &ConfigureWindowAux::new() | ||
| 1448 | - .x(client.x_position as i32) | ||
| 1449 | - .y(client.y_position as i32), | ||
| 1450 | - )?; | ||
| 1451 | - | ||
| 1452 | - let is_floating = client.is_floating; | ||
| 1453 | - let is_fullscreen = client.is_fullscreen; | ||
| 1454 | - let has_no_layout = self.layout.name() == LayoutType::Normie.as_str(); | ||
| 1455 | - | ||
| 1456 | - if (has_no_layout || is_floating) && !is_fullscreen { | ||
| 1457 | - let (x, y, w, h, changed) = self.apply_size_hints( | ||
| 1458 | - window, | ||
| 1459 | - client.x_position as i32, | ||
| 1460 | - client.y_position as i32, | ||
| 1461 | - client.width as i32, | ||
| 1462 | - client.height as i32, | ||
| 1463 | - ); | ||
| 1464 | - if changed { | ||
| 1465 | - if let Some(c) = self.clients.get_mut(&window) { | ||
| 1466 | - c.old_x_position = c.x_position; | ||
| 1467 | - c.old_y_position = c.y_position; | ||
| 1468 | - c.old_width = c.width; | ||
| 1469 | - c.old_height = c.height; | ||
| 1470 | - c.x_position = x as i16; | ||
| 1471 | - c.y_position = y as i16; | ||
| 1472 | - c.width = w as u16; | ||
| 1473 | - c.height = h as u16; | ||
| 1474 | - } | ||
| 1475 | - self.connection.configure_window( | ||
| 1476 | - window, | ||
| 1477 | - &ConfigureWindowAux::new() | ||
| 1478 | - .x(x) | ||
| 1479 | - .y(y) | ||
| 1480 | - .width(w as u32) | ||
| 1481 | - .height(h as u32) | ||
| 1482 | - .border_width(self.config.border_width), | ||
| 1483 | - )?; | ||
| 1484 | - self.send_configure_notify(window)?; | ||
| 1485 | - self.connection.flush()?; | ||
| 1486 | - } | ||
| 1487 | - } | ||
| 1488 | - | ||
| 1489 | - self.showhide(client.stack_next)?; | ||
| 1490 | - } else { | ||
| 1491 | - self.showhide(client.stack_next)?; | ||
| 1492 | - | ||
| 1493 | - let width = client.width_with_border() as i32; | ||
| 1494 | - self.connection.configure_window( | ||
| 1495 | - window, | ||
| 1496 | - &ConfigureWindowAux::new() | ||
| 1497 | - .x(width * -2) | ||
| 1498 | - .y(client.y_position as i32), | ||
| 1499 | - )?; | ||
| 1500 | - } | ||
| 1501 | - | ||
| 1502 | - Ok(()) | ||
| 1503 | - } | ||
| 1504 | - | ||
| 1505 | - pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { | ||
| 1506 | - if tag_index >= self.config.tags.len() { | ||
| 1507 | - return Ok(()); | ||
| 1508 | - } | ||
| 1509 | - | ||
| 1510 | - let new_tagset = tag_mask(tag_index); | ||
| 1511 | - let mut layout_name: Option<String> = None; | ||
| 1512 | - let mut toggle_bar = false; | ||
| 1513 | - | ||
| 1514 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 1515 | - if new_tagset == monitor.tagset[monitor.selected_tags_index] { | ||
| 1516 | - if !self.config.tag_back_and_forth { | ||
| 1517 | - return Ok(()); | ||
| 1518 | - } | ||
| 1519 | - monitor.tagset.swap(0, 1); | ||
| 1520 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 1521 | - let tmp = pertag.previous_tag; | ||
| 1522 | - pertag.previous_tag = pertag.current_tag; | ||
| 1523 | - pertag.current_tag = tmp; | ||
| 1524 | - } | ||
| 1525 | - } else { | ||
| 1526 | - monitor.selected_tags_index ^= 1; | ||
| 1527 | - monitor.tagset[monitor.selected_tags_index] = new_tagset; | ||
| 1528 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 1529 | - pertag.previous_tag = pertag.current_tag; | ||
| 1530 | - pertag.current_tag = tag_index + 1; | ||
| 1531 | - } | ||
| 1532 | - } | ||
| 1533 | - | ||
| 1534 | - if let Some(ref pertag) = monitor.pertag { | ||
| 1535 | - monitor.num_master = pertag.num_masters[pertag.current_tag]; | ||
| 1536 | - monitor.master_factor = pertag.master_factors[pertag.current_tag]; | ||
| 1537 | - layout_name = Some(pertag.layouts[pertag.current_tag].clone()); | ||
| 1538 | - if monitor.show_bar != pertag.show_bars[pertag.current_tag] { | ||
| 1539 | - toggle_bar = true; | ||
| 1540 | - } | ||
| 1541 | - } | ||
| 1542 | - } | ||
| 1543 | - | ||
| 1544 | - if let Some(name) = layout_name { | ||
| 1545 | - if let Ok(layout) = layout_from_str(&name) { | ||
| 1546 | - self.layout = layout; | ||
| 1547 | - } | ||
| 1548 | - } | ||
| 1549 | - | ||
| 1550 | - if toggle_bar { | ||
| 1551 | - self.toggle_bar()?; | ||
| 1552 | - } | ||
| 1553 | - | ||
| 1554 | - self.save_selected_tags()?; | ||
| 1555 | - self.focus(None)?; | ||
| 1556 | - self.apply_layout()?; | ||
| 1557 | - self.update_bar()?; | ||
| 1558 | - | ||
| 1559 | - Ok(()) | ||
| 1560 | - } | ||
| 1561 | - | ||
| 1562 | - pub fn toggleview(&mut self, tag_index: usize) -> WmResult<()> { | ||
| 1563 | - if tag_index >= self.config.tags.len() { | ||
| 1564 | - return Ok(()); | ||
| 1565 | - } | ||
| 1566 | - | ||
| 1567 | - let num_tags = self.config.tags.len(); | ||
| 1568 | - let all_tags_mask = (1u32 << num_tags) - 1; | ||
| 1569 | - let mut layout_name: Option<String> = None; | ||
| 1570 | - let mut toggle_bar = false; | ||
| 1571 | - | ||
| 1572 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 1573 | - let mask = tag_mask(tag_index); | ||
| 1574 | - let new_tagset = monitor.tagset[monitor.selected_tags_index] ^ mask; | ||
| 1575 | - | ||
| 1576 | - if new_tagset == 0 { | ||
| 1577 | - return Ok(()); | ||
| 1578 | - } | ||
| 1579 | - | ||
| 1580 | - monitor.tagset[monitor.selected_tags_index] = new_tagset; | ||
| 1581 | - | ||
| 1582 | - if let Some(ref mut pertag) = monitor.pertag { | ||
| 1583 | - if new_tagset == all_tags_mask { | ||
| 1584 | - pertag.previous_tag = pertag.current_tag; | ||
| 1585 | - pertag.current_tag = 0; | ||
| 1586 | - } | ||
| 1587 | - | ||
| 1588 | - if pertag.current_tag > 0 && (new_tagset & (1 << (pertag.current_tag - 1))) == 0 { | ||
| 1589 | - pertag.previous_tag = pertag.current_tag; | ||
| 1590 | - pertag.current_tag = (new_tagset.trailing_zeros() as usize) + 1; | ||
| 1591 | - } | ||
| 1592 | - | ||
| 1593 | - monitor.num_master = pertag.num_masters[pertag.current_tag]; | ||
| 1594 | - monitor.master_factor = pertag.master_factors[pertag.current_tag]; | ||
| 1595 | - layout_name = Some(pertag.layouts[pertag.current_tag].clone()); | ||
| 1596 | - if monitor.show_bar != pertag.show_bars[pertag.current_tag] { | ||
| 1597 | - toggle_bar = true; | ||
| 1598 | - } | ||
| 1599 | - } | ||
| 1600 | - } | ||
| 1601 | - | ||
| 1602 | - if let Some(name) = layout_name { | ||
| 1603 | - if let Ok(layout) = layout_from_str(&name) { | ||
| 1604 | - self.layout = layout; | ||
| 1605 | - } | ||
| 1606 | - } | ||
| 1607 | - | ||
| 1608 | - if toggle_bar { | ||
| 1609 | - self.toggle_bar()?; | ||
| 1610 | - } | ||
| 1611 | - | ||
| 1612 | - self.save_selected_tags()?; | ||
| 1613 | - self.focus(None)?; | ||
| 1614 | - self.apply_layout()?; | ||
| 1615 | - self.update_bar()?; | ||
| 1616 | - | ||
| 1617 | - Ok(()) | ||
| 1618 | - } | ||
| 1619 | - | ||
| 1620 | - fn save_selected_tags(&self) -> WmResult<()> { | ||
| 1621 | - let net_current_desktop = self.atoms.net_current_desktop; | ||
| 1622 | - | ||
| 1623 | - let selected_tags = self | ||
| 1624 | - .monitors | ||
| 1625 | - .get(self.selected_monitor) | ||
| 1626 | - .map(|m| m.tagset[m.selected_tags_index]) | ||
| 1627 | - .unwrap_or(tag_mask(0)); | ||
| 1628 | - let desktop = selected_tags.trailing_zeros(); | ||
| 1629 | - | ||
| 1630 | - let bytes = (desktop as u32).to_ne_bytes(); | ||
| 1631 | - self.connection.change_property( | ||
| 1632 | - PropMode::REPLACE, | ||
| 1633 | - self.root, | ||
| 1634 | - net_current_desktop, | ||
| 1635 | - AtomEnum::CARDINAL, | ||
| 1636 | - 32, | ||
| 1637 | - 1, | ||
| 1638 | - &bytes, | ||
| 1639 | - )?; | ||
| 1640 | - | ||
| 1641 | - self.connection.flush()?; | ||
| 1642 | - Ok(()) | ||
| 1643 | - } | ||
| 1644 | - | ||
| 1645 | - pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { | ||
| 1646 | - if tag_index >= self.config.tags.len() { | ||
| 1647 | - return Ok(()); | ||
| 1648 | - } | ||
| 1649 | - | ||
| 1650 | - let focused = match self | ||
| 1651 | - .monitors | ||
| 1652 | - .get(self.selected_monitor) | ||
| 1653 | - .and_then(|m| m.selected_client) | ||
| 1654 | - { | ||
| 1655 | - Some(win) => win, | ||
| 1656 | - None => return Ok(()), | ||
| 1657 | - }; | ||
| 1658 | - | ||
| 1659 | - let mask = tag_mask(tag_index); | ||
| 1660 | - | ||
| 1661 | - if let Some(client) = self.clients.get_mut(&focused) { | ||
| 1662 | - client.tags = mask; | ||
| 1663 | - } | ||
| 1664 | - | ||
| 1665 | - if let Err(error) = self.save_client_tag(focused, mask) { | ||
| 1666 | - eprintln!("Failed to save client tag: {:?}", error); | ||
| 1667 | - } | ||
| 1668 | - | ||
| 1669 | - self.focus(None)?; | ||
| 1670 | - self.apply_layout()?; | ||
| 1671 | - self.update_bar()?; | ||
| 1672 | - | ||
| 1673 | - Ok(()) | ||
| 1674 | - } | ||
| 1675 | - | ||
| 1676 | - pub fn toggletag(&mut self, tag_index: usize) -> WmResult<()> { | ||
| 1677 | - if tag_index >= self.config.tags.len() { | ||
| 1678 | - return Ok(()); | ||
| 1679 | - } | ||
| 1680 | - | ||
| 1681 | - let focused = match self | ||
| 1682 | - .monitors | ||
| 1683 | - .get(self.selected_monitor) | ||
| 1684 | - .and_then(|m| m.selected_client) | ||
| 1685 | - { | ||
| 1686 | - Some(win) => win, | ||
| 1687 | - None => return Ok(()), | ||
| 1688 | - }; | ||
| 1689 | - | ||
| 1690 | - let mask = tag_mask(tag_index); | ||
| 1691 | - let current_tags = self.clients.get(&focused).map(|c| c.tags).unwrap_or(0); | ||
| 1692 | - let new_tags = current_tags ^ mask; | ||
| 1693 | - | ||
| 1694 | - if new_tags == 0 { | ||
| 1695 | - return Ok(()); | ||
| 1696 | - } | ||
| 1697 | - | ||
| 1698 | - if let Some(client) = self.clients.get_mut(&focused) { | ||
| 1699 | - client.tags = new_tags; | ||
| 1700 | - } | ||
| 1701 | - | ||
| 1702 | - if let Err(error) = self.save_client_tag(focused, new_tags) { | ||
| 1703 | - eprintln!("Failed to save client tag: {:?}", error); | ||
| 1704 | - } | ||
| 1705 | - | ||
| 1706 | - self.focus(None)?; | ||
| 1707 | - self.apply_layout()?; | ||
| 1708 | - self.update_bar()?; | ||
| 1709 | - | ||
| 1710 | - Ok(()) | ||
| 1711 | - } | ||
| 1712 | - | ||
| 1713 | - pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { | ||
| 1714 | - let visible = self.visible_windows(); | ||
| 1715 | - | ||
| 1716 | - if visible.is_empty() { | ||
| 1717 | - return Ok(()); | ||
| 1718 | - } | ||
| 1719 | - | ||
| 1720 | - let current = self | ||
| 1721 | - .monitors | ||
| 1722 | - .get(self.selected_monitor) | ||
| 1723 | - .and_then(|m| m.selected_client); | ||
| 1724 | - | ||
| 1725 | - let next_window = if let Some(current) = current { | ||
| 1726 | - if let Some(current_index) = visible.iter().position(|&w| w == current) { | ||
| 1727 | - let next_index = if direction > 0 { | ||
| 1728 | - (current_index + 1) % visible.len() | ||
| 1729 | - } else { | ||
| 1730 | - (current_index + visible.len() - 1) % visible.len() | ||
| 1731 | - }; | ||
| 1732 | - visible[next_index] | ||
| 1733 | - } else { | ||
| 1734 | - visible[0] | ||
| 1735 | - } | ||
| 1736 | - } else { | ||
| 1737 | - visible[0] | ||
| 1738 | - }; | ||
| 1739 | - | ||
| 1740 | - let is_tabbed = self.layout.name() == "tabbed"; | ||
| 1741 | - if is_tabbed { | ||
| 1742 | - self.connection.configure_window( | ||
| 1743 | - next_window, | ||
| 1744 | - &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | ||
| 1745 | - )?; | ||
| 1746 | - } | ||
| 1747 | - | ||
| 1748 | - self.focus(Some(next_window))?; | ||
| 1749 | - | ||
| 1750 | - if is_tabbed { | ||
| 1751 | - self.update_tab_bars()?; | ||
| 1752 | - } | ||
| 1753 | - | ||
| 1754 | - Ok(()) | ||
| 1755 | - } | ||
| 1756 | - | ||
| 1757 | - fn grab_keys(&mut self) -> WmResult<()> { | ||
| 1758 | - self.keyboard_mapping = Some(keyboard::grab_keys( | ||
| 1759 | - &self.connection, | ||
| 1760 | - self.root, | ||
| 1761 | - &self.config.keybindings, | ||
| 1762 | - self.current_key, | ||
| 1763 | - )?); | ||
| 1764 | - Ok(()) | ||
| 1765 | - } | ||
| 1766 | - | ||
| 1767 | - fn kill_client(&self, window: Window) -> WmResult<()> { | ||
| 1768 | - if self.send_event(window, self.atoms.wm_delete_window)? { | ||
| 1769 | - self.connection.flush()?; | ||
| 1770 | - } else { | ||
| 1771 | - eprintln!( | ||
| 1772 | - "Window {} doesn't support WM_DELETE_WINDOW, killing forcefully", | ||
| 1773 | - window | ||
| 1774 | - ); | ||
| 1775 | - self.connection.kill_client(window)?; | ||
| 1776 | - self.connection.flush()?; | ||
| 1777 | - } | ||
| 1778 | - Ok(()) | ||
| 1779 | - } | ||
| 1780 | - | ||
| 1781 | - fn send_event(&self, window: Window, protocol: Atom) -> WmResult<bool> { | ||
| 1782 | - let protocols_reply = self | ||
| 1783 | - .connection | ||
| 1784 | - .get_property( | ||
| 1785 | - false, | ||
| 1786 | - window, | ||
| 1787 | - self.atoms.wm_protocols, | ||
| 1788 | - AtomEnum::ATOM, | ||
| 1789 | - 0, | ||
| 1790 | - 100, | ||
| 1791 | - )? | ||
| 1792 | - .reply(); | ||
| 1793 | - | ||
| 1794 | - let protocols_reply = match protocols_reply { | ||
| 1795 | - Ok(reply) => reply, | ||
| 1796 | - Err(_) => return Ok(false), | ||
| 1797 | - }; | ||
| 1798 | - | ||
| 1799 | - let protocols: Vec<Atom> = protocols_reply | ||
| 1800 | - .value | ||
| 1801 | - .chunks_exact(4) | ||
| 1802 | - .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])) | ||
| 1803 | - .collect(); | ||
| 1804 | - | ||
| 1805 | - if !protocols.contains(&protocol) { | ||
| 1806 | - return Ok(false); | ||
| 1807 | - } | ||
| 1808 | - | ||
| 1809 | - let event = x11rb::protocol::xproto::ClientMessageEvent { | ||
| 1810 | - response_type: x11rb::protocol::xproto::CLIENT_MESSAGE_EVENT, | ||
| 1811 | - format: 32, | ||
| 1812 | - sequence: 0, | ||
| 1813 | - window, | ||
| 1814 | - type_: self.atoms.wm_protocols, | ||
| 1815 | - data: x11rb::protocol::xproto::ClientMessageData::from([ | ||
| 1816 | - protocol, | ||
| 1817 | - x11rb::CURRENT_TIME, | ||
| 1818 | - 0, | ||
| 1819 | - 0, | ||
| 1820 | - 0, | ||
| 1821 | - ]), | ||
| 1822 | - }; | ||
| 1823 | - | ||
| 1824 | - self.connection | ||
| 1825 | - .send_event(false, window, EventMask::NO_EVENT, event)?; | ||
| 1826 | - self.connection.flush()?; | ||
| 1827 | - Ok(true) | ||
| 1828 | - } | ||
| 1829 | - | ||
| 1830 | - fn set_urgent(&mut self, window: Window, urgent: bool) -> WmResult<()> { | ||
| 1831 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 1832 | - client.is_urgent = urgent; | ||
| 1833 | - } | ||
| 1834 | - | ||
| 1835 | - let hints_reply = self | ||
| 1836 | - .connection | ||
| 1837 | - .get_property(false, window, AtomEnum::WM_HINTS, AtomEnum::WM_HINTS, 0, 9)? | ||
| 1838 | - .reply(); | ||
| 1839 | - | ||
| 1840 | - if let Ok(hints) = hints_reply | ||
| 1841 | - && hints.value.len() >= 4 | ||
| 1842 | - { | ||
| 1843 | - let mut flags = u32::from_ne_bytes([ | ||
| 1844 | - hints.value[0], | ||
| 1845 | - hints.value[1], | ||
| 1846 | - hints.value[2], | ||
| 1847 | - hints.value[3], | ||
| 1848 | - ]); | ||
| 1849 | - | ||
| 1850 | - if urgent { | ||
| 1851 | - flags |= 256; | ||
| 1852 | - } else { | ||
| 1853 | - flags &= !256; | ||
| 1854 | - } | ||
| 1855 | - | ||
| 1856 | - let mut new_hints = hints.value.clone(); | ||
| 1857 | - new_hints[0..4].copy_from_slice(&flags.to_ne_bytes()); | ||
| 1858 | - | ||
| 1859 | - self.connection.change_property( | ||
| 1860 | - PropMode::REPLACE, | ||
| 1861 | - window, | ||
| 1862 | - AtomEnum::WM_HINTS, | ||
| 1863 | - AtomEnum::WM_HINTS, | ||
| 1864 | - 32, | ||
| 1865 | - new_hints.len() as u32 / 4, | ||
| 1866 | - &new_hints, | ||
| 1867 | - )?; | ||
| 1868 | - } | ||
| 1869 | - | ||
| 1870 | - Ok(()) | ||
| 1871 | - } | ||
| 1872 | - | ||
| 1873 | - fn get_window_atom_property(&self, window: Window, property: Atom) -> WmResult<Option<Atom>> { | ||
| 1874 | - let reply = self | ||
| 1875 | - .connection | ||
| 1876 | - .get_property(false, window, property, AtomEnum::ATOM, 0, 1)? | ||
| 1877 | - .reply(); | ||
| 1878 | - | ||
| 1879 | - match reply { | ||
| 1880 | - Ok(prop) if !prop.value.is_empty() && prop.value.len() >= 4 => { | ||
| 1881 | - let atom = u32::from_ne_bytes([ | ||
| 1882 | - prop.value[0], | ||
| 1883 | - prop.value[1], | ||
| 1884 | - prop.value[2], | ||
| 1885 | - prop.value[3], | ||
| 1886 | - ]); | ||
| 1887 | - Ok(Some(atom)) | ||
| 1888 | - } | ||
| 1889 | - _ => Ok(None), | ||
| 1890 | - } | ||
| 1891 | - } | ||
| 1892 | - | ||
| 1893 | - fn get_window_atom_list_property(&self, window: Window, property: Atom) -> WmResult<Vec<Atom>> { | ||
| 1894 | - let reply = self | ||
| 1895 | - .connection | ||
| 1896 | - .get_property(false, window, property, AtomEnum::ATOM, 0, 32)? | ||
| 1897 | - .reply(); | ||
| 1898 | - | ||
| 1899 | - match reply { | ||
| 1900 | - Ok(prop) if !prop.value.is_empty() => { | ||
| 1901 | - let atoms: Vec<Atom> = prop | ||
| 1902 | - .value | ||
| 1903 | - .chunks_exact(4) | ||
| 1904 | - .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])) | ||
| 1905 | - .collect(); | ||
| 1906 | - Ok(atoms) | ||
| 1907 | - } | ||
| 1908 | - _ => Ok(Vec::new()), | ||
| 1909 | - } | ||
| 1910 | - } | ||
| 1911 | - | ||
| 1912 | - fn fullscreen(&mut self) -> WmResult<()> { | ||
| 1913 | - let Some(focused_window) = self | ||
| 1914 | - .monitors | ||
| 1915 | - .get(self.selected_monitor) | ||
| 1916 | - .and_then(|m| m.selected_client) | ||
| 1917 | - else { | ||
| 1918 | - return Ok(()); | ||
| 1919 | - }; | ||
| 1920 | - | ||
| 1921 | - let is_fullscreen = self.fullscreen_windows.contains(&focused_window); | ||
| 1922 | - self.set_window_fullscreen(focused_window, !is_fullscreen)?; | ||
| 1923 | - Ok(()) | ||
| 1924 | - } | ||
| 1925 | - | ||
| 1926 | - fn set_window_fullscreen(&mut self, window: Window, fullscreen: bool) -> WmResult<()> { | ||
| 1927 | - let monitor_idx = self | ||
| 1928 | - .clients | ||
| 1929 | - .get(&window) | ||
| 1930 | - .map(|c| c.monitor_index) | ||
| 1931 | - .unwrap_or(self.selected_monitor); | ||
| 1932 | - let monitor = &self.monitors[monitor_idx]; | ||
| 1933 | - | ||
| 1934 | - if fullscreen && !self.fullscreen_windows.contains(&window) { | ||
| 1935 | - let bytes = self.atoms.net_wm_state_fullscreen.to_ne_bytes().to_vec(); | ||
| 1936 | - self.connection.change_property( | ||
| 1937 | - PropMode::REPLACE, | ||
| 1938 | - window, | ||
| 1939 | - self.atoms.net_wm_state, | ||
| 1940 | - AtomEnum::ATOM, | ||
| 1941 | - 32, | ||
| 1942 | - 1, | ||
| 1943 | - &bytes, | ||
| 1944 | - )?; | ||
| 1945 | - | ||
| 1946 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 1947 | - client.is_fullscreen = true; | ||
| 1948 | - client.old_state = client.is_floating; | ||
| 1949 | - client.old_border_width = client.border_width; | ||
| 1950 | - client.old_x_position = client.x_position; | ||
| 1951 | - client.old_y_position = client.y_position; | ||
| 1952 | - client.old_width = client.width; | ||
| 1953 | - client.old_height = client.height; | ||
| 1954 | - client.border_width = 0; | ||
| 1955 | - client.is_floating = true; | ||
| 1956 | - } | ||
| 1957 | - | ||
| 1958 | - self.fullscreen_windows.insert(window); | ||
| 1959 | - self.floating_windows.insert(window); | ||
| 1960 | - | ||
| 1961 | - self.connection.configure_window( | ||
| 1962 | - window, | ||
| 1963 | - &x11rb::protocol::xproto::ConfigureWindowAux::new() | ||
| 1964 | - .border_width(0) | ||
| 1965 | - .x(monitor.screen_x) | ||
| 1966 | - .y(monitor.screen_y) | ||
| 1967 | - .width(monitor.screen_width as u32) | ||
| 1968 | - .height(monitor.screen_height as u32) | ||
| 1969 | - .stack_mode(x11rb::protocol::xproto::StackMode::ABOVE), | ||
| 1970 | - )?; | ||
| 1971 | - | ||
| 1972 | - self.connection.flush()?; | ||
| 1973 | - } else if !fullscreen && self.fullscreen_windows.contains(&window) { | ||
| 1974 | - self.connection.change_property( | ||
| 1975 | - PropMode::REPLACE, | ||
| 1976 | - window, | ||
| 1977 | - self.atoms.net_wm_state, | ||
| 1978 | - AtomEnum::ATOM, | ||
| 1979 | - 32, | ||
| 1980 | - 0, | ||
| 1981 | - &[], | ||
| 1982 | - )?; | ||
| 1983 | - | ||
| 1984 | - self.fullscreen_windows.remove(&window); | ||
| 1985 | - | ||
| 1986 | - let ( | ||
| 1987 | - was_floating, | ||
| 1988 | - restored_x, | ||
| 1989 | - restored_y, | ||
| 1990 | - restored_width, | ||
| 1991 | - restored_height, | ||
| 1992 | - restored_border, | ||
| 1993 | - ) = self | ||
| 1994 | - .clients | ||
| 1995 | - .get(&window) | ||
| 1996 | - .map(|client| { | ||
| 1997 | - ( | ||
| 1998 | - client.old_state, | ||
| 1999 | - client.old_x_position, | ||
| 2000 | - client.old_y_position, | ||
| 2001 | - client.old_width, | ||
| 2002 | - client.old_height, | ||
| 2003 | - client.old_border_width, | ||
| 2004 | - ) | ||
| 2005 | - }) | ||
| 2006 | - .unwrap_or((false, 0, 0, 100, 100, 0)); | ||
| 2007 | - | ||
| 2008 | - if !was_floating { | ||
| 2009 | - self.floating_windows.remove(&window); | ||
| 2010 | - } | ||
| 2011 | - | ||
| 2012 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 2013 | - client.is_fullscreen = false; | ||
| 2014 | - client.is_floating = client.old_state; | ||
| 2015 | - client.border_width = client.old_border_width; | ||
| 2016 | - client.x_position = client.old_x_position; | ||
| 2017 | - client.y_position = client.old_y_position; | ||
| 2018 | - client.width = client.old_width; | ||
| 2019 | - client.height = client.old_height; | ||
| 2020 | - } | ||
| 2021 | - | ||
| 2022 | - self.connection.configure_window( | ||
| 2023 | - window, | ||
| 2024 | - &ConfigureWindowAux::new() | ||
| 2025 | - .x(restored_x as i32) | ||
| 2026 | - .y(restored_y as i32) | ||
| 2027 | - .width(restored_width as u32) | ||
| 2028 | - .height(restored_height as u32) | ||
| 2029 | - .border_width(restored_border as u32), | ||
| 2030 | - )?; | ||
| 2031 | - | ||
| 2032 | - self.apply_layout()?; | ||
| 2033 | - } | ||
| 2034 | - | ||
| 2035 | - Ok(()) | ||
| 2036 | - } | ||
| 2037 | - | ||
| 2038 | - fn get_transient_parent(&self, window: Window) -> Option<Window> { | ||
| 2039 | - self.connection | ||
| 2040 | - .get_property( | ||
| 2041 | - false, | ||
| 2042 | - window, | ||
| 2043 | - AtomEnum::WM_TRANSIENT_FOR, | ||
| 2044 | - AtomEnum::WINDOW, | ||
| 2045 | - 0, | ||
| 2046 | - 1, | ||
| 2047 | - ) | ||
| 2048 | - .ok() | ||
| 2049 | - .and_then(|cookie| cookie.reply().ok()) | ||
| 2050 | - .filter(|reply| !reply.value.is_empty()) | ||
| 2051 | - .and_then(|reply| { | ||
| 2052 | - if reply.value.len() >= 4 { | ||
| 2053 | - let parent_window = u32::from_ne_bytes([ | ||
| 2054 | - reply.value[0], | ||
| 2055 | - reply.value[1], | ||
| 2056 | - reply.value[2], | ||
| 2057 | - reply.value[3], | ||
| 2058 | - ]); | ||
| 2059 | - Some(parent_window) | ||
| 2060 | - } else { | ||
| 2061 | - None | ||
| 2062 | - } | ||
| 2063 | - }) | ||
| 2064 | - } | ||
| 2065 | - | ||
| 2066 | - fn get_window_class_instance(&self, window: Window) -> (String, String) { | ||
| 2067 | - let reply = self | ||
| 2068 | - .connection | ||
| 2069 | - .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024) | ||
| 2070 | - .ok() | ||
| 2071 | - .and_then(|cookie| cookie.reply().ok()); | ||
| 2072 | - | ||
| 2073 | - if let Some(reply) = reply | ||
| 2074 | - && !reply.value.is_empty() | ||
| 2075 | - && let Ok(text) = std::str::from_utf8(&reply.value) | ||
| 2076 | - { | ||
| 2077 | - let parts: Vec<&str> = text.split('\0').collect(); | ||
| 2078 | - let instance = parts.first().unwrap_or(&"").to_string(); | ||
| 2079 | - let class = parts.get(1).unwrap_or(&"").to_string(); | ||
| 2080 | - return (instance, class); | ||
| 2081 | - } | ||
| 2082 | - | ||
| 2083 | - (String::new(), String::new()) | ||
| 2084 | - } | ||
| 2085 | - | ||
| 2086 | - fn apply_rules(&mut self, window: Window) -> WmResult<()> { | ||
| 2087 | - let (instance, class) = self.get_window_class_instance(window); | ||
| 2088 | - let title = self | ||
| 2089 | - .clients | ||
| 2090 | - .get(&window) | ||
| 2091 | - .map(|c| c.name.clone()) | ||
| 2092 | - .unwrap_or_default(); | ||
| 2093 | - | ||
| 2094 | - let mut rule_tags: Option<u32> = None; | ||
| 2095 | - let mut rule_floating: Option<bool> = None; | ||
| 2096 | - let mut rule_monitor: Option<usize> = None; | ||
| 2097 | - let mut rule_focus = false; | ||
| 2098 | - | ||
| 2099 | - for rule in &self.config.window_rules { | ||
| 2100 | - if rule.matches(&class, &instance, &title) { | ||
| 2101 | - if rule.tags.is_some() { | ||
| 2102 | - rule_tags = rule.tags; | ||
| 2103 | - } | ||
| 2104 | - if rule.is_floating.is_some() { | ||
| 2105 | - rule_floating = rule.is_floating; | ||
| 2106 | - } | ||
| 2107 | - if rule.monitor.is_some() { | ||
| 2108 | - rule_monitor = rule.monitor; | ||
| 2109 | - } | ||
| 2110 | - rule_focus = rule.focus.unwrap_or(false); | ||
| 2111 | - } | ||
| 2112 | - } | ||
| 2113 | - | ||
| 2114 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 2115 | - if let Some(is_floating) = rule_floating { | ||
| 2116 | - client.is_floating = is_floating; | ||
| 2117 | - if is_floating { | ||
| 2118 | - self.floating_windows.insert(window); | ||
| 2119 | - } else { | ||
| 2120 | - self.floating_windows.remove(&window); | ||
| 2121 | - } | ||
| 2122 | - } | ||
| 2123 | - | ||
| 2124 | - if let Some(monitor_index) = rule_monitor | ||
| 2125 | - && monitor_index < self.monitors.len() | ||
| 2126 | - { | ||
| 2127 | - client.monitor_index = monitor_index; | ||
| 2128 | - } | ||
| 2129 | - | ||
| 2130 | - if let Some(tags) = rule_tags { | ||
| 2131 | - client.tags = tags; | ||
| 2132 | - | ||
| 2133 | - if rule_focus { | ||
| 2134 | - let tag_index = unmask_tag(tags); | ||
| 2135 | - let monitor_tagset = self | ||
| 2136 | - .monitors | ||
| 2137 | - .get(client.monitor_index) | ||
| 2138 | - .map(|monitor| monitor.get_selected_tag()) | ||
| 2139 | - .unwrap_or(tag_mask(0)); | ||
| 2140 | - let is_tag_focused = monitor_tagset & tags == tags; | ||
| 2141 | - | ||
| 2142 | - if !is_tag_focused { | ||
| 2143 | - self.view_tag(tag_index)?; | ||
| 2144 | - } | ||
| 2145 | - } | ||
| 2146 | - } | ||
| 2147 | - } | ||
| 2148 | - | ||
| 2149 | - Ok(()) | ||
| 2150 | - } | ||
| 2151 | - | ||
| 2152 | - fn manage_window(&mut self, window: Window) -> WmResult<()> { | ||
| 2153 | - let geometry = self.connection.get_geometry(window)?.reply()?; | ||
| 2154 | - let border_width = self.config.border_width; | ||
| 2155 | - | ||
| 2156 | - let transient_parent = self.get_transient_parent(window); | ||
| 2157 | - let is_transient = transient_parent.is_some(); | ||
| 2158 | - | ||
| 2159 | - let (monitor_index, tags) = if let Some(parent) = transient_parent { | ||
| 2160 | - if let Some(parent_client) = self.clients.get(&parent) { | ||
| 2161 | - (parent_client.monitor_index, parent_client.tags) | ||
| 2162 | - } else { | ||
| 2163 | - let tags = self | ||
| 2164 | - .monitors | ||
| 2165 | - .get(self.selected_monitor) | ||
| 2166 | - .map(|monitor| monitor.tagset[monitor.selected_tags_index]) | ||
| 2167 | - .unwrap_or(tag_mask(0)); | ||
| 2168 | - (self.selected_monitor, tags) | ||
| 2169 | - } | ||
| 2170 | - } else { | ||
| 2171 | - let tags = self | ||
| 2172 | - .monitors | ||
| 2173 | - .get(self.selected_monitor) | ||
| 2174 | - .map(|monitor| monitor.tagset[monitor.selected_tags_index]) | ||
| 2175 | - .unwrap_or(tag_mask(0)); | ||
| 2176 | - (self.selected_monitor, tags) | ||
| 2177 | - }; | ||
| 2178 | - | ||
| 2179 | - let mut client = Client::new(window, monitor_index, tags); | ||
| 2180 | - client.x_position = geometry.x; | ||
| 2181 | - client.y_position = geometry.y; | ||
| 2182 | - client.width = geometry.width; | ||
| 2183 | - client.height = geometry.height; | ||
| 2184 | - client.old_x_position = geometry.x; | ||
| 2185 | - client.old_y_position = geometry.y; | ||
| 2186 | - client.old_width = geometry.width; | ||
| 2187 | - client.old_height = geometry.height; | ||
| 2188 | - client.old_border_width = geometry.border_width; | ||
| 2189 | - client.border_width = border_width as u16; | ||
| 2190 | - | ||
| 2191 | - self.clients.insert(window, client); | ||
| 2192 | - self.update_window_title(window)?; | ||
| 2193 | - | ||
| 2194 | - if !is_transient { | ||
| 2195 | - self.apply_rules(window)?; | ||
| 2196 | - } | ||
| 2197 | - | ||
| 2198 | - let client_monitor = self | ||
| 2199 | - .clients | ||
| 2200 | - .get(&window) | ||
| 2201 | - .map(|c| c.monitor_index) | ||
| 2202 | - .unwrap_or(monitor_index); | ||
| 2203 | - let monitor = &self.monitors[client_monitor]; | ||
| 2204 | - | ||
| 2205 | - let mut x = self | ||
| 2206 | - .clients | ||
| 2207 | - .get(&window) | ||
| 2208 | - .map(|c| c.x_position as i32) | ||
| 2209 | - .unwrap_or(0); | ||
| 2210 | - let mut y = self | ||
| 2211 | - .clients | ||
| 2212 | - .get(&window) | ||
| 2213 | - .map(|c| c.y_position as i32) | ||
| 2214 | - .unwrap_or(0); | ||
| 2215 | - let w = self | ||
| 2216 | - .clients | ||
| 2217 | - .get(&window) | ||
| 2218 | - .map(|c| c.width as i32) | ||
| 2219 | - .unwrap_or(1); | ||
| 2220 | - let h = self | ||
| 2221 | - .clients | ||
| 2222 | - .get(&window) | ||
| 2223 | - .map(|c| c.height as i32) | ||
| 2224 | - .unwrap_or(1); | ||
| 2225 | - let bw = border_width as i32; | ||
| 2226 | - | ||
| 2227 | - if x + w + 2 * bw > monitor.window_area_x + monitor.window_area_width { | ||
| 2228 | - x = monitor.window_area_x + monitor.window_area_width - w - 2 * bw; | ||
| 2229 | - } | ||
| 2230 | - if y + h + 2 * bw > monitor.window_area_y + monitor.window_area_height { | ||
| 2231 | - y = monitor.window_area_y + monitor.window_area_height - h - 2 * bw; | ||
| 2232 | - } | ||
| 2233 | - x = x.max(monitor.window_area_x); | ||
| 2234 | - y = y.max(monitor.window_area_y); | ||
| 2235 | - | ||
| 2236 | - if let Some(c) = self.clients.get_mut(&window) { | ||
| 2237 | - c.x_position = x as i16; | ||
| 2238 | - c.y_position = y as i16; | ||
| 2239 | - } | ||
| 2240 | - | ||
| 2241 | - self.connection.configure_window( | ||
| 2242 | - window, | ||
| 2243 | - &ConfigureWindowAux::new().border_width(border_width), | ||
| 2244 | - )?; | ||
| 2245 | - self.connection.change_window_attributes( | ||
| 2246 | - window, | ||
| 2247 | - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), | ||
| 2248 | - )?; | ||
| 2249 | - self.send_configure_notify(window)?; | ||
| 2250 | - self.update_window_type(window)?; | ||
| 2251 | - self.update_size_hints(window)?; | ||
| 2252 | - self.update_window_hints(window)?; | ||
| 2253 | - | ||
| 2254 | - self.connection.change_window_attributes( | ||
| 2255 | - window, | ||
| 2256 | - &ChangeWindowAttributesAux::new().event_mask( | ||
| 2257 | - EventMask::ENTER_WINDOW | ||
| 2258 | - | EventMask::FOCUS_CHANGE | ||
| 2259 | - | EventMask::PROPERTY_CHANGE | ||
| 2260 | - | EventMask::STRUCTURE_NOTIFY, | ||
| 2261 | - ), | ||
| 2262 | - )?; | ||
| 2263 | - | ||
| 2264 | - let is_fixed = self | ||
| 2265 | - .clients | ||
| 2266 | - .get(&window) | ||
| 2267 | - .map(|c| c.is_fixed) | ||
| 2268 | - .unwrap_or(false); | ||
| 2269 | - if let Some(c) = self.clients.get_mut(&window) | ||
| 2270 | - && !c.is_floating | ||
| 2271 | - { | ||
| 2272 | - c.is_floating = is_transient || is_fixed; | ||
| 2273 | - c.old_state = c.is_floating; | ||
| 2274 | - } | ||
| 2275 | - | ||
| 2276 | - if self | ||
| 2277 | - .clients | ||
| 2278 | - .get(&window) | ||
| 2279 | - .map(|c| c.is_floating) | ||
| 2280 | - .unwrap_or(false) | ||
| 2281 | - { | ||
| 2282 | - self.floating_windows.insert(window); | ||
| 2283 | - self.connection.configure_window( | ||
| 2284 | - window, | ||
| 2285 | - &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | ||
| 2286 | - )?; | ||
| 2287 | - } | ||
| 2288 | - | ||
| 2289 | - if self.layout.name() == "scrolling" { | ||
| 2290 | - if let Some(selected) = self | ||
| 2291 | - .monitors | ||
| 2292 | - .get(client_monitor) | ||
| 2293 | - .and_then(|m| m.selected_client) | ||
| 2294 | - { | ||
| 2295 | - self.attach_after(window, selected, client_monitor); | ||
| 2296 | - } else { | ||
| 2297 | - self.attach_aside(window, client_monitor); | ||
| 2298 | - } | ||
| 2299 | - } else { | ||
| 2300 | - self.attach_aside(window, client_monitor); | ||
| 2301 | - } | ||
| 2302 | - self.attach_stack(window, client_monitor); | ||
| 2303 | - self.windows.push(window); | ||
| 2304 | - | ||
| 2305 | - let off_screen_x = x + 2 * self.screen.width_in_pixels as i32; | ||
| 2306 | - self.connection.configure_window( | ||
| 2307 | - window, | ||
| 2308 | - &ConfigureWindowAux::new() | ||
| 2309 | - .x(off_screen_x) | ||
| 2310 | - .y(y) | ||
| 2311 | - .width(w as u32) | ||
| 2312 | - .height(h as u32), | ||
| 2313 | - )?; | ||
| 2314 | - | ||
| 2315 | - self.set_wm_state(window, 1)?; | ||
| 2316 | - self.update_client_list()?; | ||
| 2317 | - | ||
| 2318 | - let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(tags); | ||
| 2319 | - let _ = self.save_client_tag(window, final_tags); | ||
| 2320 | - | ||
| 2321 | - if client_monitor == self.selected_monitor | ||
| 2322 | - && let Some(old_sel) = self | ||
| 2323 | - .monitors | ||
| 2324 | - .get(self.selected_monitor) | ||
| 2325 | - .and_then(|m| m.selected_client) | ||
| 2326 | - { | ||
| 2327 | - self.unfocus(old_sel, false)?; | ||
| 2328 | - } | ||
| 2329 | - | ||
| 2330 | - if let Some(m) = self.monitors.get_mut(client_monitor) { | ||
| 2331 | - m.selected_client = Some(window); | ||
| 2332 | - } | ||
| 2333 | - | ||
| 2334 | - if self.layout.name() == "scrolling" { | ||
| 2335 | - self.scroll_to_window(window, true)?; | ||
| 2336 | - } | ||
| 2337 | - | ||
| 2338 | - self.apply_layout()?; | ||
| 2339 | - self.connection.map_window(window)?; | ||
| 2340 | - self.focus(None)?; | ||
| 2341 | - self.update_bar()?; | ||
| 2342 | - | ||
| 2343 | - if self.layout.name() == "tabbed" { | ||
| 2344 | - self.update_tab_bars()?; | ||
| 2345 | - } | ||
| 2346 | - | ||
| 2347 | - Ok(()) | ||
| 2348 | - } | ||
| 2349 | - | ||
| 2350 | - pub fn set_focus(&mut self, window: Window) -> WmResult<()> { | ||
| 2351 | - let never_focus = self | ||
| 2352 | - .clients | ||
| 2353 | - .get(&window) | ||
| 2354 | - .map(|c| c.never_focus) | ||
| 2355 | - .unwrap_or(false); | ||
| 2356 | - | ||
| 2357 | - if !never_focus { | ||
| 2358 | - self.connection.set_input_focus( | ||
| 2359 | - InputFocus::POINTER_ROOT, | ||
| 2360 | - window, | ||
| 2361 | - x11rb::CURRENT_TIME, | ||
| 2362 | - )?; | ||
| 2363 | - | ||
| 2364 | - self.connection.change_property( | ||
| 2365 | - PropMode::REPLACE, | ||
| 2366 | - self.root, | ||
| 2367 | - self.atoms.net_active_window, | ||
| 2368 | - AtomEnum::WINDOW, | ||
| 2369 | - 32, | ||
| 2370 | - 1, | ||
| 2371 | - &window.to_ne_bytes(), | ||
| 2372 | - )?; | ||
| 2373 | - } | ||
| 2374 | - | ||
| 2375 | - let _ = self.send_event(window, self.atoms.wm_take_focus); | ||
| 2376 | - self.connection.flush()?; | ||
| 2377 | - | ||
| 2378 | - Ok(()) | ||
| 2379 | - } | ||
| 2380 | - | ||
| 2381 | - fn grabbuttons(&self, window: Window, focused: bool) -> WmResult<()> { | ||
| 2382 | - self.connection | ||
| 2383 | - .ungrab_button(ButtonIndex::ANY, window, ModMask::ANY)?; | ||
| 2384 | - | ||
| 2385 | - if !focused { | ||
| 2386 | - self.connection.grab_button( | ||
| 2387 | - false, | ||
| 2388 | - window, | ||
| 2389 | - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | ||
| 2390 | - GrabMode::SYNC, | ||
| 2391 | - GrabMode::SYNC, | ||
| 2392 | - x11rb::NONE, | ||
| 2393 | - x11rb::NONE, | ||
| 2394 | - ButtonIndex::ANY, | ||
| 2395 | - ModMask::ANY, | ||
| 2396 | - )?; | ||
| 2397 | - } | ||
| 2398 | - | ||
| 2399 | - let ignore_modifiers = [ | ||
| 2400 | - 0u16, | ||
| 2401 | - u16::from(ModMask::LOCK), | ||
| 2402 | - u16::from(ModMask::M2), | ||
| 2403 | - u16::from(ModMask::LOCK | ModMask::M2), | ||
| 2404 | - ]; | ||
| 2405 | - | ||
| 2406 | - for &ignore_mask in &ignore_modifiers { | ||
| 2407 | - let grab_mask = u16::from(self.config.modkey) | ignore_mask; | ||
| 2408 | - | ||
| 2409 | - self.connection.grab_button( | ||
| 2410 | - false, | ||
| 2411 | - window, | ||
| 2412 | - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | ||
| 2413 | - GrabMode::ASYNC, | ||
| 2414 | - GrabMode::SYNC, | ||
| 2415 | - x11rb::NONE, | ||
| 2416 | - x11rb::NONE, | ||
| 2417 | - ButtonIndex::M1, | ||
| 2418 | - grab_mask.into(), | ||
| 2419 | - )?; | ||
| 2420 | - | ||
| 2421 | - self.connection.grab_button( | ||
| 2422 | - false, | ||
| 2423 | - window, | ||
| 2424 | - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | ||
| 2425 | - GrabMode::ASYNC, | ||
| 2426 | - GrabMode::SYNC, | ||
| 2427 | - x11rb::NONE, | ||
| 2428 | - x11rb::NONE, | ||
| 2429 | - ButtonIndex::M3, | ||
| 2430 | - grab_mask.into(), | ||
| 2431 | - )?; | ||
| 2432 | - } | ||
| 2433 | - | ||
| 2434 | - Ok(()) | ||
| 2435 | - } | ||
| 2436 | - | ||
| 2437 | - fn unfocus(&self, window: Window, reset_input_focus: bool) -> WmResult<()> { | ||
| 2438 | - if !self.windows.contains(&window) { | ||
| 2439 | - return Ok(()); | ||
| 2440 | - } | ||
| 2441 | - | ||
| 2442 | - self.grabbuttons(window, false)?; | ||
| 2443 | - | ||
| 2444 | - self.connection.change_window_attributes( | ||
| 2445 | - window, | ||
| 2446 | - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), | ||
| 2447 | - )?; | ||
| 2448 | - | ||
| 2449 | - if reset_input_focus { | ||
| 2450 | - self.connection.set_input_focus( | ||
| 2451 | - InputFocus::POINTER_ROOT, | ||
| 2452 | - self.root, | ||
| 2453 | - x11rb::CURRENT_TIME, | ||
| 2454 | - )?; | ||
| 2455 | - self.connection | ||
| 2456 | - .delete_property(self.root, self.atoms.net_active_window)?; | ||
| 2457 | - } | ||
| 2458 | - | ||
| 2459 | - Ok(()) | ||
| 2460 | - } | ||
| 2461 | - | ||
| 2462 | - fn focus(&mut self, window: Option<Window>) -> WmResult<()> { | ||
| 2463 | - let old_selected = self | ||
| 2464 | - .monitors | ||
| 2465 | - .get(self.selected_monitor) | ||
| 2466 | - .and_then(|m| m.selected_client); | ||
| 2467 | - | ||
| 2468 | - let mut focus_client = window; | ||
| 2469 | - if focus_client.is_none() || focus_client.is_some_and(|w| !self.is_visible(w)) { | ||
| 2470 | - let mut current = self | ||
| 2471 | - .monitors | ||
| 2472 | - .get(self.selected_monitor) | ||
| 2473 | - .and_then(|m| m.stack_head); | ||
| 2474 | - | ||
| 2475 | - focus_client = None; | ||
| 2476 | - while let Some(w) = current { | ||
| 2477 | - if self.is_visible(w) { | ||
| 2478 | - focus_client = Some(w); | ||
| 2479 | - break; | ||
| 2480 | - } | ||
| 2481 | - current = self.clients.get(&w).and_then(|c| c.stack_next); | ||
| 2482 | - } | ||
| 2483 | - } | ||
| 2484 | - | ||
| 2485 | - if old_selected != focus_client { | ||
| 2486 | - if let Some(old_win) = old_selected { | ||
| 2487 | - self.unfocus(old_win, false)?; | ||
| 2488 | - } | ||
| 2489 | - } | ||
| 2490 | - | ||
| 2491 | - if let Some(win) = focus_client { | ||
| 2492 | - let monitor_idx = self | ||
| 2493 | - .clients | ||
| 2494 | - .get(&win) | ||
| 2495 | - .map(|c| c.monitor_index) | ||
| 2496 | - .unwrap_or(self.selected_monitor); | ||
| 2497 | - | ||
| 2498 | - if monitor_idx != self.selected_monitor { | ||
| 2499 | - self.selected_monitor = monitor_idx; | ||
| 2500 | - } | ||
| 2501 | - | ||
| 2502 | - if self.clients.get(&win).is_some_and(|c| c.is_urgent) { | ||
| 2503 | - self.set_urgent(win, false)?; | ||
| 2504 | - } | ||
| 2505 | - | ||
| 2506 | - self.detach_stack(win); | ||
| 2507 | - self.attach_stack(win, monitor_idx); | ||
| 2508 | - | ||
| 2509 | - self.grabbuttons(win, true)?; | ||
| 2510 | - | ||
| 2511 | - self.connection.change_window_attributes( | ||
| 2512 | - win, | ||
| 2513 | - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), | ||
| 2514 | - )?; | ||
| 2515 | - | ||
| 2516 | - let never_focus = self | ||
| 2517 | - .clients | ||
| 2518 | - .get(&win) | ||
| 2519 | - .map(|client| client.never_focus) | ||
| 2520 | - .unwrap_or(false); | ||
| 2521 | - | ||
| 2522 | - if !never_focus { | ||
| 2523 | - self.connection.set_input_focus( | ||
| 2524 | - InputFocus::POINTER_ROOT, | ||
| 2525 | - win, | ||
| 2526 | - x11rb::CURRENT_TIME, | ||
| 2527 | - )?; | ||
| 2528 | - | ||
| 2529 | - self.connection.change_property( | ||
| 2530 | - PropMode::REPLACE, | ||
| 2531 | - self.root, | ||
| 2532 | - self.atoms.net_active_window, | ||
| 2533 | - AtomEnum::WINDOW, | ||
| 2534 | - 32, | ||
| 2535 | - 1, | ||
| 2536 | - &win.to_ne_bytes(), | ||
| 2537 | - )?; | ||
| 2538 | - } | ||
| 2539 | - | ||
| 2540 | - let _ = self.send_event(win, self.atoms.wm_take_focus); | ||
| 2541 | - | ||
| 2542 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 2543 | - monitor.selected_client = Some(win); | ||
| 2544 | - } | ||
| 2545 | - | ||
| 2546 | - self.previous_focused = Some(win); | ||
| 2547 | - } else { | ||
| 2548 | - self.connection.set_input_focus( | ||
| 2549 | - InputFocus::POINTER_ROOT, | ||
| 2550 | - self.root, | ||
| 2551 | - x11rb::CURRENT_TIME, | ||
| 2552 | - )?; | ||
| 2553 | - | ||
| 2554 | - self.connection | ||
| 2555 | - .delete_property(self.root, self.atoms.net_active_window)?; | ||
| 2556 | - | ||
| 2557 | - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 2558 | - monitor.selected_client = None; | ||
| 2559 | - } | ||
| 2560 | - } | ||
| 2561 | - | ||
| 2562 | - self.connection.flush()?; | ||
| 2563 | - | ||
| 2564 | - Ok(()) | ||
| 2565 | - } | ||
| 2566 | - | ||
| 2567 | - fn restack(&mut self) -> WmResult<()> { | ||
| 2568 | - let monitor = match self.monitors.get(self.selected_monitor) { | ||
| 2569 | - Some(m) => m, | ||
| 2570 | - None => return Ok(()), | ||
| 2571 | - }; | ||
| 2572 | - | ||
| 2573 | - let mut windows_to_restack: Vec<Window> = Vec::new(); | ||
| 2574 | - | ||
| 2575 | - if let Some(selected) = monitor.selected_client | ||
| 2576 | - && self.floating_windows.contains(&selected) | ||
| 2577 | - { | ||
| 2578 | - windows_to_restack.push(selected); | ||
| 2579 | - } | ||
| 2580 | - | ||
| 2581 | - let mut current = monitor.stack_head; | ||
| 2582 | - while let Some(win) = current { | ||
| 2583 | - if self.windows.contains(&win) | ||
| 2584 | - && self.floating_windows.contains(&win) | ||
| 2585 | - && Some(win) != monitor.selected_client | ||
| 2586 | - { | ||
| 2587 | - windows_to_restack.push(win); | ||
| 2588 | - } | ||
| 2589 | - current = self.clients.get(&win).and_then(|c| c.stack_next); | ||
| 2590 | - } | ||
| 2591 | - | ||
| 2592 | - current = monitor.stack_head; | ||
| 2593 | - while let Some(win) = current { | ||
| 2594 | - if self.windows.contains(&win) && !self.floating_windows.contains(&win) { | ||
| 2595 | - windows_to_restack.push(win); | ||
| 2596 | - } | ||
| 2597 | - current = self.clients.get(&win).and_then(|c| c.stack_next); | ||
| 2598 | - } | ||
| 2599 | - | ||
| 2600 | - for (i, &win) in windows_to_restack.iter().enumerate() { | ||
| 2601 | - if i == 0 { | ||
| 2602 | - self.connection.configure_window( | ||
| 2603 | - win, | ||
| 2604 | - &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | ||
| 2605 | - )?; | ||
| 2606 | - } else { | ||
| 2607 | - self.connection.configure_window( | ||
| 2608 | - win, | ||
| 2609 | - &ConfigureWindowAux::new() | ||
| 2610 | - .sibling(windows_to_restack[i - 1]) | ||
| 2611 | - .stack_mode(StackMode::BELOW), | ||
| 2612 | - )?; | ||
| 2613 | - } | ||
| 2614 | - } | ||
| 2615 | - | ||
| 2616 | - Ok(()) | ||
| 2617 | - } | ||
| 2618 | - | ||
| 2619 | - fn focusstack(&mut self, direction: i32) -> WmResult<()> { | ||
| 2620 | - let monitor = match self.monitors.get(self.selected_monitor) { | ||
| 2621 | - Some(monitor) => monitor, | ||
| 2622 | - None => return Ok(()), | ||
| 2623 | - }; | ||
| 2624 | - | ||
| 2625 | - let selected_window = match monitor.selected_client { | ||
| 2626 | - Some(window) => window, | ||
| 2627 | - None => return Ok(()), | ||
| 2628 | - }; | ||
| 2629 | - | ||
| 2630 | - let selected_is_fullscreen = self | ||
| 2631 | - .clients | ||
| 2632 | - .get(&selected_window) | ||
| 2633 | - .map(|client| client.is_fullscreen) | ||
| 2634 | - .unwrap_or(false); | ||
| 2635 | - | ||
| 2636 | - if selected_is_fullscreen { | ||
| 2637 | - return Ok(()); | ||
| 2638 | - } | ||
| 2639 | - | ||
| 2640 | - let selected_tags = monitor.tagset[monitor.selected_tags_index]; | ||
| 2641 | - | ||
| 2642 | - let mut stack_windows: Vec<Window> = Vec::new(); | ||
| 2643 | - let mut current_window = monitor.clients_head; | ||
| 2644 | - while let Some(window) = current_window { | ||
| 2645 | - if let Some(client) = self.clients.get(&window) { | ||
| 2646 | - if client.tags & selected_tags != 0 && !client.is_floating { | ||
| 2647 | - stack_windows.push(window); | ||
| 2648 | - } | ||
| 2649 | - current_window = client.next; | ||
| 2650 | - } else { | ||
| 2651 | - break; | ||
| 2652 | - } | ||
| 2653 | - } | ||
| 2654 | - | ||
| 2655 | - if stack_windows.is_empty() { | ||
| 2656 | - return Ok(()); | ||
| 2657 | - } | ||
| 2658 | - | ||
| 2659 | - let current_index = stack_windows | ||
| 2660 | - .iter() | ||
| 2661 | - .position(|&window| window == selected_window); | ||
| 2662 | - | ||
| 2663 | - let next_window = if let Some(index) = current_index { | ||
| 2664 | - if direction > 0 { | ||
| 2665 | - if index + 1 < stack_windows.len() { | ||
| 2666 | - stack_windows[index + 1] | ||
| 2667 | - } else { | ||
| 2668 | - stack_windows[0] | ||
| 2669 | - } | ||
| 2670 | - } else if index > 0 { | ||
| 2671 | - stack_windows[index - 1] | ||
| 2672 | - } else { | ||
| 2673 | - stack_windows[stack_windows.len() - 1] | ||
| 2674 | - } | ||
| 2675 | - } else { | ||
| 2676 | - return Ok(()); | ||
| 2677 | - }; | ||
| 2678 | - | ||
| 2679 | - self.focus(Some(next_window))?; | ||
| 2680 | - | ||
| 2681 | - if self.layout.name() == "scrolling" { | ||
| 2682 | - self.scroll_to_window(next_window, true)?; | ||
| 2683 | - } | ||
| 2684 | - | ||
| 2685 | - self.restack()?; | ||
| 2686 | - self.update_tab_bars()?; | ||
| 2687 | - | ||
| 2688 | - Ok(()) | ||
| 2689 | - } | ||
| 2690 | - | ||
| 2691 | - pub fn move_stack(&mut self, direction: i32) -> WmResult<()> { | ||
| 2692 | - let monitor_index = self.selected_monitor; | ||
| 2693 | - let monitor = match self.monitors.get(monitor_index) { | ||
| 2694 | - Some(m) => m.clone(), | ||
| 2695 | - None => return Ok(()), | ||
| 2696 | - }; | ||
| 2697 | - | ||
| 2698 | - let selected = match monitor.selected_client { | ||
| 2699 | - Some(win) => win, | ||
| 2700 | - None => return Ok(()), | ||
| 2701 | - }; | ||
| 2702 | - | ||
| 2703 | - let selected_client = match self.clients.get(&selected) { | ||
| 2704 | - Some(c) => c, | ||
| 2705 | - None => return Ok(()), | ||
| 2706 | - }; | ||
| 2707 | - | ||
| 2708 | - let target = if direction > 0 { | ||
| 2709 | - let next = self.next_tiled(selected_client.next, &monitor); | ||
| 2710 | - if next.is_some() { | ||
| 2711 | - next | ||
| 2712 | - } else { | ||
| 2713 | - self.next_tiled(monitor.clients_head, &monitor) | ||
| 2714 | - } | ||
| 2715 | - } else { | ||
| 2716 | - let mut previous = None; | ||
| 2717 | - let mut current = monitor.clients_head; | ||
| 2718 | - while let Some(window) = current { | ||
| 2719 | - if window == selected { | ||
| 2720 | - break; | ||
| 2721 | - } | ||
| 2722 | - if let Some(client) = self.clients.get(&window) { | ||
| 2723 | - let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; | ||
| 2724 | - if visible_tags != 0 && !client.is_floating { | ||
| 2725 | - previous = Some(window); | ||
| 2726 | - } | ||
| 2727 | - current = client.next; | ||
| 2728 | - } else { | ||
| 2729 | - break; | ||
| 2730 | - } | ||
| 2731 | - } | ||
| 2732 | - if previous.is_none() { | ||
| 2733 | - let mut last = None; | ||
| 2734 | - let mut current = monitor.clients_head; | ||
| 2735 | - while let Some(window) = current { | ||
| 2736 | - if let Some(client) = self.clients.get(&window) { | ||
| 2737 | - let visible_tags = | ||
| 2738 | - client.tags & monitor.tagset[monitor.selected_tags_index]; | ||
| 2739 | - if visible_tags != 0 && !client.is_floating { | ||
| 2740 | - last = Some(window); | ||
| 2741 | - } | ||
| 2742 | - current = client.next; | ||
| 2743 | - } else { | ||
| 2744 | - break; | ||
| 2745 | - } | ||
| 2746 | - } | ||
| 2747 | - last | ||
| 2748 | - } else { | ||
| 2749 | - previous | ||
| 2750 | - } | ||
| 2751 | - }; | ||
| 2752 | - | ||
| 2753 | - let target = match target { | ||
| 2754 | - Some(t) if t != selected => t, | ||
| 2755 | - _ => return Ok(()), | ||
| 2756 | - }; | ||
| 2757 | - | ||
| 2758 | - let mut prev_selected = None; | ||
| 2759 | - let mut prev_target = None; | ||
| 2760 | - let mut current = monitor.clients_head; | ||
| 2761 | - | ||
| 2762 | - while let Some(window) = current { | ||
| 2763 | - if let Some(client) = self.clients.get(&window) { | ||
| 2764 | - if client.next == Some(selected) { | ||
| 2765 | - prev_selected = Some(window); | ||
| 2766 | - } | ||
| 2767 | - if client.next == Some(target) { | ||
| 2768 | - prev_target = Some(window); | ||
| 2769 | - } | ||
| 2770 | - current = client.next; | ||
| 2771 | - } else { | ||
| 2772 | - break; | ||
| 2773 | - } | ||
| 2774 | - } | ||
| 2775 | - | ||
| 2776 | - let selected_next = self.clients.get(&selected).and_then(|c| c.next); | ||
| 2777 | - let target_next = self.clients.get(&target).and_then(|c| c.next); | ||
| 2778 | - | ||
| 2779 | - let temp = if selected_next == Some(target) { | ||
| 2780 | - Some(selected) | ||
| 2781 | - } else { | ||
| 2782 | - selected_next | ||
| 2783 | - }; | ||
| 2784 | - | ||
| 2785 | - if let Some(client) = self.clients.get_mut(&selected) { | ||
| 2786 | - client.next = if target_next == Some(selected) { | ||
| 2787 | - Some(target) | ||
| 2788 | - } else { | ||
| 2789 | - target_next | ||
| 2790 | - }; | ||
| 2791 | - } | ||
| 2792 | - | ||
| 2793 | - if let Some(client) = self.clients.get_mut(&target) { | ||
| 2794 | - client.next = temp; | ||
| 2795 | - } | ||
| 2796 | - | ||
| 2797 | - if let Some(prev) = prev_selected | ||
| 2798 | - && prev != target | ||
| 2799 | - && let Some(client) = self.clients.get_mut(&prev) | ||
| 2800 | - { | ||
| 2801 | - client.next = Some(target); | ||
| 2802 | - } | ||
| 2803 | - | ||
| 2804 | - if let Some(prev) = prev_target | ||
| 2805 | - && prev != selected | ||
| 2806 | - && let Some(client) = self.clients.get_mut(&prev) | ||
| 2807 | - { | ||
| 2808 | - client.next = Some(selected); | ||
| 2809 | - } | ||
| 2810 | - | ||
| 2811 | - if let Some(monitor) = self.monitors.get_mut(monitor_index) { | ||
| 2812 | - if monitor.clients_head == Some(selected) { | ||
| 2813 | - monitor.clients_head = Some(target); | ||
| 2814 | - } else if monitor.clients_head == Some(target) { | ||
| 2815 | - monitor.clients_head = Some(selected); | ||
| 2816 | - } | ||
| 2817 | - } | ||
| 2818 | - | ||
| 2819 | - self.apply_layout()?; | ||
| 2820 | - Ok(()) | ||
| 2821 | - } | ||
| 2822 | - | ||
| 2823 | - pub fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { | ||
| 2824 | - if self.monitors.len() <= 1 { | ||
| 2825 | - return Ok(()); | ||
| 2826 | - } | ||
| 2827 | - | ||
| 2828 | - let target_monitor = match self.get_adjacent_monitor(direction) { | ||
| 2829 | - Some(idx) if idx != self.selected_monitor => idx, | ||
| 2830 | - _ => return Ok(()), | ||
| 2831 | - }; | ||
| 2832 | - | ||
| 2833 | - let old_selected = self | ||
| 2834 | - .monitors | ||
| 2835 | - .get(self.selected_monitor) | ||
| 2836 | - .and_then(|m| m.selected_client); | ||
| 2837 | - | ||
| 2838 | - if let Some(win) = old_selected { | ||
| 2839 | - self.unfocus(win, true)?; | ||
| 2840 | - } | ||
| 2841 | - | ||
| 2842 | - self.selected_monitor = target_monitor; | ||
| 2843 | - self.focus(None)?; | ||
| 2844 | - | ||
| 2845 | - Ok(()) | ||
| 2846 | - } | ||
| 2847 | - | ||
| 2848 | - pub fn send_window_to_adjacent_monitor(&mut self, direction: i32) -> WmResult<()> { | ||
| 2849 | - if self.monitors.len() <= 1 { | ||
| 2850 | - return Ok(()); | ||
| 2851 | - } | ||
| 2852 | - | ||
| 2853 | - let selected_window = self | ||
| 2854 | - .monitors | ||
| 2855 | - .get(self.selected_monitor) | ||
| 2856 | - .and_then(|m| m.selected_client); | ||
| 2857 | - | ||
| 2858 | - let window = match selected_window { | ||
| 2859 | - Some(win) => win, | ||
| 2860 | - None => return Ok(()), | ||
| 2861 | - }; | ||
| 2862 | - | ||
| 2863 | - let target_monitor = match self.get_adjacent_monitor(direction) { | ||
| 2864 | - Some(idx) => idx, | ||
| 2865 | - None => return Ok(()), | ||
| 2866 | - }; | ||
| 2867 | - | ||
| 2868 | - self.move_window_to_monitor(window, target_monitor)?; | ||
| 2869 | - | ||
| 2870 | - Ok(()) | ||
| 2871 | - } | ||
| 2872 | - | ||
| 2873 | - fn drag_window(&mut self, window: Window) -> WmResult<()> { | ||
| 2874 | - let is_fullscreen = self | ||
| 2875 | - .clients | ||
| 2876 | - .get(&window) | ||
| 2877 | - .map(|c| c.is_fullscreen) | ||
| 2878 | - .unwrap_or(false); | ||
| 2879 | - | ||
| 2880 | - if is_fullscreen { | ||
| 2881 | - return Ok(()); | ||
| 2882 | - } | ||
| 2883 | - | ||
| 2884 | - let client_info = self.clients.get(&window).map(|c| { | ||
| 2885 | - ( | ||
| 2886 | - c.x_position, | ||
| 2887 | - c.y_position, | ||
| 2888 | - c.width, | ||
| 2889 | - c.height, | ||
| 2890 | - c.is_floating, | ||
| 2891 | - c.monitor_index, | ||
| 2892 | - ) | ||
| 2893 | - }); | ||
| 2894 | - | ||
| 2895 | - let Some((orig_x, orig_y, width, height, was_floating, monitor_idx)) = client_info else { | ||
| 2896 | - return Ok(()); | ||
| 2897 | - }; | ||
| 2898 | - | ||
| 2899 | - let monitor = self.monitors.get(monitor_idx).cloned(); | ||
| 2900 | - let Some(monitor) = monitor else { | ||
| 2901 | - return Ok(()); | ||
| 2902 | - }; | ||
| 2903 | - | ||
| 2904 | - let snap = 32; | ||
| 2905 | - let is_normie = self.layout.name() == "normie"; | ||
| 2906 | - | ||
| 2907 | - if !was_floating && !is_normie { | ||
| 2908 | - self.toggle_floating()?; | ||
| 2909 | - } | ||
| 2910 | - | ||
| 2911 | - self.connection | ||
| 2912 | - .grab_pointer( | ||
| 2913 | - false, | ||
| 2914 | - self.root, | ||
| 2915 | - EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE | EventMask::BUTTON_PRESS, | ||
| 2916 | - GrabMode::ASYNC, | ||
| 2917 | - GrabMode::ASYNC, | ||
| 2918 | - x11rb::NONE, | ||
| 2919 | - x11rb::NONE, | ||
| 2920 | - x11rb::CURRENT_TIME, | ||
| 2921 | - )? | ||
| 2922 | - .reply()?; | ||
| 2923 | - | ||
| 2924 | - let pointer = self.connection.query_pointer(self.root)?.reply()?; | ||
| 2925 | - let (start_x, start_y) = (pointer.root_x as i32, pointer.root_y as i32); | ||
| 2926 | - | ||
| 2927 | - let mut last_time = 0u32; | ||
| 2928 | - | ||
| 2929 | - loop { | ||
| 2930 | - let event = self.connection.wait_for_event()?; | ||
| 2931 | - match event { | ||
| 2932 | - Event::ConfigureRequest(_) | Event::MapRequest(_) | Event::Expose(_) => {} | ||
| 2933 | - Event::MotionNotify(e) => { | ||
| 2934 | - if e.time.wrapping_sub(last_time) <= 16 { | ||
| 2935 | - continue; | ||
| 2936 | - } | ||
| 2937 | - last_time = e.time; | ||
| 2938 | - | ||
| 2939 | - let mut new_x = orig_x as i32 + (e.root_x as i32 - start_x); | ||
| 2940 | - let mut new_y = orig_y as i32 + (e.root_y as i32 - start_y); | ||
| 2941 | - | ||
| 2942 | - if (monitor.window_area_x - new_x).abs() < snap { | ||
| 2943 | - new_x = monitor.window_area_x; | ||
| 2944 | - } else if ((monitor.window_area_x + monitor.window_area_width) | ||
| 2945 | - - (new_x + width as i32)) | ||
| 2946 | - .abs() | ||
| 2947 | - < snap | ||
| 2948 | - { | ||
| 2949 | - new_x = monitor.window_area_x + monitor.window_area_width - width as i32; | ||
| 2950 | - } | ||
| 2951 | - | ||
| 2952 | - if (monitor.window_area_y - new_y).abs() < snap { | ||
| 2953 | - new_y = monitor.window_area_y; | ||
| 2954 | - } else if ((monitor.window_area_y + monitor.window_area_height) | ||
| 2955 | - - (new_y + height as i32)) | ||
| 2956 | - .abs() | ||
| 2957 | - < snap | ||
| 2958 | - { | ||
| 2959 | - new_y = monitor.window_area_y + monitor.window_area_height - height as i32; | ||
| 2960 | - } | ||
| 2961 | - | ||
| 2962 | - let should_resize = is_normie | ||
| 2963 | - || self | ||
| 2964 | - .clients | ||
| 2965 | - .get(&window) | ||
| 2966 | - .map(|c| c.is_floating) | ||
| 2967 | - .unwrap_or(false); | ||
| 2968 | - | ||
| 2969 | - if should_resize { | ||
| 2970 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 2971 | - client.x_position = new_x as i16; | ||
| 2972 | - client.y_position = new_y as i16; | ||
| 2973 | - } | ||
| 2974 | - | ||
| 2975 | - self.connection.configure_window( | ||
| 2976 | - window, | ||
| 2977 | - &ConfigureWindowAux::new().x(new_x).y(new_y), | ||
| 2978 | - )?; | ||
| 2979 | - self.connection.flush()?; | ||
| 2980 | - } | ||
| 2981 | - } | ||
| 2982 | - Event::ButtonRelease(_) => break, | ||
| 2983 | - _ => {} | ||
| 2984 | - } | ||
| 2985 | - } | ||
| 2986 | - | ||
| 2987 | - self.connection | ||
| 2988 | - .ungrab_pointer(x11rb::CURRENT_TIME)? | ||
| 2989 | - .check()?; | ||
| 2990 | - | ||
| 2991 | - let final_client = self | ||
| 2992 | - .clients | ||
| 2993 | - .get(&window) | ||
| 2994 | - .map(|c| (c.x_position, c.y_position, c.width, c.height)); | ||
| 2995 | - | ||
| 2996 | - if let Some((x, y, w, h)) = final_client { | ||
| 2997 | - let new_monitor = self.get_monitor_for_rect(x as i32, y as i32, w as i32, h as i32); | ||
| 2998 | - if new_monitor != monitor_idx { | ||
| 2999 | - self.move_window_to_monitor(window, new_monitor)?; | ||
| 3000 | - self.selected_monitor = new_monitor; | ||
| 3001 | - self.focus(None)?; | ||
| 3002 | - } | ||
| 3003 | - } | ||
| 3004 | - | ||
| 3005 | - if self.config.auto_tile && !was_floating && !is_normie { | ||
| 3006 | - let drop_monitor_idx = self | ||
| 3007 | - .clients | ||
| 3008 | - .get(&window) | ||
| 3009 | - .map(|c| c.monitor_index) | ||
| 3010 | - .unwrap_or(monitor_idx); | ||
| 3011 | - | ||
| 3012 | - if let Some((x, y, w, h)) = final_client { | ||
| 3013 | - let center = (x as i32 + w as i32 / 2, y as i32 + h as i32 / 2); | ||
| 3014 | - if let Some(target) = self.tiled_window_at(window, drop_monitor_idx, center) { | ||
| 3015 | - self.detach(window); | ||
| 3016 | - self.insert_before(window, target, drop_monitor_idx); | ||
| 3017 | - } | ||
| 3018 | - } | ||
| 3019 | - | ||
| 3020 | - self.floating_windows.remove(&window); | ||
| 3021 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 3022 | - client.is_floating = false; | ||
| 3023 | - } | ||
| 3024 | - self.apply_layout()?; | ||
| 3025 | - } | ||
| 3026 | - | ||
| 3027 | - Ok(()) | ||
| 3028 | - } | ||
| 3029 | - | ||
| 3030 | - fn tiled_window_at( | ||
| 3031 | - &self, | ||
| 3032 | - exclude: Window, | ||
| 3033 | - monitor_idx: usize, | ||
| 3034 | - (px, py): (i32, i32), | ||
| 3035 | - ) -> Option<Window> { | ||
| 3036 | - let monitor = self.monitors.get(monitor_idx)?; | ||
| 3037 | - let tags = monitor.tagset[monitor.selected_tags_index]; | ||
| 3038 | - let mut current = monitor.clients_head; | ||
| 3039 | - | ||
| 3040 | - while let Some(win) = current { | ||
| 3041 | - let c = self.clients.get(&win)?; | ||
| 3042 | - current = c.next; | ||
| 3043 | - | ||
| 3044 | - if win == exclude || c.is_floating || (c.tags & tags) == 0 { | ||
| 3045 | - continue; | ||
| 3046 | - } | ||
| 3047 | - | ||
| 3048 | - let (x, y) = (c.x_position as i32, c.y_position as i32); | ||
| 3049 | - let (w, h) = ( | ||
| 3050 | - c.width as i32 + c.border_width as i32 * 2, | ||
| 3051 | - c.height as i32 + c.border_width as i32 * 2, | ||
| 3052 | - ); | ||
| 3053 | - | ||
| 3054 | - if px >= x && px < x + w && py >= y && py < y + h { | ||
| 3055 | - return Some(win); | ||
| 3056 | - } | ||
| 3057 | - } | ||
| 3058 | - None | ||
| 3059 | - } | ||
| 3060 | - | ||
| 3061 | - fn insert_before(&mut self, window: Window, target: Window, monitor_idx: usize) { | ||
| 3062 | - let Some(monitor) = self.monitors.get_mut(monitor_idx) else { | ||
| 3063 | - return; | ||
| 3064 | - }; | ||
| 3065 | - | ||
| 3066 | - if monitor.clients_head == Some(target) { | ||
| 3067 | - if let Some(c) = self.clients.get_mut(&window) { | ||
| 3068 | - c.next = Some(target); | ||
| 3069 | - } | ||
| 3070 | - monitor.clients_head = Some(window); | ||
| 3071 | - return; | ||
| 3072 | - } | ||
| 3073 | - | ||
| 3074 | - let mut current = monitor.clients_head; | ||
| 3075 | - while let Some(w) = current { | ||
| 3076 | - let Some(c) = self.clients.get(&w) else { break }; | ||
| 3077 | - if c.next != Some(target) { | ||
| 3078 | - current = c.next; | ||
| 3079 | - continue; | ||
| 3080 | - } | ||
| 3081 | - if let Some(prev) = self.clients.get_mut(&w) { | ||
| 3082 | - prev.next = Some(window); | ||
| 3083 | - } | ||
| 3084 | - if let Some(inserted) = self.clients.get_mut(&window) { | ||
| 3085 | - inserted.next = Some(target); | ||
| 3086 | - } | ||
| 3087 | - break; | ||
| 3088 | - } | ||
| 3089 | - } | ||
| 3090 | - | ||
| 3091 | - fn resize_window_with_mouse(&mut self, window: Window) -> WmResult<()> { | ||
| 3092 | - let is_fullscreen = self | ||
| 3093 | - .clients | ||
| 3094 | - .get(&window) | ||
| 3095 | - .map(|c| c.is_fullscreen) | ||
| 3096 | - .unwrap_or(false); | ||
| 3097 | - | ||
| 3098 | - if is_fullscreen { | ||
| 3099 | - return Ok(()); | ||
| 3100 | - } | ||
| 3101 | - | ||
| 3102 | - let client_info = self.clients.get(&window).map(|c| { | ||
| 3103 | - ( | ||
| 3104 | - c.x_position, | ||
| 3105 | - c.y_position, | ||
| 3106 | - c.width, | ||
| 3107 | - c.height, | ||
| 3108 | - c.border_width, | ||
| 3109 | - c.is_floating, | ||
| 3110 | - c.monitor_index, | ||
| 3111 | - ) | ||
| 3112 | - }); | ||
| 3113 | - | ||
| 3114 | - let Some(( | ||
| 3115 | - orig_x, | ||
| 3116 | - orig_y, | ||
| 3117 | - orig_width, | ||
| 3118 | - orig_height, | ||
| 3119 | - border_width, | ||
| 3120 | - was_floating, | ||
| 3121 | - monitor_idx, | ||
| 3122 | - )) = client_info | ||
| 3123 | - else { | ||
| 3124 | - return Ok(()); | ||
| 3125 | - }; | ||
| 3126 | - | ||
| 3127 | - let monitor = match self.monitors.get(monitor_idx) { | ||
| 3128 | - Some(m) => m, | ||
| 3129 | - None => return Ok(()), | ||
| 3130 | - }; | ||
| 3131 | - | ||
| 3132 | - let is_normie = self.layout.name() == "normie"; | ||
| 3133 | - | ||
| 3134 | - if self.config.auto_tile && !was_floating && !is_normie { | ||
| 3135 | - let mut tiled_count = 0; | ||
| 3136 | - let mut current = monitor.clients_head; | ||
| 3137 | - while let Some(w) = current { | ||
| 3138 | - if let Some(c) = self.clients.get(&w) { | ||
| 3139 | - let visible = (c.tags & monitor.tagset[monitor.selected_tags_index]) != 0; | ||
| 3140 | - if visible && !c.is_floating { | ||
| 3141 | - tiled_count += 1; | ||
| 3142 | - } | ||
| 3143 | - current = c.next; | ||
| 3144 | - } else { | ||
| 3145 | - break; | ||
| 3146 | - } | ||
| 3147 | - } | ||
| 3148 | - if tiled_count <= 1 { | ||
| 3149 | - return Ok(()); | ||
| 3150 | - } | ||
| 3151 | - } | ||
| 3152 | - | ||
| 3153 | - if !was_floating && !is_normie { | ||
| 3154 | - self.toggle_floating()?; | ||
| 3155 | - } | ||
| 3156 | - | ||
| 3157 | - self.connection.warp_pointer( | ||
| 3158 | - x11rb::NONE, | ||
| 3159 | - window, | ||
| 3160 | - 0, | ||
| 3161 | - 0, | ||
| 3162 | - 0, | ||
| 3163 | - 0, | ||
| 3164 | - (orig_width + border_width - 1) as i16, | ||
| 3165 | - (orig_height + border_width - 1) as i16, | ||
| 3166 | - )?; | ||
| 3167 | - | ||
| 3168 | - self.connection | ||
| 3169 | - .grab_pointer( | ||
| 3170 | - false, | ||
| 3171 | - self.root, | ||
| 3172 | - EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE | EventMask::BUTTON_PRESS, | ||
| 3173 | - GrabMode::ASYNC, | ||
| 3174 | - GrabMode::ASYNC, | ||
| 3175 | - x11rb::NONE, | ||
| 3176 | - x11rb::NONE, | ||
| 3177 | - x11rb::CURRENT_TIME, | ||
| 3178 | - )? | ||
| 3179 | - .reply()?; | ||
| 3180 | - | ||
| 3181 | - let mut last_time = 0u32; | ||
| 3182 | - | ||
| 3183 | - loop { | ||
| 3184 | - let event = self.connection.wait_for_event()?; | ||
| 3185 | - match event { | ||
| 3186 | - Event::ConfigureRequest(_) | Event::MapRequest(_) | Event::Expose(_) => {} | ||
| 3187 | - Event::MotionNotify(e) => { | ||
| 3188 | - if e.time.wrapping_sub(last_time) <= 16 { | ||
| 3189 | - continue; | ||
| 3190 | - } | ||
| 3191 | - last_time = e.time; | ||
| 3192 | - | ||
| 3193 | - let new_width = ((e.root_x as i32 - orig_x as i32 - 2 * border_width as i32 | ||
| 3194 | - + 1) | ||
| 3195 | - .max(1)) as u32; | ||
| 3196 | - let new_height = ((e.root_y as i32 - orig_y as i32 - 2 * border_width as i32 | ||
| 3197 | - + 1) | ||
| 3198 | - .max(1)) as u32; | ||
| 3199 | - | ||
| 3200 | - let should_resize = is_normie | ||
| 3201 | - || self | ||
| 3202 | - .clients | ||
| 3203 | - .get(&window) | ||
| 3204 | - .map(|c| c.is_floating) | ||
| 3205 | - .unwrap_or(false); | ||
| 3206 | - | ||
| 3207 | - if should_resize && let Some(client) = self.clients.get(&window).cloned() { | ||
| 3208 | - let (_, _, hint_width, hint_height, _) = self.apply_size_hints( | ||
| 3209 | - window, | ||
| 3210 | - client.x_position as i32, | ||
| 3211 | - client.y_position as i32, | ||
| 3212 | - new_width as i32, | ||
| 3213 | - new_height as i32, | ||
| 3214 | - ); | ||
| 3215 | - | ||
| 3216 | - if let Some(client_mut) = self.clients.get_mut(&window) { | ||
| 3217 | - client_mut.width = hint_width as u16; | ||
| 3218 | - client_mut.height = hint_height as u16; | ||
| 3219 | - } | ||
| 3220 | - | ||
| 3221 | - self.connection.configure_window( | ||
| 3222 | - window, | ||
| 3223 | - &ConfigureWindowAux::new() | ||
| 3224 | - .width(hint_width as u32) | ||
| 3225 | - .height(hint_height as u32), | ||
| 3226 | - )?; | ||
| 3227 | - self.connection.flush()?; | ||
| 3228 | - } | ||
| 3229 | - } | ||
| 3230 | - Event::ButtonRelease(_) => break, | ||
| 3231 | - _ => {} | ||
| 3232 | - } | ||
| 3233 | - } | ||
| 3234 | - | ||
| 3235 | - let final_client = self.clients.get(&window).map(|c| (c.width, c.border_width)); | ||
| 3236 | - | ||
| 3237 | - if let Some((w, bw)) = final_client { | ||
| 3238 | - self.connection.warp_pointer( | ||
| 3239 | - x11rb::NONE, | ||
| 3240 | - window, | ||
| 3241 | - 0, | ||
| 3242 | - 0, | ||
| 3243 | - 0, | ||
| 3244 | - 0, | ||
| 3245 | - (w + bw - 1) as i16, | ||
| 3246 | - (w + bw - 1) as i16, | ||
| 3247 | - )?; | ||
| 3248 | - } | ||
| 3249 | - | ||
| 3250 | - self.connection | ||
| 3251 | - .ungrab_pointer(x11rb::CURRENT_TIME)? | ||
| 3252 | - .check()?; | ||
| 3253 | - | ||
| 3254 | - let final_client_pos = self | ||
| 3255 | - .clients | ||
| 3256 | - .get(&window) | ||
| 3257 | - .map(|c| (c.x_position, c.y_position, c.width, c.height)); | ||
| 3258 | - | ||
| 3259 | - if let Some((x, y, w, h)) = final_client_pos { | ||
| 3260 | - let new_monitor = self.get_monitor_for_rect(x as i32, y as i32, w as i32, h as i32); | ||
| 3261 | - if new_monitor != monitor_idx { | ||
| 3262 | - self.move_window_to_monitor(window, new_monitor)?; | ||
| 3263 | - self.selected_monitor = new_monitor; | ||
| 3264 | - self.focus(None)?; | ||
| 3265 | - } | ||
| 3266 | - } | ||
| 3267 | - | ||
| 3268 | - if self.config.auto_tile && !was_floating && !is_normie { | ||
| 3269 | - self.floating_windows.remove(&window); | ||
| 3270 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 3271 | - client.is_floating = false; | ||
| 3272 | - } | ||
| 3273 | - self.apply_layout()?; | ||
| 3274 | - } | ||
| 3275 | - | ||
| 3276 | - Ok(()) | ||
| 3277 | - } | ||
| 3278 | - | ||
| 3279 | - fn handle_event(&mut self, event: Event) -> WmResult<Control> { | ||
| 3280 | - match event { | ||
| 3281 | - Event::KeyPress(ref key_event) if key_event.event == self.overlay.window() => { | ||
| 3282 | - if self.overlay.is_visible() | ||
| 3283 | - && let Err(error) = self.overlay.hide(&self.connection) | ||
| 3284 | - { | ||
| 3285 | - eprintln!("Failed to hide overlay: {:?}", error); | ||
| 3286 | - } | ||
| 3287 | - return Ok(Control::Continue); | ||
| 3288 | - } | ||
| 3289 | - Event::ButtonPress(ref button_event) if button_event.event == self.overlay.window() => { | ||
| 3290 | - if self.overlay.is_visible() | ||
| 3291 | - && let Err(error) = self.overlay.hide(&self.connection) | ||
| 3292 | - { | ||
| 3293 | - eprintln!("Failed to hide overlay: {:?}", error); | ||
| 3294 | - } | ||
| 3295 | - return Ok(Control::Continue); | ||
| 3296 | - } | ||
| 3297 | - Event::Expose(ref expose_event) if expose_event.window == self.overlay.window() => { | ||
| 3298 | - if self.overlay.is_visible() | ||
| 3299 | - && let Err(error) = self.overlay.draw(&self.connection, &self.font) | ||
| 3300 | - { | ||
| 3301 | - eprintln!("Failed to draw overlay: {:?}", error); | ||
| 3302 | - } | ||
| 3303 | - return Ok(Control::Continue); | ||
| 3304 | - } | ||
| 3305 | - Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => { | ||
| 3306 | - if self.keybind_overlay.is_visible() | ||
| 3307 | - && !self.keybind_overlay.should_suppress_input() | ||
| 3308 | - { | ||
| 3309 | - use crate::keyboard::keysyms; | ||
| 3310 | - if let Some(mapping) = &self.keyboard_mapping { | ||
| 3311 | - let keysym = mapping.keycode_to_keysym(e.detail); | ||
| 3312 | - let is_escape = keysym == keysyms::XK_ESCAPE; | ||
| 3313 | - let is_q = keysym == keysyms::XK_Q || keysym == 0x0051; | ||
| 3314 | - if (is_escape || is_q) | ||
| 3315 | - && let Err(error) = self.keybind_overlay.hide(&self.connection) | ||
| 3316 | - { | ||
| 3317 | - eprintln!("Failed to hide keybind overlay: {:?}", error); | ||
| 3318 | - } | ||
| 3319 | - } | ||
| 3320 | - } | ||
| 3321 | - return Ok(Control::Continue); | ||
| 3322 | - } | ||
| 3323 | - Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => { | ||
| 3324 | - self.connection | ||
| 3325 | - .allow_events(Allow::REPLAY_POINTER, e.time)?; | ||
| 3326 | - return Ok(Control::Continue); | ||
| 3327 | - } | ||
| 3328 | - Event::Expose(ref expose_event) | ||
| 3329 | - if expose_event.window == self.keybind_overlay.window() => | ||
| 3330 | - { | ||
| 3331 | - if self.keybind_overlay.is_visible() | ||
| 3332 | - && let Err(error) = self.keybind_overlay.draw(&self.connection, &self.font) | ||
| 3333 | - { | ||
| 3334 | - eprintln!("Failed to draw keybind overlay: {:?}", error); | ||
| 3335 | - } | ||
| 3336 | - return Ok(Control::Continue); | ||
| 3337 | - } | ||
| 3338 | - Event::MapRequest(event) => { | ||
| 3339 | - let attrs = match self.connection.get_window_attributes(event.window)?.reply() { | ||
| 3340 | - Ok(attrs) => attrs, | ||
| 3341 | - Err(_) => return Ok(Control::Continue), | ||
| 3342 | - }; | ||
| 3343 | - | ||
| 3344 | - if attrs.override_redirect { | ||
| 3345 | - return Ok(Control::Continue); | ||
| 3346 | - } | ||
| 3347 | - | ||
| 3348 | - if !self.windows.contains(&event.window) { | ||
| 3349 | - self.manage_window(event.window)?; | ||
| 3350 | - } | ||
| 3351 | - } | ||
| 3352 | - Event::UnmapNotify(event) => { | ||
| 3353 | - if self.windows.contains(&event.window) && self.is_window_visible(event.window) { | ||
| 3354 | - self.remove_window(event.window, false)?; | ||
| 3355 | - } | ||
| 3356 | - } | ||
| 3357 | - Event::DestroyNotify(event) => { | ||
| 3358 | - if self.windows.contains(&event.window) { | ||
| 3359 | - self.remove_window(event.window, true)?; | ||
| 3360 | - } | ||
| 3361 | - } | ||
| 3362 | - Event::PropertyNotify(event) => { | ||
| 3363 | - if event.state == Property::DELETE { | ||
| 3364 | - return Ok(Control::Continue); | ||
| 3365 | - } | ||
| 3366 | - | ||
| 3367 | - if !self.clients.contains_key(&event.window) { | ||
| 3368 | - return Ok(Control::Continue); | ||
| 3369 | - } | ||
| 3370 | - | ||
| 3371 | - if event.atom == AtomEnum::WM_TRANSIENT_FOR.into() { | ||
| 3372 | - let is_floating = self | ||
| 3373 | - .clients | ||
| 3374 | - .get(&event.window) | ||
| 3375 | - .map(|c| c.is_floating) | ||
| 3376 | - .unwrap_or(false); | ||
| 3377 | - if !is_floating | ||
| 3378 | - && let Some(parent) = self.get_transient_parent(event.window) | ||
| 3379 | - && self.clients.contains_key(&parent) | ||
| 3380 | - { | ||
| 3381 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3382 | - c.is_floating = true; | ||
| 3383 | - } | ||
| 3384 | - self.floating_windows.insert(event.window); | ||
| 3385 | - self.apply_layout()?; | ||
| 3386 | - } | ||
| 3387 | - } else if event.atom == AtomEnum::WM_NORMAL_HINTS.into() { | ||
| 3388 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3389 | - c.hints_valid = false; | ||
| 3390 | - } | ||
| 3391 | - } else if event.atom == AtomEnum::WM_HINTS.into() { | ||
| 3392 | - self.update_window_hints(event.window)?; | ||
| 3393 | - self.update_bar()?; | ||
| 3394 | - } | ||
| 3395 | - | ||
| 3396 | - if event.atom == self.atoms.wm_name || event.atom == self.atoms.net_wm_name { | ||
| 3397 | - let _ = self.update_window_title(event.window); | ||
| 3398 | - if self.layout.name() == "tabbed" { | ||
| 3399 | - self.update_tab_bars()?; | ||
| 3400 | - } | ||
| 3401 | - } | ||
| 3402 | - | ||
| 3403 | - if event.atom == self.atoms.net_wm_window_type { | ||
| 3404 | - self.update_window_type(event.window)?; | ||
| 3405 | - } | ||
| 3406 | - } | ||
| 3407 | - Event::EnterNotify(event) => { | ||
| 3408 | - if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL | ||
| 3409 | - || event.detail == x11rb::protocol::xproto::NotifyDetail::INFERIOR | ||
| 3410 | - { | ||
| 3411 | - return Ok(Control::Continue); | ||
| 3412 | - } | ||
| 3413 | - if self.windows.contains(&event.event) { | ||
| 3414 | - if let Some(client) = self.clients.get(&event.event) | ||
| 3415 | - && client.monitor_index != self.selected_monitor | ||
| 3416 | - { | ||
| 3417 | - if let Some(old_selected) = self | ||
| 3418 | - .monitors | ||
| 3419 | - .get(self.selected_monitor) | ||
| 3420 | - .and_then(|monitor| monitor.selected_client) | ||
| 3421 | - { | ||
| 3422 | - self.unfocus(old_selected, false)?; | ||
| 3423 | - } | ||
| 3424 | - self.selected_monitor = client.monitor_index; | ||
| 3425 | - self.update_bar()?; | ||
| 3426 | - } | ||
| 3427 | - self.focus(Some(event.event))?; | ||
| 3428 | - self.update_tab_bars()?; | ||
| 3429 | - } | ||
| 3430 | - } | ||
| 3431 | - Event::MotionNotify(event) => { | ||
| 3432 | - if event.event != self.root { | ||
| 3433 | - return Ok(Control::Continue); | ||
| 3434 | - } | ||
| 3435 | - | ||
| 3436 | - if let Some(monitor_index) = | ||
| 3437 | - self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) | ||
| 3438 | - && monitor_index != self.selected_monitor | ||
| 3439 | - { | ||
| 3440 | - if let Some(old_selected) = self | ||
| 3441 | - .monitors | ||
| 3442 | - .get(self.selected_monitor) | ||
| 3443 | - .and_then(|monitor| monitor.selected_client) | ||
| 3444 | - { | ||
| 3445 | - self.unfocus(old_selected, true)?; | ||
| 3446 | - } | ||
| 3447 | - | ||
| 3448 | - self.selected_monitor = monitor_index; | ||
| 3449 | - self.focus(None)?; | ||
| 3450 | - self.update_bar()?; | ||
| 3451 | - self.update_tab_bars()?; | ||
| 3452 | - } | ||
| 3453 | - } | ||
| 3454 | - Event::KeyPress(event) => { | ||
| 3455 | - let Some(mapping) = &self.keyboard_mapping else { | ||
| 3456 | - return Ok(Control::Continue); | ||
| 3457 | - }; | ||
| 3458 | - | ||
| 3459 | - let result = keyboard::handle_key_press( | ||
| 3460 | - event, | ||
| 3461 | - &self.config.keybindings, | ||
| 3462 | - &self.keychord_state, | ||
| 3463 | - mapping, | ||
| 3464 | - ); | ||
| 3465 | - | ||
| 3466 | - match result { | ||
| 3467 | - keyboard::handlers::KeychordResult::Completed(action, arg) => { | ||
| 3468 | - self.keychord_state = keyboard::handlers::KeychordState::Idle; | ||
| 3469 | - self.current_key = 0; | ||
| 3470 | - self.grab_keys()?; | ||
| 3471 | - self.update_bar()?; | ||
| 3472 | - | ||
| 3473 | - match action { | ||
| 3474 | - KeyAction::Quit => return Ok(Control::Quit), | ||
| 3475 | - KeyAction::Restart => match self.try_reload_config() { | ||
| 3476 | - Ok(()) => { | ||
| 3477 | - self.gaps_enabled = self.config.gaps_enabled; | ||
| 3478 | - self.error_message = None; | ||
| 3479 | - if let Err(error) = self.overlay.hide(&self.connection) { | ||
| 3480 | - eprintln!( | ||
| 3481 | - "Failed to hide overlay after config reload: {:?}", | ||
| 3482 | - error | ||
| 3483 | - ); | ||
| 3484 | - } | ||
| 3485 | - self.apply_layout()?; | ||
| 3486 | - self.update_bar()?; | ||
| 3487 | - } | ||
| 3488 | - Err(err) => { | ||
| 3489 | - eprintln!("Config reload error: {}", err); | ||
| 3490 | - self.error_message = Some(err.to_string()); | ||
| 3491 | - let monitor = &self.monitors[self.selected_monitor]; | ||
| 3492 | - let monitor_x = monitor.screen_x as i16; | ||
| 3493 | - let monitor_y = monitor.screen_y as i16; | ||
| 3494 | - let screen_width = monitor.screen_width as u16; | ||
| 3495 | - let screen_height = monitor.screen_height as u16; | ||
| 3496 | - match self.overlay.show_error( | ||
| 3497 | - &self.connection, | ||
| 3498 | - &self.font, | ||
| 3499 | - err, | ||
| 3500 | - monitor_x, | ||
| 3501 | - monitor_y, | ||
| 3502 | - screen_width, | ||
| 3503 | - screen_height, | ||
| 3504 | - ) { | ||
| 3505 | - Ok(()) => eprintln!("Error modal displayed"), | ||
| 3506 | - Err(e) => eprintln!("Failed to show error modal: {:?}", e), | ||
| 3507 | - } | ||
| 3508 | - } | ||
| 3509 | - }, | ||
| 3510 | - _ => self.handle_key_action(action, &arg)?, | ||
| 3511 | - } | ||
| 3512 | - } | ||
| 3513 | - keyboard::handlers::KeychordResult::InProgress(candidates) => { | ||
| 3514 | - self.current_key += 1; | ||
| 3515 | - self.keychord_state = keyboard::handlers::KeychordState::InProgress { | ||
| 3516 | - candidates: candidates.clone(), | ||
| 3517 | - keys_pressed: self.current_key, | ||
| 3518 | - }; | ||
| 3519 | - self.grab_keys()?; | ||
| 3520 | - self.update_bar()?; | ||
| 3521 | - } | ||
| 3522 | - keyboard::handlers::KeychordResult::Cancelled | ||
| 3523 | - | keyboard::handlers::KeychordResult::None => { | ||
| 3524 | - self.keychord_state = keyboard::handlers::KeychordState::Idle; | ||
| 3525 | - self.current_key = 0; | ||
| 3526 | - self.grab_keys()?; | ||
| 3527 | - self.update_bar()?; | ||
| 3528 | - } | ||
| 3529 | - } | ||
| 3530 | - } | ||
| 3531 | - Event::ButtonPress(event) => { | ||
| 3532 | - if self.keybind_overlay.is_visible() | ||
| 3533 | - && event.event != self.keybind_overlay.window() | ||
| 3534 | - && let Err(error) = self.keybind_overlay.hide(&self.connection) | ||
| 3535 | - { | ||
| 3536 | - eprintln!("Failed to hide keybind overlay: {:?}", error); | ||
| 3537 | - } | ||
| 3538 | - | ||
| 3539 | - let is_bar_click = self | ||
| 3540 | - .bars | ||
| 3541 | - .iter() | ||
| 3542 | - .enumerate() | ||
| 3543 | - .find(|(_, bar)| bar.window() == event.event); | ||
| 3544 | - | ||
| 3545 | - if let Some((monitor_index, bar)) = is_bar_click { | ||
| 3546 | - if let Some(tag_index) = bar.handle_click(event.event_x) { | ||
| 3547 | - if monitor_index != self.selected_monitor { | ||
| 3548 | - self.selected_monitor = monitor_index; | ||
| 3549 | - } | ||
| 3550 | - self.view_tag(tag_index)?; | ||
| 3551 | - } | ||
| 3552 | - } else { | ||
| 3553 | - let is_tab_bar_click = self | ||
| 3554 | - .tab_bars | ||
| 3555 | - .iter() | ||
| 3556 | - .enumerate() | ||
| 3557 | - .find(|(_, tab_bar)| tab_bar.window() == event.event); | ||
| 3558 | - | ||
| 3559 | - if let Some((monitor_index, tab_bar)) = is_tab_bar_click { | ||
| 3560 | - if monitor_index != self.selected_monitor { | ||
| 3561 | - self.selected_monitor = monitor_index; | ||
| 3562 | - } | ||
| 3563 | - | ||
| 3564 | - let visible_windows: Vec<(Window, String)> = self | ||
| 3565 | - .windows | ||
| 3566 | - .iter() | ||
| 3567 | - .filter_map(|&window| { | ||
| 3568 | - if let Some(client) = self.clients.get(&window) { | ||
| 3569 | - if client.monitor_index != monitor_index | ||
| 3570 | - || self.floating_windows.contains(&window) | ||
| 3571 | - || self.fullscreen_windows.contains(&window) | ||
| 3572 | - { | ||
| 3573 | - return None; | ||
| 3574 | - } | ||
| 3575 | - let monitor_tags = self | ||
| 3576 | - .monitors | ||
| 3577 | - .get(monitor_index) | ||
| 3578 | - .map(|m| m.tagset[m.selected_tags_index]) | ||
| 3579 | - .unwrap_or(0); | ||
| 3580 | - if (client.tags & monitor_tags) != 0 { | ||
| 3581 | - return Some((window, client.name.clone())); | ||
| 3582 | - } | ||
| 3583 | - } | ||
| 3584 | - None | ||
| 3585 | - }) | ||
| 3586 | - .collect(); | ||
| 3587 | - | ||
| 3588 | - if let Some(clicked_window) = | ||
| 3589 | - tab_bar.get_clicked_window(&visible_windows, event.event_x) | ||
| 3590 | - { | ||
| 3591 | - self.connection.configure_window( | ||
| 3592 | - clicked_window, | ||
| 3593 | - &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | ||
| 3594 | - )?; | ||
| 3595 | - self.focus(Some(clicked_window))?; | ||
| 3596 | - self.update_tab_bars()?; | ||
| 3597 | - } | ||
| 3598 | - } else if event.child != x11rb::NONE { | ||
| 3599 | - self.focus(Some(event.child))?; | ||
| 3600 | - self.restack()?; | ||
| 3601 | - self.update_tab_bars()?; | ||
| 3602 | - | ||
| 3603 | - let state_clean = u16::from(event.state) | ||
| 3604 | - & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2)); | ||
| 3605 | - let modkey_held = state_clean & u16::from(self.config.modkey) != 0; | ||
| 3606 | - | ||
| 3607 | - if modkey_held && event.detail == ButtonIndex::M1.into() { | ||
| 3608 | - if self.clients.contains_key(&event.child) { | ||
| 3609 | - self.drag_window(event.child)?; | ||
| 3610 | - } | ||
| 3611 | - self.connection | ||
| 3612 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3613 | - } else if modkey_held && event.detail == ButtonIndex::M3.into() { | ||
| 3614 | - if self.clients.contains_key(&event.child) { | ||
| 3615 | - self.resize_window_with_mouse(event.child)?; | ||
| 3616 | - } | ||
| 3617 | - self.connection | ||
| 3618 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3619 | - } else { | ||
| 3620 | - self.connection | ||
| 3621 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3622 | - } | ||
| 3623 | - } else if self.windows.contains(&event.event) { | ||
| 3624 | - self.focus(Some(event.event))?; | ||
| 3625 | - self.restack()?; | ||
| 3626 | - self.update_tab_bars()?; | ||
| 3627 | - | ||
| 3628 | - let state_clean = u16::from(event.state) | ||
| 3629 | - & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2)); | ||
| 3630 | - let modkey_held = state_clean & u16::from(self.config.modkey) != 0; | ||
| 3631 | - | ||
| 3632 | - if modkey_held && event.detail == ButtonIndex::M1.into() { | ||
| 3633 | - self.drag_window(event.event)?; | ||
| 3634 | - self.connection | ||
| 3635 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3636 | - } else if modkey_held && event.detail == ButtonIndex::M3.into() { | ||
| 3637 | - self.resize_window_with_mouse(event.event)?; | ||
| 3638 | - self.connection | ||
| 3639 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3640 | - } else { | ||
| 3641 | - self.connection | ||
| 3642 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3643 | - } | ||
| 3644 | - } else { | ||
| 3645 | - self.connection | ||
| 3646 | - .allow_events(Allow::REPLAY_POINTER, event.time)?; | ||
| 3647 | - } | ||
| 3648 | - } | ||
| 3649 | - } | ||
| 3650 | - Event::Expose(event) => { | ||
| 3651 | - for bar in &mut self.bars { | ||
| 3652 | - if event.window == bar.window() { | ||
| 3653 | - bar.invalidate(); | ||
| 3654 | - self.update_bar()?; | ||
| 3655 | - break; | ||
| 3656 | - } | ||
| 3657 | - } | ||
| 3658 | - for _tab_bar in &self.tab_bars { | ||
| 3659 | - if event.window == _tab_bar.window() { | ||
| 3660 | - self.update_tab_bars()?; | ||
| 3661 | - break; | ||
| 3662 | - } | ||
| 3663 | - } | ||
| 3664 | - } | ||
| 3665 | - Event::ConfigureRequest(event) => { | ||
| 3666 | - if let Some(client) = self.clients.get(&event.window) { | ||
| 3667 | - let monitor = &self.monitors[client.monitor_index]; | ||
| 3668 | - let is_floating = client.is_floating; | ||
| 3669 | - let is_fullscreen = client.is_fullscreen; | ||
| 3670 | - let has_layout = self.layout.name() != "normie"; | ||
| 3671 | - | ||
| 3672 | - if event.value_mask.contains(ConfigWindow::BORDER_WIDTH) { | ||
| 3673 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3674 | - c.border_width = event.border_width; | ||
| 3675 | - } | ||
| 3676 | - } else if is_fullscreen { | ||
| 3677 | - self.send_configure_notify(event.window)?; | ||
| 3678 | - } else if is_floating || !has_layout { | ||
| 3679 | - let mut x = client.x_position as i32; | ||
| 3680 | - let mut y = client.y_position as i32; | ||
| 3681 | - let mut w = client.width as i32; | ||
| 3682 | - let mut h = client.height as i32; | ||
| 3683 | - | ||
| 3684 | - if event.value_mask.contains(ConfigWindow::X) { | ||
| 3685 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3686 | - c.old_x_position = c.x_position; | ||
| 3687 | - } | ||
| 3688 | - x = monitor.screen_x + event.x as i32; | ||
| 3689 | - } | ||
| 3690 | - if event.value_mask.contains(ConfigWindow::Y) { | ||
| 3691 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3692 | - c.old_y_position = c.y_position; | ||
| 3693 | - } | ||
| 3694 | - y = monitor.screen_y + event.y as i32; | ||
| 3695 | - } | ||
| 3696 | - if event.value_mask.contains(ConfigWindow::WIDTH) { | ||
| 3697 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3698 | - c.old_width = c.width; | ||
| 3699 | - } | ||
| 3700 | - w = event.width as i32; | ||
| 3701 | - } | ||
| 3702 | - if event.value_mask.contains(ConfigWindow::HEIGHT) { | ||
| 3703 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3704 | - c.old_height = c.height; | ||
| 3705 | - } | ||
| 3706 | - h = event.height as i32; | ||
| 3707 | - } | ||
| 3708 | - | ||
| 3709 | - let bw = self.config.border_width as i32; | ||
| 3710 | - let width_with_border = w + 2 * bw; | ||
| 3711 | - let height_with_border = h + 2 * bw; | ||
| 3712 | - | ||
| 3713 | - if (x + w) > monitor.screen_x + monitor.screen_width && is_floating { | ||
| 3714 | - x = monitor.screen_x | ||
| 3715 | - + (monitor.screen_width / 2 - width_with_border / 2); | ||
| 3716 | - } | ||
| 3717 | - if (y + h) > monitor.screen_y + monitor.screen_height && is_floating { | ||
| 3718 | - y = monitor.screen_y | ||
| 3719 | - + (monitor.screen_height / 2 - height_with_border / 2); | ||
| 3720 | - } | ||
| 3721 | - | ||
| 3722 | - if let Some(c) = self.clients.get_mut(&event.window) { | ||
| 3723 | - c.x_position = x as i16; | ||
| 3724 | - c.y_position = y as i16; | ||
| 3725 | - c.width = w as u16; | ||
| 3726 | - c.height = h as u16; | ||
| 3727 | - } | ||
| 3728 | - | ||
| 3729 | - let only_position_change = event.value_mask.contains(ConfigWindow::X) | ||
| 3730 | - || event.value_mask.contains(ConfigWindow::Y); | ||
| 3731 | - let no_size_change = !event.value_mask.contains(ConfigWindow::WIDTH) | ||
| 3732 | - && !event.value_mask.contains(ConfigWindow::HEIGHT); | ||
| 3733 | - if only_position_change && no_size_change { | ||
| 3734 | - self.send_configure_notify(event.window)?; | ||
| 3735 | - } | ||
| 3736 | - | ||
| 3737 | - if self.is_visible(event.window) { | ||
| 3738 | - self.connection.configure_window( | ||
| 3739 | - event.window, | ||
| 3740 | - &ConfigureWindowAux::new() | ||
| 3741 | - .x(x) | ||
| 3742 | - .y(y) | ||
| 3743 | - .width(w as u32) | ||
| 3744 | - .height(h as u32), | ||
| 3745 | - )?; | ||
| 3746 | - } | ||
| 3747 | - } else { | ||
| 3748 | - self.send_configure_notify(event.window)?; | ||
| 3749 | - } | ||
| 3750 | - } else { | ||
| 3751 | - let mut aux = ConfigureWindowAux::new(); | ||
| 3752 | - if event.value_mask.contains(ConfigWindow::X) { | ||
| 3753 | - aux = aux.x(event.x as i32); | ||
| 3754 | - } | ||
| 3755 | - if event.value_mask.contains(ConfigWindow::Y) { | ||
| 3756 | - aux = aux.y(event.y as i32); | ||
| 3757 | - } | ||
| 3758 | - if event.value_mask.contains(ConfigWindow::WIDTH) { | ||
| 3759 | - aux = aux.width(event.width as u32); | ||
| 3760 | - } | ||
| 3761 | - if event.value_mask.contains(ConfigWindow::HEIGHT) { | ||
| 3762 | - aux = aux.height(event.height as u32); | ||
| 3763 | - } | ||
| 3764 | - if event.value_mask.contains(ConfigWindow::BORDER_WIDTH) { | ||
| 3765 | - aux = aux.border_width(event.border_width as u32); | ||
| 3766 | - } | ||
| 3767 | - if event.value_mask.contains(ConfigWindow::SIBLING) { | ||
| 3768 | - aux = aux.sibling(event.sibling); | ||
| 3769 | - } | ||
| 3770 | - if event.value_mask.contains(ConfigWindow::STACK_MODE) { | ||
| 3771 | - aux = aux.stack_mode(event.stack_mode); | ||
| 3772 | - } | ||
| 3773 | - self.connection.configure_window(event.window, &aux)?; | ||
| 3774 | - } | ||
| 3775 | - self.connection.flush()?; | ||
| 3776 | - } | ||
| 3777 | - Event::ClientMessage(event) => { | ||
| 3778 | - if !self.clients.contains_key(&event.window) { | ||
| 3779 | - return Ok(Control::Continue); | ||
| 3780 | - } | ||
| 3781 | - | ||
| 3782 | - if event.type_ == self.atoms.net_wm_state { | ||
| 3783 | - let data = event.data.as_data32(); | ||
| 3784 | - let atom1 = data.get(1).copied().unwrap_or(0); | ||
| 3785 | - let atom2 = data.get(2).copied().unwrap_or(0); | ||
| 3786 | - | ||
| 3787 | - if atom1 == self.atoms.net_wm_state_fullscreen | ||
| 3788 | - || atom2 == self.atoms.net_wm_state_fullscreen | ||
| 3789 | - { | ||
| 3790 | - let action = data[0]; | ||
| 3791 | - let fullscreen = match action { | ||
| 3792 | - 1 => true, | ||
| 3793 | - 0 => false, | ||
| 3794 | - 2 => !self.fullscreen_windows.contains(&event.window), | ||
| 3795 | - _ => return Ok(Control::Continue), | ||
| 3796 | - }; | ||
| 3797 | - self.set_window_fullscreen(event.window, fullscreen)?; | ||
| 3798 | - self.restack()?; | ||
| 3799 | - } | ||
| 3800 | - } else if event.type_ == self.atoms.net_active_window { | ||
| 3801 | - let selected_window = self | ||
| 3802 | - .monitors | ||
| 3803 | - .get(self.selected_monitor) | ||
| 3804 | - .and_then(|m| m.selected_client); | ||
| 3805 | - | ||
| 3806 | - let is_urgent = self | ||
| 3807 | - .clients | ||
| 3808 | - .get(&event.window) | ||
| 3809 | - .map(|c| c.is_urgent) | ||
| 3810 | - .unwrap_or(false); | ||
| 3811 | - | ||
| 3812 | - if Some(event.window) != selected_window && !is_urgent { | ||
| 3813 | - self.set_urgent(event.window, true)?; | ||
| 3814 | - } | ||
| 3815 | - } | ||
| 3816 | - } | ||
| 3817 | - Event::FocusIn(event) => { | ||
| 3818 | - if !self.windows.contains(&event.event) { | ||
| 3819 | - return Ok(Control::Continue); | ||
| 3820 | - } | ||
| 3821 | - | ||
| 3822 | - let event_window_visible = self.is_visible(event.event); | ||
| 3823 | - | ||
| 3824 | - if !event_window_visible { | ||
| 3825 | - self.focus(None)?; | ||
| 3826 | - } else { | ||
| 3827 | - let selected_window = self | ||
| 3828 | - .monitors | ||
| 3829 | - .get(self.selected_monitor) | ||
| 3830 | - .and_then(|monitor| monitor.selected_client); | ||
| 3831 | - | ||
| 3832 | - if let Some(focused_window) = selected_window | ||
| 3833 | - && event.event != focused_window | ||
| 3834 | - { | ||
| 3835 | - self.set_focus(focused_window)?; | ||
| 3836 | - } | ||
| 3837 | - } | ||
| 3838 | - } | ||
| 3839 | - Event::MappingNotify(event) => { | ||
| 3840 | - if event.request == x11rb::protocol::xproto::Mapping::KEYBOARD { | ||
| 3841 | - self.grab_keys()?; | ||
| 3842 | - } | ||
| 3843 | - } | ||
| 3844 | - Event::ConfigureNotify(event) => { | ||
| 3845 | - if event.window == self.root { | ||
| 3846 | - let old_width = self.screen.width_in_pixels; | ||
| 3847 | - let old_height = self.screen.height_in_pixels; | ||
| 3848 | - | ||
| 3849 | - if event.width != old_width || event.height != old_height { | ||
| 3850 | - self.screen = self.connection.setup().roots[self.screen_number].clone(); | ||
| 3851 | - | ||
| 3852 | - for monitor_index in 0..self.monitors.len() { | ||
| 3853 | - let monitor = &self.monitors[monitor_index]; | ||
| 3854 | - let monitor_x = monitor.screen_x; | ||
| 3855 | - let monitor_y = monitor.screen_y; | ||
| 3856 | - let monitor_width = monitor.screen_width as u32; | ||
| 3857 | - let monitor_height = monitor.screen_height as u32; | ||
| 3858 | - | ||
| 3859 | - let fullscreen_on_monitor: Vec<Window> = self | ||
| 3860 | - .fullscreen_windows | ||
| 3861 | - .iter() | ||
| 3862 | - .filter(|&&window| { | ||
| 3863 | - self.clients | ||
| 3864 | - .get(&window) | ||
| 3865 | - .map(|client| client.monitor_index == monitor_index) | ||
| 3866 | - .unwrap_or(false) | ||
| 3867 | - }) | ||
| 3868 | - .copied() | ||
| 3869 | - .collect(); | ||
| 3870 | - | ||
| 3871 | - for window in fullscreen_on_monitor { | ||
| 3872 | - self.connection.configure_window( | ||
| 3873 | - window, | ||
| 3874 | - &ConfigureWindowAux::new() | ||
| 3875 | - .x(monitor_x) | ||
| 3876 | - .y(monitor_y) | ||
| 3877 | - .width(monitor_width) | ||
| 3878 | - .height(monitor_height), | ||
| 3879 | - )?; | ||
| 3880 | - } | ||
| 3881 | - } | ||
| 3882 | - | ||
| 3883 | - self.apply_layout()?; | ||
| 3884 | - } | ||
| 3885 | - } | ||
| 3886 | - } | ||
| 3887 | - _ => {} | ||
| 3888 | - } | ||
| 3889 | - Ok(Control::Continue) | ||
| 3890 | - } | ||
| 3891 | - | ||
| 3892 | - fn apply_layout(&mut self) -> WmResult<()> { | ||
| 3893 | - for monitor_index in 0..self.monitors.len() { | ||
| 3894 | - let stack_head = self.monitors.get(monitor_index).and_then(|m| m.stack_head); | ||
| 3895 | - self.showhide(stack_head)?; | ||
| 3896 | - } | ||
| 3897 | - | ||
| 3898 | - let is_normie = self.layout.name() == LayoutType::Normie.as_str(); | ||
| 3899 | - | ||
| 3900 | - if !is_normie { | ||
| 3901 | - let monitor_count = self.monitors.len(); | ||
| 3902 | - for monitor_index in 0..monitor_count { | ||
| 3903 | - let monitor = &self.monitors[monitor_index]; | ||
| 3904 | - let border_width = self.config.border_width; | ||
| 3905 | - | ||
| 3906 | - let gaps = if self.gaps_enabled { | ||
| 3907 | - GapConfig { | ||
| 3908 | - inner_horizontal: self.config.gap_inner_horizontal, | ||
| 3909 | - inner_vertical: self.config.gap_inner_vertical, | ||
| 3910 | - outer_horizontal: self.config.gap_outer_horizontal, | ||
| 3911 | - outer_vertical: self.config.gap_outer_vertical, | ||
| 3912 | - } | ||
| 3913 | - } else { | ||
| 3914 | - GapConfig { | ||
| 3915 | - inner_horizontal: 0, | ||
| 3916 | - inner_vertical: 0, | ||
| 3917 | - outer_horizontal: 0, | ||
| 3918 | - outer_vertical: 0, | ||
| 3919 | - } | ||
| 3920 | - }; | ||
| 3921 | - | ||
| 3922 | - let monitor_x = monitor.screen_x; | ||
| 3923 | - let monitor_y = monitor.screen_y; | ||
| 3924 | - let monitor_width = monitor.screen_width; | ||
| 3925 | - let monitor_height = monitor.screen_height; | ||
| 3926 | - let scroll_offset = monitor.scroll_offset; | ||
| 3927 | - | ||
| 3928 | - let mut visible: Vec<Window> = Vec::new(); | ||
| 3929 | - let mut current = self.next_tiled(monitor.clients_head, monitor); | ||
| 3930 | - while let Some(window) = current { | ||
| 3931 | - visible.push(window); | ||
| 3932 | - if let Some(client) = self.clients.get(&window) { | ||
| 3933 | - current = self.next_tiled(client.next, monitor); | ||
| 3934 | - } else { | ||
| 3935 | - break; | ||
| 3936 | - } | ||
| 3937 | - } | ||
| 3938 | - | ||
| 3939 | - let bar_height = if self.show_bar { | ||
| 3940 | - self.bars | ||
| 3941 | - .get(monitor_index) | ||
| 3942 | - .map(|bar| bar.height() as u32) | ||
| 3943 | - .unwrap_or(0) | ||
| 3944 | - } else { | ||
| 3945 | - 0 | ||
| 3946 | - }; | ||
| 3947 | - let usable_height = monitor_height.saturating_sub(bar_height as i32); | ||
| 3948 | - let master_factor = monitor.master_factor; | ||
| 3949 | - let num_master = monitor.num_master; | ||
| 3950 | - let smartgaps_enabled = self.config.smartgaps_enabled; | ||
| 3951 | - | ||
| 3952 | - let geometries = self.layout.arrange( | ||
| 3953 | - &visible, | ||
| 3954 | - monitor_width as u32, | ||
| 3955 | - usable_height as u32, | ||
| 3956 | - &gaps, | ||
| 3957 | - master_factor, | ||
| 3958 | - num_master, | ||
| 3959 | - smartgaps_enabled, | ||
| 3960 | - ); | ||
| 3961 | - | ||
| 3962 | - for (window, geometry) in visible.iter().zip(geometries.iter()) { | ||
| 3963 | - let mut adjusted_width = geometry.width.saturating_sub(2 * border_width); | ||
| 3964 | - let mut adjusted_height = geometry.height.saturating_sub(2 * border_width); | ||
| 3965 | - | ||
| 3966 | - if let Some(client) = self.clients.get(window).cloned() | ||
| 3967 | - && !client.is_floating | ||
| 3968 | - { | ||
| 3969 | - let (_, _, hint_width, hint_height, _) = self.apply_size_hints( | ||
| 3970 | - *window, | ||
| 3971 | - geometry.x_coordinate, | ||
| 3972 | - geometry.y_coordinate, | ||
| 3973 | - adjusted_width as i32, | ||
| 3974 | - adjusted_height as i32, | ||
| 3975 | - ); | ||
| 3976 | - adjusted_width = hint_width as u32; | ||
| 3977 | - adjusted_height = hint_height as u32; | ||
| 3978 | - } | ||
| 3979 | - | ||
| 3980 | - let is_scrolling = self.layout.name() == "scrolling"; | ||
| 3981 | - let adjusted_x = if is_scrolling { | ||
| 3982 | - geometry.x_coordinate + monitor_x - scroll_offset | ||
| 3983 | - } else { | ||
| 3984 | - geometry.x_coordinate + monitor_x | ||
| 3985 | - }; | ||
| 3986 | - let adjusted_y = geometry.y_coordinate + monitor_y + bar_height as i32; | ||
| 3987 | - | ||
| 3988 | - if let Some(client) = self.clients.get_mut(window) { | ||
| 3989 | - client.x_position = adjusted_x as i16; | ||
| 3990 | - client.y_position = adjusted_y as i16; | ||
| 3991 | - client.width = adjusted_width as u16; | ||
| 3992 | - client.height = adjusted_height as u16; | ||
| 3993 | - } | ||
| 3994 | - | ||
| 3995 | - self.connection.configure_window( | ||
| 3996 | - *window, | ||
| 3997 | - &ConfigureWindowAux::new() | ||
| 3998 | - .x(adjusted_x) | ||
| 3999 | - .y(adjusted_y) | ||
| 4000 | - .width(adjusted_width) | ||
| 4001 | - .height(adjusted_height) | ||
| 4002 | - .border_width(border_width), | ||
| 4003 | - )?; | ||
| 4004 | - | ||
| 4005 | - if let Some(c) = self.clients.get_mut(window) { | ||
| 4006 | - c.x_position = adjusted_x as i16; | ||
| 4007 | - c.y_position = adjusted_y as i16; | ||
| 4008 | - c.width = adjusted_width as u16; | ||
| 4009 | - c.height = adjusted_height as u16; | ||
| 4010 | - c.border_width = border_width as u16; | ||
| 4011 | - } | ||
| 4012 | - } | ||
| 4013 | - } | ||
| 4014 | - } | ||
| 4015 | - | ||
| 4016 | - for monitor_index in 0..self.monitors.len() { | ||
| 4017 | - let stack_head = self.monitors[monitor_index].stack_head; | ||
| 4018 | - self.showhide(stack_head)?; | ||
| 4019 | - } | ||
| 4020 | - | ||
| 4021 | - for monitor_index in 0..self.monitors.len() { | ||
| 4022 | - let monitor = &self.monitors[monitor_index]; | ||
| 4023 | - let tags = monitor.tagset[monitor.selected_tags_index]; | ||
| 4024 | - | ||
| 4025 | - let has_visible_fullscreen = self.fullscreen_windows.iter().any(|&w| { | ||
| 4026 | - self.clients.get(&w).map_or(false, |c| { | ||
| 4027 | - c.monitor_index == monitor_index && (c.tags & tags) != 0 | ||
| 4028 | - }) | ||
| 4029 | - }); | ||
| 4030 | - | ||
| 4031 | - if has_visible_fullscreen { | ||
| 4032 | - if let Some(bar) = self.bars.get(monitor_index) { | ||
| 4033 | - self.connection.unmap_window(bar.window())?; | ||
| 4034 | - } | ||
| 4035 | - | ||
| 4036 | - for &window in &self.fullscreen_windows { | ||
| 4037 | - if let Some(client) = self.clients.get(&window) { | ||
| 4038 | - if client.monitor_index == monitor_index && (client.tags & tags) != 0 { | ||
| 4039 | - self.connection.configure_window( | ||
| 4040 | - window, | ||
| 4041 | - &ConfigureWindowAux::new() | ||
| 4042 | - .border_width(0) | ||
| 4043 | - .x(monitor.screen_x) | ||
| 4044 | - .y(monitor.screen_y) | ||
| 4045 | - .width(monitor.screen_width as u32) | ||
| 4046 | - .height(monitor.screen_height as u32) | ||
| 4047 | - .stack_mode(StackMode::ABOVE), | ||
| 4048 | - )?; | ||
| 4049 | - } | ||
| 4050 | - } | ||
| 4051 | - } | ||
| 4052 | - } else if self.show_bar { | ||
| 4053 | - if let Some(bar) = self.bars.get(monitor_index) { | ||
| 4054 | - self.connection.map_window(bar.window())?; | ||
| 4055 | - } | ||
| 4056 | - } | ||
| 4057 | - } | ||
| 4058 | - | ||
| 4059 | - self.connection.flush()?; | ||
| 4060 | - | ||
| 4061 | - let is_tabbed = self.layout.name() == LayoutType::Tabbed.as_str(); | ||
| 4062 | - | ||
| 4063 | - if is_tabbed { | ||
| 4064 | - let outer_horizontal = if self.gaps_enabled { | ||
| 4065 | - self.config.gap_outer_horizontal | ||
| 4066 | - } else { | ||
| 4067 | - 0 | ||
| 4068 | - }; | ||
| 4069 | - let outer_vertical = if self.gaps_enabled { | ||
| 4070 | - self.config.gap_outer_vertical | ||
| 4071 | - } else { | ||
| 4072 | - 0 | ||
| 4073 | - }; | ||
| 4074 | - | ||
| 4075 | - for monitor_index in 0..self.tab_bars.len() { | ||
| 4076 | - if let Some(monitor) = self.monitors.get(monitor_index) { | ||
| 4077 | - let bar_height = if self.show_bar { | ||
| 4078 | - self.bars | ||
| 4079 | - .get(monitor_index) | ||
| 4080 | - .map(|bar| bar.height() as f32) | ||
| 4081 | - .unwrap_or(0.0) | ||
| 4082 | - } else { | ||
| 4083 | - 0.0 | ||
| 4084 | - }; | ||
| 4085 | - | ||
| 4086 | - let tab_bar_x = (monitor.screen_x + outer_horizontal as i32) as i16; | ||
| 4087 | - let tab_bar_y = | ||
| 4088 | - (monitor.screen_y as f32 + bar_height + outer_vertical as f32) as i16; | ||
| 4089 | - let tab_bar_width = monitor | ||
| 4090 | - .screen_width | ||
| 4091 | - .saturating_sub(2 * outer_horizontal as i32) | ||
| 4092 | - as u16; | ||
| 4093 | - | ||
| 4094 | - if let Err(e) = self.tab_bars[monitor_index].reposition( | ||
| 4095 | - &self.connection, | ||
| 4096 | - tab_bar_x, | ||
| 4097 | - tab_bar_y, | ||
| 4098 | - tab_bar_width, | ||
| 4099 | - ) { | ||
| 4100 | - eprintln!("Failed to reposition tab bar: {:?}", e); | ||
| 4101 | - } | ||
| 4102 | - } | ||
| 4103 | - } | ||
| 4104 | - } | ||
| 4105 | - | ||
| 4106 | - for monitor_index in 0..self.tab_bars.len() { | ||
| 4107 | - let has_visible_windows = self.windows.iter().any(|&window| { | ||
| 4108 | - if let Some(client) = self.clients.get(&window) { | ||
| 4109 | - if client.monitor_index != monitor_index | ||
| 4110 | - || self.floating_windows.contains(&window) | ||
| 4111 | - || self.fullscreen_windows.contains(&window) | ||
| 4112 | - { | ||
| 4113 | - return false; | ||
| 4114 | - } | ||
| 4115 | - if let Some(monitor) = self.monitors.get(monitor_index) { | ||
| 4116 | - return (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0; | ||
| 4117 | - } | ||
| 4118 | - } | ||
| 4119 | - false | ||
| 4120 | - }); | ||
| 4121 | - | ||
| 4122 | - if is_tabbed && has_visible_windows { | ||
| 4123 | - if let Err(e) = self.tab_bars[monitor_index].show(&self.connection) { | ||
| 4124 | - eprintln!("Failed to show tab bar: {:?}", e); | ||
| 4125 | - } | ||
| 4126 | - } else if let Err(e) = self.tab_bars[monitor_index].hide(&self.connection) { | ||
| 4127 | - eprintln!("Failed to hide tab bar: {:?}", e); | ||
| 4128 | - } | ||
| 4129 | - } | ||
| 4130 | - | ||
| 4131 | - if is_tabbed { | ||
| 4132 | - self.update_tab_bars()?; | ||
| 4133 | - } | ||
| 4134 | - | ||
| 4135 | - Ok(()) | ||
| 4136 | - } | ||
| 4137 | - | ||
| 4138 | - pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { | ||
| 4139 | - self.layout = Box::new(new_layout); | ||
| 4140 | - self.apply_layout()?; | ||
| 4141 | - Ok(()) | ||
| 4142 | - } | ||
| 4143 | - | ||
| 4144 | - fn send_configure_notify(&self, window: Window) -> WmResult<()> { | ||
| 4145 | - let client = self.clients.get(&window); | ||
| 4146 | - let (x, y, w, h, bw) = if let Some(c) = client { | ||
| 4147 | - ( | ||
| 4148 | - c.x_position, | ||
| 4149 | - c.y_position, | ||
| 4150 | - c.width, | ||
| 4151 | - c.height, | ||
| 4152 | - c.border_width, | ||
| 4153 | - ) | ||
| 4154 | - } else { | ||
| 4155 | - let geom = self.connection.get_geometry(window)?.reply()?; | ||
| 4156 | - (geom.x, geom.y, geom.width, geom.height, geom.border_width) | ||
| 4157 | - }; | ||
| 4158 | - | ||
| 4159 | - let event = ConfigureNotifyEvent { | ||
| 4160 | - response_type: CONFIGURE_NOTIFY_EVENT, | ||
| 4161 | - sequence: 0, | ||
| 4162 | - event: window, | ||
| 4163 | - window, | ||
| 4164 | - above_sibling: x11rb::NONE, | ||
| 4165 | - x, | ||
| 4166 | - y, | ||
| 4167 | - width: w, | ||
| 4168 | - height: h, | ||
| 4169 | - border_width: bw, | ||
| 4170 | - override_redirect: false, | ||
| 4171 | - }; | ||
| 4172 | - | ||
| 4173 | - self.connection | ||
| 4174 | - .send_event(false, window, EventMask::STRUCTURE_NOTIFY, event)?; | ||
| 4175 | - | ||
| 4176 | - Ok(()) | ||
| 4177 | - } | ||
| 4178 | - | ||
| 4179 | - fn update_size_hints(&mut self, window: Window) -> WmResult<()> { | ||
| 4180 | - let size_hints = self | ||
| 4181 | - .connection | ||
| 4182 | - .get_property( | ||
| 4183 | - false, | ||
| 4184 | - window, | ||
| 4185 | - x11rb::protocol::xproto::AtomEnum::WM_NORMAL_HINTS, | ||
| 4186 | - x11rb::protocol::xproto::AtomEnum::WM_SIZE_HINTS, | ||
| 4187 | - 0, | ||
| 4188 | - 18, | ||
| 4189 | - )? | ||
| 4190 | - .reply()?; | ||
| 4191 | - | ||
| 4192 | - if size_hints.value.is_empty() { | ||
| 4193 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 4194 | - client.hints_valid = false; | ||
| 4195 | - } | ||
| 4196 | - return Ok(()); | ||
| 4197 | - } | ||
| 4198 | - | ||
| 4199 | - if size_hints.value.len() < 18 * 4 { | ||
| 4200 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 4201 | - client.hints_valid = false; | ||
| 4202 | - } | ||
| 4203 | - return Ok(()); | ||
| 4204 | - } | ||
| 4205 | - | ||
| 4206 | - use crate::size_hints::{flags::*, offset::*}; | ||
| 4207 | - | ||
| 4208 | - let read_u32 = |offset: usize| -> u32 { | ||
| 4209 | - let bytes = &size_hints.value[offset * 4..(offset + 1) * 4]; | ||
| 4210 | - u32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) | ||
| 4211 | - }; | ||
| 4212 | - | ||
| 4213 | - let flags = read_u32(FLAGS); | ||
| 4214 | - | ||
| 4215 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 4216 | - if flags & P_BASE_SIZE != 0 { | ||
| 4217 | - client.base_width = read_u32(BASE_WIDTH) as i32; | ||
| 4218 | - client.base_height = read_u32(BASE_HEIGHT) as i32; | ||
| 4219 | - } else if flags & P_MIN_SIZE != 0 { | ||
| 4220 | - client.base_width = read_u32(MIN_WIDTH) as i32; | ||
| 4221 | - client.base_height = read_u32(MIN_HEIGHT) as i32; | ||
| 4222 | - } else { | ||
| 4223 | - client.base_width = 0; | ||
| 4224 | - client.base_height = 0; | ||
| 4225 | - } | ||
| 4226 | - | ||
| 4227 | - if flags & P_RESIZE_INC != 0 { | ||
| 4228 | - client.increment_width = read_u32(WIDTH_INC) as i32; | ||
| 4229 | - client.increment_height = read_u32(HEIGHT_INC) as i32; | ||
| 4230 | - } else { | ||
| 4231 | - client.increment_width = 0; | ||
| 4232 | - client.increment_height = 0; | ||
| 4233 | - } | ||
| 4234 | - | ||
| 4235 | - if flags & P_MAX_SIZE != 0 { | ||
| 4236 | - client.max_width = read_u32(MAX_WIDTH) as i32; | ||
| 4237 | - client.max_height = read_u32(MAX_HEIGHT) as i32; | ||
| 4238 | - } else { | ||
| 4239 | - client.max_width = 0; | ||
| 4240 | - client.max_height = 0; | ||
| 4241 | - } | ||
| 4242 | - | ||
| 4243 | - if flags & P_MIN_SIZE != 0 { | ||
| 4244 | - client.min_width = read_u32(MIN_WIDTH) as i32; | ||
| 4245 | - client.min_height = read_u32(MIN_HEIGHT) as i32; | ||
| 4246 | - } else if flags & P_BASE_SIZE != 0 { | ||
| 4247 | - client.min_width = read_u32(BASE_WIDTH) as i32; | ||
| 4248 | - client.min_height = read_u32(BASE_HEIGHT) as i32; | ||
| 4249 | - } else { | ||
| 4250 | - client.min_width = 0; | ||
| 4251 | - client.min_height = 0; | ||
| 4252 | - } | ||
| 4253 | - | ||
| 4254 | - if flags & P_ASPECT != 0 { | ||
| 4255 | - client.min_aspect = | ||
| 4256 | - (read_u32(MIN_ASPECT_Y) as f32) / (read_u32(MIN_ASPECT_X) as f32).max(1.0); | ||
| 4257 | - client.max_aspect = | ||
| 4258 | - (read_u32(MAX_ASPECT_X) as f32) / (read_u32(MAX_ASPECT_Y) as f32).max(1.0); | ||
| 4259 | - } else { | ||
| 4260 | - client.min_aspect = 0.0; | ||
| 4261 | - client.max_aspect = 0.0; | ||
| 4262 | - } | ||
| 4263 | - | ||
| 4264 | - client.is_fixed = client.max_width > 0 | ||
| 4265 | - && client.max_height > 0 | ||
| 4266 | - && client.max_width == client.min_width | ||
| 4267 | - && client.max_height == client.min_height; | ||
| 4268 | - | ||
| 4269 | - client.hints_valid = true; | ||
| 4270 | - } | ||
| 4271 | - Ok(()) | ||
| 4272 | - } | ||
| 4273 | - | ||
| 4274 | - fn update_window_title(&mut self, window: Window) -> WmResult<()> { | ||
| 4275 | - let net_name = self | ||
| 4276 | - .connection | ||
| 4277 | - .get_property( | ||
| 4278 | - false, | ||
| 4279 | - window, | ||
| 4280 | - self.atoms.net_wm_name, | ||
| 4281 | - self.atoms.utf8_string, | ||
| 4282 | - 0, | ||
| 4283 | - 256, | ||
| 4284 | - ) | ||
| 4285 | - .ok() | ||
| 4286 | - .and_then(|cookie| cookie.reply().ok()); | ||
| 4287 | - | ||
| 4288 | - if let Some(name) = net_name | ||
| 4289 | - && !name.value.is_empty() | ||
| 4290 | - && let Ok(title) = String::from_utf8(name.value.clone()) | ||
| 4291 | - && let Some(client) = self.clients.get_mut(&window) | ||
| 4292 | - { | ||
| 4293 | - client.name = title; | ||
| 4294 | - return Ok(()); | ||
| 4295 | - } | ||
| 4296 | - | ||
| 4297 | - let wm_name = self | ||
| 4298 | - .connection | ||
| 4299 | - .get_property( | ||
| 4300 | - false, | ||
| 4301 | - window, | ||
| 4302 | - self.atoms.wm_name, | ||
| 4303 | - x11rb::protocol::xproto::AtomEnum::STRING, | ||
| 4304 | - 0, | ||
| 4305 | - 256, | ||
| 4306 | - )? | ||
| 4307 | - .reply()?; | ||
| 4308 | - | ||
| 4309 | - if !wm_name.value.is_empty() | ||
| 4310 | - && let Ok(title) = String::from_utf8(wm_name.value.clone()) | ||
| 4311 | - && let Some(client) = self.clients.get_mut(&window) | ||
| 4312 | - { | ||
| 4313 | - client.name = title; | ||
| 4314 | - } | ||
| 4315 | - | ||
| 4316 | - Ok(()) | ||
| 4317 | - } | ||
| 4318 | - | ||
| 4319 | - fn update_window_hints(&mut self, window: Window) -> WmResult<()> { | ||
| 4320 | - let hints_reply = self | ||
| 4321 | - .connection | ||
| 4322 | - .get_property(false, window, AtomEnum::WM_HINTS, AtomEnum::WM_HINTS, 0, 9)? | ||
| 4323 | - .reply(); | ||
| 4324 | - | ||
| 4325 | - if let Ok(hints) = hints_reply | ||
| 4326 | - && hints.value.len() >= 4 | ||
| 4327 | - { | ||
| 4328 | - let flags = u32::from_ne_bytes([ | ||
| 4329 | - hints.value[0], | ||
| 4330 | - hints.value[1], | ||
| 4331 | - hints.value[2], | ||
| 4332 | - hints.value[3], | ||
| 4333 | - ]); | ||
| 4334 | - | ||
| 4335 | - let selected_window = self | ||
| 4336 | - .monitors | ||
| 4337 | - .get(self.selected_monitor) | ||
| 4338 | - .and_then(|m| m.selected_client); | ||
| 4339 | - | ||
| 4340 | - if Some(window) == selected_window && (flags & 256) != 0 { | ||
| 4341 | - let new_flags = flags & !256; | ||
| 4342 | - let mut new_hints = hints.value.clone(); | ||
| 4343 | - new_hints[0..4].copy_from_slice(&new_flags.to_ne_bytes()); | ||
| 4344 | - | ||
| 4345 | - self.connection.change_property( | ||
| 4346 | - x11rb::protocol::xproto::PropMode::REPLACE, | ||
| 4347 | - window, | ||
| 4348 | - AtomEnum::WM_HINTS, | ||
| 4349 | - AtomEnum::WM_HINTS, | ||
| 4350 | - 32, | ||
| 4351 | - 9, | ||
| 4352 | - &new_hints, | ||
| 4353 | - )?; | ||
| 4354 | - } else if let Some(client) = self.clients.get_mut(&window) { | ||
| 4355 | - client.is_urgent = (flags & 256) != 0; | ||
| 4356 | - } | ||
| 4357 | - | ||
| 4358 | - if hints.value.len() >= 8 && (flags & 1) != 0 { | ||
| 4359 | - let input = i32::from_ne_bytes([ | ||
| 4360 | - hints.value[4], | ||
| 4361 | - hints.value[5], | ||
| 4362 | - hints.value[6], | ||
| 4363 | - hints.value[7], | ||
| 4364 | - ]); | ||
| 4365 | - | ||
| 4366 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 4367 | - client.never_focus = input == 0; | ||
| 4368 | - } | ||
| 4369 | - } else if let Some(client) = self.clients.get_mut(&window) { | ||
| 4370 | - client.never_focus = false; | ||
| 4371 | - } | ||
| 4372 | - } | ||
| 4373 | - | ||
| 4374 | - Ok(()) | ||
| 4375 | - } | ||
| 4376 | - | ||
| 4377 | - fn update_window_type(&mut self, window: Window) -> WmResult<()> { | ||
| 4378 | - if let Ok(state_atoms) = self.get_window_atom_list_property(window, self.atoms.net_wm_state) | ||
| 4379 | - { | ||
| 4380 | - if state_atoms.contains(&self.atoms.net_wm_state_fullscreen) { | ||
| 4381 | - self.set_window_fullscreen(window, true)?; | ||
| 4382 | - } | ||
| 4383 | - } | ||
| 4384 | - | ||
| 4385 | - if let Ok(Some(type_atom)) = | ||
| 4386 | - self.get_window_atom_property(window, self.atoms.net_wm_window_type) | ||
| 4387 | - && type_atom == self.atoms.net_wm_window_type_dialog | ||
| 4388 | - { | ||
| 4389 | - if let Some(client) = self.clients.get_mut(&window) { | ||
| 4390 | - client.is_floating = true; | ||
| 4391 | - } | ||
| 4392 | - self.floating_windows.insert(window); | ||
| 4393 | - } | ||
| 4394 | - | ||
| 4395 | - Ok(()) | ||
| 4396 | - } | ||
| 4397 | - | ||
| 4398 | - fn apply_size_hints( | ||
| 4399 | - &mut self, | ||
| 4400 | - window: Window, | ||
| 4401 | - mut x: i32, | ||
| 4402 | - mut y: i32, | ||
| 4403 | - mut w: i32, | ||
| 4404 | - mut h: i32, | ||
| 4405 | - ) -> (i32, i32, i32, i32, bool) { | ||
| 4406 | - let bh = 20; | ||
| 4407 | - | ||
| 4408 | - let ( | ||
| 4409 | - client_x, | ||
| 4410 | - client_y, | ||
| 4411 | - client_w, | ||
| 4412 | - client_h, | ||
| 4413 | - bw, | ||
| 4414 | - monitor_index, | ||
| 4415 | - is_floating, | ||
| 4416 | - mut hints_valid, | ||
| 4417 | - ) = { | ||
| 4418 | - let client = match self.clients.get(&window) { | ||
| 4419 | - Some(c) => c, | ||
| 4420 | - None => return (x, y, w, h, false), | ||
| 4421 | - }; | ||
| 4422 | - ( | ||
| 4423 | - client.x_position as i32, | ||
| 4424 | - client.y_position as i32, | ||
| 4425 | - client.width as i32, | ||
| 4426 | - client.height as i32, | ||
| 4427 | - client.border_width as i32, | ||
| 4428 | - client.monitor_index, | ||
| 4429 | - client.is_floating, | ||
| 4430 | - client.hints_valid, | ||
| 4431 | - ) | ||
| 4432 | - }; | ||
| 4433 | - | ||
| 4434 | - let monitor = &self.monitors[monitor_index]; | ||
| 4435 | - let client_width = client_w + 2 * bw; | ||
| 4436 | - let client_height = client_h + 2 * bw; | ||
| 4437 | - | ||
| 4438 | - w = w.max(1); | ||
| 4439 | - h = h.max(1); | ||
| 4440 | - | ||
| 4441 | - if x >= monitor.window_area_x + monitor.window_area_width { | ||
| 4442 | - x = monitor.window_area_x + monitor.window_area_width - client_width; | ||
| 4443 | - } | ||
| 4444 | - if y >= monitor.window_area_y + monitor.window_area_height { | ||
| 4445 | - y = monitor.window_area_y + monitor.window_area_height - client_height; | ||
| 4446 | - } | ||
| 4447 | - if x + w + 2 * bw <= monitor.window_area_x { | ||
| 4448 | - x = monitor.window_area_x; | ||
| 4449 | - } | ||
| 4450 | - if y + h + 2 * bw <= monitor.window_area_y { | ||
| 4451 | - y = monitor.window_area_y; | ||
| 4452 | - } | ||
| 4453 | - | ||
| 4454 | - if h < bh { | ||
| 4455 | - h = bh; | ||
| 4456 | - } | ||
| 4457 | - if w < bh { | ||
| 4458 | - w = bh; | ||
| 4459 | - } | ||
| 4460 | - | ||
| 4461 | - if is_floating || self.layout.name() == "normie" { | ||
| 4462 | - if !hints_valid { | ||
| 4463 | - let _ = self.update_size_hints(window); | ||
| 4464 | - hints_valid = self | ||
| 4465 | - .clients | ||
| 4466 | - .get(&window) | ||
| 4467 | - .map(|c| c.hints_valid) | ||
| 4468 | - .unwrap_or(false); | ||
| 4469 | - } | ||
| 4470 | - | ||
| 4471 | - if hints_valid { | ||
| 4472 | - let ( | ||
| 4473 | - base_width, | ||
| 4474 | - base_height, | ||
| 4475 | - min_width, | ||
| 4476 | - min_height, | ||
| 4477 | - max_width, | ||
| 4478 | - max_height, | ||
| 4479 | - inc_width, | ||
| 4480 | - inc_height, | ||
| 4481 | - min_aspect, | ||
| 4482 | - max_aspect, | ||
| 4483 | - ) = { | ||
| 4484 | - let client = self.clients.get(&window).unwrap(); | ||
| 4485 | - ( | ||
| 4486 | - client.base_width, | ||
| 4487 | - client.base_height, | ||
| 4488 | - client.min_width, | ||
| 4489 | - client.min_height, | ||
| 4490 | - client.max_width, | ||
| 4491 | - client.max_height, | ||
| 4492 | - client.increment_width, | ||
| 4493 | - client.increment_height, | ||
| 4494 | - client.min_aspect, | ||
| 4495 | - client.max_aspect, | ||
| 4496 | - ) | ||
| 4497 | - }; | ||
| 4498 | - | ||
| 4499 | - let base_is_min = base_width == min_width && base_height == min_height; | ||
| 4500 | - | ||
| 4501 | - if !base_is_min { | ||
| 4502 | - w -= base_width; | ||
| 4503 | - h -= base_height; | ||
| 4504 | - } | ||
| 4505 | - | ||
| 4506 | - if min_aspect > 0.0 && max_aspect > 0.0 { | ||
| 4507 | - if max_aspect < (w as f32 / h as f32) { | ||
| 4508 | - w = (h as f32 * max_aspect + 0.5) as i32; | ||
| 4509 | - } else if min_aspect < (h as f32 / w as f32) { | ||
| 4510 | - h = (w as f32 * min_aspect + 0.5) as i32; | ||
| 4511 | - } | ||
| 4512 | - } | ||
| 4513 | - | ||
| 4514 | - if base_is_min { | ||
| 4515 | - w -= base_width; | ||
| 4516 | - h -= base_height; | ||
| 4517 | - } | ||
| 4518 | - | ||
| 4519 | - if inc_width > 0 { | ||
| 4520 | - w -= w % inc_width; | ||
| 4521 | - } | ||
| 4522 | - if inc_height > 0 { | ||
| 4523 | - h -= h % inc_height; | ||
| 4524 | - } | ||
| 4525 | - | ||
| 4526 | - w = (w + base_width).max(min_width); | ||
| 4527 | - h = (h + base_height).max(min_height); | ||
| 4528 | - | ||
| 4529 | - if max_width > 0 { | ||
| 4530 | - w = w.min(max_width); | ||
| 4531 | - } | ||
| 4532 | - if max_height > 0 { | ||
| 4533 | - h = h.min(max_height); | ||
| 4534 | - } | ||
| 4535 | - } | ||
| 4536 | - } | ||
| 4537 | - | ||
| 4538 | - let changed = x != client_x || y != client_y || w != client_w || h != client_h; | ||
| 4539 | - (x, y, w, h, changed) | ||
| 4540 | - } | ||
| 4541 | - | ||
| 4542 | - fn next_tiled(&self, start: Option<Window>, monitor: &Monitor) -> Option<Window> { | ||
| 4543 | - let mut current = start; | ||
| 4544 | - while let Some(window) = current { | ||
| 4545 | - if let Some(client) = self.clients.get(&window) { | ||
| 4546 | - let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; | ||
| 4547 | - if visible_tags != 0 && !client.is_floating { | ||
| 4548 | - return Some(window); | ||
| 4549 | - } | ||
| 4550 | - current = client.next; | ||
| 4551 | - } else { | ||
| 4552 | - break; | ||
| 4553 | - } | ||
| 4554 | - } | ||
| 4555 | - None | ||
| 4556 | - } | ||
| 4557 | - | ||
| 4558 | - fn next_tagged(&self, start: Option<Window>, tags: u32) -> Option<Window> { | ||
| 4559 | - let mut current = start; | ||
| 4560 | - while let Some(window) = current { | ||
| 4561 | - if let Some(client) = self.clients.get(&window) { | ||
| 4562 | - let visible_on_tags = (client.tags & tags) != 0; | ||
| 4563 | - if !client.is_floating && visible_on_tags { | ||
| 4564 | - return Some(window); | ||
| 4565 | - } | ||
| 4566 | - current = client.next; | ||
| 4567 | - } else { | ||
| 4568 | - break; | ||
| 4569 | - } | ||
| 4570 | - } | ||
| 4571 | - None | ||
| 4572 | - } | ||
| 4573 | - | ||
| 4574 | - fn attach(&mut self, window: Window, monitor_index: usize) { | ||
| 4575 | - if let Some(monitor) = self.monitors.get_mut(monitor_index) | ||
| 4576 | - && let Some(client) = self.clients.get_mut(&window) | ||
| 4577 | - { | ||
| 4578 | - client.next = monitor.clients_head; | ||
| 4579 | - monitor.clients_head = Some(window); | ||
| 4580 | - } | ||
| 4581 | - } | ||
| 4582 | - | ||
| 4583 | - fn attach_after(&mut self, window: Window, after_window: Window, monitor_index: usize) { | ||
| 4584 | - if let Some(after_client) = self.clients.get(&after_window) { | ||
| 4585 | - let old_next = after_client.next; | ||
| 4586 | - if let Some(new_client) = self.clients.get_mut(&window) { | ||
| 4587 | - new_client.next = old_next; | ||
| 4588 | - } | ||
| 4589 | - if let Some(after_client_mut) = self.clients.get_mut(&after_window) { | ||
| 4590 | - after_client_mut.next = Some(window); | ||
| 4591 | - } | ||
| 4592 | - } else { | ||
| 4593 | - self.attach(window, monitor_index); | ||
| 4594 | - } | ||
| 4595 | - } | ||
| 4596 | - | ||
| 4597 | - fn attach_aside(&mut self, window: Window, monitor_index: usize) { | ||
| 4598 | - let monitor = match self.monitors.get(monitor_index) { | ||
| 4599 | - Some(m) => m, | ||
| 4600 | - None => return, | ||
| 4601 | - }; | ||
| 4602 | - | ||
| 4603 | - let new_window_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(0); | ||
| 4604 | - let first_tagged = self.next_tagged(monitor.clients_head, new_window_tags); | ||
| 4605 | - | ||
| 4606 | - if first_tagged.is_none() { | ||
| 4607 | - self.attach(window, monitor_index); | ||
| 4608 | - return; | ||
| 4609 | - } | ||
| 4610 | - | ||
| 4611 | - if let Some(insert_after_window) = first_tagged | ||
| 4612 | - && let Some(after_client) = self.clients.get(&insert_after_window) | ||
| 4613 | - { | ||
| 4614 | - let old_next = after_client.next; | ||
| 4615 | - if let Some(new_client) = self.clients.get_mut(&window) { | ||
| 4616 | - new_client.next = old_next; | ||
| 4617 | - } | ||
| 4618 | - if let Some(after_client_mut) = self.clients.get_mut(&insert_after_window) { | ||
| 4619 | - after_client_mut.next = Some(window); | ||
| 4620 | - } | ||
| 4621 | - } | ||
| 4622 | - } | ||
| 4623 | - | ||
| 4624 | - fn detach(&mut self, window: Window) { | ||
| 4625 | - let monitor_index = self.clients.get(&window).map(|c| c.monitor_index); | ||
| 4626 | - if let Some(monitor_index) = monitor_index | ||
| 4627 | - && let Some(monitor) = self.monitors.get_mut(monitor_index) | ||
| 4628 | - { | ||
| 4629 | - if monitor.clients_head == Some(window) { | ||
| 4630 | - if let Some(client) = self.clients.get(&window) { | ||
| 4631 | - monitor.clients_head = client.next; | ||
| 4632 | - } | ||
| 4633 | - } else { | ||
| 4634 | - let mut current = monitor.clients_head; | ||
| 4635 | - while let Some(current_window) = current { | ||
| 4636 | - if let Some(current_client) = self.clients.get(¤t_window) { | ||
| 4637 | - if current_client.next == Some(window) { | ||
| 4638 | - let new_next = self.clients.get(&window).and_then(|c| c.next); | ||
| 4639 | - if let Some(current_client_mut) = self.clients.get_mut(¤t_window) | ||
| 4640 | - { | ||
| 4641 | - current_client_mut.next = new_next; | ||
| 4642 | - } | ||
| 4643 | - break; | ||
| 4644 | - } | ||
| 4645 | - current = current_client.next; | ||
| 4646 | - } else { | ||
| 4647 | - break; | ||
| 4648 | - } | ||
| 4649 | - } | ||
| 4650 | - } | ||
| 4651 | - } | ||
| 4652 | - } | ||
| 4653 | - | ||
| 4654 | - fn attach_stack(&mut self, window: Window, monitor_index: usize) { | ||
| 4655 | - if let Some(monitor) = self.monitors.get_mut(monitor_index) | ||
| 4656 | - && let Some(client) = self.clients.get_mut(&window) | ||
| 4657 | - { | ||
| 4658 | - client.stack_next = monitor.stack_head; | ||
| 4659 | - monitor.stack_head = Some(window); | ||
| 4660 | - } | ||
| 4661 | - } | ||
| 4662 | - | ||
| 4663 | - fn detach_stack(&mut self, window: Window) { | ||
| 4664 | - let monitor_index = self.clients.get(&window).map(|c| c.monitor_index); | ||
| 4665 | - if let Some(monitor_index) = monitor_index | ||
| 4666 | - && let Some(monitor) = self.monitors.get_mut(monitor_index) | ||
| 4667 | - { | ||
| 4668 | - if monitor.stack_head == Some(window) { | ||
| 4669 | - if let Some(client) = self.clients.get(&window) { | ||
| 4670 | - monitor.stack_head = client.stack_next; | ||
| 4671 | - } | ||
| 4672 | - let should_update_selected = monitor.selected_client == Some(window); | ||
| 4673 | - let mut new_selected: Option<Window> = None; | ||
| 4674 | - if should_update_selected { | ||
| 4675 | - let mut stack_current = monitor.stack_head; | ||
| 4676 | - while let Some(stack_window) = stack_current { | ||
| 4677 | - if let Some(stack_client) = self.clients.get(&stack_window) { | ||
| 4678 | - if self.is_window_visible(stack_window) { | ||
| 4679 | - new_selected = Some(stack_window); | ||
| 4680 | - break; | ||
| 4681 | - } | ||
| 4682 | - stack_current = stack_client.stack_next; | ||
| 4683 | - } else { | ||
| 4684 | - break; | ||
| 4685 | - } | ||
| 4686 | - } | ||
| 4687 | - } | ||
| 4688 | - if should_update_selected | ||
| 4689 | - && let Some(monitor) = self.monitors.get_mut(monitor_index) | ||
| 4690 | - { | ||
| 4691 | - monitor.selected_client = new_selected; | ||
| 4692 | - } | ||
| 4693 | - } else { | ||
| 4694 | - let mut current = monitor.stack_head; | ||
| 4695 | - while let Some(current_window) = current { | ||
| 4696 | - if let Some(current_client) = self.clients.get(¤t_window) { | ||
| 4697 | - if current_client.stack_next == Some(window) { | ||
| 4698 | - let new_stack_next = | ||
| 4699 | - self.clients.get(&window).and_then(|c| c.stack_next); | ||
| 4700 | - if let Some(current_client_mut) = self.clients.get_mut(¤t_window) | ||
| 4701 | - { | ||
| 4702 | - current_client_mut.stack_next = new_stack_next; | ||
| 4703 | - } | ||
| 4704 | - break; | ||
| 4705 | - } | ||
| 4706 | - current = current_client.stack_next; | ||
| 4707 | - } else { | ||
| 4708 | - break; | ||
| 4709 | - } | ||
| 4710 | - } | ||
| 4711 | - } | ||
| 4712 | - } | ||
| 4713 | - } | ||
| 4714 | - | ||
| 4715 | - fn remove_window(&mut self, window: Window, destroyed: bool) -> WmResult<()> { | ||
| 4716 | - let initial_count = self.windows.len(); | ||
| 4717 | - | ||
| 4718 | - let focused = self | ||
| 4719 | - .monitors | ||
| 4720 | - .get(self.selected_monitor) | ||
| 4721 | - .and_then(|m| m.selected_client); | ||
| 4722 | - | ||
| 4723 | - if !destroyed { | ||
| 4724 | - if let Some(client) = self.clients.get(&window) { | ||
| 4725 | - let old_border_width = client.old_border_width; | ||
| 4726 | - self.connection.configure_window( | ||
| 4727 | - window, | ||
| 4728 | - &ConfigureWindowAux::new().border_width(old_border_width as u32), | ||
| 4729 | - )?; | ||
| 4730 | - } | ||
| 4731 | - self.set_wm_state(window, 0)?; | ||
| 4732 | - } | ||
| 4733 | - | ||
| 4734 | - if self.clients.contains_key(&window) { | ||
| 4735 | - self.detach(window); | ||
| 4736 | - self.detach_stack(window); | ||
| 4737 | - self.clients.remove(&window); | ||
| 4738 | - } | ||
| 4739 | - | ||
| 4740 | - self.windows.retain(|&w| w != window); | ||
| 4741 | - self.floating_windows.remove(&window); | ||
| 4742 | - self.update_client_list()?; | ||
| 4743 | - | ||
| 4744 | - if self.windows.len() < initial_count { | ||
| 4745 | - if focused == Some(window) { | ||
| 4746 | - let visible = self.visible_windows_on_monitor(self.selected_monitor); | ||
| 4747 | - if let Some(&new_win) = visible.last() { | ||
| 4748 | - self.focus(Some(new_win))?; | ||
| 4749 | - if self.layout.name() == "scrolling" { | ||
| 4750 | - self.scroll_to_window(new_win, true)?; | ||
| 4751 | - } | ||
| 4752 | - } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { | ||
| 4753 | - monitor.selected_client = None; | ||
| 4754 | - } | ||
| 4755 | - } | ||
| 4756 | - | ||
| 4757 | - self.apply_layout()?; | ||
| 4758 | - self.update_bar()?; | ||
| 4759 | - } | ||
| 4760 | - Ok(()) | ||
| 4761 | - } | ||
| 4762 | - | ||
| 4763 | - fn get_selected_monitor(&self) -> &Monitor { | ||
| 4764 | - &self.monitors[self.selected_monitor] | ||
| 4765 | - } | ||
| 4766 | - | ||
| 4767 | - fn has_windows_on_tag(&self, monitor_number: usize, tag_index: usize) -> bool { | ||
| 4768 | - let Some(monitor) = self.monitors.get(monitor_number) else { | ||
| 4769 | - return false; | ||
| 4770 | - }; | ||
| 4771 | - | ||
| 4772 | - let mut current = monitor.clients_head; | ||
| 4773 | - while let Some(window) = current { | ||
| 4774 | - let Some(client) = self.clients.get(&window) else { | ||
| 4775 | - break; | ||
| 4776 | - }; | ||
| 4777 | - | ||
| 4778 | - if unmask_tag(client.tags) == tag_index { | ||
| 4779 | - return true; | ||
| 4780 | - } | ||
| 4781 | - current = client.next; | ||
| 4782 | - } | ||
| 4783 | - | ||
| 4784 | - false | ||
| 4785 | - } | ||
| 4786 | - | ||
| 4787 | - fn run_autostart_commands(&self) { | ||
| 4788 | - for command in &self.config.autostart { | ||
| 4789 | - crate::signal::spawn_detached(command); | ||
| 4790 | - eprintln!("[autostart] Spawned: {}", command); | ||
| 4791 | - } | ||
| 4792 | - } | ||
| 4793 | -} | ||
+100 −0 src/x11/display.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,100 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const xlib = @import("xlib.zig"); | ||
| 3 | + | ||
| 4 | +pub const DisplayError = error{ | ||
| 5 | + cannot_open_display, | ||
| 6 | + another_wm_running, | ||
| 7 | +}; | ||
| 8 | + | ||
| 9 | +var wm_detected: bool = false; | ||
| 10 | + | ||
| 11 | +pub const Display = struct { | ||
| 12 | + handle: *xlib.Display, | ||
| 13 | + screen: c_int, | ||
| 14 | + root: xlib.Window, | ||
| 15 | + | ||
| 16 | + pub fn open() DisplayError!Display { | ||
| 17 | + const handle = xlib.XOpenDisplay(null) orelse return DisplayError.cannot_open_display; | ||
| 18 | + const screen = xlib.XDefaultScreen(handle); | ||
| 19 | + const root = xlib.XRootWindow(handle, screen); | ||
| 20 | + | ||
| 21 | + return Display{ | ||
| 22 | + .handle = handle, | ||
| 23 | + .screen = screen, | ||
| 24 | + .root = root, | ||
| 25 | + }; | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + pub fn close(self: *Display) void { | ||
| 29 | + _ = xlib.XCloseDisplay(self.handle); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + pub fn become_window_manager(self: *Display) DisplayError!void { | ||
| 33 | + wm_detected = false; | ||
| 34 | + _ = xlib.XSetErrorHandler(on_wm_detected); | ||
| 35 | + _ = xlib.XSelectInput( | ||
| 36 | + self.handle, | ||
| 37 | + self.root, | ||
| 38 | + xlib.SubstructureRedirectMask | xlib.SubstructureNotifyMask | xlib.ButtonPressMask | xlib.PointerMotionMask | xlib.EnterWindowMask, | ||
| 39 | + ); | ||
| 40 | + _ = xlib.XSync(self.handle, xlib.False); | ||
| 41 | + | ||
| 42 | + if (wm_detected) { | ||
| 43 | + return DisplayError.another_wm_running; | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + _ = xlib.XSetErrorHandler(on_x_error); | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | + pub fn screen_width(self: *Display) c_int { | ||
| 50 | + return xlib.XDisplayWidth(self.handle, self.screen); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + pub fn screen_height(self: *Display) c_int { | ||
| 54 | + return xlib.XDisplayHeight(self.handle, self.screen); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + pub fn next_event(self: *Display) xlib.XEvent { | ||
| 58 | + var event: xlib.XEvent = undefined; | ||
| 59 | + _ = xlib.XNextEvent(self.handle, &event); | ||
| 60 | + return event; | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + pub fn pending(self: *Display) c_int { | ||
| 64 | + return xlib.XPending(self.handle); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + pub fn sync(self: *Display, discard: bool) void { | ||
| 68 | + _ = xlib.XSync(self.handle, if (discard) xlib.True else xlib.False); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + pub fn grab_key( | ||
| 72 | + self: *Display, | ||
| 73 | + keycode: c_int, | ||
| 74 | + modifiers: c_uint, | ||
| 75 | + ) void { | ||
| 76 | + _ = xlib.XGrabKey( | ||
| 77 | + self.handle, | ||
| 78 | + keycode, | ||
| 79 | + modifiers, | ||
| 80 | + self.root, | ||
| 81 | + xlib.True, | ||
| 82 | + xlib.GrabModeAsync, | ||
| 83 | + xlib.GrabModeAsync, | ||
| 84 | + ); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + pub fn keysym_to_keycode(self: *Display, keysym: xlib.KeySym) c_int { | ||
| 88 | + return @intCast(xlib.XKeysymToKeycode(self.handle, keysym)); | ||
| 89 | + } | ||
| 90 | +}; | ||
| 91 | + | ||
| 92 | +fn on_wm_detected(_: ?*xlib.Display, _: [*c]xlib.XErrorEvent) callconv(.c) c_int { | ||
| 93 | + wm_detected = true; | ||
| 94 | + return 0; | ||
| 95 | +} | ||
| 96 | + | ||
| 97 | +fn on_x_error(_: ?*xlib.Display, event: [*c]xlib.XErrorEvent) callconv(.c) c_int { | ||
| 98 | + std.debug.print("x11 error: request={d} error={d}\n", .{ event.*.request_code, event.*.error_code }); | ||
| 99 | + return 0; | ||
| 100 | +} | ||
+84 −0 src/x11/events.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,84 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const xlib = @import("xlib.zig"); | ||
| 3 | + | ||
| 4 | +pub const EventType = enum(c_int) { | ||
| 5 | + key_press = xlib.KeyPress, | ||
| 6 | + key_release = xlib.KeyRelease, | ||
| 7 | + button_press = xlib.ButtonPress, | ||
| 8 | + button_release = xlib.ButtonRelease, | ||
| 9 | + motion_notify = xlib.MotionNotify, | ||
| 10 | + enter_notify = xlib.EnterNotify, | ||
| 11 | + leave_notify = xlib.LeaveNotify, | ||
| 12 | + focus_in = xlib.FocusIn, | ||
| 13 | + focus_out = xlib.FocusOut, | ||
| 14 | + keymap_notify = xlib.KeymapNotify, | ||
| 15 | + expose = xlib.Expose, | ||
| 16 | + graphics_expose = xlib.GraphicsExpose, | ||
| 17 | + no_expose = xlib.NoExpose, | ||
| 18 | + visibility_notify = xlib.VisibilityNotify, | ||
| 19 | + create_notify = xlib.CreateNotify, | ||
| 20 | + destroy_notify = xlib.DestroyNotify, | ||
| 21 | + unmap_notify = xlib.UnmapNotify, | ||
| 22 | + map_notify = xlib.MapNotify, | ||
| 23 | + map_request = xlib.MapRequest, | ||
| 24 | + reparent_notify = xlib.ReparentNotify, | ||
| 25 | + configure_notify = xlib.ConfigureNotify, | ||
| 26 | + configure_request = xlib.ConfigureRequest, | ||
| 27 | + gravity_notify = xlib.GravityNotify, | ||
| 28 | + resize_request = xlib.ResizeRequest, | ||
| 29 | + circulate_notify = xlib.CirculateNotify, | ||
| 30 | + circulate_request = xlib.CirculateRequest, | ||
| 31 | + property_notify = xlib.PropertyNotify, | ||
| 32 | + selection_clear = xlib.SelectionClear, | ||
| 33 | + selection_request = xlib.SelectionRequest, | ||
| 34 | + selection_notify = xlib.SelectionNotify, | ||
| 35 | + colormap_notify = xlib.ColormapNotify, | ||
| 36 | + client_message = xlib.ClientMessage, | ||
| 37 | + mapping_notify = xlib.MappingNotify, | ||
| 38 | + generic_event = xlib.GenericEvent, | ||
| 39 | + _, | ||
| 40 | +}; | ||
| 41 | + | ||
| 42 | +pub fn get_event_type(event: *const xlib.XEvent) EventType { | ||
| 43 | + return @enumFromInt(event.type); | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +pub fn event_name(event_type: EventType) []const u8 { | ||
| 47 | + return switch (event_type) { | ||
| 48 | + .key_press => "key_press", | ||
| 49 | + .key_release => "key_release", | ||
| 50 | + .button_press => "button_press", | ||
| 51 | + .button_release => "button_release", | ||
| 52 | + .motion_notify => "motion_notify", | ||
| 53 | + .enter_notify => "enter_notify", | ||
| 54 | + .leave_notify => "leave_notify", | ||
| 55 | + .focus_in => "focus_in", | ||
| 56 | + .focus_out => "focus_out", | ||
| 57 | + .keymap_notify => "keymap_notify", | ||
| 58 | + .expose => "expose", | ||
| 59 | + .graphics_expose => "graphics_expose", | ||
| 60 | + .no_expose => "no_expose", | ||
| 61 | + .visibility_notify => "visibility_notify", | ||
| 62 | + .create_notify => "create_notify", | ||
| 63 | + .destroy_notify => "destroy_notify", | ||
| 64 | + .unmap_notify => "unmap_notify", | ||
| 65 | + .map_notify => "map_notify", | ||
| 66 | + .map_request => "map_request", | ||
| 67 | + .reparent_notify => "reparent_notify", | ||
| 68 | + .configure_notify => "configure_notify", | ||
| 69 | + .configure_request => "configure_request", | ||
| 70 | + .gravity_notify => "gravity_notify", | ||
| 71 | + .resize_request => "resize_request", | ||
| 72 | + .circulate_notify => "circulate_notify", | ||
| 73 | + .circulate_request => "circulate_request", | ||
| 74 | + .property_notify => "property_notify", | ||
| 75 | + .selection_clear => "selection_clear", | ||
| 76 | + .selection_request => "selection_request", | ||
| 77 | + .selection_notify => "selection_notify", | ||
| 78 | + .colormap_notify => "colormap_notify", | ||
| 79 | + .client_message => "client_message", | ||
| 80 | + .mapping_notify => "mapping_notify", | ||
| 81 | + .generic_event => "generic_event", | ||
| 82 | + _ => "unknown", | ||
| 83 | + }; | ||
| 84 | +} | ||
+268 −0 src/x11/xlib.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,268 @@ | |||
| 1 | +pub const c = @cImport({ | ||
| 2 | + @cInclude("X11/Xlib.h"); | ||
| 3 | + @cInclude("X11/Xutil.h"); | ||
| 4 | + @cInclude("X11/Xatom.h"); | ||
| 5 | + @cInclude("X11/cursorfont.h"); | ||
| 6 | + @cInclude("X11/keysym.h"); | ||
| 7 | + @cInclude("X11/extensions/Xinerama.h"); | ||
| 8 | + @cInclude("X11/Xft/Xft.h"); | ||
| 9 | +}); | ||
| 10 | + | ||
| 11 | +pub const Display = c.Display; | ||
| 12 | +pub const Window = c.Window; | ||
| 13 | +pub const XEvent = c.XEvent; | ||
| 14 | +pub const XWindowAttributes = c.XWindowAttributes; | ||
| 15 | +pub const XWindowChanges = c.XWindowChanges; | ||
| 16 | +pub const XMapRequestEvent = c.XMapRequestEvent; | ||
| 17 | +pub const XConfigureRequestEvent = c.XConfigureRequestEvent; | ||
| 18 | +pub const XKeyEvent = c.XKeyEvent; | ||
| 19 | +pub const XDestroyWindowEvent = c.XDestroyWindowEvent; | ||
| 20 | +pub const XUnmapEvent = c.XUnmapEvent; | ||
| 21 | +pub const XCrossingEvent = c.XCrossingEvent; | ||
| 22 | +pub const XFocusChangeEvent = c.XFocusChangeEvent; | ||
| 23 | +pub const XErrorEvent = c.XErrorEvent; | ||
| 24 | +pub const KeySym = c.KeySym; | ||
| 25 | + | ||
| 26 | +pub const XOpenDisplay = c.XOpenDisplay; | ||
| 27 | +pub const XCloseDisplay = c.XCloseDisplay; | ||
| 28 | +pub const XConnectionNumber = c.XConnectionNumber; | ||
| 29 | +pub const XDefaultScreen = c.XDefaultScreen; | ||
| 30 | +pub const XRootWindow = c.XRootWindow; | ||
| 31 | +pub const XDisplayWidth = c.XDisplayWidth; | ||
| 32 | +pub const XDisplayHeight = c.XDisplayHeight; | ||
| 33 | +pub const XNextEvent = c.XNextEvent; | ||
| 34 | +pub const XPending = c.XPending; | ||
| 35 | +pub const XSync = c.XSync; | ||
| 36 | +pub const XSelectInput = c.XSelectInput; | ||
| 37 | +pub const XSetErrorHandler = c.XSetErrorHandler; | ||
| 38 | +pub const XGrabKey = c.XGrabKey; | ||
| 39 | +pub const XKeysymToKeycode = c.XKeysymToKeycode; | ||
| 40 | +pub const XKeycodeToKeysym = c.XKeycodeToKeysym; | ||
| 41 | +pub const XQueryTree = c.XQueryTree; | ||
| 42 | +pub const XFree = c.XFree; | ||
| 43 | +pub const XGetWindowAttributes = c.XGetWindowAttributes; | ||
| 44 | +pub const XMapWindow = c.XMapWindow; | ||
| 45 | +pub const XConfigureWindow = c.XConfigureWindow; | ||
| 46 | +pub const XSetInputFocus = c.XSetInputFocus; | ||
| 47 | +pub const XRaiseWindow = c.XRaiseWindow; | ||
| 48 | +pub const XMoveResizeWindow = c.XMoveResizeWindow; | ||
| 49 | +pub const XMoveWindow = c.XMoveWindow; | ||
| 50 | +pub const XSetWindowBorder = c.XSetWindowBorder; | ||
| 51 | +pub const XSetWindowBorderWidth = c.XSetWindowBorderWidth; | ||
| 52 | + | ||
| 53 | +pub const SubstructureRedirectMask = c.SubstructureRedirectMask; | ||
| 54 | +pub const SubstructureNotifyMask = c.SubstructureNotifyMask; | ||
| 55 | +pub const EnterWindowMask = c.EnterWindowMask; | ||
| 56 | +pub const FocusChangeMask = c.FocusChangeMask; | ||
| 57 | +pub const PropertyChangeMask = c.PropertyChangeMask; | ||
| 58 | +pub const StructureNotifyMask = c.StructureNotifyMask; | ||
| 59 | + | ||
| 60 | +pub const Mod4Mask = c.Mod4Mask; | ||
| 61 | +pub const ShiftMask = c.ShiftMask; | ||
| 62 | +pub const LockMask = c.LockMask; | ||
| 63 | +pub const Mod2Mask = c.Mod2Mask; | ||
| 64 | +pub const ControlMask = c.ControlMask; | ||
| 65 | + | ||
| 66 | +pub const GrabModeAsync = c.GrabModeAsync; | ||
| 67 | +pub const RevertToPointerRoot = c.RevertToPointerRoot; | ||
| 68 | +pub const CurrentTime = c.CurrentTime; | ||
| 69 | +pub const NotifyNormal = c.NotifyNormal; | ||
| 70 | +pub const NotifyInferior = c.NotifyInferior; | ||
| 71 | + | ||
| 72 | +pub const True = c.True; | ||
| 73 | +pub const False = c.False; | ||
| 74 | + | ||
| 75 | +pub const XK_q = c.XK_q; | ||
| 76 | +pub const XK_f = c.XK_f; | ||
| 77 | +pub const XK_h = c.XK_h; | ||
| 78 | +pub const XK_i = c.XK_i; | ||
| 79 | +pub const XK_d = c.XK_d; | ||
| 80 | +pub const XK_j = c.XK_j; | ||
| 81 | +pub const XK_k = c.XK_k; | ||
| 82 | +pub const XK_l = c.XK_l; | ||
| 83 | +pub const XK_m = c.XK_m; | ||
| 84 | +pub const XK_comma = c.XK_comma; | ||
| 85 | +pub const XK_period = c.XK_period; | ||
| 86 | +pub const XK_space = c.XK_space; | ||
| 87 | +pub const XK_Return = c.XK_Return; | ||
| 88 | +pub const XK_p = c.XK_p; | ||
| 89 | +pub const XK_a = c.XK_a; | ||
| 90 | +pub const XK_s = c.XK_s; | ||
| 91 | +pub const XK_1 = c.XK_1; | ||
| 92 | +pub const XK_2 = c.XK_2; | ||
| 93 | +pub const XK_3 = c.XK_3; | ||
| 94 | +pub const XK_4 = c.XK_4; | ||
| 95 | +pub const XK_5 = c.XK_5; | ||
| 96 | +pub const XK_6 = c.XK_6; | ||
| 97 | +pub const XK_7 = c.XK_7; | ||
| 98 | +pub const XK_8 = c.XK_8; | ||
| 99 | +pub const XK_9 = c.XK_9; | ||
| 100 | + | ||
| 101 | +pub const Mod1Mask = c.Mod1Mask; | ||
| 102 | +pub const Mod3Mask = c.Mod3Mask; | ||
| 103 | +pub const Mod5Mask = c.Mod5Mask; | ||
| 104 | + | ||
| 105 | +pub const XKillClient = c.XKillClient; | ||
| 106 | +pub const XInternAtom = c.XInternAtom; | ||
| 107 | +pub const XChangeProperty = c.XChangeProperty; | ||
| 108 | +pub const XGetWindowProperty = c.XGetWindowProperty; | ||
| 109 | +pub const XSendEvent = c.XSendEvent; | ||
| 110 | + | ||
| 111 | +pub const Atom = c.Atom; | ||
| 112 | +pub const XA_ATOM = c.XA_ATOM; | ||
| 113 | +pub const XClientMessageEvent = c.XClientMessageEvent; | ||
| 114 | + | ||
| 115 | +pub const PropModeReplace = c.PropModeReplace; | ||
| 116 | + | ||
| 117 | +pub const XGrabPointer = c.XGrabPointer; | ||
| 118 | +pub const XUngrabPointer = c.XUngrabPointer; | ||
| 119 | +pub const XGrabButton = c.XGrabButton; | ||
| 120 | +pub const XQueryPointer = c.XQueryPointer; | ||
| 121 | +pub const XWarpPointer = c.XWarpPointer; | ||
| 122 | +pub const XGetModifierMapping = c.XGetModifierMapping; | ||
| 123 | +pub const XFreeModifiermap = c.XFreeModifiermap; | ||
| 124 | +pub const XModifierKeymap = c.XModifierKeymap; | ||
| 125 | +pub const XK_Num_Lock = c.XK_Num_Lock; | ||
| 126 | + | ||
| 127 | +pub const Button1 = c.Button1; | ||
| 128 | +pub const Button1Mask = c.Button1Mask; | ||
| 129 | +pub const Button3 = c.Button3; | ||
| 130 | +pub const Button3Mask = c.Button3Mask; | ||
| 131 | +pub const ButtonPressMask = c.ButtonPressMask; | ||
| 132 | +pub const ButtonReleaseMask = c.ButtonReleaseMask; | ||
| 133 | +pub const PointerMotionMask = c.PointerMotionMask; | ||
| 134 | +pub const GrabModeSync = c.GrabModeSync; | ||
| 135 | +pub const GrabSuccess = c.GrabSuccess; | ||
| 136 | +pub const None = c.None; | ||
| 137 | + | ||
| 138 | +pub const XButtonEvent = c.XButtonEvent; | ||
| 139 | +pub const XMotionEvent = c.XMotionEvent; | ||
| 140 | +pub const XExposeEvent = c.XExposeEvent; | ||
| 141 | + | ||
| 142 | +pub const XineramaIsActive = c.XineramaIsActive; | ||
| 143 | +pub const XineramaQueryScreens = c.XineramaQueryScreens; | ||
| 144 | +pub const XineramaScreenInfo = c.XineramaScreenInfo; | ||
| 145 | + | ||
| 146 | +pub const XftFont = c.XftFont; | ||
| 147 | +pub const XftColor = c.XftColor; | ||
| 148 | +pub const XftDraw = c.XftDraw; | ||
| 149 | +pub const XftFontOpenName = c.XftFontOpenName; | ||
| 150 | +pub const XftFontClose = c.XftFontClose; | ||
| 151 | +pub const XftDrawCreate = c.XftDrawCreate; | ||
| 152 | +pub const XftDrawDestroy = c.XftDrawDestroy; | ||
| 153 | +pub const XftDrawStringUtf8 = c.XftDrawStringUtf8; | ||
| 154 | +pub const XftColorAllocValue = c.XftColorAllocValue; | ||
| 155 | +pub const XftColorFree = c.XftColorFree; | ||
| 156 | +pub const XftTextExtentsUtf8 = c.XftTextExtentsUtf8; | ||
| 157 | +pub const XGlyphInfo = c.XGlyphInfo; | ||
| 158 | +pub const XRenderColor = c.XRenderColor; | ||
| 159 | + | ||
| 160 | +pub const XCreatePixmap = c.XCreatePixmap; | ||
| 161 | +pub const XFreePixmap = c.XFreePixmap; | ||
| 162 | +pub const XCopyArea = c.XCopyArea; | ||
| 163 | +pub const XCreateGC = c.XCreateGC; | ||
| 164 | +pub const XFreeGC = c.XFreeGC; | ||
| 165 | +pub const XSetForeground = c.XSetForeground; | ||
| 166 | +pub const XFillRectangle = c.XFillRectangle; | ||
| 167 | +pub const XDefaultVisual = c.XDefaultVisual; | ||
| 168 | +pub const XDefaultColormap = c.XDefaultColormap; | ||
| 169 | +pub const XDefaultDepth = c.XDefaultDepth; | ||
| 170 | +pub const Pixmap = c.Pixmap; | ||
| 171 | +pub const Drawable = c.Drawable; | ||
| 172 | +pub const GC = c.GC; | ||
| 173 | +pub const Visual = c.Visual; | ||
| 174 | +pub const Colormap = c.Colormap; | ||
| 175 | + | ||
| 176 | +pub const KeyPress = c.KeyPress; | ||
| 177 | +pub const KeyRelease = c.KeyRelease; | ||
| 178 | +pub const ButtonPress = c.ButtonPress; | ||
| 179 | +pub const ButtonRelease = c.ButtonRelease; | ||
| 180 | +pub const MotionNotify = c.MotionNotify; | ||
| 181 | +pub const EnterNotify = c.EnterNotify; | ||
| 182 | +pub const LeaveNotify = c.LeaveNotify; | ||
| 183 | +pub const FocusIn = c.FocusIn; | ||
| 184 | +pub const FocusOut = c.FocusOut; | ||
| 185 | +pub const KeymapNotify = c.KeymapNotify; | ||
| 186 | +pub const Expose = c.Expose; | ||
| 187 | +pub const GraphicsExpose = c.GraphicsExpose; | ||
| 188 | +pub const NoExpose = c.NoExpose; | ||
| 189 | +pub const VisibilityNotify = c.VisibilityNotify; | ||
| 190 | +pub const CreateNotify = c.CreateNotify; | ||
| 191 | +pub const DestroyNotify = c.DestroyNotify; | ||
| 192 | +pub const UnmapNotify = c.UnmapNotify; | ||
| 193 | +pub const MapNotify = c.MapNotify; | ||
| 194 | +pub const MapRequest = c.MapRequest; | ||
| 195 | +pub const ReparentNotify = c.ReparentNotify; | ||
| 196 | +pub const ConfigureNotify = c.ConfigureNotify; | ||
| 197 | +pub const ConfigureRequest = c.ConfigureRequest; | ||
| 198 | +pub const GravityNotify = c.GravityNotify; | ||
| 199 | +pub const ResizeRequest = c.ResizeRequest; | ||
| 200 | +pub const CirculateNotify = c.CirculateNotify; | ||
| 201 | +pub const CirculateRequest = c.CirculateRequest; | ||
| 202 | +pub const PropertyNotify = c.PropertyNotify; | ||
| 203 | +pub const SelectionClear = c.SelectionClear; | ||
| 204 | +pub const SelectionRequest = c.SelectionRequest; | ||
| 205 | +pub const SelectionNotify = c.SelectionNotify; | ||
| 206 | +pub const ColormapNotify = c.ColormapNotify; | ||
| 207 | +pub const ClientMessage = c.ClientMessage; | ||
| 208 | +pub const MappingNotify = c.MappingNotify; | ||
| 209 | +pub const GenericEvent = c.GenericEvent; | ||
| 210 | + | ||
| 211 | +pub const XClassHint = c.XClassHint; | ||
| 212 | +pub const XGetClassHint = c.XGetClassHint; | ||
| 213 | +pub const XWMHints = c.XWMHints; | ||
| 214 | +pub const XGetWMHints = c.XGetWMHints; | ||
| 215 | +pub const XSetWMHints = c.XSetWMHints; | ||
| 216 | +pub const XSizeHints = c.XSizeHints; | ||
| 217 | +pub const XGetWMNormalHints = c.XGetWMNormalHints; | ||
| 218 | +pub const XGetTransientForHint = c.XGetTransientForHint; | ||
| 219 | +pub const XTextProperty = c.XTextProperty; | ||
| 220 | +pub const XGetTextProperty = c.XGetTextProperty; | ||
| 221 | +pub const XmbTextPropertyToTextList = c.XmbTextPropertyToTextList; | ||
| 222 | +pub const XFreeStringList = c.XFreeStringList; | ||
| 223 | +pub const Success = c.Success; | ||
| 224 | +pub const XGetWMProtocols = c.XGetWMProtocols; | ||
| 225 | +pub const XAllocSizeHints = c.XAllocSizeHints; | ||
| 226 | + | ||
| 227 | +pub const XUrgencyHint = c.XUrgencyHint; | ||
| 228 | +pub const InputHint = c.InputHint; | ||
| 229 | +pub const PBaseSize = c.PBaseSize; | ||
| 230 | +pub const PMinSize = c.PMinSize; | ||
| 231 | +pub const PMaxSize = c.PMaxSize; | ||
| 232 | +pub const PResizeInc = c.PResizeInc; | ||
| 233 | +pub const PAspect = c.PAspect; | ||
| 234 | +pub const PSize = c.PSize; | ||
| 235 | + | ||
| 236 | +pub const XA_WM_NAME = c.XA_WM_NAME; | ||
| 237 | +pub const XA_WINDOW = c.XA_WINDOW; | ||
| 238 | +pub const XA_STRING = c.XA_STRING; | ||
| 239 | +pub const PropModeAppend = c.PropModeAppend; | ||
| 240 | +pub const NoEventMask = c.NoEventMask; | ||
| 241 | + | ||
| 242 | +pub const XPropertyEvent = c.XPropertyEvent; | ||
| 243 | +pub const PropertyDelete = c.PropertyDelete; | ||
| 244 | +pub const XA_WM_TRANSIENT_FOR = c.XA_WM_TRANSIENT_FOR; | ||
| 245 | +pub const XA_WM_NORMAL_HINTS = c.XA_WM_NORMAL_HINTS; | ||
| 246 | +pub const XA_WM_HINTS = c.XA_WM_HINTS; | ||
| 247 | + | ||
| 248 | +pub const XDeleteProperty = c.XDeleteProperty; | ||
| 249 | +pub const XCreateSimpleWindow = c.XCreateSimpleWindow; | ||
| 250 | +pub const XDestroyWindow = c.XDestroyWindow; | ||
| 251 | +pub const XGrabServer = c.XGrabServer; | ||
| 252 | +pub const XUngrabServer = c.XUngrabServer; | ||
| 253 | +pub const XUngrabButton = c.XUngrabButton; | ||
| 254 | +pub const XUngrabKey = c.XUngrabKey; | ||
| 255 | +pub const AnyKey = c.AnyKey; | ||
| 256 | +pub const AnyModifier = c.AnyModifier; | ||
| 257 | + | ||
| 258 | +pub const Cursor = c.Cursor; | ||
| 259 | +pub const XCreateFontCursor = c.XCreateFontCursor; | ||
| 260 | +pub const XFreeCursor = c.XFreeCursor; | ||
| 261 | +pub const XDefineCursor = c.XDefineCursor; | ||
| 262 | +pub const XC_left_ptr = c.XC_left_ptr; | ||
| 263 | +pub const XC_sizing = c.XC_sizing; | ||
| 264 | +pub const XC_fleur = c.XC_fleur; | ||
| 265 | + | ||
| 266 | +pub const XAllowEvents = c.XAllowEvents; | ||
| 267 | +pub const ReplayPointer = c.ReplayPointer; | ||
| 268 | +pub const AnyButton = c.AnyButton; | ||
+88 −0 tests/config_tests.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,88 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const testing = std.testing; | ||
| 3 | + | ||
| 4 | +const Config = struct { | ||
| 5 | + terminal: []const u8 = "st", | ||
| 6 | + font: []const u8 = "monospace:size=10", | ||
| 7 | + border_width: i32 = 2, | ||
| 8 | + border_focused: u32 = 0x6dade3, | ||
| 9 | + border_unfocused: u32 = 0x444444, | ||
| 10 | + gaps_enabled: bool = true, | ||
| 11 | + gap_inner_h: i32 = 5, | ||
| 12 | + gap_inner_v: i32 = 5, | ||
| 13 | + gap_outer_h: i32 = 5, | ||
| 14 | + gap_outer_v: i32 = 5, | ||
| 15 | + modkey: u32 = (1 << 6), | ||
| 16 | + auto_tile: bool = false, | ||
| 17 | + tag_back_and_forth: bool = false, | ||
| 18 | + hide_vacant_tags: bool = false, | ||
| 19 | +}; | ||
| 20 | + | ||
| 21 | +test "config has correct default terminal" { | ||
| 22 | + const cfg = Config{}; | ||
| 23 | + try testing.expectEqualStrings("st", cfg.terminal); | ||
| 24 | +} | ||
| 25 | + | ||
| 26 | +test "config has correct default font" { | ||
| 27 | + const cfg = Config{}; | ||
| 28 | + try testing.expectEqualStrings("monospace:size=10", cfg.font); | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +test "config has correct default border width" { | ||
| 32 | + const cfg = Config{}; | ||
| 33 | + try testing.expectEqual(@as(i32, 2), cfg.border_width); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +test "config has correct default focused border color" { | ||
| 37 | + const cfg = Config{}; | ||
| 38 | + try testing.expectEqual(@as(u32, 0x6dade3), cfg.border_focused); | ||
| 39 | +} | ||
| 40 | + | ||
| 41 | +test "config has correct default unfocused border color" { | ||
| 42 | + const cfg = Config{}; | ||
| 43 | + try testing.expectEqual(@as(u32, 0x444444), cfg.border_unfocused); | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +test "config gaps are enabled by default" { | ||
| 47 | + const cfg = Config{}; | ||
| 48 | + try testing.expect(cfg.gaps_enabled); | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +test "config has correct default gap values" { | ||
| 52 | + const cfg = Config{}; | ||
| 53 | + try testing.expectEqual(@as(i32, 5), cfg.gap_inner_h); | ||
| 54 | + try testing.expectEqual(@as(i32, 5), cfg.gap_inner_v); | ||
| 55 | + try testing.expectEqual(@as(i32, 5), cfg.gap_outer_h); | ||
| 56 | + try testing.expectEqual(@as(i32, 5), cfg.gap_outer_v); | ||
| 57 | +} | ||
| 58 | + | ||
| 59 | +test "config auto_tile is disabled by default" { | ||
| 60 | + const cfg = Config{}; | ||
| 61 | + try testing.expect(!cfg.auto_tile); | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +test "config tag_back_and_forth is disabled by default" { | ||
| 65 | + const cfg = Config{}; | ||
| 66 | + try testing.expect(!cfg.tag_back_and_forth); | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +test "config hide_vacant_tags is disabled by default" { | ||
| 70 | + const cfg = Config{}; | ||
| 71 | + try testing.expect(!cfg.hide_vacant_tags); | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +test "config default modkey is Mod4 (super)" { | ||
| 75 | + const cfg = Config{}; | ||
| 76 | + try testing.expectEqual(@as(u32, 1 << 6), cfg.modkey); | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +test "can override config defaults" { | ||
| 80 | + const cfg = Config{ | ||
| 81 | + .terminal = "alacritty", | ||
| 82 | + .border_width = 3, | ||
| 83 | + .gaps_enabled = false, | ||
| 84 | + }; | ||
| 85 | + try testing.expectEqualStrings("alacritty", cfg.terminal); | ||
| 86 | + try testing.expectEqual(@as(i32, 3), cfg.border_width); | ||
| 87 | + try testing.expect(!cfg.gaps_enabled); | ||
| 88 | +} | ||
+137 −0 tests/keybind_tests.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,137 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const testing = std.testing; | ||
| 3 | + | ||
| 4 | +const Mod4Mask: u32 = 1 << 6; | ||
| 5 | +const Mod1Mask: u32 = 1 << 3; | ||
| 6 | +const ShiftMask: u32 = 1 << 0; | ||
| 7 | +const ControlMask: u32 = 1 << 2; | ||
| 8 | + | ||
| 9 | +fn parse_modifier(name: []const u8) u32 { | ||
| 10 | + if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) { | ||
| 11 | + return Mod4Mask; | ||
| 12 | + } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) { | ||
| 13 | + return Mod1Mask; | ||
| 14 | + } else if (std.mem.eql(u8, name, "Shift") or std.mem.eql(u8, name, "shift")) { | ||
| 15 | + return ShiftMask; | ||
| 16 | + } else if (std.mem.eql(u8, name, "Control") or std.mem.eql(u8, name, "control") or std.mem.eql(u8, name, "ctrl")) { | ||
| 17 | + return ControlMask; | ||
| 18 | + } | ||
| 19 | + return 0; | ||
| 20 | +} | ||
| 21 | + | ||
| 22 | +fn parse_modifiers(names: []const []const u8) u32 { | ||
| 23 | + var mask: u32 = 0; | ||
| 24 | + for (names) |name| { | ||
| 25 | + mask |= parse_modifier(name); | ||
| 26 | + } | ||
| 27 | + return mask; | ||
| 28 | +} | ||
| 29 | + | ||
| 30 | +test "parse_modifier: Mod4 variants" { | ||
| 31 | + try testing.expectEqual(Mod4Mask, parse_modifier("Mod4")); | ||
| 32 | + try testing.expectEqual(Mod4Mask, parse_modifier("mod4")); | ||
| 33 | + try testing.expectEqual(Mod4Mask, parse_modifier("super")); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +test "parse_modifier: Mod1 (Alt) variants" { | ||
| 37 | + try testing.expectEqual(Mod1Mask, parse_modifier("Mod1")); | ||
| 38 | + try testing.expectEqual(Mod1Mask, parse_modifier("mod1")); | ||
| 39 | + try testing.expectEqual(Mod1Mask, parse_modifier("alt")); | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +test "parse_modifier: Shift variants" { | ||
| 43 | + try testing.expectEqual(ShiftMask, parse_modifier("Shift")); | ||
| 44 | + try testing.expectEqual(ShiftMask, parse_modifier("shift")); | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +test "parse_modifier: Control variants" { | ||
| 48 | + try testing.expectEqual(ControlMask, parse_modifier("Control")); | ||
| 49 | + try testing.expectEqual(ControlMask, parse_modifier("control")); | ||
| 50 | + try testing.expectEqual(ControlMask, parse_modifier("ctrl")); | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +test "parse_modifier: unknown returns 0" { | ||
| 54 | + try testing.expectEqual(@as(u32, 0), parse_modifier("Unknown")); | ||
| 55 | + try testing.expectEqual(@as(u32, 0), parse_modifier("")); | ||
| 56 | +} | ||
| 57 | + | ||
| 58 | +test "parse_modifiers: combines multiple modifiers" { | ||
| 59 | + const mods = &[_][]const u8{ "Mod4", "Shift" }; | ||
| 60 | + try testing.expectEqual(Mod4Mask | ShiftMask, parse_modifiers(mods)); | ||
| 61 | +} | ||
| 62 | + | ||
| 63 | +test "parse_modifiers: triple combination" { | ||
| 64 | + const mods = &[_][]const u8{ "super", "shift", "ctrl" }; | ||
| 65 | + try testing.expectEqual(Mod4Mask | ShiftMask | ControlMask, parse_modifiers(mods)); | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +test "parse_modifiers: empty list returns 0" { | ||
| 69 | + const mods = &[_][]const u8{}; | ||
| 70 | + try testing.expectEqual(@as(u32, 0), parse_modifiers(mods)); | ||
| 71 | +} | ||
| 72 | + | ||
| 73 | +fn key_name_to_keysym(name: []const u8) ?u64 { | ||
| 74 | + const key_map = .{ | ||
| 75 | + .{ "Return", 0xff0d }, | ||
| 76 | + .{ "Enter", 0xff0d }, | ||
| 77 | + .{ "Tab", 0xff09 }, | ||
| 78 | + .{ "Escape", 0xff1b }, | ||
| 79 | + .{ "BackSpace", 0xff08 }, | ||
| 80 | + .{ "Delete", 0xffff }, | ||
| 81 | + .{ "space", 0x0020 }, | ||
| 82 | + .{ "Space", 0x0020 }, | ||
| 83 | + }; | ||
| 84 | + | ||
| 85 | + inline for (key_map) |entry| { | ||
| 86 | + if (std.mem.eql(u8, name, entry[0])) { | ||
| 87 | + return entry[1]; | ||
| 88 | + } | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + if (name.len == 1) { | ||
| 92 | + const char = name[0]; | ||
| 93 | + if (char >= 'a' and char <= 'z') { | ||
| 94 | + return char; | ||
| 95 | + } | ||
| 96 | + if (char >= 'A' and char <= 'Z') { | ||
| 97 | + return char + 32; | ||
| 98 | + } | ||
| 99 | + if (char >= '0' and char <= '9') { | ||
| 100 | + return char; | ||
| 101 | + } | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + return null; | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +test "key_name_to_keysym: special keys" { | ||
| 108 | + try testing.expectEqual(@as(?u64, 0xff0d), key_name_to_keysym("Return")); | ||
| 109 | + try testing.expectEqual(@as(?u64, 0xff0d), key_name_to_keysym("Enter")); | ||
| 110 | + try testing.expectEqual(@as(?u64, 0xff09), key_name_to_keysym("Tab")); | ||
| 111 | + try testing.expectEqual(@as(?u64, 0xff1b), key_name_to_keysym("Escape")); | ||
| 112 | +} | ||
| 113 | + | ||
| 114 | +test "key_name_to_keysym: lowercase letters" { | ||
| 115 | + try testing.expectEqual(@as(?u64, 'a'), key_name_to_keysym("a")); | ||
| 116 | + try testing.expectEqual(@as(?u64, 'z'), key_name_to_keysym("z")); | ||
| 117 | +} | ||
| 118 | + | ||
| 119 | +test "key_name_to_keysym: uppercase converts to lowercase" { | ||
| 120 | + try testing.expectEqual(@as(?u64, 'a'), key_name_to_keysym("A")); | ||
| 121 | + try testing.expectEqual(@as(?u64, 'z'), key_name_to_keysym("Z")); | ||
| 122 | +} | ||
| 123 | + | ||
| 124 | +test "key_name_to_keysym: numbers" { | ||
| 125 | + try testing.expectEqual(@as(?u64, '0'), key_name_to_keysym("0")); | ||
| 126 | + try testing.expectEqual(@as(?u64, '9'), key_name_to_keysym("9")); | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +test "key_name_to_keysym: unknown returns null" { | ||
| 130 | + try testing.expectEqual(@as(?u64, null), key_name_to_keysym("UnknownKey")); | ||
| 131 | + try testing.expectEqual(@as(?u64, null), key_name_to_keysym("")); | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +test "key_name_to_keysym: space variants" { | ||
| 135 | + try testing.expectEqual(@as(?u64, 0x0020), key_name_to_keysym("space")); | ||
| 136 | + try testing.expectEqual(@as(?u64, 0x0020), key_name_to_keysym("Space")); | ||
| 137 | +} | ||
+138 −0 tests/layout_tests.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,138 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | +const testing = std.testing; | ||
| 3 | + | ||
| 4 | +const Rect = struct { | ||
| 5 | + x: i32, | ||
| 6 | + y: i32, | ||
| 7 | + w: i32, | ||
| 8 | + h: i32, | ||
| 9 | +}; | ||
| 10 | + | ||
| 11 | +fn calculate_master_stack_layout( | ||
| 12 | + area: Rect, | ||
| 13 | + num_clients: usize, | ||
| 14 | + num_master: usize, | ||
| 15 | + master_factor: f32, | ||
| 16 | + gap: i32, | ||
| 17 | +) []Rect { | ||
| 18 | + var rects: [16]Rect = undefined; | ||
| 19 | + if (num_clients == 0) return rects[0..0]; | ||
| 20 | + | ||
| 21 | + const actual_master = @min(num_master, num_clients); | ||
| 22 | + const stack_count = if (num_clients > actual_master) num_clients - actual_master else 0; | ||
| 23 | + | ||
| 24 | + const master_width: i32 = if (stack_count > 0) | ||
| 25 | + @intFromFloat(@as(f32, @floatFromInt(area.w - gap)) * master_factor) | ||
| 26 | + else | ||
| 27 | + area.w; | ||
| 28 | + | ||
| 29 | + var i: usize = 0; | ||
| 30 | + while (i < actual_master) : (i += 1) { | ||
| 31 | + const h = @divTrunc(area.h - @as(i32, @intCast(actual_master - 1)) * gap, @as(i32, @intCast(actual_master))); | ||
| 32 | + rects[i] = .{ | ||
| 33 | + .x = area.x, | ||
| 34 | + .y = area.y + @as(i32, @intCast(i)) * (h + gap), | ||
| 35 | + .w = master_width, | ||
| 36 | + .h = h, | ||
| 37 | + }; | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + const stack_width = area.w - master_width - gap; | ||
| 41 | + var j: usize = 0; | ||
| 42 | + while (j < stack_count) : (j += 1) { | ||
| 43 | + const h = @divTrunc(area.h - @as(i32, @intCast(stack_count - 1)) * gap, @as(i32, @intCast(stack_count))); | ||
| 44 | + rects[actual_master + j] = .{ | ||
| 45 | + .x = area.x + master_width + gap, | ||
| 46 | + .y = area.y + @as(i32, @intCast(j)) * (h + gap), | ||
| 47 | + .w = stack_width, | ||
| 48 | + .h = h, | ||
| 49 | + }; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + return rects[0..num_clients]; | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +test "tiling layout: single window fills area" { | ||
| 56 | + const area = Rect{ .x = 0, .y = 0, .w = 800, .h = 600 }; | ||
| 57 | + const rects = calculate_master_stack_layout(area, 1, 1, 0.55, 5); | ||
| 58 | + | ||
| 59 | + try testing.expectEqual(@as(usize, 1), rects.len); | ||
| 60 | + try testing.expectEqual(@as(i32, 0), rects[0].x); | ||
| 61 | + try testing.expectEqual(@as(i32, 0), rects[0].y); | ||
| 62 | + try testing.expectEqual(@as(i32, 800), rects[0].w); | ||
| 63 | + try testing.expectEqual(@as(i32, 600), rects[0].h); | ||
| 64 | +} | ||
| 65 | + | ||
| 66 | +test "tiling layout: two windows split horizontally" { | ||
| 67 | + const area = Rect{ .x = 0, .y = 0, .w = 800, .h = 600 }; | ||
| 68 | + const rects = calculate_master_stack_layout(area, 2, 1, 0.5, 0); | ||
| 69 | + | ||
| 70 | + try testing.expectEqual(@as(usize, 2), rects.len); | ||
| 71 | + try testing.expectEqual(@as(i32, 400), rects[0].w); | ||
| 72 | + try testing.expectEqual(@as(i32, 400), rects[1].w); | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +test "tiling layout: respects master factor" { | ||
| 76 | + const area = Rect{ .x = 0, .y = 0, .w = 1000, .h = 600 }; | ||
| 77 | + const rects = calculate_master_stack_layout(area, 2, 1, 0.6, 0); | ||
| 78 | + | ||
| 79 | + try testing.expectEqual(@as(usize, 2), rects.len); | ||
| 80 | + try testing.expectEqual(@as(i32, 600), rects[0].w); | ||
| 81 | + try testing.expectEqual(@as(i32, 400), rects[1].w); | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +test "tiling layout: no clients returns empty" { | ||
| 85 | + const area = Rect{ .x = 0, .y = 0, .w = 800, .h = 600 }; | ||
| 86 | + const rects = calculate_master_stack_layout(area, 0, 1, 0.55, 5); | ||
| 87 | + | ||
| 88 | + try testing.expectEqual(@as(usize, 0), rects.len); | ||
| 89 | +} | ||
| 90 | + | ||
| 91 | +test "tiling layout: multiple stack windows divide height" { | ||
| 92 | + const area = Rect{ .x = 0, .y = 0, .w = 800, .h = 600 }; | ||
| 93 | + const rects = calculate_master_stack_layout(area, 3, 1, 0.5, 0); | ||
| 94 | + | ||
| 95 | + try testing.expectEqual(@as(usize, 3), rects.len); | ||
| 96 | + try testing.expectEqual(@as(i32, 600), rects[0].h); | ||
| 97 | + try testing.expectEqual(@as(i32, 300), rects[1].h); | ||
| 98 | + try testing.expectEqual(@as(i32, 300), rects[2].h); | ||
| 99 | +} | ||
| 100 | + | ||
| 101 | +fn calculate_monocle_layout(area: Rect, num_clients: usize) []Rect { | ||
| 102 | + var rects: [16]Rect = undefined; | ||
| 103 | + if (num_clients == 0) return rects[0..0]; | ||
| 104 | + | ||
| 105 | + var i: usize = 0; | ||
| 106 | + while (i < num_clients) : (i += 1) { | ||
| 107 | + rects[i] = area; | ||
| 108 | + } | ||
| 109 | + return rects[0..num_clients]; | ||
| 110 | +} | ||
| 111 | + | ||
| 112 | +test "monocle layout: all windows get full area" { | ||
| 113 | + const area = Rect{ .x = 0, .y = 0, .w = 800, .h = 600 }; | ||
| 114 | + const rects = calculate_monocle_layout(area, 3); | ||
| 115 | + | ||
| 116 | + try testing.expectEqual(@as(usize, 3), rects.len); | ||
| 117 | + for (rects) |rect| { | ||
| 118 | + try testing.expectEqual(@as(i32, 0), rect.x); | ||
| 119 | + try testing.expectEqual(@as(i32, 0), rect.y); | ||
| 120 | + try testing.expectEqual(@as(i32, 800), rect.w); | ||
| 121 | + try testing.expectEqual(@as(i32, 600), rect.h); | ||
| 122 | + } | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +test "monocle layout: no clients returns empty" { | ||
| 126 | + const area = Rect{ .x = 0, .y = 0, .w = 800, .h = 600 }; | ||
| 127 | + const rects = calculate_monocle_layout(area, 0); | ||
| 128 | + | ||
| 129 | + try testing.expectEqual(@as(usize, 0), rects.len); | ||
| 130 | +} | ||
| 131 | + | ||
| 132 | +test "monocle layout: respects area offset" { | ||
| 133 | + const area = Rect{ .x = 100, .y = 50, .w = 800, .h = 600 }; | ||
| 134 | + const rects = calculate_monocle_layout(area, 1); | ||
| 135 | + | ||
| 136 | + try testing.expectEqual(@as(i32, 100), rects[0].x); | ||
| 137 | + try testing.expectEqual(@as(i32, 50), rects[0].y); | ||
| 138 | +} | ||
+7 −0 tests/main_tests.zig new file mode 100644 Large diff collapsed, click to expand
| @@ -0,0 +1,7 @@ | |||
| 1 | +const std = @import("std"); | ||
| 2 | + | ||
| 3 | +test { | ||
| 4 | + _ = @import("config_tests.zig"); | ||
| 5 | + _ = @import("layout_tests.zig"); | ||
| 6 | + _ = @import("keybind_tests.zig"); | ||
| 7 | +} | ||