Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ jobs:
sudo mkdir -p /run/user/$UID |
sed -e s/UID/$UID/ -e s/PATH/path/ CI/dbus-session.conf > /tmp/dbus-session.conf
sed -e s/UID/$UID/ -e s/PATH/abstract/ CI/dbus-session.conf > /tmp/dbus-session-abstract.conf
sudo apt-get install -y dbus
sudo apt-get update
sudo apt-get install -y dbus ibus
- uses: dtolnay/rust-toolchain@master
with:
toolchain: stable
Expand All @@ -101,6 +102,9 @@ jobs:
- name: Build and Test
run: |
dbus-run-session --config-file /tmp/dbus-session-abstract.conf -- cargo --locked test --release --verbose -- basic_connection
# Start IBus daemon for IBus test.
ibus-daemon -d --xim
sleep 2
# All features except tokio.
dbus-run-session --config-file /tmp/dbus-session.conf -- \
cargo --locked test --release --verbose --features uuid,url,time,chrono,option-as-array,vsock,bus-impl \
Expand Down
5 changes: 3 additions & 2 deletions zbus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ uds_windows.workspace = true
[target.'cfg(unix)'.dependencies]
rustix.workspace = true
libc.workspace = true

[target.'cfg(any(target_os = "macos", windows))'.dependencies]
async-recursion.workspace = true

[target.'cfg(windows)'.dependencies.async-recursion]
workspace = true

[dev-dependencies]
zbus_xml.workspace = true

