XKBAlloc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <os.h>
  34. #include <string.h>
  35. /***===================================================================***/
  36. /*ARGSUSED*/ Status
  37. XkbAllocCompatMap(XkbDescPtr xkb, unsigned which, unsigned nSI)
  38. {
  39. XkbCompatMapPtr compat;
  40. XkbSymInterpretRec *prev_interpret;
  41. if (!xkb)
  42. return BadMatch;
  43. if (xkb->compat) {
  44. if (xkb->compat->size_si >= nSI)
  45. return Success;
  46. compat = xkb->compat;
  47. compat->size_si = nSI;
  48. if (compat->sym_interpret == NULL)
  49. compat->num_si = 0;
  50. prev_interpret = compat->sym_interpret;
  51. compat->sym_interpret = realloc(compat->sym_interpret,
  52. nSI * sizeof(XkbSymInterpretRec));
  53. if (compat->sym_interpret == NULL) {
  54. free(prev_interpret);
  55. compat->size_si = compat->num_si = 0;
  56. return BadAlloc;
  57. }
  58. if (compat->num_si != 0) {
  59. memset(&compat->sym_interpret[compat->num_si], 0,
  60. (compat->size_si -
  61. compat->num_si) * sizeof(XkbSymInterpretRec));
  62. }
  63. return Success;
  64. }
  65. compat = calloc(1, sizeof(XkbCompatMapRec));
  66. if (compat == NULL)
  67. return BadAlloc;
  68. if (nSI > 0) {
  69. compat->sym_interpret = calloc(nSI, sizeof(XkbSymInterpretRec));
  70. if (!compat->sym_interpret) {
  71. free(compat);
  72. return BadAlloc;
  73. }
  74. }
  75. compat->size_si = nSI;
  76. compat->num_si = 0;
  77. memset((char *) &compat->groups[0], 0,
  78. XkbNumKbdGroups * sizeof(XkbModsRec));
  79. xkb->compat = compat;
  80. return Success;
  81. }
  82. void
  83. XkbFreeCompatMap(XkbDescPtr xkb, unsigned which, Bool freeMap)
  84. {
  85. register XkbCompatMapPtr compat;
  86. if ((xkb == NULL) || (xkb->compat == NULL))
  87. return;
  88. compat = xkb->compat;
  89. if (freeMap)
  90. which = XkbAllCompatMask;
  91. if (which & XkbGroupCompatMask)
  92. memset((char *) &compat->groups[0], 0,
  93. XkbNumKbdGroups * sizeof(XkbModsRec));
  94. if (which & XkbSymInterpMask) {
  95. if ((compat->sym_interpret) && (compat->size_si > 0))
  96. free(compat->sym_interpret);
  97. compat->size_si = compat->num_si = 0;
  98. compat->sym_interpret = NULL;
  99. }
  100. if (freeMap) {
  101. free(compat);
  102. xkb->compat = NULL;
  103. }
  104. return;
  105. }
  106. /***===================================================================***/
  107. Status
  108. XkbAllocNames(XkbDescPtr xkb, unsigned which, int nTotalRG, int nTotalAliases)
  109. {
  110. XkbNamesPtr names;
  111. if (xkb == NULL)
  112. return BadMatch;
  113. if (xkb->names == NULL) {
  114. xkb->names = calloc(1, sizeof(XkbNamesRec));
  115. if (xkb->names == NULL)
  116. return BadAlloc;
  117. }
  118. names = xkb->names;
  119. if ((which & XkbKTLevelNamesMask) && (xkb->map != NULL) &&
  120. (xkb->map->types != NULL)) {
  121. register int i;
  122. XkbKeyTypePtr type;
  123. type = xkb->map->types;
  124. for (i = 0; i < xkb->map->num_types; i++, type++) {
  125. if (type->level_names == NULL) {
  126. type->level_names = calloc(type->num_levels, sizeof(Atom));
  127. if (type->level_names == NULL)
  128. return BadAlloc;
  129. }
  130. }
  131. }
  132. if ((which & XkbKeyNamesMask) && (names->keys == NULL)) {
  133. if ((!XkbIsLegalKeycode(xkb->min_key_code)) ||
  134. (!XkbIsLegalKeycode(xkb->max_key_code)) ||
  135. (xkb->max_key_code < xkb->min_key_code))
  136. return BadValue;
  137. names->keys = calloc((xkb->max_key_code + 1), sizeof(XkbKeyNameRec));
  138. if (names->keys == NULL)
  139. return BadAlloc;
  140. }
  141. if ((which & XkbKeyAliasesMask) && (nTotalAliases > 0)) {
  142. if (names->key_aliases == NULL) {
  143. names->key_aliases = calloc(nTotalAliases, sizeof(XkbKeyAliasRec));
  144. }
  145. else if (nTotalAliases > names->num_key_aliases) {
  146. XkbKeyAliasRec *prev_aliases = names->key_aliases;
  147. names->key_aliases = realloc(names->key_aliases,
  148. nTotalAliases *
  149. sizeof(XkbKeyAliasRec));
  150. if (names->key_aliases != NULL) {
  151. memset(&names->key_aliases[names->num_key_aliases], 0,
  152. (nTotalAliases -
  153. names->num_key_aliases) * sizeof(XkbKeyAliasRec));
  154. }
  155. else {
  156. free(prev_aliases);
  157. }
  158. }
  159. if (names->key_aliases == NULL) {
  160. names->num_key_aliases = 0;
  161. return BadAlloc;
  162. }
  163. names->num_key_aliases = nTotalAliases;
  164. }
  165. if ((which & XkbRGNamesMask) && (nTotalRG > 0)) {
  166. if (names->radio_groups == NULL) {
  167. names->radio_groups = calloc(nTotalRG, sizeof(Atom));
  168. }
  169. else if (nTotalRG > names->num_rg) {
  170. Atom *prev_radio_groups = names->radio_groups;
  171. names->radio_groups = realloc(names->radio_groups,
  172. nTotalRG * sizeof(Atom));
  173. if (names->radio_groups != NULL) {
  174. memset(&names->radio_groups[names->num_rg], 0,
  175. (nTotalRG - names->num_rg) * sizeof(Atom));
  176. }
  177. else {
  178. free(prev_radio_groups);
  179. }
  180. }
  181. if (names->radio_groups == NULL)
  182. return BadAlloc;
  183. names->num_rg = nTotalRG;
  184. }
  185. return Success;
  186. }
  187. void
  188. XkbFreeNames(XkbDescPtr xkb, unsigned which, Bool freeMap)
  189. {
  190. XkbNamesPtr names;
  191. if ((xkb == NULL) || (xkb->names == NULL))
  192. return;
  193. names = xkb->names;
  194. if (freeMap)
  195. which = XkbAllNamesMask;
  196. if (which & XkbKTLevelNamesMask) {
  197. XkbClientMapPtr map = xkb->map;
  198. if ((map != NULL) && (map->types != NULL)) {
  199. register int i;
  200. register XkbKeyTypePtr type;
  201. type = map->types;
  202. for (i = 0; i < map->num_types; i++, type++) {
  203. free(type->level_names);
  204. type->level_names = NULL;
  205. }
  206. }
  207. }
  208. if ((which & XkbKeyNamesMask) && (names->keys != NULL)) {
  209. free(names->keys);
  210. names->keys = NULL;
  211. names->num_keys = 0;
  212. }
  213. if ((which & XkbKeyAliasesMask) && (names->key_aliases)) {
  214. free(names->key_aliases);
  215. names->key_aliases = NULL;
  216. names->num_key_aliases = 0;
  217. }
  218. if ((which & XkbRGNamesMask) && (names->radio_groups)) {
  219. free(names->radio_groups);
  220. names->radio_groups = NULL;
  221. names->num_rg = 0;
  222. }
  223. if (freeMap) {
  224. free(names);
  225. xkb->names = NULL;
  226. }
  227. return;
  228. }
  229. /***===================================================================***/
  230. /*ARGSUSED*/ Status
  231. XkbAllocControls(XkbDescPtr xkb, unsigned which)
  232. {
  233. if (xkb == NULL)
  234. return BadMatch;
  235. if (xkb->ctrls == NULL) {
  236. xkb->ctrls = calloc(1, sizeof(XkbControlsRec));
  237. if (!xkb->ctrls)
  238. return BadAlloc;
  239. }
  240. return Success;
  241. }
  242. /*ARGSUSED*/ static void
  243. XkbFreeControls(XkbDescPtr xkb, unsigned which, Bool freeMap)
  244. {
  245. if (freeMap && (xkb != NULL) && (xkb->ctrls != NULL)) {
  246. free(xkb->ctrls);
  247. xkb->ctrls = NULL;
  248. }
  249. return;
  250. }
  251. /***===================================================================***/
  252. Status
  253. XkbAllocIndicatorMaps(XkbDescPtr xkb)
  254. {
  255. if (xkb == NULL)
  256. return BadMatch;
  257. if (xkb->indicators == NULL) {
  258. xkb->indicators = calloc(1, sizeof(XkbIndicatorRec));
  259. if (!xkb->indicators)
  260. return BadAlloc;
  261. }
  262. return Success;
  263. }
  264. static void
  265. XkbFreeIndicatorMaps(XkbDescPtr xkb)
  266. {
  267. if ((xkb != NULL) && (xkb->indicators != NULL)) {
  268. free(xkb->indicators);
  269. xkb->indicators = NULL;
  270. }
  271. return;
  272. }
  273. /***====================================================================***/
  274. XkbDescRec *
  275. XkbAllocKeyboard(void)
  276. {
  277. XkbDescRec *xkb;
  278. xkb = calloc(1, sizeof(XkbDescRec));
  279. if (xkb)
  280. xkb->device_spec = XkbUseCoreKbd;
  281. return xkb;
  282. }
  283. void
  284. XkbFreeKeyboard(XkbDescPtr xkb, unsigned which, Bool freeAll)
  285. {
  286. if (xkb == NULL)
  287. return;
  288. if (freeAll)
  289. which = XkbAllComponentsMask;
  290. if (which & XkbClientMapMask)
  291. XkbFreeClientMap(xkb, XkbAllClientInfoMask, TRUE);
  292. if (which & XkbServerMapMask)
  293. XkbFreeServerMap(xkb, XkbAllServerInfoMask, TRUE);
  294. if (which & XkbCompatMapMask)
  295. XkbFreeCompatMap(xkb, XkbAllCompatMask, TRUE);
  296. if (which & XkbIndicatorMapMask)
  297. XkbFreeIndicatorMaps(xkb);
  298. if (which & XkbNamesMask)
  299. XkbFreeNames(xkb, XkbAllNamesMask, TRUE);
  300. if ((which & XkbGeometryMask) && (xkb->geom != NULL)) {
  301. XkbFreeGeometry(xkb->geom, XkbGeomAllMask, TRUE);
  302. /* PERHAPS BONGHITS etc */
  303. xkb->geom = NULL;
  304. }
  305. if (which & XkbControlsMask)
  306. XkbFreeControls(xkb, XkbAllControlsMask, TRUE);
  307. if (freeAll)
  308. free(xkb);
  309. return;
  310. }
  311. /***====================================================================***/
  312. void
  313. XkbFreeComponentNames(XkbComponentNamesPtr names, Bool freeNames)
  314. {
  315. if (names) {
  316. free(names->keycodes);
  317. free(names->types);
  318. free(names->compat);
  319. free(names->symbols);
  320. free(names->geometry);
  321. memset(names, 0, sizeof(XkbComponentNamesRec));
  322. }
  323. if (freeNames)
  324. free(names);
  325. }