windows_code_gen.dart 3.82 KB
Newer Older
1 2 3 4 5 6 7
// 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';
8
import 'constants.dart';
9 10
import 'logical_key_data.dart';
import 'physical_key_data.dart';
11 12
import 'utils.dart';

13
/// Generates the key mapping for Windows, based on the information in the key
14 15
/// data structure given to it.
class WindowsCodeGenerator extends PlatformCodeGenerator {
16 17 18 19 20 21
  WindowsCodeGenerator(
    PhysicalKeyData keyData,
    LogicalKeyData logicalData,
    String scancodeToLogical,
  ) : _scancodeToLogical = parseMapOfString(scancodeToLogical),
      super(keyData, logicalData);
22 23 24

  /// This generates the map of Windows scan codes to physical keys.
  String get _windowsScanCodeMap {
25
    final OutputLines<int> lines = OutputLines<int>('Windows scancode map');
26
    for (final PhysicalKeyEntry entry in keyData.entries) {
27
      if (entry.windowsScanCode != null) {
28 29
        lines.add(entry.windowsScanCode!,
            '        {${toHex(entry.windowsScanCode)}, ${toHex(entry.usbHidCode)}},  // ${entry.constantName}');
30 31
      }
    }
32
    return lines.sortedJoin().trimRight();
33 34
  }

35 36
  /// This generates the map of Windows key codes to logical keys.
  String get _windowsLogicalKeyCodeMap {
37
    final OutputLines<int> lines = OutputLines<int>('Windows logical map');
38
    for (final LogicalKeyEntry entry in logicalData.entries) {
39 40 41 42 43 44 45
      zipStrict(entry.windowsValues, entry.windowsNames,
        (int windowsValue, String windowsName) {
          lines.add(windowsValue,
              '        {${toHex(windowsValue)}, ${toHex(entry.value, digits: 11)}},  '
              '// $windowsName -> ${entry.constantName}');
        },
      );
46
    }
47
    return lines.sortedJoin().trimRight();
48 49
  }

50 51 52 53 54 55
  /// This generates the map from scan code to logical keys.
  ///
  /// Normally logical keys should only be derived from key codes, but since some
  /// key codes are either 0 or ambiguous (multiple keys using the same key
  /// code), these keys are resolved by scan codes.
  String get _scanCodeToLogicalMap {
56
    final OutputLines<int> lines = OutputLines<int>('Windows scancode to logical map');
57 58
    _scancodeToLogical.forEach((String scanCodeName, String logicalName) {
      final PhysicalKeyEntry physicalEntry = keyData.entryByName(scanCodeName);
59 60 61 62
      final LogicalKeyEntry logicalEntry = logicalData.entryByName(logicalName);
      lines.add(physicalEntry.windowsScanCode!,
          '        {${toHex(physicalEntry.windowsScanCode)}, ${toHex(logicalEntry.value, digits: 11)}},  '
          '// ${physicalEntry.constantName} -> ${logicalEntry.constantName}');
63
    });
64
    return lines.sortedJoin().trimRight();
65
  }
66 67
  final Map<String, String> _scancodeToLogical;

68 69 70 71 72 73 74 75 76 77 78 79 80 81
  /// 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();
  }

82 83
  @override
  String get templatePath => path.join(dataRoot, 'windows_flutter_key_map_cc.tmpl');
84 85

  @override
86 87
  String outputPath(String platform) => path.join(PlatformCodeGenerator.engineRoot,
      'shell', 'platform', 'windows', 'flutter_key_map.cc');
88 89 90 91 92

  @override
  Map<String, String> mappings() {
    return <String, String>{
      'WINDOWS_SCAN_CODE_MAP': _windowsScanCodeMap,
93 94
      'WINDOWS_SCAN_CODE_TO_LOGICAL_MAP': _scanCodeToLogicalMap,
      'WINDOWS_KEY_CODE_MAP': _windowsLogicalKeyCodeMap,
95
      'MASK_CONSTANTS': _maskConstants,
96 97 98
    };
  }
}