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/>.
10 /** @file win32_v.cpp Implementation of the Windows (GDI) video driver. */
12 #include "../stdafx.h"
14 #include "../openttd.h"
15 #include "../gfx_func.h"
16 #include "../os/windows/win32.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"
31 /* Missing define in MinGW headers. */
32 #ifndef MAPVK_VK_TO_CHAR
33 #define MAPVK_VK_TO_CHAR (2)
37 #define PM_QS_INPUT 0x20000
55 bool _force_full_redraw
;
56 bool _window_maximize
;
58 static Dimension _bck_resolution
;
59 #if !defined(WINCE) || _WIN32_WCE >= 0x400
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
)
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);
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
),
152 AM(VK_F1
, VK_F12
, WKC_F1
, WKC_F12
),
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 */
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
),
176 static uint
MapWindowsKey(uint sym
)
178 const VkMapping
*map
;
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
;
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
;
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;
213 /* Keep this function here..
214 * It allows you to redraw the screen from within the MSVC debugger */
215 int RedrawScreenDebug()
220 HPALETTE old_palette
;
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
);
233 ReleaseDC(_wnd
.main_wnd
, dc
);
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
243 #define TID_POLLMOUSE 1
244 #define MOUSE_POLL_DELAY 75
246 static void CALLBACK
TrackMouseTimerProc(HWND hwnd
, UINT msg
, UINT event
, DWORD time
)
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);
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
);
282 /* WinCE is always fullscreen */
287 memset(&settings
, 0, sizeof(settings
));
288 settings
.dmSize
= sizeof(settings
);
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
) {
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
;
331 DWORD style
, showstyle
;
334 showstyle
= SW_SHOWNORMAL
;
335 _wnd
.fullscreen
= full_screen
;
336 if (_wnd
.fullscreen
) {
338 SetRect(&r
, 0, 0, _wnd
.width_org
, _wnd
.height_org
);
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
);
347 AdjustWindowRect(&r
, style
, FALSE
);
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
);
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
);
384 case Blitter::PALETTE_ANIMATION_BLITTER
:
385 VideoDriver::PaletteAnimate (_local_palette
);
388 case Blitter::PALETTE_ANIMATION_NONE
:
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
);
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. */
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
);
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. */
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};
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
;
453 } else if (IsDBCSLeadByte(charcode
)) {
454 /* We got a lead byte, store and exit. */
455 prev_char
= charcode
;
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);
463 case 1: // Normal unicode character.
467 case 2: // Got an UTF-16 surrogate pair back.
468 charcode
= Utf16DecodeSurrogate(w
[0], w
[1]);
471 default: // Some kind of error.
472 DEBUG(driver
, 1, "Invalid DBCS character sequence encountered, dropping input");
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
;
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
);
491 DEBUG(driver
, 1, "Got an UTF-16 lead surrogate without a trail surrogate, dropping the lead surrogate");
497 HandleKeypress(keycode
, charcode
);
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
);
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
;
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
);
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
;
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
;
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
);
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. */
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';
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. */
606 if (Utf16IsLeadSurrogate(*c
)) {
608 if (IsDBCSLeadByte(*c
)) {
616 HandleTextInput(utf8_buf
, true, caret
);
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);
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;
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
);
663 case WM_ENTERSIZEMOVE
:
667 case WM_EXITSIZEMOVE
:
672 if (!in_sizemove
&& _draw_mutex
!= NULL
&& !HasModalProgress()) {
673 /* Get the union of the old update rect and the new update rect. */
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();
684 BeginPaint(hwnd
, &ps
);
690 case WM_PALETTECHANGED
:
691 if ((HWND
)wParam
== hwnd
) return 0;
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
);
706 HandleExitGameRequest();
710 if (_window_maximize
) _cur_resolution
= _bck_resolution
;
715 _left_button_down
= true;
721 _left_button_down
= false;
722 _left_button_clicked
= false;
728 _right_button_down
= true;
729 _right_button_clicked
= true;
735 _right_button_down
= false;
741 _cursor
.in_window
= false;
743 if (!_left_button_down
&& !_right_button_down
) MyShowCursor(true);
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. */
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)) {
770 pt
.x
= _cursor
.pos
.x
;
771 pt
.y
= _cursor
.pos
.y
;
772 ClientToScreen(hwnd
, &pt
);
773 SetCursorPos(pt
.x
, pt
.y
);
780 #if !defined(WINCE) || _WIN32_WCE >= 0x400
781 case WM_INPUTLANGCHANGE
:
782 _imm_props
= ImmGetProperty(GetKeyboardLayout(0), IGP_PROPERTY
);
785 case WM_IME_SETCONTEXT
:
786 /* Don't show the composition window if we draw the string ourself. */
787 if (DrawIMECompositionString()) lParam
&= ~ISC_SHOWUICOMPOSITIONWINDOW
;
790 case WM_IME_STARTCOMPOSITION
:
791 SetCompositionPos(hwnd
);
792 if (DrawIMECompositionString()) return 0;
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;
805 if (wParam
== IMN_OPENCANDIDATE
) SetCandidatePos(hwnd
);
808 #if !defined(UNICODE)
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));
820 console
= GB(lParam
, 16, 8) == 41;
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) {
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
;
839 return HandleCharMsg(cur_keycode
, charcode
);
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. */
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)) {
855 uint charcode
= MapVirtualKey(wParam
, MAPVK_VK_TO_CHAR
);
857 /* No character translation? */
859 HandleKeypress(keycode
, 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) {
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
;
877 return HandleCharMsg(cur_keycode
, LOWORD(charcode
));
880 case WM_SYSKEYDOWN
: // user presses F10 or Alt, both activating the title-menu
883 case 'F': // Full Screen on ALT + ENTER/F
884 ToggleFullScreen(!_wnd
.fullscreen
);
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);
894 default: // ALT in combination with something else
895 HandleKeypress(MapWindowsKey(wParam
), 0);
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
));
912 RECT
*r
= (RECT
*)lParam
;
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
);
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
;
931 r
->bottom
= r
->top
+ h
;
934 case WMSZ_BOTTOMLEFT
:
935 r
->bottom
= r
->top
+ h
;
936 r
->left
= r
->right
- w
;
939 case WMSZ_BOTTOMRIGHT
:
940 r
->bottom
= r
->top
+ h
;
941 r
->right
= r
->left
+ w
;
945 r
->left
= r
->right
- w
;
949 r
->right
= r
->left
+ w
;
953 r
->top
= r
->bottom
- h
;
957 r
->top
= r
->bottom
- h
;
958 r
->left
= r
->right
- w
;
962 r
->top
= r
->bottom
- h
;
963 r
->right
= r
->left
+ w
;
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
);
983 } else if (delta
> 0) {
991 _wnd
.has_focus
= true;
992 SetCompositionPos(hwnd
);
996 _wnd
.has_focus
= false;
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
);
1011 } else if (!active
&& !minimized
) {
1012 /* Minimise the window and restore desktop */
1013 ShowWindow(hwnd
, SW_MINIMIZE
);
1014 ChangeDisplaySettings(NULL
, 0);
1022 return DefWindowProc(hwnd
, msg
, wParam
, lParam
);
1025 static void RegisterWndClass()
1027 static bool registered
= false;
1030 HINSTANCE hinst
= GetModuleHandle(NULL
);
1037 LoadIcon(hinst
, MAKEINTRESOURCE(100)),
1038 LoadCursor(NULL
, IDC_ARROW
),
1045 if (!RegisterClass(&wnd
)) usererror("RegisterClass failed");
1049 static bool AllocateDibSection(int w
, int h
, bool force
)
1053 uint bpp
= Blitter::get()->screen_depth
;
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
);
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");
1080 _screen_surface
.reset (Blitter::get()->create (_wnd
.buffer_bits
,
1081 w
, h
, (bpp
== 8) ? Align(w
, 4) : w
, true));
1088 static const Dimension default_resolutions
[] = {
1102 static void FindResolutions()
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 */
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
1118 for (i
= 0; EnumDisplaySettingsA(NULL
, i
, &dm
) != 0; i
++) {
1119 if (dm
.dmBitsPerPel
== bpp
&&
1120 dm
.dmPelsWidth
>= 640 && dm
.dmPelsHeight
>= 480) {
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 */
1132 _resolutions
[j
].width
= dm
.dmPelsWidth
;
1133 _resolutions
[j
].height
= dm
.dmPelsHeight
;
1134 if (++n
== lengthof(_resolutions
)) break;
1140 /* We have found no resolutions, show the default list */
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
));
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;
1180 void VideoDriver_Win32::Stop()
1182 DeleteObject(_wnd
.gdi_palette
);
1183 DeleteObject(_wnd
.dib_sect
);
1184 DestroyWindow(_wnd
.main_wnd
);
1187 if (_wnd
.fullscreen
) ChangeDisplaySettings(NULL
, 0);
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()
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;
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
) {
1232 CloseHandle(_draw_thread_initialized
);
1233 _draw_thread_initialized
= NULL
;
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;
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;
1258 if (_wnd
.has_focus
&& GetAsyncKeyState(VK_SHIFT
) < 0 &&
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 &&
1263 !_networking
&& _game_mode
!= GM_MENU
) {
1265 } else if (_fast_forward
& 2) {
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
) {
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);
1291 if (old_ctrl_pressed
!= _ctrl_pressed
) HandleCtrlChanged();
1294 /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
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();
1302 if (_draw_threaded
) _draw_mutex
->BeginCritical();
1304 if (_force_full_redraw
) MarkWholeScreenDirty();
1310 /* Flush GDI buffer to ensure we don't conflict with the drawing thread. */
1314 /* Release the thread while sleeping */
1315 if (_draw_threaded
) _draw_mutex
->EndCritical();
1317 if (_draw_threaded
) _draw_mutex
->BeginCritical();
1319 NetworkDrawChatMessage();
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
);
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);
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);
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);
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);