msdos.c 142 KB
Newer Older
1
/* MS-DOS specific C utilities.          -*- coding: raw-text -*-
2
   Copyright (C) 1993, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002,
Glenn Morris's avatar
Glenn Morris committed
3
                 2003, 2004, 2005, 2006, 2007, 2008, 2009
Glenn Morris's avatar
Glenn Morris committed
4
                 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
5 6 7

This file is part of GNU Emacs.

8
GNU Emacs is free software: you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
9
it under the terms of the GNU General Public License as published by
10 11
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
12 13 14 15 16 17 18

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
19
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
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

#ifdef MSDOS
29
#include <setjmp.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
30 31 32
#include "lisp.h"
#include <stdio.h>
#include <stdlib.h>
33
#include <time.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
34 35 36
#include <sys/param.h>
#include <sys/time.h>
#include <dos.h>
37
#include <errno.h>
38
#include <string.h>	 /* for bzero and string functions */
39
#include <sys/stat.h>    /* for _fixpath */
40
#include <unistd.h>	 /* for chdir, dup, dup2, etc. */
41
#include <dir.h>	 /* for getdisk */
42
#if __DJGPP__ >= 2
43
#pragma pack(0)		 /* dir.h does a pack(4), which isn't GCC's default */
44
#include <fcntl.h>
45
#include <io.h>		 /* for setmode */
46 47
#include <dpmi.h>	 /* for __dpmi_xxx stuff */
#include <sys/farptr.h>	 /* for _farsetsel, _farnspokeb */
48
#include <libc/dosio.h>  /* for _USE_LFN */
49
#include <conio.h>	 /* for cputs */
50 51
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
52 53
#include "msdos.h"
#include "systime.h"
54
#include "frame.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
55
#include "termhooks.h"
56
#include "termchar.h"
Morten Welinder's avatar
Morten Welinder committed
57
#include "dispextern.h"
58
#include "dosfns.h"
Morten Welinder's avatar
Morten Welinder committed
59
#include "termopts.h"
60
#include "character.h"
61 62
#include "coding.h"
#include "disptab.h"
Morten Welinder's avatar
Morten Welinder committed
63
#include "window.h"
64 65
#include "buffer.h"
#include "commands.h"
66
#include "blockinput.h"
Eli Zaretskii's avatar
Eli Zaretskii committed
67
#include "keyboard.h"
Kenichi Handa's avatar
Kenichi Handa committed
68
#include "intervals.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
69 70 71 72 73 74 75
#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

76 77 78 79
#ifndef _USE_LFN
#define _USE_LFN 0
#endif

80 81 82 83
#ifndef _dos_ds
#define _dos_ds _go32_info_block.selector_for_linear_memory
#endif

84 85
#if __DJGPP__ > 1

Richard M. Stallman's avatar
Richard M. Stallman committed
86
#include <signal.h>
87
#include "syssignal.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#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 */
108 109 110 111 112 113

static unsigned long
event_timestamp ()
{
  struct time t;
  unsigned long s;
Eli Zaretskii's avatar
Eli Zaretskii committed
114

115 116 117 118 119 120
  gettime (&t);
  s = t.ti_min;
  s *= 60;
  s += t.ti_sec;
  s *= 1000;
  s += t.ti_hund * 10;
Eli Zaretskii's avatar
Eli Zaretskii committed
121

122 123 124
  return s;
}

Kim F. Storm's avatar
Kim F. Storm committed
125 126 127 128 129 130

/* ------------------------ 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
131

Eli Zaretskii's avatar
Eli Zaretskii committed
132 133 134 135
/* 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
136 137
int have_mouse;          /* 0: no, 1: enabled, -1: disabled */
static int mouse_visible;
Richard M. Stallman's avatar
Richard M. Stallman committed
138

Kim F. Storm's avatar
Kim F. Storm committed
139 140
static int mouse_last_x;
static int mouse_last_y;
Richard M. Stallman's avatar
Richard M. Stallman committed
141

