msdos.c 108 KB
Newer Older
1
/* MS-DOS specific C utilities.          -*- coding: raw-text -*-
2
   Copyright (C) 1993, 94, 95, 96, 97, 1999 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

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

Richard M. Stallman's avatar
Richard M. Stallman committed
49 50 51
#include "msdos.h"
#include "systime.h"
#include "termhooks.h"
52
#include "termchar.h"
Morten Welinder's avatar
Morten Welinder committed
53
#include "dispextern.h"
54
#include "dosfns.h"
Morten Welinder's avatar
Morten Welinder committed
55
#include "termopts.h"
56 57 58
#include "charset.h"
#include "coding.h"
#include "disptab.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
59
#include "frame.h"
Morten Welinder's avatar
Morten Welinder committed
60
#include "window.h"
61 62
#include "buffer.h"
#include "commands.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
63 64 65 66 67 68 69
#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

70 71 72 73
#ifndef _USE_LFN
#define _USE_LFN 0
#endif

74 75 76 77
#ifndef _dos_ds
#define _dos_ds _go32_info_block.selector_for_linear_memory
#endif

78 79
#if __DJGPP__ > 1

Richard M. Stallman's avatar
Richard M. Stallman committed
80
#include <signal.h>
81
#include "syssignal.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
#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 */
102 103 104 105 106 107

static unsigned long
event_timestamp ()
{
  struct time t;
  unsigned long s;
Kim F. Storm's avatar
Kim F. Storm committed
108
  
109 110 111 112 113 114
  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
115
  
116 117 118
  return s;
}

Kim F. Storm's avatar
Kim F. Storm committed
119 120 121 122 123 124

/* ------------------------ 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
125

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

Kim F. Storm's avatar
Kim F. Storm committed
129 130
static int mouse_last_x;
static int mouse_last_y;
Richard M. Stallman's avatar
Richard M. Stallman committed
131

Kim F. Storm's avatar
Kim F. Storm committed
132 133
static int mouse_button_translate[NUM_MOUSE_BUTTONS];
static int mouse_button_count;
Richard M. Stallman's avatar
Richard M. Stallman committed
134

Kim F. Storm's avatar
Kim F. Storm committed
135 136
void
mouse_on ()
Richard M. Stallman's avatar
Richard M. Stallman committed
137 138 139
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
140
  if (have_mouse > 0 && !mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
141
    {
Kim F. Storm's avatar
Kim F. Storm committed
142 143 144 145 146
      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
147 148 149
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
150 151
void
mouse_off ()
Richard M. Stallman's avatar
Richard M. Stallman committed
152
{
Kim F. Storm's avatar
Kim F. Storm committed
153
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
154

Kim F. Storm's avatar
Kim F. Storm committed
155
  if (have_mouse > 0 && mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
156
    {
Kim F. Storm's avatar
Kim F. Storm committed
157 158 159 160 161
      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
162 163 164
    }
}

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

Kim F. Storm's avatar
Kim F. Storm committed
182 183 184 185 186 187
  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
188 189
}

Kim F. Storm's avatar
Kim F. Storm committed
190 191 192
static int
mouse_pressed (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
193
{
Kim F. Storm's avatar
Kim F. Storm committed
194
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
195

Kim F. Storm's avatar
Kim F. Storm committed
196 197 198 199 200 201 202 203
  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
204 205
}

Kim F. Storm's avatar
Kim F. Storm committed
206 207 208
static int
mouse_released (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
209 210 211
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
212 213 214 215 216 217 218 219
  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
220 221
}

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
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
241 242 243 244 245 246 247 248 249
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;
250 251 252 253 254
  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
255

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

Kim F. Storm's avatar
Kim F. Storm committed
264 265
static void
mouse_check_moved ()
Richard M. Stallman's avatar
Richard M. Stallman committed
266
{
267
  int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
268

Kim F. Storm's avatar
Kim F. Storm committed
269
  mouse_get_xy (&x, &y);
270
  SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
Kim F. Storm's avatar
Kim F. Storm committed
271 272 273
  mouse_last_x = x;
  mouse_last_y = y;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
274

Kim F. Storm's avatar
Kim F. Storm committed
275 276 277 278
void
mouse_init ()
{
  union REGS regs;
279
  int b;
280

Kim F. Storm's avatar
Kim F. Storm committed
281 282
  if (termscript)
    fprintf (termscript, "<M_INIT>");
Richard M. Stallman's avatar
Richard M. Stallman committed
283

Kim F. Storm's avatar
Kim F. Storm committed
284 285
  regs.x.ax = 0x0021;
  int86 (0x33, &regs, &regs);
286

287 288 289 290 291 292 293 294 295 296 297 298
  /* 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
299 300 301 302
  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
303

Kim F. Storm's avatar
Kim F. Storm committed
304 305 306 307
  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
308

Kim F. Storm's avatar
Kim F. Storm committed
309 310 311
  mouse_moveto (0, 0);
  mouse_visible = 0;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
312

Kim F. Storm's avatar
Kim F. Storm committed
313 314 315
/* ------------------------- Screen control ----------------------
 *
 */
