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