InputState.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. /**
  3. * Copyright 2023 Google LLC.
  4. * Copyright (c) Microsoft Corporation.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.InputState = void 0;
  20. const protocol_js_1 = require("../../../protocol/protocol.js");
  21. const Mutex_js_1 = require("../../../utils/Mutex.js");
  22. const InputSource_js_1 = require("./InputSource.js");
  23. class InputState {
  24. cancelList = [];
  25. #sources = new Map();
  26. #mutex = new Mutex_js_1.Mutex();
  27. getOrCreate(id, type, subtype) {
  28. let source = this.#sources.get(id);
  29. if (!source) {
  30. switch (type) {
  31. case InputSource_js_1.SourceType.None:
  32. source = new InputSource_js_1.NoneSource();
  33. break;
  34. case InputSource_js_1.SourceType.Key:
  35. source = new InputSource_js_1.KeySource();
  36. break;
  37. case InputSource_js_1.SourceType.Pointer: {
  38. let pointerId = subtype === protocol_js_1.Input.PointerType.Mouse ? 0 : 2;
  39. const pointerIds = new Set();
  40. for (const [, source] of this.#sources) {
  41. if (source.type === InputSource_js_1.SourceType.Pointer) {
  42. pointerIds.add(source.pointerId);
  43. }
  44. }
  45. while (pointerIds.has(pointerId)) {
  46. ++pointerId;
  47. }
  48. source = new InputSource_js_1.PointerSource(pointerId, subtype);
  49. break;
  50. }
  51. case InputSource_js_1.SourceType.Wheel:
  52. source = new InputSource_js_1.WheelSource();
  53. break;
  54. default:
  55. throw new protocol_js_1.Message.InvalidArgumentException(`Expected "${InputSource_js_1.SourceType.None}", "${InputSource_js_1.SourceType.Key}", "${InputSource_js_1.SourceType.Pointer}", or "${InputSource_js_1.SourceType.Wheel}". Found unknown source type ${type}.`);
  56. }
  57. this.#sources.set(id, source);
  58. return source;
  59. }
  60. if (source.type !== type) {
  61. throw new protocol_js_1.Message.InvalidArgumentException(`Input source type of ${id} is ${source.type}, but received ${type}.`);
  62. }
  63. return source;
  64. }
  65. get(id) {
  66. const source = this.#sources.get(id);
  67. if (!source) {
  68. throw new protocol_js_1.Message.UnknownErrorException(`Internal error.`);
  69. }
  70. return source;
  71. }
  72. getGlobalKeyState() {
  73. const state = new InputSource_js_1.KeySource();
  74. for (const [, source] of this.#sources) {
  75. if (source.type !== InputSource_js_1.SourceType.Key) {
  76. continue;
  77. }
  78. for (const pressed of source.pressed) {
  79. state.pressed.add(pressed);
  80. }
  81. state.alt ||= source.alt;
  82. state.ctrl ||= source.ctrl;
  83. state.meta ||= source.meta;
  84. state.shift ||= source.shift;
  85. }
  86. return state;
  87. }
  88. get queue() {
  89. return this.#mutex;
  90. }
  91. }
  92. exports.InputState = InputState;
  93. //# sourceMappingURL=InputState.js.map