Skip to content
Open
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
17 changes: 17 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ driftwm msg --json focus --id 5
| Command | Description |
| --- | --- |
| [`state`](#driftwm-msg-state) | Dump camera, zoom, and the window inventory |
| [`bindings`](#driftwm-msg-bindings) | List the effective keybindings: built-in defaults merged with the config |
| [`subscribe`](#driftwm-msg-subscribe) | Stream state snapshots as they change (one JSON line per event with --json) |
| [`focus`](#driftwm-msg-focus) | Print the focused window, or focus one by `app_id` substring or `--id` |
| [`move`](#driftwm-msg-move) | Get a window's position, or move it to `<x> <y>` (visible-frame center, Y-up) |
Expand Down Expand Up @@ -85,6 +86,22 @@ Also prints the keyboard layout, the fullscreen and pinned screen-space inventor
driftwm msg --json state | jq '.Ok.State.windows'
```

#### `driftwm msg bindings`

```
driftwm msg bindings
```

List the effective keybindings: built-in defaults merged with the config.

Prints keyboard, tap and mouse bindings in config syntax; mouse bindings are tagged with the `[mouse.*]` context they live in. Reflects the config as currently loaded, including hot reloads. Gesture and touch bindings are not listed.

`--json` reply: `{"Ok":{"Bindings":{"mod_key":"super","keys":[{"trigger":"super+Return","action":"exec-terminal"}],"taps":[..],"mouse":[..]}}}`.

```bash
driftwm msg --json bindings | jq -r '.Ok.Bindings.keys[] | "\(.trigger)\t\(.action)"'
```

#### `driftwm msg subscribe`

```
Expand Down
160 changes: 160 additions & 0 deletions src/config/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//! Config-syntax rendering of bindings: the inverse of the `parse_*` functions,
//! so a binding read back out of the compositor reads like the config line that
//! produced it.

use std::fmt;

use smithay::input::keyboard::xkb;

use super::types::*;

impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Direction::Up => "up",
Direction::Down => "down",
Direction::Left => "left",
Direction::Right => "right",
Direction::UpLeft => "up-left",
Direction::UpRight => "up-right",
Direction::DownLeft => "down-left",
Direction::DownRight => "down-right",
})
}
}

impl fmt::Display for LayoutSwitch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LayoutSwitch::Next => f.write_str("next"),
LayoutSwitch::Prev => f.write_str("prev"),
LayoutSwitch::Index(i) => write!(f, "{i}"),
}
}
}

impl fmt::Display for TrackpadState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
TrackpadState::Toggle => "toggle",
TrackpadState::On => "on",
TrackpadState::Off => "off",
})
}
}

impl fmt::Display for Action {
/// Exhaustive on purpose: a new `Action` variant fails to compile here until
/// it is given its config spelling.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Action::Exec(cmd) => write!(f, "exec {cmd}"),
Action::ExecTerminal => f.write_str("exec-terminal"),
Action::ExecLauncher => f.write_str("exec-launcher"),
Action::Spawn(cmd) => write!(f, "spawn {cmd}"),
Action::CloseWindow => f.write_str("close-window"),
Action::SuspendWindow => f.write_str("suspend-window"),
Action::NudgeWindow(d) => write!(f, "nudge-window {d}"),
Action::MoveToSnap(d) => write!(f, "move-to-snap {d}"),
Action::GrowWindow(d) => write!(f, "grow-window {d}"),
Action::ShrinkWindow(d) => write!(f, "shrink-window {d}"),
Action::ResizeToSnap(d) => write!(f, "resize-to-snap {d}"),
Action::PanViewport(d) => write!(f, "pan-viewport {d}"),
Action::CenterWindow => f.write_str("center-window"),
Action::CenterNearest(d) => write!(f, "center-nearest {d}"),
Action::FocusNearest(d) => write!(f, "focus-nearest {d}"),
Action::CycleWindows { backward: false } => f.write_str("cycle-windows forward"),
Action::CycleWindows { backward: true } => f.write_str("cycle-windows backward"),
Action::HomeToggle => f.write_str("home-toggle"),
Action::GoToBookmark(name) => write!(f, "go-to-bookmark {name}"),
Action::SetBookmark(name) => write!(f, "set-bookmark {name}"),
Action::MoveToBookmark(name) => write!(f, "move-to-bookmark {name}"),
Action::ZoomIn => f.write_str("zoom-in"),
Action::ZoomOut => f.write_str("zoom-out"),
Action::ZoomReset => f.write_str("zoom-reset"),
Action::ZoomToFit => f.write_str("zoom-to-fit"),
Action::ZoomToFitSnapped => f.write_str("zoom-to-fit-snapped"),
Action::ToggleFullscreen => f.write_str("toggle-fullscreen"),
Action::FitWindow => f.write_str("fit-window"),
Action::FitWindowSnapped => f.write_str("fit-window-snapped"),
Action::FillWindow => f.write_str("fill-window"),
Action::SendToOutput(d) => write!(f, "send-to-output {d}"),
Action::SendCursorToOutput(d) => write!(f, "send-cursor-to-output {d}"),
Action::FocusCenter => f.write_str("focus-center"),
Action::TogglePinToScreen => f.write_str("toggle-pin-to-screen"),
Action::SwitchLayout(target) => write!(f, "switch-layout {target}"),
Action::ReloadConfig => f.write_str("reload-config"),
Action::ToggleCursorPan => f.write_str("toggle-cursor-pan"),
Action::SetTrackpad(state) => write!(f, "set-trackpad {state}"),
Action::Quit => f.write_str("quit"),
}
}
}

