1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993, 1994, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
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/>. */
37 #if 1 /* I'd really appreciate it if this code could go away... -JimB */
38 /* This avoids lossage in the `dual-universe' headers on AT&T SysV
39 X11. Don't do it on Solaris, because it breaks compilation with
40 XFree86 4.0.3 (and probably many other X11R6 releases) on Solaris
42 #if defined(USG5) && !defined(SOLARIS2)
46 #endif /* USG5 && !SOLARIS2 */
51 #include <X11/Xatom.h>
56 #include <X11/Xutil.h>
57 #include <X11/Xresource.h>
63 #if !defined(S_ISDIR) && defined(S_IFDIR)
64 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
69 extern char *getenv ();
71 /* This does cause trouble on AIX. I'm going to take the comment at
74 extern short getuid (); /* If this causes portability problems,
75 I think we should just delete it; it'll
76 default to `int' anyway. */
79 #ifdef DECLARE_GETPWUID_WITH_UID_T
80 extern struct passwd
*getpwuid (uid_t
);
81 extern struct passwd
*getpwnam (const char *);
83 extern struct passwd
*getpwuid ();
84 extern struct passwd
*getpwnam ();
87 extern char *get_system_name ();
89 /* Make sure not to #include anything after these definitions. Let's
90 not step on anyone's prototypes. */
92 /* darwin.h may have already defined these. */
96 #define malloc xmalloc
97 #define realloc xrealloc
101 char *x_get_string_resource ();
102 static int file_p ();
105 /* X file search path processing. */
108 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
109 and friends, or zero if none was specified. */
110 char *x_customization_string
;
113 /* Return the value of the emacs.customization (Emacs.Customization)
114 resource, for later use in search path decoding. If we find no
115 such resource, return zero. */
117 x_get_customization_string (db
, name
, class)
122 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
124 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
127 sprintf (full_name
, "%s.%s", name
, "customization");
128 sprintf (full_class
, "%s.%s", class, "Customization");
130 result
= x_get_string_resource (db
, full_name
, full_class
);
134 char *copy
= (char *) malloc (strlen (result
) + 1);
135 strcpy (copy
, result
);
143 /* Expand all the Xt-style %-escapes in STRING, whose length is given
144 by STRING_LEN. Here are the escapes we're supposed to recognize:
146 %N The value of the application's class name
147 %T The value of the type parameter ("app-defaults" in this
149 %S The value of the suffix parameter ("" in this context)
150 %L The language string associated with the specified display
151 (We use the "LANG" environment variable here, if it's set.)
152 %l The language part of the display's language string
153 (We treat this just like %L. If someone can tell us what
154 we're really supposed to do, dandy.)
155 %t The territory part of the display's language string
156 (This never gets used.)
157 %c The codeset part of the display's language string
158 (This never gets used either.)
159 %C The customization string retrieved from the resource
160 database associated with display.
161 (This is x_customization_string.)
163 Return the expanded file name if it exists and is readable, and
164 refers to %L only when the LANG environment variable is set, or
165 otherwise provided by X.
167 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
168 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
171 Return NULL otherwise. */
174 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
177 char *class, *escaped_suffix
, *suffix
;
179 char *lang
= getenv ("LANG");
182 char *path
= (char *) malloc (path_size
);
187 while (p
< string
+ string_len
)
189 /* The chunk we're about to stick on the end of result. */
197 if (p
>= string
+ string_len
)
208 next
= (x_customization_string
209 ? x_customization_string
211 next_len
= strlen (next
);
216 next_len
= strlen (class);
220 next
= "app-defaults";
221 next_len
= strlen (next
);
238 next_len
= strlen (next
);
248 next
= p
, next_len
= 1;
250 /* Do we have room for this component followed by a '\0' ? */
251 if (path_len
+ next_len
+ 1 > path_size
)
253 path_size
= (path_len
+ next_len
+ 1) * 2;
254 path
= (char *) realloc (path
, path_size
);
257 bcopy (next
, path
+ path_len
, next_len
);
258 path_len
+= next_len
;
262 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
263 if (p
>= string
+ string_len
&& escaped_suffix
)
265 string
= escaped_suffix
;
266 string_len
= strlen (string
);
268 escaped_suffix
= NULL
;
272 /* Perhaps we should add the SUFFIX now. */
275 int suffix_len
= strlen (suffix
);
277 if (path_len
+ suffix_len
+ 1 > path_size
)
279 path_size
= (path_len
+ suffix_len
+ 1);
280 path
= (char *) realloc (path
, path_size
);
283 bcopy (suffix
, path
+ path_len
, suffix_len
);
284 path_len
+= suffix_len
;
287 path
[path_len
] = '\0';
306 if ((ptr
= getenv ("HOME")) == NULL
)
308 if ((ptr
= getenv ("LOGNAME")) != NULL
309 || (ptr
= getenv ("USER")) != NULL
)
312 pw
= getpwuid (getuid ());
319 return xstrdup ("/");
321 copy
= (char *) malloc (strlen (ptr
) + 2);
335 return (access (filename
, 4) == 0 /* exists and is readable */
336 && stat (filename
, &status
) == 0 /* get the status */
337 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
341 /* Find the first element of SEARCH_PATH which exists and is readable,
342 after expanding the %-escapes. Return 0 if we didn't find any, and
343 the path name of the one we found otherwise. */
346 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
347 char *search_path
, *class, *escaped_suffix
, *suffix
;
349 register char *s
, *p
;
351 for (s
= search_path
; *s
; s
= p
)
353 for (p
= s
; *p
&& *p
!= ':'; p
++)
358 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
367 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
379 /* Producing databases for individual sources. */
382 get_system_app (class)
385 XrmDatabase db
= NULL
;
388 path
= getenv ("XFILESEARCHPATH");
389 if (! path
) path
= PATH_X_DEFAULTS
;
391 path
= search_magic_path (path
, class, 0, 0);
394 db
= XrmGetFileDatabase (path
);
403 get_fallback (display
)
418 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
419 names, not directories. */
420 if (((path
= getenv ("XUSERFILESEARCHPATH"))
421 && (file
= search_magic_path (path
, class, 0, 0)))
423 /* Check for APPLRESDIR; it is a path of directories. In each,
424 we have to search for LANG/CLASS and then CLASS. */
425 || ((path
= getenv ("XAPPLRESDIR"))
426 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
427 || (file
= search_magic_path (path
, class, "/%N", 0))))
429 /* Check in the home directory. This is a bit of a hack; let's
430 hope one's home directory doesn't contain any %-escapes. */
431 || (free_it
= gethomedir (),
432 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
433 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
435 XrmDatabase db
= XrmGetFileDatabase (file
);
447 get_user_db (display
)
453 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
454 xdefs
= XResourceManagerString (display
);
456 xdefs
= display
->xdefaults
;
460 db
= XrmGetStringDatabase (xdefs
);
466 home
= gethomedir ();
467 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
468 strcpy (xdefault
, home
);
469 strcat (xdefault
, ".Xdefaults");
470 db
= XrmGetFileDatabase (xdefault
);
475 #ifdef HAVE_XSCREENRESOURCESTRING
476 /* Get the screen-specific resources too. */
477 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
480 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
493 char *path
= 0, *home
= 0, *host
;
495 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
497 home
= gethomedir ();
498 host
= get_system_name ();
499 path
= (char *) malloc (strlen (home
)
500 + sizeof (".Xdefaults-")
502 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
506 db
= XrmGetFileDatabase (p
);
514 /* External interface. */
516 /* Types of values that we can find in a database */
518 #define XrmStringType "String" /* String representation */
519 XrmRepresentation x_rm_string
; /* Quark representation */
521 /* Load X resources based on the display and a possible -xrm option. */
524 x_load_resources (display
, xrm_string
, myname
, myclass
)
526 char *xrm_string
, *myname
, *myclass
;
528 XrmDatabase user_database
;
533 char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
536 char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
537 extern Lisp_Object Vdouble_click_time
;
540 x_rm_string
= XrmStringToQuark (XrmStringType
);
541 #ifndef USE_X_TOOLKIT
542 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
543 I suspect it's because the toolkit version does this elsewhere. */
546 rdb
= XrmGetStringDatabase ("");
548 /* Add some font defaults. If the font `helv' doesn't exist, widgets
549 will use some other default font. */
552 sprintf (line
, "%s.pane.background: grey75", myclass
);
553 XrmPutLineResource (&rdb
, line
);
554 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
555 XrmPutLineResource (&rdb
, line
);
556 sprintf (line
, "%s*menu*background: grey75", myclass
);
557 XrmPutLineResource (&rdb
, line
);
558 sprintf (line
, "%s*menubar*background: grey75", myclass
);
559 XrmPutLineResource (&rdb
, line
);
560 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
561 XrmPutLineResource (&rdb
, line
);
562 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
563 XrmPutLineResource (&rdb
, line
);
564 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
565 XrmPutLineResource (&rdb
, line
);
566 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
567 XrmPutLineResource (&rdb
, line
);
568 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
569 XrmPutLineResource (&rdb
, line
);
570 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
571 XrmPutLineResource (&rdb
, line
);
572 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
573 XrmPutLineResource (&rdb
, line
);
574 sprintf (line
, "%s*fsb*background: grey75", myclass
);
575 XrmPutLineResource (&rdb
, line
);
576 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
577 XrmPutLineResource (&rdb
, line
);
578 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
579 XrmPutLineResource (&rdb
, line
);
580 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
581 XrmPutLineResource (&rdb
, line
);
582 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
583 XrmPutLineResource (&rdb
, line
);
585 /* Set double click time of list boxes in the file selection
586 dialog from `double-click-time'. */
587 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
589 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
590 myclass
, XFASTINT (Vdouble_click_time
));
591 XrmPutLineResource (&rdb
, line
);
592 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
593 myclass
, XFASTINT (Vdouble_click_time
));
594 XrmPutLineResource (&rdb
, line
);
597 #else /* not USE_MOTIF */
599 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
600 XrmPutLineResource (&rdb
, line
);
601 sprintf (line
, "Emacs.dialog*.background: grey75");
602 XrmPutLineResource (&rdb
, line
);
603 sprintf (line
, "*XlwMenu*font: %s", helv
);
604 XrmPutLineResource (&rdb
, line
);
605 sprintf (line
, "*XlwMenu*background: grey75");
606 XrmPutLineResource (&rdb
, line
);
607 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
608 XrmPutLineResource (&rdb
, line
);
610 #endif /* not USE_MOTIF */
612 user_database
= get_user_db (display
);
614 /* Figure out what the "customization string" is, so we can use it
616 free (x_customization_string
);
617 x_customization_string
618 = x_get_customization_string (user_database
, myname
, myclass
);
620 /* Get application system defaults */
621 db
= get_system_app (myclass
);
623 XrmMergeDatabases (db
, &rdb
);
625 /* Get Fallback resources */
626 db
= get_fallback (display
);
628 XrmMergeDatabases (db
, &rdb
);
630 /* Get application user defaults */
631 db
= get_user_app (myclass
);
633 XrmMergeDatabases (db
, &rdb
);
635 /* get User defaults */
636 if (user_database
!= NULL
)
637 XrmMergeDatabases (user_database
, &rdb
);
639 /* Get Environment defaults. */
640 db
= get_environ_db ();
642 XrmMergeDatabases (db
, &rdb
);
644 /* Last, merge in any specification from the command line. */
645 if (xrm_string
!= NULL
)
647 db
= XrmGetStringDatabase (xrm_string
);
649 XrmMergeDatabases (db
, &rdb
);
656 /* Retrieve the value of the resource specified by NAME with class CLASS
657 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
660 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
663 XrmRepresentation expected_type
;
667 XrmName namelist
[100];
668 XrmClass classlist
[100];
669 XrmRepresentation type
;
671 XrmStringToNameList(name
, namelist
);
672 XrmStringToClassList(class, classlist
);
674 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
675 && (type
== expected_type
))
677 if (type
== x_rm_string
)
678 ret_value
->addr
= (char *) value
.addr
;
680 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
688 /* Retrieve the string resource specified by NAME with CLASS from
692 x_get_string_resource (rdb
, name
, class)
698 if (inhibit_x_resources
)
699 /* --quick was passed, so this is a no-op. */
702 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
703 return (char *) value
.addr
;
708 /* Stand-alone test facilities. */
713 #define arg_listify(len, list) (list)
714 #define car(list) (*(list))
715 #define cdr(list) (list + 1)
716 #define NIL(list) (! *(list))
717 #define free_arglist(list)
726 for (p
= list
; ! NIL (p
); p
= cdr (p
))
727 if (! strcmp (elt
, car (p
)))
734 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
736 int x1
, x2
, x3
, x4
, x5
;
741 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
750 char *displayname
, *resource_string
, *class, *name
;
754 arg_list
= arg_listify (argc
, argv
);
756 lp
= member ("-d", arg_list
);
758 displayname
= car (cdr (lp
));
760 displayname
= "localhost:0.0";
762 lp
= member ("-xrm", arg_list
);
764 resource_string
= car (cdr (lp
));
766 resource_string
= (char *) 0;
768 lp
= member ("-c", arg_list
);
770 class = car (cdr (lp
));
774 lp
= member ("-n", arg_list
);
776 name
= car (cdr (lp
));
780 free_arglist (arg_list
);
782 if (!(display
= XOpenDisplay (displayname
)))
783 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
785 xdb
= x_load_resources (display
, resource_string
, name
, class);
787 /* In a real program, you'd want to also do this: */
793 char query_class
[90];
798 if (strlen (query_name
))
805 value
= x_get_string_resource (xdb
, query_name
, query_class
);
808 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
810 printf ("\tNo Value.\n\n");
815 printf ("\tExit.\n\n");
817 XCloseDisplay (display
);
821 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
822 (do not change this comment) */