*** empty log message ***
[bpt/emacs.git] / src / dosfns.c
1 /* MS-DOS specific Lisp utilities. Coded by Manabu Higashida, 1991.
2 Major changes May-July 1993 Morten Welinder (only 10% original code left)
3 Copyright (C) 1991, 1993, 1996, 1997 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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 2, or (at your option)
10 any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include <config.h>
24
25 #ifdef MSDOS
26 /* The entire file is within this conditional */
27
28 #include <stdio.h>
29 #include <dos.h>
30 #include "lisp.h"
31 #include "buffer.h"
32 #include "termchar.h"
33 #include "termhooks.h"
34 #include "frame.h"
35 #include "blockinput.h"
36 #include "window.h"
37 #include "dosfns.h"
38 #include "msdos.h"
39 #include <dpmi.h>
40 #include <go32.h>
41 #include <dirent.h>
42
43 #ifndef __DJGPP_MINOR__
44 # define __tb _go32_info_block.linear_address_of_transfer_buffer;
45 #endif
46
47 DEFUN ("int86", Fint86, Sint86, 2, 2, 0,
48 "Call specific MSDOS interrupt number INTERRUPT with REGISTERS.\n\
49 Return the updated REGISTER vector.\n\
50 \n\
51 INTERRUPT should be an integer in the range 0 to 255.\n\
52 REGISTERS should be a vector produced by `make-register' and\n\
53 `set-register-value'.")
54 (interrupt, registers)
55 Lisp_Object interrupt, registers;
56 {
57 register int i;
58 int no;
59 union REGS inregs, outregs;
60 Lisp_Object val;
61
62 CHECK_NUMBER (interrupt, 0);
63 no = (unsigned long) XINT (interrupt);
64 CHECK_VECTOR (registers, 1);
65 if (no < 0 || no > 0xff || XVECTOR (registers)-> size != 8)
66 return Qnil;
67 for (i = 0; i < 8; i++)
68 CHECK_NUMBER (XVECTOR (registers)->contents[i], 1);
69
70 inregs.x.ax = (unsigned long) XFASTINT (XVECTOR (registers)->contents[0]);
71 inregs.x.bx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[1]);
72 inregs.x.cx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[2]);
73 inregs.x.dx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[3]);
74 inregs.x.si = (unsigned long) XFASTINT (XVECTOR (registers)->contents[4]);
75 inregs.x.di = (unsigned long) XFASTINT (XVECTOR (registers)->contents[5]);
76 inregs.x.cflag = (unsigned long) XFASTINT (XVECTOR (registers)->contents[6]);
77 inregs.x.flags = (unsigned long) XFASTINT (XVECTOR (registers)->contents[7]);
78
79 int86 (no, &inregs, &outregs);
80
81 XVECTOR (registers)->contents[0] = make_number (outregs.x.ax);
82 XVECTOR (registers)->contents[1] = make_number (outregs.x.bx);
83 XVECTOR (registers)->contents[2] = make_number (outregs.x.cx);
84 XVECTOR (registers)->contents[3] = make_number (outregs.x.dx);
85 XVECTOR (registers)->contents[4] = make_number (outregs.x.si);
86 XVECTOR (registers)->contents[5] = make_number (outregs.x.di);
87 XVECTOR (registers)->contents[6] = make_number (outregs.x.cflag);
88 XVECTOR (registers)->contents[7] = make_number (outregs.x.flags);
89
90 return registers;
91 }
92
93 DEFUN ("msdos-memget", Fdos_memget, Sdos_memget, 2, 2, 0,
94 "Read DOS memory at offset ADDRESS into VECTOR.\n\
95 Return the updated VECTOR.")
96 (address, vector)
97 Lisp_Object address, vector;
98 {
99 register int i;
100 int offs, len;
101 char *buf;
102 Lisp_Object val;
103
104 CHECK_NUMBER (address, 0);
105 offs = (unsigned long) XINT (address);
106 CHECK_VECTOR (vector, 1);
107 len = XVECTOR (vector)-> size;
108 if (len < 1 || len > 2048 || address < 0 || address > 0xfffff - len)
109 return Qnil;
110 buf = alloca (len);
111 dosmemget (offs, len, buf);
112
113 for (i = 0; i < len; i++)
114 XVECTOR (vector)->contents[i] = make_number (buf[i]);
115
116 return vector;
117 }
118
119 DEFUN ("msdos-memput", Fdos_memput, Sdos_memput, 2, 2, 0,
120 "Write DOS memory at offset ADDRESS from VECTOR.")
121 (address, vector)
122 Lisp_Object address, vector;
123 {
124 register int i;
125 int offs, len;
126 char *buf;
127 Lisp_Object val;
128
129 CHECK_NUMBER (address, 0);
130 offs = (unsigned long) XINT (address);
131 CHECK_VECTOR (vector, 1);
132 len = XVECTOR (vector)-> size;
133 if (len < 1 || len > 2048 || address < 0 || address > 0xfffff - len)
134 return Qnil;
135 buf = alloca (len);
136
137 for (i = 0; i < len; i++)
138 {
139 CHECK_NUMBER (XVECTOR (vector)->contents[i], 1);
140 buf[i] = (unsigned char) XFASTINT (XVECTOR (vector)->contents[i]) & 0xFF;
141 }
142
143 dosmemput (buf, len, offs);
144 return Qt;
145 }
146
147 DEFUN ("msdos-set-keyboard", Fmsdos_set_keyboard, Smsdos_set_keyboard, 1, 2, 0,
148 "Set keyboard layout according to COUNTRY-CODE.\n\
149 If the optional argument ALLKEYS is non-nil, the keyboard is mapped for\n\
150 all keys; otherwise it is only used when the ALT key is pressed.\n\
151 The current keyboard layout is available in dos-keyboard-code.")
152 (country_code, allkeys)
153 Lisp_Object country_code;
154 {
155 CHECK_NUMBER (country_code, 0);
156 if (!dos_set_keyboard (XINT (country_code), !NILP (allkeys)))
157 return Qnil;
158 return Qt;
159 }
160
161 #ifndef HAVE_X_WINDOWS
162 /* Later we might want to control the mouse interface with this function,
163 e.g., with respect to non-80 column screen modes. */
164
165 DEFUN ("msdos-mouse-p", Fmsdos_mouse_p, Smsdos_mouse_p, 0, 0, 0, "\
166 Report whether a mouse is present.")
167 ()
168 {
169 if (have_mouse)
170 return Qt;
171 else
172 return Qnil;
173 }
174
175 /* Function to translate colour names to integers. See lisp/term/pc-win.el
176 for its definition. */
177
178 Lisp_Object Qmsdos_color_translate;
179 #endif
180
181
182 DEFUN ("msdos-mouse-init", Fmsdos_mouse_init, Smsdos_mouse_init, 0, 0, "",
183 "Initialize and enable mouse if available.")
184 ()
185 {
186 if (have_mouse)
187 {
188 have_mouse = 1;
189 mouse_init ();
190 return Qt;
191 }
192 return Qnil;
193 }
194
195 DEFUN ("msdos-mouse-enable", Fmsdos_mouse_enable, Smsdos_mouse_enable, 0, 0, "",
196 "Enable mouse if available.")
197 ()
198 {
199 if (have_mouse)
200 {
201 have_mouse = 1;
202 mouse_on ();
203 }
204 return have_mouse ? Qt : Qnil;
205 }
206
207 DEFUN ("msdos-mouse-disable", Fmsdos_mouse_disable, Smsdos_mouse_disable, 0, 0, "",
208 "Disable mouse if available.")
209 ()
210 {
211 mouse_off ();
212 if (have_mouse) have_mouse = -1;
213 return Qnil;
214 }
215
216 DEFUN ("insert-startup-screen", Finsert_startup_screen, Sinsert_startup_screen, 0, 0, "", "\
217 Insert copy of screen contents prior to starting emacs.\n\
218 Return nil if startup screen is not available.")
219 ()
220 {
221 char *s;
222 int rows, cols;
223 int i, j;
224
225 if (!dos_get_saved_screen (&s, &rows, &cols))
226 return Qnil;
227
228 for (i = 0; i < rows; i++)
229 {
230 for (j = 0; j < cols; j++)
231 {
232 insert_char (*s);
233 s += 2;
234 }
235 insert_char ('\n');
236 }
237
238 return Qt;
239 }
240 \f
241 /* country info */
242 int dos_country_code;
243 int dos_codepage;
244 int dos_timezone_offset;
245 int dos_decimal_point;
246 int dos_keyboard_layout;
247 unsigned char dos_country_info[DOS_COUNTRY_INFO];
248 static unsigned char usa_country_info[DOS_COUNTRY_INFO] = {
249 0, 0, /* date format */
250 '$', 0, 0, 0, 0, /* currency string */
251 ',', 0, /* thousands separator */
252 '.', 0, /* decimal separator */
253 '/', 0, /* date separator */
254 ':', 0, /* time separator */
255 0, /* currency format */
256 2, /* digits after decimal in currency */
257 0, /* time format */
258 0, 0, 0, 0, /* address of case map routine, GPF if used */
259 ' ', 0, /* data-list separator (?) */
260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* reserved */
261 };
262
263 int dos_hyper_key;
264 int dos_super_key;
265 int dos_keypad_mode;
266
267 Lisp_Object Vdos_version;
268 Lisp_Object Vdos_display_scancodes;
269
270 #ifndef HAVE_X_WINDOWS
271 static unsigned dos_windows_version;
272 Lisp_Object Vdos_windows_version;
273
274 char parent_vm_title[50]; /* Ralf Brown says 30 is enough */
275 int w95_set_virtual_machine_title (const char *);
276
277 void
278 restore_parent_vm_title (void)
279 {
280 if (NILP (Vdos_windows_version))
281 return;
282 if ((dos_windows_version & 0xff) >= 4 && parent_vm_title[0])
283 w95_set_virtual_machine_title (parent_vm_title);
284 delay (50);
285 }
286 #endif /* !HAVE_X_WINDOWS */
287
288 void
289 init_dosfns ()
290 {
291 union REGS regs;
292 _go32_dpmi_registers dpmiregs;
293 unsigned long xbuf = _go32_info_block.linear_address_of_transfer_buffer;
294
295 #ifndef SYSTEM_MALLOC
296 get_lim_data (); /* why the hell isn't this called elsewhere? */
297 #endif
298
299 regs.x.ax = 0x3000;
300 intdos (&regs, &regs);
301 Vdos_version = Fcons (make_number (regs.h.al), make_number (regs.h.ah));
302
303 /* Obtain the country code via DPMI, use DJGPP transfer buffer. */
304 dpmiregs.x.ax = 0x3800;
305 dpmiregs.x.ds = xbuf >> 4;
306 dpmiregs.x.dx = 0;
307 dpmiregs.x.ss = dpmiregs.x.sp = dpmiregs.x.flags = 0;
308 _go32_dpmi_simulate_int (0x21, &dpmiregs);
309 if (dpmiregs.x.flags & 1)
310 {
311 dos_country_code = 1; /* assume USA if 213800 failed */
312 memcpy (dos_country_info, usa_country_info, DOS_COUNTRY_INFO);
313 }
314 else
315 {
316 dos_country_code = dpmiregs.x.bx;
317 dosmemget (xbuf, DOS_COUNTRY_INFO, dos_country_info);
318 }
319
320 dos_set_keyboard (dos_country_code, 0);
321
322 regs.x.ax = 0x6601;
323 intdos (&regs, &regs);
324 if (regs.x.cflag)
325 /* Estimate code page from country code */
326 switch (dos_country_code)
327 {
328 case 45: /* Denmark */
329 case 47: /* Norway */
330 dos_codepage = 865;
331 break;
332 default:
333 /* US */
334 dos_codepage = 437;
335 }
336 else
337 dos_codepage = regs.x.bx & 0xffff;
338
339 #ifndef HAVE_X_WINDOWS
340 parent_vm_title[0] = '\0';
341
342 /* If we are running from DOS box on MS-Windows, get Windows version. */
343 dpmiregs.x.ax = 0x1600; /* enhanced mode installation check */
344 dpmiregs.x.ss = dpmiregs.x.sp = dpmiregs.x.flags = 0;
345 _go32_dpmi_simulate_int (0x2f, &dpmiregs);
346 /* We only support Windows-specific features when we run on Windows 9X
347 or on Windows 3.X/enhanced mode.
348
349 Int 2Fh/AX=1600h returns:
350
351 AL = 00: no Windows at all;
352 AL = 01: Windows/386 2.x;
353 AL = 80h: Windows 3.x in mode other than enhanced;
354 AL = FFh: Windows/386 2.x
355
356 We also check AH > 0 (Windows 3.1 or later), in case AL tricks us. */
357 if (dpmiregs.h.al > 2 && dpmiregs.h.al != 0x80 && dpmiregs.h.al != 0xff
358 && (dpmiregs.h.al > 3 || dpmiregs.h.ah > 0))
359 {
360 dos_windows_version = dpmiregs.x.ax;
361 Vdos_windows_version =
362 Fcons (make_number (dpmiregs.h.al), make_number (dpmiregs.h.ah));
363
364 /* Save the current title of this virtual machine, so we can restore
365 it before exiting. Otherwise, Windows 95 will continue to use
366 the title we set even after we are history, stupido... */
367 if (dpmiregs.h.al >= 4)
368 {
369 dpmiregs.x.ax = 0x168e;
370 dpmiregs.x.dx = 3; /* get VM title */
371 dpmiregs.x.cx = sizeof(parent_vm_title) - 1;
372 dpmiregs.x.es = __tb >> 4;
373 dpmiregs.x.di = __tb & 15;
374 dpmiregs.x.sp = dpmiregs.x.ss = dpmiregs.x.flags = 0;
375 _go32_dpmi_simulate_int (0x2f, &dpmiregs);
376 if (dpmiregs.x.ax == 1)
377 dosmemget (__tb, sizeof(parent_vm_title), parent_vm_title);
378 }
379 }
380 else
381 {
382 dos_windows_version = 0;
383 Vdos_windows_version = Qnil;
384 }
385 #endif /* !HAVE_X_WINDOWS */
386
387 #if __DJGPP__ >= 2
388
389 /* Without this, we never see hidden files.
390 Don't OR it with the previous value, so the value recorded at dump
391 time, possibly with `preserve-case' flags set, won't get through. */
392 __opendir_flags = __OPENDIR_FIND_HIDDEN;
393
394 #if __DJGPP_MINOR__ == 0
395 /* Under LFN, preserve the case of files as recorded in the directory
396 (in DJGPP 2.01 and later this is automagically done by the library). */
397 if (!NILP (Fmsdos_long_file_names ()))
398 __opendir_flags |= __OPENDIR_PRESERVE_CASE;
399 #endif /* __DJGPP_MINOR__ == 0 */
400 #endif /* __DJGPP__ >= 2 */
401 }
402 \f
403 #ifndef HAVE_X_WINDOWS
404 /* Support for features that are available when we run in a DOS box
405 on MS-Windows. */
406 int
407 ms_windows_version (void)
408 {
409 return dos_windows_version;
410 }
411
412 /* Set the title of the current virtual machine, to appear on
413 the caption bar of that machine's window. */
414
415 int
416 w95_set_virtual_machine_title (const char *title_string)
417 {
418 /* Only Windows 9X (version 4 and higher) support this function. */
419 if (!NILP (Vdos_windows_version)
420 && (dos_windows_version & 0xff) >= 4)
421 {
422 _go32_dpmi_registers regs;
423 dosmemput (title_string, strlen (title_string) + 1, __tb);
424 regs.x.ax = 0x168e;
425 regs.x.dx = 1;
426 regs.x.es = __tb >> 4;
427 regs.x.di = __tb & 15;
428 regs.x.sp = regs.x.ss = regs.x.flags = 0;
429 _go32_dpmi_simulate_int (0x2f, &regs);
430 return regs.x.ax == 1;
431 }
432 return 0;
433 }
434
435 /* Change the title of frame F to NAME.
436 If NAME is nil, use the frame name as the title.
437
438 If Emacs is not run from a DOS box on Windows 9X, this only
439 sets the name in the frame struct, but has no other effects. */
440
441 void
442 x_set_title (f, name)
443 struct frame *f;
444 Lisp_Object name;
445 {
446 /* Don't change the title if it's already NAME. */
447 if (EQ (name, f->title))
448 return;
449
450 update_mode_lines = 1;
451
452 f->title = name;
453
454 if (NILP (name))
455 name = f->name;
456
457 if (FRAME_MSDOS_P (f))
458 {
459 BLOCK_INPUT;
460 w95_set_virtual_machine_title (XSTRING (name)->data);
461 UNBLOCK_INPUT;
462 }
463 }
464 #endif /* !HAVE_X_WINDOWS */
465 \f
466 void
467 dos_cleanup (void)
468 {
469 #ifndef HAVE_X_WINDOWS
470 restore_parent_vm_title ();
471 #endif
472 /* Make sure the termscript file is committed, in case we are
473 crashing and some vital info was written there. */
474 if (termscript)
475 {
476 fflush (termscript);
477 fsync (fileno (termscript));
478 }
479 }
480
481 /*
482 * Define everything
483 */
484 syms_of_dosfns ()
485 {
486 defsubr (&Sint86);
487 defsubr (&Sdos_memget);
488 defsubr (&Sdos_memput);
489 defsubr (&Smsdos_mouse_init);
490 defsubr (&Smsdos_mouse_enable);
491 defsubr (&Smsdos_set_keyboard);
492 defsubr (&Sinsert_startup_screen);
493 defsubr (&Smsdos_mouse_disable);
494 #ifndef HAVE_X_WINDOWS
495 defsubr (&Smsdos_mouse_p);
496 Qmsdos_color_translate = intern ("msdos-color-translate");
497 staticpro (&Qmsdos_color_translate);
498 #endif
499
500 DEFVAR_INT ("dos-country-code", &dos_country_code,
501 "The country code returned by Dos when Emacs was started.\n\
502 Usually this is the international telephone prefix.");
503
504 DEFVAR_INT ("dos-codepage", &dos_codepage,
505 "The codepage active when Emacs was started.\n\
506 The following are known:\n\
507 437 United States\n\
508 850 Multilingual (Latin I)\n\
509 852 Slavic (Latin II)\n\
510 857 Turkish\n\
511 860 Portugal\n\
512 861 Iceland\n\
513 863 Canada (French)\n\
514 865 Norway/Denmark");
515
516 DEFVAR_INT ("dos-timezone-offset", &dos_timezone_offset,
517 "The current timezone offset to UTC in minutes.
518 Implicitly modified when the TZ variable is changed.");
519
520 DEFVAR_LISP ("dos-version", &Vdos_version,
521 "The (MAJOR . MINOR) Dos version (subject to modification with setver).");
522
523 #ifndef HAVE_X_WINDOWS
524 DEFVAR_LISP ("dos-windows-version", &Vdos_windows_version,
525 "The (MAJOR . MINOR) Windows version for DOS session on MS-Windows.");
526 #endif
527
528 DEFVAR_LISP ("dos-display-scancodes", &Vdos_display_scancodes,
529 "*When non-nil, the keyboard scan-codes are displayed at the bottom right\n\
530 corner of the display (typically at the end of the mode line).\n\
531 The output format is: scan code:char code*modifiers.");
532 Vdos_display_scancodes = Qnil;
533
534 DEFVAR_INT ("dos-hyper-key", &dos_hyper_key,
535 "*If set to 1, use right ALT key as hyper key.\n\
536 If set to 2, use right CTRL key as hyper key.");
537 dos_hyper_key = 0;
538
539 DEFVAR_INT ("dos-super-key", &dos_super_key,
540 "*If set to 1, use right ALT key as super key.\n\
541 If set to 2, use right CTRL key as super key.");
542 dos_super_key = 0;
543
544 DEFVAR_INT ("dos-keypad-mode", &dos_keypad_mode,
545 "*Controls what key code is returned by a key in the numeric keypad.\n\
546 The `numlock ON' action is only taken if no modifier keys are pressed.\n\
547 The value is an integer constructed by adding the following bits together:\n\
548 \n\
549 0x00 Digit key returns digit (if numlock ON)\n\
550 0x01 Digit key returns kp-digit (if numlock ON)\n\
551 0x02 Digit key returns M-digit (if numlock ON)\n\
552 0x03 Digit key returns edit key (if numlock ON)\n\
553 \n\
554 0x00 Grey key returns char (if numlock ON)\n\
555 0x04 Grey key returns kp-key (if numlock ON)\n\
556 \n\
557 0x00 Digit key returns digit (if numlock OFF)\n\
558 0x10 Digit key returns kp-digit (if numlock OFF)\n\
559 0x20 Digit key returns M-digit (if numlock OFF)\n\
560 0x30 Digit key returns edit key (if numlock OFF)\n\
561 \n\
562 0x00 Grey key returns char (if numlock OFF)\n\
563 0x40 Grey key returns kp-key (if numlock OFF)\n\
564 \n\
565 0x200 ALT-0..ALT-9 in top-row produces shifted codes.");
566 dos_keypad_mode = 0x75;
567
568 DEFVAR_INT ("dos-keyboard-layout", &dos_keyboard_layout,
569 "Contains the country code for the current keyboard layout.\n\
570 Use msdos-set-keyboard to select another keyboard layout.");
571 dos_keyboard_layout = 1; /* US */
572
573 DEFVAR_INT ("dos-decimal-point", &dos_decimal_point,
574 "If non-zero, it contains the character to be returned when the\n\
575 decimal point key in the numeric keypad is pressed when Num Lock is on.\n\
576 If zero, the decimal point key returns the country code specific value.");
577 dos_decimal_point = 0;
578 }
579 #endif /* MSDOS */