1 /* Utility routines for XEmacs on Windows 9x, NT and Cygwin.
2 Copyright (C) 2000 Ben Wing.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 #include "syssignal.h"
28 #include "syswindows.h"
30 typedef BOOL (WINAPI *pfSwitchToThread_t) (VOID);
31 pfSwitchToThread_t xSwitchToThread;
33 typedef HKL (WINAPI *pfGetKeyboardLayout_t) (DWORD);
34 pfGetKeyboardLayout_t xGetKeyboardLayout;
35 typedef BOOL (WINAPI *pfSetMenuDefaultItem_t) (HMENU, UINT, UINT);
36 pfSetMenuDefaultItem_t xSetMenuDefaultItem;
37 typedef BOOL (WINAPI *pfInsertMenuItemA_t)
38 (HMENU, UINT, BOOL, LPCMENUITEMINFOA);
39 pfInsertMenuItemA_t xInsertMenuItemA;
40 typedef BOOL (WINAPI *pfInsertMenuItemW_t)
41 (HMENU, UINT, BOOL, LPCMENUITEMINFOW);
42 pfInsertMenuItemW_t xInsertMenuItemW;
43 typedef HANDLE (WINAPI *pfLoadImageA_t)
44 (HINSTANCE, LPCSTR, UINT, int, int, UINT);
45 pfLoadImageA_t xLoadImageA;
46 typedef HANDLE (WINAPI *pfLoadImageW_t)
47 (HINSTANCE, LPCWSTR, UINT, int, int, UINT);
48 pfLoadImageW_t xLoadImageW;
49 typedef ATOM (WINAPI *pfRegisterClassExA_t) (CONST WNDCLASSEXA *);
50 pfRegisterClassExA_t xRegisterClassExA;
51 typedef ATOM (WINAPI *pfRegisterClassExW_t) (CONST WNDCLASSEXW *);
52 pfRegisterClassExW_t xRegisterClassExW;
54 typedef int (WINAPI *pfEnumFontFamiliesExA_t)
55 (HDC, LPLOGFONTA, FONTENUMPROCA, LPARAM, DWORD);
56 pfEnumFontFamiliesExA_t xEnumFontFamiliesExA;
57 typedef int (WINAPI *pfEnumFontFamiliesExW_t)
58 (HDC, LPLOGFONTW, FONTENUMPROCW, LPARAM, DWORD);
59 pfEnumFontFamiliesExW_t xEnumFontFamiliesExW;
61 typedef DWORD (WINAPI *pfSHGetFileInfoA_t)
62 (LPCSTR, DWORD, SHFILEINFOA FAR *, UINT, UINT);
63 pfSHGetFileInfoA_t xSHGetFileInfoA;
64 typedef DWORD (WINAPI *pfSHGetFileInfoW_t)
65 (LPCWSTR, DWORD, SHFILEINFOW FAR *, UINT, UINT);
66 pfSHGetFileInfoW_t xSHGetFileInfoW;
69 tstr_to_local_file_format (Extbyte *pathout)
74 EXTERNAL_TO_C_STRING (pathout, ttlff, Qmswindows_tstr);
75 WIN32_TO_LOCAL_FILE_FORMAT (ttlff, in);
81 init_potentially_nonexistent_functions (void)
83 HMODULE h_kernel = GetModuleHandle ("kernel32");
84 HMODULE h_user = GetModuleHandle ("user32");
85 HMODULE h_gdi = GetModuleHandle ("gdi32");
86 HMODULE h_shell = GetModuleHandle ("shell32");
91 (pfSwitchToThread_t) GetProcAddress (h_kernel, "SwitchToThread");
97 (pfGetKeyboardLayout_t) GetProcAddress (h_user, "GetKeyboardLayout");
99 (pfSetMenuDefaultItem_t) GetProcAddress (h_user, "SetMenuDefaultItem");
101 (pfInsertMenuItemA_t) GetProcAddress (h_user, "InsertMenuItemA");
103 (pfInsertMenuItemW_t) GetProcAddress (h_user, "InsertMenuItemW");
105 (pfLoadImageA_t) GetProcAddress (h_user, "LoadImageA");
107 (pfLoadImageW_t) GetProcAddress (h_user, "LoadImageW");
109 (pfRegisterClassExA_t) GetProcAddress (h_user, "RegisterClassExA");
111 (pfRegisterClassExW_t) GetProcAddress (h_user, "RegisterClassExW");
116 xEnumFontFamiliesExA =
117 (pfEnumFontFamiliesExA_t) GetProcAddress (h_gdi, "EnumFontFamiliesExA");
118 xEnumFontFamiliesExW =
119 (pfEnumFontFamiliesExW_t) GetProcAddress (h_gdi, "EnumFontFamiliesExW");
125 (pfSHGetFileInfoA_t) GetProcAddress (h_shell, "SHGetFileInfoA");
127 (pfSHGetFileInfoW_t) GetProcAddress (h_shell, "SHGetFileInfoW");
131 DEFUN ("mswindows-shell-execute", Fmswindows_shell_execute, 2, 4, 0, /*
132 Get Windows to perform OPERATION on DOCUMENT.
133 This is a wrapper around the ShellExecute system function, which
134 invokes the application registered to handle OPERATION for DOCUMENT.
135 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
136 nil for the default action), and DOCUMENT is typically the name of a
137 document file or URL, but can also be a program executable to run or
138 a directory to open in the Windows Explorer.
140 If DOCUMENT is a program executable, PARAMETERS can be a string
141 containing command line parameters, but otherwise should be nil.
143 SHOW-FLAG can be used to control whether the invoked application is hidden
144 or minimized. If SHOW-FLAG is nil, the application is displayed normally,
145 otherwise it is an integer representing a ShowWindow flag:
152 (operation, document, parameters, show_flag))
154 /* Encode filename and current directory. */
155 Lisp_Object current_dir = Ffile_name_directory (document);
158 char* fname1, *fname2;
163 struct gcpro gcpro1, gcpro2;
165 CHECK_STRING (document);
167 if (NILP (current_dir))
168 current_dir = current_buffer->directory;
170 GCPRO2 (current_dir, document);
172 /* Use mule and cygwin-safe APIs top get at file data. */
173 if (STRINGP (current_dir))
175 LOCAL_TO_WIN32_FILE_FORMAT (current_dir, path);
178 if (STRINGP (document))
180 doc = XSTRING_DATA (document);
182 if ((fname1 = strchr (doc, ':')) != NULL
183 && *++fname1 == '/' && *++fname1 == '/')
185 // URL-style if we get here, but we must only convert file
186 // arguments, since win32 paths are illegal in http etc.
187 if (strncmp (doc, "file://", 7) == 0)
191 if (!(isalpha (fname1[0]) && (IS_DEVICE_SEP (fname1[1]))))
193 sz = cygwin_posix_to_win32_path_list_buf_size (fname1);
194 fname2 = alloca (sz + pos);
195 strncpy (fname2, doc, pos);
198 cygwin_posix_to_win32_path_list (fname1, fname2);
203 // Not URL-style, must be a straight filename.
204 LOCAL_TO_WIN32_FILE_FORMAT (document, doc);
211 ret = (int) ShellExecute (NULL,
212 (STRINGP (operation) ?
213 XSTRING_DATA (operation) : NULL),
215 (STRINGP (parameters) ?
216 XSTRING_DATA (parameters) : NULL),
219 XINT (show_flag) : SW_SHOWDEFAULT));
224 if (ret == ERROR_FILE_NOT_FOUND)
225 signal_simple_error ("file not found", document);
226 else if (ret == ERROR_PATH_NOT_FOUND)
227 signal_simple_error ("path not found", current_dir);
228 else if (ret == ERROR_BAD_FORMAT)
229 signal_simple_error ("bad executable format", document);
231 error ("internal error");
237 DEFUN ("mswindows-cygwin-to-win32-path", Fmswindows_cygwin_to_win32_path, 1, 1, 0, /*
238 Get the cygwin environment to convert the Unix PATH to win32 format.
239 No expansion is performed, all conversion is done by the cygwin runtime.
247 /* There appears to be a bug in the cygwin conversion routines in
248 that they are not idempotent. */
249 p = XSTRING_DATA (path);
250 if (isalpha (p[0]) && (IS_DEVICE_SEP (p[1])))
253 /* Use mule and cygwin-safe APIs top get at file data. */
254 LOCAL_TO_WIN32_FILE_FORMAT (path, f);
255 return build_ext_string (f, Qnative);
260 /*--------------------------------------------------------------------*/
262 /*--------------------------------------------------------------------*/
264 /* setitimer() does not exist on native MS Windows, and appears broken
265 on Cygwin (random lockups when BROKEN_SIGIO is defined), so we
266 emulate in both cases by using multimedia timers. */
268 /* We emulate two timers, one for SIGALRM, another for SIGPROF.
270 itimerproc() function has an implementation limitation: it does
271 not allow to set *both* interval and period. If an attempt is
272 made to set both, and then they are unequal, the function
275 Minimum timer resolution on Win32 systems varies, and is greater
276 than or equal than 1 ms. The resolution is always wrapped not to
277 attempt to get below the system defined limit.
280 /* Timer precision, denominator of one fraction: for 100 ms
281 interval, request 10 ms precision
283 const int setitimer_helper_timer_prec = 10;
285 /* Last itimervals, as set by calls to setitimer */
286 static struct itimerval it_alarm;
287 static struct itimerval it_prof;
289 /* Timer IDs as returned by MM */
290 MMRESULT tid_alarm = 0;
291 MMRESULT tid_prof = 0;
294 setitimer_helper_proc (UINT uID, UINT uMsg, DWORD dwUser,
295 DWORD dw1, DWORD dw2)
297 /* Just raise the signal indicated by the dwUser parameter */
299 kill (getpid (), dwUser);
301 mswindows_raise (dwUser);
305 /* Divide time in ms specified by IT by DENOM. Return 1 ms
306 if division results in zero */
308 setitimer_helper_period (const struct itimerval* it, UINT denom)
310 static TIMECAPS time_caps;
313 const struct timeval* tv =
314 (it->it_value.tv_sec == 0 && it->it_value.tv_usec == 0)
315 ? &it->it_interval : &it->it_value;
317 /* Zero means stop timer */
318 if (tv->tv_sec == 0 && tv->tv_usec == 0)
321 /* Convert to ms and divide by denom */
322 res = (tv->tv_sec * 1000 + (tv->tv_usec + 500) / 1000) / denom;
324 /* Converge to minimum timer resolution */
325 if (time_caps.wPeriodMin == 0)
326 timeGetDevCaps (&time_caps, sizeof(time_caps));
328 if (res < time_caps.wPeriodMin)
329 res = time_caps.wPeriodMin;
335 setitimer_helper (const struct itimerval* itnew,
336 struct itimerval* itold, struct itimerval* itcurrent,
337 MMRESULT* tid, DWORD sigkind)
339 UINT delay, resolution, event_type;
341 /* First stop the old timer */
344 timeKillEvent (*tid);
345 timeEndPeriod (setitimer_helper_period (itcurrent,
346 setitimer_helper_timer_prec));
350 /* Return old itimerval if requested */
356 /* Determine if to start new timer */
357 delay = setitimer_helper_period (itnew, 1);
360 resolution = setitimer_helper_period (itnew,
361 setitimer_helper_timer_prec);
362 event_type = (itnew->it_value.tv_sec == 0 &&
363 itnew->it_value.tv_usec == 0)
364 ? TIME_ONESHOT : TIME_PERIODIC;
365 timeBeginPeriod (resolution);
366 *tid = timeSetEvent (delay, resolution, setitimer_helper_proc, sigkind,
370 return !delay || *tid;
374 mswindows_setitimer (int kind, const struct itimerval *itnew,
375 struct itimerval *itold)
377 /* In this version, both interval and value are allowed
378 only if they are equal. */
379 assert ((itnew->it_value.tv_sec == 0 && itnew->it_value.tv_usec == 0)
380 || (itnew->it_interval.tv_sec == 0 &&
381 itnew->it_interval.tv_usec == 0)
382 || (itnew->it_value.tv_sec == itnew->it_interval.tv_sec &&
383 itnew->it_value.tv_usec == itnew->it_interval.tv_usec));
385 if (kind == ITIMER_REAL)
386 return setitimer_helper (itnew, itold, &it_alarm, &tid_alarm, SIGALRM);
387 else if (kind == ITIMER_PROF)
388 return setitimer_helper (itnew, itold, &it_prof, &tid_prof, SIGPROF);
390 return errno = EINVAL;
397 DEFSUBR (Fmswindows_shell_execute);
399 DEFSUBR (Fmswindows_cygwin_to_win32_path);
406 init_potentially_nonexistent_functions ();