From 49f44a2aedce6d727d9e6aa7998e766b899c6eda Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 19 May 2016 16:26:01 +0900 Subject: [PATCH] adb_usb: autodetect ISO keyboards and swap codes --- converter/adb_usb/Makefile | 2 +- converter/adb_usb/keymap_common.h | 53 ++++++++++++++++++++--- converter/adb_usb/keymap_plain.c | 42 ++++++++++++++++++ converter/adb_usb/matrix.c | 72 ++++++++++++++++++++++++++++--- 4 files changed, 156 insertions(+), 13 deletions(-) create mode 100644 converter/adb_usb/keymap_plain.c diff --git a/converter/adb_usb/Makefile b/converter/adb_usb/Makefile index 7b385749..76e10950 100644 --- a/converter/adb_usb/Makefile +++ b/converter/adb_usb/Makefile @@ -56,7 +56,7 @@ SRC = keymap_common.c \ ifdef KEYMAP SRC := keymap_$(KEYMAP).c $(SRC) else - SRC := keymap_ansi.c $(SRC) + SRC := keymap_plain.c $(SRC) endif CONFIG_H = config.h diff --git a/converter/adb_usb/keymap_common.h b/converter/adb_usb/keymap_common.h index bc99ab72..0a917cbc 100644 --- a/converter/adb_usb/keymap_common.h +++ b/converter/adb_usb/keymap_common.h @@ -33,6 +33,49 @@ extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; extern const uint16_t fn_actions[]; +/* Common layout: ANSI+ISO + * ,---. ,---------------. ,---------------. ,---------------. ,-----------. ,---. + * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau| |Pwr| + * `---' `---------------' `---------------' `---------------' `-----------' `---' + * ,-----------------------------------------------------------. ,-----------. ,---------------. + * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa| |Ins|Hom|PgU| |NmL| =| /| *| + * |-----------------------------------------------------------| |-----------| |---------------| + * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \| |Del|End|PgD| | 7| 8| 9| -| + * |-----------------------------------------------------------| `-----------' |---------------| + * |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return | | 4| 5| 6| +| + * |-----------------------------------------------------------| ,---. |---------------| + * |Shif|\ | Z| X| C| V| B| N| M| ,| ,| /|Shift | |Up | | 1| 2| 3| | + * |-----------------------------------------------------------| ,-----------. |-----------|Ent| + * |Ctrl |Opt |Cmd | Space | |Opt |Ctrl | |Lef|Dow|Rig| | 0| .| | + * `-----------------------------------------------------------' `-----------' `---------------' + */ +#define KEYMAP( \ + K35, K7A,K78,K63,K76, K60,K61,K62,K64, K65,K6D,K67,K6F, K69,K6B,K71, K7F, \ + K32,K12,K13,K14,K15,K17,K16,K1A,K1C,K19,K1D,K1B,K18,K33, K72,K73,K74, K47,K51,K4B,K43, \ + K30,K0C,K0D,K0E,K0F,K11,K10,K20,K22,K1F,K23,K21,K1E,K2A, K75,K77,K79, K59,K5B,K5C,K4E, \ + K39,K00,K01,K02,K03,K05,K04,K26,K28,K25,K29,K27, K24, K56,K57,K58,K45, \ + K38,K0A,K06,K07,K08,K09,K0B,K2D,K2E,K2B,K2F,K2C, K7B, K3E, K53,K54,K55, \ + K36,K3A,K37, K31, K7C,K7D, K3B,K3D,K3C, K52, K41,K4C \ +) { \ + { KC_##K00, KC_##K01, KC_##K02, KC_##K03, KC_##K04, KC_##K05, KC_##K06, KC_##K07 }, \ + { KC_##K08, KC_##K09, KC_##K0A, KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_##K0F }, \ + { KC_##K10, KC_##K11, KC_##K12, KC_##K13, KC_##K14, KC_##K15, KC_##K16, KC_##K17 }, \ + { KC_##K18, KC_##K19, KC_##K1A, KC_##K1B, KC_##K1C, KC_##K1D, KC_##K1E, KC_##K1F }, \ + { KC_##K20, KC_##K21, KC_##K22, KC_##K23, KC_##K24, KC_##K25, KC_##K26, KC_##K27 }, \ + { KC_##K28, KC_##K29, KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_##K2F }, \ + { KC_##K30, KC_##K31, KC_##K32, KC_##K33, KC_PENT, KC_##K35, KC_##K36, KC_##K37 }, \ + { KC_##K38, KC_##K39, KC_##K3A, KC_##K3B, KC_##K3C, KC_##K3D, KC_##K3E, KC_NO }, \ + { KC_F17, KC_##K41, KC_NO, KC_##K43, KC_F18, KC_##K45, KC_NO, KC_##K47 }, \ + { KC_NO, KC_NO, KC_NO, KC_##K4B, KC_##K4C, KC_NO, KC_##K4E, KC_F18 }, \ + { KC_F19, KC_##K51, KC_##K52, KC_##K53, KC_##K54, KC_##K55, KC_##K56, KC_##K57 }, \ + { KC_##K58, KC_##K59, KC_F20, KC_##K5B, KC_##K5C, KC_INT3, KC_INT1, KC_PCMM }, \ + { KC_##K60, KC_##K61, KC_##K62, KC_##K63, KC_##K64, KC_##K65, KC_LANG2, KC_##K67 }, \ + { KC_LANG1, KC_##K69, KC_F16, KC_##K6B, KC_NO, KC_##K6D, KC_APP, KC_##K6F }, \ + { KC_NO, KC_##K71, KC_##K72, KC_##K73, KC_##K74, KC_##K75, KC_##K76, KC_##K77 }, \ + { KC_##K78, KC_##K79, KC_##K7A, KC_##K7B, KC_##K7C, KC_##K7D, KC_NO, KC_##K7F } \ +} + + /* M0115 Apple Extended Keyboard ANSI * ,---. ,---------------. ,---------------. ,---------------. ,-----------. ,---. * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau| |Pwr| @@ -93,10 +136,10 @@ extern const uint16_t fn_actions[]; */ #define KEYMAP_EXT_ISO( \ K35, K7A,K78,K63,K76, K60,K61,K62,K64, K65,K6D,K67,K6F, K69,K6B,K71, K7F, \ - K0A,K12,K13,K14,K15,K17,K16,K1A,K1C,K19,K1D,K1B,K18,K33, K72,K73,K74, K47,K51,K4B,K43, \ + K32,K12,K13,K14,K15,K17,K16,K1A,K1C,K19,K1D,K1B,K18,K33, K72,K73,K74, K47,K51,K4B,K43, \ K30,K0C,K0D,K0E,K0F,K11,K10,K20,K22,K1F,K23,K21,K1E,K24, K75,K77,K79, K59,K5B,K5C,K4E, \ K39,K00,K01,K02,K03,K05,K04,K26,K28,K25,K29,K27,K2A, K56,K57,K58,K45, \ - K38,K32,K06,K07,K08,K09,K0B,K2D,K2E,K2B,K2F,K2C, K7B, K3E, K53,K54,K55, \ + K38,K0A,K06,K07,K08,K09,K0B,K2D,K2E,K2B,K2F,K2C, K7B, K3E, K53,K54,K55, \ K36,K3A,K37, K31, K7C,K7D, K3B,K3D,K3C, K52, K41,K4C \ ) { \ { KC_##K00, KC_##K01, KC_##K02, KC_##K03, KC_##K04, KC_##K05, KC_##K06, KC_##K07 }, \ @@ -183,16 +226,16 @@ extern const uint16_t fn_actions[]; K35,K12,K13,K14,K15,K17,K16,K1A,K1C,K19,K1D,K1B,K18,K33, K47,K51,K4B,K43, \ K30,K0C,K0D,K0E,K0F,K11,K10,K20,K22,K1F,K23,K21,K1E, K59,K5B,K5C,K45, \ K39,K00,K01,K02,K03,K05,K04,K26,K28,K25,K29,K27,K2A,K24, K56,K57,K58,K4E, \ - K38,K32,K06,K07,K08,K09,K0B,K2D,K2E,K2B,K2F,K2C,K7B,K3E, K53,K54,K55, \ + K38,K0A,K06,K07,K08,K09,K0B,K2D,K2E,K2B,K2F,K2C,K7B,K3E, K53,K54,K55, \ K36,K3A,K37, K31, K3B,K3C,K3D, K52, K41,K4C \ ) { \ { KC_##K00, KC_##K01, KC_##K02, KC_##K03, KC_##K04, KC_##K05, KC_##K06, KC_##K07 }, \ - { KC_##K08, KC_##K09, KC_NO, KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_##K0F }, \ + { KC_##K08, KC_##K09, KC_##K0A KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_##K0F }, \ { KC_##K10, KC_##K11, KC_##K12, KC_##K13, KC_##K14, KC_##K15, KC_##K16, KC_##K17 }, \ { KC_##K18, KC_##K19, KC_##K1A, KC_##K1B, KC_##K1C, KC_##K1D, KC_##K1E, KC_##K1F }, \ { KC_##K20, KC_##K21, KC_##K22, KC_##K23, KC_##K24, KC_##K25, KC_##K26, KC_##K27 }, \ { KC_##K28, KC_##K29, KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_##K2F }, \ - { KC_##K30, KC_##K31, KC_##K32, KC_##K33, KC_NO, KC_##K35, KC_##K36, KC_##K37 }, \ + { KC_##K30, KC_##K31, KC_NO, KC_##K33, KC_NO, KC_##K35, KC_##K36, KC_##K37 }, \ { KC_##K38, KC_##K39, KC_##K3A, KC_##K3B, KC_##K3C, KC_##K3D, KC_##K3E, KC_NO }, \ { KC_NO, KC_##K41, KC_NO, KC_##K43, KC_NO, KC_##K45, KC_NO, KC_##K47 }, \ { KC_NO, KC_NO, KC_NO, KC_##K4B, KC_##K4C, KC_NO, KC_##K4E, KC_NO }, \ diff --git a/converter/adb_usb/keymap_plain.c b/converter/adb_usb/keymap_plain.c new file mode 100644 index 00000000..433a5cb3 --- /dev/null +++ b/converter/adb_usb/keymap_plain.c @@ -0,0 +1,42 @@ +#include "keymap_common.h" + + +/* + * ,---. ,---------------. ,---------------. ,---------------. ,-----------. ,---. + * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau| |Pwr| + * `---' `---------------' `---------------' `---------------' `-----------' `---' + * ,-----------------------------------------------------------. ,-----------. ,---------------. + * | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backspa| |Ins|Hom|PgU| |NmL| =| /| *| + * |-----------------------------------------------------------| |-----------| |---------------| + * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \| |Del|End|PgD| | 7| 8| 9| -| + * |-----------------------------------------------------------| `-----------' |---------------| + * |CapsLo| A| S| D| F| G| H| J| K| L| ;| '|Return | | 4| 5| 6| +| + * |-----------------------------------------------------------| ,---. |---------------| + * |Shif|\ | Z| X| C| V| B| N| M| ,| ,| /|Shift | |Up | | 1| 2| 3| | + * |-----------------------------------------------------------| ,-----------. |-----------|Ent| + * |Ctrl |Opt |Cmd | Space | |Opt |Ctrl | |Lef|Dow|Rig| | 0| .| | + * `-----------------------------------------------------------' `-----------' `---------------' + */ +const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + [0] = KEYMAP( + ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,PAUS, NO, + FN0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSPC, INS, HOME,PGUP, NLCK,EQL, PSLS,PAST, + TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,FN1, DEL, END, PGDN, P7, P8, P9, PMNS, + LCAP,A, S, D, F, G, H, J, K, L, SCLN,QUOT, ENT, P4, P5, P6, PPLS, + LSFT,NUBS,Z, X, C, V, B, N, M, COMM,DOT, SLSH, RSFT, UP, P1, P2, P3, + LCTL,LALT,LGUI, SPC, RALT,RCTL, LEFT,DOWN,RGHT, P0, PDOT,PENT + ), + [1] = KEYMAP( + GRV, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR,SLCK,BRK, NO, + FN0, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, DEL, INS, HOME,PGUP, NLCK,BTN1,BTN2,BTN3, + TAB, Q, W, E, R, T, Y, U, PSCR,SLCK,PAUS,UP, INS, FN1, DEL, END, PGDN, WH_D,MS_U,WH_U,WH_D, + LCAP,VOLD,VOLU,MUTE,F, G, H, J, HOME,PGUP,LEFT,RGHT, ENT, MS_L,MS_D,MS_R,WH_U, + LSFT,NUBS,Z, X, C, V, B, N, M, END, PGDN,DOWN, RSFT, PGUP, WH_L,MS_D,WH_R, + LCTL,LGUI,LALT, SPC, RGUI,RCTL, HOME,PGDN,END, BTN1, BTN2,BTN3 + ), +}; + +const uint16_t PROGMEM fn_actions[] = { + [0] = ACTION_LAYER_TAP_KEY(1, KC_GRV), + [1] = ACTION_LAYER_TAP_KEY(1, KC_BSLS), +}; diff --git a/converter/adb_usb/matrix.c b/converter/adb_usb/matrix.c index dad71758..6857e692 100644 --- a/converter/adb_usb/matrix.c +++ b/converter/adb_usb/matrix.c @@ -39,6 +39,7 @@ along with this program. If not, see . #endif +static bool is_iso_layout = false; static bool is_modified = false; static report_mouse_t mouse_report = {}; @@ -72,6 +73,21 @@ void matrix_init(void) adb_host_init(); // wait for keyboard to boot up and receive command _delay_ms(1000); + + // Determine ISO keyboard by handle id + // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815 + uint16_t handle_id = adb_host_talk(ADB_ADDR_KEYBOARD, 3); + switch (handle_id) { + case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D: + case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1: + case 0xC4: case 0xC7: + is_iso_layout = true; + break; + default: + is_iso_layout = false; + break; + } + // Enable keyboard left/right modifier distinction // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11) // upper byte: reserved bits 0000, device address 0010 @@ -92,6 +108,9 @@ void matrix_init(void) _delay_ms(500); DDRD |= (1<<6); PORTD &= ~(1<<6); + uint16_t handle_id2 = adb_host_talk(ADB_ADDR_KEYBOARD, 3); + xprintf("handle_id: %02X -> %02X\n", handle_id&0xff, handle_id2&0xff); + return; } @@ -106,10 +125,10 @@ void adb_mouse_task(void) { uint16_t codes; int16_t x, y; - static int8_t mouseacc; + static int8_t mouseacc; _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller codes = adb_host_mouse_recv(); - // If nothing received reset mouse acceleration, and quit. + // If nothing received reset mouse acceleration, and quit. if (!codes) { mouseacc = 1; return; @@ -119,12 +138,12 @@ void adb_mouse_task(void) mouse_report.buttons |= MOUSE_BTN1; if (codes & (1 << 15)) mouse_report.buttons &= ~MOUSE_BTN1; - // lower seven bits are movement, as signed int_7. - // low byte is X-axis, high byte is Y. + // lower seven bits are movement, as signed int_7. + // low byte is X-axis, high byte is Y. y = (codes>>8 & 0x3F); x = (codes>>0 & 0x3F); // bit seven and fifteen is negative - // usb does not use int_8, but int_7 (measuring distance) with sign-bit. + // usb does not use int_8, but int_7 (measuring distance) with sign-bit. if (codes & (1 << 6)) x = (x-0x40); if (codes & (1 << 14)) @@ -132,7 +151,7 @@ void adb_mouse_task(void) // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200). x *= mouseacc; y *= mouseacc; - // Cap our two bytes per axis to one byte. + // Cap our two bytes per axis to one byte. // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)... // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127) mouse_report.x = -MAX(-MAX(x, -127), -127); @@ -145,7 +164,7 @@ void adb_mouse_task(void) print_decs(mouse_report.x); print(" "); print_decs(mouse_report.y); print("]\n"); } - // Send result by usb. + // Send result by usb. host_mouse_send(&mouse_report); // increase acceleration of mouse mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 ); @@ -192,6 +211,45 @@ uint8_t matrix_scan(void) xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes); return key1; } else { + /* Swap codes for ISO keyboard + * + * ANSI + * ,----------- ----------. + * | *a| 1| 2 =|Backspa| + * |----------- ----------| + * |Tab | Q| | ]| *c| + * |----------- ----------| + * |CapsLo| A| '|Return | + * |----------- ----------| + * |Shift | Shift | + * `----------- ----------' + * + * ISO + * ,----------- ----------. + * | *a| 1| 2 =|Backspa| + * |----------- ----------| + * |Tab | Q| | ]|Retur| + * |----------- -----` | + * |CapsLo| A| '| *c| | + * |----------- ----------| + * |Shif| *b| Shift | + * `----------- ----------' + * + * ADB scan code USB usage + * ------------- --------- + * Key ANSI ISO ANSI ISO + * --------------------------------------------- + * *a 0x32 0x0A 0x35 0x35 + * *b ---- 0x32 ---- 0x64 + * *c 0x2A 0x2A 0x31 0x31(or 0x32) + */ + if (is_iso_layout) { + if (key0 == 0x32) { + key0 = 0x0A; + } else if (key0 == 0x0A) { + key0 = 0x32; + } + } register_key(key0); if (key1 != 0xFF) // key1 is 0xFF when no second key. extra_key = key1<<8 | 0xFF; // process in a separate call -- 2.39.3