Skip to content

Commit

Permalink
feat: add helpers for getting and using activation tokens in applets
Browse files Browse the repository at this point in the history
refactor(applet): connect to privileged socket if available

cleanup
  • Loading branch information
wash2 committed Nov 18, 2023
1 parent c9554a8 commit ef5b6fb
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 8 deletions.
8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ animated-image = ["image", "dep:async-fs", "tokio?/io-util", "tokio?/fs"]
debug = ["iced/debug"]
# Enables pipewire support in ashpd, if ashpd is enabled
pipewire = ["ashpd?/pipewire"]
# Enables process spawning helper
process = ["nix"]
# Enables keycode serialization
serde-keycode = ["iced_core/serde"]
# smol async runtime
Expand All @@ -27,7 +29,7 @@ wayland = [
"iced_runtime/wayland",
"iced/wayland",
"iced_sctk",
"sctk",
"cctk",
]
# Render with wgpu
wgpu = ["iced/wgpu", "iced_wgpu"]
Expand All @@ -40,6 +42,7 @@ winit_wgpu = ["winit", "wgpu"]
xdg-portal = ["ashpd"]
# XXX Use "a11y"; which is causing a panic currently
applet = ["wayland", "tokio", "cosmic-panel-config", "ron"]
applet-token = []
zbus = ["dep:zbus", "serde", "ron"]

[dependencies]
Expand All @@ -48,7 +51,7 @@ derive_setters = "0.1.5"
lazy_static = "1.4.0"
palette = "0.7.3"
tokio = { version = "1.24.2", optional = true }
sctk = { package = "smithay-client-toolkit", git = "https://github.com/smithay/client-toolkit", optional = true, rev = "dc8c4a0" }
cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-client-toolkit", rev = "5faec87", optional = true }
slotmap = "1.0.6"
fraction = "0.13.0"
cosmic-config = { path = "cosmic-config" }
Expand All @@ -60,6 +63,7 @@ ashpd = { version = "0.5.0", default-features = false, optional = true }
url = "2.4.0"
unicode-segmentation = "1.6"
css-color = "0.2.5"
nix = { version = "0.26", optional = true }
zbus = {version = "3.14.1", default-features = false, optional = true}
serde = { version = "1.0.180", optional = true }

Expand Down
4 changes: 2 additions & 2 deletions src/app/cosmic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use super::{command, Application, ApplicationExt, Core, Subscription};
use crate::theme::{self, Theme, ThemeType, THEME};
use crate::widget::nav_bar;
use crate::{keyboard_nav, Element};
#[cfg(feature = "wayland")]
use cctk::sctk::reexports::csd_frame::{WindowManagerCapabilities, WindowState};
use cosmic_theme::ThemeMode;
#[cfg(feature = "wayland")]
use iced::event::wayland::{self, WindowEvent};
Expand All @@ -15,8 +17,6 @@ use iced::window;
use iced_runtime::command::Action;
#[cfg(not(feature = "wayland"))]
use iced_runtime::window::Action as WindowAction;
#[cfg(feature = "wayland")]
use sctk::reexports::csd_frame::{WindowManagerCapabilities, WindowState};

/// A message managed internally by COSMIC.
#[derive(Clone, Debug)]
Expand Down
6 changes: 5 additions & 1 deletion src/applet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#[cfg(feature = "applet-token")]
pub mod token;

use crate::{
app::Core,
cctk::sctk,
iced::{
self,
alignment::{Horizontal, Vertical},
widget::Container,
window, Color, Length, Limits, Rectangle,
},
iced_style, iced_widget, sctk,
iced_style, iced_widget,
theme::{self, Button, THEME},
widget, Application, Element, Renderer,
};
Expand Down
2 changes: 2 additions & 0 deletions src/applet/token/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod subscription;
pub mod wayland_handler;
78 changes: 78 additions & 0 deletions src/applet/token/subscription.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::iced;
use crate::iced::subscription;
use crate::iced_futures::futures;
use cctk::sctk::reexports::calloop;
use futures::{
channel::mpsc::{unbounded, UnboundedReceiver},
SinkExt, StreamExt,
};
use std::{fmt::Debug, hash::Hash, thread::JoinHandle};

