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 extern char *getenv (const char *);
54 extern struct passwd
*getpwuid (uid_t
);
55 extern struct passwd
*getpwnam (const char *);
57 char *x_get_string_resource (XrmDatabase rdb
, const char *name
,
59 static int file_p (const char *filename
);
62 /* X file search path processing. */
65 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
66 and friends, or zero if none was specified. */
67 static char *x_customization_string
;
70 /* Return the value of the emacs.customization (Emacs.Customization)
71 resource, for later use in search path decoding. If we find no
72 such resource, return zero. */
74 x_get_customization_string (XrmDatabase db
, const char *name
,
77 char *full_name
= alloca (strlen (name
) + sizeof "customization" + 3);
78 char *full_class
= alloca (strlen (class) + sizeof "Customization" + 3);
81 sprintf (full_name
, "%s.%s", name
, "customization");
82 sprintf (full_class
, "%s.%s", class, "Customization");
84 result
= x_get_string_resource (db
, full_name
, full_class
);
88 char *copy
= xmalloc (strlen (result
) + 1);
89 strcpy (copy
, result
);
97 /* Expand all the Xt-style %-escapes in STRING, whose length is given
98 by STRING_LEN. Here are the escapes we're supposed to recognize:
100 %N The value of the application's class name
101 %T The value of the type parameter ("app-defaults" in this
103 %S The value of the suffix parameter ("" in this context)
104 %L The language string associated with the specified display
105 (We use the "LANG" environment variable here, if it's set.)
106 %l The language part of the display's language string
107 (We treat this just like %L. If someone can tell us what
108 we're really supposed to do, dandy.)
109 %t The territory part of the display's language string
110 (This never gets used.)
111 %c The codeset part of the display's language string
112 (This never gets used either.)
113 %C The customization string retrieved from the resource
114 database associated with display.
115 (This is x_customization_string.)
117 Return the expanded file name if it exists and is readable, and
118 refers to %L only when the LANG environment variable is set, or
119 otherwise provided by X.
121 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
122 %-escapes in ESCAPED_SUFFIX are expanded.
124 Return NULL otherwise. */
127 magic_file_p (const char *string
, ptrdiff_t string_len
, const char *class,
128 const char *escaped_suffix
)
130 char *lang
= getenv ("LANG");
132 ptrdiff_t path_size
= 100;
133 char *path
= xmalloc (path_size
);
134 ptrdiff_t path_len
= 0;
136 const char *p
= string
;
138 while (p
< string
+ string_len
)
140 /* The chunk we're about to stick on the end of result. */
141 const char *next
= NULL
;
148 if (p
>= string
+ string_len
)
159 next
= (x_customization_string
160 ? x_customization_string
162 next_len
= strlen (next
);
167 next_len
= strlen (class);
171 next
= "app-defaults";
172 next_len
= strlen (next
);
189 next_len
= strlen (next
);
199 next
= p
, next_len
= 1;
201 /* Do we have room for this component followed by a '\0' ? */
202 if (path_size
- path_len
<= next_len
)
204 if (min (PTRDIFF_MAX
, SIZE_MAX
) / 2 - 1 - path_len
< next_len
)
205 memory_full (SIZE_MAX
);
206 path_size
= (path_len
+ next_len
+ 1) * 2;
207 path
= 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 path
[path_len
] = '\0';
244 if ((ptr
= getenv ("HOME")) == NULL
)
246 if ((ptr
= getenv ("LOGNAME")) != NULL
247 || (ptr
= getenv ("USER")) != NULL
)
250 pw
= getpwuid (getuid ());
257 return xstrdup ("/");
259 copy
= xmalloc (strlen (ptr
) + 2);
268 file_p (const char *filename
)
272 return (access (filename
, 4) == 0 /* exists and is readable */
273 && stat (filename
, &status
) == 0 /* get the status */
274 && (S_ISDIR (status
.st_mode
)) == 0); /* not a directory */
278 /* Find the first element of SEARCH_PATH which exists and is readable,
279 after expanding the %-escapes. Return 0 if we didn't find any, and
280 the path name of the one we found otherwise. */
283 search_magic_path (const char *search_path
, const char *class,
284 const char *escaped_suffix
)
288 for (s
= search_path
; *s
; s
= p
)
290 for (p
= s
; *p
&& *p
!= ':'; p
++)
295 char *path
= magic_file_p (s
, p
- s
, class, escaped_suffix
);
304 path
= magic_file_p (s
, strlen (s
), class, escaped_suffix
);
316 /* Producing databases for individual sources. */
319 get_system_app (const char *class)
321 XrmDatabase db
= NULL
;
325 path
= getenv ("XFILESEARCHPATH");
326 if (! path
) path
= PATH_X_DEFAULTS
;
328 p
= search_magic_path (path
, class, 0);
331 db
= XrmGetFileDatabase (p
);
340 get_fallback (Display
*display
)
347 get_user_app (const char *class)
353 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
354 names, not directories. */
355 if (((path
= getenv ("XUSERFILESEARCHPATH"))
356 && (file
= search_magic_path (path
, class, 0)))
358 /* Check for APPLRESDIR; it is a path of directories. In each,
359 we have to search for LANG/CLASS and then CLASS. */
360 || ((path
= getenv ("XAPPLRESDIR"))
361 && ((file
= search_magic_path (path
, class, "/%L/%N"))
362 || (file
= search_magic_path (path
, class, "/%N"))))
364 /* Check in the home directory. This is a bit of a hack; let's
365 hope one's home directory doesn't contain any %-escapes. */
366 || (free_it
= gethomedir (),
367 ((file
= search_magic_path (free_it
, class, "%L/%N"))
368 || (file
= search_magic_path (free_it
, class, "%N")))))
370 XrmDatabase db
= XrmGetFileDatabase (file
);
382 get_user_db (Display
*display
)
387 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
388 xdefs
= XResourceManagerString (display
);
390 xdefs
= display
->xdefaults
;
394 db
= XrmGetStringDatabase (xdefs
);
400 home
= gethomedir ();
401 xdefault
= xmalloc (strlen (home
) + sizeof ".Xdefaults");
402 strcpy (xdefault
, home
);
403 strcat (xdefault
, ".Xdefaults");
404 db
= XrmGetFileDatabase (xdefault
);
409 #ifdef HAVE_XSCREENRESOURCESTRING
410 /* Get the screen-specific resources too. */
411 xdefs
= XScreenResourceString (DefaultScreenOfDisplay (display
));
414 XrmMergeDatabases (XrmGetStringDatabase (xdefs
), &db
);
423 get_environ_db (void)
429 if ((p
= getenv ("XENVIRONMENT")) == NULL
)
431 static char const xdefaults
[] = ".Xdefaults-";
432 char *home
= gethomedir ();
433 char const *host
= get_system_name ();
434 ptrdiff_t pathsize
= strlen (home
) + sizeof xdefaults
+ strlen (host
);
435 path
= xrealloc (home
, pathsize
);
436 strcat (strcat (path
, xdefaults
), host
);
440 db
= XrmGetFileDatabase (p
);
447 /* External interface. */
449 /* Types of values that we can find in a database */
451 #define XrmStringType "String" /* String representation */
452 static XrmRepresentation x_rm_string
; /* Quark representation */
454 /* Load X resources based on the display and a possible -xrm option. */
457 x_load_resources (Display
*display
, const char *xrm_string
,
458 const char *myname
, const char *myclass
)
460 XrmDatabase user_database
;
465 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
466 const char *helv
= "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
470 const char *courier
= "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
473 x_rm_string
= XrmStringToQuark (XrmStringType
);
474 #ifndef USE_X_TOOLKIT
475 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
476 I suspect it's because the toolkit version does this elsewhere. */
479 rdb
= XrmGetStringDatabase ("");
481 /* Add some font defaults. If the font `helv' doesn't exist, widgets
482 will use some other default font. */
485 sprintf (line
, "%s.pane.background: grey75", myclass
);
486 XrmPutLineResource (&rdb
, line
);
487 sprintf (line
, "%s*fontList: %s", myclass
, helv
);
488 XrmPutLineResource (&rdb
, line
);
489 sprintf (line
, "%s*menu*background: grey75", myclass
);
490 XrmPutLineResource (&rdb
, line
);
491 sprintf (line
, "%s*menubar*background: grey75", myclass
);
492 XrmPutLineResource (&rdb
, line
);
493 sprintf (line
, "%s*verticalScrollBar.background: grey75", myclass
);
494 XrmPutLineResource (&rdb
, line
);
495 sprintf (line
, "%s*verticalScrollBar.troughColor: grey75", myclass
);
496 XrmPutLineResource (&rdb
, line
);
497 sprintf (line
, "%s.dialog*.background: grey75", myclass
);
498 XrmPutLineResource (&rdb
, line
);
499 sprintf (line
, "%s*fsb.Text.background: white", myclass
);
500 XrmPutLineResource (&rdb
, line
);
501 sprintf (line
, "%s*fsb.FilterText.background: white", myclass
);
502 XrmPutLineResource (&rdb
, line
);
503 sprintf (line
, "%s*fsb*DirList.background: white", myclass
);
504 XrmPutLineResource (&rdb
, line
);
505 sprintf (line
, "%s*fsb*ItemsList.background: white", myclass
);
506 XrmPutLineResource (&rdb
, line
);
507 sprintf (line
, "%s*fsb*background: grey75", myclass
);
508 XrmPutLineResource (&rdb
, line
);
509 sprintf (line
, "%s*fsb.Text.fontList: %s", myclass
, courier
);
510 XrmPutLineResource (&rdb
, line
);
511 sprintf (line
, "%s*fsb.FilterText.fontList: %s", myclass
, courier
);
512 XrmPutLineResource (&rdb
, line
);
513 sprintf (line
, "%s*fsb*ItemsList.fontList: %s", myclass
, courier
);
514 XrmPutLineResource (&rdb
, line
);
515 sprintf (line
, "%s*fsb*DirList.fontList: %s", myclass
, courier
);
516 XrmPutLineResource (&rdb
, line
);
518 /* Set double click time of list boxes in the file selection
519 dialog from `double-click-time'. */
520 if (INTEGERP (Vdouble_click_time
) && XINT (Vdouble_click_time
) > 0)
522 sprintf (line
, "%s*fsb*DirList.doubleClickInterval: %"pI
"d",
523 myclass
, XFASTINT (Vdouble_click_time
));
524 XrmPutLineResource (&rdb
, line
);
525 sprintf (line
, "%s*fsb*ItemsList.doubleClickInterval: %"pI
"d",
526 myclass
, XFASTINT (Vdouble_click_time
));
527 XrmPutLineResource (&rdb
, line
);
530 #else /* not USE_MOTIF */
532 sprintf (line
, "Emacs.dialog*.background: grey75");
533 XrmPutLineResource (&rdb
, line
);
534 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
535 sprintf (line
, "Emacs.dialog*.font: %s", helv
);
536 XrmPutLineResource (&rdb
, line
);
537 sprintf (line
, "*XlwMenu*font: %s", helv
);
538 XrmPutLineResource (&rdb
, line
);
540 sprintf (line
, "*XlwMenu*background: grey75");
541 XrmPutLineResource (&rdb
, line
);
542 sprintf (line
, "Emacs*verticalScrollBar.background: grey75");
543 XrmPutLineResource (&rdb
, line
);
545 #endif /* not USE_MOTIF */
547 user_database
= get_user_db (display
);
549 /* Figure out what the "customization string" is, so we can use it
551 xfree (x_customization_string
);
552 x_customization_string
553 = x_get_customization_string (user_database
, myname
, myclass
);
555 /* Get application system defaults */
556 db
= get_system_app (myclass
);
558 XrmMergeDatabases (db
, &rdb
);
560 /* Get Fallback resources */
561 db
= get_fallback (display
);
563 XrmMergeDatabases (db
, &rdb
);
565 /* Get application user defaults */
566 db
= get_user_app (myclass
);
568 XrmMergeDatabases (db
, &rdb
);
570 /* get User defaults */
571 if (user_database
!= NULL
)
572 XrmMergeDatabases (user_database
, &rdb
);
574 /* Get Environment defaults. */
575 db
= get_environ_db ();
577 XrmMergeDatabases (db
, &rdb
);
579 /* Last, merge in any specification from the command line. */
580 if (xrm_string
!= NULL
)
582 db
= XrmGetStringDatabase (xrm_string
);
584 XrmMergeDatabases (db
, &rdb
);
591 /* Retrieve the value of the resource specified by NAME with class CLASS
592 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
595 x_get_resource (XrmDatabase rdb
, const char *name
, const char *class,
596 XrmRepresentation expected_type
, XrmValue
*ret_value
)
599 XrmName namelist
[100];
600 XrmClass classlist
[100];
601 XrmRepresentation type
;
603 XrmStringToNameList (name
, namelist
);
604 XrmStringToClassList (class, classlist
);
606 if (XrmQGetResource (rdb
, namelist
, classlist
, &type
, &value
) == True
607 && (type
== expected_type
))
609 if (type
== x_rm_string
)
610 ret_value
->addr
= (char *) value
.addr
;
612 memcpy (ret_value
->addr
, value
.addr
, ret_value
->size
);
620 /* Retrieve the string resource specified by NAME with CLASS from
624 x_get_string_resource (XrmDatabase rdb
, const char *name
, const char *class)
628 if (inhibit_x_resources
)
629 /* --quick was passed, so this is a no-op. */
632 if (x_get_resource (rdb
, name
, class, x_rm_string
, &value
))
633 return (char *) value
.addr
;
638 /* Stand-alone test facilities. */
643 #define arg_listify(len, list) (list)
644 #define car(list) (*(list))
645 #define cdr(list) (list + 1)
646 #define NIL(list) (! *(list))
647 #define free_arglist(list)
650 member (char *elt
, List list
)
654 for (p
= list
; ! NIL (p
); p
= cdr (p
))
655 if (! strcmp (elt
, car (p
)))
662 fatal (char *msg
, char *prog
)
667 (void) fprintf (stderr
, msg
, prog
);
672 main (int argc
, char **argv
)
675 char *displayname
, *resource_string
, *class, *name
;
679 arg_list
= arg_listify (argc
, argv
);
681 lp
= member ("-d", arg_list
);
683 displayname
= car (cdr (lp
));
685 displayname
= "localhost:0.0";
687 lp
= member ("-xrm", arg_list
);
689 resource_string
= car (cdr (lp
));
691 resource_string
= (char *) 0;
693 lp
= member ("-c", arg_list
);
695 class = car (cdr (lp
));
699 lp
= member ("-n", arg_list
);
701 name
= car (cdr (lp
));
705 free_arglist (arg_list
);
707 if (!(display
= XOpenDisplay (displayname
)))
708 fatal ("Can't open display '%s'\n", XDisplayName (displayname
));
710 xdb
= x_load_resources (display
, resource_string
, name
, class);
712 /* In a real program, you'd want to also do this: */
718 char query_class
[90];
723 if (strlen (query_name
))
730 value
= x_get_string_resource (xdb
, query_name
, query_class
);
733 printf ("\t%s(%s): %s\n\n", query_name
, query_class
, value
);
735 printf ("\tNo Value.\n\n");
740 printf ("\tExit.\n\n");
742 XCloseDisplay (display
);