updated
[platform/core/uifw/at-spi2-atk.git] / test / visual-bell.c
1 /*
2  * AT-SPI - Assistive Technology Service Provider Interface
3  * (Gnome Accessibility Project; http://developer.gnome.org/projects/gap)
4  *
5  * Copyright 2001, 2002 Sun Microsystems Inc.,
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <X11/XKBlib.h>
29
30 static  Display *display;
31 static  Window flash_window = None;
32
33 static void
34 visual_bell_notify (XkbAnyEvent *xkb_ev)
35 {
36         Window root;
37         int width, height;
38         root = RootWindow (display, DefaultScreen (display));
39         width = DisplayWidth (display, DefaultScreen (display));
40         height = DisplayHeight (display, DefaultScreen (display));
41         if (flash_window == None)
42         {
43                 Visual *visual = CopyFromParent;
44                 XVisualInfo info_return;
45                 XSetWindowAttributes xswa;
46                 int depth = CopyFromParent;
47                 xswa.save_under = True;
48                 xswa.override_redirect = True;
49                 /* TODO: use XGetVisualInfo and determine which is an
50                    overlay, if one is present.  Not sure how to tell
51                    this yet... */
52                 if (XMatchVisualInfo (display,
53                                       DefaultScreen (display),
54                                       8,
55                                       PseudoColor,
56                                       &info_return)) {
57                         depth = 8;
58                         visual = info_return.visual;
59                 }
60                 else
61                 {
62                         fprintf (stderr, "could not create overlay visual, using default root visual type\n");
63                 }
64                 flash_window = XCreateWindow (display, root,
65                                               0, 0, width, height,
66                                               0, depth,
67                                               InputOutput,
68                                               visual,
69                                               CWSaveUnder | CWOverrideRedirect,
70                                               &xswa);
71                 XSelectInput (display, flash_window, ExposureMask);
72                 XMapWindow (display, flash_window);
73         }
74         else
75         {
76                 /* just draw something in the window */
77                 GC gc = XCreateGC (display, flash_window, 0, NULL);
78                 XMapWindow (display, flash_window);
79                 XSetForeground (display, gc,
80                                 WhitePixel (display, DefaultScreen (display)));
81                 XFillRectangle (display, flash_window, gc,
82                                 0, 0, width, height);
83                 XSetForeground (display, gc,
84                                 BlackPixel (display, DefaultScreen (display)));
85                 XFillRectangle (display, flash_window, gc,
86                                 0, 0, width, height);
87         }
88         XFlush (display);
89 }
90
91 int main (int argc, char **argv)
92 {
93         XEvent xev;
94         int ir, xkb_base_event_type, reason_return;
95         char *display_name = getenv ("DISPLAY");
96
97         if (!display_name) display_name = ":0.0";
98         
99         display = XkbOpenDisplay (display_name,
100                                   &xkb_base_event_type,
101                                   &ir, NULL, NULL, &reason_return);
102         if (!display)
103         {
104                 fprintf (stderr, "Could not connect to display! (%d)\n",
105                          reason_return);
106                 exit (-1);
107         }
108         
109         XkbSelectEvents (display,
110                          XkbUseCoreKbd,
111                          XkbBellNotifyMask,
112                          XkbBellNotifyMask);
113
114         /* comment this out to prevent bell on startup */
115         XkbBell (display, None, 100, None);
116
117         while (1)
118         {
119                 XNextEvent (display, &xev);
120                 if (xev.type == Expose)
121                 {
122                         XExposeEvent *exev = (XExposeEvent *) &xev;
123                         if (exev->window == flash_window)
124                         {
125                                 XUnmapWindow (display, flash_window);
126                                 /* discard pending bells */
127                                 XSync (display, True); 
128                                 XFlush (display);
129                         }
130                 }
131                 else if (xev.type == xkb_base_event_type)
132                 {
133                         XkbAnyEvent *xkb_ev = (XkbAnyEvent *) &xev;
134                         
135                         switch (xkb_ev->xkb_type)
136                         {
137                         case XkbBellNotify:
138                                 /* flash something */
139                                 visual_bell_notify (xkb_ev);
140                                 break;
141                         }
142                 }
143         }
144 }
145