summaryrefslogtreecommitdiff
path: root/keyboards/gboards/dicts/aset/_generator/main.go
blob: ae7e15c8be960346f3c0a99b1d78f17f70a8bacd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main

import (
	"io/ioutil"
	"fmt"
	"encoding/json"
	"os"
	"path/filepath"
	"strings"
	"sort"
	"hash/crc64"
	//"encoding/base64"
)

func main() {
	// Show Usage
	if len(os.Args) < 3 {
		fmt.Println("Usage: ./keymap-gen src-dir out-dir")
		fmt.Println("Outputs c files in out-dir")
		fmt.Println("Make sure the dirs exist.")
		return
	}

	files, err := filepath.Glob(os.Args[1] + "/*.json")
	if err != nil {
		fmt.Printf("Could not open src-dir: %v\n", err)
		return
	}

	for _, fname := range(files) {
		fmt.Println("Processing: ", fname)
		// Read the source
		data, err := ioutil.ReadFile(fname)
		if err != nil {
			panic(err)
		}

		// Unbundle Data
		var FullDict map[string]Entry
		json.Unmarshal(data, &FullDict)

		// Loop over entries and store
		var output []string
		for i,v := range FullDict {
			if i == "0" {
				continue
			}
			
			// Special handling for numbermap
			var entry string
			if strings.Contains(fname, "num") {
				entry = v.toKeymap("NUM|")
			} else {
				entry = v.toKeymap("")
			}

			if entry != "" {
				output = append(output, entry)
			}
		}

		// Sort by length, then amount of whitespace lol
		sort.Slice(output, func (i,j int) bool {
			var maxLen int
			if len(output[i]) > len(output[j]) { 
				maxLen = len(output[i])
			} else {
				maxLen = len(output[j])
			}

			return maxLen-strings.Count(output[i][:40], " ") < maxLen-strings.Count(output[j][:40], " ")
		})

		// Whack a disclaimer
		output = append([]string{"// This file is automatically generated. Do not edit it!\n\n"}, output...)
		
		// Write all data out
		outName := filepath.Base(fname)
		outName = os.Args[2] + outName[:len(outName)-5]+".def"
		fmt.Println("Saving: ", outName)
		ioutil.WriteFile(outName, []byte(strings.Join(output, "")), 0755)
	}
}

func (e Entry) toKeymap(prefix string) (string) {
	// storage for parts
	var command, chord, arg string
	wordInfo := parseWords(e)

	// Handle prefix
	if prefix != "" {
		chord = prefix
	}

	// Format the chord
	keys := []string{"AA", "AS", "AE", "AT", "AN", "AI", "AO", "AP"}
	for i, v := range e.Input { 
		chord += keys[v-1]

		if i != len(e.Input)-1 {
			chord += "|"
		}
	}

	// Handle specials/base first
	var ok bool
	var v  []string

	if e.Special != "" {
		v, ok = QMKLookup[e.Special] 
	}
	if !ok && e.Base != "" {
		v, ok = QMKLookup[e.Base] 
	}

	if ok {
		// Determine way to send key
		if len(v) == 1 {
			command = "PRES("
		} else {
			command = "KEYS("
		}
	}

	if ok {
		if len(v) > 1 {
			arg += "{"
		}

		// String together args
		for ii, vv := range(v) {
			arg += vv
			if ii == len(v)-1 && len(v) > 1 {
				arg += ", COMBO_END}"
			} else if ii != len(v)-1 {
				arg += ", "
			}
		}

		
		hash := crc64.Checksum([]byte(fmt.Sprintf("%v%v", arg, chord)), crc64.MakeTable(crc64.ECMA))
		hashStr := fmt.Sprintf("cmb_%x", hash)
		wordSpacer := strings.Repeat(" ", 42-len(arg))
		if command == "KEYS(" {
			arg = fmt.Sprintf("%v, %v %v", hashStr, wordSpacer, arg)
		} else {
			arg = fmt.Sprintf("%65v", arg)
		}
			
		goto Found
	}

	// Parse out word info
	if wordInfo.LRank == 0 && wordInfo.RRank == 0 {
		goto Blank
	}

	if wordInfo.LRank != 0 || wordInfo.RRank != 0 {
		if wordInfo.LRank != 0 && wordInfo.RRank != 0 {
			// Just blank the structure and recall
			left, right := e, e
			left.Trw = nil
			right.Tlw = nil
			return fmt.Sprintf("%v%v", left.toKeymap("LFT|"), right.toKeymap("RGT|"))
		}

		var word string
		if wordInfo.LRank > wordInfo.RRank {
			word = wordInfo.LWord
		} else {
			word = wordInfo.RWord
		}

		// Add in thumb
		chord = "AR|" + chord


		// generate function name
		hash := crc64.Checksum([]byte(word), crc64.MakeTable(crc64.ECMA))
		hashStr := fmt.Sprintf("str_%016X", hash)
		command = "SUBS("
		wordSpacer := strings.Repeat(" ", 40-len(word))
		arg = fmt.Sprintf("%v, %v \"%v \"", hashStr, wordSpacer, word)
		goto Found
	}

	panic(e.String())

	Found:
	chord += ","
	return fmt.Sprintf("%v%-35v%v)\n", command, chord, arg)

	Blank:
	return ""
}

