XGrabPointer失败了代码AlreadyGrabbed,XUngrabPointer不会ungrab

时间:2015-10-24 11:34:22

标签: gtk x11 gdk

我正在尝试获取XEvents,然后决定它是否应该像正常一样继续或被阻止。

基本上gdk_window_add_filter正在做什么:https://developer.gnome.org/gdk3/stable/gdk3-Windows.html#gdk-window-add-filter

我尝试使用下面的代码,但遇到了这些问题:

  • 如果在启动后完成XGrabPointer,则无法正常工作,但如果我在启动时执行它,则可以正常工作。
  • 如果我让XGrabPointer工作,通过与启动一起执行,XUngrabPointer不起作用,它总是返回1.
  • 如果我让XGrabPointer工作,通过与启动一起完成,XAllowEvents不会让事件通过。

这是我在ctypes中的完整代码:

var win = ostypes.TYPE.Window();
var revert_to = ostypes.TYPE.int();
var rez_XGetFocus = ostypes.API('XGetInputFocus')(ostypes.HELPER.cachedXOpenDisplay(), win.address(), revert_to.address());
console.log('rez_XGetFocus:', rez_XGetFocus);

var rez_XSelectInput = ostypes.API('XSelectInput')(ostypes.HELPER.cachedXOpenDisplay(), win, ostypes.CONST.ButtonPressMask | ostypes.CONST.ButtonReleaseMask);
console.log('rez_XSelectInput:', rez_XSelectInput);

var rez_XGrab = ostypes.API('XGrabPointer')(ostypes.HELPER.cachedXOpenDisplay(), win, true, ostypes.CONST.ButtonPressMask | ostypes.CONST.ButtonReleaseMask, ostypes.CONST.GrabModeSync, ostypes.CONST.GrabModeAsync, ostypes.CONST.None, ostypes.CONST.None, ostypes.CONST.CurrentTime);
console.log('rez_XGrab:', rez_XGrab);
var rez_XGrab = 0;
if (!cutils.jscEqual(rez_XGrab, ostypes.CONST.GrabSuccess)) {
    if (cutils.jscEqual(rez_XGrab, ostypes.CONST.AlreadyGrabbed)) {
        console.log('already grabbed so will ungrab then regrab');

        var rez_XUngrab = ostypes.API('XUngrabPointer')(ostypes.HELPER.cachedXOpenDisplay(), ostypes.CONST.CurrentTime);
        console.log('rez_XUngrab:', rez_XUngrab);

        var rez_XFlush = ostypes.API('XFlush')(ostypes.HELPER.cachedXOpenDisplay());
        console.log('rez_XFlush:', rez_XFlush);

        var rez_XGrab2 = ostypes.API('XGrabPointer')(ostypes.HELPER.cachedXOpenDisplay(), win, false, ostypes.CONST.ButtonPressMask | ostypes.CONST.ButtonReleaseMask, ostypes.CONST.GrabModeSync, ostypes.CONST.GrabModeAsync, ostypes.CONST.None, ostypes.CONST.None, ostypes.CONST.CurrentTime);
        console.log('rez_XGrab2:', rez_XGrab2);

        if (!cutils.jscEqual(rez_XGrab2, ostypes.CONST.GrabSuccess)) {
            console.error('failed to XGrabPointer a SECOND time with value:', rez_XGrab);
            throw new Error('failed to XGrabPointer a SECOND time with value: ' + rez_XGrab);
        }
    } else {
        console.error('failed to XGrabPointer with value:', rez_XGrab);
        throw new Error('failed to XGrabPointer with value: ' + rez_XGrab);
    }
}
// 
// // var rez_XChangeGrab = ostypes.API('XChangeActivePointerGrab')(ostypes.HELPER.cachedXOpenDisplay(), ostypes.HELPER.cachedDefaultRootWindow(), ostypes.CONST.ButtonPressMask | ostypes.CONST.ButtonReleaseMask, ostypes.CONST.CurrentTime);
// // console.log('rez_XChangeGrab:', rez_XChangeGrab);
// 
// // throw new Error('ok?');
// // var rez_XSelectInput = ostypes.API('XSelectInput')(ostypes.HELPER.cachedXOpenDisplay(), ostypes.HELPER.cachedDefaultRootWindow(), ostypes.CONST.ButtonPressMask | ostypes.CONST.ButtonReleaseMask);
// // console.log('rez_XSelectInput:', rez_XSelectInput);
// 
var ev = ostypes.TYPE.XEvent();

var st = new Date().getTime();
var runFor = 10000; // ms
while (true) {

    var rez_XAllow = ostypes.API('XAllowEvents')(ostypes.HELPER.cachedXOpenDisplay(), ostypes.CONST.SyncPointer, ostypes.CONST.CurrentTime);
    console.log('rez_XAllow:', rez_XAllow);

    var rez_XNextEvent = ostypes.API('XNextEvent')(ostypes.HELPER.cachedXOpenDisplay(), ev.address());
    console.log('rez_XNextEvent:', rez_XNextEvent);
    console.info('ev:', ev.xbutton);

    // :debug:
    if (new Date().getTime() - st > runFor) {
        console.log('time up');
        break;
    }
}


var rez_XUngrab2 = ostypes.API('XUngrabPointer')(ostypes.HELPER.cachedXOpenDisplay(), ostypes.CONST.CurrentTime);
console.log('rez_XUngrab2:', rez_XUngrab2);

0 个答案:

没有答案