Adding back in requirements to the spec needed for IVI hardware. Build against the...
[profile/ivi/system-info.git] / src / system_info_screen.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22
23 #include <vconf.h>
24 #include <dlog.h>
25
26 #include <X11/X.h>
27 #include <X11/Xlib.h>
28 #include <X11/extensions/Xrandr.h>
29
30 #include <system_info.h>
31 #include <system_info_private.h>
32
33 #ifdef LOG_TAG
34 #undef LOG_TAG
35 #endif
36
37 #define LOG_TAG "CAPI_SYSTEM_INFO"
38
39 typedef struct _progInfo ProgInfo;
40
41 /* globals */
42 ProgInfo g_pinfo;
43
44 struct _progInfo {
45         Display *dpy;
46         Window root;
47         int screen;
48         int event_base, error_base;
49         int major, minor;
50         XRRScreenResources *res;
51 };
52
53 static int RCA_SUPPORTED;
54 static int HDMI_SUPPORTED;
55 static int SCREEN_DPI;
56 static int BITS_PER_PIXEL;
57 static int SCREEN_WIDTH;
58 static int SCREEN_HEIGHT;
59 static int PHYSICAL_SCREEN_WIDTH;
60 static int PHYSICAL_SCREEN_HEIGHT;
61 int system_info_screen_initialized;
62
63 int system_info_screen_init()
64 {
65         int i, n;
66         XPixmapFormatValues *pmf = NULL;
67
68         memset(&g_pinfo, 0x0, sizeof(ProgInfo));
69
70         g_pinfo.dpy = XOpenDisplay(NULL);
71         if (NULL == g_pinfo.dpy) {
72                 LOGE("XOpenDisplay Failed");
73                 return -1;
74         }
75
76         if (0 > g_pinfo.screen)
77                 g_pinfo.screen = DefaultScreen(g_pinfo.dpy);
78         g_pinfo.root = RootWindow(g_pinfo.dpy, g_pinfo.screen);
79
80         if (!XRRQueryExtension(g_pinfo.dpy, &g_pinfo.event_base, &g_pinfo.error_base) ||
81                 !XRRQueryVersion(g_pinfo.dpy, &g_pinfo.major, &g_pinfo.minor)) {
82                 LOGE("XRRQuery Failed");
83                 XCloseDisplay(g_pinfo.dpy);
84                 return -1;
85         }
86
87         g_pinfo.res = XRRGetScreenResources(g_pinfo.dpy, g_pinfo.root);
88
89         if (!g_pinfo.res) {
90                 LOGE("XRRGetScreenResources Failed");
91                 XCloseDisplay(g_pinfo.dpy);
92                 return -1;
93         }
94
95         pmf = XListPixmapFormats(g_pinfo.dpy, &n);
96
97         if (!pmf) {
98                 LOGE("XListPixmapFormats Failed");
99                 XCloseDisplay(g_pinfo.dpy);
100                 return -1;
101         }
102
103         for (i = 0; i < n; i++) {
104                 if (BITS_PER_PIXEL < pmf[i].bits_per_pixel)
105                         BITS_PER_PIXEL = pmf[i].bits_per_pixel;
106         }
107         XFree(pmf);
108         pmf = NULL;
109
110         for (i = 0; i < g_pinfo.res->noutput; i++) {
111                 XRROutputInfo *output_info = XRRGetOutputInfo(g_pinfo.dpy, g_pinfo.res, g_pinfo.res->outputs[i]);
112                 if (!output_info) {
113                         LOGE("XRRGetOutputInfo Failed");
114                         XCloseDisplay(g_pinfo.dpy);
115                         return -1;
116                 }
117
118                 /* find target lcd */
119                 if (!strcmp(output_info->name, "LVDS1")) {
120                         /* XRRCrtcInfo information */
121                         XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(g_pinfo.dpy, g_pinfo.res, output_info->crtc);
122                         if (!crtc_info)
123                                 break;
124
125                         SCREEN_WIDTH = crtc_info->width;
126                         SCREEN_HEIGHT = crtc_info->height;
127                         PHYSICAL_SCREEN_WIDTH = output_info->mm_width;
128                         PHYSICAL_SCREEN_HEIGHT = output_info->mm_height;
129
130                         XRRFreeCrtcInfo(crtc_info);
131                 } else if (!strcmp(output_info->name, "HDMI1"))
132                         HDMI_SUPPORTED = true;
133                 XRRFreeOutputInfo(output_info);
134         }
135
136         if (BITS_PER_PIXEL == 0) {
137                 LOGE("BIT PER PIXEL is Zero");
138                 XCloseDisplay(g_pinfo.dpy);
139                 return -1;
140         }
141
142         if (!SCREEN_WIDTH)
143                 SCREEN_WIDTH = DisplayWidth(g_pinfo.dpy, DefaultScreen(g_pinfo.dpy));
144
145         if (!SCREEN_WIDTH) {
146                 LOGE("SCREEN WIDTH is Zero");
147                 XCloseDisplay(g_pinfo.dpy);
148                 return -1;
149         }
150
151         if (!SCREEN_HEIGHT)
152                 SCREEN_HEIGHT = DisplayHeight(g_pinfo.dpy, DefaultScreen(g_pinfo.dpy));
153
154         if (!SCREEN_HEIGHT) {
155                 LOGE("SCREEN HEIGHT is Zero");
156                 XCloseDisplay(g_pinfo.dpy);
157                 return -1;
158         }
159
160         if (system_info_get_system_info_model_type() == SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
161                 FILE *cmdline;
162                 char *dpi;
163                 char str[MAXBUFSIZE];
164
165                 cmdline = fopen(CMDLINE_PATH, "r");
166                 if (NULL == cmdline) {
167                         LOGE("cannot file open %s file!!!", CPU_INFO_FILE_PATH);
168                         XCloseDisplay(g_pinfo.dpy);
169                         return SYSTEM_INFO_ERROR_IO_ERROR;
170                 } else {
171                         while (fgets(str, MAXBUFSIZE, cmdline)) {
172                                 dpi = strstr(str, "dpi=");
173                                 SCREEN_DPI = atoi(dpi+4) / 10;
174                                 break;
175                         }
176                 }
177                 fclose(cmdline);
178
179         } else {
180                 double dp, di;
181
182                 /* diagonal size(logical) by pixel */
183                 dp = sqrt(SCREEN_WIDTH*SCREEN_WIDTH+SCREEN_HEIGHT*SCREEN_HEIGHT);
184                 /* diagonal size(physical) by inch */
185                 di = sqrt(PHYSICAL_SCREEN_WIDTH*PHYSICAL_SCREEN_WIDTH + PHYSICAL_SCREEN_HEIGHT*PHYSICAL_SCREEN_HEIGHT) / 10 / 2.54;
186                 /* DPI = PPI */
187                 SCREEN_DPI = round(dp/di);
188         }
189
190         XCloseDisplay(g_pinfo.dpy);
191
192         system_info_screen_initialized = 1;
193
194         return 0;
195 }
196
197 int system_info_get_screen_bits_per_pixel(system_info_key_e key, system_info_data_type_e data_type, void **value)
198 {
199         int *bpp;
200         int ret_val;
201
202         bpp = (int *)value;
203
204         if (0 == system_info_screen_initialized) {
205                 ret_val = system_info_screen_init();
206                 if (ret_val)
207                         return ret_val;
208         }
209
210         *bpp = BITS_PER_PIXEL;
211
212         return SYSTEM_INFO_ERROR_NONE;
213 }
214
215 int system_info_get_screen_width(system_info_key_e key, system_info_data_type_e data_type, void **value)
216 {
217         int *width;
218         int ret_val;
219
220         width = (int *)value;
221
222         if (0 == system_info_screen_initialized) {
223                 ret_val = system_info_screen_init();
224                 if (ret_val)
225                         return ret_val;
226         }
227
228         *width = SCREEN_WIDTH;
229
230         return SYSTEM_INFO_ERROR_NONE;
231 }
232
233 int system_info_get_screen_height(system_info_key_e key, system_info_data_type_e data_type, void **value)
234 {
235         int *height;
236         int ret_val;
237
238         height = (int *)value;
239
240         if (0 == system_info_screen_initialized) {
241                 ret_val = system_info_screen_init();
242                 if (ret_val)
243                         return ret_val;
244         }
245
246         *height = SCREEN_HEIGHT;
247
248         return SYSTEM_INFO_ERROR_NONE;
249 }
250
251 int system_info_get_screen_DPI(system_info_key_e key, system_info_data_type_e data_type, void **value)
252 {
253         int *bpp;
254         int ret_val;
255
256         bpp = (int *)value;
257
258         if (0 == system_info_screen_initialized) {
259                 ret_val = system_info_screen_init();
260                 if (ret_val)
261                         return ret_val;
262         }
263
264         *bpp = SCREEN_DPI;
265
266         return SYSTEM_INFO_ERROR_NONE;
267 }
268
269 int system_info_get_hdmi_supported(system_info_key_e key, system_info_data_type_e data_type, void **value)
270 {
271         bool *supported;
272         int ret_val;
273
274         if (0 == system_info_screen_initialized) {
275                 ret_val = system_info_screen_init();
276                 if (ret_val)
277                         return ret_val;
278         }
279
280         supported = (bool *)value;
281
282         *supported = HDMI_SUPPORTED;
283
284         return SYSTEM_INFO_ERROR_NONE;
285 }
286
287 int system_info_get_rca_supported(system_info_key_e key, system_info_data_type_e data_type, void **value)
288 {
289         bool *supported;
290
291         supported = (bool *)value;
292
293         *supported = RCA_SUPPORTED;
294
295         return SYSTEM_INFO_ERROR_NONE;
296 }
297
298 int system_info_get_physical_screen_height(system_info_key_e key, system_info_data_type_e data_type, void **value)
299 {
300         int *bpp;
301         int ret_val;
302
303         bpp = (int *)value;
304
305         if (0 == system_info_screen_initialized) {
306                 ret_val = system_info_screen_init();
307                 if (ret_val)
308                         return ret_val;
309         }
310
311         *bpp = PHYSICAL_SCREEN_HEIGHT;
312
313         return SYSTEM_INFO_ERROR_NONE;
314 }
315
316 int system_info_get_physical_screen_width(system_info_key_e key, system_info_data_type_e data_type, void **value)
317 {
318         int *bpp;
319         int ret_val;
320
321         bpp = (int *)value;
322
323         if (0 == system_info_screen_initialized) {
324                 ret_val = system_info_screen_init();
325                 if (ret_val)
326                         return ret_val;
327         }
328
329         *bpp = PHYSICAL_SCREEN_WIDTH;
330
331         return SYSTEM_INFO_ERROR_NONE;
332 }