summaryrefslogtreecommitdiffstats
path: root/OSX/grabclient-osx.m
blob: 7bfdcb4fa811b0e921dbd585f571b2422e4e39c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* xscreensaver, Copyright (c) 1992-2018 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 */

/* This is the OSX implementation of desktop-grabbing and image-loading.
   This code is invoked by "utils/grabclient.c", which is linked directly
   in to each screen saver bundle.

   X11-based builds of the savers do not use this code (even on MacOS).
   This is used only by the Cocoa build of the savers.
 */

#import <stdlib.h>
#import <stdint.h>
#ifndef USE_IPHONE
# import <Cocoa/Cocoa.h>
#else
# import "SaverRunner.h"
#endif
#import "jwxyz-cocoa.h"
#import "grabscreen.h"
#import "colorbars.h"
#import "resources.h"
#import "usleep.h"


#ifdef USE_IPHONE
# define NSImage UIImage
#endif


#ifndef  MAC_OS_X_VERSION_10_6
# define MAC_OS_X_VERSION_10_6 1060  /* undefined in 10.4 SDK, grr */
#endif

#if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5

     /* 10.4 code.

        This version of the code works on 10.4, but is flaky.  There is
        a better way to do it on 10.5 and newer, but taking this path,
        then we are being compiled against the 10.4 SDK instead of the
        10.5 SDK, and the newer API is not available to us.
      */

static void
copy_framebuffer_to_ximage (CGDirectDisplayID cgdpy, XImage *xim,
                            int window_x, int window_y)
{
  unsigned char *data = (unsigned char *) 
    CGDisplayAddressForPosition (cgdpy, window_x, window_y);
  int bpp = CGDisplayBitsPerPixel (cgdpy);
  int spp = CGDisplaySamplesPerPixel (cgdpy);
  int bps = CGDisplayBitsPerSample (cgdpy);
  int bpr = CGDisplayBytesPerRow (cgdpy);

  int y;
  int ximw = xim->width;
  int ximh = xim->height;

  uint32_t *odata = (uint32_t *) xim->data;

  switch (bpp) {
  case 32:
    if (spp != 3) abort();
    if (bps != 8) abort();
    int xwpl = xim->bytes_per_line/4;
    for (y = 0; y < ximh; y++) {
      // We can do this because the frame buffer and XImage are both ARGB 32.
      // Both PPC and Intel use ARGB, viewed in word order (not byte-order).
      memcpy (odata, data, ximw * 4);
      odata += xwpl;
      data += bpr;
    }
    break;

  case 16:
    if (spp != 3) abort();
    if (bps != 5) abort();
    for (y = 0; y < ximh; y++) {
      uint16_t *ip = (uint16_t *) data;
      int x;
      for (x = 0; x < ximw; x++) {
        uint16_t p = *ip++;
        // This should be ok on both PPC and Intel (ARGB, word order)
        unsigned char r = (p >> 10) & 0x1F;
        unsigned char g = (p >>  5) & 0x1F;
        unsigned char b = (p      ) & 0x1F;
        r = (r << 3) | (r >> 2);
        g = (g << 3) | (g >> 2);
        b = (b << 3) | (b >> 2);
        uint32_t pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
        // XPutPixel (xim, x, y, pixel);
        *odata++ = pixel;
      }
      data += bpr;
    }
    break;

  case 8:
    {
      /* Get the current palette of the display. */
      CGDirectPaletteRef pal = CGPaletteCreateWithDisplay (cgdpy);

      /* Map it to 32bpp pixels */
      uint32_t map[256];
      for (y = 0; y < 256; y++) {
        CGDeviceColor c = CGPaletteGetColorAtIndex (pal, y);
        unsigned char r = c.red   * 255.0;
        unsigned char g = c.green * 255.0;
        unsigned char b = c.blue  * 255.0;
        uint32_t pixel = 0xFF000000 | (r << 16) | (g << 8) | b;
        map[y] = pixel;
      }

      for (y = 0; y < ximh; y++) {
        unsigned char *ip = data;
        int x;
        for (x = 0; x < ximw; x++) {
          *odata++ = map[*ip++];
        }
        data += bpr;
      }
      CGPaletteRelease (pal);
    }
    break;

  default:
    abort();
    break;
  }
}


/* Loads an image into the Drawable, returning once the image is loaded.
 */
