~smspillaz/+junk/focusinevents

« back to all changes in this revision

Viewing changes to focusinevents.cpp

  • Committer: Sam Spilsbury
  • Date: 2011-05-29 14:51:47 UTC
  • Revision ID: sam.spilsbury@canonical.com-20110529145147-fjfe0wjom1xk5pvg
Focus In Events is a small program to capture who's stealing the focus
with their damn key grabs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <X11/Xlib.h>
 
4
/*Linux users will need to add -ldl to the Makefile to compile 
 
5
 *this example.
 
6
 */
 
7
Display *dis;
 
8
Window win;
 
9
XEvent report;
 
10
 
 
11
int handle_error (Display *d, XErrorEvent *error)
 
12
{
 
13
    fprintf (stderr, "X Error\n");
 
14
    return 0;
 
15
}
 
16
 
 
17
int select_all_substructure_notify (Window id)
 
18
{
 
19
    Window root_return, parent_return;
 
20
    Window *children = NULL;
 
21
    unsigned int nchildren;
 
22
    XEvent ev;
 
23
 
 
24
    int ret = XQueryTree (dis, id, &root_return, &parent_return, &children, &nchildren), i = 0;
 
25
 
 
26
    fprintf (stderr, "%i selecting for StructureNotifyMask or FocusChangeMask on 0x%x\n", ret, id);
 
27
 
 
28
    XSelectInput (dis, id, SubstructureNotifyMask | StructureNotifyMask | FocusChangeMask);
 
29
 
 
30
    if (!ret)
 
31
    {
 
32
        if (children)
 
33
            XFree (children);
 
34
 
 
35
        return ret;
 
36
    }
 
37
 
 
38
    for (i = i; i < nchildren; i++)
 
39
        select_all_substructure_notify (children[i]);
 
40
}
 
41
 
 
42
int main() {
 
43
        dis = XOpenDisplay(NULL);
 
44
 
 
45
        XSetErrorHandler (&handle_error);
 
46
        
 
47
        select_all_substructure_notify (RootWindow (dis, DefaultScreen (dis))); 
 
48
 
 
49
        while (1)  {
 
50
        XNextEvent(dis, &report);
 
51
                switch  (report.type) {
 
52
        case FocusIn:  
 
53
            if (report.xfocus.mode == NotifyGrab)
 
54
                fprintf (stdout, "NotifyGrab FocusIn from 0x%x\n", report.xfocus.window);
 
55
            break;
 
56
        case FocusOut:
 
57
            if (report.xfocus.mode == NotifyUngrab)
 
58
                fprintf (stdout, "NotifyUngrab FocusOut from 0x%x\n", report.xfocus.window);
 
59
            break;
 
60
        case CreateNotify:
 
61
            select_all_substructure_notify (report.xcreatewindow.window);
 
62
            break;
 
63
        default:
 
64
            break;
 
65
                }
 
66
        }
 
67
 
 
68
    return 0;
 
69
}