From ab15ec6eca74cffae4379c68b87e0f8dd6b87cdb Mon Sep 17 00:00:00 2001 From: smudge Date: Mon, 15 Jun 2020 03:03:28 -0400 Subject: [PATCH 01/12] Only install objc/objc-foundation on macos --- Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7360a78..67cca52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,8 @@ readme = "README.md" license = "MIT" [dependencies] +time = "0.2.10" + +[target.'cfg(macos)'.dependencies] objc = "0.2" objc-foundation = "0.1.1" -time = "0.2.10" From 5c2bf0b3a6d56dc14ebe4304dbb399a8c0bcbe16 Mon Sep 17 00:00:00 2001 From: smudge Date: Mon, 15 Jun 2020 23:48:49 -0400 Subject: [PATCH 02/12] [WIP] Get it building on linux (and nothing else) --- src/lib.rs | 18 +++++++++----- src/linux/locale.rs | 19 ++++++++++++++ src/linux/mod.rs | 48 ++++++++++++++++++++++++++++++++++++ src/{ffi => macos}/client.rs | 16 ++++++++++-- src/{ffi => macos}/locale.rs | 0 src/{ffi.rs => macos/mod.rs} | 0 src/{ffi => macos}/status.rs | 0 src/schedule/time.rs | 2 +- 8 files changed, 94 insertions(+), 9 deletions(-) create mode 100644 src/linux/locale.rs create mode 100644 src/linux/mod.rs rename src/{ffi => macos}/client.rs (86%) rename src/{ffi => macos}/locale.rs (100%) rename src/{ffi.rs => macos/mod.rs} (100%) rename src/{ffi => macos}/status.rs (100%) diff --git a/src/lib.rs b/src/lib.rs index 3e3f034..b93a492 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ -mod ffi; +#[cfg_attr(target_os = "linux", path = "linux/mod.rs")] +#[cfg_attr(target_os = "macos", path = "macos/mod.rs")] +mod os; mod schedule; mod status; @@ -6,13 +8,13 @@ pub use schedule::{Schedule, Time}; pub use status::Status; pub struct NightLight { - client: ffi::CBBlueLightClient, + client: os::CBBlueLightClient, } impl NightLight { pub fn new() -> NightLight { NightLight { - client: ffi::CBBlueLightClient::new(), + client: os::CBBlueLightClient::new(), } } @@ -46,8 +48,12 @@ impl NightLight { } pub fn get_schedule(&self) -> Result { - let status = self.client.status()?; - NightLight::schedule(status.mode(), status.from_time(), status.to_time()) + let (from_time, to_time) = self.client.get_schedule()?; + NightLight::schedule( + self.client.get_mode()?, + from_time, + to_time, + ) } pub fn set_temp(&self, temp: i32) -> Result<(), String> { @@ -63,7 +69,7 @@ impl NightLight { } pub fn status(&self) -> Result { - Ok(match self.client.status()?.enabled() { + Ok(match self.client.get_enabled()? { true => Status::On, false => Status::Off, }) diff --git a/src/linux/locale.rs b/src/linux/locale.rs new file mode 100644 index 0000000..c4446ee --- /dev/null +++ b/src/linux/locale.rs @@ -0,0 +1,19 @@ +use std::sync::Arc; + +pub struct Locale { + is_24_hr: bool, +} + +impl Locale { + pub fn initialize() -> Result<(), String> { + Ok(()) + } + + pub fn current() -> Arc { + Arc::new(Locale { is_24_hr: true }) + } + + pub fn is_24_hr(&self) -> bool { + true + } +} diff --git a/src/linux/mod.rs b/src/linux/mod.rs new file mode 100644 index 0000000..fcb7329 --- /dev/null +++ b/src/linux/mod.rs @@ -0,0 +1,48 @@ +use std::os::raw::{c_int, c_float}; + +mod locale; + +pub use self::locale::Locale; + +pub struct CBBlueLightClient { +} + +impl CBBlueLightClient { + pub fn new() -> CBBlueLightClient { + CBBlueLightClient { + + } + } + + pub fn set_enabled(&self, enabled: bool) -> Result<(), String> { + Ok(()) + } + + pub fn set_mode(&self, mode: c_int) -> Result<(), String> { + Ok(()) + } + + pub fn set_schedule(&self, from: (u8, u8), to: (u8, u8)) -> Result<(), String> { + Ok(()) + } + + pub fn set_strength(&self, strength: c_float) -> Result<(), String> { + Ok(()) + } + + pub fn get_strength(&self) -> Result { + Ok(100) + } + + pub fn get_enabled(&self) -> Result { + Ok(false) + } + + pub fn get_mode(&self) -> Result { + Ok(0) + } + + pub fn get_schedule(&self) -> Result<((u8, u8), (u8, u8)), String> { + Ok(((0, 0), (1, 1))) + } +} diff --git a/src/ffi/client.rs b/src/macos/client.rs similarity index 86% rename from src/ffi/client.rs rename to src/macos/client.rs index 662f85f..79c3a16 100644 --- a/src/ffi/client.rs +++ b/src/macos/client.rs @@ -1,4 +1,4 @@ -use crate::ffi::BlueLightStatus; +use crate::os::BlueLightStatus; use objc::rc::StrongPtr; use objc::runtime::{Object, BOOL, YES}; use objc::{class, msg_send, sel, sel_impl}; @@ -71,7 +71,19 @@ impl CBBlueLightClient { } } - pub fn status(&self) -> Result { + pub fn get_enabled(&self) -> Result { + Ok(self.status()?.enabled()) + } + + pub fn get_mode(&self) -> Result { + ok(self.status()?.mode()) + } + + pub fn get_schedule(&self) -> Result<((u8, u8), (u8, u8)), String> { + Ok((self.status()?.from_time(), self.status()?.to_time())) + } + + fn status(&self) -> Result { let mut ptr = BlueLightStatus::c_ptr(); let result: BOOL = unsafe { msg_send![*self.inner, getBlueLightStatus: &mut ptr] }; if result == YES { diff --git a/src/ffi/locale.rs b/src/macos/locale.rs similarity index 100% rename from src/ffi/locale.rs rename to src/macos/locale.rs diff --git a/src/ffi.rs b/src/macos/mod.rs similarity index 100% rename from src/ffi.rs rename to src/macos/mod.rs diff --git a/src/ffi/status.rs b/src/macos/status.rs similarity index 100% rename from src/ffi/status.rs rename to src/macos/status.rs diff --git a/src/schedule/time.rs b/src/schedule/time.rs index 0242f07..9a199bf 100644 --- a/src/schedule/time.rs +++ b/src/schedule/time.rs @@ -1,6 +1,6 @@ extern crate time; -use crate::ffi::Locale; +use crate::os::Locale; use std::fmt; pub struct Time { From e103b6b07007b0416e9db5ead8362cfd591888dc Mon Sep 17 00:00:00 2001 From: smudge Date: Mon, 15 Jun 2020 23:55:13 -0400 Subject: [PATCH 03/12] Ok but actually fix macos build --- Cargo.toml | 3 ++- src/macos/client.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 67cca52..75ea810 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,7 @@ license = "MIT" [dependencies] time = "0.2.10" -[target.'cfg(macos)'.dependencies] + +[target.'cfg(target_os = "macos")'.dependencies] objc = "0.2" objc-foundation = "0.1.1" diff --git a/src/macos/client.rs b/src/macos/client.rs index 79c3a16..00bd7c9 100644 --- a/src/macos/client.rs +++ b/src/macos/client.rs @@ -76,7 +76,7 @@ impl CBBlueLightClient { } pub fn get_mode(&self) -> Result { - ok(self.status()?.mode()) + Ok(self.status()?.mode()) } pub fn get_schedule(&self) -> Result<((u8, u8), (u8, u8)), String> { From 202a7b3d055a858cb098ae0dffc59cdf7ab2fb18 Mon Sep 17 00:00:00 2001 From: smudge Date: Mon, 15 Jun 2020 23:56:47 -0400 Subject: [PATCH 04/12] autoformatting (only works on my mac) --- src/lib.rs | 6 +----- src/linux/mod.rs | 9 +++------ 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b93a492..086301b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,11 +49,7 @@ impl NightLight { pub fn get_schedule(&self) -> Result { let (from_time, to_time) = self.client.get_schedule()?; - NightLight::schedule( - self.client.get_mode()?, - from_time, - to_time, - ) + NightLight::schedule(self.client.get_mode()?, from_time, to_time) } pub fn set_temp(&self, temp: i32) -> Result<(), String> { diff --git a/src/linux/mod.rs b/src/linux/mod.rs index fcb7329..0177590 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -1,17 +1,14 @@ -use std::os::raw::{c_int, c_float}; +use std::os::raw::{c_float, c_int}; mod locale; pub use self::locale::Locale; -pub struct CBBlueLightClient { -} +pub struct CBBlueLightClient {} impl CBBlueLightClient { pub fn new() -> CBBlueLightClient { - CBBlueLightClient { - - } + CBBlueLightClient {} } pub fn set_enabled(&self, enabled: bool) -> Result<(), String> { From c24519987b6fdebec3a8b58e23f1cc6c684a8b40 Mon Sep 17 00:00:00 2001 From: smudge Date: Tue, 16 Jun 2020 18:27:55 -0400 Subject: [PATCH 05/12] Rename CBBlueLightClient to Client --- src/lib.rs | 4 ++-- src/linux/mod.rs | 8 ++++---- src/macos/client.rs | 10 +++++----- src/macos/mod.rs | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 086301b..d0244b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,13 +8,13 @@ pub use schedule::{Schedule, Time}; pub use status::Status; pub struct NightLight { - client: os::CBBlueLightClient, + client: os::Client, } impl NightLight { pub fn new() -> NightLight { NightLight { - client: os::CBBlueLightClient::new(), + client: os::Client::new(), } } diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 0177590..2ac5829 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -4,11 +4,11 @@ mod locale; pub use self::locale::Locale; -pub struct CBBlueLightClient {} +pub struct Client {} -impl CBBlueLightClient { - pub fn new() -> CBBlueLightClient { - CBBlueLightClient {} +impl Client { + pub fn new() -> Client { + Client {} } pub fn set_enabled(&self, enabled: bool) -> Result<(), String> { diff --git a/src/macos/client.rs b/src/macos/client.rs index 00bd7c9..4bfd10e 100644 --- a/src/macos/client.rs +++ b/src/macos/client.rs @@ -5,14 +5,14 @@ use objc::{class, msg_send, sel, sel_impl}; use std::mem::MaybeUninit; use std::os::raw::{c_float, c_int}; -pub struct CBBlueLightClient { +pub struct Client { inner: StrongPtr, } -impl CBBlueLightClient { - pub fn new() -> CBBlueLightClient { - CBBlueLightClient { - inner: CBBlueLightClient::client(), +impl Client { + pub fn new() -> Client { + Client { + inner: Client::client(), } } diff --git a/src/macos/mod.rs b/src/macos/mod.rs index bc81aa5..ef6d3ff 100644 --- a/src/macos/mod.rs +++ b/src/macos/mod.rs @@ -7,6 +7,6 @@ mod client; mod locale; mod status; -pub use self::client::CBBlueLightClient; +pub use self::client::Client; pub use self::locale::Locale; pub use self::status::BlueLightStatus; From d00795811fddd462b5384f8deb8061c411b0b470 Mon Sep 17 00:00:00 2001 From: smudge Date: Tue, 16 Jun 2020 23:54:23 -0400 Subject: [PATCH 06/12] WIP: starting to access gnome settings, status works But on/off doesn't work for some reason. Not writeable? --- Cargo.lock | 196 +++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 +- build.rs | 9 ++- src/linux/mod.rs | 18 ++++- 4 files changed, 219 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 71a4677..56057de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,11 @@ name = "base-x" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "block" version = "0.1.6" @@ -25,6 +30,133 @@ name = "discard" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "futures-channel" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-core" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-executor" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-io" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "futures-macro" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro-hack 0.5.15 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-task" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "once_cell 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "futures-util" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures-core 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-macro 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-project 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-hack 0.5.15 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro-nested 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gio" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-channel 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-io 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "gio-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "glib 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gio-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glib" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-channel 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-executor 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-task 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-util 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "glib-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gobject-sys" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.4.5" @@ -60,6 +192,7 @@ dependencies = [ name = "nightlight" version = "0.1.1" dependencies = [ + "gio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "objc-foundation 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -91,11 +224,49 @@ dependencies = [ "objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "once_cell" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pin-project" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pin-project-internal 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pin-project-internal" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pkg-config" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro-hack" version = "0.5.15" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "proc-macro-nested" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "1.0.10" @@ -178,6 +349,11 @@ name = "sha1" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "standback" version = "0.2.6" @@ -348,10 +524,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum base-x 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1b20b618342cf9891c292c4f5ac2cde7287cc5c87e87e9c769d617793607dec1" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum block 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" "checksum bumpalo 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "12ae9db68ad7fac5fe51304d20f016c911539251075a214f8e663babefa35187" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" "checksum discard 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "212d0f5754cb6769937f4501cc0e67f4f4483c8d2c3e1e922ee9edbe4ab4c7c0" +"checksum futures-channel 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f366ad74c28cca6ba456d95e6422883cfb4b252a83bed929c83abfdbbf2967d5" +"checksum futures-core 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "59f5fff90fd5d971f936ad674802482ba441b6f09ba5e15fd8b39145582ca399" +"checksum futures-executor 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "10d6bb888be1153d3abeb9006b11b02cf5e9b209fda28693c31ae1e4e012e314" +"checksum futures-io 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" +"checksum futures-macro 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d0b5a30a4328ab5473878237c447333c093297bded83a4983d10f4deea240d39" +"checksum futures-task 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "bdb66b5f09e22019b1ab0830f7785bcea8e7a42148683f99214f73f8ec21a626" +"checksum futures-util 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8764574ff08b701a084482c3c7031349104b07ac897393010494beaa18ce32c6" +"checksum gio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0cd10f9415cce39b53f8024bf39a21f84f8157afa52da53837b102e585a296a5" +"checksum gio-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4fad225242b9eae7ec8a063bb86974aca56885014672375e5775dc0ea3533911" +"checksum glib 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)" = "40fb573a09841b6386ddf15fd4bc6655b4f5b106ca962f57ecaecde32a0061c0" +"checksum glib-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "95856f3802f446c05feffa5e24859fe6a183a7cb849c8449afc35c86b1e316e2" +"checksum gobject-sys 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "31d1a804f62034eccf370006ccaef3708a71c31d561fee88564abe71177553d9" "checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)" = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" @@ -360,7 +549,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" "checksum objc-foundation 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9" "checksum objc_id 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b" +"checksum once_cell 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" +"checksum pin-project 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)" = "12e3a6cdbfe94a5e4572812a0201f8c0ed98c1c452c7b8563ce2276988ef9c17" +"checksum pin-project-internal 0.4.22 (registry+https://github.com/rust-lang/crates.io-index)" = "6a0ffd45cf79d88737d7cc85bfd5d2894bee1139b356e616fe85dc389c61aaf7" +"checksum pin-utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" "checksum proc-macro-hack 0.5.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0d659fe7c6d27f25e9d80a1a094c223f5246f6a6596453e09d7229bf42750b63" +"checksum proc-macro-nested 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eba180dafb9038b050a4c280019bbedf9f2467b61e5d892dcad585bb57aadc5a" "checksum proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3" "checksum quote 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2bdc6c187c65bca4260c9011c9e3132efe4909da44726bad24cf7572ae338d7f" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" @@ -372,6 +567,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)" = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" "checksum serde_json 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)" = "da07b57ee2623368351e9a0488bb0b261322a15a6e0ae53e243cbdc0f4208da9" "checksum sha1 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2579985fda508104f7587689507983eadd6a6e84dd35d6d115361f530916fa0d" +"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum standback 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "898341a519ec30272f8665dd05fb00156254310e99df3dc1e0cb096883d8656c" "checksum stdweb 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "d022496b16281348b52d0e30ae99e01a73d737b2f45d38fed4edf79f9325a1d5" "checksum stdweb-derive 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c87a60a40fccc84bef0652345bbbbbe20a605bf5d0ce81719fc476f5c03b50ef" diff --git a/Cargo.toml b/Cargo.toml index 75ea810..8f04bf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,9 @@ license = "MIT" [dependencies] time = "0.2.10" - [target.'cfg(target_os = "macos")'.dependencies] objc = "0.2" objc-foundation = "0.1.1" + +[target.'cfg(target_os = "linux")'.dependencies] +gio = "0.8.1" diff --git a/build.rs b/build.rs index ad34dae..34d6ec7 100644 --- a/build.rs +++ b/build.rs @@ -1,4 +1,7 @@ -fn main() -{ - println!("cargo:rustc-link-search=framework={}", "/System/Library/PrivateFrameworks"); +fn main() { + #[cfg(target_os = "macos")] + println!( + "cargo:rustc-link-search=framework={}", + "/System/Library/PrivateFrameworks" + ); } diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 2ac5829..fd2f125 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -1,18 +1,28 @@ +extern crate gio; + +use gio::SettingsExt; use std::os::raw::{c_float, c_int}; mod locale; pub use self::locale::Locale; -pub struct Client {} +pub struct Client { + settings: gio::Settings, +} impl Client { pub fn new() -> Client { - Client {} + Client { + settings: gio::Settings::new("org.gnome.settings-daemon.plugins.color"), + } } pub fn set_enabled(&self, enabled: bool) -> Result<(), String> { - Ok(()) + match self.settings.set_boolean("night-light-enabled", enabled) { + Ok(_) => Ok(()), + Err(_) => Err(format!("Failed to set enabled to {}", enabled).to_string()), + } } pub fn set_mode(&self, mode: c_int) -> Result<(), String> { @@ -32,7 +42,7 @@ impl Client { } pub fn get_enabled(&self) -> Result { - Ok(false) + Ok(self.settings.get_boolean("night-light-enabled")) } pub fn get_mode(&self) -> Result { From 5f6864fc6b8ffb81ef0fb0f4224fb6f9e2e67f09 Mon Sep 17 00:00:00 2001 From: smudge Date: Sun, 21 Jun 2020 16:33:41 -0400 Subject: [PATCH 07/12] Implement set_strength and get_strength --- src/linux/mod.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/linux/mod.rs b/src/linux/mod.rs index fd2f125..e8bde29 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -34,11 +34,21 @@ impl Client { } pub fn set_strength(&self, strength: c_float) -> Result<(), String> { - Ok(()) + let ratio = strength / 100.0; + let kelvins = (1.0 - ratio) * (6000.0 - 3500.0) + 3500.0; + match self + .settings + .set_uint("night-light-temperature", kelvins as u32) + { + Ok(_) => Ok(()), + Err(_) => Err("Unable to set temperature".to_string()), + } } pub fn get_strength(&self) -> Result { - Ok(100) + let kelvins = self.settings.get_uint("night-light-temperature"); + let ratio = 1.0 - (kelvins as f64 - 3500.0) / (6000.0 - 3500.0); + Ok((ratio * 100.0) as i32) } pub fn get_enabled(&self) -> Result { From 1b6431d0d5f2d61857b34e58d21463409ddb86de Mon Sep 17 00:00:00 2001 From: smudge Date: Sun, 21 Jun 2020 21:54:25 -0400 Subject: [PATCH 08/12] Implement 'set_schedule' and 'get_schedule' --- src/linux/mod.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/linux/mod.rs b/src/linux/mod.rs index e8bde29..5baeefb 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -30,7 +30,15 @@ impl Client { } pub fn set_schedule(&self, from: (u8, u8), to: (u8, u8)) -> Result<(), String> { - Ok(()) + let from = from.0 as f64 + (from.1 as f64 / 60.0); + let to = to.0 as f64 + (to.1 as f64 / 60.0); + match self.settings.set_double("night-light-schedule-from", from) { + Ok(_) => match self.settings.set_double("night-light-schedule-to", to) { + Ok(_) => Ok(()), + Err(_) => Err("Unable to set schedule!".to_string()), + }, + Err(_) => Err("Unable to set schedule!".to_string()), + } } pub fn set_strength(&self, strength: c_float) -> Result<(), String> { @@ -60,6 +68,11 @@ impl Client { } pub fn get_schedule(&self) -> Result<((u8, u8), (u8, u8)), String> { - Ok(((0, 0), (1, 1))) + let from = self.settings.get_double("night-light-schedule-from"); + let to = self.settings.get_double("night-light-schedule-to"); + Ok(( + (from.trunc() as u8, (from.fract() * 60.0).round() as u8), + (to.trunc() as u8, (to.fract() * 60.0).round() as u8), + )) } } From e4f3b787289e95e19168f2e9a763e88f33d980a4 Mon Sep 17 00:00:00 2001 From: smudge Date: Sun, 21 Jun 2020 22:11:53 -0400 Subject: [PATCH 09/12] Define set_mode/get_mode --- src/linux/mod.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 5baeefb..4967022 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -26,7 +26,20 @@ impl Client { } pub fn set_mode(&self, mode: c_int) -> Result<(), String> { - Ok(()) + let (enabled, scheduled) = match mode { + 1 => (true, false), + 2 => (true, true), + _ => (false, false), + }; + + self.set_enabled(enabled)?; + match self + .settings + .set_boolean("night-light-schedule-automatic", scheduled) + { + Ok(_) => Ok(()), + Err(_) => Err("Unable to set schedule!".to_string()), + } } pub fn set_schedule(&self, from: (u8, u8), to: (u8, u8)) -> Result<(), String> { @@ -64,7 +77,14 @@ impl Client { } pub fn get_mode(&self) -> Result { - Ok(0) + match ( + self.get_enabled()?, + self.settings.get_boolean("night-light-schedule-automatic"), + ) { + (true, true) => Ok(1), + (true, false) => Ok(2), + (false, _) => Ok(0), + } } pub fn get_schedule(&self) -> Result<((u8, u8), (u8, u8)), String> { From d5296b52dc9110cde7e1d6ff83e28203720ef7cd Mon Sep 17 00:00:00 2001 From: smudge Date: Sun, 21 Jun 2020 22:50:32 -0400 Subject: [PATCH 10/12] Get write actions actually writing (need to call 'sync') --- src/linux/mod.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 4967022..967f2e3 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -20,15 +20,15 @@ impl Client { pub fn set_enabled(&self, enabled: bool) -> Result<(), String> { match self.settings.set_boolean("night-light-enabled", enabled) { - Ok(_) => Ok(()), + Ok(_) => Ok(gio::Settings::sync()), Err(_) => Err(format!("Failed to set enabled to {}", enabled).to_string()), } } pub fn set_mode(&self, mode: c_int) -> Result<(), String> { let (enabled, scheduled) = match mode { - 1 => (true, false), - 2 => (true, true), + 1 => (true, true), + 2 => (true, false), _ => (false, false), }; @@ -37,7 +37,7 @@ impl Client { .settings .set_boolean("night-light-schedule-automatic", scheduled) { - Ok(_) => Ok(()), + Ok(_) => Ok(gio::Settings::sync()), Err(_) => Err("Unable to set schedule!".to_string()), } } @@ -47,7 +47,7 @@ impl Client { let to = to.0 as f64 + (to.1 as f64 / 60.0); match self.settings.set_double("night-light-schedule-from", from) { Ok(_) => match self.settings.set_double("night-light-schedule-to", to) { - Ok(_) => Ok(()), + Ok(_) => Ok(gio::Settings::sync()), Err(_) => Err("Unable to set schedule!".to_string()), }, Err(_) => Err("Unable to set schedule!".to_string()), @@ -61,7 +61,7 @@ impl Client { .settings .set_uint("night-light-temperature", kelvins as u32) { - Ok(_) => Ok(()), + Ok(_) => Ok(gio::Settings::sync()), Err(_) => Err("Unable to set temperature".to_string()), } } From 1addf318d605d32c5be3ced90c30cebf07916382 Mon Sep 17 00:00:00 2001 From: smudge Date: Sun, 21 Jun 2020 23:02:39 -0400 Subject: [PATCH 11/12] Get 'set_strength' working properly --- src/linux/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/linux/mod.rs b/src/linux/mod.rs index 967f2e3..230b9ce 100644 --- a/src/linux/mod.rs +++ b/src/linux/mod.rs @@ -54,8 +54,7 @@ impl Client { } } - pub fn set_strength(&self, strength: c_float) -> Result<(), String> { - let ratio = strength / 100.0; + pub fn set_strength(&self, ratio: f32) -> Result<(), String> { let kelvins = (1.0 - ratio) * (6000.0 - 3500.0) + 3500.0; match self .settings From 91d31e7fec777fea3e0a541ee7592b8466d0cebd Mon Sep 17 00:00:00 2001 From: smudge Date: Sun, 21 Jun 2020 23:27:01 -0400 Subject: [PATCH 12/12] Bump to 0.2.0 (proof-of-concept Linux support!) --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 56057de..260c4c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -190,7 +190,7 @@ dependencies = [ [[package]] name = "nightlight" -version = "0.1.1" +version = "0.2.0" dependencies = [ "gio 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "objc 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 8f04bf8..40c79d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "nightlight" -version = "0.1.1" +version = "0.2.0" authors = ["smudge "] edition = "2018" categories = ["command-line-utilities", "os::macos-apis"] diff --git a/README.md b/README.md index 55e589e..ee1892c 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ nightlight schedule off In addition to a CLI, `nightlight` can be pulled-in as a dependency for other Rust crates: ``` -nightlight = "0.1.1" +nightlight = "0.2.0" ``` Here's an example `fn` that toggles Night Shift off,