use super::wayland_handler::wayland_handler;

pub fn activation_token_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
id: I,
) -> iced::Subscription<TokenUpdate> {
subscription::channel(id, 50, move |mut output| async move {
let mut state = State::Ready;

loop {
state = start_listening(state, &mut output).await;
}
})
}

pub enum State {
Ready,
Waiting(
UnboundedReceiver<TokenUpdate>,
calloop::channel::Sender<TokenRequest>,
JoinHandle<()>,
),
Finished,
}

async fn start_listening(
state: State,
output: &mut futures::channel::mpsc::Sender<TokenUpdate>,
) -> State {
match state {
State::Ready => {
let (calloop_tx, calloop_rx) = calloop::channel::channel();
let (toplevel_tx, toplevel_rx) = unbounded();
let handle = std::thread::spawn(move || {
wayland_handler(toplevel_tx, calloop_rx);
});
let tx = calloop_tx.clone();
_ = output.send(TokenUpdate::Init(tx)).await;
State::Waiting(toplevel_rx, calloop_tx, handle)
}
State::Waiting(mut rx, tx, handle) => {
if handle.is_finished() {
_ = output.send(TokenUpdate::Finished).await;
return State::Finished;
}
if let Some(u) = rx.next().await {
_ = output.send(u).await;
State::Waiting(rx, tx, handle)
} else {
_ = output.send(TokenUpdate::Finished).await;
State::Finished
}
}
State::Finished => iced::futures::future::pending().await,
}
}

#[derive(Clone, Debug)]
pub enum TokenUpdate {
Init(calloop::channel::Sender<TokenRequest>),
Finished,
ActivationToken { token: Option<String>, exec: String },
}

#[derive(Clone, Debug)]
pub struct TokenRequest {
pub app_id: String,
pub exec: String,
}
180 changes: 180 additions & 0 deletions src/applet/token/wayland_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
use std::os::{
fd::{FromRawFd, RawFd},
unix::net::UnixStream,
};

use super::subscription::{TokenRequest, TokenUpdate};
use cctk::{
sctk::{
self,
activation::{RequestData, RequestDataExt},
reexports::{calloop, calloop_wayland_source::WaylandSource},
seat::{SeatHandler, SeatState},
},
wayland_client::{
self,
protocol::{wl_seat::WlSeat, wl_surface::WlSurface},
},
};
use iced_futures::futures::channel::mpsc::UnboundedSender;
use sctk::{
activation::{ActivationHandler, ActivationState},
registry::{ProvidesRegistryState, RegistryState},
};
use wayland_client::{globals::registry_queue_init, Connection, QueueHandle};

struct AppData {
exit: bool,
queue_handle: QueueHandle<Self>,
registry_state: RegistryState,
activation_state: Option<ActivationState>,
tx: UnboundedSender<TokenUpdate>,
seat_state: SeatState,
}

impl ProvidesRegistryState for AppData {
fn registry(&mut self) -> &mut RegistryState {
&mut self.registry_state
}

sctk::registry_handlers!();
}

struct ExecRequestData {
data: RequestData,
exec: String,
}

impl RequestDataExt for ExecRequestData {
fn app_id(&self) -> Option<&str> {
self.data.app_id()
}

fn seat_and_serial(&self) -> Option<(&WlSeat, u32)> {
self.data.seat_and_serial()
}

fn surface(&self) -> Option<&WlSurface> {
self.data.surface()
}
}

impl ActivationHandler for AppData {
type RequestData = ExecRequestData;
fn new_token(&mut self, token: String, data: &ExecRequestData) {
let _ = self.tx.unbounded_send(TokenUpdate::ActivationToken {
token: Some(token),
exec: data.exec.clone(),
});
}
}

impl SeatHandler for AppData {
fn seat_state(&mut self) -> &mut sctk::seat::SeatState {
&mut self.seat_state
}

fn new_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: WlSeat) {}

fn new_capability(
&mut self,
_: &Connection,
_: &QueueHandle<Self>,
_: WlSeat,
_: sctk::seat::Capability,
) {
}

