From 02523d8b3468dd0abbb51384dedfb7f99c973e92 Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Mon, 22 Sep 2025 11:45:22 -0700 Subject: [PATCH] feat(wm): enrich monitors with serial id This commit does some wrangling of the ioreg output to enrich monitors with product names and serial number IDs, the latter of which is very important to maintain compatibility with komorebi for Windows. I can't get the equivalent of device and device_id on macOS, but these are legacy things in komorebi for Windows so I think I'll leave them out. To verify monitor information, SocketMessage::MonitorInformation has been implemented. --- Cargo.lock | 95 ++++++++++++++++++++++--------- Cargo.toml | 2 +- deny.toml | 2 +- dependencies.json | 61 +++++++++++++------- komorebi/Cargo.toml | 7 ++- komorebi/src/core/mod.rs | 1 + komorebi/src/core_graphics/mod.rs | 5 ++ komorebi/src/ioreg.rs | 81 ++++++++++++++++++++++++++ komorebi/src/lib.rs | 4 ++ komorebi/src/macos_api.rs | 40 +++++++++---- komorebi/src/monitor.rs | 43 +++++++++++++- komorebi/src/process_command.rs | 12 ++++ komorebi/src/state.rs | 2 + komorebic/src/main.rs | 7 ++- 14 files changed, 300 insertions(+), 62 deletions(-) create mode 100644 komorebi/src/ioreg.rs diff --git a/Cargo.lock b/Cargo.lock index 1beed588..76acb08c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,9 +141,9 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "cc" -version = "1.2.37" +version = "1.2.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65193589c6404eb80b450d618eaf9a2cafaaafd57ecce47370519ef674a7bd44" +checksum = "80f41ae168f955c12fb8960b057d70d0ca153fb83182b57d86380443527be7e9" dependencies = [ "find-msvc-tools", "shlex", @@ -175,9 +175,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.47" +version = "4.5.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eac00902d9d136acd712710d71823fb8ac8004ca445a89e73a41d45aa712931" +checksum = "e2134bb3ea021b78629caa971416385309e0131b351b25e01dc16fb54e1b5fae" dependencies = [ "clap_builder", "clap_derive", @@ -185,9 +185,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.5.47" +version = "4.5.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ad9bbf750e73b5884fb8a211a9424a1906c1e156724260fdae972f31d70e1d6" +checksum = "c2ba64afa3c0a6df7fa517765e31314e983f51dda798ffba27b988194fb65dc9" dependencies = [ "anstream", "anstyle", @@ -281,9 +281,9 @@ dependencies = [ [[package]] name = "darling" -version = "0.20.11" +version = "0.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc7f46116c46ff9ab3eb1597a45688b6715c6e628b5c133e288e709a29bcb4ee" +checksum = "9cdf337090841a411e2a7f3deb9187445851f91b309c0c0a29e05f74a00a48c0" dependencies = [ "darling_core", "darling_macro", @@ -291,9 +291,9 @@ dependencies = [ [[package]] name = "darling_core" -version = "0.20.11" +version = "0.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d00b9596d185e565c2207a0b01f8bd1a135483d02d9b7b0a54b11da8d53412e" +checksum = "1247195ecd7e3c85f83c8d2a366e4210d588e802133e1e355180a9870b517ea4" dependencies = [ "fnv", "ident_case", @@ -305,9 +305,9 @@ dependencies = [ [[package]] name = "darling_macro" -version = "0.20.11" +version = "0.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc34b93ccb385b40dc71c6fceac4b2ad23662c7eeb248cf10d529b7e055b6ead" +checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81" dependencies = [ "darling_core", "quote", @@ -416,9 +416,9 @@ dependencies = [ [[package]] name = "find-msvc-tools" -version = "0.1.1" +version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fd99930f64d146689264c637b5af2f0233a933bef0d8570e2526bf9e083192d" +checksum = "1ced73b1dacfc750a6db6c0a0c3a3853c8b41997e2e2c563dc90804ae6867959" [[package]] name = "fnv" @@ -608,6 +608,8 @@ dependencies = [ "objc2-foundation", "parking_lot", "pastey", + "quick-xml 0.38.3", + "quickxml_to_serde", "regex", "serde", "serde_json_lenient", @@ -718,6 +720,15 @@ version = "2.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0" +[[package]] +name = "minidom" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe549115a674f5ec64c754d85e37d6f42664bd0ef4ffb62b619489ad99c6cb1a" +dependencies = [ + "quick-xml 0.17.2", +] + [[package]] name = "miniz_oxide" version = "0.8.9" @@ -1087,6 +1098,37 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-xml" +version = "0.17.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe1e430bdcf30c9fdc25053b9c459bb1a4672af4617b6c783d7d91dc17c6bbb0" +dependencies = [ + "memchr", +] + +[[package]] +name = "quick-xml" +version = "0.38.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42a232e7487fc2ef313d96dde7948e7a3c05101870d8985e4fd8d26aedd27b89" +dependencies = [ + "memchr", + "serde", +] + +[[package]] +name = "quickxml_to_serde" +version = "0.6.1" +source = "git+https://github.com/LGUG2Z/quickxml_to_serde.git#312fabc913738c544dfcf299f5e73de4f6932236" +dependencies = [ + "minidom", + "regex", + "serde", + "serde_derive", + "serde_json_lenient", +] + [[package]] name = "quote" version = "1.0.40" @@ -1267,9 +1309,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "serde" -version = "1.0.225" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd6c24dee235d0da097043389623fb913daddf92c76e9f5a1db88607a0bcbd1d" +checksum = "0dca6411025b24b60bfa7ec1fe1f8e710ac09782dca409ee8237ba74b51295fd" dependencies = [ "serde_core", "serde_derive", @@ -1277,18 +1319,18 @@ dependencies = [ [[package]] name = "serde_core" -version = "1.0.225" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "659356f9a0cb1e529b24c01e43ad2bdf520ec4ceaf83047b83ddcc2251f96383" +checksum = "ba2ba63999edb9dac981fb34b3e5c0d111a69b0924e253ed29d83f7c99e966a4" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.225" +version = "1.0.226" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0ea936adf78b1f766949a4977b91d2f5595825bd6ec079aa9543ad2685fc4516" +checksum = "8db53ae22f34573731bafa1db20f04027b2d25e02d8205921b569171699cdb33" dependencies = [ "proc-macro2", "quote", @@ -1322,9 +1364,9 @@ dependencies = [ [[package]] name = "serde_with" -version = "3.14.0" +version = "3.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2c45cd61fefa9db6f254525d46e392b852e0e61d9a1fd36e5bd183450a556d5" +checksum = "c522100790450cf78eeac1507263d0a350d4d5b30df0c8e1fe051a10c22b376e" dependencies = [ "base64", "chrono", @@ -1342,9 +1384,9 @@ dependencies = [ [[package]] name = "serde_with_macros" -version = "3.14.0" +version = "3.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "de90945e6565ce0d9a25098082ed4ee4002e047cb59892c318d66821e14bb30f" +checksum = "327ada00f7d64abaac1e55a6911e90cf665aa051b9a561c7006c157f4633135e" dependencies = [ "darling", "proc-macro2", @@ -1452,11 +1494,12 @@ dependencies = [ [[package]] name = "time" -version = "0.3.43" +version = "0.3.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83bde6f1ec10e72d583d91623c939f623002284ef622b87de38cfd546cbf2031" +checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d" dependencies = [ "deranged", + "itoa", "num-conv", "powerfmt", "serde", diff --git a/Cargo.toml b/Cargo.toml index 35a63335..5581106a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,4 +15,4 @@ lazy_static = "1" pastey = "0.1" serde = { version = "1", features = ["derive"] } serde_json = { package = "serde_json_lenient", version = "0.2" } -thiserror = "2" +thiserror = "2" \ No newline at end of file diff --git a/deny.toml b/deny.toml index 4bcaa988..b33143d8 100644 --- a/deny.toml +++ b/deny.toml @@ -63,5 +63,5 @@ unknown-registry = "deny" unknown-git = "deny" allow-registry = ["https://github.com/rust-lang/crates.io-index"] allow-git = [ - # "https://github.com/LGUG2Z/komorebi-for-mac", + "https://github.com/LGUG2Z/quickxml_to_serde", ] \ No newline at end of file diff --git a/dependencies.json b/dependencies.json index cfe2f739..a60bb042 100644 --- a/dependencies.json +++ b/dependencies.json @@ -19,8 +19,8 @@ "backtrace 0.3.75 registry+https://github.com/rust-lang/crates.io-index", "bitflags 2.9.4 registry+https://github.com/rust-lang/crates.io-index", "cfg-if 1.0.3 registry+https://github.com/rust-lang/crates.io-index", - "clap 4.5.47 registry+https://github.com/rust-lang/crates.io-index", - "clap_builder 4.5.47 registry+https://github.com/rust-lang/crates.io-index", + "clap 4.5.48 registry+https://github.com/rust-lang/crates.io-index", + "clap_builder 4.5.48 registry+https://github.com/rust-lang/crates.io-index", "clap_derive 4.5.47 registry+https://github.com/rust-lang/crates.io-index", "clap_lex 0.7.5 registry+https://github.com/rust-lang/crates.io-index", "color-eyre 0.6.5 registry+https://github.com/rust-lang/crates.io-index", @@ -36,6 +36,7 @@ "eyre 0.6.12 registry+https://github.com/rust-lang/crates.io-index", "filetime 0.2.26 registry+https://github.com/rust-lang/crates.io-index", "fnv 1.0.7 registry+https://github.com/rust-lang/crates.io-index", + "getrandom 0.2.16 registry+https://github.com/rust-lang/crates.io-index", "gimli 0.31.1 registry+https://github.com/rust-lang/crates.io-index", "heck 0.5.0 registry+https://github.com/rust-lang/crates.io-index", "hotwatch 0.5.0 registry+https://github.com/rust-lang/crates.io-index", @@ -64,8 +65,12 @@ "parking_lot_core 0.9.11 registry+https://github.com/rust-lang/crates.io-index", "pastey 0.1.1 registry+https://github.com/rust-lang/crates.io-index", "pin-project-lite 0.2.16 registry+https://github.com/rust-lang/crates.io-index", + "ppv-lite86 0.2.21 registry+https://github.com/rust-lang/crates.io-index", "proc-macro2 1.0.101 registry+https://github.com/rust-lang/crates.io-index", "quote 1.0.40 registry+https://github.com/rust-lang/crates.io-index", + "rand 0.8.5 registry+https://github.com/rust-lang/crates.io-index", + "rand_chacha 0.3.1 registry+https://github.com/rust-lang/crates.io-index", + "rand_core 0.6.4 registry+https://github.com/rust-lang/crates.io-index", "regex 1.11.2 registry+https://github.com/rust-lang/crates.io-index", "regex-automata 0.4.10 registry+https://github.com/rust-lang/crates.io-index", "regex-syntax 0.8.6 registry+https://github.com/rust-lang/crates.io-index", @@ -73,12 +78,12 @@ "rustix 1.1.2 registry+https://github.com/rust-lang/crates.io-index", "ryu 1.0.20 registry+https://github.com/rust-lang/crates.io-index", "scopeguard 1.2.0 registry+https://github.com/rust-lang/crates.io-index", - "serde 1.0.225 registry+https://github.com/rust-lang/crates.io-index", - "serde_core 1.0.225 registry+https://github.com/rust-lang/crates.io-index", - "serde_derive 1.0.225 registry+https://github.com/rust-lang/crates.io-index", + "serde 1.0.226 registry+https://github.com/rust-lang/crates.io-index", + "serde_core 1.0.226 registry+https://github.com/rust-lang/crates.io-index", + "serde_derive 1.0.226 registry+https://github.com/rust-lang/crates.io-index", "serde_json_lenient 0.2.4 registry+https://github.com/rust-lang/crates.io-index", - "serde_with 3.14.0 registry+https://github.com/rust-lang/crates.io-index", - "serde_with_macros 3.14.0 registry+https://github.com/rust-lang/crates.io-index", + "serde_with 3.14.1 registry+https://github.com/rust-lang/crates.io-index", + "serde_with_macros 3.14.1 registry+https://github.com/rust-lang/crates.io-index", "smallvec 1.15.1 registry+https://github.com/rust-lang/crates.io-index", "syn 2.0.106 registry+https://github.com/rust-lang/crates.io-index", "terminal_size 0.4.3 registry+https://github.com/rust-lang/crates.io-index", @@ -86,7 +91,8 @@ "thiserror-impl 2.0.16 registry+https://github.com/rust-lang/crates.io-index", "thread_local 1.1.9 registry+https://github.com/rust-lang/crates.io-index", "unicode-ident 1.0.19 registry+https://github.com/rust-lang/crates.io-index", - "utf8parse 0.2.2 registry+https://github.com/rust-lang/crates.io-index" + "utf8parse 0.2.2 registry+https://github.com/rust-lang/crates.io-index", + "zerocopy 0.8.27 registry+https://github.com/rust-lang/crates.io-index" ] ], [ @@ -102,6 +108,12 @@ "notify-debouncer-full 0.1.0 registry+https://github.com/rust-lang/crates.io-index" ] ], + [ + "BSD-2-Clause", + [ + "zerocopy 0.8.27 registry+https://github.com/rust-lang/crates.io-index" + ] + ], [ "BSL-1.0", [ @@ -132,8 +144,8 @@ "block2 0.6.1 registry+https://github.com/rust-lang/crates.io-index", "cfg-if 1.0.3 registry+https://github.com/rust-lang/crates.io-index", "cfg_aliases 0.2.1 registry+https://github.com/rust-lang/crates.io-index", - "clap 4.5.47 registry+https://github.com/rust-lang/crates.io-index", - "clap_builder 4.5.47 registry+https://github.com/rust-lang/crates.io-index", + "clap 4.5.48 registry+https://github.com/rust-lang/crates.io-index", + "clap_builder 4.5.48 registry+https://github.com/rust-lang/crates.io-index", "clap_derive 4.5.47 registry+https://github.com/rust-lang/crates.io-index", "clap_lex 0.7.5 registry+https://github.com/rust-lang/crates.io-index", "color-eyre 0.6.5 registry+https://github.com/rust-lang/crates.io-index", @@ -142,9 +154,9 @@ "crossbeam-channel 0.5.15 registry+https://github.com/rust-lang/crates.io-index", "crossbeam-utils 0.8.21 registry+https://github.com/rust-lang/crates.io-index", "ctrlc 3.5.0 registry+https://github.com/rust-lang/crates.io-index", - "darling 0.20.11 registry+https://github.com/rust-lang/crates.io-index", - "darling_core 0.20.11 registry+https://github.com/rust-lang/crates.io-index", - "darling_macro 0.20.11 registry+https://github.com/rust-lang/crates.io-index", + "darling 0.21.3 registry+https://github.com/rust-lang/crates.io-index", + "darling_core 0.21.3 registry+https://github.com/rust-lang/crates.io-index", + "darling_macro 0.21.3 registry+https://github.com/rust-lang/crates.io-index", "dirs 6.0.0 registry+https://github.com/rust-lang/crates.io-index", "dirs-sys 0.5.0 registry+https://github.com/rust-lang/crates.io-index", "dispatch 0.2.0 registry+https://github.com/rust-lang/crates.io-index", @@ -154,6 +166,7 @@ "filetime 0.2.26 registry+https://github.com/rust-lang/crates.io-index", "fnv 1.0.7 registry+https://github.com/rust-lang/crates.io-index", "fsevent-sys 4.1.0 registry+https://github.com/rust-lang/crates.io-index", + "getrandom 0.2.16 registry+https://github.com/rust-lang/crates.io-index", "gimli 0.31.1 registry+https://github.com/rust-lang/crates.io-index", "heck 0.5.0 registry+https://github.com/rust-lang/crates.io-index", "hotwatch 0.5.0 registry+https://github.com/rust-lang/crates.io-index", @@ -168,6 +181,7 @@ "matchers 0.2.0 registry+https://github.com/rust-lang/crates.io-index", "memchr 2.7.5 registry+https://github.com/rust-lang/crates.io-index", "miniz_oxide 0.8.9 registry+https://github.com/rust-lang/crates.io-index", + "nanoid 0.4.0 registry+https://github.com/rust-lang/crates.io-index", "nix 0.30.1 registry+https://github.com/rust-lang/crates.io-index", "nu-ansi-term 0.50.1 registry+https://github.com/rust-lang/crates.io-index", "objc2 0.6.2 registry+https://github.com/rust-lang/crates.io-index", @@ -190,8 +204,15 @@ "parking_lot_core 0.9.11 registry+https://github.com/rust-lang/crates.io-index", "pastey 0.1.1 registry+https://github.com/rust-lang/crates.io-index", "pin-project-lite 0.2.16 registry+https://github.com/rust-lang/crates.io-index", + "ppv-lite86 0.2.21 registry+https://github.com/rust-lang/crates.io-index", "proc-macro2 1.0.101 registry+https://github.com/rust-lang/crates.io-index", + "quick-xml 0.17.2 registry+https://github.com/rust-lang/crates.io-index", + "quick-xml 0.38.3 registry+https://github.com/rust-lang/crates.io-index", + "quickxml_to_serde 0.6.1 git+https://github.com/LGUG2Z/quickxml_to_serde.git", "quote 1.0.40 registry+https://github.com/rust-lang/crates.io-index", + "rand 0.8.5 registry+https://github.com/rust-lang/crates.io-index", + "rand_chacha 0.3.1 registry+https://github.com/rust-lang/crates.io-index", + "rand_core 0.6.4 registry+https://github.com/rust-lang/crates.io-index", "regex 1.11.2 registry+https://github.com/rust-lang/crates.io-index", "regex-automata 0.4.10 registry+https://github.com/rust-lang/crates.io-index", "regex-syntax 0.8.6 registry+https://github.com/rust-lang/crates.io-index", @@ -199,12 +220,12 @@ "rustix 1.1.2 registry+https://github.com/rust-lang/crates.io-index", "same-file 1.0.6 registry+https://github.com/rust-lang/crates.io-index", "scopeguard 1.2.0 registry+https://github.com/rust-lang/crates.io-index", - "serde 1.0.225 registry+https://github.com/rust-lang/crates.io-index", - "serde_core 1.0.225 registry+https://github.com/rust-lang/crates.io-index", - "serde_derive 1.0.225 registry+https://github.com/rust-lang/crates.io-index", + "serde 1.0.226 registry+https://github.com/rust-lang/crates.io-index", + "serde_core 1.0.226 registry+https://github.com/rust-lang/crates.io-index", + "serde_derive 1.0.226 registry+https://github.com/rust-lang/crates.io-index", "serde_json_lenient 0.2.4 registry+https://github.com/rust-lang/crates.io-index", - "serde_with 3.14.0 registry+https://github.com/rust-lang/crates.io-index", - "serde_with_macros 3.14.0 registry+https://github.com/rust-lang/crates.io-index", + "serde_with 3.14.1 registry+https://github.com/rust-lang/crates.io-index", + "serde_with_macros 3.14.1 registry+https://github.com/rust-lang/crates.io-index", "sharded-slab 0.1.7 registry+https://github.com/rust-lang/crates.io-index", "smallvec 1.15.1 registry+https://github.com/rust-lang/crates.io-index", "strsim 0.11.1 registry+https://github.com/rust-lang/crates.io-index", @@ -223,12 +244,14 @@ "tracing-subscriber 0.3.20 registry+https://github.com/rust-lang/crates.io-index", "unicode-ident 1.0.19 registry+https://github.com/rust-lang/crates.io-index", "utf8parse 0.2.2 registry+https://github.com/rust-lang/crates.io-index", - "walkdir 2.5.0 registry+https://github.com/rust-lang/crates.io-index" + "walkdir 2.5.0 registry+https://github.com/rust-lang/crates.io-index", + "zerocopy 0.8.27 registry+https://github.com/rust-lang/crates.io-index" ] ], [ "MPL-2.0", [ + "minidom 0.12.0 registry+https://github.com/rust-lang/crates.io-index", "option-ext 0.2.0 registry+https://github.com/rust-lang/crates.io-index" ] ], diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 15552e31..1f73abfe 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -11,6 +11,7 @@ ctrlc = { version = "3", features = ["termination"] } dirs = { workspace = true } hotwatch = "0.5" lazy_static = { workspace = true } +nanoid = "0.4" objc2 = "0.6" objc2-app-kit = "0.3" objc2-application-services = "0.3" @@ -19,6 +20,9 @@ objc2-core-graphics = { version = "0.3", features = ["CGWindow", "alloc"] } objc2-foundation = "0.3" parking_lot = "0.12" pastey = { workspace = true } +quick-xml = { version = "0.38", features = ["serialize"] } +# forked to patch in serde_json_lenient and update some old dependencies +quickxml_to_serde = { git = "https://github.com/LGUG2Z/quickxml_to_serde.git", version = "0.6.1" } regex = "1" serde = { workspace = true } serde_json = { workspace = true } @@ -26,5 +30,4 @@ serde_with = "3" strum = { version = "0.27", features = ["derive"] } thiserror = { workspace = true } tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } -nanoid = "0.4" \ No newline at end of file +tracing-subscriber = { version = "0.3", features = ["env-filter"] } \ No newline at end of file diff --git a/komorebi/src/core/mod.rs b/komorebi/src/core/mod.rs index 2db56ab5..a0901c53 100644 --- a/komorebi/src/core/mod.rs +++ b/komorebi/src/core/mod.rs @@ -235,6 +235,7 @@ pub enum SocketMessage { State, GlobalState, VisibleWindows, + MonitorInformation, } impl SocketMessage { diff --git a/komorebi/src/core_graphics/mod.rs b/komorebi/src/core_graphics/mod.rs index b7758e4c..bf7b71ea 100644 --- a/komorebi/src/core_graphics/mod.rs +++ b/komorebi/src/core_graphics/mod.rs @@ -5,6 +5,7 @@ use objc2_core_foundation::CGFloat; use objc2_core_foundation::CGPoint; use objc2_core_foundation::CGRect; use objc2_core_graphics::CGDisplayBounds; +use objc2_core_graphics::CGDisplaySerialNumber; use objc2_core_graphics::CGGetDisplaysWithPoint; use objc2_core_graphics::CGGetOnlineDisplayList; use objc2_core_graphics::CGRectIntersectsRect; @@ -57,6 +58,10 @@ impl CoreGraphicsApi { unsafe { CGDisplayBounds(display_id) } } + pub fn display_serial_number(display_id: u32) -> u32 { + unsafe { CGDisplaySerialNumber(display_id) } + } + pub fn display_bounds_for_window_rect(window_rect: CGRect) -> Option { let mut displays: Vec = Vec::with_capacity(1); let mut display_count = 0; diff --git a/komorebi/src/ioreg.rs b/komorebi/src/ioreg.rs new file mode 100644 index 00000000..2d7b7eee --- /dev/null +++ b/komorebi/src/ioreg.rs @@ -0,0 +1,81 @@ +use crate::monitor::MonitorInfo; +use color_eyre::eyre; +use color_eyre::eyre::bail; +use quickxml_to_serde::Config; +use quickxml_to_serde::xml_string_to_json; +use std::process::Command; + +use serde_json::Value; +pub struct IoReg; + +impl IoReg { + #[tracing::instrument] + pub fn query_monitors() -> eyre::Result> { + let output = Command::new("ioreg") + .arg("-a") + .arg("-c") + .arg("IOMobileFramebufferShim") + .output()?; + + if !output.status.success() { + bail!( + "ioreg command failed: {}", + String::from_utf8_lossy(&output.stderr) + ); + } + let xml_output = String::from_utf8(output.stdout)?; + let value = xml_string_to_json(xml_output, &Config::new_with_defaults())?; + let mut results = vec![]; + find_display_entries(&value, &mut results); + + Ok(results) + } +} + +fn find_display_entries(value: &Value, results: &mut Vec) { + match value { + Value::Object(obj) => { + if let ( + Some(Value::Array(keys)), + Some(Value::Array(strings)), + Some(Value::Array(integers)), + ) = (obj.get("key"), obj.get("string"), obj.get("integer")) + { + let has_product_name = keys + .iter() + .any(|k| k.as_str().map(|s| s == "ProductName").unwrap_or(false)); + + if has_product_name + && strings.len() >= 3 + && integers.len() >= 5 + && let (Some(alpha_serial), Some(manuf_id), Some(product_name)) = ( + strings[0].as_str(), + strings[1].as_str(), + strings[2].as_str(), + ) + { + results.push(MonitorInfo { + alphanumeric_serial_number: alpha_serial.to_string(), + manufacturer_id: manuf_id.to_string(), + product_name: product_name.to_string(), + legacy_manufacturer_id: integers[0].as_u64().unwrap_or(0).to_string(), + product_id: integers[1].as_u64().unwrap_or(0).to_string(), + serial_number: integers[2].as_u64().unwrap_or(0) as u32, + week_of_manufacture: integers[3].as_u64().unwrap_or(0).to_string(), + year_of_manufacture: integers[4].as_u64().unwrap_or(0).to_string(), + }); + } + } + + for v in obj.values() { + find_display_entries(v, results); + } + } + Value::Array(arr) => { + for item in arr { + find_display_entries(item, results); + } + } + _ => {} + } +} diff --git a/komorebi/src/lib.rs b/komorebi/src/lib.rs index 9e7142a5..34b2a33c 100644 --- a/komorebi/src/lib.rs +++ b/komorebi/src/lib.rs @@ -10,6 +10,7 @@ use crate::core::config_generation::WorkspaceMatchingRule; use crate::core_graphics::error::CoreGraphicsError; use crate::window::AspectRatio; use crate::window::PredefinedAspectRatio; +use color_eyre::eyre; use core::pathext::PathExt; use lazy_static::lazy_static; use objc2_application_services::AXObserver; @@ -44,6 +45,7 @@ pub mod core; pub mod core_graphics; pub mod display_reconfiguration_listener; pub mod input_event_listener; +pub mod ioreg; pub mod lockable_sequence; pub mod macos_api; pub mod monitor; @@ -186,6 +188,8 @@ pub enum LibraryError { Accessibility(#[from] AccessibilityError), #[error(transparent)] CoreGraphics(#[from] CoreGraphicsError), + #[error(transparent)] + Eyre(#[from] eyre::Error), } pub fn hidden_frame_bottom_left(screen_frame: CGRect, window_size: CGSize) -> CGRect { diff --git a/komorebi/src/macos_api.rs b/komorebi/src/macos_api.rs index d33e86fa..17c55b55 100644 --- a/komorebi/src/macos_api.rs +++ b/komorebi/src/macos_api.rs @@ -11,6 +11,7 @@ use crate::cf_array_as; use crate::container::Container; use crate::core::rect::Rect; use crate::core_graphics::CoreGraphicsApi; +use crate::ioreg::IoReg; use crate::monitor::Monitor; use crate::window::WindowInfo; use crate::window_manager::WindowManager; @@ -36,25 +37,41 @@ use std::sync::Arc; pub struct MacosApi; impl MacosApi { + #[tracing::instrument(skip_all)] pub fn load_monitor_information(wm: &mut WindowManager) -> Result<(), LibraryError> { + let monitor_info = IoReg::query_monitors()?; + let mut monitor_info_map = HashMap::new(); + for m in monitor_info { + monitor_info_map.insert(m.serial_number, m); + } + let screens = NSScreen::screens(MainThreadMarker::new().unwrap()); for display_id in CoreGraphicsApi::connected_display_ids()? { let display_bounds = CoreGraphicsApi::display_bounds(display_id); + let serial_number = CoreGraphicsApi::display_serial_number(display_id); - for screen in &screens { - let menu_bar_height = screen.frame().size.height - - screen.visibleFrame().size.height - - screen.visibleFrame().origin.y; + if let Some(info) = monitor_info_map.get(&serial_number) { + for screen in &screens { + let menu_bar_height = screen.frame().size.height + - screen.visibleFrame().size.height + - screen.visibleFrame().origin.y; - if screen.frame() == display_bounds { - let size = Rect::from(display_bounds); - let mut work_area_size = Rect::from(display_bounds); - work_area_size.top += menu_bar_height as i32; - work_area_size.bottom = screen.visibleFrame().size.height as i32; + if screen.frame() == display_bounds { + let size = Rect::from(display_bounds); + let mut work_area_size = Rect::from(display_bounds); + work_area_size.top += menu_bar_height as i32; + work_area_size.bottom = screen.visibleFrame().size.height as i32; - let monitor = Monitor::new(display_id, size, work_area_size); - wm.monitors.elements_mut().push_back(monitor); + let monitor = Monitor::new( + display_id, + size, + work_area_size, + &info.product_name, + &info.alphanumeric_serial_number, + ); + wm.monitors.elements_mut().push_back(monitor); + } } } } @@ -101,6 +118,7 @@ impl MacosApi { Ok(()) } + #[tracing::instrument(skip_all)] pub fn load_workspace_information(wm: &mut WindowManager) -> Result<(), LibraryError> { let mut monitor_size_map = HashMap::new(); let mut monitor_workspace_map = HashMap::new(); diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index d90b7df2..1e3fb96a 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -18,11 +18,44 @@ use serde::Serialize; use std::collections::VecDeque; use std::sync::atomic::Ordering; +#[derive(Debug, Serialize, Deserialize)] +pub struct MonitorInfo { + pub alphanumeric_serial_number: String, + pub manufacturer_id: String, + pub product_name: String, + pub legacy_manufacturer_id: String, + pub product_id: String, + pub serial_number: u32, + pub week_of_manufacture: String, + pub year_of_manufacture: String, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct MonitorInformation { + pub id: u32, + pub device: String, + pub serial_number_id: String, + pub size: Rect, +} + +impl From<&Monitor> for MonitorInformation { + fn from(value: &Monitor) -> Self { + Self { + id: value.id, + device: value.device.clone(), + serial_number_id: value.serial_number_id.clone(), + size: value.size, + } + } +} + impl_ring_elements!(Monitor, Workspace); #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Monitor { pub id: u32, + pub device: String, + pub serial_number_id: String, pub workspaces: Ring, pub size: Rect, pub work_area_offset: Option, @@ -38,12 +71,20 @@ pub struct Monitor { impl Monitor { #[allow(clippy::field_reassign_with_default)] - pub fn new(id: u32, size: Rect, work_area_size: Rect) -> Self { + pub fn new( + id: u32, + size: Rect, + work_area_size: Rect, + device: &str, + serial_number_id: &str, + ) -> Self { let mut workspaces = Ring::default(); workspaces.elements_mut().push_back(Workspace::default()); Self { id, + device: device.to_string(), + serial_number_id: serial_number_id.to_string(), workspaces, size, work_area_offset: None, diff --git a/komorebi/src/process_command.rs b/komorebi/src/process_command.rs index ff19ca9b..ce38aa41 100644 --- a/komorebi/src/process_command.rs +++ b/komorebi/src/process_command.rs @@ -6,6 +6,7 @@ use crate::core::operation_direction::OperationDirection; use crate::core::rect::Rect; use crate::macos_api::MacosApi; use crate::monitor::Monitor; +use crate::monitor::MonitorInformation; use crate::state::GlobalState; use crate::state::State; use crate::window_manager::WindowManager; @@ -835,6 +836,17 @@ impl WindowManager { reply.write_all(visible_windows_state.as_bytes())?; } + SocketMessage::MonitorInformation => { + let mut monitors = vec![]; + for monitor in self.monitors() { + monitors.push(MonitorInformation::from(monitor)); + } + + let monitors_state = serde_json::to_string_pretty(&monitors) + .unwrap_or_else(|error| error.to_string()); + + reply.write_all(monitors_state.as_bytes())?; + } } Ok(()) diff --git a/komorebi/src/state.rs b/komorebi/src/state.rs index 72e17913..856897ff 100644 --- a/komorebi/src/state.rs +++ b/komorebi/src/state.rs @@ -94,6 +94,8 @@ impl From<&WindowManager> for State { .iter() .map(|monitor| Monitor { id: monitor.id, + device: monitor.device.clone(), + serial_number_id: monitor.serial_number_id.clone(), size: monitor.size, work_area_size: monitor.work_area_size, work_area_offset: monitor.work_area_offset, diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index d1a71899..157be97d 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -183,6 +183,9 @@ enum SubCommand { GlobalState, /// Show a JSON representation of visible windows VisibleWindows, + /// Show information about connected monitors + #[clap(alias = "monitor-info")] + MonitorInformation, /// Change focus to the window in the specified direction #[clap(arg_required_else_help = true)] Focus(Focus), @@ -243,7 +246,6 @@ enum SubCommand { PromoteFocus, /// Promote the window in the specified direction PromoteWindow(PromoteWindow), - /// Move the focused window to the specified monitor #[clap(arg_required_else_help = true)] MoveToMonitor(MoveToMonitor), @@ -568,6 +570,9 @@ fn main() -> eyre::Result<()> { SubCommand::VisibleWindows => { print_query(&SocketMessage::VisibleWindows); } + SubCommand::MonitorInformation => { + print_query(&SocketMessage::MonitorInformation); + } } Ok(())