msdos.c 93.4 KB
Newer Older
1
/* MS-DOS specific C utilities.          -*- coding: raw-text -*-
2
   Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
3 4 5 6 7

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
20

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

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

Richard M. Stallman's avatar
Richard M. Stallman committed
26
#include <config.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
27 28 29 30 31 32 33 34

#ifdef MSDOS
#include "lisp.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/param.h>
#include <sys/time.h>
#include <dos.h>
35
#include <errno.h>
36
#include <string.h>	 /* for bzero and string functions */
37
#include <sys/stat.h>    /* for _fixpath */
38
#include <unistd.h>	 /* for chdir, dup, dup2, etc. */
39 40
#if __DJGPP__ >= 2
#include <fcntl.h>
41
#include <io.h>		 /* for setmode */
42 43
#include <dpmi.h>	 /* for __dpmi_xxx stuff */
#include <sys/farptr.h>	 /* for _farsetsel, _farnspokeb */
44
#include <libc/dosio.h>  /* for _USE_LFN */
45
#include <conio.h>	 /* for cputs */
46 47
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
48 49 50 51
#include "dosfns.h"
#include "msdos.h"
#include "systime.h"
#include "termhooks.h"
Morten Welinder's avatar
Morten Welinder committed
52 53
#include "dispextern.h"
#include "termopts.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
54
#include "frame.h"
Morten Welinder's avatar
Morten Welinder committed
55
#include "window.h"
56 57
#include "buffer.h"
#include "commands.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
58 59 60 61 62 63 64
#include <go32.h>
#include <pc.h>
#include <ctype.h>
/* #include <process.h> */
/* Damn that local process.h!  Instead we can define P_WAIT ourselves.  */
#define P_WAIT 1

65 66 67 68
#ifndef _USE_LFN
#define _USE_LFN 0
#endif

69 70 71 72
#ifndef _dos_ds
#define _dos_ds _go32_info_block.selector_for_linear_memory
#endif

73 74
#if __DJGPP__ > 1

Richard M. Stallman's avatar
Richard M. Stallman committed
75
#include <signal.h>
76
#include "syssignal.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
#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 */
#endif /* __DJGPP__ > 1 */
97 98 99 100 101 102

static unsigned long
event_timestamp ()
{
  struct time t;
  unsigned long s;
Kim F. Storm's avatar
Kim F. Storm committed
103
  
104 105 106 107 108 109
  gettime (&t);
  s = t.ti_min;
  s *= 60;
  s += t.ti_sec;
  s *= 1000;
  s += t.ti_hund * 10;
Kim F. Storm's avatar
Kim F. Storm committed
110
  
111 112 113
  return s;
}

Kim F. Storm's avatar
Kim F. Storm committed
114 115 116 117 118 119

