Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial test get keycode version #205

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ const BufferSizeUsage = () => {
if (!connectedDevice || !api) {
return null;
}
const {protocol} = connectedDevice;
const macroApi = getMacroAPI(protocol, api);
const {protocol, keycodeVersion} = connectedDevice;
const macroApi = getMacroAPI(protocol, keycodeVersion, api);
const bytesUsed = macroApi.rawKeycodeSequencesToMacroBytes(ast).length;
return (
<ProgressBarContainer>
Expand Down
4 changes: 4 additions & 0 deletions src/components/panes/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ export const Settings = () => {
<Label>VIA Firmware Protocol</Label>
<Detail>{selectedDevice.protocol}</Detail>
</ControlRow>
<ControlRow>
<Label>Keycode Version</Label>
<Detail>{selectedDevice.keycodeVersion}</Detail>
</ControlRow>
</DiagnosticContainer>
) : null}
</SpanOverflowCell>
Expand Down
1 change: 1 addition & 0 deletions src/store/definitionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const getBasicKeyToByte = createSelector(
(connectedDevice) => {
const basicKeyToByte = getBasicKeyDict(
connectedDevice ? connectedDevice.protocol : 0,
connectedDevice ? connectedDevice.keycodeVersion : 0,
);
return {basicKeyToByte, byteToKey: getByteToKey(basicKeyToByte)};
},
Expand Down
26 changes: 26 additions & 0 deletions src/store/devicesThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ export const reloadConnectedDevices =
});
}

const keycodeVersions = await Promise.all(
recognisedDevices.map((device) =>
new KeyboardAPI(device.path).getKeycodeVersion(),
),
);

const recognisedDevicesWithBadKeycodeVersion = recognisedDevices.filter(
(_, i) => keycodeVersions[i] === -1,
);

if (recognisedDevicesWithBadKeycodeVersion.length) {
// Should we exit early??
recognisedDevicesWithBadKeycodeVersion.forEach((device: WebVIADevice) => {
const deviceInfo = extractDeviceInfo(device);
dispatch(
logAppError({
message: 'Received invalid keycode version from device',
deviceInfo,
}),
);
});
}

