2 * window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
3 * emulator and backend in a window.
14 #define COMPILE_MULTIMON_STUBS
17 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
32 /* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
33 * wParam are used by Windows, and should be masked off, so we shouldn't
34 * attempt to store information in them. Hence all these identifiers have
35 * the low 4 bits clear. Also, identifiers should < 0xF000. */
37 #define IDM_SHOWLOG 0x0010
38 #define IDM_NEWSESS 0x0020
39 #define IDM_DUPSESS 0x0030
40 #define IDM_RESTART 0x0040
41 #define IDM_RECONF 0x0050
42 #define IDM_CLRSB 0x0060
43 #define IDM_RESET 0x0070
44 #define IDM_HELP 0x0140
45 #define IDM_ABOUT 0x0150
46 #define IDM_SAVEDSESS 0x0160
47 #define IDM_COPYALL 0x0170
48 #define IDM_FULLSCREEN 0x0180
49 #define IDM_PASTE 0x0190
50 #define IDM_SPECIALSEP 0x0200
52 #define IDM_SPECIAL_MIN 0x0400
53 #define IDM_SPECIAL_MAX 0x0800
55 #define IDM_SAVED_MIN 0x1000
56 #define IDM_SAVED_MAX 0x5000
57 #define MENU_SAVED_STEP 16
58 /* Maximum number of sessions on saved-session submenu */
59 #define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
61 #define WM_IGNORE_CLIP (WM_APP + 2)
62 #define WM_FULLSCR_ON_MAX (WM_APP + 3)
63 #define WM_AGENT_CALLBACK (WM_APP + 4)
64 #define WM_GOT_CLIPDATA (WM_APP + 6)
66 /* Needed for Chinese support and apparently not always defined. */
68 #define VK_PROCESSKEY 0xE5
71 /* Mouse wheel support. */
73 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
76 #define WHEEL_DELTA 120
79 static Mouse_Button
translate_button(Mouse_Button button
);
80 static LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
81 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
82 unsigned char *output
);
83 static void cfgtopalette(void);
84 static void systopalette(void);
85 static void init_palette(void);
86 static void init_fonts(int, int);
87 static void another_font(int);
88 static void deinit_fonts(void);
89 static void set_input_locale(HKL
);
90 static void update_savedsess_menu(void);
91 static void init_flashwindow(void);
93 static int is_full_screen(void);
94 static void make_full_screen(void);
95 static void clear_full_screen(void);
96 static void flip_full_screen(void);
97 static int process_clipdata(HGLOBAL clipdata
, int unicode
);
99 /* Window layout information */
100 static void reset_window(int);
101 static int extra_width
, extra_height
;
102 static int font_width
, font_height
, font_dualwidth
;
103 static int offset_width
, offset_height
;
104 static int was_zoomed
= 0;
105 static int prev_rows
, prev_cols
;
107 static int pending_netevent
= 0;
108 static WPARAM pend_netevent_wParam
= 0;
109 static LPARAM pend_netevent_lParam
= 0;
110 static void enact_pending_netevent(void);
111 static void flash_window(int mode
);
112 static void sys_cursor_update(void);
113 static int is_shift_pressed(void);
114 static int get_fullscreen_rect(RECT
* ss
);
116 static int caret_x
= -1, caret_y
= -1;
118 static int kbd_codepage
;
121 static Backend
*back
;
122 static void *backhandle
;
124 static struct unicode_data ucsdata
;
125 static int must_close_session
, session_closed
;
126 static int reconfiguring
= FALSE
;
128 static const struct telnet_special
*specials
= NULL
;
129 static HMENU specials_menu
= NULL
;
130 static int n_specials
= 0;
132 static wchar_t *clipboard_contents
;
133 static size_t clipboard_length
;
135 #define TIMING_TIMER_ID 1234
136 static long timing_next_time
;
141 enum { SYSMENU
, CTXMENU
};
142 static HMENU savedsess_menu
;
144 Config cfg
; /* exported to windlg.c */
146 static struct sesslist sesslist
; /* for saved-session menu */
148 struct agent_callback
{
149 void (*callback
)(void *, void *, int);
155 #define FONT_NORMAL 0
157 #define FONT_UNDERLINE 2
158 #define FONT_BOLDUND 3
159 #define FONT_WIDE 0x04
160 #define FONT_HIGH 0x08
161 #define FONT_NARROW 0x10
163 #define FONT_OEM 0x20
164 #define FONT_OEMBOLD 0x21
165 #define FONT_OEMUND 0x22
166 #define FONT_OEMBOLDUND 0x23
168 #define FONT_MAXNO 0x2F
170 static HFONT fonts
[FONT_MAXNO
];
171 static LOGFONT lfont
;
172 static int fontflag
[FONT_MAXNO
];
174 BOLD_COLOURS
, BOLD_SHADOW
, BOLD_FONT
181 #define NCFGCOLOURS 22
182 #define NEXTCOLOURS 240
183 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
184 static COLORREF colours
[NALLCOLOURS
];
186 static LPLOGPALETTE logpal
;
187 static RGBTRIPLE defpal
[NALLCOLOURS
];
189 static HBITMAP caretbm
;
191 static int dbltime
, lasttime
, lastact
;
192 static Mouse_Button lastbtn
;
194 /* this allows xterm-style mouse handling. */
195 static int send_raw_mouse
= 0;
196 static int wheel_accumulator
= 0;
198 static int busy_status
= BUSY_NOT
;
200 static char *window_name
, *icon_name
;
202 static int compose_state
= 0;
204 static UINT wm_mousewheel
= WM_MOUSEWHEEL
;
206 /* Dummy routine, only required in plink. */
207 void ldisc_update(void *frontend
, int echo
, int edit
)
211 char *get_ttymode(void *frontend
, const char *mode
)
213 return term_get_ttymode(term
, mode
);
216 static void start_backend(void)
219 char msg
[1024], *title
;
224 * Select protocol. This is farmed out into a table in a
225 * separate file to enable an ssh-free variant.
227 back
= backend_from_proto(cfg
.protocol
);
229 char *str
= dupprintf("%s Internal Error", appname
);
230 MessageBox(NULL
, "Unsupported protocol number found",
231 str
, MB_OK
| MB_ICONEXCLAMATION
);
236 error
= back
->init(NULL
, &backhandle
, &cfg
,
237 cfg
.host
, cfg
.port
, &realhost
, cfg
.tcp_nodelay
,
239 back
->provide_logctx(backhandle
, logctx
);
241 char *str
= dupprintf("%s Error", appname
);
242 sprintf(msg
, "Unable to open connection to\n"
243 "%.800s\n" "%s", cfg_dest(&cfg
), error
);
244 MessageBox(NULL
, msg
, str
, MB_ICONERROR
| MB_OK
);
248 window_name
= icon_name
= NULL
;
250 title
= cfg
.wintitle
;
252 sprintf(msg
, "%s - %s", realhost
, appname
);
256 set_title(NULL
, title
);
257 set_icon(NULL
, title
);
260 * Connect the terminal to the backend for resize purposes.
262 term_provide_resize_fn(term
, back
->size
, backhandle
);
265 * Set up a line discipline.
267 ldisc
= ldisc_create(&cfg
, term
, back
, backhandle
, NULL
);
270 * Destroy the Restart Session menu item. (This will return
271 * failure if it's already absent, as it will be the very first
272 * time we call this function. We ignore that, because as long
273 * as the menu item ends up not being there, we don't care
274 * whether it was us who removed it or not!)
276 for (i
= 0; i
< lenof(popup_menus
); i
++) {
277 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
280 must_close_session
= FALSE
;
281 session_closed
= FALSE
;
284 static void close_session(void)
289 session_closed
= TRUE
;
290 sprintf(morestuff
, "%.70s (inactive)", appname
);
291 set_icon(NULL
, morestuff
);
292 set_title(NULL
, morestuff
);
299 back
->free(backhandle
);
302 term_provide_resize_fn(term
, NULL
, NULL
);
303 update_specials_menu(NULL
);
307 * Show the Restart Session menu item. Do a precautionary
308 * delete first to ensure we never end up with more than one.
310 for (i
= 0; i
< lenof(popup_menus
); i
++) {
311 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
312 InsertMenu(popup_menus
[i
].menu
, IDM_DUPSESS
, MF_BYCOMMAND
| MF_ENABLED
,
313 IDM_RESTART
, "&Restart Session");
317 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
321 int guess_width
, guess_height
;
325 flags
= FLAG_VERBOSE
| FLAG_INTERACTIVE
;
329 InitCommonControls();
331 /* Ensure a Maximize setting in Explorer doesn't maximise the
337 char *str
= dupprintf("%s Fatal Error", appname
);
338 MessageBox(NULL
, "Windows refuses to report a version",
339 str
, MB_OK
| MB_ICONEXCLAMATION
);
345 * If we're running a version of Windows that doesn't support
346 * WM_MOUSEWHEEL, find out what message number we should be
349 if (osVersion
.dwMajorVersion
< 4 ||
350 (osVersion
.dwMajorVersion
== 4 &&
351 osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
))
352 wm_mousewheel
= RegisterWindowMessage("MSWHEEL_ROLLMSG");
359 * Process the command line.
364 /* By default, we bring up the config dialog, rather than launching
365 * a session. This gets set to TRUE if something happens to change
366 * that (e.g., a hostname is specified on the command-line). */
367 int allow_launch
= FALSE
;
369 default_protocol
= be_default_protocol
;
370 /* Find the appropriate default port. */
372 Backend
*b
= backend_from_proto(default_protocol
);
373 default_port
= 0; /* illegal */
375 default_port
= b
->default_port
;
377 cfg
.logtype
= LGTYP_NONE
;
379 do_defaults(NULL
, &cfg
);
384 * Process a couple of command-line options which are more
385 * easily dealt with before the line is broken up into
386 * words. These are the soon-to-be-defunct @sessionname and
387 * the internal-use-only &sharedmemoryhandle, neither of
388 * which are combined with anything else.
390 while (*p
&& isspace(*p
))
394 while (i
> 1 && isspace(p
[i
- 1]))
397 do_defaults(p
+ 1, &cfg
);
398 if (!cfg_launchable(&cfg
) && !do_config()) {
401 allow_launch
= TRUE
; /* allow it to be launched directly */
402 } else if (*p
== '&') {
404 * An initial & means we've been given a command line
405 * containing the hex value of a HANDLE for a file
406 * mapping object, which we must then extract as a
411 if (sscanf(p
+ 1, "%p", &filemap
) == 1 &&
412 (cp
= MapViewOfFile(filemap
, FILE_MAP_READ
,
413 0, 0, sizeof(Config
))) != NULL
) {
416 CloseHandle(filemap
);
417 } else if (!do_config()) {
423 * Otherwise, break up the command line and deal with
429 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
431 for (i
= 0; i
< argc
; i
++) {
435 ret
= cmdline_process_param(p
, i
+1<argc?argv
[i
+1]:NULL
,
438 cmdline_error("option \"%s\" requires an argument", p
);
439 } else if (ret
== 2) {
440 i
++; /* skip next argument */
441 } else if (ret
== 1) {
442 continue; /* nothing further needs doing */
443 } else if (!strcmp(p
, "-cleanup") ||
444 !strcmp(p
, "-cleanup-during-uninstall")) {
446 * `putty -cleanup'. Remove all registry
447 * entries associated with PuTTY, and also find
448 * and delete the random seed file.
451 /* Are we being invoked from an uninstaller? */
452 if (!strcmp(p
, "-cleanup-during-uninstall")) {
453 s1
= dupprintf("Remove saved sessions and random seed file?\n"
455 "If you hit Yes, ALL Registry entries associated\n"
456 "with %s will be removed, as well as the\n"
457 "random seed file. THIS PROCESS WILL\n"
458 "DESTROY YOUR SAVED SESSIONS.\n"
459 "(This only affects the currently logged-in user.)\n"
461 "If you hit No, uninstallation will proceed, but\n"
462 "saved sessions etc will be left on the machine.",
464 s2
= dupprintf("%s Uninstallation", appname
);
466 s1
= dupprintf("This procedure will remove ALL Registry entries\n"
467 "associated with %s, and will also remove\n"
468 "the random seed file. (This only affects the\n"
469 "currently logged-in user.)\n"
471 "THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
472 "Are you really sure you want to continue?",
474 s2
= dupprintf("%s Warning", appname
);
476 if (message_box(s1
, s2
,
477 MB_YESNO
| MB_ICONWARNING
| MB_DEFBUTTON2
,
478 HELPCTXID(option_cleanup
)) == IDYES
) {
484 } else if (!strcmp(p
, "-pgpfp")) {
487 } else if (*p
!= '-') {
491 * If we already have a host name, treat
492 * this argument as a port number. NB we
493 * have to treat this as a saved -P
494 * argument, so that it will be deferred
495 * until it's a good moment to run it.
497 int ret
= cmdline_process_param("-P", p
, 1, &cfg
);
499 } else if (!strncmp(q
, "telnet:", 7)) {
501 * If the hostname starts with "telnet:",
502 * set the protocol to Telnet and process
503 * the string as a Telnet URL.
508 if (q
[0] == '/' && q
[1] == '/')
510 cfg
.protocol
= PROT_TELNET
;
512 while (*p
&& *p
!= ':' && *p
!= '/')
521 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
522 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
526 * Otherwise, treat this argument as a host
529 while (*p
&& !isspace(*p
))
533 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
534 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
538 cmdline_error("unknown option \"%s\"", p
);
543 cmdline_run_saved(&cfg
);
545 if (loaded_session
|| got_host
)
548 if ((!allow_launch
|| !cfg_launchable(&cfg
)) && !do_config()) {
553 * Trim leading whitespace off the hostname if it's there.
556 int space
= strspn(cfg
.host
, " \t");
557 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
560 /* See if host is of the form user@host */
561 if (cfg
.host
[0] != '\0') {
562 char *atsign
= strrchr(cfg
.host
, '@');
563 /* Make sure we're not overflowing the user field */
565 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
566 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
567 cfg
.username
[atsign
- cfg
.host
] = '\0';
569 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
574 * Trim a colon suffix off the hostname if it's there. In
575 * order to protect IPv6 address literals against this
576 * treatment, we do not do this if there's _more_ than one
580 char *c
= strchr(cfg
.host
, ':');
583 char *d
= strchr(c
+1, ':');
590 * Remove any remaining whitespace from the hostname.
594 while (cfg
.host
[p2
] != '\0') {
595 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
596 cfg
.host
[p1
] = cfg
.host
[p2
];
607 wndclass
.lpfnWndProc
= WndProc
;
608 wndclass
.cbClsExtra
= 0;
609 wndclass
.cbWndExtra
= 0;
610 wndclass
.hInstance
= inst
;
611 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
612 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
613 wndclass
.hbrBackground
= NULL
;
614 wndclass
.lpszMenuName
= NULL
;
615 wndclass
.lpszClassName
= appname
;
617 RegisterClass(&wndclass
);
620 memset(&ucsdata
, 0, sizeof(ucsdata
));
625 * Guess some defaults for the window size. This all gets
626 * updated later, so we don't really care too much. However, we
627 * do want the font width/height guesses to correspond to a
628 * large font rather than a small one...
635 guess_width
= extra_width
+ font_width
* cfg
.width
;
636 guess_height
= extra_height
+ font_height
* cfg
.height
;
639 get_fullscreen_rect(&r
);
640 if (guess_width
> r
.right
- r
.left
)
641 guess_width
= r
.right
- r
.left
;
642 if (guess_height
> r
.bottom
- r
.top
)
643 guess_height
= r
.bottom
- r
.top
;
647 int winmode
= WS_OVERLAPPEDWINDOW
| WS_VSCROLL
;
650 winmode
&= ~(WS_VSCROLL
);
651 if (cfg
.resize_action
== RESIZE_DISABLED
)
652 winmode
&= ~(WS_THICKFRAME
| WS_MAXIMIZEBOX
);
654 exwinmode
|= WS_EX_TOPMOST
;
656 exwinmode
|= WS_EX_CLIENTEDGE
;
657 hwnd
= CreateWindowEx(exwinmode
, appname
, appname
,
658 winmode
, CW_USEDEFAULT
, CW_USEDEFAULT
,
659 guess_width
, guess_height
,
660 NULL
, NULL
, inst
, NULL
);
664 * Initialise the terminal. (We have to do this _after_
665 * creating the window, since the terminal is the first thing
666 * which will call schedule_timer(), which will in turn call
667 * timer_change_notify() which will expect hwnd to exist.)
669 term
= term_init(&cfg
, &ucsdata
, NULL
);
670 logctx
= log_init(NULL
, &cfg
);
671 term_provide_logctx(term
, logctx
);
672 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
675 * Initialise the fonts, simultaneously correcting the guesses
676 * for font_{width,height}.
681 * Correct the guesses for extra_{width,height}.
685 GetWindowRect(hwnd
, &wr
);
686 GetClientRect(hwnd
, &cr
);
687 offset_width
= offset_height
= cfg
.window_border
;
688 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
689 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
693 * Resize the window, now we know what size we _really_ want it
696 guess_width
= extra_width
+ font_width
* term
->cols
;
697 guess_height
= extra_height
+ font_height
* term
->rows
;
698 SetWindowPos(hwnd
, NULL
, 0, 0, guess_width
, guess_height
,
699 SWP_NOMOVE
| SWP_NOREDRAW
| SWP_NOZORDER
);
702 * Set up a caret bitmap, with no content.
706 int size
= (font_width
+ 15) / 16 * 2 * font_height
;
707 bits
= snewn(size
, char);
708 memset(bits
, 0, size
);
709 caretbm
= CreateBitmap(font_width
, font_height
, 1, 1, bits
);
712 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
715 * Initialise the scroll bar.
720 si
.cbSize
= sizeof(si
);
721 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
723 si
.nMax
= term
->rows
- 1;
724 si
.nPage
= term
->rows
;
726 SetScrollInfo(hwnd
, SB_VERT
, &si
, FALSE
);
730 * Prepare the mouse handler.
732 lastact
= MA_NOTHING
;
733 lastbtn
= MBT_NOTHING
;
734 dbltime
= GetDoubleClickTime();
737 * Set up the session-control options on the system menu.
744 popup_menus
[SYSMENU
].menu
= GetSystemMenu(hwnd
, FALSE
);
745 popup_menus
[CTXMENU
].menu
= CreatePopupMenu();
746 AppendMenu(popup_menus
[CTXMENU
].menu
, MF_ENABLED
, IDM_PASTE
, "&Paste");
748 savedsess_menu
= CreateMenu();
749 get_sesslist(&sesslist
, TRUE
);
750 update_savedsess_menu();
752 for (j
= 0; j
< lenof(popup_menus
); j
++) {
753 m
= popup_menus
[j
].menu
;
755 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
756 AppendMenu(m
, MF_ENABLED
, IDM_SHOWLOG
, "&Event Log");
757 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
758 AppendMenu(m
, MF_ENABLED
, IDM_NEWSESS
, "Ne&w Session...");
759 AppendMenu(m
, MF_ENABLED
, IDM_DUPSESS
, "&Duplicate Session");
760 AppendMenu(m
, MF_POPUP
| MF_ENABLED
, (UINT
) savedsess_menu
,
762 AppendMenu(m
, MF_ENABLED
, IDM_RECONF
, "Chan&ge Settings...");
763 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
764 AppendMenu(m
, MF_ENABLED
, IDM_COPYALL
, "C&opy All to Clipboard");
765 AppendMenu(m
, MF_ENABLED
, IDM_CLRSB
, "C&lear Scrollback");
766 AppendMenu(m
, MF_ENABLED
, IDM_RESET
, "Rese&t Terminal");
767 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
768 AppendMenu(m
, (cfg
.resize_action
== RESIZE_DISABLED
) ?
769 MF_GRAYED
: MF_ENABLED
, IDM_FULLSCREEN
, "&Full Screen");
770 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
772 AppendMenu(m
, MF_ENABLED
, IDM_HELP
, "&Help");
773 str
= dupprintf("&About %s", appname
);
774 AppendMenu(m
, MF_ENABLED
, IDM_ABOUT
, str
);
782 * Set up the initial input locale.
784 set_input_locale(GetKeyboardLayout(0));
787 * Finally show the window!
789 ShowWindow(hwnd
, show
);
790 SetForegroundWindow(hwnd
);
793 * Set the palette up.
799 term_set_focus(term
, GetForegroundWindow() == hwnd
);
806 handles
= handle_get_events(&nhandles
);
808 n
= MsgWaitForMultipleObjects(nhandles
, handles
, FALSE
, INFINITE
,
811 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
812 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
814 if (must_close_session
)
819 while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
)) {
820 if (msg
.message
== WM_QUIT
)
821 goto finished
; /* two-level break */
823 if (!(IsWindow(logbox
) && IsDialogMessage(logbox
, &msg
)))
824 DispatchMessage(&msg
);
825 /* Send the paste buffer if there's anything to send */
827 /* If there's nothing new in the queue then we can do everything
828 * we've delayed, reading the socket, writing, and repainting
831 if (must_close_session
)
835 /* The messages seem unreliable; especially if we're being tricky */
836 term_set_focus(term
, GetForegroundWindow() == hwnd
);
838 if (pending_netevent
)
839 enact_pending_netevent();
841 net_pending_errors();
845 cleanup_exit(msg
.wParam
); /* this doesn't return... */
846 return msg
.wParam
; /* ... but optimiser doesn't know */
852 void cleanup_exit(int code
)
863 if (cfg
.protocol
== PROT_SSH
) {
875 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
877 char *do_select(SOCKET skt
, int startup
)
882 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
883 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
888 return "do_select(): internal error (hwnd==NULL)";
889 if (p_WSAAsyncSelect(skt
, hwnd
, msg
, events
) == SOCKET_ERROR
) {
890 switch (p_WSAGetLastError()) {
892 return "Network is down";
894 return "WSAAsyncSelect(): unknown error";
901 * Refresh the saved-session submenu from `sesslist'.
903 static void update_savedsess_menu(void)
906 while (DeleteMenu(savedsess_menu
, 0, MF_BYPOSITION
)) ;
907 /* skip sesslist.sessions[0] == Default Settings */
909 i
< ((sesslist
.nsessions
<= MENU_SAVED_MAX
+1) ? sesslist
.nsessions
912 AppendMenu(savedsess_menu
, MF_ENABLED
,
913 IDM_SAVED_MIN
+ (i
-1)*MENU_SAVED_STEP
,
914 sesslist
.sessions
[i
]);
915 if (sesslist
.nsessions
<= 1)
916 AppendMenu(savedsess_menu
, MF_GRAYED
, IDM_SAVED_MIN
, "(No sessions)");
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 */
2118 for (i
= 0; i
< lenof(popup_menus
); i
++)
2119 EnableMenuItem(popup_menus
[i
].menu
, IDM_FULLSCREEN
,
2121 (cfg
.resize_action
== RESIZE_DISABLED
)
2122 ? MF_GRAYED
: MF_ENABLED
);
2123 /* Gracefully unzoom if necessary */
2124 if (IsZoomed(hwnd
) &&
2125 (cfg
.resize_action
== RESIZE_DISABLED
)) {
2126 ShowWindow(hwnd
, SW_RESTORE
);
2130 /* Pass new config data to the logging module */
2131 log_reconfig(logctx
, &cfg
);
2135 * Flush the line discipline's edit buffer in the
2136 * case where local editing has just been disabled.
2139 ldisc_send(ldisc
, NULL
, 0, 0);
2147 /* Pass new config data to the terminal */
2148 term_reconfig(term
, &cfg
);
2150 /* Pass new config data to the back end */
2152 back
->reconfig(backhandle
, &cfg
);
2154 /* Screen size changed ? */
2155 if (cfg
.height
!= prev_cfg
.height
||
2156 cfg
.width
!= prev_cfg
.width
||
2157 cfg
.savelines
!= prev_cfg
.savelines
||
2158 cfg
.resize_action
== RESIZE_FONT
||
2159 (cfg
.resize_action
== RESIZE_EITHER
&& IsZoomed(hwnd
)) ||
2160 cfg
.resize_action
== RESIZE_DISABLED
)
2161 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2163 /* Enable or disable the scroll bar, etc */
2165 LONG nflg
, flag
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
2166 LONG nexflag
, exflag
=
2167 GetWindowLongPtr(hwnd
, GWL_EXSTYLE
);
2170 if (cfg
.alwaysontop
!= prev_cfg
.alwaysontop
) {
2171 if (cfg
.alwaysontop
) {
2172 nexflag
|= WS_EX_TOPMOST
;
2173 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
2174 SWP_NOMOVE
| SWP_NOSIZE
);
2176 nexflag
&= ~(WS_EX_TOPMOST
);
2177 SetWindowPos(hwnd
, HWND_NOTOPMOST
, 0, 0, 0, 0,
2178 SWP_NOMOVE
| SWP_NOSIZE
);
2181 if (cfg
.sunken_edge
)
2182 nexflag
|= WS_EX_CLIENTEDGE
;
2184 nexflag
&= ~(WS_EX_CLIENTEDGE
);
2187 if (is_full_screen() ?
2188 cfg
.scrollbar_in_fullscreen
: cfg
.scrollbar
)
2191 nflg
&= ~WS_VSCROLL
;
2193 if (cfg
.resize_action
== RESIZE_DISABLED
||
2195 nflg
&= ~WS_THICKFRAME
;
2197 nflg
|= WS_THICKFRAME
;
2199 if (cfg
.resize_action
== RESIZE_DISABLED
)
2200 nflg
&= ~WS_MAXIMIZEBOX
;
2202 nflg
|= WS_MAXIMIZEBOX
;
2204 if (nflg
!= flag
|| nexflag
!= exflag
) {
2206 SetWindowLongPtr(hwnd
, GWL_STYLE
, nflg
);
2207 if (nexflag
!= exflag
)
2208 SetWindowLongPtr(hwnd
, GWL_EXSTYLE
, nexflag
);
2210 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
2211 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
2212 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
2220 if (cfg
.resize_action
== RESIZE_DISABLED
&& IsZoomed(hwnd
)) {
2225 set_title(NULL
, cfg
.wintitle
);
2226 if (IsIconic(hwnd
)) {
2228 cfg
.win_name_always ? window_name
:
2232 if (strcmp(cfg
.font
.name
, prev_cfg
.font
.name
) != 0 ||
2233 strcmp(cfg
.line_codepage
, prev_cfg
.line_codepage
) != 0 ||
2234 cfg
.font
.isbold
!= prev_cfg
.font
.isbold
||
2235 cfg
.font
.height
!= prev_cfg
.font
.height
||
2236 cfg
.font
.charset
!= prev_cfg
.font
.charset
||
2237 cfg
.font_quality
!= prev_cfg
.font_quality
||
2238 cfg
.vtmode
!= prev_cfg
.vtmode
||
2239 cfg
.bold_colour
!= prev_cfg
.bold_colour
||
2240 cfg
.resize_action
== RESIZE_DISABLED
||
2241 cfg
.resize_action
== RESIZE_EITHER
||
2242 (cfg
.resize_action
!= prev_cfg
.resize_action
))
2245 InvalidateRect(hwnd
, NULL
, TRUE
);
2246 reset_window(init_lvl
);
2247 net_pending_errors();
2254 request_paste(NULL
);
2260 term_pwron(term
, TRUE
);
2262 ldisc_send(ldisc
, NULL
, 0, 0);
2268 launch_help(hwnd
, NULL
);
2272 * We get this if the System menu has been activated
2279 * We get this if the System menu has been activated
2280 * using the keyboard. This might happen from within
2281 * TranslateKey, in which case it really wants to be
2282 * followed by a `space' character to actually _bring
2283 * the menu up_ rather than just sitting there in
2284 * `ready to appear' state.
2286 show_mouseptr(1); /* make sure pointer is visible */
2288 PostMessage(hwnd
, WM_CHAR
, ' ', 0);
2290 case IDM_FULLSCREEN
:
2294 if (wParam
>= IDM_SAVED_MIN
&& wParam
< IDM_SAVED_MAX
) {
2295 SendMessage(hwnd
, WM_SYSCOMMAND
, IDM_SAVEDSESS
, wParam
);
2297 if (wParam
>= IDM_SPECIAL_MIN
&& wParam
<= IDM_SPECIAL_MAX
) {
2298 int i
= (wParam
- IDM_SPECIAL_MIN
) / 0x10;
2300 * Ensure we haven't been sent a bogus SYSCOMMAND
2301 * which would cause us to reference invalid memory
2302 * and crash. Perhaps I'm just too paranoid here.
2304 if (i
>= n_specials
)
2307 back
->special(backhandle
, specials
[i
].code
);
2308 net_pending_errors();
2313 #define X_POS(l) ((int)(short)LOWORD(l))
2314 #define Y_POS(l) ((int)(short)HIWORD(l))
2316 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2317 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2318 case WM_LBUTTONDOWN
:
2319 case WM_MBUTTONDOWN
:
2320 case WM_RBUTTONDOWN
:
2324 if (message
== WM_RBUTTONDOWN
&&
2325 ((wParam
& MK_CONTROL
) || (cfg
.mouse_is_xterm
== 2))) {
2328 show_mouseptr(1); /* make sure pointer is visible */
2329 GetCursorPos(&cursorpos
);
2330 TrackPopupMenu(popup_menus
[CTXMENU
].menu
,
2331 TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
2332 cursorpos
.x
, cursorpos
.y
,
2340 case WM_LBUTTONDOWN
:
2342 wParam
|= MK_LBUTTON
;
2345 case WM_MBUTTONDOWN
:
2346 button
= MBT_MIDDLE
;
2347 wParam
|= MK_MBUTTON
;
2350 case WM_RBUTTONDOWN
:
2352 wParam
|= MK_RBUTTON
;
2357 wParam
&= ~MK_LBUTTON
;
2361 button
= MBT_MIDDLE
;
2362 wParam
&= ~MK_MBUTTON
;
2367 wParam
&= ~MK_RBUTTON
;
2371 button
= press
= 0; /* shouldn't happen */
2375 * Special case: in full-screen mode, if the left
2376 * button is clicked in the very top left corner of the
2377 * window, we put up the System menu instead of doing
2381 char mouse_on_hotspot
= 0;
2390 mon
= MonitorFromPoint(pt
, MONITOR_DEFAULTTONULL
);
2393 mi
.cbSize
= sizeof(MONITORINFO
);
2394 GetMonitorInfo(mon
, &mi
);
2396 if (mi
.rcMonitor
.left
== pt
.x
&&
2397 mi
.rcMonitor
.top
== pt
.y
) {
2398 mouse_on_hotspot
= 1;
2403 if (pt
.x
== 0 && pt
.y
== 0) {
2404 mouse_on_hotspot
= 1;
2407 if (is_full_screen() && press
&&
2408 button
== MBT_LEFT
&& mouse_on_hotspot
) {
2409 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MOUSEMENU
,
2410 MAKELPARAM(pt
.x
, pt
.y
));
2417 TO_CHR_X(X_POS(lParam
)), TO_CHR_Y(Y_POS(lParam
)),
2418 wParam
& MK_SHIFT
, wParam
& MK_CONTROL
,
2422 term_mouse(term
, button
, translate_button(button
), MA_RELEASE
,
2423 TO_CHR_X(X_POS(lParam
)),
2424 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2425 wParam
& MK_CONTROL
, is_alt_pressed());
2426 if (!(wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
)))
2434 * Windows seems to like to occasionally send MOUSEMOVE
2435 * events even if the mouse hasn't moved. Don't unhide
2436 * the mouse pointer in this case.
2438 static WPARAM wp
= 0;
2439 static LPARAM lp
= 0;
2440 if (wParam
!= wp
|| lParam
!= lp
||
2441 last_mousemove
!= WM_MOUSEMOVE
) {
2443 wp
= wParam
; lp
= lParam
;
2444 last_mousemove
= WM_MOUSEMOVE
;
2448 * Add the mouse position and message time to the random
2451 noise_ultralight(lParam
);
2453 if (wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
) &&
2454 GetCapture() == hwnd
) {
2456 if (wParam
& MK_LBUTTON
)
2458 else if (wParam
& MK_MBUTTON
)
2462 term_mouse(term
, b
, translate_button(b
), MA_DRAG
,
2463 TO_CHR_X(X_POS(lParam
)),
2464 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2465 wParam
& MK_CONTROL
, is_alt_pressed());
2468 case WM_NCMOUSEMOVE
:
2470 static WPARAM wp
= 0;
2471 static LPARAM lp
= 0;
2472 if (wParam
!= wp
|| lParam
!= lp
||
2473 last_mousemove
!= WM_NCMOUSEMOVE
) {
2475 wp
= wParam
; lp
= lParam
;
2476 last_mousemove
= WM_NCMOUSEMOVE
;
2479 noise_ultralight(lParam
);
2481 case WM_IGNORE_CLIP
:
2482 ignore_clip
= wParam
; /* don't panic on DESTROYCLIPBOARD */
2484 case WM_DESTROYCLIPBOARD
:
2486 term_deselect(term
);
2487 ignore_clip
= FALSE
;
2494 hdc
= BeginPaint(hwnd
, &p
);
2496 SelectPalette(hdc
, pal
, TRUE
);
2497 RealizePalette(hdc
);
2501 * We have to be careful about term_paint(). It will
2502 * set a bunch of character cells to INVALID and then
2503 * call do_paint(), which will redraw those cells and
2504 * _then mark them as done_. This may not be accurate:
2505 * when painting in WM_PAINT context we are restricted
2506 * to the rectangle which has just been exposed - so if
2507 * that only covers _part_ of a character cell and the
2508 * rest of it was already visible, that remainder will
2509 * not be redrawn at all. Accordingly, we must not
2510 * paint any character cell in a WM_PAINT context which
2511 * already has a pending update due to terminal output.
2512 * The simplest solution to this - and many, many
2513 * thanks to Hung-Te Lin for working all this out - is
2514 * not to do any actual painting at _all_ if there's a
2515 * pending terminal update: just mark the relevant
2516 * character cells as INVALID and wait for the
2517 * scheduled full update to sort it out.
2519 * I have a suspicion this isn't the _right_ solution.
2520 * An alternative approach would be to have terminal.c
2521 * separately track what _should_ be on the terminal
2522 * screen and what _is_ on the terminal screen, and
2523 * have two completely different types of redraw (one
2524 * for full updates, which syncs the former with the
2525 * terminal itself, and one for WM_PAINT which syncs
2526 * the latter with the former); yet another possibility
2527 * would be to have the Windows front end do what the
2528 * GTK one already does, and maintain a bitmap of the
2529 * current terminal appearance so that WM_PAINT becomes
2530 * completely trivial. However, this should do for now.
2532 term_paint(term
, hdc
,
2533 (p
.rcPaint
.left
-offset_width
)/font_width
,
2534 (p
.rcPaint
.top
-offset_height
)/font_height
,
2535 (p
.rcPaint
.right
-offset_width
-1)/font_width
,
2536 (p
.rcPaint
.bottom
-offset_height
-1)/font_height
,
2537 !term
->window_update_pending
);
2540 p
.rcPaint
.left
< offset_width
||
2541 p
.rcPaint
.top
< offset_height
||
2542 p
.rcPaint
.right
>= offset_width
+ font_width
*term
->cols
||
2543 p
.rcPaint
.bottom
>= offset_height
+ font_height
*term
->rows
)
2545 HBRUSH fillcolour
, oldbrush
;
2547 fillcolour
= CreateSolidBrush (
2548 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2549 oldbrush
= SelectObject(hdc
, fillcolour
);
2550 edge
= CreatePen(PS_SOLID
, 0,
2551 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2552 oldpen
= SelectObject(hdc
, edge
);
2555 * Jordan Russell reports that this apparently
2556 * ineffectual IntersectClipRect() call masks a
2557 * Windows NT/2K bug causing strange display
2558 * problems when the PuTTY window is taller than
2559 * the primary monitor. It seems harmless enough...
2561 IntersectClipRect(hdc
,
2562 p
.rcPaint
.left
, p
.rcPaint
.top
,
2563 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2565 ExcludeClipRect(hdc
,
2566 offset_width
, offset_height
,
2567 offset_width
+font_width
*term
->cols
,
2568 offset_height
+font_height
*term
->rows
);
2570 Rectangle(hdc
, p
.rcPaint
.left
, p
.rcPaint
.top
,
2571 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2573 /* SelectClipRgn(hdc, NULL); */
2575 SelectObject(hdc
, oldbrush
);
2576 DeleteObject(fillcolour
);
2577 SelectObject(hdc
, oldpen
);
2580 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
2581 SelectObject(hdc
, GetStockObject(WHITE_PEN
));
2587 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2588 * but the only one that's likely to try to overload us is FD_READ.
2589 * This means buffering just one is fine.
2591 if (pending_netevent
)
2592 enact_pending_netevent();
2594 pending_netevent
= TRUE
;
2595 pend_netevent_wParam
= wParam
;
2596 pend_netevent_lParam
= lParam
;
2597 if (WSAGETSELECTEVENT(lParam
) != FD_READ
)
2598 enact_pending_netevent();
2600 net_pending_errors();
2603 term_set_focus(term
, TRUE
);
2604 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
2606 flash_window(0); /* stop */
2612 term_set_focus(term
, FALSE
);
2614 caret_x
= caret_y
= -1; /* ensure caret is replaced next time */
2617 case WM_ENTERSIZEMOVE
:
2618 #ifdef RDB_DEBUG_PATCH
2619 debug((27, "WM_ENTERSIZEMOVE"));
2623 need_backend_resize
= FALSE
;
2625 case WM_EXITSIZEMOVE
:
2628 #ifdef RDB_DEBUG_PATCH
2629 debug((27, "WM_EXITSIZEMOVE"));
2631 if (need_backend_resize
) {
2632 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2633 InvalidateRect(hwnd
, NULL
, TRUE
);
2638 * This does two jobs:
2639 * 1) Keep the sizetip uptodate
2640 * 2) Make sure the window size is _stepped_ in units of the font size.
2642 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2643 int width
, height
, w
, h
, ew
, eh
;
2644 LPRECT r
= (LPRECT
) lParam
;
2646 if ( !need_backend_resize
&& cfg
.resize_action
== RESIZE_EITHER
&&
2647 (cfg
.height
!= term
->rows
|| cfg
.width
!= term
->cols
)) {
2649 * Great! It seems that both the terminal size and the
2650 * font size have been changed and the user is now dragging.
2652 * It will now be difficult to get back to the configured
2655 * This would be easier but it seems to be too confusing.
2657 term_size(term, cfg.height, cfg.width, cfg.savelines);
2660 cfg
.height
=term
->rows
; cfg
.width
=term
->cols
;
2662 InvalidateRect(hwnd
, NULL
, TRUE
);
2663 need_backend_resize
= TRUE
;
2666 width
= r
->right
- r
->left
- extra_width
;
2667 height
= r
->bottom
- r
->top
- extra_height
;
2668 w
= (width
+ font_width
/ 2) / font_width
;
2671 h
= (height
+ font_height
/ 2) / font_height
;
2674 UpdateSizeTip(hwnd
, w
, h
);
2675 ew
= width
- w
* font_width
;
2676 eh
= height
- h
* font_height
;
2678 if (wParam
== WMSZ_LEFT
||
2679 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2685 if (wParam
== WMSZ_TOP
||
2686 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2696 int width
, height
, w
, h
, rv
= 0;
2697 int ex_width
= extra_width
+ (cfg
.window_border
- offset_width
) * 2;
2698 int ex_height
= extra_height
+ (cfg
.window_border
- offset_height
) * 2;
2699 LPRECT r
= (LPRECT
) lParam
;
2701 width
= r
->right
- r
->left
- ex_width
;
2702 height
= r
->bottom
- r
->top
- ex_height
;
2704 w
= (width
+ term
->cols
/2)/term
->cols
;
2705 h
= (height
+ term
->rows
/2)/term
->rows
;
2706 if ( r
->right
!= r
->left
+ w
*term
->cols
+ ex_width
)
2709 if (wParam
== WMSZ_LEFT
||
2710 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2711 r
->left
= r
->right
- w
*term
->cols
- ex_width
;
2713 r
->right
= r
->left
+ w
*term
->cols
+ ex_width
;
2715 if (r
->bottom
!= r
->top
+ h
*term
->rows
+ ex_height
)
2718 if (wParam
== WMSZ_TOP
||
2719 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2720 r
->top
= r
->bottom
- h
*term
->rows
- ex_height
;
2722 r
->bottom
= r
->top
+ h
*term
->rows
+ ex_height
;
2726 /* break; (never reached) */
2727 case WM_FULLSCR_ON_MAX
:
2728 fullscr_on_max
= TRUE
;
2731 sys_cursor_update();
2734 #ifdef RDB_DEBUG_PATCH
2735 debug((27, "WM_SIZE %s (%d,%d)",
2736 (wParam
== SIZE_MINIMIZED
) ?
"SIZE_MINIMIZED":
2737 (wParam
== SIZE_MAXIMIZED
) ?
"SIZE_MAXIMIZED":
2738 (wParam
== SIZE_RESTORED
&& resizing
) ?
"to":
2739 (wParam
== SIZE_RESTORED
) ?
"SIZE_RESTORED":
2741 LOWORD(lParam
), HIWORD(lParam
)));
2743 if (wParam
== SIZE_MINIMIZED
)
2745 cfg
.win_name_always ? window_name
: icon_name
);
2746 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
)
2747 SetWindowText(hwnd
, window_name
);
2748 if (wParam
== SIZE_RESTORED
)
2749 clear_full_screen();
2750 if (wParam
== SIZE_MAXIMIZED
&& fullscr_on_max
) {
2751 fullscr_on_max
= FALSE
;
2755 if (cfg
.resize_action
== RESIZE_DISABLED
) {
2756 /* A resize, well it better be a minimize. */
2760 int width
, height
, w
, h
;
2762 width
= LOWORD(lParam
);
2763 height
= HIWORD(lParam
);
2766 if (wParam
== SIZE_MAXIMIZED
&& !was_zoomed
) {
2768 prev_rows
= term
->rows
;
2769 prev_cols
= term
->cols
;
2770 if (cfg
.resize_action
== RESIZE_TERM
) {
2771 w
= width
/ font_width
;
2773 h
= height
/ font_height
;
2776 term_size(term
, h
, w
, cfg
.savelines
);
2779 } else if (wParam
== SIZE_RESTORED
&& was_zoomed
) {
2781 if (cfg
.resize_action
== RESIZE_TERM
)
2782 term_size(term
, prev_rows
, prev_cols
, cfg
.savelines
);
2783 if (cfg
.resize_action
!= RESIZE_FONT
)
2788 /* This is an unexpected resize, these will normally happen
2789 * if the window is too large. Probably either the user
2790 * selected a huge font or the screen size has changed.
2792 * This is also called with minimize.
2794 else reset_window(-1);
2798 * Don't call back->size in mid-resize. (To prevent
2799 * massive numbers of resize events getting sent
2800 * down the connection during an NT opaque drag.)
2803 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2804 need_backend_resize
= TRUE
;
2805 w
= (width
-cfg
.window_border
*2) / font_width
;
2807 h
= (height
-cfg
.window_border
*2) / font_height
;
2816 sys_cursor_update();
2819 switch (LOWORD(wParam
)) {
2821 term_scroll(term
, -1, 0);
2824 term_scroll(term
, +1, 0);
2827 term_scroll(term
, 0, +1);
2830 term_scroll(term
, 0, -1);
2833 term_scroll(term
, 0, +term
->rows
/ 2);
2836 term_scroll(term
, 0, -term
->rows
/ 2);
2838 case SB_THUMBPOSITION
:
2840 term_scroll(term
, 1, HIWORD(wParam
));
2844 case WM_PALETTECHANGED
:
2845 if ((HWND
) wParam
!= hwnd
&& pal
!= NULL
) {
2846 HDC hdc
= get_ctx(NULL
);
2848 if (RealizePalette(hdc
) > 0)
2854 case WM_QUERYNEWPALETTE
:
2856 HDC hdc
= get_ctx(NULL
);
2858 if (RealizePalette(hdc
) > 0)
2870 * Add the scan code and keypress timing to the random
2873 noise_ultralight(lParam
);
2876 * We don't do TranslateMessage since it disassociates the
2877 * resulting CHAR message from the KEYDOWN that sparked it,
2878 * which we occasionally don't want. Instead, we process
2879 * KEYDOWN, and call the Win32 translator functions so that
2880 * we get the translations under _our_ control.
2883 unsigned char buf
[20];
2886 if (wParam
== VK_PROCESSKEY
) { /* IME PROCESS key */
2887 if (message
== WM_KEYDOWN
) {
2890 m
.message
= WM_KEYDOWN
;
2892 m
.lParam
= lParam
& 0xdfff;
2893 TranslateMessage(&m
);
2894 } else break; /* pass to Windows for default processing */
2896 len
= TranslateKey(message
, wParam
, lParam
, buf
);
2898 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2902 * Interrupt an ongoing paste. I'm not sure
2903 * this is sensible, but for the moment it's
2904 * preferable to having to faff about buffering
2910 * We need not bother about stdin backlogs
2911 * here, because in GUI PuTTY we can't do
2912 * anything about it anyway; there's no means
2913 * of asking Windows to hold off on KEYDOWN
2914 * messages. We _have_ to buffer everything
2917 term_seen_key_event(term
);
2919 ldisc_send(ldisc
, buf
, len
, 1);
2924 net_pending_errors();
2926 case WM_INPUTLANGCHANGE
:
2927 /* wParam == Font number */
2928 /* lParam == Locale */
2929 set_input_locale((HKL
)lParam
);
2930 sys_cursor_update();
2932 case WM_IME_STARTCOMPOSITION
:
2934 HIMC hImc
= ImmGetContext(hwnd
);
2935 ImmSetCompositionFont(hImc
, &lfont
);
2936 ImmReleaseContext(hwnd
, hImc
);
2939 case WM_IME_COMPOSITION
:
2945 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
||
2946 osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) break; /* no Unicode */
2948 if ((lParam
& GCS_RESULTSTR
) == 0) /* Composition unfinished. */
2949 break; /* fall back to DefWindowProc */
2951 hIMC
= ImmGetContext(hwnd
);
2952 n
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
2956 buff
= snewn(n
, char);
2957 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, buff
, n
);
2959 * Jaeyoun Chung reports that Korean character
2960 * input doesn't work correctly if we do a single
2961 * luni_send() covering the whole of buff. So
2962 * instead we luni_send the characters one by one.
2964 term_seen_key_event(term
);
2965 for (i
= 0; i
< n
; i
+= 2) {
2967 luni_send(ldisc
, (unsigned short *)(buff
+i
), 1, 1);
2971 ImmReleaseContext(hwnd
, hIMC
);
2976 if (wParam
& 0xFF00) {
2977 unsigned char buf
[2];
2980 buf
[0] = wParam
>> 8;
2981 term_seen_key_event(term
);
2983 lpage_send(ldisc
, kbd_codepage
, buf
, 2, 1);
2985 char c
= (unsigned char) wParam
;
2986 term_seen_key_event(term
);
2988 lpage_send(ldisc
, kbd_codepage
, &c
, 1, 1);
2994 * Nevertheless, we are prepared to deal with WM_CHAR
2995 * messages, should they crop up. So if someone wants to
2996 * post the things to us as part of a macro manoeuvre,
2997 * we're ready to cope.
3000 char c
= (unsigned char)wParam
;
3001 term_seen_key_event(term
);
3003 lpage_send(ldisc
, CP_ACP
, &c
, 1, 1);
3006 case WM_SYSCOLORCHANGE
:
3007 if (cfg
.system_colour
) {
3008 /* Refresh palette from system colours. */
3009 /* XXX actually this zaps the entire palette. */
3012 /* Force a repaint of the terminal window. */
3013 term_invalidate(term
);
3016 case WM_AGENT_CALLBACK
:
3018 struct agent_callback
*c
= (struct agent_callback
*)lParam
;
3019 c
->callback(c
->callback_ctx
, c
->data
, c
->len
);
3023 case WM_GOT_CLIPDATA
:
3024 if (process_clipdata((HGLOBAL
)lParam
, wParam
))
3025 term_do_paste(term
);
3028 if (message
== wm_mousewheel
|| message
== WM_MOUSEWHEEL
) {
3029 int shift_pressed
=0, control_pressed
=0;
3031 if (message
== WM_MOUSEWHEEL
) {
3032 wheel_accumulator
+= (short)HIWORD(wParam
);
3033 shift_pressed
=LOWORD(wParam
) & MK_SHIFT
;
3034 control_pressed
=LOWORD(wParam
) & MK_CONTROL
;
3037 wheel_accumulator
+= (int)wParam
;
3038 if (GetKeyboardState(keys
)!=0) {
3039 shift_pressed
=keys
[VK_SHIFT
]&0x80;
3040 control_pressed
=keys
[VK_CONTROL
]&0x80;
3044 /* process events when the threshold is reached */
3045 while (abs(wheel_accumulator
) >= WHEEL_DELTA
) {
3048 /* reduce amount for next time */
3049 if (wheel_accumulator
> 0) {
3051 wheel_accumulator
-= WHEEL_DELTA
;
3052 } else if (wheel_accumulator
< 0) {
3054 wheel_accumulator
+= WHEEL_DELTA
;
3058 if (send_raw_mouse
&&
3059 !(cfg
.mouse_override
&& shift_pressed
)) {
3060 /* Mouse wheel position is in screen coordinates for
3063 p
.x
= X_POS(lParam
); p
.y
= Y_POS(lParam
);
3064 if (ScreenToClient(hwnd
, &p
)) {
3065 /* send a mouse-down followed by a mouse up */
3066 term_mouse(term
, b
, translate_button(b
),
3069 TO_CHR_Y(p
.y
), shift_pressed
,
3070 control_pressed
, is_alt_pressed());
3071 term_mouse(term
, b
, translate_button(b
),
3072 MA_RELEASE
, TO_CHR_X(p
.x
),
3073 TO_CHR_Y(p
.y
), shift_pressed
,
3074 control_pressed
, is_alt_pressed());
3075 } /* else: not sure when this can fail */
3077 /* trigger a scroll */
3078 term_scroll(term
, 0,
3080 -term
->rows
/ 2 : term
->rows
/ 2);
3088 * Any messages we don't process completely above are passed through to
3089 * DefWindowProc() for default processing.
3091 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
3095 * Move the system caret. (We maintain one, even though it's
3096 * invisible, for the benefit of blind people: apparently some
3097 * helper software tracks the system caret, so we should arrange to
3100 void sys_cursor(void *frontend
, int x
, int y
)
3104 if (!term
->has_focus
) return;
3107 * Avoid gratuitously re-updating the cursor position and IMM
3108 * window if there's no actual change required.
3110 cx
= x
* font_width
+ offset_width
;
3111 cy
= y
* font_height
+ offset_height
;
3112 if (cx
== caret_x
&& cy
== caret_y
)
3117 sys_cursor_update();
3120 static void sys_cursor_update(void)
3125 if (!term
->has_focus
) return;
3127 if (caret_x
< 0 || caret_y
< 0)
3130 SetCaretPos(caret_x
, caret_y
);
3132 /* IMM calls on Win98 and beyond only */
3133 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) return; /* 3.11 */
3135 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
&&
3136 osVersion
.dwMinorVersion
== 0) return; /* 95 */
3138 /* we should have the IMM functions */
3139 hIMC
= ImmGetContext(hwnd
);
3140 cf
.dwStyle
= CFS_POINT
;
3141 cf
.ptCurrentPos
.x
= caret_x
;
3142 cf
.ptCurrentPos
.y
= caret_y
;
3143 ImmSetCompositionWindow(hIMC
, &cf
);
3145 ImmReleaseContext(hwnd
, hIMC
);
3149 * Draw a line of text in the window, at given character
3150 * coordinates, in given attributes.
3152 * We are allowed to fiddle with the contents of `text'.
3154 void do_text_internal(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3155 unsigned long attr
, int lattr
)
3158 int nfg
, nbg
, nfont
;
3161 int force_manual_underline
= 0;
3162 int fnt_width
, char_width
;
3163 int text_adjust
= 0;
3164 static int *IpDx
= 0, IpDxLEN
= 0;
3166 lattr
&= LATTR_MODE
;
3168 char_width
= fnt_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3170 if (attr
& ATTR_WIDE
)
3173 if (len
> IpDxLEN
|| IpDx
[0] != char_width
) {
3175 if (len
> IpDxLEN
) {
3177 IpDx
= snewn(len
+ 16, int);
3178 IpDxLEN
= (len
+ 16);
3180 for (i
= 0; i
< IpDxLEN
; i
++)
3181 IpDx
[i
] = char_width
;
3184 /* Only want the left half of double width lines */
3185 if (lattr
!= LATTR_NORM
&& x
*2 >= term
->cols
)
3193 if ((attr
& TATTR_ACTCURS
) && (cfg
.cursor_type
== 0 || term
->big_cursor
)) {
3194 attr
&= ~(ATTR_REVERSE
|ATTR_BLINK
|ATTR_COLOURS
);
3195 if (bold_mode
== BOLD_COLOURS
)
3198 /* cursor fg and bg */
3199 attr
|= (260 << ATTR_FGSHIFT
) | (261 << ATTR_BGSHIFT
);
3203 if (cfg
.vtmode
== VT_POORMAN
&& lattr
!= LATTR_NORM
) {
3204 /* Assume a poorman font is borken in other ways too. */
3214 nfont
|= FONT_WIDE
+ FONT_HIGH
;
3217 if (attr
& ATTR_NARROW
)
3218 nfont
|= FONT_NARROW
;
3220 /* Special hack for the VT100 linedraw glyphs. */
3221 if (text
[0] >= 0x23BA && text
[0] <= 0x23BD) {
3222 switch ((unsigned char) (text
[0])) {
3224 text_adjust
= -2 * font_height
/ 5;
3227 text_adjust
= -1 * font_height
/ 5;
3230 text_adjust
= font_height
/ 5;
3233 text_adjust
= 2 * font_height
/ 5;
3236 if (lattr
== LATTR_TOP
|| lattr
== LATTR_BOT
)
3238 text
[0] = ucsdata
.unitab_xterm
['q'];
3239 if (attr
& ATTR_UNDER
) {
3240 attr
&= ~ATTR_UNDER
;
3241 force_manual_underline
= 1;
3245 /* Anything left as an original character set is unprintable. */
3246 if (DIRECT_CHAR(text
[0])) {
3248 for (i
= 0; i
< len
; i
++)
3253 if ((text
[0] & CSET_MASK
) == CSET_OEMCP
)
3256 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
3257 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
3258 if (bold_mode
== BOLD_FONT
&& (attr
& ATTR_BOLD
))
3260 if (und_mode
== UND_FONT
&& (attr
& ATTR_UNDER
))
3261 nfont
|= FONT_UNDERLINE
;
3262 another_font(nfont
);
3263 if (!fonts
[nfont
]) {
3264 if (nfont
& FONT_UNDERLINE
)
3265 force_manual_underline
= 1;
3266 /* Don't do the same for manual bold, it could be bad news. */
3268 nfont
&= ~(FONT_BOLD
| FONT_UNDERLINE
);
3270 another_font(nfont
);
3272 nfont
= FONT_NORMAL
;
3273 if (attr
& ATTR_REVERSE
) {
3278 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BOLD
)) {
3279 if (nfg
< 16) nfg
|= 8;
3280 else if (nfg
>= 256) nfg
|= 1;
3282 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BLINK
)) {
3283 if (nbg
< 16) nbg
|= 8;
3284 else if (nbg
>= 256) nbg
|= 1;
3288 SelectObject(hdc
, fonts
[nfont
]);
3289 SetTextColor(hdc
, fg
);
3290 SetBkColor(hdc
, bg
);
3291 if (attr
& TATTR_COMBINING
)
3292 SetBkMode(hdc
, TRANSPARENT
);
3294 SetBkMode(hdc
, OPAQUE
);
3297 line_box
.right
= x
+ char_width
* len
;
3298 line_box
.bottom
= y
+ font_height
;
3300 /* Only want the left half of double width lines */
3301 if (line_box
.right
> font_width
*term
->cols
+offset_width
)
3302 line_box
.right
= font_width
*term
->cols
+offset_width
;
3304 /* We're using a private area for direct to font. (512 chars.) */
3305 if (ucsdata
.dbcs_screenfont
&& (text
[0] & CSET_MASK
) == CSET_ACP
) {
3306 /* Ho Hum, dbcs fonts are a PITA! */
3307 /* To display on W9x I have to convert to UCS */
3308 static wchar_t *uni_buf
= 0;
3309 static int uni_len
= 0;
3311 if (len
> uni_len
) {
3314 uni_buf
= snewn(uni_len
, wchar_t);
3317 for(nlen
= mptr
= 0; mptr
<len
; mptr
++) {
3318 uni_buf
[nlen
] = 0xFFFD;
3319 if (IsDBCSLeadByteEx(ucsdata
.font_codepage
, (BYTE
) text
[mptr
])) {
3321 dbcstext
[0] = text
[mptr
] & 0xFF;
3322 dbcstext
[1] = text
[mptr
+1] & 0xFF;
3323 IpDx
[nlen
] += char_width
;
3324 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3325 dbcstext
, 2, uni_buf
+nlen
, 1);
3331 dbcstext
[0] = text
[mptr
] & 0xFF;
3332 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3333 dbcstext
, 1, uni_buf
+nlen
, 1);
3341 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3342 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, uni_buf
, nlen
, IpDx
);
3343 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3344 SetBkMode(hdc
, TRANSPARENT
);
3345 ExtTextOutW(hdc
, x
- 1,
3346 y
- font_height
* (lattr
==
3347 LATTR_BOT
) + text_adjust
,
3348 ETO_CLIPPED
, &line_box
, uni_buf
, nlen
, IpDx
);
3352 } else if (DIRECT_FONT(text
[0])) {
3353 static char *directbuf
= NULL
;
3354 static int directlen
= 0;
3356 if (len
> directlen
) {
3358 directbuf
= sresize(directbuf
, directlen
, char);
3361 for (i
= 0; i
< len
; i
++)
3362 directbuf
[i
] = text
[i
] & 0xFF;
3365 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3366 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, directbuf
, len
, IpDx
);
3367 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3368 SetBkMode(hdc
, TRANSPARENT
);
3370 /* GRR: This draws the character outside it's box and can leave
3371 * 'droppings' even with the clip box! I suppose I could loop it
3372 * one character at a time ... yuk.
3374 * Or ... I could do a test print with "W", and use +1 or -1 for this
3375 * shift depending on if the leftmost column is blank...
3377 ExtTextOut(hdc
, x
- 1,
3378 y
- font_height
* (lattr
==
3379 LATTR_BOT
) + text_adjust
,
3380 ETO_CLIPPED
, &line_box
, directbuf
, len
, IpDx
);
3383 /* And 'normal' unicode characters */
3384 static WCHAR
*wbuf
= NULL
;
3385 static int wlen
= 0;
3391 wbuf
= snewn(wlen
, WCHAR
);
3394 for (i
= 0; i
< len
; i
++)
3397 /* print Glyphs as they are, without Windows' Shaping*/
3398 general_textout(hdc
, x
, y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3399 &line_box
, wbuf
, len
, IpDx
, !(attr
& TATTR_COMBINING
));
3401 /* And the shadow bold hack. */
3402 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3403 SetBkMode(hdc
, TRANSPARENT
);
3404 ExtTextOutW(hdc
, x
- 1,
3405 y
- font_height
* (lattr
==
3406 LATTR_BOT
) + text_adjust
,
3407 ETO_CLIPPED
, &line_box
, wbuf
, len
, IpDx
);
3410 if (lattr
!= LATTR_TOP
&& (force_manual_underline
||
3411 (und_mode
== UND_LINE
3412 && (attr
& ATTR_UNDER
)))) {
3415 if (lattr
== LATTR_BOT
)
3416 dec
= dec
* 2 - font_height
;
3418 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, fg
));
3419 MoveToEx(hdc
, x
, y
+ dec
, NULL
);
3420 LineTo(hdc
, x
+ len
* char_width
, y
+ dec
);
3421 oldpen
= SelectObject(hdc
, oldpen
);
3422 DeleteObject(oldpen
);
3427 * Wrapper that handles combining characters.
3429 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3430 unsigned long attr
, int lattr
)
3432 if (attr
& TATTR_COMBINING
) {
3433 unsigned long a
= 0;
3434 attr
&= ~TATTR_COMBINING
;
3436 do_text_internal(ctx
, x
, y
, text
, 1, attr
| a
, lattr
);
3438 a
= TATTR_COMBINING
;
3441 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
3444 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3445 unsigned long attr
, int lattr
)
3451 int ctype
= cfg
.cursor_type
;
3453 lattr
&= LATTR_MODE
;
3455 if ((attr
& TATTR_ACTCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3456 if (*text
!= UCSWIDE
) {
3457 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
3461 attr
|= TATTR_RIGHTCURS
;
3464 fnt_width
= char_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3465 if (attr
& ATTR_WIDE
)
3472 if ((attr
& TATTR_PASCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3475 pts
[0].x
= pts
[1].x
= pts
[4].x
= x
;
3476 pts
[2].x
= pts
[3].x
= x
+ char_width
- 1;
3477 pts
[0].y
= pts
[3].y
= pts
[4].y
= y
;
3478 pts
[1].y
= pts
[2].y
= y
+ font_height
- 1;
3479 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3480 Polyline(hdc
, pts
, 5);
3481 oldpen
= SelectObject(hdc
, oldpen
);
3482 DeleteObject(oldpen
);
3483 } else if ((attr
& (TATTR_ACTCURS
| TATTR_PASCURS
)) && ctype
!= 0) {
3484 int startx
, starty
, dx
, dy
, length
, i
;
3487 starty
= y
+ descent
;
3490 length
= char_width
;
3493 if (attr
& TATTR_RIGHTCURS
)
3494 xadjust
= char_width
- 1;
3495 startx
= x
+ xadjust
;
3499 length
= font_height
;
3501 if (attr
& TATTR_ACTCURS
) {
3504 SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3505 MoveToEx(hdc
, startx
, starty
, NULL
);
3506 LineTo(hdc
, startx
+ dx
* length
, starty
+ dy
* length
);
3507 oldpen
= SelectObject(hdc
, oldpen
);
3508 DeleteObject(oldpen
);
3510 for (i
= 0; i
< length
; i
++) {
3512 SetPixel(hdc
, startx
, starty
, colours
[261]);
3521 /* This function gets the actual width of a character in the normal font.
3523 int char_width(Context ctx
, int uc
) {
3527 /* If the font max is the same as the font ave width then this
3528 * function is a no-op.
3530 if (!font_dualwidth
) return 1;
3532 switch (uc
& CSET_MASK
) {
3534 uc
= ucsdata
.unitab_line
[uc
& 0xFF];
3537 uc
= ucsdata
.unitab_xterm
[uc
& 0xFF];
3540 uc
= ucsdata
.unitab_scoacs
[uc
& 0xFF];
3543 if (DIRECT_FONT(uc
)) {
3544 if (ucsdata
.dbcs_screenfont
) return 1;
3546 /* Speedup, I know of no font where ascii is the wrong width */
3547 if ((uc
&~CSET_MASK
) >= ' ' && (uc
&~CSET_MASK
)<= '~')
3550 if ( (uc
& CSET_MASK
) == CSET_ACP
) {
3551 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3552 } else if ( (uc
& CSET_MASK
) == CSET_OEMCP
) {
3553 another_font(FONT_OEM
);
3554 if (!fonts
[FONT_OEM
]) return 0;
3556 SelectObject(hdc
, fonts
[FONT_OEM
]);
3560 if ( GetCharWidth32(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1 &&
3561 GetCharWidth(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1)
3564 /* Speedup, I know of no font where ascii is the wrong width */
3565 if (uc
>= ' ' && uc
<= '~') return 1;
3567 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3568 if ( GetCharWidth32W(hdc
, uc
, uc
, &ibuf
) == 1 )
3569 /* Okay that one worked */ ;
3570 else if ( GetCharWidthW(hdc
, uc
, uc
, &ibuf
) == 1 )
3571 /* This should work on 9x too, but it's "less accurate" */ ;
3576 ibuf
+= font_width
/ 2 -1;
3583 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3584 * codes. Returns number of bytes used, zero to drop the message,
3585 * -1 to forward the message to Windows, or another negative number
3586 * to indicate a NUL-terminated "special" string.
3588 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
3589 unsigned char *output
)
3592 int scan
, left_alt
= 0, key_down
, shift_state
;
3594 unsigned char *p
= output
;
3595 static int alt_sum
= 0;
3597 HKL kbd_layout
= GetKeyboardLayout(0);
3599 /* keys is for ToAsciiEx. There's some ick here, see below. */
3600 static WORD keys
[3];
3601 static int compose_char
= 0;
3602 static WPARAM compose_key
= 0;
3604 r
= GetKeyboardState(keystate
);
3606 memset(keystate
, 0, sizeof(keystate
));
3609 #define SHOW_TOASCII_RESULT
3610 { /* Tell us all about key events */
3611 static BYTE oldstate
[256];
3612 static int first
= 1;
3616 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3619 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
) {
3621 } else if ((HIWORD(lParam
) & KF_UP
)
3622 && scan
== (HIWORD(lParam
) & 0xFF)) {
3626 if (wParam
>= VK_F1
&& wParam
<= VK_F20
)
3627 debug(("K_F%d", wParam
+ 1 - VK_F1
));
3640 debug(("VK_%02x", wParam
));
3642 if (message
== WM_SYSKEYDOWN
|| message
== WM_SYSKEYUP
)
3644 debug((", S%02x", scan
= (HIWORD(lParam
) & 0xFF)));
3646 ch
= MapVirtualKeyEx(wParam
, 2, kbd_layout
);
3647 if (ch
>= ' ' && ch
<= '~')
3648 debug((", '%c'", ch
));
3650 debug((", $%02x", ch
));
3653 debug((", KB0=%02x", keys
[0]));
3655 debug((", KB1=%02x", keys
[1]));
3657 debug((", KB2=%02x", keys
[2]));
3659 if ((keystate
[VK_SHIFT
] & 0x80) != 0)
3661 if ((keystate
[VK_CONTROL
] & 0x80) != 0)
3663 if ((HIWORD(lParam
) & KF_EXTENDED
))
3665 if ((HIWORD(lParam
) & KF_UP
))
3669 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
);
3670 else if ((HIWORD(lParam
) & KF_UP
))
3671 oldstate
[wParam
& 0xFF] ^= 0x80;
3673 oldstate
[wParam
& 0xFF] ^= 0x81;
3675 for (ch
= 0; ch
< 256; ch
++)
3676 if (oldstate
[ch
] != keystate
[ch
])
3677 debug((", M%02x=%02x", ch
, keystate
[ch
]));
3679 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3683 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
)) {
3684 keystate
[VK_RMENU
] = keystate
[VK_MENU
];
3688 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3689 if ((cfg
.funky_type
== FUNKY_VT400
||
3690 (cfg
.funky_type
<= FUNKY_LINUX
&& term
->app_keypad_keys
&&
3692 && wParam
== VK_NUMLOCK
&& !(keystate
[VK_SHIFT
] & 0x80)) {
3694 wParam
= VK_EXECUTE
;
3696 /* UnToggle NUMLock */
3697 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0)
3698 keystate
[VK_NUMLOCK
] ^= 1;
3701 /* And write back the 'adjusted' state */
3702 SetKeyboardState(keystate
);
3705 /* Disable Auto repeat if required */
3706 if (term
->repeat_off
&&
3707 (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
)
3710 if ((HIWORD(lParam
) & KF_ALTDOWN
) && (keystate
[VK_RMENU
] & 0x80) == 0)
3713 key_down
= ((HIWORD(lParam
) & KF_UP
) == 0);
3715 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3716 if (left_alt
&& (keystate
[VK_CONTROL
] & 0x80)) {
3717 if (cfg
.ctrlaltkeys
)
3718 keystate
[VK_MENU
] = 0;
3720 keystate
[VK_RMENU
] = 0x80;
3725 scan
= (HIWORD(lParam
) & (KF_UP
| KF_EXTENDED
| 0xFF));
3726 shift_state
= ((keystate
[VK_SHIFT
] & 0x80) != 0)
3727 + ((keystate
[VK_CONTROL
] & 0x80) != 0) * 2;
3729 /* Note if AltGr was pressed and if it was used as a compose key */
3730 if (!compose_state
) {
3731 compose_key
= 0x100;
3732 if (cfg
.compose_key
) {
3733 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
))
3734 compose_key
= wParam
;
3736 if (wParam
== VK_APPS
)
3737 compose_key
= wParam
;
3740 if (wParam
== compose_key
) {
3741 if (compose_state
== 0
3742 && (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0) compose_state
=
3744 else if (compose_state
== 1 && (HIWORD(lParam
) & KF_UP
))
3748 } else if (compose_state
== 1 && wParam
!= VK_CONTROL
)
3751 if (compose_state
> 1 && left_alt
)
3754 /* Sanitize the number pad if not using a PC NumPad */
3755 if (left_alt
|| (term
->app_keypad_keys
&& !cfg
.no_applic_k
3756 && cfg
.funky_type
!= FUNKY_XTERM
)
3757 || cfg
.funky_type
== FUNKY_VT400
|| cfg
.nethack_keypad
|| compose_state
) {
3758 if ((HIWORD(lParam
) & KF_EXTENDED
) == 0) {
3762 nParam
= VK_NUMPAD0
;
3765 nParam
= VK_NUMPAD1
;
3768 nParam
= VK_NUMPAD2
;
3771 nParam
= VK_NUMPAD3
;
3774 nParam
= VK_NUMPAD4
;
3777 nParam
= VK_NUMPAD5
;
3780 nParam
= VK_NUMPAD6
;
3783 nParam
= VK_NUMPAD7
;
3786 nParam
= VK_NUMPAD8
;
3789 nParam
= VK_NUMPAD9
;
3792 nParam
= VK_DECIMAL
;
3796 if (keystate
[VK_NUMLOCK
] & 1)
3803 /* If a key is pressed and AltGr is not active */
3804 if (key_down
&& (keystate
[VK_RMENU
] & 0x80) == 0 && !compose_state
) {
3805 /* Okay, prepare for most alts then ... */
3809 /* Lets see if it's a pattern we know all about ... */
3810 if (wParam
== VK_PRIOR
&& shift_state
== 1) {
3811 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEUP
, 0);
3814 if (wParam
== VK_PRIOR
&& shift_state
== 2) {
3815 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEUP
, 0);
3818 if (wParam
== VK_NEXT
&& shift_state
== 1) {
3819 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEDOWN
, 0);
3822 if (wParam
== VK_NEXT
&& shift_state
== 2) {
3823 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEDOWN
, 0);
3826 if (wParam
== VK_INSERT
&& shift_state
== 1) {
3827 request_paste(NULL
);
3830 if (left_alt
&& wParam
== VK_F4
&& cfg
.alt_f4
) {
3833 if (left_alt
&& wParam
== VK_SPACE
&& cfg
.alt_space
) {
3834 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_KEYMENU
, 0);
3837 if (left_alt
&& wParam
== VK_RETURN
&& cfg
.fullscreenonaltenter
&&
3838 (cfg
.resize_action
!= RESIZE_DISABLED
)) {
3839 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) != KF_REPEAT
)
3843 /* Control-Numlock for app-keypad mode switch */
3844 if (wParam
== VK_PAUSE
&& shift_state
== 2) {
3845 term
->app_keypad_keys
^= 1;
3849 /* Nethack keypad */
3850 if (cfg
.nethack_keypad
&& !left_alt
) {
3853 *p
++ = "bB\002\002"[shift_state
& 3];
3856 *p
++ = "jJ\012\012"[shift_state
& 3];
3859 *p
++ = "nN\016\016"[shift_state
& 3];
3862 *p
++ = "hH\010\010"[shift_state
& 3];
3865 *p
++ = shift_state ?
'.' : '.';
3868 *p
++ = "lL\014\014"[shift_state
& 3];
3871 *p
++ = "yY\031\031"[shift_state
& 3];
3874 *p
++ = "kK\013\013"[shift_state
& 3];
3877 *p
++ = "uU\025\025"[shift_state
& 3];
3882 /* Application Keypad */
3886 if (cfg
.funky_type
== FUNKY_VT400
||
3887 (cfg
.funky_type
<= FUNKY_LINUX
&&
3888 term
->app_keypad_keys
&& !cfg
.no_applic_k
)) switch (wParam
) {
3902 if (term
->app_keypad_keys
&& !cfg
.no_applic_k
)
3939 if (cfg
.funky_type
== FUNKY_XTERM
) {
3944 } else if (shift_state
)
3951 if (cfg
.funky_type
== FUNKY_XTERM
)
3955 if (cfg
.funky_type
== FUNKY_XTERM
)
3959 if (cfg
.funky_type
== FUNKY_XTERM
)
3964 if (HIWORD(lParam
) & KF_EXTENDED
)
3969 if (term
->vt52_mode
) {
3970 if (xkey
>= 'P' && xkey
<= 'S')
3971 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3973 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
3975 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3980 if (wParam
== VK_BACK
&& shift_state
== 0) { /* Backspace */
3981 *p
++ = (cfg
.bksp_is_delete ?
0x7F : 0x08);
3985 if (wParam
== VK_BACK
&& shift_state
== 1) { /* Shift Backspace */
3986 /* We do the opposite of what is configured */
3987 *p
++ = (cfg
.bksp_is_delete ?
0x08 : 0x7F);
3991 if (wParam
== VK_TAB
&& shift_state
== 1) { /* Shift tab */
3997 if (wParam
== VK_SPACE
&& shift_state
== 2) { /* Ctrl-Space */
4001 if (wParam
== VK_SPACE
&& shift_state
== 3) { /* Ctrl-Shift-Space */
4005 if (wParam
== VK_CANCEL
&& shift_state
== 2) { /* Ctrl-Break */
4007 back
->special(backhandle
, TS_BRK
);
4010 if (wParam
== VK_PAUSE
) { /* Break/Pause */
4015 /* Control-2 to Control-8 are special */
4016 if (shift_state
== 2 && wParam
>= '2' && wParam
<= '8') {
4017 *p
++ = "\000\033\034\035\036\037\177"[wParam
- '2'];
4020 if (shift_state
== 2 && (wParam
== 0xBD || wParam
== 0xBF)) {
4024 if (shift_state
== 2 && (wParam
== 0xDF || wParam
== 0xDC)) {
4028 if (shift_state
== 3 && wParam
== 0xDE) {
4029 *p
++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
4032 if (shift_state
== 0 && wParam
== VK_RETURN
&& term
->cr_lf_return
) {
4039 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
4040 * for integer decimal nn.)
4042 * We also deal with the weird ones here. Linux VCs replace F1
4043 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
4044 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
4050 code
= (keystate
[VK_SHIFT
] & 0x80 ?
23 : 11);
4053 code
= (keystate
[VK_SHIFT
] & 0x80 ?
24 : 12);
4056 code
= (keystate
[VK_SHIFT
] & 0x80 ?
25 : 13);
4059 code
= (keystate
[VK_SHIFT
] & 0x80 ?
26 : 14);
4062 code
= (keystate
[VK_SHIFT
] & 0x80 ?
28 : 15);
4065 code
= (keystate
[VK_SHIFT
] & 0x80 ?
29 : 17);
4068 code
= (keystate
[VK_SHIFT
] & 0x80 ?
31 : 18);
4071 code
= (keystate
[VK_SHIFT
] & 0x80 ?
32 : 19);
4074 code
= (keystate
[VK_SHIFT
] & 0x80 ?
33 : 20);
4077 code
= (keystate
[VK_SHIFT
] & 0x80 ?
34 : 21);
4110 if ((shift_state
&2) == 0) switch (wParam
) {
4130 /* Reorder edit keys to physical order */
4131 if (cfg
.funky_type
== FUNKY_VT400
&& code
<= 6)
4132 code
= "\0\2\1\4\5\3\6"[code
];
4134 if (term
->vt52_mode
&& code
> 0 && code
<= 6) {
4135 p
+= sprintf((char *) p
, "\x1B%c", " HLMEIG"[code
]);
4139 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO function keys */
4140 code
>= 11 && code
<= 34) {
4141 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
4144 case VK_F1
: index
= 0; break;
4145 case VK_F2
: index
= 1; break;
4146 case VK_F3
: index
= 2; break;
4147 case VK_F4
: index
= 3; break;
4148 case VK_F5
: index
= 4; break;
4149 case VK_F6
: index
= 5; break;
4150 case VK_F7
: index
= 6; break;
4151 case VK_F8
: index
= 7; break;
4152 case VK_F9
: index
= 8; break;
4153 case VK_F10
: index
= 9; break;
4154 case VK_F11
: index
= 10; break;
4155 case VK_F12
: index
= 11; break;
4157 if (keystate
[VK_SHIFT
] & 0x80) index
+= 12;
4158 if (keystate
[VK_CONTROL
] & 0x80) index
+= 24;
4159 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
4162 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO small keypad */
4163 code
>= 1 && code
<= 6) {
4164 char codes
[] = "HL.FIG";
4168 p
+= sprintf((char *) p
, "\x1B[%c", codes
[code
-1]);
4172 if ((term
->vt52_mode
|| cfg
.funky_type
== FUNKY_VT100P
) && code
>= 11 && code
<= 24) {
4178 if (term
->vt52_mode
)
4179 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11 - offt
);
4182 sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11 - offt
);
4185 if (cfg
.funky_type
== FUNKY_LINUX
&& code
>= 11 && code
<= 15) {
4186 p
+= sprintf((char *) p
, "\x1B[[%c", code
+ 'A' - 11);
4189 if (cfg
.funky_type
== FUNKY_XTERM
&& code
>= 11 && code
<= 14) {
4190 if (term
->vt52_mode
)
4191 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11);
4193 p
+= sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11);
4196 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
4197 p
+= sprintf((char *) p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
4201 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
4206 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
4207 * some reason seems to send VK_CLEAR to Windows...).
4229 if (term
->vt52_mode
)
4230 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4232 int app_flg
= (term
->app_cursor_keys
&& !cfg
.no_applic_c
);
4235 * RDB: VT100 & VT102 manuals both state the
4236 * app cursor keys only work if the app keypad
4239 * SGT: That may well be true, but xterm
4240 * disagrees and so does at least one
4241 * application, so I've #if'ed this out and the
4242 * behaviour is back to PuTTY's original: app
4243 * cursor and app keypad are independently
4244 * switchable modes. If anyone complains about
4245 * _this_ I'll have to put in a configurable
4248 if (!term
->app_keypad_keys
)
4251 /* Useful mapping of Ctrl-arrows */
4252 if (shift_state
== 2)
4256 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4258 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4265 * Finally, deal with Return ourselves. (Win95 seems to
4266 * foul it up when Alt is pressed, for some reason.)
4268 if (wParam
== VK_RETURN
) { /* Return */
4274 if (left_alt
&& wParam
>= VK_NUMPAD0
&& wParam
<= VK_NUMPAD9
)
4275 alt_sum
= alt_sum
* 10 + wParam
- VK_NUMPAD0
;
4280 /* Okay we've done everything interesting; let windows deal with
4281 * the boring stuff */
4285 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4286 if(cfg
.xlat_capslockcyr
&& keystate
[VK_CAPITAL
] != 0) {
4288 keystate
[VK_CAPITAL
] = 0;
4291 /* XXX how do we know what the max size of the keys array should
4292 * be is? There's indication on MS' website of an Inquire/InquireEx
4293 * functioning returning a KBINFO structure which tells us. */
4294 if (osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
4295 /* XXX 'keys' parameter is declared in MSDN documentation as
4297 * The experience of a French user indicates that on
4298 * Win98, WORD[] should be passed in, but on Win2K, it should
4299 * be BYTE[]. German WinXP and my Win2K with "US International"
4300 * driver corroborate this.
4301 * Experimentally I've conditionalised the behaviour on the
4302 * Win9x/NT split, but I suspect it's worse than that.
4303 * See wishlist item `win-dead-keys' for more horrible detail
4304 * and speculations. */
4307 r
= ToAsciiEx(wParam
, scan
, keystate
, (LPWORD
)keybs
, 0, kbd_layout
);
4308 for (i
=0; i
<3; i
++) keys
[i
] = keybs
[i
];
4310 r
= ToAsciiEx(wParam
, scan
, keystate
, keys
, 0, kbd_layout
);
4312 #ifdef SHOW_TOASCII_RESULT
4313 if (r
== 1 && !key_down
) {
4315 if (in_utf(term
) || ucsdata
.dbcs_screenfont
)
4316 debug((", (U+%04x)", alt_sum
));
4318 debug((", LCH(%d)", alt_sum
));
4320 debug((", ACH(%d)", keys
[0]));
4325 for (r1
= 0; r1
< r
; r1
++) {
4326 debug(("%s%d", r1 ?
"," : "", keys
[r1
]));
4335 * Interrupt an ongoing paste. I'm not sure this is
4336 * sensible, but for the moment it's preferable to
4337 * having to faff about buffering things.
4342 for (i
= 0; i
< r
; i
++) {
4343 unsigned char ch
= (unsigned char) keys
[i
];
4345 if (compose_state
== 2 && (ch
& 0x80) == 0 && ch
> ' ') {
4350 if (compose_state
== 3 && (ch
& 0x80) == 0 && ch
> ' ') {
4354 if ((nc
= check_compose(compose_char
, ch
)) == -1) {
4355 MessageBeep(MB_ICONHAND
);
4359 term_seen_key_event(term
);
4361 luni_send(ldisc
, &keybuf
, 1, 1);
4369 if (in_utf(term
) || ucsdata
.dbcs_screenfont
) {
4371 term_seen_key_event(term
);
4373 luni_send(ldisc
, &keybuf
, 1, 1);
4375 ch
= (char) alt_sum
;
4377 * We need not bother about stdin
4378 * backlogs here, because in GUI PuTTY
4379 * we can't do anything about it
4380 * anyway; there's no means of asking
4381 * Windows to hold off on KEYDOWN
4382 * messages. We _have_ to buffer
4383 * everything we're sent.
4385 term_seen_key_event(term
);
4387 ldisc_send(ldisc
, &ch
, 1, 1);
4391 term_seen_key_event(term
);
4393 lpage_send(ldisc
, kbd_codepage
, &ch
, 1, 1);
4396 if(capsOn
&& ch
< 0x80) {
4399 cbuf
[1] = xlat_uskbd2cyrllic(ch
);
4400 term_seen_key_event(term
);
4402 luni_send(ldisc
, cbuf
+!left_alt
, 1+!!left_alt
, 1);
4407 term_seen_key_event(term
);
4409 lpage_send(ldisc
, kbd_codepage
,
4410 cbuf
+!left_alt
, 1+!!left_alt
, 1);
4416 /* This is so the ALT-Numpad and dead keys work correctly. */
4421 /* If we're definitly not building up an ALT-54321 then clear it */
4424 /* If we will be using alt_sum fix the 256s */
4425 else if (keys
[0] && (in_utf(term
) || ucsdata
.dbcs_screenfont
))
4430 * ALT alone may or may not want to bring up the System menu.
4431 * If it's not meant to, we return 0 on presses or releases of
4432 * ALT, to show that we've swallowed the keystroke. Otherwise
4433 * we return -1, which means Windows will give the keystroke
4434 * its default handling (i.e. bring up the System menu).
4436 if (wParam
== VK_MENU
&& !cfg
.alt_only
)
4442 void set_title(void *frontend
, char *title
)
4445 window_name
= snewn(1 + strlen(title
), char);
4446 strcpy(window_name
, title
);
4447 if (cfg
.win_name_always
|| !IsIconic(hwnd
))
4448 SetWindowText(hwnd
, title
);
4451 void set_icon(void *frontend
, char *title
)
4454 icon_name
= snewn(1 + strlen(title
), char);
4455 strcpy(icon_name
, title
);
4456 if (!cfg
.win_name_always
&& IsIconic(hwnd
))
4457 SetWindowText(hwnd
, title
);
4460 void set_sbar(void *frontend
, int total
, int start
, int page
)
4464 if (is_full_screen() ?
!cfg
.scrollbar_in_fullscreen
: !cfg
.scrollbar
)
4467 si
.cbSize
= sizeof(si
);
4468 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
4470 si
.nMax
= total
- 1;
4474 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
4477 Context
get_ctx(void *frontend
)
4483 SelectPalette(hdc
, pal
, FALSE
);
4489 void free_ctx(Context ctx
)
4491 SelectPalette(ctx
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
4492 ReleaseDC(hwnd
, ctx
);
4495 static void real_palette_set(int n
, int r
, int g
, int b
)
4498 logpal
->palPalEntry
[n
].peRed
= r
;
4499 logpal
->palPalEntry
[n
].peGreen
= g
;
4500 logpal
->palPalEntry
[n
].peBlue
= b
;
4501 logpal
->palPalEntry
[n
].peFlags
= PC_NOCOLLAPSE
;
4502 colours
[n
] = PALETTERGB(r
, g
, b
);
4503 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4505 colours
[n
] = RGB(r
, g
, b
);
4508 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
4512 if (n
> NALLCOLOURS
)
4514 real_palette_set(n
, r
, g
, b
);
4516 HDC hdc
= get_ctx(frontend
);
4517 UnrealizeObject(pal
);
4518 RealizePalette(hdc
);
4521 if (n
== (ATTR_DEFBG
>>ATTR_BGSHIFT
))
4522 /* If Default Background changes, we need to ensure any
4523 * space between the text area and the window border is
4525 InvalidateRect(hwnd
, NULL
, TRUE
);
4529 void palette_reset(void *frontend
)
4534 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4536 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
4537 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
4538 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
4539 logpal
->palPalEntry
[i
].peFlags
= 0;
4540 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
4541 defpal
[i
].rgbtGreen
,
4542 defpal
[i
].rgbtBlue
);
4544 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
4545 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4550 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4551 hdc
= get_ctx(frontend
);
4552 RealizePalette(hdc
);
4555 /* Default Background may have changed. Ensure any space between
4556 * text area and window border is redrawn. */
4557 InvalidateRect(hwnd
, NULL
, TRUE
);
4561 void write_aclip(void *frontend
, char *data
, int len
, int must_deselect
)
4566 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
4569 lock
= GlobalLock(clipdata
);
4572 memcpy(lock
, data
, len
);
4573 ((unsigned char *) lock
)[len
] = 0;
4574 GlobalUnlock(clipdata
);
4577 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4579 if (OpenClipboard(hwnd
)) {
4581 SetClipboardData(CF_TEXT
, clipdata
);
4584 GlobalFree(clipdata
);
4587 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4591 * Note: unlike write_aclip() this will not append a nul.
4593 void write_clip(void *frontend
, wchar_t * data
, int *attr
, int len
, int must_deselect
)
4595 HGLOBAL clipdata
, clipdata2
, clipdata3
;
4597 void *lock
, *lock2
, *lock3
;
4599 len2
= WideCharToMultiByte(CP_ACP
, 0, data
, len
, 0, 0, NULL
, NULL
);
4601 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
,
4602 len
* sizeof(wchar_t));
4603 clipdata2
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len2
);
4605 if (!clipdata
|| !clipdata2
) {
4607 GlobalFree(clipdata
);
4609 GlobalFree(clipdata2
);
4612 if (!(lock
= GlobalLock(clipdata
)))
4614 if (!(lock2
= GlobalLock(clipdata2
)))
4617 memcpy(lock
, data
, len
* sizeof(wchar_t));
4618 WideCharToMultiByte(CP_ACP
, 0, data
, len
, lock2
, len2
, NULL
, NULL
);
4620 if (cfg
.rtf_paste
) {
4621 wchar_t unitab
[256];
4623 unsigned char *tdata
= (unsigned char *)lock2
;
4624 wchar_t *udata
= (wchar_t *)lock
;
4625 int rtflen
= 0, uindex
= 0, tindex
= 0;
4627 int multilen
, blen
, alen
, totallen
, i
;
4628 char before
[16], after
[4];
4629 int fgcolour
, lastfgcolour
= 0;
4630 int bgcolour
, lastbgcolour
= 0;
4631 int attrBold
, lastAttrBold
= 0;
4632 int attrUnder
, lastAttrUnder
= 0;
4633 int palette
[NALLCOLOURS
];
4636 get_unitab(CP_ACP
, unitab
, 0);
4638 rtfsize
= 100 + strlen(cfg
.font
.name
);
4639 rtf
= snewn(rtfsize
, char);
4640 rtflen
= sprintf(rtf
, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
4641 cfg
.font
.name
, cfg
.font
.height
*2);
4644 * Add colour palette
4645 * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
4649 * First - Determine all colours in use
4650 * o Foregound and background colours share the same palette
4653 memset(palette
, 0, sizeof(palette
));
4654 for (i
= 0; i
< (len
-1); i
++) {
4655 fgcolour
= ((attr
[i
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4656 bgcolour
= ((attr
[i
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4658 if (attr
[i
] & ATTR_REVERSE
) {
4659 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4660 fgcolour
= bgcolour
;
4661 bgcolour
= tmpcolour
;
4664 if (bold_mode
== BOLD_COLOURS
&& (attr
[i
] & ATTR_BOLD
)) {
4665 if (fgcolour
< 8) /* ANSI colours */
4667 else if (fgcolour
>= 256) /* Default colours */
4671 if (attr
[i
] & ATTR_BLINK
) {
4672 if (bgcolour
< 8) /* ANSI colours */
4674 else if (bgcolour
>= 256) /* Default colours */
4678 palette
[fgcolour
]++;
4679 palette
[bgcolour
]++;
4683 * Next - Create a reduced palette
4686 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4687 if (palette
[i
] != 0)
4688 palette
[i
] = ++numcolours
;
4692 * Finally - Write the colour table
4694 rtf
= sresize(rtf
, rtfsize
+ (numcolours
* 25), char);
4695 strcat(rtf
, "{\\colortbl ;");
4696 rtflen
= strlen(rtf
);
4698 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4699 if (palette
[i
] != 0) {
4700 rtflen
+= sprintf(&rtf
[rtflen
], "\\red%d\\green%d\\blue%d;", defpal
[i
].rgbtRed
, defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4703 strcpy(&rtf
[rtflen
], "}");
4708 * We want to construct a piece of RTF that specifies the
4709 * same Unicode text. To do this we will read back in
4710 * parallel from the Unicode data in `udata' and the
4711 * non-Unicode data in `tdata'. For each character in
4712 * `tdata' which becomes the right thing in `udata' when
4713 * looked up in `unitab', we just copy straight over from
4714 * tdata. For each one that doesn't, we must WCToMB it
4715 * individually and produce a \u escape sequence.
4717 * It would probably be more robust to just bite the bullet
4718 * and WCToMB each individual Unicode character one by one,
4719 * then MBToWC each one back to see if it was an accurate
4720 * translation; but that strikes me as a horrifying number
4721 * of Windows API calls so I want to see if this faster way
4722 * will work. If it screws up badly we can always revert to
4723 * the simple and slow way.
4725 while (tindex
< len2
&& uindex
< len
&&
4726 tdata
[tindex
] && udata
[uindex
]) {
4727 if (tindex
+ 1 < len2
&&
4728 tdata
[tindex
] == '\r' &&
4729 tdata
[tindex
+1] == '\n') {
4735 * Set text attributes
4738 if (rtfsize
< rtflen
+ 64) {
4739 rtfsize
= rtflen
+ 512;
4740 rtf
= sresize(rtf
, rtfsize
, char);
4744 * Determine foreground and background colours
4746 fgcolour
= ((attr
[tindex
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4747 bgcolour
= ((attr
[tindex
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4749 if (attr
[tindex
] & ATTR_REVERSE
) {
4750 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4751 fgcolour
= bgcolour
;
4752 bgcolour
= tmpcolour
;
4755 if (bold_mode
== BOLD_COLOURS
&& (attr
[tindex
] & ATTR_BOLD
)) {
4756 if (fgcolour
< 8) /* ANSI colours */
4758 else if (fgcolour
>= 256) /* Default colours */
4762 if (attr
[tindex
] & ATTR_BLINK
) {
4763 if (bgcolour
< 8) /* ANSI colours */
4765 else if (bgcolour
>= 256) /* Default colours */
4770 * Collect other attributes
4772 if (bold_mode
!= BOLD_COLOURS
)
4773 attrBold
= attr
[tindex
] & ATTR_BOLD
;
4777 attrUnder
= attr
[tindex
] & ATTR_UNDER
;
4781 * o If video isn't reversed, ignore colour attributes for default foregound
4783 * o Special case where bolded text is displayed using the default foregound
4784 * and background colours - force to bolded RTF.
4786 if (!(attr
[tindex
] & ATTR_REVERSE
)) {
4787 if (bgcolour
>= 256) /* Default color */
4788 bgcolour
= -1; /* No coloring */
4790 if (fgcolour
>= 256) { /* Default colour */
4791 if (bold_mode
== BOLD_COLOURS
&& (fgcolour
& 1) && bgcolour
== -1)
4792 attrBold
= ATTR_BOLD
; /* Emphasize text with bold attribute */
4794 fgcolour
= -1; /* No coloring */
4799 * Write RTF text attributes
4801 if (lastfgcolour
!= fgcolour
) {
4802 lastfgcolour
= fgcolour
;
4803 rtflen
+= sprintf(&rtf
[rtflen
], "\\cf%d ", (fgcolour
>= 0) ? palette
[fgcolour
] : 0);
4806 if (lastbgcolour
!= bgcolour
) {
4807 lastbgcolour
= bgcolour
;
4808 rtflen
+= sprintf(&rtf
[rtflen
], "\\highlight%d ", (bgcolour
>= 0) ? palette
[bgcolour
] : 0);
4811 if (lastAttrBold
!= attrBold
) {
4812 lastAttrBold
= attrBold
;
4813 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrBold ?
"\\b " : "\\b0 ");
4816 if (lastAttrUnder
!= attrUnder
) {
4817 lastAttrUnder
= attrUnder
;
4818 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrUnder ?
"\\ul " : "\\ulnone ");
4822 if (unitab
[tdata
[tindex
]] == udata
[uindex
]) {
4828 multilen
= WideCharToMultiByte(CP_ACP
, 0, unitab
+uindex
, 1,
4829 NULL
, 0, NULL
, NULL
);
4830 if (multilen
!= 1) {
4831 blen
= sprintf(before
, "{\\uc%d\\u%d", multilen
,
4833 alen
= 1; strcpy(after
, "}");
4835 blen
= sprintf(before
, "\\u%d", udata
[uindex
]);
4836 alen
= 0; after
[0] = '\0';
4839 assert(tindex
+ multilen
<= len2
);
4840 totallen
= blen
+ alen
;
4841 for (i
= 0; i
< multilen
; i
++) {
4842 if (tdata
[tindex
+i
] == '\\' ||
4843 tdata
[tindex
+i
] == '{' ||
4844 tdata
[tindex
+i
] == '}')
4846 else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A)
4847 totallen
+= 6; /* \par\r\n */
4848 else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20)
4854 if (rtfsize
< rtflen
+ totallen
+ 3) {
4855 rtfsize
= rtflen
+ totallen
+ 512;
4856 rtf
= sresize(rtf
, rtfsize
, char);
4859 strcpy(rtf
+ rtflen
, before
); rtflen
+= blen
;
4860 for (i
= 0; i
< multilen
; i
++) {
4861 if (tdata
[tindex
+i
] == '\\' ||
4862 tdata
[tindex
+i
] == '{' ||
4863 tdata
[tindex
+i
] == '}') {
4864 rtf
[rtflen
++] = '\\';
4865 rtf
[rtflen
++] = tdata
[tindex
+i
];
4866 } else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A) {
4867 rtflen
+= sprintf(rtf
+rtflen
, "\\par\r\n");
4868 } else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20) {
4869 rtflen
+= sprintf(rtf
+rtflen
, "\\'%02x", tdata
[tindex
+i
]);
4871 rtf
[rtflen
++] = tdata
[tindex
+i
];
4874 strcpy(rtf
+ rtflen
, after
); rtflen
+= alen
;
4880 rtf
[rtflen
++] = '}'; /* Terminate RTF stream */
4881 rtf
[rtflen
++] = '\0';
4882 rtf
[rtflen
++] = '\0';
4884 clipdata3
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, rtflen
);
4885 if (clipdata3
&& (lock3
= GlobalLock(clipdata3
)) != NULL
) {
4886 memcpy(lock3
, rtf
, rtflen
);
4887 GlobalUnlock(clipdata3
);
4893 GlobalUnlock(clipdata
);
4894 GlobalUnlock(clipdata2
);
4897 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4899 if (OpenClipboard(hwnd
)) {
4901 SetClipboardData(CF_UNICODETEXT
, clipdata
);
4902 SetClipboardData(CF_TEXT
, clipdata2
);
4904 SetClipboardData(RegisterClipboardFormat(CF_RTF
), clipdata3
);
4907 GlobalFree(clipdata
);
4908 GlobalFree(clipdata2
);
4912 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4915 static DWORD WINAPI
clipboard_read_threadfunc(void *param
)
4917 HWND hwnd
= (HWND
)param
;
4920 if (OpenClipboard(NULL
)) {
4921 if ((clipdata
= GetClipboardData(CF_UNICODETEXT
))) {
4922 SendMessage(hwnd
, WM_GOT_CLIPDATA
, (WPARAM
)1, (LPARAM
)clipdata
);
4923 } else if ((clipdata
= GetClipboardData(CF_TEXT
))) {
4924 SendMessage(hwnd
, WM_GOT_CLIPDATA
, (WPARAM
)0, (LPARAM
)clipdata
);
4932 static int process_clipdata(HGLOBAL clipdata
, int unicode
)
4934 sfree(clipboard_contents
);
4935 clipboard_contents
= NULL
;
4936 clipboard_length
= 0;
4939 wchar_t *p
= GlobalLock(clipdata
);
4943 /* Unwilling to rely on Windows having wcslen() */
4944 for (p2
= p
; *p2
; p2
++);
4945 clipboard_length
= p2
- p
;
4946 clipboard_contents
= snewn(clipboard_length
+ 1, wchar_t);
4947 memcpy(clipboard_contents
, p
, clipboard_length
* sizeof(wchar_t));
4948 clipboard_contents
[clipboard_length
] = L
'\0';
4952 char *s
= GlobalLock(clipdata
);
4956 i
= MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, 0, 0);
4957 clipboard_contents
= snewn(i
, wchar_t);
4958 MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1,
4959 clipboard_contents
, i
);
4960 clipboard_length
= i
- 1;
4961 clipboard_contents
[clipboard_length
] = L
'\0';
4969 void request_paste(void *frontend
)
4972 * I always thought pasting was synchronous in Windows; the
4973 * clipboard access functions certainly _look_ synchronous,
4974 * unlike the X ones. But in fact it seems that in some
4975 * situations the contents of the clipboard might not be
4976 * immediately available, and the clipboard-reading functions
4977 * may block. This leads to trouble if the application
4978 * delivering the clipboard data has to get hold of it by -
4979 * for example - talking over a network connection which is
4980 * forwarded through this very PuTTY.
4982 * Hence, we spawn a subthread to read the clipboard, and do
4983 * our paste when it's finished. The thread will send a
4984 * message back to our main window when it terminates, and
4985 * that tells us it's OK to paste.
4987 DWORD in_threadid
; /* required for Win9x */
4988 CreateThread(NULL
, 0, clipboard_read_threadfunc
,
4989 hwnd
, 0, &in_threadid
);
4992 void get_clip(void *frontend
, wchar_t **p
, int *len
)
4995 *p
= clipboard_contents
;
4996 *len
= clipboard_length
;
5002 * Move `lines' lines from position `from' to position `to' in the
5005 void optimised_move(void *frontend
, int to
, int from
, int lines
)
5010 min
= (to
< from ? to
: from
);
5011 max
= to
+ from
- min
;
5013 r
.left
= offset_width
;
5014 r
.right
= offset_width
+ term
->cols
* font_width
;
5015 r
.top
= offset_height
+ min
* font_height
;
5016 r
.bottom
= offset_height
+ (max
+ lines
) * font_height
;
5017 ScrollWindow(hwnd
, 0, (to
- from
) * font_height
, &r
, &r
);
5022 * Print a message box and perform a fatal exit.
5024 void fatalbox(char *fmt
, ...)
5027 char *stuff
, morestuff
[100];
5030 stuff
= dupvprintf(fmt
, ap
);
5032 sprintf(morestuff
, "%.70s Fatal Error", appname
);
5033 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
5039 * Print a modal (Really Bad) message box and perform a fatal exit.
5041 void modalfatalbox(char *fmt
, ...)
5044 char *stuff
, morestuff
[100];
5047 stuff
= dupvprintf(fmt
, ap
);
5049 sprintf(morestuff
, "%.70s Fatal Error", appname
);
5050 MessageBox(hwnd
, stuff
, morestuff
,
5051 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
5056 typedef BOOL (WINAPI
*p_FlashWindowEx_t
)(PFLASHWINFO
);
5057 static p_FlashWindowEx_t p_FlashWindowEx
= NULL
;
5059 static void init_flashwindow(void)
5061 HMODULE user32_module
= LoadLibrary("USER32.DLL");
5062 if (user32_module
) {
5063 p_FlashWindowEx
= (p_FlashWindowEx_t
)
5064 GetProcAddress(user32_module
, "FlashWindowEx");
5068 static BOOL
flash_window_ex(DWORD dwFlags
, UINT uCount
, DWORD dwTimeout
)
5070 if (p_FlashWindowEx
) {
5072 fi
.cbSize
= sizeof(fi
);
5074 fi
.dwFlags
= dwFlags
;
5076 fi
.dwTimeout
= dwTimeout
;
5077 return (*p_FlashWindowEx
)(&fi
);
5080 return FALSE
; /* shrug */
5083 static void flash_window(int mode
);
5084 static long next_flash
;
5085 static int flashing
= 0;
5088 * Timer for platforms where we must maintain window flashing manually
5091 static void flash_window_timer(void *ctx
, long now
)
5093 if (flashing
&& now
- next_flash
>= 0) {
5099 * Manage window caption / taskbar flashing, if enabled.
5100 * 0 = stop, 1 = maintain, 2 = start
5102 static void flash_window(int mode
)
5104 if ((mode
== 0) || (cfg
.beep_ind
== B_IND_DISABLED
)) {
5108 if (p_FlashWindowEx
)
5109 flash_window_ex(FLASHW_STOP
, 0, 0);
5111 FlashWindow(hwnd
, FALSE
);
5114 } else if (mode
== 2) {
5118 if (p_FlashWindowEx
) {
5119 /* For so-called "steady" mode, we use uCount=2, which
5120 * seems to be the traditional number of flashes used
5121 * by user notifications (e.g., by Explorer).
5122 * uCount=0 appears to enable continuous flashing, per
5123 * "flashing" mode, although I haven't seen this
5125 flash_window_ex(FLASHW_ALL
| FLASHW_TIMER
,
5126 (cfg
.beep_ind
== B_IND_FLASH ?
0 : 2),
5127 0 /* system cursor blink rate */);
5128 /* No need to schedule timer */
5130 FlashWindow(hwnd
, TRUE
);
5131 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
5135 } else if ((mode
== 1) && (cfg
.beep_ind
== B_IND_FLASH
)) {
5137 if (flashing
&& !p_FlashWindowEx
) {
5138 FlashWindow(hwnd
, TRUE
); /* toggle */
5139 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
5147 void do_beep(void *frontend
, int mode
)
5149 if (mode
== BELL_DEFAULT
) {
5151 * For MessageBeep style bells, we want to be careful of
5152 * timing, because they don't have the nice property of
5153 * PlaySound bells that each one cancels the previous
5154 * active one. So we limit the rate to one per 50ms or so.
5156 static long lastbeep
= 0;
5159 beepdiff
= GetTickCount() - lastbeep
;
5160 if (beepdiff
>= 0 && beepdiff
< 50)
5164 * The above MessageBeep call takes time, so we record the
5165 * time _after_ it finishes rather than before it starts.
5167 lastbeep
= GetTickCount();
5168 } else if (mode
== BELL_WAVEFILE
) {
5169 if (!PlaySound(cfg
.bell_wavefile
.path
, NULL
,
5170 SND_ASYNC
| SND_FILENAME
)) {
5171 char buf
[sizeof(cfg
.bell_wavefile
.path
) + 80];
5173 sprintf(buf
, "Unable to play sound file\n%s\n"
5174 "Using default sound instead", cfg
.bell_wavefile
.path
);
5175 sprintf(otherbuf
, "%.70s Sound Error", appname
);
5176 MessageBox(hwnd
, buf
, otherbuf
,
5177 MB_OK
| MB_ICONEXCLAMATION
);
5178 cfg
.beep
= BELL_DEFAULT
;
5180 } else if (mode
== BELL_PCSPEAKER
) {
5181 static long lastbeep
= 0;
5184 beepdiff
= GetTickCount() - lastbeep
;
5185 if (beepdiff
>= 0 && beepdiff
< 50)
5189 * We must beep in different ways depending on whether this
5190 * is a 95-series or NT-series OS.
5192 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
5196 lastbeep
= GetTickCount();
5198 /* Otherwise, either visual bell or disabled; do nothing here */
5199 if (!term
->has_focus
) {
5200 flash_window(2); /* start */
5205 * Minimise or restore the window in response to a server-side
5208 void set_iconic(void *frontend
, int iconic
)
5210 if (IsIconic(hwnd
)) {
5212 ShowWindow(hwnd
, SW_RESTORE
);
5215 ShowWindow(hwnd
, SW_MINIMIZE
);
5220 * Move the window in response to a server-side request.
5222 void move_window(void *frontend
, int x
, int y
)
5224 if (cfg
.resize_action
== RESIZE_DISABLED
||
5225 cfg
.resize_action
== RESIZE_FONT
||
5229 SetWindowPos(hwnd
, NULL
, x
, y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
);
5233 * Move the window to the top or bottom of the z-order in response
5234 * to a server-side request.
5236 void set_zorder(void *frontend
, int top
)
5238 if (cfg
.alwaysontop
)
5239 return; /* ignore */
5240 SetWindowPos(hwnd
, top ? HWND_TOP
: HWND_BOTTOM
, 0, 0, 0, 0,
5241 SWP_NOMOVE
| SWP_NOSIZE
);
5245 * Refresh the window in response to a server-side request.
5247 void refresh_window(void *frontend
)
5249 InvalidateRect(hwnd
, NULL
, TRUE
);
5253 * Maximise or restore the window in response to a server-side
5256 void set_zoomed(void *frontend
, int zoomed
)
5258 if (IsZoomed(hwnd
)) {
5260 ShowWindow(hwnd
, SW_RESTORE
);
5263 ShowWindow(hwnd
, SW_MAXIMIZE
);
5268 * Report whether the window is iconic, for terminal reports.
5270 int is_iconic(void *frontend
)
5272 return IsIconic(hwnd
);
5276 * Report the window's position, for terminal reports.
5278 void get_window_pos(void *frontend
, int *x
, int *y
)
5281 GetWindowRect(hwnd
, &r
);
5287 * Report the window's pixel size, for terminal reports.
5289 void get_window_pixels(void *frontend
, int *x
, int *y
)
5292 GetWindowRect(hwnd
, &r
);
5293 *x
= r
.right
- r
.left
;
5294 *y
= r
.bottom
- r
.top
;
5298 * Return the window or icon title.
5300 char *get_window_title(void *frontend
, int icon
)
5302 return icon ? icon_name
: window_name
;
5306 * See if we're in full-screen mode.
5308 static int is_full_screen()
5310 if (!IsZoomed(hwnd
))
5312 if (GetWindowLongPtr(hwnd
, GWL_STYLE
) & WS_CAPTION
)
5317 /* Get the rect/size of a full screen window using the nearest available
5318 * monitor in multimon systems; default to something sensible if only
5319 * one monitor is present. */
5320 static int get_fullscreen_rect(RECT
* ss
)
5322 #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
5325 mon
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
5326 mi
.cbSize
= sizeof(mi
);
5327 GetMonitorInfo(mon
, &mi
);
5329 /* structure copy */
5333 /* could also use code like this:
5334 ss->left = ss->top = 0;
5335 ss->right = GetSystemMetrics(SM_CXSCREEN);
5336 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
5338 return GetClientRect(GetDesktopWindow(), ss
);
5344 * Go full-screen. This should only be called when we are already
5347 static void make_full_screen()
5352 assert(IsZoomed(hwnd
));
5354 if (is_full_screen())
5357 /* Remove the window furniture. */
5358 style
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5359 style
&= ~(WS_CAPTION
| WS_BORDER
| WS_THICKFRAME
);
5360 if (cfg
.scrollbar_in_fullscreen
)
5361 style
|= WS_VSCROLL
;
5363 style
&= ~WS_VSCROLL
;
5364 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5366 /* Resize ourselves to exactly cover the nearest monitor. */
5367 get_fullscreen_rect(&ss
);
5368 SetWindowPos(hwnd
, HWND_TOP
, ss
.left
, ss
.top
,
5373 /* We may have changed size as a result */
5377 /* Tick the menu item in the System and context menus. */
5380 for (i
= 0; i
< lenof(popup_menus
); i
++)
5381 CheckMenuItem(popup_menus
[i
].menu
, IDM_FULLSCREEN
, MF_CHECKED
);
5386 * Clear the full-screen attributes.
5388 static void clear_full_screen()
5390 DWORD oldstyle
, style
;
5392 /* Reinstate the window furniture. */
5393 style
= oldstyle
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5394 style
|= WS_CAPTION
| WS_BORDER
;
5395 if (cfg
.resize_action
== RESIZE_DISABLED
)
5396 style
&= ~WS_THICKFRAME
;
5398 style
|= WS_THICKFRAME
;
5400 style
|= WS_VSCROLL
;
5402 style
&= ~WS_VSCROLL
;
5403 if (style
!= oldstyle
) {
5404 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5405 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
5406 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
5410 /* Untick the menu item in the System and context menus. */
5413 for (i
= 0; i
< lenof(popup_menus
); i
++)
5414 CheckMenuItem(popup_menus
[i
].menu
, IDM_FULLSCREEN
, MF_UNCHECKED
);
5419 * Toggle full-screen mode.
5421 static void flip_full_screen()
5423 if (is_full_screen()) {
5424 ShowWindow(hwnd
, SW_RESTORE
);
5425 } else if (IsZoomed(hwnd
)) {
5428 SendMessage(hwnd
, WM_FULLSCR_ON_MAX
, 0, 0);
5429 ShowWindow(hwnd
, SW_MAXIMIZE
);
5433 void frontend_keypress(void *handle
)
5436 * Keypress termination in non-Close-On-Exit mode is not
5437 * currently supported in PuTTY proper, because the window
5438 * always has a perfectly good Close button anyway. So we do
5444 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
5446 return term_data(term
, is_stderr
, data
, len
);
5449 int from_backend_untrusted(void *frontend
, const char *data
, int len
)
5451 return term_data_untrusted(term
, data
, len
);
5454 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
5457 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
5459 ret
= term_get_userpass_input(term
, p
, in
, inlen
);
5463 void agent_schedule_callback(void (*callback
)(void *, void *, int),
5464 void *callback_ctx
, void *data
, int len
)
5466 struct agent_callback
*c
= snew(struct agent_callback
);
5467 c
->callback
= callback
;
5468 c
->callback_ctx
= callback_ctx
;
5471 PostMessage(hwnd
, WM_AGENT_CALLBACK
, 0, (LPARAM
)c
);