2.0_alpha release commit
[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 "TIZEN_N_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;
67
68         memset(&g_pinfo, 0x0, sizeof(ProgInfo));
69
70         g_pinfo.dpy = XOpenDisplay(NULL);
71         if (NULL == g_pinfo.dpy)
72                 return -1;
73
74         if (0 > g_pinfo.screen)
75                 g_pinfo.screen = DefaultScreen(g_pinfo.dpy);
76         g_pinfo.root = RootWindow(g_pinfo.dpy, g_pinfo.screen);
77
78         if (!XRRQueryExtension(g_pinfo.dpy, &g_pinfo.event_base, &g_pinfo.error_base) ||
79                 !XRRQueryVersion(g_pinfo.dpy, &g_pinfo.major, &g_pinfo.minor)) {
80                 return -1;
81         }
82
83         g_pinfo.res = XRRGetScreenResources(g_pinfo.dpy, g_pinfo.root);
84
85         if (!g_pinfo.res)
86                 return -1;
87
88         pmf = XListPixmapFormats(g_pinfo.dpy, &n);
89
90         for (i = 0; i < n; i++) {
91                 if (BITS_PER_PIXEL < pmf->bits_per_pixel)
92                         BITS_PER_PIXEL = pmf->bits_per_pixel;
93                 pmf++;
94         }
95
96         for (i = 0; i < g_pinfo.res->noutput; i++) {
97                 XRROutputInfo *output_info = XRRGetOutputInfo(g_pinfo.dpy, g_pinfo.res, g_pinfo.res->outputs[i]);
98                 if (!output_info)
99                         return -1;
100
101                 /* find target lcd */
102                 if (!strcmp(output_info->name, "LVDS1")) {
103                         /* XRRCrtcInfo information */
104                         XRRCrtcInfo *crtc_info = XRRGetCrtcInfo(g_pinfo.dpy, g_pinfo.res, output_info->crtc);
105                         if (!crtc_info)
106                                 break;
107
108                         SCREEN_WIDTH = crtc_info->width;
109                         SCREEN_HEIGHT = crtc_info->height;
110                         PHYSICAL_SCREEN_WIDTH = output_info->mm_width;
111                         PHYSICAL_SCREEN_HEIGHT = output_info->mm_height;
112
113                         XRRFreeCrtcInfo(crtc_info);
114                 } else if (!strcmp(output_info->name, "HDMI1"))
115                         HDMI_SUPPORTED = true;
116                 XRRFreeOutputInfo(output_info);
117         }
118
119         if (BITS_PER_PIXEL == 0)
120                 return -1;
121
122         if (!SCREEN_WIDTH)
123                 SCREEN_WIDTH = DisplayWidth(g_pinfo.dpy, DefaultScreen(g_pinfo.dpy));
124
125         if (!SCREEN_WIDTH)
126                 return -1;
127
128         if (!SCREEN_HEIGHT)
129                 SCREEN_HEIGHT = DisplayHeight(g_pinfo.dpy, DefaultScreen(g_pinfo.dpy));
130
131         if (!SCREEN_HEIGHT)
132                 return -1;
133
134         if (system_info_get_system_info_model_type() == SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
135                 FILE *cmdline;
136                 char *dpi;
137                 char str[MAXBUFSIZE];
138
139                 cmdline = fopen(CMDLINE_PATH, "r");
140                 if (NULL == cmdline) {
141                         LOGE("[%s] cannot file open %s file!!!", __func__, CPU_INFO_FILE_PATH);
142                         return SYSTEM_INFO_ERROR_IO_ERROR;
143                 } else {
144                         while (fgets(str, MAXBUFSIZE, cmdline)) {
145                                 dpi = strstr(str, "dpi=");
146                                 SCREEN_DPI = atoi(dpi+4) / 10;
147                                 fclose(cmdline);
148                                 break;
149                         }
150                 }
151         } else {
152                 int max_resolution;
153                 double ratio, width_inch;
154
155                 width_inch = PHYSICAL_SCREEN_WIDTH / 10 / 2.54;
156                 max_resolution = SCREEN_WIDTH + SCREEN_HEIGHT;
157                 ratio = SCREEN_WIDTH / SCREEN_HEIGHT;
158
159                 SCREEN_DPI = max_resolution / (width_inch + (ratio * width_inch)) + 0.5;
160         }
161
162         XCloseDisplay(g_pinfo.dpy);
163
164         system_info_screen_initialized = 1;
165
166         return 0;
167 }
168
169 int system_info_get_screen_bits_per_pixel(system_info_key_e key, system_info_data_type_e data_type, void **value)
170 {
171         int *bpp;
172         int ret_val;
173
174         bpp = (int *)value;
175
176         if (0 == system_info_screen_initialized) {
177                 ret_val = system_info_screen_init();
178                 if (ret_val)
179                         return ret_val;
180         }
181
182         *bpp = BITS_PER_PIXEL;
183
184         return SYSTEM_INFO_ERROR_NONE;
185 }
186
187 int system_info_get_screen_width(system_info_key_e key, system_info_data_type_e data_type, void **value)
188 {
189         int *width;
190         int ret_val;
191
192         width = (int *)value;
193
194         if (0 == system_info_screen_initialized) {
195                 ret_val = system_info_screen_init();
196                 if (ret_val)
197                         return ret_val;
198         }
199
200         *width = SCREEN_WIDTH;
201
202         return SYSTEM_INFO_ERROR_NONE;
203 }
204
205 int system_info_get_screen_height(system_info_key_e key, system_info_data_type_e data_type, void **value)
206 {
207         int *height;
208         int ret_val;
209
210         height = (int *)value;
211
212         if (0 == system_info_screen_initialized) {
213                 ret_val = system_info_screen_init();
214                 if (ret_val)
215                         return ret_val;
216         }
217
218         *height = SCREEN_HEIGHT;
219
220         return SYSTEM_INFO_ERROR_NONE;
221 }
222
223 int system_info_get_screen_DPI(system_info_key_e key, system_info_data_type_e data_type, void **value)
224 {
225         int *bpp;
226         int ret_val;
227
228         bpp = (int *)value;
229
230         if (0 == system_info_screen_initialized) {
231                 ret_val = system_info_screen_init();
232                 if (ret_val)
233                         return ret_val;
234         }
235
236         *bpp = SCREEN_DPI;
237
238         return SYSTEM_INFO_ERROR_NONE;
239 }
240
241 int system_info_get_hdmi_supported(system_info_key_e key, system_info_data_type_e data_type, void **value)
242 {
243         bool *supported;
244         int ret_val;
245
246         if (0 == system_info_screen_initialized) {
247                 ret_val = system_info_screen_init();
248                 if (ret_val)
249                         return ret_val;
250         }
251
252         supported = (bool *)value;
253
254         *supported = HDMI_SUPPORTED;
255
256         return SYSTEM_INFO_ERROR_NONE;
257 }
258
259 int system_info_get_rca_supported(system_info_key_e key, system_info_data_type_e data_type, void **value)
260 {
261         bool *supported;
262
263         supported = (bool *)value;
264
265         *supported = RCA_SUPPORTED;
266
267         return SYSTEM_INFO_ERROR_NONE;
268 }
269
270 int system_info_get_physical_screen_height(system_info_key_e key, system_info_data_type_e data_type, void **value)
271 {
272         int *bpp;
273         int ret_val;
274
275         bpp = (int *)value;
276
277         if (0 == system_info_screen_initialized) {
278                 ret_val = system_info_screen_init();
279                 if (ret_val)
280                         return ret_val;
281         }
282
283         *bpp = PHYSICAL_SCREEN_HEIGHT;
284
285         return SYSTEM_INFO_ERROR_NONE;
286 }
287
288 int system_info_get_physical_screen_width(system_info_key_e key, system_info_data_type_e data_type, void **value)
289 {
290         int *bpp;
291         int ret_val;
292
293         bpp = (int *)value;
294
295         if (0 == system_info_screen_initialized) {
296                 ret_val = system_info_screen_init();
297                 if (ret_val)
298                         return ret_val;
299         }
300
301         *bpp = PHYSICAL_SCREEN_WIDTH;
302
303         return SYSTEM_INFO_ERROR_NONE;
304 }
305