• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
16 #![allow(dead_code)]
17 
18 use std::ffi::{ c_char, CString };
19 
20 use hilog_rust::{error, hilog, HiLogLabel, LogType};
21 
22 use fusion_utils_rust::{ FusionResult, FusionErrorCode };
23 
24 use crate::{ input_binding, input_binding::OnPointerEventCallback };
25 
26 const LOG_LABEL: HiLogLabel = HiLogLabel {
27     log_type: LogType::LogCore,
28     domain: 0xD002220,
29     tag: "RustInputManager"
30 };
31 const INPUT_BINDING_OK: i32 = 0;
32 
33 /// Call the input manager interface.
34 pub struct InputManager;
35 
36 impl InputManager {
37     /// Call the AddMonitor interface of the input subsystem to add monitor.
add_monitor(callback: OnPointerEventCallback) -> FusionResult<()>38     pub fn add_monitor(callback: OnPointerEventCallback) -> FusionResult<()> {
39         // SAFETY: No `None` here, cause `callback` is valid.
40         unsafe {
41             if input_binding::CAddMonitor(callback) != INPUT_BINDING_OK {
42                 error!(LOG_LABEL, "Failed to add monitor");
43                 return Err(FusionErrorCode::Fail);
44             }
45             Ok(())
46         }
47     }
48 
49     /// Call the SetPointerVisible interface of the input subsystem to set pointer visible.
set_pointer_visible(visible: bool) -> FusionResult<()>50     pub fn set_pointer_visible(visible: bool) -> FusionResult<()> {
51         // SAFETY: Set pointer event visible.
52         unsafe {
53             if input_binding::CSetPointerVisible(visible) != INPUT_BINDING_OK {
54                 error!(LOG_LABEL, "Failed to set pointer visible");
55                 return Err(FusionErrorCode::Fail);
56             }
57             Ok(())
58         }
59     }
60 
61     /// Call the EnableInputDevice interface of the input subsystem to enable input device.
enable_input_device(enable: bool) -> FusionResult<()>62     pub fn enable_input_device(enable: bool) -> FusionResult<()> {
63         // SAFETY: Enable input device.
64         unsafe {
65             if input_binding::CEnableInputDevice(enable) != INPUT_BINDING_OK {
66                 error!(LOG_LABEL, "Failed to enable input device");
67                 return Err(FusionErrorCode::Fail);
68             }
69             Ok(())
70         }
71     }
72 
73     /// Call the RemoveInputEventFilter interface of the input subsystem to remove filter.
remove_input_event_filter(filter_id: i32) -> FusionResult<()>74     pub fn remove_input_event_filter(filter_id: i32) -> FusionResult<()> {
75         // SAFETY: Remove input event filter.
76         unsafe {
77             if input_binding::CRemoveInputEventFilter(filter_id) != INPUT_BINDING_OK {
78                 error!(LOG_LABEL, "Failed to remove input event filter");
79                 return Err(FusionErrorCode::Fail);
80             }
81             Ok(())
82         }
83     }
84 
85     /// Call the RemoveMonitor interface of the input subsystem to remove monitor.
remove_monitor(monitor_id: i32)86     pub fn remove_monitor(monitor_id: i32) {
87         // SAFETY: Remove monitor.
88         unsafe {
89             input_binding::CRemoveMonitor(monitor_id);
90         }
91     }
92 
93     /// Call the RemoveInterceptor interface of the input subsystem to remove Interceptor.
remove_interceptor(interceptor_id: i32)94     pub fn remove_interceptor(interceptor_id: i32) {
95         // SAFETY: Remove interceptor.
96         unsafe {
97             input_binding::CRemoveInterceptor(interceptor_id);
98         }
99     }
100 
101     /// Call the SetPointerLocation interface of the input subsystem to set pointer location.
set_pointer_location(physical_x: i32, physical_y: i32)102     pub fn set_pointer_location(physical_x: i32, physical_y: i32) {
103         // SAFETY: Remove monitor.
104         unsafe {
105             input_binding::CSetPointerLocation(physical_x, physical_y);
106         }
107     }
108 }