XKBMAlloc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. Status
  38. XkbAllocClientMap(XkbDescPtr xkb, unsigned which, unsigned nTotalTypes)
  39. {
  40. register int i;
  41. XkbClientMapPtr map;
  42. if ((xkb == NULL) ||
  43. ((nTotalTypes > 0) && (nTotalTypes < XkbNumRequiredTypes)))
  44. return BadValue;
  45. if ((which & XkbKeySymsMask) &&
  46. ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  47. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  48. (xkb->max_key_code < xkb->min_key_code))) {
  49. DebugF("bad keycode (%d,%d) in XkbAllocClientMap\n",
  50. xkb->min_key_code, xkb->max_key_code);
  51. return BadValue;
  52. }
  53. if (xkb->map == NULL) {
  54. map = calloc(1, sizeof(XkbClientMapRec));
  55. if (map == NULL)
  56. return BadAlloc;
  57. xkb->map = map;
  58. }
  59. else
  60. map = xkb->map;
  61. if ((which & XkbKeyTypesMask) && (nTotalTypes > 0)) {
  62. if (map->types == NULL) {
  63. map->types = calloc(nTotalTypes, sizeof(XkbKeyTypeRec));
  64. if (map->types == NULL)
  65. return BadAlloc;
  66. map->num_types = 0;
  67. map->size_types = nTotalTypes;
  68. }
  69. else if (map->size_types < nTotalTypes) {
  70. XkbKeyTypeRec *prev_types = map->types;
  71. map->types =
  72. realloc(map->types, nTotalTypes * sizeof(XkbKeyTypeRec));
  73. if (map->types == NULL) {
  74. free(prev_types);
  75. map->num_types = map->size_types = 0;
  76. return BadAlloc;
  77. }
  78. map->size_types = nTotalTypes;
  79. memset(&map->types[map->num_types], 0,
  80. ((map->size_types -
  81. map->num_types) * sizeof(XkbKeyTypeRec)));
  82. }
  83. }
  84. if (which & XkbKeySymsMask) {
  85. int nKeys = XkbNumKeys(xkb);
  86. if (map->syms == NULL) {
  87. map->size_syms = (nKeys * 15) / 10;
  88. map->syms = calloc(map->size_syms, sizeof(KeySym));
  89. if (!map->syms) {
  90. map->size_syms = 0;
  91. return BadAlloc;
  92. }
  93. map->num_syms = 1;
  94. map->syms[0] = NoSymbol;
  95. }
  96. if (map->key_sym_map == NULL) {
  97. i = xkb->max_key_code + 1;
  98. map->key_sym_map = calloc(i, sizeof(XkbSymMapRec));
  99. if (map->key_sym_map == NULL)
  100. return BadAlloc;
  101. }
  102. }
  103. if (which & XkbModifierMapMask) {
  104. if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  105. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  106. (xkb->max_key_code < xkb->min_key_code))
  107. return BadMatch;
  108. if (map->modmap == NULL) {
  109. i = xkb->max_key_code + 1;
  110. map->modmap = calloc(i, sizeof(unsigned char));
  111. if (map->modmap == NULL)
  112. return BadAlloc;
  113. }
  114. }
  115. return Success;
  116. }
  117. Status
  118. XkbAllocServerMap(XkbDescPtr xkb, unsigned which, unsigned nNewActions)
  119. {
  120. register int i;
  121. XkbServerMapPtr map;
  122. if (xkb == NULL)
  123. return BadMatch;
  124. if (xkb->server == NULL) {
  125. map = calloc(1, sizeof(XkbServerMapRec));
  126. if (map == NULL)
  127. return BadAlloc;
  128. for (i = 0; i < XkbNumVirtualMods; i++) {
  129. map->vmods[i] = XkbNoModifierMask;
  130. }
  131. xkb->server = map;
  132. }
  133. else
  134. map = xkb->server;
  135. if (which & XkbExplicitComponentsMask) {
  136. if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  137. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  138. (xkb->max_key_code < xkb->min_key_code))
  139. return BadMatch;
  140. if (map->explicit == NULL) {
  141. i = xkb->max_key_code + 1;
  142. map->explicit = calloc(i, sizeof(unsigned char));
  143. if (map->explicit == NULL)
  144. return BadAlloc;
  145. }
  146. }
  147. if (which & XkbKeyActionsMask) {
  148. if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  149. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  150. (xkb->max_key_code < xkb->min_key_code))
  151. return BadMatch;
  152. if (nNewActions < 1)
  153. nNewActions = 1;
  154. if (map->acts == NULL) {
  155. map->acts = calloc((nNewActions + 1), sizeof(XkbAction));
  156. if (map->acts == NULL)
  157. return BadAlloc;
  158. map->num_acts = 1;
  159. map->size_acts = nNewActions + 1;
  160. }
  161. else if ((map->size_acts - map->num_acts) < nNewActions) {
  162. unsigned need;
  163. XkbAction *prev_acts = map->acts;
  164. need = map->num_acts + nNewActions;
  165. map->acts = realloc(map->acts, need * sizeof(XkbAction));
  166. if (map->acts == NULL) {
  167. free(prev_acts);
  168. map->num_acts = map->size_acts = 0;
  169. return BadAlloc;
  170. }
  171. map->size_acts = need;
  172. memset(&map->acts[map->num_acts], 0,
  173. ((map->size_acts - map->num_acts) * sizeof(XkbAction)));
  174. }
  175. if (map->key_acts == NULL) {
  176. i = xkb->max_key_code + 1;
  177. map->key_acts = calloc(i, sizeof(unsigned short));
  178. if (map->key_acts == NULL)
  179. return BadAlloc;
  180. }
  181. }
  182. if (which & XkbKeyBehaviorsMask) {
  183. if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  184. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  185. (xkb->max_key_code < xkb->min_key_code))
  186. return BadMatch;
  187. if (map->behaviors == NULL) {
  188. i = xkb->max_key_code + 1;
  189. map->behaviors = calloc(i, sizeof(XkbBehavior));
  190. if (map->behaviors == NULL)
  191. return BadAlloc;
  192. }
  193. }
  194. if (which & XkbVirtualModMapMask) {
  195. if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  196. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  197. (xkb->max_key_code < xkb->min_key_code))
  198. return BadMatch;
  199. if (map->vmodmap == NULL) {
  200. i = xkb->max_key_code + 1;
  201. map->vmodmap = calloc(i, sizeof(unsigned short));
  202. if (map->vmodmap == NULL)
  203. return BadAlloc;
  204. }
  205. }
  206. return Success;
  207. }
  208. /***====================================================================***/
  209. static Status
  210. XkbCopyKeyType(XkbKeyTypePtr from, XkbKeyTypePtr into)
  211. {
  212. if ((!from) || (!into))
  213. return BadMatch;
  214. free(into->map);
  215. into->map = NULL;
  216. free(into->preserve);
  217. into->preserve = NULL;
  218. free(into->level_names);
  219. into->level_names = NULL;
  220. *into = *from;
  221. if ((from->map) && (into->map_count > 0)) {
  222. into->map = calloc(into->map_count, sizeof(XkbKTMapEntryRec));
  223. if (!into->map)
  224. return BadAlloc;
  225. memcpy(into->map, from->map,
  226. into->map_count * sizeof(XkbKTMapEntryRec));
  227. }
  228. if ((from->preserve) && (into->map_count > 0)) {
  229. into->preserve = calloc(into->map_count, sizeof(XkbModsRec));
  230. if (!into->preserve)
  231. return BadAlloc;
  232. memcpy(into->preserve, from->preserve,
  233. into->map_count * sizeof(XkbModsRec));
  234. }
  235. if ((from->level_names) && (into->num_levels > 0)) {
  236. into->level_names = calloc(into->num_levels, sizeof(Atom));
  237. if (!into->level_names)
  238. return BadAlloc;
  239. memcpy(into->level_names, from->level_names,
  240. into->num_levels * sizeof(Atom));
  241. }
  242. return Success;
  243. }
  244. Status
  245. XkbCopyKeyTypes(XkbKeyTypePtr from, XkbKeyTypePtr into, int num_types)
  246. {
  247. register int i, rtrn;
  248. if ((!from) || (!into) || (num_types < 0))
  249. return BadMatch;
  250. for (i = 0; i < num_types; i++) {
  251. if ((rtrn = XkbCopyKeyType(from++, into++)) != Success)
  252. return rtrn;
  253. }
  254. return Success;
  255. }
  256. Status
  257. XkbResizeKeyType(XkbDescPtr xkb,
  258. int type_ndx,
  259. int map_count, Bool want_preserve, int new_num_lvls)
  260. {
  261. XkbKeyTypePtr type;
  262. KeyCode matchingKeys[XkbMaxKeyCount], nMatchingKeys;
  263. if ((type_ndx < 0) || (type_ndx >= xkb->map->num_types) || (map_count < 0)
  264. || (new_num_lvls < 1))
  265. return BadValue;
  266. switch (type_ndx) {
  267. case XkbOneLevelIndex:
  268. if (new_num_lvls != 1)
  269. return BadMatch;
  270. break;
  271. case XkbTwoLevelIndex:
  272. case XkbAlphabeticIndex:
  273. case XkbKeypadIndex:
  274. if (new_num_lvls != 2)
  275. return BadMatch;
  276. break;
  277. }
  278. type = &xkb->map->types[type_ndx];
  279. if (map_count == 0) {
  280. free(type->map);
  281. type->map = NULL;
  282. free(type->preserve);
  283. type->preserve = NULL;
  284. type->map_count = 0;
  285. }
  286. else {
  287. XkbKTMapEntryRec *prev_map = type->map;
  288. if ((map_count > type->map_count) || (type->map == NULL))
  289. type->map =
  290. realloc(type->map, map_count * sizeof(XkbKTMapEntryRec));
  291. if (!type->map) {
  292. free(prev_map);
  293. return BadAlloc;
  294. }
  295. if (want_preserve) {
  296. XkbModsRec *prev_preserve = type->preserve;
  297. if ((map_count > type->map_count) || (type->preserve == NULL)) {
  298. type->preserve = realloc(type->preserve,
  299. map_count * sizeof(XkbModsRec));
  300. }
  301. if (!type->preserve) {
  302. free(prev_preserve);
  303. return BadAlloc;
  304. }
  305. }
  306. else {
  307. free(type->preserve);
  308. type->preserve = NULL;
  309. }
  310. type->map_count = map_count;
  311. }
  312. if ((new_num_lvls > type->num_levels) || (type->level_names == NULL)) {
  313. Atom *prev_level_names = type->level_names;
  314. type->level_names = realloc(type->level_names,
  315. new_num_lvls * sizeof(Atom));
  316. if (!type->level_names) {
  317. free(prev_level_names);
  318. return BadAlloc;
  319. }
  320. }
  321. /*
  322. * Here's the theory:
  323. * If the width of the type changed, we might have to resize the symbol
  324. * maps for any keys that use the type for one or more groups. This is
  325. * expensive, so we'll try to cull out any keys that are obviously okay:
  326. * In any case:
  327. * - keys that have a group width <= the old width are okay (because
  328. * they could not possibly have been associated with the old type)
  329. * If the key type increased in size:
  330. * - keys that already have a group width >= to the new width are okay
  331. * + keys that have a group width >= the old width but < the new width
  332. * might have to be enlarged.
  333. * If the key type decreased in size:
  334. * - keys that have a group width > the old width don't have to be
  335. * resized (because they must have some other wider type associated
  336. * with some group).
  337. * + keys that have a group width == the old width might have to be
  338. * shrunk.
  339. * The possibilities marked with '+' require us to examine the key types
  340. * associated with each group for the key.
  341. */
  342. memset(matchingKeys, 0, XkbMaxKeyCount * sizeof(KeyCode));
  343. nMatchingKeys = 0;
  344. if (new_num_lvls > type->num_levels) {
  345. int nTotal;
  346. KeySym *newSyms;
  347. int width, match, nResize;
  348. register int i, g, nSyms;
  349. nResize = 0;
  350. for (nTotal = 1, i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
  351. width = XkbKeyGroupsWidth(xkb, i);
  352. if (width < type->num_levels || width >= new_num_lvls) {
  353. nTotal += XkbKeyNumSyms(xkb,i);
  354. continue;
  355. }
  356. for (match = 0, g = XkbKeyNumGroups(xkb, i) - 1;
  357. (g >= 0) && (!match); g--) {
  358. if (XkbKeyKeyTypeIndex(xkb, i, g) == type_ndx) {
  359. matchingKeys[nMatchingKeys++] = i;
  360. match = 1;
  361. }
  362. }
  363. if (!match)
  364. nTotal += XkbKeyNumSyms(xkb, i);
  365. else {
  366. nTotal += XkbKeyNumGroups(xkb, i) * new_num_lvls;
  367. nResize++;
  368. }
  369. }
  370. if (nResize > 0) {
  371. int nextMatch;
  372. xkb->map->size_syms = (nTotal * 15) / 10;
  373. newSyms = calloc(xkb->map->size_syms, sizeof(KeySym));
  374. if (newSyms == NULL)
  375. return BadAlloc;
  376. nextMatch = 0;
  377. nSyms = 1;
  378. for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
  379. if (matchingKeys[nextMatch] == i) {
  380. KeySym *pOld;
  381. nextMatch++;
  382. width = XkbKeyGroupsWidth(xkb, i);
  383. pOld = XkbKeySymsPtr(xkb, i);
  384. for (g = XkbKeyNumGroups(xkb, i) - 1; g >= 0; g--) {
  385. memcpy(&newSyms[nSyms + (new_num_lvls * g)],
  386. &pOld[width * g], width * sizeof(KeySym));
  387. }
  388. xkb->map->key_sym_map[i].offset = nSyms;
  389. nSyms += XkbKeyNumGroups(xkb, i) * new_num_lvls;
  390. }
  391. else {
  392. memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
  393. XkbKeyNumSyms(xkb, i) * sizeof(KeySym));
  394. xkb->map->key_sym_map[i].offset = nSyms;
  395. nSyms += XkbKeyNumSyms(xkb, i);
  396. }
  397. }
  398. type->num_levels = new_num_lvls;
  399. free(xkb->map->syms);
  400. xkb->map->syms = newSyms;
  401. xkb->map->num_syms = nSyms;
  402. return Success;
  403. }
  404. }
  405. else if (new_num_lvls < type->num_levels) {
  406. int width, match;
  407. register int g, i;
  408. for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
  409. width = XkbKeyGroupsWidth(xkb, i);
  410. if (width < type->num_levels)
  411. continue;
  412. for (match = 0, g = XkbKeyNumGroups(xkb, i) - 1;
  413. (g >= 0) && (!match); g--) {
  414. if (XkbKeyKeyTypeIndex(xkb, i, g) == type_ndx) {
  415. matchingKeys[nMatchingKeys++] = i;
  416. match = 1;
  417. }
  418. }
  419. }
  420. }
  421. if (nMatchingKeys > 0) {
  422. int key, firstClear;
  423. register int i, g;
  424. if (new_num_lvls > type->num_levels)
  425. firstClear = type->num_levels;
  426. else
  427. firstClear = new_num_lvls;
  428. for (i = 0; i < nMatchingKeys; i++) {
  429. KeySym *pSyms;
  430. int width, nClear;
  431. key = matchingKeys[i];
  432. width = XkbKeyGroupsWidth(xkb, key);
  433. nClear = width - firstClear;
  434. pSyms = XkbKeySymsPtr(xkb, key);
  435. for (g = XkbKeyNumGroups(xkb, key) - 1; g >= 0; g--) {
  436. if (XkbKeyKeyTypeIndex(xkb, key, g) == type_ndx) {
  437. if (nClear > 0)
  438. memset(&pSyms[g * width + firstClear], 0,
  439. nClear * sizeof(KeySym));
  440. }
  441. }
  442. }
  443. }
  444. type->num_levels = new_num_lvls;
  445. return Success;
  446. }
  447. KeySym *
  448. XkbResizeKeySyms(XkbDescPtr xkb, int key, int needed)
  449. {
  450. register int i, nSyms, nKeySyms;
  451. unsigned nOldSyms;
  452. KeySym *newSyms;
  453. if (needed == 0) {
  454. xkb->map->key_sym_map[key].offset = 0;
  455. return xkb->map->syms;
  456. }
  457. nOldSyms = XkbKeyNumSyms(xkb, key);
  458. if (nOldSyms >= (unsigned) needed) {
  459. return XkbKeySymsPtr(xkb, key);
  460. }
  461. if (xkb->map->size_syms - xkb->map->num_syms >= (unsigned) needed) {
  462. if (nOldSyms > 0) {
  463. memcpy(&xkb->map->syms[xkb->map->num_syms], XkbKeySymsPtr(xkb, key),
  464. nOldSyms * sizeof(KeySym));
  465. }
  466. if ((needed - nOldSyms) > 0) {
  467. memset(&xkb->map->
  468. syms[xkb->map->num_syms + XkbKeyNumSyms(xkb, key)], 0,
  469. (needed - nOldSyms) * sizeof(KeySym));
  470. }
  471. xkb->map->key_sym_map[key].offset = xkb->map->num_syms;
  472. xkb->map->num_syms += needed;
  473. return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
  474. }
  475. xkb->map->size_syms += (needed > 32 ? needed : 32);
  476. newSyms = calloc(xkb->map->size_syms, sizeof(KeySym));
  477. if (newSyms == NULL)
  478. return NULL;
  479. newSyms[0] = NoSymbol;
  480. nSyms = 1;
  481. for (i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++) {
  482. int nCopy;
  483. nCopy = nKeySyms = XkbKeyNumSyms(xkb, i);
  484. if ((nKeySyms == 0) && (i != key))
  485. continue;
  486. if (i == key)
  487. nKeySyms = needed;
  488. if (nCopy != 0)
  489. memcpy(&newSyms[nSyms], XkbKeySymsPtr(xkb, i),
  490. nCopy * sizeof(KeySym));
  491. if (nKeySyms > nCopy)
  492. memset(&newSyms[nSyms + nCopy], 0,
  493. (nKeySyms - nCopy) * sizeof(KeySym));
  494. xkb->map->key_sym_map[i].offset = nSyms;
  495. nSyms += nKeySyms;
  496. }
  497. free(xkb->map->syms);
  498. xkb->map->syms = newSyms;
  499. xkb->map->num_syms = nSyms;
  500. return &xkb->map->syms[xkb->map->key_sym_map[key].offset];
  501. }
  502. static unsigned
  503. _ExtendRange(unsigned int old_flags,
  504. unsigned int flag,
  505. KeyCode newKC, KeyCode *old_min, unsigned char *old_num)
  506. {
  507. if ((old_flags & flag) == 0) {
  508. old_flags |= flag;
  509. *old_min = newKC;
  510. *old_num = 1;
  511. }
  512. else {
  513. int last = (*old_min) + (*old_num) - 1;
  514. if (newKC < *old_min) {
  515. *old_min = newKC;
  516. *old_num = (last - newKC) + 1;
  517. }
  518. else if (newKC > last) {
  519. *old_num = (newKC - (*old_min)) + 1;
  520. }
  521. }
  522. return old_flags;
  523. }
  524. Status
  525. XkbChangeKeycodeRange(XkbDescPtr xkb,
  526. int minKC, int maxKC, XkbChangesPtr changes)
  527. {
  528. int tmp;
  529. if ((!xkb) || (minKC < XkbMinLegalKeyCode) || (maxKC > XkbMaxLegalKeyCode))
  530. return BadValue;
  531. if (minKC > maxKC)
  532. return BadMatch;
  533. if (minKC < xkb->min_key_code) {
  534. if (changes)
  535. changes->map.min_key_code = minKC;
  536. tmp = xkb->min_key_code - minKC;
  537. if (xkb->map) {
  538. if (xkb->map->key_sym_map) {
  539. memset((char *) &xkb->map->key_sym_map[minKC], 0,
  540. tmp * sizeof(XkbSymMapRec));
  541. if (changes) {
  542. changes->map.changed = _ExtendRange(changes->map.changed,
  543. XkbKeySymsMask, minKC,
  544. &changes->map.
  545. first_key_sym,
  546. &changes->map.
  547. num_key_syms);
  548. }
  549. }
  550. if (xkb->map->modmap) {
  551. memset((char *) &xkb->map->modmap[minKC], 0, tmp);
  552. if (changes) {
  553. changes->map.changed = _ExtendRange(changes->map.changed,
  554. XkbModifierMapMask,
  555. minKC,
  556. &changes->map.
  557. first_modmap_key,
  558. &changes->map.
  559. num_modmap_keys);
  560. }
  561. }
  562. }
  563. if (xkb->server) {
  564. if (xkb->server->behaviors) {
  565. memset((char *) &xkb->server->behaviors[minKC], 0,
  566. tmp * sizeof(XkbBehavior));
  567. if (changes) {
  568. changes->map.changed = _ExtendRange(changes->map.changed,
  569. XkbKeyBehaviorsMask,
  570. minKC,
  571. &changes->map.
  572. first_key_behavior,
  573. &changes->map.
  574. num_key_behaviors);
  575. }
  576. }
  577. if (xkb->server->key_acts) {
  578. memset((char *) &xkb->server->key_acts[minKC], 0,
  579. tmp * sizeof(unsigned short));
  580. if (changes) {
  581. changes->map.changed = _ExtendRange(changes->map.changed,
  582. XkbKeyActionsMask,
  583. minKC,
  584. &changes->map.
  585. first_key_act,
  586. &changes->map.
  587. num_key_acts);
  588. }
  589. }
  590. if (xkb->server->vmodmap) {
  591. memset((char *) &xkb->server->vmodmap[minKC], 0,
  592. tmp * sizeof(unsigned short));
  593. if (changes) {
  594. changes->map.changed = _ExtendRange(changes->map.changed,
  595. XkbVirtualModMapMask,
  596. minKC,
  597. &changes->map.
  598. first_modmap_key,
  599. &changes->map.
  600. num_vmodmap_keys);
  601. }
  602. }
  603. }
  604. if ((xkb->names) && (xkb->names->keys)) {
  605. memset((char *) &xkb->names->keys[minKC], 0,
  606. tmp * sizeof(XkbKeyNameRec));
  607. if (changes) {
  608. changes->names.changed = _ExtendRange(changes->names.changed,
  609. XkbKeyNamesMask, minKC,
  610. &changes->names.first_key,
  611. &changes->names.num_keys);
  612. }
  613. }
  614. xkb->min_key_code = minKC;
  615. }
  616. if (maxKC > xkb->max_key_code) {
  617. if (changes)
  618. changes->map.max_key_code = maxKC;
  619. tmp = maxKC - xkb->max_key_code;
  620. if (xkb->map) {
  621. if (xkb->map->key_sym_map) {
  622. XkbSymMapRec *prev_key_sym_map = xkb->map->key_sym_map;
  623. xkb->map->key_sym_map = realloc(xkb->map->key_sym_map,
  624. (maxKC +
  625. 1) * sizeof(XkbSymMapRec));
  626. if (!xkb->map->key_sym_map) {
  627. free(prev_key_sym_map);
  628. return BadAlloc;
  629. }
  630. memset((char *) &xkb->map->key_sym_map[xkb->max_key_code], 0,
  631. tmp * sizeof(XkbSymMapRec));
  632. if (changes) {
  633. changes->map.changed = _ExtendRange(changes->map.changed,
  634. XkbKeySymsMask, maxKC,
  635. &changes->map.
  636. first_key_sym,
  637. &changes->map.
  638. num_key_syms);
  639. }
  640. }
  641. if (xkb->map->modmap) {
  642. unsigned char *prev_modmap = xkb->map->modmap;
  643. xkb->map->modmap = realloc(xkb->map->modmap,
  644. (maxKC + 1) * sizeof(unsigned char));
  645. if (!xkb->map->modmap) {
  646. free(prev_modmap);
  647. return BadAlloc;
  648. }
  649. memset((char *) &xkb->map->modmap[xkb->max_key_code], 0, tmp);
  650. if (changes) {
  651. changes->map.changed = _ExtendRange(changes->map.changed,
  652. XkbModifierMapMask,
  653. maxKC,
  654. &changes->map.
  655. first_modmap_key,
  656. &changes->map.
  657. num_modmap_keys);
  658. }
  659. }
  660. }
  661. if (xkb->server) {
  662. if (xkb->server->behaviors) {
  663. XkbBehavior *prev_behaviors = xkb->server->behaviors;
  664. xkb->server->behaviors = realloc(xkb->server->behaviors,
  665. (maxKC +
  666. 1) * sizeof(XkbBehavior));
  667. if (!xkb->server->behaviors) {
  668. free(prev_behaviors);
  669. return BadAlloc;
  670. }
  671. memset((char *) &xkb->server->behaviors[xkb->max_key_code], 0,
  672. tmp * sizeof(XkbBehavior));
  673. if (changes) {
  674. changes->map.changed = _ExtendRange(changes->map.changed,
  675. XkbKeyBehaviorsMask,
  676. maxKC,
  677. &changes->map.
  678. first_key_behavior,
  679. &changes->map.
  680. num_key_behaviors);
  681. }
  682. }
  683. if (xkb->server->key_acts) {
  684. unsigned short *prev_key_acts = xkb->server->key_acts;
  685. xkb->server->key_acts = realloc(xkb->server->key_acts,
  686. (maxKC +
  687. 1) * sizeof(unsigned short));
  688. if (!xkb->server->key_acts) {
  689. free(prev_key_acts);
  690. return BadAlloc;
  691. }
  692. memset((char *) &xkb->server->key_acts[xkb->max_key_code], 0,
  693. tmp * sizeof(unsigned short));
  694. if (changes) {
  695. changes->map.changed = _ExtendRange(changes->map.changed,
  696. XkbKeyActionsMask,
  697. maxKC,
  698. &changes->map.
  699. first_key_act,
  700. &changes->map.
  701. num_key_acts);
  702. }
  703. }
  704. if (xkb->server->vmodmap) {
  705. unsigned short *prev_vmodmap = xkb->server->vmodmap;
  706. xkb->server->vmodmap = realloc(xkb->server->vmodmap,
  707. (maxKC +
  708. 1) * sizeof(unsigned short));
  709. if (!xkb->server->vmodmap) {
  710. free(prev_vmodmap);
  711. return BadAlloc;
  712. }
  713. memset((char *) &xkb->server->vmodmap[xkb->max_key_code], 0,
  714. tmp * sizeof(unsigned short));
  715. if (changes) {
  716. changes->map.changed = _ExtendRange(changes->map.changed,
  717. XkbVirtualModMapMask,
  718. maxKC,
  719. &changes->map.
  720. first_modmap_key,
  721. &changes->map.
  722. num_vmodmap_keys);
  723. }
  724. }
  725. }
  726. if ((xkb->names) && (xkb->names->keys)) {
  727. XkbKeyNameRec *prev_keys = xkb->names->keys;
  728. xkb->names->keys = realloc(xkb->names->keys,
  729. (maxKC + 1) * sizeof(XkbKeyNameRec));
  730. if (!xkb->names->keys) {
  731. free(prev_keys);
  732. return BadAlloc;
  733. }
  734. memset((char *) &xkb->names->keys[xkb->max_key_code], 0,
  735. tmp * sizeof(XkbKeyNameRec));
  736. if (changes) {
  737. changes->names.changed = _ExtendRange(changes->names.changed,
  738. XkbKeyNamesMask, maxKC,
  739. &changes->names.first_key,
  740. &changes->names.num_keys);
  741. }
  742. }
  743. xkb->max_key_code = maxKC;
  744. }
  745. return Success;
  746. }
  747. XkbAction *
  748. XkbResizeKeyActions(XkbDescPtr xkb, int key, int needed)
  749. {
  750. register int i, nActs;
  751. XkbAction *newActs;
  752. if (needed == 0) {
  753. xkb->server->key_acts[key] = 0;
  754. return NULL;
  755. }
  756. if (XkbKeyHasActions(xkb, key) &&
  757. (XkbKeyNumSyms(xkb, key) >= (unsigned) needed))
  758. return XkbKeyActionsPtr(xkb, key);
  759. if (xkb->server->size_acts - xkb->server->num_acts >= (unsigned) needed) {
  760. xkb->server->key_acts[key] = xkb->server->num_acts;
  761. xkb->server->num_acts += needed;
  762. return &xkb->server->acts[xkb->server->key_acts[key]];
  763. }
  764. xkb->server->size_acts = xkb->server->num_acts + needed + 8;
  765. newActs = calloc(xkb->server->size_acts, sizeof(XkbAction));
  766. if (newActs == NULL)
  767. return NULL;
  768. newActs[0].type = XkbSA_NoAction;
  769. nActs = 1;
  770. for (i = xkb->min_key_code; i <= (int) xkb->max_key_code; i++) {
  771. int nKeyActs, nCopy;
  772. if ((xkb->server->key_acts[i] == 0) && (i != key))
  773. continue;
  774. nCopy = nKeyActs = XkbKeyNumActions(xkb, i);
  775. if (i == key) {
  776. nKeyActs = needed;
  777. if (needed < nCopy)
  778. nCopy = needed;
  779. }
  780. if (nCopy > 0)
  781. memcpy(&newActs[nActs], XkbKeyActionsPtr(xkb, i),
  782. nCopy * sizeof(XkbAction));
  783. if (nCopy < nKeyActs)
  784. memset(&newActs[nActs + nCopy], 0,
  785. (nKeyActs - nCopy) * sizeof(XkbAction));
  786. xkb->server->key_acts[i] = nActs;
  787. nActs += nKeyActs;
  788. }
  789. free(xkb->server->acts);
  790. xkb->server->acts = newActs;
  791. xkb->server->num_acts = nActs;
  792. return &xkb->server->acts[xkb->server->key_acts[key]];
  793. }
  794. void
  795. XkbFreeClientMap(XkbDescPtr xkb, unsigned what, Bool freeMap)
  796. {
  797. XkbClientMapPtr map;
  798. if ((xkb == NULL) || (xkb->map == NULL))
  799. return;
  800. if (freeMap)
  801. what = XkbAllClientInfoMask;
  802. map = xkb->map;
  803. if (what & XkbKeyTypesMask) {
  804. if (map->types != NULL) {
  805. if (map->num_types > 0) {
  806. register int i;
  807. XkbKeyTypePtr type;
  808. for (i = 0, type = map->types; i < map->num_types; i++, type++) {
  809. free(type->map);
  810. type->map = NULL;
  811. free(type->preserve);
  812. type->preserve = NULL;
  813. type->map_count = 0;
  814. free(type->level_names);
  815. type->level_names = NULL;
  816. }
  817. }
  818. free(map->types);
  819. map->num_types = map->size_types = 0;
  820. map->types = NULL;
  821. }
  822. }
  823. if (what & XkbKeySymsMask) {
  824. free(map->key_sym_map);
  825. map->key_sym_map = NULL;
  826. if (map->syms != NULL) {
  827. free(map->syms);
  828. map->size_syms = map->num_syms = 0;
  829. map->syms = NULL;
  830. }
  831. }
  832. if ((what & XkbModifierMapMask) && (map->modmap != NULL)) {
  833. free(map->modmap);
  834. map->modmap = NULL;
  835. }
  836. if (freeMap) {
  837. free(xkb->map);
  838. xkb->map = NULL;
  839. }
  840. return;
  841. }
  842. void
  843. XkbFreeServerMap(XkbDescPtr xkb, unsigned what, Bool freeMap)
  844. {
  845. XkbServerMapPtr map;
  846. if ((xkb == NULL) || (xkb->server == NULL))
  847. return;
  848. if (freeMap)
  849. what = XkbAllServerInfoMask;
  850. map = xkb->server;
  851. if ((what & XkbExplicitComponentsMask) && (map->explicit != NULL)) {
  852. free(map->explicit);
  853. map->explicit = NULL;
  854. }
  855. if (what & XkbKeyActionsMask) {
  856. free(map->key_acts);
  857. map->key_acts = NULL;
  858. if (map->acts != NULL) {
  859. free(map->acts);
  860. map->num_acts = map->size_acts = 0;
  861. map->acts = NULL;
  862. }
  863. }
  864. if ((what & XkbKeyBehaviorsMask) && (map->behaviors != NULL)) {
  865. free(map->behaviors);
  866. map->behaviors = NULL;
  867. }
  868. if ((what & XkbVirtualModMapMask) && (map->vmodmap != NULL)) {
  869. free(map->vmodmap);
  870. map->vmodmap = NULL;
  871. }
  872. if (freeMap) {
  873. free(xkb->server);
  874. xkb->server = NULL;
  875. }
  876. return;
  877. }