xkbLEDs.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /************************************************************
  2. Copyright (c) 1995 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 <ctype.h>
  28. #include <math.h>
  29. #include <X11/X.h>
  30. #include <X11/Xproto.h>
  31. #include "misc.h"
  32. #include "inputstr.h"
  33. #include <X11/extensions/XI.h>
  34. #include <xkbsrv.h>
  35. #include "xkb.h"
  36. /***====================================================================***/
  37. /*
  38. * unsigned
  39. * XkbIndicatorsToUpdate(dev,changed,check_devs_rtrn)
  40. *
  41. * Given a keyboard and a set of state components that have changed,
  42. * this function returns the indicators on the default keyboard
  43. * feedback that might be affected. It also reports whether or not
  44. * any extension devices might be affected in check_devs_rtrn.
  45. */
  46. unsigned
  47. XkbIndicatorsToUpdate(DeviceIntPtr dev,
  48. unsigned long state_changes, Bool enable_changes)
  49. {
  50. register unsigned update = 0;
  51. XkbSrvLedInfoPtr sli;
  52. sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
  53. if (!sli)
  54. return update;
  55. if (state_changes & (XkbModifierStateMask | XkbGroupStateMask))
  56. update |= sli->usesEffective;
  57. if (state_changes & (XkbModifierBaseMask | XkbGroupBaseMask))
  58. update |= sli->usesBase;
  59. if (state_changes & (XkbModifierLatchMask | XkbGroupLatchMask))
  60. update |= sli->usesLatched;
  61. if (state_changes & (XkbModifierLockMask | XkbGroupLockMask))
  62. update |= sli->usesLocked;
  63. if (state_changes & XkbCompatStateMask)
  64. update |= sli->usesCompat;
  65. if (enable_changes)
  66. update |= sli->usesControls;
  67. return update;
  68. }
  69. /***====================================================================***/
  70. /*
  71. * Bool
  72. *XkbApplyLEDChangeToKeyboard(xkbi,map,on,change)
  73. *
  74. * Some indicators "drive" the keyboard when their state is explicitly
  75. * changed, as described in section 9.2.1 of the XKB protocol spec.
  76. * This function updates the state and controls for the keyboard
  77. * specified by 'xkbi' to reflect any changes that are required
  78. * when the indicator described by 'map' is turned on or off. The
  79. * extent of the changes is reported in change, which must be defined.
  80. */
  81. static Bool
  82. XkbApplyLEDChangeToKeyboard(XkbSrvInfoPtr xkbi,
  83. XkbIndicatorMapPtr map,
  84. Bool on, XkbChangesPtr change)
  85. {
  86. Bool ctrlChange, stateChange;
  87. XkbStatePtr state;
  88. if ((map->flags & XkbIM_NoExplicit) ||
  89. ((map->flags & XkbIM_LEDDrivesKB) == 0))
  90. return FALSE;
  91. ctrlChange = stateChange = FALSE;
  92. if (map->ctrls) {
  93. XkbControlsPtr ctrls = xkbi->desc->ctrls;
  94. unsigned old;
  95. old = ctrls->enabled_ctrls;
  96. if (on)
  97. ctrls->enabled_ctrls |= map->ctrls;
  98. else
  99. ctrls->enabled_ctrls &= ~map->ctrls;
  100. if (old != ctrls->enabled_ctrls) {
  101. change->ctrls.changed_ctrls = XkbControlsEnabledMask;
  102. change->ctrls.enabled_ctrls_changes = old ^ ctrls->enabled_ctrls;
  103. ctrlChange = TRUE;
  104. }
  105. }
  106. state = &xkbi->state;
  107. if ((map->groups) && ((map->which_groups & (~XkbIM_UseBase)) != 0)) {
  108. register int i;
  109. register unsigned bit, match;
  110. if (on)
  111. match = (map->groups) & XkbAllGroupsMask;
  112. else
  113. match = (~map->groups) & XkbAllGroupsMask;
  114. if (map->which_groups & (XkbIM_UseLocked | XkbIM_UseEffective)) {
  115. for (i = 0, bit = 1; i < XkbNumKbdGroups; i++, bit <<= 1) {
  116. if (bit & match)
  117. break;
  118. }
  119. if (map->which_groups & XkbIM_UseLatched)
  120. XkbLatchGroup(xkbi->device, 0); /* unlatch group */
  121. state->locked_group = i;
  122. stateChange = TRUE;
  123. }
  124. else if (map->which_groups & (XkbIM_UseLatched | XkbIM_UseEffective)) {
  125. for (i = 0, bit = 1; i < XkbNumKbdGroups; i++, bit <<= 1) {
  126. if (bit & match)
  127. break;
  128. }
  129. state->locked_group = 0;
  130. XkbLatchGroup(xkbi->device, i);
  131. stateChange = TRUE;
  132. }
  133. }
  134. if ((map->mods.mask) && ((map->which_mods & (~XkbIM_UseBase)) != 0)) {
  135. if (map->which_mods & (XkbIM_UseLocked | XkbIM_UseEffective)) {
  136. register unsigned long old;
  137. old = state->locked_mods;
  138. if (on)
  139. state->locked_mods |= map->mods.mask;
  140. else
  141. state->locked_mods &= ~map->mods.mask;
  142. if (state->locked_mods != old)
  143. stateChange = TRUE;
  144. }
  145. if (map->which_mods & (XkbIM_UseLatched | XkbIM_UseEffective)) {
  146. register unsigned long newmods;
  147. newmods = state->latched_mods;
  148. if (on)
  149. newmods |= map->mods.mask;
  150. else
  151. newmods &= ~map->mods.mask;
  152. if (newmods != state->locked_mods) {
  153. newmods &= map->mods.mask;
  154. XkbLatchModifiers(xkbi->device, map->mods.mask, newmods);
  155. stateChange = TRUE;
  156. }
  157. }
  158. }
  159. return stateChange || ctrlChange;
  160. }
  161. /*
  162. * Bool
  163. * ComputeAutoState(map,state,ctrls)
  164. *
  165. * This function reports the effect of applying the specified
  166. * indicator map given the specified state and controls, as
  167. * described in section 9.2 of the XKB protocol specification.
  168. */
  169. static Bool
  170. ComputeAutoState(XkbIndicatorMapPtr map,
  171. XkbStatePtr state, XkbControlsPtr ctrls)
  172. {
  173. Bool on;
  174. CARD8 mods, group;
  175. on = FALSE;
  176. mods = group = 0;
  177. if (map->which_mods & XkbIM_UseAnyMods) {
  178. if (map->which_mods & XkbIM_UseBase)
  179. mods |= state->base_mods;
  180. if (map->which_mods & XkbIM_UseLatched)
  181. mods |= state->latched_mods;
  182. if (map->which_mods & XkbIM_UseLocked)
  183. mods |= state->locked_mods;
  184. if (map->which_mods & XkbIM_UseEffective)
  185. mods |= state->mods;
  186. if (map->which_mods & XkbIM_UseCompat)
  187. mods |= state->compat_state;
  188. on = ((map->mods.mask & mods) != 0);
  189. on = on || ((mods == 0) && (map->mods.mask == 0) &&
  190. (map->mods.vmods == 0));
  191. }
  192. if (map->which_groups & XkbIM_UseAnyGroup) {
  193. if (map->which_groups & XkbIM_UseBase)
  194. group |= (1L << state->base_group);
  195. if (map->which_groups & XkbIM_UseLatched)
  196. group |= (1L << state->latched_group);
  197. if (map->which_groups & XkbIM_UseLocked)
  198. group |= (1L << state->locked_group);
  199. if (map->which_groups & XkbIM_UseEffective)
  200. group |= (1L << state->group);
  201. on = on || (((map->groups & group) != 0) || (map->groups == 0));
  202. }
  203. if (map->ctrls)
  204. on = on || (ctrls->enabled_ctrls & map->ctrls);
  205. return on;
  206. }
  207. static void
  208. XkbUpdateLedAutoState(DeviceIntPtr dev,
  209. XkbSrvLedInfoPtr sli,
  210. unsigned maps_to_check,
  211. xkbExtensionDeviceNotify * ed,
  212. XkbChangesPtr changes, XkbEventCausePtr cause)
  213. {
  214. DeviceIntPtr kbd;
  215. XkbStatePtr state;
  216. XkbControlsPtr ctrls;
  217. XkbChangesRec my_changes;
  218. xkbExtensionDeviceNotify my_ed;
  219. register unsigned i, bit, affected;
  220. register XkbIndicatorMapPtr map;
  221. unsigned oldState;
  222. if ((maps_to_check == 0) || (sli->maps == NULL) || (sli->mapsPresent == 0))
  223. return;
  224. if (dev->key && dev->key->xkbInfo)
  225. kbd = dev;
  226. else
  227. kbd = inputInfo.keyboard;
  228. state = &kbd->key->xkbInfo->state;
  229. ctrls = kbd->key->xkbInfo->desc->ctrls;
  230. affected = maps_to_check;
  231. oldState = sli->effectiveState;
  232. sli->autoState &= ~affected;
  233. for (i = 0, bit = 1; (i < XkbNumIndicators) && (affected); i++, bit <<= 1) {
  234. if ((affected & bit) == 0)
  235. continue;
  236. affected &= ~bit;
  237. map = &sli->maps[i];
  238. if ((!(map->flags & XkbIM_NoAutomatic)) &&
  239. ComputeAutoState(map, state, ctrls))
  240. sli->autoState |= bit;
  241. }
  242. sli->effectiveState = (sli->autoState | sli->explicitState);
  243. affected = sli->effectiveState ^ oldState;
  244. if (affected == 0)
  245. return;
  246. if (ed == NULL) {
  247. ed = &my_ed;
  248. memset((char *) ed, 0, sizeof(xkbExtensionDeviceNotify));
  249. }
  250. else if ((ed->reason & XkbXI_IndicatorsMask) &&
  251. ((ed->ledClass != sli->class) || (ed->ledID != sli->id))) {
  252. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  253. }
  254. if ((kbd == dev) && (sli->flags & XkbSLI_IsDefault)) {
  255. if (changes == NULL) {
  256. changes = &my_changes;
  257. memset((char *) changes, 0, sizeof(XkbChangesRec));
  258. }
  259. changes->indicators.state_changes |= affected;
  260. }
  261. ed->reason |= XkbXI_IndicatorStateMask;
  262. ed->ledClass = sli->class;
  263. ed->ledID = sli->id;
  264. ed->ledsDefined = sli->namesPresent | sli->mapsPresent;
  265. ed->ledState = sli->effectiveState;
  266. ed->unsupported = 0;
  267. ed->supported = XkbXI_AllFeaturesMask;
  268. if (changes != &my_changes)
  269. changes = NULL;
  270. if (ed != &my_ed)
  271. ed = NULL;
  272. if (changes || ed)
  273. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  274. return;
  275. }
  276. static void
  277. XkbUpdateAllDeviceIndicators(XkbChangesPtr changes, XkbEventCausePtr cause)
  278. {
  279. DeviceIntPtr edev;
  280. XkbSrvLedInfoPtr sli;
  281. for (edev = inputInfo.devices; edev != NULL; edev = edev->next) {
  282. if (edev->kbdfeed) {
  283. KbdFeedbackPtr kf;
  284. for (kf = edev->kbdfeed; kf != NULL; kf = kf->next) {
  285. if ((kf->xkb_sli == NULL) || (kf->xkb_sli->maps == NULL))
  286. continue;
  287. sli = kf->xkb_sli;
  288. XkbUpdateLedAutoState(edev, sli, sli->mapsPresent, NULL,
  289. changes, cause);
  290. }
  291. }
  292. if (edev->leds) {
  293. LedFeedbackPtr lf;
  294. for (lf = edev->leds; lf != NULL; lf = lf->next) {
  295. if ((lf->xkb_sli == NULL) || (lf->xkb_sli->maps == NULL))
  296. continue;
  297. sli = lf->xkb_sli;
  298. XkbUpdateLedAutoState(edev, sli, sli->mapsPresent, NULL,
  299. changes, cause);
  300. }
  301. }
  302. }
  303. return;
  304. }
  305. /***====================================================================***/
  306. /*
  307. * void
  308. * XkbSetIndicators(dev,affect,values,cause)
  309. *
  310. * Attempts to change the indicators specified in 'affect' to the
  311. * states specified in 'values' for the default keyboard feedback
  312. * on the keyboard specified by 'dev.' Attempts to change indicator
  313. * state might be ignored or have no affect, depending on the XKB
  314. * indicator map for any affected indicators, as described in section
  315. * 9.2 of the XKB protocol specification.
  316. *
  317. * If 'changes' is non-NULL, this function notes any changes to the
  318. * keyboard state, controls, or indicator state that result from this
  319. * attempted change. If 'changes' is NULL, this function generates
  320. * XKB events to report any such changes to interested clients.
  321. *
  322. * If 'cause' is non-NULL, it specifies the reason for the change,
  323. * as reported in some XKB events. If it is NULL, this function
  324. * assumes that the change is the result of a core protocol
  325. * ChangeKeyboardMapping request.
  326. */
  327. void
  328. XkbSetIndicators(DeviceIntPtr dev,
  329. CARD32 affect, CARD32 values, XkbEventCausePtr cause)
  330. {
  331. XkbSrvLedInfoPtr sli;
  332. XkbChangesRec changes;
  333. xkbExtensionDeviceNotify ed;
  334. unsigned side_affected;
  335. memset((char *) &changes, 0, sizeof(XkbChangesRec));
  336. memset((char *) &ed, 0, sizeof(xkbExtensionDeviceNotify));
  337. sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
  338. sli->explicitState &= ~affect;
  339. sli->explicitState |= (affect & values);
  340. XkbApplyLedStateChanges(dev, sli, affect, &ed, &changes, cause);
  341. side_affected = 0;
  342. if (changes.state_changes != 0)
  343. side_affected |=
  344. XkbIndicatorsToUpdate(dev, changes.state_changes, FALSE);
  345. if (changes.ctrls.enabled_ctrls_changes)
  346. side_affected |= sli->usesControls;
  347. if (side_affected) {
  348. XkbUpdateLedAutoState(dev, sli, side_affected, &ed, &changes, cause);
  349. affect |= side_affected;
  350. }
  351. if (changes.state_changes || changes.ctrls.enabled_ctrls_changes)
  352. XkbUpdateAllDeviceIndicators(NULL, cause);
  353. XkbFlushLedEvents(dev, dev, sli, &ed, &changes, cause);
  354. return;
  355. }
  356. /***====================================================================***/
  357. /***====================================================================***/
  358. /*
  359. * void
  360. * XkbUpdateIndicators(dev,update,check_edevs,changes,cause)
  361. *
  362. * Applies the indicator maps for any indicators specified in
  363. * 'update' from the default keyboard feedback on the device
  364. * specified by 'dev.'
  365. *
  366. * If 'changes' is NULL, this function generates and XKB events
  367. * required to report the necessary changes, otherwise it simply
  368. * notes the indicators with changed state.
  369. *
  370. * If 'check_edevs' is TRUE, this function also checks the indicator
  371. * maps for any open extension devices that have them, and updates
  372. * the state of any extension device indicators as necessary.
  373. */
  374. void
  375. XkbUpdateIndicators(DeviceIntPtr dev,
  376. register CARD32 update,
  377. Bool check_edevs,
  378. XkbChangesPtr changes, XkbEventCausePtr cause)
  379. {
  380. XkbSrvLedInfoPtr sli;
  381. sli = XkbFindSrvLedInfo(dev, XkbDfltXIClass, XkbDfltXIId, 0);
  382. XkbUpdateLedAutoState(dev, sli, update, NULL, changes, cause);
  383. if (check_edevs)
  384. XkbUpdateAllDeviceIndicators(changes, cause);
  385. return;
  386. }
  387. /***====================================================================***/
  388. /***====================================================================***/
  389. /*
  390. * void
  391. * XkbCheckIndicatorMaps(dev,sli,which)
  392. *
  393. * Updates the 'indicator accelerators' for the indicators specified
  394. * by 'which' in the feedback specified by 'sli.' The indicator
  395. * accelerators are internal to the server and are used to simplify
  396. * and speed up the process of figuring out which indicators might
  397. * be affected by a particular change in keyboard state or controls.
  398. */
  399. void
  400. XkbCheckIndicatorMaps(DeviceIntPtr dev, XkbSrvLedInfoPtr sli, unsigned which)
  401. {
  402. register unsigned i, bit;
  403. XkbIndicatorMapPtr map;
  404. XkbDescPtr xkb;
  405. if ((sli->flags & XkbSLI_HasOwnState) == 0)
  406. return;
  407. sli->usesBase &= ~which;
  408. sli->usesLatched &= ~which;
  409. sli->usesLocked &= ~which;
  410. sli->usesEffective &= ~which;
  411. sli->usesCompat &= ~which;
  412. sli->usesControls &= ~which;
  413. sli->mapsPresent &= ~which;
  414. xkb = dev->key->xkbInfo->desc;
  415. for (i = 0, bit = 1, map = sli->maps; i < XkbNumIndicators;
  416. i++, bit <<= 1, map++) {
  417. if (which & bit) {
  418. CARD8 what;
  419. if (!map || !XkbIM_InUse(map))
  420. continue;
  421. sli->mapsPresent |= bit;
  422. what = (map->which_mods | map->which_groups);
  423. if (what & XkbIM_UseBase)
  424. sli->usesBase |= bit;
  425. if (what & XkbIM_UseLatched)
  426. sli->usesLatched |= bit;
  427. if (what & XkbIM_UseLocked)
  428. sli->usesLocked |= bit;
  429. if (what & XkbIM_UseEffective)
  430. sli->usesEffective |= bit;
  431. if (what & XkbIM_UseCompat)
  432. sli->usesCompat |= bit;
  433. if (map->ctrls)
  434. sli->usesControls |= bit;
  435. map->mods.mask = map->mods.real_mods;
  436. if (map->mods.vmods != 0) {
  437. map->mods.mask |= XkbMaskForVMask(xkb, map->mods.vmods);
  438. }
  439. }
  440. }
  441. sli->usedComponents = 0;
  442. if (sli->usesBase)
  443. sli->usedComponents |= XkbModifierBaseMask | XkbGroupBaseMask;
  444. if (sli->usesLatched)
  445. sli->usedComponents |= XkbModifierLatchMask | XkbGroupLatchMask;
  446. if (sli->usesLocked)
  447. sli->usedComponents |= XkbModifierLockMask | XkbGroupLockMask;
  448. if (sli->usesEffective)
  449. sli->usedComponents |= XkbModifierStateMask | XkbGroupStateMask;
  450. if (sli->usesCompat)
  451. sli->usedComponents |= XkbCompatStateMask;
  452. return;
  453. }
  454. /***====================================================================***/
  455. /*
  456. * XkbSrvLedInfoPtr
  457. * XkbAllocSrvLedInfo(dev,kf,lf,needed_parts)
  458. *
  459. * Allocates an XkbSrvLedInfoPtr for the feedback specified by either
  460. * 'kf' or 'lf' on the keyboard specified by 'dev.'
  461. *
  462. * If 'needed_parts' is non-zero, this function makes sure that any
  463. * of the parts speicified therein are allocated.
  464. */
  465. XkbSrvLedInfoPtr
  466. XkbAllocSrvLedInfo(DeviceIntPtr dev,
  467. KbdFeedbackPtr kf, LedFeedbackPtr lf, unsigned needed_parts)
  468. {
  469. XkbSrvLedInfoPtr sli;
  470. Bool checkAccel;
  471. Bool checkNames;
  472. sli = NULL;
  473. checkAccel = checkNames = FALSE;
  474. if ((kf != NULL) && (kf->xkb_sli == NULL)) {
  475. kf->xkb_sli = sli = calloc(1, sizeof(XkbSrvLedInfoRec));
  476. if (sli == NULL)
  477. return NULL; /* ALLOCATION ERROR */
  478. if (dev->key && dev->key->xkbInfo)
  479. sli->flags = XkbSLI_HasOwnState;
  480. else
  481. sli->flags = 0;
  482. sli->class = KbdFeedbackClass;
  483. sli->id = kf->ctrl.id;
  484. sli->fb.kf = kf;
  485. sli->autoState = 0;
  486. sli->explicitState = kf->ctrl.leds;
  487. sli->effectiveState = kf->ctrl.leds;
  488. if ((kf == dev->kbdfeed) && (dev->key) && (dev->key->xkbInfo)) {
  489. XkbDescPtr xkb;
  490. xkb = dev->key->xkbInfo->desc;
  491. sli->flags |= XkbSLI_IsDefault;
  492. sli->physIndicators = xkb->indicators->phys_indicators;
  493. sli->names = xkb->names->indicators;
  494. sli->maps = xkb->indicators->maps;
  495. checkNames = checkAccel = TRUE;
  496. }
  497. else {
  498. sli->physIndicators = XkbAllIndicatorsMask;
  499. sli->names = NULL;
  500. sli->maps = NULL;
  501. }
  502. }
  503. else if ((kf != NULL) && ((kf->xkb_sli->flags & XkbSLI_IsDefault) != 0)) {
  504. XkbDescPtr xkb;
  505. xkb = dev->key->xkbInfo->desc;
  506. sli = kf->xkb_sli;
  507. sli->physIndicators = xkb->indicators->phys_indicators;
  508. if (xkb->names->indicators != sli->names) {
  509. checkNames = TRUE;
  510. sli->names = xkb->names->indicators;
  511. }
  512. if (xkb->indicators->maps != sli->maps) {
  513. checkAccel = TRUE;
  514. sli->maps = xkb->indicators->maps;
  515. }
  516. }
  517. else if ((lf != NULL) && (lf->xkb_sli == NULL)) {
  518. lf->xkb_sli = sli = calloc(1, sizeof(XkbSrvLedInfoRec));
  519. if (sli == NULL)
  520. return NULL; /* ALLOCATION ERROR */
  521. if (dev->key && dev->key->xkbInfo)
  522. sli->flags = XkbSLI_HasOwnState;
  523. else
  524. sli->flags = 0;
  525. sli->class = LedFeedbackClass;
  526. sli->id = lf->ctrl.id;
  527. sli->fb.lf = lf;
  528. sli->physIndicators = lf->ctrl.led_mask;
  529. sli->autoState = 0;
  530. sli->explicitState = lf->ctrl.led_values;
  531. sli->effectiveState = lf->ctrl.led_values;
  532. sli->maps = NULL;
  533. sli->names = NULL;
  534. }
  535. else
  536. return NULL;
  537. if ((sli->names == NULL) && (needed_parts & XkbXI_IndicatorNamesMask))
  538. sli->names = calloc(XkbNumIndicators, sizeof(Atom));
  539. if ((sli->maps == NULL) && (needed_parts & XkbXI_IndicatorMapsMask))
  540. sli->maps = calloc(XkbNumIndicators, sizeof(XkbIndicatorMapRec));
  541. if (checkNames) {
  542. register unsigned i, bit;
  543. sli->namesPresent = 0;
  544. for (i = 0, bit = 1; i < XkbNumIndicators; i++, bit <<= 1) {
  545. if (sli->names[i] != None)
  546. sli->namesPresent |= bit;
  547. }
  548. }
  549. if (checkAccel)
  550. XkbCheckIndicatorMaps(dev, sli, XkbAllIndicatorsMask);
  551. return sli;
  552. }
  553. void
  554. XkbFreeSrvLedInfo(XkbSrvLedInfoPtr sli)
  555. {
  556. if ((sli->flags & XkbSLI_IsDefault) == 0) {
  557. free(sli->maps);
  558. free(sli->names);
  559. }
  560. sli->maps = NULL;
  561. sli->names = NULL;
  562. free(sli);
  563. return;
  564. }
  565. /*
  566. * XkbSrvLedInfoPtr
  567. * XkbCopySrvLedInfo(dev,src,kf,lf)
  568. *
  569. * Takes the given XkbSrvLedInfoPtr and duplicates it. A deep copy is made,
  570. * thus the new copy behaves like the original one and can be freed with
  571. * XkbFreeSrvLedInfo.
  572. */
  573. XkbSrvLedInfoPtr
  574. XkbCopySrvLedInfo(DeviceIntPtr from,
  575. XkbSrvLedInfoPtr src, KbdFeedbackPtr kf, LedFeedbackPtr lf)
  576. {
  577. XkbSrvLedInfoPtr sli_new = NULL;
  578. if (!src)
  579. goto finish;
  580. sli_new = calloc(1, sizeof(XkbSrvLedInfoRec));
  581. if (!sli_new)
  582. goto finish;
  583. memcpy(sli_new, src, sizeof(XkbSrvLedInfoRec));
  584. if (sli_new->class == KbdFeedbackClass)
  585. sli_new->fb.kf = kf;
  586. else
  587. sli_new->fb.lf = lf;
  588. if (!(sli_new->flags & XkbSLI_IsDefault)) {
  589. sli_new->names = calloc(XkbNumIndicators, sizeof(Atom));
  590. sli_new->maps = calloc(XkbNumIndicators, sizeof(XkbIndicatorMapRec));
  591. } /* else sli_new->names/maps is pointing to
  592. dev->key->xkbInfo->desc->names->indicators;
  593. dev->key->xkbInfo->desc->names->indicators; */
  594. finish:
  595. return sli_new;
  596. }
  597. /***====================================================================***/
  598. /*
  599. * XkbSrvLedInfoPtr
  600. * XkbFindSrvLedInfo(dev,class,id,needed_parts)
  601. *
  602. * Finds the XkbSrvLedInfoPtr for the specified 'class' and 'id'
  603. * on the device specified by 'dev.' If the class and id specify
  604. * a valid device feedback, this function returns the existing
  605. * feedback or allocates a new one.
  606. *
  607. */
  608. XkbSrvLedInfoPtr
  609. XkbFindSrvLedInfo(DeviceIntPtr dev,
  610. unsigned class, unsigned id, unsigned needed_parts)
  611. {
  612. XkbSrvLedInfoPtr sli;
  613. /* optimization to check for most common case */
  614. if (((class == XkbDfltXIClass) && (id == XkbDfltXIId)) && (dev->kbdfeed)) {
  615. if (dev->kbdfeed->xkb_sli == NULL) {
  616. dev->kbdfeed->xkb_sli =
  617. XkbAllocSrvLedInfo(dev, dev->kbdfeed, NULL, needed_parts);
  618. }
  619. return dev->kbdfeed->xkb_sli;
  620. }
  621. sli = NULL;
  622. if (class == XkbDfltXIClass) {
  623. if (dev->kbdfeed)
  624. class = KbdFeedbackClass;
  625. else if (dev->leds)
  626. class = LedFeedbackClass;
  627. else
  628. return NULL;
  629. }
  630. if (class == KbdFeedbackClass) {
  631. KbdFeedbackPtr kf;
  632. for (kf = dev->kbdfeed; kf != NULL; kf = kf->next) {
  633. if ((id == XkbDfltXIId) || (id == kf->ctrl.id)) {
  634. if (kf->xkb_sli == NULL)
  635. kf->xkb_sli =
  636. XkbAllocSrvLedInfo(dev, kf, NULL, needed_parts);
  637. sli = kf->xkb_sli;
  638. break;
  639. }
  640. }
  641. }
  642. else if (class == LedFeedbackClass) {
  643. LedFeedbackPtr lf;
  644. for (lf = dev->leds; lf != NULL; lf = lf->next) {
  645. if ((id == XkbDfltXIId) || (id == lf->ctrl.id)) {
  646. if (lf->xkb_sli == NULL)
  647. lf->xkb_sli =
  648. XkbAllocSrvLedInfo(dev, NULL, lf, needed_parts);
  649. sli = lf->xkb_sli;
  650. break;
  651. }
  652. }
  653. }
  654. if (sli) {
  655. if ((sli->names == NULL) && (needed_parts & XkbXI_IndicatorNamesMask))
  656. sli->names = calloc(XkbNumIndicators, sizeof(Atom));
  657. if ((sli->maps == NULL) && (needed_parts & XkbXI_IndicatorMapsMask))
  658. sli->maps = calloc(XkbNumIndicators, sizeof(XkbIndicatorMapRec));
  659. }
  660. return sli;
  661. }
  662. /***====================================================================***/
  663. void
  664. XkbFlushLedEvents(DeviceIntPtr dev,
  665. DeviceIntPtr kbd,
  666. XkbSrvLedInfoPtr sli,
  667. xkbExtensionDeviceNotify * ed,
  668. XkbChangesPtr changes, XkbEventCausePtr cause)
  669. {
  670. if (changes) {
  671. if (changes->indicators.state_changes)
  672. XkbDDXUpdateDeviceIndicators(dev, sli, sli->effectiveState);
  673. XkbSendNotification(kbd, changes, cause);
  674. memset((char *) changes, 0, sizeof(XkbChangesRec));
  675. if (XkbAX_NeedFeedback
  676. (kbd->key->xkbInfo->desc->ctrls, XkbAX_IndicatorFBMask)) {
  677. if (sli->effectiveState)
  678. /* it appears that the which parameter is not used */
  679. XkbDDXAccessXBeep(dev, _BEEP_LED_ON, XkbAccessXFeedbackMask);
  680. else
  681. XkbDDXAccessXBeep(dev, _BEEP_LED_OFF, XkbAccessXFeedbackMask);
  682. }
  683. }
  684. if (ed) {
  685. if (ed->reason) {
  686. if ((dev != kbd) && (ed->reason & XkbXI_IndicatorStateMask))
  687. XkbDDXUpdateDeviceIndicators(dev, sli, sli->effectiveState);
  688. XkbSendExtensionDeviceNotify(dev, cause->client, ed);
  689. }
  690. memset((char *) ed, 0, sizeof(XkbExtensionDeviceNotify));
  691. }
  692. return;
  693. }
  694. /***====================================================================***/
  695. void
  696. XkbApplyLedNameChanges(DeviceIntPtr dev,
  697. XkbSrvLedInfoPtr sli,
  698. unsigned changed_names,
  699. xkbExtensionDeviceNotify * ed,
  700. XkbChangesPtr changes, XkbEventCausePtr cause)
  701. {
  702. DeviceIntPtr kbd;
  703. XkbChangesRec my_changes;
  704. xkbExtensionDeviceNotify my_ed;
  705. if (changed_names == 0)
  706. return;
  707. if (dev->key && dev->key->xkbInfo)
  708. kbd = dev;
  709. else
  710. kbd = inputInfo.keyboard;
  711. if (ed == NULL) {
  712. ed = &my_ed;
  713. memset((char *) ed, 0, sizeof(xkbExtensionDeviceNotify));
  714. }
  715. else if ((ed->reason & XkbXI_IndicatorsMask) &&
  716. ((ed->ledClass != sli->class) || (ed->ledID != sli->id))) {
  717. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  718. }
  719. if ((kbd == dev) && (sli->flags & XkbSLI_IsDefault)) {
  720. if (changes == NULL) {
  721. changes = &my_changes;
  722. memset((char *) changes, 0, sizeof(XkbChangesRec));
  723. }
  724. changes->names.changed |= XkbIndicatorNamesMask;
  725. changes->names.changed_indicators |= changed_names;
  726. }
  727. ed->reason |= XkbXI_IndicatorNamesMask;
  728. ed->ledClass = sli->class;
  729. ed->ledID = sli->id;
  730. ed->ledsDefined = sli->namesPresent | sli->mapsPresent;
  731. ed->ledState = sli->effectiveState;
  732. ed->unsupported = 0;
  733. ed->supported = XkbXI_AllFeaturesMask;
  734. if (changes != &my_changes)
  735. changes = NULL;
  736. if (ed != &my_ed)
  737. ed = NULL;
  738. if (changes || ed)
  739. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  740. return;
  741. }
  742. /***====================================================================***/
  743. /*
  744. * void
  745. * XkbApplyLedMapChanges(dev,sli,changed_maps,changes,cause)
  746. *
  747. * Handles all of the secondary effects of the changes to the
  748. * feedback specified by 'sli' on the device specified by 'dev.'
  749. *
  750. * If 'changed_maps' specifies any indicators, this function generates
  751. * XkbExtensionDeviceNotify events and possibly IndicatorMapNotify
  752. * events to report the changes, and recalculates the effective
  753. * state of each indicator with a changed map. If any indicators
  754. * change state, the server generates XkbExtensionDeviceNotify and
  755. * XkbIndicatorStateNotify events as appropriate.
  756. *
  757. * If 'changes' is non-NULL, this function updates it to reflect
  758. * any changes to the keyboard state or controls or to the 'core'
  759. * indicator names, maps, or state. If 'changes' is NULL, this
  760. * function generates XKB events as needed to report the changes.
  761. * If 'dev' is not a keyboard device, any changes are reported
  762. * for the core keyboard.
  763. *
  764. * The 'cause' specifies the reason for the event (key event or
  765. * request) for the change, as reported in some XKB events.
  766. */
  767. void
  768. XkbApplyLedMapChanges(DeviceIntPtr dev,
  769. XkbSrvLedInfoPtr sli,
  770. unsigned changed_maps,
  771. xkbExtensionDeviceNotify * ed,
  772. XkbChangesPtr changes, XkbEventCausePtr cause)
  773. {
  774. DeviceIntPtr kbd;
  775. XkbChangesRec my_changes;
  776. xkbExtensionDeviceNotify my_ed;
  777. if (changed_maps == 0)
  778. return;
  779. if (dev->key && dev->key->xkbInfo)
  780. kbd = dev;
  781. else
  782. kbd = inputInfo.keyboard;
  783. if (ed == NULL) {
  784. ed = &my_ed;
  785. memset((char *) ed, 0, sizeof(xkbExtensionDeviceNotify));
  786. }
  787. else if ((ed->reason & XkbXI_IndicatorsMask) &&
  788. ((ed->ledClass != sli->class) || (ed->ledID != sli->id))) {
  789. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  790. }
  791. if ((kbd == dev) && (sli->flags & XkbSLI_IsDefault)) {
  792. if (changes == NULL) {
  793. changes = &my_changes;
  794. memset((char *) changes, 0, sizeof(XkbChangesRec));
  795. }
  796. changes->indicators.map_changes |= changed_maps;
  797. }
  798. XkbCheckIndicatorMaps(dev, sli, changed_maps);
  799. ed->reason |= XkbXI_IndicatorMapsMask;
  800. ed->ledClass = sli->class;
  801. ed->ledID = sli->id;
  802. ed->ledsDefined = sli->namesPresent | sli->mapsPresent;
  803. ed->ledState = sli->effectiveState;
  804. ed->unsupported = 0;
  805. ed->supported = XkbXI_AllFeaturesMask;
  806. XkbUpdateLedAutoState(dev, sli, changed_maps, ed, changes, cause);
  807. if (changes != &my_changes)
  808. changes = NULL;
  809. if (ed != &my_ed)
  810. ed = NULL;
  811. if (changes || ed)
  812. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  813. return;
  814. }
  815. /***====================================================================***/
  816. void
  817. XkbApplyLedStateChanges(DeviceIntPtr dev,
  818. XkbSrvLedInfoPtr sli,
  819. unsigned changed_leds,
  820. xkbExtensionDeviceNotify * ed,
  821. XkbChangesPtr changes, XkbEventCausePtr cause)
  822. {
  823. XkbSrvInfoPtr xkbi;
  824. DeviceIntPtr kbd;
  825. XkbChangesRec my_changes;
  826. xkbExtensionDeviceNotify my_ed;
  827. register unsigned i, bit, affected;
  828. XkbIndicatorMapPtr map;
  829. unsigned oldState;
  830. Bool kb_changed;
  831. if (changed_leds == 0)
  832. return;
  833. if (dev->key && dev->key->xkbInfo)
  834. kbd = dev;
  835. else
  836. kbd = inputInfo.keyboard;
  837. xkbi = kbd->key->xkbInfo;
  838. if (changes == NULL) {
  839. changes = &my_changes;
  840. memset((char *) changes, 0, sizeof(XkbChangesRec));
  841. }
  842. kb_changed = FALSE;
  843. affected = changed_leds;
  844. oldState = sli->effectiveState;
  845. for (i = 0, bit = 1; (i < XkbNumIndicators) && (affected); i++, bit <<= 1) {
  846. if ((affected & bit) == 0)
  847. continue;
  848. affected &= ~bit;
  849. map = &sli->maps[i];
  850. if (map->flags & XkbIM_NoExplicit) {
  851. sli->explicitState &= ~bit;
  852. continue;
  853. }
  854. if (map->flags & XkbIM_LEDDrivesKB) {
  855. Bool on = ((sli->explicitState & bit) != 0);
  856. if (XkbApplyLEDChangeToKeyboard(xkbi, map, on, changes))
  857. kb_changed = TRUE;
  858. }
  859. }
  860. sli->effectiveState = (sli->autoState | sli->explicitState);
  861. affected = sli->effectiveState ^ oldState;
  862. if (ed == NULL) {
  863. ed = &my_ed;
  864. memset((char *) ed, 0, sizeof(xkbExtensionDeviceNotify));
  865. }
  866. else if (affected && (ed->reason & XkbXI_IndicatorsMask) &&
  867. ((ed->ledClass != sli->class) || (ed->ledID != sli->id))) {
  868. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  869. }
  870. if ((kbd == dev) && (sli->flags & XkbSLI_IsDefault))
  871. changes->indicators.state_changes |= affected;
  872. if (affected) {
  873. ed->reason |= XkbXI_IndicatorStateMask;
  874. ed->ledClass = sli->class;
  875. ed->ledID = sli->id;
  876. ed->ledsDefined = sli->namesPresent | sli->mapsPresent;
  877. ed->ledState = sli->effectiveState;
  878. ed->unsupported = 0;
  879. ed->supported = XkbXI_AllFeaturesMask;
  880. }
  881. if (kb_changed) {
  882. XkbComputeDerivedState(kbd->key->xkbInfo);
  883. XkbUpdateLedAutoState(dev, sli, sli->mapsPresent, ed, changes, cause);
  884. }
  885. if (changes != &my_changes)
  886. changes = NULL;
  887. if (ed != &my_ed)
  888. ed = NULL;
  889. if (changes || ed)
  890. XkbFlushLedEvents(dev, kbd, sli, ed, changes, cause);
  891. if (kb_changed)
  892. XkbUpdateAllDeviceIndicators(NULL, cause);
  893. return;
  894. }