PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/nt/addpm.c

https://gitlab.com/freesoftware/emacs
C | 347 lines | 240 code | 42 blank | 65 comment | 47 complexity | 8c549c94e0affee1ecfeb84a76164d49 MD5 | raw file
  1. /* Add entries to the GNU Emacs Program Manager folder.
  2. Copyright (C) 1995, 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or (at
  7. your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
  14. /****************************************************************************
  15. *
  16. * Program: addpm (adds emacs to the Windows program manager)
  17. *
  18. * Usage:
  19. * argv[1] = install path for emacs
  20. *
  21. * argv[2] used to be an optional argument for setting the icon.
  22. * But now Emacs has a professional looking icon of its own.
  23. * If users really want to change it, they can go into the settings of
  24. * the shortcut that is created and do it there.
  25. */
  26. /* Use parts of shell API that were introduced by the merge of IE4
  27. into the desktop shell. If Windows 95 or NT4 users do not have IE4
  28. installed, then the DDE fallback for creating icons the Windows 3.1
  29. progman way will be used instead, but that is prone to lockups
  30. caused by other applications not servicing their message queues. */
  31. #define DEFER_MS_W32_H
  32. #include <config.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #include <malloc.h>
  36. /* MinGW64 barfs if _WIN32_IE is defined to anything below 0x0500. */
  37. #ifndef MINGW_W64
  38. # ifdef _WIN32_IE
  39. # undef _WIN32_IE
  40. # endif
  41. #define _WIN32_IE 0x0400
  42. #endif
  43. /* Request C Object macros for COM interfaces. */
  44. #define COBJMACROS 1
  45. #include <windows.h>
  46. #include <shlobj.h>
  47. #include <ddeml.h>
  48. #ifndef OLD_PATHS
  49. #include "../src/epaths.h"
  50. #endif
  51. HDDEDATA CALLBACK DdeCallback (UINT, UINT, HCONV, HSZ, HSZ, HDDEDATA, DWORD_PTR,
  52. DWORD_PTR);
  53. HDDEDATA CALLBACK
  54. DdeCallback (UINT uType, UINT uFmt, HCONV hconv,
  55. HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
  56. DWORD_PTR dwData1, DWORD_PTR dwData2)
  57. {
  58. return ((HDDEDATA) NULL);
  59. }
  60. #define DdeCommand(str) \
  61. DdeClientTransaction ((LPBYTE)str, strlen (str)+1, conversation, (HSZ)NULL, \
  62. CF_TEXT, XTYP_EXECUTE, 30000, NULL)
  63. #define REG_ROOT "SOFTWARE\\GNU\\Emacs"
  64. #define REG_APP_PATH \
  65. "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\emacs.exe"
  66. static struct entry
  67. {
  68. const char *name;
  69. const char *value;
  70. }
  71. env_vars[] =
  72. {
  73. #ifdef OLD_PATHS
  74. {"emacs_dir", NULL},
  75. {"EMACSLOADPATH", "%emacs_dir%/site-lisp;%emacs_dir%/../site-lisp;%emacs_dir%/lisp"},
  76. {"SHELL", "%emacs_dir%/bin/cmdproxy.exe"},
  77. {"EMACSDATA", "%emacs_dir%/etc"},
  78. {"EMACSPATH", "%emacs_dir%/bin"},
  79. /* We no longer set INFOPATH because Info-default-directory-list
  80. is then ignored. */
  81. /* {"INFOPATH", "%emacs_dir%/info"}, */
  82. {"EMACSDOC", "%emacs_dir%/etc"},
  83. {"TERM", "cmd"}
  84. #else /* !OLD_PATHS */
  85. {"emacs_dir", NULL},
  86. {"EMACSLOADPATH", PATH_SITELOADSEARCH ";" PATH_LOADSEARCH},
  87. {"SHELL", PATH_EXEC "/cmdproxy.exe"},
  88. {"EMACSDATA", PATH_DATA},
  89. {"EMACSPATH", PATH_EXEC},
  90. /* We no longer set INFOPATH because Info-default-directory-list
  91. is then ignored. */
  92. /* {"INFOPATH", "%emacs_dir%/info"}, */
  93. {"EMACSDOC", PATH_DOC},
  94. {"TERM", "cmd"}
  95. #endif
  96. };
  97. static void
  98. add_registry (const char *path)
  99. {
  100. HKEY hrootkey = NULL;
  101. int i;
  102. /* Record the location of Emacs to the App Paths key if we have
  103. sufficient permissions to do so. This helps Windows find emacs quickly
  104. if the user types emacs.exe in the "Run Program" dialog without having
  105. emacs on their path. Some applications also use the same registry key
  106. to discover the installation directory for programs they are looking for.
  107. Multiple installations cannot be handled by this method, but it does not
  108. affect the general operation of other installations of Emacs, and we
  109. are blindly overwriting the Start Menu entries already.
  110. */
  111. if (RegCreateKeyEx (HKEY_LOCAL_MACHINE, REG_APP_PATH, 0, NULL,
  112. REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
  113. &hrootkey, NULL) == ERROR_SUCCESS)
  114. {
  115. int len;
  116. char *emacs_path;
  117. len = strlen (path) + 15; /* \bin\emacs.exe + terminator. */
  118. emacs_path = (char *) alloca (len);
  119. sprintf (emacs_path, "%s\\bin\\emacs.exe", path);
  120. RegSetValueEx (hrootkey, NULL, 0, REG_EXPAND_SZ, emacs_path, len);
  121. RegCloseKey (hrootkey);
  122. }
  123. /* Previous versions relied on registry settings, but we do not need
  124. them any more. If registry settings are installed from a previous
  125. version, replace them to ensure they are the current settings.
  126. Otherwise, do nothing. */
  127. /* Check both the current user and the local machine to see if we
  128. have any resources. */
  129. if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0,
  130. KEY_WRITE | KEY_QUERY_VALUE, &hrootkey) != ERROR_SUCCESS
  131. && RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0,
  132. KEY_WRITE | KEY_QUERY_VALUE, &hrootkey) != ERROR_SUCCESS)
  133. return;
  134. for (i = 0; i < (sizeof (env_vars) / sizeof (env_vars[0])); i++)
  135. {
  136. const char * value = env_vars[i].value ? env_vars[i].value : path;
  137. /* Replace only those settings that already exist. */
  138. if (RegQueryValueEx (hrootkey, env_vars[i].name, NULL,
  139. NULL, NULL, NULL) == ERROR_SUCCESS)
  140. RegSetValueEx (hrootkey, env_vars[i].name, 0, REG_EXPAND_SZ,
  141. value, lstrlen (value) + 1);
  142. }
  143. RegCloseKey (hrootkey);
  144. }
  145. int
  146. main (int argc, char *argv[])
  147. {
  148. char start_folder[MAX_PATH + 1];
  149. int shortcuts_created = 0;
  150. int com_available = 1;
  151. char modname[MAX_PATH];
  152. const char *prog_name;
  153. const char *emacs_path;
  154. char *p;
  155. int quiet = 0;
  156. HRESULT result;
  157. IShellLinkA *shortcut;
  158. /* If no args specified, use our location to set emacs_path. */
  159. if (argc > 1
  160. && (argv[1][0] == '/' || argv[1][0] == '-')
  161. && argv[1][1] == 'q')
  162. {
  163. quiet = 1;
  164. --argc;
  165. ++argv;
  166. }
  167. if (argc > 1)
  168. emacs_path = argv[1];
  169. else
  170. {
  171. if (!GetModuleFileName (NULL, modname, MAX_PATH) ||
  172. (p = strrchr (modname, '\\')) == NULL)
  173. {
  174. fprintf (stderr, "fatal error");
  175. exit (1);
  176. }
  177. *p = 0;
  178. /* Set emacs_path to emacs_dir if we are in "%emacs_dir%\bin". */
  179. if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
  180. {
  181. *p = 0;
  182. emacs_path = modname;
  183. }
  184. else
  185. {
  186. fprintf (stderr, "usage: addpm emacs_path\n");
  187. exit (1);
  188. }
  189. /* Tell user what we are going to do. */
  190. if (!quiet)
  191. {
  192. int result;
  193. const char install_msg[] = "Install Emacs at %s?\n";
  194. char msg[ MAX_PATH + sizeof (install_msg) ];
  195. sprintf (msg, install_msg, emacs_path);
  196. result = MessageBox (NULL, msg, "Install Emacs",
  197. MB_OKCANCEL | MB_ICONQUESTION);
  198. if (result != IDOK)
  199. {
  200. fprintf (stderr, "Install canceled\n");
  201. exit (1);
  202. }
  203. }
  204. }
  205. add_registry (emacs_path);
  206. prog_name = "runemacs.exe";
  207. /* Try to install globally. */
  208. if (!SUCCEEDED (CoInitialize (NULL))
  209. || !SUCCEEDED (CoCreateInstance (&CLSID_ShellLink, NULL,
  210. CLSCTX_INPROC_SERVER, &IID_IShellLinkA,
  211. (void **) &shortcut)))
  212. {
  213. com_available = 0;
  214. }
  215. if (com_available
  216. && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_COMMON_PROGRAMS, 0))
  217. {
  218. if (strlen (start_folder) < (MAX_PATH - 20))
  219. {
  220. strcat (start_folder, "\\Gnu Emacs");
  221. if (CreateDirectory (start_folder, NULL)
  222. || GetLastError () == ERROR_ALREADY_EXISTS)
  223. {
  224. char full_emacs_path[MAX_PATH + 1];
  225. IPersistFile *lnk;
  226. strcat (start_folder, "\\Emacs.lnk");
  227. sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
  228. IShellLinkA_SetPath (shortcut, full_emacs_path);
  229. IShellLinkA_SetDescription (shortcut, "GNU Emacs");
  230. result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
  231. (void **) &lnk);
  232. if (SUCCEEDED (result))
  233. {
  234. wchar_t unicode_path[MAX_PATH];
  235. MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
  236. unicode_path, MAX_PATH);
  237. if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
  238. shortcuts_created = 1;
  239. IPersistFile_Release (lnk);
  240. }
  241. }
  242. }
  243. }
  244. if (!shortcuts_created && com_available
  245. && SHGetSpecialFolderPath (NULL, start_folder, CSIDL_PROGRAMS, 0))
  246. {
  247. /* Ensure there is enough room for "...\GNU Emacs\Emacs.lnk". */
  248. if (strlen (start_folder) < (MAX_PATH - 20))
  249. {
  250. strcat (start_folder, "\\Gnu Emacs");
  251. if (CreateDirectory (start_folder, NULL)
  252. || GetLastError () == ERROR_ALREADY_EXISTS)
  253. {
  254. char full_emacs_path[MAX_PATH + 1];
  255. IPersistFile *lnk;
  256. strcat (start_folder, "\\Emacs.lnk");
  257. sprintf (full_emacs_path, "%s\\bin\\%s", emacs_path, prog_name);
  258. IShellLinkA_SetPath (shortcut, full_emacs_path);
  259. IShellLinkA_SetDescription (shortcut, "GNU Emacs");
  260. result = IShellLinkA_QueryInterface (shortcut, &IID_IPersistFile,
  261. (void **) &lnk);
  262. if (SUCCEEDED (result))
  263. {
  264. wchar_t unicode_path[MAX_PATH];
  265. MultiByteToWideChar (CP_ACP, 0, start_folder, -1,
  266. unicode_path, MAX_PATH);
  267. if (SUCCEEDED (IPersistFile_Save (lnk, unicode_path, TRUE)))
  268. shortcuts_created = 1;
  269. IPersistFile_Release (lnk);
  270. }
  271. }
  272. }
  273. }
  274. if (com_available)
  275. IShellLinkA_Release (shortcut);
  276. /* Need to call uninitialize, even if ComInitialize failed. */
  277. CoUninitialize ();
  278. /* Fallback on old DDE method if the above failed. */
  279. if (!shortcuts_created)
  280. {
  281. DWORD dde = 0;
  282. HCONV conversation;
  283. HSZ progman;
  284. char add_item[MAX_PATH*2 + 100];
  285. DdeInitialize (&dde, (PFNCALLBACK) DdeCallback, APPCMD_CLIENTONLY, 0);
  286. progman = DdeCreateStringHandle (dde, "PROGMAN", CP_WINANSI);
  287. conversation = DdeConnect (dde, progman, progman, NULL);
  288. if (conversation)
  289. {
  290. DdeCommand ("[CreateGroup (\"Gnu Emacs\")]");
  291. DdeCommand ("[ReplaceItem (Emacs)]");
  292. sprintf (add_item, "[AddItem (\"%s\\bin\\%s\", Emacs)]",
  293. emacs_path, prog_name);
  294. DdeCommand (add_item);
  295. DdeDisconnect (conversation);
  296. }
  297. DdeFreeStringHandle (dde, progman);
  298. DdeUninitialize (dde);
  299. }
  300. return 0;
  301. }