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 Nov 25, 2023
1 parent 0168754 commit ed6b275
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion monitorcontrol/vcp/vcp_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,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 +63,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 +84,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 +105,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 +129,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 +143,9 @@ 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(
f"GetVCPFeatureAndVCPFeatureReply -> ({feature_current.value}, {feature_max.value})"
)
return feature_current.value, feature_max.value

def get_vcp_capabilities(self):
Expand All @@ -153,7 +164,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 +173,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 Expand Up @@ -196,6 +208,8 @@ def _callback(hmonitor, hdc, lprect, lparam):
BOOL, HMONITOR, HDC, ctypes.POINTER(RECT), LPARAM
)
callback = MONITORENUMPROC(_callback)

self.logger.debug("EnumDisplayMonitors")
if not ctypes.windll.user32.EnumDisplayMonitors(0, 0, callback, 0):
raise VCPError("Call to EnumDisplayMonitors failed")
except OSError as e:
Expand Down

0 comments on commit ed6b275

Please sign in to comment.