swaywm / sway

i3-compatible Wayland compositor
https://swaywm.org
MIT License
14.55k stars 1.11k forks source link

Load custom xkb keymap #3999

Closed Pu-Anlai closed 5 years ago

Pu-Anlai commented 5 years ago

I use a custom keyboard layout defined in ~/.config/xkb/custom.xkb which I upload to X using xkbcomp ~/.config/xkb/custom.xkb $DISPLAY. Is there any way to do the same in Sway? I tried input * xkb_layout ~/.config/xkb/custom.xkb but that didn't work.

I guess I could register my layout globally and then just load it with input * xkb_layout custom but a) I'd like to do this from within my home folder and b) making a layout available like that is unexpectedly complicated; I edited both the .lst and .xml files in /usr/share/X11/xkb/rules and that didn't work. Those changes could also be gone with the next system update.

ghost commented 5 years ago

Maybe it's not the same thing you mean, but I installed my own custom layout in /usr/share/X11/xkb/symbols/, with a custom name so it doesn't touch / isn't touched by anything else, and it works just fine. No need to register anything, just input YOURKEYBOARD xkb_layout YOURNAME

mdeff commented 5 years ago

I did that as well: custom keyboard layout in /usr/share/X11/xkb/symbols/custom, then input * xkb_layout custom.

progandy commented 5 years ago

Local user settings for keyboard layouts can be stored in ~/.xkb/symbols/ and then loaded by their names just like global layouts. (source)

mdeff commented 5 years ago

Confirmed to work! Thanks for the tip @progandy. I'll do that from now on.

Pu-Anlai commented 5 years ago

My apologies, I should have been more specific with my question. It's been a while since I set up my layout so I forgot about the xkb nomenclature. The thing is that my keyboard layout file not only contains an xkb_symbols section but also xkb_types and xkb_keycodes sections. This is all contained in a struct named xkb_keymap. This is the format returned by xkbcomp. I could probably get rid of the xkb_keycodes section but xkb_types is essential for me as that is where custom modifier levels are defined.

So when I try to load this file by doing input [identifier] xkb_layout custom, I get an error because only an xkb_symbols section is expected:

2019-04-04 21:40:33 - [sway-1.0/sway/input/seat.c:673] adding device 1133:16388:Logitech_K360 to seat seat0
xkbcommon: ERROR: Include file of wrong type (expected xkb_symbols, got xkb_keymap); Include file "custom" ignored
xkbcommon: ERROR: Abandoning symbols file "(unnamed)"
xkbcommon: ERROR: Failed to compile xkb_symbols
xkbcommon: ERROR: Failed to compile keymap
2019-04-04 21:40:33 - [sway-1.0/sway/input/keyboard.c:518] cannot configure keyboard: keymap does not exist

So I guess my real question would be, is there any way to load an xkb keymap file? I could of course get rid of the xkb_keycodes section and put the xkb_symbols and xkb_types sections into their own files. Then I could load the symbols file with input [identifier] xkb_layout custom_symbols. However, as far as I know, there is no way to load a custom types file, right?

Emantor commented 5 years ago

Accordding to https://www.vinc17.net/unix/xkb.en.html ~/.xkb/keymap should work.

Pu-Anlai commented 5 years ago

Accordding to https://www.vinc17.net/unix/xkb.en.html ~/.xkb/keymap should work.

Yeah, but that's for X. The location of the keymap is also incidental because you can put it anywhere and then just upload it to X using xkbcomp [location of file] $DISPLAY.

progandy commented 5 years ago

So I guess my real question would be, is there any way to load an xkb keymap file?

There is no way. Maybe you can create a patch with a new configuration option that makes sway ignore the RMLVO configuration and load a keymap from a file with xkb_keymap_new_from_file

However, as far as I know, there is no way to load a custom types file, right?

That is not straight forward. You could write your own xkb_rules file which in the end produces your desired layout if you really want to. These articles may help you:

emersion commented 5 years ago

I wouldn't be against a patch that uses xkb_keymap_new_from_file

Pu-Anlai commented 5 years ago

Phew, my C knowledge is pretty much zero. I looked at keyboard.c and input.c and don't think I will be able to implement this at any point in the near future. Unless one of the existing developers has an interest in this, I will try to accomplish this with xkb_rules.

CRCulver commented 4 years ago

I have successfully migrated from my X11 setup using xkbcomp like the OP, to Sway using the xkb_file option added through the above-mentioned commits. However, while Wayland applications load my keyboard settings, XWayland applications do not.

Is it reasonable to expect Sway’s xkb_file setting to apply to XWayland windows as well? I cannot find any way to configure XWayland’s keyboard settings, so I assume the Wayland compositor passes them to XWayland.