316

Kim F. Storm's avatar
Kim F. Storm committed
317
static int internal_terminal = 0;
318

Kim F. Storm's avatar
Kim F. Storm committed
319 320 321 322
#ifndef HAVE_X_WINDOWS
extern unsigned char ScreenAttrib;
static int screen_face;
static int highlight;
323

Kim F. Storm's avatar
Kim F. Storm committed
324 325 326
static int screen_size_X;
static int screen_size_Y;
static int screen_size;
Richard M. Stallman's avatar
Richard M. Stallman committed
327

Kim F. Storm's avatar
Kim F. Storm committed
328 329 330 331
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
332

Kim F. Storm's avatar
Kim F. Storm committed
333 334 335 336 337
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;
338
static unsigned char startup_screen_attrib;
Richard M. Stallman's avatar
Richard M. Stallman committed
339

340 341
static clock_t startup_time;

Kim F. Storm's avatar
Kim F. Storm committed
342
static int term_setup_done;
Richard M. Stallman's avatar
Richard M. Stallman committed
343

Kim F. Storm's avatar
Kim F. Storm committed
344
/* Similar to the_only_frame.  */
345
struct x_output the_only_x_display;
Richard M. Stallman's avatar
Richard M. Stallman committed
346

347 348 349 350 351 352 353 354
/* 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;
355 356
/* A flag to control how to display unibyte 8-bit characters.  */
extern int unibyte_display_via_language_environment;
357 358 359 360 361 362 363 364 365

#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;

366 367 368
  if (offset < 0 || count < 0)	/* paranoia; illegal values crash DOS/V */
    return;

369 370 371 372 373 374 375 376
  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

377
static void
Kim F. Storm's avatar
Kim F. Storm committed
378 379 380 381 382
dos_direct_output (y, x, buf, len)
     int y;
     int x;
     char *buf;
     int len;
Richard M. Stallman's avatar
Richard M. Stallman committed
383
{
384 385
  int t0 = 2 * (x + y * screen_size_X);
  int t = t0 + (int) ScreenPrimary;
386
  int l0 = len;
387 388

#if (__DJGPP__ < 2)
Kim F. Storm's avatar
Kim F. Storm committed
389 390 391 392
  while (--len >= 0) {
    dosmemput (buf++, 1, t);
    t += 2;
  }
393 394 395 396
#else
  /* This is faster.  */
  for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
    _farnspokeb (t, *buf);
397 398 399

  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (t0, l0);
400
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
401
}
402
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
403 404 405

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

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

Kim F. Storm's avatar
Kim F. Storm committed
441 442 443 444 445 446 447 448 449 450 451 452 453
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

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

