2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: ds_xlib.c /main/3 1995/11/01 12:40:45 rswiston $ */
26 * Contains some common functions which use the xlib library used *
27 * throughout the Desktop Calculator. *
29 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
30 * (c) Copyright 1993, 1994 International Business Machines Corp. *
31 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
32 * (c) Copyright 1993, 1994 Novell, Inc. *
38 #include <sys/param.h>
41 #include <X11/Xutil.h>
42 #include <X11/StringDefs.h>
44 #include <Xm/Protocols.h>
46 #define FREE (void) free
47 #define FPRINTF (void) fprintf
48 #define GETHOSTNAME (void) gethostname
49 #define SPRINTF (void) sprintf
50 #define STRCPY (void) strcpy
52 #define EQUAL(a, b) !strncmp(a, b, strlen(b))
53 #define MAXLINE 120 /* Maximum length for character strings. */
55 /* Function: ds_beep()
57 * Purpose: Ring the bell (at base volume).
59 * Parameters: display connection to the X server.
60 * (returned from XOpenDisplay).
74 /* Function: ds_get_resource()
76 * Purpose: Get an X resource from the server.
78 * Parameters: rDB X resources database.
80 * appname application name.
82 * resource X resource string to search for.
84 * Returns: resource string, or NULL if not found.
86 * Note: The first character of the appname and resource strings may
91 ds_get_resource(rDB, appname, resource)
92 XrmDatabase rDB ; /* Resources database. */
93 char *appname ; /* Application name. */
94 char *resource ; /* X resource to search for. */
96 char app[MAXLINE], res[MAXLINE] ;
97 char cstr[MAXLINE], nstr[MAXLINE], str[MAXLINE] ;
101 STRCPY(app, appname) ;
102 STRCPY(res, resource) ;
103 if (isupper(app[0])) app[0] = tolower(app[0]) ;
104 SPRINTF(nstr, "%s.%s", app, res) ;
106 if (islower(res[0])) res[0] = toupper(res[0]) ;
107 if (islower(app[0])) app[0] = toupper(app[0]) ;
108 SPRINTF(cstr, "%s.%s", app, res) ;
110 if (XrmGetResource(rDB, nstr, cstr, str_type, &value) == 0)
111 return((char *) NULL) ;
112 else return(value.addr) ;
116 /* Function: ds_load_resources()
118 * Purpose: Get the resource databases. These are looked for in the
121 * Classname file in the app-defaults directory.
123 * Classname file in the directory specified by the
124 * XUSERFILESEARCHPATH or XAPPLRESDIR environment variable.
126 * Property set using xrdb, accessible through the
127 * XResourceManagerString macro or, if that is empty, the
130 * XENVIRONMENT environment variable or, if not set,
131 * .Xdefaults-hostname file.
133 * DTCALCDEF environment variable or, if not set, the
136 * Parameters: display connection to the X server.
137 * (returned from XOpenDisplay).
139 * Returns: X combined resources database.
143 ds_load_resources(display)
146 XrmDatabase db, rDB ;
147 char *home, name[MAXPATHLEN], *ptr ;
151 home = getenv("HOME") ;
154 /* Merge server defaults, created by xrdb. If nor defined, use ~/.Xdefaults. */
156 if (XResourceManagerString(display) != NULL)
157 db = XrmGetStringDatabase(XResourceManagerString(display)) ;
160 SPRINTF(name, "%s/.Xdefaults", home) ;
161 db = XrmGetFileDatabase(name) ;
163 XrmMergeDatabases(db, &rDB) ;
165 /* Open XENVIRONMENT file or, if not defined, the .Xdefaults, and merge
166 * into existing database.
169 if ((ptr = getenv("XENVIRONMENT")) == NULL)
171 SPRINTF(name, "%s/.Xdefaults-", home) ;
173 GETHOSTNAME(name+len, 1024-len) ;
174 db = XrmGetFileDatabase(name) ;
176 else db = XrmGetFileDatabase(ptr) ;
177 XrmMergeDatabases(db, &rDB) ;
179 /* Finally merge in Dtcalc defaults via DTCALCDEF or, if not
180 * defined, the ~/.dtcalcdef file.
183 if ((ptr = getenv("DTCALCDEF")) == NULL)
185 SPRINTF(name, "%s/.dtcalcdef", home) ;
186 db = XrmGetFileDatabase(name) ;
188 else db = XrmGetFileDatabase(ptr) ;
189 XrmMergeDatabases(db, &rDB) ;
194 /* Function: ds_put_resource()
196 * Purpose: Adds an X resource string (name and value) to a resources
199 * Parameters: rDB X resources database.
201 * appname application name.
203 * rstr X resource string name.
205 * rval X resource string value.
209 * Note: The first character of the appname and resource strings may
214 ds_put_resource(rDB, appname, rstr, rval)
216 char *appname, *rstr, *rval ;
218 char app[MAXLINE], resource[MAXLINE] ;
220 STRCPY(app, appname) ;
221 if (isupper(app[0])) app[0] = tolower(app[0]) ;
222 SPRINTF(resource, "%s.%s", app, rstr) ;
224 XrmPutStringResource(rDB, resource, rval) ;
228 /* Function: ds_save_cmdline()
230 * Purpose: Save away the application command line options.
232 * Parameters: display connection to the X server.
233 * (returned from XOpenDisplay).
235 * w The id of the applications main window.
237 * argc Number of command line options.
239 * argv An array of command line options.
245 ds_save_cmdline(display, w, argc, argv)
251 XSetCommand(display, w, argv, argc) ;
255 /* Function: ds_save_resources()
257 * Purpose: Save away the resources database to the file given by the
258 * DTCALCDEF environment variable (if set), or
259 * to $HOME/.dtcalcdef.
261 * Parameters: rDB X resources database to save.
263 * Returns: 1 if cannot access resource database to write.
264 * 0 on successful completion.
269 ds_save_resources(rDB, filename)
274 struct stat statbuf ;
278 if ((filename = getenv("DTCALCDEF")) == NULL)
280 home = getenv("HOME") ;
281 filename = (char*) calloc(1, strlen(home) + 18) ;
282 SPRINTF(filename, "%s/.dtcalcdef", home) ;
286 /* If file exists but user does not have access. */
288 if (stat(filename, &statbuf) != -1 && access(filename, W_OK) != 0)
294 /* If file does not exist this call will create it. */
296 XrmPutFileDatabase(rDB, filename) ;