From ef7d10a36e7645357fb17dff4c7d3f89d52b598d Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 27 May 2022 22:11:05 +0900 Subject: [PATCH] internal/glfwwin: add a build tag microsoftgdk Updates #1162 --- internal/glfwwin/api_windows.go | 22 +-------- internal/glfwwin/microsoftgdk_windows.go | 50 +++++++++++++++++++++ internal/glfwwin/notmicrosoftgdk_windows.go | 22 +++++++++ internal/glfwwin/win32init_windows.go | 14 ++++-- internal/glfwwin/win32monitor_windows.go | 2 +- internal/glfwwin/win32window_windows.go | 6 +-- 6 files changed, 87 insertions(+), 29 deletions(-) create mode 100644 internal/glfwwin/microsoftgdk_windows.go create mode 100644 internal/glfwwin/notmicrosoftgdk_windows.go diff --git a/internal/glfwwin/api_windows.go b/internal/glfwwin/api_windows.go index 0cd523bb8..eef1a8373 100644 --- a/internal/glfwwin/api_windows.go +++ b/internal/glfwwin/api_windows.go @@ -1,4 +1,4 @@ -// Copyright 2022 The Ebiten Authors +// Copyright 2022 The Ebitengine Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -1133,10 +1133,6 @@ func _EnumDisplayDevicesW(device string, iDevNum uint32, dwFlags uint32) (_DISPL return displayDevice, true } -func _EnumDisplayDevicesW_Available() bool { - return procEnumDisplayDevicesW.Find() == nil -} - func _EnumDisplayMonitors(hdc _HDC, lprcClip *_RECT, lpfnEnum uintptr, dwData _LPARAM) error { r, _, e := procEnumDisplayMonitors.Call(uintptr(hdc), uintptr(unsafe.Pointer(lprcClip)), uintptr(lpfnEnum), uintptr(dwData)) if int32(r) == 0 { @@ -1395,19 +1391,11 @@ func _LoadImageW(hInst _HINSTANCE, name uintptr, typ uint32, cx int32, cy int32, return windows.Handle(r), nil } -func _LoadImageW_Available() bool { - return procLoadImageW.Find() == nil -} - func _MapVirtualKeyW(uCode uint32, uMapType uint32) uint32 { r, _, _ := procMapVirtualKeyW.Call(uintptr(uCode), uintptr(uMapType)) return uint32(r) } -func _MapVirtualKeyW_Available() bool { - return procMapVirtualKeyW.Find() == nil -} - func _MonitorFromWindow(hwnd windows.HWND, dwFlags uint32) _HMONITOR { r, _, _ := procMonitorFromWindow.Call(uintptr(hwnd), uintptr(dwFlags)) return _HMONITOR(r) @@ -1489,10 +1477,6 @@ func _RegisterDeviceNotificationW(hRecipient windows.Handle, notificationFilter return _HDEVNOTIFY(r), nil } -func _RegisterDeviceNotificationW_Available() bool { - return procRegisterDeviceNotificationW.Find() == nil -} - func _RegisterRawInputDevices(pRawInputDevices []_RAWINPUTDEVICE) error { r, _, e := procRegisterRawInputDevices.Call(uintptr(unsafe.Pointer(&pRawInputDevices[0])), uintptr(len(pRawInputDevices)), unsafe.Sizeof(pRawInputDevices[0])) if int32(r) == 0 { @@ -1629,10 +1613,6 @@ func _SetProcessDpiAwarenessContext(value _DPI_AWARENESS_CONTEXT) error { return nil } -func _SetProcessDpiAwarenessContext_Available() bool { - return procSetProcessDpiAwarenessContext.Find() == nil -} - func _SetThreadExecutionState(esFlags _EXECUTION_STATE) _EXECUTION_STATE { r, _, _ := procSetThreadExecutionState.Call(uintptr(esFlags)) return _EXECUTION_STATE(r) diff --git a/internal/glfwwin/microsoftgdk_windows.go b/internal/glfwwin/microsoftgdk_windows.go new file mode 100644 index 000000000..1857c5618 --- /dev/null +++ b/internal/glfwwin/microsoftgdk_windows.go @@ -0,0 +1,50 @@ +// Copyright 2022 The Ebitengine Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build microsoftgdk +// +build microsoftgdk + +package glfwwin + +// This file does not include the headers of Microsoft GDK in order to compile easier. +// In order to compile this, create a dummy DLL with empty implementations like below, +// and link it. +// +// #include +// __declspec(dllexport) __cdecl uint32_t XSystemGetDeviceType(void) { +// return 0; +// } +// +// Unfortunately, some functions like XSystemGetDeviceType is not implemented in a DLL, +// so LoadLibrary is not available. + +// typedef enum { +// Unknown = 0x00, +// Pc = 0x01, +// XboxOne = 0x02, +// XboxOneS = 0x03, +// XboxOneX = 0x04, +// XboxOneXDevkit = 0x05, +// XboxScarlettLockhart = 0x06, +// XboxScarlettAnaconda = 0x07, +// XboxScarlettDevkit = 0x08, +// } XSystemDeviceType; +// +// XSystemDeviceType XSystemGetDeviceType(void); +import "C" + +func isXbox() bool { + t := C.XSystemGetDeviceType() + return t != C.Unknown && t != C.Pc +} diff --git a/internal/glfwwin/notmicrosoftgdk_windows.go b/internal/glfwwin/notmicrosoftgdk_windows.go new file mode 100644 index 000000000..6eae6950f --- /dev/null +++ b/internal/glfwwin/notmicrosoftgdk_windows.go @@ -0,0 +1,22 @@ +// Copyright 2022 The Ebitengine Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !microsoftgdk +// +build !microsoftgdk + +package glfwwin + +func isXbox() bool { + return false +} diff --git a/internal/glfwwin/win32init_windows.go b/internal/glfwwin/win32init_windows.go index 3090adbbb..1ff066b53 100644 --- a/internal/glfwwin/win32init_windows.go +++ b/internal/glfwwin/win32init_windows.go @@ -168,7 +168,7 @@ func createHelperWindow() error { _ShowWindow(_glfw.win32.helperWindowHandle, _SW_HIDE) // Register for HID device notifications - if _RegisterDeviceNotificationW_Available() { + if !isXbox() { _GUID_DEVINTERFACE_HID := windows.GUID{ Data1: 0x4d1e55b2, Data2: 0xf16f, @@ -238,7 +238,7 @@ func platformInit() error { createKeyTables() if isWindows10CreatorsUpdateOrGreaterWin32() { - if _SetProcessDpiAwarenessContext_Available() { + if !isXbox() { if err := _SetProcessDpiAwarenessContext(_DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); err != nil && !errors.Is(err, windows.ERROR_ACCESS_DENIED) { return err } @@ -258,8 +258,14 @@ func platformInit() error { if err := createHelperWindow(); err != nil { return err } - if err := pollMonitorsWin32(); err != nil { - return err + if isXbox() { + // TODO: Create a dummy window. + // The resolution can be deterined based on a device. + // See https://github.com/microsoft/Xbox-GDK-Samples/blob/1f44bdabed6e340170e2c7d1007500e4dff897bb/Samples/IntroGraphics/SimpleDynamicResources/Main.cpp#L118-L148 + } else { + if err := pollMonitorsWin32(); err != nil { + return err + } } return nil } diff --git a/internal/glfwwin/win32monitor_windows.go b/internal/glfwwin/win32monitor_windows.go index 0cfa6b4c8..37b0d7fdd 100644 --- a/internal/glfwwin/win32monitor_windows.go +++ b/internal/glfwwin/win32monitor_windows.go @@ -66,7 +66,7 @@ func createMonitor(adapter *_DISPLAY_DEVICEW, display *_DISPLAY_DEVICEW) (*Monit } func pollMonitorsWin32() error { - if !_EnumDisplayDevicesW_Available() { + if isXbox() { return nil } diff --git a/internal/glfwwin/win32window_windows.go b/internal/glfwwin/win32window_windows.go index e565855f5..49ddcf2d4 100644 --- a/internal/glfwwin/win32window_windows.go +++ b/internal/glfwwin/win32window_windows.go @@ -714,7 +714,7 @@ func windowProc(hWnd windows.HWND, uMsg uint32, wParam _WPARAM, lParam _LPARAM) scancode := uint32((_HIWORD(uint32(lParam)) & (_KF_EXTENDED | 0xff))) if scancode == 0 { - if !_MapVirtualKeyW_Available() { + if isXbox() { break } // NOTE: Some synthetic key messages have a scancode of zero @@ -1337,7 +1337,7 @@ func registerWindowClassWin32() error { // In the original GLFW implementation, an embedded resource GLFW_ICON is used if possible. // See https://www.glfw.org/docs/3.3/group__window.html - if _LoadImageW_Available() { + if !isXbox() { icon, err := _LoadImageW(0, _IDI_APPLICATION, _IMAGE_ICON, 0, 0, _LR_DEFAULTSIZE|_LR_SHARED) if err != nil { return err @@ -2123,7 +2123,7 @@ func (c *Cursor) platformCreateCursor(image *Image, xhot, yhot int) error { } func (c *Cursor) platformCreateStandardCursor(shape StandardCursor) error { - if !_LoadImageW_Available() { + if isXbox() { return nil }