summaryrefslogtreecommitdiff
path: root/hwc2/common/utils/Utils.cpp (plain)
blob: 52b630961aea3e09c53ce459509b5dde74fc5857
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
64bool Utils::get_str_prop(const char *key, char *value, const char *def){
65 property_get(key, value, def);
66 return true;
67}
68
69int Utils::getSysfsInt(const char* syspath, int def) {
70 int val = def;
71 char valstr[64];
72 if (getSysfsStr(syspath, valstr, sizeof(valstr)) == 0) {
73 val = atoi(valstr);
74 DTRACE("sysfs(%s) read int (%d)", syspath, val);
75 }
76 return val;
77}
78
79int Utils::getSysfsStr(const char* syspath, char *valstr, int size,
80 bool needOriginalData) {
81
82 int fd, len;
83
84 if ( NULL == valstr ) {
85 ETRACE("buf is NULL");
86 return -1;
87 }
88
89 if ((fd = open(syspath, O_RDONLY)) < 0) {
90 ETRACE("readSysFs, open %s fail.", syspath);
91 return -1;
92 }
93
94 len = read(fd, valstr, size);
95 if (len < 0) {
96 ETRACE("read error: %s, %s\n", syspath, strerror(errno));
97 close(fd);
98 return -1;
99 }
100
101 if (!needOriginalData) {
102 int i , j;
103 for (i = 0, j = 0; i <= len -1; i++) {
104 /*change '\0' to 0x20(spacing), otherwise the string buffer will be cut off
105 * if the last char is '\0' should not replace it
106 */
107 if (0x0 == valstr[i] && i < len - 1) {
108 valstr[i] = 0x20;
109 }
110 //DTRACE("read buffer index:%d is a 0x0, replace to spacing \n", i);
111
112
113 /* delete all the character of '\n' */
114 if (0x0a != valstr[i]) {
115 valstr[j++] = valstr[i];
116 }
117 }
118
119 valstr[j] = 0x0;
120 }
121
122 //DTRACE("read %s, result length:%d, val:%s\n", syspath, len, valstr);
123
124 close(fd);
125 return 0;
126
127}
128
129int Utils::setSysfsStr(const char *path, const char *val) {
130 int bytes;
131 int fd = open(path, O_RDWR);
132 if (fd >= 0) {
133 bytes = write(fd, val, strlen(val));
134 //DTRACE("setSysfsStr %s= %s\n", path,val);
135 close(fd);
136 return 0;
137 } else {
138 ETRACE(" open file error: [%s]", path);
139 return -1;
140 }
141}
142
143bool Utils::checkBoolProp(const char* prop) {
144 char val[PROPERTY_VALUE_MAX];
145
146 memset(val, 0, sizeof(val));
147 if (property_get(prop, val, "false") && strcmp(val, "true") == 0) {
148 DTRACE("prop: %s is %s",prop, val);
149 return true;
150 }
151
152 return false;
153}
154
155int32_t Utils::checkIntProp(const char* prop) {
156 char val[PROPERTY_VALUE_MAX];
157
158 memset(val, 0, sizeof(val));
159 if (property_get(prop, val, "2")) {
160 VTRACE("prop: %s is %s",prop, val);
161 return atoi(val);
162 }
163 return 0;
164}
165
166int32_t Utils::checkAndDupFd(int32_t fd) {
167 if (fd < 0) {
168 ETRACE("not a vliad fd %d", fd);
169 return -1;
170 }
171
172 int32_t dup_fd = ::dup(fd);
173 if (dup_fd < 0) {
174 ETRACE("fd dup failed: %s", strerror(errno));
175 }
176
177 return dup_fd;
178}
179
180bool Utils::checkSysfsStatus(const char* sysfstr, char* lastr, int32_t size) {
181 char val[32];
182 char *p = lastr;
183
184 memset(val, 0, sizeof(val));
185 if (getSysfsStr(sysfstr, val, sizeof(val)) == 0) {
186 DTRACE("val: %s, lastr: %s",val, p);
187 if ((strcmp(val, p) != 0)) {
188 memset(p, 0, size);
189 strcpy(p, val);
190 return true;
191 }
192 }
193
194 return false;
195}
196
197const char* Utils::getHotplugUeventEnvelope()
198{
199 return "change@/devices/virtual/switch/hdmi_delay";
200}
201
202const char* Utils::getHdcpUeventEnvelope()
203{
204 return "change@/devices/virtual/switch/hdcp";
205}
206
207const char* Utils::getSwitchState0()
208{
209 return "SWITCH_STATE=0";
210}
211
212const char* Utils::getSwitchState1()
213{
214 return "SWITCH_STATE=1";
215}
216
217
218bool Utils::rectEmpty(hwc_rect_t& rect) {
219 if ((rect.right - rect.left <= 0) ||(rect.bottom - rect.top <= 0))
220 return true;
221 else
222 return false;
223}
224
225bool Utils::rectIntersect(hwc_rect_t& source, hwc_rect_t& dest, hwc_rect_t& result) {
226 result.left = max(source.left, dest.left);
227 result.top = max(source.top, dest.top);
228 result.right = min(source.right, dest.right);
229 result.bottom = min(source.bottom, dest.bottom);
230 return !rectEmpty(result);
231}
232
233Utils::OVERLAP_TYPE Utils::rectOverlap(hwc_rect_t& source, hwc_rect_t& dest) {
234 hwc_rect_t result;
235 if (!rectIntersect(source, dest, result)) {
236 return OVERLAP_EMPTY;
237 } else {
238 if (compareRect(result, source) == true) {
239 return OVERLAP_FULL;
240 } else {
241 return OVERLAP_PART;
242 }
243 }
244}
245
246
247} // namespace amlogic
248} // namespace android
249