From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / OSX / grabclient-osx.m
1 /* xscreensaver, Copyright (c) 1992-2017 Jamie Zawinski <jwz@jwz.org>
2  *
3  * Permission to use, copy, modify, distribute, and sell this software and its
4  * documentation for any purpose is hereby granted without fee, provided that
5  * the above copyright notice appear in all copies and that both that
6  * copyright notice and this permission notice appear in supporting
7  * documentation.  No representations are made about the suitability of this
8  * software for any purpose.  It is provided "as is" without express or 
9  * implied warranty.
10  */
11
12 /* This is the OSX implementation of desktop-grabbing and image-loading.
13    This code is invoked by "utils/grabclient.c", which is linked directly
14    in to each screen saver bundle.
15
16    X11-based builds of the savers do not use this code (even on MacOS).
17    This is used only by the Cocoa build of the savers.
18  */
19
20 #import <stdlib.h>
21 #import <stdint.h>
22 #ifndef USE_IPHONE
23 # import <Cocoa/Cocoa.h>
24 #else
25 # import "SaverRunner.h"
26 #endif
27 #import "jwxyz-cocoa.h"
28 #import "grabscreen.h"
29 #import "colorbars.h"
30 #import "resources.h"
31 #import "usleep.h"
32
33
34 #ifdef USE_IPHONE
35 # define NSImage UIImage
36 #endif
37
38
39 #ifndef  MAC_OS_X_VERSION_10_6
40 # define MAC_OS_X_VERSION_10_6 1060  /* undefined in 10.4 SDK, grr */
41 #endif
42
43 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
44
45      /* 10.4 code.
46
47         This version of the code works on 10.4, but is flaky.  There is
48         a better way to do it on 10.5 and newer, but taking this path,
49         then we are being compiled against the 10.4 SDK instead of the
50         10.5 SDK, and the newer API is not available to us.
51       */
52
53 static void
54 copy_framebuffer_to_ximage (CGDirectDisplayID cgdpy, XImage *xim,
55                             int window_x, int window_y)
56 {
57   unsigned char *data = (unsigned char *) 
58     CGDisplayAddressForPosition (cgdpy, window_x, window_y);
59   int bpp = CGDisplayBitsPerPixel (cgdpy);
60   int spp = CGDisplaySamplesPerPixel (cgdpy);
61   int bps = CGDisplayBitsPerSample (cgdpy);
62   int bpr = CGDisplayBytesPerRow (cgdpy);
63
64   int y;
65   int ximw = xim->width;
66   int ximh = xim->height;
67
68   uint32_t *odata = (uint32_t *) xim->data;
69
70   switch (bpp) {
71   case 32:
72     if (spp != 3) abort();
73     if (bps != 8) abort();
74     int xwpl = xim->bytes_per_line/4;
75     for (y = 0; y < ximh; y++) {
76       // We can do this because the frame buffer and XImage are both ARGB 32.
77       // Both PPC and Intel use ARGB, viewed in word order (not byte-order).
78       memcpy (odata, data, ximw * 4);
79       odata += xwpl;
80       data += bpr;
81     }
82     break;
83
84   case 16:
85     if (spp != 3) abort();
86     if (bps != 5) abort();
87     for (y = 0; y < ximh; y++) {
88       uint16_t *ip = (uint16_t *) data;
89       int x;
90       for (x = 0; x < ximw; x++) {
91         uint16_t p = *ip++;
92         // This should be ok on both PPC and Intel (ARGB, word order)
93         unsigned char r = (p >> 10) & 0x1F;
94         unsigned char g = (p >>  5) & 0x1F;
95         unsigned char b = (p      ) & 0x1F;
96         r = (r << 3) | (r >> 2);
97         g = (g << 3) | (g >> 2);
98         b = (b << 3) | (b >> 2);
99         uint32_t pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
100         // XPutPixel (xim, x, y, pixel);
101         *odata++ = pixel;
102       }
103       data += bpr;
104     }
105     break;
106
107   case 8:
108     {
109       /* Get the current palette of the display. */
110       CGDirectPaletteRef pal = CGPaletteCreateWithDisplay (cgdpy);
111
112       /* Map it to 32bpp pixels */
113       uint32_t map[256];
114       for (y = 0; y < 256; y++) {
115         CGDeviceColor c = CGPaletteGetColorAtIndex (pal, y);
116         unsigned char r = c.red   * 255.0;
117         unsigned char g = c.green * 255.0;
118         unsigned char b = c.blue  * 255.0;
119         uint32_t pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
120         map[y] = pixel;
121       }
122
123       for (y = 0; y < ximh; y++) {
124         unsigned char *ip = data;
125         int x;
126         for (x = 0; x < ximw; x++) {
127           *odata++ = map[*ip++];
128         }
129         data += bpr;
130       }
131       CGPaletteRelease (pal);
132     }
133     break;
134
135   default:
136     abort();
137     break;
138   }
139 }
140
141
142 /* Loads an image into the Drawable, returning once the image is loaded.
143  */
144 Bool
145 osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
146                         XRectangle *geom_ret)
147 {
148   Display *dpy = DisplayOfScreen (screen);
149   NSView *nsview = jwxyz_window_view (xwindow);
150   NSWindow *nswindow = [nsview window];
151   XWindowAttributes xgwa;
152   int window_x, window_y;
153   Window unused;
154
155   // Figure out where this window is on the screen.
156   //
157   XGetWindowAttributes (dpy, xwindow, &xgwa);
158   XTranslateCoordinates (dpy, xwindow, RootWindowOfScreen (screen), 0, 0, 
159                          &window_x, &window_y, &unused);
160
161   // Use the size of the Drawable, not the Window.
162   {
163     Window r;
164     int x, y;
165     unsigned int w, h, bbw, d;
166     XGetGeometry (dpy, drawable, &r, &x, &y, &w, &h, &bbw, &d);
167     xgwa.width = w;
168     xgwa.height = h;
169   }
170
171   // Create a tmp ximage to hold the screen data.
172   //
173   XImage *xim = XCreateImage (dpy, xgwa.visual, 32, ZPixmap, 0, 0,
174                               xgwa.width, xgwa.height, 8, 0);
175   xim->data = (char *) malloc (xim->height * xim->bytes_per_line);
176
177
178   // Find the address in the frame buffer of the top left of this window.
179   //
180   CGDirectDisplayID cgdpy = 0;
181   {
182     CGPoint p;
183     // #### this isn't quite right for screen 2: it's offset slightly.
184     p.x = window_x;
185     p.y = window_y;
186     CGDisplayCount n;
187     CGGetDisplaysWithPoint (p, 1, &cgdpy, &n);
188     if (!cgdpy) abort();
189   }
190
191   // Paint a transparent "hole" in this window.
192   //
193   BOOL oopaque = [nswindow isOpaque];
194   [nswindow setOpaque:NO];
195
196   [[NSColor clearColor] set];
197   NSRectFill ([nsview frame]);
198   [[nswindow graphicsContext] flushGraphics];
199
200
201   // Without this, we get a dozen black scanlines at the top.
202   // #### But with this, the screen saver loops, because calling this
203   //      seems to implicitly mark the display as non-idle!
204   // CGDisplayCaptureWithOptions (cgdpy, kCGCaptureNoFill);
205
206   // #### So let's try waiting for the vertical blank instead...
207   //      Nope, that doesn't work.
208   //
209   // CGDisplayWaitForBeamPositionOutsideLines (cgdpy, 0,
210   //   window_y + [nswindow frame].size.height);
211
212   // #### Ok, try a busy-wait?
213   //      Nope.
214   //
215
216   // #### Ok, just fuckin' sleep!
217   //
218   usleep (100000);
219
220
221   // Pull the bits out of the frame buffer.
222   //
223   copy_framebuffer_to_ximage (cgdpy, xim, window_x, window_y);
224
225   // CGDisplayRelease (cgdpy);
226
227   // Make the window visible again.
228   //
229   [nswindow setOpaque:oopaque];
230
231   // Splat the XImage onto the target drawable (probably the window)
232   // and free the bits.
233   //
234   XGCValues gcv;
235   GC gc = XCreateGC (dpy, drawable, 0, &gcv);
236   XPutImage (dpy, drawable, gc, xim, 0, 0, 0, 0, xim->width, xim->height);
237   XFreeGC (dpy, gc);
238
239   if (geom_ret) {
240     geom_ret->x = 0;
241     geom_ret->y = 0;
242     geom_ret->width  = xim->width;
243     geom_ret->height = xim->height;
244   }
245
246   XDestroyImage (xim);
247   return True;
248 }
249
250
251 #elif defined(USE_IPHONE)
252
253         /* What a hack!
254
255            On iOS, our application delegate, SaverRunner, grabs an image
256            of itself as a UIImage before mapping the XScreenSaverView.
257            In this code, we ask SaverRunner for that UIImage, then copy
258            it to the root window.
259          */
260
261 Bool
262 osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
263                         XRectangle *geom_ret)
264 {
265   SaverRunner *s = 
266     (SaverRunner *) [[UIApplication sharedApplication] delegate];
267   if (! s)
268     return False;
269   if (! [s isKindOfClass:[SaverRunner class]])
270     return False;
271   UIImage *img = [s screenshot];
272   if (! img)
273     return False;
274   jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable,
275                                  True, img, geom_ret, 0);
276   return True;
277 }
278
279
280 #else /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
281
282          10.5+ code.
283
284          This version of the code is simpler and more reliable, but
285          uses an API that only exist on 10.5 and newer, so we can only
286          use it if when being compiled against the 10.5 SDK or later.
287        */
288
289 /* Loads an image into the Drawable, returning once the image is loaded.
290  */
291 Bool
292 osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
293                         XRectangle *geom_ret)
294 {
295   Display *dpy = DisplayOfScreen (screen);
296   NSView *nsview = jwxyz_window_view (xwindow);
297   XWindowAttributes xgwa;
298   int window_x, window_y;
299   Window unused;
300
301   // Figure out where this window is on the screen.
302   //
303   XGetWindowAttributes (dpy, xwindow, &xgwa);
304   XTranslateCoordinates (dpy, xwindow, RootWindowOfScreen (screen), 0, 0, 
305                          &window_x, &window_y, &unused);
306
307   // Grab only the rectangle of the screen underlying this window.
308   //
309   CGRect cgrect;
310   cgrect.origin.x    = window_x;
311   cgrect.origin.y    = window_y;
312   cgrect.size.width  = xgwa.width;
313   cgrect.size.height = xgwa.height;
314
315   /* If a password is required to unlock the screen, a large black
316      window will be on top of all of the desktop windows by the time
317      we reach here, making the screen-grab rather uninteresting.  If
318      we move ourselves temporarily below the login-window windows
319      before capturing the image, we capture the real desktop as
320      intended.
321
322      Oct 2016: Surprise, this trick no longer works on MacOS 10.12.  Sigh.
323    */
324
325   CGWindowID windowNumber = (CGWindowID) nsview.window.windowNumber;
326
327   {
328     CFArrayRef L = CGWindowListCopyWindowInfo (kCGWindowListOptionOnScreenOnly,
329                                                kCGNullWindowID);
330
331     CFIndex n = CFArrayGetCount(L);
332     for (int i = 0; i < n; i++) {
333       NSDictionary *dict = (NSDictionary *) CFArrayGetValueAtIndex(L, i);
334
335       // loginwindow creates multiple toplevel windows. Grab the lowest one.
336       if(![([dict objectForKey:(NSString *)kCGWindowOwnerName])
337            compare:@"loginwindow"]) {
338         windowNumber = ((NSNumber *)[dict objectForKey:
339                                      (NSString *)kCGWindowNumber]).intValue;
340       }
341     }
342   }
343
344   // Grab a screen shot of those windows below this one
345   // (hey, X11 can't do that!)
346   //
347   CGImageRef img = 
348     CGWindowListCreateImage (cgrect,
349                              kCGWindowListOptionOnScreenBelowWindow,
350                              windowNumber,
351                              kCGWindowImageDefault);
352
353   if (! img) return False;
354
355   // Render the grabbed CGImage into the Drawable.
356   jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable, 
357                                  False, img, geom_ret, 0);
358   CGImageRelease (img);
359   return True;
360 }
361
362 #endif /* 10.5+ code */
363
364
365 # ifndef USE_IPHONE
366
367 /* Returns the EXIF rotation property of the image, if any.
368  */
369 static int
370 exif_rotation (const char *filename)
371 {
372   /* As of 10.6, NSImage rotates according to EXIF by default:
373      http://developer.apple.com/mac/library/releasenotes/cocoa/appkit.html
374      So this function should return -1 when *running* on 10.6 systems.
375      But when running against older systems, we need to examine the image
376      to figure out its rotation.
377    */
378
379 # if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6  /* 10.6 SDK */
380
381   /* When we have compiled against the 10.6 SDK, we know that we are 
382      running on a 10.6 or later system.
383    */
384   return -1;
385
386 # else /* Compiled against 10.5 SDK or earlier */
387
388   /* If this selector exists, then we are running against a 10.6 runtime
389      that does automatic EXIF rotation (despite the fact that we were
390      compiled against the 10.5 or earlier SDK).  So in that case, this
391      function should no-op.
392    */
393   if ([NSImage instancesRespondToSelector:
394                  @selector(initWithDataIgnoringOrientation:)])
395     return -1;
396
397   /* Otherwise, go ahead and figure out what the rotational characteristics
398      of this image are. */
399
400
401
402   /* This is a ridiculous amount of rigamarole to go through, but for some
403      reason the "Orientation" tag does not exist in the "NSImageEXIFData"
404      dictionary inside the NSBitmapImageRep of the NSImage.  Several other
405      EXIF tags are there (e.g., shutter speed) but not orientation.  WTF?
406    */
407   CFStringRef s = CFStringCreateWithCString (NULL, filename, 
408                                              kCFStringEncodingUTF8);
409   CFURLRef url = CFURLCreateWithFileSystemPath (NULL, s, 
410                                                 kCFURLPOSIXPathStyle, 0);
411   CGImageSourceRef cgimg = CGImageSourceCreateWithURL (url, NULL);
412   if (! cgimg) return -1;
413
414   NSDictionary *props = (NSDictionary *)
415     CGImageSourceCopyPropertiesAtIndex (cgimg, 0, NULL);
416   int rot = [[props objectForKey:@"Orientation"] intValue];
417   CFRelease (cgimg);
418   CFRelease (url);
419   CFRelease (s);
420   return rot;
421
422 # endif /* 10.5 */
423 }
424
425 # endif /* USE_IPHONE */
426
427
428
429 /* Loads an image file and splats it onto the drawable.
430    The image is drawn as large as possible while preserving its aspect ratio.
431    If geom_ret is provided, the actual rectangle the rendered image takes
432    up will be returned there.
433  */
434 Bool
435 osx_load_image_file (Screen *screen, Window xwindow, Drawable drawable,
436                      const char *filename, XRectangle *geom_ret)
437 {
438 # ifndef USE_IPHONE
439
440   if (!filename || !*filename) return False;
441
442   NSImage *img = [[NSImage alloc] initWithContentsOfFile:
443                                     [NSString stringWithCString:filename
444                                               encoding:NSUTF8StringEncoding]];
445   if (!img)
446     return False;
447
448   jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable, 
449                                  True, img, geom_ret,
450                                  exif_rotation (filename));
451   [img release];
452   return True;
453
454 # else  /* USE_IPHONE */
455
456   /* This is handled differently: see grabclient.c and grabclient-ios.m. */
457   return False;
458
459 # endif /* USE_IPHONE */
460 }