Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/3d4a4e8df625dbea8cc1 to your computer and use it in GitHub Desktop.
Save Noitidart/3d4a4e8df625dbea8cc1 to your computer and use it in GitHub Desktop.
jsc
var displays = ostypes.TYPE.CGDirectDisplayID.array(32)(); // i guess max possible monitors is 32
var count = ostypes.TYPE.uint32_t();
console.info('displays.constructor.size:', displays.constructor.size);
console.info('ostypes.TYPE.CGDirectDisplayID.size:', ostypes.TYPE.CGDirectDisplayID.size);
////////////////////////////////////////////////////////////////
var maxDisplays = displays.constructor.size / ostypes.TYPE.CGDirectDisplayID.size;
var activeDspys = displays; // displays.address() didnt work it threw `expected type pointer, got ctypes.uint32_t.array(32).ptr(ctypes.UInt64("0x11e978080"))` // the arg in declare is `self.TYPE.CGDirectDisplayID.ptr, // *activeDisplays` // without .address() worked
var dspyCnt = count.address();
console.info('maxDisplays:', maxDisplays);
var rez_CGGetActiveDisplayList = ostypes.API('CGGetActiveDisplayList')(maxDisplays, activeDspys, dspyCnt);
console.info('rez_CGGetActiveDisplayList:', rez_CGGetActiveDisplayList.toString(), uneval(rez_CGGetActiveDisplayList), cutils.jscGetDeepest(rez_CGGetActiveDisplayList));
if (!cutils.jscEqual(rez_CGGetActiveDisplayList, ostypes.CONST.kCGErrorSuccess)) {
console.error('Failed , errno:', ctypes.errno);
throw new Error({
name: 'os-api-error',
message: 'Failed , errno: "' + ctypes.errno + '" and : "' + rez_CGGetActiveDisplayList.toString(),
errno: ctypes.errno
});
}
count = parseInt(cutils.jscGetDeepest(count));
console.info('count:', count);
////////////////////////////////////////////////////////////////
var i_nonMirror = {};
var rect = ostypes.CONST.CGRectNull;
console.info('rect preloop:', rect.toString()); // "CGRect({"x": Infinity, "y": Infinity}, {"width": 0, "height": 0})"
for (var i=0; i<count; i++) {
// if display is secondary mirror of another display, skip it
console.info('displays[i]:', displays[i]);
var rez_CGDisplayMirrorsDisplay = ostypes.API('CGDisplayMirrorsDisplay')(displays[i]);
console.info('rez_CGDisplayMirrorsDisplay:', rez_CGDisplayMirrorsDisplay.toString(), uneval(rez_CGDisplayMirrorsDisplay), cutils.jscGetDeepest(rez_CGDisplayMirrorsDisplay));
if (!cutils.jscEqual(rez_CGDisplayMirrorsDisplay, ostypes.CONST.kCGNullDirectDisplay)) { // If CGDisplayMirrorsDisplay() returns 0 (a.k.a. kCGNullDirectDisplay), then that means the display is not mirrored.
continue;
}
////////////////////////////////////////////////////////////////
i_nonMirror[i] = null;
var rez_CGDisplayBounds = ostypes.API('CGDisplayBounds')(displays[i]);
console.info('rez_CGDisplayBounds:', rez_CGDisplayBounds.toString(), uneval(rez_CGDisplayBounds)/*, cutils.jscGetDeepest(rez_CGDisplayBounds)*/); // :todo: fix cutils.jscEqual because its throwing `Error: cannot convert to primitive value` for ctypes.float64_t and ctypes.double ACTUALLY its a struct, so no duhhh so no :todo:
rect = ostypes.API('CGRectUnion')(rect, rez_CGDisplayBounds);
console.info('rect post loop ' + i + ':', rect.toString());
////////////////////////////////////////////////////////////////
}
if (Object.keys(i_nonMirror).length == 0) {
// what on earth, no monitors that arent mirrors?
return []; // as there is nothing to screenshot
}
var myNSStrings;
var allocNSBIP;
try {
myNSStrings = new ostypes.HELPER.nsstringColl();
////////////////////////////////////////////////////////////////
var rez_width = ostypes.API('CGRectGetWidth')(rect);
console.info('rez_width:', rez_width.toString(), uneval(rez_width), cutils.jscGetDeepest(rez_width));
var rez_height = ostypes.API('CGRectGetHeight')(rect);
console.info('rez_height:', rez_height.toString(), uneval(rez_height), cutils.jscGetDeepest(rez_height));
var NSBitmapImageRep = ostypes.HELPER.class('NSBitmapImageRep');
allocNSBIP = ostypes.API('objc_msgSend')(NSBitmapImageRep, ostypes.HELPER.sel('alloc'));
console.info('allocNSBIP:', allocNSBIP.toString(), uneval(allocNSBIP));
var imageRep = ostypes.API('objc_msgSend')(allocNSBIP, ostypes.HELPER.sel('initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bitmapFormat:bytesPerRow:bitsPerPixel:'), // https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/index.html#//apple_ref/occ/instm/NSBitmapImageRep/initWithBitmapDataPlanes:pixelsWide:pixelsHigh:bitsPerSample:samplesPerPixel:hasAlpha:isPlanar:colorSpaceName:bitmapFormat:bytesPerRow:bitsPerPixel:
ostypes.TYPE.unsigned_char.ptr.ptr(null), // planes
ostypes.TYPE.NSInteger(rez_width), // pixelsWide
ostypes.TYPE.NSInteger(rez_height), // pixelsHigh
ostypes.TYPE.NSInteger(8), // bitsPerSample
ostypes.TYPE.NSInteger(4), // samplesPerPixel
ostypes.CONST.YES, // hasAlpha
ostypes.CONST.NO, // isPlanar
myNSStrings.get('NSCalibratedRGBColorSpace'), // colorSpaceName
ostypes.TYPE.NSBitmapFormat(0), // bitmapFormat
ostypes.TYPE.NSInteger(0), // bytesPerRow
ostypes.TYPE.NSInteger(32) // bitsPerPixel
);
console.info('imageRep:', imageRep.toString(), uneval(imageRep), cutils.jscGetDeepest(imageRep));
if (imageRep.isNull()) { // im guessing this is how to error check it
throw new Error({
name: 'os-api-error',
message: 'Failed imageRep, errno: "' + ctypes.errno + '" and : "' + imageRep.toString(),
errno: ctypes.errno
});
}
////////////////////////////////////////////////////////////////
// NSGraphicsContext* context = [NSGraphicsContext graphicsContextWithBitmapImageRep:imageRep];
var NSGraphicsContext = ostypes.HELPER.class('NSGraphicsContext');
var context = ostypes.API('objc_msgSend')(NSGraphicsContext, ostypes.HELPER.sel('graphicsContextWithBitmapImageRep:'), imageRep);
console.info('context:', context.toString(), uneval(context), cutils.jscGetDeepest(context));
if (context.isNull()) { // im guessing this is how to error check it
throw new Error({
name: 'os-api-error',
message: 'Failed context, errno: "' + ctypes.errno + '" and : "' + context.toString(),
errno: ctypes.errno
});
}
////////////////////////////////////////////////////////////////
// [NSGraphicsContext saveGraphicsState];
var rez_saveGraphicsState = ostypes.API('objc_msgSend')(NSGraphicsContext, ostypes.HELPER.sel('saveGraphicsState'));
console.info('rez_saveGraphicsState:', rez_saveGraphicsState.toString(), uneval(rez_saveGraphicsState), cutils.jscGetDeepest(rez_saveGraphicsState));
////////////////////////////////////////////////////////////////
// [NSGraphicsContext setCurrentContext:context];
var rez_setCurrentContext = ostypes.API('objc_msgSend')(NSGraphicsContext, ostypes.HELPER.sel('setCurrentContext:'), context);
console.info('rez_setCurrentContext:', rez_setCurrentContext.toString(), uneval(rez_setCurrentContext), cutils.jscGetDeepest(rez_setCurrentContext));
////////////////////////////////////////////////////////////////
// CGContextRef cgcontext = [context graphicsPort];
var cgcontext = ostypes.API('objc_msgSend')(context, ostypes.HELPER.sel('graphicsPort'));
console.info('cgcontext:', cgcontext.toString(), uneval(cgcontext), cutils.jscGetDeepest(cgcontext));
////////////////////////////////////////////////////////////////
// CGContextClearRect(cgcontext, CGRectMake(0, 0, CGRectGetWidth(rect), CGRectGetHeight(rect)));
var rez_width2 = ostypes.API('CGRectGetWidth')(rect);
console.info('rez_width2:', rez_width2.toString(), uneval(rez_width2), cutils.jscGetDeepest(rez_width2));
var rez_height2 = ostypes.API('CGRectGetHeight')(rect);
console.info('rez_height2:', rez_height2.toString(), uneval(rez_height2), cutils.jscGetDeepest(rez_height2));
var rez_CGRectMake = ostypes.API('CGRectMake')(0, 0, rez_width2, rez_height2);
console.info('rez_CGRectMake:', rez_CGRectMake.toString(), uneval(rez_CGRectMake)/*, cutils.jscGetDeepest(rez_CGRectMake)*/);
var casted_cgcontext = ctypes.cast(cgcontext, ostypes.TYPE.CGContextRef);
var rez_CGContextClearRect = ostypes.API('CGContextClearRect')(casted_cgcontext, rez_CGRectMake); // returns void
//console.info('rez_CGContextClearRect:', rez_CGContextClearRect.toString(), uneval(rez_CGContextClearRect), cutils.jscGetDeepest(rez_CGContextClearRect));
console.log('did CGContextClearRect');
////////////////////////////////////////////////////////////////
for (var i in i_nonMirror) { // if display is secondary mirror of another display, skip it
console.log('entering nonMirror');
////////////////////////////////////////////////////////////////
// CGRect displayRect = CGDisplayBounds(displays[i]);
var displayRect = ostypes.API('CGDisplayBounds')(displays[i]);
console.info('displayRect:', displayRect.toString(), uneval(displayRect));
////////////////////////////////////////////////////////////////
console.warn('pre CGDisplayCreateImage');
// CGImageRef image = CGDisplayCreateImage(displays[i]);
var image = ostypes.API('CGDisplayCreateImage')(displays[i]);
console.info('image:', image.toString(), uneval(image));
if (image.isNull()) {
console.warn('no image so continuing');
continue;
}
////////////////////////////////////////////////////////////////
// CGRect dest = CGRectMake(displayRect.origin.x - rect.origin.x,
// displayRect.origin.y - rect.origin.y,
// displayRect.size.width,
// displayRect.size.height);
var dest = ostypes.API('CGRectMake')(
displayRect.origin.x - rect.origin.x,
displayRect.origin.y - rect.origin.y,
displayRect.size.width,
displayRect.size.height
);
console.info('dest:', dest.toString(), uneval(dest));
////////////////////////////////////////////////////////////////
// CGContextDrawImage(cgcontext, dest, image);
ostypes.API('CGContextDrawImage')(casted_cgcontext, dest, image); // reutrns void
console.info('did CGContextDrawImage');
////////////////////////////////////////////////////////////////
// CGImageRelease(image);
ostypes.API('CGImageRelease')(image); // returns void
console.info('did CGImageRelease');
////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////
// [[NSGraphicsContext currentContext] flushGraphics];
var rez_currentContext = ostypes.API('objc_msgSend')(NSGraphicsContext, ostypes.HELPER.sel('currentContext'));
console.info('rez_currentContext:', rez_currentContext.toString(), uneval(rez_currentContext));
var rez_flushGraphics = ostypes.API('objc_msgSend')(rez_currentContext, ostypes.HELPER.sel('flushGraphics'));
console.info('rez_flushGraphics:', rez_flushGraphics.toString(), uneval(rez_flushGraphics));
////////////////////////////////////////////////////////////////
// [NSGraphicsContext restoreGraphicsState];
var rez_restoreGraphicsState = ostypes.API('objc_msgSend')(NSGraphicsContext, ostypes.HELPER.sel('restoreGraphicsState'));
console.info('rez_restoreGraphicsState:', rez_restoreGraphicsState.toString(), uneval(rez_restoreGraphicsState));
////////////////////////////////////////////////////////////////
// NSData* data = [imageRep representationUsingType:NSPNGFileType properties:@{ }];
var NSDictionary = ostypes.HELPER.class('NSDictionary');
var tempDict = ostypes.API('objc_msgSend')(NSDictionary, ostypes.HELPER.sel('dictionary')); //gives us temporary dicationary, one that gets auto released? well whatever its something not allocated so we dont have to release it
console.info('tempDict:', tempDict.toString(), uneval(tempDict));
var data = ostypes.API('objc_msgSend')(imageRep, ostypes.HELPER.sel('representationUsingType:properties:'), ostypes.CONST.NSPNGFileType, tempDict); // https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/index.html#//apple_ref/occ/instm/NSBitmapImageRep/representationUsingType:properties:
console.info('data:', data.toString());
////////////////////////////////////////////////////////////////
// [data writeToFile:@"/Users/noit/Desktop/full_ss.png" atomically:YES];
console.info(OS.Path.join(OS.Constants.Path.desktopDir, 'full_ss.png'));
console.info(myNSStrings.get(OS.Path.join(OS.Constants.Path.desktopDir, 'full_ss.png')).toString());
var rez_writeToFile = ostypes.API('objc_msgSend')(data, ostypes.HELPER.sel('writeTofile:atomically:'), myNSStrings.get(OS.Path.join(OS.Constants.Path.desktopDir, 'full_ss.png')), ostypes.CONST.YES);
//console.info('rez_writeToFile:', rez_writeToFile.toString(), uneval(rez_writeToFile));
////////////////////////////////////////////////////////////////
//****** this here below is me releasing stuff
} finally {
console.error('starting finally block');
if (allocNSBIP) {
var rez_relNSBPI = ostypes.API('objc_msgSend')(allocNSBIP, ostypes.HELPER.sel('release'));
console.info('rez_relNSBPI:', rez_relNSBPI.toString());
}
if (myNSStrings) {
myNSStrings.releaseAll()
}
console.info('released things, i want to know if it gets here even if return was called within the try block');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment