1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-11-26 07:33:47 +00:00
emacs/nt/runemacs.c

268 lines
7.7 KiB
C
Raw Normal View History

/* runemacs --- Simple program to start Emacs with its console window hidden.
Copyright (C) 2001-2021 Free Software Foundation, Inc.
2005-08-10 15:40:54 +00:00
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
2005-08-10 15:40:54 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
2005-08-10 15:40:54 +00:00
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
2005-08-10 15:40:54 +00:00
1996-05-03 18:29:10 +00:00
/*
Simple program to start Emacs with its console window hidden.
This program is provided purely for convenience, since most users will
use Emacs in windowing (GUI) mode, and will not want to have an extra
console window lying around. */
/*
You may want to define this if you want to be able to install updated
emacs binaries even when other users are using the current version.
The problem with some file servers (notably Novell) is that an open
file cannot be overwritten, deleted, or even renamed. So if someone
is running emacs.exe already, you cannot install a newer version.
By defining CHOOSE_NEWEST_EXE, you can name your new emacs.exe
something else which matches "emacs*.exe", and runemacs will
automatically select the newest emacs executable in the bin directory.
(So you'll probably be able to delete the old version some hours/days
later).
*/
/* #define CHOOSE_NEWEST_EXE */
1996-05-03 18:29:10 +00:00
#include <windows.h>
#include <string.h>
#include <malloc.h>
Make building under stricter warning flags somewhat cleaner. Flags used: -Wold-style-declaration -Wunused-function -Wstrict-prototypes * lib-src/emacsclient.c (getcwd, w32_getenv): * lib-src/ntlib.h (getlogin, getuid, getegid, getgid): Fix prototypes. * nt/runemacs.c (set_user_model_id): Fix prototype. * src/callproc.c (relocate_fd): Set inside #ifndef WINDOWSNT. * src/dired.c (opendir, readdir): Fix prototypes. * src/editfns.c (w32_get_internal_run_time): Fix prototypes. * src/keyboard.c (input_available_signal): Declare inside #ifdef SIGIO. * src/ndir.h (opendir, readdir, seekdir, closedir): Fix prototypes. (telldir): Remove declaration. * src/ralloc.c (real_morecore, __morecore): Fix prototypes. * src/sound.c (alsa_sound_perror): Declare inside #ifdef HAVE_ALSA. * src/syssignal.h (strsignal): Fix prototype. * src/term.c (tparam): Fix prototype. (term_get_fkeys_address, term_get_fkeys_kboard, term_get_fkeys_1) (term_get_fkeys): Set inside "#ifndef DOS_NT". * src/vm-limit.c (check_memory_limits): Fix prototypes of real_morecore and __morecore. * src/w32gui.h (XParseGeometry): Fix prototype. * src/w32heap.h (get_data_start, get_data_end, init_heap): Fix prototypes. * src/w32term.c (my_set_focus): Declare inside #if 0. * src/w32term.h (x_window_to_frame, x_display_info_for_name, w32_term_init) (w32_fill_rect, w32_clear_window, init_crit, delete_crit, signal_quit) (drain_message_queue, get_next_msg, post_msg, parse_button) (ClipboardSequence_Proc): Fix prototypes. (wait_for_sync): Remove declaration.
2010-07-25 00:20:51 +00:00
static void set_user_model_id (void);
static int ensure_unicows_dll (void);
1996-05-03 18:29:10 +00:00
int WINAPI
WinMain (HINSTANCE hSelf, HINSTANCE hPrev, LPSTR cmdline, int nShow)
{
STARTUPINFO start;
SECURITY_ATTRIBUTES sec_attrs;
PROCESS_INFORMATION child;
int wait_for_child = FALSE;
DWORD priority_class = NORMAL_PRIORITY_CLASS;
1996-05-03 18:29:10 +00:00
DWORD ret_code = 0;
char *new_cmdline;
char *p;
1996-05-03 18:29:10 +00:00
char modname[MAX_PATH];
static const char iconic_opt[] = "--iconic ", maximized_opt[] = "--maximized ";
1996-05-03 18:29:10 +00:00
if (!ensure_unicows_dll ())
goto error;
set_user_model_id ();
1996-05-03 18:29:10 +00:00
if (!GetModuleFileName (NULL, modname, MAX_PATH))
goto error;
if ((p = strrchr (modname, '\\')) == NULL)
goto error;
*p = 0;
new_cmdline = alloca (MAX_PATH
+ strlen (cmdline)
+ ((nShow == SW_SHOWMINNOACTIVE
|| nShow == SW_SHOWMAXIMIZED)
? max (sizeof (iconic_opt), sizeof (maximized_opt))
: 0)
+ 3);
/* Quote executable name in case of spaces in the path. */
*new_cmdline = '"';
strcpy (new_cmdline + 1, modname);
/* Detect and handle un-installed runemacs.exe in nt/ subdirectory,
while emacs.exe is in src/. */
if ((p = strrchr (new_cmdline, '\\')) != NULL
&& stricmp (p, "\\nt") == 0)
strcpy (p, "\\src");
1996-05-03 18:29:10 +00:00
#ifdef CHOOSE_NEWEST_EXE
1996-05-03 18:29:10 +00:00
{
/* Silly hack to allow new versions to be installed on
server even when current version is in use. */
char * best_name = alloca (MAX_PATH + 1);
FILETIME best_time = {0,0};
WIN32_FIND_DATA wfd;
HANDLE fh;
p = new_cmdline + strlen (new_cmdline);
strcpy (p, "\\emacs*.exe\" ");
fh = FindFirstFile (new_cmdline, &wfd);
if (fh == INVALID_HANDLE_VALUE)
goto error;
do
{
if (wfd.ftLastWriteTime.dwHighDateTime > best_time.dwHighDateTime
|| (wfd.ftLastWriteTime.dwHighDateTime == best_time.dwHighDateTime
&& wfd.ftLastWriteTime.dwLowDateTime > best_time.dwLowDateTime))
{
best_time = wfd.ftLastWriteTime;
strcpy (best_name, wfd.cFileName);
}
}
while (FindNextFile (fh, &wfd));
FindClose (fh);
*p++ = '\\';
strcpy (p, best_name);
strcat (p, " ");
1996-05-03 18:29:10 +00:00
}
#else
strcat (new_cmdline, "\\emacs.exe\" ");
#endif
/* Append original arguments if any; first look for arguments we
2011-11-20 02:29:42 +00:00
recognize (-wait, -high, and -low), and apply them ourselves. */
while (cmdline[0] == '-' || cmdline[0] == '/')
{
if (strncmp (cmdline+1, "wait", 4) == 0)
{
wait_for_child = TRUE;
cmdline += 5;
}
else if (strncmp (cmdline+1, "high", 4) == 0)
{
priority_class = HIGH_PRIORITY_CLASS;
cmdline += 5;
}
else if (strncmp (cmdline+1, "low", 3) == 0)
{
priority_class = IDLE_PRIORITY_CLASS;
cmdline += 4;
}
else
break;
/* Look for next argument. */
while (*++cmdline == ' ');
}
/* If the desktop shortcut properties tell to invoke runemacs
minimized, or if they invoked runemacs via "start /min", pass
'--iconic' to Emacs, as that's what users will expect. Likewise
with invoking runemacs maximized: pass '--maximized' to Emacs. */
if (nShow == SW_SHOWMINNOACTIVE)
strcat (new_cmdline, iconic_opt);
else if (nShow == SW_SHOWMAXIMIZED)
strcat (new_cmdline, maximized_opt);
1996-05-03 18:29:10 +00:00
strcat (new_cmdline, cmdline);
/* Set emacs_dir variable if runemacs was in "%emacs_dir%\bin". */
1996-05-03 18:29:10 +00:00
if ((p = strrchr (modname, '\\')) && stricmp (p, "\\bin") == 0)
{
*p = 0;
for (p = modname; *p; p++)
if (*p == '\\') *p = '/';
SetEnvironmentVariable ("emacs_dir", modname);
}
1996-05-03 18:29:10 +00:00
memset (&start, 0, sizeof (start));
start.cb = sizeof (start);
2006-03-11 15:02:34 +00:00
start.dwFlags = STARTF_USESHOWWINDOW | STARTF_USECOUNTCHARS;
1996-05-03 18:29:10 +00:00
start.wShowWindow = SW_HIDE;
2006-03-11 15:02:34 +00:00
/* Ensure that we don't waste memory if the user has specified a huge
default screen buffer for command windows. */
start.dwXCountChars = 80;
start.dwYCountChars = 25;
1996-05-03 18:29:10 +00:00
sec_attrs.nLength = sizeof (sec_attrs);
sec_attrs.lpSecurityDescriptor = NULL;
sec_attrs.bInheritHandle = FALSE;
if (CreateProcess (NULL, new_cmdline, &sec_attrs, NULL, TRUE, priority_class,
NULL, NULL, &start, &child))
1996-05-03 18:29:10 +00:00
{
if (wait_for_child)
{
WaitForSingleObject (child.hProcess, INFINITE);
GetExitCodeProcess (child.hProcess, &ret_code);
}
CloseHandle (child.hThread);
CloseHandle (child.hProcess);
}
else
goto error;
return (int) ret_code;
error:
MessageBox (NULL, "Could not start Emacs.", "Error", MB_ICONSTOP);
return 1;
}
2003-09-01 15:45:59 +00:00
void
set_user_model_id (void)
{
HMODULE shell;
Fix compiler warnings in the MinGW build * configure.ac [mingw32]: Don't add -Wpointer-sign, and add -Wno-pointer-sign, to keep the noise level down. * nt/mingw-cfg.site (gl_cv_warn_c__Wredundant_decls): Disable -Wredundant-decls, as that produces a lot of noise due to redeclaration of time-related functions by gnulib. * nt/runemacs.c (set_user_model_id): Fix argument type of 'SetCurrentProcessExplicitAppUserModelID'. * src/image.c (x_create_bitmap_from_file) [HAVE_NTGUI]: Don't declare 'dpyinfo', as it is unused. (xpm_load): Fix warnings about pointer signedness. * src/w32proc.c (IsValidLocale, init_winsock): Remove redundant prototypes. (sys_spawnve): Avoid warnings about discarding 'const' qualifier. (sys_select): Provide prototype. (g_b_init_compare_string_w): Move declaration to file scope. * src/w32heap.c (dumped_data_commit): Now static. (FREEABLE_P): Avoid warnings about pointer comparison with integer. (mmap_realloc): Cast to 'char *' for arithmetics on void pointers. * src/w32console.c (ctrl_c_handler, sys_tputs, sys_tgetstr) (evalcost, cmputc, cmcheckmagic, cmcostinit, cmgoto, Wcm_clear): Provide prototypes. * src/w32.c (globals_of_w32, conv_sockaddr_to_lisp): Remove redundant prototypes. (w32_get_internal_run_time, map_w32_filename): Provide prototype. (init_environment, sys_ctime): Avoid warnings about discarding 'const' qualifier. Include utimens.h. (sys_ctime, sys_chdir, sys_creat, sys_fopen, sys_mkdir) (sys_open, sys_rename, sys_rmdir, is_slow_fs, term_winsock) (sys_close, sys_dup2, sys_read, sys_write, sys_localtime): Provide prototypes. (sys_rename_replace): Use %d to avoid compiler warnings. (_wsa_errlist): Make the message text 'const char *', to avoid compilation warnings. (dynlib_reset_last_error): Move prototype to file scope. (w32_get_resource): First argument is now 'const char *'. * src/w32uniscribe.c (syms_of_w32uniscribe): Provide prototype. (otf_features): Second argument is no 'const char *'. * src/w32term.c (free_frame_menubar, x_wm_set_size_hint) (x_set_window_size): Remove redundant prototypes. (XChangeGC, XGetGCValues, w32_draw_underwave) (w32_draw_rectangle, w32_shift_glyphs_for_insert, x_mouse_leave) (x_calc_absolute_position, x_destroy_window): Now static. (menubar_selection_callback): Move prototype to file scope. * src/w32font.c (g_b_init_get_glyph_outline_w): Remove redundant declaration. (w32_to_x_charset): Fix warnings about discarding 'const' qualifier. (w32font_full_name): Fix warnings about implicit conversion of 'float' to 'double'. * src/w32reg.c (w32_get_rdb_resource): Fix warnings about discarding 'const' qualifier. * src/w32menu.c (syms_of_w32menu, globals_of_w32menu) (set_frame_menubar): Remove redundant prototypes. (menubar_selection_callback, w32_menu_display_help): Provide prototypes. (simple_dialog_show): Avoid warnings about discarding 'const' qualifier. * src/w32fns.c (syms_of_w32fns, globals_of_w32fns) (free_frame_menubar, w32_strerror, x_set_menu_bar_lines) (x_set_tool_bar_lines, x_set_internal_border_width): Remove redundant prototypes. (current_popup_menu): Remove redundant declaration. (colormap_t): Member 'name' is now 'const char *'. (add_system_logical_colors_to_map): Fix signed/unsigned warnings. (x_decode_color, x_set_border_pixel) (x_clear_under_internal_border, x_set_name, hook_w32_key) (reset_w32_kbdhook_state, deliver_wm_chars, w32_backtrace): Now static. (w32_load_cursor, w32_key_to_modifier, map_keypad_keys) (w32_msg_worker, w32_last_error): Provide prototypes. (funhook, lookup_vk_code): Avoid warnings about missing parentheses. (x_default_font_parameter, Fw32_notification_notify): Avoid warnings about discarding 'const' qualifier. (Fx_create_frame): Avoid warnings about empty body of 'else'. (x_screen_planes): Ifdef away unused function. (Fx_show_tip): Remove unused variables. (Fw32_battery_status): Avoid warnings about implicit promotion from float to double. (Fw32_notification_notify): Initialize 'timeout'. * src/profiler.c (profiler_cpu_running) [HAVE_ITIMERSPEC]: Only define the TIMER_SETTIME_RUNNING value if it will be used. * src/w32notify.c (send_notifications): Ifdef away an empty if clause. Remove unused variable. (watch_end, watch_completion): Provide prototypes. * src/sound.c (sound_warning) [WINDOWSNT]: Don't define: unused. * src/callproc.c (child_setup, getenv_internal_1) [WINDOWSNT]: Fix warning with pointer signedness. * src/gnutls.c (gnutls_x509_crt_get_signature) (gnutls_alert_send_appropriate) [WINDOWSNT]: Don't define, and don't load them from the GnuTLS library, as they are no longer used. * src/process.c (DATAGRAM_CHAN_P) [!DATAGRAM_SOCKETS]: Don't define, as it's unused. * src/unexw32.c (open_input_file, open_output_file) (close_file_data): Remove redundant prototypes. (_start): provide prototype. (mainCRTStartup): Move prototype to file level. (find_section): Use type-cast to shut up compiler warnings. (offset_to_section, relocate_offset): Now static. (find_section): First argument is now a 'const char *'. (offset_to_section): Ifdef away, as it's unused. * src/w32heap.h (find_section): Adjust prototype. * src/dynlib.c (dynlib_reset_last_error): Provide prototype. * src/dired.c (directory_files_internal_w32_unwind): Avoid warnings about missing prototypes. (is_slow_fs) [WINDOWSNT]: Provide prototype at file level. (directory_files_internal) [WINDOWSNT]: Fix warnings about pointer signedness. * src/fileio.c (Ffile_writable_p, Ffile_regular_p) [WINDOWSNT]: Fix warnings about pointer signedness. * src/filelock.c (WTMP_FILE) [WINDOWSNT]: Don't define, it's unused. * src/sysdep.c (_getpid): Remove redundant prototype. (sys_subshell) [DOS_NT]: Don't define 'status', it's unused. [!MSDOS]: Don't define 'st', it's unused. (init_sys_modes) [DOS_NT]: Don't define 'terminal', it's unused. (str_collate) [WINDOWSNT]: Avoid warnings about pointer signedness. * src/keyboard.c (tty_read_avail_input) [WINDOWSNT]: Don't define n_to_read, as it is not used. (MAX_ENCODED_BYTES) [WINDOWSNT]: Don't define, as it's unused. * src/w32font.h (syms_of_w32font): Remove redundant prototype. * src/xfaces.c (x_display_info) [HAVE_NTGUI]: Remove unused macro. * src/term.c (init_tty) [DOS_NT]: Ifdef away variables that are not used by DOS_NT builds, to avoid compiler warnings. * src/menu.c (current_popup_menu) [HAVE_NTGUI]: Remove redundant declaration. * src/dispnew.c (init_display) [WINDOWSNT]: Use type-cast to shut up compiler warnings. * src/w32term.h (x_set_window_size, x_get_focus_frame) (x_make_frame_visible, x_make_frame_invisible, x_iconify_frame) (x_set_frame_alpha, x_activate_menubar, x_bitmap_icon) (x_free_frame_resources, x_real_positions) (display_x_get_resource): Remove redundant prototypes. * lib-src/ntlib.c (sys_ctime, sys_fopen, sys_chdir, mkostemp) (sys_rename, gettimeofday): Provide prototypes. * lib-src/ntlib.h (getuid, geteuid, mkostemp): Remove redundant declarations. * lib-src/emacsclient.c (w32_getenv): Argument is now 'const char *'. (xstrdup, w32_get_resource, w32_window_app, w32_execvp, ttyname) (close_winsock, initialize_sockets, w32_set_user_model_id) (w32_find_emacs_process, w32_give_focus) [WINDOWSNT]: Add prototypes. (w32_get_resource) [WINDOWSNT]: Fix a warning about signedness difference. (w32_set_user_model_id): Update prototype of SetCurrentProcessExplicitAppUserModelID to avoid compiler warnings. (start_daemon_and_retry_set_socket) [WINDOWSNT]: Use type-cast to shut up compiler warnings. * lib-src/etags.c (MAXPATHLEN) [WINDOWSNT]: Remove unused macro.
2016-05-21 17:06:33 +00:00
HRESULT (WINAPI * set_user_model) (const wchar_t * id);
/* On Windows 7 and later, we need to set the user model ID
to associate emacsclient launched files with Emacs frames
in the UI. */
shell = LoadLibrary ("shell32.dll");
if (shell)
{
set_user_model
= (void *) GetProcAddress (shell,
"SetCurrentProcessExplicitAppUserModelID");
/* If the function is defined, then we are running on Windows 7
or newer, and the UI uses this to group related windows
together. Since emacs, runemacs, emacsclient are related, we
want them grouped even though the executables are different,
so we need to set a consistent ID between them. */
if (set_user_model)
set_user_model (L"GNU.Emacs");
FreeLibrary (shell);
}
}
static int
ensure_unicows_dll (void)
{
OSVERSIONINFO os_ver;
HMODULE h;
ZeroMemory (&os_ver, sizeof (OSVERSIONINFO));
os_ver.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (!GetVersionEx (&os_ver))
return 0;
if (os_ver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
h = LoadLibrary ("Unicows.dll");
if (!h)
{
int button;
button = MessageBox (NULL,
"Emacs cannot load the UNICOWS.DLL library.\n"
"This library is essential for using Emacs\n"
"on this system. You need to install it.\n\n"
"Emacs will exit when you click OK.",
"Emacs cannot load UNICOWS.DLL",
MB_ICONERROR | MB_TASKMODAL
| MB_SETFOREGROUND | MB_OK);
switch (button)
{
case IDOK:
default:
return 0;
}
}
FreeLibrary (h);
return 1;
}
return 1;
}