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