process_unicode_common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_unicode_common.h"
  17. static uint8_t input_mode;
  18. uint8_t mods;
  19. void set_unicode_input_mode(uint8_t os_target)
  20. {
  21. input_mode = os_target;
  22. eeprom_update_byte(EECONFIG_UNICODEMODE, os_target);
  23. }
  24. uint8_t get_unicode_input_mode(void) {
  25. return input_mode;
  26. }
  27. __attribute__((weak))
  28. void unicode_input_start (void) {
  29. // save current mods
  30. mods = keyboard_report->mods;
  31. // unregister all mods to start from clean state
  32. if (mods & MOD_BIT(KC_LSFT)) unregister_code(KC_LSFT);
  33. if (mods & MOD_BIT(KC_RSFT)) unregister_code(KC_RSFT);
  34. if (mods & MOD_BIT(KC_LCTL)) unregister_code(KC_LCTL);
  35. if (mods & MOD_BIT(KC_RCTL)) unregister_code(KC_RCTL);
  36. if (mods & MOD_BIT(KC_LALT)) unregister_code(KC_LALT);
  37. if (mods & MOD_BIT(KC_RALT)) unregister_code(KC_RALT);
  38. if (mods & MOD_BIT(KC_LGUI)) unregister_code(KC_LGUI);
  39. if (mods & MOD_BIT(KC_RGUI)) unregister_code(KC_RGUI);
  40. switch(input_mode) {
  41. case UC_OSX:
  42. register_code(KC_LALT);
  43. break;
  44. case UC_LNX:
  45. register_code(KC_LCTL);
  46. register_code(KC_LSFT);
  47. register_code(KC_U);
  48. unregister_code(KC_U);
  49. unregister_code(KC_LSFT);
  50. unregister_code(KC_LCTL);
  51. break;
  52. case UC_WIN:
  53. register_code(KC_LALT);
  54. register_code(KC_PPLS);
  55. unregister_code(KC_PPLS);
  56. break;
  57. case UC_WINC:
  58. register_code(KC_RALT);
  59. unregister_code(KC_RALT);
  60. register_code(KC_U);
  61. unregister_code(KC_U);
  62. }
  63. wait_ms(UNICODE_TYPE_DELAY);
  64. }
  65. __attribute__((weak))
  66. void unicode_input_finish (void) {
  67. switch(input_mode) {
  68. case UC_OSX:
  69. case UC_WIN:
  70. unregister_code(KC_LALT);
  71. break;
  72. case UC_LNX:
  73. register_code(KC_SPC);
  74. unregister_code(KC_SPC);
  75. break;
  76. }
  77. // reregister previously set mods
  78. if (mods & MOD_BIT(KC_LSFT)) register_code(KC_LSFT);
  79. if (mods & MOD_BIT(KC_RSFT)) register_code(KC_RSFT);
  80. if (mods & MOD_BIT(KC_LCTL)) register_code(KC_LCTL);
  81. if (mods & MOD_BIT(KC_RCTL)) register_code(KC_RCTL);
  82. if (mods & MOD_BIT(KC_LALT)) register_code(KC_LALT);
  83. if (mods & MOD_BIT(KC_RALT)) register_code(KC_RALT);
  84. if (mods & MOD_BIT(KC_LGUI)) register_code(KC_LGUI);
  85. if (mods & MOD_BIT(KC_RGUI)) register_code(KC_RGUI);
  86. }
  87. __attribute__((weak))
  88. uint16_t hex_to_keycode(uint8_t hex)
  89. {
  90. if (hex == 0x0) {
  91. return KC_0;
  92. } else if (hex < 0xA) {
  93. return KC_1 + (hex - 0x1);
  94. } else {
  95. return KC_A + (hex - 0xA);
  96. }
  97. }
  98. void register_hex(uint16_t hex) {
  99. for(int i = 3; i >= 0; i--) {
  100. uint8_t digit = ((hex >> (i*4)) & 0xF);
  101. register_code(hex_to_keycode(digit));
  102. unregister_code(hex_to_keycode(digit));
  103. }
  104. }