]> code.delx.au - gnu-emacs/blob - nt/addpm.c
addpm.c: Replace existing entries, but do not create new ones
[gnu-emacs] / nt / addpm.c
1 /* Add entries to the GNU Emacs Program Manager folder.
2 Copyright (C) 1995, 2001-2015 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 /****************************************************************************
20 *
21 * Program: addpm (adds emacs to the Windows program manager)
22 *
23 * Usage:
24 * argv[1] = install path for emacs
25 *
26 * argv[2] used to be an optional argument for setting the icon.
27 * But now Emacs has a professional looking icon of its own.
28 * If users really want to change it, they can go into the settings of
29 * the shortcut that is created and do it there.
30 */
31
32 /* Use parts of shell API that were introduced by the merge of IE4
33 into the desktop shell. If Windows 95 or NT4 users do not have IE4
34 installed, then the DDE fallback for creating icons the Windows 3.1
35 progman way will be used instead, but that is prone to lockups
36 caused by other applications not servicing their message queues. */
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <malloc.h>
40
41 /* MinGW64 barfs if _WIN32_IE is defined to anything below 0x500. */
42 #ifndef MINGW_W64
43 #define _WIN32_IE 0x400
44 #endif
45 /* Request C Object macros for COM interfaces. */
46 #define COBJMACROS 1
47
48 #include <windows.h>
49 #include <shlobj.h>
50 #include <ddeml.h>
51
52 #ifndef OLD_PATHS
53 #include "../src/epaths.h"
54 #endif
55
56 HDDEDATA CALLBACK
57 DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
58 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
59 DWORD dwData1, DWORD dwData2)
60 {
61 return ((HDDEDATA) NULL);
62 }
63
64 #define DdeCommand(str) \
65 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
66 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
67
68 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
69 #define REG_GTK "SOFTWARE\\GTK\\2.0"
70 #define REG_APP_PATH \
71 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
72 #define REG_RUNEMACS_PATH \
73 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\runemacs.exe"
74
75 static struct entry
76 {
77 const char *name;
78 const char *value;
79 }
80 env_vars[] =
81 {
82 #ifdef OLD_PATHS
83 {"emacs_dir", NULL},
84 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp"},
85 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
86 {"EMACSDATA", "%emacs_dir%/etc"},
87 {"EMACSPATH", "%emacs_dir%/bin"},
88 /* We no longer set INFOPATH because Info-default-directory-list
89 is then ignored. */
90 /* {"INFOPATH", "%emacs_dir%/info"}, */
91 {"EMACSDOC", "%emacs_dir%/etc"},
92 {"TERM", "cmd"}
93 #else /* !OLD_PATHS */
94 {"emacs_dir", NULL},
95 {"EMACSLOADPATH", PATH_SITELOADSEARCH ";" PATH_LOADSEARCH},
96 {"SHELL", PATH_EXEC "/cmdproxy.exe"},
97 {"EMACSDATA", PATH_DATA},
98 {"EMACSPATH", PATH_EXEC},
99 /* We no longer set INFOPATH because Info-default-directory-list
100 is then ignored. */
101 /* {"INFOPATH", "%emacs_dir%/info"}, */
102 {"EMACSDOC", PATH_DOC},
103 {"TERM", "cmd"}
104 #endif
105 };
106
107 void
108 add_registry (const char *path)
109 {
110 HKEY hrootkey = NULL;
111 int i;
112 DWORD size;
113
114 /* Record the location of Emacs to the App Paths key if we have
115 sufficient permissions to do so. This helps Windows find emacs quickly
116 if the user types emacs.exe in the "Run Program" dialog without having
117 emacs on their path. Some applications also use the same registry key
118 to discover the installation directory for programs they are looking for.
119 Multiple installations cannot be handled by this method, but it does not
120 affect the general operation of other installations of Emacs, and we
121 are blindly overwriting the Start Menu entries already.
122 */
123 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
124 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
125 &hrootkey, NULL) == ERROR_SUCCESS)
126 {
127 int len;
128 char *emacs_path;
129 HKEY gtk_key = NULL;
130
131 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
132 emacs_path = (char *) alloca (len);
133 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
134
135 RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
136
137 /* Look for a GTK installation. If found, add it to the library search
138 path for Emacs so that the image libraries it provides are available
139 to Emacs regardless of whether it is in the path or not. */
140 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_GTK, 0,
141 KEY_READ, &gtk_key) == ERROR_SUCCESS)
142 {
143 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
144 NULL, &size) == ERROR_SUCCESS)
145 {
146 char *gtk_path = (char *) alloca (size);
147 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
148 gtk_path, &size) == ERROR_SUCCESS)
149 {
150 /* Make sure the emacs bin directory continues to be searched
151 first by including it as well. */
152 char *dll_paths;
153 HKEY runemacs_key = NULL;
154 len = strlen (path) + 5 + size;
155 dll_paths = (char *) alloca (size + strlen (path) + 1);
156 sprintf (dll_paths, "%s\\bin;%s", path, gtk_path);
157 RegSetValueEx (hrootkey, "Path", 0, REG_EXPAND_SZ,
158 dll_paths, len);
159
160 /* Set the same path for runemacs.exe, as the Explorer shell
161 looks this up, so the above does not take effect when
162 emacs.exe is spawned from runemacs.exe. */
163 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_RUNEMACS_PATH,
164 0, "", REG_OPTION_NON_VOLATILE,
165 KEY_WRITE, NULL, &runemacs_key, NULL)
166 == ERROR_SUCCESS)
167 {
168 RegSetValueEx (runemacs_key, "Path", 0, REG_EXPAND_SZ,
169 dll_paths, len);
170
171 RegCloseKey (runemacs_key);
172 }
173 }
174 }
175 RegCloseKey (gtk_key);
176 }
177 RegCloseKey (hrootkey);
178 }
179
180 /* Previous versions relied on registry settings, but we do not need
181 them any more. If registry settings are installed from a previous
182 version, replace them to ensure they are the current settings.
183 Otherwise, do nothing. */
184
185 /* Check both the current user and the local machine to see if we
186 have any resources. */
187
188 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0,
189 KEY_WRITE | KEY_QUERY_VALUE, &hrootkey) != ERROR_SUCCESS
190 && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0,
191 KEY_WRITE | KEY_QUERY_VALUE, &hrootkey) != ERROR_SUCCESS)
192 return;
193
194 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
195 {
196 const char * value = env_vars[i].value ? env_vars[i].value : path;
197
198 /* Replace only those settings that already exist. */
199 if (RegQueryValueEx (hrootkey, env_vars[i].name, NULL,
200 NULL, NULL, NULL) == ERROR_SUCCESS)
201 RegSetValueEx (hrootkey, env_vars[i].name, 0, REG_EXPAND_SZ,
202 value, lstrlen (value) + 1);
203 }
204
205 RegCloseKey (hrootkey);
206 }
207
208 int
209 main (int argc, char *argv[])
210 {
211 char start_folder[MAX_PATH + 1];
212 int shortcuts_created = 0;
213 int com_available = 1;
214 char modname[MAX_PATH];
215 const char *prog_name;
216 const char *emacs_path;
217 char *p;
218 int quiet = 0;
219 HRESULT result;
220 IShellLinkA *shortcut;
221
222 /* If no args specified, use our location to set emacs_path. */
223
224 if (argc > 1
225 && (argv[1][0] == '/' || argv[1][0] == '-')
226 && argv[1][1] == 'q')
227 {
228 quiet = 1;
229 --argc;
230 ++argv;
231 }
232
233 if (argc > 1)
234 emacs_path = argv[1];
235 else
236 {
237 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
238 (p = strrchr (modname, '\\')) == NULL)
239 {
240 fprintf (stderr, "fatal error");
241 exit (1);
242 }
243 *p = 0;
244
245 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
246 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
247 {
248 *p = 0;
249 emacs_path = modname;
250 }
251 else
252 {
253 fprintf (stderr, "usage: addpm emacs_path\n");
254 exit (1);
255 }
256
257 /* Tell user what we are going to do. */
258 if (!quiet)
259 {
260 int result;
261
262 char msg[ MAX_PATH ];
263 sprintf (msg, "Install Emacs at %s?\n", emacs_path);
264 result = MessageBox (NULL, msg, "Install Emacs",
265 MB_OKCANCEL | MB_ICONQUESTION);
266 if (result != IDOK)
267 {
268 fprintf (stderr, "Install canceled\n");
269 exit (1);
270 }
271 }
272 }
273
274 add_registry (emacs_path);
275 prog_name = "runemacs.exe";
276
277 /* Try to install globally. */
278
279 if (!SUCCEEDED (CoInitialize (NULL))
280 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
281 CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
282 (void **) &shortcut)))
283 {
284 com_available = 0;
285 }
286
287 if (com_available
288 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
289 {
290 if (strlen (start_folder) < (MAX_PATH - 20))
291 {
292 strcat (start_folder, "\\Gnu Emacs");
293 if (CreateDirectory (start_folder, NULL)
294 || GetLastError () == ERROR_ALREADY_EXISTS)
295 {
296 char full_emacs_path[MAX_PATH + 1];
297 IPersistFile *lnk;
298 strcat (start_folder, "\\Emacs.lnk");
299 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
300 IShellLinkA_SetPath (shortcut, full_emacs_path);
301 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
302 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
303 (void **) &lnk);
304 if (SUCCEEDED (result))
305 {
306 wchar_t unicode_path[MAX_PATH];
307 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
308 unicode_path, MAX_PATH);
309 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
310 shortcuts_created = 1;
311 IPersistFile_Release (lnk);
312 }
313 }
314 }
315 }
316
317 if (!shortcuts_created && com_available
318 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
319 {
320 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
321 if (strlen (start_folder) < (MAX_PATH - 20))
322 {
323 strcat (start_folder, "\\Gnu Emacs");
324 if (CreateDirectory (start_folder, NULL)
325 || GetLastError () == ERROR_ALREADY_EXISTS)
326 {
327 char full_emacs_path[MAX_PATH + 1];
328 IPersistFile *lnk;
329 strcat (start_folder, "\\Emacs.lnk");
330 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
331 IShellLinkA_SetPath (shortcut, full_emacs_path);
332 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
333 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
334 (void **) &lnk);
335 if (SUCCEEDED (result))
336 {
337 wchar_t unicode_path[MAX_PATH];
338 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
339 unicode_path, MAX_PATH);
340 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
341 shortcuts_created = 1;
342 IPersistFile_Release (lnk);
343
344 }
345 }
346 }
347 }
348
349 if (com_available)
350 IShellLinkA_Release (shortcut);
351
352 /* Need to call uninitialize, even if ComInitialize failed. */
353 CoUninitialize ();
354
355 /* Fallback on old DDE method if the above failed. */
356 if (!shortcuts_created)
357 {
358 DWORD dde = 0;
359 HCONV conversation;
360 HSZ progman;
361 char add_item[MAX_PATH*2 + 100];
362
363 DdeInitialize (&dde, (PFNCALLBACK) DdeCallback, APPCMD_CLIENTONLY, 0);
364 progman = DdeCreateStringHandle (dde, "PROGMAN", CP_WINANSI);
365 conversation = DdeConnect (dde, progman, progman, NULL);
366 if (conversation)
367 {
368 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
369 DdeCommand ("[ReplaceItem (Emacs)]");
370 sprintf (add_item, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
371 emacs_path, prog_name);
372 DdeCommand (add_item);
373
374 DdeDisconnect (conversation);
375 }
376
377 DdeFreeStringHandle (dde, progman);
378 DdeUninitialize (dde);
379 }
380
381 return 0;
382 }