New scheme for keyboard logical key ID (#85121)
This PR updates the ID used by logical keyboard keys. The logical key ID is still composed of 2 parts: 32 bits of value, and 8 bits of plane. But the assignment of planes has been drastically changed. HID plane is removed, and unprintable plane and Flutter plane are added. This is to reflect the new generation method for logical key IDs. Now keys that are defined by Flutter but not by dom_key_data are placed into the Flutter plane, including numpad keys, sided modifier keys, and gamepad keys. The values for platform planes have also been adjusted. The generation script and README have been updated accordingly as well. A new file, test_utils/key_codes.h is now generated to assist engine unit testing. All lists generated by the script are now sorted by the key.
This commit is contained in:
@@ -2,147 +2,107 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
/* === LOGICAL KEY BITMASKS === */
|
||||
// The following constants are used to composite the value for a Flutter logical
|
||||
// key.
|
||||
//
|
||||
// A Flutter logical key supports up to 53-bits, as limited by JavaScript
|
||||
// integers.
|
||||
|
||||
/* Bit 31-0: Value */
|
||||
// The lower 32 bits represent different values within a plane.
|
||||
|
||||
/// Mask for the 32-bit value portion of the code.
|
||||
const int kValueMask = 0x000FFFFFFFF;
|
||||
|
||||
|
||||
/* Bit 39-32: Source of value */
|
||||
// The next 8 bits represent where the values come from, consequently how to
|
||||
// interpret them.
|
||||
|
||||
/// Mask for bit #39-32 portion of the code, indicating the source of the value.
|
||||
const int kPlatformMask = 0x0FF00000000;
|
||||
|
||||
/// The code prefix for keys that have a Unicode representation, generated from
|
||||
/// their code points.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [kUnprintablePlane], for keys that are also generated from code points but
|
||||
/// unprintable.
|
||||
const int kUnicodePlane = 0x00000000000;
|
||||
|
||||
/// The code prefix for keys generated from their USB HID usages.
|
||||
const int kHidPlane = 0x00100000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to Android, generated
|
||||
/// from platform-specific codes.
|
||||
const int kAndroidPlane = 0x00200000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to Fuchsia, generated
|
||||
/// from platform-specific codes.
|
||||
const int kFuchsiaPlane = 0x00300000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to iOS, generated
|
||||
/// from platform-specific codes.
|
||||
const int kIosPlane = 0x00400000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to macOS, generated
|
||||
/// from platform-specific codes.
|
||||
const int kMacOsPlane = 0x00500000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to Gtk, generated from
|
||||
/// platform-specific codes.
|
||||
const int kGtkPlane = 0x00600000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to Windows, generated
|
||||
/// from platform-specific codes.
|
||||
const int kWindowsPlane = 0x00700000000;
|
||||
|
||||
/// The code prefix for unrecognized keys that are unique to Web, generated from
|
||||
/// platform-specific codes.
|
||||
const int kWebPlane = 0x00800000000;
|
||||
|
||||
/// The code prefix for keys that do not have a Unicode representation, generated
|
||||
/// from their code points.
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [kUnicodePlane], for keys that are also generated from code points but
|
||||
/// printable.
|
||||
const int kUnprintablePlane = 0x01000000000;
|
||||
|
||||
|
||||
/* Bit 43-40: Key variation */
|
||||
// The next 4 bits represent keys that have multiple variations, such as a left
|
||||
// and right variation of a modifier key, or a Numpad variation of a digit key.
|
||||
|
||||
/// Mask for bit #43-40 portion of the code, indicating the variation of the key.
|
||||
const int kVariationMask = 0xF0000000000;
|
||||
|
||||
/// The code prefix for pseudo-keys which represent collections of key synonyms,
|
||||
/// such as Control.
|
||||
const int kSynonymPlane = 0x20000000000;
|
||||
|
||||
/// The code prefix for the left modifier of a pair of modifiers, such as Control
|
||||
/// Left.
|
||||
const int kLeftModifierPlane = 0x30000000000;
|
||||
|
||||
/// The code prefix for the right modifier of a pair of modifiers, such as Control
|
||||
/// Right.
|
||||
const int kRightModifierPlane = 0x40000000000;
|
||||
|
||||
/// The code prefix for a Numpad key, such as Numpad 1.
|
||||
const int kNumpadPlane = 0x50000000000;
|
||||
import 'utils.dart';
|
||||
|
||||
class MaskConstant {
|
||||
const MaskConstant({required this.name, required this.value, required this.description});
|
||||
|
||||
const MaskConstant.platform({required String platform, required int value})
|
||||
: this(
|
||||
name: '$platform Plane',
|
||||
value: value,
|
||||
description: 'The plane value for the private keys defined by the $platform embedding.'
|
||||
);
|
||||
|
||||
final String name;
|
||||
final int value;
|
||||
final List<String> description;
|
||||
final String description;
|
||||
|
||||
String get upperCamelName {
|
||||
return name
|
||||
.split(' ')
|
||||
.map<String>((String word) => lowerCamelToUpperCamel(word.toLowerCase()))
|
||||
.join('');
|
||||
}
|
||||
|
||||
String get lowerCamelName {
|
||||
final String upperCamel = upperCamelName;
|
||||
return upperCamel.substring(0, 1).toLowerCase() + upperCamel.substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
const List<MaskConstant> maskConstants = <MaskConstant>[
|
||||
MaskConstant(
|
||||
name: 'valueMask',
|
||||
value: kValueMask,
|
||||
description: <String>[
|
||||
'Mask for the 32-bit value portion of the key code.',
|
||||
'This is used by platform-specific code to generate Flutter key codes.',
|
||||
],
|
||||
),
|
||||
MaskConstant(
|
||||
name: 'platformMask',
|
||||
value: kPlatformMask,
|
||||
description: <String>[
|
||||
'Mask for the platform prefix portion of the key code.',
|
||||
'This is used by platform-specific code to generate Flutter key codes.'
|
||||
],
|
||||
),
|
||||
MaskConstant(
|
||||
name: 'unicodePlane',
|
||||
value: kUnicodePlane,
|
||||
description: <String>[
|
||||
'The code prefix for keys which have a Unicode representation.',
|
||||
'This is used by platform-specific code to generate Flutter key codes.'
|
||||
],
|
||||
),
|
||||
MaskConstant(
|
||||
name: 'synonymMask',
|
||||
value: kSynonymPlane,
|
||||
description: <String>[
|
||||
'Mask for the synonym pseudo-keys generated for keys which appear in more than one place on the keyboard.',
|
||||
'IDs in this range are used to represent keys which appear in multiple places on the keyboard, such as the SHIFT, ALT, CTRL, and numeric keypad keys. These key codes will never be generated by the key event system, but may be used in key maps to represent the union of all the keys of each type in order to match them.',
|
||||
'To look up the synonyms that are defined, look in the [synonyms] map.'
|
||||
],
|
||||
),
|
||||
MaskConstant(
|
||||
name: 'hidPlane',
|
||||
value: kHidPlane,
|
||||
description: <String>[
|
||||
'The code prefix for keys which do not have a Unicode representation.',
|
||||
'This is used by platform-specific code to generate Flutter key codes using HID Usage codes.'
|
||||
],
|
||||
),
|
||||
];
|
||||
const MaskConstant kValueMask = MaskConstant(
|
||||
name: 'Value Mask',
|
||||
value: 0x00FFFFFFFF,
|
||||
description: 'Mask for the 32-bit value portion of the key code.',
|
||||
);
|
||||
|
||||
const MaskConstant kPlaneMask = MaskConstant(
|
||||
name: 'Plane Mask',
|
||||
value: 0xFF00000000,
|
||||
description: 'Mask for the plane prefix portion of the key code.',
|
||||
);
|
||||
|
||||
const MaskConstant kUnicodePlane = MaskConstant(
|
||||
name: 'Unicode Plane',
|
||||
value: 0x0000000000,
|
||||
description: 'The plane value for keys which have a Unicode representation.',
|
||||
);
|
||||
|
||||
const MaskConstant kUnprintablePlane = MaskConstant(
|
||||
name: 'Unprintable Plane',
|
||||
value: 0x0100000000,
|
||||
description: 'The plane value for keys defined by Chromium and does not have a Unicode representation.',
|
||||
);
|
||||
|
||||
const MaskConstant kFlutterPlane = MaskConstant(
|
||||
name: 'Flutter Plane',
|
||||
value: 0x0200000000,
|
||||
description: 'The plane value for keys defined by Flutter.',
|
||||
);
|
||||
|
||||
const MaskConstant kStartOfPlatformPlanes = MaskConstant(
|
||||
name: 'Start Of Platform Planes',
|
||||
value: 0x1100000000,
|
||||
description: 'The platform plane with the lowest mask value, beyond which the keys are considered autogenerated.',
|
||||
);
|
||||
|
||||
const MaskConstant kAndroidPlane = MaskConstant.platform(
|
||||
platform: 'Android',
|
||||
value: 0x1100000000,
|
||||
);
|
||||
|
||||
const MaskConstant kFuchsiaPlane = MaskConstant.platform(
|
||||
platform: 'Fuchsia',
|
||||
value: 0x1200000000,
|
||||
);
|
||||
|
||||
const MaskConstant kIosPlane = MaskConstant.platform(
|
||||
platform: 'iOS',
|
||||
value: 0x1300000000,
|
||||
);
|
||||
|
||||
const MaskConstant kMacosPlane = MaskConstant.platform(
|
||||
platform: 'macOS',
|
||||
value: 0x1400000000,
|
||||
);
|
||||
|
||||
const MaskConstant kGtkPlane = MaskConstant.platform(
|
||||
platform: 'Gtk',
|
||||
value: 0x1500000000,
|
||||
);
|
||||
|
||||
const MaskConstant kWindowsPlane = MaskConstant.platform(
|
||||
platform: 'Windows',
|
||||
value: 0x1600000000,
|
||||
);
|
||||
|
||||
const MaskConstant kWebPlane = MaskConstant.platform(
|
||||
platform: 'Web',
|
||||
value: 0x1700000000,
|
||||
);
|
||||
|
||||
const MaskConstant kGlfwPlane = MaskConstant.platform(
|
||||
platform: 'GLFW',
|
||||
value: 0x1800000000,
|
||||
);
|
||||
|
||||
@@ -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';
|
||||
@@ -55,27 +56,27 @@ class GtkCodeGenerator extends PlatformCodeGenerator {
|
||||
final StringBuffer result = StringBuffer();
|
||||
source.forEach((String modifierBitName, List<String> keyNames) {
|
||||
assert(keyNames.length == 2 || keyNames.length == 3);
|
||||
final String primaryLogicalName = keyNames[0];
|
||||
final String primaryPhysicalName = keyNames[1];
|
||||
final String? secondaryPhysicalName = keyNames.length == 3 ? keyNames[2] : null;
|
||||
final LogicalKeyEntry primaryLogical = logicalData.entryByName(primaryLogicalName);
|
||||
final String primaryPhysicalName = keyNames[0];
|
||||
final String primaryLogicalName = keyNames[1];
|
||||
final String? secondaryLogicalName = keyNames.length == 3 ? keyNames[2] : null;
|
||||
final PhysicalKeyEntry primaryPhysical = physicalData.entryByName(primaryPhysicalName);
|
||||
final PhysicalKeyEntry? secondaryPhysical = secondaryPhysicalName == null ? null : physicalData.entryByName(secondaryPhysicalName);
|
||||
if (secondaryPhysical == null && secondaryPhysicalName != null) {
|
||||
print('Unrecognized secondary physical key $secondaryPhysicalName specified for $debugFunctionName.');
|
||||
final LogicalKeyEntry primaryLogical = logicalData.entryByName(primaryLogicalName);
|
||||
final LogicalKeyEntry? secondaryLogical = secondaryLogicalName == null ? null : logicalData.entryByName(secondaryLogicalName);
|
||||
if (secondaryLogical == null && secondaryLogicalName != null) {
|
||||
print('Unrecognized secondary logical key $secondaryLogicalName specified for $debugFunctionName.');
|
||||
return;
|
||||
}
|
||||
final String pad = secondaryPhysical == null ? '' : ' ';
|
||||
final String pad = secondaryLogical == null ? '' : ' ';
|
||||
result.writeln('''
|
||||
|
||||
data = g_new(FlKeyEmbedderCheckedKey, 1);
|
||||
g_hash_table_insert(table, GUINT_TO_POINTER(GDK_${modifierBitName}_MASK), data);
|
||||
data->is_caps_lock = ${primaryPhysicalName == 'CapsLock' ? 'true' : 'false'};
|
||||
data->primary_logical_key = ${toHex(primaryLogical.value, digits: 11)};$pad // ${primaryLogical.constantName}
|
||||
data->primary_physical_key = ${toHex(primaryPhysical.usbHidCode, digits: 9)};$pad // ${primaryPhysical.constantName}''');
|
||||
if (secondaryPhysical != null) {
|
||||
data->primary_physical_key = ${toHex(primaryPhysical.usbHidCode, digits: 9)};$pad // ${primaryPhysical.constantName}
|
||||
data->primary_logical_key = ${toHex(primaryLogical.value, digits: 11)};$pad // ${primaryLogical.constantName}''');
|
||||
if (secondaryLogical != null) {
|
||||
result.writeln('''
|
||||
data->secondary_physical_key = ${toHex(secondaryPhysical.usbHidCode, digits: 9)}; // ${secondaryPhysical.constantName}''');
|
||||
data->secondary_logical_key = ${toHex(secondaryLogical.value, digits: 11)}; // ${secondaryLogical.constantName}''');
|
||||
}
|
||||
});
|
||||
return result.toString().trimRight();
|
||||
@@ -91,6 +92,20 @@ class GtkCodeGenerator extends PlatformCodeGenerator {
|
||||
}
|
||||
final Map<String, List<String>> _lockBitMapping;
|
||||
|
||||
/// 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,
|
||||
kGtkPlane,
|
||||
];
|
||||
for (final MaskConstant constant in maskConstants) {
|
||||
buffer.writeln('const uint64_t k${constant.upperCamelName} = ${toHex(constant.value, digits: 11)};');
|
||||
}
|
||||
return buffer.toString().trimRight();
|
||||
}
|
||||
|
||||
@override
|
||||
String get templatePath => path.join(dataRoot, 'gtk_key_mapping_cc.tmpl');
|
||||
|
||||
@@ -105,6 +120,7 @@ class GtkCodeGenerator extends PlatformCodeGenerator {
|
||||
'GTK_KEYVAL_CODE_MAP': _gtkKeyvalCodeMap,
|
||||
'GTK_MODIFIER_BIT_MAP': _gtkModifierBitMap,
|
||||
'GTK_MODE_BIT_MAP': _gtkModeBitMap,
|
||||
'MASK_CONSTANTS': _maskConstants,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,10 +25,6 @@ const List<String> kModifiersOfInterest = <String>[
|
||||
const List<String> kSpecialPhysicalKeys = <String>['CapsLock'];
|
||||
const List<String> kSpecialLogicalKeys = <String>['CapsLock'];
|
||||
|
||||
String _toConstantVariableName(String variableName) {
|
||||
return 'k${variableName[0].toUpperCase()}${variableName.substring(1)}';
|
||||
}
|
||||
|
||||
/// Generates the key mapping for iOS, based on the information in the key
|
||||
/// data structure given to it.
|
||||
class IOSCodeGenerator extends PlatformCodeGenerator {
|
||||
@@ -37,35 +33,39 @@ class IOSCodeGenerator extends PlatformCodeGenerator {
|
||||
|
||||
/// This generates the map of iOS key codes to physical keys.
|
||||
String get _scanCodeMap {
|
||||
final StringBuffer scanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('iOS scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.iOSScanCode != null) {
|
||||
scanCodeMap.writeln(' {${toHex(entry.iOSScanCode)}, ${toHex(entry.usbHidCode)}}, // ${entry.constantName}');
|
||||
lines.add(entry.iOSScanCode!,
|
||||
' {${toHex(entry.iOSScanCode)}, ${toHex(entry.usbHidCode)}}, // ${entry.constantName}');
|
||||
}
|
||||
}
|
||||
return scanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String get _keyCodeToLogicalMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('iOS keycode map');
|
||||
for (final LogicalKeyEntry entry in logicalData.entries) {
|
||||
zipStrict(entry.iOSKeyCodeValues, entry.iOSKeyCodeNames, (int iOSValue, String iOSName) {
|
||||
result.writeln(' {${toHex(iOSValue)}, ${toHex(entry.value, digits: 11)}}, // $iOSName');
|
||||
lines.add(iOSValue, ' {${toHex(iOSValue)}, ${toHex(entry.value, digits: 11)}}, // $iOSName');
|
||||
});
|
||||
}
|
||||
return result.toString().trimRight();
|
||||
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,
|
||||
kIosPlane,
|
||||
];
|
||||
for (final MaskConstant constant in maskConstants) {
|
||||
buffer.writeln('/**');
|
||||
buffer.write(constant.description
|
||||
.map((String line) => wrapString(line, prefix: ' * '))
|
||||
.join(' *\n'));
|
||||
buffer.write(wrapString(constant.description, prefix: ' * '));
|
||||
buffer.writeln(' */');
|
||||
buffer.writeln('const uint64_t ${_toConstantVariableName(constant.name)} = ${toHex(constant.value, digits: 11)};');
|
||||
buffer.writeln('const uint64_t k${constant.upperCamelName} = ${toHex(constant.value, digits: 11)};');
|
||||
buffer.writeln('');
|
||||
}
|
||||
return buffer.toString().trimRight();
|
||||
@@ -75,7 +75,8 @@ class IOSCodeGenerator extends PlatformCodeGenerator {
|
||||
String get _keyToModifierFlagMap {
|
||||
final StringBuffer modifierKeyMap = StringBuffer();
|
||||
for (final String name in kModifiersOfInterest) {
|
||||
modifierKeyMap.writeln(' {${toHex(logicalData.entryByName(name).iOSKeyCodeValues[0])}, kModifierFlag${lowerCamelToUpperCamel(name)}},');
|
||||
final String line = '{${toHex(logicalData.entryByName(name).iOSKeyCodeValues[0])}, kModifierFlag${lowerCamelToUpperCamel(name)}},';
|
||||
modifierKeyMap.writeln(' ${line.padRight(42)}// $name');
|
||||
}
|
||||
return modifierKeyMap.toString().trimRight();
|
||||
}
|
||||
@@ -84,7 +85,8 @@ class IOSCodeGenerator extends PlatformCodeGenerator {
|
||||
String get _modifierFlagToKeyMap {
|
||||
final StringBuffer modifierKeyMap = StringBuffer();
|
||||
for (final String name in kModifiersOfInterest) {
|
||||
modifierKeyMap.writeln(' {kModifierFlag${lowerCamelToUpperCamel(name)}, ${toHex(logicalData.entryByName(name).iOSKeyCodeValues[0])}},');
|
||||
final String line = '{kModifierFlag${lowerCamelToUpperCamel(name)}, ${toHex(logicalData.entryByName(name).iOSKeyCodeValues[0])}},';
|
||||
modifierKeyMap.writeln(' ${line.padRight(42)}// $name');
|
||||
}
|
||||
return modifierKeyMap.toString().trimRight();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,23 @@ String _wrapString(String input) {
|
||||
return wrapString(input, prefix: ' /// ');
|
||||
}
|
||||
|
||||
final List<MaskConstant> _maskConstants = <MaskConstant>[
|
||||
kValueMask,
|
||||
kPlaneMask,
|
||||
kUnicodePlane,
|
||||
kUnprintablePlane,
|
||||
kFlutterPlane,
|
||||
kStartOfPlatformPlanes,
|
||||
kAndroidPlane,
|
||||
kFuchsiaPlane,
|
||||
kIosPlane,
|
||||
kMacosPlane,
|
||||
kGtkPlane,
|
||||
kWindowsPlane,
|
||||
kWebPlane,
|
||||
kGlfwPlane,
|
||||
];
|
||||
|
||||
class SynonymKeyInfo {
|
||||
SynonymKeyInfo(this.keys, this.name);
|
||||
|
||||
@@ -26,7 +43,6 @@ class SynonymKeyInfo {
|
||||
// It won't end up being the same value because it'll be in the pseudo-key
|
||||
// plane.
|
||||
LogicalKeyEntry get primaryKey => keys[0];
|
||||
int get value => (primaryKey.value & ~kVariationMask) + kSynonymPlane;
|
||||
String get constantName => upperCamelToLowerCamel(name);
|
||||
}
|
||||
|
||||
@@ -37,39 +53,36 @@ class KeyboardKeysCodeGenerator extends BaseCodeGenerator {
|
||||
|
||||
/// Gets the generated definitions of PhysicalKeyboardKeys.
|
||||
String get _physicalDefinitions {
|
||||
final StringBuffer definitions = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Physical Key Definition');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
final String firstComment = _wrapString('Represents the location of the '
|
||||
'"${entry.commentName}" key on a generalized keyboard.');
|
||||
final String otherComments = _wrapString('See the function '
|
||||
'[KeyEvent.physical] for more information.');
|
||||
definitions.write('''
|
||||
|
||||
'[RawKeyEvent.physicalKey] for more information.');
|
||||
lines.add(entry.usbHidCode, '''
|
||||
$firstComment ///
|
||||
$otherComments static const PhysicalKeyboardKey ${entry.constantName} = PhysicalKeyboardKey(${toHex(entry.usbHidCode, digits: 8)});
|
||||
''');
|
||||
}
|
||||
return definitions.toString();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String get _physicalDebugNames {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Physical debug names');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
result.write('''
|
||||
${toHex(entry.usbHidCode, digits: 8)}: '${entry.commentName}',
|
||||
''');
|
||||
lines.add(entry.usbHidCode, '''
|
||||
${toHex(entry.usbHidCode, digits: 8)}: '${entry.commentName}',''');
|
||||
}
|
||||
return result.toString();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// Gets the generated definitions of LogicalKeyboardKeys.
|
||||
String get _logicalDefinitions {
|
||||
final StringBuffer definitions = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Logical debug names');
|
||||
void printKey(int flutterId, String constantName, String commentName, {String? otherComments}) {
|
||||
final String firstComment = _wrapString('Represents the logical "$commentName" key on the keyboard.');
|
||||
otherComments ??= _wrapString('See the function [KeyEvent.logical] for more information.');
|
||||
definitions.write('''
|
||||
|
||||
otherComments ??= _wrapString('See the function [RawKeyEvent.logicalKey] for more information.');
|
||||
lines.add(flutterId, '''
|
||||
$firstComment ///
|
||||
$otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardKey(${toHex(flutterId, digits: 11)});
|
||||
''');
|
||||
@@ -80,67 +93,71 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
|
||||
entry.value,
|
||||
entry.constantName,
|
||||
entry.commentName,
|
||||
otherComments: _otherComments(entry.name),
|
||||
);
|
||||
}
|
||||
for (final SynonymKeyInfo synonymInfo in synonyms) {
|
||||
// Use the first item in the synonyms as a template for the ID to use.
|
||||
// It won't end up being the same value because it'll be in the pseudo-key
|
||||
// plane.
|
||||
final Set<String> unionNames = synonymInfo.keys.map(
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String? _otherComments(String name) {
|
||||
if (synonyms.containsKey(name)) {
|
||||
final Set<String> unionNames = synonyms[name]!.keys.map(
|
||||
(LogicalKeyEntry entry) => entry.constantName).toSet();
|
||||
printKey(synonymInfo.value, synonymInfo.constantName, PhysicalKeyEntry.getCommentName(synonymInfo.name),
|
||||
otherComments: _wrapString('This key represents the union of the keys '
|
||||
return _wrapString('This key represents the union of the keys '
|
||||
'$unionNames when comparing keys. This key will never be generated '
|
||||
'directly, its main use is in defining key maps.'));
|
||||
'directly, its main use is in defining key maps.');
|
||||
}
|
||||
return definitions.toString();
|
||||
return null;
|
||||
}
|
||||
|
||||
String get _logicalSynonyms {
|
||||
final StringBuffer result = StringBuffer();
|
||||
for (final SynonymKeyInfo synonymInfo in synonyms) {
|
||||
for (final SynonymKeyInfo synonymInfo in synonyms.values) {
|
||||
for (final LogicalKeyEntry key in synonymInfo.keys) {
|
||||
final String synonymName = upperCamelToLowerCamel(synonymInfo.name);
|
||||
result.writeln(' ${key.constantName}: $synonymName,');
|
||||
final LogicalKeyEntry synonnym = logicalData.entryByName(synonymInfo.name);
|
||||
result.writeln(' ${key.constantName}: ${synonnym.constantName},');
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
String get _logicalKeyLabels {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Logical key labels');
|
||||
for (final LogicalKeyEntry entry in logicalData.entries) {
|
||||
result.write('''
|
||||
${toHex(entry.value, digits: 11)}: '${entry.commentName}',
|
||||
''');
|
||||
lines.add(entry.value, '''
|
||||
${toHex(entry.value, digits: 11)}: '${entry.commentName}',''');
|
||||
}
|
||||
for (final SynonymKeyInfo synonymInfo in synonyms) {
|
||||
result.write('''
|
||||
${toHex(synonymInfo.value)}: '${synonymInfo.name}',
|
||||
''');
|
||||
}
|
||||
return result.toString();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of USB HID codes to physical keys.
|
||||
String get _predefinedHidCodeMap {
|
||||
final StringBuffer scanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Physical key map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
scanCodeMap.writeln(' ${toHex(entry.usbHidCode)}: ${entry.constantName},');
|
||||
lines.add(entry.usbHidCode, ' ${toHex(entry.usbHidCode)}: ${entry.constantName},');
|
||||
}
|
||||
return scanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Flutter key codes to logical keys.
|
||||
String get _predefinedKeyCodeMap {
|
||||
final StringBuffer keyCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Logical key map');
|
||||
for (final LogicalKeyEntry entry in logicalData.entries) {
|
||||
keyCodeMap.writeln(' ${toHex(entry.value, digits: 11)}: ${entry.constantName},');
|
||||
lines.add(entry.value, ' ${toHex(entry.value, digits: 11)}: ${entry.constantName},');
|
||||
}
|
||||
for (final SynonymKeyInfo synonymInfo in synonyms) {
|
||||
keyCodeMap.writeln(' ${toHex(synonymInfo.value, digits: 11)}: ${synonymInfo.constantName},');
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String get _maskConstantVariables {
|
||||
final OutputLines<int> lines = OutputLines<int>('Mask constants', checkDuplicate: false);
|
||||
for (final MaskConstant constant in _maskConstants) {
|
||||
lines.add(constant.value, '''
|
||||
${_wrapString(constant.description)} ///
|
||||
/// This is used by platform-specific code to generate Flutter key codes.
|
||||
static const int ${constant.lowerCamelName} = ${toHex(constant.value, digits: 11)};
|
||||
''');
|
||||
}
|
||||
return keyCodeMap.toString().trimRight();
|
||||
return lines.join().trimRight();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -156,17 +173,21 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK
|
||||
'PHYSICAL_KEY_MAP': _predefinedHidCodeMap,
|
||||
'PHYSICAL_KEY_DEFINITIONS': _physicalDefinitions,
|
||||
'PHYSICAL_KEY_DEBUG_NAMES': _physicalDebugNames,
|
||||
'MASK_CONSTANTS': _maskConstantVariables,
|
||||
};
|
||||
}
|
||||
|
||||
late final List<SynonymKeyInfo> synonyms = LogicalKeyData.synonyms.entries.map(
|
||||
(MapEntry<String, List<String>> synonymDefinition) {
|
||||
late final Map<String, SynonymKeyInfo> synonyms = Map<String, SynonymKeyInfo>.fromEntries(
|
||||
LogicalKeyData.synonyms.entries.map((MapEntry<String, List<String>> synonymDefinition) {
|
||||
final List<LogicalKeyEntry> entries = synonymDefinition.value.map(
|
||||
(String name) => logicalData.entryByName(name)).toList();
|
||||
return SynonymKeyInfo(
|
||||
entries,
|
||||
return MapEntry<String, SynonymKeyInfo>(
|
||||
synonymDefinition.key,
|
||||
SynonymKeyInfo(
|
||||
entries,
|
||||
synonymDefinition.key,
|
||||
),
|
||||
);
|
||||
}
|
||||
).toList();
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -51,24 +51,24 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
|
||||
|
||||
/// This generates the map of GLFW number pad key codes to logical keys.
|
||||
String get _glfwNumpadMap {
|
||||
final StringBuffer glfwNumpadMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('GLFW numpad map');
|
||||
for (final PhysicalKeyEntry entry in _numpadKeyData) {
|
||||
for (final int code in entry.glfwKeyCodes) {
|
||||
glfwNumpadMap.writeln(' $code: LogicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(code, ' $code: LogicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return glfwNumpadMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of GLFW key codes to logical keys.
|
||||
String get _glfwKeyCodeMap {
|
||||
final StringBuffer glfwKeyCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('GLFW key code map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
for (final int code in entry.glfwKeyCodes) {
|
||||
glfwKeyCodeMap.writeln(' $code: LogicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(code, ' $code: LogicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return glfwKeyCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of GTK number pad key codes to logical keys.
|
||||
@@ -95,13 +95,14 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
|
||||
|
||||
/// This generates the map of XKB USB HID codes to physical keys.
|
||||
String get _xkbScanCodeMap {
|
||||
final StringBuffer xkbScanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('GTK scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.xKbScanCode != null) {
|
||||
xkbScanCodeMap.writeln(' ${toHex(entry.xKbScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(entry.xKbScanCode!,
|
||||
' ${toHex(entry.xKbScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return xkbScanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Android key codes to logical keys.
|
||||
@@ -128,26 +129,26 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
|
||||
|
||||
/// This generates the map of Android scan codes to physical keys.
|
||||
String get _androidScanCodeMap {
|
||||
final StringBuffer androidScanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Android scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.androidScanCodes != null) {
|
||||
for (final int code in entry.androidScanCodes) {
|
||||
androidScanCodeMap.writeln(' $code: PhysicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(code, ' $code: PhysicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
}
|
||||
return androidScanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Windows scan codes to physical keys.
|
||||
String get _windowsScanCodeMap {
|
||||
final StringBuffer windowsScanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Windows scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.windowsScanCode != null) {
|
||||
windowsScanCodeMap.writeln(' ${toHex(entry.windowsScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(entry.windowsScanCode!, ' ${entry.windowsScanCode}: PhysicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return windowsScanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Windows number pad key codes to logical keys.
|
||||
@@ -181,34 +182,34 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
|
||||
|
||||
/// This generates the map of macOS key codes to physical keys.
|
||||
String get _macOSScanCodeMap {
|
||||
final StringBuffer macOSScanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('macOS scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.macOSScanCode != null) {
|
||||
macOSScanCodeMap.writeln(' ${toHex(entry.macOSScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(entry.macOSScanCode!, ' ${toHex(entry.macOSScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return macOSScanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of macOS number pad key codes to logical keys.
|
||||
String get _macOSNumpadMap {
|
||||
final StringBuffer macOSNumPadMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('macOS numpad map');
|
||||
for (final PhysicalKeyEntry entry in _numpadKeyData) {
|
||||
if (entry.macOSScanCode != null) {
|
||||
macOSNumPadMap.writeln(' ${toHex(entry.macOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(entry.macOSScanCode!, ' ${toHex(entry.macOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return macOSNumPadMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String get _macOSFunctionKeyMap {
|
||||
final StringBuffer macOSFunctionKeyMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('macOS function key map');
|
||||
for (final PhysicalKeyEntry entry in _functionKeyData) {
|
||||
if (entry.macOSScanCode != null) {
|
||||
macOSFunctionKeyMap.writeln(' ${toHex(entry.macOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
|
||||
lines.add(entry.macOSScanCode!, ' ${toHex(entry.macOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
|
||||
}
|
||||
}
|
||||
return macOSFunctionKeyMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of macOS key codes to physical keys.
|
||||
@@ -290,38 +291,38 @@ class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
|
||||
|
||||
/// This generates the map of Web KeyboardEvent codes to physical keys.
|
||||
String get _webPhysicalKeyMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<String> lines = OutputLines<String>('Web physical key map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.name != null) {
|
||||
result.writeln(" '${entry.name}': PhysicalKeyboardKey.${entry.constantName},");
|
||||
lines.add(entry.name, " '${entry.name}': PhysicalKeyboardKey.${entry.constantName},");
|
||||
}
|
||||
}
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String get _webNumpadMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<String> lines = OutputLines<String>('Web numpad map');
|
||||
for (final LogicalKeyEntry entry in _numpadLogicalKeyData) {
|
||||
if (entry.name != null) {
|
||||
result.writeln(" '${entry.name}': LogicalKeyboardKey.${entry.constantName},");
|
||||
lines.add(entry.name, " '${entry.name}': LogicalKeyboardKey.${entry.constantName},");
|
||||
}
|
||||
}
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Web number pad codes to logical keys.
|
||||
String get _webLocationMap {
|
||||
final String jsonRaw = File(path.join(dataRoot, 'web_logical_location_mapping.json')).readAsStringSync();
|
||||
final Map<String, List<String?>> locationMap = parseMapOfListOfNullableString(jsonRaw);
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<String> lines = OutputLines<String>('Web location map');
|
||||
locationMap.forEach((String key, List<String?> keyNames) {
|
||||
final String keyStrings = keyNames.map((String? keyName) {
|
||||
final String? constantName = keyName == null ? null : logicalData.entryByName(keyName).constantName;
|
||||
return constantName != null ? 'LogicalKeyboardKey.$constantName' : 'null';
|
||||
}).join(', ');
|
||||
result.writeln(" '$key': <LogicalKeyboardKey?>[$keyStrings],");
|
||||
lines.add(key, " '$key': <LogicalKeyboardKey?>[$keyStrings],");
|
||||
});
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -120,15 +120,17 @@ class LogicalKeyData {
|
||||
/// Key Enum Value
|
||||
/// DOM_KEY_MAP("Accel", ACCEL, 0x0101),
|
||||
///
|
||||
/// Flutter's supplemental_key_data.inc also has a new format
|
||||
/// that uses a character as the 3rd argument.
|
||||
/// Flutter's supplemental_key_data.inc also has some new formats.
|
||||
/// The following format uses a character as the 3rd argument.
|
||||
/// Key Enum Character
|
||||
/// DOM_KEY_UNI("KeyB", KEY_B, 'b'),
|
||||
///
|
||||
/// The following format should be mapped to the Flutter plane.
|
||||
/// Key Enum Character
|
||||
/// FLUTTER_KEY_MAP("Lang4", LANG4, 0x00013),
|
||||
static void _readKeyEntries(Map<String, LogicalKeyEntry> data, String input) {
|
||||
final Map<String, String> unusedNumpad = Map<String, String>.from(_printableToNumpads);
|
||||
|
||||
final RegExp domKeyRegExp = RegExp(
|
||||
r'DOM_KEY_(?<kind>UNI|MAP)\s*\(\s*'
|
||||
r'(?<source>DOM|FLUTTER)_KEY_(?<kind>UNI|MAP)\s*\(\s*'
|
||||
r'"(?<name>[^\s]+?)",\s*'
|
||||
r'(?<enum>[^\s]+?),\s*'
|
||||
r"(?:0[xX](?<unicode>[a-fA-F0-9]+)|'(?<char>.)')\s*"
|
||||
@@ -140,6 +142,7 @@ class LogicalKeyData {
|
||||
final RegExp commentRegExp = RegExp(r'//.*$', multiLine: true);
|
||||
input = input.replaceAll(commentRegExp, '');
|
||||
for (final RegExpMatch match in domKeyRegExp.allMatches(input)) {
|
||||
final String source = match.namedGroup('source')!;
|
||||
final String webName = match.namedGroup('name')!;
|
||||
// ".AltGraphLatch" is consumed internally and not expressed to the Web.
|
||||
if (webName.startsWith('.')) {
|
||||
@@ -150,51 +153,21 @@ class LogicalKeyData {
|
||||
getHex(match.namedGroup('unicode')!) :
|
||||
match.namedGroup('char')!.codeUnitAt(0);
|
||||
final String? keyLabel = match.namedGroup('kind')! == 'UNI' ? String.fromCharCode(value) : null;
|
||||
// If it's a modifier key, add left and right keys instead.
|
||||
// Don't add web names and values; they're solved with locations.
|
||||
if (_chromeModifiers.containsKey(name)) {
|
||||
final _ModifierPair pair = _chromeModifiers[name]!;
|
||||
data[pair.left] = LogicalKeyEntry.fromName(
|
||||
value: value + kLeftModifierPlane,
|
||||
name: pair.left,
|
||||
keyLabel: null, // Modifier keys don't have keyLabels
|
||||
)..webNames.add(pair.left);
|
||||
data[pair.right] = LogicalKeyEntry.fromName(
|
||||
value: value + kRightModifierPlane,
|
||||
name: pair.right,
|
||||
keyLabel: null, // Modifier keys don't have keyLabels
|
||||
)..webNames.add(pair.right);
|
||||
// Skip modifier keys from DOM. They will be added with supplemental data.
|
||||
if (_chromeModifiers.containsKey(name) && source == 'DOM') {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If it has a numpad counterpart, also add the numpad key.
|
||||
final String? char = value < 256 ? String.fromCharCode(value) : null;
|
||||
if (char != null && _printableToNumpads.containsKey(char)) {
|
||||
final String numpadName = _printableToNumpads[char]!;
|
||||
data[numpadName] = LogicalKeyEntry.fromName(
|
||||
value: char.codeUnitAt(0) + kNumpadPlane,
|
||||
name: numpadName,
|
||||
keyLabel: null, // Don't add keyLabel for numpad counterparts
|
||||
)..webNames.add(numpadName);
|
||||
unusedNumpad.remove(char);
|
||||
}
|
||||
|
||||
final bool isPrintable = (keyLabel != null && !_isControlCharacter(keyLabel))
|
||||
|| printable.containsKey(name);
|
||||
data.putIfAbsent(name, () {
|
||||
final bool isPrintable = (keyLabel != null && !_isControlCharacter(keyLabel))
|
||||
|| printable.containsKey(name)
|
||||
|| value == 0; // "None" key
|
||||
return LogicalKeyEntry.fromName(
|
||||
value: value + (isPrintable ? kUnicodePlane : kUnprintablePlane),
|
||||
value: toPlane(value, _sourceToPlane(source, isPrintable)),
|
||||
name: name,
|
||||
keyLabel: keyLabel,
|
||||
)..webNames.add(webName);
|
||||
});
|
||||
}
|
||||
|
||||
// Make sure every Numpad key that we care about has been defined.
|
||||
unusedNumpad.forEach((String key, String value) {
|
||||
print('Undefined numpad key $value');
|
||||
});
|
||||
}
|
||||
|
||||
static void _readMacOsKeyCodes(
|
||||
@@ -347,11 +320,11 @@ class LogicalKeyData {
|
||||
return 0;
|
||||
final String? keyLabel = printable[entry.constantName];
|
||||
if (keyLabel != null && !entry.constantName.startsWith('numpad')) {
|
||||
return kUnicodePlane | (keyLabel.codeUnitAt(0) & kValueMask);
|
||||
return toPlane(keyLabel.codeUnitAt(0), kUnicodePlane.value);
|
||||
} else {
|
||||
final PhysicalKeyEntry? physicalEntry = physicalData.tryEntryByName(entry.name);
|
||||
if (physicalEntry != null) {
|
||||
return kHidPlane | (physicalEntry.usbHidCode & kValueMask);
|
||||
return toPlane(physicalEntry.usbHidCode, kFuchsiaPlane.value);
|
||||
}
|
||||
}
|
||||
})();
|
||||
@@ -376,14 +349,6 @@ class LogicalKeyData {
|
||||
.cast<String, String>();
|
||||
})();
|
||||
|
||||
// Map printable to corresponding numpad key name
|
||||
static late final Map<String, String> _printableToNumpads = () {
|
||||
final String rawJson = File(path.join(dataRoot, 'printable_to_numpads.json',)).readAsStringSync();
|
||||
return (json.decode(rawJson) as Map<String, dynamic>).map((String key, dynamic value) {
|
||||
return MapEntry<String, String>(key, value as String);
|
||||
});
|
||||
}();
|
||||
|
||||
/// Returns the static map of synonym representations.
|
||||
///
|
||||
/// These include synonyms for keys which don't have printable
|
||||
@@ -400,6 +365,18 @@ class LogicalKeyData {
|
||||
return MapEntry<String, List<String>>(name, names);
|
||||
});
|
||||
})();
|
||||
|
||||
static int _sourceToPlane(String source, bool isPrintable) {
|
||||
switch (source) {
|
||||
case 'DOM':
|
||||
return isPrintable ? kUnicodePlane.value : kUnprintablePlane.value;
|
||||
case 'FLUTTER':
|
||||
return kFlutterPlane.value;
|
||||
default:
|
||||
assert(false, 'Unrecognized logical key source $source');
|
||||
return kFlutterPlane.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -25,10 +25,6 @@ const List<String> kModifiersOfInterest = <String>[
|
||||
const List<String> kSpecialPhysicalKeys = <String>['CapsLock'];
|
||||
const List<String> kSpecialLogicalKeys = <String>['CapsLock'];
|
||||
|
||||
String _toConstantVariableName(String variableName) {
|
||||
return 'k${variableName[0].toUpperCase()}${variableName.substring(1)}';
|
||||
}
|
||||
|
||||
/// Generates the key mapping for macOS, based on the information in the key
|
||||
/// data structure given to it.
|
||||
class MacOSCodeGenerator extends PlatformCodeGenerator {
|
||||
@@ -37,36 +33,36 @@ class MacOSCodeGenerator extends PlatformCodeGenerator {
|
||||
|
||||
/// This generates the map of macOS key codes to physical keys.
|
||||
String get _scanCodeMap {
|
||||
final StringBuffer scanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('macOS scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.macOSScanCode != null) {
|
||||
scanCodeMap.writeln(' @${toHex(entry.macOSScanCode)} : @${toHex(entry.usbHidCode)}, // ${entry.constantName}');
|
||||
lines.add(entry.macOSScanCode!, ' @${toHex(entry.macOSScanCode)} : @${toHex(entry.usbHidCode)}, // ${entry.constantName}');
|
||||
}
|
||||
}
|
||||
return scanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
String get _keyCodeToLogicalMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('macOS keycode map');
|
||||
for (final LogicalKeyEntry entry in logicalData.entries) {
|
||||
zipStrict(entry.macOSKeyCodeValues, entry.macOSKeyCodeNames, (int macOSValue, String macOSName) {
|
||||
result.writeln(' @${toHex(macOSValue)} : @${toHex(entry.value, digits: 11)}, // $macOSName');
|
||||
lines.add(macOSValue,
|
||||
' @${toHex(macOSValue)} : @${toHex(entry.value, digits: 11)}, // $macOSName -> ${entry.constantName}');
|
||||
});
|
||||
}
|
||||
return result.toString().trimRight();
|
||||
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,
|
||||
kMacosPlane,
|
||||
];
|
||||
for (final MaskConstant constant in maskConstants) {
|
||||
buffer.writeln('/**');
|
||||
buffer.write(constant.description
|
||||
.map((String line) => wrapString(line, prefix: ' * '))
|
||||
.join(' *\n'));
|
||||
buffer.writeln(' */');
|
||||
buffer.writeln('const uint64_t ${_toConstantVariableName(constant.name)} = ${toHex(constant.value, digits: 11)};');
|
||||
buffer.writeln('');
|
||||
buffer.writeln('const uint64_t k${constant.upperCamelName} = ${toHex(constant.value, digits: 11)};');
|
||||
}
|
||||
return buffer.toString().trimRight();
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ class PhysicalKeyData {
|
||||
final String enumName = match.namedGroup('enum')!.replaceAll('MINIMIUM', 'MINIMUM');
|
||||
|
||||
final String name = chromiumCode ?? shoutingToUpperCamel(enumName);
|
||||
if (name == 'IntlHash') {
|
||||
if (name == 'IntlHash' || name == 'None') {
|
||||
// Skip key that is not actually generated by any keyboard.
|
||||
continue;
|
||||
}
|
||||
|
||||
51
dev/tools/gen_keycodes/lib/testing_key_codes_gen.dart
Normal file
51
dev/tools/gen_keycodes/lib/testing_key_codes_gen.dart
Normal file
@@ -0,0 +1,51 @@
|
||||
// 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 _toUpperCammel(String lowerCammel) {
|
||||
return lowerCammel.substring(0, 1).toUpperCase() + lowerCammel.substring(1);
|
||||
}
|
||||
|
||||
/// Generates the common/testing/key_codes.h based on the information in the key
|
||||
/// data structure given to it.
|
||||
class KeyCodesCcGenerator extends BaseCodeGenerator {
|
||||
KeyCodesCcGenerator(PhysicalKeyData keyData, LogicalKeyData logicalData) : super(keyData, 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, '''
|
||||
constexpr uint64_t kPhysical${_toUpperCammel(entry.constantName)} = ${toHex(entry.usbHidCode, digits: 8)};''');
|
||||
}
|
||||
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, '''
|
||||
constexpr uint64_t kLogical${_toUpperCammel(entry.constantName)} = ${toHex(entry.value, digits: 11)};''');
|
||||
}
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
@override
|
||||
String get templatePath => path.join(dataRoot, 'key_codes_h.tmpl');
|
||||
|
||||
@override
|
||||
Map<String, String> mappings() {
|
||||
return <String, String>{
|
||||
'LOGICAL_KEY_DEFINITIONS': _logicalDefinitions,
|
||||
'PHYSICAL_KEY_DEFINITIONS': _physicalDefinitions,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import 'dart:io';
|
||||
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import 'constants.dart';
|
||||
|
||||
/// The location of the Flutter root directory, based on the known location of
|
||||
/// this script.
|
||||
final Directory flutterRoot = Directory(path.dirname(Platform.script.toFilePath())).parent.parent.parent.parent;
|
||||
@@ -221,32 +223,63 @@ void addNameValue(List<String> names, List<int> values, String name, int value)
|
||||
}
|
||||
}
|
||||
|
||||
/// The information for a line used by [OutputLines].
|
||||
class OutputLine<T extends Comparable<Object>> {
|
||||
const OutputLine(this.key, this.value);
|
||||
|
||||
final T key;
|
||||
final String value;
|
||||
}
|
||||
|
||||
/// A utility class to build join a number of lines in a sorted order.
|
||||
///
|
||||
/// Use [add] to add a line and associate it with an index. Use [sortedJoin] to
|
||||
/// get the joined string of these lines joined sorting them in the order of the
|
||||
/// index.
|
||||
class OutputLines<T extends Comparable<Object>> {
|
||||
OutputLines(this.mapName);
|
||||
OutputLines(this.mapName, {this.checkDuplicate = true});
|
||||
|
||||
/// If true, then lines with duplicate keys will be warned and discarded.
|
||||
///
|
||||
/// Default to true.
|
||||
final bool checkDuplicate;
|
||||
|
||||
/// The name for this map.
|
||||
///
|
||||
/// Used in warning messages.
|
||||
final String mapName;
|
||||
|
||||
final Map<T, String> lines = <T, String>{};
|
||||
final Set<T> keys = <T>{};
|
||||
final List<OutputLine<T>> lines = <OutputLine<T>>[];
|
||||
|
||||
void add(T code, String line) {
|
||||
if (lines.containsKey(code)) {
|
||||
print('Warn: $mapName is requested to add line $code as:\n $line\n but it already exists as:\n ${lines[code]}');
|
||||
if (checkDuplicate) {
|
||||
if (keys.contains(code)) {
|
||||
final OutputLine<T> existing = lines.firstWhere((OutputLine<T> line) => line.key == code);
|
||||
print('Warn: $mapName is requested to add line $code as:\n $line\n but it already exists as:\n ${existing.value}');
|
||||
return;
|
||||
}
|
||||
keys.add(code);
|
||||
}
|
||||
lines[code] = line;
|
||||
lines.add(OutputLine<T>(code, line));
|
||||
}
|
||||
|
||||
String join() {
|
||||
return lines.map((OutputLine<T> line) => line.value).join('\n');
|
||||
}
|
||||
|
||||
String sortedJoin() {
|
||||
return (lines.entries.toList()
|
||||
..sort((MapEntry<T, String> a, MapEntry<T, String> b) => a.key.compareTo(b.key)))
|
||||
.map((MapEntry<T, String> entry) => entry.value)
|
||||
return (lines.sublist(0)
|
||||
..sort((OutputLine<T> a, OutputLine<T> b) => a.key.compareTo(b.key)))
|
||||
.map((OutputLine<T> line) => line.value)
|
||||
.join('\n');
|
||||
}
|
||||
}
|
||||
|
||||
int toPlane(int value, int plane) {
|
||||
return (value & kValueMask.value) + (plane & kPlaneMask.value);
|
||||
}
|
||||
|
||||
int getPlane(int value) {
|
||||
return value & kPlaneMask.value;
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
@@ -23,8 +24,11 @@ class WebCodeGenerator extends PlatformCodeGenerator {
|
||||
String get _webLogicalKeyCodeMap {
|
||||
final OutputLines<String> lines = OutputLines<String>('Web logical map');
|
||||
for (final LogicalKeyEntry entry in logicalData.entries) {
|
||||
for (final String name in entry.webNames) {
|
||||
lines.add(name, " '$name': ${toHex(entry.value, digits: 11)},");
|
||||
final int plane = getPlane(entry.value);
|
||||
if (plane == kUnprintablePlane.value) {
|
||||
for (final String name in entry.webNames) {
|
||||
lines.add(name, " '$name': ${toHex(entry.value, digits: 11)},");
|
||||
}
|
||||
}
|
||||
}
|
||||
return lines.sortedJoin().trimRight();
|
||||
@@ -32,25 +36,29 @@ class WebCodeGenerator extends PlatformCodeGenerator {
|
||||
|
||||
/// This generates the map of Web KeyboardEvent codes to physical key USB HID codes.
|
||||
String get _webPhysicalKeyCodeMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<String> lines = OutputLines<String>('Web physical map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.name != null) {
|
||||
result.writeln(" '${entry.name}': ${toHex(entry.usbHidCode)},");
|
||||
lines.add(entry.name,
|
||||
" '${entry.name}': ${toHex(entry.usbHidCode)}, // ${entry.constantName}");
|
||||
}
|
||||
}
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Web number pad codes to logical key ids.
|
||||
String get _webLogicalLocationMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<String> lines = OutputLines<String>('Web logical location map');
|
||||
_logicalLocationMap.forEach((String webKey, List<String?> locations) {
|
||||
final String valuesString = locations.map((String? value) {
|
||||
return value == null ? 'null' : toHex(logicalData.entryByName(value).value, digits: 10);
|
||||
return value == null ? 'null' : toHex(logicalData.entryByName(value).value, digits: 11);
|
||||
}).join(', ');
|
||||
result.writeln(" '$webKey': <int?>[$valuesString],");
|
||||
final String namesString = locations.map((String? value) {
|
||||
return value == null ? 'null' : logicalData.entryByName(value).constantName;
|
||||
}).join(', ');
|
||||
lines.add(webKey, " '$webKey': <int?>[$valuesString], // $namesString");
|
||||
});
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
final Map<String, List<String?>> _logicalLocationMap;
|
||||
|
||||
|
||||
@@ -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';
|
||||
@@ -21,24 +22,29 @@ class WindowsCodeGenerator extends PlatformCodeGenerator {
|
||||
|
||||
/// This generates the map of Windows scan codes to physical keys.
|
||||
String get _windowsScanCodeMap {
|
||||
final StringBuffer windowsScanCodeMap = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Windows scancode map');
|
||||
for (final PhysicalKeyEntry entry in keyData.entries) {
|
||||
if (entry.windowsScanCode != null) {
|
||||
windowsScanCodeMap.writeln(' {${toHex(entry.windowsScanCode)}, ${toHex(entry.usbHidCode)}}, // ${entry.constantName}');
|
||||
lines.add(entry.windowsScanCode!,
|
||||
' {${toHex(entry.windowsScanCode)}, ${toHex(entry.usbHidCode)}}, // ${entry.constantName}');
|
||||
}
|
||||
}
|
||||
return windowsScanCodeMap.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map of Windows key codes to logical keys.
|
||||
String get _windowsLogicalKeyCodeMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Windows logical map');
|
||||
for (final LogicalKeyEntry entry in logicalData.entries) {
|
||||
zipStrict(entry.windowsValues, entry.windowsNames, (int windowsValue, String windowsName) {
|
||||
result.writeln(' {${toHex(windowsValue)}, ${toHex(entry.value, digits: 11)}}, // $windowsName');
|
||||
});
|
||||
zipStrict(entry.windowsValues, entry.windowsNames,
|
||||
(int windowsValue, String windowsName) {
|
||||
lines.add(windowsValue,
|
||||
' {${toHex(windowsValue)}, ${toHex(entry.value, digits: 11)}}, '
|
||||
'// $windowsName -> ${entry.constantName}');
|
||||
},
|
||||
);
|
||||
}
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
|
||||
/// This generates the map from scan code to logical keys.
|
||||
@@ -47,16 +53,32 @@ class WindowsCodeGenerator extends PlatformCodeGenerator {
|
||||
/// key codes are either 0 or ambiguous (multiple keys using the same key
|
||||
/// code), these keys are resolved by scan codes.
|
||||
String get _scanCodeToLogicalMap {
|
||||
final StringBuffer result = StringBuffer();
|
||||
final OutputLines<int> lines = OutputLines<int>('Windows scancode to logical map');
|
||||
_scancodeToLogical.forEach((String scanCodeName, String logicalName) {
|
||||
final PhysicalKeyEntry physicalEntry = keyData.entryByName(scanCodeName);
|
||||
final int logicalValue = logicalData.entryByName(logicalName).value;
|
||||
result.writeln(' {${toHex(physicalEntry.windowsScanCode)}, ${toHex(logicalValue, digits: 10)}}, // ${physicalEntry.name}');
|
||||
final LogicalKeyEntry logicalEntry = logicalData.entryByName(logicalName);
|
||||
lines.add(physicalEntry.windowsScanCode!,
|
||||
' {${toHex(physicalEntry.windowsScanCode)}, ${toHex(logicalEntry.value, digits: 11)}}, '
|
||||
'// ${physicalEntry.constantName} -> ${logicalEntry.constantName}');
|
||||
});
|
||||
return result.toString().trimRight();
|
||||
return lines.sortedJoin().trimRight();
|
||||
}
|
||||
final Map<String, String> _scancodeToLogical;
|
||||
|
||||
/// 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,
|
||||
kWindowsPlane,
|
||||
];
|
||||
for (final MaskConstant constant in maskConstants) {
|
||||
buffer.writeln('const uint64_t KeyboardKeyEmbedderHandler::${constant.lowerCamelName} = ${toHex(constant.value, digits: 11)};');
|
||||
}
|
||||
return buffer.toString().trimRight();
|
||||
}
|
||||
|
||||
@override
|
||||
String get templatePath => path.join(dataRoot, 'windows_flutter_key_map_cc.tmpl');
|
||||
|
||||
@@ -70,6 +92,7 @@ class WindowsCodeGenerator extends PlatformCodeGenerator {
|
||||
'WINDOWS_SCAN_CODE_MAP': _windowsScanCodeMap,
|
||||
'WINDOWS_SCAN_CODE_TO_LOGICAL_MAP': _scanCodeToLogicalMap,
|
||||
'WINDOWS_KEY_CODE_MAP': _windowsLogicalKeyCodeMap,
|
||||
'MASK_CONSTANTS': _maskConstants,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user