1 /* Terminal hooks for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1992, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
22 Tim Fleehart (apollo@online.com) 1-17-92
23 Geoff Voelker (voelker@cs.washington.edu) 9-12-93
38 /* Disable features in frame.h that require a Window System. */
39 #undef HAVE_WINDOW_SYSTEM
41 #include "termhooks.h"
43 #include "dispextern.h"
47 extern Lisp_Object
Frecenter ();
50 extern int detect_input_pending ();
53 extern int read_input_pending ();
55 static void w32con_move_cursor (struct frame
*f
, int row
, int col
);
56 static void w32con_clear_to_end (struct frame
*f
);
57 static void w32con_clear_frame (struct frame
*f
);
58 static void w32con_clear_end_of_line (struct frame
*f
, int);
59 static void w32con_ins_del_lines (struct frame
*f
, int vpos
, int n
);
60 static void w32con_insert_glyphs (struct frame
*f
, struct glyph
*start
, int len
);
61 static void w32con_write_glyphs (struct frame
*f
, struct glyph
*string
, int len
);
62 static void w32con_delete_glyphs (struct frame
*f
, int n
);
63 static void w32con_reset_terminal_modes (struct terminal
*t
);
64 static void w32con_set_terminal_modes (struct terminal
*t
);
65 static void w32con_set_terminal_window (struct frame
*f
, int size
);
66 static void w32con_update_begin (struct frame
* f
);
67 static void w32con_update_end (struct frame
* f
);
68 static WORD
w32_face_attributes (struct frame
*f
, int face_id
);
70 static COORD cursor_coords
;
71 static HANDLE prev_screen
, cur_screen
;
72 static WORD char_attr_normal
;
73 static DWORD prev_console_mode
;
75 #ifndef USE_SEPARATE_SCREEN
76 static CONSOLE_CURSOR_INFO prev_console_cursor
;
79 /* Determine whether to make frame dimensions match the screen buffer,
80 or the current window size. The former is desirable when running
81 over telnet, while the latter is more useful when working directly at
82 the console with a large scroll-back buffer. */
83 int w32_use_full_screen_buffer
;
84 HANDLE keyboard_handle
;
87 /* Setting this as the ctrl handler prevents emacs from being killed when
88 someone hits ^C in a 'suspended' session (child shell).
89 Also ignore Ctrl-Break signals. */
92 ctrl_c_handler (unsigned long type
)
94 /* Only ignore "interrupt" events when running interactively. */
95 return (!noninteractive
96 && (type
== CTRL_C_EVENT
|| type
== CTRL_BREAK_EVENT
));
100 /* Move the cursor to (ROW, COL) on FRAME. */
102 w32con_move_cursor (struct frame
*f
, int row
, int col
)
104 cursor_coords
.X
= col
;
105 cursor_coords
.Y
= row
;
107 /* TODO: for multi-tty support, cur_screen should be replaced with a
108 reference to the terminal for this frame. */
109 SetConsoleCursorPosition (cur_screen
, cursor_coords
);
112 /* Clear from cursor to end of screen. */
114 w32con_clear_to_end (struct frame
*f
)
116 w32con_clear_end_of_line (f
, FRAME_COLS (f
) - 1);
117 w32con_ins_del_lines (f
, cursor_coords
.Y
, FRAME_LINES (f
) - cursor_coords
.Y
- 1);
120 /* Clear the frame. */
122 w32con_clear_frame (struct frame
*f
)
127 CONSOLE_SCREEN_BUFFER_INFO info
;
129 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &info
);
131 /* Remember that the screen buffer might be wider than the window. */
132 n
= FRAME_LINES (f
) * info
.dwSize
.X
;
135 FillConsoleOutputAttribute (cur_screen
, char_attr_normal
, n
, dest
, &r
);
136 FillConsoleOutputCharacter (cur_screen
, ' ', n
, dest
, &r
);
138 w32con_move_cursor (f
, 0, 0);
142 static struct glyph glyph_base
[256];
143 static BOOL ceol_initialized
= FALSE
;
145 /* Clear from Cursor to end (what's "standout marker"?). */
147 w32con_clear_end_of_line (struct frame
*f
, int end
)
149 if (!ceol_initialized
)
152 for (i
= 0; i
< 256; i
++)
154 memcpy (&glyph_base
[i
], &space_glyph
, sizeof (struct glyph
));
156 ceol_initialized
= TRUE
;
158 w32con_write_glyphs (f
, glyph_base
, end
- cursor_coords
.X
); /* fencepost ? */
161 /* Insert n lines at vpos. if n is negative delete -n lines. */
163 w32con_ins_del_lines (struct frame
*f
, int vpos
, int n
)
172 scroll
.Top
= vpos
- n
;
173 scroll
.Bottom
= FRAME_LINES (f
);
179 scroll
.Bottom
= FRAME_LINES (f
) - n
;
183 scroll
.Right
= FRAME_COLS (f
);
187 fill
.Char
.AsciiChar
= 0x20;
188 fill
.Attributes
= char_attr_normal
;
190 ScrollConsoleScreenBuffer (cur_screen
, &scroll
, NULL
, dest
, &fill
);
192 /* Here we have to deal with a w32 console flake: If the scroll
193 region looks like abc and we scroll c to a and fill with d we get
194 cbd... if we scroll block c one line at a time to a, we get cdd...
195 Emacs expects cdd consistently... So we have to deal with that
196 here... (this also occurs scrolling the same way in the other
201 if (scroll
.Bottom
< dest
.Y
)
203 for (i
= scroll
.Bottom
; i
< dest
.Y
; i
++)
205 w32con_move_cursor (f
, i
, 0);
206 w32con_clear_end_of_line (f
, FRAME_COLS (f
));
212 nb
= dest
.Y
+ (scroll
.Bottom
- scroll
.Top
) + 1;
216 for (i
= nb
; i
< scroll
.Top
; i
++)
218 w32con_move_cursor (f
, i
, 0);
219 w32con_clear_end_of_line (f
, FRAME_COLS (f
));
225 cursor_coords
.Y
= vpos
;
234 scroll_line (struct frame
*f
, int dist
, int direction
)
236 /* The idea here is to implement a horizontal scroll in one line to
237 implement delete and half of insert. */
242 scroll
.Top
= cursor_coords
.Y
;
243 scroll
.Bottom
= cursor_coords
.Y
;
245 if (direction
== LEFT
)
247 scroll
.Left
= cursor_coords
.X
+ dist
;
248 scroll
.Right
= FRAME_COLS (f
) - 1;
252 scroll
.Left
= cursor_coords
.X
;
253 scroll
.Right
= FRAME_COLS (f
) - dist
- 1;
256 dest
.X
= cursor_coords
.X
;
257 dest
.Y
= cursor_coords
.Y
;
259 fill
.Char
.AsciiChar
= 0x20;
260 fill
.Attributes
= char_attr_normal
;
262 ScrollConsoleScreenBuffer (cur_screen
, &scroll
, NULL
, dest
, &fill
);
266 /* If start is zero insert blanks instead of a string at start ?. */
268 w32con_insert_glyphs (struct frame
*f
, register struct glyph
*start
, register int len
)
270 scroll_line (f
, len
, RIGHT
);
272 /* Move len chars to the right starting at cursor_coords, fill with blanks */
275 /* Print the first len characters of start, cursor_coords.X adjusted
278 w32con_write_glyphs (f
, start
, len
);
282 w32con_clear_end_of_line (f
, cursor_coords
.X
+ len
);
286 extern unsigned char *encode_terminal_code
P_ ((struct glyph
*, int,
287 struct coding_system
*));
290 w32con_write_glyphs (struct frame
*f
, register struct glyph
*string
,
295 unsigned char *conversion_buffer
;
296 struct coding_system
*coding
;
301 /* If terminal_coding does any conversion, use it, otherwise use
302 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
303 because it always return 1 if the member src_multibyte is 1. */
304 coding
= (FRAME_TERMINAL_CODING (f
)->common_flags
& CODING_REQUIRE_ENCODING_MASK
305 ? FRAME_TERMINAL_CODING (f
) : &safe_terminal_coding
);
306 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
308 coding
->mode
&= ~CODING_MODE_LAST_BLOCK
;
312 /* Identify a run of glyphs with the same face. */
313 int face_id
= string
->face_id
;
316 for (n
= 1; n
< len
; ++n
)
317 if (string
[n
].face_id
!= face_id
)
320 /* Turn appearance modes of the face of the run on. */
321 char_attr
= w32_face_attributes (f
, face_id
);
324 /* This is the last run. */
325 coding
->mode
|= CODING_MODE_LAST_BLOCK
;
326 conversion_buffer
= encode_terminal_code (string
, n
, coding
);
327 if (coding
->produced
> 0)
329 /* Set the attribute for these characters. */
330 if (!FillConsoleOutputAttribute (cur_screen
, char_attr
,
331 coding
->produced
, cursor_coords
,
334 printf ("Failed writing console attributes: %d\n",
339 /* Write the characters. */
340 if (!WriteConsoleOutputCharacter (cur_screen
, conversion_buffer
,
341 coding
->produced
, cursor_coords
,
344 printf ("Failed writing console characters: %d\n",
349 cursor_coords
.X
+= coding
->produced
;
350 w32con_move_cursor (f
, cursor_coords
.Y
, cursor_coords
.X
);
359 w32con_delete_glyphs (struct frame
*f
, int n
)
361 /* delete chars means scroll chars from cursor_coords.X + n to
362 cursor_coords.X, anything beyond the edge of the screen should
365 scroll_line (f
, n
, LEFT
);
368 static unsigned int sound_type
= 0xFFFFFFFF;
369 #define MB_EMACS_SILENT (0xFFFFFFFF - 1)
372 w32_sys_ring_bell (struct frame
*f
)
374 if (sound_type
== 0xFFFFFFFF)
378 else if (sound_type
== MB_EMACS_SILENT
)
383 MessageBeep (sound_type
);
386 DEFUN ("set-message-beep", Fset_message_beep
, Sset_message_beep
, 1, 1, 0,
387 doc
: /* Set the sound generated when the bell is rung.
388 SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
389 to use the corresponding system sound for the bell. The 'silent sound
390 prevents Emacs from making any sound at all.
391 SOUND is nil to use the normal beep. */)
395 CHECK_SYMBOL (sound
);
398 sound_type
= 0xFFFFFFFF;
399 else if (EQ (sound
, intern ("asterisk")))
400 sound_type
= MB_ICONASTERISK
;
401 else if (EQ (sound
, intern ("exclamation")))
402 sound_type
= MB_ICONEXCLAMATION
;
403 else if (EQ (sound
, intern ("hand")))
404 sound_type
= MB_ICONHAND
;
405 else if (EQ (sound
, intern ("question")))
406 sound_type
= MB_ICONQUESTION
;
407 else if (EQ (sound
, intern ("ok")))
409 else if (EQ (sound
, intern ("silent")))
410 sound_type
= MB_EMACS_SILENT
;
412 sound_type
= 0xFFFFFFFF;
418 w32con_reset_terminal_modes (struct terminal
*t
)
420 #ifdef USE_SEPARATE_SCREEN
421 SetConsoleActiveScreenBuffer (prev_screen
);
423 SetConsoleCursorInfo (prev_screen
, &prev_console_cursor
);
425 SetConsoleMode (keyboard_handle
, prev_console_mode
);
429 w32con_set_terminal_modes (struct terminal
*t
)
431 CONSOLE_CURSOR_INFO cci
;
433 /* make cursor big and visible (100 on Win95 makes it disappear) */
436 (void) SetConsoleCursorInfo (cur_screen
, &cci
);
438 SetConsoleActiveScreenBuffer (cur_screen
);
440 SetConsoleMode (keyboard_handle
, ENABLE_MOUSE_INPUT
| ENABLE_WINDOW_INPUT
);
442 /* Initialize input mode: interrupt_input off, no flow control, allow
443 8 bit character input, standard quit char. */
444 Fset_input_mode (Qnil
, Qnil
, make_number (2), Qnil
);
447 /* hmmm... perhaps these let us bracket screen changes so that we can flush
448 clumps rather than one-character-at-a-time...
450 we'll start with not moving the cursor while an update is in progress. */
452 w32con_update_begin (struct frame
* f
)
457 w32con_update_end (struct frame
* f
)
459 SetConsoleCursorPosition (cur_screen
, cursor_coords
);
463 w32con_set_terminal_window (struct frame
*f
, int size
)
467 /***********************************************************************
469 ***********************************************************************/
472 /* Turn appearances of face FACE_ID on tty frame F on. */
475 w32_face_attributes (f
, face_id
)
480 struct face
*face
= FACE_FROM_ID (f
, face_id
);
482 xassert (face
!= NULL
);
484 char_attr
= char_attr_normal
;
486 if (face
->foreground
!= FACE_TTY_DEFAULT_FG_COLOR
487 && face
->foreground
!= FACE_TTY_DEFAULT_COLOR
)
488 char_attr
= (char_attr
& 0xfff0) + (face
->foreground
% 16);
490 if (face
->background
!= FACE_TTY_DEFAULT_BG_COLOR
491 && face
->background
!= FACE_TTY_DEFAULT_COLOR
)
492 char_attr
= (char_attr
& 0xff0f) + ((face
->background
% 16) << 4);
495 /* NTEMACS_TODO: Faces defined during startup get both foreground
496 and background of 0. Need a better way around this - for now detect
497 the problem and invert one of the faces to make the text readable. */
498 if (((char_attr
& 0x00f0) >> 4) == (char_attr
& 0x000f))
501 if (face
->tty_reverse_p
)
502 char_attr
= (char_attr
& 0xff00) + ((char_attr
& 0x000f) << 4)
503 + ((char_attr
& 0x00f0) >> 4);
509 /* Emulation of some X window features from xfns.c and xfaces.c. */
511 extern char unspecified_fg
[], unspecified_bg
[];
514 /* Given a color index, return its standard name. */
516 vga_stdcolor_name (int idx
)
518 /* Standard VGA colors, in the order of their standard numbering
519 in the default VGA palette. */
520 static char *vga_colors
[16] = {
521 "black", "blue", "green", "cyan", "red", "magenta", "brown",
522 "lightgray", "darkgray", "lightblue", "lightgreen", "lightcyan",
523 "lightred", "lightmagenta", "yellow", "white"
526 extern Lisp_Object Qunspecified
;
528 if (idx
>= 0 && idx
< sizeof (vga_colors
) / sizeof (vga_colors
[0]))
529 return build_string (vga_colors
[idx
]);
531 return Qunspecified
; /* meaning the default */
534 typedef int (*term_hook
) ();
537 initialize_w32_display (struct terminal
*term
)
539 CONSOLE_SCREEN_BUFFER_INFO info
;
541 term
->rif
= 0; /* No window based redisplay on the console. */
542 term
->cursor_to_hook
= w32con_move_cursor
;
543 term
->raw_cursor_to_hook
= w32con_move_cursor
;
544 term
->clear_to_end_hook
= w32con_clear_to_end
;
545 term
->clear_frame_hook
= w32con_clear_frame
;
546 term
->clear_end_of_line_hook
= w32con_clear_end_of_line
;
547 term
->ins_del_lines_hook
= w32con_ins_del_lines
;
548 term
->insert_glyphs_hook
= w32con_insert_glyphs
;
549 term
->write_glyphs_hook
= w32con_write_glyphs
;
550 term
->delete_glyphs_hook
= w32con_delete_glyphs
;
551 term
->ring_bell_hook
= w32_sys_ring_bell
;
552 term
->reset_terminal_modes_hook
= w32con_reset_terminal_modes
;
553 term
->set_terminal_modes_hook
= w32con_set_terminal_modes
;
554 term
->set_terminal_window_hook
= w32con_set_terminal_window
;
555 term
->update_begin_hook
= w32con_update_begin
;
556 term
->update_end_hook
= w32con_update_end
;
558 term
->read_socket_hook
= w32_console_read_socket
;
559 term
->mouse_position_hook
= w32_console_mouse_position
;
561 /* The following are not used on the console. */
562 term
->frame_rehighlight_hook
= 0;
563 term
->frame_raise_lower_hook
= 0;
564 term
->set_vertical_scroll_bar_hook
= 0;
565 term
->condemn_scroll_bars_hook
= 0;
566 term
->redeem_scroll_bar_hook
= 0;
567 term
->judge_scroll_bars_hook
= 0;
568 term
->frame_up_to_date_hook
= 0;
570 /* Initialize interrupt_handle. */
573 /* Remember original console settings. */
574 keyboard_handle
= GetStdHandle (STD_INPUT_HANDLE
);
575 GetConsoleMode (keyboard_handle
, &prev_console_mode
);
577 prev_screen
= GetStdHandle (STD_OUTPUT_HANDLE
);
579 #ifdef USE_SEPARATE_SCREEN
580 cur_screen
= CreateConsoleScreenBuffer (GENERIC_READ
| GENERIC_WRITE
,
582 CONSOLE_TEXTMODE_BUFFER
,
585 if (cur_screen
== INVALID_HANDLE_VALUE
)
587 printf ("CreateConsoleScreenBuffer failed in ResetTerm\n");
588 printf ("LastError = 0x%lx\n", GetLastError ());
593 cur_screen
= prev_screen
;
594 GetConsoleCursorInfo (prev_screen
, &prev_console_cursor
);
597 /* Respect setting of LINES and COLUMNS environment variables. */
599 char * lines
= getenv("LINES");
600 char * columns
= getenv("COLUMNS");
602 if (lines
!= NULL
&& columns
!= NULL
)
604 SMALL_RECT new_win_dims
;
607 new_size
.X
= atoi (columns
);
608 new_size
.Y
= atoi (lines
);
610 GetConsoleScreenBufferInfo (cur_screen
, &info
);
612 /* Shrink the window first, so the buffer dimensions can be
613 reduced if necessary. */
614 new_win_dims
.Top
= 0;
615 new_win_dims
.Left
= 0;
616 new_win_dims
.Bottom
= min (new_size
.Y
, info
.dwSize
.Y
) - 1;
617 new_win_dims
.Right
= min (new_size
.X
, info
.dwSize
.X
) - 1;
618 SetConsoleWindowInfo (cur_screen
, TRUE
, &new_win_dims
);
620 SetConsoleScreenBufferSize (cur_screen
, new_size
);
622 /* Set the window size to match the buffer dimension. */
623 new_win_dims
.Top
= 0;
624 new_win_dims
.Left
= 0;
625 new_win_dims
.Bottom
= new_size
.Y
- 1;
626 new_win_dims
.Right
= new_size
.X
- 1;
627 SetConsoleWindowInfo (cur_screen
, TRUE
, &new_win_dims
);
631 GetConsoleScreenBufferInfo (cur_screen
, &info
);
633 char_attr_normal
= info
.wAttributes
;
635 /* Determine if the info returned by GetConsoleScreenBufferInfo
636 is realistic. Old MS Telnet servers used to only fill out
637 the dwSize portion, even modern one fill the whole struct with
638 garbage when using non-MS telnet clients. */
639 if ((w32_use_full_screen_buffer
640 && (info
.dwSize
.Y
< 20 || info
.dwSize
.Y
> 100
641 || info
.dwSize
.X
< 40 || info
.dwSize
.X
> 200))
642 || (!w32_use_full_screen_buffer
643 && (info
.srWindow
.Bottom
- info
.srWindow
.Top
< 20
644 || info
.srWindow
.Bottom
- info
.srWindow
.Top
> 100
645 || info
.srWindow
.Right
- info
.srWindow
.Left
< 40
646 || info
.srWindow
.Right
- info
.srWindow
.Left
> 100)))
648 FRAME_LINES (SELECTED_FRAME ()) = 25;
649 SET_FRAME_COLS (SELECTED_FRAME (), 80);
652 else if (w32_use_full_screen_buffer
)
654 FRAME_LINES (SELECTED_FRAME ()) = info
.dwSize
.Y
; /* lines per page */
655 SET_FRAME_COLS (SELECTED_FRAME (), info
.dwSize
.X
); /* characters per line */
659 /* Lines per page. Use buffer coords instead of buffer size. */
660 FRAME_LINES (SELECTED_FRAME ()) = 1 + info
.srWindow
.Bottom
-
662 /* Characters per line. Use buffer coords instead of buffer size. */
663 SET_FRAME_COLS (SELECTED_FRAME (), 1 + info
.srWindow
.Right
-
667 /* Setup w32_display_info structure for this frame. */
669 w32_initialize_display_info (build_string ("Console"));
674 DEFUN ("set-screen-color", Fset_screen_color
, Sset_screen_color
, 2, 2, 0,
675 doc
: /* Set screen colors. */)
676 (foreground
, background
)
677 Lisp_Object foreground
;
678 Lisp_Object background
;
680 char_attr_normal
= XFASTINT (foreground
) + (XFASTINT (background
) << 4);
686 DEFUN ("set-cursor-size", Fset_cursor_size
, Sset_cursor_size
, 1, 1, 0,
687 doc
: /* Set cursor size. */)
691 CONSOLE_CURSOR_INFO cci
;
692 cci
.dwSize
= XFASTINT (size
);
694 (void) SetConsoleCursorInfo (cur_screen
, &cci
);
702 DEFVAR_BOOL ("w32-use-full-screen-buffer",
703 &w32_use_full_screen_buffer
,
704 doc
: /* Non-nil means make terminal frames use the full screen buffer dimensions.
705 This is desirable when running Emacs over telnet.
706 A value of nil means use the current console window dimensions; this
707 may be preferrable when working directly at the console with a large
708 scroll-back buffer. */);
709 w32_use_full_screen_buffer
= 0;
711 defsubr (&Sset_screen_color
);
712 defsubr (&Sset_cursor_size
);
713 defsubr (&Sset_message_beep
);
716 /* arch-tag: a390a07f-f661-42bc-aeb4-e6d8bf860337
717 (do not change this comment) */