free5lot / hid-apple-patched

Allows to swap the Fn key and left Control key and other tweaks on Macbook Pro and Apple keyboards in GNU/Linux
GNU General Public License v2.0
349 stars 61 forks source link

iso_layout not working when the keyboard is connected via Bluetooth #68

Closed daquinons closed 4 years ago

daquinons commented 4 years ago

I've successfully installed the driver using DKMS, but the parameter iso_layout doesn't take any effect. For example, ejectcd_as_delete works (not in all the input fields and applications, but at least the standard eject is not appearing every time that I missclick).

Anyway, when I connect the keyboard via usb, iso_layout is used. I can see it because the mapping of the >< key is correct.

Please, let me know if I can provide any debugging information.

I just got the Keyboard and I haven't updated the firmware because I don't have a Mac right now.

daquinons commented 4 years ago

Another test that may give some information about what's going on.

Is the bluetooth using a different driver?

huseyin1419 commented 4 years ago

@daquinons Same Bug This module works great except two keys. [ The key below ESC ] and [ The key next to the Left Shift ] mixed. When I connect the keyboard via USB, everything works as expected. When I connect the keyboard via Bluetooth, the keys are wrongly mapped.

I bought [ Apple Magic Keyboard 2, without numpad version, Turkish Q Layout ] two days ago. I installed Arch Linux ( Turkish F Layout ) yesterday.

LOGS

$ xev -event keyboard # WHEN I PRESS THE KEY BELOW ESC
KeyPress event, serial 28, synthetic NO, window 0x4c00001,
    root 0x5c2, subw 0x0, time 1471626, (553,469), root:(589,498),
    state 0x0, keycode 94 (keysym 0x3c, less), same_screen YES,
    XLookupString gives 1 bytes: (3c) "<"
    XmbLookupString gives 1 bytes: (3c) "<"
    XFilterEvent returns: False

KeyRelease event, serial 28, synthetic NO, window 0x4c00001,
    root 0x5c2, subw 0x0, time 1471626, (553,469), root:(589,498),
    state 0x0, keycode 94 (keysym 0x3c, less), same_screen YES,
    XLookupString gives 1 bytes: (3c) "<"
    XFilterEvent returns: False

$ xev -event keyboard # WHEN I PRESS THE KEY NEXT TO THE LEFT SHIFT
KeyPress event, serial 25, synthetic NO, window 0x5000001,
    root 0x5c2, subw 0x0, time 1612400, (467,433), root:(503,462),
    state 0x0, keycode 49 (keysym 0x2b, plus), same_screen YES,
    XLookupString gives 1 bytes: (2b) "+"
    XmbLookupString gives 1 bytes: (2b) "+"
    XFilterEvent returns: False

KeyRelease event, serial 28, synthetic NO, window 0x5000001,
    root 0x5c2, subw 0x0, time 1612460, (467,433), root:(503,462),
    state 0x0, keycode 49 (keysym 0x2b, plus), same_screen YES,
    XLookupString gives 1 bytes: (2b) "+"
    XFilterEvent returns: False

# dkms status
hid-apple, 1.0, 5.6.15-arch1-1, x86_64: installed
hid-apple-patched, 20200410.d5ffaff, 5.6.15-arch1-1, x86_64: installed (original_module exists)

# ls /sys/module/hid_apple/parameters/
ejectcd_as_delete  fnmode  iso_layout  rightalt_as_rightctrl  swap_fn_leftctrl  swap_opt_cmd

# cat /sys/module/hid_apple/parameters/iso_layout 
1

# uname -a
Linux arch 5.6.15-arch1-1 #1 SMP PREEMPT Wed, 27 May 2020 23:42:26 +0000 x86_64 GNU/Linux

# cat /etc/modprobe.d/hid_apple.conf 
options hid_apple fnmode=2 swap_fn_leftctrl=1 swap_opt_cmd=1 rightalt_as_rightctrl=1 ejectcd_as_delete=1

# echo "0" > /sys/module/hid_apple/parameters/iso_layout # not working
# echo "1" > /sys/module/hid_apple/parameters/iso_layout # not working
# echo "0" > /sys/module/hid_apple/parameters/iso_layout # not working
# echo "1" > /sys/module/hid_apple/parameters/iso_layout # not working

I applied these solutions, apple keyboard works without problems but this time laptop keys are mixing (I use apple keyboard with laptop)

