Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / src / msdos.c
... / ...
CommitLineData
1/* MS-DOS specific C utilities. -*- coding: raw-text -*-
2
3Copyright (C) 1993-1997, 1999-2012 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20/* Contributed by Morten Welinder */
21/* New display, keyboard, and mouse control by Kim F. Storm */
22
23/* Note: some of the stuff here was taken from end of sysdep.c in demacs. */
24
25#include <config.h>
26
27#ifdef MSDOS
28#include <setjmp.h>
29#include "lisp.h"
30#include <stdio.h>
31#include <time.h>
32#include <sys/param.h>
33#include <sys/time.h>
34#include <dos.h>
35#include <errno.h>
36#include <sys/stat.h> /* for _fixpath */
37#include <unistd.h> /* for chdir, dup, dup2, etc. */
38#include <dir.h> /* for getdisk */
39#pragma pack(0) /* dir.h does a pack(4), which isn't GCC's default */
40#include <fcntl.h>
41#include <io.h> /* for setmode */
42#include <dpmi.h> /* for __dpmi_xxx stuff */
43#include <sys/farptr.h> /* for _farsetsel, _farnspokeb */
44#include <libc/dosio.h> /* for _USE_LFN */
45#include <conio.h> /* for cputs */
46
47#include "msdos.h"
48#include "systime.h"
49#include "frame.h"
50#include "termhooks.h"
51#include "termchar.h"
52#include "dispextern.h"
53#include "dosfns.h"
54#include "termopts.h"
55#include "character.h"
56#include "coding.h"
57#include "disptab.h"
58#include "window.h"
59#include "buffer.h"
60#include "commands.h"
61#include "blockinput.h"
62#include "keyboard.h"
63#include "intervals.h"
64#include <go32.h>
65#include <pc.h>
66#include <ctype.h>
67/* #include <process.h> */
68/* Damn that local process.h! Instead we can define P_WAIT and
69 spawnve ourselves. */
70#define P_WAIT 1
71extern int spawnve (int, const char *, char *const [], char *const []);
72
73#ifndef _USE_LFN
74#define _USE_LFN 0
75#endif
76
77#ifndef _dos_ds
78#define _dos_ds _go32_info_block.selector_for_linear_memory
79#endif
80
81#include <signal.h>
82#include "syssignal.h"
83
84#include "careadlinkat.h"
85#include "allocator.h"
86
87#ifndef SYSTEM_MALLOC
88
89#ifdef GNU_MALLOC
90
91/* If other `malloc' than ours is used, force our `sbrk' behave like
92 Unix programs expect (resize memory blocks to keep them contiguous).
93 If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
94 because that's what `gmalloc' expects to get. */
95#include <crt0.h>
96
97#ifdef REL_ALLOC
98int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
99#else /* not REL_ALLOC */
100int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
101#endif /* not REL_ALLOC */
102#endif /* GNU_MALLOC */
103
104#endif /* not SYSTEM_MALLOC */
105
106static unsigned long
107event_timestamp (void)
108{
109 struct time t;
110 unsigned long s;
111
112 gettime (&t);
113 s = t.ti_min;
114 s *= 60;
115 s += t.ti_sec;
116 s *= 1000;
117 s += t.ti_hund * 10;
118
119 return s;
120}
121
122\f
123/* ------------------------ Mouse control ---------------------------
124 *
125 * Coordinates are in screen positions and zero based.
126 * Mouse buttons are numbered from left to right and also zero based.
127 */
128
129/* This used to be in termhooks.h, but mainstream Emacs code no longer
130 uses it, and it was removed... */
131#define NUM_MOUSE_BUTTONS (5)
132
133int have_mouse; /* 0: no, 1: enabled, -1: disabled */
134static int mouse_visible;
135
136static int mouse_last_x;
137static int mouse_last_y;
138
139static int mouse_button_translate[NUM_MOUSE_BUTTONS];
140static int mouse_button_count;
141
142void
143mouse_on (void)
144{
145 union REGS regs;
146
147 if (have_mouse > 0 && !mouse_visible)
148 {
149 struct tty_display_info *tty = CURTTY ();
150
151 if (tty->termscript)
152 fprintf (tty->termscript, "<M_ON>");
153 regs.x.ax = 0x0001;
154 int86 (0x33, &regs, &regs);
155 mouse_visible = 1;
156 }
157}
158
159void
160mouse_off (void)
161{
162 union REGS regs;
163
164 if (have_mouse > 0 && mouse_visible)
165 {
166 struct tty_display_info *tty = CURTTY ();
167
168 if (tty->termscript)
169 fprintf (tty->termscript, "<M_OFF>");
170 regs.x.ax = 0x0002;
171 int86 (0x33, &regs, &regs);
172 mouse_visible = 0;
173 }
174}
175
176static void
177mouse_setup_buttons (int n_buttons)
178{
179 if (n_buttons == 3)
180 {
181 mouse_button_count = 3;
182 mouse_button_translate[0] = 0; /* Left */
183 mouse_button_translate[1] = 2; /* Middle */
184 mouse_button_translate[2] = 1; /* Right */
185 }
186 else /* two, what else? */
187 {
188 mouse_button_count = 2;
189 mouse_button_translate[0] = 0;
190 mouse_button_translate[1] = 1;
191 }
192}
193
194DEFUN ("msdos-set-mouse-buttons", Fmsdos_set_mouse_buttons, Smsdos_set_mouse_buttons,
195 1, 1, "NSet number of mouse buttons to: ",
196 doc: /* Set the number of mouse buttons to use by Emacs.
197This is useful with mice that report the number of buttons inconsistently,
198e.g., if the number of buttons is reported as 3, but Emacs only sees 2 of
199them. This happens with wheeled mice on Windows 9X, for example. */)
200 (Lisp_Object nbuttons)
201{
202 int n;
203
204 CHECK_NUMBER (nbuttons);
205 n = XINT (nbuttons);
206 if (n < 2 || n > 3)
207 xsignal2 (Qargs_out_of_range,
208 build_string ("only 2 or 3 mouse buttons are supported"),
209 nbuttons);
210 mouse_setup_buttons (n);
211 return Qnil;
212}
213
214static void
215mouse_get_xy (int *x, int *y)
216{
217 union REGS regs;
218
219 regs.x.ax = 0x0003;
220 int86 (0x33, &regs, &regs);
221 *x = regs.x.cx / 8;
222 *y = regs.x.dx / 8;
223}
224
225void
226mouse_moveto (int x, int y)
227{
228 union REGS regs;
229 struct tty_display_info *tty = CURTTY ();
230
231 if (tty->termscript)
232 fprintf (tty->termscript, "<M_XY=%dx%d>", x, y);
233 regs.x.ax = 0x0004;
234 mouse_last_x = regs.x.cx = x * 8;
235 mouse_last_y = regs.x.dx = y * 8;
236 int86 (0x33, &regs, &regs);
237}
238
239static int
240mouse_pressed (int b, int *xp, int *yp)
241{
242 union REGS regs;
243
244 if (b >= mouse_button_count)
245 return 0;
246 regs.x.ax = 0x0005;
247 regs.x.bx = mouse_button_translate[b];
248 int86 (0x33, &regs, &regs);
249 if (regs.x.bx)
250 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
251 return (regs.x.bx != 0);
252}
253
254static int
255mouse_released (int b, int *xp, int *yp)
256{
257 union REGS regs;
258
259 if (b >= mouse_button_count)
260 return 0;
261 regs.x.ax = 0x0006;
262 regs.x.bx = mouse_button_translate[b];
263 int86 (0x33, &regs, &regs);
264 if (regs.x.bx)
265 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
266 return (regs.x.bx != 0);
267}
268
269static int
270mouse_button_depressed (int b, int *xp, int *yp)
271{
272 union REGS regs;
273
274 if (b >= mouse_button_count)
275 return 0;
276 regs.x.ax = 0x0003;
277 int86 (0x33, &regs, &regs);
278 if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
279 {
280 *xp = regs.x.cx / 8;
281 *yp = regs.x.dx / 8;
282 return 1;
283 }
284 return 0;
285}
286
287void
288mouse_get_pos (FRAME_PTR *f, int insist, Lisp_Object *bar_window,
289 enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
290 Time *time)
291{
292 int ix, iy;
293 Lisp_Object frame, tail;
294
295 /* Clear the mouse-moved flag for every frame on this display. */
296 FOR_EACH_FRAME (tail, frame)
297 XFRAME (frame)->mouse_moved = 0;
298
299 *f = SELECTED_FRAME ();
300 *bar_window = Qnil;
301 mouse_get_xy (&ix, &iy);
302 *time = event_timestamp ();
303 *x = make_number (mouse_last_x = ix);
304 *y = make_number (mouse_last_y = iy);
305}
306
307static void
308mouse_check_moved (void)
309{
310 int x, y;
311
312 mouse_get_xy (&x, &y);
313 SELECTED_FRAME ()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
314 mouse_last_x = x;
315 mouse_last_y = y;
316}
317
318/* Force the mouse driver to ``forget'' about any button clicks until
319 now. */
320static void
321mouse_clear_clicks (void)
322{
323 int b;
324
325 for (b = 0; b < mouse_button_count; b++)
326 {
327 int dummy_x, dummy_y;
328
329 (void) mouse_pressed (b, &dummy_x, &dummy_y);
330 (void) mouse_released (b, &dummy_x, &dummy_y);
331 }
332}
333
334void
335mouse_init (void)
336{
337 union REGS regs;
338 struct tty_display_info *tty = CURTTY ();
339
340 if (tty->termscript)
341 fprintf (tty->termscript, "<M_INIT>");
342
343 regs.x.ax = 0x0021;
344 int86 (0x33, &regs, &regs);
345
346 /* Reset the mouse last press/release info. It seems that Windows
347 doesn't do that automatically when function 21h is called, which
348 causes Emacs to ``remember'' the click that switched focus to the
349 window just before Emacs was started from that window. */
350 mouse_clear_clicks ();
351
352 regs.x.ax = 0x0007;
353 regs.x.cx = 0;
354 regs.x.dx = 8 * (ScreenCols () - 1);
355 int86 (0x33, &regs, &regs);
356
357 regs.x.ax = 0x0008;
358 regs.x.cx = 0;
359 regs.x.dx = 8 * (ScreenRows () - 1);
360 int86 (0x33, &regs, &regs);
361
362 mouse_moveto (0, 0);
363 mouse_visible = 0;
364}
365\f
366/* ------------------------- Screen control ----------------------
367 *
368 */
369
370static int internal_terminal = 0;
371
372#ifndef HAVE_X_WINDOWS
373extern unsigned char ScreenAttrib;
374static int screen_face;
375
376static int screen_size_X;
377static int screen_size_Y;
378static int screen_size;
379
380static int current_pos_X;
381static int current_pos_Y;
382static int new_pos_X;
383static int new_pos_Y;
384
385static void *startup_screen_buffer;
386static int startup_screen_size_X;
387static int startup_screen_size_Y;
388static int startup_pos_X;
389static int startup_pos_Y;
390static unsigned char startup_screen_attrib;
391
392static clock_t startup_time;
393
394static int term_setup_done;
395
396static unsigned short outside_cursor;
397
398/* Similar to the_only_frame. */
399struct tty_display_info the_only_display_info;
400
401/* Support for DOS/V (allows Japanese characters to be displayed on
402 standard, non-Japanese, ATs). Only supported for DJGPP v2 and later. */
403
404/* Holds the address of the text-mode screen buffer. */
405static unsigned long screen_old_address = 0;
406/* Segment and offset of the virtual screen. If 0, DOS/V is NOT loaded. */
407static unsigned short screen_virtual_segment = 0;
408static unsigned short screen_virtual_offset = 0;
409extern Lisp_Object Qcursor_type;
410extern Lisp_Object Qbar, Qhbar;
411
412/* The screen colors of the current frame, which serve as the default
413 colors for newly-created frames. */
414static int initial_screen_colors[2];
415
416/* Update the screen from a part of relocated DOS/V screen buffer which
417 begins at OFFSET and includes COUNT characters. */
418static void
419dosv_refresh_virtual_screen (int offset, int count)
420{
421 __dpmi_regs regs;
422
423 if (offset < 0 || count < 0) /* paranoia; invalid values crash DOS/V */
424 return;
425
426 regs.h.ah = 0xff; /* update relocated screen */
427 regs.x.es = screen_virtual_segment;
428 regs.x.di = screen_virtual_offset + offset;
429 regs.x.cx = count;
430 __dpmi_int (0x10, &regs);
431}
432
433static void
434dos_direct_output (int y, int x, char *buf, int len)
435{
436 int t0 = 2 * (x + y * screen_size_X);
437 int t = t0 + (int) ScreenPrimary;
438 int l0 = len;
439
440 /* This is faster. */
441 for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
442 _farnspokeb (t, *buf);
443
444 if (screen_virtual_segment)
445 dosv_refresh_virtual_screen (t0, l0);
446}
447#endif
448
449#ifndef HAVE_X_WINDOWS
450
451static int blink_bit = -1; /* the state of the blink bit at startup */
452
453/* Enable bright background colors. */
454static void
455bright_bg (void)
456{
457 union REGS regs;
458
459 /* Remember the original state of the blink/bright-background bit.
460 It is stored at 0040:0065h in the BIOS data area. */
461 if (blink_bit == -1)
462 blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;
463
464 regs.h.bl = 0;
465 regs.x.ax = 0x1003;
466 int86 (0x10, &regs, &regs);
467}
468
469/* Disable bright background colors (and enable blinking) if we found
470 the video system in that state at startup. */
471static void
472maybe_enable_blinking (void)
473{
474 if (blink_bit == 1)
475 {
476 union REGS regs;
477
478 regs.h.bl = 1;
479 regs.x.ax = 0x1003;
480 int86 (0x10, &regs, &regs);
481 }
482}
483
484/* Return non-zero if the system has a VGA adapter. */
485static int
486vga_installed (void)
487{
488 union REGS regs;
489
490 regs.x.ax = 0x1a00;
491 int86 (0x10, &regs, &regs);
492 if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
493 return 1;
494 return 0;
495}
496
497/* Set the screen dimensions so that it can show no less than
498 ROWS x COLS frame. */
499
500void
501dos_set_window_size (int *rows, int *cols)
502{
503 char video_name[30];
504 union REGS regs;
505 Lisp_Object video_mode;
506 int video_mode_value, have_vga = 0;
507 int current_rows = ScreenRows (), current_cols = ScreenCols ();
508
509 if (*rows == current_rows && *cols == current_cols)
510 return;
511
512 mouse_off ();
513 have_vga = vga_installed ();
514
515 /* If the user specified a special video mode for these dimensions,
516 use that mode. */
517 sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
518 video_mode = Fsymbol_value (Fintern_soft (build_string (video_name), Qnil));
519
520 if (INTEGERP (video_mode)
521 && (video_mode_value = XINT (video_mode)) > 0)
522 {
523 regs.x.ax = video_mode_value;
524 int86 (0x10, &regs, &regs);
525
526 if (have_mouse)
527 {
528 /* Must hardware-reset the mouse, or else it won't update
529 its notion of screen dimensions for some non-standard
530 video modes. This is *painfully* slow... */
531 regs.x.ax = 0;
532 int86 (0x33, &regs, &regs);
533 }
534 }
535
536 /* Find one of the dimensions supported by standard EGA/VGA
537 which gives us at least the required dimensions. */
538 else
539 {
540 static struct {
541 int rows, need_vga;
542 } std_dimension[] = {
543 {25, 0},
544 {28, 1},
545 {35, 0},
546 {40, 1},
547 {43, 0},
548 {50, 1}
549 };
550 int i = 0;
551
552 while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
553 {
554 if (std_dimension[i].need_vga <= have_vga
555 && std_dimension[i].rows >= *rows)
556 {
557 if (std_dimension[i].rows != current_rows
558 || *cols != current_cols)
559 _set_screen_lines (std_dimension[i].rows);
560 break;
561 }
562 i++;
563 }
564 }
565
566
567 if (have_mouse)
568 {
569 mouse_init ();
570 mouse_on ();
571 }
572
573 /* Tell the caller what dimensions have been REALLY set. */
574 *rows = ScreenRows ();
575 *cols = ScreenCols ();
576
577 /* Update Emacs' notion of screen dimensions. */
578 screen_size_X = *cols;
579 screen_size_Y = *rows;
580 screen_size = *cols * *rows;
581
582 /* If the dimensions changed, the mouse highlight info is invalid. */
583 if (current_rows != *rows || current_cols != *cols)
584 {
585 struct frame *f = SELECTED_FRAME ();
586 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
587 Lisp_Object window = hlinfo->mouse_face_window;
588
589 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
590 {
591 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
592 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
593 hlinfo->mouse_face_window = Qnil;
594 }
595 }
596
597 /* Enable bright background colors. */
598 bright_bg ();
599
600 /* FIXME: I'm not sure the above will run at all on DOS/V. But let's
601 be defensive anyway. */
602 if (screen_virtual_segment)
603 dosv_refresh_virtual_screen (0, *cols * *rows);
604}
605
606/* If we write a character in the position where the mouse is,
607 the mouse cursor may need to be refreshed. */
608
609static void
610mouse_off_maybe (void)
611{
612 int x, y;
613
614 if (!mouse_visible)
615 return;
616
617 mouse_get_xy (&x, &y);
618 if (y != new_pos_Y || x < new_pos_X)
619 return;
620
621 mouse_off ();
622}
623
624#define DEFAULT_CURSOR_START (-1)
625#define DEFAULT_CURSOR_WIDTH (-1)
626#define BOX_CURSOR_WIDTH (-32)
627
628/* Set cursor to begin at scan line START_LINE in the character cell
629 and extend for WIDTH scan lines. Scan lines are counted from top
630 of the character cell, starting from zero. */
631static void
632msdos_set_cursor_shape (struct frame *f, int start_line, int width)
633{
634 unsigned desired_cursor;
635 __dpmi_regs regs;
636 int max_line, top_line, bot_line;
637 struct tty_display_info *tty = FRAME_TTY (f);
638
639 /* Avoid the costly BIOS call if F isn't the currently selected
640 frame. Allow for NULL as unconditionally meaning the selected
641 frame. */
642 if (f && f != SELECTED_FRAME ())
643 return;
644
645 if (tty->termscript)
646 fprintf (tty->termscript, "\nCURSOR SHAPE=(%d,%d)", start_line, width);
647
648 /* The character cell size in scan lines is stored at 40:85 in the
649 BIOS data area. */
650 max_line = _farpeekw (_dos_ds, 0x485) - 1;
651 switch (max_line)
652 {
653 default: /* this relies on CGA cursor emulation being ON! */
654 case 7:
655 bot_line = 7;
656 break;
657 case 9:
658 bot_line = 9;
659 break;
660 case 13:
661 bot_line = 12;
662 break;
663 case 15:
664 bot_line = 14;
665 break;
666 }
667
668 if (width < 0)
669 {
670 if (width == BOX_CURSOR_WIDTH)
671 {
672 top_line = 0;
673 bot_line = max_line;
674 }
675 else if (start_line != DEFAULT_CURSOR_START)
676 {
677 top_line = start_line;
678 bot_line = top_line - width - 1;
679 }
680 else if (width != DEFAULT_CURSOR_WIDTH)
681 {
682 top_line = 0;
683 bot_line = -1 - width;
684 }
685 else
686 top_line = bot_line + 1;
687 }
688 else if (width == 0)
689 {
690 /* [31, 0] seems to DTRT for all screen sizes. */
691 top_line = 31;
692 bot_line = 0;
693 }
694 else /* WIDTH is positive */
695 {
696 if (start_line != DEFAULT_CURSOR_START)
697 bot_line = start_line;
698 top_line = bot_line - (width - 1);
699 }
700
701 /* If the current cursor shape is already what they want, we are
702 history here. */
703 desired_cursor = ((top_line & 0x1f) << 8) | (bot_line & 0x1f);
704 if (desired_cursor == _farpeekw (_dos_ds, 0x460))
705 return;
706
707 regs.h.ah = 1;
708 regs.x.cx = desired_cursor;
709 __dpmi_int (0x10, &regs);
710}
711
712static void
713IT_set_cursor_type (struct frame *f, Lisp_Object cursor_type)
714{
715 if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
716 {
717 /* Just BAR means the normal EGA/VGA cursor. */
718 msdos_set_cursor_shape (f, DEFAULT_CURSOR_START, DEFAULT_CURSOR_WIDTH);
719 }
720 else if (CONSP (cursor_type)
721 && (EQ (XCAR (cursor_type), Qbar)
722 || EQ (XCAR (cursor_type), Qhbar)))
723 {
724 Lisp_Object bar_parms = XCDR (cursor_type);
725 int width;
726
727 if (INTEGERP (bar_parms))
728 {
729 /* Feature: negative WIDTH means cursor at the top
730 of the character cell, zero means invisible cursor. */
731 width = XINT (bar_parms);
732 msdos_set_cursor_shape (f, width >= 0 ? DEFAULT_CURSOR_START : 0,
733 width);
734 }
735 else if (CONSP (bar_parms)
736 && INTEGERP (XCAR (bar_parms))
737 && INTEGERP (XCDR (bar_parms)))
738 {
739 int start_line = XINT (XCDR (bar_parms));
740
741 width = XINT (XCAR (bar_parms));
742 msdos_set_cursor_shape (f, start_line, width);
743 }
744 }
745 else
746 {
747 /* Treat anything unknown as "box cursor". This includes nil, so
748 that a frame which doesn't specify a cursor type gets a box,
749 which is the default in Emacs. */
750 msdos_set_cursor_shape (f, 0, BOX_CURSOR_WIDTH);
751 }
752}
753
754static void
755IT_ring_bell (struct frame *f)
756{
757 if (visible_bell)
758 {
759 mouse_off ();
760 ScreenVisualBell ();
761 }
762 else
763 {
764 union REGS inregs, outregs;
765 inregs.h.ah = 2;
766 inregs.h.dl = 7;
767 intdos (&inregs, &outregs);
768 }
769}
770
771/* Given a face id FACE, extract the face parameters to be used for
772 display until the face changes. The face parameters (actually, its
773 color) are used to construct the video attribute byte for each
774 glyph during the construction of the buffer that is then blitted to
775 the video RAM. */
776static void
777IT_set_face (int face)
778{
779 struct frame *sf = SELECTED_FRAME ();
780 struct face *fp = FACE_FROM_ID (sf, face);
781 struct face *dfp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
782 unsigned long fg, bg, dflt_fg, dflt_bg;
783 struct tty_display_info *tty = FRAME_TTY (sf);
784
785 if (!fp)
786 {
787 fp = dfp;
788 /* The default face for the frame should always be realized and
789 cached. */
790 if (!fp)
791 abort ();
792 }
793 screen_face = face;
794 fg = fp->foreground;
795 bg = fp->background;
796 dflt_fg = dfp->foreground;
797 dflt_bg = dfp->background;
798
799 /* Don't use invalid colors. In particular, FACE_TTY_DEFAULT_* colors
800 mean use the colors of the default face. Note that we assume all
801 16 colors to be available for the background, since Emacs switches
802 on this mode (and loses the blinking attribute) at startup. */
803 if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
804 fg = FRAME_FOREGROUND_PIXEL (sf);
805 else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
806 fg = FRAME_BACKGROUND_PIXEL (sf);
807 if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
808 bg = FRAME_BACKGROUND_PIXEL (sf);
809 else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
810 bg = FRAME_FOREGROUND_PIXEL (sf);
811
812 /* Make sure highlighted lines really stand out, come what may. */
813 if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
814 {
815 unsigned long tem = fg;
816
817 fg = bg;
818 bg = tem;
819 }
820 /* If the user requested inverse video, obey. */
821 if (inverse_video)
822 {
823 unsigned long tem2 = fg;
824
825 fg = bg;
826 bg = tem2;
827 }
828 if (tty->termscript)
829 fprintf (tty->termscript, "<FACE %d: %lu/%lu[FG:%lu/BG:%lu]>", face,
830 fp->foreground, fp->background, fg, bg);
831 if (fg >= 0 && fg < 16)
832 {
833 ScreenAttrib &= 0xf0;
834 ScreenAttrib |= fg;
835 }
836 if (bg >= 0 && bg < 16)
837 {
838 ScreenAttrib &= 0x0f;
839 ScreenAttrib |= ((bg & 0x0f) << 4);
840 }
841}
842
843/* According to RBIL (INTERRUP.A, V-1000), 160 is the maximum possible
844 width of a DOS display in any known text mode. We multiply by 2 to
845 accommodate the screen attribute byte. */
846#define MAX_SCREEN_BUF 160*2
847
848extern unsigned char *encode_terminal_code (struct glyph *, int,
849 struct coding_system *);
850
851static void
852IT_write_glyphs (struct frame *f, struct glyph *str, int str_len)
853{
854 unsigned char screen_buf[MAX_SCREEN_BUF], *screen_bp, *bp;
855 int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
856 register int sl = str_len;
857 struct tty_display_info *tty = FRAME_TTY (f);
858 struct frame *sf;
859 unsigned char *conversion_buffer;
860
861 /* If terminal_coding does any conversion, use it, otherwise use
862 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
863 because it always returns 1 if terminal_coding.src_multibyte is 1. */
864 struct coding_system *coding = FRAME_TERMINAL_CODING (f);
865
866 if (!(coding->common_flags & CODING_REQUIRE_ENCODING_MASK))
867 coding = &safe_terminal_coding;
868
869 if (str_len <= 0) return;
870
871 sf = SELECTED_FRAME ();
872
873 /* Since faces get cached and uncached behind our back, we can't
874 rely on their indices in the cache being consistent across
875 invocations. So always reset the screen face to the default
876 face of the frame, before writing glyphs, and let the glyphs
877 set the right face if it's different from the default. */
878 IT_set_face (DEFAULT_FACE_ID);
879
880 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
881 the tail. */
882 coding->mode &= ~CODING_MODE_LAST_BLOCK;
883 screen_bp = &screen_buf[0];
884 while (sl > 0)
885 {
886 int cf;
887 int n;
888
889 /* If the face of this glyph is different from the current
890 screen face, update the screen attribute byte. */
891 cf = str->face_id;
892 if (cf != screen_face)
893 IT_set_face (cf); /* handles invalid faces gracefully */
894
895 /* Identify a run of glyphs with the same face. */
896 for (n = 1; n < sl; ++n)
897 if (str[n].face_id != cf)
898 break;
899
900 if (n >= sl)
901 /* This is the last glyph. */
902 coding->mode |= CODING_MODE_LAST_BLOCK;
903
904 conversion_buffer = encode_terminal_code (str, n, coding);
905 if (coding->produced > 0)
906 {
907 /* Copy the encoded bytes to the screen buffer. */
908 for (bp = conversion_buffer; coding->produced--; bp++)
909 {
910 /* Paranoia: discard bytes that would overrun the end of
911 the screen buffer. */
912 if (screen_bp - screen_buf <= MAX_SCREEN_BUF - 2)
913 {
914 *screen_bp++ = (unsigned char)*bp;
915 *screen_bp++ = ScreenAttrib;
916 }
917 if (tty->termscript)
918 fputc (*bp, tty->termscript);
919 }
920 }
921 /* Update STR and its remaining length. */
922 str += n;
923 sl -= n;
924 }
925
926 /* Dump whatever we have in the screen buffer. */
927 mouse_off_maybe ();
928 dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
929 if (screen_virtual_segment)
930 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
931 new_pos_X += (screen_bp - screen_buf) / 2;
932}
933
934/************************************************************************
935 Mouse Highlight (and friends..)
936 ************************************************************************/
937
938/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
939static Lisp_Object last_mouse_window;
940
941static int mouse_preempted = 0; /* non-zero when XMenu gobbles mouse events */
942
943int
944popup_activated (void)
945{
946 return mouse_preempted;
947}
948
949/* Draw TEXT_AREA glyphs between START and END of glyph row ROW on
950 window W. X is relative to TEXT_AREA in W. HL is a face override
951 for drawing the glyphs. */
952void
953tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
954 int start_hpos, int end_hpos,
955 enum draw_glyphs_face hl)
956{
957 struct frame *f = XFRAME (WINDOW_FRAME (w));
958 struct tty_display_info *tty = FRAME_TTY (f);
959 Mouse_HLInfo *hlinfo = &tty->mouse_highlight;
960
961 if (hl == DRAW_MOUSE_FACE)
962 {
963 int vpos = row->y + WINDOW_TOP_EDGE_Y (w);
964 int kstart = start_hpos + WINDOW_LEFT_EDGE_X (w);
965 int nglyphs = end_hpos - start_hpos;
966 int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
967 int start_offset = offset;
968
969 if (tty->termscript)
970 fprintf (tty->termscript, "\n<MH+ %d-%d:%d>",
971 kstart, kstart + nglyphs - 1, vpos);
972
973 mouse_off ();
974 IT_set_face (hlinfo->mouse_face_face_id);
975 /* Since we are going to change only the _colors_ of already
976 displayed text, there's no need to go through all the pain of
977 generating and encoding the text from the glyphs. Instead,
978 we simply poke the attribute byte of each affected position
979 in video memory with the colors computed by IT_set_face! */
980 _farsetsel (_dos_ds);
981 while (nglyphs--)
982 {
983 _farnspokeb (offset, ScreenAttrib);
984 offset += 2;
985 }
986 if (screen_virtual_segment)
987 dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
988 mouse_on ();
989 }
990 else if (hl == DRAW_NORMAL_TEXT)
991 {
992 /* We are removing a previously-drawn mouse highlight. The
993 safest way to do so is to redraw the glyphs anew, since all
994 kinds of faces and display tables could have changed behind
995 our back. */
996 int nglyphs = end_hpos - start_hpos;
997 int save_x = new_pos_X, save_y = new_pos_Y;
998
999 if (end_hpos >= row->used[TEXT_AREA])
1000 nglyphs = row->used[TEXT_AREA] - start_hpos;
1001
1002 /* IT_write_glyphs writes at cursor position, so we need to
1003 temporarily move cursor coordinates to the beginning of
1004 the highlight region. */
1005 new_pos_X = start_hpos + WINDOW_LEFT_EDGE_X (w);
1006 new_pos_Y = row->y + WINDOW_TOP_EDGE_Y (w);
1007
1008 if (tty->termscript)
1009 fprintf (tty->termscript, "<MH- %d-%d:%d>",
1010 new_pos_X, new_pos_X + nglyphs - 1, new_pos_Y);
1011 IT_write_glyphs (f, row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
1012 if (tty->termscript)
1013 fputs ("\n", tty->termscript);
1014 new_pos_X = save_x;
1015 new_pos_Y = save_y;
1016 }
1017}
1018
1019static void
1020IT_clear_end_of_line (struct frame *f, int first_unused)
1021{
1022 char *spaces, *sp;
1023 int i, j, offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
1024 extern int fatal_error_in_progress;
1025 struct tty_display_info *tty = FRAME_TTY (f);
1026
1027 if (new_pos_X >= first_unused || fatal_error_in_progress)
1028 return;
1029
1030 IT_set_face (0);
1031 i = (j = first_unused - new_pos_X) * 2;
1032 if (tty->termscript)
1033 fprintf (tty->termscript, "<CLR:EOL[%d..%d)>", new_pos_X, first_unused);
1034 spaces = sp = alloca (i);
1035
1036 while (--j >= 0)
1037 {
1038 *sp++ = ' ';
1039 *sp++ = ScreenAttrib;
1040 }
1041
1042 mouse_off_maybe ();
1043 dosmemput (spaces, i, (int)ScreenPrimary + offset);
1044 if (screen_virtual_segment)
1045 dosv_refresh_virtual_screen (offset, i / 2);
1046
1047 /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
1048 Let's follow their lead, in case someone relies on this. */
1049 new_pos_X = first_unused;
1050}
1051
1052static void
1053IT_clear_screen (struct frame *f)
1054{
1055 struct tty_display_info *tty = FRAME_TTY (f);
1056
1057 if (tty->termscript)
1058 fprintf (tty->termscript, "<CLR:SCR>");
1059 /* We are sometimes called (from clear_garbaged_frames) when a new
1060 frame is being created, but its faces are not yet realized. In
1061 such a case we cannot call IT_set_face, since it will fail to find
1062 any valid faces and will abort. Instead, use the initial screen
1063 colors; that should mimic what a Unix tty does, which simply clears
1064 the screen with whatever default colors are in use. */
1065 if (FACE_FROM_ID (SELECTED_FRAME (), DEFAULT_FACE_ID) == NULL)
1066 ScreenAttrib = (initial_screen_colors[0] << 4) | initial_screen_colors[1];
1067 else
1068 IT_set_face (0);
1069 mouse_off ();
1070 ScreenClear ();
1071 if (screen_virtual_segment)
1072 dosv_refresh_virtual_screen (0, screen_size);
1073 new_pos_X = new_pos_Y = 0;
1074}
1075
1076static void
1077IT_clear_to_end (struct frame *f)
1078{
1079 struct tty_display_info *tty = FRAME_TTY (f);
1080
1081 if (tty->termscript)
1082 fprintf (tty->termscript, "<CLR:EOS>");
1083
1084 while (new_pos_Y < screen_size_Y) {
1085 new_pos_X = 0;
1086 IT_clear_end_of_line (f, screen_size_X);
1087 new_pos_Y++;
1088 }
1089}
1090
1091static void
1092IT_cursor_to (struct frame *f, int y, int x)
1093{
1094 struct tty_display_info *tty = FRAME_TTY (f);
1095
1096 if (tty->termscript)
1097 fprintf (tty->termscript, "\n<XY=%dx%d>", x, y);
1098 new_pos_X = x;
1099 new_pos_Y = y;
1100}
1101
1102static int cursor_cleared;
1103
1104static void
1105IT_display_cursor (int on)
1106{
1107 struct tty_display_info *tty = CURTTY ();
1108
1109 if (on && cursor_cleared)
1110 {
1111 ScreenSetCursor (current_pos_Y, current_pos_X);
1112 cursor_cleared = 0;
1113 if (tty->termscript)
1114 fprintf (tty->termscript, "\nCURSOR ON (%dx%d)",
1115 current_pos_Y, current_pos_X);
1116 }
1117 else if (!on && !cursor_cleared)
1118 {
1119 ScreenSetCursor (-1, -1);
1120 cursor_cleared = 1;
1121 if (tty->termscript)
1122 fprintf (tty->termscript, "\nCURSOR OFF (%dx%d)",
1123 current_pos_Y, current_pos_X);
1124 }
1125}
1126
1127/* Emacs calls cursor-movement functions a lot when it updates the
1128 display (probably a legacy of old terminals where you cannot
1129 update a screen line without first moving the cursor there).
1130 However, cursor movement is expensive on MSDOS (it calls a slow
1131 BIOS function and requires 2 mode switches), while actual screen
1132 updates access the video memory directly and don't depend on
1133 cursor position. To avoid slowing down the redisplay, we cheat:
1134 all functions that move the cursor only set internal variables
1135 which record the cursor position, whereas the cursor is only
1136 moved to its final position whenever screen update is complete.
1137
1138 `IT_cmgoto' is called from the keyboard reading loop and when the
1139 frame update is complete. This means that we are ready for user
1140 input, so we update the cursor position to show where the point is,
1141 and also make the mouse pointer visible.
1142
1143 Special treatment is required when the cursor is in the echo area,
1144 to put the cursor at the end of the text displayed there. */
1145
1146static void
1147IT_cmgoto (FRAME_PTR f)
1148{
1149 /* Only set the cursor to where it should be if the display is
1150 already in sync with the window contents. */
1151 int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */
1152 struct tty_display_info *tty = FRAME_TTY (f);
1153
1154 /* FIXME: This needs to be rewritten for the new redisplay, or
1155 removed. */
1156#if 0
1157 static int previous_pos_X = -1;
1158
1159 update_cursor_pos = 1; /* temporary!!! */
1160
1161 /* If the display is in sync, forget any previous knowledge about
1162 cursor position. This is primarily for unexpected events like
1163 C-g in the minibuffer. */
1164 if (update_cursor_pos && previous_pos_X >= 0)
1165 previous_pos_X = -1;
1166 /* If we are in the echo area, put the cursor at the
1167 end of the echo area message. */
1168 if (!update_cursor_pos
1169 && WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f))) <= new_pos_Y)
1170 {
1171 int tem_X = current_pos_X, dummy;
1172
1173 if (echo_area_glyphs)
1174 {
1175 tem_X = echo_area_glyphs_length;
1176 /* Save current cursor position, to be restored after the
1177 echo area message is erased. Only remember one level
1178 of previous cursor position. */
1179 if (previous_pos_X == -1)
1180 ScreenGetCursor (&dummy, &previous_pos_X);
1181 }
1182 else if (previous_pos_X >= 0)
1183 {
1184 /* We wind up here after the echo area message is erased.
1185 Restore the cursor position we remembered above. */
1186 tem_X = previous_pos_X;
1187 previous_pos_X = -1;
1188 }
1189
1190 if (current_pos_X != tem_X)
1191 {
1192 new_pos_X = tem_X;
1193 update_cursor_pos = 1;
1194 }
1195 }
1196#endif
1197
1198 if (update_cursor_pos
1199 && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
1200 {
1201 ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
1202 if (tty->termscript)
1203 fprintf (tty->termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
1204 }
1205
1206 /* Maybe cursor is invisible, so make it visible. */
1207 IT_display_cursor (1);
1208
1209 /* Mouse pointer should be always visible if we are waiting for
1210 keyboard input. */
1211 if (!mouse_visible)
1212 mouse_on ();
1213}
1214
1215static void
1216IT_update_begin (struct frame *f)
1217{
1218 struct tty_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
1219 Mouse_HLInfo *hlinfo = &display_info->mouse_highlight;
1220 struct frame *mouse_face_frame = hlinfo->mouse_face_mouse_frame;
1221
1222 if (display_info->termscript)
1223 fprintf (display_info->termscript, "\n\n<UPDATE_BEGIN");
1224
1225 BLOCK_INPUT;
1226
1227 if (f && f == mouse_face_frame)
1228 {
1229 /* Don't do highlighting for mouse motion during the update. */
1230 hlinfo->mouse_face_defer = 1;
1231
1232 /* If F needs to be redrawn, simply forget about any prior mouse
1233 highlighting. */
1234 if (FRAME_GARBAGED_P (f))
1235 hlinfo->mouse_face_window = Qnil;
1236
1237 /* Can we tell that this update does not affect the window
1238 where the mouse highlight is? If so, no need to turn off.
1239 Likewise, don't do anything if none of the enabled rows
1240 contains glyphs highlighted in mouse face. */
1241 if (!NILP (hlinfo->mouse_face_window)
1242 && WINDOWP (hlinfo->mouse_face_window))
1243 {
1244 struct window *w = XWINDOW (hlinfo->mouse_face_window);
1245 int i;
1246
1247 /* If the mouse highlight is in the window that was deleted
1248 (e.g., if it was popped by completion), clear highlight
1249 unconditionally. */
1250 if (NILP (w->buffer))
1251 hlinfo->mouse_face_window = Qnil;
1252 else
1253 {
1254 for (i = 0; i < w->desired_matrix->nrows; ++i)
1255 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i)
1256 && MATRIX_ROW (w->current_matrix, i)->mouse_face_p)
1257 break;
1258 }
1259
1260 if (NILP (w->buffer) || i < w->desired_matrix->nrows)
1261 clear_mouse_face (hlinfo);
1262 }
1263 }
1264 else if (mouse_face_frame && !FRAME_LIVE_P (mouse_face_frame))
1265 {
1266 /* If the frame with mouse highlight was deleted, invalidate the
1267 highlight info. */
1268 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
1269 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
1270 hlinfo->mouse_face_window = Qnil;
1271 hlinfo->mouse_face_deferred_gc = 0;
1272 hlinfo->mouse_face_mouse_frame = NULL;
1273 }
1274
1275 UNBLOCK_INPUT;
1276}
1277
1278static void
1279IT_update_end (struct frame *f)
1280{
1281 struct tty_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1282
1283 if (dpyinfo->termscript)
1284 fprintf (dpyinfo->termscript, "\n<UPDATE_END\n");
1285 dpyinfo->mouse_highlight.mouse_face_defer = 0;
1286}
1287
1288static void
1289IT_frame_up_to_date (struct frame *f)
1290{
1291 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
1292 Lisp_Object new_cursor, frame_desired_cursor;
1293 struct window *sw;
1294
1295 if (hlinfo->mouse_face_deferred_gc
1296 || (f && f == hlinfo->mouse_face_mouse_frame))
1297 {
1298 BLOCK_INPUT;
1299 if (hlinfo->mouse_face_mouse_frame)
1300 note_mouse_highlight (hlinfo->mouse_face_mouse_frame,
1301 hlinfo->mouse_face_mouse_x,
1302 hlinfo->mouse_face_mouse_y);
1303 hlinfo->mouse_face_deferred_gc = 0;
1304 UNBLOCK_INPUT;
1305 }
1306
1307 /* Set the cursor type to whatever they wanted. In a minibuffer
1308 window, we want the cursor to appear only if we are reading input
1309 from this window, and we want the cursor to be taken from the
1310 frame parameters. For the selected window, we use either its
1311 buffer-local value or the value from the frame parameters if the
1312 buffer doesn't define its local value for the cursor type. */
1313 sw = XWINDOW (f->selected_window);
1314 frame_desired_cursor = Fcdr (Fassq (Qcursor_type, f->param_alist));
1315 if (cursor_in_echo_area
1316 && FRAME_HAS_MINIBUF_P (f)
1317 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window)
1318 && sw == XWINDOW (echo_area_window))
1319 new_cursor = frame_desired_cursor;
1320 else
1321 {
1322 struct buffer *b = XBUFFER (sw->buffer);
1323
1324 if (EQ (BVAR (b,cursor_type), Qt))
1325 new_cursor = frame_desired_cursor;
1326 else if (NILP (BVAR (b, cursor_type))) /* nil means no cursor */
1327 new_cursor = Fcons (Qbar, make_number (0));
1328 else
1329 new_cursor = BVAR (b, cursor_type);
1330 }
1331
1332 IT_set_cursor_type (f, new_cursor);
1333
1334 IT_cmgoto (f); /* position cursor when update is done */
1335}
1336
1337/* Copy LEN glyphs displayed on a single line whose vertical position
1338 is YPOS, beginning at horizontal position XFROM to horizontal
1339 position XTO, by moving blocks in the video memory. Used by
1340 functions that insert and delete glyphs. */
1341static void
1342IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
1343{
1344 /* The offsets of source and destination relative to the
1345 conventional memory selector. */
1346 int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
1347 int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;
1348
1349 if (from == to || len <= 0)
1350 return;
1351
1352 _farsetsel (_dos_ds);
1353
1354 /* The source and destination might overlap, so we need to move
1355 glyphs non-destructively. */
1356 if (from > to)
1357 {
1358 for ( ; len; from += 2, to += 2, len--)
1359 _farnspokew (to, _farnspeekw (from));
1360 }
1361 else
1362 {
1363 from += (len - 1) * 2;
1364 to += (len - 1) * 2;
1365 for ( ; len; from -= 2, to -= 2, len--)
1366 _farnspokew (to, _farnspeekw (from));
1367 }
1368 if (screen_virtual_segment)
1369 dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
1370}
1371
1372/* Insert and delete glyphs. */
1373static void
1374IT_insert_glyphs (struct frame *f, struct glyph *start, int len)
1375{
1376 int shift_by_width = screen_size_X - (new_pos_X + len);
1377
1378 /* Shift right the glyphs from the nominal cursor position to the
1379 end of this line. */
1380 IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);
1381
1382 /* Now write the glyphs to be inserted. */
1383 IT_write_glyphs (f, start, len);
1384}
1385
1386static void
1387IT_delete_glyphs (struct frame *f, int n)
1388{
1389 abort ();
1390}
1391
1392/* set-window-configuration on window.c needs this. */
1393void
1394x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1395{
1396 set_menu_bar_lines (f, value, oldval);
1397}
1398
1399/* This was copied from xfaces.c */
1400
1401extern Lisp_Object Qbackground_color;
1402extern Lisp_Object Qforeground_color;
1403Lisp_Object Qreverse;
1404extern Lisp_Object Qtitle;
1405
1406/* IT_set_terminal_modes is called when emacs is started,
1407 resumed, and whenever the screen is redrawn! */
1408
1409static void
1410IT_set_terminal_modes (struct terminal *term)
1411{
1412 struct tty_display_info *tty;
1413
1414 /* If called with initial terminal, it's too early to do anything
1415 useful. */
1416 if (term->type == output_initial)
1417 return;
1418
1419 tty = term->display_info.tty;
1420
1421 if (tty->termscript)
1422 fprintf (tty->termscript, "\n<SET_TERM>");
1423
1424 screen_size_X = ScreenCols ();
1425 screen_size_Y = ScreenRows ();
1426 screen_size = screen_size_X * screen_size_Y;
1427
1428 new_pos_X = new_pos_Y = 0;
1429 current_pos_X = current_pos_Y = -1;
1430
1431 if (term_setup_done)
1432 return;
1433 term_setup_done = 1;
1434
1435 startup_screen_size_X = screen_size_X;
1436 startup_screen_size_Y = screen_size_Y;
1437 startup_screen_attrib = ScreenAttrib;
1438
1439 /* Is DOS/V (or any other RSIS software which relocates
1440 the screen) installed? */
1441 {
1442 unsigned short es_value;
1443 __dpmi_regs regs;
1444
1445 regs.h.ah = 0xfe; /* get relocated screen address */
1446 if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
1447 regs.x.es = (ScreenPrimary >> 4) & 0xffff;
1448 else if (screen_old_address) /* already switched to Japanese mode once */
1449 regs.x.es = (screen_old_address >> 4) & 0xffff;
1450 else
1451 regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
1452 regs.x.di = 0;
1453 es_value = regs.x.es;
1454 __dpmi_int (0x10, &regs);
1455
1456 if (regs.x.es != es_value)
1457 {
1458 /* screen_old_address is only set if ScreenPrimary does NOT
1459 already point to the relocated buffer address returned by
1460 the Int 10h/AX=FEh call above. DJGPP v2.02 and later sets
1461 ScreenPrimary to that address at startup under DOS/V. */
1462 if (regs.x.es != ((ScreenPrimary >> 4) & 0xffff))
1463 screen_old_address = ScreenPrimary;
1464 screen_virtual_segment = regs.x.es;
1465 screen_virtual_offset = regs.x.di;
1466 ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
1467 }
1468 }
1469
1470 ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
1471 ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));
1472
1473 bright_bg ();
1474}
1475
1476/* IT_reset_terminal_modes is called when emacs is
1477 suspended or killed. */
1478
1479static void
1480IT_reset_terminal_modes (struct terminal *term)
1481{
1482 int display_row_start = (int) ScreenPrimary;
1483 int saved_row_len = startup_screen_size_X * 2;
1484 int update_row_len = ScreenCols () * 2, current_rows = ScreenRows ();
1485 int to_next_row = update_row_len;
1486 unsigned char *saved_row = startup_screen_buffer;
1487 int cursor_pos_X = ScreenCols () - 1, cursor_pos_Y = ScreenRows () - 1;
1488 struct tty_display_info *tty = term->display_info.tty;
1489
1490 if (tty->termscript)
1491 fprintf (tty->termscript, "\n<RESET_TERM>");
1492
1493 if (!term_setup_done)
1494 return;
1495
1496 mouse_off ();
1497
1498 /* Leave the video system in the same state as we found it,
1499 as far as the blink/bright-background bit is concerned. */
1500 maybe_enable_blinking ();
1501
1502 /* We have a situation here.
1503 We cannot just do ScreenUpdate(startup_screen_buffer) because
1504 the luser could have changed screen dimensions inside Emacs
1505 and failed (or didn't want) to restore them before killing
1506 Emacs. ScreenUpdate() uses the *current* screen dimensions and
1507 thus will happily use memory outside what was allocated for
1508 `startup_screen_buffer'.
1509 Thus we only restore as much as the current screen dimensions
1510 can hold, and clear the rest (if the saved screen is smaller than
1511 the current) with the color attribute saved at startup. The cursor
1512 is also restored within the visible dimensions. */
1513
1514 ScreenAttrib = startup_screen_attrib;
1515
1516 /* Don't restore the screen if we are exiting less than 2 seconds
1517 after startup: we might be crashing, and the screen might show
1518 some vital clues to what's wrong. */
1519 if (clock () - startup_time >= 2*CLOCKS_PER_SEC)
1520 {
1521 ScreenClear ();
1522 if (screen_virtual_segment)
1523 dosv_refresh_virtual_screen (0, screen_size);
1524
1525 if (update_row_len > saved_row_len)
1526 update_row_len = saved_row_len;
1527 if (current_rows > startup_screen_size_Y)
1528 current_rows = startup_screen_size_Y;
1529
1530 if (tty->termscript)
1531 fprintf (tty->termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
1532 update_row_len / 2, current_rows);
1533
1534 while (current_rows--)
1535 {
1536 dosmemput (saved_row, update_row_len, display_row_start);
1537 if (screen_virtual_segment)
1538 dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
1539 update_row_len / 2);
1540 saved_row += saved_row_len;
1541 display_row_start += to_next_row;
1542 }
1543 }
1544 if (startup_pos_X < cursor_pos_X)
1545 cursor_pos_X = startup_pos_X;
1546 if (startup_pos_Y < cursor_pos_Y)
1547 cursor_pos_Y = startup_pos_Y;
1548
1549 ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
1550 xfree (startup_screen_buffer);
1551 startup_screen_buffer = NULL;
1552
1553 term_setup_done = 0;
1554}
1555
1556static void
1557IT_set_terminal_window (struct frame *f, int foo)
1558{
1559}
1560
1561/* Remember the screen colors of the current frame, to serve as the
1562 default colors for newly-created frames. */
1563DEFUN ("msdos-remember-default-colors", Fmsdos_remember_default_colors,
1564 Smsdos_remember_default_colors, 1, 1, 0,
1565 doc: /* Remember the screen colors of the current frame. */)
1566 (Lisp_Object frame)
1567{
1568 struct frame *f;
1569
1570 CHECK_FRAME (frame);
1571 f = XFRAME (frame);
1572
1573 /* This function is called after applying default-frame-alist to the
1574 initial frame. At that time, if reverse-colors option was
1575 specified in default-frame-alist, it was already applied, and
1576 frame colors are reversed. */
1577 initial_screen_colors[0] = FRAME_FOREGROUND_PIXEL (f);
1578 initial_screen_colors[1] = FRAME_BACKGROUND_PIXEL (f);
1579
1580 return Qnil;
1581}
1582
1583void
1584IT_set_frame_parameters (struct frame *f, Lisp_Object alist)
1585{
1586 Lisp_Object tail;
1587 int i, j, length = XINT (Flength (alist));
1588 Lisp_Object *parms
1589 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
1590 Lisp_Object *values
1591 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
1592 /* Do we have to reverse the foreground and background colors? */
1593 int reverse = EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt);
1594 int redraw = 0, fg_set = 0, bg_set = 0;
1595 unsigned long orig_fg, orig_bg;
1596 struct tty_display_info *tty = FRAME_TTY (f);
1597
1598 /* If we are creating a new frame, begin with the original screen colors
1599 used for the initial frame. */
1600 if (!f->default_face_done_p
1601 && initial_screen_colors[0] != -1 && initial_screen_colors[1] != -1)
1602 {
1603 FRAME_FOREGROUND_PIXEL (f) = initial_screen_colors[0];
1604 FRAME_BACKGROUND_PIXEL (f) = initial_screen_colors[1];
1605 init_frame_faces (f);
1606 f->default_face_done_p = 1;
1607 }
1608 orig_fg = reverse ? FRAME_BACKGROUND_PIXEL (f) : FRAME_FOREGROUND_PIXEL (f);
1609 orig_bg = reverse ? FRAME_FOREGROUND_PIXEL (f) : FRAME_BACKGROUND_PIXEL (f);
1610
1611 /* Extract parm names and values into those vectors. */
1612 i = 0;
1613 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
1614 {
1615 Lisp_Object elt;
1616
1617 elt = Fcar (tail);
1618 parms[i] = Fcar (elt);
1619 CHECK_SYMBOL (parms[i]);
1620 values[i] = Fcdr (elt);
1621 i++;
1622 }
1623
1624 j = i;
1625
1626 for (i = 0; i < j; i++)
1627 {
1628 Lisp_Object prop, val;
1629
1630 prop = parms[i];
1631 val = values[i];
1632
1633 if (EQ (prop, Qreverse))
1634 reverse = EQ (val, Qt);
1635 }
1636
1637 if (tty->termscript && reverse)
1638 fprintf (tty->termscript, "<INVERSE-VIDEO>\n");
1639
1640 /* Now process the alist elements in reverse of specified order. */
1641 for (i--; i >= 0; i--)
1642 {
1643 Lisp_Object prop, val;
1644
1645 prop = parms[i];
1646 val = values[i];
1647
1648 if (EQ (prop, Qforeground_color))
1649 {
1650 unsigned long new_color = load_color (f, NULL, val, reverse
1651 ? LFACE_BACKGROUND_INDEX
1652 : LFACE_FOREGROUND_INDEX);
1653 if (new_color != FACE_TTY_DEFAULT_COLOR
1654 && new_color != FACE_TTY_DEFAULT_FG_COLOR
1655 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
1656 {
1657 if (!reverse)
1658 {
1659 FRAME_FOREGROUND_PIXEL (f) = new_color;
1660 /* Make sure the foreground of the default face for
1661 this frame is changed as well. */
1662 update_face_from_frame_parameter (f, Qforeground_color, val);
1663 fg_set = 1;
1664 if (tty->termscript)
1665 fprintf (tty->termscript, "<FGCOLOR %lu>\n", new_color);
1666 }
1667 else
1668 {
1669 FRAME_BACKGROUND_PIXEL (f) = new_color;
1670 update_face_from_frame_parameter (f, Qbackground_color, val);
1671 bg_set = 1;
1672 if (tty->termscript)
1673 fprintf (tty->termscript, "<BGCOLOR %lu>\n", new_color);
1674 }
1675 redraw = 1;
1676 }
1677 }
1678 else if (EQ (prop, Qbackground_color))
1679 {
1680 unsigned long new_color = load_color (f, NULL, val, reverse
1681 ? LFACE_FOREGROUND_INDEX
1682 : LFACE_BACKGROUND_INDEX);
1683 if (new_color != FACE_TTY_DEFAULT_COLOR
1684 && new_color != FACE_TTY_DEFAULT_FG_COLOR
1685 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
1686 {
1687 if (!reverse)
1688 {
1689 FRAME_BACKGROUND_PIXEL (f) = new_color;
1690 /* Make sure the background of the default face for
1691 this frame is changed as well. */
1692 bg_set = 1;
1693 update_face_from_frame_parameter (f, Qbackground_color, val);
1694 if (tty->termscript)
1695 fprintf (tty->termscript, "<BGCOLOR %lu>\n", new_color);
1696 }
1697 else
1698 {
1699 FRAME_FOREGROUND_PIXEL (f) = new_color;
1700 fg_set = 1;
1701 update_face_from_frame_parameter (f, Qforeground_color, val);
1702 if (tty->termscript)
1703 fprintf (tty->termscript, "<FGCOLOR %lu>\n", new_color);
1704 }
1705 redraw = 1;
1706 }
1707 }
1708 else if (EQ (prop, Qtitle))
1709 {
1710 x_set_title (f, val);
1711 if (tty->termscript)
1712 fprintf (tty->termscript, "<TITLE: %s>\n", SDATA (val));
1713 }
1714 else if (EQ (prop, Qcursor_type))
1715 {
1716 IT_set_cursor_type (f, val);
1717 if (tty->termscript)
1718 fprintf (tty->termscript, "<CTYPE: %s>\n",
1719 EQ (val, Qbar)
1720 || EQ (val, Qhbar)
1721 || (CONSP (val) && (EQ (XCAR (val), Qbar)
1722 || EQ (XCAR (val), Qhbar)))
1723 ? "bar" : "box");
1724 }
1725 else if (EQ (prop, Qtty_type))
1726 {
1727 internal_terminal_init ();
1728 if (tty->termscript)
1729 fprintf (tty->termscript, "<TERM_INIT done, TTY_TYPE: %.*s>\n",
1730 SBYTES (val), SDATA (val));
1731 }
1732 store_frame_param (f, prop, val);
1733 }
1734
1735 /* If they specified "reverse", but not the colors, we need to swap
1736 the current frame colors. */
1737 if (reverse)
1738 {
1739 if (!fg_set)
1740 {
1741 FRAME_FOREGROUND_PIXEL (f) = orig_bg;
1742 update_face_from_frame_parameter (f, Qforeground_color,
1743 tty_color_name (f, orig_bg));
1744 redraw = 1;
1745 }
1746 if (!bg_set)
1747 {
1748 FRAME_BACKGROUND_PIXEL (f) = orig_fg;
1749 update_face_from_frame_parameter (f, Qbackground_color,
1750 tty_color_name (f, orig_fg));
1751 redraw = 1;
1752 }
1753 }
1754
1755 if (redraw)
1756 {
1757 face_change_count++; /* forces xdisp.c to recompute basic faces */
1758 if (f == SELECTED_FRAME ())
1759 redraw_frame (f);
1760 }
1761}
1762
1763extern void init_frame_faces (FRAME_PTR);
1764
1765#endif /* !HAVE_X_WINDOWS */
1766
1767
1768/* Do we need the internal terminal? */
1769
1770void
1771internal_terminal_init (void)
1772{
1773 static int init_needed = 1;
1774 char *term = getenv ("TERM"), *colors;
1775 struct frame *sf = SELECTED_FRAME ();
1776 struct tty_display_info *tty;
1777
1778#ifdef HAVE_X_WINDOWS
1779 if (!inhibit_window_system)
1780 return;
1781#endif
1782
1783 /* If this is the initial terminal, we are done here. */
1784 if (sf->output_method == output_initial)
1785 return;
1786
1787 internal_terminal
1788 = (!noninteractive) && term && !strcmp (term, "internal");
1789
1790#ifndef HAVE_X_WINDOWS
1791 if (!internal_terminal || inhibit_window_system)
1792 {
1793 sf->output_method = output_termcap;
1794 return;
1795 }
1796
1797 tty = FRAME_TTY (sf);
1798 KVAR (current_kboard, Vwindow_system) = Qpc;
1799 sf->output_method = output_msdos_raw;
1800 if (init_needed)
1801 {
1802 if (!tty->termscript && getenv ("EMACSTEST"))
1803 tty->termscript = fopen (getenv ("EMACSTEST"), "wt");
1804 if (tty->termscript)
1805 {
1806 time_t now = time (NULL);
1807 struct tm *tnow = localtime (&now);
1808 char tbuf[100];
1809
1810 strftime (tbuf, sizeof (tbuf) - 1, "%a %b %e %Y %H:%M:%S %Z", tnow);
1811 fprintf (tty->termscript, "\nEmacs session started at %s\n", tbuf);
1812 fprintf (tty->termscript, "=====================\n\n");
1813 }
1814
1815 Vinitial_window_system = Qpc;
1816 Vwindow_system_version = make_number (23); /* RE Emacs version */
1817 tty->terminal->type = output_msdos_raw;
1818
1819 /* If Emacs was dumped on DOS/V machine, forget the stale VRAM
1820 address. */
1821 screen_old_address = 0;
1822
1823 /* Forget the stale screen colors as well. */
1824 initial_screen_colors[0] = initial_screen_colors[1] = -1;
1825
1826 FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()) = 7; /* White */
1827 FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ()) = 0; /* Black */
1828 bright_bg ();
1829 colors = getenv ("EMACSCOLORS");
1830 if (colors && strlen (colors) >= 2)
1831 {
1832 /* The colors use 4 bits each (we enable bright background). */
1833 if (isdigit (colors[0]))
1834 colors[0] -= '0';
1835 else if (isxdigit (colors[0]))
1836 colors[0] -= (isupper (colors[0]) ? 'A' : 'a') - 10;
1837 if (colors[0] >= 0 && colors[0] < 16)
1838 FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ()) = colors[0];
1839 if (isdigit (colors[1]))
1840 colors[1] -= '0';
1841 else if (isxdigit (colors[1]))
1842 colors[1] -= (isupper (colors[1]) ? 'A' : 'a') - 10;
1843 if (colors[1] >= 0 && colors[1] < 16)
1844 FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()) = colors[1];
1845 }
1846 the_only_display_info.mouse_highlight.mouse_face_mouse_frame = NULL;
1847 the_only_display_info.mouse_highlight.mouse_face_deferred_gc = 0;
1848 the_only_display_info.mouse_highlight.mouse_face_beg_row =
1849 the_only_display_info.mouse_highlight.mouse_face_beg_col = -1;
1850 the_only_display_info.mouse_highlight.mouse_face_end_row =
1851 the_only_display_info.mouse_highlight.mouse_face_end_col = -1;
1852 the_only_display_info.mouse_highlight.mouse_face_face_id = DEFAULT_FACE_ID;
1853 the_only_display_info.mouse_highlight.mouse_face_window = Qnil;
1854 the_only_display_info.mouse_highlight.mouse_face_mouse_x =
1855 the_only_display_info.mouse_highlight.mouse_face_mouse_y = 0;
1856 the_only_display_info.mouse_highlight.mouse_face_defer = 0;
1857 the_only_display_info.mouse_highlight.mouse_face_hidden = 0;
1858
1859 if (have_mouse) /* detected in dos_ttraw, which see */
1860 {
1861 have_mouse = 1; /* enable mouse */
1862 mouse_visible = 0;
1863 mouse_setup_buttons (mouse_button_count);
1864 tty->terminal->mouse_position_hook = &mouse_get_pos;
1865 mouse_init ();
1866 }
1867
1868 if (tty->termscript && screen_size)
1869 fprintf (tty->termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
1870 screen_size_X, screen_size_Y);
1871
1872 init_frame_faces (sf);
1873 init_needed = 0;
1874 }
1875#endif
1876}
1877
1878void
1879initialize_msdos_display (struct terminal *term)
1880{
1881 term->rif = 0; /* we don't support window-based display */
1882 term->cursor_to_hook = term->raw_cursor_to_hook = IT_cursor_to;
1883 term->clear_to_end_hook = IT_clear_to_end;
1884 term->clear_frame_hook = IT_clear_screen;
1885 term->clear_end_of_line_hook = IT_clear_end_of_line;
1886 term->ins_del_lines_hook = 0;
1887 term->insert_glyphs_hook = IT_insert_glyphs;
1888 term->write_glyphs_hook = IT_write_glyphs;
1889 term->delete_glyphs_hook = IT_delete_glyphs;
1890 term->ring_bell_hook = IT_ring_bell;
1891 term->reset_terminal_modes_hook = IT_reset_terminal_modes;
1892 term->set_terminal_modes_hook = IT_set_terminal_modes;
1893 term->set_terminal_window_hook = IT_set_terminal_window;
1894 term->update_begin_hook = IT_update_begin;
1895 term->update_end_hook = IT_update_end;
1896 term->frame_up_to_date_hook = IT_frame_up_to_date;
1897 term->mouse_position_hook = 0; /* set later by dos_ttraw */
1898 term->frame_rehighlight_hook = 0;
1899 term->frame_raise_lower_hook = 0;
1900 term->set_vertical_scroll_bar_hook = 0;
1901 term->condemn_scroll_bars_hook = 0;
1902 term->redeem_scroll_bar_hook = 0;
1903 term->judge_scroll_bars_hook = 0;
1904 term->read_socket_hook = &tty_read_avail_input; /* from keyboard.c */
1905}
1906
1907int
1908dos_get_saved_screen (char **screen, int *rows, int *cols)
1909{
1910#ifndef HAVE_X_WINDOWS
1911 *screen = startup_screen_buffer;
1912 *cols = startup_screen_size_X;
1913 *rows = startup_screen_size_Y;
1914 return *screen != (char *)0;
1915#else
1916 return 0;
1917#endif
1918}
1919
1920#ifndef HAVE_X_WINDOWS
1921
1922/* We are not X, but we can emulate it well enough for our needs... */
1923void
1924check_x (void)
1925{
1926 if (! FRAME_MSDOS_P (SELECTED_FRAME ()))
1927 error ("Not running under a window system");
1928}
1929
1930#endif
1931
1932\f
1933/* ----------------------- Keyboard control ----------------------
1934 *
1935 * Keymaps reflect the following keyboard layout:
1936 *
1937 * 0 1 2 3 4 5 6 7 8 9 10 11 12 BS
1938 * TAB 15 16 17 18 19 20 21 22 23 24 25 26 (41)
1939 * CLOK 30 31 32 33 34 35 36 37 38 39 40 (41) RET
1940 * SH () 45 46 47 48 49 50 51 52 53 54 SHIFT
1941 * SPACE
1942 */
1943
1944#define Ignore 0x0000
1945#define Normal 0x0000 /* normal key - alt changes scan-code */
1946#define FctKey 0x1000 /* func key if c == 0, else c */
1947#define Special 0x2000 /* func key even if c != 0 */
1948#define ModFct 0x3000 /* special if mod-keys, else 'c' */
1949#define Map 0x4000 /* alt scan-code, map to unshift/shift key */
1950#define KeyPad 0x5000 /* map to insert/kp-0 depending on c == 0xe0 */
1951#define Grey 0x6000 /* Grey keypad key */
1952
1953#define Alt 0x0100 /* alt scan-code */
1954#define Ctrl 0x0200 /* ctrl scan-code */
1955#define Shift 0x0400 /* shift scan-code */
1956
1957static int extended_kbd; /* 101 (102) keyboard present. */
1958
1959struct kbd_translate {
1960 unsigned char sc;
1961 unsigned char ch;
1962 unsigned short code;
1963};
1964
1965struct dos_keyboard_map
1966{
1967 char *unshifted;
1968 char *shifted;
1969 char *alt_gr;
1970 struct kbd_translate *translate_table;
1971};
1972
1973
1974static struct dos_keyboard_map us_keyboard = {
1975/* 0 1 2 3 4 5 */
1976/* 01234567890123456789012345678901234567890 12345678901234 */
1977 "`1234567890-= qwertyuiop[] asdfghjkl;'\\ zxcvbnm,./ ",
1978/* 0123456789012345678901234567890123456789 012345678901234 */
1979 "~!@#$%^&*()_+ QWERTYUIOP{} ASDFGHJKL:\"| ZXCVBNM<>? ",
1980 0, /* no Alt-Gr key */
1981 0 /* no translate table */
1982};
1983
1984static struct dos_keyboard_map fr_keyboard = {
1985/* 0 1 2 3 4 5 */
1986/* 012 3456789012345678901234567890123456789012345678901234 */
1987