process_unicode.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Copyright 2016 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_unicode.h"
  17. #include "action_util.h"
  18. static uint8_t input_mode;
  19. static uint8_t first_flag = 0;
  20. __attribute__((weak))
  21. uint16_t hex_to_keycode(uint8_t hex)
  22. {
  23. if (hex == 0x0) {
  24. return KC_0;
  25. } else if (hex < 0xA) {
  26. return KC_1 + (hex - 0x1);
  27. } else {
  28. return KC_A + (hex - 0xA);
  29. }
  30. }
  31. void set_unicode_input_mode(uint8_t os_target)
  32. {
  33. input_mode = os_target;
  34. eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
  35. }
  36. uint8_t get_unicode_input_mode(void) {
  37. return input_mode;
  38. }
  39. __attribute__((weak))
  40. void unicode_input_start (void) {
  41. switch(input_mode) {
  42. case UC_OSX:
  43. register_code(KC_LALT);
  44. break;
  45. case UC_LNX:
  46. register_code(KC_LCTL);
  47. register_code(KC_LSFT);
  48. register_code(KC_U);
  49. unregister_code(KC_U);
  50. unregister_code(KC_LSFT);
  51. unregister_code(KC_LCTL);
  52. break;
  53. case UC_WIN:
  54. register_code(KC_LALT);
  55. register_code(KC_PPLS);
  56. unregister_code(KC_PPLS);
  57. break;
  58. case UC_WINC:
  59. register_code(KC_RALT);
  60. unregister_code(KC_RALT);
  61. register_code(KC_U);
  62. unregister_code(KC_U);
  63. }
  64. wait_ms(UNICODE_TYPE_DELAY);
  65. }
  66. __attribute__((weak))
  67. void unicode_input_finish (void) {
  68. switch(input_mode) {
  69. case UC_OSX:
  70. case UC_WIN:
  71. unregister_code(KC_LALT);
  72. break;
  73. case UC_LNX:
  74. register_code(KC_SPC);
  75. unregister_code(KC_SPC);
  76. break;
  77. }
  78. }
  79. void register_hex(uint16_t hex) {
  80. for(int i = 3; i >= 0; i--) {
  81. uint8_t digit = ((hex >> (i*4)) & 0xF);
  82. register_code(hex_to_keycode(digit));
  83. unregister_code(hex_to_keycode(digit));
  84. }
  85. }
  86. bool process_unicode(uint16_t keycode, keyrecord_t *record) {
  87. if (keycode > QK_UNICODE && record->event.pressed) {
  88. if (first_flag == 0) {
  89. set_unicode_input_mode(eeprom_read_byte(EECONFIG_UNICODEMODE));
  90. first_flag = 1;
  91. }
  92. uint16_t unicode = keycode & 0x7FFF;
  93. unicode_input_start();
  94. register_hex(unicode);
  95. unicode_input_finish();
  96. }
  97. return true;
  98. }