diff --git a/dev/tools/gen_keycodes/bin/gen_keycodes.dart b/dev/tools/gen_keycodes/bin/gen_keycodes.dart index d8c22d6c23..dc4241efef 100644 --- a/dev/tools/gen_keycodes/bin/gen_keycodes.dart +++ b/dev/tools/gen_keycodes/bin/gen_keycodes.dart @@ -7,10 +7,11 @@ import 'dart:convert'; import 'dart:io' hide Platform; import 'package:args/args.dart'; +import 'package:gen_keycodes/cc_code_gen.dart'; import 'package:http/http.dart' as http; import 'package:path/path.dart' as path; -import 'package:gen_keycodes/code_gen.dart'; +import 'package:gen_keycodes/dart_code_gen.dart'; import 'package:gen_keycodes/key_data.dart'; import 'package:gen_keycodes/utils.dart'; @@ -215,7 +216,24 @@ Future main(List rawArguments) async { mapsFile.createSync(recursive: true); } - final CodeGenerator generator = CodeGenerator(data); + final DartCodeGenerator generator = DartCodeGenerator(data); await codeFile.writeAsString(generator.generateKeyboardKeys()); await mapsFile.writeAsString(generator.generateKeyboardMaps()); + + final CcCodeGenerator ccCodeGenerator = CcCodeGenerator(data); + for (final String platform in ['android', 'darwin', 'glfw', 'fuchsia', 'linux', 'windows']) { + final File platformFile = File(path.join(flutterRoot.path, '..', path.join('engine', 'src', 'flutter', 'shell', 'platform', platform, 'keycodes', 'keyboard_map_$platform.h'))); + if (!platformFile.existsSync()) { + platformFile.createSync(recursive: true); + } + print('Writing map ${platformFile.absolute}'); + await platformFile.writeAsString(ccCodeGenerator.generateKeyboardMaps(platform)); + } + + final File webPlatformFile = File(path.join(flutterRoot.path, '..', 'engine', 'src', 'flutter', path.join('lib', 'web_ui', 'lib', 'src', 'engine', 'keycodes', 'keyboard_map_web.dart'))); + if (!webPlatformFile.existsSync()) { + webPlatformFile.createSync(recursive: true); + } + print('Writing map ${webPlatformFile.absolute}'); + await webPlatformFile.writeAsString(generator.generateWebKeyboardMap()); } diff --git a/dev/tools/gen_keycodes/data/key_data.json b/dev/tools/gen_keycodes/data/key_data.json index f3b28991ec..b4c56f7539 100644 --- a/dev/tools/gen_keycodes/data/key_data.json +++ b/dev/tools/gen_keycodes/data/key_data.json @@ -191,6 +191,29 @@ "windows": null } }, + "privacyScreenToggle": { + "names": { + "domkey": "PrivacyScreenToggle", + "android": null, + "english": "Privacy Screen Toggle", + "chromium": "privacyScreenToggle", + "glfw": null, + "windows": null + }, + "scanCodes": { + "android": null, + "usb": 23, + "linux": 633, + "xkb": 641, + "windows": null, + "macos": null + }, + "keyCodes": { + "android": null, + "glfw": null, + "windows": null + } + }, "sleep": { "names": { "domkey": "Sleep", @@ -8253,4 +8276,4 @@ "windows": null } } -} \ No newline at end of file +} diff --git a/dev/tools/gen_keycodes/data/keyboard_map_android_cc.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_android_cc.tmpl new file mode 100644 index 0000000000..aaf84d943a --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_android_cc.tmpl @@ -0,0 +1,30 @@ +// 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. + +#include + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps_android_cxx.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +// Maps Android-specific key codes to the matching LogicalKeyboardKey id. +const std::map g_android_to_logical_key = { +@@@ANDROID_KEY_CODE_MAP@@@ +}; + +// Maps Android-specific scan codes to the matching PhysicalKeyboardKey id (a.k.a. HID USB code). +const std::map g_android_to_physical_key = { +@@@ANDROID_SCAN_CODE_MAP@@@ +}; + +// A map of Android key codes which have printable representations, but appear +// on the number pad. Used to provide different key objects for keys like +// KEY_EQUALS and NUMPAD_EQUALS. Maps Android key codes to PhysicalKeyboardKey +// codes (USB HID codes). +const std::map g_android_numpad_map = { +@@@ANDROID_NUMPAD_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/data/keyboard_map_darwin_cc.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_darwin_cc.tmpl new file mode 100644 index 0000000000..96c0d9efb3 --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_darwin_cc.tmpl @@ -0,0 +1,32 @@ +// 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. + +#include + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps_android_cxx.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +// Maps macOS-specific key code values representing [PhysicalKeyboardKey]. +// +// MacOS doesn't provide a scan code, but a virtual keycode to represent a physical key. +const std::map g_macos_to_physical_key = { +@@@MACOS_SCAN_CODE_MAP@@@ +}; + +// A map of macOS key codes which have printable representations, but appear +// on the number pad. Used to provide different key objects for keys like +// KEY_EQUALS and NUMPAD_EQUALS. +const std::map g_macos_numpad_map = { +@@@MACOS_NUMPAD_MAP@@@ +}; + +// A map of macOS key codes which are numbered function keys, so that they +// can be excluded when asking "is the Fn modifier down?". +const std::map g_macos_function_key_map = { +@@@MACOS_FUNCTION_KEY_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/data/keyboard_map_fuchsia_cc.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_fuchsia_cc.tmpl new file mode 100644 index 0000000000..01d8dbf81b --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_fuchsia_cc.tmpl @@ -0,0 +1,23 @@ +// 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. + +#include + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps_android_cxx.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +/// Maps Fuchsia-specific IDs to the matching LogicalKeyboardKey. +const std::map g_fuchsia_to_logical_key = { +@@@FUCHSIA_KEY_CODE_MAP@@@ +}; + +/// Maps Fuchsia-specific USB HID Usage IDs to the matching +/// [PhysicalKeyboardKey]. +const std::map g_fuchsia_to_physical_key = { +@@@FUCHSIA_SCAN_CODE_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/data/keyboard_map_glfw_cc.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_glfw_cc.tmpl new file mode 100644 index 0000000000..a2fce7f139 --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_glfw_cc.tmpl @@ -0,0 +1,24 @@ +// 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. + +#include + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps_android_cxx.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +/// Maps GLFW-specific key codes to the matching [LogicalKeyboardKey]. +const std::map g_glfw_to_logical_key = { +@@@GLFW_KEY_CODE_MAP@@@ +}; + +/// A map of GLFW key codes which have printable representations, but appear +/// on the number pad. Used to provide different key objects for keys like +/// KEY_EQUALS and NUMPAD_EQUALS. +const std::map g_glfw_numpad_map = { +@@@GLFW_NUMPAD_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/data/keyboard_map_linux_cc.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_linux_cc.tmpl new file mode 100644 index 0000000000..149cc4826b --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_linux_cc.tmpl @@ -0,0 +1,17 @@ +// 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. + +#include + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps_android_cxx.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +/// Maps XKB specific key code values representing [PhysicalKeyboardKey]. +const std::map g_xkb_to_physical_key = { +@@@XKB_SCAN_CODE_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/data/keyboard_map_web.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_web.tmpl new file mode 100644 index 0000000000..b017be085e --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_web.tmpl @@ -0,0 +1,27 @@ +// 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. + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +/// Maps Web KeyboardEvent codes to the matching LogicalKeyboardKey id. +const Map kWebToLogicalKey = { +@@@WEB_LOGICAL_KEY_CODE_MAP@@@ +}; + +/// Maps Web KeyboardEvent codes to the matching PhysicalKeyboardKey USB HID code. +const Map kWebToPhysicalKey = { +@@@WEB_PHYSICAL_KEY_CODE_MAP@@@ +}; + +/// A map of Web KeyboardEvent codes which have printable representations, but appear +/// on the number pad. Used to provide different key objects for keys like +/// KEY_EQUALS and NUMPAD_EQUALS. +const Map kWebNumPadMap = { +@@@WEB_NUMPAD_CODE_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/data/keyboard_map_windows_cc.tmpl b/dev/tools/gen_keycodes/data/keyboard_map_windows_cc.tmpl new file mode 100644 index 0000000000..48b28913a3 --- /dev/null +++ b/dev/tools/gen_keycodes/data/keyboard_map_windows_cc.tmpl @@ -0,0 +1,29 @@ +// 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. + +#include + +// DO NOT EDIT -- DO NOT EDIT -- DO NOT EDIT +// This file is generated by flutter/flutter@dev/tools/gen_keycodes/bin/gen_keycodes.dart and +// should not be edited directly. +// +// Edit the template dev/tools/gen_keycodes/data/keyboard_maps_windows_cc.tmpl instead. +// See dev/tools/gen_keycodes/README.md for more information. + +/// Maps g_windows_to_physical_key specific key code values representing [PhysicalKeyboardKey]. +const std::map g_windows_to_physical_key = { +@@@WINDOWS_SCAN_CODE_MAP@@@ +}; + +/// Maps Windows-specific key codes to the matching [LogicalKeyboardKey]. +const std::map g_windows_to_logical_key = { +@@@WINDOWS_KEY_CODE_MAP@@@ +}; + +/// A map of GLFW key codes which have printable representations, but appear +/// on the number pad. Used to provide different key objects for keys like +/// KEY_EQUALS and NUMPAD_EQUALS. +const std::map g_windows_numpad_map = { +@@@WINDOWS_NUMPAD_MAP@@@ +}; diff --git a/dev/tools/gen_keycodes/lib/cc_code_gen.dart b/dev/tools/gen_keycodes/lib/cc_code_gen.dart new file mode 100644 index 0000000000..341403a999 --- /dev/null +++ b/dev/tools/gen_keycodes/lib/cc_code_gen.dart @@ -0,0 +1,307 @@ +// 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 'dart:io'; +import 'package:path/path.dart' as path; + +import 'package:gen_keycodes/key_data.dart'; +import 'package:gen_keycodes/utils.dart'; + +/// Generates the keyboard_keys.dart and keyboard_maps.dart files, based on the +/// information in the key data structure given to it. +class CcCodeGenerator { + CcCodeGenerator(this.keyData); + + /// Given an [input] string, wraps the text at 80 characters and prepends each + /// line with the [prefix] string. Use for generated comments. + String wrapString(String input, {String prefix = ' // '}) { + final int wrapWidth = 80 - prefix.length; + final StringBuffer result = StringBuffer(); + final List words = input.split(RegExp(r'\s+')); + String currentLine = words.removeAt(0); + for (final String word in words) { + if ((currentLine.length + word.length) < wrapWidth) { + currentLine += ' $word'; + } else { + result.writeln('$prefix$currentLine'); + currentLine = word; + } + } + if (currentLine.isNotEmpty) { + result.writeln('$prefix$currentLine'); + } + return result.toString(); + } + + List get numpadKeyData { + return keyData.data.where((Key entry) { + return entry.constantName.startsWith('numpad') && entry.keyLabel != null; + }).toList(); + } + + List get functionKeyData { + final RegExp functionKeyRe = RegExp(r'^f[0-9]+$'); + return keyData.data.where((Key entry) { + return functionKeyRe.hasMatch(entry.constantName); + }).toList(); + } + + /// This generates the map of Flutter key codes to logical keys. + String get predefinedKeyCodeMap { + final StringBuffer keyCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + keyCodeMap.writeln(' ${toHex(entry.flutterId, digits: 10)}: ${entry.constantName},'); + } + for (final String entry in Key.synonyms.keys) { + // 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 Key primaryKey = keyData.data.firstWhere((Key item) { + return item.name == Key.synonyms[entry][0]; + }, orElse: () => null); + assert(primaryKey != null); + keyCodeMap.writeln(' ${toHex(Key.synonymPlane | primaryKey.flutterId, digits: 10)}: $entry,'); + } + return keyCodeMap.toString().trimRight(); + } + + /// This generates the map of GLFW number pad key codes to logical keys. + String get glfwNumpadMap { + final StringBuffer glfwNumpadMap = StringBuffer(); + for (final Key entry in numpadKeyData) { + if (entry.glfwKeyCodes != null) { + for (final int code in entry.glfwKeyCodes.cast()) { + glfwNumpadMap.writeln(' { $code, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + } + return glfwNumpadMap.toString().trimRight(); + } + + /// This generates the map of GLFW key codes to logical keys. + String get glfwKeyCodeMap { + final StringBuffer glfwKeyCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.glfwKeyCodes != null) { + for (final int code in entry.glfwKeyCodes.cast()) { + glfwKeyCodeMap.writeln(' { $code, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + } + return glfwKeyCodeMap.toString().trimRight(); + } + + /// This generates the map of XKB scan codes to USB HID codes. + String get xkbScanCodeMap { + final StringBuffer xkbScanCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.xKbScanCode != null) { + xkbScanCodeMap.writeln(' { ${toHex(entry.xKbScanCode)}, ${toHex(entry.usbHidCode)} }, // ${entry.constantName}'); + } + } + return xkbScanCodeMap.toString().trimRight(); + } + + /// This generates the map of Android key codes to logical keys. + String get androidKeyCodeMap { + final StringBuffer androidKeyCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.androidKeyCodes != null) { + for (final int code in entry.androidKeyCodes.cast()) { + androidKeyCodeMap.writeln(' { $code, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + } + return androidKeyCodeMap.toString().trimRight(); + } + + /// This generates the map of Android number pad key codes to logical keys. + String get androidNumpadMap { + final StringBuffer androidKeyCodeMap = StringBuffer(); + for (final Key entry in numpadKeyData) { + if (entry.androidKeyCodes != null) { + for (final int code in entry.androidKeyCodes.cast()) { + androidKeyCodeMap.writeln(' { $code, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + } + return androidKeyCodeMap.toString().trimRight(); + } + + /// This generates the map of Android scan codes to physical keys. + String get androidScanCodeMap { + final StringBuffer androidScanCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.androidScanCodes != null) { + for (final int code in entry.androidScanCodes.cast()) { + androidScanCodeMap.writeln(' { $code, ${toHex(entry.usbHidCode)} }, // ${entry.constantName}'); + } + } + } + return androidScanCodeMap.toString().trimRight(); + } + + /// This generates the map of Windows scan codes to physical keys. + String get windowsScanCodeMap { + final StringBuffer windowsScanCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.windowsScanCode != null) { + windowsScanCodeMap.writeln(' { ${entry.windowsScanCode}, ${toHex(entry.usbHidCode)} }, // ${entry.constantName}'); + } + } + return windowsScanCodeMap.toString().trimRight(); + } + + /// This generates the map of Windows number pad key codes to logical keys. + String get windowsNumpadMap { + final StringBuffer windowsNumPadMap = StringBuffer(); + for (final Key entry in numpadKeyData) { + if (entry.windowsScanCode != null) { + windowsNumPadMap.writeln(' { ${toHex(entry.windowsScanCode)}, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + return windowsNumPadMap.toString().trimRight(); + } + + /// This generates the map of Android key codes to logical keys. + String get windowsKeyCodeMap { + final StringBuffer windowsKeyCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.windowsKeyCodes != null) { + for (final int code in entry.windowsKeyCodes.cast()) { + windowsKeyCodeMap.writeln(' { $code, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + } + return windowsKeyCodeMap.toString().trimRight(); + } + + /// This generates the map of macOS key codes to physical keys. + String get macOsScanCodeMap { + final StringBuffer macOsScanCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.macOsScanCode != null) { + macOsScanCodeMap.writeln(' { ${toHex(entry.macOsScanCode)}, ${toHex(entry.usbHidCode)} }, // ${entry.constantName}'); + } + } + return macOsScanCodeMap.toString().trimRight(); + } + + /// This generates the map of macOS number pad key codes to logical keys. + String get macOsNumpadMap { + final StringBuffer macOsNumPadMap = StringBuffer(); + for (final Key entry in numpadKeyData) { + if (entry.macOsScanCode != null) { + macOsNumPadMap.writeln(' { ${toHex(entry.macOsScanCode)}, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + return macOsNumPadMap.toString().trimRight(); + } + + String get macOsFunctionKeyMap { + final StringBuffer macOsFunctionKeyMap = StringBuffer(); + for (final Key entry in functionKeyData) { + if (entry.macOsScanCode != null) { + macOsFunctionKeyMap.writeln(' { ${toHex(entry.macOsScanCode)}, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + return macOsFunctionKeyMap.toString().trimRight(); + } + + /// This generates the map of Fuchsia key codes to logical keys. + String get fuchsiaKeyCodeMap { + final StringBuffer fuchsiaKeyCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.usbHidCode != null) { + fuchsiaKeyCodeMap.writeln(' { ${toHex(entry.flutterId)}, ${toHex(entry.flutterId, digits: 10)} }, // ${entry.constantName}'); + } + } + return fuchsiaKeyCodeMap.toString().trimRight(); + } + + /// This generates the map of Fuchsia USB HID codes to physical keys. + String get fuchsiaHidCodeMap { + final StringBuffer fuchsiaScanCodeMap = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.usbHidCode != null) { + fuchsiaScanCodeMap.writeln(' { ${toHex(entry.usbHidCode)}, ${toHex(entry.usbHidCode)} }, // ${entry.constantName}'); + } + } + return fuchsiaScanCodeMap.toString().trimRight(); + } + + /// This generates the map of Web KeyboardEvent codes to logical keys. + String get webLogicalKeyMap { + final StringBuffer result = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.name != null) { + result.writeln(" '${entry.name}': ${toHex(entry.flutterId, digits: 10)}, // ${entry.constantName}"); + } + } + return result.toString().trimRight(); + } + + /// This generates the map of Web KeyboardEvent codes to physical keys. + String get webPhysicalKeyMap { + final StringBuffer result = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.name != null) { + result.writeln(" '${entry.name}': ${toHex(entry.usbHidCode)}, // ${entry.constantName}"); + } + } + return result.toString().trimRight(); + } + + /// This generates the map of Web number pad codes to logical keys. + String get webNumpadMap { + final StringBuffer result = StringBuffer(); + for (final Key entry in numpadKeyData) { + if (entry.name != null) { + result.writeln(" '${entry.name}': ${toHex(entry.flutterId, digits: 10)}, // ${entry.constantName}"); + } + } + return result.toString().trimRight(); + } + + /// Substitutes the various platform specific maps into the template file for + /// keyboard_maps.dart. + String generateKeyboardMaps(String platform) { + // There is no macOS keycode map since macOS uses keycode to represent a physical key. + // The LogicalKeyboardKey is generated by raw_keyboard_macos.dart from the unmodified characters + // from NSEvent. + final Map mappings = { + 'ANDROID_SCAN_CODE_MAP': androidScanCodeMap, + 'ANDROID_KEY_CODE_MAP': androidKeyCodeMap, + 'ANDROID_NUMPAD_MAP': androidNumpadMap, + 'FUCHSIA_SCAN_CODE_MAP': fuchsiaHidCodeMap, + 'FUCHSIA_KEY_CODE_MAP': fuchsiaKeyCodeMap, + 'MACOS_SCAN_CODE_MAP': macOsScanCodeMap, + 'MACOS_NUMPAD_MAP': macOsNumpadMap, + 'MACOS_FUNCTION_KEY_MAP': macOsFunctionKeyMap, + 'GLFW_KEY_CODE_MAP': glfwKeyCodeMap, + 'GLFW_NUMPAD_MAP': glfwNumpadMap, + 'XKB_SCAN_CODE_MAP': xkbScanCodeMap, + 'WEB_LOGICAL_KEY_MAP': webLogicalKeyMap, + 'WEB_PHYSICAL_KEY_MAP': webPhysicalKeyMap, + 'WEB_NUMPAD_MAP': webNumpadMap, + 'WINDOWS_SCAN_CODE_MAP': windowsScanCodeMap, + 'WINDOWS_NUMPAD_MAP': windowsNumpadMap, + 'WINDOWS_KEY_CODE_MAP': windowsKeyCodeMap, + }; + + final String template = File(path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'keyboard_map_${platform}_cc.tmpl')).readAsStringSync(); + return _injectDictionary(template, mappings); + } + + /// The database of keys loaded from disk. + final KeyData keyData; + + static String _injectDictionary(String template, Map dictionary) { + String result = template; + for (final String key in dictionary.keys) { + result = result.replaceAll('@@@$key@@@', dictionary[key]); + } + return result; + } +} diff --git a/dev/tools/gen_keycodes/lib/code_gen.dart b/dev/tools/gen_keycodes/lib/dart_code_gen.dart similarity index 88% rename from dev/tools/gen_keycodes/lib/code_gen.dart rename to dev/tools/gen_keycodes/lib/dart_code_gen.dart index 7d80476130..07ddb9929e 100644 --- a/dev/tools/gen_keycodes/lib/code_gen.dart +++ b/dev/tools/gen_keycodes/lib/dart_code_gen.dart @@ -10,8 +10,8 @@ import 'package:gen_keycodes/utils.dart'; /// Generates the keyboard_keys.dart and keyboard_maps.dart files, based on the /// information in the key data structure given to it. -class CodeGenerator { - CodeGenerator(this.keyData); +class DartCodeGenerator { + DartCodeGenerator(this.keyData); /// Given an [input] string, wraps the text at 80 characters and prepends each /// line with the [prefix] string. Use for generated comments. @@ -130,7 +130,7 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK return scanCodeMap.toString().trimRight(); } - /// THis generates the map of Flutter key codes to logical keys. + /// This generates the map of Flutter key codes to logical keys. String get predefinedKeyCodeMap { final StringBuffer keyCodeMap = StringBuffer(); for (final Key entry in keyData.data) { @@ -312,6 +312,39 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK return result.toString().trimRight(); } + /// This generates the map of Web KeyboardEvent codes to logical key ids. + String get webLogicalKeyCodeMap { + final StringBuffer result = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.name != null) { + result.writeln(" '${entry.name}': ${toHex(entry.flutterId, digits: 10)},"); + } + } + return result.toString().trimRight(); + } + + /// This generates the map of Web KeyboardEvent codes to physical key USB HID codes. + String get webPhysicalKeyCodeMap { + final StringBuffer result = StringBuffer(); + for (final Key entry in keyData.data) { + if (entry.name != null) { + result.writeln(" '${entry.name}': ${toHex(entry.usbHidCode)},"); + } + } + return result.toString().trimRight(); + } + + /// This generates the map of Web number pad codes to logical key ids. + String get webNumpadCodeMap { + final StringBuffer result = StringBuffer(); + for (final Key entry in numpadKeyData) { + if (entry.name != null) { + result.writeln(" '${entry.name}': ${toHex(entry.flutterId, digits: 10)},"); + } + } + return result.toString().trimRight(); + } + /// Substitutes the various maps and definitions into the template file for /// keyboard_key.dart. String generateKeyboardKeys() { @@ -354,6 +387,19 @@ $otherComments static const LogicalKeyboardKey $constantName = LogicalKeyboardK return _injectDictionary(template, mappings); } + /// Substitutes the web specific maps into the template file for + /// keyboard_map_web.dart in the engine. + String generateWebKeyboardMap() { + final Map mappings = { + 'WEB_LOGICAL_KEY_CODE_MAP': webLogicalKeyCodeMap, + 'WEB_PHYSICAL_KEY_CODE_MAP': webPhysicalKeyCodeMap, + 'WEB_NUMPAD_CODE_MAP': webNumpadCodeMap, + }; + + final String template = File(path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'keyboard_map_web.tmpl')).readAsStringSync(); + return _injectDictionary(template, mappings); + } + /// The database of keys loaded from disk. final KeyData keyData;