2 * window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
3 * emulator and backend in a window.
12 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
20 #define COMPILE_MULTIMON_STUBS
30 /* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
31 * wParam are used by Windows, and should be masked off, so we shouldn't
32 * attempt to store information in them. Hence all these identifiers have
33 * the low 4 bits clear. Also, identifiers should < 0xF000. */
35 #define IDM_SHOWLOG 0x0010
36 #define IDM_NEWSESS 0x0020
37 #define IDM_DUPSESS 0x0030
38 #define IDM_RESTART 0x0040
39 #define IDM_RECONF 0x0050
40 #define IDM_CLRSB 0x0060
41 #define IDM_RESET 0x0070
42 #define IDM_HELP 0x0140
43 #define IDM_ABOUT 0x0150
44 #define IDM_SAVEDSESS 0x0160
45 #define IDM_COPYALL 0x0170
46 #define IDM_FULLSCREEN 0x0180
47 #define IDM_PASTE 0x0190
48 #define IDM_SPECIALSEP 0x0200
50 #define IDM_SPECIAL_MIN 0x0400
51 #define IDM_SPECIAL_MAX 0x0800
53 #define IDM_SAVED_MIN 0x1000
54 #define IDM_SAVED_MAX 0x5000
55 #define MENU_SAVED_STEP 16
56 /* Maximum number of sessions on saved-session submenu */
57 #define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
59 #define WM_IGNORE_CLIP (WM_APP + 2)
60 #define WM_FULLSCR_ON_MAX (WM_APP + 3)
61 #define WM_AGENT_CALLBACK (WM_APP + 4)
63 /* Needed for Chinese support and apparently not always defined. */
65 #define VK_PROCESSKEY 0xE5
68 /* Mouse wheel support. */
70 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
73 #define WHEEL_DELTA 120
76 static Mouse_Button
translate_button(Mouse_Button button
);
77 static LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
78 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
79 unsigned char *output
);
80 static void cfgtopalette(void);
81 static void systopalette(void);
82 static void init_palette(void);
83 static void init_fonts(int, int);
84 static void another_font(int);
85 static void deinit_fonts(void);
86 static void set_input_locale(HKL
);
87 static void update_savedsess_menu(void);
89 static int is_full_screen(void);
90 static void make_full_screen(void);
91 static void clear_full_screen(void);
92 static void flip_full_screen(void);
94 /* Window layout information */
95 static void reset_window(int);
96 static int extra_width
, extra_height
;
97 static int font_width
, font_height
, font_dualwidth
;
98 static int offset_width
, offset_height
;
99 static int was_zoomed
= 0;
100 static int prev_rows
, prev_cols
;
102 static void enact_netevent(WPARAM
, LPARAM
);
103 static void flash_window(int mode
);
104 static void sys_cursor_update(void);
105 static int is_shift_pressed(void);
106 static int get_fullscreen_rect(RECT
* ss
);
108 static int caret_x
= -1, caret_y
= -1;
110 static int kbd_codepage
;
113 static Backend
*back
;
114 static void *backhandle
;
116 static struct unicode_data ucsdata
;
117 static int session_closed
;
118 static int reconfiguring
= FALSE
;
120 static const struct telnet_special
*specials
= NULL
;
121 static HMENU specials_menu
= NULL
;
122 static int n_specials
= 0;
124 #define TIMING_TIMER_ID 1234
125 static long timing_next_time
;
130 enum { SYSMENU
, CTXMENU
};
131 static HMENU savedsess_menu
;
133 Config cfg
; /* exported to windlg.c */
135 static struct sesslist sesslist
; /* for saved-session menu */
137 struct agent_callback
{
138 void (*callback
)(void *, void *, int);
144 #define FONT_NORMAL 0
146 #define FONT_UNDERLINE 2
147 #define FONT_BOLDUND 3
148 #define FONT_WIDE 0x04
149 #define FONT_HIGH 0x08
150 #define FONT_NARROW 0x10
152 #define FONT_OEM 0x20
153 #define FONT_OEMBOLD 0x21
154 #define FONT_OEMUND 0x22
155 #define FONT_OEMBOLDUND 0x23
157 #define FONT_MAXNO 0x2F
159 static HFONT fonts
[FONT_MAXNO
];
160 static LOGFONT lfont
;
161 static int fontflag
[FONT_MAXNO
];
163 BOLD_COLOURS
, BOLD_SHADOW
, BOLD_FONT
170 #define NCFGCOLOURS 22
171 #define NEXTCOLOURS 240
172 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
173 static COLORREF colours
[NALLCOLOURS
];
175 static LPLOGPALETTE logpal
;
176 static RGBTRIPLE defpal
[NALLCOLOURS
];
178 static HBITMAP caretbm
;
180 static int dbltime
, lasttime
, lastact
;
181 static Mouse_Button lastbtn
;
183 /* this allows xterm-style mouse handling. */
184 static int send_raw_mouse
= 0;
185 static int wheel_accumulator
= 0;
187 static int busy_status
= BUSY_NOT
;
189 static char *window_name
, *icon_name
;
191 static int compose_state
= 0;
193 static UINT wm_mousewheel
= WM_MOUSEWHEEL
;
195 /* Dummy routine, only required in plink. */
196 void ldisc_update(void *frontend
, int echo
, int edit
)
200 char *get_ttymode(void *frontend
, const char *mode
)
202 return term_get_ttymode(term
, mode
);
205 static void start_backend(void)
208 char msg
[1024], *title
;
213 * Select protocol. This is farmed out into a table in a
214 * separate file to enable an ssh-free variant.
217 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
218 if (backends
[i
].protocol
== cfg
.protocol
) {
219 back
= backends
[i
].backend
;
223 char *str
= dupprintf("%s Internal Error", appname
);
224 MessageBox(NULL
, "Unsupported protocol number found",
225 str
, MB_OK
| MB_ICONEXCLAMATION
);
230 error
= back
->init(NULL
, &backhandle
, &cfg
,
231 cfg
.host
, cfg
.port
, &realhost
, cfg
.tcp_nodelay
,
233 back
->provide_logctx(backhandle
, logctx
);
235 char *str
= dupprintf("%s Error", appname
);
236 sprintf(msg
, "Unable to open connection to\n"
237 "%.800s\n" "%s", cfg
.host
, error
);
238 MessageBox(NULL
, msg
, str
, MB_ICONERROR
| MB_OK
);
242 window_name
= icon_name
= NULL
;
244 title
= cfg
.wintitle
;
246 sprintf(msg
, "%s - %s", realhost
, appname
);
250 set_title(NULL
, title
);
251 set_icon(NULL
, title
);
254 * Connect the terminal to the backend for resize purposes.
256 term_provide_resize_fn(term
, back
->size
, backhandle
);
259 * Set up a line discipline.
261 ldisc
= ldisc_create(&cfg
, term
, back
, backhandle
, NULL
);
264 * Destroy the Restart Session menu item. (This will return
265 * failure if it's already absent, as it will be the very first
266 * time we call this function. We ignore that, because as long
267 * as the menu item ends up not being there, we don't care
268 * whether it was us who removed it or not!)
270 for (i
= 0; i
< lenof(popup_menus
); i
++) {
271 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
274 session_closed
= FALSE
;
277 static void close_session(void)
282 session_closed
= TRUE
;
283 sprintf(morestuff
, "%.70s (inactive)", appname
);
284 set_icon(NULL
, morestuff
);
285 set_title(NULL
, morestuff
);
292 back
->free(backhandle
);
295 update_specials_menu(NULL
);
299 * Show the Restart Session menu item. Do a precautionary
300 * delete first to ensure we never end up with more than one.
302 for (i
= 0; i
< lenof(popup_menus
); i
++) {
303 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
304 InsertMenu(popup_menus
[i
].menu
, IDM_DUPSESS
, MF_BYCOMMAND
| MF_ENABLED
,
305 IDM_RESTART
, "&Restart Session");
309 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
313 int guess_width
, guess_height
;
317 flags
= FLAG_VERBOSE
| FLAG_INTERACTIVE
;
321 InitCommonControls();
323 /* Ensure a Maximize setting in Explorer doesn't maximise the
329 char *str
= dupprintf("%s Fatal Error", appname
);
330 MessageBox(NULL
, "Windows refuses to report a version",
331 str
, MB_OK
| MB_ICONEXCLAMATION
);
337 * If we're running a version of Windows that doesn't support
338 * WM_MOUSEWHEEL, find out what message number we should be
341 if (osVersion
.dwMajorVersion
< 4 ||
342 (osVersion
.dwMajorVersion
== 4 &&
343 osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
))
344 wm_mousewheel
= RegisterWindowMessage("MSWHEEL_ROLLMSG");
347 * See if we can find our Help file.
350 char b
[2048], *p
, *q
, *r
;
352 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
354 p
= strrchr(b
, '\\');
355 if (p
&& p
>= r
) r
= p
+1;
357 if (q
&& q
>= r
) r
= q
+1;
358 strcpy(r
, PUTTY_HELP_FILE
);
359 if ( (fp
= fopen(b
, "r")) != NULL
) {
360 help_path
= dupstr(b
);
364 strcpy(r
, PUTTY_HELP_CONTENTS
);
365 if ( (fp
= fopen(b
, "r")) != NULL
) {
366 help_has_contents
= TRUE
;
369 help_has_contents
= FALSE
;
373 * Process the command line.
379 default_protocol
= be_default_protocol
;
380 /* Find the appropriate default port. */
383 default_port
= 0; /* illegal */
384 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
385 if (backends
[i
].protocol
== default_protocol
) {
386 default_port
= backends
[i
].backend
->default_port
;
390 cfg
.logtype
= LGTYP_NONE
;
392 do_defaults(NULL
, &cfg
);
397 * Process a couple of command-line options which are more
398 * easily dealt with before the line is broken up into
399 * words. These are the soon-to-be-defunct @sessionname and
400 * the internal-use-only &sharedmemoryhandle, neither of
401 * which are combined with anything else.
403 while (*p
&& isspace(*p
))
407 while (i
> 1 && isspace(p
[i
- 1]))
410 do_defaults(p
+ 1, &cfg
);
411 if (!*cfg
.host
&& !do_config()) {
414 } else if (*p
== '&') {
416 * An initial & means we've been given a command line
417 * containing the hex value of a HANDLE for a file
418 * mapping object, which we must then extract as a
423 if (sscanf(p
+ 1, "%p", &filemap
) == 1 &&
424 (cp
= MapViewOfFile(filemap
, FILE_MAP_READ
,
425 0, 0, sizeof(Config
))) != NULL
) {
428 CloseHandle(filemap
);
429 } else if (!do_config()) {
434 * Otherwise, break up the command line and deal with
440 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
442 for (i
= 0; i
< argc
; i
++) {
446 ret
= cmdline_process_param(p
, i
+1<argc?argv
[i
+1]:NULL
,
449 cmdline_error("option \"%s\" requires an argument", p
);
450 } else if (ret
== 2) {
451 i
++; /* skip next argument */
452 } else if (ret
== 1) {
453 continue; /* nothing further needs doing */
454 } else if (!strcmp(p
, "-cleanup") ||
455 !strcmp(p
, "-cleanup-during-uninstall")) {
457 * `putty -cleanup'. Remove all registry
458 * entries associated with PuTTY, and also find
459 * and delete the random seed file.
462 /* Are we being invoked from an uninstaller? */
463 if (!strcmp(p
, "-cleanup-during-uninstall")) {
464 s1
= dupprintf("Remove saved sessions and random seed file?\n"
466 "If you hit Yes, ALL Registry entries associated\n"
467 "with %s will be removed, as well as the\n"
468 "random seed file. THIS PROCESS WILL\n"
469 "DESTROY YOUR SAVED SESSIONS.\n"
470 "(This only affects the currently logged-in user.)\n"
472 "If you hit No, uninstallation will proceed, but\n"
473 "saved sessions etc will be left on the machine.",
475 s2
= dupprintf("%s Uninstallation", appname
);
477 s1
= dupprintf("This procedure will remove ALL Registry entries\n"
478 "associated with %s, and will also remove\n"
479 "the random seed file. (This only affects the\n"
480 "currently logged-in user.)\n"
482 "THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
483 "Are you really sure you want to continue?",
485 s2
= dupprintf("%s Warning", appname
);
487 if (message_box(s1
, s2
,
488 MB_YESNO
| MB_ICONWARNING
| MB_DEFBUTTON2
,
489 HELPCTXID(option_cleanup
)) == IDYES
) {
495 } else if (!strcmp(p
, "-pgpfp")) {
498 } else if (*p
!= '-') {
502 * If we already have a host name, treat
503 * this argument as a port number. NB we
504 * have to treat this as a saved -P
505 * argument, so that it will be deferred
506 * until it's a good moment to run it.
508 int ret
= cmdline_process_param("-P", p
, 1, &cfg
);
510 } else if (!strncmp(q
, "telnet:", 7)) {
512 * If the hostname starts with "telnet:",
513 * set the protocol to Telnet and process
514 * the string as a Telnet URL.
519 if (q
[0] == '/' && q
[1] == '/')
521 cfg
.protocol
= PROT_TELNET
;
523 while (*p
&& *p
!= ':' && *p
!= '/')
532 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
533 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
537 * Otherwise, treat this argument as a host
540 while (*p
&& !isspace(*p
))
544 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
545 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
549 cmdline_error("unknown option \"%s\"", p
);
554 cmdline_run_saved(&cfg
);
556 if (!*cfg
.host
&& !do_config()) {
561 * Trim leading whitespace off the hostname if it's there.
564 int space
= strspn(cfg
.host
, " \t");
565 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
568 /* See if host is of the form user@host */
569 if (cfg
.host
[0] != '\0') {
570 char *atsign
= strrchr(cfg
.host
, '@');
571 /* Make sure we're not overflowing the user field */
573 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
574 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
575 cfg
.username
[atsign
- cfg
.host
] = '\0';
577 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
582 * Trim a colon suffix off the hostname if it's there. In
583 * order to protect IPv6 address literals against this
584 * treatment, we do not do this if there's _more_ than one
588 char *c
= strchr(cfg
.host
, ':');
591 char *d
= strchr(c
+1, ':');
598 * Remove any remaining whitespace from the hostname.
602 while (cfg
.host
[p2
] != '\0') {
603 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
604 cfg
.host
[p1
] = cfg
.host
[p2
];
613 /* Check for invalid Port number (i.e. zero) */
615 char *str
= dupprintf("%s Internal Error", appname
);
616 MessageBox(NULL
, "Invalid Port Number",
617 str
, MB_OK
| MB_ICONEXCLAMATION
);
624 wndclass
.lpfnWndProc
= WndProc
;
625 wndclass
.cbClsExtra
= 0;
626 wndclass
.cbWndExtra
= 0;
627 wndclass
.hInstance
= inst
;
628 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
629 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
630 wndclass
.hbrBackground
= NULL
;
631 wndclass
.lpszMenuName
= NULL
;
632 wndclass
.lpszClassName
= appname
;
634 RegisterClass(&wndclass
);
637 memset(&ucsdata
, 0, sizeof(ucsdata
));
642 * Guess some defaults for the window size. This all gets
643 * updated later, so we don't really care too much. However, we
644 * do want the font width/height guesses to correspond to a
645 * large font rather than a small one...
652 guess_width
= extra_width
+ font_width
* cfg
.width
;
653 guess_height
= extra_height
+ font_height
* cfg
.height
;
656 get_fullscreen_rect(&r
);
657 if (guess_width
> r
.right
- r
.left
)
658 guess_width
= r
.right
- r
.left
;
659 if (guess_height
> r
.bottom
- r
.top
)
660 guess_height
= r
.bottom
- r
.top
;
664 int winmode
= WS_OVERLAPPEDWINDOW
| WS_VSCROLL
;
667 winmode
&= ~(WS_VSCROLL
);
668 if (cfg
.resize_action
== RESIZE_DISABLED
)
669 winmode
&= ~(WS_THICKFRAME
| WS_MAXIMIZEBOX
);
671 exwinmode
|= WS_EX_TOPMOST
;
673 exwinmode
|= WS_EX_CLIENTEDGE
;
674 hwnd
= CreateWindowEx(exwinmode
, appname
, appname
,
675 winmode
, CW_USEDEFAULT
, CW_USEDEFAULT
,
676 guess_width
, guess_height
,
677 NULL
, NULL
, inst
, NULL
);
681 * Initialise the terminal. (We have to do this _after_
682 * creating the window, since the terminal is the first thing
683 * which will call schedule_timer(), which will in turn call
684 * timer_change_notify() which will expect hwnd to exist.)
686 term
= term_init(&cfg
, &ucsdata
, NULL
);
687 logctx
= log_init(NULL
, &cfg
);
688 term_provide_logctx(term
, logctx
);
689 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
692 * Initialise the fonts, simultaneously correcting the guesses
693 * for font_{width,height}.
698 * Correct the guesses for extra_{width,height}.
702 GetWindowRect(hwnd
, &wr
);
703 GetClientRect(hwnd
, &cr
);
704 offset_width
= offset_height
= cfg
.window_border
;
705 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
706 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
710 * Resize the window, now we know what size we _really_ want it
713 guess_width
= extra_width
+ font_width
* term
->cols
;
714 guess_height
= extra_height
+ font_height
* term
->rows
;
715 SetWindowPos(hwnd
, NULL
, 0, 0, guess_width
, guess_height
,
716 SWP_NOMOVE
| SWP_NOREDRAW
| SWP_NOZORDER
);
719 * Set up a caret bitmap, with no content.
723 int size
= (font_width
+ 15) / 16 * 2 * font_height
;
724 bits
= snewn(size
, char);
725 memset(bits
, 0, size
);
726 caretbm
= CreateBitmap(font_width
, font_height
, 1, 1, bits
);
729 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
732 * Initialise the scroll bar.
737 si
.cbSize
= sizeof(si
);
738 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
740 si
.nMax
= term
->rows
- 1;
741 si
.nPage
= term
->rows
;
743 SetScrollInfo(hwnd
, SB_VERT
, &si
, FALSE
);
747 * Prepare the mouse handler.
749 lastact
= MA_NOTHING
;
750 lastbtn
= MBT_NOTHING
;
751 dbltime
= GetDoubleClickTime();
754 * Set up the session-control options on the system menu.
761 popup_menus
[SYSMENU
].menu
= GetSystemMenu(hwnd
, FALSE
);
762 popup_menus
[CTXMENU
].menu
= CreatePopupMenu();
763 AppendMenu(popup_menus
[CTXMENU
].menu
, MF_ENABLED
, IDM_PASTE
, "&Paste");
765 savedsess_menu
= CreateMenu();
766 get_sesslist(&sesslist
, TRUE
);
767 update_savedsess_menu();
769 for (j
= 0; j
< lenof(popup_menus
); j
++) {
770 m
= popup_menus
[j
].menu
;
772 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
773 AppendMenu(m
, MF_ENABLED
, IDM_SHOWLOG
, "&Event Log");
774 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
775 AppendMenu(m
, MF_ENABLED
, IDM_NEWSESS
, "Ne&w Session...");
776 AppendMenu(m
, MF_ENABLED
, IDM_DUPSESS
, "&Duplicate Session");
777 AppendMenu(m
, MF_POPUP
| MF_ENABLED
, (UINT
) savedsess_menu
,
779 AppendMenu(m
, MF_ENABLED
, IDM_RECONF
, "Chan&ge Settings...");
780 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
781 AppendMenu(m
, MF_ENABLED
, IDM_COPYALL
, "C&opy All to Clipboard");
782 AppendMenu(m
, MF_ENABLED
, IDM_CLRSB
, "C&lear Scrollback");
783 AppendMenu(m
, MF_ENABLED
, IDM_RESET
, "Rese&t Terminal");
784 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
785 AppendMenu(m
, (cfg
.resize_action
== RESIZE_DISABLED
) ?
786 MF_GRAYED
: MF_ENABLED
, IDM_FULLSCREEN
, "&Full Screen");
787 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
789 AppendMenu(m
, MF_ENABLED
, IDM_HELP
, "&Help");
790 str
= dupprintf("&About %s", appname
);
791 AppendMenu(m
, MF_ENABLED
, IDM_ABOUT
, str
);
799 * Set up the initial input locale.
801 set_input_locale(GetKeyboardLayout(0));
804 * Finally show the window!
806 ShowWindow(hwnd
, show
);
807 SetForegroundWindow(hwnd
);
810 * Set the palette up.
816 term_set_focus(term
, GetForegroundWindow() == hwnd
);
819 if (GetMessage(&msg
, NULL
, 0, 0) == 1) {
820 while (msg
.message
!= WM_QUIT
) {
821 if (!(IsWindow(logbox
) && IsDialogMessage(logbox
, &msg
)))
822 DispatchMessage(&msg
);
823 /* Send the paste buffer if there's anything to send */
825 /* If there's nothing new in the queue then we can do everything
826 * we've delayed, reading the socket, writing, and repainting
829 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
832 /* The messages seem unreliable; especially if we're being tricky */
833 term_set_focus(term
, GetForegroundWindow() == hwnd
);
835 net_pending_errors();
837 /* There's no point rescanning everything in the message queue
838 * so we do an apparently unnecessary wait here
841 if (GetMessage(&msg
, NULL
, 0, 0) != 1)
846 cleanup_exit(msg
.wParam
); /* this doesn't return... */
847 return msg
.wParam
; /* ... but optimiser doesn't know */
853 void cleanup_exit(int code
)
864 if (cfg
.protocol
== PROT_SSH
) {
875 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
877 char *do_select(SOCKET skt
, int startup
)
882 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
883 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
888 return "do_select(): internal error (hwnd==NULL)";
889 if (p_WSAAsyncSelect(skt
, hwnd
, msg
, events
) == SOCKET_ERROR
) {
890 switch (p_WSAGetLastError()) {
892 return "Network is down";
894 return "WSAAsyncSelect(): unknown error";
901 * Refresh the saved-session submenu from `sesslist'.
903 static void update_savedsess_menu(void)
906 while (DeleteMenu(savedsess_menu
, 0, MF_BYPOSITION
)) ;
907 /* skip sesslist.sessions[0] == Default Settings */
909 i
< ((sesslist
.nsessions
<= MENU_SAVED_MAX
+1) ? sesslist
.nsessions
912 AppendMenu(savedsess_menu
, MF_ENABLED
,
913 IDM_SAVED_MIN
+ (i
-1)*MENU_SAVED_STEP
,
914 sesslist
.sessions
[i
]);
918 * Update the Special Commands submenu.
920 void update_specials_menu(void *frontend
)
926 specials
= back
->get_specials(backhandle
);
931 /* We can't use Windows to provide a stack for submenus, so
932 * here's a lame "stack" that will do for now. */
933 HMENU saved_menu
= NULL
;
935 new_menu
= CreatePopupMenu();
936 for (i
= 0; nesting
> 0; i
++) {
937 assert(IDM_SPECIAL_MIN
+ 0x10 * i
< IDM_SPECIAL_MAX
);
938 switch (specials
[i
].code
) {
940 AppendMenu(new_menu
, MF_SEPARATOR
, 0, 0);
945 saved_menu
= new_menu
; /* XXX lame stacking */
946 new_menu
= CreatePopupMenu();
947 AppendMenu(saved_menu
, MF_POPUP
| MF_ENABLED
,
948 (UINT
) new_menu
, specials
[i
].name
);
953 new_menu
= saved_menu
; /* XXX lame stacking */
958 AppendMenu(new_menu
, MF_ENABLED
, IDM_SPECIAL_MIN
+ 0x10 * i
,
963 /* Squirrel the highest special. */
970 for (j
= 0; j
< lenof(popup_menus
); j
++) {
972 /* XXX does this free up all submenus? */
973 DeleteMenu(popup_menus
[j
].menu
, specials_menu
, MF_BYCOMMAND
);
974 DeleteMenu(popup_menus
[j
].menu
, IDM_SPECIALSEP
, MF_BYCOMMAND
);
977 InsertMenu(popup_menus
[j
].menu
, IDM_SHOWLOG
,
978 MF_BYCOMMAND
| MF_POPUP
| MF_ENABLED
,
979 (UINT
) new_menu
, "S&pecial Command");
980 InsertMenu(popup_menus
[j
].menu
, IDM_SHOWLOG
,
981 MF_BYCOMMAND
| MF_SEPARATOR
, IDM_SPECIALSEP
, 0);
984 specials_menu
= new_menu
;
987 static void update_mouse_pointer(void)
990 int force_visible
= FALSE
;
991 static int forced_visible
= FALSE
;
992 switch (busy_status
) {
995 curstype
= IDC_ARROW
;
997 curstype
= IDC_IBEAM
;
1000 curstype
= IDC_APPSTARTING
; /* this may be an abuse */
1001 force_visible
= TRUE
;
1004 curstype
= IDC_WAIT
;
1005 force_visible
= TRUE
;
1011 HCURSOR cursor
= LoadCursor(NULL
, curstype
);
1012 SetClassLongPtr(hwnd
, GCLP_HCURSOR
, (LONG_PTR
)cursor
);
1013 SetCursor(cursor
); /* force redraw of cursor at current posn */
1015 if (force_visible
!= forced_visible
) {
1016 /* We want some cursor shapes to be visible always.
1017 * Along with show_mouseptr(), this manages the ShowCursor()
1018 * counter such that if we switch back to a non-force_visible
1019 * cursor, the previous visibility state is restored. */
1020 ShowCursor(force_visible
);
1021 forced_visible
= force_visible
;
1025 void set_busy_status(void *frontend
, int status
)
1027 busy_status
= status
;
1028 update_mouse_pointer();
1032 * set or clear the "raw mouse message" mode
1034 void set_raw_mouse_mode(void *frontend
, int activate
)
1036 activate
= activate
&& !cfg
.no_mouse_rep
;
1037 send_raw_mouse
= activate
;
1038 update_mouse_pointer();
1042 * Print a message box and close the connection.
1044 void connection_fatal(void *frontend
, char *fmt
, ...)
1047 char *stuff
, morestuff
[100];
1050 stuff
= dupvprintf(fmt
, ap
);
1052 sprintf(morestuff
, "%.70s Fatal Error", appname
);
1053 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
1056 if (cfg
.close_on_exit
== FORCE_ON
)
1064 * Report an error at the command-line parsing stage.
1066 void cmdline_error(char *fmt
, ...)
1069 char *stuff
, morestuff
[100];
1072 stuff
= dupvprintf(fmt
, ap
);
1074 sprintf(morestuff
, "%.70s Command Line Error", appname
);
1075 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
1081 * Actually do the job requested by a WM_NETEVENT
1083 static void enact_netevent(WPARAM wParam
, LPARAM lParam
)
1085 static int reentering
= 0;
1086 extern int select_result(WPARAM
, LPARAM
);
1090 return; /* don't unpend the pending */
1093 ret
= select_result(wParam
, lParam
);
1096 if (ret
== 0 && !session_closed
) {
1097 /* Abnormal exits will already have set session_closed and taken
1098 * appropriate action. */
1099 if (cfg
.close_on_exit
== FORCE_ON
||
1100 cfg
.close_on_exit
== AUTO
) PostQuitMessage(0);
1103 session_closed
= TRUE
;
1104 MessageBox(hwnd
, "Connection closed by remote host",
1105 appname
, MB_OK
| MB_ICONINFORMATION
);
1111 * Copy the colour palette from the configuration data into defpal.
1112 * This is non-trivial because the colour indices are different.
1114 static void cfgtopalette(void)
1117 static const int ww
[] = {
1118 256, 257, 258, 259, 260, 261,
1119 0, 8, 1, 9, 2, 10, 3, 11,
1120 4, 12, 5, 13, 6, 14, 7, 15
1123 for (i
= 0; i
< 22; i
++) {
1125 defpal
[w
].rgbtRed
= cfg
.colours
[i
][0];
1126 defpal
[w
].rgbtGreen
= cfg
.colours
[i
][1];
1127 defpal
[w
].rgbtBlue
= cfg
.colours
[i
][2];
1129 for (i
= 0; i
< NEXTCOLOURS
; i
++) {
1131 int r
= i
/ 36, g
= (i
/ 6) % 6, b
= i
% 6;
1132 defpal
[i
+16].rgbtRed
= r ? r
* 40 + 55 : 0;
1133 defpal
[i
+16].rgbtGreen
= g ? g
* 40 + 55 : 0;
1134 defpal
[i
+16].rgbtBlue
= b ? b
* 40 + 55 : 0;
1136 int shade
= i
- 216;
1137 shade
= shade
* 10 + 8;
1138 defpal
[i
+16].rgbtRed
= defpal
[i
+16].rgbtGreen
=
1139 defpal
[i
+16].rgbtBlue
= shade
;
1143 /* Override with system colours if appropriate */
1144 if (cfg
.system_colour
)
1149 * Override bit of defpal with colours from the system.
1150 * (NB that this takes a copy the system colours at the time this is called,
1151 * so subsequent colour scheme changes don't take effect. To fix that we'd
1152 * probably want to be using GetSysColorBrush() and the like.)
1154 static void systopalette(void)
1157 static const struct { int nIndex
; int norm
; int bold
; } or[] =
1159 { COLOR_WINDOWTEXT
, 256, 257 }, /* Default Foreground */
1160 { COLOR_WINDOW
, 258, 259 }, /* Default Background */
1161 { COLOR_HIGHLIGHTTEXT
, 260, 260 }, /* Cursor Text */
1162 { COLOR_HIGHLIGHT
, 261, 261 }, /* Cursor Colour */
1165 for (i
= 0; i
< (sizeof(or)/sizeof(or[0])); i
++) {
1166 COLORREF colour
= GetSysColor(or[i
].nIndex
);
1167 defpal
[or[i
].norm
].rgbtRed
=
1168 defpal
[or[i
].bold
].rgbtRed
= GetRValue(colour
);
1169 defpal
[or[i
].norm
].rgbtGreen
=
1170 defpal
[or[i
].bold
].rgbtGreen
= GetGValue(colour
);
1171 defpal
[or[i
].norm
].rgbtBlue
=
1172 defpal
[or[i
].bold
].rgbtBlue
= GetBValue(colour
);
1177 * Set up the colour palette.
1179 static void init_palette(void)
1182 HDC hdc
= GetDC(hwnd
);
1184 if (cfg
.try_palette
&& GetDeviceCaps(hdc
, RASTERCAPS
) & RC_PALETTE
) {
1186 * This is a genuine case where we must use smalloc
1187 * because the snew macros can't cope.
1189 logpal
= smalloc(sizeof(*logpal
)
1190 - sizeof(logpal
->palPalEntry
)
1191 + NALLCOLOURS
* sizeof(PALETTEENTRY
));
1192 logpal
->palVersion
= 0x300;
1193 logpal
->palNumEntries
= NALLCOLOURS
;
1194 for (i
= 0; i
< NALLCOLOURS
; i
++) {
1195 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
1196 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
1197 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
1198 logpal
->palPalEntry
[i
].peFlags
= PC_NOCOLLAPSE
;
1200 pal
= CreatePalette(logpal
);
1202 SelectPalette(hdc
, pal
, FALSE
);
1203 RealizePalette(hdc
);
1204 SelectPalette(hdc
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
1207 ReleaseDC(hwnd
, hdc
);
1210 for (i
= 0; i
< NALLCOLOURS
; i
++)
1211 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
1212 defpal
[i
].rgbtGreen
,
1213 defpal
[i
].rgbtBlue
);
1215 for (i
= 0; i
< NALLCOLOURS
; i
++)
1216 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
1217 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
1221 * This is a wrapper to ExtTextOut() to force Windows to display
1222 * the precise glyphs we give it. Otherwise it would do its own
1223 * bidi and Arabic shaping, and we would end up uncertain which
1224 * characters it had put where.
1226 static void exact_textout(HDC hdc
, int x
, int y
, CONST RECT
*lprc
,
1227 unsigned short *lpString
, UINT cbCount
,
1228 CONST INT
*lpDx
, int opaque
)
1232 * The LCC include files apparently don't supply the
1233 * GCP_RESULTSW type, but we can make do with GCP_RESULTS
1234 * proper: the differences aren't important to us (the only
1235 * variable-width string parameter is one we don't use anyway).
1241 char *buffer
= snewn(cbCount
*2+2, char);
1242 char *classbuffer
= snewn(cbCount
, char);
1243 memset(&gcpr
, 0, sizeof(gcpr
));
1244 memset(buffer
, 0, cbCount
*2+2);
1245 memset(classbuffer
, GCPCLASS_NEUTRAL
, cbCount
);
1247 gcpr
.lStructSize
= sizeof(gcpr
);
1248 gcpr
.lpGlyphs
= (void *)buffer
;
1249 gcpr
.lpClass
= classbuffer
;
1250 gcpr
.nGlyphs
= cbCount
;
1252 GetCharacterPlacementW(hdc
, lpString
, cbCount
, 0, &gcpr
,
1253 FLI_MASK
| GCP_CLASSIN
| GCP_DIACRITIC
);
1255 ExtTextOut(hdc
, x
, y
,
1256 ETO_GLYPH_INDEX
| ETO_CLIPPED
| (opaque ? ETO_OPAQUE
: 0),
1257 lprc
, buffer
, cbCount
, lpDx
);
1261 * Initialise all the fonts we will need initially. There may be as many as
1262 * three or as few as one. The other (poentially) twentyone fonts are done
1263 * if/when they are needed.
1267 * - check the font width and height, correcting our guesses if
1270 * - verify that the bold font is the same width as the ordinary
1271 * one, and engage shadow bolding if not.
1273 * - verify that the underlined font is the same width as the
1274 * ordinary one (manual underlining by means of line drawing can
1275 * be done in a pinch).
1277 static void init_fonts(int pick_width
, int pick_height
)
1284 int fw_dontcare
, fw_bold
;
1286 for (i
= 0; i
< FONT_MAXNO
; i
++)
1289 bold_mode
= cfg
.bold_colour ? BOLD_COLOURS
: BOLD_FONT
;
1290 und_mode
= UND_FONT
;
1292 if (cfg
.font
.isbold
) {
1293 fw_dontcare
= FW_BOLD
;
1296 fw_dontcare
= FW_DONTCARE
;
1303 font_height
= pick_height
;
1305 font_height
= cfg
.font
.height
;
1306 if (font_height
> 0) {
1308 -MulDiv(font_height
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1311 font_width
= pick_width
;
1313 #define f(i,c,w,u) \
1314 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1315 c, OUT_DEFAULT_PRECIS, \
1316 CLIP_DEFAULT_PRECIS, FONT_QUALITY(cfg.font_quality), \
1317 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1319 f(FONT_NORMAL
, cfg
.font
.charset
, fw_dontcare
, FALSE
);
1321 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
1322 GetTextMetrics(hdc
, &tm
);
1324 GetObject(fonts
[FONT_NORMAL
], sizeof(LOGFONT
), &lfont
);
1326 if (pick_width
== 0 || pick_height
== 0) {
1327 font_height
= tm
.tmHeight
;
1328 font_width
= tm
.tmAveCharWidth
;
1330 font_dualwidth
= (tm
.tmAveCharWidth
!= tm
.tmMaxCharWidth
);
1332 #ifdef RDB_DEBUG_PATCH
1333 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1334 tm
.tmHeight
, tm
.tmAveCharWidth
, tm
.tmMaxCharWidth
);
1339 DWORD cset
= tm
.tmCharSet
;
1340 memset(&info
, 0xFF, sizeof(info
));
1342 /* !!! Yes the next line is right */
1343 if (cset
== OEM_CHARSET
)
1344 ucsdata
.font_codepage
= GetOEMCP();
1346 if (TranslateCharsetInfo ((DWORD
*) cset
, &info
, TCI_SRCCHARSET
))
1347 ucsdata
.font_codepage
= info
.ciACP
;
1349 ucsdata
.font_codepage
= -1;
1351 GetCPInfo(ucsdata
.font_codepage
, &cpinfo
);
1352 ucsdata
.dbcs_screenfont
= (cpinfo
.MaxCharSize
> 1);
1355 f(FONT_UNDERLINE
, cfg
.font
.charset
, fw_dontcare
, TRUE
);
1358 * Some fonts, e.g. 9-pt Courier, draw their underlines
1359 * outside their character cell. We successfully prevent
1360 * screen corruption by clipping the text output, but then
1361 * we lose the underline completely. Here we try to work
1362 * out whether this is such a font, and if it is, we set a
1363 * flag that causes underlines to be drawn by hand.
1365 * Having tried other more sophisticated approaches (such
1366 * as examining the TEXTMETRIC structure or requesting the
1367 * height of a string), I think we'll do this the brute
1368 * force way: we create a small bitmap, draw an underlined
1369 * space on it, and test to see whether any pixels are
1370 * foreground-coloured. (Since we expect the underline to
1371 * go all the way across the character cell, we only search
1372 * down a single column of the bitmap, half way across.)
1376 HBITMAP und_bm
, und_oldbm
;
1380 und_dc
= CreateCompatibleDC(hdc
);
1381 und_bm
= CreateCompatibleBitmap(hdc
, font_width
, font_height
);
1382 und_oldbm
= SelectObject(und_dc
, und_bm
);
1383 SelectObject(und_dc
, fonts
[FONT_UNDERLINE
]);
1384 SetTextAlign(und_dc
, TA_TOP
| TA_LEFT
| TA_NOUPDATECP
);
1385 SetTextColor(und_dc
, RGB(255, 255, 255));
1386 SetBkColor(und_dc
, RGB(0, 0, 0));
1387 SetBkMode(und_dc
, OPAQUE
);
1388 ExtTextOut(und_dc
, 0, 0, ETO_OPAQUE
, NULL
, " ", 1, NULL
);
1390 for (i
= 0; i
< font_height
; i
++) {
1391 c
= GetPixel(und_dc
, font_width
/ 2, i
);
1392 if (c
!= RGB(0, 0, 0))
1395 SelectObject(und_dc
, und_oldbm
);
1396 DeleteObject(und_bm
);
1399 und_mode
= UND_LINE
;
1400 DeleteObject(fonts
[FONT_UNDERLINE
]);
1401 fonts
[FONT_UNDERLINE
] = 0;
1405 if (bold_mode
== BOLD_FONT
) {
1406 f(FONT_BOLD
, cfg
.font
.charset
, fw_bold
, FALSE
);
1410 descent
= tm
.tmAscent
+ 1;
1411 if (descent
>= font_height
)
1412 descent
= font_height
- 1;
1414 for (i
= 0; i
< 3; i
++) {
1416 if (SelectObject(hdc
, fonts
[i
]) && GetTextMetrics(hdc
, &tm
))
1417 fontsize
[i
] = tm
.tmAveCharWidth
+ 256 * tm
.tmHeight
;
1424 ReleaseDC(hwnd
, hdc
);
1426 if (fontsize
[FONT_UNDERLINE
] != fontsize
[FONT_NORMAL
]) {
1427 und_mode
= UND_LINE
;
1428 DeleteObject(fonts
[FONT_UNDERLINE
]);
1429 fonts
[FONT_UNDERLINE
] = 0;
1432 if (bold_mode
== BOLD_FONT
&&
1433 fontsize
[FONT_BOLD
] != fontsize
[FONT_NORMAL
]) {
1434 bold_mode
= BOLD_SHADOW
;
1435 DeleteObject(fonts
[FONT_BOLD
]);
1436 fonts
[FONT_BOLD
] = 0;
1438 fontflag
[0] = fontflag
[1] = fontflag
[2] = 1;
1440 init_ucs(&cfg
, &ucsdata
);
1443 static void another_font(int fontno
)
1446 int fw_dontcare
, fw_bold
;
1450 if (fontno
< 0 || fontno
>= FONT_MAXNO
|| fontflag
[fontno
])
1453 basefont
= (fontno
& ~(FONT_BOLDUND
));
1454 if (basefont
!= fontno
&& !fontflag
[basefont
])
1455 another_font(basefont
);
1457 if (cfg
.font
.isbold
) {
1458 fw_dontcare
= FW_BOLD
;
1461 fw_dontcare
= FW_DONTCARE
;
1465 c
= cfg
.font
.charset
;
1471 if (fontno
& FONT_WIDE
)
1473 if (fontno
& FONT_NARROW
)
1475 if (fontno
& FONT_OEM
)
1477 if (fontno
& FONT_BOLD
)
1479 if (fontno
& FONT_UNDERLINE
)
1483 CreateFont(font_height
* (1 + !!(fontno
& FONT_HIGH
)), x
, 0, 0, w
,
1484 FALSE
, u
, FALSE
, c
, OUT_DEFAULT_PRECIS
,
1485 CLIP_DEFAULT_PRECIS
, FONT_QUALITY(cfg
.font_quality
),
1486 FIXED_PITCH
| FF_DONTCARE
, s
);
1488 fontflag
[fontno
] = 1;
1491 static void deinit_fonts(void)
1494 for (i
= 0; i
< FONT_MAXNO
; i
++) {
1496 DeleteObject(fonts
[i
]);
1502 void request_resize(void *frontend
, int w
, int h
)
1506 /* If the window is maximized supress resizing attempts */
1507 if (IsZoomed(hwnd
)) {
1508 if (cfg
.resize_action
== RESIZE_TERM
)
1512 if (cfg
.resize_action
== RESIZE_DISABLED
) return;
1513 if (h
== term
->rows
&& w
== term
->cols
) return;
1515 /* Sanity checks ... */
1517 static int first_time
= 1;
1520 switch (first_time
) {
1522 /* Get the size of the screen */
1523 if (get_fullscreen_rect(&ss
))
1524 /* first_time = 0 */ ;
1530 /* Make sure the values are sane */
1531 width
= (ss
.right
- ss
.left
- extra_width
) / 4;
1532 height
= (ss
.bottom
- ss
.top
- extra_height
) / 6;
1534 if (w
> width
|| h
> height
)
1543 term_size(term
, h
, w
, cfg
.savelines
);
1545 if (cfg
.resize_action
!= RESIZE_FONT
&& !IsZoomed(hwnd
)) {
1546 width
= extra_width
+ font_width
* w
;
1547 height
= extra_height
+ font_height
* h
;
1549 SetWindowPos(hwnd
, NULL
, 0, 0, width
, height
,
1550 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1551 SWP_NOMOVE
| SWP_NOZORDER
);
1555 InvalidateRect(hwnd
, NULL
, TRUE
);
1558 static void reset_window(int reinit
) {
1560 * This function decides how to resize or redraw when the
1561 * user changes something.
1563 * This function doesn't like to change the terminal size but if the
1564 * font size is locked that may be it's only soluion.
1566 int win_width
, win_height
;
1569 #ifdef RDB_DEBUG_PATCH
1570 debug((27, "reset_window()"));
1573 /* Current window sizes ... */
1574 GetWindowRect(hwnd
, &wr
);
1575 GetClientRect(hwnd
, &cr
);
1577 win_width
= cr
.right
- cr
.left
;
1578 win_height
= cr
.bottom
- cr
.top
;
1580 if (cfg
.resize_action
== RESIZE_DISABLED
) reinit
= 2;
1582 /* Are we being forced to reload the fonts ? */
1584 #ifdef RDB_DEBUG_PATCH
1585 debug((27, "reset_window() -- Forced deinit"));
1591 /* Oh, looks like we're minimised */
1592 if (win_width
== 0 || win_height
== 0)
1595 /* Is the window out of position ? */
1597 (offset_width
!= (win_width
-font_width
*term
->cols
)/2 ||
1598 offset_height
!= (win_height
-font_height
*term
->rows
)/2) ){
1599 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1600 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1601 InvalidateRect(hwnd
, NULL
, TRUE
);
1602 #ifdef RDB_DEBUG_PATCH
1603 debug((27, "reset_window() -> Reposition terminal"));
1607 if (IsZoomed(hwnd
)) {
1608 /* We're fullscreen, this means we must not change the size of
1609 * the window so it's the font size or the terminal itself.
1612 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
;
1613 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
;
1615 if (cfg
.resize_action
!= RESIZE_TERM
) {
1616 if ( font_width
!= win_width
/term
->cols
||
1617 font_height
!= win_height
/term
->rows
) {
1619 init_fonts(win_width
/term
->cols
, win_height
/term
->rows
);
1620 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1621 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1622 InvalidateRect(hwnd
, NULL
, TRUE
);
1623 #ifdef RDB_DEBUG_PATCH
1624 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1625 font_width
, font_height
));
1629 if ( font_width
* term
->cols
!= win_width
||
1630 font_height
* term
->rows
!= win_height
) {
1631 /* Our only choice at this point is to change the
1632 * size of the terminal; Oh well.
1634 term_size(term
, win_height
/font_height
, win_width
/font_width
,
1636 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1637 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1638 InvalidateRect(hwnd
, NULL
, TRUE
);
1639 #ifdef RDB_DEBUG_PATCH
1640 debug((27, "reset_window() -> Zoomed term_size"));
1647 /* Hmm, a force re-init means we should ignore the current window
1648 * so we resize to the default font size.
1651 #ifdef RDB_DEBUG_PATCH
1652 debug((27, "reset_window() -> Forced re-init"));
1655 offset_width
= offset_height
= cfg
.window_border
;
1656 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1657 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1659 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1660 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1662 /* If this is too large windows will resize it to the maximum
1663 * allowed window size, we will then be back in here and resize
1664 * the font or terminal to fit.
1666 SetWindowPos(hwnd
, NULL
, 0, 0,
1667 font_width
*term
->cols
+ extra_width
,
1668 font_height
*term
->rows
+ extra_height
,
1669 SWP_NOMOVE
| SWP_NOZORDER
);
1672 InvalidateRect(hwnd
, NULL
, TRUE
);
1676 /* Okay the user doesn't want us to change the font so we try the
1677 * window. But that may be too big for the screen which forces us
1678 * to change the terminal.
1680 if ((cfg
.resize_action
== RESIZE_TERM
&& reinit
<=0) ||
1681 (cfg
.resize_action
== RESIZE_EITHER
&& reinit
<0) ||
1683 offset_width
= offset_height
= cfg
.window_border
;
1684 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1685 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1687 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1688 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1693 get_fullscreen_rect(&ss
);
1695 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1696 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1699 if ( term
->rows
> height
|| term
->cols
> width
) {
1700 if (cfg
.resize_action
== RESIZE_EITHER
) {
1701 /* Make the font the biggest we can */
1702 if (term
->cols
> width
)
1703 font_width
= (ss
.right
- ss
.left
- extra_width
)
1705 if (term
->rows
> height
)
1706 font_height
= (ss
.bottom
- ss
.top
- extra_height
)
1710 init_fonts(font_width
, font_height
);
1712 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1713 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1715 if ( height
> term
->rows
) height
= term
->rows
;
1716 if ( width
> term
->cols
) width
= term
->cols
;
1717 term_size(term
, height
, width
, cfg
.savelines
);
1718 #ifdef RDB_DEBUG_PATCH
1719 debug((27, "reset_window() -> term resize to (%d,%d)",
1725 SetWindowPos(hwnd
, NULL
, 0, 0,
1726 font_width
*term
->cols
+ extra_width
,
1727 font_height
*term
->rows
+ extra_height
,
1728 SWP_NOMOVE
| SWP_NOZORDER
);
1730 InvalidateRect(hwnd
, NULL
, TRUE
);
1731 #ifdef RDB_DEBUG_PATCH
1732 debug((27, "reset_window() -> window resize to (%d,%d)",
1733 font_width
*term
->cols
+ extra_width
,
1734 font_height
*term
->rows
+ extra_height
));
1740 /* We're allowed to or must change the font but do we want to ? */
1742 if (font_width
!= (win_width
-cfg
.window_border
*2)/term
->cols
||
1743 font_height
!= (win_height
-cfg
.window_border
*2)/term
->rows
) {
1746 init_fonts((win_width
-cfg
.window_border
*2)/term
->cols
,
1747 (win_height
-cfg
.window_border
*2)/term
->rows
);
1748 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1749 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1751 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+offset_width
*2;
1752 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1754 InvalidateRect(hwnd
, NULL
, TRUE
);
1755 #ifdef RDB_DEBUG_PATCH
1756 debug((25, "reset_window() -> font resize to (%d,%d)",
1757 font_width
, font_height
));
1762 static void set_input_locale(HKL kl
)
1766 GetLocaleInfo(LOWORD(kl
), LOCALE_IDEFAULTANSICODEPAGE
,
1767 lbuf
, sizeof(lbuf
));
1769 kbd_codepage
= atoi(lbuf
);
1772 static void click(Mouse_Button b
, int x
, int y
, int shift
, int ctrl
, int alt
)
1774 int thistime
= GetMessageTime();
1776 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
)) {
1777 lastbtn
= MBT_NOTHING
;
1778 term_mouse(term
, b
, translate_button(b
), MA_CLICK
,
1779 x
, y
, shift
, ctrl
, alt
);
1783 if (lastbtn
== b
&& thistime
- lasttime
< dbltime
) {
1784 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
1785 lastact
== MA_2CLK ? MA_3CLK
:
1786 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
1791 if (lastact
!= MA_NOTHING
)
1792 term_mouse(term
, b
, translate_button(b
), lastact
,
1793 x
, y
, shift
, ctrl
, alt
);
1794 lasttime
= thistime
;
1798 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1799 * into a cooked one (SELECT, EXTEND, PASTE).
1801 static Mouse_Button
translate_button(Mouse_Button button
)
1803 if (button
== MBT_LEFT
)
1805 if (button
== MBT_MIDDLE
)
1806 return cfg
.mouse_is_xterm
== 1 ? MBT_PASTE
: MBT_EXTEND
;
1807 if (button
== MBT_RIGHT
)
1808 return cfg
.mouse_is_xterm
== 1 ? MBT_EXTEND
: MBT_PASTE
;
1809 return 0; /* shouldn't happen */
1812 static void show_mouseptr(int show
)
1814 /* NB that the counter in ShowCursor() is also frobbed by
1815 * update_mouse_pointer() */
1816 static int cursor_visible
= 1;
1817 if (!cfg
.hide_mouseptr
) /* override if this feature disabled */
1819 if (cursor_visible
&& !show
)
1821 else if (!cursor_visible
&& show
)
1823 cursor_visible
= show
;
1826 static int is_alt_pressed(void)
1829 int r
= GetKeyboardState(keystate
);
1832 if (keystate
[VK_MENU
] & 0x80)
1834 if (keystate
[VK_RMENU
] & 0x80)
1839 static int is_shift_pressed(void)
1842 int r
= GetKeyboardState(keystate
);
1845 if (keystate
[VK_SHIFT
] & 0x80)
1850 static int resizing
;
1852 void notify_remote_exit(void *fe
) { /* stub not needed in this frontend */ }
1854 void timer_change_notify(long next
)
1856 long ticks
= next
- GETTICKCOUNT();
1857 if (ticks
<= 0) ticks
= 1; /* just in case */
1858 KillTimer(hwnd
, TIMING_TIMER_ID
);
1859 SetTimer(hwnd
, TIMING_TIMER_ID
, ticks
, NULL
);
1860 timing_next_time
= next
;
1863 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1864 WPARAM wParam
, LPARAM lParam
)
1867 static int ignore_clip
= FALSE
;
1868 static int need_backend_resize
= FALSE
;
1869 static int fullscr_on_max
= FALSE
;
1870 static UINT last_mousemove
= 0;
1874 if ((UINT_PTR
)wParam
== TIMING_TIMER_ID
) {
1877 KillTimer(hwnd
, TIMING_TIMER_ID
);
1878 if (run_timers(timing_next_time
, &next
)) {
1879 timer_change_notify(next
);
1890 str
= dupprintf("%s Exit Confirmation", appname
);
1891 if (!cfg
.warn_on_close
|| session_closed
||
1893 "Are you sure you want to close this session?",
1894 str
, MB_ICONWARNING
| MB_OKCANCEL
| MB_DEFBUTTON1
)
1896 DestroyWindow(hwnd
);
1904 case WM_INITMENUPOPUP
:
1905 if ((HMENU
)wParam
== savedsess_menu
) {
1906 /* About to pop up Saved Sessions sub-menu.
1907 * Refresh the session list. */
1908 get_sesslist(&sesslist
, FALSE
); /* free */
1909 get_sesslist(&sesslist
, TRUE
);
1910 update_savedsess_menu();
1916 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1927 BOOL inherit_handles
;
1929 PROCESS_INFORMATION pi
;
1930 HANDLE filemap
= NULL
;
1932 if (wParam
== IDM_DUPSESS
) {
1934 * Allocate a file-mapping memory chunk for the
1937 SECURITY_ATTRIBUTES sa
;
1940 sa
.nLength
= sizeof(sa
);
1941 sa
.lpSecurityDescriptor
= NULL
;
1942 sa
.bInheritHandle
= TRUE
;
1943 filemap
= CreateFileMapping((HANDLE
) 0xFFFFFFFF,
1946 0, sizeof(Config
), NULL
);
1948 p
= (Config
*) MapViewOfFile(filemap
,
1950 0, 0, sizeof(Config
));
1952 *p
= cfg
; /* structure copy */
1956 inherit_handles
= TRUE
;
1957 sprintf(c
, "putty &%p", filemap
);
1959 } else if (wParam
== IDM_SAVEDSESS
) {
1960 unsigned int sessno
= ((lParam
- IDM_SAVED_MIN
)
1961 / MENU_SAVED_STEP
) + 1;
1962 if (sessno
< sesslist
.nsessions
) {
1963 char *session
= sesslist
.sessions
[sessno
];
1964 /* XXX spaces? quotes? "-load"? */
1965 cl
= dupprintf("putty @%s", session
);
1966 inherit_handles
= FALSE
;
1970 } else /* IDM_NEWSESS */ {
1972 inherit_handles
= FALSE
;
1975 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
1977 si
.lpReserved
= NULL
;
1978 si
.lpDesktop
= NULL
;
1982 si
.lpReserved2
= NULL
;
1983 CreateProcess(b
, cl
, NULL
, NULL
, inherit_handles
,
1984 NORMAL_PRIORITY_CLASS
, NULL
, NULL
, &si
, &pi
);
1987 CloseHandle(filemap
);
1994 logevent(NULL
, "----- Session restarted -----");
1995 term_pwron(term
, FALSE
);
2004 int reconfig_result
;
2009 reconfiguring
= TRUE
;
2011 GetWindowText(hwnd
, cfg
.wintitle
, sizeof(cfg
.wintitle
));
2015 do_reconfig(hwnd
, back ? back
->cfg_info(backhandle
) : 0);
2016 reconfiguring
= FALSE
;
2017 if (!reconfig_result
)
2021 /* Disable full-screen if resizing forbidden */
2022 HMENU m
= GetSystemMenu (hwnd
, FALSE
);
2023 EnableMenuItem(m
, IDM_FULLSCREEN
, MF_BYCOMMAND
|
2024 (cfg
.resize_action
== RESIZE_DISABLED
)
2025 ? MF_GRAYED
: MF_ENABLED
);
2026 /* Gracefully unzoom if necessary */
2027 if (IsZoomed(hwnd
) &&
2028 (cfg
.resize_action
== RESIZE_DISABLED
)) {
2029 ShowWindow(hwnd
, SW_RESTORE
);
2033 /* Pass new config data to the logging module */
2034 log_reconfig(logctx
, &cfg
);
2038 * Flush the line discipline's edit buffer in the
2039 * case where local editing has just been disabled.
2042 ldisc_send(ldisc
, NULL
, 0, 0);
2050 /* Pass new config data to the terminal */
2051 term_reconfig(term
, &cfg
);
2053 /* Pass new config data to the back end */
2055 back
->reconfig(backhandle
, &cfg
);
2057 /* Screen size changed ? */
2058 if (cfg
.height
!= prev_cfg
.height
||
2059 cfg
.width
!= prev_cfg
.width
||
2060 cfg
.savelines
!= prev_cfg
.savelines
||
2061 cfg
.resize_action
== RESIZE_FONT
||
2062 (cfg
.resize_action
== RESIZE_EITHER
&& IsZoomed(hwnd
)) ||
2063 cfg
.resize_action
== RESIZE_DISABLED
)
2064 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2066 /* Enable or disable the scroll bar, etc */
2068 LONG nflg
, flag
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
2069 LONG nexflag
, exflag
=
2070 GetWindowLongPtr(hwnd
, GWL_EXSTYLE
);
2073 if (cfg
.alwaysontop
!= prev_cfg
.alwaysontop
) {
2074 if (cfg
.alwaysontop
) {
2075 nexflag
|= WS_EX_TOPMOST
;
2076 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
2077 SWP_NOMOVE
| SWP_NOSIZE
);
2079 nexflag
&= ~(WS_EX_TOPMOST
);
2080 SetWindowPos(hwnd
, HWND_NOTOPMOST
, 0, 0, 0, 0,
2081 SWP_NOMOVE
| SWP_NOSIZE
);
2084 if (cfg
.sunken_edge
)
2085 nexflag
|= WS_EX_CLIENTEDGE
;
2087 nexflag
&= ~(WS_EX_CLIENTEDGE
);
2090 if (is_full_screen() ?
2091 cfg
.scrollbar_in_fullscreen
: cfg
.scrollbar
)
2094 nflg
&= ~WS_VSCROLL
;
2096 if (cfg
.resize_action
== RESIZE_DISABLED
||
2098 nflg
&= ~WS_THICKFRAME
;
2100 nflg
|= WS_THICKFRAME
;
2102 if (cfg
.resize_action
== RESIZE_DISABLED
)
2103 nflg
&= ~WS_MAXIMIZEBOX
;
2105 nflg
|= WS_MAXIMIZEBOX
;
2107 if (nflg
!= flag
|| nexflag
!= exflag
) {
2109 SetWindowLongPtr(hwnd
, GWL_STYLE
, nflg
);
2110 if (nexflag
!= exflag
)
2111 SetWindowLongPtr(hwnd
, GWL_EXSTYLE
, nexflag
);
2113 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
2114 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
2115 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
2123 if (cfg
.resize_action
== RESIZE_DISABLED
&& IsZoomed(hwnd
)) {
2128 set_title(NULL
, cfg
.wintitle
);
2129 if (IsIconic(hwnd
)) {
2131 cfg
.win_name_always ? window_name
:
2135 if (strcmp(cfg
.font
.name
, prev_cfg
.font
.name
) != 0 ||
2136 strcmp(cfg
.line_codepage
, prev_cfg
.line_codepage
) != 0 ||
2137 cfg
.font
.isbold
!= prev_cfg
.font
.isbold
||
2138 cfg
.font
.height
!= prev_cfg
.font
.height
||
2139 cfg
.font
.charset
!= prev_cfg
.font
.charset
||
2140 cfg
.font_quality
!= prev_cfg
.font_quality
||
2141 cfg
.vtmode
!= prev_cfg
.vtmode
||
2142 cfg
.bold_colour
!= prev_cfg
.bold_colour
||
2143 cfg
.resize_action
== RESIZE_DISABLED
||
2144 cfg
.resize_action
== RESIZE_EITHER
||
2145 (cfg
.resize_action
!= prev_cfg
.resize_action
))
2148 InvalidateRect(hwnd
, NULL
, TRUE
);
2149 reset_window(init_lvl
);
2150 net_pending_errors();
2157 term_do_paste(term
);
2163 term_pwron(term
, TRUE
);
2165 ldisc_send(ldisc
, NULL
, 0, 0);
2171 WinHelp(hwnd
, help_path
,
2172 help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
2176 * We get this if the System menu has been activated
2183 * We get this if the System menu has been activated
2184 * using the keyboard. This might happen from within
2185 * TranslateKey, in which case it really wants to be
2186 * followed by a `space' character to actually _bring
2187 * the menu up_ rather than just sitting there in
2188 * `ready to appear' state.
2190 show_mouseptr(1); /* make sure pointer is visible */
2192 PostMessage(hwnd
, WM_CHAR
, ' ', 0);
2194 case IDM_FULLSCREEN
:
2198 if (wParam
>= IDM_SAVED_MIN
&& wParam
< IDM_SAVED_MAX
) {
2199 SendMessage(hwnd
, WM_SYSCOMMAND
, IDM_SAVEDSESS
, wParam
);
2201 if (wParam
>= IDM_SPECIAL_MIN
&& wParam
<= IDM_SPECIAL_MAX
) {
2202 int i
= (wParam
- IDM_SPECIAL_MIN
) / 0x10;
2204 * Ensure we haven't been sent a bogus SYSCOMMAND
2205 * which would cause us to reference invalid memory
2206 * and crash. Perhaps I'm just too paranoid here.
2208 if (i
>= n_specials
)
2211 back
->special(backhandle
, specials
[i
].code
);
2212 net_pending_errors();
2217 #define X_POS(l) ((int)(short)LOWORD(l))
2218 #define Y_POS(l) ((int)(short)HIWORD(l))
2220 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2221 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2222 case WM_LBUTTONDOWN
:
2223 case WM_MBUTTONDOWN
:
2224 case WM_RBUTTONDOWN
:
2228 if (message
== WM_RBUTTONDOWN
&&
2229 ((wParam
& MK_CONTROL
) || (cfg
.mouse_is_xterm
== 2))) {
2232 show_mouseptr(1); /* make sure pointer is visible */
2233 GetCursorPos(&cursorpos
);
2234 TrackPopupMenu(popup_menus
[CTXMENU
].menu
,
2235 TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
2236 cursorpos
.x
, cursorpos
.y
,
2244 case WM_LBUTTONDOWN
:
2248 case WM_MBUTTONDOWN
:
2249 button
= MBT_MIDDLE
;
2252 case WM_RBUTTONDOWN
:
2261 button
= MBT_MIDDLE
;
2269 button
= press
= 0; /* shouldn't happen */
2273 * Special case: in full-screen mode, if the left
2274 * button is clicked in the very top left corner of the
2275 * window, we put up the System menu instead of doing
2279 char mouse_on_hotspot
= 0;
2288 mon
= MonitorFromPoint(pt
, MONITOR_DEFAULTTONULL
);
2291 mi
.cbSize
= sizeof(MONITORINFO
);
2292 GetMonitorInfo(mon
, &mi
);
2294 if (mi
.rcMonitor
.left
== pt
.x
&&
2295 mi
.rcMonitor
.top
== pt
.y
) {
2296 mouse_on_hotspot
= 1;
2301 if (pt
.x
== 0 && pt
.y
== 0) {
2302 mouse_on_hotspot
= 1;
2305 if (is_full_screen() && press
&&
2306 button
== MBT_LEFT
&& mouse_on_hotspot
) {
2307 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MOUSEMENU
,
2308 MAKELPARAM(pt
.x
, pt
.y
));
2315 TO_CHR_X(X_POS(lParam
)), TO_CHR_Y(Y_POS(lParam
)),
2316 wParam
& MK_SHIFT
, wParam
& MK_CONTROL
,
2320 term_mouse(term
, button
, translate_button(button
), MA_RELEASE
,
2321 TO_CHR_X(X_POS(lParam
)),
2322 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2323 wParam
& MK_CONTROL
, is_alt_pressed());
2331 * Windows seems to like to occasionally send MOUSEMOVE
2332 * events even if the mouse hasn't moved. Don't unhide
2333 * the mouse pointer in this case.
2335 static WPARAM wp
= 0;
2336 static LPARAM lp
= 0;
2337 if (wParam
!= wp
|| lParam
!= lp
||
2338 last_mousemove
!= WM_MOUSEMOVE
) {
2340 wp
= wParam
; lp
= lParam
;
2341 last_mousemove
= WM_MOUSEMOVE
;
2345 * Add the mouse position and message time to the random
2348 noise_ultralight(lParam
);
2350 if (wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
) &&
2351 GetCapture() == hwnd
) {
2353 if (wParam
& MK_LBUTTON
)
2355 else if (wParam
& MK_MBUTTON
)
2359 term_mouse(term
, b
, translate_button(b
), MA_DRAG
,
2360 TO_CHR_X(X_POS(lParam
)),
2361 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2362 wParam
& MK_CONTROL
, is_alt_pressed());
2365 case WM_NCMOUSEMOVE
:
2367 static WPARAM wp
= 0;
2368 static LPARAM lp
= 0;
2369 if (wParam
!= wp
|| lParam
!= lp
||
2370 last_mousemove
!= WM_NCMOUSEMOVE
) {
2372 wp
= wParam
; lp
= lParam
;
2373 last_mousemove
= WM_NCMOUSEMOVE
;
2376 noise_ultralight(lParam
);
2378 case WM_IGNORE_CLIP
:
2379 ignore_clip
= wParam
; /* don't panic on DESTROYCLIPBOARD */
2381 case WM_DESTROYCLIPBOARD
:
2383 term_deselect(term
);
2384 ignore_clip
= FALSE
;
2391 hdc
= BeginPaint(hwnd
, &p
);
2393 SelectPalette(hdc
, pal
, TRUE
);
2394 RealizePalette(hdc
);
2398 * We have to be careful about term_paint(). It will
2399 * set a bunch of character cells to INVALID and then
2400 * call do_paint(), which will redraw those cells and
2401 * _then mark them as done_. This may not be accurate:
2402 * when painting in WM_PAINT context we are restricted
2403 * to the rectangle which has just been exposed - so if
2404 * that only covers _part_ of a character cell and the
2405 * rest of it was already visible, that remainder will
2406 * not be redrawn at all. Accordingly, we must not
2407 * paint any character cell in a WM_PAINT context which
2408 * already has a pending update due to terminal output.
2409 * The simplest solution to this - and many, many
2410 * thanks to Hung-Te Lin for working all this out - is
2411 * not to do any actual painting at _all_ if there's a
2412 * pending terminal update: just mark the relevant
2413 * character cells as INVALID and wait for the
2414 * scheduled full update to sort it out.
2416 * I have a suspicion this isn't the _right_ solution.
2417 * An alternative approach would be to have terminal.c
2418 * separately track what _should_ be on the terminal
2419 * screen and what _is_ on the terminal screen, and
2420 * have two completely different types of redraw (one
2421 * for full updates, which syncs the former with the
2422 * terminal itself, and one for WM_PAINT which syncs
2423 * the latter with the former); yet another possibility
2424 * would be to have the Windows front end do what the
2425 * GTK one already does, and maintain a bitmap of the
2426 * current terminal appearance so that WM_PAINT becomes
2427 * completely trivial. However, this should do for now.
2429 term_paint(term
, hdc
,
2430 (p
.rcPaint
.left
-offset_width
)/font_width
,
2431 (p
.rcPaint
.top
-offset_height
)/font_height
,
2432 (p
.rcPaint
.right
-offset_width
-1)/font_width
,
2433 (p
.rcPaint
.bottom
-offset_height
-1)/font_height
,
2434 !term
->window_update_pending
);
2437 p
.rcPaint
.left
< offset_width
||
2438 p
.rcPaint
.top
< offset_height
||
2439 p
.rcPaint
.right
>= offset_width
+ font_width
*term
->cols
||
2440 p
.rcPaint
.bottom
>= offset_height
+ font_height
*term
->rows
)
2442 HBRUSH fillcolour
, oldbrush
;
2444 fillcolour
= CreateSolidBrush (
2445 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2446 oldbrush
= SelectObject(hdc
, fillcolour
);
2447 edge
= CreatePen(PS_SOLID
, 0,
2448 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2449 oldpen
= SelectObject(hdc
, edge
);
2452 * Jordan Russell reports that this apparently
2453 * ineffectual IntersectClipRect() call masks a
2454 * Windows NT/2K bug causing strange display
2455 * problems when the PuTTY window is taller than
2456 * the primary monitor. It seems harmless enough...
2458 IntersectClipRect(hdc
,
2459 p
.rcPaint
.left
, p
.rcPaint
.top
,
2460 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2462 ExcludeClipRect(hdc
,
2463 offset_width
, offset_height
,
2464 offset_width
+font_width
*term
->cols
,
2465 offset_height
+font_height
*term
->rows
);
2467 Rectangle(hdc
, p
.rcPaint
.left
, p
.rcPaint
.top
,
2468 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2470 /* SelectClipRgn(hdc, NULL); */
2472 SelectObject(hdc
, oldbrush
);
2473 DeleteObject(fillcolour
);
2474 SelectObject(hdc
, oldpen
);
2477 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
2478 SelectObject(hdc
, GetStockObject(WHITE_PEN
));
2484 enact_netevent(wParam
, lParam
);
2485 net_pending_errors();
2488 term_set_focus(term
, TRUE
);
2489 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
2491 flash_window(0); /* stop */
2497 term_set_focus(term
, FALSE
);
2499 caret_x
= caret_y
= -1; /* ensure caret is replaced next time */
2502 case WM_ENTERSIZEMOVE
:
2503 #ifdef RDB_DEBUG_PATCH
2504 debug((27, "WM_ENTERSIZEMOVE"));
2508 need_backend_resize
= FALSE
;
2510 case WM_EXITSIZEMOVE
:
2513 #ifdef RDB_DEBUG_PATCH
2514 debug((27, "WM_EXITSIZEMOVE"));
2516 if (need_backend_resize
) {
2517 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2518 InvalidateRect(hwnd
, NULL
, TRUE
);
2523 * This does two jobs:
2524 * 1) Keep the sizetip uptodate
2525 * 2) Make sure the window size is _stepped_ in units of the font size.
2527 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2528 int width
, height
, w
, h
, ew
, eh
;
2529 LPRECT r
= (LPRECT
) lParam
;
2531 if ( !need_backend_resize
&& cfg
.resize_action
== RESIZE_EITHER
&&
2532 (cfg
.height
!= term
->rows
|| cfg
.width
!= term
->cols
)) {
2534 * Great! It seems that both the terminal size and the
2535 * font size have been changed and the user is now dragging.
2537 * It will now be difficult to get back to the configured
2540 * This would be easier but it seems to be too confusing.
2542 term_size(term, cfg.height, cfg.width, cfg.savelines);
2545 cfg
.height
=term
->rows
; cfg
.width
=term
->cols
;
2547 InvalidateRect(hwnd
, NULL
, TRUE
);
2548 need_backend_resize
= TRUE
;
2551 width
= r
->right
- r
->left
- extra_width
;
2552 height
= r
->bottom
- r
->top
- extra_height
;
2553 w
= (width
+ font_width
/ 2) / font_width
;
2556 h
= (height
+ font_height
/ 2) / font_height
;
2559 UpdateSizeTip(hwnd
, w
, h
);
2560 ew
= width
- w
* font_width
;
2561 eh
= height
- h
* font_height
;
2563 if (wParam
== WMSZ_LEFT
||
2564 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2570 if (wParam
== WMSZ_TOP
||
2571 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2581 int width
, height
, w
, h
, rv
= 0;
2582 int ex_width
= extra_width
+ (cfg
.window_border
- offset_width
) * 2;
2583 int ex_height
= extra_height
+ (cfg
.window_border
- offset_height
) * 2;
2584 LPRECT r
= (LPRECT
) lParam
;
2586 width
= r
->right
- r
->left
- ex_width
;
2587 height
= r
->bottom
- r
->top
- ex_height
;
2589 w
= (width
+ term
->cols
/2)/term
->cols
;
2590 h
= (height
+ term
->rows
/2)/term
->rows
;
2591 if ( r
->right
!= r
->left
+ w
*term
->cols
+ ex_width
)
2594 if (wParam
== WMSZ_LEFT
||
2595 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2596 r
->left
= r
->right
- w
*term
->cols
- ex_width
;
2598 r
->right
= r
->left
+ w
*term
->cols
+ ex_width
;
2600 if (r
->bottom
!= r
->top
+ h
*term
->rows
+ ex_height
)
2603 if (wParam
== WMSZ_TOP
||
2604 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2605 r
->top
= r
->bottom
- h
*term
->rows
- ex_height
;
2607 r
->bottom
= r
->top
+ h
*term
->rows
+ ex_height
;
2611 /* break; (never reached) */
2612 case WM_FULLSCR_ON_MAX
:
2613 fullscr_on_max
= TRUE
;
2616 sys_cursor_update();
2619 #ifdef RDB_DEBUG_PATCH
2620 debug((27, "WM_SIZE %s (%d,%d)",
2621 (wParam
== SIZE_MINIMIZED
) ?
"SIZE_MINIMIZED":
2622 (wParam
== SIZE_MAXIMIZED
) ?
"SIZE_MAXIMIZED":
2623 (wParam
== SIZE_RESTORED
&& resizing
) ?
"to":
2624 (wParam
== SIZE_RESTORED
) ?
"SIZE_RESTORED":
2626 LOWORD(lParam
), HIWORD(lParam
)));
2628 if (wParam
== SIZE_MINIMIZED
)
2630 cfg
.win_name_always ? window_name
: icon_name
);
2631 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
)
2632 SetWindowText(hwnd
, window_name
);
2633 if (wParam
== SIZE_RESTORED
)
2634 clear_full_screen();
2635 if (wParam
== SIZE_MAXIMIZED
&& fullscr_on_max
) {
2636 fullscr_on_max
= FALSE
;
2640 if (cfg
.resize_action
== RESIZE_DISABLED
) {
2641 /* A resize, well it better be a minimize. */
2645 int width
, height
, w
, h
;
2647 width
= LOWORD(lParam
);
2648 height
= HIWORD(lParam
);
2651 if (wParam
== SIZE_MAXIMIZED
&& !was_zoomed
) {
2653 prev_rows
= term
->rows
;
2654 prev_cols
= term
->cols
;
2655 if (cfg
.resize_action
== RESIZE_TERM
) {
2656 w
= width
/ font_width
;
2658 h
= height
/ font_height
;
2661 term_size(term
, h
, w
, cfg
.savelines
);
2664 } else if (wParam
== SIZE_RESTORED
&& was_zoomed
) {
2666 if (cfg
.resize_action
== RESIZE_TERM
)
2667 term_size(term
, prev_rows
, prev_cols
, cfg
.savelines
);
2668 if (cfg
.resize_action
!= RESIZE_FONT
)
2673 /* This is an unexpected resize, these will normally happen
2674 * if the window is too large. Probably either the user
2675 * selected a huge font or the screen size has changed.
2677 * This is also called with minimize.
2679 else reset_window(-1);
2683 * Don't call back->size in mid-resize. (To prevent
2684 * massive numbers of resize events getting sent
2685 * down the connection during an NT opaque drag.)
2688 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2689 need_backend_resize
= TRUE
;
2690 w
= (width
-cfg
.window_border
*2) / font_width
;
2692 h
= (height
-cfg
.window_border
*2) / font_height
;
2701 sys_cursor_update();
2704 switch (LOWORD(wParam
)) {
2706 term_scroll(term
, -1, 0);
2709 term_scroll(term
, +1, 0);
2712 term_scroll(term
, 0, +1);
2715 term_scroll(term
, 0, -1);
2718 term_scroll(term
, 0, +term
->rows
/ 2);
2721 term_scroll(term
, 0, -term
->rows
/ 2);
2723 case SB_THUMBPOSITION
:
2725 term_scroll(term
, 1, HIWORD(wParam
));
2729 case WM_PALETTECHANGED
:
2730 if ((HWND
) wParam
!= hwnd
&& pal
!= NULL
) {
2731 HDC hdc
= get_ctx(NULL
);
2733 if (RealizePalette(hdc
) > 0)
2739 case WM_QUERYNEWPALETTE
:
2741 HDC hdc
= get_ctx(NULL
);
2743 if (RealizePalette(hdc
) > 0)
2755 * Add the scan code and keypress timing to the random
2758 noise_ultralight(lParam
);
2761 * We don't do TranslateMessage since it disassociates the
2762 * resulting CHAR message from the KEYDOWN that sparked it,
2763 * which we occasionally don't want. Instead, we process
2764 * KEYDOWN, and call the Win32 translator functions so that
2765 * we get the translations under _our_ control.
2768 unsigned char buf
[20];
2771 if (wParam
== VK_PROCESSKEY
) { /* IME PROCESS key */
2772 if (message
== WM_KEYDOWN
) {
2775 m
.message
= WM_KEYDOWN
;
2777 m
.lParam
= lParam
& 0xdfff;
2778 TranslateMessage(&m
);
2779 } else break; /* pass to Windows for default processing */
2781 len
= TranslateKey(message
, wParam
, lParam
, buf
);
2783 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2787 * Interrupt an ongoing paste. I'm not sure
2788 * this is sensible, but for the moment it's
2789 * preferable to having to faff about buffering
2795 * We need not bother about stdin backlogs
2796 * here, because in GUI PuTTY we can't do
2797 * anything about it anyway; there's no means
2798 * of asking Windows to hold off on KEYDOWN
2799 * messages. We _have_ to buffer everything
2802 term_seen_key_event(term
);
2804 ldisc_send(ldisc
, buf
, len
, 1);
2809 net_pending_errors();
2811 case WM_INPUTLANGCHANGE
:
2812 /* wParam == Font number */
2813 /* lParam == Locale */
2814 set_input_locale((HKL
)lParam
);
2815 sys_cursor_update();
2817 case WM_IME_STARTCOMPOSITION
:
2819 HIMC hImc
= ImmGetContext(hwnd
);
2820 ImmSetCompositionFont(hImc
, &lfont
);
2821 ImmReleaseContext(hwnd
, hImc
);
2824 case WM_IME_COMPOSITION
:
2830 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
||
2831 osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) break; /* no Unicode */
2833 if ((lParam
& GCS_RESULTSTR
) == 0) /* Composition unfinished. */
2834 break; /* fall back to DefWindowProc */
2836 hIMC
= ImmGetContext(hwnd
);
2837 n
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
2841 buff
= snewn(n
, char);
2842 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, buff
, n
);
2844 * Jaeyoun Chung reports that Korean character
2845 * input doesn't work correctly if we do a single
2846 * luni_send() covering the whole of buff. So
2847 * instead we luni_send the characters one by one.
2849 term_seen_key_event(term
);
2850 for (i
= 0; i
< n
; i
+= 2) {
2852 luni_send(ldisc
, (unsigned short *)(buff
+i
), 1, 1);
2856 ImmReleaseContext(hwnd
, hIMC
);
2861 if (wParam
& 0xFF00) {
2862 unsigned char buf
[2];
2865 buf
[0] = wParam
>> 8;
2866 term_seen_key_event(term
);
2868 lpage_send(ldisc
, kbd_codepage
, buf
, 2, 1);
2870 char c
= (unsigned char) wParam
;
2871 term_seen_key_event(term
);
2873 lpage_send(ldisc
, kbd_codepage
, &c
, 1, 1);
2879 * Nevertheless, we are prepared to deal with WM_CHAR
2880 * messages, should they crop up. So if someone wants to
2881 * post the things to us as part of a macro manoeuvre,
2882 * we're ready to cope.
2885 char c
= (unsigned char)wParam
;
2886 term_seen_key_event(term
);
2888 lpage_send(ldisc
, CP_ACP
, &c
, 1, 1);
2891 case WM_SYSCOLORCHANGE
:
2892 if (cfg
.system_colour
) {
2893 /* Refresh palette from system colours. */
2894 /* XXX actually this zaps the entire palette. */
2897 /* Force a repaint of the terminal window. */
2898 term_invalidate(term
);
2901 case WM_AGENT_CALLBACK
:
2903 struct agent_callback
*c
= (struct agent_callback
*)lParam
;
2904 c
->callback(c
->callback_ctx
, c
->data
, c
->len
);
2909 if (message
== wm_mousewheel
|| message
== WM_MOUSEWHEEL
) {
2910 int shift_pressed
=0, control_pressed
=0;
2912 if (message
== WM_MOUSEWHEEL
) {
2913 wheel_accumulator
+= (short)HIWORD(wParam
);
2914 shift_pressed
=LOWORD(wParam
) & MK_SHIFT
;
2915 control_pressed
=LOWORD(wParam
) & MK_CONTROL
;
2918 wheel_accumulator
+= (int)wParam
;
2919 if (GetKeyboardState(keys
)!=0) {
2920 shift_pressed
=keys
[VK_SHIFT
]&0x80;
2921 control_pressed
=keys
[VK_CONTROL
]&0x80;
2925 /* process events when the threshold is reached */
2926 while (abs(wheel_accumulator
) >= WHEEL_DELTA
) {
2929 /* reduce amount for next time */
2930 if (wheel_accumulator
> 0) {
2932 wheel_accumulator
-= WHEEL_DELTA
;
2933 } else if (wheel_accumulator
< 0) {
2935 wheel_accumulator
+= WHEEL_DELTA
;
2939 if (send_raw_mouse
&&
2940 !(cfg
.mouse_override
&& shift_pressed
)) {
2941 /* send a mouse-down followed by a mouse up */
2942 term_mouse(term
, b
, translate_button(b
),
2944 TO_CHR_X(X_POS(lParam
)),
2945 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2946 control_pressed
, is_alt_pressed());
2947 term_mouse(term
, b
, translate_button(b
),
2948 MA_RELEASE
, TO_CHR_X(X_POS(lParam
)),
2949 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2950 control_pressed
, is_alt_pressed());
2952 /* trigger a scroll */
2953 term_scroll(term
, 0,
2955 -term
->rows
/ 2 : term
->rows
/ 2);
2963 * Any messages we don't process completely above are passed through to
2964 * DefWindowProc() for default processing.
2966 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2970 * Move the system caret. (We maintain one, even though it's
2971 * invisible, for the benefit of blind people: apparently some
2972 * helper software tracks the system caret, so we should arrange to
2975 void sys_cursor(void *frontend
, int x
, int y
)
2979 if (!term
->has_focus
) return;
2982 * Avoid gratuitously re-updating the cursor position and IMM
2983 * window if there's no actual change required.
2985 cx
= x
* font_width
+ offset_width
;
2986 cy
= y
* font_height
+ offset_height
;
2987 if (cx
== caret_x
&& cy
== caret_y
)
2992 sys_cursor_update();
2995 static void sys_cursor_update(void)
3000 if (!term
->has_focus
) return;
3002 if (caret_x
< 0 || caret_y
< 0)
3005 SetCaretPos(caret_x
, caret_y
);
3007 /* IMM calls on Win98 and beyond only */
3008 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) return; /* 3.11 */
3010 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
&&
3011 osVersion
.dwMinorVersion
== 0) return; /* 95 */
3013 /* we should have the IMM functions */
3014 hIMC
= ImmGetContext(hwnd
);
3015 cf
.dwStyle
= CFS_POINT
;
3016 cf
.ptCurrentPos
.x
= caret_x
;
3017 cf
.ptCurrentPos
.y
= caret_y
;
3018 ImmSetCompositionWindow(hIMC
, &cf
);
3020 ImmReleaseContext(hwnd
, hIMC
);
3024 * Draw a line of text in the window, at given character
3025 * coordinates, in given attributes.
3027 * We are allowed to fiddle with the contents of `text'.
3029 void do_text_internal(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3030 unsigned long attr
, int lattr
)
3033 int nfg
, nbg
, nfont
;
3036 int force_manual_underline
= 0;
3037 int fnt_width
, char_width
;
3038 int text_adjust
= 0;
3039 static int *IpDx
= 0, IpDxLEN
= 0;
3041 lattr
&= LATTR_MODE
;
3043 char_width
= fnt_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3045 if (attr
& ATTR_WIDE
)
3048 if (len
> IpDxLEN
|| IpDx
[0] != char_width
) {
3050 if (len
> IpDxLEN
) {
3052 IpDx
= snewn(len
+ 16, int);
3053 IpDxLEN
= (len
+ 16);
3055 for (i
= 0; i
< IpDxLEN
; i
++)
3056 IpDx
[i
] = char_width
;
3059 /* Only want the left half of double width lines */
3060 if (lattr
!= LATTR_NORM
&& x
*2 >= term
->cols
)
3068 if ((attr
& TATTR_ACTCURS
) && (cfg
.cursor_type
== 0 || term
->big_cursor
)) {
3069 attr
&= ~(ATTR_REVERSE
|ATTR_BLINK
|ATTR_COLOURS
);
3070 if (bold_mode
== BOLD_COLOURS
)
3073 /* cursor fg and bg */
3074 attr
|= (260 << ATTR_FGSHIFT
) | (261 << ATTR_BGSHIFT
);
3078 if (cfg
.vtmode
== VT_POORMAN
&& lattr
!= LATTR_NORM
) {
3079 /* Assume a poorman font is borken in other ways too. */
3089 nfont
|= FONT_WIDE
+ FONT_HIGH
;
3092 if (attr
& ATTR_NARROW
)
3093 nfont
|= FONT_NARROW
;
3095 /* Special hack for the VT100 linedraw glyphs. */
3096 if (text
[0] >= 0x23BA && text
[0] <= 0x23BD) {
3097 switch ((unsigned char) (text
[0])) {
3099 text_adjust
= -2 * font_height
/ 5;
3102 text_adjust
= -1 * font_height
/ 5;
3105 text_adjust
= font_height
/ 5;
3108 text_adjust
= 2 * font_height
/ 5;
3111 if (lattr
== LATTR_TOP
|| lattr
== LATTR_BOT
)
3113 text
[0] = ucsdata
.unitab_xterm
['q'];
3114 if (attr
& ATTR_UNDER
) {
3115 attr
&= ~ATTR_UNDER
;
3116 force_manual_underline
= 1;
3120 /* Anything left as an original character set is unprintable. */
3121 if (DIRECT_CHAR(text
[0])) {
3123 for (i
= 0; i
< len
; i
++)
3128 if ((text
[0] & CSET_MASK
) == CSET_OEMCP
)
3131 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
3132 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
3133 if (bold_mode
== BOLD_FONT
&& (attr
& ATTR_BOLD
))
3135 if (und_mode
== UND_FONT
&& (attr
& ATTR_UNDER
))
3136 nfont
|= FONT_UNDERLINE
;
3137 another_font(nfont
);
3138 if (!fonts
[nfont
]) {
3139 if (nfont
& FONT_UNDERLINE
)
3140 force_manual_underline
= 1;
3141 /* Don't do the same for manual bold, it could be bad news. */
3143 nfont
&= ~(FONT_BOLD
| FONT_UNDERLINE
);
3145 another_font(nfont
);
3147 nfont
= FONT_NORMAL
;
3148 if (attr
& ATTR_REVERSE
) {
3153 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BOLD
)) {
3154 if (nfg
< 16) nfg
|= 8;
3155 else if (nfg
>= 256) nfg
|= 1;
3157 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BLINK
)) {
3158 if (nbg
< 16) nbg
|= 8;
3159 else if (nbg
>= 256) nbg
|= 1;
3163 SelectObject(hdc
, fonts
[nfont
]);
3164 SetTextColor(hdc
, fg
);
3165 SetBkColor(hdc
, bg
);
3166 if (attr
& TATTR_COMBINING
)
3167 SetBkMode(hdc
, TRANSPARENT
);
3169 SetBkMode(hdc
, OPAQUE
);
3172 line_box
.right
= x
+ char_width
* len
;
3173 line_box
.bottom
= y
+ font_height
;
3175 /* Only want the left half of double width lines */
3176 if (line_box
.right
> font_width
*term
->cols
+offset_width
)
3177 line_box
.right
= font_width
*term
->cols
+offset_width
;
3179 /* We're using a private area for direct to font. (512 chars.) */
3180 if (ucsdata
.dbcs_screenfont
&& (text
[0] & CSET_MASK
) == CSET_ACP
) {
3181 /* Ho Hum, dbcs fonts are a PITA! */
3182 /* To display on W9x I have to convert to UCS */
3183 static wchar_t *uni_buf
= 0;
3184 static int uni_len
= 0;
3186 if (len
> uni_len
) {
3189 uni_buf
= snewn(uni_len
, wchar_t);
3192 for(nlen
= mptr
= 0; mptr
<len
; mptr
++) {
3193 uni_buf
[nlen
] = 0xFFFD;
3194 if (IsDBCSLeadByteEx(ucsdata
.font_codepage
, (BYTE
) text
[mptr
])) {
3196 dbcstext
[0] = text
[mptr
] & 0xFF;
3197 dbcstext
[1] = text
[mptr
+1] & 0xFF;
3198 IpDx
[nlen
] += char_width
;
3199 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3200 dbcstext
, 2, uni_buf
+nlen
, 1);
3206 dbcstext
[0] = text
[mptr
] & 0xFF;
3207 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3208 dbcstext
, 1, uni_buf
+nlen
, 1);
3216 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3217 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, uni_buf
, nlen
, IpDx
);
3218 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3219 SetBkMode(hdc
, TRANSPARENT
);
3220 ExtTextOutW(hdc
, x
- 1,
3221 y
- font_height
* (lattr
==
3222 LATTR_BOT
) + text_adjust
,
3223 ETO_CLIPPED
, &line_box
, uni_buf
, nlen
, IpDx
);
3227 } else if (DIRECT_FONT(text
[0])) {
3228 static char *directbuf
= NULL
;
3229 static int directlen
= 0;
3231 if (len
> directlen
) {
3233 directbuf
= sresize(directbuf
, directlen
, char);
3236 for (i
= 0; i
< len
; i
++)
3237 directbuf
[i
] = text
[i
] & 0xFF;
3240 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3241 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, directbuf
, len
, IpDx
);
3242 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3243 SetBkMode(hdc
, TRANSPARENT
);
3245 /* GRR: This draws the character outside it's box and can leave
3246 * 'droppings' even with the clip box! I suppose I could loop it
3247 * one character at a time ... yuk.
3249 * Or ... I could do a test print with "W", and use +1 or -1 for this
3250 * shift depending on if the leftmost column is blank...
3252 ExtTextOut(hdc
, x
- 1,
3253 y
- font_height
* (lattr
==
3254 LATTR_BOT
) + text_adjust
,
3255 ETO_CLIPPED
, &line_box
, directbuf
, len
, IpDx
);
3258 /* And 'normal' unicode characters */
3259 static WCHAR
*wbuf
= NULL
;
3260 static int wlen
= 0;
3266 wbuf
= snewn(wlen
, WCHAR
);
3269 for (i
= 0; i
< len
; i
++)
3272 /* print Glyphs as they are, without Windows' Shaping*/
3273 exact_textout(hdc
, x
, y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3274 &line_box
, wbuf
, len
, IpDx
, !(attr
& TATTR_COMBINING
));
3275 /* ExtTextOutW(hdc, x,
3276 y - font_height * (lattr == LATTR_BOT) + text_adjust,
3277 ETO_CLIPPED | ETO_OPAQUE, &line_box, wbuf, len, IpDx);
3280 /* And the shadow bold hack. */
3281 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3282 SetBkMode(hdc
, TRANSPARENT
);
3283 ExtTextOutW(hdc
, x
- 1,
3284 y
- font_height
* (lattr
==
3285 LATTR_BOT
) + text_adjust
,
3286 ETO_CLIPPED
, &line_box
, wbuf
, len
, IpDx
);
3289 if (lattr
!= LATTR_TOP
&& (force_manual_underline
||
3290 (und_mode
== UND_LINE
3291 && (attr
& ATTR_UNDER
)))) {
3294 if (lattr
== LATTR_BOT
)
3295 dec
= dec
* 2 - font_height
;
3297 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, fg
));
3298 MoveToEx(hdc
, x
, y
+ dec
, NULL
);
3299 LineTo(hdc
, x
+ len
* char_width
, y
+ dec
);
3300 oldpen
= SelectObject(hdc
, oldpen
);
3301 DeleteObject(oldpen
);
3306 * Wrapper that handles combining characters.
3308 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3309 unsigned long attr
, int lattr
)
3311 if (attr
& TATTR_COMBINING
) {
3312 unsigned long a
= 0;
3313 attr
&= ~TATTR_COMBINING
;
3315 do_text_internal(ctx
, x
, y
, text
, 1, attr
| a
, lattr
);
3317 a
= TATTR_COMBINING
;
3320 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
3323 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3324 unsigned long attr
, int lattr
)
3330 int ctype
= cfg
.cursor_type
;
3332 lattr
&= LATTR_MODE
;
3334 if ((attr
& TATTR_ACTCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3335 if (*text
!= UCSWIDE
) {
3336 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
3340 attr
|= TATTR_RIGHTCURS
;
3343 fnt_width
= char_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3344 if (attr
& ATTR_WIDE
)
3351 if ((attr
& TATTR_PASCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3354 pts
[0].x
= pts
[1].x
= pts
[4].x
= x
;
3355 pts
[2].x
= pts
[3].x
= x
+ char_width
- 1;
3356 pts
[0].y
= pts
[3].y
= pts
[4].y
= y
;
3357 pts
[1].y
= pts
[2].y
= y
+ font_height
- 1;
3358 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3359 Polyline(hdc
, pts
, 5);
3360 oldpen
= SelectObject(hdc
, oldpen
);
3361 DeleteObject(oldpen
);
3362 } else if ((attr
& (TATTR_ACTCURS
| TATTR_PASCURS
)) && ctype
!= 0) {
3363 int startx
, starty
, dx
, dy
, length
, i
;
3366 starty
= y
+ descent
;
3369 length
= char_width
;
3372 if (attr
& TATTR_RIGHTCURS
)
3373 xadjust
= char_width
- 1;
3374 startx
= x
+ xadjust
;
3378 length
= font_height
;
3380 if (attr
& TATTR_ACTCURS
) {
3383 SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3384 MoveToEx(hdc
, startx
, starty
, NULL
);
3385 LineTo(hdc
, startx
+ dx
* length
, starty
+ dy
* length
);
3386 oldpen
= SelectObject(hdc
, oldpen
);
3387 DeleteObject(oldpen
);
3389 for (i
= 0; i
< length
; i
++) {
3391 SetPixel(hdc
, startx
, starty
, colours
[261]);
3400 /* This function gets the actual width of a character in the normal font.
3402 int char_width(Context ctx
, int uc
) {
3406 /* If the font max is the same as the font ave width then this
3407 * function is a no-op.
3409 if (!font_dualwidth
) return 1;
3411 switch (uc
& CSET_MASK
) {
3413 uc
= ucsdata
.unitab_line
[uc
& 0xFF];
3416 uc
= ucsdata
.unitab_xterm
[uc
& 0xFF];
3419 uc
= ucsdata
.unitab_scoacs
[uc
& 0xFF];
3422 if (DIRECT_FONT(uc
)) {
3423 if (ucsdata
.dbcs_screenfont
) return 1;
3425 /* Speedup, I know of no font where ascii is the wrong width */
3426 if ((uc
&~CSET_MASK
) >= ' ' && (uc
&~CSET_MASK
)<= '~')
3429 if ( (uc
& CSET_MASK
) == CSET_ACP
) {
3430 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3431 } else if ( (uc
& CSET_MASK
) == CSET_OEMCP
) {
3432 another_font(FONT_OEM
);
3433 if (!fonts
[FONT_OEM
]) return 0;
3435 SelectObject(hdc
, fonts
[FONT_OEM
]);
3439 if ( GetCharWidth32(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1 &&
3440 GetCharWidth(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1)
3443 /* Speedup, I know of no font where ascii is the wrong width */
3444 if (uc
>= ' ' && uc
<= '~') return 1;
3446 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3447 if ( GetCharWidth32W(hdc
, uc
, uc
, &ibuf
) == 1 )
3448 /* Okay that one worked */ ;
3449 else if ( GetCharWidthW(hdc
, uc
, uc
, &ibuf
) == 1 )
3450 /* This should work on 9x too, but it's "less accurate" */ ;
3455 ibuf
+= font_width
/ 2 -1;
3462 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3463 * codes. Returns number of bytes used or zero to drop the message
3464 * or -1 to forward the message to windows.
3466 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
3467 unsigned char *output
)
3470 int scan
, left_alt
= 0, key_down
, shift_state
;
3472 unsigned char *p
= output
;
3473 static int alt_sum
= 0;
3475 HKL kbd_layout
= GetKeyboardLayout(0);
3477 /* keys is for ToAsciiEx. There's some ick here, see below. */
3478 static WORD keys
[3];
3479 static int compose_char
= 0;
3480 static WPARAM compose_key
= 0;
3482 r
= GetKeyboardState(keystate
);
3484 memset(keystate
, 0, sizeof(keystate
));
3487 #define SHOW_TOASCII_RESULT
3488 { /* Tell us all about key events */
3489 static BYTE oldstate
[256];
3490 static int first
= 1;
3494 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3497 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
) {
3499 } else if ((HIWORD(lParam
) & KF_UP
)
3500 && scan
== (HIWORD(lParam
) & 0xFF)) {
3504 if (wParam
>= VK_F1
&& wParam
<= VK_F20
)
3505 debug(("K_F%d", wParam
+ 1 - VK_F1
));
3518 debug(("VK_%02x", wParam
));
3520 if (message
== WM_SYSKEYDOWN
|| message
== WM_SYSKEYUP
)
3522 debug((", S%02x", scan
= (HIWORD(lParam
) & 0xFF)));
3524 ch
= MapVirtualKeyEx(wParam
, 2, kbd_layout
);
3525 if (ch
>= ' ' && ch
<= '~')
3526 debug((", '%c'", ch
));
3528 debug((", $%02x", ch
));
3531 debug((", KB0=%02x", keys
[0]));
3533 debug((", KB1=%02x", keys
[1]));
3535 debug((", KB2=%02x", keys
[2]));
3537 if ((keystate
[VK_SHIFT
] & 0x80) != 0)
3539 if ((keystate
[VK_CONTROL
] & 0x80) != 0)
3541 if ((HIWORD(lParam
) & KF_EXTENDED
))
3543 if ((HIWORD(lParam
) & KF_UP
))
3547 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
);
3548 else if ((HIWORD(lParam
) & KF_UP
))
3549 oldstate
[wParam
& 0xFF] ^= 0x80;
3551 oldstate
[wParam
& 0xFF] ^= 0x81;
3553 for (ch
= 0; ch
< 256; ch
++)
3554 if (oldstate
[ch
] != keystate
[ch
])
3555 debug((", M%02x=%02x", ch
, keystate
[ch
]));
3557 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3561 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
)) {
3562 keystate
[VK_RMENU
] = keystate
[VK_MENU
];
3566 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3567 if ((cfg
.funky_type
== FUNKY_VT400
||
3568 (cfg
.funky_type
<= FUNKY_LINUX
&& term
->app_keypad_keys
&&
3570 && wParam
== VK_NUMLOCK
&& !(keystate
[VK_SHIFT
] & 0x80)) {
3572 wParam
= VK_EXECUTE
;
3574 /* UnToggle NUMLock */
3575 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0)
3576 keystate
[VK_NUMLOCK
] ^= 1;
3579 /* And write back the 'adjusted' state */
3580 SetKeyboardState(keystate
);
3583 /* Disable Auto repeat if required */
3584 if (term
->repeat_off
&&
3585 (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
)
3588 if ((HIWORD(lParam
) & KF_ALTDOWN
) && (keystate
[VK_RMENU
] & 0x80) == 0)
3591 key_down
= ((HIWORD(lParam
) & KF_UP
) == 0);
3593 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3594 if (left_alt
&& (keystate
[VK_CONTROL
] & 0x80)) {
3595 if (cfg
.ctrlaltkeys
)
3596 keystate
[VK_MENU
] = 0;
3598 keystate
[VK_RMENU
] = 0x80;
3603 scan
= (HIWORD(lParam
) & (KF_UP
| KF_EXTENDED
| 0xFF));
3604 shift_state
= ((keystate
[VK_SHIFT
] & 0x80) != 0)
3605 + ((keystate
[VK_CONTROL
] & 0x80) != 0) * 2;
3607 /* Note if AltGr was pressed and if it was used as a compose key */
3608 if (!compose_state
) {
3609 compose_key
= 0x100;
3610 if (cfg
.compose_key
) {
3611 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
))
3612 compose_key
= wParam
;
3614 if (wParam
== VK_APPS
)
3615 compose_key
= wParam
;
3618 if (wParam
== compose_key
) {
3619 if (compose_state
== 0
3620 && (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0) compose_state
=
3622 else if (compose_state
== 1 && (HIWORD(lParam
) & KF_UP
))
3626 } else if (compose_state
== 1 && wParam
!= VK_CONTROL
)
3629 if (compose_state
> 1 && left_alt
)
3632 /* Sanitize the number pad if not using a PC NumPad */
3633 if (left_alt
|| (term
->app_keypad_keys
&& !cfg
.no_applic_k
3634 && cfg
.funky_type
!= FUNKY_XTERM
)
3635 || cfg
.funky_type
== FUNKY_VT400
|| cfg
.nethack_keypad
|| compose_state
) {
3636 if ((HIWORD(lParam
) & KF_EXTENDED
) == 0) {
3640 nParam
= VK_NUMPAD0
;
3643 nParam
= VK_NUMPAD1
;
3646 nParam
= VK_NUMPAD2
;
3649 nParam
= VK_NUMPAD3
;
3652 nParam
= VK_NUMPAD4
;
3655 nParam
= VK_NUMPAD5
;
3658 nParam
= VK_NUMPAD6
;
3661 nParam
= VK_NUMPAD7
;
3664 nParam
= VK_NUMPAD8
;
3667 nParam
= VK_NUMPAD9
;
3670 nParam
= VK_DECIMAL
;
3674 if (keystate
[VK_NUMLOCK
] & 1)
3681 /* If a key is pressed and AltGr is not active */
3682 if (key_down
&& (keystate
[VK_RMENU
] & 0x80) == 0 && !compose_state
) {
3683 /* Okay, prepare for most alts then ... */
3687 /* Lets see if it's a pattern we know all about ... */
3688 if (wParam
== VK_PRIOR
&& shift_state
== 1) {
3689 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEUP
, 0);
3692 if (wParam
== VK_PRIOR
&& shift_state
== 2) {
3693 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEUP
, 0);
3696 if (wParam
== VK_NEXT
&& shift_state
== 1) {
3697 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEDOWN
, 0);
3700 if (wParam
== VK_NEXT
&& shift_state
== 2) {
3701 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEDOWN
, 0);
3704 if (wParam
== VK_INSERT
&& shift_state
== 1) {
3705 term_do_paste(term
);
3708 if (left_alt
&& wParam
== VK_F4
&& cfg
.alt_f4
) {
3711 if (left_alt
&& wParam
== VK_SPACE
&& cfg
.alt_space
) {
3712 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_KEYMENU
, 0);
3715 if (left_alt
&& wParam
== VK_RETURN
&& cfg
.fullscreenonaltenter
&&
3716 (cfg
.resize_action
!= RESIZE_DISABLED
)) {
3717 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) != KF_REPEAT
)
3721 /* Control-Numlock for app-keypad mode switch */
3722 if (wParam
== VK_PAUSE
&& shift_state
== 2) {
3723 term
->app_keypad_keys
^= 1;
3727 /* Nethack keypad */
3728 if (cfg
.nethack_keypad
&& !left_alt
) {
3731 *p
++ = "bB\002\002"[shift_state
& 3];
3734 *p
++ = "jJ\012\012"[shift_state
& 3];
3737 *p
++ = "nN\016\016"[shift_state
& 3];
3740 *p
++ = "hH\010\010"[shift_state
& 3];
3743 *p
++ = shift_state ?
'.' : '.';
3746 *p
++ = "lL\014\014"[shift_state
& 3];
3749 *p
++ = "yY\031\031"[shift_state
& 3];
3752 *p
++ = "kK\013\013"[shift_state
& 3];
3755 *p
++ = "uU\025\025"[shift_state
& 3];
3760 /* Application Keypad */
3764 if (cfg
.funky_type
== FUNKY_VT400
||
3765 (cfg
.funky_type
<= FUNKY_LINUX
&&
3766 term
->app_keypad_keys
&& !cfg
.no_applic_k
)) switch (wParam
) {
3780 if (term
->app_keypad_keys
&& !cfg
.no_applic_k
)
3817 if (cfg
.funky_type
== FUNKY_XTERM
) {
3822 } else if (shift_state
)
3829 if (cfg
.funky_type
== FUNKY_XTERM
)
3833 if (cfg
.funky_type
== FUNKY_XTERM
)
3837 if (cfg
.funky_type
== FUNKY_XTERM
)
3842 if (HIWORD(lParam
) & KF_EXTENDED
)
3847 if (term
->vt52_mode
) {
3848 if (xkey
>= 'P' && xkey
<= 'S')
3849 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3851 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
3853 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3858 if (wParam
== VK_BACK
&& shift_state
== 0) { /* Backspace */
3859 *p
++ = (cfg
.bksp_is_delete ?
0x7F : 0x08);
3863 if (wParam
== VK_BACK
&& shift_state
== 1) { /* Shift Backspace */
3864 /* We do the opposite of what is configured */
3865 *p
++ = (cfg
.bksp_is_delete ?
0x08 : 0x7F);
3869 if (wParam
== VK_TAB
&& shift_state
== 1) { /* Shift tab */
3875 if (wParam
== VK_SPACE
&& shift_state
== 2) { /* Ctrl-Space */
3879 if (wParam
== VK_SPACE
&& shift_state
== 3) { /* Ctrl-Shift-Space */
3883 if (wParam
== VK_CANCEL
&& shift_state
== 2) { /* Ctrl-Break */
3888 if (wParam
== VK_PAUSE
) { /* Break/Pause */
3893 /* Control-2 to Control-8 are special */
3894 if (shift_state
== 2 && wParam
>= '2' && wParam
<= '8') {
3895 *p
++ = "\000\033\034\035\036\037\177"[wParam
- '2'];
3898 if (shift_state
== 2 && (wParam
== 0xBD || wParam
== 0xBF)) {
3902 if (shift_state
== 2 && wParam
== 0xDF) {
3906 if (shift_state
== 3 && wParam
== 0xDE) {
3907 *p
++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
3910 if (shift_state
== 0 && wParam
== VK_RETURN
&& term
->cr_lf_return
) {
3917 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3918 * for integer decimal nn.)
3920 * We also deal with the weird ones here. Linux VCs replace F1
3921 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3922 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3928 code
= (keystate
[VK_SHIFT
] & 0x80 ?
23 : 11);
3931 code
= (keystate
[VK_SHIFT
] & 0x80 ?
24 : 12);
3934 code
= (keystate
[VK_SHIFT
] & 0x80 ?
25 : 13);
3937 code
= (keystate
[VK_SHIFT
] & 0x80 ?
26 : 14);
3940 code
= (keystate
[VK_SHIFT
] & 0x80 ?
28 : 15);
3943 code
= (keystate
[VK_SHIFT
] & 0x80 ?
29 : 17);
3946 code
= (keystate
[VK_SHIFT
] & 0x80 ?
31 : 18);
3949 code
= (keystate
[VK_SHIFT
] & 0x80 ?
32 : 19);
3952 code
= (keystate
[VK_SHIFT
] & 0x80 ?
33 : 20);
3955 code
= (keystate
[VK_SHIFT
] & 0x80 ?
34 : 21);
3988 if ((shift_state
&2) == 0) switch (wParam
) {
4008 /* Reorder edit keys to physical order */
4009 if (cfg
.funky_type
== FUNKY_VT400
&& code
<= 6)
4010 code
= "\0\2\1\4\5\3\6"[code
];
4012 if (term
->vt52_mode
&& code
> 0 && code
<= 6) {
4013 p
+= sprintf((char *) p
, "\x1B%c", " HLMEIG"[code
]);
4017 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO function keys */
4018 code
>= 11 && code
<= 34) {
4019 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
4022 case VK_F1
: index
= 0; break;
4023 case VK_F2
: index
= 1; break;
4024 case VK_F3
: index
= 2; break;
4025 case VK_F4
: index
= 3; break;
4026 case VK_F5
: index
= 4; break;
4027 case VK_F6
: index
= 5; break;
4028 case VK_F7
: index
= 6; break;
4029 case VK_F8
: index
= 7; break;
4030 case VK_F9
: index
= 8; break;
4031 case VK_F10
: index
= 9; break;
4032 case VK_F11
: index
= 10; break;
4033 case VK_F12
: index
= 11; break;
4035 if (keystate
[VK_SHIFT
] & 0x80) index
+= 12;
4036 if (keystate
[VK_CONTROL
] & 0x80) index
+= 24;
4037 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
4040 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO small keypad */
4041 code
>= 1 && code
<= 6) {
4042 char codes
[] = "HL.FIG";
4046 p
+= sprintf((char *) p
, "\x1B[%c", codes
[code
-1]);
4050 if ((term
->vt52_mode
|| cfg
.funky_type
== FUNKY_VT100P
) && code
>= 11 && code
<= 24) {
4056 if (term
->vt52_mode
)
4057 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11 - offt
);
4060 sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11 - offt
);
4063 if (cfg
.funky_type
== FUNKY_LINUX
&& code
>= 11 && code
<= 15) {
4064 p
+= sprintf((char *) p
, "\x1B[[%c", code
+ 'A' - 11);
4067 if (cfg
.funky_type
== FUNKY_XTERM
&& code
>= 11 && code
<= 14) {
4068 if (term
->vt52_mode
)
4069 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11);
4071 p
+= sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11);
4074 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
4075 p
+= sprintf((char *) p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
4079 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
4084 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
4085 * some reason seems to send VK_CLEAR to Windows...).
4107 if (term
->vt52_mode
)
4108 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4110 int app_flg
= (term
->app_cursor_keys
&& !cfg
.no_applic_c
);
4113 * RDB: VT100 & VT102 manuals both state the
4114 * app cursor keys only work if the app keypad
4117 * SGT: That may well be true, but xterm
4118 * disagrees and so does at least one
4119 * application, so I've #if'ed this out and the
4120 * behaviour is back to PuTTY's original: app
4121 * cursor and app keypad are independently
4122 * switchable modes. If anyone complains about
4123 * _this_ I'll have to put in a configurable
4126 if (!term
->app_keypad_keys
)
4129 /* Useful mapping of Ctrl-arrows */
4130 if (shift_state
== 2)
4134 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4136 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4143 * Finally, deal with Return ourselves. (Win95 seems to
4144 * foul it up when Alt is pressed, for some reason.)
4146 if (wParam
== VK_RETURN
) { /* Return */
4152 if (left_alt
&& wParam
>= VK_NUMPAD0
&& wParam
<= VK_NUMPAD9
)
4153 alt_sum
= alt_sum
* 10 + wParam
- VK_NUMPAD0
;
4158 /* Okay we've done everything interesting; let windows deal with
4159 * the boring stuff */
4163 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4164 if(cfg
.xlat_capslockcyr
&& keystate
[VK_CAPITAL
] != 0) {
4166 keystate
[VK_CAPITAL
] = 0;
4169 /* XXX how do we know what the max size of the keys array should
4170 * be is? There's indication on MS' website of an Inquire/InquireEx
4171 * functioning returning a KBINFO structure which tells us. */
4172 if (osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
4173 /* XXX 'keys' parameter is declared in MSDN documentation as
4175 * The experience of a French user indicates that on
4176 * Win98, WORD[] should be passed in, but on Win2K, it should
4177 * be BYTE[]. German WinXP and my Win2K with "US International"
4178 * driver corroborate this.
4179 * Experimentally I've conditionalised the behaviour on the
4180 * Win9x/NT split, but I suspect it's worse than that.
4181 * See wishlist item `win-dead-keys' for more horrible detail
4182 * and speculations. */
4185 r
= ToAsciiEx(wParam
, scan
, keystate
, (LPWORD
)keybs
, 0, kbd_layout
);
4186 for (i
=0; i
<3; i
++) keys
[i
] = keybs
[i
];
4188 r
= ToAsciiEx(wParam
, scan
, keystate
, keys
, 0, kbd_layout
);
4190 #ifdef SHOW_TOASCII_RESULT
4191 if (r
== 1 && !key_down
) {
4193 if (in_utf(term
) || ucsdata
.dbcs_screenfont
)
4194 debug((", (U+%04x)", alt_sum
));
4196 debug((", LCH(%d)", alt_sum
));
4198 debug((", ACH(%d)", keys
[0]));
4203 for (r1
= 0; r1
< r
; r1
++) {
4204 debug(("%s%d", r1 ?
"," : "", keys
[r1
]));
4213 * Interrupt an ongoing paste. I'm not sure this is
4214 * sensible, but for the moment it's preferable to
4215 * having to faff about buffering things.
4220 for (i
= 0; i
< r
; i
++) {
4221 unsigned char ch
= (unsigned char) keys
[i
];
4223 if (compose_state
== 2 && (ch
& 0x80) == 0 && ch
> ' ') {
4228 if (compose_state
== 3 && (ch
& 0x80) == 0 && ch
> ' ') {
4232 if ((nc
= check_compose(compose_char
, ch
)) == -1) {
4233 MessageBeep(MB_ICONHAND
);
4237 term_seen_key_event(term
);
4239 luni_send(ldisc
, &keybuf
, 1, 1);
4247 if (in_utf(term
) || ucsdata
.dbcs_screenfont
) {
4249 term_seen_key_event(term
);
4251 luni_send(ldisc
, &keybuf
, 1, 1);
4253 ch
= (char) alt_sum
;
4255 * We need not bother about stdin
4256 * backlogs here, because in GUI PuTTY
4257 * we can't do anything about it
4258 * anyway; there's no means of asking
4259 * Windows to hold off on KEYDOWN
4260 * messages. We _have_ to buffer
4261 * everything we're sent.
4263 term_seen_key_event(term
);
4265 ldisc_send(ldisc
, &ch
, 1, 1);
4269 term_seen_key_event(term
);
4271 lpage_send(ldisc
, kbd_codepage
, &ch
, 1, 1);
4274 if(capsOn
&& ch
< 0x80) {
4277 cbuf
[1] = xlat_uskbd2cyrllic(ch
);
4278 term_seen_key_event(term
);
4280 luni_send(ldisc
, cbuf
+!left_alt
, 1+!!left_alt
, 1);
4285 term_seen_key_event(term
);
4287 lpage_send(ldisc
, kbd_codepage
,
4288 cbuf
+!left_alt
, 1+!!left_alt
, 1);
4294 /* This is so the ALT-Numpad and dead keys work correctly. */
4299 /* If we're definitly not building up an ALT-54321 then clear it */
4302 /* If we will be using alt_sum fix the 256s */
4303 else if (keys
[0] && (in_utf(term
) || ucsdata
.dbcs_screenfont
))
4308 * ALT alone may or may not want to bring up the System menu.
4309 * If it's not meant to, we return 0 on presses or releases of
4310 * ALT, to show that we've swallowed the keystroke. Otherwise
4311 * we return -1, which means Windows will give the keystroke
4312 * its default handling (i.e. bring up the System menu).
4314 if (wParam
== VK_MENU
&& !cfg
.alt_only
)
4320 void request_paste(void *frontend
)
4323 * In Windows, pasting is synchronous: we can read the
4324 * clipboard with no difficulty, so request_paste() can just go
4327 term_do_paste(term
);
4330 void set_title(void *frontend
, char *title
)
4333 window_name
= snewn(1 + strlen(title
), char);
4334 strcpy(window_name
, title
);
4335 if (cfg
.win_name_always
|| !IsIconic(hwnd
))
4336 SetWindowText(hwnd
, title
);
4339 void set_icon(void *frontend
, char *title
)
4342 icon_name
= snewn(1 + strlen(title
), char);
4343 strcpy(icon_name
, title
);
4344 if (!cfg
.win_name_always
&& IsIconic(hwnd
))
4345 SetWindowText(hwnd
, title
);
4348 void set_sbar(void *frontend
, int total
, int start
, int page
)
4352 if (is_full_screen() ?
!cfg
.scrollbar_in_fullscreen
: !cfg
.scrollbar
)
4355 si
.cbSize
= sizeof(si
);
4356 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
4358 si
.nMax
= total
- 1;
4362 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
4365 Context
get_ctx(void *frontend
)
4371 SelectPalette(hdc
, pal
, FALSE
);
4377 void free_ctx(Context ctx
)
4379 SelectPalette(ctx
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
4380 ReleaseDC(hwnd
, ctx
);
4383 static void real_palette_set(int n
, int r
, int g
, int b
)
4386 logpal
->palPalEntry
[n
].peRed
= r
;
4387 logpal
->palPalEntry
[n
].peGreen
= g
;
4388 logpal
->palPalEntry
[n
].peBlue
= b
;
4389 logpal
->palPalEntry
[n
].peFlags
= PC_NOCOLLAPSE
;
4390 colours
[n
] = PALETTERGB(r
, g
, b
);
4391 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4393 colours
[n
] = RGB(r
, g
, b
);
4396 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
4400 if (n
> NALLCOLOURS
)
4402 real_palette_set(n
, r
, g
, b
);
4404 HDC hdc
= get_ctx(frontend
);
4405 UnrealizeObject(pal
);
4406 RealizePalette(hdc
);
4409 if (n
== (ATTR_DEFBG
>>ATTR_BGSHIFT
))
4410 /* If Default Background changes, we need to ensure any
4411 * space between the text area and the window border is
4413 InvalidateRect(hwnd
, NULL
, TRUE
);
4417 void palette_reset(void *frontend
)
4422 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4424 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
4425 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
4426 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
4427 logpal
->palPalEntry
[i
].peFlags
= 0;
4428 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
4429 defpal
[i
].rgbtGreen
,
4430 defpal
[i
].rgbtBlue
);
4432 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
4433 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4438 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4439 hdc
= get_ctx(frontend
);
4440 RealizePalette(hdc
);
4443 /* Default Background may have changed. Ensure any space between
4444 * text area and window border is redrawn. */
4445 InvalidateRect(hwnd
, NULL
, TRUE
);
4449 void write_aclip(void *frontend
, char *data
, int len
, int must_deselect
)
4454 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
4457 lock
= GlobalLock(clipdata
);
4460 memcpy(lock
, data
, len
);
4461 ((unsigned char *) lock
)[len
] = 0;
4462 GlobalUnlock(clipdata
);
4465 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4467 if (OpenClipboard(hwnd
)) {
4469 SetClipboardData(CF_TEXT
, clipdata
);
4472 GlobalFree(clipdata
);
4475 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4479 * Note: unlike write_aclip() this will not append a nul.
4481 void write_clip(void *frontend
, wchar_t * data
, int *attr
, int len
, int must_deselect
)
4483 HGLOBAL clipdata
, clipdata2
, clipdata3
;
4485 void *lock
, *lock2
, *lock3
;
4487 len2
= WideCharToMultiByte(CP_ACP
, 0, data
, len
, 0, 0, NULL
, NULL
);
4489 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
,
4490 len
* sizeof(wchar_t));
4491 clipdata2
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len2
);
4493 if (!clipdata
|| !clipdata2
) {
4495 GlobalFree(clipdata
);
4497 GlobalFree(clipdata2
);
4500 if (!(lock
= GlobalLock(clipdata
)))
4502 if (!(lock2
= GlobalLock(clipdata2
)))
4505 memcpy(lock
, data
, len
* sizeof(wchar_t));
4506 WideCharToMultiByte(CP_ACP
, 0, data
, len
, lock2
, len2
, NULL
, NULL
);
4508 if (cfg
.rtf_paste
) {
4509 wchar_t unitab
[256];
4511 unsigned char *tdata
= (unsigned char *)lock2
;
4512 wchar_t *udata
= (wchar_t *)lock
;
4513 int rtflen
= 0, uindex
= 0, tindex
= 0;
4515 int multilen
, blen
, alen
, totallen
, i
;
4516 char before
[16], after
[4];
4517 int fgcolour
, lastfgcolour
= 0;
4518 int bgcolour
, lastbgcolour
= 0;
4519 int attrBold
, lastAttrBold
= 0;
4520 int attrUnder
, lastAttrUnder
= 0;
4521 int palette
[NALLCOLOURS
];
4524 get_unitab(CP_ACP
, unitab
, 0);
4526 rtfsize
= 100 + strlen(cfg
.font
.name
);
4527 rtf
= snewn(rtfsize
, char);
4528 rtflen
= sprintf(rtf
, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
4529 cfg
.font
.name
, cfg
.font
.height
*2);
4532 * Add colour palette
4533 * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
4537 * First - Determine all colours in use
4538 * o Foregound and background colours share the same palette
4541 memset(palette
, 0, sizeof(palette
));
4542 for (i
= 0; i
< (len
-1); i
++) {
4543 fgcolour
= ((attr
[i
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4544 bgcolour
= ((attr
[i
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4546 if (attr
[i
] & ATTR_REVERSE
) {
4547 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4548 fgcolour
= bgcolour
;
4549 bgcolour
= tmpcolour
;
4552 if (bold_mode
== BOLD_COLOURS
&& (attr
[i
] & ATTR_BOLD
)) {
4553 if (fgcolour
< 8) /* ANSI colours */
4555 else if (fgcolour
>= 256) /* Default colours */
4559 if (attr
[i
] & ATTR_BLINK
) {
4560 if (bgcolour
< 8) /* ANSI colours */
4562 else if (bgcolour
>= 256) /* Default colours */
4566 palette
[fgcolour
]++;
4567 palette
[bgcolour
]++;
4571 * Next - Create a reduced palette
4574 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4575 if (palette
[i
] != 0)
4576 palette
[i
] = ++numcolours
;
4580 * Finally - Write the colour table
4582 rtf
= sresize(rtf
, rtfsize
+ (numcolours
* 25), char);
4583 strcat(rtf
, "{\\colortbl ;");
4584 rtflen
= strlen(rtf
);
4586 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4587 if (palette
[i
] != 0) {
4588 rtflen
+= sprintf(&rtf
[rtflen
], "\\red%d\\green%d\\blue%d;", defpal
[i
].rgbtRed
, defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4591 strcpy(&rtf
[rtflen
], "}");
4596 * We want to construct a piece of RTF that specifies the
4597 * same Unicode text. To do this we will read back in
4598 * parallel from the Unicode data in `udata' and the
4599 * non-Unicode data in `tdata'. For each character in
4600 * `tdata' which becomes the right thing in `udata' when
4601 * looked up in `unitab', we just copy straight over from
4602 * tdata. For each one that doesn't, we must WCToMB it
4603 * individually and produce a \u escape sequence.
4605 * It would probably be more robust to just bite the bullet
4606 * and WCToMB each individual Unicode character one by one,
4607 * then MBToWC each one back to see if it was an accurate
4608 * translation; but that strikes me as a horrifying number
4609 * of Windows API calls so I want to see if this faster way
4610 * will work. If it screws up badly we can always revert to
4611 * the simple and slow way.
4613 while (tindex
< len2
&& uindex
< len
&&
4614 tdata
[tindex
] && udata
[uindex
]) {
4615 if (tindex
+ 1 < len2
&&
4616 tdata
[tindex
] == '\r' &&
4617 tdata
[tindex
+1] == '\n') {
4623 * Set text attributes
4626 if (rtfsize
< rtflen
+ 64) {
4627 rtfsize
= rtflen
+ 512;
4628 rtf
= sresize(rtf
, rtfsize
, char);
4632 * Determine foreground and background colours
4634 fgcolour
= ((attr
[tindex
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4635 bgcolour
= ((attr
[tindex
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4637 if (attr
[tindex
] & ATTR_REVERSE
) {
4638 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4639 fgcolour
= bgcolour
;
4640 bgcolour
= tmpcolour
;
4643 if (bold_mode
== BOLD_COLOURS
&& (attr
[tindex
] & ATTR_BOLD
)) {
4644 if (fgcolour
< 8) /* ANSI colours */
4646 else if (fgcolour
>= 256) /* Default colours */
4650 if (attr
[tindex
] & ATTR_BLINK
) {
4651 if (bgcolour
< 8) /* ANSI colours */
4653 else if (bgcolour
>= 256) /* Default colours */
4658 * Collect other attributes
4660 if (bold_mode
!= BOLD_COLOURS
)
4661 attrBold
= attr
[tindex
] & ATTR_BOLD
;
4665 attrUnder
= attr
[tindex
] & ATTR_UNDER
;
4669 * o If video isn't reversed, ignore colour attributes for default foregound
4671 * o Special case where bolded text is displayed using the default foregound
4672 * and background colours - force to bolded RTF.
4674 if (!(attr
[tindex
] & ATTR_REVERSE
)) {
4675 if (bgcolour
>= 256) /* Default color */
4676 bgcolour
= -1; /* No coloring */
4678 if (fgcolour
>= 256) { /* Default colour */
4679 if (bold_mode
== BOLD_COLOURS
&& (fgcolour
& 1) && bgcolour
== -1)
4680 attrBold
= ATTR_BOLD
; /* Emphasize text with bold attribute */
4682 fgcolour
= -1; /* No coloring */
4687 * Write RTF text attributes
4689 if (lastfgcolour
!= fgcolour
) {
4690 lastfgcolour
= fgcolour
;
4691 rtflen
+= sprintf(&rtf
[rtflen
], "\\cf%d ", (fgcolour
>= 0) ? palette
[fgcolour
] : 0);
4694 if (lastbgcolour
!= bgcolour
) {
4695 lastbgcolour
= bgcolour
;
4696 rtflen
+= sprintf(&rtf
[rtflen
], "\\highlight%d ", (bgcolour
>= 0) ? palette
[bgcolour
] : 0);
4699 if (lastAttrBold
!= attrBold
) {
4700 lastAttrBold
= attrBold
;
4701 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrBold ?
"\\b " : "\\b0 ");
4704 if (lastAttrUnder
!= attrUnder
) {
4705 lastAttrUnder
= attrUnder
;
4706 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrUnder ?
"\\ul " : "\\ulnone ");
4710 if (unitab
[tdata
[tindex
]] == udata
[uindex
]) {
4716 multilen
= WideCharToMultiByte(CP_ACP
, 0, unitab
+uindex
, 1,
4717 NULL
, 0, NULL
, NULL
);
4718 if (multilen
!= 1) {
4719 blen
= sprintf(before
, "{\\uc%d\\u%d", multilen
,
4721 alen
= 1; strcpy(after
, "}");
4723 blen
= sprintf(before
, "\\u%d", udata
[uindex
]);
4724 alen
= 0; after
[0] = '\0';
4727 assert(tindex
+ multilen
<= len2
);
4728 totallen
= blen
+ alen
;
4729 for (i
= 0; i
< multilen
; i
++) {
4730 if (tdata
[tindex
+i
] == '\\' ||
4731 tdata
[tindex
+i
] == '{' ||
4732 tdata
[tindex
+i
] == '}')
4734 else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A)
4735 totallen
+= 6; /* \par\r\n */
4736 else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20)
4742 if (rtfsize
< rtflen
+ totallen
+ 3) {
4743 rtfsize
= rtflen
+ totallen
+ 512;
4744 rtf
= sresize(rtf
, rtfsize
, char);
4747 strcpy(rtf
+ rtflen
, before
); rtflen
+= blen
;
4748 for (i
= 0; i
< multilen
; i
++) {
4749 if (tdata
[tindex
+i
] == '\\' ||
4750 tdata
[tindex
+i
] == '{' ||
4751 tdata
[tindex
+i
] == '}') {
4752 rtf
[rtflen
++] = '\\';
4753 rtf
[rtflen
++] = tdata
[tindex
+i
];
4754 } else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A) {
4755 rtflen
+= sprintf(rtf
+rtflen
, "\\par\r\n");
4756 } else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20) {
4757 rtflen
+= sprintf(rtf
+rtflen
, "\\'%02x", tdata
[tindex
+i
]);
4759 rtf
[rtflen
++] = tdata
[tindex
+i
];
4762 strcpy(rtf
+ rtflen
, after
); rtflen
+= alen
;
4768 rtf
[rtflen
++] = '}'; /* Terminate RTF stream */
4769 rtf
[rtflen
++] = '\0';
4770 rtf
[rtflen
++] = '\0';
4772 clipdata3
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, rtflen
);
4773 if (clipdata3
&& (lock3
= GlobalLock(clipdata3
)) != NULL
) {
4774 memcpy(lock3
, rtf
, rtflen
);
4775 GlobalUnlock(clipdata3
);
4781 GlobalUnlock(clipdata
);
4782 GlobalUnlock(clipdata2
);
4785 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4787 if (OpenClipboard(hwnd
)) {
4789 SetClipboardData(CF_UNICODETEXT
, clipdata
);
4790 SetClipboardData(CF_TEXT
, clipdata2
);
4792 SetClipboardData(RegisterClipboardFormat(CF_RTF
), clipdata3
);
4795 GlobalFree(clipdata
);
4796 GlobalFree(clipdata2
);
4800 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4803 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
4805 static HGLOBAL clipdata
= NULL
;
4806 static wchar_t *converted
= 0;
4815 GlobalUnlock(clipdata
);
4818 } else if (OpenClipboard(NULL
)) {
4819 if ((clipdata
= GetClipboardData(CF_UNICODETEXT
))) {
4821 *p
= GlobalLock(clipdata
);
4823 for (p2
= *p
; *p2
; p2
++);
4827 } else if ( (clipdata
= GetClipboardData(CF_TEXT
)) ) {
4831 s
= GlobalLock(clipdata
);
4832 i
= MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, 0, 0);
4833 *p
= converted
= snewn(i
, wchar_t);
4834 MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, converted
, i
);
4847 * Move `lines' lines from position `from' to position `to' in the
4850 void optimised_move(void *frontend
, int to
, int from
, int lines
)
4855 min
= (to
< from ? to
: from
);
4856 max
= to
+ from
- min
;
4858 r
.left
= offset_width
;
4859 r
.right
= offset_width
+ term
->cols
* font_width
;
4860 r
.top
= offset_height
+ min
* font_height
;
4861 r
.bottom
= offset_height
+ (max
+ lines
) * font_height
;
4862 ScrollWindow(hwnd
, 0, (to
- from
) * font_height
, &r
, &r
);
4867 * Print a message box and perform a fatal exit.
4869 void fatalbox(char *fmt
, ...)
4872 char *stuff
, morestuff
[100];
4875 stuff
= dupvprintf(fmt
, ap
);
4877 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4878 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
4884 * Print a modal (Really Bad) message box and perform a fatal exit.
4886 void modalfatalbox(char *fmt
, ...)
4889 char *stuff
, morestuff
[100];
4892 stuff
= dupvprintf(fmt
, ap
);
4894 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4895 MessageBox(hwnd
, stuff
, morestuff
,
4896 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
4901 static void flash_window(int mode
);
4902 static long next_flash
;
4903 static int flashing
= 0;
4905 static void flash_window_timer(void *ctx
, long now
)
4907 if (flashing
&& now
- next_flash
>= 0) {
4913 * Manage window caption / taskbar flashing, if enabled.
4914 * 0 = stop, 1 = maintain, 2 = start
4916 static void flash_window(int mode
)
4918 if ((mode
== 0) || (cfg
.beep_ind
== B_IND_DISABLED
)) {
4921 FlashWindow(hwnd
, FALSE
);
4925 } else if (mode
== 2) {
4929 FlashWindow(hwnd
, TRUE
);
4930 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
4933 } else if ((mode
== 1) && (cfg
.beep_ind
== B_IND_FLASH
)) {
4936 FlashWindow(hwnd
, TRUE
); /* toggle */
4937 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
4945 void do_beep(void *frontend
, int mode
)
4947 if (mode
== BELL_DEFAULT
) {
4949 * For MessageBeep style bells, we want to be careful of
4950 * timing, because they don't have the nice property of
4951 * PlaySound bells that each one cancels the previous
4952 * active one. So we limit the rate to one per 50ms or so.
4954 static long lastbeep
= 0;
4957 beepdiff
= GetTickCount() - lastbeep
;
4958 if (beepdiff
>= 0 && beepdiff
< 50)
4962 * The above MessageBeep call takes time, so we record the
4963 * time _after_ it finishes rather than before it starts.
4965 lastbeep
= GetTickCount();
4966 } else if (mode
== BELL_WAVEFILE
) {
4967 if (!PlaySound(cfg
.bell_wavefile
.path
, NULL
,
4968 SND_ASYNC
| SND_FILENAME
)) {
4969 char buf
[sizeof(cfg
.bell_wavefile
.path
) + 80];
4971 sprintf(buf
, "Unable to play sound file\n%s\n"
4972 "Using default sound instead", cfg
.bell_wavefile
.path
);
4973 sprintf(otherbuf
, "%.70s Sound Error", appname
);
4974 MessageBox(hwnd
, buf
, otherbuf
,
4975 MB_OK
| MB_ICONEXCLAMATION
);
4976 cfg
.beep
= BELL_DEFAULT
;
4978 } else if (mode
== BELL_PCSPEAKER
) {
4979 static long lastbeep
= 0;
4982 beepdiff
= GetTickCount() - lastbeep
;
4983 if (beepdiff
>= 0 && beepdiff
< 50)
4987 * We must beep in different ways depending on whether this
4988 * is a 95-series or NT-series OS.
4990 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
4994 lastbeep
= GetTickCount();
4996 /* Otherwise, either visual bell or disabled; do nothing here */
4997 if (!term
->has_focus
) {
4998 flash_window(2); /* start */
5003 * Minimise or restore the window in response to a server-side
5006 void set_iconic(void *frontend
, int iconic
)
5008 if (IsIconic(hwnd
)) {
5010 ShowWindow(hwnd
, SW_RESTORE
);
5013 ShowWindow(hwnd
, SW_MINIMIZE
);
5018 * Move the window in response to a server-side request.
5020 void move_window(void *frontend
, int x
, int y
)
5022 if (cfg
.resize_action
== RESIZE_DISABLED
||
5023 cfg
.resize_action
== RESIZE_FONT
||
5027 SetWindowPos(hwnd
, NULL
, x
, y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
);
5031 * Move the window to the top or bottom of the z-order in response
5032 * to a server-side request.
5034 void set_zorder(void *frontend
, int top
)
5036 if (cfg
.alwaysontop
)
5037 return; /* ignore */
5038 SetWindowPos(hwnd
, top ? HWND_TOP
: HWND_BOTTOM
, 0, 0, 0, 0,
5039 SWP_NOMOVE
| SWP_NOSIZE
);
5043 * Refresh the window in response to a server-side request.
5045 void refresh_window(void *frontend
)
5047 InvalidateRect(hwnd
, NULL
, TRUE
);
5051 * Maximise or restore the window in response to a server-side
5054 void set_zoomed(void *frontend
, int zoomed
)
5056 if (IsZoomed(hwnd
)) {
5058 ShowWindow(hwnd
, SW_RESTORE
);
5061 ShowWindow(hwnd
, SW_MAXIMIZE
);
5066 * Report whether the window is iconic, for terminal reports.
5068 int is_iconic(void *frontend
)
5070 return IsIconic(hwnd
);
5074 * Report the window's position, for terminal reports.
5076 void get_window_pos(void *frontend
, int *x
, int *y
)
5079 GetWindowRect(hwnd
, &r
);
5085 * Report the window's pixel size, for terminal reports.
5087 void get_window_pixels(void *frontend
, int *x
, int *y
)
5090 GetWindowRect(hwnd
, &r
);
5091 *x
= r
.right
- r
.left
;
5092 *y
= r
.bottom
- r
.top
;
5096 * Return the window or icon title.
5098 char *get_window_title(void *frontend
, int icon
)
5100 return icon ? icon_name
: window_name
;
5104 * See if we're in full-screen mode.
5106 static int is_full_screen()
5108 if (!IsZoomed(hwnd
))
5110 if (GetWindowLongPtr(hwnd
, GWL_STYLE
) & WS_CAPTION
)
5115 /* Get the rect/size of a full screen window using the nearest available
5116 * monitor in multimon systems; default to something sensible if only
5117 * one monitor is present. */
5118 static int get_fullscreen_rect(RECT
* ss
)
5120 #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
5123 mon
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
5124 mi
.cbSize
= sizeof(mi
);
5125 GetMonitorInfo(mon
, &mi
);
5127 /* structure copy */
5131 /* could also use code like this:
5132 ss->left = ss->top = 0;
5133 ss->right = GetSystemMetrics(SM_CXSCREEN);
5134 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
5136 return GetClientRect(GetDesktopWindow(), ss
);
5142 * Go full-screen. This should only be called when we are already
5145 static void make_full_screen()
5150 assert(IsZoomed(hwnd
));
5152 if (is_full_screen())
5155 /* Remove the window furniture. */
5156 style
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5157 style
&= ~(WS_CAPTION
| WS_BORDER
| WS_THICKFRAME
);
5158 if (cfg
.scrollbar_in_fullscreen
)
5159 style
|= WS_VSCROLL
;
5161 style
&= ~WS_VSCROLL
;
5162 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5164 /* Resize ourselves to exactly cover the nearest monitor. */
5165 get_fullscreen_rect(&ss
);
5166 SetWindowPos(hwnd
, HWND_TOP
, ss
.left
, ss
.top
,
5171 /* We may have changed size as a result */
5175 /* Tick the menu item in the System menu. */
5176 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
5181 * Clear the full-screen attributes.
5183 static void clear_full_screen()
5185 DWORD oldstyle
, style
;
5187 /* Reinstate the window furniture. */
5188 style
= oldstyle
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5189 style
|= WS_CAPTION
| WS_BORDER
;
5190 if (cfg
.resize_action
== RESIZE_DISABLED
)
5191 style
&= ~WS_THICKFRAME
;
5193 style
|= WS_THICKFRAME
;
5195 style
|= WS_VSCROLL
;
5197 style
&= ~WS_VSCROLL
;
5198 if (style
!= oldstyle
) {
5199 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5200 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
5201 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
5205 /* Untick the menu item in the System menu. */
5206 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
5211 * Toggle full-screen mode.
5213 static void flip_full_screen()
5215 if (is_full_screen()) {
5216 ShowWindow(hwnd
, SW_RESTORE
);
5217 } else if (IsZoomed(hwnd
)) {
5220 SendMessage(hwnd
, WM_FULLSCR_ON_MAX
, 0, 0);
5221 ShowWindow(hwnd
, SW_MAXIMIZE
);
5225 void frontend_keypress(void *handle
)
5228 * Keypress termination in non-Close-On-Exit mode is not
5229 * currently supported in PuTTY proper, because the window
5230 * always has a perfectly good Close button anyway. So we do
5236 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
5238 return term_data(term
, is_stderr
, data
, len
);
5241 int from_backend_untrusted(void *frontend
, const char *data
, int len
)
5243 return term_data_untrusted(term
, data
, len
);
5246 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
5249 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
5251 ret
= term_get_userpass_input(term
, p
, in
, inlen
);
5255 void agent_schedule_callback(void (*callback
)(void *, void *, int),
5256 void *callback_ctx
, void *data
, int len
)
5258 struct agent_callback
*c
= snew(struct agent_callback
);
5259 c
->callback
= callback
;
5260 c
->callback_ctx
= callback_ctx
;
5263 PostMessage(hwnd
, WM_AGENT_CALLBACK
, 0, (LPARAM
)c
);