xref: /OK3568_Linux_fs/external/xserver/xkb/ddxBeep.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /************************************************************
2*4882a593Smuzhiyun Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun Permission to use, copy, modify, and distribute this
5*4882a593Smuzhiyun software and its documentation for any purpose and without
6*4882a593Smuzhiyun fee is hereby granted, provided that the above copyright
7*4882a593Smuzhiyun notice appear in all copies and that both that copyright
8*4882a593Smuzhiyun notice and this permission notice appear in supporting
9*4882a593Smuzhiyun documentation, and that the name of Silicon Graphics not be
10*4882a593Smuzhiyun used in advertising or publicity pertaining to distribution
11*4882a593Smuzhiyun of the software without specific prior written permission.
12*4882a593Smuzhiyun Silicon Graphics makes no representation about the suitability
13*4882a593Smuzhiyun of this software for any purpose. It is provided "as is"
14*4882a593Smuzhiyun without any express or implied warranty.
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17*4882a593Smuzhiyun SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
18*4882a593Smuzhiyun AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19*4882a593Smuzhiyun GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
20*4882a593Smuzhiyun DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21*4882a593Smuzhiyun DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22*4882a593Smuzhiyun OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
23*4882a593Smuzhiyun THE USE OR PERFORMANCE OF THIS SOFTWARE.
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun ********************************************************/
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #ifdef HAVE_DIX_CONFIG_H
28*4882a593Smuzhiyun #include <dix-config.h>
29*4882a593Smuzhiyun #endif
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <stdio.h>
32*4882a593Smuzhiyun #include <X11/X.h>
33*4882a593Smuzhiyun #include <X11/Xproto.h>
34*4882a593Smuzhiyun #include <X11/keysym.h>
35*4882a593Smuzhiyun #include "inputstr.h"
36*4882a593Smuzhiyun #include "scrnintstr.h"
37*4882a593Smuzhiyun #include "windowstr.h"
38*4882a593Smuzhiyun #include <xkbsrv.h>
39*4882a593Smuzhiyun #include <X11/extensions/XI.h>
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*#define FALLING_TONE	1*/
42*4882a593Smuzhiyun /*#define RISING_TONE	1*/
43*4882a593Smuzhiyun #define FALLING_TONE	10
44*4882a593Smuzhiyun #define RISING_TONE	10
45*4882a593Smuzhiyun #define	SHORT_TONE	50
46*4882a593Smuzhiyun #define	SHORT_DELAY	60
47*4882a593Smuzhiyun #define	LONG_TONE	75
48*4882a593Smuzhiyun #define	VERY_LONG_TONE	100
49*4882a593Smuzhiyun #define	LONG_DELAY	85
50*4882a593Smuzhiyun #define CLICK_DURATION	1
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define	DEEP_PITCH	250
53*4882a593Smuzhiyun #define	LOW_PITCH	500
54*4882a593Smuzhiyun #define	MID_PITCH	1000
55*4882a593Smuzhiyun #define	HIGH_PITCH	2000
56*4882a593Smuzhiyun #define CLICK_PITCH	1500
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun static unsigned long atomGeneration = 0;
59*4882a593Smuzhiyun static Atom featureOn;
60*4882a593Smuzhiyun static Atom featureOff;
61*4882a593Smuzhiyun static Atom featureChange;
62*4882a593Smuzhiyun static Atom ledOn;
63*4882a593Smuzhiyun static Atom ledOff;
64*4882a593Smuzhiyun static Atom ledChange;
65*4882a593Smuzhiyun static Atom slowWarn;
66*4882a593Smuzhiyun static Atom slowPress;
67*4882a593Smuzhiyun static Atom slowReject;
68*4882a593Smuzhiyun static Atom slowAccept;
69*4882a593Smuzhiyun static Atom slowRelease;
70*4882a593Smuzhiyun static Atom stickyLatch;
71*4882a593Smuzhiyun static Atom stickyLock;
72*4882a593Smuzhiyun static Atom stickyUnlock;
73*4882a593Smuzhiyun static Atom bounceReject;
74*4882a593Smuzhiyun static char doesPitch = 1;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define	FEATURE_ON	"AX_FeatureOn"
77*4882a593Smuzhiyun #define	FEATURE_OFF	"AX_FeatureOff"
78*4882a593Smuzhiyun #define	FEATURE_CHANGE	"AX_FeatureChange"
79*4882a593Smuzhiyun #define	LED_ON		"AX_IndicatorOn"
80*4882a593Smuzhiyun #define	LED_OFF		"AX_IndicatorOff"
81*4882a593Smuzhiyun #define	LED_CHANGE	"AX_IndicatorChange"
82*4882a593Smuzhiyun #define	SLOW_WARN	"AX_SlowKeysWarning"
83*4882a593Smuzhiyun #define	SLOW_PRESS	"AX_SlowKeyPress"
84*4882a593Smuzhiyun #define	SLOW_REJECT	"AX_SlowKeyReject"
85*4882a593Smuzhiyun #define	SLOW_ACCEPT	"AX_SlowKeyAccept"
86*4882a593Smuzhiyun #define	SLOW_RELEASE	"AX_SlowKeyRelease"
87*4882a593Smuzhiyun #define	STICKY_LATCH	"AX_StickyLatch"
88*4882a593Smuzhiyun #define	STICKY_LOCK	"AX_StickyLock"
89*4882a593Smuzhiyun #define	STICKY_UNLOCK	"AX_StickyUnlock"
90*4882a593Smuzhiyun #define	BOUNCE_REJECT	"AX_BounceKeyReject"
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun #define	MAKE_ATOM(a)	MakeAtom(a,sizeof(a)-1,TRUE)
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun static void
_XkbDDXBeepInitAtoms(void)95*4882a593Smuzhiyun _XkbDDXBeepInitAtoms(void)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun     featureOn = MAKE_ATOM(FEATURE_ON);
98*4882a593Smuzhiyun     featureOff = MAKE_ATOM(FEATURE_OFF);
99*4882a593Smuzhiyun     featureChange = MAKE_ATOM(FEATURE_CHANGE);
100*4882a593Smuzhiyun     ledOn = MAKE_ATOM(LED_ON);
101*4882a593Smuzhiyun     ledOff = MAKE_ATOM(LED_OFF);
102*4882a593Smuzhiyun     ledChange = MAKE_ATOM(LED_CHANGE);
103*4882a593Smuzhiyun     slowWarn = MAKE_ATOM(SLOW_WARN);
104*4882a593Smuzhiyun     slowPress = MAKE_ATOM(SLOW_PRESS);
105*4882a593Smuzhiyun     slowReject = MAKE_ATOM(SLOW_REJECT);
106*4882a593Smuzhiyun     slowAccept = MAKE_ATOM(SLOW_ACCEPT);
107*4882a593Smuzhiyun     slowRelease = MAKE_ATOM(SLOW_RELEASE);
108*4882a593Smuzhiyun     stickyLatch = MAKE_ATOM(STICKY_LATCH);
109*4882a593Smuzhiyun     stickyLock = MAKE_ATOM(STICKY_LOCK);
110*4882a593Smuzhiyun     stickyUnlock = MAKE_ATOM(STICKY_UNLOCK);
111*4882a593Smuzhiyun     bounceReject = MAKE_ATOM(BOUNCE_REJECT);
112*4882a593Smuzhiyun     return;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun static CARD32
_XkbDDXBeepExpire(OsTimerPtr timer,CARD32 now,void * arg)116*4882a593Smuzhiyun _XkbDDXBeepExpire(OsTimerPtr timer, CARD32 now, void *arg)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun     DeviceIntPtr dev = (DeviceIntPtr) arg;
119*4882a593Smuzhiyun     KbdFeedbackPtr feed;
120*4882a593Smuzhiyun     KeybdCtrl *ctrl;
121*4882a593Smuzhiyun     XkbSrvInfoPtr xkbInfo;
122*4882a593Smuzhiyun     CARD32 next;
123*4882a593Smuzhiyun     int pitch, duration;
124*4882a593Smuzhiyun     int oldPitch, oldDuration;
125*4882a593Smuzhiyun     Atom name;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun     if ((dev == NULL) || (dev->key == NULL) || (dev->key->xkbInfo == NULL) ||
128*4882a593Smuzhiyun         (dev->kbdfeed == NULL))
129*4882a593Smuzhiyun         return 0;
130*4882a593Smuzhiyun     if (atomGeneration != serverGeneration) {
131*4882a593Smuzhiyun         _XkbDDXBeepInitAtoms();
132*4882a593Smuzhiyun         atomGeneration = serverGeneration;
133*4882a593Smuzhiyun     }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun     feed = dev->kbdfeed;
136*4882a593Smuzhiyun     ctrl = &feed->ctrl;
137*4882a593Smuzhiyun     xkbInfo = dev->key->xkbInfo;
138*4882a593Smuzhiyun     next = 0;
139*4882a593Smuzhiyun     pitch = oldPitch = ctrl->bell_pitch;
140*4882a593Smuzhiyun     duration = oldDuration = ctrl->bell_duration;
141*4882a593Smuzhiyun     name = None;
142*4882a593Smuzhiyun     switch (xkbInfo->beepType) {
143*4882a593Smuzhiyun     default:
144*4882a593Smuzhiyun         ErrorF("[xkb] Unknown beep type %d\n", xkbInfo->beepType);
145*4882a593Smuzhiyun     case _BEEP_NONE:
146*4882a593Smuzhiyun         duration = 0;
147*4882a593Smuzhiyun         break;
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun         /* When an LED is turned on, we want a high-pitched beep.
150*4882a593Smuzhiyun          * When the LED it turned off, we want a low-pitched beep.
151*4882a593Smuzhiyun          * If we cannot do pitch, we want a single beep for on and two
152*4882a593Smuzhiyun          * beeps for off.
153*4882a593Smuzhiyun          */
154*4882a593Smuzhiyun     case _BEEP_LED_ON:
155*4882a593Smuzhiyun         if (name == None)
156*4882a593Smuzhiyun             name = ledOn;
157*4882a593Smuzhiyun         duration = SHORT_TONE;
158*4882a593Smuzhiyun         pitch = HIGH_PITCH;
159*4882a593Smuzhiyun         break;
160*4882a593Smuzhiyun     case _BEEP_LED_OFF:
161*4882a593Smuzhiyun         if (name == None)
162*4882a593Smuzhiyun             name = ledOff;
163*4882a593Smuzhiyun         duration = SHORT_TONE;
164*4882a593Smuzhiyun         pitch = LOW_PITCH;
165*4882a593Smuzhiyun         if (!doesPitch && xkbInfo->beepCount < 1)
166*4882a593Smuzhiyun             next = SHORT_DELAY;
167*4882a593Smuzhiyun         break;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun         /* When a Feature is turned on, we want an up-siren.
170*4882a593Smuzhiyun          * When a Feature is turned off, we want a down-siren.
171*4882a593Smuzhiyun          * If we cannot do pitch, we want a single beep for on and two
172*4882a593Smuzhiyun          * beeps for off.
173*4882a593Smuzhiyun          */
174*4882a593Smuzhiyun     case _BEEP_FEATURE_ON:
175*4882a593Smuzhiyun         if (name == None)
176*4882a593Smuzhiyun             name = featureOn;
177*4882a593Smuzhiyun         if (xkbInfo->beepCount < 1) {
178*4882a593Smuzhiyun             pitch = LOW_PITCH;
179*4882a593Smuzhiyun             duration = VERY_LONG_TONE;
180*4882a593Smuzhiyun             if (doesPitch)
181*4882a593Smuzhiyun                 next = SHORT_DELAY;
182*4882a593Smuzhiyun         }
183*4882a593Smuzhiyun         else {
184*4882a593Smuzhiyun             pitch = MID_PITCH;
185*4882a593Smuzhiyun             duration = SHORT_TONE;
186*4882a593Smuzhiyun         }
187*4882a593Smuzhiyun         break;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun     case _BEEP_FEATURE_OFF:
190*4882a593Smuzhiyun         if (name == None)
191*4882a593Smuzhiyun             name = featureOff;
192*4882a593Smuzhiyun         if (xkbInfo->beepCount < 1) {
193*4882a593Smuzhiyun             pitch = MID_PITCH;
194*4882a593Smuzhiyun             if (doesPitch)
195*4882a593Smuzhiyun                 duration = VERY_LONG_TONE;
196*4882a593Smuzhiyun             else
197*4882a593Smuzhiyun                 duration = SHORT_TONE;
198*4882a593Smuzhiyun             next = SHORT_DELAY;
199*4882a593Smuzhiyun         }
200*4882a593Smuzhiyun         else {
201*4882a593Smuzhiyun             pitch = LOW_PITCH;
202*4882a593Smuzhiyun             duration = SHORT_TONE;
203*4882a593Smuzhiyun         }
204*4882a593Smuzhiyun         break;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun         /* Two high beeps indicate an LED or Feature changed
207*4882a593Smuzhiyun          * state, but that another LED or Feature is also on.
208*4882a593Smuzhiyun          * [[[WDW - This is not in AccessDOS ]]]
209*4882a593Smuzhiyun          */
210*4882a593Smuzhiyun     case _BEEP_LED_CHANGE:
211*4882a593Smuzhiyun         if (name == None)
212*4882a593Smuzhiyun             name = ledChange;
213*4882a593Smuzhiyun     case _BEEP_FEATURE_CHANGE:
214*4882a593Smuzhiyun         if (name == None)
215*4882a593Smuzhiyun             name = featureChange;
216*4882a593Smuzhiyun         duration = SHORT_TONE;
217*4882a593Smuzhiyun         pitch = HIGH_PITCH;
218*4882a593Smuzhiyun         if (xkbInfo->beepCount < 1) {
219*4882a593Smuzhiyun             next = SHORT_DELAY;
220*4882a593Smuzhiyun         }
221*4882a593Smuzhiyun         break;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun         /* Three high-pitched beeps are the warning that SlowKeys
224*4882a593Smuzhiyun          * is going to be turned on or off.
225*4882a593Smuzhiyun          */
226*4882a593Smuzhiyun     case _BEEP_SLOW_WARN:
227*4882a593Smuzhiyun         if (name == None)
228*4882a593Smuzhiyun             name = slowWarn;
229*4882a593Smuzhiyun         duration = SHORT_TONE;
230*4882a593Smuzhiyun         pitch = HIGH_PITCH;
231*4882a593Smuzhiyun         if (xkbInfo->beepCount < 2)
232*4882a593Smuzhiyun             next = SHORT_DELAY;
233*4882a593Smuzhiyun         break;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun         /* Click on SlowKeys press and accept.
236*4882a593Smuzhiyun          * Deep pitch when a SlowKey or BounceKey is rejected.
237*4882a593Smuzhiyun          * [[[WDW - Rejects are not in AccessDOS ]]]
238*4882a593Smuzhiyun          * If we cannot do pitch, we want single beeps.
239*4882a593Smuzhiyun          */
240*4882a593Smuzhiyun     case _BEEP_SLOW_PRESS:
241*4882a593Smuzhiyun         if (name == None)
242*4882a593Smuzhiyun             name = slowPress;
243*4882a593Smuzhiyun     case _BEEP_SLOW_ACCEPT:
244*4882a593Smuzhiyun         if (name == None)
245*4882a593Smuzhiyun             name = slowAccept;
246*4882a593Smuzhiyun     case _BEEP_SLOW_RELEASE:
247*4882a593Smuzhiyun         if (name == None)
248*4882a593Smuzhiyun             name = slowRelease;
249*4882a593Smuzhiyun         duration = CLICK_DURATION;
250*4882a593Smuzhiyun         pitch = CLICK_PITCH;
251*4882a593Smuzhiyun         break;
252*4882a593Smuzhiyun     case _BEEP_BOUNCE_REJECT:
253*4882a593Smuzhiyun         if (name == None)
254*4882a593Smuzhiyun             name = bounceReject;
255*4882a593Smuzhiyun     case _BEEP_SLOW_REJECT:
256*4882a593Smuzhiyun         if (name == None)
257*4882a593Smuzhiyun             name = slowReject;
258*4882a593Smuzhiyun         duration = SHORT_TONE;
259*4882a593Smuzhiyun         pitch = DEEP_PITCH;
260*4882a593Smuzhiyun         break;
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun         /* Low followed by high pitch when a StickyKey is latched.
263*4882a593Smuzhiyun          * High pitch when a StickyKey is locked.
264*4882a593Smuzhiyun          * Low pitch when unlocked.
265*4882a593Smuzhiyun          * If we cannot do pitch, two beeps for latch, nothing for
266*4882a593Smuzhiyun          * lock, and two for unlock.
267*4882a593Smuzhiyun          */
268*4882a593Smuzhiyun     case _BEEP_STICKY_LATCH:
269*4882a593Smuzhiyun         if (name == None)
270*4882a593Smuzhiyun             name = stickyLatch;
271*4882a593Smuzhiyun         duration = SHORT_TONE;
272*4882a593Smuzhiyun         if (xkbInfo->beepCount < 1) {
273*4882a593Smuzhiyun             next = SHORT_DELAY;
274*4882a593Smuzhiyun             pitch = LOW_PITCH;
275*4882a593Smuzhiyun         }
276*4882a593Smuzhiyun         else
277*4882a593Smuzhiyun             pitch = HIGH_PITCH;
278*4882a593Smuzhiyun         break;
279*4882a593Smuzhiyun     case _BEEP_STICKY_LOCK:
280*4882a593Smuzhiyun         if (name == None)
281*4882a593Smuzhiyun             name = stickyLock;
282*4882a593Smuzhiyun         if (doesPitch) {
283*4882a593Smuzhiyun             duration = SHORT_TONE;
284*4882a593Smuzhiyun             pitch = HIGH_PITCH;
285*4882a593Smuzhiyun         }
286*4882a593Smuzhiyun         break;
287*4882a593Smuzhiyun     case _BEEP_STICKY_UNLOCK:
288*4882a593Smuzhiyun         if (name == None)
289*4882a593Smuzhiyun             name = stickyUnlock;
290*4882a593Smuzhiyun         duration = SHORT_TONE;
291*4882a593Smuzhiyun         pitch = LOW_PITCH;
292*4882a593Smuzhiyun         if (!doesPitch && xkbInfo->beepCount < 1)
293*4882a593Smuzhiyun             next = SHORT_DELAY;
294*4882a593Smuzhiyun         break;
295*4882a593Smuzhiyun     }
296*4882a593Smuzhiyun     if (timer == NULL && duration > 0) {
297*4882a593Smuzhiyun         CARD32 starttime = GetTimeInMillis();
298*4882a593Smuzhiyun         CARD32 elapsedtime;
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun         ctrl->bell_duration = duration;
301*4882a593Smuzhiyun         ctrl->bell_pitch = pitch;
302*4882a593Smuzhiyun         if (xkbInfo->beepCount == 0) {
303*4882a593Smuzhiyun             XkbHandleBell(0, 0, dev, ctrl->bell, (void *) ctrl,
304*4882a593Smuzhiyun                           KbdFeedbackClass, name, None, NULL);
305*4882a593Smuzhiyun         }
306*4882a593Smuzhiyun         else if (xkbInfo->desc->ctrls->enabled_ctrls & XkbAudibleBellMask) {
307*4882a593Smuzhiyun             (*dev->kbdfeed->BellProc) (ctrl->bell, dev, (void *) ctrl,
308*4882a593Smuzhiyun                                        KbdFeedbackClass);
309*4882a593Smuzhiyun         }
310*4882a593Smuzhiyun         ctrl->bell_duration = oldDuration;
311*4882a593Smuzhiyun         ctrl->bell_pitch = oldPitch;
312*4882a593Smuzhiyun         xkbInfo->beepCount++;
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun         /* Some DDX schedule the beep and return immediately, others don't
315*4882a593Smuzhiyun            return until the beep is completed.  We measure the time and if
316*4882a593Smuzhiyun            it's less than the beep duration, make sure not to schedule the
317*4882a593Smuzhiyun            next beep until after the current one finishes. */
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun         elapsedtime = GetTimeInMillis();
320*4882a593Smuzhiyun         if (elapsedtime > starttime) {  /* watch out for millisecond counter
321*4882a593Smuzhiyun                                            overflow! */
322*4882a593Smuzhiyun             elapsedtime -= starttime;
323*4882a593Smuzhiyun         }
324*4882a593Smuzhiyun         else {
325*4882a593Smuzhiyun             elapsedtime = 0;
326*4882a593Smuzhiyun         }
327*4882a593Smuzhiyun         if (elapsedtime < duration) {
328*4882a593Smuzhiyun             next += duration - elapsedtime;
329*4882a593Smuzhiyun         }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun     }
332*4882a593Smuzhiyun     return next;
333*4882a593Smuzhiyun }
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun int
XkbDDXAccessXBeep(DeviceIntPtr dev,unsigned what,unsigned which)336*4882a593Smuzhiyun XkbDDXAccessXBeep(DeviceIntPtr dev, unsigned what, unsigned which)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun     XkbSrvInfoRec *xkbInfo = dev->key->xkbInfo;
339*4882a593Smuzhiyun     CARD32 next;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun     xkbInfo->beepType = what;
342*4882a593Smuzhiyun     xkbInfo->beepCount = 0;
343*4882a593Smuzhiyun     next = _XkbDDXBeepExpire(NULL, 0, (void *) dev);
344*4882a593Smuzhiyun     if (next > 0) {
345*4882a593Smuzhiyun         xkbInfo->beepTimer = TimerSet(xkbInfo->beepTimer,
346*4882a593Smuzhiyun                                       0, next,
347*4882a593Smuzhiyun                                       _XkbDDXBeepExpire, (void *) dev);
348*4882a593Smuzhiyun     }
349*4882a593Smuzhiyun     return 1;
350*4882a593Smuzhiyun }
351