456 457 458 459 460 461
/* Enable bright background colors.  */
static void
bright_bg (void)
{
  union REGS regs;

462 463 464 465 466
  /* 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;

467 468 469 470 471
  regs.h.bl = 0;
  regs.x.ax = 0x1003;
  int86 (0x10, &regs, &regs);
}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
/* 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);
    }
}

487 488
/* Set the screen dimensions so that it can show no less than
   ROWS x COLS frame.  */
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
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 ();

512
  /* If the user specified a special video mode for these dimensions,
513 514 515 516 517 518 519 520 521 522
     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);
523 524 525 526 527 528 529 530 531

      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);
	}
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    }

  /* 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)
561
	       _set_screen_lines (std_dimension[i].rows);
562 563
	     break;
	   }
564
	 i++;
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 607 608 609 610 611 612
	}
    }

#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 ();
613 614 615

  /* Enable bright background colors.  */
  bright_bg ();
616 617 618 619 620

  /* 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);
621 622
}

623 624
/* If we write a character in the position where the mouse is,
   the mouse cursor may need to be refreshed.  */
625 626

static void
Kim F. Storm's avatar
Kim F. Storm committed
627
mouse_off_maybe ()
628
{
Kim F. Storm's avatar
Kim F. Storm committed
629 630 631 632 633 634 635 636 637 638 639 640
  int x, y;
  
  if (!mouse_visible)
    return;
  
  mouse_get_xy (&x, &y);
  if (y != new_pos_Y || x < new_pos_X)
    return;
  
  mouse_off ();
}

641 642
static void
IT_ring_bell (void)
Kim F. Storm's avatar
Kim F. Storm committed
643 644
{
  if (visible_bell)
645
    {
Kim F. Storm's avatar
Kim F. Storm committed
646 647
      mouse_off ();
      ScreenVisualBell ();
648
    }
Kim F. Storm's avatar
Kim F. Storm committed
649
  else
650 651 652 653 654 655
    {
      union REGS inregs, outregs;
      inregs.h.ah = 2;
      inregs.h.dl = 7;
      intdos (&inregs, &outregs);
    }
656 657
}

658 659 660 661 662
/* 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
663 664 665
static void
IT_set_face (int face)
{
666 667
  struct frame *sf = SELECTED_FRAME();
  struct face *fp = FACE_FROM_ID (sf, face);
668
  unsigned long fg, bg;
Kim F. Storm's avatar
Kim F. Storm committed
669

670
  if (!fp)
671
    {
672
      fp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
673 674 675 676 677
      /* The default face for the frame should always be realized and
	 cached.  */
      if (!fp)
	abort ();
    }
Kim F. Storm's avatar
Kim F. Storm committed
678
  screen_face = face;
679 680 681 682 683 684 685 686 687 688
  fg = fp->foreground;
  bg = fp->background;

  /* Don't use invalid colors.  In particular, a color of -1 means use
     the colors of the default face, except that if highlight is on,
     invert the foreground and the background.  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.  */
  if (fg == (unsigned long)-1)
689 690
    fg = highlight || fp->tty_reverse_p ? FRAME_BACKGROUND_PIXEL (sf)
					: FRAME_FOREGROUND_PIXEL (sf);
691
  if (bg == (unsigned long)-1)
692 693
    bg = highlight || fp->tty_reverse_p ? FRAME_FOREGROUND_PIXEL (sf)
					: FRAME_BACKGROUND_PIXEL (sf);
694 695 696 697 698 699 700 701 702 703 704 705 706
  if (termscript)
    fprintf (termscript, "<FACE %d%s: %d/%d>",
	     face, highlight ? "H" : "", fp->foreground, fp->background);
  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
707 708
}

709 710
Lisp_Object Vdos_unsupported_char_glyph;

