imInsClbk.c revision d4a3aaf4
11ab64890Smrg/******************************************************************
21ab64890Smrg
31ab64890Smrg           Copyright 1993, 1994 by Sony Corporation
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 Sony Corporation
101ab64890Smrg not be used in advertising or publicity pertaining to distribution
111ab64890Smrg of the software without specific, written prior permission.
121ab64890SmrgSony Corporation makes no representations about the suitability of
131ab64890Smrg this software for any purpose. It is provided "as is" without
141ab64890Smrg express or implied warranty.
151ab64890Smrg
161ab64890SmrgSONY CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
171ab64890SmrgINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
181ab64890SmrgEVENT SHALL SONY CORPORATION 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
241ab64890Smrg  Author:   Makoto Wakamatsu   Sony Corporation
251ab64890Smrg                               makoto@sm.sony.co.jp
261ab64890Smrg
271ab64890Smrg******************************************************************/
281ab64890Smrg
291ab64890Smrg#ifdef HAVE_CONFIG_H
301ab64890Smrg#include <config.h>
311ab64890Smrg#endif
321ab64890Smrg#include	<X11/Xatom.h>
331ab64890Smrg#include	"Xlibint.h"
341ab64890Smrg#include	"Xlcint.h"
351ab64890Smrg#include	"XlcPublic.h"
361ab64890Smrg#include	"Ximint.h"
371ab64890Smrg
381ab64890Smrg
391ab64890Smrgtypedef struct _XimInstCallback {
401ab64890Smrg    Bool			 call;
411ab64890Smrg    Bool			 destroy;
421ab64890Smrg    Display			*display;
431ab64890Smrg    XLCd			 lcd;
441ab64890Smrg    char			 name[XIM_MAXLCNAMELEN];
451ab64890Smrg    char			*modifiers;
461ab64890Smrg    XrmDatabase			 rdb;
471ab64890Smrg    char			*res_name;
481ab64890Smrg    char			*res_class;
491ab64890Smrg    XIDProc			 callback;
501ab64890Smrg    XPointer			 client_data;
511ab64890Smrg    struct _XimInstCallback	*next;
521ab64890Smrg} XimInstCallbackRec, *XimInstCallback;
531ab64890Smrg
541ab64890Smrg
55eb411b4bSmrgstatic XimInstCallback	callback_list	= NULL;
56eb411b4bSmrgstatic Bool		lock		= False;
571ab64890Smrg
581ab64890Smrg
59eb411b4bSmrgstatic void
601ab64890SmrgMakeLocale( XLCd lcd, char locale[] )
611ab64890Smrg{
621ab64890Smrg    char	*language, *territory, *codeset;
631ab64890Smrg
641ab64890Smrg    _XGetLCValues( lcd, XlcNLanguage, &language, XlcNTerritory, &territory,
651ab64890Smrg		     XlcNCodeset, &codeset, NULL );
661ab64890Smrg
671ab64890Smrg    strcpy( locale, language );
681ab64890Smrg    if( territory  &&  *territory ) {
691ab64890Smrg	strcat( locale, "_" );
701ab64890Smrg	strcat( locale, territory );
711ab64890Smrg    }
721ab64890Smrg    if( codeset  &&  *codeset ) {
731ab64890Smrg	strcat( locale, "." );
741ab64890Smrg	strcat( locale, codeset );
751ab64890Smrg    }
761ab64890Smrg}
771ab64890Smrg
781ab64890Smrg
79eb411b4bSmrgstatic Bool
801ab64890Smrg_XimFilterPropertyNotify(
811ab64890Smrg    Display	*display,
821ab64890Smrg    Window	 window,
831ab64890Smrg    XEvent	*event,
841ab64890Smrg    XPointer	 client_data)
851ab64890Smrg{
861ab64890Smrg    Atom		ims, actual_type, *atoms;
871ab64890Smrg    int			actual_format;
881ab64890Smrg    unsigned long	nitems, bytes_after;
891ab64890Smrg    int			ii;
901ab64890Smrg    XIM			xim;
911ab64890Smrg    Bool		flag = False;
921ab64890Smrg    XimInstCallback	icb, picb, tmp;
931ab64890Smrg
941ab64890Smrg    if( (ims = XInternAtom( display, XIM_SERVERS, True )) == None  ||
951ab64890Smrg	event->xproperty.atom != ims  ||
961ab64890Smrg	event->xproperty.state == PropertyDelete )
971ab64890Smrg	return( False );
981ab64890Smrg
991ab64890Smrg    if( XGetWindowProperty( display, RootWindow(display, 0), ims, 0L, 1000000L,
1001ab64890Smrg			    False, XA_ATOM, &actual_type, &actual_format,
1011ab64890Smrg			    &nitems, &bytes_after, (unsigned char **)&atoms )
1021ab64890Smrg	    != Success ) {
1031ab64890Smrg	return( False );
1041ab64890Smrg    }
1051ab64890Smrg    if( actual_type != XA_ATOM  ||  actual_format != 32 ) {
1061ab64890Smrg	XFree( atoms );
1071ab64890Smrg	return( False );
1081ab64890Smrg    }
1091ab64890Smrg
1101ab64890Smrg    lock = True;
1113233502eSmrg    for( ii = 0; ii < nitems; ii++ ) {
1121ab64890Smrg	if(XGetSelectionOwner (display, atoms[ii])) {
1131ab64890Smrg	    for( icb = callback_list; icb; icb = icb->next ) {
1141ab64890Smrg		if( !icb->call  &&  !icb->destroy ) {
1151ab64890Smrg		    xim = (*icb->lcd->methods->open_im)( icb->lcd, display,
1161ab64890Smrg							 icb->rdb,
1171ab64890Smrg							 icb->res_name,
1181ab64890Smrg							 icb->res_class );
1191ab64890Smrg		    if( xim ) {
1201ab64890Smrg			xim->methods->close( (XIM)xim );
1211ab64890Smrg			flag = True;
1221ab64890Smrg			icb->call = True;
1231ab64890Smrg			icb->callback( icb->display, icb->client_data, NULL );
1241ab64890Smrg		    }
1251ab64890Smrg		}
1261ab64890Smrg	    }
1271ab64890Smrg	    break;
1281ab64890Smrg	}
1291ab64890Smrg    }
1301ab64890Smrg    XFree( atoms );
1311ab64890Smrg
1321ab64890Smrg    for( icb = callback_list, picb = NULL; icb; ) {
1331ab64890Smrg	if( icb->destroy ) {
1341ab64890Smrg	    if( picb )
1351ab64890Smrg		picb->next = icb->next;
1361ab64890Smrg	    else
1371ab64890Smrg		callback_list = icb->next;
1381ab64890Smrg	    tmp = icb;
1391ab64890Smrg	    icb = icb->next;
1401ab64890Smrg	    XFree( tmp );
1411ab64890Smrg	}
1421ab64890Smrg	else {
1431ab64890Smrg	    picb = icb;
1441ab64890Smrg	    icb = icb->next;
1451ab64890Smrg	}
1461ab64890Smrg    }
1471ab64890Smrg    lock = False;
1481ab64890Smrg
1491ab64890Smrg    return( flag );
1501ab64890Smrg}
1511ab64890Smrg
1521ab64890Smrg
153eb411b4bSmrgBool
1541ab64890Smrg_XimRegisterIMInstantiateCallback(
1551ab64890Smrg    XLCd	 lcd,
1561ab64890Smrg    Display	*display,
1571ab64890Smrg    XrmDatabase	 rdb,
1581ab64890Smrg    char	*res_name,
1591ab64890Smrg    char	*res_class,
1601ab64890Smrg    XIDProc	 callback,
1611ab64890Smrg    XPointer	 client_data)
1621ab64890Smrg{
1631ab64890Smrg    XimInstCallback	icb, tmp;
1641ab64890Smrg    XIM			xim;
165d4a3aaf4Smrg    char		*modifiers = NULL;
1661ab64890Smrg    Window		root;
1671ab64890Smrg    XWindowAttributes	attr;
1681ab64890Smrg
1691ab64890Smrg    if( lock )
1701ab64890Smrg	return( False );
1711ab64890Smrg
172818534a1Smrg    icb = Xmalloc(sizeof(XimInstCallbackRec));
1731ab64890Smrg    if( !icb )
1741ab64890Smrg	return( False );
175d4a3aaf4Smrg    if (lcd->core->modifiers) {
176d4a3aaf4Smrg	modifiers = strdup(lcd->core->modifiers);
177d4a3aaf4Smrg	if (!modifiers) {
178d4a3aaf4Smrg	    Xfree(icb);
179d4a3aaf4Smrg	    return( False );
180d4a3aaf4Smrg	}
181d4a3aaf4Smrg    }
1821ab64890Smrg    icb->call = icb->destroy = False;
1831ab64890Smrg    icb->display = display;
1841ab64890Smrg    icb->lcd = lcd;
1851ab64890Smrg    MakeLocale( lcd, icb->name );
186d4a3aaf4Smrg    icb->modifiers = modifiers;
1871ab64890Smrg    icb->rdb = rdb;
1881ab64890Smrg    icb->res_name = res_name;
1891ab64890Smrg    icb->res_class = res_class;
1901ab64890Smrg    icb->callback = callback;
1911ab64890Smrg    icb->client_data = client_data;
1921ab64890Smrg    icb->next = NULL;
1931ab64890Smrg
1941ab64890Smrg    if( !callback_list )
1951ab64890Smrg	callback_list = icb;
1961ab64890Smrg    else {
1971ab64890Smrg	for( tmp = callback_list; tmp->next; tmp = tmp->next );
1981ab64890Smrg	tmp->next = icb;
1991ab64890Smrg    }
2001ab64890Smrg
2011ab64890Smrg    xim = (*lcd->methods->open_im)( lcd, display, rdb, res_name, res_class );
2021ab64890Smrg
2031ab64890Smrg    if( icb == callback_list ) {
2041ab64890Smrg	root = RootWindow( display, 0 );
2051ab64890Smrg	XGetWindowAttributes( display, root, &attr );
2061ab64890Smrg	_XRegisterFilterByType( display, root, PropertyNotify, PropertyNotify,
2071ab64890Smrg				_XimFilterPropertyNotify, (XPointer)NULL );
2081ab64890Smrg	XSelectInput( display, root,
2091ab64890Smrg		      attr.your_event_mask | PropertyChangeMask );
2101ab64890Smrg    }
2111ab64890Smrg
2121ab64890Smrg    if( xim ) {
2131ab64890Smrg	lock = True;
2141ab64890Smrg	xim->methods->close( (XIM)xim );
2151ab64890Smrg	lock = False;
2161ab64890Smrg	icb->call = True;
2171ab64890Smrg	callback( display, client_data, NULL );
2181ab64890Smrg    }
2191ab64890Smrg
2201ab64890Smrg    return( True );
2211ab64890Smrg}
2221ab64890Smrg
2231ab64890Smrg
224eb411b4bSmrgBool
2251ab64890Smrg_XimUnRegisterIMInstantiateCallback(
2261ab64890Smrg    XLCd	 lcd,
2271ab64890Smrg    Display	*display,
2281ab64890Smrg    XrmDatabase	 rdb,
2291ab64890Smrg    char	*res_name,
2301ab64890Smrg    char	*res_class,
2311ab64890Smrg    XIDProc	 callback,
2321ab64890Smrg    XPointer	 client_data)
2331ab64890Smrg{
2341ab64890Smrg    char		locale[XIM_MAXLCNAMELEN];
2351ab64890Smrg    XimInstCallback	icb, picb;
2361ab64890Smrg
2371ab64890Smrg    if( !callback_list )
2381ab64890Smrg	return( False );
2391ab64890Smrg
2401ab64890Smrg    MakeLocale( lcd, locale );
2411ab64890Smrg
2421ab64890Smrg    for( icb = callback_list, picb = NULL; icb; picb = icb, icb = icb->next ) {
2431ab64890Smrg	if( !strcmp( locale, icb->name )  &&
2441ab64890Smrg	    (lcd->core->modifiers == icb->modifiers  ||		/* XXXXX */
2451ab64890Smrg	     (lcd->core->modifiers  &&  icb->modifiers  &&
2461ab64890Smrg	      !strcmp( lcd->core->modifiers, icb->modifiers )))  &&
2471ab64890Smrg	    rdb == icb->rdb  &&					/* XXXXX */
2481ab64890Smrg	    ((res_name == NULL  &&  icb->res_name == NULL)  ||
2491ab64890Smrg	     (res_name != NULL  &&  icb->res_name != NULL  &&
2501ab64890Smrg	      !strcmp( res_name, icb->res_name )))  &&
2511ab64890Smrg	    ((res_class == NULL  &&  icb->res_class == NULL)  ||
2521ab64890Smrg	     (res_class != NULL  &&  icb->res_class != NULL  &&
2531ab64890Smrg	      !strcmp( res_class, icb->res_class )))  &&
2541ab64890Smrg	    (callback == icb->callback)  &&
2551ab64890Smrg	    (client_data  ==  icb->client_data)  &&		/* XXXXX */
2561ab64890Smrg	    !icb->destroy ) {
2571ab64890Smrg	    if( lock )
2581ab64890Smrg		icb->destroy = True;
2591ab64890Smrg	    else {
2601ab64890Smrg		if( !picb ) {
2611ab64890Smrg		    callback_list = icb->next;
2621ab64890Smrg		    _XUnregisterFilter( display, RootWindow(display, 0),
2631ab64890Smrg					_XimFilterPropertyNotify,
2641ab64890Smrg					(XPointer)NULL );
2651ab64890Smrg		}
2661ab64890Smrg		else
2671ab64890Smrg		    picb->next = icb->next;
2681ab64890Smrg		_XCloseLC( icb->lcd );
269d4a3aaf4Smrg		XFree( icb->modifiers );
2701ab64890Smrg		XFree( icb );
2711ab64890Smrg	    }
2721ab64890Smrg	    return( True );
2731ab64890Smrg	}
2741ab64890Smrg    }
2751ab64890Smrg    return( False );
2761ab64890Smrg}
2771ab64890Smrg
2781ab64890Smrg
279eb411b4bSmrgvoid
28061b2299dSmrg_XimResetIMInstantiateCallback(Xim xim)
2811ab64890Smrg{
2821ab64890Smrg    char		 locale[XIM_MAXLCNAMELEN];
2831ab64890Smrg    XimInstCallback	 icb;
2841ab64890Smrg    XLCd	 	 lcd = xim->core.lcd;
2851ab64890Smrg
2861ab64890Smrg    if( !callback_list  &&  lock )
2871ab64890Smrg	return;
2881ab64890Smrg
2891ab64890Smrg    MakeLocale( lcd, locale );
2901ab64890Smrg
2911ab64890Smrg    for( icb = callback_list; icb; icb = icb->next )
2921ab64890Smrg	if( !strcmp( locale, icb->name )  &&
2931ab64890Smrg	    (lcd->core->modifiers == icb->modifiers  ||
2941ab64890Smrg	     (lcd->core->modifiers  &&  icb->modifiers  &&
2951ab64890Smrg	      !strcmp( lcd->core->modifiers, icb->modifiers ))) )
2961ab64890Smrg	    icb->call = False;
2971ab64890Smrg}
298