Cleanup config handling

This commit is contained in:
Jeremy Soller 2025-05-09 14:16:12 -06:00
parent 75948f857a
commit c9913834f2
6 changed files with 177 additions and 142 deletions

29
Cargo.lock generated
View file

@ -1081,10 +1081,19 @@ dependencies = [
"libm", "libm",
] ]
[[package]]
name = "cosmic-applets-config"
version = "0.1.0"
source = "git+https://github.com/pop-os/cosmic-applets#edaf5b994e5bd6c78e9e395ebfa4f53c631faeae"
dependencies = [
"cosmic-config",
"serde",
]
[[package]] [[package]]
name = "cosmic-bg-config" name = "cosmic-bg-config"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/cosmic-bg#d41c8506ed5c44afd51f74bdb56f620e1dec1ffc" source = "git+https://github.com/pop-os/cosmic-bg#1da843a63656cf58b373a4823c15326be448b24e"
dependencies = [ dependencies = [
"colorgrad", "colorgrad",
"cosmic-config", "cosmic-config",
@ -1110,7 +1119,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-comp-config" name = "cosmic-comp-config"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/cosmic-comp#99bbd10168aed50a24db730cf20eb778e072c5e4" source = "git+https://github.com/pop-os/cosmic-comp#a57f4a84664b36a661509d9fe58f6940a774e34a"
dependencies = [ dependencies = [
"cosmic-config", "cosmic-config",
"input", "input",
@ -1120,7 +1129,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-config" name = "cosmic-config"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#67df54f38390eda8180b55b55b0fe9825894a62a" source = "git+https://github.com/pop-os/libcosmic#c8c650c0410258bb8cd5321bb93845979c7260a4"
dependencies = [ dependencies = [
"atomicwrites", "atomicwrites",
"calloop 0.14.2", "calloop 0.14.2",
@ -1143,7 +1152,7 @@ dependencies = [
[[package]] [[package]]
name = "cosmic-config-derive" name = "cosmic-config-derive"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/pop-os/libcosmic#67df54f38390eda8180b55b55b0fe9825894a62a" source = "git+https://github.com/pop-os/libcosmic#c8c650c0410258bb8cd5321bb93845979c7260a4"
dependencies = [ dependencies = [
"quote", "quote",
"syn 1.0.109", "syn 1.0.109",
@ -1185,6 +1194,7 @@ dependencies = [
"chrono", "chrono",
"chrono-tz", "chrono-tz",
"clap_lex", "clap_lex",
"cosmic-applets-config",
"cosmic-bg-config", "cosmic-bg-config",
"cosmic-comp-config", "cosmic-comp-config",
"cosmic-config", "cosmic-config",
@ -1231,6 +1241,7 @@ dependencies = [
name = "cosmic-greeter-daemon" name = "cosmic-greeter-daemon"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cosmic-applets-config",
"cosmic-bg-config", "cosmic-bg-config",
"cosmic-comp-config", "cosmic-comp-config",
"cosmic-config", "cosmic-config",
@ -2067,9 +2078,9 @@ dependencies = [
[[package]] [[package]]
name = "freedesktop-desktop-entry" name = "freedesktop-desktop-entry"
version = "0.7.10" version = "0.7.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2258b98a780699da05c858682498ceaf3942013d7d93ca7584e26fbacc58f2d9" checksum = "cbcb2951884fd80c5b3093ce762bebcc4ec351fadff3c253f9d09cb91917ddd2"
dependencies = [ dependencies = [
"gettext-rs", "gettext-rs",
"log", "log",
@ -2625,7 +2636,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_core" name = "iced_core"
version = "0.14.0-dev" version = "0.14.0-dev"
source = "git+https://github.com/pop-os/libcosmic#67df54f38390eda8180b55b55b0fe9825894a62a" source = "git+https://github.com/pop-os/libcosmic#c8c650c0410258bb8cd5321bb93845979c7260a4"
dependencies = [ dependencies = [
"bitflags 2.9.0", "bitflags 2.9.0",
"bytes", "bytes",
@ -2649,7 +2660,7 @@ dependencies = [
[[package]] [[package]]
name = "iced_futures" name = "iced_futures"
version = "0.14.0-dev" version = "0.14.0-dev"
source = "git+https://github.com/pop-os/libcosmic#67df54f38390eda8180b55b55b0fe9825894a62a" source = "git+https://github.com/pop-os/libcosmic#c8c650c0410258bb8cd5321bb93845979c7260a4"
dependencies = [ dependencies = [
"futures", "futures",
"iced_core", "iced_core",
@ -4048,7 +4059,7 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56"
dependencies = [ dependencies = [
"proc-macro-crate 3.3.0", "proc-macro-crate 1.3.1",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.100", "syn 2.0.100",

View file

@ -16,6 +16,7 @@ icu = { version = "1.5.0", features = [
] } ] }
chrono-tz = "0.10" chrono-tz = "0.10"
chrono = { version = "0.4", features = ["unstable-locales"] } chrono = { version = "0.4", features = ["unstable-locales"] }
cosmic-applets-config.workspace = true
cosmic-bg-config.workspace = true cosmic-bg-config.workspace = true
cosmic-comp-config.workspace = true cosmic-comp-config.workspace = true
cosmic-config = { workspace = true, features = ["calloop", "macro"] } cosmic-config = { workspace = true, features = ["calloop", "macro"] }
@ -103,6 +104,9 @@ serde = "1"
tokio = "1.39.1" tokio = "1.39.1"
zbus = "4" zbus = "4"
[workspace.dependencies.cosmic-applets-config]
git = "https://github.com/pop-os/cosmic-applets"
default-features = false
[workspace.dependencies.cosmic-bg-config] [workspace.dependencies.cosmic-bg-config]
git = "https://github.com/pop-os/cosmic-bg" git = "https://github.com/pop-os/cosmic-bg"

View file

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
cosmic-applets-config.workspace = true
cosmic-bg-config.workspace = true cosmic-bg-config.workspace = true
cosmic-comp-config.workspace = true cosmic-comp-config.workspace = true
cosmic-config.workspace = true cosmic-config.workspace = true

View file

@ -1,58 +1,67 @@
use cosmic_config::{ConfigGet, CosmicConfigEntry}; use cosmic_config::CosmicConfigEntry;
use std::{fs, path::Path}; use std::{
collections::BTreeMap,
fs,
path::{Path, PathBuf},
};
pub use cosmic_bg_config::{Color, Source}; pub use cosmic_applets_config::time::TimeAppletConfig;
pub use cosmic_bg_config::{state::State as BgState, Color, Source as BgSource};
pub use cosmic_comp_config::{CosmicCompConfig, XkbConfig}; pub use cosmic_comp_config::{CosmicCompConfig, XkbConfig};
pub use cosmic_theme::Theme; pub use cosmic_theme::Theme;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, Default, serde::Deserialize, serde::Serialize)]
pub struct UserData { pub struct UserData {
pub uid: u32, pub uid: u32,
pub name: String, pub name: String,
pub full_name_opt: Option<String>, pub full_name: String,
pub icon_opt: Option<Vec<u8>>, pub icon_opt: Option<Vec<u8>>,
pub theme_opt: Option<Theme>, pub theme_opt: Option<Theme>,
pub wallpapers_opt: Option<Vec<(String, WallpaperData)>>, pub bg_state: BgState,
pub bg_path_data: BTreeMap<PathBuf, Vec<u8>>,
pub xkb_config_opt: Option<XkbConfig>, pub xkb_config_opt: Option<XkbConfig>,
pub clock_military_time_opt: Option<bool>, pub time_applet_config: TimeAppletConfig,
} }
impl UserData { impl UserData {
pub fn full_name_or_name(&self) -> &str { pub fn load_wallpapers_as_user(&mut self) {
if let Some(full_name) = &self.full_name_opt { //TODO: reload changed background files?
if !full_name.is_empty() { self.bg_path_data.retain(|path, _| {
return full_name.as_str(); self.bg_state
} .wallpapers
} .iter()
self.name.as_str() .any(|(_, source)| match source {
} BgSource::Path(source_path) => source_path == path,
_ => false,
pub fn load_wallpapers_as_user(&mut self, wallpaper_state: &cosmic_bg_config::state::State) { })
let mut wallpaper_datas = Vec::new(); });
for (output, source) in wallpaper_state.wallpapers.iter() { for (_, source) in self.bg_state.wallpapers.iter() {
match source { match source {
Source::Path(path) => match fs::read(&path) { //TODO: do not reread duplicate paths, cache data by path?
Ok(bytes) => { BgSource::Path(path) => {
wallpaper_datas.push((output.clone(), WallpaperData::Bytes(bytes))); if !self.bg_path_data.contains_key(path) {
match fs::read(&path) {
Ok(bytes) => {
self.bg_path_data.insert(path.clone(), bytes);
}
Err(err) => {
log::error!("failed to read wallpaper {:?}: {:?}", path, err);
}
}
} }
Err(err) => {
log::error!("failed to read wallpaper {:?}: {:?}", path, err);
}
},
Source::Color(color) => {
wallpaper_datas.push((output.clone(), WallpaperData::Color(color.clone())));
} }
// Other types not supported
_ => {}
} }
} }
self.wallpapers_opt = Some(wallpaper_datas);
} }
pub fn load_config_as_user(&mut self) { pub fn load_config_as_user(&mut self) {
self.icon_opt = None; self.icon_opt = None;
self.theme_opt = None; self.theme_opt = None;
self.wallpapers_opt = None; self.bg_state = Default::default();
self.xkb_config_opt = None; self.xkb_config_opt = None;
self.clock_military_time_opt = None; self.time_applet_config = Default::default();
//TODO: use accountsservice? //TODO: use accountsservice?
//IMPORTANT: This file is owned by root and safe to read (it won't be a link to /etc/shadow for example) //IMPORTANT: This file is owned by root and safe to read (it won't be a link to /etc/shadow for example)
@ -106,25 +115,21 @@ impl UserData {
} }
//TODO: fallback to background config if background state is not set? //TODO: fallback to background config if background state is not set?
let mut wallpaper_state_opt = None;
match cosmic_bg_config::state::State::state() { match cosmic_bg_config::state::State::state() {
Ok(helper) => match cosmic_bg_config::state::State::get_entry(&helper) { Ok(helper) => match cosmic_bg_config::state::State::get_entry(&helper) {
Ok(state) => { Ok(state) => {
wallpaper_state_opt = Some(state); self.bg_state = state;
} }
Err((errs, state)) => { Err((errs, state)) => {
log::error!("failed to load cosmic-bg state: {:?}", errs); log::error!("failed to load cosmic-bg state: {:?}", errs);
wallpaper_state_opt = Some(state); self.bg_state = state;
} }
}, },
Err(err) => { Err(err) => {
log::error!("failed to create cosmic-bg state helper: {:?}", err); log::error!("failed to create cosmic-bg state helper: {:?}", err);
} }
} }
self.load_wallpapers_as_user();
if let Some(wallpaper_state) = wallpaper_state_opt {
self.load_wallpapers_as_user(&wallpaper_state);
}
match cosmic_config::Config::new("com.system76.CosmicComp", CosmicCompConfig::VERSION) { match cosmic_config::Config::new("com.system76.CosmicComp", CosmicCompConfig::VERSION) {
Ok(config_handler) => match CosmicCompConfig::get_entry(&config_handler) { Ok(config_handler) => match CosmicCompConfig::get_entry(&config_handler) {
@ -141,13 +146,15 @@ impl UserData {
} }
}; };
match cosmic_config::Config::new("com.system76.CosmicAppletTime", 1) { match cosmic_config::Config::new("com.system76.CosmicAppletTime", TimeAppletConfig::VERSION)
Ok(config_handler) => match config_handler.get("military_time") { {
Ok(value) => { Ok(config_handler) => match TimeAppletConfig::get_entry(&config_handler) {
self.clock_military_time_opt = Some(value); Ok(config) => {
self.time_applet_config = config;
} }
Err(err) => { Err((errs, config)) => {
log::error!("failed to load military time config: {:?}", err); log::error!("failed to load time applet config: {:?}", errs);
self.time_applet_config = config;
} }
}, },
Err(err) => { Err(err) => {
@ -162,25 +169,20 @@ impl UserData {
impl From<pwd::Passwd> for UserData { impl From<pwd::Passwd> for UserData {
fn from(user: pwd::Passwd) -> Self { fn from(user: pwd::Passwd) -> Self {
let mut full_name = user
.gecos
.as_ref()
.and_then(|gecos| gecos.split(',').next())
.map(|x| x.to_string())
.unwrap_or_default();
if full_name.is_empty() {
full_name = user.name.clone();
}
Self { Self {
uid: user.uid, uid: user.uid,
name: user.name.clone(), name: user.name.clone(),
full_name_opt: user full_name,
.gecos ..Default::default()
.as_ref()
.and_then(|gecos| gecos.split(',').next())
.map(|x| x.to_string()),
icon_opt: None,
theme_opt: None,
wallpapers_opt: None,
xkb_config_opt: None,
clock_military_time_opt: None,
} }
} }
} }
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub enum WallpaperData {
Bytes(Vec<u8>),
Color(Color),
}

View file

@ -33,7 +33,7 @@ use cosmic::{
}; };
use cosmic_comp_config::CosmicCompConfig; use cosmic_comp_config::CosmicCompConfig;
use cosmic_greeter_config::Config as CosmicGreeterConfig; use cosmic_greeter_config::Config as CosmicGreeterConfig;
use cosmic_greeter_daemon::{UserData, WallpaperData}; use cosmic_greeter_daemon::{BgSource, UserData};
use greetd_ipc::Request; use greetd_ipc::Request;
use std::{ use std::{
collections::{HashMap, hash_map}, collections::{HashMap, hash_map},
@ -432,12 +432,8 @@ impl App {
let military_time = self let military_time = self
.selected_username .selected_username
.data_idx .data_idx
.and_then(|i| { .and_then(|i| self.flags.user_datas.get(i))
self.flags .map(|user_data| user_data.time_applet_config.military_time)
.user_datas
.get(i)
.and_then(|user| user.clock_military_time_opt)
})
.unwrap_or_default(); .unwrap_or_default();
let date_time_column = self.time.date_time_widget(military_time); let date_time_column = self.time.date_time_widget(military_time);
@ -639,11 +635,9 @@ impl App {
None => {} None => {}
} }
column = column.push( column = column.push(
widget::container(widget::text::title4( widget::container(widget::text::title4(&user_data.full_name))
user_data.full_name_or_name(), .width(Length::Fill)
)) .align_x(alignment::Horizontal::Center),
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
); );
} }
} }
@ -834,42 +828,46 @@ impl App {
} }
}; };
if let Some(wallpapers) = &user_data.wallpapers_opt { for (_output, surface_id) in self.surface_ids.iter() {
for (output, surface_id) in self.surface_ids.iter() { if self.surface_images.contains_key(surface_id) {
if self.surface_images.contains_key(surface_id) { continue;
continue; }
}
let output_name = match self.surface_names.get(surface_id) { let Some(output_name) = self.surface_names.get(surface_id) else {
Some(some) => some, continue;
None => continue, };
};
log::info!("updating wallpaper for {:?}", output_name); log::info!("updating wallpaper for {:?}", output_name);
for (wallpaper_output_name, wallpaper_data) in wallpapers.iter() { for (wallpaper_output_name, wallpaper_source) in user_data.bg_state.wallpapers.iter() {
if wallpaper_output_name == output_name { if wallpaper_output_name == output_name {
match wallpaper_data { match wallpaper_source {
WallpaperData::Bytes(bytes) => { BgSource::Path(path) => {
self.surface_images match user_data.bg_path_data.get(path) {
.insert(*surface_id, image::Handle::from_bytes(bytes.clone())); Some(bytes) => {
let image = widget::image::Handle::from_bytes(bytes.clone());
//TODO: what to do about duplicates? self.surface_images.insert(*surface_id, image);
break; //TODO: what to do about duplicates?
} }
WallpaperData::Color(color) => { None => {
//TODO: support color sources log::warn!(
log::warn!( "output {}: failed to find wallpaper data for source {:?}",
"output {}: unsupported source {:?}", output_name,
output.id(), path
color );
); }
} }
break;
}
BgSource::Color(color) => {
//TODO: support color sources
log::warn!("output {}: unsupported source {:?}", output_name, color);
} }
} }
} }
} }
} }
// From cosmic-applet-input-sources // From cosmic-applet-input-sources
if let Some(keyboard_layouts) = &self.flags.layouts_opt { if let Some(keyboard_layouts) = &self.flags.layouts_opt {
if let Some(xkb_config) = &user_data.xkb_config_opt { if let Some(xkb_config) = &user_data.xkb_config_opt {
@ -964,15 +962,11 @@ impl cosmic::Application for App {
core.window.show_minimize = false; core.window.show_minimize = false;
core.window.use_template = false; core.window.use_template = false;
//TODO: use full_name_opt //TODO: use full_name?
let mut usernames: Vec<_> = flags let mut usernames: Vec<_> = flags
.user_datas .user_datas
.iter() .iter()
.map(|x| { .map(|x| (x.name.clone(), x.full_name.clone()))
let name = x.name.clone();
let full_name = x.full_name_opt.clone().unwrap_or_else(|| name.clone());
(name, full_name)
})
.collect(); .collect();
usernames.sort_by(|a, b| a.1.cmp(&b.1)); usernames.sort_by(|a, b| a.1.cmp(&b.1));

View file

@ -22,7 +22,8 @@ use cosmic::{
iced_runtime::core::window::Id as SurfaceId, iced_runtime::core::window::Id as SurfaceId,
widget, widget,
}; };
use cosmic_greeter_daemon::{UserData, WallpaperData}; use cosmic_config::CosmicConfigEntry;
use cosmic_greeter_daemon::{BgSource, TimeAppletConfig, UserData};
use std::time::Duration; use std::time::Duration;
use std::{ use std::{
any::TypeId, any::TypeId,
@ -194,6 +195,7 @@ pub enum Message {
SessionLockEvent(SessionLockEvent), SessionLockEvent(SessionLockEvent),
Channel(mpsc::Sender<String>), Channel(mpsc::Sender<String>),
BackgroundState(cosmic_bg_config::state::State), BackgroundState(cosmic_bg_config::state::State),
TimeAppletConfig(TimeAppletConfig),
Focus(SurfaceId), Focus(SurfaceId),
Inhibit(Arc<OwnedFd>), Inhibit(Arc<OwnedFd>),
NetworkIcon(Option<&'static str>), NetworkIcon(Option<&'static str>),
@ -264,11 +266,7 @@ impl App {
window_width window_width
}; };
let left_element = { let left_element = {
let military_time = self let military_time = self.flags.user_data.time_applet_config.military_time;
.flags
.user_data
.clock_military_time_opt
.unwrap_or_default();
let date_time_column = self.time.date_time_widget(military_time); let date_time_column = self.time.date_time_widget(military_time);
let mut status_row = widget::row::with_capacity(2).padding(16.0).spacing(12.0); let mut status_row = widget::row::with_capacity(2).padding(16.0).spacing(12.0);
@ -336,11 +334,9 @@ impl App {
} }
column = column.push( column = column.push(
widget::container(widget::text::title4( widget::container(widget::text::title4(&self.flags.user_data.full_name))
self.flags.user_data.full_name_or_name(), .width(Length::Fill)
)) .align_x(alignment::Horizontal::Center),
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
); );
match &self.prompt_opt { match &self.prompt_opt {
@ -421,7 +417,9 @@ impl App {
//TODO: cache wallpapers by source? //TODO: cache wallpapers by source?
fn update_wallpapers(&mut self) { fn update_wallpapers(&mut self) {
for (output, surface_id) in self.surface_ids.iter() { let user_data = &self.flags.user_data;
for (_output, surface_id) in self.surface_ids.iter() {
if self.surface_images.contains_key(surface_id) { if self.surface_images.contains_key(surface_id) {
continue; continue;
} }
@ -432,22 +430,29 @@ impl App {
log::info!("updating wallpaper for {:?}", output_name); log::info!("updating wallpaper for {:?}", output_name);
let Some(wallpapers) = &self.flags.user_data.wallpapers_opt else { for (wallpaper_output_name, wallpaper_source) in user_data.bg_state.wallpapers.iter() {
continue;
};
for (wallpaper_output_name, wallpaper_data) in wallpapers.iter() {
if wallpaper_output_name == output_name { if wallpaper_output_name == output_name {
match wallpaper_data { match wallpaper_source {
WallpaperData::Bytes(bytes) => { BgSource::Path(path) => {
let image = widget::image::Handle::from_bytes(bytes.clone()); match user_data.bg_path_data.get(path) {
self.surface_images.insert(*surface_id, image); Some(bytes) => {
//TODO: what to do about duplicates? let image = widget::image::Handle::from_bytes(bytes.clone());
self.surface_images.insert(*surface_id, image);
//TODO: what to do about duplicates?
}
None => {
log::warn!(
"output {}: failed to find wallpaper data for source {:?}",
output_name,
path
);
}
}
break; break;
} }
WallpaperData::Color(color) => { BgSource::Color(color) => {
//TODO: support color sources //TODO: support color sources
log::warn!("output {}: unsupported source {:?}", output.id(), color); log::warn!("output {}: unsupported source {:?}", output_name, color);
} }
} }
} }
@ -837,13 +842,16 @@ impl cosmic::Application for App {
Message::Channel(value_tx) => { Message::Channel(value_tx) => {
self.value_tx_opt = Some(value_tx); self.value_tx_opt = Some(value_tx);
} }
Message::BackgroundState(background_state) => { Message::BackgroundState(bg_state) => {
self.flags eprintln!("{:#?}", bg_state);
.user_data self.flags.user_data.bg_state = bg_state;
.load_wallpapers_as_user(&background_state); self.flags.user_data.load_wallpapers_as_user();
self.surface_images.clear(); self.surface_images.clear();
self.update_wallpapers(); self.update_wallpapers();
} }
Message::TimeAppletConfig(config) => {
self.flags.user_data.time_applet_config = config;
}
Message::Inhibit(inhibit) => { Message::Inhibit(inhibit) => {
self.inhibit_opt = Some(inhibit); self.inhibit_opt = Some(inhibit);
} }
@ -1031,6 +1039,21 @@ impl cosmic::Application for App {
}), }),
); );
struct TimeAppletSubscription;
subscriptions.push(
cosmic_config::config_subscription(
TypeId::of::<TimeAppletSubscription>(),
"com.system76.CosmicAppletTime".into(),
TimeAppletConfig::VERSION,
)
.map(|res| {
if !res.errors.is_empty() {
log::info!("errors loading background state: {:?}", res.errors);
}
Message::TimeAppletConfig(res.config)
}),
);
#[cfg(feature = "logind")] #[cfg(feature = "logind")]
{ {
subscriptions.push(crate::logind::subscription()); subscriptions.push(crate::logind::subscription());