711
static void
712
IT_write_glyphs (struct glyph *str, int str_len)
Kim F. Storm's avatar
Kim F. Storm committed
713
{
714 715 716
  unsigned char *screen_buf, *screen_bp, *screen_buf_end, *bp;
  int unsupported_face = FAST_GLYPH_FACE (Vdos_unsupported_char_glyph);
  unsigned unsupported_char= FAST_GLYPH_CHAR (Vdos_unsupported_char_glyph);
717
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
718 719 720 721
  register int sl = str_len;
  register int tlen = GLYPH_TABLE_LENGTH;
  register Lisp_Object *tbase = GLYPH_TABLE_BASE;

722 723 724
  struct coding_system *coding = (CODING_REQUIRE_ENCODING (&terminal_coding)
				  ? &terminal_coding
				  : &safe_terminal_coding);
725
  struct frame *sf;
Morten Welinder's avatar
Morten Welinder committed
726

727 728 729
  /* Do we need to consider conversion of unibyte characters to
     multibyte?  */
  int convert_unibyte_characters
730 731
    = (NILP (current_buffer->enable_multibyte_characters)
       && unibyte_display_via_language_environment);
732

733
  if (str_len == 0) return;
734
  
735 736
  screen_buf = screen_bp = alloca (str_len * 2);
  screen_buf_end = screen_buf + str_len * 2;
737
  sf = SELECTED_FRAME();
738 739 740 741 742 743 744

  /* 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);
745
  
746 747 748 749 750
  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
     the tail.  */
  terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
  while (sl)
    {
751
      int cf, chlen, enclen;
752
      unsigned char workbuf[4], *buf;
753 754
      unsigned ch;
      register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
755 756 757 758 759 760 761 762

      /* Find the actual glyph to display by traversing the entire
	 aliases chain for this glyph.  */
      GLYPH_FOLLOW_ALIASES (tbase, tlen, g);

      /* Glyphs with GLYPH_MASK_PADDING bit set are actually there
	 only for the redisplay code to know how many columns does
         this character occupy on the screen.  Skip padding glyphs.  */
763
      if (CHAR_GLYPH_PADDING_P (*str))
764 765 766 767 768 769 770 771 772
	{
	  str++;
	  sl--;
	}
      else
	{
	  /* Convert the character code to multibyte, if they
	     requested display via language environment.  */
	  ch = FAST_GLYPH_CHAR (g);
773 774 775
	  /* We only want to convert unibyte characters to multibyte
	     in unibyte buffers!  Otherwise, the 8-bit code might come
	     from the display table set up to display foreign characters.  */
776
	  if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
777 778
	      && (ch >= 0240
		  || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
779 780 781
	    ch = unibyte_char_to_multibyte (ch);

	  /* Invalid characters are displayed with a special glyph.  */
782
	  if (! GLYPH_CHAR_VALID_P (ch))
783 784 785
	    {
	      g = !NILP (Vdos_unsupported_char_glyph)
		? Vdos_unsupported_char_glyph
786
		: MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
787 788 789 790 791 792 793
	      ch = FAST_GLYPH_CHAR (g);
	    }
	  if (COMPOSITE_CHAR_P (ch))
	    {
	      /* If CH is a composite character, we can display
		 only the first component.  */
	      g = cmpchar_table[COMPOSITE_CHAR_ID (ch)]->glyph[0],
794
	      ch = GLYPH_CHAR (sf, g);
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	      cf = FAST_GLYPH_FACE (g);
	    }

	  /* If the face of this glyph is different from the current
	     screen face, update the screen attribute byte.  */
	  cf = FAST_GLYPH_FACE (g);
	  if (cf != screen_face)
	    IT_set_face (cf);	/* handles invalid faces gracefully */

	  if (GLYPH_SIMPLE_P (tbase, tlen, g))
	    /* We generate the multi-byte form of CH in BUF.  */
	    chlen = CHAR_STRING (ch, workbuf, buf);
	  else
	    {
	      /* We have a string in Vglyph_table.  */
	      chlen = GLYPH_LENGTH (tbase, g);
	      buf = GLYPH_STRING (tbase, g);
	    }

814
	  /* If the character is not multibyte, don't bother converting it.  */
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	  if (chlen == 1)
	    {
	      *conversion_buffer = (unsigned char)ch;
	      chlen = 0;
	      enclen = 1;
	    }
	  else
	    {
	      encode_coding (coding, buf, conversion_buffer, chlen,
			     conversion_buffer_size);
	      chlen -= coding->consumed;
	      enclen = coding->produced;

	      /* Replace glyph codes that cannot be converted by
		 terminal_coding with Vdos_unsupported_char_glyph.  */
	      if (*conversion_buffer == '?')
		{
		  char *cbp = conversion_buffer;

		  while (cbp < conversion_buffer + enclen && *cbp == '?')
		    *cbp++ = unsupported_char;
		  if (unsupported_face != screen_face)
		    IT_set_face (unsupported_face);
		}
	    }

	  if (enclen + chlen > screen_buf_end - screen_bp)
	    {
	      /* The allocated buffer for screen writes is too small.
		 Flush it and loop again without incrementing STR, so
		 that the next loop will begin with the same glyph.  */
	      int nbytes = screen_bp - screen_buf;

	      mouse_off_maybe ();
	      dosmemput (screen_buf, nbytes, (int)ScreenPrimary + offset);
	      if (screen_virtual_segment)
		dosv_refresh_virtual_screen (offset, nbytes / 2);
	      new_pos_X += nbytes / 2;
	      offset += nbytes;

	      /* Prepare to reuse the same buffer again.  */
	      screen_bp = screen_buf;
	    }
	  else
	    {
	      /* There's enough place in the allocated buffer to add
		 the encoding of this glyph.  */

	      /* First, copy the encoded bytes.  */
	      for (bp = conversion_buffer; enclen--; bp++)
		{
		  *screen_bp++ = (unsigned char)*bp;
		  *screen_bp++ = ScreenAttrib;
		  if (termscript)
		    fputc (*bp, termscript);
		}

	      /* Now copy the bytes not consumed by the encoding.  */
	      if (chlen > 0)
		{
		  buf += coding->consumed;
		  while (chlen--)
		    {
		      if (termscript)
			fputc (*buf, termscript);
		      *screen_bp++ = (unsigned char)*buf++;
		      *screen_bp++ = ScreenAttrib;
		    }
		}

	      /* Update STR and its remaining length.  */
	      str++;
	      sl--;
	    }
	}
890 891
    }

892
  /* Dump whatever is left in the screen buffer.  */
Kim F. Storm's avatar
Kim F. Storm committed
893
  mouse_off_maybe ();
894
  dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
895
  if (screen_virtual_segment)
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
    dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
  new_pos_X += (screen_bp - screen_buf) / 2;

  /* We may have to output some codes to terminate the writing.  */
  if (CODING_REQUIRE_FLUSHING (coding))
    {
      coding->mode |= CODING_MODE_LAST_BLOCK;
      encode_coding (coding, "", conversion_buffer, 0, conversion_buffer_size);
      if (coding->produced > 0)
	{
	  for (screen_bp = screen_buf, bp = conversion_buffer;
	       coding->produced--; bp++)
	    {
	      *screen_bp++ = (unsigned char)*bp;
	      *screen_bp++ = ScreenAttrib;
	      if (termscript)
		fputc (*bp, termscript);
	    }
	  offset += screen_bp - screen_buf;
	  mouse_off_maybe ();
	  dosmemput (screen_buf, screen_bp - screen_buf,
		     (int)ScreenPrimary + offset);
	  if (screen_virtual_segment)
	    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
923
}
924

925 926
static void
IT_clear_end_of_line (int first_unused)
Kim F. Storm's avatar
Kim F. Storm committed
927 928 929
{
  char *spaces, *sp;
  int i, j;
930
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
931 932
  extern int fatal_error_in_progress;

933
  if (new_pos_X >= first_unused || fatal_error_in_progress)
934
    return;
Kim F. Storm's avatar
Kim F. Storm committed
935 936 937 938

  IT_set_face (0);
  if (termscript)
    fprintf (termscript, "<CLR:EOL>");
939
  i = (j = first_unused - new_pos_X) * 2;
Kim F. Storm's avatar
Kim F. Storm committed
940
  spaces = sp = alloca (i);
941
  
Kim F. Storm's avatar
Kim F. Storm committed
942
  while (--j >= 0)
943
    {
Kim F. Storm's avatar
Kim F. Storm committed
944 945
      *sp++ = ' ';
      *sp++ = ScreenAttrib;
946 947
    }

Kim F. Storm's avatar
Kim F. Storm committed
948
  mouse_off_maybe ();
949 950 951
  dosmemput (spaces, i, (int)ScreenPrimary + offset);
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (offset, i / 2);
952 953 954 955

  /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
     Let's follow their lead, in case someone relies on this.  */
  new_pos_X = first_unused;
956 957
}

958
static void
Kim F. Storm's avatar
Kim F. Storm committed
959 960 961 962 963 964 965
IT_clear_screen (void)
{
  if (termscript)
    fprintf (termscript, "<CLR:SCR>");
  IT_set_face (0);
  mouse_off ();
  ScreenClear ();
966 967
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (0, screen_size);
Kim F. Storm's avatar
Kim F. Storm committed
968 969 970
  new_pos_X = new_pos_Y = 0;
}

971
static void
Kim F. Storm's avatar
Kim F. Storm committed
972 973 974 975 976 977 978 979 980 981 982 983
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++;
  }
}

984
static void
Kim F. Storm's avatar
Kim F. Storm committed
985 986 987 988 989 990 991 992
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;
}

