Unverified Commit 289deef8 authored by Greg Spencer's avatar Greg Spencer Committed by GitHub

Add C++ code generation for key maps. (#50998)

This adds generation of some C++ keycode maps for various platforms.

This isn't meant for general use: it's so that the Flutter authors can keep the keycodes up to date with the Chromium and Android (and other) sources. It doesn't run continuously, it's only run occasionally (manually) to update the maps, because they don't change very often, and there is often manual work needed to make it function.

In this PR, I added output of various .cc files that write into an engine repo situated next to the flutter repo on disk. This is a starting point for implementing platform-side handling of key events.
parent 14d8534c
......@@ -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<void> main(List<String> 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 <String>['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());
}
......@@ -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
}
// 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 <map>
// 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<int, int> 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<int, int> 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<int, int> g_android_numpad_map = {
@@@ANDROID_NUMPAD_MAP@@@
};
// 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 <map>
// 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<int, int> 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<int, int> 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<int, int> g_macos_function_key_map = {
@@@MACOS_FUNCTION_KEY_MAP@@@
};
// 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 <map>
// 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<int, int> g_fuchsia_to_logical_key = {
@@@FUCHSIA_KEY_CODE_MAP@@@
};
/// Maps Fuchsia-specific USB HID Usage IDs to the matching
/// [PhysicalKeyboardKey].
const std::map<int, int> g_fuchsia_to_physical_key = {
@@@FUCHSIA_SCAN_CODE_MAP@@@
};
// 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 <map>
// 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<int, int> 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<int, int> g_glfw_numpad_map = {
@@@GLFW_NUMPAD_MAP@@@
};
// 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 <map>
// 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<int, int> g_xkb_to_physical_key = {
@@@XKB_SCAN_CODE_MAP@@@
};
// 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<String, int> kWebToLogicalKey = <String, int>{
@@@WEB_LOGICAL_KEY_CODE_MAP@@@
};
/// Maps Web KeyboardEvent codes to the matching PhysicalKeyboardKey USB HID code.
const Map<String, int> kWebToPhysicalKey = <String, int>{
@@@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<String, int> kWebNumPadMap = <String, int>{
@@@WEB_NUMPAD_CODE_MAP@@@
};
// 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 <map>
// 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<int, uint64_t> g_windows_to_physical_key = {
@@@WINDOWS_SCAN_CODE_MAP@@@
};
/// Maps Windows-specific key codes to the matching [LogicalKeyboardKey].
const std::map<int, uint64_t> 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<int, uint64_t> g_windows_numpad_map = {
@@@WINDOWS_NUMPAD_MAP@@@
};
This diff is collapsed.
......@@ -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<String, String> mappings = <String, String>{
'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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment