From 5b6f62c11319bc4e81b6bd22bb10bcb4451c3a6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Wed, 30 Mar 2016 17:19:09 +0200 Subject: [PATCH] Make the first byte of KeyAction items purely denote their type. Make the binary representation of KeyAction items variable-length. --- .../config-items/DualRoleKeystrokeAction.ts | 32 +++++++--- config-serializer/config-items/KeyAction.ts | 62 ++++++++++--------- .../config-items/KeystrokeAction.ts | 9 +-- .../KeystrokeWithModifiersAction.ts | 48 ++++++++++++++ config-serializer/config-items/MouseAction.ts | 6 +- config-serializer/config-items/NoneAction.ts | 10 +-- .../config-items/PlayMacroAction.ts | 11 +--- .../config-items/SwitchKeymapAction.ts | 2 + .../config-items/config-items.ts | 1 + 9 files changed, 116 insertions(+), 65 deletions(-) create mode 100644 config-serializer/config-items/KeystrokeWithModifiersAction.ts diff --git a/config-serializer/config-items/DualRoleKeystrokeAction.ts b/config-serializer/config-items/DualRoleKeystrokeAction.ts index 653b3b92..b9f6ad8d 100644 --- a/config-serializer/config-items/DualRoleKeystrokeAction.ts +++ b/config-serializer/config-items/DualRoleKeystrokeAction.ts @@ -1,9 +1,23 @@ +enum LongPressAction { + leftCtrl, + leftShift, + leftAlt, + leftSuper, + rightCtrl, + rightShift, + rightAlt, + rightSuper, + mod, + fn, + mouse +} + class DualRoleKeystrokeAction extends KeyAction implements Serializable { static keyActionTypeString = 'dualRoleKeystroke'; public scancode; - private _longPressAction: KeyActionId; + private _longPressAction: LongPressAction; get longPressAction(): number { return this._longPressAction; @@ -17,32 +31,34 @@ class DualRoleKeystrokeAction extends KeyAction implements Serializable static keyActionTypeString = 'keystroke'; - modifierMask: number; - private _scancode: number; get scancode(): number { @@ -18,14 +16,14 @@ class KeystrokeAction extends KeyAction implements Serializable } fromJsObject(jsObject: any): KeystrokeAction { + this.assertKeyActionType(jsObject, KeystrokeAction.keyActionTypeString, 'KeystrokeAction'); this.scancode = jsObject.scancode; - this.modifierMask = jsObject.modifierMask; return this; } fromBinary(buffer: UhkBuffer): KeystrokeAction { + this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeAction, 'KeystrokeAction'); this.scancode = buffer.readUInt8(); - this.modifierMask = buffer.readUInt8(); return this; } @@ -33,12 +31,11 @@ class KeystrokeAction extends KeyAction implements Serializable return { keyActionType: KeystrokeAction.keyActionTypeString, scancode: this.scancode, - modifierMask: this.modifierMask }; } toBinary(buffer: UhkBuffer) { + buffer.writeUInt8(KeyActionId.KeystrokeAction); buffer.writeUInt8(this.scancode); - buffer.writeUInt8(this.modifierMask); } } diff --git a/config-serializer/config-items/KeystrokeWithModifiersAction.ts b/config-serializer/config-items/KeystrokeWithModifiersAction.ts new file mode 100644 index 00000000..1e18d0e7 --- /dev/null +++ b/config-serializer/config-items/KeystrokeWithModifiersAction.ts @@ -0,0 +1,48 @@ +class KeystrokeWithModifiersAction extends KeyAction implements Serializable { + + static keyActionTypeString = 'keystrokeWithModifiers'; + + modifierMask: number; + + private _scancode: number; + + get scancode(): number { + return this._scancode; + } + + set scancode(value) { + if (!TypeChecker.isScancodeValid(value)) { + throw 'Invalid KeystrokeWithModifiersAction.scancode: ${scancode}'; + } + this._scancode = value; + } + + fromJsObject(jsObject: any): KeystrokeWithModifiersAction { + this.assertKeyActionType( + jsObject, KeystrokeWithModifiersAction.keyActionTypeString, 'KeystrokeWithModifiersAction'); + this.scancode = jsObject.scancode; + this.modifierMask = jsObject.modifierMask; + return this; + } + + fromBinary(buffer: UhkBuffer): KeystrokeWithModifiersAction { + this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeWithModifiersAction, 'KeystrokeWithModifiersAction'); + this.scancode = buffer.readUInt8(); + this.modifierMask = buffer.readUInt8(); + return this; + } + + toJsObject(): any { + return { + keyActionType: KeystrokeWithModifiersAction.keyActionTypeString, + scancode: this.scancode, + modifierMask: this.modifierMask + }; + } + + toBinary(buffer: UhkBuffer) { + buffer.writeUInt8(KeyActionId.KeystrokeWithModifiersAction); + buffer.writeUInt8(this.scancode); + buffer.writeUInt8(this.modifierMask); + } +} diff --git a/config-serializer/config-items/MouseAction.ts b/config-serializer/config-items/MouseAction.ts index 540cadce..17741e8e 100644 --- a/config-serializer/config-items/MouseAction.ts +++ b/config-serializer/config-items/MouseAction.ts @@ -35,15 +35,13 @@ class MouseAction extends KeyAction implements Serializable { } fromJsObject(jsObject: any): MouseAction { + this.assertKeyActionType(jsObject, MouseAction.keyActionTypeString, 'MouseAction'); this.mouseAction = jsObject.mouseAction; return this; } fromBinary(buffer: UhkBuffer): MouseAction { - let keyActionId = buffer.readUInt8(); - if (keyActionId !== KeyActionId.MouseAction) { - throw 'Invalid MouseAction.id: ${keyActionId}'; - } + this.readAndAssertKeyActionId(buffer, KeyActionId.MouseAction, 'MouseAction'); this.mouseAction = buffer.readUInt8(); if (!MouseAction.isMouseActionValid(this.mouseAction)) { diff --git a/config-serializer/config-items/NoneAction.ts b/config-serializer/config-items/NoneAction.ts index 9791f75b..ff760216 100644 --- a/config-serializer/config-items/NoneAction.ts +++ b/config-serializer/config-items/NoneAction.ts @@ -4,17 +4,12 @@ class NoneAction extends KeyAction implements Serializable { static noneActionParam = 0; fromJsObject(jsObject: any): NoneAction { - if (jsObject.keyActionType !== NoneAction.keyActionTypeString) { - throw 'Invalid NoneAction.keyActionType: "${jsObject.keyActionType}"'; - } + this.assertKeyActionType(jsObject, NoneAction.keyActionTypeString, 'NoneAction'); return this; } fromBinary(buffer: UhkBuffer): NoneAction { - let keyActionId = buffer.readUInt8(); - if (keyActionId !== KeyActionId.NoneAction) { - throw 'Invalid NoneAction.id: ${keyActionId}'; - } + this.readAndAssertKeyActionId(buffer, KeyActionId.NoneAction, 'NoneAction'); let keyActionParam = buffer.readUInt8(); if (keyActionParam !== NoneAction.noneActionParam) { @@ -32,6 +27,5 @@ class NoneAction extends KeyAction implements Serializable { toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.NoneAction); - buffer.writeUInt8(NoneAction.noneActionParam); } } diff --git a/config-serializer/config-items/PlayMacroAction.ts b/config-serializer/config-items/PlayMacroAction.ts index a1eea66f..cc021bba 100644 --- a/config-serializer/config-items/PlayMacroAction.ts +++ b/config-serializer/config-items/PlayMacroAction.ts @@ -16,20 +16,13 @@ class PlayMacroAction extends KeyAction implements Serializable } fromJsObject(jsObject: any): PlayMacroAction { - if (jsObject.keyActionType !== PlayMacroAction.keyActionTypeString) { - throw 'Invalid PlayMacroAction.keyActionType: "${jsObject.keyActionType}"'; - } - + this.assertKeyActionType(jsObject, PlayMacroAction.keyActionTypeString, 'PlayMacroAction'); this.macroId = jsObject.macroId; return this; } fromBinary(buffer: UhkBuffer): PlayMacroAction { - let keyActionId = buffer.readUInt8(); - if (keyActionId !== KeyActionId.PlayMacroAction) { - throw 'Invalid PlayMacroAction.keyActionId: ${keyActionId}'; - } - + this.readAndAssertKeyActionId(buffer, KeyActionId.PlayMacroAction, 'PlayMacroAction'); this.macroId = buffer.readUInt8(); return this; } diff --git a/config-serializer/config-items/SwitchKeymapAction.ts b/config-serializer/config-items/SwitchKeymapAction.ts index 2f90fabf..9749786f 100644 --- a/config-serializer/config-items/SwitchKeymapAction.ts +++ b/config-serializer/config-items/SwitchKeymapAction.ts @@ -16,11 +16,13 @@ class SwitchKeymapAction extends KeyAction implements Serializable /// +/// /// /// ///