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 //! ExtraData data definitions of DRAG module.
17 
18 use crate::{ input_binding, input_binding::CExtraData };
19 use fusion_data_rust::DragData;
20 use fusion_utils_rust::{ FusionResult, FusionErrorCode };
21 
22 impl CExtraData {
23     /// Create a CExtraData object.
new(appended: bool) -> Self24     pub fn new(appended: bool) -> Self {
25         CExtraData {
26             appended,
27             buffer: std::ptr::null(),
28             buffer_size: 0usize,
29             source_type: -1i32,
30             pointer_id: -1i32,
31         }
32     }
33 
34     /// Set the appended property for CExtraData.
set_appended(&mut self, appended: bool) -> &mut Self35     pub fn set_appended(&mut self, appended: bool) -> &mut Self {
36         self.appended = appended;
37         self
38     }
39 
40     /// Set the buffer property for CExtraData.
set_buffer(&mut self, vec: &Vec<u8>) -> &mut Self41     pub fn set_buffer(&mut self, vec: &Vec<u8>) -> &mut Self {
42         let vec_ptr = vec.as_ptr();
43         self.buffer = vec_ptr;
44         self.buffer_size = vec.len();
45         self
46     }
47 
48     /// Set the source type property for CExtraData.
set_source_type(&mut self, source_type: i32) -> &mut Self49     pub fn set_source_type(&mut self, source_type: i32) -> &mut Self {
50         self.source_type = source_type;
51         self
52     }
53 
54     /// Set the pointer id property for CExtraData.
set_pointer_id(&mut self, pointer_id: i32) -> &mut Self55     pub fn set_pointer_id(&mut self, pointer_id: i32) -> &mut Self {
56         self.pointer_id = pointer_id;
57         self
58     }
59 }
60 
61 /// Struct extra data.
62 pub struct ExtraData {
63     inner: CExtraData,
64 }
65 
66 impl ExtraData {
67     /// Create a ExtraData object.
new(appended: bool) -> Self68     pub fn new(appended: bool) -> Self {
69         Self {
70             inner: CExtraData::new(appended)
71         }
72     }
73 
74     /// The extra data information is sent to the external subsystem.
appended_extra_data(&mut self, allow_appended: bool, drag_data: DragData) -> FusionResult<()>75     pub fn appended_extra_data(&mut self, allow_appended: bool, drag_data: DragData) -> FusionResult<()> {
76         let buffer: &Vec<u8>= &drag_data.buffer;
77         if buffer.is_empty() {
78             return Err(FusionErrorCode::Fail);
79         }
80         self.inner.set_appended(allow_appended)
81                   .set_buffer(buffer)
82                   .set_source_type(drag_data.source_type)
83                   .set_pointer_id(drag_data.pointer_id);
84 
85         // SAFETY:  No `None` here, cause `cextra_data` is valid.
86         unsafe {
87             input_binding::CAppendExtraData(&self.inner);
88         }
89         Ok(())
90     }
91 }