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 use crate::{ input_binding, input_binding::CPointerEvent }; 17 18 /// PointerEvent packed the native CPointerEvent. 19 #[repr(C)] 20 pub struct PointerEvent(*const CPointerEvent); 21 22 impl PointerEvent { 23 /// Create a PointerEvent object. new(pointer_event: *const CPointerEvent) -> Self24 pub fn new(pointer_event: *const CPointerEvent) -> Self { 25 Self(pointer_event) 26 } 27 28 /// Extract a raw `CPointerEvent` pointer from this wrapper. 29 /// # Safety as_inner(&self) -> *const CPointerEvent30 pub unsafe fn as_inner(&self) -> *const CPointerEvent { 31 self.0 32 } 33 34 /// Create an `PointerEvent` wrapper object from a raw `CPointerEvent` pointer. from_raw(c_pointer_event: *const CPointerEvent) -> Option<Self>35 pub fn from_raw(c_pointer_event: *const CPointerEvent) -> Option<Self> { 36 if c_pointer_event.is_null() { 37 return None; 38 } 39 Some(Self(c_pointer_event)) 40 } 41 } 42 43 impl PointerEvent { 44 /// Get the pointer id from the PointerEvent. pointer_id(&self) -> i3245 pub fn pointer_id(&self) -> i32 { 46 // SAFETY: 47 // Rust PointerEvent always hold a valid native CPointerEvent. 48 unsafe { 49 input_binding::CGetPointerId(self.as_inner()) 50 } 51 } 52 53 /// Get the pointer action from the PointerEvent. pointer_action(&self) -> i3254 pub fn pointer_action(&self) -> i32 { 55 // SAFETY: 56 // Rust PointerEvent always hold a valid native CPointerEvent. 57 unsafe { 58 input_binding::CGetPointerAction(self.as_inner()) 59 } 60 } 61 62 /// Get the target window id from the PointerEvent. target_window_id(&self) -> i3263 pub fn target_window_id(&self) -> i32 { 64 // SAFETY: 65 // Rust PointerEvent always hold a valid native CPointerEvent. 66 unsafe { 67 input_binding::CGetTargetWindowId(self.as_inner()) 68 } 69 } 70 71 /// Get the source type from the PointerEvent. source_type(&self) -> i3272 pub fn source_type(&self) -> i32 { 73 // SAFETY: 74 // Rust PointerEvent always hold a valid native CPointerEvent. 75 unsafe { 76 input_binding::CGetSourceType(self.as_inner()) 77 } 78 } 79 80 /// Get the target display y from the PointerEvent. taget_display_id(&self) -> i3281 pub fn taget_display_id(&self) -> i32 { 82 // SAFETY: 83 // Rust PointerEvent always hold a valid native CPointerEvent. 84 unsafe { 85 input_binding::CGetTargetDisplayId(self.as_inner()) 86 } 87 } 88 89 /// Get the display x from the PointerEvent. display_x(&self) -> i3290 pub fn display_x(&self) -> i32 { 91 // SAFETY: 92 // Rust PointerEvent always hold a valid native CPointerEvent. 93 unsafe { 94 input_binding::CGetDisplayX(self.as_inner()) 95 } 96 } 97 98 /// Get the display y from the PointerEvent. display_y(&self) -> i3299 pub fn display_y(&self) -> i32 { 100 // SAFETY: 101 // Rust PointerEvent always hold a valid native CPointerEvent. 102 unsafe { 103 input_binding::CGetDisplayY(self.as_inner()) 104 } 105 } 106 107 /// Get the device id from the PointerEvent. device_id(&self) -> i32108 pub fn device_id(&self) -> i32 { 109 // SAFETY: 110 // Rust PointerEvent always hold a valid native CPointerEvent. 111 unsafe { 112 input_binding::CGetDeviceId(self.as_inner()) 113 } 114 } 115 116 /// Get the window id from the PointerEvent. window_pid(&self) -> i32117 pub fn window_pid(&self) -> i32 { 118 // SAFETY: 119 // Rust PointerEvent always hold a valid native CPointerEvent. 120 unsafe { 121 input_binding::CGetWindowPid(self.as_inner()) 122 } 123 } 124 125 /// Add flag to the PointerEvent. add_flag(&self)126 pub fn add_flag(&self) { 127 // SAFETY: 128 // Rust PointerEvent always hold a valid native CPointerEvent. 129 unsafe { 130 input_binding::CPointerEventAddFlag(self.as_inner()) 131 } 132 } 133 }