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