impl fmt::Display for MouseAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MouseAction::MoveWindow => f.write_str("move-window"),
MouseAction::MoveSnappedWindows => f.write_str("move-snapped-windows"),
MouseAction::ResizeWindow => f.write_str("resize-window"),
MouseAction::ResizeWindowSnapped => f.write_str("resize-window-snapped"),
MouseAction::PanViewport => f.write_str("pan-viewport"),
MouseAction::Zoom => f.write_str("zoom"),
MouseAction::CenterNearest => f.write_str("center-nearest"),
MouseAction::FocusNearest => f.write_str("focus-nearest"),
MouseAction::Action(action) => action.fmt(f),
}
}
}

fn modifier_names(m: &Modifiers) -> impl Iterator<Item = &'static str> {
[
(m.logo, "super"),
(m.ctrl, "ctrl"),
(m.alt, "alt"),
(m.shift, "shift"),
(m.mod3, "mod3"),
]
.into_iter()
.filter_map(|(on, name)| on.then_some(name))
}

fn write_modifier_prefix(f: &mut fmt::Formatter<'_>, m: &Modifiers) -> fmt::Result {
modifier_names(m).try_for_each(|name| write!(f, "{name}+"))
}

impl fmt::Display for Modifiers {
/// A bare modifier chord, as a tap binding is written (`alt+shift`).
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&modifier_names(self).collect::<Vec<_>>().join("+"))
}
}

impl fmt::Display for KeyCombo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write_modifier_prefix(f, &self.modifiers)?;
f.write_str(&xkb::keysym_get_name(self.sym))
}
}

impl fmt::Display for MouseTrigger {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MouseTrigger::Button(BTN_LEFT) => f.write_str("left"),
MouseTrigger::Button(BTN_RIGHT) => f.write_str("right"),
MouseTrigger::Button(BTN_MIDDLE) => f.write_str("middle"),
MouseTrigger::Button(code) => write!(f, "button-{code:#x}"),
MouseTrigger::TrackpadScroll => f.write_str("trackpad-scroll"),
MouseTrigger::WheelScroll => f.write_str("wheel-scroll"),
MouseTrigger::WheelUp => f.write_str("wheel-up"),
MouseTrigger::WheelDown => f.write_str("wheel-down"),
}
}
}

impl fmt::Display for MouseBinding {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write_modifier_prefix(f, &self.modifiers)?;
self.trigger.fmt(f)
}
}
9 changes: 9 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod defaults;
mod display;

mod parse;
mod parse_helpers;
Expand Down Expand Up @@ -321,6 +322,14 @@ impl Config {
self.bindings.get(&combo)
}

pub fn iter_key_bindings(&self) -> impl Iterator<Item = (&KeyCombo, &Action)> {
self.bindings.iter()
}

pub fn iter_tap_bindings(&self) -> impl Iterator<Item = (&Modifiers, &Action)> {
self.tap_bindings.iter()
}

/// Look up a tap-modifier binding by the completed chord's modifier set.
pub fn tap_lookup(&self, mods: &Modifiers) -> Option<&Action> {
self.tap_bindings.get(mods)
Expand Down
37 changes: 36 additions & 1 deletion src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf};

use super::protocol::{
Event, Reply, Request, Response, ScreenshotTarget, StateInfo, WindowSelector, socket_path,
BindingInfo, BindingsInfo, Event, Reply, Request, Response, ScreenshotTarget, StateInfo,
WindowSelector, socket_path,
};

