xkmread.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  1. /************************************************************
  2. Copyright (c) 1994 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/Xos.h>
  28. #include <X11/Xfuncs.h>
  29. #include <X11/X.h>
  30. #include <X11/Xproto.h>
  31. #include <X11/keysym.h>
  32. #include <X11/extensions/XKMformat.h>
  33. #include "misc.h"
  34. #include "inputstr.h"
  35. #include "xkbstr.h"
  36. #include "xkbsrv.h"
  37. #include "xkbgeom.h"
  38. Atom
  39. XkbInternAtom(char *str, Bool only_if_exists)
  40. {
  41. if (str == NULL)
  42. return None;
  43. return MakeAtom(str, strlen(str), !only_if_exists);
  44. }
  45. /***====================================================================***/
  46. static void *
  47. XkmInsureSize(void *oldPtr, int oldCount, int *newCountRtrn, int elemSize)
  48. {
  49. int newCount = *newCountRtrn;
  50. if (oldPtr == NULL) {
  51. if (newCount == 0)
  52. return NULL;
  53. oldPtr = calloc(newCount, elemSize);
  54. }
  55. else if (oldCount < newCount) {
  56. oldPtr = realloc(oldPtr, newCount * elemSize);
  57. if (oldPtr != NULL) {
  58. char *tmp = (char *) oldPtr;
  59. memset(&tmp[oldCount * elemSize], 0,
  60. (newCount - oldCount) * elemSize);
  61. }
  62. }
  63. else if (newCount < oldCount) {
  64. *newCountRtrn = oldCount;
  65. }
  66. return oldPtr;
  67. }
  68. #define XkmInsureTypedSize(p,o,n,t) ((p)=((t *)XkmInsureSize((char *)(p),(o),(n),sizeof(t))))
  69. static CARD8
  70. XkmGetCARD8(FILE * file, int *pNRead)
  71. {
  72. int tmp;
  73. tmp = getc(file);
  74. if (pNRead && (tmp != EOF))
  75. (*pNRead) += 1;
  76. return tmp;
  77. }
  78. static CARD16
  79. XkmGetCARD16(FILE * file, int *pNRead)
  80. {
  81. CARD16 val;
  82. if ((fread(&val, 2, 1, file) == 1) && (pNRead))
  83. (*pNRead) += 2;
  84. return val;
  85. }
  86. static CARD32
  87. XkmGetCARD32(FILE * file, int *pNRead)
  88. {
  89. CARD32 val;
  90. if ((fread(&val, 4, 1, file) == 1) && (pNRead))
  91. (*pNRead) += 4;
  92. return val;
  93. }
  94. static int
  95. XkmSkipPadding(FILE * file, unsigned pad)
  96. {
  97. register int i, nRead = 0;
  98. for (i = 0; i < pad; i++) {
  99. if (getc(file) != EOF)
  100. nRead++;
  101. }
  102. return nRead;
  103. }
  104. static int
  105. XkmGetCountedString(FILE * file, char *str, int max_len)
  106. {
  107. int count, nRead = 0;
  108. count = XkmGetCARD16(file, &nRead);
  109. if (count > 0) {
  110. int tmp;
  111. if (count > max_len) {
  112. tmp = fread(str, 1, max_len, file);
  113. while (tmp < count) {
  114. if ((getc(file)) != EOF)
  115. tmp++;
  116. else
  117. break;
  118. }
  119. }
  120. else {
  121. tmp = fread(str, 1, count, file);
  122. }
  123. nRead += tmp;
  124. }
  125. if (count >= max_len)
  126. str[max_len - 1] = '\0';
  127. else
  128. str[count] = '\0';
  129. count = XkbPaddedSize(nRead) - nRead;
  130. if (count > 0)
  131. nRead += XkmSkipPadding(file, count);
  132. return nRead;
  133. }
  134. /***====================================================================***/
  135. static int
  136. ReadXkmVirtualMods(FILE * file, XkbDescPtr xkb, XkbChangesPtr changes)
  137. {
  138. register unsigned int i, bit;
  139. unsigned int bound, named, tmp;
  140. int nRead = 0;
  141. if (XkbAllocServerMap(xkb, XkbVirtualModsMask, 0) != Success) {
  142. _XkbLibError(_XkbErrBadAlloc, "ReadXkmVirtualMods", 0);
  143. return -1;
  144. }
  145. bound = XkmGetCARD16(file, &nRead);
  146. named = XkmGetCARD16(file, &nRead);
  147. for (i = tmp = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
  148. if (bound & bit) {
  149. xkb->server->vmods[i] = XkmGetCARD8(file, &nRead);
  150. if (changes)
  151. changes->map.vmods |= bit;
  152. tmp++;
  153. }
  154. }
  155. if ((i = XkbPaddedSize(tmp) - tmp) > 0)
  156. nRead += XkmSkipPadding(file, i);
  157. if (XkbAllocNames(xkb, XkbVirtualModNamesMask, 0, 0) != Success) {
  158. _XkbLibError(_XkbErrBadAlloc, "ReadXkmVirtualMods", 0);
  159. return -1;
  160. }
  161. for (i = 0, bit = 1; i < XkbNumVirtualMods; i++, bit <<= 1) {
  162. char name[100];
  163. if (named & bit) {
  164. if (nRead += XkmGetCountedString(file, name, 100)) {
  165. xkb->names->vmods[i] = XkbInternAtom(name, FALSE);
  166. if (changes)
  167. changes->names.changed_vmods |= bit;
  168. }
  169. }
  170. }
  171. return nRead;
  172. }
  173. /***====================================================================***/
  174. static int
  175. ReadXkmKeycodes(FILE * file, XkbDescPtr xkb, XkbChangesPtr changes)
  176. {
  177. register int i;
  178. unsigned minKC, maxKC, nAl;
  179. int nRead = 0;
  180. char name[100];
  181. XkbKeyNamePtr pN;
  182. name[0] = '\0';
  183. nRead += XkmGetCountedString(file, name, 100);
  184. minKC = XkmGetCARD8(file, &nRead);
  185. maxKC = XkmGetCARD8(file, &nRead);
  186. if (xkb->min_key_code == 0) {
  187. xkb->min_key_code = minKC;
  188. xkb->max_key_code = maxKC;
  189. }
  190. else {
  191. if (minKC < xkb->min_key_code)
  192. xkb->min_key_code = minKC;
  193. if (maxKC > xkb->max_key_code) {
  194. _XkbLibError(_XkbErrBadValue, "ReadXkmKeycodes", maxKC);
  195. return -1;
  196. }
  197. }
  198. nAl = XkmGetCARD8(file, &nRead);
  199. nRead += XkmSkipPadding(file, 1);
  200. #define WANTED (XkbKeycodesNameMask|XkbKeyNamesMask|XkbKeyAliasesMask)
  201. if (XkbAllocNames(xkb, WANTED, 0, nAl) != Success) {
  202. _XkbLibError(_XkbErrBadAlloc, "ReadXkmKeycodes", 0);
  203. return -1;
  204. }
  205. if (name[0] != '\0') {
  206. xkb->names->keycodes = XkbInternAtom(name, FALSE);
  207. }
  208. for (pN = &xkb->names->keys[minKC], i = minKC; i <= (int) maxKC; i++, pN++) {
  209. if (fread(pN, 1, XkbKeyNameLength, file) != XkbKeyNameLength) {
  210. _XkbLibError(_XkbErrBadLength, "ReadXkmKeycodes", 0);
  211. return -1;
  212. }
  213. nRead += XkbKeyNameLength;
  214. }
  215. if (nAl > 0) {
  216. XkbKeyAliasPtr pAl;
  217. for (pAl = xkb->names->key_aliases, i = 0; i < nAl; i++, pAl++) {
  218. int tmp;
  219. tmp = fread(pAl, 1, 2 * XkbKeyNameLength, file);
  220. if (tmp != 2 * XkbKeyNameLength) {
  221. _XkbLibError(_XkbErrBadLength, "ReadXkmKeycodes", 0);
  222. return -1;
  223. }
  224. nRead += 2 * XkbKeyNameLength;
  225. }
  226. if (changes)
  227. changes->names.changed |= XkbKeyAliasesMask;
  228. }
  229. if (changes)
  230. changes->names.changed |= XkbKeyNamesMask;
  231. return nRead;
  232. }
  233. /***====================================================================***/
  234. static int
  235. ReadXkmKeyTypes(FILE * file, XkbDescPtr xkb, XkbChangesPtr changes)
  236. {
  237. register unsigned i, n;
  238. unsigned num_types;
  239. int nRead = 0;
  240. int tmp;
  241. XkbKeyTypePtr type;
  242. xkmKeyTypeDesc wire;
  243. XkbKTMapEntryPtr entry;
  244. xkmKTMapEntryDesc wire_entry;
  245. char buf[100];
  246. if ((tmp = XkmGetCountedString(file, buf, 100)) < 1) {
  247. _XkbLibError(_XkbErrBadLength, "ReadXkmKeyTypes", 0);
  248. return -1;
  249. }
  250. nRead += tmp;
  251. if (buf[0] != '\0') {
  252. if (XkbAllocNames(xkb, XkbTypesNameMask, 0, 0) != Success) {
  253. _XkbLibError(_XkbErrBadAlloc, "ReadXkmKeyTypes", 0);
  254. return -1;
  255. }
  256. xkb->names->types = XkbInternAtom(buf, FALSE);
  257. }
  258. num_types = XkmGetCARD16(file, &nRead);
  259. nRead += XkmSkipPadding(file, 2);
  260. if (num_types < 1)
  261. return nRead;
  262. if (XkbAllocClientMap(xkb, XkbKeyTypesMask, num_types) != Success) {
  263. _XkbLibError(_XkbErrBadAlloc, "ReadXkmKeyTypes", 0);
  264. return nRead;
  265. }
  266. xkb->map->num_types = num_types;
  267. if (num_types < XkbNumRequiredTypes) {
  268. _XkbLibError(_XkbErrMissingReqTypes, "ReadXkmKeyTypes", 0);
  269. return -1;
  270. }
  271. type = xkb->map->types;
  272. for (i = 0; i < num_types; i++, type++) {
  273. if ((int) fread(&wire, SIZEOF(xkmKeyTypeDesc), 1, file) < 1) {
  274. _XkbLibError(_XkbErrBadLength, "ReadXkmKeyTypes", 0);
  275. return -1;
  276. }
  277. nRead += SIZEOF(xkmKeyTypeDesc);
  278. if (((i == XkbOneLevelIndex) && (wire.numLevels != 1)) ||
  279. (((i == XkbTwoLevelIndex) || (i == XkbAlphabeticIndex) ||
  280. ((i) == XkbKeypadIndex)) && (wire.numLevels != 2))) {
  281. _XkbLibError(_XkbErrBadTypeWidth, "ReadXkmKeyTypes", i);
  282. return -1;
  283. }
  284. tmp = wire.nMapEntries;
  285. XkmInsureTypedSize(type->map, type->map_count, &tmp, XkbKTMapEntryRec);
  286. if ((wire.nMapEntries > 0) && (type->map == NULL)) {
  287. _XkbLibError(_XkbErrBadValue, "ReadXkmKeyTypes", wire.nMapEntries);
  288. return -1;
  289. }
  290. for (n = 0, entry = type->map; n < wire.nMapEntries; n++, entry++) {
  291. if (fread(&wire_entry, SIZEOF(xkmKTMapEntryDesc), 1, file) <
  292. (int) 1) {
  293. _XkbLibError(_XkbErrBadLength, "ReadXkmKeyTypes", 0);
  294. return -1;
  295. }
  296. nRead += SIZEOF(xkmKTMapEntryDesc);
  297. entry->active = (wire_entry.virtualMods == 0);
  298. entry->level = wire_entry.level;
  299. entry->mods.mask = wire_entry.realMods;
  300. entry->mods.real_mods = wire_entry.realMods;
  301. entry->mods.vmods = wire_entry.virtualMods;
  302. }
  303. nRead += XkmGetCountedString(file, buf, 100);
  304. if (((i == XkbOneLevelIndex) && (strcmp(buf, "ONE_LEVEL") != 0)) ||
  305. ((i == XkbTwoLevelIndex) && (strcmp(buf, "TWO_LEVEL") != 0)) ||
  306. ((i == XkbAlphabeticIndex) && (strcmp(buf, "ALPHABETIC") != 0)) ||
  307. ((i == XkbKeypadIndex) && (strcmp(buf, "KEYPAD") != 0))) {
  308. _XkbLibError(_XkbErrBadTypeName, "ReadXkmKeyTypes", 0);
  309. return -1;
  310. }
  311. if (buf[0] != '\0') {
  312. type->name = XkbInternAtom(buf, FALSE);
  313. }
  314. else
  315. type->name = None;
  316. if (wire.preserve) {
  317. xkmModsDesc p_entry;
  318. XkbModsPtr pre;
  319. XkmInsureTypedSize(type->preserve, type->map_count, &tmp,
  320. XkbModsRec);
  321. if (type->preserve == NULL) {
  322. _XkbLibError(_XkbErrBadMatch, "ReadXkmKeycodes", 0);
  323. return -1;
  324. }
  325. for (n = 0, pre = type->preserve; n < wire.nMapEntries; n++, pre++) {
  326. if (fread(&p_entry, SIZEOF(xkmModsDesc), 1, file) < 1) {
  327. _XkbLibError(_XkbErrBadLength, "ReadXkmKeycodes", 0);
  328. return -1;
  329. }
  330. nRead += SIZEOF(xkmModsDesc);
  331. pre->mask = p_entry.realMods;
  332. pre->real_mods = p_entry.realMods;
  333. pre->vmods = p_entry.virtualMods;
  334. }
  335. }
  336. if (wire.nLevelNames > 0) {
  337. int width = wire.numLevels;
  338. if (wire.nLevelNames > (unsigned) width) {
  339. _XkbLibError(_XkbErrBadMatch, "ReadXkmKeycodes", 0);
  340. return -1;
  341. }
  342. XkmInsureTypedSize(type->level_names, type->num_levels, &width,
  343. Atom);
  344. if (type->level_names != NULL) {
  345. for (n = 0; n < wire.nLevelNames; n++) {
  346. if ((tmp = XkmGetCountedString(file, buf, 100)) < 1)
  347. return -1;
  348. nRead += tmp;
  349. if (strlen(buf) == 0)
  350. type->level_names[n] = None;
  351. else
  352. type->level_names[n] = XkbInternAtom(buf, 0);
  353. }
  354. }
  355. }
  356. type->mods.mask = wire.realMods;
  357. type->mods.real_mods = wire.realMods;
  358. type->mods.vmods = wire.virtualMods;
  359. type->num_levels = wire.numLevels;
  360. type->map_count = wire.nMapEntries;
  361. }
  362. if (changes) {
  363. changes->map.changed |= XkbKeyTypesMask;
  364. changes->map.first_type = 0;
  365. changes->map.num_types = xkb->map->num_types;
  366. }
  367. return nRead;
  368. }
  369. /***====================================================================***/
  370. static int
  371. ReadXkmCompatMap(FILE * file, XkbDescPtr xkb, XkbChangesPtr changes)
  372. {
  373. register int i;
  374. unsigned num_si, groups;
  375. char name[100];
  376. XkbSymInterpretPtr interp;
  377. xkmSymInterpretDesc wire;
  378. unsigned tmp;
  379. int nRead = 0;
  380. XkbCompatMapPtr compat;
  381. XkbAction *act;
  382. if ((tmp = XkmGetCountedString(file, name, 100)) < 1) {
  383. _XkbLibError(_XkbErrBadLength, "ReadXkmCompatMap", 0);
  384. return -1;
  385. }
  386. nRead += tmp;
  387. if (name[0] != '\0') {
  388. if (XkbAllocNames(xkb, XkbCompatNameMask, 0, 0) != Success) {
  389. _XkbLibError(_XkbErrBadAlloc, "ReadXkmCompatMap", 0);
  390. return -1;
  391. }
  392. xkb->names->compat = XkbInternAtom(name, FALSE);
  393. }
  394. num_si = XkmGetCARD16(file, &nRead);
  395. groups = XkmGetCARD8(file, &nRead);
  396. nRead += XkmSkipPadding(file, 1);
  397. if (XkbAllocCompatMap(xkb, XkbAllCompatMask, num_si) != Success)
  398. return -1;
  399. compat = xkb->compat;
  400. compat->num_si = 0;
  401. interp = compat->sym_interpret;
  402. for (i = 0; i < num_si; i++) {
  403. tmp = fread(&wire, SIZEOF(xkmSymInterpretDesc), 1, file);
  404. nRead += tmp * SIZEOF(xkmSymInterpretDesc);
  405. interp->sym = wire.sym;
  406. interp->mods = wire.mods;
  407. interp->match = wire.match;
  408. interp->virtual_mod = wire.virtualMod;
  409. interp->flags = wire.flags;
  410. interp->act.type = wire.actionType;
  411. act = (XkbAction *) &interp->act;
  412. switch (interp->act.type) {
  413. case XkbSA_SetMods:
  414. case XkbSA_LatchMods:
  415. case XkbSA_LockMods:
  416. act->mods.flags = wire.actionData[0];
  417. act->mods.mask = wire.actionData[1];
  418. act->mods.real_mods = wire.actionData[2];
  419. act->mods.vmods1 = wire.actionData[3];
  420. act->mods.vmods2 = wire.actionData[4];
  421. break;
  422. case XkbSA_SetGroup:
  423. case XkbSA_LatchGroup:
  424. case XkbSA_LockGroup:
  425. act->group.flags = wire.actionData[0];
  426. act->group.group_XXX = wire.actionData[1];
  427. break;
  428. case XkbSA_MovePtr:
  429. act->ptr.flags = wire.actionData[0];
  430. act->ptr.high_XXX = wire.actionData[1];
  431. act->ptr.low_XXX = wire.actionData[2];
  432. act->ptr.high_YYY = wire.actionData[3];
  433. act->ptr.low_YYY = wire.actionData[4];
  434. break;
  435. case XkbSA_PtrBtn:
  436. case XkbSA_LockPtrBtn:
  437. act->btn.flags = wire.actionData[0];
  438. act->btn.count = wire.actionData[1];
  439. act->btn.button = wire.actionData[2];
  440. break;
  441. case XkbSA_DeviceBtn:
  442. case XkbSA_LockDeviceBtn:
  443. act->devbtn.flags = wire.actionData[0];
  444. act->devbtn.count = wire.actionData[1];
  445. act->devbtn.button = wire.actionData[2];
  446. act->devbtn.device = wire.actionData[3];
  447. break;
  448. case XkbSA_SetPtrDflt:
  449. act->dflt.flags = wire.actionData[0];
  450. act->dflt.affect = wire.actionData[1];
  451. act->dflt.valueXXX = wire.actionData[2];
  452. break;
  453. case XkbSA_ISOLock:
  454. act->iso.flags = wire.actionData[0];
  455. act->iso.mask = wire.actionData[1];
  456. act->iso.real_mods = wire.actionData[2];
  457. act->iso.group_XXX = wire.actionData[3];
  458. act->iso.affect = wire.actionData[4];
  459. act->iso.vmods1 = wire.actionData[5];
  460. act->iso.vmods2 = wire.actionData[6];
  461. break;
  462. case XkbSA_SwitchScreen:
  463. act->screen.flags = wire.actionData[0];
  464. act->screen.screenXXX = wire.actionData[1];
  465. break;
  466. case XkbSA_SetControls:
  467. case XkbSA_LockControls:
  468. act->ctrls.flags = wire.actionData[0];
  469. act->ctrls.ctrls3 = wire.actionData[1];
  470. act->ctrls.ctrls2 = wire.actionData[2];
  471. act->ctrls.ctrls1 = wire.actionData[3];
  472. act->ctrls.ctrls0 = wire.actionData[4];
  473. break;
  474. case XkbSA_RedirectKey:
  475. act->redirect.new_key = wire.actionData[0];
  476. act->redirect.mods_mask = wire.actionData[1];
  477. act->redirect.mods = wire.actionData[2];
  478. act->redirect.vmods_mask0 = wire.actionData[3];
  479. act->redirect.vmods_mask1 = wire.actionData[4];
  480. act->redirect.vmods0 = wire.actionData[4];
  481. act->redirect.vmods1 = wire.actionData[5];
  482. break;
  483. case XkbSA_DeviceValuator:
  484. act->devval.device = wire.actionData[0];
  485. act->devval.v1_what = wire.actionData[1];
  486. act->devval.v1_ndx = wire.actionData[2];
  487. act->devval.v1_value = wire.actionData[3];
  488. act->devval.v2_what = wire.actionData[4];
  489. act->devval.v2_ndx = wire.actionData[5];
  490. act->devval.v2_what = wire.actionData[6];
  491. break;
  492. case XkbSA_XFree86Private:
  493. /*
  494. * Bugfix for broken xkbcomp: if we encounter an XFree86Private
  495. * action with Any+AnyOfOrNone(All), then we skip the interp as
  496. * broken. Versions of xkbcomp below 1.2.2 had a bug where they
  497. * would interpret a symbol that couldn't be found in an interpret
  498. * as Any. So, an XF86LogWindowTree+AnyOfOrNone(All) interp that
  499. * triggered the PrWins action would make every key without an
  500. * action trigger PrWins if libX11 didn't yet know about the
  501. * XF86LogWindowTree keysym. None too useful.
  502. *
  503. * We only do this for XFree86 actions, as the current XKB
  504. * dataset relies on Any+AnyOfOrNone(All) -> SetMods for Ctrl in
  505. * particular.
  506. *
  507. * See xkbcomp commits 2a473b906943ffd807ad81960c47530ee7ae9a60 and
  508. * 3caab5aa37decb7b5dc1642a0452efc3e1f5100e for more details.
  509. */
  510. if (interp->sym == NoSymbol && interp->match == XkbSI_AnyOfOrNone &&
  511. (interp->mods & 0xff) == 0xff) {
  512. ErrorF("XKB: Skipping broken Any+AnyOfOrNone(All) -> Private "
  513. "action from compiled keymap\n");
  514. continue;
  515. }
  516. /* copy the kind of action */
  517. memcpy(act->any.data, wire.actionData, XkbAnyActionDataSize);
  518. break;
  519. case XkbSA_Terminate:
  520. /* no args, kinda (note: untrue for xfree86). */
  521. break;
  522. case XkbSA_ActionMessage:
  523. /* unsupported. */
  524. break;
  525. }
  526. interp++;
  527. compat->num_si++;
  528. }
  529. if ((num_si > 0) && (changes)) {
  530. changes->compat.first_si = 0;
  531. changes->compat.num_si = compat->num_si;
  532. }
  533. if (groups) {
  534. register unsigned bit;
  535. for (i = 0, bit = 1; i < XkbNumKbdGroups; i++, bit <<= 1) {
  536. xkmModsDesc md;
  537. if (groups & bit) {
  538. tmp = fread(&md, SIZEOF(xkmModsDesc), 1, file);
  539. nRead += tmp * SIZEOF(xkmModsDesc);
  540. xkb->compat->groups[i].real_mods = md.realMods;
  541. xkb->compat->groups[i].vmods = md.virtualMods;
  542. if (md.virtualMods != 0) {
  543. unsigned mask;
  544. if (XkbVirtualModsToReal(xkb, md.virtualMods, &mask))
  545. xkb->compat->groups[i].mask = md.realMods | mask;
  546. }
  547. else
  548. xkb->compat->groups[i].mask = md.realMods;
  549. }
  550. }
  551. if (changes)
  552. changes->compat.changed_groups |= groups;
  553. }
  554. return nRead;
  555. }
  556. static int
  557. ReadXkmIndicators(FILE * file, XkbDescPtr xkb, XkbChangesPtr changes)
  558. {
  559. register unsigned nLEDs;
  560. xkmIndicatorMapDesc wire;
  561. char buf[100];
  562. unsigned tmp;
  563. int nRead = 0;
  564. if ((xkb->indicators == NULL) && (XkbAllocIndicatorMaps(xkb) != Success)) {
  565. _XkbLibError(_XkbErrBadAlloc, "indicator rec", 0);
  566. return -1;
  567. }
  568. if (XkbAllocNames(xkb, XkbIndicatorNamesMask, 0, 0) != Success) {
  569. _XkbLibError(_XkbErrBadAlloc, "indicator names", 0);
  570. return -1;
  571. }
  572. nLEDs = XkmGetCARD8(file, &nRead);
  573. nRead += XkmSkipPadding(file, 3);
  574. xkb->indicators->phys_indicators = XkmGetCARD32(file, &nRead);
  575. while (nLEDs-- > 0) {
  576. Atom name;
  577. XkbIndicatorMapPtr map;
  578. if ((tmp = XkmGetCountedString(file, buf, 100)) < 1) {
  579. _XkbLibError(_XkbErrBadLength, "ReadXkmIndicators", 0);
  580. return -1;
  581. }
  582. nRead += tmp;
  583. if (buf[0] != '\0')
  584. name = XkbInternAtom(buf, FALSE);
  585. else
  586. name = None;
  587. if ((tmp = fread(&wire, SIZEOF(xkmIndicatorMapDesc), 1, file)) < 1) {
  588. _XkbLibError(_XkbErrBadLength, "ReadXkmIndicators", 0);
  589. return -1;
  590. }
  591. nRead += tmp * SIZEOF(xkmIndicatorMapDesc);
  592. if (xkb->names) {
  593. xkb->names->indicators[wire.indicator - 1] = name;
  594. if (changes)
  595. changes->names.changed_indicators |=
  596. (1 << (wire.indicator - 1));
  597. }
  598. map = &xkb->indicators->maps[wire.indicator - 1];
  599. map->flags = wire.flags;
  600. map->which_groups = wire.which_groups;
  601. map->groups = wire.groups;
  602. map->which_mods = wire.which_mods;
  603. map->mods.mask = wire.real_mods;
  604. map->mods.real_mods = wire.real_mods;
  605. map->mods.vmods = wire.vmods;
  606. map->ctrls = wire.ctrls;
  607. }
  608. return nRead;
  609. }
  610. static XkbKeyTypePtr
  611. FindTypeForKey(XkbDescPtr xkb, Atom name, unsigned width, KeySym * syms)
  612. {
  613. if ((!xkb) || (!xkb->map))
  614. return NULL;
  615. if (name != None) {
  616. register unsigned i;
  617. for (i = 0; i < xkb->map->num_types; i++) {
  618. if (xkb->map->types[i].name == name) {
  619. if (xkb->map->types[i].num_levels != width)
  620. DebugF("Group width mismatch between key and type\n");
  621. return &xkb->map->types[i];
  622. }
  623. }
  624. }
  625. if ((width < 2) || ((syms != NULL) && (syms[1] == NoSymbol)))
  626. return &xkb->map->types[XkbOneLevelIndex];
  627. if (syms != NULL) {
  628. if (XkbKSIsLower(syms[0]) && XkbKSIsUpper(syms[1]))
  629. return &xkb->map->types[XkbAlphabeticIndex];
  630. else if (XkbKSIsKeypad(syms[0]) || XkbKSIsKeypad(syms[1]))
  631. return &xkb->map->types[XkbKeypadIndex];
  632. }
  633. return &xkb->map->types[XkbTwoLevelIndex];
  634. }
  635. static int
  636. ReadXkmSymbols(FILE * file, XkbDescPtr xkb)
  637. {
  638. register int i, g, s, totalVModMaps;
  639. xkmKeySymMapDesc wireMap;
  640. char buf[100];
  641. unsigned minKC, maxKC, groupNames, tmp;
  642. int nRead = 0;
  643. if ((tmp = XkmGetCountedString(file, buf, 100)) < 1)
  644. return -1;
  645. nRead += tmp;
  646. minKC = XkmGetCARD8(file, &nRead);
  647. maxKC = XkmGetCARD8(file, &nRead);
  648. groupNames = XkmGetCARD8(file, &nRead);
  649. totalVModMaps = XkmGetCARD8(file, &nRead);
  650. if (XkbAllocNames(xkb,
  651. XkbSymbolsNameMask | XkbPhysSymbolsNameMask |
  652. XkbGroupNamesMask, 0, 0) != Success) {
  653. _XkbLibError(_XkbErrBadAlloc, "physical names", 0);
  654. return -1;
  655. }
  656. if ((buf[0] != '\0') && (xkb->names)) {
  657. Atom name;
  658. name = XkbInternAtom(buf, 0);
  659. xkb->names->symbols = name;
  660. xkb->names->phys_symbols = name;
  661. }
  662. for (i = 0, g = 1; i < XkbNumKbdGroups; i++, g <<= 1) {
  663. if (groupNames & g) {
  664. if ((tmp = XkmGetCountedString(file, buf, 100)) < 1)
  665. return -1;
  666. nRead += tmp;
  667. if (!xkb->names)
  668. continue;
  669. if (buf[0] != '\0') {
  670. Atom name;
  671. name = XkbInternAtom(buf, 0);
  672. xkb->names->groups[i] = name;
  673. }
  674. else
  675. xkb->names->groups[i] = None;
  676. }
  677. }
  678. if (XkbAllocServerMap(xkb, XkbAllServerInfoMask, 0) != Success) {
  679. _XkbLibError(_XkbErrBadAlloc, "server map", 0);
  680. return -1;
  681. }
  682. if (XkbAllocClientMap(xkb, XkbAllClientInfoMask, 0) != Success) {
  683. _XkbLibError(_XkbErrBadAlloc, "client map", 0);
  684. return -1;
  685. }
  686. if (XkbAllocControls(xkb, XkbAllControlsMask) != Success) {
  687. _XkbLibError(_XkbErrBadAlloc, "controls", 0);
  688. return -1;
  689. }
  690. if ((xkb->map == NULL) || (xkb->server == NULL))
  691. return -1;
  692. if (xkb->min_key_code < 8)
  693. xkb->min_key_code = minKC;
  694. if (xkb->max_key_code < 8)
  695. xkb->max_key_code = maxKC;
  696. if ((minKC >= 8) && (minKC < xkb->min_key_code))
  697. xkb->min_key_code = minKC;
  698. if ((maxKC >= 8) && (maxKC > xkb->max_key_code)) {
  699. _XkbLibError(_XkbErrBadValue, "keys in symbol map", maxKC);
  700. return -1;
  701. }
  702. for (i = minKC; i <= (int) maxKC; i++) {
  703. Atom typeName[XkbNumKbdGroups];
  704. XkbKeyTypePtr type[XkbNumKbdGroups];
  705. if ((tmp = fread(&wireMap, SIZEOF(xkmKeySymMapDesc), 1, file)) < 1) {
  706. _XkbLibError(_XkbErrBadLength, "ReadXkmSymbols", 0);
  707. return -1;
  708. }
  709. nRead += tmp * SIZEOF(xkmKeySymMapDesc);
  710. memset((char *) typeName, 0, XkbNumKbdGroups * sizeof(Atom));
  711. memset((char *) type, 0, XkbNumKbdGroups * sizeof(XkbKeyTypePtr));
  712. if (wireMap.flags & XkmKeyHasTypes) {
  713. for (g = 0; g < XkbNumKbdGroups; g++) {
  714. if ((wireMap.flags & (1 << g)) &&
  715. ((tmp = XkmGetCountedString(file, buf, 100)) > 0)) {
  716. typeName[g] = XkbInternAtom(buf, 1);
  717. nRead += tmp;
  718. }
  719. type[g] = FindTypeForKey(xkb, typeName[g], wireMap.width, NULL);
  720. if (type[g] == NULL) {
  721. _XkbLibError(_XkbErrMissingTypes, "ReadXkmSymbols", 0);
  722. return -1;
  723. }
  724. if (typeName[g] == type[g]->name)
  725. xkb->server->explicit[i] |= (1 << g);
  726. }
  727. }
  728. if (wireMap.flags & XkmRepeatingKey) {
  729. xkb->ctrls->per_key_repeat[i / 8] |= (1 << (i % 8));
  730. xkb->server->explicit[i] |= XkbExplicitAutoRepeatMask;
  731. }
  732. else if (wireMap.flags & XkmNonRepeatingKey) {
  733. xkb->ctrls->per_key_repeat[i / 8] &= ~(1 << (i % 8));
  734. xkb->server->explicit[i] |= XkbExplicitAutoRepeatMask;
  735. }
  736. xkb->map->modmap[i] = wireMap.modifier_map;
  737. if (XkbNumGroups(wireMap.num_groups) > 0) {
  738. KeySym *sym;
  739. int nSyms;
  740. if (XkbNumGroups(wireMap.num_groups) > xkb->ctrls->num_groups)
  741. xkb->ctrls->num_groups = wireMap.num_groups;
  742. nSyms = XkbNumGroups(wireMap.num_groups) * wireMap.width;
  743. sym = XkbResizeKeySyms(xkb, i, nSyms);
  744. if (!sym)
  745. return -1;
  746. for (s = 0; s < nSyms; s++) {
  747. *sym++ = XkmGetCARD32(file, &nRead);
  748. }
  749. if (wireMap.flags & XkmKeyHasActions) {
  750. XkbAction *act;
  751. act = XkbResizeKeyActions(xkb, i, nSyms);
  752. for (s = 0; s < nSyms; s++, act++) {
  753. tmp = fread(act, SIZEOF(xkmActionDesc), 1, file);
  754. nRead += tmp * SIZEOF(xkmActionDesc);
  755. }
  756. xkb->server->explicit[i] |= XkbExplicitInterpretMask;
  757. }
  758. }
  759. for (g = 0; g < XkbNumGroups(wireMap.num_groups); g++) {
  760. if (((xkb->server->explicit[i] & (1 << g)) == 0) ||
  761. (type[g] == NULL)) {
  762. KeySym *tmpSyms;
  763. tmpSyms = XkbKeySymsPtr(xkb, i) + (wireMap.width * g);
  764. type[g] = FindTypeForKey(xkb, None, wireMap.width, tmpSyms);
  765. }
  766. xkb->map->key_sym_map[i].kt_index[g] =
  767. type[g] - (&xkb->map->types[0]);
  768. }
  769. xkb->map->key_sym_map[i].group_info = wireMap.num_groups;
  770. xkb->map->key_sym_map[i].width = wireMap.width;
  771. if (wireMap.flags & XkmKeyHasBehavior) {
  772. xkmBehaviorDesc b;
  773. tmp = fread(&b, SIZEOF(xkmBehaviorDesc), 1, file);
  774. nRead += tmp * SIZEOF(xkmBehaviorDesc);
  775. xkb->server->behaviors[i].type = b.type;
  776. xkb->server->behaviors[i].data = b.data;
  777. xkb->server->explicit[i] |= XkbExplicitBehaviorMask;
  778. }
  779. }
  780. if (totalVModMaps > 0) {
  781. xkmVModMapDesc v;
  782. for (i = 0; i < totalVModMaps; i++) {
  783. tmp = fread(&v, SIZEOF(xkmVModMapDesc), 1, file);
  784. nRead += tmp * SIZEOF(xkmVModMapDesc);
  785. if (tmp > 0)
  786. xkb->server->vmodmap[v.key] = v.vmods;
  787. }
  788. }
  789. return nRead;
  790. }
  791. static int
  792. ReadXkmGeomDoodad(FILE * file, XkbGeometryPtr geom, XkbSectionPtr section)
  793. {
  794. XkbDoodadPtr doodad;
  795. xkmDoodadDesc doodadWire;
  796. char buf[100];
  797. unsigned tmp;
  798. int nRead = 0;
  799. nRead += XkmGetCountedString(file, buf, 100);
  800. tmp = fread(&doodadWire, SIZEOF(xkmDoodadDesc), 1, file);
  801. nRead += SIZEOF(xkmDoodadDesc) * tmp;
  802. doodad = XkbAddGeomDoodad(geom, section, XkbInternAtom(buf, FALSE));
  803. if (!doodad)
  804. return nRead;
  805. doodad->any.type = doodadWire.any.type;
  806. doodad->any.priority = doodadWire.any.priority;
  807. doodad->any.top = doodadWire.any.top;
  808. doodad->any.left = doodadWire.any.left;
  809. switch (doodadWire.any.type) {
  810. case XkbOutlineDoodad:
  811. case XkbSolidDoodad:
  812. doodad->shape.angle = doodadWire.shape.angle;
  813. doodad->shape.color_ndx = doodadWire.shape.color_ndx;
  814. doodad->shape.shape_ndx = doodadWire.shape.shape_ndx;
  815. break;
  816. case XkbTextDoodad:
  817. doodad->text.angle = doodadWire.text.angle;
  818. doodad->text.width = doodadWire.text.width;
  819. doodad->text.height = doodadWire.text.height;
  820. doodad->text.color_ndx = doodadWire.text.color_ndx;
  821. nRead += XkmGetCountedString(file, buf, 100);
  822. doodad->text.text = Xstrdup(buf);
  823. nRead += XkmGetCountedString(file, buf, 100);
  824. doodad->text.font = Xstrdup(buf);
  825. break;
  826. case XkbIndicatorDoodad:
  827. doodad->indicator.shape_ndx = doodadWire.indicator.shape_ndx;
  828. doodad->indicator.on_color_ndx = doodadWire.indicator.on_color_ndx;
  829. doodad->indicator.off_color_ndx = doodadWire.indicator.off_color_ndx;
  830. break;
  831. case XkbLogoDoodad:
  832. doodad->logo.angle = doodadWire.logo.angle;
  833. doodad->logo.color_ndx = doodadWire.logo.color_ndx;
  834. doodad->logo.shape_ndx = doodadWire.logo.shape_ndx;
  835. nRead += XkmGetCountedString(file, buf, 100);
  836. doodad->logo.logo_name = Xstrdup(buf);
  837. break;
  838. default:
  839. /* report error? */
  840. return nRead;
  841. }
  842. return nRead;
  843. }
  844. static int
  845. ReadXkmGeomOverlay(FILE * file, XkbGeometryPtr geom, XkbSectionPtr section)
  846. {
  847. char buf[100];
  848. unsigned tmp;
  849. int nRead = 0;
  850. XkbOverlayPtr ol;
  851. XkbOverlayRowPtr row;
  852. xkmOverlayDesc olWire;
  853. xkmOverlayRowDesc rowWire;
  854. register int r;
  855. nRead += XkmGetCountedString(file, buf, 100);
  856. tmp = fread(&olWire, SIZEOF(xkmOverlayDesc), 1, file);
  857. nRead += tmp * SIZEOF(xkmOverlayDesc);
  858. ol = XkbAddGeomOverlay(section, XkbInternAtom(buf, FALSE), olWire.num_rows);
  859. if (!ol)
  860. return nRead;
  861. for (r = 0; r < olWire.num_rows; r++) {
  862. int k;
  863. xkmOverlayKeyDesc keyWire;
  864. tmp = fread(&rowWire, SIZEOF(xkmOverlayRowDesc), 1, file);
  865. nRead += tmp * SIZEOF(xkmOverlayRowDesc);
  866. row = XkbAddGeomOverlayRow(ol, rowWire.row_under, rowWire.num_keys);
  867. if (!row) {
  868. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeomOverlay", 0);
  869. return nRead;
  870. }
  871. for (k = 0; k < rowWire.num_keys; k++) {
  872. tmp = fread(&keyWire, SIZEOF(xkmOverlayKeyDesc), 1, file);
  873. nRead += tmp * SIZEOF(xkmOverlayKeyDesc);
  874. memcpy(row->keys[k].over.name, keyWire.over, XkbKeyNameLength);
  875. memcpy(row->keys[k].under.name, keyWire.under, XkbKeyNameLength);
  876. }
  877. row->num_keys = rowWire.num_keys;
  878. }
  879. return nRead;
  880. }
  881. static int
  882. ReadXkmGeomSection(FILE * file, XkbGeometryPtr geom)
  883. {
  884. register int i;
  885. XkbSectionPtr section;
  886. xkmSectionDesc sectionWire;
  887. unsigned tmp;
  888. int nRead = 0;
  889. char buf[100];
  890. Atom nameAtom;
  891. nRead += XkmGetCountedString(file, buf, 100);
  892. nameAtom = XkbInternAtom(buf, FALSE);
  893. tmp = fread(&sectionWire, SIZEOF(xkmSectionDesc), 1, file);
  894. nRead += SIZEOF(xkmSectionDesc) * tmp;
  895. section = XkbAddGeomSection(geom, nameAtom, sectionWire.num_rows,
  896. sectionWire.num_doodads,
  897. sectionWire.num_overlays);
  898. if (!section) {
  899. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeomSection", 0);
  900. return nRead;
  901. }
  902. section->top = sectionWire.top;
  903. section->left = sectionWire.left;
  904. section->width = sectionWire.width;
  905. section->height = sectionWire.height;
  906. section->angle = sectionWire.angle;
  907. section->priority = sectionWire.priority;
  908. if (sectionWire.num_rows > 0) {
  909. register int k;
  910. XkbRowPtr row;
  911. xkmRowDesc rowWire;
  912. XkbKeyPtr key;
  913. xkmKeyDesc keyWire;
  914. for (i = 0; i < sectionWire.num_rows; i++) {
  915. tmp = fread(&rowWire, SIZEOF(xkmRowDesc), 1, file);
  916. nRead += SIZEOF(xkmRowDesc) * tmp;
  917. row = XkbAddGeomRow(section, rowWire.num_keys);
  918. if (!row) {
  919. _XkbLibError(_XkbErrBadAlloc, "ReadXkmKeycodes", 0);
  920. return nRead;
  921. }
  922. row->top = rowWire.top;
  923. row->left = rowWire.left;
  924. row->vertical = rowWire.vertical;
  925. for (k = 0; k < rowWire.num_keys; k++) {
  926. tmp = fread(&keyWire, SIZEOF(xkmKeyDesc), 1, file);
  927. nRead += SIZEOF(xkmKeyDesc) * tmp;
  928. key = XkbAddGeomKey(row);
  929. if (!key) {
  930. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeomSection", 0);
  931. return nRead;
  932. }
  933. memcpy(key->name.name, keyWire.name, XkbKeyNameLength);
  934. key->gap = keyWire.gap;
  935. key->shape_ndx = keyWire.shape_ndx;
  936. key->color_ndx = keyWire.color_ndx;
  937. }
  938. }
  939. }
  940. if (sectionWire.num_doodads > 0) {
  941. for (i = 0; i < sectionWire.num_doodads; i++) {
  942. tmp = ReadXkmGeomDoodad(file, geom, section);
  943. nRead += tmp;
  944. if (tmp < 1)
  945. return nRead;
  946. }
  947. }
  948. if (sectionWire.num_overlays > 0) {
  949. for (i = 0; i < sectionWire.num_overlays; i++) {
  950. tmp = ReadXkmGeomOverlay(file, geom, section);
  951. nRead += tmp;
  952. if (tmp < 1)
  953. return nRead;
  954. }
  955. }
  956. return nRead;
  957. }
  958. static int
  959. ReadXkmGeometry(FILE * file, XkbDescPtr xkb)
  960. {
  961. register int i;
  962. char buf[100];
  963. unsigned tmp;
  964. int nRead = 0;
  965. xkmGeometryDesc wireGeom;
  966. XkbGeometryPtr geom;
  967. XkbGeometrySizesRec sizes;
  968. nRead += XkmGetCountedString(file, buf, 100);
  969. tmp = fread(&wireGeom, SIZEOF(xkmGeometryDesc), 1, file);
  970. nRead += tmp * SIZEOF(xkmGeometryDesc);
  971. sizes.which = XkbGeomAllMask;
  972. sizes.num_properties = wireGeom.num_properties;
  973. sizes.num_colors = wireGeom.num_colors;
  974. sizes.num_shapes = wireGeom.num_shapes;
  975. sizes.num_sections = wireGeom.num_sections;
  976. sizes.num_doodads = wireGeom.num_doodads;
  977. sizes.num_key_aliases = wireGeom.num_key_aliases;
  978. if (XkbAllocGeometry(xkb, &sizes) != Success) {
  979. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeometry", 0);
  980. return nRead;
  981. }
  982. geom = xkb->geom;
  983. geom->name = XkbInternAtom(buf, FALSE);
  984. geom->width_mm = wireGeom.width_mm;
  985. geom->height_mm = wireGeom.height_mm;
  986. nRead += XkmGetCountedString(file, buf, 100);
  987. geom->label_font = Xstrdup(buf);
  988. if (wireGeom.num_properties > 0) {
  989. char val[1024];
  990. for (i = 0; i < wireGeom.num_properties; i++) {
  991. nRead += XkmGetCountedString(file, buf, 100);
  992. nRead += XkmGetCountedString(file, val, 1024);
  993. if (XkbAddGeomProperty(geom, buf, val) == NULL) {
  994. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeometry", 0);
  995. return nRead;
  996. }
  997. }
  998. }
  999. if (wireGeom.num_colors > 0) {
  1000. for (i = 0; i < wireGeom.num_colors; i++) {
  1001. nRead += XkmGetCountedString(file, buf, 100);
  1002. if (XkbAddGeomColor(geom, buf, i) == NULL) {
  1003. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeometry", 0);
  1004. return nRead;
  1005. }
  1006. }
  1007. }
  1008. geom->base_color = &geom->colors[wireGeom.base_color_ndx];
  1009. geom->label_color = &geom->colors[wireGeom.label_color_ndx];
  1010. if (wireGeom.num_shapes > 0) {
  1011. XkbShapePtr shape;
  1012. xkmShapeDesc shapeWire;
  1013. Atom nameAtom;
  1014. for (i = 0; i < wireGeom.num_shapes; i++) {
  1015. register int n;
  1016. XkbOutlinePtr ol;
  1017. xkmOutlineDesc olWire;
  1018. nRead += XkmGetCountedString(file, buf, 100);
  1019. nameAtom = XkbInternAtom(buf, FALSE);
  1020. tmp = fread(&shapeWire, SIZEOF(xkmShapeDesc), 1, file);
  1021. nRead += tmp * SIZEOF(xkmShapeDesc);
  1022. shape = XkbAddGeomShape(geom, nameAtom, shapeWire.num_outlines);
  1023. if (!shape) {
  1024. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeometry", 0);
  1025. return nRead;
  1026. }
  1027. for (n = 0; n < shapeWire.num_outlines; n++) {
  1028. register int p;
  1029. xkmPointDesc ptWire;
  1030. tmp = fread(&olWire, SIZEOF(xkmOutlineDesc), 1, file);
  1031. nRead += tmp * SIZEOF(xkmOutlineDesc);
  1032. ol = XkbAddGeomOutline(shape, olWire.num_points);
  1033. if (!ol) {
  1034. _XkbLibError(_XkbErrBadAlloc, "ReadXkmGeometry", 0);
  1035. return nRead;
  1036. }
  1037. ol->num_points = olWire.num_points;
  1038. ol->corner_radius = olWire.corner_radius;
  1039. for (p = 0; p < olWire.num_points; p++) {
  1040. tmp = fread(&ptWire, SIZEOF(xkmPointDesc), 1, file);
  1041. nRead += tmp * SIZEOF(xkmPointDesc);
  1042. ol->points[p].x = ptWire.x;
  1043. ol->points[p].y = ptWire.y;
  1044. if (ptWire.x < shape->bounds.x1)
  1045. shape->bounds.x1 = ptWire.x;
  1046. if (ptWire.x > shape->bounds.x2)
  1047. shape->bounds.x2 = ptWire.x;
  1048. if (ptWire.y < shape->bounds.y1)
  1049. shape->bounds.y1 = ptWire.y;
  1050. if (ptWire.y > shape->bounds.y2)
  1051. shape->bounds.y2 = ptWire.y;
  1052. }
  1053. }
  1054. if (shapeWire.primary_ndx != XkbNoShape)
  1055. shape->primary = &shape->outlines[shapeWire.primary_ndx];
  1056. if (shapeWire.approx_ndx != XkbNoShape)
  1057. shape->approx = &shape->outlines[shapeWire.approx_ndx];
  1058. }
  1059. }
  1060. if (wireGeom.num_sections > 0) {
  1061. for (i = 0; i < wireGeom.num_sections; i++) {
  1062. tmp = ReadXkmGeomSection(file, geom);
  1063. nRead += tmp;
  1064. if (tmp == 0)
  1065. return nRead;
  1066. }
  1067. }
  1068. if (wireGeom.num_doodads > 0) {
  1069. for (i = 0; i < wireGeom.num_doodads; i++) {
  1070. tmp = ReadXkmGeomDoodad(file, geom, NULL);
  1071. nRead += tmp;
  1072. if (tmp == 0)
  1073. return nRead;
  1074. }
  1075. }
  1076. if ((wireGeom.num_key_aliases > 0) && (geom->key_aliases)) {
  1077. int sz = XkbKeyNameLength * 2;
  1078. int num = wireGeom.num_key_aliases;
  1079. if (fread(geom->key_aliases, sz, num, file) != num) {
  1080. _XkbLibError(_XkbErrBadLength, "ReadXkmGeometry", 0);
  1081. return -1;
  1082. }
  1083. nRead += (num * sz);
  1084. geom->num_key_aliases = num;
  1085. }
  1086. return nRead;
  1087. }
  1088. Bool
  1089. XkmProbe(FILE * file)
  1090. {
  1091. unsigned hdr, tmp;
  1092. int nRead = 0;
  1093. hdr = (('x' << 24) | ('k' << 16) | ('m' << 8) | XkmFileVersion);
  1094. tmp = XkmGetCARD32(file, &nRead);
  1095. if (tmp != hdr) {
  1096. if ((tmp & (~0xff)) == (hdr & (~0xff))) {
  1097. _XkbLibError(_XkbErrBadFileVersion, "XkmProbe", tmp & 0xff);
  1098. }
  1099. return 0;
  1100. }
  1101. return 1;
  1102. }
  1103. static Bool
  1104. XkmReadTOC(FILE * file, xkmFileInfo * file_info, int max_toc,
  1105. xkmSectionInfo * toc)
  1106. {
  1107. unsigned hdr, tmp;
  1108. int nRead = 0;
  1109. unsigned i, size_toc;
  1110. hdr = (('x' << 24) | ('k' << 16) | ('m' << 8) | XkmFileVersion);
  1111. tmp = XkmGetCARD32(file, &nRead);
  1112. if (tmp != hdr) {
  1113. if ((tmp & (~0xff)) == (hdr & (~0xff))) {
  1114. _XkbLibError(_XkbErrBadFileVersion, "XkmReadTOC", tmp & 0xff);
  1115. }
  1116. else {
  1117. _XkbLibError(_XkbErrBadFileType, "XkmReadTOC", tmp);
  1118. }
  1119. return 0;
  1120. }
  1121. if (fread(file_info, SIZEOF(xkmFileInfo), 1, file) != 1)
  1122. return 0;
  1123. size_toc = file_info->num_toc;
  1124. if (size_toc > max_toc) {
  1125. DebugF("Warning! Too many TOC entries; last %d ignored\n",
  1126. size_toc - max_toc);
  1127. size_toc = max_toc;
  1128. }
  1129. for (i = 0; i < size_toc; i++) {
  1130. if (fread(&toc[i], SIZEOF(xkmSectionInfo), 1, file) != 1)
  1131. return 0;
  1132. }
  1133. return 1;
  1134. }
  1135. /***====================================================================***/
  1136. #define MAX_TOC 16
  1137. unsigned
  1138. XkmReadFile(FILE * file, unsigned need, unsigned want, XkbDescPtr *xkb)
  1139. {
  1140. register unsigned i;
  1141. xkmSectionInfo toc[MAX_TOC], tmpTOC;
  1142. xkmFileInfo fileInfo;
  1143. unsigned tmp, nRead = 0;
  1144. unsigned which = need | want;
  1145. if (!XkmReadTOC(file, &fileInfo, MAX_TOC, toc))
  1146. return which;
  1147. if ((fileInfo.present & need) != need) {
  1148. _XkbLibError(_XkbErrIllegalContents, "XkmReadFile",
  1149. need & (~fileInfo.present));
  1150. return which;
  1151. }
  1152. if (*xkb == NULL)
  1153. *xkb = XkbAllocKeyboard();
  1154. for (i = 0; i < fileInfo.num_toc; i++) {
  1155. fseek(file, toc[i].offset, SEEK_SET);
  1156. tmp = fread(&tmpTOC, SIZEOF(xkmSectionInfo), 1, file);
  1157. nRead = tmp * SIZEOF(xkmSectionInfo);
  1158. if ((tmpTOC.type != toc[i].type) || (tmpTOC.format != toc[i].format) ||
  1159. (tmpTOC.size != toc[i].size) || (tmpTOC.offset != toc[i].offset)) {
  1160. return which;
  1161. }
  1162. if ((which & (1 << tmpTOC.type)) == 0) {
  1163. continue;
  1164. }
  1165. switch (tmpTOC.type) {
  1166. case XkmVirtualModsIndex:
  1167. tmp = ReadXkmVirtualMods(file, *xkb, NULL);
  1168. break;
  1169. case XkmTypesIndex:
  1170. tmp = ReadXkmKeyTypes(file, *xkb, NULL);
  1171. break;
  1172. case XkmCompatMapIndex:
  1173. tmp = ReadXkmCompatMap(file, *xkb, NULL);
  1174. break;
  1175. case XkmKeyNamesIndex:
  1176. tmp = ReadXkmKeycodes(file, *xkb, NULL);
  1177. break;
  1178. case XkmIndicatorsIndex:
  1179. tmp = ReadXkmIndicators(file, *xkb, NULL);
  1180. break;
  1181. case XkmSymbolsIndex:
  1182. tmp = ReadXkmSymbols(file, *xkb);
  1183. break;
  1184. case XkmGeometryIndex:
  1185. tmp = ReadXkmGeometry(file, *xkb);
  1186. break;
  1187. default:
  1188. _XkbLibError(_XkbErrBadImplementation,
  1189. XkbConfigText(tmpTOC.type, XkbMessage), 0);
  1190. tmp = 0;
  1191. break;
  1192. }
  1193. if (tmp > 0) {
  1194. nRead += tmp;
  1195. which &= ~(1 << toc[i].type);
  1196. (*xkb)->defined |= (1 << toc[i].type);
  1197. }
  1198. if (nRead != tmpTOC.size) {
  1199. _XkbLibError(_XkbErrBadLength,
  1200. XkbConfigText(tmpTOC.type, XkbMessage),
  1201. nRead - tmpTOC.size);
  1202. }
  1203. }
  1204. return which;
  1205. }