Skip to content

Latest commit

 

History

History
1022 lines (875 loc) · 31.6 KB

SDL_windowswindow.c

File metadata and controls

1022 lines (875 loc) · 31.6 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_WINDOWS
#include "../../core/windows/SDL_windows.h"
#include "SDL_assert.h"
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "../../events/SDL_keyboard_c.h"
#include "../../events/SDL_mouse_c.h"
Mar 16, 2020
Mar 16, 2020
33
#include "../../joystick/windows/SDL_rawinputjoystick_c.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "SDL_windowsvideo.h"
#include "SDL_windowswindow.h"
#include "SDL_hints.h"
/* Dropfile support */
#include <shellapi.h>
/* This is included after SDL_windowsvideo.h, which includes windows.h */
#include "SDL_syswm.h"
/* Windows CE compatibility */
#ifndef SWP_NOCOPYBITS
#define SWP_NOCOPYBITS 0
#endif
/* Fake window to help with DirectInput events. */
HWND SDL_HelperWindow = NULL;
static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
static ATOM SDL_HelperWindowClass = 0;
Jan 11, 2018
Jan 11, 2018
55
56
57
58
59
60
/* For borderless Windows, still want the following flags:
- WS_CAPTION: this seems to enable the Windows minimize animation
- WS_SYSMENU: enables system context menu on task bar
- WS_MINIMIZEBOX: window will respond to Windows minimize commands sent to all windows, such as windows key + m, shaking title bar, etc.
This will also cause the task bar to overlap the window and other windowed behaviors, so only use this for windows that shouldn't appear to be fullscreen
*/
Sep 22, 2017
Sep 22, 2017
61
62
63
#define STYLE_BASIC (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
#define STYLE_FULLSCREEN (WS_POPUP)
Jan 11, 2018
Jan 11, 2018
64
65
#define STYLE_BORDERLESS (WS_POPUP)
#define STYLE_BORDERLESS_WINDOWED (WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
66
67
68
69
70
71
72
73
74
75
76
77
#define STYLE_NORMAL (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
#define STYLE_RESIZABLE (WS_THICKFRAME | WS_MAXIMIZEBOX)
#define STYLE_MASK (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
static DWORD
GetWindowStyle(SDL_Window * window)
{
DWORD style = 0;
if (window->flags & SDL_WINDOW_FULLSCREEN) {
style |= STYLE_FULLSCREEN;
} else {
Sep 6, 2017
Sep 6, 2017
78
if (window->flags & SDL_WINDOW_BORDERLESS) {
Jan 11, 2018
Jan 11, 2018
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* SDL 2.1:
This behavior more closely matches other platform where the window is borderless
but still interacts with the window manager (e.g. task bar shows above it, it can
be resized to fit within usable desktop area, etc.) so this should be the behavior
for a future SDL release.
If you want a borderless window the size of the desktop that looks like a fullscreen
window, then you should use the SDL_WINDOW_FULLSCREEN_DESKTOP flag.
*/
if (SDL_GetHintBoolean("SDL_BORDERLESS_WINDOWED_STYLE", SDL_FALSE)) {
style |= STYLE_BORDERLESS_WINDOWED;
} else {
style |= STYLE_BORDERLESS;
}
Sep 6, 2017
Sep 6, 2017
93
94
95
96
} else {
style |= STYLE_NORMAL;
}
97
if (window->flags & SDL_WINDOW_RESIZABLE) {
Feb 11, 2020
Feb 11, 2020
98
99
100
101
102
103
104
/* You can have a borderless resizable window, but Windows doesn't always draw it correctly,
see https://bugzilla.libsdl.org/show_bug.cgi?id=4466
*/
if (!(window->flags & SDL_WINDOW_BORDERLESS) ||
SDL_GetHintBoolean("SDL_BORDERLESS_RESIZABLE_STYLE", SDL_FALSE)) {
style |= STYLE_RESIZABLE;
}
Jun 5, 2018
Jun 5, 2018
106
Aug 26, 2018
Aug 26, 2018
107
108
109
110
/* Need to set initialize minimize style, or when we call ShowWindow with WS_MINIMIZE it will activate a random window */
if (window->flags & SDL_WINDOW_MINIMIZED) {
style |= WS_MINIMIZE;
}
111
112
113
114
115
}
return style;
}
static void
Sep 9, 2017
Sep 9, 2017
116
WIN_AdjustWindowRectWithStyle(SDL_Window *window, DWORD style, BOOL menu, int *x, int *y, int *width, int *height, SDL_bool use_current)
Sep 6, 2017
Sep 6, 2017
117
118
119
120
121
{
RECT rect;
rect.left = 0;
rect.top = 0;
Sep 9, 2017
Sep 9, 2017
122
123
rect.right = (use_current ? window->w : window->windowed.w);
rect.bottom = (use_current ? window->h : window->windowed.h);
Sep 22, 2017
Sep 22, 2017
124
Jan 25, 2018
Jan 25, 2018
125
126
127
/* borderless windows will have WM_NCCALCSIZE return 0 for the non-client area. When this happens, it looks like windows will send a resize message
expanding the window client area to the previous window + chrome size, so shouldn't need to adjust the window size for the set styles.
*/
Sep 23, 2017
Sep 23, 2017
128
129
if (!(window->flags & SDL_WINDOW_BORDERLESS))
AdjustWindowRectEx(&rect, style, menu, 0);
Sep 6, 2017
Sep 6, 2017
130
Sep 9, 2017
Sep 9, 2017
131
132
*x = (use_current ? window->x : window->windowed.x) + rect.left;
*y = (use_current ? window->y : window->windowed.y) + rect.top;
Sep 6, 2017
Sep 6, 2017
133
134
135
136
137
*width = (rect.right - rect.left);
*height = (rect.bottom - rect.top);
}
static void
Sep 9, 2017
Sep 9, 2017
138
WIN_AdjustWindowRect(SDL_Window *window, int *x, int *y, int *width, int *height, SDL_bool use_current)
139
140
141
142
143
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
DWORD style;
BOOL menu;
Sep 6, 2017
Sep 6, 2017
144
Sep 9, 2017
Sep 9, 2017
145
146
147
style = GetWindowLong(hwnd, GWL_STYLE);
menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
WIN_AdjustWindowRectWithStyle(window, style, menu, x, y, width, height, use_current);
Sep 6, 2017
Sep 6, 2017
148
149
150
151
152
153
154
155
}
static void
WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
HWND top;
156
157
158
159
int x, y;
int w, h;
/* Figure out what the window area will be */
Jun 16, 2017
Jun 16, 2017
160
if (SDL_ShouldAllowTopmost() && ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS) || (window->flags & SDL_WINDOW_ALWAYS_ON_TOP))) {
161
162
163
164
top = HWND_TOPMOST;
} else {
top = HWND_NOTOPMOST;
}
Sep 6, 2017
Sep 6, 2017
165
Sep 9, 2017
Sep 9, 2017
166
WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_TRUE);
167
168
data->expected_resize = SDL_TRUE;
Oct 1, 2015
Oct 1, 2015
169
SetWindowPos(hwnd, top, x, y, w, h, flags);
170
171
172
173
data->expected_resize = SDL_FALSE;
}
static int
Jun 16, 2017
Jun 16, 2017
174
SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, HWND parent, SDL_bool created)
175
176
177
178
179
180
181
182
183
184
185
{
SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
SDL_WindowData *data;
/* Allocate the window data */
data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
if (!data) {
return SDL_OutOfMemory();
}
data->window = window;
data->hwnd = hwnd;
Jun 16, 2017
Jun 16, 2017
186
data->parent = parent;
187
data->hdc = GetDC(hwnd);
Nov 21, 2016
Nov 21, 2016
188
data->hinstance = (HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
189
190
data->created = created;
data->mouse_button_flags = 0;
Apr 7, 2020
Apr 7, 2020
191
data->last_pointer_update = (LPARAM)-1;
192
data->videodata = videodata;
Oct 7, 2015
Oct 7, 2015
193
data->initializing = SDL_TRUE;
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
window->driverdata = data;
/* Associate the data with the window */
if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
ReleaseDC(hwnd, data->hdc);
SDL_free(data);
return WIN_SetError("SetProp() failed");
}
/* Set up the window proc function */
#ifdef GWLP_WNDPROC
data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
if (data->wndproc == WIN_WindowProc) {
data->wndproc = NULL;
} else {
SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
}
#else
data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
if (data->wndproc == WIN_WindowProc) {
data->wndproc = NULL;
} else {
SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
}
#endif
/* Fill in the SDL window with the window data */
{
RECT rect;
if (GetClientRect(hwnd, &rect)) {
int w = rect.right;
int h = rect.bottom;
Jan 25, 2018
Jan 25, 2018
227
if ((window->windowed.w && window->windowed.w != w) || (window->windowed.h && window->windowed.h != h)) {
228
/* We tried to create a window larger than the desktop and Windows didn't allow it. Override! */
Oct 1, 2015
Oct 1, 2015
229
230
int x, y;
/* Figure out what the window area will be */
Jan 25, 2018
Jan 25, 2018
231
WIN_AdjustWindowRect(window, &x, &y, &w, &h, SDL_FALSE);
Oct 1, 2015
Oct 1, 2015
232
SetWindowPos(hwnd, HWND_NOTOPMOST, x, y, w, h, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
} else {
window->w = w;
window->h = h;
}
}
}
{
POINT point;
point.x = 0;
point.y = 0;
if (ClientToScreen(hwnd, &point)) {
window->x = point.x;
window->y = point.y;
}
}
{
DWORD style = GetWindowLong(hwnd, GWL_STYLE);
if (style & WS_VISIBLE) {
window->flags |= SDL_WINDOW_SHOWN;
} else {
window->flags &= ~SDL_WINDOW_SHOWN;
}
Sep 22, 2017
Sep 22, 2017
255
if (style & WS_POPUP) {
256
window->flags |= SDL_WINDOW_BORDERLESS;
Sep 22, 2017
Sep 22, 2017
257
258
} else {
window->flags &= ~SDL_WINDOW_BORDERLESS;
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
}
if (style & WS_THICKFRAME) {
window->flags |= SDL_WINDOW_RESIZABLE;
} else {
window->flags &= ~SDL_WINDOW_RESIZABLE;
}
#ifdef WS_MAXIMIZE
if (style & WS_MAXIMIZE) {
window->flags |= SDL_WINDOW_MAXIMIZED;
} else
#endif
{
window->flags &= ~SDL_WINDOW_MAXIMIZED;
}
#ifdef WS_MINIMIZE
if (style & WS_MINIMIZE) {
window->flags |= SDL_WINDOW_MINIMIZED;
} else
#endif
{
window->flags &= ~SDL_WINDOW_MINIMIZED;
}
}
if (GetFocus() == hwnd) {
window->flags |= SDL_WINDOW_INPUT_FOCUS;
SDL_SetKeyboardFocus(data->window);
if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
RECT rect;
GetClientRect(hwnd, &rect);
ClientToScreen(hwnd, (LPPOINT) & rect);
ClientToScreen(hwnd, (LPPOINT) & rect + 1);
ClipCursor(&rect);
}
}
/* Enable multi-touch */
if (videodata->RegisterTouchWindow) {
videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
}
Oct 7, 2015
Oct 7, 2015
300
data->initializing = SDL_FALSE;
Oct 7, 2015
Oct 7, 2015
301
302
303
304
305
/* All done! */
return 0;
}
Jan 10, 2017
Jan 10, 2017
306
307
308
309
310
int
WIN_CreateWindow(_THIS, SDL_Window * window)
{
Jun 16, 2017
Jun 16, 2017
311
HWND hwnd, parent = NULL;
312
313
314
315
DWORD style = STYLE_BASIC;
int x, y;
int w, h;
Jun 16, 2017
Jun 16, 2017
316
317
318
319
if (window->flags & SDL_WINDOW_SKIP_TASKBAR) {
parent = CreateWindow(SDL_Appname, TEXT(""), STYLE_BASIC, 0, 0, 32, 32, NULL, NULL, SDL_Instance, NULL);
}
320
321
322
style |= GetWindowStyle(window);
/* Figure out what the window area will be */
Jan 25, 2018
Jan 25, 2018
323
WIN_AdjustWindowRectWithStyle(window, style, FALSE, &x, &y, &w, &h, SDL_FALSE);
Jun 16, 2017
Jun 16, 2017
326
CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, parent, NULL,
327
328
329
330
331
332
333
SDL_Instance, NULL);
if (!hwnd) {
return WIN_SetError("Couldn't create window");
}
WIN_PumpEvents(_this);
Jun 16, 2017
Jun 16, 2017
334
if (SetupWindowData(_this, window, hwnd, parent, SDL_TRUE) < 0) {
335
DestroyWindow(hwnd);
Jun 16, 2017
Jun 16, 2017
336
337
338
if (parent) {
DestroyWindow(parent);
}
339
340
341
return -1;
}
Jan 25, 2018
Jan 25, 2018
342
/* Inform Windows of the frame change so we can respond to WM_NCCALCSIZE */
Sep 23, 2017
Sep 23, 2017
343
SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
Sep 22, 2017
Sep 22, 2017
344
Aug 26, 2018
Aug 26, 2018
345
346
347
if (window->flags & SDL_WINDOW_MINIMIZED) {
ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
}
Jun 5, 2018
Jun 5, 2018
348
Jan 10, 2017
Jan 10, 2017
349
350
if (!(window->flags & SDL_WINDOW_OPENGL)) {
return 0;
Jan 10, 2017
Jan 10, 2017
353
/* The rest of this macro mess is for OpenGL or OpenGL ES windows */
354
#if SDL_VIDEO_OPENGL_ES2
Jan 10, 2017
Jan 10, 2017
355
356
357
358
359
360
if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
#if SDL_VIDEO_OPENGL_WGL
&& (!_this->gl_data || WIN_GL_UseEGL(_this))
#endif /* SDL_VIDEO_OPENGL_WGL */
) {
#if SDL_VIDEO_OPENGL_EGL
361
362
363
364
if (WIN_GLES_SetupWindow(_this, window) < 0) {
WIN_DestroyWindow(_this, window);
return -1;
}
Jan 10, 2017
Jan 10, 2017
365
return 0;
Jan 10, 2017
Jan 10, 2017
367
368
369
return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
#endif /* SDL_VIDEO_OPENGL_EGL */
}
370
371
372
#endif /* SDL_VIDEO_OPENGL_ES2 */
#if SDL_VIDEO_OPENGL_WGL
Jan 10, 2017
Jan 10, 2017
373
374
375
if (WIN_GL_SetupWindow(_this, window) < 0) {
WIN_DestroyWindow(_this, window);
return -1;
Jan 10, 2017
Jan 10, 2017
377
378
#else
return SDL_SetError("Could not create GL window (WGL support not configured)");
379
380
381
382
383
384
385
386
387
388
389
#endif
return 0;
}
int
WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
{
HWND hwnd = (HWND) data;
LPTSTR title;
int titleLen;
Oct 23, 2018
Oct 23, 2018
390
SDL_bool isstack;
391
392
393
/* Query the title from the existing window */
titleLen = GetWindowTextLength(hwnd);
Oct 23, 2018
Oct 23, 2018
394
title = SDL_small_alloc(TCHAR, titleLen + 1, &isstack);
Jun 26, 2019
Jun 26, 2019
396
titleLen = GetWindowText(hwnd, title, titleLen + 1);
397
398
399
400
401
402
403
} else {
titleLen = 0;
}
if (titleLen > 0) {
window->title = WIN_StringToUTF8(title);
}
if (title) {
Oct 23, 2018
Oct 23, 2018
404
SDL_small_free(title, isstack);
Jun 16, 2017
Jun 16, 2017
407
if (SetupWindowData(_this, window, hwnd, GetParent(hwnd), SDL_FALSE) < 0) {
408
409
410
411
412
413
414
415
416
417
418
419
420
421
return -1;
}
#if SDL_VIDEO_OPENGL_WGL
{
const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
if (hint) {
/* This hint is a pointer (in string form) of the address of
the window to share a pixel format with
*/
SDL_Window *otherWindow = NULL;
SDL_sscanf(hint, "%p", (void**)&otherWindow);
/* Do some error checking on the pointer */
Aug 26, 2018
Aug 26, 2018
422
if (otherWindow != NULL && otherWindow->magic == &_this->window_magic) {
423
/* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
Aug 26, 2018
Aug 26, 2018
424
if (otherWindow->flags & SDL_WINDOW_OPENGL) {
425
window->flags |= SDL_WINDOW_OPENGL;
Aug 26, 2018
Aug 26, 2018
426
if (!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
return -1;
}
}
}
}
}
#endif
return 0;
}
void
WIN_SetWindowTitle(_THIS, SDL_Window * window)
{
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
LPTSTR title = WIN_UTF8ToString(window->title);
SetWindowText(hwnd, title);
SDL_free(title);
}
void
WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
{
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
HICON hicon = NULL;
BYTE *icon_bmp;
Jan 15, 2018
Jan 15, 2018
452
int icon_len, mask_len, y;
Oct 23, 2018
Oct 23, 2018
454
SDL_bool isstack;
Jan 15, 2018
Jan 15, 2018
456
457
458
/* Create temporary buffer for ICONIMAGE structure */
mask_len = (icon->h * (icon->w + 7)/8);
icon_len = 40 + icon->h * icon->w * sizeof(Uint32) + mask_len;
Oct 23, 2018
Oct 23, 2018
459
icon_bmp = SDL_small_alloc(BYTE, icon_len, &isstack);
460
461
dst = SDL_RWFromMem(icon_bmp, icon_len);
if (!dst) {
Oct 23, 2018
Oct 23, 2018
462
SDL_small_free(icon_bmp, isstack);
463
464
465
466
467
468
469
470
471
472
return;
}
/* Write the BITMAPINFO header */
SDL_WriteLE32(dst, 40);
SDL_WriteLE32(dst, icon->w);
SDL_WriteLE32(dst, icon->h * 2);
SDL_WriteLE16(dst, 1);
SDL_WriteLE16(dst, 32);
SDL_WriteLE32(dst, BI_RGB);
Jan 18, 2017
Jan 18, 2017
473
SDL_WriteLE32(dst, icon->h * icon->w * sizeof(Uint32));
474
475
476
477
478
479
480
481
482
483
SDL_WriteLE32(dst, 0);
SDL_WriteLE32(dst, 0);
SDL_WriteLE32(dst, 0);
SDL_WriteLE32(dst, 0);
/* Write the pixels upside down into the bitmap buffer */
SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
y = icon->h;
while (y--) {
Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
Jan 18, 2017
Jan 18, 2017
484
SDL_RWwrite(dst, src, icon->w * sizeof(Uint32), 1);
Jan 15, 2018
Jan 15, 2018
487
488
489
/* Write the mask */
SDL_memset(icon_bmp + icon_len - mask_len, 0xFF, mask_len);
490
491
492
hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
SDL_RWclose(dst);
Oct 23, 2018
Oct 23, 2018
493
SDL_small_free(icon_bmp, isstack);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/* Set the icon for the window */
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
/* Set the icon in the task manager (should we do this?) */
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
}
void
WIN_SetWindowPosition(_THIS, SDL_Window * window)
{
WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
}
void
WIN_SetWindowSize(_THIS, SDL_Window * window)
{
WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
}
Jan 3, 2018
Jan 3, 2018
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
int
WIN_GetWindowBordersSize(_THIS, SDL_Window * window, int *top, int *left, int *bottom, int *right)
{
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
RECT rcClient, rcWindow;
POINT ptDiff;
/* rcClient stores the size of the inner window, while rcWindow stores the outer size relative to the top-left
* screen position; so the top/left values of rcClient are always {0,0} and bottom/right are {height,width} */
GetClientRect(hwnd, &rcClient);
GetWindowRect(hwnd, &rcWindow);
/* convert the top/left values to make them relative to
* the window; they will end up being slightly negative */
ptDiff.y = rcWindow.top;
ptDiff.x = rcWindow.left;
ScreenToClient(hwnd, &ptDiff);
rcWindow.top = ptDiff.y;
rcWindow.left = ptDiff.x;
/* convert the bottom/right values to make them relative to the window,
* these will be slightly bigger than the inner width/height */
ptDiff.y = rcWindow.bottom;
ptDiff.x = rcWindow.right;
ScreenToClient(hwnd, &ptDiff);
rcWindow.bottom = ptDiff.y;
rcWindow.right = ptDiff.x;
/* Now that both the inner and outer rects use the same coordinate system we can substract them to get the border size.
* Keep in mind that the top/left coordinates of rcWindow are negative because the border lies slightly before {0,0},
* so switch them around because SDL2 wants them in positive. */
*top = rcClient.top - rcWindow.top;
*left = rcClient.left - rcWindow.left;
*bottom = rcWindow.bottom - rcClient.bottom;
*right = rcWindow.right - rcClient.right;
return 0;
}
556
557
558
559
void
WIN_ShowWindow(_THIS, SDL_Window * window)
{
Aug 26, 2018
Aug 26, 2018
560
561
562
563
564
565
566
567
568
569
570
DWORD style;
HWND hwnd;
int nCmdShow;
hwnd = ((SDL_WindowData *)window->driverdata)->hwnd;
nCmdShow = SW_SHOW;
style = GetWindowLong(hwnd, GWL_EXSTYLE);
if (style & WS_EX_NOACTIVATE) {
nCmdShow = SW_SHOWNOACTIVATE;
}
ShowWindow(hwnd, nCmdShow);
571
572
573
574
575
576
577
578
579
580
581
582
}
void
WIN_HideWindow(_THIS, SDL_Window * window)
{
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
ShowWindow(hwnd, SW_HIDE);
}
void
WIN_RaiseWindow(_THIS, SDL_Window * window)
{
Oct 1, 2016
Oct 1, 2016
583
584
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
SetForegroundWindow(hwnd);
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
}
void
WIN_MaximizeWindow(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
data->expected_resize = SDL_TRUE;
ShowWindow(hwnd, SW_MAXIMIZE);
data->expected_resize = SDL_FALSE;
}
void
WIN_MinimizeWindow(_THIS, SDL_Window * window)
{
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
ShowWindow(hwnd, SW_MINIMIZE);
}
void
WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
Aug 12, 2017
Aug 12, 2017
609
DWORD style;
Aug 12, 2017
Aug 12, 2017
611
612
613
style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~STYLE_MASK;
style |= GetWindowStyle(window);
614
615
data->in_border_change = SDL_TRUE;
Oct 1, 2015
Oct 1, 2015
616
SetWindowLong(hwnd, GWL_STYLE, style);
617
618
619
620
WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE);
data->in_border_change = SDL_FALSE;
}
Sep 30, 2016
Sep 30, 2016
621
622
623
624
625
void
WIN_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
Aug 12, 2017
Aug 12, 2017
626
DWORD style;
Sep 30, 2016
Sep 30, 2016
627
Aug 12, 2017
Aug 12, 2017
628
629
630
style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~STYLE_MASK;
style |= GetWindowStyle(window);
Sep 30, 2016
Sep 30, 2016
631
632
633
634
SetWindowLong(hwnd, GWL_STYLE, style);
}
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
void
WIN_RestoreWindow(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
data->expected_resize = SDL_TRUE;
ShowWindow(hwnd, SW_RESTORE);
data->expected_resize = SDL_FALSE;
}
void
WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
HWND hwnd = data->hwnd;
SDL_Rect bounds;
DWORD style;
HWND top;
int x, y;
int w, h;
Apr 21, 2017
Apr 21, 2017
656
if (SDL_ShouldAllowTopmost() && ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS) || window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
top = HWND_TOPMOST;
} else {
top = HWND_NOTOPMOST;
}
style = GetWindowLong(hwnd, GWL_STYLE);
style &= ~STYLE_MASK;
style |= GetWindowStyle(window);
WIN_GetDisplayBounds(_this, display, &bounds);
if (fullscreen) {
x = bounds.x;
y = bounds.y;
w = bounds.w;
h = bounds.h;
Dec 30, 2015
Dec 30, 2015
673
674
675
676
677
678
679
680
/* Unset the maximized flag. This fixes
https://bugzilla.libsdl.org/show_bug.cgi?id=3215
*/
if (style & WS_MAXIMIZE) {
data->windowed_mode_was_maximized = SDL_TRUE;
style &= ~WS_MAXIMIZE;
}
Oct 7, 2017
Oct 7, 2017
682
683
BOOL menu;
Dec 30, 2015
Dec 30, 2015
684
685
686
687
688
689
690
691
692
693
/* Restore window-maximization state, as applicable.
Special care is taken to *not* do this if and when we're
alt-tab'ing away (to some other window; as indicated by
in_window_deactivation), otherwise
https://bugzilla.libsdl.org/show_bug.cgi?id=3215 can reproduce!
*/
if (data->windowed_mode_was_maximized && !data->in_window_deactivation) {
style |= WS_MAXIMIZE;
data->windowed_mode_was_maximized = SDL_FALSE;
}
Sep 6, 2017
Sep 6, 2017
694
Oct 7, 2017
Oct 7, 2017
695
696
menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
WIN_AdjustWindowRectWithStyle(window, style, menu, &x, &y, &w, &h, SDL_FALSE);
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
}
SetWindowLong(hwnd, GWL_STYLE, style);
data->expected_resize = SDL_TRUE;
SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
data->expected_resize = SDL_FALSE;
}
int
WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
HDC hdc;
BOOL succeeded = FALSE;
hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
if (hdc) {
succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
if (!succeeded) {
WIN_SetError("SetDeviceGammaRamp()");
}
DeleteDC(hdc);
}
return succeeded ? 0 : -1;
}
int
WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
{
SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
HDC hdc;
BOOL succeeded = FALSE;
hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
if (hdc) {
succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
if (!succeeded) {
WIN_SetError("GetDeviceGammaRamp()");
}
DeleteDC(hdc);
}
return succeeded ? 0 : -1;
}
void
WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
WIN_UpdateClipCursor(window);
if (window->flags & SDL_WINDOW_FULLSCREEN) {
UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
if (!(window->flags & SDL_WINDOW_SHOWN)) {
flags |= SWP_NOACTIVATE;
}
WIN_SetWindowPositionInternal(_this, window, flags);
}
}
void
WIN_DestroyWindow(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
if (data) {
ReleaseDC(data->hwnd, data->hdc);
RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
if (data->created) {
DestroyWindow(data->hwnd);
Jun 16, 2017
Jun 16, 2017
767
768
769
if (data->parent) {
DestroyWindow(data->parent);
}
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
} else {
/* Restore any original event handler... */
if (data->wndproc != NULL) {
#ifdef GWLP_WNDPROC
SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
(LONG_PTR) data->wndproc);
#else
SetWindowLong(data->hwnd, GWL_WNDPROC,
(LONG_PTR) data->wndproc);
#endif
}
}
SDL_free(data);
}
window->driverdata = NULL;
}
SDL_bool
WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
{
const SDL_WindowData *data = (const SDL_WindowData *) window->driverdata;
if (info->version.major <= SDL_MAJOR_VERSION) {
Nov 17, 2016
Nov 17, 2016
792
793
int versionnum = SDL_VERSIONNUM(info->version.major, info->version.minor, info->version.patch);
794
795
info->subsystem = SDL_SYSWM_WINDOWS;
info->info.win.window = data->hwnd;
Nov 17, 2016
Nov 17, 2016
796
797
798
799
800
if (versionnum >= SDL_VERSIONNUM(2, 0, 4)) {
info->info.win.hdc = data->hdc;
}
Nov 21, 2016
Nov 21, 2016
801
802
803
804
if (versionnum >= SDL_VERSIONNUM(2, 0, 5)) {
info->info.win.hinstance = data->hinstance;
}
805
806
return SDL_TRUE;
} else {
Mar 26, 2017
Mar 26, 2017
807
SDL_SetError("Application not compiled with SDL %d.%d",
808
809
810
811
812
SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
return SDL_FALSE;
}
}
Mar 16, 2020
Mar 16, 2020
813
814
815
816
817
818
819
820
821
static LRESULT CALLBACK SDL_HelperWindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
#if SDL_JOYSTICK_RAWINPUT
if (RAWINPUT_WindowProc(hWnd, msg, wParam, lParam) == 0) {
return 0;
}
#endif
return DefWindowProc(hWnd, msg, wParam, lParam);
}
Mar 16, 2020
Mar 16, 2020
824
* Creates a HelperWindow used for DirectInput and RawInput events.
825
826
827
828
829
830
831
832
833
834
835
836
837
838
*/
int
SDL_HelperWindowCreate(void)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASS wce;
/* Make sure window isn't created twice. */
if (SDL_HelperWindow != NULL) {
return 0;
}
/* Create the class. */
SDL_zero(wce);
Mar 16, 2020
Mar 16, 2020
839
wce.lpfnWndProc = SDL_GetHintBoolean(SDL_HINT_JOYSTICK_RAWINPUT, SDL_TRUE) ? SDL_HelperWindowProc : DefWindowProc;
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
wce.hInstance = hInstance;
/* Register the class. */
SDL_HelperWindowClass = RegisterClass(&wce);
if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
return WIN_SetError("Unable to create Helper Window Class");
}
/* Create the window. */
SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
SDL_HelperWindowName,
WS_OVERLAPPED, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, HWND_MESSAGE, NULL,
hInstance, NULL);
if (SDL_HelperWindow == NULL) {
UnregisterClass(SDL_HelperWindowClassName, hInstance);
return WIN_SetError("Unable to create Helper Window");
}
return 0;
}
/*
* Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
*/
void
SDL_HelperWindowDestroy(void)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
/* Destroy the window. */
if (SDL_HelperWindow != NULL) {
if (DestroyWindow(SDL_HelperWindow) == 0) {
WIN_SetError("Unable to destroy Helper Window");
return;
}
SDL_HelperWindow = NULL;
}
/* Unregister the class. */
if (SDL_HelperWindowClass != 0) {
if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
WIN_SetError("Unable to destroy Helper Window Class");
return;
}
SDL_HelperWindowClass = 0;
}
}
void WIN_OnWindowEnter(_THIS, SDL_Window * window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
if (!data || !data->hwnd) {
/* The window wasn't fully initialized */
return;
}
Apr 21, 2017
Apr 21, 2017
901
902
903
if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
}
Apr 21, 2017
Apr 21, 2017
905
906
907
908
909
910
911
912
913
914
#ifdef WM_MOUSELEAVE
{
TRACKMOUSEEVENT trackMouseEvent;
trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
trackMouseEvent.dwFlags = TME_LEAVE;
trackMouseEvent.hwndTrack = data->hwnd;
TrackMouseEvent(&trackMouseEvent);
}
915
916
917
918
919
920
921
922
#endif /* WM_MOUSELEAVE */
}
void
WIN_UpdateClipCursor(SDL_Window *window)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
SDL_Mouse *mouse = SDL_GetMouse();
Feb 12, 2020
Feb 12, 2020
923
RECT rect, clipped_rect;
Sep 26, 2018
Sep 26, 2018
925
926
927
928
929
if (data->in_title_click || data->focus_click_pending) {
return;
}
if (data->skip_update_clipcursor) {
data->skip_update_clipcursor = SDL_FALSE;
Feb 12, 2020
Feb 12, 2020
932
933
934
if (!GetClipCursor(&clipped_rect)) {
return;
}
935
936
937
938
if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
(window->flags & SDL_WINDOW_INPUT_FOCUS)) {
if (mouse->relative_mode && !mouse->relative_mode_warp) {
Feb 12, 2020
Feb 12, 2020
939
940
if (GetWindowRect(data->hwnd, &rect)) {
LONG cx, cy;
Feb 12, 2020
Feb 12, 2020
942
943
cx = (rect.left + rect.right) / 2;
cy = (rect.top + rect.bottom) / 2;
Feb 12, 2020
Feb 12, 2020
945
946
947
948
949
/* Make an absurdly small clip rect */
rect.left = cx - 1;
rect.right = cx + 1;
rect.top = cy - 1;
rect.bottom = cy + 1;
Feb 12, 2020
Feb 12, 2020
951
952
953
954
955
if (SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
if (ClipCursor(&rect)) {
data->cursor_clipped_rect = rect;
}
}
Aug 26, 2018
Aug 26, 2018
956
}
957
958
959
960
} else {
if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
ClientToScreen(data->hwnd, (LPPOINT) & rect);
ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
Feb 12, 2020
Feb 12, 2020
961
if (SDL_memcmp(&rect, &clipped_rect, sizeof(rect)) != 0) {
Feb 11, 2020
Feb 11, 2020
962
963
964
if (ClipCursor(&rect)) {
data->cursor_clipped_rect = rect;
}
Aug 26, 2018
Aug 26, 2018
965
}
Feb 12, 2020
Feb 12, 2020
968
} else if (SDL_memcmp(&clipped_rect, &data->cursor_clipped_rect, sizeof(clipped_rect)) == 0) {
Aug 26, 2018
Aug 26, 2018
970
SDL_zero(data->cursor_clipped_rect);
971
972
973
974
975
976
977
978
979
}
}
int
WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
{
return 0; /* just succeed, the real work is done elsewhere. */
}
Jan 5, 2016
Jan 5, 2016
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
int
WIN_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
{
const SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
const HWND hwnd = data->hwnd;
const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
SDL_assert(style != 0);
if (opacity == 1.0f) {
/* want it fully opaque, just mark it unlayered if necessary. */
if (style & WS_EX_LAYERED) {
if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
return WIN_SetError("SetWindowLong()");
}
}
} else {
const BYTE alpha = (BYTE) ((int) (opacity * 255.0f));
/* want it transparent, mark it layered if necessary. */
if ((style & WS_EX_LAYERED) == 0) {
if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {