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