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/>. */
36 #include <X11/Xatom.h>
38 #include <X11/Xutil.h>
39 #include <X11/Xresource.h>
45 #if !defined(S_ISDIR) && defined(S_IFDIR)
46 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
52 /* For Vdouble_click_time. */
56 extern char *getenv (const char *);
58 extern struct passwd
*getpwuid (uid_t
);
59 extern struct passwd
*getpwnam (const char *);
61 extern const char *get_system_name (void);
63 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
65 static int file_p (const char *filename
);
68 /* X file search path processing. */
71 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
72 and friends, or zero if none was specified. */
73 char *x_customization_string
;
76 /* Return the value of the emacs.customization (Emacs.Customization)
77 resource, for later use in search path decoding. If we find no
78 such resource, return zero. */
80 x_get_customization_string (XrmDatabase db
, const char *name
,
84 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
86 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
89 sprintf (full_name
, "%s.%s", name
, "customization");
90 sprintf (full_class
, "%s.%s", class, "Customization");
92 result
= x_get_string_resource (db
, full_name
, full_class
);
96 char *copy
= (char *) xmalloc (strlen (result
) + 1);
97 strcpy (copy
, result
);
105 /* Expand all the Xt-style %-escapes in STRING, whose length is given
106 by STRING_LEN. Here are the escapes we're supposed to recognize:
108 %N The value of the application's class name
109 %T The value of the type parameter ("app-defaults" in this
111 %S The value of the suffix parameter ("" in this context)
112 %L The language string associated with the specified display
113 (We use the "LANG" environment variable here, if it's set.)
114 %l The language part of the display's language string
115 (We treat this just like %L. If someone can tell us what
116 we're really supposed to do, dandy.)
117 %t The territory part of the display's language string
118 (This never gets used.)
119 %c The codeset part of the display's language string
120 (This never gets used either.)
121 %C The customization string retrieved from the resource
122 database associated with display.
123 (This is x_customization_string.)
125 Return the expanded file name if it exists and is readable, and
126 refers to %L only when the LANG environment variable is set, or
127 otherwise provided by X.
129 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
130 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
133 Return NULL otherwise. */
136 magic_file_p (const char *string
, EMACS_INT string_len
, const char *class, const char *escaped_suffix
, const char *suffix
)
138 char *lang
= getenv ("LANG");
141 char *path
= (char *) xmalloc (path_size
);
144 const char *p
= string
;
146 while (p
< string
+ string_len
)
148 /* The chunk we're about to stick on the end of result. */
149 const char *next
= NULL
;
156 if (p
>= string
+ string_len
)
167 next
= (x_customization_string
168 ? x_customization_string
170 next_len
= strlen (next
);
175 next_len
= strlen (class);
179 next
= "app-defaults";
180 next_len
= strlen (next
);
197 next_len
= strlen (next
);
207 next
= p
, next_len
= 1;
209 /* Do we have room for this component followed by a '\0' ? */
210 if (path_len
+ next_len
+ 1 > path_size
)
212 path_size
= (path_len
+ next_len
+ 1) * 2;
213 path
= (char *) xrealloc (path
, path_size
);
216 memcpy (path
+ path_len
, next
, next_len
);
217 path_len
+= next_len
;
221 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
222 if (p
>= string
+ string_len
&& escaped_suffix
)
224 string
= escaped_suffix
;
225 string_len
= strlen (string
);
227 escaped_suffix
= NULL
;
231 /* Perhaps we should add the SUFFIX now. */
234 int suffix_len
= strlen (suffix
);
236 if (path_len
+ suffix_len
+ 1 > path_size
)
238 path_size
= (path_len
+ suffix_len
+ 1);
239 path
= (char *) xrealloc (path
, path_size
);
242 memcpy (path
+ path_len
, suffix
, suffix_len
);
243 path_len
+= suffix_len
;
246 path
[path_len
] = '\0';
265 if ((ptr
= getenv ("HOME")) == NULL
)
267 if ((ptr
= getenv ("LOGNAME")) != NULL
268 || (ptr
= getenv ("USER")) != NULL
)
271 pw
= getpwuid (getuid ());
278 return xstrdup ("/");
280 copy
= (char *) xmalloc (strlen (ptr
) + 2);
289 file_p (const char *filename
)
293 return (access (filename
, 4) == 0 /* exists and is readable */
294 && stat (filename
, &status
) == 0 /* get the status */
295 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
299 /* Find the first element of SEARCH_PATH which exists and is readable,
300 after expanding the %-escapes. Return 0 if we didn't find any, and
301 the path name of the one we found otherwise. */
304 search_magic_path (const char *search_path
, const char *class, const char *escaped_suffix
, const char *suffix
)
308 for (s
= search_path
; *s
; s
= p
)
310 for (p
= s
; *p
&& *p
!= ':'; p
++)
315 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
,
325 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
337 /* Producing databases for individual sources. */
340 get_system_app (const char *class)
342 XrmDatabase db
= NULL
;
346 path
= getenv ("XFILESEARCHPATH");
347 if (! path
) path
= PATH_X_DEFAULTS
;
349 p
= search_magic_path (path
, class, 0, 0);
352 db
= XrmGetFileDatabase (p
);
361 get_fallback (Display
*display
)
368 get_user_app (const char *class)
374 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
375 names, not directories. */
376 if (((path
= getenv ("XUSERFILESEARCHPATH"))
377 && (file
= search_magic_path (path
, class, 0, 0)))
379 /* Check for APPLRESDIR; it is a path of directories. In each,
380 we have to search for LANG/CLASS and then CLASS. */
381 || ((path
= getenv ("XAPPLRESDIR"))
382 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
383 || (file
= search_magic_path (path
, class, "/%N", 0))))
385 /* Check in the home directory. This is a bit of a hack; let's
386 hope one's home directory doesn't contain any %-escapes. */
387 || (free_it
= gethomedir (),
388 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
389 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
391 XrmDatabase db
= XrmGetFileDatabase (file
);
403 get_user_db (Display
*display
)
408 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
409 xdefs
= XResourceManagerString (display
);
411 xdefs
= display
->xdefaults
;
415 db
= XrmGetStringDatabase (xdefs
);
421 home
= gethomedir ();
422 xdefault
= (char *) xmalloc (strlen (home
) + sizeof (".Xdefaults"));
423 strcpy (xdefault
, home
);
424 strcat (xdefault
, ".Xdefaults");
425 db
= XrmGetFileDatabase (xdefault
);
430 #ifdef HAVE_XSCREENRESOURCESTRING
431 /* Get the screen-specific resources too. */
432 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
435 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
444 get_environ_db (void)
448 char *path
= 0, *home
= 0;
451 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
453 home
= gethomedir ();
454 host
= get_system_name ();
455 path
= (char *) xmalloc (strlen (home
)
456 + sizeof (".Xdefaults-")
458 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
462 db
= XrmGetFileDatabase (p
);
470 /* External interface. */
472 /* Types of values that we can find in a database */
474 #define XrmStringType "String" /* String representation */
475 XrmRepresentation x_rm_string
; /* Quark representation */
477 /* Load X resources based on the display and a possible -xrm option. */
480 x_load_resources (Display
*display
, const char *xrm_string
,
481 const char *myname
, const char *myclass
)
483 XrmDatabase user_database
;
488 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
491 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
494 x_rm_string
= XrmStringToQuark (XrmStringType
);
495 #ifndef USE_X_TOOLKIT
496 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
497 I suspect it's because the toolkit version does this elsewhere. */
500 rdb
= XrmGetStringDatabase ("");
502 /* Add some font defaults. If the font `helv' doesn't exist, widgets
503 will use some other default font. */
506 sprintf (line
, "%s.pane.background: grey75", myclass
);
507 XrmPutLineResource (&rdb
, line
);
508 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
509 XrmPutLineResource (&rdb
, line
);
510 sprintf (line
, "%s*menu*background: grey75", myclass
);
511 XrmPutLineResource (&rdb
, line
);
512 sprintf (line
, "%s*menubar*background: grey75", myclass
);
513 XrmPutLineResource (&rdb
, line
);
514 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
515 XrmPutLineResource (&rdb
, line
);
516 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
517 XrmPutLineResource (&rdb
, line
);
518 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
519 XrmPutLineResource (&rdb
, line
);
520 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
521 XrmPutLineResource (&rdb
, line
);
522 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
523 XrmPutLineResource (&rdb
, line
);
524 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
525 XrmPutLineResource (&rdb
, line
);
526 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
527 XrmPutLineResource (&rdb
, line
);
528 sprintf (line
, "%s*fsb*background: grey75", myclass
);
529 XrmPutLineResource (&rdb
, line
);
530 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
531 XrmPutLineResource (&rdb
, line
);
532 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
533 XrmPutLineResource (&rdb
, line
);
534 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
535 XrmPutLineResource (&rdb
, line
);
536 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
537 XrmPutLineResource (&rdb
, line
);
539 /* Set double click time of list boxes in the file selection
540 dialog from `double-click-time'. */
541 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
543 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
544 myclass
, XFASTINT (Vdouble_click_time
));
545 XrmPutLineResource (&rdb
, line
);
546 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
547 myclass
, XFASTINT (Vdouble_click_time
));
548 XrmPutLineResource (&rdb
, line
);
551 #else /* not USE_MOTIF */
553 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
554 XrmPutLineResource (&rdb
, line
);
555 sprintf (line
, "Emacs.dialog*.background: grey75");
556 XrmPutLineResource (&rdb
, line
);
557 sprintf (line
, "*XlwMenu*font: %s", helv
);
558 XrmPutLineResource (&rdb
, line
);
559 sprintf (line
, "*XlwMenu*background: grey75");
560 XrmPutLineResource (&rdb
, line
);
561 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
562 XrmPutLineResource (&rdb
, line
);
564 #endif /* not USE_MOTIF */
566 user_database
= get_user_db (display
);
568 /* Figure out what the "customization string" is, so we can use it
570 xfree (x_customization_string
);
571 x_customization_string
572 = x_get_customization_string (user_database
, myname
, myclass
);
574 /* Get application system defaults */
575 db
= get_system_app (myclass
);
577 XrmMergeDatabases (db
, &rdb
);
579 /* Get Fallback resources */
580 db
= get_fallback (display
);
582 XrmMergeDatabases (db
, &rdb
);
584 /* Get application user defaults */
585 db
= get_user_app (myclass
);
587 XrmMergeDatabases (db
, &rdb
);
589 /* get User defaults */
590 if (user_database
!= NULL
)
591 XrmMergeDatabases (user_database
, &rdb
);
593 /* Get Environment defaults. */
594 db
= get_environ_db ();
596 XrmMergeDatabases (db
, &rdb
);
598 /* Last, merge in any specification from the command line. */
599 if (xrm_string
!= NULL
)
601 db
= XrmGetStringDatabase (xrm_string
);
603 XrmMergeDatabases (db
, &rdb
);
610 /* Retrieve the value of the resource specified by NAME with class CLASS
611 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
614 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
615 XrmRepresentation expected_type
, XrmValue
*ret_value
)
618 XrmName namelist
[100];
619 XrmClass classlist
[100];
620 XrmRepresentation type
;
622 XrmStringToNameList(name
, namelist
);
623 XrmStringToClassList(class, classlist
);
625 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
626 && (type
== expected_type
))
628 if (type
== x_rm_string
)
629 ret_value
->addr
= (char *) value
.addr
;
631 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
639 /* Retrieve the string resource specified by NAME with CLASS from
643 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
647 if (inhibit_x_resources
)
648 /* --quick was passed, so this is a no-op. */
651 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
652 return (char *) value
.addr
;
657 /* Stand-alone test facilities. */
662 #define arg_listify(len, list) (list)
663 #define car(list) (*(list))
664 #define cdr(list) (list + 1)
665 #define NIL(list) (! *(list))
666 #define free_arglist(list)
675 for (p
= list
; ! NIL (p
); p
= cdr (p
))
676 if (! strcmp (elt
, car (p
)))
683 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
685 int x1
, x2
, x3
, x4
, x5
;
690 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
699 char *displayname
, *resource_string
, *class, *name
;
703 arg_list
= arg_listify (argc
, argv
);
705 lp
= member ("-d", arg_list
);
707 displayname
= car (cdr (lp
));
709 displayname
= "localhost:0.0";
711 lp
= member ("-xrm", arg_list
);
713 resource_string
= car (cdr (lp
));
715 resource_string
= (char *) 0;
717 lp
= member ("-c", arg_list
);
719 class = car (cdr (lp
));
723 lp
= member ("-n", arg_list
);
725 name
= car (cdr (lp
));
729 free_arglist (arg_list
);
731 if (!(display
= XOpenDisplay (displayname
)))
732 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
734 xdb
= x_load_resources (display
, resource_string
, name
, class);
736 /* In a real program, you'd want to also do this: */
742 char query_class
[90];
747 if (strlen (query_name
))
754 value
= x_get_string_resource (xdb
, query_name
, query_class
);
757 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
759 printf ("\tNo Value.\n\n");
764 printf ("\tExit.\n\n");
766 XCloseDisplay (display
);
770 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
771 (do not change this comment) */