[Keyboard, Android] Generate keyboard codes (#104032)

This commit is contained in:
Tong Mu
2022-05-19 18:38:06 -07:00
committed by GitHub
parent 23bc3d64eb
commit a92f0ef14f
7 changed files with 255 additions and 33 deletions

View File

@@ -5,6 +5,7 @@
import 'package:path/path.dart' as path;
import 'base_code_gen.dart';
import 'constants.dart';
import 'logical_key_data.dart';
import 'physical_key_data.dart';
import 'utils.dart';
@@ -39,6 +40,62 @@ class AndroidCodeGenerator extends PlatformCodeGenerator {
return androidScanCodeMap.toString().trimRight();
}
String get _pressingGoals {
final OutputLines<int> lines = OutputLines<int>('Android pressing goals');
const Map<String, List<String>> goalsSource = <String, List<String>>{
'SHIFT': <String>['ShiftLeft', 'ShiftRight'],
'CTRL': <String>['ControlLeft', 'ControlRight'],
'ALT': <String>['AltLeft', 'AltRight'],
};
goalsSource.forEach((String flagName, List<String> keys) {
int? lineId;
final List<String> keysString = keys.map((String keyName) {
final PhysicalKeyEntry physicalKey = keyData.entryByName(keyName);
final LogicalKeyEntry logicalKey = logicalData.entryByName(keyName);
lineId ??= physicalKey.usbHidCode;
return ' new KeyPair(${toHex(physicalKey.usbHidCode)}L, '
'${toHex(logicalKey.value, digits: 10)}L), // ${physicalKey.name}';
}).toList();
lines.add(lineId!,
' new PressingGoal(\n'
' KeyEvent.META_${flagName}_ON,\n'
' new KeyPair[] {\n'
'${keysString.join('\n')}\n'
' }),');
});
return lines.sortedJoin().trimRight();
}
String get _togglingGoals {
final OutputLines<int> lines = OutputLines<int>('Android toggling goals');
const Map<String, String> goalsSource = <String, String>{
'CAPS_LOCK': 'CapsLock',
};
goalsSource.forEach((String flagName, String keyName) {
final PhysicalKeyEntry physicalKey = keyData.entryByName(keyName);
final LogicalKeyEntry logicalKey = logicalData.entryByName(keyName);
lines.add(physicalKey.usbHidCode,
' new TogglingGoal(KeyEvent.META_${flagName}_ON, '
'${toHex(physicalKey.usbHidCode)}L, '
'${toHex(logicalKey.value, digits: 10)}L),');
});
return lines.sortedJoin().trimRight();
}
/// This generates the mask values for the part of a key code that defines its plane.
String get _maskConstants {
final StringBuffer buffer = StringBuffer();
const List<MaskConstant> maskConstants = <MaskConstant>[
kValueMask,
kUnicodePlane,
kAndroidPlane,
];
for (final MaskConstant constant in maskConstants) {
buffer.writeln(' public static final long k${constant.upperCamelName} = ${toHex(constant.value, digits: 11)}L;');
}
return buffer.toString().trimRight();
}
@override
String get templatePath => path.join(dataRoot, 'android_keyboard_map_java.tmpl');
@@ -51,6 +108,9 @@ class AndroidCodeGenerator extends PlatformCodeGenerator {
return <String, String>{
'ANDROID_SCAN_CODE_MAP': _androidScanCodeMap,
'ANDROID_KEY_CODE_MAP': _androidKeyCodeMap,
'PRESSING_GOALS': _pressingGoals,
'TOGGLING_GOALS': _togglingGoals,
'MASK_CONSTANTS': _maskConstants,
};
}
}

View File

@@ -0,0 +1,63 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:path/path.dart' as path;
import 'base_code_gen.dart';
import 'logical_key_data.dart';
import 'physical_key_data.dart';
import 'utils.dart';
String _toUpperSnake(String lowerCammel) {
// Converts 'myTVFoo' to 'myTvFoo'.
final String trueUpperCammel = lowerCammel.replaceAllMapped(
RegExp(r'([A-Z]{3,})'),
(Match match) {
final String matched = match.group(1)!;
return matched.substring(0, 1)
+ matched.substring(1, matched.length - 2).toLowerCase()
+ matched.substring(matched.length - 2, matched.length - 1);
});
// Converts 'myTvFoo' to 'MY_TV_FOO'.
return trueUpperCammel.replaceAllMapped(
RegExp(r'([A-Z])'),
(Match match) => '_${match.group(1)!}').toUpperCase();
}
/// Generates the common/testing/key_codes.h based on the information in the key
/// data structure given to it.
class KeyCodesJavaGenerator extends BaseCodeGenerator {
KeyCodesJavaGenerator(super.keyData, super.logicalData);
/// Gets the generated definitions of PhysicalKeyboardKeys.
String get _physicalDefinitions {
final OutputLines<int> lines = OutputLines<int>('Physical Key list');
for (final PhysicalKeyEntry entry in keyData.entries) {
lines.add(entry.usbHidCode, '''
public static final long PHYSICAL_${_toUpperSnake(entry.constantName)} = ${toHex(entry.usbHidCode)}l;''');
}
return lines.sortedJoin().trimRight();
}
/// Gets the generated definitions of PhysicalKeyboardKeys.
String get _logicalDefinitions {
final OutputLines<int> lines = OutputLines<int>('Logical Key list');
for (final LogicalKeyEntry entry in logicalData.entries) {
lines.add(entry.value, '''
public static final long LOGICAL_${_toUpperSnake(entry.constantName)} = ${toHex(entry.value, digits: 11)}l;''');
}
return lines.sortedJoin().trimRight();
}
@override
String get templatePath => path.join(dataRoot, 'key_codes_java.tmpl');
@override
Map<String, String> mappings() {
return <String, String>{
'LOGICAL_KEY_DEFINITIONS': _logicalDefinitions,
'PHYSICAL_KEY_DEFINITIONS': _physicalDefinitions,
};
}
}