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