Skip to content

Commit

Permalink
Add debug logging to Windows API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
newAM committed Dec 1, 2023
1 parent 63865b7 commit 4cf2c92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html),
as of version 2.1.1.

## [Unreleased]
### Added
- Added additional logging for Windows API calls.

## [3.1.0] - 2023-10-10
### Added
- Added `get_color_preset` and `set_color_preset`.
Expand Down
16 changes: 15 additions & 1 deletion monitorcontrol/vcp/vcp_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from types import TracebackType
from typing import List, Optional, Tuple, Type
import ctypes
import logging
import sys

# hide the Windows code from Linux CI coverage
Expand Down Expand Up @@ -35,10 +36,12 @@ def __init__(self, hmonitor: HMONITOR):
Args:
hmonitor: logical monitor handle
"""
self.logger = logging.getLogger(__name__)
self.hmonitor = hmonitor

def __enter__(self):
num_physical = DWORD()
self.logger.debug("GetNumberOfPhysicalMonitorsFromHMONITOR")
try:
if not ctypes.windll.dxva2.GetNumberOfPhysicalMonitorsFromHMONITOR(
self.hmonitor, ctypes.byref(num_physical)
Expand All @@ -61,6 +64,7 @@ def __enter__(self):
raise VCPError("more than one physical monitor per hmonitor")

physical_monitors = (PhysicalMonitor * num_physical.value)()
self.logger.debug("GetPhysicalMonitorsFromHMONITOR")
try:
if not ctypes.windll.dxva2.GetPhysicalMonitorsFromHMONITOR(
self.hmonitor, num_physical.value, physical_monitors
Expand All @@ -81,6 +85,7 @@ def __exit__(
exception_value: Optional[BaseException],
exception_traceback: Optional[TracebackType],
) -> Optional[bool]:
self.logger.debug("DestroyPhysicalMonitor")
try:
if not ctypes.windll.dxva2.DestroyPhysicalMonitor(self.handle):
raise VCPError(
Expand All @@ -101,6 +106,7 @@ def set_vcp_feature(self, code: int, value: int):
Raises:
VCPError: Failed to set VCP feature.
"""
self.logger.debug(f"SetVCPFeature(_, {code=}, {value=})")
try:
if not ctypes.windll.dxva2.SetVCPFeature(
HANDLE(self.handle), BYTE(code), DWORD(value)
Expand All @@ -124,6 +130,9 @@ def get_vcp_feature(self, code: int) -> Tuple[int, int]:
"""
feature_current = DWORD()
feature_max = DWORD()
self.logger.debug(
f"GetVCPFeatureAndVCPFeatureReply(_, {code=}, None, _, _)"
)
try:
if not ctypes.windll.dxva2.GetVCPFeatureAndVCPFeatureReply(
HANDLE(self.handle),
Expand All @@ -135,6 +144,10 @@ def get_vcp_feature(self, code: int) -> Tuple[int, int]:
raise VCPError("failed to get VCP feature: " + ctypes.FormatError())
except OSError as e:
raise VCPError("failed to get VCP feature") from e
self.logger.debug(
"GetVCPFeatureAndVCPFeatureReply -> "
f"({feature_current.value}, {feature_max.value})"
)
return feature_current.value, feature_max.value

def get_vcp_capabilities(self):
Expand All @@ -153,7 +166,7 @@ def get_vcp_capabilities(self):
"""

cap_length = DWORD()

self.logger.debug("GetCapabilitiesStringLength")
try:
if not ctypes.windll.dxva2.GetCapabilitiesStringLength(
HANDLE(self.handle), ctypes.byref(cap_length)
Expand All @@ -162,6 +175,7 @@ def get_vcp_capabilities(self):
"failed to get VCP capabilities: " + ctypes.FormatError()
)
cap_string = (ctypes.c_char * cap_length.value)()
self.logger.debug("CapabilitiesRequestAndCapabilitiesReply")
if not ctypes.windll.dxva2.CapabilitiesRequestAndCapabilitiesReply(
HANDLE(self.handle), cap_string, cap_length
):
Expand Down

0 comments on commit 4cf2c92

Please sign in to comment.