Fix more incompatibilities between MinGW.org and MinGW64 headers
[bpt/emacs.git] / nt / addpm.c
CommitLineData
cef9e134 1/* Add entries to the GNU Emacs Program Manager folder.
ab422c4d 2 Copyright (C) 1995, 2001-2013 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. */
b88b62de
EZ
37/* MinGW64 defines _W64 and barfs if _WIN32_IE is defined to anything
38 below 0x500. */
39#ifndef _W64
84ef4ca2 40#define _WIN32_IE 0x400
b88b62de 41#endif
84ef4ca2
JR
42/* Request C Object macros for COM interfaces. */
43#define COBJMACROS 1
44
6fbcbee7 45#include <windows.h>
84ef4ca2 46#include <shlobj.h>
6fbcbee7
RS
47#include <ddeml.h>
48#include <stdlib.h>
49#include <stdio.h>
c44b4b46 50#include <malloc.h>
cef9e134 51
177c0ea7 52HDDEDATA CALLBACK
2cc1905e
GV
53DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
54 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
55 DWORD dwData1, DWORD dwData2)
6fbcbee7 56{
2cc1905e 57 return ((HDDEDATA) NULL);
6fbcbee7 58}
cef9e134 59
6fbcbee7 60#define DdeCommand(str) \
84ef4ca2 61 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
6fbcbee7 62 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
cef9e134 63
c6e63684 64#define REG_ROOT "SOFTWARE\\GNU\\Emacs"
f1cefe09
JR
65#define REG_GTK "SOFTWARE\\GTK\\2.0"
66#define REG_APP_PATH \
67 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
856a6b77
JR
68#define REG_RUNEMACS_PATH \
69 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\runemacs.exe"
2cc1905e
GV
70
71static struct entry
72{
0597ab06
JB
73 const char *name;
74 const char *value;
177c0ea7
JB
75}
76env_vars[] =
2cc1905e 77{
c6e63684 78 {"emacs_dir", NULL},
5488afcc 79 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
0655d4d4 80 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
26b430b7
RS
81 {"EMACSDATA", "%emacs_dir%/etc"},
82 {"EMACSPATH", "%emacs_dir%/bin"},
9296c0e8
GV
83 /* We no longer set INFOPATH because Info-default-directory-list
84 is then ignored. */
85 /* {"INFOPATH", "%emacs_dir%/info"}, */
26b430b7 86 {"EMACSDOC", "%emacs_dir%/etc"},
2cc1905e
GV
87 {"TERM", "cmd"}
88};
89
177c0ea7 90BOOL
0597ab06 91add_registry (const char *path)
2cc1905e
GV
92{
93 HKEY hrootkey = NULL;
2cc1905e
GV
94 int i;
95 BOOL ok = TRUE;
f1cefe09
JR
96 DWORD size;
97
98 /* Record the location of Emacs to the App Paths key if we have
99 sufficient permissions to do so. This helps Windows find emacs quickly
100 if the user types emacs.exe in the "Run Program" dialog without having
101 emacs on their path. Some applications also use the same registry key
102 to discover the installation directory for programs they are looking for.
103 Multiple installations cannot be handled by this method, but it does not
104 affect the general operation of other installations of Emacs, and we
105 are blindly overwriting the Start Menu entries already.
106 */
c44b4b46 107 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
f1cefe09
JR
108 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
109 &hrootkey, NULL) == ERROR_SUCCESS)
110 {
111 int len;
112 char *emacs_path;
113 HKEY gtk_key = NULL;
114
115 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
c44b4b46 116 emacs_path = (char *) alloca (len);
f1cefe09
JR
117 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
118
926cd98c 119 RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
f1cefe09
JR
120
121 /* Look for a GTK installation. If found, add it to the library search
122 path for Emacs so that the image libraries it provides are available
123 to Emacs regardless of whether it is in the path or not. */
124 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_GTK, REG_OPTION_NON_VOLATILE,
125 KEY_READ, &gtk_key) == ERROR_SUCCESS)
126 {
127 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
128 NULL, &size) == ERROR_SUCCESS)
129 {
130 char *gtk_path = (char *) alloca (size);
131 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
132 gtk_path, &size) == ERROR_SUCCESS)
133 {
134 /* Make sure the emacs bin directory continues to be searched
135 first by including it as well. */
136 char *dll_paths;
856a6b77 137 HKEY runemacs_key = NULL;
f1cefe09
JR
138 len = strlen (path) + 5 + size;
139 dll_paths = (char *) alloca (size + strlen (path) + 1);
140 sprintf (dll_paths, "%s\\bin;%s", path, gtk_path);
926cd98c
JB
141 RegSetValueEx (hrootkey, "Path", 0, REG_EXPAND_SZ,
142 dll_paths, len);
856a6b77
JR
143
144 /* Set the same path for runemacs.exe, as the Explorer shell
145 looks this up, so the above does not take effect when
146 emacs.exe is spawned from runemacs.exe. */
147 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_RUNEMACS_PATH,
148 0, "", REG_OPTION_NON_VOLATILE,
149 KEY_WRITE, NULL, &runemacs_key, NULL)
150 == ERROR_SUCCESS)
151 {
926cd98c 152 RegSetValueEx (runemacs_key, "Path", 0, REG_EXPAND_SZ,
856a6b77
JR
153 dll_paths, len);
154
155 RegCloseKey (runemacs_key);
156 }
f1cefe09
JR
157 }
158 }
159 RegCloseKey (gtk_key);
160 }
161 RegCloseKey (hrootkey);
162 }
177c0ea7 163
ebe98f49
JR
164 /* Previous versions relied on registry settings, but we do not need
165 them any more. If registry settings are installed from a previous
166 version, replace them to ensure they are the current settings.
167 Otherwise, do nothing. */
168
177c0ea7 169 /* Check both the current user and the local machine to see if we
2cc1905e 170 have any resources. */
177c0ea7 171
ebe98f49
JR
172 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT,
173 REG_OPTION_NON_VOLATILE,
174 KEY_WRITE, &hrootkey) != ERROR_SUCCESS
175 && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT,
176 REG_OPTION_NON_VOLATILE,
177 KEY_WRITE, &hrootkey) != ERROR_SUCCESS)
2cc1905e
GV
178 {
179 return FALSE;
180 }
177c0ea7
JB
181
182 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
2cc1905e 183 {
0597ab06 184 const char * value = env_vars[i].value ? env_vars[i].value : path;
177c0ea7 185
2cc1905e
GV
186 if (RegSetValueEx (hrootkey, env_vars[i].name,
187 0, REG_EXPAND_SZ,
188 value, lstrlen (value) + 1) != ERROR_SUCCESS)
189 ok = FALSE;
177c0ea7
JB
190 }
191
2cc1905e 192 RegCloseKey (hrootkey);
177c0ea7 193
2cc1905e
GV
194 return (ok);
195}
196
197int
7c3320d8 198main (int argc, char *argv[])
cef9e134 199{
84ef4ca2
JR
200 char start_folder[MAX_PATH + 1];
201 int shortcuts_created = 0;
202 int com_available = 1;
65cd6687 203 char modname[MAX_PATH];
0597ab06
JB
204 const char *prog_name;
205 const char *emacs_path;
65cd6687 206 char *p;
5205d900 207 int quiet = 0;
84ef4ca2
JR
208 HRESULT result;
209 IShellLinkA *shortcut;
cef9e134 210
65cd6687 211 /* If no args specified, use our location to set emacs_path. */
cef9e134 212
f4c6fac4
JR
213 if (argc > 1
214 && (argv[1][0] == '/' || argv[1][0] == '-')
215 && argv[1][1] == 'q')
5205d900
AI
216 {
217 quiet = 1;
218 --argc;
219 ++argv;
220 }
221
65cd6687
GV
222 if (argc > 1)
223 emacs_path = argv[1];
224 else
225 {
226 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
227 (p = strrchr (modname, '\\')) == NULL)
228 {
229 fprintf (stderr, "fatal error");
230 exit (1);
231 }
232 *p = 0;
233
234 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
235 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
236 {
237 *p = 0;
238 emacs_path = modname;
239 }
240 else
241 {
84ef4ca2 242 fprintf (stderr, "usage: addpm emacs_path\n");
65cd6687
GV
243 exit (1);
244 }
245
246 /* Tell user what we are going to do. */
5205d900
AI
247 if (!quiet)
248 {
249 int result;
250
251 char msg[ MAX_PATH ];
252 sprintf (msg, "Install Emacs at %s?\n", emacs_path);
253 result = MessageBox (NULL, msg, "Install Emacs",
254 MB_OKCANCEL | MB_ICONQUESTION);
255 if (result != IDOK)
256 {
c80e3b4a 257 fprintf (stderr, "Install canceled\n");
5205d900
AI
258 exit (1);
259 }
260 }
65cd6687
GV
261 }
262
5205d900
AI
263 add_registry (emacs_path);
264 prog_name = "runemacs.exe";
2cc1905e 265
84ef4ca2 266 /* Try to install globally. */
cef9e134 267
84ef4ca2
JR
268 if (!SUCCEEDED (CoInitialize (NULL))
269 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
270 CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
271 (void **) &shortcut)))
272 {
273 com_available = 0;
274 }
cef9e134 275
84ef4ca2
JR
276 if (com_available
277 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
cef9e134 278 {
84ef4ca2
JR
279 if (strlen (start_folder) < (MAX_PATH - 20))
280 {
84ef4ca2
JR
281 strcat (start_folder, "\\Gnu Emacs");
282 if (CreateDirectory (start_folder, NULL)
283 || GetLastError () == ERROR_ALREADY_EXISTS)
284 {
285 char full_emacs_path[MAX_PATH + 1];
286 IPersistFile *lnk;
287 strcat (start_folder, "\\Emacs.lnk");
288 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
289 IShellLinkA_SetPath (shortcut, full_emacs_path);
290 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
291 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
292 (void **) &lnk);
293 if (SUCCEEDED (result))
294 {
295 wchar_t unicode_path[MAX_PATH];
296 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
297 unicode_path, MAX_PATH);
298 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
299 shortcuts_created = 1;
300 IPersistFile_Release (lnk);
301 }
302 }
303 }
304 }
305
306 if (!shortcuts_created && com_available
307 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
308 {
309 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
310 if (strlen (start_folder) < (MAX_PATH - 20))
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}