1

我正在为 Firefox(SDK 插件)创建一个扩展,我需要在其中检测屏幕保护程序和锁屏事件,以便我可以在 Web 应用程序中设置用户的可用性状态。

我已经设法为 Windows 做到这一点,现在需要移植到 OS X。对于 Windows 版本,我使用对本机 API 的调用来确定屏幕是否被锁定等。是否有类似的方法来获取操作系统信息来自 OS X 上的 Firefox 扩展?我试过谷歌搜索,但没有找到可靠的答案 - 任何帮助表示赞赏!

4

1 回答 1

2

在 OSX 上,您可以使用并查找密钥CGSessionCopyCurrentDictionary的存在和值来查询锁定的屏幕/屏幕保护程序。"CGSSessionScreenIsLocked"

这是平台 API,因此必须再次使用 js-ctypes 并编写一堆代码才能使其正常工作。

我确实让它工作了:以下代码是一个工作示例,您可以在特权Scratchpad中运行。要获得特权,请打开一个例如about:newtab.

Components.utils.import("resource://gre/modules/ctypes.jsm");

var CoreFoundation = new (function() {
    this.CFNumberRef = ctypes.voidptr_t;
    this.CFStringRef = ctypes.voidptr_t;
    this.CFDictionaryRef = ctypes.voidptr_t;

    var lib = ctypes.open("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation");
    this.CFRelease = lib.declare(
        "CFRelease",
        ctypes.default_abi,
        ctypes.void_t,
        ctypes.voidptr_t);

    var CFStringCreateWithCharacters = lib.declare(
        "CFStringCreateWithCharacters",
        ctypes.default_abi,
        this.CFStringRef,
        ctypes.voidptr_t,
        ctypes.jschar.ptr,
        ctypes.int32_t);
    this.CFStringCreateWithCharacters = function(str) {
        var rv = CFStringCreateWithCharacters(null, str, str.length);
        if (!rv || rv.isNull()) {
            return null;
        }
        return ctypes.CDataFinalizer(rv, this.CFRelease);
    };


    var CFDictionaryGetValue = lib.declare(
        "CFDictionaryGetValue",
        ctypes.default_abi,
        this.CFNumberRef,
        this.CFDictionaryRef,
        this.CFStringRef);
    this.CFDictionaryGetInt = function(dict, str) {
        var rv = CFDictionaryGetValue(dict, this.CFStringCreateWithCharacters(str));
        if (!rv || rv.isNull()) {
            return null;
        };
        return this.CFNumberGetValue(rv);
    };

    var CFNumberGetValue = lib.declare(
        "CFNumberGetValue",
        ctypes.default_abi,
        ctypes.bool,
        this.CFNumberRef,
        ctypes.int32_t,
        ctypes.int32_t.ptr);
    this.CFNumberGetValue = function(num) {
        var rv = new ctypes.int32_t();
        CFNumberGetValue(num, 3, rv.address());
        console.log("CFNumberGetValue", rv, rv.value);
        return rv.value;
    };
    this.close = function() {
        lib.close();
    };
})();
var ApplicationServices = new (function() {
    var lib = ctypes.open("/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices");

    var CGSessionCopyCurrentDictionary = lib.declare(
        "CGSessionCopyCurrentDictionary",
        ctypes.default_abi,
        CoreFoundation.CFDictionaryRef);
    this.CGSessionCopyCurrentDictionary = function() {
        var rv = CGSessionCopyCurrentDictionary();
        if (!rv || rv.isNull()) {
            return null;
        }
        return ctypes.CDataFinalizer(rv, CoreFoundation.CFRelease);
    };

    this.close = function() {
        lib.close();
    };
})();

setInterval(function() {
    var dict = ApplicationServices.CGSessionCopyCurrentDictionary();
    if (dict) {
        var locked = CoreFoundation.CFDictionaryGetInt(dict, "CGSSessionScreenIsLocked");
        console.log("rv", locked);
        if (locked) {
            // do something;
        }
    }
}, 500);
于 2013-11-05T04:18:28.403 回答