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