Convert some more functions to standard C.
[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,
114f9c96 3 2008, 2009, 2010 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"
2cc1905e
GV
65
66static struct entry
67{
68 char *name;
69 char *value;
177c0ea7
JB
70}
71env_vars[] =
2cc1905e 72{
c6e63684 73 {"emacs_dir", NULL},
5488afcc 74 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
0655d4d4 75 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
26b430b7
RS
76 {"EMACSDATA", "%emacs_dir%/etc"},
77 {"EMACSPATH", "%emacs_dir%/bin"},
9296c0e8
GV
78 /* We no longer set INFOPATH because Info-default-directory-list
79 is then ignored. */
80 /* {"INFOPATH", "%emacs_dir%/info"}, */
26b430b7 81 {"EMACSDOC", "%emacs_dir%/etc"},
2cc1905e
GV
82 {"TERM", "cmd"}
83};
84
177c0ea7 85BOOL
7c3320d8 86add_registry (char *path)
2cc1905e
GV
87{
88 HKEY hrootkey = NULL;
2cc1905e
GV
89 int i;
90 BOOL ok = TRUE;
f1cefe09
JR
91 DWORD size;
92
93 /* Record the location of Emacs to the App Paths key if we have
94 sufficient permissions to do so. This helps Windows find emacs quickly
95 if the user types emacs.exe in the "Run Program" dialog without having
96 emacs on their path. Some applications also use the same registry key
97 to discover the installation directory for programs they are looking for.
98 Multiple installations cannot be handled by this method, but it does not
99 affect the general operation of other installations of Emacs, and we
100 are blindly overwriting the Start Menu entries already.
101 */
c44b4b46 102 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
f1cefe09
JR
103 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
104 &hrootkey, NULL) == ERROR_SUCCESS)
105 {
106 int len;
107 char *emacs_path;
108 HKEY gtk_key = NULL;
109
110 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
c44b4b46 111 emacs_path = (char *) alloca (len);
f1cefe09
JR
112 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
113
114 RegSetValueEx (hrootkey, NULL, 0, REG_SZ, emacs_path, len);
115
116 /* Look for a GTK installation. If found, add it to the library search
117 path for Emacs so that the image libraries it provides are available
118 to Emacs regardless of whether it is in the path or not. */
119 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_GTK, REG_OPTION_NON_VOLATILE,
120 KEY_READ, &gtk_key) == ERROR_SUCCESS)
121 {
122 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
123 NULL, &size) == ERROR_SUCCESS)
124 {
125 char *gtk_path = (char *) alloca (size);
126 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
127 gtk_path, &size) == ERROR_SUCCESS)
128 {
129 /* Make sure the emacs bin directory continues to be searched
130 first by including it as well. */
131 char *dll_paths;
132 len = strlen (path) + 5 + size;
133 dll_paths = (char *) alloca (size + strlen (path) + 1);
134 sprintf (dll_paths, "%s\\bin;%s", path, gtk_path);
135 RegSetValueEx (hrootkey, "Path", 0, REG_SZ, dll_paths, len);
136 }
137 }
138 RegCloseKey (gtk_key);
139 }
140 RegCloseKey (hrootkey);
141 }
177c0ea7 142
ebe98f49
JR
143 /* Previous versions relied on registry settings, but we do not need
144 them any more. If registry settings are installed from a previous
145 version, replace them to ensure they are the current settings.
146 Otherwise, do nothing. */
147
177c0ea7 148 /* Check both the current user and the local machine to see if we
2cc1905e 149 have any resources. */
177c0ea7 150
ebe98f49
JR
151 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT,
152 REG_OPTION_NON_VOLATILE,
153 KEY_WRITE, &hrootkey) != ERROR_SUCCESS
154 && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT,
155 REG_OPTION_NON_VOLATILE,
156 KEY_WRITE, &hrootkey) != ERROR_SUCCESS)
2cc1905e
GV
157 {
158 return FALSE;
159 }
177c0ea7
JB
160
161 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
2cc1905e
GV
162 {
163 char * value = env_vars[i].value ? env_vars[i].value : path;
177c0ea7 164
2cc1905e
GV
165 if (RegSetValueEx (hrootkey, env_vars[i].name,
166 0, REG_EXPAND_SZ,
167 value, lstrlen (value) + 1) != ERROR_SUCCESS)
168 ok = FALSE;
177c0ea7
JB
169 }
170
2cc1905e 171 RegCloseKey (hrootkey);
177c0ea7 172
2cc1905e
GV
173 return (ok);
174}
175
176int
7c3320d8 177main (int argc, char *argv[])
cef9e134 178{
84ef4ca2
JR
179 char start_folder[MAX_PATH + 1];
180 int shortcuts_created = 0;
181 int com_available = 1;
65cd6687 182 char modname[MAX_PATH];
9296c0e8 183 char *prog_name;
65cd6687
GV
184 char *emacs_path;
185 char *p;
5205d900 186 int quiet = 0;
84ef4ca2
JR
187 HRESULT result;
188 IShellLinkA *shortcut;
cef9e134 189
65cd6687 190 /* If no args specified, use our location to set emacs_path. */
cef9e134 191
f4c6fac4
JR
192 if (argc > 1
193 && (argv[1][0] == '/' || argv[1][0] == '-')
194 && argv[1][1] == 'q')
5205d900
AI
195 {
196 quiet = 1;
197 --argc;
198 ++argv;
199 }
200
65cd6687
GV
201 if (argc > 1)
202 emacs_path = argv[1];
203 else
204 {
205 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
206 (p = strrchr (modname, '\\')) == NULL)
207 {
208 fprintf (stderr, "fatal error");
209 exit (1);
210 }
211 *p = 0;
212
213 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
214 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
215 {
216 *p = 0;
217 emacs_path = modname;
218 }
219 else
220 {
84ef4ca2 221 fprintf (stderr, "usage: addpm emacs_path\n");
65cd6687
GV
222 exit (1);
223 }
224
225 /* Tell user what we are going to do. */
5205d900
AI
226 if (!quiet)
227 {
228 int result;
229
230 char msg[ MAX_PATH ];
231 sprintf (msg, "Install Emacs at %s?\n", emacs_path);
232 result = MessageBox (NULL, msg, "Install Emacs",
233 MB_OKCANCEL | MB_ICONQUESTION);
234 if (result != IDOK)
235 {
236 fprintf (stderr, "Install cancelled\n");
237 exit (1);
238 }
239 }
65cd6687
GV
240 }
241
5205d900
AI
242 add_registry (emacs_path);
243 prog_name = "runemacs.exe";
2cc1905e 244
84ef4ca2 245 /* Try to install globally. */
cef9e134 246
84ef4ca2
JR
247 if (!SUCCEEDED (CoInitialize (NULL))
248 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
249 CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
250 (void **) &shortcut)))
251 {
252 com_available = 0;
253 }
cef9e134 254
84ef4ca2
JR
255 if (com_available
256 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
cef9e134 257 {
84ef4ca2
JR
258 if (strlen (start_folder) < (MAX_PATH - 20))
259 {
260 BOOL retval;
261
262 strcat (start_folder, "\\Gnu Emacs");
263 if (CreateDirectory (start_folder, NULL)
264 || GetLastError () == ERROR_ALREADY_EXISTS)
265 {
266 char full_emacs_path[MAX_PATH + 1];
267 IPersistFile *lnk;
268 strcat (start_folder, "\\Emacs.lnk");
269 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
270 IShellLinkA_SetPath (shortcut, full_emacs_path);
271 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
272 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
273 (void **) &lnk);
274 if (SUCCEEDED (result))
275 {
276 wchar_t unicode_path[MAX_PATH];
277 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
278 unicode_path, MAX_PATH);
279 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
280 shortcuts_created = 1;
281 IPersistFile_Release (lnk);
282 }
283 }
284 }
285 }
286
287 if (!shortcuts_created && com_available
288 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
289 {
290 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
291 if (strlen (start_folder) < (MAX_PATH - 20))
292 {
293 BOOL retval;
6fbcbee7 294
84ef4ca2
JR
295 strcat (start_folder, "\\Gnu Emacs");
296 if (CreateDirectory (start_folder, NULL)
297 || GetLastError () == ERROR_ALREADY_EXISTS)
298 {
299 char full_emacs_path[MAX_PATH + 1];
300 IPersistFile *lnk;
301 strcat (start_folder, "\\Emacs.lnk");
302 sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
303 IShellLinkA_SetPath (shortcut, full_emacs_path);
304 IShellLinkA_SetDescription (shortcut, "GNU Emacs");
305 result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
306 (void **) &lnk);
307 if (SUCCEEDED (result))
308 {
309 wchar_t unicode_path[MAX_PATH];
310 MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
311 unicode_path, MAX_PATH);
312 if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
313 shortcuts_created = 1;
314 IPersistFile_Release (lnk);
7c3320d8 315
84ef4ca2
JR
316 }
317 }
7c3320d8 318 }
cef9e134
GV
319 }
320
84ef4ca2
JR
321 if (com_available)
322 IShellLinkA_Release (shortcut);
323
324 /* Need to call uninitialize, even if ComInitialize failed. */
325 CoUninitialize ();
326
327 /* Fallback on old DDE method if the above failed. */
328 if (!shortcuts_created)
329 {
330 DWORD dde = 0;
331 HCONV conversation;
332 HSZ progman;
333 char add_item[MAX_PATH*2 + 100];
334
335 DdeInitialize (&dde, (PFNCALLBACK) DdeCallback, APPCMD_CLIENTONLY, 0);
336 progman = DdeCreateStringHandle (dde, "PROGMAN", CP_WINANSI);
337 conversation = DdeConnect (dde, progman, progman, NULL);
338 if (conversation)
339 {
340 DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
341 DdeCommand ("[ReplaceItem (Emacs)]");
342 sprintf (add_item, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
343 emacs_path, prog_name);
344 DdeCommand (add_item);
345
346 DdeDisconnect (conversation);
347 }
6fbcbee7 348
84ef4ca2
JR
349 DdeFreeStringHandle (dde, progman);
350 DdeUninitialize (dde);
351 }
cef9e134 352
84ef4ca2 353 return 0;
cef9e134 354}
ab5796a9
MB
355
356/* arch-tag: f923609d-b781-4ef4-abce-ca0da29cbbf0
357 (do not change this comment) */