"Fossies" - the Fresh Open Source Software Archive

Member "flutter-1.22.4/dev/tools/gen_keycodes/bin/gen_keycodes.dart" (13 Nov 2020, 12273 Bytes) of package /linux/misc/flutter-1.22.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2014 The Flutter Authors. All rights reserved.
    2 // Use of this source code is governed by a BSD-style license that can be
    3 // found in the LICENSE file.
    4 
    5 import 'dart:async';
    6 import 'dart:convert';
    7 import 'dart:io' hide Platform;
    8 
    9 import 'package:args/args.dart';
   10 import 'package:http/http.dart' as http;
   11 import 'package:path/path.dart' as path;
   12 
   13 import 'package:gen_keycodes/android_code_gen.dart';
   14 import 'package:gen_keycodes/base_code_gen.dart';
   15 import 'package:gen_keycodes/macos_code_gen.dart';
   16 import 'package:gen_keycodes/fuchsia_code_gen.dart';
   17 import 'package:gen_keycodes/glfw_code_gen.dart';
   18 import 'package:gen_keycodes/gtk_code_gen.dart';
   19 import 'package:gen_keycodes/windows_code_gen.dart';
   20 import 'package:gen_keycodes/web_code_gen.dart';
   21 import 'package:gen_keycodes/keyboard_keys_code_gen.dart';
   22 import 'package:gen_keycodes/keyboard_maps_code_gen.dart';
   23 import 'package:gen_keycodes/key_data.dart';
   24 import 'package:gen_keycodes/utils.dart';
   25 
   26 /// Get contents of the file that contains the key code mapping in Chromium
   27 /// source.
   28 Future<String> getChromiumConversions() async {
   29   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');
   30   return utf8.decode(base64.decode(await http.read(keyCodesUri)));
   31 }
   32 
   33 /// Get contents of the file that contains the key codes in Android source.
   34 Future<String> getAndroidKeyCodes() async {
   35   final Uri keyCodesUri = Uri.parse('https://android.googlesource.com/platform/frameworks/native/+/master/include/android/keycodes.h?format=TEXT');
   36   return utf8.decode(base64.decode(await http.read(keyCodesUri)));
   37 }
   38 
   39 Future<String> getWindowsKeyCodes() async {
   40   final Uri keyCodesUri = Uri.parse('https://raw.githubusercontent.com/tpn/winsdk-10/master/Include/10.0.10240.0/um/WinUser.h');
   41   return await http.read(keyCodesUri);
   42 }
   43 
   44 /// Get contents of the file that contains the scan codes in Android source.
   45 /// Yes, this is just the generic keyboard layout file for base Android distro
   46 /// This is because there isn't any facility in Android to get the keyboard
   47 /// layout, so we're using this to match scan codes with symbol names for most
   48 /// common keyboards. Other than some special keyboards and game pads, this
   49 /// should be OK.
   50 Future<String> getAndroidScanCodes() async {
   51   final Uri scanCodesUri = Uri.parse('https://android.googlesource.com/platform/frameworks/base/+/master/data/keyboards/Generic.kl?format=TEXT');
   52   return utf8.decode(base64.decode(await http.read(scanCodesUri)));
   53 }
   54 
   55 Future<String> getGlfwKeyCodes() async {
   56   final Uri keyCodesUri = Uri.parse('https://raw.githubusercontent.com/glfw/glfw/master/include/GLFW/glfw3.h');
   57   return await http.read(keyCodesUri);
   58 }
   59 
   60 Future<String> getGtkKeyCodes() async {
   61   final Uri keyCodesUri = Uri.parse('https://gitlab.gnome.org/GNOME/gtk/-/raw/master/gdk/gdkkeysyms.h');
   62   return await http.read(keyCodesUri);
   63 }
   64 
   65 Future<void> main(List<String> rawArguments) async {
   66   final ArgParser argParser = ArgParser();
   67   argParser.addOption(
   68     'chromium-hid-codes',
   69     defaultsTo: null,
   70     help: 'The path to where the Chromium HID code mapping file should be '
   71         'read. If --chromium-hid-codes is not specified, the input will be read '
   72         'from the correct file in the Chromium repository.',
   73   );
   74   argParser.addOption(
   75     'supplemental-hid-codes',
   76     defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'supplemental_hid_codes.inc'),
   77     help: "The path to where the supplemental HID codes that don't appear in the "
   78         'Chromium map should be read.',
   79   );
   80   argParser.addOption(
   81     'android-keycodes',
   82     defaultsTo: null,
   83     help: 'The path to where the Android keycodes header file should be read. '
   84         'If --android-keycodes is not specified, the input will be read from the '
   85         'correct file in the Android repository.',
   86   );
   87   argParser.addOption(
   88     'android-scancodes',
   89     defaultsTo: null,
   90     help: 'The path to where the Android scancodes header file should be read. '
   91       'If --android-scancodes is not specified, the input will be read from the '
   92       'correct file in the Android repository.',
   93   );
   94   argParser.addOption(
   95     'android-domkey',
   96     defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_android_name.json'),
   97     help: 'The path to where the Android keycode to DomKey mapping is.',
   98   );
   99   argParser.addOption(
  100     'glfw-keycodes',
  101     defaultsTo: null,
  102     help: 'The path to where the GLFW keycodes header file should be read. '
  103         'If --glfw-keycodes is not specified, the input will be read from the '
  104         'correct file in the GLFW github repository.',
  105   );
  106   argParser.addOption(
  107     'gtk-keycodes',
  108     defaultsTo: null,
  109     help: 'The path to where the GTK keycodes header file should be read. '
  110         'If --gtk-keycodes is not specified, the input will be read from the '
  111         'correct file in the GTK repository.',
  112   );
  113   argParser.addOption(
  114     'windows-keycodes',
  115     defaultsTo: null,
  116     help: 'The path to where the Windows keycodes header file should be read. '
  117         'If --windows-keycodes is not specified, the input will be read from the '
  118         'correct file in the Windows github repository.',
  119   );
  120   argParser.addOption(
  121     'windows-domkey',
  122     defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_windows_name.json'),
  123     help: 'The path to where the Windows keycode to DomKey mapping is.',
  124   );
  125   argParser.addOption(
  126     'glfw-domkey',
  127     defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_glfw_name.json'),
  128     help: 'The path to where the GLFW keycode to DomKey mapping is.',
  129   );
  130   argParser.addOption(
  131     'gtk-domkey',
  132     defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_name_to_gtk_name.json'),
  133     help: 'The path to where the GTK keycode to DomKey mapping is.',
  134   );
  135   argParser.addOption(
  136     'data',
  137     defaultsTo: path.join(flutterRoot.path, 'dev', 'tools', 'gen_keycodes', 'data', 'key_data.json'),
  138     help: 'The path to where the key code data file should be written when '
  139         'collected, and read from when generating output code. If --data is '
  140         'not specified, the output will be written to/read from the current '
  141         "directory. If the output directory doesn't exist, it, and the path to "
  142         'it, will be created.',
  143   );
  144   argParser.addOption(
  145     'code',
  146     defaultsTo: path.join(flutterRoot.path, 'packages', 'flutter', 'lib', 'src', 'services', 'keyboard_key.dart'),
  147     help: 'The path to where the output "keyboard_keys.dart" file should be '
  148         'written. If --code is not specified, the output will be written to the '
  149         'correct directory in the flutter tree. If the output directory does not '
  150         'exist, it, and the path to it, will be created.',
  151   );
  152   argParser.addOption(
  153     'maps',
  154     defaultsTo: path.join(flutterRoot.path, 'packages', 'flutter', 'lib', 'src', 'services', 'keyboard_maps.dart'),
  155     help: 'The path to where the output "keyboard_maps.dart" file should be '
  156       'written. If --maps is not specified, the output will be written to the '
  157       'correct directory in the flutter tree. If the output directory does not '
  158       'exist, it, and the path to it, will be created.',
  159   );
  160   argParser.addFlag(
  161     'collect',
  162     defaultsTo: false,
  163     negatable: false,
  164     help: 'If this flag is set, then collect and parse header files from '
  165         'Chromium and Android instead of reading pre-parsed data from '
  166         '"key_data.json", and then update "key_data.json" with the fresh data.',
  167   );
  168   argParser.addFlag(
  169     'help',
  170     defaultsTo: false,
  171     negatable: false,
  172     help: 'Print help for this command.',
  173   );
  174 
  175   final ArgResults parsedArguments = argParser.parse(rawArguments);
  176 
  177   if (parsedArguments['help'] as bool) {
  178     print(argParser.usage);
  179     exit(0);
  180   }
  181 
  182   KeyData data;
  183   if (parsedArguments['collect'] as bool) {
  184     String hidCodes;
  185     if (parsedArguments['chromium-hid-codes'] == null) {
  186       hidCodes = await getChromiumConversions();
  187     } else {
  188       hidCodes = File(parsedArguments['chromium-hid-codes'] as String).readAsStringSync();
  189     }
  190 
  191     final String supplementalHidCodes = File(parsedArguments['supplemental-hid-codes'] as String).readAsStringSync();
  192     hidCodes = '$hidCodes\n$supplementalHidCodes';
  193 
  194     String androidKeyCodes;
  195     if (parsedArguments['android-keycodes'] == null) {
  196       androidKeyCodes = await getAndroidKeyCodes();
  197     } else {
  198       androidKeyCodes = File(parsedArguments['android-keycodes'] as String).readAsStringSync();
  199     }
  200 
  201     String androidScanCodes;
  202     if (parsedArguments['android-scancodes'] == null) {
  203       androidScanCodes = await getAndroidScanCodes();
  204     } else {
  205       androidScanCodes = File(parsedArguments['android-scancodes'] as String).readAsStringSync();
  206     }
  207 
  208     String glfwKeyCodes;
  209     if (parsedArguments['glfw-keycodes'] == null) {
  210       glfwKeyCodes = await getGlfwKeyCodes();
  211     } else {
  212       glfwKeyCodes = File(parsedArguments['glfw-keycodes'] as String).readAsStringSync();
  213     }
  214 
  215     String gtkKeyCodes;
  216     if (parsedArguments['gtk-keycodes'] == null) {
  217       gtkKeyCodes = await getGtkKeyCodes();
  218     } else {
  219       gtkKeyCodes = File(parsedArguments['gtk-keycodes'] as String).readAsStringSync();
  220     }
  221 
  222     String windowsKeyCodes;
  223     if (parsedArguments['windows-keycodes'] == null) {
  224       windowsKeyCodes = await getWindowsKeyCodes();
  225     } else {
  226       windowsKeyCodes = File(parsedArguments['windows-keycodes'] as String).readAsStringSync();
  227     }
  228 
  229     final String windowsToDomKey = File(parsedArguments['windows-domkey'] as String).readAsStringSync();
  230     final String glfwToDomKey = File(parsedArguments['glfw-domkey'] as String).readAsStringSync();
  231     final String gtkToDomKey = File(parsedArguments['gtk-domkey'] as String).readAsStringSync();
  232     final String androidToDomKey = File(parsedArguments['android-domkey'] as String).readAsStringSync();
  233 
  234     data = KeyData(hidCodes, androidScanCodes, androidKeyCodes, androidToDomKey, glfwKeyCodes, glfwToDomKey, gtkKeyCodes, gtkToDomKey, windowsKeyCodes, windowsToDomKey);
  235 
  236     const JsonEncoder encoder = JsonEncoder.withIndent('  ');
  237     File(parsedArguments['data'] as String).writeAsStringSync(encoder.convert(data.toJson()));
  238   } else {
  239     data = KeyData.fromJson(json.decode(await File(parsedArguments['data'] as String).readAsString()) as Map<String, dynamic>);
  240   }
  241 
  242   final File codeFile = File(parsedArguments['code'] as String);
  243   if (!codeFile.existsSync()) {
  244     codeFile.createSync(recursive: true);
  245   }
  246   print('Writing ${'key codes'.padRight(15)}${codeFile.absolute}');
  247   await codeFile.writeAsString(KeyboardKeysCodeGenerator(data).generate());
  248 
  249   final File mapsFile = File(parsedArguments['maps'] as String);
  250   if (!mapsFile.existsSync()) {
  251     mapsFile.createSync(recursive: true);
  252   }
  253   print('Writing ${'key maps'.padRight(15)}${mapsFile.absolute}');
  254   await mapsFile.writeAsString(KeyboardMapsCodeGenerator(data).generate());
  255 
  256   for (final String platform in <String>['android', 'darwin', 'glfw', 'fuchsia', 'linux', 'windows', 'web']) {
  257     PlatformCodeGenerator codeGenerator;
  258     switch (platform) {
  259       case 'glfw':
  260         codeGenerator = GlfwCodeGenerator(data);
  261         break;
  262       case 'fuchsia':
  263         codeGenerator = FuchsiaCodeGenerator(data);
  264         break;
  265       case 'android':
  266         codeGenerator = AndroidCodeGenerator(data);
  267         break;
  268       case 'darwin':
  269         codeGenerator = MacOsCodeGenerator(data);
  270         break;
  271       case 'windows':
  272         codeGenerator = WindowsCodeGenerator(data);
  273         break;
  274       case 'linux':
  275         codeGenerator = GtkCodeGenerator(data);
  276         break;
  277       case 'web':
  278         codeGenerator = WebCodeGenerator(data);
  279         break;
  280       default:
  281         assert(false);
  282     }
  283 
  284     final File platformFile = File(codeGenerator.outputPath(platform));
  285     if (!platformFile.existsSync()) {
  286       platformFile.createSync(recursive: true);
  287     }
  288     print('Writing ${'$platform map'.padRight(15)}${platformFile.absolute}');
  289     await platformFile.writeAsString(codeGenerator.generate());
  290   }
  291 }