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)
51 extern char *getenv (const char *);
53 extern struct passwd
*getpwuid (uid_t
);
54 extern struct passwd
*getpwnam (const char *);
56 extern char *get_system_name (void);
58 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
60 static int file_p (const char *filename
);
63 /* X file search path processing. */
66 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
67 and friends, or zero if none was specified. */
68 char *x_customization_string
;
71 /* Return the value of the emacs.customization (Emacs.Customization)
72 resource, for later use in search path decoding. If we find no
73 such resource, return zero. */
75 x_get_customization_string (XrmDatabase db
, const char *name
, const char *class)
78 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
80 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
83 sprintf (full_name
, "%s.%s", name
, "customization");
84 sprintf (full_class
, "%s.%s", class, "Customization");
86 result
= x_get_string_resource (db
, full_name
, full_class
);
90 char *copy
= (char *) xmalloc (strlen (result
) + 1);
91 strcpy (copy
, result
);
99 /* Expand all the Xt-style %-escapes in STRING, whose length is given
100 by STRING_LEN. Here are the escapes we're supposed to recognize:
102 %N The value of the application's class name
103 %T The value of the type parameter ("app-defaults" in this
105 %S The value of the suffix parameter ("" in this context)
106 %L The language string associated with the specified display
107 (We use the "LANG" environment variable here, if it's set.)
108 %l The language part of the display's language string
109 (We treat this just like %L. If someone can tell us what
110 we're really supposed to do, dandy.)
111 %t The territory part of the display's language string
112 (This never gets used.)
113 %c The codeset part of the display's language string
114 (This never gets used either.)
115 %C The customization string retrieved from the resource
116 database associated with display.
117 (This is x_customization_string.)
119 Return the expanded file name if it exists and is readable, and
120 refers to %L only when the LANG environment variable is set, or
121 otherwise provided by X.
123 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
124 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
127 Return NULL otherwise. */
130 magic_file_p (const char *string
, int string_len
, const char *class, const char *escaped_suffix
, const char *suffix
)
132 char *lang
= getenv ("LANG");
135 char *path
= (char *) xmalloc (path_size
);
138 const char *p
= string
;
140 while (p
< string
+ string_len
)
142 /* The chunk we're about to stick on the end of result. */
143 const char *next
= NULL
;
150 if (p
>= string
+ string_len
)
161 next
= (x_customization_string
162 ? x_customization_string
164 next_len
= strlen (next
);
169 next_len
= strlen (class);
173 next
= "app-defaults";
174 next_len
= strlen (next
);
191 next_len
= strlen (next
);
201 next
= p
, next_len
= 1;
203 /* Do we have room for this component followed by a '\0' ? */
204 if (path_len
+ next_len
+ 1 > path_size
)
206 path_size
= (path_len
+ next_len
+ 1) * 2;
207 path
= (char *) xrealloc (path
, path_size
);
210 memcpy (path
+ path_len
, next
, next_len
);
211 path_len
+= next_len
;
215 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
216 if (p
>= string
+ string_len
&& escaped_suffix
)
218 string
= escaped_suffix
;
219 string_len
= strlen (string
);
221 escaped_suffix
= NULL
;
225 /* Perhaps we should add the SUFFIX now. */
228 int suffix_len
= strlen (suffix
);
230 if (path_len
+ suffix_len
+ 1 > path_size
)
232 path_size
= (path_len
+ suffix_len
+ 1);
233 path
= (char *) xrealloc (path
, path_size
);
236 memcpy (path
+ path_len
, suffix
, suffix_len
);
237 path_len
+= suffix_len
;
240 path
[path_len
] = '\0';
259 if ((ptr
= getenv ("HOME")) == NULL
)
261 if ((ptr
= getenv ("LOGNAME")) != NULL
262 || (ptr
= getenv ("USER")) != NULL
)
265 pw
= getpwuid (getuid ());
272 return xstrdup ("/");
274 copy
= (char *) xmalloc (strlen (ptr
) + 2);
283 file_p (const char *filename
)
287 return (access (filename
, 4) == 0 /* exists and is readable */
288 && stat (filename
, &status
) == 0 /* get the status */
289 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
293 /* Find the first element of SEARCH_PATH which exists and is readable,
294 after expanding the %-escapes. Return 0 if we didn't find any, and
295 the path name of the one we found otherwise. */
298 search_magic_path (const char *search_path
, const char *class, const char *escaped_suffix
, const char *suffix
)
302 for (s
= search_path
; *s
; s
= p
)
304 for (p
= s
; *p
&& *p
!= ':'; p
++)
309 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
,
319 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
331 /* Producing databases for individual sources. */
334 get_system_app (const char *class)
336 XrmDatabase db
= NULL
;
339 path
= getenv ("XFILESEARCHPATH");
340 if (! path
) path
= PATH_X_DEFAULTS
;
342 path
= search_magic_path (path
, class, 0, 0);
345 db
= XrmGetFileDatabase (path
);
354 get_fallback (Display
*display
)
361 get_user_app (const char *class)
367 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
368 names, not directories. */
369 if (((path
= getenv ("XUSERFILESEARCHPATH"))
370 && (file
= search_magic_path (path
, class, 0, 0)))
372 /* Check for APPLRESDIR; it is a path of directories. In each,
373 we have to search for LANG/CLASS and then CLASS. */
374 || ((path
= getenv ("XAPPLRESDIR"))
375 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
376 || (file
= search_magic_path (path
, class, "/%N", 0))))
378 /* Check in the home directory. This is a bit of a hack; let's
379 hope one's home directory doesn't contain any %-escapes. */
380 || (free_it
= gethomedir (),
381 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
382 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
384 XrmDatabase db
= XrmGetFileDatabase (file
);
396 get_user_db (Display
*display
)
401 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
402 xdefs
= XResourceManagerString (display
);
404 xdefs
= display
->xdefaults
;
408 db
= XrmGetStringDatabase (xdefs
);
414 home
= gethomedir ();
415 xdefault
= (char *) xmalloc (strlen (home
) + sizeof (".Xdefaults"));
416 strcpy (xdefault
, home
);
417 strcat (xdefault
, ".Xdefaults");
418 db
= XrmGetFileDatabase (xdefault
);
423 #ifdef HAVE_XSCREENRESOURCESTRING
424 /* Get the screen-specific resources too. */
425 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
428 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
437 get_environ_db (void)
441 char *path
= 0, *home
= 0, *host
;
443 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
445 home
= gethomedir ();
446 host
= get_system_name ();
447 path
= (char *) xmalloc (strlen (home
)
448 + sizeof (".Xdefaults-")
450 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
454 db
= XrmGetFileDatabase (p
);
462 /* External interface. */
464 /* Types of values that we can find in a database */
466 #define XrmStringType "String" /* String representation */
467 XrmRepresentation x_rm_string
; /* Quark representation */
469 /* Load X resources based on the display and a possible -xrm option. */
472 x_load_resources (Display
*display
, const char *xrm_string
,
473 const char *myname
, const char *myclass
)
475 XrmDatabase user_database
;
480 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
483 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
486 x_rm_string
= XrmStringToQuark (XrmStringType
);
487 #ifndef USE_X_TOOLKIT
488 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
489 I suspect it's because the toolkit version does this elsewhere. */
492 rdb
= XrmGetStringDatabase ("");
494 /* Add some font defaults. If the font `helv' doesn't exist, widgets
495 will use some other default font. */
498 sprintf (line
, "%s.pane.background: grey75", myclass
);
499 XrmPutLineResource (&rdb
, line
);
500 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
501 XrmPutLineResource (&rdb
, line
);
502 sprintf (line
, "%s*menu*background: grey75", myclass
);
503 XrmPutLineResource (&rdb
, line
);
504 sprintf (line
, "%s*menubar*background: grey75", myclass
);
505 XrmPutLineResource (&rdb
, line
);
506 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
507 XrmPutLineResource (&rdb
, line
);
508 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
509 XrmPutLineResource (&rdb
, line
);
510 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
511 XrmPutLineResource (&rdb
, line
);
512 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
513 XrmPutLineResource (&rdb
, line
);
514 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
515 XrmPutLineResource (&rdb
, line
);
516 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
517 XrmPutLineResource (&rdb
, line
);
518 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
519 XrmPutLineResource (&rdb
, line
);
520 sprintf (line
, "%s*fsb*background: grey75", myclass
);
521 XrmPutLineResource (&rdb
, line
);
522 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
523 XrmPutLineResource (&rdb
, line
);
524 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
525 XrmPutLineResource (&rdb
, line
);
526 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
527 XrmPutLineResource (&rdb
, line
);
528 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
529 XrmPutLineResource (&rdb
, line
);
531 /* Set double click time of list boxes in the file selection
532 dialog from `double-click-time'. */
533 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
535 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
536 myclass
, XFASTINT (Vdouble_click_time
));
537 XrmPutLineResource (&rdb
, line
);
538 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
539 myclass
, XFASTINT (Vdouble_click_time
));
540 XrmPutLineResource (&rdb
, line
);
543 #else /* not USE_MOTIF */
545 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
546 XrmPutLineResource (&rdb
, line
);
547 sprintf (line
, "Emacs.dialog*.background: grey75");
548 XrmPutLineResource (&rdb
, line
);
549 sprintf (line
, "*XlwMenu*font: %s", helv
);
550 XrmPutLineResource (&rdb
, line
);
551 sprintf (line
, "*XlwMenu*background: grey75");
552 XrmPutLineResource (&rdb
, line
);
553 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
554 XrmPutLineResource (&rdb
, line
);
556 #endif /* not USE_MOTIF */
558 user_database
= get_user_db (display
);
560 /* Figure out what the "customization string" is, so we can use it
562 xfree (x_customization_string
);
563 x_customization_string
564 = x_get_customization_string (user_database
, myname
, myclass
);
566 /* Get application system defaults */
567 db
= get_system_app (myclass
);
569 XrmMergeDatabases (db
, &rdb
);
571 /* Get Fallback resources */
572 db
= get_fallback (display
);
574 XrmMergeDatabases (db
, &rdb
);
576 /* Get application user defaults */
577 db
= get_user_app (myclass
);
579 XrmMergeDatabases (db
, &rdb
);
581 /* get User defaults */
582 if (user_database
!= NULL
)
583 XrmMergeDatabases (user_database
, &rdb
);
585 /* Get Environment defaults. */
586 db
= get_environ_db ();
588 XrmMergeDatabases (db
, &rdb
);
590 /* Last, merge in any specification from the command line. */
591 if (xrm_string
!= NULL
)
593 db
= XrmGetStringDatabase (xrm_string
);
595 XrmMergeDatabases (db
, &rdb
);
602 /* Retrieve the value of the resource specified by NAME with class CLASS
603 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
606 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class, XrmRepresentation expected_type
, XrmValue
*ret_value
)
609 XrmName namelist
[100];
610 XrmClass classlist
[100];
611 XrmRepresentation type
;
613 XrmStringToNameList(name
, namelist
);
614 XrmStringToClassList(class, classlist
);
616 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
617 && (type
== expected_type
))
619 if (type
== x_rm_string
)
620 ret_value
->addr
= (char *) value
.addr
;
622 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
630 /* Retrieve the string resource specified by NAME with CLASS from
634 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
638 if (inhibit_x_resources
)
639 /* --quick was passed, so this is a no-op. */
642 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
643 return (char *) value
.addr
;
648 /* Stand-alone test facilities. */
653 #define arg_listify(len, list) (list)
654 #define car(list) (*(list))
655 #define cdr(list) (list + 1)
656 #define NIL(list) (! *(list))
657 #define free_arglist(list)
666 for (p
= list
; ! NIL (p
); p
= cdr (p
))
667 if (! strcmp (elt
, car (p
)))
674 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
676 int x1
, x2
, x3
, x4
, x5
;
681 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
690 char *displayname
, *resource_string
, *class, *name
;
694 arg_list
= arg_listify (argc
, argv
);
696 lp
= member ("-d", arg_list
);
698 displayname
= car (cdr (lp
));
700 displayname
= "localhost:0.0";
702 lp
= member ("-xrm", arg_list
);
704 resource_string
= car (cdr (lp
));
706 resource_string
= (char *) 0;
708 lp
= member ("-c", arg_list
);
710 class = car (cdr (lp
));
714 lp
= member ("-n", arg_list
);
716 name
= car (cdr (lp
));
720 free_arglist (arg_list
);
722 if (!(display
= XOpenDisplay (displayname
)))
723 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
725 xdb
= x_load_resources (display
, resource_string
, name
, class);
727 /* In a real program, you'd want to also do this: */
733 char query_class
[90];
738 if (strlen (query_name
))
745 value
= x_get_string_resource (xdb
, query_name
, query_class
);
748 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
750 printf ("\tNo Value.\n\n");
755 printf ("\tExit.\n\n");
757 XCloseDisplay (display
);
761 /* arch-tag: 37e6fbab-ed05-4363-9e76-6c4109ed511f
762 (do not change this comment) */