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