File: XResource.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (302 lines) | stat: -rw-r--r-- 8,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* -*-c-*- */
/* Copyright (C) 1999  Dominik Vogt */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/*
** XResource.c:
** These routines provide modules with an interface to parse all kinds of
** configuration options (X resources, command line options and configuration
** file lines) in the same way (Xrm database).
*/

#include "config.h"

#include <X11/Xlib.h>
#include <X11/Xresource.h>

#include "fvwmlib.h"
#include "XResource.h"



/*
 * If you have a module MyModule and want to parse X resources as well as
 * command line options and a config file:
 *
 *** EXAMPLE */
#if 0
    #include <fvwmlib.h>

    void main(int argc, char **argv)
    {
      const char *MyName = "MyModule";
      XrmDatabase db = NULL;
      XrmValue *rm_value;
      char *line;

      /* our private options */
      const XrmOptionDescRec my_opts[] = {
	{ "-iconic",   ".Iconic", XrmoptionNoArg,  "any_string" },
	{ "-foo",      "*bar",    XrmoptionSepArg, NULL }
      };
      int opt_argc = argc - 6; /* options start at 6th argument for modules */
      char **opt_argv = argv + 6;

      /* ... (open config file, etc.) */

      /* Get global X resources */
      MergeXResources(NULL, &db, False);

      /* config file descriptor in fd; config file takes precedence over X
       * resources (this may not be what you want). */
      for (GetConfigLine(fd, &line); line != NULL; GetConfigLine(fd, &line))
      {
	if (!MergeConfigLineResource(&db, line, MyName, '*'))
	{
	  /* Parse other lines here (e.g. "ImagePath") */
	}
	else
	{
	  /* You may still have to parse the line here yourself (e.g.
	   * FvwmButtons may have multiple lines for the same resource). */
	}
      }

      /* command line takes precedence over all */
      MergeCmdLineResources(&db, (XrmOptionDescList)my_opts, 2, MyName,
			    &opt_argc, opt_argv, True /*no default options*/);

      /* Now parse the database values: */
      if (GetResourceString(db, "iconic", MyName, &rm_value))
      {
	/* Just see if there is *any* string and don't mind it's value. */
	/* flags |= ICONIC */
      }
      if (GetResourceString(db, "bar", MyName, &rm_value))
      {
	/* ... */
      }

      /* ... */
      XrmDestroyDatabase(db);
    }
#endif

/*** END OF EXAMPLE ***/




/* Default option table */
static XrmOptionDescRec default_opts[] =
{
  { "-fg",       "*Foreground", XrmoptionSepArg, NULL },
  { "-bg",       "*Background", XrmoptionSepArg, NULL },
  { "-fn",       "*Font",       XrmoptionSepArg, NULL },
  { "-geometry", "*Geometry",   XrmoptionSepArg, NULL },
  { "-title",    "*Title",      XrmoptionSepArg, NULL }
  /* Remember to update NUM_DEFAULT_OPTIONS if you change this list! */
};
#define NUM_DEFAULT_OPTS 5



/* internal function */
static void DoMergeString(char *resource, XrmDatabase *ptarget, Bool override)
{
  XrmDatabase db;

  if (!resource)
    return;
  db = XrmGetStringDatabase(resource);
  XrmCombineDatabase(db, ptarget, override);
}

/*
 *
 * Merges all X resources for the display/screen into a Xrm database.
 * If the database does not exist (*pdb == NULL), a new database is created.
 * If override is True, existing entries of the same name are overwritten.
 *
 * Please remember to destroy the database with XrmDestroyDatabase(*pdb)
 * if you do not need it amymore.
 *
 */
void MergeXResources(Display *dpy, XrmDatabase *pdb, Bool override)
{
  if (!*pdb)
    /* create new database */
    XrmPutStringResource(pdb, "", "");
  DoMergeString(XResourceManagerString(dpy), pdb, override);
  DoMergeString(XScreenResourceString(DefaultScreenOfDisplay(dpy)), pdb,
		override);
}

/*
 *
 * Parses the command line given through pargc/argv and puts recognized
 * entries into the Xrm database *pdb (if *pdb is NULL a new database is
 * created). The caller may provide an option list in XrmOptionDescList
 * format (see XrmParseCommand manpage) and/or parse only standard options
 * (fg, bg, geometry, fn, title). User given options have precedence over
 * standard options which are disabled if fNoDefaults is True. Existing
 * values are overwritten.
 *
 * All recognised options are removed from the command line (*pargc and
 * argv are updated accordingly).
 *
 * Please remember to destroy the database with XrmDestroyDatabase(*pdb)
 * if you do not need it amymore.
 *
 */
