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