PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/nx-3.5.0/nx-X11/lib/X11/imThaiFlt.c

#
C | 1375 lines | 993 code | 147 blank | 235 comment | 300 complexity | d03a2da156bce2bc7e232b8a8c3cb54c MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, LGPL-2.0
  1. /* $Xorg: imThaiFlt.c,v 1.5 2001/02/09 02:03:39 xorgcvs Exp $ */
  2. /***********************************************************
  3. Copyright 1993, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. Copyright 1993 by Digital Equipment Corporation, Maynard, Massachusetts.
  21. All Rights Reserved
  22. Permission to use, copy, modify, and distribute this software and its
  23. documentation for any purpose and without fee is hereby granted,
  24. provided that the above copyright notice appear in all copies and that
  25. both that copyright notice and this permission notice appear in
  26. supporting documentation, and that the name of Digital not be
  27. used in advertising or publicity pertaining to distribution of the
  28. software without specific, written prior permission.
  29. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  30. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  31. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  32. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  33. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  34. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  35. SOFTWARE.
  36. ******************************************************************/
  37. /* $XFree86: xc/lib/X11/imThaiFlt.c,v 3.22tsi Exp $ */
  38. /*
  39. **++
  40. ** FACILITY:
  41. **
  42. ** Xlib
  43. **
  44. ** ABSTRACT:
  45. **
  46. ** Thai specific functions.
  47. ** Handles character classifications, composibility checking,
  48. ** Input sequence check and other Thai specific requirements
  49. ** according to WTT specification and DEC extensions.
  50. **
  51. ** MODIFICATION HISTORY:
  52. **
  53. **/
  54. #ifdef HAVE_CONFIG_H
  55. #include <config.h>
  56. #endif
  57. #include <stdio.h>
  58. #include <X11/Xlib.h>
  59. #include <X11/Xmd.h>
  60. #include <X11/keysym.h>
  61. #include <X11/Xutil.h>
  62. #include "Xlibint.h"
  63. #include "Xlcint.h"
  64. #include "Ximint.h"
  65. #include "XimThai.h"
  66. #include "XlcPubI.h"
  67. #define SPACE 32
  68. /* character classification table */
  69. #define TACTIS_CHARS 256
  70. Private
  71. char const tactis_chtype[TACTIS_CHARS] = {
  72. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 0 - 7 */
  73. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 8 - 15 */
  74. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 16 - 23 */
  75. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 24 - 31 */
  76. NON, NON, NON, NON, NON, NON, NON, NON, /* 32 - 39 */
  77. NON, NON, NON, NON, NON, NON, NON, NON, /* 40 - 47 */
  78. NON, NON, NON, NON, NON, NON, NON, NON, /* 48 - 55 */
  79. NON, NON, NON, NON, NON, NON, NON, NON, /* 56 - 63 */
  80. NON, NON, NON, NON, NON, NON, NON, NON, /* 64 - 71 */
  81. NON, NON, NON, NON, NON, NON, NON, NON, /* 72 - 79 */
  82. NON, NON, NON, NON, NON, NON, NON, NON, /* 80 - 87 */
  83. NON, NON, NON, NON, NON, NON, NON, NON, /* 88 - 95 */
  84. NON, NON, NON, NON, NON, NON, NON, NON, /* 96 - 103 */
  85. NON, NON, NON, NON, NON, NON, NON, NON, /* 104 - 111 */
  86. NON, NON, NON, NON, NON, NON, NON, NON, /* 112 - 119 */
  87. NON, NON, NON, NON, NON, NON, NON, CTRL, /* 120 - 127 */
  88. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 128 - 135 */
  89. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 136 - 143 */
  90. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 144 - 151 */
  91. CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, CTRL, /* 152 - 159 */
  92. NON, CONS, CONS, CONS, CONS, CONS, CONS, CONS, /* 160 - 167 */
  93. CONS, CONS, CONS, CONS, CONS, CONS, CONS, CONS, /* 168 - 175 */
  94. CONS, CONS, CONS, CONS, CONS, CONS, CONS, CONS, /* 176 - 183 */
  95. CONS, CONS, CONS, CONS, CONS, CONS, CONS, CONS, /* 184 - 191 */
  96. CONS, CONS, CONS, CONS, FV3, CONS, FV3, CONS, /* 192 - 199 */
  97. CONS, CONS, CONS, CONS, CONS, CONS, CONS, NON, /* 200 - 207 */
  98. FV1, AV2, FV1, FV1, AV1, AV3, AV2, AV3, /* 208 - 215 */
  99. BV1, BV2, BD, NON, NON, NON, NON, NON, /* 216 - 223 */
  100. LV, LV, LV, LV, LV, FV2, NON, AD2, /* 224 - 231 */
  101. TONE, TONE, TONE, TONE, AD1, AD1, AD3, NON, /* 232 - 239 */
  102. NON, NON, NON, NON, NON, NON, NON, NON, /* 240 - 247 */
  103. NON, NON, NON, NON, NON, NON, NON, CTRL /* 248 - 255 */
  104. };
  105. /* Composibility checking tables */
  106. #define NC 0 /* NOT COMPOSIBLE - following char displays in next cell */
  107. #define CP 1 /* COMPOSIBLE - following char is displayed in the same cell
  108. as leading char, also implies ACCEPT */
  109. #define XC 3 /* Non-display */
  110. #define AC 4 /* ACCEPT - display the following char in the next cell */
  111. #define RJ 5 /* REJECT - discard that following char, ignore it */
  112. #define CH_CLASSES 17 /* 17 classes of chars */
  113. Private
  114. char const write_rules_lookup[CH_CLASSES][CH_CLASSES] = {
  115. /* Table 0: writing/outputing rules */
  116. /* row: leading char, column: following char */
  117. /* CTRL NON CONS LV FV1 FV2 FV3 BV1 BV2 BD TONE AD1 AD2 AD3 AV1 AV2 AV3 */
  118. {XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*CTRL*/
  119. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*NON*/
  120. ,{XC, NC, NC, NC, NC, NC, NC, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP}/*CONS*/
  121. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*LV*/
  122. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*FV1*/
  123. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*FV2*/
  124. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*FV3*/
  125. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, CP, CP, NC, NC, NC, NC, NC}/*BV1*/
  126. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, CP, NC, NC, NC, NC, NC, NC}/*BV2*/
  127. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*BD*/
  128. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*TONE*/
  129. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*AD1*/
  130. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*AD2*/
  131. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC, NC}/*AD3*/
  132. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, CP, CP, NC, NC, NC, NC, NC}/*AV1*/
  133. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, CP, NC, NC, NC, NC, NC, NC}/*AV2*/
  134. ,{XC, NC, NC, NC, NC, NC, NC, NC, NC, NC, CP, NC, CP, NC, NC, NC, NC}/*AV3*/
  135. };
  136. Private
  137. char const wtt_isc1_lookup[CH_CLASSES][CH_CLASSES] = {
  138. /* Table 1: WTT default input sequence check rules */
  139. /* row: leading char, column: following char */
  140. /* CTRL NON CONS LV FV1 FV2 FV3 BV1 BV2 BD TONE AD1 AD2 AD3 AV1 AV2 AV3 */
  141. {XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*CTRL*/
  142. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*NON*/
  143. ,{XC, AC, AC, AC, AC, AC, AC, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP}/*CONS*/
  144. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*LV*/
  145. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV1*/
  146. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV2*/
  147. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV3*/
  148. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, CP, RJ, RJ, RJ, RJ, RJ}/*BV1*/
  149. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, RJ, RJ, RJ, RJ, RJ, RJ}/*BV2*/
  150. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*BD*/
  151. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*TONE*/
  152. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD1*/
  153. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD2*/
  154. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD3*/
  155. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, CP, RJ, RJ, RJ, RJ, RJ}/*AV1*/
  156. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, RJ, RJ, RJ, RJ, RJ, RJ}/*AV2*/
  157. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, RJ, CP, RJ, RJ, RJ, RJ}/*AV3*/
  158. };
  159. Private
  160. char const wtt_isc2_lookup[CH_CLASSES][CH_CLASSES] = {
  161. /* Table 2: WTT strict input sequence check rules */
  162. /* row: leading char, column: following char */
  163. /* CTRL NON CONS LV FV1 FV2 FV3 BV1 BV2 BD TONE AD1 AD2 AD3 AV1 AV2 AV3 */
  164. {XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*CTRL*/
  165. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*NON*/
  166. ,{XC, AC, AC, AC, AC, RJ, AC, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP}/*CONS*/
  167. ,{XC, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*LV*/
  168. ,{XC, AC, AC, AC, AC, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV1*/
  169. ,{XC, AC, AC, AC, AC, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV2*/
  170. ,{XC, AC, AC, AC, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV3*/
  171. ,{XC, AC, AC, AC, AC, RJ, AC, RJ, RJ, RJ, CP, CP, RJ, RJ, RJ, RJ, RJ}/*BV1*/
  172. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, CP, RJ, RJ, RJ, RJ, RJ, RJ}/*BV2*/
  173. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*BD*/
  174. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*TONE*/
  175. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD1*/
  176. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD2*/
  177. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD3*/
  178. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, CP, CP, RJ, RJ, RJ, RJ, RJ}/*AV1*/
  179. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, CP, RJ, RJ, RJ, RJ, RJ, RJ}/*AV2*/
  180. ,{XC, AC, AC, AC, RJ, RJ, AC, RJ, RJ, RJ, CP, RJ, CP, RJ, RJ, RJ, RJ}/*AV3*/
  181. };
  182. Private
  183. char const thaicat_isc_lookup[CH_CLASSES][CH_CLASSES] = {
  184. /* Table 3: Thaicat input sequence check rules */
  185. /* row: leading char, column: following char */
  186. /* CTRL NON CONS LV FV1 FV2 FV3 BV1 BV2 BD TONE AD1 AD2 AD3 AV1 AV2 AV3 */
  187. {XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*CTRL*/
  188. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*NON*/
  189. ,{XC, AC, AC, AC, AC, AC, AC, CP, CP, CP, CP, CP, CP, CP, CP, CP, CP}/*CONS*/
  190. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*LV*/
  191. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV1*/
  192. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*FV2*/
  193. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ} /*FV3*/
  194. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, CP, RJ, RJ, RJ, RJ, RJ}/*BV1*/
  195. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, RJ, RJ, RJ, RJ, RJ, RJ}/*BV2*/
  196. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*BD*/
  197. ,{XC, AC, AC, AC, AC, AC, AC, CP, CP, RJ, RJ, RJ, RJ, RJ, CP, CP, CP}/*TONE*/
  198. ,{XC, AC, AC, AC, AC, AC, AC, CP, RJ, RJ, RJ, RJ, RJ, RJ, CP, RJ, RJ}/*AD1*/
  199. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, CP}/*AD2*/
  200. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ, RJ}/*AD3*/
  201. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, CP, RJ, RJ, RJ, RJ, RJ}/*AV1*/
  202. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, RJ, RJ, RJ, RJ, RJ, RJ}/*AV2*/
  203. ,{XC, AC, AC, AC, AC, AC, AC, RJ, RJ, RJ, CP, RJ, CP, RJ, RJ, RJ, RJ}/*AV3*/
  204. };
  205. /* returns classification of a char */
  206. Private int
  207. THAI_chtype (unsigned char ch)
  208. {
  209. return tactis_chtype[ch];
  210. }
  211. #ifdef UNUSED
  212. /* returns the display level */
  213. Private int
  214. THAI_chlevel (unsigned char ch)
  215. {
  216. int chlevel;
  217. switch (tactis_chtype[ch])
  218. {
  219. case CTRL:
  220. chlevel = NON;
  221. break;
  222. case BV1:
  223. case BV2:
  224. case BD:
  225. chlevel = BELOW;
  226. break;
  227. case TONE:
  228. case AD1:
  229. case AD2:
  230. chlevel = TOP;
  231. break;
  232. case AV1:
  233. case AV2:
  234. case AV3:
  235. case AD3:
  236. chlevel = ABOVE;
  237. break;
  238. case NON:
  239. case CONS:
  240. case LV:
  241. case FV1:
  242. case FV2:
  243. case FV3:
  244. default: /* if tactis_chtype is invalid */
  245. chlevel = BASE;
  246. break;
  247. }
  248. return chlevel;
  249. }
  250. /* return True if char is non-spacing */
  251. Private Bool
  252. THAI_isdead (unsigned char ch)
  253. {
  254. return ((tactis_chtype[ch] == CTRL) || (tactis_chtype[ch] == BV1) ||
  255. (tactis_chtype[ch] == BV2) || (tactis_chtype[ch] == BD) ||
  256. (tactis_chtype[ch] == TONE) || (tactis_chtype[ch] == AD1) ||
  257. (tactis_chtype[ch] == AD2) || (tactis_chtype[ch] == AD3) ||
  258. (tactis_chtype[ch] == AV1) || (tactis_chtype[ch] == AV2) ||
  259. (tactis_chtype[ch] == AV3));
  260. }
  261. /* return True if char is consonant */
  262. Private Bool
  263. THAI_iscons (unsigned char ch)
  264. {
  265. return (tactis_chtype[ch] == CONS);
  266. }
  267. /* return True if char is vowel */
  268. Private Bool
  269. THAI_isvowel (unsigned char ch)
  270. {
  271. return ((tactis_chtype[ch] == LV) || (tactis_chtype[ch] == FV1) ||
  272. (tactis_chtype[ch] == FV2) || (tactis_chtype[ch] == FV3) ||
  273. (tactis_chtype[ch] == BV1) || (tactis_chtype[ch] == BV2) ||
  274. (tactis_chtype[ch] == AV1) || (tactis_chtype[ch] == AV2) ||
  275. (tactis_chtype[ch] == AV3));
  276. }
  277. /* return True if char is tonemark */
  278. Private Bool
  279. THAI_istone (unsigned char ch)
  280. {
  281. return (tactis_chtype[ch] == TONE);
  282. }
  283. #endif
  284. Private Bool
  285. THAI_iscomposible (
  286. unsigned char follow_ch,
  287. unsigned char lead_ch)
  288. {/* "Can follow_ch be put in the same display cell as lead_ch?" */
  289. return (write_rules_lookup[THAI_chtype(lead_ch)][THAI_chtype(follow_ch)]
  290. == CP);
  291. }
  292. Private Bool
  293. THAI_isaccepted (
  294. unsigned char follow_ch,
  295. unsigned char lead_ch,
  296. unsigned char mode)
  297. {
  298. Bool iskeyvalid; /* means "Can follow_ch be keyed in after lead_ch?" */
  299. switch (mode)
  300. {
  301. case WTT_ISC1:
  302. iskeyvalid =
  303. (wtt_isc1_lookup[THAI_chtype(lead_ch)][THAI_chtype(follow_ch)] != RJ);
  304. break;
  305. case WTT_ISC2:
  306. iskeyvalid =
  307. (wtt_isc2_lookup[THAI_chtype(lead_ch)][THAI_chtype(follow_ch)] != RJ);
  308. break;
  309. case THAICAT_ISC:
  310. iskeyvalid =
  311. (thaicat_isc_lookup[THAI_chtype(lead_ch)][THAI_chtype(follow_ch)] != RJ);
  312. break;
  313. default:
  314. iskeyvalid = True;
  315. break;
  316. }
  317. return iskeyvalid;
  318. }
  319. #ifdef UNUSED
  320. Private void
  321. THAI_apply_write_rules(
  322. unsigned char *instr,
  323. unsigned char *outstr,
  324. unsigned char insert_ch,
  325. int *num_insert_ch)
  326. {
  327. /*
  328. Input parameters:
  329. instr - input string
  330. insert_ch specify what char to be added when invalid composition is found
  331. Output parameters:
  332. outstr - output string after input string has been applied the rules
  333. num_insert_ch - number of insert_ch added to outstr.
  334. */
  335. unsigned char *lead_ch = NULL, *follow_ch = NULL, *out_ch = NULL;
  336. *num_insert_ch = 0;
  337. lead_ch = follow_ch = instr;
  338. out_ch = outstr;
  339. if ((*lead_ch == '\0') || !(THAI_find_chtype(instr,DEAD)))
  340. { /* Empty string or can't find any non-spacing char*/
  341. strcpy((char *)outstr, (char *)instr);
  342. } else { /* String of length >= 1, keep looking */
  343. follow_ch++;
  344. if (THAI_isdead(*lead_ch)) { /* is first char non-spacing? */
  345. *out_ch++ = SPACE;
  346. (*num_insert_ch)++;
  347. }
  348. *out_ch++ = *lead_ch;
  349. while (*follow_ch != '\0') /* more char in string to check */
  350. {
  351. if (THAI_isdead(*follow_ch) &&
  352. !THAI_iscomposible(*follow_ch,*lead_ch))
  353. {
  354. *out_ch++ = SPACE;
  355. (*num_insert_ch)++;
  356. }
  357. *out_ch++ = *follow_ch;
  358. lead_ch = follow_ch;
  359. follow_ch++;
  360. }
  361. *out_ch = '\0';
  362. }
  363. }
  364. Private int
  365. THAI_find_chtype (
  366. unsigned char *instr,
  367. int chtype)
  368. {
  369. /*
  370. Input parameters:
  371. instr - input string
  372. chtype - type of character to look for
  373. Output parameters:
  374. function returns first position of character with matched chtype
  375. function returns -1 if it does not find.
  376. */
  377. int i = 0, position = -1;
  378. switch (chtype)
  379. {
  380. case DEAD:
  381. for (i = 0; *instr != '\0' && THAI_isdead(*instr); i++, instr++)
  382. ;
  383. if (*instr != '\0') position = i;
  384. break;
  385. default:
  386. break;
  387. }
  388. return position;
  389. }
  390. Private int
  391. THAI_apply_scm(
  392. unsigned char *instr,
  393. unsigned char *outstr,
  394. unsigned char spec_ch,
  395. int num_sp,
  396. unsigned char insert_ch)
  397. {
  398. unsigned char *scan, *outch;
  399. int i, dead_count, found_count;
  400. Bool isconsecutive;
  401. scan = instr;
  402. outch = outstr;
  403. dead_count = found_count = 0;
  404. isconsecutive = False;
  405. while (*scan != '\0') {
  406. if (THAI_isdead(*scan))
  407. dead_count++; /* count number of non-spacing char */
  408. if (*scan == spec_ch)
  409. if (!isconsecutive)
  410. found_count++; /* count number consecutive spec char found */
  411. *outch++ = *scan++;
  412. if (found_count == num_sp) {
  413. for (i = 0; i < dead_count; i++)
  414. *outch++ = insert_ch;
  415. dead_count = found_count = 0;
  416. }
  417. }
  418. /* what to return? */
  419. return 0; /* probably not right but better than returning garbage */
  420. }
  421. /* The following functions are copied from XKeyBind.c */
  422. Private void ComputeMaskFromKeytrans();
  423. Private int IsCancelComposeKey(KeySym *symbol, XKeyEvent *event);
  424. Private void SetLed(Display *dpy, int num, int state);
  425. Private CARD8 FindKeyCode();
  426. /* The following functions are specific to this module */
  427. Private int XThaiTranslateKey();
  428. Private int XThaiTranslateKeySym();
  429. Private KeySym HexIMNormalKey(
  430. XicThaiPart *thai_part,
  431. KeySym symbol,
  432. XKeyEvent *event);
  433. Private KeySym HexIMFirstComposeKey(
  434. XicThaiPart *thai_part,
  435. KeySym symbol,
  436. XKeyEvent *event);
  437. Private KeySym HexIMSecondComposeKey(
  438. XicThaiPart *thai_part,
  439. KeySym symbol
  440. XKeyEvent *event);
  441. Private KeySym HexIMComposeSequence(KeySym ks1, KeySym ks2);
  442. Private void InitIscMode(Xic ic);
  443. Private Bool ThaiComposeConvert(
  444. Display *dpy,
  445. KeySym insym,
  446. KeySym *outsym, KeySym *lower, KeySym *upper);
  447. #endif
  448. /*
  449. * Definitions
  450. */
  451. #define BellVolume 0
  452. #define ucs2tis(wc) \
  453. (unsigned char) ( \
  454. (0<=(wc)&&(wc)<=0x7F) ? \
  455. (wc) : \
  456. ((0x0E01<=(wc)&&(wc)<=0x0E5F) ? ((wc)-0x0E00+0xA0) : 0))
  457. /* "c" is an unsigned char */
  458. #define tis2ucs(c) \
  459. ( \
  460. ((c)<=0x7F) ? \
  461. (wchar_t)(c) : \
  462. ((0x0A1<=(c)) ? ((wchar_t)(c)-0xA0+0x0E00) : 0))
  463. /*
  464. * Macros to save and recall last input character in XIC
  465. */
  466. #define IC_SavePreviousChar(ic,ch) \
  467. (*((ic)->private.local.context->mb) = (char) (ch))
  468. #define IC_ClearPreviousChar(ic) \
  469. (*((ic)->private.local.context->mb) = 0)
  470. #define IC_GetPreviousChar(ic) \
  471. (IC_RealGetPreviousChar(ic,1))
  472. #define IC_GetContextChar(ic) \
  473. (IC_RealGetPreviousChar(ic,2))
  474. #define IC_DeletePreviousChar(ic) \
  475. (IC_RealDeletePreviousChar(ic))
  476. Private unsigned char
  477. IC_RealGetPreviousChar(Xic ic, unsigned short pos)
  478. {
  479. XICCallback* cb = &ic->core.string_conversion_callback;
  480. if (cb && cb->callback) {
  481. XIMStringConversionCallbackStruct screc;
  482. unsigned char c;
  483. /* Use a safe value of position = 0 and stretch the range to desired
  484. * place, as XIM protocol is unclear here whether it could be negative
  485. */
  486. screc.position = 0;
  487. screc.direction = XIMBackwardChar;
  488. screc.operation = XIMStringConversionRetrieval;
  489. screc.factor = pos;
  490. screc.text = 0;
  491. (cb->callback)((XIC)ic, cb->client_data, (XPointer)&screc);
  492. if (!screc.text)
  493. return (unsigned char) *((ic)->private.local.context->mb);
  494. if ((screc.text->feedback &&
  495. *screc.text->feedback == XIMStringConversionLeftEdge) ||
  496. screc.text->length < 1)
  497. {
  498. c = 0;
  499. } else {
  500. if (screc.text->encoding_is_wchar) {
  501. c = ucs2tis(screc.text->string.wcs[0]);
  502. XFree(screc.text->string.wcs);
  503. } else {
  504. c = screc.text->string.mbs[0];
  505. XFree(screc.text->string.mbs);
  506. }
  507. }
  508. XFree(screc.text);
  509. return c;
  510. } else {
  511. return (unsigned char) *((ic)->private.local.context->mb);
  512. }
  513. }
  514. Private unsigned char
  515. IC_RealDeletePreviousChar(Xic ic)
  516. {
  517. XICCallback* cb = &ic->core.string_conversion_callback;
  518. if (cb && cb->callback) {
  519. XIMStringConversionCallbackStruct screc;
  520. unsigned char c;
  521. screc.position = 0;
  522. screc.direction = XIMBackwardChar;
  523. screc.operation = XIMStringConversionSubstitution;
  524. screc.factor = 1;
  525. screc.text = 0;
  526. (cb->callback)((XIC)ic, cb->client_data, (XPointer)&screc);
  527. if (!screc.text) { return 0; }
  528. if ((screc.text->feedback &&
  529. *screc.text->feedback == XIMStringConversionLeftEdge) ||
  530. screc.text->length < 1)
  531. {
  532. c = 0;
  533. } else {
  534. if (screc.text->encoding_is_wchar) {
  535. c = ucs2tis(screc.text->string.wcs[0]);
  536. XFree(screc.text->string.wcs);
  537. } else {
  538. c = screc.text->string.mbs[0];
  539. XFree(screc.text->string.mbs);
  540. }
  541. }
  542. XFree(screc.text);
  543. return c;
  544. } else {
  545. return 0;
  546. }
  547. }
  548. /*
  549. * Input sequence check mode in XIC
  550. */
  551. #define IC_IscMode(ic) ((ic)->private.local.thai.input_mode)
  552. /*
  553. * Max. size of string handled by the two String Lookup functions.
  554. */
  555. #define STR_LKUP_BUF_SIZE 256
  556. /*
  557. * Size of buffer to contain previous locale name.
  558. */
  559. #define SAV_LOCALE_NAME_SIZE 256
  560. /*
  561. * Size of buffer to contain the IM modifier.
  562. */
  563. #define MAXTHAIIMMODLEN 20
  564. #define AllMods (ShiftMask|LockMask|ControlMask| \
  565. Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)
  566. #define IsISOControlKey(ks) ((ks) >= XK_2 && (ks) <= XK_8)
  567. #define IsValidControlKey(ks) (((((ks)>=XK_A && (ks)<=XK_asciitilde) || \
  568. (ks)==XK_space || (ks)==XK_Delete) && \
  569. ((ks)!=0)))
  570. #define COMPOSE_LED 2
  571. #ifdef UNUSED
  572. typedef KeySym (*StateProc)(
  573. XicThaiPart *thai_part,
  574. KeySym symbol,
  575. XKeyEvent *event);
  576. /*
  577. * macros to classify XKeyEvent state field
  578. */
  579. #define IsShift(state) (((state) & ShiftMask) != 0)
  580. #define IsLock(state) (((state) & LockMask) != 0)
  581. #define IsControl(state) (((state) & ControlMask) != 0)
  582. #define IsMod1(state) (((state) & Mod1Mask) != 0)
  583. #define IsMod2(state) (((state) & Mod2Mask) != 0)
  584. #define IsMod3(state) (((state) & Mod3Mask) != 0)
  585. #define IsMod4(state) (((state) & Mod4Mask) != 0)
  586. #define IsMod5(state) (((state) & Mod5Mask) != 0)
  587. /*
  588. * key starts Thai compose sequence (Hex input method) if :
  589. */
  590. #define IsComposeKey(ks, event) \
  591. (( ks==XK_Alt_L && \
  592. IsControl((event)->state) && \
  593. !IsShift((event)->state)) \
  594. ? True : False)
  595. /*
  596. * State handler to implement the Thai hex input method.
  597. */
  598. Private int const nstate_handlers = 3;
  599. Private StateProc state_handler[] = {
  600. HexIMNormalKey,
  601. HexIMFirstComposeKey,
  602. HexIMSecondComposeKey
  603. };
  604. /*
  605. * Table for 'Thai Compose' character input.
  606. * The current implementation uses latin-1 keysyms.
  607. */
  608. struct _XMapThaiKey {
  609. KeySym from;
  610. KeySym to;
  611. };
  612. Private struct _XMapThaiKey const ThaiComposeTable[] = {
  613. { /* 0xa4 */ XK_currency, /* 0xa5 */ XK_yen },
  614. { /* 0xa2 */ XK_cent, /* 0xa3 */ XK_sterling },
  615. { /* 0xe6 */ XK_ae, /* 0xef */ XK_idiaeresis },
  616. { /* 0xd3 */ XK_Oacute, /* 0xee */ XK_icircumflex },
  617. { /* 0xb9 */ XK_onesuperior, /* 0xfa */ XK_uacute },
  618. { /* 0xd2 */ XK_Ograve, /* 0xe5 */ XK_aring },
  619. { /* 0xbc */ XK_onequarter, /* 0xfb */ XK_ucircumflex },
  620. { XK_VoidSymbol, XK_VoidSymbol }
  621. };
  622. struct _XKeytrans {
  623. struct _XKeytrans *next;/* next on list */
  624. char *string; /* string to return when the time comes */
  625. int len; /* length of string (since NULL is legit)*/
  626. KeySym key; /* keysym rebound */
  627. unsigned int state; /* modifier state */
  628. KeySym *modifiers; /* modifier keysyms you want */
  629. int mlen; /* length of modifier list */
  630. };
  631. /* Convert keysym to 'Thai Compose' keysym */
  632. /* The current implementation use latin-1 keysyms */
  633. Private Bool
  634. ThaiComposeConvert(
  635. Display *dpy,
  636. KeySym insym,
  637. KeySym *outsym, KeySym *lower, KeySym *upper)
  638. {
  639. struct _XMapThaiKey const *table_entry = ThaiComposeTable;
  640. while (table_entry->from != XK_VoidSymbol) {
  641. if (table_entry->from == insym) {
  642. *outsym = table_entry->to;
  643. *lower = *outsym;
  644. *upper = *outsym;
  645. return True;
  646. }
  647. table_entry++;
  648. }
  649. return False;
  650. }
  651. Private int
  652. XThaiTranslateKey(
  653. register Display *dpy,
  654. KeyCode keycode,
  655. register unsigned int modifiers,
  656. unsigned int *modifiers_return,
  657. KeySym *keysym_return,
  658. KeySym *lsym_return,
  659. KeySym *usym_return)
  660. {
  661. int per;
  662. register KeySym *syms;
  663. KeySym sym = 0, lsym = 0, usym = 0;
  664. if ((! dpy->keysyms) && (! _XKeyInitialize(dpy)))
  665. return 0;
  666. *modifiers_return = (ShiftMask|LockMask) | dpy->mode_switch;
  667. if (((int)keycode < dpy->min_keycode) || ((int)keycode > dpy->max_keycode))
  668. {
  669. *keysym_return = NoSymbol;
  670. return 1;
  671. }
  672. per = dpy->keysyms_per_keycode;
  673. syms = &dpy->keysyms[(keycode - dpy->min_keycode) * per];
  674. while ((per > 2) && (syms[per - 1] == NoSymbol))
  675. per--;
  676. if ((per > 2) && (modifiers & dpy->mode_switch)) {
  677. syms += 2;
  678. per -= 2;
  679. }
  680. if (!(modifiers & ShiftMask) &&
  681. (!(modifiers & LockMask) || (dpy->lock_meaning == NoSymbol))) {
  682. if ((per == 1) || (syms[1] == NoSymbol))
  683. XConvertCase(syms[0], keysym_return, &usym);
  684. else {
  685. XConvertCase(syms[0], &lsym, &usym);
  686. *keysym_return = syms[0];
  687. }
  688. } else if (!(modifiers & LockMask) ||
  689. (dpy->lock_meaning != XK_Caps_Lock)) {
  690. if ((per == 1) || ((usym = syms[1]) == NoSymbol))
  691. XConvertCase(syms[0], &lsym, &usym);
  692. *keysym_return = usym;
  693. } else {
  694. if ((per == 1) || ((sym = syms[1]) == NoSymbol))
  695. sym = syms[0];
  696. XConvertCase(sym, &lsym, &usym);
  697. if (!(modifiers & ShiftMask) && (sym != syms[0]) &&
  698. ((sym != usym) || (lsym == usym)))
  699. XConvertCase(syms[0], &lsym, &usym);
  700. *keysym_return = usym;
  701. }
  702. /*
  703. * ThaiCat keyboard support :
  704. * When the Shift and Thai keys are hold for some keys a 'Thai Compose'
  705. * character code is generated which is different from column 3 and
  706. * 4 of the keymap.
  707. * Since we don't know whether ThaiCat keyboard or WTT keyboard is
  708. * in use, the same mapping is done for all Thai input.
  709. * We just arbitary choose to use column 3 keysyms as the indices of
  710. * this mapping.
  711. * When the control key is also hold, this mapping has no effect.
  712. */
  713. if ((modifiers & Mod1Mask) &&
  714. (modifiers & ShiftMask) &&
  715. !(modifiers & ControlMask)) {
  716. if (ThaiComposeConvert(dpy, syms[0], &sym, &lsym, &usym))
  717. *keysym_return = sym;
  718. }
  719. if (*keysym_return == XK_VoidSymbol)
  720. *keysym_return = NoSymbol;
  721. *lsym_return = lsym;
  722. *usym_return = usym;
  723. return 1;
  724. }
  725. /*
  726. * XThaiTranslateKeySym
  727. *
  728. * Translate KeySym to TACTIS code output.
  729. * The current implementation uses ISO latin-1 keysym.
  730. * Should be changed to TACTIS keysyms when they are defined by the
  731. * standard.
  732. */
  733. Private int
  734. XThaiTranslateKeySym(
  735. Display *dpy,
  736. register KeySym symbol,
  737. register KeySym lsym,
  738. register KeySym usym,
  739. unsigned int modifiers,
  740. unsigned char *buffer,
  741. int nbytes)
  742. {
  743. KeySym ckey = 0;
  744. register struct _XKeytrans *p;
  745. int length;
  746. unsigned long hiBytes;
  747. register unsigned char c;
  748. /*
  749. * initialize length = 1 ;
  750. */
  751. length = 1;
  752. if (!symbol)
  753. return 0;
  754. /* see if symbol rebound, if so, return that string. */
  755. for (p = dpy->key_bindings; p; p = p->next) {
  756. if (((modifiers & AllMods) == p->state) && (symbol == p->key)) {
  757. length = p->len;
  758. if (length > nbytes) length = nbytes;
  759. memcpy (buffer, p->string, length);
  760. return length;
  761. }
  762. }
  763. /* try to convert to TACTIS, handling control */
  764. hiBytes = symbol >> 8;
  765. if (!(nbytes &&
  766. ((hiBytes == 0) ||
  767. ((hiBytes == 0xFF) &&
  768. (((symbol >= XK_BackSpace) && (symbol <= XK_Clear)) ||
  769. (symbol == XK_Return) ||
  770. (symbol == XK_Escape) ||
  771. (symbol == XK_KP_Space) ||
  772. (symbol == XK_KP_Tab) ||
  773. (symbol == XK_KP_Enter) ||
  774. ((symbol >= XK_KP_Multiply) && (symbol <= XK_KP_9)) ||
  775. (symbol == XK_KP_Equal) ||
  776. (symbol == XK_Scroll_Lock) ||
  777. #ifdef DXK_PRIVATE /* DEC private keysyms */
  778. (symbol == DXK_Remove) ||
  779. #endif
  780. (symbol == NoSymbol) ||
  781. (symbol == XK_Delete))))))
  782. return 0;
  783. /* if X keysym, convert to ascii by grabbing low 7 bits */
  784. if (symbol == XK_KP_Space)
  785. c = XK_space & 0x7F; /* patch encoding botch */
  786. /* not for Thai
  787. else if (symbol == XK_hyphen)
  788. c = XK_minus & 0xFF; */ /* map to equiv character */
  789. else if (hiBytes == 0xFF)
  790. c = symbol & 0x7F;
  791. else
  792. c = symbol & 0xFF;
  793. /* only apply Control key if it makes sense, else ignore it */
  794. if (modifiers & ControlMask) {
  795. if (!(IsKeypadKey(lsym) || lsym==XK_Return || lsym==XK_Tab)) {
  796. if (IsISOControlKey(lsym)) ckey = lsym;
  797. else if (IsISOControlKey(usym)) ckey = usym;
  798. else if (lsym == XK_question) ckey = lsym;
  799. else if (usym == XK_question) ckey = usym;
  800. else if (IsValidControlKey(lsym)) ckey = lsym;
  801. else if (IsValidControlKey(usym)) ckey = usym;
  802. else length = 0;
  803. if (length != 0) {
  804. if (ckey == XK_2) c = '\000';
  805. else if (ckey >= XK_3 && ckey <= XK_7)
  806. c = (char)(ckey-('3'-'\033'));
  807. else if (ckey == XK_8) c = '\177';
  808. else if (ckey == XK_Delete) c = '\030';
  809. else if (ckey == XK_question) c = '\037';
  810. else if (ckey == XK_quoteleft) c = '\036'; /* KLee 1/24/91 */
  811. else c = (char)(ckey & 0x1f);
  812. }
  813. }
  814. }
  815. /*
  816. * ThaiCat has a key that generates two TACTIS codes D1 & E9.
  817. * It is represented by the latin-1 keysym XK_thorn (0xfe).
  818. * If c is XK_thorn, this key is pressed and it is converted to
  819. * 0xd1 0xe9.
  820. */
  821. if (c == XK_thorn) {
  822. buffer[0] = 0xd1;
  823. buffer[1] = 0xe9;
  824. buffer[2] = '\0';
  825. return 2;
  826. }
  827. else {
  828. /* Normal case */
  829. buffer[0] = c;
  830. buffer[1] = '\0';
  831. return 1;
  832. }
  833. }
  834. /*
  835. * given a KeySym, returns the first keycode containing it, if any.
  836. */
  837. Private CARD8
  838. FindKeyCode(
  839. register Display *dpy,
  840. register KeySym code)
  841. {
  842. register KeySym *kmax = dpy->keysyms +
  843. (dpy->max_keycode - dpy->min_keycode + 1) * dpy->keysyms_per_keycode;
  844. register KeySym *k = dpy->keysyms;
  845. while (k < kmax) {
  846. if (*k == code)
  847. return (((k - dpy->keysyms) / dpy->keysyms_per_keycode) +
  848. dpy->min_keycode);
  849. k += 1;
  850. }
  851. return 0;
  852. }
  853. /*
  854. * given a list of modifiers, computes the mask necessary for later matching.
  855. * This routine must lookup the key in the Keymap and then search to see
  856. * what modifier it is bound to, if any. Sets the AnyModifier bit if it
  857. * can't map some keysym to a modifier.
  858. */
  859. Private void
  860. ComputeMaskFromKeytrans(
  861. Display *dpy,
  862. register struct _XKeytrans *p)
  863. {
  864. register int i;
  865. register CARD8 code;
  866. register XModifierKeymap *m = dpy->modifiermap;
  867. p->state = AnyModifier;
  868. for (i = 0; i < p->mlen; i++) {
  869. /* if not found, then not on current keyboard */
  870. if ((code = FindKeyCode(dpy, p->modifiers[i])) == 0)
  871. return;
  872. /* code is now the keycode for the modifier you want */
  873. {
  874. register int j = m->max_keypermod<<3;
  875. while ((--j >= 0) && (code != m->modifiermap[j]))
  876. ;
  877. if (j < 0)
  878. return;
  879. p->state |= (1<<(j/m->max_keypermod));
  880. }
  881. }
  882. p->state &= AllMods;
  883. }
  884. /************************************************************************
  885. *
  886. *
  887. * Compose handling routines - compose handlers 0,1,2
  888. *
  889. *
  890. ************************************************************************/
  891. #define NORMAL_KEY_STATE 0
  892. #define FIRST_COMPOSE_KEY_STATE 1
  893. #define SECOND_COMPOSE_KEY_STATE 2
  894. Private
  895. KeySym HexIMNormalKey(
  896. XicThaiPart *thai_part,
  897. KeySym symbol,
  898. XKeyEvent *event)
  899. {
  900. if (IsComposeKey (symbol, event)) /* start compose sequence */
  901. {
  902. SetLed (event->display,COMPOSE_LED, LedModeOn);
  903. thai_part->comp_state = FIRST_COMPOSE_KEY_STATE;
  904. return NoSymbol;
  905. }
  906. return symbol;
  907. }
  908. Private
  909. KeySym HexIMFirstComposeKey(
  910. XicThaiPart *thai_part,
  911. KeySym symbol,
  912. XKeyEvent *event)
  913. {
  914. if (IsModifierKey (symbol)) return symbol; /* ignore shift etc. */
  915. if (IsCancelComposeKey (&symbol, event)) /* cancel sequence */
  916. {
  917. SetLed (event->display,COMPOSE_LED, LedModeOff);
  918. thai_part->comp_state = NORMAL_KEY_STATE;
  919. return symbol;
  920. }
  921. if (IsComposeKey (symbol, event)) /* restart sequence ?? */
  922. {
  923. return NoSymbol; /* no state change necessary */
  924. }
  925. thai_part->keysym = symbol; /* save key pressed */
  926. thai_part->comp_state = SECOND_COMPOSE_KEY_STATE;
  927. return NoSymbol;
  928. }
  929. Private
  930. KeySym HexIMSecondComposeKey(
  931. XicThaiPart *thai_part,
  932. KeySym symbol,
  933. XKeyEvent *event)
  934. {
  935. if (IsModifierKey (symbol)) return symbol; /* ignore shift etc. */
  936. if (IsComposeKey (symbol, event)) /* restart sequence ? */
  937. {
  938. thai_part->comp_state =FIRST_COMPOSE_KEY_STATE;
  939. return NoSymbol;
  940. }
  941. SetLed (event->display,COMPOSE_LED, LedModeOff);
  942. if (IsCancelComposeKey (&symbol, event)) /* cancel sequence ? */
  943. {
  944. thai_part->comp_state = NORMAL_KEY_STATE;
  945. return symbol;
  946. }
  947. if ((symbol = HexIMComposeSequence (thai_part->keysym, symbol))
  948. ==NoSymbol)
  949. { /* invalid compose sequence */
  950. XBell(event->display, BellVolume);
  951. }
  952. thai_part->comp_state = NORMAL_KEY_STATE; /* reset to normal state */
  953. return symbol;
  954. }
  955. /*
  956. * Interprets two keysyms entered as hex digits and return the Thai keysym
  957. * correspond to the TACTIS code formed.
  958. * The current implementation of this routine returns ISO Latin Keysyms.
  959. */
  960. Private
  961. KeySym HexIMComposeSequence(KeySym ks1, KeySym ks2)
  962. {
  963. int hi_digit;
  964. int lo_digit;
  965. int tactis_code;
  966. if ((ks1 >= XK_0) && (ks1 <= XK_9))
  967. hi_digit = ks1 - XK_0;
  968. else if ((ks1 >= XK_A) && (ks1 <= XK_F))
  969. hi_digit = ks1 - XK_A + 10;
  970. else if ((ks1 >= XK_a) && (ks1 <= XK_f))
  971. hi_digit = ks1 - XK_a + 10;
  972. else /* out of range */
  973. return NoSymbol;
  974. if ((ks2 >= XK_0) && (ks2 <= XK_9))
  975. lo_digit = ks2 - XK_0;
  976. else if ((ks2 >= XK_A) && (ks2 <= XK_F))
  977. lo_digit = ks2 - XK_A + 10;
  978. else if ((ks2 >= XK_a) && (ks2 <= XK_f))
  979. lo_digit = ks2 - XK_a + 10;
  980. else /* out of range */
  981. return NoSymbol;
  982. tactis_code = hi_digit * 0x10 + lo_digit ;
  983. return (KeySym)tactis_code;
  984. }
  985. /*
  986. * routine determines
  987. * 1) whether key event should cancel a compose sequence
  988. * 2) whether cancelling key event should be processed or ignored
  989. */
  990. Private
  991. int IsCancelComposeKey(
  992. KeySym *symbol,
  993. XKeyEvent *event)
  994. {
  995. if (*symbol==XK_Delete && !IsControl(event->state) &&
  996. !IsMod1(event->state)) {
  997. *symbol=NoSymbol; /* cancel compose sequence, and ignore key */
  998. return True;
  999. }
  1000. if (IsComposeKey(*symbol, event)) return False;
  1001. return (
  1002. IsControl (event->state) ||
  1003. IsMod1(event->state) ||
  1004. IsKeypadKey (*symbol) ||
  1005. IsFunctionKey (*symbol) ||
  1006. IsMiscFunctionKey (*symbol) ||
  1007. #ifdef DXK_PRIVATE /* DEC private keysyms */
  1008. *symbol == DXK_Remove ||
  1009. #endif
  1010. IsPFKey (*symbol) ||
  1011. IsCursorKey (*symbol) ||
  1012. (*symbol >= XK_Tab && *symbol < XK_Multi_key)
  1013. ? True : False); /* cancel compose sequence and pass */
  1014. /* cancelling key through */
  1015. }
  1016. /*
  1017. * set specified keyboard LED on or off
  1018. */
  1019. Private
  1020. void SetLed(
  1021. Display *dpy,
  1022. int num,
  1023. int state)
  1024. {
  1025. XKeyboardControl led_control;
  1026. led_control.led_mode = state;
  1027. led_control.led = num;
  1028. XChangeKeyboardControl (dpy, KBLed | KBLedMode, &led_control);
  1029. }
  1030. #endif
  1031. /*
  1032. * Initialize ISC mode from im modifier
  1033. */
  1034. Private void InitIscMode(Xic ic)
  1035. {
  1036. Xim im;
  1037. char *im_modifier_name;
  1038. /* If already defined, just return */
  1039. if (IC_IscMode(ic)) return;
  1040. /* Get IM modifier */
  1041. im = (Xim) XIMOfIC((XIC)ic);
  1042. im_modifier_name = im->core.im_name;
  1043. /* Match with predefined value, default is Basic Check */
  1044. if (!strncmp(im_modifier_name,"BasicCheck",MAXTHAIIMMODLEN+1))
  1045. IC_IscMode(ic) = WTT_ISC1;
  1046. else if (!strncmp(im_modifier_name,"Strict",MAXTHAIIMMODLEN+1))
  1047. IC_IscMode(ic) = WTT_ISC2;
  1048. else if (!strncmp(im_modifier_name,"Thaicat",MAXTHAIIMMODLEN+1))
  1049. IC_IscMode(ic) = THAICAT_ISC;
  1050. else if (!strncmp(im_modifier_name,"Passthrough",MAXTHAIIMMODLEN+1))
  1051. IC_IscMode(ic) = NOISC;
  1052. else
  1053. IC_IscMode(ic) = WTT_ISC1;
  1054. return;
  1055. }
  1056. /*
  1057. * Helper functions for _XimThaiFilter()
  1058. */
  1059. Private Bool
  1060. ThaiFltAcceptInput(Xic ic, unsigned char new_char, KeySym symbol)
  1061. {
  1062. ic->private.local.composed->wc[0] = tis2ucs(new_char);
  1063. ic->private.local.composed->wc[1] = '\0';
  1064. if ((new_char <= 0x1f) || (new_char == 0x7f))
  1065. ic->private.local.composed->keysym = symbol;
  1066. else
  1067. ic->private.local.composed->keysym = NoSymbol;
  1068. return True;
  1069. }
  1070. Private Bool
  1071. ThaiFltReorderInput(Xic ic, unsigned char previous_char, unsigned char new_char)
  1072. {
  1073. if (!IC_DeletePreviousChar(ic)) return False;
  1074. ic->private.local.composed->wc[0] = tis2ucs(new_char);
  1075. ic->private.local.composed->wc[1] = tis2ucs(previous_char);
  1076. ic->private.local.composed->wc[2] = '\0';
  1077. ic->private.local.composed->keysym = NoSymbol;
  1078. return True;
  1079. }
  1080. Private Bool
  1081. ThaiFltReplaceInput(Xic ic, unsigned char new_char, KeySym symbol)
  1082. {
  1083. if (!IC_DeletePreviousChar(ic)) return False;
  1084. ic->private.local.composed->wc[0] = tis2ucs(new_char);
  1085. ic->private.local.composed->wc[1] = '\0';
  1086. if ((new_char <= 0x1f) || (new_char == 0x7f))
  1087. ic->private.local.composed->keysym = symbol;
  1088. else
  1089. ic->private.local.composed->keysym = NoSymbol;
  1090. return True;
  1091. }
  1092. /*
  1093. * Filter function for TACTIS
  1094. */
  1095. Bool
  1096. _XimThaiFilter(d, w, ev, client_data)
  1097. Display *d;
  1098. Window w;
  1099. XEvent *ev;
  1100. XPointer client_data;
  1101. {
  1102. Xic ic = (Xic)client_data;
  1103. KeySym symbol;
  1104. int isc_mode; /* Thai Input Sequence Check mode */
  1105. unsigned char previous_char; /* Last inputted Thai char */
  1106. unsigned char new_char;
  1107. #ifdef UNUSED
  1108. unsigned int modifiers;
  1109. KeySym lsym,usym;
  1110. int state;
  1111. XicThaiPart *thai_part;
  1112. char buf[10];
  1113. #endif
  1114. wchar_t wbuf[10];
  1115. Bool isReject;
  1116. if ((ev->type != KeyPress)
  1117. || (ev->xkey.keycode == 0))
  1118. return False;
  1119. if (!IC_IscMode(ic)) InitIscMode(ic);
  1120. XwcLookupString((XIC)ic, &ev->xkey, wbuf, sizeof(wbuf) / sizeof(wbuf[0]),
  1121. &symbol, NULL);
  1122. if ((ev->xkey.state & (AllMods & ~ShiftMask)) ||
  1123. ((symbol >> 8 == 0xFF) &&
  1124. ((XK_BackSpace <= symbol && symbol <= XK_Clear) ||
  1125. (symbol == XK_Return) ||
  1126. (symbol == XK_Pause) ||
  1127. (symbol == XK_Scroll_Lock) ||
  1128. (symbol == XK_Sys_Req) ||
  1129. (symbol == XK_Escape) ||
  1130. (symbol == XK_Delete) ||
  1131. IsCursorKey(symbol) ||
  1132. IsKeypadKey(symbol) ||
  1133. IsMiscFunctionKey(symbol) ||
  1134. IsFunctionKey(symbol))))
  1135. {
  1136. IC_ClearPreviousChar(ic);
  1137. return False;
  1138. }
  1139. if (((symbol >> 8 == 0xFF) &&
  1140. IsModifierKey(symbol)) ||
  1141. #ifdef XK_XKB_KEYS
  1142. ((symbol >> 8 == 0xFE) &&
  1143. (XK_ISO_Lock <= symbol && symbol <= XK_ISO_Last_Group_Lock)) ||
  1144. #endif
  1145. (symbol == NoSymbol))
  1146. {
  1147. return False;
  1148. }
  1149. #ifdef UNUSED
  1150. if (! XThaiTranslateKey(ev->xkey.display, ev->xkey.keycode, ev->xkey.state,
  1151. &modifiers, &symbol, &lsym, &usym))
  1152. return False;
  1153. /*
  1154. * Hex input method processing
  1155. */
  1156. thai_part = &ic->private.local.thai;
  1157. state = thai_part->comp_state;
  1158. if (state >= 0 && state < nstate_handlers) /* call handler for state */
  1159. {
  1160. symbol = (* state_handler[state])(thai_part, symbol, (XKeyEvent *)ev);
  1161. }
  1162. /*
  1163. * Translate KeySym into mb.
  1164. */
  1165. count = XThaiTranslateKeySym(ev->xkey.display, symbol, lsym,
  1166. usym, ev->xkey.state, buf, 10);
  1167. if (!symbol && !count)
  1168. return True;
  1169. /* Return symbol if cannot convert to character */
  1170. if (!count)
  1171. return False;
  1172. #endif
  1173. /*
  1174. * Thai Input sequence check
  1175. */
  1176. isc_mode = IC_IscMode(ic);
  1177. if (!(previous_char = IC_GetPreviousChar(ic))) previous_char = ' ';
  1178. new_char = ucs2tis(wbuf[0]);
  1179. isReject = True;
  1180. if (THAI_isaccepted(new_char, previous_char, isc_mode)) {
  1181. ThaiFltAcceptInput(ic, new_char, symbol);
  1182. isReject = False;
  1183. } else {
  1184. unsigned char context_char;
  1185. context_char = IC_GetContextChar(ic);
  1186. if (context_char) {
  1187. if (THAI_iscomposible(new_char, context_char)) {
  1188. if (THAI_iscomposible(previous_char, new_char)) {
  1189. isReject = !ThaiFltReorderInput(ic, previous_char, new_char);
  1190. } else if (THAI_iscomposible(previous_char, context_char)) {
  1191. isReject = !ThaiFltReplaceInput(ic, new_char, symbol);
  1192. } else if (THAI_chtype(previous_char) == FV1
  1193. && THAI_chtype(new_char) == TONE) {
  1194. isReject = !ThaiFltReorderInput(ic, previous_char, new_char);
  1195. }
  1196. } else if (THAI_isaccepted(new_char, context_char, isc_mode)) {
  1197. isReject = !ThaiFltReplaceInput(ic, new_char, symbol);
  1198. }
  1199. }
  1200. }
  1201. if (isReject) {
  1202. /* reject character */
  1203. XBell(ev->xkey.display, BellVolume);
  1204. return True;
  1205. }
  1206. _Xlcwcstombs(ic->core.im->core.lcd, ic->private.local.composed->mb,
  1207. ic->private.local.composed->wc, 10);
  1208. _Xlcmbstoutf8(ic->core.im->core.lcd, ic->private.local.composed->utf8,
  1209. ic->private.local.composed->mb, 10);
  1210. /* Remember the last character inputted
  1211. * (as fallback in case StringConversionCallback is not provided)
  1212. */
  1213. IC_SavePreviousChar(ic, new_char);
  1214. ev->xkey.keycode = 0;
  1215. XPutBackEvent(d, ev);
  1216. return True;
  1217. }