imDefFlt.c 9.42 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
/* $Xorg: imDefFlt.c,v 1.4 2000/08/17 19:45:11 cpqbld Exp $ */
/******************************************************************

           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
this software for any purpose. 
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.

  Author: Takashi Fujiwara     FUJITSU LIMITED 
                               fujiwara@a80.tech.yk.fujitsu.co.jp

******************************************************************/
/* $XFree86: xc/lib/X11/imDefFlt.c,v 1.4 2001/01/17 19:41:51 dawes Exp $ */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
34
#include <nx-X11/Xatom.h>
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
#define NEED_EVENTS
#include "Xlibint.h"
#include "Xutil.h"
#include "Xlcint.h"
#include "Ximint.h"

Private long
_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;
}

Private long
_XimTriggerOnCheck(
    Xim		 im,
    XKeyEvent	*ev)
{
    return _XimTriggerCheck(im, ev, (INT32)im->private.proto.im_onkeylist[0],
					&im->private.proto.im_onkeylist[1]);
}

Private long
_XimTriggerOffCheck(
    Xim		 im,
    XKeyEvent	*ev)
{
    return _XimTriggerCheck(im, ev, (INT32)im->private.proto.im_offkeylist[0],
					&im->private.proto.im_offkeylist[1]);
}

Private Bool
_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;
}

Private Bool
_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;
}

Private void
_XimPendingFilter(
    Xic	 	 ic)
{
    Xim          im = (Xim)ic->core.im;

    if (IS_NEED_SYNC_REPLY(ic)) {
	(void)_XimProcSyncReply(im, ic);
	UNMARK_NEED_SYNC_REPLY(ic);
    }
    return;
}

Private Bool
_XimProtoKeypressFilter(
    Xic		 ic,
    XKeyEvent	*ev)
{
#ifdef XIM_CONNECTABLE
    Xim		im = (Xim)ic->core.im;
#endif

    if (IS_FABLICATED(ic)) {
	_XimPendingFilter(ic);
	UNMARK_FABLICATED(ic);
	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;
}

Private Bool
_XimFilterKeypress(
    Display		*d,
    Window		 w,
    XEvent		*ev,
    XPointer		 client_data)
{
    return _XimProtoKeypressFilter((Xic)client_data, (XKeyEvent *)ev );
}

Private Bool
_XimProtoKeyreleaseFilter(
    Xic		 ic,
    XKeyEvent	*ev)
{
#ifdef XIM_CONNECTABLE
    Xim		im = (Xim)ic->core.im;
#endif

    if (IS_FABLICATED(ic)) {
	_XimPendingFilter(ic);
	UNMARK_FABLICATED(ic);
	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;
}

Private Bool
_XimFilterKeyrelease(
    Display		*d,
    Window		 w,
    XEvent		*ev,
    XPointer		 client_data)
{
    return _XimProtoKeyreleaseFilter((Xic)client_data, (XKeyEvent *)ev);
}

Private void 
_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;
}

Private void 
_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;
}

Private void 
_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;
}

Private void 
_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;
}

Public void
_XimRegisterFilter(
    Xic		 ic)
{
    _XimRegisterKeyPressFilter(ic);
    if (IS_FORWARD_EVENT(ic, KeyReleaseMask))
	_XimRegisterKeyReleaseFilter(ic);
    return;
}

Public void
_XimUnregisterFilter(
    Xic		 ic)
{
    _XimUnregisterKeyPressFilter(ic);
    _XimUnregisterKeyReleaseFilter(ic);
    return;
}

Public void
_XimReregisterFilter(
    Xic		 ic)
{
    if (IS_FORWARD_EVENT(ic, KeyReleaseMask))
	_XimRegisterKeyReleaseFilter(ic);
    else
	_XimUnregisterKeyReleaseFilter(ic);

    return;
}

Private Bool
_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;
}

Public void 
_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;
}

Public void 
_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;
}