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
, const char *class)
83 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
85 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
88 sprintf (full_name
, "%s.%s", name
, "customization");
89 sprintf (full_class
, "%s.%s", class, "Customization");
91 result
= x_get_string_resource (db
, full_name
, full_class
);
95 char *copy
= (char *) xmalloc (strlen (result
) + 1);
96 strcpy (copy
, result
);
104 /* Expand all the Xt-style %-escapes in STRING, whose length is given
105 by STRING_LEN. Here are the escapes we're supposed to recognize:
107 %N The value of the application's class name
108 %T The value of the type parameter ("app-defaults" in this
110 %S The value of the suffix parameter ("" in this context)
111 %L The language string associated with the specified display
112 (We use the "LANG" environment variable here, if it's set.)
113 %l The language part of the display's language string
114 (We treat this just like %L. If someone can tell us what
115 we're really supposed to do, dandy.)
116 %t The territory part of the display's language string
117 (This never gets used.)
118 %c The codeset part of the display's language string
119 (This never gets used either.)
120 %C The customization string retrieved from the resource
121 database associated with display.
122 (This is x_customization_string.)
124 Return the expanded file name if it exists and is readable, and
125 refers to %L only when the LANG environment variable is set, or
126 otherwise provided by X.
128 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
129 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
132 Return NULL otherwise. */
135 magic_file_p (const char *string
, EMACS_INT string_len
, const char *class, const char *escaped_suffix
, const char *suffix
)
137 char *lang
= getenv ("LANG");
140 char *path
= (char *) xmalloc (path_size
);
143 const char *p
= string
;
145 while (p
< string
+ string_len
)
147 /* The chunk we're about to stick on the end of result. */
148 const char *next
= NULL
;
155 if (p
>= string
+ string_len
)
166 next
= (x_customization_string
167 ? x_customization_string
169 next_len
= strlen (next
);
174 next_len
= strlen (class);
178 next
= "app-defaults";
179 next_len
= strlen (next
);
196 next_len
= strlen (next
);
206 next
= p
, next_len
= 1;
208 /* Do we have room for this component followed by a '\0' ? */
209 if (path_len
+ next_len
+ 1 > path_size
)
211 path_size
= (path_len
+ next_len
+ 1) * 2;
212 path
= (char *) xrealloc (path
, path_size
);
215 memcpy (path
+ path_len
, next
, next_len
);
216 path_len
+= next_len
;
220 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
221 if (p
>= string
+ string_len
&& escaped_suffix
)
223 string
= escaped_suffix
;
224 string_len
= strlen (string
);
226 escaped_suffix
= NULL
;
230 /* Perhaps we should add the SUFFIX now. */
233 int suffix_len
= strlen (suffix
);
235 if (path_len
+ suffix_len
+ 1 > path_size
)
237 path_size
= (path_len
+ suffix_len
+ 1);
238 path
= (char *) xrealloc (path
, path_size
);
241 memcpy (path
+ path_len
, suffix
, suffix_len
);
242 path_len
+= suffix_len
;
245 path
[path_len
] = '\0';
264 if ((ptr
= getenv ("HOME")) == NULL
)
266 if ((ptr
= getenv ("LOGNAME")) != NULL
267 || (ptr
= getenv ("USER")) != NULL
)
270 pw
= getpwuid (getuid ());
277 return xstrdup ("/");
279 copy
= (char *) xmalloc (strlen (ptr
) + 2);
288 file_p (const char *filename
)
292 return (access (filename
, 4) == 0 /* exists and is readable */
293 && stat (filename
, &status
) == 0 /* get the status */
294 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
298 /* Find the first element of SEARCH_PATH which exists and is readable,
299 after expanding the %-escapes. Return 0 if we didn't find any, and
300 the path name of the one we found otherwise. */
303 search_magic_path (const char *search_path
, const char *class, const char *escaped_suffix
, const char *suffix
)
307 for (s
= search_path
; *s
; s
= p
)
309 for (p
= s
; *p
&& *p
!= ':'; p
++)
314 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
,
324 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
336 /* Producing databases for individual sources. */
339 get_system_app (const char *class)
341 XrmDatabase db
= NULL
;
345 path
= getenv ("XFILESEARCHPATH");
346 if (! path
) path
= PATH_X_DEFAULTS
;
348 p
= search_magic_path (path
, class, 0, 0);
351 db
= XrmGetFileDatabase (p
);
360 get_fallback (Display
*display
)
367 get_user_app (const char *class)
373 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
374 names, not directories. */
375 if (((path
= getenv ("XUSERFILESEARCHPATH"))
376 && (file
= search_magic_path (path
, class, 0, 0)))
378 /* Check for APPLRESDIR; it is a path of directories. In each,
379 we have to search for LANG/CLASS and then CLASS. */
380 || ((path
= getenv ("XAPPLRESDIR"))
381 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
382 || (file
= search_magic_path (path
, class, "/%N", 0))))
384 /* Check in the home directory. This is a bit of a hack; let's
385 hope one's home directory doesn't contain any %-escapes. */
386 || (free_it
= gethomedir (),
387 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
388 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
390 XrmDatabase db
= XrmGetFileDatabase (file
);
402 get_user_db (Display
*display
)
407 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
408 xdefs
= XResourceManagerString (display
);
410 xdefs
= display
->xdefaults
;
414 db
= XrmGetStringDatabase (xdefs
);
420 home
= gethomedir ();
421 xdefault
= (char *) xmalloc (strlen (home
) + sizeof (".Xdefaults"));
422 strcpy (xdefault
, home
);
423 strcat (xdefault
, ".Xdefaults");
424 db
= XrmGetFileDatabase (xdefault
);
429 #ifdef HAVE_XSCREENRESOURCESTRING
430 /* Get the screen-specific resources too. */
431 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
434 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
443 get_environ_db (void)
447 char *path
= 0, *home
= 0;
450 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
452 home
= gethomedir ();
453 host
= get_system_name ();
454 path
= (char *) xmalloc (strlen (home
)
455 + sizeof (".Xdefaults-")
457 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
461 db
= XrmGetFileDatabase (p
);
469 /* External interface. */
471 /* Types of values that we can find in a database */
473 #define XrmStringType "String" /* String representation */
474 XrmRepresentation x_rm_string
; /* Quark representation */
476 /* Load X resources based on the display and a possible -xrm option. */
479 x_load_resources (Display
*display
, const char *xrm_string
,
480 const char *myname
, const char *myclass
)
482 XrmDatabase user_database
;
487 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
490 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
493 x_rm_string
= XrmStringToQuark (XrmStringType
);
494 #ifndef USE_X_TOOLKIT
495 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
496 I suspect it's because the toolkit version does this elsewhere. */
499 rdb
= XrmGetStringDatabase ("");
501 /* Add some font defaults. If the font `helv' doesn't exist, widgets
502 will use some other default font. */
505 sprintf (line
, "%s.pane.background: grey75", myclass
);
506 XrmPutLineResource (&rdb
, line
);
507 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
508 XrmPutLineResource (&rdb
, line
);
509 sprintf (line
, "%s*menu*background: grey75", myclass
);
510 XrmPutLineResource (&rdb
, line
);
511 sprintf (line
, "%s*menubar*background: grey75", myclass
);
512 XrmPutLineResource (&rdb
, line
);
513 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
514 XrmPutLineResource (&rdb
, line
);
515 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
516 XrmPutLineResource (&rdb
, line
);
517 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
518 XrmPutLineResource (&rdb
, line
);
519 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
520 XrmPutLineResource (&rdb
, line
);
521 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
522 XrmPutLineResource (&rdb
, line
);
523 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
524 XrmPutLineResource (&rdb
, line
);
525 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
526 XrmPutLineResource (&rdb
, line
);
527 sprintf (line
, "%s*fsb*background: grey75", myclass
);
528 XrmPutLineResource (&rdb
, line
);
529 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
530 XrmPutLineResource (&rdb
, line
);
531 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
532 XrmPutLineResource (&rdb
, line
);
533 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
534 XrmPutLineResource (&rdb
, line
);
535 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
536 XrmPutLineResource (&rdb
, line
);
538 /* Set double click time of list boxes in the file selection
539 dialog from `double-click-time'. */
540 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
542 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
543 myclass
, XFASTINT (Vdouble_click_time
));
544 XrmPutLineResource (&rdb
, line
);
545 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
546 myclass
, XFASTINT (Vdouble_click_time
));
547 XrmPutLineResource (&rdb
, line
);
550 #else /* not USE_MOTIF */
552 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
553 XrmPutLineResource (&rdb
, line
);
554 sprintf (line
, "Emacs.dialog*.background: grey75");
555 XrmPutLineResource (&rdb
, line
);
556 sprintf (line
, "*XlwMenu*font: %s", helv
);
557 XrmPutLineResource (&rdb
, line
);
558 sprintf (line
, "*XlwMenu*background: grey75");
559 XrmPutLineResource (&rdb
, line
);
560 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
561 XrmPutLineResource (&rdb
, line
);
563 #endif /* not USE_MOTIF */
565 user_database
= get_user_db (display
);
567 /* Figure out what the "customization string" is, so we can use it
569 xfree (x_customization_string
);
570 x_customization_string
571 = x_get_customization_string (user_database
, myname
, myclass
);
573 /* Get application system defaults */
574 db
= get_system_app (myclass
);
576 XrmMergeDatabases (db
, &rdb
);
578 /* Get Fallback resources */
579 db
= get_fallback (display
);
581 XrmMergeDatabases (db
, &rdb
);
583 /* Get application user defaults */
584 db
= get_user_app (myclass
);
586 XrmMergeDatabases (db
, &rdb
);
588 /* get User defaults */
589 if (user_database
!= NULL
)
590 XrmMergeDatabases (user_database
, &rdb
);
592 /* Get Environment defaults. */
593 db
= get_environ_db ();
595 XrmMergeDatabases (db
, &rdb
);
597 /* Last, merge in any specification from the command line. */
598 if (xrm_string
!= NULL
)
600 db
= XrmGetStringDatabase (xrm_string
);
602 XrmMergeDatabases (db
, &rdb
);
609 /* Retrieve the value of the resource specified by NAME with class CLASS
610 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
613 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class, XrmRepresentation expected_type
, XrmValue
*ret_value
)
616 XrmName namelist
[100];
617 XrmClass classlist
[100];
618 XrmRepresentation type
;
620 XrmStringToNameList(name
, namelist
);
621 XrmStringToClassList(class, classlist
);
623 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
624 && (type
== expected_type
))
626 if (type
== x_rm_string
)
627 ret_value
->addr
= (char *) value
.addr
;
629 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
637 /* Retrieve the string resource specified by NAME with CLASS from
641 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
645 if (inhibit_x_resources
)
646 /* --quick was passed, so this is a no-op. */
649 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
650 return (char *) value
.addr
;
655 /* Stand-alone test facilities. */
660 #define arg_listify(len, list) (list)
661 #define car(list) (*(list))
662 #define cdr(list) (list + 1)
663 #define NIL(list) (! *(list))
664 #define free_arglist(list)
673 for (p
= list
; ! NIL (p
); p
= cdr (p
))
674 if (! strcmp (elt
, car (p
)))
681 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
683 int x1
, x2
, x3
, x4
, x5
;
688 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
697 char *displayname
, *resource_string
, *class, *name
;
701 arg_list
= arg_listify (argc
, argv
);
703 lp
= member ("-d", arg_list
);
705 displayname
= car (cdr (lp
));
707 displayname
= "localhost:0.0";
709 lp
= member ("-xrm", arg_list
);
711 resource_string
= car (cdr (lp
));
713 resource_string
= (char *) 0;
715 lp
= member ("-c", arg_list
);
717 class = car (cdr (lp
));
721 lp
= member ("-n", arg_list
);
723 name
= car (cdr (lp
));
727 free_arglist (arg_list
);
729 if (!(display
= XOpenDisplay (displayname
)))
730 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
732 xdb
= x_load_resources (display
, resource_string
, name
, class);
734 /* In a real program, you'd want to also do this: */
740 char query_class
[90];
745 if (strlen (query_name
))
752 value
= x_get_string_resource (xdb
, query_name
, query_class
);
755 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
757 printf ("\tNo Value.\n\n");
762 printf ("\tExit.\n\n");
764 XCloseDisplay (display
);
768 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
769 (do not change this comment) */