In Arch Linux, apple:badmap does not work. Because some settings missing. I copied these files from Debian 10 Machine to Arch Linux Machine and replaced same destination.

/usr/share/X11/xkb/keycodes/macintosh
/usr/share/X11/xkb/rules/evdev
/usr/share/X11/xkb/rules/base

Fix 1 Temporary

setxkbmap -option apple:badmap

Fix 2 Permanent

$ cat /usr/share/X11/xorg.conf.d/00-keyboard.conf
Section "InputClass"
    Identifier "system-keyboard"
    MatchIsKeyboard "on"
    Option "XkbLayout" "tr"
    Option "XkbModel" "pc101"
    Option "XkbVariant" "f"
    Option "XkbOptions" "apple:badmap"
EndSection

Here is how I installed this module

# 1 pacman -S base-devel dkms linux-headers

# 2 This Aur package is dependency. aur.archlinux.org/packages/mkinitcpio-modconf-hook-git/

# 3 I think Aur package is recommended way aur.archlinux.org/packages/hid-apple-patched-git-dkms/


Note: I installed Aur packages with makepkg -si command. And I did not run mkinitcpio -p linux command. Aur package automaticly updates initramfs.

free5lot commented 4 years ago

iso_layout is a parameter from the original mainstream hid-apple and is not expected to act differently. To be clear, how does iso_layout work with the original hid-apple (without this repo) with Bluetooth keyboard? Is it acting differently with mainstream kernel? Because according to the code it should not.

huseyin1419 commented 4 years ago

@free5lot Same result. iso_layout not working with the original hid_apple (without this repo) with Bluetooth keyboard. [ Tested now with fresh Arch Linux installation ] It is not acting differently with mainstream kernel.

free5lot commented 4 years ago

@ozdemir1419 that's what I've expected. The patched version should not affect iso_layout in any way. I'm not sure this issue can be fixed in this repo, you probably should file the report to the mainstream bug-trackers or connect to the maintainers directly. If it's fixed upstream it will be migrated here too as this repo is not modifying how iso_layout acts.

I searched a bit and it seems this problem exists in mainstream for a while: Params to hid_apple kernel module not recognized for Bluetooth Apple keyboard - Linux Mint Forums

I will keep this issue open for a while so people be able to discuss it and maybe provide any ideas or links for bug reports.

daquinons commented 4 years ago

I reported it here as well https://bugzilla.kernel.org/show_bug.cgi?id=99881

free5lot commented 4 years ago

@daquinons thanks. Maybe they will fix.

But we can try to do it too. One thing we can check easily is to modify one line, install via DKMS and check. Anyone with Bluetooth keyboard can do it?

My idea would be modify this line: https://github.com/free5lot/hid-apple-patched/blob/master/hid-apple.c#L289 by replacing if (hid->country == HID_COUNTRY_INTERNATIONAL_ISO) { with if (true) {

Then test if iso_layout is working properly after it. I have no such Bluetooth keyboard. Anyone?

Maybe for Bluetooth keyboards in Bluetooth mode for some reason hid->country is not equal to HID_COUNTRY_INTERNATIONAL_ISO. This condition was added recently (2017), previously it was different, take a look.

huseyin1419 commented 4 years ago

@free5lot THANKS! Now iso_layout working without problems with apple magic bluetooth keyboard 2 without numpad version in Arch Linux.

free5lot commented 4 years ago

@ozdemir1419 great, you are welcome!

We need the second confirmation and I will write to the upstream maintainer, actually it seems this problem was in the upstream kernel for about 3 years.

daquinons commented 4 years ago

EDIT: Solved the previous issue

