a55251efac66de5832c3f458e29cd5ce00da8644
[platform/core/uifw/libtdm.git] / src / tdm_macro.h
1 /**************************************************************************
2  *
3  * libtdm
4  *
5  * Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6  *
7  * Contact: Eunchul Kim <chulspro.kim@samsung.com>,
8  *          JinYoung Jeon <jy0.jeon@samsung.com>,
9  *          Taeheon Kim <th908.kim@samsung.com>,
10  *          YoungJun Cho <yj44.cho@samsung.com>,
11  *          SooChan Lim <sc1.lim@samsung.com>,
12  *          Boram Park <sc1.lim@samsung.com>
13  *
14  * Permission is hereby granted, free of charge, to any person obtaining a
15  * copy of this software and associated documentation files (the
16  * "Software"), to deal in the Software without restriction, including
17  * without limitation the rights to use, copy, modify, merge, publish,
18  * distribute, sub license, and/or sell copies of the Software, and to
19  * permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
27  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
29  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
30  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
31  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
32  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34 **************************************************************************/
35
36 #ifndef _TDM_MACRO_H_
37 #define _TDM_MACRO_H_
38
39 #include <stdio.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <sys/types.h>
43
44 #include <tdm_common.h>
45 #include <tbm_surface.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #define TDM_SERVER_REPLY_MSG_LEN        8192
52 #define TDM_DEBUG_REPLY_MSG_LEN         2048
53
54 #undef EXTERN
55 #undef DEPRECATED
56 #undef INTERN
57
58 #if defined(__GNUC__) && __GNUC__ >= 4
59 #define EXTERN __attribute__ ((visibility("default")))
60 #else
61 #define EXTERN
62 #endif
63
64 #if defined(__GNUC__) && __GNUC__ >= 4
65 #define INTERN __attribute__ ((visibility("hidden")))
66 #else
67 #define INTERN
68 #endif
69
70 #if defined(__GNUC__) && __GNUC__ >= 4
71 #define DEPRECATED __attribute__ ((deprecated))
72 #else
73 #define DEPRECATED
74 #endif
75
76 /* check condition */
77 #define TDM_RETURN_IF_FAIL(cond) { \
78         if (!(cond))  { \
79                 TDM_ERR("'%s' failed", #cond); \
80                 return; \
81         } \
82 }
83 #define TDM_RETURN_VAL_IF_FAIL(cond, val) { \
84         if (!(cond)) { \
85                 TDM_ERR("'%s' failed", #cond); \
86                 return val; \
87         } \
88 }
89 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) { \
90         if (!(cond)) { \
91                 TDM_ERR("'%s' failed", #cond); \
92                 ret = error_v; \
93                 if (error) *error = ret; \
94                 return val; \
95         } \
96 }
97
98 #define TDM_WARNING_IF_FAIL(cond)  { \
99         if (!(cond)) \
100                 TDM_WRN("'%s' failed", #cond); \
101 }
102 #define TDM_GOTO_IF_FAIL(cond, dst) { \
103         if (!(cond)) { \
104                 TDM_ERR("'%s' failed", #cond); \
105                 goto dst; \
106         } \
107 }
108 #define TDM_EXIT_IF_FAIL(cond) { \
109         if (!(cond)) { \
110                 TDM_ERR("'%s' failed", #cond); \
111                 exit(0); \
112         } \
113 }
114
115 #define TDM_NEVER_GET_HERE() TDM_WRN("** NEVER GET HERE **")
116
117 #define TDM_SNPRINTF(p, len, fmt, ARG...)  \
118         do { \
119                 if (p && len && *len > 0) { \
120                         int s = snprintf(p, *len, fmt, ##ARG); \
121                         p += s; \
122                         *len -= s; \
123                 } \
124         } while (0)
125
126 #define TDM_DBG_RETURN_IF_FAIL(cond) { \
127         if (!(cond))  { \
128                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
129                 return; \
130         } \
131 }
132 #define TDM_DBG_GOTO_IF_FAIL(cond, dst) { \
133         if (!(cond))  { \
134                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
135                 goto dst; \
136         } \
137 }
138
139 #define TDM_NOT_DEFINED_VALUE       (-1)
140 #define TDM_FRONT_VALUE(n)          (((n) > 0) ? (n) : TDM_NOT_DEFINED_VALUE)
141
142 #define TDM_MAX(x, y) (((x) > (y)) ? (x) : (y))
143 #define TDM_MIN(x, y) (((x) < (y)) ? (x) : (y))
144
145 #define TDM_TIME(sec, usec)   ((double)(sec) + ((double)(usec)) / 1000000.0)
146 #define TDM_TIME_SEC(time)    ((unsigned int)(time))
147 #define TDM_TIME_USEC(time)   (unsigned int)(((time) - (unsigned int)(time)) * 1000000.0)
148
149 #define C(b, m)             (((b) >> (m)) & 0xFF)
150 #define B(c, s)             ((((unsigned int)(c)) & 0xff) << (s))
151 #define FOURCC(a, b, c, d)  (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
152 #define FOURCC_STR(id)      C(id, 0), C(id, 8), C(id, 16), C(id, 24)
153 #define FOURCC_ID(str)      FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
154 #define IS_RGB(f)           ((f) == TBM_FORMAT_XRGB8888 || (f) == TBM_FORMAT_ARGB8888)
155
156 /* don't using !,$,# */
157 #define TDM_DELIM           "@^&*+-|,:~"
158 #define TDM_ALIGN(a, b)     (((a) + ((b) - 1)) & ~((b) - 1))
159 #define TDM_SWAP(a, b)      ({ int t; t = a; a = b; b = t; })
160 #define TDM_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
161
162 /* can't export VSYNC macro because we can't define the exact meaning of vsync off
163  * at this time. Does committing in standy mode work? Doesn't committing in suspend mode work?
164  */
165 #define TDM_OUTPUT_DPMS_DEFAULT_MASK         0xF
166 #define TDM_OUTPUT_DPMS_VSYNC_OFF_MASK       0x2
167 #define TDM_OUTPUT_DPMS_VSYNC_IS_OFF(dpms)   ((dpms) & TDM_OUTPUT_DPMS_VSYNC_OFF_MASK)
168
169 struct tdm_type_name {
170         int type;
171         const char *name;
172 };
173
174 #define TDM_TYPE_NAME_FN(res) \
175 static inline const char * tdm_##res##_str(int type)    \
176 {                       \
177         unsigned int i;                                 \
178         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) { \
179                 if (tdm_##res##_names[i].type == type)  \
180                         return tdm_##res##_names[i].name;       \
181         }                                               \
182         return "(invalid)";                             \
183 }
184
185 static struct tdm_type_name tdm_dpms_names[] = {
186         { TDM_OUTPUT_DPMS_ON, "on" },
187         { TDM_OUTPUT_DPMS_STANDBY, "standby" },
188         { TDM_OUTPUT_DPMS_SUSPEND, "suspend" },
189         { TDM_OUTPUT_DPMS_OFF, "off" },
190         { TDM_OUTPUT_DPMS_AOD, "aod" },
191 };
192 TDM_TYPE_NAME_FN(dpms)
193
194 static struct tdm_type_name tdm_status_names[] = {
195         { TDM_OUTPUT_CONN_STATUS_DISCONNECTED, "disconnected" },
196         { TDM_OUTPUT_CONN_STATUS_CONNECTED, "connected" },
197         { TDM_OUTPUT_CONN_STATUS_MODE_SETTED, "mode_setted" },
198 };
199 TDM_TYPE_NAME_FN(status)
200
201 static struct tdm_type_name tdm_conn_names[] = {
202         { TDM_OUTPUT_TYPE_Unknown, "Unknown" },
203         { TDM_OUTPUT_TYPE_VGA, "VGA" },
204         { TDM_OUTPUT_TYPE_DVII, "DVII" },
205         { TDM_OUTPUT_TYPE_DVID, "DVID" },
206         { TDM_OUTPUT_TYPE_DVIA, "DVIA" },
207         { TDM_OUTPUT_TYPE_Composite, "Composite" },
208         { TDM_OUTPUT_TYPE_SVIDEO, "SVIDEO" },
209         { TDM_OUTPUT_TYPE_LVDS, "LVDS" },
210         { TDM_OUTPUT_TYPE_Component, "Component" },
211         { TDM_OUTPUT_TYPE_9PinDIN, "9PinDIN" },
212         { TDM_OUTPUT_TYPE_DisplayPort, "DisplayPort" },
213         { TDM_OUTPUT_TYPE_HDMIA, "HDMIA" },
214         { TDM_OUTPUT_TYPE_HDMIB, "HDMIB" },
215         { TDM_OUTPUT_TYPE_TV, "TV" },
216         { TDM_OUTPUT_TYPE_eDP, "eDP" },
217         { TDM_OUTPUT_TYPE_VIRTUAL, "VIRTUAL" },
218         { TDM_OUTPUT_TYPE_DSI, "DSI" },
219 };
220 TDM_TYPE_NAME_FN(conn)
221
222 static struct tdm_type_name tdm_transform_names[] = {
223         { TDM_TRANSFORM_NORMAL, "none" },
224         { TDM_TRANSFORM_90, "90" },
225         { TDM_TRANSFORM_180, "180" },
226         { TDM_TRANSFORM_270, "270" },
227         { TDM_TRANSFORM_FLIPPED, "flipped" },
228         { TDM_TRANSFORM_FLIPPED_90, "90,flipped" },
229         { TDM_TRANSFORM_FLIPPED_180, "180,flipped" },
230         { TDM_TRANSFORM_FLIPPED_270, "270,flipped" },
231 };
232 TDM_TYPE_NAME_FN(transform)
233
234 static struct tdm_type_name tdm_value_type_names[] = {
235         { TDM_VALUE_TYPE_UNKNOWN, "unknown" },
236         { TDM_VALUE_TYPE_PTR, "ptr" },
237         { TDM_VALUE_TYPE_INT32, "int32" },
238         { TDM_VALUE_TYPE_UINT32, "uint32" },
239         { TDM_VALUE_TYPE_INT64, "int64" },
240         { TDM_VALUE_TYPE_UINT64, "uint64" },
241 };
242 TDM_TYPE_NAME_FN(value_type)
243
244 #define TDM_BIT_NAME_FB(res)                                    \
245 static inline const char * tdm_##res##_str(int type, char **reply, int *len)    \
246 {                       \
247         unsigned int i;                                         \
248         const char *sep = "";                                   \
249         if (type == 0) {        \
250                 TDM_SNPRINTF(*reply, len, "none");      \
251                 return NULL;    \
252         }       \
253         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) {               \
254                 if (type & (1 << i)) {                          \
255                         TDM_SNPRINTF(*reply, len, "%s%s", sep, tdm_##res##_names[i]);   \
256                         sep = ",";                              \
257                 }                                               \
258         }                                                       \
259         return NULL;                                            \
260 }
261
262 static const char *tdm_mode_type_names[] = {
263         "builtin",
264         "clock_c",
265         "crtc_c",
266         "preferred",
267         "default",
268         "userdef",
269         "driver",
270 };
271 TDM_BIT_NAME_FB(mode_type)
272
273 static const char *tdm_mode_flag_names[] = {
274         "phsync",
275         "nhsync",
276         "pvsync",
277         "nvsync",
278         "interlace",
279         "dblscan",
280         "csync",
281         "pcsync",
282         "ncsync",
283         "hskew",
284         "bcast",
285         "pixmux",
286         "dblclk",
287         "clkdiv2"
288 };
289 TDM_BIT_NAME_FB(mode_flag)
290
291 static const char *tdm_layer_caps_names[] = {
292         "cursor",
293         "primary",
294         "overlay",
295         "",
296         "graphic",
297         "video",
298         "",
299         "",
300         "scale",
301         "transform",
302         "scanout",
303         "reserved",
304         "no_crop",
305 };
306 TDM_BIT_NAME_FB(layer_caps)
307
308 static const char *tdm_pp_caps_names[] = {
309         "sync",
310         "async",
311         "scale",
312         "transform",
313 };
314 TDM_BIT_NAME_FB(pp_caps)
315
316 static const char *tdm_capture_caps_names[] = {
317         "output",
318         "layer",
319         "scale",
320         "transform",
321 };
322 TDM_BIT_NAME_FB(capture_caps)
323
324 static inline char*
325 strtostr(char *buf, int len, char *str, char *delim)
326 {
327         char *end;
328         end = strpbrk(str, delim);
329         if (end)
330                 len = ((end - str + 1) < len) ? (end - str + 1) : len;
331         else {
332                 int l = strlen(str);
333                 len = ((l + 1) < len) ? (l + 1) : len;
334         }
335         snprintf(buf, len, "%s", str);
336         return str + len - 1;
337 }
338
339 #ifdef __cplusplus
340 }
341 #endif
342
343 #endif /* _TDM_MACRO_H_ */