Merge from emacs-23 branch, up to 2010-05-20T22:16:19Z!juri@jurta.org.
[bpt/emacs.git] / nt / addpm.c
1 /* Add entries to the GNU Emacs Program Manager folder.
2 Copyright (C) 1995, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /****************************************************************************
21 *
22 * Program: addpm (adds emacs to the Windows program manager)
23 *
24 * Usage:
25 * argv[1] = install path for emacs
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.
31 */
32
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
42 #include <windows.h>
43 #include <shlobj.h>
44 #include <ddeml.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <malloc.h>
48
49 HDDEDATA CALLBACK
50 DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
51 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
52 DWORD dwData1, DWORD dwData2)
53 {
54 return ((HDDEDATA) NULL);
55 }
56
57 #define DdeCommand(str) \
58 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
59 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
60
61 #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
62 #define REG_GTK "SOFTWARE\\GTK\\2.0"
63 #define REG_APP_PATH \
64 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
65 #define REG_RUNEMACS_PATH \
66 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\runemacs.exe"
67
68 static struct entry
69 {
70 const char *name;
71 const char *value;
72 }
73 env_vars[] =
74 {
75 {"emacs_dir", NULL},
76 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
77 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
78 {"EMACSDATA", "%emacs_dir%/etc"},
79 {"EMACSPATH", "%emacs_dir%/bin"},
80 /* We no longer set INFOPATH because Info-default-directory-list
81 is then ignored. */
82 /* {"INFOPATH", "%emacs_dir%/info"}, */
83 {"EMACSDOC", "%emacs_dir%/etc"},
84 {"TERM", "cmd"}
85 };
86
87 BOOL
88 add_registry (const char *path)
89 {
90 HKEY hrootkey = NULL;
91 int i;
92 BOOL ok = TRUE;
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 */
104 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
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. */
113 emacs_path = (char *) alloca (len);
114 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
115
116 RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
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;
134 HKEY runemacs_key = NULL;
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);
138 RegSetValueEx (hrootkey, "Path", 0, REG_EXPAND_SZ,
139 dll_paths, len);
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 {
149 RegSetValueEx (runemacs_key, "Path", 0, REG_EXPAND_SZ,
150 dll_paths, len);
151
152 RegCloseKey (runemacs_key);
153 }
154 }
155 }
156 RegCloseKey (gtk_key);
157 }
158 RegCloseKey (hrootkey);
159 }
160
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
166 /* Check both the current user and the local machine to see if we
167 have any resources. */
168
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)
175 {
176 return FALSE;
177 }
178
179 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
180 {
181 const char * value = env_vars[i].value ? env_vars[i].value : path;
182
183 if (RegSetValueEx (hrootkey, env_vars[i].name,
184 0, REG_EXPAND_SZ,
185 value, lstrlen (value) + 1) != ERROR_SUCCESS)
186 ok = FALSE;
187 }
188
189 RegCloseKey (hrootkey);
190
191 return (ok);
192 }
193
194 int
195 main (int argc, char *argv[])
196 {
197 char start_folder[MAX_PATH + 1];
198 int shortcuts_created = 0;
199 int com_available = 1;
200 char modname[MAX_PATH];
201 const char *prog_name;
202 const char *emacs_path;
203 char *p;
204 int quiet = 0;
205 HRESULT result;
206 IShellLinkA *shortcut;
207
208 /* If no args specified, use our location to set emacs_path. */
209
210 if (argc > 1
211 && (argv[1][0] == '/' || argv[1][0] == '-')
212 && argv[1][1] == 'q')
213 {
214 quiet = 1;
215 --argc;
216 ++argv;
217 }
218
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 {
239 fprintf (stderr, "usage: addpm emacs_path\n");
240 exit (1);
241 }
242
243 /* Tell user what we are going to do. */
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 }
258 }
259
260 add_registry (emacs_path);
261 prog_name = "runemacs.exe";
262
263 /* Try to install globally. */
264
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 }
272
273 if (com_available
274 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
275 {
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;
312
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);
333
334 }
335 }
336 }
337 }
338
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 }
366
367 DdeFreeStringHandle (dde, progman);
368 DdeUninitialize (dde);
369 }
370
371 return 0;
372 }
373