XKBGAlloc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /************************************************************
  2. Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
  3. Permission to use, copy, modify, and distribute this
  4. software and its documentation for any purpose and without
  5. fee is hereby granted, provided that the above copyright
  6. notice appear in all copies and that both that copyright
  7. notice and this permission notice appear in supporting
  8. documentation, and that the name of Silicon Graphics not be
  9. used in advertising or publicity pertaining to distribution
  10. of the software without specific prior written permission.
  11. Silicon Graphics makes no representation about the suitability
  12. of this software for any purpose. It is provided "as is"
  13. without any express or implied warranty.
  14. SILICON GRAPHICS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  16. AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
  17. GRAPHICS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  18. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19. DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  20. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
  21. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. ********************************************************/
  23. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <stdio.h>
  27. #include <X11/X.h>
  28. #include <X11/Xproto.h>
  29. #include "misc.h"
  30. #include "inputstr.h"
  31. #include <xkbsrv.h>
  32. #include "xkbgeom.h"
  33. /***====================================================================***/
  34. static void
  35. _XkbFreeGeomLeafElems(Bool freeAll,
  36. int first,
  37. int count,
  38. unsigned short *num_inout,
  39. unsigned short *sz_inout,
  40. char **elems, unsigned int elem_sz)
  41. {
  42. if ((freeAll) || (*elems == NULL)) {
  43. *num_inout = *sz_inout = 0;
  44. free(*elems);
  45. *elems = NULL;
  46. return;
  47. }
  48. if ((first >= (*num_inout)) || (first < 0) || (count < 1))
  49. return;
  50. if (first + count >= (*num_inout)) {
  51. /* truncating the array is easy */
  52. (*num_inout) = first;
  53. }
  54. else {
  55. char *ptr;
  56. int extra;
  57. ptr = *elems;
  58. extra = ((*num_inout) - (first + count)) * elem_sz;
  59. if (extra > 0)
  60. memmove(&ptr[first * elem_sz], &ptr[(first + count) * elem_sz],
  61. extra);
  62. (*num_inout) -= count;
  63. }
  64. return;
  65. }
  66. typedef void (*ContentsClearFunc) (char * /* priv */
  67. );
  68. static void
  69. _XkbFreeGeomNonLeafElems(Bool freeAll,
  70. int first,
  71. int count,
  72. unsigned short *num_inout,
  73. unsigned short *sz_inout,
  74. char **elems,
  75. unsigned int elem_sz, ContentsClearFunc freeFunc)
  76. {
  77. register int i;
  78. register char *ptr;
  79. if (freeAll) {
  80. first = 0;
  81. count = (*num_inout);
  82. }
  83. else if ((first >= (*num_inout)) || (first < 0) || (count < 1))
  84. return;
  85. else if (first + count > (*num_inout))
  86. count = (*num_inout) - first;
  87. if (*elems == NULL)
  88. return;
  89. if (freeFunc) {
  90. ptr = *elems;
  91. ptr += first * elem_sz;
  92. for (i = 0; i < count; i++) {
  93. (*freeFunc) (ptr);
  94. ptr += elem_sz;
  95. }
  96. }
  97. if (freeAll) {
  98. (*num_inout) = (*sz_inout) = 0;
  99. free(*elems);
  100. *elems = NULL;
  101. }
  102. else if (first + count >= (*num_inout))
  103. *num_inout = first;
  104. else {
  105. i = ((*num_inout) - (first + count)) * elem_sz;
  106. ptr = *elems;
  107. memmove(&ptr[first * elem_sz], &ptr[(first + count) * elem_sz], i);
  108. (*num_inout) -= count;
  109. }
  110. return;
  111. }
  112. /***====================================================================***/
  113. static void
  114. _XkbClearProperty(char *prop_in)
  115. {
  116. XkbPropertyPtr prop = (XkbPropertyPtr) prop_in;
  117. free(prop->name);
  118. prop->name = NULL;
  119. free(prop->value);
  120. prop->value = NULL;
  121. return;
  122. }
  123. void
  124. XkbFreeGeomProperties(XkbGeometryPtr geom, int first, int count, Bool freeAll)
  125. {
  126. _XkbFreeGeomNonLeafElems(freeAll, first, count,
  127. &geom->num_properties, &geom->sz_properties,
  128. (char **) &geom->properties,
  129. sizeof(XkbPropertyRec), _XkbClearProperty);
  130. return;
  131. }
  132. /***====================================================================***/
  133. void
  134. XkbFreeGeomKeyAliases(XkbGeometryPtr geom, int first, int count, Bool freeAll)
  135. {
  136. _XkbFreeGeomLeafElems(freeAll, first, count,
  137. &geom->num_key_aliases, &geom->sz_key_aliases,
  138. (char **) &geom->key_aliases, sizeof(XkbKeyAliasRec));
  139. return;
  140. }
  141. /***====================================================================***/
  142. static void
  143. _XkbClearColor(char *color_in)
  144. {
  145. XkbColorPtr color = (XkbColorPtr) color_in;
  146. free(color->spec);
  147. return;
  148. }
  149. void
  150. XkbFreeGeomColors(XkbGeometryPtr geom, int first, int count, Bool freeAll)
  151. {
  152. _XkbFreeGeomNonLeafElems(freeAll, first, count,
  153. &geom->num_colors, &geom->sz_colors,
  154. (char **) &geom->colors,
  155. sizeof(XkbColorRec), _XkbClearColor);
  156. return;
  157. }
  158. /***====================================================================***/
  159. void
  160. XkbFreeGeomPoints(XkbOutlinePtr outline, int first, int count, Bool freeAll)
  161. {
  162. _XkbFreeGeomLeafElems(freeAll, first, count,
  163. &outline->num_points, &outline->sz_points,
  164. (char **) &outline->points, sizeof(XkbPointRec));
  165. return;
  166. }
  167. /***====================================================================***/
  168. static void
  169. _XkbClearOutline(char *outline_in)
  170. {
  171. XkbOutlinePtr outline = (XkbOutlinePtr) outline_in;
  172. if (outline->points != NULL)
  173. XkbFreeGeomPoints(outline, 0, outline->num_points, TRUE);
  174. return;
  175. }
  176. void
  177. XkbFreeGeomOutlines(XkbShapePtr shape, int first, int count, Bool freeAll)
  178. {
  179. _XkbFreeGeomNonLeafElems(freeAll, first, count,
  180. &shape->num_outlines, &shape->sz_outlines,
  181. (char **) &shape->outlines,
  182. sizeof(XkbOutlineRec), _XkbClearOutline);
  183. return;
  184. }
  185. /***====================================================================***/
  186. static void
  187. _XkbClearShape(char *shape_in)
  188. {
  189. XkbShapePtr shape = (XkbShapePtr) shape_in;
  190. if (shape->outlines)
  191. XkbFreeGeomOutlines(shape, 0, shape->num_outlines, TRUE);
  192. return;
  193. }
  194. void
  195. XkbFreeGeomShapes(XkbGeometryPtr geom, int first, int count, Bool freeAll)
  196. {
  197. _XkbFreeGeomNonLeafElems(freeAll, first, count,
  198. &geom->num_shapes, &geom->sz_shapes,
  199. (char **) &geom->shapes,
  200. sizeof(XkbShapeRec), _XkbClearShape);
  201. return;
  202. }
  203. /***====================================================================***/
  204. void
  205. XkbFreeGeomOverlayKeys(XkbOverlayRowPtr row, int first, int count, Bool freeAll)
  206. {
  207. _XkbFreeGeomLeafElems(freeAll, first, count,
  208. &row->num_keys, &row->sz_keys,
  209. (char **) &row->keys, sizeof(XkbOverlayKeyRec));
  210. return;
  211. }
  212. /***====================================================================***/
  213. void
  214. XkbFreeGeomKeys(XkbRowPtr row, int first, int count, Bool freeAll)
  215. {
  216. _XkbFreeGeomLeafElems(freeAll, first, count,
  217. &row->num_keys, &row->sz_keys,
  218. (char **) &row->keys, sizeof(XkbKeyRec));
  219. return;
  220. }
  221. /***====================================================================***/
  222. static void
  223. _XkbClearRow(char *row_in)
  224. {
  225. XkbRowPtr row = (XkbRowPtr) row_in;
  226. if (row->keys != NULL)
  227. XkbFreeGeomKeys(row, 0, row->num_keys, TRUE);
  228. return;
  229. }
  230. void
  231. XkbFreeGeomRows(XkbSectionPtr section, int first, int count, Bool freeAll)
  232. {
  233. _XkbFreeGeomNonLeafElems(freeAll, first, count,
  234. &section->num_rows, &section->sz_rows,
  235. (char **) &section->rows,
  236. sizeof(XkbRowRec), _XkbClearRow);
  237. }
  238. /***====================================================================***/
  239. static void
  240. _XkbClearSection(char *section_in)
  241. {
  242. XkbSectionPtr section = (XkbSectionPtr) section_in;
  243. if (section->rows != NULL)
  244. XkbFreeGeomRows(section, 0, section->num_rows, TRUE);
  245. if (section->doodads != NULL) {
  246. XkbFreeGeomDoodads(section->doodads, section->num_doodads, TRUE);
  247. section->doodads = NULL;
  248. }
  249. return;
  250. }
  251. void
  252. XkbFreeGeomSections(XkbGeometryPtr geom, int first, int count, Bool freeAll)
  253. {
  254. _XkbFreeGeomNonLeafElems(freeAll, first, count,
  255. &geom->num_sections, &geom->sz_sections,
  256. (char **) &geom->sections,
  257. sizeof(XkbSectionRec), _XkbClearSection);
  258. return;
  259. }
  260. /***====================================================================***/
  261. static void
  262. _XkbClearDoodad(char *doodad_in)
  263. {
  264. XkbDoodadPtr doodad = (XkbDoodadPtr) doodad_in;
  265. switch (doodad->any.type) {
  266. case XkbTextDoodad:
  267. {
  268. free(doodad->text.text);
  269. doodad->text.text = NULL;
  270. free(doodad->text.font);
  271. doodad->text.font = NULL;
  272. }
  273. break;
  274. case XkbLogoDoodad:
  275. {
  276. free(doodad->logo.logo_name);
  277. doodad->logo.logo_name = NULL;
  278. }
  279. break;
  280. }
  281. return;
  282. }
  283. void
  284. XkbFreeGeomDoodads(XkbDoodadPtr doodads, int nDoodads, Bool freeAll)
  285. {
  286. register int i;
  287. register XkbDoodadPtr doodad;
  288. if (doodads) {
  289. for (i = 0, doodad = doodads; i < nDoodads; i++, doodad++) {
  290. _XkbClearDoodad((char *) doodad);
  291. }
  292. if (freeAll)
  293. free(doodads);
  294. }
  295. return;
  296. }
  297. void
  298. XkbFreeGeometry(XkbGeometryPtr geom, unsigned which, Bool freeMap)
  299. {
  300. if (geom == NULL)
  301. return;
  302. if (freeMap)
  303. which = XkbGeomAllMask;
  304. if ((which & XkbGeomPropertiesMask) && (geom->properties != NULL))
  305. XkbFreeGeomProperties(geom, 0, geom->num_properties, TRUE);
  306. if ((which & XkbGeomColorsMask) && (geom->colors != NULL))
  307. XkbFreeGeomColors(geom, 0, geom->num_colors, TRUE);
  308. if ((which & XkbGeomShapesMask) && (geom->shapes != NULL))
  309. XkbFreeGeomShapes(geom, 0, geom->num_shapes, TRUE);
  310. if ((which & XkbGeomSectionsMask) && (geom->sections != NULL))
  311. XkbFreeGeomSections(geom, 0, geom->num_sections, TRUE);
  312. if ((which & XkbGeomDoodadsMask) && (geom->doodads != NULL)) {
  313. XkbFreeGeomDoodads(geom->doodads, geom->num_doodads, TRUE);
  314. geom->doodads = NULL;
  315. geom->num_doodads = geom->sz_doodads = 0;
  316. }
  317. if ((which & XkbGeomKeyAliasesMask) && (geom->key_aliases != NULL))
  318. XkbFreeGeomKeyAliases(geom, 0, geom->num_key_aliases, TRUE);
  319. if (freeMap) {
  320. free(geom->label_font);
  321. geom->label_font = NULL;
  322. free(geom);
  323. }
  324. return;
  325. }
  326. /***====================================================================***/
  327. /**
  328. * Resize and clear an XKB geometry item array. The array size may
  329. * grow or shrink unlike in _XkbGeomAlloc.
  330. *
  331. * @param buffer[in,out] buffer to reallocate and clear
  332. * @param szItems[in] currently allocated item count for "buffer"
  333. * @param nrItems[in] required item count for "buffer"
  334. * @param itemSize[in] size of a single item in "buffer"
  335. * @param clearance[in] items to clear after reallocation
  336. *
  337. * @see _XkbGeomAlloc
  338. *
  339. * @return TRUE if reallocation succeeded. Otherwise FALSE is returned
  340. * and contents of "buffer" aren't touched.
  341. */
  342. Bool
  343. XkbGeomRealloc(void **buffer, int szItems, int nrItems,
  344. int itemSize, XkbGeomClearance clearance)
  345. {
  346. void *items;
  347. int clearBegin;
  348. /* Check validity of arguments. */
  349. if (!buffer)
  350. return FALSE;
  351. items = *buffer;
  352. if (!((items && (szItems > 0)) || (!items && !szItems)))
  353. return FALSE;
  354. /* Check if there is need to resize. */
  355. if (nrItems != szItems)
  356. if (!(items = realloc(items, nrItems * itemSize)))
  357. return FALSE;
  358. /* Clear specified items to zero. */
  359. switch (clearance) {
  360. case XKB_GEOM_CLEAR_EXCESS:
  361. clearBegin = szItems;
  362. break;
  363. case XKB_GEOM_CLEAR_ALL:
  364. clearBegin = 0;
  365. break;
  366. case XKB_GEOM_CLEAR_NONE:
  367. default:
  368. clearBegin = nrItems;
  369. break;
  370. }
  371. if (items && (clearBegin < nrItems))
  372. memset((char *) items + (clearBegin * itemSize), 0,
  373. (nrItems - clearBegin) * itemSize);
  374. *buffer = items;
  375. return TRUE;
  376. }
  377. static Status
  378. _XkbGeomAlloc(void **old,
  379. unsigned short *num,
  380. unsigned short *total, int num_new, size_t sz_elem)
  381. {
  382. if (num_new < 1)
  383. return Success;
  384. if ((*old) == NULL)
  385. *num = *total = 0;
  386. if ((*num) + num_new <= (*total))
  387. return Success;
  388. *total = (*num) + num_new;
  389. if (!XkbGeomRealloc(old, *num, *total, sz_elem, XKB_GEOM_CLEAR_EXCESS)) {
  390. free(*old);
  391. (*old) = NULL;
  392. *total = *num = 0;
  393. return BadAlloc;
  394. }
  395. return Success;
  396. }
  397. #define _XkbAllocProps(g,n) _XkbGeomAlloc((void *)&(g)->properties,\
  398. &(g)->num_properties,&(g)->sz_properties,\
  399. (n),sizeof(XkbPropertyRec))
  400. #define _XkbAllocColors(g,n) _XkbGeomAlloc((void *)&(g)->colors,\
  401. &(g)->num_colors,&(g)->sz_colors,\
  402. (n),sizeof(XkbColorRec))
  403. #define _XkbAllocShapes(g,n) _XkbGeomAlloc((void *)&(g)->shapes,\
  404. &(g)->num_shapes,&(g)->sz_shapes,\
  405. (n),sizeof(XkbShapeRec))
  406. #define _XkbAllocSections(g,n) _XkbGeomAlloc((void *)&(g)->sections,\
  407. &(g)->num_sections,&(g)->sz_sections,\
  408. (n),sizeof(XkbSectionRec))
  409. #define _XkbAllocDoodads(g,n) _XkbGeomAlloc((void *)&(g)->doodads,\
  410. &(g)->num_doodads,&(g)->sz_doodads,\
  411. (n),sizeof(XkbDoodadRec))
  412. #define _XkbAllocKeyAliases(g,n) _XkbGeomAlloc((void *)&(g)->key_aliases,\
  413. &(g)->num_key_aliases,&(g)->sz_key_aliases,\
  414. (n),sizeof(XkbKeyAliasRec))
  415. #define _XkbAllocOutlines(s,n) _XkbGeomAlloc((void *)&(s)->outlines,\
  416. &(s)->num_outlines,&(s)->sz_outlines,\
  417. (n),sizeof(XkbOutlineRec))
  418. #define _XkbAllocRows(s,n) _XkbGeomAlloc((void *)&(s)->rows,\
  419. &(s)->num_rows,&(s)->sz_rows,\
  420. (n),sizeof(XkbRowRec))
  421. #define _XkbAllocPoints(o,n) _XkbGeomAlloc((void *)&(o)->points,\
  422. &(o)->num_points,&(o)->sz_points,\
  423. (n),sizeof(XkbPointRec))
  424. #define _XkbAllocKeys(r,n) _XkbGeomAlloc((void *)&(r)->keys,\
  425. &(r)->num_keys,&(r)->sz_keys,\
  426. (n),sizeof(XkbKeyRec))
  427. #define _XkbAllocOverlays(s,n) _XkbGeomAlloc((void *)&(s)->overlays,\
  428. &(s)->num_overlays,&(s)->sz_overlays,\
  429. (n),sizeof(XkbOverlayRec))
  430. #define _XkbAllocOverlayRows(o,n) _XkbGeomAlloc((void *)&(o)->rows,\
  431. &(o)->num_rows,&(o)->sz_rows,\
  432. (n),sizeof(XkbOverlayRowRec))
  433. #define _XkbAllocOverlayKeys(r,n) _XkbGeomAlloc((void *)&(r)->keys,\
  434. &(r)->num_keys,&(r)->sz_keys,\
  435. (n),sizeof(XkbOverlayKeyRec))
  436. Status
  437. XkbAllocGeometry(XkbDescPtr xkb, XkbGeometrySizesPtr sizes)
  438. {
  439. XkbGeometryPtr geom;
  440. Status rtrn;
  441. if (xkb->geom == NULL) {
  442. xkb->geom = calloc(1, sizeof(XkbGeometryRec));
  443. if (!xkb->geom)
  444. return BadAlloc;
  445. }
  446. geom = xkb->geom;
  447. if ((sizes->which & XkbGeomPropertiesMask) &&
  448. ((rtrn = _XkbAllocProps(geom, sizes->num_properties)) != Success)) {
  449. goto BAIL;
  450. }
  451. if ((sizes->which & XkbGeomColorsMask) &&
  452. ((rtrn = _XkbAllocColors(geom, sizes->num_colors)) != Success)) {
  453. goto BAIL;
  454. }
  455. if ((sizes->which & XkbGeomShapesMask) &&
  456. ((rtrn = _XkbAllocShapes(geom, sizes->num_shapes)) != Success)) {
  457. goto BAIL;
  458. }
  459. if ((sizes->which & XkbGeomSectionsMask) &&
  460. ((rtrn = _XkbAllocSections(geom, sizes->num_sections)) != Success)) {
  461. goto BAIL;
  462. }
  463. if ((sizes->which & XkbGeomDoodadsMask) &&
  464. ((rtrn = _XkbAllocDoodads(geom, sizes->num_doodads)) != Success)) {
  465. goto BAIL;
  466. }
  467. if ((sizes->which & XkbGeomKeyAliasesMask) &&
  468. ((rtrn =
  469. _XkbAllocKeyAliases(geom, sizes->num_key_aliases)) != Success)) {
  470. goto BAIL;
  471. }
  472. return Success;
  473. BAIL:
  474. XkbFreeGeometry(geom, XkbGeomAllMask, TRUE);
  475. xkb->geom = NULL;
  476. return rtrn;
  477. }
  478. /***====================================================================***/
  479. XkbPropertyPtr
  480. XkbAddGeomProperty(XkbGeometryPtr geom, char *name, char *value)
  481. {
  482. register int i;
  483. register XkbPropertyPtr prop;
  484. if ((!geom) || (!name) || (!value))
  485. return NULL;
  486. for (i = 0, prop = geom->properties; i < geom->num_properties; i++, prop++) {
  487. if ((prop->name) && (strcmp(name, prop->name) == 0)) {
  488. free(prop->value);
  489. prop->value = strdup(value);
  490. return prop;
  491. }
  492. }
  493. if ((geom->num_properties >= geom->sz_properties) &&
  494. (_XkbAllocProps(geom, 1) != Success)) {
  495. return NULL;
  496. }
  497. prop = &geom->properties[geom->num_properties];
  498. prop->name = strdup(name);
  499. if (!prop->name)
  500. return NULL;
  501. prop->value = strdup(value);
  502. if (!prop->value) {
  503. free(prop->name);
  504. prop->name = NULL;
  505. return NULL;
  506. }
  507. geom->num_properties++;
  508. return prop;
  509. }
  510. XkbKeyAliasPtr
  511. XkbAddGeomKeyAlias(XkbGeometryPtr geom, char *aliasStr, char *realStr)
  512. {
  513. register int i;
  514. register XkbKeyAliasPtr alias;
  515. if ((!geom) || (!aliasStr) || (!realStr) || (!aliasStr[0]) || (!realStr[0]))
  516. return NULL;
  517. for (i = 0, alias = geom->key_aliases; i < geom->num_key_aliases;
  518. i++, alias++) {
  519. if (strncmp(alias->alias, aliasStr, XkbKeyNameLength) == 0) {
  520. memset(alias->real, 0, XkbKeyNameLength);
  521. strncpy(alias->real, realStr, XkbKeyNameLength);
  522. return alias;
  523. }
  524. }
  525. if ((geom->num_key_aliases >= geom->sz_key_aliases) &&
  526. (_XkbAllocKeyAliases(geom, 1) != Success)) {
  527. return NULL;
  528. }
  529. alias = &geom->key_aliases[geom->num_key_aliases];
  530. memset(alias, 0, sizeof(XkbKeyAliasRec));
  531. strncpy(alias->alias, aliasStr, XkbKeyNameLength);
  532. strncpy(alias->real, realStr, XkbKeyNameLength);
  533. geom->num_key_aliases++;
  534. return alias;
  535. }
  536. XkbColorPtr
  537. XkbAddGeomColor(XkbGeometryPtr geom, char *spec, unsigned int pixel)
  538. {
  539. register int i;
  540. register XkbColorPtr color;
  541. if ((!geom) || (!spec))
  542. return NULL;
  543. for (i = 0, color = geom->colors; i < geom->num_colors; i++, color++) {
  544. if ((color->spec) && (strcmp(color->spec, spec) == 0)) {
  545. color->pixel = pixel;
  546. return color;
  547. }
  548. }
  549. if ((geom->num_colors >= geom->sz_colors) &&
  550. (_XkbAllocColors(geom, 1) != Success)) {
  551. return NULL;
  552. }
  553. color = &geom->colors[geom->num_colors];
  554. color->pixel = pixel;
  555. color->spec = strdup(spec);
  556. if (!color->spec)
  557. return NULL;
  558. geom->num_colors++;
  559. return color;
  560. }
  561. XkbOutlinePtr
  562. XkbAddGeomOutline(XkbShapePtr shape, int sz_points)
  563. {
  564. XkbOutlinePtr outline;
  565. if ((!shape) || (sz_points < 0))
  566. return NULL;
  567. if ((shape->num_outlines >= shape->sz_outlines) &&
  568. (_XkbAllocOutlines(shape, 1) != Success)) {
  569. return NULL;
  570. }
  571. outline = &shape->outlines[shape->num_outlines];
  572. memset(outline, 0, sizeof(XkbOutlineRec));
  573. if ((sz_points > 0) && (_XkbAllocPoints(outline, sz_points) != Success))
  574. return NULL;
  575. shape->num_outlines++;
  576. return outline;
  577. }
  578. XkbShapePtr
  579. XkbAddGeomShape(XkbGeometryPtr geom, Atom name, int sz_outlines)
  580. {
  581. XkbShapePtr shape;
  582. register int i;
  583. if ((!geom) || (!name) || (sz_outlines < 0))
  584. return NULL;
  585. if (geom->num_shapes > 0) {
  586. for (shape = geom->shapes, i = 0; i < geom->num_shapes; i++, shape++) {
  587. if (name == shape->name)
  588. return shape;
  589. }
  590. }
  591. if ((geom->num_shapes >= geom->sz_shapes) &&
  592. (_XkbAllocShapes(geom, 1) != Success))
  593. return NULL;
  594. shape = &geom->shapes[geom->num_shapes];
  595. memset(shape, 0, sizeof(XkbShapeRec));
  596. if ((sz_outlines > 0) && (_XkbAllocOutlines(shape, sz_outlines) != Success))
  597. return NULL;
  598. shape->name = name;
  599. shape->primary = shape->approx = NULL;
  600. geom->num_shapes++;
  601. return shape;
  602. }
  603. XkbKeyPtr
  604. XkbAddGeomKey(XkbRowPtr row)
  605. {
  606. XkbKeyPtr key;
  607. if (!row)
  608. return NULL;
  609. if ((row->num_keys >= row->sz_keys) && (_XkbAllocKeys(row, 1) != Success))
  610. return NULL;
  611. key = &row->keys[row->num_keys++];
  612. memset(key, 0, sizeof(XkbKeyRec));
  613. return key;
  614. }
  615. XkbRowPtr
  616. XkbAddGeomRow(XkbSectionPtr section, int sz_keys)
  617. {
  618. XkbRowPtr row;
  619. if ((!section) || (sz_keys < 0))
  620. return NULL;
  621. if ((section->num_rows >= section->sz_rows) &&
  622. (_XkbAllocRows(section, 1) != Success))
  623. return NULL;
  624. row = &section->rows[section->num_rows];
  625. memset(row, 0, sizeof(XkbRowRec));
  626. if ((sz_keys > 0) && (_XkbAllocKeys(row, sz_keys) != Success))
  627. return NULL;
  628. section->num_rows++;
  629. return row;
  630. }
  631. XkbSectionPtr
  632. XkbAddGeomSection(XkbGeometryPtr geom,
  633. Atom name, int sz_rows, int sz_doodads, int sz_over)
  634. {
  635. register int i;
  636. XkbSectionPtr section;
  637. if ((!geom) || (name == None) || (sz_rows < 0))
  638. return NULL;
  639. for (i = 0, section = geom->sections; i < geom->num_sections;
  640. i++, section++) {
  641. if (section->name != name)
  642. continue;
  643. if (((sz_rows > 0) && (_XkbAllocRows(section, sz_rows) != Success)) ||
  644. ((sz_doodads > 0) &&
  645. (_XkbAllocDoodads(section, sz_doodads) != Success)) ||
  646. ((sz_over > 0) && (_XkbAllocOverlays(section, sz_over) != Success)))
  647. return NULL;
  648. return section;
  649. }
  650. if ((geom->num_sections >= geom->sz_sections) &&
  651. (_XkbAllocSections(geom, 1) != Success))
  652. return NULL;
  653. section = &geom->sections[geom->num_sections];
  654. if ((sz_rows > 0) && (_XkbAllocRows(section, sz_rows) != Success))
  655. return NULL;
  656. if ((sz_doodads > 0) && (_XkbAllocDoodads(section, sz_doodads) != Success)) {
  657. if (section->rows) {
  658. free(section->rows);
  659. section->rows = NULL;
  660. section->sz_rows = section->num_rows = 0;
  661. }
  662. return NULL;
  663. }
  664. section->name = name;
  665. geom->num_sections++;
  666. return section;
  667. }
  668. XkbDoodadPtr
  669. XkbAddGeomDoodad(XkbGeometryPtr geom, XkbSectionPtr section, Atom name)
  670. {
  671. XkbDoodadPtr old, doodad;
  672. register int i, nDoodads;
  673. if ((!geom) || (name == None))
  674. return NULL;
  675. if ((section != NULL) && (section->num_doodads > 0)) {
  676. old = section->doodads;
  677. nDoodads = section->num_doodads;
  678. }
  679. else {
  680. old = geom->doodads;
  681. nDoodads = geom->num_doodads;
  682. }
  683. for (i = 0, doodad = old; i < nDoodads; i++, doodad++) {
  684. if (doodad->any.name == name)
  685. return doodad;
  686. }
  687. if (section) {
  688. if ((section->num_doodads >= geom->sz_doodads) &&
  689. (_XkbAllocDoodads(section, 1) != Success)) {
  690. return NULL;
  691. }
  692. doodad = &section->doodads[section->num_doodads++];
  693. }
  694. else {
  695. if ((geom->num_doodads >= geom->sz_doodads) &&
  696. (_XkbAllocDoodads(geom, 1) != Success))
  697. return NULL;
  698. doodad = &geom->doodads[geom->num_doodads++];
  699. }
  700. memset(doodad, 0, sizeof(XkbDoodadRec));
  701. doodad->any.name = name;
  702. return doodad;
  703. }
  704. XkbOverlayKeyPtr
  705. XkbAddGeomOverlayKey(XkbOverlayPtr overlay,
  706. XkbOverlayRowPtr row, char *over, char *under)
  707. {
  708. register int i;
  709. XkbOverlayKeyPtr key;
  710. XkbSectionPtr section;
  711. XkbRowPtr row_under;
  712. Bool found;
  713. if ((!overlay) || (!row) || (!over) || (!under))
  714. return NULL;
  715. section = overlay->section_under;
  716. if (row->row_under >= section->num_rows)
  717. return NULL;
  718. row_under = &section->rows[row->row_under];
  719. for (i = 0, found = FALSE; i < row_under->num_keys; i++) {
  720. if (strncmp(under, row_under->keys[i].name.name, XkbKeyNameLength) == 0) {
  721. found = TRUE;
  722. break;
  723. }
  724. }
  725. if (!found)
  726. return NULL;
  727. if ((row->num_keys >= row->sz_keys) &&
  728. (_XkbAllocOverlayKeys(row, 1) != Success))
  729. return NULL;
  730. key = &row->keys[row->num_keys];
  731. strncpy(key->under.name, under, XkbKeyNameLength);
  732. strncpy(key->over.name, over, XkbKeyNameLength);
  733. row->num_keys++;
  734. return key;
  735. }
  736. XkbOverlayRowPtr
  737. XkbAddGeomOverlayRow(XkbOverlayPtr overlay, int row_under, int sz_keys)
  738. {
  739. register int i;
  740. XkbOverlayRowPtr row;
  741. if ((!overlay) || (sz_keys < 0))
  742. return NULL;
  743. if (row_under >= overlay->section_under->num_rows)
  744. return NULL;
  745. for (i = 0; i < overlay->num_rows; i++) {
  746. if (overlay->rows[i].row_under == row_under) {
  747. row = &overlay->rows[i];
  748. if ((row->sz_keys < sz_keys) &&
  749. (_XkbAllocOverlayKeys(row, sz_keys) != Success)) {
  750. return NULL;
  751. }
  752. return &overlay->rows[i];
  753. }
  754. }
  755. if ((overlay->num_rows >= overlay->sz_rows) &&
  756. (_XkbAllocOverlayRows(overlay, 1) != Success))
  757. return NULL;
  758. row = &overlay->rows[overlay->num_rows];
  759. memset(row, 0, sizeof(XkbOverlayRowRec));
  760. if ((sz_keys > 0) && (_XkbAllocOverlayKeys(row, sz_keys) != Success))
  761. return NULL;
  762. row->row_under = row_under;
  763. overlay->num_rows++;
  764. return row;
  765. }
  766. XkbOverlayPtr
  767. XkbAddGeomOverlay(XkbSectionPtr section, Atom name, int sz_rows)
  768. {
  769. register int i;
  770. XkbOverlayPtr overlay;
  771. if ((!section) || (name == None) || (sz_rows == 0))
  772. return NULL;
  773. for (i = 0, overlay = section->overlays; i < section->num_overlays;
  774. i++, overlay++) {
  775. if (overlay->name == name) {
  776. if ((sz_rows > 0) &&
  777. (_XkbAllocOverlayRows(overlay, sz_rows) != Success))
  778. return NULL;
  779. return overlay;
  780. }
  781. }
  782. if ((section->num_overlays >= section->sz_overlays) &&
  783. (_XkbAllocOverlays(section, 1) != Success))
  784. return NULL;
  785. overlay = &section->overlays[section->num_overlays];
  786. if ((sz_rows > 0) && (_XkbAllocOverlayRows(overlay, sz_rows) != Success))
  787. return NULL;
  788. overlay->name = name;
  789. overlay->section_under = section;
  790. section->num_overlays++;
  791. return overlay;
  792. }