1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2013 Free Software Foundation,
5 Author: Joseph Arceneaux
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
38 #include <X11/Xatom.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
47 /* For Vdouble_click_time. */
51 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
55 /* X file search path processing. */
58 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
59 and friends, or zero if none was specified. */
60 static char *x_customization_string
;
63 /* Return the value of the emacs.customization (Emacs.Customization)
64 resource, for later use in search path decoding. If we find no
65 such resource, return zero. */
67 x_get_customization_string (XrmDatabase db
, const char *name
,
70 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
71 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
74 sprintf (full_name
, "%s.%s", name
, "customization");
75 sprintf (full_class
, "%s.%s", class, "Customization");
77 result
= x_get_string_resource (db
, full_name
, full_class
);
78 return result
? xstrdup (result
) : NULL
;
81 /* Expand all the Xt-style %-escapes in STRING, whose length is given
82 by STRING_LEN. Here are the escapes we're supposed to recognize:
84 %N The value of the application's class name
85 %T The value of the type parameter ("app-defaults" in this
87 %S The value of the suffix parameter ("" in this context)
88 %L The language string associated with the specified display
89 (We use the "LANG" environment variable here, if it's set.)
90 %l The language part of the display's language string
91 (We treat this just like %L. If someone can tell us what
92 we're really supposed to do, dandy.)
93 %t The territory part of the display's language string
94 (This never gets used.)
95 %c The codeset part of the display's language string
96 (This never gets used either.)
97 %C The customization string retrieved from the resource
98 database associated with display.
99 (This is x_customization_string.)
101 Return the resource database if its file was read successfully, and
102 refers to %L only when the LANG environment variable is set, or
103 otherwise provided by X.
105 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
106 %-escapes in ESCAPED_SUFFIX are expanded.
108 Return NULL otherwise. */
111 magic_db (const char *string
, ptrdiff_t string_len
, const char *class,
112 const char *escaped_suffix
)
115 char *lang
= getenv ("LANG");
117 ptrdiff_t path_size
= 100;
118 char *path
= xmalloc (path_size
);
119 ptrdiff_t path_len
= 0;
121 const char *p
= string
;
123 while (p
< string
+ string_len
)
125 /* The chunk we're about to stick on the end of result. */
126 const char *next
= NULL
;
133 if (p
>= string
+ string_len
)
144 next
= (x_customization_string
145 ? x_customization_string
147 next_len
= strlen (next
);
152 next_len
= strlen (class);
156 next
= "app-defaults";
157 next_len
= strlen (next
);
174 next_len
= strlen (next
);
184 next
= p
, next_len
= 1;
186 /* Do we have room for this component followed by a '\0' ? */
187 if (path_size
- path_len
<= next_len
)
189 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
190 memory_full (SIZE_MAX
);
191 path_size
= (path_len
+ next_len
+ 1) * 2;
192 path
= xrealloc (path
, path_size
);
195 memcpy (path
+ path_len
, next
, next_len
);
196 path_len
+= next_len
;
200 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
201 if (p
>= string
+ string_len
&& escaped_suffix
)
203 string
= escaped_suffix
;
204 string_len
= strlen (string
);
206 escaped_suffix
= NULL
;
210 path
[path_len
] = '\0';
211 db
= XrmGetFileDatabase (path
);
224 if ((ptr
= getenv ("HOME")) == NULL
)
226 if ((ptr
= getenv ("LOGNAME")) != NULL
227 || (ptr
= getenv ("USER")) != NULL
)
230 pw
= getpwuid (getuid ());
237 return xstrdup ("/");
239 copy
= xmalloc (strlen (ptr
) + 2);
247 /* Find the first element of SEARCH_PATH which exists and is readable,
248 after expanding the %-escapes. Return 0 if we didn't find any, and
249 the path name of the one we found otherwise. */
252 search_magic_path (const char *search_path
, const char *class,
253 const char *escaped_suffix
)
257 for (s
= search_path
; *s
; s
= p
)
259 for (p
= s
; *p
&& *p
!= ':'; p
++)
264 XrmDatabase db
= magic_db (s
, p
- s
, class, escaped_suffix
);
270 static char const ns
[] = "%N%S";
271 XrmDatabase db
= magic_db (ns
, strlen (ns
), class, escaped_suffix
);
283 /* Producing databases for individual sources. */
286 get_system_app (const char *class)
290 path
= getenv ("XFILESEARCHPATH");
291 if (! path
) path
= PATH_X_DEFAULTS
;
293 return search_magic_path (path
, class, 0);
298 get_fallback (Display
*display
)
305 get_user_app (const char *class)
310 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
311 names, not directories. */
312 path
= getenv ("XUSERFILESEARCHPATH");
314 db
= search_magic_path (path
, class, 0);
318 /* Check for APPLRESDIR; it is a path of directories. In each,
319 we have to search for LANG/CLASS and then CLASS. */
320 path
= getenv ("XAPPLRESDIR");
323 db
= search_magic_path (path
, class, "/%L/%N");
325 db
= search_magic_path (path
, class, "/%N");
331 /* Check in the home directory. This is a bit of a hack; let's
332 hope one's home directory doesn't contain any %-escapes. */
333 char *home
= gethomedir ();
334 db
= search_magic_path (home
, class, "%L/%N");
336 db
= search_magic_path (home
, class, "%N");
345 get_user_db (Display
*display
)
350 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
351 xdefs
= XResourceManagerString (display
);
353 xdefs
= display
->xdefaults
;
357 db
= XrmGetStringDatabase (xdefs
);
363 home
= gethomedir ();
364 xdefault
= xmalloc (strlen (home
) + sizeof ".Xdefaults");
365 strcpy (xdefault
, home
);
366 strcat (xdefault
, ".Xdefaults");
367 db
= XrmGetFileDatabase (xdefault
);
372 #ifdef HAVE_XSCREENRESOURCESTRING
373 /* Get the screen-specific resources too. */
374 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
377 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
386 get_environ_db (void)
392 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
394 static char const xdefaults
[] = ".Xdefaults-";
395 char *home
= gethomedir ();
396 char const *host
= SSDATA (Vsystem_name
);
397 ptrdiff_t pathsize
= (strlen (home
) + sizeof xdefaults
398 + SBYTES (Vsystem_name
));
399 path
= xrealloc (home
, pathsize
);
400 strcat (strcat (path
, xdefaults
), host
);
404 db
= XrmGetFileDatabase (p
);
411 /* External interface. */
413 /* Types of values that we can find in a database */
415 #define XrmStringType "String" /* String representation */
416 static XrmRepresentation x_rm_string
; /* Quark representation */
418 /* Load X resources based on the display and a possible -xrm option. */
421 x_load_resources (Display
*display
, const char *xrm_string
,
422 const char *myname
, const char *myclass
)
424 XrmDatabase user_database
;
429 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
430 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
434 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
437 x_rm_string
= XrmStringToQuark (XrmStringType
);
438 #ifndef USE_X_TOOLKIT
439 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
440 I suspect it's because the toolkit version does this elsewhere. */
443 rdb
= XrmGetStringDatabase ("");
445 /* Add some font defaults. If the font `helv' doesn't exist, widgets
446 will use some other default font. */
449 sprintf (line
, "%s.pane.background: grey75", myclass
);
450 XrmPutLineResource (&rdb
, line
);
451 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
452 XrmPutLineResource (&rdb
, line
);
453 sprintf (line
, "%s*menu*background: grey75", myclass
);
454 XrmPutLineResource (&rdb
, line
);
455 sprintf (line
, "%s*menubar*background: grey75", myclass
);
456 XrmPutLineResource (&rdb
, line
);
457 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
458 XrmPutLineResource (&rdb
, line
);
459 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
460 XrmPutLineResource (&rdb
, line
);
461 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
462 XrmPutLineResource (&rdb
, line
);
463 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
464 XrmPutLineResource (&rdb
, line
);
465 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
466 XrmPutLineResource (&rdb
, line
);
467 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
468 XrmPutLineResource (&rdb
, line
);
469 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
470 XrmPutLineResource (&rdb
, line
);
471 sprintf (line
, "%s*fsb*background: grey75", myclass
);
472 XrmPutLineResource (&rdb
, line
);
473 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
474 XrmPutLineResource (&rdb
, line
);
475 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
476 XrmPutLineResource (&rdb
, line
);
477 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
478 XrmPutLineResource (&rdb
, line
);
479 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
480 XrmPutLineResource (&rdb
, line
);
482 /* Set double click time of list boxes in the file selection
483 dialog from `double-click-time'. */
484 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
486 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
487 myclass
, XFASTINT (Vdouble_click_time
));
488 XrmPutLineResource (&rdb
, line
);
489 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
490 myclass
, XFASTINT (Vdouble_click_time
));
491 XrmPutLineResource (&rdb
, line
);
494 #else /* not USE_MOTIF */
496 sprintf (line
, "Emacs.dialog*.background: grey75");
497 XrmPutLineResource (&rdb
, line
);
498 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
499 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
500 XrmPutLineResource (&rdb
, line
);
501 sprintf (line
, "*XlwMenu*font: %s", helv
);
502 XrmPutLineResource (&rdb
, line
);
504 sprintf (line
, "*XlwMenu*background: grey75");
505 XrmPutLineResource (&rdb
, line
);
506 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
507 XrmPutLineResource (&rdb
, line
);
509 #endif /* not USE_MOTIF */
511 user_database
= get_user_db (display
);
513 /* Figure out what the "customization string" is, so we can use it
515 xfree (x_customization_string
);
516 x_customization_string
517 = x_get_customization_string (user_database
, myname
, myclass
);
519 /* Get application system defaults */
520 db
= get_system_app (myclass
);
522 XrmMergeDatabases (db
, &rdb
);
524 /* Get Fallback resources */
525 db
= get_fallback (display
);
527 XrmMergeDatabases (db
, &rdb
);
529 /* Get application user defaults */
530 db
= get_user_app (myclass
);
532 XrmMergeDatabases (db
, &rdb
);
534 /* get User defaults */
535 if (user_database
!= NULL
)
536 XrmMergeDatabases (user_database
, &rdb
);
538 /* Get Environment defaults. */
539 db
= get_environ_db ();
541 XrmMergeDatabases (db
, &rdb
);
543 /* Last, merge in any specification from the command line. */
544 if (xrm_string
!= NULL
)
546 db
= XrmGetStringDatabase (xrm_string
);
548 XrmMergeDatabases (db
, &rdb
);
555 /* Retrieve the value of the resource specified by NAME with class CLASS
556 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
559 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
560 XrmRepresentation expected_type
, XrmValue
*ret_value
)
563 XrmName namelist
[100];
564 XrmClass classlist
[100];
565 XrmRepresentation type
;
567 XrmStringToNameList (name
, namelist
);
568 XrmStringToClassList (class, classlist
);
570 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
571 && (type
== expected_type
))
573 if (type
== x_rm_string
)
574 ret_value
->addr
= (char *) value
.addr
;
576 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
584 /* Retrieve the string resource specified by NAME with CLASS from
588 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
592 if (inhibit_x_resources
)
593 /* --quick was passed, so this is a no-op. */
596 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
597 return (char *) value
.addr
;
602 /* Stand-alone test facilities. */
607 #define arg_listify(len, list) (list)
608 #define car(list) (*(list))
609 #define cdr(list) (list + 1)
610 #define NIL(list) (! *(list))
611 #define free_arglist(list)
614 member (char *elt
, List list
)
618 for (p
= list
; ! NIL (p
); p
= cdr (p
))
619 if (! strcmp (elt
, car (p
)))
626 fatal (char *msg
, char *prog
)
628 fprintf (stderr
, msg
, prog
);
633 main (int argc
, char **argv
)
636 char *displayname
, *resource_string
, *class, *name
;
640 arg_list
= arg_listify (argc
, argv
);
642 lp
= member ("-d", arg_list
);
644 displayname
= car (cdr (lp
));
646 displayname
= "localhost:0.0";
648 lp
= member ("-xrm", arg_list
);
649 resource_string
= NIL (lp
) ? 0 : car (cdr (lp
));
651 lp
= member ("-c", arg_list
);
653 class = car (cdr (lp
));
657 lp
= member ("-n", arg_list
);
659 name
= car (cdr (lp
));
663 free_arglist (arg_list
);
665 if (!(display
= XOpenDisplay (displayname
)))
666 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
668 xdb
= x_load_resources (display
, resource_string
, name
, class);
670 /* In a real program, you'd want to also do this: */
676 char query_class
[90];
681 if (strlen (query_name
))
688 value
= x_get_string_resource (xdb
, query_name
, query_class
);
691 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
693 printf ("\tNo Value.\n\n");
698 printf ("\tExit.\n\n");
700 XCloseDisplay (display
);