1/*
2 * Copyright 1991 by the Open Software Foundation
3 * Copyright 1993, 1994 by the Sony Corporation
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the names of Open Software Foundation and
10 * Sony Corporation not be used in advertising or publicity pertaining to
11 * distribution of the software without specific, written prior permission.
12 * Open Software Foundation and Sony Corporation make no
13 * representations about the suitability of this software for any purpose.
14 * It is provided "as is" without express or implied warranty.
15 *
16 * OPEN SOFTWARE FOUNDATION AND SONY CORPORATION DISCLAIM ALL
17 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL OPEN
19 * SOFTWARE FOUNDATIONN OR SONY CORPORATION BE LIABLE FOR ANY SPECIAL,
20 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
21 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
22 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
24 *
25 *		 M. Collins		OSF
26 *               Makoto Wakamatsu       Sony Corporation
27 */
28/*
29
30Copyright 1991, 1998  The Open Group
31
32Permission to use, copy, modify, distribute, and sell this software and its
33documentation for any purpose is hereby granted without fee, provided that
34the above copyright notice appear in all copies and that both that
35copyright notice and this permission notice appear in supporting
36documentation.
37
38The above copyright notice and this permission notice shall be included
39in all copies or substantial portions of the Software.
40
41THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
42OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
45OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
46ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
47OTHER DEALINGS IN THE SOFTWARE.
48
49Except as contained in this notice, the name of The Open Group shall
50not be used in advertising or otherwise to promote the sale, use or
51other dealings in this Software without prior written authorization
52from The Open Group.
53
54*/
55
56
57#ifdef HAVE_CONFIG_H
58#include <config.h>
59#endif
60#include "Xlibint.h"
61#include "Xlcint.h"
62
63/*
64 * Compile the resource name. (resource_name ---> xrm_name)
65 */
66void
67_XIMCompileResourceList(XIMResourceList res, unsigned int num_res)
68{
69    register unsigned int count;
70
71    for (count = 0; count < num_res; res++, count++) {
72	res->xrm_name = XrmStringToQuark(res->resource_name);
73    }
74}
75
76void
77_XCopyToArg(XPointer src, XPointer *dst, unsigned int size)
78{
79    if (!*dst) {
80	union {
81	    long	longval;
82#ifdef LONG64
83	    int		intval;
84#endif
85	    short	shortval;
86	    char	charval;
87	    char*	charptr;
88	    XPointer	ptr;
89	} u;
90	if (size <= sizeof(XPointer)) {
91	    memcpy((char *)&u, (char *)src, (int)size);
92	    if (size == sizeof(long))	       *dst = (XPointer)u.longval;
93#ifdef LONG64
94	    else if (size == sizeof(int))      *dst = (XPointer)(long)u.intval;
95#endif
96	    else if (size == sizeof(short))    *dst = (XPointer)(long)u.shortval;
97	    else if (size == sizeof(char))     *dst = (XPointer)(long)u.charval;
98	    else if (size == sizeof(char*))    *dst = (XPointer)u.charptr;
99	    else if (size == sizeof(XPointer)) *dst = (XPointer)u.ptr;
100	    else memcpy( (char*)dst, (char*)src, (int)size );
101	} else {
102	    memcpy( (char*)dst, (char*)src, (int)size );
103	}
104    } else {
105	memcpy( (char*)*dst, (char*)src, (int)size );
106    }
107}
108
109/*
110 * Connects to an input method matching current locale specification, creates
111 * a XIM object and return a pointer the newly created XIM back to the caller.
112 */
113
114XIM
115XOpenIM(Display *display, XrmDatabase rdb, char *res_name, char *res_class)
116{
117    XLCd	lcd = _XOpenLC( (char *)NULL );
118
119    if( !lcd )
120	return( (XIM)NULL );
121    return (*lcd->methods->open_im) (lcd, display, rdb, res_name, res_class);
122}
123
124/*
125 * Close the connection to the input manager, and free the XIM structure
126 */
127Status
128XCloseIM(XIM im)
129{
130    Status s;
131    XIC ic;
132    XLCd lcd = im->core.lcd;
133
134    s = (im->methods->close) (im);
135    for (ic = im->core.ic_chain; ic; ic = ic->core.next)
136	ic->core.im = (XIM)NULL;
137    Xfree (im);
138    _XCloseLC (lcd);
139    return (s);
140}
141
142/*
143 * Return the Display associated with the input method.
144 */
145Display *
146XDisplayOfIM(XIM im)
147{
148    return im->core.display;
149}
150
151/*
152 * Return the Locale associated with the input method.
153 */
154char *
155XLocaleOfIM(XIM im)
156{
157    return im->core.lcd->core->name;
158}
159
160/*
161 * Register to a input method instantiation callback to prepare the
162 * on-demand input method instantiation.
163 */
164Bool
165XRegisterIMInstantiateCallback(
166    Display	*display,
167    XrmDatabase	 rdb,
168    char	*res_name,
169    char	*res_class,
170    XIDProc	 callback,
171    XPointer	 client_data)
172{
173    XLCd	lcd = _XOpenLC( (char *)NULL );
174
175    if( !lcd )
176	return( False );
177    return( (*lcd->methods->register_callback)( lcd, display, rdb, res_name,
178						res_class, callback,
179						client_data ) );
180}
181
182/*
183 * Unregister to a input method instantiation callback.
184 */
185Bool
186XUnregisterIMInstantiateCallback(
187    Display	*display,
188    XrmDatabase	 rdb,
189    char	*res_name,
190    char	*res_class,
191    XIDProc	 callback,
192    XPointer	 client_data)
193{
194    XLCd	lcd = _XlcCurrentLC();
195
196    if( !lcd )
197	return( False );
198    if( lcd->methods->unregister_callback == NULL )
199	return( False );
200    return( (*lcd->methods->unregister_callback)( lcd, display, rdb, res_name,
201						  res_class, callback,
202						  client_data ) );
203}
204
205