2023-10-05 17:47:23 -06:00
|
|
|
// Copyright 2023 System76 <info@system76.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
|
|
2025-03-12 16:07:57 -04:00
|
|
|
use cosmic::app::{Core, Settings, Task};
|
|
|
|
|
use cosmic::cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity;
|
|
|
|
|
use cosmic::iced::{Point, Rectangle, Size};
|
|
|
|
|
use cosmic::iced_runtime::platform_specific::wayland::subsurface::SctkSubsurfaceSettings;
|
2025-03-05 23:08:56 -05:00
|
|
|
use cosmic::surface;
|
2023-10-05 17:47:23 -06:00
|
|
|
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},
|
2025-02-21 17:05:50 -05:00
|
|
|
platform_specific::shell::wayland::commands::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
|
|
|
};
|
2023-10-12 09:18:04 -06:00
|
|
|
use cosmic_config::CosmicConfigEntry;
|
2023-10-06 10:44:05 -06:00
|
|
|
use std::{
|
2023-11-27 15:41:21 -07:00
|
|
|
any::TypeId,
|
2023-10-06 10:44:05 -06:00
|
|
|
collections::HashMap,
|
2024-11-07 09:37:16 -07:00
|
|
|
env,
|
2023-10-06 10:44:05 -06:00
|
|
|
ffi::{CStr, CString},
|
|
|
|
|
fs,
|
2024-04-05 19:02:20 -06:00
|
|
|
os::fd::OwnedFd,
|
2024-11-07 09:37:16 -07:00
|
|
|
path::{Path, PathBuf},
|
2024-01-17 12:37:22 -07:00
|
|
|
process,
|
2024-04-05 19:02:20 -06:00
|
|
|
sync::Arc,
|
2023-10-06 10:44:05 -06:00
|
|
|
};
|
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
|
|
|
|
2024-11-07 09:37:16 -07:00
|
|
|
fn lockfile_opt() -> Option<PathBuf> {
|
|
|
|
|
let runtime_dir = dirs::runtime_dir()?;
|
2025-01-24 09:24:46 +01:00
|
|
|
let session_id = env::var("XDG_SESSION_ID").ok()?;
|
2024-11-07 09:37:16 -07:00
|
|
|
Some(runtime_dir.join(format!("cosmic-greeter-{}.lock", session_id)))
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 17:47:23 -06:00
|
|
|
pub fn main(current_user: pwd::Passwd) -> Result<(), Box<dyn std::error::Error>> {
|
2024-06-04 22:17:44 -06:00
|
|
|
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
|
|
|
|
|
|
2024-05-07 10:07:48 -06:00
|
|
|
crate::localize::localize();
|
|
|
|
|
|
2023-10-06 10:44:05 -06:00
|
|
|
//TODO: use accountsservice
|
|
|
|
|
let icon_path = Path::new("/var/lib/AccountsService/icons").join(¤t_user.name);
|
|
|
|
|
let icon_opt = if icon_path.is_file() {
|
|
|
|
|
match fs::read(&icon_path) {
|
2025-02-21 17:05:50 -05:00
|
|
|
Ok(icon_data) => Some(widget::image::Handle::from_bytes(icon_data)),
|
2023-10-06 10:44:05 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2025-03-12 16:07:57 -04:00
|
|
|
let fallback_background =
|
|
|
|
|
widget::image::Handle::from_bytes(include_bytes!("../res/background.jpg").as_slice());
|
2023-10-06 13:37:38 -06:00
|
|
|
|
2023-10-06 10:44:05 -06:00
|
|
|
let flags = Flags {
|
|
|
|
|
current_user,
|
|
|
|
|
icon_opt,
|
2024-11-07 09:37:16 -07:00
|
|
|
lockfile_opt: lockfile_opt(),
|
2023-10-09 12:31:13 -06:00
|
|
|
wallpapers,
|
2025-03-12 16:07:57 -04:00
|
|
|
fallback_background,
|
2023-10-06 10:44:05 -06:00
|
|
|
};
|
2023-10-05 17:47:23 -06:00
|
|
|
|
2023-10-11 19:16:45 -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
|
2024-09-06 10:34:37 -06:00
|
|
|
let mut context = pam_client::Context::new("cosmic-greeter", 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
|
|
|
|
|
})?;
|
2025-02-21 17:05:50 -05:00
|
|
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
|
|
|
|
.enable_all()
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
runtime
|
|
|
|
|
.block_on(async {
|
|
|
|
|
self.msg_tx
|
|
|
|
|
.send(Message::Prompt(
|
|
|
|
|
prompt.to_string(),
|
|
|
|
|
secret,
|
|
|
|
|
Some(String::new()),
|
|
|
|
|
))
|
|
|
|
|
.await
|
|
|
|
|
})
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
log::error!("failed to send prompt: {:?}", err);
|
|
|
|
|
pam_client::ErrorCode::CONV_ERR
|
|
|
|
|
})?;
|
2023-10-05 17:47:23 -06:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-10-06 15:02:25 -06:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|
2023-10-06 15:02:25 -06:00
|
|
|
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
|
|
|
}
|
2023-10-06 15:02:25 -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>,
|
2024-11-07 09:37:16 -07:00
|
|
|
lockfile_opt: Option<PathBuf>,
|
2023-10-09 12:31:13 -06:00
|
|
|
wallpapers: Vec<(String, cosmic_bg_config::Source)>,
|
2025-03-12 16:07:57 -04:00
|
|
|
fallback_background: widget::image::Handle,
|
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>),
|
2023-11-27 15:41:21 -07:00
|
|
|
BackgroundState(cosmic_bg_config::state::State),
|
2025-03-12 16:07:57 -04:00
|
|
|
Focus(SurfaceId),
|
2024-04-05 19:02:20 -06:00
|
|
|
Inhibit(Arc<OwnedFd>),
|
2024-01-17 11:18:58 -07:00
|
|
|
NetworkIcon(Option<&'static str>),
|
2024-01-17 12:37:22 -07:00
|
|
|
PowerInfo(Option<(String, f64)>),
|
2023-10-06 15:02:25 -06:00
|
|
|
Prompt(String, bool, Option<String>),
|
2025-03-12 16:07:57 -04:00
|
|
|
Submit(String),
|
2025-03-05 23:08:56 -05:00
|
|
|
Surface(surface::Action),
|
2023-11-29 08:02:14 -07:00
|
|
|
Suspend,
|
2023-10-05 17:47:23 -06:00
|
|
|
Error(String),
|
2024-04-05 14:08:40 -06:00
|
|
|
Lock,
|
2024-04-05 11:31:15 -06:00
|
|
|
Unlock,
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
2024-04-05 14:08:40 -06:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
enum State {
|
|
|
|
|
Locking,
|
|
|
|
|
Locked,
|
|
|
|
|
Unlocking,
|
|
|
|
|
Unlocked,
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-05 17:47:23 -06:00
|
|
|
/// The [`App`] stores application-specific state.
|
|
|
|
|
pub struct App {
|
|
|
|
|
core: Core,
|
|
|
|
|
flags: Flags,
|
2024-04-05 14:08:40 -06:00
|
|
|
state: State,
|
2025-03-12 16:07:57 -04:00
|
|
|
output_names: HashMap<WlOutput, String>,
|
2023-10-06 10:44:05 -06:00
|
|
|
surface_ids: HashMap<WlOutput, SurfaceId>,
|
2025-03-12 16:07:57 -04:00
|
|
|
subsurface_rects: HashMap<WlOutput, Rectangle>,
|
2023-10-09 10:59:45 -06:00
|
|
|
active_surface_id_opt: Option<SurfaceId>,
|
2023-10-09 12:31:13 -06:00
|
|
|
surface_images: HashMap<SurfaceId, widget::image::Handle>,
|
2023-11-27 15:41:21 -07:00
|
|
|
surface_names: HashMap<SurfaceId, String>,
|
2025-03-12 16:07:57 -04:00
|
|
|
text_input_ids: HashMap<String, widget::Id>,
|
2024-04-05 19:02:20 -06:00
|
|
|
inhibit_opt: Option<Arc<OwnedFd>>,
|
2024-01-17 11:18:58 -07:00
|
|
|
network_icon_opt: Option<&'static str>,
|
2024-01-17 12:37:22 -07:00
|
|
|
power_info_opt: Option<(String, f64)>,
|
2023-10-05 17:47:23 -06:00
|
|
|
value_tx_opt: Option<mpsc::Sender<String>>,
|
2023-10-06 15:02:25 -06:00
|
|
|
prompt_opt: Option<(String, bool, Option<String>)>,
|
2023-10-05 17:47:23 -06:00
|
|
|
error_opt: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 15:41:21 -07:00
|
|
|
impl App {
|
2025-03-12 16:07:57 -04:00
|
|
|
fn menu<'a>(&'a self, surface_id: SurfaceId) -> Element<'a, Message> {
|
|
|
|
|
let left_element = {
|
|
|
|
|
let date_time_column = {
|
|
|
|
|
let mut column = widget::column::with_capacity(2).padding(16.0);
|
|
|
|
|
|
|
|
|
|
let dt = chrono::Local::now();
|
|
|
|
|
let locale = *crate::localize::LANGUAGE_CHRONO;
|
|
|
|
|
|
|
|
|
|
let date = dt.format_localized("%A, %B %-d", locale);
|
|
|
|
|
column = column
|
|
|
|
|
.push(widget::text::title2(format!("{}", date)).class(style::Text::Accent));
|
|
|
|
|
|
|
|
|
|
let time = dt.format_localized("%R", locale);
|
|
|
|
|
column = column.push(
|
|
|
|
|
widget::text(format!("{}", time))
|
|
|
|
|
.size(112.0)
|
|
|
|
|
.class(style::Text::Accent),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
column
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some((power_icon, power_percent)) = &self.power_info_opt {
|
|
|
|
|
status_row = status_row.push(iced::widget::row![
|
|
|
|
|
widget::icon::from_name(power_icon.clone()),
|
|
|
|
|
widget::text(format!("{:.0}%", power_percent)),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: implement these buttons
|
|
|
|
|
let button_row = iced::widget::row![
|
|
|
|
|
widget::button::custom(widget::icon::from_name(
|
|
|
|
|
"applications-accessibility-symbolic"
|
|
|
|
|
))
|
|
|
|
|
.padding(12.0)
|
|
|
|
|
.on_press(Message::None),
|
|
|
|
|
widget::button::custom(widget::icon::from_name("input-keyboard-symbolic"))
|
|
|
|
|
.padding(12.0)
|
|
|
|
|
.on_press(Message::None),
|
|
|
|
|
widget::button::custom(widget::icon::from_name("system-users-symbolic"))
|
|
|
|
|
.padding(12.0)
|
|
|
|
|
.on_press(Message::None),
|
|
|
|
|
widget::button::custom(widget::icon::from_name("system-suspend-symbolic"))
|
|
|
|
|
.padding(12.0)
|
|
|
|
|
.on_press(Message::Suspend),
|
|
|
|
|
]
|
|
|
|
|
.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),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
match self
|
|
|
|
|
.flags
|
|
|
|
|
.current_user
|
|
|
|
|
.gecos
|
|
|
|
|
.as_ref()
|
|
|
|
|
.filter(|s| !s.is_empty())
|
|
|
|
|
{
|
|
|
|
|
Some(gecos) => {
|
|
|
|
|
let full_name = gecos.split(",").next().unwrap_or_default();
|
|
|
|
|
column = column.push(
|
|
|
|
|
widget::container(widget::text::title4(full_name))
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.align_x(alignment::Horizontal::Center),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match &self.prompt_opt {
|
|
|
|
|
Some((prompt, secret, value_opt)) => match value_opt {
|
|
|
|
|
Some(value) => {
|
|
|
|
|
let text_input_id = self
|
|
|
|
|
.surface_names
|
|
|
|
|
.get(&surface_id)
|
|
|
|
|
.and_then(|id| self.text_input_ids.get(id))
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or_else(|| cosmic::widget::Id::new("text_input"));
|
|
|
|
|
|
|
|
|
|
let mut text_input = widget::secure_input(
|
|
|
|
|
prompt.clone(),
|
|
|
|
|
"",
|
|
|
|
|
Some(Message::Prompt(
|
|
|
|
|
prompt.clone(),
|
|
|
|
|
!*secret,
|
|
|
|
|
Some(value.clone()),
|
|
|
|
|
)),
|
|
|
|
|
*secret,
|
|
|
|
|
)
|
|
|
|
|
.id(text_input_id)
|
|
|
|
|
.manage_value(true)
|
|
|
|
|
.on_submit(|v| Message::Submit(v));
|
|
|
|
|
|
|
|
|
|
if *secret {
|
|
|
|
|
text_input = text_input.password()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
column = column.push(text_input);
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
column = column.push(widget::text(prompt));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(error) = &self.error_opt {
|
|
|
|
|
column = column.push(widget::text(error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widget::container(column)
|
|
|
|
|
.align_x(alignment::Horizontal::Center)
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
widget::container(
|
|
|
|
|
widget::layer_container(
|
|
|
|
|
iced::widget::row![left_element, right_element]
|
|
|
|
|
.align_y(alignment::Alignment::Center),
|
|
|
|
|
)
|
|
|
|
|
.layer(cosmic::cosmic_theme::Layer::Background)
|
|
|
|
|
.padding(16)
|
|
|
|
|
.class(cosmic::theme::Container::Custom(Box::new(
|
|
|
|
|
|theme: &cosmic::Theme| {
|
|
|
|
|
// Use background appearance as the base
|
|
|
|
|
let mut appearance = widget::container::Catalog::style(
|
|
|
|
|
theme,
|
|
|
|
|
&cosmic::theme::Container::Background,
|
|
|
|
|
);
|
|
|
|
|
appearance.border = iced::Border::default().rounded(16.0);
|
|
|
|
|
appearance
|
|
|
|
|
},
|
|
|
|
|
)))
|
|
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Shrink),
|
|
|
|
|
)
|
|
|
|
|
.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)
|
|
|
|
|
.class(cosmic::theme::Container::Transparent)
|
|
|
|
|
.into()
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-27 15:41:21 -07:00
|
|
|
//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) => {
|
2025-02-21 17:05:50 -05:00
|
|
|
let image = widget::image::Handle::from_bytes(bytes);
|
2023-11-27 15:41:21 -07:00
|
|
|
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.
|
2025-02-21 17:05:50 -05:00
|
|
|
fn init(mut core: Core, flags: Self::Flags) -> (Self, Task<Self::Message>) {
|
2023-10-05 17:47:23 -06:00
|
|
|
core.window.show_window_menu = false;
|
|
|
|
|
core.window.show_headerbar = false;
|
2025-03-12 16:07:57 -04:00
|
|
|
// XXX must be false or define custom style to have transparent bg
|
|
|
|
|
core.window.sharp_corners = false;
|
2023-10-05 17:47:23 -06:00
|
|
|
core.window.show_maximize = false;
|
|
|
|
|
core.window.show_minimize = false;
|
|
|
|
|
core.window.use_template = false;
|
|
|
|
|
|
2024-11-07 09:37:16 -07:00
|
|
|
let already_locked = match flags.lockfile_opt {
|
|
|
|
|
Some(ref lockfile) => lockfile.exists(),
|
|
|
|
|
None => false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut app = App {
|
|
|
|
|
core,
|
|
|
|
|
flags,
|
|
|
|
|
state: State::Unlocked,
|
|
|
|
|
surface_ids: HashMap::new(),
|
|
|
|
|
active_surface_id_opt: None,
|
2025-03-12 16:07:57 -04:00
|
|
|
output_names: HashMap::new(),
|
2024-11-07 09:37:16 -07:00
|
|
|
surface_images: HashMap::new(),
|
|
|
|
|
surface_names: HashMap::new(),
|
|
|
|
|
text_input_ids: HashMap::new(),
|
2025-03-12 16:07:57 -04:00
|
|
|
subsurface_rects: HashMap::new(),
|
2024-11-07 09:37:16 -07:00
|
|
|
inhibit_opt: None,
|
|
|
|
|
network_icon_opt: None,
|
|
|
|
|
power_info_opt: None,
|
|
|
|
|
value_tx_opt: None,
|
|
|
|
|
prompt_opt: None,
|
|
|
|
|
error_opt: None,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let command = if cfg!(feature = "logind") {
|
|
|
|
|
if already_locked {
|
|
|
|
|
// Recover previously locked state
|
|
|
|
|
log::info!("recovering previous locked state");
|
|
|
|
|
app.state = State::Locking;
|
|
|
|
|
lock()
|
|
|
|
|
} else {
|
2024-04-05 14:08:40 -06:00
|
|
|
// When logind feature is used, wait for lock signal
|
2025-02-21 17:05:50 -05:00
|
|
|
Task::none()
|
2024-11-07 09:37:16 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// When logind feature not used, lock immediately
|
|
|
|
|
log::info!("locking immediately");
|
|
|
|
|
app.state = State::Locking;
|
|
|
|
|
lock()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(app, command)
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Handle application events here.
|
2025-02-21 17:05:50 -05:00
|
|
|
fn update(&mut self, message: Self::Message) -> Task<Self::Message> {
|
2023-10-05 17:47:23 -06:00
|
|
|
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();
|
2025-03-12 16:07:57 -04:00
|
|
|
let subsurface_id = SurfaceId::unique();
|
2025-02-21 17:05:50 -05:00
|
|
|
|
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
|
|
|
);
|
2025-02-21 17:05:50 -05:00
|
|
|
return Task::none();
|
2023-10-09 12:31:13 -06:00
|
|
|
}
|
|
|
|
|
None => {}
|
2023-10-06 10:44:05 -06:00
|
|
|
}
|
2025-03-12 16:07:57 -04:00
|
|
|
let size = if let Some((w, h)) =
|
|
|
|
|
output_info_opt.as_ref().and_then(|info| info.logical_size)
|
|
|
|
|
{
|
|
|
|
|
Some((Some(w as u32), Some(h as u32)))
|
|
|
|
|
} else {
|
|
|
|
|
Some((None, None))
|
|
|
|
|
};
|
2023-10-09 12:31:13 -06:00
|
|
|
match output_info_opt {
|
2023-11-27 15:41:21 -07:00
|
|
|
Some(output_info) => match output_info.name {
|
|
|
|
|
Some(output_name) => {
|
2025-03-12 16:07:57 -04:00
|
|
|
self.output_names
|
|
|
|
|
.insert(output.clone(), output_name.clone());
|
2023-11-27 15:41:21 -07:00
|
|
|
self.surface_names.insert(surface_id, output_name.clone());
|
2025-03-12 16:07:57 -04:00
|
|
|
self.surface_names
|
|
|
|
|
.insert(subsurface_id, output_name.clone());
|
2023-11-27 15:41:21 -07:00
|
|
|
self.surface_images.remove(&surface_id);
|
|
|
|
|
self.update_wallpapers();
|
2025-03-12 16:07:57 -04:00
|
|
|
let text_input_id =
|
|
|
|
|
widget::Id::new(format!("input-{output_name}",));
|
|
|
|
|
self.text_input_ids
|
|
|
|
|
.insert(output_name.clone(), text_input_id.clone());
|
2023-10-09 12:31:13 -06:00
|
|
|
}
|
2023-11-27 15:41:21 -07: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
|
|
|
|
2025-03-12 16:07:57 -04:00
|
|
|
let unwrapped_size = size
|
|
|
|
|
.map(|s| (s.0.unwrap_or(1920), s.1.unwrap_or(1080)))
|
|
|
|
|
.unwrap_or((1920, 1080));
|
|
|
|
|
let (loc, sub_size) = if unwrapped_size.0 > 800 {
|
|
|
|
|
(
|
|
|
|
|
Point::new(unwrapped_size.0 as f32 / 2. - 400., 32.),
|
|
|
|
|
Size::new(800., unwrapped_size.1 as f32 - 32.),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(
|
|
|
|
|
Point::new(0., 32.),
|
|
|
|
|
Size::new(unwrapped_size.0 as f32, unwrapped_size.1 as f32 - 32.),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
self.subsurface_rects
|
|
|
|
|
.insert(output.clone(), Rectangle::new(loc, sub_size));
|
|
|
|
|
|
|
|
|
|
let msg = cosmic::surface::action::subsurface(
|
|
|
|
|
move |_: &mut App| SctkSubsurfaceSettings {
|
|
|
|
|
parent: surface_id,
|
|
|
|
|
id: subsurface_id,
|
|
|
|
|
loc,
|
|
|
|
|
size: Some(sub_size),
|
|
|
|
|
z: 10,
|
|
|
|
|
steal_keyboard_focus: true,
|
|
|
|
|
gravity: Gravity::BottomRight,
|
|
|
|
|
offset: (0, 0),
|
|
|
|
|
input_zone: None,
|
|
|
|
|
},
|
|
|
|
|
Some(Box::new(move |app: &App| {
|
|
|
|
|
app.menu(subsurface_id).map(cosmic::Action::App)
|
|
|
|
|
})),
|
|
|
|
|
);
|
2024-01-17 09:37:15 -07:00
|
|
|
|
2024-04-05 14:08:40 -06:00
|
|
|
if matches!(self.state, State::Locked) {
|
2025-02-21 17:05:50 -05:00
|
|
|
return Task::batch([
|
2024-04-05 14:08:40 -06:00
|
|
|
get_lock_surface(surface_id, output),
|
2025-03-12 16:07:57 -04:00
|
|
|
cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(msg),
|
|
|
|
|
)),
|
2024-04-05 14:08:40 -06:00
|
|
|
]);
|
|
|
|
|
}
|
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);
|
2023-11-27 15:41:21 -07:00
|
|
|
self.surface_names.remove(&surface_id);
|
2025-03-12 16:07:57 -04:00
|
|
|
if let Some(n) = self.surface_names.remove(&surface_id) {
|
|
|
|
|
self.text_input_ids.remove(&n);
|
|
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
if matches!(self.state, State::Locked) {
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2025-03-12 16:07:57 -04:00
|
|
|
OutputEvent::InfoUpdate(info) => {
|
|
|
|
|
let size = if let Some((w, h)) = info.logical_size {
|
|
|
|
|
Some((Some(w as u32), Some(h as u32)))
|
|
|
|
|
} else {
|
|
|
|
|
Some((None, None))
|
|
|
|
|
};
|
|
|
|
|
let unwrapped_size = size
|
|
|
|
|
.map(|s| (s.0.unwrap_or(1920), s.1.unwrap_or(1080)))
|
|
|
|
|
.unwrap_or((1920, 1080));
|
|
|
|
|
let (loc, sub_size) = if unwrapped_size.0 > 800 {
|
|
|
|
|
(
|
|
|
|
|
Point::new(unwrapped_size.0 as f32 / 2. - 400., 32.),
|
|
|
|
|
Size::new(800., unwrapped_size.1 as f32 - 32.),
|
|
|
|
|
)
|
|
|
|
|
} else {
|
|
|
|
|
(Point::ORIGIN, Size::new(1920., 1080.))
|
|
|
|
|
};
|
|
|
|
|
self.subsurface_rects
|
|
|
|
|
.insert(output.clone(), Rectangle::new(loc, sub_size));
|
|
|
|
|
|
2024-04-05 14:08:40 -06:00
|
|
|
log::info!("output {}: info update", output.id());
|
2023-10-09 12:31:13 -06:00
|
|
|
}
|
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 {
|
2025-03-12 16:07:57 -04:00
|
|
|
SessionLockEvent::Focused(..) => {}
|
2024-04-05 14:08:40 -06:00
|
|
|
SessionLockEvent::Locked => {
|
|
|
|
|
log::info!("session locked");
|
2025-03-12 16:07:57 -04:00
|
|
|
if matches!(self.state, State::Locked) {
|
|
|
|
|
return Task::none();
|
|
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
self.state = State::Locked;
|
2024-04-05 19:02:20 -06:00
|
|
|
// Allow suspend
|
|
|
|
|
self.inhibit_opt = None;
|
2024-11-07 09:37:16 -07:00
|
|
|
// Create lock surfaces
|
2025-03-12 16:07:57 -04:00
|
|
|
|
2024-04-05 14:08:40 -06:00
|
|
|
let mut commands = Vec::with_capacity(self.surface_ids.len());
|
|
|
|
|
for (output, surface_id) in self.surface_ids.iter() {
|
|
|
|
|
commands.push(get_lock_surface(*surface_id, output.clone()));
|
2025-03-12 16:07:57 -04:00
|
|
|
|
|
|
|
|
if let Some((rect, name)) = self
|
|
|
|
|
.subsurface_rects
|
|
|
|
|
.get(output)
|
|
|
|
|
.copied()
|
|
|
|
|
.zip(self.output_names.get(output))
|
|
|
|
|
{
|
|
|
|
|
let subsurface_id = SurfaceId::unique();
|
|
|
|
|
let surface_id = *surface_id;
|
|
|
|
|
self.surface_names.insert(surface_id, name.clone());
|
|
|
|
|
self.surface_names.insert(subsurface_id, name.clone());
|
|
|
|
|
let msg = cosmic::surface::action::subsurface(
|
|
|
|
|
move |_: &mut App| SctkSubsurfaceSettings {
|
|
|
|
|
parent: surface_id,
|
|
|
|
|
id: subsurface_id,
|
|
|
|
|
loc: Point::new(rect.x, rect.y),
|
|
|
|
|
size: Some(Size::new(rect.width, rect.height)),
|
|
|
|
|
z: 10,
|
|
|
|
|
steal_keyboard_focus: true,
|
|
|
|
|
gravity: Gravity::BottomRight,
|
|
|
|
|
offset: (0, 0),
|
|
|
|
|
input_zone: None,
|
|
|
|
|
},
|
|
|
|
|
Some(Box::new(move |app: &App| {
|
|
|
|
|
app.menu(subsurface_id).map(cosmic::Action::App)
|
|
|
|
|
})),
|
|
|
|
|
);
|
|
|
|
|
commands.push(cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(msg),
|
|
|
|
|
)));
|
|
|
|
|
} else {
|
|
|
|
|
log::error!("no rectangle for subsurface...");
|
|
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
}
|
2025-02-21 17:05:50 -05:00
|
|
|
return Task::batch(commands);
|
2024-04-05 14:08:40 -06:00
|
|
|
}
|
2023-10-26 15:10:09 -07:00
|
|
|
SessionLockEvent::Unlocked => {
|
2024-04-05 14:08:40 -06:00
|
|
|
log::info!("session unlocked");
|
|
|
|
|
self.state = State::Unlocked;
|
2025-03-12 16:07:57 -04:00
|
|
|
|
|
|
|
|
let mut commands = Vec::new();
|
|
|
|
|
for (_output, surface_id) in self.surface_ids.iter() {
|
|
|
|
|
self.surface_names.remove(surface_id);
|
|
|
|
|
|
|
|
|
|
commands.push(destroy_lock_surface(*surface_id));
|
|
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
if cfg!(feature = "logind") {
|
2025-03-12 16:07:57 -04:00
|
|
|
return Task::batch(commands);
|
2024-04-05 14:08:40 -06:00
|
|
|
// When using logind feature, stick around for more lock signals
|
|
|
|
|
} else {
|
|
|
|
|
// When not using logind feature, exit immediately after unlocking
|
|
|
|
|
//TODO: cleaner method to exit?
|
|
|
|
|
process::exit(0);
|
|
|
|
|
}
|
2023-10-09 10:59:45 -06:00
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
//TODO: handle finished signal
|
2023-10-26 15:10:09 -07:00
|
|
|
_ => {}
|
2023-10-09 10:59:45 -06:00
|
|
|
},
|
2023-10-05 17:47:23 -06:00
|
|
|
Message::Channel(value_tx) => {
|
|
|
|
|
self.value_tx_opt = Some(value_tx);
|
|
|
|
|
}
|
2023-11-27 15:41:21 -07:00
|
|
|
Message::BackgroundState(background_state) => {
|
|
|
|
|
self.flags.wallpapers = background_state.wallpapers;
|
|
|
|
|
self.surface_images.clear();
|
|
|
|
|
self.update_wallpapers();
|
|
|
|
|
}
|
2024-04-05 19:02:20 -06:00
|
|
|
Message::Inhibit(inhibit) => {
|
|
|
|
|
self.inhibit_opt = Some(inhibit);
|
|
|
|
|
}
|
2024-01-17 11:18:58 -07:00
|
|
|
Message::NetworkIcon(network_icon_opt) => {
|
|
|
|
|
self.network_icon_opt = network_icon_opt;
|
|
|
|
|
}
|
2024-01-17 12:37:22 -07:00
|
|
|
Message::PowerInfo(power_info_opt) => {
|
|
|
|
|
self.power_info_opt = power_info_opt;
|
|
|
|
|
}
|
2025-03-12 16:07:57 -04:00
|
|
|
Message::Focus(surface_id) => {
|
|
|
|
|
self.active_surface_id_opt = Some(surface_id);
|
|
|
|
|
self.active_surface_id_opt = Some(surface_id);
|
|
|
|
|
if let Some(text_input_id) = self
|
|
|
|
|
.surface_names
|
|
|
|
|
.get(&surface_id)
|
|
|
|
|
.and_then(|id| self.text_input_ids.get(id))
|
|
|
|
|
{
|
|
|
|
|
return widget::text_input::focus(text_input_id.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 15:02:25 -06:00
|
|
|
Message::Prompt(prompt, secret, value_opt) => {
|
2023-10-09 10:59:45 -06:00
|
|
|
let prompt_was_none = self.prompt_opt.is_none();
|
2023-10-06 15:02:25 -06:00
|
|
|
self.prompt_opt = Some((prompt, secret, value_opt));
|
2023-10-09 10:59:45 -06:00
|
|
|
if prompt_was_none {
|
|
|
|
|
if let Some(surface_id) = self.active_surface_id_opt {
|
2025-03-12 16:07:57 -04:00
|
|
|
if let Some(text_input_id) = self
|
|
|
|
|
.surface_names
|
|
|
|
|
.get(&surface_id)
|
|
|
|
|
.and_then(|id| self.text_input_ids.get(id))
|
|
|
|
|
{
|
|
|
|
|
log::error!("focus surface found id {:?}", text_input_id);
|
|
|
|
|
|
2024-01-17 09:37:15 -07:00
|
|
|
return widget::text_input::focus(text_input_id.clone());
|
|
|
|
|
}
|
2023-10-09 10:59:45 -06:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
2025-03-12 16:07:57 -04:00
|
|
|
Message::Submit(value) => match self.value_tx_opt.take() {
|
|
|
|
|
Some(value_tx) => {
|
|
|
|
|
// Clear errors
|
|
|
|
|
self.error_opt = None;
|
|
|
|
|
return cosmic::task::future(async move {
|
|
|
|
|
value_tx.send(value).await.unwrap();
|
|
|
|
|
Message::Channel(value_tx)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
None => log::warn!("tried to submit when value_tx_opt not set"),
|
2023-10-05 17:47:23 -06:00
|
|
|
},
|
2023-11-29 08:02:14 -07:00
|
|
|
Message::Suspend => {
|
|
|
|
|
#[cfg(feature = "logind")]
|
2025-02-21 17:05:50 -05:00
|
|
|
return cosmic::task::future(async move {
|
2024-07-28 05:12:48 +02:00
|
|
|
match crate::logind::suspend().await {
|
2025-03-05 23:08:56 -05:00
|
|
|
Ok(()) => cosmic::action::none(),
|
2024-07-28 05:12:48 +02:00
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("failed to suspend: {:?}", err);
|
2025-03-05 23:08:56 -05:00
|
|
|
cosmic::Action::App(Message::Error(err.to_string()))
|
2023-11-29 08:02:14 -07:00
|
|
|
}
|
2024-07-28 05:12:48 +02:00
|
|
|
}
|
|
|
|
|
});
|
2023-11-29 08:02:14 -07:00
|
|
|
}
|
2023-10-05 17:47:23 -06:00
|
|
|
Message::Error(error) => {
|
|
|
|
|
self.error_opt = Some(error);
|
|
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
Message::Lock => match self.state {
|
|
|
|
|
State::Unlocked => {
|
|
|
|
|
log::info!("session locking");
|
|
|
|
|
self.state = State::Locking;
|
|
|
|
|
// Clear errors
|
|
|
|
|
self.error_opt = None;
|
|
|
|
|
// Clear value_tx
|
|
|
|
|
self.value_tx_opt = None;
|
2024-11-07 09:37:16 -07:00
|
|
|
// Try to create lockfile when locking
|
|
|
|
|
if let Some(ref lockfile) = self.flags.lockfile_opt {
|
|
|
|
|
if let Err(err) = fs::File::create(lockfile) {
|
|
|
|
|
log::warn!("failed to create lockfile {:?}: {}", lockfile, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Tell compositor to lock
|
2024-04-05 14:08:40 -06:00
|
|
|
return lock();
|
|
|
|
|
}
|
|
|
|
|
State::Unlocking => {
|
|
|
|
|
log::info!("session still unlocking");
|
|
|
|
|
}
|
|
|
|
|
State::Locking | State::Locked => {
|
|
|
|
|
log::info!("session already locking or locked");
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-04-05 11:31:15 -06:00
|
|
|
Message::Unlock => {
|
2024-04-05 14:08:40 -06:00
|
|
|
match self.state {
|
|
|
|
|
State::Locked => {
|
|
|
|
|
log::info!("sessing unlocking");
|
|
|
|
|
self.state = State::Unlocking;
|
|
|
|
|
// Clear errors
|
|
|
|
|
self.error_opt = None;
|
|
|
|
|
// Clear value_tx
|
|
|
|
|
self.value_tx_opt = None;
|
2024-11-07 09:37:16 -07:00
|
|
|
// Try to delete lockfile when unlocking
|
|
|
|
|
if let Some(ref lockfile) = self.flags.lockfile_opt {
|
|
|
|
|
if let Err(err) = fs::remove_file(lockfile) {
|
|
|
|
|
log::warn!("failed to remove lockfile {:?}: {}", lockfile, err);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-12 16:07:57 -04:00
|
|
|
|
2024-11-07 09:37:16 -07:00
|
|
|
// Destroy lock surfaces
|
2024-04-05 14:08:40 -06:00
|
|
|
let mut commands = Vec::with_capacity(self.surface_ids.len() + 1);
|
2024-11-07 09:37:16 -07:00
|
|
|
// Tell compositor to unlock
|
2024-04-05 14:08:40 -06:00
|
|
|
commands.push(unlock());
|
2025-03-12 16:07:57 -04:00
|
|
|
|
2024-04-05 14:08:40 -06:00
|
|
|
// Wait to exit until `Unlocked` event, when server has processed unlock
|
2025-02-21 17:05:50 -05:00
|
|
|
return Task::batch(commands);
|
2024-04-05 14:08:40 -06:00
|
|
|
}
|
|
|
|
|
State::Locking => {
|
|
|
|
|
log::info!("session still locking");
|
|
|
|
|
}
|
|
|
|
|
State::Unlocking | State::Unlocked => {
|
|
|
|
|
log::info!("session already unlocking or unlocked");
|
|
|
|
|
}
|
2023-10-06 13:07:53 -06:00
|
|
|
}
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
2025-03-05 23:08:56 -05:00
|
|
|
Message::Surface(a) => {
|
|
|
|
|
return cosmic::task::message(cosmic::Action::Cosmic(
|
|
|
|
|
cosmic::app::Action::Surface(a),
|
|
|
|
|
));
|
|
|
|
|
}
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
2025-02-21 17:05:50 -05:00
|
|
|
Task::none()
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
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.
|
2023-10-09 10:59:45 -06:00
|
|
|
fn view_window(&self, surface_id: SurfaceId) -> Element<Self::Message> {
|
2025-03-12 16:07:57 -04:00
|
|
|
let img = self
|
|
|
|
|
.surface_images
|
|
|
|
|
.get(&surface_id)
|
|
|
|
|
.unwrap_or(&self.flags.fallback_background);
|
|
|
|
|
widget::image(img)
|
|
|
|
|
.content_fit(iced::ContentFit::Cover)
|
2023-10-06 13:37:38 -06:00
|
|
|
.width(Length::Fill)
|
|
|
|
|
.height(Length::Fill)
|
2025-03-12 16:07:57 -04:00
|
|
|
.into()
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn subscription(&self) -> Subscription<Self::Message> {
|
2024-04-05 14:08:40 -06:00
|
|
|
let mut subscriptions = Vec::with_capacity(7);
|
|
|
|
|
|
2025-03-12 16:07:57 -04:00
|
|
|
subscriptions.push(event::listen_with(|event, _, id| match event {
|
2024-04-05 14:08:40 -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))
|
2024-04-05 11:01:57 -06:00
|
|
|
}
|
2024-04-05 14:08:40 -06:00
|
|
|
WaylandEvent::SessionLock(evt) => Some(Message::SessionLockEvent(evt)),
|
2023-10-06 10:44:05 -06:00
|
|
|
_ => None,
|
2024-04-05 14:08:40 -06:00
|
|
|
},
|
2025-03-12 16:07:57 -04:00
|
|
|
iced::Event::Window(iced::window::Event::Focused) => Some(Message::Focus(id)),
|
2024-04-05 14:08:40 -06:00
|
|
|
_ => None,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
struct BackgroundSubscription;
|
|
|
|
|
subscriptions.push(
|
2023-11-27 15:41:21 -07:00
|
|
|
cosmic_config::config_state_subscription(
|
|
|
|
|
TypeId::of::<BackgroundSubscription>(),
|
|
|
|
|
cosmic_bg_config::NAME.into(),
|
|
|
|
|
cosmic_bg_config::state::State::version(),
|
|
|
|
|
)
|
2024-02-23 19:25:35 -05:00
|
|
|
.map(|res| {
|
|
|
|
|
if !res.errors.is_empty() {
|
|
|
|
|
log::info!("errors loading background state: {:?}", res.errors);
|
2023-11-27 15:41:21 -07:00
|
|
|
}
|
2024-02-23 19:25:35 -05:00
|
|
|
Message::BackgroundState(res.config)
|
2023-11-27 15:41:21 -07:00
|
|
|
}),
|
2024-04-05 14:08:40 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if matches!(self.state, State::Locked) {
|
|
|
|
|
struct HeartbeatSubscription;
|
2025-02-21 17:05:50 -05:00
|
|
|
subscriptions.push(Subscription::run_with_id(
|
2023-11-27 15:41:21 -07:00
|
|
|
TypeId::of::<HeartbeatSubscription>(),
|
2025-02-21 17:05:50 -05:00
|
|
|
cosmic::iced_futures::stream::channel(16, |mut msg_tx| async move {
|
2023-11-21 15:48:22 -07:00
|
|
|
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;
|
|
|
|
|
}
|
2025-02-21 17:05:50 -05:00
|
|
|
}),
|
2024-04-05 14:08:40 -06:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
struct PamSubscription;
|
|
|
|
|
//TODO: how to avoid cloning this on every time subscription is called?
|
|
|
|
|
let username = self.flags.current_user.name.clone();
|
2025-02-21 17:05:50 -05:00
|
|
|
subscriptions.push(Subscription::run_with_id(
|
2023-11-27 15:41:21 -07:00
|
|
|
TypeId::of::<PamSubscription>(),
|
2025-02-21 17:05:50 -05:00
|
|
|
cosmic::iced_futures::stream::channel(16, |mut msg_tx| async move {
|
2023-10-06 10:44:05 -06:00
|
|
|
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");
|
2024-04-05 11:31:15 -06:00
|
|
|
msg_tx.send(Message::Unlock).await.unwrap();
|
2023-10-06 10:44:05 -06:00
|
|
|
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
|
|
|
}
|
2025-02-21 17:05:50 -05:00
|
|
|
}),
|
2024-04-05 14:08:40 -06:00
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "logind")]
|
|
|
|
|
{
|
2024-04-05 19:02:20 -06:00
|
|
|
subscriptions.push(crate::logind::subscription());
|
2024-04-05 14:08:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "networkmanager")]
|
|
|
|
|
{
|
|
|
|
|
subscriptions.push(
|
|
|
|
|
crate::networkmanager::subscription()
|
|
|
|
|
.map(|icon_opt| Message::NetworkIcon(icon_opt)),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "upower")]
|
|
|
|
|
{
|
|
|
|
|
subscriptions
|
|
|
|
|
.push(crate::upower::subscription().map(|info_opt| Message::PowerInfo(info_opt)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Subscription::batch(subscriptions)
|
2023-10-05 17:47:23 -06:00
|
|
|
}
|
|
|
|
|
}
|