1 /* Terminal hooks for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1992, 1999, 2001-2012 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
20 Tim Fleehart (apollo@online.com) 1-17-92
21 Geoff Voelker (voelker@cs.washington.edu) 9-12-93
32 #include "character.h"
36 #include "termhooks.h"
38 #include "dispextern.h"
42 extern Lisp_Object
Frecenter (Lisp_Object
);
44 static void w32con_move_cursor (struct frame
*f
, int row
, int col
);
45 static void w32con_clear_to_end (struct frame
*f
);
46 static void w32con_clear_frame (struct frame
*f
);
47 static void w32con_clear_end_of_line (struct frame
*f
, int);
48 static void w32con_ins_del_lines (struct frame
*f
, int vpos
, int n
);
49 static void w32con_insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
);
50 static void w32con_write_glyphs (struct frame
*f
, struct glyph
*string
, int len
);
51 static void w32con_delete_glyphs (struct frame
*f
, int n
);
52 static void w32con_reset_terminal_modes (struct terminal
*t
);
53 static void w32con_set_terminal_modes (struct terminal
*t
);
54 static void w32con_set_terminal_window (struct frame
*f
, int size
);
55 static void w32con_update_begin (struct frame
* f
);
56 static void w32con_update_end (struct frame
* f
);
57 static WORD
w32_face_attributes (struct frame
*f
, int face_id
);
59 static COORD cursor_coords
;
60 static HANDLE prev_screen
, cur_screen
;
61 static WORD char_attr_normal
;
62 static DWORD prev_console_mode
;
64 #ifndef USE_SEPARATE_SCREEN
65 static CONSOLE_CURSOR_INFO prev_console_cursor
;
68 HANDLE keyboard_handle
;
71 /* Setting this as the ctrl handler prevents emacs from being killed when
72 someone hits ^C in a 'suspended' session (child shell).
73 Also ignore Ctrl-Break signals. */
76 ctrl_c_handler (unsigned long type
)
78 /* Only ignore "interrupt" events when running interactively. */
79 return (!noninteractive
80 && (type
== CTRL_C_EVENT
|| type
== CTRL_BREAK_EVENT
));
84 /* Move the cursor to (ROW, COL) on FRAME. */
86 w32con_move_cursor (struct frame
*f
, int row
, int col
)
88 cursor_coords
.X
= col
;
89 cursor_coords
.Y
= row
;
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
);
96 /* Clear from cursor to end of screen. */
98 w32con_clear_to_end (struct frame
*f
)
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);
104 /* Clear the frame. */
106 w32con_clear_frame (struct frame
*f
)
111 CONSOLE_SCREEN_BUFFER_INFO info
;
113 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &info
);
115 /* Remember that the screen buffer might be wider than the window. */
116 n
= FRAME_LINES (f
) * info
.dwSize
.X
;
119 FillConsoleOutputAttribute (cur_screen
, char_attr_normal
, n
, dest
, &r
);
120 FillConsoleOutputCharacter (cur_screen
, ' ', n
, dest
, &r
);
122 w32con_move_cursor (f
, 0, 0);
126 static struct glyph glyph_base
[256];
127 static BOOL ceol_initialized
= FALSE
;
129 /* Clear from Cursor to end (what's "standout marker"?). */
131 w32con_clear_end_of_line (struct frame
*f
, int end
)
133 if (!ceol_initialized
)
136 for (i
= 0; i
< 256; i
++)
138 memcpy (&glyph_base
[i
], &space_glyph
, sizeof (struct glyph
));
140 ceol_initialized
= TRUE
;
142 w32con_write_glyphs (f
, glyph_base
, end
- cursor_coords
.X
); /* fencepost ? */
145 /* Insert n lines at vpos. if n is negative delete -n lines. */
147 w32con_ins_del_lines (struct frame
*f
, int vpos
, int n
)
157 scroll
.Top
= vpos
- n
;
158 scroll
.Bottom
= FRAME_LINES (f
);
164 scroll
.Bottom
= FRAME_LINES (f
) - n
;
167 clip
.Top
= clip
.Left
= scroll
.Left
= 0;
168 clip
.Right
= scroll
.Right
= FRAME_COLS (f
);
169 clip
.Bottom
= FRAME_LINES (f
);
173 fill
.Char
.AsciiChar
= 0x20;
174 fill
.Attributes
= char_attr_normal
;
176 ScrollConsoleScreenBuffer (cur_screen
, &scroll
, &clip
, dest
, &fill
);
178 /* Here we have to deal with a w32 console flake: If the scroll
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
187 if (scroll
.Bottom
< dest
.Y
)
189 for (i
= scroll
.Bottom
; i
< dest
.Y
; i
++)
191 w32con_move_cursor (f
, i
, 0);
192 w32con_clear_end_of_line (f
, FRAME_COLS (f
));
198 nb
= dest
.Y
+ (scroll
.Bottom
- scroll
.Top
) + 1;
202 for (i
= nb
; i
< scroll
.Top
; i
++)
204 w32con_move_cursor (f
, i
, 0);
205 w32con_clear_end_of_line (f
, FRAME_COLS (f
));
211 cursor_coords
.Y
= vpos
;
220 scroll_line (struct frame
*f
, int dist
, int direction
)
222 /* The idea here is to implement a horizontal scroll in one line to
223 implement delete and half of insert. */
224 SMALL_RECT scroll
, clip
;
228 clip
.Top
= scroll
.Top
= clip
.Bottom
= scroll
.Bottom
= cursor_coords
.Y
;
230 clip
.Right
= FRAME_COLS (f
);
232 if (direction
== LEFT
)
234 scroll
.Left
= cursor_coords
.X
+ dist
;
235 scroll
.Right
= FRAME_COLS (f
) - 1;
239 scroll
.Left
= cursor_coords
.X
;
240 scroll
.Right
= FRAME_COLS (f
) - dist
- 1;
243 dest
.X
= cursor_coords
.X
;
244 dest
.Y
= cursor_coords
.Y
;
246 fill
.Char
.AsciiChar
= 0x20;
247 fill
.Attributes
= char_attr_normal
;
249 ScrollConsoleScreenBuffer (cur_screen
, &scroll
, &clip
, dest
, &fill
);
253 /* If start is zero insert blanks instead of a string at start ?. */
255 w32con_insert_glyphs (struct frame
*f
, register struct glyph
*start
,
258 scroll_line (f
, len
, RIGHT
);
260 /* Move len chars to the right starting at cursor_coords, fill with blanks */
263 /* Print the first len characters of start, cursor_coords.X adjusted
266 w32con_write_glyphs (f
, start
, len
);
270 w32con_clear_end_of_line (f
, cursor_coords
.X
+ len
);
275 w32con_write_glyphs (struct frame
*f
, register struct glyph
*string
,
280 unsigned char *conversion_buffer
;
281 struct coding_system
*coding
;
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. */
289 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
290 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
291 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
293 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
297 /* Identify a run of glyphs with the same face. */
298 int face_id
= string
->face_id
;
301 for (n
= 1; n
< len
; ++n
)
302 if (string
[n
].face_id
!= face_id
)
305 /* Turn appearance modes of the face of the run on. */
306 char_attr
= w32_face_attributes (f
, face_id
);
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)
314 /* Set the attribute for these characters. */
315 if (!FillConsoleOutputAttribute (cur_screen
, char_attr
,
316 coding
->produced
, cursor_coords
,
319 printf ("Failed writing console attributes: %d\n",
324 /* Write the characters. */
325 if (!WriteConsoleOutputCharacter (cur_screen
, conversion_buffer
,
326 coding
->produced
, cursor_coords
,
329 printf ("Failed writing console characters: %d\n",
334 cursor_coords
.X
+= coding
->produced
;
335 w32con_move_cursor (f
, cursor_coords
.Y
, cursor_coords
.X
);
344 w32con_delete_glyphs (struct frame
*f
, int n
)
346 /* delete chars means scroll chars from cursor_coords.X + n to
347 cursor_coords.X, anything beyond the edge of the screen should
350 scroll_line (f
, n
, LEFT
);
353 static unsigned int sound_type
= 0xFFFFFFFF;
354 #define MB_EMACS_SILENT (0xFFFFFFFF - 1)
357 w32_sys_ring_bell (struct frame
*f
)
359 if (sound_type
== 0xFFFFFFFF)
363 else if (sound_type
== MB_EMACS_SILENT
)
368 MessageBeep (sound_type
);
371 DEFUN ("set-message-beep", Fset_message_beep
, Sset_message_beep
, 1, 1, 0,
372 doc
: /* Set the sound generated when the bell is rung.
373 SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
374 to use the corresponding system sound for the bell. The 'silent sound
375 prevents Emacs from making any sound at all.
376 SOUND is nil to use the normal beep. */)
379 CHECK_SYMBOL (sound
);
382 sound_type
= 0xFFFFFFFF;
383 else if (EQ (sound
, intern ("asterisk")))
384 sound_type
= MB_ICONASTERISK
;
385 else if (EQ (sound
, intern ("exclamation")))
386 sound_type
= MB_ICONEXCLAMATION
;
387 else if (EQ (sound
, intern ("hand")))
388 sound_type
= MB_ICONHAND
;
389 else if (EQ (sound
, intern ("question")))
390 sound_type
= MB_ICONQUESTION
;
391 else if (EQ (sound
, intern ("ok")))
393 else if (EQ (sound
, intern ("silent")))
394 sound_type
= MB_EMACS_SILENT
;
396 sound_type
= 0xFFFFFFFF;
402 w32con_reset_terminal_modes (struct terminal
*t
)
405 CONSOLE_SCREEN_BUFFER_INFO info
;
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
);
416 n
= info
.dwSize
.X
* info
.dwSize
.Y
;
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
);
423 #ifdef USE_SEPARATE_SCREEN
424 SetConsoleActiveScreenBuffer (prev_screen
);
426 SetConsoleCursorInfo (prev_screen
, &prev_console_cursor
);
429 SetConsoleMode (keyboard_handle
, prev_console_mode
);
433 w32con_set_terminal_modes (struct terminal
*t
)
435 CONSOLE_CURSOR_INFO cci
;
437 /* make cursor big and visible (100 on Win95 makes it disappear) */
440 (void) SetConsoleCursorInfo (cur_screen
, &cci
);
442 SetConsoleActiveScreenBuffer (cur_screen
);
444 SetConsoleMode (keyboard_handle
, ENABLE_MOUSE_INPUT
| ENABLE_WINDOW_INPUT
);
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
);
451 /* hmmm... perhaps these let us bracket screen changes so that we can flush
452 clumps rather than one-character-at-a-time...
454 we'll start with not moving the cursor while an update is in progress. */
456 w32con_update_begin (struct frame
* f
)
461 w32con_update_end (struct frame
* f
)
463 SetConsoleCursorPosition (cur_screen
, cursor_coords
);
467 w32con_set_terminal_window (struct frame
*f
, int size
)
471 /***********************************************************************
473 ***********************************************************************/
476 sys_tputs (char *str
, int nlines
, int (*outfun
) (int))
481 sys_tgetstr (char *cap
, char **area
)
487 /***********************************************************************
489 ***********************************************************************/
491 struct tty_display_info
*current_tty
= NULL
;
507 cmcheckmagic (struct tty_display_info
*tty
)
512 cmcostinit (struct tty_display_info
*tty
)
517 cmgoto (struct tty_display_info
*tty
, int row
, int col
)
522 Wcm_clear (struct tty_display_info
*tty
)
527 /***********************************************************************
529 ***********************************************************************/
532 /* Turn appearances of face FACE_ID on tty frame F on. */
535 w32_face_attributes (struct frame
*f
, int face_id
)
538 struct face
*face
= FACE_FROM_ID (f
, face_id
);
540 xassert (face
!= NULL
);
542 char_attr
= char_attr_normal
;
544 /* Reverse the default color if requested. If background and
545 foreground are specified, then they have been reversed already. */
546 if (face
->tty_reverse_p
)
547 char_attr
= (char_attr
& 0xff00) + ((char_attr
& 0x000f) << 4)
548 + ((char_attr
& 0x00f0) >> 4);
550 /* Before the terminal is properly initialized, all colors map to 0.
551 Don't try to resolve them. */
552 if (NILP (Vtty_defined_color_alist
))
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)
561 char_attr
= (char_attr
& 0xfff0) + face
->foreground
;
563 if (face
->background
>= 0 && face
->background
< 16)
564 char_attr
= (char_attr
& 0xff0f) + (face
->background
<< 4);
570 initialize_w32_display (struct terminal
*term
)
572 CONSOLE_SCREEN_BUFFER_INFO info
;
574 term
->rif
= 0; /* No window based redisplay on the console. */
575 term
->cursor_to_hook
= w32con_move_cursor
;
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
;
579 term
->clear_end_of_line_hook
= w32con_clear_end_of_line
;
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
;
584 term
->ring_bell_hook
= w32_sys_ring_bell
;
585 term
->reset_terminal_modes_hook
= w32con_reset_terminal_modes
;
586 term
->set_terminal_modes_hook
= w32con_set_terminal_modes
;
587 term
->set_terminal_window_hook
= w32con_set_terminal_window
;
588 term
->update_begin_hook
= w32con_update_begin
;
589 term
->update_end_hook
= w32con_update_end
;
591 term
->read_socket_hook
= w32_console_read_socket
;
592 term
->mouse_position_hook
= w32_console_mouse_position
;
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;
603 /* Initialize interrupt_handle. */
606 /* Remember original console settings. */
607 keyboard_handle
= GetStdHandle (STD_INPUT_HANDLE
);
608 GetConsoleMode (keyboard_handle
, &prev_console_mode
);
610 prev_screen
= GetStdHandle (STD_OUTPUT_HANDLE
);
612 #ifdef USE_SEPARATE_SCREEN
613 cur_screen
= CreateConsoleScreenBuffer (GENERIC_READ
| GENERIC_WRITE
,
615 CONSOLE_TEXTMODE_BUFFER
,
618 if (cur_screen
== INVALID_HANDLE_VALUE
)
620 printf ("CreateConsoleScreenBuffer failed in ResetTerm\n");
621 printf ("LastError = 0x%lx\n", GetLastError ());
626 cur_screen
= prev_screen
;
627 GetConsoleCursorInfo (prev_screen
, &prev_console_cursor
);
630 /* Respect setting of LINES and COLUMNS environment variables. */
632 char * lines
= getenv ("LINES");
633 char * columns
= getenv ("COLUMNS");
635 if (lines
!= NULL
&& columns
!= NULL
)
637 SMALL_RECT new_win_dims
;
640 new_size
.X
= atoi (columns
);
641 new_size
.Y
= atoi (lines
);
643 GetConsoleScreenBufferInfo (cur_screen
, &info
);
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
);
653 SetConsoleScreenBufferSize (cur_screen
, new_size
);
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
);
664 GetConsoleScreenBufferInfo (cur_screen
, &info
);
666 char_attr_normal
= info
.wAttributes
;
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)))
681 FRAME_LINES (SELECTED_FRAME ()) = 25;
682 SET_FRAME_COLS (SELECTED_FRAME (), 80);
685 else if (w32_use_full_screen_buffer
)
687 FRAME_LINES (SELECTED_FRAME ()) = info
.dwSize
.Y
; /* lines per page */
688 SET_FRAME_COLS (SELECTED_FRAME (), info
.dwSize
.X
); /* characters per line */
692 /* Lines per page. Use buffer coords instead of buffer size. */
693 FRAME_LINES (SELECTED_FRAME ()) = 1 + info
.srWindow
.Bottom
-
695 /* Characters per line. Use buffer coords instead of buffer size. */
696 SET_FRAME_COLS (SELECTED_FRAME (), 1 + info
.srWindow
.Right
-
700 /* Setup w32_display_info structure for this frame. */
702 w32_initialize_display_info (build_string ("Console"));
707 DEFUN ("set-screen-color", Fset_screen_color
, Sset_screen_color
, 2, 2, 0,
708 doc
: /* Set screen foreground and background colors.
710 Arguments should be indices between 0 and 15, see w32console.el. */)
711 (Lisp_Object foreground
, Lisp_Object background
)
713 char_attr_normal
= XFASTINT (foreground
) + (XFASTINT (background
) << 4);
719 DEFUN ("get-screen-color", Fget_screen_color
, Sget_screen_color
, 0, 0, 0,
720 doc
: /* Get color indices of the current screen foreground and background.
722 The colors are returned as a list of 2 indices (FOREGROUND BACKGROUND).
723 See w32console.el and `tty-defined-color-alist' for mapping of indices
727 return Fcons (make_number (char_attr_normal
& 0x000f),
728 Fcons (make_number ((char_attr_normal
>> 4) & 0x000f), Qnil
));
731 DEFUN ("set-cursor-size", Fset_cursor_size
, Sset_cursor_size
, 1, 1, 0,
732 doc
: /* Set cursor size. */)
735 CONSOLE_CURSOR_INFO cci
;
736 cci
.dwSize
= XFASTINT (size
);
738 (void) SetConsoleCursorInfo (cur_screen
, &cci
);
744 syms_of_ntterm (void)
746 DEFVAR_BOOL ("w32-use-full-screen-buffer",
747 w32_use_full_screen_buffer
,
748 doc
: /* Non-nil means make terminal frames use the full screen buffer dimensions.
749 This is desirable when running Emacs over telnet.
750 A value of nil means use the current console window dimensions; this
751 may be preferable when working directly at the console with a large
752 scroll-back buffer. */);
753 w32_use_full_screen_buffer
= 0;
755 defsubr (&Sset_screen_color
);
756 defsubr (&Sget_screen_color
);
757 defsubr (&Sset_cursor_size
);
758 defsubr (&Sset_message_beep
);