Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Noitidart/ff19ae88500a649c1ef9 to your computer and use it in GitHub Desktop.
Save Noitidart/ff19ae88500a649c1ef9 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-CF_QueryWorkspaceLockedInterval - Every one second it checks if workstaiton is locked. It looks like as soon as screensaver turns on, even if the require password delay is not "immedietly" the CGSSessionScreenIsLocked property is 1/true.(Mac OS X) (jsctypes)
if (typeof myint == 'undefined') {
var myint = null;
}
function doit(){
if (myint === null) {
myint = setInterval(function() {
Cu.import('resource://gre/modules/ctypes.jsm');
var lib = {
CoreGraphics: '/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics',
CoreFoundation: '/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation'
};
for (var l in lib) {
lib[l] = ctypes.open(lib[l]);
}
//start mactypes
var Boolean = ctypes.unsigned_char;
var UniChar = ctypes.jschar; // uint16 with automatic conversion
//end mactypes
//start CoreFoundationTypes
var __CFBoolean = new ctypes.StructType('__CFBoolean');
var CFBooleanRef = __CFBoolean.ptr;
var CFTypeRef = ctypes.void_t.ptr;
var __CFString = new ctypes.StructType('__CFString');
var CFStringRef = __CFString.ptr;
var CFIndex = ctypes.long;
var __CFAllocator = new ctypes.StructType('__CFAllocator');
var CFAllocatorRef = __CFAllocator.ptr;
//end CoreFoundationTypes
//dictionary functionality
var __CFDictionary = new ctypes.StructType('__CFDictionary');
var CFDictionaryRef = __CFDictionary.ptr;
var CFDictionaryGetValue = lib.CoreFoundation.declare('CFDictionaryGetValue', ctypes.default_abi, ctypes.void_t.ptr/*returns CFTypeRef*/, CFDictionaryRef, ctypes.void_t.ptr/*CFStringRef*/);
//end dictionary functionality
//string functionality
var CFStringCreateWithCharacters = lib.CoreFoundation.declare('CFStringCreateWithCharacters', ctypes.default_abi, CFStringRef, CFAllocatorRef, UniChar.ptr, CFIndex);
//end string functionality
var CFBooleanGetValue = lib.CoreFoundation.declare('CFBooleanGetValue', ctypes.default_abi, Boolean, CFBooleanRef);
//common declares
var CFRelease = lib.CoreFoundation.declare('CFRelease', ctypes.default_abi, ctypes.void_t, CFTypeRef);
function makeCFStr(input) { //input is just a js string so like `var input = 'blah';`
return CFStringCreateWithCharacters(null, input, input.length);
}
//end common declares
var CGSessionCopyCurrentDictionary = lib.CoreGraphics.declare('CGSessionCopyCurrentDictionary', ctypes.default_abi, CFDictionaryRef);
var CGSessionDict = CGSessionCopyCurrentDictionary();
//console.log(uneval(CGSessionDict));
if (CGSessionDict.isNull()) {
console.error('CGSessionDict is null');
} else {
var kCGSSessionOnConsoleKey_str = 'CGSSessionScreenIsLocked';
var kCGSSessionOnConsoleKey = CFStringCreateWithCharacters(null, kCGSSessionOnConsoleKey_str, kCGSSessionOnConsoleKey_str.length); //works // i figured it should be a string because of: https://github.com/JuliaEichler/Mac_OSX_SDKs/blob/392649d7112884481a94b8cd1f601f3a5edae999/MacOSX10.6.sdk/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/CoreGraphics.framework/Versions/A/Headers/CGSession.h#L38 here they are making CFSTR of a text string
var kCGSSessionOnConsoleKey_val = CFDictionaryGetValue(CGSessionDict, kCGSSessionOnConsoleKey);
//console.log('kCGSSessionOnConsoleKey_val:', kCGSSessionOnConsoleKey_val, uneval(kCGSSessionOnConsoleKey_val)); //printing `"kCGSSessionOnConsoleKey_val:" CData { } "ctypes.voidptr_t(ctypes.UInt64("0x7fff7a13b7f0"))"`
if (kCGSSessionOnConsoleKey_val.isNull()) {
console.log('CFDictionaryGetValue isNull so the key is not present in the dictionary, I am guesing');
} else {
var kCGSSessionOnConsoleKey_val_casted = ctypes.cast(kCGSSessionOnConsoleKey_val, CFBooleanRef); //we know its a boolean so cast it
//console.log('kCGSSessionOnConsoleKey_val_casted:', uneval(kCGSSessionOnConsoleKey_val_casted));
var kCGSSessionOnConsoleKey_val_casted_valInDict = CFBooleanGetValue(kCGSSessionOnConsoleKey_val_casted);
console.log('kCGSSessionOnConsoleKey_val_casted_valInDict:', kCGSSessionOnConsoleKey_val_casted_valInDict);
}
CFRelease(kCGSSessionOnConsoleKey); //do release on things made with CFStringCreateWithCharacters per https://github.com/philikon/osxtypes/blob/master/examples/content/iphoto.js#L89
CFRelease(CGSessionDict); //apple docs say to release dictionary's
}
for (var l in lib) {
lib[l].close();
}
}, 1000);
} else {
clearInterval(myint);
myint = null;
}
}
doit();
@Noitidart
Copy link
Author

README

Rev1

  • Works
  • Research running this shows, that:
    • If lock is enabled, no matter what the delay is for requiring the password, as soon as screen saver shows, the value of CGSSessionScreenIsLocked is 1 (true)
    • If lock is NOT enabled, even if screen saver is on, it will not be in the dictionary so CFDictionaryGetValue returns null

Rev2

  • CFReleaseing the dictionarying, this is proper cleanup, otherwise leak i guess
  • Also error check if dictionary is null

Rev3

  • Made it so on every other run of scratchpad it toggles interval off, and on every other run it toggles it on.
  • Made it not throw if it finds that dict is null, so it can do the library closing
  • Removed the unused ctypes defintions/declares

Rev4

  • On first run, Rev3 wouldn't run because doing myint === undefined was not working, had to change that to typeof myint == 'undefined'
  • Now in working order for togalability, the ctypes was working from Rev1

Rev5

  • Added CF_ to name of file and gist meaning its CoreFoundation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment