Convert uses of XKeycodeToKeysym (deprecated) to XkbKeycodeToKeysym
[oweals/cde.git] / cde / programs / dtsession / SmXdef.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 libraries 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: SmXdef.c /main/4 1995/10/30 09:39:16 rswiston $ */
24 /*                                                                      *
25  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
26  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
27  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
28  * (c) Copyright 1993, 1994 Novell, Inc.                                *
29  */
30 /*************************************<+>*************************************
31  *****************************************************************************
32  **
33  **  File:        SmXdef.c
34  **
35  **  Project:     DT Session Manager (dtsession)
36  **
37  **  Description:
38  **  -----------
39  **  This file contains routines to manage the Xdefaults file.
40  **
41  **  SmXdefMerge() - merge .Xdefaults file into RESOURCE_MANAGER
42  **  SmXdefSubtract() - subtract .Xdefaults file from given database
43  **
44  *****************************************************************************
45  *************************************<+>*************************************/
46
47 #include <stdio.h>
48 #include <X11/Intrinsic.h>
49 #include "Sm.h"
50 #include "SmXrm.h"
51
52 /*
53  * State data
54  *   dbXdefaults - copy of .Xdefaults in Xrm database form
55  */
56  static XrmDatabase dbXdefaults = NULL;
57
58
59 /*
60  * Note:
61  * 
62  * The memory for dbXdefaults is freed only upon dtsession termination
63  *
64  * This code is currently restricted to handling the .Xdefaults file,
65  * but can easily be extended to handle other default resource files.
66  *
67  */
68
69 \f
70 /*************************************<->*************************************
71  *
72  *  SmXdefMerge(display)
73  *
74  *  Description:
75  *  -----------
76  *  Merge the .Xdefaults file into the RESOURCE_MANAGER database
77  *
78  *  Inputs:
79  *  ------
80  *  display - display connection
81  *
82  *  Outputs:
83  *  -------
84  *
85  *
86  *  Comments:
87  *  --------
88  *
89  *************************************<->***********************************/
90 void
91 SmXdefMerge(Display *display)
92 {
93   char *xdefaults;
94   char *home;
95
96  /*
97   * Load .Xdefaults
98   */
99   if ((home = getenv("HOME")) == NULL)
100     home = "";
101
102   if( (xdefaults = (char *)malloc(strlen(home)+12)) != NULL)  
103   {
104     sprintf(xdefaults,"%s/%s",home,".Xdefaults");
105     if(access(xdefaults,R_OK) == 0)
106     {
107       FILE *fp;
108       char *b = NULL;
109       int size;
110       struct stat statinfo; 
111
112      /*
113       * Determine size of file.
114       */
115       if (stat(xdefaults, &statinfo) == -1)
116       {
117         statinfo.st_size = 0;
118       }
119
120      /*
121       * Get some memory.
122       */
123       if (statinfo.st_size > 0)
124       {
125         b = (char *)SM_MALLOC(statinfo.st_size + 1);
126       }
127
128       if (b != NULL)
129       {
130        /*
131         * Read file into memory.
132         */
133         if ((fp = fopen(xdefaults, "r")) != NULL)
134         {
135           size = fread(b, 1, statinfo.st_size, fp);
136           fclose(fp);
137         }
138
139         if (size == statinfo.st_size)
140         {
141          /*
142           * Merge .Xdefaults string into RESOURCE_MANAGER database, and
143           * also convert to Xrm database form for later subtraction.
144           */
145           b[size] = '\0';
146           _DtAddToResource(display, b);
147           dbXdefaults = XrmGetStringDatabase(b);  
148           SM_FREE(b);
149         }
150       }
151     }
152     free(xdefaults);
153   }
154 }
155
156 \f
157 /*************************************<->*************************************
158  *
159  *  SmXdefSubtract(db)
160  *
161  *  Description:
162  *  -----------
163  *  Subract prior merged .Xdefaults file from given database
164  *
165  *  Inputs:
166  *  ------
167  *  db - Xrm database from which to subtract .Xdefaults
168  *
169  *  Outputs:
170  *  -------
171  *
172  *  Return: 
173  *  ------
174  *  dbResult - result database
175  *
176  *  Comments:
177  *  --------
178  *  Caller is responsible for freeing dbResult using XrmDestroyDatabase()
179  *
180  *
181  *************************************<->***********************************/
182 XrmDatabase
183 SmXdefSubtract(XrmDatabase db)
184 {
185   XrmDatabase dbResult;
186
187   if (dbXdefaults)
188   {
189     dbResult = SmXrmSubtractDatabase(dbXdefaults, db);
190   }
191   else
192   {
193     dbResult = NULL;
194   }
195   return(dbResult);
196 }