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/>. */
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
38 #include <X11/Xatom.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
48 /* For Vdouble_click_time. */
52 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
54 static int file_p (const char *filename
);
57 /* X file search path processing. */
60 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
61 and friends, or zero if none was specified. */
62 static char *x_customization_string
;
65 /* Return the value of the emacs.customization (Emacs.Customization)
66 resource, for later use in search path decoding. If we find no
67 such resource, return zero. */
69 x_get_customization_string (XrmDatabase db
, const char *name
,
72 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
73 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
76 sprintf (full_name
, "%s.%s", name
, "customization");
77 sprintf (full_class
, "%s.%s", class, "Customization");
79 result
= x_get_string_resource (db
, full_name
, full_class
);
83 char *copy
= xmalloc (strlen (result
) + 1);
84 strcpy (copy
, result
);
92 /* Expand all the Xt-style %-escapes in STRING, whose length is given
93 by STRING_LEN. Here are the escapes we're supposed to recognize:
95 %N The value of the application's class name
96 %T The value of the type parameter ("app-defaults" in this
98 %S The value of the suffix parameter ("" in this context)
99 %L The language string associated with the specified display
100 (We use the "LANG" environment variable here, if it's set.)
101 %l The language part of the display's language string
102 (We treat this just like %L. If someone can tell us what
103 we're really supposed to do, dandy.)
104 %t The territory part of the display's language string
105 (This never gets used.)
106 %c The codeset part of the display's language string
107 (This never gets used either.)
108 %C The customization string retrieved from the resource
109 database associated with display.
110 (This is x_customization_string.)
112 Return the expanded file name if it exists and is readable, and
113 refers to %L only when the LANG environment variable is set, or
114 otherwise provided by X.
116 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
117 %-escapes in ESCAPED_SUFFIX are expanded.
119 Return NULL otherwise. */
122 magic_file_p (const char *string
, ptrdiff_t string_len
, const char *class,
123 const char *escaped_suffix
)
125 char *lang
= getenv ("LANG");
127 ptrdiff_t path_size
= 100;
128 char *path
= xmalloc (path_size
);
129 ptrdiff_t path_len
= 0;
131 const char *p
= string
;
133 while (p
< string
+ string_len
)
135 /* The chunk we're about to stick on the end of result. */
136 const char *next
= NULL
;
143 if (p
>= string
+ string_len
)
154 next
= (x_customization_string
155 ? x_customization_string
157 next_len
= strlen (next
);
162 next_len
= strlen (class);
166 next
= "app-defaults";
167 next_len
= strlen (next
);
184 next_len
= strlen (next
);
194 next
= p
, next_len
= 1;
196 /* Do we have room for this component followed by a '\0' ? */
197 if (path_size
- path_len
<= next_len
)
199 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
200 memory_full (SIZE_MAX
);
201 path_size
= (path_len
+ next_len
+ 1) * 2;
202 path
= xrealloc (path
, path_size
);
205 memcpy (path
+ path_len
, next
, next_len
);
206 path_len
+= next_len
;
210 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
211 if (p
>= string
+ string_len
&& escaped_suffix
)
213 string
= escaped_suffix
;
214 string_len
= strlen (string
);
216 escaped_suffix
= NULL
;
220 path
[path_len
] = '\0';
239 if ((ptr
= getenv ("HOME")) == NULL
)
241 if ((ptr
= getenv ("LOGNAME")) != NULL
242 || (ptr
= getenv ("USER")) != NULL
)
245 pw
= getpwuid (getuid ());
252 return xstrdup ("/");
254 copy
= xmalloc (strlen (ptr
) + 2);
263 file_p (const char *filename
)
265 return (faccessat (AT_FDCWD
, filename
, R_OK
, AT_EACCESS
) == 0
266 && ! file_directory_p (filename
));
270 /* Find the first element of SEARCH_PATH which exists and is readable,
271 after expanding the %-escapes. Return 0 if we didn't find any, and
272 the path name of the one we found otherwise. */
275 search_magic_path (const char *search_path
, const char *class,
276 const char *escaped_suffix
)
280 for (s
= search_path
; *s
; s
= p
)
282 for (p
= s
; *p
&& *p
!= ':'; p
++)
287 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
);
296 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
);
308 /* Producing databases for individual sources. */
311 get_system_app (const char *class)
313 XrmDatabase db
= NULL
;
317 path
= getenv ("XFILESEARCHPATH");
318 if (! path
) path
= PATH_X_DEFAULTS
;
320 p
= search_magic_path (path
, class, 0);
323 db
= XrmGetFileDatabase (p
);
332 get_fallback (Display
*display
)
339 get_user_app (const char *class)
345 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
346 names, not directories. */
347 if (((path
= getenv ("XUSERFILESEARCHPATH"))
348 && (file
= search_magic_path (path
, class, 0)))
350 /* Check for APPLRESDIR; it is a path of directories. In each,
351 we have to search for LANG/CLASS and then CLASS. */
352 || ((path
= getenv ("XAPPLRESDIR"))
353 && ((file
= search_magic_path (path
, class, "/%L/%N"))
354 || (file
= search_magic_path (path
, class, "/%N"))))
356 /* Check in the home directory. This is a bit of a hack; let's
357 hope one's home directory doesn't contain any %-escapes. */
358 || (free_it
= gethomedir (),
359 ((file
= search_magic_path (free_it
, class, "%L/%N"))
360 || (file
= search_magic_path (free_it
, class, "%N")))))
362 XrmDatabase db
= XrmGetFileDatabase (file
);
374 get_user_db (Display
*display
)
379 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
380 xdefs
= XResourceManagerString (display
);
382 xdefs
= display
->xdefaults
;
386 db
= XrmGetStringDatabase (xdefs
);
392 home
= gethomedir ();
393 xdefault
= xmalloc (strlen (home
) + sizeof ".Xdefaults");
394 strcpy (xdefault
, home
);
395 strcat (xdefault
, ".Xdefaults");
396 db
= XrmGetFileDatabase (xdefault
);
401 #ifdef HAVE_XSCREENRESOURCESTRING
402 /* Get the screen-specific resources too. */
403 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
406 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
415 get_environ_db (void)
421 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
423 static char const xdefaults
[] = ".Xdefaults-";
424 char *home
= gethomedir ();
425 char const *host
= SSDATA (Vsystem_name
);
426 ptrdiff_t pathsize
= (strlen (home
) + sizeof xdefaults
427 + SBYTES (Vsystem_name
));
428 path
= xrealloc (home
, pathsize
);
429 strcat (strcat (path
, xdefaults
), host
);
433 db
= XrmGetFileDatabase (p
);
440 /* External interface. */
442 /* Types of values that we can find in a database */
444 #define XrmStringType "String" /* String representation */
445 static XrmRepresentation x_rm_string
; /* Quark representation */
447 /* Load X resources based on the display and a possible -xrm option. */
450 x_load_resources (Display
*display
, const char *xrm_string
,
451 const char *myname
, const char *myclass
)
453 XrmDatabase user_database
;
458 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
459 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
463 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
466 x_rm_string
= XrmStringToQuark (XrmStringType
);
467 #ifndef USE_X_TOOLKIT
468 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
469 I suspect it's because the toolkit version does this elsewhere. */
472 rdb
= XrmGetStringDatabase ("");
474 /* Add some font defaults. If the font `helv' doesn't exist, widgets
475 will use some other default font. */
478 sprintf (line
, "%s.pane.background: grey75", myclass
);
479 XrmPutLineResource (&rdb
, line
);
480 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
481 XrmPutLineResource (&rdb
, line
);
482 sprintf (line
, "%s*menu*background: grey75", myclass
);
483 XrmPutLineResource (&rdb
, line
);
484 sprintf (line
, "%s*menubar*background: grey75", myclass
);
485 XrmPutLineResource (&rdb
, line
);
486 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
487 XrmPutLineResource (&rdb
, line
);
488 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
489 XrmPutLineResource (&rdb
, line
);
490 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
491 XrmPutLineResource (&rdb
, line
);
492 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
493 XrmPutLineResource (&rdb
, line
);
494 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
495 XrmPutLineResource (&rdb
, line
);
496 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
497 XrmPutLineResource (&rdb
, line
);
498 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
499 XrmPutLineResource (&rdb
, line
);
500 sprintf (line
, "%s*fsb*background: grey75", myclass
);
501 XrmPutLineResource (&rdb
, line
);
502 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
503 XrmPutLineResource (&rdb
, line
);
504 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
505 XrmPutLineResource (&rdb
, line
);
506 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
507 XrmPutLineResource (&rdb
, line
);
508 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
509 XrmPutLineResource (&rdb
, line
);
511 /* Set double click time of list boxes in the file selection
512 dialog from `double-click-time'. */
513 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
515 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
516 myclass
, XFASTINT (Vdouble_click_time
));
517 XrmPutLineResource (&rdb
, line
);
518 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
519 myclass
, XFASTINT (Vdouble_click_time
));
520 XrmPutLineResource (&rdb
, line
);
523 #else /* not USE_MOTIF */
525 sprintf (line
, "Emacs.dialog*.background: grey75");
526 XrmPutLineResource (&rdb
, line
);
527 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
528 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
529 XrmPutLineResource (&rdb
, line
);
530 sprintf (line
, "*XlwMenu*font: %s", helv
);
531 XrmPutLineResource (&rdb
, line
);
533 sprintf (line
, "*XlwMenu*background: grey75");
534 XrmPutLineResource (&rdb
, line
);
535 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
536 XrmPutLineResource (&rdb
, line
);
538 #endif /* not USE_MOTIF */
540 user_database
= get_user_db (display
);
542 /* Figure out what the "customization string" is, so we can use it
544 xfree (x_customization_string
);
545 x_customization_string
546 = x_get_customization_string (user_database
, myname
, myclass
);
548 /* Get application system defaults */
549 db
= get_system_app (myclass
);
551 XrmMergeDatabases (db
, &rdb
);
553 /* Get Fallback resources */
554 db
= get_fallback (display
);
556 XrmMergeDatabases (db
, &rdb
);
558 /* Get application user defaults */
559 db
= get_user_app (myclass
);
561 XrmMergeDatabases (db
, &rdb
);
563 /* get User defaults */
564 if (user_database
!= NULL
)
565 XrmMergeDatabases (user_database
, &rdb
);
567 /* Get Environment defaults. */
568 db
= get_environ_db ();
570 XrmMergeDatabases (db
, &rdb
);
572 /* Last, merge in any specification from the command line. */
573 if (xrm_string
!= NULL
)
575 db
= XrmGetStringDatabase (xrm_string
);
577 XrmMergeDatabases (db
, &rdb
);
584 /* Retrieve the value of the resource specified by NAME with class CLASS
585 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
588 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
589 XrmRepresentation expected_type
, XrmValue
*ret_value
)
592 XrmName namelist
[100];
593 XrmClass classlist
[100];
594 XrmRepresentation type
;
596 XrmStringToNameList (name
, namelist
);
597 XrmStringToClassList (class, classlist
);
599 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
600 && (type
== expected_type
))
602 if (type
== x_rm_string
)
603 ret_value
->addr
= (char *) value
.addr
;
605 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
613 /* Retrieve the string resource specified by NAME with CLASS from
617 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
621 if (inhibit_x_resources
)
622 /* --quick was passed, so this is a no-op. */
625 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
626 return (char *) value
.addr
;
631 /* Stand-alone test facilities. */
636 #define arg_listify(len, list) (list)
637 #define car(list) (*(list))
638 #define cdr(list) (list + 1)
639 #define NIL(list) (! *(list))
640 #define free_arglist(list)
643 member (char *elt
, List list
)
647 for (p
= list
; ! NIL (p
); p
= cdr (p
))
648 if (! strcmp (elt
, car (p
)))
655 fatal (char *msg
, char *prog
)
660 (void) fprintf (stderr
, msg
, prog
);
665 main (int argc
, char **argv
)
668 char *displayname
, *resource_string
, *class, *name
;
672 arg_list
= arg_listify (argc
, argv
);
674 lp
= member ("-d", arg_list
);
676 displayname
= car (cdr (lp
));
678 displayname
= "localhost:0.0";
680 lp
= member ("-xrm", arg_list
);
682 resource_string
= car (cdr (lp
));
684 resource_string
= (char *) 0;
686 lp
= member ("-c", arg_list
);
688 class = car (cdr (lp
));
692 lp
= member ("-n", arg_list
);
694 name
= car (cdr (lp
));
698 free_arglist (arg_list
);
700 if (!(display
= XOpenDisplay (displayname
)))
701 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
703 xdb
= x_load_resources (display
, resource_string
, name
, class);
705 /* In a real program, you'd want to also do this: */
711 char query_class
[90];
716 if (strlen (query_name
))
723 value
= x_get_string_resource (xdb
, query_name
, query_class
);
726 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
728 printf ("\tNo Value.\n\n");
733 printf ("\tExit.\n\n");
735 XCloseDisplay (display
);