@@ -59,6 +59,12 @@ MODULE_PARM_DESC(swap_opt_cmd, "Swap the Option (\"Alt\") and Command (\"Flag\")
5959 "(For people who want to keep Windows PC keyboard muscle memory. "
6060 "[0] = as-is, Mac layout. 1 = swapped, Windows layout.)" );
6161
62+ static unsigned int swap_ctrl_cmd ;
63+ module_param (swap_ctrl_cmd , uint , 0644 );
64+ MODULE_PARM_DESC (swap_ctrl_cmd , "Swap the Control (\"Ctrl\") and Command (\"Flag\") keys. "
65+ "(For people who are used to Mac shortcuts involving Command instead of Control. "
66+ "[0] = No change. 1 = Swapped.)" );
67+
6268static unsigned int swap_fn_leftctrl ;
6369module_param (swap_fn_leftctrl , uint , 0644 );
6470MODULE_PARM_DESC (swap_fn_leftctrl , "Swap the Fn and left Control keys. "
@@ -308,12 +314,21 @@ static const struct apple_key_translation swapped_option_cmd_keys[] = {
308314 { KEY_LEFTALT , KEY_LEFTMETA },
309315 { KEY_LEFTMETA , KEY_LEFTALT },
310316 { KEY_RIGHTALT , KEY_RIGHTMETA },
311- { KEY_RIGHTMETA ,KEY_RIGHTALT },
317+ { KEY_RIGHTMETA , KEY_RIGHTALT },
318+ { }
319+ };
320+
321+ static const struct apple_key_translation swapped_ctrl_cmd_keys [] = {
322+ { KEY_LEFTCTRL , KEY_LEFTMETA },
323+ { KEY_LEFTMETA , KEY_LEFTCTRL },
324+ { KEY_RIGHTCTRL , KEY_RIGHTMETA },
325+ { KEY_RIGHTMETA , KEY_RIGHTCTRL },
312326 { }
313327};
314328
315329static const struct apple_key_translation swapped_fn_leftctrl_keys [] = {
316330 { KEY_FN , KEY_LEFTCTRL },
331+ { KEY_LEFTCTRL , KEY_FN },
317332 { }
318333};
319334
@@ -375,24 +390,47 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
375390 struct apple_sc * asc = hid_get_drvdata (hid );
376391 const struct apple_key_translation * trans , * table ;
377392 bool do_translate ;
378- u16 code = 0 ;
393+ u16 code = usage -> code ;
379394 unsigned int real_fnmode ;
380395
381- u16 fn_keycode = (swap_fn_leftctrl ) ? (KEY_LEFTCTRL ) : (KEY_FN );
382-
383- if (usage -> code == fn_keycode ) {
384- asc -> fn_on = !!value ;
385- input_event_with_scancode (input , usage -> type , KEY_FN ,
386- usage -> hid , value );
387- return 1 ;
388- }
389-
390396 if (fnmode == 3 ) {
391397 real_fnmode = (asc -> quirks & APPLE_IS_NON_APPLE ) ? 2 : 1 ;
392398 } else {
393399 real_fnmode = fnmode ;
394400 }
395401
402+ if (swap_fn_leftctrl ) {
403+ trans = apple_find_translation (swapped_fn_leftctrl_keys , code );
404+
405+ if (trans )
406+ code = trans -> to ;
407+ }
408+
409+ if (iso_layout > 0 || (iso_layout < 0 && (asc -> quirks & APPLE_ISO_TILDE_QUIRK ) &&
410+ hid -> country == HID_COUNTRY_INTERNATIONAL_ISO )) {
411+ trans = apple_find_translation (apple_iso_keyboard , code );
412+
413+ if (trans )
414+ code = trans -> to ;
415+ }
416+
417+ if (swap_opt_cmd ) {
418+ trans = apple_find_translation (swapped_option_cmd_keys , code );
419+
420+ if (trans )
421+ code = trans -> to ;
422+ }
423+
424+ if (swap_ctrl_cmd ) {
425+ trans = apple_find_translation (swapped_ctrl_cmd_keys , code );
426+
427+ if (trans )
428+ code = trans -> to ;
429+ }
430+
431+ if (code == KEY_FN )
432+ asc -> fn_on = !!value ;
433+
396434 if (real_fnmode ) {
397435 if (hid -> product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI ||
398436 hid -> product == USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO ||
@@ -430,15 +468,18 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
430468 else
431469 table = apple_fn_keys ;
432470
433- trans = apple_find_translation (table , usage -> code );
471+ trans = apple_find_translation (table , code );
434472
435473 if (trans ) {
436- if (test_bit (trans -> from , input -> key ))
474+ bool from_is_set = test_bit (trans -> from , input -> key );
475+ bool to_is_set = test_bit (trans -> to , input -> key );
476+
477+ if (from_is_set )
437478 code = trans -> from ;
438- else if (test_bit ( trans -> to , input -> key ) )
479+ else if (to_is_set )
439480 code = trans -> to ;
440481
441- if (!code ) {
482+ if (!( from_is_set || to_is_set ) ) {
442483 if (trans -> flags & APPLE_FLAG_FKEY ) {
443484 switch (real_fnmode ) {
444485 case 1 :
@@ -455,62 +496,31 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
455496 do_translate = asc -> fn_on ;
456497 }
457498
458- code = do_translate ? trans -> to : trans -> from ;
499+ if (do_translate )
500+ code = trans -> to ;
459501 }
460-
461- input_event_with_scancode (input , usage -> type , code ,
462- usage -> hid , value );
463- return 1 ;
464502 }
465503
466504 if (asc -> quirks & APPLE_NUMLOCK_EMULATION &&
467- (test_bit (usage -> code , asc -> pressed_numlock ) ||
505+ (test_bit (code , asc -> pressed_numlock ) ||
468506 test_bit (LED_NUML , input -> led ))) {
469- trans = apple_find_translation (powerbook_numlock_keys ,
470- usage -> code );
507+ trans = apple_find_translation (powerbook_numlock_keys , code );
471508
472509 if (trans ) {
473510 if (value )
474- set_bit (usage -> code ,
475- asc -> pressed_numlock );
511+ set_bit (code , asc -> pressed_numlock );
476512 else
477- clear_bit (usage -> code ,
478- asc -> pressed_numlock );
513+ clear_bit (code , asc -> pressed_numlock );
479514
480- input_event_with_scancode (input , usage -> type ,
481- trans -> to , usage -> hid , value );
515+ code = trans -> to ;
482516 }
483-
484- return 1 ;
485- }
486- }
487-
488- if (iso_layout > 0 || (iso_layout < 0 && (asc -> quirks & APPLE_ISO_TILDE_QUIRK ) &&
489- hid -> country == HID_COUNTRY_INTERNATIONAL_ISO )) {
490- trans = apple_find_translation (apple_iso_keyboard , usage -> code );
491- if (trans ) {
492- input_event_with_scancode (input , usage -> type ,
493- trans -> to , usage -> hid , value );
494- return 1 ;
495517 }
496518 }
497519
498- if (swap_opt_cmd ) {
499- trans = apple_find_translation (swapped_option_cmd_keys , usage -> code );
500- if (trans ) {
501- input_event_with_scancode (input , usage -> type ,
502- trans -> to , usage -> hid , value );
503- return 1 ;
504- }
505- }
520+ if (usage -> code != code ) {
521+ input_event_with_scancode (input , usage -> type , code , usage -> hid , value );
506522
507- if (swap_fn_leftctrl ) {
508- trans = apple_find_translation (swapped_fn_leftctrl_keys , usage -> code );
509- if (trans ) {
510- input_event_with_scancode (input , usage -> type ,
511- trans -> to , usage -> hid , value );
512- return 1 ;
513- }
523+ return 1 ;
514524 }
515525
516526 return 0 ;
@@ -640,9 +650,6 @@ static void apple_setup_input(struct input_dev *input)
640650 apple_setup_key_translation (input , apple2021_fn_keys );
641651 apple_setup_key_translation (input , macbookpro_no_esc_fn_keys );
642652 apple_setup_key_translation (input , macbookpro_dedicated_esc_fn_keys );
643-
644- if (swap_fn_leftctrl )
645- apple_setup_key_translation (input , swapped_fn_leftctrl_keys );
646653}
647654
648655static int apple_input_mapping (struct hid_device * hdev , struct hid_input * hi ,
@@ -1011,21 +1018,21 @@ static const struct hid_device_id apple_devices[] = {
10111018 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRING9_JIS ),
10121019 .driver_data = APPLE_HAS_FN | APPLE_RDESC_JIS },
10131020 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K ),
1014- .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
1021+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
10151022 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132 ),
1016- .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
1023+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
10171024 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680 ),
1018- .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
1025+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
10191026 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213 ),
1020- .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL },
1027+ .driver_data = APPLE_HAS_FN | APPLE_BACKLIGHT_CTL | APPLE_ISO_TILDE_QUIRK },
10211028 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K ),
1022- .driver_data = APPLE_HAS_FN },
1029+ .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
10231030 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223 ),
1024- .driver_data = APPLE_HAS_FN },
1031+ .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
10251032 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K ),
1026- .driver_data = APPLE_HAS_FN },
1033+ .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
10271034 { HID_USB_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F ),
1028- .driver_data = APPLE_HAS_FN },
1035+ .driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
10291036 { HID_BLUETOOTH_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI ),
10301037 .driver_data = APPLE_NUMLOCK_EMULATION | APPLE_HAS_FN },
10311038 { HID_BLUETOOTH_DEVICE (USB_VENDOR_ID_APPLE , USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO ),
0 commit comments