Change to the linux kernel coding style
[wmaker-crm.git] / test / notest.c
1 /* quick and dirty test application that demonstrates: Notify grabbing
2 *
3 * TODO: remake
4 */
5
6 #include <stdio.h>
7 #include <X11/Xlib.h>
8 #include <X11/Xutil.h>
9 #include <X11/Xproto.h>
10 #include <WMaker.h>
11
12 Display *dpy;
13 Window leader;
14 WMAppContext *app;
15 Atom delete_win;
16 Atom prots[6];
17 XWMHints *hints;
18 WMMenu *menu;
19
20 static void quit(void *foo, int item, Time time)
21 {
22 exit(0);
23 }
24
25 static void hide(void *foo, int item, Time time)
26 {
27 WMHideApplication(app);
28 }
29
30 int notify_print(int id, XEvent * event, void *data)
31 {
32 printf("Got notification 0x%x, window 0x%lx, data '%s'\n", id, event->xclient.data.l[1], (char *)data);
33 return True;
34 }
35
36 static void newwin(void *foo, int item, Time time)
37 {
38 Window win;
39 XClassHint classhint;
40 char title[100];
41
42 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 200, 100, 0, 0, 0);
43 prots[0] = delete_win;
44 XSetWMProtocols(dpy, win, prots, 1);
45 sprintf(title, "Notify Test Window");
46 XStoreName(dpy, win, title);
47
48 /* set class hint */
49 classhint.res_name = "notest";
50 classhint.res_class = "Notest";
51 XSetClassHint(dpy, win, &classhint);
52
53 hints = XAllocWMHints();
54 /* set window group leader */
55 hints->window_group = leader;
56 hints->flags = WindowGroupHint;
57 XSetWMHints(dpy, win, hints);
58
59 WMAppAddWindow(app, win);
60 XMapWindow(dpy, win);
61 }
62
63 int main(int argc, char **argv)
64 {
65 XClassHint classhint;
66
67 dpy = XOpenDisplay("");
68 if (!dpy) {
69 puts("could not open display!");
70 exit(1);
71 }
72 delete_win = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
73
74 leader = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 10, 10, 10, 10, 0, 0, 0);
75 /* set class hint */
76 classhint.res_name = "notest";
77 classhint.res_class = "Notest";
78 XSetClassHint(dpy, leader, &classhint);
79
80 /* set window group leader to self */
81 hints = XAllocWMHints();
82 hints->window_group = leader;
83 hints->flags = WindowGroupHint;
84 XSetWMHints(dpy, leader, hints);
85
86 /* create app context */
87 app = WMAppCreateWithMain(dpy, DefaultScreen(dpy), leader);
88 menu = WMMenuCreate(app, "Notify Test Menu");
89 WMMenuAddItem(menu, "Hide", (WMMenuAction) hide, NULL, NULL, NULL);
90 WMMenuAddItem(menu, "Quit", (WMMenuAction) quit, NULL, NULL, NULL);
91
92 WMAppSetMainMenu(app, menu);
93 WMRealizeMenus(app);
94
95 /* Get some WindowMaker notifications */
96 WMNotifySet(app, WMN_APP_START, notify_print, (void *)"App start");
97 WMNotifySet(app, WMN_APP_EXIT, notify_print, (void *)"App end");
98 WMNotifySet(app, WMN_WIN_FOCUS, notify_print, (void *)"Focus in");
99 WMNotifySet(app, WMN_WIN_UNFOCUS, notify_print, (void *)"Focus out");
100 WMNotifySet(app, WMN_NOTIFY_ALL, notify_print, (void *)"Unknown type");
101 WMNotifyMaskUpdate(app); /* Mask isn't actually set till we do this */
102
103 /* set command to use to startup this */
104 XSetCommand(dpy, leader, argv, argc);
105
106 /* create first window */
107 newwin(NULL, 0, 0);
108
109 XFlush(dpy);
110 while (1) {
111 XEvent ev;
112 XNextEvent(dpy, &ev);
113 if (ev.type == ClientMessage) {
114 if (ev.xclient.data.l[0] == delete_win) {
115 XDestroyWindow(dpy, ev.xclient.window);
116 break;
117 }
118 }
119 WMProcessEvent(app, &ev);
120 }
121 exit(0);
122 }