]> code.delx.au - gnu-emacs/blob - src/msdos.c
Merge from emacs--devo--0
[gnu-emacs] / src / msdos.c
1 /* MS-DOS specific C utilities. -*- coding: raw-text -*-
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 /* Contributed by Morten Welinder */
24 /* New display, keyboard, and mouse control by Kim F. Storm */
25
26 /* Note: some of the stuff here was taken from end of sysdep.c in demacs. */
27
28 #include <config.h>
29
30 #ifdef MSDOS
31 #include "lisp.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <time.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <dos.h>
38 #include <errno.h>
39 #include <string.h> /* for bzero and string functions */
40 #include <sys/stat.h> /* for _fixpath */
41 #include <unistd.h> /* for chdir, dup, dup2, etc. */
42 #include <dir.h> /* for getdisk */
43 #if __DJGPP__ >= 2
44 #pragma pack(0) /* dir.h does a pack(4), which isn't GCC's default */
45 #include <fcntl.h>
46 #include <io.h> /* for setmode */
47 #include <dpmi.h> /* for __dpmi_xxx stuff */
48 #include <sys/farptr.h> /* for _farsetsel, _farnspokeb */
49 #include <libc/dosio.h> /* for _USE_LFN */
50 #include <conio.h> /* for cputs */
51 #endif
52
53 #include "msdos.h"
54 #include "systime.h"
55 #include "termhooks.h"
56 #include "termchar.h"
57 #include "dispextern.h"
58 #include "dosfns.h"
59 #include "termopts.h"
60 #include "character.h"
61 #include "coding.h"
62 #include "disptab.h"
63 #include "frame.h"
64 #include "window.h"
65 #include "buffer.h"
66 #include "commands.h"
67 #include "blockinput.h"
68 #include "keyboard.h"
69 #include "intervals.h"
70 #include <go32.h>
71 #include <pc.h>
72 #include <ctype.h>
73 /* #include <process.h> */
74 /* Damn that local process.h! Instead we can define P_WAIT ourselves. */
75 #define P_WAIT 1
76
77 #ifndef _USE_LFN
78 #define _USE_LFN 0
79 #endif
80
81 #ifndef _dos_ds
82 #define _dos_ds _go32_info_block.selector_for_linear_memory
83 #endif
84
85 #if __DJGPP__ > 1
86
87 #include <signal.h>
88 #include "syssignal.h"
89
90 #ifndef SYSTEM_MALLOC
91
92 #ifdef GNU_MALLOC
93
94 /* If other `malloc' than ours is used, force our `sbrk' behave like
95 Unix programs expect (resize memory blocks to keep them contiguous).
96 If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
97 because that's what `gmalloc' expects to get. */
98 #include <crt0.h>
99
100 #ifdef REL_ALLOC
101 int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
102 #else /* not REL_ALLOC */
103 int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
104 #endif /* not REL_ALLOC */
105 #endif /* GNU_MALLOC */
106
107 #endif /* not SYSTEM_MALLOC */
108 #endif /* __DJGPP__ > 1 */
109
110 static unsigned long
111 event_timestamp ()
112 {
113 struct time t;
114 unsigned long s;
115
116 gettime (&t);
117 s = t.ti_min;
118 s *= 60;
119 s += t.ti_sec;
120 s *= 1000;
121 s += t.ti_hund * 10;
122
123 return s;
124 }
125
126 \f
127 /* ------------------------ Mouse control ---------------------------
128 *
129 * Coordinates are in screen positions and zero based.
130 * Mouse buttons are numbered from left to right and also zero based.
131 */
132
133 /* This used to be in termhooks.h, but mainstream Emacs code no longer
134 uses it, and it was removed... */
135 #define NUM_MOUSE_BUTTONS (5)
136
137 int have_mouse; /* 0: no, 1: enabled, -1: disabled */
138 static int mouse_visible;
139
140 static int mouse_last_x;
141 static int mouse_last_y;
142
143 static int mouse_button_translate[NUM_MOUSE_BUTTONS];
144 static int mouse_button_count;
145
146 void
147 mouse_on ()
148 {
149 union REGS regs;
150
151 if (have_mouse > 0 && !mouse_visible)
152 {
153 if (termscript)
154 fprintf (termscript, "<M_ON>");
155 regs.x.ax = 0x0001;
156 int86 (0x33, &regs, &regs);
157 mouse_visible = 1;
158 }
159 }
160
161 void
162 mouse_off ()
163 {
164 union REGS regs;
165
166 if (have_mouse > 0 && mouse_visible)
167 {
168 if (termscript)
169 fprintf (termscript, "<M_OFF>");
170 regs.x.ax = 0x0002;
171 int86 (0x33, &regs, &regs);
172 mouse_visible = 0;
173 }
174 }
175
176 static void
177 mouse_setup_buttons (int n_buttons)
178 {
179 if (n_buttons == 3)
180 {
181 mouse_button_count = 3;
182 mouse_button_translate[0] = 0; /* Left */
183 mouse_button_translate[1] = 2; /* Middle */
184 mouse_button_translate[2] = 1; /* Right */
185 }
186 else /* two, what else? */
187 {
188 mouse_button_count = 2;
189 mouse_button_translate[0] = 0;
190 mouse_button_translate[1] = 1;
191 }
192 }
193
194 DEFUN ("msdos-set-mouse-buttons", Fmsdos_set_mouse_buttons, Smsdos_set_mouse_buttons,
195 1, 1, "NSet number of mouse buttons to: ",
196 doc: /* Set the number of mouse buttons to use by Emacs.
197 This is useful with mice that report the number of buttons inconsistently,
198 e.g., if the number of buttons is reported as 3, but Emacs only sees 2 of
199 them. This happens with wheeled mice on Windows 9X, for example. */)
200 (nbuttons)
201 Lisp_Object nbuttons;
202 {
203 int n;
204
205 CHECK_NUMBER (nbuttons);
206 n = XINT (nbuttons);
207 if (n < 2 || n > 3)
208 xsignal2 (Qargs_out_of_range,
209 build_string ("only 2 or 3 mouse buttons are supported"),
210 nbuttons);
211 mouse_setup_buttons (n);
212 return Qnil;
213 }
214
215 static void
216 mouse_get_xy (int *x, int *y)
217 {
218 union REGS regs;
219
220 regs.x.ax = 0x0003;
221 int86 (0x33, &regs, &regs);
222 *x = regs.x.cx / 8;
223 *y = regs.x.dx / 8;
224 }
225
226 void
227 mouse_moveto (x, y)
228 int x, y;
229 {
230 union REGS regs;
231
232 if (termscript)
233 fprintf (termscript, "<M_XY=%dx%d>", x, y);
234 regs.x.ax = 0x0004;
235 mouse_last_x = regs.x.cx = x * 8;
236 mouse_last_y = regs.x.dx = y * 8;
237 int86 (0x33, &regs, &regs);
238 }
239
240 static int
241 mouse_pressed (b, xp, yp)
242 int b, *xp, *yp;
243 {
244 union REGS regs;
245
246 if (b >= mouse_button_count)
247 return 0;
248 regs.x.ax = 0x0005;
249 regs.x.bx = mouse_button_translate[b];
250 int86 (0x33, &regs, &regs);
251 if (regs.x.bx)
252 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
253 return (regs.x.bx != 0);
254 }
255
256 static int
257 mouse_released (b, xp, yp)
258 int b, *xp, *yp;
259 {
260 union REGS regs;
261
262 if (b >= mouse_button_count)
263 return 0;
264 regs.x.ax = 0x0006;
265 regs.x.bx = mouse_button_translate[b];
266 int86 (0x33, &regs, &regs);
267 if (regs.x.bx)
268 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
269 return (regs.x.bx != 0);
270 }
271
272 static int
273 mouse_button_depressed (b, xp, yp)
274 int b, *xp, *yp;
275 {
276 union REGS regs;
277
278 if (b >= mouse_button_count)
279 return 0;
280 regs.x.ax = 0x0003;
281 int86 (0x33, &regs, &regs);
282 if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
283 {
284 *xp = regs.x.cx / 8;
285 *yp = regs.x.dx / 8;
286 return 1;
287 }
288 return 0;
289 }
290
291 void
292 mouse_get_pos (f, insist, bar_window, part, x, y, time)
293 FRAME_PTR *f;
294 int insist;
295 Lisp_Object *bar_window, *x, *y;
296 enum scroll_bar_part *part;
297 unsigned long *time;
298 {
299 int ix, iy;
300 Lisp_Object frame, tail;
301
302 /* Clear the mouse-moved flag for every frame on this display. */
303 FOR_EACH_FRAME (tail, frame)
304 XFRAME (frame)->mouse_moved = 0;
305
306 *f = SELECTED_FRAME();
307 *bar_window = Qnil;
308 mouse_get_xy (&ix, &iy);
309 *time = event_timestamp ();
310 *x = make_number (mouse_last_x = ix);
311 *y = make_number (mouse_last_y = iy);
312 }
313
314 static void
315 mouse_check_moved ()
316 {
317 int x, y;
318
319 mouse_get_xy (&x, &y);
320 SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
321 mouse_last_x = x;
322 mouse_last_y = y;
323 }
324
325 /* Force the mouse driver to ``forget'' about any button clicks until
326 now. */
327 static void
328 mouse_clear_clicks (void)
329 {
330 int b;
331
332 for (b = 0; b < mouse_button_count; b++)
333 {
334 int dummy_x, dummy_y;
335
336 (void) mouse_pressed (b, &dummy_x, &dummy_y);
337 (void) mouse_released (b, &dummy_x, &dummy_y);
338 }
339 }
340
341 void
342 mouse_init ()
343 {
344 union REGS regs;
345
346 if (termscript)
347 fprintf (termscript, "<M_INIT>");
348
349 regs.x.ax = 0x0021;
350 int86 (0x33, &regs, &regs);
351
352 /* Reset the mouse last press/release info. It seems that Windows
353 doesn't do that automatically when function 21h is called, which
354 causes Emacs to ``remember'' the click that switched focus to the
355 window just before Emacs was started from that window. */
356 mouse_clear_clicks ();
357
358 regs.x.ax = 0x0007;
359 regs.x.cx = 0;
360 regs.x.dx = 8 * (ScreenCols () - 1);
361 int86 (0x33, &regs, &regs);
362
363 regs.x.ax = 0x0008;
364 regs.x.cx = 0;
365 regs.x.dx = 8 * (ScreenRows () - 1);
366 int86 (0x33, &regs, &regs);
367
368 mouse_moveto (0, 0);
369 mouse_visible = 0;
370 }
371 \f
372 /* ------------------------- Screen control ----------------------
373 *
374 */
375
376 static int internal_terminal = 0;
377
378 #ifndef HAVE_X_WINDOWS
379 extern unsigned char ScreenAttrib;
380 static int screen_face;
381
382 static int screen_size_X;
383 static int screen_size_Y;
384 static int screen_size;
385
386 static int current_pos_X;
387 static int current_pos_Y;
388 static int new_pos_X;
389 static int new_pos_Y;
390
391 static void *startup_screen_buffer;
392 static int startup_screen_size_X;
393 static int startup_screen_size_Y;
394 static int startup_pos_X;
395 static int startup_pos_Y;
396 static unsigned char startup_screen_attrib;
397
398 static clock_t startup_time;
399
400 static int term_setup_done;
401
402 static unsigned short outside_cursor;
403
404 /* Similar to the_only_frame. */
405 struct x_output the_only_x_display;
406
407 /* Support for DOS/V (allows Japanese characters to be displayed on
408 standard, non-Japanese, ATs). Only supported for DJGPP v2 and later. */
409
410 /* Holds the address of the text-mode screen buffer. */
411 static unsigned long screen_old_address = 0;
412 /* Segment and offset of the virtual screen. If 0, DOS/V is NOT loaded. */
413 static unsigned short screen_virtual_segment = 0;
414 static unsigned short screen_virtual_offset = 0;
415 /* A flag to control how to display unibyte 8-bit characters. */
416 extern int unibyte_display_via_language_environment;
417
418 extern Lisp_Object Qcursor_type;
419 extern Lisp_Object Qbar, Qhbar;
420
421 /* The screen colors of the current frame, which serve as the default
422 colors for newly-created frames. */
423 static int initial_screen_colors[2];
424
425 #if __DJGPP__ > 1
426 /* Update the screen from a part of relocated DOS/V screen buffer which
427 begins at OFFSET and includes COUNT characters. */
428 static void
429 dosv_refresh_virtual_screen (int offset, int count)
430 {
431 __dpmi_regs regs;
432
433 if (offset < 0 || count < 0) /* paranoia; invalid values crash DOS/V */
434 return;
435
436 regs.h.ah = 0xff; /* update relocated screen */
437 regs.x.es = screen_virtual_segment;
438 regs.x.di = screen_virtual_offset + offset;
439 regs.x.cx = count;
440 __dpmi_int (0x10, &regs);
441 }
442 #endif
443
444 static void
445 dos_direct_output (y, x, buf, len)
446 int x, y;
447 char *buf;
448 int len;
449 {
450 int t0 = 2 * (x + y * screen_size_X);
451 int t = t0 + (int) ScreenPrimary;
452 int l0 = len;
453
454 #if (__DJGPP__ < 2)
455 while (--len >= 0) {
456 dosmemput (buf++, 1, t);
457 t += 2;
458 }
459 #else
460 /* This is faster. */
461 for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
462 _farnspokeb (t, *buf);
463
464 if (screen_virtual_segment)
465 dosv_refresh_virtual_screen (t0, l0);
466 #endif
467 }
468 #endif
469
470 /* Flash the screen as a substitute for BEEPs. */
471
472 #if (__DJGPP__ < 2)
473 static void
474 do_visible_bell (xorattr)
475 unsigned char xorattr;
476 {
477 asm volatile
478 (" movb $1,%%dl \n\
479 visible_bell_0: \n\
480 movl _ScreenPrimary,%%eax \n\
481 call dosmemsetup \n\
482 movl %%eax,%%ebx \n\
483 movl %1,%%ecx \n\
484 movb %0,%%al \n\
485 incl %%ebx \n\
486 visible_bell_1: \n\
487 xorb %%al,%%gs:(%%ebx) \n\
488 addl $2,%%ebx \n\
489 decl %%ecx \n\
490 jne visible_bell_1 \n\
491 decb %%dl \n\
492 jne visible_bell_3 \n\
493 visible_bell_2: \n\
494 movzwl %%ax,%%eax \n\
495 movzwl %%ax,%%eax \n\
496 movzwl %%ax,%%eax \n\
497 movzwl %%ax,%%eax \n\
498 decw %%cx \n\
499 jne visible_bell_2 \n\
500 jmp visible_bell_0 \n\
501 visible_bell_3:"
502 : /* no output */
503 : "m" (xorattr), "g" (screen_size)
504 : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
505 }
506
507 static void
508 ScreenVisualBell (void)
509 {
510 /* This creates an xor-mask that will swap the default fore- and
511 background colors. */
512 do_visible_bell (((FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ())
513 ^ FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()))
514 * 0x11) & 0x7f);
515 }
516 #endif
517
518 #ifndef HAVE_X_WINDOWS
519
520 static int blink_bit = -1; /* the state of the blink bit at startup */
521
522 /* Enable bright background colors. */
523 static void
524 bright_bg (void)
525 {
526 union REGS regs;
527
528 /* Remember the original state of the blink/bright-background bit.
529 It is stored at 0040:0065h in the BIOS data area. */
530 if (blink_bit == -1)
531 blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;
532
533 regs.h.bl = 0;
534 regs.x.ax = 0x1003;
535 int86 (0x10, &regs, &regs);
536 }
537
538 /* Disable bright background colors (and enable blinking) if we found
539 the video system in that state at startup. */
540 static void
541 maybe_enable_blinking (void)
542 {
543 if (blink_bit == 1)
544 {
545 union REGS regs;
546
547 regs.h.bl = 1;
548 regs.x.ax = 0x1003;
549 int86 (0x10, &regs, &regs);
550 }
551 }
552
553 /* Return non-zero if the system has a VGA adapter. */
554 static int
555 vga_installed (void)
556 {
557 union REGS regs;
558
559 regs.x.ax = 0x1a00;
560 int86 (0x10, &regs, &regs);
561 if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
562 return 1;
563 return 0;
564 }
565
566 /* Set the screen dimensions so that it can show no less than
567 ROWS x COLS frame. */
568
569 void
570 dos_set_window_size (rows, cols)
571 int *rows, *cols;
572 {
573 char video_name[30];
574 union REGS regs;
575 Lisp_Object video_mode;
576 int video_mode_value, have_vga = 0;
577 int current_rows = ScreenRows (), current_cols = ScreenCols ();
578
579 if (*rows == current_rows && *cols == current_cols)
580 return;
581
582 mouse_off ();
583 have_vga = vga_installed ();
584
585 /* If the user specified a special video mode for these dimensions,
586 use that mode. */
587 sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
588 video_mode = XSYMBOL (Fintern_soft (build_string (video_name),
589 Qnil))-> value;
590
591 if (INTEGERP (video_mode)
592 && (video_mode_value = XINT (video_mode)) > 0)
593 {
594 regs.x.ax = video_mode_value;
595 int86 (0x10, &regs, &regs);
596
597 if (have_mouse)
598 {
599 /* Must hardware-reset the mouse, or else it won't update
600 its notion of screen dimensions for some non-standard
601 video modes. This is *painfully* slow... */
602 regs.x.ax = 0;
603 int86 (0x33, &regs, &regs);
604 }
605 }
606
607 /* Find one of the dimensions supported by standard EGA/VGA
608 which gives us at least the required dimensions. */
609
610 #if __DJGPP__ > 1
611
612 else
613 {
614 static struct {
615 int rows, need_vga;
616 } std_dimension[] = {
617 {25, 0},
618 {28, 1},
619 {35, 0},
620 {40, 1},
621 {43, 0},
622 {50, 1}
623 };
624 int i = 0;
625
626 while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
627 {
628 if (std_dimension[i].need_vga <= have_vga
629 && std_dimension[i].rows >= *rows)
630 {
631 if (std_dimension[i].rows != current_rows
632 || *cols != current_cols)
633 _set_screen_lines (std_dimension[i].rows);
634 break;
635 }
636 i++;
637 }
638 }
639
640 #else /* not __DJGPP__ > 1 */
641
642 else if (*rows <= 25)
643 {
644 if (current_rows != 25 || current_cols != 80)
645 {
646 regs.x.ax = 3;
647 int86 (0x10, &regs, &regs);
648 regs.x.ax = 0x1101;
649 regs.h.bl = 0;
650 int86 (0x10, &regs, &regs);
651 regs.x.ax = 0x1200;
652 regs.h.bl = 32;
653 int86 (0x10, &regs, &regs);
654 regs.x.ax = 3;
655 int86 (0x10, &regs, &regs);
656 }
657 }
658 else if (*rows <= 50)
659 if (have_vga && (current_rows != 50 || current_cols != 80)
660 || *rows <= 43 && (current_rows != 43 || current_cols != 80))
661 {
662 regs.x.ax = 3;
663 int86 (0x10, &regs, &regs);
664 regs.x.ax = 0x1112;
665 regs.h.bl = 0;
666 int86 (0x10, &regs, &regs);
667 regs.x.ax = 0x1200;
668 regs.h.bl = 32;
669 int86 (0x10, &regs, &regs);
670 regs.x.ax = 0x0100;
671 regs.x.cx = 7;
672 int86 (0x10, &regs, &regs);
673 }
674 #endif /* not __DJGPP__ > 1 */
675
676 if (have_mouse)
677 {
678 mouse_init ();
679 mouse_on ();
680 }
681
682 /* Tell the caller what dimensions have been REALLY set. */
683 *rows = ScreenRows ();
684 *cols = ScreenCols ();
685
686 /* Update Emacs' notion of screen dimensions. */
687 screen_size_X = *cols;
688 screen_size_Y = *rows;
689 screen_size = *cols * *rows;
690
691 #if __DJGPP__ > 1
692 /* If the dimensions changed, the mouse highlight info is invalid. */
693 if (current_rows != *rows || current_cols != *cols)
694 {
695 struct frame *f = SELECTED_FRAME();
696 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
697 Lisp_Object window = dpyinfo->mouse_face_window;
698
699 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
700 {
701 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
702 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
703 dpyinfo->mouse_face_window = Qnil;
704 }
705 }
706 #endif
707
708 /* Enable bright background colors. */
709 bright_bg ();
710
711 /* FIXME: I'm not sure the above will run at all on DOS/V. But let's
712 be defensive anyway. */
713 if (screen_virtual_segment)
714 dosv_refresh_virtual_screen (0, *cols * *rows);
715 }
716
717 /* If we write a character in the position where the mouse is,
718 the mouse cursor may need to be refreshed. */
719
720 static void
721 mouse_off_maybe ()
722 {
723 int x, y;
724
725 if (!mouse_visible)
726 return;
727
728 mouse_get_xy (&x, &y);
729 if (y != new_pos_Y || x < new_pos_X)
730 return;
731
732 mouse_off ();
733 }
734
735 #define DEFAULT_CURSOR_START (-1)
736 #define DEFAULT_CURSOR_WIDTH (-1)
737 #define BOX_CURSOR_WIDTH (-32)
738
739 /* Set cursor to begin at scan line START_LINE in the character cell
740 and extend for WIDTH scan lines. Scan lines are counted from top
741 of the character cell, starting from zero. */
742 static void
743 msdos_set_cursor_shape (struct frame *f, int start_line, int width)
744 {
745 #if __DJGPP__ > 1
746 unsigned desired_cursor;
747 __dpmi_regs regs;
748 int max_line, top_line, bot_line;
749
750 /* Avoid the costly BIOS call if F isn't the currently selected
751 frame. Allow for NULL as unconditionally meaning the selected
752 frame. */
753 if (f && f != SELECTED_FRAME())
754 return;
755
756 if (termscript)
757 fprintf (termscript, "\nCURSOR SHAPE=(%d,%d)", start_line, width);
758
759 /* The character cell size in scan lines is stored at 40:85 in the
760 BIOS data area. */
761 max_line = _farpeekw (_dos_ds, 0x485) - 1;
762 switch (max_line)
763 {
764 default: /* this relies on CGA cursor emulation being ON! */
765 case 7:
766 bot_line = 7;
767 break;
768 case 9:
769 bot_line = 9;
770 break;
771 case 13:
772 bot_line = 12;
773 break;
774 case 15:
775 bot_line = 14;
776 break;
777 }
778
779 if (width < 0)
780 {
781 if (width == BOX_CURSOR_WIDTH)
782 {
783 top_line = 0;
784 bot_line = max_line;
785 }
786 else if (start_line != DEFAULT_CURSOR_START)
787 {
788 top_line = start_line;
789 bot_line = top_line - width - 1;
790 }
791 else if (width != DEFAULT_CURSOR_WIDTH)
792 {
793 top_line = 0;
794 bot_line = -1 - width;
795 }
796 else
797 top_line = bot_line + 1;
798 }
799 else if (width == 0)
800 {
801 /* [31, 0] seems to DTRT for all screen sizes. */
802 top_line = 31;
803 bot_line = 0;
804 }
805 else /* WIDTH is positive */
806 {
807 if (start_line != DEFAULT_CURSOR_START)
808 bot_line = start_line;
809 top_line = bot_line - (width - 1);
810 }
811
812 /* If the current cursor shape is already what they want, we are
813 history here. */
814 desired_cursor = ((top_line & 0x1f) << 8) | (bot_line & 0x1f);
815 if (desired_cursor == _farpeekw (_dos_ds, 0x460))
816 return;
817
818 regs.h.ah = 1;
819 regs.x.cx = desired_cursor;
820 __dpmi_int (0x10, &regs);
821 #endif /* __DJGPP__ > 1 */
822 }
823
824 static void
825 IT_set_cursor_type (struct frame *f, Lisp_Object cursor_type)
826 {
827 if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
828 {
829 /* Just BAR means the normal EGA/VGA cursor. */
830 msdos_set_cursor_shape (f, DEFAULT_CURSOR_START, DEFAULT_CURSOR_WIDTH);
831 }
832 else if (CONSP (cursor_type)
833 && (EQ (XCAR (cursor_type), Qbar)
834 || EQ (XCAR (cursor_type), Qhbar)))
835 {
836 Lisp_Object bar_parms = XCDR (cursor_type);
837 int width;
838
839 if (INTEGERP (bar_parms))
840 {
841 /* Feature: negative WIDTH means cursor at the top
842 of the character cell, zero means invisible cursor. */
843 width = XINT (bar_parms);
844 msdos_set_cursor_shape (f, width >= 0 ? DEFAULT_CURSOR_START : 0,
845 width);
846 }
847 else if (CONSP (bar_parms)
848 && INTEGERP (XCAR (bar_parms))
849 && INTEGERP (XCDR (bar_parms)))
850 {
851 int start_line = XINT (XCDR (bar_parms));
852
853 width = XINT (XCAR (bar_parms));
854 msdos_set_cursor_shape (f, start_line, width);
855 }
856 }
857 else
858 {
859 /* Treat anything unknown as "box cursor". This includes nil, so
860 that a frame which doesn't specify a cursor type gets a box,
861 which is the default in Emacs. */
862 msdos_set_cursor_shape (f, 0, BOX_CURSOR_WIDTH);
863 }
864 }
865
866 static void
867 IT_ring_bell (void)
868 {
869 if (visible_bell)
870 {
871 mouse_off ();
872 ScreenVisualBell ();
873 }
874 else
875 {
876 union REGS inregs, outregs;
877 inregs.h.ah = 2;
878 inregs.h.dl = 7;
879 intdos (&inregs, &outregs);
880 }
881 }
882
883 /* Given a face id FACE, extract the face parameters to be used for
884 display until the face changes. The face parameters (actually, its
885 color) are used to construct the video attribute byte for each
886 glyph during the construction of the buffer that is then blitted to
887 the video RAM. */
888 static void
889 IT_set_face (int face)
890 {
891 struct frame *sf = SELECTED_FRAME();
892 struct face *fp = FACE_FROM_ID (sf, face);
893 struct face *dfp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
894 unsigned long fg, bg, dflt_fg, dflt_bg;
895
896 if (!fp)
897 {
898 fp = dfp;
899 /* The default face for the frame should always be realized and
900 cached. */
901 if (!fp)
902 abort ();
903 }
904 screen_face = face;
905 fg = fp->foreground;
906 bg = fp->background;
907 dflt_fg = dfp->foreground;
908 dflt_bg = dfp->background;
909
910 /* Don't use invalid colors. In particular, FACE_TTY_DEFAULT_* colors
911 mean use the colors of the default face. Note that we assume all
912 16 colors to be available for the background, since Emacs switches
913 on this mode (and loses the blinking attribute) at startup. */
914 if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
915 fg = FRAME_FOREGROUND_PIXEL (sf);
916 else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
917 fg = FRAME_BACKGROUND_PIXEL (sf);
918 if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
919 bg = FRAME_BACKGROUND_PIXEL (sf);
920 else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
921 bg = FRAME_FOREGROUND_PIXEL (sf);
922
923 /* Make sure highlighted lines really stand out, come what may. */
924 if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
925 {
926 unsigned long tem = fg;
927
928 fg = bg;
929 bg = tem;
930 }
931 /* If the user requested inverse video, obey. */
932 if (inverse_video)
933 {
934 unsigned long tem2 = fg;
935
936 fg = bg;
937 bg = tem2;
938 }
939 if (termscript)
940 fprintf (termscript, "<FACE %d: %d/%d[FG:%d/BG:%d]>", face,
941 fp->foreground, fp->background, fg, bg);
942 if (fg >= 0 && fg < 16)
943 {
944 ScreenAttrib &= 0xf0;
945 ScreenAttrib |= fg;
946 }
947 if (bg >= 0 && bg < 16)
948 {
949 ScreenAttrib &= 0x0f;
950 ScreenAttrib |= ((bg & 0x0f) << 4);
951 }
952 }
953
954 Lisp_Object Vdos_unsupported_char_glyph;
955
956 static void
957 IT_write_glyphs (struct glyph *str, int str_len)
958 {
959 unsigned char *screen_buf, *screen_bp, *screen_buf_end, *bp;
960 int unsupported_face = 0;
961 unsigned unsupported_char = '\177';
962 int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
963 register int sl = str_len;
964 register int tlen = GLYPH_TABLE_LENGTH;
965 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
966
967 /* If terminal_coding does any conversion, use it, otherwise use
968 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
969 because it always returns 1 if terminal_coding.src_multibyte is 1. */
970 struct coding_system *coding =
971 (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
972 ? &terminal_coding
973 : &safe_terminal_coding);
974 struct frame *sf;
975
976 /* Do we need to consider conversion of unibyte characters to
977 multibyte? */
978 int convert_unibyte_characters
979 = (NILP (current_buffer->enable_multibyte_characters)
980 && unibyte_display_via_language_environment);
981
982 unsigned char conversion_buffer[256];
983 int conversion_buffer_size = sizeof conversion_buffer;
984
985 if (str_len <= 0) return;
986
987 /* Set up the unsupported character glyph */
988 if (!NILP (Vdos_unsupported_char_glyph))
989 {
990 unsupported_char = FAST_GLYPH_CHAR (XINT (Vdos_unsupported_char_glyph));
991 unsupported_face = FAST_GLYPH_FACE (XINT (Vdos_unsupported_char_glyph));
992 }
993
994 screen_buf = screen_bp = alloca (str_len * 2);
995 screen_buf_end = screen_buf + str_len * 2;
996 sf = SELECTED_FRAME();
997
998 /* Since faces get cached and uncached behind our back, we can't
999 rely on their indices in the cache being consistent across
1000 invocations. So always reset the screen face to the default
1001 face of the frame, before writing glyphs, and let the glyphs
1002 set the right face if it's different from the default. */
1003 IT_set_face (DEFAULT_FACE_ID);
1004
1005 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
1006 the tail. */
1007 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
1008 while (sl)
1009 {
1010 int cf, chlen, enclen;
1011 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
1012 unsigned ch;
1013
1014 /* Glyphs with GLYPH_MASK_PADDING bit set are actually there
1015 only for the redisplay code to know how many columns does
1016 this character occupy on the screen. Skip padding glyphs. */
1017 if (CHAR_GLYPH_PADDING_P (*str))
1018 {
1019 str++;
1020 sl--;
1021 }
1022 else
1023 {
1024 register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
1025 int glyph_not_in_table = 0;
1026
1027 /* If g is negative, it means we have a multibyte character
1028 in *str. That's what GLYPH_FROM_CHAR_GLYPH returns for
1029 multibyte characters. */
1030 if (g < 0 || g >= tlen)
1031 {
1032 /* This glyph doesn't have an entry in Vglyph_table. */
1033 ch = str->u.ch;
1034 glyph_not_in_table = 1;
1035 }
1036 else
1037 {
1038 /* This glyph has an entry in Vglyph_table, so process
1039 any aliases before testing for simpleness. */
1040 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
1041 ch = FAST_GLYPH_CHAR (g);
1042 }
1043
1044 /* Convert the character code to multibyte, if they
1045 requested display via language environment. We only want
1046 to convert unibyte characters to multibyte in unibyte
1047 buffers! Otherwise, the 8-bit value in CH came from the
1048 display table set up to display foreign characters. */
1049 if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
1050 && (ch >= 0240
1051 || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
1052 ch = unibyte_char_to_multibyte (ch);
1053
1054 /* Invalid characters are displayed with a special glyph. */
1055 if (! CHAR_VALID_P (ch, 0))
1056 {
1057 g = !NILP (Vdos_unsupported_char_glyph)
1058 ? XINT (Vdos_unsupported_char_glyph)
1059 : MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
1060 ch = FAST_GLYPH_CHAR (g);
1061 }
1062
1063 /* If the face of this glyph is different from the current
1064 screen face, update the screen attribute byte. */
1065 cf = str->face_id;
1066 if (cf != screen_face)
1067 IT_set_face (cf); /* handles invalid faces gracefully */
1068
1069 if (glyph_not_in_table || GLYPH_SIMPLE_P (tbase, tlen, g))
1070 {
1071 /* We generate the multi-byte form of CH in WORKBUF. */
1072 chlen = CHAR_STRING (ch, workbuf);
1073 buf = workbuf;
1074 }
1075 else
1076 {
1077 /* We have a string in Vglyph_table. */
1078 chlen = GLYPH_LENGTH (tbase, g);
1079 buf = GLYPH_STRING (tbase, g);
1080 }
1081
1082 /* If the character is not multibyte, don't bother converting it. */
1083 if (chlen == 1)
1084 {
1085 *conversion_buffer = (unsigned char)ch;
1086 chlen = 0;
1087 enclen = 1;
1088 }
1089 else
1090 {
1091 coding->src_multibyte = 1;
1092 encode_coding (coding, buf, conversion_buffer, chlen,
1093 conversion_buffer_size);
1094 chlen -= coding->consumed;
1095 enclen = coding->produced;
1096
1097 /* Replace glyph codes that cannot be converted by
1098 terminal_coding with Vdos_unsupported_char_glyph. */
1099 if (*conversion_buffer == '?')
1100 {
1101 unsigned char *cbp = conversion_buffer;
1102
1103 while (cbp < conversion_buffer + enclen && *cbp == '?')
1104 *cbp++ = unsupported_char;
1105 if (unsupported_face != screen_face)
1106 IT_set_face (unsupported_face);
1107 }
1108 }
1109
1110 if (enclen + chlen > screen_buf_end - screen_bp)
1111 {
1112 /* The allocated buffer for screen writes is too small.
1113 Flush it and loop again without incrementing STR, so
1114 that the next loop will begin with the same glyph. */
1115 int nbytes = screen_bp - screen_buf;
1116
1117 mouse_off_maybe ();
1118 dosmemput (screen_buf, nbytes, (int)ScreenPrimary + offset);
1119 if (screen_virtual_segment)
1120 dosv_refresh_virtual_screen (offset, nbytes / 2);
1121 new_pos_X += nbytes / 2;
1122 offset += nbytes;
1123
1124 /* Prepare to reuse the same buffer again. */
1125 screen_bp = screen_buf;
1126 }
1127 else
1128 {
1129 /* There's enough place in the allocated buffer to add
1130 the encoding of this glyph. */
1131
1132 /* First, copy the encoded bytes. */
1133 for (bp = conversion_buffer; enclen--; bp++)
1134 {
1135 *screen_bp++ = (unsigned char)*bp;
1136 *screen_bp++ = ScreenAttrib;
1137 if (termscript)
1138 fputc (*bp, termscript);
1139 }
1140
1141 /* Now copy the bytes not consumed by the encoding. */
1142 if (chlen > 0)
1143 {
1144 buf += coding->consumed;
1145 while (chlen--)
1146 {
1147 if (termscript)
1148 fputc (*buf, termscript);
1149 *screen_bp++ = (unsigned char)*buf++;
1150 *screen_bp++ = ScreenAttrib;
1151 }
1152 }
1153
1154 /* Update STR and its remaining length. */
1155 str++;
1156 sl--;
1157 }
1158 }
1159 }
1160
1161 /* Dump whatever is left in the screen buffer. */
1162 mouse_off_maybe ();
1163 dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
1164 if (screen_virtual_segment)
1165 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
1166 new_pos_X += (screen_bp - screen_buf) / 2;
1167
1168 /* We may have to output some codes to terminate the writing. */
1169 if (CODING_REQUIRE_FLUSHING (coding))
1170 {
1171 coding->mode |= CODING_MODE_LAST_BLOCK;
1172 encode_coding (coding, "", conversion_buffer, 0, conversion_buffer_size);
1173 if (coding->produced > 0)
1174 {
1175 screen_buf = alloca (coding->produced * 2);
1176 for (screen_bp = screen_buf, bp = conversion_buffer;
1177 coding->produced--; bp++)
1178 {
1179 *screen_bp++ = (unsigned char)*bp;
1180 *screen_bp++ = ScreenAttrib;
1181 if (termscript)
1182 fputc (*bp, termscript);
1183 }
1184 offset += screen_bp - screen_buf;
1185 mouse_off_maybe ();
1186 dosmemput (screen_buf, screen_bp - screen_buf,
1187 (int)ScreenPrimary + offset);
1188 if (screen_virtual_segment)
1189 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
1190 new_pos_X += (screen_bp - screen_buf) / 2;
1191 }
1192 }
1193 }
1194
1195 /************************************************************************
1196 Mouse Highlight (and friends..)
1197 ************************************************************************/
1198
1199 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
1200 static Lisp_Object last_mouse_window;
1201
1202 static int mouse_preempted = 0; /* non-zero when XMenu gobbles mouse events */
1203
1204 /* Set the mouse pointer shape according to whether it is in the
1205 area where the mouse highlight is in effect. */
1206 static void
1207 IT_set_mouse_pointer (int mode)
1208 {
1209 /* A no-op for now. DOS text-mode mouse pointer doesn't offer too
1210 many possibilities to change its shape, and the available
1211 functionality pretty much sucks (e.g., almost every reasonable
1212 shape will conceal the character it is on). Since the color of
1213 the pointer changes in the highlighted area, it is not clear to
1214 me whether anything else is required, anyway. */
1215 }
1216
1217 /* Display the active region described by mouse_face_*
1218 in its mouse-face if HL > 0, in its normal face if HL = 0. */
1219 static void
1220 show_mouse_face (struct display_info *dpyinfo, int hl)
1221 {
1222 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
1223 struct frame *f = XFRAME (WINDOW_FRAME (w));
1224 int i;
1225 struct face *fp;
1226
1227
1228 /* If window is in the process of being destroyed, don't bother
1229 doing anything. */
1230 if (w->current_matrix == NULL)
1231 goto set_cursor_shape;
1232
1233 /* Recognize when we are called to operate on rows that don't exist
1234 anymore. This can happen when a window is split. */
1235 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
1236 goto set_cursor_shape;
1237
1238 /* There's no sense to do anything if the mouse face isn't realized. */
1239 if (hl > 0)
1240 {
1241 if (dpyinfo->mouse_face_hidden)
1242 goto set_cursor_shape;
1243
1244 fp = FACE_FROM_ID (SELECTED_FRAME(), dpyinfo->mouse_face_face_id);
1245 if (!fp)
1246 goto set_cursor_shape;
1247 }
1248
1249 /* Note that mouse_face_beg_row etc. are window relative. */
1250 for (i = dpyinfo->mouse_face_beg_row;
1251 i <= dpyinfo->mouse_face_end_row;
1252 i++)
1253 {
1254 int start_hpos, end_hpos;
1255 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
1256
1257 /* Don't do anything if row doesn't have valid contents. */
1258 if (!row->enabled_p)
1259 continue;
1260
1261 /* For all but the first row, the highlight starts at column 0. */
1262 if (i == dpyinfo->mouse_face_beg_row)
1263 start_hpos = dpyinfo->mouse_face_beg_col;
1264 else
1265 start_hpos = 0;
1266
1267 if (i == dpyinfo->mouse_face_end_row)
1268 end_hpos = dpyinfo->mouse_face_end_col;
1269 else
1270 end_hpos = row->used[TEXT_AREA];
1271
1272 if (end_hpos <= start_hpos)
1273 continue;
1274 /* Record that some glyphs of this row are displayed in
1275 mouse-face. */
1276 row->mouse_face_p = hl > 0;
1277 if (hl > 0)
1278 {
1279 int vpos = row->y + WINDOW_TOP_EDGE_Y (w);
1280 int kstart = start_hpos + WINDOW_LEFT_EDGE_X (w);
1281 int nglyphs = end_hpos - start_hpos;
1282 int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
1283 int start_offset = offset;
1284
1285 if (termscript)
1286 fprintf (termscript, "\n<MH+ %d-%d:%d>",
1287 kstart, kstart + nglyphs - 1, vpos);
1288
1289 mouse_off ();
1290 IT_set_face (dpyinfo->mouse_face_face_id);
1291 /* Since we are going to change only the _colors_ of the
1292 displayed text, there's no need to go through all the
1293 pain of generating and encoding the text from the glyphs.
1294 Instead, we simply poke the attribute byte of each
1295 affected position in video memory with the colors
1296 computed by IT_set_face! */
1297 _farsetsel (_dos_ds);
1298 while (nglyphs--)
1299 {
1300 _farnspokeb (offset, ScreenAttrib);
1301 offset += 2;
1302 }
1303 if (screen_virtual_segment)
1304 dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
1305 mouse_on ();
1306 }
1307 else
1308 {
1309 /* We are removing a previously-drawn mouse highlight. The
1310 safest way to do so is to redraw the glyphs anew, since
1311 all kinds of faces and display tables could have changed
1312 behind our back. */
1313 int nglyphs = end_hpos - start_hpos;
1314 int save_x = new_pos_X, save_y = new_pos_Y;
1315
1316 if (end_hpos >= row->used[TEXT_AREA])
1317 nglyphs = row->used[TEXT_AREA] - start_hpos;
1318
1319 /* IT_write_glyphs writes at cursor position, so we need to
1320 temporarily move cursor coordinates to the beginning of
1321 the highlight region. */
1322 new_pos_X = start_hpos + WINDOW_LEFT_EDGE_X (w);
1323 new_pos_Y = row->y + WINDOW_TOP_EDGE_Y (w);
1324
1325 if (termscript)
1326 fprintf (termscript, "<MH- %d-%d:%d>",
1327 new_pos_X, new_pos_X + nglyphs - 1, new_pos_Y);
1328 IT_write_glyphs (row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
1329 if (termscript)
1330 fputs ("\n", termscript);
1331 new_pos_X = save_x;
1332 new_pos_Y = save_y;
1333 }
1334 }
1335
1336 set_cursor_shape:
1337 /* Change the mouse pointer shape. */
1338 IT_set_mouse_pointer (hl);
1339 }
1340
1341 /* Clear out the mouse-highlighted active region.
1342 Redraw it un-highlighted first. */
1343 static void
1344 clear_mouse_face (struct display_info *dpyinfo)
1345 {
1346 if (!dpyinfo->mouse_face_hidden && ! NILP (dpyinfo->mouse_face_window))
1347 show_mouse_face (dpyinfo, 0);
1348
1349 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1350 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1351 dpyinfo->mouse_face_window = Qnil;
1352 }
1353
1354 /* Find the glyph matrix position of buffer position POS in window W.
1355 *HPOS and *VPOS are set to the positions found. W's current glyphs
1356 must be up to date. If POS is above window start return (0, 0).
1357 If POS is after end of W, return end of last line in W. */
1358 static int
1359 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
1360 {
1361 int i, lastcol, line_start_position, maybe_next_line_p = 0;
1362 int yb = window_text_bottom_y (w);
1363 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
1364
1365 while (row->y < yb)
1366 {
1367 if (row->used[TEXT_AREA])
1368 line_start_position = row->glyphs[TEXT_AREA]->charpos;
1369 else
1370 line_start_position = 0;
1371
1372 if (line_start_position > pos)
1373 break;
1374 /* If the position sought is the end of the buffer,
1375 don't include the blank lines at the bottom of the window. */
1376 else if (line_start_position == pos
1377 && pos == BUF_ZV (XBUFFER (w->buffer)))
1378 {
1379 maybe_next_line_p = 1;
1380 break;
1381 }
1382 else if (line_start_position > 0)
1383 best_row = row;
1384
1385 /* Don't overstep the last matrix row, lest we get into the
1386 never-never land... */
1387 if (row->y + 1 >= yb)
1388 break;
1389
1390 ++row;
1391 }
1392
1393 /* Find the right column within BEST_ROW. */
1394 lastcol = 0;
1395 row = best_row;
1396 for (i = 0; i < row->used[TEXT_AREA]; i++)
1397 {
1398 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
1399 int charpos;
1400
1401 charpos = glyph->charpos;
1402 if (charpos == pos)
1403 {
1404 *hpos = i;
1405 *vpos = row->y;
1406 return 1;
1407 }
1408 else if (charpos > pos)
1409 break;
1410 else if (charpos > 0)
1411 lastcol = i;
1412 }
1413
1414 /* If we're looking for the end of the buffer,
1415 and we didn't find it in the line we scanned,
1416 use the start of the following line. */
1417 if (maybe_next_line_p)
1418 {
1419 ++row;
1420 lastcol = 0;
1421 }
1422
1423 *vpos = row->y;
1424 *hpos = lastcol + 1;
1425 return 0;
1426 }
1427
1428 /* Take proper action when mouse has moved to the mode or top line of
1429 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
1430 mode line. X is relative to the start of the text display area of
1431 W, so the width of fringes and scroll bars must be subtracted
1432 to get a position relative to the start of the mode line. */
1433 static void
1434 IT_note_mode_line_highlight (struct window *w, int x, int mode_line_p)
1435 {
1436 struct frame *f = XFRAME (w->frame);
1437 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1438 struct glyph_row *row;
1439
1440 if (mode_line_p)
1441 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1442 else
1443 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1444
1445 if (row->enabled_p)
1446 {
1447 extern Lisp_Object Qhelp_echo;
1448 struct glyph *glyph, *end;
1449 Lisp_Object help, map;
1450
1451 /* Find the glyph under X. */
1452 glyph = (row->glyphs[TEXT_AREA]
1453 + x
1454 /* in case someone implements scroll bars some day... */
1455 - WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w));
1456 end = glyph + row->used[TEXT_AREA];
1457 if (glyph < end
1458 && STRINGP (glyph->object)
1459 && STRING_INTERVALS (glyph->object)
1460 && glyph->charpos >= 0
1461 && glyph->charpos < SCHARS (glyph->object))
1462 {
1463 /* If we're on a string with `help-echo' text property,
1464 arrange for the help to be displayed. This is done by
1465 setting the global variable help_echo to the help string. */
1466 help = Fget_text_property (make_number (glyph->charpos),
1467 Qhelp_echo, glyph->object);
1468 if (!NILP (help))
1469 {
1470 help_echo_string = help;
1471 XSETWINDOW (help_echo_window, w);
1472 help_echo_object = glyph->object;
1473 help_echo_pos = glyph->charpos;
1474 }
1475 }
1476 }
1477 }
1478
1479 /* Take proper action when the mouse has moved to position X, Y on
1480 frame F as regards highlighting characters that have mouse-face
1481 properties. Also de-highlighting chars where the mouse was before.
1482 X and Y can be negative or out of range. */
1483 static void
1484 IT_note_mouse_highlight (struct frame *f, int x, int y)
1485 {
1486 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1487 enum window_part part = ON_NOTHING;
1488 Lisp_Object window;
1489 struct window *w;
1490
1491 /* When a menu is active, don't highlight because this looks odd. */
1492 if (mouse_preempted)
1493 return;
1494
1495 if (NILP (Vmouse_highlight)
1496 || !f->glyphs_initialized_p)
1497 return;
1498
1499 dpyinfo->mouse_face_mouse_x = x;
1500 dpyinfo->mouse_face_mouse_y = y;
1501 dpyinfo->mouse_face_mouse_frame = f;
1502
1503 if (dpyinfo->mouse_face_defer)
1504 return;
1505
1506 if (gc_in_progress)
1507 {
1508 dpyinfo->mouse_face_deferred_gc = 1;
1509 return;
1510 }
1511
1512 /* Which window is that in? */
1513 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
1514
1515 /* If we were displaying active text in another window, clear that. */
1516 if (! EQ (window, dpyinfo->mouse_face_window))
1517 clear_mouse_face (dpyinfo);
1518
1519 /* Not on a window -> return. */
1520 if (!WINDOWP (window))
1521 return;
1522
1523 /* Convert to window-relative coordinates. */
1524 w = XWINDOW (window);
1525
1526 if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
1527 {
1528 /* Mouse is on the mode or top line. */
1529 IT_note_mode_line_highlight (w, x, part == ON_MODE_LINE);
1530 return;
1531 }
1532
1533 IT_set_mouse_pointer (0);
1534
1535 /* Are we in a window whose display is up to date?
1536 And verify the buffer's text has not changed. */
1537 if (part == ON_TEXT
1538 && EQ (w->window_end_valid, w->buffer)
1539 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
1540 && (XFASTINT (w->last_overlay_modified)
1541 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
1542 {
1543 int pos, i, nrows = w->current_matrix->nrows;
1544 struct glyph_row *row;
1545 struct glyph *glyph;
1546
1547 /* Find the glyph under X/Y. */
1548 glyph = NULL;
1549 if (y >= 0 && y < nrows)
1550 {
1551 row = MATRIX_ROW (w->current_matrix, y);
1552 /* Give up if some row before the one we are looking for is
1553 not enabled. */
1554 for (i = 0; i <= y; i++)
1555 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
1556 break;
1557 if (i > y /* all rows upto and including the one at Y are enabled */
1558 && row->displays_text_p
1559 && x < window_box_width (w, TEXT_AREA))
1560 {
1561 glyph = row->glyphs[TEXT_AREA];
1562 if (x >= row->used[TEXT_AREA])
1563 glyph = NULL;
1564 else
1565 {
1566 glyph += x;
1567 if (!BUFFERP (glyph->object))
1568 glyph = NULL;
1569 }
1570 }
1571 }
1572
1573 /* Clear mouse face if X/Y not over text. */
1574 if (glyph == NULL)
1575 {
1576 clear_mouse_face (dpyinfo);
1577 return;
1578 }
1579
1580 if (!BUFFERP (glyph->object))
1581 abort ();
1582 pos = glyph->charpos;
1583
1584 /* Check for mouse-face and help-echo. */
1585 {
1586 extern Lisp_Object Qmouse_face;
1587 Lisp_Object mouse_face, overlay, position, *overlay_vec;
1588 int noverlays, obegv, ozv;
1589 struct buffer *obuf;
1590
1591 /* If we get an out-of-range value, return now; avoid an error. */
1592 if (pos > BUF_Z (XBUFFER (w->buffer)))
1593 return;
1594
1595 /* Make the window's buffer temporarily current for
1596 overlays_at and compute_char_face. */
1597 obuf = current_buffer;
1598 current_buffer = XBUFFER (w->buffer);
1599 obegv = BEGV;
1600 ozv = ZV;
1601 BEGV = BEG;
1602 ZV = Z;
1603
1604 /* Is this char mouse-active or does it have help-echo? */
1605 XSETINT (position, pos);
1606
1607 /* Put all the overlays we want in a vector in overlay_vec. */
1608 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
1609 /* Sort overlays into increasing priority order. */
1610 noverlays = sort_overlays (overlay_vec, noverlays, w);
1611
1612 /* Check mouse-face highlighting. */
1613 if (! (EQ (window, dpyinfo->mouse_face_window)
1614 && y >= dpyinfo->mouse_face_beg_row
1615 && y <= dpyinfo->mouse_face_end_row
1616 && (y > dpyinfo->mouse_face_beg_row
1617 || x >= dpyinfo->mouse_face_beg_col)
1618 && (y < dpyinfo->mouse_face_end_row
1619 || x < dpyinfo->mouse_face_end_col
1620 || dpyinfo->mouse_face_past_end)))
1621 {
1622 /* Clear the display of the old active region, if any. */
1623 clear_mouse_face (dpyinfo);
1624
1625 /* Find highest priority overlay that has a mouse-face prop. */
1626 overlay = Qnil;
1627 for (i = noverlays - 1; i >= 0; --i)
1628 {
1629 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
1630 if (!NILP (mouse_face))
1631 {
1632 overlay = overlay_vec[i];
1633 break;
1634 }
1635 }
1636
1637 /* If no overlay applies, get a text property. */
1638 if (NILP (overlay))
1639 mouse_face = Fget_text_property (position, Qmouse_face,
1640 w->buffer);
1641
1642 /* Handle the overlay case. */
1643 if (! NILP (overlay))
1644 {
1645 /* Find the range of text around this char that
1646 should be active. */
1647 Lisp_Object before, after;
1648 int ignore;
1649
1650 before = Foverlay_start (overlay);
1651 after = Foverlay_end (overlay);
1652 /* Record this as the current active region. */
1653 fast_find_position (w, XFASTINT (before),
1654 &dpyinfo->mouse_face_beg_col,
1655 &dpyinfo->mouse_face_beg_row);
1656 dpyinfo->mouse_face_past_end
1657 = !fast_find_position (w, XFASTINT (after),
1658 &dpyinfo->mouse_face_end_col,
1659 &dpyinfo->mouse_face_end_row);
1660 dpyinfo->mouse_face_window = window;
1661 dpyinfo->mouse_face_face_id
1662 = face_at_buffer_position (w, pos, 0, 0,
1663 &ignore, pos + 1,
1664 !dpyinfo->mouse_face_hidden);
1665
1666 /* Display it as active. */
1667 show_mouse_face (dpyinfo, 1);
1668 }
1669 /* Handle the text property case. */
1670 else if (! NILP (mouse_face))
1671 {
1672 /* Find the range of text around this char that
1673 should be active. */
1674 Lisp_Object before, after, beginning, end;
1675 int ignore;
1676
1677 beginning = Fmarker_position (w->start);
1678 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
1679 - XFASTINT (w->window_end_pos)));
1680 before
1681 = Fprevious_single_property_change (make_number (pos + 1),
1682 Qmouse_face,
1683 w->buffer, beginning);
1684 after
1685 = Fnext_single_property_change (position, Qmouse_face,
1686 w->buffer, end);
1687 /* Record this as the current active region. */
1688 fast_find_position (w, XFASTINT (before),
1689 &dpyinfo->mouse_face_beg_col,
1690 &dpyinfo->mouse_face_beg_row);
1691 dpyinfo->mouse_face_past_end
1692 = !fast_find_position (w, XFASTINT (after),
1693 &dpyinfo->mouse_face_end_col,
1694 &dpyinfo->mouse_face_end_row);
1695 dpyinfo->mouse_face_window = window;
1696 dpyinfo->mouse_face_face_id
1697 = face_at_buffer_position (w, pos, 0, 0,
1698 &ignore, pos + 1,
1699 !dpyinfo->mouse_face_hidden);
1700
1701 /* Display it as active. */
1702 show_mouse_face (dpyinfo, 1);
1703 }
1704 }
1705
1706 /* Look for a `help-echo' property. */
1707 {
1708 Lisp_Object help;
1709 extern Lisp_Object Qhelp_echo;
1710
1711 /* Check overlays first. */
1712 help = Qnil;
1713 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
1714 {
1715 overlay = overlay_vec[i];
1716 help = Foverlay_get (overlay, Qhelp_echo);
1717 }
1718
1719 if (!NILP (help))
1720 {
1721 help_echo_string = help;
1722 help_echo_window = window;
1723 help_echo_object = overlay;
1724 help_echo_pos = pos;
1725 }
1726 /* Try text properties. */
1727 else if (NILP (help)
1728 && ((STRINGP (glyph->object)
1729 && glyph->charpos >= 0
1730 && glyph->charpos < SCHARS (glyph->object))
1731 || (BUFFERP (glyph->object)
1732 && glyph->charpos >= BEGV
1733 && glyph->charpos < ZV)))
1734 {
1735 help = Fget_text_property (make_number (glyph->charpos),
1736 Qhelp_echo, glyph->object);
1737 if (!NILP (help))
1738 {
1739 help_echo_string = help;
1740 help_echo_window = window;
1741 help_echo_object = glyph->object;
1742 help_echo_pos = glyph->charpos;
1743 }
1744 }
1745 }
1746
1747 BEGV = obegv;
1748 ZV = ozv;
1749 current_buffer = obuf;
1750 }
1751 }
1752 }
1753
1754 static void
1755 IT_clear_end_of_line (int first_unused)
1756 {
1757 char *spaces, *sp;
1758 int i, j, offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
1759 extern int fatal_error_in_progress;
1760
1761 if (new_pos_X >= first_unused || fatal_error_in_progress)
1762 return;
1763
1764 IT_set_face (0);
1765 i = (j = first_unused - new_pos_X) * 2;
1766 if (termscript)
1767 fprintf (termscript, "<CLR:EOL[%d..%d)>", new_pos_X, first_unused);
1768 spaces = sp = alloca (i);
1769
1770 while (--j >= 0)
1771 {
1772 *sp++ = ' ';
1773 *sp++ = ScreenAttrib;
1774 }
1775
1776 mouse_off_maybe ();
1777 dosmemput (spaces, i, (int)ScreenPrimary + offset);
1778 if (screen_virtual_segment)
1779 dosv_refresh_virtual_screen (offset, i / 2);
1780
1781 /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
1782 Let's follow their lead, in case someone relies on this. */
1783 new_pos_X = first_unused;
1784 }
1785
1786 static void
1787 IT_clear_screen (void)
1788 {
1789 if (termscript)
1790 fprintf (termscript, "<CLR:SCR>");
1791 /* We are sometimes called (from clear_garbaged_frames) when a new
1792 frame is being created, but its faces are not yet realized. In
1793 such a case we cannot call IT_set_face, since it will fail to find
1794 any valid faces and will abort. Instead, use the initial screen
1795 colors; that should mimic what a Unix tty does, which simply clears
1796 the screen with whatever default colors are in use. */
1797 if (FACE_FROM_ID (SELECTED_FRAME (), DEFAULT_FACE_ID) == NULL)
1798 ScreenAttrib = (initial_screen_colors[0] << 4) | initial_screen_colors[1];
1799 else
1800 IT_set_face (0);
1801 mouse_off ();
1802 ScreenClear ();
1803 if (screen_virtual_segment)
1804 dosv_refresh_virtual_screen (0, screen_size);
1805 new_pos_X = new_pos_Y = 0;
1806 }
1807
1808 static void
1809 IT_clear_to_end (void)
1810 {
1811 if (termscript)
1812 fprintf (termscript, "<CLR:EOS>");
1813
1814 while (new_pos_Y < screen_size_Y) {
1815 new_pos_X = 0;
1816 IT_clear_end_of_line (screen_size_X);
1817 new_pos_Y++;
1818 }
1819 }
1820
1821 static void
1822 IT_cursor_to (int y, int x)
1823 {
1824 if (termscript)
1825 fprintf (termscript, "\n<XY=%dx%d>", x, y);
1826 new_pos_X = x;
1827 new_pos_Y = y;
1828 }
1829
1830 static int cursor_cleared;
1831
1832 static void
1833 IT_display_cursor (int on)
1834 {
1835 if (termscript)
1836 fprintf (termscript, "\nCURSOR %s", on ? "ON" : "OFF");
1837 if (on && cursor_cleared)
1838 {
1839 ScreenSetCursor (current_pos_Y, current_pos_X);
1840 cursor_cleared = 0;
1841 }
1842 else if (!on && !cursor_cleared)
1843 {
1844 ScreenSetCursor (-1, -1);
1845 cursor_cleared = 1;
1846 }
1847 }
1848
1849 /* Emacs calls cursor-movement functions a lot when it updates the
1850 display (probably a legacy of old terminals where you cannot
1851 update a screen line without first moving the cursor there).
1852 However, cursor movement is expensive on MSDOS (it calls a slow
1853 BIOS function and requires 2 mode switches), while actual screen
1854 updates access the video memory directly and don't depend on
1855 cursor position. To avoid slowing down the redisplay, we cheat:
1856 all functions that move the cursor only set internal variables
1857 which record the cursor position, whereas the cursor is only
1858 moved to its final position whenever screen update is complete.
1859
1860 `IT_cmgoto' is called from the keyboard reading loop and when the
1861 frame update is complete. This means that we are ready for user
1862 input, so we update the cursor position to show where the point is,
1863 and also make the mouse pointer visible.
1864
1865 Special treatment is required when the cursor is in the echo area,
1866 to put the cursor at the end of the text displayed there. */
1867
1868 static void
1869 IT_cmgoto (FRAME_PTR f)
1870 {
1871 /* Only set the cursor to where it should be if the display is
1872 already in sync with the window contents. */
1873 int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */
1874
1875 /* FIXME: This needs to be rewritten for the new redisplay, or
1876 removed. */
1877 #if 0
1878 static int previous_pos_X = -1;
1879
1880 update_cursor_pos = 1; /* temporary!!! */
1881
1882 /* If the display is in sync, forget any previous knowledge about
1883 cursor position. This is primarily for unexpected events like
1884 C-g in the minibuffer. */
1885 if (update_cursor_pos && previous_pos_X >= 0)
1886 previous_pos_X = -1;
1887 /* If we are in the echo area, put the cursor at the
1888 end of the echo area message. */
1889 if (!update_cursor_pos
1890 && WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f))) <= new_pos_Y)
1891 {
1892 int tem_X = current_pos_X, dummy;
1893
1894 if (echo_area_glyphs)
1895 {
1896 tem_X = echo_area_glyphs_length;
1897 /* Save current cursor position, to be restored after the
1898 echo area message is erased. Only remember one level
1899 of previous cursor position. */
1900 if (previous_pos_X == -1)
1901 ScreenGetCursor (&dummy, &previous_pos_X);
1902 }
1903 else if (previous_pos_X >= 0)
1904 {
1905 /* We wind up here after the echo area message is erased.
1906 Restore the cursor position we remembered above. */
1907 tem_X = previous_pos_X;
1908 previous_pos_X = -1;
1909 }
1910
1911 if (current_pos_X != tem_X)
1912 {
1913 new_pos_X = tem_X;
1914 update_cursor_pos = 1;
1915 }
1916 }
1917 #endif
1918
1919 if (update_cursor_pos
1920 && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
1921 {
1922 ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
1923 if (termscript)
1924 fprintf (termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
1925 }
1926
1927 /* Maybe cursor is invisible, so make it visible. */
1928 IT_display_cursor (1);
1929
1930 /* Mouse pointer should be always visible if we are waiting for
1931 keyboard input. */
1932 if (!mouse_visible)
1933 mouse_on ();
1934 }
1935
1936 static void
1937 IT_update_begin (struct frame *f)
1938 {
1939 struct display_info *display_info = FRAME_X_DISPLAY_INFO (f);
1940 struct frame *mouse_face_frame = display_info->mouse_face_mouse_frame;
1941
1942 BLOCK_INPUT;
1943
1944 if (f && f == mouse_face_frame)
1945 {
1946 /* Don't do highlighting for mouse motion during the update. */
1947 display_info->mouse_face_defer = 1;
1948
1949 /* If F needs to be redrawn, simply forget about any prior mouse
1950 highlighting. */
1951 if (FRAME_GARBAGED_P (f))
1952 display_info->mouse_face_window = Qnil;
1953
1954 /* Can we tell that this update does not affect the window
1955 where the mouse highlight is? If so, no need to turn off.
1956 Likewise, don't do anything if none of the enabled rows
1957 contains glyphs highlighted in mouse face. */
1958 if (!NILP (display_info->mouse_face_window)
1959 && WINDOWP (display_info->mouse_face_window))
1960 {
1961 struct window *w = XWINDOW (display_info->mouse_face_window);
1962 int i;
1963
1964 /* If the mouse highlight is in the window that was deleted
1965 (e.g., if it was popped by completion), clear highlight
1966 unconditionally. */
1967 if (NILP (w->buffer))
1968 display_info->mouse_face_window = Qnil;
1969 else
1970 {
1971 for (i = 0; i < w->desired_matrix->nrows; ++i)
1972 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i)
1973 && MATRIX_ROW (w->current_matrix, i)->mouse_face_p)
1974 break;
1975 }
1976
1977 if (NILP (w->buffer) || i < w->desired_matrix->nrows)
1978 clear_mouse_face (display_info);
1979 }
1980 }
1981 else if (mouse_face_frame && !FRAME_LIVE_P (mouse_face_frame))
1982 {
1983 /* If the frame with mouse highlight was deleted, invalidate the
1984 highlight info. */
1985 display_info->mouse_face_beg_row = display_info->mouse_face_beg_col = -1;
1986 display_info->mouse_face_end_row = display_info->mouse_face_end_col = -1;
1987 display_info->mouse_face_window = Qnil;
1988 display_info->mouse_face_deferred_gc = 0;
1989 display_info->mouse_face_mouse_frame = NULL;
1990 }
1991
1992 UNBLOCK_INPUT;
1993 }
1994
1995 static void
1996 IT_update_end (struct frame *f)
1997 {
1998 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
1999 }
2000
2001 static void
2002 IT_frame_up_to_date (struct frame *f)
2003 {
2004 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2005 Lisp_Object new_cursor, frame_desired_cursor;
2006 struct window *sw;
2007
2008 if (dpyinfo->mouse_face_deferred_gc
2009 || (f && f == dpyinfo->mouse_face_mouse_frame))
2010 {
2011 BLOCK_INPUT;
2012 if (dpyinfo->mouse_face_mouse_frame)
2013 IT_note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
2014 dpyinfo->mouse_face_mouse_x,
2015 dpyinfo->mouse_face_mouse_y);
2016 dpyinfo->mouse_face_deferred_gc = 0;
2017 UNBLOCK_INPUT;
2018 }
2019
2020 /* Set the cursor type to whatever they wanted. In a minibuffer
2021 window, we want the cursor to appear only if we are reading input
2022 from this window, and we want the cursor to be taken from the
2023 frame parameters. For the selected window, we use either its
2024 buffer-local value or the value from the frame parameters if the
2025 buffer doesn't define its local value for the cursor type. */
2026 sw = XWINDOW (f->selected_window);
2027 frame_desired_cursor = Fcdr (Fassq (Qcursor_type, f->param_alist));
2028 if (cursor_in_echo_area
2029 && FRAME_HAS_MINIBUF_P (f)
2030 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window)
2031 && sw == XWINDOW (echo_area_window))
2032 new_cursor = frame_desired_cursor;
2033 else
2034 {
2035 struct buffer *b = XBUFFER (sw->buffer);
2036
2037 if (EQ (b->cursor_type, Qt))
2038 new_cursor = frame_desired_cursor;
2039 else if (NILP (b->cursor_type)) /* nil means no cursor */
2040 new_cursor = Fcons (Qbar, make_number (0));
2041 else
2042 new_cursor = b->cursor_type;
2043 }
2044
2045 IT_set_cursor_type (f, new_cursor);
2046
2047 IT_cmgoto (f); /* position cursor when update is done */
2048 }
2049
2050 /* Copy LEN glyphs displayed on a single line whose vertical position
2051 is YPOS, beginning at horizontal position XFROM to horizontal
2052 position XTO, by moving blocks in the video memory. Used by
2053 functions that insert and delete glyphs. */
2054 static void
2055 IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
2056 {
2057 /* The offsets of source and destination relative to the
2058 conventional memorty selector. */
2059 int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
2060 int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;
2061
2062 if (from == to || len <= 0)
2063 return;
2064
2065 _farsetsel (_dos_ds);
2066
2067 /* The source and destination might overlap, so we need to move
2068 glyphs non-destructively. */
2069 if (from > to)
2070 {
2071 for ( ; len; from += 2, to += 2, len--)
2072 _farnspokew (to, _farnspeekw (from));
2073 }
2074 else
2075 {
2076 from += (len - 1) * 2;
2077 to += (len - 1) * 2;
2078 for ( ; len; from -= 2, to -= 2, len--)
2079 _farnspokew (to, _farnspeekw (from));
2080 }
2081 if (screen_virtual_segment)
2082 dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
2083 }
2084
2085 /* Insert and delete glyphs. */
2086 static void
2087 IT_insert_glyphs (start, len)
2088 register struct glyph *start;
2089 register int len;
2090 {
2091 int shift_by_width = screen_size_X - (new_pos_X + len);
2092
2093 /* Shift right the glyphs from the nominal cursor position to the
2094 end of this line. */
2095 IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);
2096
2097 /* Now write the glyphs to be inserted. */
2098 IT_write_glyphs (start, len);
2099 }
2100
2101 static void
2102 IT_delete_glyphs (n)
2103 register int n;
2104 {
2105 abort ();
2106 }
2107
2108 /* set-window-configuration on window.c needs this. */
2109 void
2110 x_set_menu_bar_lines (f, value, oldval)
2111 struct frame *f;
2112 Lisp_Object value, oldval;
2113 {
2114 set_menu_bar_lines (f, value, oldval);
2115 }
2116
2117 /* This was copied from xfaces.c */
2118
2119 extern Lisp_Object Qbackground_color;
2120 extern Lisp_Object Qforeground_color;
2121 Lisp_Object Qreverse;
2122 extern Lisp_Object Qtitle;
2123
2124 /* IT_set_terminal_modes is called when emacs is started,
2125 resumed, and whenever the screen is redrawn! */
2126
2127 static void
2128 IT_set_terminal_modes (void)
2129 {
2130 if (termscript)
2131 fprintf (termscript, "\n<SET_TERM>");
2132
2133 screen_size_X = ScreenCols ();
2134 screen_size_Y = ScreenRows ();
2135 screen_size = screen_size_X * screen_size_Y;
2136
2137 new_pos_X = new_pos_Y = 0;
2138 current_pos_X = current_pos_Y = -1;
2139
2140 if (term_setup_done)
2141 return;
2142 term_setup_done = 1;
2143
2144 startup_screen_size_X = screen_size_X;
2145 startup_screen_size_Y = screen_size_Y;
2146 startup_screen_attrib = ScreenAttrib;
2147
2148 #if __DJGPP__ > 1
2149 /* Is DOS/V (or any other RSIS software which relocates
2150 the screen) installed? */
2151 {
2152 unsigned short es_value;
2153 __dpmi_regs regs;
2154
2155 regs.h.ah = 0xfe; /* get relocated screen address */
2156 if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
2157 regs.x.es = (ScreenPrimary >> 4) & 0xffff;
2158 else if (screen_old_address) /* already switched to Japanese mode once */
2159 regs.x.es = (screen_old_address >> 4) & 0xffff;
2160 else
2161 regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
2162 regs.x.di = 0;
2163 es_value = regs.x.es;
2164 __dpmi_int (0x10, &regs);
2165
2166 if (regs.x.es != es_value)
2167 {
2168 /* screen_old_address is only set if ScreenPrimary does NOT
2169 already point to the relocated buffer address returned by
2170 the Int 10h/AX=FEh call above. DJGPP v2.02 and later sets
2171 ScreenPrimary to that address at startup under DOS/V. */
2172 if (regs.x.es != (ScreenPrimary >> 4) & 0xffff)
2173 screen_old_address = ScreenPrimary;
2174 screen_virtual_segment = regs.x.es;
2175 screen_virtual_offset = regs.x.di;
2176 ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
2177 }
2178 }
2179 #endif /* __DJGPP__ > 1 */
2180
2181 ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
2182 ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));
2183
2184 if (termscript)
2185 fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
2186 screen_size_X, screen_size_Y);
2187
2188 bright_bg ();
2189 }
2190
2191 /* IT_reset_terminal_modes is called when emacs is
2192 suspended or killed. */
2193
2194 static void
2195 IT_reset_terminal_modes (void)
2196 {
2197 int display_row_start = (int) ScreenPrimary;
2198 int saved_row_len = startup_screen_size_X * 2;
2199 int update_row_len = ScreenCols () * 2, current_rows = ScreenRows ();
2200 int to_next_row = update_row_len;
2201 unsigned char *saved_row = startup_screen_buffer;
2202 int cursor_pos_X = ScreenCols () - 1, cursor_pos_Y = ScreenRows () - 1;
2203
2204 if (termscript)
2205 fprintf (termscript, "\n<RESET_TERM>");
2206
2207 if (!term_setup_done)
2208 return;
2209
2210 mouse_off ();
2211
2212 /* Leave the video system in the same state as we found it,
2213 as far as the blink/bright-background bit is concerned. */
2214 maybe_enable_blinking ();
2215
2216 /* We have a situation here.
2217 We cannot just do ScreenUpdate(startup_screen_buffer) because
2218 the luser could have changed screen dimensions inside Emacs
2219 and failed (or didn't want) to restore them before killing
2220 Emacs. ScreenUpdate() uses the *current* screen dimensions and
2221 thus will happily use memory outside what was allocated for
2222 `startup_screen_buffer'.
2223 Thus we only restore as much as the current screen dimensions
2224 can hold, and clear the rest (if the saved screen is smaller than
2225 the current) with the color attribute saved at startup. The cursor
2226 is also restored within the visible dimensions. */
2227
2228 ScreenAttrib = startup_screen_attrib;
2229
2230 /* Don't restore the screen if we are exiting less than 2 seconds
2231 after startup: we might be crashing, and the screen might show
2232 some vital clues to what's wrong. */
2233 if (clock () - startup_time >= 2*CLOCKS_PER_SEC)
2234 {
2235 ScreenClear ();
2236 if (screen_virtual_segment)
2237 dosv_refresh_virtual_screen (0, screen_size);
2238
2239 if (update_row_len > saved_row_len)
2240 update_row_len = saved_row_len;
2241 if (current_rows > startup_screen_size_Y)
2242 current_rows = startup_screen_size_Y;
2243
2244 if (termscript)
2245 fprintf (termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
2246 update_row_len / 2, current_rows);
2247
2248 while (current_rows--)
2249 {
2250 dosmemput (saved_row, update_row_len, display_row_start);
2251 if (screen_virtual_segment)
2252 dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
2253 update_row_len / 2);
2254 saved_row += saved_row_len;
2255 display_row_start += to_next_row;
2256 }
2257 }
2258 if (startup_pos_X < cursor_pos_X)
2259 cursor_pos_X = startup_pos_X;
2260 if (startup_pos_Y < cursor_pos_Y)
2261 cursor_pos_Y = startup_pos_Y;
2262
2263 ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
2264 xfree (startup_screen_buffer);
2265
2266 term_setup_done = 0;
2267 }
2268
2269 static void
2270 IT_set_terminal_window (int foo)
2271 {
2272 }
2273
2274 /* Remember the screen colors of the curent frame, to serve as the
2275 default colors for newly-created frames. */
2276 DEFUN ("msdos-remember-default-colors", Fmsdos_remember_default_colors,
2277 Smsdos_remember_default_colors, 1, 1, 0,
2278 doc: /* Remember the screen colors of the current frame. */)
2279 (frame)
2280 Lisp_Object frame;
2281 {
2282 struct frame *f;
2283
2284 CHECK_FRAME (frame);
2285 f= XFRAME (frame);
2286
2287 /* This function is called after applying default-frame-alist to the
2288 initial frame. At that time, if reverse-colors option was
2289 specified in default-frame-alist, it was already applied, and
2290 frame colors are reversed. We need to account for that. */
2291 if (EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt))
2292 {
2293 initial_screen_colors[0] = FRAME_BACKGROUND_PIXEL (f);
2294 initial_screen_colors[1] = FRAME_FOREGROUND_PIXEL (f);
2295 }
2296 else
2297 {
2298 initial_screen_colors[0] = FRAME_FOREGROUND_PIXEL (f);
2299 initial_screen_colors[1] = FRAME_BACKGROUND_PIXEL (f);
2300 }
2301 }
2302
2303 void
2304 IT_set_frame_parameters (f, alist)
2305 struct frame *f;
2306 Lisp_Object alist;
2307 {
2308 Lisp_Object tail;
2309 int i, j, length = XINT (Flength (alist));
2310 Lisp_Object *parms
2311 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2312 Lisp_Object *values
2313 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2314 /* Do we have to reverse the foreground and background colors? */
2315 int reverse = EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt);
2316 int need_to_reverse, was_reverse = reverse;
2317 int redraw = 0, fg_set = 0, bg_set = 0;
2318 unsigned long orig_fg, orig_bg;
2319 Lisp_Object frame_bg, frame_fg;
2320 extern Lisp_Object Qdefault, QCforeground, QCbackground;
2321
2322 /* If we are creating a new frame, begin with the original screen colors
2323 used for the initial frame. */
2324 if (EQ (alist, Vdefault_frame_alist)
2325 && initial_screen_colors[0] != -1 && initial_screen_colors[1] != -1)
2326 {
2327 FRAME_FOREGROUND_PIXEL (f) = initial_screen_colors[0];
2328 FRAME_BACKGROUND_PIXEL (f) = initial_screen_colors[1];
2329 }
2330 orig_fg = FRAME_FOREGROUND_PIXEL (f);
2331 orig_bg = FRAME_BACKGROUND_PIXEL (f);
2332 frame_fg = Fcdr (Fassq (Qforeground_color, f->param_alist));
2333 frame_bg = Fcdr (Fassq (Qbackground_color, f->param_alist));
2334 /* frame_fg and frame_bg could be nil if, for example,
2335 f->param_alist is nil, e.g. if we are called from
2336 Fmake_terminal_frame. */
2337 if (NILP (frame_fg))
2338 frame_fg = build_string (unspecified_fg);
2339 if (NILP (frame_bg))
2340 frame_bg = build_string (unspecified_bg);
2341
2342 /* Extract parm names and values into those vectors. */
2343 i = 0;
2344 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
2345 {
2346 Lisp_Object elt;
2347
2348 elt = Fcar (tail);
2349 parms[i] = Fcar (elt);
2350 CHECK_SYMBOL (parms[i]);
2351 values[i] = Fcdr (elt);
2352 i++;
2353 }
2354
2355 j = i;
2356
2357 for (i = 0; i < j; i++)
2358 {
2359 Lisp_Object prop, val;
2360
2361 prop = parms[i];
2362 val = values[i];
2363
2364 if (EQ (prop, Qreverse))
2365 reverse = EQ (val, Qt);
2366 }
2367
2368 need_to_reverse = reverse && !was_reverse;
2369 if (termscript && need_to_reverse)
2370 fprintf (termscript, "<INVERSE-VIDEO>\n");
2371
2372 /* Now process the alist elements in reverse of specified order. */
2373 for (i--; i >= 0; i--)
2374 {
2375 Lisp_Object prop, val, frame;
2376
2377 prop = parms[i];
2378 val = values[i];
2379
2380 if (EQ (prop, Qforeground_color))
2381 {
2382 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2383 ? LFACE_BACKGROUND_INDEX
2384 : LFACE_FOREGROUND_INDEX);
2385 if (new_color != FACE_TTY_DEFAULT_COLOR
2386 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2387 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2388 {
2389 FRAME_FOREGROUND_PIXEL (f) = new_color;
2390 /* Make sure the foreground of the default face for this
2391 frame is changed as well. */
2392 XSETFRAME (frame, f);
2393 if (need_to_reverse)
2394 {
2395 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2396 val, frame);
2397 prop = Qbackground_color;
2398 bg_set = 1;
2399 }
2400 else
2401 {
2402 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2403 val, frame);
2404 fg_set = 1;
2405 }
2406 redraw = 1;
2407 if (termscript)
2408 fprintf (termscript, "<FGCOLOR %lu>\n", new_color);
2409 }
2410 }
2411 else if (EQ (prop, Qbackground_color))
2412 {
2413 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2414 ? LFACE_FOREGROUND_INDEX
2415 : LFACE_BACKGROUND_INDEX);
2416 if (new_color != FACE_TTY_DEFAULT_COLOR
2417 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2418 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2419 {
2420 FRAME_BACKGROUND_PIXEL (f) = new_color;
2421 /* Make sure the background of the default face for this
2422 frame is changed as well. */
2423 XSETFRAME (frame, f);
2424 if (need_to_reverse)
2425 {
2426 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2427 val, frame);
2428 prop = Qforeground_color;
2429 fg_set = 1;
2430 }
2431 else
2432 {
2433 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2434 val, frame);
2435 bg_set = 1;
2436 }
2437 redraw = 1;
2438 if (termscript)
2439 fprintf (termscript, "<BGCOLOR %lu>\n", new_color);
2440 }
2441 }
2442 else if (EQ (prop, Qtitle))
2443 {
2444 x_set_title (f, val);
2445 if (termscript)
2446 fprintf (termscript, "<TITLE: %s>\n", SDATA (val));
2447 }
2448 else if (EQ (prop, Qcursor_type))
2449 {
2450 IT_set_cursor_type (f, val);
2451 if (termscript)
2452 fprintf (termscript, "<CTYPE: %s>\n",
2453 EQ (val, Qbar) || EQ (val, Qhbar)
2454 || CONSP (val) && (EQ (XCAR (val), Qbar)
2455 || EQ (XCAR (val), Qhbar))
2456 ? "bar" : "box");
2457 }
2458 store_frame_param (f, prop, val);
2459 }
2460
2461 /* If they specified "reverse", but not the colors, we need to swap
2462 the current frame colors. */
2463 if (need_to_reverse)
2464 {
2465 Lisp_Object frame;
2466
2467 if (!fg_set)
2468 {
2469 XSETFRAME (frame, f);
2470 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2471 tty_color_name (f, orig_bg),
2472 frame);
2473 redraw = 1;
2474 }
2475 if (!bg_set)
2476 {
2477 XSETFRAME (frame, f);
2478 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2479 tty_color_name (f, orig_fg),
2480 frame);
2481 redraw = 1;
2482 }
2483 }
2484
2485 if (redraw)
2486 {
2487 face_change_count++; /* forces xdisp.c to recompute basic faces */
2488 if (f == SELECTED_FRAME())
2489 redraw_frame (f);
2490 }
2491 }
2492
2493 extern void init_frame_faces (FRAME_PTR);
2494
2495 #endif /* !HAVE_X_WINDOWS */
2496
2497
2498 /* Do we need the internal terminal? */
2499
2500 void
2501 internal_terminal_init ()
2502 {
2503 char *term = getenv ("TERM"), *colors;
2504 struct frame *sf = SELECTED_FRAME();
2505
2506 #ifdef HAVE_X_WINDOWS
2507 if (!inhibit_window_system)
2508 return;
2509 #endif
2510
2511 internal_terminal
2512 = (!noninteractive) && term && !strcmp (term, "internal");
2513
2514 if (getenv ("EMACSTEST"))
2515 termscript = fopen (getenv ("EMACSTEST"), "wt");
2516
2517 #ifndef HAVE_X_WINDOWS
2518 if (!internal_terminal || inhibit_window_system)
2519 {
2520 sf->output_method = output_termcap;
2521 return;
2522 }
2523
2524 Vwindow_system = intern ("pc");
2525 Vwindow_system_version = make_number (1);
2526 sf->output_method = output_msdos_raw;
2527
2528 /* If Emacs was dumped on DOS/V machine, forget the stale VRAM address. */
2529 screen_old_address = 0;
2530
2531 /* Forget the stale screen colors as well. */
2532 initial_screen_colors[0] = initial_screen_colors[1] = -1;
2533
2534 bzero (&the_only_x_display, sizeof the_only_x_display);
2535 FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()) = 7; /* White */
2536 FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ()) = 0; /* Black */
2537 bright_bg ();
2538 colors = getenv ("EMACSCOLORS");
2539 if (colors && strlen (colors) >= 2)
2540 {
2541 /* The colors use 4 bits each (we enable bright background). */
2542 if (isdigit (colors[0]))
2543 colors[0] -= '0';
2544 else if (isxdigit (colors[0]))
2545 colors[0] -= (isupper (colors[0]) ? 'A' : 'a') - 10;
2546 if (colors[0] >= 0 && colors[0] < 16)
2547 FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ()) = colors[0];
2548 if (isdigit (colors[1]))
2549 colors[1] -= '0';
2550 else if (isxdigit (colors[1]))
2551 colors[1] -= (isupper (colors[1]) ? 'A' : 'a') - 10;
2552 if (colors[1] >= 0 && colors[1] < 16)
2553 FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()) = colors[1];
2554 }
2555 the_only_x_display.font = (XFontStruct *)1; /* must *not* be zero */
2556 the_only_x_display.display_info.mouse_face_mouse_frame = NULL;
2557 the_only_x_display.display_info.mouse_face_deferred_gc = 0;
2558 the_only_x_display.display_info.mouse_face_beg_row =
2559 the_only_x_display.display_info.mouse_face_beg_col = -1;
2560 the_only_x_display.display_info.mouse_face_end_row =
2561 the_only_x_display.display_info.mouse_face_end_col = -1;
2562 the_only_x_display.display_info.mouse_face_face_id = DEFAULT_FACE_ID;
2563 the_only_x_display.display_info.mouse_face_window = Qnil;
2564 the_only_x_display.display_info.mouse_face_mouse_x =
2565 the_only_x_display.display_info.mouse_face_mouse_y = 0;
2566 the_only_x_display.display_info.mouse_face_defer = 0;
2567 the_only_x_display.display_info.mouse_face_hidden = 0;
2568
2569 init_frame_faces (sf);
2570
2571 ring_bell_hook = IT_ring_bell;
2572 insert_glyphs_hook = IT_insert_glyphs;
2573 delete_glyphs_hook = IT_delete_glyphs;
2574 write_glyphs_hook = IT_write_glyphs;
2575 cursor_to_hook = raw_cursor_to_hook = IT_cursor_to;
2576 clear_to_end_hook = IT_clear_to_end;
2577 clear_end_of_line_hook = IT_clear_end_of_line;
2578 clear_frame_hook = IT_clear_screen;
2579 update_begin_hook = IT_update_begin;
2580 update_end_hook = IT_update_end;
2581 frame_up_to_date_hook = IT_frame_up_to_date;
2582
2583 /* These hooks are called by term.c without being checked. */
2584 set_terminal_modes_hook = IT_set_terminal_modes;
2585 reset_terminal_modes_hook = IT_reset_terminal_modes;
2586 set_terminal_window_hook = IT_set_terminal_window;
2587 TTY_CHAR_INS_DEL_OK (CURTTY ()) = 0;
2588 #endif
2589 }
2590
2591 dos_get_saved_screen (screen, rows, cols)
2592 char **screen;
2593 int *rows;
2594 int *cols;
2595 {
2596 #ifndef HAVE_X_WINDOWS
2597 *screen = startup_screen_buffer;
2598 *cols = startup_screen_size_X;
2599 *rows = startup_screen_size_Y;
2600 return *screen != (char *)0;
2601 #else
2602 return 0;
2603 #endif
2604 }
2605
2606 #ifndef HAVE_X_WINDOWS
2607
2608 /* We are not X, but we can emulate it well enough for our needs... */
2609 void
2610 check_x (void)
2611 {
2612 if (! FRAME_MSDOS_P (SELECTED_FRAME()))
2613 error ("Not running under a window system");
2614 }
2615
2616 #endif
2617
2618 \f
2619 /* ----------------------- Keyboard control ----------------------
2620 *
2621 * Keymaps reflect the following keyboard layout:
2622 *
2623 * 0 1 2 3 4 5 6 7 8 9 10 11 12 BS
2624 * TAB 15 16 17 18 19 20 21 22 23 24 25 26 (41)
2625 * CLOK 30 31 32 33 34 35 36 37 38 39 40 (41) RET
2626 * SH () 45 46 47 48 49 50 51 52 53 54 SHIFT
2627 * SPACE
2628 */
2629
2630 #define Ignore 0x0000
2631 #define Normal 0x0000 /* normal key - alt changes scan-code */
2632 #define FctKey 0x1000 /* func key if c == 0, else c */
2633 #define Special 0x2000 /* func key even if c != 0 */
2634 #define ModFct 0x3000 /* special if mod-keys, else 'c' */
2635 #define Map 0x4000 /* alt scan-code, map to unshift/shift key */
2636 #define KeyPad 0x5000 /* map to insert/kp-0 depending on c == 0xe0 */
2637 #define Grey 0x6000 /* Grey keypad key */
2638
2639 #define Alt 0x0100 /* alt scan-code */
2640 #define Ctrl 0x0200 /* ctrl scan-code */
2641 #define Shift 0x0400 /* shift scan-code */
2642
2643 static int extended_kbd; /* 101 (102) keyboard present. */
2644
2645 struct kbd_translate {
2646 unsigned char sc;
2647 unsigned char ch;
2648 unsigned short code;
2649 };
2650
2651 struct dos_keyboard_map
2652 {
2653 char *unshifted;
2654 char *shifted;
2655 char *alt_gr;
2656 struct kbd_translate *translate_table;
2657 };
2658
2659
2660 static struct dos_keyboard_map us_keyboard = {
2661 /* 0 1 2 3 4 5 */
2662 /* 01234567890123456789012345678901234567890 12345678901234 */
2663 "`1234567890-= qwertyuiop[] asdfghjkl;'\\ zxcvbnm,./ ",
2664 /* 0123456789012345678901234567890123456789 012345678901234 */
2665 "~!@#$%^&*()_+ QWERTYUIOP{} ASDFGHJKL:\"| ZXCVBNM<>? ",
2666 0, /* no Alt-Gr key */
2667 0 /* no translate table */
2668 };
2669
2670 static struct dos_keyboard_map fr_keyboard = {
2671 /* 0 1 2 3 4 5 */
2672 /* 012 3456789012345678901234567890123456789012345678901234 */
2673 "ý&\82\",(-\8a_\80\85)= azertyuiop^$ qsdfghjklm\97* wxcvbnm;:! ",
2674 /* 0123456789012345678901234567890123456789012345678901234 */
2675 " 1234567890ø+ AZERTYUIOPù\9c QSDFGHJKLM%æ WXCVBN?./õ ",
2676 /* 01234567 89012345678901234567890123456789012345678901234 */
2677 " ~#{[|`\\^@]} Ï ",
2678 0 /* no translate table */
2679 };
2680
2681 /*
2682 * Italian keyboard support, country code 39.
2683 * '<' 56:3c*0000
2684 * '>' 56:3e*0000
2685 * added also {,},` as, respectively, AltGr-8, AltGr-9, AltGr-'
2686 * Donated by Stefano Brozzi <brozzis@mag00.cedi.unipr.it>
2687 */
2688
2689 static struct kbd_translate it_kbd_translate_table[] = {
2690 { 0x56, 0x3c, Normal | 13 },
2691 { 0x56, 0x3e, Normal | 27 },
2692 { 0, 0, 0 }
2693 };
2694 static struct dos_keyboard_map it_keyboard = {
2695 /* 0 1 2 3 4 5 */
2696 /* 0 123456789012345678901234567890123456789012345678901234 */
2697 "\\1234567890'\8d< qwertyuiop\8a+> asdfghjkl\95\85\97 zxcvbnm,.- ",
2698 /* 01 23456789012345678901234567890123456789012345678901234 */
2699 "|!\"\9c$%&/()=?^> QWERTYUIOP\82* ASDFGHJKL\87øõ ZXCVBNM;:_ ",
2700 /* 0123456789012345678901234567890123456789012345678901234 */
2701 " {}~` [] @# ",
2702 it_kbd_translate_table
2703 };
2704
2705 static struct dos_keyboard_map dk_keyboard = {
2706 /* 0 1 2 3 4 5 */
2707 /* 0123456789012345678901234567890123456789012345678901234 */
2708 "«1234567890+| qwertyuiop\86~ asdfghjkl\91\9b' zxcvbnm,.- ",
2709 /* 01 23456789012345678901234567890123456789012345678901234 */
2710 "õ!\"#$%&/()=?` QWERTYUIOP\8f^ ASDFGHJKL\92\9d* ZXCVBNM;:_ ",
2711 /* 0123456789012345678901234567890123456789012345678901234 */
2712 " @\9c$ {[]} | ",
2713 0 /* no translate table */
2714 };
2715
2716 static struct kbd_translate jp_kbd_translate_table[] = {
2717 { 0x73, 0x5c, Normal | 0 },
2718 { 0x73, 0x5f, Normal | 0 },
2719 { 0x73, 0x1c, Map | 0 },
2720 { 0x7d, 0x5c, Normal | 13 },
2721 { 0x7d, 0x7c, Normal | 13 },
2722 { 0x7d, 0x1c, Map | 13 },
2723 { 0, 0, 0 }
2724 };
2725 static struct dos_keyboard_map jp_keyboard = {
2726 /* 0 1 2 3 4 5 */
2727 /* 0123456789012 345678901234567890123456789012345678901234 */
2728 "\\1234567890-^\\ qwertyuiop@[ asdfghjkl;:] zxcvbnm,./ ",
2729 /* 01 23456789012345678901234567890123456789012345678901234 */
2730 "_!\"#$%&'()~=~| QWERTYUIOP`{ ASDFGHJKL+*} ZXCVBNM<>? ",
2731 0, /* no Alt-Gr key */
2732 jp_kbd_translate_table
2733 };
2734
2735 static struct keyboard_layout_list
2736 {
2737 int country_code;
2738 struct dos_keyboard_map *keyboard_map;
2739 } keyboard_layout_list[] =
2740 {
2741 1, &us_keyboard,
2742 33, &fr_keyboard,
2743 39, &it_keyboard,
2744 45, &dk_keyboard,
2745 81, &jp_keyboard
2746 };
2747
2748 static struct dos_keyboard_map *keyboard;
2749 static int keyboard_map_all;
2750 static int international_keyboard;
2751
2752 int
2753 dos_set_keyboard (code, always)
2754 int code;
2755 int always;
2756 {
2757 int i;
2758 _go32_dpmi_registers regs;
2759
2760 /* See if Keyb.Com is installed (for international keyboard support).
2761 Note: calling Int 2Fh via int86 wedges the DOS box on some versions
2762 of Windows 9X! So don't do that! */
2763 regs.x.ax = 0xad80;
2764 regs.x.ss = regs.x.sp = regs.x.flags = 0;
2765 _go32_dpmi_simulate_int (0x2f, &regs);
2766 if (regs.h.al == 0xff)
2767 international_keyboard = 1;
2768
2769 /* Initialize to US settings, for countries that don't have their own. */
2770 keyboard = keyboard_layout_list[0].keyboard_map;
2771 keyboard_map_all = always;
2772 dos_keyboard_layout = 1;
2773
2774 for (i = 0; i < (sizeof (keyboard_layout_list)/sizeof (struct keyboard_layout_list)); i++)
2775 if (code == keyboard_layout_list[i].country_code)
2776 {
2777 keyboard = keyboard_layout_list[i].keyboard_map;
2778 keyboard_map_all = always;
2779 dos_keyboard_layout = code;
2780 return 1;
2781 }
2782 return 0;
2783 }
2784 \f
2785 static struct
2786 {
2787 unsigned char char_code; /* normal code */
2788 unsigned char meta_code; /* M- code */
2789 unsigned char keypad_code; /* keypad code */
2790 unsigned char editkey_code; /* edit key */
2791 } keypad_translate_map[] = {
2792 '0', '0', 0xb0, /* kp-0 */ 0x63, /* insert */
2793 '1', '1', 0xb1, /* kp-1 */ 0x57, /* end */
2794 '2', '2', 0xb2, /* kp-2 */ 0x54, /* down */
2795 '3', '3', 0xb3, /* kp-3 */ 0x56, /* next */
2796 '4', '4', 0xb4, /* kp-4 */ 0x51, /* left */
2797 '5', '5', 0xb5, /* kp-5 */ 0xb5, /* kp-5 */
2798 '6', '6', 0xb6, /* kp-6 */ 0x53, /* right */
2799 '7', '7', 0xb7, /* kp-7 */ 0x50, /* home */
2800 '8', '8', 0xb8, /* kp-8 */ 0x52, /* up */
2801 '9', '9', 0xb9, /* kp-9 */ 0x55, /* prior */
2802 '.', '-', 0xae, /* kp-decimal */ 0xff /* delete */
2803 };
2804
2805 static struct
2806 {
2807 unsigned char char_code; /* normal code */
2808 unsigned char keypad_code; /* keypad code */
2809 } grey_key_translate_map[] = {
2810 '/', 0xaf, /* kp-decimal */
2811 '*', 0xaa, /* kp-multiply */
2812 '-', 0xad, /* kp-subtract */
2813 '+', 0xab, /* kp-add */
2814 '\r', 0x8d /* kp-enter */
2815 };
2816
2817 static unsigned short
2818 ibmpc_translate_map[] =
2819 {
2820 /* --------------- 00 to 0f --------------- */
2821 Normal | 0xff, /* Ctrl Break + Alt-NNN */
2822 Alt | ModFct | 0x1b, /* Escape */
2823 Normal | 1, /* '1' */
2824 Normal | 2, /* '2' */
2825 Normal | 3, /* '3' */
2826 Normal | 4, /* '4' */
2827 Normal | 5, /* '5' */
2828 Normal | 6, /* '6' */
2829 Normal | 7, /* '7' */
2830 Normal | 8, /* '8' */
2831 Normal | 9, /* '9' */
2832 Normal | 10, /* '0' */
2833 Normal | 11, /* '-' */
2834 Normal | 12, /* '=' */
2835 Special | 0x08, /* Backspace */
2836 ModFct | 0x74, /* Tab/Backtab */
2837
2838 /* --------------- 10 to 1f --------------- */
2839 Map | 15, /* 'q' */
2840 Map | 16, /* 'w' */
2841 Map | 17, /* 'e' */
2842 Map | 18, /* 'r' */
2843 Map | 19, /* 't' */
2844 Map | 20, /* 'y' */
2845 Map | 21, /* 'u' */
2846 Map | 22, /* 'i' */
2847 Map | 23, /* 'o' */
2848 Map | 24, /* 'p' */
2849 Map | 25, /* '[' */
2850 Map | 26, /* ']' */
2851 ModFct | 0x0d, /* Return */
2852 Ignore, /* Ctrl */
2853 Map | 30, /* 'a' */
2854 Map | 31, /* 's' */
2855
2856 /* --------------- 20 to 2f --------------- */
2857 Map | 32, /* 'd' */
2858 Map | 33, /* 'f' */
2859 Map | 34, /* 'g' */
2860 Map | 35, /* 'h' */
2861 Map | 36, /* 'j' */
2862 Map | 37, /* 'k' */
2863 Map | 38, /* 'l' */
2864 Map | 39, /* ';' */
2865 Map | 40, /* '\'' */
2866 Map | 0, /* '`' */
2867 Ignore, /* Left shift */
2868 Map | 41, /* '\\' */
2869 Map | 45, /* 'z' */
2870 Map | 46, /* 'x' */
2871 Map | 47, /* 'c' */
2872 Map | 48, /* 'v' */
2873
2874 /* --------------- 30 to 3f --------------- */
2875 Map | 49, /* 'b' */
2876 Map | 50, /* 'n' */
2877 Map | 51, /* 'm' */
2878 Map | 52, /* ',' */
2879 Map | 53, /* '.' */
2880 Map | 54, /* '/' */
2881 Ignore, /* Right shift */
2882 Grey | 1, /* Grey * */
2883 Ignore, /* Alt */
2884 Normal | 55, /* ' ' */
2885 Ignore, /* Caps Lock */
2886 FctKey | 0xbe, /* F1 */
2887 FctKey | 0xbf, /* F2 */
2888 FctKey | 0xc0, /* F3 */
2889 FctKey | 0xc1, /* F4 */
2890 FctKey | 0xc2, /* F5 */
2891
2892 /* --------------- 40 to 4f --------------- */
2893 FctKey | 0xc3, /* F6 */
2894 FctKey | 0xc4, /* F7 */
2895 FctKey | 0xc5, /* F8 */
2896 FctKey | 0xc6, /* F9 */
2897 FctKey | 0xc7, /* F10 */
2898 Ignore, /* Num Lock */
2899 Ignore, /* Scroll Lock */
2900 KeyPad | 7, /* Home */
2901 KeyPad | 8, /* Up */
2902 KeyPad | 9, /* Page Up */
2903 Grey | 2, /* Grey - */
2904 KeyPad | 4, /* Left */
2905 KeyPad | 5, /* Keypad 5 */
2906 KeyPad | 6, /* Right */
2907 Grey | 3, /* Grey + */
2908 KeyPad | 1, /* End */
2909
2910 /* --------------- 50 to 5f --------------- */
2911 KeyPad | 2, /* Down */
2912 KeyPad | 3, /* Page Down */
2913 KeyPad | 0, /* Insert */
2914 KeyPad | 10, /* Delete */
2915 Shift | FctKey | 0xbe, /* (Shift) F1 */
2916 Shift | FctKey | 0xbf, /* (Shift) F2 */
2917 Shift | FctKey | 0xc0, /* (Shift) F3 */
2918 Shift | FctKey | 0xc1, /* (Shift) F4 */
2919 Shift | FctKey | 0xc2, /* (Shift) F5 */
2920 Shift | FctKey | 0xc3, /* (Shift) F6 */
2921 Shift | FctKey | 0xc4, /* (Shift) F7 */
2922 Shift | FctKey | 0xc5, /* (Shift) F8 */
2923 Shift | FctKey | 0xc6, /* (Shift) F9 */
2924 Shift | FctKey | 0xc7, /* (Shift) F10 */
2925 Ctrl | FctKey | 0xbe, /* (Ctrl) F1 */
2926 Ctrl | FctKey | 0xbf, /* (Ctrl) F2 */
2927
2928 /* --------------- 60 to 6f --------------- */
2929 Ctrl | FctKey | 0xc0, /* (Ctrl) F3 */
2930 Ctrl | FctKey | 0xc1, /* (Ctrl) F4 */
2931 Ctrl | FctKey | 0xc2, /* (Ctrl) F5 */
2932 Ctrl | FctKey | 0xc3, /* (Ctrl) F6 */
2933 Ctrl | FctKey | 0xc4, /* (Ctrl) F7 */
2934 Ctrl | FctKey | 0xc5, /* (Ctrl) F8 */
2935 Ctrl | FctKey | 0xc6, /* (Ctrl) F9 */
2936 Ctrl | FctKey | 0xc7, /* (Ctrl) F10 */
2937 Alt | FctKey | 0xbe, /* (Alt) F1 */
2938 Alt | FctKey | 0xbf, /* (Alt) F2 */
2939 Alt | FctKey | 0xc0, /* (Alt) F3 */
2940 Alt | FctKey | 0xc1, /* (Alt) F4 */
2941 Alt | FctKey | 0xc2, /* (Alt) F5 */
2942 Alt | FctKey | 0xc3, /* (Alt) F6 */
2943 Alt | FctKey | 0xc4, /* (Alt) F7 */
2944 Alt | FctKey | 0xc5, /* (Alt) F8 */
2945
2946 /* --------------- 70 to 7f --------------- */
2947 Alt | FctKey | 0xc6, /* (Alt) F9 */
2948 Alt | FctKey | 0xc7, /* (Alt) F10 */
2949 Ctrl | FctKey | 0x6d, /* (Ctrl) Sys Rq */
2950 Ctrl | KeyPad | 4, /* (Ctrl) Left */
2951 Ctrl | KeyPad | 6, /* (Ctrl) Right */
2952 Ctrl | KeyPad | 1, /* (Ctrl) End */
2953 Ctrl | KeyPad | 3, /* (Ctrl) Page Down */
2954 Ctrl | KeyPad | 7, /* (Ctrl) Home */
2955 Alt | Map | 1, /* '1' */
2956 Alt | Map | 2, /* '2' */
2957 Alt | Map | 3, /* '3' */
2958 Alt | Map | 4, /* '4' */
2959 Alt | Map | 5, /* '5' */
2960 Alt | Map | 6, /* '6' */
2961 Alt | Map | 7, /* '7' */
2962 Alt | Map | 8, /* '8' */
2963
2964 /* --------------- 80 to 8f --------------- */
2965 Alt | Map | 9, /* '9' */
2966 Alt | Map | 10, /* '0' */
2967 Alt | Map | 11, /* '-' */
2968 Alt | Map | 12, /* '=' */
2969 Ctrl | KeyPad | 9, /* (Ctrl) Page Up */
2970 FctKey | 0xc8, /* F11 */
2971 FctKey | 0xc9, /* F12 */
2972 Shift | FctKey | 0xc8, /* (Shift) F11 */
2973 Shift | FctKey | 0xc9, /* (Shift) F12 */
2974 Ctrl | FctKey | 0xc8, /* (Ctrl) F11 */
2975 Ctrl | FctKey | 0xc9, /* (Ctrl) F12 */
2976 Alt | FctKey | 0xc8, /* (Alt) F11 */
2977 Alt | FctKey | 0xc9, /* (Alt) F12 */
2978 Ctrl | KeyPad | 8, /* (Ctrl) Up */
2979 Ctrl | Grey | 2, /* (Ctrl) Grey - */
2980 Ctrl | KeyPad | 5, /* (Ctrl) Keypad 5 */
2981
2982 /* --------------- 90 to 9f --------------- */
2983 Ctrl | Grey | 3, /* (Ctrl) Grey + */
2984 Ctrl | KeyPad | 2, /* (Ctrl) Down */
2985 Ctrl | KeyPad | 0, /* (Ctrl) Insert */
2986 Ctrl | KeyPad | 10, /* (Ctrl) Delete */
2987 Ctrl | FctKey | 0x09, /* (Ctrl) Tab */
2988 Ctrl | Grey | 0, /* (Ctrl) Grey / */
2989 Ctrl | Grey | 1, /* (Ctrl) Grey * */
2990 Alt | FctKey | 0x50, /* (Alt) Home */
2991 Alt | FctKey | 0x52, /* (Alt) Up */
2992 Alt | FctKey | 0x55, /* (Alt) Page Up */
2993 Ignore, /* NO KEY */
2994 Alt | FctKey | 0x51, /* (Alt) Left */
2995 Ignore, /* NO KEY */
2996 Alt | FctKey | 0x53, /* (Alt) Right */
2997 Ignore, /* NO KEY */
2998 Alt | FctKey | 0x57, /* (Alt) End */
2999
3000 /* --------------- a0 to af --------------- */
3001 Alt | KeyPad | 2, /* (Alt) Down */
3002 Alt | KeyPad | 3, /* (Alt) Page Down */
3003 Alt | KeyPad | 0, /* (Alt) Insert */
3004 Alt | KeyPad | 10, /* (Alt) Delete */
3005 Alt | Grey | 0, /* (Alt) Grey / */
3006 Alt | FctKey | 0x09, /* (Alt) Tab */
3007 Alt | Grey | 4 /* (Alt) Keypad Enter */
3008 };
3009 \f
3010 /* These bit-positions corresponds to values returned by BIOS */
3011 #define SHIFT_P 0x0003 /* two bits! */
3012 #define CTRL_P 0x0004
3013 #define ALT_P 0x0008
3014 #define SCRLOCK_P 0x0010
3015 #define NUMLOCK_P 0x0020
3016 #define CAPSLOCK_P 0x0040
3017 #define ALT_GR_P 0x0800
3018 #define SUPER_P 0x4000 /* pseudo */
3019 #define HYPER_P 0x8000 /* pseudo */
3020
3021 static int
3022 dos_get_modifiers (keymask)
3023 int *keymask;
3024 {
3025 union REGS regs;
3026 int mask, modifiers = 0;
3027
3028 /* Calculate modifier bits */
3029 regs.h.ah = extended_kbd ? 0x12 : 0x02;
3030 int86 (0x16, &regs, &regs);
3031
3032 if (!extended_kbd)
3033 {
3034 mask = regs.h.al & (SHIFT_P | CTRL_P | ALT_P |
3035 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
3036 }
3037 else
3038 {
3039 mask = regs.h.al & (SHIFT_P |
3040 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
3041
3042 /* Do not break international keyboard support. */
3043 /* When Keyb.Com is loaded, the right Alt key is */
3044 /* used for accessing characters like { and } */
3045 if (regs.h.ah & 2) /* Left ALT pressed ? */
3046 mask |= ALT_P;
3047
3048 if ((regs.h.ah & 8) != 0) /* Right ALT pressed ? */
3049 {
3050 mask |= ALT_GR_P;
3051 if (dos_hyper_key == 1)
3052 {
3053 mask |= HYPER_P;
3054 modifiers |= hyper_modifier;
3055 }
3056 else if (dos_super_key == 1)
3057 {
3058 mask |= SUPER_P;
3059 modifiers |= super_modifier;
3060 }
3061 else if (!international_keyboard)
3062 {
3063 /* If Keyb.Com is NOT installed, let Right Alt behave
3064 like the Left Alt. */
3065 mask &= ~ALT_GR_P;
3066 mask |= ALT_P;
3067 }
3068 }
3069
3070 if (regs.h.ah & 1) /* Left CTRL pressed ? */
3071 mask |= CTRL_P;
3072
3073 if (regs.h.ah & 4) /* Right CTRL pressed ? */
3074 {
3075 if (dos_hyper_key == 2)
3076 {
3077 mask |= HYPER_P;
3078 modifiers |= hyper_modifier;
3079 }
3080 else if (dos_super_key == 2)
3081 {
3082 mask |= SUPER_P;
3083 modifiers |= super_modifier;
3084 }
3085 else
3086 mask |= CTRL_P;
3087 }
3088 }
3089
3090 if (mask & SHIFT_P)
3091 modifiers |= shift_modifier;
3092 if (mask & CTRL_P)
3093 modifiers |= ctrl_modifier;
3094 if (mask & ALT_P)
3095 modifiers |= meta_modifier;
3096
3097 if (keymask)
3098 *keymask = mask;
3099 return modifiers;
3100 }
3101
3102 #define NUM_RECENT_DOSKEYS (100)
3103 int recent_doskeys_index; /* Index for storing next element into recent_doskeys */
3104 int total_doskeys; /* Total number of elements stored into recent_doskeys */
3105 Lisp_Object recent_doskeys; /* A vector, holding the last 100 keystrokes */
3106
3107 DEFUN ("recent-doskeys", Frecent_doskeys, Srecent_doskeys, 0, 0, 0,
3108 doc: /* Return vector of last 100 keyboard input values seen in dos_rawgetc.
3109 Each input key receives two values in this vector: first the ASCII code,
3110 and then the scan code. */)
3111 ()
3112 {
3113 Lisp_Object val, *keys = XVECTOR (recent_doskeys)->contents;
3114
3115 if (total_doskeys < NUM_RECENT_DOSKEYS)
3116 return Fvector (total_doskeys, keys);
3117 else
3118 {
3119 val = Fvector (NUM_RECENT_DOSKEYS, keys);
3120 bcopy (keys + recent_doskeys_index,
3121 XVECTOR (val)->contents,
3122 (NUM_RECENT_DOSKEYS - recent_doskeys_index) * sizeof (Lisp_Object));
3123 bcopy (keys,
3124 XVECTOR (val)->contents + NUM_RECENT_DOSKEYS - recent_doskeys_index,
3125 recent_doskeys_index * sizeof (Lisp_Object));
3126 return val;
3127 }
3128 }
3129
3130 /* Get a char from keyboard. Function keys are put into the event queue. */
3131 static int
3132 dos_rawgetc ()
3133 {
3134 struct input_event event;
3135 union REGS regs;
3136 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (SELECTED_FRAME());
3137 EVENT_INIT (event);
3138
3139 #ifndef HAVE_X_WINDOWS
3140 /* Maybe put the cursor where it should be. */
3141 IT_cmgoto (SELECTED_FRAME());
3142 #endif
3143
3144 /* The following condition is equivalent to `kbhit ()', except that
3145 it uses the bios to do its job. This pleases DESQview/X. */
3146 while ((regs.h.ah = extended_kbd ? 0x11 : 0x01),
3147 int86 (0x16, &regs, &regs),
3148 (regs.x.flags & 0x40) == 0)
3149 {
3150 union REGS regs;
3151 register unsigned char c;
3152 int modifiers, sc, code = -1, mask, kp_mode;
3153
3154 regs.h.ah = extended_kbd ? 0x10 : 0x00;
3155 int86 (0x16, &regs, &regs);
3156 c = regs.h.al;
3157 sc = regs.h.ah;
3158
3159 total_doskeys += 2;
3160 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
3161 = make_number (c);
3162 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
3163 recent_doskeys_index = 0;
3164 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
3165 = make_number (sc);
3166 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
3167 recent_doskeys_index = 0;
3168
3169 modifiers = dos_get_modifiers (&mask);
3170
3171 #ifndef HAVE_X_WINDOWS
3172 if (!NILP (Vdos_display_scancodes))
3173 {
3174 char buf[11];
3175 sprintf (buf, "%02x:%02x*%04x",
3176 (unsigned) (sc&0xff), (unsigned) c, mask);
3177 dos_direct_output (screen_size_Y - 2, screen_size_X - 12, buf, 10);
3178 }
3179 #endif
3180
3181 if (sc == 0xe0)
3182 {
3183 switch (c)
3184 {
3185 case 10: /* Ctrl Grey Enter */
3186 code = Ctrl | Grey | 4;
3187 break;
3188 case 13: /* Grey Enter */
3189 code = Grey | 4;
3190 break;
3191 case '/': /* Grey / */
3192 code = Grey | 0;
3193 break;
3194 default:
3195 continue;
3196 };
3197 c = 0;
3198 }
3199 else
3200 {
3201 /* Try the keyboard-private translation table first. */
3202 if (keyboard->translate_table)
3203 {
3204 struct kbd_translate *p = keyboard->translate_table;
3205
3206 while (p->sc)
3207 {
3208 if (p->sc == sc && p->ch == c)
3209 {
3210 code = p->code;
3211 break;
3212 }
3213 p++;
3214 }
3215 }
3216 /* If the private table didn't translate it, use the general
3217 one. */
3218 if (code == -1)
3219 {
3220 if (sc >= (sizeof (ibmpc_translate_map) / sizeof (short)))
3221 continue;
3222 if ((code = ibmpc_translate_map[sc]) == Ignore)
3223 continue;
3224 }
3225 }
3226
3227 if (c == 0)
3228 {
3229 /* We only look at the keyboard Ctrl/Shift/Alt keys when
3230 Emacs is ready to read a key. Therefore, if they press
3231 `Alt-x' when Emacs is busy, by the time we get to
3232 `dos_get_modifiers', they might have already released the
3233 Alt key, and Emacs gets just `x', which is BAD.
3234 However, for keys with the `Map' property set, the ASCII
3235 code returns zero only if Alt is pressed. So, when we DON'T
3236 have to support international_keyboard, we don't have to
3237 distinguish between the left and right Alt keys, and we
3238 can set the META modifier for any keys with the `Map'
3239 property if they return zero ASCII code (c = 0). */
3240 if ( (code & Alt)
3241 || ( (code & 0xf000) == Map && !international_keyboard))
3242 modifiers |= meta_modifier;
3243 if (code & Ctrl)
3244 modifiers |= ctrl_modifier;
3245 if (code & Shift)
3246 modifiers |= shift_modifier;
3247 }
3248
3249 switch (code & 0xf000)
3250 {
3251 case ModFct:
3252 if (c && !(mask & (SHIFT_P | ALT_P | CTRL_P | HYPER_P | SUPER_P)))
3253 return c;
3254 c = 0; /* Special */
3255
3256 case FctKey:
3257 if (c != 0)
3258 return c;
3259
3260 case Special:
3261 code |= 0xff00;
3262 break;
3263
3264 case Normal:
3265 if (sc == 0)
3266 {
3267 if (c == 0) /* ctrl-break */
3268 continue;
3269 return c; /* ALT-nnn */
3270 }
3271 if (!keyboard_map_all)
3272 {
3273 if (c != ' ')
3274 return c;
3275 code = c;
3276 break;
3277 }
3278
3279 case Map:
3280 if (c && !(mask & ALT_P) && !((mask & SHIFT_P) && (mask & CTRL_P)))
3281 if (!keyboard_map_all)
3282 return c;
3283
3284 code &= 0xff;
3285 if (mask & ALT_P && code <= 10 && code > 0 && dos_keypad_mode & 0x200)
3286 mask |= SHIFT_P; /* ALT-1 => M-! etc. */
3287
3288 if (mask & SHIFT_P)
3289 {
3290 code = keyboard->shifted[code];
3291 mask -= SHIFT_P;
3292 modifiers &= ~shift_modifier;
3293 }
3294 else
3295 if ((mask & ALT_GR_P) && keyboard->alt_gr && keyboard->alt_gr[code] != ' ')
3296 code = keyboard->alt_gr[code];
3297 else
3298 code = keyboard->unshifted[code];
3299 break;
3300
3301 case KeyPad:
3302 code &= 0xff;
3303 if (c == 0xe0) /* edit key */
3304 kp_mode = 3;
3305 else
3306 if ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) /* numlock on */
3307 kp_mode = dos_keypad_mode & 0x03;
3308 else
3309 kp_mode = (dos_keypad_mode >> 4) & 0x03;
3310
3311 switch (kp_mode)
3312 {
3313 case 0:
3314 if (code == 10 && dos_decimal_point)
3315 return dos_decimal_point;
3316 return keypad_translate_map[code].char_code;
3317
3318 case 1:
3319 code = 0xff00 | keypad_translate_map[code].keypad_code;
3320 break;
3321
3322 case 2:
3323 code = keypad_translate_map[code].meta_code;
3324 modifiers = meta_modifier;
3325 break;
3326
3327 case 3:
3328 code = 0xff00 | keypad_translate_map[code].editkey_code;
3329 break;
3330 }
3331 break;
3332
3333 case Grey:
3334 code &= 0xff;
3335 kp_mode = ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) ? 0x04 : 0x40;
3336 if (dos_keypad_mode & kp_mode)
3337 code = 0xff00 | grey_key_translate_map[code].keypad_code;
3338 else
3339 code = grey_key_translate_map[code].char_code;
3340 break;
3341 }
3342
3343 make_event:
3344 if (code == 0)
3345 continue;
3346
3347 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
3348 {
3349 clear_mouse_face (dpyinfo);
3350 dpyinfo->mouse_face_hidden = 1;
3351 }
3352
3353 if (code >= 0x100)
3354 event.kind = NON_ASCII_KEYSTROKE_EVENT;
3355 else
3356 event.kind = ASCII_KEYSTROKE_EVENT;
3357 event.code = code;
3358 event.modifiers = modifiers;
3359 event.frame_or_window = selected_frame;
3360 event.arg = Qnil;
3361 event.timestamp = event_timestamp ();
3362 kbd_buffer_store_event (&event);
3363 }
3364
3365 if (have_mouse > 0 && !mouse_preempted)
3366 {
3367 int but, press, x, y, ok;
3368 int mouse_prev_x = mouse_last_x, mouse_prev_y = mouse_last_y;
3369 Lisp_Object mouse_window = Qnil;
3370
3371 /* Check for mouse movement *before* buttons. */
3372 mouse_check_moved ();
3373
3374 /* If the mouse moved from the spot of its last sighting, we
3375 might need to update mouse highlight. */
3376 if (mouse_last_x != mouse_prev_x || mouse_last_y != mouse_prev_y)
3377 {
3378 if (dpyinfo->mouse_face_hidden)
3379 {
3380 dpyinfo->mouse_face_hidden = 0;
3381 clear_mouse_face (dpyinfo);
3382 }
3383
3384 /* Generate SELECT_WINDOW_EVENTs when needed. */
3385 if (!NILP (Vmouse_autoselect_window))
3386 {
3387 mouse_window = window_from_coordinates (SELECTED_FRAME(),
3388 mouse_last_x,
3389 mouse_last_y,
3390 0, 0, 0, 0);
3391 /* A window will be selected only when it is not
3392 selected now, and the last mouse movement event was
3393 not in it. A minibuffer window will be selected iff
3394 it is active. */
3395 if (WINDOWP (mouse_window)
3396 && !EQ (mouse_window, last_mouse_window)
3397 && !EQ (mouse_window, selected_window))
3398 {
3399 event.kind = SELECT_WINDOW_EVENT;
3400 event.frame_or_window = mouse_window;
3401 event.arg = Qnil;
3402 event.timestamp = event_timestamp ();
3403 kbd_buffer_store_event (&event);
3404 }
3405 last_mouse_window = mouse_window;
3406 }
3407 else
3408 last_mouse_window = Qnil;
3409
3410 previous_help_echo_string = help_echo_string;
3411 help_echo_string = help_echo_object = help_echo_window = Qnil;
3412 help_echo_pos = -1;
3413 IT_note_mouse_highlight (SELECTED_FRAME(),
3414 mouse_last_x, mouse_last_y);
3415 /* If the contents of the global variable help_echo has
3416 changed, generate a HELP_EVENT. */
3417 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
3418 {
3419 event.kind = HELP_EVENT;
3420 event.frame_or_window = selected_frame;
3421 event.arg = help_echo_object;
3422 event.x = WINDOWP (help_echo_window)
3423 ? help_echo_window : selected_frame;
3424 event.y = help_echo_string;
3425 event.timestamp = event_timestamp ();
3426 event.code = help_echo_pos;
3427 kbd_buffer_store_event (&event);
3428 }
3429 }
3430
3431 for (but = 0; but < NUM_MOUSE_BUTTONS; but++)
3432 for (press = 0; press < 2; press++)
3433 {
3434 int button_num = but;
3435
3436 if (press)
3437 ok = mouse_pressed (but, &x, &y);
3438 else
3439 ok = mouse_released (but, &x, &y);
3440 if (ok)
3441 {
3442 /* Allow a simultaneous press/release of Mouse-1 and
3443 Mouse-2 to simulate Mouse-3 on two-button mice. */
3444 if (mouse_button_count == 2 && but < 2)
3445 {
3446 int x2, y2; /* don't clobber original coordinates */
3447
3448 /* If only one button is pressed, wait 100 msec and
3449 check again. This way, Speedy Gonzales isn't
3450 punished, while the slow get their chance. */
3451 if (press && mouse_pressed (1-but, &x2, &y2)
3452 || !press && mouse_released (1-but, &x2, &y2))
3453 button_num = 2;
3454 else
3455 {
3456 delay (100);
3457 if (press && mouse_pressed (1-but, &x2, &y2)
3458 || !press && mouse_released (1-but, &x2, &y2))
3459 button_num = 2;
3460 }
3461 }
3462
3463 event.kind = MOUSE_CLICK_EVENT;
3464 event.code = button_num;
3465 event.modifiers = dos_get_modifiers (0)
3466 | (press ? down_modifier : up_modifier);
3467 event.x = make_number (x);
3468 event.y = make_number (y);
3469 event.frame_or_window = selected_frame;
3470 event.arg = Qnil;
3471 event.timestamp = event_timestamp ();
3472 kbd_buffer_store_event (&event);
3473 }
3474 }
3475 }
3476
3477 return -1;
3478 }
3479
3480 static int prev_get_char = -1;
3481
3482 /* Return 1 if a key is ready to be read without suspending execution. */
3483
3484 dos_keysns ()
3485 {
3486 if (prev_get_char != -1)
3487 return 1;
3488 else
3489 return ((prev_get_char = dos_rawgetc ()) != -1);
3490 }
3491
3492 /* Read a key. Return -1 if no key is ready. */
3493
3494 dos_keyread ()
3495 {
3496 if (prev_get_char != -1)
3497 {
3498 int c = prev_get_char;
3499 prev_get_char = -1;
3500 return c;
3501 }
3502 else
3503 return dos_rawgetc ();
3504 }
3505 \f
3506 #ifndef HAVE_X_WINDOWS
3507
3508 /* Simulation of X's menus. Nothing too fancy here -- just make it work
3509 for now.
3510
3511 Actually, I don't know the meaning of all the parameters of the functions
3512 here -- I only know how they are called by xmenu.c. I could of course
3513 grab the nearest Xlib manual (down the hall, second-to-last door on the
3514 left), but I don't think it's worth the effort. */
3515
3516 /* These hold text of the current and the previous menu help messages. */
3517 static char *menu_help_message, *prev_menu_help_message;
3518 /* Pane number and item number of the menu item which generated the
3519 last menu help message. */
3520 static int menu_help_paneno, menu_help_itemno;
3521
3522 static XMenu *
3523 IT_menu_create ()
3524 {
3525 XMenu *menu;
3526
3527 menu = (XMenu *) xmalloc (sizeof (XMenu));
3528 menu->allocated = menu->count = menu->panecount = menu->width = 0;
3529 return menu;
3530 }
3531
3532 /* Allocate some (more) memory for MENU ensuring that there is room for one
3533 for item. */
3534
3535 static void
3536 IT_menu_make_room (XMenu *menu)
3537 {
3538 if (menu->allocated == 0)
3539 {
3540 int count = menu->allocated = 10;
3541 menu->text = (char **) xmalloc (count * sizeof (char *));
3542 menu->submenu = (XMenu **) xmalloc (count * sizeof (XMenu *));
3543 menu->panenumber = (int *) xmalloc (count * sizeof (int));
3544 menu->help_text = (char **) xmalloc (count * sizeof (char *));
3545 }
3546 else if (menu->allocated == menu->count)
3547 {
3548 int count = menu->allocated = menu->allocated + 10;
3549 menu->text
3550 = (char **) xrealloc (menu->text, count * sizeof (char *));
3551 menu->submenu
3552 = (XMenu **) xrealloc (menu->submenu, count * sizeof (XMenu *));
3553 menu->panenumber
3554 = (int *) xrealloc (menu->panenumber, count * sizeof (int));
3555 menu->help_text
3556 = (char **) xrealloc (menu->help_text, count * sizeof (char *));
3557 }
3558 }
3559
3560 /* Search the given menu structure for a given pane number. */
3561
3562 static XMenu *
3563 IT_menu_search_pane (XMenu *menu, int pane)
3564 {
3565 int i;
3566 XMenu *try;
3567
3568 for (i = 0; i < menu->count; i++)
3569 if (menu->submenu[i])
3570 {
3571 if (pane == menu->panenumber[i])
3572 return menu->submenu[i];
3573 if ((try = IT_menu_search_pane (menu->submenu[i], pane)))
3574 return try;
3575 }
3576 return (XMenu *) 0;
3577 }
3578
3579 /* Determine how much screen space a given menu needs. */
3580
3581 static void
3582 IT_menu_calc_size (XMenu *menu, int *width, int *height)
3583 {
3584 int i, h2, w2, maxsubwidth, maxheight;
3585
3586 maxsubwidth = 0;
3587 maxheight = menu->count;
3588 for (i = 0; i < menu->count; i++)
3589 {
3590 if (menu->submenu[i])
3591 {
3592 IT_menu_calc_size (menu->submenu[i], &w2, &h2);
3593 if (w2 > maxsubwidth) maxsubwidth = w2;
3594 if (i + h2 > maxheight) maxheight = i + h2;
3595 }
3596 }
3597 *width = menu->width + maxsubwidth;
3598 *height = maxheight;
3599 }
3600
3601 /* Display MENU at (X,Y) using FACES. */
3602
3603 static void
3604 IT_menu_display (XMenu *menu, int y, int x, int pn, int *faces, int disp_help)
3605 {
3606 int i, j, face, width, mx, my, enabled, mousehere, row, col;
3607 struct glyph *text, *p;
3608 char *q;
3609 struct frame *sf = SELECTED_FRAME();
3610
3611 menu_help_message = NULL;
3612
3613 width = menu->width;
3614 text = (struct glyph *) xmalloc ((width + 2) * sizeof (struct glyph));
3615 ScreenGetCursor (&row, &col);
3616 mouse_get_xy (&mx, &my);
3617 IT_update_begin (sf);
3618 for (i = 0; i < menu->count; i++)
3619 {
3620 int max_width = width + 2;
3621
3622 IT_cursor_to (y + i, x);
3623 enabled
3624 = (!menu->submenu[i] && menu->panenumber[i]) || (menu->submenu[i]);
3625 mousehere = (y + i == my && x <= mx && mx < x + width + 2);
3626 face = faces[enabled + mousehere * 2];
3627 /* The following if clause means that we display the menu help
3628 strings even if the menu item is currently disabled. */
3629 if (disp_help && enabled + mousehere * 2 >= 2)
3630 {
3631 menu_help_message = menu->help_text[i];
3632 menu_help_paneno = pn - 1;
3633 menu_help_itemno = i;
3634 }
3635 p = text;
3636 SET_CHAR_GLYPH (*p, ' ', face, 0);
3637 p++;
3638 for (j = 0, q = menu->text[i]; *q; j++)
3639 {
3640 if (*q > 26)
3641 {
3642 SET_CHAR_GLYPH (*p, *q++, face, 0);
3643 p++;
3644 }
3645 else /* make '^x' */
3646 {
3647 SET_CHAR_GLYPH (*p, '^', face, 0);
3648 p++;
3649 j++;
3650 SET_CHAR_GLYPH (*p, *q++ + 64, face, 0);
3651 p++;
3652 }
3653 }
3654 /* Don't let the menu text overflow into the next screen row. */
3655 if (x + max_width > screen_size_X)
3656 {
3657 max_width = screen_size_X - x;
3658 text[max_width - 1].u.ch = '$'; /* indicate it's truncated */
3659 }
3660 for (; j < max_width - 2; j++, p++)
3661 SET_CHAR_GLYPH (*p, ' ', face, 0);
3662
3663 SET_CHAR_GLYPH (*p, menu->submenu[i] ? 16 : ' ', face, 0);
3664 p++;
3665 IT_write_glyphs (text, max_width);
3666 }
3667 IT_update_end (sf);
3668 IT_cursor_to (row, col);
3669 xfree (text);
3670 }
3671 \f
3672 /* --------------------------- X Menu emulation ---------------------- */
3673
3674 /* Report availability of menus. */
3675
3676 int
3677 have_menus_p () { return 1; }
3678
3679 /* Create a brand new menu structure. */
3680
3681 XMenu *
3682 XMenuCreate (Display *foo1, Window foo2, char *foo3)
3683 {
3684 return IT_menu_create ();
3685 }
3686
3687 /* Create a new pane and place it on the outer-most level. It is not
3688 clear that it should be placed out there, but I don't know what else
3689 to do. */
3690
3691 int
3692 XMenuAddPane (Display *foo, XMenu *menu, char *txt, int enable)
3693 {
3694 int len;
3695 char *p;
3696
3697 if (!enable)
3698 abort ();
3699
3700 IT_menu_make_room (menu);
3701 menu->submenu[menu->count] = IT_menu_create ();
3702 menu->text[menu->count] = txt;
3703 menu->panenumber[menu->count] = ++menu->panecount;
3704 menu->help_text[menu->count] = NULL;
3705 menu->count++;
3706
3707 /* Adjust length for possible control characters (which will
3708 be written as ^x). */
3709 for (len = strlen (txt), p = txt; *p; p++)
3710 if (*p < 27)
3711 len++;
3712
3713 if (len > menu->width)
3714 menu->width = len;
3715
3716 return menu->panecount;
3717 }
3718
3719 /* Create a new item in a menu pane. */
3720
3721 int
3722 XMenuAddSelection (Display *bar, XMenu *menu, int pane,
3723 int foo, char *txt, int enable, char *help_text)
3724 {
3725 int len;
3726 char *p;
3727
3728 if (pane)
3729 if (!(menu = IT_menu_search_pane (menu, pane)))
3730 return XM_FAILURE;
3731 IT_menu_make_room (menu);
3732 menu->submenu[menu->count] = (XMenu *) 0;
3733 menu->text[menu->count] = txt;
3734 menu->panenumber[menu->count] = enable;
3735 menu->help_text[menu->count] = help_text;
3736 menu->count++;
3737
3738 /* Adjust length for possible control characters (which will
3739 be written as ^x). */
3740 for (len = strlen (txt), p = txt; *p; p++)
3741 if (*p < 27)
3742 len++;
3743
3744 if (len > menu->width)
3745 menu->width = len;
3746
3747 return XM_SUCCESS;
3748 }
3749
3750 /* Decide where the menu would be placed if requested at (X,Y). */
3751
3752 void
3753 XMenuLocate (Display *foo0, XMenu *menu, int foo1, int foo2, int x, int y,
3754 int *ulx, int *uly, int *width, int *height)
3755 {
3756 IT_menu_calc_size (menu, width, height);
3757 *ulx = x + 1;
3758 *uly = y;
3759 *width += 2;
3760 }
3761
3762 struct IT_menu_state
3763 {
3764 void *screen_behind;
3765 XMenu *menu;
3766 int pane;
3767 int x, y;
3768 };
3769
3770
3771 /* Display menu, wait for user's response, and return that response. */
3772
3773 int
3774 XMenuActivate (Display *foo, XMenu *menu, int *pane, int *selidx,
3775 int x0, int y0, unsigned ButtonMask, char **txt,
3776 void (*help_callback)(char *, int, int))
3777 {
3778 struct IT_menu_state *state;
3779 int statecount, x, y, i, b, screensize, leave, result, onepane;
3780 int title_faces[4]; /* face to display the menu title */
3781 int faces[4], buffers_num_deleted = 0;
3782 struct frame *sf = SELECTED_FRAME();
3783 Lisp_Object saved_echo_area_message, selectface;
3784
3785 /* Just in case we got here without a mouse present... */
3786 if (have_mouse <= 0)
3787 return XM_IA_SELECT;
3788 /* Don't allow non-positive x0 and y0, lest the menu will wrap
3789 around the display. */
3790 if (x0 <= 0)
3791 x0 = 1;
3792 if (y0 <= 0)
3793 y0 = 1;
3794
3795 /* We will process all the mouse events directly, so we had
3796 better prevent dos_rawgetc from stealing them from us. */
3797 mouse_preempted++;
3798
3799 state = alloca (menu->panecount * sizeof (struct IT_menu_state));
3800 screensize = screen_size * 2;
3801 faces[0]
3802 = lookup_derived_face (sf, intern ("msdos-menu-passive-face"),
3803 DEFAULT_FACE_ID, 1);
3804 faces[1]
3805 = lookup_derived_face (sf, intern ("msdos-menu-active-face"),
3806 DEFAULT_FACE_ID, 1);
3807 selectface = intern ("msdos-menu-select-face");
3808 faces[2] = lookup_derived_face (sf, selectface,
3809 faces[0], 1);
3810 faces[3] = lookup_derived_face (sf, selectface,
3811 faces[1], 1);
3812
3813 /* Make sure the menu title is always displayed with
3814 `msdos-menu-active-face', no matter where the mouse pointer is. */
3815 for (i = 0; i < 4; i++)
3816 title_faces[i] = faces[3];
3817
3818 statecount = 1;
3819
3820 /* Don't let the title for the "Buffers" popup menu include a
3821 digit (which is ugly).
3822
3823 This is a terrible kludge, but I think the "Buffers" case is
3824 the only one where the title includes a number, so it doesn't
3825 seem to be necessary to make this more general. */
3826 if (strncmp (menu->text[0], "Buffers 1", 9) == 0)
3827 {
3828 menu->text[0][7] = '\0';
3829 buffers_num_deleted = 1;
3830 }
3831
3832 /* We need to save the current echo area message, so that we could
3833 restore it below, before we exit. See the commentary below,
3834 before the call to message_with_string. */
3835 saved_echo_area_message = Fcurrent_message ();
3836 state[0].menu = menu;
3837 mouse_off ();
3838 ScreenRetrieve (state[0].screen_behind = xmalloc (screensize));
3839
3840 /* Turn off the cursor. Otherwise it shows through the menu
3841 panes, which is ugly. */
3842 IT_display_cursor (0);
3843
3844 /* Display the menu title. */
3845 IT_menu_display (menu, y0 - 1, x0 - 1, 1, title_faces, 0);
3846 if (buffers_num_deleted)
3847 menu->text[0][7] = ' ';
3848 if ((onepane = menu->count == 1 && menu->submenu[0]))
3849 {
3850 menu->width = menu->submenu[0]->width;
3851 state[0].menu = menu->submenu[0];
3852 }
3853 else
3854 {
3855 state[0].menu = menu;
3856 }
3857 state[0].x = x0 - 1;
3858 state[0].y = y0;
3859 state[0].pane = onepane;
3860
3861 mouse_last_x = -1; /* A hack that forces display. */
3862 leave = 0;
3863 while (!leave)
3864 {
3865 if (!mouse_visible) mouse_on ();
3866 mouse_check_moved ();
3867 if (sf->mouse_moved)
3868 {
3869 sf->mouse_moved = 0;
3870 result = XM_IA_SELECT;
3871 mouse_get_xy (&x, &y);
3872 for (i = 0; i < statecount; i++)
3873 if (state[i].x <= x && x < state[i].x + state[i].menu->width + 2)
3874 {
3875 int dy = y - state[i].y;
3876 if (0 <= dy && dy < state[i].menu->count)
3877 {
3878 if (!state[i].menu->submenu[dy])
3879 if (state[i].menu->panenumber[dy])
3880 result = XM_SUCCESS;
3881 else
3882 result = XM_IA_SELECT;
3883 *pane = state[i].pane - 1;
3884 *selidx = dy;
3885 /* We hit some part of a menu, so drop extra menus that
3886 have been opened. That does not include an open and
3887 active submenu. */
3888 if (i != statecount - 2
3889 || state[i].menu->submenu[dy] != state[i+1].menu)
3890 while (i != statecount - 1)
3891 {
3892 statecount--;
3893 mouse_off ();
3894 ScreenUpdate (state[statecount].screen_behind);
3895 if (screen_virtual_segment)
3896 dosv_refresh_virtual_screen (0, screen_size);
3897 xfree (state[statecount].screen_behind);
3898 }
3899 if (i == statecount - 1 && state[i].menu->submenu[dy])
3900 {
3901 IT_menu_display (state[i].menu,
3902 state[i].y,
3903 state[i].x,
3904 state[i].pane,
3905 faces, 1);
3906 state[statecount].menu = state[i].menu->submenu[dy];
3907 state[statecount].pane = state[i].menu->panenumber[dy];
3908 mouse_off ();
3909 ScreenRetrieve (state[statecount].screen_behind
3910 = xmalloc (screensize));
3911 state[statecount].x
3912 = state[i].x + state[i].menu->width + 2;
3913 state[statecount].y = y;
3914 statecount++;
3915 }
3916 }
3917 }
3918 IT_menu_display (state[statecount - 1].menu,
3919 state[statecount - 1].y,
3920 state[statecount - 1].x,
3921 state[statecount - 1].pane,
3922 faces, 1);
3923 }
3924 else
3925 {
3926 if ((menu_help_message || prev_menu_help_message)
3927 && menu_help_message != prev_menu_help_message)
3928 {
3929 help_callback (menu_help_message,
3930 menu_help_paneno, menu_help_itemno);
3931 IT_display_cursor (0);
3932 prev_menu_help_message = menu_help_message;
3933 }
3934 /* We are busy-waiting for the mouse to move, so let's be nice
3935 to other Windows applications by releasing our time slice. */
3936 __dpmi_yield ();
3937 }
3938 for (b = 0; b < mouse_button_count && !leave; b++)
3939 {
3940 /* Only leave if user both pressed and released the mouse, and in
3941 that order. This avoids popping down the menu pane unless
3942 the user is really done with it. */
3943 if (mouse_pressed (b, &x, &y))
3944 {
3945 while (mouse_button_depressed (b, &x, &y))
3946 __dpmi_yield ();
3947 leave = 1;
3948 }
3949 (void) mouse_released (b, &x, &y);
3950 }
3951 }
3952
3953 mouse_off ();
3954 ScreenUpdate (state[0].screen_behind);
3955 if (screen_virtual_segment)
3956 dosv_refresh_virtual_screen (0, screen_size);
3957
3958 /* We have a situation here. ScreenUpdate has just restored the
3959 screen contents as it was before we started drawing this menu.
3960 That includes any echo area message that could have been
3961 displayed back then. (In reality, that echo area message will
3962 almost always be the ``keystroke echo'' that echoes the sequence
3963 of menu items chosen by the user.) However, if the menu had some
3964 help messages, then displaying those messages caused Emacs to
3965 forget about the original echo area message. So when
3966 ScreenUpdate restored it, it created a discrepancy between the
3967 actual screen contents and what Emacs internal data structures
3968 know about it.
3969
3970 To avoid this conflict, we force Emacs to restore the original
3971 echo area message as we found it when we entered this function.
3972 The irony of this is that we then erase the restored message
3973 right away, so the only purpose of restoring it is so that
3974 erasing it works correctly... */
3975 if (! NILP (saved_echo_area_message))
3976 message_with_string ("%s", saved_echo_area_message, 0);
3977 message (0);
3978 while (statecount--)
3979 xfree (state[statecount].screen_behind);
3980 IT_display_cursor (1); /* turn cursor back on */
3981 /* Clean up any mouse events that are waiting inside Emacs event queue.
3982 These events are likely to be generated before the menu was even
3983 displayed, probably because the user pressed and released the button
3984 (which invoked the menu) too quickly. If we don't remove these events,
3985 Emacs will process them after we return and surprise the user. */
3986 discard_mouse_events ();
3987 mouse_clear_clicks ();
3988 if (!kbd_buffer_events_waiting (1))
3989 clear_input_pending ();
3990 /* Allow mouse events generation by dos_rawgetc. */
3991 mouse_preempted--;
3992 return result;
3993 }
3994
3995 /* Dispose of a menu. */
3996
3997 void
3998 XMenuDestroy (Display *foo, XMenu *menu)
3999 {
4000 int i;
4001 if (menu->allocated)
4002 {
4003 for (i = 0; i < menu->count; i++)
4004 if (menu->submenu[i])
4005 XMenuDestroy (foo, menu->submenu[i]);
4006 xfree (menu->text);
4007 xfree (menu->submenu);
4008 xfree (menu->panenumber);
4009 xfree (menu->help_text);
4010 }
4011 xfree (menu);
4012 menu_help_message = prev_menu_help_message = NULL;
4013 }
4014
4015 int
4016 x_pixel_width (struct frame *f)
4017 {
4018 return FRAME_COLS (f);
4019 }
4020
4021 int
4022 x_pixel_height (struct frame *f)
4023 {
4024 return FRAME_LINES (f);
4025 }
4026 #endif /* !HAVE_X_WINDOWS */
4027 \f
4028 /* ----------------------- DOS / UNIX conversion --------------------- */
4029
4030 void msdos_downcase_filename (unsigned char *);
4031
4032 /* Destructively turn backslashes into slashes. */
4033
4034 void
4035 dostounix_filename (p)
4036 register char *p;
4037 {
4038 msdos_downcase_filename (p);
4039
4040 while (*p)
4041 {
4042 if (*p == '\\')
4043 *p = '/';
4044 p++;
4045 }
4046 }
4047
4048 /* Destructively turn slashes into backslashes. */
4049
4050 void
4051 unixtodos_filename (p)
4052 register char *p;
4053 {
4054 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
4055 {
4056 *p += 'a' - 'A';
4057 p += 2;
4058 }
4059
4060 while (*p)
4061 {
4062 if (*p == '/')
4063 *p = '\\';
4064 p++;
4065 }
4066 }
4067
4068 /* Get the default directory for a given drive. 0=def, 1=A, 2=B, ... */
4069
4070 int
4071 getdefdir (drive, dst)
4072 int drive;
4073 char *dst;
4074 {
4075 char in_path[4], *p = in_path, e = errno;
4076
4077 /* Generate "X:." (when drive is X) or "." (when drive is 0). */
4078 if (drive != 0)
4079 {
4080 *p++ = drive + 'A' - 1;
4081 *p++ = ':';
4082 }
4083
4084 *p++ = '.';
4085 *p = '\0';
4086 errno = 0;
4087 _fixpath (in_path, dst);
4088 /* _fixpath can set errno to ENOSYS on non-LFN systems because
4089 it queries the LFN support, so ignore that error. */
4090 if ((errno && errno != ENOSYS) || *dst == '\0')
4091 return 0;
4092
4093 msdos_downcase_filename (dst);
4094
4095 errno = e;
4096 return 1;
4097 }
4098
4099 char *
4100 emacs_root_dir (void)
4101 {
4102 static char root_dir[4];
4103
4104 sprintf (root_dir, "%c:/", 'A' + getdisk ());
4105 root_dir[0] = tolower (root_dir[0]);
4106 return root_dir;
4107 }
4108
4109 /* Remove all CR's that are followed by a LF. */
4110
4111 int
4112 crlf_to_lf (n, buf)
4113 register int n;
4114 register unsigned char *buf;
4115 {
4116 unsigned char *np = buf, *startp = buf, *endp = buf + n;
4117
4118 if (n == 0)
4119 return n;
4120 while (buf < endp - 1)
4121 {
4122 if (*buf == 0x0d)
4123 {
4124 if (*(++buf) != 0x0a)
4125 *np++ = 0x0d;
4126 }
4127 else
4128 *np++ = *buf++;
4129 }
4130 if (buf < endp)
4131 *np++ = *buf++;
4132 return np - startp;
4133 }
4134
4135 #if defined(__DJGPP__) && __DJGPP__ == 2 && __DJGPP_MINOR__ == 0
4136
4137 /* In DJGPP v2.0, library `write' can call `malloc', which might
4138 cause relocation of the buffer whose address we get in ADDR.
4139 Here is a version of `write' that avoids calling `malloc',
4140 to serve us until such time as the library is fixed.
4141 Actually, what we define here is called `__write', because
4142 `write' is a stub that just jmp's to `__write' (to be
4143 POSIXLY-correct with respect to the global name-space). */
4144
4145 #include <io.h> /* for _write */
4146 #include <libc/dosio.h> /* for __file_handle_modes[] */
4147
4148 static char xbuf[64 * 1024]; /* DOS cannot write more in one chunk */
4149
4150 #define XBUF_END (xbuf + sizeof (xbuf) - 1)
4151
4152 int
4153 __write (int handle, const void *buffer, size_t count)
4154 {
4155 if (count == 0)
4156 return 0;
4157
4158 if(__file_handle_modes[handle] & O_BINARY)
4159 return _write (handle, buffer, count);
4160 else
4161 {
4162 char *xbp = xbuf;
4163 const char *bp = buffer;
4164 int total_written = 0;
4165 int nmoved = 0, ncr = 0;
4166
4167 while (count)
4168 {
4169 /* The next test makes sure there's space for at least 2 more
4170 characters in xbuf[], so both CR and LF can be put there. */
4171 if (xbp < XBUF_END)
4172 {
4173 if (*bp == '\n')
4174 {
4175 ncr++;
4176 *xbp++ = '\r';
4177 }
4178 *xbp++ = *bp++;
4179 nmoved++;
4180 count--;
4181 }
4182 if (xbp >= XBUF_END || !count)
4183 {
4184 size_t to_write = nmoved + ncr;
4185 int written = _write (handle, xbuf, to_write);
4186
4187 if (written == -1)
4188 return -1;
4189 else
4190 total_written += nmoved; /* CRs aren't counted in ret value */
4191
4192 /* If some, but not all were written (disk full?), return
4193 an estimate of the total written bytes not counting CRs. */
4194 if (written < to_write)
4195 return total_written - (to_write - written) * nmoved/to_write;
4196
4197 nmoved = 0;
4198 ncr = 0;
4199 xbp = xbuf;
4200 }
4201 }
4202 return total_written;
4203 }
4204 }
4205
4206 /* A low-level file-renaming function which works around Windows 95 bug.
4207 This is pulled directly out of DJGPP v2.01 library sources, and only
4208 used when you compile with DJGPP v2.0. */
4209
4210 #include <io.h>
4211
4212 int _rename(const char *old, const char *new)
4213 {
4214 __dpmi_regs r;
4215 int olen = strlen(old) + 1;
4216 int i;
4217 int use_lfn = _USE_LFN;
4218 char tempfile[FILENAME_MAX];
4219 const char *orig = old;
4220 int lfn_fd = -1;
4221
4222 r.x.dx = __tb_offset;
4223 r.x.di = __tb_offset + olen;
4224 r.x.ds = r.x.es = __tb_segment;
4225
4226 if (use_lfn)
4227 {
4228 /* Windows 95 bug: for some filenames, when you rename
4229 file -> file~ (as in Emacs, to leave a backup), the
4230 short 8+3 alias doesn't change, which effectively
4231 makes OLD and NEW the same file. We must rename
4232 through a temporary file to work around this. */
4233
4234 char *pbase = 0, *p;
4235 static char try_char[] = "abcdefghijklmnopqrstuvwxyz012345789";
4236 int idx = sizeof(try_char) - 1;
4237
4238 /* Generate a temporary name. Can't use `tmpnam', since $TMPDIR
4239 might point to another drive, which will fail the DOS call. */
4240 strcpy(tempfile, old);
4241 for (p = tempfile; *p; p++) /* ensure temporary is on the same drive */
4242 if (*p == '/' || *p == '\\' || *p == ':')
4243 pbase = p;
4244 if (pbase)
4245 pbase++;
4246 else
4247 pbase = tempfile;
4248 strcpy(pbase, "X$$djren$$.$$temp$$");
4249
4250 do
4251 {
4252 if (idx <= 0)
4253 return -1;
4254 *pbase = try_char[--idx];
4255 } while (_chmod(tempfile, 0) != -1);
4256
4257 r.x.ax = 0x7156;
4258 _put_path2(tempfile, olen);
4259 _put_path(old);
4260 __dpmi_int(0x21, &r);
4261 if (r.x.flags & 1)
4262 {
4263 errno = __doserr_to_errno(r.x.ax);
4264 return -1;
4265 }
4266
4267 /* Now create a file with the original name. This will
4268 ensure that NEW will always have a 8+3 alias
4269 different from that of OLD. (Seems to be required
4270 when NameNumericTail in the Registry is set to 0.) */
4271 lfn_fd = _creat(old, 0);
4272
4273 olen = strlen(tempfile) + 1;
4274 old = tempfile;
4275 r.x.di = __tb_offset + olen;
4276 }
4277
4278 for (i=0; i<2; i++)
4279 {
4280 if(use_lfn)
4281 r.x.ax = 0x7156;
4282 else
4283 r.h.ah = 0x56;
4284 _put_path2(new, olen);
4285 _put_path(old);
4286 __dpmi_int(0x21, &r);
4287 if(r.x.flags & 1)
4288 {
4289 if (r.x.ax == 5 && i == 0) /* access denied */
4290 remove(new); /* and try again */
4291 else
4292 {
4293 errno = __doserr_to_errno(r.x.ax);
4294
4295 /* Restore to original name if we renamed it to temporary. */
4296 if (use_lfn)
4297 {
4298 if (lfn_fd != -1)
4299 {
4300 _close (lfn_fd);
4301 remove (orig);
4302 }
4303 _put_path2(orig, olen);
4304 _put_path(tempfile);
4305 r.x.ax = 0x7156;
4306 __dpmi_int(0x21, &r);
4307 }
4308 return -1;
4309 }
4310 }
4311 else
4312 break;
4313 }
4314
4315 /* Success. Delete the file possibly created to work
4316 around the Windows 95 bug. */
4317 if (lfn_fd != -1)
4318 return (_close (lfn_fd) == 0) ? remove (orig) : -1;
4319 return 0;
4320 }
4321
4322 #endif /* __DJGPP__ == 2 && __DJGPP_MINOR__ == 0 */
4323
4324 DEFUN ("msdos-long-file-names", Fmsdos_long_file_names, Smsdos_long_file_names,
4325 0, 0, 0,
4326 doc: /* Return non-nil if long file names are supported on MSDOS. */)
4327 ()
4328 {
4329 return (_USE_LFN ? Qt : Qnil);
4330 }
4331
4332 /* Convert alphabetic characters in a filename to lower-case. */
4333
4334 void
4335 msdos_downcase_filename (p)
4336 register unsigned char *p;
4337 {
4338 /* Always lower-case drive letters a-z, even if the filesystem
4339 preserves case in filenames.
4340 This is so MSDOS filenames could be compared by string comparison
4341 functions that are case-sensitive. Even case-preserving filesystems
4342 do not distinguish case in drive letters. */
4343 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
4344 {
4345 *p += 'a' - 'A';
4346 p += 2;
4347 }
4348
4349 /* Under LFN we expect to get pathnames in their true case. */
4350 if (NILP (Fmsdos_long_file_names ()))
4351 for ( ; *p; p++)
4352 if (*p >= 'A' && *p <= 'Z')
4353 *p += 'a' - 'A';
4354 }
4355
4356 DEFUN ("msdos-downcase-filename", Fmsdos_downcase_filename, Smsdos_downcase_filename,
4357 1, 1, 0,
4358 doc: /* Convert alphabetic characters in FILENAME to lower case and return that.
4359 When long filenames are supported, doesn't change FILENAME.
4360 If FILENAME is not a string, returns nil.
4361 The argument object is never altered--the value is a copy. */)
4362 (filename)
4363 Lisp_Object filename;
4364 {
4365 Lisp_Object tem;
4366
4367 if (! STRINGP (filename))
4368 return Qnil;
4369
4370 tem = Fcopy_sequence (filename);
4371 msdos_downcase_filename (SDATA (tem));
4372 return tem;
4373 }
4374 \f
4375 /* The Emacs root directory as determined by init_environment. */
4376
4377 static char emacsroot[MAXPATHLEN];
4378
4379 char *
4380 rootrelativepath (rel)
4381 char *rel;
4382 {
4383 static char result[MAXPATHLEN + 10];
4384
4385 strcpy (result, emacsroot);
4386 strcat (result, "/");
4387 strcat (result, rel);
4388 return result;
4389 }
4390
4391 /* Define a lot of environment variables if not already defined. Don't
4392 remove anything unless you know what you're doing -- lots of code will
4393 break if one or more of these are missing. */
4394
4395 void
4396 init_environment (argc, argv, skip_args)
4397 int argc;
4398 char **argv;
4399 int skip_args;
4400 {
4401 char *s, *t, *root;
4402 int len, i;
4403 static const char * const tempdirs[] = {
4404 "$TMPDIR", "$TEMP", "$TMP", "c:/"
4405 };
4406 const int imax = sizeof (tempdirs) / sizeof (tempdirs[0]);
4407
4408 /* Make sure they have a usable $TMPDIR. Many Emacs functions use
4409 temporary files and assume "/tmp" if $TMPDIR is unset, which
4410 will break on DOS/Windows. Refuse to work if we cannot find
4411 a directory, not even "c:/", usable for that purpose. */
4412 for (i = 0; i < imax ; i++)
4413 {
4414 const char *tmp = tempdirs[i];
4415 char buf[FILENAME_MAX];
4416
4417 if (*tmp == '$')
4418 {
4419 int tmp_len;
4420
4421 tmp = getenv (tmp + 1);
4422 if (!tmp)
4423 continue;
4424
4425 /* Some lusers set TMPDIR=e:, probably because some losing
4426 programs cannot handle multiple slashes if they use e:/.
4427 e: fails in `access' below, so we interpret e: as e:/. */
4428 tmp_len = strlen(tmp);
4429 if (tmp[tmp_len - 1] != '/' && tmp[tmp_len - 1] != '\\')
4430 {
4431 strcpy(buf, tmp);
4432 buf[tmp_len++] = '/', buf[tmp_len] = 0;
4433 tmp = buf;
4434 }
4435 }
4436
4437 /* Note that `access' can lie to us if the directory resides on a
4438 read-only filesystem, like CD-ROM or a write-protected floppy.
4439 The only way to be really sure is to actually create a file and
4440 see if it succeeds. But I think that's too much to ask. */
4441 if (tmp && access (tmp, D_OK) == 0)
4442 {
4443 setenv ("TMPDIR", tmp, 1);
4444 break;
4445 }
4446 }
4447 if (i >= imax)
4448 cmd_error_internal
4449 (Fcons (Qerror,
4450 Fcons (build_string ("no usable temporary directories found!!"),
4451 Qnil)),
4452 "While setting TMPDIR: ");
4453
4454 /* Note the startup time, so we know not to clear the screen if we
4455 exit immediately; see IT_reset_terminal_modes.
4456 (Yes, I know `clock' returns zero the first time it's called, but
4457 I do this anyway, in case some wiseguy changes that at some point.) */
4458 startup_time = clock ();
4459
4460 /* Find our root from argv[0]. Assuming argv[0] is, say,
4461 "c:/emacs/bin/emacs.exe" our root will be "c:/emacs". */
4462 root = alloca (MAXPATHLEN + 20);
4463 _fixpath (argv[0], root);
4464 msdos_downcase_filename (root);
4465 len = strlen (root);
4466 while (len > 0 && root[len] != '/' && root[len] != ':')
4467 len--;
4468 root[len] = '\0';
4469 if (len > 4
4470 && (strcmp (root + len - 4, "/bin") == 0
4471 || strcmp (root + len - 4, "/src") == 0)) /* under a debugger */
4472 root[len - 4] = '\0';
4473 else
4474 strcpy (root, "c:/emacs"); /* let's be defensive */
4475 len = strlen (root);
4476 strcpy (emacsroot, root);
4477
4478 /* We default HOME to our root. */
4479 setenv ("HOME", root, 0);
4480
4481 /* We default EMACSPATH to root + "/bin". */
4482 strcpy (root + len, "/bin");
4483 setenv ("EMACSPATH", root, 0);
4484
4485 /* I don't expect anybody to ever use other terminals so the internal
4486 terminal is the default. */
4487 setenv ("TERM", "internal", 0);
4488
4489 #ifdef HAVE_X_WINDOWS
4490 /* Emacs expects DISPLAY to be set. */
4491 setenv ("DISPLAY", "unix:0.0", 0);
4492 #endif
4493
4494 /* SHELL is a bit tricky -- COMSPEC is the closest we come, but we must
4495 downcase it and mirror the backslashes. */
4496 s = getenv ("COMSPEC");
4497 if (!s) s = "c:/command.com";
4498 t = alloca (strlen (s) + 1);
4499 strcpy (t, s);
4500 dostounix_filename (t);
4501 setenv ("SHELL", t, 0);
4502
4503 /* PATH is also downcased and backslashes mirrored. */
4504 s = getenv ("PATH");
4505 if (!s) s = "";
4506 t = alloca (strlen (s) + 3);
4507 /* Current directory is always considered part of MsDos's path but it is
4508 not normally mentioned. Now it is. */
4509 strcat (strcpy (t, ".;"), s);
4510 dostounix_filename (t); /* Not a single file name, but this should work. */
4511 setenv ("PATH", t, 1);
4512
4513 /* In some sense all dos users have root privileges, so... */
4514 setenv ("USER", "root", 0);
4515 setenv ("NAME", getenv ("USER"), 0);
4516
4517 /* Time zone determined from country code. To make this possible, the
4518 country code may not span more than one time zone. In other words,
4519 in the USA, you lose. */
4520 if (!getenv ("TZ"))
4521 switch (dos_country_code)
4522 {
4523 case 31: /* Belgium */
4524 case 32: /* The Netherlands */
4525 case 33: /* France */
4526 case 34: /* Spain */
4527 case 36: /* Hungary */
4528 case 38: /* Yugoslavia (or what's left of it?) */
4529 case 39: /* Italy */
4530 case 41: /* Switzerland */
4531 case 42: /* Tjekia */
4532 case 45: /* Denmark */
4533 case 46: /* Sweden */
4534 case 47: /* Norway */
4535 case 48: /* Poland */
4536 case 49: /* Germany */
4537 /* Daylight saving from last Sunday in March to last Sunday in
4538 September, both at 2AM. */
4539 setenv ("TZ", "MET-01METDST-02,M3.5.0/02:00,M9.5.0/02:00", 0);
4540 break;
4541 case 44: /* United Kingdom */
4542 case 351: /* Portugal */
4543 case 354: /* Iceland */
4544 setenv ("TZ", "GMT+00", 0);
4545 break;
4546 case 81: /* Japan */
4547 case 82: /* Korea */
4548 setenv ("TZ", "JST-09", 0);
4549 break;
4550 case 90: /* Turkey */
4551 case 358: /* Finland */
4552 setenv ("TZ", "EET-02", 0);
4553 break;
4554 case 972: /* Israel */
4555 /* This is an approximation. (For exact rules, use the
4556 `zoneinfo/israel' file which comes with DJGPP, but you need
4557 to install it in `/usr/share/zoneinfo/' directory first.) */
4558 setenv ("TZ", "IST-02IDT-03,M4.1.6/00:00,M9.5.6/01:00", 0);
4559 break;
4560 }
4561 tzset ();
4562 }
4563
4564 \f
4565
4566 static int break_stat; /* BREAK check mode status. */
4567 static int stdin_stat; /* stdin IOCTL status. */
4568
4569 #if __DJGPP__ < 2
4570
4571 /* These must be global. */
4572 static _go32_dpmi_seginfo ctrl_break_vector;
4573 static _go32_dpmi_registers ctrl_break_regs;
4574 static int ctrlbreakinstalled = 0;
4575
4576 /* Interrupt level detection of Ctrl-Break. Don't do anything fancy here! */
4577
4578 void
4579 ctrl_break_func (regs)
4580 _go32_dpmi_registers *regs;
4581 {
4582 Vquit_flag = Qt;
4583 }
4584
4585 void
4586 install_ctrl_break_check ()
4587 {
4588 if (!ctrlbreakinstalled)
4589 {
4590 /* Don't press Ctrl-Break if you don't have either DPMI or Emacs
4591 was compiler with Djgpp 1.11 maintenance level 5 or later! */
4592 ctrlbreakinstalled = 1;
4593 ctrl_break_vector.pm_offset = (int) ctrl_break_func;
4594 _go32_dpmi_allocate_real_mode_callback_iret (&ctrl_break_vector,
4595 &ctrl_break_regs);
4596 _go32_dpmi_set_real_mode_interrupt_vector (0x1b, &ctrl_break_vector);
4597 }
4598 }
4599
4600 #endif /* __DJGPP__ < 2 */
4601
4602 /* Turn off Dos' Ctrl-C checking and inhibit interpretation of
4603 control chars by DOS. Determine the keyboard type. */
4604
4605 int
4606 dos_ttraw ()
4607 {
4608 union REGS inregs, outregs;
4609 static int first_time = 1;
4610
4611 break_stat = getcbrk ();
4612 setcbrk (0);
4613 #if __DJGPP__ < 2
4614 install_ctrl_break_check ();
4615 #endif
4616
4617 if (first_time)
4618 {
4619 inregs.h.ah = 0xc0;
4620 int86 (0x15, &inregs, &outregs);
4621 extended_kbd = (!outregs.x.cflag) && (outregs.h.ah == 0);
4622
4623 have_mouse = 0;
4624
4625 if (internal_terminal
4626 #ifdef HAVE_X_WINDOWS
4627 && inhibit_window_system
4628 #endif
4629 )
4630 {
4631 inregs.x.ax = 0x0021;
4632 int86 (0x33, &inregs, &outregs);
4633 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4634 if (!have_mouse)
4635 {
4636 /* Reportedly, the above doesn't work for some mouse drivers. There
4637 is an additional detection method that should work, but might be
4638 a little slower. Use that as an alternative. */
4639 inregs.x.ax = 0x0000;
4640 int86 (0x33, &inregs, &outregs);
4641 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4642 }
4643
4644 if (have_mouse)
4645 {
4646 have_mouse = 1; /* enable mouse */
4647 mouse_visible = 0;
4648 mouse_setup_buttons (outregs.x.bx);
4649 mouse_position_hook = &mouse_get_pos;
4650 mouse_init ();
4651 }
4652
4653 #ifndef HAVE_X_WINDOWS
4654 #if __DJGPP__ >= 2
4655 /* Save the cursor shape used outside Emacs. */
4656 outside_cursor = _farpeekw (_dos_ds, 0x460);
4657 #endif
4658 #endif
4659 }
4660
4661 first_time = 0;
4662
4663 #if __DJGPP__ >= 2
4664
4665 stdin_stat = setmode (fileno (stdin), O_BINARY);
4666 return (stdin_stat != -1);
4667 }
4668 else
4669 return (setmode (fileno (stdin), O_BINARY) != -1);
4670
4671 #else /* __DJGPP__ < 2 */
4672
4673 }
4674
4675 /* I think it is wrong to overwrite `stdin_stat' every time
4676 but the first one this function is called, but I don't
4677 want to change the way it used to work in v1.x.--EZ */
4678
4679 inregs.x.ax = 0x4400; /* Get IOCTL status. */
4680 inregs.x.bx = 0x00; /* 0 = stdin. */
4681 intdos (&inregs, &outregs);
4682 stdin_stat = outregs.h.dl;
4683
4684 inregs.x.dx = stdin_stat | 0x0020; /* raw mode */
4685 inregs.x.ax = 0x4401; /* Set IOCTL status */
4686 intdos (&inregs, &outregs);
4687 return !outregs.x.cflag;
4688
4689 #endif /* __DJGPP__ < 2 */
4690 }
4691
4692 /* Restore status of standard input and Ctrl-C checking. */
4693
4694 int
4695 dos_ttcooked ()
4696 {
4697 union REGS inregs, outregs;
4698
4699 setcbrk (break_stat);
4700 mouse_off ();
4701
4702 #if __DJGPP__ >= 2
4703
4704 #ifndef HAVE_X_WINDOWS
4705 /* Restore the cursor shape we found on startup. */
4706 if (outside_cursor)
4707 {
4708 inregs.h.ah = 1;
4709 inregs.x.cx = outside_cursor;
4710 int86 (0x10, &inregs, &outregs);
4711 }
4712 #endif
4713
4714 return (setmode (fileno (stdin), stdin_stat) != -1);
4715
4716 #else /* not __DJGPP__ >= 2 */
4717
4718 inregs.x.ax = 0x4401; /* Set IOCTL status. */
4719 inregs.x.bx = 0x00; /* 0 = stdin. */
4720 inregs.x.dx = stdin_stat;
4721 intdos (&inregs, &outregs);
4722 return !outregs.x.cflag;
4723
4724 #endif /* not __DJGPP__ >= 2 */
4725 }
4726
4727 \f
4728 /* Run command as specified by ARGV in directory DIR.
4729 The command is run with input from TEMPIN, output to
4730 file TEMPOUT and stderr to TEMPERR. */
4731
4732 int
4733 run_msdos_command (argv, working_dir, tempin, tempout, temperr, envv)
4734 unsigned char **argv;
4735 const char *working_dir;
4736 int tempin, tempout, temperr;
4737 char **envv;
4738 {
4739 char *saveargv1, *saveargv2, *lowcase_argv0, *pa, *pl;
4740 char oldwd[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4741 int msshell, result = -1, inbak, outbak, errbak, x, y;
4742 Lisp_Object cmd;
4743
4744 /* Get current directory as MSDOS cwd is not per-process. */
4745 getwd (oldwd);
4746
4747 /* If argv[0] is the shell, it might come in any lettercase.
4748 Since `Fmember' is case-sensitive, we need to downcase
4749 argv[0], even if we are on case-preserving filesystems. */
4750 lowcase_argv0 = alloca (strlen (argv[0]) + 1);
4751 for (pa = argv[0], pl = lowcase_argv0; *pa; pl++)
4752 {
4753 *pl = *pa++;
4754 if (*pl >= 'A' && *pl <= 'Z')
4755 *pl += 'a' - 'A';
4756 }
4757 *pl = '\0';
4758
4759 cmd = Ffile_name_nondirectory (build_string (lowcase_argv0));
4760 msshell = !NILP (Fmember (cmd, Fsymbol_value (intern ("msdos-shells"))))
4761 && !strcmp ("-c", argv[1]);
4762 if (msshell)
4763 {
4764 saveargv1 = argv[1];
4765 saveargv2 = argv[2];
4766 argv[1] = "/c";
4767 /* We only need to mirror slashes if a DOS shell will be invoked
4768 not via `system' (which does the mirroring itself). Yes, that
4769 means DJGPP v1.x will lose here. */
4770 if (argv[2] && argv[3])
4771 {
4772 char *p = alloca (strlen (argv[2]) + 1);
4773
4774 strcpy (argv[2] = p, saveargv2);
4775 while (*p && isspace (*p))
4776 p++;
4777 while (*p)
4778 {
4779 if (*p == '/')
4780 *p++ = '\\';
4781 else
4782 p++;
4783 }
4784 }
4785 }
4786
4787 chdir (working_dir);
4788 inbak = dup (0);
4789 outbak = dup (1);
4790 errbak = dup (2);
4791 if (inbak < 0 || outbak < 0 || errbak < 0)
4792 goto done; /* Allocation might fail due to lack of descriptors. */
4793
4794 if (have_mouse > 0)
4795 mouse_get_xy (&x, &y);
4796
4797 dos_ttcooked (); /* do it here while 0 = stdin */
4798
4799 dup2 (tempin, 0);
4800 dup2 (tempout, 1);
4801 dup2 (temperr, 2);
4802
4803 #if __DJGPP__ > 1
4804
4805 if (msshell && !argv[3])
4806 {
4807 /* MS-DOS native shells are too restrictive. For starters, they
4808 cannot grok commands longer than 126 characters. In DJGPP v2
4809 and later, `system' is much smarter, so we'll call it instead. */
4810
4811 const char *cmnd;
4812
4813 /* A shell gets a single argument--its full command
4814 line--whose original was saved in `saveargv2'. */
4815
4816 /* Don't let them pass empty command lines to `system', since
4817 with some shells it will try to invoke an interactive shell,
4818 which will hang Emacs. */
4819 for (cmnd = saveargv2; *cmnd && isspace (*cmnd); cmnd++)
4820 ;
4821 if (*cmnd)
4822 {
4823 extern char **environ;
4824 char **save_env = environ;
4825 int save_system_flags = __system_flags;
4826
4827 /* Request the most powerful version of `system'. We need
4828 all the help we can get to avoid calling stock DOS shells. */
4829 __system_flags = (__system_redirect
4830 | __system_use_shell
4831 | __system_allow_multiple_cmds
4832 | __system_allow_long_cmds
4833 | __system_handle_null_commands
4834 | __system_emulate_chdir);
4835
4836 environ = envv;
4837 result = system (cmnd);
4838 __system_flags = save_system_flags;
4839 environ = save_env;
4840 }
4841 else
4842 result = 0; /* emulate Unixy shell behavior with empty cmd line */
4843 }
4844 else
4845
4846 #endif /* __DJGPP__ > 1 */
4847
4848 result = spawnve (P_WAIT, argv[0], argv, envv);
4849
4850 dup2 (inbak, 0);
4851 dup2 (outbak, 1);
4852 dup2 (errbak, 2);
4853 emacs_close (inbak);
4854 emacs_close (outbak);
4855 emacs_close (errbak);
4856
4857 dos_ttraw ();
4858 if (have_mouse > 0)
4859 {
4860 mouse_init ();
4861 mouse_moveto (x, y);
4862 }
4863
4864 /* Some programs might change the meaning of the highest bit of the
4865 text attribute byte, so we get blinking characters instead of the
4866 bright background colors. Restore that. */
4867 bright_bg ();
4868
4869 done:
4870 chdir (oldwd);
4871 if (msshell)
4872 {
4873 argv[1] = saveargv1;
4874 argv[2] = saveargv2;
4875 }
4876 return result;
4877 }
4878
4879 void
4880 croak (badfunc)
4881 char *badfunc;
4882 {
4883 fprintf (stderr, "%s not yet implemented\r\n", badfunc);
4884 reset_all_sys_modes ();
4885 exit (1);
4886 }
4887 \f
4888 #if __DJGPP__ < 2
4889
4890 /* ------------------------- Compatibility functions -------------------
4891 * gethostname
4892 * gettimeofday
4893 */
4894
4895 /* Hostnames for a pc are not really funny,
4896 but they are used in change log so we emulate the best we can. */
4897
4898 gethostname (p, size)
4899 char *p;
4900 int size;
4901 {
4902 char *q = egetenv ("HOSTNAME");
4903
4904 if (!q) q = "pc";
4905 strcpy (p, q);
4906 return 0;
4907 }
4908
4909 /* When time zones are set from Ms-Dos too many C-libraries are playing
4910 tricks with time values. We solve this by defining our own version
4911 of `gettimeofday' bypassing GO32. Our version needs to be initialized
4912 once and after each call to `tzset' with TZ changed. That is
4913 accomplished by aliasing tzset to init_gettimeofday. */
4914
4915 static struct tm time_rec;
4916
4917 int
4918 gettimeofday (struct timeval *tp, struct timezone *tzp)
4919 {
4920 if (tp)
4921 {
4922 struct time t;
4923 struct tm tm;
4924
4925 gettime (&t);
4926 if (t.ti_hour < time_rec.tm_hour) /* midnight wrap */
4927 {
4928 struct date d;
4929 getdate (&d);
4930 time_rec.tm_year = d.da_year - 1900;
4931 time_rec.tm_mon = d.da_mon - 1;
4932 time_rec.tm_mday = d.da_day;
4933 }
4934
4935 time_rec.tm_hour = t.ti_hour;
4936 time_rec.tm_min = t.ti_min;
4937 time_rec.tm_sec = t.ti_sec;
4938
4939 tm = time_rec;
4940 tm.tm_gmtoff = dos_timezone_offset;
4941
4942 tp->tv_sec = mktime (&tm); /* may modify tm */
4943 tp->tv_usec = t.ti_hund * (1000000 / 100);
4944 }
4945 /* Ignore tzp; it's obsolescent. */
4946 return 0;
4947 }
4948
4949 #endif /* __DJGPP__ < 2 */
4950
4951 /*
4952 * A list of unimplemented functions that we silently ignore.
4953 */
4954
4955 #if __DJGPP__ < 2
4956 unsigned alarm (s) unsigned s; {}
4957 fork () { return 0; }
4958 int kill (x, y) int x, y; { return -1; }
4959 nice (p) int p; {}
4960 void volatile pause () {}
4961 sigsetmask (x) int x; { return 0; }
4962 sigblock (mask) int mask; { return 0; }
4963 #endif
4964
4965 void request_sigio (void) {}
4966 setpgrp () {return 0; }
4967 setpriority (x,y,z) int x,y,z; { return 0; }
4968 void unrequest_sigio (void) {}
4969
4970 #if __DJGPP__ > 1
4971 #if __DJGPP_MINOR__ < 2
4972
4973 #ifdef POSIX_SIGNALS
4974
4975 /* Augment DJGPP library POSIX signal functions. This is needed
4976 as of DJGPP v2.01, but might be in the library in later releases. */
4977
4978 #include <libc/bss.h>
4979
4980 /* A counter to know when to re-initialize the static sets. */
4981 static int sigprocmask_count = -1;
4982
4983 /* Which signals are currently blocked (initially none). */
4984 static sigset_t current_mask;
4985
4986 /* Which signals are pending (initially none). */
4987 static sigset_t pending_signals;
4988
4989 /* Previous handlers to restore when the blocked signals are unblocked. */
4990 typedef void (*sighandler_t)(int);
4991 static sighandler_t prev_handlers[320];
4992
4993 /* A signal handler which just records that a signal occurred
4994 (it will be raised later, if and when the signal is unblocked). */
4995 static void
4996 sig_suspender (signo)
4997 int signo;
4998 {
4999 sigaddset (&pending_signals, signo);
5000 }
5001
5002 int
5003 sigprocmask (how, new_set, old_set)
5004 int how;
5005 const sigset_t *new_set;
5006 sigset_t *old_set;
5007 {
5008 int signo;
5009 sigset_t new_mask;
5010
5011 /* If called for the first time, initialize. */
5012 if (sigprocmask_count != __bss_count)
5013 {
5014 sigprocmask_count = __bss_count;
5015 sigemptyset (&pending_signals);
5016 sigemptyset (&current_mask);
5017 for (signo = 0; signo < 320; signo++)
5018 prev_handlers[signo] = SIG_ERR;
5019 }
5020
5021 if (old_set)
5022 *old_set = current_mask;
5023
5024 if (new_set == 0)
5025 return 0;
5026
5027 if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK)
5028 {
5029 errno = EINVAL;
5030 return -1;
5031 }
5032
5033 sigemptyset (&new_mask);
5034
5035 /* DJGPP supports upto 320 signals. */
5036 for (signo = 0; signo < 320; signo++)
5037 {
5038 if (sigismember (&current_mask, signo))
5039 sigaddset (&new_mask, signo);
5040 else if (sigismember (new_set, signo) && how != SIG_UNBLOCK)
5041 {
5042 sigaddset (&new_mask, signo);
5043
5044 /* SIGKILL is silently ignored, as on other platforms. */
5045 if (signo != SIGKILL && prev_handlers[signo] == SIG_ERR)
5046 prev_handlers[signo] = signal (signo, sig_suspender);
5047 }
5048 if (( how == SIG_UNBLOCK
5049 && sigismember (&new_mask, signo)
5050 && sigismember (new_set, signo))
5051 || (how == SIG_SETMASK
5052 && sigismember (&new_mask, signo)
5053 && !sigismember (new_set, signo)))
5054 {
5055 sigdelset (&new_mask, signo);
5056 if (prev_handlers[signo] != SIG_ERR)
5057 {
5058 signal (signo, prev_handlers[signo]);
5059 prev_handlers[signo] = SIG_ERR;
5060 }
5061 if (sigismember (&pending_signals, signo))
5062 {
5063 sigdelset (&pending_signals, signo);
5064 raise (signo);
5065 }
5066 }
5067 }
5068 current_mask = new_mask;
5069 return 0;
5070 }
5071
5072 #else /* not POSIX_SIGNALS */
5073
5074 sigsetmask (x) int x; { return 0; }
5075 sigblock (mask) int mask; { return 0; }
5076
5077 #endif /* not POSIX_SIGNALS */
5078 #endif /* not __DJGPP_MINOR__ < 2 */
5079 #endif /* __DJGPP__ > 1 */
5080
5081 #ifndef HAVE_SELECT
5082 #include "sysselect.h"
5083
5084 #ifndef EMACS_TIME_ZERO_OR_NEG_P
5085 #define EMACS_TIME_ZERO_OR_NEG_P(time) \
5086 ((long)(time).tv_sec < 0 \
5087 || ((time).tv_sec == 0 \
5088 && (long)(time).tv_usec <= 0))
5089 #endif
5090
5091 /* This yields the rest of the current time slice to the task manager.
5092 It should be called by any code which knows that it has nothing
5093 useful to do except idle.
5094
5095 I don't use __dpmi_yield here, since versions of library before 2.02
5096 called Int 2Fh/AX=1680h there in a way that would wedge the DOS box
5097 on some versions of Windows 9X. */
5098
5099 void
5100 dos_yield_time_slice (void)
5101 {
5102 _go32_dpmi_registers r;
5103
5104 r.x.ax = 0x1680;
5105 r.x.ss = r.x.sp = r.x.flags = 0;
5106 _go32_dpmi_simulate_int (0x2f, &r);
5107 if (r.h.al == 0x80)
5108 errno = ENOSYS;
5109 }
5110
5111 /* Only event queue is checked. */
5112 /* We don't have to call timer_check here
5113 because wait_reading_process_output takes care of that. */
5114 int
5115 sys_select (nfds, rfds, wfds, efds, timeout)
5116 int nfds;
5117 SELECT_TYPE *rfds, *wfds, *efds;
5118 EMACS_TIME *timeout;
5119 {
5120 int check_input;
5121 struct time t;
5122
5123 check_input = 0;
5124 if (rfds)
5125 {
5126 check_input = FD_ISSET (0, rfds);
5127 FD_ZERO (rfds);
5128 }
5129 if (wfds)
5130 FD_ZERO (wfds);
5131 if (efds)
5132 FD_ZERO (efds);
5133
5134 if (nfds != 1)
5135 abort ();
5136
5137 /* If we are looking only for the terminal, with no timeout,
5138 just read it and wait -- that's more efficient. */
5139 if (!timeout)
5140 {
5141 while (!detect_input_pending ())
5142 {
5143 dos_yield_time_slice ();
5144 }
5145 }
5146 else
5147 {
5148 EMACS_TIME clnow, cllast, cldiff;
5149
5150 gettime (&t);
5151 EMACS_SET_SECS_USECS (cllast, t.ti_sec, t.ti_hund * 10000L);
5152
5153 while (!check_input || !detect_input_pending ())
5154 {
5155 gettime (&t);
5156 EMACS_SET_SECS_USECS (clnow, t.ti_sec, t.ti_hund * 10000L);
5157 EMACS_SUB_TIME (cldiff, clnow, cllast);
5158
5159 /* When seconds wrap around, we assume that no more than
5160 1 minute passed since last `gettime'. */
5161 if (EMACS_TIME_NEG_P (cldiff))
5162 EMACS_SET_SECS (cldiff, EMACS_SECS (cldiff) + 60);
5163 EMACS_SUB_TIME (*timeout, *timeout, cldiff);
5164
5165 /* Stop when timeout value crosses zero. */
5166 if (EMACS_TIME_ZERO_OR_NEG_P (*timeout))
5167 return 0;
5168 cllast = clnow;
5169 dos_yield_time_slice ();
5170 }
5171 }
5172
5173 FD_SET (0, rfds);
5174 return 1;
5175 }
5176 #endif
5177
5178 /*
5179 * Define overlaid functions:
5180 *
5181 * chdir -> sys_chdir
5182 * tzset -> init_gettimeofday
5183 * abort -> dos_abort
5184 */
5185
5186 #ifdef chdir
5187 #undef chdir
5188 extern int chdir ();
5189
5190 int
5191 sys_chdir (path)
5192 const char* path;
5193 {
5194 int len = strlen (path);
5195 char *tmp = (char *)path;
5196
5197 if (*tmp && tmp[1] == ':')
5198 {
5199 if (getdisk () != tolower (tmp[0]) - 'a')
5200 setdisk (tolower (tmp[0]) - 'a');
5201 tmp += 2; /* strip drive: KFS 1995-07-06 */
5202 len -= 2;
5203 }
5204
5205 if (len > 1 && (tmp[len - 1] == '/'))
5206 {
5207 char *tmp1 = (char *) alloca (len + 1);
5208 strcpy (tmp1, tmp);
5209 tmp1[len - 1] = 0;
5210 tmp = tmp1;
5211 }
5212 return chdir (tmp);
5213 }
5214 #endif
5215
5216 #ifdef tzset
5217 #undef tzset
5218 extern void tzset (void);
5219
5220 void
5221 init_gettimeofday ()
5222 {
5223 time_t ltm, gtm;
5224 struct tm *lstm;
5225
5226 tzset ();
5227 ltm = gtm = time (NULL);
5228 ltm = mktime (lstm = localtime (&ltm));
5229 gtm = mktime (gmtime (&gtm));
5230 time_rec.tm_hour = 99; /* force gettimeofday to get date */
5231 time_rec.tm_isdst = lstm->tm_isdst;
5232 dos_timezone_offset = time_rec.tm_gmtoff = (int)(gtm - ltm) / 60;
5233 }
5234 #endif
5235
5236 #ifdef abort
5237 #undef abort
5238 void
5239 dos_abort (file, line)
5240 char *file;
5241 int line;
5242 {
5243 char buffer1[200], buffer2[400];
5244 int i, j;
5245
5246 sprintf (buffer1, "<EMACS FATAL ERROR IN %s LINE %d>", file, line);
5247 for (i = j = 0; buffer1[i]; i++) {
5248 buffer2[j++] = buffer1[i];
5249 buffer2[j++] = 0x70;
5250 }
5251 dosmemput (buffer2, j, (int)ScreenPrimary);
5252 ScreenSetCursor (2, 0);
5253 abort ();
5254 }
5255 #else
5256 void
5257 abort ()
5258 {
5259 dos_ttcooked ();
5260 ScreenSetCursor (10, 0);
5261 cputs ("\r\n\nEmacs aborted!\r\n");
5262 #if __DJGPP__ > 1
5263 #if __DJGPP__ == 2 && __DJGPP_MINOR__ < 2
5264 if (screen_virtual_segment)
5265 dosv_refresh_virtual_screen (2 * 10 * screen_size_X, 4 * screen_size_X);
5266 /* Generate traceback, so we could tell whodunit. */
5267 signal (SIGINT, SIG_DFL);
5268 __asm__ __volatile__ ("movb $0x1b,%al;call ___djgpp_hw_exception");
5269 #else /* __DJGPP_MINOR__ >= 2 */
5270 raise (SIGABRT);
5271 #endif /* __DJGPP_MINOR__ >= 2 */
5272 #endif
5273 exit (2);
5274 }
5275 #endif
5276
5277 /* The following variables are required so that cus-start.el won't
5278 complain about unbound variables. */
5279 #ifndef subprocesses
5280 /* Nonzero means delete a process right away if it exits (process.c). */
5281 static int delete_exited_processes;
5282 #endif
5283
5284 syms_of_msdos ()
5285 {
5286 recent_doskeys = Fmake_vector (make_number (NUM_RECENT_DOSKEYS), Qnil);
5287 staticpro (&recent_doskeys);
5288
5289 #ifndef HAVE_X_WINDOWS
5290
5291 /* The following two are from xfns.c: */
5292 Qreverse = intern ("reverse");
5293 staticpro (&Qreverse);
5294
5295 DEFVAR_LISP ("dos-unsupported-char-glyph", &Vdos_unsupported_char_glyph,
5296 doc: /* *Glyph to display instead of chars not supported by current codepage.
5297 This variable is used only by MSDOS terminals. */);
5298 Vdos_unsupported_char_glyph = make_number ('\177');
5299
5300 #endif
5301 #ifndef subprocesses
5302 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
5303 doc: /* *Non-nil means delete processes immediately when they exit.
5304 A value of nil means don't delete them until `list-processes' is run. */);
5305 delete_exited_processes = 0;
5306 #endif
5307
5308 defsubr (&Srecent_doskeys);
5309 defsubr (&Smsdos_long_file_names);
5310 defsubr (&Smsdos_downcase_filename);
5311 defsubr (&Smsdos_remember_default_colors);
5312 defsubr (&Smsdos_set_mouse_buttons);
5313 }
5314
5315 #endif /* MSDOS */
5316
5317 /* arch-tag: db404e92-52a5-475f-9eb2-1cb78dd05f30
5318 (do not change this comment) */