msdos.c 142 KB
Newer Older
1
/* MS-DOS specific C utilities.          -*- coding: raw-text -*-
2
   Copyright (C) 1993, 94, 95, 96, 97, 1999, 2000, 01, 2003
Eli Zaretskii's avatar
Eli Zaretskii committed
3
   Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6 7 8

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
9
the Free Software Foundation; either version 2, or (at your option)
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11 12 13 14 15 16 17 18
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
19 20
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
21

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

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

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

#ifdef MSDOS
#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 43
#if __DJGPP__ >= 2
#include <fcntl.h>
44
#include <io.h>		 /* for setmode */
45 46
#include <dpmi.h>	 /* for __dpmi_xxx stuff */
#include <sys/farptr.h>	 /* for _farsetsel, _farnspokeb */
47
#include <libc/dosio.h>  /* for _USE_LFN */
48
#include <conio.h>	 /* for cputs */
49 50
#endif

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

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

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

83 84
#if __DJGPP__ > 1

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

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

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

114 115 116 117 118 119
  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
120

121 122 123
  return s;
}

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

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

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

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

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

Kim F. Storm's avatar
Kim F. Storm committed
149
  if (have_mouse > 0 && !mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
150
    {
Kim F. Storm's avatar
Kim F. Storm committed
151 152 153 154 155
      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
156 157 158
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
159 160
void
mouse_off ()
Richard M. Stallman's avatar
Richard M. Stallman committed
161
{
Kim F. Storm's avatar
Kim F. Storm committed
162
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
163

Kim F. Storm's avatar
Kim F. Storm committed
164
  if (have_mouse > 0 && mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
165
    {
Kim F. Storm's avatar
Kim F. Storm committed
166 167 168 169 170
      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
171 172 173
    }
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
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: ",
194 195 196 197 198
       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)
199 200
     Lisp_Object nbuttons;
{
201 202
  int n;

203
  CHECK_NUMBER (nbuttons);
204 205 206 207 208 209
  n = XINT (nbuttons);
  if (n < 2 || n > 3)
    Fsignal (Qargs_out_of_range,
	     Fcons (build_string ("only 2 or 3 mouse buttons are supported"),
		    Fcons (nbuttons, Qnil)));
  mouse_setup_buttons (n);
210 211 212
  return Qnil;
}

213 214 215 216 217 218 219 220 221 222 223
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
224 225 226
void
mouse_moveto (x, y)
     int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
227
{
Kim F. Storm's avatar
Kim F. Storm committed
228
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
229

Kim F. Storm's avatar
Kim F. Storm committed
230 231 232 233 234 235
  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
236 237
}

Kim F. Storm's avatar
Kim F. Storm committed
238 239 240
static int
mouse_pressed (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
241
{
Kim F. Storm's avatar
Kim F. Storm committed
242
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
243

Kim F. Storm's avatar
Kim F. Storm committed
244 245 246 247 248 249 250 251
  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
252 253
}

Kim F. Storm's avatar
Kim F. Storm committed
254 255 256
static int
mouse_released (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
257 258 259
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
260 261 262 263 264 265 266 267
  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
268 269
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
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
289 290 291 292 293 294 295 296 297
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;
298 299 300 301 302
  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
303

304
  *f = SELECTED_FRAME();
Kim F. Storm's avatar
Kim F. Storm committed
305 306 307
  *bar_window = Qnil;
  mouse_get_xy (&ix, &iy);
  *time = event_timestamp ();
308 309
  *x = make_number (mouse_last_x = ix);
  *y = make_number (mouse_last_y = iy);
Kim F. Storm's avatar
Kim F. Storm committed
310
}
Richard M. Stallman's avatar
Richard M. Stallman committed
311

Kim F. Storm's avatar
Kim F. Storm committed
312 313
static void
mouse_check_moved ()
Richard M. Stallman's avatar
Richard M. Stallman committed
314
{
315
  int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
316

Kim F. Storm's avatar
Kim F. Storm committed
317
  mouse_get_xy (&x, &y);
318
  SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
Kim F. Storm's avatar
Kim F. Storm committed
319 320 321
  mouse_last_x = x;
  mouse_last_y = y;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
322

Eli Zaretskii's avatar
Eli Zaretskii committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
/* 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
339 340 341 342
void
mouse_init ()
{
  union REGS regs;
343

Kim F. Storm's avatar
Kim F. Storm committed
344 345
  if (termscript)
    fprintf (termscript, "<M_INIT>");
Richard M. Stallman's avatar
Richard M. Stallman committed
346

Kim F. Storm's avatar
Kim F. Storm committed
347 348
  regs.x.ax = 0x0021;
  int86 (0x33, &regs, &regs);
349

350 351 352 353
  /* 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
354
  mouse_clear_clicks ();
355

Kim F. Storm's avatar
Kim F. Storm committed
356 357 358 359
  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
360

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

Kim F. Storm's avatar
Kim F. Storm committed
366 367 368
  mouse_moveto (0, 0);
  mouse_visible = 0;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
369

Kim F. Storm's avatar
Kim F. Storm committed
370 371 372
/* ------------------------- Screen control ----------------------
 *
 */
373

Kim F. Storm's avatar
Kim F. Storm committed
374
static int internal_terminal = 0;
375

Kim F. Storm's avatar
Kim F. Storm committed
376 377 378
#ifndef HAVE_X_WINDOWS
extern unsigned char ScreenAttrib;
static int screen_face;
379

Kim F. Storm's avatar
Kim F. Storm committed
380 381 382
static int screen_size_X;
static int screen_size_Y;
static int screen_size;
Richard M. Stallman's avatar
Richard M. Stallman committed
383

Kim F. Storm's avatar
Kim F. Storm committed
384 385 386 387
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
388

Kim F. Storm's avatar
Kim F. Storm committed
389 390 391 392 393
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;
394
static unsigned char startup_screen_attrib;
Richard M. Stallman's avatar
Richard M. Stallman committed
395

396 397
static clock_t startup_time;

Kim F. Storm's avatar
Kim F. Storm committed
398
static int term_setup_done;
Richard M. Stallman's avatar
Richard M. Stallman committed
399

400 401
static unsigned short outside_cursor;

Kim F. Storm's avatar
Kim F. Storm committed
402
/* Similar to the_only_frame.  */
403
struct x_output the_only_x_display;
Richard M. Stallman's avatar
Richard M. Stallman committed
404

405 406 407 408 409 410 411 412
/* 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;
413 414
/* A flag to control how to display unibyte 8-bit characters.  */
extern int unibyte_display_via_language_environment;
415

Eli Zaretskii's avatar
Eli Zaretskii committed
416
Lisp_Object Qbar, Qhbar;
417

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

422 423 424 425 426 427 428 429
#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;

430
  if (offset < 0 || count < 0)	/* paranoia; invalid values crash DOS/V */
431 432
    return;

433 434 435 436 437 438 439 440
  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

441
static void
Kim F. Storm's avatar
Kim F. Storm committed
442
dos_direct_output (y, x, buf, len)
Eli Zaretskii's avatar
Eli Zaretskii committed
443
     int x, y;
Kim F. Storm's avatar
Kim F. Storm committed
444 445
     char *buf;
     int len;
Richard M. Stallman's avatar
Richard M. Stallman committed
446
{
447 448
  int t0 = 2 * (x + y * screen_size_X);
  int t = t0 + (int) ScreenPrimary;
449
  int l0 = len;
450 451

#if (__DJGPP__ < 2)
Kim F. Storm's avatar
Kim F. Storm committed
452 453 454 455
  while (--len >= 0) {
    dosmemput (buf++, 1, t);
    t += 2;
  }
456 457 458 459
#else
  /* This is faster.  */
  for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
    _farnspokeb (t, *buf);
460 461 462

  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (t0, l0);
463
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
464
}
465
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
466 467 468

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

Kim F. Storm's avatar
Kim F. Storm committed
469
#if (__DJGPP__ < 2)
470
static void
471
do_visible_bell (xorattr)
Richard M. Stallman's avatar
Richard M. Stallman committed
472 473
     unsigned char xorattr;
{
474
  asm volatile
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    ("  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\
498 499
visible_bell_3:"
     : /* no output */
Kim F. Storm's avatar
Kim F. Storm committed
500
     : "m" (xorattr), "g" (screen_size)
501
     : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
Richard M. Stallman's avatar
Richard M. Stallman committed
502 503
}

Kim F. Storm's avatar
Kim F. Storm committed
504 505 506 507 508 509 510 511 512 513 514 515 516
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

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

519 520 521 522 523 524
/* Enable bright background colors.  */
static void
bright_bg (void)
{
  union REGS regs;

525 526 527 528 529
  /* 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;

530 531 532 533 534
  regs.h.bl = 0;
  regs.x.ax = 0x1003;
  int86 (0x10, &regs, &regs);
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
/* 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);
    }
}

550 551 552 553 554 555 556 557 558 559 560 561 562
/* 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;
}

563 564
/* Set the screen dimensions so that it can show no less than
   ROWS x COLS frame.  */
565

566 567 568 569 570 571
void
dos_set_window_size (rows, cols)
     int *rows, *cols;
{
  char video_name[30];
  union REGS regs;
Eli Zaretskii's avatar
Eli Zaretskii committed
572 573
  Lisp_Object video_mode;
  int video_mode_value, have_vga = 0;
574 575 576 577 578 579
  int current_rows = ScreenRows (), current_cols = ScreenCols ();

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

  mouse_off ();
580
  have_vga = vga_installed ();
581

582
  /* If the user specified a special video mode for these dimensions,
583 584 585 586 587 588 589 590 591 592
     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);
593 594 595 596 597 598 599 600 601

      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);
	}
602 603 604 605 606 607 608 609 610 611
    }

  /* 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
612
	int rows, need_vga;
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
      }	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)
630
	       _set_screen_lines (std_dimension[i].rows);
631 632
	     break;
	   }
633
	 i++;
634 635 636 637 638 639 640 641 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
	}
    }

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

683 684 685 686 687
  /* Update Emacs' notion of screen dimensions.  */
  screen_size_X = *cols;
  screen_size_Y = *rows;
  screen_size = *cols * *rows;

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
#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();
      struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
      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

705 706
  /* Enable bright background colors.  */
  bright_bg ();
707 708 709 710 711

  /* 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);
712 713
}

714 715
/* If we write a character in the position where the mouse is,
   the mouse cursor may need to be refreshed.  */
716 717

static void
Kim F. Storm's avatar
Kim F. Storm committed
718
mouse_off_maybe ()
719
{
Kim F. Storm's avatar
Kim F. Storm committed
720
  int x, y;
Eli Zaretskii's avatar
Eli Zaretskii committed
721

Kim F. Storm's avatar
Kim F. Storm committed
722 723
  if (!mouse_visible)
    return;
Eli Zaretskii's avatar
Eli Zaretskii committed
724

Kim F. Storm's avatar
Kim F. Storm committed
725 726 727
  mouse_get_xy (&x, &y);
  if (y != new_pos_Y || x < new_pos_X)
    return;
Eli Zaretskii's avatar
Eli Zaretskii committed
728

Kim F. Storm's avatar
Kim F. Storm committed
729 730 731
  mouse_off ();
}

732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 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
#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;

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

  /* 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
821
  if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
822 823 824 825
    {
      /* 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
826 827 828
  else if (CONSP (cursor_type)
	   && (EQ (XCAR (cursor_type), Qbar)
	       || EQ (XCAR (cursor_type), Qhbar)))
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
    {
      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
    /* 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);
}

858 859
static void
IT_ring_bell (void)
Kim F. Storm's avatar
Kim F. Storm committed
860 861
{
  if (visible_bell)
862
    {
Kim F. Storm's avatar
Kim F. Storm committed
863 864
      mouse_off ();
      ScreenVisualBell ();
865
    }
Kim F. Storm's avatar
Kim F. Storm committed
866
  else
867 868 869 870 871 872
    {
      union REGS inregs, outregs;
      inregs.h.ah = 2;
      inregs.h.dl = 7;
      intdos (&inregs, &outregs);
    }
873 874
}

875 876 877 878 879
/* 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
880 881 882
static void
IT_set_face (int face)
{
883
  struct frame *sf = SELECTED_FRAME();
884 885 886
  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;
Kim F. Storm's avatar
Kim F. Storm committed
887

888
  if (!fp)
889
    {
890
      fp = dfp;
891 892 893 894 895
      /* The default face for the frame should always be realized and
	 cached.  */
      if (!fp)
	abort ();
    }
Kim F. Storm's avatar
Kim F. Storm committed
896
  screen_face = face;
897 898
  fg = fp->foreground;
  bg = fp->background;
899 900
  dflt_fg = dfp->foreground;
  dflt_bg = dfp->background;
901

902 903 904 905
  /* 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.  */
906
  if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
907
    fg = FRAME_FOREGROUND_PIXEL (sf);
908
  else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
909 910 911
    fg = FRAME_BACKGROUND_PIXEL (sf);
  if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
    bg = FRAME_BACKGROUND_PIXEL (sf);
912
  else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
913 914 915
    bg = FRAME_FOREGROUND_PIXEL (sf);

  /* Make sure highlighted lines really stand out, come what may.  */
916
  if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
917 918 919 920 921 922
    {
      unsigned long tem = fg;

      fg = bg;
      bg = tem;
    }
923 924 925 926 927 928 929 930
  /* If the user requested inverse video, obey.  */
  if (inverse_video)
    {
      unsigned long tem2 = fg;

      fg = bg;
      bg = tem2;
    }
931
  if (termscript)
932 933
    fprintf (termscript, "<FACE %d: %d/%d[FG:%d/BG:%d]>", face,
	     fp->foreground, fp->background, fg, bg);
934 935 936 937 938 939 940 941 942 943
  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
944 945
}

946 947
Lisp_Object Vdos_unsupported_char_glyph;

948
static void
949
IT_write_glyphs (struct glyph *str, int str_len)
Kim F. Storm's avatar
Kim F. Storm committed
950
{
951 952 953
  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);
954
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
955 956 957 958
  register int sl = str_len;
  register int tlen = GLYPH_TABLE_LENGTH;
  register Lisp_Object *tbase = GLYPH_TABLE_BASE;

959 960 961 962 963 964 965
  /* 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 =
    (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
     ? &terminal_coding
     : &safe_terminal_coding);
966
  struct frame *sf;
Morten Welinder's avatar
Morten Welinder committed
967

968 969 970
  /* Do we need to consider conversion of unibyte characters to
     multibyte?  */
  int convert_unibyte_characters
971 972
    = (NILP (current_buffer->enable_multibyte_characters)
       && unibyte_display_via_language_environment);
973

974 975 976
  unsigned char conversion_buffer[256];
  int conversion_buffer_size = sizeof conversion_buffer;

977
  if (str_len <= 0) return;
Eli Zaretskii's avatar
Eli Zaretskii committed
978

979 980
  screen_buf = screen_bp = alloca (str_len * 2);
  screen_buf_end = screen_buf + str_len * 2;
981
  sf = SELECTED_FRAME();
982 983 984 985 986 987 988

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

990 991 992 993 994
  /* 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)
    {
995
      int cf, chlen, enclen;
996
      unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
997
      unsigned ch;
998 999 1000 1001

      /* 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.  */
1002
      if (CHAR_GLYPH_PADDING_P (*str))
1003 1004 1005 1006 1007 1008
	{
	  str++;
	  sl--;
	}
      else
	{
1009 1010 1011
	  register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
	  int glyph_not_in_table = 0;

1012 1013 1014
	  /* If g is negative, it means we have a multibyte character
	     in *str.  That's what GLYPH_FROM_CHAR_GLYPH returns for
	     multibyte characters.  */
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
	  if (g < 0 || g >= tlen)
	    {
	      /* This glyph doesn't have an entry in Vglyph_table.  */
	      ch = str->u.ch;
	      glyph_not_in_table = 1;
	    }
	  else
	    {
	      /* This glyph has an entry in Vglyph_table, so process
		 any aliases before testing for simpleness.  */
	      GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
	      ch = FAST_GLYPH_CHAR (g);
	    }

1029
	  /* Convert the character code to multibyte, if they
1030 1031 1032 1033
	     requested display via language environment.  We only want
	     to convert unibyte characters to multibyte in unibyte
	     buffers!  Otherwise, the 8-bit value in CH came from the
	     display table set up to display foreign characters.  */
1034
	  if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
1035 1036
	      && (ch >= 0240
		  || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
1037 1038 1039
	    ch = unibyte_char_to_multibyte (ch);

	  /* Invalid characters are displayed with a special glyph.  */
1040
	  if (! CHAR_VALID_P (ch, 0))
1041 1042 1043
	    {
	      g = !NILP (Vdos_unsupported_char_glyph)
		? Vdos_unsupported_char_glyph
1044
		: MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
1045 1046 1047 1048 1049
	      ch = FAST_GLYPH_CHAR (g);
	    }

	  /* If the face of this glyph is different from the current
	     screen face, update the screen attribute byte.  */
1050
	  cf = str->face_id;
1051 1052 1053
	  if (cf != screen_face)
	    IT_set_face (cf);	/* handles invalid faces gracefully */

1054
	  if (glyph_not_in_table || GLYPH_SIMPLE_P (tbase, tlen, g))
1055 1056 1057 1058 1059
	    {
	      /* We generate the multi-byte form of CH in WORKBUF.  */
	      chlen = CHAR_STRING (ch, workbuf);
	      buf = workbuf;
	    }
1060 1061 1062 1063 1064 1065 1066
	  else
	    {
	      /* We have a string in Vglyph_table.  */
	      chlen = GLYPH_LENGTH (tbase, g);
	      buf = GLYPH_STRING (tbase, g);
	    }

1067
	  /* If the character is not multibyte, don't bother converting it.  */
1068 1069 1070 1071 1072 1073 1074 1075
	  if (chlen == 1)
	    {
	      *conversion_buffer = (unsigned char)ch;
	      chlen = 0;
	      enclen = 1;
	    }
	  else
	    {
1076
	      coding->src_multibyte = 1;
1077 1078 1079 1080 1081 1082 1083 1084 1085
	      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 == '?')
		{
1086
		  unsigned char *cbp = conversion_buffer;
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

		  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--;
	    }
	}
1144 1145
    }

1146
  /* Dump whatever is left in the screen buffer.  */
Kim F. Storm's avatar
Kim F. Storm committed
1147
  mouse_off_maybe ();
1148
  dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
1149
  if (screen_virtual_segment)
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
    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)