summaryrefslogtreecommitdiff
path: root/hwc2/common/utils/Utils.cpp (plain)
blob: 7d9ebfeb7b5bda0f88b42255493fc7a7ce9d2c11
1/*
2// Copyright (c) 2016 Amlogic
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16*/
17
18
19#include <hardware/hardware.h>
20
21#include <HwcTrace.h>
22#include <fcntl.h>
23#include <stdlib.h>
24#include <errno.h>
25#include <cutils/properties.h>
26#include <hardware/hwcomposer2.h>
27
28#include <Utils.h>
29
30namespace android {
31namespace amlogic {
32
33Utils::Utils()
34{
35
36}
37
38Utils::~Utils()
39{
40
41}
42
43bool Utils::get_bool_prop(const char* prop) {
44 char val[PROPERTY_VALUE_MAX];
45 memset(val, 0, sizeof(val));
46 if (property_get(prop, val, "false") && strcmp(val, "true") == 0) {
47 //VTRACE("prop: %s is %s",prop, val);
48 return true;
49 }
50
51 return false;
52}
53
54int Utils::get_int_prop(const char* prop) {
55 char val[PROPERTY_VALUE_MAX];
56 memset(val, 0, sizeof(val));
57 if (property_get(prop, val, "0")) {
58 VTRACE("prop: %s is %s",prop, val);
59 return atoi(val);
60 }
61 return -1;
62}
63
64int Utils::getSysfsInt(const char* syspath, int def) {
65 int val = def;
66 char valstr[64];
67 if (getSysfsStr(syspath, valstr, sizeof(valstr)) == 0) {
68 val = atoi(valstr);
69 DTRACE("sysfs(%s) read int (%d)", syspath, val);
70 }
71 return val;
72}
73
74int Utils::getSysfsStr(const char* syspath, char *valstr, int size,
75 bool needOriginalData) {
76
77 int fd, len;
78
79 if ( NULL == valstr ) {
80 ETRACE("buf is NULL");
81 return -1;
82 }
83
84 if ((fd = open(syspath, O_RDONLY)) < 0) {
85 ETRACE("readSysFs, open %s fail.", syspath);
86 return -1;
87 }
88
89 len = read(fd, valstr, size);
90 if (len < 0) {
91 ETRACE("read error: %s, %s\n", syspath, strerror(errno));
92 close(fd);
93 return -1;
94 }
95
96 if (!needOriginalData) {
97 int i , j;
98 for (i = 0, j = 0; i <= len -1; i++) {
99 /*change '\0' to 0x20(spacing), otherwise the string buffer will be cut off
100 * if the last char is '\0' should not replace it
101 */
102 if (0x0 == valstr[i] && i < len - 1) {
103 valstr[i] = 0x20;
104 }
105 //DTRACE("read buffer index:%d is a 0x0, replace to spacing \n", i);
106
107
108 /* delete all the character of '\n' */
109 if (0x0a != valstr[i]) {
110 valstr[j++] = valstr[i];
111 }
112 }
113
114 valstr[j] = 0x0;
115 }
116
117 //DTRACE("read %s, result length:%d, val:%s\n", syspath, len, valstr);
118
119 close(fd);
120 return 0;
121
122}
123
124int Utils::setSysfsStr(const char *path, const char *val) {
125 int bytes;
126 int fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
127 if (fd >= 0) {
128 bytes = write(fd, val, strlen(val));
129 //DTRACE("setSysfsStr %s= %s\n", path,val);
130 close(fd);
131 return 0;
132 } else {
133 return -1;
134 }
135}
136
137bool Utils::checkBoolProp(const char* prop) {
138 char val[PROPERTY_VALUE_MAX];
139
140 memset(val, 0, sizeof(val));
141 if (property_get(prop, val, "false") && strcmp(val, "true") == 0) {
142 DTRACE("prop: %s is %s",prop, val);
143 return true;
144 }
145
146 return false;
147}
148
149int32_t Utils::checkIntProp(const char* prop) {
150 char val[PROPERTY_VALUE_MAX];
151
152 memset(val, 0, sizeof(val));
153 if (property_get(prop, val, "2")) {
154 VTRACE("prop: %s is %s",prop, val);
155 return atoi(val);
156 }
157 return 0;
158}
159
160int32_t Utils::checkAndDupFd(int32_t fd) {
161 if (fd < 0) {
162 ETRACE("not a vliad fd %d", fd);
163 return -1;
164 }
165
166 int32_t dup_fd = ::dup(fd);
167 if (dup_fd < 0) {
168 ETRACE("fd dup failed: %s", strerror(errno));
169 }
170
171 return dup_fd;
172}
173
174#if WITH_LIBPLAYER_MODULE
175bool Utils::checkSysfsStatus(const char* sysfstr, char* lastr, int32_t size) {
176 char val[32];
177 char *p = lastr;
178
179 memset(val, 0, sizeof(val));
180 if (getSysfsStr(sysfstr, val, sizeof(val)) == 0) {
181 DTRACE("val: %s, lastr: %s",val, p);
182 if ((strcmp(val, p) != 0)) {
183 memset(p, 0, size);
184 strcpy(p, val);
185 return true;
186 }
187 }
188
189 return false;
190}
191#endif
192
193const char* Utils::getHotplugUeventEnvelope()
194{
195 return "change@/devices/virtual/switch/hdmi_delay";
196}
197
198const char* Utils::getHdcpUeventEnvelope()
199{
200 return "change@/devices/virtual/switch/hdcp";
201}
202
203const char* Utils::getSwitchState0()
204{
205 return "SWITCH_STATE=0";
206}
207
208const char* Utils::getSwitchState1()
209{
210 return "SWITCH_STATE=1";
211}
212
213
214bool Utils::rectEmpty(hwc_rect_t& rect) {
215 if ((rect.right - rect.left <= 0) ||(rect.bottom - rect.top <= 0))
216 return true;
217 else
218 return false;
219}
220
221bool Utils::rectIntersect(hwc_rect_t& source, hwc_rect_t& dest, hwc_rect_t& result) {
222 result.left = max(source.left, dest.left);
223 result.top = max(source.top, dest.top);
224 result.right = min(source.right, dest.right);
225 result.bottom = min(source.bottom, dest.bottom);
226 return !rectEmpty(result);
227}
228
229Utils::OVERLAP_TYPE Utils::rectOverlap(hwc_rect_t& source, hwc_rect_t& dest) {
230 hwc_rect_t result;
231 if (!rectIntersect(source, dest, result)) {
232 return OVERLAP_EMPTY;
233 } else {
234 if (compareRect(result, source) == true) {
235 return OVERLAP_FULL;
236 } else {
237 return OVERLAP_PART;
238 }
239 }
240}
241
242
243} // namespace amlogic
244} // namespace android
245