-
-
Notifications
You must be signed in to change notification settings - Fork 692
/
Copy pathwin32_window_windows.go
2423 lines (2109 loc) · 61.2 KB
/
win32_window_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2002-2006 Marcus Geelnard
// SPDX-FileCopyrightText: 2006-2019 Camilla Löwy <elmindreda@glfw.org>
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
package glfw
import (
"errors"
"fmt"
"math"
"runtime"
"unsafe"
"golang.org/x/sys/windows"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
"github.com/hajimehoshi/ebiten/v2/internal/winver"
)
func (w *Window) getWindowStyle() uint32 {
var style uint32 = _WS_CLIPSIBLINGS | _WS_CLIPCHILDREN
if w.monitor != nil {
style |= _WS_POPUP
} else {
style |= _WS_SYSMENU | _WS_MINIMIZEBOX
if w.decorated {
style |= _WS_CAPTION
if w.resizable {
style |= _WS_THICKFRAME
if w.maxwidth == DontCare && w.maxheight == DontCare {
style |= _WS_MAXIMIZEBOX
}
}
} else {
style |= _WS_POPUP
}
}
return style
}
func (w *Window) getWindowExStyle() uint32 {
var style uint32 = _WS_EX_APPWINDOW
if w.floating {
style |= _WS_EX_TOPMOST
}
return style
}
func chooseImage(images []*Image, width, height int) *Image {
var leastDiff uint = math.MaxUint32
var closest *Image
for _, image := range images {
currDiff := abs(image.Width*image.Height - width*height)
if currDiff < leastDiff {
closest = image
leastDiff = currDiff
}
}
return closest
}
func createIcon(image *Image, xhot, yhot int, icon bool) (_HICON, error) {
var bi _BITMAPV5HEADER
bi.bV5Size = uint32(unsafe.Sizeof(bi))
bi.bV5Width = int32(image.Width)
bi.bV5Height = int32(-image.Height)
bi.bV5Planes = 1
bi.bV5BitCount = 32
bi.bV5Compression = _BI_BITFIELDS
bi.bV5RedMask = 0x00ff0000
bi.bV5GreenMask = 0x0000ff00
bi.bV5BlueMask = 0x000000ff
bi.bV5AlphaMask = 0xff000000
dc, err := _GetDC(0)
if err != nil {
return 0, err
}
defer _ReleaseDC(0, dc)
color, targetPtr, err := _CreateDIBSection(dc, &bi, _DIB_RGB_COLORS, 0, 0)
if err != nil {
return 0, err
}
defer func() {
_ = _DeleteObject(_HGDIOBJ(color))
}()
mask, err := _CreateBitmap(int32(image.Width), int32(image.Height), 1, 1, nil)
if err != nil {
return 0, err
}
defer func() {
_ = _DeleteObject(_HGDIOBJ(mask))
}()
source := image.Pixels
target := unsafe.Slice((*byte)(unsafe.Pointer(targetPtr)), len(source))
for i := 0; i < len(source)/4; i++ {
target[4*i] = source[4*i+2]
target[4*i+1] = source[4*i+1]
target[4*i+2] = source[4*i+0]
target[4*i+3] = source[4*i+3]
}
var iconInt32 int32
if icon {
iconInt32 = 1
}
ii := _ICONINFO{
fIcon: iconInt32,
xHotspot: uint32(xhot),
yHotspot: uint32(yhot),
hbmMask: mask,
hbmColor: color,
}
handle, err := _CreateIconIndirect(&ii)
if err != nil {
return 0, err
}
return handle, nil
}
func (w *Window) applyAspectRatio(edge int, area *_RECT) error {
var frame _RECT
ratio := float32(w.numer) / float32(w.denom)
style := w.getWindowStyle()
exStyle := w.getWindowExStyle()
if winver.IsWindows10AnniversaryUpdateOrGreater() {
if err := _AdjustWindowRectExForDpi(&frame, style, false, exStyle, _GetDpiForWindow(w.platform.handle)); err != nil {
return err
}
} else {
if err := _AdjustWindowRectEx(&frame, style, false, exStyle); err != nil {
return err
}
}
if edge == _WMSZ_LEFT || edge == _WMSZ_BOTTOMLEFT || edge == _WMSZ_RIGHT || edge == _WMSZ_BOTTOMRIGHT {
area.bottom = area.top + int32(frame.bottom-frame.top) + int32(float32(area.right-area.left-int32(frame.right-frame.left))/ratio)
} else if edge == _WMSZ_TOPLEFT || edge == _WMSZ_TOPRIGHT {
area.top = area.bottom - int32(frame.bottom-frame.top) - int32(float32(area.right-area.left-int32(frame.right-frame.left))/ratio)
} else if edge == _WMSZ_TOP || edge == _WMSZ_BOTTOM {
area.right = area.left + int32(frame.right-frame.left) + int32(float32(area.bottom-area.top-int32(frame.bottom-frame.top))*ratio)
}
return nil
}
func (w *Window) updateCursorImage() error {
if w.cursorMode == CursorNormal {
if w.cursor != nil {
_SetCursor(w.cursor.platform.handle)
} else {
cursor, err := _LoadCursorW(0, _IDC_ARROW)
if err != nil {
return err
}
_SetCursor(cursor)
}
} else {
// Connected via Remote Desktop, nil cursor will present SetCursorPos the move the cursor.
// using a blank cursor fix that.
// When not via Remote Desktop, platformWindow.blankCursor should be nil.
_SetCursor(_glfw.platformWindow.blankCursor)
}
return nil
}
func (w *Window) clientToScreen(rect _RECT) (_RECT, error) {
point := _POINT{
x: rect.left,
y: rect.top,
}
if err := _ClientToScreen(w.platform.handle, &point); err != nil {
return _RECT{}, err
}
rect.left = point.x
rect.top = point.y
point = _POINT{
x: rect.right,
y: rect.bottom,
}
if err := _ClientToScreen(w.platform.handle, &point); err != nil {
return _RECT{}, err
}
rect.right = point.x
rect.bottom = point.y
return rect, nil
}
func captureCursor(window *Window) error {
clipRect, err := _GetClientRect(window.platform.handle)
if err != nil {
return err
}
clipRect, err = window.clientToScreen(clipRect)
if err != nil {
return err
}
if err := _ClipCursor(&clipRect); err != nil {
return err
}
_glfw.platformWindow.capturedCursorWindow = window
return nil
}
func releaseCursor() error {
if err := _ClipCursor(nil); err != nil {
return err
}
_glfw.platformWindow.capturedCursorWindow = nil
return nil
}
func (w *Window) enableRawMouseMotion() error {
rid := []_RAWINPUTDEVICE{
{
usUsagePage: 0x01,
usUsage: 0x02,
dwFlags: 0,
hwndTarget: w.platform.handle,
},
}
return _RegisterRawInputDevices(rid)
}
func (w *Window) disableRawMouseMotion() error {
rid := []_RAWINPUTDEVICE{
{
usUsagePage: 0x01,
usUsage: 0x02,
dwFlags: _RIDEV_REMOVE,
hwndTarget: 0,
},
}
return _RegisterRawInputDevices(rid)
}
func (w *Window) disableCursor() error {
_glfw.platformWindow.disabledCursorWindow = w
x, y, err := w.platformGetCursorPos()
if err != nil {
return err
}
_glfw.platformWindow.restoreCursorPosX, _glfw.platformWindow.restoreCursorPosY = x, y
if err := w.updateCursorImage(); err != nil {
return err
}
if err := w.centerCursorInContentArea(); err != nil {
return err
}
if err := captureCursor(w); err != nil {
return err
}
if w.rawMouseMotion {
if err := w.enableRawMouseMotion(); err != nil {
return err
}
}
return nil
}
func (w *Window) enableCursor() error {
if w.rawMouseMotion {
if err := w.disableRawMouseMotion(); err != nil {
return err
}
}
_glfw.platformWindow.disabledCursorWindow = nil
if err := releaseCursor(); err != nil {
return err
}
if err := w.platformSetCursorPos(_glfw.platformWindow.restoreCursorPosX, _glfw.platformWindow.restoreCursorPosY); err != nil {
return err
}
if err := w.updateCursorImage(); err != nil {
return err
}
return nil
}
func (w *Window) cursorInContentArea() (bool, error) {
if microsoftgdk.IsXbox() {
return true, nil
}
pos, err := _GetCursorPos()
if err != nil {
if errors.Is(err, windows.ERROR_ACCESS_DENIED) {
return false, nil
}
return false, err
}
if _WindowFromPoint(pos) != w.platform.handle {
return false, nil
}
area, err := _GetClientRect(w.platform.handle)
if err != nil {
return false, err
}
area, err = w.clientToScreen(area)
if err != nil {
return false, err
}
return _PtInRect(&area, pos), nil
}
func (w *Window) updateWindowStyles() error {
s, err := _GetWindowLongW(w.platform.handle, _GWL_STYLE)
if err != nil {
return err
}
style := uint32(s)
style &^= _WS_OVERLAPPEDWINDOW | _WS_POPUP
style |= w.getWindowStyle()
rect, err := _GetClientRect(w.platform.handle)
if err != nil {
return err
}
if winver.IsWindows10AnniversaryUpdateOrGreater() {
if err := _AdjustWindowRectExForDpi(&rect, style, false, w.getWindowExStyle(), _GetDpiForWindow(w.platform.handle)); err != nil {
return err
}
} else {
if err := _AdjustWindowRectEx(&rect, style, false, w.getWindowExStyle()); err != nil {
return err
}
}
rect, err = w.clientToScreen(rect)
if err != nil {
return err
}
if _, err := _SetWindowLongW(w.platform.handle, _GWL_STYLE, int32(style)); err != nil {
return err
}
if err := _SetWindowPos(w.platform.handle, _HWND_TOP, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, _SWP_FRAMECHANGED|_SWP_NOACTIVATE|_SWP_NOZORDER); err != nil {
return err
}
return nil
}
func (w *Window) updateFramebufferTransparency() error {
if !winver.IsWindowsVistaOrGreater() {
return nil
}
composition, err := _DwmIsCompositionEnabled()
if err != nil {
// Ignore an error from DWM functions as they might not be implemented e.g. on Proton (#2113).
return nil
}
if !composition {
return nil
}
var opaque bool
if !winver.IsWindows8OrGreater() {
_, opaque, err = _DwmGetColorizationColor()
if err != nil {
// Ignore an error from DWM functions as they might not be implemented e.g. on Proton (#2113).
return nil
}
}
if winver.IsWindows8OrGreater() || !opaque {
region, err := _CreateRectRgn(0, 0, -1, -1)
if err != nil {
return err
}
defer func() {
_ = _DeleteObject(_HGDIOBJ(region))
}()
bb := _DWM_BLURBEHIND{
dwFlags: _DWM_BB_ENABLE | _DWM_BB_BLURREGION,
hRgnBlur: region,
fEnable: 1, // true
}
// Ignore an error from DWM functions as they might not be implemented e.g. on Proton (#2113).
_ = _DwmEnableBlurBehindWindow(w.platform.handle, &bb)
} else {
// HACK: Disable framebuffer transparency on Windows 7 when the
// colorization color is opaque, because otherwise the window
// contents is blended additively with the previous frame instead
// of replacing it
bb := _DWM_BLURBEHIND{
dwFlags: _DWM_BB_ENABLE,
}
// Ignore an error from DWM functions as they might not be implemented e.g. on Proton (#2113).
_ = _DwmEnableBlurBehindWindow(w.platform.handle, &bb)
}
return nil
}
func getKeyMods() ModifierKey {
var mods ModifierKey
if uint16(_GetKeyState(_VK_SHIFT))&0x8000 != 0 {
mods |= ModShift
}
if uint16(_GetKeyState(_VK_CONTROL))&0x8000 != 0 {
mods |= ModControl
}
if uint16(_GetKeyState(_VK_MENU))&0x8000 != 0 {
mods |= ModAlt
}
if uint16(_GetKeyState(_VK_LWIN)|_GetKeyState(_VK_RWIN))&0x8000 != 0 {
mods |= ModSuper
}
if _GetKeyState(_VK_CAPITAL)&1 != 0 {
mods |= ModCapsLock
}
if _GetKeyState(_VK_NUMLOCK)&1 != 0 {
mods |= ModNumLock
}
return mods
}
func (w *Window) fitToMonitor() error {
mi, ok := _GetMonitorInfoW(w.monitor.platform.handle)
if !ok {
return nil
}
var hWndInsertAfter windows.HWND
if w.floating {
hWndInsertAfter = _HWND_TOPMOST
} else {
hWndInsertAfter = _HWND_NOTOPMOST
}
if err := _SetWindowPos(w.platform.handle, hWndInsertAfter,
mi.rcMonitor.left,
mi.rcMonitor.top,
mi.rcMonitor.right-mi.rcMonitor.left,
mi.rcMonitor.bottom-mi.rcMonitor.top,
_SWP_NOZORDER|_SWP_NOACTIVATE|_SWP_NOCOPYBITS); err != nil {
return err
}
return nil
}
func (w *Window) acquireMonitor() error {
if _glfw.platformWindow.acquiredMonitorCount == 0 {
_SetThreadExecutionState(_ES_CONTINUOUS | _ES_DISPLAY_REQUIRED)
// HACK: When mouse trails are enabled the cursor becomes invisible when
// the OpenGL ICD switches to page flipping
if winver.IsWindowsXPOrGreater() {
if err := _SystemParametersInfoW(_SPI_GETMOUSETRAILS, 0, uintptr(unsafe.Pointer(&_glfw.platformWindow.mouseTrailSize)), 0); err != nil {
return err
}
if err := _SystemParametersInfoW(_SPI_SETMOUSETRAILS, 0, 0, 0); err != nil {
return err
}
}
}
if w.monitor.window == nil {
_glfw.platformWindow.acquiredMonitorCount++
}
if err := w.monitor.setVideoModeWin32(&w.videoMode); err != nil {
return err
}
w.monitor.inputMonitorWindow(w)
return nil
}
func (w *Window) releaseMonitor() error {
if w.monitor.window != w {
return nil
}
_glfw.platformWindow.acquiredMonitorCount--
if _glfw.platformWindow.acquiredMonitorCount == 0 {
_SetThreadExecutionState(_ES_CONTINUOUS)
// HACK: Restore mouse trail length saved in acquireMonitor
if winver.IsWindowsXPOrGreater() {
if err := _SystemParametersInfoW(_SPI_SETMOUSETRAILS, _glfw.platformWindow.mouseTrailSize, 0, 0); err != nil {
return err
}
}
}
w.monitor.inputMonitorWindow(nil)
w.monitor.restoreVideoModeWin32()
return nil
}
func (w *Window) maximizeWindowManually() error {
mi, _ := _GetMonitorInfoW(_MonitorFromWindow(w.platform.handle, _MONITOR_DEFAULTTONEAREST))
rect := mi.rcWork
if w.maxwidth != DontCare && w.maxheight != DontCare {
if rect.right-rect.left > int32(w.maxwidth) {
rect.right = rect.left + int32(w.maxwidth)
}
if rect.bottom-rect.top > int32(w.maxheight) {
rect.bottom = rect.top + int32(w.maxheight)
}
}
s, err := _GetWindowLongW(w.platform.handle, _GWL_STYLE)
if err != nil {
return err
}
style := uint32(s)
style |= _WS_MAXIMIZE
if _, err := _SetWindowLongW(w.platform.handle, _GWL_STYLE, int32(style)); err != nil {
return err
}
if w.decorated {
s, err := _GetWindowLongW(w.platform.handle, _GWL_EXSTYLE)
if err != nil {
return err
}
exStyle := uint32(s)
if winver.IsWindows10AnniversaryUpdateOrGreater() {
dpi := _GetDpiForWindow(w.platform.handle)
if err := _AdjustWindowRectExForDpi(&rect, style, false, exStyle, dpi); err != nil {
return err
}
m, err := _GetSystemMetricsForDpi(_SM_CYCAPTION, dpi)
if err != nil {
return err
}
_OffsetRect(&rect, 0, m)
} else {
if err := _AdjustWindowRectEx(&rect, style, false, exStyle); err != nil {
return err
}
m, err := _GetSystemMetrics(_SM_CYCAPTION)
if err != nil {
return err
}
_OffsetRect(&rect, 0, m)
}
if rect.bottom > mi.rcWork.bottom {
rect.bottom = mi.rcWork.bottom
}
}
if err := _SetWindowPos(w.platform.handle, _HWND_TOP,
rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
_SWP_NOACTIVATE|_SWP_NOZORDER|_SWP_FRAMECHANGED); err != nil {
return err
}
return nil
}
func windowProc(hWnd windows.HWND, uMsg uint32, wParam _WPARAM, lParam _LPARAM) uintptr /*_LRESULT*/ {
window := handleToWindow[hWnd]
if window == nil {
// This is the message handling for the hidden helper window
// and for a regular window during its initial creation
switch uMsg {
case _WM_NCCREATE:
if winver.IsWindows10AnniversaryUpdateOrGreater() {
cs := (*_CREATESTRUCTW)(unsafe.Pointer(lParam))
wndconfig := (*wndconfig)(cs.lpCreateParams)
// On per-monitor DPI aware V1 systems, only enable
// non-client scaling for windows that scale the client area
// We need WM_GETDPISCALEDSIZE from V2 to keep the client
// area static when the non-client area is scaled
if wndconfig != nil && wndconfig.scaleToMonitor {
if err := _EnableNonClientDpiScaling(hWnd); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
}
case _WM_DISPLAYCHANGE:
if err := pollMonitorsWin32(); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
return uintptr(_DefWindowProcW(hWnd, uMsg, wParam, lParam))
}
switch uMsg {
case _WM_MOUSEACTIVATE:
// HACK: Postpone cursor disabling when the window was activated by
// clicking a caption button
if _HIWORD(uint32(lParam)) == _WM_LBUTTONDOWN {
if _LOWORD(uint32(lParam)) != _HTCLIENT {
window.platform.frameAction = true
}
}
case _WM_CAPTURECHANGED:
// HACK: Disable the cursor once the caption button action has been
// completed or cancelled
if lParam == 0 && window.platform.frameAction {
if window.cursorMode == CursorDisabled {
if err := window.disableCursor(); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
window.platform.frameAction = false
}
case _WM_SETFOCUS:
window.inputWindowFocus(true)
// HACK: Do not disable cursor while the user is interacting with
// a caption button
if window.platform.frameAction {
break
}
if window.cursorMode == CursorDisabled {
if err := window.disableCursor(); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
return 0
case _WM_KILLFOCUS:
if window.cursorMode == CursorDisabled {
if err := window.enableCursor(); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
if window.monitor != nil && window.autoIconify {
window.platformIconifyWindow()
}
window.inputWindowFocus(false)
return 0
case _WM_SYSCOMMAND:
switch wParam & 0xfff0 {
case _SC_SCREENSAVE, _SC_MONITORPOWER:
if window.monitor != nil {
// We are running in full screen mode, so disallow
// screen saver and screen blanking
return 0
} else {
break
}
// User trying to access application menu using ALT?
case _SC_KEYMENU:
return 0
}
case _WM_CLOSE:
window.inputWindowCloseRequest()
return 0
case _WM_INPUTLANGCHANGE:
updateKeyNamesWin32()
return 0
case _WM_CHAR, _WM_SYSCHAR:
if wParam >= 0xd800 && wParam <= 0xdbff {
window.platform.highSurrogate = uint16(wParam)
} else {
var codepoint rune
if wParam >= 0xdc00 && wParam <= 0xdfff {
if window.platform.highSurrogate != 0 {
codepoint += (rune(window.platform.highSurrogate) - 0xd800) << 10
codepoint += (rune(wParam) & 0xffff) - 0xdc00
codepoint += 0x10000
}
} else {
codepoint = rune(wParam) & 0xffff
}
window.platform.highSurrogate = 0
window.inputChar(codepoint, getKeyMods(), uMsg != _WM_SYSCHAR)
}
return 0
case _WM_UNICHAR:
if wParam == _UNICODE_NOCHAR {
// WM_UNICHAR is not sent by Windows, but is sent by some
// third-party input method engine
// Returning TRUE here announces support for this message
return 1
}
window.inputChar(rune(wParam), getKeyMods(), true)
return 0
case _WM_KEYDOWN, _WM_SYSKEYDOWN, _WM_KEYUP, _WM_SYSKEYUP:
action := Press
if _HIWORD(uint32(lParam))&_KF_UP != 0 {
action = Release
}
mods := getKeyMods()
scancode := uint32((_HIWORD(uint32(lParam)) & (_KF_EXTENDED | 0xff)))
if scancode == 0 {
if microsoftgdk.IsXbox() {
break
}
// NOTE: Some synthetic key messages have a scancode of zero
// HACK: Map the virtual key back to a usable scancode
scancode = _MapVirtualKeyW(uint32(wParam), _MAPVK_VK_TO_VSC)
}
// HACK: Alt+PrtSc has a different scancode than just PrtSc
if scancode == 0x54 {
scancode = 0x137
}
// HACK: Ctrl+Pause has a different scancode than just Pause
if scancode == 0x146 {
scancode = 0x45
}
// HACK: CJK IME sets the extended bit for right Shift
if scancode == 0x136 {
scancode = 0x36
}
key := _glfw.platformWindow.keycodes[scancode]
// The Ctrl keys require special handling
if wParam == _VK_CONTROL {
if _HIWORD(uint32(lParam))&_KF_EXTENDED != 0 {
// Right side keys have the extended key bit set
key = KeyRightControl
} else {
// NOTE: Alt Gr sends Left Ctrl followed by Right Alt
// HACK: We only want one event for Alt Gr, so if we detect
// this sequence we discard this Left Ctrl message now
// and later report Right Alt normally
var next _MSG
time := _GetMessageTime()
if _PeekMessageW(&next, 0, 0, 0, _PM_NOREMOVE) {
if next.message == _WM_KEYDOWN ||
next.message == _WM_SYSKEYDOWN ||
next.message == _WM_KEYUP ||
next.message == _WM_SYSKEYUP {
if next.wParam == _VK_MENU && (_HIWORD(uint32(next.lParam))&_KF_EXTENDED) != 0 && next.time == uint32(time) {
// Next message is Right Alt down so discard this
break
}
}
}
// This is a regular Left Ctrl message
key = KeyLeftControl
}
} else if wParam == _VK_PROCESSKEY {
// IME notifies that keys have been filtered by setting the
// virtual key-code to VK_PROCESSKEY
break
}
if action == Release && wParam == _VK_SHIFT {
// HACK: Release both Shift keys on Shift up event, as when both
// are pressed the first release does not emit any event
// NOTE: The other half of this is in _glfwPlatformPollEvents
window.inputKey(KeyLeftShift, int(scancode), action, mods)
window.inputKey(KeyRightShift, int(scancode), action, mods)
} else if wParam == _VK_SNAPSHOT {
// HACK: Key down is not reported for the Print Screen key
window.inputKey(key, int(scancode), Press, mods)
window.inputKey(key, int(scancode), Release, mods)
} else {
window.inputKey(key, int(scancode), action, mods)
}
case _WM_LBUTTONDOWN, _WM_RBUTTONDOWN, _WM_MBUTTONDOWN, _WM_XBUTTONDOWN, _WM_LBUTTONUP, _WM_RBUTTONUP, _WM_MBUTTONUP, _WM_XBUTTONUP:
var button MouseButton
if uMsg == _WM_LBUTTONDOWN || uMsg == _WM_LBUTTONUP {
button = MouseButtonLeft
} else if uMsg == _WM_RBUTTONDOWN || uMsg == _WM_RBUTTONUP {
button = MouseButtonRight
} else if uMsg == _WM_MBUTTONDOWN || uMsg == _WM_MBUTTONUP {
button = MouseButtonMiddle
} else if _GET_XBUTTON_WPARAM(wParam) == _XBUTTON1 {
button = MouseButton4
} else {
button = MouseButton5
}
var action Action
if uMsg == _WM_LBUTTONDOWN || uMsg == _WM_RBUTTONDOWN || uMsg == _WM_MBUTTONDOWN || uMsg == _WM_XBUTTONDOWN {
action = Press
} else {
action = Release
}
var i MouseButton
for i = 0; i <= MouseButtonLast; i++ {
if window.mouseButtons[i] == Press {
break
}
}
if i > MouseButtonLast {
_SetCapture(hWnd)
}
window.inputMouseClick(button, action, getKeyMods())
for i = 0; i <= MouseButtonLast; i++ {
if window.mouseButtons[i] == Press {
break
}
}
if i > MouseButtonLast {
if err := _ReleaseCapture(); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
if uMsg == _WM_XBUTTONDOWN || uMsg == _WM_XBUTTONUP {
return 1
}
return 0
case _WM_MOUSEMOVE:
x := _GET_X_LPARAM(lParam)
y := _GET_Y_LPARAM(lParam)
if !window.platform.cursorTracked {
var tme _TRACKMOUSEEVENT
tme.cbSize = uint32(unsafe.Sizeof(tme))
tme.dwFlags = _TME_LEAVE
tme.hwndTrack = window.platform.handle
if err := _TrackMouseEvent(&tme); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
window.platform.cursorTracked = true
window.inputCursorEnter(true)
}
if window.cursorMode == CursorDisabled {
dx := x - window.platform.lastCursorPosX
dy := y - window.platform.lastCursorPosY
if _glfw.platformWindow.disabledCursorWindow != window {
break
}
if window.rawMouseMotion {
break
}
window.inputCursorPos(window.virtualCursorPosX+float64(dx), window.virtualCursorPosY+float64(dy))
} else {
window.inputCursorPos(float64(x), float64(y))
}
window.platform.lastCursorPosX = x
window.platform.lastCursorPosY = y
return 0
case _WM_INPUT:
if _glfw.platformWindow.disabledCursorWindow != window {
break
}
if !window.rawMouseMotion {
break
}
ri := _HRAWINPUT(lParam)
var size uint32
if _, err := _GetRawInputData(ri, _RID_INPUT, nil, &size); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
if size > uint32(len(_glfw.platformWindow.rawInput)) {
_glfw.platformWindow.rawInput = make([]byte, size)
}
size = uint32(len(_glfw.platformWindow.rawInput))
if _, err := _GetRawInputData(ri, _RID_INPUT, unsafe.Pointer(&_glfw.platformWindow.rawInput[0]), &size); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
// TODO: break?
}
var dx, dy int
data := (*_RAWINPUT)(unsafe.Pointer(&_glfw.platformWindow.rawInput[0]))
if data.mouse.usFlags&_MOUSE_MOVE_ABSOLUTE != 0 {
if _glfw.platformWindow.isRemoteSession {
// Remote Desktop Mode
// As per https://github.com/Microsoft/DirectXTK/commit/ef56b63f3739381e451f7a5a5bd2c9779d2a7555
// MOUSE_MOVE_ABSOLUTE is a range from 0 through 65535, based on the screen size.
// Apparently, absolute mode only occurs over RDP though.
var smx int32 = _SM_CXSCREEN
var smy int32 = _SM_CYSCREEN
if data.mouse.usFlags&_MOUSE_VIRTUAL_DESKTOP != 0 {
smx = _SM_CXVIRTUALSCREEN
smy = _SM_CYVIRTUALSCREEN
}
width, err := _GetSystemMetrics(smx)
if err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
height, err := _GetSystemMetrics(smy)
if err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
pos := _POINT{
x: int32(float64(data.mouse.lLastX) / 65535.0 * float64(width)),
y: int32(float64(data.mouse.lLastY) / 65535.0 * float64(height)),
}
if err := _ScreenToClient(window.platform.handle, &pos); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
dx = int(pos.x) - window.platform.lastCursorPosX
dy = int(pos.y) - window.platform.lastCursorPosY
} else {
// Normal mode
// We should have the right absolute coords in data.mouse
dx = int(data.mouse.lLastX) - window.platform.lastCursorPosX
dy = int(data.mouse.lLastY) - window.platform.lastCursorPosY
}
} else {
dx = int(data.mouse.lLastX)
dy = int(data.mouse.lLastY)
}
window.inputCursorPos(window.virtualCursorPosX+float64(dx), window.virtualCursorPosY+float64(dy))
window.platform.lastCursorPosX += dx
window.platform.lastCursorPosY += dy
case _WM_MOUSELEAVE:
window.platform.cursorTracked = false
window.inputCursorEnter(false)
return 0
case _WM_MOUSEWHEEL:
window.inputScroll(0, float64(int16(_HIWORD(uint32(wParam))))/_WHEEL_DELTA)
return 0
case _WM_MOUSEHWHEEL:
// This message is only sent on Windows Vista and later
// NOTE: The X-axis is inverted for consistency with macOS and X11
window.inputScroll(float64(-(int16(_HIWORD(uint32(wParam))))/_WHEEL_DELTA), 0)
return 0
case _WM_ENTERSIZEMOVE, _WM_ENTERMENULOOP:
if window.platform.frameAction {
break
}
// HACK: Enable the cursor while the user is moving or
// resizing the window or using the window menu
if window.cursorMode == CursorDisabled {
if err := window.enableCursor(); err != nil {
_glfw.errors = append(_glfw.errors, err)
return 0
}
}
case _WM_EXITSIZEMOVE, _WM_EXITMENULOOP:
if window.platform.frameAction {
break
}
// HACK: Disable the cursor once the user is done moving or
// resizing the window or using the menu
if window.cursorMode == CursorDisabled {
if err := window.disableCursor(); err != nil {