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