rverst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright 2021 Robert Verst <robert@verst.eu> @rverst
  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 "rverst.h"
  17. #include "print.h"
  18. #ifdef UNICODEMAP_ENABLE
  19. # include "unicode.h"
  20. #endif
  21. userspace_config_t userspace_config;
  22. uint8_t get_mode(void) {
  23. int m = 0;
  24. if (userspace_config.mode_1) {
  25. m += 1;
  26. }
  27. if (userspace_config.mode_2) {
  28. m += 2;
  29. }
  30. if (userspace_config.mode_3) {
  31. m += 4;
  32. }
  33. return m;
  34. }
  35. void set_mode(uint8_t mode, bool save) {
  36. if (mode == get_mode()) {
  37. return;
  38. }
  39. switch_mode(mode);
  40. if (mode > 7) {
  41. mode = 7;
  42. }
  43. if (mode >= 4) {
  44. userspace_config.mode_3 = true;
  45. mode -= 4;
  46. } else {
  47. userspace_config.mode_3 = false;
  48. }
  49. if (mode >= 2) {
  50. userspace_config.mode_2 = true;
  51. mode -= 2;
  52. } else {
  53. userspace_config.mode_2 = false;
  54. }
  55. if (mode > 0) {
  56. userspace_config.mode_1 = true;
  57. } else {
  58. userspace_config.mode_1 = false;
  59. }
  60. if (save) {
  61. eeconfig_update_user(userspace_config.raw);
  62. }
  63. }
  64. void switch_mode(uint8_t mode) {
  65. #ifdef UNICODEMAP_ENABLE
  66. switch (mode) {
  67. case MAC_UNI:
  68. set_unicode_input_mode(UC_MAC);
  69. break;
  70. case WINDOWS_UNI:
  71. set_unicode_input_mode(UC_WINC);
  72. break;
  73. case LINUX_UNI:
  74. set_unicode_input_mode(UC_LNX);
  75. break;
  76. }
  77. #endif
  78. }
  79. bool is_unicode(uint8_t mode) { return (mode == MAC_UNI) || (mode == WINDOWS_UNI) || (mode == LINUX_UNI); }
  80. //**********************
  81. // keyboard_pre_init
  82. //**********************
  83. __attribute__((weak)) void keyboard_pre_init_keymap(void) {}
  84. void keyboard_pre_init_user(void) {
  85. userspace_config.raw = eeconfig_read_user();
  86. switch_mode(get_mode());
  87. keyboard_pre_init_keymap();
  88. }
  89. //************************
  90. // keyboard_post_init
  91. //************************
  92. __attribute__((weak)) void keyboard_post_init_keymap(void) {}
  93. void keyboard_post_init_user(void) {
  94. // debug_enable = true;
  95. // debug_matrix=true;
  96. // debug_keyboard = true;
  97. #ifdef RGBLIGHT_ENABLE
  98. #endif
  99. keyboard_post_init_keymap();
  100. }
  101. //**********************
  102. // eeconfig_init
  103. //**********************
  104. __attribute__((weak)) void eeconfig_init_keymap(void) {}
  105. void eeconfig_init_user(void) {
  106. userspace_config.raw = 0;
  107. eeconfig_update_user(userspace_config.raw);
  108. eeconfig_init_keymap();
  109. keyboard_init();
  110. }
  111. //**********************
  112. // process_record
  113. //**********************
  114. __attribute__((weak)) bool process_record_keymap(uint16_t keycode, keyrecord_t *record) { return true; }
  115. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  116. if (!process_record_keymap(keycode, record)) {
  117. return false;
  118. }
  119. if (!record->event.pressed) {
  120. return true;
  121. }
  122. bool ls = (get_mods() | get_weak_mods()) & MOD_BIT(KC_LSFT);
  123. bool rs = (get_mods() | get_weak_mods()) & MOD_BIT(KC_RSFT);
  124. bool as = ls || rs;
  125. int mode = get_mode();
  126. switch (keycode) {
  127. case RV_SM0:
  128. case RV_SM0S:
  129. set_mode(MAC_UNI, keycode == RV_SM0S);
  130. return false;
  131. case RV_SM1:
  132. case RV_SM1S:
  133. set_mode(WINDOWS_UNI, keycode == RV_SM1S);
  134. return false;
  135. case RV_SM2:
  136. case RV_SM2S:
  137. set_mode(LINUX_UNI, keycode == RV_SM2S);
  138. return false;
  139. case RV_SM3:
  140. case RV_SM3S:
  141. set_mode(MAC, keycode == RV_SM3S);
  142. return false;
  143. case RV_SM4:
  144. case RV_SM4S:
  145. set_mode(WINDOWS, keycode == RV_SM4S);
  146. return false;
  147. case RV_SAYM:
  148. switch (mode) {
  149. case MAC:
  150. send_string("MacOS (normal)");
  151. break;
  152. case WINDOWS:
  153. send_string("Windows (normal)");
  154. break;
  155. case MAC_UNI:
  156. send_string("MacOS (unicode)");
  157. break;
  158. case WINDOWS_UNI:
  159. send_string("Windows (unicode)");
  160. break;
  161. case LINUX_UNI:
  162. send_string("Linux (unicode)");
  163. break;
  164. }
  165. return false;
  166. // Lock computer
  167. case RV_LOCK:
  168. if (mode == MAC || mode == MAC_UNI) {
  169. tap_code16(G(C(KC_Q)));
  170. } else if (mode == WINDOWS || mode == WINDOWS_UNI) {
  171. tap_code16(G(KC_L));
  172. }
  173. return false;
  174. // Screenshot
  175. case RV_SNAP:
  176. if (mode == MAC || mode == MAC_UNI) {
  177. if (ls) unregister_code(KC_LSFT);
  178. if (rs) unregister_code(KC_RSFT);
  179. tap_code16(G(S(as ? KC_4 : KC_5)));
  180. if (ls) register_code(KC_LSFT);
  181. if (rs) register_code(KC_RSFT);
  182. } else if (mode == WINDOWS || mode == WINDOWS_UNI) {
  183. tap_code16(G(S(KC_S)));
  184. }
  185. return false;
  186. // Umlauts - äÄöÖüÜ
  187. case RV_AUML:
  188. case RV_OUML:
  189. case RV_UUML:
  190. if (is_unicode(mode)) {
  191. if (keycode == RV_AUML) {
  192. if (as)
  193. send_unicode_string("Ä");
  194. else
  195. send_unicode_string("ä");
  196. } else if (keycode == RV_OUML) {
  197. if (as)
  198. send_unicode_string("Ö");
  199. else
  200. send_unicode_string("ö");
  201. } else if (keycode == RV_UUML) {
  202. if (as)
  203. send_unicode_string("Ü");
  204. else
  205. send_unicode_string("ü");
  206. }
  207. } else if (mode == MAC) {
  208. if (ls) unregister_code(KC_LSFT);
  209. if (rs) unregister_code(KC_RSFT);
  210. tap_code16(A(KC_U));
  211. if (as) register_code(KC_LSFT);
  212. if (keycode == RV_AUML) {
  213. tap_code(KC_A);
  214. } else if (keycode == RV_OUML) {
  215. tap_code(KC_O);
  216. } else if (keycode == RV_UUML) {
  217. tap_code(KC_U);
  218. }
  219. if (rs) {
  220. unregister_code(KC_LSFT);
  221. register_code(KC_RSFT);
  222. }
  223. } else if (mode == WINDOWS) {
  224. if (ls) unregister_code(KC_LSFT);
  225. if (rs) unregister_code(KC_RSFT);
  226. register_code(KC_RALT);
  227. tap_code(KC_1);
  228. if (keycode == RV_AUML) {
  229. if (as)
  230. tap_code(KC_4);
  231. else
  232. tap_code(KC_3);
  233. tap_code(KC_2);
  234. } else if (keycode == RV_OUML) {
  235. if (as) {
  236. tap_code(KC_5);
  237. tap_code(KC_3);
  238. } else {
  239. tap_code(KC_4);
  240. tap_code(KC_8);
  241. }
  242. } else if (keycode == RV_UUML) {
  243. if (as) {
  244. tap_code(KC_5);
  245. tap_code(KC_4);
  246. } else {
  247. tap_code(KC_2);
  248. tap_code(KC_9);
  249. }
  250. }
  251. unregister_code(KC_RALT);
  252. if (ls) register_code(KC_LSFT);
  253. if (rs) register_code(KC_RSFT);
  254. }
  255. return false;
  256. // Euro sign - €
  257. // with legacy-mode for MAC and WINDOWS without unicode support
  258. case RV_EUR:
  259. if (is_unicode(mode)) {
  260. send_unicode_string("€");
  261. } else if (mode == MAC) {
  262. tap_code16(S(A(KC_2)));
  263. } else if (mode == WINDOWS) {
  264. register_code(KC_RALT);
  265. tap_code(KC_0);
  266. tap_code(KC_1);
  267. tap_code(KC_2);
  268. tap_code(KC_8);
  269. unregister_code(KC_RALT);
  270. }
  271. return false;
  272. // Sharp-S - ß
  273. // with legacy-mode for MAC and WINDOWS without unicode support
  274. case RV_SZ:
  275. if (is_unicode(mode)) {
  276. if (as) {
  277. send_unicode_string("§");
  278. } else {
  279. send_unicode_string("ß");
  280. }
  281. } else if (mode == MAC) {
  282. tap_code16(A(KC_S));
  283. } else if (mode == WINDOWS) {
  284. register_code(KC_RALT);
  285. tap_code(KC_2);
  286. tap_code(KC_2);
  287. tap_code(KC_5);
  288. unregister_code(KC_RALT);
  289. }
  290. return false;
  291. // Trademark - ™
  292. case RV_TM:
  293. if (is_unicode(mode)) {
  294. send_unicode_string("™");
  295. }
  296. return false;
  297. // Registered trademark - ®
  298. case RV_RT:
  299. if (is_unicode(mode)) {
  300. send_unicode_string("®");
  301. }
  302. return false;
  303. // Copyright - ©
  304. case RV_CC:
  305. if (is_unicode(mode)) {
  306. send_unicode_string("©");
  307. }
  308. return false;
  309. // Degree - °
  310. case RV_DEG:
  311. if (is_unicode(mode)) {
  312. send_unicode_string("°");
  313. }
  314. return false;
  315. // Plus-minus - ±
  316. case RV_PM:
  317. if (is_unicode(mode)) {
  318. send_unicode_string("±");
  319. }
  320. return false;
  321. // Not equal - ≠
  322. case RV_UNEQ:
  323. if (is_unicode(mode)) {
  324. send_unicode_string("≠");
  325. }
  326. return false;
  327. // Superscript one - ¹
  328. case RV_SUP1:
  329. if (is_unicode(mode)) {
  330. send_unicode_string("¹");
  331. }
  332. return false;
  333. // Superscript two - ²
  334. case RV_SUP2:
  335. if (is_unicode(mode)) {
  336. send_unicode_string("²");
  337. }
  338. return false;
  339. // Superscript three - ³
  340. case RV_SUP3:
  341. if (is_unicode(mode)) {
  342. send_unicode_string("³");
  343. }
  344. return false;
  345. }
  346. return true;
  347. }