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