Hardware keyboard: codegen (#73440)
Rewrites tools/gen_keycodes, the script that generates key mappings across the framework and the engine.
This commit is contained in:
39
dev/tools/gen_keycodes/bin/gen_keycodes
Executable file
39
dev/tools/gen_keycodes/bin/gen_keycodes
Executable file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Needed because if it is set, cd may print the path it changed to.
|
||||
unset CDPATH
|
||||
|
||||
# On Mac OS, readlink -f doesn't work, so follow_links traverses the path one
|
||||
# link at a time, and then cds into the link destination and find out where it
|
||||
# ends up.
|
||||
#
|
||||
# The returned filesystem path must be a format usable by Dart's URI parser,
|
||||
# since the Dart command line tool treats its argument as a file URI, not a
|
||||
# filename. For instance, multiple consecutive slashes should be reduced to a
|
||||
# single slash, since double-slashes indicate a URI "authority", and these are
|
||||
# supposed to be filenames. There is an edge case where this will return
|
||||
# multiple slashes: when the input resolves to the root directory. However, if
|
||||
# that were the case, we wouldn't be running this shell, so we don't do anything
|
||||
# about it.
|
||||
#
|
||||
# The function is enclosed in a subshell to avoid changing the working directory
|
||||
# of the caller.
|
||||
function follow_links() (
|
||||
cd -P "$(dirname -- "$1")"
|
||||
file="$PWD/$(basename -- "$1")"
|
||||
while [[ -h "$file" ]]; do
|
||||
cd -P "$(dirname -- "$file")"
|
||||
file="$(readlink -- "$file")"
|
||||
cd -P "$(dirname -- "$file")"
|
||||
file="$PWD/$(basename -- "$file")"
|
||||
done
|
||||
echo "$file"
|
||||
)
|
||||
|
||||
PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")"
|
||||
BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
|
||||
DART_BIN="$BIN_DIR/../../../../bin/dart"
|
||||
|
||||
"$DART_BIN" --enable-asserts "$BIN_DIR/gen_keycodes.dart" "$@"
|
||||
@@ -8,27 +8,32 @@ import 'dart:io' hide Platform;
|
||||
import 'package:args/args.dart';
|
||||
import 'package:gen_keycodes/android_code_gen.dart';
|
||||
import 'package:gen_keycodes/base_code_gen.dart';
|
||||
import 'package:gen_keycodes/fuchsia_code_gen.dart';
|
||||
import 'package:gen_keycodes/glfw_code_gen.dart';
|
||||
import 'package:gen_keycodes/gtk_code_gen.dart';
|
||||
import 'package:gen_keycodes/ios_code_gen.dart';
|
||||
import 'package:gen_keycodes/key_data.dart';
|
||||
import 'package:gen_keycodes/keyboard_keys_code_gen.dart';
|
||||
import 'package:gen_keycodes/keyboard_maps_code_gen.dart';
|
||||
import 'package:gen_keycodes/macos_code_gen.dart';
|
||||
import 'package:gen_keycodes/utils.dart';
|
||||
import 'package:gen_keycodes/web_code_gen.dart';
|
||||
import 'package:gen_keycodes/windows_code_gen.dart';
|
||||
import 'package:gen_keycodes/keyboard_keys_code_gen.dart';
|
||||
import 'package:gen_keycodes/keyboard_maps_code_gen.dart';
|
||||
import 'package:gen_keycodes/physical_key_data.dart';
|
||||
import 'package:gen_keycodes/logical_key_data.dart';
|
||||
import 'package:gen_keycodes/utils.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
/// Get contents of the file that contains the key code mapping in Chromium
|
||||
/// Get contents of the file that contains the physical key mapping in Chromium
|
||||
/// source.
|
||||
Future<String> getChromiumConversions() async {
|
||||
Future<String> getChromiumCodes() async {
|
||||
final Uri keyCodesUri = Uri.parse('https://chromium.googlesource.com/codesearch/chromium/src/+/refs/heads/master/ui/events/keycodes/dom/dom_code_data.inc?format=TEXT');
|
||||
return utf8.decode(base64.decode(await http.read(keyCodesUri)));
|
||||
}
|
||||
|
||||
/// Get contents of the file that contains the logical key mapping in Chromium
|
||||
/// source.
|
||||
Future<String> getChromiumKeys() async {
|
||||
final Uri keyCodesUri = Uri.parse('https://chromium.googlesource.com/codesearch/chromium/src/+/refs/heads/master/ui/events/keycodes/dom/dom_key_data.inc?format=TEXT');
|
||||
return utf8.decode(base64.decode(await http.read(keyCodesUri)));
|
||||
}
|
||||
|
||||
/// Get contents of the file that contains the key codes in Android source.
|
||||
Future<String> getAndroidKeyCodes() async {
|
||||
final Uri keyCodesUri = Uri.parse('https://android.googlesource.com/platform/frameworks/native/+/master/include/android/keycodes.h?format=TEXT');
|
||||
@@ -61,81 +66,35 @@ Future<String> getGtkKeyCodes() async {
|
||||
return http.read(keyCodesUri);
|
||||
}
|
||||
|
||||
String readDataFile(String fileName) {
|
||||
return File(path.join(dataRoot, fileName)).readAsStringSync();
|
||||
}
|
||||
|
||||
Future<void> main(List<String> rawArguments) async {
|
||||
final ArgParser argParser = ArgParser();
|
||||
argParser.addOption(
|
||||
'chromium-hid-codes',
|
||||
defaultsTo: null,
|
||||
help: 'The path to where the Chromium HID code mapping file should be '
|
||||
'read. If --chromium-hid-codes is not specified, the input will be read '
|
||||
'from the correct file in the Chromium repository.',
|
||||
'engine-root',
|
||||
defaultsTo: path.join(flutterRoot.path, '..', 'engine', 'src', 'flutter'),
|
||||
help: 'The path to the root of the flutter/engine repository. This is used '
|
||||
'to place the generated engine mapping files. If --engine-root is not '
|
||||
r'specified, it will default to $flutterRoot/../engine/src/flutter, '
|
||||
'assuming the engine gclient folder is placed at the same folder as '
|
||||
'the flutter/flutter repository.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'supplemental-hid-codes',
|
||||
defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'supplemental_hid_codes.inc'),
|
||||
help: "The path to where the supplemental HID codes that don't appear in the "
|
||||
'Chromium map should be read.',
|
||||
'physical-data',
|
||||
defaultsTo: path.join(dataRoot, 'physical_key_data.json'),
|
||||
help: 'The path to where the physical key data file should be written when '
|
||||
'collected, and read from when generating output code. If --physical-data is '
|
||||
'not specified, the output will be written to/read from the current '
|
||||
"directory. If the output directory doesn't exist, it, and the path to "
|
||||
'it, will be created.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'android-keycodes',
|
||||
defaultsTo: null,
|
||||
help: 'The path to where the Android keycodes header file should be read. '
|
||||
'If --android-keycodes is not specified, the input will be read from the '
|
||||
'correct file in the Android repository.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'android-scancodes',
|
||||
defaultsTo: null,
|
||||
help: 'The path to where the Android scancodes header file should be read. '
|
||||
'If --android-scancodes is not specified, the input will be read from the '
|
||||
'correct file in the Android repository.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'android-domkey',
|
||||
defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_android_name.json'),
|
||||
help: 'The path to where the Android keycode to DomKey mapping is.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'glfw-keycodes',
|
||||
defaultsTo: null,
|
||||
help: 'The path to where the GLFW keycodes header file should be read. '
|
||||
'If --glfw-keycodes is not specified, the input will be read from the '
|
||||
'correct file in the GLFW github repository.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'gtk-keycodes',
|
||||
defaultsTo: null,
|
||||
help: 'The path to where the GTK keycodes header file should be read. '
|
||||
'If --gtk-keycodes is not specified, the input will be read from the '
|
||||
'correct file in the GTK repository.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'windows-keycodes',
|
||||
defaultsTo: null,
|
||||
help: 'The path to where the Windows keycodes header file should be read. '
|
||||
'If --windows-keycodes is not specified, the input will be read from the '
|
||||
'correct file in the Windows github repository.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'windows-domkey',
|
||||
defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_windows_name.json'),
|
||||
help: 'The path to where the Windows keycode to DomKey mapping is.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'glfw-domkey',
|
||||
defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_glfw_name.json'),
|
||||
help: 'The path to where the GLFW keycode to DomKey mapping is.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'gtk-domkey',
|
||||
defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_gtk_name.json'),
|
||||
help: 'The path to where the GTK keycode to DomKey mapping is.',
|
||||
);
|
||||
argParser.addOption(
|
||||
'data',
|
||||
defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_data.json'),
|
||||
help: 'The path to where the key code data file should be written when '
|
||||
'collected, and read from when generating output code. If --data is '
|
||||
'logical-data',
|
||||
defaultsTo: path.join(dataRoot, 'logical_key_data.json'),
|
||||
help: 'The path to where the logical key data file should be written when '
|
||||
'collected, and read from when generating output code. If --logical-data is '
|
||||
'not specified, the output will be written to/read from the current '
|
||||
"directory. If the output directory doesn't exist, it, and the path to "
|
||||
'it, will be created.',
|
||||
@@ -143,7 +102,7 @@ Future<void> main(List<String> rawArguments) async {
|
||||
argParser.addOption(
|
||||
'code',
|
||||
defaultsTo: path.join(flutterRoot.path, 'packages', 'flutter', 'lib', 'src', 'services', 'keyboard_key.dart'),
|
||||
help: 'The path to where the output "keyboard_keys.dart" file should be '
|
||||
help: 'The path to where the output "keyboard_key.dart" file should be '
|
||||
'written. If --code is not specified, the output will be written to the '
|
||||
'correct directory in the flutter tree. If the output directory does not '
|
||||
'exist, it, and the path to it, will be created.',
|
||||
@@ -162,7 +121,8 @@ Future<void> main(List<String> rawArguments) async {
|
||||
negatable: false,
|
||||
help: 'If this flag is set, then collect and parse header files from '
|
||||
'Chromium and Android instead of reading pre-parsed data from '
|
||||
'"key_data.json", and then update "key_data.json" with the fresh data.',
|
||||
'"physical_key_data.json" and "logical_key_data.json", and then '
|
||||
'update these files with the fresh data.',
|
||||
);
|
||||
argParser.addFlag(
|
||||
'help',
|
||||
@@ -178,64 +138,57 @@ Future<void> main(List<String> rawArguments) async {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
KeyData data;
|
||||
PlatformCodeGenerator.engineRoot = parsedArguments['engine-root'] as String;
|
||||
|
||||
PhysicalKeyData physicalData;
|
||||
LogicalKeyData logicalData;
|
||||
if (parsedArguments['collect'] as bool) {
|
||||
String hidCodes;
|
||||
if (parsedArguments['chromium-hid-codes'] == null) {
|
||||
hidCodes = await getChromiumConversions();
|
||||
} else {
|
||||
hidCodes = File(parsedArguments['chromium-hid-codes'] as String).readAsStringSync();
|
||||
}
|
||||
// Physical
|
||||
final String baseHidCodes = await getChromiumCodes();
|
||||
final String supplementalHidCodes = readDataFile('supplemental_hid_codes.inc');
|
||||
final String androidScanCodes = await getAndroidScanCodes();
|
||||
final String androidToDomKey = readDataFile('android_key_name_to_name.json');
|
||||
final String glfwKeyCodes = await getGlfwKeyCodes();
|
||||
final String glfwToDomKey = readDataFile('glfw_key_name_to_name.json');
|
||||
physicalData = PhysicalKeyData(
|
||||
<String>[baseHidCodes, supplementalHidCodes].join('\n'),
|
||||
androidScanCodes,
|
||||
androidToDomKey,
|
||||
glfwKeyCodes,
|
||||
glfwToDomKey,
|
||||
);
|
||||
|
||||
final String supplementalHidCodes = File(parsedArguments['supplemental-hid-codes'] as String).readAsStringSync();
|
||||
hidCodes = '$hidCodes\n$supplementalHidCodes';
|
||||
// Logical
|
||||
final String gtkKeyCodes = await getGtkKeyCodes();
|
||||
final String webLogicalKeys = await getChromiumKeys();
|
||||
final String supplementalKeyData = readDataFile('supplemental_key_data.inc');
|
||||
final String gtkToDomKey = readDataFile('gtk_logical_name_mapping.json');
|
||||
final String windowsKeyCodes = await getWindowsKeyCodes();
|
||||
final String windowsToDomKey = readDataFile('windows_logical_to_window_vk.json');
|
||||
final String macosLogicalToPhysical = readDataFile('macos_logical_to_physical.json');
|
||||
final String iosLogicalToPhysical = readDataFile('ios_logical_to_physical.json');
|
||||
final String androidKeyCodes = await getAndroidKeyCodes();
|
||||
|
||||
String androidKeyCodes;
|
||||
if (parsedArguments['android-keycodes'] == null) {
|
||||
androidKeyCodes = await getAndroidKeyCodes();
|
||||
} else {
|
||||
androidKeyCodes = File(parsedArguments['android-keycodes'] as String).readAsStringSync();
|
||||
}
|
||||
|
||||
String androidScanCodes;
|
||||
if (parsedArguments['android-scancodes'] == null) {
|
||||
androidScanCodes = await getAndroidScanCodes();
|
||||
} else {
|
||||
androidScanCodes = File(parsedArguments['android-scancodes'] as String).readAsStringSync();
|
||||
}
|
||||
|
||||
String glfwKeyCodes;
|
||||
if (parsedArguments['glfw-keycodes'] == null) {
|
||||
glfwKeyCodes = await getGlfwKeyCodes();
|
||||
} else {
|
||||
glfwKeyCodes = File(parsedArguments['glfw-keycodes'] as String).readAsStringSync();
|
||||
}
|
||||
|
||||
String gtkKeyCodes;
|
||||
if (parsedArguments['gtk-keycodes'] == null) {
|
||||
gtkKeyCodes = await getGtkKeyCodes();
|
||||
} else {
|
||||
gtkKeyCodes = File(parsedArguments['gtk-keycodes'] as String).readAsStringSync();
|
||||
}
|
||||
|
||||
String windowsKeyCodes;
|
||||
if (parsedArguments['windows-keycodes'] == null) {
|
||||
windowsKeyCodes = await getWindowsKeyCodes();
|
||||
} else {
|
||||
windowsKeyCodes = File(parsedArguments['windows-keycodes'] as String).readAsStringSync();
|
||||
}
|
||||
|
||||
final String windowsToDomKey = File(parsedArguments['windows-domkey'] as String).readAsStringSync();
|
||||
final String glfwToDomKey = File(parsedArguments['glfw-domkey'] as String).readAsStringSync();
|
||||
final String gtkToDomKey = File(parsedArguments['gtk-domkey'] as String).readAsStringSync();
|
||||
final String androidToDomKey = File(parsedArguments['android-domkey'] as String).readAsStringSync();
|
||||
|
||||
data = KeyData(hidCodes, androidScanCodes, androidKeyCodes, androidToDomKey, glfwKeyCodes, glfwToDomKey, gtkKeyCodes, gtkToDomKey, windowsKeyCodes, windowsToDomKey);
|
||||
logicalData = LogicalKeyData(
|
||||
<String>[webLogicalKeys, supplementalKeyData].join('\n'),
|
||||
gtkKeyCodes,
|
||||
gtkToDomKey,
|
||||
windowsKeyCodes,
|
||||
windowsToDomKey,
|
||||
androidKeyCodes,
|
||||
androidToDomKey,
|
||||
macosLogicalToPhysical,
|
||||
iosLogicalToPhysical,
|
||||
physicalData,
|
||||
);
|
||||
|
||||
// Write data files
|
||||
const JsonEncoder encoder = JsonEncoder.withIndent(' ');
|
||||
File(parsedArguments['data'] as String).writeAsStringSync(encoder.convert(data.toJson()));
|
||||
File(parsedArguments['physical-data'] as String).writeAsStringSync(encoder.convert(physicalData.toJson()) + '\n');
|
||||
File(parsedArguments['logical-data'] as String).writeAsStringSync(encoder.convert(logicalData.toJson()) + '\n');
|
||||
} else {
|
||||
data = KeyData.fromJson(json.decode(await File(parsedArguments['data'] as String).readAsString()) as Map<String, dynamic>);
|
||||
physicalData = PhysicalKeyData.fromJson(json.decode(await File(parsedArguments['physical-data'] as String).readAsString()) as Map<String, dynamic>);
|
||||
logicalData = LogicalKeyData.fromJson(json.decode(await File(parsedArguments['logical-data'] as String).readAsString()) as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
final File codeFile = File(parsedArguments['code'] as String);
|
||||
@@ -243,51 +196,49 @@ Future<void> main(List<String> rawArguments) async {
|
||||
codeFile.createSync(recursive: true);
|
||||
}
|
||||
print('Writing ${'key codes'.padRight(15)}${codeFile.absolute}');
|
||||
await codeFile.writeAsString(KeyboardKeysCodeGenerator(data).generate());
|
||||
await codeFile.writeAsString(KeyboardKeysCodeGenerator(physicalData, logicalData).generate());
|
||||
|
||||
final File mapsFile = File(parsedArguments['maps'] as String);
|
||||
if (!mapsFile.existsSync()) {
|
||||
mapsFile.createSync(recursive: true);
|
||||
}
|
||||
print('Writing ${'key maps'.padRight(15)}${mapsFile.absolute}');
|
||||
await mapsFile.writeAsString(KeyboardMapsCodeGenerator(data).generate());
|
||||
|
||||
for (final String platform in <String>['android', 'macos', 'ios', 'glfw', 'fuchsia', 'linux', 'windows', 'web']) {
|
||||
PlatformCodeGenerator codeGenerator;
|
||||
switch (platform) {
|
||||
case 'glfw':
|
||||
codeGenerator = GlfwCodeGenerator(data);
|
||||
break;
|
||||
case 'fuchsia':
|
||||
codeGenerator = FuchsiaCodeGenerator(data);
|
||||
break;
|
||||
case 'android':
|
||||
codeGenerator = AndroidCodeGenerator(data);
|
||||
break;
|
||||
case 'macos':
|
||||
codeGenerator = MacOsCodeGenerator(data);
|
||||
break;
|
||||
case 'ios':
|
||||
codeGenerator = IosCodeGenerator(data);
|
||||
break;
|
||||
case 'windows':
|
||||
codeGenerator = WindowsCodeGenerator(data);
|
||||
break;
|
||||
case 'linux':
|
||||
codeGenerator = GtkCodeGenerator(data);
|
||||
break;
|
||||
case 'web':
|
||||
codeGenerator = WebCodeGenerator(data);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
await mapsFile.writeAsString(KeyboardMapsCodeGenerator(physicalData, logicalData).generate());
|
||||
|
||||
final Map<String, PlatformCodeGenerator> platforms = <String, PlatformCodeGenerator>{
|
||||
'android': AndroidCodeGenerator(
|
||||
physicalData,
|
||||
logicalData,
|
||||
),
|
||||
'macos': MacOsCodeGenerator(
|
||||
physicalData,
|
||||
logicalData,
|
||||
),
|
||||
'windows': WindowsCodeGenerator(
|
||||
physicalData,
|
||||
logicalData,
|
||||
readDataFile('windows_scancode_logical_map.json')
|
||||
),
|
||||
'linux': GtkCodeGenerator(
|
||||
physicalData,
|
||||
logicalData,
|
||||
readDataFile('gtk_modifier_bit_mapping.json'),
|
||||
readDataFile('gtk_lock_bit_mapping.json'),
|
||||
),
|
||||
'web': WebCodeGenerator(
|
||||
physicalData,
|
||||
logicalData,
|
||||
readDataFile('web_logical_location_mapping.json'),
|
||||
),
|
||||
};
|
||||
await Future.wait(platforms.entries.map((MapEntry<String, PlatformCodeGenerator> entry) {
|
||||
final String platform = entry.key;
|
||||
final PlatformCodeGenerator codeGenerator = entry.value;
|
||||
final File platformFile = File(codeGenerator.outputPath(platform));
|
||||
if (!platformFile.existsSync()) {
|
||||
platformFile.createSync(recursive: true);
|
||||
}
|
||||
print('Writing ${'$platform map'.padRight(15)}${platformFile.absolute}');
|
||||
await platformFile.writeAsString(codeGenerator.generate());
|
||||
}
|
||||
return platformFile.writeAsString(codeGenerator.generate());
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user