emersion commented 4 years ago

Is it reasonable to expect Sway’s xkb_file setting to apply to XWayland windows as well?

Yes.

CRCulver commented 4 years ago

Shall I therefore reopen this Github issue, or open a new one?

emersion commented 4 years ago

If it doesn't work, open a bug report.

isti115 commented 4 years ago

Many many thanks to @edyounis I was finally able to recreate what I had earlier done in i3 using Xmodmap. My issue was that inet(evdev) would override what I have defined in my own symbols file, but using a complete xkb_keymap I was able to fix the order and for me it works in xwayland windows, such as firefox as well.

CodedBinary commented 4 years ago

First I'd like to thank you for getting this far.

I have a somewhat peculiar setup and a peculiar problem with it. Basically, I've rebound the following, and get the following instead:

Key combo Xorg Outcome sway Outcome
Caps + a - nothing
Caps + s + nothing
Caps + d backspace -
Caps + f enter +
Caps + g / enter
Caps + hjkl left down up right left down up right
Caps + qwe.. 123... 123...
Caps + Shift + qwe... !@#... !@#...
Caps + x ` nothing
Caps + c comma *
Caps + v period /
Caps + Alt + q f1 nothing
Caps + Super + h home nothing

Heres my setup: my .xkeymap: https://pastebin.com/wph6WY1k

First, CAPS (the key, not the sym) is the key i use for accessing the extra layers. This is then bound to NumLock to avoid weirdness when programs look for the caps state, and to stop the light on my keyboard blinking. The rest of the config looks at the state of NumLock, not Lock, for whether they should enter my extra layers (stored in xkb_types, of course). When a key is pressed, it is meant to redirect the keystroke to a different key (not a sym) and clear the relevant modifiers. This part is done in xkb_symbols, next to each key.

One reason this seems so convoluted is I need things like caps+alt+j (caps+j is down) to look like alt+down, with no other modifiers. Similarly, I need shift+caps+x to give me ~ (caps+x gives `) with no modifiers.

Here is a quick rundown of my types:

Another issue (which I don't think is this program's fault) is that only in vim it doesn't deal handle holding down movements. For instance, I hold down j and I move down once. Weird thing is I hold down downarrow and its as normal - but if I hold down caps+j, which should be exactly downarrow, it moves once.

Other programs seem to handle it normally, but I thought I'd ask if you happened to know anything about why that may be before I pop over somewhere vim related. Let me know if you need me to explain how my config works any more, or if you have a nice way to implement this functionality in a non breaking way.

Nikola-Milovic commented 1 year ago

Anyone can give their input on why my setup does not work as expected?

Ubuntu 22.04, sway version 1.7

input * xkb_file ~/keyboard/real_prog_dvorak.xkb

If I leave it as is, it does not work for xwayland apps, but works for the terminal and other apps. If I add the

exec_always xkbcomp ~/keyboard/real_prog_dvorak.xkb $DISPLAY

It still does not work, but if I just run xkbcomp ~/keyboard/real_prog_dvorak.xkb $DISPLAY from the terminal afterward, it starts working as expected for the xwayland apps as well

My .xkb can be found here, I got it by dumping it

xkbcomp $DISPLAY current_keymap.xkb

I get some warnings and an error but it seems to be working fine otherwise.

+ ~ ➜ xkbcomp ~/keyboard/real_prog_dvorak.xkb $DISPLAY
Error:            Key <CAPS> added to map for multiple modifiers
                  Using Control, ignoring Lock.
Warning:          No symbols defined for <AB11> (keycode 97)
Warning:          No symbols defined for <JPCM> (keycode 103)
Warning:          No symbols defined for <I120> (keycode 120)
Warning:          No symbols defined for <AE13> (keycode 132)
Warning:          No symbols defined for <I149> (keycode 149)
Warning:          No symbols defined for <I154> (keycode 154)
Warning:          No symbols defined for <I168> (keycode 168)
Warning:          No symbols defined for <I178> (keycode 178)
Warning:          No symbols defined for <I183> (keycode 183)
Warning:          No symbols defined for <I184> (keycode 184)
Warning:          No symbols defined for <FK19> (keycode 197)
Warning:          No symbols defined for <FK24> (keycode 202)
Warning:          No symbols defined for <I217> (keycode 217)
Warning:          No symbols defined for <I219> (keycode 219)
Warning:          No symbols defined for <I222> (keycode 222)
Warning:          No symbols defined for <I230> (keycode 230)
Warning:          No symbols defined for <I248> (keycode 248)