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>
47 /* For Vdouble_click_time. */
51 /* X file search path processing. */
54 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
55 and friends, or zero if none was specified. */
56 static char *x_customization_string
;
59 /* Return the value of the emacs.customization (Emacs.Customization)
60 resource, for later use in search path decoding. If we find no
61 such resource, return zero. */
63 x_get_customization_string (XrmDatabase db
, const char *name
,
66 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
67 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
70 sprintf (full_name
, "%s.%s", name
, "customization");
71 sprintf (full_class
, "%s.%s", class, "Customization");
73 result
= x_get_string_resource (db
, full_name
, full_class
);
74 return result
? xstrdup (result
) : NULL
;
77 /* Expand all the Xt-style %-escapes in STRING, whose length is given
78 by STRING_LEN. Here are the escapes we're supposed to recognize:
80 %N The value of the application's class name
81 %T The value of the type parameter ("app-defaults" in this
83 %S The value of the suffix parameter ("" in this context)
84 %L The language string associated with the specified display
85 (We use the "LANG" environment variable here, if it's set.)
86 %l The language part of the display's language string
87 (We treat this just like %L. If someone can tell us what
88 we're really supposed to do, dandy.)
89 %t The territory part of the display's language string
90 (This never gets used.)
91 %c The codeset part of the display's language string
92 (This never gets used either.)
93 %C The customization string retrieved from the resource
94 database associated with display.
95 (This is x_customization_string.)
97 Return the resource database if its file was read successfully, and
98 refers to %L only when the LANG environment variable is set, or
99 otherwise provided by X.
101 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
102 %-escapes in ESCAPED_SUFFIX are expanded.
104 Return NULL otherwise. */
107 magic_db (const char *string
, ptrdiff_t string_len
, const char *class,
108 const char *escaped_suffix
)
111 char *lang
= getenv ("LANG");
113 ptrdiff_t path_size
= 100;
114 char *path
= xmalloc (path_size
);
115 ptrdiff_t path_len
= 0;
117 const char *p
= string
;
119 while (p
< string
+ string_len
)
121 /* The chunk we're about to stick on the end of result. */
122 const char *next
= NULL
;
129 if (p
>= string
+ string_len
)
140 next
= (x_customization_string
141 ? x_customization_string
143 next_len
= strlen (next
);
148 next_len
= strlen (class);
152 next
= "app-defaults";
153 next_len
= strlen (next
);
170 next_len
= strlen (next
);
180 next
= p
, next_len
= 1;
182 /* Do we have room for this component followed by a '\0'? */
183 if (path_size
- path_len
<= next_len
)
185 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
186 memory_full (SIZE_MAX
);
187 path_size
= (path_len
+ next_len
+ 1) * 2;
188 path
= xrealloc (path
, path_size
);
191 memcpy (path
+ path_len
, next
, next_len
);
192 path_len
+= next_len
;
196 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
197 if (p
>= string
+ string_len
&& escaped_suffix
)
199 string
= escaped_suffix
;
200 string_len
= strlen (string
);
202 escaped_suffix
= NULL
;
206 path
[path_len
] = '\0';
207 db
= XrmGetFileDatabase (path
);
220 if ((ptr
= getenv ("HOME")) == NULL
)
222 if ((ptr
= getenv ("LOGNAME")) != NULL
223 || (ptr
= getenv ("USER")) != NULL
)
226 pw
= getpwuid (getuid ());
233 return xstrdup ("/");
235 ptrdiff_t len
= strlen (ptr
);
236 copy
= xmalloc (len
+ 2);
237 strcpy (copy
+ len
, "/");
238 return memcpy (copy
, ptr
, len
);
242 /* Find the first element of SEARCH_PATH which exists and is readable,
243 after expanding the %-escapes. Return 0 if we didn't find any, and
244 the path name of the one we found otherwise. */
247 search_magic_path (const char *search_path
, const char *class,
248 const char *escaped_suffix
)
252 for (s
= search_path
; *s
; s
= p
)
254 for (p
= s
; *p
&& *p
!= ':'; p
++)
259 XrmDatabase db
= magic_db (s
, p
- s
, class, escaped_suffix
);
265 static char const ns
[] = "%N%S";
266 XrmDatabase db
= magic_db (ns
, strlen (ns
), class, escaped_suffix
);
278 /* Producing databases for individual sources. */
281 get_system_app (const char *class)
285 path
= getenv ("XFILESEARCHPATH");
286 if (! path
) path
= PATH_X_DEFAULTS
;
288 return search_magic_path (path
, class, 0);
293 get_fallback (Display
*display
)
300 get_user_app (const char *class)
305 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
306 names, not directories. */
307 path
= getenv ("XUSERFILESEARCHPATH");
309 db
= search_magic_path (path
, class, 0);
313 /* Check for APPLRESDIR; it is a path of directories. In each,
314 we have to search for LANG/CLASS and then CLASS. */
315 path
= getenv ("XAPPLRESDIR");
318 db
= search_magic_path (path
, class, "/%L/%N");
320 db
= search_magic_path (path
, class, "/%N");
326 /* Check in the home directory. This is a bit of a hack; let's
327 hope one's home directory doesn't contain any %-escapes. */
328 char *home
= gethomedir ();
329 db
= search_magic_path (home
, class, "%L/%N");
331 db
= search_magic_path (home
, class, "%N");
338 static char const xdefaults
[] = ".Xdefaults";
341 get_user_db (Display
*display
)
346 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
347 xdefs
= XResourceManagerString (display
);
349 xdefs
= display
->xdefaults
;
353 db
= XrmGetStringDatabase (xdefs
);
356 char *home
= gethomedir ();
357 ptrdiff_t homelen
= strlen (home
);
358 char *filename
= xrealloc (home
, homelen
+ sizeof xdefaults
);
359 strcpy (filename
+ homelen
, xdefaults
);
360 db
= XrmGetFileDatabase (filename
);
364 #ifdef HAVE_XSCREENRESOURCESTRING
365 /* Get the screen-specific resources too. */
366 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
369 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
378 get_environ_db (void)
381 char *p
= getenv ("XENVIRONMENT");
386 char *home
= gethomedir ();
387 ptrdiff_t homelen
= strlen (home
);
388 Lisp_Object system_name
= Fsystem_name ();
389 ptrdiff_t filenamesize
= (homelen
+ sizeof xdefaults
390 + SBYTES (system_name
));
391 p
= filename
= xrealloc (home
, filenamesize
);
392 lispstpcpy (stpcpy (filename
+ homelen
, xdefaults
), system_name
);
395 db
= XrmGetFileDatabase (p
);
402 /* External interface. */
404 /* Types of values that we can find in a database */
406 #define XrmStringType "String" /* String representation */
407 static XrmRepresentation x_rm_string
; /* Quark representation */
409 /* Load X resources based on the display and a possible -xrm option. */
412 x_load_resources (Display
*display
, const char *xrm_string
,
413 const char *myname
, const char *myclass
)
415 XrmDatabase user_database
;
420 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
421 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
425 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
428 x_rm_string
= XrmStringToQuark (XrmStringType
);
429 #ifndef USE_X_TOOLKIT
430 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
431 I suspect it's because the toolkit version does this elsewhere. */
434 rdb
= XrmGetStringDatabase ("");
436 /* Add some font defaults. If the font `helv' doesn't exist, widgets
437 will use some other default font. */
440 sprintf (line
, "%s.pane.background: grey75", myclass
);
441 XrmPutLineResource (&rdb
, line
);
442 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
443 XrmPutLineResource (&rdb
, line
);
444 sprintf (line
, "%s*menu*background: grey75", myclass
);
445 XrmPutLineResource (&rdb
, line
);
446 sprintf (line
, "%s*menubar*background: grey75", myclass
);
447 XrmPutLineResource (&rdb
, line
);
448 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
449 XrmPutLineResource (&rdb
, line
);
450 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
451 XrmPutLineResource (&rdb
, line
);
452 sprintf (line
, "%s*horizontalScrollBar.background: grey75", myclass
);
453 XrmPutLineResource (&rdb
, line
);
454 sprintf (line
, "%s*horizontalScrollBar.troughColor: grey75", myclass
);
455 XrmPutLineResource (&rdb
, line
);
456 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
457 XrmPutLineResource (&rdb
, line
);
458 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
459 XrmPutLineResource (&rdb
, line
);
460 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
461 XrmPutLineResource (&rdb
, line
);
462 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
463 XrmPutLineResource (&rdb
, line
);
464 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
465 XrmPutLineResource (&rdb
, line
);
466 sprintf (line
, "%s*fsb*background: grey75", myclass
);
467 XrmPutLineResource (&rdb
, line
);
468 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
469 XrmPutLineResource (&rdb
, line
);
470 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
471 XrmPutLineResource (&rdb
, line
);
472 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
473 XrmPutLineResource (&rdb
, line
);
474 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
475 XrmPutLineResource (&rdb
, line
);
477 /* Set double click time of list boxes in the file selection
478 dialog from `double-click-time'. */
479 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
481 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
482 myclass
, XFASTINT (Vdouble_click_time
));
483 XrmPutLineResource (&rdb
, line
);
484 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
485 myclass
, XFASTINT (Vdouble_click_time
));
486 XrmPutLineResource (&rdb
, line
);
489 #else /* not USE_MOTIF */
491 sprintf (line
, "Emacs.dialog*.background: grey75");
492 XrmPutLineResource (&rdb
, line
);
493 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
494 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
495 XrmPutLineResource (&rdb
, line
);
496 sprintf (line
, "*XlwMenu*font: %s", helv
);
497 XrmPutLineResource (&rdb
, line
);
499 sprintf (line
, "*XlwMenu*background: grey75");
500 XrmPutLineResource (&rdb
, line
);
501 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
502 XrmPutLineResource (&rdb
, line
);
503 sprintf (line
, "Emacs*horizontalScrollBar.background: grey75");
504 XrmPutLineResource (&rdb
, line
);
506 #endif /* not USE_MOTIF */
508 user_database
= get_user_db (display
);
510 /* Figure out what the "customization string" is, so we can use it
512 xfree (x_customization_string
);
513 x_customization_string
514 = x_get_customization_string (user_database
, myname
, myclass
);
516 /* Get application system defaults */
517 db
= get_system_app (myclass
);
519 XrmMergeDatabases (db
, &rdb
);
521 /* Get Fallback resources */
522 db
= get_fallback (display
);
524 XrmMergeDatabases (db
, &rdb
);
526 /* Get application user defaults */
527 db
= get_user_app (myclass
);
529 XrmMergeDatabases (db
, &rdb
);
531 /* get User defaults */
532 if (user_database
!= NULL
)
533 XrmMergeDatabases (user_database
, &rdb
);
535 /* Get Environment defaults. */
536 db
= get_environ_db ();
538 XrmMergeDatabases (db
, &rdb
);
540 /* Last, merge in any specification from the command line. */
541 if (xrm_string
!= NULL
)
543 db
= XrmGetStringDatabase (xrm_string
);
545 XrmMergeDatabases (db
, &rdb
);
552 /* Retrieve the value of the resource specified by NAME with class CLASS
553 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
556 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
557 XrmRepresentation expected_type
, XrmValue
*ret_value
)
560 XrmName namelist
[100];
561 XrmClass classlist
[100];
562 XrmRepresentation type
;
564 XrmStringToNameList (name
, namelist
);
565 XrmStringToClassList (class, classlist
);
567 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
568 && (type
== expected_type
))
570 if (type
== x_rm_string
)
571 ret_value
->addr
= (char *) value
.addr
;
573 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
581 /* Retrieve the string resource specified by NAME with CLASS from
585 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
589 if (inhibit_x_resources
)
590 /* --quick was passed, so this is a no-op. */
593 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
594 return (char *) value
.addr
;
599 /* Stand-alone test facilities. */
604 #define arg_listify(len, list) (list)
605 #define car(list) (*(list))
606 #define cdr(list) (list + 1)
607 #define NIL(list) (! *(list))
608 #define free_arglist(list)
611 member (char *elt
, List list
)
615 for (p
= list
; ! NIL (p
); p
= cdr (p
))
616 if (! strcmp (elt
, car (p
)))
623 fatal (char *msg
, char *prog
)
625 fprintf (stderr
, msg
, prog
);
630 main (int argc
, char **argv
)
633 char *displayname
, *resource_string
, *class, *name
;
637 arg_list
= arg_listify (argc
, argv
);
639 lp
= member ("-d", arg_list
);
641 displayname
= car (cdr (lp
));
643 displayname
= "localhost:0.0";
645 lp
= member ("-xrm", arg_list
);
646 resource_string
= NIL (lp
) ? 0 : car (cdr (lp
));
648 lp
= member ("-c", arg_list
);
650 class = car (cdr (lp
));
654 lp
= member ("-n", arg_list
);
656 name
= car (cdr (lp
));
660 free_arglist (arg_list
);
662 if (!(display
= XOpenDisplay (displayname
)))
663 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
665 xdb
= x_load_resources (display
, resource_string
, name
, class);
667 /* In a real program, you'd want to also do this: */
673 char query_class
[90];
678 if (strlen (query_name
))
685 value
= x_get_string_resource (xdb
, query_name
, query_class
);
688 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
690 printf ("\tNo Value.\n\n");
695 printf ("\tExit.\n\n");
697 XCloseDisplay (display
);