Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib: add a keyboard layout per window script #4504

Closed
Closed
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
44 changes: 44 additions & 0 deletions contrib/keyboard-layout-per-window.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python

# This script keeps track of active keyboard layouts per window.
#
# This script requires i3ipc-python package (install it from a system package
# manager or pip).

import i3ipc

ipc = i3ipc.Connection()
prev_focused = -1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use current window id ipc.get_tree().find_focused().id?

windows = {}

def on_window_focus(ipc, event):
global windows, prev_focused

# Save current layout
layouts = {}
for input in ipc.get_inputs():
layouts[input.identifier] = input.xkb_active_layout_index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating layouts uninitialized is unnecessary here, you can create them initialized as:

    layouts = {input.identifier : input.xkb_active_layout_index
               for input in ipc.get_inputs()}

And it's faster as a bonus.

windows[prev_focused] = layouts

# Restore layout of the newly focused window
if event.container.id in windows:
emersion marked this conversation as resolved.
Show resolved Hide resolved
for (input_id, layout_index) in windows[event.container.id].items():
emersion marked this conversation as resolved.
Show resolved Hide resolved
if layout_index != layouts[input_id]:
ipc.command("input \"{}\" xkb_switch_layout {}".format(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ipc.command("input \"{}\" xkb_switch_layout {}".format(
ipc.command("""input "{}" xkb_switch_layout {}""".format(

maybe this is more readable, the back slashes really threw me off

input_id, layout_index))

prev_focused = event.container.id

def on_window_close(ipc, event):
global windows
if event.container.id in windows:
del(windows[event.container.id])

def on_window(ipc, event):
if event.change == "focus":
on_window_focus(ipc, event)
elif event.change == "close":
on_window_close(ipc, event)

ipc.on("window", on_window)
ipc.main()