未触发 GLFW 监视器回调

GLFW Monitor Callback not Triggered

我正在为 Node.js 开发一组 GLFW 3.1.2 绑定。我 运行 遇到了一个问题 API,特别是 glfwSetMonitorCallback API。

问题是当我断开或连接显示器到我的系统时它没有被触发。

以下是我的回调处理程序:

void monitor::onGLFWMonitor (GLFWmonitor* monitor, int action) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    auto it = monitor::monitorObjects.find(monitor);
    Local<Object> jsMonitor;

    if (it == monitor::monitorObjects.end()) {
        // New monitor, add it
        jsMonitor = monitor::monitorObjectTemplate.Get(isolate)->NewInstance();
        jsMonitor->SetAlignedPointerInInternalField(0, monitor);
        jsMonitor->SetInternalField(1, String::NewFromUtf8(isolate, "GLFWmonitor"));
        CopyablePersistent<Object> perst(isolate, jsMonitor);
        MonitorIndexedPair<Object> pr(monitor, perst);
        it = monitor::monitorObjects.insert(pr).first;
    } else {
        jsMonitor = it->second.Get(isolate);
    }

    if (action == GLFW_DISCONNECTED) {
        // Monitor removed, clean up!
        jsMonitor->SetAlignedPointerInInternalField(0, NULL);
        monitor::monitorObjects.erase(it);
        // TODO Cleanup video modes once implemented
    }

    if (monitor::monitorCallback.IsEmpty()) {
        // No callback, do nothing
        return;
    }

    Local<Function> callback = monitor::monitorCallback.Get(isolate);
    const unsigned int argc = 2;
    Local<Value> argv[argc] = {
        jsMonitor,
        Number::New(isolate, action)
    };
    callback->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}

它被设置为对 glfwInit 的任何调用的一部分,如下所示:

void core::init (const FunctionCallbackInfo<Value>& args) {
    Isolate* isolate = Isolate::GetCurrent();
    HandleScope scope(isolate);

    bool result = glfwInit();

    if (result == GL_TRUE) {
        // Setup callbacks
        glfwSetMonitorCallback(monitor::onGLFWMonitor);
    }

    args.GetReturnValue().Set(Boolean::New(isolate, result == GL_TRUE));
}

我在回调函数中添加了几个 printf 语句,并且曾经生成过 none 个语句。此外,如果我调用 glfwGetMonitors,它总是 returns 错误的显示器数量。 EG:我有两台显示器,断开一台,glfwGetMonitors 仍然 returns 两台显示器。

我在多台计算机 运行ning Windows 7、Windows 8/8.1 和 Windows 10 上看到此问题。如有任何建议,我们将不胜感激。

经过更多的挖掘,我确定了我的问题的原因。我编写的简单测试用例从未调用过 glfwPollEvents,因此我的回调自然会被触发 none。