imDefFlt.c 9.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/******************************************************************

           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 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
13
this software for any purpose.
14 15 16 17 18 19 20 21 22 23
It is provided "as is" without express or implied warranty.

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.

24
  Author: Takashi Fujiwara     FUJITSU LIMITED
25 26 27 28 29 30 31
                               fujiwara@a80.tech.yk.fujitsu.co.jp

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

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
32
#include <nx-X11/Xatom.h>
33 34 35 36 37
#include "Xlibint.h"
#include "Xutil.h"
#include "Xlcint.h"
#include "Ximint.h"

38
static long
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
_XimTriggerCheck(
    Xim			 im,
    XKeyEvent		*ev,
    INT32		 len,
    CARD32		*keylist)
{
    register long	 i;
    KeySym	 	 keysym;
    CARD32	 	 buf32[BUFSIZE/4];
    char	 	*buf = (char *)buf32;
    int			 modifier;
    int			 modifier_mask;
    CARD32		 min_len = sizeof(CARD32)   /* sizeof keysym */
				 + sizeof(CARD32)   /* sizeof modifier */
				 + sizeof(CARD32);  /* sizeof modifier mask */

    XLookupString(ev, buf, BUFSIZE, &keysym, NULL);
    if (!keysym)
	return -1;

    for (i = 0; len >= min_len; i += 3, len -= min_len) {
	modifier      = keylist[i + 1];
	modifier_mask = keylist[i + 2];
	if (((KeySym)keylist[i] == keysym)
	 && ((ev->state & modifier_mask) == modifier))
	    return i;
    }
    return -1;
}

69
static long
70 71 72 73 74 75 76 77
_XimTriggerOnCheck(
    Xim		 im,
    XKeyEvent	*ev)
{
    return _XimTriggerCheck(im, ev, (INT32)im->private.proto.im_onkeylist[0],
					&im->private.proto.im_onkeylist[1]);
}

78
static long
79 80 81 82 83 84 85 86
_XimTriggerOffCheck(
    Xim		 im,
    XKeyEvent	*ev)
{
    return _XimTriggerCheck(im, ev, (INT32)im->private.proto.im_offkeylist[0],
					&im->private.proto.im_offkeylist[1]);
}

87
static Bool
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
_XimOnKeysCheck(
    Xic		 ic,
    XKeyEvent	*ev)
{
    Xim		 im = (Xim)ic->core.im;
    long	 idx;

    if (IS_DYNAMIC_EVENT_FLOW(ic->core.im) &&
	    im->private.proto.im_onkeylist &&
	    im->private.proto.im_onkeylist[0]) {
	if ((idx = _XimTriggerOnCheck(im, ev)) >= 0) {
	    (void)_XimTriggerNotify(im, ic, 0, (CARD32)idx); /* Trigger on */
	    return True;
	}
    }
    return False;
}

106
static Bool
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
_XimOffKeysCheck(
    Xic		 ic,
    XKeyEvent	*ev)
{
    Xim		 im = (Xim)ic->core.im;
    long	 idx;

    if (IS_DYNAMIC_EVENT_FLOW(ic->core.im) &&
	    im->private.proto.im_offkeylist &&
	    im->private.proto.im_offkeylist[0]) {
	if ((idx = _XimTriggerOffCheck(im, ev)) >= 0) {
	    _XimTriggerNotify(im, ic, 1, (CARD32)idx); /* Trigger off */
	    return True;
	}
    }
    return False;
}

125
static void
126 127 128 129 130
_XimPendingFilter(
    Xic	 	 ic)
{
    Xim          im = (Xim)ic->core.im;

131
    if (IS_NEED_SYNC_REPLY(im)) {
132
	(void)_XimProcSyncReply(im, ic);
133
	UNMARK_NEED_SYNC_REPLY(im);
134 135 136 137
    }
    return;
}