Bool
osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
                        XRectangle *geom_ret)
{
  Display *dpy = DisplayOfScreen (screen);
  NSView *nsview = jwxyz_window_view (xwindow);
  NSWindow *nswindow = [nsview window];
  XWindowAttributes xgwa;
  int window_x, window_y;
  Window unused;

  // Figure out where this window is on the screen.
  //
  XGetWindowAttributes (dpy, xwindow, &xgwa);
  XTranslateCoordinates (dpy, xwindow, RootWindowOfScreen (screen), 0, 0, 
                         &window_x, &window_y, &unused);

  // Use the size of the Drawable, not the Window.
  {
    Window r;
    int x, y;
    unsigned int w, h, bbw, d;
    XGetGeometry (dpy, drawable, &r, &x, &y, &w, &h, &bbw, &d);
    xgwa.width = w;
    xgwa.height = h;
  }

  // Create a tmp ximage to hold the screen data.
  //
  XImage *xim = XCreateImage (dpy, xgwa.visual, 32, ZPixmap, 0, 0,
                              xgwa.width, xgwa.height, 8, 0);
  xim->data = (char *) malloc (xim->height * xim->bytes_per_line);


  // Find the address in the frame buffer of the top left of this window.
  //
  CGDirectDisplayID cgdpy = 0;
  {
    CGPoint p;
    // #### this isn't quite right for screen 2: it's offset slightly.
    p.x = window_x;
    p.y = window_y;
    CGDisplayCount n;
    CGGetDisplaysWithPoint (p, 1, &cgdpy, &n);
    if (!cgdpy) abort();
  }

  // Paint a transparent "hole" in this window.
  //
  BOOL oopaque = [nswindow isOpaque];
  [nswindow setOpaque:NO];

  [[NSColor clearColor] set];
  NSRectFill ([nsview frame]);
  [[nswindow graphicsContext] flushGraphics];


  // Without this, we get a dozen black scanlines at the top.
  // #### But with this, the screen saver loops, because calling this
  //      seems to implicitly mark the display as non-idle!
  // CGDisplayCaptureWithOptions (cgdpy, kCGCaptureNoFill);

  // #### So let's try waiting for the vertical blank instead...
  //      Nope, that doesn't work.
  //
  // CGDisplayWaitForBeamPositionOutsideLines (cgdpy, 0,
  //   window_y + [nswindow frame].size.height);

  // #### Ok, try a busy-wait?
  //      Nope.
  //

  // #### Ok, just fuckin' sleep!
  //
  usleep (100000);


  // Pull the bits out of the frame buffer.
  //
  copy_framebuffer_to_ximage (cgdpy, xim, window_x, window_y);

  // CGDisplayRelease (cgdpy);

  // Make the window visible again.
  //
  [nswindow setOpaque:oopaque];

  // Splat the XImage onto the target drawable (probably the window)
  // and free the bits.
  //
  XGCValues gcv;
  GC gc = XCreateGC (dpy, drawable, 0, &gcv);
  XPutImage (dpy, drawable, gc, xim, 0, 0, 0, 0, xim->width, xim->height);
  XFreeGC (dpy, gc);

  if (geom_ret) {
    geom_ret->x = 0;
    geom_ret->y = 0;
    geom_ret->width  = xim->width;
    geom_ret->height = xim->height;
  }

  XDestroyImage (xim);
  return True;
}


#elif defined(USE_IPHONE)

	/* What a hack!

           On iOS, our application delegate, SaverRunner, grabs an image
           of itself as a UIImage before mapping the XScreenSaverView.
           In this code, we ask SaverRunner for that UIImage, then copy
           it to the root window.
         */

Bool
osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
                        XRectangle *geom_ret)
{
  SaverRunner *s = 
    (SaverRunner *) [[UIApplication sharedApplication] delegate];
  if (! s)
    return False;
  if (! [s isKindOfClass:[SaverRunner class]])
    return False;
  UIImage *img = [s screenshot];
  if (! img)
    return False;
  jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable,
                                 True, img, geom_ret, 0);
  return True;
}


#else /* MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5

         10.5+ code.

         This version of the code is simpler and more reliable, but
         uses an API that only exist on 10.5 and newer, so we can only
         use it if when being compiled against the 10.5 SDK or later.
       */

extern float jwxyz_scale (Window);  /* jwxyzI.h */

/* Loads an image into the Drawable, returning once the image is loaded.
 */
