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
impl ModuleCtx
Sourcepub async fn connect() -> Result<Self, Error>
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.
Sourcepub async fn over(stream: UnixStream) -> Result<Self, Error>
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, .. }));Sourcepub fn settings<T: DeserializeOwned>(&self) -> Result<T, Error>
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)].
Sourcepub fn data_dir(&self) -> &Path
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.
Sourcepub fn session_dir(&self) -> &Path
pub fn session_dir(&self) -> &Path
Like data_dir, but kept across mochid restarts.
Created on first use.
Sourcepub fn publish_state(&self, state: Value)
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.
Sourcepub fn present(&self, spec: ActivitySpec) -> ActivityId
pub fn present(&self, spec: ActivitySpec) -> ActivityId
Submits an activity to the island. Whether and when it shows is up to the arbiter.
Sourcepub fn update(&self, id: ActivityId, payload: Value)
pub fn update(&self, id: ActivityId, payload: Value)
Replaces an activity’s payload. Its view updates in place.
Sourcepub fn withdraw(&self, id: ActivityId)
pub fn withdraw(&self, id: ActivityId)
Removes an activity, shown or waiting. It ends with
EndReason::Withdrawn.
Sourcepub fn show_bubble(&self, spec: BubbleSpec) -> BubbleId
pub fn show_bubble(&self, spec: BubbleSpec) -> BubbleId
Shows a bubble, or replaces the plugin’s bubble with the same key.
Sourcepub fn update_bubble(&self, id: BubbleId, payload: Value)
pub fn update_bubble(&self, id: BubbleId, payload: Value)
Replaces a bubble’s payload, keeping its place. Not news: see
BubbleSpec::news.
Sourcepub fn hide_bubble(&self, id: BubbleId)
pub fn hide_bubble(&self, id: BubbleId)
Removes a bubble.
Sourcepub fn call(
&self,
module: &str,
action: &str,
args: &[&str],
) -> impl Future<Output = Result<(), CallError>> + Send + 'static
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.
Sourcepub fn ask(
&self,
module: &str,
action: &str,
args: &[&str],
) -> impl Future<Output = Result<Option<String>, CallError>> + Send + 'static
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.
Sourcepub fn compositor(&self) -> CompositorState
pub fn compositor(&self) -> CompositorState
The compositor’s state as it is now.
Sourcepub fn compositor_changes(&self) -> Receiver<CompositorState>
pub fn compositor_changes(&self) -> Receiver<CompositorState>
Wakes on every new compositor state.
Sourcepub fn activate_workspace(
&self,
workspace: u32,
) -> impl Future<Output = Result<(), String>> + Send + 'static
pub fn activate_workspace( &self, workspace: u32, ) -> impl Future<Output = Result<(), String>> + Send + 'static
Switches to a workspace, by its id from CompositorState.
Sourcepub fn windows(
&self,
) -> impl Future<Output = Result<Vec<WindowInfo>, String>> + Send + 'static
pub fn windows( &self, ) -> impl Future<Output = Result<Vec<WindowInfo>, String>> + Send + 'static
The windows on visible workspaces, where the compositor says.
Sourcepub fn pointer_output(
&self,
) -> impl Future<Output = Result<Option<String>, String>> + Send + 'static
pub fn pointer_output( &self, ) -> impl Future<Output = Result<Option<String>, String>> + Send + 'static
The output the pointer is on, when the compositor says.
Sourcepub async fn next_event(&mut self) -> Option<ModuleEvent>
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.