summaryrefslogtreecommitdiff
path: root/plugins/Scriver/multimon.h
blob: aedd943da4be59df54e658ba0cf486f8e06e0db0 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//#if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)

//=============================================================================
//
// MULTIMON
// stub module that "stubs" multiple monitor APIs on pre-Memphis Win32 OSes
//
// By using this header your code will work unchanged on Win95,
// you will get back correct values from GetSystemMetrics() for new metrics
// and the new APIs will act like only one display is present.
//
// exactly one source must include this with COMPILE_MULTIMON_STUBS defined
//
//=============================================================================

#ifdef __cplusplus
extern "C" {            /* Assume C declarations for C++ */
#endif  /* __cplusplus */

//
// if we are building on Win95/NT4 headers we need to declare this stuff ourselves
//
#ifndef SM_SAMEDISPLAYFORMAT
#define SM_SAMEDISPLAYFORMAT    81
#endif

#ifndef SM_CMONITORS
#ifndef MONITORINFOF_PRIMARY

#define SM_XVIRTUALSCREEN       76
#define SM_YVIRTUALSCREEN       77
#define SM_CXVIRTUALSCREEN      78
#define SM_CYVIRTUALSCREEN      79
#define SM_CMONITORS            80

DECLARE_HANDLE(HMONITOR);

typedef struct tagMONITORINFO
{
    DWORD   cbSize;
    RECT    rcMonitor;
    RECT    rcWork;
    DWORD   dwFlags;
} MONITORINFO, *LPMONITORINFO;

#define CCHDEVICENAME 32
#endif  // SM_CMONITORS

#define MONITORINFOF_PRIMARY        0x00000001

#ifndef MONITOR_DEFAULTTONULL
#define MONITOR_DEFAULTTONULL       0x00000000
#endif
#ifndef MONITOR_DEFAULTTOPRIMARY
#define MONITOR_DEFAULTTOPRIMARY    0x00000001
#endif
#ifndef MONITOR_DEFAULTTONEAREST
#define MONITOR_DEFAULTTONEAREST    0x00000002
#endif

typedef BOOL (CALLBACK* MONITORENUMPROC)(HMONITOR, HDC, LPRECT, LPARAM);

	#ifdef __cplusplus
		typedef struct tagMONITORINFOEX : public tagMONITORINFO
		{
		  TCHAR       szDevice[CCHDEVICENAME];
		} MONITORINFOEX, *LPMONITORINFOEX;
	#else
		typedef struct
		{
		  MONITORINFO;
		  TCHAR       szDevice[CCHDEVICENAME];
		} MONITORINFOEX, *LPMONITORINFOEX;
	#endif


#ifndef DISPLAY_DEVICE_ATTACHED_TO_DESKTOP

typedef struct {
    DWORD  cb;
    CHAR   DeviceName[32];
    CHAR   DeviceString[128];
    DWORD  StateFlags;
} DISPLAY_DEVICE;

#define DISPLAY_DEVICE_ATTACHED_TO_DESKTOP 0x00000001
#define DISPLAY_DEVICE_MULTI_DRIVER        0x00000002
#define DISPLAY_DEVICE_PRIMARY_DEVICE      0x00000004
#define DISPLAY_DEVICE_MIRRORING_DRIVER    0x00000008

#endif
#endif
#define DISPLAY_DEVICE_VGA                 0x00000010

#ifndef ENUM_CURRENT_SETTINGS
#define ENUM_CURRENT_SETTINGS       ((DWORD)-1)
#define ENUM_REGISTRY_SETTINGS      ((DWORD)-2)
#endif

#undef GetMonitorInfo
#undef GetSystemMetrics
#undef MonitorFromWindow
#undef MonitorFromRect
#undef MonitorFromPoint
#undef EnumDisplayMonitors
#undef EnumDisplayDevices

//
// define this to compile the stubs
// otherwise you get the declarations
//
#ifdef COMPILE_MULTIMON_STUBS

    //---------------------------------------------------------------------------
    //
    // Implement the API stubs.
    //
    //---------------------------------------------------------------------------

    int      (WINAPI* g_pfnGetSystemMetrics)(int);
    HMONITOR (WINAPI* g_pfnMonitorFromWindow)(HWND, BOOL);
    HMONITOR (WINAPI* g_pfnMonitorFromRect)(LPCRECT, BOOL);
    HMONITOR (WINAPI* g_pfnMonitorFromPoint)(POINT, BOOL);
    BOOL     (WINAPI* g_pfnGetMonitorInfo)(HMONITOR, LPMONITORINFO);
    BOOL     (WINAPI* g_pfnEnumDisplayMonitors)(HDC, LPCRECT,
              MONITORENUMPROC, LPARAM);
    BOOL     (WINAPI *g_pfnEnumDisplayDevices)(LPVOID, int,
              DISPLAY_DEVICE *,DWORD);

    BOOL InitMultipleMonitorStubs(void)
    {
        HMODULE hUser32;
        static BOOL fInitDone;

        if (fInitDone)
        {
            return g_pfnGetMonitorInfo != NULL;
        }

        if ((hUser32 = GetModuleHandle(TEXT("USER32"))) &&
            (*(FARPROC*)&g_pfnGetSystemMetrics    = GetProcAddress(hUser32,"GetSystemMetrics")) &&
            (*(FARPROC*)&g_pfnMonitorFromWindow   = GetProcAddress(hUser32,"MonitorFromWindow")) &&
            (*(FARPROC*)&g_pfnMonitorFromRect     = GetProcAddress(hUser32,"MonitorFromRect")) &&
            (*(FARPROC*)&g_pfnMonitorFromPoint    = GetProcAddress(hUser32,"MonitorFromPoint")) &&
            (*(FARPROC*)&g_pfnEnumDisplayMonitors = GetProcAddress(hUser32,"EnumDisplayMonitors")) &&
			(*(FARPROC*)&g_pfnGetMonitorInfo      = GetProcAddress(hUser32,"GetMonitorInfoW")) &&
            (*(FARPROC*)&g_pfnEnumDisplayDevices  = GetProcAddress(hUser32,"EnumDisplayDevicesW")) &&
			(GetSystemMetrics(SM_CXVIRTUALSCREEN) >= GetSystemMetrics(SM_CXSCREEN)) &&
            (GetSystemMetrics(SM_CYVIRTUALSCREEN) >= GetSystemMetrics(SM_CYSCREEN)))
        {
            fInitDone = TRUE;
            return TRUE;
        }
        else
        {
            g_pfnGetSystemMetrics    = NULL;
            g_pfnMonitorFromWindow   = NULL;
            g_pfnMonitorFromRect     = NULL;
            g_pfnMonitorFromPoint    = NULL;
            g_pfnGetMonitorInfo      = NULL;
            g_pfnEnumDisplayMonitors = NULL;
            g_pfnEnumDisplayDevices  = NULL;

            fInitDone = TRUE;
            return FALSE;
        }
    }

    //---------------------------------------------------------------------------
    //
    // "stubbed" implementations of Monitor APIs that work with the primary         //  display
    //
    //---------------------------------------------------------------------------

    int WINAPI
    xGetSystemMetrics(int nIndex)
    {
        if (InitMultipleMonitorStubs())
            return g_pfnGetSystemMetrics(nIndex);

        switch (nIndex)
        {
        case SM_CMONITORS:
        case SM_SAMEDISPLAYFORMAT:
            return 1;

        case SM_XVIRTUALSCREEN:
        case SM_YVIRTUALSCREEN:
            return 0;

        case SM_CXVIRTUALSCREEN:
            nIndex = SM_CXSCREEN;
            break;

        case SM_CYVIRTUALSCREEN:
            nIndex = SM_CYSCREEN;
            break;
        }

        return GetSystemMetrics(nIndex);
    }

    #define xPRIMARY_MONITOR ((HMONITOR)0x42)

    HMONITOR WINAPI
    xMonitorFromRect(LPCRECT lprcScreenCoords, UINT uFlags)
    {
        if (InitMultipleMonitorStubs())
            return g_pfnMonitorFromRect(lprcScreenCoords, uFlags);

        if ((uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
            ((lprcScreenCoords->right > 0) &&
            (lprcScreenCoords->bottom > 0) &&
            (lprcScreenCoords->left < GetSystemMetrics(SM_CXSCREEN)) &&
            (lprcScreenCoords->top < GetSystemMetrics(SM_CYSCREEN))))
        {
            return xPRIMARY_MONITOR;
        }

        return NULL;
    }

    HMONITOR WINAPI
    xMonitorFromWindow(HWND hWnd, UINT uFlags)
    {
        RECT rc;

        if (InitMultipleMonitorStubs())
            return g_pfnMonitorFromWindow(hWnd, uFlags);

        if (uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST))
            return xPRIMARY_MONITOR;

        if (GetWindowRect(hWnd, &rc))
            return xMonitorFromRect(&rc, uFlags);

        return NULL;
    }

    HMONITOR WINAPI
    xMonitorFromPoint(POINT ptScreenCoords, UINT uFlags)
    {
        if (InitMultipleMonitorStubs())
            return g_pfnMonitorFromPoint(ptScreenCoords, uFlags);

        if ((uFlags & (MONITOR_DEFAULTTOPRIMARY | MONITOR_DEFAULTTONEAREST)) ||
            ((ptScreenCoords.x >= 0) &&
            (ptScreenCoords.x < GetSystemMetrics(SM_CXSCREEN)) &&
            (ptScreenCoords.y >= 0) &&
            (ptScreenCoords.y < GetSystemMetrics(SM_CYSCREEN))))
        {
            return xPRIMARY_MONITOR;
        }

        return NULL;
    }

    BOOL WINAPI
    xGetMonitorInfo(HMONITOR hMonitor, LPMONITORINFO lpMonitorInfo)
    {
        RECT rcWork;

        if (InitMultipleMonitorStubs())
            return g_pfnGetMonitorInfo(hMonitor, lpMonitorInfo);

        if ((hMonitor == xPRIMARY_MONITOR) && lpMonitorInfo &&
            (lpMonitorInfo->cbSize >= sizeof(MONITORINFO)) &&
            SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWork, 0))
        {
            lpMonitorInfo->rcMonitor.left = 0;
            lpMonitorInfo->rcMonitor.top  = 0;
            lpMonitorInfo->rcMonitor.right  = GetSystemMetrics(SM_CXSCREEN);
            lpMonitorInfo->rcMonitor.bottom = GetSystemMetrics(SM_CYSCREEN);
            lpMonitorInfo->rcWork = rcWork;
            lpMonitorInfo->dwFlags = MONITORINFOF_PRIMARY;

            if (lpMonitorInfo->cbSize >= sizeof(MONITORINFOEX))
                lstrcpy(((MONITORINFOEX*)lpMonitorInfo)->szDevice,
                TEXT("DISPLAY"));

            return TRUE;
        }

        return FALSE;
    }

    BOOL WINAPI
    xEnumDisplayMonitors(
        HDC hdc,
        LPCRECT lprcIntersect,
        MONITORENUMPROC lpfnEnumProc,
        LPARAM lData)
    {
        RECT rcCallback, rcLimit;

        if (InitMultipleMonitorStubs())
            return g_pfnEnumDisplayMonitors(hdc, lprcIntersect, lpfnEnumProc,
                                           lData);

        if (!lpfnEnumProc)
            return FALSE;

        rcLimit.left   = 0;
        rcLimit.top    = 0;
        rcLimit.right  = GetSystemMetrics(SM_CXSCREEN);
        rcLimit.bottom = GetSystemMetrics(SM_CYSCREEN);

        if (hdc)
        {
            RECT rcClip;
            HWND hWnd;

            if ((hWnd = WindowFromDC(hdc)) == NULL)
                return FALSE;

            switch (GetClipBox(hdc, &rcClip))
            {
            default:
                MapWindowPoints(NULL, hWnd, (LPPOINT)&rcLimit, 2);
                if (IntersectRect(&rcCallback, &rcClip, &rcLimit))
                    break;
                //fall thru
            case NULLREGION:
                 return TRUE;
            case ERROR:
                 return FALSE;
            }

            rcLimit = rcCallback;
        }

        if (!lprcIntersect ||
            IntersectRect(&rcCallback, lprcIntersect, &rcLimit))
        {
            lpfnEnumProc(xPRIMARY_MONITOR, hdc, &rcCallback, lData);
        }

        return TRUE;
    }

    BOOL WINAPI
    xEnumDisplayDevices(LPVOID lpReserved, int iDeviceNum,
DISPLAY_DEVICE * pDisplayDevice, DWORD dwFlags)
    {
        if (InitMultipleMonitorStubs())
            return g_pfnEnumDisplayDevices(lpReserved, iDeviceNum,
                                           pDisplayDevice, dwFlags);

        return FALSE;
    }

    #undef xPRIMARY_MONITOR
    #undef COMPILE_MULTIMON_STUBS

