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