msdos.c 115 KB
Newer Older
1
/* MS-DOS specific C utilities.          -*- coding: cp850 -*-
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1993-1997, 1999-2019 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
Paul Eggert's avatar
Paul Eggert committed
18
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
19

20
/* Contributed by Morten Welinder */
Kim F. Storm's avatar
Kim F. Storm committed
21
/* New display, keyboard, and mouse control by Kim F. Storm */
22

23 24 25 26 27 28 29
/* Note: This file MUST use a unibyte encoding, to both display the
   keys on the non-US keyboard layout as their respective labels, and
   provide the correct byte values for the keyboard input to inject
   into Emacs.  See 'struct dos_keyboard_map' below.  As long as there
   are only European keyboard layouts here, we are OK with DOS
   codepage 850 encoding.  */

Richard M. Stallman's avatar
Richard M. Stallman committed
30 31
/* Note: some of the stuff here was taken from end of sysdep.c in demacs. */

Richard M. Stallman's avatar
Richard M. Stallman committed
32
#include <config.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
33 34

#ifdef MSDOS
35
#include <setjmp.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
36 37
#include "lisp.h"
#include <stdio.h>
38
#include <time.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
39 40
#include <sys/param.h>
#include <sys/time.h>
Paul Eggert's avatar
Paul Eggert committed
41
/* gettime and settime in dos.h clash with their namesakes from
42 43 44
   gnulib, so we move out of our way the prototypes in dos.h.  */
#define gettime dos_h_gettime_
#define settime dos_h_settime_
Richard M. Stallman's avatar
Richard M. Stallman committed
45
#include <dos.h>
46 47
#undef gettime
#undef settime
48 49
#include <errno.h>
#include <sys/stat.h>    /* for _fixpath */
50
#include <unistd.h>	 /* for chdir, dup, dup2, etc. */
51
#include <dir.h>	 /* for getdisk */
52
#pragma pack(0)		 /* dir.h does a pack(4), which isn't GCC's default */
Eli Zaretskii's avatar
Eli Zaretskii committed
53 54
#undef opendir
#include <dirent.h>	 /* for opendir */
55
#include <fcntl.h>
56
#include <io.h>		 /* for setmode */
57 58
#include <dpmi.h>	 /* for __dpmi_xxx stuff */
#include <sys/farptr.h>	 /* for _farsetsel, _farnspokeb */
59
#include <libc/dosio.h>  /* for _USE_LFN */
60
#include <conio.h>	 /* for cputs */
61

Eli Zaretskii's avatar
Eli Zaretskii committed
62 63 64 65 66 67
#if (__DJGPP__ + (__DJGPP_MINOR__ > 3)) >= 3
#define SYS_ENVIRON _environ
#else
#define SYS_ENVIRON environ
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
68 69
#include "msdos.h"
#include "systime.h"
70
#include "frame.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
71
#include "termhooks.h"
72
#include "termchar.h"
Morten Welinder's avatar
Morten Welinder committed
73
#include "dispextern.h"
74
#include "dosfns.h"
Morten Welinder's avatar
Morten Welinder committed
75
#include "termopts.h"
76
#include "character.h"
77 78
#include "coding.h"
#include "disptab.h"
Morten Welinder's avatar
Morten Welinder committed
79
#include "window.h"
80
#include "menu.h"
81 82
#include "buffer.h"
#include "commands.h"
83
#include "blockinput.h"
Eli Zaretskii's avatar
Eli Zaretskii committed
84
#include "keyboard.h"
Kenichi Handa's avatar
Kenichi Handa committed
85
#include "intervals.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
86 87 88 89
#include <go32.h>
#include <pc.h>
#include <ctype.h>
/* #include <process.h> */
90 91
/* Damn that local process.h!  Instead we can define P_WAIT and
   spawnve ourselves.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
92
#define P_WAIT 1
93
extern int spawnve (int, const char *, char *const [], char *const []);
Richard M. Stallman's avatar
Richard M. Stallman committed
94

95 96 97 98
#ifndef _USE_LFN
#define _USE_LFN 0
#endif

99 100 101 102
#ifndef _dos_ds
#define _dos_ds _go32_info_block.selector_for_linear_memory
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
103
#include <signal.h>
104
#include "syssignal.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
105

106 107 108
#include "careadlinkat.h"
#include "allocator.h"

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#ifndef SYSTEM_MALLOC

#ifdef GNU_MALLOC

/* If other `malloc' than ours is used, force our `sbrk' behave like
   Unix programs expect (resize memory blocks to keep them contiguous).
   If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
   because that's what `gmalloc' expects to get.  */
#include <crt0.h>

#ifdef REL_ALLOC
int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
#else  /* not REL_ALLOC */
int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
#endif /* not REL_ALLOC */
#endif /* GNU_MALLOC */

#endif /* not SYSTEM_MALLOC */
127

128
/* Return the current timestamp in milliseconds since midnight.  */
129
static unsigned long
130
event_timestamp (void)
131
{
132
  struct timespec t;
133
  unsigned long s;
Eli Zaretskii's avatar
Eli Zaretskii committed
134

135
  gettime (&t);
136 137
  s = t.tv_sec;
  s %= 86400;
138
  s *= 1000;
139
  s += t.tv_nsec * 1000000;
Eli Zaretskii's avatar
Eli Zaretskii committed
140

141 142 143
  return s;
}

