artemsen / swaykbdd

Keyboard layout switcher for Sway
MIT License
72 stars 5 forks source link

Doesn't work with sway predefined layout option for particular windows #3

Closed timsofteng closed 2 years ago

timsofteng commented 2 years ago

Hello. I have this options for some apps: for_window [app_id="swappy"] input type:keyboard xkb_switch_layout 0 for_window [app_id="fuzzel"] input type:keyboard xkb_switch_layout 0 for_window [app_id="mpv"] input type:keyboard xkb_switch_layout 0

When I go back to previous window from one of them swaykbdd doesn't switch to its previous layout.

artemsen commented 2 years ago

Hi Tim, I can't reproduce this issue in my environment. Which version of Sway do you use? One more detail: your mouse (and any other input device) has layout too. Please, try to use * instead of type:keyboard.

timsofteng commented 2 years ago

Hi Tim, I can't reproduce this issue in my environment. Which version of Sway do you use? One more detail: your mouse (and any other input device) has layout too. Please, try to use * instead of type:keyboard.

Still doesn't work even with this * tip.

I'm using sway 1.6.1.

Steps to reproduce.

  1. Set for_window [app_id="swappy"] input * xkb_switch_layout 0 to some floating window (swappy in my case) in sway.
  2. Open any window and set different layout index to it than 0.
  3. Open swappy. (notice that layout was changed correctly).
  4. Close swappy by pressing esc. Focus will back to previous window BUT layout still 0.

Here is small screencast. https://user-images.githubusercontent.com/51912173/149501986-31735324-7a1b-4b80-be7b-b463dcde42cd.mp4

timsofteng commented 2 years ago

@artemsen can you reproduce it?

artemsen commented 2 years ago

Yes, I reproduced the problem, thank you!

This happens because Sway changes the keyboard layout before the new window gets focus. By the time the window from step 2 loses focus (and the layout save routine is triggered), rule 1 has already been applied. So, from the swaykbdd perspective, it looks like an user set layout 0 and then switched to the swappy.

And I still have no idea how to fix it :(

UPD. We can consider the timeout between layout change and focus lost events.

artemsen commented 2 years ago

@timsofteng, I will be grateful if you check this fix. There is a timeout (50ms) which is an ignored amount of time between layout change and focus loss. It works for me.

timsofteng commented 2 years ago

@artemsen can you please describe how to do it?

artemsen commented 2 years ago

Just update the package using AUR: https://aur.archlinux.org/packages/swaykbdd Or build and install from source:

meson build
ninja -C build
sudo ninja -C build install
timsofteng commented 2 years ago

Looks like it works. Thanks!