100954
|
1 /* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
|
75249
|
2 Free Software Foundation, Inc.
|
64890
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
94795
|
6 GNU Emacs is free software: you can redistribute it and/or modify
|
64890
|
7 it under the terms of the GNU General Public License as published by
|
94795
|
8 the Free Software Foundation, either version 3 of the License, or
|
|
9 (at your option) any later version.
|
64890
|
10
|
|
11 GNU Emacs is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
94795
|
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
|
64890
|
18
|
|
19
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
20 /*
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
21 Simple program to start Emacs with its console window hidden.
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
22
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
23 This program is provided purely for convenience, since most users will
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
24 use Emacs in windowing (GUI) mode, and will not want to have an extra
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
25 console window lying around. */
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
26
|
15320
|
27 /*
|
|
28 You may want to define this if you want to be able to install updated
|
|
29 emacs binaries even when other users are using the current version.
|
|
30 The problem with some file servers (notably Novell) is that an open
|
|
31 file cannot be overwritten, deleted, or even renamed. So if someone
|
|
32 is running emacs.exe already, you cannot install a newer version.
|
|
33 By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
|
|
34 something else which matches "emacs*.exe", and runemacs will
|
73541
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
35 automatically select the newest emacs executable in the bin directory.
|
15320
|
36 (So you'll probably be able to delete the old version some hours/days
|
|
37 later).
|
|
38 */
|
|
39
|
|
40 /* #define CHOOSE_NEWEST_EXE */
|
|
41
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
42 #include <windows.h>
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
43 #include <string.h>
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
44 #include <malloc.h>
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
45
|
103641
|
46 static void set_user_model_id ();
|
|
47
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
48 int WINAPI
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
49 WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
50 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
51 STARTUPINFO start;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
52 SECURITY_ATTRIBUTES sec_attrs;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
53 PROCESS_INFORMATION child;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
54 int wait_for_child = FALSE;
|
19720
|
55 DWORD priority_class = NORMAL_PRIORITY_CLASS;
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
56 DWORD ret_code = 0;
|
15320
|
57 char *new_cmdline;
|
|
58 char *p;
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
59 char modname[MAX_PATH];
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
60
|
103641
|
61 set_user_model_id ();
|
|
62
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
63 if (!GetModuleFileName (NULL, modname, MAX_PATH))
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
64 goto error;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
65 if ((p = strrchr (modname, '\\')) == NULL)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
66 goto error;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
67 *p = 0;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
68
|
38139
|
69 new_cmdline = alloca (MAX_PATH + strlen (cmdline) + 3);
|
|
70 /* Quote executable name in case of spaces in the path. */
|
|
71 *new_cmdline = '"';
|
|
72 strcpy (new_cmdline + 1, modname);
|
15320
|
73
|
|
74 #ifdef CHOOSE_NEWEST_EXE
|
|
75 {
|
|
76 /* Silly hack to allow new versions to be installed on
|
|
77 server even when current version is in use. */
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
78
|
15320
|
79 char * best_name = alloca (MAX_PATH + 1);
|
|
80 FILETIME best_time = {0,0};
|
|
81 WIN32_FIND_DATA wfd;
|
|
82 HANDLE fh;
|
|
83 p = new_cmdline + strlen (new_cmdline);
|
38139
|
84 strcpy (p, "\\emacs*.exe\" ");
|
15320
|
85 fh = FindFirstFile (new_cmdline, &wfd);
|
|
86 if (fh == INVALID_HANDLE_VALUE)
|
|
87 goto error;
|
|
88 do
|
|
89 {
|
73541
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
90 if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
91 || (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
92 && wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
93 {
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
94 best_time = wfd.ftLastWriteTime;
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
95 strcpy (best_name, wfd.cFileName);
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
96 }
|
15320
|
97 }
|
|
98 while (FindNextFile (fh, &wfd));
|
|
99 FindClose (fh);
|
|
100 *p++ = '\\';
|
|
101 strcpy (p, best_name);
|
|
102 strcat (p, " ");
|
|
103 }
|
|
104 #else
|
38139
|
105 strcat (new_cmdline, "\\emacs.exe\" ");
|
15320
|
106 #endif
|
|
107
|
19720
|
108 /* Append original arguments if any; first look for arguments we
|
|
109 recognise (-wait, -high, and -low), and apply them ourselves. */
|
|
110 while (cmdline[0] == '-' || cmdline[0] == '/')
|
15320
|
111 {
|
19720
|
112 if (strncmp (cmdline+1, "wait", 4) == 0)
|
|
113 {
|
73541
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
114 wait_for_child = TRUE;
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
115 cmdline += 5;
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
116 }
|
19720
|
117 else if (strncmp (cmdline+1, "high", 4) == 0)
|
|
118 {
|
|
119 priority_class = HIGH_PRIORITY_CLASS;
|
|
120 cmdline += 5;
|
|
121 }
|
|
122 else if (strncmp (cmdline+1, "low", 3) == 0)
|
|
123 {
|
|
124 priority_class = IDLE_PRIORITY_CLASS;
|
|
125 cmdline += 4;
|
|
126 }
|
|
127 else
|
|
128 break;
|
73541
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
129 /* Look for next argument. */
|
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
130 while (*++cmdline == ' ');
|
19720
|
131 }
|
73541
964e568f1d1d
(WinMain): Process all recognized arguments, not just the first one.
Juanma Barranquero <lekktu@gmail.com>
diff
changeset
|
132
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
133 strcat (new_cmdline, cmdline);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
134
|
15320
|
135 /* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
136 if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
|
15320
|
137 {
|
|
138 *p = 0;
|
|
139 for (p = modname; *p; p++)
|
|
140 if (*p == '\\') *p = '/';
|
|
141 SetEnvironmentVariable ("emacs_dir", modname);
|
|
142 }
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
143
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
144 memset (&start, 0, sizeof (start));
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
145 start.cb = sizeof (start);
|
69378
|
146 start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
147 start.wShowWindow = SW_HIDE;
|
69378
|
148 /* Ensure that we don't waste memory if the user has specified a huge
|
|
149 default screen buffer for command windows. */
|
|
150 start.dwXCountChars = 80;
|
|
151 start.dwYCountChars = 25;
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
152
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
153 sec_attrs.nLength = sizeof (sec_attrs);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
154 sec_attrs.lpSecurityDescriptor = NULL;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
155 sec_attrs.bInheritHandle = FALSE;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
156
|
19720
|
157 if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
|
54773
|
158 NULL, NULL, &start, &child))
|
15139
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
159 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
160 if (wait_for_child)
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
161 {
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
162 WaitForSingleObject (child.hProcess, INFINITE);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
163 GetExitCodeProcess (child.hProcess, &ret_code);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
164 }
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
165 CloseHandle (child.hThread);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
166 CloseHandle (child.hProcess);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
167 }
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
168 else
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
169 goto error;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
170 return (int) ret_code;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
171
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
172 error:
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
173 MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
174 return 1;
|
Geoff Voelker <voelker@cs.washington.edu>
parents:
diff
changeset
|
175 }
|
52401
|
176
|
103641
|
177 void set_user_model_id ()
|
|
178 {
|
|
179 HMODULE shell;
|
103716
|
180 HRESULT (WINAPI * set_user_model) (wchar_t * id);
|
103641
|
181
|
|
182 /* On Windows 7 and later, we need to set the user model ID
|
|
183 to associate emacsclient launched files with Emacs frames
|
|
184 in the UI. */
|
|
185 shell = LoadLibrary ("shell32.dll");
|
|
186 if (shell)
|
|
187 {
|
|
188 set_user_model
|
|
189 = (void *) GetProcAddress (shell,
|
|
190 "SetCurrentProcessExplicitAppUserModelID");
|
|
191
|
|
192 /* If the function is defined, then we are running on Windows 7
|
|
193 or newer, and the UI uses this to group related windows
|
|
194 together. Since emacs, runemacs, emacsclient are related, we
|
|
195 want them grouped even though the executables are different,
|
|
196 so we need to set a consistent ID between them. */
|
|
197 if (set_user_model)
|
|
198 set_user_model (L"GNU.Emacs");
|
|
199
|
|
200 FreeLibrary (shell);
|
|
201 }
|
|
202 }
|
|
203
|
52401
|
204 /* arch-tag: 7e02df73-4df7-4aa0-baea-99c6d047a384
|
|
205 (do not change this comment) */
|