From 2688a51b1b6716ef4ce85220d37e04b80e978125 Mon Sep 17 00:00:00 2001 From: Sam Rang Date: Sun, 17 Apr 2016 21:52:22 -0500 Subject: [PATCH] simple keypress action macro working w/ macro type determination --- config-serializer/config-items/MacroAction.ts | 55 +++++++++++++++++++ .../config-items/MacroActions.ts | 28 ++++++++++ .../config-items/PressKeyAction.ts | 33 +++++++++++ .../config-items/config-items.ts | 3 + config-serializer/test-serializer.ts | 6 +- config-serializer/uhk-config.json | 50 ----------------- 6 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 config-serializer/config-items/MacroAction.ts create mode 100644 config-serializer/config-items/MacroActions.ts create mode 100644 config-serializer/config-items/PressKeyAction.ts diff --git a/config-serializer/config-items/MacroAction.ts b/config-serializer/config-items/MacroAction.ts new file mode 100644 index 00000000..930dada1 --- /dev/null +++ b/config-serializer/config-items/MacroAction.ts @@ -0,0 +1,55 @@ +enum MacroActionId { + PressKeyAction = 0, + HoldKeyAction = 1, + ReleaseKeyAction = 2, + PressModifiersAction = 3, + HoldModifiersAction = 4, + ReleaseModifiersAction = 5, + PressMouseButtonsAction = 6, + HoldMouseButtonsAction = 7, + ReleaseMouseButtonsAction = 8, + MoveMouseAction = 9, + ScrollMouseAction = 10, + DelayAction = 11, + TextAction = 12 +} + +let macroActionType = { + PressKeyAction : 'pressKey', + HoldKeyAction : 'holdKey', + ReleaseKeyAction : 'releaseKey', + PressModifiersAction : 'pressModifiers', + HoldModifiersAction : 'holdModifiers', + ReleaseModifiersAction : 'releaseModifiers', + PressMouseButtonsAction : 'pressMouseButtons', + HoldMouseButtonsAction : 'holdMouseButtons', + ReleaseMouseButtonsAction : 'releaseMouseButtons', + MoveMouseAction : 'moveMouse', + ScrollMouseAction : 'scrollMouse', + DelayAction : 'delay', + TextAction : 'text' +}; + +abstract class MacroAction extends Serializable { + assertMacroActionType(jsObject: any) { + let macroActionClassname = this.constructor.name; + let macroActionTypeString = macroActionType[macroActionClassname]; + if (jsObject.macroActionType !== macroActionTypeString) { + throw `Invalid ${macroActionClassname}.macroActionType: ${jsObject.macroActionType}`; + } + } + + readAndAssertMacroActionId(buffer: UhkBuffer) { + let classname = this.constructor.name; + let readMacroActionId = buffer.readUInt8(); + let macroActionId = MacroActionId[ classname]; + if (readMacroActionId !== macroActionId) { + throw `Invalid ${classname} first byte: ${readMacroActionId}`; + } + } + + abstract _fromJsObject(jsObject: any): MacroAction; + abstract _fromBinary(buffer: UhkBuffer): MacroAction; + abstract _toJsObject(): any; + abstract _toBinary(buffer: UhkBuffer): void; +} diff --git a/config-serializer/config-items/MacroActions.ts b/config-serializer/config-items/MacroActions.ts new file mode 100644 index 00000000..8a0f13f6 --- /dev/null +++ b/config-serializer/config-items/MacroActions.ts @@ -0,0 +1,28 @@ +class MacroActions extends ClassArray { + + jsObjectToClass(jsObject: any): Serializable { + switch (jsObject.macroActionType) { + case macroActionType.PressKeyAction: + return new PressKeyAction().fromJsObject(jsObject); + default: + throw `Invalid MacroAction.macroActionType: "${jsObject.macroActionType}"`; + } + } + + binaryToClass(buffer: UhkBuffer): Serializable { + let macroActionFirstByte = buffer.readUInt8(); + buffer.backtrack(); + + if (buffer.enableDump) { + process.stdout.write(']\n'); + buffer.enableDump = false; + } + + switch (macroActionFirstByte) { + case MacroActionId.PressKeyAction: + return new PressKeyAction().fromBinary(buffer); + default: + throw `Invalid MacroAction first byte: ${macroActionFirstByte}`; + } + } +} diff --git a/config-serializer/config-items/PressKeyAction.ts b/config-serializer/config-items/PressKeyAction.ts new file mode 100644 index 00000000..456b4bbb --- /dev/null +++ b/config-serializer/config-items/PressKeyAction.ts @@ -0,0 +1,33 @@ +class PressKeyAction extends MacroAction { + + // @assertUInt8 + scancode: number; + + _fromJsObject(jsObject: any): PressKeyAction { + this.assertMacroActionType(jsObject); + this.scancode = jsObject.scancode; + return this; + } + + _fromBinary(buffer: UhkBuffer): PressKeyAction { + this.readAndAssertMacroActionId(buffer); + this.scancode = buffer.readUInt8(); + return this; + } + + _toJsObject(): any { + return { + macroActionType: macroActionType.PressKeyAction, + scancode: this.scancode + }; + } + + _toBinary(buffer: UhkBuffer) { + buffer.writeUInt8(MacroActionId.PressKeyAction); + buffer.writeUInt8(this.scancode); + } + + toString(): string { + return ``; + } +} diff --git a/config-serializer/config-items/config-items.ts b/config-serializer/config-items/config-items.ts index 62fa52fd..08a05a1a 100644 --- a/config-serializer/config-items/config-items.ts +++ b/config-serializer/config-items/config-items.ts @@ -15,3 +15,6 @@ /// /// /// +/// +/// +/// diff --git a/config-serializer/test-serializer.ts b/config-serializer/test-serializer.ts index 50f0791e..cc4c97ab 100644 --- a/config-serializer/test-serializer.ts +++ b/config-serializer/test-serializer.ts @@ -10,8 +10,8 @@ let fs = require('fs'); let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json')); -let config1Js = uhkConfig.keymaps; -let config1Ts: Serializable = new KeyMaps().fromJsObject(config1Js); +let config1Js = uhkConfig.macros[0].macroActions; +let config1Ts: Serializable = new MacroActions().fromJsObject(config1Js); let config1Buffer = new UhkBuffer(); config1Ts.toBinary(config1Buffer); let config1BufferContent = config1Buffer.getBufferContent(); @@ -19,7 +19,7 @@ fs.writeFileSync('uhk-config.bin', config1BufferContent); config1Buffer.offset = 0; console.log(); -let config2Ts = new KeyMaps().fromBinary(config1Buffer); +let config2Ts = new MacroActions().fromBinary(config1Buffer); console.log('\n'); let config2Js = config2Ts.toJsObject(); let config2Buffer = new UhkBuffer(); diff --git a/config-serializer/uhk-config.json b/config-serializer/uhk-config.json index 246c6096..afada8d0 100644 --- a/config-serializer/uhk-config.json +++ b/config-serializer/uhk-config.json @@ -202,56 +202,6 @@ { "macroActionType": "pressKey", "scancode": 111 - }, - { - "macroActionType": "holdKey", - "scancode": 111 - }, - { - "macroActionType": "releaseKey", - "scancode": 111 - }, - { - "macroActionType": "pressModifiers", - "modifierMask": 111 - }, - { - "macroActionType": "holdModifiers", - "modifierMask": 111 - }, - { - "macroActionType": "releaseModifiers", - "modifierMask": 111 - }, - { - "macroActionType": "pressMouseButtons", - "mouseButtonsMask": 9 - }, - { - "macroActionType": "holdMouseButtons", - "mouseButtonsMask": 9 - }, - { - "macroActionType": "releaseMouseButtons", - "mouseButtonsMask": 9 - }, - { - "macroActionType": "moveMouse", - "x": 123, - "y": 123 - }, - { - "macroActionType": "scrollMouse", - "x": 123, - "y": 123 - }, - { - "macroActionType": "delay", - "delay": "1000" - }, - { - "macroActionType": "text", - "text": "this is a text" } ] }