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 libraries 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. *
41 #include <sys/param.h>
44 #include <X11/Xutil.h>
45 #include <X11/StringDefs.h>
47 #include <Xm/Protocols.h>
50 #define EQUAL(a, b) !strncmp(a, b, strlen(b))
51 #define MAXLINE 120 /* Maximum length for character strings. */
53 /* Function: ds_beep()
55 * Purpose: Ring the bell (at base volume).
57 * Parameters: display connection to the X server.
58 * (returned from XOpenDisplay).
64 ds_beep(Display *display)
71 /* Function: ds_get_resource()
73 * Purpose: Get an X resource from the server.
75 * Parameters: rDB X resources database.
77 * appname application name.
79 * resource X resource string to search for.
81 * Returns: resource string, or NULL if not found.
83 * Note: The first character of the appname and resource strings may
88 ds_get_resource(XrmDatabase rDB, char *appname, char *resource)
90 char app[MAXLINE], res[MAXLINE] ;
91 char cstr[MAXLINE], nstr[MAXLINE] ;
95 snprintf(app, sizeof(app), "%s", appname) ;
96 snprintf(res, sizeof(res), "%s", resource) ;
97 if (isupper(app[0])) app[0] = tolower(app[0]) ;
98 snprintf(nstr, sizeof(nstr), "%s.%s", app, res) ;
100 if (islower(res[0])) res[0] = toupper(res[0]) ;
101 if (islower(app[0])) app[0] = toupper(app[0]) ;
102 snprintf(cstr, sizeof(cstr), "%s.%s", app, res) ;
104 if (XrmGetResource(rDB, nstr, cstr, str_type, &value) == 0)
105 return((char *) NULL) ;
106 else return(value.addr) ;
110 /* Function: ds_load_resources()
112 * Purpose: Get the resource databases. These are looked for in the
115 * Classname file in the app-defaults directory.
117 * Classname file in the directory specified by the
118 * XUSERFILESEARCHPATH or XAPPLRESDIR environment variable.
120 * Property set using xrdb, accessible through the
121 * XResourceManagerString macro or, if that is empty, the
124 * XENVIRONMENT environment variable or, if not set,
125 * .Xdefaults-hostname file.
127 * DTCALCDEF environment variable or, if not set, the
130 * Parameters: display connection to the X server.
131 * (returned from XOpenDisplay).
133 * Returns: X combined resources database.
137 ds_load_resources(Display *display)
139 XrmDatabase db, rDB ;
140 char *home, name[MAXPATHLEN], *ptr ;
144 home = getenv("HOME") ;
147 /* Merge server defaults, created by xrdb. If nor defined, use ~/.Xdefaults. */
149 if (XResourceManagerString(display) != NULL)
150 db = XrmGetStringDatabase(XResourceManagerString(display)) ;
153 snprintf(name, sizeof(name), "%s/.Xdefaults", home) ;
154 db = XrmGetFileDatabase(name) ;
156 XrmMergeDatabases(db, &rDB) ;
158 /* Open XENVIRONMENT file or, if not defined, the .Xdefaults, and merge
159 * into existing database.
162 if ((ptr = getenv("XENVIRONMENT")) == NULL)
164 snprintf(name, sizeof(name), "%s/.Xdefaults-", home) ;
166 gethostname(name+len, 1024-len) ;
167 db = XrmGetFileDatabase(name) ;
169 else db = XrmGetFileDatabase(ptr) ;
170 XrmMergeDatabases(db, &rDB) ;
172 /* Finally merge in Dtcalc defaults via DTCALCDEF or, if not
173 * defined, the ~/.dtcalcdef file.
176 if ((ptr = getenv("DTCALCDEF")) == NULL)
178 snprintf(name, MAXPATHLEN - 1, "%s/.dtcalcdef", home) ;
179 db = XrmGetFileDatabase(name) ;
181 else db = XrmGetFileDatabase(ptr) ;
182 XrmMergeDatabases(db, &rDB) ;
187 /* Function: ds_put_resource()
189 * Purpose: Adds an X resource string (name and value) to a resources
192 * Parameters: rDB X resources database.
194 * appname application name.
196 * rstr X resource string name.
198 * rval X resource string value.
202 * Note: The first character of the appname and resource strings may
207 ds_put_resource(XrmDatabase *rDB, char *appname, char *rstr, char *rval)
209 char app[MAXLINE], resource[MAXLINE] ;
211 snprintf(app, sizeof(app), "%s", appname) ;
212 if (isupper(app[0])) app[0] = tolower(app[0]) ;
213 snprintf(resource, sizeof(resource), "%s.%s", app, rstr) ;
215 XrmPutStringResource(rDB, resource, rval) ;
219 /* Function: ds_save_cmdline()
221 * Purpose: Save away the application command line options.
223 * Parameters: display connection to the X server.
224 * (returned from XOpenDisplay).
226 * w The id of the applications main window.
228 * argc Number of command line options.
230 * argv An array of command line options.
236 ds_save_cmdline(Display *display, Window w, int argc, char **argv)
238 XSetCommand(display, w, argv, argc) ;
242 /* Function: ds_save_resources()
244 * Purpose: Save away the resources database to the file given by the
245 * DTCALCDEF environment variable (if set), or
246 * to $HOME/.dtcalcdef.
248 * Parameters: rDB X resources database to save.
250 * Returns: 1 if cannot access resource database to write.
251 * 0 on successful completion.
256 ds_save_resources(XrmDatabase rDB, char *filename)
260 bool needsFree = false;
262 if (filename == NULL)
264 if ((filename = getenv("DTCALCDEF")) == NULL)
266 if ( !(home = getenv("HOME")) )
269 size_t fileLen = strlen(home) + 18;
270 if ( (filename = calloc(1, fileLen)) != NULL )
273 snprintf(filename, fileLen, "%s/.dtcalcdef", home);
282 /* If file exists but user does not have access. */
284 if (stat(filename, &statbuf) != -1 && access(filename, W_OK) != 0)
292 /* If file does not exist this call will create it. */
294 XrmPutFileDatabase(rDB, filename);