-
-
Notifications
You must be signed in to change notification settings - Fork 629
/
win32init_windows.go
312 lines (281 loc) · 10.2 KB
/
win32init_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
// SPDX-License-Identifier: Zlib
// SPDX-FileCopyrightText: 2002-2006 Marcus Geelnard
// SPDX-FileCopyrightText: 2006-2019 Camilla Löwy
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
package glfwwin
import (
"errors"
"unsafe"
"golang.org/x/sys/windows"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
)
func createKeyTables() {
for i := range _glfw.win32.keycodes {
_glfw.win32.keycodes[i] = -1
}
for i := range _glfw.win32.scancodes {
_glfw.win32.keycodes[i] = -1
}
_glfw.win32.keycodes[0x00B] = Key0
_glfw.win32.keycodes[0x002] = Key1
_glfw.win32.keycodes[0x003] = Key2
_glfw.win32.keycodes[0x004] = Key3
_glfw.win32.keycodes[0x005] = Key4
_glfw.win32.keycodes[0x006] = Key5
_glfw.win32.keycodes[0x007] = Key6
_glfw.win32.keycodes[0x008] = Key7
_glfw.win32.keycodes[0x009] = Key8
_glfw.win32.keycodes[0x00A] = Key9
_glfw.win32.keycodes[0x01E] = KeyA
_glfw.win32.keycodes[0x030] = KeyB
_glfw.win32.keycodes[0x02E] = KeyC
_glfw.win32.keycodes[0x020] = KeyD
_glfw.win32.keycodes[0x012] = KeyE
_glfw.win32.keycodes[0x021] = KeyF
_glfw.win32.keycodes[0x022] = KeyG
_glfw.win32.keycodes[0x023] = KeyH
_glfw.win32.keycodes[0x017] = KeyI
_glfw.win32.keycodes[0x024] = KeyJ
_glfw.win32.keycodes[0x025] = KeyK
_glfw.win32.keycodes[0x026] = KeyL
_glfw.win32.keycodes[0x032] = KeyM
_glfw.win32.keycodes[0x031] = KeyN
_glfw.win32.keycodes[0x018] = KeyO
_glfw.win32.keycodes[0x019] = KeyP
_glfw.win32.keycodes[0x010] = KeyQ
_glfw.win32.keycodes[0x013] = KeyR
_glfw.win32.keycodes[0x01F] = KeyS
_glfw.win32.keycodes[0x014] = KeyT
_glfw.win32.keycodes[0x016] = KeyU
_glfw.win32.keycodes[0x02F] = KeyV
_glfw.win32.keycodes[0x011] = KeyW
_glfw.win32.keycodes[0x02D] = KeyX
_glfw.win32.keycodes[0x015] = KeyY
_glfw.win32.keycodes[0x02C] = KeyZ
_glfw.win32.keycodes[0x028] = KeyApostrophe
_glfw.win32.keycodes[0x02B] = KeyBackslash
_glfw.win32.keycodes[0x033] = KeyComma
_glfw.win32.keycodes[0x00D] = KeyEqual
_glfw.win32.keycodes[0x029] = KeyGraveAccent
_glfw.win32.keycodes[0x01A] = KeyLeftBracket
_glfw.win32.keycodes[0x00C] = KeyMinus
_glfw.win32.keycodes[0x034] = KeyPeriod
_glfw.win32.keycodes[0x01B] = KeyRightBracket
_glfw.win32.keycodes[0x027] = KeySemicolon
_glfw.win32.keycodes[0x035] = KeySlash
_glfw.win32.keycodes[0x056] = KeyWorld2
_glfw.win32.keycodes[0x00E] = KeyBackspace
_glfw.win32.keycodes[0x153] = KeyDelete
_glfw.win32.keycodes[0x14F] = KeyEnd
_glfw.win32.keycodes[0x01C] = KeyEnter
_glfw.win32.keycodes[0x001] = KeyEscape
_glfw.win32.keycodes[0x147] = KeyHome
_glfw.win32.keycodes[0x152] = KeyInsert
_glfw.win32.keycodes[0x15D] = KeyMenu
_glfw.win32.keycodes[0x151] = KeyPageDown
_glfw.win32.keycodes[0x149] = KeyPageUp
_glfw.win32.keycodes[0x045] = KeyPause
_glfw.win32.keycodes[0x039] = KeySpace
_glfw.win32.keycodes[0x00F] = KeyTab
_glfw.win32.keycodes[0x03A] = KeyCapsLock
_glfw.win32.keycodes[0x145] = KeyNumLock
_glfw.win32.keycodes[0x046] = KeyScrollLock
_glfw.win32.keycodes[0x03B] = KeyF1
_glfw.win32.keycodes[0x03C] = KeyF2
_glfw.win32.keycodes[0x03D] = KeyF3
_glfw.win32.keycodes[0x03E] = KeyF4
_glfw.win32.keycodes[0x03F] = KeyF5
_glfw.win32.keycodes[0x040] = KeyF6
_glfw.win32.keycodes[0x041] = KeyF7
_glfw.win32.keycodes[0x042] = KeyF8
_glfw.win32.keycodes[0x043] = KeyF9
_glfw.win32.keycodes[0x044] = KeyF10
_glfw.win32.keycodes[0x057] = KeyF11
_glfw.win32.keycodes[0x058] = KeyF12
_glfw.win32.keycodes[0x064] = KeyF13
_glfw.win32.keycodes[0x065] = KeyF14
_glfw.win32.keycodes[0x066] = KeyF15
_glfw.win32.keycodes[0x067] = KeyF16
_glfw.win32.keycodes[0x068] = KeyF17
_glfw.win32.keycodes[0x069] = KeyF18
_glfw.win32.keycodes[0x06A] = KeyF19
_glfw.win32.keycodes[0x06B] = KeyF20
_glfw.win32.keycodes[0x06C] = KeyF21
_glfw.win32.keycodes[0x06D] = KeyF22
_glfw.win32.keycodes[0x06E] = KeyF23
_glfw.win32.keycodes[0x076] = KeyF24
_glfw.win32.keycodes[0x038] = KeyLeftAlt
_glfw.win32.keycodes[0x01D] = KeyLeftControl
_glfw.win32.keycodes[0x02A] = KeyLeftShift
_glfw.win32.keycodes[0x15B] = KeyLeftSuper
_glfw.win32.keycodes[0x137] = KeyPrintScreen
_glfw.win32.keycodes[0x138] = KeyRightAlt
_glfw.win32.keycodes[0x11D] = KeyRightControl
_glfw.win32.keycodes[0x036] = KeyRightShift
_glfw.win32.keycodes[0x15C] = KeyRightSuper
_glfw.win32.keycodes[0x150] = KeyDown
_glfw.win32.keycodes[0x14B] = KeyLeft
_glfw.win32.keycodes[0x14D] = KeyRight
_glfw.win32.keycodes[0x148] = KeyUp
_glfw.win32.keycodes[0x052] = KeyKP0
_glfw.win32.keycodes[0x04F] = KeyKP1
_glfw.win32.keycodes[0x050] = KeyKP2
_glfw.win32.keycodes[0x051] = KeyKP3
_glfw.win32.keycodes[0x04B] = KeyKP4
_glfw.win32.keycodes[0x04C] = KeyKP5
_glfw.win32.keycodes[0x04D] = KeyKP6
_glfw.win32.keycodes[0x047] = KeyKP7
_glfw.win32.keycodes[0x048] = KeyKP8
_glfw.win32.keycodes[0x049] = KeyKP9
_glfw.win32.keycodes[0x04E] = KeyKPAdd
_glfw.win32.keycodes[0x053] = KeyKPDecimal
_glfw.win32.keycodes[0x135] = KeyKPDivide
_glfw.win32.keycodes[0x11C] = KeyKPEnter
_glfw.win32.keycodes[0x059] = KeyKPEqual
_glfw.win32.keycodes[0x037] = KeyKPMultiply
_glfw.win32.keycodes[0x04A] = KeyKPSubtract
for scancode := 0; scancode < 512; scancode++ {
if _glfw.win32.keycodes[scancode] > 0 {
_glfw.win32.scancodes[_glfw.win32.keycodes[scancode]] = scancode
}
}
}
func createHelperWindow() error {
h, err := _CreateWindowExW(_WS_EX_OVERLAPPEDWINDOW, _GLFW_WNDCLASSNAME, "GLFW message window", _WS_CLIPSIBLINGS|_WS_CLIPCHILDREN, 0, 0, 1, 1, 0, 0, _glfw.win32.instance, nil)
if err != nil {
return err
}
_glfw.win32.helperWindowHandle = h
// HACK: The command to the first ShowWindow call is ignored if the parent
// process passed along a STARTUPINFO, so clear that with a no-op call
_ShowWindow(_glfw.win32.helperWindowHandle, _SW_HIDE)
// Register for HID device notifications
if !microsoftgdk.IsXbox() {
_GUID_DEVINTERFACE_HID := windows.GUID{
Data1: 0x4d1e55b2,
Data2: 0xf16f,
Data3: 0x11cf,
Data4: [...]byte{0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30},
}
var dbi _DEV_BROADCAST_DEVICEINTERFACE_W
dbi.dbcc_size = uint32(unsafe.Sizeof(dbi))
dbi.dbcc_devicetype = _DBT_DEVTYP_DEVICEINTERFACE
dbi.dbcc_classguid = _GUID_DEVINTERFACE_HID
notify, err := _RegisterDeviceNotificationW(windows.Handle(_glfw.win32.helperWindowHandle), unsafe.Pointer(&dbi), _DEVICE_NOTIFY_WINDOW_HANDLE)
if err != nil {
return err
}
_glfw.win32.deviceNotificationHandle = notify
}
var msg _MSG
for _PeekMessageW(&msg, _glfw.win32.helperWindowHandle, 0, 0, _PM_REMOVE) {
_TranslateMessage(&msg)
_DispatchMessageW(&msg)
}
return nil
}
func isWindowsVersionOrGreaterWin32(major, minor, sp uint16) bool {
osvi := _OSVERSIONINFOEXW{
dwMajorVersion: uint32(major),
dwMinorVersion: uint32(minor),
wServicePackMajor: sp,
}
osvi.dwOSVersionInfoSize = uint32(unsafe.Sizeof(osvi))
var mask uint32 = _VER_MAJORVERSION | _VER_MINORVERSION | _VER_SERVICEPACKMAJOR
cond := _VerSetConditionMask(0, _VER_MAJORVERSION, _VER_GREATER_EQUAL)
cond = _VerSetConditionMask(cond, _VER_MINORVERSION, _VER_GREATER_EQUAL)
cond = _VerSetConditionMask(cond, _VER_SERVICEPACKMAJOR, _VER_GREATER_EQUAL)
// HACK: Use RtlVerifyVersionInfo instead of VerifyVersionInfoW as the
// latter lies unless the user knew to embed a non-default manifest
// announcing support for Windows 10 via supportedOS GUID
return _RtlVerifyVersionInfo(&osvi, mask, cond) == 0
}
func isWindows10BuildOrGreaterWin32(build uint16) bool {
osvi := _OSVERSIONINFOEXW{
dwMajorVersion: 10,
dwMinorVersion: 0,
dwBuildNumber: uint32(build),
}
osvi.dwOSVersionInfoSize = uint32(unsafe.Sizeof(osvi))
var mask uint32 = _VER_MAJORVERSION | _VER_MINORVERSION | _VER_BUILDNUMBER
cond := _VerSetConditionMask(0, _VER_MAJORVERSION, _VER_GREATER_EQUAL)
cond = _VerSetConditionMask(cond, _VER_MINORVERSION, _VER_GREATER_EQUAL)
cond = _VerSetConditionMask(cond, _VER_BUILDNUMBER, _VER_GREATER_EQUAL)
// HACK: Use RtlVerifyVersionInfo instead of VerifyVersionInfoW as the
// latter lies unless the user knew to embed a non-default manifest
// announcing support for Windows 10 via supportedOS GUID
return _RtlVerifyVersionInfo(&osvi, mask, cond) == 0
}
func platformInit() error {
// Changing the foreground lock timeout was removed from the original code.
// See https://github.com/glfw/glfw/commit/58b48a3a00d9c2a5ca10cc23069a71d8773cc7a4
m, err := _GetModuleHandleExW(_GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|_GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, unsafe.Pointer(&_glfw))
if err != nil {
return err
}
_glfw.win32.instance = _HINSTANCE(m)
createKeyTables()
if isWindows10CreatorsUpdateOrGreaterWin32() {
if !microsoftgdk.IsXbox() {
// Ignore the error as SetProcessDpiAwarenessContext returns an error on Steam Deck (#2113).
// This seems an issue in Wine and/or Proton, but there is nothing we can do.
_ = _SetProcessDpiAwarenessContext(_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)
}
} else if _IsWindows8Point1OrGreater() {
if err := _SetProcessDpiAwareness(_PROCESS_PER_MONITOR_DPI_AWARE); err != nil && !errors.Is(err, handleError(windows.E_ACCESSDENIED)) {
return err
}
} else if _IsWindowsVistaOrGreater() {
_SetProcessDPIAware()
}
if err := registerWindowClassWin32(); err != nil {
return err
}
if err := createHelperWindow(); err != nil {
return err
}
if microsoftgdk.IsXbox() {
// On Xbox, APIs to get monitors are not available.
// Create a pseudo monitor instance instead.
w, h := microsoftgdk.MonitorResolution()
mode := &VidMode{
Width: w,
Height: h,
RedBits: 8,
GreenBits: 8,
BlueBits: 8,
RefreshRate: 0, // TODO: Is it possible to get an appropriate refresh rate?
}
m := &Monitor{
name: "Xbox Monitor",
modes: []*VidMode{mode},
}
if err := inputMonitor(m, Connected, _GLFW_INSERT_LAST); err != nil {
return err
}
} else {
if err := pollMonitorsWin32(); err != nil {
return err
}
}
return nil
}
func platformTerminate() error {
if _glfw.win32.deviceNotificationHandle != 0 {
if err := _UnregisterDeviceNotification(_glfw.win32.deviceNotificationHandle); err != nil {
return err
}
}
if _glfw.win32.helperWindowHandle != 0 {
if !microsoftgdk.IsXbox() {
if err := _DestroyWindow(_glfw.win32.helperWindowHandle); err != nil {
return err
}
}
}
if err := unregisterWindowClassWin32(); err != nil {
return err
}
terminateWGL()
return nil
}