/* ------------------------ 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
120

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

Kim F. Storm's avatar
Kim F. Storm committed
124 125
static int mouse_last_x;
static int mouse_last_y;
Richard M. Stallman's avatar
Richard M. Stallman committed
126

Kim F. Storm's avatar
Kim F. Storm committed
127 128
static int mouse_button_translate[NUM_MOUSE_BUTTONS];
static int mouse_button_count;
Richard M. Stallman's avatar
Richard M. Stallman committed
129

Kim F. Storm's avatar
Kim F. Storm committed
130 131
void
mouse_on ()
Richard M. Stallman's avatar
Richard M. Stallman committed
132 133 134
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
135
  if (have_mouse > 0 && !mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
136
    {
Kim F. Storm's avatar
Kim F. Storm committed
137 138 139 140 141
      if (termscript)
	fprintf (termscript, "<M_ON>");
      regs.x.ax = 0x0001;
      int86 (0x33, &regs, &regs);
      mouse_visible = 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
142 143 144
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
145 146
void
mouse_off ()
Richard M. Stallman's avatar
Richard M. Stallman committed
147
{
Kim F. Storm's avatar
Kim F. Storm committed
148
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
149

Kim F. Storm's avatar
Kim F. Storm committed
150
  if (have_mouse > 0 && mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
151
    {
Kim F. Storm's avatar
Kim F. Storm committed
152 153 154 155 156
      if (termscript)
	fprintf (termscript, "<M_OFF>");
      regs.x.ax = 0x0002;
      int86 (0x33, &regs, &regs);
      mouse_visible = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
157 158 159
    }
}

160 161 162 163 164 165 166 167 168 169 170
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
171 172 173
void
mouse_moveto (x, y)
     int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
174
{
Kim F. Storm's avatar
Kim F. Storm committed
175
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
176

Kim F. Storm's avatar
Kim F. Storm committed
177 178 179 180 181 182
  if (termscript)
    fprintf (termscript, "<M_XY=%dx%d>", x, y);
  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
183 184
}

Kim F. Storm's avatar
Kim F. Storm committed
185 186 187
static int
mouse_pressed (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
188
{
Kim F. Storm's avatar
Kim F. Storm committed
189
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
190

Kim F. Storm's avatar
Kim F. Storm committed
191 192 193 194 195 196 197 198
  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
199 200
}

Kim F. Storm's avatar
Kim F. Storm committed
201 202 203
static int
mouse_released (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
204 205 206
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
207 208 209 210 211 212 213 214
  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
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
static int
mouse_button_depressed (b, xp, yp)
     int b, *xp, *yp;
{
  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
236 237 238 239 240 241 242 243 244
void
mouse_get_pos (f, insist, bar_window, part, x, y, time)
     FRAME_PTR *f;
     int insist;
     Lisp_Object *bar_window, *x, *y;
     enum scroll_bar_part *part;
     unsigned long *time;
{
  int ix, iy;
245 246 247 248 249
  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
250 251 252 253 254

  *f = selected_frame;
  *bar_window = Qnil;
  mouse_get_xy (&ix, &iy);
  *time = event_timestamp ();
255 256
  *x = make_number (mouse_last_x = ix);
  *y = make_number (mouse_last_y = iy);
Kim F. Storm's avatar
Kim F. Storm committed
257
}
Richard M. Stallman's avatar
Richard M. Stallman committed
258

Kim F. Storm's avatar
Kim F. Storm committed
259 260
static void
mouse_check_moved ()
Richard M. Stallman's avatar
Richard M. Stallman committed
261
{
262
  int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
263

Kim F. Storm's avatar
Kim F. Storm committed
264 265 266 267 268
  mouse_get_xy (&x, &y);
  selected_frame->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
  mouse_last_x = x;
  mouse_last_y = y;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
269

Kim F. Storm's avatar
Kim F. Storm committed
270 271 272 273
void
mouse_init ()
{
  union REGS regs;
274
  int b;
275

Kim F. Storm's avatar
Kim F. Storm committed
276 277
  if (termscript)
    fprintf (termscript, "<M_INIT>");
Richard M. Stallman's avatar
Richard M. Stallman committed
278

Kim F. Storm's avatar
Kim F. Storm committed
279 280
  regs.x.ax = 0x0021;
  int86 (0x33, &regs, &regs);
281

282 283 284 285 286 287 288 289 290 291 292 293
  /* 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.  */
  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
294 295 296 297
  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
298

Kim F. Storm's avatar
Kim F. Storm committed
299 300 301 302
  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
303

Kim F. Storm's avatar
Kim F. Storm committed
304 305 306
  mouse_moveto (0, 0);
  mouse_visible = 0;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
307

Kim F. Storm's avatar
Kim F. Storm committed
308 309 310
/* ------------------------- Screen control ----------------------
 *
 */
311

Kim F. Storm's avatar
Kim F. Storm committed
312
static int internal_terminal = 0;
313

Kim F. Storm's avatar
Kim F. Storm committed
314 315 316 317
#ifndef HAVE_X_WINDOWS
extern unsigned char ScreenAttrib;
static int screen_face;
static int highlight;
318

Kim F. Storm's avatar
Kim F. Storm committed
319 320 321
static int screen_size_X;
static int screen_size_Y;
static int screen_size;
Richard M. Stallman's avatar
Richard M. Stallman committed
322

Kim F. Storm's avatar
Kim F. Storm committed
323 324 325 326
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
327

Kim F. Storm's avatar
Kim F. Storm committed
328 329 330 331 332
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;
333
static unsigned char startup_screen_attrib;
Richard M. Stallman's avatar
Richard M. Stallman committed
334

Kim F. Storm's avatar
Kim F. Storm committed
335
static int term_setup_done;
Richard M. Stallman's avatar
Richard M. Stallman committed
336

Kim F. Storm's avatar
Kim F. Storm committed
337
/* Similar to the_only_frame.  */
338
struct x_output the_only_x_display;
Richard M. Stallman's avatar
Richard M. Stallman committed
339

Kim F. Storm's avatar
Kim F. Storm committed
340 341
/* This is never dereferenced.  */
Display *x_current_display;
Richard M. Stallman's avatar
Richard M. Stallman committed
342

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
/* 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;

#if __DJGPP__ > 1
/* 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;

360 361 362
  if (offset < 0 || count < 0)	/* paranoia; illegal values crash DOS/V */
    return;

