442
|
1 /* Utility routines for XEmacs on Windows 9x, NT and Cygwin.
|
|
2 Copyright (C) 2000 Ben Wing.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
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
|
|
9 later version.
|
|
10
|
|
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
|
|
14 for more details.
|
|
15
|
|
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
|
|
19 02111-1307, USA. */
|
|
20
|
|
21 #include <config.h>
|
|
22 #include "lisp.h"
|
|
23
|
|
24 #include "buffer.h"
|
611
|
25
|
|
26 #include "syssignal.h"
|
|
27 #include "systime.h"
|
442
|
28 #include "syswindows.h"
|
|
29
|
|
30 typedef BOOL (WINAPI *pfSwitchToThread_t) (VOID);
|
|
31 pfSwitchToThread_t xSwitchToThread;
|
|
32
|
|
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;
|
|
53
|
|
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;
|
|
60
|
|
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;
|
|
67
|
531
|
68 typedef NET_API_STATUS (NET_API_FUNCTION *pfNetUserEnum_t)
|
|
69 (
|
|
70 IN LPCWSTR servername OPTIONAL,
|
|
71 IN DWORD level,
|
|
72 IN DWORD filter,
|
|
73 OUT LPBYTE *bufptr,
|
|
74 IN DWORD prefmaxlen,
|
|
75 OUT LPDWORD entriesread,
|
|
76 OUT LPDWORD totalentries,
|
|
77 IN OUT LPDWORD resume_handle OPTIONAL
|
|
78 );
|
|
79 pfNetUserEnum_t xNetUserEnum;
|
|
80
|
|
81 typedef NET_API_STATUS (NET_API_FUNCTION *pfNetApiBufferFree_t)
|
|
82 (
|
|
83 IN LPVOID Buffer
|
|
84 );
|
|
85 pfNetApiBufferFree_t xNetApiBufferFree;
|
|
86
|
442
|
87 Lisp_Object
|
|
88 tstr_to_local_file_format (Extbyte *pathout)
|
|
89 {
|
578
|
90 Bufbyte *ttlff;
|
442
|
91 Lisp_Object in;
|
|
92
|
|
93 EXTERNAL_TO_C_STRING (pathout, ttlff, Qmswindows_tstr);
|
|
94 WIN32_TO_LOCAL_FILE_FORMAT (ttlff, in);
|
|
95
|
|
96 return in;
|
|
97 }
|
|
98
|
|
99 static void
|
|
100 init_potentially_nonexistent_functions (void)
|
|
101 {
|
|
102 HMODULE h_kernel = GetModuleHandle ("kernel32");
|
|
103 HMODULE h_user = GetModuleHandle ("user32");
|
|
104 HMODULE h_gdi = GetModuleHandle ("gdi32");
|
|
105 HMODULE h_shell = GetModuleHandle ("shell32");
|
531
|
106 /* the following does not seem to get mapped in automatically */
|
|
107 HMODULE h_netapi = LoadLibrary ("netapi32.dll");
|
442
|
108
|
|
109 if (h_kernel)
|
|
110 {
|
|
111 xSwitchToThread =
|
|
112 (pfSwitchToThread_t) GetProcAddress (h_kernel, "SwitchToThread");
|
|
113 }
|
|
114
|
|
115 if (h_user)
|
|
116 {
|
|
117 xGetKeyboardLayout =
|
|
118 (pfGetKeyboardLayout_t) GetProcAddress (h_user, "GetKeyboardLayout");
|
|
119 xSetMenuDefaultItem =
|
|
120 (pfSetMenuDefaultItem_t) GetProcAddress (h_user, "SetMenuDefaultItem");
|
|
121 xInsertMenuItemA =
|
|
122 (pfInsertMenuItemA_t) GetProcAddress (h_user, "InsertMenuItemA");
|
|
123 xInsertMenuItemW =
|
|
124 (pfInsertMenuItemW_t) GetProcAddress (h_user, "InsertMenuItemW");
|
|
125 xLoadImageA =
|
|
126 (pfLoadImageA_t) GetProcAddress (h_user, "LoadImageA");
|
|
127 xLoadImageW =
|
|
128 (pfLoadImageW_t) GetProcAddress (h_user, "LoadImageW");
|
|
129 xRegisterClassExA =
|
|
130 (pfRegisterClassExA_t) GetProcAddress (h_user, "RegisterClassExA");
|
|
131 xRegisterClassExW =
|
|
132 (pfRegisterClassExW_t) GetProcAddress (h_user, "RegisterClassExW");
|
|
133 }
|
|
134
|
|
135 if (h_gdi)
|
|
136 {
|
|
137 xEnumFontFamiliesExA =
|
|
138 (pfEnumFontFamiliesExA_t) GetProcAddress (h_gdi, "EnumFontFamiliesExA");
|
|
139 xEnumFontFamiliesExW =
|
|
140 (pfEnumFontFamiliesExW_t) GetProcAddress (h_gdi, "EnumFontFamiliesExW");
|
|
141 }
|
|
142
|
|
143 if (h_shell)
|
|
144 {
|
|
145 xSHGetFileInfoA =
|
|
146 (pfSHGetFileInfoA_t) GetProcAddress (h_shell, "SHGetFileInfoA");
|
|
147 xSHGetFileInfoW =
|
|
148 (pfSHGetFileInfoW_t) GetProcAddress (h_shell, "SHGetFileInfoW");
|
|
149 }
|
531
|
150
|
|
151 if (h_netapi)
|
|
152 {
|
|
153 xNetUserEnum =
|
|
154 (pfNetUserEnum_t) GetProcAddress (h_netapi, "NetUserEnum");
|
|
155 xNetApiBufferFree =
|
|
156 (pfNetApiBufferFree_t) GetProcAddress (h_netapi, "NetApiBufferFree");
|
|
157 }
|
442
|
158 }
|
|
159
|
|
160 DEFUN ("mswindows-shell-execute", Fmswindows_shell_execute, 2, 4, 0, /*
|
|
161 Get Windows to perform OPERATION on DOCUMENT.
|
|
162 This is a wrapper around the ShellExecute system function, which
|
|
163 invokes the application registered to handle OPERATION for DOCUMENT.
|
|
164 OPERATION is typically \"open\", \"print\" or \"explore\" (but can be
|
|
165 nil for the default action), and DOCUMENT is typically the name of a
|
|
166 document file or URL, but can also be a program executable to run or
|
|
167 a directory to open in the Windows Explorer.
|
|
168
|
|
169 If DOCUMENT is a program executable, PARAMETERS can be a string
|
|
170 containing command line parameters, but otherwise should be nil.
|
|
171
|
|
172 SHOW-FLAG can be used to control whether the invoked application is hidden
|
|
173 or minimized. If SHOW-FLAG is nil, the application is displayed normally,
|
|
174 otherwise it is an integer representing a ShowWindow flag:
|
|
175
|
|
176 0 - start hidden
|
|
177 1 - start normally
|
|
178 3 - start maximized
|
|
179 6 - start minimized
|
|
180 */
|
|
181 (operation, document, parameters, show_flag))
|
|
182 {
|
|
183 /* Encode filename and current directory. */
|
|
184 Lisp_Object current_dir = Ffile_name_directory (document);
|
|
185 char* path = NULL;
|
|
186 char* doc = NULL;
|
|
187 Extbyte* f=0;
|
|
188 int ret;
|
|
189 struct gcpro gcpro1, gcpro2;
|
|
190
|
|
191 CHECK_STRING (document);
|
|
192
|
|
193 if (NILP (current_dir))
|
|
194 current_dir = current_buffer->directory;
|
|
195
|
|
196 GCPRO2 (current_dir, document);
|
|
197
|
|
198 /* Use mule and cygwin-safe APIs top get at file data. */
|
|
199 if (STRINGP (current_dir))
|
|
200 {
|
|
201 TO_EXTERNAL_FORMAT (LISP_STRING, current_dir,
|
|
202 C_STRING_ALLOCA, f,
|
|
203 Qfile_name);
|
|
204 #ifdef CYGWIN
|
|
205 CYGWIN_WIN32_PATH (f, path);
|
|
206 #else
|
|
207 path = f;
|
|
208 #endif
|
|
209 }
|
|
210
|
|
211 if (STRINGP (document))
|
|
212 {
|
|
213 TO_EXTERNAL_FORMAT (LISP_STRING, document,
|
|
214 C_STRING_ALLOCA, f,
|
|
215 Qfile_name);
|
|
216 #ifdef CYGWIN
|
|
217 CYGWIN_WIN32_PATH (f, doc);
|
|
218 #else
|
|
219 doc = f;
|
|
220 #endif
|
|
221 }
|
|
222
|
|
223 UNGCPRO;
|
|
224
|
|
225 ret = (int) ShellExecute (NULL,
|
|
226 (STRINGP (operation) ?
|
593
|
227 /* !!#### more mule bogosity */
|
|
228 (char *) XSTRING_DATA (operation) : NULL),
|
442
|
229 doc,
|
|
230 (STRINGP (parameters) ?
|
593
|
231 /* !!#### more mule bogosity */
|
|
232 (char *) XSTRING_DATA (parameters) : NULL),
|
442
|
233 path,
|
|
234 (INTP (show_flag) ?
|
|
235 XINT (show_flag) : SW_SHOWDEFAULT));
|
|
236
|
|
237 if (ret > 32)
|
|
238 return Qt;
|
|
239
|
|
240 if (ret == ERROR_FILE_NOT_FOUND)
|
563
|
241 signal_error (Qfile_error, "file not found", document);
|
442
|
242 else if (ret == ERROR_PATH_NOT_FOUND)
|
563
|
243 signal_error (Qfile_error, "path not found", current_dir);
|
442
|
244 else if (ret == ERROR_BAD_FORMAT)
|
563
|
245 signal_error (Qfile_error, "bad executable format", document);
|
442
|
246 else
|
563
|
247 signal_error (Qinternal_error, "internal error", Qunbound);
|
442
|
248
|
|
249 return Qnil;
|
|
250 }
|
|
251
|
613
|
252 #if defined (WIN32_NATIVE) || defined (CYGWIN_BROKEN_SIGNALS)
|
|
253
|
|
254 /* setitimer() does not exist on native MS Windows, and appears broken
|
|
255 on Cygwin (random lockups when BROKEN_SIGIO is defined), so we
|
|
256 emulate in both cases by using multimedia timers. Furthermore,
|
|
257 the lockups still occur on Cygwin even when we do nothing but
|
|
258 use the standard signalling mechanism -- so we have to emulate
|
|
259 that, too. (But only for timeouts -- we have to use the standard
|
|
260 mechanism for SIGCHLD. Yuck.)
|
|
261 */
|
|
262
|
|
263
|
|
264 /*--------------------------------------------------------------------*/
|
|
265 /* Signal support */
|
|
266 /*--------------------------------------------------------------------*/
|
|
267
|
|
268 #define sigmask(nsig) (1U << nsig)
|
|
269
|
|
270 /* We can support as many signals as fit into word */
|
|
271 #define SIG_MAX 32
|
|
272
|
|
273 /* Signal handlers. Initial value = 0 = SIG_DFL */
|
|
274 static mswindows_sighandler signal_handlers[SIG_MAX] = {0};
|
|
275
|
|
276 /* Signal block mask: bit set to 1 means blocked */
|
|
277 unsigned signal_block_mask = 0;
|
|
278
|
|
279 /* Signal pending mask: bit set to 1 means sig is pending */
|
|
280 unsigned signal_pending_mask = 0;
|
|
281
|
|
282 mswindows_sighandler
|
|
283 mswindows_sigset (int nsig, mswindows_sighandler handler)
|
|
284 {
|
|
285 /* We delegate some signals to the system function */
|
|
286 if (nsig == SIGFPE || nsig == SIGABRT || nsig == SIGINT)
|
|
287 return signal (nsig, handler);
|
|
288
|
|
289 if (nsig < 0 || nsig > SIG_MAX)
|
|
290 {
|
|
291 errno = EINVAL;
|
|
292 return NULL;
|
|
293 }
|
|
294
|
|
295 /* Store handler ptr */
|
|
296 {
|
|
297 mswindows_sighandler old_handler = signal_handlers[nsig];
|
|
298 signal_handlers[nsig] = handler;
|
|
299 return old_handler;
|
|
300 }
|
|
301 }
|
|
302
|
|
303 int
|
|
304 mswindows_sighold (int nsig)
|
|
305 {
|
|
306 if (nsig < 0 || nsig > SIG_MAX)
|
|
307 return errno = EINVAL;
|
|
308
|
|
309 signal_block_mask |= sigmask (nsig);
|
|
310 return 0;
|
|
311 }
|
|
312
|
|
313 int
|
|
314 mswindows_sigrelse (int nsig)
|
|
315 {
|
|
316 if (nsig < 0 || nsig > SIG_MAX)
|
|
317 return errno = EINVAL;
|
|
318
|
|
319 signal_block_mask &= ~sigmask (nsig);
|
|
320
|
|
321 if (signal_pending_mask & sigmask (nsig))
|
|
322 mswindows_raise (nsig);
|
|
323
|
|
324 return 0;
|
|
325 }
|
|
326
|
|
327 int
|
|
328 mswindows_sigpause (int nsig)
|
|
329 {
|
|
330 /* This is currently not called, because the only call to sigpause
|
|
331 inside XEmacs is with SIGCHLD parameter. Just in case, we put an
|
|
332 assert here, so anyone adds a call to sigpause will be surprised
|
|
333 (or surprise someone else...) */
|
|
334 assert (0);
|
|
335 return 0;
|
|
336 }
|
|
337
|
|
338 int
|
|
339 mswindows_raise (int nsig)
|
|
340 {
|
|
341 /* We delegate some raises to the system routine */
|
|
342 if (nsig == SIGFPE || nsig == SIGABRT || nsig == SIGINT)
|
|
343 return raise (nsig);
|
|
344
|
|
345 if (nsig < 0 || nsig > SIG_MAX)
|
|
346 return errno = EINVAL;
|
|
347
|
|
348 /* If the signal is blocked, remember to issue later */
|
|
349 if (signal_block_mask & sigmask (nsig))
|
|
350 {
|
|
351 signal_pending_mask |= sigmask (nsig);
|
|
352 return 0;
|
|
353 }
|
|
354
|
|
355 if (signal_handlers[nsig] == SIG_IGN)
|
|
356 return 0;
|
|
357
|
|
358 if (signal_handlers[nsig] != SIG_DFL)
|
|
359 {
|
|
360 (*signal_handlers[nsig]) (nsig);
|
|
361 return 0;
|
|
362 }
|
|
363
|
|
364 /* Default signal actions */
|
|
365 if (nsig == SIGALRM || nsig == SIGPROF)
|
|
366 exit (3);
|
|
367
|
|
368 /* Other signals are ignored by default */
|
|
369 return 0;
|
|
370 }
|
|
371
|
611
|
372
|
|
373 /*--------------------------------------------------------------------*/
|
|
374 /* Async timers */
|
|
375 /*--------------------------------------------------------------------*/
|
|
376
|
|
377 /* We emulate two timers, one for SIGALRM, another for SIGPROF.
|
|
378
|
|
379 itimerproc() function has an implementation limitation: it does
|
|
380 not allow to set *both* interval and period. If an attempt is
|
|
381 made to set both, and then they are unequal, the function
|
|
382 asserts.
|
|
383
|
|
384 Minimum timer resolution on Win32 systems varies, and is greater
|
|
385 than or equal than 1 ms. The resolution is always wrapped not to
|
|
386 attempt to get below the system defined limit.
|
|
387 */
|
|
388
|
|
389 /* Timer precision, denominator of one fraction: for 100 ms
|
|
390 interval, request 10 ms precision
|
|
391 */
|
|
392 const int setitimer_helper_timer_prec = 10;
|
|
393
|
|
394 /* Last itimervals, as set by calls to setitimer */
|
|
395 static struct itimerval it_alarm;
|
|
396 static struct itimerval it_prof;
|
|
397
|
|
398 /* Timer IDs as returned by MM */
|
|
399 MMRESULT tid_alarm = 0;
|
|
400 MMRESULT tid_prof = 0;
|
|
401
|
|
402 static void CALLBACK
|
|
403 setitimer_helper_proc (UINT uID, UINT uMsg, DWORD dwUser,
|
|
404 DWORD dw1, DWORD dw2)
|
|
405 {
|
|
406 /* Just raise the signal indicated by the dwUser parameter */
|
|
407 mswindows_raise (dwUser);
|
|
408 }
|
|
409
|
|
410 /* Divide time in ms specified by IT by DENOM. Return 1 ms
|
|
411 if division results in zero */
|
|
412 static UINT
|
|
413 setitimer_helper_period (const struct itimerval* it, UINT denom)
|
|
414 {
|
|
415 static TIMECAPS time_caps;
|
|
416
|
|
417 UINT res;
|
|
418 const struct timeval* tv =
|
|
419 (it->it_value.tv_sec == 0 && it->it_value.tv_usec == 0)
|
|
420 ? &it->it_interval : &it->it_value;
|
|
421
|
|
422 /* Zero means stop timer */
|
|
423 if (tv->tv_sec == 0 && tv->tv_usec == 0)
|
|
424 return 0;
|
|
425
|
|
426 /* Convert to ms and divide by denom */
|
|
427 res = (tv->tv_sec * 1000 + (tv->tv_usec + 500) / 1000) / denom;
|
|
428
|
|
429 /* Converge to minimum timer resolution */
|
|
430 if (time_caps.wPeriodMin == 0)
|
|
431 timeGetDevCaps (&time_caps, sizeof(time_caps));
|
|
432
|
|
433 if (res < time_caps.wPeriodMin)
|
|
434 res = time_caps.wPeriodMin;
|
|
435
|
|
436 return res;
|
|
437 }
|
|
438
|
|
439 static int
|
|
440 setitimer_helper (const struct itimerval* itnew,
|
|
441 struct itimerval* itold, struct itimerval* itcurrent,
|
|
442 MMRESULT* tid, DWORD sigkind)
|
|
443 {
|
|
444 UINT delay, resolution, event_type;
|
|
445
|
|
446 /* First stop the old timer */
|
|
447 if (*tid)
|
|
448 {
|
|
449 timeKillEvent (*tid);
|
|
450 timeEndPeriod (setitimer_helper_period (itcurrent,
|
|
451 setitimer_helper_timer_prec));
|
|
452 *tid = 0;
|
|
453 }
|
|
454
|
|
455 /* Return old itimerval if requested */
|
|
456 if (itold)
|
|
457 *itold = *itcurrent;
|
|
458
|
|
459 *itcurrent = *itnew;
|
|
460
|
|
461 /* Determine if to start new timer */
|
|
462 delay = setitimer_helper_period (itnew, 1);
|
|
463 if (delay)
|
|
464 {
|
|
465 resolution = setitimer_helper_period (itnew,
|
|
466 setitimer_helper_timer_prec);
|
|
467 event_type = (itnew->it_value.tv_sec == 0 &&
|
|
468 itnew->it_value.tv_usec == 0)
|
|
469 ? TIME_ONESHOT : TIME_PERIODIC;
|
|
470 timeBeginPeriod (resolution);
|
|
471 *tid = timeSetEvent (delay, resolution, setitimer_helper_proc, sigkind,
|
|
472 event_type);
|
|
473 }
|
|
474
|
|
475 return !delay || *tid;
|
|
476 }
|
|
477
|
|
478 int
|
|
479 mswindows_setitimer (int kind, const struct itimerval *itnew,
|
|
480 struct itimerval *itold)
|
|
481 {
|
|
482 /* In this version, both interval and value are allowed
|
|
483 only if they are equal. */
|
|
484 assert ((itnew->it_value.tv_sec == 0 && itnew->it_value.tv_usec == 0)
|
|
485 || (itnew->it_interval.tv_sec == 0 &&
|
|
486 itnew->it_interval.tv_usec == 0)
|
|
487 || (itnew->it_value.tv_sec == itnew->it_interval.tv_sec &&
|
|
488 itnew->it_value.tv_usec == itnew->it_interval.tv_usec));
|
|
489
|
|
490 if (kind == ITIMER_REAL)
|
|
491 return setitimer_helper (itnew, itold, &it_alarm, &tid_alarm, SIGALRM);
|
|
492 else if (kind == ITIMER_PROF)
|
|
493 return setitimer_helper (itnew, itold, &it_prof, &tid_prof, SIGPROF);
|
|
494 else
|
|
495 return errno = EINVAL;
|
|
496 }
|
|
497
|
613
|
498 #endif /* defined (WIN32_NATIVE) || defined (CYGWIN_BROKEN_SIGNALS) */
|
|
499
|
611
|
500
|
442
|
501 void
|
|
502 syms_of_win32 (void)
|
|
503 {
|
|
504 DEFSUBR (Fmswindows_shell_execute);
|
|
505 }
|
|
506
|
|
507 void
|
|
508 init_win32 (void)
|
|
509 {
|
|
510 init_potentially_nonexistent_functions ();
|
|
511 }
|