Skip to content

Commit

Permalink
fix NKRO for keyboards with QMK firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
No0ne committed Dec 12, 2024
1 parent 26ce89d commit 435bc05
Showing 1 changed file with 61 additions and 74 deletions.
135 changes: 61 additions & 74 deletions src/usbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,24 +503,12 @@ void tuh_hid_mount_cb(u8 dev_addr, u8 instance, u8 const* desc_report, u16 desc_
u16 vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);

char* hidprotostr;
switch (hid_if_proto) {
case HID_ITF_PROTOCOL_NONE:
hidprotostr = "NONE";
break;
case HID_ITF_PROTOCOL_KEYBOARD:
hidprotostr = "KEYBOARD";
break;
case HID_ITF_PROTOCOL_MOUSE:
hidprotostr = "MOUSE";
break;
default:
hidprotostr = "UNKNOWN";
break;
};
char* hidprotostr = "none";
if(hid_if_proto == HID_ITF_PROTOCOL_KEYBOARD) hidprotostr = "keyboard";
if(hid_if_proto == HID_ITF_PROTOCOL_MOUSE) hidprotostr = "mouse";

printf("\nHID(%d,%d,%s) mounted\n", dev_addr, instance, hidprotostr);
printf(" ID: %04x:%04x\n", vid, pid);
printf(" VID: %04x PID: %04x\n", vid, pid);

hid_info[instance].report_count = hid_parse_report_descriptor(hid_info[instance].report_info, MAX_REPORT, desc_report, desc_len);
printf(" HID has %u reports\n", hid_info[instance].report_count);
Expand All @@ -539,22 +527,20 @@ void tuh_hid_mount_cb(u8 dev_addr, u8 instance, u8 const* desc_report, u16 desc_
}
printf("\n\n");*/

if(hid_if_proto == HID_ITF_PROTOCOL_KEYBOARD || hid_if_proto == HID_ITF_PROTOCOL_MOUSE) {
if(!tuh_hid_receive_report(dev_addr, instance)) {
printf(" ERROR: Could not register for HID(%d,%d,%s)!\n", dev_addr, instance, hidprotostr);
} else {
printf(" HID(%d,%d,%s) registered for reports\n", dev_addr, instance, hidprotostr);
if(hid_if_proto == HID_ITF_PROTOCOL_KEYBOARD) {
for(u8 i = 0; i < 8; i++) {
if(keyboards[i].dev_addr == 0 && keyboards[i].instance == 0) {
keyboards[i].dev_addr = dev_addr;
keyboards[i].instance = instance;
break;
}
if(!tuh_hid_receive_report(dev_addr, instance)) {
printf(" ERROR: Could not register for HID(%d,%d,%s)!\n", dev_addr, instance, hidprotostr);
} else {
printf(" HID(%d,%d,%s) registered for reports\n", dev_addr, instance, hidprotostr);
if(hid_if_proto == HID_ITF_PROTOCOL_KEYBOARD) {
for(u8 i = 0; i < 8; i++) {
if(keyboards[i].dev_addr == 0 && keyboards[i].instance == 0) {
keyboards[i].dev_addr = dev_addr;
keyboards[i].instance = instance;
break;
}
}
board_led_write(1);
}
board_led_write(1);
}
}

Expand Down Expand Up @@ -591,62 +577,63 @@ void tuh_hid_report_received_cb(u8 dev_addr, u8 instance, u8 const* report, u16

if(!rpt_info) return;

switch(tuh_hid_interface_protocol(dev_addr, instance)) {
case HID_ITF_PROTOCOL_KEYBOARD:
if(tuh_hid_get_protocol(dev_addr, instance) == HID_PROTOCOL_BOOT) {
u8 modifiers = report[0];
report++; report++;
kb_report_receive(modifiers, report, 6);
if(tuh_hid_interface_protocol(dev_addr, instance) == HID_ITF_PROTOCOL_MOUSE) {

} else if(rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP && rpt_info->usage == HID_USAGE_DESKTOP_KEYBOARD) {
if(tuh_hid_get_protocol(dev_addr, instance) == HID_PROTOCOL_BOOT) {
ms_send_movement(report[0], report[1], report[2], report[3]);

u8 modifiers = hid_parse_keyboard_modifiers(rpt_info, report, len);
} else if(rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP && rpt_info->usage == HID_USAGE_DESKTOP_MOUSE) {
ms_setup(rpt_info);
ms_report_receive(report, len);

if(hid_parse_keyboard_is_nkro(rpt_info)) {
u8 current_key = 0;
u8 newreport[sizeof(kb_keys)] = {0};
u8 newindex = 0;
} else {
printf("mouse unknown usage_page: %02x usage: %02x\n", rpt_info->usage_page, rpt_info->usage);
}

for(u8 i = 1; i < len && i < 16; i++) {
for(u8 j = 0; j < 8; j++) {
if(report[i] >> j & 1 && newindex < sizeof(kb_keys)) {
newreport[newindex] = current_key;
newindex++;
}
current_key++;
}
}
} else {

kb_report_receive(modifiers, newreport, sizeof(kb_keys));
if(tuh_hid_get_protocol(dev_addr, instance) == HID_PROTOCOL_BOOT) {
u8 modifiers = report[0];
report++; report++;
kb_report_receive(modifiers, report, 6);

} else if(len == 7) {
report++;
kb_report_receive(modifiers, report, 6);
} else if(rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP && rpt_info->usage == HID_USAGE_DESKTOP_KEYBOARD) {
u8 modifiers = hid_parse_keyboard_modifiers(rpt_info, report, len);

} else if(len == 8 || len == 9) {
report++; report++;
kb_report_receive(modifiers, report, 6);
if(hid_parse_keyboard_is_nkro(rpt_info)) {
u8 current_key = 0;
u8 newreport[sizeof(kb_keys)] = {0};
u8 newindex = 0;

} else {
printf("keyboard unknown len: %02x\n", len);
for(u8 i = 1; i < len && i < 16; i++) {
for(u8 j = 0; j < 8; j++) {
if(report[i] >> j & 1 && newindex < sizeof(kb_keys)) {
newreport[newindex] = current_key;
newindex++;
}
current_key++;
}
}

kb_report_receive(modifiers, newreport, sizeof(kb_keys));

} else if(len == 7) {
report++;
kb_report_receive(modifiers, report, 6);

} else if(len == 8 || len == 9) {
report++; report++;
kb_report_receive(modifiers, report, 6);

} else {
printf("keyboard unknown usage_page: %02x usage: %02x\n", rpt_info->usage_page, rpt_info->usage);
}
tuh_hid_receive_report(dev_addr, instance);
break;

case HID_ITF_PROTOCOL_MOUSE:
if(tuh_hid_get_protocol(dev_addr, instance) == HID_PROTOCOL_BOOT) {
ms_send_movement(report[0], report[1], report[2], report[3]);
} else if(rpt_info->usage_page == HID_USAGE_PAGE_DESKTOP && rpt_info->usage == HID_USAGE_DESKTOP_MOUSE) {
ms_setup(rpt_info);
ms_report_receive(report, len);
} else {
printf("mouse unknown usage_page: %02x usage: %02x\n", rpt_info->usage_page, rpt_info->usage);
printf("keyboard unknown len: %02x\n", len);
}
tuh_hid_receive_report(dev_addr, instance);
break;

} else {
printf("keyboard unknown usage_page: %02x usage: %02x\n", rpt_info->usage_page, rpt_info->usage);
}

}

tuh_hid_receive_report(dev_addr, instance);
}

0 comments on commit 435bc05

Please sign in to comment.