keyboard_maps_code_gen.dart 15 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:io';

7 8
import 'package:path/path.dart' as path;

9
import 'base_code_gen.dart';
10
import 'data.dart';
11 12
import 'logical_key_data.dart';
import 'physical_key_data.dart';
13
import 'utils.dart';
14

15
bool _isAsciiLetter(String? char) {
16
  if (char == null) {
17
    return false;
18
  }
19 20 21 22 23 24 25 26 27
  const int charUpperA = 0x41;
  const int charUpperZ = 0x5A;
  const int charLowerA = 0x61;
  const int charLowerZ = 0x7A;
  assert(char.length == 1);
  final int charCode = char.codeUnitAt(0);
  return (charCode >= charUpperA && charCode <= charUpperZ)
      || (charCode >= charLowerA && charCode <= charLowerZ);
}
28

29
bool _isDigit(String? char) {
30
  if (char == null) {
31
    return false;
32
  }
33 34 35 36 37 38 39
  final int charDigit0 = '0'.codeUnitAt(0);
  final int charDigit9 = '9'.codeUnitAt(0);
  assert(char.length == 1);
  final int charCode = char.codeUnitAt(0);
  return charCode >= charDigit0 && charCode <= charDigit9;
}

40
/// Generates the keyboard_maps.g.dart files, based on the information in the key
41 42
/// data structure given to it.
class KeyboardMapsCodeGenerator extends BaseCodeGenerator {
43
  KeyboardMapsCodeGenerator(super.keyData, super.logicalData);
44

45 46 47
  List<PhysicalKeyEntry> get _numpadKeyData {
    return keyData.entries.where((PhysicalKeyEntry entry) {
      return entry.constantName.startsWith('numpad') && LogicalKeyData.printable.containsKey(entry.name);
Mouad Debbar's avatar
Mouad Debbar committed
48 49 50
    }).toList();
  }

51
  List<PhysicalKeyEntry> get _functionKeyData {
52
    final RegExp functionKeyRe = RegExp(r'^f[0-9]+$');
53
    return keyData.entries.where((PhysicalKeyEntry entry) {
54 55 56 57
      return functionKeyRe.hasMatch(entry.constantName);
    }).toList();
  }

58 59 60 61 62 63
  List<LogicalKeyEntry> get _numpadLogicalKeyData {
    return logicalData.entries.where((LogicalKeyEntry entry) {
      return entry.constantName.startsWith('numpad') && LogicalKeyData.printable.containsKey(entry.name);
    }).toList();
  }

64
  /// This generates the map of GLFW number pad key codes to logical keys.
65
  String get _glfwNumpadMap {
66
    final OutputLines<int> lines = OutputLines<int>('GLFW numpad map');
67
    for (final PhysicalKeyEntry entry in _numpadKeyData) {
68 69
      final LogicalKeyEntry logicalKey = logicalData.entryByName(entry.name);
      for (final int code in logicalKey.glfwValues) {
70
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
71 72
      }
    }
73
    return lines.sortedJoin().trimRight();
74 75 76
  }

  /// This generates the map of GLFW key codes to logical keys.
77
  String get _glfwKeyCodeMap {
78
    final OutputLines<int> lines = OutputLines<int>('GLFW key code map');
79 80 81
    for (final LogicalKeyEntry entry in logicalData.entries) {
      for (final int value in entry.glfwValues) {
        lines.add(value, '  $value: LogicalKeyboardKey.${entry.constantName},');
82 83
      }
    }
84
    return lines.sortedJoin().trimRight();
85 86
  }

87
  /// This generates the map of GTK number pad key codes to logical keys.
88 89 90 91 92
  String get _gtkNumpadMap {
    final OutputLines<int> lines = OutputLines<int>('GTK numpad map');
    for (final LogicalKeyEntry entry in _numpadLogicalKeyData) {
      for (final int code in entry.gtkValues) {
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
93 94
      }
    }
95
    return lines.sortedJoin().trimRight();
96 97 98
  }

  /// This generates the map of GTK key codes to logical keys.
99 100 101 102 103
  String get _gtkKeyCodeMap {
    final OutputLines<int> lines = OutputLines<int>('GTK key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
      for (final int code in entry.gtkValues) {
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
104 105
      }
    }
106
    return lines.sortedJoin().trimRight();
107 108 109
  }

