cosmic-greeter/src/locker.rs

743 lines
28 KiB
Rust
Raw Normal View History

2023-10-05 17:47:23 -06:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::app::{message, Command, Core, Settings};
use cosmic::{
executor,
iced::{
2023-10-06 13:07:53 -06:00
self, alignment,
2024-01-17 09:37:15 -07:00
event::{
self,
wayland::{Event as WaylandEvent, OutputEvent, SessionLockEvent},
},
2023-10-05 17:47:23 -06:00
futures::{self, SinkExt},
2023-10-06 10:44:05 -06:00
subscription,
2023-11-21 15:48:22 -07:00
wayland::session_lock::{destroy_lock_surface, get_lock_surface, lock, unlock},
2023-10-06 13:07:53 -06:00
Length, Subscription,
2023-10-05 17:47:23 -06:00
},
2023-10-06 10:44:05 -06:00
iced_runtime::core::window::Id as SurfaceId,
2023-10-06 13:07:53 -06:00
style, widget, Element,
2023-10-05 17:47:23 -06:00
};
use cosmic_config::CosmicConfigEntry;
2023-10-06 10:44:05 -06:00
use std::{
any::TypeId,
2023-10-06 10:44:05 -06:00
collections::HashMap,
ffi::{CStr, CString},
fs,
path::Path,
};
2023-10-05 17:47:23 -06:00
use tokio::{sync::mpsc, task, time};
2023-10-06 10:44:05 -06:00
use wayland_client::{protocol::wl_output::WlOutput, Proxy};
2023-10-05 17:47:23 -06:00
pub fn main(current_user: pwd::Passwd) -> Result<(), Box<dyn std::error::Error>> {
2023-10-06 10:44:05 -06:00
//TODO: use accountsservice
let icon_path = Path::new("/var/lib/AccountsService/icons").join(&current_user.name);
let icon_opt = if icon_path.is_file() {
match fs::read(&icon_path) {
Ok(icon_data) => Some(widget::image::Handle::from_memory(icon_data)),
Err(err) => {
log::error!("failed to read {:?}: {:?}", icon_path, err);
None
}
}
} else {
None
};
2023-10-09 12:31:13 -06:00
let mut wallpapers = Vec::new();
match cosmic_bg_config::state::State::state() {
Ok(helper) => match cosmic_bg_config::state::State::get_entry(&helper) {
Ok(state) => {
wallpapers = state.wallpapers;
2023-10-09 10:59:07 -06:00
}
Err(err) => {
2023-10-09 12:31:13 -06:00
log::error!("failed to load cosmic-bg state: {:?}", err);
2023-10-09 10:59:07 -06:00
}
},
Err(err) => {
2023-10-09 12:31:13 -06:00
log::error!("failed to create cosmic-bg state helper: {:?}", err);
2023-10-09 10:59:07 -06:00
}
}
2023-10-06 10:44:05 -06:00
let flags = Flags {
current_user,
icon_opt,
2023-10-09 12:31:13 -06:00
wallpapers,
2023-10-06 10:44:05 -06:00
};
2023-10-05 17:47:23 -06:00
let settings = Settings::default().no_main_window(true);
2023-10-05 17:47:23 -06:00
cosmic::app::run::<App>(settings, flags)?;
Ok(())
}
pub fn pam_thread(username: String, conversation: Conversation) -> Result<(), pam_client::Error> {
//TODO: send errors to GUI, restart process
// Create PAM context
2023-11-28 08:57:48 -07:00
//TODO: search for and use custom context?
let mut context = pam_client::Context::new("login", Some(&username), conversation)?;
2023-10-05 17:47:23 -06:00
// Authenticate the user (ask for password, 2nd-factor token, fingerprint, etc.)
log::info!("authenticate");
context.authenticate(pam_client::Flag::NONE)?;
// Validate the account (is not locked, expired, etc.)
log::info!("acct_mgmt");
context.acct_mgmt(pam_client::Flag::NONE)?;
Ok(())
}
pub struct Conversation {
msg_tx: futures::channel::mpsc::Sender<Message>,
value_rx: mpsc::Receiver<String>,
}
impl Conversation {
fn prompt_value(
&mut self,
prompt_c: &CStr,
secret: bool,
) -> Result<CString, pam_client::ErrorCode> {
let prompt = prompt_c.to_str().map_err(|err| {
log::error!("failed to convert prompt to UTF-8: {:?}", err);
pam_client::ErrorCode::CONV_ERR
})?;
futures::executor::block_on(async {
self.msg_tx
.send(Message::Prompt(
prompt.to_string(),
secret,
Some(String::new()),
))
2023-10-05 17:47:23 -06:00
.await
})
.map_err(|err| {
log::error!("failed to send prompt: {:?}", err);
pam_client::ErrorCode::CONV_ERR
})?;
let value = self.value_rx.blocking_recv().ok_or_else(|| {
log::error!("failed to receive value: channel closed");
pam_client::ErrorCode::CONV_ERR
})?;
CString::new(value).map_err(|err| {
log::error!("failed to convert value to C string: {:?}", err);
pam_client::ErrorCode::CONV_ERR
})
}
fn message(&mut self, prompt_c: &CStr) -> Result<(), pam_client::ErrorCode> {
let prompt = prompt_c.to_str().map_err(|err| {
log::error!("failed to convert prompt to UTF-8: {:?}", err);
pam_client::ErrorCode::CONV_ERR
})?;
futures::executor::block_on(async {
self.msg_tx
.send(Message::Prompt(prompt.to_string(), false, None))
.await
})
.map_err(|err| {
log::error!("failed to send prompt: {:?}", err);
pam_client::ErrorCode::CONV_ERR
})
}
2023-10-05 17:47:23 -06:00
}
impl pam_client::ConversationHandler for Conversation {
fn prompt_echo_on(&mut self, prompt_c: &CStr) -> Result<CString, pam_client::ErrorCode> {
log::info!("prompt_echo_on {:?}", prompt_c);
self.prompt_value(prompt_c, false)
}
fn prompt_echo_off(&mut self, prompt_c: &CStr) -> Result<CString, pam_client::ErrorCode> {
log::info!("prompt_echo_off {:?}", prompt_c);
self.prompt_value(prompt_c, true)
}
fn text_info(&mut self, prompt_c: &CStr) {
log::info!("text_info {:?}", prompt_c);
match self.message(prompt_c) {
Ok(()) => (),
Err(err) => {
log::warn!("failed to send text_info: {:?}", err);
}
}
2023-10-05 17:47:23 -06:00
}
fn error_msg(&mut self, prompt_c: &CStr) {
//TODO: treat error type differently?
log::info!("error_msg {:?}", prompt_c);
match self.message(prompt_c) {
Ok(()) => (),
Err(err) => {
log::warn!("failed to send error_msg: {:?}", err);
}
}
2023-10-05 17:47:23 -06:00
}
}
#[derive(Clone)]
pub struct Flags {
current_user: pwd::Passwd,
2023-10-06 10:44:05 -06:00
icon_opt: Option<widget::image::Handle>,
2023-10-09 12:31:13 -06:00
wallpapers: Vec<(String, cosmic_bg_config::Source)>,
2023-10-05 17:47:23 -06:00
}
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {
2023-10-06 13:07:53 -06:00
None,
2023-10-06 10:44:05 -06:00
OutputEvent(OutputEvent, WlOutput),
2023-10-26 15:10:09 -07:00
SessionLockEvent(SessionLockEvent),
2023-10-05 17:47:23 -06:00
Channel(mpsc::Sender<String>),
BackgroundState(cosmic_bg_config::state::State),
2024-01-17 11:18:58 -07:00
NetworkIcon(Option<&'static str>),
Prompt(String, bool, Option<String>),
2023-10-05 17:47:23 -06:00
Submit,
2023-11-29 08:02:14 -07:00
Suspend,
2023-10-05 17:47:23 -06:00
Error(String),
Exit,
}
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
flags: Flags,
2023-10-06 10:44:05 -06:00
surface_ids: HashMap<WlOutput, SurfaceId>,
active_surface_id_opt: Option<SurfaceId>,
2023-10-09 12:31:13 -06:00
surface_images: HashMap<SurfaceId, widget::image::Handle>,
surface_names: HashMap<SurfaceId, String>,
2024-01-17 09:37:15 -07:00
text_input_ids: HashMap<SurfaceId, widget::Id>,
2024-01-17 11:18:58 -07:00
network_icon_opt: Option<&'static str>,
2023-10-05 17:47:23 -06:00
value_tx_opt: Option<mpsc::Sender<String>>,
prompt_opt: Option<(String, bool, Option<String>)>,
2023-10-05 17:47:23 -06:00
error_opt: Option<String>,
}
impl App {
//TODO: cache wallpapers by source?
fn update_wallpapers(&mut self) {
for (output, surface_id) in self.surface_ids.iter() {
if self.surface_images.contains_key(surface_id) {
continue;
}
let output_name = match self.surface_names.get(surface_id) {
Some(some) => some,
None => continue,
};
log::info!("updating wallpaper for {:?}", output_name);
for (wallpaper_output_name, wallpaper_source) in self.flags.wallpapers.iter() {
if wallpaper_output_name == output_name {
match wallpaper_source {
cosmic_bg_config::Source::Path(path) => {
match fs::read(path) {
Ok(bytes) => {
let image = widget::image::Handle::from_memory(bytes);
self.surface_images.insert(*surface_id, image);
//TODO: what to do about duplicates?
break;
}
Err(err) => {
log::warn!(
"output {}: failed to load wallpaper {:?}: {:?}",
output.id(),
path,
err
);
}
}
}
cosmic_bg_config::Source::Color(color) => {
//TODO: support color sources
log::warn!("output {}: unsupported source {:?}", output.id(), color);
}
}
}
}
}
}
}
2023-10-05 17:47:23 -06:00
/// Implement [`cosmic::Application`] to integrate with COSMIC.
impl cosmic::Application for App {
/// Default async executor to use with the app.
type Executor = executor::Default;
/// Argument received [`cosmic::Application::new`].
type Flags = Flags;
/// Message type specific to our [`App`].
type Message = Message;
/// The unique application ID to supply to the window manager.
const APP_ID: &'static str = "com.system76.CosmicGreeter";
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
/// Creates the application, and optionally emits command on initialize.
fn init(mut core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
core.window.show_window_menu = false;
core.window.show_headerbar = false;
core.window.sharp_corners = true;
core.window.show_maximize = false;
core.window.show_minimize = false;
core.window.use_template = false;
(
App {
core,
flags,
2023-10-06 10:44:05 -06:00
surface_ids: HashMap::new(),
active_surface_id_opt: None,
2023-10-09 12:31:13 -06:00
surface_images: HashMap::new(),
surface_names: HashMap::new(),
2024-01-17 09:37:15 -07:00
text_input_ids: HashMap::new(),
2024-01-17 11:18:58 -07:00
network_icon_opt: None,
2023-10-05 17:47:23 -06:00
value_tx_opt: None,
prompt_opt: None,
error_opt: None,
},
2023-10-26 15:10:09 -07:00
lock(),
2023-10-05 17:47:23 -06:00
)
}
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
2023-10-06 13:07:53 -06:00
Message::None => {}
2023-10-09 12:31:13 -06:00
Message::OutputEvent(output_event, output) => {
match output_event {
OutputEvent::Created(output_info_opt) => {
log::info!("output {}: created", output.id());
2024-01-17 09:37:15 -07:00
let surface_id = SurfaceId::unique();
2023-10-09 12:31:13 -06:00
match self.surface_ids.insert(output.clone(), surface_id) {
Some(old_surface_id) => {
//TODO: remove old surface?
log::warn!(
2024-01-17 09:37:15 -07:00
"output {}: already had surface ID {:?}",
2023-10-09 12:31:13 -06:00
output.id(),
2024-01-17 09:37:15 -07:00
old_surface_id
2023-10-09 12:31:13 -06:00
);
}
None => {}
2023-10-06 10:44:05 -06:00
}
2023-10-09 12:31:13 -06:00
match output_info_opt {
Some(output_info) => match output_info.name {
Some(output_name) => {
self.surface_names.insert(surface_id, output_name.clone());
self.surface_images.remove(&surface_id);
self.update_wallpapers();
2023-10-09 12:31:13 -06:00
}
None => {
log::warn!("output {}: no output name", output.id());
}
},
2023-10-09 12:31:13 -06:00
None => {
log::warn!("output {}: no output info", output.id());
}
2023-10-06 10:44:05 -06:00
}
2023-10-09 12:31:13 -06:00
2024-01-17 09:37:15 -07:00
let text_input_id = widget::Id::unique();
self.text_input_ids
.insert(surface_id, text_input_id.clone());
2023-10-09 12:31:13 -06:00
return Command::batch([
2023-10-26 15:10:09 -07:00
get_lock_surface(surface_id, output),
2024-01-17 09:37:15 -07:00
widget::text_input::focus(text_input_id),
2023-10-09 12:31:13 -06:00
]);
}
OutputEvent::Removed => {
log::info!("output {}: removed", output.id());
match self.surface_ids.remove(&output) {
Some(surface_id) => {
self.surface_images.remove(&surface_id);
self.surface_names.remove(&surface_id);
2024-01-17 09:37:15 -07:00
self.text_input_ids.remove(&surface_id);
2023-10-26 15:10:09 -07:00
return destroy_lock_surface(surface_id);
2023-10-09 12:31:13 -06:00
}
None => {
log::warn!("output {}: no surface found", output.id());
}
2023-10-06 10:44:05 -06:00
}
}
2023-10-09 12:31:13 -06:00
OutputEvent::InfoUpdate(output_info) => {
log::info!("output {}: info update {:#?}", output.id(), output_info);
}
2023-10-06 10:44:05 -06:00
}
2023-10-09 12:31:13 -06:00
}
2023-10-26 15:10:09 -07:00
Message::SessionLockEvent(session_lock_event) => match session_lock_event {
SessionLockEvent::Focused(_, surface_id) => {
2024-01-17 09:37:15 -07:00
log::info!("focus surface {:?}", surface_id);
self.active_surface_id_opt = Some(surface_id);
2024-01-17 09:37:15 -07:00
if let Some(text_input_id) = self.text_input_ids.get(&surface_id) {
return widget::text_input::focus(text_input_id.clone());
}
}
2023-10-26 15:10:09 -07:00
SessionLockEvent::Unlocked => {
2024-01-17 09:37:15 -07:00
return iced::window::close(SurfaceId::MAIN);
}
2023-10-26 15:10:09 -07:00
_ => {}
},
2023-10-05 17:47:23 -06:00
Message::Channel(value_tx) => {
self.value_tx_opt = Some(value_tx);
}
Message::BackgroundState(background_state) => {
self.flags.wallpapers = background_state.wallpapers;
self.surface_images.clear();
self.update_wallpapers();
}
2024-01-17 11:18:58 -07:00
Message::NetworkIcon(network_icon_opt) => {
self.network_icon_opt = network_icon_opt;
}
Message::Prompt(prompt, secret, value_opt) => {
let prompt_was_none = self.prompt_opt.is_none();
self.prompt_opt = Some((prompt, secret, value_opt));
if prompt_was_none {
if let Some(surface_id) = self.active_surface_id_opt {
2024-01-17 09:37:15 -07:00
if let Some(text_input_id) = self.text_input_ids.get(&surface_id) {
return widget::text_input::focus(text_input_id.clone());
}
}
}
2023-10-05 17:47:23 -06:00
}
Message::Submit => match self.prompt_opt.take() {
Some((_prompt, _secret, value_opt)) => match value_opt {
Some(value) => match self.value_tx_opt.take() {
Some(value_tx) => {
return Command::perform(
async move {
value_tx.send(value).await.unwrap();
message::app(Message::Channel(value_tx))
},
|x| x,
);
}
None => log::warn!("tried to submit when value_tx_opt not set"),
},
None => log::warn!("tried to submit without value"),
2023-10-05 17:47:23 -06:00
},
None => log::warn!("tried to submit without prompt"),
},
2023-11-29 08:02:14 -07:00
Message::Suspend => {
#[cfg(feature = "logind")]
return Command::perform(
async move {
match crate::logind::suspend().await {
Ok(()) => message::none(),
Err(err) => {
log::error!("failed to suspend: {:?}", err);
message::app(Message::Error(err.to_string()))
}
}
},
|x| x,
);
}
2023-10-05 17:47:23 -06:00
Message::Error(error) => {
self.error_opt = Some(error);
}
Message::Exit => {
2023-10-06 13:07:53 -06:00
let mut commands = Vec::new();
for (_output, surface_id) in self.surface_ids.drain() {
2023-10-09 12:31:13 -06:00
self.surface_images.remove(&surface_id);
self.surface_names.remove(&surface_id);
2024-01-17 09:37:15 -07:00
self.text_input_ids.remove(&surface_id);
2023-10-26 15:10:09 -07:00
commands.push(destroy_lock_surface(surface_id));
2023-10-06 13:07:53 -06:00
}
2023-10-26 15:10:09 -07:00
commands.push(unlock());
// Wait to exit until `Unlocked` event, when server has processed unlock
2023-10-06 13:07:53 -06:00
return Command::batch(commands);
2023-10-05 17:47:23 -06:00
}
}
Command::none()
}
2023-10-06 10:44:05 -06:00
// Not used for layer surface window
2023-10-05 17:47:23 -06:00
fn view(&self) -> Element<Self::Message> {
2023-10-06 10:44:05 -06:00
unimplemented!()
}
/// Creates a view after each update.
fn view_window(&self, surface_id: SurfaceId) -> Element<Self::Message> {
2023-10-06 13:07:53 -06:00
let left_element = {
let date_time_column = {
let mut column = widget::column::with_capacity(2).padding(16.0);
2023-10-06 13:07:53 -06:00
let dt = chrono::Local::now();
//TODO: localized format
let date = dt.format("%A, %B %-d");
column = column
.push(widget::text::title2(format!("{}", date)).style(style::Text::Accent));
//TODO: localized format
let time = dt.format("%R");
column = column.push(
widget::text(format!("{}", time))
.size(112.0)
.style(style::Text::Accent),
);
column
};
2024-01-17 11:18:58 -07:00
let mut status_row = widget::row::with_capacity(2).padding(16.0).spacing(12.0);
if let Some(network_icon) = self.network_icon_opt {
status_row = status_row.push(widget::icon::from_name(network_icon));
}
2023-10-06 13:07:53 -06:00
//TODO: get actual status
2024-01-17 11:18:58 -07:00
status_row = status_row.push(iced::widget::row![
widget::icon::from_name("battery-level-50-symbolic"),
widget::text("50%"),
]);
2023-10-05 17:47:23 -06:00
2023-10-06 13:07:53 -06:00
//TODO: implement these buttons
let button_row = iced::widget::row![
widget::button(widget::icon::from_name(
"applications-accessibility-symbolic"
))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("input-keyboard-symbolic"))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("system-users-symbolic"))
.padding(12.0)
.on_press(Message::None),
widget::button(widget::icon::from_name("system-suspend-symbolic"))
.padding(12.0)
2023-11-29 08:02:14 -07:00
.on_press(Message::Suspend),
2023-10-06 13:07:53 -06:00
]
.padding([16.0, 0.0, 0.0, 0.0])
.spacing(8.0);
widget::container(iced::widget::column![
date_time_column,
widget::divider::horizontal::default(),
status_row,
widget::divider::horizontal::default(),
button_row,
])
.width(Length::Fill)
.align_x(alignment::Horizontal::Left)
};
let right_element = {
let mut column = widget::column::with_capacity(2)
.spacing(12.0)
.max_width(280.0);
match &self.flags.icon_opt {
Some(icon) => {
column = column.push(
widget::container(
widget::Image::new(icon.clone())
.width(Length::Fixed(78.0))
.height(Length::Fixed(78.0)),
)
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
)
2023-10-05 17:47:23 -06:00
}
2023-10-06 13:07:53 -06:00
None => {}
}
match &self.flags.current_user.gecos {
Some(gecos) => {
2023-10-25 10:05:13 -06:00
let full_name = gecos.split(",").next().unwrap_or("");
2023-10-06 13:07:53 -06:00
column = column.push(
2023-10-25 10:05:13 -06:00
widget::container(widget::text::title4(full_name))
2023-10-06 13:07:53 -06:00
.width(Length::Fill)
.align_x(alignment::Horizontal::Center),
);
}
None => {}
2023-10-05 17:47:23 -06:00
}
2023-10-06 13:07:53 -06:00
match &self.prompt_opt {
Some((prompt, secret, value_opt)) => match value_opt {
Some(value) => {
2024-01-17 09:37:15 -07:00
let mut text_input = widget::text_input(prompt.clone(), value.clone())
.leading_icon(
widget::icon::from_name("system-lock-screen-symbolic").into(),
)
.trailing_icon(
widget::icon::from_name("document-properties-symbolic").into(),
)
.on_input(|value| Message::Prompt(prompt.clone(), *secret, Some(value)))
.on_submit(Message::Submit);
2024-01-17 09:37:15 -07:00
if let Some(text_input_id) = self.text_input_ids.get(&surface_id) {
text_input = text_input.id(text_input_id.clone());
}
if *secret {
text_input = text_input.password()
}
2023-10-06 13:07:53 -06:00
column = column.push(text_input);
2023-10-06 13:07:53 -06:00
}
None => {
column = column.push(widget::text(prompt));
}
},
2023-10-06 13:07:53 -06:00
None => {}
}
2023-10-05 17:47:23 -06:00
2023-10-06 13:07:53 -06:00
if let Some(error) = &self.error_opt {
column = column.push(widget::text(error));
}
2023-10-05 17:47:23 -06:00
2023-10-06 13:07:53 -06:00
widget::container(column)
.align_x(alignment::Horizontal::Center)
.width(Length::Fill)
};
crate::image_container::ImageContainer::new(
widget::container(
widget::cosmic_container::container(
iced::widget::row![left_element, right_element]
.align_items(alignment::Alignment::Center),
)
.layer(cosmic::cosmic_theme::Layer::Background)
.padding(16)
.style(cosmic::theme::Container::Custom(Box::new(
|theme: &cosmic::Theme| {
// Use background appearance as the base
let mut appearance = widget::container::StyleSheet::appearance(
theme,
&cosmic::theme::Container::Background,
);
appearance.border_radius = 16.0.into();
appearance
},
)))
.width(Length::Fixed(800.0)),
2023-10-06 13:07:53 -06:00
)
.padding([32.0, 0.0, 0.0, 0.0])
.width(Length::Fill)
.height(Length::Fill)
.align_x(alignment::Horizontal::Center)
.align_y(alignment::Vertical::Top)
.style(cosmic::theme::Container::Transparent),
2023-10-06 13:07:53 -06:00
)
2023-10-09 12:31:13 -06:00
.image(match self.surface_images.get(&surface_id) {
Some(some) => some.clone(),
//TODO: default image
None => widget::image::Handle::from_pixels(1, 1, vec![0x00, 0x00, 0x00, 0xFF]),
})
2023-10-06 20:24:03 -06:00
.content_fit(iced::ContentFit::Cover)
2023-10-06 13:07:53 -06:00
.into()
2023-10-05 17:47:23 -06:00
}
fn subscription(&self) -> Subscription<Self::Message> {
struct BackgroundSubscription;
2023-11-21 15:48:22 -07:00
struct HeartbeatSubscription;
struct PamSubscription;
2023-10-05 17:47:23 -06:00
2024-01-17 11:18:58 -07:00
//TODO: just use one vec for all subscriptions
let mut network_subscriptions = Vec::with_capacity(1);
#[cfg(feature = "networkmanager")]
{
network_subscriptions.push(
crate::networkmanager::subscription()
.map(|network_icon_opt| Message::NetworkIcon(network_icon_opt)),
);
}
2023-10-05 17:47:23 -06:00
//TODO: how to avoid cloning this on every time subscription is called?
let username = self.flags.current_user.name.clone();
2023-10-06 10:44:05 -06:00
Subscription::batch([
2024-01-17 09:37:15 -07:00
event::listen_with(|event, _| match event {
2023-10-06 10:44:05 -06:00
iced::Event::PlatformSpecific(iced::event::PlatformSpecific::Wayland(
wayland_event,
)) => match wayland_event {
WaylandEvent::Output(output_event, output) => {
Some(Message::OutputEvent(output_event, output))
}
2023-10-26 15:10:09 -07:00
WaylandEvent::SessionLock(evt) => Some(Message::SessionLockEvent(evt)),
_ => None,
},
2023-10-06 10:44:05 -06:00
_ => None,
}),
cosmic_config::config_state_subscription(
TypeId::of::<BackgroundSubscription>(),
cosmic_bg_config::NAME.into(),
cosmic_bg_config::state::State::version(),
)
.map(|(_, res)| match res {
Ok(background_state) => Message::BackgroundState(background_state),
Err((errs, background_state)) => {
log::info!("errors loading background state: {:?}", errs);
Message::BackgroundState(background_state)
}
}),
2023-10-06 10:44:05 -06:00
subscription::channel(
TypeId::of::<HeartbeatSubscription>(),
2023-11-21 15:48:22 -07:00
16,
|mut msg_tx| async move {
loop {
// Send heartbeat once a second to update time
//TODO: only send this when needed
msg_tx.send(Message::None).await.unwrap();
time::sleep(time::Duration::new(1, 0)).await;
}
},
),
subscription::channel(
TypeId::of::<PamSubscription>(),
2023-10-06 10:44:05 -06:00
16,
|mut msg_tx| async move {
loop {
let (value_tx, value_rx) = mpsc::channel(16);
msg_tx.send(Message::Channel(value_tx)).await.unwrap();
let pam_res = {
let username = username.clone();
let msg_tx = msg_tx.clone();
task::spawn_blocking(move || {
pam_thread(username, Conversation { msg_tx, value_rx })
})
.await
.unwrap()
};
match pam_res {
Ok(()) => {
log::info!("successfully authenticated");
msg_tx.send(Message::Exit).await.unwrap();
break;
}
Err(err) => {
2024-01-17 11:18:58 -07:00
log::warn!("authentication error: {}", err);
2023-10-06 10:44:05 -06:00
msg_tx.send(Message::Error(err.to_string())).await.unwrap();
}
2023-10-05 17:47:23 -06:00
}
}
2023-10-06 10:44:05 -06:00
loop {
2024-01-17 11:18:58 -07:00
time::sleep(time::Duration::new(60, 0)).await;
2023-10-06 10:44:05 -06:00
}
},
),
2024-01-17 11:18:58 -07:00
Subscription::batch(network_subscriptions),
2023-10-06 10:44:05 -06:00
])
2023-10-05 17:47:23 -06:00
}
}