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