Hello.

There is such task: I need to grab all mouse and keyboard events on some screen, do 
something after each event, and generate that events back to applications.

I try to do it like follows:

At first I grab mouse, remove first event from the queue, (here I must do smth.), 
ungrab the pointer (to simulate event), and simulate that event back to app. Than I 
try to grab mouse again, but XGrabPointer says me that it "already grabed".

This is the code:

#include <X11/X.h>
#include <X11/Xlib.h>
#include <iostream>
#include <unistd.h>
#include <X11/extensions/XTest.h>
#include <vector>

int Record()
{
  Display* tmp_pDisplay=XOpenDisplay(0);
  if (0 == tmp_pDisplay) return 0;

  Window root=RootWindow(tmp_pDisplay,0);

  if (GrabSuccess != 
XGrabPointer(tmp_pDisplay,root,false,ButtonPressMask|ButtonPressMask,
                                  GrabModeSync,GrabModeAsync,None,None,CurrentTime))
  {
    std::cout<<"couldn't grab mouse\n";
    exit(1);
  }

  XEvent Event;
  XButtonEvent EButton;

  static int count=0;

  while (count++ < 50)
  {
    XAllowEvents(tmp_pDisplay,SyncPointer,CurrentTime);
    XWindowEvent(tmp_pDisplay,root,ButtonPressMask|ButtonReleaseMask,&Event);

    XUngrabPointer(tmp_pDisplay,CurrentTime);

    switch (Event.type)
    {
    case ButtonPress:
      EButton=Event.xbutton;
      XTestFakeButtonEvent(tmp_pDisplay,EButton.button,true,CurrentTime);
      std::cout<<"ButtonPress\t"<<EButton.time<<'\n';
      break;

    case ButtonRelease:
      EButton=Event.xbutton;
      XTestFakeButtonEvent(tmp_pDisplay,EButton.button,false,CurrentTime);
      std::cout<<"ButtonRelease\t"<<EButton.time<<'\n';
      break;
    };

    XFlush(tmp_pDisplay);

    int Status1=XGrabPointer(tmp_pDisplay,root,false,ButtonPressMask|ButtonPressMask,
                             GrabModeSync,GrabModeAsync,None,None,CurrentTime);

    if (Status1==AlreadyGrabbed)
    {
      std::cout<<"Already grabbed\n";
      exit(1);
    }

    if (Status1!=GrabSuccess)
    {
      std::cout<<"can't grab mouse\n";
      exit(1);
    }
  }

  XUngrabPointer(tmp_pDisplay,CurrentTime);
  XCloseDisplay(tmp_pDisplay);
}

int main()
{
  Record();
  return 0;
}

_______________________________________________
Devel mailing list
[EMAIL PROTECTED]
http://XFree86.Org/mailman/listinfo/devel

Reply via email to