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