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