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 (const char *);
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 (uid_t
);
68 extern struct passwd
*getpwnam (const char *);
71 extern char *get_system_name (void);
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 (XrmDatabase rdb
, char *name
, char *class);
86 static int file_p (char *filename
);
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 (XrmDatabase db
, char *name
, char *class)
104 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
106 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
109 sprintf (full_name
, "%s.%s", name
, "customization");
110 sprintf (full_class
, "%s.%s", class, "Customization");
112 result
= x_get_string_resource (db
, full_name
, full_class
);
116 char *copy
= (char *) malloc (strlen (result
) + 1);
117 strcpy (copy
, result
);
125 /* Expand all the Xt-style %-escapes in STRING, whose length is given
126 by STRING_LEN. Here are the escapes we're supposed to recognize:
128 %N The value of the application's class name
129 %T The value of the type parameter ("app-defaults" in this
131 %S The value of the suffix parameter ("" in this context)
132 %L The language string associated with the specified display
133 (We use the "LANG" environment variable here, if it's set.)
134 %l The language part of the display's language string
135 (We treat this just like %L. If someone can tell us what
136 we're really supposed to do, dandy.)
137 %t The territory part of the display's language string
138 (This never gets used.)
139 %c The codeset part of the display's language string
140 (This never gets used either.)
141 %C The customization string retrieved from the resource
142 database associated with display.
143 (This is x_customization_string.)
145 Return the expanded file name if it exists and is readable, and
146 refers to %L only when the LANG environment variable is set, or
147 otherwise provided by X.
149 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
150 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
153 Return NULL otherwise. */
156 magic_file_p (char *string
, int string_len
, char *class, char *escaped_suffix
, char *suffix
)
158 char *lang
= getenv ("LANG");
161 char *path
= (char *) malloc (path_size
);
166 while (p
< string
+ string_len
)
168 /* The chunk we're about to stick on the end of result. */
176 if (p
>= string
+ string_len
)
187 next
= (x_customization_string
188 ? x_customization_string
190 next_len
= strlen (next
);
195 next_len
= strlen (class);
199 next
= "app-defaults";
200 next_len
= strlen (next
);
217 next_len
= strlen (next
);
227 next
= p
, next_len
= 1;
229 /* Do we have room for this component followed by a '\0' ? */
230 if (path_len
+ next_len
+ 1 > path_size
)
232 path_size
= (path_len
+ next_len
+ 1) * 2;
233 path
= (char *) realloc (path
, path_size
);
236 memcpy (path
+ path_len
, next
, next_len
);
237 path_len
+= next_len
;
241 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
242 if (p
>= string
+ string_len
&& escaped_suffix
)
244 string
= escaped_suffix
;
245 string_len
= strlen (string
);
247 escaped_suffix
= NULL
;
251 /* Perhaps we should add the SUFFIX now. */
254 int suffix_len
= strlen (suffix
);
256 if (path_len
+ suffix_len
+ 1 > path_size
)
258 path_size
= (path_len
+ suffix_len
+ 1);
259 path
= (char *) realloc (path
, path_size
);
262 memcpy (path
+ path_len
, suffix
, suffix_len
);
263 path_len
+= suffix_len
;
266 path
[path_len
] = '\0';
285 if ((ptr
= getenv ("HOME")) == NULL
)
287 if ((ptr
= getenv ("LOGNAME")) != NULL
288 || (ptr
= getenv ("USER")) != NULL
)
291 pw
= getpwuid (getuid ());
298 return xstrdup ("/");
300 copy
= (char *) malloc (strlen (ptr
) + 2);
309 file_p (char *filename
)
313 return (access (filename
, 4) == 0 /* exists and is readable */
314 && stat (filename
, &status
) == 0 /* get the status */
315 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
319 /* Find the first element of SEARCH_PATH which exists and is readable,
320 after expanding the %-escapes. Return 0 if we didn't find any, and
321 the path name of the one we found otherwise. */
324 search_magic_path (char *search_path
, char *class, char *escaped_suffix
, char *suffix
)
326 register char *s
, *p
;
328 for (s
= search_path
; *s
; s
= p
)
330 for (p
= s
; *p
&& *p
!= ':'; p
++)
335 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
, suffix
);
344 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
356 /* Producing databases for individual sources. */
359 get_system_app (char *class)
361 XrmDatabase db
= NULL
;
364 path
= getenv ("XFILESEARCHPATH");
365 if (! path
) path
= PATH_X_DEFAULTS
;
367 path
= search_magic_path (path
, class, 0, 0);
370 db
= XrmGetFileDatabase (path
);
379 get_fallback (Display
*display
)
386 get_user_app (char *class)
392 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
393 names, not directories. */
394 if (((path
= getenv ("XUSERFILESEARCHPATH"))
395 && (file
= search_magic_path (path
, class, 0, 0)))
397 /* Check for APPLRESDIR; it is a path of directories. In each,
398 we have to search for LANG/CLASS and then CLASS. */
399 || ((path
= getenv ("XAPPLRESDIR"))
400 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
401 || (file
= search_magic_path (path
, class, "/%N", 0))))
403 /* Check in the home directory. This is a bit of a hack; let's
404 hope one's home directory doesn't contain any %-escapes. */
405 || (free_it
= gethomedir (),
406 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
407 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
409 XrmDatabase db
= XrmGetFileDatabase (file
);
421 get_user_db (Display
*display
)
426 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
427 xdefs
= XResourceManagerString (display
);
429 xdefs
= display
->xdefaults
;
433 db
= XrmGetStringDatabase (xdefs
);
439 home
= gethomedir ();
440 xdefault
= (char *) malloc (strlen (home
) + sizeof (".Xdefaults"));
441 strcpy (xdefault
, home
);
442 strcat (xdefault
, ".Xdefaults");
443 db
= XrmGetFileDatabase (xdefault
);
448 #ifdef HAVE_XSCREENRESOURCESTRING
449 /* Get the screen-specific resources too. */
450 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
453 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
462 get_environ_db (void)
466 char *path
= 0, *home
= 0, *host
;
468 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
470 home
= gethomedir ();
471 host
= get_system_name ();
472 path
= (char *) malloc (strlen (home
)
473 + sizeof (".Xdefaults-")
475 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
479 db
= XrmGetFileDatabase (p
);
487 /* External interface. */
489 /* Types of values that we can find in a database */
491 #define XrmStringType "String" /* String representation */
492 XrmRepresentation x_rm_string
; /* Quark representation */
494 /* Load X resources based on the display and a possible -xrm option. */
497 x_load_resources (Display
*display
, char *xrm_string
, char *myname
, char *myclass
)
499 XrmDatabase user_database
;
504 char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
507 char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
508 extern Lisp_Object Vdouble_click_time
;
511 x_rm_string
= XrmStringToQuark (XrmStringType
);
512 #ifndef USE_X_TOOLKIT
513 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
514 I suspect it's because the toolkit version does this elsewhere. */
517 rdb
= XrmGetStringDatabase ("");
519 /* Add some font defaults. If the font `helv' doesn't exist, widgets
520 will use some other default font. */
523 sprintf (line
, "%s.pane.background: grey75", myclass
);
524 XrmPutLineResource (&rdb
, line
);
525 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
526 XrmPutLineResource (&rdb
, line
);
527 sprintf (line
, "%s*menu*background: grey75", myclass
);
528 XrmPutLineResource (&rdb
, line
);
529 sprintf (line
, "%s*menubar*background: grey75", myclass
);
530 XrmPutLineResource (&rdb
, line
);
531 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
532 XrmPutLineResource (&rdb
, line
);
533 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
534 XrmPutLineResource (&rdb
, line
);
535 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
536 XrmPutLineResource (&rdb
, line
);
537 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
538 XrmPutLineResource (&rdb
, line
);
539 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
540 XrmPutLineResource (&rdb
, line
);
541 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
542 XrmPutLineResource (&rdb
, line
);
543 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
544 XrmPutLineResource (&rdb
, line
);
545 sprintf (line
, "%s*fsb*background: grey75", myclass
);
546 XrmPutLineResource (&rdb
, line
);
547 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
548 XrmPutLineResource (&rdb
, line
);
549 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
550 XrmPutLineResource (&rdb
, line
);
551 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
552 XrmPutLineResource (&rdb
, line
);
553 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
554 XrmPutLineResource (&rdb
, line
);
556 /* Set double click time of list boxes in the file selection
557 dialog from `double-click-time'. */
558 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
560 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
561 myclass
, XFASTINT (Vdouble_click_time
));
562 XrmPutLineResource (&rdb
, line
);
563 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
564 myclass
, XFASTINT (Vdouble_click_time
));
565 XrmPutLineResource (&rdb
, line
);
568 #else /* not USE_MOTIF */
570 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
571 XrmPutLineResource (&rdb
, line
);
572 sprintf (line
, "Emacs.dialog*.background: grey75");
573 XrmPutLineResource (&rdb
, line
);
574 sprintf (line
, "*XlwMenu*font: %s", helv
);
575 XrmPutLineResource (&rdb
, line
);
576 sprintf (line
, "*XlwMenu*background: grey75");
577 XrmPutLineResource (&rdb
, line
);
578 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
579 XrmPutLineResource (&rdb
, line
);
581 #endif /* not USE_MOTIF */
583 user_database
= get_user_db (display
);
585 /* Figure out what the "customization string" is, so we can use it
587 free (x_customization_string
);
588 x_customization_string
589 = x_get_customization_string (user_database
, myname
, myclass
);
591 /* Get application system defaults */
592 db
= get_system_app (myclass
);
594 XrmMergeDatabases (db
, &rdb
);
596 /* Get Fallback resources */
597 db
= get_fallback (display
);
599 XrmMergeDatabases (db
, &rdb
);
601 /* Get application user defaults */
602 db
= get_user_app (myclass
);
604 XrmMergeDatabases (db
, &rdb
);
606 /* get User defaults */
607 if (user_database
!= NULL
)
608 XrmMergeDatabases (user_database
, &rdb
);
610 /* Get Environment defaults. */
611 db
= get_environ_db ();
613 XrmMergeDatabases (db
, &rdb
);
615 /* Last, merge in any specification from the command line. */
616 if (xrm_string
!= NULL
)
618 db
= XrmGetStringDatabase (xrm_string
);
620 XrmMergeDatabases (db
, &rdb
);
627 /* Retrieve the value of the resource specified by NAME with class CLASS
628 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
631 x_get_resource (XrmDatabase rdb
, char *name
, char *class, XrmRepresentation expected_type
, XrmValue
*ret_value
)
634 XrmName namelist
[100];
635 XrmClass classlist
[100];
636 XrmRepresentation type
;
638 XrmStringToNameList(name
, namelist
);
639 XrmStringToClassList(class, classlist
);
641 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
642 && (type
== expected_type
))
644 if (type
== x_rm_string
)
645 ret_value
->addr
= (char *) value
.addr
;
647 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
655 /* Retrieve the string resource specified by NAME with CLASS from
659 x_get_string_resource (XrmDatabase rdb
, char *name
, char *class)
663 if (inhibit_x_resources
)
664 /* --quick was passed, so this is a no-op. */
667 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
668 return (char *) value
.addr
;
673 /* Stand-alone test facilities. */
678 #define arg_listify(len, list) (list)
679 #define car(list) (*(list))
680 #define cdr(list) (list + 1)
681 #define NIL(list) (! *(list))
682 #define free_arglist(list)
691 for (p
= list
; ! NIL (p
); p
= cdr (p
))
692 if (! strcmp (elt
, car (p
)))
699 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
701 int x1
, x2
, x3
, x4
, x5
;
706 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
715 char *displayname
, *resource_string
, *class, *name
;
719 arg_list
= arg_listify (argc
, argv
);
721 lp
= member ("-d", arg_list
);
723 displayname
= car (cdr (lp
));
725 displayname
= "localhost:0.0";
727 lp
= member ("-xrm", arg_list
);
729 resource_string
= car (cdr (lp
));
731 resource_string
= (char *) 0;
733 lp
= member ("-c", arg_list
);
735 class = car (cdr (lp
));
739 lp
= member ("-n", arg_list
);
741 name
= car (cdr (lp
));
745 free_arglist (arg_list
);
747 if (!(display
= XOpenDisplay (displayname
)))
748 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
750 xdb
= x_load_resources (display
, resource_string
, name
, class);
752 /* In a real program, you'd want to also do this: */
758 char query_class
[90];
763 if (strlen (query_name
))
770 value
= x_get_string_resource (xdb
, query_name
, query_class
);
773 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
775 printf ("\tNo Value.\n\n");
780 printf ("\tExit.\n\n");
782 XCloseDisplay (display
);
786 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
787 (do not change this comment) */