138
static Bool
139 140 141 142 143 144
_XimProtoKeypressFilter(
    Xic		 ic,
    XKeyEvent	*ev)
{
    Xim		im = (Xim)ic->core.im;

145
    if (IS_FABRICATED(im)) {
146
	_XimPendingFilter(ic);
147
	UNMARK_FABRICATED(im);
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
	return NOTFILTERD;
    }

    if (IS_NEGLECT_EVENT(ic, KeyPressMask))
	return FILTERD;

#ifdef XIM_CONNECTABLE
    if (!IS_IC_CONNECTED(ic)) {
	if (IS_CONNECTABLE(im)) {
	    if (_XimConnectServer(im)) {
		if (!_XimReCreateIC(ic)) {
		    _XimDelayModeSetAttr(im);
		    return NOTFILTERD;
		}
	    } else {
		return NOTFILTERD;
	    }
	} else {
	    return NOTFILTERD;
	}
    }
#else
    if (!IS_IC_CONNECTED(ic))
	return NOTFILTERD;
#endif /* XIM_CONNECTABLE */

    if (!IS_FORWARD_EVENT(ic, KeyPressMask)) {
	if (_XimOnKeysCheck(ic, ev))
	    return FILTERD;
	return NOTFILTERD;
    }
    if (_XimOffKeysCheck(ic, ev))
	return FILTERD;

    if (_XimForwardEvent(ic, (XEvent *)ev,
				IS_SYNCHRONOUS_EVENT(ic, KeyPressMask)))
	return FILTERD;

    return NOTFILTERD;
}

189
static Bool
190 191 192 193 194 195 196 197 198
_XimFilterKeypress(
    Display		*d,
    Window		 w,
    XEvent		*ev,
    XPointer		 client_data)
{
    return _XimProtoKeypressFilter((Xic)client_data, (XKeyEvent *)ev );
}

199
static Bool
200 201 202 203 204 205
_XimProtoKeyreleaseFilter(
    Xic		 ic,
    XKeyEvent	*ev)
{
    Xim		im = (Xim)ic->core.im;

206
    if (IS_FABRICATED(im)) {
207
	_XimPendingFilter(ic);
208
	UNMARK_FABRICATED(im);
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
	return NOTFILTERD;
    }

    if (IS_NEGLECT_EVENT(ic, KeyReleaseMask))
	return FILTERD;

#ifdef XIM_CONNECTABLE
    if (!IS_IC_CONNECTED(ic)) {
	if (IS_CONNECTABLE(im)) {
	    if (_XimConnectServer(im)) {
		if (!_XimReCreateIC(ic)) {
		    _XimDelayModeSetAttr(im);
		    return NOTFILTERD;
		}
	    } else {
		return NOTFILTERD;
	    }
	} else {
	    return NOTFILTERD;
	}
    }
#else
    if (!IS_IC_CONNECTED(ic))
	return NOTFILTERD;
#endif /* XIM_CONNECTABLE */

    if (!IS_FORWARD_EVENT(ic, KeyReleaseMask)) {
	if (_XimOnKeysCheck(ic, ev))
	    return FILTERD;
	return NOTFILTERD;
    }
    if (_XimOffKeysCheck(ic, ev))
	    return FILTERD;

    if (_XimForwardEvent(ic, (XEvent *)ev,
				IS_SYNCHRONOUS_EVENT(ic, KeyPressMask)))
	return FILTERD;

    return NOTFILTERD;
}

250
static Bool
251 252 253 254 255 256 257 258 259
_XimFilterKeyrelease(
    Display		*d,
    Window		 w,
    XEvent		*ev,
    XPointer		 client_data)
{
    return _XimProtoKeyreleaseFilter((Xic)client_data, (XKeyEvent *)ev);
}

260
static void
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
_XimRegisterKeyPressFilter(
    Xic		 ic)
{
    if (ic->core.focus_window) {
	if (!(ic->private.proto.registed_filter_event & KEYPRESS_MASK)) {
	    _XRegisterFilterByType (ic->core.im->core.display,
				    ic->core.focus_window,
				    KeyPress, KeyPress,
				    _XimFilterKeypress,
				    (XPointer)ic);
	    ic->private.proto.registed_filter_event |= KEYPRESS_MASK;
	}
    }
    return;
}