void MergeCmdLineResources(XrmDatabase *pdb, XrmOptionDescList opts,
			   int num_opts, char *name, int *pargc, char **argv,
			   Bool fNoDefaults)
{
  if (!name)
    return;
  if (opts && num_opts > 0)
    XrmParseCommand(pdb, opts, num_opts, name, pargc, argv);
  if (!fNoDefaults)
    XrmParseCommand(pdb, default_opts, NUM_DEFAULT_OPTS,
		    name, pargc, argv);
}

/*
 *
 * Takes a line from a config file and puts a corresponding value into the
 * Xrm database *pdb (will be created if *pdb is NULL). 'prefix' is the
 * name of the module. A specific type of binding in the database must be
 * provided in bindstr (either "*" or "."). Leading unquoted whitespace are
 * stripped from value. Existing values in the database are overwritten.
 * True is returned if the line was indeed merged into the database (i.e. it
 * had the correct format) or False if not.
 *
 * Example: If prefix = "MyModule" and bindstr = "*", the line
 *
 *   *MyModuleGeometry   80x25+0+0
 *
 * will be put into the database as if you had this line in your .Xdefaults:
 *
 *   MyModule*Geometry:  80x25+0+0
 *
 * Please remember to destroy the database with XrmDestroyDatabase(*pdb)
 * if you do not need it amymore.
 *
 */
Bool MergeConfigLineResource(XrmDatabase *pdb, char *line, char *prefix,
			     char *bindstr)
{
  int len;
  char *end;
  char *value;
  char *myvalue;
  char *resource;

  /* translate "*(prefix)(suffix)" to "(prefix)(binding)(suffix)",
   * e.g. "*FvwmPagerGeometry" to "FvwmPager.Geometry" */
  if (!line || *line != '*')
    return False;

  line++;
  len = (prefix) ? strlen(prefix) : 0;
  if (!prefix || strncasecmp(line, prefix, len))
    return False;

  line += len;
  end = line;
  while (*end && !isspace((unsigned char)*end))
    end++;
  if (line == end)
    return False;
  value = end;
  while (*value && isspace((unsigned char)*value))
    value++;

  /* prefix*suffix: value */
  resource = (char *)safemalloc(len + (end - line) + 2);
  strcpy(resource, prefix);
  strcat(resource, bindstr);
  strncat(resource, line, end - line);

  len = strlen(value);
  myvalue = (char *)safemalloc(len + 1);
  strcpy(myvalue, value);
  for (len--; len >= 0 && isspace((unsigned char)myvalue[len]); len--)
    myvalue[len] = 0;

  /* merge string into database */
  XrmPutStringResource(pdb, resource, myvalue);

  free(resource);
  free(myvalue);
  return True;
}

/*
 *
 * Reads the string-value for the pair prefix/resource from the Xrm database
 * db and returns a pointer to it. The string may only be read and must not
 * be freed by the caller. 'prefix' is the class name (usually the name of
 * the module). If no value is found in the database, *val will be NULL.
 * True is returned if a value was found, False if not. If you are only
 * interested if there is a string, but not it's value, you can set val to
 * NULL.
 *
 * Example:
 *
 *   GetResourceString(db, "Geometry", "MyModule", &r)
 *
 * returns the resource value of the "Geometry" resource for MyModule in r.
 *
 */
Bool GetResourceString(
  XrmDatabase db, const char *resource, const char *prefix, XrmValue *xval)
{
  char *str_type;
  char *name;
  char *Name;
  int i;

  name = (char *)safemalloc(strlen(resource) + strlen(prefix) + 2);
  Name = (char *)safemalloc(strlen(resource) + strlen(prefix) + 2);
  strcpy(name, prefix);
  strcat(name, ".");
  strcat(name, resource);
  strcpy(Name, name);
  if (isupper(name[0]))
    name[0] = tolower(name[0]);
  if (islower(Name[0]))
    Name[0] = toupper(Name[0]);
  i = strlen(prefix) + 1;
  if (isupper(name[i]))
    name[i] = tolower(name[i]);
  if (islower(Name[i]))
    Name[i] = toupper(Name[i]);
  if (!XrmGetResource(db, name, Name, &str_type, xval) ||
      xval->addr == NULL || xval->size == 0)
  {
    free(name);
    free(Name);
    xval->size = 0;
    xval->addr = NULL;

    return False;
  }
  free(name);
  free(Name);

  return True;
}