Skip to content

Commit 56347f7

Browse files
committed
MSS: improve monitors finding
By saving several lookups.
1 parent d88aa55 commit 56347f7

File tree

4 files changed

+50
-40
lines changed

4 files changed

+50
-40
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ History:
77
- MSS: improve type annotations and add CI check
88
- MSS: use __slots__ for better performances
99
- MSS: better handle resources to prevent leaks
10+
- MSS: improve monitors finding
1011
- Windows: use our own instances of GDI32 and User32 DLLs
1112
- doc: add project_urls to setup.cfg
1213
- doc: add an example using the multiprocessing module (closes #82)

mss/darwin.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def monitors(self):
126126
""" Get positions of monitors (see parent class). """
127127

128128
if not self._monitors:
129+
int_ = int
130+
core = self.core
131+
129132
# All monitors
130133
# We need to update the value with every single monitor found
131134
# using CGRectUnion. Else we will end with infinite values.
@@ -135,36 +138,36 @@ def monitors(self):
135138
# Each monitors
136139
display_count = ctypes.c_uint32(0)
137140
active_displays = (ctypes.c_uint32 * self.max_displays)()
138-
self.core.CGGetActiveDisplayList(
141+
core.CGGetActiveDisplayList(
139142
self.max_displays, active_displays, ctypes.byref(display_count)
140143
)
141144
rotations = {0.0: "normal", 90.0: "right", -90.0: "left"}
142145
for idx in range(display_count.value):
143146
display = active_displays[idx]
144-
rect = self.core.CGDisplayBounds(display)
145-
rect = self.core.CGRectStandardize(rect)
147+
rect = core.CGDisplayBounds(display)
148+
rect = core.CGRectStandardize(rect)
146149
width, height = rect.size.width, rect.size.height
147-
rot = self.core.CGDisplayRotation(display)
150+
rot = core.CGDisplayRotation(display)
148151
if rotations[rot] in ["left", "right"]:
149152
width, height = height, width
150153
self._monitors.append(
151154
{
152-
"left": int(rect.origin.x),
153-
"top": int(rect.origin.y),
154-
"width": int(width),
155-
"height": int(height),
155+
"left": int_(rect.origin.x),
156+
"top": int_(rect.origin.y),
157+
"width": int_(width),
158+
"height": int_(height),
156159
}
157160
)
158161

159162
# Update AiO monitor's values
160-
all_monitors = self.core.CGRectUnion(all_monitors, rect)
163+
all_monitors = core.CGRectUnion(all_monitors, rect)
161164

162165
# Set the AiO monitor's values
163166
self._monitors[0] = {
164-
"left": int(all_monitors.origin.x),
165-
"top": int(all_monitors.origin.y),
166-
"width": int(all_monitors.size.width),
167-
"height": int(all_monitors.size.height),
167+
"left": int_(all_monitors.origin.x),
168+
"top": int_(all_monitors.origin.y),
169+
"width": int_(all_monitors.size.width),
170+
"height": int_(all_monitors.size.height),
168171
}
169172

170173
return self._monitors

mss/linux.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -359,37 +359,40 @@ def monitors(self):
359359

360360
if not self._monitors:
361361
display = MSS.display
362+
int_ = int
363+
xrandr = self.xrandr
362364

363365
# All monitors
364366
gwa = XWindowAttributes()
365367
self.xlib.XGetWindowAttributes(display, self.root, ctypes.byref(gwa))
366368
self._monitors.append(
367369
{
368-
"left": int(gwa.x),
369-
"top": int(gwa.y),
370-
"width": int(gwa.width),
371-
"height": int(gwa.height),
370+
"left": int_(gwa.x),
371+
"top": int_(gwa.y),
372+
"width": int_(gwa.width),
373+
"height": int_(gwa.height),
372374
}
373375
)
374376

375377
# Each monitors
376-
mon = self.xrandr.XRRGetScreenResourcesCurrent(display, self.drawable)
377-
for idx in range(mon.contents.ncrtc):
378-
crtc = self.xrandr.XRRGetCrtcInfo(display, mon, mon.contents.crtcs[idx])
379-
if crtc.contents.noutput == 0:
380-
self.xrandr.XRRFreeCrtcInfo(crtc)
378+
mon = xrandr.XRRGetScreenResourcesCurrent(display, self.drawable).contents
379+
crtcs = mon.crtcs
380+
for idx in range(mon.ncrtc):
381+
crtc = xrandr.XRRGetCrtcInfo(display, mon, crtcs[idx]).contents
382+
if crtc.noutput == 0:
383+
xrandr.XRRFreeCrtcInfo(crtc)
381384
continue
382385

383386
self._monitors.append(
384387
{
385-
"left": int(crtc.contents.x),
386-
"top": int(crtc.contents.y),
387-
"width": int(crtc.contents.width),
388-
"height": int(crtc.contents.height),
388+
"left": int_(crtc.x),
389+
"top": int_(crtc.y),
390+
"width": int_(crtc.width),
391+
"height": int_(crtc.height),
389392
}
390393
)
391-
self.xrandr.XRRFreeCrtcInfo(crtc)
392-
self.xrandr.XRRFreeScreenResources(mon)
394+
xrandr.XRRFreeCrtcInfo(crtc)
395+
xrandr.XRRFreeScreenResources(mon)
393396

394397
return self._monitors
395398

mss/windows.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,17 @@ def monitors(self):
180180
""" Get positions of monitors (see parent class). """
181181

182182
if not self._monitors:
183+
int_ = int
184+
user32 = self.user32
185+
get_system_metrics = user32.GetSystemMetrics
186+
183187
# All monitors
184-
sm_xvirtualscreen, sm_yvirtualscreen = 76, 77
185-
sm_cxvirtualscreen, sm_cyvirtualscreen = 78, 79
186188
self._monitors.append(
187189
{
188-
"left": int(self.user32.GetSystemMetrics(sm_xvirtualscreen)),
189-
"top": int(self.user32.GetSystemMetrics(sm_yvirtualscreen)),
190-
"width": int(self.user32.GetSystemMetrics(sm_cxvirtualscreen)),
191-
"height": int(self.user32.GetSystemMetrics(sm_cyvirtualscreen)),
190+
"left": int_(get_system_metrics(76)), # SM_XVIRTUALSCREEN
191+
"top": int_(get_system_metrics(77)), # SM_YVIRTUALSCREEN
192+
"width": int_(get_system_metrics(78)), # SM_CXVIRTUALSCREEN
193+
"height": int_(get_system_metrics(79)), # SM_CYVIRTUALSCREEN
192194
}
193195
)
194196

@@ -199,20 +201,21 @@ def _callback(monitor, data, rect, dc_):
199201
Callback for monitorenumproc() function, it will return
200202
a RECT with appropriate values.
201203
"""
202-
del monitor, data, dc_
204+
# pylint: disable=unused-argument
205+
203206
rct = rect.contents
204207
self._monitors.append(
205208
{
206-
"left": int(rct.left),
207-
"top": int(rct.top),
208-
"width": int(rct.right - rct.left),
209-
"height": int(rct.bottom - rct.top),
209+
"left": int_(rct.left),
210+
"top": int_(rct.top),
211+
"width": int_(rct.right - rct.left),
212+
"height": int_(rct.bottom - rct.top),
210213
}
211214
)
212215
return 1
213216

214217
callback = self.monitorenumproc(_callback)
215-
self.user32.EnumDisplayMonitors(0, 0, callback, 0)
218+
user32.EnumDisplayMonitors(0, 0, callback, 0)
216219

217220
return self._monitors
218221

0 commit comments

Comments
 (0)