Debian Codegraph @

src/win/wwindow.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * New Windows window handling
12 *
13 * By Trent Gamblin.
14 *
15 */
16
17 /* Raw input */
18 #define _WIN32_WINNT 0x0501
19 #ifndef WINVER
20 #define WINVER 0x0501
21 #endif
22
23 #include <windows.h>
24 #include <windowsx.h>
25 #include <winuser.h>
26 #include <dinput.h>
27 #include <dbt.h>
28
29 /* Only used for Vista and up. */
30 #ifndef WM_MOUSEHWHEEL
31 #define WM_MOUSEHWHEEL 0x020E
32 #endif
33
34 #include <allegro5/allegro.h>
35 #include <process.h>
36
37 #include "allegro5/allegro_windows.h"
38 #include "allegro5/internal/aintern.h"
39 #include "allegro5/internal/aintern_bitmap.h"
40 #include "allegro5/internal/aintern_vector.h"
41 #include "allegro5/internal/aintern_display.h"
42 #include "allegro5/internal/aintern_wunicode.h"
43 #include "allegro5/internal/aintern_joystick.h"
44 #include "allegro5/internal/aintern_wjoydxnu.h"
45 #include "allegro5/platform/aintwin.h"
46
47
48 ALLEGRO_DEBUG_CHANNEL("wwindow")
49
50 static WNDCLASS window_class;
51
52 static _AL_VECTOR resizing_displays;
53 static ALLEGRO_MUTEX *resize_event_thread_mutex;
54 static bool end_resize_event_thread;
55 static bool resize_event_thread_ended;
56
57
58 UINT _al_win_msg_call_proc = 0;
59 UINT _al_win_msg_suicide = 0;
60
61
62 #ifndef WM_DPICHANGED
63 #define WM_DPICHANGED 0x02E0
64 #endif
65
66
67 static void display_flags_to_window_styles(int flags,
68 DWORD *style, DWORD *ex_style)
69 {
70 if (flags & ALLEGRO_FULLSCREEN) {
71 *style = WS_POPUP;
72 *ex_style = WS_EX_APPWINDOW;
73 }
74 else if (flags & ALLEGRO_MAXIMIZED) {
75 *style = WS_OVERLAPPEDWINDOW;
76 *ex_style = WS_EX_APPWINDOW;
77 }
78 else if (flags & ALLEGRO_RESIZABLE) {
79 *style = WS_OVERLAPPEDWINDOW;
80 *ex_style = WS_EX_APPWINDOW | WS_EX_OVERLAPPEDWINDOW;
81 }
82 else {
83 *style = WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
84 *ex_style = WS_EX_APPWINDOW;
85 }
86 }
87
88 // Clears a window to black
89 static void clear_window(HWND window, int w, int h)
90 {
91 PAINTSTRUCT ps;
92 HDC hdc;
93
94 hdc = BeginPaint(window, &ps);
95
96 SelectObject(hdc, GetStockObject(DC_BRUSH));
97 SetDCBrushColor(hdc, RGB(0, 0, 0));
98
99 Rectangle(hdc, 0, 0, w, h);
100 }
101
102 HWND _al_win_create_hidden_window()
103 {
104 HWND window = CreateWindowEx(0,
105 TEXT("ALEX"), TEXT("hidden"), WS_POPUP,
106 -5000, -5000, 0, 0,
107 NULL,NULL,window_class.hInstance,0);
108 return window;
109 }
110
111 static void _al_win_get_window_center(
112 ALLEGRO_DISPLAY_WIN *win_display, int width, int height, int *out_x, int *out_y)
113 {
114 int a = win_display->adapter;
115 bool *is_fullscreen;
116 ALLEGRO_MONITOR_INFO info;
117 RECT win_size;
118
119 ALLEGRO_SYSTEM *sys = al_get_system_driver();
120 unsigned int num;
121 unsigned int i;
122 unsigned int fullscreen_found = 0;
123 num = al_get_num_video_adapters();
124 is_fullscreen = al_calloc(num, sizeof(bool));
125 for (i = 0; i < sys->displays._size; i++) {
126 ALLEGRO_DISPLAY **dptr = _al_vector_ref(&sys->displays, i);
127 ALLEGRO_DISPLAY *d = *dptr;
128 if (d->flags & ALLEGRO_FULLSCREEN) {
129 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)d;
130 is_fullscreen[win_display->adapter] = true;
131 fullscreen_found++;
132 }
133 }
134 if (fullscreen_found && fullscreen_found < num) {
135 for (i = 0; i < num; i++) {
136 if (is_fullscreen[i] == false) {
137 a = i;
138 break;
139 }
140 }
141 }
142 al_free(is_fullscreen);
143
144 al_get_monitor_info(a, &info);
145
146 win_size.left = info.x1 + (info.x2 - info.x1 - width) / 2;
147 win_size.top = info.y1 + (info.y2 - info.y1 - height) / 2;
148
149 *out_x = win_size.left;
150 *out_y = win_size.top;
151 }
152
153 HWND _al_win_create_window(ALLEGRO_DISPLAY *display, int width, int height, int flags)
154 {
155 HWND my_window;
156 DWORD style;
157 DWORD ex_style;
158 DEV_BROADCAST_DEVICEINTERFACE notification_filter;
159 int pos_x, pos_y;
160 bool center = false;
161 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
162 WINDOWINFO wi;
163 int lsize, rsize, tsize, bsize; // left, right, top, bottom border sizes
164 TCHAR* window_title;
165
166 wi.cbSize = sizeof(WINDOWINFO);
167
168 display_flags_to_window_styles(flags, &style, &ex_style);
169
170 al_get_new_window_position(&pos_x, &pos_y);
171 if ((flags & ALLEGRO_FULLSCREEN) || (flags & ALLEGRO_FULLSCREEN_WINDOW)) {
172 pos_x = pos_y = 0;
173 }
174 else if (pos_x == INT_MAX) {
175 pos_x = pos_y = 0;
176 center = true;
177 }
178
179 if (center) {
180 _al_win_get_window_center(win_display, width, height, &pos_x, &pos_y);
181 }
182
183 window_title = _twin_utf8_to_tchar(al_get_new_window_title());
184 my_window = CreateWindowEx(ex_style,
185 TEXT("ALEX"), window_title, style,
186 pos_x, pos_y, width, height,
187 NULL,NULL,window_class.hInstance,0);
188 al_free(window_title);
189
190 if (_al_win_register_touch_window)
191 _al_win_register_touch_window(my_window, 0);
192
193 ZeroMemory(&notification_filter, sizeof(notification_filter));
194 notification_filter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
195 notification_filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
196 RegisterDeviceNotification(my_window, &notification_filter, DEVICE_NOTIFY_WINDOW_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
197
198 GetWindowInfo(my_window, &wi);
199
200 lsize = (wi.rcClient.left - wi.rcWindow.left);
201 tsize = (wi.rcClient.top - wi.rcWindow.top);
202 rsize = (wi.rcWindow.right - wi.rcClient.right);
203 bsize = (wi.rcWindow.bottom - wi.rcClient.bottom);
204
205 SetWindowPos(my_window, 0, 0, 0,
206 width+lsize+rsize,
207 height+tsize+bsize,
208 SWP_NOZORDER | SWP_NOMOVE);
209 SetWindowPos(my_window, 0, pos_x-lsize, pos_y-tsize,
210 0, 0,
211 SWP_NOZORDER | SWP_NOSIZE);
212
213
214 if (flags & ALLEGRO_FRAMELESS) {
215 SetWindowLong(my_window, GWL_STYLE, WS_VISIBLE);
216 SetWindowLong(my_window, GWL_EXSTYLE, WS_EX_APPWINDOW);
217 SetWindowPos(my_window, 0, pos_x, pos_y, width, height, SWP_NOZORDER | SWP_FRAMECHANGED);
218 }
219
220 ShowWindow(my_window, SW_SHOW);
221
222 clear_window(my_window, width, height);
223
224 if (!(flags & ALLEGRO_RESIZABLE) && !(flags & ALLEGRO_FULLSCREEN)) {
225 /* Make the window non-resizable */
226 HMENU menu;
227 menu = GetSystemMenu(my_window, false);
228 DeleteMenu(menu, SC_SIZE, MF_BYCOMMAND);
229 DeleteMenu(menu, SC_MAXIMIZE, MF_BYCOMMAND);
230 DrawMenuBar(my_window);
231 }
232
233 _al_vector_init(&win_display->msg_callbacks, sizeof(ALLEGRO_DISPLAY_WIN_CALLBACK));
234
235 return my_window;
236 }
237
238
239 HWND _al_win_create_faux_fullscreen_window(LPCTSTR devname, ALLEGRO_DISPLAY *display,
240 int x1, int y1, int width, int height, int refresh_rate, int flags)
241 {
242 HWND my_window;
243 DWORD style;
244 DWORD ex_style;
245 DEVMODE mode;
246 LONG temp;
247 TCHAR* window_title;
248
249 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
250 (void)flags;
251
252 _al_vector_init(&win_display->msg_callbacks, sizeof(ALLEGRO_DISPLAY_WIN_CALLBACK));
253
254 style = WS_VISIBLE;
255 ex_style = 0;
256
257 window_title = _twin_utf8_to_tchar(al_get_new_window_title());
258 my_window = CreateWindowEx(ex_style,
259 TEXT("ALEX"), window_title, style,
260 x1, y1, width, height,
261 NULL,NULL,window_class.hInstance,0);
262 al_free(window_title);
263
264 if (_al_win_register_touch_window)
265 _al_win_register_touch_window(my_window, 0);
266
267 temp = GetWindowLong(my_window, GWL_STYLE);
268 temp &= ~WS_CAPTION;
269 SetWindowLong(my_window, GWL_STYLE, temp);
270 SetWindowPos(my_window, HWND_TOP, 0, 0, width, height, SWP_NOMOVE | SWP_FRAMECHANGED);
271
272 /* Go fullscreen */
273 memset(&mode, 0, sizeof(DEVMODE));
274 mode.dmSize = sizeof(DEVMODE);
275 mode.dmDriverExtra = 0;
276 mode.dmBitsPerPel = al_get_new_display_option(ALLEGRO_COLOR_SIZE, NULL);
277 mode.dmPelsWidth = width;
278 mode.dmPelsHeight = height;
279 mode.dmDisplayFlags = 0;
280 mode.dmDisplayFrequency = refresh_rate;
281 mode.dmPosition.x = x1;
282 mode.dmPosition.y = y1;
283 mode.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFLAGS|
284 DM_DISPLAYFREQUENCY|DM_POSITION;
285
286 ChangeDisplaySettingsEx(devname, &mode, NULL, 0, NULL/*CDS_FULLSCREEN*/);
287
288 clear_window(my_window, width, height);
289
290 return my_window;
291 }
292
293
294 /* _al_win_grab_input:
295 * Makes the passed display grab the input. All consequent input events will be
296 * generated the this display's window. The display's window must the the
297 * foreground window.
298 */
299 void _al_win_grab_input(ALLEGRO_DISPLAY_WIN *win_disp)
300 {
301 _al_win_wnd_schedule_proc(win_disp->window,
302 _al_win_joystick_dinput_grab,
303 win_disp);
304 }
305
306 /* Generate a resize event if the size has changed. We cannot asynchronously
307 * change the display size here yet, since the user will only know about a
308 * changed size after receiving the resize event. Here we merely add the
309 * event to the queue.
310 */
311 static void win_generate_resize_event(ALLEGRO_DISPLAY_WIN *win_display)
312 {
313 ALLEGRO_DISPLAY *display = (ALLEGRO_DISPLAY *)win_display;
314 ALLEGRO_EVENT_SOURCE *es = &display->es;
315 WINDOWINFO wi;
316 int x, y, w, h;
317
318 if (win_display->ignore_resize || win_display->d3d_ignore_resize) {
319 return;
320 }
321
322 wi.cbSize = sizeof(WINDOWINFO);
323 GetWindowInfo(win_display->window, &wi);
324 x = wi.rcClient.left;
325 y = wi.rcClient.top;
326 w = wi.rcClient.right - wi.rcClient.left;
327 h = wi.rcClient.bottom - wi.rcClient.top;
328
329 /* Don't generate events when restoring after minimise. */
330 if (w == 0 && h == 0 && x == -32000 && y == -32000)
331 return;
332
333 /* Always generate resize event when constraints are used.
334 * This is needed because d3d_acknowledge_resize() updates d->w, d->h
335 * before this function will be called.
336 */
337 if (display->use_constraints || display->w != w || display->h != h) {
338 _al_event_source_lock(es);
339 if (_al_event_source_needs_to_generate_event(es)) {
340 ALLEGRO_EVENT event;
341 event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE;
342 event.display.timestamp = al_get_time();
343 event.display.x = x;
344 event.display.y = y;
345 event.display.width = w;
346 event.display.height = h;
347 event.display.source = display;
348 _al_event_source_emit_event(es, &event);
349
350 /* Generate an expose event. */
351 /* This seems a bit redundant after a resize. */
352 if (win_display->display.flags & ALLEGRO_GENERATE_EXPOSE_EVENTS) {
353 event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE;
354 _al_event_source_emit_event(es, &event);
355 }
356 }
357 _al_event_source_unlock(es);
358 }
359 }
360
361 static void handle_mouse_capture(bool down, HWND hWnd)
362 {
363 int i;
364 bool any_button_down = false;
365 ALLEGRO_MOUSE_STATE state;
366
367 if (!al_is_mouse_installed())
368 return;
369
370 al_get_mouse_state(&state);
371 for (i = 1; i <= 5; i++) {
372 any_button_down |= al_mouse_button_down(&state, i);
373 }
374
375 if (down && GetCapture() != hWnd) {
376 SetCapture(hWnd);
377 }
378 else if (!any_button_down) {
379 ReleaseCapture();
380 }
381 }
382
383 static void break_window_message_pump(ALLEGRO_DISPLAY_WIN *win_display, HWND hWnd)
384 {
385 /* Get the ID of the thread which created the HWND and is processing its messages */
386 DWORD wnd_thread_id = GetWindowThreadProcessId(hWnd, NULL);
387
388 /* Set the "end_thread" flag to stop the message pump */
389 win_display->end_thread = true;
390
391 /* Wake-up the message pump so the thread can read the new value of "end_thread" */
392 PostThreadMessage(wnd_thread_id, WM_NULL, 0, 0);
393 }
394
395 /* Windows Touch Input emulate WM_MOUSE* events. If we are using touch input explicitly,
396 * we do not want to use this, because Allegro driver provide emulation already. This
397 * way we can be consistent across platforms.
398 */
399 static bool accept_mouse_event(void)
400 {
401 if (!al_is_touch_input_installed())
402 return true;
403
404 return !((GetMessageExtraInfo() & _AL_MOUSEEVENTF_FROMTOUCH) == _AL_MOUSEEVENTF_FROMTOUCH);
405 }
406
407 /* We want to rate-limit the ALLEGRO_EVENT_DISPLAY_RESIZE events while
408 * live-resizing via mouse. To this end, we detect start/end of this
409 * via WM_ENTERSIZEMOVE/WM_EXITSIZEMOVE and then enable this code to
410 * periodically send those events.
411 */
412 static void resize_event_thread_proc(void *arg)
413 {
414 (void)arg;
415 while (true) {
416 al_lock_mutex(resize_event_thread_mutex);
417 if (end_resize_event_thread) {
418 al_unlock_mutex(resize_event_thread_mutex);
419 break;
420 }
421
422 for (int j = 0; j < (int)_al_vector_size(&resizing_displays); j++) {
423 ALLEGRO_DISPLAY_WIN **win_display = (ALLEGRO_DISPLAY_WIN **)_al_vector_ref(&resizing_displays, j);
424 win_generate_resize_event(*win_display);
425 }
426
427 al_unlock_mutex(resize_event_thread_mutex);
428 Sleep(50);
429 }
430 resize_event_thread_ended = true;
431 }
432
433 static LRESULT CALLBACK window_callback(HWND hWnd, UINT message,
434 WPARAM wParam, LPARAM lParam)
435 {
436 ALLEGRO_DISPLAY *d = NULL;
437 ALLEGRO_DISPLAY_WIN *win_display = NULL;
438 //WINDOWINFO wi;
439 unsigned int i;
440 ALLEGRO_EVENT_SOURCE *es = NULL;
441 ALLEGRO_SYSTEM *system = al_get_system_driver();
442
443 //wi.cbSize = sizeof(WINDOWINFO);
444
445 if (message == _al_win_msg_call_proc) {
446 ((void (*)(void*))wParam) ((void*)lParam);
447 return 0;
448 }
449
450 if (!system) {
451 return DefWindowProc(hWnd,message,wParam,lParam);
452 }
453
454 if (message == _al_win_msg_suicide && wParam) {
455 win_display = (ALLEGRO_DISPLAY_WIN*)wParam;
456 break_window_message_pump(win_display, hWnd);
457 if (_al_win_unregister_touch_window)
458 _al_win_unregister_touch_window(hWnd);
459 al_lock_mutex(resize_event_thread_mutex);
460 _al_vector_find_and_delete(&resizing_displays, &win_display);
461 al_unlock_mutex(resize_event_thread_mutex);
462 DestroyWindow(hWnd);
463 return 0;
464 }
465
466 for (i = 0; i < system->displays._size; i++) {
467 ALLEGRO_DISPLAY **dptr = _al_vector_ref(&system->displays, i);
468 d = *dptr;
469 win_display = (void*)d;
470 if (win_display->window == hWnd) {
471 es = &d->es;
472 break;
473 }
474 }
475
476 if (i == system->displays._size)
477 return DefWindowProc(hWnd,message,wParam,lParam);
478
479 if (message == _al_win_msg_suicide) {
480 break_window_message_pump(win_display, hWnd);
481 if (_al_win_unregister_touch_window)
482 _al_win_unregister_touch_window(hWnd);
483 al_lock_mutex(resize_event_thread_mutex);
484 _al_vector_find_and_delete(&resizing_displays, &win_display);
485 al_unlock_mutex(resize_event_thread_mutex);
486 DestroyWindow(hWnd);
487 return 0;
488 }
489
490 for (i = 0; i < _al_vector_size(&win_display->msg_callbacks); ++i) {
491 LRESULT result = TRUE;
492 ALLEGRO_DISPLAY_WIN_CALLBACK *ptr = _al_vector_ref(&win_display->msg_callbacks, i);
493 if ((ptr->proc)(d, message, wParam, lParam, &result, ptr->userdata))
494 return result;
495 }
496
497 switch (message) {
498 case WM_INPUT:
499 {
500 /* RAW Input is currently unused. */
501 UINT dwSize;
502 LPBYTE lpb;
503 RAWINPUT* raw;
504
505 /* We can't uninstall WM_INPUT mesages. */
506 if (!al_is_mouse_installed())
507 break;
508
509 GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize,
510 sizeof(RAWINPUTHEADER));
511 lpb = al_malloc(sizeof(BYTE)*dwSize);
512 if (lpb == NULL)
513 break;
514
515 GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER));
516 raw = (RAWINPUT*)lpb;
517
518 if (raw->header.dwType != RIM_TYPEMOUSE) {
519 al_free(lpb);
520 break;
521 }
522
523 {
524 RAWMOUSE *rm = &raw->data.mouse;
525 int x = raw->data.mouse.lLastX;
526 int y = raw->data.mouse.lLastY;
527 bool abs = (rm->usFlags & (MOUSE_MOVE_ABSOLUTE
528 | MOUSE_VIRTUAL_DESKTOP)) != 0;
529 if (abs || x || y)
530 _al_win_mouse_handle_move(x, y, abs, win_display);
531
532 if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_DOWN)
533 _al_win_mouse_handle_button(1, true, x, y, abs, win_display);
534 if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_UP)
535 _al_win_mouse_handle_button(1, false, x, y, abs, win_display);
536 if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_DOWN)
537 _al_win_mouse_handle_button(2, true, x, y, abs, win_display);
538 if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_UP)
539 _al_win_mouse_handle_button(2, false, x, y, abs, win_display);
540 if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_DOWN)
541 _al_win_mouse_handle_button(3, true, x, y, abs, win_display);
542 if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_UP)
543 _al_win_mouse_handle_button(3, false, x, y, abs, win_display);
544 if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_DOWN)
545 _al_win_mouse_handle_button(4, true, x, y, abs, win_display);
546 if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_UP)
547 _al_win_mouse_handle_button(4, false, x, y, abs, win_display);
548 if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_DOWN)
549 _al_win_mouse_handle_button(5, true, x, y, abs, win_display);
550 if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_UP)
551 _al_win_mouse_handle_button(5, false, x, y, abs, win_display);
552
553 if (rm->usButtonFlags & RI_MOUSE_WHEEL) {
554 SHORT z = (SHORT)rm->usButtonData;
555 _al_win_mouse_handle_wheel(z, false, win_display);
556 }
557 }
558
559 al_free(lpb);
560 break;
561 }
562 case WM_LBUTTONDOWN:
563 case WM_LBUTTONUP: {
564 if (accept_mouse_event()) {
565 int mx = GET_X_LPARAM(lParam);
566 int my = GET_Y_LPARAM(lParam);
567 bool down = (message == WM_LBUTTONDOWN);
568 _al_win_mouse_handle_button(1, down, mx, my, true, win_display);
569 handle_mouse_capture(down, hWnd);
570 }
571 break;
572 }
573 case WM_MBUTTONDOWN:
574 case WM_MBUTTONUP: {
575 if (accept_mouse_event()) {
576 int mx = GET_X_LPARAM(lParam);
577 int my = GET_Y_LPARAM(lParam);
578 bool down = (message == WM_MBUTTONDOWN);
579 _al_win_mouse_handle_button(3, down, mx, my, true, win_display);
580 handle_mouse_capture(down, hWnd);
581 }
582 break;
583 }
584 case WM_RBUTTONDOWN:
585 case WM_RBUTTONUP: {
586 if (accept_mouse_event()) {
587 int mx = GET_X_LPARAM(lParam);
588 int my = GET_Y_LPARAM(lParam);
589 bool down = (message == WM_RBUTTONDOWN);
590 _al_win_mouse_handle_button(2, down, mx, my, true, win_display);
591 handle_mouse_capture(down, hWnd);
592 }
593 break;
594 }
595 case WM_XBUTTONDOWN:
596 case WM_XBUTTONUP: {
597 if (accept_mouse_event()) {
598 int mx = GET_X_LPARAM(lParam);
599 int my = GET_Y_LPARAM(lParam);
600 int button = HIWORD(wParam);
601 bool down = (message == WM_XBUTTONDOWN);
602 if (button == XBUTTON1)
603 _al_win_mouse_handle_button(4, down, mx, my, true, win_display);
604 else if (button == XBUTTON2)
605 _al_win_mouse_handle_button(5, down, mx, my, true, win_display);
606 handle_mouse_capture(down, hWnd);
607 return TRUE;
608 }
609 break;
610 }
611 case WM_MOUSEWHEEL: {
612 if (accept_mouse_event()) {
613 int d = GET_WHEEL_DELTA_WPARAM(wParam);
614 _al_win_mouse_handle_wheel(d, false, win_display);
615 return TRUE;
616 }
617 break;
618 }
619 case WM_MOUSEHWHEEL: {
620 if (accept_mouse_event()) {
621 int d = GET_WHEEL_DELTA_WPARAM(wParam);
622 _al_win_mouse_handle_hwheel(d, false, win_display);
623 return TRUE;
624 }
625 break;
626 }
627 case WM_MOUSEMOVE: {
628 if (accept_mouse_event()) {
629 TRACKMOUSEEVENT tme;
630 int mx = GET_X_LPARAM(lParam);
631 int my = GET_Y_LPARAM(lParam);
632
633 if (win_display->mouse_cursor_shown && win_display->hide_mouse_on_move) {
634 win_display->hide_mouse_on_move = false;
635 win_display->display.vt->hide_mouse_cursor((void*)win_display);
636 }
637
638 _al_win_mouse_handle_move(mx, my, true, win_display);
639 if (mx >= 0 && my >= 0 && mx < d->w && my < d->h) {
640 tme.cbSize = sizeof(tme);
641 tme.dwFlags = TME_QUERY;
642 if (TrackMouseEvent(&tme) && !tme.hwndTrack) {
643 tme.dwFlags = TME_LEAVE;
644 tme.hwndTrack = hWnd;
645 tme.dwHoverTime = 0;
646 TrackMouseEvent(&tme);
647 _al_win_mouse_handle_enter(win_display);
648 }
649 }
650 }
651
652 /* WM_SETCURSOR messages are not received while the mouse is
653 * captured. We call SetCursor here so that changing the mouse
654 * cursor has an effect while the user is holding down the mouse
655 * button.
656 */
657 if (GetCapture() == hWnd && win_display->mouse_cursor_shown) {
658 SetCursor(win_display->mouse_selected_hcursor);
659 }
660
661 break;
662 }
663 case WM_MOUSELEAVE: {
664 if (accept_mouse_event()) {
665 _al_win_mouse_handle_leave(win_display);
666 }
667 break;
668 }
669 case _AL_WM_TOUCH: {
670 if (_al_win_get_touch_input_info && _al_win_close_touch_input_handle) {
671 int number_of_touches = LOWORD(wParam);
672
673 TOUCHINPUT* touches = al_malloc(number_of_touches * sizeof(TOUCHINPUT));
674
675 if (_al_win_get_touch_input_info((HANDLE)lParam, number_of_touches, touches, sizeof(TOUCHINPUT))) {
676
677 if (al_is_touch_input_installed()) {
678
679 int i;
680
681 POINT origin = { 0, 0 };
682
683 ClientToScreen(hWnd, &origin);
684
685 _al_win_touch_input_set_time_stamp((touches + number_of_touches - 1)->dwTime);
686
687 for (i = 0; i < number_of_touches; ++i) {
688
689 TOUCHINPUT* touch = touches + i;
690
691 float x = touch->x / 100.0f - (float)origin.x;
692 float y = touch->y / 100.0f - (float)origin.y;
693
694 bool primary = touch->dwFlags & _AL_TOUCHEVENTF_PRIMARY ? true : false;
695
696 if (touch->dwFlags & _AL_TOUCHEVENTF_DOWN)
697 _al_win_touch_input_handle_begin((int)touch->dwID, (size_t)touch->dwTime, x, y, primary, win_display);
698 else if (touch->dwFlags & _AL_TOUCHEVENTF_UP)
699 _al_win_touch_input_handle_end((int)touch->dwID, (size_t)touch->dwTime, x, y, primary, win_display);
700 else if (touch->dwFlags & _AL_TOUCHEVENTF_MOVE)
701 _al_win_touch_input_handle_move((int)touch->dwID, (size_t)touch->dwTime, x, y, primary, win_display);
702 }
703 }
704
705 _al_win_close_touch_input_handle((HANDLE)lParam);
706 }
707
708 al_free(touches);
709 }
710 break;
711 }
712 case WM_CAPTURECHANGED: {
713 if (al_is_mouse_installed()) {
714 int i;
715 ALLEGRO_MOUSE_STATE state;
716 if (!lParam || (HWND)lParam == hWnd)
717 break;
718 al_get_mouse_state(&state);
719 for (i = 1; i <= 5; i++) {
720 if (al_mouse_button_down(&state, i))
721 _al_win_mouse_handle_button(i, 0, 0, 0, true, win_display);
722 }
723 }
724 break;
725 }
726 case WM_NCMOUSEMOVE: {
727 if (!win_display->mouse_cursor_shown) {
728 win_display->display.vt->show_mouse_cursor((void*)win_display);
729 win_display->hide_mouse_on_move = true;
730 }
731 break;
732 }
733 case WM_SYSKEYDOWN: {
734 int vcode = wParam;
735 int scode = (lParam >> 16) & 0xff;
736 bool extended = (lParam >> 24) & 0x1;
737 bool repeated = (lParam >> 30) & 0x1;
738 _al_win_kbd_handle_key_press(scode, vcode, extended, repeated, win_display);
739 break;
740 }
741 case WM_KEYDOWN: {
742 int vcode = wParam;
743 int scode = (lParam >> 16) & 0xff;
744 bool extended = (lParam >> 24) & 0x1;
745 bool repeated = (lParam >> 30) & 0x1;
746 /* We can't use TranslateMessage() because we don't know if it will
747 produce a WM_CHAR or not. */
748 _al_win_kbd_handle_key_press(scode, vcode, extended, repeated, win_display);
749 break;
750 }
751 case WM_SYSKEYUP:
752 case WM_KEYUP: {
753 int vcode = wParam;
754 int scode = (lParam >> 16) & 0xff;
755 bool extended = (lParam >> 24) & 0x1;
756 bool previous = (lParam >> 30) & 0x1;
757
758 /* The docs say that previous should always be 1, but it's not in practice.
759 * The events with previous = 0 seem malformed? I can get them reliably by
760 * pressing + holding Shift and then tapping Alt. */
761 if (previous)
762 _al_win_kbd_handle_key_release(scode, vcode, extended, win_display);
763 break;
764 }
765 case WM_SYSCOMMAND: {
766 DWORD style;
767 DWORD exstyle;
768 if (_al_win_disable_screensaver &&
769 ((wParam & 0xfff0) == SC_MONITORPOWER || (wParam & 0xfff0) == SC_SCREENSAVE)) {
770 return 0;
771 }
772 else if ((wParam & 0xfff0) == SC_KEYMENU) {
773 /* Prevent Windows from intercepting the ALT key.
774 (Disables opening menus via the ALT key.) */
775 return 0;
776 }
777 /* This is used by WM_GETMINMAXINFO to set constraints. */
778 else if ((wParam & 0xfff0) == SC_MAXIMIZE) {
779 d->flags |= ALLEGRO_MAXIMIZED;
780 display_flags_to_window_styles(d->flags, &style, &exstyle);
781 SetWindowLong(hWnd, GWL_EXSTYLE, exstyle);
782 }
783 else if ((wParam & 0xfff0) == SC_RESTORE) {
784 d->flags &= ~ALLEGRO_MAXIMIZED;
785 display_flags_to_window_styles(d->flags, &style, &exstyle);
786 SetWindowLong(hWnd, GWL_EXSTYLE, exstyle);
787 }
788 break;
789 }
790 case WM_PAINT: {
791 if (win_display->display.flags & ALLEGRO_GENERATE_EXPOSE_EVENTS) {
792 RECT r;
793 HRGN hrgn;
794 GetWindowRect(win_display->window, &r);
795 hrgn = CreateRectRgn(r.left, r.top, r.right, r.bottom);
796 if (GetUpdateRgn(win_display->window, hrgn, false) != ERROR) {
797 PAINTSTRUCT ps;
798 DWORD size;
799 LPRGNDATA rgndata;
800 int n;
801 int i;
802 RECT *rects;
803 BeginPaint(win_display->window, &ps);
804 size = GetRegionData(hrgn, 0, NULL);
805 rgndata = al_malloc(size);
806 GetRegionData(hrgn, size, rgndata);
807 n = rgndata->rdh.nCount;
808 rects = (RECT *)rgndata->Buffer;
809 //GetWindowInfo(win_display->window, &wi);
810 _al_event_source_lock(es);
811 if (_al_event_source_needs_to_generate_event(es)) {
812 ALLEGRO_EVENT event;
813 event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE;
814 event.display.timestamp = al_get_time();
815 for (i = 0; i < n; i++) {
816 event.display.x = rects[i].left;
817 event.display.y = rects[i].top;
818 event.display.width = rects[i].right - rects[i].left;
819 event.display.height = rects[i].bottom - rects[i].top;
820 _al_event_source_emit_event(es, &event);
821 }
822 }
823 _al_event_source_unlock(es);
824 al_free(rgndata);
825 EndPaint(win_display->window, &ps);
826 DeleteObject(hrgn);
827 }
828 return 0;
829 }
830 break;
831 }
832
833 case WM_SETCURSOR:
834 switch (LOWORD(lParam)) {
835 case HTLEFT:
836 case HTRIGHT:
837 SetCursor(LoadCursor(NULL, IDC_SIZEWE));
838 break;
839 case HTBOTTOM:
840 case HTTOP:
841 SetCursor(LoadCursor(NULL, IDC_SIZENS));
842 break;
843 case HTBOTTOMLEFT:
844 case HTTOPRIGHT:
845 SetCursor(LoadCursor(NULL, IDC_SIZENESW));
846 break;
847 case HTBOTTOMRIGHT:
848 case HTTOPLEFT:
849 SetCursor(LoadCursor(NULL, IDC_SIZENWSE));
850 break;
851 default:
852 if (win_display->mouse_cursor_shown) {
853 SetCursor(win_display->mouse_selected_hcursor);
854 }
855 else {
856 SetCursor(NULL);
857 }
858 break;
859 }
860 return 1;
861 case WM_ACTIVATE:
862 if (HIWORD(wParam) && LOWORD(wParam) != WA_INACTIVE)
863 break;
864
865 if (HIWORD(wParam))
866 d->flags |= ALLEGRO_MINIMIZED;
867 else
868 d->flags &= ~ALLEGRO_MINIMIZED;
869
870 if (LOWORD(wParam) != WA_INACTIVE) {
871 if (d->vt->switch_in)
872 d->vt->switch_in(d);
873 _al_win_fix_modifiers();
874 _al_event_source_lock(es);
875 if (_al_event_source_needs_to_generate_event(es)) {
876 ALLEGRO_EVENT event;
877 memset(&event, 0, sizeof(event));
878 event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN;
879 event.display.timestamp = al_get_time();
880 _al_event_source_emit_event(es, &event);
881 }
882 _al_event_source_unlock(es);
883 _al_win_grab_input(win_display);
884 return 0;
885 }
886 else {
887 if (d->flags & ALLEGRO_FULLSCREEN) {
888 d->vt->switch_out(d);
889 }
890 _al_event_source_lock(es);
891 if (_al_event_source_needs_to_generate_event(es)) {
892 ALLEGRO_EVENT event;
893 memset(&event, 0, sizeof(event));
894 event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT;
895 event.display.timestamp = al_get_time();
896 _al_event_source_emit_event(es, &event);
897 }
898 _al_event_source_unlock(es);
899 return 0;
900 }
901 break;
902 case WM_MENUCHAR :
903 return (MNC_CLOSE << 16) | (wParam & 0xffff);
904 case WM_CLOSE:
905 _al_event_source_lock(es);
906 if (_al_event_source_needs_to_generate_event(es)) {
907 ALLEGRO_EVENT event;
908 memset(&event, 0, sizeof(event));
909 event.display.type = ALLEGRO_EVENT_DISPLAY_CLOSE;
910 event.display.timestamp = al_get_time();
911 _al_event_source_emit_event(es, &event);
912 }
913 _al_event_source_unlock(es);
914 return 0;
915 case WM_GETMINMAXINFO:
916 /* Set window constraints only when needed. */
917 if (d->use_constraints) {
918 LPMINMAXINFO p_info = (LPMINMAXINFO)lParam;
919 RECT wRect;
920 RECT cRect;
921 int wWidth;
922 int wHeight;
923 int cWidth;
924 int cHeight;
925
926 GetWindowRect(hWnd, &wRect);
927 GetClientRect(hWnd, &cRect);
928 wWidth = wRect.right - wRect.left;
929 wHeight = wRect.bottom - wRect.top;
930 cWidth = cRect.right - cRect.left;
931 cHeight = cRect.bottom - cRect.top;
932
933 /* Client size is zero when the window is restored. */
934 if (cWidth != 0 && cHeight != 0) {
935 int total_border_width = wWidth - cWidth;
936 int total_border_height = wHeight - cHeight;
937 POINT wmin, wmax;
938
939 wmin.x = (d->min_w > 0) ? d->min_w + total_border_width : p_info->ptMinTrackSize.x;
940 wmin.y = (d->min_h > 0) ? d->min_h + total_border_height : p_info->ptMinTrackSize.y;
941
942 /* don't use max_w & max_h constraints when window maximized */
943 if (d->flags & ALLEGRO_MAXIMIZED) {
944 wmax.x = p_info->ptMaxTrackSize.x;
945 wmax.y = p_info->ptMaxTrackSize.y;
946 }
947 else {
948 wmax.x = (d->max_w > 0) ? d->max_w + total_border_width : p_info->ptMaxTrackSize.x;
949 wmax.y = (d->max_h > 0) ? d->max_h + total_border_height : p_info->ptMaxTrackSize.y;
950 }
951
952 p_info->ptMinTrackSize = wmin;
953 p_info->ptMaxTrackSize = wmax;
954 }
955 }
956 break;
957 case WM_SIZE: {
958 bool generate = false;
959 if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED) {
960 generate = true;
961 d->flags &= ~ALLEGRO_MAXIMIZED;
962 if (wParam == SIZE_MAXIMIZED) {
963 d->flags |= ALLEGRO_MAXIMIZED;
964 }
965 }
966 else if (d->use_constraints) {
967 /* al_apply_window_constraints() resizes a window if the current
968 * width & height do not fit in the constraint's range.
969 * We have to create the resize event, so the application could
970 * redraw its content.
971 */
972 generate = true;
973 }
974 al_lock_mutex(resize_event_thread_mutex);
975 if (_al_vector_contains(&resizing_displays, &win_display)) {
976 generate = false;
977 }
978 al_unlock_mutex(resize_event_thread_mutex);
979 if (generate) {
980 win_generate_resize_event(win_display);
981 }
982 return 0;
983 }
984 case WM_ENTERSIZEMOVE: {
985 al_lock_mutex(resize_event_thread_mutex);
986 if (d->flags & ALLEGRO_DIRECT3D_INTERNAL) {
987 win_display->d3d_ignore_resize = true;
988 }
989 ALLEGRO_DISPLAY_WIN **add = (ALLEGRO_DISPLAY_WIN **)_al_vector_alloc_back(&resizing_displays);
990 *add = win_display;
991 al_unlock_mutex(resize_event_thread_mutex);
992 break;
993 }
994 case WM_EXITSIZEMOVE:
995 al_lock_mutex(resize_event_thread_mutex);
996 if (d->flags & ALLEGRO_DIRECT3D_INTERNAL) {
997 win_display->d3d_ignore_resize = false;
998 }
999 _al_vector_find_and_delete(&resizing_displays, &win_display);
1000 al_unlock_mutex(resize_event_thread_mutex);
1001 win_generate_resize_event(win_display);
1002 break;
1003 case WM_DPICHANGED:
1004 if ((d->flags & ALLEGRO_RESIZABLE) && !(d->flags & ALLEGRO_FULLSCREEN)) {
1005 RECT* rect = (RECT*)lParam;
1006 // XXX: This doesn't seem to actually move the window... why?
1007 SetWindowPos(
1008 hWnd,
1009 0,
1010 rect->left,
1011 rect->top,
1012 rect->right - rect->left,
1013 rect->bottom - rect->top,
1014 SWP_NOZORDER | SWP_NOACTIVATE);
1015 win_generate_resize_event(win_display);
1016 }
1017 break;
1018 case WM_DEVICECHANGE:
1019 _al_win_joystick_dinput_trigger_enumeration();
1020 break;
1021 }
1022
1023 return DefWindowProc(hWnd,message,wParam,lParam);
1024 }
1025
1026 int _al_win_init_window(void)
1027 {
1028 // Create A Window Class Structure
1029 window_class.cbClsExtra = 0;
1030 window_class.cbWndExtra = 0;
1031 window_class.hbrBackground = NULL;
1032 window_class.hCursor = NULL;
1033 window_class.hIcon = NULL;
1034 window_class.hInstance = GetModuleHandle(NULL);
1035 window_class.lpfnWndProc = window_callback;
1036 window_class.lpszClassName = TEXT("ALEX");
1037 window_class.lpszMenuName = NULL;
1038 window_class.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
1039
1040 RegisterClass(&window_class);
1041
1042 if (_al_win_msg_call_proc == 0 && _al_win_msg_suicide == 0) {
1043 _al_win_msg_call_proc = RegisterWindowMessage(TEXT("Allegro call proc"));
1044 _al_win_msg_suicide = RegisterWindowMessage(TEXT("Allegro window suicide"));
1045 }
1046
1047 resize_event_thread_mutex = al_create_mutex();
1048 _al_vector_init(&resizing_displays, sizeof(ALLEGRO_DISPLAY_WIN *));
1049 end_resize_event_thread = false;
1050 resize_event_thread_ended = false;
1051 _beginthread(resize_event_thread_proc, 0, 0);
1052
1053 return true;
1054 }
1055
1056 void _al_win_shutdown_window(void)
1057 {
1058 al_lock_mutex(resize_event_thread_mutex);
1059 end_resize_event_thread = true;
1060 al_unlock_mutex(resize_event_thread_mutex);
1061
1062 while (true) {
1063 al_lock_mutex(resize_event_thread_mutex);
1064 if (resize_event_thread_ended) {
1065 al_unlock_mutex(resize_event_thread_mutex);
1066 break;
1067 }
1068 al_unlock_mutex(resize_event_thread_mutex);
1069 Sleep(10);
1070 }
1071 _al_vector_free(&resizing_displays);
1072 al_destroy_mutex(resize_event_thread_mutex);
1073 resize_event_thread_mutex = NULL;
1074 }
1075
1076
1077 static int win_choose_icon_bitmap(const int sys_w, const int sys_h,
1078 const int num_icons, ALLEGRO_BITMAP *bmps[])
1079 {
1080 int best_i = 0;
1081 int best_score = INT_MAX;
1082 int i;
1083
1084 for (i = 0; i < num_icons; i++) {
1085 int bmp_w = al_get_bitmap_width(bmps[i]);
1086 int bmp_h = al_get_bitmap_height(bmps[i]);
1087 int score;
1088
1089 if (bmp_w == sys_w && bmp_h == sys_h)
1090 return i;
1091
1092 /* We prefer to scale up smaller bitmaps to the desired size than to
1093 * scale down larger bitmaps. At these resolutions, scaled up bitmaps
1094 * look blocky, but scaled down bitmaps can look even worse due to to
1095 * dropping crucial pixels.
1096 */
1097 if (bmp_w * bmp_h <= sys_w * sys_h)
1098 score = (sys_w * sys_h) - (bmp_w * bmp_h);
1099 else
1100 score = bmp_w * bmp_h;
1101
1102 if (score < best_score) {
1103 best_score = score;
1104 best_i = i;
1105 }
1106 }
1107
1108 return best_i;
1109 }
1110
1111 static void win_set_display_icon(ALLEGRO_DISPLAY_WIN *win_display,
1112 const WPARAM icon_type, const int sys_w, const int sys_h,
1113 const int num_icons, ALLEGRO_BITMAP *bmps[])
1114 {
1115 HICON icon;
1116 HICON old_icon;
1117 ALLEGRO_BITMAP *bmp;
1118 int bmp_w;
1119 int bmp_h;
1120 int i;
1121
1122 i = win_choose_icon_bitmap(sys_w, sys_h, num_icons, bmps);
1123 bmp = bmps[i];
1124 bmp_w = al_get_bitmap_width(bmp);
1125 bmp_h = al_get_bitmap_height(bmp);
1126
1127 if (bmp_w == sys_w && bmp_h == sys_h) {
1128 icon = _al_win_create_icon(win_display->window, bmp, 0, 0, false, false);
1129 }
1130 else {
1131 ALLEGRO_BITMAP *tmp_bmp;
1132 ALLEGRO_STATE backup;
1133
1134 tmp_bmp = al_create_bitmap(sys_w, sys_h);
1135 if (!tmp_bmp)
1136 return;
1137
1138 al_store_state(&backup, ALLEGRO_STATE_BITMAP | ALLEGRO_STATE_BLENDER);
1139 al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
1140 al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ARGB_8888);
1141
1142 al_set_target_bitmap(tmp_bmp);
1143 al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
1144 al_draw_scaled_bitmap(bmp, 0, 0, bmp_w, bmp_h, 0, 0, sys_w, sys_h, 0);
1145
1146 al_restore_state(&backup);
1147
1148 icon = _al_win_create_icon(win_display->window, tmp_bmp, 0, 0, false,
1149 false);
1150
1151 al_destroy_bitmap(tmp_bmp);
1152 }
1153
1154 old_icon = (HICON)SendMessage(win_display->window, WM_SETICON,
1155 icon_type, (LPARAM)icon);
1156
1157 if (old_icon)
1158 DestroyIcon(old_icon);
1159 }
1160
1161 void _al_win_set_display_icons(ALLEGRO_DISPLAY *display,
1162 int num_icons, ALLEGRO_BITMAP *bmps[])
1163 {
1164 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
1165 int sys_w;
1166 int sys_h;
1167
1168 sys_w = GetSystemMetrics(SM_CXSMICON);
1169 sys_h = GetSystemMetrics(SM_CYSMICON);
1170 win_set_display_icon(win_display, ICON_SMALL, sys_w, sys_h,
1171 num_icons, bmps);
1172
1173 sys_w = GetSystemMetrics(SM_CXICON);
1174 sys_h = GetSystemMetrics(SM_CYICON);
1175 win_set_display_icon(win_display, ICON_BIG, sys_w, sys_h,
1176 num_icons, bmps);
1177 }
1178
1179 void _al_win_destroy_display_icons(ALLEGRO_DISPLAY *display)
1180 {
1181 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
1182 HICON old_icon;
1183
1184 old_icon = (HICON)SendMessage(win_display->window, WM_SETICON, ICON_SMALL, 0);
1185 if (old_icon)
1186 DestroyIcon(old_icon);
1187 old_icon = (HICON)SendMessage(win_display->window, WM_SETICON, ICON_BIG, 0);
1188 if (old_icon)
1189 DestroyIcon(old_icon);
1190 }
1191
1192 static void get_top_left_border(HWND window, int *left, int *top)
1193 {
1194 WINDOWINFO wi;
1195 wi.cbSize = sizeof(WINDOWINFO);
1196 GetWindowInfo(window, &wi);
1197
1198 *left = (wi.rcClient.left - wi.rcWindow.left);
1199 *top = (wi.rcClient.top - wi.rcWindow.top);
1200 }
1201
1202 void _al_win_set_window_position(HWND window, int x, int y)
1203 {
1204 int left, top;
1205 get_top_left_border(window, &left, &top);
1206 SetWindowPos(
1207 window,
1208 HWND_TOP,
1209 x - left,
1210 y - top,
1211 0,
1212 0,
1213 SWP_NOSIZE | SWP_NOZORDER);
1214 }
1215
1216 void _al_win_get_window_position(HWND window, int *x, int *y)
1217 {
1218 RECT r;
1219 int left, top;
1220 get_top_left_border(window, &left, &top);
1221 GetWindowRect(window, &r);
1222
1223 if (x) {
1224 *x = r.left + left;
1225 }
1226 if (y) {
1227 *y = r.top + top;
1228 }
1229 }
1230
1231
1232 static void update_adapter(ALLEGRO_DISPLAY *display)
1233 {
1234 ALLEGRO_DISPLAY_WIN *win_display = (void*)display;
1235 int adapter = al_get_display_adapter(display);
1236 if (adapter >= 0)
1237 win_display->adapter = adapter;
1238 else
1239 win_display->adapter = 0;
1240 }
1241
1242
1243 void _al_win_set_window_frameless(ALLEGRO_DISPLAY *display, HWND hWnd,
1244 bool frameless)
1245 {
1246 int w = display->w;
1247 int h = display->h;
1248
1249 if (frameless) {
1250 SetWindowLong(hWnd, GWL_STYLE, WS_VISIBLE);
1251 SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_APPWINDOW);
1252 SetWindowPos(hWnd, 0, 0, 0, w, h, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
1253 }
1254 else {
1255 RECT r;
1256 DWORD style;
1257 DWORD exStyle;
1258
1259 display_flags_to_window_styles(display->flags, &style, &exStyle);
1260 style |= WS_VISIBLE;
1261
1262 GetWindowRect(hWnd, &r);
1263 AdjustWindowRectEx(&r, style, GetMenu(hWnd) ? TRUE : FALSE, exStyle);
1264
1265 w = r.right - r.left;
1266 h = r.bottom - r.top;
1267
1268 SetWindowLong(hWnd, GWL_STYLE, style);
1269 SetWindowLong(hWnd, GWL_EXSTYLE, exStyle);
1270 SetWindowPos(hWnd, 0, 0, 0, w, h, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED);
1271 }
1272 }
1273
1274
1275 bool _al_win_set_display_flag(ALLEGRO_DISPLAY *display, int flag, bool onoff)
1276 {
1277 ALLEGRO_DISPLAY_WIN *win_display = (void*)display;
1278 //double timeout;
1279 ALLEGRO_MONITOR_INFO mi;
1280
1281 memset(&mi, 0, sizeof(mi));
1282
1283 switch (flag) {
1284 case ALLEGRO_FRAMELESS: {
1285 if (onoff) {
1286 display->flags |= ALLEGRO_FRAMELESS;
1287 }
1288 else {
1289 display->flags &= ~ALLEGRO_FRAMELESS;
1290 }
1291 _al_win_set_window_frameless(display, win_display->window,
1292 (display->flags & ALLEGRO_FRAMELESS));
1293 return true;
1294 }
1295
1296 case ALLEGRO_FULLSCREEN_WINDOW:
1297 if ((display->flags & ALLEGRO_FULLSCREEN_WINDOW) && onoff) {
1298 ALLEGRO_DEBUG("Already a fullscreen window\n");
1299 return true;
1300 }
1301 if (!(display->flags & ALLEGRO_FULLSCREEN_WINDOW) && !onoff) {
1302 ALLEGRO_DEBUG("Already a non-fullscreen window\n");
1303 return true;
1304 }
1305
1306 if (onoff) {
1307 /* Switch off frame in fullscreen window mode. */
1308 _al_win_set_window_frameless(display, win_display->window, true);
1309 }
1310 else {
1311 /* Respect display flag in windowed mode. */
1312 _al_win_set_window_frameless(display, win_display->window,
1313 (display->flags & ALLEGRO_FRAMELESS));
1314 }
1315
1316 if (onoff) {
1317 update_adapter(display);
1318 int adapter = win_display->adapter;
1319 al_get_monitor_info(adapter, &mi);
1320 display->flags |= ALLEGRO_FULLSCREEN_WINDOW;
1321 display->w = mi.x2 - mi.x1;
1322 display->h = mi.y2 - mi.y1;
1323 }
1324 else {
1325 display->flags &= ~ALLEGRO_FULLSCREEN_WINDOW;
1326 display->w = win_display->toggle_w;
1327 display->h = win_display->toggle_h;
1328 }
1329
1330 ASSERT(!!(display->flags & ALLEGRO_FULLSCREEN_WINDOW) == onoff);
1331
1332 // Hide the window temporarily
1333 SetWindowPos(win_display->window, 0, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE);
1334
1335 al_resize_display(display, display->w, display->h);
1336
1337 if (onoff) {
1338 // Move to position
1339 SetWindowPos(win_display->window, HWND_TOP, mi.x1, mi.y1, 0, 0, SWP_NOSIZE);
1340 }
1341 else {
1342 int pos_x = 0;
1343 int pos_y = 0;
1344 WINDOWINFO wi;
1345 int bw, bh;
1346
1347 // Center the window
1348 _al_win_get_window_center(win_display, display->w, display->h, &pos_x, &pos_y);
1349 GetWindowInfo(win_display->window, &wi);
1350 bw = (wi.rcClient.left - wi.rcWindow.left) + (wi.rcWindow.right - wi.rcClient.right),
1351 bh = (wi.rcClient.top - wi.rcWindow.top) + (wi.rcWindow.bottom - wi.rcClient.bottom),
1352 SetWindowPos(
1353 win_display->window, HWND_TOP, 0, 0, display->w+bw, display->h+bh, SWP_NOMOVE
1354 );
1355 SetWindowPos(
1356 win_display->window, HWND_TOP, pos_x-bw/2, pos_y-bh/2, 0, 0, SWP_NOSIZE
1357 );
1358 }
1359
1360 // Show the window again
1361 SetWindowPos(win_display->window, 0, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE);
1362
1363 // Clear the window to black
1364 clear_window(win_display->window, display->w, display->h);
1365
1366 ASSERT(!!(display->flags & ALLEGRO_FULLSCREEN_WINDOW) == onoff);
1367 return true;
1368 case ALLEGRO_MAXIMIZED:
1369 if ((!!(display->flags & ALLEGRO_MAXIMIZED)) == onoff)
1370 return true;
1371 if (onoff) {
1372 ShowWindow(win_display->window, SW_SHOWMAXIMIZED);
1373 }
1374 else {
1375 ShowWindow(win_display->window, SW_RESTORE);
1376 }
1377 return true;
1378 }
1379 return false;
1380 }
1381
1382
1383 void _al_win_set_window_title(ALLEGRO_DISPLAY *display, const char *title)
1384 {
1385 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
1386 TCHAR *ttitle = _twin_utf8_to_tchar(title);
1387 SetWindowText(win_display->window, ttitle);
1388 al_free(ttitle);
1389 }
1390
1391 bool _al_win_set_window_constraints(ALLEGRO_DISPLAY *display,
1392 int min_w, int min_h, int max_w, int max_h)
1393 {
1394 display->min_w = min_w;
1395 display->min_h = min_h;
1396 display->max_w = max_w;
1397 display->max_h = max_h;
1398
1399 return true;
1400 }
1401
1402 void _al_win_apply_window_constraints(ALLEGRO_DISPLAY *display, bool onoff)
1403 {
1404 if (onoff) {
1405 if (!(display->flags & ALLEGRO_MAXIMIZED)) {
1406 al_resize_display(display, display->w, display->h);
1407 }
1408 }
1409 }
1410
1411 void _al_win_post_create_window(ALLEGRO_DISPLAY *display)
1412 {
1413 /* Ideally the d3d/wgl window creation would already create the
1414 * window maximized - but that code looks too messy to me to touch
1415 * right now.
1416 */
1417 if (display->flags & ALLEGRO_MAXIMIZED) {
1418 display->flags &= ~ALLEGRO_MAXIMIZED;
1419 al_set_display_flag(display, ALLEGRO_MAXIMIZED, true);
1420 }
1421 }
1422
1423 bool _al_win_get_window_constraints(ALLEGRO_DISPLAY *display,
1424 int *min_w, int *min_h, int *max_w, int *max_h)
1425 {
1426 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
1427 *min_w = win_display->display.min_w;
1428 *min_h = win_display->display.min_h;
1429 *max_w = win_display->display.max_w;
1430 *max_h = win_display->display.max_h;
1431
1432 return true;
1433 }
1434
1435
1436 /* _al_win_wnd_call_proc:
1437 * instructs the specifed window thread to call the specified procedure. Waits
1438 * until the procedure has returned.
1439 */
1440 void _al_win_wnd_call_proc(HWND wnd, void (*proc) (void*), void* param)
1441 {
1442 ASSERT(_al_win_msg_call_proc);
1443 SendMessage(wnd, _al_win_msg_call_proc, (WPARAM)proc, (LPARAM)param);
1444 }
1445
1446
1447 /* _al_win_wnd_schedule_proc:
1448 * instructs the specifed window thread to call the specified procedure but
1449 * doesn't wait until the procedure has returned.
1450 */
1451 void _al_win_wnd_schedule_proc(HWND wnd, void (*proc) (void*), void* param)
1452 {
1453 ASSERT(_al_win_msg_call_proc);
1454 if (!PostMessage(wnd, _al_win_msg_call_proc, (WPARAM)proc, (LPARAM)param)) {
1455 ALLEGRO_ERROR("_al_win_wnd_schedule_proc failed.\n");
1456 }
1457 }
1458
1459
1460 /* Function: al_get_win_window_handle
1461 */
1462 HWND al_get_win_window_handle(ALLEGRO_DISPLAY *display)
1463 {
1464 if (!display)
1465 return NULL;
1466 return ((ALLEGRO_DISPLAY_WIN *)display)->window;
1467 }
1468
1469
1470 int _al_win_determine_adapter(void)
1471 {
1472 int a = al_get_new_display_adapter();
1473 if (a == -1) {
1474 int num_screens = al_get_num_video_adapters();
1475 int cScreen = 0;
1476 ALLEGRO_MONITOR_INFO temp_info;
1477 for (cScreen = 0; cScreen < num_screens; cScreen++) {
1478 al_get_monitor_info(cScreen, &temp_info);
1479 if (temp_info.x1 == 0 && temp_info.y1 == 0) { // ..probably found primary display
1480 return cScreen;
1481 }
1482 }
1483 return 0; // safety measure, probably not necessary
1484 }
1485 return a;
1486 }
1487
1488 bool _al_win_get_window_borders(ALLEGRO_DISPLAY *display, int *left, int *top, int *right, int *bottom)
1489 {
1490 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display;
1491 WINDOWINFO wi;
1492 wi.cbSize = sizeof(WINDOWINFO);
1493 GetWindowInfo(win_display->window, &wi);
1494
1495 if (left) *left = (wi.rcClient.left - wi.rcWindow.left);
1496 if (top) *top = (wi.rcClient.top - wi.rcWindow.top);
1497 if (right) *right = (wi.rcWindow.right - wi.rcClient.right);
1498 if (bottom) *bottom = (wi.rcWindow.bottom - wi.rcClient.bottom);
1499 return true;
1500 }
1501
1502 /* Function: al_win_add_window_callback
1503 */
1504 bool al_win_add_window_callback(ALLEGRO_DISPLAY *display,
1505 bool (*callback)(ALLEGRO_DISPLAY *display, UINT message, WPARAM wparam,
1506 LPARAM lparam, LRESULT *result, void *userdata), void *userdata)
1507 {
1508 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *) display;
1509 ALLEGRO_DISPLAY_WIN_CALLBACK *ptr;
1510
1511 if (!display || !callback) {
1512 return false;
1513 }
1514 else {
1515 size_t i;
1516 for (i = 0; i < _al_vector_size(&win_display->msg_callbacks); ++i) {
1517 ALLEGRO_DISPLAY_WIN_CALLBACK *ptr = _al_vector_ref(&win_display->msg_callbacks, i);
1518 if (ptr->proc == callback && ptr->userdata == userdata)
1519 return false;
1520 }
1521 }
1522
1523 if (!(ptr = _al_vector_alloc_back(&win_display->msg_callbacks)))
1524 return false;
1525
1526 ptr->proc = callback;
1527 ptr->userdata = userdata;
1528 return true;
1529 }
1530
1531 /* Function: al_win_remove_window_callback
1532 */
1533 bool al_win_remove_window_callback(ALLEGRO_DISPLAY *display,
1534 bool (*callback)(ALLEGRO_DISPLAY *display, UINT message, WPARAM wparam,
1535 LPARAM lparam, LRESULT *result, void *userdata), void *userdata)
1536 {
1537 ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *) display;
1538
1539 if (display && callback) {
1540 size_t i;
1541 for (i = 0; i < _al_vector_size(&win_display->msg_callbacks); ++i) {
1542 ALLEGRO_DISPLAY_WIN_CALLBACK *ptr = _al_vector_ref(&win_display->msg_callbacks, i);
1543 if (ptr->proc == callback && ptr->userdata == userdata) {
1544 _al_vector_delete_at(&win_display->msg_callbacks, i);
1545 return true;
1546 }
1547 }
1548 }
1549
1550 return false;
1551 }
1552
1553 /* vi: set ts=8 sts=3 sw=3 et: */