aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/display.c
blob: e1ba507c985d24e83865576aeac56557771fb936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/

#include "android/utils/display.h"
#include "android/utils/debug.h"

#define  D(...)   VERBOSE_PRINT(init,__VA_ARGS__)

/** HOST RESOLUTION SETTINGS
 **
 ** return the main monitor's DPI resolution according to the host device
 ** beware: this is not always reliable or even obtainable.
 **
 ** returns 0 on success, or -1 in case of error (e.g. the system returns funky values)
 **/

/** NOTE: the following code assumes that we exclusively use X11 on Linux, and Quartz on OS X
 **/

#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <SDL_syswm.h>

int
get_monitor_resolution( int  *px_dpi, int  *py_dpi )
{
    HDC  displayDC = CreateDC( "DISPLAY", NULL, NULL, NULL );
    int  xdpi, ydpi;

    if (displayDC == NULL) {
        D( "%s: could not get display DC\n", __FUNCTION__ );
        return -1;
    }
    xdpi = GetDeviceCaps( displayDC, LOGPIXELSX );
    ydpi = GetDeviceCaps( displayDC, LOGPIXELSY );

    /* sanity checks */
    if (xdpi < 20 || xdpi > 400 || ydpi < 20 || ydpi > 400) {
        D( "%s: bad resolution: xpi=%d ydpi=%d", __FUNCTION__,
                xdpi, ydpi );
        return -1;
    }

    *px_dpi = xdpi;
    *py_dpi = ydpi;
    return 0;
}

int
get_nearest_monitor_rect( int  *x, int  *y, int  *width, int  *height )
{
    SDL_SysWMinfo  info;
    HMONITOR       monitor;
    MONITORINFO    monitorInfo;

    SDL_VERSION(&info.version);

    if ( !SDL_GetWMInfo(&info) ) {
        D( "%s: SDL_GetWMInfo() failed: %s", __FUNCTION__, SDL_GetError());
        return -1;
    }

    monitor = MonitorFromWindow( info.window, MONITOR_DEFAULTTONEAREST );
    monitorInfo.cbSize = sizeof(monitorInfo);
    GetMonitorInfo( monitor, &monitorInfo );

    *x      = monitorInfo.rcMonitor.left;
    *y      = monitorInfo.rcMonitor.top;
    *width  = monitorInfo.rcMonitor.right - *x;
    *height = monitorInfo.rcMonitor.bottom - *y;

    D("%s: found (x,y,w,h)=(%d,%d,%d,%d)", __FUNCTION__,
      *x, *y, *width, *height);

    return 0;
}


#elif defined __APPLE__

/* the real implementation is in display-quartz.m, but
 * unfortunately, the Android build system doesn't know
 * how to build Objective-C sources, so provide stubs
 * here instead.
 *
 * CONFIG_NO_COCOA is set by Makefile.android
 */

#ifdef CONFIG_NO_COCOA
int
get_monitor_resolution( int  *px_dpi, int  *py_dpi )
{
    return -1;
}

int
get_nearest_monitor_rect( int  *x, int  *y, int  *width, int  *height )
{
    return -1;
}
#endif /* CONFIG_NO_COCOA */

#else  /* Linux and others */
#include <SDL.h>
#include <SDL_syswm.h>
#include <dlfcn.h>
#include <X11/Xlib.h>
#define  MM_PER_INCH   25.4

#define  DYNLINK_FUNCTIONS  \
    DYNLINK_FUNC(int,XDefaultScreen,(Display*)) \
    DYNLINK_FUNC(int,XDisplayWidth,(Display*,int)) \
    DYNLINK_FUNC(int,XDisplayWidthMM,(Display*,int)) \
    DYNLINK_FUNC(int,XDisplayHeight,(Display*,int)) \
    DYNLINK_FUNC(int,XDisplayHeightMM,(Display*,int)) \

#define  DYNLINK_FUNCTIONS_INIT \
    x11_dynlink_init

#include "dynlink.h"

static int    x11_lib_inited;
static void*  x11_lib;

int
x11_lib_init( void )
{
    if (!x11_lib_inited) {
        x11_lib_inited = 1;

        x11_lib = dlopen( "libX11.so", RTLD_NOW );

        if (x11_lib == NULL) {
            x11_lib = dlopen( "libX11.so.6", RTLD_NOW );
        }
        if (x11_lib == NULL) {
            D("%s: Could not find libX11.so on this machine",
              __FUNCTION__);
            return -1;
        }

        if (x11_dynlink_init(x11_lib) < 0) {
            D("%s: didn't find necessary symbols in libX11.so",
              __FUNCTION__);
            dlclose(x11_lib);
            x11_lib = NULL;
        }
    }
    return x11_lib ? 0 : -1;
}


int
get_monitor_resolution( int  *px_dpi, int  *py_dpi )
{
    SDL_SysWMinfo info;
    Display*      display;
    int           screen;
    int           width, width_mm, height, height_mm, xdpi, ydpi;

    SDL_VERSION(&info.version);

    if ( !SDL_GetWMInfo(&info) ) {
        D( "%s: SDL_GetWMInfo() failed: %s", __FUNCTION__, SDL_GetError());
        return -1;
    }

    if (x11_lib_init() < 0)
        return -1;

    display = info.info.x11.display;
    screen  = FF(XDefaultScreen)(display);

    width     = FF(XDisplayWidth)(display, screen);
    width_mm  = FF(XDisplayWidthMM)(display, screen);
    height    = FF(XDisplayHeight)(display, screen);
    height_mm = FF(XDisplayHeightMM)(display, screen);

    if (width_mm <= 0 || height_mm <= 0) {
        D( "%s: bad screen dimensions: width_mm = %d, height_mm = %d",
                __FUNCTION__, width_mm, height_mm);
        return -1;
    }

    D( "%s: found screen width=%d height=%d width_mm=%d height_mm=%d",
            __FUNCTION__, width, height, width_mm, height_mm );

    xdpi = width  * MM_PER_INCH / width_mm;
    ydpi = height * MM_PER_INCH / height_mm;

    if (xdpi < 20 || xdpi > 400 || ydpi < 20 || ydpi > 400) {
        D( "%s: bad resolution: xpi=%d ydpi=%d", __FUNCTION__,
                xdpi, ydpi );
        return -1;
    }

    *px_dpi = xdpi;
    *py_dpi = ydpi;

    return 0;
}

int
get_nearest_monitor_rect( int  *x, int *y, int  *width, int  *height )
{
    SDL_SysWMinfo info;
    Display*      display;
    int           screen;

    SDL_VERSION(&info.version);

    if ( !SDL_GetWMInfo(&info) ) {
        D( "%s: SDL_GetWMInfo() failed: %s", __FUNCTION__, SDL_GetError());
        return -1;
    }

    if (x11_lib_init() < 0)
        return -1;

    display = info.info.x11.display;
    screen  = FF(XDefaultScreen)(display);

    *x      = 0;
    *y      = 0;
    *width  = FF(XDisplayWidth)(display, screen);
    *height = FF(XDisplayHeight)(display, screen);

    D("%s: found (x,y,w,h)=(%d,%d,%d,%d)", __FUNCTION__,
      *x, *y, *width, *height);

    return 0;
}

#endif