ddxBeep.c 10 KB

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