277
static void
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
_XimRegisterKeyReleaseFilter(
    Xic		 ic)
{
    if (ic->core.focus_window) {
	if (!(ic->private.proto.registed_filter_event & KEYRELEASE_MASK)) {
	    _XRegisterFilterByType (ic->core.im->core.display,
				    ic->core.focus_window,
				    KeyRelease, KeyRelease,
				    _XimFilterKeyrelease,
				    (XPointer)ic);
	    ic->private.proto.registed_filter_event |= KEYRELEASE_MASK;
	}
    }
    return;
}

294
static void
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
_XimUnregisterKeyPressFilter(
    Xic		 ic)
{
    if (ic->core.focus_window) {
	if (ic->private.proto.registed_filter_event & KEYPRESS_MASK) {
	    _XUnregisterFilter (ic->core.im->core.display,
				ic->core.focus_window,
				_XimFilterKeypress,
				(XPointer)ic);
	    ic->private.proto.registed_filter_event &= ~KEYPRESS_MASK;
	}
    }
    return;
}

310
static void
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
_XimUnregisterKeyReleaseFilter(
    Xic		 ic)
{
    if (ic->core.focus_window) {
	if (ic->private.proto.registed_filter_event & KEYRELEASE_MASK) {
	    _XUnregisterFilter (ic->core.im->core.display,
				ic->core.focus_window,
				_XimFilterKeyrelease,
				(XPointer)ic);
	    ic->private.proto.registed_filter_event &= ~KEYRELEASE_MASK;
	}
    }
    return;
}

326
void
327 328 329 330 331 332 333 334 335
_XimRegisterFilter(
    Xic		 ic)
{
    _XimRegisterKeyPressFilter(ic);
    if (IS_FORWARD_EVENT(ic, KeyReleaseMask))
	_XimRegisterKeyReleaseFilter(ic);
    return;
}

336
void
337 338 339 340 341 342 343 344
_XimUnregisterFilter(
    Xic		 ic)
{
    _XimUnregisterKeyPressFilter(ic);
    _XimUnregisterKeyReleaseFilter(ic);
    return;
}

345
void
346 347 348 349 350 351 352 353 354 355 356
_XimReregisterFilter(
    Xic		 ic)
{
    if (IS_FORWARD_EVENT(ic, KeyReleaseMask))
	_XimRegisterKeyReleaseFilter(ic);
    else
	_XimUnregisterKeyReleaseFilter(ic);

    return;
}

357
static Bool
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
_XimFilterServerDestroy(
    Display		*d,
    Window		 w,
    XEvent		*ev,
    XPointer		 client_data)
{
    Xim			 im = (Xim)client_data;

    if (ev->type == DestroyNotify) {
	UNMARK_SERVER_CONNECTED(im);
#ifdef XIM_CONNECTABLE
	if (!IS_SERVER_CONNECTED(im) && IS_RECONNECTABLE(im)) {
	    _XimServerReconectableDestroy();
	    return True;
	}
#endif /* XIM_CONNECTABLE */
	_XimServerDestroy(im);
    }
    return True;
}

379
void
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
_XimRegisterServerFilter(
    Xim		 im)
{
    if (im->private.proto.im_window) {
	if (!(im->private.proto.registed_filter_event & DESTROYNOTIFY_MASK)) {
	    _XRegisterFilterByMask(im->core.display,
		    im->private.proto.im_window,
		    StructureNotifyMask,
		    _XimFilterServerDestroy,
		    (XPointer)im);
	    XSelectInput(im->core.display, im->private.proto.im_window,
			 StructureNotifyMask);
	    im->private.proto.registed_filter_event |= DESTROYNOTIFY_MASK;
	}
    }
    return;
}

398
void
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
_XimUnregisterServerFilter(
    Xim		 im)
{
    if (im->private.proto.im_window) {
	if (im->private.proto.registed_filter_event & DESTROYNOTIFY_MASK) {
	    _XUnregisterFilter(im->core.display,
		    im->private.proto.im_window,
		    _XimFilterServerDestroy,
		    (XPointer)im);
	    im->private.proto.registed_filter_event &= ~DESTROYNOTIFY_MASK;
	}
    }
    return;
}