2018-01-02 21:22:56 +01:00
|
|
|
// Copyright 2018 The Ebiten Authors
|
2016-06-18 17:55:24 +02:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2018-01-02 21:22:56 +01:00
|
|
|
// +build !js
|
2016-06-18 17:55:24 +02:00
|
|
|
|
2018-01-02 21:22:56 +01:00
|
|
|
package devicescale
|
2016-06-18 17:55:24 +02:00
|
|
|
|
2018-01-03 07:43:56 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2018-01-03 08:24:53 +01:00
|
|
|
const logPixelSx = 88
|
|
|
|
|
2018-01-03 07:43:56 +01:00
|
|
|
var (
|
|
|
|
user32 = syscall.NewLazyDLL("user32")
|
2018-01-03 08:24:53 +01:00
|
|
|
gdi32 = syscall.NewLazyDLL("gdi32")
|
2018-01-03 07:43:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
procSetProcessDPIAware = user32.NewProc("SetProcessDPIAware")
|
2018-01-03 08:24:53 +01:00
|
|
|
procGetWindowDC = user32.NewProc("GetWindowDC")
|
|
|
|
procReleaseDC = user32.NewProc("ReleaseDC")
|
|
|
|
procGetDeviceCaps = gdi32.NewProc("GetDeviceCaps")
|
2018-01-03 07:43:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func setProcessDPIAware() error {
|
|
|
|
r, _, e := syscall.Syscall(procSetProcessDPIAware.Addr(), 0, 0, 0, 0)
|
|
|
|
if e != 0 {
|
|
|
|
return fmt.Errorf("devicescale: SetProcessDPIAware failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
if r == 0 {
|
|
|
|
return fmt.Errorf("devicescale: SetProcessDPIAware failed: returned value: %d", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-03 08:24:53 +01:00
|
|
|
func getWindowDC(hwnd uintptr) (uintptr, error) {
|
|
|
|
r, _, e := syscall.Syscall(procGetWindowDC.Addr(), 1, hwnd, 0, 0)
|
|
|
|
if e != 0 {
|
|
|
|
return 0, fmt.Errorf("devicescale: GetWindowDC failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
if r == 0 {
|
|
|
|
return 0, fmt.Errorf("devicescale: GetWindowDC failed: returned value: %d", r)
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func releaseDC(hwnd, hdc uintptr) error {
|
|
|
|
r, _, e := syscall.Syscall(procReleaseDC.Addr(), 2, hwnd, hdc, 0)
|
|
|
|
if e != 0 {
|
|
|
|
return fmt.Errorf("devicescale: ReleaseDC failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
if r == 0 {
|
|
|
|
return fmt.Errorf("devicescale: ReleaseDC failed: returned value: %d", r)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeviceCaps(hdc uintptr, nindex int) (int, error) {
|
|
|
|
r, _, e := syscall.Syscall(procGetDeviceCaps.Addr(), 2, hdc, uintptr(nindex), 0)
|
|
|
|
if e != 0 {
|
|
|
|
return 0, fmt.Errorf("devicescale: GetDeviceCaps failed: error code: %d", e)
|
|
|
|
}
|
|
|
|
return int(r), nil
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:22:56 +01:00
|
|
|
func impl() float64 {
|
2018-01-03 07:43:56 +01:00
|
|
|
if err := setProcessDPIAware(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-03 08:24:53 +01:00
|
|
|
|
|
|
|
dc, err := getWindowDC(0)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
dpi, err := getDeviceCaps(dc, logPixelSx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2018-01-02 21:22:56 +01:00
|
|
|
}
|
2018-01-03 08:24:53 +01:00
|
|
|
if err := releaseDC(0, dc); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-01-02 21:22:56 +01:00
|
|
|
return float64(dpi) / 96
|
2016-06-18 17:55:24 +02:00
|
|
|
}
|