Use *font for Xft font names for Lucid menus and dialogs.
[bpt/emacs.git] / src / xrdb.c
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2011 Free Software Foundation, Inc.
3
4 Author: Joseph Arceneaux
5 Created: 4/90
6
7 This file is part of GNU Emacs.
8
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.
13
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.
18
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/>. */
21
22 #include <config.h>
23
24 #include <unistd.h>
25 #include <errno.h>
26 #include <epaths.h>
27
28 #include <stdio.h>
29 #include <setjmp.h>
30
31 #include <X11/Xlib.h>
32 #include <X11/Xatom.h>
33 #include <X11/X.h>
34 #include <X11/Xutil.h>
35 #include <X11/Xresource.h>
36 #ifdef HAVE_PWD_H
37 #include <pwd.h>
38 #endif
39 #include <sys/stat.h>
40
41 #if !defined(S_ISDIR) && defined(S_IFDIR)
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
43 #endif
44
45 #include "lisp.h"
46
47 #ifdef USE_MOTIF
48 /* For Vdouble_click_time. */
49 #include "keyboard.h"
50 #endif
51
52 extern char *getenv (const char *);
53
54 extern struct passwd *getpwuid (uid_t);
55 extern struct passwd *getpwnam (const char *);
56
57 extern const char *get_system_name (void);
58
59 char *x_get_string_resource (XrmDatabase rdb, const char *name,
60 const char *class);
61 static int file_p (const char *filename);
62
63 \f
64 /* X file search path processing. */
65
66
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;
70
71
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. */
75 static char *
76 x_get_customization_string (XrmDatabase db, const char *name,
77 const char *class)
78 {
79 char *full_name
80 = (char *) alloca (strlen (name) + sizeof ("customization") + 3);
81 char *full_class
82 = (char *) alloca (strlen (class) + sizeof ("Customization") + 3);
83 char *result;
84
85 sprintf (full_name, "%s.%s", name, "customization");
86 sprintf (full_class, "%s.%s", class, "Customization");
87
88 result = x_get_string_resource (db, full_name, full_class);
89
90 if (result)
91 {
92 char *copy = (char *) xmalloc (strlen (result) + 1);
93 strcpy (copy, result);
94 return copy;
95 }
96 else
97 return 0;
98 }
99
100
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:
103
104 %N The value of the application's class name
105 %T The value of the type parameter ("app-defaults" in this
106 context)
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.)
120
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.
124
125 ESCAPED_SUFFIX and SUFFIX are postpended to STRING if they are
126 non-zero. %-escapes in ESCAPED_SUFFIX are expanded; STRING is left
127 alone.
128
129 Return NULL otherwise. */
130
131 static char *
132 magic_file_p (const char *string, EMACS_INT string_len, const char *class, const char *escaped_suffix, const char *suffix)
133 {
134 char *lang = getenv ("LANG");
135
136 int path_size = 100;
137 char *path = (char *) xmalloc (path_size);
138 int path_len = 0;
139
140 const char *p = string;
141
142 while (p < string + string_len)
143 {
144 /* The chunk we're about to stick on the end of result. */
145 const char *next = NULL;
146 int next_len;
147
148 if (*p == '%')
149 {
150 p++;
151
152 if (p >= string + string_len)
153 next_len = 0;
154 else
155 switch (*p)
156 {
157 case '%':
158 next = "%";
159 next_len = 1;
160 break;
161
162 case 'C':
163 next = (x_customization_string
164 ? x_customization_string
165 : "");
166 next_len = strlen (next);
167 break;
168
169 case 'N':
170 next = class;
171 next_len = strlen (class);
172 break;
173
174 case 'T':
175 next = "app-defaults";
176 next_len = strlen (next);
177 break;
178
179 default:
180 case 'S':
181 next_len = 0;
182 break;
183
184 case 'L':
185 case 'l':
186 if (! lang)
187 {
188 xfree (path);
189 return NULL;
190 }
191
192 next = lang;
193 next_len = strlen (next);
194 break;
195
196 case 't':
197 case 'c':
198 xfree (path);
199 return NULL;
200 }
201 }
202 else
203 next = p, next_len = 1;
204
205 /* Do we have room for this component followed by a '\0' ? */
206 if (path_len + next_len + 1 > path_size)
207 {
208 path_size = (path_len + next_len + 1) * 2;
209 path = (char *) xrealloc (path, path_size);
210 }
211
212 memcpy (path + path_len, next, next_len);
213 path_len += next_len;
214
215 p++;
216
217 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
218 if (p >= string + string_len && escaped_suffix)
219 {
220 string = escaped_suffix;
221 string_len = strlen (string);
222 p = string;
223 escaped_suffix = NULL;
224 }
225 }
226
227 /* Perhaps we should add the SUFFIX now. */
228 if (suffix)
229 {
230 int suffix_len = strlen (suffix);
231
232 if (path_len + suffix_len + 1 > path_size)
233 {
234 path_size = (path_len + suffix_len + 1);
235 path = (char *) xrealloc (path, path_size);
236 }
237
238 memcpy (path + path_len, suffix, suffix_len);
239 path_len += suffix_len;
240 }
241
242 path[path_len] = '\0';
243
244 if (! file_p (path))
245 {
246 xfree (path);
247 return NULL;
248 }
249
250 return path;
251 }
252
253
254 static char *
255 gethomedir (void)
256 {
257 struct passwd *pw;
258 char *ptr;
259 char *copy;
260
261 if ((ptr = getenv ("HOME")) == NULL)
262 {
263 if ((ptr = getenv ("LOGNAME")) != NULL
264 || (ptr = getenv ("USER")) != NULL)
265 pw = getpwnam (ptr);
266 else
267 pw = getpwuid (getuid ());
268
269 if (pw)
270 ptr = pw->pw_dir;
271 }
272
273 if (ptr == NULL)
274 return xstrdup ("/");
275
276 copy = (char *) xmalloc (strlen (ptr) + 2);
277 strcpy (copy, ptr);
278 strcat (copy, "/");
279
280 return copy;
281 }
282
283
284 static int
285 file_p (const char *filename)
286 {
287 struct stat status;
288
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 */
292 }
293
294
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. */
298
299 static char *
300 search_magic_path (const char *search_path, const char *class, const char *escaped_suffix, const char *suffix)
301 {
302 const char *s, *p;
303
304 for (s = search_path; *s; s = p)
305 {
306 for (p = s; *p && *p != ':'; p++)
307 ;
308
309 if (p > s)
310 {
311 char *path = magic_file_p (s, p - s, class, escaped_suffix,
312 suffix);
313 if (path)
314 return path;
315 }
316 else if (*p == ':')
317 {
318 char *path;
319
320 s = "%N%S";
321 path = magic_file_p (s, strlen (s), class, escaped_suffix, suffix);
322 if (path)
323 return path;
324 }
325
326 if (*p == ':')
327 p++;
328 }
329
330 return 0;
331 }
332 \f
333 /* Producing databases for individual sources. */
334
335 static XrmDatabase
336 get_system_app (const char *class)
337 {
338 XrmDatabase db = NULL;
339 const char *path;
340 char *p;
341
342 path = getenv ("XFILESEARCHPATH");
343 if (! path) path = PATH_X_DEFAULTS;
344
345 p = search_magic_path (path, class, 0, 0);
346 if (p)
347 {
348 db = XrmGetFileDatabase (p);
349 xfree (p);
350 }
351
352 return db;
353 }
354
355
356 static XrmDatabase
357 get_fallback (Display *display)
358 {
359 return NULL;
360 }
361
362
363 static XrmDatabase
364 get_user_app (const char *class)
365 {
366 const char *path;
367 char *file = 0;
368 char *free_it = 0;
369
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)))
374
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))))
380
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)))))
386 {
387 XrmDatabase db = XrmGetFileDatabase (file);
388 xfree (file);
389 xfree (free_it);
390 return db;
391 }
392
393 xfree (free_it);
394 return NULL;
395 }
396
397
398 static XrmDatabase
399 get_user_db (Display *display)
400 {
401 XrmDatabase db;
402 char *xdefs;
403
404 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
405 xdefs = XResourceManagerString (display);
406 #else
407 xdefs = display->xdefaults;
408 #endif
409
410 if (xdefs != NULL)
411 db = XrmGetStringDatabase (xdefs);
412 else
413 {
414 char *home;
415 char *xdefault;
416
417 home = gethomedir ();
418 xdefault = (char *) xmalloc (strlen (home) + sizeof (".Xdefaults"));
419 strcpy (xdefault, home);
420 strcat (xdefault, ".Xdefaults");
421 db = XrmGetFileDatabase (xdefault);
422 xfree (home);
423 xfree (xdefault);
424 }
425
426 #ifdef HAVE_XSCREENRESOURCESTRING
427 /* Get the screen-specific resources too. */
428 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
429 if (xdefs != NULL)
430 {
431 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
432 XFree (xdefs);
433 }
434 #endif
435
436 return db;
437 }
438
439 static XrmDatabase
440 get_environ_db (void)
441 {
442 XrmDatabase db;
443 char *p;
444 char *path = 0, *home = 0;
445 const char *host;
446
447 if ((p = getenv ("XENVIRONMENT")) == NULL)
448 {
449 home = gethomedir ();
450 host = get_system_name ();
451 path = (char *) xmalloc (strlen (home)
452 + sizeof (".Xdefaults-")
453 + strlen (host));
454 sprintf (path, "%s%s%s", home, ".Xdefaults-", host);
455 p = path;
456 }
457
458 db = XrmGetFileDatabase (p);
459
460 xfree (path);
461 xfree (home);
462
463 return db;
464 }
465 \f
466 /* External interface. */
467
468 /* Types of values that we can find in a database */
469
470 #define XrmStringType "String" /* String representation */
471 XrmRepresentation x_rm_string; /* Quark representation */
472
473 /* Load X resources based on the display and a possible -xrm option. */
474
475 XrmDatabase
476 x_load_resources (Display *display, const char *xrm_string,
477 const char *myname, const char *myclass)
478 {
479 XrmDatabase user_database;
480 XrmDatabase rdb;
481 XrmDatabase db;
482 char line[256];
483
484 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
485
486 #ifdef USE_MOTIF
487 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
488 #endif
489
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. */
494 XrmInitialize ();
495 #endif
496 rdb = XrmGetStringDatabase ("");
497
498 /* Add some font defaults. If the font `helv' doesn't exist, widgets
499 will use some other default font. */
500 #ifdef USE_MOTIF
501
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);
534
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)
538 {
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);
545 }
546
547 #else /* not USE_MOTIF */
548
549 sprintf (line, "Emacs.dialog*.background: grey75");
550 XrmPutLineResource (&rdb, line);
551 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
552 sprintf (line, "Emacs.dialog*.font: %s", helv);
553 XrmPutLineResource (&rdb, line);
554 sprintf (line, "*XlwMenu*font: %s", helv);
555 XrmPutLineResource (&rdb, line);
556 #endif
557 sprintf (line, "*XlwMenu*background: grey75");
558 XrmPutLineResource (&rdb, line);
559 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
560 XrmPutLineResource (&rdb, line);
561
562 #endif /* not USE_MOTIF */
563
564 user_database = get_user_db (display);
565
566 /* Figure out what the "customization string" is, so we can use it
567 to decode paths. */
568 xfree (x_customization_string);
569 x_customization_string
570 = x_get_customization_string (user_database, myname, myclass);
571
572 /* Get application system defaults */
573 db = get_system_app (myclass);
574 if (db != NULL)
575 XrmMergeDatabases (db, &rdb);
576
577 /* Get Fallback resources */
578 db = get_fallback (display);
579 if (db != NULL)
580 XrmMergeDatabases (db, &rdb);
581
582 /* Get application user defaults */
583 db = get_user_app (myclass);
584 if (db != NULL)
585 XrmMergeDatabases (db, &rdb);
586
587 /* get User defaults */
588 if (user_database != NULL)
589 XrmMergeDatabases (user_database, &rdb);
590
591 /* Get Environment defaults. */
592 db = get_environ_db ();
593 if (db != NULL)
594 XrmMergeDatabases (db, &rdb);
595
596 /* Last, merge in any specification from the command line. */
597 if (xrm_string != NULL)
598 {
599 db = XrmGetStringDatabase (xrm_string);
600 if (db != NULL)
601 XrmMergeDatabases (db, &rdb);
602 }
603
604 return rdb;
605 }
606
607
608 /* Retrieve the value of the resource specified by NAME with class CLASS
609 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
610
611 static int
612 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
613 XrmRepresentation expected_type, XrmValue *ret_value)
614 {
615 XrmValue value;
616 XrmName namelist[100];
617 XrmClass classlist[100];
618 XrmRepresentation type;
619
620 XrmStringToNameList(name, namelist);
621 XrmStringToClassList(class, classlist);
622
623 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
624 && (type == expected_type))
625 {
626 if (type == x_rm_string)
627 ret_value->addr = (char *) value.addr;
628 else
629 memcpy (ret_value->addr, value.addr, ret_value->size);
630
631 return value.size;
632 }
633
634 return 0;
635 }
636
637 /* Retrieve the string resource specified by NAME with CLASS from
638 database RDB. */
639
640 char *
641 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
642 {
643 XrmValue value;
644
645 if (inhibit_x_resources)
646 /* --quick was passed, so this is a no-op. */
647 return NULL;
648
649 if (x_get_resource (rdb, name, class, x_rm_string, &value))
650 return (char *) value.addr;
651
652 return (char *) 0;
653 }
654 \f
655 /* Stand-alone test facilities. */
656
657 #ifdef TESTRM
658
659 typedef char **List;
660 #define arg_listify(len, list) (list)
661 #define car(list) (*(list))
662 #define cdr(list) (list + 1)
663 #define NIL(list) (! *(list))
664 #define free_arglist(list)
665
666 static List
667 member (elt, list)
668 char *elt;
669 List list;
670 {
671 List p;
672
673 for (p = list; ! NIL (p); p = cdr (p))
674 if (! strcmp (elt, car (p)))
675 return p;
676
677 return p;
678 }
679
680 static void
681 fatal (msg, prog, x1, x2, x3, x4, x5)
682 char *msg, *prog;
683 int x1, x2, x3, x4, x5;
684 {
685 if (errno)
686 perror (prog);
687
688 (void) fprintf (stderr, msg, prog, x1, x2, x3, x4, x5);
689 exit (1);
690 }
691
692 main (argc, argv)
693 int argc;
694 char **argv;
695 {
696 Display *display;
697 char *displayname, *resource_string, *class, *name;
698 XrmDatabase xdb;
699 List arg_list, lp;
700
701 arg_list = arg_listify (argc, argv);
702
703 lp = member ("-d", arg_list);
704 if (!NIL (lp))
705 displayname = car (cdr (lp));
706 else
707 displayname = "localhost:0.0";
708
709 lp = member ("-xrm", arg_list);
710 if (! NIL (lp))
711 resource_string = car (cdr (lp));
712 else
713 resource_string = (char *) 0;
714
715 lp = member ("-c", arg_list);
716 if (! NIL (lp))
717 class = car (cdr (lp));
718 else
719 class = "Emacs";
720
721 lp = member ("-n", arg_list);
722 if (! NIL (lp))
723 name = car (cdr (lp));
724 else
725 name = "emacs";
726
727 free_arglist (arg_list);
728
729 if (!(display = XOpenDisplay (displayname)))
730 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
731
732 xdb = x_load_resources (display, resource_string, name, class);
733
734 /* In a real program, you'd want to also do this: */
735 display->db = xdb;
736
737 while (1)
738 {
739 char query_name[90];
740 char query_class[90];
741
742 printf ("Name: ");
743 gets (query_name);
744
745 if (strlen (query_name))
746 {
747 char *value;
748
749 printf ("Class: ");
750 gets (query_class);
751
752 value = x_get_string_resource (xdb, query_name, query_class);
753
754 if (value != NULL)
755 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
756 else
757 printf ("\tNo Value.\n\n");
758 }
759 else
760 break;
761 }
762 printf ("\tExit.\n\n");
763
764 XCloseDisplay (display);
765 }
766 #endif /* TESTRM */
767