* alloc.c: Do not define struct catchtag.
[bpt/emacs.git] / src / w32console.c
CommitLineData
e6b20d65 1/* Terminal hooks for GNU Emacs on the Microsoft W32 API.
9ec0b715 2 Copyright (C) 1992, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
76b6f707 3 2008, 2009 Free Software Foundation, Inc.
6cdfb6e6 4
3b7ad313
EN
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
3b7ad313 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
3b7ad313
EN
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
6cdfb6e6 19
9ec0b715 20/*
6cdfb6e6
RS
21 Tim Fleehart (apollo@online.com) 1-17-92
22 Geoff Voelker (voelker@cs.washington.edu) 9-12-93
23*/
24
25
6816efce
GV
26#include <config.h>
27
6cdfb6e6
RS
28#include <stdlib.h>
29#include <stdio.h>
6cdfb6e6 30#include <windows.h>
338b1bb2 31#include <string.h>
d7306fe6 32#include <setjmp.h>
6cdfb6e6
RS
33
34#include "lisp.h"
83be827a 35#include "character.h"
8c2ac482 36#include "coding.h"
6cdfb6e6 37#include "disptab.h"
65fb143c 38#include "frame.h"
1c30a66f
JR
39#include "termhooks.h"
40#include "termchar.h"
41#include "dispextern.h"
489f9371 42#include "w32inevt.h"
6cdfb6e6 43
0534d577 44/* from window.c */
6cdfb6e6
RS
45extern Lisp_Object Frecenter ();
46
47/* from keyboard.c */
48extern int detect_input_pending ();
49
50/* from sysdep.c */
51extern int read_input_pending ();
52
1c30a66f
JR
53static void w32con_move_cursor (struct frame *f, int row, int col);
54static void w32con_clear_to_end (struct frame *f);
55static void w32con_clear_frame (struct frame *f);
56static void w32con_clear_end_of_line (struct frame *f, int);
57static void w32con_ins_del_lines (struct frame *f, int vpos, int n);
58static void w32con_insert_glyphs (struct frame *f, struct glyph *start, int len);
59static void w32con_write_glyphs (struct frame *f, struct glyph *string, int len);
60static void w32con_delete_glyphs (struct frame *f, int n);
61static void w32con_reset_terminal_modes (struct terminal *t);
62static void w32con_set_terminal_modes (struct terminal *t);
63static void w32con_set_terminal_window (struct frame *f, int size);
7cb010ed
JR
64static void w32con_update_begin (struct frame * f);
65static void w32con_update_end (struct frame * f);
338b1bb2 66static WORD w32_face_attributes (struct frame *f, int face_id);
6cdfb6e6 67
338b1bb2
JR
68static COORD cursor_coords;
69static HANDLE prev_screen, cur_screen;
70static WORD char_attr_normal;
71static DWORD prev_console_mode;
6cdfb6e6 72
3e671d90 73#ifndef USE_SEPARATE_SCREEN
338b1bb2 74static CONSOLE_CURSOR_INFO prev_console_cursor;
3e671d90
GV
75#endif
76
b510360c
JR
77extern Lisp_Object Vtty_defined_color_alist;
78
191100f2
AI
79/* Determine whether to make frame dimensions match the screen buffer,
80 or the current window size. The former is desirable when running
81 over telnet, while the latter is more useful when working directly at
82 the console with a large scroll-back buffer. */
83int w32_use_full_screen_buffer;
338b1bb2 84HANDLE keyboard_handle;
191100f2 85
6cdfb6e6
RS
86
87/* Setting this as the ctrl handler prevents emacs from being killed when
40d578c9
RS
88 someone hits ^C in a 'suspended' session (child shell).
89 Also ignore Ctrl-Break signals. */
90
6cdfb6e6
RS
91BOOL
92ctrl_c_handler (unsigned long type)
93{
3e671d90
GV
94 /* Only ignore "interrupt" events when running interactively. */
95 return (!noninteractive
96 && (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT));
6cdfb6e6
RS
97}
98
6cdfb6e6 99
1c30a66f 100/* Move the cursor to (ROW, COL) on FRAME. */
4fdbdbbd 101static void
1c30a66f 102w32con_move_cursor (struct frame *f, int row, int col)
6cdfb6e6
RS
103{
104 cursor_coords.X = col;
105 cursor_coords.Y = row;
177c0ea7 106
1c30a66f
JR
107 /* TODO: for multi-tty support, cur_screen should be replaced with a
108 reference to the terminal for this frame. */
109 SetConsoleCursorPosition (cur_screen, cursor_coords);
6cdfb6e6
RS
110}
111
112/* Clear from cursor to end of screen. */
4fdbdbbd 113static void
1c30a66f 114w32con_clear_to_end (struct frame *f)
6cdfb6e6 115{
1c30a66f
JR
116 w32con_clear_end_of_line (f, FRAME_COLS (f) - 1);
117 w32con_ins_del_lines (f, cursor_coords.Y, FRAME_LINES (f) - cursor_coords.Y - 1);
6cdfb6e6
RS
118}
119
120/* Clear the frame. */
7cb010ed 121static void
1c30a66f 122w32con_clear_frame (struct frame *f)
6cdfb6e6 123{
b47278e1 124 COORD dest;
84f5bd81
AI
125 int n;
126 DWORD r;
a5404e3a
AI
127 CONSOLE_SCREEN_BUFFER_INFO info;
128
129 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info);
b47278e1 130
a5404e3a 131 /* Remember that the screen buffer might be wider than the window. */
90022f5a 132 n = FRAME_LINES (f) * info.dwSize.X;
b47278e1
GV
133 dest.X = dest.Y = 0;
134
338b1bb2 135 FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r);
b47278e1
GV
136 FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r);
137
1c30a66f 138 w32con_move_cursor (f, 0, 0);
6cdfb6e6
RS
139}
140
141
8c2ac482 142static struct glyph glyph_base[256];
6cdfb6e6
RS
143static BOOL ceol_initialized = FALSE;
144
145/* Clear from Cursor to end (what's "standout marker"?). */
7cb010ed 146static void
1c30a66f 147w32con_clear_end_of_line (struct frame *f, int end)
6cdfb6e6
RS
148{
149 if (!ceol_initialized)
150 {
151 int i;
152 for (i = 0; i < 256; i++)
153 {
8c2ac482 154 memcpy (&glyph_base[i], &space_glyph, sizeof (struct glyph));
6cdfb6e6
RS
155 }
156 ceol_initialized = TRUE;
157 }
1c30a66f 158 w32con_write_glyphs (f, glyph_base, end - cursor_coords.X); /* fencepost ? */
6cdfb6e6
RS
159}
160
161/* Insert n lines at vpos. if n is negative delete -n lines. */
7cb010ed 162static void
1c30a66f 163w32con_ins_del_lines (struct frame *f, int vpos, int n)
6cdfb6e6 164{
e850a615 165 int i, nb;
6cdfb6e6 166 SMALL_RECT scroll;
09320d30 167 SMALL_RECT clip;
6cdfb6e6
RS
168 COORD dest;
169 CHAR_INFO fill;
6cdfb6e6
RS
170
171 if (n < 0)
172 {
173 scroll.Top = vpos - n;
90022f5a 174 scroll.Bottom = FRAME_LINES (f);
6cdfb6e6
RS
175 dest.Y = vpos;
176 }
177 else
178 {
179 scroll.Top = vpos;
90022f5a 180 scroll.Bottom = FRAME_LINES (f) - n;
6cdfb6e6
RS
181 dest.Y = vpos + n;
182 }
09320d30
JR
183 clip.Top = clip.Left = scroll.Left = 0;
184 clip.Right = scroll.Right = FRAME_COLS (f);
185 clip.Bottom = FRAME_LINES (f);
177c0ea7 186
6cdfb6e6 187 dest.X = 0;
177c0ea7 188
6cdfb6e6 189 fill.Char.AsciiChar = 0x20;
338b1bb2 190 fill.Attributes = char_attr_normal;
177c0ea7 191
09320d30 192 ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill);
6cdfb6e6 193
fbd6baed 194 /* Here we have to deal with a w32 console flake: If the scroll
6cdfb6e6
RS
195 region looks like abc and we scroll c to a and fill with d we get
196 cbd... if we scroll block c one line at a time to a, we get cdd...
197 Emacs expects cdd consistently... So we have to deal with that
198 here... (this also occurs scrolling the same way in the other
199 direction. */
200
201 if (n > 0)
202 {
203 if (scroll.Bottom < dest.Y)
204 {
205 for (i = scroll.Bottom; i < dest.Y; i++)
206 {
1c30a66f
JR
207 w32con_move_cursor (f, i, 0);
208 w32con_clear_end_of_line (f, FRAME_COLS (f));
6cdfb6e6
RS
209 }
210 }
211 }
212 else
213 {
214 nb = dest.Y + (scroll.Bottom - scroll.Top) + 1;
215
216 if (nb < scroll.Top)
177c0ea7 217 {
6cdfb6e6
RS
218 for (i = nb; i < scroll.Top; i++)
219 {
1c30a66f
JR
220 w32con_move_cursor (f, i, 0);
221 w32con_clear_end_of_line (f, FRAME_COLS (f));
6cdfb6e6
RS
222 }
223 }
224 }
177c0ea7 225
6cdfb6e6
RS
226 cursor_coords.X = 0;
227 cursor_coords.Y = vpos;
6cdfb6e6
RS
228}
229
230#undef LEFT
231#undef RIGHT
232#define LEFT 1
233#define RIGHT 0
234
4fdbdbbd 235static void
1c30a66f 236scroll_line (struct frame *f, int dist, int direction)
6cdfb6e6
RS
237{
238 /* The idea here is to implement a horizontal scroll in one line to
239 implement delete and half of insert. */
09320d30 240 SMALL_RECT scroll, clip;
6cdfb6e6
RS
241 COORD dest;
242 CHAR_INFO fill;
177c0ea7 243
09320d30
JR
244 clip.Top = scroll.Top = clip.Bottom = scroll.Bottom = cursor_coords.Y;
245 clip.Left = 0;
246 clip.Right = FRAME_COLS (f);
177c0ea7 247
6cdfb6e6
RS
248 if (direction == LEFT)
249 {
250 scroll.Left = cursor_coords.X + dist;
90022f5a 251 scroll.Right = FRAME_COLS (f) - 1;
6cdfb6e6
RS
252 }
253 else
254 {
255 scroll.Left = cursor_coords.X;
90022f5a 256 scroll.Right = FRAME_COLS (f) - dist - 1;
6cdfb6e6 257 }
177c0ea7 258
6cdfb6e6
RS
259 dest.X = cursor_coords.X;
260 dest.Y = cursor_coords.Y;
177c0ea7 261
6cdfb6e6 262 fill.Char.AsciiChar = 0x20;
338b1bb2
JR
263 fill.Attributes = char_attr_normal;
264
09320d30 265 ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill);
6cdfb6e6
RS
266}
267
268
269/* If start is zero insert blanks instead of a string at start ?. */
7cb010ed 270static void
1c30a66f 271w32con_insert_glyphs (struct frame *f, register struct glyph *start, register int len)
6cdfb6e6 272{
1c30a66f 273 scroll_line (f, len, RIGHT);
6cdfb6e6
RS
274
275 /* Move len chars to the right starting at cursor_coords, fill with blanks */
276 if (start)
277 {
278 /* Print the first len characters of start, cursor_coords.X adjusted
279 by write_glyphs. */
177c0ea7 280
1c30a66f 281 w32con_write_glyphs (f, start, len);
6cdfb6e6
RS
282 }
283 else
284 {
1c30a66f 285 w32con_clear_end_of_line (f, cursor_coords.X + len);
6cdfb6e6
RS
286 }
287}
288
853895f6 289extern unsigned char *encode_terminal_code P_ ((struct glyph *, int,
29e1d29d 290 struct coding_system *));
853895f6 291
7cb010ed 292static void
1c30a66f
JR
293w32con_write_glyphs (struct frame *f, register struct glyph *string,
294 register int len)
6cdfb6e6 295{
84f5bd81 296 DWORD r;
338b1bb2 297 WORD char_attr;
aee31bf1
KH
298 unsigned char *conversion_buffer;
299 struct coding_system *coding;
338b1bb2
JR
300
301 if (len <= 0)
302 return;
b47278e1 303
aee31bf1
KH
304 /* If terminal_coding does any conversion, use it, otherwise use
305 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
306 because it always return 1 if the member src_multibyte is 1. */
1c30a66f
JR
307 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
308 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
8c2ac482
JR
309 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
310 the tail. */
1c30a66f 311 coding->mode &= ~CODING_MODE_LAST_BLOCK;
6cdfb6e6 312
8c2ac482 313 while (len > 0)
6cdfb6e6 314 {
8c2ac482
JR
315 /* Identify a run of glyphs with the same face. */
316 int face_id = string->face_id;
317 int n;
177c0ea7 318
8c2ac482
JR
319 for (n = 1; n < len; ++n)
320 if (string[n].face_id != face_id)
321 break;
322
323 /* Turn appearance modes of the face of the run on. */
338b1bb2 324 char_attr = w32_face_attributes (f, face_id);
8c2ac482 325
aee31bf1
KH
326 if (n == len)
327 /* This is the last run. */
328 coding->mode |= CODING_MODE_LAST_BLOCK;
329 conversion_buffer = encode_terminal_code (string, n, coding);
330 if (coding->produced > 0)
331 {
332 /* Set the attribute for these characters. */
333 if (!FillConsoleOutputAttribute (cur_screen, char_attr,
334 coding->produced, cursor_coords,
335 &r))
8c2ac482 336 {
aee31bf1
KH
337 printf ("Failed writing console attributes: %d\n",
338 GetLastError ());
339 fflush (stdout);
340 }
341
342 /* Write the characters. */
56cc8ca9 343 if (!WriteConsoleOutputCharacter (cur_screen, conversion_buffer,
aee31bf1
KH
344 coding->produced, cursor_coords,
345 &r))
346 {
347 printf ("Failed writing console characters: %d\n",
348 GetLastError ());
349 fflush (stdout);
350 }
351
352 cursor_coords.X += coding->produced;
1c30a66f 353 w32con_move_cursor (f, cursor_coords.Y, cursor_coords.X);
aee31bf1
KH
354 }
355 len -= n;
356 string += n;
6cdfb6e6 357 }
6cdfb6e6
RS
358}
359
8c2ac482 360
7cb010ed 361static void
1c30a66f 362w32con_delete_glyphs (struct frame *f, int n)
6cdfb6e6 363{
177c0ea7
JB
364 /* delete chars means scroll chars from cursor_coords.X + n to
365 cursor_coords.X, anything beyond the edge of the screen should
6cdfb6e6
RS
366 come out empty... */
367
1c30a66f 368 scroll_line (f, n, LEFT);
6cdfb6e6
RS
369}
370
0534d577 371static unsigned int sound_type = 0xFFFFFFFF;
78859b80 372#define MB_EMACS_SILENT (0xFFFFFFFF - 1)
0534d577 373
6cdfb6e6 374void
1c30a66f 375w32_sys_ring_bell (struct frame *f)
6cdfb6e6 376{
177c0ea7 377 if (sound_type == 0xFFFFFFFF)
78859b80 378 {
0534d577 379 Beep (666, 100);
78859b80
GV
380 }
381 else if (sound_type == MB_EMACS_SILENT)
382 {
383 /* Do nothing. */
384 }
0534d577 385 else
78859b80 386 MessageBeep (sound_type);
6cdfb6e6
RS
387}
388
0534d577 389DEFUN ("set-message-beep", Fset_message_beep, Sset_message_beep, 1, 1, 0,
33f09670
JR
390 doc: /* Set the sound generated when the bell is rung.
391SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
392to use the corresponding system sound for the bell. The 'silent sound
393prevents Emacs from making any sound at all.
394SOUND is nil to use the normal beep. */)
0534d577
KH
395 (sound)
396 Lisp_Object sound;
6cdfb6e6 397{
b7826503 398 CHECK_SYMBOL (sound);
0534d577 399
177c0ea7 400 if (NILP (sound))
0534d577
KH
401 sound_type = 0xFFFFFFFF;
402 else if (EQ (sound, intern ("asterisk")))
403 sound_type = MB_ICONASTERISK;
177c0ea7 404 else if (EQ (sound, intern ("exclamation")))
0534d577 405 sound_type = MB_ICONEXCLAMATION;
177c0ea7 406 else if (EQ (sound, intern ("hand")))
0534d577 407 sound_type = MB_ICONHAND;
177c0ea7 408 else if (EQ (sound, intern ("question")))
0534d577 409 sound_type = MB_ICONQUESTION;
177c0ea7 410 else if (EQ (sound, intern ("ok")))
0534d577 411 sound_type = MB_OK;
78859b80
GV
412 else if (EQ (sound, intern ("silent")))
413 sound_type = MB_EMACS_SILENT;
0534d577
KH
414 else
415 sound_type = 0xFFFFFFFF;
416
417 return sound;
6cdfb6e6 418}
177c0ea7 419
7cb010ed 420static void
1c30a66f 421w32con_reset_terminal_modes (struct terminal *t)
6cdfb6e6 422{
09320d30
JR
423 COORD dest;
424 CONSOLE_SCREEN_BUFFER_INFO info;
425 int n;
426 DWORD r;
427
428 /* Clear the complete screen buffer. This is required because Emacs
429 sets the cursor position to the top of the buffer, but there might
430 be other output below the bottom of the Emacs frame if the screen buffer
431 is larger than the window size. */
432 GetConsoleScreenBufferInfo (cur_screen, &info);
433 dest.X = 0;
434 dest.Y = 0;
435 n = info.dwSize.X * info.dwSize.Y;
436
437 FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r);
438 FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r);
439 /* Now that the screen is clear, put the cursor at the top. */
440 SetConsoleCursorPosition (cur_screen, dest);
441
3e671d90 442#ifdef USE_SEPARATE_SCREEN
6cdfb6e6 443 SetConsoleActiveScreenBuffer (prev_screen);
3e671d90
GV
444#else
445 SetConsoleCursorInfo (prev_screen, &prev_console_cursor);
446#endif
09320d30 447
3e671d90 448 SetConsoleMode (keyboard_handle, prev_console_mode);
6cdfb6e6
RS
449}
450
7cb010ed 451static void
1c30a66f 452w32con_set_terminal_modes (struct terminal *t)
6cdfb6e6
RS
453{
454 CONSOLE_CURSOR_INFO cci;
455
3e671d90
GV
456 /* make cursor big and visible (100 on Win95 makes it disappear) */
457 cci.dwSize = 99;
458 cci.bVisible = TRUE;
459 (void) SetConsoleCursorInfo (cur_screen, &cci);
6cdfb6e6 460
3e671d90 461 SetConsoleActiveScreenBuffer (cur_screen);
6cdfb6e6 462
3e671d90 463 SetConsoleMode (keyboard_handle, ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
6cdfb6e6 464
3e671d90
GV
465 /* Initialize input mode: interrupt_input off, no flow control, allow
466 8 bit character input, standard quit char. */
467 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
6cdfb6e6
RS
468}
469
470/* hmmm... perhaps these let us bracket screen changes so that we can flush
471 clumps rather than one-character-at-a-time...
177c0ea7 472
6cdfb6e6 473 we'll start with not moving the cursor while an update is in progress. */
7cb010ed
JR
474static void
475w32con_update_begin (struct frame * f)
6cdfb6e6
RS
476{
477}
478
7cb010ed
JR
479static void
480w32con_update_end (struct frame * f)
6cdfb6e6
RS
481{
482 SetConsoleCursorPosition (cur_screen, cursor_coords);
483}
484
7cb010ed 485static void
1c30a66f 486w32con_set_terminal_window (struct frame *f, int size)
6cdfb6e6
RS
487{
488}
489
8c2ac482
JR
490/***********************************************************************
491 Faces
492 ***********************************************************************/
493
494
495/* Turn appearances of face FACE_ID on tty frame F on. */
496
338b1bb2
JR
497static WORD
498w32_face_attributes (f, face_id)
8c2ac482
JR
499 struct frame *f;
500 int face_id;
501{
338b1bb2 502 WORD char_attr;
8c2ac482
JR
503 struct face *face = FACE_FROM_ID (f, face_id);
504
505 xassert (face != NULL);
506
507 char_attr = char_attr_normal;
508
f442d348
JR
509 /* Reverse the default color if requested. If background and
510 foreground are specified, then they have been reversed already. */
e850a615 511 if (face->tty_reverse_p)
338b1bb2
JR
512 char_attr = (char_attr & 0xff00) + ((char_attr & 0x000f) << 4)
513 + ((char_attr & 0x00f0) >> 4);
8c2ac482 514
b510360c 515 /* Before the terminal is properly initialized, all colors map to 0.
f442d348 516 Don't try to resolve them. */
b510360c 517 if (NILP (Vtty_defined_color_alist))
f442d348 518 return char_attr;
8c2ac482 519
30b7f3e6
JR
520 /* Colors should be in the range 0...15 unless they are one of
521 FACE_TTY_DEFAULT_COLOR, FACE_TTY_DEFAULT_FG_COLOR or
522 FACE_TTY_DEFAULT_BG_COLOR. Other out of range colors are
523 invalid, so it is better to use the default color if they ever
524 get through to here. */
525 if (face->foreground >= 0 && face->foreground < 16)
f442d348 526 char_attr = (char_attr & 0xfff0) + face->foreground;
8c2ac482 527
30b7f3e6 528 if (face->background >= 0 && face->background < 16)
f442d348 529 char_attr = (char_attr & 0xff0f) + (face->background << 4);
8c2ac482 530
338b1bb2 531 return char_attr;
8c2ac482 532}
8c2ac482 533
338b1bb2
JR
534
535
536/* Given a color index, return its standard name. */
537Lisp_Object
538vga_stdcolor_name (int idx)
8c2ac482 539{
338b1bb2
JR
540 /* Standard VGA colors, in the order of their standard numbering
541 in the default VGA palette. */
542 static char *vga_colors[16] = {
543 "black", "blue", "green", "cyan", "red", "magenta", "brown",
544 "lightgray", "darkgray", "lightblue", "lightgreen", "lightcyan",
545 "lightred", "lightmagenta", "yellow", "white"
546 };
547
548 extern Lisp_Object Qunspecified;
549
550 if (idx >= 0 && idx < sizeof (vga_colors) / sizeof (vga_colors[0]))
551 return build_string (vga_colors[idx]);
552 else
553 return Qunspecified; /* meaning the default */
8c2ac482 554}
338b1bb2 555
6cdfb6e6
RS
556typedef int (*term_hook) ();
557
558void
79af260e 559initialize_w32_display (struct terminal *term)
6cdfb6e6
RS
560{
561 CONSOLE_SCREEN_BUFFER_INFO info;
177c0ea7 562
9b8f0fc3 563 term->rif = 0; /* No window based redisplay on the console. */
1c30a66f
JR
564 term->cursor_to_hook = w32con_move_cursor;
565 term->raw_cursor_to_hook = w32con_move_cursor;
566 term->clear_to_end_hook = w32con_clear_to_end;
567 term->clear_frame_hook = w32con_clear_frame;
568 term->clear_end_of_line_hook = w32con_clear_end_of_line;
569 term->ins_del_lines_hook = w32con_ins_del_lines;
570 term->insert_glyphs_hook = w32con_insert_glyphs;
571 term->write_glyphs_hook = w32con_write_glyphs;
572 term->delete_glyphs_hook = w32con_delete_glyphs;
573 term->ring_bell_hook = w32_sys_ring_bell;
574 term->reset_terminal_modes_hook = w32con_reset_terminal_modes;
575 term->set_terminal_modes_hook = w32con_set_terminal_modes;
576 term->set_terminal_window_hook = w32con_set_terminal_window;
577 term->update_begin_hook = w32con_update_begin;
578 term->update_end_hook = w32con_update_end;
579
580 term->read_socket_hook = w32_console_read_socket;
581 term->mouse_position_hook = w32_console_mouse_position;
3e671d90 582
9b8f0fc3
JR
583 /* The following are not used on the console. */
584 term->frame_rehighlight_hook = 0;
585 term->frame_raise_lower_hook = 0;
586 term->set_vertical_scroll_bar_hook = 0;
587 term->condemn_scroll_bars_hook = 0;
588 term->redeem_scroll_bar_hook = 0;
589 term->judge_scroll_bars_hook = 0;
590 term->frame_up_to_date_hook = 0;
3e671d90 591
c1e06681
AI
592 /* Initialize interrupt_handle. */
593 init_crit ();
594
3e671d90
GV
595 /* Remember original console settings. */
596 keyboard_handle = GetStdHandle (STD_INPUT_HANDLE);
597 GetConsoleMode (keyboard_handle, &prev_console_mode);
598
6cdfb6e6 599 prev_screen = GetStdHandle (STD_OUTPUT_HANDLE);
177c0ea7 600
3e671d90
GV
601#ifdef USE_SEPARATE_SCREEN
602 cur_screen = CreateConsoleScreenBuffer (GENERIC_READ | GENERIC_WRITE,
603 0, NULL,
604 CONSOLE_TEXTMODE_BUFFER,
605 NULL);
606
607 if (cur_screen == INVALID_HANDLE_VALUE)
608 {
609 printf ("CreateConsoleScreenBuffer failed in ResetTerm\n");
610 printf ("LastError = 0x%lx\n", GetLastError ());
611 fflush (stdout);
612 exit (0);
613 }
614#else
615 cur_screen = prev_screen;
616 GetConsoleCursorInfo (prev_screen, &prev_console_cursor);
617#endif
618
191100f2
AI
619 /* Respect setting of LINES and COLUMNS environment variables. */
620 {
621 char * lines = getenv("LINES");
622 char * columns = getenv("COLUMNS");
623
624 if (lines != NULL && columns != NULL)
625 {
626 SMALL_RECT new_win_dims;
627 COORD new_size;
628
629 new_size.X = atoi (columns);
630 new_size.Y = atoi (lines);
631
632 GetConsoleScreenBufferInfo (cur_screen, &info);
633
634 /* Shrink the window first, so the buffer dimensions can be
635 reduced if necessary. */
636 new_win_dims.Top = 0;
637 new_win_dims.Left = 0;
638 new_win_dims.Bottom = min (new_size.Y, info.dwSize.Y) - 1;
639 new_win_dims.Right = min (new_size.X, info.dwSize.X) - 1;
640 SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
641
642 SetConsoleScreenBufferSize (cur_screen, new_size);
643
644 /* Set the window size to match the buffer dimension. */
645 new_win_dims.Top = 0;
646 new_win_dims.Left = 0;
647 new_win_dims.Bottom = new_size.Y - 1;
648 new_win_dims.Right = new_size.X - 1;
649 SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
650 }
651 }
652
6cdfb6e6 653 GetConsoleScreenBufferInfo (cur_screen, &info);
177c0ea7 654
338b1bb2 655 char_attr_normal = info.wAttributes;
938469f2 656
3fb1d72b
JR
657 /* Determine if the info returned by GetConsoleScreenBufferInfo
658 is realistic. Old MS Telnet servers used to only fill out
659 the dwSize portion, even modern one fill the whole struct with
660 garbage when using non-MS telnet clients. */
661 if ((w32_use_full_screen_buffer
662 && (info.dwSize.Y < 20 || info.dwSize.Y > 100
663 || info.dwSize.X < 40 || info.dwSize.X > 200))
664 || (!w32_use_full_screen_buffer
665 && (info.srWindow.Bottom - info.srWindow.Top < 20
666 || info.srWindow.Bottom - info.srWindow.Top > 100
667 || info.srWindow.Right - info.srWindow.Left < 40
668 || info.srWindow.Right - info.srWindow.Left > 100)))
669 {
670 FRAME_LINES (SELECTED_FRAME ()) = 25;
671 SET_FRAME_COLS (SELECTED_FRAME (), 80);
672 }
673
674 else if (w32_use_full_screen_buffer)
191100f2 675 {
90022f5a
KS
676 FRAME_LINES (SELECTED_FRAME ()) = info.dwSize.Y; /* lines per page */
677 SET_FRAME_COLS (SELECTED_FRAME (), info.dwSize.X); /* characters per line */
191100f2
AI
678 }
679 else
680 {
681 /* Lines per page. Use buffer coords instead of buffer size. */
90022f5a 682 FRAME_LINES (SELECTED_FRAME ()) = 1 + info.srWindow.Bottom -
177c0ea7 683 info.srWindow.Top;
191100f2 684 /* Characters per line. Use buffer coords instead of buffer size. */
90022f5a 685 SET_FRAME_COLS (SELECTED_FRAME (), 1 + info.srWindow.Right -
191100f2
AI
686 info.srWindow.Left);
687 }
65fb143c
JR
688
689 /* Setup w32_display_info structure for this frame. */
690
691 w32_initialize_display_info (build_string ("Console"));
692
6cdfb6e6
RS
693}
694
1c30a66f 695
6cdfb6e6 696DEFUN ("set-screen-color", Fset_screen_color, Sset_screen_color, 2, 2, 0,
33f09670 697 doc: /* Set screen colors. */)
6cdfb6e6
RS
698 (foreground, background)
699 Lisp_Object foreground;
700 Lisp_Object background;
701{
702 char_attr_normal = XFASTINT (foreground) + (XFASTINT (background) << 4);
6cdfb6e6
RS
703
704 Frecenter (Qnil);
705 return Qt;
706}
707
708DEFUN ("set-cursor-size", Fset_cursor_size, Sset_cursor_size, 1, 1, 0,
33f09670 709 doc: /* Set cursor size. */)
6cdfb6e6
RS
710 (size)
711 Lisp_Object size;
712{
713 CONSOLE_CURSOR_INFO cci;
714 cci.dwSize = XFASTINT (size);
715 cci.bVisible = TRUE;
716 (void) SetConsoleCursorInfo (cur_screen, &cci);
177c0ea7 717
6cdfb6e6
RS
718 return Qt;
719}
720
0534d577 721void
6cdfb6e6
RS
722syms_of_ntterm ()
723{
191100f2
AI
724 DEFVAR_BOOL ("w32-use-full-screen-buffer",
725 &w32_use_full_screen_buffer,
33f09670 726 doc: /* Non-nil means make terminal frames use the full screen buffer dimensions.
639804b3 727This is desirable when running Emacs over telnet.
33f09670
JR
728A value of nil means use the current console window dimensions; this
729may be preferrable when working directly at the console with a large
730scroll-back buffer. */);
3fb1d72b 731 w32_use_full_screen_buffer = 0;
191100f2 732
6cdfb6e6
RS
733 defsubr (&Sset_screen_color);
734 defsubr (&Sset_cursor_size);
0534d577 735 defsubr (&Sset_message_beep);
6cdfb6e6 736}
6b61353c
KH
737
738/* arch-tag: a390a07f-f661-42bc-aeb4-e6d8bf860337
739 (do not change this comment) */