const authorizedDevices: AuthorizedDevice[] = recognisedDevices
.filter((_, i) => protocolVersions[i] !== -1)
.map((device, idx) => {
Expand All @@ -161,6 +184,7 @@ export const reloadConnectedDevices =
productId,
vendorId,
protocol,
keycodeVersion: protocol >= 13 ? keycodeVersions[idx] : 0,
productName,
hasResolvedDefinition: false,
requiredDefinitionVersion: protocol >= 11 ? 'v3' : 'v2',
Expand All @@ -173,6 +197,8 @@ export const reloadConnectedDevices =

await dispatch(reloadDefinitions(authorizedDevices));



const newDefinitions = getDefinitions(getState());
const connectedDevices = authorizedDevices
.filter((device, i) =>
Expand Down
1 change: 1 addition & 0 deletions src/store/errorsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const extractDeviceInfo = (device: DeviceInfo): DeviceInfo => ({
vendorId: device.vendorId,
productName: device.productName,
protocol: device.protocol,
keycodeVersion: device.keycodeVersion,
});

type ErrorsState = {
Expand Down
8 changes: 4 additions & 4 deletions src/store/macrosSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ export default macrosSlice.reducer;
export const loadMacros =
(connectedDevice: ConnectedDevice): AppThunk =>
async (dispatch, getState) => {
const {protocol} = connectedDevice;
const {protocol, keycodeVersion} = connectedDevice;
if (protocol < 8) {
dispatch(setMacrosNotSupported());
} else {
try {
const state = getState();
const api = getSelectedKeyboardAPI(state) as KeyboardAPI;
const macroApi = getMacroAPI(protocol, api);
const macroApi = getMacroAPI(protocol, keycodeVersion, api);
if (macroApi) {
const sequences = await macroApi.readRawKeycodeSequences();
const macroBufferSize = await api.getMacroBufferSize();
Expand All @@ -92,8 +92,8 @@ export const saveMacros =
async (dispatch, getState) => {
const state = getState();
const api = getSelectedKeyboardAPI(state) as KeyboardAPI;
const {protocol} = connectedDevice;
const macroApi = getMacroAPI(protocol, api);
const {protocol, keycodeVersion} = connectedDevice;
const macroApi = getMacroAPI(protocol, keycodeVersion, api);
if (macroApi) {
const sequences = macros.map((expression) => {
const optimizedSequence = expressionToSequence(expression);
Expand Down
3 changes: 3 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type DeviceInfo = {
productId: number;
productName: string;
protocol?: number;
keycodeVersion?: number;
};

export type Device = DeviceInfo & {
Expand All @@ -52,6 +53,7 @@ export type AuthorizedDevice = DeviceInfo & {
path: string;
vendorProductId: number;
protocol: number;
keycodeVersion: number;
requiredDefinitionVersion: DefinitionVersion;
hasResolvedDefinition: false;
};
Expand All @@ -60,6 +62,7 @@ export type ConnectedDevice = DeviceInfo & {
path: string;
vendorProductId: number;
protocol: number;
keycodeVersion: number;
requiredDefinitionVersion: DefinitionVersion;
hasResolvedDefinition: true;
};
Expand Down
40 changes: 27 additions & 13 deletions src/utils/key-to-byte/dictionary-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ import basicKeyToByte from './default';
import v10BasicKeyToByte from './v10';
import v11BasicKeyToByte from './v11';
import v12BasicKeyToByte from './v12';
export function getBasicKeyDict(version: number) {
switch (version) {
case 13:
case 12: {
return v12BasicKeyToByte;
export function getBasicKeyDict(protocol: number, keycodeVersion: number) {
if (protocol <= 12) {
switch (protocol) {
case 12: {
return v12BasicKeyToByte;
}
case 11: {
return v11BasicKeyToByte;
}
case 10: {
return v10BasicKeyToByte;
}
default: {
return basicKeyToByte;
}
}
case 11: {
return v11BasicKeyToByte;
}
case 10: {
return v10BasicKeyToByte;
}
default: {
return basicKeyToByte;
}
else
{
switch(keycodeVersion) {
// dummy to check the keycode ver too
case 2:
case 1: {
return v12BasicKeyToByte;
}
default: {
return v12BasicKeyToByte;
}
}
}
}
21 changes: 21 additions & 0 deletions src/utils/keyboard-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ enum APICommand {
DYNAMIC_KEYMAP_SET_BUFFER = 0x13,
DYNAMIC_KEYMAP_GET_ENCODER = 0x14,
DYNAMIC_KEYMAP_SET_ENCODER = 0x15,

GET_KEYCODE_VERSION = 0x15,

// DEPRECATED:
BACKLIGHT_CONFIG_SET_VALUE = 0x07,
Expand Down Expand Up @@ -105,6 +107,16 @@ export const shiftFrom16Bit = (value: number): [number, number] => [
value & 255,
];

export const shiftTo32Bit = ([msb, hi, lo, lsb]: [number, number, number, number]): number =>
(msb << 24) | (hi << 16) | (lo << 8) | lsb;

export const shiftFrom32Bit = (value: number): [number, number, number, number] => [
value >> 24,
value >> 16,
value >> 8,
value & 255,
];

const shiftBufferTo16Bit = (buffer: number[]): number[] => {
const shiftedBuffer = [];
for (let i = 0; i < buffer.length; i += 2) {
Expand Down Expand Up @@ -174,6 +186,15 @@ export class KeyboardAPI {
return -1;
}
}

async getKeycodeVersion() {
try {
const [, msb, hi, lo, lsb] = await this.hidCommand(APICommand.GET_KEYCODE_VERSION);
return shiftTo32Bit([msb, hi, lo, lsb]);
} catch (e) {
return -1;
}
}

async getKey(layer: Layer, row: Row, col: Column) {
const buffer = await this.hidCommand(
Expand Down
6 changes: 3 additions & 3 deletions src/utils/macro-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import type {KeyboardAPI} from '../keyboard-api';
import {MacroAPI, validateMacroExpression} from './macro-api';
import {MacroAPIV11, validateMacroExpressionV11} from './macro-api.v11';

export const getMacroAPI = (protocol: number, keyboardApi: KeyboardAPI) => {
const basicKeyToByte = getBasicKeyDict(protocol);
const byteToKey = getByteToKey(getBasicKeyDict(protocol));
export const getMacroAPI = (protocol: number, keycodeVersion: number, keyboardApi: KeyboardAPI) => {
const basicKeyToByte = getBasicKeyDict(protocol, keycodeVersion);
const byteToKey = getByteToKey(getBasicKeyDict(protocol, keycodeVersion));
return protocol >= 11
? new MacroAPIV11(keyboardApi, basicKeyToByte, byteToKey)
: new MacroAPI(keyboardApi, basicKeyToByte, byteToKey);
Expand Down