1 /*
2  * $Id: callback.c,v 1.2 2001/06/14 18:16:14 ura Exp $
3  */
4 
5 /*
6  * FreeWnn is a network-extensible Kana-to-Kanji conversion system.
7  * This file is part of FreeWnn.
8  *
9  * Copyright OMRON Corporation. 1987, 1988, 1989, 1990, 1991, 1992, 1999
10  * Copyright 1991, 1992 by Massachusetts Institute of Technology
11  *
12  * Author: OMRON SOFTWARE Co., Ltd. <freewnn@rd.kyoto.omronsoft.co.jp>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with GNU Emacs; see the file COPYING.  If not, write to the
26  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  * Commentary:
29  *
30  * Change log:
31  *
32  * Last modified date: 8,Feb.1999
33  *
34  * Code:
35  *
36  */
37 /*      Version 4.0
38  */
39 #ifdef  CALLBACKS
40 #include <stdio.h>
41 #include <string.h>
42 #include "commonhd.h"
43 #include "sdefine.h"
44 #include "xim.h"
45 #include "sheader.h"
46 #include "proto.h"
47 #include "ext.h"
48 
49 #if !defined(X11R5) && defined(BC_X11R5)
50 extern XIMCmblk *cur_cblk;
51 #define IS_XIMR5_CLIENT()       (cur_cblk->ximr5)
52 #endif /* !defined(X11R5) && defined(BC_X11R5) */
53 
54 static void
send_statusdraw(str,len)55 send_statusdraw (str, len)
56      wchar *str;
57      int len;
58 {
59   ximStatusDrawReply reply;
60   int send_len;
61 
62   while (1)
63     {
64       send_len = wchar_to_ct (cur_p->cur_xl->xlc, str, ct_buf, len, ct_buf_max);
65       if (send_len < -1)
66         {
67           return;
68         }
69       else if (send_len == -1)
70         {
71           if (realloc_ct_buf () < 0)
72             return;
73         }
74       else
75         {
76           break;
77         }
78     }
79   reply.type = XIM_ST_TEXT;
80   reply.encoding_is_wchar = False;
81   reply.length = send_len;
82   reply.feedback = 0;
83   if (need_byteswap () == True)
84     {
85       byteswap_StatusDrawReply (&reply);
86     }
87   if (_WriteToClient (&reply, sz_ximStatusDrawReply) == -1)
88     return;
89   if (send_len > 0)
90     {
91       if (_WriteToClient ((char *) ct_buf, send_len) == -1)
92         return;
93     }
94   if (_Send_Flush () == -1)
95     return;
96   return;
97 }
98 
99 static void
send_callbackpro(mode)100 send_callbackpro (mode)
101      int mode;
102 {
103   ximNormalReply reply;
104 
105   reply.state = 0;
106   reply.detail = mode;
107   if (need_byteswap () == True)
108     {
109       byteswap_NormalReply (&reply);
110     }
111   if (_WriteToClient (&reply, sz_ximNormalReply) == -1)
112     return;
113   if (_Send_Flush () == -1)
114     return;
115   return;
116 
117 }
118 
119 void
CBStatusDraw(w_buf,len)120 CBStatusDraw (w_buf, len)
121      wchar *w_buf;
122      int len;
123 {
124   if (len <= 0)
125     return;
126   if (cur_x->cb_st_start == 0)
127     return;
128 
129 #ifndef X11R5
130 #ifdef BC_X11R5
131   if (IS_XIMR5_CLIENT ())
132     {
133       return_eventreply (0, XIM_CALLBACK);
134       send_callbackpro (XIM_CB_ST_DRAW);
135       send_statusdraw (w_buf, len);
136       send_callbackpro (0);
137     }
138   else
139 #endif /* BC_X11R5 */
140     xim_send_statusdraw (w_buf, len);
141 #else
142   return_eventreply (0, XIM_CALLBACK);
143   send_callbackpro (XIM_CB_ST_DRAW);
144   send_statusdraw (w_buf, len);
145   send_callbackpro (0);
146 #endif /* !X11R5 */
147 
148 }
149 
150 void
CBStatusStart()151 CBStatusStart ()
152 {
153   if (cur_x->cb_st_start)
154     return;
155 
156 #ifndef X11R5
157 #ifdef BC_X11R5
158   if (IS_XIMR5_CLIENT ())
159     {
160       return_eventreply (0, XIM_CALLBACK);
161       send_callbackpro (XIM_CB_ST_START);
162       send_callbackpro (0);
163       if (_Send_Flush () == -1)
164         return;
165     }
166   else
167 #endif /* BC_X11R5 */
168     xim_send_statusstart ();
169 #else
170   return_eventreply (0, XIM_CALLBACK);
171   send_callbackpro (XIM_CB_ST_START);
172   send_callbackpro (0);
173   if (_Send_Flush () == -1)
174     return;
175 #endif /* !X11R5 */
176 
177   cur_x->cb_st_start = 1;
178 }
179 
180 void
CBStatusDone()181 CBStatusDone ()
182 {
183   if (cur_x->cb_st_start == 0)
184     return;
185 
186 #ifndef X11R5
187 #ifdef BC_X11R5
188   if (IS_XIMR5_CLIENT ())
189     {
190       return_eventreply (0, XIM_CALLBACK);
191       send_callbackpro (XIM_CB_ST_DONE);
192       send_callbackpro (0);
193       if (_Send_Flush () == -1)
194         return;
195     }
196   else
197 #endif /* BC_X11R5 */
198     xim_send_statusdone ();
199 #else
200   return_eventreply (0, XIM_CALLBACK);
201   send_callbackpro (XIM_CB_ST_DONE);
202   send_callbackpro (0);
203   if (_Send_Flush () == -1)
204     return;
205 #endif /* !X11R5 */
206   cur_x->cb_st_start = 0;
207 }
208 
209 static void
send_preeditdraw(caret,first,chg_len,str,len,flg)210 send_preeditdraw (caret, first, chg_len, str, len, flg)
211      int caret, first, chg_len;
212      wchar *str;
213      int len;
214      int flg;
215 {
216   ximPreDrawReply reply;
217   unsigned short fb = 0;
218   int send_len;
219 
220   if (str)
221     {
222       while (1)
223         {
224           send_len = wchar_to_ct (cur_p->cur_xl->xlc, str, ct_buf, len, ct_buf_max);
225           if (send_len < -1)
226             {
227               return;
228             }
229           else if (send_len == -1)
230             {
231               if (realloc_ct_buf () < 0)
232                 return;
233             }
234           else
235             {
236               break;
237             }
238         }
239     }
240   else
241     {
242       send_len = 0;
243     }
244   if (flg & REV_FLAG)
245     fb |= XIMReverse;
246   if (flg & UNDER_FLAG)
247     fb |= XIMUnderline;
248   if (flg & BOLD_FLAG)
249     fb |= XIMHighlight;
250   reply.caret = caret;
251   reply.chg_first = first;
252   reply.chg_length = chg_len;
253   reply.length = send_len;
254   reply.encoding_is_wchar = False;
255   reply.feedback = fb;
256   if (need_byteswap () == True)
257     {
258       byteswap_PreDrawReply (&reply);
259     }
260   if (_WriteToClient (&reply, sz_ximPreDrawReply) == -1)
261     return;
262   if (send_len > 0)
263     {
264       if (_WriteToClient ((char *) ct_buf, send_len) == -1)
265         return;
266     }
267   if (_Send_Flush () == -1)
268     return;
269   return;
270 }
271 
272 void
CBPreeditClear(x)273 CBPreeditClear (x)
274      int x;
275 {
276   XIMLangRec *xl;
277   int caret = 0, max_len = 0;
278   int left;
279 
280   xl = cur_x->cur_xl;
281 
282   if (cur_x->cb_pe_start == 0)
283     return;
284   caret = XwcGetChars (xl->buf, x, &left);
285   max_len = XwcGetChars (xl->buf, xl->max_pos, &left);
286   if (max_len <= caret)
287     return;
288 #ifndef X11R5
289 #ifdef BC_X11R5
290   if (IS_XIMR5_CLIENT ())
291     {
292       return_eventreply (0, XIM_CALLBACK);
293       send_callbackpro (XIM_CB_PRE_DRAW);
294       send_preeditdraw (caret, caret, max_len - caret, (wchar *) NULL, 0, 0);
295       send_callbackpro (0);
296       if (_Send_Flush () == -1)
297         return;
298     }
299   else
300 #endif /* BC_X11R5 */
301     xim_send_preeditdraw (caret, caret, max_len - caret, (wchar *) NULL, 0, 0);
302 #else
303   return_eventreply (0, XIM_CALLBACK);
304   send_callbackpro (XIM_CB_PRE_DRAW);
305   send_preeditdraw (caret, caret, max_len - caret, (wchar *) NULL, 0, 0);
306   send_callbackpro (0);
307   if (_Send_Flush () == -1)
308     return;
309 #endif /* !X11R5 */
310   return;
311 }
312 
313 void
CBPreeditDraw(wc,wc_len)314 CBPreeditDraw (wc, wc_len)
315      register wchar *wc;
316      int wc_len;
317 {
318   register int cnt = wc_len;
319   register XIMLangRec *xl;
320   register wchar *JW_buf, *p = wc;
321   register unsigned char *JW_att, flg = 0;
322   int caret, first, chg_len = 0, col = 0;
323   int mb_len, left, save_len = 0;
324   wchar tmp[80];
325 
326   if (cnt <= 0)
327     return;
328   xl = cur_x->cur_xl;
329 
330   visual_window ();
331   if (xl->currentcol >= cur_x->maxcolumns)
332     return;
333   JW_buf = xl->buf + xl->currentcol;
334   JW_att = xl->att + xl->currentcol;
335 
336   first = XwcGetChars (xl->buf, xl->currentcol, &left);
337 
338   if (xl->r_flag)
339     flg |= REV_FLAG;
340   if (xl->b_flag)
341     flg |= BOLD_FLAG;
342   if (xl->u_line_flag)
343     flg |= UNDER_FLAG;
344 
345   if ((save_len = xl->max_pos - xl->currentcol) > 0)
346     {
347       bcopy (JW_buf, tmp, (save_len * sizeof (wchar)));
348       tmp[save_len] = 0;
349     }
350   for (; cnt > 0; cnt--)
351     {
352       mb_len = put_pending_wchar_and_flg (*p, JW_buf, JW_att, flg);
353       col += mb_len;
354       JW_buf += mb_len;
355       JW_att += mb_len;
356     }
357   xl->currentcol += col;
358   if (save_len > 0)
359     {
360       chg_len = XwcGetChars (tmp, col, &left);
361       if (left > 0)
362         {
363           CBPreeditClear (xl->currentcol + col);
364         }
365     }
366   caret = XwcGetChars (xl->buf, xl->currentcol, &left);
367 
368 #ifndef X11R5
369 #ifdef BC_X11R5
370   if (IS_XIMR5_CLIENT ())
371     {
372       return_eventreply (0, XIM_CALLBACK);
373       send_callbackpro (XIM_CB_PRE_DRAW);
374       send_preeditdraw (caret, first, chg_len, wc, wc_len, flg);
375       send_callbackpro (0);
376       if (_Send_Flush () == -1)
377         return;
378     }
379   else
380 #endif /* BC_X11R5 */
381     xim_send_preeditdraw (caret, first, chg_len, wc, wc_len, flg);
382 #else
383   return_eventreply (0, XIM_CALLBACK);
384   send_callbackpro (XIM_CB_PRE_DRAW);
385   send_preeditdraw (caret, first, chg_len, wc, wc_len, flg);
386   send_callbackpro (0);
387   if (_Send_Flush () == -1)
388     return;
389 #endif /* !X11R5 */
390 }
391 
392 void
CBPreeditStart()393 CBPreeditStart ()
394 {
395   if (cur_x->cb_pe_start)
396     return;
397   cur_x->cur_xl->visible = 1;
398 
399 #ifndef X11R5
400 #ifdef BC_X11R5
401   if (IS_XIMR5_CLIENT ())
402     {
403       return_eventreply (0, XIM_CALLBACK);
404       send_callbackpro (XIM_CB_PRE_START);
405       send_callbackpro (0);
406       if (_Send_Flush () == -1)
407         return;
408     }
409   else
410 #endif /* BC_X11R5 */
411     xim_send_preeditstart ();
412 #else
413   return_eventreply (0, XIM_CALLBACK);
414   send_callbackpro (XIM_CB_PRE_START);
415   send_callbackpro (0);
416   if (_Send_Flush () == -1)
417     return;
418 #endif /* !X11R5 */
419   cur_x->cb_pe_start = 1;
420 }
421 
422 void
CBPreeditDone()423 CBPreeditDone ()
424 {
425   if (cur_x->cb_pe_start == 0)
426     return;
427   cur_x->cur_xl->visible = 0;
428 #ifndef X11R5
429 #ifdef BC_X11R5
430   if (IS_XIMR5_CLIENT ())
431     {
432       return_eventreply (0, XIM_CALLBACK);
433       send_callbackpro (XIM_CB_PRE_DONE);
434       send_callbackpro (0);
435       if (_Send_Flush () == -1)
436         return;
437     }
438   else
439 #endif /* BC_X11R5 */
440     xim_send_preeditdone ();
441 #else
442   return_eventreply (0, XIM_CALLBACK);
443   send_callbackpro (XIM_CB_PRE_DONE);
444   send_callbackpro (0);
445   if (_Send_Flush () == -1)
446     return;
447 #endif /* !X11R5 */
448   cur_x->cb_pe_start = 0;
449 }
450 
451 void
CBPreeditRedraw(xl)452 CBPreeditRedraw (xl)
453      register XIMLangRec *xl;
454 {
455   register wchar *JW_buf, *wc;
456   register unsigned char *JW_att, old_JW_att;
457   register int currentcol = 0;
458   int caret = 0, first = 0;
459   int wc_len = 0, mb_len = 0;
460   int left;
461 
462   if (xl->currentcol >= cur_x->maxcolumns || xl->max_pos <= 0)
463     return;
464   visual_window ();
465   CBPreeditClear (0);
466 
467   return_eventreply (0, XIM_CALLBACK);
468   send_callbackpro (XIM_CB_PRE_DRAW);
469 
470   xl = cur_p->cur_xl;
471 
472   JW_buf = xl->buf;
473   JW_att = xl->att;
474   old_JW_att = *JW_att;
475   wc = wc_buf;
476   skip_pending_wchar (wc, JW_buf);
477   wc_len = 0;
478   caret = 0;
479   first = 0;
480   currentcol = 0;
481   for (currentcol = 0; currentcol < xl->max_pos;)
482     {
483       JW_buf = xl->buf + currentcol;
484       JW_att = xl->att + currentcol;
485       if ((mb_len = get_columns_wchar (JW_buf)) > 0)
486         {
487           JW_buf += (mb_len - 1);
488           JW_att += (mb_len - 1);
489         }
490       if ((*JW_att != old_JW_att) && (wc_len > 0))
491         {
492           caret = XwcGetChars (xl->buf, currentcol, &left);
493           send_preeditdraw (caret, first, 0, wc, wc_len, old_JW_att);
494           old_JW_att = *JW_att;
495           wc += wc_len;
496           wc_len = 0;
497           first = caret;
498         }
499       wc_len++;
500       currentcol += mb_len;
501     }
502 
503   caret = XwcGetChars (xl->buf, currentcol, &left);
504 #ifndef X11R5
505 #ifdef BC_X11R5
506   if (IS_XIMR5_CLIENT ())
507     {
508       send_preeditdraw (caret, first, 0, wc, wc_len, old_JW_att);
509       send_callbackpro (0);
510       if (_Send_Flush () == -1)
511         return;
512     }
513   else
514 #endif /* BC_X11R5 */
515     xim_send_preeditdraw (caret, first, 0, wc, wc_len, old_JW_att);
516 #else
517   send_preeditdraw (caret, first, 0, wc, wc_len, old_JW_att);
518   send_callbackpro (0);
519   if (_Send_Flush () == -1)
520     return;
521 #endif /* !X11R5 */
522   return;
523 }
524 
525 static void
send_preeditcaret(flg,pos)526 send_preeditcaret (flg, pos)
527      int flg;
528      int pos;
529 {
530   ximPreCaretReply reply;
531 
532   if (flg)
533     {
534       reply.style = XIMIsPrimary;
535     }
536   else
537     {
538       reply.style = XIMIsInvisible;
539     }
540   reply.position = pos;
541   reply.direction = XIM_CB_DONT_CHANGE;
542   if (need_byteswap () == True)
543     {
544       byteswap_PreCaretReply (&reply);
545     }
546   if (_WriteToClient (&reply, sz_ximPreCaretReply) == -1)
547     return;
548   if (_Send_Flush () == -1)
549     return;
550   return;
551 }
552 
553 void
CBCursorMove(x)554 CBCursorMove (x)
555      int x;
556 {
557   XIMLangRec *xl;
558   int pos = 0;
559   int left;
560 
561   xl = cur_x->cur_xl;
562 
563   if (cur_x->cb_pe_start == 0)
564     return;
565 #ifndef X11R5
566   pos = XwcGetChars (xl->buf, x, &left);
567 #ifdef BC_X11R5
568   if (IS_XIMR5_CLIENT ())
569     {
570       return_eventreply (0, XIM_CALLBACK);
571       send_callbackpro (XIM_CB_PRE_CARET);
572       send_preeditcaret (xl->mark_flag, pos);
573       send_callbackpro (0);
574       if (_Send_Flush () == -1)
575         return;
576     }
577   else
578 #endif /* BC_X11R5 */
579     xim_send_preeditcaret (xl->mark_flag, pos);
580 #else
581   return_eventreply (0, XIM_CALLBACK);
582   send_callbackpro (XIM_CB_PRE_CARET);
583   pos = XwcGetChars (xl->buf, x, &left);
584   send_preeditcaret (xl->mark_flag, pos);
585   send_callbackpro (0);
586   if (_Send_Flush () == -1)
587     return;
588   return;
589 #endif /* !X11R5 */
590 }
591 
592 void
CBCursorMark(flg)593 CBCursorMark (flg)
594      int flg;
595 {
596   XIMLangRec *xl;
597   int pos = 0;
598   int left;
599 
600   xl = cur_x->cur_xl;
601 
602   if (cur_x->cb_pe_start == 0)
603     return;
604   if (xl->cursor_flag == 1 && xl->mark_flag != flg)
605     {
606       xl->mark_flag = flg;
607 #ifndef X11R5
608       pos = XwcGetChars (xl->buf, xl->currentcol, &left);
609 #ifdef BC_X11R5
610       if (IS_XIMR5_CLIENT ())
611         {
612           return_eventreply (0, XIM_CALLBACK);
613           send_callbackpro (XIM_CB_PRE_CARET);
614           send_preeditcaret (flg, pos);
615           send_callbackpro (0);
616           if (_Send_Flush () == -1)
617             return;
618         }
619       else
620 #endif /* BC_X11R5 */
621         xim_send_preeditcaret (flg, pos);
622 #else
623       return_eventreply (0, XIM_CALLBACK);
624       send_callbackpro (XIM_CB_PRE_CARET);
625       pos = XwcGetChars (xl->buf, xl->currentcol, &left);
626       send_preeditcaret (flg, pos);
627       send_callbackpro (0);
628       if (_Send_Flush () == -1)
629         return;
630       return;
631 #endif /* !X11R5 */
632     }
633 }
634 
635 void
SendCBRedraw()636 SendCBRedraw ()
637 {
638   XEvent ev;
639 
640   ev.type = KeyPress;
641   ev.xkey.window = cur_x->focus_window;
642   ev.xkey.root = cur_x->root_pointer->root_window;
643   ev.xkey.state = 0;
644   ev.xkey.keycode = cur_x->max_keycode + 2;
645   XSendEvent (dpy, ev.xkey.window, False, NoEventMask, &ev);
646   XFlush (dpy);
647 }
648 #endif /* CALLBACKS */
649