363 364 365 366 367 368 369 370
  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);
}
#endif

Kim F. Storm's avatar
Kim F. Storm committed
371 372 373 374 375 376
static
dos_direct_output (y, x, buf, len)
     int y;
     int x;
     char *buf;
     int len;
Richard M. Stallman's avatar
Richard M. Stallman committed
377
{
378 379
  int t0 = 2 * (x + y * screen_size_X);
  int t = t0 + (int) ScreenPrimary;
380
  int l0 = len;
381 382

#if (__DJGPP__ < 2)
Kim F. Storm's avatar
Kim F. Storm committed
383 384 385 386
  while (--len >= 0) {
    dosmemput (buf++, 1, t);
    t += 2;
  }
387 388 389 390
#else
  /* This is faster.  */
  for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
    _farnspokeb (t, *buf);
391 392 393

  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (t0, l0);
394
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
395
}
396
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
397 398 399

/* Flash the screen as a substitute for BEEPs.  */

Kim F. Storm's avatar
Kim F. Storm committed
400
#if (__DJGPP__ < 2)
401
static void
402
do_visible_bell (xorattr)
Richard M. Stallman's avatar
Richard M. Stallman committed
403 404
     unsigned char xorattr;
{
405
  asm volatile
406
    ("  movb   $1,%%dl
Richard M. Stallman's avatar
Richard M. Stallman committed
407
visible_bell_0:
408
	movl   _ScreenPrimary,%%eax
409
	call   dosmemsetup
410 411 412 413
	movl   %%eax,%%ebx
	movl   %1,%%ecx
	movb   %0,%%al
	incl   %%ebx
Richard M. Stallman's avatar
Richard M. Stallman committed
414
visible_bell_1:
415 416 417
	xorb   %%al,%%gs:(%%ebx)
	addl   $2,%%ebx
	decl   %%ecx
418
	jne    visible_bell_1
419
	decb   %%dl
420
	jne    visible_bell_3
Richard M. Stallman's avatar
Richard M. Stallman committed
421
visible_bell_2:
422 423 424 425 426
	movzwl %%ax,%%eax
        movzwl %%ax,%%eax
	movzwl %%ax,%%eax
	movzwl %%ax,%%eax
	decw   %%cx
427 428
	jne    visible_bell_2
	jmp    visible_bell_0
429 430
visible_bell_3:"
     : /* no output */
Kim F. Storm's avatar
Kim F. Storm committed
431
     : "m" (xorattr), "g" (screen_size)
432
     : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
Richard M. Stallman's avatar
Richard M. Stallman committed
433 434
}

Kim F. Storm's avatar
Kim F. Storm committed
435 436 437 438 439 440 441 442 443 444 445 446 447
static void
ScreenVisualBell (void)
{
  /* This creates an xor-mask that will swap the default fore- and
     background colors.  */
  do_visible_bell (((the_only_x_display.foreground_pixel
		     ^ the_only_x_display.background_pixel)
		    * 0x11) & 0x7f);
}
#endif

#ifndef HAVE_X_WINDOWS

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

450 451 452 453 454 455
/* Enable bright background colors.  */
static void
bright_bg (void)
{
  union REGS regs;

456 457 458 459 460
  /* 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;

461 462 463 464 465
  regs.h.bl = 0;
  regs.x.ax = 0x1003;
  int86 (0x10, &regs, &regs);
}

466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
/* 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);
    }
}

481 482
/* Set the screen dimensions so that it can show no less than
   ROWS x COLS frame.  */
483

484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
void
dos_set_window_size (rows, cols)
     int *rows, *cols;
{
  char video_name[30];
  Lisp_Object video_mode;
  int video_mode_value;
  int have_vga = 0;
  union REGS regs;
  int current_rows = ScreenRows (), current_cols = ScreenCols ();

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

  /* Do we have a VGA?  */
  regs.x.ax = 0x1a00;
  int86 (0x10, &regs, &regs);
  if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
    have_vga = 1;

  mouse_off ();

506
  /* If the user specified a special video mode for these dimensions,
507 508 509 510 511 512 513 514 515 516
     use that mode.  */
  sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
  video_mode = XSYMBOL (Fintern_soft (build_string (video_name),
				      Qnil))-> value;

  if (INTEGERP (video_mode)
      && (video_mode_value = XINT (video_mode)) > 0)
    {
      regs.x.ax = video_mode_value;
      int86 (0x10, &regs, &regs);
517 518 519 520 521 522 523 524 525

      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);
	}
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
    }

  /* Find one of the dimensions supported by standard EGA/VGA
     which gives us at least the required dimensions.  */

#if __DJGPP__ > 1

  else
    {
      static struct {
	int rows;
	int need_vga;
      }	std_dimension[] = {
	  {25, 0},
	  {28, 1},
	  {35, 0},
	  {40, 1},
	  {43, 0},
	  {50, 1}
      };
      int i = 0;

      while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
	{
	 if (std_dimension[i].need_vga <= have_vga
	     && std_dimension[i].rows >= *rows)
	   {
	     if (std_dimension[i].rows != current_rows
		 || *cols != current_cols)
555
	       _set_screen_lines (std_dimension[i].rows);
556 557
	     break;
	   }
558
	 i++;
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
	}
    }

#else /* not __DJGPP__ > 1 */

  else if (*rows <= 25)
    {
      if (current_rows != 25 || current_cols != 80)
	{
	  regs.x.ax = 3;
	  int86 (0x10, &regs, &regs);
	  regs.x.ax = 0x1101;
	  regs.h.bl = 0;
	  int86 (0x10, &regs, &regs);
	  regs.x.ax = 0x1200;
	  regs.h.bl = 32;
	  int86 (0x10, &regs, &regs);
	  regs.x.ax = 3;
	  int86 (0x10, &regs, &regs);
	}
    }
  else if (*rows <= 50)
    if (have_vga && (current_rows != 50 || current_cols != 80)
	|| *rows <= 43 && (current_rows != 43 || current_cols != 80))
      {
	regs.x.ax = 3;
	int86 (0x10, &regs, &regs);
	regs.x.ax = 0x1112;
	regs.h.bl = 0;
	int86 (0x10, &regs, &regs);
	regs.x.ax = 0x1200;
	regs.h.bl = 32;
	int86 (0x10, &regs, &regs);
	regs.x.ax = 0x0100;
	regs.x.cx = 7;
	int86 (0x10, &regs, &regs);
      }
#endif /* not __DJGPP__ > 1 */

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

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

  /* Enable bright background colors.  */
  bright_bg ();
610 611 612 613 614

  /* 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);
615 616
}

617 618
/* If we write a character in the position where the mouse is,
   the mouse cursor may need to be refreshed.  */
619 620

static void
Kim F. Storm's avatar
Kim F. Storm committed
621
mouse_off_maybe ()
622
{
Kim F. Storm's avatar
Kim F. Storm committed
623 624 625 626 627 628 629 630 631 632 633 634
  int x, y;
  
  if (!mouse_visible)
    return;
  
  mouse_get_xy (&x, &y);
  if (y != new_pos_Y || x < new_pos_X)
    return;
  
  mouse_off ();
}

635 636
static void
IT_ring_bell (void)
Kim F. Storm's avatar
Kim F. Storm committed
637 638
{
  if (visible_bell)
639
    {
Kim F. Storm's avatar
Kim F. Storm committed
640 641
      mouse_off ();
      ScreenVisualBell ();
642
    }
Kim F. Storm's avatar
Kim F. Storm committed
643
  else
644 645 646 647 648 649
    {
      union REGS inregs, outregs;
      inregs.h.ah = 2;
      inregs.h.dl = 7;
      intdos (&inregs, &outregs);
    }
650 651
}

Kim F. Storm's avatar
Kim F. Storm committed
652 653 654 655 656 657 658 659 660 661 662 663 664
static void
IT_set_face (int face)
{
  struct face *fp;
  extern struct face *intern_face (/* FRAME_PTR, struct face * */);

  if (face == 1 || (face == 0 && highlight))
    fp = FRAME_MODE_LINE_FACE (foo);
  else if (face <= 0 || face >= FRAME_N_COMPUTED_FACES (foo))
    fp = FRAME_DEFAULT_FACE (foo);
  else
    fp = intern_face (selected_frame, FRAME_COMPUTED_FACES (foo)[face]);
  if (termscript)
665 666
    fprintf (termscript, "<FACE %d: %d/%d>",
	     face, FACE_FOREGROUND (fp), FACE_BACKGROUND (fp));
Kim F. Storm's avatar
Kim F. Storm committed
667 668 669 670
  screen_face = face;
  ScreenAttrib = (FACE_BACKGROUND (fp) << 4) | FACE_FOREGROUND (fp);
}

671
static void
Kim F. Storm's avatar
Kim F. Storm committed
672 673 674 675 676
IT_write_glyphs (GLYPH *str, int len)
{
  int newface;
  int ch, l = len;
  unsigned char *buf, *bp;
677
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
Morten Welinder's avatar
Morten Welinder committed
678

Kim F. Storm's avatar
Kim F. Storm committed
679
  if (len == 0) return;
680
  
Kim F. Storm's avatar
Kim F. Storm committed
681
  buf = bp = alloca (len * 2);
682
  
Kim F. Storm's avatar
Kim F. Storm committed
683
  while (--l >= 0)
684
    {
Kim F. Storm's avatar
Kim F. Storm committed
685 686 687 688 689 690 691 692 693 694
      newface = FAST_GLYPH_FACE (*str);
      if (newface != screen_face)
	IT_set_face (newface);
      ch = FAST_GLYPH_CHAR (*str);
      *bp++ = (unsigned char)ch;
      *bp++ = ScreenAttrib;
      
      if (termscript)
	fputc (ch, termscript);
      str++;
695 696
    }

Kim F. Storm's avatar
Kim F. Storm committed
697
  mouse_off_maybe ();
698 699 700
  dosmemput (buf, 2 * len, (int)ScreenPrimary + offset);
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (offset, len);
Kim F. Storm's avatar
Kim F. Storm committed
701 702
  new_pos_X += len;
}
703

704 705
static void
IT_clear_end_of_line (int first_unused)
Kim F. Storm's avatar
Kim F. Storm committed
706 707 708
{
  char *spaces, *sp;
  int i, j;
709
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
Kim F. Storm's avatar
Kim F. Storm committed
710 711 712 713 714 715

  IT_set_face (0);
  if (termscript)
    fprintf (termscript, "<CLR:EOL>");
  i = (j = screen_size_X - new_pos_X) * 2;
  spaces = sp = alloca (i);
716
  
Kim F. Storm's avatar
Kim F. Storm committed
717
  while (--j >= 0)
718
    {
Kim F. Storm's avatar
Kim F. Storm committed
719 720
      *sp++ = ' ';
      *sp++ = ScreenAttrib;
721 722
    }

Kim F. Storm's avatar
Kim F. Storm committed
723
  mouse_off_maybe ();
724 725 726
  dosmemput (spaces, i, (int)ScreenPrimary + offset);
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (offset, i / 2);
727 728
}

729
static void
Kim F. Storm's avatar
Kim F. Storm committed
730 731 732 733 734 735 736
IT_clear_screen (void)
{
  if (termscript)
    fprintf (termscript, "<CLR:SCR>");
  IT_set_face (0);
  mouse_off ();
  ScreenClear ();
737 738
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (0, screen_size);
Kim F. Storm's avatar
Kim F. Storm committed
739 740 741
  new_pos_X = new_pos_Y = 0;
}

742
static void
Kim F. Storm's avatar
Kim F. Storm committed
743 744 745 746 747 748 749 750 751 752 753 754
IT_clear_to_end (void)
{
  if (termscript)
    fprintf (termscript, "<CLR:EOS>");

  while (new_pos_Y < screen_size_Y) {
    new_pos_X = 0;
    IT_clear_end_of_line (0);
    new_pos_Y++;
  }
}

755
static void
Kim F. Storm's avatar
Kim F. Storm committed
756 757 758 759 760 761 762 763
IT_cursor_to (int y, int x)
{
  if (termscript)
    fprintf (termscript, "\n<XY=%dx%d>", x, y);
  new_pos_X = x;
  new_pos_Y = y;
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
static int cursor_cleared;

static
IT_display_cursor (int on)
{
  if (on && cursor_cleared)
    {
      ScreenSetCursor (current_pos_Y, current_pos_X);
      cursor_cleared = 0;
    }
  else if (!on && !cursor_cleared)
    {
      ScreenSetCursor (-1, -1);
      cursor_cleared = 1;
    }
}

/* Emacs calls cursor-movement functions a lot when it updates the
   display (probably a legacy of old terminals where you cannot
   update a screen line without first moving the cursor there).
   However, cursor movement is expensive on MSDOS (it calls a slow
   BIOS function and requires 2 mode switches), while actual screen
   updates access the video memory directly and don't depend on
   cursor position.  To avoid slowing down the redisplay, we cheat:
   all functions that move the cursor only set internal variables
   which record the cursor position, whereas the cursor is only
   moved to its final position whenever screen update is complete.

   `IT_cmgoto' is called from the keyboard reading loop and when the
   frame update is complete.  This means that we are ready for user
   input, so we update the cursor position to show where the point is,
   and also make the mouse pointer visible.

   Special treatment is required when the cursor is in the echo area,
   to put the cursor at the end of the text displayed there.  */

800 801
static void
IT_cmgoto (FRAME_PTR f)
802 803 804 805
{
  /* Only set the cursor to where it should be if the display is
     already in sync with the window contents.  */
  int update_cursor_pos = MODIFF == unchanged_modified;
806 807 808 809 810 811 812 813 814
  static int previous_pos_X = -1;

  /* If the display is in sync, forget any previous knowledge about
     cursor position.  This is primarily for unexpected events like
     C-g in the minibuffer.  */
  if (update_cursor_pos && previous_pos_X >= 0)
    previous_pos_X = -1;
  /* If we are in the echo area, put the cursor at the
     end of the echo area message.  */
815 816 817
  if (!update_cursor_pos
      && XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top) <= new_pos_Y)
    {
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
      int tem_X = current_pos_X, dummy;

      if (echo_area_glyphs)
	{
	  tem_X = echo_area_glyphs_length;
	  /* Save current cursor position, to be restored after the
	     echo area message is erased.  Only remember one level
	     of previous cursor position.  */
	  if (previous_pos_X == -1)
	    ScreenGetCursor (&dummy, &previous_pos_X);
	}
      else if (previous_pos_X >= 0)
	{
	  /* We wind up here after the echo area message is erased.
	     Restore the cursor position we remembered above.  */
	  tem_X = previous_pos_X;
	  previous_pos_X = -1;
	}
836

837
      if (current_pos_X != tem_X)
838 839 840 841
	{
	  new_pos_X = tem_X;
	  update_cursor_pos = 1;
	}
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    }

  if (update_cursor_pos
      && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
    {
      ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
      if (termscript)
	fprintf (termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
    }

  /* Maybe cursor is invisible, so make it visible.  */
  IT_display_cursor (1);

  /* Mouse pointer should be always visible if we are waiting for
     keyboard input.  */
  if (!mouse_visible)
    mouse_on ();
}

861 862
static void
IT_reassert_line_highlight (int new, int vpos)
Kim F. Storm's avatar
Kim F. Storm committed
863 864 865 866 867
{
  highlight = new;
  IT_set_face (0); /* To possibly clear the highlighting.  */
}

868 869
static void
IT_change_line_highlight (int new_highlight, int vpos, int first_unused_hpos)
Richard M. Stallman's avatar
Richard M. Stallman committed
870
{
Kim F. Storm's avatar
Kim F. Storm committed
871 872 873 874 875 876
  highlight = new_highlight;
  IT_set_face (0); /* To possibly clear the highlighting.  */
  IT_cursor_to (vpos, 0);
  IT_clear_end_of_line (first_unused_hpos);
}

877 878
static void
IT_update_begin (struct frame *foo)
Kim F. Storm's avatar
Kim F. Storm committed
879 880 881 882 883 884
{
  highlight = 0;
  IT_set_face (0); /* To possibly clear the highlighting.  */
  screen_face = -1;
}

885 886
static void
IT_update_end (struct frame *foo)
Kim F. Storm's avatar
Kim F. Storm committed
887 888
{
}
Richard M. Stallman's avatar
Richard M. Stallman committed
889

890
/* set-window-configuration on window.c needs this.  */
891 892 893 894 895
void
x_set_menu_bar_lines (f, value, oldval)
     struct frame *f;
     Lisp_Object value, oldval;
{
896 897
  set_menu_bar_lines (f, value, oldval);
}
898

899
/* This was copied from xfns.c  */
900

901 902 903
Lisp_Object Qbackground_color;
Lisp_Object Qforeground_color;
extern Lisp_Object Qtitle;
904

905 906
/* IT_set_terminal_modes is called when emacs is started,
   resumed, and whenever the screen is redrawn!  */
Kim F. Storm's avatar
Kim F. Storm committed
907

908
static void
Kim F. Storm's avatar
Kim F. Storm committed
909 910
IT_set_terminal_modes (void)
{
911
  if (termscript)
Kim F. Storm's avatar
Kim F. Storm committed
912 913 914 915 916 917
    fprintf (termscript, "\n<SET_TERM>");
  highlight = 0;

  screen_size_X = ScreenCols ();
  screen_size_Y = ScreenRows ();
  screen_size = screen_size_X * screen_size_Y;
918
  
Kim F. Storm's avatar
Kim F. Storm committed
919 920 921 922 923 924
  new_pos_X = new_pos_Y = 0;
  current_pos_X = current_pos_Y = -1;

  if (term_setup_done)
    return;
  term_setup_done = 1;
925
  
Kim F. Storm's avatar
Kim F. Storm committed
926 927
  startup_screen_size_X = screen_size_X;
  startup_screen_size_Y = screen_size_Y;
928
  startup_screen_attrib = ScreenAttrib;
Kim F. Storm's avatar
Kim F. Storm committed
929

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
#if __DJGPP__ > 1
  /* Is DOS/V (or any other RSIS software which relocates
     the screen) installed?  */
  {
    unsigned short es_value;
    __dpmi_regs regs;

    regs.h.ah = 0xfe;	/* get relocated screen address */
    if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
      regs.x.es = (ScreenPrimary >> 4) & 0xffff;
    else if (screen_old_address) /* already switched to Japanese mode once */
      regs.x.es = (screen_old_address >> 4) & 0xffff;
    else
      regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
    regs.x.di = 0;
    es_value = regs.x.es;
    __dpmi_int (0x10, &regs);

    if (regs.x.es != es_value && regs.x.es != (ScreenPrimary >> 4) & 0xffff)
      {
	screen_old_address = ScreenPrimary;
	screen_virtual_segment = regs.x.es;
	screen_virtual_offset  = regs.x.di;
	ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
      }
  }
#endif /* __DJGPP__ > 1 */

Kim F. Storm's avatar
Kim F. Storm committed
958 959 960 961
  ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
  ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));

  if (termscript)
962
    fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
963
	     screen_size_X, screen_size_Y);
964 965

  bright_bg ();
Kim F. Storm's avatar
Kim F. Storm committed
966 967
}

