1 /*
2  * $Id: write.c,v 1.2 2001/06/14 18:16:18 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 #include <stdio.h>
40 #include "commonhd.h"
41 #include "sdefine.h"
42 #include "xim.h"
43 #include "sheader.h"
44 #include "proto.h"
45 #include "ext.h"
46 
47 #if !defined(X11R5) && defined(BC_X11R5)
48 extern XIMCmblk *cur_cblk;
49 #define IS_XIMR5_CLIENT()       (cur_cblk->ximr5)
50 #endif /* !defined(X11R5) && defined(BC_X11R5) */
51 
52 static int
send_returnend()53 send_returnend ()
54 {
55   ximReturnReply reply;
56 
57   reply.type = 0;
58   reply.length = 0;
59   reply.keysym = 0;
60   if (need_byteswap () == True)
61     {
62       byteswap_ReturnReply (&reply);
63     }
64   if (_WriteToClient (&reply, sz_ximReturnReply) == -1)
65     return (-1);
66   return (0);
67 }
68 
69 static int
send_ct(ct,ct_len)70 send_ct (ct, ct_len)
71      register char *ct;
72      register int ct_len;
73 {
74   ximReturnReply reply;
75 
76   reply.type = XIM_STRING;
77   reply.length = ct_len;
78   reply.keysym = 0;
79   if (need_byteswap () == True)
80     {
81       byteswap_ReturnReply (&reply);
82     }
83   if (_WriteToClient (&reply, sz_ximReturnReply) == -1)
84     return (-1);
85   if (ct_len > 0)
86     {
87       if (_WriteToClient (ct, ct_len) == -1)
88         return (-1);
89     }
90   return (0);
91 }
92 
93 static int
send_keysym(keysym,str)94 send_keysym (keysym, str)
95      KeySym keysym;
96      char str;
97 {
98   ximReturnReply reply;
99   char buf[2];
100   int tmp_length;
101 
102   reply.type = XIM_KEYSYM;
103   if (keysym < 0x20)
104     {
105       keysym |= 0xff00;
106       reply.length = 0;
107     }
108   else
109     {
110       reply.length = 1;
111       buf[0] = str & 0xff;
112       buf[1] = '\0';
113     }
114   reply.keysym = keysym;
115   tmp_length = reply.length;
116   if (need_byteswap () == True)
117     {
118       byteswap_ReturnReply (&reply);
119     }
120   if (_WriteToClient (&reply, sz_ximReturnReply) == -1)
121     return (-1);
122   if (tmp_length > 0)
123     {
124       if (_WriteToClient (buf, tmp_length) == -1)
125         return (-1);
126     }
127   return (0);
128 }
129 
130 int
return_eventreply(state,mode,num)131 return_eventreply (state, mode, num)
132      short state;
133      short mode;
134      short num;
135 {
136   ximEventReply reply;
137 
138   reply.state = state;
139   reply.detail = mode;
140   reply.number = num;
141   if (need_byteswap () == True)
142     {
143       byteswap_EventReply (&reply);
144     }
145   if (_WriteToClient (&reply, sz_ximEventReply) == -1)
146     return (-1);
147   return (0);
148 }
149 
150 int
send_nofilter()151 send_nofilter ()
152 {
153 #ifndef X11R5
154 #ifdef BC_X11R5
155   if (IS_XIMR5_CLIENT ())
156     {
157       if (return_eventreply (0, XIM_NOFILTER, 0) == -1)
158         return (-1);
159     }
160   else
161 #endif /* BC_X11R5 */
162     return xim_send_nofilter ();
163 #else
164   if (return_eventreply (0, XIM_NOFILTER, 0) == -1)
165     return (-1);
166 #endif /* !X11R5 */
167   return (0);
168 }
169 
170 static int
send_ch_locale(locale)171 send_ch_locale (locale)
172      char *locale;
173 {
174   return_eventreply (0, XIM_CH_LOCALE, strlen (locale));
175   if (_WriteToClient (locale, strlen (locale)) == -1)
176     return (-1);
177   return (0);
178 }
179 
180 int
send_end()181 send_end ()
182 {
183   if (cur_p && cur_p->ch_lc_flg)
184     {
185       send_ch_locale (cur_p->cur_xl->cur_lc->lc_name);
186       cur_p->ch_lc_flg = 0;
187     }
188   if (return_eventreply (0, 0, 0) == -1)
189     return (-1);
190   if (_Send_Flush () == -1)
191     return (-1);
192   return (0);
193 }
194 
195 int
return_cl_it()196 return_cl_it ()
197 {
198 #ifdef  XJPLIB
199   if (cur_x && cur_x->xjp)
200     {
201       XJp_return_cl_it ();
202       return (0);
203     }
204 #endif /* XJPLIB */
205   if (cur_input != 0)
206     {
207       if (send_nofilter () == -1)
208         return (-1);
209     }
210   return (0);
211 }
212 
213 void
xw_write(w_buf,size)214 xw_write (w_buf, size)
215      register w_char *w_buf;
216      register int size;
217 {
218   register XIMLangRec *xl;
219   w_char w_c;
220   w_char *start;
221   register int cnt;
222   int conv_len, send_len;
223   KeySym keysym;
224 
225   if (!size)
226     {
227       return;
228     }
229 #ifdef  XJPLIB
230   if (cur_x && cur_x->xjp)
231     {
232       XJp_xw_write (w_buf, size);
233       return;
234     }
235 #endif /* XJPLIB */
236 
237   if (henkan_off_flag)
238     {
239       return_cl_it ();
240       return;
241     }
242 #ifdef BC_X11R5
243   if (IS_XIMR5_CLIENT ())
244 #endif /* BC_X11R5 */
245 #if defined(X11R5) || defined(BC_X11R5)
246     return_eventreply (0, XIM_RETURN, 0);
247 #endif /* defined(X11R5) || defined(BC_X11R5) */
248 
249   xl = cur_p->cur_xl;
250 
251   if (IsPreeditPosition (cur_x) && cur_p->cur_xl->del_x >= cur_p->cur_xl->max_pos)
252     {
253       JWMline_clear1 ();
254     }
255 
256   conv_len = 0;
257   start = w_buf;
258   for (cnt = 0; cnt < size; cnt++, w_buf++)
259     {
260       if (!((w_c = *w_buf) & 0xff80))
261         {
262           if (conv_len)
263             {
264               while (1)
265                 {
266                   send_len = w_char_to_ct (xl->xlc, start, ct_buf, conv_len, ct_buf_max);
267                   if (send_len < -1)
268                     {
269                       break;
270                     }
271                   else if (send_len == -1)
272                     {
273                       if (realloc_ct_buf () < 0)
274                         break;
275                     }
276                   else if (send_len > 0)
277                     {
278 #ifndef X11R5
279 #ifdef BC_X11R5
280                       if (IS_XIMR5_CLIENT ())
281                         send_ct (ct_buf, send_len);
282                       else
283 #endif /* BC_X11R5 */
284                         xim_send_ct (ct_buf, send_len);
285 #else
286                       send_ct (ct_buf, send_len);
287 #endif /* !X11R5 */
288                       break;
289                     }
290                 }
291               conv_len = 0;
292             }
293           keysym = (int) w_c;
294 #ifndef X11R5
295 #ifdef BC_X11R5
296           if (IS_XIMR5_CLIENT ())
297             send_keysym (keysym, w_c);
298           else
299 #endif /* BC_X11R5 */
300             xim_send_keysym (keysym, w_c);
301 #else
302           send_keysym (keysym, w_c);
303 #endif /* !X11R5 */
304         }
305       else
306         {
307           if (conv_len == 0)
308             {
309               start = w_buf;
310             }
311           conv_len++;
312         }
313     }
314   if (conv_len)
315     {
316       while (1)
317         {
318           send_len = w_char_to_ct (xl->xlc, start, ct_buf, conv_len, ct_buf_max);
319           if (send_len < -1)
320             {
321               break;
322             }
323           else if (send_len == -1)
324             {
325               if (realloc_ct_buf () < 0)
326                 break;
327             }
328           else if (send_len > 0)
329             {
330 #ifndef X11R5
331 #ifdef BC_X11R5
332               if (IS_XIMR5_CLIENT ())
333                 send_ct (ct_buf, send_len);
334               else
335 #endif /* BC_X11R5 */
336                 xim_send_ct (ct_buf, send_len);
337 #else
338               send_ct (ct_buf, send_len);
339 #endif /* !X11R5 */
340               break;
341             }
342         }
343       conv_len = 0;
344     }
345 #ifdef BC_X11R5
346   if (IS_XIMR5_CLIENT ())
347 #endif /* BC_X11R5 */
348 #if defined(X11R5) || defined(BC_X11R5)
349     send_returnend ();
350 #endif /* defined(X11R5) || defined(BC_X11R5) */
351   return;
352 }
353