1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* Written by jla, 4/90 */
36 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
37 /* This avoids lossage in the `dual-universe' headers on AT&T SysV
38 X11. Don't do it on Solaris, because it breaks compilation with
39 XFree86 4.0.3 (and probably many other X11R6 releases) on Solaris
41 #if defined(USG5) && !defined(SOLARIS2)
45 #endif /* USG5 && !SOLARIS2 */
50 #include <X11/Xatom.h>
55 #include <X11/Xutil.h>
56 #include <X11/Xresource.h>
62 #if !defined(S_ISDIR) && defined(S_IFDIR)
63 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
68 extern char *getenv ();
70 /* This does cause trouble on AIX. I'm going to take the comment at
73 extern short getuid (); /* If this causes portability problems,
74 I think we should just delete it; it'll
75 default to `int' anyway. */
78 #ifdef DECLARE_GETPWUID_WITH_UID_T
79 extern struct passwd
*getpwuid (uid_t
);
80 extern struct passwd
*getpwnam (const char *);
82 extern struct passwd
*getpwuid ();
83 extern struct passwd
*getpwnam ();
86 extern char *get_system_name ();
88 /* Make sure not to #include anything after these definitions. Let's
89 not step on anyone's prototypes. */
91 #define malloc xmalloc
92 #define realloc xrealloc
96 char *x_get_string_resource ();
100 /* X file search path processing. */
103 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
104 and friends, or zero if none was specified. */
105 char *x_customization_string
;
108 /* Return the value of the emacs.customization (Emacs.Customization)
109 resource, for later use in search path decoding. If we find no
110 such resource, return zero. */
112 x_get_customization_string (db
, name
, class)
117 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
119 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
122 sprintf (full_name
, "%s.%s", name
, "customization");
123 sprintf (full_class
, "%s.%s", class, "Customization");
125 result
= x_get_string_resource (db
, full_name
, full_class
);
129 char *copy
= (char *) malloc (strlen (result
) + 1);
130 strcpy (copy
, result
);
138 /* Expand all the Xt-style %-escapes in STRING, whose length is given
139 by STRING_LEN. Here are the escapes we're supposed to recognize:
141 %N The value of the application's class name
142 %T The value of the type parameter ("app-defaults" in this
144 %S The value of the suffix parameter ("" in this context)
145 %L The language string associated with the specified display
146 (We use the "LANG" environment variable here, if it's set.)
147 %l The language part of the display's language string
148 (We treat this just like %L. If someone can tell us what
149 we're really supposed to do, dandy.)
150 %t The territory part of the display's language string
151 (This never gets used.)
152 %c The codeset part of the display's language string
153 (This never gets used either.)
154 %C The customization string retrieved from the resource
155 database associated with display.
156 (This is x_customization_string.)
158 Return the expanded file name if it exists and is readable, and
159 refers to %L only when the LANG environment variable is set, or
160 otherwise provided by X.
162 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
163 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
166 Return NULL otherwise. */
169 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
172 char *class, *escaped_suffix
, *suffix
;
174 char *lang
= getenv ("LANG");
177 char *path
= (char *) malloc (path_size
);
182 while (p
< string
+ string_len
)
184 /* The chunk we're about to stick on the end of result. */
192 if (p
>= string
+ string_len
)
203 next
= (x_customization_string
204 ? x_customization_string
206 next_len
= strlen (next
);
211 next_len
= strlen (class);
215 next
= "app-defaults";
216 next_len
= strlen (next
);
233 next_len
= strlen (next
);
243 next
= p
, next_len
= 1;
245 /* Do we have room for this component followed by a '\0' ? */
246 if (path_len
+ next_len
+ 1 > path_size
)
248 path_size
= (path_len
+ next_len
+ 1) * 2;
249 path
= (char *) realloc (path
, path_size
);
252 bcopy (next
, path
+ path_len
, next_len
);
253 path_len
+= next_len
;
257 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
258 if (p
>= string
+ string_len
&& escaped_suffix
)
260 string
= escaped_suffix
;
261 string_len
= strlen (string
);
263 escaped_suffix
= NULL
;
267 /* Perhaps we should add the SUFFIX now. */
270 int suffix_len
= strlen (suffix
);
272 if (path_len
+ suffix_len
+ 1 > path_size
)
274 path_size
= (path_len
+ suffix_len
+ 1);
275 path
= (char *) realloc (path
, path_size
);
278 bcopy (suffix
, path
+ path_len
, suffix_len
);
279 path_len
+= suffix_len
;
282 path
[path_len
] = '\0';
301 if ((ptr
= getenv ("HOME")) == NULL
)
303 if ((ptr
= getenv ("LOGNAME")) != NULL
304 || (ptr
= getenv ("USER")) != NULL
)
307 pw
= getpwuid (getuid ());
316 copy
= (char *) malloc (strlen (ptr
) + 2);
330 return (access (filename
, 4) == 0 /* exists and is readable */
331 && stat (filename
, &status
) == 0 /* get the status */
332 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
336 /* Find the first element of SEARCH_PATH which exists and is readable,
337 after expanding the %-escapes. Return 0 if we didn't find any, and
338 the path name of the one we found otherwise. */
341 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
342 char *search_path
, *class, *escaped_suffix
, *suffix
;
344 register char *s
, *p
;
346 for (s
= search_path
; *s
; s
= p
)
348 for (p
= s
; *p
&& *p
!= ':'; p
++)
353 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
362 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
374 /* Producing databases for individual sources. */
377 get_system_app (class)
380 XrmDatabase db
= NULL
;
383 path
= getenv ("XFILESEARCHPATH");
384 if (! path
) path
= PATH_X_DEFAULTS
;
386 path
= search_magic_path (path
, class, 0, 0);
389 db
= XrmGetFileDatabase (path
);
398 get_fallback (display
)
413 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
414 names, not directories. */
415 if (((path
= getenv ("XUSERFILESEARCHPATH"))
416 && (file
= search_magic_path (path
, class, 0, 0)))
418 /* Check for APPLRESDIR; it is a path of directories. In each,
419 we have to search for LANG/CLASS and then CLASS. */
420 || ((path
= getenv ("XAPPLRESDIR"))
421 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
422 || (file
= search_magic_path (path
, class, "/%N", 0))))
424 /* Check in the home directory. This is a bit of a hack; let's
425 hope one's home directory doesn't contain any %-escapes. */
426 || (free_it
= gethomedir (),
427 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
428 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
430 XrmDatabase db
= XrmGetFileDatabase (file
);
444 get_user_db (display
)
450 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
451 xdefs
= XResourceManagerString (display
);
453 xdefs
= display
->xdefaults
;
457 db
= XrmGetStringDatabase (xdefs
);
463 home
= gethomedir ();
464 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
465 strcpy (xdefault
, home
);
466 strcat (xdefault
, ".Xdefaults");
467 db
= XrmGetFileDatabase (xdefault
);
472 #ifdef HAVE_XSCREENRESOURCESTRING
473 /* Get the screen-specific resources too. */
474 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
477 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
490 char *path
= 0, *home
= 0, *host
;
492 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
494 home
= gethomedir ();
495 host
= get_system_name ();
496 path
= (char *) malloc (strlen (home
)
497 + sizeof (".Xdefaults-")
499 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
503 db
= XrmGetFileDatabase (p
);
505 if (path
) free (path
);
506 if (home
) free (home
);
511 /* External interface. */
513 /* Types of values that we can find in a database */
515 #define XrmStringType "String" /* String representation */
516 XrmRepresentation x_rm_string
; /* Quark representation */
518 /* Load X resources based on the display and a possible -xrm option. */
521 x_load_resources (display
, xrm_string
, myname
, myclass
)
523 char *xrm_string
, *myname
, *myclass
;
525 XrmDatabase user_database
;
530 char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
533 char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
534 extern Lisp_Object Vdouble_click_time
;
537 x_rm_string
= XrmStringToQuark (XrmStringType
);
538 #ifndef USE_X_TOOLKIT
539 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
540 I suspect it's because the toolkit version does this elsewhere. */
543 rdb
= XrmGetStringDatabase ("");
545 /* Add some font defaults. If the font `helv' doesn't exist, widgets
546 will use some other default font. */
549 sprintf (line
, "%s.pane.background: grey75", myclass
);
550 XrmPutLineResource (&rdb
, line
);
551 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
552 XrmPutLineResource (&rdb
, line
);
553 sprintf (line
, "%s*menu*background: grey75", myclass
);
554 XrmPutLineResource (&rdb
, line
);
555 sprintf (line
, "%s*menubar*background: grey75", myclass
);
556 XrmPutLineResource (&rdb
, line
);
557 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
558 XrmPutLineResource (&rdb
, line
);
559 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
560 XrmPutLineResource (&rdb
, line
);
561 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
562 XrmPutLineResource (&rdb
, line
);
563 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
564 XrmPutLineResource (&rdb
, line
);
565 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
566 XrmPutLineResource (&rdb
, line
);
567 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
568 XrmPutLineResource (&rdb
, line
);
569 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
570 XrmPutLineResource (&rdb
, line
);
571 sprintf (line
, "%s*fsb*background: grey75", myclass
);
572 XrmPutLineResource (&rdb
, line
);
573 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
574 XrmPutLineResource (&rdb
, line
);
575 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
576 XrmPutLineResource (&rdb
, line
);
577 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
578 XrmPutLineResource (&rdb
, line
);
579 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
580 XrmPutLineResource (&rdb
, line
);
582 /* Set double click time of list boxes in the file selection
583 dialog from `double-click-time'. */
584 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
586 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
587 myclass
, XFASTINT (Vdouble_click_time
));
588 XrmPutLineResource (&rdb
, line
);
589 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
590 myclass
, XFASTINT (Vdouble_click_time
));
591 XrmPutLineResource (&rdb
, line
);
594 #else /* not USE_MOTIF */
596 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
597 XrmPutLineResource (&rdb
, line
);
598 sprintf (line
, "Emacs.dialog*.background: grey75");
599 XrmPutLineResource (&rdb
, line
);
600 sprintf (line
, "*XlwMenu*font: %s", helv
);
601 XrmPutLineResource (&rdb
, line
);
602 sprintf (line
, "*XlwMenu*background: grey75");
603 XrmPutLineResource (&rdb
, line
);
604 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
605 XrmPutLineResource (&rdb
, line
);
607 #endif /* not USE_MOTIF */
609 user_database
= get_user_db (display
);
611 /* Figure out what the "customization string" is, so we can use it
613 if (x_customization_string
)
614 free (x_customization_string
);
615 x_customization_string
616 = x_get_customization_string (user_database
, myname
, myclass
);
618 /* Get application system defaults */
619 db
= get_system_app (myclass
);
621 XrmMergeDatabases (db
, &rdb
);
623 /* Get Fallback resources */
624 db
= get_fallback (display
);
626 XrmMergeDatabases (db
, &rdb
);
628 /* Get application user defaults */
629 db
= get_user_app (myclass
);
631 XrmMergeDatabases (db
, &rdb
);
633 /* get User defaults */
634 if (user_database
!= NULL
)
635 XrmMergeDatabases (user_database
, &rdb
);
637 /* Get Environment defaults. */
638 db
= get_environ_db ();
640 XrmMergeDatabases (db
, &rdb
);
642 /* Last, merge in any specification from the command line. */
643 if (xrm_string
!= NULL
)
645 db
= XrmGetStringDatabase (xrm_string
);
647 XrmMergeDatabases (db
, &rdb
);
654 /* Retrieve the value of the resource specified by NAME with class CLASS
655 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
658 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
661 XrmRepresentation expected_type
;
665 XrmName namelist
[100];
666 XrmClass classlist
[100];
667 XrmRepresentation type
;
669 XrmStringToNameList(name
, namelist
);
670 XrmStringToClassList(class, classlist
);
672 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
673 && (type
== expected_type
))
675 if (type
== x_rm_string
)
676 ret_value
->addr
= (char *) value
.addr
;
678 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
686 /* Retrieve the string resource specified by NAME with CLASS from
690 x_get_string_resource (rdb
, name
, class)
696 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
697 return (char *) value
.addr
;
702 /* Stand-alone test facilities. */
707 #define arg_listify(len, list) (list)
708 #define car(list) (*(list))
709 #define cdr(list) (list + 1)
710 #define NIL(list) (! *(list))
711 #define free_arglist(list)
720 for (p
= list
; ! NIL (p
); p
= cdr (p
))
721 if (! strcmp (elt
, car (p
)))
728 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
730 int x1
, x2
, x3
, x4
, x5
;
737 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
746 char *displayname
, *resource_string
, *class, *name
;
750 arg_list
= arg_listify (argc
, argv
);
752 lp
= member ("-d", arg_list
);
754 displayname
= car (cdr (lp
));
756 displayname
= "localhost:0.0";
758 lp
= member ("-xrm", arg_list
);
760 resource_string
= car (cdr (lp
));
762 resource_string
= (char *) 0;
764 lp
= member ("-c", arg_list
);
766 class = car (cdr (lp
));
770 lp
= member ("-n", arg_list
);
772 name
= car (cdr (lp
));
776 free_arglist (arg_list
);
778 if (!(display
= XOpenDisplay (displayname
)))
779 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
781 xdb
= x_load_resources (display
, resource_string
, name
, class);
783 /* In a real program, you'd want to also do this: */
789 char query_class
[90];
794 if (strlen (query_name
))
801 value
= x_get_string_resource (xdb
, query_name
, query_class
);
804 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
806 printf ("\tNo Value.\n\n");
811 printf ("\tExit.\n\n");
813 XCloseDisplay (display
);
817 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
818 (do not change this comment) */