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