xkbfmisc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /************************************************************
  2. Copyright (c) 1995 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 <ctype.h>
  28. #include <stdlib.h>
  29. #include <X11/Xos.h>
  30. #include <X11/Xfuncs.h>
  31. #include <X11/extensions/XKMformat.h>
  32. #include <X11/X.h>
  33. #include <X11/keysym.h>
  34. #include <X11/Xproto.h>
  35. #include "misc.h"
  36. #include "inputstr.h"
  37. #include "dix.h"
  38. #include "xkbstr.h"
  39. #define XKBSRV_NEED_FILE_FUNCS 1
  40. #include <xkbsrv.h>
  41. #include "xkbgeom.h"
  42. #include "xkb.h"
  43. unsigned
  44. _XkbKSCheckCase(KeySym ks)
  45. {
  46. unsigned set, rtrn;
  47. set = (ks & (~0xff)) >> 8;
  48. rtrn = 0;
  49. switch (set) {
  50. case 0: /* latin 1 */
  51. if (((ks >= XK_A) && (ks <= XK_Z)) ||
  52. ((ks >= XK_Agrave) && (ks <= XK_THORN) && (ks != XK_multiply))) {
  53. rtrn |= _XkbKSUpper;
  54. }
  55. if (((ks >= XK_a) && (ks <= XK_z)) ||
  56. ((ks >= XK_ssharp) && (ks <= XK_ydiaeresis) &&
  57. (ks != XK_division))) {
  58. rtrn |= _XkbKSLower;
  59. }
  60. break;
  61. case 1: /* latin 2 */
  62. if (((ks >= XK_Aogonek) && (ks <= XK_Zabovedot) && (ks != XK_breve)) ||
  63. ((ks >= XK_Racute) && (ks <= XK_Tcedilla))) {
  64. rtrn |= _XkbKSUpper;
  65. }
  66. if (((ks >= XK_aogonek) && (ks <= XK_zabovedot) && (ks != XK_ogonek) &&
  67. (ks != XK_caron) && (ks != XK_doubleacute)) || ((ks >= XK_racute)
  68. && (ks <=
  69. XK_tcedilla)))
  70. {
  71. rtrn |= _XkbKSLower;
  72. }
  73. break;
  74. case 2: /* latin 3 */
  75. if (((ks >= XK_Hstroke) && (ks <= XK_Jcircumflex)) ||
  76. ((ks >= XK_Cabovedot) && (ks <= XK_Scircumflex))) {
  77. rtrn |= _XkbKSUpper;
  78. }
  79. if (((ks >= XK_hstroke) && (ks <= XK_jcircumflex)) ||
  80. ((ks >= XK_cabovedot) && (ks <= XK_scircumflex))) {
  81. rtrn |= _XkbKSLower;
  82. }
  83. break;
  84. case 3: /* latin 4 */
  85. if (((ks >= XK_Rcedilla) && (ks <= XK_Tslash)) ||
  86. (ks == XK_ENG) || ((ks >= XK_Amacron) && (ks <= XK_Umacron))) {
  87. rtrn |= _XkbKSUpper;
  88. }
  89. if ((ks == XK_kra) ||
  90. ((ks >= XK_rcedilla) && (ks <= XK_tslash)) ||
  91. (ks == XK_eng) || ((ks >= XK_amacron) && (ks <= XK_umacron))) {
  92. rtrn |= _XkbKSLower;
  93. }
  94. break;
  95. case 18: /* latin 8 */
  96. if ((ks == XK_Wcircumflex) ||
  97. (ks == XK_Ycircumflex) ||
  98. (ks == XK_Babovedot) ||
  99. (ks == XK_Dabovedot) ||
  100. (ks == XK_Fabovedot) ||
  101. (ks == XK_Mabovedot) ||
  102. (ks == XK_Pabovedot) ||
  103. (ks == XK_Sabovedot) ||
  104. (ks == XK_Tabovedot) ||
  105. (ks == XK_Wgrave) ||
  106. (ks == XK_Wacute) || (ks == XK_Wdiaeresis) || (ks == XK_Ygrave)) {
  107. rtrn |= _XkbKSUpper;
  108. }
  109. if ((ks == XK_wcircumflex) ||
  110. (ks == XK_ycircumflex) ||
  111. (ks == XK_babovedot) ||
  112. (ks == XK_dabovedot) ||
  113. (ks == XK_fabovedot) ||
  114. (ks == XK_mabovedot) ||
  115. (ks == XK_pabovedot) ||
  116. (ks == XK_sabovedot) ||
  117. (ks == XK_tabovedot) ||
  118. (ks == XK_wgrave) ||
  119. (ks == XK_wacute) || (ks == XK_wdiaeresis) || (ks == XK_ygrave)) {
  120. rtrn |= _XkbKSLower;
  121. }
  122. break;
  123. case 19: /* latin 9 */
  124. if ((ks == XK_OE) || (ks == XK_Ydiaeresis)) {
  125. rtrn |= _XkbKSUpper;
  126. }
  127. if (ks == XK_oe) {
  128. rtrn |= _XkbKSLower;
  129. }
  130. break;
  131. }
  132. return rtrn;
  133. }
  134. /***===================================================================***/
  135. static Bool
  136. XkbWriteSectionFromName(FILE * file, const char *sectionName, const char *name)
  137. {
  138. fprintf(file, " xkb_%-20s { include \"%s\" };\n", sectionName, name);
  139. return TRUE;
  140. }
  141. #define NEED_DESC(n) ((!n)||((n)[0]=='+')||((n)[0]=='|')||(strchr((n),'%')))
  142. #define COMPLETE(n) ((n)&&(!NEED_DESC(n)))
  143. /* ARGSUSED */
  144. static void
  145. _AddIncl(FILE * file,
  146. XkbDescPtr xkb,
  147. Bool topLevel, Bool showImplicit, int index, void *priv)
  148. {
  149. if ((priv) && (strcmp((char *) priv, "%") != 0))
  150. fprintf(file, " include \"%s\"\n", (char *) priv);
  151. return;
  152. }
  153. Bool
  154. XkbWriteXKBKeymapForNames(FILE * file,
  155. XkbComponentNamesPtr names,
  156. XkbDescPtr xkb, unsigned want, unsigned need)
  157. {
  158. const char *tmp;
  159. unsigned complete;
  160. XkbNamesPtr old_names;
  161. int multi_section;
  162. unsigned wantNames, wantConfig, wantDflts;
  163. complete = 0;
  164. if (COMPLETE(names->keycodes))
  165. complete |= XkmKeyNamesMask;
  166. if (COMPLETE(names->types))
  167. complete |= XkmTypesMask;
  168. if (COMPLETE(names->compat))
  169. complete |= XkmCompatMapMask;
  170. if (COMPLETE(names->symbols))
  171. complete |= XkmSymbolsMask;
  172. if (COMPLETE(names->geometry))
  173. complete |= XkmGeometryMask;
  174. want |= (complete | need);
  175. if (want & XkmSymbolsMask)
  176. want |= XkmKeyNamesMask | XkmTypesMask;
  177. if (want == 0)
  178. return FALSE;
  179. if (xkb) {
  180. old_names = xkb->names;
  181. xkb->defined = 0;
  182. /* Wow would it ever be neat if we didn't need this noise. */
  183. if (xkb->names && xkb->names->keys)
  184. xkb->defined |= XkmKeyNamesMask;
  185. if (xkb->map && xkb->map->types)
  186. xkb->defined |= XkmTypesMask;
  187. if (xkb->compat)
  188. xkb->defined |= XkmCompatMapMask;
  189. if (xkb->map && xkb->map->num_syms)
  190. xkb->defined |= XkmSymbolsMask;
  191. if (xkb->indicators)
  192. xkb->defined |= XkmIndicatorsMask;
  193. if (xkb->geom)
  194. xkb->defined |= XkmGeometryMask;
  195. }
  196. else {
  197. old_names = NULL;
  198. }
  199. wantConfig = want & (~complete);
  200. if (xkb != NULL) {
  201. if (wantConfig & XkmTypesMask) {
  202. if ((!xkb->map) || (xkb->map->num_types < XkbNumRequiredTypes))
  203. wantConfig &= ~XkmTypesMask;
  204. }
  205. if (wantConfig & XkmCompatMapMask) {
  206. if ((!xkb->compat) || (xkb->compat->num_si < 1))
  207. wantConfig &= ~XkmCompatMapMask;
  208. }
  209. if (wantConfig & XkmSymbolsMask) {
  210. if ((!xkb->map) || (!xkb->map->key_sym_map))
  211. wantConfig &= ~XkmSymbolsMask;
  212. }
  213. if (wantConfig & XkmIndicatorsMask) {
  214. if (!xkb->indicators)
  215. wantConfig &= ~XkmIndicatorsMask;
  216. }
  217. if (wantConfig & XkmKeyNamesMask) {
  218. if ((!xkb->names) || (!xkb->names->keys))
  219. wantConfig &= ~XkmKeyNamesMask;
  220. }
  221. if ((wantConfig & XkmGeometryMask) && (!xkb->geom))
  222. wantConfig &= ~XkmGeometryMask;
  223. }
  224. else {
  225. wantConfig = 0;
  226. }
  227. complete |= wantConfig;
  228. wantDflts = 0;
  229. wantNames = want & (~complete);
  230. if ((xkb != NULL) && (old_names != NULL)) {
  231. if (wantNames & XkmTypesMask) {
  232. if (old_names->types != None) {
  233. tmp = NameForAtom(old_names->types);
  234. names->types = Xstrdup(tmp);
  235. }
  236. else {
  237. wantDflts |= XkmTypesMask;
  238. }
  239. complete |= XkmTypesMask;
  240. }
  241. if (wantNames & XkmCompatMapMask) {
  242. if (old_names->compat != None) {
  243. tmp = NameForAtom(old_names->compat);
  244. names->compat = Xstrdup(tmp);
  245. }
  246. else
  247. wantDflts |= XkmCompatMapMask;
  248. complete |= XkmCompatMapMask;
  249. }
  250. if (wantNames & XkmSymbolsMask) {
  251. if (old_names->symbols == None)
  252. return FALSE;
  253. tmp = NameForAtom(old_names->symbols);
  254. names->symbols = Xstrdup(tmp);
  255. complete |= XkmSymbolsMask;
  256. }
  257. if (wantNames & XkmKeyNamesMask) {
  258. if (old_names->keycodes != None) {
  259. tmp = NameForAtom(old_names->keycodes);
  260. names->keycodes = Xstrdup(tmp);
  261. }
  262. else
  263. wantDflts |= XkmKeyNamesMask;
  264. complete |= XkmKeyNamesMask;
  265. }
  266. if (wantNames & XkmGeometryMask) {
  267. if (old_names->geometry == None)
  268. return FALSE;
  269. tmp = NameForAtom(old_names->geometry);
  270. names->geometry = Xstrdup(tmp);
  271. complete |= XkmGeometryMask;
  272. wantNames &= ~XkmGeometryMask;
  273. }
  274. }
  275. if (complete & XkmCompatMapMask)
  276. complete |= XkmIndicatorsMask | XkmVirtualModsMask;
  277. else if (complete & (XkmSymbolsMask | XkmTypesMask))
  278. complete |= XkmVirtualModsMask;
  279. if (need & (~complete))
  280. return FALSE;
  281. if ((complete & XkmSymbolsMask) &&
  282. ((XkmKeyNamesMask | XkmTypesMask) & (~complete)))
  283. return FALSE;
  284. multi_section = 1;
  285. if (((complete & XkmKeymapRequired) == XkmKeymapRequired) &&
  286. ((complete & (~XkmKeymapLegal)) == 0)) {
  287. fprintf(file, "xkb_keymap \"default\" {\n");
  288. }
  289. else if (((complete & XkmSemanticsRequired) == XkmSemanticsRequired) &&
  290. ((complete & (~XkmSemanticsLegal)) == 0)) {
  291. fprintf(file, "xkb_semantics \"default\" {\n");
  292. }
  293. else if (((complete & XkmLayoutRequired) == XkmLayoutRequired) &&
  294. ((complete & (~XkmLayoutLegal)) == 0)) {
  295. fprintf(file, "xkb_layout \"default\" {\n");
  296. }
  297. else if (XkmSingleSection(complete & (~XkmVirtualModsMask))) {
  298. multi_section = 0;
  299. }
  300. else {
  301. return FALSE;
  302. }
  303. wantNames = complete & (~(wantConfig | wantDflts));
  304. if (wantConfig & XkmKeyNamesMask)
  305. XkbWriteXKBKeycodes(file, xkb, FALSE, FALSE, _AddIncl, names->keycodes);
  306. else if (wantDflts & XkmKeyNamesMask)
  307. fprintf(stderr, "Default symbols not implemented yet!\n");
  308. else if (wantNames & XkmKeyNamesMask)
  309. XkbWriteSectionFromName(file, "keycodes", names->keycodes);
  310. if (wantConfig & XkmTypesMask)
  311. XkbWriteXKBKeyTypes(file, xkb, FALSE, FALSE, _AddIncl, names->types);
  312. else if (wantDflts & XkmTypesMask)
  313. fprintf(stderr, "Default types not implemented yet!\n");
  314. else if (wantNames & XkmTypesMask)
  315. XkbWriteSectionFromName(file, "types", names->types);
  316. if (wantConfig & XkmCompatMapMask)
  317. XkbWriteXKBCompatMap(file, xkb, FALSE, FALSE, _AddIncl, names->compat);
  318. else if (wantDflts & XkmCompatMapMask)
  319. fprintf(stderr, "Default interps not implemented yet!\n");
  320. else if (wantNames & XkmCompatMapMask)
  321. XkbWriteSectionFromName(file, "compatibility", names->compat);
  322. if (wantConfig & XkmSymbolsMask)
  323. XkbWriteXKBSymbols(file, xkb, FALSE, FALSE, _AddIncl, names->symbols);
  324. else if (wantNames & XkmSymbolsMask)
  325. XkbWriteSectionFromName(file, "symbols", names->symbols);
  326. if (wantConfig & XkmGeometryMask)
  327. XkbWriteXKBGeometry(file, xkb, FALSE, FALSE, _AddIncl, names->geometry);
  328. else if (wantNames & XkmGeometryMask)
  329. XkbWriteSectionFromName(file, "geometry", names->geometry);
  330. if (multi_section)
  331. fprintf(file, "};\n");
  332. return TRUE;
  333. }
  334. /***====================================================================***/
  335. int
  336. XkbFindKeycodeByName(XkbDescPtr xkb, char *name, Bool use_aliases)
  337. {
  338. register int i;
  339. if ((!xkb) || (!xkb->names) || (!xkb->names->keys))
  340. return 0;
  341. for (i = xkb->min_key_code; i <= xkb->max_key_code; i++) {
  342. if (strncmp(xkb->names->keys[i].name, name, XkbKeyNameLength) == 0)
  343. return i;
  344. }
  345. if (!use_aliases)
  346. return 0;
  347. if (xkb->geom && xkb->geom->key_aliases) {
  348. XkbKeyAliasPtr a;
  349. a = xkb->geom->key_aliases;
  350. for (i = 0; i < xkb->geom->num_key_aliases; i++, a++) {
  351. if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
  352. return XkbFindKeycodeByName(xkb, a->real, FALSE);
  353. }
  354. }
  355. if (xkb->names && xkb->names->key_aliases) {
  356. XkbKeyAliasPtr a;
  357. a = xkb->names->key_aliases;
  358. for (i = 0; i < xkb->names->num_key_aliases; i++, a++) {
  359. if (strncmp(name, a->alias, XkbKeyNameLength) == 0)
  360. return XkbFindKeycodeByName(xkb, a->real, FALSE);
  361. }
  362. }
  363. return 0;
  364. }
  365. unsigned
  366. XkbConvertGetByNameComponents(Bool toXkm, unsigned orig)
  367. {
  368. unsigned rtrn;
  369. rtrn = 0;
  370. if (toXkm) {
  371. if (orig & XkbGBN_TypesMask)
  372. rtrn |= XkmTypesMask;
  373. if (orig & XkbGBN_CompatMapMask)
  374. rtrn |= XkmCompatMapMask;
  375. if (orig & XkbGBN_SymbolsMask)
  376. rtrn |= XkmSymbolsMask;
  377. if (orig & XkbGBN_IndicatorMapMask)
  378. rtrn |= XkmIndicatorsMask;
  379. if (orig & XkbGBN_KeyNamesMask)
  380. rtrn |= XkmKeyNamesMask;
  381. if (orig & XkbGBN_GeometryMask)
  382. rtrn |= XkmGeometryMask;
  383. }
  384. else {
  385. if (orig & XkmTypesMask)
  386. rtrn |= XkbGBN_TypesMask;
  387. if (orig & XkmCompatMapMask)
  388. rtrn |= XkbGBN_CompatMapMask;
  389. if (orig & XkmSymbolsMask)
  390. rtrn |= XkbGBN_SymbolsMask;
  391. if (orig & XkmIndicatorsMask)
  392. rtrn |= XkbGBN_IndicatorMapMask;
  393. if (orig & XkmKeyNamesMask)
  394. rtrn |= XkbGBN_KeyNamesMask;
  395. if (orig & XkmGeometryMask)
  396. rtrn |= XkbGBN_GeometryMask;
  397. if (orig != 0)
  398. rtrn |= XkbGBN_OtherNamesMask;
  399. }
  400. return rtrn;
  401. }
  402. /***====================================================================***/
  403. #define UNMATCHABLE(c) (((c)=='(')||((c)==')')||((c)=='/'))
  404. Bool
  405. XkbNameMatchesPattern(char *name, char *ptrn)
  406. {
  407. while (ptrn[0] != '\0') {
  408. if (name[0] == '\0') {
  409. if (ptrn[0] == '*') {
  410. ptrn++;
  411. continue;
  412. }
  413. return FALSE;
  414. }
  415. if (ptrn[0] == '?') {
  416. if (UNMATCHABLE(name[0]))
  417. return FALSE;
  418. }
  419. else if (ptrn[0] == '*') {
  420. if ((!UNMATCHABLE(name[0])) &&
  421. XkbNameMatchesPattern(name + 1, ptrn))
  422. return TRUE;
  423. return XkbNameMatchesPattern(name, ptrn + 1);
  424. }
  425. else if (ptrn[0] != name[0])
  426. return FALSE;
  427. name++;
  428. ptrn++;
  429. }
  430. /* if we get here, the pattern is exhausted (-:just like me:-) */
  431. return name[0] == '\0';
  432. }