#else    // COMPILE_MULTIMON_STUBS

    extern int    WINAPI xGetSystemMetrics(int);
    extern HMONITOR WINAPI xMonitorFromWindow(HWND, UINT);
    extern HMONITOR WINAPI xMonitorFromRect(LPCRECT, UINT);
    extern HMONITOR WINAPI xMonitorFromPoint(POINT, UINT);
    extern BOOL    WINAPI xGetMonitorInfo(HMONITOR, LPMONITORINFO);
    extern BOOL    WINAPI xEnumDisplayMonitors(HDC, LPCRECT, MONITORENUMPROC,
                                               LPARAM);
    extern BOOL WINAPI xEnumDisplayDevices(LPVOID, int, DISPLAY_DEVICE *,DWORD);

#endif    // COMPILE_MULTIMON_STUBS

//
// build defines that replace the regular APIs with our versions
//
#define GetSystemMetrics    xGetSystemMetrics
#define MonitorFromWindow   xMonitorFromWindow
#define MonitorFromRect     xMonitorFromRect
#define MonitorFromPoint    xMonitorFromPoint
#define GetMonitorInfo      xGetMonitorInfo
#define EnumDisplayMonitors xEnumDisplayMonitors
#define EnumDisplayDevices    xEnumDisplayDevices

#ifdef __cplusplus
}
#endif    /* __cplusplus */

//#endif  /* !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500) */