  /// This generates the map of XKB USB HID codes to physical keys.
110
  String get _xkbScanCodeMap {
111
    final OutputLines<int> lines = OutputLines<int>('GTK scancode map');
112
    for (final PhysicalKeyEntry entry in keyData.entries) {
113
      if (entry.xKbScanCode != null) {
114 115
        lines.add(entry.xKbScanCode!,
            '  ${toHex(entry.xKbScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
116 117
      }
    }
118
    return lines.sortedJoin().trimRight();
119 120
  }

121
  /// This generates the map of Android key codes to logical keys.
122 123 124 125 126
  String get _androidKeyCodeMap {
    final OutputLines<int> lines = OutputLines<int>('Android key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
      for (final int code in entry.androidValues) {
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
127 128
      }
    }
129
    return lines.sortedJoin().trimRight();
130 131 132
  }

  /// This generates the map of Android number pad key codes to logical keys.
133 134 135 136 137
  String get _androidNumpadMap {
    final OutputLines<int> lines = OutputLines<int>('Android numpad map');
    for (final LogicalKeyEntry entry in _numpadLogicalKeyData) {
      for (final int code in entry.androidValues) {
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
138 139
      }
    }
140
    return lines.sortedJoin().trimRight();
141 142 143
  }

  /// This generates the map of Android scan codes to physical keys.
144
  String get _androidScanCodeMap {
145
    final OutputLines<int> lines = OutputLines<int>('Android scancode map');
146
    for (final PhysicalKeyEntry entry in keyData.entries) {
147
      if (entry.androidScanCodes != null) {
148
        for (final int code in entry.androidScanCodes) {
149
          lines.add(code, '  $code: PhysicalKeyboardKey.${entry.constantName},');
150 151 152
        }
      }
    }
153
    return lines.sortedJoin().trimRight();
154 155
  }

156
  /// This generates the map of Windows scan codes to physical keys.
157
  String get _windowsScanCodeMap {
158
    final OutputLines<int> lines = OutputLines<int>('Windows scancode map');
159
    for (final PhysicalKeyEntry entry in keyData.entries) {
160
      if (entry.windowsScanCode != null) {
161
        lines.add(entry.windowsScanCode!, '  ${entry.windowsScanCode}: PhysicalKeyboardKey.${entry.constantName},');
162 163
      }
    }
164
    return lines.sortedJoin().trimRight();
165 166 167
  }

  /// This generates the map of Windows number pad key codes to logical keys.
168 169 170 171 172
  String get _windowsNumpadMap {
    final OutputLines<int> lines = OutputLines<int>('Windows numpad map');
    for (final LogicalKeyEntry entry in _numpadLogicalKeyData) {
      for (final int code in entry.windowsValues) {
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
173 174
      }
    }
175
    return lines.sortedJoin().trimRight();
176 177
  }

178
  /// This generates the map of Windows key codes to logical keys.
179 180 181
  String get _windowsKeyCodeMap {
    final OutputLines<int> lines = OutputLines<int>('Windows key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
182
      // Letter keys on Windows are not recorded in logical_key_data.g.json,
183 184 185
      // because they are not used by the embedding. Add them manually.
      final List<int>? keyCodes = entry.windowsValues.isNotEmpty
        ? entry.windowsValues
186 187 188
        : (_isAsciiLetter(entry.keyLabel) ? <int>[entry.keyLabel!.toUpperCase().codeUnitAt(0)] :
           _isDigit(entry.keyLabel)       ? <int>[entry.keyLabel!.toUpperCase().codeUnitAt(0)] :
           null);
189 190 191
      if (keyCodes != null) {
        for (final int code in keyCodes) {
          lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
192 193 194
        }
      }
    }
195
    return lines.sortedJoin().trimRight();
196 197
  }

198
  /// This generates the map of macOS key codes to physical keys.
199
  String get _macOSScanCodeMap {
200
    final OutputLines<int> lines = OutputLines<int>('macOS scancode map');
201
    for (final PhysicalKeyEntry entry in keyData.entries) {
202
      if (entry.macOSScanCode != null) {
203
        lines.add(entry.macOSScanCode!, '  ${toHex(entry.macOSScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
204 205
      }
    }
206
    return lines.sortedJoin().trimRight();
207 208 209
  }

  /// This generates the map of macOS number pad key codes to logical keys.
210
  String get _macOSNumpadMap {
211
    final OutputLines<int> lines = OutputLines<int>('macOS numpad map');
212
    for (final PhysicalKeyEntry entry in _numpadKeyData) {
213
      if (entry.macOSScanCode != null) {
214
        lines.add(entry.macOSScanCode!, '  ${toHex(entry.macOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
215 216
      }
    }
217
    return lines.sortedJoin().trimRight();
218 219
  }

220
  String get _macOSFunctionKeyMap {
221
    final OutputLines<int> lines = OutputLines<int>('macOS function key map');
222
    for (final PhysicalKeyEntry entry in _functionKeyData) {
223
      if (entry.macOSScanCode != null) {
224
        lines.add(entry.macOSScanCode!, '  ${toHex(entry.macOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
225 226
      }
    }
227
    return lines.sortedJoin().trimRight();
228 229
  }

230
  /// This generates the map of macOS key codes to physical keys.
231
  String get _macOSKeyCodeMap {
232 233
    final OutputLines<int> lines = OutputLines<int>('MacOS key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
234
      for (final int code in entry.macOSKeyCodeValues) {
235 236 237 238 239 240
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
      }
    }
    return lines.sortedJoin().trimRight();
  }

241
  /// This generates the map of iOS key codes to physical keys.
242
  String get _iOSScanCodeMap {
243 244
    final OutputLines<int> lines = OutputLines<int>('iOS scancode map');
    for (final PhysicalKeyEntry entry in keyData.entries) {
245 246
      if (entry.iOSScanCode != null) {
        lines.add(entry.iOSScanCode!, '  ${toHex(entry.iOSScanCode)}: PhysicalKeyboardKey.${entry.constantName},');
247 248
      }
    }
249
    return lines.sortedJoin().trimRight();
250 251
  }

252 253 254 255 256 257 258 259 260 261
  /// This generates the map of iOS key label to logical keys for special keys.
  String get _iOSSpecialMap {
    final OutputLines<int> lines = OutputLines<int>('iOS special key mapping');
    kIosSpecialKeyMapping.forEach((String key, String logicalName) {
      final LogicalKeyEntry entry = logicalData.entryByName(logicalName);
      lines.add(entry.value, "  '$key': LogicalKeyboardKey.${entry.constantName},");
    });
    return lines.join().trimRight();
  }

262
  /// This generates the map of iOS number pad key codes to logical keys.
263
  String get _iOSNumpadMap {
264 265
    final OutputLines<int> lines = OutputLines<int>('iOS numpad map');
    for (final PhysicalKeyEntry entry in _numpadKeyData) {
266 267
      if (entry.iOSScanCode != null) {
        lines.add(entry.iOSScanCode!,'  ${toHex(entry.iOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
268 269
      }
    }
270 271 272 273
    return lines.sortedJoin().trimRight();
  }

  /// This generates the map of macOS key codes to physical keys.
274
  String get _iOSKeyCodeMap {
275 276
    final OutputLines<int> lines = OutputLines<int>('iOS key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
277
      for (final int code in entry.iOSKeyCodeValues) {
278 279 280 281
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
      }
    }
    return lines.sortedJoin().trimRight();
282 283
  }

284
  /// This generates the map of Fuchsia key codes to logical keys.
285 286 287 288 289
  String get _fuchsiaKeyCodeMap {
    final OutputLines<int> lines = OutputLines<int>('Fuchsia key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
      for (final int value in entry.fuchsiaValues) {
        lines.add(value, '  ${toHex(value)}: LogicalKeyboardKey.${entry.constantName},');
290 291
      }
    }
292
    return lines.sortedJoin().trimRight();
293 294 295
  }

  /// This generates the map of Fuchsia USB HID codes to physical keys.
296
  String get _fuchsiaHidCodeMap {
297
    final StringBuffer fuchsiaScanCodeMap = StringBuffer();
298
    for (final PhysicalKeyEntry entry in keyData.entries) {
299
      if (entry.usbHidCode != null) {
300
        fuchsiaScanCodeMap.writeln('  ${toHex(entry.usbHidCode)}: PhysicalKeyboardKey.${entry.constantName},');
301 302 303 304 305
      }
    }
    return fuchsiaScanCodeMap.toString().trimRight();
  }

Mouad Debbar's avatar
Mouad Debbar committed
306
  /// This generates the map of Web KeyboardEvent codes to logical keys.
307 308 309 310 311
  String get _webLogicalKeyMap {
    final OutputLines<String> lines = OutputLines<String>('Web logical key map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
      for (final String name in entry.webNames) {
        lines.add(name, "  '$name': LogicalKeyboardKey.${entry.constantName},");
Mouad Debbar's avatar
Mouad Debbar committed
312 313
      }
    }
314
    return lines.sortedJoin().trimRight();
Mouad Debbar's avatar
Mouad Debbar committed
315 316 317
  }

  /// This generates the map of Web KeyboardEvent codes to physical keys.
318
  String get _webPhysicalKeyMap {
319
    final OutputLines<String> lines = OutputLines<String>('Web physical key map', behavior: DeduplicateBehavior.kKeep);
320
    for (final PhysicalKeyEntry entry in keyData.entries) {
321 322
      for (final String webCodes in entry.webCodes()) {
        lines.add(entry.name, "  '$webCodes': PhysicalKeyboardKey.${entry.constantName},");
Mouad Debbar's avatar
Mouad Debbar committed
323 324
      }
    }
325
    return lines.sortedJoin().trimRight();
Mouad Debbar's avatar
Mouad Debbar committed
326 327
  }

328
  String get _webNumpadMap {
329
    final OutputLines<String> lines = OutputLines<String>('Web numpad map');
330
    for (final LogicalKeyEntry entry in _numpadLogicalKeyData) {
Mouad Debbar's avatar
Mouad Debbar committed
331
      if (entry.name != null) {
332
        lines.add(entry.name, "  '${entry.name}': LogicalKeyboardKey.${entry.constantName},");
Mouad Debbar's avatar
Mouad Debbar committed
333 334
      }
    }
335
    return lines.sortedJoin().trimRight();
Mouad Debbar's avatar
Mouad Debbar committed
336 337
  }

338 339 340 341
  /// This generates the map of Web number pad codes to logical keys.
  String get _webLocationMap {
    final String jsonRaw = File(path.join(dataRoot, 'web_logical_location_mapping.json')).readAsStringSync();
    final Map<String, List<String?>> locationMap = parseMapOfListOfNullableString(jsonRaw);
342
    final OutputLines<String> lines = OutputLines<String>('Web location map');
343 344 345 346 347
    locationMap.forEach((String key, List<String?> keyNames) {
      final String keyStrings = keyNames.map((String? keyName) {
        final String? constantName = keyName == null ? null : logicalData.entryByName(keyName).constantName;
        return constantName != null ? 'LogicalKeyboardKey.$constantName' : 'null';
      }).join(', ');
348
      lines.add(key, "  '$key': <LogicalKeyboardKey?>[$keyStrings],");
349
    });
350
    return lines.sortedJoin().trimRight();
351 352
  }

353
  @override
354
  String get templatePath => path.join(dataRoot, 'keyboard_maps.tmpl');
355 356 357 358

  @override
  Map<String, String> mappings() {
    return <String, String>{
359 360 361 362 363
      'ANDROID_SCAN_CODE_MAP': _androidScanCodeMap,
      'ANDROID_KEY_CODE_MAP': _androidKeyCodeMap,
      'ANDROID_NUMPAD_MAP': _androidNumpadMap,
      'FUCHSIA_SCAN_CODE_MAP': _fuchsiaHidCodeMap,
      'FUCHSIA_KEY_CODE_MAP': _fuchsiaKeyCodeMap,
364 365 366 367 368
      'MACOS_SCAN_CODE_MAP': _macOSScanCodeMap,
      'MACOS_NUMPAD_MAP': _macOSNumpadMap,
      'MACOS_FUNCTION_KEY_MAP': _macOSFunctionKeyMap,
      'MACOS_KEY_CODE_MAP': _macOSKeyCodeMap,
      'IOS_SCAN_CODE_MAP': _iOSScanCodeMap,
369
      'IOS_SPECIAL_MAP': _iOSSpecialMap,
370 371
      'IOS_NUMPAD_MAP': _iOSNumpadMap,
      'IOS_KEY_CODE_MAP': _iOSKeyCodeMap,
372 373 374 375 376 377 378 379 380 381 382 383
      'GLFW_KEY_CODE_MAP': _glfwKeyCodeMap,
      'GLFW_NUMPAD_MAP': _glfwNumpadMap,
      'GTK_KEY_CODE_MAP': _gtkKeyCodeMap,
      'GTK_NUMPAD_MAP': _gtkNumpadMap,
      'XKB_SCAN_CODE_MAP': _xkbScanCodeMap,
      'WEB_LOGICAL_KEY_MAP': _webLogicalKeyMap,
      'WEB_PHYSICAL_KEY_MAP': _webPhysicalKeyMap,
      'WEB_NUMPAD_MAP': _webNumpadMap,
      'WEB_LOCATION_MAP': _webLocationMap,
      'WINDOWS_LOGICAL_KEY_MAP': _windowsKeyCodeMap,
      'WINDOWS_PHYSICAL_KEY_MAP': _windowsScanCodeMap,
      'WINDOWS_NUMPAD_MAP': _windowsNumpadMap,
384 385 386
    };
  }
}