993 994
static int cursor_cleared;

995
static void
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
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.  */

1029 1030
static void
IT_cmgoto (FRAME_PTR f)
1031 1032 1033
{
  /* Only set the cursor to where it should be if the display is
     already in sync with the window contents.  */
1034 1035 1036 1037 1038
  int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */

  /* FIXME: This needs to be rewritten for the new redisplay, or
     removed.  */
#if 0
1039 1040
  static int previous_pos_X = -1;

1041 1042
  update_cursor_pos = 1;	/* temporary!!! */

1043 1044 1045 1046 1047 1048 1049
  /* 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.  */
1050 1051 1052
  if (!update_cursor_pos
      && XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top) <= new_pos_Y)
    {
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
      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;
	}
1071

1072
      if (current_pos_X != tem_X)
1073 1074 1075 1076
	{
	  new_pos_X = tem_X;
	  update_cursor_pos = 1;
	}
1077
    }
1078
#endif
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

  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 ();
}

1097 1098
static void
IT_reassert_line_highlight (int new, int vpos)
Kim F. Storm's avatar
Kim F. Storm committed
1099 1100 1101 1102
{
  highlight = new;
}

1103
static void
1104
IT_change_line_highlight (int new_highlight, int y, int vpos, int first_unused_hpos)
Richard M. Stallman's avatar
Richard M. Stallman committed
1105
{
Kim F. Storm's avatar
Kim F. Storm committed
1106 1107 1108 1109 1110
  highlight = new_highlight;
  IT_cursor_to (vpos, 0);
  IT_clear_end_of_line (first_unused_hpos);
}

