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