Kim F. Storm's avatar
Kim F. Storm committed
142 143
static int mouse_button_translate[NUM_MOUSE_BUTTONS];
static int mouse_button_count;
Richard M. Stallman's avatar
Richard M. Stallman committed
144

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

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
    {
152 153 154 155
      struct tty_display_info *tty = CURTTY ();

      if (tty->termscript)
	fprintf (tty->termscript, "<M_ON>");
Kim F. Storm's avatar
Kim F. Storm committed
156 157 158
      regs.x.ax = 0x0001;
      int86 (0x33, &regs, &regs);
      mouse_visible = 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
159 160 161
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
162 163
void
mouse_off ()
Richard M. Stallman's avatar
Richard M. Stallman committed
164
{
Kim F. Storm's avatar
Kim F. Storm committed
165
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
166

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

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

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
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: ",
199 200 201 202 203
       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.  */)
     (nbuttons)
204 205
     Lisp_Object nbuttons;
{
206 207
  int n;

208
  CHECK_NUMBER (nbuttons);
209 210
  n = XINT (nbuttons);
  if (n < 2 || n > 3)
211 212 213
    xsignal2 (Qargs_out_of_range,
	      build_string ("only 2 or 3 mouse buttons are supported"),
	      nbuttons);
214
  mouse_setup_buttons (n);
215 216 217
  return Qnil;
}

218 219 220 221 222 223 224 225 226 227 228
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
229 230 231
void
mouse_moveto (x, y)
     int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
232
{
Kim F. Storm's avatar
Kim F. Storm committed
233
  union REGS regs;
234
  struct tty_display_info *tty = CURTTY ();
Richard M. Stallman's avatar
Richard M. Stallman committed
235

236 237
  if (tty->termscript)
    fprintf (tty->termscript, "<M_XY=%dx%d>", x, y);
Kim F. Storm's avatar
Kim F. Storm committed
238 239 240 241
  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
242 243
}

Kim F. Storm's avatar
Kim F. Storm committed
244 245 246
static int
mouse_pressed (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
247
{
Kim F. Storm's avatar
Kim F. Storm committed
248
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
249

Kim F. Storm's avatar
Kim F. Storm committed
250 251 252 253 254 255 256 257
  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
258 259
}

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

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 = 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
274 275
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
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
295 296 297 298 299 300 301 302 303
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;
304 305 306 307 308
  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
309

310
  *f = SELECTED_FRAME();
Kim F. Storm's avatar
Kim F. Storm committed
311 312 313
  *bar_window = Qnil;
  mouse_get_xy (&ix, &iy);
  *time = event_timestamp ();
314 315
  *x = make_number (mouse_last_x = ix);
  *y = make_number (mouse_last_y = iy);
Kim F. Storm's avatar
Kim F. Storm committed
316
}
Richard M. Stallman's avatar
Richard M. Stallman committed
317

Kim F. Storm's avatar
Kim F. Storm committed
318 319
static void
mouse_check_moved ()
Richard M. Stallman's avatar
Richard M. Stallman committed
320
{
321
  int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
322

Kim F. Storm's avatar
Kim F. Storm committed
323
  mouse_get_xy (&x, &y);
324
  SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
Kim F. Storm's avatar
Kim F. Storm committed
325 326 327
  mouse_last_x = x;
  mouse_last_y = y;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
328

Eli Zaretskii's avatar
Eli Zaretskii committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
/* 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
345 346 347 348
void
mouse_init ()
{
  union REGS regs;
349
  struct tty_display_info *tty = CURTTY ();
350

351 352
  if (tty->termscript)
    fprintf (tty->termscript, "<M_INIT>");
Richard M. Stallman's avatar
Richard M. Stallman committed
353

Kim F. Storm's avatar
Kim F. Storm committed
354 355
  regs.x.ax = 0x0021;
  int86 (0x33, &regs, &regs);
356

357 358 359 360
  /* 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
361
  mouse_clear_clicks ();
362

Kim F. Storm's avatar
Kim F. Storm committed
363 364 365 366
  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
367

Kim F. Storm's avatar
Kim F. Storm committed
368 369 370 371
  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
372

Kim F. Storm's avatar
Kim F. Storm committed
373 374 375
  mouse_moveto (0, 0);
  mouse_visible = 0;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
376

Kim F. Storm's avatar
Kim F. Storm committed
377 378 379
/* ------------------------- Screen control ----------------------
 *
 */
380

Kim F. Storm's avatar
Kim F. Storm committed
381
static int internal_terminal = 0;
382

Kim F. Storm's avatar
Kim F. Storm committed
383 384 385
#ifndef HAVE_X_WINDOWS
extern unsigned char ScreenAttrib;
static int screen_face;
386

Kim F. Storm's avatar
Kim F. Storm committed
387 388 389
static int screen_size_X;
static int screen_size_Y;
static int screen_size;
Richard M. Stallman's avatar
Richard M. Stallman committed
390

Kim F. Storm's avatar
Kim F. Storm committed
391 392 393 394
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
395

Kim F. Storm's avatar
Kim F. Storm committed
396 397 398 399 400
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;
401
static unsigned char startup_screen_attrib;
Richard M. Stallman's avatar
Richard M. Stallman committed
402

403 404
static clock_t startup_time;

Kim F. Storm's avatar
Kim F. Storm committed
405
static int term_setup_done;
Richard M. Stallman's avatar
Richard M. Stallman committed
406

407 408
static unsigned short outside_cursor;

Kim F. Storm's avatar
Kim F. Storm committed
409
/* Similar to the_only_frame.  */
410
struct tty_display_info the_only_display_info;
Richard M. Stallman's avatar
Richard M. Stallman committed
411

412 413 414 415 416 417 418 419
/* 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;
420 421
/* A flag to control how to display unibyte 8-bit characters.  */
extern int unibyte_display_via_language_environment;
422

Kenichi Handa's avatar
Kenichi Handa committed
423 424
extern Lisp_Object Qcursor_type;
extern Lisp_Object Qbar, Qhbar;
425

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

430 431 432 433 434 435 436 437
#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;

438
  if (offset < 0 || count < 0)	/* paranoia; invalid values crash DOS/V */
439 440
    return;

441 442 443 444 445 446 447 448
  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

449
static void
Kim F. Storm's avatar
Kim F. Storm committed
450
dos_direct_output (y, x, buf, len)
Eli Zaretskii's avatar
Eli Zaretskii committed
451
     int x, y;
Kim F. Storm's avatar
Kim F. Storm committed
452 453
     char *buf;
     int len;
Richard M. Stallman's avatar
Richard M. Stallman committed
454
{
455 456
  int t0 = 2 * (x + y * screen_size_X);
  int t = t0 + (int) ScreenPrimary;
457
  int l0 = len;
458 459

#if (__DJGPP__ < 2)
Kim F. Storm's avatar
Kim F. Storm committed
460 461 462 463
  while (--len >= 0) {
    dosmemput (buf++, 1, t);
    t += 2;
  }
464 465 466 467
#else
  /* This is faster.  */
  for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
    _farnspokeb (t, *buf);
468 469 470

  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (t0, l0);
471
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
472
}
473
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
474 475 476

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

Kim F. Storm's avatar
Kim F. Storm committed
477
#if (__DJGPP__ < 2)
478
static void
479
do_visible_bell (xorattr)
Richard M. Stallman's avatar
Richard M. Stallman committed
480 481
     unsigned char xorattr;
{
482
  asm volatile
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    ("  movb   $1,%%dl				\n\
visible_bell_0:					\n\
	movl   _ScreenPrimary,%%eax		\n\
	call   dosmemsetup			\n\
	movl   %%eax,%%ebx			\n\
	movl   %1,%%ecx				\n\
	movb   %0,%%al				\n\
	incl   %%ebx				\n\
visible_bell_1:					\n\
	xorb   %%al,%%gs:(%%ebx)		\n\
	addl   $2,%%ebx				\n\
	decl   %%ecx				\n\
	jne    visible_bell_1			\n\
	decb   %%dl				\n\
	jne    visible_bell_3			\n\
visible_bell_2:					\n\
	movzwl %%ax,%%eax			\n\
        movzwl %%ax,%%eax			\n\
	movzwl %%ax,%%eax			\n\
	movzwl %%ax,%%eax			\n\
	decw   %%cx				\n\
	jne    visible_bell_2			\n\
	jmp    visible_bell_0                   \n\
506 507
visible_bell_3:"
     : /* no output */
Kim F. Storm's avatar
Kim F. Storm committed
508
     : "m" (xorattr), "g" (screen_size)
509
     : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
Richard M. Stallman's avatar
Richard M. Stallman committed
510 511
}

Kim F. Storm's avatar
Kim F. Storm committed
512 513 514 515 516
static void
ScreenVisualBell (void)
{
  /* This creates an xor-mask that will swap the default fore- and
     background colors.  */
Karoly Lorentey's avatar
Karoly Lorentey committed
517 518
  do_visible_bell (((FRAME_FOREGROUND_PIXEL (SELECTED_FRAME ())
		     ^ FRAME_BACKGROUND_PIXEL (SELECTED_FRAME ()))
Kim F. Storm's avatar
Kim F. Storm committed
519 520 521 522 523 524
		    * 0x11) & 0x7f);
}
#endif

#ifndef HAVE_X_WINDOWS

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

527 528 529 530 531 532
/* Enable bright background colors.  */
static void
bright_bg (void)
{
  union REGS regs;

533 534 535 536 537
  /* 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;

538 539 540 541 542
  regs.h.bl = 0;
  regs.x.ax = 0x1003;
  int86 (0x10, &regs, &regs);
}

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/* 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);
    }
}

558 559 560 561 562 563 564 565 566 567 568 569 570
/* 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;
}

571 572
/* Set the screen dimensions so that it can show no less than
   ROWS x COLS frame.  */
573

574 575 576 577 578 579
void
dos_set_window_size (rows, cols)
     int *rows, *cols;
{
  char video_name[30];
  union REGS regs;
Eli Zaretskii's avatar
Eli Zaretskii committed
580 581
  Lisp_Object video_mode;
  int video_mode_value, have_vga = 0;
582 583 584 585 586 587
  int current_rows = ScreenRows (), current_cols = ScreenCols ();

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

  mouse_off ();
588
  have_vga = vga_installed ();
589

590
  /* If the user specified a special video mode for these dimensions,
591 592 593 594 595 596 597 598 599 600
     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);
601 602 603 604 605 606 607 608 609

      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);
	}
610 611 612 613 614 615 616 617 618 619
    }

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

#if __DJGPP__ > 1

  else
    {
      static struct {
Eli Zaretskii's avatar
Eli Zaretskii committed
620
	int rows, need_vga;
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
      }	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)
638
	       _set_screen_lines (std_dimension[i].rows);
639 640
	     break;
	   }
641
	 i++;
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
	}
    }

#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 ();
690

691 692 693 694 695
  /* Update Emacs' notion of screen dimensions.  */
  screen_size_X = *cols;
  screen_size_Y = *rows;
  screen_size = *cols * *rows;

696 697 698 699 700
#if __DJGPP__ > 1
  /* If the dimensions changed, the mouse highlight info is invalid.  */
  if (current_rows != *rows || current_cols != *cols)
    {
      struct frame *f = SELECTED_FRAME();
701
      struct tty_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
702 703 704 705 706 707 708 709 710 711 712
      Lisp_Object window = dpyinfo->mouse_face_window;

      if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
	{
	  dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
	  dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
	  dpyinfo->mouse_face_window = Qnil;
	}
    }
#endif

713 714
  /* Enable bright background colors.  */
  bright_bg ();
715 716 717 718 719

  /* 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);
720 721
}

722 723
/* If we write a character in the position where the mouse is,
   the mouse cursor may need to be refreshed.  */
724 725

static void
Kim F. Storm's avatar
Kim F. Storm committed
726
mouse_off_maybe ()
727
{
Kim F. Storm's avatar
Kim F. Storm committed
728
  int x, y;
Eli Zaretskii's avatar
Eli Zaretskii committed
729

Kim F. Storm's avatar
Kim F. Storm committed
730 731
  if (!mouse_visible)
    return;
Eli Zaretskii's avatar
Eli Zaretskii committed
732

Kim F. Storm's avatar
Kim F. Storm committed
733 734 735
  mouse_get_xy (&x, &y);
  if (y != new_pos_Y || x < new_pos_X)
    return;
Eli Zaretskii's avatar
Eli Zaretskii committed
736

Kim F. Storm's avatar
Kim F. Storm committed
737 738 739
  mouse_off ();
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753
#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)
{
#if __DJGPP__ > 1
  unsigned desired_cursor;
  __dpmi_regs regs;
  int max_line, top_line, bot_line;
754
  struct tty_display_info *tty = FRAME_TTY (f);
755 756 757 758 759 760 761

  /* Avoid the costly BIOS call if F isn't the currently selected
     frame.  Allow for NULL as unconditionally meaning the selected
     frame.  */
  if (f && f != SELECTED_FRAME())
    return;

762 763
  if (tty->termscript)
    fprintf (tty->termscript, "\nCURSOR SHAPE=(%d,%d)", start_line, width);
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 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  /* 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);
#endif /* __DJGPP__ > 1 */
}

static void
IT_set_cursor_type (struct frame *f, Lisp_Object cursor_type)
{
Eli Zaretskii's avatar
Eli Zaretskii committed
833
  if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
834 835 836 837
    {
      /* 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
838 839 840
  else if (CONSP (cursor_type)
	   && (EQ (XCAR (cursor_type), Qbar)
	       || EQ (XCAR (cursor_type), Qhbar)))
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
    {
      Lisp_Object bar_parms = XCDR (cursor_type);
      int width;

      if (INTEGERP (bar_parms))
	{
	  /* Feature: negative WIDTH means cursor at the top
	     of the character cell, zero means invisible cursor.  */
	  width = XINT (bar_parms);
	  msdos_set_cursor_shape (f, width >= 0 ? DEFAULT_CURSOR_START : 0,
				  width);
	}
      else if (CONSP (bar_parms)
	       && INTEGERP (XCAR (bar_parms))
	       && INTEGERP (XCDR (bar_parms)))
	{
	  int start_line = XINT (XCDR (bar_parms));

	  width = XINT (XCAR (bar_parms));
	  msdos_set_cursor_shape (f, start_line, width);
	}
    }
  else
864 865 866 867 868 869
    {
      /* 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);
    }
870 871
}

872
static void
873
IT_ring_bell (struct frame *f)
Kim F. Storm's avatar
Kim F. Storm committed
874 875
{
  if (visible_bell)
876
    {
Kim F. Storm's avatar
Kim F. Storm committed
877 878
      mouse_off ();
      ScreenVisualBell ();
879
    }
Kim F. Storm's avatar
Kim F. Storm committed
880
  else
881 882 883 884 885 886
    {
      union REGS inregs, outregs;
      inregs.h.ah = 2;
      inregs.h.dl = 7;
      intdos (&inregs, &outregs);
    }
887 888
}

889 890 891 892 893
/* 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
894 895 896
static void
IT_set_face (int face)
{
897
  struct frame *sf = SELECTED_FRAME();
898 899 900
  struct face *fp  = FACE_FROM_ID (sf, face);
  struct face *dfp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
  unsigned long fg, bg, dflt_fg, dflt_bg;
901
  struct tty_display_info *tty = FRAME_TTY (sf);
Kim F. Storm's avatar
Kim F. Storm committed
902

903
  if (!fp)
904
    {
905
      fp = dfp;
906 907 908 909 910
      /* The default face for the frame should always be realized and
	 cached.  */
      if (!fp)
	abort ();
    }
Kim F. Storm's avatar
Kim F. Storm committed
911
  screen_face = face;
912 913
  fg = fp->foreground;
  bg = fp->background;
914 915
  dflt_fg = dfp->foreground;
  dflt_bg = dfp->background;
916

917 918 919 920
  /* 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.  */
921
  if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
922
    fg = FRAME_FOREGROUND_PIXEL (sf);
923
  else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
924 925 926
    fg = FRAME_BACKGROUND_PIXEL (sf);
  if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
    bg = FRAME_BACKGROUND_PIXEL (sf);
927
  else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
928 929 930
    bg = FRAME_FOREGROUND_PIXEL (sf);

  /* Make sure highlighted lines really stand out, come what may.  */
931
  if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
932 933 934 935 936 937
    {
      unsigned long tem = fg;

      fg = bg;
      bg = tem;
    }
938 939 940 941 942 943 944 945
  /* If the user requested inverse video, obey.  */
  if (inverse_video)
    {
      unsigned long tem2 = fg;

      fg = bg;
      bg = tem2;
    }
946 947
  if (tty->termscript)
    fprintf (tty->termscript, "<FACE %d: %d/%d[FG:%d/BG:%d]>", face,
948
	     fp->foreground, fp->background, fg, bg);
949 950 951 952 953 954 955 956 957 958
  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
959 960
}

Eli Zaretskii's avatar
Eli Zaretskii committed
961 962 963 964 965
/* 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
   accomodate the screen attribute byte.  */
#define MAX_SCREEN_BUF 160*2

966
Lisp_Object Vdos_unsupported_char_glyph;
967 968
extern unsigned char *encode_terminal_code (struct glyph *, int,
					    struct coding_system *);
969
static void
970
IT_write_glyphs (struct frame *f, struct glyph *str, int str_len)
Kim F. Storm's avatar
Kim F. Storm committed
971
{
Eli Zaretskii's avatar
Eli Zaretskii committed
972
  unsigned char screen_buf[MAX_SCREEN_BUF], *screen_bp, *bp;
973
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
974
  register int sl = str_len;
975
  struct tty_display_info *tty = FRAME_TTY (f);
976
  struct frame *sf;
977
  unsigned char *conversion_buffer;
Morten Welinder's avatar
Morten Welinder committed
978

979 980 981
  /* Do we need to consider conversion of unibyte characters to
     multibyte?  */
  int convert_unibyte_characters
982 983
    = (NILP (current_buffer->enable_multibyte_characters)
       && unibyte_display_via_language_environment);
984

985 986 987 988 989 990 991
  /* 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;
992

993
  if (str_len <= 0) return;
Eli Zaretskii's avatar
Eli Zaretskii committed
994

995
  sf = SELECTED_FRAME();
996 997 998 999 1000 1001 1002

  /* 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
1003

1004 1005
  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
     the tail.  */
1006
  coding->mode &= ~CODING_MODE_LAST_BLOCK;
Eli Zaretskii's avatar
Eli Zaretskii committed
1007
  screen_bp = &screen_buf[0];
1008
  while (sl > 0)
1009
    {
1010
      int cf;
Eli Zaretskii's avatar
Eli Zaretskii committed
1011
      int n;
1012

Eli Zaretskii's avatar
Eli Zaretskii committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
      /* 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)
1030
	{
Eli Zaretskii's avatar
Eli Zaretskii committed
1031 1032
	  /* Copy the encoded bytes to the screen buffer.  */
	  for (bp = conversion_buffer; coding->produced--; bp++)
1033
	    {
Eli Zaretskii's avatar
Eli Zaretskii committed
1034 1035 1036
	      /* Paranoia: discard bytes that would overrun the end of
		 the screen buffer.  */
	      if (screen_bp - screen_buf <= MAX_SCREEN_BUF - 2)
1037
		{
Eli Zaretskii's avatar
Eli Zaretskii committed
1038 1039
		  *screen_bp++ = (unsigned char)*bp;
		  *screen_bp++ = ScreenAttrib;
1040
		}
Eli Zaretskii's avatar
Eli Zaretskii committed
1041 1042
	      if (tty->termscript)
		fputc (*bp, tty->termscript);
1043 1044
	    }
	}
Eli Zaretskii's avatar
Eli Zaretskii committed
1045 1046 1047
      /* Update STR and its remaining length.  */
      str += n;
      sl -= n;
1048 1049
    }

Eli Zaretskii's avatar
Eli Zaretskii committed
1050
  /* Dump whatever we have in the screen buffer.  */
Kim F. Storm's avatar
Kim F. Storm committed
1051
  mouse_off_maybe ();
1052
  dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
1053
  if (screen_virtual_segment)
1054 1055
    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
1056
}
1057

1058 1059 1060 1061
/************************************************************************
			  Mouse Highlight (and friends..)
 ************************************************************************/

1062
/* Last window where we saw the mouse.  Used by mouse-autoselect-window.  */
1063 1064
static Lisp_Object last_mouse_window;

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076