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::CKeyEvent };
17 
18 /// KeyEvent packed the native CKeyEvent.
19 #[repr(C)]
20 pub struct KeyEvent(*const CKeyEvent);
21 
22 impl KeyEvent {
23     /// Create a KeyEvent object.
new(key_event: *const CKeyEvent) -> Self24     pub fn new(key_event: *const CKeyEvent) -> Self {
25         Self(key_event)
26     }
27 
28     /// Extract a raw `CKeyEvent` pointer from this wrapper.
29     /// # Safety
as_inner(&self) -> *const CKeyEvent30     pub unsafe fn as_inner(&self) -> *const CKeyEvent {
31         self.0
32     }
33 
34     /// Create an `KeyEvent` wrapper object from a raw `CKeyEvent` pointer.
from_raw(c_key_event: *const CKeyEvent) -> Option<Self>35     pub fn from_raw(c_key_event: *const CKeyEvent) -> Option<Self> {
36         if c_key_event.is_null() {
37             return None;
38         }
39         Some(Self(c_key_event))
40     }
41 }
42 
43 impl KeyEvent {
44     /// Add flag to KeyEvent.
add_flag(&self)45     pub fn add_flag(&self) {
46         // SAFETY:
47         // Rust KeyEvent always hold a valid native CKeyEvent.
48         unsafe {
49             input_binding::CKeyEventAddFlag(self.as_inner())
50         }
51     }
52 
53     /// Get the key code from the KeyEvent.
key_code(&self) -> i3254     pub fn key_code(&self) -> i32 {
55         // SAFETY:
56         // Rust KeyEvent always hold a valid native CKeyEvent.
57         unsafe {
58             input_binding::CGetKeyCode(self.as_inner())
59         }
60     }
61 }