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/>. */
38 #include <X11/Xatom.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
47 #if !defined(S_ISDIR) && defined(S_IFDIR)
48 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
53 extern char *getenv ();
55 /* This does cause trouble on AIX. I'm going to take the comment at
58 extern short getuid (); /* If this causes portability problems,
59 I think we should just delete it; it'll
60 default to `int' anyway. */
63 #ifdef DECLARE_GETPWUID_WITH_UID_T
64 extern struct passwd
*getpwuid (uid_t
);
65 extern struct passwd
*getpwnam (const char *);
67 extern struct passwd
*getpwuid ();
68 extern struct passwd
*getpwnam ();
71 extern char *get_system_name ();
73 /* Make sure not to #include anything after these definitions. Let's
74 not step on anyone's prototypes. */
76 /* darwin.h may have already defined these. */
80 #define malloc xmalloc
81 #define realloc xrealloc
85 char *x_get_string_resource ();
89 /* X file search path processing. */
92 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
93 and friends, or zero if none was specified. */
94 char *x_customization_string
;
97 /* Return the value of the emacs.customization (Emacs.Customization)
98 resource, for later use in search path decoding. If we find no
99 such resource, return zero. */
101 x_get_customization_string (db
, name
, class)
106 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
108 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
111 sprintf (full_name
, "%s.%s", name
, "customization");
112 sprintf (full_class
, "%s.%s", class, "Customization");
114 result
= x_get_string_resource (db
, full_name
, full_class
);
118 char *copy
= (char *) malloc (strlen (result
) + 1);
119 strcpy (copy
, result
);
127 /* Expand all the Xt-style %-escapes in STRING, whose length is given
128 by STRING_LEN. Here are the escapes we're supposed to recognize:
130 %N The value of the application's class name
131 %T The value of the type parameter ("app-defaults" in this
133 %S The value of the suffix parameter ("" in this context)
134 %L The language string associated with the specified display
135 (We use the "LANG" environment variable here, if it's set.)
136 %l The language part of the display's language string
137 (We treat this just like %L. If someone can tell us what
138 we're really supposed to do, dandy.)
139 %t The territory part of the display's language string
140 (This never gets used.)
141 %c The codeset part of the display's language string
142 (This never gets used either.)
143 %C The customization string retrieved from the resource
144 database associated with display.
145 (This is x_customization_string.)
147 Return the expanded file name if it exists and is readable, and
148 refers to %L only when the LANG environment variable is set, or
149 otherwise provided by X.
151 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
152 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
155 Return NULL otherwise. */
158 magic_file_p (string
, string_len
, class, escaped_suffix
, suffix
)
161 char *class, *escaped_suffix
, *suffix
;
163 char *lang
= getenv ("LANG");
166 char *path
= (char *) malloc (path_size
);
171 while (p
< string
+ string_len
)
173 /* The chunk we're about to stick on the end of result. */
181 if (p
>= string
+ string_len
)
192 next
= (x_customization_string
193 ? x_customization_string
195 next_len
= strlen (next
);
200 next_len
= strlen (class);
204 next
= "app-defaults";
205 next_len
= strlen (next
);
222 next_len
= strlen (next
);
232 next
= p
, next_len
= 1;
234 /* Do we have room for this component followed by a '\0' ? */
235 if (path_len
+ next_len
+ 1 > path_size
)
237 path_size
= (path_len
+ next_len
+ 1) * 2;
238 path
= (char *) realloc (path
, path_size
);
241 bcopy (next
, path
+ path_len
, next_len
);
242 path_len
+= next_len
;
246 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
247 if (p
>= string
+ string_len
&& escaped_suffix
)
249 string
= escaped_suffix
;
250 string_len
= strlen (string
);
252 escaped_suffix
= NULL
;
256 /* Perhaps we should add the SUFFIX now. */
259 int suffix_len
= strlen (suffix
);
261 if (path_len
+ suffix_len
+ 1 > path_size
)
263 path_size
= (path_len
+ suffix_len
+ 1);
264 path
= (char *) realloc (path
, path_size
);
267 bcopy (suffix
, path
+ path_len
, suffix_len
);
268 path_len
+= suffix_len
;
271 path
[path_len
] = '\0';
290 if ((ptr
= getenv ("HOME")) == NULL
)
292 if ((ptr
= getenv ("LOGNAME")) != NULL
293 || (ptr
= getenv ("USER")) != NULL
)
296 pw
= getpwuid (getuid ());
303 return xstrdup ("/");
305 copy
= (char *) malloc (strlen (ptr
) + 2);
319 return (access (filename
, 4) == 0 /* exists and is readable */
320 && stat (filename
, &status
) == 0 /* get the status */
321 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
325 /* Find the first element of SEARCH_PATH which exists and is readable,
326 after expanding the %-escapes. Return 0 if we didn't find any, and
327 the path name of the one we found otherwise. */
330 search_magic_path (search_path
, class, escaped_suffix
, suffix
)
331 char *search_path
, *class, *escaped_suffix
, *suffix
;
333 register char *s
, *p
;
335 for (s
= search_path
; *s
; s
= p
)
337 for (p
= s
; *p
&& *p
!= ':'; p
++)
342 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
351 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
363 /* Producing databases for individual sources. */
366 get_system_app (class)
369 XrmDatabase db
= NULL
;
372 path
= getenv ("XFILESEARCHPATH");
373 if (! path
) path
= PATH_X_DEFAULTS
;
375 path
= search_magic_path (path
, class, 0, 0);
378 db
= XrmGetFileDatabase (path
);
387 get_fallback (display
)
402 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
403 names, not directories. */
404 if (((path
= getenv ("XUSERFILESEARCHPATH"))
405 && (file
= search_magic_path (path
, class, 0, 0)))
407 /* Check for APPLRESDIR; it is a path of directories. In each,
408 we have to search for LANG/CLASS and then CLASS. */
409 || ((path
= getenv ("XAPPLRESDIR"))
410 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
411 || (file
= search_magic_path (path
, class, "/%N", 0))))
413 /* Check in the home directory. This is a bit of a hack; let's
414 hope one's home directory doesn't contain any %-escapes. */
415 || (free_it
= gethomedir (),
416 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
417 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
419 XrmDatabase db
= XrmGetFileDatabase (file
);
431 get_user_db (display
)
437 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
438 xdefs
= XResourceManagerString (display
);
440 xdefs
= display
->xdefaults
;
444 db
= XrmGetStringDatabase (xdefs
);
450 home
= gethomedir ();
451 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
452 strcpy (xdefault
, home
);
453 strcat (xdefault
, ".Xdefaults");
454 db
= XrmGetFileDatabase (xdefault
);
459 #ifdef HAVE_XSCREENRESOURCESTRING
460 /* Get the screen-specific resources too. */
461 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
464 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
477 char *path
= 0, *home
= 0, *host
;
479 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
481 home
= gethomedir ();
482 host
= get_system_name ();
483 path
= (char *) malloc (strlen (home
)
484 + sizeof (".Xdefaults-")
486 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
490 db
= XrmGetFileDatabase (p
);
498 /* External interface. */
500 /* Types of values that we can find in a database */
502 #define XrmStringType "String" /* String representation */
503 XrmRepresentation x_rm_string
; /* Quark representation */
505 /* Load X resources based on the display and a possible -xrm option. */
508 x_load_resources (display
, xrm_string
, myname
, myclass
)
510 char *xrm_string
, *myname
, *myclass
;
512 XrmDatabase user_database
;
517 char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
520 char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
521 extern Lisp_Object Vdouble_click_time
;
524 x_rm_string
= XrmStringToQuark (XrmStringType
);
525 #ifndef USE_X_TOOLKIT
526 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
527 I suspect it's because the toolkit version does this elsewhere. */
530 rdb
= XrmGetStringDatabase ("");
532 /* Add some font defaults. If the font `helv' doesn't exist, widgets
533 will use some other default font. */
536 sprintf (line
, "%s.pane.background: grey75", myclass
);
537 XrmPutLineResource (&rdb
, line
);
538 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
539 XrmPutLineResource (&rdb
, line
);
540 sprintf (line
, "%s*menu*background: grey75", myclass
);
541 XrmPutLineResource (&rdb
, line
);
542 sprintf (line
, "%s*menubar*background: grey75", myclass
);
543 XrmPutLineResource (&rdb
, line
);
544 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
545 XrmPutLineResource (&rdb
, line
);
546 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
547 XrmPutLineResource (&rdb
, line
);
548 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
549 XrmPutLineResource (&rdb
, line
);
550 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
551 XrmPutLineResource (&rdb
, line
);
552 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
553 XrmPutLineResource (&rdb
, line
);
554 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
555 XrmPutLineResource (&rdb
, line
);
556 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
557 XrmPutLineResource (&rdb
, line
);
558 sprintf (line
, "%s*fsb*background: grey75", myclass
);
559 XrmPutLineResource (&rdb
, line
);
560 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
561 XrmPutLineResource (&rdb
, line
);
562 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
563 XrmPutLineResource (&rdb
, line
);
564 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
565 XrmPutLineResource (&rdb
, line
);
566 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
567 XrmPutLineResource (&rdb
, line
);
569 /* Set double click time of list boxes in the file selection
570 dialog from `double-click-time'. */
571 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
573 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
574 myclass
, XFASTINT (Vdouble_click_time
));
575 XrmPutLineResource (&rdb
, line
);
576 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
577 myclass
, XFASTINT (Vdouble_click_time
));
578 XrmPutLineResource (&rdb
, line
);
581 #else /* not USE_MOTIF */
583 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
584 XrmPutLineResource (&rdb
, line
);
585 sprintf (line
, "Emacs.dialog*.background: grey75");
586 XrmPutLineResource (&rdb
, line
);
587 sprintf (line
, "*XlwMenu*font: %s", helv
);
588 XrmPutLineResource (&rdb
, line
);
589 sprintf (line
, "*XlwMenu*background: grey75");
590 XrmPutLineResource (&rdb
, line
);
591 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
592 XrmPutLineResource (&rdb
, line
);
594 #endif /* not USE_MOTIF */
596 user_database
= get_user_db (display
);
598 /* Figure out what the "customization string" is, so we can use it
600 free (x_customization_string
);
601 x_customization_string
602 = x_get_customization_string (user_database
, myname
, myclass
);
604 /* Get application system defaults */
605 db
= get_system_app (myclass
);
607 XrmMergeDatabases (db
, &rdb
);
609 /* Get Fallback resources */
610 db
= get_fallback (display
);
612 XrmMergeDatabases (db
, &rdb
);
614 /* Get application user defaults */
615 db
= get_user_app (myclass
);
617 XrmMergeDatabases (db
, &rdb
);
619 /* get User defaults */
620 if (user_database
!= NULL
)
621 XrmMergeDatabases (user_database
, &rdb
);
623 /* Get Environment defaults. */
624 db
= get_environ_db ();
626 XrmMergeDatabases (db
, &rdb
);
628 /* Last, merge in any specification from the command line. */
629 if (xrm_string
!= NULL
)
631 db
= XrmGetStringDatabase (xrm_string
);
633 XrmMergeDatabases (db
, &rdb
);
640 /* Retrieve the value of the resource specified by NAME with class CLASS
641 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
644 x_get_resource (rdb
, name
, class, expected_type
, ret_value
)
647 XrmRepresentation expected_type
;
651 XrmName namelist
[100];
652 XrmClass classlist
[100];
653 XrmRepresentation type
;
655 XrmStringToNameList(name
, namelist
);
656 XrmStringToClassList(class, classlist
);
658 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
659 && (type
== expected_type
))
661 if (type
== x_rm_string
)
662 ret_value
->addr
= (char *) value
.addr
;
664 bcopy (value
.addr
, ret_value
->addr
, ret_value
->size
);
672 /* Retrieve the string resource specified by NAME with CLASS from
676 x_get_string_resource (rdb
, name
, class)
682 if (inhibit_x_resources
)
683 /* --quick was passed, so this is a no-op. */
686 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
687 return (char *) value
.addr
;
692 /* Stand-alone test facilities. */
697 #define arg_listify(len, list) (list)
698 #define car(list) (*(list))
699 #define cdr(list) (list + 1)
700 #define NIL(list) (! *(list))
701 #define free_arglist(list)
710 for (p
= list
; ! NIL (p
); p
= cdr (p
))
711 if (! strcmp (elt
, car (p
)))
718 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
720 int x1
, x2
, x3
, x4
, x5
;
725 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
734 char *displayname
, *resource_string
, *class, *name
;
738 arg_list
= arg_listify (argc
, argv
);
740 lp
= member ("-d", arg_list
);
742 displayname
= car (cdr (lp
));
744 displayname
= "localhost:0.0";
746 lp
= member ("-xrm", arg_list
);
748 resource_string
= car (cdr (lp
));
750 resource_string
= (char *) 0;
752 lp
= member ("-c", arg_list
);
754 class = car (cdr (lp
));
758 lp
= member ("-n", arg_list
);
760 name
= car (cdr (lp
));
764 free_arglist (arg_list
);
766 if (!(display
= XOpenDisplay (displayname
)))
767 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
769 xdb
= x_load_resources (display
, resource_string
, name
, class);
771 /* In a real program, you'd want to also do this: */
777 char query_class
[90];
782 if (strlen (query_name
))
789 value
= x_get_string_resource (xdb
, query_name
, query_class
);
792 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
794 printf ("\tNo Value.\n\n");
799 printf ("\tExit.\n\n");
801 XCloseDisplay (display
);
805 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
806 (do not change this comment) */