imTrans.c 7.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/* $Xorg: imTrans.c,v 1.3 2000/08/17 19:45:16 cpqbld Exp $ */
/******************************************************************

           Copyright 1992 by Sun Microsystems, Inc.
           Copyright 1992, 1993, 1994 by FUJITSU LIMITED

Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear
in supporting documentation, and that the name of Sun Microsystems, Inc.
and FUJITSU LIMITED not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
Sun Microsystems, Inc. and FUJITSU LIMITED makes no representations about
the suitability of this software for any purpose.
It is provided "as is" without express or implied warranty.

Sun Microsystems Inc. AND FUJITSU LIMITED DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS, IN NO EVENT SHALL Sun Microsystems, Inc. AND FUJITSU LIMITED
BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

  Author: Hideki Hiura (hhiura@Sun.COM) Sun Microsystems, Inc.
          Takashi Fujiwara     FUJITSU LIMITED 
                               fujiwara@a80.tech.yk.fujitsu.co.jp

******************************************************************/
/* $XFree86: xc/lib/X11/imTrans.c,v 1.3 2003/04/17 02:06:32 dawes Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <X11/Xatom.h>
#include <X11/Xmd.h>
#define NEED_EVENTS
#include "Xlibint.h"
#include <X11/Xtrans/Xtrans.h>
#include "Xlcint.h"
#include "Ximint.h"
#include "XimTrans.h"
#include "XimTrInt.h"

#ifdef WIN32
#include <X11/Xwindows.h>
#endif


#ifndef XIM_CONNECTION_RETRIES
#define XIM_CONNECTION_RETRIES 5
#endif


Private Bool
_XimTransConnect(
    Xim			 im)
{
    TransSpecRec	*spec = (TransSpecRec *)im->private.proto.spec;
    int			connect_stat, retry;
    Window		window;

    for (retry = XIM_CONNECTION_RETRIES; retry >= 0; retry--)
    {
	if ((spec->trans_conn = _XimXTransOpenCOTSClient (
	    spec->address)) == NULL)
	{
	    break;
	}

	if ((connect_stat = _XimXTransConnect (
	    spec->trans_conn, spec->address)) < 0)
	{
	    _XimXTransClose (spec->trans_conn);
	    spec->trans_conn = NULL;

	    if (connect_stat == TRANS_TRY_CONNECT_AGAIN)
	    {
		sleep(1);
		continue;
	    }
	    else
		break;
	}
	else
	    break;
    }

    if (spec->trans_conn == NULL)
	return False;

    spec->fd = _XimXTransGetConnectionNumber (spec->trans_conn);

    if (!(window = XCreateSimpleWindow(im->core.display,
		DefaultRootWindow(im->core.display), 0, 0, 1, 1, 1, 0, 0)))
	return False;
    spec->window = window;

    _XRegisterFilterByType(im->core.display, window, KeyPress, KeyPress,
				_XimTransFilterWaitEvent, (XPointer)im);

    return _XRegisterInternalConnection(im->core.display, spec->fd, 
			(_XInternalConnectionProc)_XimTransInternalConnection, 
			(XPointer)im);
}


Private Bool
_XimTransShutdown(
    Xim im)
{
    TransSpecRec *spec = (TransSpecRec *)im->private.proto.spec;

    _XimXTransDisconnect(spec->trans_conn);
    (void)_XimXTransClose(spec->trans_conn);
    _XimFreeTransIntrCallback(im);
    _XUnregisterInternalConnection(im->core.display, spec->fd);
    _XUnregisterFilter(im->core.display, spec->window,
				_XimTransFilterWaitEvent, (XPointer)im);
    XDestroyWindow(im->core.display, spec->window);
    Xfree(spec->address);
    Xfree(spec);
    return True;
}



Public Bool
_XimTransRegisterDispatcher(
    Xim				 im,
    Bool			 (*callback)(
					     Xim, INT16, XPointer, XPointer
					     ),
    XPointer			 call_data)
{
    TransSpecRec		*spec = (TransSpecRec *)im->private.proto.spec;
    TransIntrCallbackPtr	 rec;

    if (!(rec = (TransIntrCallbackPtr)Xmalloc(sizeof(TransIntrCallbackRec))))
        return False;

    rec->func       = callback;
    rec->call_data  = call_data;
    rec->next       = spec->intr_cb;
    spec->intr_cb   = rec;
    return True;
}


Public void
_XimFreeTransIntrCallback(
    Xim				 im)
{
    TransSpecRec		*spec = (TransSpecRec *)im->private.proto.spec;
    register TransIntrCallbackPtr	 rec, next;

    for (rec = spec->intr_cb; rec;) {
	next = rec->next;
	Xfree(rec);
	rec = next;
    }
    return;
}


Public Bool
_XimTransCallDispatcher(Xim im, INT16 len, XPointer data)
{
    TransSpecRec		*spec = (TransSpecRec *)im->private.proto.spec;
    TransIntrCallbackRec	*rec;

    for (rec = spec->intr_cb; rec; rec = rec->next) {
	if ((*rec->func)(im, len, data, rec->call_data))
	    return True;
    }
    return False;
}


Public Bool
_XimTransFilterWaitEvent(
    Display		*d,
    Window		 w,
    XEvent		*ev,
    XPointer		 arg)
{
    Xim			 im = (Xim)arg;
    TransSpecRec	*spec = (TransSpecRec *)im->private.proto.spec;

    spec->is_putback  = False;
    return _XimFilterWaitEvent(im);
}


Public void
_XimTransInternalConnection(
    Display		*d,
    int			 fd,
    XPointer		 arg)
{
    Xim			 im = (Xim)arg;
    XEvent		 ev;
    XKeyEvent		*kev;
    TransSpecRec	*spec = (TransSpecRec *)im->private.proto.spec;

    if (spec->is_putback)
	return;
    kev = (XKeyEvent *)&ev;
    kev->type = KeyPress;
    kev->send_event = False;
    kev->display = im->core.display;
    kev->window = spec->window;
    kev->keycode = 0;
    XPutBackEvent(im->core.display, &ev);
    XFlush(im->core.display);
    spec->is_putback = True;
    return;
}


Public Bool
_XimTransWrite(Xim im, INT16 len, XPointer data)
{
    TransSpecRec	*spec	= (TransSpecRec *)im->private.proto.spec;
    char		*buf = (char *)data;
    register int	 nbyte;

    while (len > 0) {
	if ((nbyte = _XimXTransWrite(spec->trans_conn, buf, len)) <= 0)
	    return False;
	len -= nbyte;
	buf += nbyte;
    }
    return True;
}


Public Bool
_XimTransRead(
    Xim			 im,
    XPointer		 recv_buf,
    int			 buf_len,
    int			*ret_len)
{
    TransSpecRec	*spec = (TransSpecRec *)im->private.proto.spec;
    int			 len;

    if (buf_len == 0) {
	*ret_len = 0;
	return True;
    }
    if ((len = _XimXTransRead(spec->trans_conn, recv_buf, buf_len)) <= 0)
	return False;
    *ret_len = len;
    return True;
}


Public void
_XimTransFlush(
    Xim		 im)
{
    return;
}



Public Bool
_XimTransConf(
    Xim		   	 im,
    char	 	*address)
{
    char		*paddr;
    TransSpecRec	*spec;

    if (!(paddr = (char *)Xmalloc(strlen(address) + 1)))
	return False;

    if (!(spec = (TransSpecRec *) Xmalloc(sizeof(TransSpecRec)))) {
	Xfree(paddr);
	return False;
    }

    bzero(spec, sizeof(TransSpecRec));

    (void)strcpy(paddr, address);
    spec->address   = paddr;

    im->private.proto.spec     = (XPointer)spec;
    im->private.proto.connect  = _XimTransConnect;
    im->private.proto.shutdown = _XimTransShutdown;
    im->private.proto.write    = _XimTransWrite;
    im->private.proto.read     = _XimTransRead;
    im->private.proto.flush    = _XimTransFlush;
    im->private.proto.register_dispatcher = _XimTransRegisterDispatcher;
    im->private.proto.call_dispatcher = _XimTransCallDispatcher;

    return True;
}