1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2011 Free Software Foundation, Inc.
4 Author: Joseph Arceneaux
7 This file is part of GNU Emacs.
9 GNU Emacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
32 #include <X11/Xatom.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
41 #if !defined(S_ISDIR) && defined(S_IFDIR)
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
48 /* For Vdouble_click_time. */
52 extern char *getenv (const char *);
54 extern struct passwd
*getpwuid (uid_t
);
55 extern struct passwd
*getpwnam (const char *);
57 extern const char *get_system_name (void);
59 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
61 static int file_p (const char *filename
);
64 /* X file search path processing. */
67 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
68 and friends, or zero if none was specified. */
69 char *x_customization_string
;
72 /* Return the value of the emacs.customization (Emacs.Customization)
73 resource, for later use in search path decoding. If we find no
74 such resource, return zero. */
76 x_get_customization_string (XrmDatabase db
, const char *name
,
80 = (char *) alloca (strlen (name
) + sizeof ("customization") + 3);
82 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
85 sprintf (full_name
, "%s.%s", name
, "customization");
86 sprintf (full_class
, "%s.%s", class, "Customization");
88 result
= x_get_string_resource (db
, full_name
, full_class
);
92 char *copy
= (char *) xmalloc (strlen (result
) + 1);
93 strcpy (copy
, result
);
101 /* Expand all the Xt-style %-escapes in STRING, whose length is given
102 by STRING_LEN. Here are the escapes we're supposed to recognize:
104 %N The value of the application's class name
105 %T The value of the type parameter ("app-defaults" in this
107 %S The value of the suffix parameter ("" in this context)
108 %L The language string associated with the specified display
109 (We use the "LANG" environment variable here, if it's set.)
110 %l The language part of the display's language string
111 (We treat this just like %L. If someone can tell us what
112 we're really supposed to do, dandy.)
113 %t The territory part of the display's language string
114 (This never gets used.)
115 %c The codeset part of the display's language string
116 (This never gets used either.)
117 %C The customization string retrieved from the resource
118 database associated with display.
119 (This is x_customization_string.)
121 Return the expanded file name if it exists and is readable, and
122 refers to %L only when the LANG environment variable is set, or
123 otherwise provided by X.
125 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
126 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
129 Return NULL otherwise. */
132 magic_file_p (const char *string
, EMACS_INT string_len
, const char *class, const char *escaped_suffix
, const char *suffix
)
134 char *lang
= getenv ("LANG");
137 char *path
= (char *) xmalloc (path_size
);
140 const char *p
= string
;
142 while (p
< string
+ string_len
)
144 /* The chunk we're about to stick on the end of result. */
145 const char *next
= NULL
;
152 if (p
>= string
+ string_len
)
163 next
= (x_customization_string
164 ? x_customization_string
166 next_len
= strlen (next
);
171 next_len
= strlen (class);
175 next
= "app-defaults";
176 next_len
= strlen (next
);
193 next_len
= strlen (next
);
203 next
= p
, next_len
= 1;
205 /* Do we have room for this component followed by a '\0' ? */
206 if (path_len
+ next_len
+ 1 > path_size
)
208 path_size
= (path_len
+ next_len
+ 1) * 2;
209 path
= (char *) xrealloc (path
, path_size
);
212 memcpy (path
+ path_len
, next
, next_len
);
213 path_len
+= next_len
;
217 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
218 if (p
>= string
+ string_len
&& escaped_suffix
)
220 string
= escaped_suffix
;
221 string_len
= strlen (string
);
223 escaped_suffix
= NULL
;
227 /* Perhaps we should add the SUFFIX now. */
230 int suffix_len
= strlen (suffix
);
232 if (path_len
+ suffix_len
+ 1 > path_size
)
234 path_size
= (path_len
+ suffix_len
+ 1);
235 path
= (char *) xrealloc (path
, path_size
);
238 memcpy (path
+ path_len
, suffix
, suffix_len
);
239 path_len
+= suffix_len
;
242 path
[path_len
] = '\0';
261 if ((ptr
= getenv ("HOME")) == NULL
)
263 if ((ptr
= getenv ("LOGNAME")) != NULL
264 || (ptr
= getenv ("USER")) != NULL
)
267 pw
= getpwuid (getuid ());
274 return xstrdup ("/");
276 copy
= (char *) xmalloc (strlen (ptr
) + 2);
285 file_p (const char *filename
)
289 return (access (filename
, 4) == 0 /* exists and is readable */
290 && stat (filename
, &status
) == 0 /* get the status */
291 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
295 /* Find the first element of SEARCH_PATH which exists and is readable,
296 after expanding the %-escapes. Return 0 if we didn't find any, and
297 the path name of the one we found otherwise. */
300 search_magic_path (const char *search_path
, const char *class, const char *escaped_suffix
, const char *suffix
)
304 for (s
= search_path
; *s
; s
= p
)
306 for (p
= s
; *p
&& *p
!= ':'; p
++)
311 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
,
321 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
, suffix
);
333 /* Producing databases for individual sources. */
336 get_system_app (const char *class)
338 XrmDatabase db
= NULL
;
342 path
= getenv ("XFILESEARCHPATH");
343 if (! path
) path
= PATH_X_DEFAULTS
;
345 p
= search_magic_path (path
, class, 0, 0);
348 db
= XrmGetFileDatabase (p
);
357 get_fallback (Display
*display
)
364 get_user_app (const char *class)
370 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
371 names, not directories. */
372 if (((path
= getenv ("XUSERFILESEARCHPATH"))
373 && (file
= search_magic_path (path
, class, 0, 0)))
375 /* Check for APPLRESDIR; it is a path of directories. In each,
376 we have to search for LANG/CLASS and then CLASS. */
377 || ((path
= getenv ("XAPPLRESDIR"))
378 && ((file
= search_magic_path (path
, class, "/%L/%N", 0))
379 || (file
= search_magic_path (path
, class, "/%N", 0))))
381 /* Check in the home directory. This is a bit of a hack; let's
382 hope one's home directory doesn't contain any %-escapes. */
383 || (free_it
= gethomedir (),
384 ((file
= search_magic_path (free_it
, class, "%L/%N", 0))
385 || (file
= search_magic_path (free_it
, class, "%N", 0)))))
387 XrmDatabase db
= XrmGetFileDatabase (file
);
399 get_user_db (Display
*display
)
404 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
405 xdefs
= XResourceManagerString (display
);
407 xdefs
= display
->xdefaults
;
411 db
= XrmGetStringDatabase (xdefs
);
417 home
= gethomedir ();
418 xdefault
= (char *) xmalloc (strlen (home
) + sizeof (".Xdefaults"));
419 strcpy (xdefault
, home
);
420 strcat (xdefault
, ".Xdefaults");
421 db
= XrmGetFileDatabase (xdefault
);
426 #ifdef HAVE_XSCREENRESOURCESTRING
427 /* Get the screen-specific resources too. */
428 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
431 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
440 get_environ_db (void)
444 char *path
= 0, *home
= 0;
447 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
449 home
= gethomedir ();
450 host
= get_system_name ();
451 path
= (char *) xmalloc (strlen (home
)
452 + sizeof (".Xdefaults-")
454 sprintf (path
, "%s%s%s", home
, ".Xdefaults-", host
);
458 db
= XrmGetFileDatabase (p
);
466 /* External interface. */
468 /* Types of values that we can find in a database */
470 #define XrmStringType "String" /* String representation */
471 XrmRepresentation x_rm_string
; /* Quark representation */
473 /* Load X resources based on the display and a possible -xrm option. */
476 x_load_resources (Display
*display
, const char *xrm_string
,
477 const char *myname
, const char *myclass
)
479 XrmDatabase user_database
;
484 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
487 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
490 x_rm_string
= XrmStringToQuark (XrmStringType
);
491 #ifndef USE_X_TOOLKIT
492 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
493 I suspect it's because the toolkit version does this elsewhere. */
496 rdb
= XrmGetStringDatabase ("");
498 /* Add some font defaults. If the font `helv' doesn't exist, widgets
499 will use some other default font. */
502 sprintf (line
, "%s.pane.background: grey75", myclass
);
503 XrmPutLineResource (&rdb
, line
);
504 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
505 XrmPutLineResource (&rdb
, line
);
506 sprintf (line
, "%s*menu*background: grey75", myclass
);
507 XrmPutLineResource (&rdb
, line
);
508 sprintf (line
, "%s*menubar*background: grey75", myclass
);
509 XrmPutLineResource (&rdb
, line
);
510 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
511 XrmPutLineResource (&rdb
, line
);
512 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
513 XrmPutLineResource (&rdb
, line
);
514 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
515 XrmPutLineResource (&rdb
, line
);
516 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
517 XrmPutLineResource (&rdb
, line
);
518 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
519 XrmPutLineResource (&rdb
, line
);
520 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
521 XrmPutLineResource (&rdb
, line
);
522 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
523 XrmPutLineResource (&rdb
, line
);
524 sprintf (line
, "%s*fsb*background: grey75", myclass
);
525 XrmPutLineResource (&rdb
, line
);
526 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
527 XrmPutLineResource (&rdb
, line
);
528 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
529 XrmPutLineResource (&rdb
, line
);
530 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
531 XrmPutLineResource (&rdb
, line
);
532 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
533 XrmPutLineResource (&rdb
, line
);
535 /* Set double click time of list boxes in the file selection
536 dialog from `double-click-time'. */
537 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
539 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %d",
540 myclass
, XFASTINT (Vdouble_click_time
));
541 XrmPutLineResource (&rdb
, line
);
542 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %d",
543 myclass
, XFASTINT (Vdouble_click_time
));
544 XrmPutLineResource (&rdb
, line
);
547 #else /* not USE_MOTIF */
549 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
550 XrmPutLineResource (&rdb
, line
);
551 sprintf (line
, "Emacs.dialog*.background: grey75");
552 XrmPutLineResource (&rdb
, line
);
553 sprintf (line
, "*XlwMenu*font: %s", helv
);
554 XrmPutLineResource (&rdb
, line
);
555 sprintf (line
, "*XlwMenu*background: grey75");
556 XrmPutLineResource (&rdb
, line
);
557 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
558 XrmPutLineResource (&rdb
, line
);
560 #endif /* not USE_MOTIF */
562 user_database
= get_user_db (display
);
564 /* Figure out what the "customization string" is, so we can use it
566 xfree (x_customization_string
);
567 x_customization_string
568 = x_get_customization_string (user_database
, myname
, myclass
);
570 /* Get application system defaults */
571 db
= get_system_app (myclass
);
573 XrmMergeDatabases (db
, &rdb
);
575 /* Get Fallback resources */
576 db
= get_fallback (display
);
578 XrmMergeDatabases (db
, &rdb
);
580 /* Get application user defaults */
581 db
= get_user_app (myclass
);
583 XrmMergeDatabases (db
, &rdb
);
585 /* get User defaults */
586 if (user_database
!= NULL
)
587 XrmMergeDatabases (user_database
, &rdb
);
589 /* Get Environment defaults. */
590 db
= get_environ_db ();
592 XrmMergeDatabases (db
, &rdb
);
594 /* Last, merge in any specification from the command line. */
595 if (xrm_string
!= NULL
)
597 db
= XrmGetStringDatabase (xrm_string
);
599 XrmMergeDatabases (db
, &rdb
);
606 /* Retrieve the value of the resource specified by NAME with class CLASS
607 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
610 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
611 XrmRepresentation expected_type
, XrmValue
*ret_value
)
614 XrmName namelist
[100];
615 XrmClass classlist
[100];
616 XrmRepresentation type
;
618 XrmStringToNameList(name
, namelist
);
619 XrmStringToClassList(class, classlist
);
621 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
622 && (type
== expected_type
))
624 if (type
== x_rm_string
)
625 ret_value
->addr
= (char *) value
.addr
;
627 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
635 /* Retrieve the string resource specified by NAME with CLASS from
639 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
643 if (inhibit_x_resources
)
644 /* --quick was passed, so this is a no-op. */
647 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
648 return (char *) value
.addr
;
653 /* Stand-alone test facilities. */
658 #define arg_listify(len, list) (list)
659 #define car(list) (*(list))
660 #define cdr(list) (list + 1)
661 #define NIL(list) (! *(list))
662 #define free_arglist(list)
671 for (p
= list
; ! NIL (p
); p
= cdr (p
))
672 if (! strcmp (elt
, car (p
)))
679 fatal (msg
, prog
, x1
, x2
, x3
, x4
, x5
)
681 int x1
, x2
, x3
, x4
, x5
;
686 (void) fprintf (stderr
, msg
, prog
, x1
, x2
, x3
, x4
, x5
);
695 char *displayname
, *resource_string
, *class, *name
;
699 arg_list
= arg_listify (argc
, argv
);
701 lp
= member ("-d", arg_list
);
703 displayname
= car (cdr (lp
));
705 displayname
= "localhost:0.0";
707 lp
= member ("-xrm", arg_list
);
709 resource_string
= car (cdr (lp
));
711 resource_string
= (char *) 0;
713 lp
= member ("-c", arg_list
);
715 class = car (cdr (lp
));
719 lp
= member ("-n", arg_list
);
721 name
= car (cdr (lp
));
725 free_arglist (arg_list
);
727 if (!(display
= XOpenDisplay (displayname
)))
728 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
730 xdb
= x_load_resources (display
, resource_string
, name
, class);
732 /* In a real program, you'd want to also do this: */
738 char query_class
[90];
743 if (strlen (query_name
))
750 value
= x_get_string_resource (xdb
, query_name
, query_class
);
753 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
755 printf ("\tNo Value.\n\n");
760 printf ("\tExit.\n\n");
762 XCloseDisplay (display
);