968 969
/* IT_reset_terminal_modes is called when emacs is
   suspended or killed.  */
Kim F. Storm's avatar
Kim F. Storm committed
970

971
static void
Kim F. Storm's avatar
Kim F. Storm committed
972 973
IT_reset_terminal_modes (void)
{
974 975 976 977 978 979 980 981 982
  int display_row_start = (int) ScreenPrimary;
  int saved_row_len     = startup_screen_size_X * 2;
  int update_row_len    = ScreenCols () * 2;
  int current_rows      = ScreenRows ();
  int to_next_row       = update_row_len;
  unsigned char *saved_row = startup_screen_buffer;
  int cursor_pos_X = ScreenCols () - 1;
  int cursor_pos_Y = ScreenRows () - 1;

Kim F. Storm's avatar
Kim F. Storm committed
983
  if (termscript)
Richard M. Stallman's avatar
Richard M. Stallman committed
984
    fprintf (termscript, "\n<RESET_TERM>");
Kim F. Storm's avatar
Kim F. Storm committed
985 986 987 988 989 990

  highlight = 0;

  if (!term_setup_done)
    return;
  
991
  mouse_off ();
992 993 994 995

  /* Leave the video system in the same state as we found it,
     as far as the blink/bright-background bit is concerned.  */
  maybe_enable_blinking ();
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
 
  /* We have a situation here.
     We cannot just do ScreenUpdate(startup_screen_buffer) because
     the luser could have changed screen dimensions inside Emacs
     and failed (or didn't want) to restore them before killing
     Emacs.  ScreenUpdate() uses the *current* screen dimensions and
     thus will happily use memory outside what was allocated for
     `startup_screen_buffer'.
     Thus we only restore as much as the current screen dimensions
     can hold, and clear the rest (if the saved screen is smaller than
     the current) with the color attribute saved at startup.  The cursor
     is also restored within the visible dimensions.  */

  ScreenAttrib = startup_screen_attrib;
  ScreenClear ();
1011 1012
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (0, screen_size);
1013 1014 1015 1016 1017

  if (update_row_len > saved_row_len)
    update_row_len = saved_row_len;
  if (current_rows > startup_screen_size_Y)
    current_rows = startup_screen_size_Y;
Kim F. Storm's avatar
Kim F. Storm committed
1018 1019

  if (termscript)
1020 1021 1022 1023 1024 1025
    fprintf (termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
             update_row_len / 2, current_rows);

  while (current_rows--)
    {
      dosmemput (saved_row, update_row_len, display_row_start);
1026 1027 1028
      if (screen_virtual_segment)
	dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
				     update_row_len / 2);
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
      saved_row         += saved_row_len;
      display_row_start += to_next_row;
    }
  if (startup_pos_X < cursor_pos_X)
    cursor_pos_X = startup_pos_X;
  if (startup_pos_Y < cursor_pos_Y)
    cursor_pos_Y = startup_pos_Y;

  ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
  xfree (startup_screen_buffer);
