You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

510 lines
9.4 KiB

  1. #include "relativity.h"
  2. #include "keymap.h"
  3. #include "keyDefinitions.h"
  4. uint16_t *macroTaps = 0;
  5. char *tableNameList = 0;
  6. uint8_t *charCount = 0;
  7. uint8_t countPointer = 0;
  8. bool relativityActive = false;
  9. bool sendAbbr = false;
  10. static int16_t relativityTimer = 0;
  11. bool tempOff = false;
  12. void initStringData()
  13. {
  14. if (macroTaps == 0)
  15. {
  16. macroTaps = malloc(macroTapsLen*sizeof(uint16_t));
  17. for(int i = 0; i < macroTapsLen; i++)
  18. {
  19. macroTaps[i] = 0;
  20. }
  21. }
  22. if (tableNameList == 0)
  23. {
  24. tableNameList = malloc(tableNameListLen*sizeof(char));
  25. for(int i = 0; i < tableNameListLen; i++)
  26. {
  27. tableNameList[i] = 0;
  28. }
  29. }
  30. if (charCount == 0)
  31. {
  32. charCount = malloc(charCountLen*sizeof(uint8_t));
  33. for (int i = 0; i < charCountLen; i++)
  34. {
  35. charCount[i] = 0;
  36. }
  37. }
  38. }
  39. void activateRelativity(void)
  40. {
  41. initStringData();
  42. rgblight_mode(RGBLIGHT_MODE_KNIGHT);
  43. relativityTimer = timer_read();
  44. relativityActive = true;
  45. }
  46. bool deactivateRelativity(void)
  47. {
  48. rgblight_mode(9);
  49. eraseKeyCodes();
  50. eraseTableAbbreviation();
  51. eraseCharCounts();
  52. relativityActive = false;
  53. tempOff = false;
  54. return false;
  55. }
  56. bool containsCode(uint16_t kc)
  57. {
  58. for (int i = 0; i < macroTapsLen && macroTaps[i] > 0; i++)
  59. {
  60. if (macroTaps[i] == kc) return true;
  61. }
  62. return false;
  63. }
  64. bool lastCodeIs(uint16_t kc)
  65. {
  66. for (int i = 0; i < macroTapsLen-1 && macroTaps[i] > 0; i++)
  67. {
  68. if (macroTaps[i] == kc && macroTaps[i+1] == 0) return true;
  69. }
  70. return false;
  71. }
  72. bool last2CodeAre(uint16_t kc)
  73. {
  74. for (int i = 0; i < macroTapsLen-2 && macroTaps[i] > 0; i++)
  75. {
  76. if (macroTaps[i] == kc && macroTaps[i+1] == kc && macroTaps[i+2] == 0) return true;
  77. }
  78. return false;
  79. }
  80. bool last2CodesAre(uint16_t kc, uint16_t kc2)
  81. {
  82. for (int i = 0; i < macroTapsLen-2 && macroTaps[i] > 0; i++)
  83. {
  84. if (macroTaps[i] == kc && macroTaps[i+1] == kc2 && macroTaps[i+2] == 0) return true;
  85. }
  86. return false;
  87. }
  88. void addKeyCode(uint16_t kc)
  89. {
  90. int i = 0;
  91. while (i < macroTapsLen-2 && macroTaps[i] > 0) i++;
  92. if (macroTaps[i] == 0)
  93. {
  94. macroTaps[i] = kc;
  95. macroTaps[i+1] = 0;
  96. }
  97. }
  98. void eraseKeyCodes(void)
  99. {
  100. int i = 0;
  101. while (i < macroTapsLen && macroTaps[i] > 0) macroTaps[i++] = 0;
  102. }
  103. void eraseCharCounts(void)
  104. {
  105. while (countPointer > 0)
  106. {
  107. charCount[countPointer] = 0;
  108. countPointer--;
  109. }
  110. charCount[countPointer] = 0;
  111. }
  112. void printTableAbbreviationLimited(void)
  113. {
  114. if (tableNameList[0] == 0)
  115. {
  116. return;
  117. }
  118. int i = 0;
  119. for (i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
  120. {
  121. if (tableNameList[i] >= 65 && tableNameList[i] <= 90)
  122. {
  123. send_char(tableNameList[i]+32);
  124. }
  125. else
  126. {
  127. send_char(tableNameList[i]);
  128. }
  129. }
  130. }
  131. void printTableAbbreviation(void)
  132. {
  133. if (tableNameList[0] == 0)
  134. {
  135. return;
  136. }
  137. send_char(0x20);
  138. int i = 0;
  139. for (i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
  140. {
  141. if (tableNameList[i] >= 65 && tableNameList[i] <= 90)
  142. {
  143. send_char(tableNameList[i]+32);
  144. }
  145. else
  146. {
  147. send_char(tableNameList[i]);
  148. }
  149. }
  150. send_char(0x20);
  151. }
  152. void eraseTableAbbreviation(void)
  153. {
  154. for (int i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
  155. {
  156. tableNameList[i] = '\0';
  157. }
  158. }
  159. void printString(char* str)
  160. {
  161. if (str[0] != '\0')
  162. {
  163. int i = 0;
  164. while (true)
  165. {
  166. if (str[i] == 0)
  167. {
  168. break;
  169. }
  170. send_char(str[i++]);
  171. charCount[countPointer]++;
  172. }
  173. }
  174. }
  175. void printStringAndQueueChar(char* str)
  176. {
  177. if (charCount[countPointer] > 0 && countPointer < charCountLen)
  178. {
  179. countPointer++;
  180. }
  181. sendAbbr = true;
  182. if (str[0] != '\0')
  183. {
  184. printString(str);
  185. for (int i = 0; i < tableNameListLen-1; i++)
  186. {
  187. if (tableNameList[i] == '\0')
  188. {
  189. tableNameList[i] = str[0];
  190. tableNameList[i+1] = '\0';
  191. break;
  192. }
  193. else if (i == tableNameListLen-2)
  194. {
  195. printTableAbbreviation();
  196. break;
  197. }
  198. }
  199. //for (i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
  200. //{
  201. // send_char(tableNameList[i]);
  202. //}
  203. //send_string_P("Darden");
  204. //send_string_P(&myarray);
  205. //send_string_P(str);
  206. }
  207. }
  208. void ReplaceString(char *orig, char *repl)
  209. {
  210. int i = 0;
  211. while((orig[i] != 0x0 && repl[i] != 0x0) && orig[i] == repl[i])
  212. i++;
  213. if(orig[i] != 0x0)
  214. {
  215. int o = i;
  216. while (orig[o++] != 0x0) {
  217. charCount[countPointer]--;
  218. register_code(KC_BSPC);
  219. unregister_code(KC_BSPC);
  220. }
  221. }
  222. printString(repl+i);
  223. }
  224. void deletePrev(void)
  225. {
  226. if (countPointer == 0 && charCount[countPointer] == 0)
  227. return;
  228. for (int i = 0; i < charCount[countPointer]; i++)
  229. {
  230. register_code(KC_BSPC);
  231. unregister_code(KC_BSPC);
  232. }
  233. charCount[countPointer] = 0;
  234. int i = 1;
  235. for (;i < tableNameListLen-1; i++)
  236. {
  237. if (tableNameList[i] == 0x0)
  238. {
  239. break;
  240. }
  241. }
  242. tableNameList[i-1] = 0x0;
  243. if (countPointer > 0)
  244. {
  245. countPointer--;
  246. }
  247. }
  248. bool processSmartMacroTap(uint16_t kc)
  249. {
  250. if (relativityTimer > 0 && TIMER_DIFF_16(timer_read(), relativityTimer) >= relTimeout)
  251. {
  252. deactivateRelativity();
  253. return true;
  254. }
  255. relativityTimer = 0;
  256. switch(kc)
  257. {
  258. case KC_C:
  259. if (containsCode(KC_D))
  260. {
  261. printString("ribution");
  262. printStringAndQueueChar("Center");
  263. }
  264. else if (last2CodeAre(KC_C))
  265. {
  266. ReplaceString("Corporation", "Contact");
  267. }
  268. else if(lastCodeIs(KC_C))
  269. {
  270. printString("oration");
  271. }
  272. else
  273. {
  274. printStringAndQueueChar("Corp");
  275. }
  276. break;
  277. case KC_D:
  278. if (last2CodeAre(KC_D))
  279. {
  280. ReplaceString("Distribution", "Distributor");
  281. }
  282. else if(lastCodeIs(KC_D))
  283. {
  284. printString("ribution");
  285. }
  286. else
  287. {
  288. printStringAndQueueChar("Dist");
  289. }
  290. break;
  291. case KC_G:
  292. printStringAndQueueChar("Global");
  293. printStringAndQueueChar("Lookup");
  294. break;
  295. case KC_I:
  296. if (containsCode(KC_W))
  297. printStringAndQueueChar("Instance");
  298. else
  299. printStringAndQueueChar("Item");
  300. break;
  301. case KC_N:
  302. printStringAndQueueChar("NadRate");
  303. break;
  304. case KC_P:
  305. if (last2CodesAre(KC_D, KC_C))
  306. {
  307. ReplaceString("DistributionCenter", "DistCenter");
  308. printStringAndQueueChar("Pricing");
  309. }
  310. else if (last2CodeAre(KC_P))
  311. {
  312. }
  313. else if(lastCodeIs(KC_P))
  314. {
  315. ReplaceString("Product", "Person");
  316. }
  317. else
  318. {
  319. printStringAndQueueChar("Product");
  320. }
  321. break;
  322. case KC_Q:
  323. printStringAndQueueChar("Darden");
  324. break;
  325. case KC_S:
  326. if (containsCode(KC_W))
  327. if (containsCode(KC_S) || containsCode(KC_D))
  328. printStringAndQueueChar("Step");
  329. else
  330. printStringAndQueueChar("Session");
  331. else
  332. printStringAndQueueChar("Supplier");
  333. break;
  334. case KC_T:
  335. if (containsCode(KC_W))
  336. printStringAndQueueChar("Task");
  337. else
  338. printStringAndQueueChar("Type");
  339. break;
  340. case KC_W:
  341. printStringAndQueueChar("Workflow");
  342. break;
  343. }
  344. addKeyCode(kc);
  345. return false;
  346. }
  347. bool shifted = false;
  348. bool isShifted()
  349. {
  350. return shifted;
  351. }
  352. void setShifted(bool val)
  353. {
  354. shifted = val;
  355. }
  356. bool storeShiftState(uint16_t keycode, keyrecord_t *record)
  357. {
  358. if (record->event.pressed)
  359. {
  360. switch (keycode)
  361. {
  362. case SC_LSPO:
  363. case SC_RSPC:
  364. shifted = true;
  365. }
  366. }
  367. else
  368. {
  369. switch (keycode)
  370. {
  371. case SC_LSPO:
  372. case SC_RSPC:
  373. shifted = false;
  374. return true;
  375. }
  376. }
  377. return true;
  378. }
  379. bool handleSmartMacros(uint16_t keycode, keyrecord_t *record)
  380. {
  381. if (relativityActive != true) return true;
  382. if (record->event.pressed)
  383. {
  384. switch (keycode)
  385. {
  386. case KC_BSPC:
  387. if (!isShifted()){
  388. deletePrev();
  389. }
  390. else {
  391. register_code(KC_BSPC);
  392. unregister_code(KC_BSPC);
  393. }
  394. return false;
  395. case KC_A:
  396. case KC_B:
  397. case KC_C:
  398. case KC_D:
  399. case KC_E:
  400. case KC_F:
  401. case KC_G:
  402. case KC_H:
  403. case KC_I:
  404. case KC_J:
  405. case KC_K:
  406. case KC_L:
  407. case KC_M:
  408. case KC_N:
  409. case KC_O:
  410. case KC_P:
  411. case KC_Q:
  412. case KC_R:
  413. case KC_S:
  414. case KC_T:
  415. case KC_U:
  416. case KC_V:
  417. case KC_W:
  418. case KC_X:
  419. case KC_Y:
  420. case KC_Z:
  421. return processSmartMacroTap(keycode);
  422. case PRRD:
  423. if (tempOff)
  424. {
  425. SEND_STRING("Id = ");
  426. printTableAbbreviationLimited();
  427. SEND_STRING(".Id");
  428. return deactivateRelativity();
  429. }
  430. else
  431. {
  432. printTableAbbreviation();
  433. SEND_STRING("ON ");
  434. printTableAbbreviationLimited();
  435. eraseKeyCodes();
  436. eraseTableAbbreviation();
  437. eraseCharCounts();
  438. tempOff = true;
  439. return true;
  440. }
  441. case KC_SPC:
  442. printTableAbbreviation();
  443. return deactivateRelativity();
  444. case ENTER_OR_SQL:
  445. if (tempOff)
  446. {
  447. SEND_STRING("Id = ");
  448. printTableAbbreviationLimited();
  449. SEND_STRING(".Id");
  450. deactivateRelativity();
  451. return true;
  452. }
  453. else
  454. {
  455. printTableAbbreviation();
  456. deactivateRelativity();
  457. return true;
  458. }
  459. case KC_ESC:
  460. return deactivateRelativity();
  461. }
  462. }
  463. return true;
  464. }