imTrans.c 8.14 KB
Newer Older
1
/*
2
 * Copyright 1992 Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
23 24 25 26 27 28 29 30
/******************************************************************

           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
31 32 33 34 35
in supporting documentation, and that the name of FUJITSU LIMITED
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.
FUJITSU LIMITED makes no representations about the suitability of
this software for any purpose.
36 37
It is provided "as is" without express or implied warranty.

38 39 40 41 42 43 44
FUJITSU LIMITED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
EVENT SHALL 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.
45 46

  Author: Hideki Hiura (hhiura@Sun.COM) Sun Microsystems, Inc.
47
          Takashi Fujiwara     FUJITSU LIMITED
48 49 50 51 52 53 54 55
                               fujiwara@a80.tech.yk.fujitsu.co.jp

******************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
56 57
#include <nx-X11/Xatom.h>
#include <nx-X11/Xmd.h>
58
#include "Xlibint.h"
59
#include <nx-X11/Xtrans/Xtrans.h>
60 61 62 63 64 65
#include "Xlcint.h"
#include "Ximint.h"
#include "XimTrans.h"
#include "XimTrInt.h"

#ifdef WIN32
66
#include <nx-X11/Xwindows.h>
67 68 69 70 71 72 73 74
#endif


#ifndef XIM_CONNECTION_RETRIES
#define XIM_CONNECTION_RETRIES 5
#endif


75
static Bool
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
_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)
		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);

119 120
    return _XRegisterInternalConnection(im->core.display, spec->fd,
			(_XInternalConnectionProc)_XimTransInternalConnection,
121 122 123 124
			(XPointer)im);
}


125
static Bool
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
_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;
}



145
Bool
146 147 148 149 150 151 152 153 154 155
_XimTransRegisterDispatcher(
    Xim				 im,
    Bool			 (*callback)(
					     Xim, INT16, XPointer, XPointer
					     ),
    XPointer			 call_data)
{
    TransSpecRec		*spec = (TransSpecRec *)im->private.proto.spec;
    TransIntrCallbackPtr	 rec;

156
    if (!(rec = Xmalloc(sizeof(TransIntrCallbackRec))))
157 158 159 160 161 162 163 164 165 166
        return False;

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


167
void
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
_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;
}


183
Bool
184 185 186 187 188 189 190 191 192 193 194 195 196
_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;
}


197
Bool
198 199 200 201 202 203 204 205 206 207 208 209 210 211
_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);
}


212
void
213 214 215 216 217 218 219 220 221 222 223 224
_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;
225 226

    bzero(&ev, sizeof(ev));	/* FIXME: other fields may be accessed, too. */
227 228 229 230 231 232
    kev = (XKeyEvent *)&ev;
    kev->type = KeyPress;
    kev->send_event = False;
    kev->display = im->core.display;
    kev->window = spec->window;
    kev->keycode = 0;
233 234 235 236 237 238
    kev->time = 0L;
    kev->serial = LastKnownRequestProcessed(im->core.display);
#if 0
    fprintf(stderr,"%s,%d: putback FIXED kev->time=0 kev->serial=%lu\n", __FILE__, __LINE__, kev->serial);
#endif

239 240 241 242 243 244 245
    XPutBackEvent(im->core.display, &ev);
    XFlush(im->core.display);
    spec->is_putback = True;
    return;
}


246
Bool
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
_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;
}


263
Bool
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
_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;
}


284
void
285 286 287 288 289 290 291 292
_XimTransFlush(
    Xim		 im)
{
    return;
}



293
Bool
294 295 296 297 298 299 300
_XimTransConf(
    Xim		   	 im,
    char	 	*address)
{
    char		*paddr;
    TransSpecRec	*spec;

301
    if (!(paddr = strdup(address)))
302 303
	return False;

304
    if (!(spec = Xcalloc(1, sizeof(TransSpecRec)))) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	Xfree(paddr);
	return False;
    }

    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;
}