xkbbell.c revision b4485a66
1/************************************************************
2Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3
4Permission to use, copy, modify, and distribute this
5software and its documentation for any purpose and without
6fee is hereby granted, provided that the above copyright
7notice appear in all copies and that both that copyright
8notice and this permission notice appear in supporting
9documentation, and that the name of Silicon Graphics not be
10used in advertising or publicity pertaining to distribution
11of the software without specific prior written permission.
12Silicon Graphics makes no representation about the suitability
13of this software for any purpose. It is provided "as is"
14without any express or implied warranty.
15
16SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25********************************************************/
26
27#ifdef HAVE_CONFIG_H
28# include       "config.h"
29#endif
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <X11/Xproto.h>
35#include <X11/Xlib.h>
36#include <X11/X.h>
37#include <X11/XKBlib.h>
38#include <X11/extensions/XI.h>
39
40static char *dpyName = NULL;
41static int volume = 0;
42static int devSpec = XkbUseCoreKbd;
43static int class = -1;
44static int id = -1;
45static char *bellName;
46static Atom nameAtom = None;
47static int synch = 0;
48static int win = None;
49static int force = 0;
50static int nobeep = 0;
51
52static int
53parseArgs(int argc, char *argv[])
54{
55    int i;
56
57    for (i = 1; i < argc; i++) {
58        if (strcmp(argv[i], "-display") == 0) {
59            if (++i < argc)
60                dpyName = argv[i];
61            else {
62                fprintf(stderr,
63                        "Must specify a display with -display option\n");
64                return 0;
65            }
66        }
67        else if ((strcmp(argv[i], "-help") == 0) ||
68                 (strcmp(argv[i], "-usage") == 0)) {
69            return 0;
70        }
71        else if (strcmp(argv[i], "-version") == 0) {
72            printf("xkbbell (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
73            exit(0);
74        }
75        else if (strcmp(argv[i], "-synch") == 0) {
76            synch = 1;
77        }
78        else if (strcmp(argv[i], "-force") == 0) {
79            force = 1;
80        }
81        else if (strcmp(argv[i], "-nobeep") == 0) {
82            nobeep = 1;
83        }
84        else if (strcmp(argv[i], "-dev") == 0) {
85            if (++i < argc) {
86                if (sscanf(argv[i], " %i ", &devSpec) != 1) {
87                    fprintf(stderr, "Device ID must be an integer\n");
88                    return 0;
89                }
90            }
91            else {
92                fprintf(stderr, "Must specify a device ID with -dev option\n");
93                return 0;
94            }
95        }
96        else if (strcmp(argv[i], "-kf") == 0) {
97            if (++i < argc) {
98                if (sscanf(argv[i], " %i ", &id) != 1) {
99                    fprintf(stderr,
100                            "Keyboard feedback ID must be an integer\n");
101                    return 0;
102                }
103                class = KbdFeedbackClass;
104            }
105            else {
106                fprintf(stderr,
107                        "Must specify a keyboard feedback ID for -kf\n");
108                return 0;
109            }
110        }
111        else if (strcmp(argv[i], "-bf") == 0) {
112            if (++i < argc) {
113                if (sscanf(argv[i], " %i ", &id) != 1) {
114                    fprintf(stderr, "Bell feedback ID must be an integer\n");
115                    return 0;
116                }
117                class = BellFeedbackClass;
118            }
119            else {
120                fprintf(stderr, "Must specify a bell feedback ID for -bf\n");
121                return 0;
122            }
123        }
124        else if (strcmp(argv[i], "-v") == 0) {
125            if (++i < argc) {
126                if ((sscanf(argv[i], " %i ", &volume) != 1) ||
127                    (volume < -100) || (volume > 100)) {
128                    fprintf(stderr, "Volume must be in the range -100..100\n");
129                    return 0;
130                }
131            }
132            else {
133                fprintf(stderr, "Must specify volume for -v\n");
134                return 0;
135            }
136        }
137        else if (strcmp(argv[i], "-w") == 0) {
138            if (++i < argc) {
139                if (sscanf(argv[i], " %i ", &win) != 1) {
140                    fprintf(stderr, "Must specify a numeric window ID\n");
141                    return 0;
142                }
143            }
144            else {
145                fprintf(stderr, "Must specify a window ID for -w\n");
146                return 0;
147            }
148        }
149        else {
150            if (i < argc - 1) {
151                fprintf(stderr, "Bell name must be the last argument\n");
152                return 0;
153            }
154            bellName = argv[i];
155        }
156    }
157    return 1;
158}
159
160int
161main(int argc, char *argv[])
162{
163    Display *dpy;
164
165    int i1, i2, i3, i4, i5;
166
167    if (!parseArgs(argc, argv)) {
168        fprintf(stderr, "Usage: %s [ <options> ] <name>\n", argv[0]);
169        fprintf(stderr, "Where legal options are:\n"
170                "-help              print this message\n"
171                "-usage             print this message\n"
172                "-version           print the program version\n"
173                "-display <dpy>     specifies display to use\n"
174                "-synch             turn on synchronization\n"
175                "-dev <id>          specifies device to use\n"
176                "-force             force audible bell\n"
177                "-nobeep            suppress server bell, event only\n"
178                "-bf <id>           specifies bell feedback to use\n"
179                "-kf <id>           specifies keyboard feedback to use\n"
180                "-v <volume>        specifies volume to use\n"
181                "-w <id>            specifies window to use\n");
182        fprintf(stderr,
183                "If neither device nor feedback are specified, %s uses the\n"
184                "default values for the core keyboard device.\n",
185                argv[0]);
186        return 1;
187    }
188    dpy = XOpenDisplay(dpyName);
189    if (!dpy) {
190        fprintf(stderr, "Couldn't open display \"%s\"\n",
191                XDisplayName(dpyName));
192        return 1;
193    }
194    if (synch)
195        XSynchronize(dpy, 1);
196    i1 = XkbMajorVersion;
197    i2 = XkbMinorVersion;
198    if (!XkbLibraryVersion(&i1, &i2)) {
199        fprintf(stderr, "Warning! X library built with XKB version %d.%02d\n",
200                i1, i2);
201        fprintf(stderr, "         but %s was built with %d.%02d\n", argv[0],
202                XkbMajorVersion, XkbMinorVersion);
203        fprintf(stderr, "         Trying anyway\n");
204    }
205    if (!XkbQueryExtension(dpy, &i1, &i2, &i3, &i4, &i5) > 0) {
206        if ((i4 != 0) || (i5 != 0))
207            fprintf(stderr,
208                    "server supports incompatible XKB version %d.%02d\n", i4,
209                    i5);
210        else
211            fprintf(stderr, "XkbQueryExtension failed\n");
212        fprintf(stderr, "Trying anyway\n");
213    }
214    if (force && (nameAtom != None))
215        fprintf(stderr, "Warning! Name ignored for forced bell requests\n");
216    if (bellName != NULL)
217        nameAtom = XInternAtom(dpy, bellName, 0);
218    if ((devSpec == XkbUseCoreKbd) && (class < 0)) {
219        Bool ok;
220
221        if (force)
222            ok = XkbForceBell(dpy, volume);
223        else if (nobeep)
224            ok = XkbBellEvent(dpy, win, volume, nameAtom);
225        else
226            ok = XkbBell(dpy, win, volume, nameAtom);
227        if (!ok)
228            fprintf(stderr, "XkbBell request failed\n");
229    }
230    else {
231        Bool ok;
232
233        if (class < 0)
234            class = KbdFeedbackClass;
235        if (id < 0)
236            id = 0;
237        if (force)
238            ok = XkbForceDeviceBell(dpy, devSpec, class, id, volume);
239        else if (nobeep)
240            ok = XkbDeviceBellEvent(dpy, win, devSpec, class, id, volume,
241                                    nameAtom);
242        else
243            ok = XkbDeviceBell(dpy, win, devSpec, class, id, volume, nameAtom);
244        if (!ok)
245            fprintf(stderr, "XkbDeviceBell request failed\n");
246    }
247/* BAIL: */
248    XCloseDisplay(dpy);
249    return 0;
250}
251