/// `driftwm msg <...>` subcommands. Variants with optional positionals read when
Expand All @@ -23,6 +24,15 @@ pub enum Msg {
///
/// `--json` reply: `{"Ok":{"State":{"camera":[..],"zoom":1.0,"windows":[..],"outputs":[..]}}}`.
State,
/// List the effective keybindings: built-in defaults merged with the config.
///
/// Prints keyboard, tap and mouse bindings in config syntax; mouse
/// bindings are tagged with the `[mouse.*]` context they live in. Reflects the config as
/// currently loaded, including hot reloads. Gesture and touch bindings are
/// not listed.
///
/// `--json` reply: `{"Ok":{"Bindings":{"mod_key":"super","keys":[{"trigger":"super+Return","action":"exec-terminal"}],"taps":[..],"mouse":[..]}}}`.
Bindings,
/// Stream state snapshots as they change (one JSON line per event with --json).
///
/// The server acks, pushes the current state immediately, then pushes a
Expand Down Expand Up @@ -406,6 +416,7 @@ fn to_request(msg: &Msg) -> Result<Request, String> {
Msg::Layout { short } => Request::Layout { short: *short },
Msg::Cursor => Request::Cursor,
Msg::State => Request::State,
Msg::Bindings => Request::Bindings,
Msg::DebugCounters => Request::DebugCounters,
Msg::Subscribe => Request::Subscribe,
Msg::Focus { app_id, id } => Request::Focus(window_selector(app_id, *id)),
Expand Down Expand Up @@ -640,6 +651,7 @@ fn print_response(response: Response) {
Response::Screenshot { path, .. } => println!("{path}"),
Response::Ok => println!("ok"),
Response::State(info) => print_state(&info),
Response::Bindings(info) => print_bindings(&info),
Response::DebugCounters(counters) => {
for (key, value) in counters {
println!("{key}: {value}");
Expand All @@ -648,6 +660,29 @@ fn print_response(response: Response) {
}
}

fn print_bindings(info: &BindingsInfo) {
println!("mod {}", info.mod_key);
let sections = [
("keys", &info.keys),
("taps", &info.taps),
("mouse", &info.mouse),
];
for (name, bindings) in sections {
println!("{name}");
for BindingInfo {
trigger,
context,
action,
} in bindings
{
match context {
Some(context) => println!(" {trigger} [{context}] = {action}"),
None => println!(" {trigger} = {action}"),
}
}
}
}

fn print_state(info: &StateInfo) {
println!("camera {} {}", info.camera.0, info.camera.1);
println!("zoom {}", info.zoom);
Expand Down
50 changes: 48 additions & 2 deletions src/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use smithay::wayland::seat::WaylandFocus;

use crate::decorations::DecorationKey;
use crate::state::{DriftWm, NavZoom, RevealPan, StageWindow, SuspendedId};
use driftwm::config::{Config, ModKey};
use driftwm::window_ext::WindowExt;

pub mod client;
pub mod protocol;

use self::protocol::{
Event, OutputInfo, Reply, Request, Response, ScreenshotTarget, StateInfo, WindowSelector,
socket_path,
BindingInfo, BindingsInfo, Event, OutputInfo, Reply, Request, Response, ScreenshotTarget,
StateInfo, WindowSelector, socket_path,
};

/// Reject a command line longer than this (without a newline) — bounds the
Expand Down Expand Up @@ -269,6 +270,7 @@ pub(crate) fn dispatch(request: Request, state: &mut DriftWm) -> Reply {
Request::Layout { short } => cmd_layout(short, state),
Request::Cursor => cmd_cursor(state),
Request::State => Ok(cmd_state(state)),
Request::Bindings => Ok(Response::Bindings(bindings_info(&state.config))),
Request::DebugCounters => Ok(Response::DebugCounters(state.debug_counters())),
// Handled in serve_connection, which has the raw stream Subscribe needs.
Request::Subscribe => unreachable!("Subscribe is handled before dispatch"),
Expand Down Expand Up @@ -432,6 +434,50 @@ fn layout_code(layout_list: &str, index: usize) -> Option<String> {
.map(str::to_owned)
}

fn bindings_info(config: &Config) -> BindingsInfo {
fn sorted(mut list: Vec<BindingInfo>) -> Vec<BindingInfo> {
list.sort_by(|a, b| (&a.trigger, &a.context).cmp(&(&b.trigger, &b.context)));
list
}
let binding = |trigger: String, context: Option<&str>, action: String| BindingInfo {
trigger,
context: context.map(str::to_owned),
action,
};

let keys = config
.iter_key_bindings()
.map(|(combo, action)| binding(combo.to_string(), None, action.to_string()))
.collect();
let taps = config
.iter_tap_bindings()
.map(|(mods, action)| binding(mods.to_string(), None, action.to_string()))
.collect();
let mouse = [
(&config.mouse.anywhere, None),
(&config.mouse.on_window, Some("on-window")),
(&config.mouse.on_canvas, Some("on-canvas")),
]
.into_iter()
.flat_map(|(map, context)| {
map.iter()
.map(move |(trigger, action)| binding(trigger.to_string(), context, action.to_string()))
})
.collect();

BindingsInfo {
mod_key: match config.mod_key {
ModKey::Alt => "alt",
ModKey::Super => "super",
ModKey::Mod3 => "mod3",
}
.to_owned(),
keys: sorted(keys),
taps: sorted(taps),
mouse: sorted(mouse),
}
}

fn cmd_state(state: &mut DriftWm) -> Response {
Response::State(Box::new(state_info(state)))
}
Expand Down
Loading