forked from Phoscur/oryx-macro-hax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.ts
186 lines (170 loc) · 6.79 KB
/
macros.ts
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
import { readFileSync, writeFileSync } from "fs";
export interface MacroBuilder {
delay: (milliseconds: number) => MacroBuilder,
sendRawCmd: (rawCmd: string) => MacroBuilder,
// Works with a-z, 0-9, space and newline-as-enter
typeAlphanumeric: (strToType: string, msDelayBetweenStrokes?: number) => MacroBuilder,
tapKey: (rawKey: string, msDelayAfter?: number) => MacroBuilder,
withModifiers: (innerMacro: MacroBuilder, rawModifiers: string[]) => MacroBuilder,
withShift: (innerMacro: MacroBuilder) => MacroBuilder,
withWin: (innerMacro: MacroBuilder) => MacroBuilder,
build: () => string,
expectedReplacements: number,
}
export const newMacro: (expectedReplacementCount?: number) => MacroBuilder = (erc: number = 1) => {
const commands: (() => string)[] = []
const self: MacroBuilder = {
expectedReplacements: erc,
tapKey: (rawKey: string, msDelayAfter: number) => {
if (!rawKey.startsWith("X_")) {
throw new Error("rawKey must come from the underlying language thingy, maybe you meant X_" + rawKey.toUpperCase() + " or something?");
}
commands.push(() => `SS_TAP(${rawKey})`);
if (msDelayAfter > 0) {
self.delay(msDelayAfter)
}
return self;
},
// Works with a-z, 0-9, space and newline-as-enter
typeAlphanumeric: (strToType: string, msDelayBetweenStrokes: number = 30) => {
for (let i = 0; i < strToType.length; i++) {
const char = strToType[i]
if (char !== char.toLowerCase()) {
self.withShift(newMacro().typeAlphanumeric(char.toLowerCase()))
}
else if (char !== char.toUpperCase() || (char >= "0" && char <= "9")) {
self.tapKey(`X_${char.toUpperCase()}`, msDelayBetweenStrokes);
}
else if (char === " ") {
self.tapKey("X_SPACE")
}
else if (char === "\n") {
self.tapKey("X_ENTER")
}
else if (char === "=") {
self.tapKey("X_EQUAL")
}
else if (char === "[") {
self.tapKey("X_LBRACKET")
}
else if (char === "]") {
self.tapKey("X_RBRACKET")
}
else if (char === ";") {
self.tapKey("X_SEMICOLON")
}
else if (char === ",") {
self.tapKey("X_COMMA")
}
else if (char === ".") {
self.tapKey("X_DOT")
}
else if (char === "+") {
self.tapKey("X_PLUS")
}
else if (char === "-") {
self.tapKey("X_MINUS")
}
else if (char === "/") {
self.tapKey("X_SLASH")
}
else if (char === "'") {
self.tapKey("X_QUOTE")
}
else if (char === '"') {
self.withShift(newMacro().tapKey("X_QUOTE"))
}
else if (char === "!") {
self.withShift(newMacro().tapKey("X_1"))
}
else if (char === "@") {
self.withShift(newMacro().tapKey("X_2"))
}
else {
throw Error("Unsupported char in typeAlphanumeric: " + char + ", use typeRaw instead?")
}
if (msDelayBetweenStrokes > 0) {
self.delay(msDelayBetweenStrokes);
}
}
return self;
},
delay: (msDelay: number) => {
self.sendRawCmd("SS_DELAY(" + msDelay + ")");
return self;
},
sendRawCmd: (rawCmd: string) => {
commands.push(() => rawCmd)
return self
},
// Runs some inner macro while holding down all
// modifier keys
withModifiers: (
innerMacro: MacroBuilder,
rawModifiers: string[]) => {
commands.push(() =>
rawModifiers.map((m) => `${m}(`) +
innerMacro.build() +
rawModifiers.map(() => ")"));
return self
},
withShift: (innerMacro: MacroBuilder) => {
return self.withModifiers(innerMacro, ["SS_LSFT"])
},
withWin: (innerMacro: MacroBuilder) => {
return self.withModifiers(innerMacro, ["SS_LWIN"])
},
build: () => {
const cmds = commands.map((cmd) => cmd())
// Trim delays off the ends of commands
while (cmds.length > 0 && cmds[cmds.length - 1].startsWith("SS_DELAY")) {
cmds.splice(cmds.length - 1, 1)
}
return cmds.join(" ");
}
};
return self;
}
export const processAll = (macroMap: {
[originalMacroKeys: string]: MacroBuilder
}, keymapFile: string) => {
const loaded = readFileSync(keymapFile).toString()
const orig = Object.keys(macroMap)
orig.forEach((macroKeys) => {
const toFind = "SEND_STRING(" + charStrToMacro(macroKeys).build() + ")"
const newMacro = macroMap[macroKeys]
const matchCount = loaded.split(toFind).length - 1
if (matchCount !== newMacro.expectedReplacements) {
console.error(toFind)
throw new Error(`Found ${matchCount} instances of the ${macroKeys} macro but expected ${newMacro.expectedReplacements} instances! Check your config and set the proper value in newMacro()`)
}
})
const backup = keymapFile + Math.random() + ".old-"
console.log("Backed up keymap.c to " + backup)
writeFileSync(backup, loaded)
let newConfig = `${loaded}`
orig.forEach((macroKeys) => {
const toFind = "SEND_STRING(" + charStrToMacro(macroKeys).build() + ")"
const newMacro = macroMap[macroKeys]
const macro = "SEND_STRING(" + newMacro.build() + ")"
for (let i = 0; i < newMacro.expectedReplacements; i++) {
console.log("Replacing \n" + toFind + "\nwith\n" + macro)
newConfig = newConfig.replace(toFind, macro)
}
})
writeFileSync(keymapFile, newConfig)
console.log("ALL done! Proceed with compilation and flashing")
};
function charStrToMacro(keys: string): MacroBuilder {
if (keys.length < 1 || keys.length > 4) {
throw new Error("Please check macro ID for " + keys);
}
let macro = newMacro()
for (let i = 0; i < keys.length; i++) {
macro = macro.tapKey(`X_${keys[i].toUpperCase()}`)
if (i < keys.length - 1) {
macro = macro.delay(100)
}
}
return macro
}