Merge branch 'master' of https://git.code.sf.net/p/cdesktopenv/code
[oweals/cde.git] / cde / programs / dtcalc / ds_xlib.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $XConsortium: ds_xlib.c /main/3 1995/11/01 12:40:45 rswiston $ */
24 /*                                                                      *
25  *  ds_xlib.c                                                           *
26  *   Contains some common functions which use the xlib library used     *
27  *   throughout the Desktop Calculator.                                 *
28  *                                                                      *
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.                                *
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include "ds_xlib.h"
41 #include <X11/Xutil.h>
42 #include <X11/StringDefs.h>
43 #include <Xm/Xm.h>
44 #include <Xm/Protocols.h>
45
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
51
52 #define  EQUAL(a, b)  !strncmp(a, b, strlen(b))
53 #define  MAXLINE      120        /* Maximum length for character strings. */
54
55 /*  Function:     ds_beep()
56  *
57  *  Purpose:      Ring the bell (at base volume).
58  *
59  *  Parameters:   display    connection to the X server.
60  *                           (returned from XOpenDisplay).
61  *
62  *  Returns:      None.
63  */
64
65 void
66 ds_beep(display)
67 Display *display ;
68 {
69   XBell(display, 0) ;
70 }
71
72
73
74 /*  Function:     ds_get_resource()
75  *
76  *  Purpose:      Get an X resource from the server.
77  *
78  *  Parameters:   rDB          X resources database.
79  *
80  *                appname      application name.
81  *
82  *                resource     X resource string to search for.
83  *
84  *  Returns:      resource string, or NULL if not found.
85  *
86  *  Note:         The first character of the appname and resource strings may
87  *                be modified.
88  */
89
90 char *
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. */
95 {
96   char app[MAXLINE], res[MAXLINE] ;
97   char cstr[MAXLINE], nstr[MAXLINE], str[MAXLINE] ;
98   char *str_type[20] ;
99   XrmValue value ;
100
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) ;
105
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) ;
109
110   if (XrmGetResource(rDB, nstr, cstr, str_type, &value) == 0)
111     return((char *) NULL) ;
112   else return(value.addr) ;
113 }
114
115
116 /*  Function:     ds_load_resources()
117  *
118  *  Purpose:      Get the resource databases. These are looked for in the
119  *                following ways:
120  *
121  *                Classname file in the app-defaults directory. 
122  *
123  *                Classname file in the directory specified by the
124  *                XUSERFILESEARCHPATH or XAPPLRESDIR environment variable.
125  *
126  *                Property set using xrdb, accessible through the
127  *                XResourceManagerString macro or, if that is empty, the
128  *                ~/.Xdefaults file.
129  *
130  *                XENVIRONMENT environment variable or, if not set,
131  *                .Xdefaults-hostname file.
132  *
133  *                DTCALCDEF environment variable or, if not set, the
134  *                ~/.dtcalcdef file
135  *
136  *  Parameters:   display    connection to the X server.
137  *                           (returned from XOpenDisplay).
138  *
139  *  Returns:      X combined resources database.
140  */
141
142 XrmDatabase
143 ds_load_resources(display)
144 Display *display ;
145 {
146   XrmDatabase db, rDB ;
147   char *home, name[MAXPATHLEN], *ptr ;
148   int len ;
149
150   rDB  = NULL ;
151   home = getenv("HOME") ;
152   XrmInitialize() ;
153
154 /* Merge server defaults, created by xrdb. If nor defined, use ~/.Xdefaults. */
155
156   if (XResourceManagerString(display) != NULL)
157     db = XrmGetStringDatabase(XResourceManagerString(display)) ;
158   else
159     { 
160       SPRINTF(name, "%s/.Xdefaults", home) ;
161       db = XrmGetFileDatabase(name) ;
162     }
163   XrmMergeDatabases(db, &rDB) ;
164
165 /*  Open XENVIRONMENT file or, if not defined, the .Xdefaults, and merge
166  *  into existing database.
167  */
168
169   if ((ptr = getenv("XENVIRONMENT")) == NULL)
170     {
171       SPRINTF(name, "%s/.Xdefaults-", home) ;
172       len = strlen(name) ;
173       GETHOSTNAME(name+len, 1024-len) ;
174       db = XrmGetFileDatabase(name) ;
175     }
176   else db = XrmGetFileDatabase(ptr) ;
177   XrmMergeDatabases(db, &rDB) ;
178
179 /*  Finally merge in Dtcalc defaults via DTCALCDEF or, if not
180  *  defined, the ~/.dtcalcdef file.
181  */
182
183   if ((ptr = getenv("DTCALCDEF")) == NULL)
184     {
185       SPRINTF(name, "%s/.dtcalcdef", home) ;
186       db = XrmGetFileDatabase(name) ;
187     }
188   else db = XrmGetFileDatabase(ptr) ;
189   XrmMergeDatabases(db, &rDB) ;
190   return(rDB) ;
191 }
192
193
194 /*  Function:     ds_put_resource()
195  *
196  *  Purpose:      Adds an X resource string (name and value) to a resources
197  *                database.
198  *
199  *  Parameters:   rDB          X resources database.
200  *
201  *                appname      application name.
202  *
203  *                rstr         X resource string name.
204  *
205  *                rval         X resource string value.
206  *
207  *  Returns:      None.
208  *
209  *  Note:         The first character of the appname and resource strings may
210  *                be modified.
211  */
212
213 void
214 ds_put_resource(rDB, appname, rstr, rval)
215 XrmDatabase *rDB ;
216 char *appname, *rstr, *rval ;
217 {
218   char app[MAXLINE], resource[MAXLINE] ;
219
220   STRCPY(app, appname) ;
221   if (isupper(app[0])) app[0] = tolower(app[0]) ;
222   SPRINTF(resource, "%s.%s", app, rstr) ;
223
224   XrmPutStringResource(rDB, resource, rval) ;
225 }
226
227
228 /*  Function:     ds_save_cmdline()
229  *
230  *  Purpose:      Save away the application command line options.
231  *
232  *  Parameters:   display      connection to the X server.
233  *                             (returned from XOpenDisplay).
234  *
235  *                w            The id of the applications main window.
236  *
237  *                argc         Number of command line options.
238  *
239  *                argv         An array of command line options.
240  *
241  *  Returns:      None.
242  */
243
244 void
245 ds_save_cmdline(display, w, argc, argv)
246 Display *display ;
247 Window w ;
248 int argc ;
249 char **argv ;
250 {
251   XSetCommand(display, w, argv, argc) ;
252 }
253
254
255 /*  Function:     ds_save_resources()
256  *
257  *  Purpose:      Save away the resources database to the file given by the
258  *                DTCALCDEF environment variable (if set), or
259  *                to $HOME/.dtcalcdef.
260  *
261  *  Parameters:   rDB        X resources database to save.
262  *
263  *  Returns:      1          if cannot access resource database to write.
264  *                0          on successful completion.
265  */
266
267
268 int
269 ds_save_resources(rDB, filename)
270 XrmDatabase rDB ;
271 char *filename;
272 {
273   char *home;
274   struct stat statbuf ;
275
276   if(filename == NULL)
277   {
278     if ((filename = getenv("DTCALCDEF")) == NULL)
279       {
280         home = getenv("HOME") ;
281         filename = (char*) calloc(1, strlen(home) + 18) ;
282         SPRINTF(filename, "%s/.dtcalcdef", home) ;
283       }
284   }
285
286 /* If file exists but user does not have access. */
287
288   if (stat(filename, &statbuf) != -1 && access(filename, W_OK) != 0)
289     { 
290       FREE(filename) ;
291       return(1) ;
292     }
293
294 /* If file does not exist this call will create it. */
295
296   XrmPutFileDatabase(rDB, filename) ;
297   FREE(filename) ;
298   return(0) ;
299 }
300