Initialize.c revision 339a7c43
1444c061aSmrg/***********************************************************
2249c3046SmrgCopyright (c) 1993, Oracle and/or its affiliates. All rights reserved.
31477040fSmrg
41477040fSmrgPermission is hereby granted, free of charge, to any person obtaining a
51477040fSmrgcopy of this software and associated documentation files (the "Software"),
61477040fSmrgto deal in the Software without restriction, including without limitation
71477040fSmrgthe rights to use, copy, modify, merge, publish, distribute, sublicense,
81477040fSmrgand/or sell copies of the Software, and to permit persons to whom the
91477040fSmrgSoftware is furnished to do so, subject to the following conditions:
101477040fSmrg
111477040fSmrgThe above copyright notice and this permission notice (including the next
121477040fSmrgparagraph) shall be included in all copies or substantial portions of the
131477040fSmrgSoftware.
141477040fSmrg
151477040fSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
161477040fSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171477040fSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
181477040fSmrgTHE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
191477040fSmrgLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
201477040fSmrgFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
211477040fSmrgDEALINGS IN THE SOFTWARE.
221477040fSmrg
231477040fSmrgCopyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
24444c061aSmrg
25444c061aSmrg                        All Rights Reserved
26444c061aSmrg
27444c061aSmrgPermission to use, copy, modify, and distribute this software and its
28444c061aSmrgdocumentation for any purpose and without fee is hereby granted,
29444c061aSmrgprovided that the above copyright notice appear in all copies and that
30444c061aSmrgboth that copyright notice and this permission notice appear in
311477040fSmrgsupporting documentation, and that the name of Digital not be
32444c061aSmrgused in advertising or publicity pertaining to distribution of the
33444c061aSmrgsoftware without specific, written prior permission.
34444c061aSmrg
35444c061aSmrgDIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
36444c061aSmrgALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
37444c061aSmrgDIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
38444c061aSmrgANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
39444c061aSmrgWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
40444c061aSmrgARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
41444c061aSmrgSOFTWARE.
42444c061aSmrg
43444c061aSmrg******************************************************************/
44444c061aSmrg
45444c061aSmrg/*
46444c061aSmrg
47444c061aSmrgCopyright 1987, 1988, 1994, 1998  The Open Group
48444c061aSmrg
49444c061aSmrgPermission to use, copy, modify, distribute, and sell this software and its
50444c061aSmrgdocumentation for any purpose is hereby granted without fee, provided that
51444c061aSmrgthe above copyright notice appear in all copies and that both that
52444c061aSmrgcopyright notice and this permission notice appear in supporting
53444c061aSmrgdocumentation.
54444c061aSmrg
55444c061aSmrgThe above copyright notice and this permission notice shall be included in
56444c061aSmrgall copies or substantial portions of the Software.
57444c061aSmrg
58444c061aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59444c061aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60444c061aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
61444c061aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
62444c061aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
63444c061aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
64444c061aSmrg
65444c061aSmrgExcept as contained in this notice, the name of The Open Group shall not be
66444c061aSmrgused in advertising or otherwise to promote the sale, use or other dealings
67444c061aSmrgin this Software without prior written authorization from The Open Group.
68444c061aSmrg
69444c061aSmrg*/
70444c061aSmrg
71444c061aSmrg/* Make sure all wm properties can make it out of the resource manager */
72444c061aSmrg
73444c061aSmrg#ifdef HAVE_CONFIG_H
74444c061aSmrg#include <config.h>
75444c061aSmrg#endif
76444c061aSmrg#include "IntrinsicI.h"
77444c061aSmrg#include "StringDefs.h"
78444c061aSmrg#include "CoreP.h"
79444c061aSmrg#include "ShellP.h"
80444c061aSmrg#include <stdio.h>
81444c061aSmrg#include <X11/Xlocale.h>
82444c061aSmrg#ifdef XTHREADS
83444c061aSmrg#include <X11/Xthreads.h>
84444c061aSmrg#endif
85444c061aSmrg#ifndef WIN32
86444c061aSmrg#define X_INCLUDE_PWD_H
87444c061aSmrg#define XOS_USE_XT_LOCKING
88444c061aSmrg#include <X11/Xos_r.h>
89444c061aSmrg#endif
90444c061aSmrg
91444c061aSmrg#include <stdlib.h>
92444c061aSmrg
93444c061aSmrg#if (defined(SUNSHLIB) || defined(AIXSHLIB)) && defined(SHAREDCODE)
94444c061aSmrg/*
95444c061aSmrg * If used as a shared library, generate code under a different name so that
96444c061aSmrg * the stub routines in sharedlib.c get loaded into the application binary.
97444c061aSmrg */
98444c061aSmrg#define XtToolkitInitialize _XtToolkitInitialize
99444c061aSmrg#define XtOpenApplication _XtOpenApplication
100444c061aSmrg#define XtAppInitialize _XtAppInitialize
101444c061aSmrg#define XtInitialize _XtInitialize
102444c061aSmrg#endif /* (SUNSHLIB || AIXSHLIB) && SHAREDCODE */
103444c061aSmrg
104444c061aSmrg/*
105444c061aSmrg * hpux
106444c061aSmrg * Hand-patched versions of HP-UX prior to version 7.0 can usefully add
107444c061aSmrg * -DUSE_UNAME in the appropriate config file to get long hostnames.
108444c061aSmrg */
109444c061aSmrg
110444c061aSmrg#ifdef USG
111444c061aSmrg#define USE_UNAME
112444c061aSmrg#endif
113444c061aSmrg
114444c061aSmrg#ifdef USE_UNAME
115444c061aSmrg#include <sys/utsname.h>
116444c061aSmrg#endif
117444c061aSmrg
118444c061aSmrg/* some unspecified magic number of expected search levels for Xrm */
119444c061aSmrg#define SEARCH_LIST_SIZE 1000
120444c061aSmrg
121444c061aSmrg/*
122444c061aSmrg This is a set of default records describing the command line arguments that
123444c061aSmrg Xlib will parse and set into the resource data base.
124444c061aSmrg
125444c061aSmrg This list is applied before the users list to enforce these defaults.  This is
126444c061aSmrg policy, which the toolkit avoids but I hate differing programs at this level.
127444c061aSmrg*/
128444c061aSmrg
129444c061aSmrgstatic XrmOptionDescRec const opTable[] = {
130444c061aSmrg{"+rv",		"*reverseVideo", XrmoptionNoArg,	(XtPointer) "off"},
131444c061aSmrg{"+synchronous","*synchronous",	XrmoptionNoArg,		(XtPointer) "off"},
132444c061aSmrg{"-background",	"*background",	XrmoptionSepArg,	(XtPointer) NULL},
133444c061aSmrg{"-bd",		"*borderColor",	XrmoptionSepArg,	(XtPointer) NULL},
134444c061aSmrg{"-bg",		"*background",	XrmoptionSepArg,	(XtPointer) NULL},
135444c061aSmrg{"-bordercolor","*borderColor",	XrmoptionSepArg,	(XtPointer) NULL},
136444c061aSmrg{"-borderwidth",".borderWidth",	XrmoptionSepArg,	(XtPointer) NULL},
137444c061aSmrg{"-bw",		".borderWidth",	XrmoptionSepArg,	(XtPointer) NULL},
138444c061aSmrg{"-display",	".display",     XrmoptionSepArg,	(XtPointer) NULL},
139444c061aSmrg{"-fg",		"*foreground",	XrmoptionSepArg,	(XtPointer) NULL},
140444c061aSmrg{"-fn",		"*font",	XrmoptionSepArg,	(XtPointer) NULL},
141444c061aSmrg{"-font",	"*font",	XrmoptionSepArg,	(XtPointer) NULL},
142444c061aSmrg{"-foreground",	"*foreground",	XrmoptionSepArg,	(XtPointer) NULL},
143444c061aSmrg{"-geometry",	".geometry",	XrmoptionSepArg,	(XtPointer) NULL},
144444c061aSmrg{"-iconic",	".iconic",	XrmoptionNoArg,		(XtPointer) "on"},
145444c061aSmrg{"-name",	".name",	XrmoptionSepArg,	(XtPointer) NULL},
146444c061aSmrg{"-reverse",	"*reverseVideo", XrmoptionNoArg,	(XtPointer) "on"},
147444c061aSmrg{"-rv",		"*reverseVideo", XrmoptionNoArg,	(XtPointer) "on"},
148444c061aSmrg{"-selectionTimeout",
149444c061aSmrg		".selectionTimeout", XrmoptionSepArg,	(XtPointer) NULL},
150444c061aSmrg{"-synchronous","*synchronous",	XrmoptionNoArg,		(XtPointer) "on"},
151444c061aSmrg{"-title",	".title",	XrmoptionSepArg,	(XtPointer) NULL},
152444c061aSmrg{"-xnllanguage",".xnlLanguage",	XrmoptionSepArg,	(XtPointer) NULL},
153444c061aSmrg{"-xrm",	NULL,		XrmoptionResArg,	(XtPointer) NULL},
154444c061aSmrg{"-xtsessionID",".sessionID",	XrmoptionSepArg,	(XtPointer) NULL},
155444c061aSmrg};
156444c061aSmrg
157444c061aSmrg
158444c061aSmrg/*
159444c061aSmrg * GetHostname - emulates gethostname() on non-bsd systems.
160444c061aSmrg */
161444c061aSmrg
162444c061aSmrgstatic void GetHostname (
163444c061aSmrg    char *buf,
164444c061aSmrg    int maxlen)
165444c061aSmrg{
166444c061aSmrg#ifdef USE_UNAME
167444c061aSmrg    int len;
168444c061aSmrg    struct utsname name;
169444c061aSmrg
170444c061aSmrg    if (maxlen <= 0 || buf == NULL)
171444c061aSmrg	return;
172444c061aSmrg
173444c061aSmrg    uname (&name);
174444c061aSmrg    len = strlen (name.nodename);
175444c061aSmrg    if (len >= maxlen) len = maxlen;
176444c061aSmrg    (void) strncpy (buf, name.nodename, len-1);
177444c061aSmrg    buf[len-1] = '\0';
178444c061aSmrg#else
179444c061aSmrg    if (maxlen <= 0 || buf == NULL)
180444c061aSmrg	return;
181444c061aSmrg
182444c061aSmrg    buf[0] = '\0';
183444c061aSmrg    (void) gethostname (buf, maxlen);
184444c061aSmrg    buf [maxlen - 1] = '\0';
185444c061aSmrg#endif
186444c061aSmrg}
187444c061aSmrg
188444c061aSmrg
189444c061aSmrg#ifdef SUNSHLIB
190444c061aSmrgvoid _XtInherit(void)
191444c061aSmrg{
192444c061aSmrg    extern void __XtInherit();
193444c061aSmrg    __XtInherit();
194444c061aSmrg}
195444c061aSmrg#define _XtInherit __XtInherit
196444c061aSmrg#endif
197444c061aSmrg
198444c061aSmrg
1992265a131Smrg#if defined (WIN32) || defined(__CYGWIN__)
200444c061aSmrg/*
201444c061aSmrg * The Symbol _XtInherit is used in two different manners.
202444c061aSmrg * First it could be used as a generic function and second
203444c061aSmrg * as an absolute address reference, which will be used to
204444c061aSmrg * check the initialisation process of several other libraries.
205444c061aSmrg * Because of this the symbol must be accessable by all
206444c061aSmrg * client dll's and applications.  In unix environments
207444c061aSmrg * this is no problem, because the used shared libraries
208444c061aSmrg * format (elf) supports this immediatly.  Under Windows
209444c061aSmrg * this isn't true, because a functions address in a dll
210444c061aSmrg * is different from the same function in another dll or
211444c061aSmrg * applications, because the used Portable Executable
212444c061aSmrg * File adds a code stub to each client to provide the
213444c061aSmrg * exported symbol name.  This stub uses an indirect
214444c061aSmrg * pointer to get the original symbol address, which is
215444c061aSmrg * then jumped to, like in this example:
216444c061aSmrg *
217444c061aSmrg * --- client ---                                     --- dll ----
218444c061aSmrg *  ...
219444c061aSmrg *  call foo
220444c061aSmrg *
221444c061aSmrg * foo: jmp (*_imp_foo)               ---->           foo: ....
222444c061aSmrg *      nop
223444c061aSmrg *      nop
224444c061aSmrg *
225444c061aSmrg * _imp_foo: .long <index of foo in dll export table, is
226444c061aSmrg *		    set to the real address by the runtime linker>
227444c061aSmrg *
228444c061aSmrg * Now it is clear why the clients symbol foo isn't the same
229444c061aSmrg * as in the dll and we can think about how to deal which
230444c061aSmrg * this two above mentioned requirements, to export this
231444c061aSmrg * symbol to all clients and to allow calling this symbol
232444c061aSmrg * as a function.  The solution I've used exports the
233444c061aSmrg * symbol _XtInherit as data symbol, because global data
234444c061aSmrg * symbols are exported to all clients.  But how to deal
235444c061aSmrg * with the second requirement, that this symbol should
236444c061aSmrg * be used as function.  The Trick is to build a little
237444c061aSmrg * code stub in the data section in the exact manner as
238444c061aSmrg * above explained.  This is done with the assembler code
239444c061aSmrg * below.
240444c061aSmrg *
241444c061aSmrg * Ralf Habacker
242444c061aSmrg *
243444c061aSmrg * References:
244444c061aSmrg * msdn          http://msdn.microsoft.com/msdnmag/issues/02/02/PE/PE.asp
245444c061aSmrg * cygwin-xfree: http://www.cygwin.com/ml/cygwin-xfree/2003-10/msg00000.html
246444c061aSmrg */
247444c061aSmrg
248339a7c43Smrg#ifdef __x86_64__
249339a7c43Smrgasm (".section .trampoline, \"dwx\" \n\
250339a7c43Smrg .globl _XtInherit        \n\
251339a7c43Smrg _XtInherit:              \n\
252339a7c43Smrg    jmp *_y(%rip)         \n\
253339a7c43Smrg_y: .quad __XtInherit     \n\
254339a7c43Smrg    .text                 \n");
255339a7c43Smrg#else
256444c061aSmrgasm (".data\n\
257444c061aSmrg .globl __XtInherit        \n\
258444c061aSmrg __XtInherit:      jmp *_y \n\
259444c061aSmrg  _y: .long ___XtInherit   \n\
260444c061aSmrg    .text                 \n");
261339a7c43Smrg#endif
262444c061aSmrg
263444c061aSmrg#define _XtInherit __XtInherit
264444c061aSmrg#endif
265444c061aSmrg
266444c061aSmrg
267444c061aSmrgvoid _XtInherit(void)
268444c061aSmrg{
269444c061aSmrg    XtErrorMsg("invalidProcedure","inheritanceProc",XtCXtToolkitError,
270444c061aSmrg            "Unresolved inheritance operation",
271444c061aSmrg              (String *)NULL, (Cardinal *)NULL);
272444c061aSmrg}
273444c061aSmrg
274444c061aSmrg
275444c061aSmrgvoid XtToolkitInitialize(void)
276444c061aSmrg{
277444c061aSmrg    static Boolean initialized = False;
278444c061aSmrg
279444c061aSmrg    LOCK_PROCESS;
280444c061aSmrg    if (initialized) {
281444c061aSmrg	UNLOCK_PROCESS;
282444c061aSmrg	return;
283444c061aSmrg    }
284444c061aSmrg    initialized = True;
285444c061aSmrg    UNLOCK_PROCESS;
286444c061aSmrg    /* Resource management initialization */
287444c061aSmrg    XrmInitialize();
288444c061aSmrg    _XtResourceListInitialize();
289444c061aSmrg
290444c061aSmrg    /* Other intrinsic intialization */
291444c061aSmrg    _XtConvertInitialize();
292444c061aSmrg    _XtEventInitialize();
293444c061aSmrg    _XtTranslateInitialize();
294444c061aSmrg
295444c061aSmrg    /* Some apps rely on old (broken) XtAppPeekEvent behavior */
296444c061aSmrg    if(getenv("XTAPPPEEKEVENT_SKIPTIMER"))
297444c061aSmrg	XtAppPeekEvent_SkipTimer = True;
298444c061aSmrg    else
299444c061aSmrg	XtAppPeekEvent_SkipTimer = False;
300444c061aSmrg}
301444c061aSmrg
302444c061aSmrg
303444c061aSmrgString _XtGetUserName(
304444c061aSmrg    String dest,
305444c061aSmrg    int len)
306444c061aSmrg{
307444c061aSmrg#ifdef WIN32
308444c061aSmrg    String ptr = NULL;
309444c061aSmrg
310444c061aSmrg    if ((ptr = getenv("USERNAME"))) {
311444c061aSmrg	(void) strncpy (dest, ptr, len-1);
312444c061aSmrg	dest[len-1] = '\0';
313444c061aSmrg    } else
314444c061aSmrg	*dest = '\0';
315444c061aSmrg#else
316444c061aSmrg#ifdef X_NEEDS_PWPARAMS
317444c061aSmrg    _Xgetpwparams pwparams;
318444c061aSmrg#endif
319444c061aSmrg    struct passwd *pw;
320444c061aSmrg    char* ptr;
321444c061aSmrg
322444c061aSmrg    if ((ptr = getenv("USER"))) {
323444c061aSmrg	(void) strncpy (dest, ptr, len-1);
324444c061aSmrg	dest[len-1] = '\0';
325444c061aSmrg    } else {
326444c061aSmrg	if ((pw = _XGetpwuid(getuid(),pwparams)) != NULL) {
327444c061aSmrg	    (void) strncpy (dest, pw->pw_name, len-1);
328444c061aSmrg	    dest[len-1] = '\0';
329444c061aSmrg	} else
330444c061aSmrg	    *dest = '\0';
331444c061aSmrg    }
332444c061aSmrg#endif
333444c061aSmrg    return dest;
334444c061aSmrg}
335444c061aSmrg
336444c061aSmrg
337444c061aSmrgstatic String GetRootDirName(
338444c061aSmrg    String dest,
339444c061aSmrg    int len)
340444c061aSmrg{
341444c061aSmrg#ifdef WIN32
342444c061aSmrg    register char *ptr1;
343444c061aSmrg    register char *ptr2 = NULL;
344444c061aSmrg    int len1 = 0, len2 = 0;
345444c061aSmrg
346444c061aSmrg    if (ptr1 = getenv("HOME")) {	/* old, deprecated */
347444c061aSmrg	len1 = strlen (ptr1);
348444c061aSmrg    } else if ((ptr1 = getenv("HOMEDRIVE")) && (ptr2 = getenv("HOMEDIR"))) {
349444c061aSmrg	len1 = strlen (ptr1);
350444c061aSmrg	len2 = strlen (ptr2);
351444c061aSmrg    } else if (ptr2 = getenv("USERNAME")) {
352444c061aSmrg	len1 = strlen (ptr1 = "/users/");
353444c061aSmrg	len2 = strlen (ptr2);
354444c061aSmrg    }
355444c061aSmrg    if ((len1 + len2 + 1) < len)
356444c061aSmrg	sprintf (dest, "%s%s", ptr1, (ptr2) ? ptr2 : "");
357444c061aSmrg    else
358444c061aSmrg	*dest = '\0';
359444c061aSmrg#else
360444c061aSmrg#ifdef X_NEEDS_PWPARAMS
361444c061aSmrg    _Xgetpwparams pwparams;
362444c061aSmrg#endif
363444c061aSmrg    struct passwd *pw;
364444c061aSmrg    static char *ptr;
365444c061aSmrg
366444c061aSmrg    if (len <= 0 || dest == NULL)
367444c061aSmrg	return NULL;
368444c061aSmrg
369444c061aSmrg    if ((ptr = getenv("HOME"))) {
370444c061aSmrg	(void) strncpy (dest, ptr, len-1);
371444c061aSmrg	dest[len-1] = '\0';
372444c061aSmrg    } else {
373444c061aSmrg	if ((ptr = getenv("USER")))
374444c061aSmrg	    pw = _XGetpwnam(ptr,pwparams);
375444c061aSmrg	else
376444c061aSmrg 	    pw = _XGetpwuid(getuid(),pwparams);
377444c061aSmrg	if (pw != NULL) {
378444c061aSmrg	    (void) strncpy (dest, pw->pw_dir, len-1);
379444c061aSmrg	    dest[len-1] = '\0';
380444c061aSmrg	} else
381444c061aSmrg	    *dest = '\0';
382444c061aSmrg    }
383444c061aSmrg#endif
384444c061aSmrg    return dest;
385444c061aSmrg}
386444c061aSmrg
387444c061aSmrgstatic void CombineAppUserDefaults(
388444c061aSmrg    Display *dpy,
389444c061aSmrg    XrmDatabase *pdb)
390444c061aSmrg{
391444c061aSmrg    char* filename;
392bdf0f55dSmrg    char* path = NULL;
393444c061aSmrg    Boolean deallocate = False;
394444c061aSmrg
395444c061aSmrg    if (!(path = getenv("XUSERFILESEARCHPATH"))) {
3962265a131Smrg#if !defined(WIN32) || !defined(__MINGW32__)
397444c061aSmrg	char *old_path;
398444c061aSmrg	char homedir[PATH_MAX];
399444c061aSmrg	GetRootDirName(homedir, PATH_MAX);
400444c061aSmrg	if (!(old_path = getenv("XAPPLRESDIR"))) {
401bdf0f55dSmrg	    XtAsprintf(&path,
402bdf0f55dSmrg		       "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N",
403bdf0f55dSmrg		       homedir, homedir, homedir, homedir, homedir, homedir);
404444c061aSmrg	} else {
405bdf0f55dSmrg	    XtAsprintf(&path,
406bdf0f55dSmrg		       "%s/%%L/%%N%%C:%s/%%l/%%N%%C:%s/%%N%%C:%s/%%N%%C:%s/%%L/%%N:%s/%%l/%%N:%s/%%N:%s/%%N",
407bdf0f55dSmrg		       old_path, old_path, old_path, homedir,
408bdf0f55dSmrg		       old_path, old_path, old_path, homedir);
409444c061aSmrg	}
410444c061aSmrg	deallocate = True;
4112265a131Smrg#endif
412444c061aSmrg    }
413444c061aSmrg
414444c061aSmrg    filename = XtResolvePathname(dpy, NULL, NULL, NULL, path, NULL, 0, NULL);
415444c061aSmrg    if (filename) {
416444c061aSmrg	(void)XrmCombineFileDatabase(filename, pdb, False);
417444c061aSmrg	XtFree(filename);
418444c061aSmrg    }
419444c061aSmrg
420bdf0f55dSmrg    if (deallocate)
421bdf0f55dSmrg	XtFree(path);
422444c061aSmrg}
423444c061aSmrg
424444c061aSmrgstatic void CombineUserDefaults(
425444c061aSmrg    Display *dpy,
426444c061aSmrg    XrmDatabase *pdb)
427444c061aSmrg{
4282265a131Smrg#ifdef __MINGW32__
4299e7bcd65Smrg    const char *slashDotXdefaults = "/Xdefaults";
4302265a131Smrg#else
4319e7bcd65Smrg    const char *slashDotXdefaults = "/.Xdefaults";
4322265a131Smrg#endif
433444c061aSmrg    char *dpy_defaults = XResourceManagerString(dpy);
434444c061aSmrg
435444c061aSmrg    if (dpy_defaults) {
436444c061aSmrg	XrmCombineDatabase(XrmGetStringDatabase(dpy_defaults), pdb, False);
437444c061aSmrg    } else {
438444c061aSmrg	char filename[PATH_MAX];
439444c061aSmrg	(void) GetRootDirName(filename,
440444c061aSmrg			PATH_MAX - strlen (slashDotXdefaults) - 1);
441444c061aSmrg	(void) strcat(filename, slashDotXdefaults);
442444c061aSmrg	(void)XrmCombineFileDatabase(filename, pdb, False);
443444c061aSmrg    }
444444c061aSmrg}
445444c061aSmrg
446444c061aSmrg/*ARGSUSED*/
447444c061aSmrgstatic Bool StoreDBEntry(
448444c061aSmrg    XrmDatabase		*db,
449444c061aSmrg    XrmBindingList      bindings,
450444c061aSmrg    XrmQuarkList	quarks,
451444c061aSmrg    XrmRepresentation   *type,
452444c061aSmrg    XrmValuePtr		value,
453444c061aSmrg    XPointer		data)
454444c061aSmrg{
455444c061aSmrg    XrmQPutResource((XrmDatabase *)data, bindings, quarks, *type, value);
456444c061aSmrg    return False;
457444c061aSmrg}
458444c061aSmrg
459444c061aSmrgstatic XrmDatabase CopyDB(XrmDatabase db)
460444c061aSmrg{
461444c061aSmrg    XrmDatabase copy = NULL;
462444c061aSmrg    XrmQuark empty = NULLQUARK;
463444c061aSmrg
464444c061aSmrg    XrmEnumerateDatabase(db, &empty, &empty, XrmEnumAllLevels,
465444c061aSmrg			 StoreDBEntry, (XPointer)&copy);
466444c061aSmrg    return copy;
467444c061aSmrg}
468444c061aSmrg
469444c061aSmrg/*ARGSUSED*/
470444c061aSmrgstatic String _XtDefaultLanguageProc(
471444c061aSmrg    Display   *dpy,	/* unused */
472444c061aSmrg    String     xnl,
473444c061aSmrg    XtPointer  closure)	/* unused */
474444c061aSmrg{
475444c061aSmrg    if (! setlocale(LC_ALL, xnl))
476444c061aSmrg	XtWarning("locale not supported by C library, locale unchanged");
477444c061aSmrg
478444c061aSmrg    if (! XSupportsLocale()) {
479444c061aSmrg	XtWarning("locale not supported by Xlib, locale set to C");
480444c061aSmrg	setlocale(LC_ALL, "C");
481444c061aSmrg    }
482444c061aSmrg    if (! XSetLocaleModifiers(""))
483444c061aSmrg	XtWarning("X locale modifiers not supported, using default");
484444c061aSmrg
485444c061aSmrg    return setlocale(LC_ALL, NULL); /* re-query in case overwritten */
486444c061aSmrg}
487444c061aSmrg
488444c061aSmrgXtLanguageProc XtSetLanguageProc(
489444c061aSmrg    XtAppContext      app,
490444c061aSmrg    XtLanguageProc    proc,
491444c061aSmrg    XtPointer         closure)
492444c061aSmrg{
493444c061aSmrg    XtLanguageProc    old;
494444c061aSmrg
495444c061aSmrg    if (!proc) {
496444c061aSmrg	proc = _XtDefaultLanguageProc;
497444c061aSmrg	closure = NULL;
498444c061aSmrg    }
499444c061aSmrg
500444c061aSmrg    if (app) {
501444c061aSmrg	LOCK_APP(app);
502444c061aSmrg	LOCK_PROCESS;
503444c061aSmrg	/* set langProcRec only for this application context */
504444c061aSmrg        old = app->langProcRec.proc;
505444c061aSmrg        app->langProcRec.proc = proc;
506444c061aSmrg        app->langProcRec.closure = closure;
507444c061aSmrg	UNLOCK_PROCESS;
508444c061aSmrg	UNLOCK_APP(app);
509444c061aSmrg    } else {
510444c061aSmrg	/* set langProcRec for all application contexts */
511444c061aSmrg        ProcessContext process;
512444c061aSmrg
513444c061aSmrg	LOCK_PROCESS;
514444c061aSmrg        process = _XtGetProcessContext();
515444c061aSmrg        old = process->globalLangProcRec.proc;
516444c061aSmrg	process->globalLangProcRec.proc = proc;
517444c061aSmrg	process->globalLangProcRec.closure = closure;
518444c061aSmrg        app = process->appContextList;
519444c061aSmrg        while (app) {
520444c061aSmrg            app->langProcRec.proc = proc;
521444c061aSmrg            app->langProcRec.closure = closure;
522444c061aSmrg	    app = app->next;
523444c061aSmrg        }
524444c061aSmrg	UNLOCK_PROCESS;
525444c061aSmrg    }
526444c061aSmrg    return (old ? old : _XtDefaultLanguageProc);
527444c061aSmrg}
528444c061aSmrg
529444c061aSmrgXrmDatabase XtScreenDatabase(
530444c061aSmrg    Screen *screen)
531444c061aSmrg{
532444c061aSmrg    int scrno;
533444c061aSmrg    Bool doing_def;
534444c061aSmrg    XrmDatabase db, olddb;
535444c061aSmrg    XtPerDisplay pd;
536444c061aSmrg    Status do_fallback;
537444c061aSmrg    char *scr_resources;
538444c061aSmrg    Display *dpy = DisplayOfScreen(screen);
539444c061aSmrg    DPY_TO_APPCON(dpy);
540444c061aSmrg
541444c061aSmrg    LOCK_APP(app);
542444c061aSmrg    LOCK_PROCESS;
543444c061aSmrg    if (screen == DefaultScreenOfDisplay(dpy)) {
544444c061aSmrg	scrno = DefaultScreen(dpy);
545444c061aSmrg	doing_def = True;
546444c061aSmrg    } else {
547444c061aSmrg	scrno = XScreenNumberOfScreen(screen);
548444c061aSmrg	doing_def = False;
549444c061aSmrg    }
550444c061aSmrg    pd = _XtGetPerDisplay(dpy);
551444c061aSmrg    if ((db = pd->per_screen_db[scrno])) {
552444c061aSmrg	UNLOCK_PROCESS;
553444c061aSmrg	UNLOCK_APP(app);
554444c061aSmrg	return (doing_def ? XrmGetDatabase(dpy) : db);
555444c061aSmrg    }
556444c061aSmrg    scr_resources = XScreenResourceString(screen);
557444c061aSmrg
558444c061aSmrg    if (ScreenCount(dpy) == 1) {
559444c061aSmrg	db = pd->cmd_db;
560444c061aSmrg	pd->cmd_db = NULL;
561444c061aSmrg    } else {
562444c061aSmrg	db = CopyDB(pd->cmd_db);
563444c061aSmrg    }
564444c061aSmrg    {   /* Environment defaults */
565444c061aSmrg	char	filenamebuf[PATH_MAX];
566444c061aSmrg	char	*filename;
567444c061aSmrg
568444c061aSmrg	if (!(filename = getenv("XENVIRONMENT"))) {
569444c061aSmrg	    int len;
5702265a131Smrg#ifdef __MINGW32__
5719e7bcd65Smrg	    const char *slashDotXdefaultsDash = "/Xdefaults-";
5722265a131Smrg#else
5739e7bcd65Smrg	    const char *slashDotXdefaultsDash = "/.Xdefaults-";
5742265a131Smrg#endif
575444c061aSmrg
576444c061aSmrg	    (void) GetRootDirName(filename = filenamebuf,
577444c061aSmrg			PATH_MAX - strlen (slashDotXdefaultsDash) - 1);
578444c061aSmrg	    (void) strcat(filename, slashDotXdefaultsDash);
579444c061aSmrg	    len = strlen(filename);
580444c061aSmrg	    GetHostname (filename+len, PATH_MAX-len);
581444c061aSmrg	}
582444c061aSmrg	(void)XrmCombineFileDatabase(filename, &db, False);
583444c061aSmrg    }
584444c061aSmrg    if (scr_resources)
585444c061aSmrg    {   /* Screen defaults */
586444c061aSmrg	XrmCombineDatabase(XrmGetStringDatabase(scr_resources), &db, False);
587444c061aSmrg	XFree(scr_resources);
588444c061aSmrg    }
589444c061aSmrg    /* Server or host defaults */
590444c061aSmrg    if (!pd->server_db)
591444c061aSmrg	CombineUserDefaults(dpy, &db);
592444c061aSmrg    else {
593444c061aSmrg	(void) XrmCombineDatabase(pd->server_db, &db, False);
594444c061aSmrg	pd->server_db = NULL;
595444c061aSmrg    }
596444c061aSmrg
597444c061aSmrg    if (!db)
598444c061aSmrg	db = XrmGetStringDatabase("");
599444c061aSmrg    pd->per_screen_db[scrno] = db;
600444c061aSmrg    olddb = XrmGetDatabase(dpy);
601444c061aSmrg    /* set database now, for XtResolvePathname to use */
602444c061aSmrg    XrmSetDatabase(dpy, db);
603444c061aSmrg    CombineAppUserDefaults(dpy, &db);
604444c061aSmrg    do_fallback = 1;
605444c061aSmrg    {   /* System app-defaults */
606444c061aSmrg	char	*filename;
607444c061aSmrg
608444c061aSmrg	if ((filename = XtResolvePathname(dpy, "app-defaults",
609444c061aSmrg					 NULL, NULL, NULL, NULL, 0, NULL))) {
610444c061aSmrg	    do_fallback = !XrmCombineFileDatabase(filename, &db, False);
611444c061aSmrg	    XtFree(filename);
612444c061aSmrg	}
613444c061aSmrg    }
614444c061aSmrg    /* now restore old database, if need be */
615444c061aSmrg    if (!doing_def)
616444c061aSmrg	XrmSetDatabase(dpy, olddb);
617444c061aSmrg    if (do_fallback && pd->appContext->fallback_resources)
618444c061aSmrg    {   /* Fallback defaults */
619444c061aSmrg        XrmDatabase fdb = NULL;
620444c061aSmrg	String *res;
621444c061aSmrg
622444c061aSmrg	for (res = pd->appContext->fallback_resources; *res; res++)
623444c061aSmrg	    XrmPutLineResource(&fdb, *res);
624444c061aSmrg	(void)XrmCombineDatabase(fdb, &db, False);
625444c061aSmrg    }
626444c061aSmrg    UNLOCK_PROCESS;
627444c061aSmrg    UNLOCK_APP(app);
628444c061aSmrg    return db;
629444c061aSmrg}
630444c061aSmrg
631444c061aSmrg/*
632444c061aSmrg * Merge two option tables, allowing the second to over-ride the first,
633444c061aSmrg * so that ambiguous abbreviations can be noticed.  The merge attempts
634444c061aSmrg * to make the resulting table lexicographically sorted, but succeeds
635444c061aSmrg * only if the first source table is sorted.  Though it _is_ recommended
636444c061aSmrg * (for optimizations later in XrmParseCommand), it is not required
637444c061aSmrg * that either source table be sorted.
638444c061aSmrg *
639444c061aSmrg * Caller is responsible for freeing the returned option table.
640444c061aSmrg */
641444c061aSmrg
642444c061aSmrgstatic void _MergeOptionTables(
643444c061aSmrg    const XrmOptionDescRec *src1,
644444c061aSmrg    Cardinal num_src1,
645444c061aSmrg    const XrmOptionDescRec *src2,
646444c061aSmrg    Cardinal num_src2,
647444c061aSmrg    XrmOptionDescRec **dst,
648444c061aSmrg    Cardinal *num_dst)
649444c061aSmrg{
650444c061aSmrg    XrmOptionDescRec *table, *endP;
651444c061aSmrg    register XrmOptionDescRec *opt1, *whereP, *dstP;
652444c061aSmrg    register const XrmOptionDescRec *opt2;
653444c061aSmrg    int i1;
654444c061aSmrg    Cardinal i2;
655444c061aSmrg    int dst_len, order;
656444c061aSmrg    Boolean found;
657444c061aSmrg    enum {Check, NotSorted, IsSorted} sort_order = Check;
658444c061aSmrg
659444c061aSmrg    *dst = table = (XrmOptionDescRec*)
660444c061aSmrg	__XtMalloc( sizeof(XrmOptionDescRec) * (num_src1 + num_src2) );
661444c061aSmrg
662444c061aSmrg    (void) memmove(table, src1, sizeof(XrmOptionDescRec) * num_src1 );
663444c061aSmrg    if (num_src2 == 0) {
664444c061aSmrg	*num_dst = num_src1;
665444c061aSmrg	return;
666444c061aSmrg    }
667444c061aSmrg    endP = &table[dst_len = num_src1];
668444c061aSmrg    for (opt2 = src2, i2= 0; i2 < num_src2; opt2++, i2++) {
669444c061aSmrg	found = False;
670444c061aSmrg	whereP = endP-1;	/* assume new option goes at the end */
671444c061aSmrg	for (opt1 = table, i1 = 0; i1 < dst_len; opt1++, i1++) {
672444c061aSmrg	    /* have to walk the entire new table so new list is ordered
673444c061aSmrg	       (if src1 was ordered) */
674444c061aSmrg	    if (sort_order == Check && i1 > 0
675444c061aSmrg		&& strcmp(opt1->option, (opt1-1)->option) < 0)
676444c061aSmrg		sort_order = NotSorted;
677444c061aSmrg	    if ((order = strcmp(opt1->option, opt2->option)) == 0) {
678444c061aSmrg		/* same option names; just overwrite opt1 with opt2 */
679444c061aSmrg		*opt1 = *opt2;
680444c061aSmrg		found = True;
681444c061aSmrg		break;
682444c061aSmrg		}
683444c061aSmrg	    /* else */
684444c061aSmrg	    if (sort_order == IsSorted && order > 0) {
685444c061aSmrg		/* insert before opt1 to preserve order */
686444c061aSmrg		/* shift rest of table forward to make room for new entry */
687444c061aSmrg		for (dstP = endP++; dstP > opt1; dstP--)
688444c061aSmrg		    *dstP = *(dstP-1);
689444c061aSmrg		*opt1 = *opt2;
690444c061aSmrg		dst_len++;
691444c061aSmrg		found = True;
692444c061aSmrg		break;
693444c061aSmrg	    }
694444c061aSmrg	    /* else */
695444c061aSmrg	    if (order < 0)
696444c061aSmrg		/* opt2 sorts after opt1, so remember this position */
697444c061aSmrg		whereP = opt1;
698444c061aSmrg	}
699444c061aSmrg	if (sort_order == Check && i1 == dst_len)
700444c061aSmrg	    sort_order = IsSorted;
701444c061aSmrg	if (!found) {
702444c061aSmrg	   /* when we get here, whereP points to the last entry in the
703444c061aSmrg	      destination that sorts before "opt2".  Shift rest of table
704444c061aSmrg	      forward and insert "opt2" after whereP. */
705444c061aSmrg	    whereP++;
706444c061aSmrg	    for (dstP = endP++; dstP > whereP; dstP--)
707444c061aSmrg		*dstP = *(dstP-1);
708444c061aSmrg	    *whereP = *opt2;
709444c061aSmrg	    dst_len++;
710444c061aSmrg	}
711444c061aSmrg    }
712444c061aSmrg    *num_dst = dst_len;
713444c061aSmrg}
714444c061aSmrg
715444c061aSmrg
716444c061aSmrg/* NOTE: name, class, and type must be permanent strings */
717444c061aSmrgstatic Boolean _GetResource(
718444c061aSmrg    Display *dpy,
719444c061aSmrg    XrmSearchList list,
720444c061aSmrg    String name,
721444c061aSmrg    String class,
722444c061aSmrg    String type,
723444c061aSmrg    XrmValue* value)
724444c061aSmrg{
725444c061aSmrg    XrmRepresentation db_type;
726444c061aSmrg    XrmValue db_value;
727444c061aSmrg    XrmName Qname = XrmPermStringToQuark(name);
728444c061aSmrg    XrmClass Qclass = XrmPermStringToQuark(class);
729444c061aSmrg    XrmRepresentation Qtype = XrmPermStringToQuark(type);
730444c061aSmrg
731444c061aSmrg    if (XrmQGetSearchResource(list, Qname, Qclass, &db_type, &db_value)) {
732444c061aSmrg	if (db_type == Qtype) {
733444c061aSmrg	    if (Qtype == _XtQString)
734444c061aSmrg		*(String*)value->addr = db_value.addr;
735444c061aSmrg	    else
736444c061aSmrg		(void) memmove(value->addr, db_value.addr, value->size );
737444c061aSmrg	    return True;
738444c061aSmrg	} else {
739444c061aSmrg	    WidgetRec widget; /* hack, hack */
740444c061aSmrg	    bzero( &widget, sizeof(widget) );
741444c061aSmrg	    widget.core.self = &widget;
742444c061aSmrg	    widget.core.widget_class = coreWidgetClass;
743444c061aSmrg	    widget.core.screen = (Screen*)DefaultScreenOfDisplay(dpy);
744444c061aSmrg	    XtInitializeWidgetClass(coreWidgetClass);
745444c061aSmrg	    if (_XtConvert(&widget,db_type,&db_value,Qtype,value,NULL)) {
746444c061aSmrg		return True;
747444c061aSmrg	    }
748444c061aSmrg	}
749444c061aSmrg    }
750444c061aSmrg    return False;
751444c061aSmrg}
752444c061aSmrg
753444c061aSmrgXrmDatabase _XtPreparseCommandLine(
754444c061aSmrg    XrmOptionDescRec *urlist,
755444c061aSmrg    Cardinal num_urs,
756444c061aSmrg    int argc,
757444c061aSmrg    String *argv,
758444c061aSmrg    /* return */
759444c061aSmrg    String *applName,
760444c061aSmrg    String *displayName,
761444c061aSmrg    String *language)
762444c061aSmrg{
7632265a131Smrg    XrmDatabase db = NULL;
764444c061aSmrg    XrmOptionDescRec *options;
765444c061aSmrg    Cardinal num_options;
766444c061aSmrg    XrmName name_list[3];
767444c061aSmrg    XrmName class_list[3];
768444c061aSmrg    XrmRepresentation type;
769444c061aSmrg    XrmValue val;
770444c061aSmrg    String *targv;
771444c061aSmrg    int targc = argc;
772444c061aSmrg
773444c061aSmrg    targv = (String *) __XtMalloc(sizeof(char *) * argc);
774444c061aSmrg    (void) memmove(targv, argv, sizeof(char *) * argc);
775444c061aSmrg    _MergeOptionTables(opTable, XtNumber(opTable), urlist, num_urs,
776444c061aSmrg		       &options, &num_options);
777444c061aSmrg    name_list[0] = class_list[0] = XrmPermStringToQuark(".");
778444c061aSmrg    name_list[2] = class_list[2] = NULLQUARK;
779444c061aSmrg    XrmParseCommand(&db, options, num_options, ".", &targc, targv);
780444c061aSmrg    if (applName) {
781444c061aSmrg	name_list[1] = XrmPermStringToQuark("name");
782444c061aSmrg	if (XrmQGetResource(db, name_list, name_list, &type, &val) &&
783444c061aSmrg	    type == _XtQString)
784444c061aSmrg	    *applName = val.addr;
785444c061aSmrg    }
786444c061aSmrg    if (displayName) {
787444c061aSmrg	name_list[1] = XrmPermStringToQuark("display");
788444c061aSmrg	if (XrmQGetResource(db, name_list, name_list, &type, &val) &&
789444c061aSmrg	    type == _XtQString)
790444c061aSmrg	    *displayName = val.addr;
791444c061aSmrg    }
792444c061aSmrg    if (language) {
793444c061aSmrg	name_list[1] = XrmPermStringToQuark("xnlLanguage");
794444c061aSmrg	class_list[1] = XrmPermStringToQuark("XnlLanguage");
795444c061aSmrg	if (XrmQGetResource(db, name_list, class_list, &type, &val) &&
796444c061aSmrg	    type == _XtQString)
797444c061aSmrg	    *language = val.addr;
798444c061aSmrg    }
799444c061aSmrg
800444c061aSmrg    XtFree((char *)targv);
801444c061aSmrg    XtFree((char *)options);
802444c061aSmrg    return db;
803444c061aSmrg}
804444c061aSmrg
805444c061aSmrg
806444c061aSmrgstatic void GetLanguage(
807444c061aSmrg    Display *dpy,
808444c061aSmrg    XtPerDisplay pd)
809444c061aSmrg{
810444c061aSmrg    XrmRepresentation type;
811444c061aSmrg    XrmValue value;
812444c061aSmrg    XrmName name_list[3];
813444c061aSmrg    XrmName class_list[3];
814444c061aSmrg
815444c061aSmrg    LOCK_PROCESS;
816444c061aSmrg    if (! pd->language) {
817444c061aSmrg	name_list[0] = pd->name;
818444c061aSmrg	name_list[1] = XrmPermStringToQuark("xnlLanguage");
819444c061aSmrg	class_list[0] = pd->class;
820444c061aSmrg	class_list[1] = XrmPermStringToQuark("XnlLanguage");
821444c061aSmrg	name_list[2] = class_list[2] = NULLQUARK;
822444c061aSmrg	if (!pd->server_db)
823444c061aSmrg	    CombineUserDefaults(dpy, &pd->server_db);
824444c061aSmrg	if (pd->server_db &&
825444c061aSmrg	    XrmQGetResource(pd->server_db,name_list,class_list, &type, &value)
826444c061aSmrg	    && type == _XtQString)
827444c061aSmrg	    pd->language = (char *) value.addr;
828444c061aSmrg    }
829444c061aSmrg
830444c061aSmrg    if (pd->appContext->langProcRec.proc) {
831444c061aSmrg	if (! pd->language) pd->language = "";
832444c061aSmrg	pd->language = (*pd->appContext->langProcRec.proc)
833444c061aSmrg	    (dpy, pd->language, pd->appContext->langProcRec.closure);
834444c061aSmrg    }
835444c061aSmrg    else if (! pd->language || pd->language[0] == '\0') /* R4 compatibility */
836444c061aSmrg	pd->language = getenv("LANG");
837444c061aSmrg
838444c061aSmrg    if (pd->language) pd->language = XtNewString(pd->language);
839444c061aSmrg    UNLOCK_PROCESS;
840444c061aSmrg}
841444c061aSmrg
842444c061aSmrgstatic void ProcessInternalConnection (
843444c061aSmrg    XtPointer client_data,
844444c061aSmrg    int* fd,
845444c061aSmrg    XtInputId* id)
846444c061aSmrg{
847444c061aSmrg    XProcessInternalConnection ((Display *) client_data, *fd);
848444c061aSmrg}
849444c061aSmrg
850444c061aSmrgstatic void ConnectionWatch (
851444c061aSmrg    Display* dpy,
852444c061aSmrg    XPointer client_data,
853444c061aSmrg    int fd,
854444c061aSmrg    Bool opening,
855444c061aSmrg    XPointer* watch_data)
856444c061aSmrg{
857444c061aSmrg    XtInputId* iptr;
858444c061aSmrg    XtAppContext app = XtDisplayToApplicationContext(dpy);
859444c061aSmrg
860444c061aSmrg    if (opening) {
861444c061aSmrg	iptr = (XtInputId *) __XtMalloc(sizeof(XtInputId));
862444c061aSmrg	*iptr = XtAppAddInput(app, fd, (XtPointer) XtInputReadMask,
863444c061aSmrg			      ProcessInternalConnection, client_data);
864444c061aSmrg	*watch_data = (XPointer) iptr;
865444c061aSmrg    } else {
866444c061aSmrg	iptr = (XtInputId *) *watch_data;
867444c061aSmrg	XtRemoveInput(*iptr);
868444c061aSmrg        (void) XtFree(*watch_data);
869444c061aSmrg    }
870444c061aSmrg}
871444c061aSmrg
872444c061aSmrgvoid _XtDisplayInitialize(
873444c061aSmrg	Display *dpy,
874444c061aSmrg        XtPerDisplay pd,
875444c061aSmrg	_Xconst char* name,
876444c061aSmrg	XrmOptionDescRec *urlist,
877444c061aSmrg	Cardinal num_urs,
878444c061aSmrg	int *argc,
879444c061aSmrg	char **argv)
880444c061aSmrg{
881444c061aSmrg	Boolean tmp_bool;
882444c061aSmrg	XrmValue value;
883444c061aSmrg	XrmOptionDescRec *options;
884444c061aSmrg	Cardinal num_options;
885444c061aSmrg	XrmDatabase db;
886444c061aSmrg	XrmName name_list[2];
887444c061aSmrg	XrmClass class_list[2];
888444c061aSmrg	XrmHashTable* search_list;
889444c061aSmrg	int search_list_size = SEARCH_LIST_SIZE;
890444c061aSmrg
891444c061aSmrg	GetLanguage(dpy, pd);
892444c061aSmrg
893444c061aSmrg	/* Parse the command line and remove Xt arguments from argv */
894444c061aSmrg	_MergeOptionTables( opTable, XtNumber(opTable), urlist, num_urs,
895444c061aSmrg			    &options, &num_options );
896444c061aSmrg	XrmParseCommand(&pd->cmd_db, options, num_options, name, argc, argv);
897444c061aSmrg
898444c061aSmrg	db = XtScreenDatabase(DefaultScreenOfDisplay(dpy));
899444c061aSmrg
900444c061aSmrg	if (!(search_list = (XrmHashTable*)
901444c061aSmrg		       ALLOCATE_LOCAL( SEARCH_LIST_SIZE*sizeof(XrmHashTable))))
902444c061aSmrg	    _XtAllocError(NULL);
903444c061aSmrg	name_list[0] = pd->name;
904444c061aSmrg	class_list[0] = pd->class;
905444c061aSmrg	name_list[1] = NULLQUARK;
906444c061aSmrg	class_list[1] = NULLQUARK;
907444c061aSmrg
908444c061aSmrg	while (!XrmQGetSearchList(db, name_list, class_list,
909444c061aSmrg				  search_list, search_list_size)) {
910444c061aSmrg	    XrmHashTable* old = search_list;
911444c061aSmrg	    Cardinal size = (search_list_size*=2)*sizeof(XrmHashTable);
912444c061aSmrg	    if (!(search_list = (XrmHashTable*)ALLOCATE_LOCAL(size)))
913444c061aSmrg		_XtAllocError(NULL);
914444c061aSmrg	    (void) memmove((char*)search_list, (char*)old, (size>>1) );
915444c061aSmrg	    DEALLOCATE_LOCAL(old);
916444c061aSmrg	}
917444c061aSmrg
918444c061aSmrg	value.size = sizeof(tmp_bool);
919444c061aSmrg	value.addr = (XtPointer)&tmp_bool;
920444c061aSmrg	if (_GetResource(dpy, search_list, "synchronous", "Synchronous",
921444c061aSmrg			 XtRBoolean, &value)) {
922444c061aSmrg	    int i;
923444c061aSmrg	    Display **dpyP = pd->appContext->list;
924444c061aSmrg	    pd->appContext->sync = tmp_bool;
925444c061aSmrg	    for (i = pd->appContext->count; i; dpyP++, i--) {
926444c061aSmrg		(void) XSynchronize(*dpyP, (Bool)tmp_bool);
927444c061aSmrg	    }
928444c061aSmrg	} else {
929444c061aSmrg	    (void) XSynchronize(dpy, (Bool)pd->appContext->sync);
930444c061aSmrg	}
931444c061aSmrg
932444c061aSmrg	if (_GetResource(dpy, search_list, "reverseVideo", "ReverseVideo",
933444c061aSmrg			 XtRBoolean, &value)
934444c061aSmrg	        && tmp_bool) {
935444c061aSmrg	    pd->rv = True;
936444c061aSmrg	}
937444c061aSmrg
938444c061aSmrg	value.size = sizeof(pd->multi_click_time);
939444c061aSmrg	value.addr = (XtPointer)&pd->multi_click_time;
940444c061aSmrg	if (!_GetResource(dpy, search_list,
941444c061aSmrg			  "multiClickTime", "MultiClickTime",
942444c061aSmrg			  XtRInt, &value)) {
943444c061aSmrg	    pd->multi_click_time = 200;
944444c061aSmrg	}
945444c061aSmrg
946444c061aSmrg	value.size = sizeof(pd->appContext->selectionTimeout);
947444c061aSmrg	value.addr = (XtPointer)&pd->appContext->selectionTimeout;
948444c061aSmrg	(void)_GetResource(dpy, search_list,
949444c061aSmrg			   "selectionTimeout", "SelectionTimeout",
950444c061aSmrg			   XtRInt, &value);
951444c061aSmrg
952444c061aSmrg#ifndef NO_IDENTIFY_WINDOWS
953444c061aSmrg	value.size = sizeof(pd->appContext->identify_windows);
954444c061aSmrg	value.addr = (XtPointer)&pd->appContext->identify_windows;
955444c061aSmrg	(void)_GetResource(dpy, search_list,
956444c061aSmrg			   "xtIdentifyWindows", "XtDebug",
957444c061aSmrg			   XtRBoolean, &value);
958444c061aSmrg#endif
959444c061aSmrg
960444c061aSmrg	XAddConnectionWatch(dpy, ConnectionWatch, (XPointer) dpy);
961444c061aSmrg
962444c061aSmrg	XtFree( (XtPointer)options );
963444c061aSmrg	DEALLOCATE_LOCAL( search_list );
964444c061aSmrg}
965444c061aSmrg
966444c061aSmrg/*	Function Name: XtAppSetFallbackResources
967444c061aSmrg *	Description: Sets the fallback resource list that will be loaded
968444c061aSmrg *                   at display initialization time.
969444c061aSmrg *	Arguments: app_context - the app context.
970444c061aSmrg *                 specification_list - the resource specification list.
971444c061aSmrg *	Returns: none.
972444c061aSmrg */
973444c061aSmrg
974444c061aSmrgvoid
975444c061aSmrgXtAppSetFallbackResources(
976444c061aSmrg    XtAppContext app_context,
977444c061aSmrg    String *specification_list)
978444c061aSmrg{
979444c061aSmrg    LOCK_APP(app_context);
980444c061aSmrg    app_context->fallback_resources = specification_list;
981444c061aSmrg    UNLOCK_APP(app_context);
982444c061aSmrg}
983444c061aSmrg
984444c061aSmrg
985444c061aSmrgWidget XtOpenApplication(XtAppContext *app_context_return,
986444c061aSmrg			 _Xconst char *application_class,
987444c061aSmrg			 XrmOptionDescRec *options, Cardinal num_options,
988444c061aSmrg			 int *argc_in_out, String *argv_in_out,
989444c061aSmrg			 String *fallback_resources, WidgetClass widget_class,
990444c061aSmrg			 ArgList args_in, Cardinal num_args_in)
991444c061aSmrg{
992444c061aSmrg    XtAppContext app_con;
993444c061aSmrg    Display * dpy;
994444c061aSmrg    register int saved_argc = *argc_in_out;
995444c061aSmrg    Widget root;
996444c061aSmrg    Arg args[3], *merged_args;
997444c061aSmrg    Cardinal num = 0;
998444c061aSmrg
999444c061aSmrg    XtToolkitInitialize(); /* cannot be moved into _XtAppInit */
1000444c061aSmrg
1001444c061aSmrg    dpy = _XtAppInit(&app_con, (String)application_class, options, num_options,
1002444c061aSmrg		     argc_in_out, &argv_in_out, fallback_resources);
1003444c061aSmrg
1004444c061aSmrg    LOCK_APP(app_con);
1005444c061aSmrg    XtSetArg(args[num], XtNscreen, DefaultScreenOfDisplay(dpy)); num++;
1006444c061aSmrg    XtSetArg(args[num], XtNargc, saved_argc);	                 num++;
1007444c061aSmrg    XtSetArg(args[num], XtNargv, argv_in_out);	                 num++;
1008444c061aSmrg
1009444c061aSmrg    merged_args = XtMergeArgLists(args_in, num_args_in, args, num);
1010444c061aSmrg    num += num_args_in;
1011444c061aSmrg
1012444c061aSmrg    root = XtAppCreateShell(NULL, application_class, widget_class, dpy,
1013444c061aSmrg			    merged_args, num);
1014444c061aSmrg
1015444c061aSmrg    if (app_context_return)
1016444c061aSmrg	*app_context_return = app_con;
1017444c061aSmrg
1018444c061aSmrg    XtFree((XtPointer)merged_args);
1019444c061aSmrg    XtFree((XtPointer)argv_in_out);
1020444c061aSmrg    UNLOCK_APP(app_con);
1021444c061aSmrg    return root;
1022444c061aSmrg}
1023444c061aSmrg
1024444c061aSmrg
1025444c061aSmrgWidget
1026444c061aSmrgXtAppInitialize(
1027444c061aSmrg    XtAppContext * app_context_return,
1028444c061aSmrg    _Xconst char* application_class,
1029444c061aSmrg    XrmOptionDescRec *options,
1030444c061aSmrg    Cardinal num_options,
1031444c061aSmrg    int *argc_in_out,
1032444c061aSmrg    String *argv_in_out,
1033444c061aSmrg    String *fallback_resources,
1034444c061aSmrg    ArgList args_in,
1035444c061aSmrg    Cardinal num_args_in)
1036444c061aSmrg{
1037444c061aSmrg    return XtOpenApplication(app_context_return, application_class,
1038444c061aSmrg			     options, num_options,
1039444c061aSmrg			     argc_in_out, argv_in_out, fallback_resources,
1040444c061aSmrg			     applicationShellWidgetClass,
1041444c061aSmrg			     args_in, num_args_in);
1042444c061aSmrg}
1043444c061aSmrg
1044444c061aSmrg
1045444c061aSmrg/*ARGSUSED*/
1046444c061aSmrgWidget
1047444c061aSmrgXtInitialize(
1048444c061aSmrg    _Xconst char* name,
1049444c061aSmrg    _Xconst char* classname,
1050444c061aSmrg    XrmOptionDescRec *options,
1051444c061aSmrg    Cardinal num_options,
1052444c061aSmrg    int *argc,
1053444c061aSmrg    String *argv)
1054444c061aSmrg{
1055444c061aSmrg    Widget root;
1056444c061aSmrg    XtAppContext app_con;
1057444c061aSmrg    register ProcessContext process = _XtGetProcessContext();
1058444c061aSmrg
1059444c061aSmrg    root = XtAppInitialize(&app_con, classname, options, num_options,
1060444c061aSmrg			   argc, argv, NULL, NULL, (Cardinal) 0);
1061444c061aSmrg
1062444c061aSmrg    LOCK_PROCESS;
1063444c061aSmrg    process->defaultAppContext = app_con;
1064444c061aSmrg    UNLOCK_PROCESS;
1065444c061aSmrg    return root;
1066444c061aSmrg}
1067