home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / Xmu / Lookup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  10.6 KB  |  307 lines

  1. /* "$XConsortium: Lookup.c,v 1.12 90/12/11 13:19:10 rws Exp $"; */
  2.  
  3. /* 
  4.  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided 
  8.  * that the above copyright notice appear in all copies and that both that 
  9.  * copyright notice and this permission notice appear in supporting 
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific, 
  12.  * written prior permission. M.I.T. makes no representations about the 
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  */
  17.  
  18. #include <X11/Xlib.h>
  19. #include <X11/Xutil.h>
  20. #define XK_LATIN1
  21. #define XK_PUBLISHING
  22. #include <X11/keysymdef.h>
  23.  
  24. #if __STDC__
  25. #define Const const
  26. #else
  27. #define Const /**/
  28. #endif
  29.  
  30. /* bit (1<<i) means character is in codeset i */
  31. static unsigned short Const latin1[128] =
  32.   {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  33.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  34.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  35.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  36.    0x10ee, 0x0000, 0x1000, 0x1084, 0x102e, 0x1000, 0x1080, 0x108e, /* 10 */
  37.    0x108e, 0x1080, 0x0000, 0x1080, 0x1080, 0x10ee, 0x1000, 0x1008,
  38.    0x108e, 0x1080, 0x1084, 0x1084, 0x108e, 0x1004, 0x1000, 0x1084, /* 11 */
  39.    0x100e, 0x1000, 0x0000, 0x1080, 0x1000, 0x1084, 0x1000, 0x0000,
  40.    0x0004, 0x000e, 0x000e, 0x0008, 0x000e, 0x0008, 0x0008, 0x0006, /* 12 */
  41.    0x0004, 0x000e, 0x0004, 0x000e, 0x0004, 0x000e, 0x000e, 0x0004,
  42.    0x0000, 0x0004, 0x0004, 0x0006, 0x000e, 0x0008, 0x000e, 0x000e, /* 13 */
  43.    0x0008, 0x0004, 0x000e, 0x000c, 0x000e, 0x0002, 0x0000, 0x000e,
  44.    0x0004, 0x000e, 0x000e, 0x0008, 0x000e, 0x0008, 0x0008, 0x0006, /* 14 */
  45.    0x0004, 0x000e, 0x0004, 0x000e, 0x0004, 0x000e, 0x000e, 0x0004,
  46.    0x0000, 0x0004, 0x0004, 0x0006, 0x000e, 0x0008, 0x000e, 0x000e, /* 15 */
  47.    0x0008, 0x0004, 0x000e, 0x000c, 0x000e, 0x0002, 0x0000, 0x0000};
  48.  
  49. /* bit (1<<i) means character is in codeset i */
  50. static unsigned short Const latin2[128] =
  51.   {0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  52.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  53.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  54.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  55.    0x0000, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 10 */
  56.    0x0000, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0008, 0x0004,
  57.    0x0000, 0x0008, 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0008, /* 11 */
  58.    0x0000, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0008, 0x0004,
  59.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 12 */
  60.    0x0008, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  61.    0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 13 */
  62.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  63.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 14 */
  64.    0x0008, 0x0000, 0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
  65.    0x0008, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /* 15 */
  66.    0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x000c};
  67.  
  68. /* maps Cyrillic keysyms to 8859-5 */
  69. static unsigned char Const cyrillic[128] =
  70.    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74.     0x00, 0xf2, 0xf3, 0xf1, 0xf4, 0xf5, 0xf6, 0xf7, /* 10 */
  75.     0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0x00, 0xfe, 0xff,
  76.     0xf0, 0xa2, 0xa3, 0xa1, 0xa4, 0xa5, 0xa6, 0xa7, /* 11 */
  77.     0xa8, 0xa9, 0xaa, 0xab, 0xac, 0x00, 0xae, 0xaf,
  78.     0xee, 0xd0, 0xd1, 0xe6, 0xd4, 0xd5, 0xe4, 0xd3, /* 12 */
  79.     0xe5, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde,
  80.     0xdf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xd6, 0xd2, /* 13 */
  81.     0xec, 0xeb, 0xd7, 0xe8, 0xed, 0xe9, 0xe7, 0xea,
  82.     0xce, 0xb0, 0xb1, 0xc6, 0xb4, 0xb5, 0xc4, 0xb3, /* 14 */
  83.     0xc5, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe,
  84.     0xbf, 0xcf, 0xc0, 0xc1, 0xc2, 0xc3, 0xb6, 0xb2, /* 15 */
  85.     0xcc, 0xcb, 0xb7, 0xc8, 0xcd, 0xc9, 0xc7, 0xca};
  86.  
  87. /* maps Greek keysyms to 8859-7 */
  88. static unsigned char Const greek[128] =
  89.    {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  90.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  91.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  92.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  93.     0x00, 0xb6, 0xb8, 0xb9, 0xba, 0xda, 0x00, 0xbc, /* 10 */
  94.     0xbe, 0xdb, 0x00, 0xbf, 0x00, 0x00, 0xb5, 0xaf,
  95.     0x00, 0xdc, 0xdd, 0xde, 0xdf, 0xfa, 0xc0, 0xfc, /* 11 */
  96.     0xfd, 0xfb, 0xe0, 0xfe, 0x00, 0x00, 0x00, 0x00,
  97.     0x00, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, /* 12 */
  98.     0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
  99.     0xd0, 0xd1, 0xd3, 0x00, 0xd4, 0xd5, 0xd6, 0xd7, /* 13 */
  100.     0xd8, 0xd9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  101.     0x00, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, /* 14 */
  102.     0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
  103.     0xf0, 0xf1, 0xf3, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7, /* 15 */
  104.     0xf8, 0xf9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  105.  
  106. #define sLatin1        0
  107. #define sLatin2        1
  108. #define sLatin3        2
  109. #define sLatin4        3
  110. #define sKana        4
  111. #define sX0201        0x01000004
  112. #define sArabic        5
  113. #define sCyrillic    6
  114. #define sGreek        7
  115. #define sAPL        11
  116. #define sHebrew        12
  117.  
  118. int XmuLookupString (event, buffer, nbytes, keysym, status, keysymSet)
  119.     register XKeyEvent *event;
  120.     char *buffer;
  121.     int nbytes;
  122.     KeySym *keysym;
  123.     XComposeStatus *status;
  124.     unsigned long keysymSet;
  125. {
  126.     int count;
  127.     KeySym symbol;
  128.     unsigned long kset;
  129.  
  130.     kset = keysymSet & 0xffffff;
  131.     count = XLookupString(event, buffer, nbytes, &symbol, status);
  132.     if (keysym) *keysym = symbol;
  133.     if ((nbytes == 0) || (symbol == NoSymbol)) {
  134.     /* nothing */
  135.     } else if ((count == 0) && ((symbol >> 8) == kset)) {
  136.     count = 1;
  137.     switch (keysymSet) {
  138.     case sKana:
  139.         buffer[0] = (symbol & 0xff);
  140.         if (buffer[0] == 0x7e)
  141.         count = 0;
  142.         break;
  143.     case sCyrillic:
  144.         buffer[0] = cyrillic[symbol & 0x7f];
  145.         break;
  146.     case sGreek:
  147.         buffer[0] = greek[symbol & 0x7f];
  148.         if (!buffer[0])
  149.         count = 0;
  150.         break;
  151.     default:
  152.         buffer[0] = (symbol & 0xff);
  153.         break;
  154.     }
  155.     } else if ((keysymSet != 0) && (count == 1) &&
  156.            (((unsigned char *)buffer)[0] == symbol) &&
  157.            (symbol & 0x80) &&
  158.            !(latin1[symbol & 0x7f] & (1 << kset))) {
  159.     if ((keysymSet == sHebrew) && (symbol == XK_multiply))
  160.         buffer[0] = 0xaa;
  161.     else if ((keysymSet == sHebrew) && (symbol == XK_division))
  162.         buffer[0] = 0xba;
  163.     else if ((keysymSet == sCyrillic) && (symbol == XK_section))
  164.         buffer[0] = 0xfd;
  165.     else if ((keysymSet == sX0201) && (symbol == XK_yen))
  166.         buffer[0] = 0x5c;
  167.     else
  168.         count = 0;
  169.     } else if (count != 0) {
  170.     if ((keysymSet == sX0201) &&
  171.         ((symbol == XK_backslash) || (symbol == XK_asciitilde)))
  172.         count = 0;
  173.     } else if (((symbol >> 8) == sLatin2) &&
  174.            (symbol & 0x80) && (latin2[symbol & 0x7f] & (1 << kset))) {
  175.     buffer[0] = (symbol & 0xff);
  176.     count = 1;
  177.     } else if ((keysymSet == sGreek) &&
  178.            ((symbol == XK_leftsinglequotemark) ||
  179.         (symbol == XK_rightsinglequotemark))) {
  180.     buffer[0] = symbol - (XK_leftsinglequotemark - 0xa1);
  181.     count = 1;
  182.     }
  183.     return count;
  184. }
  185.  
  186. /* produces ISO 8859-1 encoding plus ASCII control */
  187. int XmuLookupLatin1 (event, buffer, nbytes, keysym, status)
  188.     register XKeyEvent *event;
  189.     char *buffer;
  190.     int nbytes;
  191.     KeySym *keysym;
  192.     XComposeStatus *status;
  193. {
  194.     return XLookupString(event, buffer, nbytes, keysym, status);
  195. }
  196.  
  197. /* produces ISO 8859-2 encoding plus ASCII control */
  198. int XmuLookupLatin2 (event, buffer, nbytes, keysym, status)
  199.     register XKeyEvent *event;
  200.     char *buffer;
  201.     int nbytes;
  202.     KeySym *keysym;
  203.     XComposeStatus *status;
  204. {
  205.     return XmuLookupString(event, buffer, nbytes, keysym, status, sLatin2);
  206. }
  207.  
  208. /* produces ISO 8859-3 encoding plus ASCII control */
  209. int XmuLookupLatin3 (event, buffer, nbytes, keysym, status)
  210.     register XKeyEvent *event;
  211.     char *buffer;
  212.     int nbytes;
  213.     KeySym *keysym;
  214.     XComposeStatus *status;
  215. {
  216.     return XmuLookupString(event, buffer, nbytes, keysym, status, sLatin3);
  217. }
  218.  
  219. /* produces ISO 8859-4 encoding plus ASCII control */
  220. int XmuLookupLatin4 (event, buffer, nbytes, keysym, status)
  221.     register XKeyEvent *event;
  222.     char *buffer;
  223.     int nbytes;
  224.     KeySym *keysym;
  225.     XComposeStatus *status;
  226. {
  227.     return XmuLookupString(event, buffer, nbytes, keysym, status, sLatin4);
  228. }
  229.  
  230. /* produces ISO 8859-1 GL plus Katakana plus ASCII control */
  231. int XmuLookupKana (event, buffer, nbytes, keysym, status)
  232.     register XKeyEvent *event;
  233.     char *buffer;
  234.     int nbytes;
  235.     KeySym *keysym;
  236.     XComposeStatus *status;
  237. {
  238.     return XmuLookupString(event, buffer, nbytes, keysym, status, sKana);
  239. }
  240.  
  241. /* produces JIS X0201-1976 (8-bit) */
  242. int XmuLookupJISX0201 (event, buffer, nbytes, keysym, status)
  243.     register XKeyEvent *event;
  244.     char *buffer;
  245.     int nbytes;
  246.     KeySym *keysym;
  247.     XComposeStatus *status;
  248. {
  249.     return XmuLookupString(event, buffer, nbytes, keysym, status, sX0201);
  250. }
  251.  
  252. /* produces ISO 8859-6 encoding plus ASCII control */
  253. int XmuLookupArabic (event, buffer, nbytes, keysym, status)
  254.     register XKeyEvent *event;
  255.     char *buffer;
  256.     int nbytes;
  257.     KeySym *keysym;
  258.     XComposeStatus *status;
  259. {
  260.     return XmuLookupString(event, buffer, nbytes, keysym, status, sArabic);
  261. }
  262.  
  263. /* produces ISO/IEC 8859-5 encoding plus ASCII control */
  264. int XmuLookupCyrillic (event, buffer, nbytes, keysym, status)
  265.     register XKeyEvent *event;
  266.     char *buffer;
  267.     int nbytes;
  268.     KeySym *keysym;
  269.     XComposeStatus *status;
  270. {
  271.     return XmuLookupString(event, buffer, nbytes, keysym, status, sCyrillic);
  272. }
  273.  
  274. /* produces ISO 8859-7 encoding plus ASCII control */
  275. int XmuLookupGreek (event, buffer, nbytes, keysym, status)
  276.     register XKeyEvent *event;
  277.     char *buffer;
  278.     int nbytes;
  279.     KeySym *keysym;
  280.     XComposeStatus *status;
  281. {
  282.     return XmuLookupString(event, buffer, nbytes, keysym, status, sGreek);
  283. }
  284.  
  285. /* XXX this character set needs work */
  286.  
  287. int XmuLookupAPL (event, buffer, nbytes, keysym, status)
  288.     register XKeyEvent *event;
  289.     char *buffer;
  290.     int nbytes;
  291.     KeySym *keysym;
  292.     XComposeStatus *status;
  293. {
  294.     return XmuLookupString(event, buffer, nbytes, keysym, status, sAPL);
  295. }
  296.  
  297. /* produces ISO 8859-8 encoding plus ASCII control */
  298. int XmuLookupHebrew (event, buffer, nbytes, keysym, status)
  299.     register XKeyEvent *event;
  300.     char *buffer;
  301.     int nbytes;
  302.     KeySym *keysym;
  303.     XComposeStatus *status;
  304. {
  305.     return XmuLookupString(event, buffer, nbytes, keysym, status, sHebrew);
  306. }
  307.