runemacs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* runemacs --- Simple program to start Emacs with its console window hidden.
  2. Copyright (C) 2001-2012 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
  7. (at 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. if (!ensure_unicows_dll ())
  50. goto error;
  51. set_user_model_id ();
  52. if (!GetModuleFileName (NULL, modname, MAX_PATH))
  53. goto error;
  54. if ((p = strrchr (modname, '\\')) == NULL)
  55. goto error;
  56. *p = 0;
  57. new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 3);
  58. /* Quote executable name in case of spaces in the path. */
  59. *new_cmdline = '"';
  60. strcpy (new_cmdline + 1, modname);
  61. #ifdef CHOOSE_NEWEST_EXE
  62. {
  63. /* Silly hack to allow new versions to be installed on
  64. server even when current version is in use. */
  65. char * best_name = alloca (MAX_PATH + 1);
  66. FILETIME best_time = {0,0};
  67. WIN32_FIND_DATA wfd;
  68. HANDLE fh;
  69. p = new_cmdline + strlen (new_cmdline);
  70. strcpy (p, "\\emacs*.exe\" ");
  71. fh = FindFirstFile (new_cmdline, &wfd);
  72. if (fh == INVALID_HANDLE_VALUE)
  73. goto error;
  74. do
  75. {
  76. if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
  77. || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
  78. && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
  79. {
  80. best_time = wfd.ftLastWriteTime;
  81. strcpy (best_name, wfd.cFileName);
  82. }
  83. }
  84. while (FindNextFile (fh, &wfd));
  85. FindClose (fh);
  86. *p++ = '\\';
  87. strcpy (p, best_name);
  88. strcat (p, " ");
  89. }
  90. #else
  91. strcat (new_cmdline, "\\emacs.exe\" ");
  92. #endif
  93. /* Append original arguments if any; first look for arguments we
  94. recognize (-wait, -high, and -low), and apply them ourselves. */
  95. while (cmdline[0] == '-' || cmdline[0] == '/')
  96. {
  97. if (strncmp (cmdline+1, "wait", 4) == 0)
  98. {
  99. wait_for_child = TRUE;
  100. cmdline += 5;
  101. }
  102. else if (strncmp (cmdline+1, "high", 4) == 0)
  103. {
  104. priority_class = HIGH_PRIORITY_CLASS;
  105. cmdline += 5;
  106. }
  107. else if (strncmp (cmdline+1, "low", 3) == 0)
  108. {
  109. priority_class = IDLE_PRIORITY_CLASS;
  110. cmdline += 4;
  111. }
  112. else
  113. break;
  114. /* Look for next argument. */
  115. while (*++cmdline == ' ');
  116. }
  117. strcat (new_cmdline, cmdline);
  118. /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
  119. if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
  120. {
  121. *p = 0;
  122. for (p = modname; *p; p++)
  123. if (*p == '\\') *p = '/';
  124. SetEnvironmentVariable ("emacs_dir", modname);
  125. }
  126. memset (&start, 0, sizeof (start));
  127. start.cb = sizeof (start);
  128. start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
  129. start.wShowWindow = SW_HIDE;
  130. /* Ensure that we don't waste memory if the user has specified a huge
  131. default screen buffer for command windows. */
  132. start.dwXCountChars = 80;
  133. start.dwYCountChars = 25;
  134. sec_attrs.nLength = sizeof (sec_attrs);
  135. sec_attrs.lpSecurityDescriptor = NULL;
  136. sec_attrs.bInheritHandle = FALSE;
  137. if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
  138. NULL, NULL, &start, &child))
  139. {
  140. if (wait_for_child)
  141. {
  142. WaitForSingleObject (child.hProcess, INFINITE);
  143. GetExitCodeProcess (child.hProcess, &ret_code);
  144. }
  145. CloseHandle (child.hThread);
  146. CloseHandle (child.hProcess);
  147. }
  148. else
  149. goto error;
  150. return (int) ret_code;
  151. error:
  152. MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
  153. return 1;
  154. }
  155. void
  156. set_user_model_id (void)
  157. {
  158. HMODULE shell;
  159. HRESULT (WINAPI * set_user_model) (wchar_t * id);
  160. /* On Windows 7 and later, we need to set the user model ID
  161. to associate emacsclient launched files with Emacs frames
  162. in the UI. */
  163. shell = LoadLibrary ("shell32.dll");
  164. if (shell)
  165. {
  166. set_user_model
  167. = (void *) GetProcAddress (shell,
  168. "SetCurrentProcessExplicitAppUserModelID");
  169. /* If the function is defined, then we are running on Windows 7
  170. or newer, and the UI uses this to group related windows
  171. together. Since emacs, runemacs, emacsclient are related, we
  172. want them grouped even though the executables are different,
  173. so we need to set a consistent ID between them. */
  174. if (set_user_model)
  175. set_user_model (L"GNU.Emacs");
  176. FreeLibrary (shell);
  177. }
  178. }
  179. static int
  180. ensure_unicows_dll (void)
  181. {
  182. OSVERSIONINFO os_ver;
  183. HMODULE h;
  184. ZeroMemory (&os_ver, sizeof (OSVERSIONINFO));
  185. os_ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
  186. if (!GetVersionEx (&os_ver))
  187. return 0;
  188. if (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  189. {
  190. h = LoadLibrary ("Unicows.dll");
  191. if (!h)
  192. {
  193. int button;
  194. button = MessageBox (NULL,
  195. "Emacs cannot load the UNICOWS.DLL library.\n"
  196. "This library is essential for using Emacs\n"
  197. "on this system. You need to install it.\n\n"
  198. "However, you can still use Emacs by invoking\n"
  199. "it with the '-nw' command-line option.\n\n"
  200. "Emacs will exit when you click OK.",
  201. "Emacs cannot load UNICOWS.DLL",
  202. MB_ICONERROR | MB_TASKMODAL
  203. | MB_SETFOREGROUND | MB_OK);
  204. switch (button)
  205. {
  206. case IDOK:
  207. default:
  208. return 0;
  209. }
  210. }
  211. FreeLibrary (h);
  212. return 1;
  213. }
  214. return 1;
  215. }