11ab64890Smrg/*
21ab64890Smrg * Copyright 1990, 1991 by OMRON Corporation, NTT Software Corporation,
31ab64890Smrg *                      and Nippon Telegraph and Telephone Corporation
41ab64890Smrg * Copyright 1991 by the Open Software Foundation
51ab64890Smrg * Copyright 1993 by the FUJITSU LIMITED
61ab64890Smrg *
71ab64890Smrg * Permission to use, copy, modify, distribute, and sell this software and its
81ab64890Smrg * documentation for any purpose is hereby granted without fee, provided that
91ab64890Smrg * the above copyright notice appear in all copies and that both that
101ab64890Smrg * copyright notice and this permission notice appear in supporting
111ab64890Smrg * documentation, and that the names of OMRON, NTT Software, NTT, and
1261b2299dSmrg * Open Software Foundation not be used in advertising or publicity
1361b2299dSmrg * pertaining to distribution of the software without specific,
141ab64890Smrg * written prior permission. OMRON, NTT Software, NTT, and Open Software
151ab64890Smrg * Foundation make no representations about the suitability of this
161ab64890Smrg * software for any purpose.  It is provided "as is" without express or
171ab64890Smrg * implied warranty.
181ab64890Smrg *
1961b2299dSmrg * OMRON, NTT SOFTWARE, NTT, AND OPEN SOFTWARE FOUNDATION
2061b2299dSmrg * DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
2161b2299dSmrg * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
221ab64890Smrg * SHALL OMRON, NTT SOFTWARE, NTT, OR OPEN SOFTWARE FOUNDATION BE
2361b2299dSmrg * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
241ab64890Smrg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
251ab64890Smrg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
261ab64890Smrg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2761b2299dSmrg *
281ab64890Smrg *	Authors: Li Yuhong		OMRON Corporation
291ab64890Smrg *		 Tatsuya Kato		NTT Software Corporation
301ab64890Smrg *		 Hiroshi Kuribayashi	OMRON Coproration
311ab64890Smrg *		 Muneiyoshi Suzuki	Nippon Telegraph and Telephone Co.
3261b2299dSmrg *
3361b2299dSmrg *		 M. Collins		OSF
341ab64890Smrg *		 Takashi Fujiwara	FUJITSU LIMITED
3561b2299dSmrg */
361ab64890Smrg/*
371ab64890Smrg
381ab64890SmrgCopyright 1991, 1998  The Open Group
391ab64890Smrg
401ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
411ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
421ab64890Smrgthe above copyright notice appear in all copies and that both that
431ab64890Smrgcopyright notice and this permission notice appear in supporting
441ab64890Smrgdocumentation.
451ab64890Smrg
461ab64890SmrgThe above copyright notice and this permission notice shall be included
471ab64890Smrgin all copies or substantial portions of the Software.
481ab64890Smrg
491ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
501ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
511ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
521ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
531ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
541ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
551ab64890SmrgOTHER DEALINGS IN THE SOFTWARE.
561ab64890Smrg
571ab64890SmrgExcept as contained in this notice, the name of The Open Group shall
581ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or
591ab64890Smrgother dealings in this Software without prior written authorization
601ab64890Smrgfrom The Open Group.
611ab64890Smrg
621ab64890Smrg*/
631ab64890Smrg
641ab64890Smrg#ifdef HAVE_CONFIG_H
651ab64890Smrg#include <config.h>
661ab64890Smrg#endif
671ab64890Smrg#include "Xlibint.h"
681ab64890Smrg#include "Xlcint.h"
69258a0ebeSmrg#include "reallocarray.h"
701ab64890Smrg
711ab64890Smrgstatic int
721ab64890Smrg_XIMNestedListToNestedList(
731ab64890Smrg    XIMArg *nlist,   /* This is the new list */
741ab64890Smrg    XIMArg *list)    /* The original list */
751ab64890Smrg{
761ab64890Smrg    register XIMArg *ptr = list;
771ab64890Smrg
781ab64890Smrg    while (ptr->name) {
791ab64890Smrg	if (!strcmp(ptr->name, XNVaNestedList)) {
801ab64890Smrg	    nlist += _XIMNestedListToNestedList(nlist, (XIMArg *)ptr->value);
811ab64890Smrg	} else {
821ab64890Smrg	    nlist->name = ptr->name;
831ab64890Smrg	    nlist->value = ptr->value;
841ab64890Smrg	    ptr++;
851ab64890Smrg	    nlist++;
861ab64890Smrg	}
871ab64890Smrg    }
889c019ec5Smaya    return (int) (ptr - list);
891ab64890Smrg}
901ab64890Smrg
911ab64890Smrgstatic void
921ab64890Smrg_XIMCountNestedList(
931ab64890Smrg    XIMArg *args,
941ab64890Smrg    int *total_count)
951ab64890Smrg{
961ab64890Smrg    for (; args->name; args++) {
971ab64890Smrg	if (!strcmp(args->name, XNVaNestedList))
981ab64890Smrg	    _XIMCountNestedList((XIMArg *)args->value, total_count);
991ab64890Smrg	else
1001ab64890Smrg	    ++(*total_count);
1011ab64890Smrg    }
1021ab64890Smrg}
1031ab64890Smrg
1041ab64890Smrgstatic void
1051ab64890Smrg_XIMCountVaList(va_list var, int *total_count)
1061ab64890Smrg{
1071ab64890Smrg    char *attr;
1081ab64890Smrg
1091ab64890Smrg    *total_count = 0;
1101ab64890Smrg
1111ab64890Smrg    for (attr = va_arg(var, char*); attr; attr = va_arg(var, char*)) {
1121ab64890Smrg	if (!strcmp(attr, XNVaNestedList)) {
1131ab64890Smrg	    _XIMCountNestedList(va_arg(var, XIMArg*), total_count);
1141ab64890Smrg	} else {
1151ab64890Smrg	    (void)va_arg(var, XIMArg*);
1161ab64890Smrg	    ++(*total_count);
1171ab64890Smrg	}
1181ab64890Smrg    }
1191ab64890Smrg}
1201ab64890Smrg
1211ab64890Smrgstatic void
1221ab64890Smrg_XIMVaToNestedList(va_list var, int max_count, XIMArg **args_return)
1231ab64890Smrg{
1241ab64890Smrg    XIMArg *args;
1251ab64890Smrg    char   *attr;
1261ab64890Smrg
1271ab64890Smrg    if (max_count <= 0) {
1281ab64890Smrg	*args_return = (XIMArg *)NULL;
1291ab64890Smrg	return;
1301ab64890Smrg    }
1311ab64890Smrg
132258a0ebeSmrg    args = Xmallocarray((unsigned)max_count + 1, sizeof(XIMArg));
1331ab64890Smrg    *args_return = args;
1341ab64890Smrg    if (!args) return;
1351ab64890Smrg
1361ab64890Smrg    for (attr = va_arg(var, char*); attr; attr = va_arg(var, char*)) {
1371ab64890Smrg	if (!strcmp(attr, XNVaNestedList)) {
1381ab64890Smrg	    args += _XIMNestedListToNestedList(args, va_arg(var, XIMArg*));
1391ab64890Smrg	} else {
1401ab64890Smrg	    args->name = attr;
1411ab64890Smrg	    args->value = va_arg(var, XPointer);
1421ab64890Smrg	    args++;
1431ab64890Smrg	}
1441ab64890Smrg    }
1451ab64890Smrg    args->name = (char*)NULL;
1461ab64890Smrg}
1471ab64890Smrg
1481ab64890Smrg/*ARGSUSED*/
1491ab64890SmrgXVaNestedList
1501ab64890SmrgXVaCreateNestedList(int dummy, ...)
1511ab64890Smrg{
1521ab64890Smrg    va_list		var;
1531ab64890Smrg    XIMArg		*args = NULL;
1541ab64890Smrg    int			total_count;
1551ab64890Smrg
1561ab64890Smrg    va_start(var, dummy);
1571ab64890Smrg    _XIMCountVaList(var, &total_count);
1581ab64890Smrg    va_end(var);
1591ab64890Smrg
1601ab64890Smrg    va_start(var, dummy);
1611ab64890Smrg    _XIMVaToNestedList(var, total_count, &args);
1621ab64890Smrg    va_end(var);
1631ab64890Smrg
1641ab64890Smrg    return (XVaNestedList)args;
1651ab64890Smrg}
1661ab64890Smrg
1671ab64890Smrgchar *
1681ab64890SmrgXSetIMValues(XIM im, ...)
1691ab64890Smrg{
1701ab64890Smrg    va_list var;
1711ab64890Smrg    int     total_count;
1721ab64890Smrg    XIMArg *args;
1732d67cb4fSmrg    char   *ret = NULL;
1741ab64890Smrg
1751ab64890Smrg    /*
1761ab64890Smrg     * so count the stuff dangling here
1771ab64890Smrg     */
1781ab64890Smrg    va_start(var, im);
1791ab64890Smrg    _XIMCountVaList(var, &total_count);
1801ab64890Smrg    va_end(var);
1811ab64890Smrg
1821ab64890Smrg    /*
1831ab64890Smrg     * now package it up so we can send it along
1841ab64890Smrg     */
1851ab64890Smrg    va_start(var, im);
1861ab64890Smrg    _XIMVaToNestedList(var, total_count, &args);
1871ab64890Smrg    va_end(var);
1881ab64890Smrg
1892d67cb4fSmrg    if (im && im->methods)
1902d67cb4fSmrg	ret = (*im->methods->set_values) (im, args);
1910f8248bfSmrg    Xfree(args);
1921ab64890Smrg    return ret;
1931ab64890Smrg}
1941ab64890Smrg
1951ab64890Smrgchar *
1961ab64890SmrgXGetIMValues(XIM im, ...)
1971ab64890Smrg{
1981ab64890Smrg    va_list var;
1991ab64890Smrg    int     total_count;
2001ab64890Smrg    XIMArg *args;
2012d67cb4fSmrg    char   *ret = NULL;
2021ab64890Smrg
2031ab64890Smrg    /*
2041ab64890Smrg     * so count the stuff dangling here
2051ab64890Smrg     */
2061ab64890Smrg    va_start(var, im);
2071ab64890Smrg    _XIMCountVaList(var, &total_count);
2081ab64890Smrg    va_end(var);
2091ab64890Smrg
2101ab64890Smrg    /*
2111ab64890Smrg     * now package it up so we can send it along
2121ab64890Smrg     */
2131ab64890Smrg    va_start(var, im);
2141ab64890Smrg    _XIMVaToNestedList(var, total_count, &args);
2151ab64890Smrg    va_end(var);
2161ab64890Smrg
2172d67cb4fSmrg    if (im && im->methods)
2182d67cb4fSmrg	ret = (*im->methods->get_values) (im, args);
2190f8248bfSmrg    Xfree(args);
2201ab64890Smrg    return ret;
2211ab64890Smrg}
2221ab64890Smrg
2231ab64890Smrg/*
22461b2299dSmrg * Create an input context within the input method,
2251ab64890Smrg * and return a pointer to the input context.
2261ab64890Smrg */
2271ab64890Smrg
2281ab64890SmrgXIC
2291ab64890SmrgXCreateIC(XIM im, ...)
2301ab64890Smrg{
2311ab64890Smrg    va_list var;
2321ab64890Smrg    int     total_count;
2331ab64890Smrg    XIMArg *args;
2342d67cb4fSmrg    XIC     ic = NULL;
23561b2299dSmrg
2361ab64890Smrg    /*
2371ab64890Smrg     * so count the stuff dangling here
2381ab64890Smrg     */
2391ab64890Smrg    va_start(var, im);
2401ab64890Smrg    _XIMCountVaList(var, &total_count);
2411ab64890Smrg    va_end(var);
2421ab64890Smrg
2431ab64890Smrg    /*
2441ab64890Smrg     * now package it up so we can send it along
2451ab64890Smrg     */
2461ab64890Smrg    va_start(var, im);
2471ab64890Smrg    _XIMVaToNestedList(var, total_count, &args);
2481ab64890Smrg    va_end(var);
2491ab64890Smrg
2502d67cb4fSmrg    if (im && im->methods)
2512d67cb4fSmrg	ic = (XIC) (*im->methods->create_ic) (im, args);
2520f8248bfSmrg    Xfree(args);
2531ab64890Smrg    if (ic) {
2541ab64890Smrg	ic->core.next = im->core.ic_chain;
2551ab64890Smrg	im->core.ic_chain = ic;
2561ab64890Smrg    }
2571ab64890Smrg    return ic;
2581ab64890Smrg}
2591ab64890Smrg
2601ab64890Smrg/*
2611ab64890Smrg * Free the input context.
2621ab64890Smrg */
2631ab64890Smrgvoid
26461b2299dSmrgXDestroyIC(XIC ic)
2651ab64890Smrg{
2661ab64890Smrg    XIM im = ic->core.im;
2671ab64890Smrg    XIC *prev;
2681ab64890Smrg
2691ab64890Smrg    (*ic->methods->destroy) (ic);
2701ab64890Smrg    if (im) {
2711ab64890Smrg	for (prev = &im->core.ic_chain; *prev; prev = &(*prev)->core.next) {
2721ab64890Smrg	    if (*prev == ic) {
2731ab64890Smrg		*prev = ic->core.next;
2741ab64890Smrg		break;
2751ab64890Smrg	    }
2761ab64890Smrg	}
2771ab64890Smrg    }
278818534a1Smrg    Xfree (ic);
2791ab64890Smrg}
2801ab64890Smrg
2811ab64890Smrgchar *
2821ab64890SmrgXGetICValues(XIC ic, ...)
28361b2299dSmrg{
2841ab64890Smrg    va_list var;
2851ab64890Smrg    int     total_count;
2861ab64890Smrg    XIMArg *args;
2871ab64890Smrg    char   *ret;
2881ab64890Smrg
2891ab64890Smrg    if (!ic->core.im)
2901ab64890Smrg	return (char *) NULL;
2911ab64890Smrg
2921ab64890Smrg    /*
2931ab64890Smrg     * so count the stuff dangling here
2941ab64890Smrg     */
2951ab64890Smrg    va_start(var, ic);
2961ab64890Smrg    _XIMCountVaList(var, &total_count);
2971ab64890Smrg    va_end(var);
2981ab64890Smrg
2991ab64890Smrg    /*
3001ab64890Smrg     * now package it up so we can send it along
3011ab64890Smrg     */
3021ab64890Smrg    va_start(var, ic);
3031ab64890Smrg    _XIMVaToNestedList(var, total_count, &args);
3041ab64890Smrg    va_end(var);
3051ab64890Smrg
3061ab64890Smrg    ret = (*ic->methods->get_values) (ic, args);
3070f8248bfSmrg    Xfree(args);
3081ab64890Smrg    return ret;
3091ab64890Smrg}
3101ab64890Smrg
3111ab64890Smrgchar *
3121ab64890SmrgXSetICValues(XIC ic, ...)
3131ab64890Smrg{
3141ab64890Smrg    va_list var;
3151ab64890Smrg    int     total_count;
3161ab64890Smrg    XIMArg *args;
3171ab64890Smrg    char   *ret;
3181ab64890Smrg
3191ab64890Smrg    if (!ic->core.im)
3201ab64890Smrg	return (char *) NULL;
3211ab64890Smrg
3221ab64890Smrg    /*
3231ab64890Smrg     * so count the stuff dangling here
3241ab64890Smrg     */
3251ab64890Smrg    va_start(var, ic);
3261ab64890Smrg    _XIMCountVaList(var, &total_count);
3271ab64890Smrg    va_end(var);
3281ab64890Smrg
3291ab64890Smrg    /*
3301ab64890Smrg     * now package it up so we can send it along
3311ab64890Smrg     */
3321ab64890Smrg    va_start(var, ic);
3331ab64890Smrg    _XIMVaToNestedList(var, total_count, &args);
3341ab64890Smrg    va_end(var);
3351ab64890Smrg
3361ab64890Smrg    ret = (*ic->methods->set_values) (ic, args);
3370f8248bfSmrg    Xfree(args);
3381ab64890Smrg    return ret;
3391ab64890Smrg}
3401ab64890Smrg
3411ab64890Smrg/*
3421ab64890Smrg * Require the input manager to focus the focus window attached to the ic
3431ab64890Smrg * argument.
3441ab64890Smrg */
3451ab64890Smrgvoid
34661b2299dSmrgXSetICFocus(XIC ic)
3471ab64890Smrg{
3481ab64890Smrg  if (ic && ic->core.im)
3491ab64890Smrg      (*ic->methods->set_focus) (ic);
3501ab64890Smrg}
3511ab64890Smrg
3521ab64890Smrg/*
3531ab64890Smrg * Require the input manager to unfocus the focus window attached to the ic
3541ab64890Smrg * argument.
3551ab64890Smrg */
3561ab64890Smrgvoid
35761b2299dSmrgXUnsetICFocus(XIC ic)
3581ab64890Smrg{
3591ab64890Smrg  if (ic->core.im)
3601ab64890Smrg      (*ic->methods->unset_focus) (ic);
3611ab64890Smrg}
3621ab64890Smrg
3631ab64890Smrg/*
3641ab64890Smrg * Return the XIM associated with the input context.
3651ab64890Smrg */
3661ab64890SmrgXIM
36761b2299dSmrgXIMOfIC(XIC ic)
3681ab64890Smrg{
3691ab64890Smrg    return ic->core.im;
3701ab64890Smrg}
3711ab64890Smrg
3721ab64890Smrgchar *
37361b2299dSmrgXmbResetIC(XIC ic)
3741ab64890Smrg{
3751ab64890Smrg    if (ic->core.im)
3761ab64890Smrg	return (*ic->methods->mb_reset)(ic);
3771ab64890Smrg    return (char *)NULL;
3781ab64890Smrg}
3791ab64890Smrg
3801ab64890Smrgwchar_t *
38161b2299dSmrgXwcResetIC(XIC ic)
3821ab64890Smrg{
3831ab64890Smrg    if (ic->core.im)
3841ab64890Smrg	return (*ic->methods->wc_reset)(ic);
3851ab64890Smrg    return (wchar_t *)NULL;
3861ab64890Smrg}
3871ab64890Smrg
3881ab64890Smrgchar *
38961b2299dSmrgXutf8ResetIC(XIC ic)
3901ab64890Smrg{
3911ab64890Smrg    if (ic->core.im) {
3921ab64890Smrg	if (ic->methods->utf8_reset)
3931ab64890Smrg	    return (*ic->methods->utf8_reset)(ic);
3941ab64890Smrg	else if (ic->methods->mb_reset)
3951ab64890Smrg	    return (*ic->methods->mb_reset)(ic);
3961ab64890Smrg    }
3971ab64890Smrg    return (char *)NULL;
3981ab64890Smrg}
3991ab64890Smrg
4001ab64890Smrgint
40161b2299dSmrgXmbLookupString(XIC ic, XKeyEvent *ev, char *buffer, int nbytes,
40261b2299dSmrg		KeySym *keysym, Status *status)
4031ab64890Smrg{
4041ab64890Smrg    if (ic->core.im)
4051ab64890Smrg	return (*ic->methods->mb_lookup_string) (ic, ev, buffer, nbytes,
4061ab64890Smrg						 keysym, status);
4071ab64890Smrg    return XLookupNone;
4081ab64890Smrg}
4091ab64890Smrg
4101ab64890Smrgint
41161b2299dSmrgXwcLookupString(XIC ic, XKeyEvent *ev, wchar_t *buffer, int nchars,
41261b2299dSmrg		KeySym *keysym, Status *status)
4131ab64890Smrg{
4141ab64890Smrg    if (ic->core.im)
4151ab64890Smrg	return (*ic->methods->wc_lookup_string) (ic, ev, buffer, nchars,
4161ab64890Smrg						 keysym, status);
4171ab64890Smrg    return XLookupNone;
4181ab64890Smrg}
4191ab64890Smrg
4201ab64890Smrgint
42161b2299dSmrgXutf8LookupString(XIC ic, XKeyEvent *ev, char *buffer, int nbytes,
42261b2299dSmrg		  KeySym *keysym, Status *status)
4231ab64890Smrg{
4241ab64890Smrg    if (ic->core.im) {
4251ab64890Smrg	if (ic->methods->utf8_lookup_string)
4261ab64890Smrg	    return (*ic->methods->utf8_lookup_string) (ic, ev, buffer, nbytes,
4271ab64890Smrg						   	keysym, status);
4281ab64890Smrg	else if (ic->methods->mb_lookup_string)
4291ab64890Smrg	    return (*ic->methods->mb_lookup_string) (ic, ev, buffer, nbytes,
4301ab64890Smrg						   	keysym, status);
4311ab64890Smrg    }
4321ab64890Smrg    return XLookupNone;
4331ab64890Smrg}
434