In w32fns.c's Fx_frame_geometry rewrite check whether frame has a titlebar
[emacs.git] / src / xrdb.c
blob2235b4535daf7665d66673689af346f08e70a7e2
1 /* Deal with the X Resource Manager.
2 Copyright (C) 1990, 1993-1994, 2000-2015 Free Software Foundation,
3 Inc.
5 Author: Joseph Arceneaux
6 Created: 4/90
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/>. */
23 #include <config.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <epaths.h>
28 #include <stdlib.h>
29 #include <stdio.h>
31 #include "lisp.h"
33 /* This may include sys/types.h, and that somehow loses
34 if this is not done before the other system files. */
35 #include "xterm.h"
37 #include <X11/Xlib.h>
38 #include <X11/Xatom.h>
39 #include <X11/X.h>
40 #include <X11/Xutil.h>
41 #include <X11/Xresource.h>
42 #ifdef HAVE_PWD_H
43 #include <pwd.h>
44 #endif
46 #ifdef USE_MOTIF
47 /* For Vdouble_click_time. */
48 #include "keyboard.h"
49 #endif
51 /* X file search path processing. */
54 /* The string which gets substituted for the %C escape in XFILESEARCHPATH
55 and friends, or zero if none was specified. */
56 static char *x_customization_string;
59 /* Return the value of the emacs.customization (Emacs.Customization)
60 resource, for later use in search path decoding. If we find no
61 such resource, return zero. */
62 static char *
63 x_get_customization_string (XrmDatabase db, const char *name,
64 const char *class)
66 char *full_name = alloca (strlen (name) + sizeof "customization" + 3);
67 char *full_class = alloca (strlen (class) + sizeof "Customization" + 3);
68 char *result;
70 sprintf (full_name, "%s.%s", name, "customization");
71 sprintf (full_class, "%s.%s", class, "Customization");
73 result = x_get_string_resource (db, full_name, full_class);
74 return result ? xstrdup (result) : NULL;
77 /* Expand all the Xt-style %-escapes in STRING, whose length is given
78 by STRING_LEN. Here are the escapes we're supposed to recognize:
80 %N The value of the application's class name
81 %T The value of the type parameter ("app-defaults" in this
82 context)
83 %S The value of the suffix parameter ("" in this context)
84 %L The language string associated with the specified display
85 (We use the "LANG" environment variable here, if it's set.)
86 %l The language part of the display's language string
87 (We treat this just like %L. If someone can tell us what
88 we're really supposed to do, dandy.)
89 %t The territory part of the display's language string
90 (This never gets used.)
91 %c The codeset part of the display's language string
92 (This never gets used either.)
93 %C The customization string retrieved from the resource
94 database associated with display.
95 (This is x_customization_string.)
97 Return the resource database if its file was read successfully, and
98 refers to %L only when the LANG environment variable is set, or
99 otherwise provided by X.
101 ESCAPED_SUFFIX is postpended to STRING if it is non-zero.
102 %-escapes in ESCAPED_SUFFIX are expanded.
104 Return NULL otherwise. */
106 static XrmDatabase
107 magic_db (const char *string, ptrdiff_t string_len, const char *class,
108 const char *escaped_suffix)
110 XrmDatabase db;
111 char *lang = getenv ("LANG");
113 ptrdiff_t path_size = 100;
114 char *path = xmalloc (path_size);
115 ptrdiff_t path_len = 0;
117 const char *p = string;
119 while (p < string + string_len)
121 /* The chunk we're about to stick on the end of result. */
122 const char *next = p;
123 ptrdiff_t next_len = 1;
125 if (*p == '%')
127 p++;
129 if (p >= string + string_len)
130 next_len = 0;
131 else
132 switch (*p)
134 case '%':
135 next = "%";
136 next_len = 1;
137 break;
139 case 'C':
140 if (x_customization_string)
142 next = x_customization_string;
143 next_len = strlen (next);
145 else
146 next_len = 0;
147 break;
149 case 'N':
150 next = class;
151 next_len = strlen (class);
152 break;
154 case 'T':
155 next = "app-defaults";
156 next_len = strlen (next);
157 break;
159 default:
160 case 'S':
161 next_len = 0;
162 break;
164 case 'L':
165 case 'l':
166 if (! lang)
168 xfree (path);
169 return NULL;
172 next = lang;
173 next_len = strlen (next);
174 break;
176 case 't':
177 case 'c':
178 xfree (path);
179 return NULL;
183 /* Do we have room for this component followed by a '\0'? */
184 if (path_size - path_len <= next_len)
186 if (min (PTRDIFF_MAX, SIZE_MAX) / 2 - 1 - path_len < next_len)
187 memory_full (SIZE_MAX);
188 path_size = (path_len + next_len + 1) * 2;
189 path = xrealloc (path, path_size);
192 memcpy (path + path_len, next, next_len);
193 path_len += next_len;
195 p++;
197 /* If we've reached the end of the string, append ESCAPED_SUFFIX. */
198 if (p >= string + string_len && escaped_suffix)
200 string = escaped_suffix;
201 string_len = strlen (string);
202 p = string;
203 escaped_suffix = NULL;
207 path[path_len] = '\0';
208 db = XrmGetFileDatabase (path);
209 xfree (path);
210 return db;
214 static char *
215 gethomedir (void)
217 struct passwd *pw;
218 char *ptr;
219 char *copy;
221 if ((ptr = getenv ("HOME")) == NULL)
223 if ((ptr = getenv ("LOGNAME")) != NULL
224 || (ptr = getenv ("USER")) != NULL)
225 pw = getpwnam (ptr);
226 else
227 pw = getpwuid (getuid ());
229 if (pw)
230 ptr = pw->pw_dir;
233 if (ptr == NULL)
234 return xstrdup ("/");
236 ptrdiff_t len = strlen (ptr);
237 copy = xmalloc (len + 2);
238 strcpy (copy + len, "/");
239 return memcpy (copy, ptr, len);
243 /* Find the first element of SEARCH_PATH which exists and is readable,
244 after expanding the %-escapes. Return 0 if we didn't find any, and
245 the path name of the one we found otherwise. */
247 static XrmDatabase
248 search_magic_path (const char *search_path, const char *class,
249 const char *escaped_suffix)
251 const char *s, *p;
253 for (s = search_path; *s; s = p)
255 for (p = s; *p && *p != ':'; p++)
258 if (p > s)
260 XrmDatabase db = magic_db (s, p - s, class, escaped_suffix);
261 if (db)
262 return db;
264 else if (*p == ':')
266 static char const ns[] = "%N%S";
267 XrmDatabase db = magic_db (ns, strlen (ns), class, escaped_suffix);
268 if (db)
269 return db;
272 if (*p == ':')
273 p++;
276 return 0;
279 /* Producing databases for individual sources. */
281 static XrmDatabase
282 get_system_app (const char *class)
284 const char *path;
286 path = getenv ("XFILESEARCHPATH");
287 if (! path) path = PATH_X_DEFAULTS;
289 return search_magic_path (path, class, 0);
293 static XrmDatabase
294 get_fallback (Display *display)
296 return NULL;
300 static XrmDatabase
301 get_user_app (const char *class)
303 XrmDatabase db = 0;
304 const char *path;
306 /* Check for XUSERFILESEARCHPATH. It is a path of complete file
307 names, not directories. */
308 path = getenv ("XUSERFILESEARCHPATH");
309 if (path)
310 db = search_magic_path (path, class, 0);
312 if (! db)
314 /* Check for APPLRESDIR; it is a path of directories. In each,
315 we have to search for LANG/CLASS and then CLASS. */
316 path = getenv ("XAPPLRESDIR");
317 if (path)
319 db = search_magic_path (path, class, "/%L/%N");
320 if (!db)
321 db = search_magic_path (path, class, "/%N");
325 if (! db)
327 /* Check in the home directory. This is a bit of a hack; let's
328 hope one's home directory doesn't contain any %-escapes. */
329 char *home = gethomedir ();
330 db = search_magic_path (home, class, "%L/%N");
331 if (! db)
332 db = search_magic_path (home, class, "%N");
333 xfree (home);
336 return db;
339 static char const xdefaults[] = ".Xdefaults";
341 static XrmDatabase
342 get_user_db (Display *display)
344 XrmDatabase db;
345 char *xdefs;
347 #ifdef PBaseSize /* Cheap way to test for X11R4 or later. */
348 xdefs = XResourceManagerString (display);
349 #else
350 xdefs = display->xdefaults;
351 #endif
353 if (xdefs != NULL)
354 db = XrmGetStringDatabase (xdefs);
355 else
357 char *home = gethomedir ();
358 ptrdiff_t homelen = strlen (home);
359 char *filename = xrealloc (home, homelen + sizeof xdefaults);
360 strcpy (filename + homelen, xdefaults);
361 db = XrmGetFileDatabase (filename);
362 xfree (filename);
365 #ifdef HAVE_XSCREENRESOURCESTRING
366 /* Get the screen-specific resources too. */
367 xdefs = XScreenResourceString (DefaultScreenOfDisplay (display));
368 if (xdefs != NULL)
370 XrmMergeDatabases (XrmGetStringDatabase (xdefs), &db);
371 XFree (xdefs);
373 #endif
375 return db;
378 static XrmDatabase
379 get_environ_db (void)
381 XrmDatabase db;
382 char *p = getenv ("XENVIRONMENT");
383 char *filename = 0;
385 if (!p)
387 char *home = gethomedir ();
388 ptrdiff_t homelen = strlen (home);
389 Lisp_Object system_name = Fsystem_name ();
390 ptrdiff_t filenamesize = (homelen + sizeof xdefaults
391 + SBYTES (system_name));
392 p = filename = xrealloc (home, filenamesize);
393 lispstpcpy (stpcpy (filename + homelen, xdefaults), system_name);
396 db = XrmGetFileDatabase (p);
398 xfree (filename);
400 return db;
403 /* External interface. */
405 /* Types of values that we can find in a database */
407 #define XrmStringType "String" /* String representation */
408 static XrmRepresentation x_rm_string; /* Quark representation */
410 /* Load X resources based on the display and a possible -xrm option. */
412 XrmDatabase
413 x_load_resources (Display *display, const char *xrm_string,
414 const char *myname, const char *myclass)
416 XrmDatabase user_database;
417 XrmDatabase rdb;
418 XrmDatabase db;
419 char line[256];
421 #if defined USE_MOTIF || !defined HAVE_XFT || !defined USE_LUCID
422 const char *helv = "-*-helvetica-medium-r-*--*-120-*-*-*-*-iso8859-1";
423 #endif
425 #ifdef USE_MOTIF
426 const char *courier = "-*-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1";
427 #endif
429 x_rm_string = XrmStringToQuark (XrmStringType);
430 #ifndef USE_X_TOOLKIT
431 /* pmr@osf.org says this shouldn't be done if USE_X_TOOLKIT.
432 I suspect it's because the toolkit version does this elsewhere. */
433 XrmInitialize ();
434 #endif
435 rdb = XrmGetStringDatabase ("");
437 /* Add some font defaults. If the font `helv' doesn't exist, widgets
438 will use some other default font. */
439 #ifdef USE_MOTIF
441 sprintf (line, "%s.pane.background: grey75", myclass);
442 XrmPutLineResource (&rdb, line);
443 sprintf (line, "%s*fontList: %s", myclass, helv);
444 XrmPutLineResource (&rdb, line);
445 sprintf (line, "%s*menu*background: grey75", myclass);
446 XrmPutLineResource (&rdb, line);
447 sprintf (line, "%s*menubar*background: grey75", myclass);
448 XrmPutLineResource (&rdb, line);
449 sprintf (line, "%s*verticalScrollBar.background: grey75", myclass);
450 XrmPutLineResource (&rdb, line);
451 sprintf (line, "%s*verticalScrollBar.troughColor: grey75", myclass);
452 XrmPutLineResource (&rdb, line);
453 sprintf (line, "%s*horizontalScrollBar.background: grey75", myclass);
454 XrmPutLineResource (&rdb, line);
455 sprintf (line, "%s*horizontalScrollBar.troughColor: grey75", myclass);
456 XrmPutLineResource (&rdb, line);
457 sprintf (line, "%s.dialog*.background: grey75", myclass);
458 XrmPutLineResource (&rdb, line);
459 sprintf (line, "%s*fsb.Text.background: white", myclass);
460 XrmPutLineResource (&rdb, line);
461 sprintf (line, "%s*fsb.FilterText.background: white", myclass);
462 XrmPutLineResource (&rdb, line);
463 sprintf (line, "%s*fsb*DirList.background: white", myclass);
464 XrmPutLineResource (&rdb, line);
465 sprintf (line, "%s*fsb*ItemsList.background: white", myclass);
466 XrmPutLineResource (&rdb, line);
467 sprintf (line, "%s*fsb*background: grey75", myclass);
468 XrmPutLineResource (&rdb, line);
469 sprintf (line, "%s*fsb.Text.fontList: %s", myclass, courier);
470 XrmPutLineResource (&rdb, line);
471 sprintf (line, "%s*fsb.FilterText.fontList: %s", myclass, courier);
472 XrmPutLineResource (&rdb, line);
473 sprintf (line, "%s*fsb*ItemsList.fontList: %s", myclass, courier);
474 XrmPutLineResource (&rdb, line);
475 sprintf (line, "%s*fsb*DirList.fontList: %s", myclass, courier);
476 XrmPutLineResource (&rdb, line);
478 /* Set double click time of list boxes in the file selection
479 dialog from `double-click-time'. */
480 if (INTEGERP (Vdouble_click_time) && XINT (Vdouble_click_time) > 0)
482 sprintf (line, "%s*fsb*DirList.doubleClickInterval: %"pI"d",
483 myclass, XFASTINT (Vdouble_click_time));
484 XrmPutLineResource (&rdb, line);
485 sprintf (line, "%s*fsb*ItemsList.doubleClickInterval: %"pI"d",
486 myclass, XFASTINT (Vdouble_click_time));
487 XrmPutLineResource (&rdb, line);
490 #else /* not USE_MOTIF */
492 sprintf (line, "Emacs.dialog*.background: grey75");
493 XrmPutLineResource (&rdb, line);
494 #if !defined (HAVE_XFT) || !defined (USE_LUCID)
495 sprintf (line, "Emacs.dialog*.font: %s", helv);
496 XrmPutLineResource (&rdb, line);
497 sprintf (line, "*XlwMenu*font: %s", helv);
498 XrmPutLineResource (&rdb, line);
499 #endif
500 sprintf (line, "*XlwMenu*background: grey75");
501 XrmPutLineResource (&rdb, line);
502 sprintf (line, "Emacs*verticalScrollBar.background: grey75");
503 XrmPutLineResource (&rdb, line);
504 sprintf (line, "Emacs*horizontalScrollBar.background: grey75");
505 XrmPutLineResource (&rdb, line);
507 #endif /* not USE_MOTIF */
509 user_database = get_user_db (display);
511 /* Figure out what the "customization string" is, so we can use it
512 to decode paths. */
513 xfree (x_customization_string);
514 x_customization_string
515 = x_get_customization_string (user_database, myname, myclass);
517 /* Get application system defaults */
518 db = get_system_app (myclass);
519 if (db != NULL)
520 XrmMergeDatabases (db, &rdb);
522 /* Get Fallback resources */
523 db = get_fallback (display);
524 if (db != NULL)
525 XrmMergeDatabases (db, &rdb);
527 /* Get application user defaults */
528 db = get_user_app (myclass);
529 if (db != NULL)
530 XrmMergeDatabases (db, &rdb);
532 /* get User defaults */
533 if (user_database != NULL)
534 XrmMergeDatabases (user_database, &rdb);
536 /* Get Environment defaults. */
537 db = get_environ_db ();
538 if (db != NULL)
539 XrmMergeDatabases (db, &rdb);
541 /* Last, merge in any specification from the command line. */
542 if (xrm_string != NULL)
544 db = XrmGetStringDatabase (xrm_string);
545 if (db != NULL)
546 XrmMergeDatabases (db, &rdb);
549 return rdb;
553 /* Retrieve the value of the resource specified by NAME with class CLASS
554 and of type TYPE from database RDB. The value is returned in RET_VALUE. */
556 static int
557 x_get_resource (XrmDatabase rdb, const char *name, const char *class,
558 XrmRepresentation expected_type, XrmValue *ret_value)
560 XrmValue value;
561 XrmName namelist[100];
562 XrmClass classlist[100];
563 XrmRepresentation type;
565 XrmStringToNameList (name, namelist);
566 XrmStringToClassList (class, classlist);
568 if (XrmQGetResource (rdb, namelist, classlist, &type, &value) == True
569 && (type == expected_type))
571 if (type == x_rm_string)
572 ret_value->addr = (char *) value.addr;
573 else
574 memcpy (ret_value->addr, value.addr, ret_value->size);
576 return value.size;
579 return 0;
582 /* Retrieve the string resource specified by NAME with CLASS from
583 database RDB. */
585 char *
586 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
588 XrmValue value;
590 if (inhibit_x_resources)
591 /* --quick was passed, so this is a no-op. */
592 return NULL;
594 if (x_get_resource (rdb, name, class, x_rm_string, &value))
595 return (char *) value.addr;
597 return 0;
600 /* Stand-alone test facilities. */
602 #ifdef TESTRM
604 typedef char **List;
605 #define arg_listify(len, list) (list)
606 #define car(list) (*(list))
607 #define cdr(list) (list + 1)
608 #define NIL(list) (! *(list))
609 #define free_arglist(list)
611 static List
612 member (char *elt, List list)
614 List p;
616 for (p = list; ! NIL (p); p = cdr (p))
617 if (! strcmp (elt, car (p)))
618 return p;
620 return p;
623 static void
624 fatal (char *msg, char *prog)
626 fprintf (stderr, msg, prog);
627 exit (1);
631 main (int argc, char **argv)
633 Display *display;
634 char *displayname, *resource_string, *class, *name;
635 XrmDatabase xdb;
636 List arg_list, lp;
638 arg_list = arg_listify (argc, argv);
640 lp = member ("-d", arg_list);
641 if (!NIL (lp))
642 displayname = car (cdr (lp));
643 else
644 displayname = "localhost:0.0";
646 lp = member ("-xrm", arg_list);
647 resource_string = NIL (lp) ? 0 : car (cdr (lp));
649 lp = member ("-c", arg_list);
650 if (! NIL (lp))
651 class = car (cdr (lp));
652 else
653 class = "Emacs";
655 lp = member ("-n", arg_list);
656 if (! NIL (lp))
657 name = car (cdr (lp));
658 else
659 name = "emacs";
661 free_arglist (arg_list);
663 if (!(display = XOpenDisplay (displayname)))
664 fatal ("Can't open display '%s'\n", XDisplayName (displayname));
666 xdb = x_load_resources (display, resource_string, name, class);
668 /* In a real program, you'd want to also do this: */
669 display->db = xdb;
671 while (true)
673 char query_name[90];
674 char query_class[90];
676 printf ("Name: ");
677 gets (query_name);
679 if (strlen (query_name))
681 char *value;
683 printf ("Class: ");
684 gets (query_class);
686 value = x_get_string_resource (xdb, query_name, query_class);
688 if (value != NULL)
689 printf ("\t%s(%s): %s\n\n", query_name, query_class, value);
690 else
691 printf ("\tNo Value.\n\n");
693 else
694 break;
696 printf ("\tExit.\n\n");
698 XCloseDisplay (display);
700 return 0;
702 #endif /* TESTRM */