Compare commits

..

2 Commits

11 changed files with 643 additions and 183 deletions

307
src-tauri/Cargo.lock generated
View File

@ -14,7 +14,7 @@ version = "0.7.20"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac"
dependencies = [ dependencies = [
"memchr", "memchr 2.5.0",
] ]
[[package]] [[package]]
@ -23,7 +23,7 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04" checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"
dependencies = [ dependencies = [
"memchr", "memchr 2.5.0",
] ]
[[package]] [[package]]
@ -157,7 +157,7 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d4260bcc2e8fc9df1eac4919a720effeb63a3f0952f5bf4944adfa18897f09" checksum = "c3d4260bcc2e8fc9df1eac4919a720effeb63a3f0952f5bf4944adfa18897f09"
dependencies = [ dependencies = [
"memchr", "memchr 2.5.0",
"serde", "serde",
] ]
@ -349,7 +349,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4"
dependencies = [ dependencies = [
"bytes", "bytes",
"memchr", "memchr 2.5.0",
] ]
[[package]] [[package]]
@ -373,10 +373,16 @@ version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [ dependencies = [
"core-foundation-sys", "core-foundation-sys 0.8.4",
"libc", "libc",
] ]
[[package]]
name = "core-foundation-sys"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ca8a5221364ef15ce201e8ed2f609fc312682a8f4e0e3d4aa5879764e0fa3b"
[[package]] [[package]]
name = "core-foundation-sys" name = "core-foundation-sys"
version = "0.8.4" version = "0.8.4"
@ -571,6 +577,72 @@ dependencies = [
"syn 2.0.15", "syn 2.0.15",
] ]
[[package]]
name = "ddc"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba69f2c53e320fc4abad17cb02bbbf04d1a36f18e9907f347589ec5991b3c6c5"
dependencies = [
"mccs",
]
[[package]]
name = "ddc-hi"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c6747b17c926a2aa34739b30f1a6cc726e3a7baf0e2f69a4c03a95cf5de94de"
dependencies = [
"anyhow",
"ddc",
"ddc-i2c",
"ddc-macos",
"ddc-winapi",
"edid",
"log",
"mccs",
"mccs-caps",
"mccs-db",
"nvapi",
]
[[package]]
name = "ddc-i2c"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ef18fac9fd5c11d0c7b85a80887b01f7361b49edb2b4627243928b90ce2691b"
dependencies = [
"ddc",
"i2c",
"i2c-linux",
"resize-slice",
]
[[package]]
name = "ddc-macos"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cbaf316c113cfc30da8856c8104dfb4168b73fdd78562d1542e358fe8299dea"
dependencies = [
"core-foundation",
"core-foundation-sys 0.8.4",
"core-graphics",
"ddc",
"io-kit-sys",
"mach 0.3.2",
"thiserror",
]
[[package]]
name = "ddc-winapi"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "015df0d6d814ea948e012977760324da6d103ec8d67c971c75a6daa3b4fc943f"
dependencies = [
"ddc",
"widestring",
"winapi",
]
[[package]] [[package]]
name = "derive_more" name = "derive_more"
version = "0.99.17" version = "0.99.17"
@ -656,6 +728,15 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b"
[[package]]
name = "edid"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24ce75530893d834dcfe3bb67ce0e7dec489484e7cb4423ca31618af4bab24fe"
dependencies = [
"nom",
]
[[package]] [[package]]
name = "either" name = "either"
version = "1.8.1" version = "1.8.1"
@ -921,7 +1002,7 @@ dependencies = [
"futures-macro", "futures-macro",
"futures-sink", "futures-sink",
"futures-task", "futures-task",
"memchr", "memchr 2.5.0",
"pin-project-lite", "pin-project-lite",
"pin-utils", "pin-utils",
"slab", "slab",
@ -1292,6 +1373,39 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "i2c"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60c7b7bdd7b3a985fdcf94a0d7d98e7a47fde8b7f22fb55ce1a91cc104a2ce9a"
dependencies = [
"bitflags",
]
[[package]]
name = "i2c-linux"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0268a871aaa071221d6c2875ebedcf64710e59b0d87c68c8faf5e98b87dd2a4"
dependencies = [
"bitflags",
"i2c",
"i2c-linux-sys",
"resize-slice",
"udev",
]
[[package]]
name = "i2c-linux-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55cd060ed0016621d3da4ed3a23b0158084de90d1f3a8e59f3d391aacd3bbcf8"
dependencies = [
"bitflags",
"byteorder",
"libc",
]
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.56" version = "0.1.56"
@ -1299,7 +1413,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c" checksum = "0722cd7114b7de04316e7ea5456a0bbb20e4adb46fd27a3697adb812cff0f37c"
dependencies = [ dependencies = [
"android_system_properties", "android_system_properties",
"core-foundation-sys", "core-foundation-sys 0.8.4",
"iana-time-zone-haiku", "iana-time-zone-haiku",
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
@ -1362,7 +1476,7 @@ dependencies = [
"globset", "globset",
"lazy_static", "lazy_static",
"log", "log",
"memchr", "memchr 2.5.0",
"regex", "regex",
"same-file", "same-file",
"thread_local", "thread_local",
@ -1412,6 +1526,16 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "io-kit-sys"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f21dcc74995dd4cd090b147e79789f8d65959cbfb5f0b118002db869ea3bd0a0"
dependencies = [
"core-foundation-sys 0.6.2",
"mach 0.2.3",
]
[[package]] [[package]]
name = "io-lifetimes" name = "io-lifetimes"
version = "1.0.10" version = "1.0.10"
@ -1555,6 +1679,16 @@ version = "0.2.142"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317" checksum = "6a987beff54b60ffa6d51982e1aa1146bc42f19bd26be28b0586f252fccf5317"
[[package]]
name = "libudev-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
dependencies = [
"libc",
"pkg-config",
]
[[package]] [[package]]
name = "line-wrap" name = "line-wrap"
version = "0.1.1" version = "0.1.1"
@ -1573,6 +1707,12 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]] [[package]]
name = "linux-raw-sys" name = "linux-raw-sys"
version = "0.3.7" version = "0.3.7"
@ -1619,6 +1759,24 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4" checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]]
name = "mach"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86dd2487cdfea56def77b88438a2c915fb45113c5319bfe7e14306ca4cd0b0e1"
dependencies = [
"libc",
]
[[package]]
name = "mach"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b823e83b2affd8f40a9ee8c29dbc56404c1e34cd2710921f2801e2cf29527afa"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "malloc_buf" name = "malloc_buf"
version = "0.0.6" version = "0.0.6"
@ -1657,6 +1815,38 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5"
[[package]]
name = "mccs"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6090d6b3ded42fed158b660a6b9cdaa1924f3eef6c6598e82a9ca9b70a1988cd"
dependencies = [
"void",
]
[[package]]
name = "mccs-caps"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eb961d01a3bb07969cfa276be2ab88c31d0fefa77a872696832732d6e9ec094"
dependencies = [
"mccs",
"nom",
]
[[package]]
name = "mccs-db"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cdaa8fe19a1a1918becc1b8cbbbdc1058bc71411dff4de0a6ec6b5269f49d38"
dependencies = [
"mccs",
"nom",
"serde",
"serde_derive",
"serde_yaml",
]
[[package]] [[package]]
name = "mdns-sd" name = "mdns-sd"
version = "0.7.3" version = "0.7.3"
@ -1670,6 +1860,15 @@ dependencies = [
"socket2", "socket2",
] ]
[[package]]
name = "memchr"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"
@ -1747,6 +1946,15 @@ version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "nom"
version = "3.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b"
dependencies = [
"memchr 1.0.2",
]
[[package]] [[package]]
name = "nu-ansi-term" name = "nu-ansi-term"
version = "0.46.0" version = "0.46.0"
@ -1818,6 +2026,28 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "nvapi"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c63de8cd8362e2c38d1a48dea6ae68e6293a8d8d22a52180d0f8dcc779b3158"
dependencies = [
"i2c",
"log",
"nvapi-sys",
"void",
]
[[package]]
name = "nvapi-sys"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b29e9a9393c69ee856bfcf5f76ed1ef32d2c0dd6f58558fd43334278fc1e7ea7"
dependencies = [
"bitflags",
"winapi",
]
[[package]] [[package]]
name = "objc" name = "objc"
version = "0.2.7" version = "0.2.7"
@ -2224,7 +2454,7 @@ version = "0.28.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce5e73202a820a31f8a0ee32ada5e21029c81fd9e3ebf668a40832e4219d9d1" checksum = "0ce5e73202a820a31f8a0ee32ada5e21029c81fd9e3ebf668a40832e4219d9d1"
dependencies = [ dependencies = [
"memchr", "memchr 2.5.0",
] ]
[[package]] [[package]]
@ -2359,7 +2589,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370" checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370"
dependencies = [ dependencies = [
"aho-corasick 1.0.1", "aho-corasick 1.0.1",
"memchr", "memchr 2.5.0",
"regex-syntax 0.7.1", "regex-syntax 0.7.1",
] ]
@ -2384,6 +2614,15 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c" checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c"
[[package]]
name = "resize-slice"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a3cb2f74a9891e76958b9e0ccd269a25b466c3ae3bb3efd71db157248308c4a"
dependencies = [
"uninitialized",
]
[[package]] [[package]]
name = "rustc_version" name = "rustc_version"
version = "0.4.0" version = "0.4.0"
@ -2560,6 +2799,18 @@ dependencies = [
"syn 2.0.15", "syn 2.0.15",
] ]
[[package]]
name = "serde_yaml"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef8099d3df28273c99a1728190c7a9f19d444c941044f64adf986bee7ec53051"
dependencies = [
"dtoa",
"linked-hash-map",
"serde",
"yaml-rust",
]
[[package]] [[package]]
name = "serialize-to-javascript" name = "serialize-to-javascript"
version = "0.1.1" version = "0.1.1"
@ -3014,7 +3265,7 @@ dependencies = [
"infer", "infer",
"json-patch 1.0.0", "json-patch 1.0.0",
"kuchiki", "kuchiki",
"memchr", "memchr 2.5.0",
"phf 0.10.1", "phf 0.10.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3079,6 +3330,7 @@ dependencies = [
"color_space", "color_space",
"core-foundation", "core-foundation",
"core-graphics", "core-graphics",
"ddc-hi",
"display-info", "display-info",
"env_logger", "env_logger",
"futures", "futures",
@ -3348,6 +3600,16 @@ version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba"
[[package]]
name = "udev"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47504d1a49b2ea1b133e7ddd1d9f0a83cf03feb9b440c2c470d06db4589cf301"
dependencies = [
"libc",
"libudev-sys",
]
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.13" version = "0.3.13"
@ -3381,6 +3643,12 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "uninitialized"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74c1aa4511c38276c548406f0b1f5f8b793f000cfb51e18f278a102abd057e81"
[[package]] [[package]]
name = "url" name = "url"
version = "2.3.1" version = "2.3.1"
@ -3453,6 +3721,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
[[package]] [[package]]
name = "vswhom" name = "vswhom"
version = "0.1.0" version = "0.1.0"
@ -3918,7 +4192,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae8970b36c66498d8ff1d66685dc86b91b29db0c7739899012f63a63814b4b28" checksum = "ae8970b36c66498d8ff1d66685dc86b91b29db0c7739899012f63a63814b4b28"
dependencies = [ dependencies = [
"memchr", "memchr 2.5.0",
] ]
[[package]] [[package]]
@ -4009,3 +4283,12 @@ dependencies = [
"libc", "libc",
"quick-xml", "quick-xml",
] ]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

