| 1 | /* Terminal hooks for GNU Emacs on the Microsoft Windows API. |
| 2 | Copyright (C) 1992, 1999, 2001-2014 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs is free software: you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation, either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | GNU Emacs is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* |
| 20 | Tim Fleehart (apollo@online.com) 1-17-92 |
| 21 | Geoff Voelker (voelker@cs.washington.edu) 9-12-93 |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <config.h> |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <windows.h> |
| 29 | |
| 30 | #include "lisp.h" |
| 31 | #include "character.h" |
| 32 | #include "coding.h" |
| 33 | #include "disptab.h" |
| 34 | #include "frame.h" |
| 35 | #include "window.h" |
| 36 | #include "termhooks.h" |
| 37 | #include "termchar.h" |
| 38 | #include "dispextern.h" |
| 39 | #include "menu.h" /* for tty_menu_show */ |
| 40 | #include "w32term.h" |
| 41 | #include "w32common.h" /* for os_subtype */ |
| 42 | #include "w32inevt.h" |
| 43 | |
| 44 | /* from window.c */ |
| 45 | extern Lisp_Object Frecenter (Lisp_Object); |
| 46 | |
| 47 | static void w32con_move_cursor (struct frame *f, int row, int col); |
| 48 | static void w32con_clear_to_end (struct frame *f); |
| 49 | static void w32con_clear_frame (struct frame *f); |
| 50 | static void w32con_clear_end_of_line (struct frame *f, int); |
| 51 | static void w32con_ins_del_lines (struct frame *f, int vpos, int n); |
| 52 | static void w32con_insert_glyphs (struct frame *f, struct glyph *start, int len); |
| 53 | static void w32con_write_glyphs (struct frame *f, struct glyph *string, int len); |
| 54 | static void w32con_delete_glyphs (struct frame *f, int n); |
| 55 | static void w32con_reset_terminal_modes (struct terminal *t); |
| 56 | static void w32con_set_terminal_modes (struct terminal *t); |
| 57 | static void w32con_update_begin (struct frame * f); |
| 58 | static void w32con_update_end (struct frame * f); |
| 59 | static WORD w32_face_attributes (struct frame *f, int face_id); |
| 60 | |
| 61 | static COORD cursor_coords; |
| 62 | static HANDLE prev_screen, cur_screen; |
| 63 | static WORD char_attr_normal; |
| 64 | static DWORD prev_console_mode; |
| 65 | |
| 66 | static CONSOLE_CURSOR_INFO console_cursor_info; |
| 67 | #ifndef USE_SEPARATE_SCREEN |
| 68 | static CONSOLE_CURSOR_INFO prev_console_cursor; |
| 69 | #endif |
| 70 | |
| 71 | HANDLE keyboard_handle; |
| 72 | int w32_console_unicode_input; |
| 73 | |
| 74 | |
| 75 | /* Setting this as the ctrl handler prevents emacs from being killed when |
| 76 | someone hits ^C in a 'suspended' session (child shell). |
| 77 | Also ignore Ctrl-Break signals. */ |
| 78 | |
| 79 | BOOL |
| 80 | ctrl_c_handler (unsigned long type) |
| 81 | { |
| 82 | /* Only ignore "interrupt" events when running interactively. */ |
| 83 | return (!noninteractive |
| 84 | && (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT)); |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /* Move the cursor to (ROW, COL) on FRAME. */ |
| 89 | static void |
| 90 | w32con_move_cursor (struct frame *f, int row, int col) |
| 91 | { |
| 92 | cursor_coords.X = col; |
| 93 | cursor_coords.Y = row; |
| 94 | |
| 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); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | w32con_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 | |
| 108 | void |
| 109 | w32con_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 | |
| 116 | /* Clear from cursor to end of screen. */ |
| 117 | static void |
| 118 | w32con_clear_to_end (struct frame *f) |
| 119 | { |
| 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); |
| 122 | } |
| 123 | |
| 124 | /* Clear the frame. */ |
| 125 | static void |
| 126 | w32con_clear_frame (struct frame *f) |
| 127 | { |
| 128 | COORD dest; |
| 129 | int n; |
| 130 | DWORD r; |
| 131 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 132 | |
| 133 | GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info); |
| 134 | |
| 135 | /* Remember that the screen buffer might be wider than the window. */ |
| 136 | n = FRAME_LINES (f) * info.dwSize.X; |
| 137 | dest.X = dest.Y = 0; |
| 138 | |
| 139 | FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r); |
| 140 | FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r); |
| 141 | |
| 142 | w32con_move_cursor (f, 0, 0); |
| 143 | } |
| 144 | |
| 145 | |
| 146 | static struct glyph glyph_base[256]; |
| 147 | static BOOL ceol_initialized = FALSE; |
| 148 | |
| 149 | /* Clear from Cursor to end (what's "standout marker"?). */ |
| 150 | static void |
| 151 | w32con_clear_end_of_line (struct frame *f, int end) |
| 152 | { |
| 153 | if (!ceol_initialized) |
| 154 | { |
| 155 | int i; |
| 156 | for (i = 0; i < 256; i++) |
| 157 | { |
| 158 | memcpy (&glyph_base[i], &space_glyph, sizeof (struct glyph)); |
| 159 | } |
| 160 | ceol_initialized = TRUE; |
| 161 | } |
| 162 | w32con_write_glyphs (f, glyph_base, end - cursor_coords.X); /* fencepost ? */ |
| 163 | } |
| 164 | |
| 165 | /* Insert n lines at vpos. if n is negative delete -n lines. */ |
| 166 | static void |
| 167 | w32con_ins_del_lines (struct frame *f, int vpos, int n) |
| 168 | { |
| 169 | int i, nb; |
| 170 | SMALL_RECT scroll; |
| 171 | SMALL_RECT clip; |
| 172 | COORD dest; |
| 173 | CHAR_INFO fill; |
| 174 | |
| 175 | if (n < 0) |
| 176 | { |
| 177 | scroll.Top = vpos - n; |
| 178 | scroll.Bottom = FRAME_LINES (f); |
| 179 | dest.Y = vpos; |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | scroll.Top = vpos; |
| 184 | scroll.Bottom = FRAME_LINES (f) - n; |
| 185 | dest.Y = vpos + n; |
| 186 | } |
| 187 | clip.Top = clip.Left = scroll.Left = 0; |
| 188 | clip.Right = scroll.Right = FRAME_COLS (f); |
| 189 | clip.Bottom = FRAME_LINES (f); |
| 190 | |
| 191 | dest.X = 0; |
| 192 | |
| 193 | fill.Char.AsciiChar = 0x20; |
| 194 | fill.Attributes = char_attr_normal; |
| 195 | |
| 196 | ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill); |
| 197 | |
| 198 | /* Here we have to deal with a w32 console flake: If the scroll |
| 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 | { |
| 211 | w32con_move_cursor (f, i, 0); |
| 212 | w32con_clear_end_of_line (f, FRAME_COLS (f)); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | else |
| 217 | { |
| 218 | nb = dest.Y + (scroll.Bottom - scroll.Top) + 1; |
| 219 | |
| 220 | if (nb < scroll.Top) |
| 221 | { |
| 222 | for (i = nb; i < scroll.Top; i++) |
| 223 | { |
| 224 | w32con_move_cursor (f, i, 0); |
| 225 | w32con_clear_end_of_line (f, FRAME_COLS (f)); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | cursor_coords.X = 0; |
| 231 | cursor_coords.Y = vpos; |
| 232 | } |
| 233 | |
| 234 | #undef LEFT |
| 235 | #undef RIGHT |
| 236 | #define LEFT 1 |
| 237 | #define RIGHT 0 |
| 238 | |
| 239 | static void |
| 240 | scroll_line (struct frame *f, int dist, int direction) |
| 241 | { |
| 242 | /* The idea here is to implement a horizontal scroll in one line to |
| 243 | implement delete and half of insert. */ |
| 244 | SMALL_RECT scroll, clip; |
| 245 | COORD dest; |
| 246 | CHAR_INFO fill; |
| 247 | |
| 248 | clip.Top = scroll.Top = clip.Bottom = scroll.Bottom = cursor_coords.Y; |
| 249 | clip.Left = 0; |
| 250 | clip.Right = FRAME_COLS (f); |
| 251 | |
| 252 | if (direction == LEFT) |
| 253 | { |
| 254 | scroll.Left = cursor_coords.X + dist; |
| 255 | scroll.Right = FRAME_COLS (f) - 1; |
| 256 | } |
| 257 | else |
| 258 | { |
| 259 | scroll.Left = cursor_coords.X; |
| 260 | scroll.Right = FRAME_COLS (f) - dist - 1; |
| 261 | } |
| 262 | |
| 263 | dest.X = cursor_coords.X; |
| 264 | dest.Y = cursor_coords.Y; |
| 265 | |
| 266 | fill.Char.AsciiChar = 0x20; |
| 267 | fill.Attributes = char_attr_normal; |
| 268 | |
| 269 | ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* If start is zero insert blanks instead of a string at start ?. */ |
| 274 | static void |
| 275 | w32con_insert_glyphs (struct frame *f, register struct glyph *start, |
| 276 | register int len) |
| 277 | { |
| 278 | scroll_line (f, len, RIGHT); |
| 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. */ |
| 285 | |
| 286 | w32con_write_glyphs (f, start, len); |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | w32con_clear_end_of_line (f, cursor_coords.X + len); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | w32con_write_glyphs (struct frame *f, register struct glyph *string, |
| 296 | register int len) |
| 297 | { |
| 298 | DWORD r; |
| 299 | WORD char_attr; |
| 300 | unsigned char *conversion_buffer; |
| 301 | struct coding_system *coding; |
| 302 | |
| 303 | if (len <= 0) |
| 304 | return; |
| 305 | |
| 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. */ |
| 309 | coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK |
| 310 | ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding); |
| 311 | /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at |
| 312 | the tail. */ |
| 313 | coding->mode &= ~CODING_MODE_LAST_BLOCK; |
| 314 | |
| 315 | while (len > 0) |
| 316 | { |
| 317 | /* Identify a run of glyphs with the same face. */ |
| 318 | int face_id = string->face_id; |
| 319 | int n; |
| 320 | |
| 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. */ |
| 326 | char_attr = w32_face_attributes (f, face_id); |
| 327 | |
| 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)) |
| 338 | { |
| 339 | printf ("Failed writing console attributes: %d\n", |
| 340 | GetLastError ()); |
| 341 | fflush (stdout); |
| 342 | } |
| 343 | |
| 344 | /* Write the characters. */ |
| 345 | if (!WriteConsoleOutputCharacter (cur_screen, conversion_buffer, |
| 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; |
| 355 | w32con_move_cursor (f, cursor_coords.Y, cursor_coords.X); |
| 356 | } |
| 357 | len -= n; |
| 358 | string += n; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /* Used for mouse highlight. */ |
| 363 | static void |
| 364 | w32con_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. */ |
| 410 | void |
| 411 | tty_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 | } |
| 440 | |
| 441 | static void |
| 442 | w32con_delete_glyphs (struct frame *f, int n) |
| 443 | { |
| 444 | /* delete chars means scroll chars from cursor_coords.X + n to |
| 445 | cursor_coords.X, anything beyond the edge of the screen should |
| 446 | come out empty... */ |
| 447 | |
| 448 | scroll_line (f, n, LEFT); |
| 449 | } |
| 450 | |
| 451 | |
| 452 | static void |
| 453 | w32con_reset_terminal_modes (struct terminal *t) |
| 454 | { |
| 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); |
| 473 | |
| 474 | #ifdef USE_SEPARATE_SCREEN |
| 475 | SetConsoleActiveScreenBuffer (prev_screen); |
| 476 | #else |
| 477 | SetConsoleCursorInfo (prev_screen, &prev_console_cursor); |
| 478 | #endif |
| 479 | |
| 480 | SetConsoleMode (keyboard_handle, prev_console_mode); |
| 481 | } |
| 482 | |
| 483 | static void |
| 484 | w32con_set_terminal_modes (struct terminal *t) |
| 485 | { |
| 486 | CONSOLE_CURSOR_INFO cci; |
| 487 | |
| 488 | /* make cursor big and visible (100 on Windows 95 makes it disappear) */ |
| 489 | cci.dwSize = 99; |
| 490 | cci.bVisible = TRUE; |
| 491 | (void) SetConsoleCursorInfo (cur_screen, &cci); |
| 492 | |
| 493 | SetConsoleActiveScreenBuffer (cur_screen); |
| 494 | |
| 495 | SetConsoleMode (keyboard_handle, ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); |
| 496 | |
| 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); |
| 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... |
| 504 | |
| 505 | we'll start with not moving the cursor while an update is in progress. */ |
| 506 | static void |
| 507 | w32con_update_begin (struct frame * f) |
| 508 | { |
| 509 | } |
| 510 | |
| 511 | static void |
| 512 | w32con_update_end (struct frame * f) |
| 513 | { |
| 514 | SetConsoleCursorPosition (cur_screen, cursor_coords); |
| 515 | } |
| 516 | |
| 517 | /*********************************************************************** |
| 518 | stubs from termcap.c |
| 519 | ***********************************************************************/ |
| 520 | |
| 521 | void |
| 522 | sys_tputs (char *str, int nlines, int (*outfun) (int)) |
| 523 | { |
| 524 | } |
| 525 | |
| 526 | char * |
| 527 | sys_tgetstr (char *cap, char **area) |
| 528 | { |
| 529 | return NULL; |
| 530 | } |
| 531 | |
| 532 | |
| 533 | /*********************************************************************** |
| 534 | stubs from cm.c |
| 535 | ***********************************************************************/ |
| 536 | |
| 537 | struct tty_display_info *current_tty = NULL; |
| 538 | int cost = 0; |
| 539 | |
| 540 | int |
| 541 | evalcost (int c) |
| 542 | { |
| 543 | return c; |
| 544 | } |
| 545 | |
| 546 | int |
| 547 | cmputc (int c) |
| 548 | { |
| 549 | return c; |
| 550 | } |
| 551 | |
| 552 | void |
| 553 | cmcheckmagic (struct tty_display_info *tty) |
| 554 | { |
| 555 | } |
| 556 | |
| 557 | void |
| 558 | cmcostinit (struct tty_display_info *tty) |
| 559 | { |
| 560 | } |
| 561 | |
| 562 | void |
| 563 | cmgoto (struct tty_display_info *tty, int row, int col) |
| 564 | { |
| 565 | } |
| 566 | |
| 567 | void |
| 568 | Wcm_clear (struct tty_display_info *tty) |
| 569 | { |
| 570 | } |
| 571 | |
| 572 | |
| 573 | /* Report the current cursor position. The following two functions |
| 574 | are used in term.c's tty menu code, so they are not really |
| 575 | "stubs". */ |
| 576 | int |
| 577 | cursorX (struct tty_display_info *tty) |
| 578 | { |
| 579 | return cursor_coords.X; |
| 580 | } |
| 581 | |
| 582 | int |
| 583 | cursorY (struct tty_display_info *tty) |
| 584 | { |
| 585 | return cursor_coords.Y; |
| 586 | } |
| 587 | |
| 588 | /*********************************************************************** |
| 589 | Faces |
| 590 | ***********************************************************************/ |
| 591 | |
| 592 | |
| 593 | /* Turn appearances of face FACE_ID on tty frame F on. */ |
| 594 | |
| 595 | static WORD |
| 596 | w32_face_attributes (struct frame *f, int face_id) |
| 597 | { |
| 598 | WORD char_attr; |
| 599 | struct face *face = FACE_FROM_ID (f, face_id); |
| 600 | |
| 601 | eassert (face != NULL); |
| 602 | |
| 603 | char_attr = char_attr_normal; |
| 604 | |
| 605 | /* Reverse the default color if requested. If background and |
| 606 | foreground are specified, then they have been reversed already. */ |
| 607 | if (face->tty_reverse_p) |
| 608 | char_attr = (char_attr & 0xff00) + ((char_attr & 0x000f) << 4) |
| 609 | + ((char_attr & 0x00f0) >> 4); |
| 610 | |
| 611 | /* Before the terminal is properly initialized, all colors map to 0. |
| 612 | Don't try to resolve them. */ |
| 613 | if (NILP (Vtty_defined_color_alist)) |
| 614 | return char_attr; |
| 615 | |
| 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) |
| 622 | char_attr = (char_attr & 0xfff0) + face->foreground; |
| 623 | |
| 624 | if (face->background >= 0 && face->background < 16) |
| 625 | char_attr = (char_attr & 0xff0f) + (face->background << 4); |
| 626 | |
| 627 | return char_attr; |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | initialize_w32_display (struct terminal *term, int *width, int *height) |
| 632 | { |
| 633 | CONSOLE_SCREEN_BUFFER_INFO info; |
| 634 | |
| 635 | term->rif = 0; /* No window based redisplay on the console. */ |
| 636 | term->cursor_to_hook = w32con_move_cursor; |
| 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; |
| 640 | term->clear_end_of_line_hook = w32con_clear_end_of_line; |
| 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; |
| 645 | term->ring_bell_hook = w32_sys_ring_bell; |
| 646 | term->reset_terminal_modes_hook = w32con_reset_terminal_modes; |
| 647 | term->set_terminal_modes_hook = w32con_set_terminal_modes; |
| 648 | term->set_terminal_window_hook = NULL; |
| 649 | term->update_begin_hook = w32con_update_begin; |
| 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; |
| 654 | term->menu_show_hook = tty_menu_show; |
| 655 | |
| 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; |
| 664 | |
| 665 | /* Initialize the mouse-highlight data. */ |
| 666 | reset_mouse_highlight (&term->display_info.tty->mouse_highlight); |
| 667 | |
| 668 | /* Initialize interrupt_handle. */ |
| 669 | init_crit (); |
| 670 | |
| 671 | /* Remember original console settings. */ |
| 672 | keyboard_handle = GetStdHandle (STD_INPUT_HANDLE); |
| 673 | GetConsoleMode (keyboard_handle, &prev_console_mode); |
| 674 | |
| 675 | prev_screen = GetStdHandle (STD_OUTPUT_HANDLE); |
| 676 | |
| 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 | |
| 695 | /* Respect setting of LINES and COLUMNS environment variables. */ |
| 696 | { |
| 697 | char * lines = getenv ("LINES"); |
| 698 | char * columns = getenv ("COLUMNS"); |
| 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 | |
| 729 | GetConsoleScreenBufferInfo (cur_screen, &info); |
| 730 | |
| 731 | char_attr_normal = info.wAttributes; |
| 732 | |
| 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 | { |
| 746 | *height = 25; |
| 747 | *width = 80; |
| 748 | } |
| 749 | |
| 750 | else if (w32_use_full_screen_buffer) |
| 751 | { |
| 752 | *height = info.dwSize.Y; /* lines per page */ |
| 753 | *width = info.dwSize.X; /* characters per line */ |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | /* Lines per page. Use buffer coords instead of buffer size. */ |
| 758 | *height = 1 + info.srWindow.Bottom - info.srWindow.Top; |
| 759 | /* Characters per line. Use buffer coords instead of buffer size. */ |
| 760 | *width = 1 + info.srWindow.Right - info.srWindow.Left; |
| 761 | } |
| 762 | |
| 763 | if (os_subtype == OS_NT) |
| 764 | w32_console_unicode_input = 1; |
| 765 | else |
| 766 | w32_console_unicode_input = 0; |
| 767 | |
| 768 | /* This is needed by w32notify.c:send_notifications. */ |
| 769 | dwMainThreadId = GetCurrentThreadId (); |
| 770 | |
| 771 | /* Setup w32_display_info structure for this frame. */ |
| 772 | |
| 773 | w32_initialize_display_info (build_string ("Console")); |
| 774 | |
| 775 | } |
| 776 | |
| 777 | |
| 778 | DEFUN ("set-screen-color", Fset_screen_color, Sset_screen_color, 2, 2, 0, |
| 779 | doc: /* Set screen foreground and background colors. |
| 780 | |
| 781 | Arguments should be indices between 0 and 15, see w32console.el. */) |
| 782 | (Lisp_Object foreground, Lisp_Object background) |
| 783 | { |
| 784 | char_attr_normal = XFASTINT (foreground) + (XFASTINT (background) << 4); |
| 785 | |
| 786 | Frecenter (Qnil); |
| 787 | return Qt; |
| 788 | } |
| 789 | |
| 790 | DEFUN ("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 | |
| 793 | The colors are returned as a list of 2 indices (FOREGROUND BACKGROUND). |
| 794 | See w32console.el and `tty-defined-color-alist' for mapping of indices |
| 795 | to 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 | |
| 802 | DEFUN ("set-cursor-size", Fset_cursor_size, Sset_cursor_size, 1, 1, 0, |
| 803 | doc: /* Set cursor size. */) |
| 804 | (Lisp_Object size) |
| 805 | { |
| 806 | CONSOLE_CURSOR_INFO cci; |
| 807 | cci.dwSize = XFASTINT (size); |
| 808 | cci.bVisible = TRUE; |
| 809 | (void) SetConsoleCursorInfo (cur_screen, &cci); |
| 810 | |
| 811 | return Qt; |
| 812 | } |
| 813 | |
| 814 | void |
| 815 | syms_of_ntterm (void) |
| 816 | { |
| 817 | #include "w32console.x" |
| 818 | |
| 819 | DEFVAR_BOOL ("w32-use-full-screen-buffer", |
| 820 | w32_use_full_screen_buffer, |
| 821 | doc: /* Non-nil means make terminal frames use the full screen buffer dimensions. |
| 822 | This is desirable when running Emacs over telnet. |
| 823 | A value of nil means use the current console window dimensions; this |
| 824 | may be preferable when working directly at the console with a large |
| 825 | scroll-back buffer. */); |
| 826 | w32_use_full_screen_buffer = 0; |
| 827 | } |