11ab64890Smrg/******************************************************************
21ab64890Smrg
31ab64890Smrg           Copyright 1992, 1993, 1994 by FUJITSU LIMITED
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software
61ab64890Smrgand its documentation for any purpose is hereby granted without fee,
71ab64890Smrgprovided that the above copyright notice appear in all copies and
81ab64890Smrgthat both that copyright notice and this permission notice appear
91ab64890Smrgin supporting documentation, and that the name of FUJITSU LIMITED
101ab64890Smrgnot be used in advertising or publicity pertaining to distribution
111ab64890Smrgof the software without specific, written prior permission.
121ab64890SmrgFUJITSU LIMITED makes no representations about the suitability of
1361b2299dSmrgthis software for any purpose.
141ab64890SmrgIt is provided "as is" without express or implied warranty.
151ab64890Smrg
161ab64890SmrgFUJITSU LIMITED DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
171ab64890SmrgINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
181ab64890SmrgEVENT SHALL FUJITSU LIMITED BE LIABLE FOR ANY SPECIAL, INDIRECT OR
191ab64890SmrgCONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
201ab64890SmrgUSE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
211ab64890SmrgOTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
221ab64890SmrgPERFORMANCE OF THIS SOFTWARE.
231ab64890Smrg
2461b2299dSmrg  Author: Takashi Fujiwara     FUJITSU LIMITED
251ab64890Smrg                               fujiwara@a80.tech.yk.fujitsu.co.jp
261ab64890Smrg
271ab64890Smrg******************************************************************/
281ab64890Smrg
291ab64890Smrg#ifdef HAVE_CONFIG_H
301ab64890Smrg#include <config.h>
311ab64890Smrg#endif
321ab64890Smrg#include <X11/Xatom.h>
331ab64890Smrg#include <X11/Xlib.h>
341ab64890Smrg#include <X11/Xmd.h>
351ab64890Smrg#include "Xlibint.h"
361ab64890Smrg#include "Xlcint.h"
371ab64890Smrg#include "Ximint.h"
381ab64890Smrg#include "XimImSw.h"
391ab64890Smrg
40eb411b4bSmrgstatic Xim 		*_XimCurrentIMlist  = (Xim *)NULL;
41eb411b4bSmrgstatic int		 _XimCurrentIMcount = 0;
421ab64890Smrg
43eb411b4bSmrgstatic Bool
441ab64890Smrg_XimSetIMStructureList(
451ab64890Smrg    Xim		  im)
461ab64890Smrg{
471ab64890Smrg    register int  i;
481ab64890Smrg    Xim		 *xim;
491ab64890Smrg
501ab64890Smrg    if(!(_XimCurrentIMlist)) {
51818534a1Smrg	if(!(_XimCurrentIMlist = Xmalloc(sizeof(Xim))))
521ab64890Smrg	    return False;
531ab64890Smrg	_XimCurrentIMlist[0] = im;
541ab64890Smrg	_XimCurrentIMcount   = 1;
551ab64890Smrg    }
561ab64890Smrg    else {
571ab64890Smrg	for(i = 0; i < _XimCurrentIMcount; i++) {
581ab64890Smrg	    if(!( _XimCurrentIMlist[i])) {
591ab64890Smrg		_XimCurrentIMlist[i] = im;
601ab64890Smrg		break;
611ab64890Smrg	    }
621ab64890Smrg	}
631ab64890Smrg	if(i >= _XimCurrentIMcount) {
64818534a1Smrg	    if(!(xim = Xrealloc(_XimCurrentIMlist,
651ab64890Smrg					 ((i + 1) * sizeof(Xim)))))
661ab64890Smrg		return False;
671ab64890Smrg	    _XimCurrentIMlist			  = xim;
681ab64890Smrg	    _XimCurrentIMlist[_XimCurrentIMcount] = im;
691ab64890Smrg	    _XimCurrentIMcount++;
701ab64890Smrg	}
711ab64890Smrg    }
721ab64890Smrg    return True;
731ab64890Smrg}
741ab64890Smrg
75eb411b4bSmrgvoid
7661b2299dSmrg_XimDestroyIMStructureList(Xim im)
771ab64890Smrg{
781ab64890Smrg    register int  i;
791ab64890Smrg
801ab64890Smrg    for(i = 0; i < _XimCurrentIMcount; i++) {
811ab64890Smrg	if(_XimCurrentIMlist[i] == im) {
821ab64890Smrg	    _XimCurrentIMlist[i] = NULL;
831ab64890Smrg	    break;
841ab64890Smrg	}
851ab64890Smrg    }
861ab64890Smrg    return;
871ab64890Smrg}
881ab64890Smrg
89eb411b4bSmrgvoid
9061b2299dSmrg_XimServerDestroy(Xim im_2_destroy)
911ab64890Smrg{
921ab64890Smrg    register int  i;
931ab64890Smrg    Xim		  im;
941ab64890Smrg    XIC		  ic;
951ab64890Smrg
961ab64890Smrg    for(i = 0; i < _XimCurrentIMcount; i++) {
971ab64890Smrg	if(!(im = _XimCurrentIMlist[i]))
981ab64890Smrg	    continue;
991ab64890Smrg	/*
1001ab64890Smrg	 * Only continue if this im is the one to be destroyed.
1011ab64890Smrg	 */
1021ab64890Smrg	if (im != im_2_destroy)
1031ab64890Smrg	    continue;
1041ab64890Smrg
1051ab64890Smrg	if (im->core.destroy_callback.callback)
1061ab64890Smrg	    (*im->core.destroy_callback.callback)((XIM)im,
1071ab64890Smrg			im->core.destroy_callback.client_data, NULL);
1081ab64890Smrg	for (ic = im->core.ic_chain; ic; ic = ic->core.next) {
1091ab64890Smrg	    if (ic->core.destroy_callback.callback) {
1101ab64890Smrg		(*ic->core.destroy_callback.callback)(ic,
1111ab64890Smrg			ic->core.destroy_callback.client_data, NULL);
1121ab64890Smrg	    }
1131ab64890Smrg	}
1141ab64890Smrg	_XimResetIMInstantiateCallback( im );
1151ab64890Smrg	(void)im->methods->close((XIM)im);
1161ab64890Smrg	Xfree(im);
1171ab64890Smrg	_XimCurrentIMlist[i] = NULL;
1181ab64890Smrg	return;
1191ab64890Smrg    }
1201ab64890Smrg}
1211ab64890Smrg
1221ab64890Smrg#ifdef XIM_CONNECTABLE
123eb411b4bSmrgvoid
12461b2299dSmrg_XimServerReconectableDestroy(void)
1251ab64890Smrg{
1261ab64890Smrg    register int  i;
1271ab64890Smrg    Xim		  im;
1281ab64890Smrg    XIC		  ic;
1291ab64890Smrg
1301ab64890Smrg    for(i = 0; i < _XimCurrentIMcount; i++) {
1311ab64890Smrg	if(!(im = _XimCurrentIMlist[i]))
1321ab64890Smrg	    continue;
1331ab64890Smrg
1341ab64890Smrg	if (im->core.destroy_callback.callback)
1351ab64890Smrg	    (*im->core.destroy_callback.callback)(im,
1361ab64890Smrg			im->core.destroy_callback.client_data, NULL);
1371ab64890Smrg	for (ic = im->core.ic_chain; ic; ic = ic->core.next) {
1381ab64890Smrg	    if (ic->core.destroy_callback.callback) {
1391ab64890Smrg		(*ic->core.destroy_callback.callback)(ic,
1401ab64890Smrg			ic->core.destroy_callback.client_data, NULL);
1411ab64890Smrg	    }
1421ab64890Smrg	}
1431ab64890Smrg	_XimResetIMInstantiateCallback( im );
1441ab64890Smrg	(void)im->methods->close((XIM)im);
1451ab64890Smrg    }
1461ab64890Smrg    return;
1471ab64890Smrg}
1481ab64890Smrg#endif /* XIM_CONNECTABLE */
1491ab64890Smrg
150eb411b4bSmrgstatic const char *
1511ab64890Smrg_XimStrstr(
1521ab64890Smrg    register const char *src,
1531ab64890Smrg    register const char *dest)
1541ab64890Smrg{
1551ab64890Smrg    int			 len;
15661b2299dSmrg
1571ab64890Smrg    len = strlen(dest);
1581ab64890Smrg    while((src = strchr(src, *dest))) {
1591ab64890Smrg	if(!strncmp(src, dest, len))
1601ab64890Smrg	    return src;
1611ab64890Smrg	src++;
1621ab64890Smrg    }
1631ab64890Smrg    return NULL;
1641ab64890Smrg}
1651ab64890Smrg
166eb411b4bSmrgstatic char *
1671ab64890Smrg_XimMakeImName(
1681ab64890Smrg    XLCd	   lcd)
1691ab64890Smrg{
1701ab64890Smrg    const char* begin = NULL;
1711ab64890Smrg    const char* end = NULL;
1721ab64890Smrg    char* ret = NULL;
1731ab64890Smrg    const char* ximmodifier = XIMMODIFIER;
1741ab64890Smrg
1751ab64890Smrg    if(lcd->core->modifiers != NULL && *lcd->core->modifiers != '\0') {
1761ab64890Smrg	begin = _XimStrstr(lcd->core->modifiers, ximmodifier);
1771ab64890Smrg	if (begin != NULL) {
1781ab64890Smrg	    end = begin += strlen(ximmodifier);
1791ab64890Smrg	    while (*end && *end != '@')
1801ab64890Smrg		end++;
1811ab64890Smrg	}
1821ab64890Smrg    }
1831ab64890Smrg    ret = Xmalloc(end - begin + 1);
1841ab64890Smrg    if (ret != NULL) {
1851ab64890Smrg	if (begin != NULL && end != NULL) {
1861ab64890Smrg	    (void)strncpy(ret, begin, end - begin);
1871ab64890Smrg	    ret[end - begin] = '\0';
1881ab64890Smrg	} else {
1891ab64890Smrg	    ret[0] = '\0';
1901ab64890Smrg	}
1911ab64890Smrg    }
1921ab64890Smrg
1931ab64890Smrg    return ret;
1941ab64890Smrg}
1951ab64890Smrg
196eb411b4bSmrgXIM
1971ab64890Smrg_XimOpenIM(
1981ab64890Smrg    XLCd		 lcd,
1991ab64890Smrg    Display		*dpy,
2001ab64890Smrg    XrmDatabase		 rdb,
2011ab64890Smrg    char		*res_name,
2021ab64890Smrg    char		*res_class)
2031ab64890Smrg{
2041ab64890Smrg    Xim			 im;
2051ab64890Smrg    register int	 i;
20661b2299dSmrg
2076cc2b21fSmrg    if (!(im = Xcalloc(1, sizeof(XimRec))))
2081ab64890Smrg	return (XIM)NULL;
2091ab64890Smrg
2101ab64890Smrg    im->core.lcd       = lcd;
2111ab64890Smrg    im->core.ic_chain  = (XIC)NULL;
2121ab64890Smrg    im->core.display   = dpy;
2131ab64890Smrg    im->core.rdb       = rdb;
2141ab64890Smrg    im->core.res_name  = NULL;
2151ab64890Smrg    im->core.res_class = NULL;
2161ab64890Smrg    if((res_name != NULL) && (*res_name != '\0')){
2176cc2b21fSmrg	if(!(im->core.res_name  = strdup(res_name)))
2181ab64890Smrg	    goto Error1;
2191ab64890Smrg    }
2201ab64890Smrg    if((res_class != NULL) && (*res_class != '\0')){
2216cc2b21fSmrg	if(!(im->core.res_class = strdup(res_class)))
2221ab64890Smrg	    goto Error2;
2231ab64890Smrg    }
2241ab64890Smrg    if(!(im->core.im_name = _XimMakeImName(lcd)))
2251ab64890Smrg	goto Error3;
2261ab64890Smrg
2271ab64890Smrg    for(i= 0; ; i++) {
2281ab64890Smrg	if(_XimImSportRec[i].checkprocessing(im)) {
2291ab64890Smrg	    if(!(_XimImSportRec[i].im_open(im)))
2301ab64890Smrg		goto Error4;
2311ab64890Smrg	    if(!_XimSetIMStructureList(im))
2321ab64890Smrg		goto Error4;
2331ab64890Smrg	    return (XIM)im;
2341ab64890Smrg	}
2351ab64890Smrg    }
2361ab64890Smrg
2371ab64890SmrgError4 :
2381ab64890Smrg    _XimImSportRec[i].im_free(im);
2391ab64890Smrg    Xfree(im);
2401ab64890Smrg    return NULL;
2411ab64890SmrgError3 :
2420f8248bfSmrg    Xfree(im->core.im_name);
2431ab64890SmrgError2:
2440f8248bfSmrg    Xfree(im->core.res_class);
2451ab64890SmrgError1:
2460f8248bfSmrg    Xfree(im->core.res_name);
2471ab64890Smrg    Xfree(im);
2481ab64890Smrg    return NULL;
2491ab64890Smrg}
2501ab64890Smrg
251eb411b4bSmrgBool
25261b2299dSmrg_XInitIM(XLCd lcd)
2531ab64890Smrg{
2541ab64890Smrg    if(lcd == (XLCd)NULL)
2551ab64890Smrg	return False;
2561ab64890Smrg    lcd->methods->open_im = _XimOpenIM;
2571ab64890Smrg    lcd->methods->register_callback = _XimRegisterIMInstantiateCallback;
2581ab64890Smrg    lcd->methods->unregister_callback = _XimUnRegisterIMInstantiateCallback;
2591ab64890Smrg    return True;
2601ab64890Smrg}
261