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