1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2013 Free Software Foundation,
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/>. */
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
)
267 return (access (filename
, 4) == 0 /* exists and is readable */
268 && stat (filename
, &status
) == 0 /* get the status */
269 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
273 /* Find the first element of SEARCH_PATH which exists and is readable,
274 after expanding the %-escapes. Return 0 if we didn't find any, and
275 the path name of the one we found otherwise. */
278 search_magic_path (const char *search_path
, const char *class,
279 const char *escaped_suffix
)
283 for (s
= search_path
; *s
; s
= p
)
285 for (p
= s
; *p
&& *p
!= ':'; p
++)
290 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
);
299 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
);
311 /* Producing databases for individual sources. */
314 get_system_app (const char *class)
316 XrmDatabase db
= NULL
;
320 path
= getenv ("XFILESEARCHPATH");
321 if (! path
) path
= PATH_X_DEFAULTS
;
323 p
= search_magic_path (path
, class, 0);
326 db
= XrmGetFileDatabase (p
);
335 get_fallback (Display
*display
)
342 get_user_app (const char *class)
348 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
349 names, not directories. */
350 if (((path
= getenv ("XUSERFILESEARCHPATH"))
351 && (file
= search_magic_path (path
, class, 0)))
353 /* Check for APPLRESDIR; it is a path of directories. In each,
354 we have to search for LANG/CLASS and then CLASS. */
355 || ((path
= getenv ("XAPPLRESDIR"))
356 && ((file
= search_magic_path (path
, class, "/%L/%N"))
357 || (file
= search_magic_path (path
, class, "/%N"))))
359 /* Check in the home directory. This is a bit of a hack; let's
360 hope one's home directory doesn't contain any %-escapes. */
361 || (free_it
= gethomedir (),
362 ((file
= search_magic_path (free_it
, class, "%L/%N"))
363 || (file
= search_magic_path (free_it
, class, "%N")))))
365 XrmDatabase db
= XrmGetFileDatabase (file
);
377 get_user_db (Display
*display
)
382 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
383 xdefs
= XResourceManagerString (display
);
385 xdefs
= display
->xdefaults
;
389 db
= XrmGetStringDatabase (xdefs
);
395 home
= gethomedir ();
396 xdefault
= xmalloc (strlen (home
) + sizeof ".Xdefaults");
397 strcpy (xdefault
, home
);
398 strcat (xdefault
, ".Xdefaults");
399 db
= XrmGetFileDatabase (xdefault
);
404 #ifdef HAVE_XSCREENRESOURCESTRING
405 /* Get the screen-specific resources too. */
406 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
409 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
418 get_environ_db (void)
424 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
426 static char const xdefaults
[] = ".Xdefaults-";
427 char *home
= gethomedir ();
428 char const *host
= SSDATA (Vsystem_name
);
429 ptrdiff_t pathsize
= (strlen (home
) + sizeof xdefaults
430 + SBYTES (Vsystem_name
));
431 path
= xrealloc (home
, pathsize
);
432 strcat (strcat (path
, xdefaults
), host
);
436 db
= XrmGetFileDatabase (p
);
443 /* External interface. */
445 /* Types of values that we can find in a database */
447 #define XrmStringType "String" /* String representation */
448 static XrmRepresentation x_rm_string
; /* Quark representation */
450 /* Load X resources based on the display and a possible -xrm option. */
453 x_load_resources (Display
*display
, const char *xrm_string
,
454 const char *myname
, const char *myclass
)
456 XrmDatabase user_database
;
461 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
462 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
466 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
469 x_rm_string
= XrmStringToQuark (XrmStringType
);
470 #ifndef USE_X_TOOLKIT
471 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
472 I suspect it's because the toolkit version does this elsewhere. */
475 rdb
= XrmGetStringDatabase ("");
477 /* Add some font defaults. If the font `helv' doesn't exist, widgets
478 will use some other default font. */
481 sprintf (line
, "%s.pane.background: grey75", myclass
);
482 XrmPutLineResource (&rdb
, line
);
483 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
484 XrmPutLineResource (&rdb
, line
);
485 sprintf (line
, "%s*menu*background: grey75", myclass
);
486 XrmPutLineResource (&rdb
, line
);
487 sprintf (line
, "%s*menubar*background: grey75", myclass
);
488 XrmPutLineResource (&rdb
, line
);
489 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
490 XrmPutLineResource (&rdb
, line
);
491 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
492 XrmPutLineResource (&rdb
, line
);
493 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
494 XrmPutLineResource (&rdb
, line
);
495 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
496 XrmPutLineResource (&rdb
, line
);
497 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
498 XrmPutLineResource (&rdb
, line
);
499 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
500 XrmPutLineResource (&rdb
, line
);
501 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
502 XrmPutLineResource (&rdb
, line
);
503 sprintf (line
, "%s*fsb*background: grey75", myclass
);
504 XrmPutLineResource (&rdb
, line
);
505 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
506 XrmPutLineResource (&rdb
, line
);
507 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
508 XrmPutLineResource (&rdb
, line
);
509 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
510 XrmPutLineResource (&rdb
, line
);
511 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
512 XrmPutLineResource (&rdb
, line
);
514 /* Set double click time of list boxes in the file selection
515 dialog from `double-click-time'. */
516 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
518 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
519 myclass
, XFASTINT (Vdouble_click_time
));
520 XrmPutLineResource (&rdb
, line
);
521 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
522 myclass
, XFASTINT (Vdouble_click_time
));
523 XrmPutLineResource (&rdb
, line
);
526 #else /* not USE_MOTIF */
528 sprintf (line
, "Emacs.dialog*.background: grey75");
529 XrmPutLineResource (&rdb
, line
);
530 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
531 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
532 XrmPutLineResource (&rdb
, line
);
533 sprintf (line
, "*XlwMenu*font: %s", helv
);
534 XrmPutLineResource (&rdb
, line
);
536 sprintf (line
, "*XlwMenu*background: grey75");
537 XrmPutLineResource (&rdb
, line
);
538 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
539 XrmPutLineResource (&rdb
, line
);
541 #endif /* not USE_MOTIF */
543 user_database
= get_user_db (display
);
545 /* Figure out what the "customization string" is, so we can use it
547 xfree (x_customization_string
);
548 x_customization_string
549 = x_get_customization_string (user_database
, myname
, myclass
);
551 /* Get application system defaults */
552 db
= get_system_app (myclass
);
554 XrmMergeDatabases (db
, &rdb
);
556 /* Get Fallback resources */
557 db
= get_fallback (display
);
559 XrmMergeDatabases (db
, &rdb
);
561 /* Get application user defaults */
562 db
= get_user_app (myclass
);
564 XrmMergeDatabases (db
, &rdb
);
566 /* get User defaults */
567 if (user_database
!= NULL
)
568 XrmMergeDatabases (user_database
, &rdb
);
570 /* Get Environment defaults. */
571 db
= get_environ_db ();
573 XrmMergeDatabases (db
, &rdb
);
575 /* Last, merge in any specification from the command line. */
576 if (xrm_string
!= NULL
)
578 db
= XrmGetStringDatabase (xrm_string
);
580 XrmMergeDatabases (db
, &rdb
);
587 /* Retrieve the value of the resource specified by NAME with class CLASS
588 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
591 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
592 XrmRepresentation expected_type
, XrmValue
*ret_value
)
595 XrmName namelist
[100];
596 XrmClass classlist
[100];
597 XrmRepresentation type
;
599 XrmStringToNameList (name
, namelist
);
600 XrmStringToClassList (class, classlist
);
602 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
603 && (type
== expected_type
))
605 if (type
== x_rm_string
)
606 ret_value
->addr
= (char *) value
.addr
;
608 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
616 /* Retrieve the string resource specified by NAME with CLASS from
620 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
624 if (inhibit_x_resources
)
625 /* --quick was passed, so this is a no-op. */
628 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
629 return (char *) value
.addr
;
634 /* Stand-alone test facilities. */
639 #define arg_listify(len, list) (list)
640 #define car(list) (*(list))
641 #define cdr(list) (list + 1)
642 #define NIL(list) (! *(list))
643 #define free_arglist(list)
646 member (char *elt
, List list
)
650 for (p
= list
; ! NIL (p
); p
= cdr (p
))
651 if (! strcmp (elt
, car (p
)))
658 fatal (char *msg
, char *prog
)
663 (void) fprintf (stderr
, msg
, prog
);
668 main (int argc
, char **argv
)
671 char *displayname
, *resource_string
, *class, *name
;
675 arg_list
= arg_listify (argc
, argv
);
677 lp
= member ("-d", arg_list
);
679 displayname
= car (cdr (lp
));
681 displayname
= "localhost:0.0";
683 lp
= member ("-xrm", arg_list
);
685 resource_string
= car (cdr (lp
));
687 resource_string
= (char *) 0;
689 lp
= member ("-c", arg_list
);
691 class = car (cdr (lp
));
695 lp
= member ("-n", arg_list
);
697 name
= car (cdr (lp
));
701 free_arglist (arg_list
);
703 if (!(display
= XOpenDisplay (displayname
)))
704 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
706 xdb
= x_load_resources (display
, resource_string
, name
, class);
708 /* In a real program, you'd want to also do this: */
714 char query_class
[90];
719 if (strlen (query_name
))
726 value
= x_get_string_resource (xdb
, query_name
, query_class
);
729 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
731 printf ("\tNo Value.\n\n");
736 printf ("\tExit.\n\n");
738 XCloseDisplay (display
);