1cc7259d09c255a266a9ab5ec9b006326077aa08
[dana/openbox.git] / tests / focusout.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    focusout.c for the Openbox window manager
4    Copyright (c) 2003-2007   Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23
24 int main () {
25     Display   *display;
26     Window     win, child;
27     XEvent     report;
28     Atom       _net_fs, _net_state;
29     XEvent     msg;
30     int        x=50,y=50,h=100,w=400;
31     XWMHints   hint;
32
33     display = XOpenDisplay(NULL);
34
35     if (display == NULL) {
36         fprintf(stderr, "couldn't connect to X server :0\n");
37         return 0;
38     }
39
40     win = XCreateWindow(display, RootWindow(display, 0),
41                         x, y, w, h, 10, CopyFromParent, CopyFromParent,
42                         CopyFromParent, 0, NULL);
43     child = XCreateWindow(display, win,
44                         10, 10, w-20, h-20, 0, CopyFromParent, CopyFromParent,
45                         CopyFromParent, 0, NULL);
46
47     XSetWindowBackground(display,win,WhitePixel(display,0)); 
48     XSetWindowBackground(display,child,BlackPixel(display,0)); 
49
50     XSelectInput(display, win,
51                  FocusChangeMask|EnterWindowMask|LeaveWindowMask);
52     XMapWindow(display, win);
53     XMapWindow(display, child);
54
55     while (1) {
56         const char *mode, *detail;
57
58         XNextEvent(display, &report);
59
60         switch (report.type) {
61         case ButtonPress:
62             printf("button press\n");
63             printf("type        : %d\n", report.xbutton.type);
64             printf("serial      : %d\n", report.xbutton.serial);
65             printf("send_event  : %d\n", report.xbutton.send_event);
66             printf("display     : 0x%x\n", report.xbutton.display);
67             printf("window      : 0x%x\n", report.xbutton.window);
68             printf("root        : 0x%x\n", report.xbutton.root);
69             printf("subwindow   : 0x%x\n", report.xbutton.subwindow);
70             printf("time        : %d\n", report.xbutton.time);
71             printf("x, y        : %d, %d\n", report.xbutton.x,
72                    report.xbutton.y);
73             printf("rootx, rooty: %d, %d\n", report.xbutton.x_root,
74                    report.xbutton.y_root);
75             printf("state       : 0x%x\n", report.xbutton.state);
76             printf("button      : %d\n", report.xbutton.button);
77             printf("same_screen : %d\n", report.xbutton.same_screen);
78             printf("---\n");
79             break;
80         case MotionNotify:
81             printf("motion\n");
82             printf("type        : %d\n", report.xmotion.type);
83             printf("serial      : %d\n", report.xmotion.serial);
84             printf("send_event  : %d\n", report.xmotion.send_event);
85             printf("display     : 0x%x\n", report.xmotion.display);
86             printf("window      : 0x%x\n", report.xmotion.window);
87             printf("root        : 0x%x\n", report.xmotion.root);
88             printf("subwindow   : 0x%x\n", report.xmotion.subwindow);
89             printf("time        : %d\n", report.xmotion.time);
90             printf("x, y        : %d, %d\n", report.xmotion.x,
91                    report.xmotion.y);
92             printf("rootx, rooty: %d, %d\n", report.xmotion.x_root,
93                    report.xmotion.y_root);
94             printf("state       : 0x%x\n", report.xmotion.state);
95             printf("is_hint     : %d\n", report.xmotion.is_hint);
96             printf("same_screen : %d\n", report.xmotion.same_screen);
97             printf("---\n");
98             if (XGrabPointer(display, win, False, ButtonReleaseMask,
99                              GrabModeAsync, GrabModeAsync, None, None,
100                              report.xmotion.time) == GrabSuccess)
101                 printf("GrabSuccess\n");
102             else
103                 printf("GrabFail\n");
104             break;
105         case ButtonRelease:
106             XUngrabPointer(display, report.xbutton.time);
107             break;
108         case FocusIn:
109             switch (report.xfocus.mode) {
110             case NotifyNormal: mode = "NotifyNormal"; break;
111             case NotifyGrab: mode = "NotifyGrab"; break;
112             case NotifyUngrab: mode = "NotifyUngrab"; break;
113             }
114
115             switch (report.xfocus.detail) {
116             case NotifyAncestor: detail = "NotifyAncestor"; break;
117             case NotifyVirtual: detail = "NotifyVirtual"; break;
118             case NotifyInferior: detail = "NotifyInferior"; break;
119             case NotifyNonlinear: detail = "NotifyNonlinear"; break;
120             case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
121             case NotifyPointer: detail = "NotifyPointer"; break;
122             case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
123             case NotifyDetailNone: detail = "NotifyDetailNone"; break;
124             }
125             printf("focusin\n");
126             printf("type      : %d\n", report.xfocus.type);
127             printf("serial    : %d\n", report.xfocus.serial);
128             printf("send_event: %d\n", report.xfocus.send_event);
129             printf("display   : 0x%x\n", report.xfocus.display);
130             printf("window    : 0x%x\n", report.xfocus.window);
131             printf("mode      : %s\n", mode);
132             printf("detail    : %s\n", detail);
133             printf("---\n");
134             break;
135         case FocusOut:
136             switch (report.xfocus.mode) {
137             case NotifyNormal: mode = "NotifyNormal"; break;
138             case NotifyGrab: mode = "NotifyGrab"; break;
139             case NotifyUngrab: mode = "NotifyUngrab"; break;
140             }
141
142             switch (report.xfocus.detail) {
143             case NotifyAncestor: detail = "NotifyAncestor"; break;
144             case NotifyVirtual: detail = "NotifyVirtual"; break;
145             case NotifyInferior: detail = "NotifyInferior"; break;
146             case NotifyNonlinear: detail = "NotifyNonlinear"; break;
147             case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
148             case NotifyPointer: detail = "NotifyPointer"; break;
149             case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
150             case NotifyDetailNone: detail = "NotifyDetailNone"; break;
151             }
152             printf("focusout\n");
153             printf("type      : %d\n", report.xfocus.type);
154             printf("serial    : %d\n", report.xfocus.serial);
155             printf("send_event: %d\n", report.xfocus.send_event);
156             printf("display   : 0x%x\n", report.xfocus.display);
157             printf("window    : 0x%x\n", report.xfocus.window);
158             printf("mode      : %s\n", mode);
159             printf("detail    : %s\n", detail);
160             printf("---\n");
161             break;
162         case EnterNotify:
163             switch (report.xcrossing.mode) {
164             case NotifyNormal: mode = "NotifyNormal"; break;
165             case NotifyGrab: mode = "NotifyGrab"; break;
166             case NotifyUngrab: mode = "NotifyUngrab"; break;
167             }
168
169             switch (report.xcrossing.detail) {
170             case NotifyAncestor: detail = "NotifyAncestor"; break;
171             case NotifyVirtual: detail = "NotifyVirtual"; break;
172             case NotifyInferior: detail = "NotifyInferior"; break;
173             case NotifyNonlinear: detail = "NotifyNonlinear"; break;
174             case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
175             case NotifyPointer: detail = "NotifyPointer"; break;
176             case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
177             case NotifyDetailNone: detail = "NotifyDetailNone"; break;
178             }
179             printf("enternotify\n");
180             printf("type      : %d\n", report.xcrossing.type);
181             printf("serial    : %d\n", report.xcrossing.serial);
182             printf("send_event: %d\n", report.xcrossing.send_event);
183             printf("display   : 0x%x\n", report.xcrossing.display);
184             printf("window    : 0x%x\n", report.xcrossing.window);
185             printf("mode      : %s\n", mode);
186             printf("detail    : %s\n", detail);
187             printf("---\n");
188             break;
189         case LeaveNotify:
190             switch (report.xcrossing.mode) {
191             case NotifyNormal: mode = "NotifyNormal"; break;
192             case NotifyGrab: mode = "NotifyGrab"; break;
193             case NotifyUngrab: mode = "NotifyUngrab"; break;
194             }
195
196             switch (report.xcrossing.detail) {
197             case NotifyAncestor: detail = "NotifyAncestor"; break;
198             case NotifyVirtual: detail = "NotifyVirtual"; break;
199             case NotifyInferior: detail = "NotifyInferior"; break;
200             case NotifyNonlinear: detail = "NotifyNonlinear"; break;
201             case NotifyNonlinearVirtual: detail = "NotifyNonlinearVirtual"; break;
202             case NotifyPointer: detail = "NotifyPointer"; break;
203             case NotifyPointerRoot: detail = "NotifyPointerRoot"; break;
204             case NotifyDetailNone: detail = "NotifyDetailNone"; break;
205             }
206             printf("leavenotify\n");
207             printf("type      : %d\n", report.xcrossing.type);
208             printf("serial    : %d\n", report.xcrossing.serial);
209             printf("send_event: %d\n", report.xcrossing.send_event);
210             printf("display   : 0x%x\n", report.xcrossing.display);
211             printf("window    : 0x%x\n", report.xcrossing.window);
212             printf("mode      : %s\n", mode);
213             printf("detail    : %s\n", detail);
214             printf("---\n");
215             break;
216         }
217     }
218
219     return 1;
220 }