Expand Down
6 changes: 3 additions & 3 deletions zbus/src/abstractions/process.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[cfg(not(feature = "tokio"))]
use async_process::{Child, unix::CommandExt};
#[cfg(target_os = "macos")]
#[cfg(unix)]
use std::process::Output;
use std::{ffi::OsStr, io::Error, process::Stdio};
#[cfg(feature = "tokio")]
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Command {

/// Executes the command as a child process, waiting for it to finish and
/// collecting all of its output.
#[cfg(target_os = "macos")]
#[cfg(unix)]
pub async fn output(&mut self) -> Result<Output, Error> {
self.0.output().await
}
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Command {
}

/// An asynchronous wrapper around running and getting command output
#[cfg(target_os = "macos")]
#[cfg(unix)]
pub async fn run<I, S>(program: S, args: I) -> Result<Output, Error>
where
I: IntoIterator<Item = S>,
Expand Down
10 changes: 10 additions & 0 deletions zbus/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,11 @@ mod tests {
Address::from_str("launchd:env=my_cool_env_key").unwrap(),
Transport::Launchd(Launchd::new("my_cool_env_key")).into(),
);
#[cfg(unix)]
assert_eq!(
Address::from_str("ibus:").unwrap(),
Transport::Ibus(crate::address::transport::Ibus::new()).into(),
);

#[cfg(all(feature = "vsock", feature = "p2p", not(feature = "tokio")))]
{
Expand Down Expand Up @@ -391,6 +396,11 @@ mod tests {
Address::from(Transport::Launchd(Launchd::new("my_cool_key"))).to_string(),
"launchd:env=my_cool_key"
);
#[cfg(unix)]
assert_eq!(
Address::from(Transport::Ibus(crate::address::transport::Ibus::new())).to_string(),
"ibus:"
);

#[cfg(all(feature = "vsock", feature = "p2p", not(feature = "tokio")))]
{
Expand Down
136 changes: 136 additions & 0 deletions zbus/src/address/transport/ibus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use crate::{Address, Result, process::run};

#[derive(Clone, Debug, PartialEq, Eq)]
/// The transport properties of an IBus D-Bus address.
///
/// This transport type queries the IBus daemon for its D-Bus address using the `ibus address`
/// command. IBus (Intelligent Input Bus) is an input method framework used primarily on Linux
/// systems for entering text in various languages.
///
/// # Platform Support
///
/// This transport is available on Unix-like systems where IBus is installed.
///
/// # Example
///
/// ```no_run
/// # use zbus::address::transport::{Transport, Ibus};
/// # use zbus::Address;
/// #
/// // Create an IBus transport
/// let ibus = Ibus::new();
/// let _addr = Transport::Ibus(ibus);
///
/// // Or use it directly as an address
/// let _addr = Address::from(Transport::Ibus(Ibus::new()));
/// ```
pub struct Ibus;

impl Ibus {
/// Create a new IBus transport.
///
/// This will query the IBus daemon for its D-Bus address when the connection is established.
#[must_use]
pub fn new() -> Self {
Self
}

/// Determine the actual transport details behind an IBus address.
///
/// This method executes the `ibus address` command to retrieve the D-Bus address from the
/// running IBus daemon, then parses and returns the underlying transport.
///
/// # Errors
///
/// Returns an error if:
/// - The `ibus` command is not found or fails to execute
/// - The IBus daemon is not running
/// - The command output cannot be parsed as a valid D-Bus address
/// - The command output is not valid UTF-8
///
/// # Example
///
/// ```no_run
/// # use zbus::connection::Builder;
/// # use zbus::block_on;
/// #
/// # block_on(async {
/// // This method is used internally by the connection builder
/// let _conn = Builder::ibus()?.build().await?;
/// # Ok::<(), zbus::Error>(())
/// # }).unwrap();
/// ```
pub(super) async fn bus_address(&self) -> Result<Address> {
let output = run("ibus", ["address"])
.await
.map_err(|e| crate::Error::Address(format!("Failed to execute ibus command: {e}")))?;

if !output.status.success() {
return Err(crate::Error::Address(format!(
"ibus terminated with code: {}",
output.status
)));
}

let addr = String::from_utf8(output.stdout).map_err(|e| {
crate::Error::Address(format!("Unable to parse ibus output as UTF-8: {e}"))
})?;

addr.trim().parse()
}

/// Parse IBus transport from D-Bus address options.
///
/// The IBus transport type does not require any options, so this method will succeed
/// as long as the transport type is specified as "ibus".
///
/// # Errors
///
/// This method does not return errors for the IBus transport, but the signature is kept
/// consistent with other transport types.
pub(super) fn from_options(_opts: std::collections::HashMap<&str, &str>) -> Result<Self> {
Ok(Self)
}
}

impl Default for Ibus {
fn default() -> Self {
Self::new()
}
}

impl std::fmt::Display for Ibus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ibus:")
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ibus_new() {
let ibus = Ibus::new();
assert_eq!(ibus.to_string(), "ibus:");
}

#[test]
fn test_ibus_default() {
let ibus = Ibus::default();
assert_eq!(ibus.to_string(), "ibus:");
}

#[test]
fn test_ibus_from_options() {
let options = std::collections::HashMap::new();
let ibus = Ibus::from_options(options).unwrap();
assert_eq!(ibus, Ibus::new());
}

#[test]
fn test_ibus_display() {
let ibus = Ibus::new();
assert_eq!(format!("{}", ibus), "ibus:");
}
}
22 changes: 21 additions & 1 deletion zbus/src/address/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub use autolaunch::{Autolaunch, AutolaunchScope};
mod launchd;
#[cfg(target_os = "macos")]
pub use launchd::Launchd;
#[cfg(unix)]
mod ibus;
#[cfg(unix)]
pub use ibus::Ibus;
#[cfg(any(
all(feature = "vsock", not(feature = "tokio")),
feature = "tokio-vsock"
Expand Down Expand Up @@ -73,6 +77,12 @@ pub enum Transport {
/// A launchd D-Bus address.
#[cfg(target_os = "macos")]
Launchd(Launchd),
/// An IBus D-Bus address.
///
/// IBus (Intelligent Input Bus) is an input method framework. This transport queries the
/// IBus daemon for its D-Bus address using the `ibus address` command.
#[cfg(unix)]
Ibus(Ibus),
#[cfg(any(
all(feature = "vsock", not(feature = "tokio")),
feature = "tokio-vsock"
Expand All @@ -89,7 +99,7 @@ pub enum Transport {
}

impl Transport {
#[cfg_attr(any(target_os = "macos", windows), async_recursion::async_recursion)]
#[cfg_attr(any(unix, windows), async_recursion::async_recursion)]
pub(super) async fn connect(self) -> Result<Stream> {
match self {
Transport::Unix(unix) => {
Expand Down Expand Up @@ -217,6 +227,12 @@ impl Transport {
let addr = launchd.bus_address().await?;
addr.connect().await
}

#[cfg(unix)]
Transport::Ibus(ibus) => {
let addr = ibus.bus_address().await?;
addr.connect().await
}
}
}

Expand All @@ -237,6 +253,8 @@ impl Transport {
"autolaunch" => Autolaunch::from_options(options).map(Self::Autolaunch),
#[cfg(target_os = "macos")]
"launchd" => Launchd::from_options(options).map(Self::Launchd),
#[cfg(unix)]
"ibus" => Ibus::from_options(options).map(Self::Ibus),

_ => Err(Error::Address(format!(
"unsupported transport '{transport}'"
Expand Down Expand Up @@ -364,6 +382,8 @@ impl Display for Transport {
Self::Autolaunch(autolaunch) => write!(f, "{autolaunch}")?,
#[cfg(target_os = "macos")]
Self::Launchd(launchd) => write!(f, "{launchd}")?,
#[cfg(unix)]
Self::Ibus(ibus) => write!(f, "{ibus}")?,
}

Ok(())
Expand Down
33 changes: 33 additions & 0 deletions zbus/src/blocking/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,39 @@ impl<'a> Builder<'a> {
crate::connection::Builder::system().map(Self)
}

/// Create a builder for an IBus connection.
///
/// IBus (Intelligent Input Bus) is an input method framework. This method creates a builder
/// that will query the IBus daemon for its D-Bus address using the `ibus address` command.
///
/// # Platform Support
///
/// This method is available on Unix-like systems where IBus is installed.
///
/// # Errors
///
/// Returns an error if:
/// - The `ibus` command is not found or fails to execute
/// - The IBus daemon is not running
/// - The command output cannot be parsed as a valid D-Bus address
///
/// # Example
///
/// ```no_run
/// # use std::error::Error;
/// # use zbus::blocking::connection;
/// #
/// let _conn = connection::Builder::ibus()?
/// .build()?;
///
/// // Use the connection to interact with IBus services.
/// # Ok::<_, Box<dyn Error + Send + Sync>>(())
/// ```
#[cfg(unix)]
pub fn ibus() -> Result<Self> {
crate::connection::Builder::ibus().map(Self)
}

/// Create a builder for a connection that will use the given [D-Bus bus address].
///
/// [D-Bus bus address]: https://dbus.freedesktop.org/doc/dbus-specification.html#addresses
Expand Down
46 changes: 45 additions & 1 deletion zbus/src/connection/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,49 @@ impl<'a> Builder<'a> {
Ok(Self::new(Target::Address(Address::system()?)))
}

/// Create a builder for an IBus connection.
///
/// IBus (Intelligent Input Bus) is an input method framework. This method creates a builder
/// that will query the IBus daemon for its D-Bus address using the `ibus address` command.
///
/// # Platform Support
///
/// This method is available on Unix-like systems where IBus is installed.
///
/// # Errors
///
/// Returns an error if:
/// - The `ibus` command is not found or fails to execute
/// - The IBus daemon is not running
/// - The command output cannot be parsed as a valid D-Bus address
///
/// # Example
///
/// ```no_run
/// # use std::error::Error;
/// # use zbus::connection::Builder;
/// # use zbus::block_on;
/// #
/// # block_on(async {
/// let conn = Builder::ibus()?
/// .build()
/// .await?;
///
/// // Use the connection to interact with IBus services
/// # drop(conn);
/// # Ok::<(), zbus::Error>(())
/// # }).unwrap();
/// #
/// # Ok::<_, Box<dyn Error + Send + Sync>>(())
/// ```
#[cfg(unix)]
pub fn ibus() -> Result<Self> {
use crate::address::transport::{Ibus, Transport};
Ok(Self::new(Target::Address(Address::from(Transport::Ibus(
Ibus::new(),
)))))
}

/// Create a builder for a connection that will use the given [D-Bus bus address].
///
/// # Example
Expand Down Expand Up @@ -126,7 +169,8 @@ impl<'a> Builder<'a> {
/// ```
///
/// **Note:** The IBus address is different for each session. You can find the address for your
/// current session using `ibus address` command.
/// current session using `ibus address` command. For a more convenient way to connect to IBus,
/// see [`Builder::ibus`].
///
/// [D-Bus bus address]: https://dbus.freedesktop.org/doc/dbus-specification.html#addresses
pub fn address<A>(address: A) -> Result<Self>
Expand Down
Loading
Loading