XKBMisc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. #elif defined(HAVE_CONFIG_H)
  26. #include <config.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <X11/X.h>
  30. #include <X11/Xproto.h>
  31. #include "misc.h"
  32. #include "inputstr.h"
  33. #include <X11/keysym.h>
  34. #define XKBSRV_NEED_FILE_FUNCS
  35. #include <xkbsrv.h>
  36. /***====================================================================***/
  37. #define CORE_SYM(i) (i<map_width?core_syms[i]:NoSymbol)
  38. #define XKB_OFFSET(g,l) (((g)*groupsWidth)+(l))
  39. int
  40. XkbKeyTypesForCoreSymbols(XkbDescPtr xkb,
  41. int map_width,
  42. KeySym * core_syms,
  43. unsigned int protected,
  44. int *types_inout, KeySym * xkb_syms_rtrn)
  45. {
  46. register int i;
  47. unsigned int empty;
  48. int nSyms[XkbNumKbdGroups];
  49. int nGroups, tmp, groupsWidth;
  50. BOOL replicated = FALSE;
  51. /* Section 12.2 of the protocol describes this process in more detail */
  52. /* Step 1: find the # of symbols in the core mapping per group */
  53. groupsWidth = 2;
  54. for (i = 0; i < XkbNumKbdGroups; i++) {
  55. if ((protected & (1 << i)) && (types_inout[i] < xkb->map->num_types)) {
  56. nSyms[i] = xkb->map->types[types_inout[i]].num_levels;
  57. if (nSyms[i] > groupsWidth)
  58. groupsWidth = nSyms[i];
  59. }
  60. else {
  61. types_inout[i] = XkbTwoLevelIndex; /* don't really know, yet */
  62. nSyms[i] = 2;
  63. }
  64. }
  65. if (nSyms[XkbGroup1Index] < 2)
  66. nSyms[XkbGroup1Index] = 2;
  67. if (nSyms[XkbGroup2Index] < 2)
  68. nSyms[XkbGroup2Index] = 2;
  69. /* Step 2: Copy the symbols from the core ordering to XKB ordering */
  70. /* symbols in the core are in the order: */
  71. /* G1L1 G1L2 G2L1 G2L2 [G1L[3-n]] [G2L[3-n]] [G3L*] [G3L*] */
  72. xkb_syms_rtrn[XKB_OFFSET(XkbGroup1Index, 0)] = CORE_SYM(0);
  73. xkb_syms_rtrn[XKB_OFFSET(XkbGroup1Index, 1)] = CORE_SYM(1);
  74. for (i = 2; i < nSyms[XkbGroup1Index]; i++) {
  75. xkb_syms_rtrn[XKB_OFFSET(XkbGroup1Index, i)] = CORE_SYM(2 + i);
  76. }
  77. xkb_syms_rtrn[XKB_OFFSET(XkbGroup2Index, 0)] = CORE_SYM(2);
  78. xkb_syms_rtrn[XKB_OFFSET(XkbGroup2Index, 1)] = CORE_SYM(3);
  79. tmp = 2 + (nSyms[XkbGroup1Index] - 2); /* offset to extra group2 syms */
  80. for (i = 2; i < nSyms[XkbGroup2Index]; i++) {
  81. xkb_syms_rtrn[XKB_OFFSET(XkbGroup2Index, i)] = CORE_SYM(tmp + i);
  82. }
  83. /* Special case: if only the first group is explicit, and the symbols
  84. * replicate across all groups, then we have a Section 12.4 replication */
  85. if ((protected & ~XkbExplicitKeyType1Mask) == 0) {
  86. int j, width = nSyms[XkbGroup1Index];
  87. replicated = TRUE;
  88. /* Check ABAB in ABABCDECDEABCDE */
  89. if ((width > 0 && CORE_SYM(0) != CORE_SYM(2)) ||
  90. (width > 1 && CORE_SYM(1) != CORE_SYM(3)))
  91. replicated = FALSE;
  92. /* Check CDECDE in ABABCDECDEABCDE */
  93. for (i = 2; i < width && replicated; i++) {
  94. if (CORE_SYM(2 + i) != CORE_SYM(i + width))
  95. replicated = FALSE;
  96. }
  97. /* Check ABCDE in ABABCDECDEABCDE */
  98. for (j = 2; replicated &&
  99. j < XkbNumKbdGroups && map_width >= width * (j + 1); j++) {
  100. for (i = 0; i < width && replicated; i++) {
  101. if (CORE_SYM(((i < 2) ? i : 2 + i)) != CORE_SYM(i + width * j))
  102. replicated = FALSE;
  103. }
  104. }
  105. }
  106. if (replicated) {
  107. nSyms[XkbGroup2Index] = 0;
  108. nSyms[XkbGroup3Index] = 0;
  109. nSyms[XkbGroup4Index] = 0;
  110. nGroups = 1;
  111. }
  112. else {
  113. tmp = nSyms[XkbGroup1Index] + nSyms[XkbGroup2Index];
  114. if ((tmp >= map_width) &&
  115. ((protected & (XkbExplicitKeyType3Mask | XkbExplicitKeyType4Mask))
  116. == 0)) {
  117. nSyms[XkbGroup3Index] = 0;
  118. nSyms[XkbGroup4Index] = 0;
  119. nGroups = 2;
  120. }
  121. else {
  122. nGroups = 3;
  123. for (i = 0; i < nSyms[XkbGroup3Index]; i++, tmp++) {
  124. xkb_syms_rtrn[XKB_OFFSET(XkbGroup3Index, i)] = CORE_SYM(tmp);
  125. }
  126. if ((tmp < map_width) || (protected & XkbExplicitKeyType4Mask)) {
  127. nGroups = 4;
  128. for (i = 0; i < nSyms[XkbGroup4Index]; i++, tmp++) {
  129. xkb_syms_rtrn[XKB_OFFSET(XkbGroup4Index, i)] =
  130. CORE_SYM(tmp);
  131. }
  132. }
  133. else {
  134. nSyms[XkbGroup4Index] = 0;
  135. }
  136. }
  137. }
  138. /* steps 3&4: alphanumeric expansion, assign canonical types */
  139. empty = 0;
  140. for (i = 0; i < nGroups; i++) {
  141. KeySym *syms;
  142. syms = &xkb_syms_rtrn[XKB_OFFSET(i, 0)];
  143. if ((nSyms[i] > 1) && (syms[1] == NoSymbol) && (syms[0] != NoSymbol)) {
  144. KeySym upper, lower;
  145. XkbConvertCase(syms[0], &lower, &upper);
  146. if (upper != lower) {
  147. xkb_syms_rtrn[XKB_OFFSET(i, 0)] = lower;
  148. xkb_syms_rtrn[XKB_OFFSET(i, 1)] = upper;
  149. if ((protected & (1 << i)) == 0)
  150. types_inout[i] = XkbAlphabeticIndex;
  151. }
  152. else if ((protected & (1 << i)) == 0) {
  153. types_inout[i] = XkbOneLevelIndex;
  154. /* nSyms[i]= 1; */
  155. }
  156. }
  157. if (((protected & (1 << i)) == 0) &&
  158. (types_inout[i] == XkbTwoLevelIndex)) {
  159. if (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1]))
  160. types_inout[i] = XkbKeypadIndex;
  161. else {
  162. KeySym upper, lower;
  163. XkbConvertCase(syms[0], &lower, &upper);
  164. if ((syms[0] == lower) && (syms[1] == upper))
  165. types_inout[i] = XkbAlphabeticIndex;
  166. }
  167. }
  168. if (syms[0] == NoSymbol) {
  169. register int n;
  170. Bool found;
  171. for (n = 1, found = FALSE; (!found) && (n < nSyms[i]); n++) {
  172. found = (syms[n] != NoSymbol);
  173. }
  174. if (!found)
  175. empty |= (1 << i);
  176. }
  177. }
  178. /* step 5: squoosh out empty groups */
  179. if (empty) {
  180. for (i = nGroups - 1; i >= 0; i--) {
  181. if (((empty & (1 << i)) == 0) || (protected & (1 << i)))
  182. break;
  183. nGroups--;
  184. }
  185. }
  186. if (nGroups < 1)
  187. return 0;
  188. /* step 6: replicate group 1 into group two, if necessary */
  189. if ((nGroups > 1) &&
  190. ((empty & (XkbGroup1Mask | XkbGroup2Mask)) == XkbGroup2Mask)) {
  191. if ((protected & (XkbExplicitKeyType1Mask | XkbExplicitKeyType2Mask)) ==
  192. 0) {
  193. nSyms[XkbGroup2Index] = nSyms[XkbGroup1Index];
  194. types_inout[XkbGroup2Index] = types_inout[XkbGroup1Index];
  195. memcpy((char *) &xkb_syms_rtrn[2], (char *) xkb_syms_rtrn,
  196. 2 * sizeof(KeySym));
  197. }
  198. else if (types_inout[XkbGroup1Index] == types_inout[XkbGroup2Index]) {
  199. memcpy((char *) &xkb_syms_rtrn[nSyms[XkbGroup1Index]],
  200. (char *) xkb_syms_rtrn,
  201. nSyms[XkbGroup1Index] * sizeof(KeySym));
  202. }
  203. }
  204. /* step 7: check for all groups identical or all width 1
  205. *
  206. * Special feature: if group 1 has an explicit type and all other groups
  207. * have canonical types with same symbols, we assume it's info lost from
  208. * the core replication.
  209. */
  210. if (nGroups > 1) {
  211. Bool sameType, allOneLevel, canonical = TRUE;
  212. allOneLevel = (xkb->map->types[types_inout[0]].num_levels == 1);
  213. for (i = 1, sameType = TRUE; (allOneLevel || sameType) && (i < nGroups);
  214. i++) {
  215. sameType = (sameType &&
  216. (types_inout[i] == types_inout[XkbGroup1Index]));
  217. if (allOneLevel)
  218. allOneLevel = (xkb->map->types[types_inout[i]].num_levels == 1);
  219. if (types_inout[i] > XkbLastRequiredType)
  220. canonical = FALSE;
  221. }
  222. if (((sameType) || canonical) &&
  223. (!(protected &
  224. (XkbExplicitKeyTypesMask & ~XkbExplicitKeyType1Mask)))) {
  225. register int s;
  226. Bool identical;
  227. for (i = 1, identical = TRUE; identical && (i < nGroups); i++) {
  228. KeySym *syms;
  229. if (nSyms[i] != nSyms[XkbGroup1Index])
  230. identical = FALSE;
  231. syms = &xkb_syms_rtrn[XKB_OFFSET(i, 0)];
  232. for (s = 0; identical && (s < nSyms[i]); s++) {
  233. if (syms[s] != xkb_syms_rtrn[s])
  234. identical = FALSE;
  235. }
  236. }
  237. if (identical)
  238. nGroups = 1;
  239. }
  240. if (allOneLevel && (nGroups > 1)) {
  241. KeySym *syms;
  242. syms = &xkb_syms_rtrn[nSyms[XkbGroup1Index]];
  243. nSyms[XkbGroup1Index] = 1;
  244. for (i = 1; i < nGroups; i++) {
  245. xkb_syms_rtrn[i] = syms[0];
  246. syms += nSyms[i];
  247. nSyms[i] = 1;
  248. }
  249. }
  250. }
  251. return nGroups;
  252. }
  253. static XkbSymInterpretPtr
  254. _XkbFindMatchingInterp(XkbDescPtr xkb,
  255. KeySym sym, unsigned int real_mods, unsigned int level)
  256. {
  257. register unsigned i;
  258. XkbSymInterpretPtr interp, rtrn;
  259. CARD8 mods;
  260. rtrn = NULL;
  261. interp = xkb->compat->sym_interpret;
  262. for (i = 0; i < xkb->compat->num_si; i++, interp++) {
  263. if ((interp->sym == NoSymbol) || (sym == interp->sym)) {
  264. int match;
  265. if ((level == 0) || ((interp->match & XkbSI_LevelOneOnly) == 0))
  266. mods = real_mods;
  267. else
  268. mods = 0;
  269. switch (interp->match & XkbSI_OpMask) {
  270. case XkbSI_NoneOf:
  271. match = ((interp->mods & mods) == 0);
  272. break;
  273. case XkbSI_AnyOfOrNone:
  274. match = ((mods == 0) || ((interp->mods & mods) != 0));
  275. break;
  276. case XkbSI_AnyOf:
  277. match = ((interp->mods & mods) != 0);
  278. break;
  279. case XkbSI_AllOf:
  280. match = ((interp->mods & mods) == interp->mods);
  281. break;
  282. case XkbSI_Exactly:
  283. match = (interp->mods == mods);
  284. break;
  285. default:
  286. match = 0;
  287. break;
  288. }
  289. if (match) {
  290. if (interp->sym != NoSymbol) {
  291. return interp;
  292. }
  293. else if (rtrn == NULL) {
  294. rtrn = interp;
  295. }
  296. }
  297. }
  298. }
  299. return rtrn;
  300. }
  301. static void
  302. _XkbAddKeyChange(KeyCode *pFirst, unsigned char *pNum, KeyCode newKey)
  303. {
  304. KeyCode last;
  305. last = (*pFirst) + (*pNum);
  306. if (newKey < *pFirst) {
  307. *pFirst = newKey;
  308. *pNum = (last - newKey) + 1;
  309. }
  310. else if (newKey > last) {
  311. *pNum = (last - *pFirst) + 1;
  312. }
  313. return;
  314. }
  315. static void
  316. _XkbSetActionKeyMods(XkbDescPtr xkb, XkbAction *act, unsigned mods)
  317. {
  318. unsigned tmp;
  319. switch (act->type) {
  320. case XkbSA_SetMods:
  321. case XkbSA_LatchMods:
  322. case XkbSA_LockMods:
  323. if (act->mods.flags & XkbSA_UseModMapMods)
  324. act->mods.real_mods = act->mods.mask = mods;
  325. if ((tmp = XkbModActionVMods(&act->mods)) != 0) {
  326. XkbVirtualModsToReal(xkb, tmp, &tmp);
  327. act->mods.mask |= tmp;
  328. }
  329. break;
  330. case XkbSA_ISOLock:
  331. if (act->iso.flags & XkbSA_UseModMapMods)
  332. act->iso.real_mods = act->iso.mask = mods;
  333. if ((tmp = XkbModActionVMods(&act->iso)) != 0) {
  334. XkbVirtualModsToReal(xkb, tmp, &tmp);
  335. act->iso.mask |= tmp;
  336. }
  337. break;
  338. }
  339. return;
  340. }
  341. #define IBUF_SIZE 8
  342. Bool
  343. XkbApplyCompatMapToKey(XkbDescPtr xkb, KeyCode key, XkbChangesPtr changes)
  344. {
  345. KeySym *syms;
  346. unsigned char explicit, mods;
  347. XkbSymInterpretPtr *interps, ibuf[IBUF_SIZE];
  348. int n, nSyms, found;
  349. unsigned changed, tmp;
  350. if ((!xkb) || (!xkb->map) || (!xkb->map->key_sym_map) ||
  351. (!xkb->compat) || (!xkb->compat->sym_interpret) ||
  352. (key < xkb->min_key_code) || (key > xkb->max_key_code)) {
  353. return FALSE;
  354. }
  355. if (((!xkb->server) || (!xkb->server->key_acts)) &&
  356. (XkbAllocServerMap(xkb, XkbAllServerInfoMask, 0) != Success)) {
  357. return FALSE;
  358. }
  359. changed = 0; /* keeps track of what has changed in _this_ call */
  360. explicit = xkb->server->explicit[key];
  361. if (explicit & XkbExplicitInterpretMask) /* nothing to do */
  362. return TRUE;
  363. mods = (xkb->map->modmap ? xkb->map->modmap[key] : 0);
  364. nSyms = XkbKeyNumSyms(xkb, key);
  365. syms = XkbKeySymsPtr(xkb, key);
  366. if (nSyms > IBUF_SIZE) {
  367. interps = calloc(nSyms, sizeof(XkbSymInterpretPtr));
  368. if (interps == NULL) {
  369. interps = ibuf;
  370. nSyms = IBUF_SIZE;
  371. }
  372. }
  373. else {
  374. interps = ibuf;
  375. }
  376. found = 0;
  377. for (n = 0; n < nSyms; n++) {
  378. unsigned level = (n % XkbKeyGroupsWidth(xkb, key));
  379. interps[n] = NULL;
  380. if (syms[n] != NoSymbol) {
  381. interps[n] = _XkbFindMatchingInterp(xkb, syms[n], mods, level);
  382. if (interps[n] && interps[n]->act.type != XkbSA_NoAction)
  383. found++;
  384. else
  385. interps[n] = NULL;
  386. }
  387. }
  388. /* 1/28/96 (ef) -- XXX! WORKING HERE */
  389. if (!found) {
  390. if (xkb->server->key_acts[key] != 0) {
  391. xkb->server->key_acts[key] = 0;
  392. changed |= XkbKeyActionsMask;
  393. }
  394. }
  395. else {
  396. XkbAction *pActs;
  397. unsigned int new_vmodmask;
  398. changed |= XkbKeyActionsMask;
  399. pActs = XkbResizeKeyActions(xkb, key, nSyms);
  400. if (!pActs) {
  401. if (nSyms > IBUF_SIZE)
  402. free(interps);
  403. return FALSE;
  404. }
  405. new_vmodmask = 0;
  406. for (n = 0; n < nSyms; n++) {
  407. if (interps[n]) {
  408. unsigned effMods;
  409. pActs[n] = *((XkbAction *) &interps[n]->act);
  410. if ((n == 0) || ((interps[n]->match & XkbSI_LevelOneOnly) == 0)) {
  411. effMods = mods;
  412. if (interps[n]->virtual_mod != XkbNoModifier)
  413. new_vmodmask |= (1 << interps[n]->virtual_mod);
  414. }
  415. else
  416. effMods = 0;
  417. _XkbSetActionKeyMods(xkb, &pActs[n], effMods);
  418. }
  419. else
  420. pActs[n].type = XkbSA_NoAction;
  421. }
  422. if (((explicit & XkbExplicitVModMapMask) == 0) &&
  423. (xkb->server->vmodmap[key] != new_vmodmask)) {
  424. changed |= XkbVirtualModMapMask;
  425. xkb->server->vmodmap[key] = new_vmodmask;
  426. }
  427. if (interps[0]) {
  428. if ((interps[0]->flags & XkbSI_LockingKey) &&
  429. ((explicit & XkbExplicitBehaviorMask) == 0)) {
  430. xkb->server->behaviors[key].type = XkbKB_Lock;
  431. changed |= XkbKeyBehaviorsMask;
  432. }
  433. if (((explicit & XkbExplicitAutoRepeatMask) == 0) && (xkb->ctrls)) {
  434. CARD8 old;
  435. old = BitIsOn(xkb->ctrls->per_key_repeat, key);
  436. if (interps[0]->flags & XkbSI_AutoRepeat)
  437. SetBit(xkb->ctrls->per_key_repeat, key);
  438. else
  439. ClearBit(xkb->ctrls->per_key_repeat, key);
  440. if (changes && old != BitIsOn(xkb->ctrls->per_key_repeat, key))
  441. changes->ctrls.changed_ctrls |= XkbPerKeyRepeatMask;
  442. }
  443. }
  444. }
  445. if ((!found) || (interps[0] == NULL)) {
  446. if (((explicit & XkbExplicitAutoRepeatMask) == 0) && (xkb->ctrls)) {
  447. CARD8 old;
  448. old = BitIsOn(xkb->ctrls->per_key_repeat, key);
  449. SetBit(xkb->ctrls->per_key_repeat, key);
  450. if (changes && (old != BitIsOn(xkb->ctrls->per_key_repeat, key)))
  451. changes->ctrls.changed_ctrls |= XkbPerKeyRepeatMask;
  452. }
  453. if (((explicit & XkbExplicitBehaviorMask) == 0) &&
  454. (xkb->server->behaviors[key].type == XkbKB_Lock)) {
  455. xkb->server->behaviors[key].type = XkbKB_Default;
  456. changed |= XkbKeyBehaviorsMask;
  457. }
  458. }
  459. if (changes) {
  460. XkbMapChangesPtr mc;
  461. mc = &changes->map;
  462. tmp = (changed & mc->changed);
  463. if (tmp & XkbKeyActionsMask)
  464. _XkbAddKeyChange(&mc->first_key_act, &mc->num_key_acts, key);
  465. else if (changed & XkbKeyActionsMask) {
  466. mc->changed |= XkbKeyActionsMask;
  467. mc->first_key_act = key;
  468. mc->num_key_acts = 1;
  469. }
  470. if (tmp & XkbKeyBehaviorsMask) {
  471. _XkbAddKeyChange(&mc->first_key_behavior, &mc->num_key_behaviors,
  472. key);
  473. }
  474. else if (changed & XkbKeyBehaviorsMask) {
  475. mc->changed |= XkbKeyBehaviorsMask;
  476. mc->first_key_behavior = key;
  477. mc->num_key_behaviors = 1;
  478. }
  479. if (tmp & XkbVirtualModMapMask)
  480. _XkbAddKeyChange(&mc->first_vmodmap_key, &mc->num_vmodmap_keys,
  481. key);
  482. else if (changed & XkbVirtualModMapMask) {
  483. mc->changed |= XkbVirtualModMapMask;
  484. mc->first_vmodmap_key = key;
  485. mc->num_vmodmap_keys = 1;
  486. }
  487. mc->changed |= changed;
  488. }
  489. if (interps != ibuf)
  490. free(interps);
  491. return TRUE;
  492. }
  493. Status
  494. XkbChangeTypesOfKey(XkbDescPtr xkb,
  495. int key,
  496. int nGroups,
  497. unsigned groups, int *newTypesIn, XkbMapChangesPtr changes)
  498. {
  499. XkbKeyTypePtr pOldType, pNewType;
  500. register int i;
  501. int width, nOldGroups, oldWidth, newTypes[XkbNumKbdGroups];
  502. if ((!xkb) || (!XkbKeycodeInRange(xkb, key)) || (!xkb->map) ||
  503. (!xkb->map->types) || (!newTypesIn) ||
  504. ((groups & XkbAllGroupsMask) == 0) || (nGroups > XkbNumKbdGroups)) {
  505. return BadMatch;
  506. }
  507. if (nGroups == 0) {
  508. for (i = 0; i < XkbNumKbdGroups; i++) {
  509. xkb->map->key_sym_map[key].kt_index[i] = XkbOneLevelIndex;
  510. }
  511. i = xkb->map->key_sym_map[key].group_info;
  512. i = XkbSetNumGroups(i, 0);
  513. xkb->map->key_sym_map[key].group_info = i;
  514. XkbResizeKeySyms(xkb, key, 0);
  515. return Success;
  516. }
  517. nOldGroups = XkbKeyNumGroups(xkb, key);
  518. oldWidth = XkbKeyGroupsWidth(xkb, key);
  519. for (width = i = 0; i < nGroups; i++) {
  520. if (groups & (1 << i))
  521. newTypes[i] = newTypesIn[i];
  522. else if (i < nOldGroups)
  523. newTypes[i] = XkbKeyKeyTypeIndex(xkb, key, i);
  524. else if (nOldGroups > 0)
  525. newTypes[i] = XkbKeyKeyTypeIndex(xkb, key, XkbGroup1Index);
  526. else
  527. newTypes[i] = XkbTwoLevelIndex;
  528. if (newTypes[i] > xkb->map->num_types)
  529. return BadMatch;
  530. pNewType = &xkb->map->types[newTypes[i]];
  531. if (pNewType->num_levels > width)
  532. width = pNewType->num_levels;
  533. }
  534. if ((xkb->ctrls) && (nGroups > xkb->ctrls->num_groups))
  535. xkb->ctrls->num_groups = nGroups;
  536. if ((width != oldWidth) || (nGroups != nOldGroups)) {
  537. KeySym oldSyms[XkbMaxSymsPerKey], *pSyms;
  538. int nCopy;
  539. if (nOldGroups == 0) {
  540. pSyms = XkbResizeKeySyms(xkb, key, width * nGroups);
  541. if (pSyms != NULL) {
  542. i = xkb->map->key_sym_map[key].group_info;
  543. i = XkbSetNumGroups(i, nGroups);
  544. xkb->map->key_sym_map[key].group_info = i;
  545. xkb->map->key_sym_map[key].width = width;
  546. for (i = 0; i < nGroups; i++) {
  547. xkb->map->key_sym_map[key].kt_index[i] = newTypes[i];
  548. }
  549. return Success;
  550. }
  551. return BadAlloc;
  552. }
  553. pSyms = XkbKeySymsPtr(xkb, key);
  554. memcpy(oldSyms, pSyms, XkbKeyNumSyms(xkb, key) * sizeof(KeySym));
  555. pSyms = XkbResizeKeySyms(xkb, key, width * nGroups);
  556. if (pSyms == NULL)
  557. return BadAlloc;
  558. memset(pSyms, 0, width * nGroups * sizeof(KeySym));
  559. for (i = 0; (i < nGroups) && (i < nOldGroups); i++) {
  560. pOldType = XkbKeyKeyType(xkb, key, i);
  561. pNewType = &xkb->map->types[newTypes[i]];
  562. if (pNewType->num_levels > pOldType->num_levels)
  563. nCopy = pOldType->num_levels;
  564. else
  565. nCopy = pNewType->num_levels;
  566. memcpy(&pSyms[i * width], &oldSyms[i * oldWidth],
  567. nCopy * sizeof(KeySym));
  568. }
  569. if (XkbKeyHasActions(xkb, key)) {
  570. XkbAction oldActs[XkbMaxSymsPerKey], *pActs;
  571. pActs = XkbKeyActionsPtr(xkb, key);
  572. memcpy(oldActs, pActs, XkbKeyNumSyms(xkb, key) * sizeof(XkbAction));
  573. pActs = XkbResizeKeyActions(xkb, key, width * nGroups);
  574. if (pActs == NULL)
  575. return BadAlloc;
  576. memset(pActs, 0, width * nGroups * sizeof(XkbAction));
  577. for (i = 0; (i < nGroups) && (i < nOldGroups); i++) {
  578. pOldType = XkbKeyKeyType(xkb, key, i);
  579. pNewType = &xkb->map->types[newTypes[i]];
  580. if (pNewType->num_levels > pOldType->num_levels)
  581. nCopy = pOldType->num_levels;
  582. else
  583. nCopy = pNewType->num_levels;
  584. memcpy(&pActs[i * width], &oldActs[i * oldWidth],
  585. nCopy * sizeof(XkbAction));
  586. }
  587. }
  588. i = xkb->map->key_sym_map[key].group_info;
  589. i = XkbSetNumGroups(i, nGroups);
  590. xkb->map->key_sym_map[key].group_info = i;
  591. xkb->map->key_sym_map[key].width = width;
  592. }
  593. width = 0;
  594. for (i = 0; i < nGroups; i++) {
  595. xkb->map->key_sym_map[key].kt_index[i] = newTypes[i];
  596. if (xkb->map->types[newTypes[i]].num_levels > width)
  597. width = xkb->map->types[newTypes[i]].num_levels;
  598. }
  599. xkb->map->key_sym_map[key].width = width;
  600. if (changes != NULL) {
  601. if (changes->changed & XkbKeySymsMask) {
  602. _XkbAddKeyChange(&changes->first_key_sym, &changes->num_key_syms,
  603. key);
  604. }
  605. else {
  606. changes->changed |= XkbKeySymsMask;
  607. changes->first_key_sym = key;
  608. changes->num_key_syms = 1;
  609. }
  610. }
  611. return Success;
  612. }
  613. /***====================================================================***/
  614. Bool
  615. XkbVirtualModsToReal(XkbDescPtr xkb, unsigned virtual_mask, unsigned *mask_rtrn)
  616. {
  617. register int i, bit;
  618. register unsigned mask;
  619. if (xkb == NULL)
  620. return FALSE;
  621. if (virtual_mask == 0) {
  622. *mask_rtrn = 0;
  623. return TRUE;
  624. }
  625. if (xkb->server == NULL)
  626. return FALSE;
  627. for (i = mask = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
  628. if (virtual_mask & bit)
  629. mask |= xkb->server->vmods[i];
  630. }
  631. *mask_rtrn = mask;
  632. return TRUE;
  633. }
  634. /***====================================================================***/
  635. static Bool
  636. XkbUpdateActionVirtualMods(XkbDescPtr xkb, XkbAction *act, unsigned changed)
  637. {
  638. unsigned int tmp;
  639. switch (act->type) {
  640. case XkbSA_SetMods:
  641. case XkbSA_LatchMods:
  642. case XkbSA_LockMods:
  643. if (((tmp = XkbModActionVMods(&act->mods)) & changed) != 0) {
  644. XkbVirtualModsToReal(xkb, tmp, &tmp);
  645. act->mods.mask = act->mods.real_mods;
  646. act->mods.mask |= tmp;
  647. return TRUE;
  648. }
  649. break;
  650. case XkbSA_ISOLock:
  651. if ((((tmp = XkbModActionVMods(&act->iso)) != 0) & changed) != 0) {
  652. XkbVirtualModsToReal(xkb, tmp, &tmp);
  653. act->iso.mask = act->iso.real_mods;
  654. act->iso.mask |= tmp;
  655. return TRUE;
  656. }
  657. break;
  658. }
  659. return FALSE;
  660. }
  661. static void
  662. XkbUpdateKeyTypeVirtualMods(XkbDescPtr xkb,
  663. XkbKeyTypePtr type,
  664. unsigned int changed, XkbChangesPtr changes)
  665. {
  666. register unsigned int i;
  667. unsigned int mask;
  668. XkbVirtualModsToReal(xkb, type->mods.vmods, &mask);
  669. type->mods.mask = type->mods.real_mods | mask;
  670. if ((type->map_count > 0) && (type->mods.vmods != 0)) {
  671. XkbKTMapEntryPtr entry;
  672. for (i = 0, entry = type->map; i < type->map_count; i++, entry++) {
  673. if (entry->mods.vmods != 0) {
  674. XkbVirtualModsToReal(xkb, entry->mods.vmods, &mask);
  675. entry->mods.mask = entry->mods.real_mods | mask;
  676. /* entry is active if vmods are bound */
  677. entry->active = (mask != 0);
  678. }
  679. else
  680. entry->active = 1;
  681. }
  682. }
  683. if (changes) {
  684. int type_ndx;
  685. type_ndx = type - xkb->map->types;
  686. if ((type_ndx < 0) || (type_ndx > xkb->map->num_types))
  687. return;
  688. if (changes->map.changed & XkbKeyTypesMask) {
  689. int last;
  690. last = changes->map.first_type + changes->map.num_types - 1;
  691. if (type_ndx < changes->map.first_type) {
  692. changes->map.first_type = type_ndx;
  693. changes->map.num_types = (last - type_ndx) + 1;
  694. }
  695. else if (type_ndx > last) {
  696. changes->map.num_types =
  697. (type_ndx - changes->map.first_type) + 1;
  698. }
  699. }
  700. else {
  701. changes->map.changed |= XkbKeyTypesMask;
  702. changes->map.first_type = type_ndx;
  703. changes->map.num_types = 1;
  704. }
  705. }
  706. return;
  707. }
  708. Bool
  709. XkbApplyVirtualModChanges(XkbDescPtr xkb, unsigned changed,
  710. XkbChangesPtr changes)
  711. {
  712. register int i;
  713. unsigned int checkState = 0;
  714. if ((!xkb) || (!xkb->map) || (changed == 0))
  715. return FALSE;
  716. for (i = 0; i < xkb->map->num_types; i++) {
  717. if (xkb->map->types[i].mods.vmods & changed)
  718. XkbUpdateKeyTypeVirtualMods(xkb, &xkb->map->types[i], changed,
  719. changes);
  720. }
  721. if (changed & xkb->ctrls->internal.vmods) {
  722. unsigned int newMask;
  723. XkbVirtualModsToReal(xkb, xkb->ctrls->internal.vmods, &newMask);
  724. newMask |= xkb->ctrls->internal.real_mods;
  725. if (xkb->ctrls->internal.mask != newMask) {
  726. xkb->ctrls->internal.mask = newMask;
  727. if (changes) {
  728. changes->ctrls.changed_ctrls |= XkbInternalModsMask;
  729. checkState = TRUE;
  730. }
  731. }
  732. }
  733. if (changed & xkb->ctrls->ignore_lock.vmods) {
  734. unsigned int newMask;
  735. XkbVirtualModsToReal(xkb, xkb->ctrls->ignore_lock.vmods, &newMask);
  736. newMask |= xkb->ctrls->ignore_lock.real_mods;
  737. if (xkb->ctrls->ignore_lock.mask != newMask) {
  738. xkb->ctrls->ignore_lock.mask = newMask;
  739. if (changes) {
  740. changes->ctrls.changed_ctrls |= XkbIgnoreLockModsMask;
  741. checkState = TRUE;
  742. }
  743. }
  744. }
  745. if (xkb->indicators != NULL) {
  746. XkbIndicatorMapPtr map;
  747. map = &xkb->indicators->maps[0];
  748. for (i = 0; i < XkbNumIndicators; i++, map++) {
  749. if (map->mods.vmods & changed) {
  750. unsigned int newMask;
  751. XkbVirtualModsToReal(xkb, map->mods.vmods, &newMask);
  752. newMask |= map->mods.real_mods;
  753. if (newMask != map->mods.mask) {
  754. map->mods.mask = newMask;
  755. if (changes) {
  756. changes->indicators.map_changes |= (1 << i);
  757. checkState = TRUE;
  758. }
  759. }
  760. }
  761. }
  762. }
  763. if (xkb->compat != NULL) {
  764. XkbCompatMapPtr compat;
  765. compat = xkb->compat;
  766. for (i = 0; i < XkbNumKbdGroups; i++) {
  767. unsigned int newMask;
  768. XkbVirtualModsToReal(xkb, compat->groups[i].vmods, &newMask);
  769. newMask |= compat->groups[i].real_mods;
  770. if (compat->groups[i].mask != newMask) {
  771. compat->groups[i].mask = newMask;
  772. if (changes) {
  773. changes->compat.changed_groups |= (1 << i);
  774. checkState = TRUE;
  775. }
  776. }
  777. }
  778. }
  779. if (xkb->map && xkb->server) {
  780. int highChange = 0, lowChange = -1;
  781. for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
  782. if (XkbKeyHasActions(xkb, i)) {
  783. register XkbAction *pAct;
  784. register int n;
  785. pAct = XkbKeyActionsPtr(xkb, i);
  786. for (n = XkbKeyNumActions(xkb, i); n > 0; n--, pAct++) {
  787. if ((pAct->type != XkbSA_NoAction) &&
  788. XkbUpdateActionVirtualMods(xkb, pAct, changed)) {
  789. if (lowChange < 0)
  790. lowChange = i;
  791. highChange = i;
  792. }
  793. }
  794. }
  795. }
  796. if (changes && (lowChange > 0)) { /* something changed */
  797. if (changes->map.changed & XkbKeyActionsMask) {
  798. int last;
  799. if (changes->map.first_key_act < lowChange)
  800. lowChange = changes->map.first_key_act;
  801. last =
  802. changes->map.first_key_act + changes->map.num_key_acts - 1;
  803. if (last > highChange)
  804. highChange = last;
  805. }
  806. changes->map.changed |= XkbKeyActionsMask;
  807. changes->map.first_key_act = lowChange;
  808. changes->map.num_key_acts = (highChange - lowChange) + 1;
  809. }
  810. }
  811. return checkState;
  812. }