1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2012 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 /* This may include sys/types.h, and that somehow loses
33 if this is not done before the other system files. */
37 #include <X11/Xatom.h>
39 #include <X11/Xutil.h>
40 #include <X11/Xresource.h>
47 /* For Vdouble_click_time. */
51 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
53 static int file_p (const char *filename
);
56 /* X file search path processing. */
59 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
60 and friends, or zero if none was specified. */
61 static char *x_customization_string
;
64 /* Return the value of the emacs.customization (Emacs.Customization)
65 resource, for later use in search path decoding. If we find no
66 such resource, return zero. */
68 x_get_customization_string (XrmDatabase db
, const char *name
,
71 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
72 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
75 sprintf (full_name
, "%s.%s", name
, "customization");
76 sprintf (full_class
, "%s.%s", class, "Customization");
78 result
= x_get_string_resource (db
, full_name
, full_class
);
82 char *copy
= xmalloc (strlen (result
) + 1);
83 strcpy (copy
, result
);
91 /* Expand all the Xt-style %-escapes in STRING, whose length is given
92 by STRING_LEN. Here are the escapes we're supposed to recognize:
94 %N The value of the application's class name
95 %T The value of the type parameter ("app-defaults" in this
97 %S The value of the suffix parameter ("" in this context)
98 %L The language string associated with the specified display
99 (We use the "LANG" environment variable here, if it's set.)
100 %l The language part of the display's language string
101 (We treat this just like %L. If someone can tell us what
102 we're really supposed to do, dandy.)
103 %t The territory part of the display's language string
104 (This never gets used.)
105 %c The codeset part of the display's language string
106 (This never gets used either.)
107 %C The customization string retrieved from the resource
108 database associated with display.
109 (This is x_customization_string.)
111 Return the expanded file name if it exists and is readable, and
112 refers to %L only when the LANG environment variable is set, or
113 otherwise provided by X.
115 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
116 %-escapes in ESCAPED_SUFFIX are expanded.
118 Return NULL otherwise. */
121 magic_file_p (const char *string
, ptrdiff_t string_len
, const char *class,
122 const char *escaped_suffix
)
124 char *lang
= getenv ("LANG");
126 ptrdiff_t path_size
= 100;
127 char *path
= xmalloc (path_size
);
128 ptrdiff_t path_len
= 0;
130 const char *p
= string
;
132 while (p
< string
+ string_len
)
134 /* The chunk we're about to stick on the end of result. */
135 const char *next
= NULL
;
142 if (p
>= string
+ string_len
)
153 next
= (x_customization_string
154 ? x_customization_string
156 next_len
= strlen (next
);
161 next_len
= strlen (class);
165 next
= "app-defaults";
166 next_len
= strlen (next
);
183 next_len
= strlen (next
);
193 next
= p
, next_len
= 1;
195 /* Do we have room for this component followed by a '\0' ? */
196 if (path_size
- path_len
<= next_len
)
198 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
199 memory_full (SIZE_MAX
);
200 path_size
= (path_len
+ next_len
+ 1) * 2;
201 path
= xrealloc (path
, path_size
);
204 memcpy (path
+ path_len
, next
, next_len
);
205 path_len
+= next_len
;
209 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
210 if (p
>= string
+ string_len
&& escaped_suffix
)
212 string
= escaped_suffix
;
213 string_len
= strlen (string
);
215 escaped_suffix
= NULL
;
219 path
[path_len
] = '\0';
238 if ((ptr
= getenv ("HOME")) == NULL
)
240 if ((ptr
= getenv ("LOGNAME")) != NULL
241 || (ptr
= getenv ("USER")) != NULL
)
244 pw
= getpwuid (getuid ());
251 return xstrdup ("/");
253 copy
= xmalloc (strlen (ptr
) + 2);
262 file_p (const char *filename
)
266 return (access (filename
, 4) == 0 /* exists and is readable */
267 && stat (filename
, &status
) == 0 /* get the status */
268 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
272 /* Find the first element of SEARCH_PATH which exists and is readable,
273 after expanding the %-escapes. Return 0 if we didn't find any, and
274 the path name of the one we found otherwise. */
277 search_magic_path (const char *search_path
, const char *class,
278 const char *escaped_suffix
)
282 for (s
= search_path
; *s
; s
= p
)
284 for (p
= s
; *p
&& *p
!= ':'; p
++)
289 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
);
298 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
);
310 /* Producing databases for individual sources. */
313 get_system_app (const char *class)
315 XrmDatabase db
= NULL
;
319 path
= getenv ("XFILESEARCHPATH");
320 if (! path
) path
= PATH_X_DEFAULTS
;
322 p
= search_magic_path (path
, class, 0);
325 db
= XrmGetFileDatabase (p
);
334 get_fallback (Display
*display
)
341 get_user_app (const char *class)
347 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
348 names, not directories. */
349 if (((path
= getenv ("XUSERFILESEARCHPATH"))
350 && (file
= search_magic_path (path
, class, 0)))
352 /* Check for APPLRESDIR; it is a path of directories. In each,
353 we have to search for LANG/CLASS and then CLASS. */
354 || ((path
= getenv ("XAPPLRESDIR"))
355 && ((file
= search_magic_path (path
, class, "/%L/%N"))
356 || (file
= search_magic_path (path
, class, "/%N"))))
358 /* Check in the home directory. This is a bit of a hack; let's
359 hope one's home directory doesn't contain any %-escapes. */
360 || (free_it
= gethomedir (),
361 ((file
= search_magic_path (free_it
, class, "%L/%N"))
362 || (file
= search_magic_path (free_it
, class, "%N")))))
364 XrmDatabase db
= XrmGetFileDatabase (file
);
376 get_user_db (Display
*display
)
381 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
382 xdefs
= XResourceManagerString (display
);
384 xdefs
= display
->xdefaults
;
388 db
= XrmGetStringDatabase (xdefs
);
394 home
= gethomedir ();
395 xdefault
= xmalloc (strlen (home
) + sizeof ".Xdefaults");
396 strcpy (xdefault
, home
);
397 strcat (xdefault
, ".Xdefaults");
398 db
= XrmGetFileDatabase (xdefault
);
403 #ifdef HAVE_XSCREENRESOURCESTRING
404 /* Get the screen-specific resources too. */
405 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
408 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
417 get_environ_db (void)
423 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
425 static char const xdefaults
[] = ".Xdefaults-";
426 char *home
= gethomedir ();
427 char const *host
= SSDATA (Vsystem_name
);
428 ptrdiff_t pathsize
= (strlen (home
) + sizeof xdefaults
429 + SBYTES (Vsystem_name
));
430 path
= xrealloc (home
, pathsize
);
431 strcat (strcat (path
, xdefaults
), host
);
435 db
= XrmGetFileDatabase (p
);
442 /* External interface. */
444 /* Types of values that we can find in a database */
446 #define XrmStringType "String" /* String representation */
447 static XrmRepresentation x_rm_string
; /* Quark representation */
449 /* Load X resources based on the display and a possible -xrm option. */
452 x_load_resources (Display
*display
, const char *xrm_string
,
453 const char *myname
, const char *myclass
)
455 XrmDatabase user_database
;
460 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
461 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
465 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
468 x_rm_string
= XrmStringToQuark (XrmStringType
);
469 #ifndef USE_X_TOOLKIT
470 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
471 I suspect it's because the toolkit version does this elsewhere. */
474 rdb
= XrmGetStringDatabase ("");
476 /* Add some font defaults. If the font `helv' doesn't exist, widgets
477 will use some other default font. */
480 sprintf (line
, "%s.pane.background: grey75", myclass
);
481 XrmPutLineResource (&rdb
, line
);
482 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
483 XrmPutLineResource (&rdb
, line
);
484 sprintf (line
, "%s*menu*background: grey75", myclass
);
485 XrmPutLineResource (&rdb
, line
);
486 sprintf (line
, "%s*menubar*background: grey75", myclass
);
487 XrmPutLineResource (&rdb
, line
);
488 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
489 XrmPutLineResource (&rdb
, line
);
490 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
491 XrmPutLineResource (&rdb
, line
);
492 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
493 XrmPutLineResource (&rdb
, line
);
494 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
495 XrmPutLineResource (&rdb
, line
);
496 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
497 XrmPutLineResource (&rdb
, line
);
498 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
499 XrmPutLineResource (&rdb
, line
);
500 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
501 XrmPutLineResource (&rdb
, line
);
502 sprintf (line
, "%s*fsb*background: grey75", myclass
);
503 XrmPutLineResource (&rdb
, line
);
504 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
505 XrmPutLineResource (&rdb
, line
);
506 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
507 XrmPutLineResource (&rdb
, line
);
508 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
509 XrmPutLineResource (&rdb
, line
);
510 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
511 XrmPutLineResource (&rdb
, line
);
513 /* Set double click time of list boxes in the file selection
514 dialog from `double-click-time'. */
515 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
517 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
518 myclass
, XFASTINT (Vdouble_click_time
));
519 XrmPutLineResource (&rdb
, line
);
520 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
521 myclass
, XFASTINT (Vdouble_click_time
));
522 XrmPutLineResource (&rdb
, line
);
525 #else /* not USE_MOTIF */
527 sprintf (line
, "Emacs.dialog*.background: grey75");
528 XrmPutLineResource (&rdb
, line
);
529 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
530 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
531 XrmPutLineResource (&rdb
, line
);
532 sprintf (line
, "*XlwMenu*font: %s", helv
);
533 XrmPutLineResource (&rdb
, line
);
535 sprintf (line
, "*XlwMenu*background: grey75");
536 XrmPutLineResource (&rdb
, line
);
537 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
538 XrmPutLineResource (&rdb
, line
);
540 #endif /* not USE_MOTIF */
542 user_database
= get_user_db (display
);
544 /* Figure out what the "customization string" is, so we can use it
546 xfree (x_customization_string
);
547 x_customization_string
548 = x_get_customization_string (user_database
, myname
, myclass
);
550 /* Get application system defaults */
551 db
= get_system_app (myclass
);
553 XrmMergeDatabases (db
, &rdb
);
555 /* Get Fallback resources */
556 db
= get_fallback (display
);
558 XrmMergeDatabases (db
, &rdb
);
560 /* Get application user defaults */
561 db
= get_user_app (myclass
);
563 XrmMergeDatabases (db
, &rdb
);
565 /* get User defaults */
566 if (user_database
!= NULL
)
567 XrmMergeDatabases (user_database
, &rdb
);
569 /* Get Environment defaults. */
570 db
= get_environ_db ();
572 XrmMergeDatabases (db
, &rdb
);
574 /* Last, merge in any specification from the command line. */
575 if (xrm_string
!= NULL
)
577 db
= XrmGetStringDatabase (xrm_string
);
579 XrmMergeDatabases (db
, &rdb
);
586 /* Retrieve the value of the resource specified by NAME with class CLASS
587 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
590 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
591 XrmRepresentation expected_type
, XrmValue
*ret_value
)
594 XrmName namelist
[100];
595 XrmClass classlist
[100];
596 XrmRepresentation type
;
598 XrmStringToNameList (name
, namelist
);
599 XrmStringToClassList (class, classlist
);
601 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
602 && (type
== expected_type
))
604 if (type
== x_rm_string
)
605 ret_value
->addr
= (char *) value
.addr
;
607 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
615 /* Retrieve the string resource specified by NAME with CLASS from
619 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
623 if (inhibit_x_resources
)
624 /* --quick was passed, so this is a no-op. */
627 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
628 return (char *) value
.addr
;
633 /* Stand-alone test facilities. */
638 #define arg_listify(len, list) (list)
639 #define car(list) (*(list))
640 #define cdr(list) (list + 1)
641 #define NIL(list) (! *(list))
642 #define free_arglist(list)
645 member (char *elt
, List list
)
649 for (p
= list
; ! NIL (p
); p
= cdr (p
))
650 if (! strcmp (elt
, car (p
)))
657 fatal (char *msg
, char *prog
)
662 (void) fprintf (stderr
, msg
, prog
);
667 main (int argc
, char **argv
)
670 char *displayname
, *resource_string
, *class, *name
;
674 arg_list
= arg_listify (argc
, argv
);
676 lp
= member ("-d", arg_list
);
678 displayname
= car (cdr (lp
));
680 displayname
= "localhost:0.0";
682 lp
= member ("-xrm", arg_list
);
684 resource_string
= car (cdr (lp
));
686 resource_string
= (char *) 0;
688 lp
= member ("-c", arg_list
);
690 class = car (cdr (lp
));
694 lp
= member ("-n", arg_list
);
696 name
= car (cdr (lp
));
700 free_arglist (arg_list
);
702 if (!(display
= XOpenDisplay (displayname
)))
703 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
705 xdb
= x_load_resources (display
, resource_string
, name
, class);
707 /* In a real program, you'd want to also do this: */
713 char query_class
[90];
718 if (strlen (query_name
))
725 value
= x_get_string_resource (xdb
, query_name
, query_class
);
728 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
730 printf ("\tNo Value.\n\n");
735 printf ("\tExit.\n\n");
737 XCloseDisplay (display
);