(init_dosfns) [DJGPP >= 2]: Make `opendir' preserve
[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 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 "dosfns.h"
36 #include "msdos.h"
37 #include <go32.h>
38 #include <dirent.h>
39
40 DEFUN ("int86", Fint86, Sint86, 2, 2, 0,
41 "Call specific MSDOS interrupt number INTERRUPT with REGISTERS.\n\
42 Return the updated REGISTER vector.\n\
43 \n\
44 INTERRUPT should be an integer in the range 0 to 255.\n\
45 REGISTERS should be a vector produced by `make-register' and\n\
46 `set-register-value'.")
47 (interrupt, registers)
48 Lisp_Object interrupt, registers;
49 {
50 register int i;
51 int no;
52 union REGS inregs, outregs;
53 Lisp_Object val;
54
55 CHECK_NUMBER (interrupt, 0);
56 no = (unsigned long) XINT (interrupt);
57 CHECK_VECTOR (registers, 1);
58 if (no < 0 || no > 0xff || XVECTOR (registers)-> size != 8)
59 return Qnil;
60 for (i = 0; i < 8; i++)
61 CHECK_NUMBER (XVECTOR (registers)->contents[i], 1);
62
63 inregs.x.ax = (unsigned long) XFASTINT (XVECTOR (registers)->contents[0]);
64 inregs.x.bx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[1]);
65 inregs.x.cx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[2]);
66 inregs.x.dx = (unsigned long) XFASTINT (XVECTOR (registers)->contents[3]);
67 inregs.x.si = (unsigned long) XFASTINT (XVECTOR (registers)->contents[4]);
68 inregs.x.di = (unsigned long) XFASTINT (XVECTOR (registers)->contents[5]);
69 inregs.x.cflag = (unsigned long) XFASTINT (XVECTOR (registers)->contents[6]);
70 inregs.x.flags = (unsigned long) XFASTINT (XVECTOR (registers)->contents[7]);
71
72 int86 (no, &inregs, &outregs);
73
74 XVECTOR (registers)->contents[0] = make_number (outregs.x.ax);
75 XVECTOR (registers)->contents[1] = make_number (outregs.x.bx);
76 XVECTOR (registers)->contents[2] = make_number (outregs.x.cx);
77 XVECTOR (registers)->contents[3] = make_number (outregs.x.dx);
78 XVECTOR (registers)->contents[4] = make_number (outregs.x.si);
79 XVECTOR (registers)->contents[5] = make_number (outregs.x.di);
80 XVECTOR (registers)->contents[6] = make_number (outregs.x.cflag);
81 XVECTOR (registers)->contents[7] = make_number (outregs.x.flags);
82
83 return registers;
84 }
85
86 DEFUN ("msdos-memget", Fdos_memget, Sdos_memget, 2, 2, 0,
87 "Read DOS memory at offset ADDRESS into VECTOR.\n\
88 Return the updated VECTOR.")
89 (address, vector)
90 Lisp_Object address, vector;
91 {
92 register int i;
93 int offs, len;
94 char *buf;
95 Lisp_Object val;
96
97 CHECK_NUMBER (address, 0);
98 offs = (unsigned long) XINT (address);
99 CHECK_VECTOR (vector, 1);
100 len = XVECTOR (vector)-> size;
101 if (len < 1 || len > 2048 || address < 0 || address > 0xfffff - len)
102 return Qnil;
103 buf = alloca (len);
104 dosmemget (offs, len, buf);
105
106 for (i = 0; i < len; i++)
107 XVECTOR (vector)->contents[i] = make_number (buf[i]);
108
109 return vector;
110 }
111
112 DEFUN ("msdos-memput", Fdos_memput, Sdos_memput, 2, 2, 0,
113 "Write DOS memory at offset ADDRESS from VECTOR.")
114 (address, vector)
115 Lisp_Object address, vector;
116 {
117 register int i;
118 int offs, len;
119 char *buf;
120 Lisp_Object val;
121
122 CHECK_NUMBER (address, 0);
123 offs = (unsigned long) XINT (address);
124 CHECK_VECTOR (vector, 1);
125 len = XVECTOR (vector)-> size;
126 if (len < 1 || len > 2048 || address < 0 || address > 0xfffff - len)
127 return Qnil;
128 buf = alloca (len);
129
130 for (i = 0; i < len; i++)
131 {
132 CHECK_NUMBER (XVECTOR (vector)->contents[i], 1);
133 buf[i] = (unsigned char) XFASTINT (XVECTOR (vector)->contents[i]) & 0xFF;
134 }
135
136 dosmemput (buf, len, offs);
137 return Qt;
138 }
139
140 DEFUN ("msdos-set-keyboard", Fmsdos_set_keyboard, Smsdos_set_keyboard, 1, 2, 0,
141 "Set keyboard layout according to COUNTRY-CODE.\n\
142 If the optional argument ALLKEYS is non-nil, the keyboard is mapped for\n\
143 all keys; otherwise it is only used when the ALT key is pressed.\n\
144 The current keyboard layout is available in dos-keyboard-code.")
145 (country_code, allkeys)
146 Lisp_Object country_code;
147 {
148 CHECK_NUMBER (country_code, 0);
149 if (!dos_set_keyboard (XINT (country_code), !NILP (allkeys)))
150 return Qnil;
151 return Qt;
152 }
153
154 #ifndef HAVE_X_WINDOWS
155 /* Later we might want to control the mouse interface with this function,
156 e.g., with respect to non-80 column screen modes. */
157
158 DEFUN ("msdos-mouse-p", Fmsdos_mouse_p, Smsdos_mouse_p, 0, 0, 0, "\
159 Report whether a mouse is present.")
160 ()
161 {
162 if (have_mouse)
163 return Qt;
164 else
165 return Qnil;
166 }
167
168 DEFUN ("set-mouse-position", Fset_mouse_position, Sset_mouse_position, 3, 3, 0,
169 "Move the mouse pointer to the center of character cell (X,Y) in FRAME.\n\
170 WARNING: If you use this under X windows,\n\
171 you should call `unfocus-frame' afterwards.")
172 (frame, x, y)
173 Lisp_Object frame, x, y;
174 {
175 mouse_moveto (XINT (x), XINT (y));
176 return Qnil;
177 }
178
179 /* Function to translate colour names to integers. See lisp/term/pc-win.el
180 for its definition. */
181
182 Lisp_Object Qmsdos_color_translate;
183 #endif
184
185
186 DEFUN ("msdos-mouse-init", Fmsdos_mouse_init, Smsdos_mouse_init, 0, 0, "",
187 "Initialize and enable mouse if available.")
188 ()
189 {
190 if (have_mouse)
191 {
192 have_mouse = 1;
193 mouse_init ();
194 return Qt;
195 }
196 return Qnil;
197 }
198
199 DEFUN ("msdos-mouse-enable", Fmsdos_mouse_enable, Smsdos_mouse_enable, 0, 0, "",
200 "Enable mouse if available.")
201 ()
202 {
203 if (have_mouse)
204 {
205 have_mouse = 1;
206 mouse_on ();
207 }
208 return have_mouse ? Qt : Qnil;
209 }
210
211 DEFUN ("msdos-mouse-disable", Fmsdos_mouse_disable, Smsdos_mouse_disable, 0, 0, "",
212 "Disable mouse if available.")
213 ()
214 {
215 mouse_off ();
216 if (have_mouse) have_mouse = -1;
217 return Qnil;
218 }
219
220 DEFUN ("insert-startup-screen", Finsert_startup_screen, Sinsert_startup_screen, 0, 0, "", "\
221 Insert copy of screen contents prior to starting emacs.\n\
222 Return nil if startup screen is not available.")
223 ()
224 {
225 char *s;
226 int rows, cols;
227 int i, j;
228
229 if (!dos_get_saved_screen (&s, &rows, &cols))
230 return Qnil;
231
232 for (i = 0; i < rows; i++)
233 {
234 for (j = 0; j < cols; j++)
235 {
236 insert_char (*s, 1);
237 s += 2;
238 }
239 insert_char ('\n', 1);
240 }
241
242 return Qt;
243 }
244 \f
245 /* country info */
246 int dos_country_code;
247 int dos_codepage;
248 int dos_timezone_offset;
249 int dos_decimal_point;
250 int dos_keyboard_layout;
251 unsigned char dos_country_info[DOS_COUNTRY_INFO];
252
253 int dos_hyper_key;
254 int dos_super_key;
255 int dos_keypad_mode;
256
257 Lisp_Object Vdos_version;
258 Lisp_Object Vdos_display_scancodes;
259
260 void
261 init_dosfns ()
262 {
263 union REGS regs;
264 _go32_dpmi_seginfo info;
265 _go32_dpmi_registers dpmiregs;
266
267 #ifndef SYSTEM_MALLOC
268 get_lim_data (); /* why the hell isn't this called elsewhere? */
269 #endif
270
271 regs.x.ax = 0x3000;
272 intdos (&regs, &regs);
273 Vdos_version = Fcons (make_number (regs.h.al), make_number (regs.h.ah));
274
275 /* Obtain the country code by calling Dos via Dpmi. Don't rely on GO32. */
276 info.size = (sizeof(dos_country_info) + 15) / 16;
277 if (_go32_dpmi_allocate_dos_memory (&info))
278 dos_country_code = 1;
279 else
280 {
281 dpmiregs.x.ax = 0x3800;
282 dpmiregs.x.ds = info.rm_segment;
283 dpmiregs.x.dx = 0;
284 dpmiregs.x.ss = dpmiregs.x.sp = 0;
285 _go32_dpmi_simulate_int (0x21, &dpmiregs);
286 dos_country_code = dpmiregs.x.bx;
287 dosmemget (info.rm_segment * 16, DOS_COUNTRY_INFO, dos_country_info);
288 _go32_dpmi_free_dos_memory (&info);
289 }
290 dos_set_keyboard (dos_country_code, 0);
291
292 regs.x.ax = 0x6601;
293 intdos (&regs, &regs);
294 if (regs.x.cflag)
295 /* Estimate code page from country code */
296 switch (dos_country_code)
297 {
298 case 45: /* Denmark */
299 case 47: /* Norway */
300 dos_codepage = 865;
301 break;
302 default:
303 /* US */
304 dos_codepage = 437;
305 }
306 else
307 dos_codepage = regs.x.bx & 0xffff;
308
309 #if __DJGPP__ >= 2
310
311 /* Without this, we never see hidden files. */
312 __opendir_flags |= __OPENDIR_FIND_HIDDEN;
313
314 /* Under LFN, preserve the case of files as recorded in the directory. */
315 if (!NILP (Fmsdos_long_file_names ()))
316 __opendir_flags |= __OPENDIR_PRESERVE_CASE;
317
318 #endif
319 }
320 \f
321 /*
322 * Define everything
323 */
324 syms_of_dosfns ()
325 {
326 defsubr (&Sint86);
327 defsubr (&Sdos_memget);
328 defsubr (&Sdos_memput);
329 defsubr (&Smsdos_mouse_init);
330 defsubr (&Smsdos_mouse_enable);
331 defsubr (&Smsdos_set_keyboard);
332 defsubr (&Sinsert_startup_screen);
333 defsubr (&Smsdos_mouse_disable);
334 #ifndef HAVE_X_WINDOWS
335 defsubr (&Smsdos_mouse_p);
336 defsubr (&Sset_mouse_position);
337
338 Qmsdos_color_translate = intern ("msdos-color-translate");
339 staticpro (&Qmsdos_color_translate);
340 #endif
341
342 DEFVAR_INT ("dos-country-code", &dos_country_code,
343 "The country code returned by Dos when Emacs was started.\n\
344 Usually this is the international telephone prefix.");
345
346 DEFVAR_INT ("dos-codepage", &dos_codepage,
347 "The codepage active when Emacs was started.\n\
348 The following are known:\n\
349 437 United States\n\
350 850 Multilingual (Latin I)\n\
351 852 Slavic (Latin II)\n\
352 857 Turkish\n\
353 860 Portugal\n\
354 861 Iceland\n\
355 863 Canada (French)\n\
356 865 Norway/Denmark");
357
358 DEFVAR_INT ("dos-timezone-offset", &dos_timezone_offset,
359 "The current timezone offset to UTC in minutes.
360 Implicitly modified when the TZ variable is changed.");
361
362 DEFVAR_LISP ("dos-version", &Vdos_version,
363 "The (MAJOR . MINOR) Dos version (subject to modification with setver).");
364
365 DEFVAR_LISP ("dos-display-scancodes", &Vdos_display_scancodes,
366 "*When non-nil, the keyboard scan-codes are displayed at the bottom right\n\
367 corner of the display (typically at the end of the mode line).\n\
368 The output format is: scan code:char code*modifiers.");
369 Vdos_display_scancodes = Qnil;
370
371 DEFVAR_INT ("dos-hyper-key", &dos_hyper_key,
372 "*If set to 1, use right ALT key as hyper key.\n\
373 If set to 2, use right CTRL key as hyper key.");
374 dos_hyper_key = 0;
375
376 DEFVAR_INT ("dos-super-key", &dos_super_key,
377 "*If set to 1, use right ALT key as super key.\n\
378 If set to 2, use right CTRL key as super key.");
379 dos_super_key = 0;
380
381 DEFVAR_INT ("dos-keypad-mode", &dos_keypad_mode,
382 "*Controls what key code is returned by a key in the numeric keypad.\n\
383 The `numlock ON' action is only taken if no modifier keys are pressed.\n\
384 The value is an integer constructed by adding the following bits together:\n\
385 \n\
386 0x00 Digit key returns digit (if numlock ON)\n\
387 0x01 Digit key returns kp-digit (if numlock ON)\n\
388 0x02 Digit key returns M-digit (if numlock ON)\n\
389 0x03 Digit key returns edit key (if numlock ON)\n\
390 \n\
391 0x00 Grey key returns char (if numlock ON)\n\
392 0x04 Grey key returns kp-key (if numlock ON)\n\
393 \n\
394 0x00 Digit key returns digit (if numlock OFF)\n\
395 0x10 Digit key returns kp-digit (if numlock OFF)\n\
396 0x20 Digit key returns M-digit (if numlock OFF)\n\
397 0x30 Digit key returns edit key (if numlock OFF)\n\
398 \n\
399 0x00 Grey key returns char (if numlock OFF)\n\
400 0x40 Grey key returns kp-key (if numlock OFF)\n\
401 \n\
402 0x200 ALT-0..ALT-9 in top-row produces shifted codes.");
403 dos_keypad_mode = 0x75;
404
405 DEFVAR_INT ("dos-keyboard-layout", &dos_keyboard_layout,
406 "Contains the country code for the current keyboard layout.\n\
407 Use msdos-set-keyboard to select another keyboard layout.");
408 dos_keyboard_layout = 1; /* US */
409
410 DEFVAR_INT ("dos-decimal-point", &dos_decimal_point,
411 "If non-zero, it contains the character to be returned when the\n\
412 decimal point key in the numeric keypad is pressed when Num Lock is on.\n\
413 If zero, the decimal point key returns the country code specific value.");
414 dos_decimal_point = 0;
415 }
416 #endif /* MSDOS */