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