refactor(protocol): use SocketMessage from komorebi-protocol in the KDE daemon

Delete KDE's SocketMessage, as_bytes and FromStr and its now unused imports, and re-export the crate's SocketMessage from core. process_command stays an exhaustive match: the 34 variants KDE does not handle (33 Windows only, plus PanGesture which only Windows and macOS have) get one explicit or-pattern arm that writes the 'not supported on this platform' reply and returns before anything else happens (no notification to subscribers). Behaviour change: those variants used to fail to parse on KDE and now parse and are answered that way. The doctest of SocketMessage lives in the crate now.

Verified: cargo check --workspace --all-targets in komorebi-for-kde: 0 errors, no warnings; cargo test -p komorebi --lib --test schemas --test rule_commands: 302 + 5 + 6 passed, 0 failed; komorebic static-config-schema byte-identical to schema.json. Full workspace run at the end of the stage.
This commit is contained in:
LGUG2Z committed 2026-10-31 01:37:53 -07:00
1 parent 68c3133d4e
commit 2adcced406
2 files changed
+40 -251

No files matched your search

+1 -251
View File
@@ -12,17 +12,9 @@
//! [`OperationDirection`] is a side of the focused tile, and a [`CycleDirection`] the next or the
//! previous container in the order of the workspace.
use crate::animation::AnimationPrefix;
use crate::core::animation::AnimationStyle;
use crate::core::pathext::ResolvedPathBuf;
use clap::ValueEnum;
use color_eyre::eyre;
use komorebi_themes::KomorebiTheme;
use serde::Deserialize;
use serde::Serialize;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::str::FromStr;
use strum::Display;
use strum::EnumString;
@@ -389,249 +381,7 @@ pub enum CrossBoundaryBehaviour {
Monitor,
}
/// A message to the daemon, as it is written on its socket: JSON, with the variant's name in
/// `type` and its arguments, if it has any, in `content`.
///
/// # Example
///
/// ```rust
/// use komorebi::core::SocketMessage;
/// use komorebi::core::StateQuery;
///
/// let query = SocketMessage::Query(StateQuery::FocusedWorkspaceIndex);
/// assert_eq!(
/// String::from_utf8(query.as_bytes()?)?,
/// r#"{"type":"Query","content":"FocusedWorkspaceIndex"}"#,
/// );
///
/// let stop: SocketMessage = r#"{"type":"Stop"}"#.parse()?;
/// assert!(matches!(stop, SocketMessage::Stop));
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
// serde_as must be before derive
#[serde_with::serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, Display)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(tag = "type", content = "content")]
pub enum SocketMessage {
// Window / Container Commands
FocusWindow(OperationDirection),
MoveWindow(OperationDirection),
PreselectDirection(OperationDirection),
CancelPreselect,
CycleFocusWindow(CycleDirection),
CycleMoveWindow(CycleDirection),
StackWindow(OperationDirection),
UnstackWindow,
CycleStack(CycleDirection),
CycleStackIndex(CycleDirection),
FocusStackWindow(usize),
StackAll,
UnstackAll,
ResizeWindowEdge(OperationDirection, Sizing),
ResizeWindowAxis(Axis, Sizing),
MoveContainerToLastWorkspace,
SendContainerToLastWorkspace,
MoveContainerToMonitorNumber(usize),
CycleMoveContainerToMonitor(CycleDirection),
MoveContainerToWorkspaceNumber(usize),
MoveContainerToNamedWorkspace(String),
CycleMoveContainerToWorkspace(CycleDirection),
SendContainerToMonitorNumber(usize),
CycleSendContainerToMonitor(CycleDirection),
SendContainerToWorkspaceNumber(usize),
CycleSendContainerToWorkspace(CycleDirection),
SendContainerToMonitorWorkspaceNumber(usize, usize),
MoveContainerToMonitorWorkspaceNumber(usize, usize),
SendContainerToNamedWorkspace(String),
CycleMoveWorkspaceToMonitor(CycleDirection),
MoveWorkspaceToMonitorNumber(usize),
SwapWorkspacesToMonitorNumber(usize),
// ForceFocus,
Close,
Minimize,
Promote,
PromoteSwap,
PromoteFocus,
PromoteWindow(OperationDirection),
EagerFocus(String),
LockMonitorWorkspaceContainer(usize, usize, usize),
UnlockMonitorWorkspaceContainer(usize, usize, usize),
ToggleLock,
ToggleFloat,
ToggleMonocle,
ToggleMaximize,
// ToggleMaximize,
ToggleWindowContainerBehaviour,
ToggleFloatOverride,
ToggleCrossMonitorMoveBehaviour,
CrossMonitorMoveBehaviour(MoveBehaviour),
ToggleMonocleFocusBehaviour,
MonocleFocusBehaviour(MonocleFocusBehaviour),
UnmanagedWindowOperationBehaviour(OperationBehaviour),
// Current Workspace Commands
ManageFocusedWindow,
UnmanageFocusedWindow,
AdjustContainerPadding(Sizing, i32),
AdjustWorkspacePadding(Sizing, i32),
ChangeLayout(DefaultLayout),
CycleLayout(CycleDirection),
ScrollingLayoutColumns(NonZeroUsize),
ScrollingLayoutFullWidth,
CenterFocus,
Expand,
Consume(OperationDirection),
Expel,
ToggleColumnStack,
Pan(PanDirection),
LayoutRatios(Option<Vec<f32>>, Option<Vec<f32>>),
// ChangeLayoutCustom(#[serde_as(as = "ResolvedPathBuf")] PathBuf),
FlipLayout(Axis),
ToggleWorkspaceWindowContainerBehaviour,
ToggleWorkspaceFloatOverride,
// Monitor and Workspace Commands
// MonitorIndexPreference(usize, i32, i32, i32, i32),
DisplayIndexPreference(usize, String),
EnsureWorkspaces(usize, usize),
EnsureNamedWorkspaces(usize, Vec<String>),
NewWorkspace,
ToggleTiling,
Stop,
StopIgnoreRestore,
TogglePause,
Retile,
RetileWithResizeDimensions,
QuickSave,
QuickLoad,
Save(#[serde_as(as = "ResolvedPathBuf")] PathBuf),
Load(#[serde_as(as = "ResolvedPathBuf")] PathBuf),
CycleFocusMonitor(CycleDirection),
CycleFocusWorkspace(CycleDirection),
CycleFocusEmptyWorkspace(CycleDirection),
FocusMonitorNumber(usize),
FocusMonitorAtCursor,
FocusLastWorkspace,
CloseWorkspace,
FocusWorkspaceNumber(usize),
FocusWorkspaceNumbers(usize),
FocusMonitorWorkspaceNumber(usize, usize),
FocusNamedWorkspace(String),
ContainerPadding(usize, usize, i32),
NamedWorkspaceContainerPadding(String, i32),
FocusedWorkspaceContainerPadding(i32),
WorkspacePadding(usize, usize, i32),
NamedWorkspacePadding(String, i32),
FocusedWorkspacePadding(i32),
WorkspaceTiling(usize, usize, bool),
NamedWorkspaceTiling(String, bool),
WorkspaceName(usize, usize, String),
WorkspaceLayout(usize, usize, DefaultLayout),
NamedWorkspaceLayout(String, DefaultLayout),
// WorkspaceLayoutCustom(usize, usize, #[serde_as(as = "ResolvedPathBuf")] PathBuf),
// NamedWorkspaceLayoutCustom(String, #[serde_as(as = "ResolvedPathBuf")] PathBuf),
WorkspaceLayoutRule(usize, usize, usize, DefaultLayout),
NamedWorkspaceLayoutRule(String, usize, DefaultLayout),
// WorkspaceLayoutCustomRule(
// usize,
// usize,
// usize,
// #[serde_as(as = "ResolvedPathBuf")] PathBuf,
// ),
// NamedWorkspaceLayoutCustomRule(String, usize, #[serde_as(as = "ResolvedPathBuf")] PathBuf),
ClearWorkspaceLayoutRules(usize, usize),
ClearNamedWorkspaceLayoutRules(String),
ToggleWorkspaceLayer,
// Configuration
// ReloadConfiguration,
ReplaceConfiguration(#[serde_as(as = "ResolvedPathBuf")] PathBuf),
ReloadStaticConfiguration(#[serde_as(as = "ResolvedPathBuf")] PathBuf),
// WatchConfiguration(bool),
// CompleteConfiguration,
// AltFocusHack(bool),
Theme(Box<KomorebiTheme>),
Transparency(bool),
ToggleTransparency,
TransparencyAlpha(u8),
Animation(bool, Option<AnimationPrefix>),
AnimationDuration(u64, Option<AnimationPrefix>),
AnimationFps(u64),
AnimationStyle(AnimationStyle, Option<AnimationPrefix>),
#[serde(alias = "ActiveWindowBorder")]
Border(bool),
#[serde(alias = "ActiveWindowBorderColour")]
BorderColour(WindowKind, u32, u32, u32),
// #[serde(alias = "ActiveWindowBorderStyle")]
// BorderStyle(BorderStyle),
BorderWidth(i32),
BorderOffset(i32),
// BorderImplementation(BorderImplementation),
// InvisibleBorders(Rect),
WorkAreaOffset(Rect),
MonitorWorkAreaOffset(usize, Rect),
WorkspaceWorkAreaOffset(usize, usize, Rect),
ToggleWindowBasedWorkAreaOffset,
ResizeDelta(i32),
InitialWorkspaceRule(ApplicationIdentifier, String, usize, usize),
InitialNamedWorkspaceRule(ApplicationIdentifier, String, String),
WorkspaceRule(ApplicationIdentifier, String, usize, usize),
NamedWorkspaceRule(ApplicationIdentifier, String, String),
ClearWorkspaceRules(usize, usize),
ClearNamedWorkspaceRules(String),
ClearAllWorkspaceRules,
EnforceWorkspaceRules,
SessionFloatRule,
SessionFloatRules,
ClearSessionFloatRules,
#[serde(alias = "FloatRule")]
IgnoreRule(ApplicationIdentifier, String),
ManageRule(ApplicationIdentifier, String),
// IdentifyObjectNameChangeApplication(ApplicationIdentifier, String),
// IdentifyTrayApplication(ApplicationIdentifier, String),
// IdentifyLayeredApplication(ApplicationIdentifier, String),
// IdentifyBorderOverflowApplication(ApplicationIdentifier, String),
State,
GlobalState,
VisibleWindows,
MonitorInformation,
Query(StateQuery),
FocusFollowsMouse(FocusFollowsMouseImplementation, bool),
ToggleFocusFollowsMouse(FocusFollowsMouseImplementation),
// FocusFollowsMouse(FocusFollowsMouseImplementation, bool),
// ToggleFocusFollowsMouse(FocusFollowsMouseImplementation),
MouseFollowsFocus(bool),
ToggleMouseFollowsFocus,
// RemoveTitleBar(ApplicationIdentifier, String),
// ToggleTitleBars,
AddSubscriberSocket(String),
AddSubscriberSocketWithOptions(String, SubscribeOptions),
RemoveSubscriberSocket(String),
// AddSubscriberPipe(String),
// RemoveSubscriberPipe(String),
ApplicationSpecificConfigurationSchema,
NotificationSchema,
SocketSchema,
StaticConfigSchema,
GenerateStaticConfig,
DebugWindow(i64),
ReloadHotkeys,
HotkeysState,
}
impl SocketMessage {
/// Returns the message as JSON, the way the daemon reads it from its socket
pub fn as_bytes(&self) -> eyre::Result<Vec<u8>> {
Ok(serde_json::to_string(self)?.as_bytes().to_vec())
}
}
impl FromStr for SocketMessage {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_str(s)
}
}
pub use komorebi_protocol::message::SocketMessage;
#[cfg(test)]
mod tests {
@@ -2074,6 +2074,45 @@ impl WindowManager {
let state = serde_json::to_string_pretty(&crate::kwin::hotkeys::state())?;
reply.write_all(state.as_bytes())?;
}
// Messages of other platforms: answered, and nothing else happens
SocketMessage::AddSubscriberPipe(..)
| SocketMessage::AltFocusHack(..)
| SocketMessage::ApplyState(..)
| SocketMessage::BorderImplementation(..)
| SocketMessage::BorderStyle(..)
| SocketMessage::ChangeLayoutCustom(..)
| SocketMessage::CompleteConfiguration
| SocketMessage::ForceFocus
| SocketMessage::IdentifyBorderOverflowApplication(..)
| SocketMessage::IdentifyLayeredApplication(..)
| SocketMessage::IdentifyObjectNameChangeApplication(..)
| SocketMessage::IdentifyTrayApplication(..)
| SocketMessage::InvisibleBorders(..)
| SocketMessage::MonitorIndexPreference(..)
| SocketMessage::NamedWorkspaceLayoutCustom(..)
| SocketMessage::NamedWorkspaceLayoutCustomRule(..)
| SocketMessage::PanGesture(..)
| SocketMessage::ReloadConfiguration
| SocketMessage::RemoveSubscriberPipe(..)
| SocketMessage::RemoveTitleBar(..)
| SocketMessage::StackbarBackgroundColour(..)
| SocketMessage::StackbarFocusedTextColour(..)
| SocketMessage::StackbarFontFamily(..)
| SocketMessage::StackbarFontSize(..)
| SocketMessage::StackbarHeight(..)
| SocketMessage::StackbarLabel(..)
| SocketMessage::StackbarMode(..)
| SocketMessage::StackbarTabWidth(..)
| SocketMessage::StackbarUnfocusedTextColour(..)
| SocketMessage::ToggleTitleBars
| SocketMessage::WatchConfiguration(..)
| SocketMessage::WindowHidingBehaviour(..)
| SocketMessage::WorkspaceLayoutCustom(..)
| SocketMessage::WorkspaceLayoutCustomRule(..)
=> {
reply.write_all(message.not_supported_reply().as_bytes())?;
return Ok(());
}
}
self.update_known_window_ids();