Skip to content

Commit

Permalink
New approach based on Monitor() class to access all properties and fu…
Browse files Browse the repository at this point in the history
…nctionalities. macOS is still experimental and not tested on multi-monitor setups.
  • Loading branch information
Kalmat committed Jul 19, 2023
1 parent ac4fc14 commit fbdb46a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
Binary file modified dist/PyMonCtl-0.0.9-py3-none-any.whl
Binary file not shown.
7 changes: 4 additions & 3 deletions src/pymonctl/_pymonctl_macos.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,9 @@ def setScale(self, scale: Tuple[float, float]):
if scale is not None:
scaleX, scaleY = scale
mode = Quartz.CGDisplayCopyDisplayMode(self.handle)
for m in Quartz.CGDisplayCopyAllDisplayModes(self.handle, {
Quartz.kCGDisplayShowDuplicateHighResolutionModes: Quartz.kCFBooleanTrue}):
for m in Quartz.CGDisplayCopyAllDisplayModes(self.handle, {}):
# for m in Quartz.CGDisplayCopyAllDisplayModes(self.handle, {
# Quartz.kCGDisplayShowDuplicateHighResolutionModes: Quartz.kCFBooleanTrue}):
if Quartz.CGDisplayModeGetWidth(mode) == Quartz.CGDisplayModeGetWidth(m) and \
Quartz.CGDisplayModeGetHeight(mode) == Quartz.CGDisplayModeGetHeight(m) and \
_getModeScale(m) == (scaleX, scaleY):
Expand Down Expand Up @@ -425,7 +426,7 @@ def setPrimary(self):
self.setPosition(PRIMARY, "")

def turnOn(self):
# This works, but won't wake up the display
# This works, but won't wake up the display despite if the mouse is moving and/or clicking

def mouseEvent(eventType, posx, posy):
theEvent = CG.CGEventCreateMouseEvent(None, eventType, CG.CGPointMake(posx, posy), CG.kCGMouseButtonLeft)
Expand Down
28 changes: 14 additions & 14 deletions src/pymonctl/_pymonctl_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import win32gui

from pymonctl import BaseMonitor, _getRelativePosition
from .structs import *
from structs import *


dpiAware = ctypes.windll.user32.GetAwarenessFromDpiAwarenessContext(ctypes.windll.user32.GetThreadDpiAwarenessContext())
Expand Down Expand Up @@ -146,12 +146,12 @@ def __init__(self, handle: Optional[int] = None):
hMon = win32api.MonitorFromPoint((0, 0), win32con.MONITOR_DEFAULTTOPRIMARY)
if hMon and hMon.handle > 0:
handle = hMon.handle
if handle:
self.handle = handle
monitorInfo = win32api.GetMonitorInfo(handle)
self.name = monitorInfo.get("Device", "")
else:
raise ValueError
if handle:
self.handle = handle
monitorInfo = win32api.GetMonitorInfo(handle)
self.name = monitorInfo.get("Device", "")
else:
raise ValueError

@property
def size(self) -> Optional[Size]:
Expand Down Expand Up @@ -265,7 +265,7 @@ def setOrientation(self, orientation: Optional[Union[int, Orientation]]):
devmode.PelsWidth, devmode.PelsHeight = devmode.PelsHeight, devmode.PelsWidth # type: ignore[misc]
devmode.DisplayOrientation = orientation # type: ignore[misc]
# devmode.Fields = devmode.Fields | win32con.DM_DISPLAYORIENTATION | win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT
win32api.ChangeDisplaySettingsEx(self.name, devmode, 0)
win32api.ChangeDisplaySettingsEx(self.name, devmode, 0) # type: ignore[arg-type]

@property
def frequency(self) -> Optional[float]:
Expand Down Expand Up @@ -353,7 +353,7 @@ def setMode(self, mode: Optional[DisplayMode]):
devmode.PelsHeight = mode.height # type: ignore[misc]
devmode.DisplayFrequency = mode.frequency # type: ignore[misc]
devmode.Fields = devmode.Fields | win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT | win32con.DM_DISPLAYFREQUENCY # type: ignore[misc]
win32api.ChangeDisplaySettingsEx(self.name, devmode, 0)
win32api.ChangeDisplaySettingsEx(self.name, devmode, 0) # type: ignore[arg-type]

@property
def defaultMode(self) -> Optional[DisplayMode]:
Expand Down Expand Up @@ -464,7 +464,7 @@ def attach(self):
# devmode.PelsWidth = settings.PelsWidth
# devmode.PelsHeight = settings.PelsHeight
devmode.Fields = devmode.Fields | win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT # type: ignore[misc]
win32api.ChangeDisplaySettingsEx(self.name, devmode, win32con.CDS_UPDATEREGISTRY)
win32api.ChangeDisplaySettingsEx(self.name, devmode, win32con.CDS_UPDATEREGISTRY) # type: ignore[arg-type]
for mon in win32api.EnumDisplayMonitors():
hMon = mon[0].handle
monInfo = win32api.GetMonitorInfo(hMon)
Expand All @@ -480,7 +480,7 @@ def detach(self, permanent: bool = False):
devmode.Position_x = 0
devmode.Position_y = 0
devmode.Fields = devmode.Fields | win32con.DM_PELSWIDTH | win32con.DM_PELSHEIGHT | win32con.DM_POSITION
win32api.ChangeDisplaySettingsEx(self.name, devmode, win32con.CDS_UPDATEREGISTRY if permanent else 0)
win32api.ChangeDisplaySettingsEx(self.name, devmode, win32con.CDS_UPDATEREGISTRY if permanent else 0) # type: ignore[arg-type]

@property
def isAttached(self) -> Optional[bool]:
Expand Down Expand Up @@ -508,7 +508,7 @@ def _setPrimary(name: str, commit: bool = True):
devmode.Position_y = 0
devmode.Fields = devmode.Fields | win32con.DM_POSITION
flags = win32con.CDS_SET_PRIMARY | win32con.CDS_UPDATEREGISTRY | win32con.CDS_NORESET
win32api.ChangeDisplaySettingsEx(name, devmode, flags)
win32api.ChangeDisplaySettingsEx(name, devmode, flags) # type: ignore[arg-type]
break

flags = win32con.CDS_UPDATEREGISTRY | win32con.CDS_NORESET
Expand All @@ -522,7 +522,7 @@ def _setPrimary(name: str, commit: bool = True):
devmode.Position_x = x
devmode.Position_y = y
devmode.Fields = devmode.Fields | win32con.DM_POSITION
win32api.ChangeDisplaySettingsEx(monName, devmode, flags)
win32api.ChangeDisplaySettingsEx(monName, devmode, flags) # type: ignore[arg-type]
if commit:
win32api.ChangeDisplaySettingsEx()

Expand Down Expand Up @@ -555,7 +555,7 @@ def _setPosition(relativePos: Union[int, Position], relativeTo: Optional[str], n
devmode.Position_y = y # type: ignore[misc]
devmode.Fields = devmode.Fields | win32con.DM_POSITION # type: ignore[misc]
flags = win32con.CDS_UPDATEREGISTRY | win32con.CDS_NORESET
win32api.ChangeDisplaySettingsEx(name, devmode, flags)
win32api.ChangeDisplaySettingsEx(name, devmode, flags) # type: ignore[arg-type]
if commit:
win32api.ChangeDisplaySettingsEx()

Expand Down

0 comments on commit fbdb46a

Please sign in to comment.