6
6
import ctypes
7
7
import ctypes .util
8
8
import sys
9
+ from ctypes import (
10
+ POINTER ,
11
+ Structure ,
12
+ c_double ,
13
+ c_float ,
14
+ c_int32 ,
15
+ c_uint64 ,
16
+ c_ubyte ,
17
+ c_uint32 ,
18
+ c_void_p ,
19
+ )
9
20
from platform import mac_ver
10
21
from typing import TYPE_CHECKING
11
22
23
34
24
35
25
36
def cgfloat ():
26
- # type: () -> Union[Type[ctypes. c_double], Type[ctypes. c_float]]
37
+ # type: () -> Union[Type[c_double], Type[c_float]]
27
38
""" Get the appropriate value for a float. """
28
39
29
- return ctypes . c_double if sys .maxsize > 2 ** 32 else ctypes . c_float
40
+ return c_double if sys .maxsize > 2 ** 32 else c_float
30
41
31
42
32
- class CGPoint (ctypes . Structure ):
43
+ class CGPoint (Structure ):
33
44
""" Structure that contains coordinates of a rectangle. """
34
45
35
46
_fields_ = [("x" , cgfloat ()), ("y" , cgfloat ())]
@@ -38,7 +49,7 @@ def __repr__(self):
38
49
return "{}(left={} top={})" .format (type (self ).__name__ , self .x , self .y )
39
50
40
51
41
- class CGSize (ctypes . Structure ):
52
+ class CGSize (Structure ):
42
53
""" Structure that contains dimensions of an rectangle. """
43
54
44
55
_fields_ = [("width" , cgfloat ()), ("height" , cgfloat ())]
@@ -49,7 +60,7 @@ def __repr__(self):
49
60
)
50
61
51
62
52
- class CGRect (ctypes . Structure ):
63
+ class CGRect (Structure ):
53
64
""" Structure that contains information about a rectangle. """
54
65
55
66
_fields_ = [("origin" , CGPoint ), ("size" , CGSize )]
@@ -58,6 +69,42 @@ def __repr__(self):
58
69
return "{}<{} {}>" .format (type (self ).__name__ , self .origin , self .size )
59
70
60
71
72
+ # C functions that will be initialised later.
73
+ #
74
+ # This is a dict:
75
+ # cfunction: (attr, argtypes, restype)
76
+ #
77
+ # Available attr: core.
78
+ #
79
+ # Note: keep it sorted by cfunction.
80
+ CFUNCTIONS = {
81
+ "CGDataProviderCopyData" : ("core" , [c_void_p ], c_void_p ),
82
+ "CGDisplayBounds" : ("core" , [c_uint32 ], CGRect ),
83
+ "CGDisplayRotation" : ("core" , [c_uint32 ], c_float ),
84
+ "CFDataGetBytePtr" : ("core" , [c_void_p ], c_void_p ),
85
+ "CFDataGetLength" : ("core" , [c_void_p ], c_uint64 ),
86
+ "CFRelease" : ("core" , [c_void_p ], c_void_p ),
87
+ "CGDataProviderRelease" : ("core" , [c_void_p ], c_void_p ),
88
+ "CGGetActiveDisplayList" : (
89
+ "core" ,
90
+ [c_uint32 , POINTER (c_uint32 ), POINTER (c_uint32 )],
91
+ c_int32 ,
92
+ ),
93
+ "CGImageGetBitsPerPixel" : ("core" , [c_void_p ], int ),
94
+ "CGImageGetBytesPerRow" : ("core" , [c_void_p ], int ),
95
+ "CGImageGetDataProvider" : ("core" , [c_void_p ], c_void_p ),
96
+ "CGImageGetHeight" : ("core" , [c_void_p ], int ),
97
+ "CGImageGetWidth" : ("core" , [c_void_p ], int ),
98
+ "CGRectStandardize" : ("core" , [CGRect ], CGRect ),
99
+ "CGRectUnion" : ("core" , [CGRect , CGRect ], CGRect ),
100
+ "CGWindowListCreateImage" : (
101
+ "core" ,
102
+ [CGRect , c_uint32 , c_uint32 , c_uint32 ],
103
+ c_void_p ,
104
+ ),
105
+ }
106
+
107
+
61
108
class MSS (MSSBase ):
62
109
"""
63
110
Multiple ScreenShots implementation for macOS.
@@ -94,36 +141,15 @@ def _set_cfunctions(self):
94
141
# type: () -> None
95
142
""" Set all ctypes functions and attach them to attributes. """
96
143
97
- uint32 = ctypes .c_uint32
98
- void = ctypes .c_void_p
99
- pointer = ctypes .POINTER
100
144
cfactory = self ._cfactory
101
- core = self .core
102
-
103
- # Note: keep it sorted
104
- for func , argtypes , restype in (
105
- ("CGDataProviderCopyData" , [void ], void ),
106
- ("CGDisplayBounds" , [uint32 ], CGRect ),
107
- ("CGDisplayRotation" , [uint32 ], ctypes .c_float ),
108
- ("CFDataGetBytePtr" , [void ], void ),
109
- ("CFDataGetLength" , [void ], ctypes .c_uint64 ),
110
- ("CFRelease" , [void ], void ),
111
- ("CGDataProviderRelease" , [void ], void ),
112
- (
113
- "CGGetActiveDisplayList" ,
114
- [uint32 , pointer (uint32 ), pointer (uint32 )],
115
- ctypes .c_int32 ,
116
- ),
117
- ("CGImageGetBitsPerPixel" , [void ], int ),
118
- ("CGImageGetBytesPerRow" , [void ], int ),
119
- ("CGImageGetDataProvider" , [void ], void ),
120
- ("CGImageGetHeight" , [void ], int ),
121
- ("CGImageGetWidth" , [void ], int ),
122
- ("CGRectStandardize" , [CGRect ], CGRect ),
123
- ("CGRectUnion" , [CGRect , CGRect ], CGRect ),
124
- ("CGWindowListCreateImage" , [CGRect , uint32 , uint32 , uint32 ], void ),
125
- ):
126
- cfactory (attr = core , func = func , argtypes = argtypes , restype = restype ) # type: ignore
145
+ attrs = {"core" : self .core }
146
+ for func , (attr , argtypes , restype ) in CFUNCTIONS .items ():
147
+ cfactory (
148
+ attr = attrs [attr ],
149
+ func = func ,
150
+ argtypes = argtypes , # type: ignore
151
+ restype = restype ,
152
+ )
127
153
128
154
def _monitors_impl (self ):
129
155
# type: () -> None
@@ -139,8 +165,8 @@ def _monitors_impl(self):
139
165
self ._monitors .append ({})
140
166
141
167
# Each monitors
142
- display_count = ctypes . c_uint32 (0 )
143
- active_displays = (ctypes . c_uint32 * self .max_displays )()
168
+ display_count = c_uint32 (0 )
169
+ active_displays = (c_uint32 * self .max_displays )()
144
170
core .CGGetActiveDisplayList (
145
171
self .max_displays , active_displays , ctypes .byref (display_count )
146
172
)
@@ -196,7 +222,7 @@ def _grab_impl(self, monitor):
196
222
copy_data = core .CGDataProviderCopyData (prov )
197
223
data_ref = core .CFDataGetBytePtr (copy_data )
198
224
buf_len = core .CFDataGetLength (copy_data )
199
- raw = ctypes .cast (data_ref , ctypes . POINTER (ctypes . c_ubyte * buf_len ))
225
+ raw = ctypes .cast (data_ref , POINTER (c_ubyte * buf_len ))
200
226
data = bytearray (raw .contents )
201
227
202
228
# Remove padding per row
0 commit comments