1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2012 Free Software Foundation, Inc.
4 Author: Joseph Arceneaux
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
32 /* This may include sys/types.h, and that somehow loses
33 if this is not done before the other system files. */
37 #include <X11/Xatom.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
46 /* For Vdouble_click_time. */
50 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
54 /* X file search path processing. */
57 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
58 and friends, or zero if none was specified. */
59 static char *x_customization_string
;
62 /* Return the value of the emacs.customization (Emacs.Customization)
63 resource, for later use in search path decoding. If we find no
64 such resource, return zero. */
66 x_get_customization_string (XrmDatabase db
, const char *name
,
69 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
70 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
73 sprintf (full_name
, "%s.%s", name
, "customization");
74 sprintf (full_class
, "%s.%s", class, "Customization");
76 result
= x_get_string_resource (db
, full_name
, full_class
);
80 char *copy
= xmalloc (strlen (result
) + 1);
81 strcpy (copy
, result
);
89 /* Expand all the Xt-style %-escapes in STRING, whose length is given
90 by STRING_LEN. Here are the escapes we're supposed to recognize:
92 %N The value of the application's class name
93 %T The value of the type parameter ("app-defaults" in this
95 %S The value of the suffix parameter ("" in this context)
96 %L The language string associated with the specified display
97 (We use the "LANG" environment variable here, if it's set.)
98 %l The language part of the display's language string
99 (We treat this just like %L. If someone can tell us what
100 we're really supposed to do, dandy.)
101 %t The territory part of the display's language string
102 (This never gets used.)
103 %c The codeset part of the display's language string
104 (This never gets used either.)
105 %C The customization string retrieved from the resource
106 database associated with display.
107 (This is x_customization_string.)
109 Return the resource database if its file was read successfully, and
110 refers to %L only when the LANG environment variable is set, or
111 otherwise provided by X.
113 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
114 %-escapes in ESCAPED_SUFFIX are expanded.
116 Return NULL otherwise. */
119 magic_db (const char *string
, ptrdiff_t string_len
, const char *class,
120 const char *escaped_suffix
)
123 char *lang
= getenv ("LANG");
125 ptrdiff_t path_size
= 100;
126 char *path
= xmalloc (path_size
);
127 ptrdiff_t path_len
= 0;
129 const char *p
= string
;
131 while (p
< string
+ string_len
)
133 /* The chunk we're about to stick on the end of result. */
134 const char *next
= NULL
;
141 if (p
>= string
+ string_len
)
152 next
= (x_customization_string
153 ? x_customization_string
155 next_len
= strlen (next
);
160 next_len
= strlen (class);
164 next
= "app-defaults";
165 next_len
= strlen (next
);
182 next_len
= strlen (next
);
192 next
= p
, next_len
= 1;
194 /* Do we have room for this component followed by a '\0' ? */
195 if (path_size
- path_len
<= next_len
)
197 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
198 memory_full (SIZE_MAX
);
199 path_size
= (path_len
+ next_len
+ 1) * 2;
200 path
= xrealloc (path
, path_size
);
203 memcpy (path
+ path_len
, next
, next_len
);
204 path_len
+= next_len
;
208 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
209 if (p
>= string
+ string_len
&& escaped_suffix
)
211 string
= escaped_suffix
;
212 string_len
= strlen (string
);
214 escaped_suffix
= NULL
;
218 path
[path_len
] = '\0';
219 db
= XrmGetFileDatabase (path
);
232 if ((ptr
= getenv ("HOME")) == NULL
)
234 if ((ptr
= getenv ("LOGNAME")) != NULL
235 || (ptr
= getenv ("USER")) != NULL
)
238 pw
= getpwuid (getuid ());
245 return xstrdup ("/");
247 copy
= xmalloc (strlen (ptr
) + 2);
255 /* Find the first element of SEARCH_PATH which exists and is readable,
256 after expanding the %-escapes. Return 0 if we didn't find any, and
257 the path name of the one we found otherwise. */
260 search_magic_path (const char *search_path
, const char *class,
261 const char *escaped_suffix
)
265 for (s
= search_path
; *s
; s
= p
)
267 for (p
= s
; *p
&& *p
!= ':'; p
++)
272 XrmDatabase db
= magic_db (s
, p
- s
, class, escaped_suffix
);
278 static char const ns
[] = "%N%S";
279 XrmDatabase db
= magic_db (ns
, strlen (ns
), class, escaped_suffix
);
291 /* Producing databases for individual sources. */
294 get_system_app (const char *class)
298 path
= getenv ("XFILESEARCHPATH");
299 if (! path
) path
= PATH_X_DEFAULTS
;
301 return search_magic_path (path
, class, 0);
306 get_fallback (Display
*display
)
313 get_user_app (const char *class)
318 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
319 names, not directories. */
320 path
= getenv ("XUSERFILESEARCHPATH");
322 db
= search_magic_path (path
, class, 0);
326 /* Check for APPLRESDIR; it is a path of directories. In each,
327 we have to search for LANG/CLASS and then CLASS. */
328 path
= getenv ("XAPPLRESDIR");
331 db
= search_magic_path (path
, class, "/%L/%N");
333 db
= search_magic_path (path
, class, "/%N");
339 /* Check in the home directory. This is a bit of a hack; let's
340 hope one's home directory doesn't contain any %-escapes. */
341 char *home
= gethomedir ();
342 db
= search_magic_path (home
, class, "%L/%N");
344 db
= search_magic_path (home
, class, "%N");
353 get_user_db (Display
*display
)
358 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
359 xdefs
= XResourceManagerString (display
);
361 xdefs
= display
->xdefaults
;
365 db
= XrmGetStringDatabase (xdefs
);
371 home
= gethomedir ();
372 xdefault
= xmalloc (strlen (home
) + sizeof ".Xdefaults");
373 strcpy (xdefault
, home
);
374 strcat (xdefault
, ".Xdefaults");
375 db
= XrmGetFileDatabase (xdefault
);
380 #ifdef HAVE_XSCREENRESOURCESTRING
381 /* Get the screen-specific resources too. */
382 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
385 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
394 get_environ_db (void)
400 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
402 static char const xdefaults
[] = ".Xdefaults-";
403 char *home
= gethomedir ();
404 char const *host
= SSDATA (Vsystem_name
);
405 ptrdiff_t pathsize
= (strlen (home
) + sizeof xdefaults
406 + SBYTES (Vsystem_name
));
407 path
= xrealloc (home
, pathsize
);
408 strcat (strcat (path
, xdefaults
), host
);
412 db
= XrmGetFileDatabase (p
);
419 /* External interface. */
421 /* Types of values that we can find in a database */
423 #define XrmStringType "String" /* String representation */
424 static XrmRepresentation x_rm_string
; /* Quark representation */
426 /* Load X resources based on the display and a possible -xrm option. */
429 x_load_resources (Display
*display
, const char *xrm_string
,
430 const char *myname
, const char *myclass
)
432 XrmDatabase user_database
;
437 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
438 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
442 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
445 x_rm_string
= XrmStringToQuark (XrmStringType
);
446 #ifndef USE_X_TOOLKIT
447 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
448 I suspect it's because the toolkit version does this elsewhere. */
451 rdb
= XrmGetStringDatabase ("");
453 /* Add some font defaults. If the font `helv' doesn't exist, widgets
454 will use some other default font. */
457 sprintf (line
, "%s.pane.background: grey75", myclass
);
458 XrmPutLineResource (&rdb
, line
);
459 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
460 XrmPutLineResource (&rdb
, line
);
461 sprintf (line
, "%s*menu*background: grey75", myclass
);
462 XrmPutLineResource (&rdb
, line
);
463 sprintf (line
, "%s*menubar*background: grey75", myclass
);
464 XrmPutLineResource (&rdb
, line
);
465 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
466 XrmPutLineResource (&rdb
, line
);
467 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
468 XrmPutLineResource (&rdb
, line
);
469 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
470 XrmPutLineResource (&rdb
, line
);
471 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
472 XrmPutLineResource (&rdb
, line
);
473 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
474 XrmPutLineResource (&rdb
, line
);
475 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
476 XrmPutLineResource (&rdb
, line
);
477 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
478 XrmPutLineResource (&rdb
, line
);
479 sprintf (line
, "%s*fsb*background: grey75", myclass
);
480 XrmPutLineResource (&rdb
, line
);
481 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
482 XrmPutLineResource (&rdb
, line
);
483 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
484 XrmPutLineResource (&rdb
, line
);
485 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
486 XrmPutLineResource (&rdb
, line
);
487 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
488 XrmPutLineResource (&rdb
, line
);
490 /* Set double click time of list boxes in the file selection
491 dialog from `double-click-time'. */
492 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
494 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
495 myclass
, XFASTINT (Vdouble_click_time
));
496 XrmPutLineResource (&rdb
, line
);
497 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
498 myclass
, XFASTINT (Vdouble_click_time
));
499 XrmPutLineResource (&rdb
, line
);
502 #else /* not USE_MOTIF */
504 sprintf (line
, "Emacs.dialog*.background: grey75");
505 XrmPutLineResource (&rdb
, line
);
506 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
507 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
508 XrmPutLineResource (&rdb
, line
);
509 sprintf (line
, "*XlwMenu*font: %s", helv
);
510 XrmPutLineResource (&rdb
, line
);
512 sprintf (line
, "*XlwMenu*background: grey75");
513 XrmPutLineResource (&rdb
, line
);
514 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
515 XrmPutLineResource (&rdb
, line
);
517 #endif /* not USE_MOTIF */
519 user_database
= get_user_db (display
);
521 /* Figure out what the "customization string" is, so we can use it
523 xfree (x_customization_string
);
524 x_customization_string
525 = x_get_customization_string (user_database
, myname
, myclass
);
527 /* Get application system defaults */
528 db
= get_system_app (myclass
);
530 XrmMergeDatabases (db
, &rdb
);
532 /* Get Fallback resources */
533 db
= get_fallback (display
);
535 XrmMergeDatabases (db
, &rdb
);
537 /* Get application user defaults */
538 db
= get_user_app (myclass
);
540 XrmMergeDatabases (db
, &rdb
);
542 /* get User defaults */
543 if (user_database
!= NULL
)
544 XrmMergeDatabases (user_database
, &rdb
);
546 /* Get Environment defaults. */
547 db
= get_environ_db ();
549 XrmMergeDatabases (db
, &rdb
);
551 /* Last, merge in any specification from the command line. */
552 if (xrm_string
!= NULL
)
554 db
= XrmGetStringDatabase (xrm_string
);
556 XrmMergeDatabases (db
, &rdb
);
563 /* Retrieve the value of the resource specified by NAME with class CLASS
564 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
567 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
568 XrmRepresentation expected_type
, XrmValue
*ret_value
)
571 XrmName namelist
[100];
572 XrmClass classlist
[100];
573 XrmRepresentation type
;
575 XrmStringToNameList (name
, namelist
);
576 XrmStringToClassList (class, classlist
);
578 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
579 && (type
== expected_type
))
581 if (type
== x_rm_string
)
582 ret_value
->addr
= (char *) value
.addr
;
584 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
592 /* Retrieve the string resource specified by NAME with CLASS from
596 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
600 if (inhibit_x_resources
)
601 /* --quick was passed, so this is a no-op. */
604 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
605 return (char *) value
.addr
;
610 /* Stand-alone test facilities. */
615 #define arg_listify(len, list) (list)
616 #define car(list) (*(list))
617 #define cdr(list) (list + 1)
618 #define NIL(list) (! *(list))
619 #define free_arglist(list)
622 member (char *elt
, List list
)
626 for (p
= list
; ! NIL (p
); p
= cdr (p
))
627 if (! strcmp (elt
, car (p
)))
634 fatal (char *msg
, char *prog
)
639 (void) fprintf (stderr
, msg
, prog
);
644 main (int argc
, char **argv
)
647 char *displayname
, *resource_string
, *class, *name
;
651 arg_list
= arg_listify (argc
, argv
);
653 lp
= member ("-d", arg_list
);
655 displayname
= car (cdr (lp
));
657 displayname
= "localhost:0.0";
659 lp
= member ("-xrm", arg_list
);
661 resource_string
= car (cdr (lp
));
663 resource_string
= (char *) 0;
665 lp
= member ("-c", arg_list
);
667 class = car (cdr (lp
));
671 lp
= member ("-n", arg_list
);
673 name
= car (cdr (lp
));
677 free_arglist (arg_list
);
679 if (!(display
= XOpenDisplay (displayname
)))
680 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
682 xdb
= x_load_resources (display
, resource_string
, name
, class);
684 /* In a real program, you'd want to also do this: */
690 char query_class
[90];
695 if (strlen (query_name
))
702 value
= x_get_string_resource (xdb
, query_name
, query_class
);
705 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
707 printf ("\tNo Value.\n\n");
712 printf ("\tExit.\n\n");
714 XCloseDisplay (display
);