(Fformat): Doc fix.
[bpt/emacs.git] / src / msdos.c
CommitLineData
c6a6499f 1/* MS-DOS specific C utilities. -*- coding: raw-text -*-
0b5538bd 2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002,
8cabe764
GM
3 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
1b94449f
RS
5
6This file is part of GNU Emacs.
7
8GNU Emacs is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
684d6f5b 10the Free Software Foundation; either version 3, or (at your option)
1b94449f
RS
11any later version.
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU Emacs; see the file COPYING. If not, write to
4fc5845f
LK
20the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21Boston, MA 02110-1301, USA. */
1b94449f 22
9da6e765 23/* Contributed by Morten Welinder */
f32d4091 24/* New display, keyboard, and mouse control by Kim F. Storm */
9da6e765 25
1b94449f
RS
26/* Note: some of the stuff here was taken from end of sysdep.c in demacs. */
27
48984716 28#include <config.h>
1b94449f
RS
29
30#ifdef MSDOS
31#include "lisp.h"
32#include <stdio.h>
33#include <stdlib.h>
06b1ea13 34#include <time.h>
1b94449f
RS
35#include <sys/param.h>
36#include <sys/time.h>
37#include <dos.h>
d21e67b5 38#include <errno.h>
a7cf9151 39#include <string.h> /* for bzero and string functions */
d21e67b5 40#include <sys/stat.h> /* for _fixpath */
a7cf9151 41#include <unistd.h> /* for chdir, dup, dup2, etc. */
bf794306 42#include <dir.h> /* for getdisk */
1bd7b2c7 43#if __DJGPP__ >= 2
affa509c 44#pragma pack(0) /* dir.h does a pack(4), which isn't GCC's default */
1bd7b2c7 45#include <fcntl.h>
a7cf9151 46#include <io.h> /* for setmode */
fc171623
KH
47#include <dpmi.h> /* for __dpmi_xxx stuff */
48#include <sys/farptr.h> /* for _farsetsel, _farnspokeb */
d21e67b5 49#include <libc/dosio.h> /* for _USE_LFN */
a7cf9151 50#include <conio.h> /* for cputs */
1bd7b2c7
RS
51#endif
52
1b94449f
RS
53#include "msdos.h"
54#include "systime.h"
55#include "termhooks.h"
aa9ce936 56#include "termchar.h"
87485d6f 57#include "dispextern.h"
c77f6f1b 58#include "dosfns.h"
87485d6f 59#include "termopts.h"
83be827a 60#include "character.h"
aa9ce936
EZ
61#include "coding.h"
62#include "disptab.h"
1b94449f 63#include "frame.h"
87485d6f 64#include "window.h"
fc171623
KH
65#include "buffer.h"
66#include "commands.h"
41ad069b 67#include "blockinput.h"
97dd288b 68#include "keyboard.h"
6b61353c 69#include "intervals.h"
1b94449f
RS
70#include <go32.h>
71#include <pc.h>
72#include <ctype.h>
73/* #include <process.h> */
74/* Damn that local process.h! Instead we can define P_WAIT ourselves. */
75#define P_WAIT 1
76
d21e67b5
RS
77#ifndef _USE_LFN
78#define _USE_LFN 0
79#endif
80
b36701cc
RS
81#ifndef _dos_ds
82#define _dos_ds _go32_info_block.selector_for_linear_memory
83#endif
84
1bd7b2c7
RS
85#if __DJGPP__ > 1
86
8748735b 87#include <signal.h>
417a04bb 88#include "syssignal.h"
8748735b 89
1bd7b2c7
RS
90#ifndef SYSTEM_MALLOC
91
92#ifdef GNU_MALLOC
93
94/* If other `malloc' than ours is used, force our `sbrk' behave like
95 Unix programs expect (resize memory blocks to keep them contiguous).
96 If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
97 because that's what `gmalloc' expects to get. */
98#include <crt0.h>
99
100#ifdef REL_ALLOC
101int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
102#else /* not REL_ALLOC */
103int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
104#endif /* not REL_ALLOC */
105#endif /* GNU_MALLOC */
106
107#endif /* not SYSTEM_MALLOC */
108#endif /* __DJGPP__ > 1 */
aee81730
RS
109
110static unsigned long
111event_timestamp ()
112{
113 struct time t;
114 unsigned long s;
56e19ec4 115
aee81730
RS
116 gettime (&t);
117 s = t.ti_min;
118 s *= 60;
119 s += t.ti_sec;
120 s *= 1000;
121 s += t.ti_hund * 10;
56e19ec4 122
aee81730
RS
123 return s;
124}
125
f32d4091
KS
126\f
127/* ------------------------ Mouse control ---------------------------
128 *
129 * Coordinates are in screen positions and zero based.
130 * Mouse buttons are numbered from left to right and also zero based.
131 */
1b94449f 132
0c3cfc51
EZ
133/* This used to be in termhooks.h, but mainstream Emacs code no longer
134 uses it, and it was removed... */
135#define NUM_MOUSE_BUTTONS (5)
136
f32d4091
KS
137int have_mouse; /* 0: no, 1: enabled, -1: disabled */
138static int mouse_visible;
1b94449f 139
f32d4091
KS
140static int mouse_last_x;
141static int mouse_last_y;
1b94449f 142
f32d4091
KS
143static int mouse_button_translate[NUM_MOUSE_BUTTONS];
144static int mouse_button_count;
1b94449f 145
f32d4091
KS
146void
147mouse_on ()
1b94449f 148{
1b94449f 149 union REGS regs;
1b94449f 150
f32d4091 151 if (have_mouse > 0 && !mouse_visible)
1b94449f 152 {
f32d4091
KS
153 if (termscript)
154 fprintf (termscript, "<M_ON>");
155 regs.x.ax = 0x0001;
156 int86 (0x33, &regs, &regs);
157 mouse_visible = 1;
1b94449f 158 }
1b94449f
RS
159}
160
f32d4091
KS
161void
162mouse_off ()
1b94449f 163{
f32d4091 164 union REGS regs;
1b94449f 165
f32d4091 166 if (have_mouse > 0 && mouse_visible)
1b94449f 167 {
f32d4091
KS
168 if (termscript)
169 fprintf (termscript, "<M_OFF>");
170 regs.x.ax = 0x0002;
171 int86 (0x33, &regs, &regs);
172 mouse_visible = 0;
1b94449f 173 }
1b94449f
RS
174}
175
8f190436
EZ
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: ",
70da46c3
PJ
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 (nbuttons)
8f190436
EZ
201 Lisp_Object nbuttons;
202{
e7522695
EZ
203 int n;
204
b7826503 205 CHECK_NUMBER (nbuttons);
e7522695
EZ
206 n = XINT (nbuttons);
207 if (n < 2 || n > 3)
ebe061ca
KS
208 xsignal2 (Qargs_out_of_range,
209 build_string ("only 2 or 3 mouse buttons are supported"),
210 nbuttons);
e7522695 211 mouse_setup_buttons (n);
8f190436
EZ
212 return Qnil;
213}
214
211c7152
EZ
215static void
216mouse_get_xy (int *x, int *y)
217{
218 union REGS regs;
219
220 regs.x.ax = 0x0003;
221 int86 (0x33, &regs, &regs);
222 *x = regs.x.cx / 8;
223 *y = regs.x.dx / 8;
224}
225
f32d4091
KS
226void
227mouse_moveto (x, y)
228 int x, y;
1b94449f 229{
f32d4091 230 union REGS regs;
1b94449f 231
f32d4091
KS
232 if (termscript)
233 fprintf (termscript, "<M_XY=%dx%d>", x, y);
234 regs.x.ax = 0x0004;
235 mouse_last_x = regs.x.cx = x * 8;
236 mouse_last_y = regs.x.dx = y * 8;
237 int86 (0x33, &regs, &regs);
1b94449f
RS
238}
239
f32d4091
KS
240static int
241mouse_pressed (b, xp, yp)
242 int b, *xp, *yp;
1b94449f 243{
f32d4091 244 union REGS regs;
1b94449f 245
f32d4091
KS
246 if (b >= mouse_button_count)
247 return 0;
248 regs.x.ax = 0x0005;
249 regs.x.bx = mouse_button_translate[b];
250 int86 (0x33, &regs, &regs);
251 if (regs.x.bx)
252 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
253 return (regs.x.bx != 0);
1b94449f
RS
254}
255
f32d4091
KS
256static int
257mouse_released (b, xp, yp)
258 int b, *xp, *yp;
1b94449f
RS
259{
260 union REGS regs;
261
f32d4091
KS
262 if (b >= mouse_button_count)
263 return 0;
264 regs.x.ax = 0x0006;
265 regs.x.bx = mouse_button_translate[b];
266 int86 (0x33, &regs, &regs);
267 if (regs.x.bx)
268 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
269 return (regs.x.bx != 0);
1b94449f
RS
270}
271
0c7bc1aa
EZ
272static int
273mouse_button_depressed (b, xp, yp)
274 int b, *xp, *yp;
275{
276 union REGS regs;
277
278 if (b >= mouse_button_count)
279 return 0;
280 regs.x.ax = 0x0003;
281 int86 (0x33, &regs, &regs);
282 if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
283 {
284 *xp = regs.x.cx / 8;
285 *yp = regs.x.dx / 8;
286 return 1;
287 }
288 return 0;
289}
290
f32d4091
KS
291void
292mouse_get_pos (f, insist, bar_window, part, x, y, time)
293 FRAME_PTR *f;
294 int insist;
295 Lisp_Object *bar_window, *x, *y;
296 enum scroll_bar_part *part;
297 unsigned long *time;
298{
299 int ix, iy;
211c7152
EZ
300 Lisp_Object frame, tail;
301
302 /* Clear the mouse-moved flag for every frame on this display. */
303 FOR_EACH_FRAME (tail, frame)
304 XFRAME (frame)->mouse_moved = 0;
f32d4091 305
2d764c78 306 *f = SELECTED_FRAME();
f32d4091
KS
307 *bar_window = Qnil;
308 mouse_get_xy (&ix, &iy);
f32d4091 309 *time = event_timestamp ();
211c7152
EZ
310 *x = make_number (mouse_last_x = ix);
311 *y = make_number (mouse_last_y = iy);
f32d4091 312}
1b94449f 313
f32d4091
KS
314static void
315mouse_check_moved ()
1b94449f 316{
aee81730 317 int x, y;
1b94449f 318
f32d4091 319 mouse_get_xy (&x, &y);
2d764c78 320 SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
f32d4091
KS
321 mouse_last_x = x;
322 mouse_last_y = y;
323}
1b94449f 324
97dd288b
EZ
325/* Force the mouse driver to ``forget'' about any button clicks until
326 now. */
327static void
328mouse_clear_clicks (void)
329{
330 int b;
331
332 for (b = 0; b < mouse_button_count; b++)
333 {
334 int dummy_x, dummy_y;
335
336 (void) mouse_pressed (b, &dummy_x, &dummy_y);
337 (void) mouse_released (b, &dummy_x, &dummy_y);
338 }
339}
340
f32d4091
KS
341void
342mouse_init ()
343{
344 union REGS regs;
647c32eb 345
f32d4091
KS
346 if (termscript)
347 fprintf (termscript, "<M_INIT>");
1b94449f 348
f32d4091
KS
349 regs.x.ax = 0x0021;
350 int86 (0x33, &regs, &regs);
091d0bdf 351
0c7bc1aa
EZ
352 /* Reset the mouse last press/release info. It seems that Windows
353 doesn't do that automatically when function 21h is called, which
354 causes Emacs to ``remember'' the click that switched focus to the
355 window just before Emacs was started from that window. */
97dd288b 356 mouse_clear_clicks ();
0c7bc1aa 357
f32d4091
KS
358 regs.x.ax = 0x0007;
359 regs.x.cx = 0;
360 regs.x.dx = 8 * (ScreenCols () - 1);
361 int86 (0x33, &regs, &regs);
1b94449f 362
f32d4091
KS
363 regs.x.ax = 0x0008;
364 regs.x.cx = 0;
365 regs.x.dx = 8 * (ScreenRows () - 1);
366 int86 (0x33, &regs, &regs);
1b94449f 367
f32d4091
KS
368 mouse_moveto (0, 0);
369 mouse_visible = 0;
370}
3eb1dbb6 371\f
f32d4091
KS
372/* ------------------------- Screen control ----------------------
373 *
374 */
aee81730 375
f32d4091 376static int internal_terminal = 0;
aee81730 377
f32d4091
KS
378#ifndef HAVE_X_WINDOWS
379extern unsigned char ScreenAttrib;
380static int screen_face;
aee81730 381
f32d4091
KS
382static int screen_size_X;
383static int screen_size_Y;
384static int screen_size;
1b94449f 385
f32d4091
KS
386static int current_pos_X;
387static int current_pos_Y;
388static int new_pos_X;
389static int new_pos_Y;
1b94449f 390
f32d4091
KS
391static void *startup_screen_buffer;
392static int startup_screen_size_X;
393static int startup_screen_size_Y;
394static int startup_pos_X;
395static int startup_pos_Y;
c9adab25 396static unsigned char startup_screen_attrib;
1b94449f 397
06b1ea13
EZ
398static clock_t startup_time;
399
f32d4091 400static int term_setup_done;
1b94449f 401
8ba01a32
EZ
402static unsigned short outside_cursor;
403
f32d4091 404/* Similar to the_only_frame. */
f6816f88 405struct x_output the_only_x_display;
1b94449f 406
039274cf
EZ
407/* Support for DOS/V (allows Japanese characters to be displayed on
408 standard, non-Japanese, ATs). Only supported for DJGPP v2 and later. */
409
410/* Holds the address of the text-mode screen buffer. */
411static unsigned long screen_old_address = 0;
412/* Segment and offset of the virtual screen. If 0, DOS/V is NOT loaded. */
413static unsigned short screen_virtual_segment = 0;
414static unsigned short screen_virtual_offset = 0;
68026917
AI
415/* A flag to control how to display unibyte 8-bit characters. */
416extern int unibyte_display_via_language_environment;
039274cf 417
6b61353c
KH
418extern Lisp_Object Qcursor_type;
419extern Lisp_Object Qbar, Qhbar;
8ba01a32 420
9ccf54e9 421/* The screen colors of the current frame, which serve as the default
deece6f5
EZ
422 colors for newly-created frames. */
423static int initial_screen_colors[2];
424
039274cf
EZ
425#if __DJGPP__ > 1
426/* Update the screen from a part of relocated DOS/V screen buffer which
427 begins at OFFSET and includes COUNT characters. */
428static void
429dosv_refresh_virtual_screen (int offset, int count)
430{
431 __dpmi_regs regs;
432
9ab8560d 433 if (offset < 0 || count < 0) /* paranoia; invalid values crash DOS/V */
40437cf5
EZ
434 return;
435
039274cf
EZ
436 regs.h.ah = 0xff; /* update relocated screen */
437 regs.x.es = screen_virtual_segment;
438 regs.x.di = screen_virtual_offset + offset;
439 regs.x.cx = count;
440 __dpmi_int (0x10, &regs);
441}
442#endif
443
d1d5dc19 444static void
f32d4091 445dos_direct_output (y, x, buf, len)
56e19ec4 446 int x, y;
f32d4091
KS
447 char *buf;
448 int len;
1b94449f 449{
40437cf5
EZ
450 int t0 = 2 * (x + y * screen_size_X);
451 int t = t0 + (int) ScreenPrimary;
039274cf 452 int l0 = len;
fc171623
KH
453
454#if (__DJGPP__ < 2)
f32d4091
KS
455 while (--len >= 0) {
456 dosmemput (buf++, 1, t);
457 t += 2;
458 }
fc171623
KH
459#else
460 /* This is faster. */
461 for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
462 _farnspokeb (t, *buf);
039274cf
EZ
463
464 if (screen_virtual_segment)
465 dosv_refresh_virtual_screen (t0, l0);
fc171623 466#endif
1b94449f 467}
aee81730 468#endif
1b94449f 469
1b94449f
RS
470/* Flash the screen as a substitute for BEEPs. */
471
f32d4091 472#if (__DJGPP__ < 2)
49a09c76 473static void
fcea9cd4 474do_visible_bell (xorattr)
1b94449f
RS
475 unsigned char xorattr;
476{
49a09c76 477 asm volatile
7677c808
EZ
478 (" movb $1,%%dl \n\
479visible_bell_0: \n\
480 movl _ScreenPrimary,%%eax \n\
481 call dosmemsetup \n\
482 movl %%eax,%%ebx \n\
483 movl %1,%%ecx \n\
484 movb %0,%%al \n\
485 incl %%ebx \n\
486visible_bell_1: \n\
487 xorb %%al,%%gs:(%%ebx) \n\
488 addl $2,%%ebx \n\
489 decl %%ecx \n\
490 jne visible_bell_1 \n\
491 decb %%dl \n\
492 jne visible_bell_3 \n\
493visible_bell_2: \n\
494 movzwl %%ax,%%eax \n\
495 movzwl %%ax,%%eax \n\
496 movzwl %%ax,%%eax \n\
497 movzwl %%ax,%%eax \n\
498 decw %%cx \n\
499 jne visible_bell_2 \n\
500 jmp visible_bell_0 \n\
ca986694
RS
501visible_bell_3:"
502 : /* no output */
f32d4091 503 : "m" (xorattr), "g" (screen_size)
ca986694 504 : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
1b94449f
RS
505}
506
f32d4091
KS
507static void
508ScreenVisualBell (void)
509{
510 /* This creates an xor-mask that will swap the default fore- and
511 background colors. */
5bcee7ef
KL
512 do_visible_bell (((FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ())
513 ^ FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()))
f32d4091
KS
514 * 0x11) & 0x7f);
515}
516#endif
517
518#ifndef HAVE_X_WINDOWS
519
b36701cc
RS
520static int blink_bit = -1; /* the state of the blink bit at startup */
521
76ac1508
RS
522/* Enable bright background colors. */
523static void
524bright_bg (void)
525{
526 union REGS regs;
527
b36701cc
RS
528 /* Remember the original state of the blink/bright-background bit.
529 It is stored at 0040:0065h in the BIOS data area. */
530 if (blink_bit == -1)
531 blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;
532
76ac1508
RS
533 regs.h.bl = 0;
534 regs.x.ax = 0x1003;
535 int86 (0x10, &regs, &regs);
536}
537
b36701cc
RS
538/* Disable bright background colors (and enable blinking) if we found
539 the video system in that state at startup. */
540static void
541maybe_enable_blinking (void)
542{
543 if (blink_bit == 1)
544 {
545 union REGS regs;
546
547 regs.h.bl = 1;
548 regs.x.ax = 0x1003;
549 int86 (0x10, &regs, &regs);
550 }
551}
552
8ba01a32
EZ
553/* Return non-zero if the system has a VGA adapter. */
554static int
555vga_installed (void)
556{
557 union REGS regs;
558
559 regs.x.ax = 0x1a00;
560 int86 (0x10, &regs, &regs);
561 if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
562 return 1;
563 return 0;
564}
565
4a96d4d2
KH
566/* Set the screen dimensions so that it can show no less than
567 ROWS x COLS frame. */
48ffe371 568
4a96d4d2
KH
569void
570dos_set_window_size (rows, cols)
571 int *rows, *cols;
572{
573 char video_name[30];
4a96d4d2 574 union REGS regs;
56e19ec4
EZ
575 Lisp_Object video_mode;
576 int video_mode_value, have_vga = 0;
4a96d4d2
KH
577 int current_rows = ScreenRows (), current_cols = ScreenCols ();
578
579 if (*rows == current_rows && *cols == current_cols)
580 return;
581
4a96d4d2 582 mouse_off ();
8ba01a32 583 have_vga = vga_installed ();
4a96d4d2 584
48ffe371 585 /* If the user specified a special video mode for these dimensions,
4a96d4d2
KH
586 use that mode. */
587 sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
588 video_mode = XSYMBOL (Fintern_soft (build_string (video_name),
589 Qnil))-> value;
590
591 if (INTEGERP (video_mode)
592 && (video_mode_value = XINT (video_mode)) > 0)
593 {
594 regs.x.ax = video_mode_value;
595 int86 (0x10, &regs, &regs);
48ffe371
RS
596
597 if (have_mouse)
598 {
599 /* Must hardware-reset the mouse, or else it won't update
600 its notion of screen dimensions for some non-standard
601 video modes. This is *painfully* slow... */
602 regs.x.ax = 0;
603 int86 (0x33, &regs, &regs);
604 }
4a96d4d2
KH
605 }
606
607 /* Find one of the dimensions supported by standard EGA/VGA
608 which gives us at least the required dimensions. */
609
610#if __DJGPP__ > 1
611
612 else
613 {
614 static struct {
56e19ec4 615 int rows, need_vga;
4a96d4d2
KH
616 } std_dimension[] = {
617 {25, 0},
618 {28, 1},
619 {35, 0},
620 {40, 1},
621 {43, 0},
622 {50, 1}
623 };
624 int i = 0;
625
626 while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
627 {
628 if (std_dimension[i].need_vga <= have_vga
629 && std_dimension[i].rows >= *rows)
630 {
631 if (std_dimension[i].rows != current_rows
632 || *cols != current_cols)
48ffe371 633 _set_screen_lines (std_dimension[i].rows);
4a96d4d2
KH
634 break;
635 }
48ffe371 636 i++;
4a96d4d2
KH
637 }
638 }
639
640#else /* not __DJGPP__ > 1 */
641
642 else if (*rows <= 25)
643 {
644 if (current_rows != 25 || current_cols != 80)
645 {
646 regs.x.ax = 3;
647 int86 (0x10, &regs, &regs);
648 regs.x.ax = 0x1101;
649 regs.h.bl = 0;
650 int86 (0x10, &regs, &regs);
651 regs.x.ax = 0x1200;
652 regs.h.bl = 32;
653 int86 (0x10, &regs, &regs);
654 regs.x.ax = 3;
655 int86 (0x10, &regs, &regs);
656 }
657 }
658 else if (*rows <= 50)
659 if (have_vga && (current_rows != 50 || current_cols != 80)
660 || *rows <= 43 && (current_rows != 43 || current_cols != 80))
661 {
662 regs.x.ax = 3;
663 int86 (0x10, &regs, &regs);
664 regs.x.ax = 0x1112;
665 regs.h.bl = 0;
666 int86 (0x10, &regs, &regs);
667 regs.x.ax = 0x1200;
668 regs.h.bl = 32;
669 int86 (0x10, &regs, &regs);
670 regs.x.ax = 0x0100;
671 regs.x.cx = 7;
672 int86 (0x10, &regs, &regs);
673 }
674#endif /* not __DJGPP__ > 1 */
675
676 if (have_mouse)
677 {
4a96d4d2
KH
678 mouse_init ();
679 mouse_on ();
680 }
681
682 /* Tell the caller what dimensions have been REALLY set. */
683 *rows = ScreenRows ();
684 *cols = ScreenCols ();
76ac1508 685
bed43f1d
EZ
686 /* Update Emacs' notion of screen dimensions. */
687 screen_size_X = *cols;
688 screen_size_Y = *rows;
689 screen_size = *cols * *rows;
690
41ad069b
EZ
691#if __DJGPP__ > 1
692 /* If the dimensions changed, the mouse highlight info is invalid. */
693 if (current_rows != *rows || current_cols != *cols)
694 {
695 struct frame *f = SELECTED_FRAME();
696 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
697 Lisp_Object window = dpyinfo->mouse_face_window;
698
699 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
700 {
701 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
702 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
703 dpyinfo->mouse_face_window = Qnil;
704 }
705 }
706#endif
707
76ac1508
RS
708 /* Enable bright background colors. */
709 bright_bg ();
039274cf
EZ
710
711 /* FIXME: I'm not sure the above will run at all on DOS/V. But let's
712 be defensive anyway. */
713 if (screen_virtual_segment)
714 dosv_refresh_virtual_screen (0, *cols * *rows);
4a96d4d2
KH
715}
716
48ffe371
RS
717/* If we write a character in the position where the mouse is,
718 the mouse cursor may need to be refreshed. */
09e2ac30
RS
719
720static void
f32d4091 721mouse_off_maybe ()
09e2ac30 722{
f32d4091 723 int x, y;
56e19ec4 724
f32d4091
KS
725 if (!mouse_visible)
726 return;
56e19ec4 727
f32d4091
KS
728 mouse_get_xy (&x, &y);
729 if (y != new_pos_Y || x < new_pos_X)
730 return;
56e19ec4 731
f32d4091
KS
732 mouse_off ();
733}
734
8ba01a32
EZ
735#define DEFAULT_CURSOR_START (-1)
736#define DEFAULT_CURSOR_WIDTH (-1)
737#define BOX_CURSOR_WIDTH (-32)
738
739/* Set cursor to begin at scan line START_LINE in the character cell
740 and extend for WIDTH scan lines. Scan lines are counted from top
741 of the character cell, starting from zero. */
742static void
743msdos_set_cursor_shape (struct frame *f, int start_line, int width)
744{
745#if __DJGPP__ > 1
746 unsigned desired_cursor;
747 __dpmi_regs regs;
748 int max_line, top_line, bot_line;
749
750 /* Avoid the costly BIOS call if F isn't the currently selected
751 frame. Allow for NULL as unconditionally meaning the selected
752 frame. */
753 if (f && f != SELECTED_FRAME())
754 return;
755
b106731c
EZ
756 if (termscript)
757 fprintf (termscript, "\nCURSOR SHAPE=(%d,%d)", start_line, width);
758
8ba01a32
EZ
759 /* The character cell size in scan lines is stored at 40:85 in the
760 BIOS data area. */
761 max_line = _farpeekw (_dos_ds, 0x485) - 1;
762 switch (max_line)
763 {
764 default: /* this relies on CGA cursor emulation being ON! */
765 case 7:
766 bot_line = 7;
767 break;
768 case 9:
769 bot_line = 9;
770 break;
771 case 13:
772 bot_line = 12;
773 break;
774 case 15:
775 bot_line = 14;
776 break;
777 }
778
779 if (width < 0)
780 {
781 if (width == BOX_CURSOR_WIDTH)
782 {
783 top_line = 0;
784 bot_line = max_line;
785 }
786 else if (start_line != DEFAULT_CURSOR_START)
787 {
788 top_line = start_line;
789 bot_line = top_line - width - 1;
790 }
791 else if (width != DEFAULT_CURSOR_WIDTH)
792 {
793 top_line = 0;
794 bot_line = -1 - width;
795 }
796 else
797 top_line = bot_line + 1;
798 }
799 else if (width == 0)
800 {
801 /* [31, 0] seems to DTRT for all screen sizes. */
802 top_line = 31;
803 bot_line = 0;
804 }
805 else /* WIDTH is positive */
806 {
807 if (start_line != DEFAULT_CURSOR_START)
808 bot_line = start_line;
809 top_line = bot_line - (width - 1);
810 }
811
812 /* If the current cursor shape is already what they want, we are
813 history here. */
814 desired_cursor = ((top_line & 0x1f) << 8) | (bot_line & 0x1f);
815 if (desired_cursor == _farpeekw (_dos_ds, 0x460))
816 return;
817
818 regs.h.ah = 1;
819 regs.x.cx = desired_cursor;
820 __dpmi_int (0x10, &regs);
821#endif /* __DJGPP__ > 1 */
822}
823
824static void
825IT_set_cursor_type (struct frame *f, Lisp_Object cursor_type)
826{
a0738471 827 if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
8ba01a32
EZ
828 {
829 /* Just BAR means the normal EGA/VGA cursor. */
830 msdos_set_cursor_shape (f, DEFAULT_CURSOR_START, DEFAULT_CURSOR_WIDTH);
831 }
a0738471
EZ
832 else if (CONSP (cursor_type)
833 && (EQ (XCAR (cursor_type), Qbar)
834 || EQ (XCAR (cursor_type), Qhbar)))
8ba01a32
EZ
835 {
836 Lisp_Object bar_parms = XCDR (cursor_type);
837 int width;
838
839 if (INTEGERP (bar_parms))
840 {
841 /* Feature: negative WIDTH means cursor at the top
842 of the character cell, zero means invisible cursor. */
843 width = XINT (bar_parms);
844 msdos_set_cursor_shape (f, width >= 0 ? DEFAULT_CURSOR_START : 0,
845 width);
846 }
847 else if (CONSP (bar_parms)
848 && INTEGERP (XCAR (bar_parms))
849 && INTEGERP (XCDR (bar_parms)))
850 {
851 int start_line = XINT (XCDR (bar_parms));
852
853 width = XINT (XCAR (bar_parms));
854 msdos_set_cursor_shape (f, start_line, width);
855 }
856 }
857 else
b106731c
EZ
858 {
859 /* Treat anything unknown as "box cursor". This includes nil, so
860 that a frame which doesn't specify a cursor type gets a box,
861 which is the default in Emacs. */
862 msdos_set_cursor_shape (f, 0, BOX_CURSOR_WIDTH);
863 }
8ba01a32
EZ
864}
865
71f65669
EZ
866static void
867IT_ring_bell (void)
f32d4091
KS
868{
869 if (visible_bell)
aee81730 870 {
f32d4091
KS
871 mouse_off ();
872 ScreenVisualBell ();
aee81730 873 }
f32d4091 874 else
3635be47
RS
875 {
876 union REGS inregs, outregs;
877 inregs.h.ah = 2;
878 inregs.h.dl = 7;
879 intdos (&inregs, &outregs);
880 }
09e2ac30
RS
881}
882
c77f6f1b
EZ
883/* Given a face id FACE, extract the face parameters to be used for
884 display until the face changes. The face parameters (actually, its
885 color) are used to construct the video attribute byte for each
886 glyph during the construction of the buffer that is then blitted to
887 the video RAM. */
f32d4091
KS
888static void
889IT_set_face (int face)
890{
2d764c78 891 struct frame *sf = SELECTED_FRAME();
546701f5
EZ
892 struct face *fp = FACE_FROM_ID (sf, face);
893 struct face *dfp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
894 unsigned long fg, bg, dflt_fg, dflt_bg;
f32d4091 895
c77f6f1b 896 if (!fp)
e30aee93 897 {
546701f5 898 fp = dfp;
e30aee93
EZ
899 /* The default face for the frame should always be realized and
900 cached. */
901 if (!fp)
902 abort ();
903 }
f32d4091 904 screen_face = face;
c77f6f1b
EZ
905 fg = fp->foreground;
906 bg = fp->background;
546701f5
EZ
907 dflt_fg = dfp->foreground;
908 dflt_bg = dfp->background;
c77f6f1b 909
abcce93a
MB
910 /* Don't use invalid colors. In particular, FACE_TTY_DEFAULT_* colors
911 mean use the colors of the default face. Note that we assume all
912 16 colors to be available for the background, since Emacs switches
913 on this mode (and loses the blinking attribute) at startup. */
f9d2fdc4 914 if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
3b620731 915 fg = FRAME_FOREGROUND_PIXEL (sf);
f9d2fdc4 916 else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
3b620731
EZ
917 fg = FRAME_BACKGROUND_PIXEL (sf);
918 if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
919 bg = FRAME_BACKGROUND_PIXEL (sf);
f9d2fdc4 920 else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
3b620731
EZ
921 bg = FRAME_FOREGROUND_PIXEL (sf);
922
923 /* Make sure highlighted lines really stand out, come what may. */
abcce93a 924 if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
3b620731
EZ
925 {
926 unsigned long tem = fg;
927
928 fg = bg;
929 bg = tem;
930 }
76648534
EZ
931 /* If the user requested inverse video, obey. */
932 if (inverse_video)
933 {
934 unsigned long tem2 = fg;
935
936 fg = bg;
937 bg = tem2;
938 }
c77f6f1b 939 if (termscript)
abcce93a
MB
940 fprintf (termscript, "<FACE %d: %d/%d[FG:%d/BG:%d]>", face,
941 fp->foreground, fp->background, fg, bg);
c77f6f1b
EZ
942 if (fg >= 0 && fg < 16)
943 {
944 ScreenAttrib &= 0xf0;
945 ScreenAttrib |= fg;
946 }
947 if (bg >= 0 && bg < 16)
948 {
949 ScreenAttrib &= 0x0f;
950 ScreenAttrib |= ((bg & 0x0f) << 4);
951 }
f32d4091
KS
952}
953
aa9ce936
EZ
954Lisp_Object Vdos_unsupported_char_glyph;
955
71f65669 956static void
c77f6f1b 957IT_write_glyphs (struct glyph *str, int str_len)
f32d4091 958{
aa9ce936 959 unsigned char *screen_buf, *screen_bp, *screen_buf_end, *bp;
6be49c3a
EZ
960 int unsupported_face = 0;
961 unsigned unsupported_char = '\177';
039274cf 962 int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
aa9ce936
EZ
963 register int sl = str_len;
964 register int tlen = GLYPH_TABLE_LENGTH;
965 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
966
e023fb27
EZ
967 /* If terminal_coding does any conversion, use it, otherwise use
968 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
969 because it always returns 1 if terminal_coding.src_multibyte is 1. */
970 struct coding_system *coding =
971 (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
972 ? &terminal_coding
973 : &safe_terminal_coding);
2d764c78 974 struct frame *sf;
87485d6f 975
52d38ab2
EZ
976 /* Do we need to consider conversion of unibyte characters to
977 multibyte? */
978 int convert_unibyte_characters
e16bf494
AI
979 = (NILP (current_buffer->enable_multibyte_characters)
980 && unibyte_display_via_language_environment);
52d38ab2 981
648648a9
KH
982 unsigned char conversion_buffer[256];
983 int conversion_buffer_size = sizeof conversion_buffer;
984
3b620731 985 if (str_len <= 0) return;
56e19ec4 986
6be49c3a
EZ
987 /* Set up the unsupported character glyph */
988 if (!NILP (Vdos_unsupported_char_glyph))
989 {
990 unsupported_char = FAST_GLYPH_CHAR (XINT (Vdos_unsupported_char_glyph));
991 unsupported_face = FAST_GLYPH_FACE (XINT (Vdos_unsupported_char_glyph));
992 }
993
aa9ce936
EZ
994 screen_buf = screen_bp = alloca (str_len * 2);
995 screen_buf_end = screen_buf + str_len * 2;
2d764c78 996 sf = SELECTED_FRAME();
e30aee93
EZ
997
998 /* Since faces get cached and uncached behind our back, we can't
999 rely on their indices in the cache being consistent across
1000 invocations. So always reset the screen face to the default
1001 face of the frame, before writing glyphs, and let the glyphs
1002 set the right face if it's different from the default. */
1003 IT_set_face (DEFAULT_FACE_ID);
56e19ec4 1004
aa9ce936
EZ
1005 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
1006 the tail. */
1007 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
1008 while (sl)
1009 {
c77f6f1b 1010 int cf, chlen, enclen;
3b620731 1011 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
c77f6f1b 1012 unsigned ch;
aa9ce936
EZ
1013
1014 /* Glyphs with GLYPH_MASK_PADDING bit set are actually there
1015 only for the redisplay code to know how many columns does
1016 this character occupy on the screen. Skip padding glyphs. */
c77f6f1b 1017 if (CHAR_GLYPH_PADDING_P (*str))
aa9ce936
EZ
1018 {
1019 str++;
1020 sl--;
1021 }
1022 else
1023 {
04ee4f45
EZ
1024 register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
1025 int glyph_not_in_table = 0;
1026
78555fbe
EZ
1027 /* If g is negative, it means we have a multibyte character
1028 in *str. That's what GLYPH_FROM_CHAR_GLYPH returns for
1029 multibyte characters. */
04ee4f45
EZ
1030 if (g < 0 || g >= tlen)
1031 {
1032 /* This glyph doesn't have an entry in Vglyph_table. */
1033 ch = str->u.ch;
1034 glyph_not_in_table = 1;
1035 }
1036 else
1037 {
1038 /* This glyph has an entry in Vglyph_table, so process
1039 any aliases before testing for simpleness. */
1040 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
1041 ch = FAST_GLYPH_CHAR (g);
1042 }
1043
aa9ce936 1044 /* Convert the character code to multibyte, if they
04ee4f45
EZ
1045 requested display via language environment. We only want
1046 to convert unibyte characters to multibyte in unibyte
1047 buffers! Otherwise, the 8-bit value in CH came from the
1048 display table set up to display foreign characters. */
52d38ab2 1049 if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
f0dd4c35
EZ
1050 && (ch >= 0240
1051 || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
aa9ce936
EZ
1052 ch = unibyte_char_to_multibyte (ch);
1053
1054 /* Invalid characters are displayed with a special glyph. */
04ee4f45 1055 if (! CHAR_VALID_P (ch, 0))
aa9ce936
EZ
1056 {
1057 g = !NILP (Vdos_unsupported_char_glyph)
6be49c3a 1058 ? XINT (Vdos_unsupported_char_glyph)
2d764c78 1059 : MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
aa9ce936
EZ
1060 ch = FAST_GLYPH_CHAR (g);
1061 }
aa9ce936
EZ
1062
1063 /* If the face of this glyph is different from the current
1064 screen face, update the screen attribute byte. */
78555fbe 1065 cf = str->face_id;
aa9ce936
EZ
1066 if (cf != screen_face)
1067 IT_set_face (cf); /* handles invalid faces gracefully */
1068
04ee4f45 1069 if (glyph_not_in_table || GLYPH_SIMPLE_P (tbase, tlen, g))
3b620731
EZ
1070 {
1071 /* We generate the multi-byte form of CH in WORKBUF. */
1072 chlen = CHAR_STRING (ch, workbuf);
1073 buf = workbuf;
1074 }
aa9ce936
EZ
1075 else
1076 {
1077 /* We have a string in Vglyph_table. */
1078 chlen = GLYPH_LENGTH (tbase, g);
1079 buf = GLYPH_STRING (tbase, g);
1080 }
1081
c77f6f1b 1082 /* If the character is not multibyte, don't bother converting it. */
aa9ce936
EZ
1083 if (chlen == 1)
1084 {
1085 *conversion_buffer = (unsigned char)ch;
1086 chlen = 0;
1087 enclen = 1;
1088 }
1089 else
1090 {
55a17202 1091 coding->src_multibyte = 1;
aa9ce936
EZ
1092 encode_coding (coding, buf, conversion_buffer, chlen,
1093 conversion_buffer_size);
1094 chlen -= coding->consumed;
1095 enclen = coding->produced;
1096
1097 /* Replace glyph codes that cannot be converted by
1098 terminal_coding with Vdos_unsupported_char_glyph. */
1099 if (*conversion_buffer == '?')
1100 {
5e30eaa2 1101 unsigned char *cbp = conversion_buffer;
aa9ce936
EZ
1102
1103 while (cbp < conversion_buffer + enclen && *cbp == '?')
1104 *cbp++ = unsupported_char;
1105 if (unsupported_face != screen_face)
1106 IT_set_face (unsupported_face);
1107 }
1108 }
1109
1110 if (enclen + chlen > screen_buf_end - screen_bp)
1111 {
1112 /* The allocated buffer for screen writes is too small.
1113 Flush it and loop again without incrementing STR, so
1114 that the next loop will begin with the same glyph. */
1115 int nbytes = screen_bp - screen_buf;
1116
1117 mouse_off_maybe ();
1118 dosmemput (screen_buf, nbytes, (int)ScreenPrimary + offset);
1119 if (screen_virtual_segment)
1120 dosv_refresh_virtual_screen (offset, nbytes / 2);
1121 new_pos_X += nbytes / 2;
1122 offset += nbytes;
1123
1124 /* Prepare to reuse the same buffer again. */
1125 screen_bp = screen_buf;
1126 }
1127 else
1128 {
1129 /* There's enough place in the allocated buffer to add
1130 the encoding of this glyph. */
1131
1132 /* First, copy the encoded bytes. */
1133 for (bp = conversion_buffer; enclen--; bp++)
1134 {
1135 *screen_bp++ = (unsigned char)*bp;
1136 *screen_bp++ = ScreenAttrib;
1137 if (termscript)
1138 fputc (*bp, termscript);
1139 }
1140
1141 /* Now copy the bytes not consumed by the encoding. */
1142 if (chlen > 0)
1143 {
1144 buf += coding->consumed;
1145 while (chlen--)
1146 {
1147 if (termscript)
1148 fputc (*buf, termscript);
1149 *screen_bp++ = (unsigned char)*buf++;
1150 *screen_bp++ = ScreenAttrib;
1151 }
1152 }
1153
1154 /* Update STR and its remaining length. */
1155 str++;
1156 sl--;
1157 }
1158 }
aee81730
RS
1159 }
1160
aa9ce936 1161 /* Dump whatever is left in the screen buffer. */
f32d4091 1162 mouse_off_maybe ();
aa9ce936 1163 dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
039274cf 1164 if (screen_virtual_segment)
aa9ce936
EZ
1165 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
1166 new_pos_X += (screen_bp - screen_buf) / 2;
1167
1168 /* We may have to output some codes to terminate the writing. */
1169 if (CODING_REQUIRE_FLUSHING (coding))
1170 {
1171 coding->mode |= CODING_MODE_LAST_BLOCK;
1172 encode_coding (coding, "", conversion_buffer, 0, conversion_buffer_size);
1173 if (coding->produced > 0)
1174 {
241424da 1175 screen_buf = alloca (coding->produced * 2);
aa9ce936
EZ
1176 for (screen_bp = screen_buf, bp = conversion_buffer;
1177 coding->produced--; bp++)
1178 {
1179 *screen_bp++ = (unsigned char)*bp;
1180 *screen_bp++ = ScreenAttrib;
1181 if (termscript)
1182 fputc (*bp, termscript);
1183 }
1184 offset += screen_bp - screen_buf;
1185 mouse_off_maybe ();
1186 dosmemput (screen_buf, screen_bp - screen_buf,
1187 (int)ScreenPrimary + offset);
1188 if (screen_virtual_segment)
1189 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
1190 new_pos_X += (screen_bp - screen_buf) / 2;
1191 }
1192 }
f32d4091 1193}
aee81730 1194
41ad069b
EZ
1195/************************************************************************
1196 Mouse Highlight (and friends..)
1197 ************************************************************************/
1198
ee8ceff8 1199/* Last window where we saw the mouse. Used by mouse-autoselect-window. */
9005a471
EZ
1200static Lisp_Object last_mouse_window;
1201
41ad069b
EZ
1202static int mouse_preempted = 0; /* non-zero when XMenu gobbles mouse events */
1203
1204/* Set the mouse pointer shape according to whether it is in the
1205 area where the mouse highlight is in effect. */
1206static void
1207IT_set_mouse_pointer (int mode)
1208{
1209 /* A no-op for now. DOS text-mode mouse pointer doesn't offer too
1210 many possibilities to change its shape, and the available
1211 functionality pretty much sucks (e.g., almost every reasonable
1212 shape will conceal the character it is on). Since the color of
1213 the pointer changes in the highlighted area, it is not clear to
1214 me whether anything else is required, anyway. */
1215}
1216
1217/* Display the active region described by mouse_face_*
1218 in its mouse-face if HL > 0, in its normal face if HL = 0. */
1219static void
1220show_mouse_face (struct display_info *dpyinfo, int hl)
1221{
1222 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
1223 struct frame *f = XFRAME (WINDOW_FRAME (w));
1224 int i;
1225 struct face *fp;
1226
56e19ec4 1227
41ad069b
EZ
1228 /* If window is in the process of being destroyed, don't bother
1229 doing anything. */
1230 if (w->current_matrix == NULL)
1231 goto set_cursor_shape;
1232
1233 /* Recognize when we are called to operate on rows that don't exist
1234 anymore. This can happen when a window is split. */
1235 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
1236 goto set_cursor_shape;
1237
1238 /* There's no sense to do anything if the mouse face isn't realized. */
1239 if (hl > 0)
1240 {
abf10ed7
KS
1241 if (dpyinfo->mouse_face_hidden)
1242 goto set_cursor_shape;
1243
41ad069b
EZ
1244 fp = FACE_FROM_ID (SELECTED_FRAME(), dpyinfo->mouse_face_face_id);
1245 if (!fp)
1246 goto set_cursor_shape;
1247 }
1248
1249 /* Note that mouse_face_beg_row etc. are window relative. */
1250 for (i = dpyinfo->mouse_face_beg_row;
1251 i <= dpyinfo->mouse_face_end_row;
1252 i++)
1253 {
1254 int start_hpos, end_hpos;
1255 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
1256
1257 /* Don't do anything if row doesn't have valid contents. */
1258 if (!row->enabled_p)
1259 continue;
1260
1261 /* For all but the first row, the highlight starts at column 0. */
1262 if (i == dpyinfo->mouse_face_beg_row)
1263 start_hpos = dpyinfo->mouse_face_beg_col;
1264 else
1265 start_hpos = 0;
1266
1267 if (i == dpyinfo->mouse_face_end_row)
1268 end_hpos = dpyinfo->mouse_face_end_col;
1269 else
1270 end_hpos = row->used[TEXT_AREA];
1271
1272 if (end_hpos <= start_hpos)
1273 continue;
48c14970
EZ
1274 /* Record that some glyphs of this row are displayed in
1275 mouse-face. */
1276 row->mouse_face_p = hl > 0;
41ad069b
EZ
1277 if (hl > 0)
1278 {
c655f6fd
KS
1279 int vpos = row->y + WINDOW_TOP_EDGE_Y (w);
1280 int kstart = start_hpos + WINDOW_LEFT_EDGE_X (w);
41ad069b
EZ
1281 int nglyphs = end_hpos - start_hpos;
1282 int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
1283 int start_offset = offset;
1284
1285 if (termscript)
1286 fprintf (termscript, "\n<MH+ %d-%d:%d>",
1287 kstart, kstart + nglyphs - 1, vpos);
1288
1289 mouse_off ();
1290 IT_set_face (dpyinfo->mouse_face_face_id);
1291 /* Since we are going to change only the _colors_ of the
1292 displayed text, there's no need to go through all the
1293 pain of generating and encoding the text from the glyphs.
1294 Instead, we simply poke the attribute byte of each
1295 affected position in video memory with the colors
1296 computed by IT_set_face! */
1297 _farsetsel (_dos_ds);
1298 while (nglyphs--)
1299 {
1300 _farnspokeb (offset, ScreenAttrib);
1301 offset += 2;
1302 }
1303 if (screen_virtual_segment)
1304 dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
1305 mouse_on ();
1306 }
1307 else
1308 {
1309 /* We are removing a previously-drawn mouse highlight. The
1310 safest way to do so is to redraw the glyphs anew, since
1311 all kinds of faces and display tables could have changed
1312 behind our back. */
1313 int nglyphs = end_hpos - start_hpos;
1314 int save_x = new_pos_X, save_y = new_pos_Y;
1315
1316 if (end_hpos >= row->used[TEXT_AREA])
1317 nglyphs = row->used[TEXT_AREA] - start_hpos;
1318
1319 /* IT_write_glyphs writes at cursor position, so we need to
1320 temporarily move cursor coordinates to the beginning of
1321 the highlight region. */
c655f6fd
KS
1322 new_pos_X = start_hpos + WINDOW_LEFT_EDGE_X (w);
1323 new_pos_Y = row->y + WINDOW_TOP_EDGE_Y (w);
41ad069b
EZ
1324
1325 if (termscript)
1326 fprintf (termscript, "<MH- %d-%d:%d>",
1327 new_pos_X, new_pos_X + nglyphs - 1, new_pos_Y);
1328 IT_write_glyphs (row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
1329 if (termscript)
1330 fputs ("\n", termscript);
1331 new_pos_X = save_x;
1332 new_pos_Y = save_y;
1333 }
1334 }
1335
1336 set_cursor_shape:
41ad069b
EZ
1337 /* Change the mouse pointer shape. */
1338 IT_set_mouse_pointer (hl);
1339}
1340
1341/* Clear out the mouse-highlighted active region.
1342 Redraw it un-highlighted first. */
1343static void
1344clear_mouse_face (struct display_info *dpyinfo)
1345{
6b61353c 1346 if (!dpyinfo->mouse_face_hidden && ! NILP (dpyinfo->mouse_face_window))
41ad069b
EZ
1347 show_mouse_face (dpyinfo, 0);
1348
1349 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1350 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1351 dpyinfo->mouse_face_window = Qnil;
1352}
1353
1354/* Find the glyph matrix position of buffer position POS in window W.
1355 *HPOS and *VPOS are set to the positions found. W's current glyphs
1356 must be up to date. If POS is above window start return (0, 0).
1357 If POS is after end of W, return end of last line in W. */
1358static int
1359fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
1360{
56e19ec4 1361 int i, lastcol, line_start_position, maybe_next_line_p = 0;
41ad069b 1362 int yb = window_text_bottom_y (w);
56e19ec4 1363 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
41ad069b
EZ
1364
1365 while (row->y < yb)
1366 {
1367 if (row->used[TEXT_AREA])
1368 line_start_position = row->glyphs[TEXT_AREA]->charpos;
1369 else
1370 line_start_position = 0;
1371
1372 if (line_start_position > pos)
1373 break;
1374 /* If the position sought is the end of the buffer,
1375 don't include the blank lines at the bottom of the window. */
1376 else if (line_start_position == pos
1377 && pos == BUF_ZV (XBUFFER (w->buffer)))
1378 {
1379 maybe_next_line_p = 1;
1380 break;
1381 }
1382 else if (line_start_position > 0)
1383 best_row = row;
440ffd67
EZ
1384
1385 /* Don't overstep the last matrix row, lest we get into the
1386 never-never land... */
1387 if (row->y + 1 >= yb)
1388 break;
56e19ec4 1389
41ad069b
EZ
1390 ++row;
1391 }
56e19ec4 1392
41ad069b
EZ
1393 /* Find the right column within BEST_ROW. */
1394 lastcol = 0;
1395 row = best_row;
1396 for (i = 0; i < row->used[TEXT_AREA]; i++)
1397 {
1398 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
1399 int charpos;
1400
1401 charpos = glyph->charpos;
1402 if (charpos == pos)
1403 {
1404 *hpos = i;
1405 *vpos = row->y;
1406 return 1;
1407 }
1408 else if (charpos > pos)
1409 break;
1410 else if (charpos > 0)
1411 lastcol = i;
1412 }
1413
1414 /* If we're looking for the end of the buffer,
1415 and we didn't find it in the line we scanned,
1416 use the start of the following line. */
1417 if (maybe_next_line_p)
1418 {
1419 ++row;
1420 lastcol = 0;
1421 }
1422
1423 *vpos = row->y;
1424 *hpos = lastcol + 1;
1425 return 0;
1426}
1427
1428/* Take proper action when mouse has moved to the mode or top line of
1429 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
1430 mode line. X is relative to the start of the text display area of
377df9e2 1431 W, so the width of fringes and scroll bars must be subtracted
41ad069b
EZ
1432 to get a position relative to the start of the mode line. */
1433static void
1434IT_note_mode_line_highlight (struct window *w, int x, int mode_line_p)
1435{
1436 struct frame *f = XFRAME (w->frame);
1437 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1438 struct glyph_row *row;
1439
1440 if (mode_line_p)
1441 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1442 else
1443 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
56e19ec4 1444
41ad069b
EZ
1445 if (row->enabled_p)
1446 {
1447 extern Lisp_Object Qhelp_echo;
1448 struct glyph *glyph, *end;
1449 Lisp_Object help, map;
56e19ec4 1450
41ad069b 1451 /* Find the glyph under X. */
c655f6fd
KS
1452 glyph = (row->glyphs[TEXT_AREA]
1453 + x
6b61353c 1454 /* in case someone implements scroll bars some day... */
c655f6fd 1455 - WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w));
41ad069b
EZ
1456 end = glyph + row->used[TEXT_AREA];
1457 if (glyph < end
1458 && STRINGP (glyph->object)
d5db4077 1459 && STRING_INTERVALS (glyph->object)
41ad069b 1460 && glyph->charpos >= 0
d5db4077 1461 && glyph->charpos < SCHARS (glyph->object))
41ad069b
EZ
1462 {
1463 /* If we're on a string with `help-echo' text property,
1464 arrange for the help to be displayed. This is done by
1465 setting the global variable help_echo to the help string. */
1466 help = Fget_text_property (make_number (glyph->charpos),
1467 Qhelp_echo, glyph->object);
b7e80413 1468 if (!NILP (help))
48c14970 1469 {
02d4b97d 1470 help_echo_string = help;
48c14970
EZ
1471 XSETWINDOW (help_echo_window, w);
1472 help_echo_object = glyph->object;
1473 help_echo_pos = glyph->charpos;
1474 }
41ad069b
EZ
1475 }
1476 }
1477}
1478
1479/* Take proper action when the mouse has moved to position X, Y on
1480 frame F as regards highlighting characters that have mouse-face
1481 properties. Also de-highlighting chars where the mouse was before.
1482 X and Y can be negative or out of range. */
1483static void
1484IT_note_mouse_highlight (struct frame *f, int x, int y)
1485{
1486 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
02d4b97d 1487 enum window_part part = ON_NOTHING;
41ad069b
EZ
1488 Lisp_Object window;
1489 struct window *w;
1490
1491 /* When a menu is active, don't highlight because this looks odd. */
1492 if (mouse_preempted)
1493 return;
1494
abf10ed7 1495 if (NILP (Vmouse_highlight)
515d0d0e 1496 || !f->glyphs_initialized_p)
41ad069b
EZ
1497 return;
1498
1499 dpyinfo->mouse_face_mouse_x = x;
1500 dpyinfo->mouse_face_mouse_y = y;
1501 dpyinfo->mouse_face_mouse_frame = f;
1502
1503 if (dpyinfo->mouse_face_defer)
1504 return;
1505
1506 if (gc_in_progress)
1507 {
1508 dpyinfo->mouse_face_deferred_gc = 1;
1509 return;
1510 }
1511
1512 /* Which window is that in? */
c655f6fd 1513 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
41ad069b
EZ
1514
1515 /* If we were displaying active text in another window, clear that. */
1516 if (! EQ (window, dpyinfo->mouse_face_window))
1517 clear_mouse_face (dpyinfo);
1518
1519 /* Not on a window -> return. */
1520 if (!WINDOWP (window))
1521 return;
1522
1523 /* Convert to window-relative coordinates. */
1524 w = XWINDOW (window);
41ad069b 1525
02d4b97d 1526 if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
41ad069b
EZ
1527 {
1528 /* Mouse is on the mode or top line. */
02d4b97d 1529 IT_note_mode_line_highlight (w, x, part == ON_MODE_LINE);
41ad069b
EZ
1530 return;
1531 }
c655f6fd
KS
1532
1533 IT_set_mouse_pointer (0);
41ad069b
EZ
1534
1535 /* Are we in a window whose display is up to date?
1536 And verify the buffer's text has not changed. */
02d4b97d 1537 if (part == ON_TEXT
41ad069b
EZ
1538 && EQ (w->window_end_valid, w->buffer)
1539 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
1540 && (XFASTINT (w->last_overlay_modified)
1541 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
1542 {
56e19ec4 1543 int pos, i, nrows = w->current_matrix->nrows;
41ad069b
EZ
1544 struct glyph_row *row;
1545 struct glyph *glyph;
1546
1547 /* Find the glyph under X/Y. */
1548 glyph = NULL;
440ffd67 1549 if (y >= 0 && y < nrows)
41ad069b
EZ
1550 {
1551 row = MATRIX_ROW (w->current_matrix, y);
440ffd67
EZ
1552 /* Give up if some row before the one we are looking for is
1553 not enabled. */
1554 for (i = 0; i <= y; i++)
1555 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
1556 break;
1557 if (i > y /* all rows upto and including the one at Y are enabled */
41ad069b
EZ
1558 && row->displays_text_p
1559 && x < window_box_width (w, TEXT_AREA))
1560 {
1561 glyph = row->glyphs[TEXT_AREA];
1562 if (x >= row->used[TEXT_AREA])
1563 glyph = NULL;
1564 else
1565 {
1566 glyph += x;
1567 if (!BUFFERP (glyph->object))
1568 glyph = NULL;
1569 }
1570 }
1571 }
1572
1573 /* Clear mouse face if X/Y not over text. */
1574 if (glyph == NULL)
1575 {
1576 clear_mouse_face (dpyinfo);
1577 return;
1578 }
1579
1580 if (!BUFFERP (glyph->object))
1581 abort ();
1582 pos = glyph->charpos;
1583
1584 /* Check for mouse-face and help-echo. */
1585 {
1586 extern Lisp_Object Qmouse_face;
56e19ec4 1587 Lisp_Object mouse_face, overlay, position, *overlay_vec;
3b8c0c70 1588 int noverlays, obegv, ozv;
41ad069b 1589 struct buffer *obuf;
41ad069b
EZ
1590
1591 /* If we get an out-of-range value, return now; avoid an error. */
1592 if (pos > BUF_Z (XBUFFER (w->buffer)))
1593 return;
1594
1595 /* Make the window's buffer temporarily current for
1596 overlays_at and compute_char_face. */
1597 obuf = current_buffer;
1598 current_buffer = XBUFFER (w->buffer);
1599 obegv = BEGV;
1600 ozv = ZV;
1601 BEGV = BEG;
1602 ZV = Z;
1603
1604 /* Is this char mouse-active or does it have help-echo? */
1605 XSETINT (position, pos);
1606
1a8da670
KS
1607 /* Put all the overlays we want in a vector in overlay_vec. */
1608 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
f0a4139c 1609 /* Sort overlays into increasing priority order. */
41ad069b
EZ
1610 noverlays = sort_overlays (overlay_vec, noverlays, w);
1611
1612 /* Check mouse-face highlighting. */
1613 if (! (EQ (window, dpyinfo->mouse_face_window)
1614 && y >= dpyinfo->mouse_face_beg_row
1615 && y <= dpyinfo->mouse_face_end_row
1616 && (y > dpyinfo->mouse_face_beg_row
1617 || x >= dpyinfo->mouse_face_beg_col)
1618 && (y < dpyinfo->mouse_face_end_row
1619 || x < dpyinfo->mouse_face_end_col
1620 || dpyinfo->mouse_face_past_end)))
1621 {
1622 /* Clear the display of the old active region, if any. */
1623 clear_mouse_face (dpyinfo);
1624
1625 /* Find highest priority overlay that has a mouse-face prop. */
1626 overlay = Qnil;
f0a4139c 1627 for (i = noverlays - 1; i >= 0; --i)
41ad069b
EZ
1628 {
1629 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
1630 if (!NILP (mouse_face))
1631 {
1632 overlay = overlay_vec[i];
1633 break;
1634 }
1635 }
1636
1637 /* If no overlay applies, get a text property. */
1638 if (NILP (overlay))
1639 mouse_face = Fget_text_property (position, Qmouse_face,
1640 w->buffer);
1641
1642 /* Handle the overlay case. */
1643 if (! NILP (overlay))
1644 {
1645 /* Find the range of text around this char that
1646 should be active. */
1647 Lisp_Object before, after;
1648 int ignore;
1649
1650 before = Foverlay_start (overlay);
1651 after = Foverlay_end (overlay);
1652 /* Record this as the current active region. */
1653 fast_find_position (w, XFASTINT (before),
1654 &dpyinfo->mouse_face_beg_col,
1655 &dpyinfo->mouse_face_beg_row);
1656 dpyinfo->mouse_face_past_end
1657 = !fast_find_position (w, XFASTINT (after),
1658 &dpyinfo->mouse_face_end_col,
1659 &dpyinfo->mouse_face_end_row);
1660 dpyinfo->mouse_face_window = window;
1661 dpyinfo->mouse_face_face_id
1662 = face_at_buffer_position (w, pos, 0, 0,
0d6239f1
KS
1663 &ignore, pos + 1,
1664 !dpyinfo->mouse_face_hidden);
41ad069b
EZ
1665
1666 /* Display it as active. */
1667 show_mouse_face (dpyinfo, 1);
1668 }
1669 /* Handle the text property case. */
1670 else if (! NILP (mouse_face))
1671 {
1672 /* Find the range of text around this char that
1673 should be active. */
1674 Lisp_Object before, after, beginning, end;
1675 int ignore;
1676
1677 beginning = Fmarker_position (w->start);
1678 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
1679 - XFASTINT (w->window_end_pos)));
1680 before
1681 = Fprevious_single_property_change (make_number (pos + 1),
1682 Qmouse_face,
1683 w->buffer, beginning);
1684 after
1685 = Fnext_single_property_change (position, Qmouse_face,
1686 w->buffer, end);
1687 /* Record this as the current active region. */
1688 fast_find_position (w, XFASTINT (before),
1689 &dpyinfo->mouse_face_beg_col,
1690 &dpyinfo->mouse_face_beg_row);
1691 dpyinfo->mouse_face_past_end
1692 = !fast_find_position (w, XFASTINT (after),
1693 &dpyinfo->mouse_face_end_col,
1694 &dpyinfo->mouse_face_end_row);
1695 dpyinfo->mouse_face_window = window;
1696 dpyinfo->mouse_face_face_id
1697 = face_at_buffer_position (w, pos, 0, 0,
0d6239f1
KS
1698 &ignore, pos + 1,
1699 !dpyinfo->mouse_face_hidden);
41ad069b
EZ
1700
1701 /* Display it as active. */
1702 show_mouse_face (dpyinfo, 1);
1703 }
1704 }
1705
1706 /* Look for a `help-echo' property. */
1707 {
1708 Lisp_Object help;
1709 extern Lisp_Object Qhelp_echo;
1710
1711 /* Check overlays first. */
1712 help = Qnil;
f0a4139c
EZ
1713 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
1714 {
1715 overlay = overlay_vec[i];
1716 help = Foverlay_get (overlay, Qhelp_echo);
1717 }
56e19ec4 1718
b7e80413 1719 if (!NILP (help))
48c14970 1720 {
02d4b97d 1721 help_echo_string = help;
48c14970 1722 help_echo_window = window;
f0a4139c 1723 help_echo_object = overlay;
48c14970
EZ
1724 help_echo_pos = pos;
1725 }
1726 /* Try text properties. */
1727 else if (NILP (help)
1728 && ((STRINGP (glyph->object)
1729 && glyph->charpos >= 0
d5db4077 1730 && glyph->charpos < SCHARS (glyph->object))
48c14970
EZ
1731 || (BUFFERP (glyph->object)
1732 && glyph->charpos >= BEGV
1733 && glyph->charpos < ZV)))
1734 {
1735 help = Fget_text_property (make_number (glyph->charpos),
1736 Qhelp_echo, glyph->object);
1737 if (!NILP (help))
1738 {
02d4b97d 1739 help_echo_string = help;
48c14970
EZ
1740 help_echo_window = window;
1741 help_echo_object = glyph->object;
1742 help_echo_pos = glyph->charpos;
1743 }
1744 }
41ad069b 1745 }
56e19ec4 1746
41ad069b
EZ
1747 BEGV = obegv;
1748 ZV = ozv;
1749 current_buffer = obuf;
1750 }
1751 }
1752}
1753
71f65669
EZ
1754static void
1755IT_clear_end_of_line (int first_unused)
f32d4091
KS
1756{
1757 char *spaces, *sp;
56e19ec4 1758 int i, j, offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
aa9ce936
EZ
1759 extern int fatal_error_in_progress;
1760
2d764c78 1761 if (new_pos_X >= first_unused || fatal_error_in_progress)
aa9ce936 1762 return;
f32d4091
KS
1763
1764 IT_set_face (0);
2d764c78 1765 i = (j = first_unused - new_pos_X) * 2;
e25d9653
EZ
1766 if (termscript)
1767 fprintf (termscript, "<CLR:EOL[%d..%d)>", new_pos_X, first_unused);
f32d4091 1768 spaces = sp = alloca (i);
56e19ec4 1769
f32d4091 1770 while (--j >= 0)
aee81730 1771 {
f32d4091
KS
1772 *sp++ = ' ';
1773 *sp++ = ScreenAttrib;
aee81730
RS
1774 }
1775
f32d4091 1776 mouse_off_maybe ();
039274cf
EZ
1777 dosmemput (spaces, i, (int)ScreenPrimary + offset);
1778 if (screen_virtual_segment)
1779 dosv_refresh_virtual_screen (offset, i / 2);
2d764c78
EZ
1780
1781 /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
1782 Let's follow their lead, in case someone relies on this. */
1783 new_pos_X = first_unused;
aee81730
RS
1784}
1785
71f65669 1786static void
f32d4091
KS
1787IT_clear_screen (void)
1788{
1789 if (termscript)
1790 fprintf (termscript, "<CLR:SCR>");
deece6f5
EZ
1791 /* We are sometimes called (from clear_garbaged_frames) when a new
1792 frame is being created, but its faces are not yet realized. In
1793 such a case we cannot call IT_set_face, since it will fail to find
1794 any valid faces and will abort. Instead, use the initial screen
1795 colors; that should mimic what a Unix tty does, which simply clears
1796 the screen with whatever default colors are in use. */
1797 if (FACE_FROM_ID (SELECTED_FRAME (), DEFAULT_FACE_ID) == NULL)
1798 ScreenAttrib = (initial_screen_colors[0] << 4) | initial_screen_colors[1];
1799 else
1800 IT_set_face (0);
f32d4091
KS
1801 mouse_off ();
1802 ScreenClear ();
039274cf
EZ
1803 if (screen_virtual_segment)
1804 dosv_refresh_virtual_screen (0, screen_size);
f32d4091
KS
1805 new_pos_X = new_pos_Y = 0;
1806}
1807
71f65669 1808static void
f32d4091
KS
1809IT_clear_to_end (void)
1810{
1811 if (termscript)
1812 fprintf (termscript, "<CLR:EOS>");
1813
1814 while (new_pos_Y < screen_size_Y) {
1815 new_pos_X = 0;
e25d9653 1816 IT_clear_end_of_line (screen_size_X);
f32d4091
KS
1817 new_pos_Y++;
1818 }
1819}
1820
71f65669 1821static void
f32d4091
KS
1822IT_cursor_to (int y, int x)
1823{
1824 if (termscript)
1825 fprintf (termscript, "\n<XY=%dx%d>", x, y);
1826 new_pos_X = x;
1827 new_pos_Y = y;
1828}
1829
fc171623
KH
1830static int cursor_cleared;
1831
d1d5dc19 1832static void
fc171623
KH
1833IT_display_cursor (int on)
1834{
b106731c
EZ
1835 if (termscript)
1836 fprintf (termscript, "\nCURSOR %s", on ? "ON" : "OFF");
fc171623
KH
1837 if (on && cursor_cleared)
1838 {
1839 ScreenSetCursor (current_pos_Y, current_pos_X);
1840 cursor_cleared = 0;
1841 }
1842 else if (!on && !cursor_cleared)
1843 {
1844 ScreenSetCursor (-1, -1);
1845 cursor_cleared = 1;
1846 }
1847}
1848
1849/* Emacs calls cursor-movement functions a lot when it updates the
1850 display (probably a legacy of old terminals where you cannot
1851 update a screen line without first moving the cursor there).
1852 However, cursor movement is expensive on MSDOS (it calls a slow
1853 BIOS function and requires 2 mode switches), while actual screen
1854 updates access the video memory directly and don't depend on
1855 cursor position. To avoid slowing down the redisplay, we cheat:
1856 all functions that move the cursor only set internal variables
1857 which record the cursor position, whereas the cursor is only
1858 moved to its final position whenever screen update is complete.
1859
1860 `IT_cmgoto' is called from the keyboard reading loop and when the
1861 frame update is complete. This means that we are ready for user
1862 input, so we update the cursor position to show where the point is,
1863 and also make the mouse pointer visible.
1864
1865 Special treatment is required when the cursor is in the echo area,
1866 to put the cursor at the end of the text displayed there. */
1867
71f65669
EZ
1868static void
1869IT_cmgoto (FRAME_PTR f)
fc171623
KH
1870{
1871 /* Only set the cursor to where it should be if the display is
1872 already in sync with the window contents. */
2d764c78
EZ
1873 int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */
1874
1875 /* FIXME: This needs to be rewritten for the new redisplay, or
1876 removed. */
1877#if 0
06da1de1
EZ
1878 static int previous_pos_X = -1;
1879
2d764c78
EZ
1880 update_cursor_pos = 1; /* temporary!!! */
1881
06da1de1
EZ
1882 /* If the display is in sync, forget any previous knowledge about
1883 cursor position. This is primarily for unexpected events like
1884 C-g in the minibuffer. */
1885 if (update_cursor_pos && previous_pos_X >= 0)
1886 previous_pos_X = -1;
1887 /* If we are in the echo area, put the cursor at the
1888 end of the echo area message. */
fc171623 1889 if (!update_cursor_pos
c655f6fd 1890 && WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f))) <= new_pos_Y)
fc171623 1891 {
06da1de1
EZ
1892 int tem_X = current_pos_X, dummy;
1893
1894 if (echo_area_glyphs)
1895 {
1896 tem_X = echo_area_glyphs_length;
1897 /* Save current cursor position, to be restored after the
1898 echo area message is erased. Only remember one level
1899 of previous cursor position. */
1900 if (previous_pos_X == -1)
1901 ScreenGetCursor (&dummy, &previous_pos_X);
1902 }
1903 else if (previous_pos_X >= 0)
1904 {
1905 /* We wind up here after the echo area message is erased.
1906 Restore the cursor position we remembered above. */
1907 tem_X = previous_pos_X;
1908 previous_pos_X = -1;
1909 }
9a599a60 1910
06da1de1 1911 if (current_pos_X != tem_X)
9a599a60
EZ
1912 {
1913 new_pos_X = tem_X;
1914 update_cursor_pos = 1;
1915 }
fc171623 1916 }
2d764c78 1917#endif
fc171623
KH
1918
1919 if (update_cursor_pos
1920 && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
1921 {
1922 ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
1923 if (termscript)
1924 fprintf (termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
1925 }
1926
1927 /* Maybe cursor is invisible, so make it visible. */
1928 IT_display_cursor (1);
1929
1930 /* Mouse pointer should be always visible if we are waiting for
1931 keyboard input. */
1932 if (!mouse_visible)
1933 mouse_on ();
1934}
1935
71f65669 1936static void
41ad069b 1937IT_update_begin (struct frame *f)
f32d4091 1938{
41ad069b 1939 struct display_info *display_info = FRAME_X_DISPLAY_INFO (f);
b9f80d41 1940 struct frame *mouse_face_frame = display_info->mouse_face_mouse_frame;
41ad069b
EZ
1941
1942 BLOCK_INPUT;
1943
b9f80d41 1944 if (f && f == mouse_face_frame)
41ad069b
EZ
1945 {
1946 /* Don't do highlighting for mouse motion during the update. */
1947 display_info->mouse_face_defer = 1;
1948
1949 /* If F needs to be redrawn, simply forget about any prior mouse
1950 highlighting. */
1951 if (FRAME_GARBAGED_P (f))
1952 display_info->mouse_face_window = Qnil;
1953
1954 /* Can we tell that this update does not affect the window
1955 where the mouse highlight is? If so, no need to turn off.
48c14970
EZ
1956 Likewise, don't do anything if none of the enabled rows
1957 contains glyphs highlighted in mouse face. */
8ccb9a54
EZ
1958 if (!NILP (display_info->mouse_face_window)
1959 && WINDOWP (display_info->mouse_face_window))
41ad069b
EZ
1960 {
1961 struct window *w = XWINDOW (display_info->mouse_face_window);
1962 int i;
1963
8ccb9a54
EZ
1964 /* If the mouse highlight is in the window that was deleted
1965 (e.g., if it was popped by completion), clear highlight
1966 unconditionally. */
1967 if (NILP (w->buffer))
1968 display_info->mouse_face_window = Qnil;
1969 else
1970 {
1971 for (i = 0; i < w->desired_matrix->nrows; ++i)
48c14970
EZ
1972 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i)
1973 && MATRIX_ROW (w->current_matrix, i)->mouse_face_p)
8ccb9a54
EZ
1974 break;
1975 }
41ad069b 1976
8ccb9a54 1977 if (NILP (w->buffer) || i < w->desired_matrix->nrows)
41ad069b
EZ
1978 clear_mouse_face (display_info);
1979 }
1980 }
b9f80d41 1981 else if (mouse_face_frame && !FRAME_LIVE_P (mouse_face_frame))
41ad069b
EZ
1982 {
1983 /* If the frame with mouse highlight was deleted, invalidate the
1984 highlight info. */
1985 display_info->mouse_face_beg_row = display_info->mouse_face_beg_col = -1;
1986 display_info->mouse_face_end_row = display_info->mouse_face_end_col = -1;
1987 display_info->mouse_face_window = Qnil;
1988 display_info->mouse_face_deferred_gc = 0;
1989 display_info->mouse_face_mouse_frame = NULL;
1990 }
1991
1992 UNBLOCK_INPUT;
f32d4091
KS
1993}
1994
71f65669 1995static void
41ad069b 1996IT_update_end (struct frame *f)
f32d4091 1997{
41ad069b
EZ
1998 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
1999}
2000
2001static void
2002IT_frame_up_to_date (struct frame *f)
2003{
2004 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
0c3cfc51
EZ
2005 Lisp_Object new_cursor, frame_desired_cursor;
2006 struct window *sw;
41ad069b
EZ
2007
2008 if (dpyinfo->mouse_face_deferred_gc
b9f80d41 2009 || (f && f == dpyinfo->mouse_face_mouse_frame))
41ad069b
EZ
2010 {
2011 BLOCK_INPUT;
2012 if (dpyinfo->mouse_face_mouse_frame)
2013 IT_note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
2014 dpyinfo->mouse_face_mouse_x,
2015 dpyinfo->mouse_face_mouse_y);
2016 dpyinfo->mouse_face_deferred_gc = 0;
2017 UNBLOCK_INPUT;
2018 }
2019
0c3cfc51
EZ
2020 /* Set the cursor type to whatever they wanted. In a minibuffer
2021 window, we want the cursor to appear only if we are reading input
2022 from this window, and we want the cursor to be taken from the
2023 frame parameters. For the selected window, we use either its
2024 buffer-local value or the value from the frame parameters if the
2025 buffer doesn't define its local value for the cursor type. */
2026 sw = XWINDOW (f->selected_window);
2027 frame_desired_cursor = Fcdr (Fassq (Qcursor_type, f->param_alist));
2028 if (cursor_in_echo_area
2029 && FRAME_HAS_MINIBUF_P (f)
2030 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window)
2031 && sw == XWINDOW (echo_area_window))
2032 new_cursor = frame_desired_cursor;
2033 else
2034 {
2035 struct buffer *b = XBUFFER (sw->buffer);
2036
2037 if (EQ (b->cursor_type, Qt))
2038 new_cursor = frame_desired_cursor;
2039 else if (NILP (b->cursor_type)) /* nil means no cursor */
2040 new_cursor = Fcons (Qbar, make_number (0));
2041 else
2042 new_cursor = b->cursor_type;
2043 }
2044
2045 IT_set_cursor_type (f, new_cursor);
8ba01a32 2046
41ad069b 2047 IT_cmgoto (f); /* position cursor when update is done */
f32d4091 2048}
1b94449f 2049
c77f6f1b
EZ
2050/* Copy LEN glyphs displayed on a single line whose vertical position
2051 is YPOS, beginning at horizontal position XFROM to horizontal
2052 position XTO, by moving blocks in the video memory. Used by
2053 functions that insert and delete glyphs. */
2054static void
2055IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
2056{
2057 /* The offsets of source and destination relative to the
2058 conventional memorty selector. */
2059 int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
2060 int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;
2061
2062 if (from == to || len <= 0)
2063 return;
2064
2065 _farsetsel (_dos_ds);
2066
2067 /* The source and destination might overlap, so we need to move
2068 glyphs non-destructively. */
2069 if (from > to)
2070 {
2071 for ( ; len; from += 2, to += 2, len--)
2072 _farnspokew (to, _farnspeekw (from));
2073 }
2074 else
2075 {
2076 from += (len - 1) * 2;
2077 to += (len - 1) * 2;
2078 for ( ; len; from -= 2, to -= 2, len--)
2079 _farnspokew (to, _farnspeekw (from));
2080 }
2081 if (screen_virtual_segment)
2082 dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
2083}
2084
2085/* Insert and delete glyphs. */
aa9ce936
EZ
2086static void
2087IT_insert_glyphs (start, len)
c77f6f1b 2088 register struct glyph *start;
aa9ce936
EZ
2089 register int len;
2090{
c77f6f1b
EZ
2091 int shift_by_width = screen_size_X - (new_pos_X + len);
2092
2093 /* Shift right the glyphs from the nominal cursor position to the
2094 end of this line. */
2095 IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);
2096
2097 /* Now write the glyphs to be inserted. */
2098 IT_write_glyphs (start, len);
aa9ce936
EZ
2099}
2100
2101static void
2102IT_delete_glyphs (n)
2103 register int n;
2104{
2105 abort ();
2106}
2107
211c7152 2108/* set-window-configuration on window.c needs this. */
3bb1f22f
RS
2109void
2110x_set_menu_bar_lines (f, value, oldval)
2111 struct frame *f;
2112 Lisp_Object value, oldval;
2113{
211c7152
EZ
2114 set_menu_bar_lines (f, value, oldval);
2115}
3bb1f22f 2116
984f5aaa 2117/* This was copied from xfaces.c */
3bb1f22f 2118
984f5aaa
EZ
2119extern Lisp_Object Qbackground_color;
2120extern Lisp_Object Qforeground_color;
8ba01a32 2121Lisp_Object Qreverse;
211c7152 2122extern Lisp_Object Qtitle;
3bb1f22f 2123
48ffe371
RS
2124/* IT_set_terminal_modes is called when emacs is started,
2125 resumed, and whenever the screen is redrawn! */
f32d4091 2126
71f65669 2127static void
f32d4091
KS
2128IT_set_terminal_modes (void)
2129{
aee81730 2130 if (termscript)
f32d4091 2131 fprintf (termscript, "\n<SET_TERM>");
f32d4091
KS
2132
2133 screen_size_X = ScreenCols ();
2134 screen_size_Y = ScreenRows ();
2135 screen_size = screen_size_X * screen_size_Y;
56e19ec4 2136
f32d4091
KS
2137 new_pos_X = new_pos_Y = 0;
2138 current_pos_X = current_pos_Y = -1;
2139
2140 if (term_setup_done)
2141 return;
2142 term_setup_done = 1;
56e19ec4 2143
f32d4091
KS
2144 startup_screen_size_X = screen_size_X;
2145 startup_screen_size_Y = screen_size_Y;
c9adab25 2146 startup_screen_attrib = ScreenAttrib;
f32d4091 2147
039274cf
EZ
2148#if __DJGPP__ > 1
2149 /* Is DOS/V (or any other RSIS software which relocates
2150 the screen) installed? */
2151 {
2152 unsigned short es_value;
2153 __dpmi_regs regs;
2154
2155 regs.h.ah = 0xfe; /* get relocated screen address */
2156 if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
2157 regs.x.es = (ScreenPrimary >> 4) & 0xffff;
2158 else if (screen_old_address) /* already switched to Japanese mode once */
2159 regs.x.es = (screen_old_address >> 4) & 0xffff;
2160 else
2161 regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
2162 regs.x.di = 0;
2163 es_value = regs.x.es;
2164 __dpmi_int (0x10, &regs);
2165
d1d5dc19 2166 if (regs.x.es != es_value)
039274cf 2167 {
d1d5dc19
EZ
2168 /* screen_old_address is only set if ScreenPrimary does NOT
2169 already point to the relocated buffer address returned by
2170 the Int 10h/AX=FEh call above. DJGPP v2.02 and later sets
2171 ScreenPrimary to that address at startup under DOS/V. */
2172 if (regs.x.es != (ScreenPrimary >> 4) & 0xffff)
2173 screen_old_address = ScreenPrimary;
039274cf
EZ
2174 screen_virtual_segment = regs.x.es;
2175 screen_virtual_offset = regs.x.di;
2176 ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
2177 }
2178 }
2179#endif /* __DJGPP__ > 1 */
2180
f32d4091
KS
2181 ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
2182 ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));
2183
2184 if (termscript)
c9adab25 2185 fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
039274cf 2186 screen_size_X, screen_size_Y);
76ac1508
RS
2187
2188 bright_bg ();
f32d4091
KS
2189}
2190
48ffe371
RS
2191/* IT_reset_terminal_modes is called when emacs is
2192 suspended or killed. */
f32d4091 2193
71f65669 2194static void
f32d4091
KS
2195IT_reset_terminal_modes (void)
2196{
c9adab25
KH
2197 int display_row_start = (int) ScreenPrimary;
2198 int saved_row_len = startup_screen_size_X * 2;
56e19ec4 2199 int update_row_len = ScreenCols () * 2, current_rows = ScreenRows ();
c9adab25
KH
2200 int to_next_row = update_row_len;
2201 unsigned char *saved_row = startup_screen_buffer;
56e19ec4 2202 int cursor_pos_X = ScreenCols () - 1, cursor_pos_Y = ScreenRows () - 1;
c9adab25 2203
f32d4091 2204 if (termscript)
5063b150 2205 fprintf (termscript, "\n<RESET_TERM>");
f32d4091 2206
f32d4091
KS
2207 if (!term_setup_done)
2208 return;
56e19ec4 2209
c9adab25 2210 mouse_off ();
b36701cc
RS
2211
2212 /* Leave the video system in the same state as we found it,
2213 as far as the blink/bright-background bit is concerned. */
2214 maybe_enable_blinking ();
06b1ea13 2215
c9adab25
KH
2216 /* We have a situation here.
2217 We cannot just do ScreenUpdate(startup_screen_buffer) because
2218 the luser could have changed screen dimensions inside Emacs
2219 and failed (or didn't want) to restore them before killing
2220 Emacs. ScreenUpdate() uses the *current* screen dimensions and
2221 thus will happily use memory outside what was allocated for
2222 `startup_screen_buffer'.
2223 Thus we only restore as much as the current screen dimensions
2224 can hold, and clear the rest (if the saved screen is smaller than
2225 the current) with the color attribute saved at startup. The cursor
2226 is also restored within the visible dimensions. */
2227
2228 ScreenAttrib = startup_screen_attrib;
c9adab25 2229
06b1ea13
EZ
2230 /* Don't restore the screen if we are exiting less than 2 seconds
2231 after startup: we might be crashing, and the screen might show
2232 some vital clues to what's wrong. */
2233 if (clock () - startup_time >= 2*CLOCKS_PER_SEC)
c9adab25 2234 {
06b1ea13 2235 ScreenClear ();
039274cf 2236 if (screen_virtual_segment)
06b1ea13
EZ
2237 dosv_refresh_virtual_screen (0, screen_size);
2238
2239 if (update_row_len > saved_row_len)
2240 update_row_len = saved_row_len;
2241 if (current_rows > startup_screen_size_Y)
2242 current_rows = startup_screen_size_Y;
2243
2244 if (termscript)
2245 fprintf (termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
2246 update_row_len / 2, current_rows);
2247
2248 while (current_rows--)
2249 {
2250 dosmemput (saved_row, update_row_len, display_row_start);
2251 if (screen_virtual_segment)
2252 dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
2253 update_row_len / 2);
2254 saved_row += saved_row_len;
2255 display_row_start += to_next_row;
2256 }
c9adab25
KH
2257 }
2258 if (startup_pos_X < cursor_pos_X)
2259 cursor_pos_X = startup_pos_X;
2260 if (startup_pos_Y < cursor_pos_Y)
2261 cursor_pos_Y = startup_pos_Y;
2262
2263 ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
2264 xfree (startup_screen_buffer);
f32d4091
KS
2265
2266 term_setup_done = 0;
2267}
2268
71f65669
EZ
2269static void
2270IT_set_terminal_window (int foo)
f32d4091
KS
2271{
2272}
2273
2d764c78
EZ
2274/* Remember the screen colors of the curent frame, to serve as the
2275 default colors for newly-created frames. */
2d764c78
EZ
2276DEFUN ("msdos-remember-default-colors", Fmsdos_remember_default_colors,
2277 Smsdos_remember_default_colors, 1, 1, 0,
70da46c3 2278 doc: /* Remember the screen colors of the current frame. */)
2d764c78
EZ
2279 (frame)
2280 Lisp_Object frame;
2281{
2d764c78
EZ
2282 struct frame *f;
2283
b7826503 2284 CHECK_FRAME (frame);
2d764c78 2285 f= XFRAME (frame);
2d764c78 2286
ff12cd1d
EZ
2287 /* This function is called after applying default-frame-alist to the
2288 initial frame. At that time, if reverse-colors option was
2289 specified in default-frame-alist, it was already applied, and
2290 frame colors are reversed. We need to account for that. */
2291 if (EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt))
2292 {
2293 initial_screen_colors[0] = FRAME_BACKGROUND_PIXEL (f);
2294 initial_screen_colors[1] = FRAME_FOREGROUND_PIXEL (f);
2295 }
2296 else
2297 {
2298 initial_screen_colors[0] = FRAME_FOREGROUND_PIXEL (f);
2299 initial_screen_colors[1] = FRAME_BACKGROUND_PIXEL (f);
2300 }
2d764c78
EZ
2301}
2302
f32d4091 2303void
3bb1f22f 2304IT_set_frame_parameters (f, alist)
c77f6f1b 2305 struct frame *f;
f32d4091
KS
2306 Lisp_Object alist;
2307{
2308 Lisp_Object tail;
56e19ec4 2309 int i, j, length = XINT (Flength (alist));
db722735
RS
2310 Lisp_Object *parms
2311 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2312 Lisp_Object *values
2313 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2d764c78 2314 /* Do we have to reverse the foreground and background colors? */
8ba01a32 2315 int reverse = EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt);
56e19ec4 2316 int need_to_reverse, was_reverse = reverse;
2d764c78 2317 int redraw = 0, fg_set = 0, bg_set = 0;
56e19ec4 2318 unsigned long orig_fg, orig_bg;
546701f5 2319 Lisp_Object frame_bg, frame_fg;
53704a07 2320 extern Lisp_Object Qdefault, QCforeground, QCbackground;
2d764c78
EZ
2321
2322 /* If we are creating a new frame, begin with the original screen colors
2323 used for the initial frame. */
238a44a8 2324 if (EQ (alist, Vdefault_frame_alist)
2d764c78
EZ
2325 && initial_screen_colors[0] != -1 && initial_screen_colors[1] != -1)
2326 {
2327 FRAME_FOREGROUND_PIXEL (f) = initial_screen_colors[0];
2328 FRAME_BACKGROUND_PIXEL (f) = initial_screen_colors[1];
2329 }
2330 orig_fg = FRAME_FOREGROUND_PIXEL (f);
2331 orig_bg = FRAME_BACKGROUND_PIXEL (f);
546701f5
EZ
2332 frame_fg = Fcdr (Fassq (Qforeground_color, f->param_alist));
2333 frame_bg = Fcdr (Fassq (Qbackground_color, f->param_alist));
2334 /* frame_fg and frame_bg could be nil if, for example,
2335 f->param_alist is nil, e.g. if we are called from
2336 Fmake_terminal_frame. */
2337 if (NILP (frame_fg))
2338 frame_fg = build_string (unspecified_fg);
2339 if (NILP (frame_bg))
2340 frame_bg = build_string (unspecified_bg);
db722735
RS
2341
2342 /* Extract parm names and values into those vectors. */
2343 i = 0;
f32d4091
KS
2344 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
2345 {
db722735 2346 Lisp_Object elt;
f32d4091
KS
2347
2348 elt = Fcar (tail);
db722735 2349 parms[i] = Fcar (elt);
b7826503 2350 CHECK_SYMBOL (parms[i]);
db722735
RS
2351 values[i] = Fcdr (elt);
2352 i++;
2353 }
2354
2d764c78 2355 j = i;
db722735 2356
2d764c78
EZ
2357 for (i = 0; i < j; i++)
2358 {
1e21fe48
EZ
2359 Lisp_Object prop, val;
2360
2361 prop = parms[i];
2362 val = values[i];
2d764c78 2363
8ba01a32 2364 if (EQ (prop, Qreverse))
2d764c78
EZ
2365 reverse = EQ (val, Qt);
2366 }
546701f5
EZ
2367
2368 need_to_reverse = reverse && !was_reverse;
2369 if (termscript && need_to_reverse)
2d764c78
EZ
2370 fprintf (termscript, "<INVERSE-VIDEO>\n");
2371
2372 /* Now process the alist elements in reverse of specified order. */
db722735
RS
2373 for (i--; i >= 0; i--)
2374 {
56e19ec4 2375 Lisp_Object prop, val, frame;
1e21fe48
EZ
2376
2377 prop = parms[i];
2378 val = values[i];
f32d4091 2379
4e825084 2380 if (EQ (prop, Qforeground_color))
f32d4091 2381 {
546701f5 2382 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2d764c78
EZ
2383 ? LFACE_BACKGROUND_INDEX
2384 : LFACE_FOREGROUND_INDEX);
3b620731
EZ
2385 if (new_color != FACE_TTY_DEFAULT_COLOR
2386 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2387 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
f32d4091 2388 {
546701f5 2389 FRAME_FOREGROUND_PIXEL (f) = new_color;
1e21fe48
EZ
2390 /* Make sure the foreground of the default face for this
2391 frame is changed as well. */
2392 XSETFRAME (frame, f);
546701f5 2393 if (need_to_reverse)
1e21fe48 2394 {
1e21fe48
EZ
2395 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2396 val, frame);
546701f5 2397 prop = Qbackground_color;
ff12cd1d 2398 bg_set = 1;
1e21fe48 2399 }
2d764c78 2400 else
1e21fe48 2401 {
1e21fe48
EZ
2402 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2403 val, frame);
ff12cd1d 2404 fg_set = 1;
1e21fe48 2405 }
f32d4091 2406 redraw = 1;
76ac1508 2407 if (termscript)
a7cf9151 2408 fprintf (termscript, "<FGCOLOR %lu>\n", new_color);
f32d4091
KS
2409 }
2410 }
4e825084 2411 else if (EQ (prop, Qbackground_color))
f32d4091 2412 {
546701f5 2413 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2d764c78
EZ
2414 ? LFACE_FOREGROUND_INDEX
2415 : LFACE_BACKGROUND_INDEX);
3b620731
EZ
2416 if (new_color != FACE_TTY_DEFAULT_COLOR
2417 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2418 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
f32d4091 2419 {
546701f5 2420 FRAME_BACKGROUND_PIXEL (f) = new_color;
1e21fe48
EZ
2421 /* Make sure the background of the default face for this
2422 frame is changed as well. */
2423 XSETFRAME (frame, f);
546701f5 2424 if (need_to_reverse)
1e21fe48 2425 {
1e21fe48
EZ
2426 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2427 val, frame);
546701f5 2428 prop = Qforeground_color;
ff12cd1d 2429 fg_set = 1;
1e21fe48 2430 }
2d764c78 2431 else
1e21fe48 2432 {
1e21fe48
EZ
2433 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2434 val, frame);
ff12cd1d 2435 bg_set = 1;
1e21fe48 2436 }
f32d4091 2437 redraw = 1;
76ac1508 2438 if (termscript)
a7cf9151 2439 fprintf (termscript, "<BGCOLOR %lu>\n", new_color);
f32d4091
KS
2440 }
2441 }
211c7152
EZ
2442 else if (EQ (prop, Qtitle))
2443 {
2444 x_set_title (f, val);
2445 if (termscript)
d5db4077 2446 fprintf (termscript, "<TITLE: %s>\n", SDATA (val));
211c7152 2447 }
8ba01a32
EZ
2448 else if (EQ (prop, Qcursor_type))
2449 {
2450 IT_set_cursor_type (f, val);
2451 if (termscript)
2452 fprintf (termscript, "<CTYPE: %s>\n",
a0738471
EZ
2453 EQ (val, Qbar) || EQ (val, Qhbar)
2454 || CONSP (val) && (EQ (XCAR (val), Qbar)
2455 || EQ (XCAR (val), Qhbar))
8ba01a32
EZ
2456 ? "bar" : "box");
2457 }
db722735 2458 store_frame_param (f, prop, val);
2d764c78 2459 }
db722735 2460
2d764c78
EZ
2461 /* If they specified "reverse", but not the colors, we need to swap
2462 the current frame colors. */
546701f5 2463 if (need_to_reverse)
2d764c78 2464 {
1e21fe48
EZ
2465 Lisp_Object frame;
2466
2d764c78
EZ
2467 if (!fg_set)
2468 {
1e21fe48 2469 XSETFRAME (frame, f);
ff12cd1d
EZ
2470 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2471 tty_color_name (f, orig_bg),
53704a07 2472 frame);
2d764c78
EZ
2473 redraw = 1;
2474 }
2475 if (!bg_set)
2476 {
1e21fe48 2477 XSETFRAME (frame, f);
ff12cd1d
EZ
2478 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2479 tty_color_name (f, orig_fg),
53704a07 2480 frame);
2d764c78
EZ
2481 redraw = 1;
2482 }
f32d4091
KS
2483 }
2484
2485 if (redraw)
2486 {
2d764c78
EZ
2487 face_change_count++; /* forces xdisp.c to recompute basic faces */
2488 if (f == SELECTED_FRAME())
3bb1f22f 2489 redraw_frame (f);
f32d4091
KS
2490 }
2491}
2492
a7cf9151
EZ
2493extern void init_frame_faces (FRAME_PTR);
2494
f32d4091
KS
2495#endif /* !HAVE_X_WINDOWS */
2496
2497
48ffe371
RS
2498/* Do we need the internal terminal? */
2499
f32d4091
KS
2500void
2501internal_terminal_init ()
2502{
56e19ec4 2503 char *term = getenv ("TERM"), *colors;
2d764c78 2504 struct frame *sf = SELECTED_FRAME();
f32d4091
KS
2505
2506#ifdef HAVE_X_WINDOWS
2507 if (!inhibit_window_system)
2508 return;
2509#endif
2510
2511 internal_terminal
2512 = (!noninteractive) && term && !strcmp (term, "internal");
2513
2514 if (getenv ("EMACSTEST"))
5063b150 2515 termscript = fopen (getenv ("EMACSTEST"), "wt");
56e19ec4 2516
f32d4091
KS
2517#ifndef HAVE_X_WINDOWS
2518 if (!internal_terminal || inhibit_window_system)
2519 {
2d764c78 2520 sf->output_method = output_termcap;
f32d4091
KS
2521 return;
2522 }
2523
2524 Vwindow_system = intern ("pc");
2525 Vwindow_system_version = make_number (1);
2d764c78 2526 sf->output_method = output_msdos_raw;
039274cf
EZ
2527
2528 /* If Emacs was dumped on DOS/V machine, forget the stale VRAM address. */
2529 screen_old_address = 0;
2530
2d764c78
EZ
2531 /* Forget the stale screen colors as well. */
2532 initial_screen_colors[0] = initial_screen_colors[1] = -1;
2533
f32d4091 2534 bzero (&the_only_x_display, sizeof the_only_x_display);
5bcee7ef
KL
2535 FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()) = 7; /* White */
2536 FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ()) = 0; /* Black */
76ac1508 2537 bright_bg ();
5063b150 2538 colors = getenv ("EMACSCOLORS");
f32d4091
KS
2539 if (colors && strlen (colors) >= 2)
2540 {
76ac1508
RS
2541 /* The colors use 4 bits each (we enable bright background). */
2542 if (isdigit (colors[0]))
2543 colors[0] -= '0';
2544 else if (isxdigit (colors[0]))
2545 colors[0] -= (isupper (colors[0]) ? 'A' : 'a') - 10;
1bd7b2c7 2546 if (colors[0] >= 0 && colors[0] < 16)
5bcee7ef 2547 FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ()) = colors[0];
76ac1508
RS
2548 if (isdigit (colors[1]))
2549 colors[1] -= '0';
2550 else if (isxdigit (colors[1]))
2551 colors[1] -= (isupper (colors[1]) ? 'A' : 'a') - 10;
2552 if (colors[1] >= 0 && colors[1] < 16)
5bcee7ef 2553 FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()) = colors[1];
f32d4091 2554 }
64ec6a02 2555 the_only_x_display.font = (XFontStruct *)1; /* must *not* be zero */
41ad069b
EZ
2556 the_only_x_display.display_info.mouse_face_mouse_frame = NULL;
2557 the_only_x_display.display_info.mouse_face_deferred_gc = 0;
2558 the_only_x_display.display_info.mouse_face_beg_row =
2559 the_only_x_display.display_info.mouse_face_beg_col = -1;
2560 the_only_x_display.display_info.mouse_face_end_row =
2561 the_only_x_display.display_info.mouse_face_end_col = -1;
2562 the_only_x_display.display_info.mouse_face_face_id = DEFAULT_FACE_ID;
2563 the_only_x_display.display_info.mouse_face_window = Qnil;
2564 the_only_x_display.display_info.mouse_face_mouse_x =
2565 the_only_x_display.display_info.mouse_face_mouse_y = 0;
2566 the_only_x_display.display_info.mouse_face_defer = 0;
abf10ed7 2567 the_only_x_display.display_info.mouse_face_hidden = 0;
f32d4091 2568
2d764c78 2569 init_frame_faces (sf);
f32d4091
KS
2570
2571 ring_bell_hook = IT_ring_bell;
aa9ce936
EZ
2572 insert_glyphs_hook = IT_insert_glyphs;
2573 delete_glyphs_hook = IT_delete_glyphs;
f32d4091
KS
2574 write_glyphs_hook = IT_write_glyphs;
2575 cursor_to_hook = raw_cursor_to_hook = IT_cursor_to;
2576 clear_to_end_hook = IT_clear_to_end;
2577 clear_end_of_line_hook = IT_clear_end_of_line;
2578 clear_frame_hook = IT_clear_screen;
f32d4091
KS
2579 update_begin_hook = IT_update_begin;
2580 update_end_hook = IT_update_end;
41ad069b 2581 frame_up_to_date_hook = IT_frame_up_to_date;
f32d4091
KS
2582
2583 /* These hooks are called by term.c without being checked. */
2584 set_terminal_modes_hook = IT_set_terminal_modes;
2585 reset_terminal_modes_hook = IT_reset_terminal_modes;
2586 set_terminal_window_hook = IT_set_terminal_window;
28d440ab 2587 TTY_CHAR_INS_DEL_OK (CURTTY ()) = 0;
f32d4091
KS
2588#endif
2589}
2590
2591dos_get_saved_screen (screen, rows, cols)
2592 char **screen;
2593 int *rows;
2594 int *cols;
2595{
2596#ifndef HAVE_X_WINDOWS
2597 *screen = startup_screen_buffer;
2598 *cols = startup_screen_size_X;
2599 *rows = startup_screen_size_Y;
039274cf 2600 return *screen != (char *)0;
f32d4091
KS
2601#else
2602 return 0;
56e19ec4 2603#endif
f32d4091 2604}
3bb1f22f
RS
2605
2606#ifndef HAVE_X_WINDOWS
2607
2608/* We are not X, but we can emulate it well enough for our needs... */
2609void
2610check_x (void)
2611{
2d764c78
EZ
2612 if (! FRAME_MSDOS_P (SELECTED_FRAME()))
2613 error ("Not running under a window system");
3bb1f22f
RS
2614}
2615
2616#endif
2617
5063b150 2618\f
f32d4091
KS
2619/* ----------------------- Keyboard control ----------------------
2620 *
2621 * Keymaps reflect the following keyboard layout:
2622 *
2623 * 0 1 2 3 4 5 6 7 8 9 10 11 12 BS
2624 * TAB 15 16 17 18 19 20 21 22 23 24 25 26 (41)
2625 * CLOK 30 31 32 33 34 35 36 37 38 39 40 (41) RET
2626 * SH () 45 46 47 48 49 50 51 52 53 54 SHIFT
2627 * SPACE
2628 */
2629
d1d5dc19
EZ
2630#define Ignore 0x0000
2631#define Normal 0x0000 /* normal key - alt changes scan-code */
2632#define FctKey 0x1000 /* func key if c == 0, else c */
2633#define Special 0x2000 /* func key even if c != 0 */
2634#define ModFct 0x3000 /* special if mod-keys, else 'c' */
2635#define Map 0x4000 /* alt scan-code, map to unshift/shift key */
2636#define KeyPad 0x5000 /* map to insert/kp-0 depending on c == 0xe0 */
2637#define Grey 0x6000 /* Grey keypad key */
2638
2639#define Alt 0x0100 /* alt scan-code */
2640#define Ctrl 0x0200 /* ctrl scan-code */
2641#define Shift 0x0400 /* shift scan-code */
2642
db9cd97a 2643static int extended_kbd; /* 101 (102) keyboard present. */
f32d4091 2644
d1d5dc19
EZ
2645struct kbd_translate {
2646 unsigned char sc;
2647 unsigned char ch;
2648 unsigned short code;
2649};
2650
f32d4091
KS
2651struct dos_keyboard_map
2652{
2653 char *unshifted;
2654 char *shifted;
2655 char *alt_gr;
d1d5dc19 2656 struct kbd_translate *translate_table;
f32d4091
KS
2657};
2658
2659
2660static struct dos_keyboard_map us_keyboard = {
2661/* 0 1 2 3 4 5 */
2662/* 01234567890123456789012345678901234567890 12345678901234 */
2663 "`1234567890-= qwertyuiop[] asdfghjkl;'\\ zxcvbnm,./ ",
2664/* 0123456789012345678901234567890123456789 012345678901234 */
2665 "~!@#$%^&*()_+ QWERTYUIOP{} ASDFGHJKL:\"| ZXCVBNM<>? ",
d1d5dc19
EZ
2666 0, /* no Alt-Gr key */
2667 0 /* no translate table */
f32d4091
KS
2668};
2669
2670static struct dos_keyboard_map fr_keyboard = {
2671/* 0 1 2 3 4 5 */
2672/* 012 3456789012345678901234567890123456789012345678901234 */
2673