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