runemacs.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* runemacs --- Simple program to start Emacs with its console window hidden.
  2. Copyright (C) 2001-2016 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 <http://www.gnu.org/licenses/>. */
  14. /*
  15. Simple program to start Emacs with its console window hidden.
  16. This program is provided purely for convenience, since most users will
  17. use Emacs in windowing (GUI) mode, and will not want to have an extra
  18. console window lying around. */
  19. /*
  20. You may want to define this if you want to be able to install updated
  21. emacs binaries even when other users are using the current version.
  22. The problem with some file servers (notably Novell) is that an open
  23. file cannot be overwritten, deleted, or even renamed. So if someone
  24. is running emacs.exe already, you cannot install a newer version.
  25. By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
  26. something else which matches "emacs*.exe", and runemacs will
  27. automatically select the newest emacs executable in the bin directory.
  28. (So you'll probably be able to delete the old version some hours/days
  29. later).
  30. */
  31. /* #define CHOOSE_NEWEST_EXE */
  32. #include <windows.h>
  33. #include <string.h>
  34. #include <malloc.h>
  35. static void set_user_model_id (void);
  36. static int ensure_unicows_dll (void);
  37. int WINAPI
  38. WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
  39. {
  40. STARTUPINFO start;
  41. SECURITY_ATTRIBUTES sec_attrs;
  42. PROCESS_INFORMATION child;
  43. int wait_for_child = FALSE;
  44. DWORD priority_class = NORMAL_PRIORITY_CLASS;
  45. DWORD ret_code = 0;
  46. char *new_cmdline;
  47. char *p;
  48. char modname[MAX_PATH];
  49. static const char iconic_opt[] = "--iconic ", maximized_opt[] = "--maximized ";
  50. if (!ensure_unicows_dll ())
  51. goto error;
  52. set_user_model_id ();
  53. if (!GetModuleFileName (NULL, modname, MAX_PATH))
  54. goto error;
  55. if ((p = strrchr (modname, '\\')) == NULL)
  56. goto error;
  57. *p = 0;
  58. new_cmdline = alloca (MAX_PATH
  59. + strlen (cmdline)
  60. + ((nShow == SW_SHOWMINNOACTIVE
  61. || nShow == SW_SHOWMAXIMIZED)
  62. ? max (sizeof (iconic_opt), sizeof (maximized_opt))
  63. : 0)
  64. + 3);
  65. /* Quote executable name in case of spaces in the path. */
  66. *new_cmdline = '"';
  67. strcpy (new_cmdline + 1, modname);
  68. /* Detect and handle un-installed runemacs.exe in nt/ subdirectory,
  69. while emacs.exe is in src/. */
  70. if ((p = strrchr (new_cmdline, '\\')) != NULL
  71. && stricmp (p, "\\nt") == 0)
  72. strcpy (p, "\\src");
  73. #ifdef CHOOSE_NEWEST_EXE
  74. {
  75. /* Silly hack to allow new versions to be installed on
  76. server even when current version is in use. */
  77. char * best_name = alloca (MAX_PATH + 1);
  78. FILETIME best_time = {0,0};
  79. WIN32_FIND_DATA wfd;
  80. HANDLE fh;
  81. p = new_cmdline + strlen (new_cmdline);
  82. strcpy (p, "\\emacs*.exe\" ");
  83. fh = FindFirstFile (new_cmdline, &wfd);
  84. if (fh == INVALID_HANDLE_VALUE)
  85. goto error;
  86. do
  87. {
  88. if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
  89. || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
  90. && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
  91. {
  92. best_time = wfd.ftLastWriteTime;
  93. strcpy (best_name, wfd.cFileName);
  94. }
  95. }
  96. while (FindNextFile (fh, &wfd));
  97. FindClose (fh);
  98. *p++ = '\\';
  99. strcpy (p, best_name);
  100. strcat (p, " ");
  101. }
  102. #else
  103. strcat (new_cmdline, "\\emacs.exe\" ");
  104. #endif
  105. /* Append original arguments if any; first look for arguments we
  106. recognize (-wait, -high, and -low), and apply them ourselves. */
  107. while (cmdline[0] == '-' || cmdline[0] == '/')
  108. {
  109. if (strncmp (cmdline+1, "wait", 4) == 0)
  110. {
  111. wait_for_child = TRUE;
  112. cmdline += 5;
  113. }
  114. else if (strncmp (cmdline+1, "high", 4) == 0)
  115. {
  116. priority_class = HIGH_PRIORITY_CLASS;
  117. cmdline += 5;
  118. }
  119. else if (strncmp (cmdline+1, "low", 3) == 0)
  120. {
  121. priority_class = IDLE_PRIORITY_CLASS;
  122. cmdline += 4;
  123. }
  124. else
  125. break;
  126. /* Look for next argument. */
  127. while (*++cmdline == ' ');
  128. }
  129. /* If the desktop shortcut properties tell to invoke runemacs
  130. minimized, or if they invoked runemacs via "start /min", pass
  131. '--iconic' to Emacs, as that's what users will expect. Likewise
  132. with invoking runemacs maximized: pass '--maximized' to Emacs. */
  133. if (nShow == SW_SHOWMINNOACTIVE)
  134. strcat (new_cmdline, iconic_opt);
  135. else if (nShow == SW_SHOWMAXIMIZED)
  136. strcat (new_cmdline, maximized_opt);
  137. strcat (new_cmdline, cmdline);
  138. /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
  139. if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
  140. {
  141. *p = 0;
  142. for (p = modname; *p; p++)
  143. if (*p == '\\') *p = '/';
  144. SetEnvironmentVariable ("emacs_dir", modname);
  145. }
  146. memset (&start, 0, sizeof (start));
  147. start.cb = sizeof (start);
  148. start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
  149. start.wShowWindow = SW_HIDE;
  150. /* Ensure that we don't waste memory if the user has specified a huge
  151. default screen buffer for command windows. */
  152. start.dwXCountChars = 80;
  153. start.dwYCountChars = 25;
  154. sec_attrs.nLength = sizeof (sec_attrs);
  155. sec_attrs.lpSecurityDescriptor = NULL;
  156. sec_attrs.bInheritHandle = FALSE;
  157. if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
  158. NULL, NULL, &start, &child))
  159. {
  160. if (wait_for_child)
  161. {
  162. WaitForSingleObject (child.hProcess, INFINITE);
  163. GetExitCodeProcess (child.hProcess, &ret_code);
  164. }
  165. CloseHandle (child.hThread);
  166. CloseHandle (child.hProcess);
  167. }
  168. else
  169. goto error;
  170. return (int) ret_code;
  171. error:
  172. MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
  173. return 1;
  174. }
  175. void
  176. set_user_model_id (void)
  177. {
  178. HMODULE shell;
  179. HRESULT (WINAPI * set_user_model) (const wchar_t * id);
  180. /* On Windows 7 and later, we need to set the user model ID
  181. to associate emacsclient launched files with Emacs frames
  182. in the UI. */
  183. shell = LoadLibrary ("shell32.dll");
  184. if (shell)
  185. {
  186. set_user_model
  187. = (void *) GetProcAddress (shell,
  188. "SetCurrentProcessExplicitAppUserModelID");
  189. /* If the function is defined, then we are running on Windows 7
  190. or newer, and the UI uses this to group related windows
  191. together. Since emacs, runemacs, emacsclient are related, we
  192. want them grouped even though the executables are different,
  193. so we need to set a consistent ID between them. */
  194. if (set_user_model)
  195. set_user_model (L"GNU.Emacs");
  196. FreeLibrary (shell);
  197. }
  198. }
  199. static int
  200. ensure_unicows_dll (void)
  201. {
  202. OSVERSIONINFO os_ver;
  203. HMODULE h;
  204. ZeroMemory (&os_ver, sizeof (OSVERSIONINFO));
  205. os_ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  206. if (!GetVersionEx (&os_ver))
  207. return 0;
  208. if (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  209. {
  210. h = LoadLibrary ("Unicows.dll");
  211. if (!h)
  212. {
  213. int button;
  214. button = MessageBox (NULL,
  215. "Emacs cannot load the UNICOWS.DLL library.\n"
  216. "This library is essential for using Emacs\n"
  217. "on this system. You need to install it.\n\n"
  218. "Emacs will exit when you click OK.",
  219. "Emacs cannot load UNICOWS.DLL",
  220. MB_ICONERROR | MB_TASKMODAL
  221. | MB_SETFOREGROUND | MB_OK);
  222. switch (button)
  223. {
  224. case IDOK:
  225. default:
  226. return 0;
  227. }
  228. }
  229. FreeLibrary (h);
  230. return 1;
  231. }
  232. return 1;
  233. }