Kim F. Storm's avatar
Kim F. Storm committed
1039 1040 1041 1042

  term_setup_done = 0;
}

1043 1044
static void
IT_set_terminal_window (int foo)
Kim F. Storm's avatar
Kim F. Storm committed
1045 1046 1047 1048
{
}

void
1049 1050
IT_set_frame_parameters (f, alist)
     FRAME_PTR f;
Kim F. Storm's avatar
Kim F. Storm committed
1051 1052 1053
     Lisp_Object alist;
{
  Lisp_Object tail;
1054 1055 1056 1057 1058 1059
  int length = XINT (Flength (alist));
  int i;
  Lisp_Object *parms
    = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
  Lisp_Object *values
    = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
Kim F. Storm's avatar
Kim F. Storm committed
1060 1061 1062 1063
  int redraw;
  extern unsigned long load_color ();

  redraw = 0;
1064 1065 1066

  /* Extract parm names and values into those vectors.  */
  i = 0;
Kim F. Storm's avatar
Kim F. Storm committed
1067 1068
  for (tail = alist; CONSP (tail); tail = Fcdr (tail))
    {
1069
      Lisp_Object elt;
Kim F. Storm's avatar
Kim F. Storm committed
1070 1071

      elt = Fcar (tail);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
      parms[i] = Fcar (elt);
      CHECK_SYMBOL (parms[i], 1);
      values[i] = Fcdr (elt);
      i++;
    }


  /* Now process them in reverse of specified order.  */
  for (i--; i >= 0; i--)
    {
      Lisp_Object prop = parms[i];
      Lisp_Object val  = values[i];
Kim F. Storm's avatar
Kim F. Storm committed
1084

1085
      if (EQ (prop, Qforeground_color))
Kim F. Storm's avatar
Kim F. Storm committed
1086 1087 1088 1089 1090 1091
	{
	  unsigned long new_color = load_color (f, val);
	  if (new_color != ~0)
	    {
	      FRAME_FOREGROUND_PIXEL (f) = new_color;
	      redraw = 1;
1092
	      if (termscript)
1093
		fprintf (termscript, "<FGCOLOR %lu>\n", new_color);
Kim F. Storm's avatar
Kim F. Storm committed
1094 1095
	    }
	}
1096
      else if (EQ (prop, Qbackground_color))
Kim F. Storm's avatar
Kim F. Storm committed
1097 1098 1099 1100
	{
	  unsigned long new_color = load_color (f, val);
	  if (new_color != ~0)
	    {
1101
	      FRAME_BACKGROUND_PIXEL (f) = new_color;
Kim F. Storm's avatar
Kim F. Storm committed
1102
	      redraw = 1;
1103
	      if (termscript)
1104
		fprintf (termscript, "<BGCOLOR %lu>\n", new_color);
Kim F. Storm's avatar
Kim F. Storm committed
1105 1106
	    }
	}
1107 1108 1109 1110 1111 1112 1113 1114 1115
      else if (EQ (prop, Qtitle))
	{
	  x_set_title (f, val);
	  if (termscript)
	    fprintf (termscript, "<TITLE: %s>\n", XSTRING (val)->data);
	}
      else if (EQ (prop, intern ("reverse")) && EQ (val, Qt))
	{
	  unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
1116

1117 1118 1119 1120 1121
	  FRAME_FOREGROUND_PIXEL (f) = FRAME_BACKGROUND_PIXEL (f);
	  FRAME_BACKGROUND_PIXEL (f) = fg;
	  if (termscript)
	    fprintf (termscript, "<INVERSE-VIDEO>\n");
	}
1122 1123
      store_frame_param (f, prop, val);

Kim F. Storm's avatar
Kim F. Storm committed
1124 1125 1126 1127
    }

  if (redraw)
    {
1128 1129 1130
      extern void recompute_basic_faces (FRAME_PTR);
      extern void redraw_frame (FRAME_PTR);

Kim F. Storm's avatar
Kim F. Storm committed
1131
      recompute_basic_faces (f);
1132 1133
      if (f == selected_frame)
	redraw_frame (f);
Kim F. Storm's avatar
Kim F. Storm committed
1134 1135 1136
    }
}

1137 1138