Fix minor problems with loaddefs autogeneration
[emacs.git] / src / w32reg.c
blobde19ae14858b123ac18ffab88dd289fc4dc50f02
1 /* Emulate the X Resource Manager through the registry.
2 Copyright (C) 1990, 1993-1994, 2001-2017 Free Software Foundation,
3 Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Written by Kevin Gallo */
22 #include <config.h>
23 #include "lisp.h"
24 #include "w32term.h" /* for XrmDatabase, xrdb */
25 #include "blockinput.h"
27 #include <stdio.h>
29 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
31 /* Default system colors from the Display Control Panel settings. */
32 #define SYSTEM_DEFAULT_RESOURCES \
33 "emacs.foreground:SystemWindowText\0" \
34 "emacs.background:SystemWindow\0" \
35 "emacs.tooltip.attributeForeground:SystemInfoText\0" \
36 "emacs.tooltip.attributeBackground:SystemInfoWindow\0" \
37 "emacs.tool-bar.attributeForeground:SystemButtonText\0" \
38 "emacs.tool-bar.attributeBackground:SystemButtonFace\0" \
39 "emacs.menu.attributeForeground:SystemMenuText\0" \
40 "emacs.menu.attributeBackground:SystemMenu\0" \
41 "emacs.scroll-bar.attributeForeground:SystemScrollbar\0"
43 /* Other possibilities for default faces:
45 region: Could use SystemHilight, but interferes with our ability to
46 see most syntax highlighting through the region face.
48 modeline: Could use System(In)ActiveTitle, gradient versions (not
49 supported on 95 and NT), but modeline is more like a status bar
50 really (which don't appear to be configurable in Windows).
52 highlight: Could use SystemHotTrackingColor, but it is not supported
53 on Windows 95 or NT, and other apps only seem to use it for menus
54 anyway.
58 static char *
59 w32_get_rdb_resource (const char *rdb, const char *resource)
61 char *value = (char *)rdb;
62 int len = strlen (resource);
64 while (*value)
66 /* Comparison is case-insensitive because registry searches are too. */
67 if ((strnicmp (value, resource, len) == 0) && (value[len] == ':'))
68 return xstrdup (&value[len + 1]);
70 value = strchr (value, '\0') + 1;
73 return NULL;
76 static char *
77 w32_get_string_resource (const char *name, const char *class, DWORD dwexptype)
79 LPBYTE lpvalue = NULL;
80 HKEY hrootkey = NULL;
81 DWORD dwType;
82 DWORD cbData;
83 BOOL ok = FALSE;
84 HKEY hive = HKEY_CURRENT_USER;
86 trykey:
88 block_input ();
90 /* Check both the current user and the local machine to see if we have
91 any resources */
93 if (RegOpenKeyEx (hive, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
95 const char *keyname;
97 if (RegQueryValueEx (hrootkey, name, NULL, &dwType, NULL, &cbData) == ERROR_SUCCESS
98 && dwType == dwexptype)
100 keyname = name;
102 else if (RegQueryValueEx (hrootkey, class, NULL, &dwType, NULL, &cbData) == ERROR_SUCCESS
103 && dwType == dwexptype)
105 keyname = class;
107 else
109 keyname = NULL;
112 ok = (keyname
113 && (lpvalue = xmalloc (cbData)) != NULL
114 && RegQueryValueEx (hrootkey, keyname, NULL, NULL, lpvalue, &cbData) == ERROR_SUCCESS);
116 RegCloseKey (hrootkey);
119 unblock_input ();
121 if (!ok)
123 if (lpvalue)
125 xfree (lpvalue);
126 lpvalue = NULL;
128 if (hive == HKEY_CURRENT_USER)
130 hive = HKEY_LOCAL_MACHINE;
131 goto trykey;
134 /* Check if there are Windows specific defaults defined. */
135 return w32_get_rdb_resource (SYSTEM_DEFAULT_RESOURCES, name);
137 return (char *)lpvalue;
140 /* Retrieve the string resource specified by NAME with CLASS from
141 database RDB. */
143 char *
144 x_get_string_resource (XrmDatabase rdb, const char *name, const char *class)
146 if (rdb)
148 char *resource;
150 if ((resource = w32_get_rdb_resource (rdb, name)))
151 return resource;
152 if ((resource = w32_get_rdb_resource (rdb, class)))
153 return resource;
156 if (inhibit_x_resources)
157 /* --quick was passed, so this is a no-op. */
158 return NULL;
160 return w32_get_string_resource (name, class, REG_SZ);