2 * window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
3 * emulator and backend in a window.
13 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
21 #define COMPILE_MULTIMON_STUBS
31 /* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
32 * wParam are used by Windows, and should be masked off, so we shouldn't
33 * attempt to store information in them. Hence all these identifiers have
34 * the low 4 bits clear. Also, identifiers should < 0xF000. */
36 #define IDM_SHOWLOG 0x0010
37 #define IDM_NEWSESS 0x0020
38 #define IDM_DUPSESS 0x0030
39 #define IDM_RESTART 0x0040
40 #define IDM_RECONF 0x0050
41 #define IDM_CLRSB 0x0060
42 #define IDM_RESET 0x0070
43 #define IDM_HELP 0x0140
44 #define IDM_ABOUT 0x0150
45 #define IDM_SAVEDSESS 0x0160
46 #define IDM_COPYALL 0x0170
47 #define IDM_FULLSCREEN 0x0180
48 #define IDM_PASTE 0x0190
49 #define IDM_SPECIALSEP 0x0200
51 #define IDM_SPECIAL_MIN 0x0400
52 #define IDM_SPECIAL_MAX 0x0800
54 #define IDM_SAVED_MIN 0x1000
55 #define IDM_SAVED_MAX 0x5000
56 #define MENU_SAVED_STEP 16
57 /* Maximum number of sessions on saved-session submenu */
58 #define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
60 #define WM_IGNORE_CLIP (WM_APP + 2)
61 #define WM_FULLSCR_ON_MAX (WM_APP + 3)
62 #define WM_AGENT_CALLBACK (WM_APP + 4)
64 /* Needed for Chinese support and apparently not always defined. */
66 #define VK_PROCESSKEY 0xE5
69 /* Mouse wheel support. */
71 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
74 #define WHEEL_DELTA 120
77 static Mouse_Button
translate_button(Mouse_Button button
);
78 static LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
79 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
80 unsigned char *output
);
81 static void cfgtopalette(void);
82 static void systopalette(void);
83 static void init_palette(void);
84 static void init_fonts(int, int);
85 static void another_font(int);
86 static void deinit_fonts(void);
87 static void set_input_locale(HKL
);
88 static void update_savedsess_menu(void);
89 static void init_flashwindow(void);
91 static int is_full_screen(void);
92 static void make_full_screen(void);
93 static void clear_full_screen(void);
94 static void flip_full_screen(void);
96 /* Window layout information */
97 static void reset_window(int);
98 static int extra_width
, extra_height
;
99 static int font_width
, font_height
, font_dualwidth
;
100 static int offset_width
, offset_height
;
101 static int was_zoomed
= 0;
102 static int prev_rows
, prev_cols
;
104 static int pending_netevent
= 0;
105 static WPARAM pend_netevent_wParam
= 0;
106 static LPARAM pend_netevent_lParam
= 0;
107 static void enact_pending_netevent(void);
108 static void flash_window(int mode
);
109 static void sys_cursor_update(void);
110 static int is_shift_pressed(void);
111 static int get_fullscreen_rect(RECT
* ss
);
113 static int caret_x
= -1, caret_y
= -1;
115 static int kbd_codepage
;
118 static Backend
*back
;
119 static void *backhandle
;
121 static struct unicode_data ucsdata
;
122 static int must_close_session
, session_closed
;
123 static int reconfiguring
= FALSE
;
125 static const struct telnet_special
*specials
= NULL
;
126 static HMENU specials_menu
= NULL
;
127 static int n_specials
= 0;
129 #define TIMING_TIMER_ID 1234
130 static long timing_next_time
;
135 enum { SYSMENU
, CTXMENU
};
136 static HMENU savedsess_menu
;
138 Config cfg
; /* exported to windlg.c */
140 static struct sesslist sesslist
; /* for saved-session menu */
142 struct agent_callback
{
143 void (*callback
)(void *, void *, int);
149 #define FONT_NORMAL 0
151 #define FONT_UNDERLINE 2
152 #define FONT_BOLDUND 3
153 #define FONT_WIDE 0x04
154 #define FONT_HIGH 0x08
155 #define FONT_NARROW 0x10
157 #define FONT_OEM 0x20
158 #define FONT_OEMBOLD 0x21
159 #define FONT_OEMUND 0x22
160 #define FONT_OEMBOLDUND 0x23
162 #define FONT_MAXNO 0x2F
164 static HFONT fonts
[FONT_MAXNO
];
165 static LOGFONT lfont
;
166 static int fontflag
[FONT_MAXNO
];
168 BOLD_COLOURS
, BOLD_SHADOW
, BOLD_FONT
175 #define NCFGCOLOURS 22
176 #define NEXTCOLOURS 240
177 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
178 static COLORREF colours
[NALLCOLOURS
];
180 static LPLOGPALETTE logpal
;
181 static RGBTRIPLE defpal
[NALLCOLOURS
];
183 static HBITMAP caretbm
;
185 static int dbltime
, lasttime
, lastact
;
186 static Mouse_Button lastbtn
;
188 /* this allows xterm-style mouse handling. */
189 static int send_raw_mouse
= 0;
190 static int wheel_accumulator
= 0;
192 static int busy_status
= BUSY_NOT
;
194 static char *window_name
, *icon_name
;
196 static int compose_state
= 0;
198 static UINT wm_mousewheel
= WM_MOUSEWHEEL
;
200 /* Dummy routine, only required in plink. */
201 void ldisc_update(void *frontend
, int echo
, int edit
)
205 char *get_ttymode(void *frontend
, const char *mode
)
207 return term_get_ttymode(term
, mode
);
210 static void start_backend(void)
213 char msg
[1024], *title
;
218 * Select protocol. This is farmed out into a table in a
219 * separate file to enable an ssh-free variant.
222 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
223 if (backends
[i
].protocol
== cfg
.protocol
) {
224 back
= backends
[i
].backend
;
228 char *str
= dupprintf("%s Internal Error", appname
);
229 MessageBox(NULL
, "Unsupported protocol number found",
230 str
, MB_OK
| MB_ICONEXCLAMATION
);
235 error
= back
->init(NULL
, &backhandle
, &cfg
,
236 cfg
.host
, cfg
.port
, &realhost
, cfg
.tcp_nodelay
,
238 back
->provide_logctx(backhandle
, logctx
);
240 char *str
= dupprintf("%s Error", appname
);
241 sprintf(msg
, "Unable to open connection to\n"
242 "%.800s\n" "%s", cfg_dest(&cfg
), error
);
243 MessageBox(NULL
, msg
, str
, MB_ICONERROR
| MB_OK
);
247 window_name
= icon_name
= NULL
;
249 title
= cfg
.wintitle
;
251 sprintf(msg
, "%s - %s", realhost
, appname
);
255 set_title(NULL
, title
);
256 set_icon(NULL
, title
);
259 * Connect the terminal to the backend for resize purposes.
261 term_provide_resize_fn(term
, back
->size
, backhandle
);
264 * Set up a line discipline.
266 ldisc
= ldisc_create(&cfg
, term
, back
, backhandle
, NULL
);
269 * Destroy the Restart Session menu item. (This will return
270 * failure if it's already absent, as it will be the very first
271 * time we call this function. We ignore that, because as long
272 * as the menu item ends up not being there, we don't care
273 * whether it was us who removed it or not!)
275 for (i
= 0; i
< lenof(popup_menus
); i
++) {
276 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
279 must_close_session
= FALSE
;
280 session_closed
= FALSE
;
283 static void close_session(void)
288 session_closed
= TRUE
;
289 sprintf(morestuff
, "%.70s (inactive)", appname
);
290 set_icon(NULL
, morestuff
);
291 set_title(NULL
, morestuff
);
298 back
->free(backhandle
);
301 update_specials_menu(NULL
);
305 * Show the Restart Session menu item. Do a precautionary
306 * delete first to ensure we never end up with more than one.
308 for (i
= 0; i
< lenof(popup_menus
); i
++) {
309 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
310 InsertMenu(popup_menus
[i
].menu
, IDM_DUPSESS
, MF_BYCOMMAND
| MF_ENABLED
,
311 IDM_RESTART
, "&Restart Session");
315 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
319 int guess_width
, guess_height
;
323 flags
= FLAG_VERBOSE
| FLAG_INTERACTIVE
;
327 InitCommonControls();
329 /* Ensure a Maximize setting in Explorer doesn't maximise the
335 char *str
= dupprintf("%s Fatal Error", appname
);
336 MessageBox(NULL
, "Windows refuses to report a version",
337 str
, MB_OK
| MB_ICONEXCLAMATION
);
343 * If we're running a version of Windows that doesn't support
344 * WM_MOUSEWHEEL, find out what message number we should be
347 if (osVersion
.dwMajorVersion
< 4 ||
348 (osVersion
.dwMajorVersion
== 4 &&
349 osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
))
350 wm_mousewheel
= RegisterWindowMessage("MSWHEEL_ROLLMSG");
357 * Process the command line.
363 default_protocol
= be_default_protocol
;
364 /* Find the appropriate default port. */
367 default_port
= 0; /* illegal */
368 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
369 if (backends
[i
].protocol
== default_protocol
) {
370 default_port
= backends
[i
].backend
->default_port
;
374 cfg
.logtype
= LGTYP_NONE
;
376 do_defaults(NULL
, &cfg
);
381 * Process a couple of command-line options which are more
382 * easily dealt with before the line is broken up into
383 * words. These are the soon-to-be-defunct @sessionname and
384 * the internal-use-only &sharedmemoryhandle, neither of
385 * which are combined with anything else.
387 while (*p
&& isspace(*p
))
391 while (i
> 1 && isspace(p
[i
- 1]))
394 do_defaults(p
+ 1, &cfg
);
395 if (!cfg_launchable(&cfg
) && !do_config()) {
398 } else if (*p
== '&') {
400 * An initial & means we've been given a command line
401 * containing the hex value of a HANDLE for a file
402 * mapping object, which we must then extract as a
407 if (sscanf(p
+ 1, "%p", &filemap
) == 1 &&
408 (cp
= MapViewOfFile(filemap
, FILE_MAP_READ
,
409 0, 0, sizeof(Config
))) != NULL
) {
412 CloseHandle(filemap
);
413 } else if (!do_config()) {
418 * Otherwise, break up the command line and deal with
424 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
426 for (i
= 0; i
< argc
; i
++) {
430 ret
= cmdline_process_param(p
, i
+1<argc?argv
[i
+1]:NULL
,
433 cmdline_error("option \"%s\" requires an argument", p
);
434 } else if (ret
== 2) {
435 i
++; /* skip next argument */
436 } else if (ret
== 1) {
437 continue; /* nothing further needs doing */
438 } else if (!strcmp(p
, "-cleanup") ||
439 !strcmp(p
, "-cleanup-during-uninstall")) {
441 * `putty -cleanup'. Remove all registry
442 * entries associated with PuTTY, and also find
443 * and delete the random seed file.
446 /* Are we being invoked from an uninstaller? */
447 if (!strcmp(p
, "-cleanup-during-uninstall")) {
448 s1
= dupprintf("Remove saved sessions and random seed file?\n"
450 "If you hit Yes, ALL Registry entries associated\n"
451 "with %s will be removed, as well as the\n"
452 "random seed file. THIS PROCESS WILL\n"
453 "DESTROY YOUR SAVED SESSIONS.\n"
454 "(This only affects the currently logged-in user.)\n"
456 "If you hit No, uninstallation will proceed, but\n"
457 "saved sessions etc will be left on the machine.",
459 s2
= dupprintf("%s Uninstallation", appname
);
461 s1
= dupprintf("This procedure will remove ALL Registry entries\n"
462 "associated with %s, and will also remove\n"
463 "the random seed file. (This only affects the\n"
464 "currently logged-in user.)\n"
466 "THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
467 "Are you really sure you want to continue?",
469 s2
= dupprintf("%s Warning", appname
);
471 if (message_box(s1
, s2
,
472 MB_YESNO
| MB_ICONWARNING
| MB_DEFBUTTON2
,
473 HELPCTXID(option_cleanup
)) == IDYES
) {
479 } else if (!strcmp(p
, "-pgpfp")) {
482 } else if (*p
!= '-') {
486 * If we already have a host name, treat
487 * this argument as a port number. NB we
488 * have to treat this as a saved -P
489 * argument, so that it will be deferred
490 * until it's a good moment to run it.
492 int ret
= cmdline_process_param("-P", p
, 1, &cfg
);
494 } else if (!strncmp(q
, "telnet:", 7)) {
496 * If the hostname starts with "telnet:",
497 * set the protocol to Telnet and process
498 * the string as a Telnet URL.
503 if (q
[0] == '/' && q
[1] == '/')
505 cfg
.protocol
= PROT_TELNET
;
507 while (*p
&& *p
!= ':' && *p
!= '/')
516 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
517 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
521 * Otherwise, treat this argument as a host
524 while (*p
&& !isspace(*p
))
528 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
529 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
533 cmdline_error("unknown option \"%s\"", p
);
538 cmdline_run_saved(&cfg
);
540 if (!cfg_launchable(&cfg
) && !do_config()) {
545 * Trim leading whitespace off the hostname if it's there.
548 int space
= strspn(cfg
.host
, " \t");
549 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
552 /* See if host is of the form user@host */
553 if (cfg
.host
[0] != '\0') {
554 char *atsign
= strrchr(cfg
.host
, '@');
555 /* Make sure we're not overflowing the user field */
557 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
558 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
559 cfg
.username
[atsign
- cfg
.host
] = '\0';
561 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
566 * Trim a colon suffix off the hostname if it's there. In
567 * order to protect IPv6 address literals against this
568 * treatment, we do not do this if there's _more_ than one
572 char *c
= strchr(cfg
.host
, ':');
575 char *d
= strchr(c
+1, ':');
582 * Remove any remaining whitespace from the hostname.
586 while (cfg
.host
[p2
] != '\0') {
587 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
588 cfg
.host
[p1
] = cfg
.host
[p2
];
597 /* Check for invalid Port number (i.e. zero) */
599 char *str
= dupprintf("%s Internal Error", appname
);
600 MessageBox(NULL
, "Invalid Port Number",
601 str
, MB_OK
| MB_ICONEXCLAMATION
);
608 wndclass
.lpfnWndProc
= WndProc
;
609 wndclass
.cbClsExtra
= 0;
610 wndclass
.cbWndExtra
= 0;
611 wndclass
.hInstance
= inst
;
612 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
613 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
614 wndclass
.hbrBackground
= NULL
;
615 wndclass
.lpszMenuName
= NULL
;
616 wndclass
.lpszClassName
= appname
;
618 RegisterClass(&wndclass
);
621 memset(&ucsdata
, 0, sizeof(ucsdata
));
626 * Guess some defaults for the window size. This all gets
627 * updated later, so we don't really care too much. However, we
628 * do want the font width/height guesses to correspond to a
629 * large font rather than a small one...
636 guess_width
= extra_width
+ font_width
* cfg
.width
;
637 guess_height
= extra_height
+ font_height
* cfg
.height
;
640 get_fullscreen_rect(&r
);
641 if (guess_width
> r
.right
- r
.left
)
642 guess_width
= r
.right
- r
.left
;
643 if (guess_height
> r
.bottom
- r
.top
)
644 guess_height
= r
.bottom
- r
.top
;
648 int winmode
= WS_OVERLAPPEDWINDOW
| WS_VSCROLL
;
651 winmode
&= ~(WS_VSCROLL
);
652 if (cfg
.resize_action
== RESIZE_DISABLED
)
653 winmode
&= ~(WS_THICKFRAME
| WS_MAXIMIZEBOX
);
655 exwinmode
|= WS_EX_TOPMOST
;
657 exwinmode
|= WS_EX_CLIENTEDGE
;
658 hwnd
= CreateWindowEx(exwinmode
, appname
, appname
,
659 winmode
, CW_USEDEFAULT
, CW_USEDEFAULT
,
660 guess_width
, guess_height
,
661 NULL
, NULL
, inst
, NULL
);
665 * Initialise the terminal. (We have to do this _after_
666 * creating the window, since the terminal is the first thing
667 * which will call schedule_timer(), which will in turn call
668 * timer_change_notify() which will expect hwnd to exist.)
670 term
= term_init(&cfg
, &ucsdata
, NULL
);
671 logctx
= log_init(NULL
, &cfg
);
672 term_provide_logctx(term
, logctx
);
673 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
676 * Initialise the fonts, simultaneously correcting the guesses
677 * for font_{width,height}.
682 * Correct the guesses for extra_{width,height}.
686 GetWindowRect(hwnd
, &wr
);
687 GetClientRect(hwnd
, &cr
);
688 offset_width
= offset_height
= cfg
.window_border
;
689 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
690 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
694 * Resize the window, now we know what size we _really_ want it
697 guess_width
= extra_width
+ font_width
* term
->cols
;
698 guess_height
= extra_height
+ font_height
* term
->rows
;
699 SetWindowPos(hwnd
, NULL
, 0, 0, guess_width
, guess_height
,
700 SWP_NOMOVE
| SWP_NOREDRAW
| SWP_NOZORDER
);
703 * Set up a caret bitmap, with no content.
707 int size
= (font_width
+ 15) / 16 * 2 * font_height
;
708 bits
= snewn(size
, char);
709 memset(bits
, 0, size
);
710 caretbm
= CreateBitmap(font_width
, font_height
, 1, 1, bits
);
713 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
716 * Initialise the scroll bar.
721 si
.cbSize
= sizeof(si
);
722 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
724 si
.nMax
= term
->rows
- 1;
725 si
.nPage
= term
->rows
;
727 SetScrollInfo(hwnd
, SB_VERT
, &si
, FALSE
);
731 * Prepare the mouse handler.
733 lastact
= MA_NOTHING
;
734 lastbtn
= MBT_NOTHING
;
735 dbltime
= GetDoubleClickTime();
738 * Set up the session-control options on the system menu.
745 popup_menus
[SYSMENU
].menu
= GetSystemMenu(hwnd
, FALSE
);
746 popup_menus
[CTXMENU
].menu
= CreatePopupMenu();
747 AppendMenu(popup_menus
[CTXMENU
].menu
, MF_ENABLED
, IDM_PASTE
, "&Paste");
749 savedsess_menu
= CreateMenu();
750 get_sesslist(&sesslist
, TRUE
);
751 update_savedsess_menu();
753 for (j
= 0; j
< lenof(popup_menus
); j
++) {
754 m
= popup_menus
[j
].menu
;
756 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
757 AppendMenu(m
, MF_ENABLED
, IDM_SHOWLOG
, "&Event Log");
758 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
759 AppendMenu(m
, MF_ENABLED
, IDM_NEWSESS
, "Ne&w Session...");
760 AppendMenu(m
, MF_ENABLED
, IDM_DUPSESS
, "&Duplicate Session");
761 AppendMenu(m
, MF_POPUP
| MF_ENABLED
, (UINT
) savedsess_menu
,
763 AppendMenu(m
, MF_ENABLED
, IDM_RECONF
, "Chan&ge Settings...");
764 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
765 AppendMenu(m
, MF_ENABLED
, IDM_COPYALL
, "C&opy All to Clipboard");
766 AppendMenu(m
, MF_ENABLED
, IDM_CLRSB
, "C&lear Scrollback");
767 AppendMenu(m
, MF_ENABLED
, IDM_RESET
, "Rese&t Terminal");
768 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
769 AppendMenu(m
, (cfg
.resize_action
== RESIZE_DISABLED
) ?
770 MF_GRAYED
: MF_ENABLED
, IDM_FULLSCREEN
, "&Full Screen");
771 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
773 AppendMenu(m
, MF_ENABLED
, IDM_HELP
, "&Help");
774 str
= dupprintf("&About %s", appname
);
775 AppendMenu(m
, MF_ENABLED
, IDM_ABOUT
, str
);
783 * Set up the initial input locale.
785 set_input_locale(GetKeyboardLayout(0));
788 * Finally show the window!
790 ShowWindow(hwnd
, show
);
791 SetForegroundWindow(hwnd
);
794 * Set the palette up.
800 term_set_focus(term
, GetForegroundWindow() == hwnd
);
807 handles
= handle_get_events(&nhandles
);
809 n
= MsgWaitForMultipleObjects(nhandles
, handles
, FALSE
, INFINITE
,
812 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
813 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
815 if (must_close_session
)
821 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
822 if (msg
.message
== WM_QUIT
)
823 goto finished
; /* two-level break */
825 if (!(IsWindow(logbox
) && IsDialogMessage(logbox
, &msg
)))
826 DispatchMessage(&msg
);
827 /* Send the paste buffer if there's anything to send */
829 /* If there's nothing new in the queue then we can do everything
830 * we've delayed, reading the socket, writing, and repainting
833 if (must_close_session
)
837 /* The messages seem unreliable; especially if we're being tricky */
838 term_set_focus(term
, GetForegroundWindow() == hwnd
);
840 if (pending_netevent
)
841 enact_pending_netevent();
843 net_pending_errors();
847 cleanup_exit(msg
.wParam
); /* this doesn't return... */
848 return msg
.wParam
; /* ... but optimiser doesn't know */
854 void cleanup_exit(int code
)
865 if (cfg
.protocol
== PROT_SSH
) {
877 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
879 char *do_select(SOCKET skt
, int startup
)
884 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
885 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
890 return "do_select(): internal error (hwnd==NULL)";
891 if (p_WSAAsyncSelect(skt
, hwnd
, msg
, events
) == SOCKET_ERROR
) {
892 switch (p_WSAGetLastError()) {
894 return "Network is down";
896 return "WSAAsyncSelect(): unknown error";
903 * Refresh the saved-session submenu from `sesslist'.
905 static void update_savedsess_menu(void)
908 while (DeleteMenu(savedsess_menu
, 0, MF_BYPOSITION
)) ;
909 /* skip sesslist.sessions[0] == Default Settings */
911 i
< ((sesslist
.nsessions
<= MENU_SAVED_MAX
+1) ? sesslist
.nsessions
914 AppendMenu(savedsess_menu
, MF_ENABLED
,
915 IDM_SAVED_MIN
+ (i
-1)*MENU_SAVED_STEP
,
916 sesslist
.sessions
[i
]);
920 * Update the Special Commands submenu.
922 void update_specials_menu(void *frontend
)
928 specials
= back
->get_specials(backhandle
);
933 /* We can't use Windows to provide a stack for submenus, so
934 * here's a lame "stack" that will do for now. */
935 HMENU saved_menu
= NULL
;
937 new_menu
= CreatePopupMenu();
938 for (i
= 0; nesting
> 0; i
++) {
939 assert(IDM_SPECIAL_MIN
+ 0x10 * i
< IDM_SPECIAL_MAX
);
940 switch (specials
[i
].code
) {
942 AppendMenu(new_menu
, MF_SEPARATOR
, 0, 0);
947 saved_menu
= new_menu
; /* XXX lame stacking */
948 new_menu
= CreatePopupMenu();
949 AppendMenu(saved_menu
, MF_POPUP
| MF_ENABLED
,
950 (UINT
) new_menu
, specials
[i
].name
);
955 new_menu
= saved_menu
; /* XXX lame stacking */
960 AppendMenu(new_menu
, MF_ENABLED
, IDM_SPECIAL_MIN
+ 0x10 * i
,
965 /* Squirrel the highest special. */
972 for (j
= 0; j
< lenof(popup_menus
); j
++) {
974 /* XXX does this free up all submenus? */
975 DeleteMenu(popup_menus
[j
].menu
, (UINT
)specials_menu
, MF_BYCOMMAND
);
976 DeleteMenu(popup_menus
[j
].menu
, IDM_SPECIALSEP
, MF_BYCOMMAND
);
979 InsertMenu(popup_menus
[j
].menu
, IDM_SHOWLOG
,
980 MF_BYCOMMAND
| MF_POPUP
| MF_ENABLED
,
981 (UINT
) new_menu
, "S&pecial Command");
982 InsertMenu(popup_menus
[j
].menu
, IDM_SHOWLOG
,
983 MF_BYCOMMAND
| MF_SEPARATOR
, IDM_SPECIALSEP
, 0);
986 specials_menu
= new_menu
;
989 static void update_mouse_pointer(void)
992 int force_visible
= FALSE
;
993 static int forced_visible
= FALSE
;
994 switch (busy_status
) {
997 curstype
= IDC_ARROW
;
999 curstype
= IDC_IBEAM
;
1002 curstype
= IDC_APPSTARTING
; /* this may be an abuse */
1003 force_visible
= TRUE
;
1006 curstype
= IDC_WAIT
;
1007 force_visible
= TRUE
;
1013 HCURSOR cursor
= LoadCursor(NULL
, curstype
);
1014 SetClassLongPtr(hwnd
, GCLP_HCURSOR
, (LONG_PTR
)cursor
);
1015 SetCursor(cursor
); /* force redraw of cursor at current posn */
1017 if (force_visible
!= forced_visible
) {
1018 /* We want some cursor shapes to be visible always.
1019 * Along with show_mouseptr(), this manages the ShowCursor()
1020 * counter such that if we switch back to a non-force_visible
1021 * cursor, the previous visibility state is restored. */
1022 ShowCursor(force_visible
);
1023 forced_visible
= force_visible
;
1027 void set_busy_status(void *frontend
, int status
)
1029 busy_status
= status
;
1030 update_mouse_pointer();
1034 * set or clear the "raw mouse message" mode
1036 void set_raw_mouse_mode(void *frontend
, int activate
)
1038 activate
= activate
&& !cfg
.no_mouse_rep
;
1039 send_raw_mouse
= activate
;
1040 update_mouse_pointer();
1044 * Print a message box and close the connection.
1046 void connection_fatal(void *frontend
, char *fmt
, ...)
1049 char *stuff
, morestuff
[100];
1052 stuff
= dupvprintf(fmt
, ap
);
1054 sprintf(morestuff
, "%.70s Fatal Error", appname
);
1055 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
1058 if (cfg
.close_on_exit
== FORCE_ON
)
1061 must_close_session
= TRUE
;
1066 * Report an error at the command-line parsing stage.
1068 void cmdline_error(char *fmt
, ...)
1071 char *stuff
, morestuff
[100];
1074 stuff
= dupvprintf(fmt
, ap
);
1076 sprintf(morestuff
, "%.70s Command Line Error", appname
);
1077 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
1083 * Actually do the job requested by a WM_NETEVENT
1085 static void enact_pending_netevent(void)
1087 static int reentering
= 0;
1088 extern int select_result(WPARAM
, LPARAM
);
1091 return; /* don't unpend the pending */
1093 pending_netevent
= FALSE
;
1096 select_result(pend_netevent_wParam
, pend_netevent_lParam
);
1101 * Copy the colour palette from the configuration data into defpal.
1102 * This is non-trivial because the colour indices are different.
1104 static void cfgtopalette(void)
1107 static const int ww
[] = {
1108 256, 257, 258, 259, 260, 261,
1109 0, 8, 1, 9, 2, 10, 3, 11,
1110 4, 12, 5, 13, 6, 14, 7, 15
1113 for (i
= 0; i
< 22; i
++) {
1115 defpal
[w
].rgbtRed
= cfg
.colours
[i
][0];
1116 defpal
[w
].rgbtGreen
= cfg
.colours
[i
][1];
1117 defpal
[w
].rgbtBlue
= cfg
.colours
[i
][2];
1119 for (i
= 0; i
< NEXTCOLOURS
; i
++) {
1121 int r
= i
/ 36, g
= (i
/ 6) % 6, b
= i
% 6;
1122 defpal
[i
+16].rgbtRed
= r ? r
* 40 + 55 : 0;
1123 defpal
[i
+16].rgbtGreen
= g ? g
* 40 + 55 : 0;
1124 defpal
[i
+16].rgbtBlue
= b ? b
* 40 + 55 : 0;
1126 int shade
= i
- 216;
1127 shade
= shade
* 10 + 8;
1128 defpal
[i
+16].rgbtRed
= defpal
[i
+16].rgbtGreen
=
1129 defpal
[i
+16].rgbtBlue
= shade
;
1133 /* Override with system colours if appropriate */
1134 if (cfg
.system_colour
)
1139 * Override bit of defpal with colours from the system.
1140 * (NB that this takes a copy the system colours at the time this is called,
1141 * so subsequent colour scheme changes don't take effect. To fix that we'd
1142 * probably want to be using GetSysColorBrush() and the like.)
1144 static void systopalette(void)
1147 static const struct { int nIndex
; int norm
; int bold
; } or[] =
1149 { COLOR_WINDOWTEXT
, 256, 257 }, /* Default Foreground */
1150 { COLOR_WINDOW
, 258, 259 }, /* Default Background */
1151 { COLOR_HIGHLIGHTTEXT
, 260, 260 }, /* Cursor Text */
1152 { COLOR_HIGHLIGHT
, 261, 261 }, /* Cursor Colour */
1155 for (i
= 0; i
< (sizeof(or)/sizeof(or[0])); i
++) {
1156 COLORREF colour
= GetSysColor(or[i
].nIndex
);
1157 defpal
[or[i
].norm
].rgbtRed
=
1158 defpal
[or[i
].bold
].rgbtRed
= GetRValue(colour
);
1159 defpal
[or[i
].norm
].rgbtGreen
=
1160 defpal
[or[i
].bold
].rgbtGreen
= GetGValue(colour
);
1161 defpal
[or[i
].norm
].rgbtBlue
=
1162 defpal
[or[i
].bold
].rgbtBlue
= GetBValue(colour
);
1167 * Set up the colour palette.
1169 static void init_palette(void)
1172 HDC hdc
= GetDC(hwnd
);
1174 if (cfg
.try_palette
&& GetDeviceCaps(hdc
, RASTERCAPS
) & RC_PALETTE
) {
1176 * This is a genuine case where we must use smalloc
1177 * because the snew macros can't cope.
1179 logpal
= smalloc(sizeof(*logpal
)
1180 - sizeof(logpal
->palPalEntry
)
1181 + NALLCOLOURS
* sizeof(PALETTEENTRY
));
1182 logpal
->palVersion
= 0x300;
1183 logpal
->palNumEntries
= NALLCOLOURS
;
1184 for (i
= 0; i
< NALLCOLOURS
; i
++) {
1185 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
1186 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
1187 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
1188 logpal
->palPalEntry
[i
].peFlags
= PC_NOCOLLAPSE
;
1190 pal
= CreatePalette(logpal
);
1192 SelectPalette(hdc
, pal
, FALSE
);
1193 RealizePalette(hdc
);
1194 SelectPalette(hdc
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
1197 ReleaseDC(hwnd
, hdc
);
1200 for (i
= 0; i
< NALLCOLOURS
; i
++)
1201 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
1202 defpal
[i
].rgbtGreen
,
1203 defpal
[i
].rgbtBlue
);
1205 for (i
= 0; i
< NALLCOLOURS
; i
++)
1206 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
1207 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
1211 * This is a wrapper to ExtTextOut() to force Windows to display
1212 * the precise glyphs we give it. Otherwise it would do its own
1213 * bidi and Arabic shaping, and we would end up uncertain which
1214 * characters it had put where.
1216 static void exact_textout(HDC hdc
, int x
, int y
, CONST RECT
*lprc
,
1217 unsigned short *lpString
, UINT cbCount
,
1218 CONST INT
*lpDx
, int opaque
)
1222 * The LCC include files apparently don't supply the
1223 * GCP_RESULTSW type, but we can make do with GCP_RESULTS
1224 * proper: the differences aren't important to us (the only
1225 * variable-width string parameter is one we don't use anyway).
1231 char *buffer
= snewn(cbCount
*2+2, char);
1232 char *classbuffer
= snewn(cbCount
, char);
1233 memset(&gcpr
, 0, sizeof(gcpr
));
1234 memset(buffer
, 0, cbCount
*2+2);
1235 memset(classbuffer
, GCPCLASS_NEUTRAL
, cbCount
);
1237 gcpr
.lStructSize
= sizeof(gcpr
);
1238 gcpr
.lpGlyphs
= (void *)buffer
;
1239 gcpr
.lpClass
= (void *)classbuffer
;
1240 gcpr
.nGlyphs
= cbCount
;
1242 GetCharacterPlacementW(hdc
, lpString
, cbCount
, 0, &gcpr
,
1243 FLI_MASK
| GCP_CLASSIN
| GCP_DIACRITIC
);
1245 ExtTextOut(hdc
, x
, y
,
1246 ETO_GLYPH_INDEX
| ETO_CLIPPED
| (opaque ? ETO_OPAQUE
: 0),
1247 lprc
, buffer
, cbCount
, lpDx
);
1251 * The exact_textout() wrapper, unfortunately, destroys the useful
1252 * Windows `font linking' behaviour: automatic handling of Unicode
1253 * code points not supported in this font by falling back to a font
1254 * which does contain them. Therefore, we adopt a multi-layered
1255 * approach: for any potentially-bidi text, we use exact_textout(),
1256 * and for everything else we use a simple ExtTextOut as we did
1257 * before exact_textout() was introduced.
1259 static void general_textout(HDC hdc
, int x
, int y
, CONST RECT
*lprc
,
1260 unsigned short *lpString
, UINT cbCount
,
1261 CONST INT
*lpDx
, int opaque
)
1266 #ifdef FIXME_REMOVE_BEFORE_CHECKIN
1268 debug(("general_textout: %d,%d", x
, y
));
1269 for(k
=0;k
<cbCount
;k
++)debug((" U+%04X", lpString
[k
]));
1270 debug(("\n rect: [%d,%d %d,%d]", lprc
->left
, lprc
->top
, lprc
->right
, lprc
->bottom
));
1276 for (i
= 0; i
< (int)cbCount
;) {
1277 int rtl
= is_rtl(lpString
[i
]);
1281 for (j
= i
+1; j
< (int)cbCount
; j
++) {
1282 if (rtl
!= is_rtl(lpString
[j
]))
1288 * Now [i,j) indicates a maximal substring of lpString
1289 * which should be displayed using the same textout
1293 newrc
.left
= lprc
->left
+ xp
- x
;
1294 newrc
.right
= lprc
->left
+ xn
- x
;
1295 newrc
.top
= lprc
->top
;
1296 newrc
.bottom
= lprc
->bottom
;
1297 #ifdef FIXME_REMOVE_BEFORE_CHECKIN
1300 debug((" exact_textout: %d,%d", xp
, y
));
1301 for(k
=0;k
<j
-i
;k
++)debug((" U+%04X", lpString
[i
+k
]));
1302 debug(("\n rect: [%d,%d %d,%d]\n", newrc
.left
, newrc
.top
, newrc
.right
, newrc
.bottom
));
1305 exact_textout(hdc
, xp
, y
, &newrc
, lpString
+i
, j
-i
, lpDx
+i
, opaque
);
1307 #ifdef FIXME_REMOVE_BEFORE_CHECKIN
1310 debug((" ExtTextOut : %d,%d", xp
, y
));
1311 for(k
=0;k
<j
-i
;k
++)debug((" U+%04X", lpString
[i
+k
]));
1312 debug(("\n rect: [%d,%d %d,%d]\n", newrc
.left
, newrc
.top
, newrc
.right
, newrc
.bottom
));
1315 newrc
.left
= lprc
->left
+ xp
- x
;
1316 newrc
.right
= lprc
->left
+ xn
- x
;
1317 newrc
.top
= lprc
->top
;
1318 newrc
.bottom
= lprc
->bottom
;
1319 ExtTextOutW(hdc
, xp
, y
, ETO_CLIPPED
| (opaque ? ETO_OPAQUE
: 0),
1320 &newrc
, lpString
+i
, j
-i
, lpDx
+i
);
1327 #ifdef FIXME_REMOVE_BEFORE_CHECKIN
1328 debug(("general_textout: done, xn=%d\n", xn
));
1330 assert(xn
- x
== lprc
->right
- lprc
->left
);
1334 * Initialise all the fonts we will need initially. There may be as many as
1335 * three or as few as one. The other (poentially) twentyone fonts are done
1336 * if/when they are needed.
1340 * - check the font width and height, correcting our guesses if
1343 * - verify that the bold font is the same width as the ordinary
1344 * one, and engage shadow bolding if not.
1346 * - verify that the underlined font is the same width as the
1347 * ordinary one (manual underlining by means of line drawing can
1348 * be done in a pinch).
1350 static void init_fonts(int pick_width
, int pick_height
)
1357 int fw_dontcare
, fw_bold
;
1359 for (i
= 0; i
< FONT_MAXNO
; i
++)
1362 bold_mode
= cfg
.bold_colour ? BOLD_COLOURS
: BOLD_FONT
;
1363 und_mode
= UND_FONT
;
1365 if (cfg
.font
.isbold
) {
1366 fw_dontcare
= FW_BOLD
;
1369 fw_dontcare
= FW_DONTCARE
;
1376 font_height
= pick_height
;
1378 font_height
= cfg
.font
.height
;
1379 if (font_height
> 0) {
1381 -MulDiv(font_height
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1384 font_width
= pick_width
;
1386 #define f(i,c,w,u) \
1387 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1388 c, OUT_DEFAULT_PRECIS, \
1389 CLIP_DEFAULT_PRECIS, FONT_QUALITY(cfg.font_quality), \
1390 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1392 f(FONT_NORMAL
, cfg
.font
.charset
, fw_dontcare
, FALSE
);
1394 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
1395 GetTextMetrics(hdc
, &tm
);
1397 GetObject(fonts
[FONT_NORMAL
], sizeof(LOGFONT
), &lfont
);
1399 if (pick_width
== 0 || pick_height
== 0) {
1400 font_height
= tm
.tmHeight
;
1401 font_width
= tm
.tmAveCharWidth
;
1403 font_dualwidth
= (tm
.tmAveCharWidth
!= tm
.tmMaxCharWidth
);
1405 #ifdef RDB_DEBUG_PATCH
1406 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1407 tm
.tmHeight
, tm
.tmAveCharWidth
, tm
.tmMaxCharWidth
);
1412 DWORD cset
= tm
.tmCharSet
;
1413 memset(&info
, 0xFF, sizeof(info
));
1415 /* !!! Yes the next line is right */
1416 if (cset
== OEM_CHARSET
)
1417 ucsdata
.font_codepage
= GetOEMCP();
1419 if (TranslateCharsetInfo ((DWORD
*) cset
, &info
, TCI_SRCCHARSET
))
1420 ucsdata
.font_codepage
= info
.ciACP
;
1422 ucsdata
.font_codepage
= -1;
1424 GetCPInfo(ucsdata
.font_codepage
, &cpinfo
);
1425 ucsdata
.dbcs_screenfont
= (cpinfo
.MaxCharSize
> 1);
1428 f(FONT_UNDERLINE
, cfg
.font
.charset
, fw_dontcare
, TRUE
);
1431 * Some fonts, e.g. 9-pt Courier, draw their underlines
1432 * outside their character cell. We successfully prevent
1433 * screen corruption by clipping the text output, but then
1434 * we lose the underline completely. Here we try to work
1435 * out whether this is such a font, and if it is, we set a
1436 * flag that causes underlines to be drawn by hand.
1438 * Having tried other more sophisticated approaches (such
1439 * as examining the TEXTMETRIC structure or requesting the
1440 * height of a string), I think we'll do this the brute
1441 * force way: we create a small bitmap, draw an underlined
1442 * space on it, and test to see whether any pixels are
1443 * foreground-coloured. (Since we expect the underline to
1444 * go all the way across the character cell, we only search
1445 * down a single column of the bitmap, half way across.)
1449 HBITMAP und_bm
, und_oldbm
;
1453 und_dc
= CreateCompatibleDC(hdc
);
1454 und_bm
= CreateCompatibleBitmap(hdc
, font_width
, font_height
);
1455 und_oldbm
= SelectObject(und_dc
, und_bm
);
1456 SelectObject(und_dc
, fonts
[FONT_UNDERLINE
]);
1457 SetTextAlign(und_dc
, TA_TOP
| TA_LEFT
| TA_NOUPDATECP
);
1458 SetTextColor(und_dc
, RGB(255, 255, 255));
1459 SetBkColor(und_dc
, RGB(0, 0, 0));
1460 SetBkMode(und_dc
, OPAQUE
);
1461 ExtTextOut(und_dc
, 0, 0, ETO_OPAQUE
, NULL
, " ", 1, NULL
);
1463 for (i
= 0; i
< font_height
; i
++) {
1464 c
= GetPixel(und_dc
, font_width
/ 2, i
);
1465 if (c
!= RGB(0, 0, 0))
1468 SelectObject(und_dc
, und_oldbm
);
1469 DeleteObject(und_bm
);
1472 und_mode
= UND_LINE
;
1473 DeleteObject(fonts
[FONT_UNDERLINE
]);
1474 fonts
[FONT_UNDERLINE
] = 0;
1478 if (bold_mode
== BOLD_FONT
) {
1479 f(FONT_BOLD
, cfg
.font
.charset
, fw_bold
, FALSE
);
1483 descent
= tm
.tmAscent
+ 1;
1484 if (descent
>= font_height
)
1485 descent
= font_height
- 1;
1487 for (i
= 0; i
< 3; i
++) {
1489 if (SelectObject(hdc
, fonts
[i
]) && GetTextMetrics(hdc
, &tm
))
1490 fontsize
[i
] = tm
.tmAveCharWidth
+ 256 * tm
.tmHeight
;
1497 ReleaseDC(hwnd
, hdc
);
1499 if (fontsize
[FONT_UNDERLINE
] != fontsize
[FONT_NORMAL
]) {
1500 und_mode
= UND_LINE
;
1501 DeleteObject(fonts
[FONT_UNDERLINE
]);
1502 fonts
[FONT_UNDERLINE
] = 0;
1505 if (bold_mode
== BOLD_FONT
&&
1506 fontsize
[FONT_BOLD
] != fontsize
[FONT_NORMAL
]) {
1507 bold_mode
= BOLD_SHADOW
;
1508 DeleteObject(fonts
[FONT_BOLD
]);
1509 fonts
[FONT_BOLD
] = 0;
1511 fontflag
[0] = fontflag
[1] = fontflag
[2] = 1;
1513 init_ucs(&cfg
, &ucsdata
);
1516 static void another_font(int fontno
)
1519 int fw_dontcare
, fw_bold
;
1523 if (fontno
< 0 || fontno
>= FONT_MAXNO
|| fontflag
[fontno
])
1526 basefont
= (fontno
& ~(FONT_BOLDUND
));
1527 if (basefont
!= fontno
&& !fontflag
[basefont
])
1528 another_font(basefont
);
1530 if (cfg
.font
.isbold
) {
1531 fw_dontcare
= FW_BOLD
;
1534 fw_dontcare
= FW_DONTCARE
;
1538 c
= cfg
.font
.charset
;
1544 if (fontno
& FONT_WIDE
)
1546 if (fontno
& FONT_NARROW
)
1548 if (fontno
& FONT_OEM
)
1550 if (fontno
& FONT_BOLD
)
1552 if (fontno
& FONT_UNDERLINE
)
1556 CreateFont(font_height
* (1 + !!(fontno
& FONT_HIGH
)), x
, 0, 0, w
,
1557 FALSE
, u
, FALSE
, c
, OUT_DEFAULT_PRECIS
,
1558 CLIP_DEFAULT_PRECIS
, FONT_QUALITY(cfg
.font_quality
),
1559 FIXED_PITCH
| FF_DONTCARE
, s
);
1561 fontflag
[fontno
] = 1;
1564 static void deinit_fonts(void)
1567 for (i
= 0; i
< FONT_MAXNO
; i
++) {
1569 DeleteObject(fonts
[i
]);
1575 void request_resize(void *frontend
, int w
, int h
)
1579 /* If the window is maximized supress resizing attempts */
1580 if (IsZoomed(hwnd
)) {
1581 if (cfg
.resize_action
== RESIZE_TERM
)
1585 if (cfg
.resize_action
== RESIZE_DISABLED
) return;
1586 if (h
== term
->rows
&& w
== term
->cols
) return;
1588 /* Sanity checks ... */
1590 static int first_time
= 1;
1593 switch (first_time
) {
1595 /* Get the size of the screen */
1596 if (get_fullscreen_rect(&ss
))
1597 /* first_time = 0 */ ;
1603 /* Make sure the values are sane */
1604 width
= (ss
.right
- ss
.left
- extra_width
) / 4;
1605 height
= (ss
.bottom
- ss
.top
- extra_height
) / 6;
1607 if (w
> width
|| h
> height
)
1616 term_size(term
, h
, w
, cfg
.savelines
);
1618 if (cfg
.resize_action
!= RESIZE_FONT
&& !IsZoomed(hwnd
)) {
1619 width
= extra_width
+ font_width
* w
;
1620 height
= extra_height
+ font_height
* h
;
1622 SetWindowPos(hwnd
, NULL
, 0, 0, width
, height
,
1623 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1624 SWP_NOMOVE
| SWP_NOZORDER
);
1628 InvalidateRect(hwnd
, NULL
, TRUE
);
1631 static void reset_window(int reinit
) {
1633 * This function decides how to resize or redraw when the
1634 * user changes something.
1636 * This function doesn't like to change the terminal size but if the
1637 * font size is locked that may be it's only soluion.
1639 int win_width
, win_height
;
1642 #ifdef RDB_DEBUG_PATCH
1643 debug((27, "reset_window()"));
1646 /* Current window sizes ... */
1647 GetWindowRect(hwnd
, &wr
);
1648 GetClientRect(hwnd
, &cr
);
1650 win_width
= cr
.right
- cr
.left
;
1651 win_height
= cr
.bottom
- cr
.top
;
1653 if (cfg
.resize_action
== RESIZE_DISABLED
) reinit
= 2;
1655 /* Are we being forced to reload the fonts ? */
1657 #ifdef RDB_DEBUG_PATCH
1658 debug((27, "reset_window() -- Forced deinit"));
1664 /* Oh, looks like we're minimised */
1665 if (win_width
== 0 || win_height
== 0)
1668 /* Is the window out of position ? */
1670 (offset_width
!= (win_width
-font_width
*term
->cols
)/2 ||
1671 offset_height
!= (win_height
-font_height
*term
->rows
)/2) ){
1672 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1673 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1674 InvalidateRect(hwnd
, NULL
, TRUE
);
1675 #ifdef RDB_DEBUG_PATCH
1676 debug((27, "reset_window() -> Reposition terminal"));
1680 if (IsZoomed(hwnd
)) {
1681 /* We're fullscreen, this means we must not change the size of
1682 * the window so it's the font size or the terminal itself.
1685 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
;
1686 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
;
1688 if (cfg
.resize_action
!= RESIZE_TERM
) {
1689 if ( font_width
!= win_width
/term
->cols
||
1690 font_height
!= win_height
/term
->rows
) {
1692 init_fonts(win_width
/term
->cols
, win_height
/term
->rows
);
1693 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1694 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1695 InvalidateRect(hwnd
, NULL
, TRUE
);
1696 #ifdef RDB_DEBUG_PATCH
1697 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1698 font_width
, font_height
));
1702 if ( font_width
* term
->cols
!= win_width
||
1703 font_height
* term
->rows
!= win_height
) {
1704 /* Our only choice at this point is to change the
1705 * size of the terminal; Oh well.
1707 term_size(term
, win_height
/font_height
, win_width
/font_width
,
1709 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1710 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1711 InvalidateRect(hwnd
, NULL
, TRUE
);
1712 #ifdef RDB_DEBUG_PATCH
1713 debug((27, "reset_window() -> Zoomed term_size"));
1720 /* Hmm, a force re-init means we should ignore the current window
1721 * so we resize to the default font size.
1724 #ifdef RDB_DEBUG_PATCH
1725 debug((27, "reset_window() -> Forced re-init"));
1728 offset_width
= offset_height
= cfg
.window_border
;
1729 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1730 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1732 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1733 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1735 /* If this is too large windows will resize it to the maximum
1736 * allowed window size, we will then be back in here and resize
1737 * the font or terminal to fit.
1739 SetWindowPos(hwnd
, NULL
, 0, 0,
1740 font_width
*term
->cols
+ extra_width
,
1741 font_height
*term
->rows
+ extra_height
,
1742 SWP_NOMOVE
| SWP_NOZORDER
);
1745 InvalidateRect(hwnd
, NULL
, TRUE
);
1749 /* Okay the user doesn't want us to change the font so we try the
1750 * window. But that may be too big for the screen which forces us
1751 * to change the terminal.
1753 if ((cfg
.resize_action
== RESIZE_TERM
&& reinit
<=0) ||
1754 (cfg
.resize_action
== RESIZE_EITHER
&& reinit
<0) ||
1756 offset_width
= offset_height
= cfg
.window_border
;
1757 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1758 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1760 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1761 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1766 get_fullscreen_rect(&ss
);
1768 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1769 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1772 if ( term
->rows
> height
|| term
->cols
> width
) {
1773 if (cfg
.resize_action
== RESIZE_EITHER
) {
1774 /* Make the font the biggest we can */
1775 if (term
->cols
> width
)
1776 font_width
= (ss
.right
- ss
.left
- extra_width
)
1778 if (term
->rows
> height
)
1779 font_height
= (ss
.bottom
- ss
.top
- extra_height
)
1783 init_fonts(font_width
, font_height
);
1785 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1786 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1788 if ( height
> term
->rows
) height
= term
->rows
;
1789 if ( width
> term
->cols
) width
= term
->cols
;
1790 term_size(term
, height
, width
, cfg
.savelines
);
1791 #ifdef RDB_DEBUG_PATCH
1792 debug((27, "reset_window() -> term resize to (%d,%d)",
1798 SetWindowPos(hwnd
, NULL
, 0, 0,
1799 font_width
*term
->cols
+ extra_width
,
1800 font_height
*term
->rows
+ extra_height
,
1801 SWP_NOMOVE
| SWP_NOZORDER
);
1803 InvalidateRect(hwnd
, NULL
, TRUE
);
1804 #ifdef RDB_DEBUG_PATCH
1805 debug((27, "reset_window() -> window resize to (%d,%d)",
1806 font_width
*term
->cols
+ extra_width
,
1807 font_height
*term
->rows
+ extra_height
));
1813 /* We're allowed to or must change the font but do we want to ? */
1815 if (font_width
!= (win_width
-cfg
.window_border
*2)/term
->cols
||
1816 font_height
!= (win_height
-cfg
.window_border
*2)/term
->rows
) {
1819 init_fonts((win_width
-cfg
.window_border
*2)/term
->cols
,
1820 (win_height
-cfg
.window_border
*2)/term
->rows
);
1821 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1822 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1824 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+offset_width
*2;
1825 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1827 InvalidateRect(hwnd
, NULL
, TRUE
);
1828 #ifdef RDB_DEBUG_PATCH
1829 debug((25, "reset_window() -> font resize to (%d,%d)",
1830 font_width
, font_height
));
1835 static void set_input_locale(HKL kl
)
1839 GetLocaleInfo(LOWORD(kl
), LOCALE_IDEFAULTANSICODEPAGE
,
1840 lbuf
, sizeof(lbuf
));
1842 kbd_codepage
= atoi(lbuf
);
1845 static void click(Mouse_Button b
, int x
, int y
, int shift
, int ctrl
, int alt
)
1847 int thistime
= GetMessageTime();
1849 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
)) {
1850 lastbtn
= MBT_NOTHING
;
1851 term_mouse(term
, b
, translate_button(b
), MA_CLICK
,
1852 x
, y
, shift
, ctrl
, alt
);
1856 if (lastbtn
== b
&& thistime
- lasttime
< dbltime
) {
1857 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
1858 lastact
== MA_2CLK ? MA_3CLK
:
1859 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
1864 if (lastact
!= MA_NOTHING
)
1865 term_mouse(term
, b
, translate_button(b
), lastact
,
1866 x
, y
, shift
, ctrl
, alt
);
1867 lasttime
= thistime
;
1871 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1872 * into a cooked one (SELECT, EXTEND, PASTE).
1874 static Mouse_Button
translate_button(Mouse_Button button
)
1876 if (button
== MBT_LEFT
)
1878 if (button
== MBT_MIDDLE
)
1879 return cfg
.mouse_is_xterm
== 1 ? MBT_PASTE
: MBT_EXTEND
;
1880 if (button
== MBT_RIGHT
)
1881 return cfg
.mouse_is_xterm
== 1 ? MBT_EXTEND
: MBT_PASTE
;
1882 return 0; /* shouldn't happen */
1885 static void show_mouseptr(int show
)
1887 /* NB that the counter in ShowCursor() is also frobbed by
1888 * update_mouse_pointer() */
1889 static int cursor_visible
= 1;
1890 if (!cfg
.hide_mouseptr
) /* override if this feature disabled */
1892 if (cursor_visible
&& !show
)
1894 else if (!cursor_visible
&& show
)
1896 cursor_visible
= show
;
1899 static int is_alt_pressed(void)
1902 int r
= GetKeyboardState(keystate
);
1905 if (keystate
[VK_MENU
] & 0x80)
1907 if (keystate
[VK_RMENU
] & 0x80)
1912 static int is_shift_pressed(void)
1915 int r
= GetKeyboardState(keystate
);
1918 if (keystate
[VK_SHIFT
] & 0x80)
1923 static int resizing
;
1925 void notify_remote_exit(void *fe
)
1929 if (!session_closed
&&
1930 (exitcode
= back
->exitcode(backhandle
)) >= 0) {
1931 /* Abnormal exits will already have set session_closed and taken
1932 * appropriate action. */
1933 if (cfg
.close_on_exit
== FORCE_ON
||
1934 (cfg
.close_on_exit
== AUTO
&& exitcode
!= INT_MAX
)) {
1937 must_close_session
= TRUE
;
1938 session_closed
= TRUE
;
1939 /* exitcode == INT_MAX indicates that the connection was closed
1940 * by a fatal error, so an error box will be coming our way and
1941 * we should not generate this informational one. */
1942 if (exitcode
!= INT_MAX
)
1943 MessageBox(hwnd
, "Connection closed by remote host",
1944 appname
, MB_OK
| MB_ICONINFORMATION
);
1949 void timer_change_notify(long next
)
1951 long ticks
= next
- GETTICKCOUNT();
1952 if (ticks
<= 0) ticks
= 1; /* just in case */
1953 KillTimer(hwnd
, TIMING_TIMER_ID
);
1954 SetTimer(hwnd
, TIMING_TIMER_ID
, ticks
, NULL
);
1955 timing_next_time
= next
;
1958 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1959 WPARAM wParam
, LPARAM lParam
)
1962 static int ignore_clip
= FALSE
;
1963 static int need_backend_resize
= FALSE
;
1964 static int fullscr_on_max
= FALSE
;
1965 static UINT last_mousemove
= 0;
1969 if ((UINT_PTR
)wParam
== TIMING_TIMER_ID
) {
1972 KillTimer(hwnd
, TIMING_TIMER_ID
);
1973 if (run_timers(timing_next_time
, &next
)) {
1974 timer_change_notify(next
);
1985 str
= dupprintf("%s Exit Confirmation", appname
);
1986 if (!cfg
.warn_on_close
|| session_closed
||
1988 "Are you sure you want to close this session?",
1989 str
, MB_ICONWARNING
| MB_OKCANCEL
| MB_DEFBUTTON1
)
1991 DestroyWindow(hwnd
);
1999 case WM_INITMENUPOPUP
:
2000 if ((HMENU
)wParam
== savedsess_menu
) {
2001 /* About to pop up Saved Sessions sub-menu.
2002 * Refresh the session list. */
2003 get_sesslist(&sesslist
, FALSE
); /* free */
2004 get_sesslist(&sesslist
, TRUE
);
2005 update_savedsess_menu();
2011 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
2022 BOOL inherit_handles
;
2024 PROCESS_INFORMATION pi
;
2025 HANDLE filemap
= NULL
;
2027 if (wParam
== IDM_DUPSESS
) {
2029 * Allocate a file-mapping memory chunk for the
2032 SECURITY_ATTRIBUTES sa
;
2035 sa
.nLength
= sizeof(sa
);
2036 sa
.lpSecurityDescriptor
= NULL
;
2037 sa
.bInheritHandle
= TRUE
;
2038 filemap
= CreateFileMapping(INVALID_HANDLE_VALUE
,
2041 0, sizeof(Config
), NULL
);
2042 if (filemap
&& filemap
!= INVALID_HANDLE_VALUE
) {
2043 p
= (Config
*) MapViewOfFile(filemap
,
2045 0, 0, sizeof(Config
));
2047 *p
= cfg
; /* structure copy */
2051 inherit_handles
= TRUE
;
2052 sprintf(c
, "putty &%p", filemap
);
2054 } else if (wParam
== IDM_SAVEDSESS
) {
2055 unsigned int sessno
= ((lParam
- IDM_SAVED_MIN
)
2056 / MENU_SAVED_STEP
) + 1;
2057 if (sessno
< (unsigned)sesslist
.nsessions
) {
2058 char *session
= sesslist
.sessions
[sessno
];
2059 /* XXX spaces? quotes? "-load"? */
2060 cl
= dupprintf("putty @%s", session
);
2061 inherit_handles
= FALSE
;
2065 } else /* IDM_NEWSESS */ {
2067 inherit_handles
= FALSE
;
2070 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
2072 si
.lpReserved
= NULL
;
2073 si
.lpDesktop
= NULL
;
2077 si
.lpReserved2
= NULL
;
2078 CreateProcess(b
, cl
, NULL
, NULL
, inherit_handles
,
2079 NORMAL_PRIORITY_CLASS
, NULL
, NULL
, &si
, &pi
);
2082 CloseHandle(filemap
);
2089 logevent(NULL
, "----- Session restarted -----");
2090 term_pwron(term
, FALSE
);
2099 int reconfig_result
;
2104 reconfiguring
= TRUE
;
2106 GetWindowText(hwnd
, cfg
.wintitle
, sizeof(cfg
.wintitle
));
2110 do_reconfig(hwnd
, back ? back
->cfg_info(backhandle
) : 0);
2111 reconfiguring
= FALSE
;
2112 if (!reconfig_result
)
2116 /* Disable full-screen if resizing forbidden */
2117 HMENU m
= GetSystemMenu (hwnd
, FALSE
);
2118 EnableMenuItem(m
, IDM_FULLSCREEN
, MF_BYCOMMAND
|
2119 (cfg
.resize_action
== RESIZE_DISABLED
)
2120 ? MF_GRAYED
: MF_ENABLED
);
2121 /* Gracefully unzoom if necessary */
2122 if (IsZoomed(hwnd
) &&
2123 (cfg
.resize_action
== RESIZE_DISABLED
)) {
2124 ShowWindow(hwnd
, SW_RESTORE
);
2128 /* Pass new config data to the logging module */
2129 log_reconfig(logctx
, &cfg
);
2133 * Flush the line discipline's edit buffer in the
2134 * case where local editing has just been disabled.
2137 ldisc_send(ldisc
, NULL
, 0, 0);
2145 /* Pass new config data to the terminal */
2146 term_reconfig(term
, &cfg
);
2148 /* Pass new config data to the back end */
2150 back
->reconfig(backhandle
, &cfg
);
2152 /* Screen size changed ? */
2153 if (cfg
.height
!= prev_cfg
.height
||
2154 cfg
.width
!= prev_cfg
.width
||
2155 cfg
.savelines
!= prev_cfg
.savelines
||
2156 cfg
.resize_action
== RESIZE_FONT
||
2157 (cfg
.resize_action
== RESIZE_EITHER
&& IsZoomed(hwnd
)) ||
2158 cfg
.resize_action
== RESIZE_DISABLED
)
2159 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2161 /* Enable or disable the scroll bar, etc */
2163 LONG nflg
, flag
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
2164 LONG nexflag
, exflag
=
2165 GetWindowLongPtr(hwnd
, GWL_EXSTYLE
);
2168 if (cfg
.alwaysontop
!= prev_cfg
.alwaysontop
) {
2169 if (cfg
.alwaysontop
) {
2170 nexflag
|= WS_EX_TOPMOST
;
2171 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
2172 SWP_NOMOVE
| SWP_NOSIZE
);
2174 nexflag
&= ~(WS_EX_TOPMOST
);
2175 SetWindowPos(hwnd
, HWND_NOTOPMOST
, 0, 0, 0, 0,
2176 SWP_NOMOVE
| SWP_NOSIZE
);
2179 if (cfg
.sunken_edge
)
2180 nexflag
|= WS_EX_CLIENTEDGE
;
2182 nexflag
&= ~(WS_EX_CLIENTEDGE
);
2185 if (is_full_screen() ?
2186 cfg
.scrollbar_in_fullscreen
: cfg
.scrollbar
)
2189 nflg
&= ~WS_VSCROLL
;
2191 if (cfg
.resize_action
== RESIZE_DISABLED
||
2193 nflg
&= ~WS_THICKFRAME
;
2195 nflg
|= WS_THICKFRAME
;
2197 if (cfg
.resize_action
== RESIZE_DISABLED
)
2198 nflg
&= ~WS_MAXIMIZEBOX
;
2200 nflg
|= WS_MAXIMIZEBOX
;
2202 if (nflg
!= flag
|| nexflag
!= exflag
) {
2204 SetWindowLongPtr(hwnd
, GWL_STYLE
, nflg
);
2205 if (nexflag
!= exflag
)
2206 SetWindowLongPtr(hwnd
, GWL_EXSTYLE
, nexflag
);
2208 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
2209 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
2210 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
2218 if (cfg
.resize_action
== RESIZE_DISABLED
&& IsZoomed(hwnd
)) {
2223 set_title(NULL
, cfg
.wintitle
);
2224 if (IsIconic(hwnd
)) {
2226 cfg
.win_name_always ? window_name
:
2230 if (strcmp(cfg
.font
.name
, prev_cfg
.font
.name
) != 0 ||
2231 strcmp(cfg
.line_codepage
, prev_cfg
.line_codepage
) != 0 ||
2232 cfg
.font
.isbold
!= prev_cfg
.font
.isbold
||
2233 cfg
.font
.height
!= prev_cfg
.font
.height
||
2234 cfg
.font
.charset
!= prev_cfg
.font
.charset
||
2235 cfg
.font_quality
!= prev_cfg
.font_quality
||
2236 cfg
.vtmode
!= prev_cfg
.vtmode
||
2237 cfg
.bold_colour
!= prev_cfg
.bold_colour
||
2238 cfg
.resize_action
== RESIZE_DISABLED
||
2239 cfg
.resize_action
== RESIZE_EITHER
||
2240 (cfg
.resize_action
!= prev_cfg
.resize_action
))
2243 InvalidateRect(hwnd
, NULL
, TRUE
);
2244 reset_window(init_lvl
);
2245 net_pending_errors();
2252 term_do_paste(term
);
2258 term_pwron(term
, TRUE
);
2260 ldisc_send(ldisc
, NULL
, 0, 0);
2266 launch_help(hwnd
, NULL
);
2270 * We get this if the System menu has been activated
2277 * We get this if the System menu has been activated
2278 * using the keyboard. This might happen from within
2279 * TranslateKey, in which case it really wants to be
2280 * followed by a `space' character to actually _bring
2281 * the menu up_ rather than just sitting there in
2282 * `ready to appear' state.
2284 show_mouseptr(1); /* make sure pointer is visible */
2286 PostMessage(hwnd
, WM_CHAR
, ' ', 0);
2288 case IDM_FULLSCREEN
:
2292 if (wParam
>= IDM_SAVED_MIN
&& wParam
< IDM_SAVED_MAX
) {
2293 SendMessage(hwnd
, WM_SYSCOMMAND
, IDM_SAVEDSESS
, wParam
);
2295 if (wParam
>= IDM_SPECIAL_MIN
&& wParam
<= IDM_SPECIAL_MAX
) {
2296 int i
= (wParam
- IDM_SPECIAL_MIN
) / 0x10;
2298 * Ensure we haven't been sent a bogus SYSCOMMAND
2299 * which would cause us to reference invalid memory
2300 * and crash. Perhaps I'm just too paranoid here.
2302 if (i
>= n_specials
)
2305 back
->special(backhandle
, specials
[i
].code
);
2306 net_pending_errors();
2311 #define X_POS(l) ((int)(short)LOWORD(l))
2312 #define Y_POS(l) ((int)(short)HIWORD(l))
2314 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2315 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2316 case WM_LBUTTONDOWN
:
2317 case WM_MBUTTONDOWN
:
2318 case WM_RBUTTONDOWN
:
2322 if (message
== WM_RBUTTONDOWN
&&
2323 ((wParam
& MK_CONTROL
) || (cfg
.mouse_is_xterm
== 2))) {
2326 show_mouseptr(1); /* make sure pointer is visible */
2327 GetCursorPos(&cursorpos
);
2328 TrackPopupMenu(popup_menus
[CTXMENU
].menu
,
2329 TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
2330 cursorpos
.x
, cursorpos
.y
,
2338 case WM_LBUTTONDOWN
:
2342 case WM_MBUTTONDOWN
:
2343 button
= MBT_MIDDLE
;
2346 case WM_RBUTTONDOWN
:
2355 button
= MBT_MIDDLE
;
2363 button
= press
= 0; /* shouldn't happen */
2367 * Special case: in full-screen mode, if the left
2368 * button is clicked in the very top left corner of the
2369 * window, we put up the System menu instead of doing
2373 char mouse_on_hotspot
= 0;
2382 mon
= MonitorFromPoint(pt
, MONITOR_DEFAULTTONULL
);
2385 mi
.cbSize
= sizeof(MONITORINFO
);
2386 GetMonitorInfo(mon
, &mi
);
2388 if (mi
.rcMonitor
.left
== pt
.x
&&
2389 mi
.rcMonitor
.top
== pt
.y
) {
2390 mouse_on_hotspot
= 1;
2395 if (pt
.x
== 0 && pt
.y
== 0) {
2396 mouse_on_hotspot
= 1;
2399 if (is_full_screen() && press
&&
2400 button
== MBT_LEFT
&& mouse_on_hotspot
) {
2401 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MOUSEMENU
,
2402 MAKELPARAM(pt
.x
, pt
.y
));
2409 TO_CHR_X(X_POS(lParam
)), TO_CHR_Y(Y_POS(lParam
)),
2410 wParam
& MK_SHIFT
, wParam
& MK_CONTROL
,
2414 term_mouse(term
, button
, translate_button(button
), MA_RELEASE
,
2415 TO_CHR_X(X_POS(lParam
)),
2416 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2417 wParam
& MK_CONTROL
, is_alt_pressed());
2425 * Windows seems to like to occasionally send MOUSEMOVE
2426 * events even if the mouse hasn't moved. Don't unhide
2427 * the mouse pointer in this case.
2429 static WPARAM wp
= 0;
2430 static LPARAM lp
= 0;
2431 if (wParam
!= wp
|| lParam
!= lp
||
2432 last_mousemove
!= WM_MOUSEMOVE
) {
2434 wp
= wParam
; lp
= lParam
;
2435 last_mousemove
= WM_MOUSEMOVE
;
2439 * Add the mouse position and message time to the random
2442 noise_ultralight(lParam
);
2444 if (wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
) &&
2445 GetCapture() == hwnd
) {
2447 if (wParam
& MK_LBUTTON
)
2449 else if (wParam
& MK_MBUTTON
)
2453 term_mouse(term
, b
, translate_button(b
), MA_DRAG
,
2454 TO_CHR_X(X_POS(lParam
)),
2455 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2456 wParam
& MK_CONTROL
, is_alt_pressed());
2459 case WM_NCMOUSEMOVE
:
2461 static WPARAM wp
= 0;
2462 static LPARAM lp
= 0;
2463 if (wParam
!= wp
|| lParam
!= lp
||
2464 last_mousemove
!= WM_NCMOUSEMOVE
) {
2466 wp
= wParam
; lp
= lParam
;
2467 last_mousemove
= WM_NCMOUSEMOVE
;
2470 noise_ultralight(lParam
);
2472 case WM_IGNORE_CLIP
:
2473 ignore_clip
= wParam
; /* don't panic on DESTROYCLIPBOARD */
2475 case WM_DESTROYCLIPBOARD
:
2477 term_deselect(term
);
2478 ignore_clip
= FALSE
;
2485 hdc
= BeginPaint(hwnd
, &p
);
2487 SelectPalette(hdc
, pal
, TRUE
);
2488 RealizePalette(hdc
);
2492 * We have to be careful about term_paint(). It will
2493 * set a bunch of character cells to INVALID and then
2494 * call do_paint(), which will redraw those cells and
2495 * _then mark them as done_. This may not be accurate:
2496 * when painting in WM_PAINT context we are restricted
2497 * to the rectangle which has just been exposed - so if
2498 * that only covers _part_ of a character cell and the
2499 * rest of it was already visible, that remainder will
2500 * not be redrawn at all. Accordingly, we must not
2501 * paint any character cell in a WM_PAINT context which
2502 * already has a pending update due to terminal output.
2503 * The simplest solution to this - and many, many
2504 * thanks to Hung-Te Lin for working all this out - is
2505 * not to do any actual painting at _all_ if there's a
2506 * pending terminal update: just mark the relevant
2507 * character cells as INVALID and wait for the
2508 * scheduled full update to sort it out.
2510 * I have a suspicion this isn't the _right_ solution.
2511 * An alternative approach would be to have terminal.c
2512 * separately track what _should_ be on the terminal
2513 * screen and what _is_ on the terminal screen, and
2514 * have two completely different types of redraw (one
2515 * for full updates, which syncs the former with the
2516 * terminal itself, and one for WM_PAINT which syncs
2517 * the latter with the former); yet another possibility
2518 * would be to have the Windows front end do what the
2519 * GTK one already does, and maintain a bitmap of the
2520 * current terminal appearance so that WM_PAINT becomes
2521 * completely trivial. However, this should do for now.
2523 term_paint(term
, hdc
,
2524 (p
.rcPaint
.left
-offset_width
)/font_width
,
2525 (p
.rcPaint
.top
-offset_height
)/font_height
,
2526 (p
.rcPaint
.right
-offset_width
-1)/font_width
,
2527 (p
.rcPaint
.bottom
-offset_height
-1)/font_height
,
2528 !term
->window_update_pending
);
2531 p
.rcPaint
.left
< offset_width
||
2532 p
.rcPaint
.top
< offset_height
||
2533 p
.rcPaint
.right
>= offset_width
+ font_width
*term
->cols
||
2534 p
.rcPaint
.bottom
>= offset_height
+ font_height
*term
->rows
)
2536 HBRUSH fillcolour
, oldbrush
;
2538 fillcolour
= CreateSolidBrush (
2539 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2540 oldbrush
= SelectObject(hdc
, fillcolour
);
2541 edge
= CreatePen(PS_SOLID
, 0,
2542 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2543 oldpen
= SelectObject(hdc
, edge
);
2546 * Jordan Russell reports that this apparently
2547 * ineffectual IntersectClipRect() call masks a
2548 * Windows NT/2K bug causing strange display
2549 * problems when the PuTTY window is taller than
2550 * the primary monitor. It seems harmless enough...
2552 IntersectClipRect(hdc
,
2553 p
.rcPaint
.left
, p
.rcPaint
.top
,
2554 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2556 ExcludeClipRect(hdc
,
2557 offset_width
, offset_height
,
2558 offset_width
+font_width
*term
->cols
,
2559 offset_height
+font_height
*term
->rows
);
2561 Rectangle(hdc
, p
.rcPaint
.left
, p
.rcPaint
.top
,
2562 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2564 /* SelectClipRgn(hdc, NULL); */
2566 SelectObject(hdc
, oldbrush
);
2567 DeleteObject(fillcolour
);
2568 SelectObject(hdc
, oldpen
);
2571 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
2572 SelectObject(hdc
, GetStockObject(WHITE_PEN
));
2578 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2579 * but the only one that's likely to try to overload us is FD_READ.
2580 * This means buffering just one is fine.
2582 if (pending_netevent
)
2583 enact_pending_netevent();
2585 pending_netevent
= TRUE
;
2586 pend_netevent_wParam
= wParam
;
2587 pend_netevent_lParam
= lParam
;
2588 if (WSAGETSELECTEVENT(lParam
) != FD_READ
)
2589 enact_pending_netevent();
2591 net_pending_errors();
2594 term_set_focus(term
, TRUE
);
2595 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
2597 flash_window(0); /* stop */
2603 term_set_focus(term
, FALSE
);
2605 caret_x
= caret_y
= -1; /* ensure caret is replaced next time */
2608 case WM_ENTERSIZEMOVE
:
2609 #ifdef RDB_DEBUG_PATCH
2610 debug((27, "WM_ENTERSIZEMOVE"));
2614 need_backend_resize
= FALSE
;
2616 case WM_EXITSIZEMOVE
:
2619 #ifdef RDB_DEBUG_PATCH
2620 debug((27, "WM_EXITSIZEMOVE"));
2622 if (need_backend_resize
) {
2623 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2624 InvalidateRect(hwnd
, NULL
, TRUE
);
2629 * This does two jobs:
2630 * 1) Keep the sizetip uptodate
2631 * 2) Make sure the window size is _stepped_ in units of the font size.
2633 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2634 int width
, height
, w
, h
, ew
, eh
;
2635 LPRECT r
= (LPRECT
) lParam
;
2637 if ( !need_backend_resize
&& cfg
.resize_action
== RESIZE_EITHER
&&
2638 (cfg
.height
!= term
->rows
|| cfg
.width
!= term
->cols
)) {
2640 * Great! It seems that both the terminal size and the
2641 * font size have been changed and the user is now dragging.
2643 * It will now be difficult to get back to the configured
2646 * This would be easier but it seems to be too confusing.
2648 term_size(term, cfg.height, cfg.width, cfg.savelines);
2651 cfg
.height
=term
->rows
; cfg
.width
=term
->cols
;
2653 InvalidateRect(hwnd
, NULL
, TRUE
);
2654 need_backend_resize
= TRUE
;
2657 width
= r
->right
- r
->left
- extra_width
;
2658 height
= r
->bottom
- r
->top
- extra_height
;
2659 w
= (width
+ font_width
/ 2) / font_width
;
2662 h
= (height
+ font_height
/ 2) / font_height
;
2665 UpdateSizeTip(hwnd
, w
, h
);
2666 ew
= width
- w
* font_width
;
2667 eh
= height
- h
* font_height
;
2669 if (wParam
== WMSZ_LEFT
||
2670 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2676 if (wParam
== WMSZ_TOP
||
2677 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2687 int width
, height
, w
, h
, rv
= 0;
2688 int ex_width
= extra_width
+ (cfg
.window_border
- offset_width
) * 2;
2689 int ex_height
= extra_height
+ (cfg
.window_border
- offset_height
) * 2;
2690 LPRECT r
= (LPRECT
) lParam
;
2692 width
= r
->right
- r
->left
- ex_width
;
2693 height
= r
->bottom
- r
->top
- ex_height
;
2695 w
= (width
+ term
->cols
/2)/term
->cols
;
2696 h
= (height
+ term
->rows
/2)/term
->rows
;
2697 if ( r
->right
!= r
->left
+ w
*term
->cols
+ ex_width
)
2700 if (wParam
== WMSZ_LEFT
||
2701 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2702 r
->left
= r
->right
- w
*term
->cols
- ex_width
;
2704 r
->right
= r
->left
+ w
*term
->cols
+ ex_width
;
2706 if (r
->bottom
!= r
->top
+ h
*term
->rows
+ ex_height
)
2709 if (wParam
== WMSZ_TOP
||
2710 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2711 r
->top
= r
->bottom
- h
*term
->rows
- ex_height
;
2713 r
->bottom
= r
->top
+ h
*term
->rows
+ ex_height
;
2717 /* break; (never reached) */
2718 case WM_FULLSCR_ON_MAX
:
2719 fullscr_on_max
= TRUE
;
2722 sys_cursor_update();
2725 #ifdef RDB_DEBUG_PATCH
2726 debug((27, "WM_SIZE %s (%d,%d)",
2727 (wParam
== SIZE_MINIMIZED
) ?
"SIZE_MINIMIZED":
2728 (wParam
== SIZE_MAXIMIZED
) ?
"SIZE_MAXIMIZED":
2729 (wParam
== SIZE_RESTORED
&& resizing
) ?
"to":
2730 (wParam
== SIZE_RESTORED
) ?
"SIZE_RESTORED":
2732 LOWORD(lParam
), HIWORD(lParam
)));
2734 if (wParam
== SIZE_MINIMIZED
)
2736 cfg
.win_name_always ? window_name
: icon_name
);
2737 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
)
2738 SetWindowText(hwnd
, window_name
);
2739 if (wParam
== SIZE_RESTORED
)
2740 clear_full_screen();
2741 if (wParam
== SIZE_MAXIMIZED
&& fullscr_on_max
) {
2742 fullscr_on_max
= FALSE
;
2746 if (cfg
.resize_action
== RESIZE_DISABLED
) {
2747 /* A resize, well it better be a minimize. */
2751 int width
, height
, w
, h
;
2753 width
= LOWORD(lParam
);
2754 height
= HIWORD(lParam
);
2757 if (wParam
== SIZE_MAXIMIZED
&& !was_zoomed
) {
2759 prev_rows
= term
->rows
;
2760 prev_cols
= term
->cols
;
2761 if (cfg
.resize_action
== RESIZE_TERM
) {
2762 w
= width
/ font_width
;
2764 h
= height
/ font_height
;
2767 term_size(term
, h
, w
, cfg
.savelines
);
2770 } else if (wParam
== SIZE_RESTORED
&& was_zoomed
) {
2772 if (cfg
.resize_action
== RESIZE_TERM
)
2773 term_size(term
, prev_rows
, prev_cols
, cfg
.savelines
);
2774 if (cfg
.resize_action
!= RESIZE_FONT
)
2779 /* This is an unexpected resize, these will normally happen
2780 * if the window is too large. Probably either the user
2781 * selected a huge font or the screen size has changed.
2783 * This is also called with minimize.
2785 else reset_window(-1);
2789 * Don't call back->size in mid-resize. (To prevent
2790 * massive numbers of resize events getting sent
2791 * down the connection during an NT opaque drag.)
2794 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2795 need_backend_resize
= TRUE
;
2796 w
= (width
-cfg
.window_border
*2) / font_width
;
2798 h
= (height
-cfg
.window_border
*2) / font_height
;
2807 sys_cursor_update();
2810 switch (LOWORD(wParam
)) {
2812 term_scroll(term
, -1, 0);
2815 term_scroll(term
, +1, 0);
2818 term_scroll(term
, 0, +1);
2821 term_scroll(term
, 0, -1);
2824 term_scroll(term
, 0, +term
->rows
/ 2);
2827 term_scroll(term
, 0, -term
->rows
/ 2);
2829 case SB_THUMBPOSITION
:
2831 term_scroll(term
, 1, HIWORD(wParam
));
2835 case WM_PALETTECHANGED
:
2836 if ((HWND
) wParam
!= hwnd
&& pal
!= NULL
) {
2837 HDC hdc
= get_ctx(NULL
);
2839 if (RealizePalette(hdc
) > 0)
2845 case WM_QUERYNEWPALETTE
:
2847 HDC hdc
= get_ctx(NULL
);
2849 if (RealizePalette(hdc
) > 0)
2861 * Add the scan code and keypress timing to the random
2864 noise_ultralight(lParam
);
2867 * We don't do TranslateMessage since it disassociates the
2868 * resulting CHAR message from the KEYDOWN that sparked it,
2869 * which we occasionally don't want. Instead, we process
2870 * KEYDOWN, and call the Win32 translator functions so that
2871 * we get the translations under _our_ control.
2874 unsigned char buf
[20];
2877 if (wParam
== VK_PROCESSKEY
) { /* IME PROCESS key */
2878 if (message
== WM_KEYDOWN
) {
2881 m
.message
= WM_KEYDOWN
;
2883 m
.lParam
= lParam
& 0xdfff;
2884 TranslateMessage(&m
);
2885 } else break; /* pass to Windows for default processing */
2887 len
= TranslateKey(message
, wParam
, lParam
, buf
);
2889 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2893 * Interrupt an ongoing paste. I'm not sure
2894 * this is sensible, but for the moment it's
2895 * preferable to having to faff about buffering
2901 * We need not bother about stdin backlogs
2902 * here, because in GUI PuTTY we can't do
2903 * anything about it anyway; there's no means
2904 * of asking Windows to hold off on KEYDOWN
2905 * messages. We _have_ to buffer everything
2908 term_seen_key_event(term
);
2910 ldisc_send(ldisc
, buf
, len
, 1);
2915 net_pending_errors();
2917 case WM_INPUTLANGCHANGE
:
2918 /* wParam == Font number */
2919 /* lParam == Locale */
2920 set_input_locale((HKL
)lParam
);
2921 sys_cursor_update();
2923 case WM_IME_STARTCOMPOSITION
:
2925 HIMC hImc
= ImmGetContext(hwnd
);
2926 ImmSetCompositionFont(hImc
, &lfont
);
2927 ImmReleaseContext(hwnd
, hImc
);
2930 case WM_IME_COMPOSITION
:
2936 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
||
2937 osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) break; /* no Unicode */
2939 if ((lParam
& GCS_RESULTSTR
) == 0) /* Composition unfinished. */
2940 break; /* fall back to DefWindowProc */
2942 hIMC
= ImmGetContext(hwnd
);
2943 n
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
2947 buff
= snewn(n
, char);
2948 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, buff
, n
);
2950 * Jaeyoun Chung reports that Korean character
2951 * input doesn't work correctly if we do a single
2952 * luni_send() covering the whole of buff. So
2953 * instead we luni_send the characters one by one.
2955 term_seen_key_event(term
);
2956 for (i
= 0; i
< n
; i
+= 2) {
2958 luni_send(ldisc
, (unsigned short *)(buff
+i
), 1, 1);
2962 ImmReleaseContext(hwnd
, hIMC
);
2967 if (wParam
& 0xFF00) {
2968 unsigned char buf
[2];
2971 buf
[0] = wParam
>> 8;
2972 term_seen_key_event(term
);
2974 lpage_send(ldisc
, kbd_codepage
, buf
, 2, 1);
2976 char c
= (unsigned char) wParam
;
2977 term_seen_key_event(term
);
2979 lpage_send(ldisc
, kbd_codepage
, &c
, 1, 1);
2985 * Nevertheless, we are prepared to deal with WM_CHAR
2986 * messages, should they crop up. So if someone wants to
2987 * post the things to us as part of a macro manoeuvre,
2988 * we're ready to cope.
2991 char c
= (unsigned char)wParam
;
2992 term_seen_key_event(term
);
2994 lpage_send(ldisc
, CP_ACP
, &c
, 1, 1);
2997 case WM_SYSCOLORCHANGE
:
2998 if (cfg
.system_colour
) {
2999 /* Refresh palette from system colours. */
3000 /* XXX actually this zaps the entire palette. */
3003 /* Force a repaint of the terminal window. */
3004 term_invalidate(term
);
3007 case WM_AGENT_CALLBACK
:
3009 struct agent_callback
*c
= (struct agent_callback
*)lParam
;
3010 c
->callback(c
->callback_ctx
, c
->data
, c
->len
);
3015 if (message
== wm_mousewheel
|| message
== WM_MOUSEWHEEL
) {
3016 int shift_pressed
=0, control_pressed
=0;
3018 if (message
== WM_MOUSEWHEEL
) {
3019 wheel_accumulator
+= (short)HIWORD(wParam
);
3020 shift_pressed
=LOWORD(wParam
) & MK_SHIFT
;
3021 control_pressed
=LOWORD(wParam
) & MK_CONTROL
;
3024 wheel_accumulator
+= (int)wParam
;
3025 if (GetKeyboardState(keys
)!=0) {
3026 shift_pressed
=keys
[VK_SHIFT
]&0x80;
3027 control_pressed
=keys
[VK_CONTROL
]&0x80;
3031 /* process events when the threshold is reached */
3032 while (abs(wheel_accumulator
) >= WHEEL_DELTA
) {
3035 /* reduce amount for next time */
3036 if (wheel_accumulator
> 0) {
3038 wheel_accumulator
-= WHEEL_DELTA
;
3039 } else if (wheel_accumulator
< 0) {
3041 wheel_accumulator
+= WHEEL_DELTA
;
3045 if (send_raw_mouse
&&
3046 !(cfg
.mouse_override
&& shift_pressed
)) {
3047 /* send a mouse-down followed by a mouse up */
3048 term_mouse(term
, b
, translate_button(b
),
3050 TO_CHR_X(X_POS(lParam
)),
3051 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
3052 control_pressed
, is_alt_pressed());
3053 term_mouse(term
, b
, translate_button(b
),
3054 MA_RELEASE
, TO_CHR_X(X_POS(lParam
)),
3055 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
3056 control_pressed
, is_alt_pressed());
3058 /* trigger a scroll */
3059 term_scroll(term
, 0,
3061 -term
->rows
/ 2 : term
->rows
/ 2);
3069 * Any messages we don't process completely above are passed through to
3070 * DefWindowProc() for default processing.
3072 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
3076 * Move the system caret. (We maintain one, even though it's
3077 * invisible, for the benefit of blind people: apparently some
3078 * helper software tracks the system caret, so we should arrange to
3081 void sys_cursor(void *frontend
, int x
, int y
)
3085 if (!term
->has_focus
) return;
3088 * Avoid gratuitously re-updating the cursor position and IMM
3089 * window if there's no actual change required.
3091 cx
= x
* font_width
+ offset_width
;
3092 cy
= y
* font_height
+ offset_height
;
3093 if (cx
== caret_x
&& cy
== caret_y
)
3098 sys_cursor_update();
3101 static void sys_cursor_update(void)
3106 if (!term
->has_focus
) return;
3108 if (caret_x
< 0 || caret_y
< 0)
3111 SetCaretPos(caret_x
, caret_y
);
3113 /* IMM calls on Win98 and beyond only */
3114 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) return; /* 3.11 */
3116 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
&&
3117 osVersion
.dwMinorVersion
== 0) return; /* 95 */
3119 /* we should have the IMM functions */
3120 hIMC
= ImmGetContext(hwnd
);
3121 cf
.dwStyle
= CFS_POINT
;
3122 cf
.ptCurrentPos
.x
= caret_x
;
3123 cf
.ptCurrentPos
.y
= caret_y
;
3124 ImmSetCompositionWindow(hIMC
, &cf
);
3126 ImmReleaseContext(hwnd
, hIMC
);
3130 * Draw a line of text in the window, at given character
3131 * coordinates, in given attributes.
3133 * We are allowed to fiddle with the contents of `text'.
3135 void do_text_internal(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3136 unsigned long attr
, int lattr
)
3139 int nfg
, nbg
, nfont
;
3142 int force_manual_underline
= 0;
3143 int fnt_width
, char_width
;
3144 int text_adjust
= 0;
3145 static int *IpDx
= 0, IpDxLEN
= 0;
3147 lattr
&= LATTR_MODE
;
3149 char_width
= fnt_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3151 if (attr
& ATTR_WIDE
)
3154 if (len
> IpDxLEN
|| IpDx
[0] != char_width
) {
3156 if (len
> IpDxLEN
) {
3158 IpDx
= snewn(len
+ 16, int);
3159 IpDxLEN
= (len
+ 16);
3161 for (i
= 0; i
< IpDxLEN
; i
++)
3162 IpDx
[i
] = char_width
;
3165 /* Only want the left half of double width lines */
3166 if (lattr
!= LATTR_NORM
&& x
*2 >= term
->cols
)
3174 if ((attr
& TATTR_ACTCURS
) && (cfg
.cursor_type
== 0 || term
->big_cursor
)) {
3175 attr
&= ~(ATTR_REVERSE
|ATTR_BLINK
|ATTR_COLOURS
);
3176 if (bold_mode
== BOLD_COLOURS
)
3179 /* cursor fg and bg */
3180 attr
|= (260 << ATTR_FGSHIFT
) | (261 << ATTR_BGSHIFT
);
3184 if (cfg
.vtmode
== VT_POORMAN
&& lattr
!= LATTR_NORM
) {
3185 /* Assume a poorman font is borken in other ways too. */
3195 nfont
|= FONT_WIDE
+ FONT_HIGH
;
3198 if (attr
& ATTR_NARROW
)
3199 nfont
|= FONT_NARROW
;
3201 /* Special hack for the VT100 linedraw glyphs. */
3202 if (text
[0] >= 0x23BA && text
[0] <= 0x23BD) {
3203 switch ((unsigned char) (text
[0])) {
3205 text_adjust
= -2 * font_height
/ 5;
3208 text_adjust
= -1 * font_height
/ 5;
3211 text_adjust
= font_height
/ 5;
3214 text_adjust
= 2 * font_height
/ 5;
3217 if (lattr
== LATTR_TOP
|| lattr
== LATTR_BOT
)
3219 text
[0] = ucsdata
.unitab_xterm
['q'];
3220 if (attr
& ATTR_UNDER
) {
3221 attr
&= ~ATTR_UNDER
;
3222 force_manual_underline
= 1;
3226 /* Anything left as an original character set is unprintable. */
3227 if (DIRECT_CHAR(text
[0])) {
3229 for (i
= 0; i
< len
; i
++)
3234 if ((text
[0] & CSET_MASK
) == CSET_OEMCP
)
3237 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
3238 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
3239 if (bold_mode
== BOLD_FONT
&& (attr
& ATTR_BOLD
))
3241 if (und_mode
== UND_FONT
&& (attr
& ATTR_UNDER
))
3242 nfont
|= FONT_UNDERLINE
;
3243 another_font(nfont
);
3244 if (!fonts
[nfont
]) {
3245 if (nfont
& FONT_UNDERLINE
)
3246 force_manual_underline
= 1;
3247 /* Don't do the same for manual bold, it could be bad news. */
3249 nfont
&= ~(FONT_BOLD
| FONT_UNDERLINE
);
3251 another_font(nfont
);
3253 nfont
= FONT_NORMAL
;
3254 if (attr
& ATTR_REVERSE
) {
3259 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BOLD
)) {
3260 if (nfg
< 16) nfg
|= 8;
3261 else if (nfg
>= 256) nfg
|= 1;
3263 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BLINK
)) {
3264 if (nbg
< 16) nbg
|= 8;
3265 else if (nbg
>= 256) nbg
|= 1;
3269 SelectObject(hdc
, fonts
[nfont
]);
3270 SetTextColor(hdc
, fg
);
3271 SetBkColor(hdc
, bg
);
3272 if (attr
& TATTR_COMBINING
)
3273 SetBkMode(hdc
, TRANSPARENT
);
3275 SetBkMode(hdc
, OPAQUE
);
3278 line_box
.right
= x
+ char_width
* len
;
3279 line_box
.bottom
= y
+ font_height
;
3281 /* Only want the left half of double width lines */
3282 if (line_box
.right
> font_width
*term
->cols
+offset_width
)
3283 line_box
.right
= font_width
*term
->cols
+offset_width
;
3285 /* We're using a private area for direct to font. (512 chars.) */
3286 if (ucsdata
.dbcs_screenfont
&& (text
[0] & CSET_MASK
) == CSET_ACP
) {
3287 /* Ho Hum, dbcs fonts are a PITA! */
3288 /* To display on W9x I have to convert to UCS */
3289 static wchar_t *uni_buf
= 0;
3290 static int uni_len
= 0;
3292 if (len
> uni_len
) {
3295 uni_buf
= snewn(uni_len
, wchar_t);
3298 for(nlen
= mptr
= 0; mptr
<len
; mptr
++) {
3299 uni_buf
[nlen
] = 0xFFFD;
3300 if (IsDBCSLeadByteEx(ucsdata
.font_codepage
, (BYTE
) text
[mptr
])) {
3302 dbcstext
[0] = text
[mptr
] & 0xFF;
3303 dbcstext
[1] = text
[mptr
+1] & 0xFF;
3304 IpDx
[nlen
] += char_width
;
3305 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3306 dbcstext
, 2, uni_buf
+nlen
, 1);
3312 dbcstext
[0] = text
[mptr
] & 0xFF;
3313 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3314 dbcstext
, 1, uni_buf
+nlen
, 1);
3322 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3323 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, uni_buf
, nlen
, IpDx
);
3324 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3325 SetBkMode(hdc
, TRANSPARENT
);
3326 ExtTextOutW(hdc
, x
- 1,
3327 y
- font_height
* (lattr
==
3328 LATTR_BOT
) + text_adjust
,
3329 ETO_CLIPPED
, &line_box
, uni_buf
, nlen
, IpDx
);
3333 } else if (DIRECT_FONT(text
[0])) {
3334 static char *directbuf
= NULL
;
3335 static int directlen
= 0;
3337 if (len
> directlen
) {
3339 directbuf
= sresize(directbuf
, directlen
, char);
3342 for (i
= 0; i
< len
; i
++)
3343 directbuf
[i
] = text
[i
] & 0xFF;
3346 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3347 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, directbuf
, len
, IpDx
);
3348 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3349 SetBkMode(hdc
, TRANSPARENT
);
3351 /* GRR: This draws the character outside it's box and can leave
3352 * 'droppings' even with the clip box! I suppose I could loop it
3353 * one character at a time ... yuk.
3355 * Or ... I could do a test print with "W", and use +1 or -1 for this
3356 * shift depending on if the leftmost column is blank...
3358 ExtTextOut(hdc
, x
- 1,
3359 y
- font_height
* (lattr
==
3360 LATTR_BOT
) + text_adjust
,
3361 ETO_CLIPPED
, &line_box
, directbuf
, len
, IpDx
);
3364 /* And 'normal' unicode characters */
3365 static WCHAR
*wbuf
= NULL
;
3366 static int wlen
= 0;
3372 wbuf
= snewn(wlen
, WCHAR
);
3375 for (i
= 0; i
< len
; i
++)
3378 /* print Glyphs as they are, without Windows' Shaping*/
3379 general_textout(hdc
, x
, y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3380 &line_box
, wbuf
, len
, IpDx
, !(attr
& TATTR_COMBINING
));
3382 /* And the shadow bold hack. */
3383 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3384 SetBkMode(hdc
, TRANSPARENT
);
3385 ExtTextOutW(hdc
, x
- 1,
3386 y
- font_height
* (lattr
==
3387 LATTR_BOT
) + text_adjust
,
3388 ETO_CLIPPED
, &line_box
, wbuf
, len
, IpDx
);
3391 if (lattr
!= LATTR_TOP
&& (force_manual_underline
||
3392 (und_mode
== UND_LINE
3393 && (attr
& ATTR_UNDER
)))) {
3396 if (lattr
== LATTR_BOT
)
3397 dec
= dec
* 2 - font_height
;
3399 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, fg
));
3400 MoveToEx(hdc
, x
, y
+ dec
, NULL
);
3401 LineTo(hdc
, x
+ len
* char_width
, y
+ dec
);
3402 oldpen
= SelectObject(hdc
, oldpen
);
3403 DeleteObject(oldpen
);
3408 * Wrapper that handles combining characters.
3410 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3411 unsigned long attr
, int lattr
)
3413 if (attr
& TATTR_COMBINING
) {
3414 unsigned long a
= 0;
3415 attr
&= ~TATTR_COMBINING
;
3417 do_text_internal(ctx
, x
, y
, text
, 1, attr
| a
, lattr
);
3419 a
= TATTR_COMBINING
;
3422 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
3425 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3426 unsigned long attr
, int lattr
)
3432 int ctype
= cfg
.cursor_type
;
3434 lattr
&= LATTR_MODE
;
3436 if ((attr
& TATTR_ACTCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3437 if (*text
!= UCSWIDE
) {
3438 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
3442 attr
|= TATTR_RIGHTCURS
;
3445 fnt_width
= char_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3446 if (attr
& ATTR_WIDE
)
3453 if ((attr
& TATTR_PASCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3456 pts
[0].x
= pts
[1].x
= pts
[4].x
= x
;
3457 pts
[2].x
= pts
[3].x
= x
+ char_width
- 1;
3458 pts
[0].y
= pts
[3].y
= pts
[4].y
= y
;
3459 pts
[1].y
= pts
[2].y
= y
+ font_height
- 1;
3460 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3461 Polyline(hdc
, pts
, 5);
3462 oldpen
= SelectObject(hdc
, oldpen
);
3463 DeleteObject(oldpen
);
3464 } else if ((attr
& (TATTR_ACTCURS
| TATTR_PASCURS
)) && ctype
!= 0) {
3465 int startx
, starty
, dx
, dy
, length
, i
;
3468 starty
= y
+ descent
;
3471 length
= char_width
;
3474 if (attr
& TATTR_RIGHTCURS
)
3475 xadjust
= char_width
- 1;
3476 startx
= x
+ xadjust
;
3480 length
= font_height
;
3482 if (attr
& TATTR_ACTCURS
) {
3485 SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3486 MoveToEx(hdc
, startx
, starty
, NULL
);
3487 LineTo(hdc
, startx
+ dx
* length
, starty
+ dy
* length
);
3488 oldpen
= SelectObject(hdc
, oldpen
);
3489 DeleteObject(oldpen
);
3491 for (i
= 0; i
< length
; i
++) {
3493 SetPixel(hdc
, startx
, starty
, colours
[261]);
3502 /* This function gets the actual width of a character in the normal font.
3504 int char_width(Context ctx
, int uc
) {
3508 /* If the font max is the same as the font ave width then this
3509 * function is a no-op.
3511 if (!font_dualwidth
) return 1;
3513 switch (uc
& CSET_MASK
) {
3515 uc
= ucsdata
.unitab_line
[uc
& 0xFF];
3518 uc
= ucsdata
.unitab_xterm
[uc
& 0xFF];
3521 uc
= ucsdata
.unitab_scoacs
[uc
& 0xFF];
3524 if (DIRECT_FONT(uc
)) {
3525 if (ucsdata
.dbcs_screenfont
) return 1;
3527 /* Speedup, I know of no font where ascii is the wrong width */
3528 if ((uc
&~CSET_MASK
) >= ' ' && (uc
&~CSET_MASK
)<= '~')
3531 if ( (uc
& CSET_MASK
) == CSET_ACP
) {
3532 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3533 } else if ( (uc
& CSET_MASK
) == CSET_OEMCP
) {
3534 another_font(FONT_OEM
);
3535 if (!fonts
[FONT_OEM
]) return 0;
3537 SelectObject(hdc
, fonts
[FONT_OEM
]);
3541 if ( GetCharWidth32(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1 &&
3542 GetCharWidth(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1)
3545 /* Speedup, I know of no font where ascii is the wrong width */
3546 if (uc
>= ' ' && uc
<= '~') return 1;
3548 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3549 if ( GetCharWidth32W(hdc
, uc
, uc
, &ibuf
) == 1 )
3550 /* Okay that one worked */ ;
3551 else if ( GetCharWidthW(hdc
, uc
, uc
, &ibuf
) == 1 )
3552 /* This should work on 9x too, but it's "less accurate" */ ;
3557 ibuf
+= font_width
/ 2 -1;
3564 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3565 * codes. Returns number of bytes used or zero to drop the message
3566 * or -1 to forward the message to windows.
3568 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
3569 unsigned char *output
)
3572 int scan
, left_alt
= 0, key_down
, shift_state
;
3574 unsigned char *p
= output
;
3575 static int alt_sum
= 0;
3577 HKL kbd_layout
= GetKeyboardLayout(0);
3579 /* keys is for ToAsciiEx. There's some ick here, see below. */
3580 static WORD keys
[3];
3581 static int compose_char
= 0;
3582 static WPARAM compose_key
= 0;
3584 r
= GetKeyboardState(keystate
);
3586 memset(keystate
, 0, sizeof(keystate
));
3589 #define SHOW_TOASCII_RESULT
3590 { /* Tell us all about key events */
3591 static BYTE oldstate
[256];
3592 static int first
= 1;
3596 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3599 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
) {
3601 } else if ((HIWORD(lParam
) & KF_UP
)
3602 && scan
== (HIWORD(lParam
) & 0xFF)) {
3606 if (wParam
>= VK_F1
&& wParam
<= VK_F20
)
3607 debug(("K_F%d", wParam
+ 1 - VK_F1
));
3620 debug(("VK_%02x", wParam
));
3622 if (message
== WM_SYSKEYDOWN
|| message
== WM_SYSKEYUP
)
3624 debug((", S%02x", scan
= (HIWORD(lParam
) & 0xFF)));
3626 ch
= MapVirtualKeyEx(wParam
, 2, kbd_layout
);
3627 if (ch
>= ' ' && ch
<= '~')
3628 debug((", '%c'", ch
));
3630 debug((", $%02x", ch
));
3633 debug((", KB0=%02x", keys
[0]));
3635 debug((", KB1=%02x", keys
[1]));
3637 debug((", KB2=%02x", keys
[2]));
3639 if ((keystate
[VK_SHIFT
] & 0x80) != 0)
3641 if ((keystate
[VK_CONTROL
] & 0x80) != 0)
3643 if ((HIWORD(lParam
) & KF_EXTENDED
))
3645 if ((HIWORD(lParam
) & KF_UP
))
3649 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
);
3650 else if ((HIWORD(lParam
) & KF_UP
))
3651 oldstate
[wParam
& 0xFF] ^= 0x80;
3653 oldstate
[wParam
& 0xFF] ^= 0x81;
3655 for (ch
= 0; ch
< 256; ch
++)
3656 if (oldstate
[ch
] != keystate
[ch
])
3657 debug((", M%02x=%02x", ch
, keystate
[ch
]));
3659 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3663 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
)) {
3664 keystate
[VK_RMENU
] = keystate
[VK_MENU
];
3668 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3669 if ((cfg
.funky_type
== FUNKY_VT400
||
3670 (cfg
.funky_type
<= FUNKY_LINUX
&& term
->app_keypad_keys
&&
3672 && wParam
== VK_NUMLOCK
&& !(keystate
[VK_SHIFT
] & 0x80)) {
3674 wParam
= VK_EXECUTE
;
3676 /* UnToggle NUMLock */
3677 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0)
3678 keystate
[VK_NUMLOCK
] ^= 1;
3681 /* And write back the 'adjusted' state */
3682 SetKeyboardState(keystate
);
3685 /* Disable Auto repeat if required */
3686 if (term
->repeat_off
&&
3687 (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
)
3690 if ((HIWORD(lParam
) & KF_ALTDOWN
) && (keystate
[VK_RMENU
] & 0x80) == 0)
3693 key_down
= ((HIWORD(lParam
) & KF_UP
) == 0);
3695 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3696 if (left_alt
&& (keystate
[VK_CONTROL
] & 0x80)) {
3697 if (cfg
.ctrlaltkeys
)
3698 keystate
[VK_MENU
] = 0;
3700 keystate
[VK_RMENU
] = 0x80;
3705 scan
= (HIWORD(lParam
) & (KF_UP
| KF_EXTENDED
| 0xFF));
3706 shift_state
= ((keystate
[VK_SHIFT
] & 0x80) != 0)
3707 + ((keystate
[VK_CONTROL
] & 0x80) != 0) * 2;
3709 /* Note if AltGr was pressed and if it was used as a compose key */
3710 if (!compose_state
) {
3711 compose_key
= 0x100;
3712 if (cfg
.compose_key
) {
3713 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
))
3714 compose_key
= wParam
;
3716 if (wParam
== VK_APPS
)
3717 compose_key
= wParam
;
3720 if (wParam
== compose_key
) {
3721 if (compose_state
== 0
3722 && (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0) compose_state
=
3724 else if (compose_state
== 1 && (HIWORD(lParam
) & KF_UP
))
3728 } else if (compose_state
== 1 && wParam
!= VK_CONTROL
)
3731 if (compose_state
> 1 && left_alt
)
3734 /* Sanitize the number pad if not using a PC NumPad */
3735 if (left_alt
|| (term
->app_keypad_keys
&& !cfg
.no_applic_k
3736 && cfg
.funky_type
!= FUNKY_XTERM
)
3737 || cfg
.funky_type
== FUNKY_VT400
|| cfg
.nethack_keypad
|| compose_state
) {
3738 if ((HIWORD(lParam
) & KF_EXTENDED
) == 0) {
3742 nParam
= VK_NUMPAD0
;
3745 nParam
= VK_NUMPAD1
;
3748 nParam
= VK_NUMPAD2
;
3751 nParam
= VK_NUMPAD3
;
3754 nParam
= VK_NUMPAD4
;
3757 nParam
= VK_NUMPAD5
;
3760 nParam
= VK_NUMPAD6
;
3763 nParam
= VK_NUMPAD7
;
3766 nParam
= VK_NUMPAD8
;
3769 nParam
= VK_NUMPAD9
;
3772 nParam
= VK_DECIMAL
;
3776 if (keystate
[VK_NUMLOCK
] & 1)
3783 /* If a key is pressed and AltGr is not active */
3784 if (key_down
&& (keystate
[VK_RMENU
] & 0x80) == 0 && !compose_state
) {
3785 /* Okay, prepare for most alts then ... */
3789 /* Lets see if it's a pattern we know all about ... */
3790 if (wParam
== VK_PRIOR
&& shift_state
== 1) {
3791 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEUP
, 0);
3794 if (wParam
== VK_PRIOR
&& shift_state
== 2) {
3795 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEUP
, 0);
3798 if (wParam
== VK_NEXT
&& shift_state
== 1) {
3799 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEDOWN
, 0);
3802 if (wParam
== VK_NEXT
&& shift_state
== 2) {
3803 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEDOWN
, 0);
3806 if (wParam
== VK_INSERT
&& shift_state
== 1) {
3807 term_do_paste(term
);
3810 if (left_alt
&& wParam
== VK_F4
&& cfg
.alt_f4
) {
3813 if (left_alt
&& wParam
== VK_SPACE
&& cfg
.alt_space
) {
3814 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_KEYMENU
, 0);
3817 if (left_alt
&& wParam
== VK_RETURN
&& cfg
.fullscreenonaltenter
&&
3818 (cfg
.resize_action
!= RESIZE_DISABLED
)) {
3819 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) != KF_REPEAT
)
3823 /* Control-Numlock for app-keypad mode switch */
3824 if (wParam
== VK_PAUSE
&& shift_state
== 2) {
3825 term
->app_keypad_keys
^= 1;
3829 /* Nethack keypad */
3830 if (cfg
.nethack_keypad
&& !left_alt
) {
3833 *p
++ = "bB\002\002"[shift_state
& 3];
3836 *p
++ = "jJ\012\012"[shift_state
& 3];
3839 *p
++ = "nN\016\016"[shift_state
& 3];
3842 *p
++ = "hH\010\010"[shift_state
& 3];
3845 *p
++ = shift_state ?
'.' : '.';
3848 *p
++ = "lL\014\014"[shift_state
& 3];
3851 *p
++ = "yY\031\031"[shift_state
& 3];
3854 *p
++ = "kK\013\013"[shift_state
& 3];
3857 *p
++ = "uU\025\025"[shift_state
& 3];
3862 /* Application Keypad */
3866 if (cfg
.funky_type
== FUNKY_VT400
||
3867 (cfg
.funky_type
<= FUNKY_LINUX
&&
3868 term
->app_keypad_keys
&& !cfg
.no_applic_k
)) switch (wParam
) {
3882 if (term
->app_keypad_keys
&& !cfg
.no_applic_k
)
3919 if (cfg
.funky_type
== FUNKY_XTERM
) {
3924 } else if (shift_state
)
3931 if (cfg
.funky_type
== FUNKY_XTERM
)
3935 if (cfg
.funky_type
== FUNKY_XTERM
)
3939 if (cfg
.funky_type
== FUNKY_XTERM
)
3944 if (HIWORD(lParam
) & KF_EXTENDED
)
3949 if (term
->vt52_mode
) {
3950 if (xkey
>= 'P' && xkey
<= 'S')
3951 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3953 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
3955 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3960 if (wParam
== VK_BACK
&& shift_state
== 0) { /* Backspace */
3961 *p
++ = (cfg
.bksp_is_delete ?
0x7F : 0x08);
3965 if (wParam
== VK_BACK
&& shift_state
== 1) { /* Shift Backspace */
3966 /* We do the opposite of what is configured */
3967 *p
++ = (cfg
.bksp_is_delete ?
0x08 : 0x7F);
3971 if (wParam
== VK_TAB
&& shift_state
== 1) { /* Shift tab */
3977 if (wParam
== VK_SPACE
&& shift_state
== 2) { /* Ctrl-Space */
3981 if (wParam
== VK_SPACE
&& shift_state
== 3) { /* Ctrl-Shift-Space */
3985 if (wParam
== VK_CANCEL
&& shift_state
== 2) { /* Ctrl-Break */
3990 if (wParam
== VK_PAUSE
) { /* Break/Pause */
3995 /* Control-2 to Control-8 are special */
3996 if (shift_state
== 2 && wParam
>= '2' && wParam
<= '8') {
3997 *p
++ = "\000\033\034\035\036\037\177"[wParam
- '2'];
4000 if (shift_state
== 2 && (wParam
== 0xBD || wParam
== 0xBF)) {
4004 if (shift_state
== 2 && wParam
== 0xDF) {
4008 if (shift_state
== 3 && wParam
== 0xDE) {
4009 *p
++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
4012 if (shift_state
== 0 && wParam
== VK_RETURN
&& term
->cr_lf_return
) {
4019 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
4020 * for integer decimal nn.)
4022 * We also deal with the weird ones here. Linux VCs replace F1
4023 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
4024 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
4030 code
= (keystate
[VK_SHIFT
] & 0x80 ?
23 : 11);
4033 code
= (keystate
[VK_SHIFT
] & 0x80 ?
24 : 12);
4036 code
= (keystate
[VK_SHIFT
] & 0x80 ?
25 : 13);
4039 code
= (keystate
[VK_SHIFT
] & 0x80 ?
26 : 14);
4042 code
= (keystate
[VK_SHIFT
] & 0x80 ?
28 : 15);
4045 code
= (keystate
[VK_SHIFT
] & 0x80 ?
29 : 17);
4048 code
= (keystate
[VK_SHIFT
] & 0x80 ?
31 : 18);
4051 code
= (keystate
[VK_SHIFT
] & 0x80 ?
32 : 19);
4054 code
= (keystate
[VK_SHIFT
] & 0x80 ?
33 : 20);
4057 code
= (keystate
[VK_SHIFT
] & 0x80 ?
34 : 21);
4090 if ((shift_state
&2) == 0) switch (wParam
) {
4110 /* Reorder edit keys to physical order */
4111 if (cfg
.funky_type
== FUNKY_VT400
&& code
<= 6)
4112 code
= "\0\2\1\4\5\3\6"[code
];
4114 if (term
->vt52_mode
&& code
> 0 && code
<= 6) {
4115 p
+= sprintf((char *) p
, "\x1B%c", " HLMEIG"[code
]);
4119 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO function keys */
4120 code
>= 11 && code
<= 34) {
4121 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
4124 case VK_F1
: index
= 0; break;
4125 case VK_F2
: index
= 1; break;
4126 case VK_F3
: index
= 2; break;
4127 case VK_F4
: index
= 3; break;
4128 case VK_F5
: index
= 4; break;
4129 case VK_F6
: index
= 5; break;
4130 case VK_F7
: index
= 6; break;
4131 case VK_F8
: index
= 7; break;
4132 case VK_F9
: index
= 8; break;
4133 case VK_F10
: index
= 9; break;
4134 case VK_F11
: index
= 10; break;
4135 case VK_F12
: index
= 11; break;
4137 if (keystate
[VK_SHIFT
] & 0x80) index
+= 12;
4138 if (keystate
[VK_CONTROL
] & 0x80) index
+= 24;
4139 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
4142 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO small keypad */
4143 code
>= 1 && code
<= 6) {
4144 char codes
[] = "HL.FIG";
4148 p
+= sprintf((char *) p
, "\x1B[%c", codes
[code
-1]);
4152 if ((term
->vt52_mode
|| cfg
.funky_type
== FUNKY_VT100P
) && code
>= 11 && code
<= 24) {
4158 if (term
->vt52_mode
)
4159 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11 - offt
);
4162 sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11 - offt
);
4165 if (cfg
.funky_type
== FUNKY_LINUX
&& code
>= 11 && code
<= 15) {
4166 p
+= sprintf((char *) p
, "\x1B[[%c", code
+ 'A' - 11);
4169 if (cfg
.funky_type
== FUNKY_XTERM
&& code
>= 11 && code
<= 14) {
4170 if (term
->vt52_mode
)
4171 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11);
4173 p
+= sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11);
4176 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
4177 p
+= sprintf((char *) p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
4181 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
4186 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
4187 * some reason seems to send VK_CLEAR to Windows...).
4209 if (term
->vt52_mode
)
4210 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4212 int app_flg
= (term
->app_cursor_keys
&& !cfg
.no_applic_c
);
4215 * RDB: VT100 & VT102 manuals both state the
4216 * app cursor keys only work if the app keypad
4219 * SGT: That may well be true, but xterm
4220 * disagrees and so does at least one
4221 * application, so I've #if'ed this out and the
4222 * behaviour is back to PuTTY's original: app
4223 * cursor and app keypad are independently
4224 * switchable modes. If anyone complains about
4225 * _this_ I'll have to put in a configurable
4228 if (!term
->app_keypad_keys
)
4231 /* Useful mapping of Ctrl-arrows */
4232 if (shift_state
== 2)
4236 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4238 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4245 * Finally, deal with Return ourselves. (Win95 seems to
4246 * foul it up when Alt is pressed, for some reason.)
4248 if (wParam
== VK_RETURN
) { /* Return */
4254 if (left_alt
&& wParam
>= VK_NUMPAD0
&& wParam
<= VK_NUMPAD9
)
4255 alt_sum
= alt_sum
* 10 + wParam
- VK_NUMPAD0
;
4260 /* Okay we've done everything interesting; let windows deal with
4261 * the boring stuff */
4265 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4266 if(cfg
.xlat_capslockcyr
&& keystate
[VK_CAPITAL
] != 0) {
4268 keystate
[VK_CAPITAL
] = 0;
4271 /* XXX how do we know what the max size of the keys array should
4272 * be is? There's indication on MS' website of an Inquire/InquireEx
4273 * functioning returning a KBINFO structure which tells us. */
4274 if (osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
4275 /* XXX 'keys' parameter is declared in MSDN documentation as
4277 * The experience of a French user indicates that on
4278 * Win98, WORD[] should be passed in, but on Win2K, it should
4279 * be BYTE[]. German WinXP and my Win2K with "US International"
4280 * driver corroborate this.
4281 * Experimentally I've conditionalised the behaviour on the
4282 * Win9x/NT split, but I suspect it's worse than that.
4283 * See wishlist item `win-dead-keys' for more horrible detail
4284 * and speculations. */
4287 r
= ToAsciiEx(wParam
, scan
, keystate
, (LPWORD
)keybs
, 0, kbd_layout
);
4288 for (i
=0; i
<3; i
++) keys
[i
] = keybs
[i
];
4290 r
= ToAsciiEx(wParam
, scan
, keystate
, keys
, 0, kbd_layout
);
4292 #ifdef SHOW_TOASCII_RESULT
4293 if (r
== 1 && !key_down
) {
4295 if (in_utf(term
) || ucsdata
.dbcs_screenfont
)
4296 debug((", (U+%04x)", alt_sum
));
4298 debug((", LCH(%d)", alt_sum
));
4300 debug((", ACH(%d)", keys
[0]));
4305 for (r1
= 0; r1
< r
; r1
++) {
4306 debug(("%s%d", r1 ?
"," : "", keys
[r1
]));
4315 * Interrupt an ongoing paste. I'm not sure this is
4316 * sensible, but for the moment it's preferable to
4317 * having to faff about buffering things.
4322 for (i
= 0; i
< r
; i
++) {
4323 unsigned char ch
= (unsigned char) keys
[i
];
4325 if (compose_state
== 2 && (ch
& 0x80) == 0 && ch
> ' ') {
4330 if (compose_state
== 3 && (ch
& 0x80) == 0 && ch
> ' ') {
4334 if ((nc
= check_compose(compose_char
, ch
)) == -1) {
4335 MessageBeep(MB_ICONHAND
);
4339 term_seen_key_event(term
);
4341 luni_send(ldisc
, &keybuf
, 1, 1);
4349 if (in_utf(term
) || ucsdata
.dbcs_screenfont
) {
4351 term_seen_key_event(term
);
4353 luni_send(ldisc
, &keybuf
, 1, 1);
4355 ch
= (char) alt_sum
;
4357 * We need not bother about stdin
4358 * backlogs here, because in GUI PuTTY
4359 * we can't do anything about it
4360 * anyway; there's no means of asking
4361 * Windows to hold off on KEYDOWN
4362 * messages. We _have_ to buffer
4363 * everything we're sent.
4365 term_seen_key_event(term
);
4367 ldisc_send(ldisc
, &ch
, 1, 1);
4371 term_seen_key_event(term
);
4373 lpage_send(ldisc
, kbd_codepage
, &ch
, 1, 1);
4376 if(capsOn
&& ch
< 0x80) {
4379 cbuf
[1] = xlat_uskbd2cyrllic(ch
);
4380 term_seen_key_event(term
);
4382 luni_send(ldisc
, cbuf
+!left_alt
, 1+!!left_alt
, 1);
4387 term_seen_key_event(term
);
4389 lpage_send(ldisc
, kbd_codepage
,
4390 cbuf
+!left_alt
, 1+!!left_alt
, 1);
4396 /* This is so the ALT-Numpad and dead keys work correctly. */
4401 /* If we're definitly not building up an ALT-54321 then clear it */
4404 /* If we will be using alt_sum fix the 256s */
4405 else if (keys
[0] && (in_utf(term
) || ucsdata
.dbcs_screenfont
))
4410 * ALT alone may or may not want to bring up the System menu.
4411 * If it's not meant to, we return 0 on presses or releases of
4412 * ALT, to show that we've swallowed the keystroke. Otherwise
4413 * we return -1, which means Windows will give the keystroke
4414 * its default handling (i.e. bring up the System menu).
4416 if (wParam
== VK_MENU
&& !cfg
.alt_only
)
4422 void request_paste(void *frontend
)
4425 * In Windows, pasting is synchronous: we can read the
4426 * clipboard with no difficulty, so request_paste() can just go
4429 term_do_paste(term
);
4432 void set_title(void *frontend
, char *title
)
4435 window_name
= snewn(1 + strlen(title
), char);
4436 strcpy(window_name
, title
);
4437 if (cfg
.win_name_always
|| !IsIconic(hwnd
))
4438 SetWindowText(hwnd
, title
);
4441 void set_icon(void *frontend
, char *title
)
4444 icon_name
= snewn(1 + strlen(title
), char);
4445 strcpy(icon_name
, title
);
4446 if (!cfg
.win_name_always
&& IsIconic(hwnd
))
4447 SetWindowText(hwnd
, title
);
4450 void set_sbar(void *frontend
, int total
, int start
, int page
)
4454 if (is_full_screen() ?
!cfg
.scrollbar_in_fullscreen
: !cfg
.scrollbar
)
4457 si
.cbSize
= sizeof(si
);
4458 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
4460 si
.nMax
= total
- 1;
4464 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
4467 Context
get_ctx(void *frontend
)
4473 SelectPalette(hdc
, pal
, FALSE
);
4479 void free_ctx(Context ctx
)
4481 SelectPalette(ctx
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
4482 ReleaseDC(hwnd
, ctx
);
4485 static void real_palette_set(int n
, int r
, int g
, int b
)
4488 logpal
->palPalEntry
[n
].peRed
= r
;
4489 logpal
->palPalEntry
[n
].peGreen
= g
;
4490 logpal
->palPalEntry
[n
].peBlue
= b
;
4491 logpal
->palPalEntry
[n
].peFlags
= PC_NOCOLLAPSE
;
4492 colours
[n
] = PALETTERGB(r
, g
, b
);
4493 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4495 colours
[n
] = RGB(r
, g
, b
);
4498 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
4502 if (n
> NALLCOLOURS
)
4504 real_palette_set(n
, r
, g
, b
);
4506 HDC hdc
= get_ctx(frontend
);
4507 UnrealizeObject(pal
);
4508 RealizePalette(hdc
);
4511 if (n
== (ATTR_DEFBG
>>ATTR_BGSHIFT
))
4512 /* If Default Background changes, we need to ensure any
4513 * space between the text area and the window border is
4515 InvalidateRect(hwnd
, NULL
, TRUE
);
4519 void palette_reset(void *frontend
)
4524 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4526 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
4527 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
4528 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
4529 logpal
->palPalEntry
[i
].peFlags
= 0;
4530 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
4531 defpal
[i
].rgbtGreen
,
4532 defpal
[i
].rgbtBlue
);
4534 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
4535 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4540 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4541 hdc
= get_ctx(frontend
);
4542 RealizePalette(hdc
);
4545 /* Default Background may have changed. Ensure any space between
4546 * text area and window border is redrawn. */
4547 InvalidateRect(hwnd
, NULL
, TRUE
);
4551 void write_aclip(void *frontend
, char *data
, int len
, int must_deselect
)
4556 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
4559 lock
= GlobalLock(clipdata
);
4562 memcpy(lock
, data
, len
);
4563 ((unsigned char *) lock
)[len
] = 0;
4564 GlobalUnlock(clipdata
);
4567 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4569 if (OpenClipboard(hwnd
)) {
4571 SetClipboardData(CF_TEXT
, clipdata
);
4574 GlobalFree(clipdata
);
4577 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4581 * Note: unlike write_aclip() this will not append a nul.
4583 void write_clip(void *frontend
, wchar_t * data
, int *attr
, int len
, int must_deselect
)
4585 HGLOBAL clipdata
, clipdata2
, clipdata3
;
4587 void *lock
, *lock2
, *lock3
;
4589 len2
= WideCharToMultiByte(CP_ACP
, 0, data
, len
, 0, 0, NULL
, NULL
);
4591 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
,
4592 len
* sizeof(wchar_t));
4593 clipdata2
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len2
);
4595 if (!clipdata
|| !clipdata2
) {
4597 GlobalFree(clipdata
);
4599 GlobalFree(clipdata2
);
4602 if (!(lock
= GlobalLock(clipdata
)))
4604 if (!(lock2
= GlobalLock(clipdata2
)))
4607 memcpy(lock
, data
, len
* sizeof(wchar_t));
4608 WideCharToMultiByte(CP_ACP
, 0, data
, len
, lock2
, len2
, NULL
, NULL
);
4610 if (cfg
.rtf_paste
) {
4611 wchar_t unitab
[256];
4613 unsigned char *tdata
= (unsigned char *)lock2
;
4614 wchar_t *udata
= (wchar_t *)lock
;
4615 int rtflen
= 0, uindex
= 0, tindex
= 0;
4617 int multilen
, blen
, alen
, totallen
, i
;
4618 char before
[16], after
[4];
4619 int fgcolour
, lastfgcolour
= 0;
4620 int bgcolour
, lastbgcolour
= 0;
4621 int attrBold
, lastAttrBold
= 0;
4622 int attrUnder
, lastAttrUnder
= 0;
4623 int palette
[NALLCOLOURS
];
4626 get_unitab(CP_ACP
, unitab
, 0);
4628 rtfsize
= 100 + strlen(cfg
.font
.name
);
4629 rtf
= snewn(rtfsize
, char);
4630 rtflen
= sprintf(rtf
, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
4631 cfg
.font
.name
, cfg
.font
.height
*2);
4634 * Add colour palette
4635 * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
4639 * First - Determine all colours in use
4640 * o Foregound and background colours share the same palette
4643 memset(palette
, 0, sizeof(palette
));
4644 for (i
= 0; i
< (len
-1); i
++) {
4645 fgcolour
= ((attr
[i
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4646 bgcolour
= ((attr
[i
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4648 if (attr
[i
] & ATTR_REVERSE
) {
4649 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4650 fgcolour
= bgcolour
;
4651 bgcolour
= tmpcolour
;
4654 if (bold_mode
== BOLD_COLOURS
&& (attr
[i
] & ATTR_BOLD
)) {
4655 if (fgcolour
< 8) /* ANSI colours */
4657 else if (fgcolour
>= 256) /* Default colours */
4661 if (attr
[i
] & ATTR_BLINK
) {
4662 if (bgcolour
< 8) /* ANSI colours */
4664 else if (bgcolour
>= 256) /* Default colours */
4668 palette
[fgcolour
]++;
4669 palette
[bgcolour
]++;
4673 * Next - Create a reduced palette
4676 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4677 if (palette
[i
] != 0)
4678 palette
[i
] = ++numcolours
;
4682 * Finally - Write the colour table
4684 rtf
= sresize(rtf
, rtfsize
+ (numcolours
* 25), char);
4685 strcat(rtf
, "{\\colortbl ;");
4686 rtflen
= strlen(rtf
);
4688 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4689 if (palette
[i
] != 0) {
4690 rtflen
+= sprintf(&rtf
[rtflen
], "\\red%d\\green%d\\blue%d;", defpal
[i
].rgbtRed
, defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4693 strcpy(&rtf
[rtflen
], "}");
4698 * We want to construct a piece of RTF that specifies the
4699 * same Unicode text. To do this we will read back in
4700 * parallel from the Unicode data in `udata' and the
4701 * non-Unicode data in `tdata'. For each character in
4702 * `tdata' which becomes the right thing in `udata' when
4703 * looked up in `unitab', we just copy straight over from
4704 * tdata. For each one that doesn't, we must WCToMB it
4705 * individually and produce a \u escape sequence.
4707 * It would probably be more robust to just bite the bullet
4708 * and WCToMB each individual Unicode character one by one,
4709 * then MBToWC each one back to see if it was an accurate
4710 * translation; but that strikes me as a horrifying number
4711 * of Windows API calls so I want to see if this faster way
4712 * will work. If it screws up badly we can always revert to
4713 * the simple and slow way.
4715 while (tindex
< len2
&& uindex
< len
&&
4716 tdata
[tindex
] && udata
[uindex
]) {
4717 if (tindex
+ 1 < len2
&&
4718 tdata
[tindex
] == '\r' &&
4719 tdata
[tindex
+1] == '\n') {
4725 * Set text attributes
4728 if (rtfsize
< rtflen
+ 64) {
4729 rtfsize
= rtflen
+ 512;
4730 rtf
= sresize(rtf
, rtfsize
, char);
4734 * Determine foreground and background colours
4736 fgcolour
= ((attr
[tindex
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4737 bgcolour
= ((attr
[tindex
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4739 if (attr
[tindex
] & ATTR_REVERSE
) {
4740 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4741 fgcolour
= bgcolour
;
4742 bgcolour
= tmpcolour
;
4745 if (bold_mode
== BOLD_COLOURS
&& (attr
[tindex
] & ATTR_BOLD
)) {
4746 if (fgcolour
< 8) /* ANSI colours */
4748 else if (fgcolour
>= 256) /* Default colours */
4752 if (attr
[tindex
] & ATTR_BLINK
) {
4753 if (bgcolour
< 8) /* ANSI colours */
4755 else if (bgcolour
>= 256) /* Default colours */
4760 * Collect other attributes
4762 if (bold_mode
!= BOLD_COLOURS
)
4763 attrBold
= attr
[tindex
] & ATTR_BOLD
;
4767 attrUnder
= attr
[tindex
] & ATTR_UNDER
;
4771 * o If video isn't reversed, ignore colour attributes for default foregound
4773 * o Special case where bolded text is displayed using the default foregound
4774 * and background colours - force to bolded RTF.
4776 if (!(attr
[tindex
] & ATTR_REVERSE
)) {
4777 if (bgcolour
>= 256) /* Default color */
4778 bgcolour
= -1; /* No coloring */
4780 if (fgcolour
>= 256) { /* Default colour */
4781 if (bold_mode
== BOLD_COLOURS
&& (fgcolour
& 1) && bgcolour
== -1)
4782 attrBold
= ATTR_BOLD
; /* Emphasize text with bold attribute */
4784 fgcolour
= -1; /* No coloring */
4789 * Write RTF text attributes
4791 if (lastfgcolour
!= fgcolour
) {
4792 lastfgcolour
= fgcolour
;
4793 rtflen
+= sprintf(&rtf
[rtflen
], "\\cf%d ", (fgcolour
>= 0) ? palette
[fgcolour
] : 0);
4796 if (lastbgcolour
!= bgcolour
) {
4797 lastbgcolour
= bgcolour
;
4798 rtflen
+= sprintf(&rtf
[rtflen
], "\\highlight%d ", (bgcolour
>= 0) ? palette
[bgcolour
] : 0);
4801 if (lastAttrBold
!= attrBold
) {
4802 lastAttrBold
= attrBold
;
4803 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrBold ?
"\\b " : "\\b0 ");
4806 if (lastAttrUnder
!= attrUnder
) {
4807 lastAttrUnder
= attrUnder
;
4808 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrUnder ?
"\\ul " : "\\ulnone ");
4812 if (unitab
[tdata
[tindex
]] == udata
[uindex
]) {
4818 multilen
= WideCharToMultiByte(CP_ACP
, 0, unitab
+uindex
, 1,
4819 NULL
, 0, NULL
, NULL
);
4820 if (multilen
!= 1) {
4821 blen
= sprintf(before
, "{\\uc%d\\u%d", multilen
,
4823 alen
= 1; strcpy(after
, "}");
4825 blen
= sprintf(before
, "\\u%d", udata
[uindex
]);
4826 alen
= 0; after
[0] = '\0';
4829 assert(tindex
+ multilen
<= len2
);
4830 totallen
= blen
+ alen
;
4831 for (i
= 0; i
< multilen
; i
++) {
4832 if (tdata
[tindex
+i
] == '\\' ||
4833 tdata
[tindex
+i
] == '{' ||
4834 tdata
[tindex
+i
] == '}')
4836 else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A)
4837 totallen
+= 6; /* \par\r\n */
4838 else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20)
4844 if (rtfsize
< rtflen
+ totallen
+ 3) {
4845 rtfsize
= rtflen
+ totallen
+ 512;
4846 rtf
= sresize(rtf
, rtfsize
, char);
4849 strcpy(rtf
+ rtflen
, before
); rtflen
+= blen
;
4850 for (i
= 0; i
< multilen
; i
++) {
4851 if (tdata
[tindex
+i
] == '\\' ||
4852 tdata
[tindex
+i
] == '{' ||
4853 tdata
[tindex
+i
] == '}') {
4854 rtf
[rtflen
++] = '\\';
4855 rtf
[rtflen
++] = tdata
[tindex
+i
];
4856 } else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A) {
4857 rtflen
+= sprintf(rtf
+rtflen
, "\\par\r\n");
4858 } else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20) {
4859 rtflen
+= sprintf(rtf
+rtflen
, "\\'%02x", tdata
[tindex
+i
]);
4861 rtf
[rtflen
++] = tdata
[tindex
+i
];
4864 strcpy(rtf
+ rtflen
, after
); rtflen
+= alen
;
4870 rtf
[rtflen
++] = '}'; /* Terminate RTF stream */
4871 rtf
[rtflen
++] = '\0';
4872 rtf
[rtflen
++] = '\0';
4874 clipdata3
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, rtflen
);
4875 if (clipdata3
&& (lock3
= GlobalLock(clipdata3
)) != NULL
) {
4876 memcpy(lock3
, rtf
, rtflen
);
4877 GlobalUnlock(clipdata3
);
4883 GlobalUnlock(clipdata
);
4884 GlobalUnlock(clipdata2
);
4887 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4889 if (OpenClipboard(hwnd
)) {
4891 SetClipboardData(CF_UNICODETEXT
, clipdata
);
4892 SetClipboardData(CF_TEXT
, clipdata2
);
4894 SetClipboardData(RegisterClipboardFormat(CF_RTF
), clipdata3
);
4897 GlobalFree(clipdata
);
4898 GlobalFree(clipdata2
);
4902 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4905 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
4907 static HGLOBAL clipdata
= NULL
;
4908 static wchar_t *converted
= 0;
4917 GlobalUnlock(clipdata
);
4920 } else if (OpenClipboard(NULL
)) {
4921 if ((clipdata
= GetClipboardData(CF_UNICODETEXT
))) {
4923 *p
= GlobalLock(clipdata
);
4925 for (p2
= *p
; *p2
; p2
++);
4929 } else if ( (clipdata
= GetClipboardData(CF_TEXT
)) ) {
4933 s
= GlobalLock(clipdata
);
4934 i
= MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, 0, 0);
4935 *p
= converted
= snewn(i
, wchar_t);
4936 MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, converted
, i
);
4949 * Move `lines' lines from position `from' to position `to' in the
4952 void optimised_move(void *frontend
, int to
, int from
, int lines
)
4957 min
= (to
< from ? to
: from
);
4958 max
= to
+ from
- min
;
4960 r
.left
= offset_width
;
4961 r
.right
= offset_width
+ term
->cols
* font_width
;
4962 r
.top
= offset_height
+ min
* font_height
;
4963 r
.bottom
= offset_height
+ (max
+ lines
) * font_height
;
4964 ScrollWindow(hwnd
, 0, (to
- from
) * font_height
, &r
, &r
);
4969 * Print a message box and perform a fatal exit.
4971 void fatalbox(char *fmt
, ...)
4974 char *stuff
, morestuff
[100];
4977 stuff
= dupvprintf(fmt
, ap
);
4979 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4980 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
4986 * Print a modal (Really Bad) message box and perform a fatal exit.
4988 void modalfatalbox(char *fmt
, ...)
4991 char *stuff
, morestuff
[100];
4994 stuff
= dupvprintf(fmt
, ap
);
4996 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4997 MessageBox(hwnd
, stuff
, morestuff
,
4998 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
5003 typedef BOOL (WINAPI
*p_FlashWindowEx_t
)(PFLASHWINFO
);
5004 static p_FlashWindowEx_t p_FlashWindowEx
= NULL
;
5006 static void init_flashwindow(void)
5008 HMODULE user32_module
= LoadLibrary("USER32.DLL");
5009 if (user32_module
) {
5010 p_FlashWindowEx
= (p_FlashWindowEx_t
)
5011 GetProcAddress(user32_module
, "FlashWindowEx");
5015 static BOOL
flash_window_ex(DWORD dwFlags
, UINT uCount
, DWORD dwTimeout
)
5017 if (p_FlashWindowEx
) {
5019 fi
.cbSize
= sizeof(fi
);
5021 fi
.dwFlags
= dwFlags
;
5023 fi
.dwTimeout
= dwTimeout
;
5024 return (*p_FlashWindowEx
)(&fi
);
5027 return FALSE
; /* shrug */
5030 static void flash_window(int mode
);
5031 static long next_flash
;
5032 static int flashing
= 0;
5035 * Timer for platforms where we must maintain window flashing manually
5038 static void flash_window_timer(void *ctx
, long now
)
5040 if (flashing
&& now
- next_flash
>= 0) {
5046 * Manage window caption / taskbar flashing, if enabled.
5047 * 0 = stop, 1 = maintain, 2 = start
5049 static void flash_window(int mode
)
5051 if ((mode
== 0) || (cfg
.beep_ind
== B_IND_DISABLED
)) {
5055 if (p_FlashWindowEx
)
5056 flash_window_ex(FLASHW_STOP
, 0, 0);
5058 FlashWindow(hwnd
, FALSE
);
5061 } else if (mode
== 2) {
5065 if (p_FlashWindowEx
) {
5066 /* For so-called "steady" mode, we use uCount=2, which
5067 * seems to be the traditional number of flashes used
5068 * by user notifications (e.g., by Explorer).
5069 * uCount=0 appears to enable continuous flashing, per
5070 * "flashing" mode, although I haven't seen this
5072 flash_window_ex(FLASHW_ALL
| FLASHW_TIMER
,
5073 (cfg
.beep_ind
== B_IND_FLASH ?
0 : 2),
5074 0 /* system cursor blink rate */);
5075 /* No need to schedule timer */
5077 FlashWindow(hwnd
, TRUE
);
5078 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
5082 } else if ((mode
== 1) && (cfg
.beep_ind
== B_IND_FLASH
)) {
5084 if (flashing
&& !p_FlashWindowEx
) {
5085 FlashWindow(hwnd
, TRUE
); /* toggle */
5086 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
5094 void do_beep(void *frontend
, int mode
)
5096 if (mode
== BELL_DEFAULT
) {
5098 * For MessageBeep style bells, we want to be careful of
5099 * timing, because they don't have the nice property of
5100 * PlaySound bells that each one cancels the previous
5101 * active one. So we limit the rate to one per 50ms or so.
5103 static long lastbeep
= 0;
5106 beepdiff
= GetTickCount() - lastbeep
;
5107 if (beepdiff
>= 0 && beepdiff
< 50)
5111 * The above MessageBeep call takes time, so we record the
5112 * time _after_ it finishes rather than before it starts.
5114 lastbeep
= GetTickCount();
5115 } else if (mode
== BELL_WAVEFILE
) {
5116 if (!PlaySound(cfg
.bell_wavefile
.path
, NULL
,
5117 SND_ASYNC
| SND_FILENAME
)) {
5118 char buf
[sizeof(cfg
.bell_wavefile
.path
) + 80];
5120 sprintf(buf
, "Unable to play sound file\n%s\n"
5121 "Using default sound instead", cfg
.bell_wavefile
.path
);
5122 sprintf(otherbuf
, "%.70s Sound Error", appname
);
5123 MessageBox(hwnd
, buf
, otherbuf
,
5124 MB_OK
| MB_ICONEXCLAMATION
);
5125 cfg
.beep
= BELL_DEFAULT
;
5127 } else if (mode
== BELL_PCSPEAKER
) {
5128 static long lastbeep
= 0;
5131 beepdiff
= GetTickCount() - lastbeep
;
5132 if (beepdiff
>= 0 && beepdiff
< 50)
5136 * We must beep in different ways depending on whether this
5137 * is a 95-series or NT-series OS.
5139 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
5143 lastbeep
= GetTickCount();
5145 /* Otherwise, either visual bell or disabled; do nothing here */
5146 if (!term
->has_focus
) {
5147 flash_window(2); /* start */
5152 * Minimise or restore the window in response to a server-side
5155 void set_iconic(void *frontend
, int iconic
)
5157 if (IsIconic(hwnd
)) {
5159 ShowWindow(hwnd
, SW_RESTORE
);
5162 ShowWindow(hwnd
, SW_MINIMIZE
);
5167 * Move the window in response to a server-side request.
5169 void move_window(void *frontend
, int x
, int y
)
5171 if (cfg
.resize_action
== RESIZE_DISABLED
||
5172 cfg
.resize_action
== RESIZE_FONT
||
5176 SetWindowPos(hwnd
, NULL
, x
, y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
);
5180 * Move the window to the top or bottom of the z-order in response
5181 * to a server-side request.
5183 void set_zorder(void *frontend
, int top
)
5185 if (cfg
.alwaysontop
)
5186 return; /* ignore */
5187 SetWindowPos(hwnd
, top ? HWND_TOP
: HWND_BOTTOM
, 0, 0, 0, 0,
5188 SWP_NOMOVE
| SWP_NOSIZE
);
5192 * Refresh the window in response to a server-side request.
5194 void refresh_window(void *frontend
)
5196 InvalidateRect(hwnd
, NULL
, TRUE
);
5200 * Maximise or restore the window in response to a server-side
5203 void set_zoomed(void *frontend
, int zoomed
)
5205 if (IsZoomed(hwnd
)) {
5207 ShowWindow(hwnd
, SW_RESTORE
);
5210 ShowWindow(hwnd
, SW_MAXIMIZE
);
5215 * Report whether the window is iconic, for terminal reports.
5217 int is_iconic(void *frontend
)
5219 return IsIconic(hwnd
);
5223 * Report the window's position, for terminal reports.
5225 void get_window_pos(void *frontend
, int *x
, int *y
)
5228 GetWindowRect(hwnd
, &r
);
5234 * Report the window's pixel size, for terminal reports.
5236 void get_window_pixels(void *frontend
, int *x
, int *y
)
5239 GetWindowRect(hwnd
, &r
);
5240 *x
= r
.right
- r
.left
;
5241 *y
= r
.bottom
- r
.top
;
5245 * Return the window or icon title.
5247 char *get_window_title(void *frontend
, int icon
)
5249 return icon ? icon_name
: window_name
;
5253 * See if we're in full-screen mode.
5255 static int is_full_screen()
5257 if (!IsZoomed(hwnd
))
5259 if (GetWindowLongPtr(hwnd
, GWL_STYLE
) & WS_CAPTION
)
5264 /* Get the rect/size of a full screen window using the nearest available
5265 * monitor in multimon systems; default to something sensible if only
5266 * one monitor is present. */
5267 static int get_fullscreen_rect(RECT
* ss
)
5269 #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
5272 mon
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
5273 mi
.cbSize
= sizeof(mi
);
5274 GetMonitorInfo(mon
, &mi
);
5276 /* structure copy */
5280 /* could also use code like this:
5281 ss->left = ss->top = 0;
5282 ss->right = GetSystemMetrics(SM_CXSCREEN);
5283 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
5285 return GetClientRect(GetDesktopWindow(), ss
);
5291 * Go full-screen. This should only be called when we are already
5294 static void make_full_screen()
5299 assert(IsZoomed(hwnd
));
5301 if (is_full_screen())
5304 /* Remove the window furniture. */
5305 style
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5306 style
&= ~(WS_CAPTION
| WS_BORDER
| WS_THICKFRAME
);
5307 if (cfg
.scrollbar_in_fullscreen
)
5308 style
|= WS_VSCROLL
;
5310 style
&= ~WS_VSCROLL
;
5311 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5313 /* Resize ourselves to exactly cover the nearest monitor. */
5314 get_fullscreen_rect(&ss
);
5315 SetWindowPos(hwnd
, HWND_TOP
, ss
.left
, ss
.top
,
5320 /* We may have changed size as a result */
5324 /* Tick the menu item in the System menu. */
5325 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
5330 * Clear the full-screen attributes.
5332 static void clear_full_screen()
5334 DWORD oldstyle
, style
;
5336 /* Reinstate the window furniture. */
5337 style
= oldstyle
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5338 style
|= WS_CAPTION
| WS_BORDER
;
5339 if (cfg
.resize_action
== RESIZE_DISABLED
)
5340 style
&= ~WS_THICKFRAME
;
5342 style
|= WS_THICKFRAME
;
5344 style
|= WS_VSCROLL
;
5346 style
&= ~WS_VSCROLL
;
5347 if (style
!= oldstyle
) {
5348 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5349 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
5350 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
5354 /* Untick the menu item in the System menu. */
5355 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
5360 * Toggle full-screen mode.
5362 static void flip_full_screen()
5364 if (is_full_screen()) {
5365 ShowWindow(hwnd
, SW_RESTORE
);
5366 } else if (IsZoomed(hwnd
)) {
5369 SendMessage(hwnd
, WM_FULLSCR_ON_MAX
, 0, 0);
5370 ShowWindow(hwnd
, SW_MAXIMIZE
);
5374 void frontend_keypress(void *handle
)
5377 * Keypress termination in non-Close-On-Exit mode is not
5378 * currently supported in PuTTY proper, because the window
5379 * always has a perfectly good Close button anyway. So we do
5385 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
5387 return term_data(term
, is_stderr
, data
, len
);
5390 int from_backend_untrusted(void *frontend
, const char *data
, int len
)
5392 return term_data_untrusted(term
, data
, len
);
5395 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
5398 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
5400 ret
= term_get_userpass_input(term
, p
, in
, inlen
);
5404 void agent_schedule_callback(void (*callback
)(void *, void *, int),
5405 void *callback_ctx
, void *data
, int len
)
5407 struct agent_callback
*c
= snew(struct agent_callback
);
5408 c
->callback
= callback
;
5409 c
->callback_ctx
= callback_ctx
;
5412 PostMessage(hwnd
, WM_AGENT_CALLBACK
, 0, (LPARAM
)c
);