* src/vm-limit.c: Do not include sys/resource.h, mem-limits.h does it.
[emacs.git] / nt / addpm.c
blob20b289f6761da64169a99d42ea753b42ce533ac1
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 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
20 /****************************************************************************
22 * Program: addpm (adds emacs to the Windows program manager)
24 * Usage:
25 * argv[1] = install path for emacs
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.
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
42 #include <windows.h>
43 #include <shlobj.h>
44 #include <ddeml.h>
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <malloc.h>
49 HDDEDATA CALLBACK
50 DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
51 HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
52 DWORD dwData1, DWORD dwData2)
54 return ((HDDEDATA) NULL);
57 #define DdeCommand(str) \
58 DdeClientTransaction (str, strlen (str)+1, conversation, (HSZ)NULL, \
59 CF_TEXT, XTYP_EXECUTE, 30000, NULL)
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"
66 static struct entry
68 char *name;
69 char *value;
71 env_vars[] =
73 {"emacs_dir", NULL},
74 {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp;%emacs_dir%/leim"},
75 {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
76 {"EMACSDATA", "%emacs_dir%/etc"},
77 {"EMACSPATH", "%emacs_dir%/bin"},
78 /* We no longer set INFOPATH because Info-default-directory-list
79 is then ignored. */
80 /* {"INFOPATH", "%emacs_dir%/info"}, */
81 {"EMACSDOC", "%emacs_dir%/etc"},
82 {"TERM", "cmd"}
85 BOOL
86 add_registry (char *path)
88 HKEY hrootkey = NULL;
89 int i;
90 BOOL ok = TRUE;
91 DWORD size;
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.
102 if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, "",
103 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
104 &hrootkey, NULL) == ERROR_SUCCESS)
106 int len;
107 char *emacs_path;
108 HKEY gtk_key = NULL;
110 len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
111 emacs_path = (char *) alloca (len);
112 sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
114 RegSetValueEx (hrootkey, NULL, 0, REG_SZ, emacs_path, len);
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)
122 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
123 NULL, &size) == ERROR_SUCCESS)
125 char *gtk_path = (char *) alloca (size);
126 if (RegQueryValueEx (gtk_key, "DllPath", NULL, NULL,
127 gtk_path, &size) == ERROR_SUCCESS)
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);
138 RegCloseKey (gtk_key);
140 RegCloseKey (hrootkey);
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. */
148 /* Check both the current user and the local machine to see if we
149 have any resources. */
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)
158 return FALSE;
161 for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
163 char * value = env_vars[i].value ? env_vars[i].value : path;
165 if (RegSetValueEx (hrootkey, env_vars[i].name,
166 0, REG_EXPAND_SZ,
167 value, lstrlen (value) + 1) != ERROR_SUCCESS)
168 ok = FALSE;
171 RegCloseKey (hrootkey);
173 return (ok);
177 main (int argc, char *argv[])
179 char start_folder[MAX_PATH + 1];
180 int shortcuts_created = 0;
181 int com_available = 1;
182 char modname[MAX_PATH];
183 char *prog_name;
184 char *emacs_path;
185 char *p;
186 int quiet = 0;
187 HRESULT result;
188 IShellLinkA *shortcut;
190 /* If no args specified, use our location to set emacs_path. */
192 if (argc > 1
193 && (argv[1][0] == '/' || argv[1][0] == '-')
194 && argv[1][1] == 'q')
196 quiet = 1;
197 --argc;
198 ++argv;
201 if (argc > 1)
202 emacs_path = argv[1];
203 else
205 if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
206 (p = strrchr (modname, '\\')) == NULL)
208 fprintf (stderr, "fatal error");
209 exit (1);
211 *p = 0;
213 /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
214 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
216 *p = 0;
217 emacs_path = modname;
219 else
221 fprintf (stderr, "usage: addpm emacs_path\n");
222 exit (1);
225 /* Tell user what we are going to do. */
226 if (!quiet)
228 int result;
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)
236 fprintf (stderr, "Install cancelled\n");
237 exit (1);
242 add_registry (emacs_path);
243 prog_name = "runemacs.exe";
245 /* Try to install globally. */
247 if (!SUCCEEDED (CoInitialize (NULL))
248 || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
249 CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
250 (void **) &shortcut)))
252 com_available = 0;
255 if (com_available
256 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
258 if (strlen (start_folder) < (MAX_PATH - 20))
260 BOOL retval;
262 strcat (start_folder, "\\Gnu Emacs");
263 if (CreateDirectory (start_folder, NULL)
264 || GetLastError () == ERROR_ALREADY_EXISTS)
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))
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);
287 if (!shortcuts_created && com_available
288 && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
290 /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
291 if (strlen (start_folder) < (MAX_PATH - 20))
293 BOOL retval;
295 strcat (start_folder, "\\Gnu Emacs");
296 if (CreateDirectory (start_folder, NULL)
297 || GetLastError () == ERROR_ALREADY_EXISTS)
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))
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);
321 if (com_available)
322 IShellLinkA_Release (shortcut);
324 /* Need to call uninitialize, even if ComInitialize failed. */
325 CoUninitialize ();
327 /* Fallback on old DDE method if the above failed. */
328 if (!shortcuts_created)
330 DWORD dde = 0;
331 HCONV conversation;
332 HSZ progman;
333 char add_item[MAX_PATH*2 + 100];
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)
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);
346 DdeDisconnect (conversation);
349 DdeFreeStringHandle (dde, progman);
350 DdeUninitialize (dde);
353 return 0;
356 /* arch-tag: f923609d-b781-4ef4-abce-ca0da29cbbf0
357 (do not change this comment) */