Skip to main content

ModuleCtx

Struct ModuleCtx 

Source
pub struct ModuleCtx { /* private fields */ }
Expand description

The plugin’s connection to mochid. Every call is one message, and none waits for mochid unless it returns a future.

Implementations§

Source§

impl ModuleCtx

Source

pub async fn connect() -> Result<Self, Error>

Connects through the socket mochid passes in MOCHI_PLUGIN_FD and says hello. Fails when the program wasn’t started by mochid.

Source

pub async fn over(stream: UnixStream) -> Result<Self, Error>

Talks to mochid over stream: for tests, which play mochid’s part on the other end. mochid speaks first, with hello.

use mochi_sdk::protocol::plugin::{CompositorState, FromPlugin, ToPlugin};
use mochi_sdk::{ActivitySpec, ModuleCtx, json};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixStream;

let (mochid, plugin) = UnixStream::pair().unwrap();
let (reader, mut writer) = mochid.into_split();
let hello = ToPlugin::Hello {
    api: mochi_sdk::API,
    version: "test".into(),
    module: "timer".into(),
    settings: json!({ "minutes": 5 }),
    data_dir: "/tmp/timer".into(),
    session_dir: "/tmp/timer".into(),
    compositor: CompositorState::default(),
};
writer.write_all(&mochi_sdk::protocol::encode(&hello).unwrap()).await.unwrap();

let ctx = ModuleCtx::over(plugin).await.unwrap();
ctx.present(ActivitySpec::new("Timer"));

// What the plugin sent: its hello, then the activity.
let mut lines = BufReader::new(reader).lines();
let mut next = async || {
    let line = lines.next_line().await.unwrap().unwrap();
    mochi_sdk::protocol::decode::<FromPlugin>(&line).unwrap()
};
assert_eq!(next().await, FromPlugin::Hello { api: mochi_sdk::API });
assert!(matches!(next().await, FromPlugin::Present { id: 1, .. }));
Source

pub fn module(&self) -> &str

The plugin’s id.

Source

pub fn version(&self) -> &str

mochid’s version.

Source

pub fn settings<T: DeserializeOwned>(&self) -> Result<T, Error>

The plugin’s [module.<id>] table from config.toml. Fields missing there take their Default values when T uses #[serde(default)].

Source

pub fn data_dir(&self) -> &Path

A directory only this plugin writes to, emptied when mochid starts, in the runtime directory: for files its views load.

Source

pub fn session_dir(&self) -> &Path

Like data_dir, but kept across mochid restarts. Created on first use.

Source

pub fn publish_state(&self, state: Value)

Replaces the plugin’s state, which its views read as Daemon.state("<id>") and other modules can watch.

Source

pub fn present(&self, spec: ActivitySpec) -> ActivityId

Submits an activity to the island. Whether and when it shows is up to the arbiter.

Source

pub fn update(&self, id: ActivityId, payload: Value)

Replaces an activity’s payload. Its view updates in place.

Source

pub fn withdraw(&self, id: ActivityId)

Removes an activity, shown or waiting. It ends with EndReason::Withdrawn.

Source

pub fn show_bubble(&self, spec: BubbleSpec) -> BubbleId

Shows a bubble, or replaces the plugin’s bubble with the same key.

Source

pub fn update_bubble(&self, id: BubbleId, payload: Value)

Replaces a bubble’s payload, keeping its place. Not news: see BubbleSpec::news.

Source

pub fn hide_bubble(&self, id: BubbleId)

Removes a bubble.

Source

pub fn call( &self, module: &str, action: &str, args: &[&str], ) -> impl Future<Output = Result<(), CallError>> + Send + 'static

Runs another module’s action, with the same checks as mochi ipc. CallError::NotEnabled means that module isn’t enabled.

Source

pub fn ask( &self, module: &str, action: &str, args: &[&str], ) -> impl Future<Output = Result<Option<String>, CallError>> + Send + 'static

Like ModuleCtx::call, and returns what the action answered with, like the output mochi ipc prints.

Source

pub fn compositor(&self) -> CompositorState

The compositor’s state as it is now.

Source

pub fn compositor_changes(&self) -> Receiver<CompositorState>

Wakes on every new compositor state.

Source

pub fn activate_workspace( &self, workspace: u32, ) -> impl Future<Output = Result<(), String>> + Send + 'static

Switches to a workspace, by its id from CompositorState.

Source

pub fn windows( &self, ) -> impl Future<Output = Result<Vec<WindowInfo>, String>> + Send + 'static

The windows on visible workspaces, where the compositor says.

Source

pub fn pointer_output( &self, ) -> impl Future<Output = Result<Option<String>, String>> + Send + 'static

The output the pointer is on, when the compositor says.

Source

pub async fn next_event(&mut self) -> Option<ModuleEvent>

The next command or event. None once mochid closes the connection: the plugin should finish then.

Trait Implementations§

Source§

impl Debug for ModuleCtx

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.