• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import hilog from '@ohos.hilog'
17import { Callback, AsyncCallback } from '@ohos.base';
18import { BusinessError } from "@ohos.base"
19
20export namespace inputDevice {
21    loadLibrary("ani_input_device");
22
23    export native function getDeviceListInner(): Array<number>;
24    export native function getDeviceInfoInner(deviceId: number): InputDeviceData;
25
26    export native function on(type: 'change', listener: Callback<DeviceListener>): void;
27
28  /**
29   * Add or remove device
30   * @typedef { 'add' | 'remove' }
31   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
32   * @since 9
33   */
34  type ChangedType = 'add' | 'remove';
35
36  /**
37   * The type of input device
38   * @typedef { 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball' }
39   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
40   * @since 9
41   */
42  type SourceType = 'keyboard' | 'mouse' | 'touchpad' | 'touchscreen' | 'joystick' | 'trackball';
43
44  /**
45   * Axis Type of the input event
46   * @typedef { 'touchmajor'| 'touchminor' | 'orientation' | 'x' | 'y' | 'pressure' | 'toolminor' | 'toolmajor' | 'null' }
47   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
48   * @since 9
49   */
50  type AxisType =
51    'touchmajor'
52    | 'touchminor'
53    | 'orientation'
54    | 'x'
55    | 'y'
56    | 'pressure'
57    | 'toolminor'
58    | 'toolmajor'
59    | 'null';
60
61  /**
62   * @enum { number }
63   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
64   * @since 9
65   */
66  enum KeyboardType {
67    /**
68     * None
69     *
70     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
71     * @since 9
72     */
73    NONE = 0,
74
75    /**
76     * Unknown key
77     *
78     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
79     * @since 9
80     */
81    UNKNOWN = 1,
82
83    /**
84     * Alphabetical keyboard
85     *
86     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
87     * @since 9
88     */
89    ALPHABETIC_KEYBOARD = 2,
90
91    /**
92     * Digital keyboard
93     *
94     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
95     * @since 9
96     */
97    DIGITAL_KEYBOARD = 3,
98
99    /**
100     * Stylus
101     *
102     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
103     * @since 9
104     */
105    HANDWRITING_PEN = 4,
106
107    /**
108     * Remote control
109     *
110     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
111     * @since 9
112     */
113    REMOTE_CONTROL = 5
114  }
115
116  /**
117   * Enumerates function keys.
118   *
119   * @enum { number }
120   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
121   * @since 15
122   */
123  enum FunctionKey {
124    /**
125     * CapsLock key. Enabling or disabling the CapsLock key is allowed only for input keyboard extensions.
126     *
127     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
128     * @since 15
129     */
130    CAPS_LOCK = 1
131  }
132
133  /**
134   * Defines the listener for input device events.
135   *
136   * @interface DeviceListener
137   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
138   * @since 9
139   */
140  interface DeviceListener {
141    /**
142     * Type of the input device event. The options are add and remove.
143     *
144     * @type { ChangedType }
145     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
146     * @since 9
147     */
148    type: ChangedType;
149
150    /**
151     * ID of the input device for the reported input device event.
152     *
153     * @type { number }
154     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
155     * @since 9
156     */
157    deviceId: number;
158  }
159
160  interface AxisRange {
161    /**
162     * Input source type of the axis. For example, if a mouse reports an x-axis event,
163     * the source of the x-axis is the mouse.
164     *
165     * @type { SourceType }
166     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
167     * @since 8
168     */
169    source: SourceType;
170
171    /**
172     * Type of the axis. for example, the x-axis, y-axis, and pressure axis.
173     *
174     * @type { AxisType }
175     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
176     * @since 8
177     */
178    axis: AxisType;
179
180    /**
181     * Maximum value of the data reported on this axis.
182     *
183     * @type { number }
184     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
185     * @since 8
186     */
187    max: number;
188
189    /**
190     * Minimum value of the data reported on this axis.
191     *
192     * @type { number }
193     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
194     * @since 8
195     */
196    min: number;
197
198    /**
199     * Fuzz value of the data reported on this axis.
200     *
201     * @type { number }
202     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
203     * @since 9
204     */
205    fuzz: number;
206
207    /**
208     * Flat value of the data reported on this axis.
209     *
210     * @type { number }
211     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
212     * @since 9
213     */
214    flat: number;
215
216    /**
217     * Resolution value of the data reported on this axis.
218     *
219     * @type { number }
220     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
221     * @since 9
222     */
223    resolution: number;
224  }
225
226  /**
227   * Defines the information about an input device.
228   *
229   * @interface InputDeviceData
230   * @syscap SystemCapability.MultimodalInput.Input.InputDevice
231   * @since 8
232   */
233  interface InputDeviceData {
234    /**
235     * Id of the input device.
236     *
237     * @type { number }
238     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
239     * @since 8
240     */
241    id: number;
242
243    /**
244     * Name of the input device.
245     *
246     * @type { string }
247     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
248     * @since 8
249     */
250    name: string;
251
252    /**
253     * Source type supported by the input device. For example, if a keyboard is attached with a touchpad,
254     * the device has two input sources: keyboard and touchpad.
255     *
256     * @type { Array<SourceType> }
257     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
258     * @since 8
259     */
260    sources: Array<SourceType>;
261
262    /**
263     * Axis range of the input device.
264     *
265     * @type { Array<AxisRange> }
266     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
267     * @since 8
268     */
269    axisRanges: Array<AxisRange>;
270
271    /**
272     * Bus of the input device.
273     *
274     * @type { number }
275     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
276     * @since 9
277     */
278    bus: number;
279
280    /**
281     * Product of the input device.
282     *
283     * @type { number }
284     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
285     * @since 9
286     */
287    product: number;
288
289    /**
290     * Vendor of the input device.
291     *
292     * @type { number }
293     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
294     * @since 9
295     */
296    vendor: number;
297
298    /**
299     * Version of the input device.
300     *
301     * @type { number }
302     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
303     * @since 9
304     */
305    version: number;
306
307    /**
308     * Physical path of the input device.
309     *
310     * @type { string }
311     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
312     * @since 9
313     */
314    phys: string;
315
316    /**
317     * Unique identifier of the input device.
318     *
319     * @type { string }
320     * @syscap SystemCapability.MultimodalInput.Input.InputDevice
321     * @since 9
322     */
323    uniq: string;
324  }
325
326  class InputDeviceDataImpl implements InputDeviceData {
327    private nativePtr:long = 0;
328    constructor(context:long) {
329      hilog.info(0x0000, 'input_device', "in constructor, context is ", context)
330      this.nativePtr = context;
331    }
332
333  setEmptyAxisRanges(): void {
334    this.axisRanges = new Array<AxisRange>();
335  }
336
337    id: number;
338    name: string;
339    sources: Array<SourceType>;
340    axisRanges: Array<AxisRange>;
341    bus: number;
342    product: number;
343    vendor: number;
344    version: number;
345    phys: string;
346    uniq: string;
347  }
348
349  class AxisRangeImpl implements AxisRange {
350    private nativePtr:long = 0;
351    constructor(context:long) {
352      hilog.info(0x0000, 'input_device', "in constructor, context is ", context)
353      this.nativePtr = context;
354    }
355
356    source: SourceType;
357    axis: AxisType;
358    max: number;
359    min: number;
360    fuzz: number;
361    flat: number;
362    resolution: number;
363  }
364
365  class DeviceListenerImpl implements DeviceListener {
366    type: ChangedType;
367    deviceId: number;
368  }
369
370  function getDeviceList(callback: AsyncCallback<Array<number>>): void {
371    let p1 = taskpool.execute(getDeviceListInner);
372    p1.then((data: NullishType) => {
373        let r =  data as Array<number>;
374        let err : BusinessError<void>
375        callback(err, r);
376    }).catch((error: NullishType) => {
377        let err =  error as BusinessError<void>;
378        let data = new Array<number>;
379        callback(err, data);
380    });
381  }
382
383  function getDeviceList(): Promise<Array<number>> {
384    let p = new Promise<Array<number>>((resolve: (v: Array<number>) => void,
385      reject: (error: Error) => void) => {
386        let p1 = taskpool.execute(getDeviceListInner);
387        p1.then((e :NullishType)=>{
388          let r =  e as Array<number>;
389          resolve(r);
390        }).catch((error: Error) : Array<number> => {
391          reject(error);
392        });
393    });
394    return p;
395  }
396
397  function getDeviceInfo(deviceId: number, callback: AsyncCallback<InputDeviceData>): void {
398    let p1 = taskpool.execute(getDeviceInfoInner, deviceId);
399    p1.then((data: NullishType) => {
400        let r =  data as InputDeviceData;
401        let err : BusinessError<void>
402        callback(err, r);
403    }).catch((error: NullishType) => {
404        let err =  error as BusinessError<void>;
405        let data = new InputDeviceDataImpl(0);
406        callback(err, data);
407    });
408  }
409  function getDeviceInfo(deviceId: number): Promise<InputDeviceData> {
410    let p = new Promise<InputDeviceData>((resolve: (v: InputDeviceData) => void,
411      reject: (error: Error) => void) => {
412        let p1 = taskpool.execute(getDeviceInfoInner, deviceId);
413        p1.then((e :NullishType)=>{
414          let r =  e as InputDeviceData;
415          resolve(r);
416        }).catch((error: Error) : InputDeviceData => {
417          reject(error);
418        });
419    });
420    return p;
421  }
422  function getDeviceInfoSync(deviceId: number): InputDeviceData {
423    return getDeviceInfoInner(deviceId);
424  }
425}
426