(Qforeground_color, Qbackground_color): Declare.
[bpt/emacs.git] / src / dosfns.c
CommitLineData
1b94449f
RS
1/* MS-DOS specific Lisp utilities. Coded by Manabu Higashida, 1991.
2 Major changes May-July 1993 Morten Welinder (only 10% original code left)
e089dc62 3 Copyright (C) 1991, 1993, 1996, 1997 Free Software Foundation, Inc.
1b94449f
RS
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
edfc0d45 9the Free Software Foundation; either version 2, or (at your option)
1b94449f
RS
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
1b94449f
RS
21
22
48984716 23#include <config.h>
1b94449f
RS
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"
838a94b8
EZ
35#include "blockinput.h"
36#include "window.h"
1b94449f
RS
37#include "dosfns.h"
38#include "msdos.h"
838a94b8 39#include <dpmi.h>
5f08dc78 40#include <go32.h>
50666766 41#include <dirent.h>
1b94449f 42
838a94b8
EZ
43#ifndef __DJGPP_MINOR__
44# define __tb _go32_info_block.linear_address_of_transfer_buffer;
45#endif
46
1b94449f
RS
47DEFUN ("int86", Fint86, Sint86, 2, 2, 0,
48 "Call specific MSDOS interrupt number INTERRUPT with REGISTERS.\n\
49Return the updated REGISTER vector.\n\
50\n\
51INTERRUPT should be an integer in the range 0 to 255.\n\
52REGISTERS should be a vector produced by `make-register' and\n\
53`set-register-value'.")
e20104ba
EN
54 (interrupt, registers)
55 Lisp_Object interrupt, registers;
1b94449f
RS
56{
57 register int i;
58 int no;
59 union REGS inregs, outregs;
60 Lisp_Object val;
61
e20104ba
EN
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)
1b94449f
RS
66 return Qnil;
67 for (i = 0; i < 8; i++)
e20104ba 68 CHECK_NUMBER (XVECTOR (registers)->contents[i], 1);
1b94449f 69
e20104ba
EN
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]);
1b94449f
RS
78
79 int86 (no, &inregs, &outregs);
80
e20104ba
EN
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);
1b94449f 89
e20104ba 90 return registers;
1b94449f
RS
91}
92
5f08dc78
KS
93DEFUN ("msdos-memget", Fdos_memget, Sdos_memget, 2, 2, 0,
94 "Read DOS memory at offset ADDRESS into VECTOR.\n\
95Return the updated VECTOR.")
e20104ba
EN
96 (address, vector)
97 Lisp_Object address, vector;
5f08dc78
KS
98{
99 register int i;
100 int offs, len;
101 char *buf;
102 Lisp_Object val;
103
e20104ba
EN
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)
5f08dc78
KS
109 return Qnil;
110 buf = alloca (len);
111 dosmemget (offs, len, buf);
112
113 for (i = 0; i < len; i++)
e20104ba 114 XVECTOR (vector)->contents[i] = make_number (buf[i]);
5f08dc78 115
e20104ba 116 return vector;
5f08dc78
KS
117}
118
119DEFUN ("msdos-memput", Fdos_memput, Sdos_memput, 2, 2, 0,
120 "Write DOS memory at offset ADDRESS from VECTOR.")
e20104ba
EN
121 (address, vector)
122 Lisp_Object address, vector;
5f08dc78
KS
123{
124 register int i;
125 int offs, len;
126 char *buf;
127 Lisp_Object val;
128
e20104ba
EN
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)
5f08dc78
KS
134 return Qnil;
135 buf = alloca (len);
136
137 for (i = 0; i < len; i++)
138 {
e20104ba
EN
139 CHECK_NUMBER (XVECTOR (vector)->contents[i], 1);
140 buf[i] = (unsigned char) XFASTINT (XVECTOR (vector)->contents[i]) & 0xFF;
5f08dc78
KS
141 }
142
143 dosmemput (buf, len, offs);
144 return Qt;
145}
146
147DEFUN ("msdos-set-keyboard", Fmsdos_set_keyboard, Smsdos_set_keyboard, 1, 2, 0,
e20104ba 148 "Set keyboard layout according to COUNTRY-CODE.\n\
5f08dc78
KS
149If the optional argument ALLKEYS is non-nil, the keyboard is mapped for\n\
150all keys; otherwise it is only used when the ALT key is pressed.\n\
151The 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
87485d6f
MW
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
165DEFUN ("msdos-mouse-p", Fmsdos_mouse_p, Smsdos_mouse_p, 0, 0, 0, "\
166Report whether a mouse is present.")
167 ()
168{
169 if (have_mouse)
170 return Qt;
171 else
172 return Qnil;
173}
174
87485d6f
MW
175/* Function to translate colour names to integers. See lisp/term/pc-win.el
176 for its definition. */
177
178Lisp_Object Qmsdos_color_translate;
179#endif
180
ac3b0279
RS
181
182DEFUN ("msdos-mouse-init", Fmsdos_mouse_init, Smsdos_mouse_init, 0, 0, "",
183 "Initialize and enable mouse if available.")
184 ()
185{
5f08dc78
KS
186 if (have_mouse)
187 {
188 have_mouse = 1;
189 mouse_init ();
190 return Qt;
191 }
ac3b0279
RS
192 return Qnil;
193}
194
195DEFUN ("msdos-mouse-enable", Fmsdos_mouse_enable, Smsdos_mouse_enable, 0, 0, "",
196 "Enable mouse if available.")
197 ()
198{
199 if (have_mouse)
200 {
5f08dc78
KS
201 have_mouse = 1;
202 mouse_on ();
ac3b0279
RS
203 }
204 return have_mouse ? Qt : Qnil;
205}
206
207DEFUN ("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
5f08dc78
KS
216DEFUN ("insert-startup-screen", Finsert_startup_screen, Sinsert_startup_screen, 0, 0, "", "\
217Insert copy of screen contents prior to starting emacs.\n\
218Return 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 {
65788bc2 232 insert_char (*s);
5f08dc78
KS
233 s += 2;
234 }
65788bc2 235 insert_char ('\n');
5f08dc78
KS
236 }
237
238 return Qt;
239}
87485d6f 240\f
5f08dc78 241/* country info */
1b94449f
RS
242int dos_country_code;
243int dos_codepage;
5f08dc78
KS
244int dos_timezone_offset;
245int dos_decimal_point;
246int dos_keyboard_layout;
247unsigned char dos_country_info[DOS_COUNTRY_INFO];
e089dc62
RS
248static 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};
5f08dc78
KS
262
263int dos_hyper_key;
264int dos_super_key;
265int dos_keypad_mode;
1b94449f 266
5f08dc78
KS
267Lisp_Object Vdos_version;
268Lisp_Object Vdos_display_scancodes;
838a94b8
EZ
269
270#ifndef HAVE_X_WINDOWS
271static unsigned dos_windows_version;
272Lisp_Object Vdos_windows_version;
273
274char parent_vm_title[50]; /* Ralf Brown says 30 is enough */
275int w95_set_virtual_machine_title (const char *);
276
277void
278restore_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 */
5f08dc78 287
1b94449f
RS
288void
289init_dosfns ()
290{
291 union REGS regs;
1b94449f 292 _go32_dpmi_registers dpmiregs;
e089dc62 293 unsigned long xbuf = _go32_info_block.linear_address_of_transfer_buffer;
1b94449f 294
87485d6f 295#ifndef SYSTEM_MALLOC
1b94449f 296 get_lim_data (); /* why the hell isn't this called elsewhere? */
87485d6f 297#endif
1b94449f
RS
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
e089dc62
RS
303 /* Obtain the country code via DPMI, use DJGPP transfer buffer. */
304 dpmiregs.x.ax = 0x3800;
7a2fd369 305 dpmiregs.x.ds = xbuf >> 4;
e089dc62
RS
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 }
1b94449f
RS
314 else
315 {
1b94449f 316 dos_country_code = dpmiregs.x.bx;
e089dc62 317 dosmemget (xbuf, DOS_COUNTRY_INFO, dos_country_info);
1b94449f 318 }
e089dc62 319
5f08dc78 320 dos_set_keyboard (dos_country_code, 0);
1b94449f
RS
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;
50666766 338
838a94b8
EZ
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
50666766
RS
387#if __DJGPP__ >= 2
388
459f4042
RS
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). */
50666766
RS
397 if (!NILP (Fmsdos_long_file_names ()))
398 __opendir_flags |= __OPENDIR_PRESERVE_CASE;
459f4042
RS
399#endif /* __DJGPP_MINOR__ == 0 */
400#endif /* __DJGPP__ >= 2 */
1b94449f
RS
401}
402\f
838a94b8
EZ
403#ifndef HAVE_X_WINDOWS
404/* Support for features that are available when we run in a DOS box
405 on MS-Windows. */
406int
407ms_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
415int
416w95_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
441void
442x_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
466void
467dos_cleanup (void)
468{
469#ifndef HAVE_X_WINDOWS
470 restore_parent_vm_title ();
471#endif
472}
473
1b94449f
RS
474/*
475 * Define everything
476 */
477syms_of_dosfns ()
478{
1b94449f 479 defsubr (&Sint86);
5f08dc78
KS
480 defsubr (&Sdos_memget);
481 defsubr (&Sdos_memput);
ac3b0279
RS
482 defsubr (&Smsdos_mouse_init);
483 defsubr (&Smsdos_mouse_enable);
5f08dc78
KS
484 defsubr (&Smsdos_set_keyboard);
485 defsubr (&Sinsert_startup_screen);
ac3b0279 486 defsubr (&Smsdos_mouse_disable);
87485d6f
MW
487#ifndef HAVE_X_WINDOWS
488 defsubr (&Smsdos_mouse_p);
87485d6f
MW
489 Qmsdos_color_translate = intern ("msdos-color-translate");
490 staticpro (&Qmsdos_color_translate);
491#endif
1b94449f
RS
492
493 DEFVAR_INT ("dos-country-code", &dos_country_code,
494 "The country code returned by Dos when Emacs was started.\n\
495Usually this is the international telephone prefix.");
496
497 DEFVAR_INT ("dos-codepage", &dos_codepage,
5f08dc78 498 "The codepage active when Emacs was started.\n\
24ba360b
RS
499The following are known:\n\
500 437 United States\n\
501 850 Multilingual (Latin I)\n\
502 852 Slavic (Latin II)\n\
503 857 Turkish\n\
504 860 Portugal\n\
505 861 Iceland\n\
506 863 Canada (French)\n\
1b94449f
RS
507 865 Norway/Denmark");
508
5f08dc78
KS
509 DEFVAR_INT ("dos-timezone-offset", &dos_timezone_offset,
510 "The current timezone offset to UTC in minutes.
511Implicitly modified when the TZ variable is changed.");
512
1b94449f
RS
513 DEFVAR_LISP ("dos-version", &Vdos_version,
514 "The (MAJOR . MINOR) Dos version (subject to modification with setver).");
5f08dc78 515
838a94b8
EZ
516#ifndef HAVE_X_WINDOWS
517 DEFVAR_LISP ("dos-windows-version", &Vdos_windows_version,
518 "The (MAJOR . MINOR) Windows version for DOS session on MS-Windows.");
519#endif
520
5f08dc78
KS
521 DEFVAR_LISP ("dos-display-scancodes", &Vdos_display_scancodes,
522 "*When non-nil, the keyboard scan-codes are displayed at the bottom right\n\
523corner of the display (typically at the end of the mode line).\n\
524The output format is: scan code:char code*modifiers.");
525 Vdos_display_scancodes = Qnil;
526
5f08dc78
KS
527 DEFVAR_INT ("dos-hyper-key", &dos_hyper_key,
528 "*If set to 1, use right ALT key as hyper key.\n\
529If set to 2, use right CTRL key as hyper key.");
530 dos_hyper_key = 0;
531
532 DEFVAR_INT ("dos-super-key", &dos_super_key,
533 "*If set to 1, use right ALT key as super key.\n\
534If set to 2, use right CTRL key as super key.");
535 dos_super_key = 0;
536
537 DEFVAR_INT ("dos-keypad-mode", &dos_keypad_mode,
538 "*Controls what key code is returned by a key in the numeric keypad.\n\
539The `numlock ON' action is only taken if no modifier keys are pressed.\n\
540The value is an integer constructed by adding the following bits together:\n\
541 \n\
542 0x00 Digit key returns digit (if numlock ON)\n\
543 0x01 Digit key returns kp-digit (if numlock ON)\n\
544 0x02 Digit key returns M-digit (if numlock ON)\n\
545 0x03 Digit key returns edit key (if numlock ON)\n\
546 \n\
547 0x00 Grey key returns char (if numlock ON)\n\
548 0x04 Grey key returns kp-key (if numlock ON)\n\
549 \n\
550 0x00 Digit key returns digit (if numlock OFF)\n\
551 0x10 Digit key returns kp-digit (if numlock OFF)\n\
552 0x20 Digit key returns M-digit (if numlock OFF)\n\
553 0x30 Digit key returns edit key (if numlock OFF)\n\
554 \n\
555 0x00 Grey key returns char (if numlock OFF)\n\
556 0x40 Grey key returns kp-key (if numlock OFF)\n\
557 \n\
558 0x200 ALT-0..ALT-9 in top-row produces shifted codes.");
b3d5621c 559 dos_keypad_mode = 0x75;
5f08dc78
KS
560
561 DEFVAR_INT ("dos-keyboard-layout", &dos_keyboard_layout,
562 "Contains the country code for the current keyboard layout.\n\
563Use msdos-set-keyboard to select another keyboard layout.");
564 dos_keyboard_layout = 1; /* US */
565
566 DEFVAR_INT ("dos-decimal-point", &dos_decimal_point,
567 "If non-zero, it contains the character to be returned when the\n\
568decimal point key in the numeric keypad is pressed when Num Lock is on.\n\
569If zero, the decimal point key returns the country code specific value.");
570 dos_decimal_point = 0;
1b94449f
RS
571}
572#endif /* MSDOS */