Load theme from proxy
This commit is contained in:
parent
586b4027aa
commit
6747366724
7 changed files with 147 additions and 96 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -834,9 +834,9 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cosmic-bg-config",
|
"cosmic-bg-config",
|
||||||
"cosmic-config",
|
"cosmic-config",
|
||||||
|
"cosmic-theme",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"libc",
|
"libc",
|
||||||
"libcosmic",
|
|
||||||
"log",
|
"log",
|
||||||
"pwd",
|
"pwd",
|
||||||
"ron",
|
"ron",
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,10 @@ default-features = false
|
||||||
git = "https://github.com/pop-os/libcosmic"
|
git = "https://github.com/pop-os/libcosmic"
|
||||||
default-features = false
|
default-features = false
|
||||||
|
|
||||||
|
[workspace.dependencies.cosmic-theme]
|
||||||
|
git = "https://github.com/pop-os/libcosmic"
|
||||||
|
default-features = false
|
||||||
|
|
||||||
[workspace.dependencies.libcosmic]
|
[workspace.dependencies.libcosmic]
|
||||||
git = "https://github.com/pop-os/libcosmic"
|
git = "https://github.com/pop-os/libcosmic"
|
||||||
default-features = false
|
default-features = false
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cosmic-bg-config.workspace = true
|
cosmic-bg-config.workspace = true
|
||||||
cosmic-config.workspace = true
|
cosmic-config.workspace = true
|
||||||
|
cosmic-theme.workspace = true
|
||||||
env_logger.workspace = true
|
env_logger.workspace = true
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
libcosmic.workspace = true
|
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
pwd.workspace = true
|
pwd.workspace = true
|
||||||
ron.workspace = true
|
ron.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
pub use cosmic_bg_config::Color;
|
pub use cosmic_bg_config::Color;
|
||||||
|
pub use cosmic_theme::Theme;
|
||||||
|
|
||||||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
|
||||||
pub struct UserData {
|
pub struct UserData {
|
||||||
|
|
@ -6,6 +7,7 @@ pub struct UserData {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub full_name_opt: Option<String>,
|
pub full_name_opt: Option<String>,
|
||||||
pub icon_opt: Option<Vec<u8>>,
|
pub icon_opt: Option<Vec<u8>>,
|
||||||
|
pub theme_opt: Option<Theme>,
|
||||||
pub wallpapers_opt: Option<Vec<(String, WallpaperData)>>,
|
pub wallpapers_opt: Option<Vec<(String, WallpaperData)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,37 @@
|
||||||
use cosmic_bg_config::Source;
|
use cosmic_bg_config::Source;
|
||||||
|
use cosmic_config::CosmicConfigEntry;
|
||||||
use cosmic_greeter_daemon::{UserData, WallpaperData};
|
use cosmic_greeter_daemon::{UserData, WallpaperData};
|
||||||
use std::{error::Error, fs, future::pending, io, path::Path};
|
use std::{env, error::Error, fs, future::pending, io, path::Path};
|
||||||
use zbus::{dbus_interface, ConnectionBuilder, DBusError};
|
use zbus::{dbus_interface, ConnectionBuilder, DBusError};
|
||||||
|
|
||||||
//IMPORTANT: this function is critical to the security of this proxy. It must ensure that the
|
//IMPORTANT: this function is critical to the security of this proxy. It must ensure that the
|
||||||
// callback is executed with the permissions of the specified user id. A good test is to see if
|
// callback is executed with the permissions of the specified user id. A good test is to see if
|
||||||
// the /etc/shadow file can be read with a non-root user, it should fail with EPERM.
|
// the /etc/shadow file can be read with a non-root user, it should fail with EPERM.
|
||||||
fn run_as_user<F: FnOnce() -> T, T>(user: &pwd::Passwd, f: F) -> Result<T, io::Error> {
|
fn run_as_user<F: FnOnce() -> T, T>(user: &pwd::Passwd, f: F) -> Result<T, io::Error> {
|
||||||
|
// Save root HOME
|
||||||
|
let root_home_opt = env::var_os("HOME");
|
||||||
|
|
||||||
|
// Switch to user HOME
|
||||||
|
env::set_var("HOME", &user.dir);
|
||||||
|
|
||||||
|
// Switch to user UID
|
||||||
if unsafe { libc::seteuid(user.uid) } != 0 {
|
if unsafe { libc::seteuid(user.uid) } != 0 {
|
||||||
return Err(io::Error::last_os_error());
|
return Err(io::Error::last_os_error());
|
||||||
}
|
}
|
||||||
|
|
||||||
let t = f();
|
let t = f();
|
||||||
|
|
||||||
|
// Restore root UID
|
||||||
if unsafe { libc::seteuid(0) } != 0 {
|
if unsafe { libc::seteuid(0) } != 0 {
|
||||||
panic!("failed to restore root user id")
|
panic!("failed to restore root user id")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore root HOME
|
||||||
|
match root_home_opt {
|
||||||
|
Some(root_home) => env::set_var("HOME", root_home),
|
||||||
|
None => env::remove_var("HOME"),
|
||||||
|
}
|
||||||
|
|
||||||
Ok(t)
|
Ok(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,77 +102,96 @@ impl GreeterProxy {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut user_data = UserData {
|
||||||
|
uid: user.uid,
|
||||||
|
name: user.name.clone(),
|
||||||
|
full_name_opt: user
|
||||||
|
.gecos
|
||||||
|
.as_ref()
|
||||||
|
.map(|gecos| gecos.split(',').next().unwrap_or_default().to_string()),
|
||||||
|
icon_opt,
|
||||||
|
theme_opt: None,
|
||||||
|
//TODO: should wallpapers come from a per-user call?
|
||||||
|
wallpapers_opt: None,
|
||||||
|
};
|
||||||
|
|
||||||
//IMPORTANT: Assume the identity of the user to ensure we don't read wallpaper file data as root
|
//IMPORTANT: Assume the identity of the user to ensure we don't read wallpaper file data as root
|
||||||
let wallpapers_opt = run_as_user(&user, || {
|
run_as_user(&user, || {
|
||||||
//TODO: use libcosmic to find this path
|
let mut is_dark = true;
|
||||||
let wallpapers_path = Path::new(&user.dir)
|
match cosmic_theme::ThemeMode::config() {
|
||||||
.join(".local/state/cosmic/com.system76.CosmicBackground/v1/wallpapers");
|
Ok(helper) => match cosmic_theme::ThemeMode::get_entry(&helper) {
|
||||||
if wallpapers_path.is_file() {
|
Ok(theme_mode) => {
|
||||||
match fs::read_to_string(&wallpapers_path) {
|
is_dark = theme_mode.is_dark;
|
||||||
Ok(wallpapers_ron) => {
|
}
|
||||||
match ron::from_str::<Vec<(String, Source)>>(&wallpapers_ron) {
|
Err((errs, theme_mode)) => {
|
||||||
Ok(sources) => {
|
log::error!("failed to load cosmic-theme config: {:?}", errs);
|
||||||
let mut wallpaper_datas = Vec::new();
|
is_dark = theme_mode.is_dark;
|
||||||
for (output, source) in sources {
|
}
|
||||||
match source {
|
},
|
||||||
Source::Path(path) => match fs::read(&path) {
|
Err(err) => {
|
||||||
Ok(bytes) => {
|
log::error!("failed to create cosmic-theme mode helper: {:?}", err);
|
||||||
wallpaper_datas.push((
|
}
|
||||||
output,
|
}
|
||||||
WallpaperData::Bytes(bytes),
|
|
||||||
));
|
match if is_dark {
|
||||||
}
|
cosmic_theme::Theme::dark_config()
|
||||||
Err(err) => {
|
} else {
|
||||||
log::error!(
|
cosmic_theme::Theme::light_config()
|
||||||
"failed to read wallpaper {:?}: {:?}",
|
} {
|
||||||
path,
|
Ok(helper) => match cosmic_theme::Theme::get_entry(&helper) {
|
||||||
err
|
Ok(theme) => {
|
||||||
);
|
user_data.theme_opt = Some(theme);
|
||||||
}
|
}
|
||||||
},
|
Err((errs, theme)) => {
|
||||||
Source::Color(color) => {
|
log::error!("failed to load cosmic-theme config: {:?}", errs);
|
||||||
wallpaper_datas
|
user_data.theme_opt = Some(theme);
|
||||||
.push((output, WallpaperData::Color(color)));
|
}
|
||||||
}
|
},
|
||||||
}
|
Err(err) => {
|
||||||
}
|
log::error!("failed to create cosmic-theme config helper: {:?}", err);
|
||||||
Some(wallpaper_datas)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: fallback to background config if background state is not set?
|
||||||
|
let mut wallpaper_state_opt = None;
|
||||||
|
match cosmic_bg_config::state::State::state() {
|
||||||
|
Ok(helper) => match cosmic_bg_config::state::State::get_entry(&helper) {
|
||||||
|
Ok(state) => {
|
||||||
|
wallpaper_state_opt = Some(state);
|
||||||
|
}
|
||||||
|
Err((errs, state)) => {
|
||||||
|
log::error!("failed to load cosmic-bg state: {:?}", errs);
|
||||||
|
wallpaper_state_opt = Some(state);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("failed to create cosmic-bg state helper: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(wallpaper_state) = wallpaper_state_opt {
|
||||||
|
let mut wallpaper_datas = Vec::new();
|
||||||
|
for (output, source) in wallpaper_state.wallpapers {
|
||||||
|
match source {
|
||||||
|
Source::Path(path) => match fs::read(&path) {
|
||||||
|
Ok(bytes) => {
|
||||||
|
wallpaper_datas.push((output, WallpaperData::Bytes(bytes)));
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!(
|
log::error!("failed to read wallpaper {:?}: {:?}", path, err);
|
||||||
"failed to parse wallpapers {:?}: {:?}",
|
|
||||||
wallpapers_path,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
Source::Color(color) => {
|
||||||
|
wallpaper_datas.push((output, WallpaperData::Color(color)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(err) => {
|
|
||||||
log::error!(
|
|
||||||
"failed to read wallpapers {:?}: {:?}",
|
|
||||||
wallpapers_path,
|
|
||||||
err
|
|
||||||
);
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
user_data.wallpapers_opt = Some(wallpaper_datas);
|
||||||
None
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map_err(|err| GreeterError::RunAsUser(err.to_string()))?;
|
.map_err(|err| GreeterError::RunAsUser(err.to_string()))?;
|
||||||
|
|
||||||
user_datas.push(UserData {
|
user_datas.push(user_data);
|
||||||
uid: user.uid,
|
|
||||||
name: user.name,
|
|
||||||
full_name_opt: user
|
|
||||||
.gecos
|
|
||||||
.map(|gecos| gecos.split(',').next().unwrap_or_default().to_string()),
|
|
||||||
icon_opt,
|
|
||||||
//TODO: should wallpapers come from a per-user call?
|
|
||||||
wallpapers_opt,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: is ron the best choice for passing around background data?
|
//TODO: is ron the best choice for passing around background data?
|
||||||
|
|
|
||||||
2
debian/rules
vendored
2
debian/rules
vendored
|
|
@ -23,4 +23,4 @@ override_dh_auto_install:
|
||||||
|
|
||||||
override_dh_installsystemd:
|
override_dh_installsystemd:
|
||||||
dh_installsystemd -pcosmic-greeter --no-start -r cosmic-greeter.service
|
dh_installsystemd -pcosmic-greeter --no-start -r cosmic-greeter.service
|
||||||
dh_installsystemd -pcosmic-greeter-daemon -r cosmic-greeter-daemon.service
|
dh_installsystemd -pcosmic-greeter-daemon cosmic-greeter-daemon.service
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,7 @@ fn user_data_fallback() -> Vec<UserData> {
|
||||||
.gecos
|
.gecos
|
||||||
.map(|gecos| gecos.split(',').next().unwrap_or_default().to_string()),
|
.map(|gecos| gecos.split(',').next().unwrap_or_default().to_string()),
|
||||||
icon_opt,
|
icon_opt,
|
||||||
|
theme_opt: None,
|
||||||
wallpapers_opt: None,
|
wallpapers_opt: None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -388,51 +389,59 @@ pub struct App {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
fn update_wallpapers(&mut self) {
|
fn update_user_config(&mut self) -> Command<Message> {
|
||||||
let username = match &self.username_opt {
|
let username = match &self.username_opt {
|
||||||
Some(some) => some,
|
Some(some) => some,
|
||||||
None => return,
|
None => return Command::none(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let user_data = match self.flags.user_datas.iter().find(|x| &x.name == username) {
|
let user_data = match self.flags.user_datas.iter().find(|x| &x.name == username) {
|
||||||
Some(some) => some,
|
Some(some) => some,
|
||||||
None => return,
|
None => return Command::none(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let wallpapers = match &user_data.wallpapers_opt {
|
if let Some(wallpapers) = &user_data.wallpapers_opt {
|
||||||
Some(some) => some,
|
for (output, surface_id) in self.surface_ids.iter() {
|
||||||
None => return,
|
if self.surface_images.contains_key(surface_id) {
|
||||||
};
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (output, surface_id) in self.surface_ids.iter() {
|
let output_name = match self.surface_names.get(surface_id) {
|
||||||
if self.surface_images.contains_key(surface_id) {
|
Some(some) => some,
|
||||||
continue;
|
None => continue,
|
||||||
}
|
};
|
||||||
|
|
||||||
let output_name = match self.surface_names.get(surface_id) {
|
log::info!("updating wallpaper for {:?}", output_name);
|
||||||
Some(some) => some,
|
|
||||||
None => continue,
|
|
||||||
};
|
|
||||||
|
|
||||||
log::info!("updating wallpaper for {:?}", output_name);
|
for (wallpaper_output_name, wallpaper_data) in wallpapers.iter() {
|
||||||
|
if wallpaper_output_name == output_name {
|
||||||
for (wallpaper_output_name, wallpaper_data) in wallpapers.iter() {
|
match wallpaper_data {
|
||||||
if wallpaper_output_name == output_name {
|
WallpaperData::Bytes(bytes) => {
|
||||||
match wallpaper_data {
|
let image = widget::image::Handle::from_memory(bytes.clone());
|
||||||
WallpaperData::Bytes(bytes) => {
|
self.surface_images.insert(*surface_id, image);
|
||||||
let image = widget::image::Handle::from_memory(bytes.clone());
|
//TODO: what to do about duplicates?
|
||||||
self.surface_images.insert(*surface_id, image);
|
break;
|
||||||
//TODO: what to do about duplicates?
|
}
|
||||||
break;
|
WallpaperData::Color(color) => {
|
||||||
}
|
//TODO: support color sources
|
||||||
WallpaperData::Color(color) => {
|
log::warn!(
|
||||||
//TODO: support color sources
|
"output {}: unsupported source {:?}",
|
||||||
log::warn!("output {}: unsupported source {:?}", output.id(), color);
|
output.id(),
|
||||||
|
color
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match &user_data.theme_opt {
|
||||||
|
Some(theme) => {
|
||||||
|
cosmic::app::command::set_theme(cosmic::Theme::custom(Arc::new(theme.clone())))
|
||||||
|
}
|
||||||
|
None => Command::none(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -533,7 +542,6 @@ impl cosmic::Application for App {
|
||||||
Some(output_name) => {
|
Some(output_name) => {
|
||||||
self.surface_names.insert(surface_id, output_name.clone());
|
self.surface_names.insert(surface_id, output_name.clone());
|
||||||
self.surface_images.remove(&surface_id);
|
self.surface_images.remove(&surface_id);
|
||||||
self.update_wallpapers();
|
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
log::warn!("output {}: no output name", output.id());
|
log::warn!("output {}: no output name", output.id());
|
||||||
|
|
@ -549,6 +557,7 @@ impl cosmic::Application for App {
|
||||||
.insert(surface_id, text_input_id.clone());
|
.insert(surface_id, text_input_id.clone());
|
||||||
|
|
||||||
return Command::batch([
|
return Command::batch([
|
||||||
|
self.update_user_config(),
|
||||||
get_layer_surface(SctkLayerSurfaceSettings {
|
get_layer_surface(SctkLayerSurfaceSettings {
|
||||||
id: surface_id,
|
id: surface_id,
|
||||||
layer: Layer::Overlay,
|
layer: Layer::Overlay,
|
||||||
|
|
@ -642,8 +651,10 @@ impl cosmic::Application for App {
|
||||||
Message::Username(socket, username) => {
|
Message::Username(socket, username) => {
|
||||||
self.username_opt = Some(username.clone());
|
self.username_opt = Some(username.clone());
|
||||||
self.surface_images.clear();
|
self.surface_images.clear();
|
||||||
self.update_wallpapers();
|
return Command::batch([
|
||||||
return request_command(socket, Request::CreateSession { username });
|
self.update_user_config(),
|
||||||
|
request_command(socket, Request::CreateSession { username }),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
Message::Auth(socket, response) => {
|
Message::Auth(socket, response) => {
|
||||||
return request_command(socket, Request::PostAuthMessageResponse { response });
|
return request_command(socket, Request::PostAuthMessageResponse { response });
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue