File: x_util.cpp

package info (click to toggle)
boinc 7.14.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,132 kB
  • sloc: cpp: 163,589; php: 113,173; ansic: 49,284; pascal: 35,620; xml: 17,864; java: 13,521; python: 6,551; sh: 4,082; perl: 1,843; makefile: 1,796; objc: 1,543; sql: 959; csh: 126; lisp: 47
file content (57 lines) | stat: -rw-r--r-- 1,523 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xutil.h>
#include <X11/Shell.h>

// listen for mouse motion in all windows under the given one
//
void listen_for_mouse(Display* display, Window window) {
    Window parent, *children;
    unsigned int nchildren;
    int retval;

    retval = XQueryTree(display, window, &window, &parent, &children, &nchildren);
    if (retval == FALSE) {
        fprintf(stderr, "XQueryTree() failed: %d\n", retval);
        return;
    }

    if (nchildren == 0) return;
    XSelectInput(display, window, PointerMotionMask);
    for (int i=0; i<nchildren; i++) {
        XSelectInput(display, children[i], PointerMotionMask);
        listen_for_mouse(display, children[i]);
    }
    XFree((char *)children);
}

// test program for the above.
//
#if 1
int main(int argc, char **argv) {
    XEvent event;
    int count = 0;
    Display* display;

    const char* hostname = argv[1]?argv[1]:":0";
    display = XOpenDisplay(hostname);
    if (!display) {
        fprintf(stderr, "XOpenDisplay(%s) failed\n", hostname);
        exit(1);
    }
    fprintf(stderr, "Sleeping for 10 sec\n");
    sleep(10);
    fprintf(stderr, "Checking for mouse movement\n");
    listen_for_mouse(display, DefaultRootWindow(display));
    XNextEvent(display, &event);
    if (event.type == MotionNotify) {
        fprintf(stderr, "mouse moved, exiting\n");
        exit(0);
    }
}
#endif