2012-06-07 4 views
0

현재 Linux의 모든 기존 창에서 마우스 포인터 움직임을 모니터링해야하는 응용 프로그램을 작성하고 있습니다. 나는 gnome으로 우분투 11.10을 사용하고 있습니다. 필요한 것은 화면상의 마우스 포인터 이동에 대한 정보를 얻는 것입니다. 나는 모든 윈도우의 제목 표시 줄에 마우스 포인터의 움직임을 캡처 할 수 있어요하지만 마우스 포인터가있을 때 움직임을 포착 할 수없는 나는 위의 코드와Xlib을 사용하여 Linux의 기존 창에서 마우스 포인터 움직임을 모니터링하는 방법

void *MonitorOffline(void *threaddata) 
{ 
    time_t  sTime, cTime; 
    DISPLAY  *dsp = NULL; 
    int   iError = 0; 

    sTime = time(NULL); 

    XSetErrorHandler(_invalid_window_handler); 
    while (1) { 
     XEvent event; 
     cTime = time(NULL); 
     if ((cTime - sTime) > OFFLINETIME) { 
      log_msg("User %s is offline", cuserid(NULL)); 
      sTime = cTime; 
     } 
     iError = RegisterWinEvents(&dsp); 
     if (iError) { 
      log_quit("%s:%d : Error in RegisterWinEvents", __FUNCTION__, 
        __LINE__); 
      break; 
     } 
     XNextEvent(dsp, &event); 
     switch(event.type) { 
      case KeyPress: 
       printf("KeyPress Encountered\n"); 
       break; 
       printf("KeyRelease Encountered\n"); 

       break; 

      case ButtonPress: 

       printf("ButtonPress Encountered\n"); 

       break; 

      case ButtonRelease: 

       printf("ButtonRelease Encountered\n"); 

       break; 

      case MotionNotify: 

       printf("MotionNotify Encountered\n"); 

       break; 

      case EnterNotify: 

       printf("EnterNotify Encountered\n"); 

       break; 
      case LeaveNotify: 

       printf("LeaveNotify Encountered\n"); 

       break; 

     } 
     XCloseDisplay (dsp); 
     fflush(stdout); 
    } 
} 

int RegisterWinEvents(DISPLAY **dsp) 
{ 
    Window window_id; 
    char *win_name; 
    int iError = 0; 
    XSetWindowAttributes attr; 
    XWindowAttributes wattr; 
    Window root_return, parent_return; 
    Window root; 
    Window client; 
    Window *children_return = NULL; 
    unsigned int num_children = 0; 
    Status status; 
    int i; 
    time_t t; 

    iError = WDGetRoot(&root, dsp); 

    if (iError == -1) { 

     return -1; 
    } 
    status = XQueryTree (*dsp, root, &root_return, 
      &parent_return, &children_return, 
      &num_children); 
    for(i = 0; i < num_children; i++) 
    { 
     attr.event_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | 
      ButtonReleaseMask | EnterWindowMask | 
      LeaveWindowMask | PointerMotionMask | 
      Button1MotionMask | 
      Button2MotionMask | Button3MotionMask | 
      Button4MotionMask | Button5MotionMask | 
      ButtonMotionMask | KeymapStateMask | 
      ExposureMask | VisibilityChangeMask | 
      StructureNotifyMask | /* ResizeRedirectMask | */ 
      SubstructureNotifyMask | SubstructureRedirectMask | 
      FocusChangeMask | PropertyChangeMask | 
      ColormapChangeMask;// | OwnerGrabButtonMask; 

     status = XGetWindowAttributes(*dsp, children_return[i], &wattr); 
     if (wattr.all_event_masks & ButtonPressMask) 
      attr.event_mask &= ~ButtonPressMask; 
     attr.event_mask &= ~SubstructureRedirectMask; 
     XSelectInput(*dsp, children_return[i], attr.event_mask); 
    } 
    XFree(children_return); 
    return 0; 
} 

WINDOW WDGetRootWindow(DISPLAY* pDisplay, INT iScreen) 
{ 
    return RootWindow(pDisplay,iScreen); 
} 

int WDGetRoot(Window *root, DISPLAY **pDisplay) 
{ 
    INT iScreen = 0; 
    setlocale(LC_CTYPE, ""); 

    //Initialize the Display 
    if((*pDisplay = WDOpenDisplay(NULL))) { 
     //Get the sceen associated with Display 
     iScreen = WDGetScreenOfDisplay(*pDisplay); 
     //Once we have the screen , we need to get the rootwindow associated 
     //with the screen so that we can traverse the window tree and get the 
     //window with current focus (the active window) 
     *root = WDGetRootWindow(*pDisplay, iScreen); 
    } else { 
     return -1; 
    } 
    return 0; 
} 

: 여기 는 기존과 동일한 작업을 수행하는 코드입니다 창 안쪽 (예 : 사무실 작가 등의 텍스트 부분). 전체 윈도우에서 마우스 움직임을 캡쳐하기 위해 코드에 추가해야 할 것은 무엇입니까?

답변

0

XQueiryPointer은 창에서 좌표를 반환 할 수 있다고 주장합니다.

XQueryPointer 함수는 ... 포인터가 루트 창의 원점을 기준으로 좌표를 반환합니다. help.XQueiryPointer 내 문제를 해결하는 데 도움이 되었습니까 & I는 현재 창에서 마우스 움직임을 캡처 할 수 있어요 위해 전화와 결합

봅니다

+0

덕분에 많이 XQueryTree합니다. 다시 한번 감사드립니다. – user1439609

+0

또 하나 더하고자하는 것은 포인터 이동이 사용자에 의해 또는 자동화 된 스크립트에 의해 시작되는지 여부를 파악하는 것입니다. 우리가 똑같이 달성 할 수있는 방법이 있습니까? – user1439609

관련 문제