1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2015 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>
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
)
181 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
182 memory_full (SIZE_MAX
);
183 path_size
= (path_len
+ next_len
+ 1) * 2;
184 path
= xrealloc (path
, path_size
);
187 memcpy (path
+ path_len
, next
, next_len
);
188 path_len
+= next_len
;
192 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
193 if (p
>= string
+ string_len
&& escaped_suffix
)
195 string
= escaped_suffix
;
196 string_len
= strlen (string
);
198 escaped_suffix
= NULL
;
202 path
[path_len
] = '\0';
203 db
= XrmGetFileDatabase (path
);
216 if ((ptr
= getenv ("HOME")) == NULL
)
218 if ((ptr
= getenv ("LOGNAME")) != NULL
219 || (ptr
= getenv ("USER")) != NULL
)
222 pw
= getpwuid (getuid ());
229 return xstrdup ("/");
231 ptrdiff_t len
= strlen (ptr
);
232 copy
= xmalloc (len
+ 2);
233 strcpy (copy
+ len
, "/");
234 return memcpy (copy
, ptr
, len
);
238 /* Find the first element of SEARCH_PATH which exists and is readable,
239 after expanding the %-escapes. Return 0 if we didn't find any, and
240 the path name of the one we found otherwise. */
243 search_magic_path (const char *search_path
, const char *class,
244 const char *escaped_suffix
)
248 for (s
= search_path
; *s
; s
= p
)
250 for (p
= s
; *p
&& *p
!= ':'; p
++)
255 XrmDatabase db
= magic_db (s
, p
- s
, class, escaped_suffix
);
261 static char const ns
[] = "%N%S";
262 XrmDatabase db
= magic_db (ns
, strlen (ns
), class, escaped_suffix
);
274 /* Producing databases for individual sources. */
277 get_system_app (const char *class)
281 path
= getenv ("XFILESEARCHPATH");
282 if (! path
) path
= PATH_X_DEFAULTS
;
284 return search_magic_path (path
, class, 0);
289 get_fallback (Display
*display
)
296 get_user_app (const char *class)
301 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
302 names, not directories. */
303 path
= getenv ("XUSERFILESEARCHPATH");
305 db
= search_magic_path (path
, class, 0);
309 /* Check for APPLRESDIR; it is a path of directories. In each,
310 we have to search for LANG/CLASS and then CLASS. */
311 path
= getenv ("XAPPLRESDIR");
314 db
= search_magic_path (path
, class, "/%L/%N");
316 db
= search_magic_path (path
, class, "/%N");
322 /* Check in the home directory. This is a bit of a hack; let's
323 hope one's home directory doesn't contain any %-escapes. */
324 char *home
= gethomedir ();
325 db
= search_magic_path (home
, class, "%L/%N");
327 db
= search_magic_path (home
, class, "%N");
334 static char const xdefaults
[] = ".Xdefaults";
337 get_user_db (Display
*display
)
342 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
343 xdefs
= XResourceManagerString (display
);
345 xdefs
= display
->xdefaults
;
349 db
= XrmGetStringDatabase (xdefs
);
352 char *home
= gethomedir ();
353 ptrdiff_t homelen
= strlen (home
);
354 char *filename
= xrealloc (home
, homelen
+ sizeof xdefaults
);
355 strcpy (filename
+ homelen
, xdefaults
);
356 db
= XrmGetFileDatabase (filename
);
360 #ifdef HAVE_XSCREENRESOURCESTRING
361 /* Get the screen-specific resources too. */
362 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
365 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
374 get_environ_db (void)
377 char *p
= getenv ("XENVIRONMENT");
382 char *home
= gethomedir ();
383 ptrdiff_t homelen
= strlen (home
);
384 Lisp_Object system_name
= Fsystem_name ();
385 ptrdiff_t filenamesize
= (homelen
+ sizeof xdefaults
386 + SBYTES (system_name
));
387 p
= filename
= xrealloc (home
, filenamesize
);
388 lispstpcpy (stpcpy (filename
+ homelen
, xdefaults
), system_name
);
391 db
= XrmGetFileDatabase (p
);
398 /* External interface. */
400 /* Types of values that we can find in a database */
402 #define XrmStringType "String" /* String representation */
403 static XrmRepresentation x_rm_string
; /* Quark representation */
405 /* Load X resources based on the display and a possible -xrm option. */
408 x_load_resources (Display
*display
, const char *xrm_string
,
409 const char *myname
, const char *myclass
)
411 XrmDatabase user_database
;
416 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
417 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
421 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
424 x_rm_string
= XrmStringToQuark (XrmStringType
);
425 #ifndef USE_X_TOOLKIT
426 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
427 I suspect it's because the toolkit version does this elsewhere. */
430 rdb
= XrmGetStringDatabase ("");
432 /* Add some font defaults. If the font `helv' doesn't exist, widgets
433 will use some other default font. */
436 sprintf (line
, "%s.pane.background: grey75", myclass
);
437 XrmPutLineResource (&rdb
, line
);
438 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
439 XrmPutLineResource (&rdb
, line
);
440 sprintf (line
, "%s*menu*background: grey75", myclass
);
441 XrmPutLineResource (&rdb
, line
);
442 sprintf (line
, "%s*menubar*background: grey75", myclass
);
443 XrmPutLineResource (&rdb
, line
);
444 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
445 XrmPutLineResource (&rdb
, line
);
446 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
447 XrmPutLineResource (&rdb
, line
);
448 sprintf (line
, "%s*horizontalScrollBar.background: grey75", myclass
);
449 XrmPutLineResource (&rdb
, line
);
450 sprintf (line
, "%s*horizontalScrollBar.troughColor: grey75", myclass
);
451 XrmPutLineResource (&rdb
, line
);
452 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
453 XrmPutLineResource (&rdb
, line
);
454 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
455 XrmPutLineResource (&rdb
, line
);
456 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
457 XrmPutLineResource (&rdb
, line
);
458 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
459 XrmPutLineResource (&rdb
, line
);
460 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
461 XrmPutLineResource (&rdb
, line
);
462 sprintf (line
, "%s*fsb*background: grey75", myclass
);
463 XrmPutLineResource (&rdb
, line
);
464 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
465 XrmPutLineResource (&rdb
, line
);
466 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
467 XrmPutLineResource (&rdb
, line
);
468 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
469 XrmPutLineResource (&rdb
, line
);
470 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
471 XrmPutLineResource (&rdb
, line
);
473 /* Set double click time of list boxes in the file selection
474 dialog from `double-click-time'. */
475 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
477 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
478 myclass
, XFASTINT (Vdouble_click_time
));
479 XrmPutLineResource (&rdb
, line
);
480 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
481 myclass
, XFASTINT (Vdouble_click_time
));
482 XrmPutLineResource (&rdb
, line
);
485 #else /* not USE_MOTIF */
487 sprintf (line
, "Emacs.dialog*.background: grey75");
488 XrmPutLineResource (&rdb
, line
);
489 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
490 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
491 XrmPutLineResource (&rdb
, line
);
492 sprintf (line
, "*XlwMenu*font: %s", helv
);
493 XrmPutLineResource (&rdb
, line
);
495 sprintf (line
, "*XlwMenu*background: grey75");
496 XrmPutLineResource (&rdb
, line
);
497 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
498 XrmPutLineResource (&rdb
, line
);
499 sprintf (line
, "Emacs*horizontalScrollBar.background: grey75");
500 XrmPutLineResource (&rdb
, line
);
502 #endif /* not USE_MOTIF */
504 user_database
= get_user_db (display
);
506 /* Figure out what the "customization string" is, so we can use it
508 xfree (x_customization_string
);
509 x_customization_string
510 = x_get_customization_string (user_database
, myname
, myclass
);
512 /* Get application system defaults */
513 db
= get_system_app (myclass
);
515 XrmMergeDatabases (db
, &rdb
);
517 /* Get Fallback resources */
518 db
= get_fallback (display
);
520 XrmMergeDatabases (db
, &rdb
);
522 /* Get application user defaults */
523 db
= get_user_app (myclass
);
525 XrmMergeDatabases (db
, &rdb
);
527 /* get User defaults */
528 if (user_database
!= NULL
)
529 XrmMergeDatabases (user_database
, &rdb
);
531 /* Get Environment defaults. */
532 db
= get_environ_db ();
534 XrmMergeDatabases (db
, &rdb
);
536 /* Last, merge in any specification from the command line. */
537 if (xrm_string
!= NULL
)
539 db
= XrmGetStringDatabase (xrm_string
);
541 XrmMergeDatabases (db
, &rdb
);
548 /* Retrieve the value of the resource specified by NAME with class CLASS
549 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
552 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
553 XrmRepresentation expected_type
, XrmValue
*ret_value
)
556 XrmName namelist
[100];
557 XrmClass classlist
[100];
558 XrmRepresentation type
;
560 XrmStringToNameList (name
, namelist
);
561 XrmStringToClassList (class, classlist
);
563 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
564 && (type
== expected_type
))
566 if (type
== x_rm_string
)
567 ret_value
->addr
= (char *) value
.addr
;
569 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
577 /* Retrieve the string resource specified by NAME with CLASS from
581 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
585 if (inhibit_x_resources
)
586 /* --quick was passed, so this is a no-op. */
589 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
590 return (char *) value
.addr
;
595 /* Stand-alone test facilities. */
600 #define arg_listify(len, list) (list)
601 #define car(list) (*(list))
602 #define cdr(list) (list + 1)
603 #define NIL(list) (! *(list))
604 #define free_arglist(list)
607 member (char *elt
, List list
)
611 for (p
= list
; ! NIL (p
); p
= cdr (p
))
612 if (! strcmp (elt
, car (p
)))
619 fatal (char *msg
, char *prog
)
621 fprintf (stderr
, msg
, prog
);
626 main (int argc
, char **argv
)
629 char *displayname
, *resource_string
, *class, *name
;
633 arg_list
= arg_listify (argc
, argv
);
635 lp
= member ("-d", arg_list
);
637 displayname
= car (cdr (lp
));
639 displayname
= "localhost:0.0";
641 lp
= member ("-xrm", arg_list
);
642 resource_string
= NIL (lp
) ? 0 : car (cdr (lp
));
644 lp
= member ("-c", arg_list
);
646 class = car (cdr (lp
));
650 lp
= member ("-n", arg_list
);
652 name
= car (cdr (lp
));
656 free_arglist (arg_list
);
658 if (!(display
= XOpenDisplay (displayname
)))
659 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
661 xdb
= x_load_resources (display
, resource_string
, name
, class);
663 /* In a real program, you'd want to also do this: */
669 char query_class
[90];
674 if (strlen (query_name
))
681 value
= x_get_string_resource (xdb
, query_name
, query_class
);
684 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
686 printf ("\tNo Value.\n\n");
691 printf ("\tExit.\n\n");
693 XCloseDisplay (display
);