2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <X11/extensions/Xrandr.h>
30 #include <system_info.h>
31 #include <system_info_private.h>
37 #define LOG_TAG "CAPI_SYSTEM_INFO"
39 typedef struct _progInfo ProgInfo;
48 int event_base, error_base;
50 XRRScreenResources *res;
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;
63 int system_info_screen_init()
66 XPixmapFormatValues *pmf = NULL;
68 memset(&g_pinfo, 0x0, sizeof(ProgInfo));
70 g_pinfo.dpy = XOpenDisplay(NULL);
71 if (NULL == g_pinfo.dpy) {
72 LOGE("XOpenDisplay Failed");
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);
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);
87 g_pinfo.res = XRRGetScreenResources(g_pinfo.dpy, g_pinfo.root);
90 LOGE("XRRGetScreenResources Failed");
91 XCloseDisplay(g_pinfo.dpy);
95 pmf = XListPixmapFormats(g_pinfo.dpy, &n);
98 LOGE("XListPixmapFormats Failed");
99 XCloseDisplay(g_pinfo.dpy);
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;
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]);
113 LOGE("XRRGetOutputInfo Failed");
114 XCloseDisplay(g_pinfo.dpy);
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);
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;
130 XRRFreeCrtcInfo(crtc_info);
131 } else if (!strcmp(output_info->name, "HDMI1"))
132 HDMI_SUPPORTED = true;
133 XRRFreeOutputInfo(output_info);
136 if (BITS_PER_PIXEL == 0) {
137 LOGE("BIT PER PIXEL is Zero");
138 XCloseDisplay(g_pinfo.dpy);
143 SCREEN_WIDTH = DisplayWidth(g_pinfo.dpy, DefaultScreen(g_pinfo.dpy));
146 LOGE("SCREEN WIDTH is Zero");
147 XCloseDisplay(g_pinfo.dpy);
152 SCREEN_HEIGHT = DisplayHeight(g_pinfo.dpy, DefaultScreen(g_pinfo.dpy));
154 if (!SCREEN_HEIGHT) {
155 LOGE("SCREEN HEIGHT is Zero");
156 XCloseDisplay(g_pinfo.dpy);
160 if (system_info_get_system_info_model_type() == SYSTEM_INFO_MODEL_TYPE_EMULATOR) {
163 char str[MAXBUFSIZE];
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;
171 while (fgets(str, MAXBUFSIZE, cmdline)) {
172 dpi = strstr(str, "dpi=");
173 SCREEN_DPI = atoi(dpi+4) / 10;
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;
187 SCREEN_DPI = round(dp/di);
190 XCloseDisplay(g_pinfo.dpy);
192 system_info_screen_initialized = 1;
197 int system_info_get_screen_bits_per_pixel(system_info_key_e key, system_info_data_type_e data_type, void **value)
204 if (0 == system_info_screen_initialized) {
205 ret_val = system_info_screen_init();
210 *bpp = BITS_PER_PIXEL;
212 return SYSTEM_INFO_ERROR_NONE;
215 int system_info_get_screen_width(system_info_key_e key, system_info_data_type_e data_type, void **value)
220 width = (int *)value;
222 if (0 == system_info_screen_initialized) {
223 ret_val = system_info_screen_init();
228 *width = SCREEN_WIDTH;
230 return SYSTEM_INFO_ERROR_NONE;
233 int system_info_get_screen_height(system_info_key_e key, system_info_data_type_e data_type, void **value)
238 height = (int *)value;
240 if (0 == system_info_screen_initialized) {
241 ret_val = system_info_screen_init();
246 *height = SCREEN_HEIGHT;
248 return SYSTEM_INFO_ERROR_NONE;
251 int system_info_get_screen_DPI(system_info_key_e key, system_info_data_type_e data_type, void **value)
258 if (0 == system_info_screen_initialized) {
259 ret_val = system_info_screen_init();
266 return SYSTEM_INFO_ERROR_NONE;
269 int system_info_get_hdmi_supported(system_info_key_e key, system_info_data_type_e data_type, void **value)
274 if (0 == system_info_screen_initialized) {
275 ret_val = system_info_screen_init();
280 supported = (bool *)value;
282 *supported = HDMI_SUPPORTED;
284 return SYSTEM_INFO_ERROR_NONE;
287 int system_info_get_rca_supported(system_info_key_e key, system_info_data_type_e data_type, void **value)
291 supported = (bool *)value;
293 *supported = RCA_SUPPORTED;
295 return SYSTEM_INFO_ERROR_NONE;
298 int system_info_get_physical_screen_height(system_info_key_e key, system_info_data_type_e data_type, void **value)
305 if (0 == system_info_screen_initialized) {
306 ret_val = system_info_screen_init();
311 *bpp = PHYSICAL_SCREEN_HEIGHT;
313 return SYSTEM_INFO_ERROR_NONE;
316 int system_info_get_physical_screen_width(system_info_key_e key, system_info_data_type_e data_type, void **value)
323 if (0 == system_info_screen_initialized) {
324 ret_val = system_info_screen_init();
329 *bpp = PHYSICAL_SCREEN_WIDTH;
331 return SYSTEM_INFO_ERROR_NONE;