1111 1112
static void
IT_update_begin (struct frame *foo)
Kim F. Storm's avatar
Kim F. Storm committed
1113 1114 1115 1116
{
  highlight = 0;
}

1117 1118
static void
IT_update_end (struct frame *foo)
Kim F. Storm's avatar
Kim F. Storm committed
1119 1120
{
}
Richard M. Stallman's avatar
Richard M. Stallman committed
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
/* Copy LEN glyphs displayed on a single line whose vertical position
   is YPOS, beginning at horizontal position XFROM to horizontal
   position XTO, by moving blocks in the video memory.  Used by
   functions that insert and delete glyphs.  */
static void
IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
{
  /* The offsets of source and destination relative to the
     conventional memorty selector.  */
  int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
  int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;

  if (from == to || len <= 0)
    return;

  _farsetsel (_dos_ds);

  /* The source and destination might overlap, so we need to move
     glyphs non-destructively.  */
  if (from > to)
    {
      for ( ; len; from += 2, to += 2, len--)
	_farnspokew (to, _farnspeekw (from));
    }
  else
    {
      from += (len - 1) * 2;
      to += (len - 1) * 2;
      for ( ; len; from -= 2, to -= 2, len--)
	_farnspokew (to, _farnspeekw (from));
    }
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
}

/* Insert and delete glyphs.  */
1158 1159
static void
IT_insert_glyphs (start, len)
1160
     register struct glyph *start;