View File

@ -35,6 +35,7 @@ core-foundation = "0.9.3"
tokio-stream = "0.1.14" tokio-stream = "0.1.14"
mdns-sd = "0.7.2" mdns-sd = "0.7.2"
futures = "0.3.28" futures = "0.3.28"
ddc-hi = "0.4.1"
[features] [features]
# this feature is used for production builds or when `devPath` points to the filesystem # this feature is used for production builds or when `devPath` points to the filesystem

View File

@ -3,8 +3,7 @@ use std::time::SystemTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Serialize, Deserialize, Debug)] #[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct DisplayConfig { pub struct DisplayState {
pub id: usize,
pub brightness: u16, pub brightness: u16,
pub max_brightness: u16, pub max_brightness: u16,
pub min_brightness: u16, pub min_brightness: u16,
@ -17,10 +16,9 @@ pub struct DisplayConfig {
pub last_modified_at: SystemTime, pub last_modified_at: SystemTime,
} }
impl DisplayConfig { impl DisplayState {
pub fn default(index: usize) -> Self { pub fn default() -> Self {
Self { Self {
id: index,
brightness: 30, brightness: 30,
contrast: 50, contrast: 50,
mode: 0, mode: 0,

View File

@ -6,182 +6,205 @@ use std::{
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use base64::Config;
use ddc_hi::Display; use ddc_hi::Display;
use paris::{error, info, warn}; use paris::{error, info, warn};
use tauri::async_runtime::Mutex; use tokio::sync::{OnceCell, OwnedMutexGuard, RwLock};
use tokio::sync::{broadcast, OwnedMutexGuard};
use tracing::warn;
use crate::{display::Brightness, models, rpc}; use super::display_state::DisplayState;
use super::{display_config::DisplayConfig, DisplayBrightness};
use ddc_hi::Ddc; use ddc_hi::Ddc;
pub struct Manager { pub struct DisplayHandler {
displays: Arc<Mutex<HashMap<usize, Arc<Mutex<DisplayConfig>>>>>, pub state: Arc<RwLock<DisplayState>>,
pub controller: Arc<RwLock<Display>>,
} }
impl Manager { pub struct DisplayManager {
pub fn global() -> &'static Self { displays: Arc<RwLock<Vec<Arc<RwLock<DisplayHandler>>>>>,
static DISPLAY_MANAGER: once_cell::sync::OnceCell<Manager> =
once_cell::sync::OnceCell::new();
DISPLAY_MANAGER.get_or_init(|| Self::create())
} }
pub fn create() -> Self { impl DisplayManager {
pub async fn global() -> &'static Self {
static DISPLAY_MANAGER: OnceCell<DisplayManager> = OnceCell::const_new();
DISPLAY_MANAGER.get_or_init(|| Self::create()).await
}
pub async fn create() -> Self {
let instance = Self { let instance = Self {
displays: Arc::new(Mutex::new(HashMap::new())), displays: Arc::new(RwLock::new(Vec::new())),
}; };
instance.fetch_displays().await;
instance instance
} }
pub async fn subscribe_display_brightness(&self) { async fn fetch_displays(&self) {
let rpc = rpc::Manager::global().await; let mut displays = self.displays.write().await;
displays.clear();
let mut rx = rpc.client().subscribe_change_display_brightness_rx(); let controllers = Display::enumerate();
loop { for display in controllers {
if let Ok(display_brightness) = rx.recv().await { let controller = Arc::new(RwLock::new(display));
if let Err(err) = self.set_display_brightness(display_brightness).await { let state = Arc::new(RwLock::new(DisplayState::default()));
error!("set_display_brightness failed. {:?}", err); displays.push(Arc::new(RwLock::new(DisplayHandler { controller, state })));
}
}
} }
} }
fn read_display_config_by_ddc(index: usize) -> anyhow::Result<DisplayConfig> { pub async fn get_displays(&self) -> Vec<DisplayState> {
let mut displays = Display::enumerate(); let displays = self.displays.read().await;
match displays.get_mut(index) { let mut states = Vec::new();
Some(display) => { for display in displays.iter() {
let mut config = DisplayConfig::default(index); let state = display.read().await.state.read().await.clone();
match display.handle.get_vcp_feature(0x10) { states.push(state);
Ok(value) => {
config.max_brightness = value.maximum();
config.min_brightness = 0;
config.brightness = value.value();
}
Err(_) => {}
};
match display.handle.get_vcp_feature(0x12) {
Ok(value) => {
config.max_contrast = value.maximum();
config.min_contrast = 0;
config.contrast = value.value();
}
Err(_) => {}
};
match display.handle.get_vcp_feature(0xdc) {
Ok(value) => {
config.max_mode = value.maximum();
config.min_mode = 0;
config.mode = value.value();
}
Err(_) => {}
};
Ok(config)
}
None => anyhow::bail!("display#{} is missed.", index),
} }
states
} }
async fn get_display(&self, index: usize) -> anyhow::Result<OwnedMutexGuard<DisplayConfig>> { // pub async fn subscribe_display_brightness(&self) {
let mut displays = self.displays.lock().await; // let rpc = rpc::Manager::global().await;
match displays.get_mut(&index) {
Some(config) => {
let mut config = config.to_owned().lock_owned().await;
if config.last_modified_at > SystemTime::now().sub(Duration::from_secs(10)) {
info!("cached");
return Ok(config);
}
return match Self::read_display_config_by_ddc(index) {
Ok(config) => {
let id = config.id;
let value = Arc::new(Mutex::new(config));
let valueGuard = value.clone().lock_owned().await;
displays.insert(id, value);
info!("read form ddc");
Ok(valueGuard)
}
Err(err) => {
warn!(
"can not read config from display by ddc, use CACHED value. {:?}",
err
);
config.last_modified_at = SystemTime::now();
Ok(config)
}
};
}
None => {
let config = Self::read_display_config_by_ddc(index).map_err(|err| {
anyhow::anyhow!(
"can not read config from display by ddc,use DEFAULT value. {:?}",
err
)
})?;
let id = config.id;
let value = Arc::new(Mutex::new(config));
let valueGuard = value.clone().lock_owned().await;
displays.insert(id, value);
Ok(valueGuard)
}
}
}
pub async fn set_display_brightness( // let mut rx = rpc.client().subscribe_change_display_brightness_rx();
&self,
display_brightness: DisplayBrightness,
) -> anyhow::Result<()> {
match Display::enumerate().get_mut(display_brightness.display_index) {
Some(display) => {
match self.get_display(display_brightness.display_index).await {
Ok(mut config) => {
let curr = config.brightness;
info!("curr_brightness: {:?}", curr);
let mut target = match display_brightness.brightness {
Brightness::Relative(v) => curr.wrapping_add_signed(v),
Brightness::Absolute(v) => v,
};
if target.gt(&config.max_brightness) {
target = config.max_brightness;
} else if target.lt(&config.min_brightness) {
target = config.min_brightness;
}
config.brightness = target;
display
.handle
.set_vcp_feature(0x10, target as u16)
.map_err(|err| anyhow::anyhow!("can not set brightness. {:?}", err))?;
let rpc = rpc::Manager::global().await; // loop {
// if let Ok(display_brightness) = rx.recv().await {
// if let Err(err) = self.set_display_brightness(display_brightness).await {
// error!("set_display_brightness failed. {:?}", err);
// }
// }
// }
// }
rpc.publish_desktop_cmd( // fn read_display_config_by_ddc(index: usize) -> anyhow::Result<DisplayState> {
format!("display{}/brightness", display_brightness.display_index) // let mut displays = Display::enumerate();
.as_str(), // match displays.get_mut(index) {
target.to_be_bytes().to_vec(), // Some(display) => {
) // let mut config = DisplayState::default(index);
.await; // match display.handle.get_vcp_feature(0x10) {
} // Ok(value) => {
Err(err) => { // config.max_brightness = value.maximum();
info!( // config.min_brightness = 0;
"can not get display#{} brightness. {:?}", // config.brightness = value.value();
display_brightness.display_index, err // }
); // Err(_) => {}
if let Brightness::Absolute(v) = display_brightness.brightness { // };
display.handle.set_vcp_feature(0x10, v).map_err(|err| { // match display.handle.get_vcp_feature(0x12) {
anyhow::anyhow!("can not set brightness. {:?}", err) // Ok(value) => {
})?; // config.max_contrast = value.maximum();
}; // config.min_contrast = 0;
} // config.contrast = value.value();
}; // }
} // Err(_) => {}
None => { // };
warn!("display#{} is not found.", display_brightness.display_index); // match display.handle.get_vcp_feature(0xdc) {
} // Ok(value) => {
} // config.max_mode = value.maximum();
Ok(()) // config.min_mode = 0;
} // config.mode = value.value();
// }
// Err(_) => {}
// };
// Ok(config)
// }
// None => anyhow::bail!("display#{} is missed.", index),
// }
// }
// async fn get_display(&self, index: usize) -> anyhow::Result<OwnedMutexGuard<DisplayState>> {
// let mut displays = self.displays.lock().await;
// match displays.get_mut(&index) {
// Some(config) => {
// let mut config = config.to_owned().lock_owned().await;
// if config.last_modified_at > SystemTime::now().sub(Duration::from_secs(10)) {
// info!("cached");
// return Ok(config);
// }
// return match Self::read_display_config_by_ddc(index) {
// Ok(config) => {
// let id = config.id;
// let value = Arc::new(Mutex::new(config));
// let valueGuard = value.clone().lock_owned().await;
// displays.insert(id, value);
// info!("read form ddc");
// Ok(valueGuard)
// }
// Err(err) => {
// warn!(
// "can not read config from display by ddc, use CACHED value. {:?}",
// err
// );
// config.last_modified_at = SystemTime::now();
// Ok(config)
// }
// };
// }
// None => {
// let config = Self::read_display_config_by_ddc(index).map_err(|err| {
// anyhow::anyhow!(
// "can not read config from display by ddc,use DEFAULT value. {:?}",
// err
// )
// })?;
// let id = config.id;
// let value = Arc::new(Mutex::new(config));
// let valueGuard = value.clone().lock_owned().await;
// displays.insert(id, value);
// Ok(valueGuard)
// }
// }
// }
// pub async fn set_display_brightness(
// &self,
// display_brightness: DisplayBrightness,
// ) -> anyhow::Result<()> {
// match Display::enumerate().get_mut(display_brightness.display_index) {
// Some(display) => {
// match self.get_display(display_brightness.display_index).await {
// Ok(mut config) => {
// let curr = config.brightness;
// info!("curr_brightness: {:?}", curr);
// let mut target = match display_brightness.brightness {
// Brightness::Relative(v) => curr.wrapping_add_signed(v),
// Brightness::Absolute(v) => v,
// };
// if target.gt(&config.max_brightness) {
// target = config.max_brightness;
// } else if target.lt(&config.min_brightness) {
// target = config.min_brightness;
// }
// config.brightness = target;
// display
// .handle
// .set_vcp_feature(0x10, target as u16)
// .map_err(|err| anyhow::anyhow!("can not set brightness. {:?}", err))?;
// let rpc = rpc::Manager::global().await;
// rpc.publish_desktop_cmd(
// format!("display{}/brightness", display_brightness.display_index)
// .as_str(),
// target.to_be_bytes().to_vec(),
// )
// .await;
// }
// Err(err) => {
// info!(
// "can not get display#{} brightness. {:?}",
// display_brightness.display_index, err
// );
// if let Brightness::Absolute(v) = display_brightness.brightness {
// display.handle.set_vcp_feature(0x10, v).map_err(|err| {
// anyhow::anyhow!("can not set brightness. {:?}", err)
// })?;
// };
// }
// };
// }
// None => {
// warn!("display#{} is not found.", display_brightness.display_index);
// }
// }
// Ok(())
// }
} }

View File

@ -1,11 +1,12 @@
// mod brightness; // mod brightness;
// mod manager; // mod manager;
mod display_config; mod display_state;
mod manager;
pub use display_config::*; pub use display_state::*;
// pub use brightness::*; // pub use brightness::*;
// pub use manager::*; pub use manager::*;

View File

@ -9,6 +9,7 @@ pub mod screenshot;
mod screenshot_manager; mod screenshot_manager;
use ambient_light::{Border, ColorCalibration, LedStripConfig, LedStripConfigGroup}; use ambient_light::{Border, ColorCalibration, LedStripConfig, LedStripConfigGroup};
use display::{DisplayManager, DisplayState};
use display_info::DisplayInfo; use display_info::DisplayInfo;
use paris::{error, info, warn}; use paris::{error, info, warn};
use rpc::{BoardInfo, MqttRpc, UdpRpc}; use rpc::{BoardInfo, MqttRpc, UdpRpc};
@ -203,6 +204,13 @@ async fn get_boards() -> Result<Vec<BoardInfo>, String> {
Ok(boards) Ok(boards)
} }
#[tauri::command]
async fn get_displays() -> Vec<DisplayState> {
let display_manager = DisplayManager::global().await;
display_manager.get_displays().await
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
env_logger::init(); env_logger::init();
@ -230,6 +238,7 @@ async fn main() {
set_color_calibration, set_color_calibration,
read_config, read_config,
get_boards, get_boards,
get_displays
]) ])
.register_uri_scheme_protocol("ambient-light", move |_app, request| { .register_uri_scheme_protocol("ambient-light", move |_app, request| {
let response = ResponseBuilder::new().header("Access-Control-Allow-Origin", "*"); let response = ResponseBuilder::new().header("Access-Control-Allow-Origin", "*");

View File

@ -1,21 +1,23 @@
use std::time::Duration; use std::{sync::Arc, time::Duration};
use paris::{info, warn}; use paris::{info, warn, error};
use tokio::{net::UdpSocket, sync::RwLock, time::timeout}; use tokio::{net::UdpSocket, sync::RwLock, time::timeout, io};
use super::{BoardConnectStatus, BoardInfo}; use super::{BoardConnectStatus, BoardInfo};
#[derive(Debug)] #[derive(Debug)]
pub struct Board { pub struct Board {
pub info: RwLock<BoardInfo>, pub info: Arc<RwLock<BoardInfo>>,
socket: Option<UdpSocket>, socket: Option<Arc<UdpSocket>>,
listen_handler: Option<tokio::task::JoinHandle<()>>,
} }
impl Board { impl Board {
pub fn new(info: BoardInfo) -> Self { pub fn new(info: BoardInfo) -> Self {
Self { Self {
info: RwLock::new(info), info: Arc::new(RwLock::new(info)),
socket: None, socket: None,
listen_handler: None,
} }
} }
@ -24,7 +26,33 @@ impl Board {
let socket = UdpSocket::bind("0.0.0.0:0").await?; let socket = UdpSocket::bind("0.0.0.0:0").await?;
socket.connect((info.address, info.port)).await?; socket.connect((info.address, info.port)).await?;
self.socket = Some(socket); let socket = Arc::new(socket);
self.socket = Some(socket.clone());
let info = self.info.clone();
let handler=tokio::spawn(async move {
let mut buf = [0u8; 128];
if let Err(err) = socket.readable().await {
error!("socket read error: {:?}", err);
return;
}
loop {
match socket.try_recv(&mut buf) {
Ok(len) => {
log::info!("recv: {:?}", &buf[..len]);
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
continue;
}
Err(e) => {
error!("socket recv error: {:?}", e);
break;
}
}
}
});
self.listen_handler = Some(handler);
Ok(()) Ok(())
} }
@ -37,10 +65,7 @@ impl Board {
let socket = self.socket.as_ref().unwrap(); let socket = self.socket.as_ref().unwrap();
socket socket.send(buf).await.unwrap();
.send(buf)
.await
.unwrap();
} }
pub async fn check(&self) -> anyhow::Result<()> { pub async fn check(&self) -> anyhow::Result<()> {
@ -99,3 +124,16 @@ impl Board {
Ok(()) Ok(())
} }
} }
impl Drop for Board {
fn drop(&mut self) {
if let Some(handler) = self.listen_handler.take() {
info!("aborting listen handler");
tokio::task::block_in_place(move || {
handler.abort();
});
info!("listen handler aborted");
}
}
}

View File

@ -6,6 +6,7 @@ import { invoke } from '@tauri-apps/api';
import { setLedStripStore } from './stores/led-strip.store'; import { setLedStripStore } from './stores/led-strip.store';
import { LedStripConfigContainer } from './models/led-strip-config'; import { LedStripConfigContainer } from './models/led-strip-config';
import { InfoIndex } from './components/info/info-index'; import { InfoIndex } from './components/info/info-index';
import { DisplayStateIndex } from './components/displays/display-state-index';
function App() { function App() {
createEffect(() => { createEffect(() => {
@ -23,11 +24,13 @@ function App() {
<div> <div>
<div> <div>
<a href="/info"></a> <a href="/info"></a>
<a href="/displays"></a>
<a href="/led-strips-configuration"></a> <a href="/led-strips-configuration"></a>
<a href="/white-balance"></a> <a href="/white-balance"></a>
</div> </div>
<Routes> <Routes>
<Route path="/info" component={InfoIndex} /> <Route path="/info" component={InfoIndex} />
<Route path="/displays" component={DisplayStateIndex} />
<Route path="/led-strips-configuration" component={LedStripConfiguration} /> <Route path="/led-strips-configuration" component={LedStripConfiguration} />
<Route path="/white-balance" component={WhiteBalance} /> <Route path="/white-balance" component={WhiteBalance} />
</Routes> </Routes>

View File

@ -0,0 +1,36 @@
import { Component, ParentComponent } from 'solid-js';
import { DisplayState } from '../../models/display-state.model';
type DisplayStateCardProps = {
state: DisplayState;
};
type ItemProps = {
label: string;
};
const Item: ParentComponent<ItemProps> = (props) => {
return (
<dl class="flex">
<dt class="w-20">{props.label}</dt>
<dd class="flex-auto">{props.children}</dd>
</dl>
);
};
export const DisplayStateCard: Component<DisplayStateCardProps> = (props) => {
return (
<section class="p-2 rounded shadow">
<Item label="Brightness">{props.state.brightness}</Item>
<Item label="Max Brightness">{props.state.max_brightness}</Item>
<Item label="Min Brightness">{props.state.min_brightness}</Item>
<Item label="Contrast">{props.state.contrast}</Item>
<Item label="Max Contrast">{props.state.max_contrast}</Item>
<Item label="Min Contrast">{props.state.min_contrast}</Item>
<Item label="Max Mode">{props.state.max_mode}</Item>
<Item label="Min Mode">{props.state.min_mode}</Item>
<Item label="Mode">{props.state.mode}</Item>
<Item label="Last Modified At">{props.state.last_modified_at.toISOString()}</Item>
</section>
);
};

View File

@ -0,0 +1,52 @@
import { Component, For, createEffect, createSignal } from 'solid-js';
import { listen } from '@tauri-apps/api/event';
import debug from 'debug';
import { invoke } from '@tauri-apps/api';
import { DisplayState, RawDisplayState } from '../../models/display-state.model';
import { DisplayStateCard } from './display-state-card';
const logger = debug('app:components:displays:display-state-index');
export const DisplayStateIndex: Component = () => {
const [states, setStates] = createSignal<DisplayState[]>([]);
createEffect(() => {
const unlisten = listen<RawDisplayState[]>('displays_changed', (ev) => {
logger('displays_changed', ev);
setStates(
ev.payload.map((it) => ({
...it,
last_modified_at: new Date(it.last_modified_at.secs_since_epoch * 1000),
})),
);
});
invoke<RawDisplayState[]>('get_displays').then((states) => {
logger('get_displays', states);
setStates(
states.map((it) => ({
...it,
last_modified_at: new Date(it.last_modified_at.secs_since_epoch * 1000),
})),
);
});
return () => {
unlisten.then((unlisten) => unlisten());
};
});
return (
<ol class="grid sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 p-2 gap-2">
<For each={states()}>
{(state, index) => (
<li class="bg-slate-50 text-gray-800 relative border-2 border-slate-50 hover:border-sky-300 focus:border-sky-300 transition">
<DisplayStateCard state={state} />
<span class="absolute left-2 -top-3 bg-sky-300 text-white px-1 py-0.5 text-xs rounded-sm font-mono">
#{index() + 1}
</span>
</li>
)}
</For>
</ol>
);
};

View File

@ -0,0 +1,16 @@
export type DisplayState = {
brightness: number;
max_brightness: number;
min_brightness: number;
contrast: number;
max_contrast: number;
min_contrast: number;
mode: number;
max_mode: number;
min_mode: number;
last_modified_at: Date;
};
export type RawDisplayState = DisplayState & {
last_modified_at: { secs_since_epoch: number };
};