1 #ifndef lint
2 static char *rcsid = "$Id: imconv.c,v 1.25 2002/01/24 09:07:19 ishisone Exp $";
3 #endif
4 /*
5  * Copyright (c) 1991, 1994  Software Research Associates, Inc.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation for any purpose and without fee is hereby granted, provided
9  * that the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of Software Research Associates not be
12  * used in advertising or publicity pertaining to distribution of the
13  * software without specific, written prior permission.  Software Research
14  * Associates makes no representations about the suitability of this software
15  * for any purpose.  It is provided "as is" without express or implied
16  * warranty.
17  *
18  * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
19  */
20 
21 #define COMMIT_SYNC
22 #define STATUS_SYNC
23 #include "im.h"
24 #include "ConvMgr.h"
25 #include "OverConv.h"
26 #include "OffConv.h"
27 #include "OnConv.h"
28 #include "InputConv.h"
29 
30 static void fillDefaultAttributesForStartup _Pt_((IMIC *icp));
31 static unsigned long makeConvAttributesForStartup _Pt_((IMIC *icp,
32 							ConversionAttributes *attrp));
33 static void commitString _Pt_((IMIC *icp, char *str, int len, int sync));
34 static void fixCallback _Pt_((Widget w, XtPointer client_data,
35 			      XtPointer call_data));
36 static void detachConverter _Pt_((IMIC *icp));
37 static void endCallback _Pt_((Widget w, XtPointer client_data,
38 			      XtPointer call_data));
39 static void unusedEventCallback _Pt_((Widget w, XtPointer client_data,
40 				      XtPointer call_data));
41 static void preeditStartCallback _Pt_((Widget w, XtPointer client_data,
42 				       XtPointer call_data));
43 static void preeditDoneCallback _Pt_((Widget w, XtPointer client_data,
44 				      XtPointer call_data));
45 static void preeditDrawCallback _Pt_((Widget w, XtPointer client_data,
46 				      XtPointer call_data));
47 static void preeditCaretCallback _Pt_((Widget w, XtPointer client_data,
48 				       XtPointer call_data));
49 static void statusStartCallback _Pt_((Widget w, XtPointer client_data,
50 				      XtPointer call_data));
51 static void statusDoneCallback _Pt_((Widget w, XtPointer client_data,
52 				     XtPointer call_data));
53 static void statusDrawCallback _Pt_((Widget w, XtPointer client_data,
54 				     XtPointer call_data));
55 static void preeditStart _Pt_((IMIC *icp));
56 static void preeditDone _Pt_((IMIC *icp));
57 static void preeditDraw _Pt_((IMIC *icp, OCCPreeditDrawArg *data));
58 static void preeditCaret _Pt_((IMIC *icp, int caret));
59 static void statusStart _Pt_((IMIC *icp));
60 static void statusDone _Pt_((IMIC *icp));
61 static void statusDraw _Pt_((IMIC *icp, OCCPreeditDrawArg *data));
62 static void setEventMask _Pt_((IMIC *icp, unsigned long forward_mask,
63 			       unsigned long synchronous_mask));
64 
65 
66 /*- fillDefaultAttributesForStartup: put default necessary for conv. start -*/
67 static void
fillDefaultAttributesForStartup(icp)68 fillDefaultAttributesForStartup(icp)
69 IMIC *icp;
70 {
71     unsigned long cmask, pmask, smask;
72 
73     cmask = ATTR_MASK_FOCUS | ATTR_MASK_PREEDIT_STATE | ATTR_MASK_RESET_STATE;
74 
75     switch (icp->style) {
76     case IMSTYLE_OVER_THE_SPOT:
77 	pmask = ATTR_MASK_FOREGROUND | ATTR_MASK_BACKGROUND |
78 	    ATTR_MASK_FONT_SET;
79 	smask = 0;
80 	break;
81     case IMSTYLE_OFF_THE_SPOT:
82 	pmask = ATTR_MASK_FOREGROUND | ATTR_MASK_BACKGROUND |
83 	    ATTR_MASK_FONT_SET | ATTR_MASK_AREA;
84 	smask = ATTR_MASK_AREA;
85 	break;
86     case IMSTYLE_ON_THE_SPOT:
87 	pmask = 0;
88 	smask = 0;
89 	break;
90     default:
91 	pmask = 0;
92 	smask = 0;
93     }
94     IMFillDefault(icp, cmask, pmask, smask);
95 }
96 
97 /*- makeConvAttributesForStartup: get conv. attrs needed for startup -*/
98 static unsigned long
makeConvAttributesForStartup(icp,attrp)99 makeConvAttributesForStartup(icp, attrp)
100 IMIC *icp;
101 ConversionAttributes *attrp;
102 {
103     icp->common_attr.change_mask = icp->common_attr.set_mask;
104     icp->preedit_attr.change_mask = icp->preedit_attr.set_mask;
105     icp->status_attr.change_mask = icp->status_attr.set_mask;
106     return IMMakeConvAttributes(icp, attrp);
107 }
108 
109 /*- commitString: commmit converted string to client -*/
110 static void
commitString(icp,str,len,sync)111 commitString(icp, str, len, sync)
112 IMIC *icp;
113 char *str;
114 int len;
115 int sync;
116 {
117     int offset;
118     IMConnection *conn = icp->im->connection;
119     unsigned int flag;
120 
121     TRACE(("imlib:commitString()\n"));
122 
123     if (DDEBUG_CONDITION(5)) {
124 	unsigned char *p = (unsigned char *)str;
125 	int i;
126 
127 	/*
128 	 * Dump commiting string.
129 	 */
130 	printf("* commit string:\n\t");
131 	for (i = 0; i < len; i++, p++) {
132 	    if (*p == '\033') {
133 		printf("ESC ");
134 	    } else if (*p < ' ') {
135 		printf("^%c ", *p + '@');
136 	    } else if (*p == ' ') {
137 		printf("sp ");
138 	    } else if (*p >= 0x7f) {
139 		printf("%x ", *p);
140 	    } else {
141 		printf("%c ", *p);
142 	    }
143 	}
144 	printf("\n");
145     }
146 
147     flag = XIM_FLAG_X_LOOKUP_CHARS;
148     if (sync) flag |= XIM_FLAG_SYNCHRONOUS;
149 
150     offset = IMPutHeader(conn, XIM_COMMIT, 0, 0);
151     IMPutC16(conn, icp->im->id);
152     IMPutC16(conn, icp->id);
153     IMPutC16(conn, flag);
154     IMPutC16(conn, (unsigned int)len);
155     IMPutString(conn, str, len);
156     IMFinishRequest(conn, offset);
157 }
158 
159 /*- fixCallback: fix callback -*/
160 /* ARGSUSED */
161 static void
fixCallback(w,client_data,call_data)162 fixCallback(w, client_data, call_data)
163 Widget w;
164 XtPointer client_data;
165 XtPointer call_data;
166 {
167     IMIC *icp = (IMIC *)client_data;
168     IMConnection *conn = icp->im->connection;
169     Widget proto = conn->proto_widget;
170     Atom ctext = IMCtextAtom(proto);
171     CCTextCallbackArg *arg = (CCTextCallbackArg *)call_data;
172 
173     TRACE(("imlib:fixCallback()\n"));
174 
175     /* check encoding and format */
176     if (arg->encoding != ctext || arg->format != 8) {
177 	/*
178 	 * since every conversion object must support COMPOUND_TEXT,
179 	 * it is a serious error.
180 	 */
181 	String params[2];
182 	Cardinal num_params;
183 	WidgetClass ioc = icp->im->converter->input_object_class;
184 
185 	params[0] = XtClass(proto)->core_class.class_name;
186 	params[1] = ioc->core_class.class_name;
187 	num_params = 2;
188 
189 	XtAppErrorMsg(XtWidgetToApplicationContext(proto),
190 		      "encodingError", "convertedString", "WidgetError",
191 		      "%s: encoding of the converted string is not COMPOUND_STRING. check inputObject %s",
192 		      params, &num_params);
193     }
194 
195     /*
196      * Send fixed string via XIM_COMMIT message.
197      * Since kinput2 uses full-synchronous mode,
198      * synchronous flag must be turned off.
199      */
200     commitString(icp, arg->text, arg->length, 0);
201 
202 #ifdef COMMIT_SYNC
203     /*
204      * Send XIM_SYNC_REPLY so that synchronize with clients here.
205      */
206     if (icp->state & IC_FORWARDING) {
207 	icp->state &= ~IC_FORWARDING;
208 	IMSendRequestWithIC(conn, XIM_SYNC_REPLY, 0, icp);
209     }
210 #endif /* COMMIT_SYNC */
211 }
212 
213 /*- detachConverter: detach conversion widget from specified IC -*/
214 static void
detachConverter(icp)215 detachConverter(icp)
216 IMIC *icp;
217 {
218     Widget conv;
219 
220     TRACE(("imlib:detachConverter()\n"));
221 
222     conv = icp->conversion;
223     XtRemoveCallback(conv, XtNtextCallback, fixCallback, (XtPointer)icp);
224     XtRemoveCallback(conv, XtNendCallback, endCallback, (XtPointer)icp);
225     XtRemoveCallback(conv, XtNunusedEventCallback, unusedEventCallback, (XtPointer)icp);
226     if (icp->style == IMSTYLE_ON_THE_SPOT) {
227 	XtRemoveCallback(conv, XtNpreeditStartCallback, preeditStartCallback,
228 			 (XtPointer)icp);
229 	XtRemoveCallback(conv, XtNpreeditDoneCallback, preeditDoneCallback,
230 			 (XtPointer)icp);
231 	XtRemoveCallback(conv, XtNpreeditDrawCallback, preeditDrawCallback,
232 			 (XtPointer)icp);
233 	XtRemoveCallback(conv, XtNpreeditCaretCallback, preeditCaretCallback,
234 			 (XtPointer)icp);
235 	XtRemoveCallback(conv, XtNstatusStartCallback, statusStartCallback,
236 			 (XtPointer)icp);
237 	XtRemoveCallback(conv, XtNstatusDoneCallback, statusDoneCallback,
238 			 (XtPointer)icp);
239 	XtRemoveCallback(conv, XtNstatusDrawCallback, statusDrawCallback,
240 			 (XtPointer)icp);
241     }
242 
243     CMReleaseConverter(XtParent(icp->im->connection->proto_widget), conv);
244     icp->conversion = NULL;
245 }
246 
247 /*- endCallback: conversion end callback -*/
248 /* ARGSUSED */
249 static void
endCallback(w,client_data,call_data)250 endCallback(w, client_data, call_data)
251 Widget w;
252 XtPointer client_data;
253 XtPointer call_data;
254 {
255     IMIC *icp = (IMIC *)client_data;
256 
257     TRACE(("imlib:endCallback()\n"));
258 
259     if (icp->state & IC_CONVERTING) {
260 	detachConverter(icp);
261 	icp->state &= ~IC_CONVERTING;
262     }
263 }
264 
265 /*- unusedEventCallback: unused key event callback -*/
266 /* ARGSUSED */
267 static void
unusedEventCallback(w,client_data,call_data)268 unusedEventCallback(w, client_data, call_data)
269 Widget w;
270 XtPointer client_data;
271 XtPointer call_data;
272 {
273     IMIC *icp = (IMIC *)client_data;
274     IMConnection *conn = icp->im->connection;
275     XKeyEvent *ev = (XKeyEvent *)call_data;
276     int offset;
277 
278     TRACE(("imlib:unusedEventCallback()\n"));
279 
280     if (icp->im->mask & XIM_EXT_FORWARD_KEYEVENT_MASK) {
281 	offset = IMPutHeader(conn, XIM_EXT_FORWARD_KEYEVENT, 0, 0);
282 	IMPutC16(conn, icp->im->id);
283 	IMPutC16(conn, icp->id);
284 	IMPutC16(conn, 0);
285 	IMPutC16(conn, (unsigned int)(ev->serial & 0xffff));
286 	IMPutC8(conn, ev->type);
287 	IMPutC8(conn, (int)ev->keycode);
288 	IMPutC16(conn, (unsigned int)ev->state);
289 	IMPutC32(conn, ev->time);
290 	IMPutC32(conn, ev->window);
291 	IMFinishRequest(conn, offset);
292     } else {
293 	offset = IMPutHeader(conn, XIM_FORWARD_EVENT, 0, 0);
294 	IMPutC16(conn, icp->im->id);
295 	IMPutC16(conn, icp->id);
296 	IMPutC16(conn, 0);	/* ?? */
297 	IMPutC16(conn, (unsigned int)((ev->serial >> 16) & 0xffff));
298 
299 	IMPutC8(conn, ev->type);
300 	IMPutC8(conn, (int)ev->keycode);
301 	IMPutC16(conn, (unsigned int)(ev->serial & 0xffff));
302 	IMPutC32(conn, ev->time);
303 	IMPutC32(conn, ev->root);
304 	IMPutC32(conn, ev->window);
305 	IMPutC32(conn, ev->subwindow);
306 	IMPutI16(conn, ev->x_root);
307 	IMPutI16(conn, ev->y_root);
308 	IMPutI16(conn, ev->x);
309 	IMPutI16(conn, ev->y);
310 	IMPutC16(conn, ev->state);
311 	IMPutC8(conn, ev->same_screen);
312 
313 	IMFinishRequest(conn, offset);
314     }
315 }
316 
317 /*- preeditStartCallback: preedit start -*/
318 /* ARGSUSED */
319 static void
preeditStartCallback(w,client_data,call_data)320 preeditStartCallback(w, client_data, call_data)
321 Widget w;
322 XtPointer client_data;
323 XtPointer call_data;
324 {
325     IMIC *icp = (IMIC *)client_data;
326 
327     TRACE(("preeditStartCallback(icp=0x%lx)\n", icp));
328 
329     if (!(icp->common_attr.input_style & XIMPreeditCallbacks))
330 	return;
331 
332     preeditStart(icp);
333 }
334 
335 /*- preeditDoneCallback: preedit done -*/
336 /* ARGSUSED */
337 static void
preeditDoneCallback(w,client_data,call_data)338 preeditDoneCallback(w, client_data, call_data)
339 Widget w;
340 XtPointer client_data;
341 XtPointer call_data;
342 {
343     IMIC *icp = (IMIC *)client_data;
344 
345     TRACE(("preeditDoneCallback(icp=0x%lx)\n", icp));
346 
347     if (!(icp->common_attr.input_style & XIMPreeditCallbacks))
348 	return;
349 
350     preeditDone(icp);
351 }
352 
353 /*- preeditDrawCallback: preedit draw -*/
354 /* ARGSUSED */
355 static void
preeditDrawCallback(w,client_data,call_data)356 preeditDrawCallback(w, client_data, call_data)
357 Widget w;
358 XtPointer client_data;
359 XtPointer call_data;
360 {
361     IMIC *icp = (IMIC *)client_data;
362     OCCPreeditDrawArg *arg = (OCCPreeditDrawArg *)call_data;
363     IMConnection *conn = icp->im->connection;
364     Widget proto = conn->proto_widget;
365     Atom ctext = IMCtextAtom(proto);
366 
367     TRACE(("preeditDrawCallback(icp=0x%lx, length=%d)\n",icp,arg->text_length));
368 
369     if (!(icp->common_attr.input_style & XIMPreeditCallbacks))
370 	return;
371 
372     /* check encoding and format */
373     if (arg->encoding != ctext || arg->format != 8) {
374 	/*
375 	 * since every conversion object must support COMPOUND_TEXT,
376 	 * it is a serious error.
377 	 */
378 	String params[2];
379 	Cardinal num_params;
380 	WidgetClass ioc = icp->im->converter->input_object_class;
381 
382 	params[0] = XtClass(proto)->core_class.class_name;
383 	params[1] = ioc->core_class.class_name;
384 	num_params = 2;
385 
386 	XtAppErrorMsg(XtWidgetToApplicationContext(proto),
387 		      "encodingError", "preeditString", "WidgetError",
388 		      "%s: encoding of the preedit string is not COMPOUND_STRING. check inputObject %s",
389 		      params, &num_params);
390     }
391 
392     preeditDraw(icp, arg);
393 }
394 
395 /*- preeditCaretCallback: preedit caret -*/
396 /* ARGSUSED */
397 static void
preeditCaretCallback(w,client_data,call_data)398 preeditCaretCallback(w, client_data, call_data)
399 Widget w;
400 XtPointer client_data;
401 XtPointer call_data;
402 {
403     IMIC *icp = (IMIC *)client_data;
404     int caret = (int)call_data;
405 
406     TRACE(("preeditCaretCallback(icp=0x%lx, caret=%d)\n", icp, caret));
407 
408     if (!(icp->common_attr.input_style & XIMPreeditCallbacks))
409 	return;
410 
411     preeditCaret(icp, caret);
412 }
413 
414 /*- statusStartCallback: status start -*/
415 /* ARGSUSED */
416 static void
statusStartCallback(w,client_data,call_data)417 statusStartCallback(w, client_data, call_data)
418 Widget w;
419 XtPointer client_data;
420 XtPointer call_data;
421 {
422     IMIC *icp = (IMIC *)client_data;
423 
424     TRACE(("statusStartCallback(icp=0x%lx)\n", icp));
425 
426     if (!(icp->common_attr.input_style & XIMStatusCallbacks))
427 	return;
428 
429     statusStart(icp);
430 }
431 
432 /*- statusDoneCallback: status done -*/
433 /* ARGSUSED */
434 static void
statusDoneCallback(w,client_data,call_data)435 statusDoneCallback(w, client_data, call_data)
436 Widget w;
437 XtPointer client_data;
438 XtPointer call_data;
439 {
440     IMIC *icp = (IMIC *)client_data;
441 
442     TRACE(("statusDoneCallback(icp=0x%lx)\n", icp));
443 
444     if (!(icp->common_attr.input_style & XIMStatusCallbacks))
445 	return;
446 
447     statusDone(icp);
448 }
449 
450 /*- statusDrawCallback: status draw -*/
451 /* ARGSUSED */
452 static void
statusDrawCallback(w,client_data,call_data)453 statusDrawCallback(w, client_data, call_data)
454 Widget w;
455 XtPointer client_data;
456 XtPointer call_data;
457 {
458     IMIC *icp = (IMIC *)client_data;
459     OCCPreeditDrawArg *arg = (OCCPreeditDrawArg *)call_data;
460     IMConnection *conn = icp->im->connection;
461     Widget proto = conn->proto_widget;
462     Atom ctext = IMCtextAtom(proto);
463 
464     TRACE(("statusDrawCallback(icp=0x%lx, length=%d)\n", icp,arg->text_length));
465 
466     if (!(icp->common_attr.input_style & XIMStatusCallbacks))
467 	return;
468 
469     /* check encoding and format */
470     if (arg->encoding != ctext || arg->format != 8) {
471 	/*
472 	 * since every conversion object must support COMPOUND_TEXT,
473 	 * it is a serious error.
474 	 */
475 	String params[2];
476 	Cardinal num_params;
477 	WidgetClass ioc = icp->im->converter->input_object_class;
478 
479 	params[0] = XtClass(proto)->core_class.class_name;
480 	params[1] = ioc->core_class.class_name;
481 	num_params = 2;
482 
483 	XtAppErrorMsg(XtWidgetToApplicationContext(proto),
484 		      "encodingError", "statusString", "WidgetError",
485 		      "%s: encoding of the status string is not COMPOUND_STRING. check inputObject %s",
486 		      params, &num_params);
487     }
488 
489     statusDraw(icp, arg);
490 }
491 
492 /*- preeditStart: do preedit start -*/
493 static void
preeditStart(icp)494 preeditStart(icp)
495 IMIC *icp;
496 {
497     if (!(icp->state & IC_IN_PREEDIT)) {
498 	int offset;
499 	IMConnection *conn = icp->im->connection;
500 
501 	TRACE(("imlib:preeditStart()\n"));
502 
503 	offset = IMPutHeader(conn, XIM_PREEDIT_START, 0, 0);
504 	IMPutC16(conn, icp->im->id);
505 	IMPutC16(conn, icp->id);
506 	IMFinishRequest(conn, offset);
507 	icp->state |= IC_IN_PREEDIT;
508     }
509 }
510 
511 /*- preeditDone: do preedit done -*/
512 static void
preeditDone(icp)513 preeditDone(icp)
514 IMIC *icp;
515 {
516     if (icp->state & IC_IN_PREEDIT) {
517 	int offset;
518 	IMConnection *conn = icp->im->connection;
519 
520 	TRACE(("imlib:preeditDone()\n"));
521 
522 	offset = IMPutHeader(conn, XIM_PREEDIT_DONE, 0, 0);
523 	IMPutC16(conn, icp->im->id);
524 	IMPutC16(conn, icp->id);
525 	IMFinishRequest(conn, offset);
526 	icp->state &= ~IC_IN_PREEDIT;
527     }
528 }
529 
530 /*- preeditDraw: do actual preedit draw -*/
531 static void
preeditDraw(icp,data)532 preeditDraw(icp, data)
533 IMIC *icp;
534 OCCPreeditDrawArg *data;
535 {
536     IMConnection *conn = icp->im->connection;
537     int offset;
538     unsigned int status;
539     XIMFeedback feedback;
540     int i;
541 
542     if (icp->state & IC_RESETTING) return;
543 
544     preeditStart(icp);
545 
546     TRACE(("imlib:preeditDraw()\n"));
547 
548     if (DDEBUG_CONDITION(5)) {
549 	unsigned char *p = (unsigned char *)data->text;
550 
551 	/*
552 	 * Dump preedit string.
553 	 */
554 	printf("* preedit string:\n\t");
555 	for (i = 0; i < data->text_length; i++, p++) {
556 	    if (*p == '\033') {
557 		printf("ESC ");
558 	    } else if (*p < ' ') {
559 		printf("^%c ", *p + '@');
560 	    } else if (*p == ' ') {
561 		printf("sp ");
562 	    } else if (*p >= 0x7f) {
563 		printf("%x ", *p);
564 	    } else {
565 		printf("%c ", *p);
566 	    }
567 	}
568 	printf("\n");
569     }
570 
571     offset = IMPutHeader(conn, XIM_PREEDIT_DRAW, 0, 0);
572     IMPutC16(conn, icp->im->id);
573     IMPutC16(conn, icp->id);
574     IMPutC32(conn, data->caret);
575     IMPutC32(conn, data->chg_first);
576     IMPutC32(conn, data->chg_length);
577     status = 0;
578     if (data->text_length == 0)  status |= 0x1; /* no string */
579     if (data->attrs_length == 0) status |= 0x2; /* no feedback */
580     IMPutC32(conn, status);
581     IMPutC16(conn, (unsigned int)data->text_length);
582     if (data->text_length > 0) {
583 	IMPutString(conn, data->text, data->text_length);
584     }
585     IMPutPad(conn);
586     IMPutC16(conn, (unsigned int)(data->attrs_length * 4));
587     IMPutC16(conn, 0L); /* unused */
588     if (data->attrs_length > 0) {
589 	for (i = 0; i < data->attrs_length; i++) {
590 	    IMPutC32(conn, data->attrs[i]);
591 	}
592     }
593     IMFinishRequest(conn, offset);
594 #ifdef STATUS_SYNC
595     IMFlush(conn);
596 #endif /* STATUS_SYNC */
597 }
598 
599 /*- preeditCaret: do actual preedit caret -*/
600 static void
preeditCaret(icp,caret)601 preeditCaret(icp, caret)
602 IMIC *icp;
603 int caret;
604 {
605     IMConnection *conn = icp->im->connection;
606     int offset;
607 
608     if (icp->state & IC_RESETTING) return;
609 
610     preeditStart(icp);
611 
612     TRACE(("imlib:preeditCaret()\n"));
613 
614     offset = IMPutHeader(conn, XIM_PREEDIT_CARET, 0, 0);
615     IMPutC16(conn, icp->im->id);
616     IMPutC16(conn, icp->id);
617     IMPutC32(conn, caret);
618     IMPutC32(conn, (long)XIMAbsolutePosition);
619     IMPutC32(conn, (long)XIMPrimary);
620     IMFinishRequest(conn, offset);
621 }
622 
623 /*- statusStart: do status start -*/
624 static void
statusStart(icp)625 statusStart(icp)
626 IMIC *icp;
627 {
628     if (!(icp->state & IC_IN_STATUS)) {
629 	int offset;
630 	IMConnection *conn = icp->im->connection;
631 
632 	TRACE(("imlib:statusStart()\n"));
633 
634 	offset = IMPutHeader(conn, XIM_STATUS_START, 0, 0);
635 	IMPutC16(conn, icp->im->id);
636 	IMPutC16(conn, icp->id);
637 	IMFinishRequest(conn, offset);
638 	icp->state |= IC_IN_STATUS;
639 #ifdef STATUS_SYNC
640         IMFlush(conn);
641 #endif /* STATUS_SYNC */
642     }
643 }
644 
645 /*- statusDone: do status done -*/
646 static void
statusDone(icp)647 statusDone(icp)
648 IMIC *icp;
649 {
650     if (icp->state & IC_IN_STATUS) {
651 	int offset;
652 	IMConnection *conn = icp->im->connection;
653 
654 	TRACE(("imlib:statusDone()\n"));
655 
656 	offset = IMPutHeader(conn, XIM_STATUS_DONE, 0, 0);
657 	IMPutC16(conn, icp->im->id);
658 	IMPutC16(conn, icp->id);
659 	IMFinishRequest(conn, offset);
660 	icp->state &= ~IC_IN_STATUS;
661 #ifdef STATUS_SYNC
662         IMFlush(conn);
663 #endif /* STATUS_SYNC */
664     }
665 }
666 
667 /*- statusDraw: do actual status draw -*/
668 static void
statusDraw(icp,data)669 statusDraw(icp, data)
670 IMIC *icp;
671 OCCPreeditDrawArg *data;
672 {
673     IMConnection *conn = icp->im->connection;
674     int offset;
675     unsigned int status;
676 
677     if (icp->state & IC_RESETTING) return;
678 
679     statusStart(icp);
680 
681     TRACE(("imlib:statusDraw()\n"));
682 
683     offset = IMPutHeader(conn, XIM_STATUS_DRAW, 0, 0);
684     IMPutC16(conn, icp->im->id);
685     IMPutC16(conn, icp->id);
686     IMPutC32(conn, 0L); /* text type */
687     status = 0;
688     if (data->text_length == 0)  status |= 0x1; /* no string */
689     if (data->attrs_length == 0) status |= 0x2; /* no feedback */
690     IMPutC32(conn, status);
691     IMPutC16(conn, (unsigned int)data->text_length);
692     if (data->text_length > 0) {
693 	IMPutString(conn, data->text, data->text_length);
694     }
695     IMPutPad(conn);
696     IMPutC16(conn, (unsigned int)(data->attrs_length * 32));
697     IMPutC16(conn, 0L); /* unused */
698     if (data->attrs_length > 0) {
699 	int i;
700 	for (i = 0; i < data->attrs_length; i++) {
701 	    IMPutC32(conn, 0L);
702 	}
703     }
704     IMFinishRequest(conn, offset);
705 #ifdef STATUS_SYNC
706     IMFlush(conn);
707 #endif /* STATUS_SYNC */
708 }
709 
710 /*- setEventMask: put XIM_SET_EVENT_MASK request on the output stream -*/
711 static void
setEventMask(icp,forward_mask,synchronous_mask)712 setEventMask(icp, forward_mask, synchronous_mask)
713 IMIC *icp;
714 unsigned long forward_mask;
715 unsigned long synchronous_mask;
716 {
717     IMConnection *conn = icp->im->connection;
718 
719     (void)IMPutHeader(conn, XIM_SET_EVENT_MASK, 0, 12);
720     IMPutC16(conn, icp->im->id);
721     IMPutC16(conn, icp->id);
722     IMPutC32(conn, forward_mask);
723     IMPutC32(conn, synchronous_mask);
724     IMSchedule(conn, SCHED_WRITE);
725 }
726 
727 
728 /*
729  * Public functions
730  */
731 
732 int
IMStartConversion(icp)733 IMStartConversion(icp)
734 IMIC *icp;
735 {
736     IMIM *imp = icp->im;
737     Widget proto = imp->connection->proto_widget;
738     Widget converter;
739     WidgetClass class;
740     unsigned long attrmask;
741     ConversionAttributes attrs;
742 
743     TRACE(("IMStartConversion()\n"));
744 
745     if (icp->state & IC_CONVERTING) return 0;
746 
747     /*
748      * Check required attributes i.e. client window.
749      */
750     if (!(icp->common_attr.set_mask & ATTR_MASK_CLIENT)) {
751 	IMSendError(icp->im->connection, IMBadSomething, icp->im->id, icp->id,
752 		    "client window required");
753 	return -1;
754     }
755 
756     /*
757      * Fill in default values for unspecified attributes.
758      */
759     fillDefaultAttributesForStartup(icp);
760 
761     /*
762      * Get appropriate converter class.
763      */
764     if (icp->style == IMSTYLE_OVER_THE_SPOT) {
765 	class = overTheSpotConversionWidgetClass;
766     } else if (icp->style == IMSTYLE_OFF_THE_SPOT) {
767 	class = offTheSpotConversionWidgetClass;
768     } else if (icp->style == IMSTYLE_ON_THE_SPOT) {
769 	class = onTheSpotConversionWidgetClass;
770     } else {
771 	class = separateConversionWidgetClass;
772     }
773 
774     /*
775      * Compute conversion attributes to be passed to the converter.
776      */
777     attrmask = makeConvAttributesForStartup(icp, &attrs);
778 
779     icp->state &= ~IC_RESETTING;
780 
781     /*
782      * Attach converter to this IC.
783      */
784     converter = CMGetConverter(XtParent(proto),
785 			       icp->common_attr.client, class,
786 			       imp->converter->input_object_class,
787 			       imp->converter->display_object_class);
788     if (converter == NULL) {
789 	IMSendError(imp->connection, IMBadSomething, imp->id, icp->id,
790 		    "can't attach converter to this IC");
791 	return -1;
792     }
793     icp->conversion = converter;
794 
795     /*
796      * Add callback functions.
797      */
798     XtAddCallback(converter, XtNtextCallback, fixCallback, (XtPointer)icp);
799     XtAddCallback(converter, XtNendCallback, endCallback, (XtPointer)icp);
800     XtAddCallback(converter, XtNunusedEventCallback, unusedEventCallback, (XtPointer)icp);
801     if (icp->style == IMSTYLE_ON_THE_SPOT) {
802 	XtAddCallback(converter, XtNpreeditStartCallback, preeditStartCallback,
803 		      (XtPointer)icp);
804 	XtAddCallback(converter, XtNpreeditDoneCallback, preeditDoneCallback,
805 		      (XtPointer)icp);
806 	XtAddCallback(converter, XtNpreeditDrawCallback, preeditDrawCallback,
807 		      (XtPointer)icp);
808 	XtAddCallback(converter, XtNpreeditCaretCallback, preeditCaretCallback,
809 		      (XtPointer)icp);
810 	XtAddCallback(converter, XtNstatusStartCallback, statusStartCallback,
811 		      (XtPointer)icp);
812 	XtAddCallback(converter, XtNstatusDoneCallback, statusDoneCallback,
813 		      (XtPointer)icp);
814 	XtAddCallback(converter, XtNstatusDrawCallback, statusDrawCallback,
815 		      (XtPointer)icp);
816     }
817 
818     /*
819      * Start conversion
820      */
821     /* !!! if front-end method is used, ESMethodSelectFocus should be used */
822     XtVaSetValues(converter, XtNeventSelectMethod, ESMethodNone, NULL);
823     CControlStartConversion(converter, icp->common_attr.client,
824 			    attrmask, &attrs);
825 
826     icp->state |= IC_CONVERTING;
827 
828     if (icp->common_attr.input_style & XIMPreeditCallbacks)
829 	preeditStart(icp);
830 
831     /*
832      * Send XIM_SET_EVENT_MASK to let the client forward the key events.
833      */
834     IMStartForwarding(icp);
835 
836     return 0;
837 }
838 
839 void
IMStopConversion(icp)840 IMStopConversion(icp)
841 IMIC *icp;
842 {
843     TRACE(("IMStopConversion()\n"));
844 
845     if (!(icp->state & IC_CONVERTING)) return;
846 
847     /*
848      * Terminate conversion.
849      */
850     CControlEndConversion(icp->conversion);
851 
852     if (icp->common_attr.input_style & XIMPreeditCallbacks)
853 	preeditDone(icp);
854     IMStatusDone(icp);
855 
856     /*
857      * Detach converter.
858      */
859     detachConverter(icp);
860 
861     /*
862      * Stop forwarding key events unless this IC is being destroyed.
863      */
864     if (!(icp->state & IC_DESTROYING)) {
865 	IMStopForwarding(icp);
866     }
867 
868     icp->state &= ~IC_CONVERTING;
869 }
870 
871 int
IMResetIC(icp,preedit_strp)872 IMResetIC(icp, preedit_strp)
873 IMIC *icp;
874 char **preedit_strp;
875 {
876     int num_bytes = 0;
877 
878     TRACE(("IMResetIC()\n"));
879 
880     *preedit_strp = NULL;
881 
882     if (icp->state & IC_CONVERTING) {
883 	/*
884 	 * get input object by asking conversion widget of XtNinputObject
885 	 * resource. however, it is not recommended since protocol widget
886 	 * should interact with input object only through conversion
887 	 * widget.
888 	 */
889 	CCTextCallbackArg arg;
890 	Widget input_obj;
891 	Widget w = icp->im->connection->proto_widget;
892 
893 	XtVaGetValues(icp->conversion, XtNinputObject, &input_obj, NULL);
894 	arg.encoding = IMCtextAtom(w);
895 #ifdef notdef
896 	if (ICGetConvertedString(input_obj, &arg.encoding, &arg.format,
897 				 &arg.length, &arg.text) >= 0) {
898 	    num_bytes = arg.length;
899 	    *preedit_strp = (char *)arg.text;
900 	}
901 #else
902 	/*
903 	 * Canna seems to have some problem with ICGetConvertedString().
904 	 * Use ICGetPreeditString instead.
905 	 */
906 	if (ICGetPreeditString(input_obj, 0, 0, &arg.encoding, &arg.format,
907 			       &arg.length, &arg.text) >= 0) {
908 	    num_bytes = arg.length;
909 	    *preedit_strp = (char *)arg.text;
910 	}
911 #endif
912 	ICClearConversion(input_obj);
913 	TRACE(("\twas converting. %d bytes left\n", num_bytes));
914 
915 	if (icp->common_attr.reset_state == XIMInitialState) {
916 	    /* Force to end the conversion. */
917 	    TRACE(("\tback to the initial state\n"));
918 	    IMStopConversion(icp);
919 	}
920     }
921     return num_bytes;
922 }
923 
924 void
IMForwardEvent(icp,ev)925 IMForwardEvent(icp, ev)
926 IMIC *icp;
927 XEvent *ev;
928 {
929     TRACE(("IMForwardEvent()\n"));
930 
931     if (icp->conversion == NULL) return;
932     XtCallActionProc(icp->conversion, "to-inputobj", ev,
933 		     (String *)NULL, (Cardinal)0);
934 }
935 
936 /* ARGSUSED */
937 void
IMSetFocus(icp)938 IMSetFocus(icp)
939 IMIC *icp;
940 {
941     TRACE(("IMSetFocus(ic%d)\n", icp->id));
942     if (icp->conversion != NULL) {
943 	CControlChangeFocus(icp->conversion, 1);
944     }
945 }
946 
947 /* ARGSUSED */
948 void
IMUnsetFocus(icp)949 IMUnsetFocus(icp)
950 IMIC *icp;
951 {
952     TRACE(("IMUnsetFocus(ic%d)\n", icp->id));
953     if (icp->conversion != NULL) {
954 	CControlChangeFocus(icp->conversion, 0);
955     }
956 }
957 
958 /* ARGSUSED */
959 void
IMStatusStart(icp)960 IMStatusStart(icp)
961 IMIC *icp;
962 {
963     TRACE(("IMStatusStart(ic%d)\n", icp->id));
964     if (!(icp->common_attr.input_style & XIMStatusCallbacks))
965 	return;
966     statusStart(icp);
967 }
968 
969 /* ARGSUSED */
970 void
IMStatusDone(icp)971 IMStatusDone(icp)
972 IMIC *icp;
973 {
974     TRACE(("IMStatusDone(ic%d)\n", icp->id));
975     if (!(icp->common_attr.input_style & XIMStatusCallbacks))
976 	return;
977     statusDone(icp);
978 }
979 
980 void
IMStartForwarding(icp)981 IMStartForwarding(icp)
982 IMIC *icp;
983 {
984     /*
985      * Make the client forward key events to us.
986      */
987     TRACE(("IMStartForwarding(ic%d)\n", icp->id));
988 
989 #define FORWARD_MASK (KeyPressMask|KeyReleaseMask)
990 
991 #ifdef notdef
992     if (synchronous) {
993 	setEventMask(icp, FORWARD_MASK, FORWARD_MASK);
994     } else {
995 	setEventMask(icp, FORWARD_MASK, NoEventMask);
996     }
997 #else
998     /* using full-synchronous method */
999     setEventMask(icp, FORWARD_MASK, FORWARD_MASK);
1000 #endif
1001 
1002 #undef FORWARD_MASK
1003 }
1004 
1005 void
IMStopForwarding(icp)1006 IMStopForwarding(icp)
1007 IMIC *icp;
1008 {
1009     /*
1010      * Make the client stop sending key events.
1011      */
1012     TRACE(("IMStopForwarding(ic%d)\n", icp->id));
1013     setEventMask(icp, NoEventMask, NoEventMask);
1014 }
1015