EDIT2: After testing, all the functions seems to apply, but still iso_layout not applying :(

I'm using a Spanish Keyboard, the wrongly mapped keys are still º and <. Any way to test that the module has been built using the modified hid-apple.c file?

huseyin1419 commented 4 years ago

@daquinons I succeed by uninstalling and reinstalling dkms module. I did these things:

Unload Module # modprobe -r hid_apple

List all dkms modules with # dkms status

Then, remove all dkms modules # dkms remove hid-apple/1.0 --all # dkms remove hid-apple-patched/20200608.3afbab7 --all this code can be different in your case (3afbab7)

Then, list remaining modules and continue uninstalling modules # dkms status

I think you should clean old module source code like I did.

# ls /var/lib/dkms/ # check any hid-apple folder and delete all hid-apple folders 
# ls /usr/src/ # continue deleting hid-apple folders

Reboot and check mainstream kernel module loaded # modinfo hid_apple It should show 3 parm values. If it shows 6 parm values, we failed uninstalling patched module. Do not continue. Go to step 1 or get help.

Edit hid-apple.c file. Just replace line 289 with if (true) {

Example

...
287
288    if (iso_layout) {
289        if (true) {
290            trans = apple_find_translation(apple_iso_keyboard, usage->code);
291            if (trans) {
292                    input_event(input, usage->type, trans->to, value);
293                    return 1;
294            }
295        }
296    }
297
...

Install fixed module

# dkms add .
# dkms build hid-apple/1.0
# dkms install hid-apple/1.0

Load fixed module # modprobe -r hid_apple ; modprobe hid_apple

Check iso_layout working

# echo "1" > /sys/module/hid_apple/parameters/iso_layout

If it is not working I think you should reinstall your linux distribution.

daquinons commented 4 years ago

@daquinons I succeed by uninstalling and reinstalling dkms module. I did these things:

Unload Module # modprobe -r hid_apple

List all dkms modules with # dkms status

Then, remove all dkms modules # dkms remove hid-apple/1.0 --all # dkms remove hid-apple-patched/20200608.3afbab7 --all this code can be different in your case (3afbab7)

Then, list remaining modules and continue uninstalling modules # dkms status

I think you should clean old module source code like I did.

# ls /var/lib/dkms/ # check any hid-apple folder and delete all hid-apple folders 
# ls /usr/src/ # continue deleting hid-apple folders

Reboot and check mainstream kernel module loaded # modinfo hid_apple It should show 3 parm values. If it shows 6 parm values, we failed uninstalling patched module. Do not continue. Go to step 1 or get help.

Edit hid-apple.c file. Just replace line 289 with if (true) {

Example

...
287
288    if (iso_layout) {
289        if (true) {
290            trans = apple_find_translation(apple_iso_keyboard, usage->code);
291            if (trans) {
292                    input_event(input, usage->type, trans->to, value);
293                    return 1;
294            }
295        }
296    }
297
...

Install fixed module

# dkms add .
# dkms build hid-apple/1.0
# dkms install hid-apple/1.0

Load fixed module # modprobe -r hid_apple ; modprobe hid_apple

Check iso_layout working

# echo "1" > /sys/module/hid_apple/parameters/iso_layout

If it is not working I think you should reinstall your linux distribution.

It works now! <> :) Thank you both for your help @free5lot @ozdemir1419, I think this is ready for an upstream fix!

free5lot commented 4 years ago

One question for @free5lot: Will you make the upstream PR or do you want me to handle it? I can report it to the Red Hat bug tracker and the kernel contributors will take it from there.

@daquinons Well, if you have time and desire you're most welcome!

Keep in mind though, that this condition hid->country == HID_COUNTRY_INTERNATIONAL_ISO is there for some reason, I believe it's for ability of the module to determine the layout automatically. It is important as iso_layout is active by default, so this condition affects almost half of users by default.

So, I'm not sure about the way to fix it properly, I was planning to write a letter to Jiri Kosina, maybe the code should be modified in some way to work for all users the way it was but work properly for external Bluetooth keyboards. But if you can solve it from here yourself - please do.

Regardless of issue solution with iso_layout adding another option something like force_iso_layout may be a good idea, because I see no reason why user should not be able to force this key swaps regardless of way the keyboard is recognized by module, the same way all other options work now.

free5lot commented 4 years ago

@daquinons so, will you take this Bluetooth-related issue from here? Your message with this offer disappeared for some reason.

daquinons commented 4 years ago

Yep, but I think is going to take a bit longer because I reported the findings to Fedora kernel contributors (as I'm definitely not capable to make a good-enough PR myself).

free5lot commented 4 years ago

I sent a report about this issue to the up-stream kernel maintainer Jiri Kosina. I put details about possible fix and etc.

free5lot commented 4 years ago

I'm going to close this issue as it's upstream-code related and not about patched version's code. I sent details to Jiri Kosina <jkosina@suse.cz> and consider my part to be done on this matter. I hope @daquinons will follow and pursuit the actual issue till it's fixed.