Make the first byte of KeyAction items purely denote their type. Make the binary representation of KeyAction items variable-length.

This commit is contained in:
László Monda
2016-03-30 17:19:09 +02:00
parent 4bd1c8b3e9
commit 5b6f62c113
9 changed files with 116 additions and 65 deletions

View File

@@ -1,9 +1,23 @@
enum LongPressAction {
leftCtrl,
leftShift,
leftAlt,
leftSuper,
rightCtrl,
rightShift,
rightAlt,
rightSuper,
mod,
fn,
mouse
}
class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRoleKeystrokeAction> {
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<DualRole
}
static isDualRoleKeystrokeActionValid(keyActionIdParam): boolean {
return KeyActionId.DualRoleActionLeftCtrlLongPressAction <= keyActionIdParam &&
keyActionIdParam <= KeyActionId.DualRoleActionMouseLongPressAction;
return MouseActionParam[keyActionIdParam] !== undefined;
}
fromJsObject(jsObject: any): DualRoleKeystrokeAction {
this.longPressAction = jsObject.longPressAction;
this.assertKeyActionType(jsObject, DualRoleKeystrokeAction.keyActionTypeString, 'DualRoleKeystrokeAction');
this.scancode = jsObject.scancode;
this.longPressAction = jsObject.longPressAction;
return this;
}
fromBinary(buffer: UhkBuffer): DualRoleKeystrokeAction {
this.longPressAction = buffer.readUInt8();
this.readAndAssertKeyActionId(buffer, KeyActionId.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction');
this.scancode = buffer.readUInt8();
this.longPressAction = buffer.readUInt8();
return this;
}
toJsObject(): any {
return {
keyActionType: DualRoleKeystrokeAction.keyActionTypeString,
longPressAction: KeyActionId[this.longPressAction],
scancode: this.scancode
scancode: this.scancode,
longPressAction: KeyActionId[this.longPressAction]
};
}
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(this.longPressAction);
buffer.writeUInt8(KeyActionId.DualRoleKeystrokeAction);
buffer.writeUInt8(this.scancode);
buffer.writeUInt8(this.longPressAction);
}
}

View File

@@ -1,27 +1,15 @@
// A KeyAction is composed of 2 bytes in the RAM of the UHK: id byte, and param byte.
// Id denotes the subclass of the KeyAction and param is subclass-specific.
// A KeyAction is composed of 3 bytes in the RAM and up to 3 bytes in the EEPROM of the UHK.
// The first byte denotes the subclass of the KeyAction and its length in the EEPROM.
enum KeyActionId {
NoneAction = 0,
KeyStrokeActionFirst = TypeChecker.firstValidScancode, // 1
// Intermediary scancodes = 2 to 230
KeyStrokeActionLast = TypeChecker.lastValidScancode, // 231
SwitchLayerAction = 232,
SwitchKeymapAction = 233,
MouseAction = 234,
PlayMacroAction = 235,
DualRoleActionLeftCtrlLongPressAction = 236,
DualRoleActionLeftShiftLongPressAction = 237,
DualRoleActionLeftAltLongPressAction = 238,
DualRoleActionLeftSuperLongPressAction = 239,
DualRoleActionRightCtrlLongPressAction = 240,
DualRoleActionRightShiftLongPressAction = 241,
DualRoleActionRightAltLongPressAction = 242,
DualRoleActionRightSuperLongPressAction = 243,
DualRoleActionModLongPressAction = 244,
DualRoleActionFnLongPressAction = 245,
DualRoleActionMouseLongPressAction = 246
// Let's leave space for further layers - additional actions should descend from 255
NoneAction = 0,
KeystrokeAction = 1,
KeystrokeWithModifiersAction = 2,
DualRoleKeystrokeAction = 3,
SwitchLayerAction = 4,
SwitchKeymapAction = 5,
MouseAction = 6,
PlayMacroAction = 7
}
class KeyAction {
@@ -43,14 +31,28 @@ class KeyAction {
let keyActionFirstByte = buffer.readUInt8();
buffer.backtrack();
if (TypeChecker.isScancodeValid(keyActionFirstByte)) {
return new KeystrokeAction().fromBinary(buffer);
} else if (keyActionFirstByte === KeyActionId.MouseAction) {
return new MouseAction().fromBinary(buffer);
} else if (keyActionFirstByte === KeyActionId.NoneAction) {
return new NoneAction().fromBinary(buffer);
} else {
throw 'Invalid KeyAction first byte "${keyActionFirstByte}"';
switch (keyActionFirstByte) {
case KeyActionId.KeystrokeAction:
return new KeystrokeAction().fromBinary(buffer);
case KeyActionId.MouseAction:
return new MouseAction().fromBinary(buffer);
case KeyActionId.NoneAction:
return new NoneAction().fromBinary(buffer);
default:
throw 'Invalid KeyAction first byte "${keyActionFirstByte}"';
}
}
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
if (jsObject.keyActionType !== keyActionTypeString) {
throw 'Invalid ${classname}.id: ${jsObject.keyActionType}';
}
}
readAndAssertKeyActionId(buffer: UhkBuffer, keyActionIdParam: KeyActionId, classname: string) {
let readKeyActionId = buffer.readUInt8();
if (readKeyActionId !== keyActionIdParam) {
throw 'Invalid ${classname} first byte: ${keyActionId}';
}
}
}

View File

@@ -2,8 +2,6 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
static keyActionTypeString = 'keystroke';
modifierMask: number;
private _scancode: number;
get scancode(): number {
@@ -18,14 +16,14 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
}
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<KeystrokeAction>
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);
}
}

View File

@@ -0,0 +1,48 @@
class KeystrokeWithModifiersAction extends KeyAction implements Serializable<KeystrokeWithModifiersAction> {
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);
}
}

View File

@@ -35,15 +35,13 @@ class MouseAction extends KeyAction implements Serializable<MouseAction> {
}
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)) {

View File

@@ -4,17 +4,12 @@ class NoneAction extends KeyAction implements Serializable<NoneAction> {
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<NoneAction> {
toBinary(buffer: UhkBuffer) {
buffer.writeUInt8(KeyActionId.NoneAction);
buffer.writeUInt8(NoneAction.noneActionParam);
}
}

View File

@@ -16,20 +16,13 @@ class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction>
}
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;
}

View File

@@ -16,11 +16,13 @@ class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapA
}
fromJsObject(jsObject: any): SwitchKeymapAction {
this.assertKeyActionType(jsObject, SwitchKeymapAction.keyActionTypeString, 'SwitchKeymapAction');
this.keymapId = jsObject.keymapId;
return this;
}
fromBinary(buffer: UhkBuffer): SwitchKeymapAction {
this.readAndAssertKeyActionId(buffer, KeyActionId.SwitchKeymapAction, 'SwitchKeymapAction');
this.keymapId = buffer.readUInt8();
return this;
}

View File

@@ -1,5 +1,6 @@
/// <reference path="KeyAction.ts" />
/// <reference path="KeystrokeAction.ts" />
/// <reference path="KeystrokeWithModifiersAction.ts" />
/// <reference path="DualRoleKeystrokeAction.ts" />
/// <reference path="MouseAction.ts" />
/// <reference path="PlayMacroAction.ts" />