]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/dz60/keymaps/marianas/keymap.c
Keymap: Adding new functionality to personal keymap, smart macros (#4159)
[qmk_firmware.git] / keyboards / dz60 / keymaps / marianas / keymap.c
1 #include QMK_KEYBOARD_H
2 #include "keymap.h"
3
4 enum marianas_layers {
5   QWERTY,
6 /*
7   COLEMAK,
8   DVORAK,
9 */
10   NAV_CLUSTER,
11   GAMING,
12   SQLMACROS,
13   SQLNAMES,
14   FN_LAYER
15 };
16
17 enum sql_macros {
18   S_LFTJN = SAFE_RANGE, // L
19   S_INRJN, // I
20   S_SLCT,  // S
21   S_FROM,  // F
22   S_DSNCT, // D
23   S_ORDER, // O
24   S_WHERE, // W
25   S_ALTER, // Esc
26   S_ASTRK, // *
27
28   TD_A,
29   TD_B,
30   TD_C, // Corp, Corporation, Company
31   TD_D, // Distribution, Dist, Distributor
32   TD_E,
33   TD_F,
34   TD_G, // GlobalLookup
35   TD_H,
36   TD_I, // Instance, Item
37   TD_J,
38   TD_K,
39   TD_L,
40   TD_M,
41   TD_N, // NadRate
42   TD_O,
43   TD_P, // Product, Person,
44   TD_Q, // Darden
45   TD_R,
46   TD_S, // Supplier, Step
47   TD_T, // Task, Type
48   TD_U,
49   TD_V,
50   TD_W, // Workflow,
51   TD_X,
52   TD_Y,
53   TD_Z,
54   TD_BSPC,
55   TD_ENT,
56   TD_ESC
57 };
58
59 uint16_t *macroTaps = 0;
60
61 char *tableNameList = 0;
62
63 uint8_t *charCount = 0;
64 uint8_t countPointer = 0;
65
66 bool shifted = false;
67
68 bool sendAbbr = false;
69
70
71 void initStringData(void)
72 {
73   if (macroTaps == 0)
74   {
75     macroTaps = malloc(macroTapsLen*sizeof(uint16_t));
76     for(int i = 0; i < macroTapsLen; i++)
77     {
78       macroTaps[i] = 0;
79     }
80   }
81   if (tableNameList == 0)
82   {
83     tableNameList = malloc(tableNameListLen*sizeof(char));
84     for(int i = 0; i < tableNameListLen; i++)
85     {
86       tableNameList[i] = 0;
87     }
88   }
89   if (charCount == 0)
90   {
91     charCount = malloc(charCountLen*sizeof(uint8_t));
92     for (int i = 0; i < charCountLen; i++)
93     {
94       charCount[i] = 0;
95     }
96   }
97 }
98
99
100 uint32_t layer_state_set_user(uint32_t state)
101 {
102   switch (biton32(state))
103   {
104     case QWERTY:
105       rgblight_mode(9);
106       break;
107     case NAV_CLUSTER:
108       rgblight_mode(29);
109       break;
110     case GAMING:
111       rgblight_mode(26);
112       break;
113     case SQLMACROS:
114       rgblight_mode(1);
115       rgblight_setrgb(0x00, 0xFF, 0x80);
116       break;
117     case SQLNAMES:
118       rgblight_mode(1);
119       rgblight_setrgb(0x80, 0xFF, 0x00);
120       break;
121     case FN_LAYER:
122       rgblight_mode(1);
123       rgblight_setrgb(0x00, 0x80, 0xFF);
124       break;
125   }
126   return state;
127 }
128
129 bool containsCode(uint16_t kc)
130 {
131   for (int i = 0; i < macroTapsLen && macroTaps[i] > 0; i++)
132   {
133     if (macroTaps[i] == kc) return true;
134   }
135   return false;
136 }
137
138 bool lastCodeIs(uint16_t kc)
139 {
140   for (int i = 0; i < macroTapsLen-1 && macroTaps[i] > 0; i++)
141   {
142     if (macroTaps[i] == kc && macroTaps[i+1] == 0) return true;
143   }
144   return false;
145 }
146
147 bool last2CodeAre(uint16_t kc)
148 {
149   for (int i = 0; i < macroTapsLen-2 && macroTaps[i] > 0; i++)
150   {
151     if (macroTaps[i] == kc && macroTaps[i+1] == kc && macroTaps[i+2] == 0) return true;
152   }
153   return false;
154 }
155
156 bool last2CodesAre(uint16_t kc, uint16_t kc2)
157 {
158   for (int i = 0; i < macroTapsLen-2 && macroTaps[i] > 0; i++)
159   {
160     if (macroTaps[i] == kc && macroTaps[i+1] == kc2 && macroTaps[i+2] == 0) return true;
161   }
162   return false;
163 }
164
165 void addKeyCode(uint16_t kc)
166 {
167   int i = 0;
168   while (i < macroTapsLen-2 && macroTaps[i] > 0) i++;
169   if (macroTaps[i] == 0)
170   {
171     macroTaps[i] = kc;
172     macroTaps[i+1] = 0;
173   }
174 }
175
176 void eraseKeyCodes(void)
177 {
178   int i = 0;
179   while (i < macroTapsLen && macroTaps[i] > 0) macroTaps[i++] = 0;
180 }
181
182 void eraseCharCounts(void)
183 {
184   int i = 0;
185   while (i < charCountLen)
186   {
187     charCount[i] = 0;
188   }
189 }
190
191 void printTableAbbreviation(void)
192 {
193   initStringData();
194   if (tableNameList[0] == 0)
195   {
196     return;
197   }
198   send_char(0x20);
199   int i = 0;
200   for (i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
201   {
202     if (tableNameList[i] >= 65 && tableNameList[i] <= 90)
203     {
204       send_char(tableNameList[i]+32);
205     }
206     else
207     {
208       send_char(tableNameList[i]);
209     }
210   }
211   send_char(0x20);
212 }
213
214 void eraseTableAbbreviation(void)
215 {
216   initStringData();
217   for (int i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
218   {
219     tableNameList[i] = '\0';
220   }
221 }
222
223 void printString(char* str)
224 {
225
226   if (str[0] != '\0')
227   {
228     int i = 0;
229     while (true)
230     {
231       if (str[i] == 0)
232       {
233         break;
234       }
235       send_char(str[i++]);
236       charCount[countPointer]++;
237     }
238   }
239 }
240
241 void printStringAndQueueChar(char* str)
242 {
243   initStringData();
244   if (charCount[countPointer] != 0)
245   {
246     countPointer++;
247   }
248   sendAbbr = true;
249   if (str[0] != '\0')
250   {
251     printString(str);
252
253     for (int i = 0; i < tableNameListLen-1; i++)
254     {
255       if (tableNameList[i] == '\0')
256       {
257         tableNameList[i] = str[0];
258         tableNameList[i+1] = '\0';
259         break;
260       }
261       else if (i == tableNameListLen-2)
262       {
263         printTableAbbreviation();
264         break;
265       }
266     }
267     //for (i = 0; i < tableNameListLen && tableNameList[i] > 0; i++)
268     //{
269     //  send_char(tableNameList[i]);
270     //}
271     //send_string_P("Darden");
272     //send_string_P(&myarray);
273     //send_string_P(str);
274   }
275 }
276
277 void ReplaceString(char *orig, char *repl)
278 {
279   int i = 0;
280   while((orig[i] != 0x0 && repl[i] != 0x0) && orig[i] == repl[i])
281    i++;
282
283   if(orig[i] != 0x0)
284   {
285     int o = i;
286     while (orig[o++] != 0x0) {
287       charCount[countPointer]--;
288       register_code(KC_BSPC);
289       unregister_code(KC_BSPC);
290     }
291   }
292   printString(repl+i);
293 }
294
295 void deletePrev(void)
296 {
297   for (int i = 0; i < charCount[countPointer]; i++)
298   {
299       register_code(KC_BSPC);
300       unregister_code(KC_BSPC);
301   }
302   charCount[countPointer] = 0;
303   countPointer--;
304   int i = 1;
305   for (;i < tableNameListLen-1; i++)
306   {
307     if (tableNameList[i] == 0x0)
308     {
309       break;
310     }
311   }
312   tableNameList[i-1] = 0x0;
313 }
314
315 void processSmartMacroTap(uint16_t kc)
316 {
317   initStringData();
318   switch(kc)
319   {
320     case TD_C:
321       if (containsCode(TD_D))
322       {
323         printString("ribution");
324         printStringAndQueueChar("Center");
325       }
326       else if (last2CodeAre(TD_C))
327       {
328         ReplaceString("Corporation", "Contact");
329       }
330       else if(lastCodeIs(TD_C))
331       {
332         printString("oration");
333       }
334       else
335       {
336         printStringAndQueueChar("Corp");
337       }
338       break;
339     case TD_D:
340       if (last2CodeAre(TD_D))
341       {
342         ReplaceString("Distribution", "Distributor");
343       }
344       else if(lastCodeIs(TD_D))
345       {
346         printString("ribution");
347       }
348       else
349       {
350         printStringAndQueueChar("Dist");
351       }
352       break;
353     case TD_G:
354         printStringAndQueueChar("Global");
355         printStringAndQueueChar("Lookup");
356       break;
357     case TD_I:
358       if (containsCode(TD_W))
359         printStringAndQueueChar("Instance");
360       else
361         printStringAndQueueChar("Item");
362       break;
363     case TD_N:
364       printStringAndQueueChar("NadRate");
365       break;
366     case TD_P:
367       if (last2CodesAre(TD_D, TD_C))
368       {
369         ReplaceString("DistributionCenter", "DistCenter");
370         printStringAndQueueChar("Pricing");
371       }
372       else if (last2CodeAre(TD_P))
373       {
374       }
375       else if(lastCodeIs(TD_P))
376       {
377         ReplaceString("Product", "Person");
378       }
379       else
380       {
381         printStringAndQueueChar("Product");
382       }
383       break;
384     case TD_Q:
385       printStringAndQueueChar("Darden");
386       break;
387     case TD_S:
388       if (containsCode(TD_W))
389         if (containsCode(TD_S) || containsCode(TD_D))
390           printStringAndQueueChar("Step");
391         else
392           printStringAndQueueChar("Session");
393       else
394         printStringAndQueueChar("Supplier");
395       break;
396     case TD_T:
397       if (containsCode(TD_W))
398         printStringAndQueueChar("Task");
399       else
400         printStringAndQueueChar("Type");
401       break;
402     case TD_W:
403       printStringAndQueueChar("Workflow");
404       break;
405   }
406   addKeyCode(kc);
407 }
408
409 bool process_record_user(uint16_t keycode, keyrecord_t *record)
410 {
411   if (record->event.pressed)
412   {
413     switch (keycode)
414     {
415       case KC_LSPO:
416       case KC_RSPC:
417         shifted = true;
418         return true;
419
420       case S_LFTJN: SEND_STRING("LEFT JOIN"); return false;
421       case S_INRJN: SEND_STRING("INNER JOIN "); return false;
422       case S_SLCT:  SEND_STRING("SELECT "); return false;
423       case S_FROM:  SEND_STRING("FROM "); return false;
424       case S_DSNCT: SEND_STRING("DISTINCT "); return false;
425       case S_ORDER: SEND_STRING("ORDER "); return false;
426       case S_WHERE: SEND_STRING("WHERE "); return false;
427       case S_ALTER: SEND_STRING("ALTER SESSION SET CURRENT_SCHEMA = "); return false;
428       case S_ASTRK: SEND_STRING("* "); return false;
429
430       case KC_BSLS:
431         initStringData();
432         layer_on(SQLNAMES);
433         return false;
434
435       case TD_BSPC:
436         if (!shifted){
437           deletePrev();
438         }
439         else {
440           register_code(KC_BSPC);
441           unregister_code(KC_BSPC);
442         }
443         return false;
444
445       case TD_A:
446       case TD_B:
447       case TD_C:
448       case TD_D:
449       case TD_E:
450       case TD_F:
451       case TD_G:
452       case TD_H:
453       case TD_I:
454       case TD_J:
455       case TD_K:
456       case TD_L:
457       case TD_M:
458       case TD_N:
459       case TD_O:
460       case TD_P:
461       case TD_Q:
462       case TD_R:
463       case TD_S:
464       case TD_T:
465       case TD_U:
466       case TD_V:
467       case TD_W:
468       case TD_X:
469       case TD_Y:
470       case TD_Z:
471         processSmartMacroTap(keycode);
472         return false;
473
474       case TD_ENT:
475         printTableAbbreviation();
476       case TD_ESC:
477         eraseKeyCodes();
478         eraseTableAbbreviation();
479         layer_off(SQLNAMES);
480         return true;
481     }
482   }
483   else
484   {
485     switch (keycode)
486     {
487
488       case KC_BSLS:
489         if (macroTaps[0] == 0)
490         {
491           SEND_STRING("\\");
492           layer_off(SQLNAMES);
493         }
494         return true;
495       case KC_LSPO:
496       case KC_RSPC:
497         shifted = false;
498         return true;
499     }
500   }
501   return true;
502 };
503
504 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
505
506   [QWERTY]=
507     LAYOUT_60_ansi(
508       ESCAP,  KC_1,  KC_2,  KC_3,  KC_4,  KC_5,  KC_6,  KC_7,  KC_8,  KC_9,  KC_0,  MNUS,  EQUL,  BACKSPC,
509       KC_TAB,  KC_Q,  KC_W,  KC_E,  KC_R,  KC_T,  KC_Y,  KC_U,  KC_I,  KC_O,  KC_P,  LBRC,  RBRC,  BSLASH,
510       MO_FNLR,  KC_A,  KC_S,  KC_D,  KC_F,  KC_G,  KC_H,  KC_J,  KC_K,  KC_L,  COLN,  QUOT,  ENTER_OR_SQL,
511       LEFTSHFT,  KC_Z,  KC_X,  KC_C,  KC_V,  KC_B,  KC_N,  KC_M,  CMMA,  PRRD,  SLSH,  RIGHT_SHIFT__PAREN,
512       CTLL,  WINL,  ALTL,  SPAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACE,  ALTR,  WINR,  APPR,  CTLR),
513
514   [NAV_CLUSTER]=
515     LAYOUT_60_ansi(
516       _____,  PSCR,  SCRL,  PAUS,  NSRT,  HOME,  PGUP,  NMLK,  KSSH,  STAR,  KMIN,  ____,  ____,  ______,
517       ______,  ____,  ____,  ____,  DELT,  END_,  PGDN,  KP_7,  KP_8,  KP_9,  PLUS,  ____,  ____,  _____,
518       _______,  ____,  ____,  ____,  ____,  UPUP,  UPUP,  KP_4,  KP_5,  KP_6,  PLUS,  ____,  ___________,
519       ________,  ____,  ____,  ____,  LEFT,  D_WN,  RGHT,  KP_1,  KP_2,  KP_3,  KNTR,  _________________,
520       ____,  ____,  ____,  /*-----------------*/KC_KP_0/*-----------------*/,  KDOT,  KNTR,  ____,  ____),
521
522   [GAMING]=
523     LAYOUT_60_ansi(
524       _____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ______,
525       ______,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  _____,
526          KCNO,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ___________,
527       ________,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  _________________,
528       ____,  KCNO,  ____,  /*------------------*/_____/*------------------*/,  ____,  KCNO,  ____,  QWRTY),
529
530   [SQLMACROS]=
531     LAYOUT_60_ansi(
532       S_ALTER, ____, ____, ____, ____, ____, ____, ____, S_ASTRK, ____, ____, ____, ____,    ___________,
533       ______,    ____, S_WHERE, ____, ____, ____, ____, ____, S_INRJN, S_ORDER, ____, ____, ____, ______,
534       _______, KC_LBRC, S_SLCT, KC_PAST,S_FROM, ____, ____, ____, ____, S_LFTJN, ____, RBRC, ___________,
535       ________,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  _________________,
536       ____,  ____,  ____,  /*------------------*/_____/*------------------*/,  ____,  ____,  ____,  ____),
537
538   [SQLNAMES]=
539     LAYOUT_60_ansi(
540       TD_ESC,    ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____,  ____, TD_BSPC,
541       ________,   TD_Q,  TD_W,  TD_E,  TD_R,  TD_T,  TD_Y,  TD_U,  TD_I,  TD_O,  TD_P,  ____,  ____,  _____,
542       ___________, TD_A,  TD_S,  TD_D,  TD_F,  TD_G,  TD_H,  TD_J,  TD_K,  TD_L,  ____,  ____,       TD_ENT,
543       ___________,  TD_Z,  TD_X,  TD_C,  TD_V,  TD_B,  TD_N,  TD_M,  ____,  ____,  ____,  _________________,
544       ____, ____, ____, /*----------------------*/TD_ENT/*-----------------------*/, ____, ____, ____, RESET),
545
546   [FN_LAYER]=
547     LAYOUT_60_ansi(
548       KC_GRV, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_DEL,
549       KC_CAPSLOCK, KC_MPRV, KC_MPLY, KC_MNXT, LWIN(KC_R), ____, KC_CALC, KC_PGUP, KC_UP, KC_PGDN, KC_PSCR, KC_SLCK, KC_BRK, ____,
550       ____, KC_VOLD, KC_MUTE, KC_VOLU, ____, ____, KC_HOME, KC_LEFT, KC_DOWN, KC_RIGHT, KC_INS, KC_DEL, ____,
551       ____, ____, ____, ____, ____, ____, KC_END, ____, QWRTY, NAVS, GAME, ____,
552       ____, ____, ____, _________________, ____, KC_HYPR, KC_MEH, ____)
553 };