(Info-index-next): Use "(`,' tries to find next)" for
[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 && STRING_INTERVALS (glyph->object)
1458 && glyph->charpos >= 0
1459 && glyph->charpos < SCHARS (glyph->object))
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,
1677 !dpyinfo->mouse_face_hidden);
1678
1679 /* Display it as active. */
1680 show_mouse_face (dpyinfo, 1);
1681 }
1682 /* Handle the text property case. */
1683 else if (! NILP (mouse_face))
1684 {
1685 /* Find the range of text around this char that
1686 should be active. */
1687 Lisp_Object before, after, beginning, end;
1688 int ignore;
1689
1690 beginning = Fmarker_position (w->start);
1691 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
1692 - XFASTINT (w->window_end_pos)));
1693 before
1694 = Fprevious_single_property_change (make_number (pos + 1),
1695 Qmouse_face,
1696 w->buffer, beginning);
1697 after
1698 = Fnext_single_property_change (position, Qmouse_face,
1699 w->buffer, end);
1700 /* Record this as the current active region. */
1701 fast_find_position (w, XFASTINT (before),
1702 &dpyinfo->mouse_face_beg_col,
1703 &dpyinfo->mouse_face_beg_row);
1704 dpyinfo->mouse_face_past_end
1705 = !fast_find_position (w, XFASTINT (after),
1706 &dpyinfo->mouse_face_end_col,
1707 &dpyinfo->mouse_face_end_row);
1708 dpyinfo->mouse_face_window = window;
1709 dpyinfo->mouse_face_face_id
1710 = face_at_buffer_position (w, pos, 0, 0,
1711 &ignore, pos + 1,
1712 !dpyinfo->mouse_face_hidden);
1713
1714 /* Display it as active. */
1715 show_mouse_face (dpyinfo, 1);
1716 }
1717 }
1718
1719 /* Look for a `help-echo' property. */
1720 {
1721 Lisp_Object help;
1722 extern Lisp_Object Qhelp_echo;
1723
1724 /* Check overlays first. */
1725 help = Qnil;
1726 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
1727 {
1728 overlay = overlay_vec[i];
1729 help = Foverlay_get (overlay, Qhelp_echo);
1730 }
1731
1732 if (!NILP (help))
1733 {
1734 help_echo = help;
1735 help_echo_window = window;
1736 help_echo_object = overlay;
1737 help_echo_pos = pos;
1738 }
1739 /* Try text properties. */
1740 else if (NILP (help)
1741 && ((STRINGP (glyph->object)
1742 && glyph->charpos >= 0
1743 && glyph->charpos < SCHARS (glyph->object))
1744 || (BUFFERP (glyph->object)
1745 && glyph->charpos >= BEGV
1746 && glyph->charpos < ZV)))
1747 {
1748 help = Fget_text_property (make_number (glyph->charpos),
1749 Qhelp_echo, glyph->object);
1750 if (!NILP (help))
1751 {
1752 help_echo = help;
1753 help_echo_window = window;
1754 help_echo_object = glyph->object;
1755 help_echo_pos = glyph->charpos;
1756 }
1757 }
1758 }
1759
1760 BEGV = obegv;
1761 ZV = ozv;
1762 current_buffer = obuf;
1763 }
1764 }
1765 }
1766
1767 static void
1768 IT_clear_end_of_line (int first_unused)
1769 {
1770 char *spaces, *sp;
1771 int i, j, offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
1772 extern int fatal_error_in_progress;
1773
1774 if (new_pos_X >= first_unused || fatal_error_in_progress)
1775 return;
1776
1777 IT_set_face (0);
1778 i = (j = first_unused - new_pos_X) * 2;
1779 if (termscript)
1780 fprintf (termscript, "<CLR:EOL[%d..%d)>", new_pos_X, first_unused);
1781 spaces = sp = alloca (i);
1782
1783 while (--j >= 0)
1784 {
1785 *sp++ = ' ';
1786 *sp++ = ScreenAttrib;
1787 }
1788
1789 mouse_off_maybe ();
1790 dosmemput (spaces, i, (int)ScreenPrimary + offset);
1791 if (screen_virtual_segment)
1792 dosv_refresh_virtual_screen (offset, i / 2);
1793
1794 /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
1795 Let's follow their lead, in case someone relies on this. */
1796 new_pos_X = first_unused;
1797 }
1798
1799 static void
1800 IT_clear_screen (void)
1801 {
1802 if (termscript)
1803 fprintf (termscript, "<CLR:SCR>");
1804 /* We are sometimes called (from clear_garbaged_frames) when a new
1805 frame is being created, but its faces are not yet realized. In
1806 such a case we cannot call IT_set_face, since it will fail to find
1807 any valid faces and will abort. Instead, use the initial screen
1808 colors; that should mimic what a Unix tty does, which simply clears
1809 the screen with whatever default colors are in use. */
1810 if (FACE_FROM_ID (SELECTED_FRAME (), DEFAULT_FACE_ID) == NULL)
1811 ScreenAttrib = (initial_screen_colors[0] << 4) | initial_screen_colors[1];
1812 else
1813 IT_set_face (0);
1814 mouse_off ();
1815 ScreenClear ();
1816 if (screen_virtual_segment)
1817 dosv_refresh_virtual_screen (0, screen_size);
1818 new_pos_X = new_pos_Y = 0;
1819 }
1820
1821 static void
1822 IT_clear_to_end (void)
1823 {
1824 if (termscript)
1825 fprintf (termscript, "<CLR:EOS>");
1826
1827 while (new_pos_Y < screen_size_Y) {
1828 new_pos_X = 0;
1829 IT_clear_end_of_line (screen_size_X);
1830 new_pos_Y++;
1831 }
1832 }
1833
1834 static void
1835 IT_cursor_to (int y, int x)
1836 {
1837 if (termscript)
1838 fprintf (termscript, "\n<XY=%dx%d>", x, y);
1839 new_pos_X = x;
1840 new_pos_Y = y;
1841 }
1842
1843 static int cursor_cleared;
1844
1845 static void
1846 IT_display_cursor (int on)
1847 {
1848 if (on && cursor_cleared)
1849 {
1850 ScreenSetCursor (current_pos_Y, current_pos_X);
1851 cursor_cleared = 0;
1852 }
1853 else if (!on && !cursor_cleared)
1854 {
1855 ScreenSetCursor (-1, -1);
1856 cursor_cleared = 1;
1857 }
1858 }
1859
1860 /* Emacs calls cursor-movement functions a lot when it updates the
1861 display (probably a legacy of old terminals where you cannot
1862 update a screen line without first moving the cursor there).
1863 However, cursor movement is expensive on MSDOS (it calls a slow
1864 BIOS function and requires 2 mode switches), while actual screen
1865 updates access the video memory directly and don't depend on
1866 cursor position. To avoid slowing down the redisplay, we cheat:
1867 all functions that move the cursor only set internal variables
1868 which record the cursor position, whereas the cursor is only
1869 moved to its final position whenever screen update is complete.
1870
1871 `IT_cmgoto' is called from the keyboard reading loop and when the
1872 frame update is complete. This means that we are ready for user
1873 input, so we update the cursor position to show where the point is,
1874 and also make the mouse pointer visible.
1875
1876 Special treatment is required when the cursor is in the echo area,
1877 to put the cursor at the end of the text displayed there. */
1878
1879 static void
1880 IT_cmgoto (FRAME_PTR f)
1881 {
1882 /* Only set the cursor to where it should be if the display is
1883 already in sync with the window contents. */
1884 int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */
1885
1886 /* FIXME: This needs to be rewritten for the new redisplay, or
1887 removed. */
1888 #if 0
1889 static int previous_pos_X = -1;
1890
1891 update_cursor_pos = 1; /* temporary!!! */
1892
1893 /* If the display is in sync, forget any previous knowledge about
1894 cursor position. This is primarily for unexpected events like
1895 C-g in the minibuffer. */
1896 if (update_cursor_pos && previous_pos_X >= 0)
1897 previous_pos_X = -1;
1898 /* If we are in the echo area, put the cursor at the
1899 end of the echo area message. */
1900 if (!update_cursor_pos
1901 && XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top) <= new_pos_Y)
1902 {
1903 int tem_X = current_pos_X, dummy;
1904
1905 if (echo_area_glyphs)
1906 {
1907 tem_X = echo_area_glyphs_length;
1908 /* Save current cursor position, to be restored after the
1909 echo area message is erased. Only remember one level
1910 of previous cursor position. */
1911 if (previous_pos_X == -1)
1912 ScreenGetCursor (&dummy, &previous_pos_X);
1913 }
1914 else if (previous_pos_X >= 0)
1915 {
1916 /* We wind up here after the echo area message is erased.
1917 Restore the cursor position we remembered above. */
1918 tem_X = previous_pos_X;
1919 previous_pos_X = -1;
1920 }
1921
1922 if (current_pos_X != tem_X)
1923 {
1924 new_pos_X = tem_X;
1925 update_cursor_pos = 1;
1926 }
1927 }
1928 #endif
1929
1930 if (update_cursor_pos
1931 && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
1932 {
1933 ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
1934 if (termscript)
1935 fprintf (termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
1936 }
1937
1938 /* Maybe cursor is invisible, so make it visible. */
1939 IT_display_cursor (1);
1940
1941 /* Mouse pointer should be always visible if we are waiting for
1942 keyboard input. */
1943 if (!mouse_visible)
1944 mouse_on ();
1945 }
1946
1947 static void
1948 IT_update_begin (struct frame *f)
1949 {
1950 struct display_info *display_info = FRAME_X_DISPLAY_INFO (f);
1951 struct frame *mouse_face_frame = display_info->mouse_face_mouse_frame;
1952
1953 BLOCK_INPUT;
1954
1955 if (f && f == mouse_face_frame)
1956 {
1957 /* Don't do highlighting for mouse motion during the update. */
1958 display_info->mouse_face_defer = 1;
1959
1960 /* If F needs to be redrawn, simply forget about any prior mouse
1961 highlighting. */
1962 if (FRAME_GARBAGED_P (f))
1963 display_info->mouse_face_window = Qnil;
1964
1965 /* Can we tell that this update does not affect the window
1966 where the mouse highlight is? If so, no need to turn off.
1967 Likewise, don't do anything if none of the enabled rows
1968 contains glyphs highlighted in mouse face. */
1969 if (!NILP (display_info->mouse_face_window)
1970 && WINDOWP (display_info->mouse_face_window))
1971 {
1972 struct window *w = XWINDOW (display_info->mouse_face_window);
1973 int i;
1974
1975 /* If the mouse highlight is in the window that was deleted
1976 (e.g., if it was popped by completion), clear highlight
1977 unconditionally. */
1978 if (NILP (w->buffer))
1979 display_info->mouse_face_window = Qnil;
1980 else
1981 {
1982 for (i = 0; i < w->desired_matrix->nrows; ++i)
1983 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i)
1984 && MATRIX_ROW (w->current_matrix, i)->mouse_face_p)
1985 break;
1986 }
1987
1988 if (NILP (w->buffer) || i < w->desired_matrix->nrows)
1989 clear_mouse_face (display_info);
1990 }
1991 }
1992 else if (mouse_face_frame && !FRAME_LIVE_P (mouse_face_frame))
1993 {
1994 /* If the frame with mouse highlight was deleted, invalidate the
1995 highlight info. */
1996 display_info->mouse_face_beg_row = display_info->mouse_face_beg_col = -1;
1997 display_info->mouse_face_end_row = display_info->mouse_face_end_col = -1;
1998 display_info->mouse_face_window = Qnil;
1999 display_info->mouse_face_deferred_gc = 0;
2000 display_info->mouse_face_mouse_frame = NULL;
2001 }
2002
2003 UNBLOCK_INPUT;
2004 }
2005
2006 static void
2007 IT_update_end (struct frame *f)
2008 {
2009 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
2010 }
2011
2012 Lisp_Object Qcursor_type;
2013
2014 static void
2015 IT_frame_up_to_date (struct frame *f)
2016 {
2017 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2018 Lisp_Object new_cursor, frame_desired_cursor;
2019 struct window *sw;
2020
2021 if (dpyinfo->mouse_face_deferred_gc
2022 || (f && f == dpyinfo->mouse_face_mouse_frame))
2023 {
2024 BLOCK_INPUT;
2025 if (dpyinfo->mouse_face_mouse_frame)
2026 IT_note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
2027 dpyinfo->mouse_face_mouse_x,
2028 dpyinfo->mouse_face_mouse_y);
2029 dpyinfo->mouse_face_deferred_gc = 0;
2030 UNBLOCK_INPUT;
2031 }
2032
2033 /* Set the cursor type to whatever they wanted. In a minibuffer
2034 window, we want the cursor to appear only if we are reading input
2035 from this window, and we want the cursor to be taken from the
2036 frame parameters. For the selected window, we use either its
2037 buffer-local value or the value from the frame parameters if the
2038 buffer doesn't define its local value for the cursor type. */
2039 sw = XWINDOW (f->selected_window);
2040 frame_desired_cursor = Fcdr (Fassq (Qcursor_type, f->param_alist));
2041 if (cursor_in_echo_area
2042 && FRAME_HAS_MINIBUF_P (f)
2043 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window)
2044 && sw == XWINDOW (echo_area_window))
2045 new_cursor = frame_desired_cursor;
2046 else
2047 {
2048 struct buffer *b = XBUFFER (sw->buffer);
2049
2050 if (EQ (b->cursor_type, Qt))
2051 new_cursor = frame_desired_cursor;
2052 else if (NILP (b->cursor_type)) /* nil means no cursor */
2053 new_cursor = Fcons (Qbar, make_number (0));
2054 else
2055 new_cursor = b->cursor_type;
2056 }
2057
2058 IT_set_cursor_type (f, new_cursor);
2059
2060 IT_cmgoto (f); /* position cursor when update is done */
2061 }
2062
2063 /* Copy LEN glyphs displayed on a single line whose vertical position
2064 is YPOS, beginning at horizontal position XFROM to horizontal
2065 position XTO, by moving blocks in the video memory. Used by
2066 functions that insert and delete glyphs. */
2067 static void
2068 IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
2069 {
2070 /* The offsets of source and destination relative to the
2071 conventional memorty selector. */
2072 int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
2073 int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;
2074
2075 if (from == to || len <= 0)
2076 return;
2077
2078 _farsetsel (_dos_ds);
2079
2080 /* The source and destination might overlap, so we need to move
2081 glyphs non-destructively. */
2082 if (from > to)
2083 {
2084 for ( ; len; from += 2, to += 2, len--)
2085 _farnspokew (to, _farnspeekw (from));
2086 }
2087 else
2088 {
2089 from += (len - 1) * 2;
2090 to += (len - 1) * 2;
2091 for ( ; len; from -= 2, to -= 2, len--)
2092 _farnspokew (to, _farnspeekw (from));
2093 }
2094 if (screen_virtual_segment)
2095 dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
2096 }
2097
2098 /* Insert and delete glyphs. */
2099 static void
2100 IT_insert_glyphs (start, len)
2101 register struct glyph *start;
2102 register int len;
2103 {
2104 int shift_by_width = screen_size_X - (new_pos_X + len);
2105
2106 /* Shift right the glyphs from the nominal cursor position to the
2107 end of this line. */
2108 IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);
2109
2110 /* Now write the glyphs to be inserted. */
2111 IT_write_glyphs (start, len);
2112 }
2113
2114 static void
2115 IT_delete_glyphs (n)
2116 register int n;
2117 {
2118 abort ();
2119 }
2120
2121 /* set-window-configuration on window.c needs this. */
2122 void
2123 x_set_menu_bar_lines (f, value, oldval)
2124 struct frame *f;
2125 Lisp_Object value, oldval;
2126 {
2127 set_menu_bar_lines (f, value, oldval);
2128 }
2129
2130 /* This was copied from xfaces.c */
2131
2132 extern Lisp_Object Qbackground_color;
2133 extern Lisp_Object Qforeground_color;
2134 Lisp_Object Qreverse;
2135 extern Lisp_Object Qtitle;
2136
2137 /* IT_set_terminal_modes is called when emacs is started,
2138 resumed, and whenever the screen is redrawn! */
2139
2140 static void
2141 IT_set_terminal_modes (void)
2142 {
2143 if (termscript)
2144 fprintf (termscript, "\n<SET_TERM>");
2145
2146 screen_size_X = ScreenCols ();
2147 screen_size_Y = ScreenRows ();
2148 screen_size = screen_size_X * screen_size_Y;
2149
2150 new_pos_X = new_pos_Y = 0;
2151 current_pos_X = current_pos_Y = -1;
2152
2153 if (term_setup_done)
2154 return;
2155 term_setup_done = 1;
2156
2157 startup_screen_size_X = screen_size_X;
2158 startup_screen_size_Y = screen_size_Y;
2159 startup_screen_attrib = ScreenAttrib;
2160
2161 #if __DJGPP__ > 1
2162 /* Is DOS/V (or any other RSIS software which relocates
2163 the screen) installed? */
2164 {
2165 unsigned short es_value;
2166 __dpmi_regs regs;
2167
2168 regs.h.ah = 0xfe; /* get relocated screen address */
2169 if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
2170 regs.x.es = (ScreenPrimary >> 4) & 0xffff;
2171 else if (screen_old_address) /* already switched to Japanese mode once */
2172 regs.x.es = (screen_old_address >> 4) & 0xffff;
2173 else
2174 regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
2175 regs.x.di = 0;
2176 es_value = regs.x.es;
2177 __dpmi_int (0x10, &regs);
2178
2179 if (regs.x.es != es_value)
2180 {
2181 /* screen_old_address is only set if ScreenPrimary does NOT
2182 already point to the relocated buffer address returned by
2183 the Int 10h/AX=FEh call above. DJGPP v2.02 and later sets
2184 ScreenPrimary to that address at startup under DOS/V. */
2185 if (regs.x.es != (ScreenPrimary >> 4) & 0xffff)
2186 screen_old_address = ScreenPrimary;
2187 screen_virtual_segment = regs.x.es;
2188 screen_virtual_offset = regs.x.di;
2189 ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
2190 }
2191 }
2192 #endif /* __DJGPP__ > 1 */
2193
2194 ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
2195 ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));
2196
2197 if (termscript)
2198 fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
2199 screen_size_X, screen_size_Y);
2200
2201 bright_bg ();
2202 }
2203
2204 /* IT_reset_terminal_modes is called when emacs is
2205 suspended or killed. */
2206
2207 static void
2208 IT_reset_terminal_modes (void)
2209 {
2210 int display_row_start = (int) ScreenPrimary;
2211 int saved_row_len = startup_screen_size_X * 2;
2212 int update_row_len = ScreenCols () * 2, current_rows = ScreenRows ();
2213 int to_next_row = update_row_len;
2214 unsigned char *saved_row = startup_screen_buffer;
2215 int cursor_pos_X = ScreenCols () - 1, cursor_pos_Y = ScreenRows () - 1;
2216
2217 if (termscript)
2218 fprintf (termscript, "\n<RESET_TERM>");
2219
2220 if (!term_setup_done)
2221 return;
2222
2223 mouse_off ();
2224
2225 /* Leave the video system in the same state as we found it,
2226 as far as the blink/bright-background bit is concerned. */
2227 maybe_enable_blinking ();
2228
2229 /* We have a situation here.
2230 We cannot just do ScreenUpdate(startup_screen_buffer) because
2231 the luser could have changed screen dimensions inside Emacs
2232 and failed (or didn't want) to restore them before killing
2233 Emacs. ScreenUpdate() uses the *current* screen dimensions and
2234 thus will happily use memory outside what was allocated for
2235 `startup_screen_buffer'.
2236 Thus we only restore as much as the current screen dimensions
2237 can hold, and clear the rest (if the saved screen is smaller than
2238 the current) with the color attribute saved at startup. The cursor
2239 is also restored within the visible dimensions. */
2240
2241 ScreenAttrib = startup_screen_attrib;
2242
2243 /* Don't restore the screen if we are exiting less than 2 seconds
2244 after startup: we might be crashing, and the screen might show
2245 some vital clues to what's wrong. */
2246 if (clock () - startup_time >= 2*CLOCKS_PER_SEC)
2247 {
2248 ScreenClear ();
2249 if (screen_virtual_segment)
2250 dosv_refresh_virtual_screen (0, screen_size);
2251
2252 if (update_row_len > saved_row_len)
2253 update_row_len = saved_row_len;
2254 if (current_rows > startup_screen_size_Y)
2255 current_rows = startup_screen_size_Y;
2256
2257 if (termscript)
2258 fprintf (termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
2259 update_row_len / 2, current_rows);
2260
2261 while (current_rows--)
2262 {
2263 dosmemput (saved_row, update_row_len, display_row_start);
2264 if (screen_virtual_segment)
2265 dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
2266 update_row_len / 2);
2267 saved_row += saved_row_len;
2268 display_row_start += to_next_row;
2269 }
2270 }
2271 if (startup_pos_X < cursor_pos_X)
2272 cursor_pos_X = startup_pos_X;
2273 if (startup_pos_Y < cursor_pos_Y)
2274 cursor_pos_Y = startup_pos_Y;
2275
2276 ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
2277 xfree (startup_screen_buffer);
2278
2279 term_setup_done = 0;
2280 }
2281
2282 static void
2283 IT_set_terminal_window (int foo)
2284 {
2285 }
2286
2287 /* Remember the screen colors of the curent frame, to serve as the
2288 default colors for newly-created frames. */
2289 DEFUN ("msdos-remember-default-colors", Fmsdos_remember_default_colors,
2290 Smsdos_remember_default_colors, 1, 1, 0,
2291 doc: /* Remember the screen colors of the current frame. */)
2292 (frame)
2293 Lisp_Object frame;
2294 {
2295 struct frame *f;
2296
2297 CHECK_FRAME (frame);
2298 f= XFRAME (frame);
2299
2300 /* This function is called after applying default-frame-alist to the
2301 initial frame. At that time, if reverse-colors option was
2302 specified in default-frame-alist, it was already applied, and
2303 frame colors are reversed. We need to account for that. */
2304 if (EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt))
2305 {
2306 initial_screen_colors[0] = FRAME_BACKGROUND_PIXEL (f);
2307 initial_screen_colors[1] = FRAME_FOREGROUND_PIXEL (f);
2308 }
2309 else
2310 {
2311 initial_screen_colors[0] = FRAME_FOREGROUND_PIXEL (f);
2312 initial_screen_colors[1] = FRAME_BACKGROUND_PIXEL (f);
2313 }
2314 }
2315
2316 void
2317 IT_set_frame_parameters (f, alist)
2318 struct frame *f;
2319 Lisp_Object alist;
2320 {
2321 Lisp_Object tail;
2322 int i, j, length = XINT (Flength (alist));
2323 Lisp_Object *parms
2324 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2325 Lisp_Object *values
2326 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2327 /* Do we have to reverse the foreground and background colors? */
2328 int reverse = EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt);
2329 int need_to_reverse, was_reverse = reverse;
2330 int redraw = 0, fg_set = 0, bg_set = 0;
2331 unsigned long orig_fg, orig_bg;
2332 Lisp_Object frame_bg, frame_fg;
2333 extern Lisp_Object Qdefault, QCforeground, QCbackground;
2334
2335 /* If we are creating a new frame, begin with the original screen colors
2336 used for the initial frame. */
2337 if (alist == Vdefault_frame_alist
2338 && initial_screen_colors[0] != -1 && initial_screen_colors[1] != -1)
2339 {
2340 FRAME_FOREGROUND_PIXEL (f) = initial_screen_colors[0];
2341 FRAME_BACKGROUND_PIXEL (f) = initial_screen_colors[1];
2342 }
2343 orig_fg = FRAME_FOREGROUND_PIXEL (f);
2344 orig_bg = FRAME_BACKGROUND_PIXEL (f);
2345 frame_fg = Fcdr (Fassq (Qforeground_color, f->param_alist));
2346 frame_bg = Fcdr (Fassq (Qbackground_color, f->param_alist));
2347 /* frame_fg and frame_bg could be nil if, for example,
2348 f->param_alist is nil, e.g. if we are called from
2349 Fmake_terminal_frame. */
2350 if (NILP (frame_fg))
2351 frame_fg = build_string (unspecified_fg);
2352 if (NILP (frame_bg))
2353 frame_bg = build_string (unspecified_bg);
2354
2355 /* Extract parm names and values into those vectors. */
2356 i = 0;
2357 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
2358 {
2359 Lisp_Object elt;
2360
2361 elt = Fcar (tail);
2362 parms[i] = Fcar (elt);
2363 CHECK_SYMBOL (parms[i]);
2364 values[i] = Fcdr (elt);
2365 i++;
2366 }
2367
2368 j = i;
2369
2370 for (i = 0; i < j; i++)
2371 {
2372 Lisp_Object prop, val;
2373
2374 prop = parms[i];
2375 val = values[i];
2376
2377 if (EQ (prop, Qreverse))
2378 reverse = EQ (val, Qt);
2379 }
2380
2381 need_to_reverse = reverse && !was_reverse;
2382 if (termscript && need_to_reverse)
2383 fprintf (termscript, "<INVERSE-VIDEO>\n");
2384
2385 /* Now process the alist elements in reverse of specified order. */
2386 for (i--; i >= 0; i--)
2387 {
2388 Lisp_Object prop, val, frame;
2389
2390 prop = parms[i];
2391 val = values[i];
2392
2393 if (EQ (prop, Qforeground_color))
2394 {
2395 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2396 ? LFACE_BACKGROUND_INDEX
2397 : LFACE_FOREGROUND_INDEX);
2398 if (new_color != FACE_TTY_DEFAULT_COLOR
2399 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2400 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2401 {
2402 FRAME_FOREGROUND_PIXEL (f) = new_color;
2403 /* Make sure the foreground of the default face for this
2404 frame is changed as well. */
2405 XSETFRAME (frame, f);
2406 if (need_to_reverse)
2407 {
2408 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2409 val, frame);
2410 prop = Qbackground_color;
2411 bg_set = 1;
2412 }
2413 else
2414 {
2415 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2416 val, frame);
2417 fg_set = 1;
2418 }
2419 redraw = 1;
2420 if (termscript)
2421 fprintf (termscript, "<FGCOLOR %lu>\n", new_color);
2422 }
2423 }
2424 else if (EQ (prop, Qbackground_color))
2425 {
2426 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2427 ? LFACE_FOREGROUND_INDEX
2428 : LFACE_BACKGROUND_INDEX);
2429 if (new_color != FACE_TTY_DEFAULT_COLOR
2430 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2431 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2432 {
2433 FRAME_BACKGROUND_PIXEL (f) = new_color;
2434 /* Make sure the background of the default face for this
2435 frame is changed as well. */
2436 XSETFRAME (frame, f);
2437 if (need_to_reverse)
2438 {
2439 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2440 val, frame);
2441 prop = Qforeground_color;
2442 fg_set = 1;
2443 }
2444 else
2445 {
2446 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2447 val, frame);
2448 bg_set = 1;
2449 }
2450 redraw = 1;
2451 if (termscript)
2452 fprintf (termscript, "<BGCOLOR %lu>\n", new_color);
2453 }
2454 }
2455 else if (EQ (prop, Qtitle))
2456 {
2457 x_set_title (f, val);
2458 if (termscript)
2459 fprintf (termscript, "<TITLE: %s>\n", SDATA (val));
2460 }
2461 else if (EQ (prop, Qcursor_type))
2462 {
2463 IT_set_cursor_type (f, val);
2464 if (termscript)
2465 fprintf (termscript, "<CTYPE: %s>\n",
2466 EQ (val, Qbar) || EQ (val, Qhbar)
2467 || CONSP (val) && (EQ (XCAR (val), Qbar)
2468 || EQ (XCAR (val), Qhbar))
2469 ? "bar" : "box");
2470 }
2471 store_frame_param (f, prop, val);
2472 }
2473
2474 /* If they specified "reverse", but not the colors, we need to swap
2475 the current frame colors. */
2476 if (need_to_reverse)
2477 {
2478 Lisp_Object frame;
2479
2480 if (!fg_set)
2481 {
2482 XSETFRAME (frame, f);
2483 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2484 tty_color_name (f, orig_bg),
2485 frame);
2486 redraw = 1;
2487 }
2488 if (!bg_set)
2489 {
2490 XSETFRAME (frame, f);
2491 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2492 tty_color_name (f, orig_fg),
2493 frame);
2494 redraw = 1;
2495 }
2496 }
2497
2498 if (redraw)
2499 {
2500 face_change_count++; /* forces xdisp.c to recompute basic faces */
2501 if (f == SELECTED_FRAME())
2502 redraw_frame (f);
2503 }
2504 }
2505
2506 extern void init_frame_faces (FRAME_PTR);
2507
2508 #endif /* !HAVE_X_WINDOWS */
2509
2510
2511 /* Do we need the internal terminal? */
2512
2513 void
2514 internal_terminal_init ()
2515 {
2516 char *term = getenv ("TERM"), *colors;
2517 struct frame *sf = SELECTED_FRAME();
2518
2519 #ifdef HAVE_X_WINDOWS
2520 if (!inhibit_window_system)
2521 return;
2522 #endif
2523
2524 internal_terminal
2525 = (!noninteractive) && term && !strcmp (term, "internal");
2526
2527 if (getenv ("EMACSTEST"))
2528 termscript = fopen (getenv ("EMACSTEST"), "wt");
2529
2530 #ifndef HAVE_X_WINDOWS
2531 if (!internal_terminal || inhibit_window_system)
2532 {
2533 sf->output_method = output_termcap;
2534 return;
2535 }
2536
2537 Vwindow_system = intern ("pc");
2538 Vwindow_system_version = make_number (1);
2539 sf->output_method = output_msdos_raw;
2540
2541 /* If Emacs was dumped on DOS/V machine, forget the stale VRAM address. */
2542 screen_old_address = 0;
2543
2544 /* Forget the stale screen colors as well. */
2545 initial_screen_colors[0] = initial_screen_colors[1] = -1;
2546
2547 bzero (&the_only_x_display, sizeof the_only_x_display);
2548 the_only_x_display.background_pixel = 7; /* White */
2549 the_only_x_display.foreground_pixel = 0; /* Black */
2550 bright_bg ();
2551 colors = getenv ("EMACSCOLORS");
2552 if (colors && strlen (colors) >= 2)
2553 {
2554 /* The colors use 4 bits each (we enable bright background). */
2555 if (isdigit (colors[0]))
2556 colors[0] -= '0';
2557 else if (isxdigit (colors[0]))
2558 colors[0] -= (isupper (colors[0]) ? 'A' : 'a') - 10;
2559 if (colors[0] >= 0 && colors[0] < 16)
2560 the_only_x_display.foreground_pixel = colors[0];
2561 if (isdigit (colors[1]))
2562 colors[1] -= '0';
2563 else if (isxdigit (colors[1]))
2564 colors[1] -= (isupper (colors[1]) ? 'A' : 'a') - 10;
2565 if (colors[1] >= 0 && colors[1] < 16)
2566 the_only_x_display.background_pixel = colors[1];
2567 }
2568 the_only_x_display.line_height = 1;
2569 the_only_x_display.font = (XFontStruct *)1; /* must *not* be zero */
2570 the_only_x_display.display_info.mouse_face_mouse_frame = NULL;
2571 the_only_x_display.display_info.mouse_face_deferred_gc = 0;
2572 the_only_x_display.display_info.mouse_face_beg_row =
2573 the_only_x_display.display_info.mouse_face_beg_col = -1;
2574 the_only_x_display.display_info.mouse_face_end_row =
2575 the_only_x_display.display_info.mouse_face_end_col = -1;
2576 the_only_x_display.display_info.mouse_face_face_id = DEFAULT_FACE_ID;
2577 the_only_x_display.display_info.mouse_face_window = Qnil;
2578 the_only_x_display.display_info.mouse_face_mouse_x =
2579 the_only_x_display.display_info.mouse_face_mouse_y = 0;
2580 the_only_x_display.display_info.mouse_face_defer = 0;
2581 the_only_x_display.display_info.mouse_face_hidden = 0;
2582
2583 init_frame_faces (sf);
2584
2585 ring_bell_hook = IT_ring_bell;
2586 insert_glyphs_hook = IT_insert_glyphs;
2587 delete_glyphs_hook = IT_delete_glyphs;
2588 write_glyphs_hook = IT_write_glyphs;
2589 cursor_to_hook = raw_cursor_to_hook = IT_cursor_to;
2590 clear_to_end_hook = IT_clear_to_end;
2591 clear_end_of_line_hook = IT_clear_end_of_line;
2592 clear_frame_hook = IT_clear_screen;
2593 update_begin_hook = IT_update_begin;
2594 update_end_hook = IT_update_end;
2595 frame_up_to_date_hook = IT_frame_up_to_date;
2596
2597 /* These hooks are called by term.c without being checked. */
2598 set_terminal_modes_hook = IT_set_terminal_modes;
2599 reset_terminal_modes_hook = IT_reset_terminal_modes;
2600 set_terminal_window_hook = IT_set_terminal_window;
2601 char_ins_del_ok = 0;
2602 #endif
2603 }
2604
2605 dos_get_saved_screen (screen, rows, cols)
2606 char **screen;
2607 int *rows;
2608 int *cols;
2609 {
2610 #ifndef HAVE_X_WINDOWS
2611 *screen = startup_screen_buffer;
2612 *cols = startup_screen_size_X;
2613 *rows = startup_screen_size_Y;
2614 return *screen != (char *)0;
2615 #else
2616 return 0;
2617 #endif
2618 }
2619
2620 #ifndef HAVE_X_WINDOWS
2621
2622 /* We are not X, but we can emulate it well enough for our needs... */
2623 void
2624 check_x (void)
2625 {
2626 if (! FRAME_MSDOS_P (SELECTED_FRAME()))
2627 error ("Not running under a window system");
2628 }
2629
2630 #endif
2631
2632 \f
2633 /* ----------------------- Keyboard control ----------------------
2634 *
2635 * Keymaps reflect the following keyboard layout:
2636 *
2637 * 0 1 2 3 4 5 6 7 8 9 10 11 12 BS
2638 * TAB 15 16 17 18 19 20 21 22 23 24 25 26 (41)
2639 * CLOK 30 31 32 33 34 35 36 37 38 39 40 (41) RET
2640 * SH () 45 46 47 48 49 50 51 52 53 54 SHIFT
2641 * SPACE
2642 */
2643
2644 #define Ignore 0x0000
2645 #define Normal 0x0000 /* normal key - alt changes scan-code */
2646 #define FctKey 0x1000 /* func key if c == 0, else c */
2647 #define Special 0x2000 /* func key even if c != 0 */
2648 #define ModFct 0x3000 /* special if mod-keys, else 'c' */
2649 #define Map 0x4000 /* alt scan-code, map to unshift/shift key */
2650 #define KeyPad 0x5000 /* map to insert/kp-0 depending on c == 0xe0 */
2651 #define Grey 0x6000 /* Grey keypad key */
2652
2653 #define Alt 0x0100 /* alt scan-code */
2654 #define Ctrl 0x0200 /* ctrl scan-code */
2655 #define Shift 0x0400 /* shift scan-code */
2656
2657 static int extended_kbd; /* 101 (102) keyboard present. */
2658
2659 struct kbd_translate {
2660 unsigned char sc;
2661 unsigned char ch;
2662 unsigned short code;
2663 };
2664
2665 struct dos_keyboard_map
2666 {
2667 char *unshifted;
2668 char *shifted;
2669 char *alt_gr;
2670 struct kbd_translate *translate_table;
2671 };
2672
2673
2674 static struct dos_keyboard_map us_keyboard = {
2675 /* 0 1 2 3 4 5 */
2676 /* 01234567890123456789012345678901234567890 12345678901234 */
2677 "`1234567890-= qwertyuiop[] asdfghjkl;'\\ zxcvbnm,./ ",
2678 /* 0123456789012345678901234567890123456789 012345678901234 */
2679 "~!@#$%^&*()_+ QWERTYUIOP{} ASDFGHJKL:\"| ZXCVBNM<>? ",
2680 0, /* no Alt-Gr key */
2681 0 /* no translate table */
2682 };
2683
2684 static struct dos_keyboard_map fr_keyboard = {
2685 /* 0 1 2 3 4 5 */
2686 /* 012 3456789012345678901234567890123456789012345678901234 */
2687 "ý&\82\",(-\8a_\80\85)= azertyuiop^$ qsdfghjklm\97* wxcvbnm;:! ",
2688 /* 0123456789012345678901234567890123456789012345678901234 */
2689 " 1234567890ø+ AZERTYUIOPù\9c QSDFGHJKLM%æ WXCVBN?./õ ",
2690 /* 01234567 89012345678901234567890123456789012345678901234 */
2691 " ~#{[|`\\^@]} Ï ",
2692 0 /* no translate table */
2693 };
2694
2695 /*
2696 * Italian keyboard support, country code 39.
2697 * '<' 56:3c*0000
2698 * '>' 56:3e*0000
2699 * added also {,},` as, respectively, AltGr-8, AltGr-9, AltGr-'
2700 * Donated by Stefano Brozzi <brozzis@mag00.cedi.unipr.it>
2701 */
2702
2703 static struct kbd_translate it_kbd_translate_table[] = {
2704 { 0x56, 0x3c, Normal | 13 },
2705 { 0x56, 0x3e, Normal | 27 },
2706 { 0, 0, 0 }
2707 };
2708 static struct dos_keyboard_map it_keyboard = {
2709 /* 0 1 2 3 4 5 */
2710 /* 0 123456789012345678901234567890123456789012345678901234 */
2711 "\\1234567890'\8d< qwertyuiop\8a+> asdfghjkl\95\85\97 zxcvbnm,.- ",
2712 /* 01 23456789012345678901234567890123456789012345678901234 */
2713 "|!\"\9c$%&/()=?^> QWERTYUIOP\82* ASDFGHJKL\87øõ ZXCVBNM;:_ ",
2714 /* 0123456789012345678901234567890123456789012345678901234 */
2715 " {}~` [] @# ",
2716 it_kbd_translate_table
2717 };
2718
2719 static struct dos_keyboard_map dk_keyboard = {
2720 /* 0 1 2 3 4 5 */
2721 /* 0123456789012345678901234567890123456789012345678901234 */
2722 "«1234567890+| qwertyuiop\86~ asdfghjkl\91\9b' zxcvbnm,.- ",
2723 /* 01 23456789012345678901234567890123456789012345678901234 */
2724 "õ!\"#$%&/()=?` QWERTYUIOP\8f^ ASDFGHJKL\92\9d* ZXCVBNM;:_ ",
2725 /* 0123456789012345678901234567890123456789012345678901234 */
2726 " @\9c$ {[]} | ",
2727 0 /* no translate table */
2728 };
2729
2730 static struct kbd_translate jp_kbd_translate_table[] = {
2731 { 0x73, 0x5c, Normal | 0 },
2732 { 0x73, 0x5f, Normal | 0 },
2733 { 0x73, 0x1c, Map | 0 },
2734 { 0x7d, 0x5c, Normal | 13 },
2735 { 0x7d, 0x7c, Normal | 13 },
2736 { 0x7d, 0x1c, Map | 13 },
2737 { 0, 0, 0 }
2738 };
2739 static struct dos_keyboard_map jp_keyboard = {
2740 /* 0 1 2 3 4 5 */
2741 /* 0123456789012 345678901234567890123456789012345678901234 */
2742 "\\1234567890-^\\ qwertyuiop@[ asdfghjkl;:] zxcvbnm,./ ",
2743 /* 01 23456789012345678901234567890123456789012345678901234 */
2744 "_!\"#$%&'()~=~| QWERTYUIOP`{ ASDFGHJKL+*} ZXCVBNM<>? ",
2745 0, /* no Alt-Gr key */
2746 jp_kbd_translate_table
2747 };
2748
2749 static struct keyboard_layout_list
2750 {
2751 int country_code;
2752 struct dos_keyboard_map *keyboard_map;
2753 } keyboard_layout_list[] =
2754 {
2755 1, &us_keyboard,
2756 33, &fr_keyboard,
2757 39, &it_keyboard,
2758 45, &dk_keyboard,
2759 81, &jp_keyboard
2760 };
2761
2762 static struct dos_keyboard_map *keyboard;
2763 static int keyboard_map_all;
2764 static int international_keyboard;
2765
2766 int
2767 dos_set_keyboard (code, always)
2768 int code;
2769 int always;
2770 {
2771 int i;
2772 _go32_dpmi_registers regs;
2773
2774 /* See if Keyb.Com is installed (for international keyboard support).
2775 Note: calling Int 2Fh via int86 wedges the DOS box on some versions
2776 of Windows 9X! So don't do that! */
2777 regs.x.ax = 0xad80;
2778 regs.x.ss = regs.x.sp = regs.x.flags = 0;
2779 _go32_dpmi_simulate_int (0x2f, &regs);
2780 if (regs.h.al == 0xff)
2781 international_keyboard = 1;
2782
2783 /* Initialize to US settings, for countries that don't have their own. */
2784 keyboard = keyboard_layout_list[0].keyboard_map;
2785 keyboard_map_all = always;
2786 dos_keyboard_layout = 1;
2787
2788 for (i = 0; i < (sizeof (keyboard_layout_list)/sizeof (struct keyboard_layout_list)); i++)
2789 if (code == keyboard_layout_list[i].country_code)
2790 {
2791 keyboard = keyboard_layout_list[i].keyboard_map;
2792 keyboard_map_all = always;
2793 dos_keyboard_layout = code;
2794 return 1;
2795 }
2796 return 0;
2797 }
2798 \f
2799 static struct
2800 {
2801 unsigned char char_code; /* normal code */
2802 unsigned char meta_code; /* M- code */
2803 unsigned char keypad_code; /* keypad code */
2804 unsigned char editkey_code; /* edit key */
2805 } keypad_translate_map[] = {
2806 '0', '0', 0xb0, /* kp-0 */ 0x63, /* insert */
2807 '1', '1', 0xb1, /* kp-1 */ 0x57, /* end */
2808 '2', '2', 0xb2, /* kp-2 */ 0x54, /* down */
2809 '3', '3', 0xb3, /* kp-3 */ 0x56, /* next */
2810 '4', '4', 0xb4, /* kp-4 */ 0x51, /* left */
2811 '5', '5', 0xb5, /* kp-5 */ 0xb5, /* kp-5 */
2812 '6', '6', 0xb6, /* kp-6 */ 0x53, /* right */
2813 '7', '7', 0xb7, /* kp-7 */ 0x50, /* home */
2814 '8', '8', 0xb8, /* kp-8 */ 0x52, /* up */
2815 '9', '9', 0xb9, /* kp-9 */ 0x55, /* prior */
2816 '.', '-', 0xae, /* kp-decimal */ 0xff /* delete */
2817 };
2818
2819 static struct
2820 {
2821 unsigned char char_code; /* normal code */
2822 unsigned char keypad_code; /* keypad code */
2823 } grey_key_translate_map[] = {
2824 '/', 0xaf, /* kp-decimal */
2825 '*', 0xaa, /* kp-multiply */
2826 '-', 0xad, /* kp-subtract */
2827 '+', 0xab, /* kp-add */
2828 '\r', 0x8d /* kp-enter */
2829 };
2830
2831 static unsigned short
2832 ibmpc_translate_map[] =
2833 {
2834 /* --------------- 00 to 0f --------------- */
2835 Normal | 0xff, /* Ctrl Break + Alt-NNN */
2836 Alt | ModFct | 0x1b, /* Escape */
2837 Normal | 1, /* '1' */
2838 Normal | 2, /* '2' */
2839 Normal | 3, /* '3' */
2840 Normal | 4, /* '4' */
2841 Normal | 5, /* '5' */
2842 Normal | 6, /* '6' */
2843 Normal | 7, /* '7' */
2844 Normal | 8, /* '8' */
2845 Normal | 9, /* '9' */
2846 Normal | 10, /* '0' */
2847 Normal | 11, /* '-' */
2848 Normal | 12, /* '=' */
2849 Special | 0x08, /* Backspace */
2850 ModFct | 0x74, /* Tab/Backtab */
2851
2852 /* --------------- 10 to 1f --------------- */
2853 Map | 15, /* 'q' */
2854 Map | 16, /* 'w' */
2855 Map | 17, /* 'e' */
2856 Map | 18, /* 'r' */
2857 Map | 19, /* 't' */
2858 Map | 20, /* 'y' */
2859 Map | 21, /* 'u' */
2860 Map | 22, /* 'i' */
2861 Map | 23, /* 'o' */
2862 Map | 24, /* 'p' */
2863 Map | 25, /* '[' */
2864 Map | 26, /* ']' */
2865 ModFct | 0x0d, /* Return */
2866 Ignore, /* Ctrl */
2867 Map | 30, /* 'a' */
2868 Map | 31, /* 's' */
2869
2870 /* --------------- 20 to 2f --------------- */
2871 Map | 32, /* 'd' */
2872 Map | 33, /* 'f' */
2873 Map | 34, /* 'g' */
2874 Map | 35, /* 'h' */
2875 Map | 36, /* 'j' */
2876 Map | 37, /* 'k' */
2877 Map | 38, /* 'l' */
2878 Map | 39, /* ';' */
2879 Map | 40, /* '\'' */
2880 Map | 0, /* '`' */
2881 Ignore, /* Left shift */
2882 Map | 41, /* '\\' */
2883 Map | 45, /* 'z' */
2884 Map | 46, /* 'x' */
2885 Map | 47, /* 'c' */
2886 Map | 48, /* 'v' */
2887
2888 /* --------------- 30 to 3f --------------- */
2889 Map | 49, /* 'b' */
2890 Map | 50, /* 'n' */
2891 Map | 51, /* 'm' */
2892 Map | 52, /* ',' */
2893 Map | 53, /* '.' */
2894 Map | 54, /* '/' */
2895 Ignore, /* Right shift */
2896 Grey | 1, /* Grey * */
2897 Ignore, /* Alt */
2898 Normal | 55, /* ' ' */
2899 Ignore, /* Caps Lock */
2900 FctKey | 0xbe, /* F1 */
2901 FctKey | 0xbf, /* F2 */
2902 FctKey | 0xc0, /* F3 */
2903 FctKey | 0xc1, /* F4 */
2904 FctKey | 0xc2, /* F5 */
2905
2906 /* --------------- 40 to 4f --------------- */
2907 FctKey | 0xc3, /* F6 */
2908 FctKey | 0xc4, /* F7 */
2909 FctKey | 0xc5, /* F8 */
2910 FctKey | 0xc6, /* F9 */
2911 FctKey | 0xc7, /* F10 */
2912 Ignore, /* Num Lock */
2913 Ignore, /* Scroll Lock */
2914 KeyPad | 7, /* Home */
2915 KeyPad | 8, /* Up */
2916 KeyPad | 9, /* Page Up */
2917 Grey | 2, /* Grey - */
2918 KeyPad | 4, /* Left */
2919 KeyPad | 5, /* Keypad 5 */
2920 KeyPad | 6, /* Right */
2921 Grey | 3, /* Grey + */
2922 KeyPad | 1, /* End */
2923
2924 /* --------------- 50 to 5f --------------- */
2925 KeyPad | 2, /* Down */
2926 KeyPad | 3, /* Page Down */
2927 KeyPad | 0, /* Insert */
2928 KeyPad | 10, /* Delete */
2929 Shift | FctKey | 0xbe, /* (Shift) F1 */
2930 Shift | FctKey | 0xbf, /* (Shift) F2 */
2931 Shift | FctKey | 0xc0, /* (Shift) F3 */
2932 Shift | FctKey | 0xc1, /* (Shift) F4 */
2933 Shift | FctKey | 0xc2, /* (Shift) F5 */
2934 Shift | FctKey | 0xc3, /* (Shift) F6 */
2935 Shift | FctKey | 0xc4, /* (Shift) F7 */
2936 Shift | FctKey | 0xc5, /* (Shift) F8 */
2937 Shift | FctKey | 0xc6, /* (Shift) F9 */
2938 Shift | FctKey | 0xc7, /* (Shift) F10 */
2939 Ctrl | FctKey | 0xbe, /* (Ctrl) F1 */
2940 Ctrl | FctKey | 0xbf, /* (Ctrl) F2 */
2941
2942 /* --------------- 60 to 6f --------------- */
2943 Ctrl | FctKey | 0xc0, /* (Ctrl) F3 */
2944 Ctrl | FctKey | 0xc1, /* (Ctrl) F4 */
2945 Ctrl | FctKey | 0xc2, /* (Ctrl) F5 */
2946 Ctrl | FctKey | 0xc3, /* (Ctrl) F6 */
2947 Ctrl | FctKey | 0xc4, /* (Ctrl) F7 */
2948 Ctrl | FctKey | 0xc5, /* (Ctrl) F8 */
2949 Ctrl | FctKey | 0xc6, /* (Ctrl) F9 */
2950 Ctrl | FctKey | 0xc7, /* (Ctrl) F10 */
2951 Alt | FctKey | 0xbe, /* (Alt) F1 */
2952 Alt | FctKey | 0xbf, /* (Alt) F2 */
2953 Alt | FctKey | 0xc0, /* (Alt) F3 */
2954 Alt | FctKey | 0xc1, /* (Alt) F4 */
2955 Alt | FctKey | 0xc2, /* (Alt) F5 */
2956 Alt | FctKey | 0xc3, /* (Alt) F6 */
2957 Alt | FctKey | 0xc4, /* (Alt) F7 */
2958 Alt | FctKey | 0xc5, /* (Alt) F8 */
2959
2960 /* --------------- 70 to 7f --------------- */
2961 Alt | FctKey | 0xc6, /* (Alt) F9 */
2962 Alt | FctKey | 0xc7, /* (Alt) F10 */
2963 Ctrl | FctKey | 0x6d, /* (Ctrl) Sys Rq */
2964 Ctrl | KeyPad | 4, /* (Ctrl) Left */
2965 Ctrl | KeyPad | 6, /* (Ctrl) Right */
2966 Ctrl | KeyPad | 1, /* (Ctrl) End */
2967 Ctrl | KeyPad | 3, /* (Ctrl) Page Down */
2968 Ctrl | KeyPad | 7, /* (Ctrl) Home */
2969 Alt | Map | 1, /* '1' */
2970 Alt | Map | 2, /* '2' */
2971 Alt | Map | 3, /* '3' */
2972 Alt | Map | 4, /* '4' */
2973 Alt | Map | 5, /* '5' */
2974 Alt | Map | 6, /* '6' */
2975 Alt | Map | 7, /* '7' */
2976 Alt | Map | 8, /* '8' */
2977
2978 /* --------------- 80 to 8f --------------- */
2979 Alt | Map | 9, /* '9' */
2980 Alt | Map | 10, /* '0' */
2981 Alt | Map | 11, /* '-' */
2982 Alt | Map | 12, /* '=' */
2983 Ctrl | KeyPad | 9, /* (Ctrl) Page Up */
2984 FctKey | 0xc8, /* F11 */
2985 FctKey | 0xc9, /* F12 */
2986 Shift | FctKey | 0xc8, /* (Shift) F11 */
2987 Shift | FctKey | 0xc9, /* (Shift) F12 */
2988 Ctrl | FctKey | 0xc8, /* (Ctrl) F11 */
2989 Ctrl | FctKey | 0xc9, /* (Ctrl) F12 */
2990 Alt | FctKey | 0xc8, /* (Alt) F11 */
2991 Alt | FctKey | 0xc9, /* (Alt) F12 */
2992 Ctrl | KeyPad | 8, /* (Ctrl) Up */
2993 Ctrl | Grey | 2, /* (Ctrl) Grey - */
2994 Ctrl | KeyPad | 5, /* (Ctrl) Keypad 5 */
2995
2996 /* --------------- 90 to 9f --------------- */
2997 Ctrl | Grey | 3, /* (Ctrl) Grey + */
2998 Ctrl | KeyPad | 2, /* (Ctrl) Down */
2999 Ctrl | KeyPad | 0, /* (Ctrl) Insert */
3000 Ctrl | KeyPad | 10, /* (Ctrl) Delete */
3001 Ctrl | FctKey | 0x09, /* (Ctrl) Tab */
3002 Ctrl | Grey | 0, /* (Ctrl) Grey / */
3003 Ctrl | Grey | 1, /* (Ctrl) Grey * */
3004 Alt | FctKey | 0x50, /* (Alt) Home */
3005 Alt | FctKey | 0x52, /* (Alt) Up */
3006 Alt | FctKey | 0x55, /* (Alt) Page Up */
3007 Ignore, /* NO KEY */
3008 Alt | FctKey | 0x51, /* (Alt) Left */
3009 Ignore, /* NO KEY */
3010 Alt | FctKey | 0x53, /* (Alt) Right */
3011 Ignore, /* NO KEY */
3012 Alt | FctKey | 0x57, /* (Alt) End */
3013
3014 /* --------------- a0 to af --------------- */
3015 Alt | KeyPad | 2, /* (Alt) Down */
3016 Alt | KeyPad | 3, /* (Alt) Page Down */
3017 Alt | KeyPad | 0, /* (Alt) Insert */
3018 Alt | KeyPad | 10, /* (Alt) Delete */
3019 Alt | Grey | 0, /* (Alt) Grey / */
3020 Alt | FctKey | 0x09, /* (Alt) Tab */
3021 Alt | Grey | 4 /* (Alt) Keypad Enter */
3022 };
3023 \f
3024 /* These bit-positions corresponds to values returned by BIOS */
3025 #define SHIFT_P 0x0003 /* two bits! */
3026 #define CTRL_P 0x0004
3027 #define ALT_P 0x0008
3028 #define SCRLOCK_P 0x0010
3029 #define NUMLOCK_P 0x0020
3030 #define CAPSLOCK_P 0x0040
3031 #define ALT_GR_P 0x0800
3032 #define SUPER_P 0x4000 /* pseudo */
3033 #define HYPER_P 0x8000 /* pseudo */
3034
3035 static int
3036 dos_get_modifiers (keymask)
3037 int *keymask;
3038 {
3039 union REGS regs;
3040 int mask, modifiers = 0;
3041
3042 /* Calculate modifier bits */
3043 regs.h.ah = extended_kbd ? 0x12 : 0x02;
3044 int86 (0x16, &regs, &regs);
3045
3046 if (!extended_kbd)
3047 {
3048 mask = regs.h.al & (SHIFT_P | CTRL_P | ALT_P |
3049 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
3050 }
3051 else
3052 {
3053 mask = regs.h.al & (SHIFT_P |
3054 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
3055
3056 /* Do not break international keyboard support. */
3057 /* When Keyb.Com is loaded, the right Alt key is */
3058 /* used for accessing characters like { and } */
3059 if (regs.h.ah & 2) /* Left ALT pressed ? */
3060 mask |= ALT_P;
3061
3062 if ((regs.h.ah & 8) != 0) /* Right ALT pressed ? */
3063 {
3064 mask |= ALT_GR_P;
3065 if (dos_hyper_key == 1)
3066 {
3067 mask |= HYPER_P;
3068 modifiers |= hyper_modifier;
3069 }
3070 else if (dos_super_key == 1)
3071 {
3072 mask |= SUPER_P;
3073 modifiers |= super_modifier;
3074 }
3075 else if (!international_keyboard)
3076 {
3077 /* If Keyb.Com is NOT installed, let Right Alt behave
3078 like the Left Alt. */
3079 mask &= ~ALT_GR_P;
3080 mask |= ALT_P;
3081 }
3082 }
3083
3084 if (regs.h.ah & 1) /* Left CTRL pressed ? */
3085 mask |= CTRL_P;
3086
3087 if (regs.h.ah & 4) /* Right CTRL pressed ? */
3088 {
3089 if (dos_hyper_key == 2)
3090 {
3091 mask |= HYPER_P;
3092 modifiers |= hyper_modifier;
3093 }
3094 else if (dos_super_key == 2)
3095 {
3096 mask |= SUPER_P;
3097 modifiers |= super_modifier;
3098 }
3099 else
3100 mask |= CTRL_P;
3101 }
3102 }
3103
3104 if (mask & SHIFT_P)
3105 modifiers |= shift_modifier;
3106 if (mask & CTRL_P)
3107 modifiers |= ctrl_modifier;
3108 if (mask & ALT_P)
3109 modifiers |= meta_modifier;
3110
3111 if (keymask)
3112 *keymask = mask;
3113 return modifiers;
3114 }
3115
3116 #define NUM_RECENT_DOSKEYS (100)
3117 int recent_doskeys_index; /* Index for storing next element into recent_doskeys */
3118 int total_doskeys; /* Total number of elements stored into recent_doskeys */
3119 Lisp_Object recent_doskeys; /* A vector, holding the last 100 keystrokes */
3120
3121 DEFUN ("recent-doskeys", Frecent_doskeys, Srecent_doskeys, 0, 0, 0,
3122 doc: /* Return vector of last 100 keyboard input values seen in dos_rawgetc.
3123 Each input key receives two values in this vector: first the ASCII code,
3124 and then the scan code. */)
3125 ()
3126 {
3127 Lisp_Object val, *keys = XVECTOR (recent_doskeys)->contents;
3128
3129 if (total_doskeys < NUM_RECENT_DOSKEYS)
3130 return Fvector (total_doskeys, keys);
3131 else
3132 {
3133 val = Fvector (NUM_RECENT_DOSKEYS, keys);
3134 bcopy (keys + recent_doskeys_index,
3135 XVECTOR (val)->contents,
3136 (NUM_RECENT_DOSKEYS - recent_doskeys_index) * sizeof (Lisp_Object));
3137 bcopy (keys,
3138 XVECTOR (val)->contents + NUM_RECENT_DOSKEYS - recent_doskeys_index,
3139 recent_doskeys_index * sizeof (Lisp_Object));
3140 return val;
3141 }
3142 }
3143
3144 /* Get a char from keyboard. Function keys are put into the event queue. */
3145 static int
3146 dos_rawgetc ()
3147 {
3148 struct input_event event;
3149 union REGS regs;
3150 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (SELECTED_FRAME());
3151
3152 #ifndef HAVE_X_WINDOWS
3153 /* Maybe put the cursor where it should be. */
3154 IT_cmgoto (SELECTED_FRAME());
3155 #endif
3156
3157 /* The following condition is equivalent to `kbhit ()', except that
3158 it uses the bios to do its job. This pleases DESQview/X. */
3159 while ((regs.h.ah = extended_kbd ? 0x11 : 0x01),
3160 int86 (0x16, &regs, &regs),
3161 (regs.x.flags & 0x40) == 0)
3162 {
3163 union REGS regs;
3164 register unsigned char c;
3165 int modifiers, sc, code = -1, mask, kp_mode;
3166
3167 regs.h.ah = extended_kbd ? 0x10 : 0x00;
3168 int86 (0x16, &regs, &regs);
3169 c = regs.h.al;
3170 sc = regs.h.ah;
3171
3172 total_doskeys += 2;
3173 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
3174 = make_number (c);
3175 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
3176 recent_doskeys_index = 0;
3177 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
3178 = make_number (sc);
3179 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
3180 recent_doskeys_index = 0;
3181
3182 modifiers = dos_get_modifiers (&mask);
3183
3184 #ifndef HAVE_X_WINDOWS
3185 if (!NILP (Vdos_display_scancodes))
3186 {
3187 char buf[11];
3188 sprintf (buf, "%02x:%02x*%04x",
3189 (unsigned) (sc&0xff), (unsigned) c, mask);
3190 dos_direct_output (screen_size_Y - 2, screen_size_X - 12, buf, 10);
3191 }
3192 #endif
3193
3194 if (sc == 0xe0)
3195 {
3196 switch (c)
3197 {
3198 case 10: /* Ctrl Grey Enter */
3199 code = Ctrl | Grey | 4;
3200 break;
3201 case 13: /* Grey Enter */
3202 code = Grey | 4;
3203 break;
3204 case '/': /* Grey / */
3205 code = Grey | 0;
3206 break;
3207 default:
3208 continue;
3209 };
3210 c = 0;
3211 }
3212 else
3213 {
3214 /* Try the keyboard-private translation table first. */
3215 if (keyboard->translate_table)
3216 {
3217 struct kbd_translate *p = keyboard->translate_table;
3218
3219 while (p->sc)
3220 {
3221 if (p->sc == sc && p->ch == c)
3222 {
3223 code = p->code;
3224 break;
3225 }
3226 p++;
3227 }
3228 }
3229 /* If the private table didn't translate it, use the general
3230 one. */
3231 if (code == -1)
3232 {
3233 if (sc >= (sizeof (ibmpc_translate_map) / sizeof (short)))
3234 continue;
3235 if ((code = ibmpc_translate_map[sc]) == Ignore)
3236 continue;
3237 }
3238 }
3239
3240 if (c == 0)
3241 {
3242 /* We only look at the keyboard Ctrl/Shift/Alt keys when
3243 Emacs is ready to read a key. Therefore, if they press
3244 `Alt-x' when Emacs is busy, by the time we get to
3245 `dos_get_modifiers', they might have already released the
3246 Alt key, and Emacs gets just `x', which is BAD.
3247 However, for keys with the `Map' property set, the ASCII
3248 code returns zero iff Alt is pressed. So, when we DON'T
3249 have to support international_keyboard, we don't have to
3250 distinguish between the left and right Alt keys, and we
3251 can set the META modifier for any keys with the `Map'
3252 property if they return zero ASCII code (c = 0). */
3253 if ( (code & Alt)
3254 || ( (code & 0xf000) == Map && !international_keyboard))
3255 modifiers |= meta_modifier;
3256 if (code & Ctrl)
3257 modifiers |= ctrl_modifier;
3258 if (code & Shift)
3259 modifiers |= shift_modifier;
3260 }
3261
3262 switch (code & 0xf000)
3263 {
3264 case ModFct:
3265 if (c && !(mask & (SHIFT_P | ALT_P | CTRL_P | HYPER_P | SUPER_P)))
3266 return c;
3267 c = 0; /* Special */
3268
3269 case FctKey:
3270 if (c != 0)
3271 return c;
3272
3273 case Special:
3274 code |= 0xff00;
3275 break;
3276
3277 case Normal:
3278 if (sc == 0)
3279 {
3280 if (c == 0) /* ctrl-break */
3281 continue;
3282 return c; /* ALT-nnn */
3283 }
3284 if (!keyboard_map_all)
3285 {
3286 if (c != ' ')
3287 return c;
3288 code = c;
3289 break;
3290 }
3291
3292 case Map:
3293 if (c && !(mask & ALT_P) && !((mask & SHIFT_P) && (mask & CTRL_P)))
3294 if (!keyboard_map_all)
3295 return c;
3296
3297 code &= 0xff;
3298 if (mask & ALT_P && code <= 10 && code > 0 && dos_keypad_mode & 0x200)
3299 mask |= SHIFT_P; /* ALT-1 => M-! etc. */
3300
3301 if (mask & SHIFT_P)
3302 {
3303 code = keyboard->shifted[code];
3304 mask -= SHIFT_P;
3305 modifiers &= ~shift_modifier;
3306 }
3307 else
3308 if ((mask & ALT_GR_P) && keyboard->alt_gr && keyboard->alt_gr[code] != ' ')
3309 code = keyboard->alt_gr[code];
3310 else
3311 code = keyboard->unshifted[code];
3312 break;
3313
3314 case KeyPad:
3315 code &= 0xff;
3316 if (c == 0xe0) /* edit key */
3317 kp_mode = 3;
3318 else
3319 if ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) /* numlock on */
3320 kp_mode = dos_keypad_mode & 0x03;
3321 else
3322 kp_mode = (dos_keypad_mode >> 4) & 0x03;
3323
3324 switch (kp_mode)
3325 {
3326 case 0:
3327 if (code == 10 && dos_decimal_point)
3328 return dos_decimal_point;
3329 return keypad_translate_map[code].char_code;
3330
3331 case 1:
3332 code = 0xff00 | keypad_translate_map[code].keypad_code;
3333 break;
3334
3335 case 2:
3336 code = keypad_translate_map[code].meta_code;
3337 modifiers = meta_modifier;
3338 break;
3339
3340 case 3:
3341 code = 0xff00 | keypad_translate_map[code].editkey_code;
3342 break;
3343 }
3344 break;
3345
3346 case Grey:
3347 code &= 0xff;
3348 kp_mode = ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) ? 0x04 : 0x40;
3349 if (dos_keypad_mode & kp_mode)
3350 code = 0xff00 | grey_key_translate_map[code].keypad_code;
3351 else
3352 code = grey_key_translate_map[code].char_code;
3353 break;
3354 }
3355
3356 make_event:
3357 if (code == 0)
3358 continue;
3359
3360 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
3361 {
3362 dpyinfo->mouse_face_hidden = 1;
3363 clear_mouse_face (dpyinfo);
3364 }
3365
3366 if (code >= 0x100)
3367 event.kind = NON_ASCII_KEYSTROKE_EVENT;
3368 else
3369 event.kind = ASCII_KEYSTROKE_EVENT;
3370 event.code = code;
3371 event.modifiers = modifiers;
3372 event.frame_or_window = selected_frame;
3373 event.arg = Qnil;
3374 event.timestamp = event_timestamp ();
3375 kbd_buffer_store_event (&event);
3376 }
3377
3378 if (have_mouse > 0 && !mouse_preempted)
3379 {
3380 int but, press, x, y, ok;
3381 int mouse_prev_x = mouse_last_x, mouse_prev_y = mouse_last_y;
3382 Lisp_Object mouse_window = Qnil;
3383
3384 /* Check for mouse movement *before* buttons. */
3385 mouse_check_moved ();
3386
3387 /* If the mouse moved from the spot of its last sighting, we
3388 might need to update mouse highlight. */
3389 if (mouse_last_x != mouse_prev_x || mouse_last_y != mouse_prev_y)
3390 {
3391 if (dpyinfo->mouse_face_hidden)
3392 {
3393 dpyinfo->mouse_face_hidden = 0;
3394 clear_mouse_face (dpyinfo);
3395 }
3396
3397 /* Generate SELECT_WINDOW_EVENTs when needed. */
3398 if (mouse_autoselect_window)
3399 {
3400 int mouse_area;
3401
3402 mouse_window = window_from_coordinates (SELECTED_FRAME(),
3403 mouse_last_x,
3404 mouse_last_y,
3405 &mouse_area, 0);
3406 /* A window will be selected only when it is not
3407 selected now, and the last mouse movement event was
3408 not in it. A minibuffer window will be selected iff
3409 it is active. */
3410 if (WINDOWP (mouse_window)
3411 && !EQ (mouse_window, last_mouse_window)
3412 && !EQ (mouse_window, selected_window))
3413 {
3414 event.kind = SELECT_WINDOW_EVENT;
3415 event.frame_or_window = mouse_window;
3416 event.arg = Qnil;
3417 event.timestamp = event_timestamp ();
3418 kbd_buffer_store_event (&event);
3419 }
3420 last_mouse_window = mouse_window;
3421 }
3422 else
3423 last_mouse_window = Qnil;
3424
3425 previous_help_echo = help_echo;
3426 help_echo = help_echo_object = help_echo_window = Qnil;
3427 help_echo_pos = -1;
3428 IT_note_mouse_highlight (SELECTED_FRAME(),
3429 mouse_last_x, mouse_last_y);
3430 /* If the contents of the global variable help_echo has
3431 changed, generate a HELP_EVENT. */
3432 if (!NILP (help_echo) || !NILP (previous_help_echo))
3433 {
3434 event.kind = HELP_EVENT;
3435 event.frame_or_window = selected_frame;
3436 event.arg = help_echo_object;
3437 event.x = WINDOWP (help_echo_window)
3438 ? help_echo_window : selected_frame;
3439 event.y = help_echo;
3440 event.timestamp = event_timestamp ();
3441 event.code = help_echo_pos;
3442 kbd_buffer_store_event (&event);
3443 }
3444 }
3445
3446 for (but = 0; but < NUM_MOUSE_BUTTONS; but++)
3447 for (press = 0; press < 2; press++)
3448 {
3449 int button_num = but;
3450
3451 if (press)
3452 ok = mouse_pressed (but, &x, &y);
3453 else
3454 ok = mouse_released (but, &x, &y);
3455 if (ok)
3456 {
3457 /* Allow a simultaneous press/release of Mouse-1 and
3458 Mouse-2 to simulate Mouse-3 on two-button mice. */
3459 if (mouse_button_count == 2 && but < 2)
3460 {
3461 int x2, y2; /* don't clobber original coordinates */
3462
3463 /* If only one button is pressed, wait 100 msec and
3464 check again. This way, Speedy Gonzales isn't
3465 punished, while the slow get their chance. */
3466 if (press && mouse_pressed (1-but, &x2, &y2)
3467 || !press && mouse_released (1-but, &x2, &y2))
3468 button_num = 2;
3469 else
3470 {
3471 delay (100);
3472 if (press && mouse_pressed (1-but, &x2, &y2)
3473 || !press && mouse_released (1-but, &x2, &y2))
3474 button_num = 2;
3475 }
3476 }
3477
3478 event.kind = MOUSE_CLICK_EVENT;
3479 event.code = button_num;
3480 event.modifiers = dos_get_modifiers (0)
3481 | (press ? down_modifier : up_modifier);
3482 event.x = x;
3483 event.y = y;
3484 event.frame_or_window = selected_frame;
3485 event.arg = Qnil;
3486 event.timestamp = event_timestamp ();
3487 kbd_buffer_store_event (&event);
3488 }
3489 }
3490 }
3491
3492 return -1;
3493 }
3494
3495 static int prev_get_char = -1;
3496
3497 /* Return 1 if a key is ready to be read without suspending execution. */
3498
3499 dos_keysns ()
3500 {
3501 if (prev_get_char != -1)
3502 return 1;
3503 else
3504 return ((prev_get_char = dos_rawgetc ()) != -1);
3505 }
3506
3507 /* Read a key. Return -1 if no key is ready. */
3508
3509 dos_keyread ()
3510 {
3511 if (prev_get_char != -1)
3512 {
3513 int c = prev_get_char;
3514 prev_get_char = -1;
3515 return c;
3516 }
3517 else
3518 return dos_rawgetc ();
3519 }
3520 \f
3521 #ifndef HAVE_X_WINDOWS
3522 /* See xterm.c for more info. */
3523 void
3524 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
3525 FRAME_PTR f;
3526 register int pix_x, pix_y, *x, *y;
3527 XRectangle *bounds;
3528 int noclip;
3529 {
3530 if (bounds) abort ();
3531
3532 /* Ignore clipping. */
3533
3534 *x = pix_x;
3535 *y = pix_y;
3536 }
3537
3538 void
3539 glyph_to_pixel_coords (f, x, y, pix_x, pix_y)
3540 FRAME_PTR f;
3541 register int x, y, *pix_x, *pix_y;
3542 {
3543 *pix_x = x;
3544 *pix_y = y;
3545 }
3546 \f
3547 /* Simulation of X's menus. Nothing too fancy here -- just make it work
3548 for now.
3549
3550 Actually, I don't know the meaning of all the parameters of the functions
3551 here -- I only know how they are called by xmenu.c. I could of course
3552 grab the nearest Xlib manual (down the hall, second-to-last door on the
3553 left), but I don't think it's worth the effort. */
3554
3555 /* These hold text of the current and the previous menu help messages. */
3556 static char *menu_help_message, *prev_menu_help_message;
3557 /* Pane number and item number of the menu item which generated the
3558 last menu help message. */
3559 static int menu_help_paneno, menu_help_itemno;
3560
3561 static XMenu *
3562 IT_menu_create ()
3563 {
3564 XMenu *menu;
3565
3566 menu = (XMenu *) xmalloc (sizeof (XMenu));
3567 menu->allocated = menu->count = menu->panecount = menu->width = 0;
3568 return menu;
3569 }
3570
3571 /* Allocate some (more) memory for MENU ensuring that there is room for one
3572 for item. */
3573
3574 static void
3575 IT_menu_make_room (XMenu *menu)
3576 {
3577 if (menu->allocated == 0)
3578 {
3579 int count = menu->allocated = 10;
3580 menu->text = (char **) xmalloc (count * sizeof (char *));
3581 menu->submenu = (XMenu **) xmalloc (count * sizeof (XMenu *));
3582 menu->panenumber = (int *) xmalloc (count * sizeof (int));
3583 menu->help_text = (char **) xmalloc (count * sizeof (char *));
3584 }
3585 else if (menu->allocated == menu->count)
3586 {
3587 int count = menu->allocated = menu->allocated + 10;
3588 menu->text
3589 = (char **) xrealloc (menu->text, count * sizeof (char *));
3590 menu->submenu
3591 = (XMenu **) xrealloc (menu->submenu, count * sizeof (XMenu *));
3592 menu->panenumber
3593 = (int *) xrealloc (menu->panenumber, count * sizeof (int));
3594 menu->help_text
3595 = (char **) xrealloc (menu->help_text, count * sizeof (char *));
3596 }
3597 }
3598
3599 /* Search the given menu structure for a given pane number. */
3600
3601 static XMenu *
3602 IT_menu_search_pane (XMenu *menu, int pane)
3603 {
3604 int i;
3605 XMenu *try;
3606
3607 for (i = 0; i < menu->count; i++)
3608 if (menu->submenu[i])
3609 {
3610 if (pane == menu->panenumber[i])
3611 return menu->submenu[i];
3612 if ((try = IT_menu_search_pane (menu->submenu[i], pane)))
3613 return try;
3614 }
3615 return (XMenu *) 0;
3616 }
3617
3618 /* Determine how much screen space a given menu needs. */
3619
3620 static void
3621 IT_menu_calc_size (XMenu *menu, int *width, int *height)
3622 {
3623 int i, h2, w2, maxsubwidth, maxheight;
3624
3625 maxsubwidth = 0;
3626 maxheight = menu->count;
3627 for (i = 0; i < menu->count; i++)
3628 {
3629 if (menu->submenu[i])
3630 {
3631 IT_menu_calc_size (menu->submenu[i], &w2, &h2);
3632 if (w2 > maxsubwidth) maxsubwidth = w2;
3633 if (i + h2 > maxheight) maxheight = i + h2;
3634 }
3635 }
3636 *width = menu->width + maxsubwidth;
3637 *height = maxheight;
3638 }
3639
3640 /* Display MENU at (X,Y) using FACES. */
3641
3642 static void
3643 IT_menu_display (XMenu *menu, int y, int x, int pn, int *faces, int disp_help)
3644 {
3645 int i, j, face, width, mx, my, enabled, mousehere, row, col;
3646 struct glyph *text, *p;
3647 char *q;
3648 struct frame *sf = SELECTED_FRAME();
3649
3650 menu_help_message = NULL;
3651
3652 width = menu->width;
3653 text = (struct glyph *) xmalloc ((width + 2) * sizeof (struct glyph));
3654 ScreenGetCursor (&row, &col);
3655 mouse_get_xy (&mx, &my);
3656 IT_update_begin (sf);
3657 for (i = 0; i < menu->count; i++)
3658 {
3659 int max_width = width + 2;
3660
3661 IT_cursor_to (y + i, x);
3662 enabled
3663 = (!menu->submenu[i] && menu->panenumber[i]) || (menu->submenu[i]);
3664 mousehere = (y + i == my && x <= mx && mx < x + width + 2);
3665 face = faces[enabled + mousehere * 2];
3666 /* The following if clause means that we display the menu help
3667 strings even if the menu item is currently disabled. */
3668 if (disp_help && enabled + mousehere * 2 >= 2)
3669 {
3670 menu_help_message = menu->help_text[i];
3671 menu_help_paneno = pn - 1;
3672 menu_help_itemno = i;
3673 }
3674 p = text;
3675 SET_CHAR_GLYPH (*p, ' ', face, 0);
3676 p++;
3677 for (j = 0, q = menu->text[i]; *q; j++)
3678 {
3679 if (*q > 26)
3680 {
3681 SET_CHAR_GLYPH (*p, *q++, face, 0);
3682 p++;
3683 }
3684 else /* make '^x' */
3685 {
3686 SET_CHAR_GLYPH (*p, '^', face, 0);
3687 p++;
3688 j++;
3689 SET_CHAR_GLYPH (*p, *q++ + 64, face, 0);
3690 p++;
3691 }
3692 }
3693 /* Don't let the menu text overflow into the next screen row. */
3694 if (x + max_width > screen_size_X)
3695 {
3696 max_width = screen_size_X - x;
3697 text[max_width - 1].u.ch = '$'; /* indicate it's truncated */
3698 }
3699 for (; j < max_width - 2; j++, p++)
3700 SET_CHAR_GLYPH (*p, ' ', face, 0);
3701
3702 SET_CHAR_GLYPH (*p, menu->submenu[i] ? 16 : ' ', face, 0);
3703 p++;
3704 IT_write_glyphs (text, max_width);
3705 }
3706 IT_update_end (sf);
3707 IT_cursor_to (row, col);
3708 xfree (text);
3709 }
3710 \f
3711 /* --------------------------- X Menu emulation ---------------------- */
3712
3713 /* Report availability of menus. */
3714
3715 int
3716 have_menus_p () { return 1; }
3717
3718 /* Create a brand new menu structure. */
3719
3720 XMenu *
3721 XMenuCreate (Display *foo1, Window foo2, char *foo3)
3722 {
3723 return IT_menu_create ();
3724 }
3725
3726 /* Create a new pane and place it on the outer-most level. It is not
3727 clear that it should be placed out there, but I don't know what else
3728 to do. */
3729
3730 int
3731 XMenuAddPane (Display *foo, XMenu *menu, char *txt, int enable)
3732 {
3733 int len;
3734 char *p;
3735
3736 if (!enable)
3737 abort ();
3738
3739 IT_menu_make_room (menu);
3740 menu->submenu[menu->count] = IT_menu_create ();
3741 menu->text[menu->count] = txt;
3742 menu->panenumber[menu->count] = ++menu->panecount;
3743 menu->help_text[menu->count] = NULL;
3744 menu->count++;
3745
3746 /* Adjust length for possible control characters (which will
3747 be written as ^x). */
3748 for (len = strlen (txt), p = txt; *p; p++)
3749 if (*p < 27)
3750 len++;
3751
3752 if (len > menu->width)
3753 menu->width = len;
3754
3755 return menu->panecount;
3756 }
3757
3758 /* Create a new item in a menu pane. */
3759
3760 int
3761 XMenuAddSelection (Display *bar, XMenu *menu, int pane,
3762 int foo, char *txt, int enable, char *help_text)
3763 {
3764 int len;
3765 char *p;
3766
3767 if (pane)
3768 if (!(menu = IT_menu_search_pane (menu, pane)))
3769 return XM_FAILURE;
3770 IT_menu_make_room (menu);
3771 menu->submenu[menu->count] = (XMenu *) 0;
3772 menu->text[menu->count] = txt;
3773 menu->panenumber[menu->count] = enable;
3774 menu->help_text[menu->count] = help_text;
3775 menu->count++;
3776
3777 /* Adjust length for possible control characters (which will
3778 be written as ^x). */
3779 for (len = strlen (txt), p = txt; *p; p++)
3780 if (*p < 27)
3781 len++;
3782
3783 if (len > menu->width)
3784 menu->width = len;
3785
3786 return XM_SUCCESS;
3787 }
3788
3789 /* Decide where the menu would be placed if requested at (X,Y). */
3790
3791 void
3792 XMenuLocate (Display *foo0, XMenu *menu, int foo1, int foo2, int x, int y,
3793 int *ulx, int *uly, int *width, int *height)
3794 {
3795 IT_menu_calc_size (menu, width, height);
3796 *ulx = x + 1;
3797 *uly = y;
3798 *width += 2;
3799 }
3800
3801 struct IT_menu_state
3802 {
3803 void *screen_behind;
3804 XMenu *menu;
3805 int pane;
3806 int x, y;
3807 };
3808
3809
3810 /* Display menu, wait for user's response, and return that response. */
3811
3812 int
3813 XMenuActivate (Display *foo, XMenu *menu, int *pane, int *selidx,
3814 int x0, int y0, unsigned ButtonMask, char **txt,
3815 void (*help_callback)(char *, int, int))
3816 {
3817 struct IT_menu_state *state;
3818 int statecount, x, y, i, b, screensize, leave, result, onepane;
3819 int title_faces[4]; /* face to display the menu title */
3820 int faces[4], buffers_num_deleted = 0;
3821 struct frame *sf = SELECTED_FRAME();
3822 Lisp_Object saved_echo_area_message, selectface;
3823
3824 /* Just in case we got here without a mouse present... */
3825 if (have_mouse <= 0)
3826 return XM_IA_SELECT;
3827 /* Don't allow non-positive x0 and y0, lest the menu will wrap
3828 around the display. */
3829 if (x0 <= 0)
3830 x0 = 1;
3831 if (y0 <= 0)
3832 y0 = 1;
3833
3834 /* We will process all the mouse events directly, so we had
3835 better prevent dos_rawgetc from stealing them from us. */
3836 mouse_preempted++;
3837
3838 state = alloca (menu->panecount * sizeof (struct IT_menu_state));
3839 screensize = screen_size * 2;
3840 faces[0]
3841 = lookup_derived_face (sf, intern ("msdos-menu-passive-face"),
3842 0, DEFAULT_FACE_ID);
3843 faces[1]
3844 = lookup_derived_face (sf, intern ("msdos-menu-active-face"),
3845 0, DEFAULT_FACE_ID);
3846 selectface = intern ("msdos-menu-select-face");
3847 faces[2] = lookup_derived_face (sf, selectface,
3848 0, faces[0]);
3849 faces[3] = lookup_derived_face (sf, selectface,
3850 0, faces[1]);
3851
3852 /* Make sure the menu title is always displayed with
3853 `msdos-menu-active-face', no matter where the mouse pointer is. */
3854 for (i = 0; i < 4; i++)
3855 title_faces[i] = faces[3];
3856
3857 statecount = 1;
3858
3859 /* Don't let the title for the "Buffers" popup menu include a
3860 digit (which is ugly).
3861
3862 This is a terrible kludge, but I think the "Buffers" case is
3863 the only one where the title includes a number, so it doesn't
3864 seem to be necessary to make this more general. */
3865 if (strncmp (menu->text[0], "Buffers 1", 9) == 0)
3866 {
3867 menu->text[0][7] = '\0';
3868 buffers_num_deleted = 1;
3869 }
3870
3871 /* We need to save the current echo area message, so that we could
3872 restore it below, before we exit. See the commentary below,
3873 before the call to message_with_string. */
3874 saved_echo_area_message = Fcurrent_message ();
3875 state[0].menu = menu;
3876 mouse_off ();
3877 ScreenRetrieve (state[0].screen_behind = xmalloc (screensize));
3878
3879 /* Turn off the cursor. Otherwise it shows through the menu
3880 panes, which is ugly. */
3881 IT_display_cursor (0);
3882
3883 /* Display the menu title. */
3884 IT_menu_display (menu, y0 - 1, x0 - 1, 1, title_faces, 0);
3885 if (buffers_num_deleted)
3886 menu->text[0][7] = ' ';
3887 if ((onepane = menu->count == 1 && menu->submenu[0]))
3888 {
3889 menu->width = menu->submenu[0]->width;
3890 state[0].menu = menu->submenu[0];
3891 }
3892 else
3893 {
3894 state[0].menu = menu;
3895 }
3896 state[0].x = x0 - 1;
3897 state[0].y = y0;
3898 state[0].pane = onepane;
3899
3900 mouse_last_x = -1; /* A hack that forces display. */
3901 leave = 0;
3902 while (!leave)
3903 {
3904 if (!mouse_visible) mouse_on ();
3905 mouse_check_moved ();
3906 if (sf->mouse_moved)
3907 {
3908 sf->mouse_moved = 0;
3909 result = XM_IA_SELECT;
3910 mouse_get_xy (&x, &y);
3911 for (i = 0; i < statecount; i++)
3912 if (state[i].x <= x && x < state[i].x + state[i].menu->width + 2)
3913 {
3914 int dy = y - state[i].y;
3915 if (0 <= dy && dy < state[i].menu->count)
3916 {
3917 if (!state[i].menu->submenu[dy])
3918 if (state[i].menu->panenumber[dy])
3919 result = XM_SUCCESS;
3920 else
3921 result = XM_IA_SELECT;
3922 *pane = state[i].pane - 1;
3923 *selidx = dy;
3924 /* We hit some part of a menu, so drop extra menus that
3925 have been opened. That does not include an open and
3926 active submenu. */
3927 if (i != statecount - 2
3928 || state[i].menu->submenu[dy] != state[i+1].menu)
3929 while (i != statecount - 1)
3930 {
3931 statecount--;
3932 mouse_off ();
3933 ScreenUpdate (state[statecount].screen_behind);
3934 if (screen_virtual_segment)
3935 dosv_refresh_virtual_screen (0, screen_size);
3936 xfree (state[statecount].screen_behind);
3937 }
3938 if (i == statecount - 1 && state[i].menu->submenu[dy])
3939 {
3940 IT_menu_display (state[i].menu,
3941 state[i].y,
3942 state[i].x,
3943 state[i].pane,
3944 faces, 1);
3945 state[statecount].menu = state[i].menu->submenu[dy];
3946 state[statecount].pane = state[i].menu->panenumber[dy];
3947 mouse_off ();
3948 ScreenRetrieve (state[statecount].screen_behind
3949 = xmalloc (screensize));
3950 state[statecount].x
3951 = state[i].x + state[i].menu->width + 2;
3952 state[statecount].y = y;
3953 statecount++;
3954 }
3955 }
3956 }
3957 IT_menu_display (state[statecount - 1].menu,
3958 state[statecount - 1].y,
3959 state[statecount - 1].x,
3960 state[statecount - 1].pane,
3961 faces, 1);
3962 }
3963 else
3964 {
3965 if ((menu_help_message || prev_menu_help_message)
3966 && menu_help_message != prev_menu_help_message)
3967 {
3968 help_callback (menu_help_message,
3969 menu_help_paneno, menu_help_itemno);
3970 IT_display_cursor (0);
3971 prev_menu_help_message = menu_help_message;
3972 }
3973 /* We are busy-waiting for the mouse to move, so let's be nice
3974 to other Windows applications by releasing our time slice. */
3975 __dpmi_yield ();
3976 }
3977 for (b = 0; b < mouse_button_count && !leave; b++)
3978 {
3979 /* Only leave if user both pressed and released the mouse, and in
3980 that order. This avoids popping down the menu pane unless
3981 the user is really done with it. */
3982 if (mouse_pressed (b, &x, &y))
3983 {
3984 while (mouse_button_depressed (b, &x, &y))
3985 __dpmi_yield ();
3986 leave = 1;
3987 }
3988 (void) mouse_released (b, &x, &y);
3989 }
3990 }
3991
3992 mouse_off ();
3993 ScreenUpdate (state[0].screen_behind);
3994 if (screen_virtual_segment)
3995 dosv_refresh_virtual_screen (0, screen_size);
3996
3997 /* We have a situation here. ScreenUpdate has just restored the
3998 screen contents as it was before we started drawing this menu.
3999 That includes any echo area message that could have been
4000 displayed back then. (In reality, that echo area message will
4001 almost always be the ``keystroke echo'' that echoes the sequence
4002 of menu items chosen by the user.) However, if the menu had some
4003 help messages, then displaying those messages caused Emacs to
4004 forget about the original echo area message. So when
4005 ScreenUpdate restored it, it created a discrepancy between the
4006 actual screen contents and what Emacs internal data structures
4007 know about it.
4008
4009 To avoid this conflict, we force Emacs to restore the original
4010 echo area message as we found it when we entered this function.
4011 The irony of this is that we then erase the restored message
4012 right away, so the only purpose of restoring it is so that
4013 erasing it works correctly... */
4014 if (! NILP (saved_echo_area_message))
4015 message_with_string ("%s", saved_echo_area_message, 0);
4016 message (0);
4017 while (statecount--)
4018 xfree (state[statecount].screen_behind);
4019 IT_display_cursor (1); /* turn cursor back on */
4020 /* Clean up any mouse events that are waiting inside Emacs event queue.
4021 These events are likely to be generated before the menu was even
4022 displayed, probably because the user pressed and released the button
4023 (which invoked the menu) too quickly. If we don't remove these events,
4024 Emacs will process them after we return and surprise the user. */
4025 discard_mouse_events ();
4026 mouse_clear_clicks ();
4027 if (!kbd_buffer_events_waiting (1))
4028 clear_input_pending ();
4029 /* Allow mouse events generation by dos_rawgetc. */
4030 mouse_preempted--;
4031 return result;
4032 }
4033
4034 /* Dispose of a menu. */
4035
4036 void
4037 XMenuDestroy (Display *foo, XMenu *menu)
4038 {
4039 int i;
4040 if (menu->allocated)
4041 {
4042 for (i = 0; i < menu->count; i++)
4043 if (menu->submenu[i])
4044 XMenuDestroy (foo, menu->submenu[i]);
4045 xfree (menu->text);
4046 xfree (menu->submenu);
4047 xfree (menu->panenumber);
4048 xfree (menu->help_text);
4049 }
4050 xfree (menu);
4051 menu_help_message = prev_menu_help_message = NULL;
4052 }
4053
4054 int
4055 x_pixel_width (struct frame *f)
4056 {
4057 return FRAME_WIDTH (f);
4058 }
4059
4060 int
4061 x_pixel_height (struct frame *f)
4062 {
4063 return FRAME_HEIGHT (f);
4064 }
4065 #endif /* !HAVE_X_WINDOWS */
4066 \f
4067 /* ----------------------- DOS / UNIX conversion --------------------- */
4068
4069 void msdos_downcase_filename (unsigned char *);
4070
4071 /* Destructively turn backslashes into slashes. */
4072
4073 void
4074 dostounix_filename (p)
4075 register char *p;
4076 {
4077 msdos_downcase_filename (p);
4078
4079 while (*p)
4080 {
4081 if (*p == '\\')
4082 *p = '/';
4083 p++;
4084 }
4085 }
4086
4087 /* Destructively turn slashes into backslashes. */
4088
4089 void
4090 unixtodos_filename (p)
4091 register char *p;
4092 {
4093 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
4094 {
4095 *p += 'a' - 'A';
4096 p += 2;
4097 }
4098
4099 while (*p)
4100 {
4101 if (*p == '/')
4102 *p = '\\';
4103 p++;
4104 }
4105 }
4106
4107 /* Get the default directory for a given drive. 0=def, 1=A, 2=B, ... */
4108
4109 int
4110 getdefdir (drive, dst)
4111 int drive;
4112 char *dst;
4113 {
4114 char in_path[4], *p = in_path, e = errno;;
4115
4116 /* Generate "X:." (when drive is X) or "." (when drive is 0). */
4117 if (drive != 0)
4118 {
4119 *p++ = drive + 'A' - 1;
4120 *p++ = ':';
4121 }
4122
4123 *p++ = '.';
4124 *p = '\0';
4125 errno = 0;
4126 _fixpath (in_path, dst);
4127 /* _fixpath can set errno to ENOSYS on non-LFN systems because
4128 it queries the LFN support, so ignore that error. */
4129 if ((errno && errno != ENOSYS) || *dst == '\0')
4130 return 0;
4131
4132 msdos_downcase_filename (dst);
4133
4134 errno = e;
4135 return 1;
4136 }
4137
4138 char *
4139 emacs_root_dir (void)
4140 {
4141 static char root_dir[4];
4142
4143 sprintf (root_dir, "%c:/", 'A' + getdisk ());
4144 root_dir[0] = tolower (root_dir[0]);
4145 return root_dir;
4146 }
4147
4148 /* Remove all CR's that are followed by a LF. */
4149
4150 int
4151 crlf_to_lf (n, buf)
4152 register int n;
4153 register unsigned char *buf;
4154 {
4155 unsigned char *np = buf, *startp = buf, *endp = buf + n;
4156
4157 if (n == 0)
4158 return n;
4159 while (buf < endp - 1)
4160 {
4161 if (*buf == 0x0d)
4162 {
4163 if (*(++buf) != 0x0a)
4164 *np++ = 0x0d;
4165 }
4166 else
4167 *np++ = *buf++;
4168 }
4169 if (buf < endp)
4170 *np++ = *buf++;
4171 return np - startp;
4172 }
4173
4174 #if defined(__DJGPP__) && __DJGPP__ == 2 && __DJGPP_MINOR__ == 0
4175
4176 /* In DJGPP v2.0, library `write' can call `malloc', which might
4177 cause relocation of the buffer whose address we get in ADDR.
4178 Here is a version of `write' that avoids calling `malloc',
4179 to serve us until such time as the library is fixed.
4180 Actually, what we define here is called `__write', because
4181 `write' is a stub that just jmp's to `__write' (to be
4182 POSIXLY-correct with respect to the global name-space). */
4183
4184 #include <io.h> /* for _write */
4185 #include <libc/dosio.h> /* for __file_handle_modes[] */
4186
4187 static char xbuf[64 * 1024]; /* DOS cannot write more in one chunk */
4188
4189 #define XBUF_END (xbuf + sizeof (xbuf) - 1)
4190
4191 int
4192 __write (int handle, const void *buffer, size_t count)
4193 {
4194 if (count == 0)
4195 return 0;
4196
4197 if(__file_handle_modes[handle] & O_BINARY)
4198 return _write (handle, buffer, count);
4199 else
4200 {
4201 char *xbp = xbuf;
4202 const char *bp = buffer;
4203 int total_written = 0;
4204 int nmoved = 0, ncr = 0;
4205
4206 while (count)
4207 {
4208 /* The next test makes sure there's space for at least 2 more
4209 characters in xbuf[], so both CR and LF can be put there. */
4210 if (xbp < XBUF_END)
4211 {
4212 if (*bp == '\n')
4213 {
4214 ncr++;
4215 *xbp++ = '\r';
4216 }
4217 *xbp++ = *bp++;
4218 nmoved++;
4219 count--;
4220 }
4221 if (xbp >= XBUF_END || !count)
4222 {
4223 size_t to_write = nmoved + ncr;
4224 int written = _write (handle, xbuf, to_write);
4225
4226 if (written == -1)
4227 return -1;
4228 else
4229 total_written += nmoved; /* CRs aren't counted in ret value */
4230
4231 /* If some, but not all were written (disk full?), return
4232 an estimate of the total written bytes not counting CRs. */
4233 if (written < to_write)
4234 return total_written - (to_write - written) * nmoved/to_write;
4235
4236 nmoved = 0;
4237 ncr = 0;
4238 xbp = xbuf;
4239 }
4240 }
4241 return total_written;
4242 }
4243 }
4244
4245 /* A low-level file-renaming function which works around Windows 95 bug.
4246 This is pulled directly out of DJGPP v2.01 library sources, and only
4247 used when you compile with DJGPP v2.0. */
4248
4249 #include <io.h>
4250
4251 int _rename(const char *old, const char *new)
4252 {
4253 __dpmi_regs r;
4254 int olen = strlen(old) + 1;
4255 int i;
4256 int use_lfn = _USE_LFN;
4257 char tempfile[FILENAME_MAX];
4258 const char *orig = old;
4259 int lfn_fd = -1;
4260
4261 r.x.dx = __tb_offset;
4262 r.x.di = __tb_offset + olen;
4263 r.x.ds = r.x.es = __tb_segment;
4264
4265 if (use_lfn)
4266 {
4267 /* Windows 95 bug: for some filenames, when you rename
4268 file -> file~ (as in Emacs, to leave a backup), the
4269 short 8+3 alias doesn't change, which effectively
4270 makes OLD and NEW the same file. We must rename
4271 through a temporary file to work around this. */
4272
4273 char *pbase = 0, *p;
4274 static char try_char[] = "abcdefghijklmnopqrstuvwxyz012345789";
4275 int idx = sizeof(try_char) - 1;
4276
4277 /* Generate a temporary name. Can't use `tmpnam', since $TMPDIR
4278 might point to another drive, which will fail the DOS call. */
4279 strcpy(tempfile, old);
4280 for (p = tempfile; *p; p++) /* ensure temporary is on the same drive */
4281 if (*p == '/' || *p == '\\' || *p == ':')
4282 pbase = p;
4283 if (pbase)
4284 pbase++;
4285 else
4286 pbase = tempfile;
4287 strcpy(pbase, "X$$djren$$.$$temp$$");
4288
4289 do
4290 {
4291 if (idx <= 0)
4292 return -1;
4293 *pbase = try_char[--idx];
4294 } while (_chmod(tempfile, 0) != -1);
4295
4296 r.x.ax = 0x7156;
4297 _put_path2(tempfile, olen);
4298 _put_path(old);
4299 __dpmi_int(0x21, &r);
4300 if (r.x.flags & 1)
4301 {
4302 errno = __doserr_to_errno(r.x.ax);
4303 return -1;
4304 }
4305
4306 /* Now create a file with the original name. This will
4307 ensure that NEW will always have a 8+3 alias
4308 different from that of OLD. (Seems to be required
4309 when NameNumericTail in the Registry is set to 0.) */
4310 lfn_fd = _creat(old, 0);
4311
4312 olen = strlen(tempfile) + 1;
4313 old = tempfile;
4314 r.x.di = __tb_offset + olen;
4315 }
4316
4317 for (i=0; i<2; i++)
4318 {
4319 if(use_lfn)
4320 r.x.ax = 0x7156;
4321 else
4322 r.h.ah = 0x56;
4323 _put_path2(new, olen);
4324 _put_path(old);
4325 __dpmi_int(0x21, &r);
4326 if(r.x.flags & 1)
4327 {
4328 if (r.x.ax == 5 && i == 0) /* access denied */
4329 remove(new); /* and try again */
4330 else
4331 {
4332 errno = __doserr_to_errno(r.x.ax);
4333
4334 /* Restore to original name if we renamed it to temporary. */
4335 if (use_lfn)
4336 {
4337 if (lfn_fd != -1)
4338 {
4339 _close (lfn_fd);
4340 remove (orig);
4341 }
4342 _put_path2(orig, olen);
4343 _put_path(tempfile);
4344 r.x.ax = 0x7156;
4345 __dpmi_int(0x21, &r);
4346 }
4347 return -1;
4348 }
4349 }
4350 else
4351 break;
4352 }
4353
4354 /* Success. Delete the file possibly created to work
4355 around the Windows 95 bug. */
4356 if (lfn_fd != -1)
4357 return (_close (lfn_fd) == 0) ? remove (orig) : -1;
4358 return 0;
4359 }
4360
4361 #endif /* __DJGPP__ == 2 && __DJGPP_MINOR__ == 0 */
4362
4363 DEFUN ("msdos-long-file-names", Fmsdos_long_file_names, Smsdos_long_file_names,
4364 0, 0, 0,
4365 doc: /* Return non-nil if long file names are supported on MSDOS. */)
4366 ()
4367 {
4368 return (_USE_LFN ? Qt : Qnil);
4369 }
4370
4371 /* Convert alphabetic characters in a filename to lower-case. */
4372
4373 void
4374 msdos_downcase_filename (p)
4375 register unsigned char *p;
4376 {
4377 /* Always lower-case drive letters a-z, even if the filesystem
4378 preserves case in filenames.
4379 This is so MSDOS filenames could be compared by string comparison
4380 functions that are case-sensitive. Even case-preserving filesystems
4381 do not distinguish case in drive letters. */
4382 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
4383 {
4384 *p += 'a' - 'A';
4385 p += 2;
4386 }
4387
4388 /* Under LFN we expect to get pathnames in their true case. */
4389 if (NILP (Fmsdos_long_file_names ()))
4390 for ( ; *p; p++)
4391 if (*p >= 'A' && *p <= 'Z')
4392 *p += 'a' - 'A';
4393 }
4394
4395 DEFUN ("msdos-downcase-filename", Fmsdos_downcase_filename, Smsdos_downcase_filename,
4396 1, 1, 0,
4397 doc: /* Convert alphabetic characters in FILENAME to lower case and return that.
4398 When long filenames are supported, doesn't change FILENAME.
4399 If FILENAME is not a string, returns nil.
4400 The argument object is never altered--the value is a copy. */)
4401 (filename)
4402 Lisp_Object filename;
4403 {
4404 Lisp_Object tem;
4405
4406 if (! STRINGP (filename))
4407 return Qnil;
4408
4409 tem = Fcopy_sequence (filename);
4410 msdos_downcase_filename (SDATA (tem));
4411 return tem;
4412 }
4413 \f
4414 /* The Emacs root directory as determined by init_environment. */
4415
4416 static char emacsroot[MAXPATHLEN];
4417
4418 char *
4419 rootrelativepath (rel)
4420 char *rel;
4421 {
4422 static char result[MAXPATHLEN + 10];
4423
4424 strcpy (result, emacsroot);
4425 strcat (result, "/");
4426 strcat (result, rel);
4427 return result;
4428 }
4429
4430 /* Define a lot of environment variables if not already defined. Don't
4431 remove anything unless you know what you're doing -- lots of code will
4432 break if one or more of these are missing. */
4433
4434 void
4435 init_environment (argc, argv, skip_args)
4436 int argc;
4437 char **argv;
4438 int skip_args;
4439 {
4440 char *s, *t, *root;
4441 int len, i;
4442 static const char * const tempdirs[] = {
4443 "$TMPDIR", "$TEMP", "$TMP", "c:/"
4444 };
4445 const int imax = sizeof (tempdirs) / sizeof (tempdirs[0]);
4446
4447 /* Make sure they have a usable $TMPDIR. Many Emacs functions use
4448 temporary files and assume "/tmp" if $TMPDIR is unset, which
4449 will break on DOS/Windows. Refuse to work if we cannot find
4450 a directory, not even "c:/", usable for that purpose. */
4451 for (i = 0; i < imax ; i++)
4452 {
4453 const char *tmp = tempdirs[i];
4454
4455 if (*tmp == '$')
4456 tmp = getenv (tmp + 1);
4457 /* Note that `access' can lie to us if the directory resides on a
4458 read-only filesystem, like CD-ROM or a write-protected floppy.
4459 The only way to be really sure is to actually create a file and
4460 see if it succeeds. But I think that's too much to ask. */
4461 if (tmp && access (tmp, D_OK) == 0)
4462 {
4463 setenv ("TMPDIR", tmp, 1);
4464 break;
4465 }
4466 }
4467 if (i >= imax)
4468 cmd_error_internal
4469 (Fcons (Qerror,
4470 Fcons (build_string ("no usable temporary directories found!!"),
4471 Qnil)),
4472 "While setting TMPDIR: ");
4473
4474 /* Note the startup time, so we know not to clear the screen if we
4475 exit immediately; see IT_reset_terminal_modes.
4476 (Yes, I know `clock' returns zero the first time it's called, but
4477 I do this anyway, in case some wiseguy changes that at some point.) */
4478 startup_time = clock ();
4479
4480 /* Find our root from argv[0]. Assuming argv[0] is, say,
4481 "c:/emacs/bin/emacs.exe" our root will be "c:/emacs". */
4482 root = alloca (MAXPATHLEN + 20);
4483 _fixpath (argv[0], root);
4484 msdos_downcase_filename (root);
4485 len = strlen (root);
4486 while (len > 0 && root[len] != '/' && root[len] != ':')
4487 len--;
4488 root[len] = '\0';
4489 if (len > 4
4490 && (strcmp (root + len - 4, "/bin") == 0
4491 || strcmp (root + len - 4, "/src") == 0)) /* under a debugger */
4492 root[len - 4] = '\0';
4493 else
4494 strcpy (root, "c:/emacs"); /* let's be defensive */
4495 len = strlen (root);
4496 strcpy (emacsroot, root);
4497
4498 /* We default HOME to our root. */
4499 setenv ("HOME", root, 0);
4500
4501 /* We default EMACSPATH to root + "/bin". */
4502 strcpy (root + len, "/bin");
4503 setenv ("EMACSPATH", root, 0);
4504
4505 /* I don't expect anybody to ever use other terminals so the internal
4506 terminal is the default. */
4507 setenv ("TERM", "internal", 0);
4508
4509 #ifdef HAVE_X_WINDOWS
4510 /* Emacs expects DISPLAY to be set. */
4511 setenv ("DISPLAY", "unix:0.0", 0);
4512 #endif
4513
4514 /* SHELL is a bit tricky -- COMSPEC is the closest we come, but we must
4515 downcase it and mirror the backslashes. */
4516 s = getenv ("COMSPEC");
4517 if (!s) s = "c:/command.com";
4518 t = alloca (strlen (s) + 1);
4519 strcpy (t, s);
4520 dostounix_filename (t);
4521 setenv ("SHELL", t, 0);
4522
4523 /* PATH is also downcased and backslashes mirrored. */
4524 s = getenv ("PATH");
4525 if (!s) s = "";
4526 t = alloca (strlen (s) + 3);
4527 /* Current directory is always considered part of MsDos's path but it is
4528 not normally mentioned. Now it is. */
4529 strcat (strcpy (t, ".;"), s);
4530 dostounix_filename (t); /* Not a single file name, but this should work. */
4531 setenv ("PATH", t, 1);
4532
4533 /* In some sense all dos users have root privileges, so... */
4534 setenv ("USER", "root", 0);
4535 setenv ("NAME", getenv ("USER"), 0);
4536
4537 /* Time zone determined from country code. To make this possible, the
4538 country code may not span more than one time zone. In other words,
4539 in the USA, you lose. */
4540 if (!getenv ("TZ"))
4541 switch (dos_country_code)
4542 {
4543 case 31: /* Belgium */
4544 case 32: /* The Netherlands */
4545 case 33: /* France */
4546 case 34: /* Spain */
4547 case 36: /* Hungary */
4548 case 38: /* Yugoslavia (or what's left of it?) */
4549 case 39: /* Italy */
4550 case 41: /* Switzerland */
4551 case 42: /* Tjekia */
4552 case 45: /* Denmark */
4553 case 46: /* Sweden */
4554 case 47: /* Norway */
4555 case 48: /* Poland */
4556 case 49: /* Germany */
4557 /* Daylight saving from last Sunday in March to last Sunday in
4558 September, both at 2AM. */
4559 setenv ("TZ", "MET-01METDST-02,M3.5.0/02:00,M9.5.0/02:00", 0);
4560 break;
4561 case 44: /* United Kingdom */
4562 case 351: /* Portugal */
4563 case 354: /* Iceland */
4564 setenv ("TZ", "GMT+00", 0);
4565 break;
4566 case 81: /* Japan */
4567 case 82: /* Korea */
4568 setenv ("TZ", "JST-09", 0);
4569 break;
4570 case 90: /* Turkey */
4571 case 358: /* Finland */
4572 setenv ("TZ", "EET-02", 0);
4573 break;
4574 case 972: /* Israel */
4575 /* This is an approximation. (For exact rules, use the
4576 `zoneinfo/israel' file which comes with DJGPP, but you need
4577 to install it in `/usr/share/zoneinfo/' directory first.) */
4578 setenv ("TZ", "IST-02IDT-03,M4.1.6/00:00,M9.5.6/01:00", 0);
4579 break;
4580 }
4581 tzset ();
4582 }
4583
4584 \f
4585
4586 static int break_stat; /* BREAK check mode status. */
4587 static int stdin_stat; /* stdin IOCTL status. */
4588
4589 #if __DJGPP__ < 2
4590
4591 /* These must be global. */
4592 static _go32_dpmi_seginfo ctrl_break_vector;
4593 static _go32_dpmi_registers ctrl_break_regs;
4594 static int ctrlbreakinstalled = 0;
4595
4596 /* Interrupt level detection of Ctrl-Break. Don't do anything fancy here! */
4597
4598 void
4599 ctrl_break_func (regs)
4600 _go32_dpmi_registers *regs;
4601 {
4602 Vquit_flag = Qt;
4603 }
4604
4605 void
4606 install_ctrl_break_check ()
4607 {
4608 if (!ctrlbreakinstalled)
4609 {
4610 /* Don't press Ctrl-Break if you don't have either DPMI or Emacs
4611 was compiler with Djgpp 1.11 maintenance level 5 or later! */
4612 ctrlbreakinstalled = 1;
4613 ctrl_break_vector.pm_offset = (int) ctrl_break_func;
4614 _go32_dpmi_allocate_real_mode_callback_iret (&ctrl_break_vector,
4615 &ctrl_break_regs);
4616 _go32_dpmi_set_real_mode_interrupt_vector (0x1b, &ctrl_break_vector);
4617 }
4618 }
4619
4620 #endif /* __DJGPP__ < 2 */
4621
4622 /* Turn off Dos' Ctrl-C checking and inhibit interpretation of
4623 control chars by DOS. Determine the keyboard type. */
4624
4625 int
4626 dos_ttraw ()
4627 {
4628 union REGS inregs, outregs;
4629 static int first_time = 1;
4630
4631 break_stat = getcbrk ();
4632 setcbrk (0);
4633 #if __DJGPP__ < 2
4634 install_ctrl_break_check ();
4635 #endif
4636
4637 if (first_time)
4638 {
4639 inregs.h.ah = 0xc0;
4640 int86 (0x15, &inregs, &outregs);
4641 extended_kbd = (!outregs.x.cflag) && (outregs.h.ah == 0);
4642
4643 have_mouse = 0;
4644
4645 if (internal_terminal
4646 #ifdef HAVE_X_WINDOWS
4647 && inhibit_window_system
4648 #endif
4649 )
4650 {
4651 inregs.x.ax = 0x0021;
4652 int86 (0x33, &inregs, &outregs);
4653 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4654 if (!have_mouse)
4655 {
4656 /* Reportedly, the above doesn't work for some mouse drivers. There
4657 is an additional detection method that should work, but might be
4658 a little slower. Use that as an alternative. */
4659 inregs.x.ax = 0x0000;
4660 int86 (0x33, &inregs, &outregs);
4661 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4662 }
4663
4664 if (have_mouse)
4665 {
4666 have_mouse = 1; /* enable mouse */
4667 mouse_visible = 0;
4668 mouse_setup_buttons (outregs.x.bx);
4669 mouse_position_hook = &mouse_get_pos;
4670 mouse_init ();
4671 }
4672
4673 #ifndef HAVE_X_WINDOWS
4674 #if __DJGPP__ >= 2
4675 /* Save the cursor shape used outside Emacs. */
4676 outside_cursor = _farpeekw (_dos_ds, 0x460);
4677 #endif
4678 #endif
4679 }
4680
4681 first_time = 0;
4682
4683 #if __DJGPP__ >= 2
4684
4685 stdin_stat = setmode (fileno (stdin), O_BINARY);
4686 return (stdin_stat != -1);
4687 }
4688 else
4689 return (setmode (fileno (stdin), O_BINARY) != -1);
4690
4691 #else /* __DJGPP__ < 2 */
4692
4693 }
4694
4695 /* I think it is wrong to overwrite `stdin_stat' every time
4696 but the first one this function is called, but I don't
4697 want to change the way it used to work in v1.x.--EZ */
4698
4699 inregs.x.ax = 0x4400; /* Get IOCTL status. */
4700 inregs.x.bx = 0x00; /* 0 = stdin. */
4701 intdos (&inregs, &outregs);
4702 stdin_stat = outregs.h.dl;
4703
4704 inregs.x.dx = stdin_stat | 0x0020; /* raw mode */
4705 inregs.x.ax = 0x4401; /* Set IOCTL status */
4706 intdos (&inregs, &outregs);
4707 return !outregs.x.cflag;
4708
4709 #endif /* __DJGPP__ < 2 */
4710 }
4711
4712 /* Restore status of standard input and Ctrl-C checking. */
4713
4714 int
4715 dos_ttcooked ()
4716 {
4717 union REGS inregs, outregs;
4718
4719 setcbrk (break_stat);
4720 mouse_off ();
4721
4722 #if __DJGPP__ >= 2
4723
4724 #ifndef HAVE_X_WINDOWS
4725 /* Restore the cursor shape we found on startup. */
4726 if (outside_cursor)
4727 {
4728 inregs.h.ah = 1;
4729 inregs.x.cx = outside_cursor;
4730 int86 (0x10, &inregs, &outregs);
4731 }
4732 #endif
4733
4734 return (setmode (fileno (stdin), stdin_stat) != -1);
4735
4736 #else /* not __DJGPP__ >= 2 */
4737
4738 inregs.x.ax = 0x4401; /* Set IOCTL status. */
4739 inregs.x.bx = 0x00; /* 0 = stdin. */
4740 inregs.x.dx = stdin_stat;
4741 intdos (&inregs, &outregs);
4742 return !outregs.x.cflag;
4743
4744 #endif /* not __DJGPP__ >= 2 */
4745 }
4746
4747 \f
4748 /* Run command as specified by ARGV in directory DIR.
4749 The command is run with input from TEMPIN, output to
4750 file TEMPOUT and stderr to TEMPERR. */
4751
4752 int
4753 run_msdos_command (argv, working_dir, tempin, tempout, temperr, envv)
4754 unsigned char **argv;
4755 const char *working_dir;
4756 int tempin, tempout, temperr;
4757 char **envv;
4758 {
4759 char *saveargv1, *saveargv2, *lowcase_argv0, *pa, *pl;
4760 char oldwd[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4761 int msshell, result = -1, inbak, outbak, errbak, x, y;
4762 Lisp_Object cmd;
4763
4764 /* Get current directory as MSDOS cwd is not per-process. */
4765 getwd (oldwd);
4766
4767 /* If argv[0] is the shell, it might come in any lettercase.
4768 Since `Fmember' is case-sensitive, we need to downcase
4769 argv[0], even if we are on case-preserving filesystems. */
4770 lowcase_argv0 = alloca (strlen (argv[0]) + 1);
4771 for (pa = argv[0], pl = lowcase_argv0; *pa; pl++)
4772 {
4773 *pl = *pa++;
4774 if (*pl >= 'A' && *pl <= 'Z')
4775 *pl += 'a' - 'A';
4776 }
4777 *pl = '\0';
4778
4779 cmd = Ffile_name_nondirectory (build_string (lowcase_argv0));
4780 msshell = !NILP (Fmember (cmd, Fsymbol_value (intern ("msdos-shells"))))
4781 && !strcmp ("-c", argv[1]);
4782 if (msshell)
4783 {
4784 saveargv1 = argv[1];
4785 saveargv2 = argv[2];
4786 argv[1] = "/c";
4787 /* We only need to mirror slashes if a DOS shell will be invoked
4788 not via `system' (which does the mirroring itself). Yes, that
4789 means DJGPP v1.x will lose here. */
4790 if (argv[2] && argv[3])
4791 {
4792 char *p = alloca (strlen (argv[2]) + 1);
4793
4794 strcpy (argv[2] = p, saveargv2);
4795 while (*p && isspace (*p))
4796 p++;
4797 while (*p)
4798 {
4799 if (*p == '/')
4800 *p++ = '\\';
4801 else
4802 p++;
4803 }
4804 }
4805 }
4806
4807 chdir (working_dir);
4808 inbak = dup (0);
4809 outbak = dup (1);
4810 errbak = dup (2);
4811 if (inbak < 0 || outbak < 0 || errbak < 0)
4812 goto done; /* Allocation might fail due to lack of descriptors. */
4813
4814 if (have_mouse > 0)
4815 mouse_get_xy (&x, &y);
4816
4817 dos_ttcooked (); /* do it here while 0 = stdin */
4818
4819 dup2 (tempin, 0);
4820 dup2 (tempout, 1);
4821 dup2 (temperr, 2);
4822
4823 #if __DJGPP__ > 1
4824
4825 if (msshell && !argv[3])
4826 {
4827 /* MS-DOS native shells are too restrictive. For starters, they
4828 cannot grok commands longer than 126 characters. In DJGPP v2
4829 and later, `system' is much smarter, so we'll call it instead. */
4830
4831 const char *cmnd;
4832
4833 /* A shell gets a single argument--its full command
4834 line--whose original was saved in `saveargv2'. */
4835
4836 /* Don't let them pass empty command lines to `system', since
4837 with some shells it will try to invoke an interactive shell,
4838 which will hang Emacs. */
4839 for (cmnd = saveargv2; *cmnd && isspace (*cmnd); cmnd++)
4840 ;
4841 if (*cmnd)
4842 {
4843 extern char **environ;
4844 char **save_env = environ;
4845 int save_system_flags = __system_flags;
4846
4847 /* Request the most powerful version of `system'. We need
4848 all the help we can get to avoid calling stock DOS shells. */
4849 __system_flags = (__system_redirect
4850 | __system_use_shell
4851 | __system_allow_multiple_cmds
4852 | __system_allow_long_cmds
4853 | __system_handle_null_commands
4854 | __system_emulate_chdir);
4855
4856 environ = envv;
4857 result = system (cmnd);
4858 __system_flags = save_system_flags;
4859 environ = save_env;
4860 }
4861 else
4862 result = 0; /* emulate Unixy shell behavior with empty cmd line */
4863 }
4864 else
4865
4866 #endif /* __DJGPP__ > 1 */
4867
4868 result = spawnve (P_WAIT, argv[0], argv, envv);
4869
4870 dup2 (inbak, 0);
4871 dup2 (outbak, 1);
4872 dup2 (errbak, 2);
4873 emacs_close (inbak);
4874 emacs_close (outbak);
4875 emacs_close (errbak);
4876
4877 dos_ttraw ();
4878 if (have_mouse > 0)
4879 {
4880 mouse_init ();
4881 mouse_moveto (x, y);
4882 }
4883
4884 /* Some programs might change the meaning of the highest bit of the
4885 text attribute byte, so we get blinking characters instead of the
4886 bright background colors. Restore that. */
4887 bright_bg ();
4888
4889 done:
4890 chdir (oldwd);
4891 if (msshell)
4892 {
4893 argv[1] = saveargv1;
4894 argv[2] = saveargv2;
4895 }
4896 return result;
4897 }
4898
4899 void
4900 croak (badfunc)
4901 char *badfunc;
4902 {
4903 fprintf (stderr, "%s not yet implemented\r\n", badfunc);
4904 reset_sys_modes ();
4905 exit (1);
4906 }
4907 \f
4908 #if __DJGPP__ < 2
4909
4910 /* ------------------------- Compatibility functions -------------------
4911 * gethostname
4912 * gettimeofday
4913 */
4914
4915 /* Hostnames for a pc are not really funny,
4916 but they are used in change log so we emulate the best we can. */
4917
4918 gethostname (p, size)
4919 char *p;
4920 int size;
4921 {
4922 char *q = egetenv ("HOSTNAME");
4923
4924 if (!q) q = "pc";
4925 strcpy (p, q);
4926 return 0;
4927 }
4928
4929 /* When time zones are set from Ms-Dos too many C-libraries are playing
4930 tricks with time values. We solve this by defining our own version
4931 of `gettimeofday' bypassing GO32. Our version needs to be initialized
4932 once and after each call to `tzset' with TZ changed. That is
4933 accomplished by aliasing tzset to init_gettimeofday. */
4934
4935 static struct tm time_rec;
4936
4937 int
4938 gettimeofday (struct timeval *tp, struct timezone *tzp)
4939 {
4940 if (tp)
4941 {
4942 struct time t;
4943 struct tm tm;
4944
4945 gettime (&t);
4946 if (t.ti_hour < time_rec.tm_hour) /* midnight wrap */
4947 {
4948 struct date d;
4949 getdate (&d);
4950 time_rec.tm_year = d.da_year - 1900;
4951 time_rec.tm_mon = d.da_mon - 1;
4952 time_rec.tm_mday = d.da_day;
4953 }
4954
4955 time_rec.tm_hour = t.ti_hour;
4956 time_rec.tm_min = t.ti_min;
4957 time_rec.tm_sec = t.ti_sec;
4958
4959 tm = time_rec;
4960 tm.tm_gmtoff = dos_timezone_offset;
4961
4962 tp->tv_sec = mktime (&tm); /* may modify tm */
4963 tp->tv_usec = t.ti_hund * (1000000 / 100);
4964 }
4965 /* Ignore tzp; it's obsolescent. */
4966 return 0;
4967 }
4968
4969 #endif /* __DJGPP__ < 2 */
4970
4971 /*
4972 * A list of unimplemented functions that we silently ignore.
4973 */
4974
4975 #if __DJGPP__ < 2
4976 unsigned alarm (s) unsigned s; {}
4977 fork () { return 0; }
4978 int kill (x, y) int x, y; { return -1; }
4979 nice (p) int p; {}
4980 void volatile pause () {}
4981 sigsetmask (x) int x; { return 0; }
4982 sigblock (mask) int mask; { return 0; }
4983 #endif
4984
4985 void request_sigio (void) {}
4986 setpgrp () {return 0; }
4987 setpriority (x,y,z) int x,y,z; { return 0; }
4988 void unrequest_sigio (void) {}
4989
4990 #if __DJGPP__ > 1
4991 #if __DJGPP_MINOR__ < 2
4992
4993 #ifdef POSIX_SIGNALS
4994
4995 /* Augment DJGPP library POSIX signal functions. This is needed
4996 as of DJGPP v2.01, but might be in the library in later releases. */
4997
4998 #include <libc/bss.h>
4999
5000 /* A counter to know when to re-initialize the static sets. */
5001 static int sigprocmask_count = -1;
5002
5003 /* Which signals are currently blocked (initially none). */
5004 static sigset_t current_mask;
5005
5006 /* Which signals are pending (initially none). */
5007 static sigset_t pending_signals;
5008
5009 /* Previous handlers to restore when the blocked signals are unblocked. */
5010 typedef void (*sighandler_t)(int);
5011 static sighandler_t prev_handlers[320];
5012
5013 /* A signal handler which just records that a signal occurred
5014 (it will be raised later, if and when the signal is unblocked). */
5015 static void
5016 sig_suspender (signo)
5017 int signo;
5018 {
5019 sigaddset (&pending_signals, signo);
5020 }
5021
5022 int
5023 sigprocmask (how, new_set, old_set)
5024 int how;
5025 const sigset_t *new_set;
5026 sigset_t *old_set;
5027 {
5028 int signo;
5029 sigset_t new_mask;
5030
5031 /* If called for the first time, initialize. */
5032 if (sigprocmask_count != __bss_count)
5033 {
5034 sigprocmask_count = __bss_count;
5035 sigemptyset (&pending_signals);
5036 sigemptyset (&current_mask);
5037 for (signo = 0; signo < 320; signo++)
5038 prev_handlers[signo] = SIG_ERR;
5039 }
5040
5041 if (old_set)
5042 *old_set = current_mask;
5043
5044 if (new_set == 0)
5045 return 0;
5046
5047 if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK)
5048 {
5049 errno = EINVAL;
5050 return -1;
5051 }
5052
5053 sigemptyset (&new_mask);
5054
5055 /* DJGPP supports upto 320 signals. */
5056 for (signo = 0; signo < 320; signo++)
5057 {
5058 if (sigismember (&current_mask, signo))
5059 sigaddset (&new_mask, signo);
5060 else if (sigismember (new_set, signo) && how != SIG_UNBLOCK)
5061 {
5062 sigaddset (&new_mask, signo);
5063
5064 /* SIGKILL is silently ignored, as on other platforms. */
5065 if (signo != SIGKILL && prev_handlers[signo] == SIG_ERR)
5066 prev_handlers[signo] = signal (signo, sig_suspender);
5067 }
5068 if (( how == SIG_UNBLOCK
5069 && sigismember (&new_mask, signo)
5070 && sigismember (new_set, signo))
5071 || (how == SIG_SETMASK
5072 && sigismember (&new_mask, signo)
5073 && !sigismember (new_set, signo)))
5074 {
5075 sigdelset (&new_mask, signo);
5076 if (prev_handlers[signo] != SIG_ERR)
5077 {
5078 signal (signo, prev_handlers[signo]);
5079 prev_handlers[signo] = SIG_ERR;
5080 }
5081 if (sigismember (&pending_signals, signo))
5082 {
5083 sigdelset (&pending_signals, signo);
5084 raise (signo);
5085 }
5086 }
5087 }
5088 current_mask = new_mask;
5089 return 0;
5090 }
5091
5092 #else /* not POSIX_SIGNALS */
5093
5094 sigsetmask (x) int x; { return 0; }
5095 sigblock (mask) int mask; { return 0; }
5096
5097 #endif /* not POSIX_SIGNALS */
5098 #endif /* not __DJGPP_MINOR__ < 2 */
5099 #endif /* __DJGPP__ > 1 */
5100
5101 #ifndef HAVE_SELECT
5102 #include "sysselect.h"
5103
5104 #ifndef EMACS_TIME_ZERO_OR_NEG_P
5105 #define EMACS_TIME_ZERO_OR_NEG_P(time) \
5106 ((long)(time).tv_sec < 0 \
5107 || ((time).tv_sec == 0 \
5108 && (long)(time).tv_usec <= 0))
5109 #endif
5110
5111 /* This yields the rest of the current time slice to the task manager.
5112 It should be called by any code which knows that it has nothing
5113 useful to do except idle.
5114
5115 I don't use __dpmi_yield here, since versions of library before 2.02
5116 called Int 2Fh/AX=1680h there in a way that would wedge the DOS box
5117 on some versions of Windows 9X. */
5118
5119 void
5120 dos_yield_time_slice (void)
5121 {
5122 _go32_dpmi_registers r;
5123
5124 r.x.ax = 0x1680;
5125 r.x.ss = r.x.sp = r.x.flags = 0;
5126 _go32_dpmi_simulate_int (0x2f, &r);
5127 if (r.h.al == 0x80)
5128 errno = ENOSYS;
5129 }
5130
5131 /* Only event queue is checked. */
5132 /* We don't have to call timer_check here
5133 because wait_reading_process_input takes care of that. */
5134 int
5135 sys_select (nfds, rfds, wfds, efds, timeout)
5136 int nfds;
5137 SELECT_TYPE *rfds, *wfds, *efds;
5138 EMACS_TIME *timeout;
5139 {
5140 int check_input;
5141 struct time t;
5142
5143 check_input = 0;
5144 if (rfds)
5145 {
5146 check_input = FD_ISSET (0, rfds);
5147 FD_ZERO (rfds);
5148 }
5149 if (wfds)
5150 FD_ZERO (wfds);
5151 if (efds)
5152 FD_ZERO (efds);
5153
5154 if (nfds != 1)
5155 abort ();
5156
5157 /* If we are looking only for the terminal, with no timeout,
5158 just read it and wait -- that's more efficient. */
5159 if (!timeout)
5160 {
5161 while (!detect_input_pending ())
5162 {
5163 dos_yield_time_slice ();
5164 }
5165 }
5166 else
5167 {
5168 EMACS_TIME clnow, cllast, cldiff;
5169
5170 gettime (&t);
5171 EMACS_SET_SECS_USECS (cllast, t.ti_sec, t.ti_hund * 10000L);
5172
5173 while (!check_input || !detect_input_pending ())
5174 {
5175 gettime (&t);
5176 EMACS_SET_SECS_USECS (clnow, t.ti_sec, t.ti_hund * 10000L);
5177 EMACS_SUB_TIME (cldiff, clnow, cllast);
5178
5179 /* When seconds wrap around, we assume that no more than
5180 1 minute passed since last `gettime'. */
5181 if (EMACS_TIME_NEG_P (cldiff))
5182 EMACS_SET_SECS (cldiff, EMACS_SECS (cldiff) + 60);
5183 EMACS_SUB_TIME (*timeout, *timeout, cldiff);
5184
5185 /* Stop when timeout value crosses zero. */
5186 if (EMACS_TIME_ZERO_OR_NEG_P (*timeout))
5187 return 0;
5188 cllast = clnow;
5189 dos_yield_time_slice ();
5190 }
5191 }
5192
5193 FD_SET (0, rfds);
5194 return 1;
5195 }
5196 #endif
5197
5198 /*
5199 * Define overlaid functions:
5200 *
5201 * chdir -> sys_chdir
5202 * tzset -> init_gettimeofday
5203 * abort -> dos_abort
5204 */
5205
5206 #ifdef chdir
5207 #undef chdir
5208 extern int chdir ();
5209
5210 int
5211 sys_chdir (path)
5212 const char* path;
5213 {
5214 int len = strlen (path);
5215 char *tmp = (char *)path;
5216
5217 if (*tmp && tmp[1] == ':')
5218 {
5219 if (getdisk () != tolower (tmp[0]) - 'a')
5220 setdisk (tolower (tmp[0]) - 'a');
5221 tmp += 2; /* strip drive: KFS 1995-07-06 */
5222 len -= 2;
5223 }
5224
5225 if (len > 1 && (tmp[len - 1] == '/'))
5226 {
5227 char *tmp1 = (char *) alloca (len + 1);
5228 strcpy (tmp1, tmp);
5229 tmp1[len - 1] = 0;
5230 tmp = tmp1;
5231 }
5232 return chdir (tmp);
5233 }
5234 #endif
5235
5236 #ifdef tzset
5237 #undef tzset
5238 extern void tzset (void);
5239
5240 void
5241 init_gettimeofday ()
5242 {
5243 time_t ltm, gtm;
5244 struct tm *lstm;
5245
5246 tzset ();
5247 ltm = gtm = time (NULL);
5248 ltm = mktime (lstm = localtime (&ltm));
5249 gtm = mktime (gmtime (&gtm));
5250 time_rec.tm_hour = 99; /* force gettimeofday to get date */
5251 time_rec.tm_isdst = lstm->tm_isdst;
5252 dos_timezone_offset = time_rec.tm_gmtoff = (int)(gtm - ltm) / 60;
5253 }
5254 #endif
5255
5256 #ifdef abort
5257 #undef abort
5258 void
5259 dos_abort (file, line)
5260 char *file;
5261 int line;
5262 {
5263 char buffer1[200], buffer2[400];
5264 int i, j;
5265
5266 sprintf (buffer1, "<EMACS FATAL ERROR IN %s LINE %d>", file, line);
5267 for (i = j = 0; buffer1[i]; i++) {
5268 buffer2[j++] = buffer1[i];
5269 buffer2[j++] = 0x70;
5270 }
5271 dosmemput (buffer2, j, (int)ScreenPrimary);
5272 ScreenSetCursor (2, 0);
5273 abort ();
5274 }
5275 #else
5276 void
5277 abort ()
5278 {
5279 dos_ttcooked ();
5280 ScreenSetCursor (10, 0);
5281 cputs ("\r\n\nEmacs aborted!\r\n");
5282 #if __DJGPP__ > 1
5283 #if __DJGPP__ == 2 && __DJGPP_MINOR__ < 2
5284 if (screen_virtual_segment)
5285 dosv_refresh_virtual_screen (2 * 10 * screen_size_X, 4 * screen_size_X);
5286 /* Generate traceback, so we could tell whodunit. */
5287 signal (SIGINT, SIG_DFL);
5288 __asm__ __volatile__ ("movb $0x1b,%al;call ___djgpp_hw_exception");
5289 #else /* __DJGPP_MINOR__ >= 2 */
5290 raise (SIGABRT);
5291 #endif /* __DJGPP_MINOR__ >= 2 */
5292 #endif
5293 exit (2);
5294 }
5295 #endif
5296
5297 /* The following variables are required so that cus-start.el won't
5298 complain about unbound variables. */
5299 #ifndef subprocesses
5300 /* Nonzero means delete a process right away if it exits (process.c). */
5301 static int delete_exited_processes;
5302 #endif
5303
5304 syms_of_msdos ()
5305 {
5306 recent_doskeys = Fmake_vector (make_number (NUM_RECENT_DOSKEYS), Qnil);
5307 staticpro (&recent_doskeys);
5308 #ifndef HAVE_X_WINDOWS
5309 help_echo = Qnil;
5310 staticpro (&help_echo);
5311 help_echo_object = Qnil;
5312 staticpro (&help_echo_object);
5313 help_echo_window = Qnil;
5314 staticpro (&help_echo_window);
5315 previous_help_echo = Qnil;
5316 staticpro (&previous_help_echo);
5317 help_echo_pos = -1;
5318
5319 /* The following two are from xfns.c: */
5320 Qbar = intern ("bar");
5321 staticpro (&Qbar);
5322 Qhbar = intern ("hbar");
5323 staticpro (&Qhbar);
5324 Qcursor_type = intern ("cursor-type");
5325 staticpro (&Qcursor_type);
5326 Qreverse = intern ("reverse");
5327 staticpro (&Qreverse);
5328
5329 DEFVAR_LISP ("dos-unsupported-char-glyph", &Vdos_unsupported_char_glyph,
5330 doc: /* *Glyph to display instead of chars not supported by current codepage.
5331
5332 This variable is used only by MSDOS terminals. */);
5333 Vdos_unsupported_char_glyph = '\177';
5334
5335 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
5336 doc: /* *Non-nil means autoselect window with mouse pointer. */);
5337 mouse_autoselect_window = 0;
5338 #endif
5339 #ifndef subprocesses
5340 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
5341 doc: /* *Non-nil means delete processes immediately when they exit.
5342 nil means don't delete them until `list-processes' is run. */);
5343 delete_exited_processes = 0;
5344 #endif
5345
5346 defsubr (&Srecent_doskeys);
5347 defsubr (&Smsdos_long_file_names);
5348 defsubr (&Smsdos_downcase_filename);
5349 defsubr (&Smsdos_remember_default_colors);
5350 defsubr (&Smsdos_set_mouse_buttons);
5351 }
5352
5353 #endif /* MSDOS */