type Entry struct {
	Input 	[]int
	Base		string
	Tlw			[]interface{}
	Trw			[]interface{}
	Special string
}
type Word struct {
	LWord 		string 
	LRank			float64
	RWord 		string 
	RRank			float64
}

func parseWords(e Entry) (ret Word) {
	if len(e.Tlw) > 0 { 
		ret.LWord = e.Tlw[0].(string)
		ret.LRank=  e.Tlw[1].(float64)
	}
	if len(e.Trw) > 0 { 
		ret.RWord = e.Trw[0].(string)
		ret.RRank=  e.Trw[1].(float64)
	}
	return ret
}
func (e Entry) String() (ret string) {
	ret = fmt.Sprintln("Input: ", e.Input)
	ret += fmt.Sprintln("Base: ",  e.Base)
	ret += fmt.Sprintln("Left: ", e.Tlw)
	ret += fmt.Sprintln("Right: ", e.Trw)
	ret += fmt.Sprintln("Special: ", e.Special)

	return ret
}

var QMKLookup = map[string][]string {
"!":[]string{"KC_LSFT", "KC_1"},
"'":[]string{"KC_QUOT"},
"(":[]string{"KC_LSFT", "KC_9"},
")":[]string{"KC_LSFT", "KC_0"},
",":[]string{"KC_COMM"},
"-":[]string{"KC_MINS"},
".":[]string{"KC_DOT"},
";":[]string{"KC_SCLN"},
"?":[]string{"KC_QUOT"},
"a":[]string{"KC_A"},
"b":[]string{"KC_B"},
"c":[]string{"KC_C"},
"d":[]string{"KC_D"},
"e":[]string{"KC_E"},
"f":[]string{"KC_F"},
"g":[]string{"KC_G"},
"h":[]string{"KC_H"},
"i":[]string{"KC_I"},
"j":[]string{"KC_J"},
"k":[]string{"KC_K"},
"l":[]string{"KC_L"},
"m":[]string{"KC_M"},
"n":[]string{"KC_N"},
"o":[]string{"KC_O"},
"p":[]string{"KC_P"},
"q":[]string{"KC_Q"},
"r":[]string{"KC_R"},
"s":[]string{"KC_S"},
"t":[]string{"KC_T"},
"u":[]string{"KC_U"},
"v":[]string{"KC_V"},
"w":[]string{"KC_W"},
"x":[]string{"KC_X"},
"y":[]string{"KC_Y"},
"z":[]string{"KC_Z"},

//specials
"bksp":[]string{"KC_BSPC"},
"enter":[]string{"KC_ENT"},
//"numsym":[]string{"NUM)"}, //TODO: Sticky
//"LETTERS":[]string{"KC_SPC"},

//symbols
"[":[]string{"KC_LBRC"},
"]":[]string{"KC_RBRC"},
" ":[]string{"KC_SPC"},
"1":[]string{"KC_1"},
"2":[]string{"KC_2"},
"3":[]string{"KC_3"},
"4":[]string{"KC_4"},
"5":[]string{"KC_5"},
"6":[]string{"KC_6"},
"7":[]string{"KC_7"},
"8":[]string{"KC_8"},
"9":[]string{"KC_9"},
"0":[]string{"KC_0"},
"=":[]string{"KC_EQL"},
"Fn":[]string{"KC_NO"},
"SPACE":[]string{"KC_SPC"},
"Home":[]string{"KC_HOME"},
"End":[]string{"KC_END"},
"    ":[]string{"KC_TAB"},
"   ":[]string{"KC_TAB"},
"\t":[]string{"KC_TAB"},
"`":[]string{"KC_GRV"},
}