windows_code_gen.dart 3.77 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
  WindowsCodeGenerator(
17 18
    super.keyData,
    super.logicalData,
19
    String scancodeToLogical,
20
  ) : _scancodeToLogical = parseMapOfString(scancodeToLogical);
21 22 23

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

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

49 50 51 52 53 54
  /// 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 {
55
    final OutputLines<int> lines = OutputLines<int>('Windows scancode to logical map');
56 57
    _scancodeToLogical.forEach((String scanCodeName, String logicalName) {
      final PhysicalKeyEntry physicalEntry = keyData.entryByName(scanCodeName);
58 59 60 61
      final LogicalKeyEntry logicalEntry = logicalData.entryByName(logicalName);
      lines.add(physicalEntry.windowsScanCode!,
          '        {${toHex(physicalEntry.windowsScanCode)}, ${toHex(logicalEntry.value, digits: 11)}},  '
          '// ${physicalEntry.constantName} -> ${logicalEntry.constantName}');
62
    });
63
    return lines.sortedJoin().trimRight();
64
  }
65 66
  final Map<String, String> _scancodeToLogical;

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

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

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

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