Kim F. Storm's avatar
Kim F. Storm committed
144 145 146 147 148 149

/* ------------------------ Mouse control ---------------------------
 *
 * Coordinates are in screen positions and zero based.
 * Mouse buttons are numbered from left to right and also zero based.
 */
Richard M. Stallman's avatar
Richard M. Stallman committed
150

Eli Zaretskii's avatar
Eli Zaretskii committed
151 152 153 154
/* This used to be in termhooks.h, but mainstream Emacs code no longer
   uses it, and it was removed...  */
#define NUM_MOUSE_BUTTONS (5)

Kim F. Storm's avatar
Kim F. Storm committed
155 156
int have_mouse;          /* 0: no, 1: enabled, -1: disabled */
static int mouse_visible;
Richard M. Stallman's avatar
Richard M. Stallman committed
157

Kim F. Storm's avatar
Kim F. Storm committed
158 159
static int mouse_last_x;
static int mouse_last_y;
Richard M. Stallman's avatar
Richard M. Stallman committed
160

Kim F. Storm's avatar
Kim F. Storm committed
161 162
static int mouse_button_translate[NUM_MOUSE_BUTTONS];
static int mouse_button_count;
Richard M. Stallman's avatar
Richard M. Stallman committed
163

Kim F. Storm's avatar
Kim F. Storm committed
164
void
165
mouse_on (void)
Richard M. Stallman's avatar
Richard M. Stallman committed
166 167 168
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
169
  if (have_mouse > 0 && !mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
170
    {
171 172 173 174
      struct tty_display_info *tty = CURTTY ();

      if (tty->termscript)
	fprintf (tty->termscript, "<M_ON>");
Kim F. Storm's avatar
Kim F. Storm committed
175 176 177
      regs.x.ax = 0x0001;
      int86 (0x33, &regs, &regs);
      mouse_visible = 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
178 179 180
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
181
void
182
mouse_off (void)
Richard M. Stallman's avatar
Richard M. Stallman committed
183
{
Kim F. Storm's avatar
Kim F. Storm committed
184
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
185

Kim F. Storm's avatar
Kim F. Storm committed
186
  if (have_mouse > 0 && mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
187
    {
188 189 190 191
      struct tty_display_info *tty = CURTTY ();

      if (tty->termscript)
	fprintf (tty->termscript, "<M_OFF>");
Kim F. Storm's avatar
Kim F. Storm committed
192 193 194
      regs.x.ax = 0x0002;
      int86 (0x33, &regs, &regs);
      mouse_visible = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
195 196 197
    }
}

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static void
mouse_setup_buttons (int n_buttons)
{
  if (n_buttons == 3)
    {
      mouse_button_count = 3;
      mouse_button_translate[0] = 0; /* Left */
      mouse_button_translate[1] = 2; /* Middle */
      mouse_button_translate[2] = 1; /* Right */
    }
  else	/* two, what else? */
    {
      mouse_button_count = 2;
      mouse_button_translate[0] = 0;
      mouse_button_translate[1] = 1;
    }
}

DEFUN ("msdos-set-mouse-buttons", Fmsdos_set_mouse_buttons, Smsdos_set_mouse_buttons,
       1, 1, "NSet number of mouse buttons to: ",
218 219 220 221
       doc: /* Set the number of mouse buttons to use by Emacs.
This is useful with mice that report the number of buttons inconsistently,
e.g., if the number of buttons is reported as 3, but Emacs only sees 2 of
them.  This happens with wheeled mice on Windows 9X, for example.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
222
  (Lisp_Object nbuttons)
223
{
224 225
  int n;

226
  CHECK_FIXNUM (nbuttons);
Tom Tromey's avatar
Tom Tromey committed
227
  n = XFIXNUM (nbuttons);
228
  if (n < 2 || n > 3)
229 230 231
    xsignal2 (Qargs_out_of_range,
	      build_string ("only 2 or 3 mouse buttons are supported"),
	      nbuttons);
232
  mouse_setup_buttons (n);
233 234 235
  return Qnil;
}

236 237 238 239 240 241 242 243 244 245 246
static void
mouse_get_xy (int *x, int *y)
{
  union REGS regs;

  regs.x.ax = 0x0003;
  int86 (0x33, &regs, &regs);
  *x = regs.x.cx / 8;
  *y = regs.x.dx / 8;
}

Kim F. Storm's avatar
Kim F. Storm committed
247
void
248
mouse_moveto (int x, int y)
Richard M. Stallman's avatar
Richard M. Stallman committed
249
{
Kim F. Storm's avatar
Kim F. Storm committed
250
  union REGS regs;
251
  struct tty_display_info *tty = CURTTY ();
Richard M. Stallman's avatar
Richard M. Stallman committed
252

253 254
  if (tty->termscript)
    fprintf (tty->termscript, "<M_XY=%dx%d>", x, y);
Kim F. Storm's avatar
Kim F. Storm committed
255 256 257 258
  regs.x.ax = 0x0004;
  mouse_last_x = regs.x.cx = x * 8;
  mouse_last_y = regs.x.dx = y * 8;
  int86 (0x33, &regs, &regs);
Richard M. Stallman's avatar
Richard M. Stallman committed
259 260
}

Kim F. Storm's avatar
Kim F. Storm committed
261
static int
262
mouse_pressed (int b, int *xp, int *yp)
Richard M. Stallman's avatar
Richard M. Stallman committed
263
{
Kim F. Storm's avatar
Kim F. Storm committed
264
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
265

Kim F. Storm's avatar
Kim F. Storm committed
266 267 268 269 270 271 272 273
  if (b >= mouse_button_count)
    return 0;
  regs.x.ax = 0x0005;
  regs.x.bx = mouse_button_translate[b];
  int86 (0x33, &regs, &regs);
  if (regs.x.bx)
    *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
  return (regs.x.bx != 0);
Richard M. Stallman's avatar
Richard M. Stallman committed
274 275
}

Kim F. Storm's avatar
Kim F. Storm committed
276
static int
277
mouse_released (int b, int *xp, int *yp)
Richard M. Stallman's avatar
Richard M. Stallman committed
278 279 280
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
281 282 283 284 285 286 287 288
  if (b >= mouse_button_count)
    return 0;
  regs.x.ax = 0x0006;
  regs.x.bx = mouse_button_translate[b];
  int86 (0x33, &regs, &regs);
  if (regs.x.bx)
    *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
  return (regs.x.bx != 0);
Richard M. Stallman's avatar
Richard M. Stallman committed
289 290
}

291
static int
292
mouse_button_depressed (int b, int *xp, int *yp)
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
  union REGS regs;

  if (b >= mouse_button_count)
    return 0;
  regs.x.ax = 0x0003;
  int86 (0x33, &regs, &regs);
  if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
    {
      *xp = regs.x.cx / 8;
      *yp = regs.x.dx / 8;
      return 1;
    }
  return 0;
}

Kim F. Storm's avatar
Kim F. Storm committed
309
void
Dmitry Antipov's avatar
Dmitry Antipov committed
310
mouse_get_pos (struct frame **f, int insist, Lisp_Object *bar_window,
311
	       enum scroll_bar_part *part, Lisp_Object *x, Lisp_Object *y,
312
	       Time *time)
Kim F. Storm's avatar
Kim F. Storm committed
313 314
{
  int ix, iy;
315 316 317 318 319
  Lisp_Object frame, tail;

  /* Clear the mouse-moved flag for every frame on this display.  */
  FOR_EACH_FRAME (tail, frame)
    XFRAME (frame)->mouse_moved = 0;
Kim F. Storm's avatar
Kim F. Storm committed
320

Juanma Barranquero's avatar
Juanma Barranquero committed
321
  *f = SELECTED_FRAME ();
Kim F. Storm's avatar
Kim F. Storm committed
322 323 324
  *bar_window = Qnil;
  mouse_get_xy (&ix, &iy);
  *time = event_timestamp ();
325 326
  *x = make_fixnum (mouse_last_x = ix);
  *y = make_fixnum (mouse_last_y = iy);
Kim F. Storm's avatar
Kim F. Storm committed
327
}
Richard M. Stallman's avatar
Richard M. Stallman committed
328

Kim F. Storm's avatar
Kim F. Storm committed
329
static void
330
mouse_check_moved (void)
Richard M. Stallman's avatar
Richard M. Stallman committed
331
{
332
  int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
333

Kim F. Storm's avatar
Kim F. Storm committed
334
  mouse_get_xy (&x, &y);
Juanma Barranquero's avatar
Juanma Barranquero committed
335
  SELECTED_FRAME ()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
Kim F. Storm's avatar
Kim F. Storm committed
336 337 338
  mouse_last_x = x;
  mouse_last_y = y;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
339

Eli Zaretskii's avatar
Eli Zaretskii committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/* Force the mouse driver to ``forget'' about any button clicks until
   now.  */
static void
mouse_clear_clicks (void)
{
  int b;

  for (b = 0; b < mouse_button_count; b++)
    {
      int dummy_x, dummy_y;

      (void) mouse_pressed (b, &dummy_x, &dummy_y);
      (void) mouse_released (b, &dummy_x, &dummy_y);
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
356
void
357
mouse_init (void)
Kim F. Storm's avatar
Kim F. Storm committed
358 359
{
  union REGS regs;
360
  struct tty_display_info *tty = CURTTY ();
361

362 363
  if (tty->termscript)
    fprintf (tty->termscript, "<M_INIT>");
Richard M. Stallman's avatar
Richard M. Stallman committed
364

Kim F. Storm's avatar
Kim F. Storm committed
365 366
  regs.x.ax = 0x0021;
  int86 (0x33, &regs, &regs);
367

368 369 370 371
  /* Reset the mouse last press/release info.  It seems that Windows
     doesn't do that automatically when function 21h is called, which
     causes Emacs to ``remember'' the click that switched focus to the
     window just before Emacs was started from that window.  */
Eli Zaretskii's avatar
Eli Zaretskii committed
372
  mouse_clear_clicks ();
373

Kim F. Storm's avatar
Kim F. Storm committed
374 375 376 377
  regs.x.ax = 0x0007;
  regs.x.cx = 0;
  regs.x.dx = 8 * (ScreenCols () - 1);
  int86 (0x33, &regs, &regs);
Richard M. Stallman's avatar
Richard M. Stallman committed
378

Kim F. Storm's avatar
Kim F. Storm committed
379 380 381 382
  regs.x.ax = 0x0008;
  regs.x.cx = 0;
  regs.x.dx = 8 * (ScreenRows () - 1);
  int86 (0x33, &regs, &regs);
Richard M. Stallman's avatar
Richard M. Stallman committed
383

Kim F. Storm's avatar
Kim F. Storm committed
384 385 386
  mouse_moveto (0, 0);
  mouse_visible = 0;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
387

Kim F. Storm's avatar
Kim F. Storm committed
388 389 390
/* ------------------------- Screen control ----------------------
 *
 */
391

Kim F. Storm's avatar
Kim F. Storm committed
392
static int internal_terminal = 0;
393

Kim F. Storm's avatar
Kim F. Storm committed
394 395 396
#ifndef HAVE_X_WINDOWS
extern unsigned char ScreenAttrib;
static int screen_face;
397

Kim F. Storm's avatar
Kim F. Storm committed
398 399 400
static int screen_size_X;
static int screen_size_Y;
static int screen_size;
Richard M. Stallman's avatar
Richard M. Stallman committed
401

Kim F. Storm's avatar
Kim F. Storm committed
402 403 404 405
static int current_pos_X;
static int current_pos_Y;
static int new_pos_X;
static int new_pos_Y;
Richard M. Stallman's avatar
Richard M. Stallman committed
406

Kim F. Storm's avatar
Kim F. Storm committed
407 408 409 410 411
static void *startup_screen_buffer;
static int startup_screen_size_X;
static int startup_screen_size_Y;
static int startup_pos_X;
static int startup_pos_Y;
412
static unsigned char startup_screen_attrib;
Richard M. Stallman's avatar
Richard M. Stallman committed
413

414 415
static clock_t startup_time;

Kim F. Storm's avatar
Kim F. Storm committed
416
static int term_setup_done;
Richard M. Stallman's avatar
Richard M. Stallman committed
417

418 419
static unsigned short outside_cursor;

420
/* The only display since MS-DOS does not support multiple ones.  */
421
struct tty_display_info the_only_display_info;
Richard M. Stallman's avatar
Richard M. Stallman committed
422

423 424 425
/* The only tty_output, since MS-DOS supports only 1 display.  */
struct tty_output the_only_tty_output;

426 427 428 429 430 431 432 433
/* Support for DOS/V (allows Japanese characters to be displayed on
   standard, non-Japanese, ATs).  Only supported for DJGPP v2 and later.  */

/* Holds the address of the text-mode screen buffer.  */
static unsigned long screen_old_address = 0;
/* Segment and offset of the virtual screen.  If 0, DOS/V is NOT loaded.  */
static unsigned short screen_virtual_segment = 0;
static unsigned short screen_virtual_offset = 0;
434

Juanma Barranquero's avatar
Juanma Barranquero committed
435
/* The screen colors of the current frame, which serve as the default
436 437 438
   colors for newly-created frames.  */
static int initial_screen_colors[2];

439 440 441 442 443 444 445
/* Update the screen from a part of relocated DOS/V screen buffer which
   begins at OFFSET and includes COUNT characters.  */
static void
dosv_refresh_virtual_screen (int offset, int count)
{
  __dpmi_regs regs;

446
  if (offset < 0 || count < 0)	/* paranoia; invalid values crash DOS/V */
447 448
    return;

449 450 451 452 453 454 455
  regs.h.ah = 0xff;	/* update relocated screen */
  regs.x.es = screen_virtual_segment;
  regs.x.di = screen_virtual_offset + offset;
  regs.x.cx = count;
  __dpmi_int (0x10, &regs);
}

456
static void
457
dos_direct_output (int y, int x, char *buf, int len)
Richard M. Stallman's avatar
Richard M. Stallman committed
458
{
459 460
  int t0 = 2 * (x + y * screen_size_X);
  int t = t0 + (int) ScreenPrimary;
461
  int l0 = len;
462 463 464 465

  /* This is faster.  */
  for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
    _farnspokeb (t, *buf);
466 467 468

  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (t0, l0);
Kim F. Storm's avatar
Kim F. Storm committed
469 470 471 472 473
}
#endif

#ifndef HAVE_X_WINDOWS

474 475
static int blink_bit = -1;	/* the state of the blink bit at startup */

476 477 478 479 480 481
/* Enable bright background colors.  */
static void
bright_bg (void)
{
  union REGS regs;

482 483 484 485 486
  /* Remember the original state of the blink/bright-background bit.
     It is stored at 0040:0065h in the BIOS data area.  */
  if (blink_bit == -1)
    blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;

487 488 489 490 491
  regs.h.bl = 0;
  regs.x.ax = 0x1003;
  int86 (0x10, &regs, &regs);
}

492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
/* Disable bright background colors (and enable blinking) if we found
   the video system in that state at startup.  */
static void
maybe_enable_blinking (void)
{
  if (blink_bit == 1)
    {
      union REGS regs;

      regs.h.bl = 1;
      regs.x.ax = 0x1003;
      int86 (0x10, &regs, &regs);
    }
}

507 508 509 510 511 512 513 514 515 516 517 518 519
/* Return non-zero if the system has a VGA adapter.  */
static int
vga_installed (void)
{
  union REGS regs;

  regs.x.ax = 0x1a00;
  int86 (0x10, &regs, &regs);
  if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
    return 1;
  return 0;
}

520 521
/* Set the screen dimensions so that it can show no less than
   ROWS x COLS frame.  */
522

523
void
524
dos_set_window_size (int *rows, int *cols)
525 526 527
{
  char video_name[30];
  union REGS regs;
Eli Zaretskii's avatar
Eli Zaretskii committed
528 529
  Lisp_Object video_mode;
  int video_mode_value, have_vga = 0;
530 531 532 533 534 535
  int current_rows = ScreenRows (), current_cols = ScreenCols ();

  if (*rows == current_rows && *cols == current_cols)
    return;

  mouse_off ();
536
  have_vga = vga_installed ();
537

538
  /* If the user specified a special video mode for these dimensions,
539
     use that mode.  */
Paul Eggert's avatar
Paul Eggert committed
540 541
  video_mode
    = Fsymbol_value (Fintern_soft (make_formatted_string
542 543
				   (video_name, "screen-dimensions-%dx%d",
				    *rows, *cols), Qnil));
544

545
  if (FIXNUMP (video_mode)
Tom Tromey's avatar
Tom Tromey committed
546
      && (video_mode_value = XFIXNUM (video_mode)) > 0)
547 548 549
    {
      regs.x.ax = video_mode_value;
      int86 (0x10, &regs, &regs);
550 551 552 553 554 555 556 557 558

      if (have_mouse)
	{
	  /* Must hardware-reset the mouse, or else it won't update
	     its notion of screen dimensions for some non-standard
	     video modes.  This is *painfully* slow...  */
	  regs.x.ax = 0;
	  int86 (0x33, &regs, &regs);
	}
559 560 561 562 563 564 565
    }

  /* Find one of the dimensions supported by standard EGA/VGA
     which gives us at least the required dimensions.  */
  else
    {
      static struct {
Eli Zaretskii's avatar
Eli Zaretskii committed
566
	int rows, need_vga;
567 568 569 570 571 572 573 574 575 576
      }	std_dimension[] = {
	  {25, 0},
	  {28, 1},
	  {35, 0},
	  {40, 1},
	  {43, 0},
	  {50, 1}
      };
      int i = 0;

577
      while (i < ARRAYELTS (std_dimension))
578 579 580 581 582 583
	{
	 if (std_dimension[i].need_vga <= have_vga
	     && std_dimension[i].rows >= *rows)
	   {
	     if (std_dimension[i].rows != current_rows
		 || *cols != current_cols)
584
	       _set_screen_lines (std_dimension[i].rows);
585 586
	     break;
	   }
587
	 i++;
588 589 590 591 592 593 594 595 596 597 598 599 600
	}
    }


  if (have_mouse)
    {
      mouse_init ();
      mouse_on ();
    }

  /* Tell the caller what dimensions have been REALLY set.  */
  *rows = ScreenRows ();
  *cols = ScreenCols ();
601

602 603 604 605 606
  /* Update Emacs' notion of screen dimensions.  */
  screen_size_X = *cols;
  screen_size_Y = *rows;
  screen_size = *cols * *rows;

607 608 609
  /* If the dimensions changed, the mouse highlight info is invalid.  */
  if (current_rows != *rows || current_cols != *cols)
    {
Juanma Barranquero's avatar
Juanma Barranquero committed
610
      struct frame *f = SELECTED_FRAME ();
611 612
      Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
      Lisp_Object window = hlinfo->mouse_face_window;
613 614

      if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
615
	reset_mouse_highlight (hlinfo);
616 617
    }

618 619
  /* Enable bright background colors.  */
  bright_bg ();
620 621 622 623 624

  /* FIXME: I'm not sure the above will run at all on DOS/V.  But let's
     be defensive anyway.  */
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (0, *cols * *rows);
625 626
}

627 628
/* If we write a character in the position where the mouse is,
   the mouse cursor may need to be refreshed.  */
629 630

static void
631
mouse_off_maybe (void)
632
{
Kim F. Storm's avatar
Kim F. Storm committed
633
  int x, y;
Eli Zaretskii's avatar
Eli Zaretskii committed
634

Kim F. Storm's avatar
Kim F. Storm committed
635 636
  if (!mouse_visible)
    return;
Eli Zaretskii's avatar
Eli Zaretskii committed
637

Kim F. Storm's avatar
Kim F. Storm committed
638 639 640
  mouse_get_xy (&x, &y);
  if (y != new_pos_Y || x < new_pos_X)
    return;
Eli Zaretskii's avatar
Eli Zaretskii committed
641

Kim F. Storm's avatar
Kim F. Storm committed
642 643 644
  mouse_off ();
}

645 646 647 648 649 650 651 652 653 654 655 656 657
#define DEFAULT_CURSOR_START (-1)
#define DEFAULT_CURSOR_WIDTH (-1)
#define BOX_CURSOR_WIDTH     (-32)

/* Set cursor to begin at scan line START_LINE in the character cell
   and extend for WIDTH scan lines.  Scan lines are counted from top
   of the character cell, starting from zero.  */
static void
msdos_set_cursor_shape (struct frame *f, int start_line, int width)
{
  unsigned desired_cursor;
  __dpmi_regs regs;
  int max_line, top_line, bot_line;
658
  struct tty_display_info *tty = FRAME_TTY (f);
659 660 661 662

  /* Avoid the costly BIOS call if F isn't the currently selected
     frame.  Allow for NULL as unconditionally meaning the selected
     frame.  */
Juanma Barranquero's avatar
Juanma Barranquero committed
663
  if (f && f != SELECTED_FRAME ())
664 665
    return;

666 667
  if (tty->termscript)
    fprintf (tty->termscript, "\nCURSOR SHAPE=(%d,%d)", start_line, width);
668

669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
  /* The character cell size in scan lines is stored at 40:85 in the
     BIOS data area.  */
  max_line = _farpeekw (_dos_ds, 0x485) - 1;
  switch (max_line)
    {
      default:	/* this relies on CGA cursor emulation being ON! */
      case 7:
	bot_line = 7;
	break;
      case 9:
	bot_line = 9;
	break;
      case 13:
	bot_line = 12;
	break;
      case 15:
	bot_line = 14;
	break;
    }

  if (width < 0)
    {
      if (width == BOX_CURSOR_WIDTH)
	{
	  top_line = 0;
	  bot_line = max_line;
	}
      else if (start_line != DEFAULT_CURSOR_START)
	{
	  top_line = start_line;
	  bot_line = top_line - width - 1;
	}
      else if (width != DEFAULT_CURSOR_WIDTH)
	{
	  top_line = 0;
	  bot_line = -1 - width;
	}
      else
	top_line = bot_line + 1;
    }
  else if (width == 0)
    {
      /* [31, 0] seems to DTRT for all screen sizes.  */
      top_line = 31;
      bot_line = 0;
    }
  else	/* WIDTH is positive */
    {
      if (start_line != DEFAULT_CURSOR_START)
	bot_line = start_line;
      top_line = bot_line - (width - 1);
    }

  /* If the current cursor shape is already what they want, we are
     history here.  */
  desired_cursor = ((top_line & 0x1f) << 8) | (bot_line & 0x1f);
  if (desired_cursor == _farpeekw (_dos_ds, 0x460))
    return;

  regs.h.ah = 1;
  regs.x.cx = desired_cursor;
  __dpmi_int (0x10, &regs);
}

static void
IT_set_cursor_type (struct frame *f, Lisp_Object cursor_type)
{
Eli Zaretskii's avatar
Eli Zaretskii committed
736
  if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
737 738 739 740
    {
      /* Just BAR means the normal EGA/VGA cursor.  */
      msdos_set_cursor_shape (f, DEFAULT_CURSOR_START, DEFAULT_CURSOR_WIDTH);
    }
Eli Zaretskii's avatar
Eli Zaretskii committed
741 742 743
  else if (CONSP (cursor_type)
	   && (EQ (XCAR (cursor_type), Qbar)
	       || EQ (XCAR (cursor_type), Qhbar)))
744 745 746 747
    {
      Lisp_Object bar_parms = XCDR (cursor_type);
      int width;

748
      if (FIXNUMP (bar_parms))
749 750 751
	{
	  /* Feature: negative WIDTH means cursor at the top
	     of the character cell, zero means invisible cursor.  */
Tom Tromey's avatar
Tom Tromey committed
752
	  width = XFIXNUM (bar_parms);
753 754 755 756
	  msdos_set_cursor_shape (f, width >= 0 ? DEFAULT_CURSOR_START : 0,
				  width);
	}
      else if (CONSP (bar_parms)
757 758
	       && FIXNUMP (XCAR (bar_parms))
	       && FIXNUMP (XCDR (bar_parms)))
759
	{
Tom Tromey's avatar
Tom Tromey committed
760
	  int start_line = XFIXNUM (XCDR (bar_parms));
761

Tom Tromey's avatar
Tom Tromey committed
762
	  width = XFIXNUM (XCAR (bar_parms));
763 764 765 766
	  msdos_set_cursor_shape (f, start_line, width);
	}
    }
  else
767 768 769 770 771 772
    {
      /* Treat anything unknown as "box cursor".  This includes nil, so
	 that a frame which doesn't specify a cursor type gets a box,
	 which is the default in Emacs.  */
      msdos_set_cursor_shape (f, 0, BOX_CURSOR_WIDTH);
    }
773 774
}

775
static void
776
IT_ring_bell (struct frame *f)
Kim F. Storm's avatar
Kim F. Storm committed
777 778
{
  if (visible_bell)
779
    {
Kim F. Storm's avatar
Kim F. Storm committed
780 781
      mouse_off ();
      ScreenVisualBell ();
782
    }
Kim F. Storm's avatar
Kim F. Storm committed
783
  else
784 785 786 787 788 789
    {
      union REGS inregs, outregs;
      inregs.h.ah = 2;
      inregs.h.dl = 7;
      intdos (&inregs, &outregs);
    }
790 791
}

792 793 794 795 796
/* Given a face id FACE, extract the face parameters to be used for
   display until the face changes.  The face parameters (actually, its
   color) are used to construct the video attribute byte for each
   glyph during the construction of the buffer that is then blitted to
   the video RAM.  */
Kim F. Storm's avatar
Kim F. Storm committed
797 798 799
static void
IT_set_face (int face)
{
Juanma Barranquero's avatar
Juanma Barranquero committed
800
  struct frame *sf = SELECTED_FRAME ();
801 802
  struct face *fp  = FACE_FROM_ID_OR_NULL (sf, face);
  struct face *dfp = FACE_FROM_ID_OR_NULL (sf, DEFAULT_FACE_ID);
803
  unsigned long fg, bg, dflt_fg, dflt_bg;
804
  struct tty_display_info *tty = FRAME_TTY (sf);
Kim F. Storm's avatar
Kim F. Storm committed
805

806
  if (!fp)
807
    {
808
      fp = dfp;
809 810 811
      /* The default face for the frame should always be realized and
	 cached.  */
      if (!fp)
812
	emacs_abort ();
813
    }
Kim F. Storm's avatar
Kim F. Storm committed
814
  screen_face = face;
815 816
  fg = fp->foreground;
  bg = fp->background;
817 818
  dflt_fg = dfp->foreground;
  dflt_bg = dfp->background;
819

820 821 822 823
  /* Don't use invalid colors.  In particular, FACE_TTY_DEFAULT_* colors
     mean use the colors of the default face.  Note that we assume all
     16 colors to be available for the background, since Emacs switches
     on this mode (and loses the blinking attribute) at startup.  */
824
  if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
825
    fg = FRAME_FOREGROUND_PIXEL (sf);
826
  else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
827 828 829
    fg = FRAME_BACKGROUND_PIXEL (sf);
  if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
    bg = FRAME_BACKGROUND_PIXEL (sf);
830
  else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
831 832 833
    bg = FRAME_FOREGROUND_PIXEL (sf);

  /* Make sure highlighted lines really stand out, come what may.  */
834
  if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
835 836 837 838 839 840
    {
      unsigned long tem = fg;

      fg = bg;
      bg = tem;
    }
841 842 843 844 845 846 847 848
  /* If the user requested inverse video, obey.  */
  if (inverse_video)
    {
      unsigned long tem2 = fg;

      fg = bg;
      bg = tem2;
    }
849
  if (tty->termscript)
850
    fprintf (tty->termscript, "<FACE %d: %lu/%lu[FG:%lu/BG:%lu]>", face,
851
	     fp->foreground, fp->background, fg, bg);
852 853 854 855 856 857 858 859 860 861
  if (fg >= 0 && fg < 16)
    {
      ScreenAttrib &= 0xf0;
      ScreenAttrib |= fg;
    }
  if (bg >= 0 && bg < 16)
    {
      ScreenAttrib &= 0x0f;
      ScreenAttrib |= ((bg & 0x0f) << 4);
    }
Kim F. Storm's avatar
Kim F. Storm committed
862 863
}

Eli Zaretskii's avatar
Eli Zaretskii committed
864 865
/* According to RBIL (INTERRUP.A, V-1000), 160 is the maximum possible
   width of a DOS display in any known text mode.  We multiply by 2 to
Juanma Barranquero's avatar
Juanma Barranquero committed
866
   accommodate the screen attribute byte.  */
Eli Zaretskii's avatar
Eli Zaretskii committed
867 868
#define MAX_SCREEN_BUF 160*2

869 870
extern unsigned char *encode_terminal_code (struct glyph *, int,
					    struct coding_system *);
871

872
static void
873
IT_write_glyphs (struct frame *f, struct glyph *str, int str_len)
Kim F. Storm's avatar
Kim F. Storm committed
874
{
Eli Zaretskii's avatar
Eli Zaretskii committed
875
  unsigned char screen_buf[MAX_SCREEN_BUF], *screen_bp, *bp;
876
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
877
  register int sl = str_len;
878
  struct tty_display_info *tty = FRAME_TTY (f);
879
  struct frame *sf;
880
  unsigned char *conversion_buffer;
Morten Welinder's avatar
Morten Welinder committed
881

882 883 884 885 886 887 888
  /* If terminal_coding does any conversion, use it, otherwise use
     safe_terminal_coding.  We can't use CODING_REQUIRE_ENCODING here
     because it always returns 1 if terminal_coding.src_multibyte is 1.  */
  struct coding_system *coding = FRAME_TERMINAL_CODING (f);

  if (!(coding->common_flags & CODING_REQUIRE_ENCODING_MASK))
    coding = &safe_terminal_coding;
889

890
  if (str_len <= 0) return;
Eli Zaretskii's avatar
Eli Zaretskii committed
891

Juanma Barranquero's avatar
Juanma Barranquero committed
892
  sf = SELECTED_FRAME ();
893 894 895 896 897 898 899

  /* Since faces get cached and uncached behind our back, we can't
     rely on their indices in the cache being consistent across
     invocations.  So always reset the screen face to the default
     face of the frame, before writing glyphs, and let the glyphs
     set the right face if it's different from the default.  */
  IT_set_face (DEFAULT_FACE_ID);
Eli Zaretskii's avatar
Eli Zaretskii committed
900

901 902
  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
     the tail.  */
903
  coding->mode &= ~CODING_MODE_LAST_BLOCK;
Eli Zaretskii's avatar
Eli Zaretskii committed
904
  screen_bp = &screen_buf[0];
905
  while (sl > 0)
906
    {
907
      int cf;
Eli Zaretskii's avatar
Eli Zaretskii committed
908
      int n;
909

Eli Zaretskii's avatar
Eli Zaretskii committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
      /* If the face of this glyph is different from the current
	 screen face, update the screen attribute byte.  */
      cf = str->face_id;
      if (cf != screen_face)
	IT_set_face (cf);	/* handles invalid faces gracefully */

      /* Identify a run of glyphs with the same face.  */
      for (n = 1; n < sl; ++n)
	if (str[n].face_id != cf)
	  break;

      if (n >= sl)
	/* This is the last glyph.  */
	coding->mode |= CODING_MODE_LAST_BLOCK;

      conversion_buffer = encode_terminal_code (str, n, coding);
      if (coding->produced > 0)
927
	{
Eli Zaretskii's avatar
Eli Zaretskii committed
928 929
	  /* Copy the encoded bytes to the screen buffer.  */
	  for (bp = conversion_buffer; coding->produced--; bp++)
930
	    {
Eli Zaretskii's avatar
Eli Zaretskii committed
931 932 933
	      /* Paranoia: discard bytes that would overrun the end of
		 the screen buffer.  */
	      if (screen_bp - screen_buf <= MAX_SCREEN_BUF - 2)
934
		{
Eli Zaretskii's avatar
Eli Zaretskii committed
935 936
		  *screen_bp++ = (unsigned char)*bp;
		  *screen_bp++ = ScreenAttrib;
937
		}
Eli Zaretskii's avatar
Eli Zaretskii committed
938 939
	      if (tty->termscript)
		fputc (*bp, tty->termscript);
940 941
	    }
	}
Eli Zaretskii's avatar
Eli Zaretskii committed
942 943 944
      /* Update STR and its remaining length.  */
      str += n;
      sl -= n;
945 946
    }

Eli Zaretskii's avatar
Eli Zaretskii committed
947
  /* Dump whatever we have in the screen buffer.  */
Kim F. Storm's avatar
Kim F. Storm committed
948
  mouse_off_maybe ();
949
  dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
950
  if (screen_virtual_segment)
951 952
    dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
  new_pos_X += (screen_bp - screen_buf) / 2;
Kim F. Storm's avatar
Kim F. Storm committed
953
}
954

955 956 957 958 959 960
/************************************************************************
			  Mouse Highlight (and friends..)
 ************************************************************************/

static int mouse_preempted = 0;	/* non-zero when XMenu gobbles mouse events */

961 962
int
popup_activated (void)
963
{
964
  return mouse_preempted;
965 966
}

967
/* Draw TEXT_AREA glyphs between START and END of glyph row ROW on
968 969
   window W.  X is relative to TEXT_AREA in W.  HL is a face override
   for drawing the glyphs.  */
970
void
971 972 973
tty_draw_row_with_mouse_face (struct window *w, struct glyph_row *row,
			      int start_hpos, int end_hpos,
			      enum draw_glyphs_face hl)
974 975
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
976
  struct tty_display_info *tty = FRAME_TTY (f);
977
  Mouse_HLInfo *hlinfo = &tty->mouse_highlight;
978

979
  if (hl == DRAW_MOUSE_FACE)
980
    {
981 982 983 984 985
      int vpos = row->y + WINDOW_TOP_EDGE_Y (w);
      int kstart = start_hpos + WINDOW_LEFT_EDGE_X (w);
      int nglyphs = end_hpos - start_hpos;
      int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
      int start_offset = offset;
986

987 988 989
      if (tty->termscript)
	fprintf (tty->termscript, "\n<MH+ %d-%d:%d>",
		 kstart, kstart + nglyphs - 1, vpos);
Eli Zaretskii's avatar
Eli Zaretskii committed
990

991
      mouse_off ();
992
      IT_set_face (hlinfo->mouse_face_face_id);
993 994 995 996 997 998 999
      /* Since we are going to change only the _colors_ of already
	 displayed text, there's no need to go through all the pain of
	 generating and encoding the text from the glyphs.  Instead,
	 we simply poke the attribute byte of each affected position
	 in video memory with the colors computed by IT_set_face!  */
      _farsetsel (_dos_ds);
      while (nglyphs--)
1000
	{
1001 1002
	  _farnspokeb (offset, ScreenAttrib);
	  offset += 2;
1003
	}
1004 1005 1006
      if (screen_virtual_segment)
	dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
      mouse_on ();
1007
    }
1008
  else if (hl == DRAW_NORMAL_TEXT)
1009
    {
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
      /* We are removing a previously-drawn mouse highlight.  The
	 safest way to do so is to redraw the glyphs anew, since all
	 kinds of faces and display tables could have changed behind
	 our back.  */
      int nglyphs = end_hpos - start_hpos;
      int save_x = new_pos_X, save_y = new_pos_Y;

      if (end_hpos >= row->used[TEXT_AREA])
	nglyphs = row->used[TEXT_AREA] - start_hpos;

      /* IT_write_glyphs writes at cursor position, so we need to
	 temporarily move cursor coordinates to the beginning of
	 the highlight region.  */
      new_pos_X = start_hpos + WINDOW_LEFT_EDGE_X (w);
      new_pos_Y = row->y + WINDOW_TOP_EDGE_Y (w);