fn remove_capability(
&mut self,
_: &Connection,
_: &QueueHandle<Self>,
_: WlSeat,
_: sctk::seat::Capability,
) {
}

fn remove_seat(&mut self, _: &Connection, _: &QueueHandle<Self>, _: WlSeat) {}
}

pub(crate) fn wayland_handler(
tx: UnboundedSender<TokenUpdate>,
rx: calloop::channel::Channel<TokenRequest>,
) {
let socket = std::env::var("X_PRIVILEGED_WAYLAND_SOCKET")
.ok()
.and_then(|fd| {
fd.parse::<RawFd>()
.ok()
.map(|fd| unsafe { UnixStream::from_raw_fd(fd) })
});

let conn = if let Some(socket) = socket {
Connection::from_socket(socket).unwrap()
} else {
Connection::connect_to_env().unwrap()
};
let (globals, event_queue) = registry_queue_init(&conn).unwrap();

let mut event_loop = calloop::EventLoop::<AppData>::try_new().unwrap();
let qh = event_queue.handle();
let wayland_source = WaylandSource::new(conn, event_queue);
let handle = event_loop.handle();
wayland_source
.insert(handle.clone())
.expect("Failed to insert wayland source.");

if handle
.insert_source(rx, |event, _, state| match event {
calloop::channel::Event::Msg(TokenRequest { app_id, exec }) => {
if let Some(activation_state) = state.activation_state.as_ref() {
activation_state.request_token_with_data(
&state.queue_handle,
ExecRequestData {
data: RequestData {
app_id: Some(app_id),
seat_and_serial: state
.seat_state
.seats()
.next()
.map(|seat| (seat, 0)),
surface: None,
},
exec,
},
);
} else {
let _ = state
.tx
.unbounded_send(TokenUpdate::ActivationToken { token: None, exec });
}
}
calloop::channel::Event::Closed => {
state.exit = true;
}
})
.is_err()
{
return;
}
let registry_state = RegistryState::new(&globals);
let mut app_data = AppData {
exit: false,
tx,
seat_state: SeatState::new(&globals, &qh),
queue_handle: qh.clone(),
activation_state: ActivationState::bind::<AppData>(&globals, &qh).ok(),
registry_state,
};

loop {
if app_data.exit {
break;
}
event_loop.dispatch(None, &mut app_data).unwrap();
}
}

sctk::delegate_activation!(AppData, ExecRequestData);
sctk::delegate_seat!(AppData);
sctk::delegate_registry!(AppData);
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ pub use iced_winit;
pub mod icon_theme;
pub mod keyboard_nav;

#[cfg(feature = "process")]
pub mod process;

#[cfg(feature = "wayland")]
pub use sctk;
pub use cctk;

pub mod theme;
pub use theme::{style, Theme};
Expand Down
31 changes: 31 additions & 0 deletions src/process.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::process::{exit, Command, Stdio};

use nix::sys::wait::waitpid;
use nix::unistd::{fork, ForkResult};

/// Performs a double fork with setsid to spawn and detach a command.
pub fn spawn(mut command: Command) {
command
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());

unsafe {
match fork() {
Ok(ForkResult::Parent { child }) => {
let _res = waitpid(Some(child), None);
}

Ok(ForkResult::Child) => {
let _res = nix::unistd::setsid();
let _res = command.spawn();

exit(0);
}

Err(why) => {
println!("failed to fork and spawn command: {}", why.desc());
}
}
}
}
4 changes: 2 additions & 2 deletions src/widget/text_input/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ use iced_runtime::command::platform_specific;
use iced_runtime::Command;

#[cfg(feature = "wayland")]
use iced_runtime::command::platform_specific::wayland::data_device::{DataFromMimeType, DndIcon};
use cctk::sctk::reexports::client::protocol::wl_data_device_manager::DndAction;
#[cfg(feature = "wayland")]
use sctk::reexports::client::protocol::wl_data_device_manager::DndAction;
use iced_runtime::command::platform_specific::wayland::data_device::{DataFromMimeType, DndIcon};

/// Creates a new [`TextInput`].
///
Expand Down

0 comments on commit ef5b6fb

Please sign in to comment.