Bool
osx_grab_desktop_image (Screen *screen, Window xwindow, Drawable drawable,
                        XRectangle *geom_ret)
{
  Display *dpy = DisplayOfScreen (screen);
  NSView *nsview = jwxyz_window_view (xwindow);
  XWindowAttributes xgwa;
  int window_x, window_y;
  Window unused;

  // Figure out where this window is on the screen.
  //
  XGetWindowAttributes (dpy, xwindow, &xgwa);
  XTranslateCoordinates (dpy, xwindow, RootWindowOfScreen (screen), 0, 0, 
                         &window_x, &window_y, &unused);

  // Grab only the rectangle of the screen underlying this window.
  //
  CGRect cgrect;
  double s = jwxyz_scale (xwindow);
  cgrect.origin.x    = window_x;
  cgrect.origin.y    = window_y;
  cgrect.size.width  = xgwa.width  / s;
  cgrect.size.height = xgwa.height / s;

  /* If a password is required to unlock the screen, a large black
     window will be on top of all of the desktop windows by the time
     we reach here, making the screen-grab rather uninteresting.  If
     we move ourselves temporarily below the login-window windows
     before capturing the image, we capture the real desktop as
     intended.

     Oct 2016: Surprise, this trick no longer works on MacOS 10.12.  Sigh.
   */

  CGWindowID windowNumber = (CGWindowID) nsview.window.windowNumber;

  {
    CFArrayRef L = CGWindowListCopyWindowInfo (kCGWindowListOptionOnScreenOnly,
                                               kCGNullWindowID);

    CFIndex n = CFArrayGetCount(L);
    for (int i = 0; i < n; i++) {
      NSDictionary *dict = (NSDictionary *) CFArrayGetValueAtIndex(L, i);

      // loginwindow creates multiple toplevel windows. Grab the lowest one.
      if(![([dict objectForKey:(NSString *)kCGWindowOwnerName])
           compare:@"loginwindow"]) {
        windowNumber = ((NSNumber *)[dict objectForKey:
                                     (NSString *)kCGWindowNumber]).intValue;
      }
    }
    CFRelease (L);
  }

  // Grab a screen shot of those windows below this one
  // (hey, X11 can't do that!)
  //
  CGImageRef img = 
    CGWindowListCreateImage (cgrect,
                             kCGWindowListOptionOnScreenBelowWindow,
                             windowNumber,
                             kCGWindowImageDefault);

  if (! img) return False;

  // Render the grabbed CGImage into the Drawable.
  jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable, 
                                 False, img, geom_ret, 0);
  CGImageRelease (img);
  return True;
}

#endif /* 10.5+ code */


# ifndef USE_IPHONE

/* Returns the EXIF rotation property of the image, if any.
 */
static int
exif_rotation (const char *filename)
{
  /* As of 10.6, NSImage rotates according to EXIF by default:
     http://developer.apple.com/mac/library/releasenotes/cocoa/appkit.html
     So this function should return -1 when *running* on 10.6 systems.
     But when running against older systems, we need to examine the image
     to figure out its rotation.
   */

# if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6  /* 10.6 SDK */

  /* When we have compiled against the 10.6 SDK, we know that we are 
     running on a 10.6 or later system.
   */
  return -1;

# else /* Compiled against 10.5 SDK or earlier */

  /* If this selector exists, then we are running against a 10.6 runtime
     that does automatic EXIF rotation (despite the fact that we were
     compiled against the 10.5 or earlier SDK).  So in that case, this
     function should no-op.
   */
  if ([NSImage instancesRespondToSelector:
                 @selector(initWithDataIgnoringOrientation:)])
    return -1;

  /* Otherwise, go ahead and figure out what the rotational characteristics
     of this image are. */



  /* This is a ridiculous amount of rigamarole to go through, but for some
     reason the "Orientation" tag does not exist in the "NSImageEXIFData"
     dictionary inside the NSBitmapImageRep of the NSImage.  Several other
     EXIF tags are there (e.g., shutter speed) but not orientation.  WTF?
   */
  CFStringRef s = CFStringCreateWithCString (NULL, filename, 
                                             kCFStringEncodingUTF8);
  CFURLRef url = CFURLCreateWithFileSystemPath (NULL, s, 
                                                kCFURLPOSIXPathStyle, 0);
  CGImageSourceRef cgimg = CGImageSourceCreateWithURL (url, NULL);
  if (! cgimg) return -1;

  NSDictionary *props = (NSDictionary *)
    CGImageSourceCopyPropertiesAtIndex (cgimg, 0, NULL);
  int rot = [[props objectForKey:@"Orientation"] intValue];
  CFRelease (cgimg);
  CFRelease (url);
  CFRelease (s);
  return rot;

# endif /* 10.5 */
}

# endif /* USE_IPHONE */



/* Loads an image file and splats it onto the drawable.
   The image is drawn as large as possible while preserving its aspect ratio.
   If geom_ret is provided, the actual rectangle the rendered image takes
   up will be returned there.
 */
Bool
osx_load_image_file (Screen *screen, Window xwindow, Drawable drawable,
                     const char *filename, XRectangle *geom_ret)
{
# ifndef USE_IPHONE

  if (!filename || !*filename) return False;

  NSImage *img = [[NSImage alloc] initWithContentsOfFile:
                                    [NSString stringWithCString:filename
                                              encoding:NSUTF8StringEncoding]];
  if (!img)
    return False;

  jwxyz_draw_NSImage_or_CGImage (DisplayOfScreen (screen), drawable, 
                                 True, img, geom_ret,
                                 exif_rotation (filename));
  [img release];
  return True;

# else  /* USE_IPHONE */

  /* This is handled differently: see grabclient.c and grabclient-ios.m. */
  return False;

# endif /* USE_IPHONE */
}