1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2018 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 (at
13 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 <https://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>
46 /* X file search path processing. */
49 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
50 and friends, or zero if none was specified. */
51 static char *x_customization_string
;
54 /* Return the value of the emacs.customization (Emacs.Customization)
55 resource, for later use in search path decoding. If we find no
56 such resource, return zero. */
58 x_get_customization_string (XrmDatabase db
, const char *name
,
61 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
62 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
65 sprintf (full_name
, "%s.%s", name
, "customization");
66 sprintf (full_class
, "%s.%s", class, "Customization");
68 result
= x_get_string_resource (db
, full_name
, full_class
);
69 return result
? xstrdup (result
) : NULL
;
72 /* Expand all the Xt-style %-escapes in STRING, whose length is given
73 by STRING_LEN. Here are the escapes we're supposed to recognize:
75 %N The value of the application's class name
76 %T The value of the type parameter ("app-defaults" in this
78 %S The value of the suffix parameter ("" in this context)
79 %L The language string associated with the specified display
80 (We use the "LANG" environment variable here, if it's set.)
81 %l The language part of the display's language string
82 (We treat this just like %L. If someone can tell us what
83 we're really supposed to do, dandy.)
84 %t The territory part of the display's language string
85 (This never gets used.)
86 %c The codeset part of the display's language string
87 (This never gets used either.)
88 %C The customization string retrieved from the resource
89 database associated with display.
90 (This is x_customization_string.)
92 Return the resource database if its file was read successfully, and
93 refers to %L only when the LANG environment variable is set, or
94 otherwise provided by X.
96 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
97 %-escapes in ESCAPED_SUFFIX are expanded.
99 Return NULL otherwise. */
102 magic_db (const char *string
, ptrdiff_t string_len
, const char *class,
103 const char *escaped_suffix
)
106 char *lang
= getenv ("LANG");
108 ptrdiff_t path_size
= 100;
109 char *path
= xmalloc (path_size
);
110 ptrdiff_t path_len
= 0;
112 const char *p
= string
;
114 while (p
< string
+ string_len
)
116 /* The chunk we're about to stick on the end of result. */
117 const char *next
= p
;
118 ptrdiff_t next_len
= 1;
124 if (p
>= string
+ string_len
)
135 if (x_customization_string
)
137 next
= x_customization_string
;
138 next_len
= strlen (next
);
146 next_len
= strlen (class);
150 next
= "app-defaults";
151 next_len
= strlen (next
);
168 next_len
= strlen (next
);
178 /* Do we have room for this component followed by a '\0'? */
179 if (path_size
- path_len
<= next_len
)
180 path
= xpalloc (path
, &path_size
, path_len
- path_size
+ next_len
+ 1,
183 memcpy (path
+ path_len
, next
, next_len
);
184 path_len
+= next_len
;
188 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
189 if (p
>= string
+ string_len
&& escaped_suffix
)
191 string
= escaped_suffix
;
192 string_len
= strlen (string
);
194 escaped_suffix
= NULL
;
198 path
[path_len
] = '\0';
199 db
= XrmGetFileDatabase (path
);
212 if ((ptr
= getenv ("HOME")) == NULL
)
214 if ((ptr
= getenv ("LOGNAME")) != NULL
215 || (ptr
= getenv ("USER")) != NULL
)
218 pw
= getpwuid (getuid ());
225 return xstrdup ("/");
227 ptrdiff_t len
= strlen (ptr
);
228 copy
= xmalloc (len
+ 2);
229 strcpy (copy
+ len
, "/");
230 return memcpy (copy
, ptr
, len
);
234 /* Find the first element of SEARCH_PATH which exists and is readable,
235 after expanding the %-escapes. Return 0 if we didn't find any, and
236 the path name of the one we found otherwise. */
239 search_magic_path (const char *search_path
, const char *class,
240 const char *escaped_suffix
)
244 for (s
= search_path
; *s
; s
= p
)
246 for (p
= s
; *p
&& *p
!= ':'; p
++)
251 XrmDatabase db
= magic_db (s
, p
- s
, class, escaped_suffix
);
257 static char const ns
[] = "%N%S";
258 XrmDatabase db
= magic_db (ns
, strlen (ns
), class, escaped_suffix
);
270 /* Producing databases for individual sources. */
273 get_system_app (const char *class)
277 path
= getenv ("XFILESEARCHPATH");
278 if (! path
) path
= PATH_X_DEFAULTS
;
280 return search_magic_path (path
, class, 0);
285 get_fallback (Display
*display
)
292 get_user_app (const char *class)
297 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
298 names, not directories. */
299 path
= getenv ("XUSERFILESEARCHPATH");
301 db
= search_magic_path (path
, class, 0);
305 /* Check for APPLRESDIR; it is a path of directories. In each,
306 we have to search for LANG/CLASS and then CLASS. */
307 path
= getenv ("XAPPLRESDIR");
310 db
= search_magic_path (path
, class, "/%L/%N");
312 db
= search_magic_path (path
, class, "/%N");
318 /* Check in the home directory. This is a bit of a hack; let's
319 hope one's home directory doesn't contain any %-escapes. */
320 char *home
= gethomedir ();
321 db
= search_magic_path (home
, class, "%L/%N");
323 db
= search_magic_path (home
, class, "%N");
330 static char const xdefaults
[] = ".Xdefaults";
333 get_user_db (Display
*display
)
338 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
339 xdefs
= XResourceManagerString (display
);
341 xdefs
= display
->xdefaults
;
345 db
= XrmGetStringDatabase (xdefs
);
348 /* Use ~/.Xdefaults. */
349 char *home
= gethomedir ();
350 ptrdiff_t homelen
= strlen (home
);
351 char *filename
= xrealloc (home
, homelen
+ sizeof xdefaults
);
352 strcpy (filename
+ homelen
, xdefaults
);
353 db
= XrmGetFileDatabase (filename
);
357 #ifdef HAVE_XSCREENRESOURCESTRING
358 /* Get the screen-specific resources too. */
359 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
362 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
371 get_environ_db (void)
374 char *p
= getenv ("XENVIRONMENT");
379 Lisp_Object system_name
= Fsystem_name ();
380 if (STRINGP (system_name
))
382 /* Use ~/.Xdefaults-HOSTNAME. */
383 char *home
= gethomedir ();
384 ptrdiff_t homelen
= strlen (home
);
385 ptrdiff_t filenamesize
= (homelen
+ sizeof xdefaults
386 + 1 + SBYTES (system_name
));
387 p
= filename
= xrealloc (home
, filenamesize
);
388 lispstpcpy (stpcpy (stpcpy (filename
+ homelen
, xdefaults
), "-"),
393 db
= XrmGetFileDatabase (p
);
400 /* External interface. */
402 /* Types of values that we can find in a database */
404 #define XrmStringType "String" /* String representation */
405 static XrmRepresentation x_rm_string
; /* Quark representation */
407 /* Load X resources based on the display and a possible -xrm option. */
410 x_load_resources (Display
*display
, const char *xrm_string
,
411 const char *myname
, const char *myclass
)
413 XrmDatabase user_database
;
418 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
419 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
423 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
426 x_rm_string
= XrmStringToQuark (XrmStringType
);
427 #ifndef USE_X_TOOLKIT
428 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
429 I suspect it's because the toolkit version does this elsewhere. */
432 rdb
= XrmGetStringDatabase ("");
434 /* Add some font defaults. If the font `helv' doesn't exist, widgets
435 will use some other default font. */
438 sprintf (line
, "%s.pane.background: grey75", myclass
);
439 XrmPutLineResource (&rdb
, line
);
440 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
441 XrmPutLineResource (&rdb
, line
);
442 sprintf (line
, "%s*menu*background: grey75", myclass
);
443 XrmPutLineResource (&rdb
, line
);
444 sprintf (line
, "%s*menubar*background: grey75", myclass
);
445 XrmPutLineResource (&rdb
, line
);
446 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
447 XrmPutLineResource (&rdb
, line
);
448 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
449 XrmPutLineResource (&rdb
, line
);
450 sprintf (line
, "%s*horizontalScrollBar.background: grey75", myclass
);
451 XrmPutLineResource (&rdb
, line
);
452 sprintf (line
, "%s*horizontalScrollBar.troughColor: grey75", myclass
);
453 XrmPutLineResource (&rdb
, line
);
454 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
455 XrmPutLineResource (&rdb
, line
);
456 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
457 XrmPutLineResource (&rdb
, line
);
458 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
459 XrmPutLineResource (&rdb
, line
);
460 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
461 XrmPutLineResource (&rdb
, line
);
462 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
463 XrmPutLineResource (&rdb
, line
);
464 sprintf (line
, "%s*fsb*background: grey75", myclass
);
465 XrmPutLineResource (&rdb
, line
);
466 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
467 XrmPutLineResource (&rdb
, line
);
468 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
469 XrmPutLineResource (&rdb
, line
);
470 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
471 XrmPutLineResource (&rdb
, line
);
472 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
473 XrmPutLineResource (&rdb
, line
);
475 /* Set double click time of list boxes in the file selection
476 dialog from `double-click-time'. */
477 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
479 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
480 myclass
, XFASTINT (Vdouble_click_time
));
481 XrmPutLineResource (&rdb
, line
);
482 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
483 myclass
, XFASTINT (Vdouble_click_time
));
484 XrmPutLineResource (&rdb
, line
);
487 #else /* not USE_MOTIF */
489 sprintf (line
, "Emacs.dialog*.background: grey75");
490 XrmPutLineResource (&rdb
, line
);
491 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
492 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
493 XrmPutLineResource (&rdb
, line
);
494 sprintf (line
, "*XlwMenu*font: %s", helv
);
495 XrmPutLineResource (&rdb
, line
);
497 sprintf (line
, "*XlwMenu*background: grey75");
498 XrmPutLineResource (&rdb
, line
);
499 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
500 XrmPutLineResource (&rdb
, line
);
501 sprintf (line
, "Emacs*horizontalScrollBar.background: grey75");
502 XrmPutLineResource (&rdb
, line
);
504 #endif /* not USE_MOTIF */
506 user_database
= get_user_db (display
);
508 /* Figure out what the "customization string" is, so we can use it
510 xfree (x_customization_string
);
511 x_customization_string
512 = x_get_customization_string (user_database
, myname
, myclass
);
514 /* Get application system defaults */
515 db
= get_system_app (myclass
);
517 XrmMergeDatabases (db
, &rdb
);
519 /* Get Fallback resources */
520 db
= get_fallback (display
);
522 XrmMergeDatabases (db
, &rdb
);
524 /* Get application user defaults */
525 db
= get_user_app (myclass
);
527 XrmMergeDatabases (db
, &rdb
);
529 /* get User defaults */
530 if (user_database
!= NULL
)
531 XrmMergeDatabases (user_database
, &rdb
);
533 /* Get Environment defaults. */
534 db
= get_environ_db ();
536 XrmMergeDatabases (db
, &rdb
);
538 /* Last, merge in any specification from the command line. */
539 if (xrm_string
!= NULL
)
541 db
= XrmGetStringDatabase (xrm_string
);
543 XrmMergeDatabases (db
, &rdb
);
550 /* Retrieve the value of the resource specified by NAME with class CLASS
551 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
554 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
555 XrmRepresentation expected_type
, XrmValue
*ret_value
)
558 XrmName namelist
[100];
559 XrmClass classlist
[100];
560 XrmRepresentation type
;
562 XrmStringToNameList (name
, namelist
);
563 XrmStringToClassList (class, classlist
);
565 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
566 && (type
== expected_type
))
568 if (type
== x_rm_string
)
569 ret_value
->addr
= (char *) value
.addr
;
571 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
579 /* Retrieve the string resource specified by NAME with CLASS from
583 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
587 if (inhibit_x_resources
)
588 /* --quick was passed, so this is a no-op. */
591 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
592 return (char *) value
.addr
;
597 /* Stand-alone test facilities. */
602 #define arg_listify(len, list) (list)
603 #define car(list) (*(list))
604 #define cdr(list) (list + 1)
605 #define NIL(list) (! *(list))
606 #define free_arglist(list)
609 member (char *elt
, List list
)
613 for (p
= list
; ! NIL (p
); p
= cdr (p
))
614 if (! strcmp (elt
, car (p
)))
621 fatal (char *msg
, char *prog
)
623 fprintf (stderr
, msg
, prog
);
628 main (int argc
, char **argv
)
631 char *displayname
, *resource_string
, *class, *name
;
635 arg_list
= arg_listify (argc
, argv
);
637 lp
= member ("-d", arg_list
);
639 displayname
= car (cdr (lp
));
641 displayname
= "localhost:0.0";
643 lp
= member ("-xrm", arg_list
);
644 resource_string
= NIL (lp
) ? 0 : car (cdr (lp
));
646 lp
= member ("-c", arg_list
);
648 class = car (cdr (lp
));
652 lp
= member ("-n", arg_list
);
654 name
= car (cdr (lp
));
658 free_arglist (arg_list
);
660 if (!(display
= XOpenDisplay (displayname
)))
661 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
663 xdb
= x_load_resources (display
, resource_string
, name
, class);
665 /* In a real program, you'd want to also do this: */
671 char query_class
[90];
676 if (strlen (query_name
))
683 value
= x_get_string_resource (xdb
, query_name
, query_class
);
686 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
688 printf ("\tNo Value.\n\n");
693 printf ("\tExit.\n\n");
695 XCloseDisplay (display
);