process_unicodemap.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "process_unicodemap.h"
  2. #include "process_unicode_common.h"
  3. __attribute__((weak))
  4. const uint32_t PROGMEM unicode_map[] = {
  5. };
  6. void register_hex32(uint32_t hex) {
  7. bool onzerostart = true;
  8. for(int i = 7; i >= 0; i--) {
  9. if (i <= 3) {
  10. onzerostart = false;
  11. }
  12. uint8_t digit = ((hex >> (i*4)) & 0xF);
  13. if (digit == 0) {
  14. if (!onzerostart) {
  15. register_code(hex_to_keycode(digit));
  16. unregister_code(hex_to_keycode(digit));
  17. }
  18. } else {
  19. register_code(hex_to_keycode(digit));
  20. unregister_code(hex_to_keycode(digit));
  21. onzerostart = false;
  22. }
  23. }
  24. }
  25. __attribute__((weak))
  26. void unicode_map_input_error() {}
  27. bool process_unicode_map(uint16_t keycode, keyrecord_t *record) {
  28. uint8_t input_mode = get_unicode_input_mode();
  29. if ((keycode & QK_UNICODE_MAP) == QK_UNICODE_MAP && record->event.pressed) {
  30. const uint32_t* map = unicode_map;
  31. uint16_t index = keycode - QK_UNICODE_MAP;
  32. uint32_t code = pgm_read_dword_far(&map[index]);
  33. if (code > 0xFFFF && code <= 0x10ffff && input_mode == UC_OSX) {
  34. // Convert to UTF-16 surrogate pair
  35. code -= 0x10000;
  36. uint32_t lo = code & 0x3ff;
  37. uint32_t hi = (code & 0xffc00) >> 10;
  38. unicode_input_start();
  39. register_hex32(hi + 0xd800);
  40. register_hex32(lo + 0xdc00);
  41. unicode_input_finish();
  42. } else if ((code > 0x10ffff && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
  43. // when character is out of range supported by the OS
  44. unicode_map_input_error();
  45. } else {
  46. unicode_input_start();
  47. register_hex32(code);
  48. unicode_input_finish();
  49. }
  50. }
  51. return true;
  52. }