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, 2011 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/>. */
33 #include <X11/Xatom.h>
35 #include <X11/Xutil.h>
36 #include <X11/Xresource.h>
42 #if !defined(S_ISDIR) && defined(S_IFDIR)
43 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
49 /* For Vdouble_click_time. */
53 extern char *getenv (const char *);
55 extern struct passwd
*getpwuid (uid_t
);
56 extern struct passwd
*getpwnam (const char *);
58 extern const char *get_system_name (void);
60 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
62 static int file_p (const char *filename
);
65 /* X file search path processing. */
68 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
69 and friends, or zero if none was specified. */
70 char *x_customization_string
;
73 /* Return the value of the emacs.customization (Emacs.Customization)
74 resource, for later use in search path decoding. If we find no
75 such resource, return zero. */
77 x_get_customization_string (XrmDatabase db
, const char *name
,
81 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
83 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
86 sprintf (full_name
, "%s.%s", name
, "customization");
87 sprintf (full_class
, "%s.%s", class, "Customization");
89 result
= x_get_string_resource (db
, full_name
, full_class
);
93 char *copy
= (char *) xmalloc (strlen (result
) + 1);
94 strcpy (copy
, result
);
102 /* Expand all the Xt-style %-escapes in STRING, whose length is given
103 by STRING_LEN. Here are the escapes we're supposed to recognize:
105 %N The value of the application's class name
106 %T The value of the type parameter ("app-defaults" in this
108 %S The value of the suffix parameter ("" in this context)
109 %L The language string associated with the specified display
110 (We use the "LANG" environment variable here, if it's set.)
111 %l The language part of the display's language string
112 (We treat this just like %L. If someone can tell us what
113 we're really supposed to do, dandy.)
114 %t The territory part of the display's language string
115 (This never gets used.)
116 %c The codeset part of the display's language string
117 (This never gets used either.)
118 %C The customization string retrieved from the resource
119 database associated with display.
120 (This is x_customization_string.)
122 Return the expanded file name if it exists and is readable, and
123 refers to %L only when the LANG environment variable is set, or
124 otherwise provided by X.
126 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
127 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
130 Return NULL otherwise. */
133 magic_file_p (const char *string
, EMACS_INT string_len
, const char *class, const char *escaped_suffix
, const char *suffix
)
135 char *lang
= getenv ("LANG");
138 char *path
= (char *) xmalloc (path_size
);
141 const char *p
= string
;
143 while (p
< string
+ string_len
)
145 /* The chunk we're about to stick on the end of result. */
146 const char *next
= NULL
;
153 if (p
>= string
+ string_len
)
164 next
= (x_customization_string
165 ? x_customization_string
167 next_len
= strlen (next
);
172 next_len
= strlen (class);
176 next
= "app-defaults";
177 next_len
= strlen (next
);
194 next_len
= strlen (next
);
204 next
= p
, next_len
= 1;
206 /* Do we have room for this component followed by a '\0' ? */
207 if (path_len
+ next_len
+ 1 > path_size
)
209 path_size
= (path_len
+ next_len
+ 1) * 2;
210 path
= (char *) xrealloc (path
, path_size
);
213 memcpy (path
+ path_len
, next
, next_len
);
214 path_len
+= next_len
;
218 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
219 if (p
>= string
+ string_len
&& escaped_suffix
)
221 string
= escaped_suffix
;
222 string_len
= strlen (string
);
224 escaped_suffix
= NULL
;
228 /* Perhaps we should add the SUFFIX now. */
231 int suffix_len
= strlen (suffix
);
233 if (path_len
+ suffix_len
+ 1 > path_size
)
235 path_size
= (path_len
+ suffix_len
+ 1);
236 path
= (char *) xrealloc (path
, path_size
);
239 memcpy (path
+ path_len
, suffix
, suffix_len
);
240 path_len
+= suffix_len
;
243 path
[path_len
] = '\0';
262 if ((ptr
= getenv ("HOME")) == NULL
)
264 if ((ptr
= getenv ("LOGNAME")) != NULL
265 || (ptr
= getenv ("USER")) != NULL
)
268 pw
= getpwuid (getuid ());
275 return xstrdup ("/");
277 copy
= (char *) xmalloc (strlen (ptr
) + 2);
286 file_p (const char *filename
)
290 return (access (filename
, 4) == 0 /* exists and is readable */
291 && stat (filename
, &status
) == 0 /* get the status */
292 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
296 /* Find the first element of SEARCH_PATH which exists and is readable,
297 after expanding the %-escapes. Return 0 if we didn't find any, and
298 the path name of the one we found otherwise. */
301 search_magic_path (const char *search_path
, const char *class, const char *escaped_suffix
, const char *suffix
)
305 for (s
= search_path
; *s
; s
= p
)
307 for (p
= s
; *p
&& *p
!= ':'; p
++)
312 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
,
322 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
334 /* Producing databases for individual sources. */
337 get_system_app (const char *class)
339 XrmDatabase db
= NULL
;
343 path
= getenv ("XFILESEARCHPATH");
344 if (! path
) path
= PATH_X_DEFAULTS
;
346 p
= search_magic_path (path
, class, 0, 0);
349 db
= XrmGetFileDatabase (p
);
358 get_fallback (Display
*display
)
365 get_user_app (const char *class)
371 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
372 names, not directories. */
373 if (((path
= getenv ("XUSERFILESEARCHPATH"))
374 && (file
= search_magic_path (path
, class, 0, 0)))
376 /* Check for APPLRESDIR; it is a path of directories. In each,
377 we have to search for LANG/CLASS and then CLASS. */
378 || ((path
= getenv ("XAPPLRESDIR"))
379 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
380 || (file
= search_magic_path (path
, class, "/%N", 0))))
382 /* Check in the home directory. This is a bit of a hack; let's
383 hope one's home directory doesn't contain any %-escapes. */
384 || (free_it
= gethomedir (),
385 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
386 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
388 XrmDatabase db
= XrmGetFileDatabase (file
);
400 get_user_db (Display
*display
)
405 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
406 xdefs
= XResourceManagerString (display
);
408 xdefs
= display
->xdefaults
;
412 db
= XrmGetStringDatabase (xdefs
);
418 home
= gethomedir ();
419 xdefault
= (char *) xmalloc (strlen (home
) + sizeof (".Xdefaults"));
420 strcpy (xdefault
, home
);
421 strcat (xdefault
, ".Xdefaults");
422 db
= XrmGetFileDatabase (xdefault
);
427 #ifdef HAVE_XSCREENRESOURCESTRING
428 /* Get the screen-specific resources too. */
429 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
432 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
441 get_environ_db (void)
445 char *path
= 0, *home
= 0;
448 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
450 home
= gethomedir ();
451 host
= get_system_name ();
452 path
= (char *) xmalloc (strlen (home
)
453 + sizeof (".Xdefaults-")
455 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
459 db
= XrmGetFileDatabase (p
);
467 /* External interface. */
469 /* Types of values that we can find in a database */
471 #define XrmStringType "String" /* String representation */
472 XrmRepresentation x_rm_string
; /* Quark representation */
474 /* Load X resources based on the display and a possible -xrm option. */
477 x_load_resources (Display
*display
, const char *xrm_string
,
478 const char *myname
, const char *myclass
)
480 XrmDatabase user_database
;
485 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
488 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
491 x_rm_string
= XrmStringToQuark (XrmStringType
);
492 #ifndef USE_X_TOOLKIT
493 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
494 I suspect it's because the toolkit version does this elsewhere. */
497 rdb
= XrmGetStringDatabase ("");
499 /* Add some font defaults. If the font `helv' doesn't exist, widgets
500 will use some other default font. */
503 sprintf (line
, "%s.pane.background: grey75", myclass
);
504 XrmPutLineResource (&rdb
, line
);
505 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
506 XrmPutLineResource (&rdb
, line
);
507 sprintf (line
, "%s*menu*background: grey75", myclass
);
508 XrmPutLineResource (&rdb
, line
);
509 sprintf (line
, "%s*menubar*background: grey75", myclass
);
510 XrmPutLineResource (&rdb
, line
);
511 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
512 XrmPutLineResource (&rdb
, line
);
513 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
514 XrmPutLineResource (&rdb
, line
);
515 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
516 XrmPutLineResource (&rdb
, line
);
517 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
518 XrmPutLineResource (&rdb
, line
);
519 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
520 XrmPutLineResource (&rdb
, line
);
521 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
522 XrmPutLineResource (&rdb
, line
);
523 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
524 XrmPutLineResource (&rdb
, line
);
525 sprintf (line
, "%s*fsb*background: grey75", myclass
);
526 XrmPutLineResource (&rdb
, line
);
527 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
528 XrmPutLineResource (&rdb
, line
);
529 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
530 XrmPutLineResource (&rdb
, line
);
531 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
532 XrmPutLineResource (&rdb
, line
);
533 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
534 XrmPutLineResource (&rdb
, line
);
536 /* Set double click time of list boxes in the file selection
537 dialog from `double-click-time'. */
538 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
540 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
541 myclass
, XFASTINT (Vdouble_click_time
));
542 XrmPutLineResource (&rdb
, line
);
543 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
544 myclass
, XFASTINT (Vdouble_click_time
));
545 XrmPutLineResource (&rdb
, line
);
548 #else /* not USE_MOTIF */
550 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
551 XrmPutLineResource (&rdb
, line
);
552 sprintf (line
, "Emacs.dialog*.background: grey75");
553 XrmPutLineResource (&rdb
, line
);
554 sprintf (line
, "*XlwMenu*font: %s", helv
);
555 XrmPutLineResource (&rdb
, line
);
556 sprintf (line
, "*XlwMenu*background: grey75");
557 XrmPutLineResource (&rdb
, line
);
558 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
559 XrmPutLineResource (&rdb
, line
);
561 #endif /* not USE_MOTIF */
563 user_database
= get_user_db (display
);
565 /* Figure out what the "customization string" is, so we can use it
567 xfree (x_customization_string
);
568 x_customization_string
569 = x_get_customization_string (user_database
, myname
, myclass
);
571 /* Get application system defaults */
572 db
= get_system_app (myclass
);
574 XrmMergeDatabases (db
, &rdb
);
576 /* Get Fallback resources */
577 db
= get_fallback (display
);
579 XrmMergeDatabases (db
, &rdb
);
581 /* Get application user defaults */
582 db
= get_user_app (myclass
);
584 XrmMergeDatabases (db
, &rdb
);
586 /* get User defaults */
587 if (user_database
!= NULL
)
588 XrmMergeDatabases (user_database
, &rdb
);
590 /* Get Environment defaults. */
591 db
= get_environ_db ();
593 XrmMergeDatabases (db
, &rdb
);
595 /* Last, merge in any specification from the command line. */
596 if (xrm_string
!= NULL
)
598 db
= XrmGetStringDatabase (xrm_string
);
600 XrmMergeDatabases (db
, &rdb
);
607 /* Retrieve the value of the resource specified by NAME with class CLASS
608 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
611 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
612 XrmRepresentation expected_type
, XrmValue
*ret_value
)
615 XrmName namelist
[100];
616 XrmClass classlist
[100];
617 XrmRepresentation type
;
619 XrmStringToNameList(name
, namelist
);
620 XrmStringToClassList(class, classlist
);
622 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
623 && (type
== expected_type
))
625 if (type
== x_rm_string
)
626 ret_value
->addr
= (char *) value
.addr
;
628 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
636 /* Retrieve the string resource specified by NAME with CLASS from
640 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
644 if (inhibit_x_resources
)
645 /* --quick was passed, so this is a no-op. */
648 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
649 return (char *) value
.addr
;
654 /* Stand-alone test facilities. */
659 #define arg_listify(len, list) (list)
660 #define car(list) (*(list))
661 #define cdr(list) (list + 1)
662 #define NIL(list) (! *(list))
663 #define free_arglist(list)
672 for (p
= list
; ! NIL (p
); p
= cdr (p
))
673 if (! strcmp (elt
, car (p
)))
680 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
682 int x1
, x2
, x3
, x4
, x5
;
687 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
696 char *displayname
, *resource_string
, *class, *name
;
700 arg_list
= arg_listify (argc
, argv
);
702 lp
= member ("-d", arg_list
);
704 displayname
= car (cdr (lp
));
706 displayname
= "localhost:0.0";
708 lp
= member ("-xrm", arg_list
);
710 resource_string
= car (cdr (lp
));
712 resource_string
= (char *) 0;
714 lp
= member ("-c", arg_list
);
716 class = car (cdr (lp
));
720 lp
= member ("-n", arg_list
);
722 name
= car (cdr (lp
));
726 free_arglist (arg_list
);
728 if (!(display
= XOpenDisplay (displayname
)))
729 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
731 xdb
= x_load_resources (display
, resource_string
, name
, class);
733 /* In a real program, you'd want to also do this: */
739 char query_class
[90];
744 if (strlen (query_name
))
751 value
= x_get_string_resource (xdb
, query_name
, query_class
);
754 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
756 printf ("\tNo Value.\n\n");
761 printf ("\tExit.\n\n");
763 XCloseDisplay (display
);
767 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
768 (do not change this comment) */