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