Fix right mouse scrolling in the win32 video driver
[openttd/fttd.git] / src / video / win32_v.cpp
blob9f37f306c822b8ae517b4d3a0c9f318a33ae9088
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file win32_v.cpp Implementation of the Windows (GDI) video driver. */
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "../openttd.h"
15 #include "../gfx_func.h"
16 #include "../os/windows/win32.h"
17 #include "../rev.h"
18 #include "../blitter/blitter.h"
19 #include "../network/network.h"
20 #include "../core/math_func.hpp"
21 #include "../core/random_func.hpp"
22 #include "../texteff.hpp"
23 #include "../thread/thread.h"
24 #include "../progress.h"
25 #include "../window_gui.h"
26 #include "../window_func.h"
27 #include "win32_v.h"
28 #include <windows.h>
29 #include <imm.h>
31 /* Missing define in MinGW headers. */
32 #ifndef MAPVK_VK_TO_CHAR
33 #define MAPVK_VK_TO_CHAR (2)
34 #endif
36 #ifndef PM_QS_INPUT
37 #define PM_QS_INPUT 0x20000
38 #endif
40 static struct {
41 HWND main_wnd;
42 HBITMAP dib_sect;
43 void *buffer_bits;
44 HPALETTE gdi_palette;
45 RECT update_rect;
46 int width;
47 int height;
48 int width_org;
49 int height_org;
50 bool fullscreen;
51 bool has_focus;
52 bool running;
53 } _wnd;
55 bool _force_full_redraw;
56 bool _window_maximize;
57 uint _display_hz;
58 static Dimension _bck_resolution;
59 #if !defined(WINCE) || _WIN32_WCE >= 0x400
60 DWORD _imm_props;
61 #endif
63 /** Whether the drawing is/may be done in a separate thread. */
64 static bool _draw_threaded;
65 /** Thread used to 'draw' to the screen, i.e. push data to the screen. */
66 static ThreadObject *_draw_thread = NULL;
67 /** Mutex to keep the access to the shared memory controlled. */
68 static ThreadMutex *_draw_mutex = NULL;
69 /** Event that is signaled when the drawing thread has finished initializing. */
70 static HANDLE _draw_thread_initialized = NULL;
71 /** Should we keep continue drawing? */
72 static volatile bool _draw_continue;
73 /** Local copy of the palette for use in the drawing thread. */
74 static Palette _local_palette;
75 /** First palette dirty element. */
76 static int _local_palette_first_dirty;
77 /** Number of palette dirty elements. */
78 static int _local_palette_count_dirty;
80 static void MakePalette()
82 LOGPALETTE *pal = (LOGPALETTE*)alloca(sizeof(LOGPALETTE) + (256 - 1) * sizeof(PALETTEENTRY));
84 pal->palVersion = 0x300;
85 pal->palNumEntries = 256;
87 for (uint i = 0; i != 256; i++) {
88 pal->palPalEntry[i].peRed = _cur_palette[i].r;
89 pal->palPalEntry[i].peGreen = _cur_palette[i].g;
90 pal->palPalEntry[i].peBlue = _cur_palette[i].b;
91 pal->palPalEntry[i].peFlags = 0;
94 _wnd.gdi_palette = CreatePalette(pal);
95 if (_wnd.gdi_palette == NULL) usererror("CreatePalette failed!\n");
97 _cur_palette_first_dirty = 0;
98 _cur_palette_count_dirty = 256;
99 assert_compile (sizeof(_local_palette) == sizeof(_cur_palette));
100 memcpy (_local_palette, _cur_palette, sizeof(_local_palette));
101 _local_palette_first_dirty = 0;
102 _local_palette_count_dirty = 256;
105 static void UpdatePalette(HDC dc, uint start, uint count)
107 RGBQUAD rgb[256];
108 uint i;
110 for (i = 0; i != count; i++) {
111 rgb[i].rgbRed = _local_palette[start + i].r;
112 rgb[i].rgbGreen = _local_palette[start + i].g;
113 rgb[i].rgbBlue = _local_palette[start + i].b;
114 rgb[i].rgbReserved = 0;
117 SetDIBColorTable(dc, start, count, rgb);
120 bool VideoDriver_Win32::ClaimMousePointer()
122 MyShowCursor(false, true);
123 return true;
126 struct VkMapping {
127 byte vk_from;
128 byte vk_count;
129 byte map_to;
132 #define AS(x, z) {x, 0, z}
133 #define AM(x, y, z, w) {x, y - x, z}
135 static const VkMapping _vk_mapping[] = {
136 /* Pageup stuff + up/down */
137 AM(VK_PRIOR, VK_DOWN, WKC_PAGEUP, WKC_DOWN),
138 /* Map letters & digits */
139 AM('A', 'Z', 'A', 'Z'),
140 AM('0', '9', '0', '9'),
142 AS(VK_ESCAPE, WKC_ESC),
143 AS(VK_PAUSE, WKC_PAUSE),
144 AS(VK_BACK, WKC_BACKSPACE),
145 AM(VK_INSERT, VK_DELETE, WKC_INSERT, WKC_DELETE),
147 AS(VK_SPACE, WKC_SPACE),
148 AS(VK_RETURN, WKC_RETURN),
149 AS(VK_TAB, WKC_TAB),
151 /* Function keys */
152 AM(VK_F1, VK_F12, WKC_F1, WKC_F12),
154 /* Numeric part */
155 AM(VK_NUMPAD0, VK_NUMPAD9, '0', '9'),
156 AS(VK_DIVIDE, WKC_NUM_DIV),
157 AS(VK_MULTIPLY, WKC_NUM_MUL),
158 AS(VK_SUBTRACT, WKC_NUM_MINUS),
159 AS(VK_ADD, WKC_NUM_PLUS),
160 AS(VK_DECIMAL, WKC_NUM_DECIMAL),
162 /* Other non-letter keys */
163 AS(0xBF, WKC_SLASH),
164 AS(0xBA, WKC_SEMICOLON),
165 AS(0xBB, WKC_EQUALS),
166 AS(0xDB, WKC_L_BRACKET),
167 AS(0xDC, WKC_BACKSLASH),
168 AS(0xDD, WKC_R_BRACKET),
170 AS(0xDE, WKC_SINGLEQUOTE),
171 AS(0xBC, WKC_COMMA),
172 AS(0xBD, WKC_MINUS),
173 AS(0xBE, WKC_PERIOD)
176 static uint MapWindowsKey(uint sym)
178 const VkMapping *map;
179 uint key = 0;
181 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
182 if ((uint)(sym - map->vk_from) <= map->vk_count) {
183 key = sym - map->vk_from + map->map_to;
184 break;
188 if (GetAsyncKeyState(VK_SHIFT) < 0) key |= WKC_SHIFT;
189 if (GetAsyncKeyState(VK_CONTROL) < 0) key |= WKC_CTRL;
190 if (GetAsyncKeyState(VK_MENU) < 0) key |= WKC_ALT;
191 return key;
194 static bool AllocateDibSection(int w, int h, bool force = false);
196 static void ClientSizeChanged(int w, int h)
198 /* allocate new dib section of the new size */
199 if (AllocateDibSection(w, h)) {
200 /* mark all palette colours dirty */
201 _cur_palette_first_dirty = 0;
202 _cur_palette_count_dirty = 256;
203 assert_compile (sizeof(_local_palette) == sizeof(_cur_palette));
204 memcpy (_local_palette, _cur_palette, sizeof(_local_palette));
205 _local_palette_first_dirty = 0;
206 _local_palette_count_dirty = 256;
208 GameSizeChanged();
212 #ifdef _DEBUG
213 /* Keep this function here..
214 * It allows you to redraw the screen from within the MSVC debugger */
215 int RedrawScreenDebug()
217 HDC dc, dc2;
218 static int _fooctr;
219 HBITMAP old_bmp;
220 HPALETTE old_palette;
222 UpdateWindows();
224 dc = GetDC(_wnd.main_wnd);
225 dc2 = CreateCompatibleDC(dc);
227 old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
228 old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
229 BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
230 SelectPalette(dc, old_palette, TRUE);
231 SelectObject(dc2, old_bmp);
232 DeleteDC(dc2);
233 ReleaseDC(_wnd.main_wnd, dc);
235 return _fooctr++;
237 #endif
239 /* Windows 95 will not have a WM_MOUSELEAVE message, so define it if needed */
240 #if !defined(WM_MOUSELEAVE)
241 #define WM_MOUSELEAVE 0x02A3
242 #endif
243 #define TID_POLLMOUSE 1
244 #define MOUSE_POLL_DELAY 75
246 static void CALLBACK TrackMouseTimerProc(HWND hwnd, UINT msg, UINT event, DWORD time)
248 RECT rc;
249 POINT pt;
251 /* Get the rectangle of our window and translate it to screen coordinates.
252 * Compare this with the current screen coordinates of the mouse and if it
253 * falls outside of the area or our window we have left the window. */
254 GetClientRect(hwnd, &rc);
255 MapWindowPoints(hwnd, HWND_DESKTOP, (LPPOINT)(LPRECT)&rc, 2);
256 GetCursorPos(&pt);
258 if (!PtInRect(&rc, pt) || (WindowFromPoint(pt) != hwnd)) {
259 KillTimer(hwnd, event);
260 PostMessage(hwnd, WM_MOUSELEAVE, 0, 0L);
264 static bool ChangeResolution (int w, int h);
267 * Instantiate a new window.
268 * @param full_screen Whether to make a full screen window or not.
269 * @return True if the window could be created.
271 static bool MakeWindow (bool full_screen)
273 _fullscreen = full_screen;
275 /* recreate window? */
276 if ((full_screen || _wnd.fullscreen) && _wnd.main_wnd) {
277 DestroyWindow(_wnd.main_wnd);
278 _wnd.main_wnd = 0;
281 #if defined(WINCE)
282 /* WinCE is always fullscreen */
283 #else
284 if (full_screen) {
285 DEVMODE settings;
287 memset(&settings, 0, sizeof(settings));
288 settings.dmSize = sizeof(settings);
289 settings.dmFields =
290 DM_BITSPERPEL |
291 DM_PELSWIDTH |
292 DM_PELSHEIGHT |
293 (_display_hz != 0 ? DM_DISPLAYFREQUENCY : 0);
294 settings.dmBitsPerPel = Blitter::get()->screen_depth;
295 settings.dmPelsWidth = _wnd.width_org;
296 settings.dmPelsHeight = _wnd.height_org;
297 settings.dmDisplayFrequency = _display_hz;
299 /* Check for 8 bpp support. */
300 if (settings.dmBitsPerPel == 8 &&
301 (_support8bpp != S8BPP_HARDWARE || ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL)) {
302 settings.dmBitsPerPel = 32;
305 /* Test fullscreen with current resolution, if it fails use desktop resolution. */
306 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN | CDS_TEST) != DISP_CHANGE_SUCCESSFUL) {
307 RECT r;
308 GetWindowRect(GetDesktopWindow(), &r);
309 /* Guard against recursion. If we already failed here once, just fall through to
310 * the next ChangeDisplaySettings call which will fail and error out appropriately. */
311 if ((int)settings.dmPelsWidth != r.right - r.left || (int)settings.dmPelsHeight != r.bottom - r.top) {
312 return ChangeResolution (r.right - r.left, r.bottom - r.top);
316 if (ChangeDisplaySettings(&settings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) {
317 MakeWindow (false); // don't care about the result
318 return false; // the request failed
320 } else if (_wnd.fullscreen) {
321 /* restore display? */
322 ChangeDisplaySettings(NULL, 0);
323 /* restore the resolution */
324 _wnd.width = _bck_resolution.width;
325 _wnd.height = _bck_resolution.height;
327 #endif
330 RECT r;
331 DWORD style, showstyle;
332 int w, h;
334 showstyle = SW_SHOWNORMAL;
335 _wnd.fullscreen = full_screen;
336 if (_wnd.fullscreen) {
337 style = WS_POPUP;
338 SetRect(&r, 0, 0, _wnd.width_org, _wnd.height_org);
339 } else {
340 style = WS_OVERLAPPEDWINDOW;
341 /* On window creation, check if we were in maximize mode before */
342 if (_window_maximize) showstyle = SW_SHOWMAXIMIZED;
343 SetRect(&r, 0, 0, _wnd.width, _wnd.height);
346 #if !defined(WINCE)
347 AdjustWindowRect(&r, style, FALSE);
348 #endif
349 w = r.right - r.left;
350 h = r.bottom - r.top;
352 if (_wnd.main_wnd != NULL) {
353 if (!_window_maximize) SetWindowPos(_wnd.main_wnd, 0, 0, 0, w, h, SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_NOMOVE);
354 } else {
355 TCHAR Windowtitle[50];
356 int x = (GetSystemMetrics(SM_CXSCREEN) - w) / 2;
357 int y = (GetSystemMetrics(SM_CYSCREEN) - h) / 2;
359 _sntprintf(Windowtitle, lengthof(Windowtitle), _T("OpenTTD %s"), MB_TO_WIDE(_openttd_revision));
361 _wnd.main_wnd = CreateWindow(_T("OTTD"), Windowtitle, style, x, y, w, h, 0, 0, GetModuleHandle(NULL), 0);
362 if (_wnd.main_wnd == NULL) usererror("CreateWindow failed");
363 ShowWindow(_wnd.main_wnd, showstyle);
367 GameSizeChanged(); // invalidate all windows, force redraw
368 return true; // the request succeeded
371 /** Do palette animation and blit to the window. */
372 static void PaintWindow(HDC dc)
374 HDC dc2 = CreateCompatibleDC(dc);
375 HBITMAP old_bmp = (HBITMAP)SelectObject(dc2, _wnd.dib_sect);
376 HPALETTE old_palette = SelectPalette(dc, _wnd.gdi_palette, FALSE);
378 if (_cur_palette_count_dirty != 0) {
379 switch (Blitter::get()->palette_animation) {
380 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
381 UpdatePalette (dc2, _local_palette_first_dirty, _local_palette_count_dirty);
382 break;
384 case Blitter::PALETTE_ANIMATION_BLITTER:
385 VideoDriver::PaletteAnimate (_local_palette);
386 break;
388 case Blitter::PALETTE_ANIMATION_NONE:
389 break;
391 default:
392 NOT_REACHED();
394 _cur_palette_count_dirty = 0;
397 BitBlt(dc, 0, 0, _wnd.width, _wnd.height, dc2, 0, 0, SRCCOPY);
398 SelectPalette(dc, old_palette, TRUE);
399 SelectObject(dc2, old_bmp);
400 DeleteDC(dc2);
403 static void PaintWindowThread(void *)
405 /* First tell the main thread we're started */
406 _draw_mutex->BeginCritical();
407 SetEvent(_draw_thread_initialized);
409 /* Now wait for the first thing to draw! */
410 _draw_mutex->WaitForSignal();
412 while (_draw_continue) {
413 /* Convert update region from logical to device coordinates. */
414 POINT pt = {0, 0};
415 ClientToScreen(_wnd.main_wnd, &pt);
416 OffsetRect(&_wnd.update_rect, pt.x, pt.y);
418 /* Create a device context that is clipped to the region we need to draw.
419 * GetDCEx 'consumes' the update region, so we may not destroy it ourself. */
420 HRGN rgn = CreateRectRgnIndirect(&_wnd.update_rect);
421 HDC dc = GetDCEx(_wnd.main_wnd, rgn, DCX_CLIPSIBLINGS | DCX_CLIPCHILDREN | DCX_INTERSECTRGN);
423 PaintWindow(dc);
425 /* Clear update rect. */
426 SetRectEmpty(&_wnd.update_rect);
427 ReleaseDC(_wnd.main_wnd, dc);
429 /* Flush GDI buffer to ensure drawing here doesn't conflict with any GDI usage in the main WndProc. */
430 GdiFlush();
432 _draw_mutex->WaitForSignal();
435 _draw_mutex->EndCritical();
436 _draw_thread->Exit();
439 /** Forward key presses to the window system. */
440 static LRESULT HandleCharMsg(uint keycode, WChar charcode)
442 #if !defined(UNICODE)
443 static char prev_char = 0;
445 char input[2] = {(char)charcode, 0};
446 int input_len = 1;
448 if (prev_char != 0) {
449 /* We stored a lead byte previously, combine it with this byte. */
450 input[0] = prev_char;
451 input[1] = (char)charcode;
452 input_len = 2;
453 } else if (IsDBCSLeadByte(charcode)) {
454 /* We got a lead byte, store and exit. */
455 prev_char = charcode;
456 return 0;
458 prev_char = 0;
460 wchar_t w[2]; // Can get up to two code points as a result.
461 int len = MultiByteToWideChar(CP_ACP, 0, input, input_len, w, 2);
462 switch (len) {
463 case 1: // Normal unicode character.
464 charcode = w[0];
465 break;
467 case 2: // Got an UTF-16 surrogate pair back.
468 charcode = Utf16DecodeSurrogate(w[0], w[1]);
469 break;
471 default: // Some kind of error.
472 DEBUG(driver, 1, "Invalid DBCS character sequence encountered, dropping input");
473 charcode = 0;
474 break;
476 #else
477 static WChar prev_char = 0;
479 /* Did we get a lead surrogate? If yes, store and exit. */
480 if (Utf16IsLeadSurrogate(charcode)) {
481 if (prev_char != 0) DEBUG(driver, 1, "Got two UTF-16 lead surrogates, dropping the first one");
482 prev_char = charcode;
483 return 0;
486 /* Stored lead surrogate and incoming trail surrogate? Combine and forward to input handling. */
487 if (prev_char != 0) {
488 if (Utf16IsTrailSurrogate(charcode)) {
489 charcode = Utf16DecodeSurrogate(prev_char, charcode);
490 } else {
491 DEBUG(driver, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
494 prev_char = 0;
495 #endif /* UNICODE */
497 HandleKeypress(keycode, charcode);
499 return 0;
502 #if !defined(WINCE) || _WIN32_WCE >= 0x400
503 /** Should we draw the composition string ourself, i.e is this a normal IME? */
504 static bool DrawIMECompositionString()
506 return (_imm_props & IME_PROP_AT_CARET) && !(_imm_props & IME_PROP_SPECIAL_UI);
509 /** Set position of the composition window to the caret position. */
510 static void SetCompositionPos(HWND hwnd)
512 HIMC hIMC = ImmGetContext(hwnd);
513 if (hIMC != NULL) {
514 COMPOSITIONFORM cf;
515 cf.dwStyle = CFS_POINT;
517 if (EditBoxInGlobalFocus()) {
518 /* Get caret position. */
519 Point pt = _focused_window->GetCaretPosition();
520 cf.ptCurrentPos.x = _focused_window->left + pt.x;
521 cf.ptCurrentPos.y = _focused_window->top + pt.y;
522 } else {
523 cf.ptCurrentPos.x = 0;
524 cf.ptCurrentPos.y = 0;
526 ImmSetCompositionWindow(hIMC, &cf);
528 ImmReleaseContext(hwnd, hIMC);
531 /** Set the position of the candidate window. */
532 static void SetCandidatePos(HWND hwnd)
534 HIMC hIMC = ImmGetContext(hwnd);
535 if (hIMC != NULL) {
536 CANDIDATEFORM cf;
537 cf.dwIndex = 0;
538 cf.dwStyle = CFS_EXCLUDE;
540 if (EditBoxInGlobalFocus()) {
541 Point pt = _focused_window->GetCaretPosition();
542 cf.ptCurrentPos.x = _focused_window->left + pt.x;
543 cf.ptCurrentPos.y = _focused_window->top + pt.y;
544 if (_focused_window->window_class == WC_CONSOLE) {
545 cf.rcArea.left = _focused_window->left;
546 cf.rcArea.top = _focused_window->top;
547 cf.rcArea.right = _focused_window->left + _focused_window->width;
548 cf.rcArea.bottom = _focused_window->top + _focused_window->height;
549 } else {
550 cf.rcArea.left = _focused_window->left + _focused_window->nested_focus->pos_x;
551 cf.rcArea.top = _focused_window->top + _focused_window->nested_focus->pos_y;
552 cf.rcArea.right = cf.rcArea.left + _focused_window->nested_focus->current_x;
553 cf.rcArea.bottom = cf.rcArea.top + _focused_window->nested_focus->current_y;
555 } else {
556 cf.ptCurrentPos.x = 0;
557 cf.ptCurrentPos.y = 0;
558 SetRectEmpty(&cf.rcArea);
560 ImmSetCandidateWindow(hIMC, &cf);
562 ImmReleaseContext(hwnd, hIMC);
565 /** Handle WM_IME_COMPOSITION messages. */
566 static LRESULT HandleIMEComposition(HWND hwnd, WPARAM wParam, LPARAM lParam)
568 HIMC hIMC = ImmGetContext(hwnd);
570 if (hIMC != NULL) {
571 if (lParam & GCS_RESULTSTR) {
572 /* Read result string from the IME. */
573 LONG len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0); // Length is always in bytes, even in UNICODE build.
574 TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR));
575 len = ImmGetCompositionString(hIMC, GCS_RESULTSTR, str, len);
576 str[len / sizeof(TCHAR)] = '\0';
578 /* Transmit text to windowing system. */
579 if (len > 0) {
580 HandleTextInput(NULL, true); // Clear marked string.
581 HandleTextInput(FS2OTTD(str));
583 SetCompositionPos(hwnd);
585 /* Don't pass the result string on to the default window proc. */
586 lParam &= ~(GCS_RESULTSTR | GCS_RESULTCLAUSE | GCS_RESULTREADCLAUSE | GCS_RESULTREADSTR);
589 if ((lParam & GCS_COMPSTR) && DrawIMECompositionString()) {
590 /* Read composition string from the IME. */
591 LONG len = ImmGetCompositionString(hIMC, GCS_COMPSTR, NULL, 0); // Length is always in bytes, even in UNICODE build.
592 TCHAR *str = (TCHAR *)_alloca(len + sizeof(TCHAR));
593 len = ImmGetCompositionString(hIMC, GCS_COMPSTR, str, len);
594 str[len / sizeof(TCHAR)] = '\0';
596 if (len > 0) {
597 static char utf8_buf[1024];
598 convert_from_fs(str, utf8_buf, lengthof(utf8_buf));
600 /* Convert caret position from bytes in the input string to a position in the UTF-8 encoded string. */
601 LONG caret_bytes = ImmGetCompositionString(hIMC, GCS_CURSORPOS, NULL, 0);
602 const char *caret = utf8_buf;
603 for (const TCHAR *c = str; *c != '\0' && *caret != '\0' && caret_bytes > 0; c++, caret_bytes--) {
604 /* Skip DBCS lead bytes or leading surrogates. */
605 #ifdef UNICODE
606 if (Utf16IsLeadSurrogate(*c)) {
607 #else
608 if (IsDBCSLeadByte(*c)) {
609 #endif
610 c++;
611 caret_bytes--;
613 Utf8Consume(&caret);
616 HandleTextInput(utf8_buf, true, caret);
617 } else {
618 HandleTextInput(NULL, true);
621 lParam &= ~(GCS_COMPSTR | GCS_COMPATTR | GCS_COMPCLAUSE | GCS_CURSORPOS | GCS_DELTASTART);
624 ImmReleaseContext(hwnd, hIMC);
626 return lParam != 0 ? DefWindowProc(hwnd, WM_IME_COMPOSITION, wParam, lParam) : 0;
629 /** Clear the current composition string. */
630 static void CancelIMEComposition(HWND hwnd)
632 HIMC hIMC = ImmGetContext(hwnd);
633 if (hIMC != NULL) ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
634 ImmReleaseContext(hwnd, hIMC);
635 /* Clear any marked string from the current edit box. */
636 HandleTextInput(NULL, true);
639 #else
641 static bool DrawIMECompositionString() { return false; }
642 static void SetCompositionPos(HWND hwnd) {}
643 static void SetCandidatePos(HWND hwnd) {}
644 static void CancelIMEComposition(HWND hwnd) {}
646 #endif /* !defined(WINCE) || _WIN32_WCE >= 0x400 */
648 static LRESULT CALLBACK WndProcGdi(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
650 static uint32 keycode = 0;
651 static bool console = false;
652 static bool in_sizemove = false;
654 switch (msg) {
655 case WM_CREATE:
656 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
657 SetCompositionPos(hwnd);
658 #if !defined(WINCE) || _WIN32_WCE >= 0x400
659 _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
660 #endif
661 break;
663 case WM_ENTERSIZEMOVE:
664 in_sizemove = true;
665 break;
667 case WM_EXITSIZEMOVE:
668 in_sizemove = false;
669 break;
671 case WM_PAINT:
672 if (!in_sizemove && _draw_mutex != NULL && !HasModalProgress()) {
673 /* Get the union of the old update rect and the new update rect. */
674 RECT r;
675 GetUpdateRect(hwnd, &r, FALSE);
676 UnionRect(&_wnd.update_rect, &_wnd.update_rect, &r);
678 /* Mark the window as updated, otherwise Windows would send more WM_PAINT messages. */
679 ValidateRect(hwnd, NULL);
680 _draw_mutex->SendSignal();
681 } else {
682 PAINTSTRUCT ps;
684 BeginPaint(hwnd, &ps);
685 PaintWindow(ps.hdc);
686 EndPaint(hwnd, &ps);
688 return 0;
690 case WM_PALETTECHANGED:
691 if ((HWND)wParam == hwnd) return 0;
692 FALLTHROUGH;
694 case WM_QUERYNEWPALETTE: {
695 HDC hDC = GetWindowDC(hwnd);
696 HPALETTE hOldPalette = SelectPalette(hDC, _wnd.gdi_palette, FALSE);
697 UINT nChanged = RealizePalette(hDC);
699 SelectPalette(hDC, hOldPalette, TRUE);
700 ReleaseDC(hwnd, hDC);
701 if (nChanged != 0) InvalidateRect(hwnd, NULL, FALSE);
702 return 0;
705 case WM_CLOSE:
706 HandleExitGameRequest();
707 return 0;
709 case WM_DESTROY:
710 if (_window_maximize) _cur_resolution = _bck_resolution;
711 return 0;
713 case WM_LBUTTONDOWN:
714 SetCapture(hwnd);
715 _left_button_down = true;
716 HandleMouseEvents();
717 return 0;
719 case WM_LBUTTONUP:
720 ReleaseCapture();
721 _left_button_down = false;
722 _left_button_clicked = false;
723 HandleMouseEvents();
724 return 0;
726 case WM_RBUTTONDOWN:
727 SetCapture(hwnd);
728 _right_button_down = true;
729 _right_button_clicked = true;
730 HandleMouseEvents();
731 return 0;
733 case WM_RBUTTONUP:
734 ReleaseCapture();
735 _right_button_down = false;
736 HandleMouseEvents();
737 return 0;
739 case WM_MOUSELEAVE:
740 UndrawMouseCursor();
741 _cursor.in_window = false;
743 if (!_left_button_down && !_right_button_down) MyShowCursor(true);
744 return 0;
746 case WM_MOUSEMOVE: {
747 int x = (int16)LOWORD(lParam);
748 int y = (int16)HIWORD(lParam);
750 /* If the mouse was not in the window and it has moved it means it has
751 * come into the window, so start drawing the mouse. Also start
752 * tracking the mouse for exiting the window */
753 if (!_cursor.in_window) {
754 _cursor.in_window = true;
755 SetTimer(hwnd, TID_POLLMOUSE, MOUSE_POLL_DELAY, (TIMERPROC)TrackMouseTimerProc);
758 if (_cursor.fix_at) {
759 /* Get all queued mouse events now in case we have to warp the cursor. In the
760 * end, we only care about the current mouse position and not bygone events. */
761 MSG m;
762 while (PeekMessage(&m, hwnd, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE | PM_NOYIELD | PM_QS_INPUT)) {
763 x = (int16)LOWORD(m.lParam);
764 y = (int16)HIWORD(m.lParam);
768 if (_cursor.UpdateCursorPosition(x, y, false)) {
769 POINT pt;
770 pt.x = _cursor.pos.x;
771 pt.y = _cursor.pos.y;
772 ClientToScreen(hwnd, &pt);
773 SetCursorPos(pt.x, pt.y);
775 MyShowCursor(false);
776 HandleMouseEvents();
777 return 0;
780 #if !defined(WINCE) || _WIN32_WCE >= 0x400
781 case WM_INPUTLANGCHANGE:
782 _imm_props = ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY);
783 break;
785 case WM_IME_SETCONTEXT:
786 /* Don't show the composition window if we draw the string ourself. */
787 if (DrawIMECompositionString()) lParam &= ~ISC_SHOWUICOMPOSITIONWINDOW;
788 break;
790 case WM_IME_STARTCOMPOSITION:
791 SetCompositionPos(hwnd);
792 if (DrawIMECompositionString()) return 0;
793 break;
795 case WM_IME_COMPOSITION:
796 return HandleIMEComposition(hwnd, wParam, lParam);
798 case WM_IME_ENDCOMPOSITION:
799 /* Clear any pending composition string. */
800 HandleTextInput(NULL, true);
801 if (DrawIMECompositionString()) return 0;
802 break;
804 case WM_IME_NOTIFY:
805 if (wParam == IMN_OPENCANDIDATE) SetCandidatePos(hwnd);
806 break;
808 #if !defined(UNICODE)
809 case WM_IME_CHAR:
810 if (GB(wParam, 8, 8) != 0) {
811 /* DBCS character, send lead byte first. */
812 HandleCharMsg(0, GB(wParam, 8, 8));
814 HandleCharMsg(0, GB(wParam, 0, 8));
815 return 0;
816 #endif
817 #endif
819 case WM_DEADCHAR:
820 console = GB(lParam, 16, 8) == 41;
821 return 0;
823 case WM_CHAR: {
824 uint scancode = GB(lParam, 16, 8);
825 uint charcode = wParam;
827 /* If the console key is a dead-key, we need to press it twice to get a WM_CHAR message.
828 * But we then get two WM_CHAR messages, so ignore the first one */
829 if (console && scancode == 41) {
830 console = false;
831 return 0;
834 /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
835 * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
836 uint cur_keycode = keycode;
837 keycode = 0;
839 return HandleCharMsg(cur_keycode, charcode);
842 case WM_KEYDOWN: {
843 /* No matter the keyboard layout, we will map the '~' to the console. */
844 uint scancode = GB(lParam, 16, 8);
845 keycode = scancode == 41 ? (uint)WKC_BACKQUOTE : MapWindowsKey(wParam);
847 /* Silently drop all messages handled by WM_CHAR. */
848 MSG msg;
849 if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
850 if ((msg.message == WM_CHAR || msg.message == WM_DEADCHAR) && GB(lParam, 16, 8) == GB(msg.lParam, 16, 8)) {
851 return 0;
855 uint charcode = MapVirtualKey(wParam, MAPVK_VK_TO_CHAR);
857 /* No character translation? */
858 if (charcode == 0) {
859 HandleKeypress(keycode, 0);
860 return 0;
863 /* Is the console key a dead key? If yes, ignore the first key down event. */
864 if (HasBit(charcode, 31) && !console) {
865 if (scancode == 41) {
866 console = true;
867 return 0;
870 console = false;
872 /* IMEs and other input methods sometimes send a WM_CHAR without a WM_KEYDOWN,
873 * clear the keycode so a previous WM_KEYDOWN doesn't become 'stuck'. */
874 uint cur_keycode = keycode;
875 keycode = 0;
877 return HandleCharMsg(cur_keycode, LOWORD(charcode));
880 case WM_SYSKEYDOWN: // user presses F10 or Alt, both activating the title-menu
881 switch (wParam) {
882 case VK_RETURN:
883 case 'F': // Full Screen on ALT + ENTER/F
884 ToggleFullScreen(!_wnd.fullscreen);
885 return 0;
887 case VK_MENU: // Just ALT
888 return 0; // do nothing
890 case VK_F10: // F10, ignore activation of menu
891 HandleKeypress(MapWindowsKey(wParam), 0);
892 return 0;
894 default: // ALT in combination with something else
895 HandleKeypress(MapWindowsKey(wParam), 0);
896 break;
898 break;
900 case WM_SIZE:
901 if (wParam != SIZE_MINIMIZED) {
902 /* Set maximized flag when we maximize (obviously), but also when we
903 * switched to fullscreen from a maximized state */
904 _window_maximize = (wParam == SIZE_MAXIMIZED || (_window_maximize && _fullscreen));
905 if (_window_maximize || _fullscreen) _bck_resolution = _cur_resolution;
906 ClientSizeChanged(LOWORD(lParam), HIWORD(lParam));
908 return 0;
910 #if !defined(WINCE)
911 case WM_SIZING: {
912 RECT *r = (RECT*)lParam;
913 RECT r2;
914 int w, h;
916 SetRect(&r2, 0, 0, 0, 0);
917 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
919 w = r->right - r->left - (r2.right - r2.left);
920 h = r->bottom - r->top - (r2.bottom - r2.top);
921 w = max(w, 64);
922 h = max(h, 64);
923 SetRect(&r2, 0, 0, w, h);
925 AdjustWindowRect(&r2, GetWindowLong(hwnd, GWL_STYLE), FALSE);
926 w = r2.right - r2.left;
927 h = r2.bottom - r2.top;
929 switch (wParam) {
930 case WMSZ_BOTTOM:
931 r->bottom = r->top + h;
932 break;
934 case WMSZ_BOTTOMLEFT:
935 r->bottom = r->top + h;
936 r->left = r->right - w;
937 break;
939 case WMSZ_BOTTOMRIGHT:
940 r->bottom = r->top + h;
941 r->right = r->left + w;
942 break;
944 case WMSZ_LEFT:
945 r->left = r->right - w;
946 break;
948 case WMSZ_RIGHT:
949 r->right = r->left + w;
950 break;
952 case WMSZ_TOP:
953 r->top = r->bottom - h;
954 break;
956 case WMSZ_TOPLEFT:
957 r->top = r->bottom - h;
958 r->left = r->right - w;
959 break;
961 case WMSZ_TOPRIGHT:
962 r->top = r->bottom - h;
963 r->right = r->left + w;
964 break;
966 return TRUE;
968 #endif
970 /* needed for wheel */
971 #if !defined(WM_MOUSEWHEEL)
972 # define WM_MOUSEWHEEL 0x020A
973 #endif /* WM_MOUSEWHEEL */
974 #if !defined(GET_WHEEL_DELTA_WPARAM)
975 # define GET_WHEEL_DELTA_WPARAM(wparam) ((short)HIWORD(wparam))
976 #endif /* GET_WHEEL_DELTA_WPARAM */
978 case WM_MOUSEWHEEL: {
979 int delta = GET_WHEEL_DELTA_WPARAM(wParam);
981 if (delta < 0) {
982 _cursor.wheel++;
983 } else if (delta > 0) {
984 _cursor.wheel--;
986 HandleMouseEvents();
987 return 0;
990 case WM_SETFOCUS:
991 _wnd.has_focus = true;
992 SetCompositionPos(hwnd);
993 break;
995 case WM_KILLFOCUS:
996 _wnd.has_focus = false;
997 break;
999 #if !defined(WINCE)
1000 case WM_ACTIVATE: {
1001 /* Don't do anything if we are closing openttd */
1002 if (_exit_game) break;
1004 bool active = (LOWORD(wParam) != WA_INACTIVE);
1005 bool minimized = (HIWORD(wParam) != 0);
1006 if (_wnd.fullscreen) {
1007 if (active && minimized) {
1008 /* Restore the game window */
1009 ShowWindow(hwnd, SW_RESTORE);
1010 MakeWindow (true);
1011 } else if (!active && !minimized) {
1012 /* Minimise the window and restore desktop */
1013 ShowWindow(hwnd, SW_MINIMIZE);
1014 ChangeDisplaySettings(NULL, 0);
1017 break;
1019 #endif
1022 return DefWindowProc(hwnd, msg, wParam, lParam);
1025 static void RegisterWndClass()
1027 static bool registered = false;
1029 if (!registered) {
1030 HINSTANCE hinst = GetModuleHandle(NULL);
1031 WNDCLASS wnd = {
1032 CS_OWNDC,
1033 WndProcGdi,
1036 hinst,
1037 LoadIcon(hinst, MAKEINTRESOURCE(100)),
1038 LoadCursor(NULL, IDC_ARROW),
1041 _T("OTTD")
1044 registered = true;
1045 if (!RegisterClass(&wnd)) usererror("RegisterClass failed");
1049 static bool AllocateDibSection(int w, int h, bool force)
1051 BITMAPINFO *bi;
1052 HDC dc;
1053 uint bpp = Blitter::get()->screen_depth;
1055 w = max(w, 64);
1056 h = max(h, 64);
1058 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
1060 if (!force && w == _screen_width && h == _screen_height) return false;
1062 bi = (BITMAPINFO*)alloca(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
1063 memset(bi, 0, sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
1064 bi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1066 bi->bmiHeader.biWidth = _wnd.width = w;
1067 bi->bmiHeader.biHeight = -(_wnd.height = h);
1069 bi->bmiHeader.biPlanes = 1;
1070 bi->bmiHeader.biBitCount = Blitter::get()->screen_depth;
1071 bi->bmiHeader.biCompression = BI_RGB;
1073 if (_wnd.dib_sect) DeleteObject(_wnd.dib_sect);
1075 dc = GetDC(0);
1076 _wnd.dib_sect = CreateDIBSection(dc, bi, DIB_RGB_COLORS, (VOID**)&_wnd.buffer_bits, NULL, 0);
1077 if (_wnd.dib_sect == NULL) usererror("CreateDIBSection failed");
1078 ReleaseDC(0, dc);
1080 _screen_surface.reset (Blitter::get()->create (_wnd.buffer_bits,
1081 w, h, (bpp == 8) ? Align(w, 4) : w, true));
1082 _screen_width = w;
1083 _screen_height = h;
1085 return true;
1088 static const Dimension default_resolutions[] = {
1089 { 640, 480 },
1090 { 800, 600 },
1091 { 1024, 768 },
1092 { 1152, 864 },
1093 { 1280, 800 },
1094 { 1280, 960 },
1095 { 1280, 1024 },
1096 { 1400, 1050 },
1097 { 1600, 1200 },
1098 { 1680, 1050 },
1099 { 1920, 1200 }
1102 static void FindResolutions()
1104 uint n = 0;
1105 #if defined(WINCE)
1106 /* EnumDisplaySettingsW is only supported in CE 4.2+
1107 * XXX -- One might argue that we assume 4.2+ on every system. Then we can use this function safely */
1108 #else
1109 uint i;
1110 DEVMODEA dm;
1112 /* Check modes for the relevant fullscreen bpp */
1113 uint bpp = _support8bpp != S8BPP_HARDWARE ? 32 : Blitter::get()->screen_depth;
1115 /* XXX - EnumDisplaySettingsW crashes with unicows.dll on Windows95
1116 * Doesn't really matter since we don't pass a string anyways, but still
1117 * a letdown */
1118 for (i = 0; EnumDisplaySettingsA(NULL, i, &dm) != 0; i++) {
1119 if (dm.dmBitsPerPel == bpp &&
1120 dm.dmPelsWidth >= 640 && dm.dmPelsHeight >= 480) {
1121 uint j;
1123 for (j = 0; j < n; j++) {
1124 if (_resolutions[j].width == dm.dmPelsWidth && _resolutions[j].height == dm.dmPelsHeight) break;
1127 /* In the previous loop we have checked already existing/added resolutions if
1128 * they are the same as the new ones. If this is not the case (j == n); we have
1129 * looped all and found none, add the new one to the list. If we have reached the
1130 * maximum amount of resolutions, then quit querying the display */
1131 if (j == n) {
1132 _resolutions[j].width = dm.dmPelsWidth;
1133 _resolutions[j].height = dm.dmPelsHeight;
1134 if (++n == lengthof(_resolutions)) break;
1138 #endif
1140 /* We have found no resolutions, show the default list */
1141 if (n == 0) {
1142 memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
1143 n = lengthof(default_resolutions);
1146 _num_resolutions = n;
1147 SortResolutions(_num_resolutions);
1150 /** The factory for Windows' video driver. */
1151 static VideoDriverFactory <VideoDriver_Win32>
1152 iFVideoDriver_Win32 (10, "win32", "Win32 GDI Video Driver");
1154 const char *VideoDriver_Win32::Start(const char * const *parm)
1156 memset(&_wnd, 0, sizeof(_wnd));
1158 RegisterWndClass();
1160 MakePalette();
1162 FindResolutions();
1164 DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
1166 /* fullscreen uses those */
1167 _wnd.width_org = _cur_resolution.width;
1168 _wnd.height_org = _cur_resolution.height;
1170 AllocateDibSection(_cur_resolution.width, _cur_resolution.height);
1171 MakeWindow (_fullscreen);
1173 MarkWholeScreenDirty();
1175 _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL && GetCPUCoreCount() > 1;
1177 return NULL;
1180 void VideoDriver_Win32::Stop()
1182 DeleteObject(_wnd.gdi_palette);
1183 DeleteObject(_wnd.dib_sect);
1184 DestroyWindow(_wnd.main_wnd);
1186 #if !defined(WINCE)
1187 if (_wnd.fullscreen) ChangeDisplaySettings(NULL, 0);
1188 #endif
1189 MyShowCursor(true);
1192 void VideoDriver_Win32::MakeDirty(int left, int top, int width, int height)
1194 RECT r = { left, top, left + width, top + height };
1196 InvalidateRect(_wnd.main_wnd, &r, FALSE);
1199 static void CheckPaletteAnim()
1201 if (_cur_palette_count_dirty == 0) return;
1203 assert_compile (sizeof(_local_palette) == sizeof(_cur_palette));
1204 memcpy (_local_palette, _cur_palette, sizeof(_local_palette));
1205 _local_palette_first_dirty = _cur_palette_first_dirty;
1206 _local_palette_count_dirty = _cur_palette_count_dirty;
1207 InvalidateRect(_wnd.main_wnd, NULL, FALSE);
1210 void VideoDriver_Win32::MainLoop()
1212 MSG mesg;
1213 uint32 cur_ticks = GetTickCount();
1214 uint32 last_cur_ticks = cur_ticks;
1215 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
1217 if (_draw_threaded) {
1218 /* Initialise the mutex first, because that's the thing we *need*
1219 * directly in the newly created thread. */
1220 _draw_mutex = ThreadMutex::New();
1221 _draw_thread_initialized = CreateEvent(NULL, FALSE, FALSE, NULL);
1222 if (_draw_mutex == NULL || _draw_thread_initialized == NULL) {
1223 _draw_threaded = false;
1224 } else {
1225 _draw_continue = true;
1226 _draw_threaded = ThreadObject::New(&PaintWindowThread, NULL, &_draw_thread, "ottd:draw-win32");
1228 /* Free the mutex if we won't be able to use it. */
1229 if (!_draw_threaded) {
1230 delete _draw_mutex;
1231 _draw_mutex = NULL;
1232 CloseHandle(_draw_thread_initialized);
1233 _draw_thread_initialized = NULL;
1234 } else {
1235 DEBUG(driver, 1, "Threaded drawing enabled");
1236 /* Wait till the draw thread has started itself. */
1237 WaitForSingleObject(_draw_thread_initialized, INFINITE);
1238 _draw_mutex->BeginCritical();
1243 _wnd.running = true;
1245 CheckPaletteAnim();
1246 for (;;) {
1247 uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
1249 while (PeekMessage(&mesg, NULL, 0, 0, PM_REMOVE)) {
1250 InteractiveRandom(); // randomness
1251 /* Convert key messages to char messages if we want text input. */
1252 if (EditBoxInGlobalFocus()) TranslateMessage(&mesg);
1253 DispatchMessage(&mesg);
1255 if (_exit_game) return;
1257 #if defined(_DEBUG)
1258 if (_wnd.has_focus && GetAsyncKeyState(VK_SHIFT) < 0 &&
1259 #else
1260 /* Speed up using TAB, but disable for ALT+TAB of course */
1261 if (_wnd.has_focus && GetAsyncKeyState(VK_TAB) < 0 && GetAsyncKeyState(VK_MENU) >= 0 &&
1262 #endif
1263 !_networking && _game_mode != GM_MENU) {
1264 _fast_forward |= 2;
1265 } else if (_fast_forward & 2) {
1266 _fast_forward = 0;
1269 cur_ticks = GetTickCount();
1270 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
1271 _realtime_tick += cur_ticks - last_cur_ticks;
1272 last_cur_ticks = cur_ticks;
1273 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
1275 bool old_ctrl_pressed = _ctrl_pressed;
1277 _ctrl_pressed = _wnd.has_focus && GetAsyncKeyState(VK_CONTROL)<0;
1278 _shift_pressed = _wnd.has_focus && GetAsyncKeyState(VK_SHIFT)<0;
1280 /* determine which directional keys are down */
1281 if (_wnd.has_focus) {
1282 _dirkeys =
1283 (GetAsyncKeyState(VK_LEFT) < 0 ? 1 : 0) +
1284 (GetAsyncKeyState(VK_UP) < 0 ? 2 : 0) +
1285 (GetAsyncKeyState(VK_RIGHT) < 0 ? 4 : 0) +
1286 (GetAsyncKeyState(VK_DOWN) < 0 ? 8 : 0);
1287 } else {
1288 _dirkeys = 0;
1291 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
1293 #if !defined(WINCE)
1294 /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
1295 GdiFlush();
1296 #endif
1298 /* The game loop is the part that can run asynchronously.
1299 * The rest except sleeping can't. */
1300 if (_draw_threaded) _draw_mutex->EndCritical();
1301 GameLoop();
1302 if (_draw_threaded) _draw_mutex->BeginCritical();
1304 if (_force_full_redraw) MarkWholeScreenDirty();
1306 UpdateWindows();
1307 CheckPaletteAnim();
1308 } else {
1309 #if !defined(WINCE)
1310 /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
1311 GdiFlush();
1312 #endif
1314 /* Release the thread while sleeping */
1315 if (_draw_threaded) _draw_mutex->EndCritical();
1316 Sleep(1);
1317 if (_draw_threaded) _draw_mutex->BeginCritical();
1319 NetworkDrawChatMessage();
1320 DrawMouseCursor();
1324 if (_draw_threaded) {
1325 _draw_continue = false;
1326 /* Sending signal if there is no thread blocked
1327 * is very valid and results in noop */
1328 _draw_mutex->SendSignal();
1329 _draw_mutex->EndCritical();
1330 _draw_thread->Join();
1332 CloseHandle(_draw_thread_initialized);
1333 delete _draw_mutex;
1334 delete _draw_thread;
1338 static bool ChangeResolution (int w, int h)
1340 if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
1341 if (_window_maximize) ShowWindow(_wnd.main_wnd, SW_SHOWNORMAL);
1343 _wnd.width = _wnd.width_org = w;
1344 _wnd.height = _wnd.height_org = h;
1346 bool ret = MakeWindow (_fullscreen); // _wnd.fullscreen screws up ingame resolution switching
1347 if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
1348 return ret;
1351 bool VideoDriver_Win32::ChangeResolution(int w, int h)
1353 return ::ChangeResolution (w, h);
1356 bool VideoDriver_Win32::ToggleFullscreen(bool full_screen)
1358 if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
1359 bool ret = MakeWindow (full_screen);
1360 if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
1361 return ret;
1365 * Callback invoked after the blitter was changed.
1366 * @return True if no error.
1368 static bool AfterBlitterChange (void)
1370 return AllocateDibSection (_screen_width, _screen_height, true) && MakeWindow (_fullscreen);
1374 * Switch to a new blitter.
1375 * @param blitter The blitter to switch to.
1376 * @return False if switching failed and the old blitter could not be restored.
1378 bool VideoDriver_Win32::SwitchBlitter (const Blitter::Info *blitter)
1380 const Blitter::Info *old = Blitter::get();
1382 if (_draw_mutex != NULL) _draw_mutex->BeginCritical (true);
1384 Blitter::select (blitter);
1386 bool ret = AfterBlitterChange() ||
1387 /* Failed to switch blitter, let's hope we can return
1388 * to the old one. */
1389 (Blitter::select (old), AfterBlitterChange());
1391 if (_draw_mutex != NULL) _draw_mutex->EndCritical (true);
1393 return ret;
1396 void VideoDriver_Win32::EditBoxLostFocus()
1398 if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
1399 CancelIMEComposition(_wnd.main_wnd);
1400 SetCompositionPos(_wnd.main_wnd);
1401 SetCandidatePos(_wnd.main_wnd);
1402 if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);