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 //! IPC data definitions of DRAG module.
17 
18 use std::ffi::{ c_char, CString };
19 use std::fmt::{ Display, Formatter, Error };
20 
21 use hilog_rust::{ info, hilog, HiLogLabel, LogType };
22 use ipc_rust::{ BorrowedMsgParcel, Serialize, Deserialize, IpcResult };
23 
24 use fusion_utils_rust::call_debug_enter;
25 
26 const LOG_LABEL: HiLogLabel = HiLogLabel {
27     log_type: LogType::LogCore,
28     domain: 0xD002220,
29     tag: "FusionDragData"
30 };
31 
32 /// C representation of [`ShadowInfo`].
33 #[repr(C)]
34 pub struct CShadowInfo {
35     x: i32,
36     y: i32,
37 }
38 
39 /// C representation of [`DragData`].
40 #[repr(C)]
41 pub struct CDragData {
42     shadow_info: CShadowInfo,
43     buffer: *const u8,
44     buffer_size: usize,
45     source_type: i32,
46     drag_num: i32,
47     pointer_id: i32,
48     display_x: i32,
49     display_y: i32,
50     display_id: i32,
51     has_canceled_animation: bool,
52 }
53 
54 /// Data of shadow.
55 pub struct ShadowInfo {
56     x: i32,
57     y: i32,
58 }
59 
60 impl ShadowInfo {
61     /// Converts `CShadowInfo` type to `ShadowInfo` type
from_c(value: &mut CShadowInfo) -> Self62     pub fn from_c(value: &mut CShadowInfo) -> Self
63     {
64         call_debug_enter!("ShadowInfo::from_c");
65         Self {
66             x: value.x,
67             y: value.y,
68         }
69     }
70 }
71 
72 impl Serialize for ShadowInfo {
serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()>73     fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()>
74     {
75         call_debug_enter!("ShadowInfo::serialize");
76         self.x.serialize(parcel)?;
77         self.y.serialize(parcel)?;
78         Ok(())
79     }
80 }
81 
82 impl Deserialize for ShadowInfo {
deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self>83     fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self>
84     {
85         call_debug_enter!("ShadowInfo::deserialize");
86         let shadow_info = Self {
87             x: i32::deserialize(parcel)?,
88             y: i32::deserialize(parcel)?,
89         };
90         Ok(shadow_info)
91     }
92 }
93 
94 /// Bookkeeping of drag operation.
95 pub struct DragData {
96     /// Meta data of shadow.
97     pub shadow_info: ShadowInfo,
98     /// Buffer for meta data of drag.
99     pub buffer: Vec<u8>,
100     /// The device type of input event that trigger this drag.
101     pub source_type: i32,
102     /// Represents `drag_num` information in the `DragData` struct
103     pub drag_num: i32,
104     /// Represents `pointer_id` information in the `DragData` struct
105     pub pointer_id: i32,
106     /// Represents `display_x` information in the `DragData` struct
107     pub display_x: i32,
108     /// Represents `display_y` information in the `DragData` struct
109     pub display_y: i32,
110     /// Represents `display_id` information in the `DragData` struct
111     pub display_id: i32,
112     /// Represents `has_canceled_animation` information in the `DragData` struct
113     pub has_canceled_animation: bool,
114 }
115 
116 impl DragData {
117     /// Converts `CDragData` type to `DragData` type
from_c(value: &mut CDragData) -> Self118     pub fn from_c(value: &mut CDragData) -> Self
119     {
120         call_debug_enter!("DragData::from_c");
121         let mut buf: Vec<u8> = Vec::new();
122         let ts = unsafe {
123             std::slice::from_raw_parts(value.buffer, value.buffer_size)
124         };
125         info!(LOG_LABEL, "Fill buffer");
126         for item in ts.iter() {
127             buf.push(*item);
128         }
129         info!(LOG_LABEL, "new DragData instance");
130         Self {
131             shadow_info: ShadowInfo::from_c(&mut value.shadow_info),
132             buffer: buf,
133             source_type: value.source_type,
134             drag_num: value.drag_num,
135             pointer_id: value.pointer_id,
136             display_x: value.display_x,
137             display_y: value.display_y,
138             display_id: value.display_id,
139             has_canceled_animation: value.has_canceled_animation,
140         }
141     }
142 }
143 
144 impl Serialize for DragData {
serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()>145     fn serialize(&self, parcel: &mut BorrowedMsgParcel<'_>) -> IpcResult<()>
146     {
147         info!(LOG_LABEL, "In DragData::serialize() enter");
148         self.shadow_info.serialize(parcel)?;
149         self.buffer.serialize(parcel)?;
150         self.source_type.serialize(parcel)?;
151         self.drag_num.serialize(parcel)?;
152         self.pointer_id.serialize(parcel)?;
153         self.display_x.serialize(parcel)?;
154         self.display_y.serialize(parcel)?;
155         self.display_id.serialize(parcel)?;
156         self.has_canceled_animation.serialize(parcel)?;
157         Ok(())
158     }
159 }
160 
161 impl Deserialize for DragData {
deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self>162     fn deserialize(parcel: &BorrowedMsgParcel<'_>) -> IpcResult<Self>
163     {
164         info!(LOG_LABEL, "In DragData::deserialize() enter");
165         let drag_data = Self {
166             shadow_info: ShadowInfo::deserialize(parcel)?,
167             buffer: Vec::<u8>::deserialize(parcel)?,
168             source_type: i32::deserialize(parcel)?,
169             drag_num: i32::deserialize(parcel)?,
170             pointer_id: i32::deserialize(parcel)?,
171             display_x: i32::deserialize(parcel)?,
172             display_y: i32::deserialize(parcel)?,
173             display_id: i32::deserialize(parcel)?,
174             has_canceled_animation: bool::deserialize(parcel)?,
175         };
176         Ok(drag_data)
177     }
178 }
179 
180 impl Display for DragData {
fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>181     fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
182     {
183         writeln!(f, "\nDragData {{")?;
184         writeln!(f, "  shadow_info: {{")?;
185         writeln!(f, "    x: {},", self.shadow_info.x)?;
186         writeln!(f, "    y: {},", self.shadow_info.y)?;
187         writeln!(f, "  }},")?;
188         writeln!(f, "  buffer: [*],")?;
189         writeln!(f, "  source_type: {},", self.source_type)?;
190         writeln!(f, "  drag_num: {},", self.drag_num)?;
191         writeln!(f, "  pointer_id: {},", self.pointer_id)?;
192         writeln!(f, "  display_x: {},", self.display_x)?;
193         writeln!(f, "  display_y: {},", self.display_y)?;
194         writeln!(f, "  display_id: {},", self.display_id)?;
195         writeln!(f, "  has_canceled_animation: {},", self.has_canceled_animation)?;
196         writeln!(f, "}}")?;
197         Ok(())
198     }
199 }
200