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