process_unicodemap.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Copyright 2017 Jack Humbert
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "process_unicodemap.h"
  17. #include "process_unicode_common.h"
  18. void register_hex32(uint32_t hex) {
  19. bool onzerostart = true;
  20. for(int i = 7; i >= 0; i--) {
  21. if (i <= 3) {
  22. onzerostart = false;
  23. }
  24. uint8_t digit = ((hex >> (i*4)) & 0xF);
  25. if (digit == 0) {
  26. if (!onzerostart) {
  27. register_code(hex_to_keycode(digit));
  28. unregister_code(hex_to_keycode(digit));
  29. }
  30. } else {
  31. register_code(hex_to_keycode(digit));
  32. unregister_code(hex_to_keycode(digit));
  33. onzerostart = false;
  34. }
  35. }
  36. }
  37. __attribute__((weak))
  38. void unicodemap_input_error() {}
  39. bool process_unicodemap(uint16_t keycode, keyrecord_t *record) {
  40. if ((keycode & QK_UNICODEMAP) == QK_UNICODEMAP && record->event.pressed) {
  41. uint16_t index = keycode - QK_UNICODEMAP;
  42. uint32_t code = pgm_read_dword(unicode_map + index);
  43. uint8_t input_mode = get_unicode_input_mode();
  44. if (code > 0xFFFF && code <= 0x10FFFF && input_mode == UC_OSX) {
  45. // Convert to UTF-16 surrogate pair
  46. code -= 0x10000;
  47. uint32_t lo = code & 0x3FF, hi = (code & 0xFFC00) >> 10;
  48. unicode_input_start();
  49. register_hex32(hi + 0xD800);
  50. register_hex32(lo + 0xDC00);
  51. unicode_input_finish();
  52. } else if ((code > 0x10FFFF && input_mode == UC_OSX) || (code > 0xFFFFF && input_mode == UC_LNX)) {
  53. // Character is out of range supported by the OS
  54. unicodemap_input_error();
  55. } else {
  56. unicode_input_start();
  57. register_hex32(code);
  58. unicode_input_finish();
  59. }
  60. }
  61. return true;
  62. }