keyboard_maps_code_gen.dart 14.4 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 11
import 'logical_key_data.dart';
import 'physical_key_data.dart';
12
import 'utils.dart';
13

14 15 16 17 18 19 20 21 22 23 24 25
bool _isAsciiLetter(String? char) {
  if (char == null)
    return false;
  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);
}
26

27 28 29 30 31 32 33 34 35 36
bool _isDigit(String? char) {
  if (char == null)
    return false;
  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;
}

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

42 43 44
  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
45 46 47
    }).toList();
  }

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

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

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

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

84
  /// This generates the map of GTK number pad key codes to logical keys.
85 86 87 88 89
  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},');
90 91
      }
    }
92
    return lines.sortedJoin().trimRight();
93 94 95
  }

  /// This generates the map of GTK key codes to logical keys.
96 97 98 99 100
  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},');
101 102
      }
    }
103
    return lines.sortedJoin().trimRight();
104 105 106
  }

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

118
  /// This generates the map of Android key codes to logical keys.
119 120 121 122 123
  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},');
124 125
      }
    }
126
    return lines.sortedJoin().trimRight();
127 128 129
  }

  /// This generates the map of Android number pad key codes to logical keys.
130 131 132 133 134
  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},');
135 136
      }
    }
137
    return lines.sortedJoin().trimRight();
138 139 140
  }

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

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

  /// This generates the map of Windows number pad key codes to logical keys.
165 166 167 168 169
  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},');
170 171
      }
    }
172
    return lines.sortedJoin().trimRight();
173 174
  }

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

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

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

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

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

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

  /// This generates the map of iOS number pad key codes to logical keys.
250
  String get _iOSNumpadMap {
251 252
    final OutputLines<int> lines = OutputLines<int>('iOS numpad map');
    for (final PhysicalKeyEntry entry in _numpadKeyData) {
253 254
      if (entry.iOSScanCode != null) {
        lines.add(entry.iOSScanCode!,'  ${toHex(entry.iOSScanCode)}: LogicalKeyboardKey.${entry.constantName},');
255 256
      }
    }
257 258 259 260
    return lines.sortedJoin().trimRight();
  }

  /// This generates the map of macOS key codes to physical keys.
261
  String get _iOSKeyCodeMap {
262 263
    final OutputLines<int> lines = OutputLines<int>('iOS key code map');
    for (final LogicalKeyEntry entry in logicalData.entries) {
264
      for (final int code in entry.iOSKeyCodeValues) {
265 266 267 268
        lines.add(code, '  $code: LogicalKeyboardKey.${entry.constantName},');
      }
    }
    return lines.sortedJoin().trimRight();
269 270
  }

271
  /// This generates the map of Fuchsia key codes to logical keys.
272 273 274 275 276
  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},');
277 278
      }
    }
279
    return lines.sortedJoin().trimRight();
280 281 282
  }

  /// This generates the map of Fuchsia USB HID codes to physical keys.
283
  String get _fuchsiaHidCodeMap {
284
    final StringBuffer fuchsiaScanCodeMap = StringBuffer();
285
    for (final PhysicalKeyEntry entry in keyData.entries) {
286
      if (entry.usbHidCode != null) {
287
        fuchsiaScanCodeMap.writeln('  ${toHex(entry.usbHidCode)}: PhysicalKeyboardKey.${entry.constantName},');
288 289 290 291 292
      }
    }
    return fuchsiaScanCodeMap.toString().trimRight();
  }

Mouad Debbar's avatar
Mouad Debbar committed
293
  /// This generates the map of Web KeyboardEvent codes to logical keys.
294 295 296 297 298
  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
299 300
      }
    }
301
    return lines.sortedJoin().trimRight();
Mouad Debbar's avatar
Mouad Debbar committed
302 303 304
  }

  /// This generates the map of Web KeyboardEvent codes to physical keys.
305
  String get _webPhysicalKeyMap {
306
    final OutputLines<String> lines = OutputLines<String>('Web physical key map');
307
    for (final PhysicalKeyEntry entry in keyData.entries) {
Mouad Debbar's avatar
Mouad Debbar committed
308
      if (entry.name != null) {
309
        lines.add(entry.name, "  '${entry.name}': PhysicalKeyboardKey.${entry.constantName},");
Mouad Debbar's avatar
Mouad Debbar committed
310 311
      }
    }
312
    return lines.sortedJoin().trimRight();
Mouad Debbar's avatar
Mouad Debbar committed
313 314
  }

315
  String get _webNumpadMap {
316
    final OutputLines<String> lines = OutputLines<String>('Web numpad map');
317
    for (final LogicalKeyEntry entry in _numpadLogicalKeyData) {
Mouad Debbar's avatar
Mouad Debbar committed
318
      if (entry.name != null) {
319
        lines.add(entry.name, "  '${entry.name}': LogicalKeyboardKey.${entry.constantName},");
Mouad Debbar's avatar
Mouad Debbar committed
320 321
      }
    }
322
    return lines.sortedJoin().trimRight();
Mouad Debbar's avatar
Mouad Debbar committed
323 324
  }

325 326 327 328
  /// 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);
329
    final OutputLines<String> lines = OutputLines<String>('Web location map');
330 331 332 333 334
    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(', ');
335
      lines.add(key, "  '$key': <LogicalKeyboardKey?>[$keyStrings],");
336
    });
337
    return lines.sortedJoin().trimRight();
338 339
  }

340
  @override
341
  String get templatePath => path.join(dataRoot, 'keyboard_maps.tmpl');
342 343 344 345

  @override
  Map<String, String> mappings() {
    return <String, String>{
346 347 348 349 350
      'ANDROID_SCAN_CODE_MAP': _androidScanCodeMap,
      'ANDROID_KEY_CODE_MAP': _androidKeyCodeMap,
      'ANDROID_NUMPAD_MAP': _androidNumpadMap,
      'FUCHSIA_SCAN_CODE_MAP': _fuchsiaHidCodeMap,
      'FUCHSIA_KEY_CODE_MAP': _fuchsiaKeyCodeMap,
351 352 353 354 355 356 357
      'MACOS_SCAN_CODE_MAP': _macOSScanCodeMap,
      'MACOS_NUMPAD_MAP': _macOSNumpadMap,
      'MACOS_FUNCTION_KEY_MAP': _macOSFunctionKeyMap,
      'MACOS_KEY_CODE_MAP': _macOSKeyCodeMap,
      'IOS_SCAN_CODE_MAP': _iOSScanCodeMap,
      'IOS_NUMPAD_MAP': _iOSNumpadMap,
      'IOS_KEY_CODE_MAP': _iOSKeyCodeMap,
358 359 360 361 362 363 364 365 366 367 368 369
      '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,
370 371 372
    };
  }
}