@@ -253,6 +253,7 @@ def enum_display_monitors(self, screen=0):
253253
254254 if screen == - 1 :
255255 rect = CGRectInfinite
256+ self .debug ('enum_display_monitors' , 'rect' , rect )
256257 yield ({
257258 b'left' : int (rect .origin .x ),
258259 b'top' : int (rect .origin .y ),
@@ -263,11 +264,14 @@ def enum_display_monitors(self, screen=0):
263264 max_displays = 32 # Could be augmented, if needed ...
264265 rotations = {0.0 : 'normal' , 90.0 : 'right' , - 90.0 : 'left' }
265266 _ , ids , _ = CGGetActiveDisplayList (max_displays , None , None )
267+ self .debug ('enum_display_monitors' , 'ids' , ids )
266268 for display in ids :
267269 rect = CGRectStandardize (CGDisplayBounds (display ))
270+ self .debug ('enum_display_monitors' , 'rect' , rect )
268271 left , top = rect .origin .x , rect .origin .y
269272 width , height = rect .size .width , rect .size .height
270273 rot = CGDisplayRotation (display )
274+ self .debug ('enum_display_monitors' , 'rot' , rot )
271275 if rotations [rot ] in ['left' , 'right' ]:
272276 width , height = height , width
273277 yield ({
@@ -290,6 +294,12 @@ def get_pixels(self, monitor):
290294 winid = kCGNullWindowID
291295 default = kCGWindowImageDefault
292296 self .image = CGWindowListCreateImage (rect , options , winid , default )
297+
298+ self .debug ('get_pixels' , 'rect' , rect )
299+ self .debug ('get_pixels' , 'options' , options )
300+ self .debug ('get_pixels' , 'winid' , winid )
301+ self .debug ('get_pixels' , 'default' , default )
302+
293303 if not self .image :
294304 raise ScreenshotError ('MSS: CGWindowListCreateImage() failed.' )
295305 return self .image
@@ -300,9 +310,13 @@ def save_img(self, data, width, height, output):
300310 self .debug ('save_img' )
301311
302312 url = NSURL .fileURLWithPath_ (output )
303- if not CGImageDestinationCreateWithURL (url , kUTTypePNG , 1 , None ):
313+ dest = CGImageDestinationCreateWithURL (url , kUTTypePNG , 1 , None )
314+ self .debug ('save_img' , 'url' , url )
315+ self .debug ('save_img' , 'dest' , dest )
316+ if not dest :
304317 err = 'MSS : CGImageDestinationCreateWithURL() failed.'
305318 raise ScreenshotError (err )
319+
306320 CGImageDestinationAddImage (dest , data , None )
307321 if not CGImageDestinationFinalize (dest ):
308322 raise ScreenshotError ('MSS: CGImageDestinationFinalize() failed.' )
@@ -333,13 +347,13 @@ def __init__(self):
333347 if not x11 :
334348 raise ScreenshotError ('MSS: no X11 library found.' )
335349 self .xlib = cdll .LoadLibrary (x11 )
336- self .debug ('init ' , 'xlib' , self .xlib )
350+ self .debug ('__init__ ' , 'self. xlib' , self .xlib )
337351
338352 xrandr = find_library ('Xrandr' )
339353 if not xrandr :
340354 raise ScreenshotError ('MSS: no Xrandr library found.' )
341355 self .xrandr = cdll .LoadLibrary (xrandr )
342- self .debug ('init ' , 'xrandr' , self .xrandr )
356+ self .debug ('__init__ ' , 'self. xrandr' , self .xrandr )
343357
344358 self ._set_argtypes ()
345359 self ._set_restypes ()
@@ -354,16 +368,16 @@ def __init__(self):
354368 except KeyError :
355369 err = 'MSS: $DISPLAY not set. Stopping to prevent segfault.'
356370 raise ScreenshotError (err )
357- self .debug ('init ' , '$DISPLAY' , disp )
371+ self .debug ('__init__ ' , '$DISPLAY' , disp )
358372
359373 # At this point, if there is no running server, it could end on
360374 # a segmentation fault. And we cannot catch it.
361375 self .display = self .xlib .XOpenDisplay (disp )
362- self .debug ('init ' , 'display' , self .display )
376+ self .debug ('__init__ ' , 'self. display' , self .display )
363377 self .screen = self .xlib .XDefaultScreen (self .display )
364- self .debug ('init ' , 'screen' , self .screen )
378+ self .debug ('__init__ ' , 'self. screen' , self .screen )
365379 self .root = self .xlib .XDefaultRootWindow (self .display , self .screen )
366- self .debug ('init ' , 'root' , self .root )
380+ self .debug ('__init__ ' , 'self. root' , self .root )
367381
368382 def _set_argtypes (self ):
369383 ''' Functions arguments. '''
@@ -422,6 +436,7 @@ def enum_display_monitors(self, screen=0):
422436 if screen == - 1 :
423437 gwa = XWindowAttributes ()
424438 self .xlib .XGetWindowAttributes (self .display , self .root , byref (gwa ))
439+ self .debug ('enum_display_monitors' , 'gwa' , gwa )
425440 yield ({
426441 b'left' : int (gwa .x ),
427442 b'top' : int (gwa .y ),
@@ -433,6 +448,8 @@ def enum_display_monitors(self, screen=0):
433448 # expected LP_Display instance instead of LP_XWindowAttributes
434449 root = cast (self .root , POINTER (Display ))
435450 mon = self .xrandr .XRRGetScreenResources (self .display , root )
451+ self .debug ('enum_display_monitors' , 'root' , root )
452+ self .debug ('enum_display_monitors' , 'mon' , mon )
436453 self .debug ('enum_display_monitors' , 'number of monitors' ,
437454 mon .contents .ncrtc )
438455 for num in range (mon .contents .ncrtc ):
@@ -462,9 +479,11 @@ def get_pixels(self, monitor):
462479 # Fix for XGetImage:
463480 # expected LP_Display instance instead of LP_XWindowAttributes
464481 root = cast (self .root , POINTER (Display ))
482+ self .debug ('get_pixels' , 'root' , root )
465483
466484 ximage = self .xlib .XGetImage (self .display , root , left , top , width ,
467485 height , allplanes , ZPixmap )
486+ self .debug ('get_pixels' , 'ximage' , ximage )
468487 if not ximage :
469488 raise ScreenshotError ('MSS: XGetImage() failed.' )
470489
@@ -474,17 +493,23 @@ def pix(pixel, _resultats={}, b=pack):
474493 This method uses of memoization.
475494 '''
476495 if pixel not in _resultats :
477- _resultats [pixel ] = b (b'<B' , (pixel & 16711680 ) >> 16 ) + \
478- b (b'<B' , (pixel & 65280 ) >> 8 ) + b (b'<B' , pixel & 255 )
496+ _resultats [pixel ] = b (b'<B' , (pixel & rmask ) >> 16 ) + \
497+ b (b'<B' , (pixel & gmask ) >> 8 ) + b (b'<B' , pixel & bmask )
479498 return _resultats [pixel ]
480499
481500 # http://cgit.freedesktop.org/xorg/lib/libX11/tree/src/ImUtil.c#n444
482501 get_pix = self .xlib .XGetPixel
502+ rmask = ximage .contents .red_mask
503+ gmask = ximage .contents .green_mask
504+ bmask = ximage .contents .blue_mask
505+ self .debug ('get_pixels' , 'rmask' , rmask )
506+ self .debug ('get_pixels' , 'gmask' , gmask )
507+ self .debug ('get_pixels' , 'bmask' , bmask )
483508 pixels = [pix (get_pix (ximage , x , y ))
484509 for y in range (height ) for x in range (width )]
510+ self .image = b'' .join (pixels )
485511
486512 self .xlib .XDestroyImage (ximage )
487- self .image = b'' .join (pixels )
488513 return self .image
489514
490515
@@ -644,7 +669,6 @@ def main():
644669
645670 systems = {'Darwin' : MSSMac , 'Linux' : MSSLinux , 'Windows' : MSSWindows }
646671 mss = systems [system ()]()
647- # mss.DEBUG = True
648672
649673 def on_exists (fname ):
650674 ''' Callback example when we try to overwrite an existing
0 commit comments