1161 1162
     register int len;
{
1163 1164 1165 1166 1167 1168 1169 1170
  int shift_by_width = screen_size_X - (new_pos_X + len);

  /* Shift right the glyphs from the nominal cursor position to the
     end of this line.  */
  IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);

  /* Now write the glyphs to be inserted.  */
  IT_write_glyphs (start, len);
1171 1172 1173 1174 1175 1176 1177 1178 1179
}

static void
IT_delete_glyphs (n)
     register int n;
{
  abort ();
}

1180
/* set-window-configuration on window.c needs this.  */
1181 1182 1183 1184 1185
void
x_set_menu_bar_lines (f, value, oldval)
     struct frame *f;
     Lisp_Object value, oldval;
{
1186 1187
  set_menu_bar_lines (f, value, oldval);
}
1188

1189
/* This was copied from xfns.c  */
1190

1191 1192 1193
Lisp_Object Qbackground_color;
Lisp_Object Qforeground_color;
extern Lisp_Object Qtitle;
1194

1195 1196
/* 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
1197

1198
static void
Kim F. Storm's avatar
Kim F. Storm committed
1199 1200
IT_set_terminal_modes (void)
{
1201
  if (termscript)
Kim F. Storm's avatar
Kim F. Storm committed
1202 1203 1204 1205 1206 1207
    fprintf (termscript, "\n<SET_TERM>");
  highlight = 0;

  screen_size_X = ScreenCols ();
  screen_size_Y = ScreenRows ();
  screen_size = screen_size_X * screen_size_Y;
1208
  
Kim F. Storm's avatar
Kim F. Storm committed
1209 1210 1211 1212 1213 1214
  new_pos_X = new_pos_Y = 0;
  current_pos_X = current_pos_Y = -1;

  if (term_setup_done)
    return;
  term_setup_done = 1;
1215
  
Kim F. Storm's avatar
Kim F. Storm committed
1216 1217
  startup_screen_size_X = screen_size_X;
  startup_screen_size_Y = screen_size_Y;
1218
  startup_screen_attrib = ScreenAttrib;
Kim F. Storm's avatar
Kim F. Storm committed
1219

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
#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);

1238
    if (regs.x.es != es_value)
1239
      {
1240 1241 1242 1243 1244 1245
	/* screen_old_address is only set if ScreenPrimary does NOT
	   already point to the relocated buffer address returned by
	   the Int 10h/AX=FEh call above.  DJGPP v2.02 and later sets
	   ScreenPrimary to that address at startup under DOS/V.  */
	if (regs.x.es != (ScreenPrimary >> 4) & 0xffff)
	  screen_old_address = ScreenPrimary;
1246 1247 1248 1249 1250 1251 1252
	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
1253 1254 1255 1256
  ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
  ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));

  if (termscript)
1257
    fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
1258
	     screen_size_X, screen_size_Y);
1259 1260

  bright_bg ();
Kim F. Storm's avatar
Kim F. Storm committed
1261 1262
}

1263 1264
/* IT_reset_terminal_modes is called when emacs is
   suspended or killed.  */
Kim F. Storm's avatar
Kim F. Storm committed
1265

1266
static void
Kim F. Storm's avatar
Kim F. Storm committed
1267 1268
IT_reset_terminal_modes (void)
{