bc4c0bd22c6cf3f0f14f870191f6f2e3839181f9
[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
53 #undef EXTERN
54 #undef DEPRECATED
55 #undef INTERN
56
57 #if defined(__GNUC__) && __GNUC__ >= 4
58 #define EXTERN __attribute__ ((visibility("default")))
59 #else
60 #define EXTERN
61 #endif
62
63 #if defined(__GNUC__) && __GNUC__ >= 4
64 #define INTERN __attribute__ ((visibility("hidden")))
65 #else
66 #define INTERN
67 #endif
68
69 #if defined(__GNUC__) && __GNUC__ >= 4
70 #define DEPRECATED __attribute__ ((deprecated))
71 #else
72 #define DEPRECATED
73 #endif
74
75 /* check condition */
76 #define TDM_RETURN_IF_FAIL(cond) { \
77         if (!(cond))  { \
78                 TDM_ERR("'%s' failed", #cond); \
79                 return; \
80         } \
81 }
82 #define TDM_RETURN_VAL_IF_FAIL(cond, val) { \
83         if (!(cond)) { \
84                 TDM_ERR("'%s' failed", #cond); \
85                 return val; \
86         } \
87 }
88 #define TDM_RETURN_VAL_IF_FAIL_WITH_ERROR(cond, error_v, val) { \
89         if (!(cond)) { \
90                 TDM_ERR("'%s' failed", #cond); \
91                 ret = error_v; \
92                 if (error) *error = ret; \
93                 return val; \
94         } \
95 }
96
97 #define TDM_WARNING_IF_FAIL(cond)  { \
98         if (!(cond)) \
99                 TDM_ERR("'%s' failed", #cond); \
100 }
101 #define TDM_GOTO_IF_FAIL(cond, dst) { \
102         if (!(cond)) { \
103                 TDM_ERR("'%s' failed", #cond); \
104                 goto dst; \
105         } \
106 }
107 #define TDM_EXIT_IF_FAIL(cond) { \
108         if (!(cond)) { \
109                 TDM_ERR("'%s' failed", #cond); \
110                 exit(0); \
111         } \
112 }
113
114 #define TDM_NEVER_GET_HERE() TDM_WRN("** NEVER GET HERE **")
115
116 #define TDM_SNPRINTF(p, len, fmt, ARG...)  \
117         do { \
118                 if (p && len && *len > 0) { \
119                         int s = snprintf(p, *len, fmt, ##ARG); \
120                         p += s; \
121                         *len -= s; \
122                 } \
123         } while (0)
124
125 #define TDM_DBG_RETURN_IF_FAIL(cond) { \
126         if (!(cond))  { \
127                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
128                 return; \
129         } \
130 }
131 #define TDM_DBG_GOTO_IF_FAIL(cond, dst) { \
132         if (!(cond))  { \
133                 TDM_SNPRINTF(reply, len, "[%s %d] '%s' failed\n", __func__, __LINE__, #cond); \
134                 goto dst; \
135         } \
136 }
137
138 #define C(b, m)             (((b) >> (m)) & 0xFF)
139 #define B(c, s)             ((((unsigned int)(c)) & 0xff) << (s))
140 #define FOURCC(a, b, c, d)  (B(d, 24) | B(c, 16) | B(b, 8) | B(a, 0))
141 #define FOURCC_STR(id)      C(id, 0), C(id, 8), C(id, 16), C(id, 24)
142 #define FOURCC_ID(str)      FOURCC(((char*)str)[0], ((char*)str)[1], ((char*)str)[2], ((char*)str)[3])
143 #define IS_RGB(f)           ((f) == TBM_FORMAT_XRGB8888 || (f) == TBM_FORMAT_ARGB8888)
144
145 /* don't using !,$,# */
146 #define TDM_DELIM           "@^&*+-|,:~"
147 #define TDM_ALIGN(a, b)     (((a) + ((b) - 1)) & ~((b) - 1))
148
149 #define TDM_ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
150
151 struct tdm_type_name {
152         int type;
153         const char *name;
154 };
155
156 #define TDM_TYPE_NAME_FN(res) \
157 static inline const char * tdm_##res##_str(int type)    \
158 {                       \
159         unsigned int i;                                 \
160         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) { \
161                 if (tdm_##res##_names[i].type == type)  \
162                         return tdm_##res##_names[i].name;       \
163         }                                               \
164         return "(invalid)";                             \
165 }
166
167 static struct tdm_type_name tdm_dpms_names[] = {
168         { TDM_OUTPUT_DPMS_ON, "on" },
169         { TDM_OUTPUT_DPMS_STANDBY, "standby" },
170         { TDM_OUTPUT_DPMS_SUSPEND, "suspend" },
171         { TDM_OUTPUT_DPMS_OFF, "off" },
172 };
173 TDM_TYPE_NAME_FN(dpms)
174
175 static struct tdm_type_name tdm_status_names[] = {
176         { TDM_OUTPUT_CONN_STATUS_DISCONNECTED, "disconnected" },
177         { TDM_OUTPUT_CONN_STATUS_CONNECTED, "connected" },
178         { TDM_OUTPUT_CONN_STATUS_MODE_SETTED, "mode_setted" },
179 };
180 TDM_TYPE_NAME_FN(status)
181
182 static struct tdm_type_name tdm_conn_names[] = {
183         { TDM_OUTPUT_TYPE_Unknown, "Unknown" },
184         { TDM_OUTPUT_TYPE_VGA, "VGA" },
185         { TDM_OUTPUT_TYPE_DVII, "DVII" },
186         { TDM_OUTPUT_TYPE_DVID, "DVID" },
187         { TDM_OUTPUT_TYPE_DVIA, "DVIA" },
188         { TDM_OUTPUT_TYPE_Composite, "Composite" },
189         { TDM_OUTPUT_TYPE_SVIDEO, "SVIDEO" },
190         { TDM_OUTPUT_TYPE_LVDS, "LVDS" },
191         { TDM_OUTPUT_TYPE_Component, "Component" },
192         { TDM_OUTPUT_TYPE_9PinDIN, "9PinDIN" },
193         { TDM_OUTPUT_TYPE_DisplayPort, "DisplayPort" },
194         { TDM_OUTPUT_TYPE_HDMIA, "HDMIA" },
195         { TDM_OUTPUT_TYPE_HDMIB, "HDMIB" },
196         { TDM_OUTPUT_TYPE_TV, "TV" },
197         { TDM_OUTPUT_TYPE_eDP, "eDP" },
198         { TDM_OUTPUT_TYPE_VIRTUAL, "VIRTUAL" },
199         { TDM_OUTPUT_TYPE_DSI, "DSI" },
200 };
201 TDM_TYPE_NAME_FN(conn)
202
203 static struct tdm_type_name tdm_transform_names[] = {
204         { TDM_TRANSFORM_NORMAL, "none" },
205         { TDM_TRANSFORM_90, "90" },
206         { TDM_TRANSFORM_180, "180" },
207         { TDM_TRANSFORM_270, "270" },
208         { TDM_TRANSFORM_FLIPPED, "flipped" },
209         { TDM_TRANSFORM_FLIPPED_90, "90,flipped" },
210         { TDM_TRANSFORM_FLIPPED_180, "180,flipped" },
211         { TDM_TRANSFORM_FLIPPED_270, "270,flipped" },
212 };
213 TDM_TYPE_NAME_FN(transform)
214
215
216 #define TDM_BIT_NAME_FB(res)                                    \
217 static inline const char * tdm_##res##_str(int type, char **reply, int *len)    \
218 {                       \
219         unsigned int i;                                         \
220         const char *sep = "";                                   \
221         if (type == 0) {        \
222                 TDM_SNPRINTF(*reply, len, "none");      \
223                 return NULL;    \
224         }       \
225         for (i = 0; i < TDM_ARRAY_SIZE(tdm_##res##_names); i++) {               \
226                 if (type & (1 << i)) {                          \
227                         TDM_SNPRINTF(*reply, len, "%s%s", sep, tdm_##res##_names[i]);   \
228                         sep = ",";                              \
229                 }                                               \
230         }                                                       \
231         return NULL;                                            \
232 }
233
234 static const char *tdm_mode_type_names[] = {
235         "builtin",
236         "clock_c",
237         "crtc_c",
238         "preferred",
239         "default",
240         "userdef",
241         "driver",
242 };
243 TDM_BIT_NAME_FB(mode_type)
244
245 static const char *tdm_mode_flag_names[] = {
246         "phsync",
247         "nhsync",
248         "pvsync",
249         "nvsync",
250         "interlace",
251         "dblscan",
252         "csync",
253         "pcsync",
254         "ncsync",
255         "hskew",
256         "bcast",
257         "pixmux",
258         "dblclk",
259         "clkdiv2"
260 };
261 TDM_BIT_NAME_FB(mode_flag)
262
263 static const char *tdm_layer_caps_names[] = {
264         "cursor",
265         "primary",
266         "overlay",
267         "",
268         "graphic",
269         "video",
270         "",
271         "",
272         "scale",
273         "transform",
274         "scanout",
275         "reserved",
276         "no_crop",
277 };
278 TDM_BIT_NAME_FB(layer_caps)
279
280 static const char *tdm_pp_caps_names[] = {
281         "sync",
282         "async",
283         "scale",
284         "transform",
285 };
286 TDM_BIT_NAME_FB(pp_caps)
287
288 static const char *tdm_capture_caps_names[] = {
289         "output",
290         "layer",
291         "scale",
292         "transform",
293 };
294 TDM_BIT_NAME_FB(capture_caps)
295
296 static inline char*
297 strtostr(char *buf, int len, char *str, char *delim)
298 {
299         char *end;
300         end = strpbrk(str, delim);
301         if (end)
302                 len = ((end - str + 1) < len) ? (end - str + 1) : len;
303         else {
304                 int l = strlen(str);
305                 len = ((l + 1) < len) ? (l + 1) : len;
306         }
307         snprintf(buf, len, "%s", str);
308         return str + len - 1;
309 }
310
311 #ifdef __cplusplus
312 }
313 #endif
314
315 #endif /* _TDM_MACRO_H_ */