i3-companion: use bindings to communicate with companion

This avoids spawning an i3-msg process. The drawback is that we have
to listen to all keybindings.
This commit is contained in:
Vincent Bernat 2021-07-08 11:58:29 +02:00
parent 956bfa4965
commit 5239148319
2 changed files with 25 additions and 17 deletions

View file

@ -111,7 +111,7 @@ async def _new_workspace(i3):
async def new_workspace(i3, event):
"""Create a new workspace and optionally move a window to it."""
# Get the currently focused window
if event.payload == "move-to-new-workspace":
if event == "move-to-new-workspace":
tree = await i3.get_tree()
current = tree.find_focused()
if not current:
@ -120,7 +120,7 @@ async def new_workspace(i3, event):
num = await _new_workspace(i3)
# Move the window to this workspace
if event.payload == "move-to-new-workspace":
if event == "move-to-new-workspace":
await current.command(f'move container to workspace '
f'number "{num}"')
@ -182,10 +182,10 @@ async def worksplace_exclusive(i3, event):
async def quake_console(i3, event):
"""Spawn a quake console or toggle an existing one."""
try:
_, term_exec, term_name, height = event.payload.split(":")
_, term_exec, term_name, height = event.split(":")
height = float(height)
except Exception as exc:
logger.warn(f"unable to parse payload {event.payload}: {exc}")
logger.warn(f"unable to parse payload {event}: {exc}")
return
tree = await i3.get_tree()
@ -295,17 +295,25 @@ async def main(options):
if isinstance(event, Event):
i3.on(event, fn)
# Ticks
async def tick_event(i3, event):
"""Process a TICK event."""
if type(event.payload) is not str:
# React to some bindings
async def binding_event(i3, event):
"""Process a binding event."""
# We only processes it when it is a nop command and we use
# this mechanism as an IPC mechanism. The alternative would be
# to use ticks but we would need to spawn an i3-msg process
# for that.
cmd = event.binding.command
if not cmd.startswith("nop "):
return
kind = event.payload.split(":")[0]
cmd = cmd[4:].strip('"\'')
if not cmd:
return
kind = cmd.split(":")[0]
for fn, events in on.functions.items():
for e in events:
if e == kind:
await fn(i3, event)
i3.on(Event.TICK, tick_event)
await fn(i3, cmd)
i3.on(Event.BINDING, binding_event)
await i3.main()

12
config
View file

@ -123,7 +123,7 @@ bindsym $mod+9 workspace number $ws9
bindsym $mod+0 workspace number $ws10
bindsym $mod+Tab workspace back_and_forth
bindsym $mod+Ctrl+o focus output right
bindsym $mod+n exec --no-startup-id i3-msg -t send_tick "new-workspace"
bindsym $mod+n nop "new-workspace"
focus_wrapping workspace
# move focused container to workspace
@ -138,7 +138,7 @@ bindsym $mod+Shift+8 move container to workspace number $ws8
bindsym $mod+Shift+9 move container to workspace number $ws9
bindsym $mod+Shift+0 move container to workspace number $ws10
bindsym $mod+Shift+o move workspace to output right
bindsym $mod+Shift+n exec --no-startup-id i3-msg -t send_tick "move-to-new-workspace"
bindsym $mod+Shift+n nop "move-to-new-workspace"
# reload/restart
bindsym $mod+Shift+c reload
@ -150,12 +150,12 @@ bindsym $mod+Delete exec --no-startup-id xset s activate
bindsym Print exec --no-startup-id "flameshot gui -r | xclip -selection clipboard -t image/png"
# Quake window
set $quake "QuakeConsoleNeedsUniqueName"
for_window [instance=$quake title=$term] move window to scratchpad
bindsym $mod+grave exec --no-startup-id i3-msg -t send_tick "quake-console:$term:$quake:0.3"
set $quake QuakeConsoleNeedsUniqueName
for_window [instance="$quake" title=$term] move window to scratchpad
bindsym $mod+grave nop "quake-console:$term:$quake:0.3"
# Get info about an app (like xprop)
bindsym $mod+i exec --no-startup-id i3-msg -t send_tick "info"
bindsym $mod+i nop "info"
# Random rules
no_focus [window_type="splash"]