16 #define COMPILE_MULTIMON_STUBS
27 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
34 #define IDM_SHOWLOG 0x0010
35 #define IDM_NEWSESS 0x0020
36 #define IDM_DUPSESS 0x0030
37 #define IDM_RECONF 0x0040
38 #define IDM_CLRSB 0x0050
39 #define IDM_RESET 0x0060
40 #define IDM_HELP 0x0140
41 #define IDM_ABOUT 0x0150
42 #define IDM_SAVEDSESS 0x0160
43 #define IDM_COPYALL 0x0170
44 #define IDM_FULLSCREEN 0x0180
46 #define IDM_SESSLGP 0x0250 /* log type printable */
47 #define IDM_SESSLGA 0x0260 /* log type all chars */
48 #define IDM_SESSLGE 0x0270 /* log end */
50 #define IDM_SPECIAL_MIN 0x0400
51 #define IDM_SPECIAL_MAX 0x0800
53 #define IDM_SAVED_MIN 0x1000
54 #define IDM_SAVED_MAX 0x2000
56 #define WM_IGNORE_CLIP (WM_XUSER + 2)
57 #define WM_FULLSCR_ON_MAX (WM_XUSER + 3)
58 #define WM_AGENT_CALLBACK (WM_XUSER + 4)
60 /* Needed for Chinese support and apparently not always defined. */
62 #define VK_PROCESSKEY 0xE5
65 /* Mouse wheel support. */
67 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
70 #define WHEEL_DELTA 120
73 static Mouse_Button
translate_button(Mouse_Button button
);
74 static LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
75 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
76 unsigned char *output
);
77 static void cfgtopalette(void);
78 static void systopalette(void);
79 static void init_palette(void);
80 static void init_fonts(int, int);
81 static void another_font(int);
82 static void deinit_fonts(void);
83 static void set_input_locale(HKL
);
85 static int is_full_screen(void);
86 static void make_full_screen(void);
87 static void clear_full_screen(void);
88 static void flip_full_screen(void);
90 /* Window layout information */
91 static void reset_window(int);
92 static int extra_width
, extra_height
;
93 static int font_width
, font_height
, font_dualwidth
;
94 static int offset_width
, offset_height
;
95 static int was_zoomed
= 0;
96 static int prev_rows
, prev_cols
;
98 static int pending_netevent
= 0;
99 static WPARAM pend_netevent_wParam
= 0;
100 static LPARAM pend_netevent_lParam
= 0;
101 static void enact_pending_netevent(void);
102 static void flash_window(int mode
);
103 static void sys_cursor_update(void);
104 static int is_shift_pressed(void);
105 static int get_fullscreen_rect(RECT
* ss
);
107 static time_t last_movement
= 0;
109 static int caret_x
= -1, caret_y
= -1;
111 static int kbd_codepage
;
114 static Backend
*back
;
115 static void *backhandle
;
117 static struct unicode_data ucsdata
;
118 static int session_closed
;
120 static const struct telnet_special
*specials
;
121 static int specials_menu_position
;
123 Config cfg
; /* exported to windlg.c */
125 extern struct sesslist sesslist
; /* imported from windlg.c */
127 struct agent_callback
{
128 void (*callback
)(void *, void *, int);
134 #define FONT_NORMAL 0
136 #define FONT_UNDERLINE 2
137 #define FONT_BOLDUND 3
138 #define FONT_WIDE 0x04
139 #define FONT_HIGH 0x08
140 #define FONT_NARROW 0x10
142 #define FONT_OEM 0x20
143 #define FONT_OEMBOLD 0x21
144 #define FONT_OEMUND 0x22
145 #define FONT_OEMBOLDUND 0x23
147 #define FONT_MAXNO 0x2F
149 static HFONT fonts
[FONT_MAXNO
];
150 static LOGFONT lfont
;
151 static int fontflag
[FONT_MAXNO
];
153 BOLD_COLOURS
, BOLD_SHADOW
, BOLD_FONT
161 static COLORREF colours
[NCOLOURS
];
163 static LPLOGPALETTE logpal
;
164 static RGBTRIPLE defpal
[NCOLOURS
];
168 static HBITMAP caretbm
;
170 static int dbltime
, lasttime
, lastact
;
171 static Mouse_Button lastbtn
;
173 /* this allows xterm-style mouse handling. */
174 static int send_raw_mouse
= 0;
175 static int wheel_accumulator
= 0;
177 static char *window_name
, *icon_name
;
179 static int compose_state
= 0;
181 static int wsa_started
= 0;
183 static UINT wm_mousewheel
= WM_MOUSEWHEEL
;
185 /* Dummy routine, only required in plink. */
186 void ldisc_update(void *frontend
, int echo
, int edit
)
190 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
196 int guess_width
, guess_height
;
199 flags
= FLAG_VERBOSE
| FLAG_INTERACTIVE
;
201 winsock_ver
= MAKEWORD(1, 1);
202 if (WSAStartup(winsock_ver
, &wsadata
)) {
203 MessageBox(NULL
, "Unable to initialise WinSock", "WinSock Error",
204 MB_OK
| MB_ICONEXCLAMATION
);
207 if (LOBYTE(wsadata
.wVersion
) != 1 || HIBYTE(wsadata
.wVersion
) != 1) {
208 MessageBox(NULL
, "WinSock version is incompatible with 1.1",
209 "WinSock Error", MB_OK
| MB_ICONEXCLAMATION
);
214 /* WISHLIST: maybe allow config tweaking even if winsock not present? */
217 InitCommonControls();
219 /* Ensure a Maximize setting in Explorer doesn't maximise the
225 char *str
= dupprintf("%s Fatal Error", appname
);
226 MessageBox(NULL
, "Windows refuses to report a version",
227 str
, MB_OK
| MB_ICONEXCLAMATION
);
233 * If we're running a version of Windows that doesn't support
234 * WM_MOUSEWHEEL, find out what message number we should be
237 if (osVersion
.dwMajorVersion
< 4 ||
238 (osVersion
.dwMajorVersion
== 4 &&
239 osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
))
240 wm_mousewheel
= RegisterWindowMessage("MSWHEEL_ROLLMSG");
243 * See if we can find our Help file.
246 char b
[2048], *p
, *q
, *r
;
248 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
250 p
= strrchr(b
, '\\');
251 if (p
&& p
>= r
) r
= p
+1;
253 if (q
&& q
>= r
) r
= q
+1;
254 strcpy(r
, "putty.hlp");
255 if ( (fp
= fopen(b
, "r")) != NULL
) {
256 help_path
= dupstr(b
);
260 strcpy(r
, "putty.cnt");
261 if ( (fp
= fopen(b
, "r")) != NULL
) {
262 help_has_contents
= TRUE
;
265 help_has_contents
= FALSE
;
269 * Process the command line.
275 default_protocol
= be_default_protocol
;
276 /* Find the appropriate default port. */
279 default_port
= 0; /* illegal */
280 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
281 if (backends
[i
].protocol
== default_protocol
) {
282 default_port
= backends
[i
].backend
->default_port
;
286 cfg
.logtype
= LGTYP_NONE
;
288 do_defaults(NULL
, &cfg
);
293 * Process a couple of command-line options which are more
294 * easily dealt with before the line is broken up into
295 * words. These are the soon-to-be-defunct @sessionname and
296 * the internal-use-only &sharedmemoryhandle, neither of
297 * which are combined with anything else.
299 while (*p
&& isspace(*p
))
303 while (i
> 1 && isspace(p
[i
- 1]))
306 do_defaults(p
+ 1, &cfg
);
307 if (!*cfg
.host
&& !do_config()) {
311 } else if (*p
== '&') {
313 * An initial & means we've been given a command line
314 * containing the hex value of a HANDLE for a file
315 * mapping object, which we must then extract as a
320 if (sscanf(p
+ 1, "%p", &filemap
) == 1 &&
321 (cp
= MapViewOfFile(filemap
, FILE_MAP_READ
,
322 0, 0, sizeof(Config
))) != NULL
) {
325 CloseHandle(filemap
);
326 } else if (!do_config()) {
332 * Otherwise, break up the command line and deal with
338 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
340 for (i
= 0; i
< argc
; i
++) {
344 ret
= cmdline_process_param(p
, i
+1<argc?argv
[i
+1]:NULL
,
347 cmdline_error("option \"%s\" requires an argument", p
);
348 } else if (ret
== 2) {
349 i
++; /* skip next argument */
350 } else if (ret
== 1) {
351 continue; /* nothing further needs doing */
352 } else if (!strcmp(p
, "-cleanup")) {
354 * `putty -cleanup'. Remove all registry
355 * entries associated with PuTTY, and also find
356 * and delete the random seed file.
359 s1
= dupprintf("This procedure will remove ALL Registry\n"
360 "entries associated with %s, and will\n"
361 "also remove the random seed file.\n"
363 "THIS PROCESS WILL DESTROY YOUR SAVED\n"
364 "SESSIONS. Are you really sure you want\n"
365 "to continue?", appname
);
366 s2
= dupprintf("%s Warning", appname
);
367 if (MessageBox(NULL
, s1
, s2
,
368 MB_YESNO
| MB_ICONWARNING
) == IDYES
) {
374 } else if (*p
!= '-') {
378 * If we already have a host name, treat
379 * this argument as a port number. NB we
380 * have to treat this as a saved -P
381 * argument, so that it will be deferred
382 * until it's a good moment to run it.
384 int ret
= cmdline_process_param("-P", p
, 1, &cfg
);
386 } else if (!strncmp(q
, "telnet:", 7)) {
388 * If the hostname starts with "telnet:",
389 * set the protocol to Telnet and process
390 * the string as a Telnet URL.
395 if (q
[0] == '/' && q
[1] == '/')
397 cfg
.protocol
= PROT_TELNET
;
399 while (*p
&& *p
!= ':' && *p
!= '/')
408 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
409 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
413 * Otherwise, treat this argument as a host
416 while (*p
&& !isspace(*p
))
420 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
421 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
425 cmdline_error("unknown option \"%s\"", p
);
430 cmdline_run_saved(&cfg
);
432 if (!*cfg
.host
&& !do_config()) {
438 * Trim leading whitespace off the hostname if it's there.
441 int space
= strspn(cfg
.host
, " \t");
442 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
445 /* See if host is of the form user@host */
446 if (cfg
.host
[0] != '\0') {
447 char *atsign
= strchr(cfg
.host
, '@');
448 /* Make sure we're not overflowing the user field */
450 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
451 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
452 cfg
.username
[atsign
- cfg
.host
] = '\0';
454 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
459 * Trim a colon suffix off the hostname if it's there.
461 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
464 * Remove any remaining whitespace from the hostname.
468 while (cfg
.host
[p2
] != '\0') {
469 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
470 cfg
.host
[p1
] = cfg
.host
[p2
];
480 * Select protocol. This is farmed out into a table in a
481 * separate file to enable an ssh-free variant.
486 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
487 if (backends
[i
].protocol
== cfg
.protocol
) {
488 back
= backends
[i
].backend
;
492 char *str
= dupprintf("%s Internal Error", appname
);
493 MessageBox(NULL
, "Unsupported protocol number found",
494 str
, MB_OK
| MB_ICONEXCLAMATION
);
501 /* Check for invalid Port number (i.e. zero) */
503 char *str
= dupprintf("%s Internal Error", appname
);
504 MessageBox(NULL
, "Invalid Port Number",
505 str
, MB_OK
| MB_ICONEXCLAMATION
);
513 wndclass
.lpfnWndProc
= WndProc
;
514 wndclass
.cbClsExtra
= 0;
515 wndclass
.cbWndExtra
= 0;
516 wndclass
.hInstance
= inst
;
517 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
518 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
519 wndclass
.hbrBackground
= NULL
;
520 wndclass
.lpszMenuName
= NULL
;
521 wndclass
.lpszClassName
= appname
;
523 RegisterClass(&wndclass
);
528 memset(&ucsdata
, 0, sizeof(ucsdata
));
530 term
= term_init(&cfg
, &ucsdata
, NULL
);
531 logctx
= log_init(NULL
, &cfg
);
532 term_provide_logctx(term
, logctx
);
537 * Guess some defaults for the window size. This all gets
538 * updated later, so we don't really care too much. However, we
539 * do want the font width/height guesses to correspond to a
540 * large font rather than a small one...
547 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
548 guess_width
= extra_width
+ font_width
* term
->cols
;
549 guess_height
= extra_height
+ font_height
* term
->rows
;
552 get_fullscreen_rect(&r
);
553 if (guess_width
> r
.right
- r
.left
)
554 guess_width
= r
.right
- r
.left
;
555 if (guess_height
> r
.bottom
- r
.top
)
556 guess_height
= r
.bottom
- r
.top
;
560 int winmode
= WS_OVERLAPPEDWINDOW
| WS_VSCROLL
;
563 winmode
&= ~(WS_VSCROLL
);
564 if (cfg
.resize_action
== RESIZE_DISABLED
)
565 winmode
&= ~(WS_THICKFRAME
| WS_MAXIMIZEBOX
);
567 exwinmode
|= WS_EX_TOPMOST
;
569 exwinmode
|= WS_EX_CLIENTEDGE
;
570 hwnd
= CreateWindowEx(exwinmode
, appname
, appname
,
571 winmode
, CW_USEDEFAULT
, CW_USEDEFAULT
,
572 guess_width
, guess_height
,
573 NULL
, NULL
, inst
, NULL
);
577 * Initialise the fonts, simultaneously correcting the guesses
578 * for font_{width,height}.
583 * Correct the guesses for extra_{width,height}.
587 GetWindowRect(hwnd
, &wr
);
588 GetClientRect(hwnd
, &cr
);
589 offset_width
= offset_height
= cfg
.window_border
;
590 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
591 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
595 * Resize the window, now we know what size we _really_ want it
598 guess_width
= extra_width
+ font_width
* term
->cols
;
599 guess_height
= extra_height
+ font_height
* term
->rows
;
600 SetWindowPos(hwnd
, NULL
, 0, 0, guess_width
, guess_height
,
601 SWP_NOMOVE
| SWP_NOREDRAW
| SWP_NOZORDER
);
604 * Set up a caret bitmap, with no content.
608 int size
= (font_width
+ 15) / 16 * 2 * font_height
;
609 bits
= snewn(size
, char);
610 memset(bits
, 0, size
);
611 caretbm
= CreateBitmap(font_width
, font_height
, 1, 1, bits
);
614 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
617 * Initialise the scroll bar.
622 si
.cbSize
= sizeof(si
);
623 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
625 si
.nMax
= term
->rows
- 1;
626 si
.nPage
= term
->rows
;
628 SetScrollInfo(hwnd
, SB_VERT
, &si
, FALSE
);
632 * Start up the telnet connection.
636 char msg
[1024], *title
;
639 error
= back
->init(NULL
, &backhandle
, &cfg
,
640 cfg
.host
, cfg
.port
, &realhost
, cfg
.tcp_nodelay
);
641 back
->provide_logctx(backhandle
, logctx
);
643 char *str
= dupprintf("%s Error", appname
);
644 sprintf(msg
, "Unable to open connection to\n"
645 "%.800s\n" "%s", cfg
.host
, error
);
646 MessageBox(NULL
, msg
, str
, MB_ICONERROR
| MB_OK
);
650 window_name
= icon_name
= NULL
;
652 title
= cfg
.wintitle
;
654 sprintf(msg
, "%s - %s", realhost
, appname
);
658 set_title(NULL
, title
);
659 set_icon(NULL
, title
);
663 * Connect the terminal to the backend for resize purposes.
665 term_provide_resize_fn(term
, back
->size
, backhandle
);
668 * Set up a line discipline.
670 ldisc
= ldisc_create(&cfg
, term
, back
, backhandle
, NULL
);
672 session_closed
= FALSE
;
675 * Prepare the mouse handler.
677 lastact
= MA_NOTHING
;
678 lastbtn
= MBT_NOTHING
;
679 dbltime
= GetDoubleClickTime();
682 * Set up the session-control options on the system menu.
685 HMENU m
= GetSystemMenu(hwnd
, FALSE
);
690 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
691 specials_menu_position
= GetMenuItemCount(m
);
692 AppendMenu(m
, MF_ENABLED
, IDM_SHOWLOG
, "&Event Log");
693 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
694 AppendMenu(m
, MF_ENABLED
, IDM_NEWSESS
, "Ne&w Session...");
695 AppendMenu(m
, MF_ENABLED
, IDM_DUPSESS
, "&Duplicate Session");
697 get_sesslist(&sesslist
, TRUE
);
699 i
< ((sesslist
.nsessions
< 256) ? sesslist
.nsessions
: 256);
701 AppendMenu(s
, MF_ENABLED
, IDM_SAVED_MIN
+ (16 * i
),
702 sesslist
.sessions
[i
]);
703 AppendMenu(m
, MF_POPUP
| MF_ENABLED
, (UINT
) s
, "Sa&ved Sessions");
704 AppendMenu(m
, MF_ENABLED
, IDM_RECONF
, "Chan&ge Settings...");
705 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
706 AppendMenu(m
, MF_ENABLED
, IDM_COPYALL
, "C&opy All to Clipboard");
707 AppendMenu(m
, MF_ENABLED
, IDM_CLRSB
, "C&lear Scrollback");
708 AppendMenu(m
, MF_ENABLED
, IDM_RESET
, "Rese&t Terminal");
709 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
710 AppendMenu(m
, (cfg
.resize_action
== RESIZE_DISABLED
) ?
711 MF_GRAYED
: MF_ENABLED
, IDM_FULLSCREEN
, "&Full Screen");
712 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
714 AppendMenu(m
, MF_ENABLED
, IDM_HELP
, "&Help");
715 str
= dupprintf("&About %s", appname
);
716 AppendMenu(m
, MF_ENABLED
, IDM_ABOUT
, str
);
720 update_specials_menu(NULL
);
723 * Set up the initial input locale.
725 set_input_locale(GetKeyboardLayout(0));
728 * Open the initial log file if there is one.
733 * Finally show the window!
735 ShowWindow(hwnd
, show
);
736 SetForegroundWindow(hwnd
);
739 * Set the palette up.
745 term
->has_focus
= (GetForegroundWindow() == hwnd
);
748 if (GetMessage(&msg
, NULL
, 0, 0) == 1) {
749 int timer_id
= 0, long_timer
= 0;
751 while (msg
.message
!= WM_QUIT
) {
752 /* Sometimes DispatchMessage calls routines that use their own
753 * GetMessage loop, setup this timer so we get some control back.
755 * Also call term_update() from the timer so that if the host
756 * is sending data flat out we still do redraws.
758 if (timer_id
&& long_timer
) {
759 KillTimer(hwnd
, timer_id
);
760 long_timer
= timer_id
= 0;
763 timer_id
= SetTimer(hwnd
, 1, 20, NULL
);
764 if (!(IsWindow(logbox
) && IsDialogMessage(logbox
, &msg
)))
765 DispatchMessage(&msg
);
767 /* Make sure we blink everything that needs it. */
770 /* Send the paste buffer if there's anything to send */
773 /* If there's nothing new in the queue then we can do everything
774 * we've delayed, reading the socket, writing, and repainting
777 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
780 if (pending_netevent
) {
781 enact_pending_netevent();
783 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
787 /* Okay there is now nothing to do so we make sure the screen is
788 * completely up to date then tell windows to call us in a little
792 KillTimer(hwnd
, timer_id
);
796 if (GetCapture() != hwnd
||
798 !(cfg
.mouse_override
&& is_shift_pressed())))
803 flash_window(1); /* maintain */
805 /* The messages seem unreliable; especially if we're being tricky */
806 term
->has_focus
= (GetForegroundWindow() == hwnd
);
809 /* Hmm, term_update didn't want to do an update too soon ... */
810 timer_id
= SetTimer(hwnd
, 1, 50, NULL
);
811 else if (!term
->has_focus
)
812 timer_id
= SetTimer(hwnd
, 1, 500, NULL
);
814 timer_id
= SetTimer(hwnd
, 1, 100, NULL
);
817 /* There's no point rescanning everything in the message queue
818 * so we do an apparently unnecessary wait here
821 if (GetMessage(&msg
, NULL
, 0, 0) != 1)
826 cleanup_exit(msg
.wParam
); /* this doesn't return... */
827 return msg
.wParam
; /* ... but optimiser doesn't know */
833 void cleanup_exit(int code
)
846 if (cfg
.protocol
== PROT_SSH
) {
857 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
859 char *do_select(SOCKET skt
, int startup
)
864 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
865 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
870 return "do_select(): internal error (hwnd==NULL)";
871 if (WSAAsyncSelect(skt
, hwnd
, msg
, events
) == SOCKET_ERROR
) {
872 switch (WSAGetLastError()) {
874 return "Network is down";
876 return "WSAAsyncSelect(): unknown error";
883 * Update the Special Commands submenu.
885 void update_specials_menu(void *frontend
)
887 HMENU m
= GetSystemMenu(hwnd
, FALSE
);
888 int menu_already_exists
= (specials
!= NULL
);
891 specials
= back
->get_specials(backhandle
);
893 HMENU p
= CreateMenu();
894 for (i
= 0; specials
[i
].name
; i
++) {
895 assert(IDM_SPECIAL_MIN
+ 0x10 * i
< IDM_SPECIAL_MAX
);
896 if (*specials
[i
].name
)
897 AppendMenu(p
, MF_ENABLED
, IDM_SPECIAL_MIN
+ 0x10 * i
,
900 AppendMenu(p
, MF_SEPARATOR
, 0, 0);
902 if (menu_already_exists
)
903 DeleteMenu(m
, specials_menu_position
, MF_BYPOSITION
);
905 InsertMenu(m
, specials_menu_position
,
906 MF_BYPOSITION
| MF_SEPARATOR
, 0, 0);
907 InsertMenu(m
, specials_menu_position
,
908 MF_BYPOSITION
| MF_POPUP
| MF_ENABLED
,
909 (UINT
) p
, "Special Command");
914 * set or clear the "raw mouse message" mode
916 void set_raw_mouse_mode(void *frontend
, int activate
)
918 activate
= activate
&& !cfg
.no_mouse_rep
;
919 send_raw_mouse
= activate
;
920 SetCursor(LoadCursor(NULL
, activate ? IDC_ARROW
: IDC_IBEAM
));
924 * Print a message box and close the connection.
926 void connection_fatal(void *frontend
, char *fmt
, ...)
929 char *stuff
, morestuff
[100];
932 stuff
= dupvprintf(fmt
, ap
);
934 sprintf(morestuff
, "%.70s Fatal Error", appname
);
935 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
938 if (cfg
.close_on_exit
== FORCE_ON
)
941 session_closed
= TRUE
;
942 sprintf(morestuff
, "%.70s (inactive)", appname
);
943 set_icon(NULL
, morestuff
);
944 set_title(NULL
, morestuff
);
949 * Report an error at the command-line parsing stage.
951 void cmdline_error(char *fmt
, ...)
954 char *stuff
, morestuff
[100];
957 stuff
= dupvprintf(fmt
, ap
);
959 sprintf(morestuff
, "%.70s Command Line Error", appname
);
960 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
966 * Actually do the job requested by a WM_NETEVENT
968 static void enact_pending_netevent(void)
970 static int reentering
= 0;
971 extern int select_result(WPARAM
, LPARAM
);
975 return; /* don't unpend the pending */
977 pending_netevent
= FALSE
;
980 ret
= select_result(pend_netevent_wParam
, pend_netevent_lParam
);
983 if (ret
== 0 && !session_closed
) {
984 /* Abnormal exits will already have set session_closed and taken
985 * appropriate action. */
986 if (cfg
.close_on_exit
== FORCE_ON
||
987 cfg
.close_on_exit
== AUTO
) PostQuitMessage(0);
990 session_closed
= TRUE
;
991 sprintf(morestuff
, "%.70s (inactive)", appname
);
992 set_icon(NULL
, morestuff
);
993 set_title(NULL
, morestuff
);
994 MessageBox(hwnd
, "Connection closed by remote host",
995 appname
, MB_OK
| MB_ICONINFORMATION
);
1001 * Copy the colour palette from the configuration data into defpal.
1002 * This is non-trivial because the colour indices are different.
1004 static void cfgtopalette(void)
1007 static const int ww
[] = {
1008 6, 7, 8, 9, 10, 11, 12, 13,
1009 14, 15, 16, 17, 18, 19, 20, 21,
1010 0, 1, 2, 3, 4, 4, 5, 5
1013 for (i
= 0; i
< 24; i
++) {
1015 defpal
[i
].rgbtRed
= cfg
.colours
[w
][0];
1016 defpal
[i
].rgbtGreen
= cfg
.colours
[w
][1];
1017 defpal
[i
].rgbtBlue
= cfg
.colours
[w
][2];
1020 /* Override with system colours if appropriate */
1021 if (cfg
.system_colour
)
1026 * Override bit of defpal with colours from the system.
1027 * (NB that this takes a copy the system colours at the time this is called,
1028 * so subsequent colour scheme changes don't take effect. To fix that we'd
1029 * probably want to be using GetSysColorBrush() and the like.)
1031 static void systopalette(void)
1034 static const struct { int nIndex
; int norm
; int bold
; } or[] =
1036 { COLOR_WINDOWTEXT
, 16, 17 }, /* Default Foreground */
1037 { COLOR_WINDOW
, 18, 19 }, /* Default Background */
1038 { COLOR_HIGHLIGHTTEXT
, 20, 21 }, /* Cursor Text */
1039 { COLOR_HIGHLIGHT
, 22, 23 }, /* Cursor Colour */
1042 for (i
= 0; i
< (sizeof(or)/sizeof(or[0])); i
++) {
1043 COLORREF colour
= GetSysColor(or[i
].nIndex
);
1044 defpal
[or[i
].norm
].rgbtRed
=
1045 defpal
[or[i
].bold
].rgbtRed
= GetRValue(colour
);
1046 defpal
[or[i
].norm
].rgbtGreen
=
1047 defpal
[or[i
].bold
].rgbtGreen
= GetGValue(colour
);
1048 defpal
[or[i
].norm
].rgbtBlue
=
1049 defpal
[or[i
].bold
].rgbtBlue
= GetBValue(colour
);
1054 * Set up the colour palette.
1056 static void init_palette(void)
1059 HDC hdc
= GetDC(hwnd
);
1061 if (cfg
.try_palette
&& GetDeviceCaps(hdc
, RASTERCAPS
) & RC_PALETTE
) {
1063 * This is a genuine case where we must use smalloc
1064 * because the snew macros can't cope.
1066 logpal
= smalloc(sizeof(*logpal
)
1067 - sizeof(logpal
->palPalEntry
)
1068 + NCOLOURS
* sizeof(PALETTEENTRY
));
1069 logpal
->palVersion
= 0x300;
1070 logpal
->palNumEntries
= NCOLOURS
;
1071 for (i
= 0; i
< NCOLOURS
; i
++) {
1072 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
1073 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
1074 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
1075 logpal
->palPalEntry
[i
].peFlags
= PC_NOCOLLAPSE
;
1077 pal
= CreatePalette(logpal
);
1079 SelectPalette(hdc
, pal
, FALSE
);
1080 RealizePalette(hdc
);
1081 SelectPalette(hdc
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
1084 ReleaseDC(hwnd
, hdc
);
1087 for (i
= 0; i
< NCOLOURS
; i
++)
1088 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
1089 defpal
[i
].rgbtGreen
,
1090 defpal
[i
].rgbtBlue
);
1092 for (i
= 0; i
< NCOLOURS
; i
++)
1093 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
1094 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
1098 * Initialise all the fonts we will need initially. There may be as many as
1099 * three or as few as one. The other (poentially) twentyone fonts are done
1100 * if/when they are needed.
1104 * - check the font width and height, correcting our guesses if
1107 * - verify that the bold font is the same width as the ordinary
1108 * one, and engage shadow bolding if not.
1110 * - verify that the underlined font is the same width as the
1111 * ordinary one (manual underlining by means of line drawing can
1112 * be done in a pinch).
1114 static void init_fonts(int pick_width
, int pick_height
)
1121 int fw_dontcare
, fw_bold
;
1123 for (i
= 0; i
< FONT_MAXNO
; i
++)
1126 bold_mode
= cfg
.bold_colour ? BOLD_COLOURS
: BOLD_FONT
;
1127 und_mode
= UND_FONT
;
1129 if (cfg
.font
.isbold
) {
1130 fw_dontcare
= FW_BOLD
;
1133 fw_dontcare
= FW_DONTCARE
;
1140 font_height
= pick_height
;
1142 font_height
= cfg
.font
.height
;
1143 if (font_height
> 0) {
1145 -MulDiv(font_height
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1148 font_width
= pick_width
;
1150 #define f(i,c,w,u) \
1151 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1152 c, OUT_DEFAULT_PRECIS, \
1153 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
1154 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1156 f(FONT_NORMAL
, cfg
.font
.charset
, fw_dontcare
, FALSE
);
1158 lfont
.lfHeight
= font_height
;
1159 lfont
.lfWidth
= font_width
;
1160 lfont
.lfEscapement
= 0;
1161 lfont
.lfOrientation
= 0;
1162 lfont
.lfWeight
= fw_dontcare
;
1163 lfont
.lfItalic
= FALSE
;
1164 lfont
.lfUnderline
= FALSE
;
1165 lfont
.lfStrikeOut
= FALSE
;
1166 lfont
.lfCharSet
= cfg
.font
.charset
;
1167 lfont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1168 lfont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1169 lfont
.lfQuality
= DEFAULT_QUALITY
;
1170 lfont
.lfPitchAndFamily
= FIXED_PITCH
| FF_DONTCARE
;
1171 strncpy(lfont
.lfFaceName
, cfg
.font
.name
, LF_FACESIZE
);
1173 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
1174 GetTextMetrics(hdc
, &tm
);
1176 if (pick_width
== 0 || pick_height
== 0) {
1177 font_height
= tm
.tmHeight
;
1178 font_width
= tm
.tmAveCharWidth
;
1180 font_dualwidth
= (tm
.tmAveCharWidth
!= tm
.tmMaxCharWidth
);
1182 #ifdef RDB_DEBUG_PATCH
1183 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1184 tm
.tmHeight
, tm
.tmAveCharWidth
, tm
.tmMaxCharWidth
);
1189 DWORD cset
= tm
.tmCharSet
;
1190 memset(&info
, 0xFF, sizeof(info
));
1192 /* !!! Yes the next line is right */
1193 if (cset
== OEM_CHARSET
)
1194 ucsdata
.font_codepage
= GetOEMCP();
1196 if (TranslateCharsetInfo ((DWORD
*) cset
, &info
, TCI_SRCCHARSET
))
1197 ucsdata
.font_codepage
= info
.ciACP
;
1199 ucsdata
.font_codepage
= -1;
1201 GetCPInfo(ucsdata
.font_codepage
, &cpinfo
);
1202 ucsdata
.dbcs_screenfont
= (cpinfo
.MaxCharSize
> 1);
1205 f(FONT_UNDERLINE
, cfg
.font
.charset
, fw_dontcare
, TRUE
);
1208 * Some fonts, e.g. 9-pt Courier, draw their underlines
1209 * outside their character cell. We successfully prevent
1210 * screen corruption by clipping the text output, but then
1211 * we lose the underline completely. Here we try to work
1212 * out whether this is such a font, and if it is, we set a
1213 * flag that causes underlines to be drawn by hand.
1215 * Having tried other more sophisticated approaches (such
1216 * as examining the TEXTMETRIC structure or requesting the
1217 * height of a string), I think we'll do this the brute
1218 * force way: we create a small bitmap, draw an underlined
1219 * space on it, and test to see whether any pixels are
1220 * foreground-coloured. (Since we expect the underline to
1221 * go all the way across the character cell, we only search
1222 * down a single column of the bitmap, half way across.)
1226 HBITMAP und_bm
, und_oldbm
;
1230 und_dc
= CreateCompatibleDC(hdc
);
1231 und_bm
= CreateCompatibleBitmap(hdc
, font_width
, font_height
);
1232 und_oldbm
= SelectObject(und_dc
, und_bm
);
1233 SelectObject(und_dc
, fonts
[FONT_UNDERLINE
]);
1234 SetTextAlign(und_dc
, TA_TOP
| TA_LEFT
| TA_NOUPDATECP
);
1235 SetTextColor(und_dc
, RGB(255, 255, 255));
1236 SetBkColor(und_dc
, RGB(0, 0, 0));
1237 SetBkMode(und_dc
, OPAQUE
);
1238 ExtTextOut(und_dc
, 0, 0, ETO_OPAQUE
, NULL
, " ", 1, NULL
);
1240 for (i
= 0; i
< font_height
; i
++) {
1241 c
= GetPixel(und_dc
, font_width
/ 2, i
);
1242 if (c
!= RGB(0, 0, 0))
1245 SelectObject(und_dc
, und_oldbm
);
1246 DeleteObject(und_bm
);
1249 und_mode
= UND_LINE
;
1250 DeleteObject(fonts
[FONT_UNDERLINE
]);
1251 fonts
[FONT_UNDERLINE
] = 0;
1255 if (bold_mode
== BOLD_FONT
) {
1256 f(FONT_BOLD
, cfg
.font
.charset
, fw_bold
, FALSE
);
1260 descent
= tm
.tmAscent
+ 1;
1261 if (descent
>= font_height
)
1262 descent
= font_height
- 1;
1264 for (i
= 0; i
< 3; i
++) {
1266 if (SelectObject(hdc
, fonts
[i
]) && GetTextMetrics(hdc
, &tm
))
1267 fontsize
[i
] = tm
.tmAveCharWidth
+ 256 * tm
.tmHeight
;
1274 ReleaseDC(hwnd
, hdc
);
1276 if (fontsize
[FONT_UNDERLINE
] != fontsize
[FONT_NORMAL
]) {
1277 und_mode
= UND_LINE
;
1278 DeleteObject(fonts
[FONT_UNDERLINE
]);
1279 fonts
[FONT_UNDERLINE
] = 0;
1282 if (bold_mode
== BOLD_FONT
&&
1283 fontsize
[FONT_BOLD
] != fontsize
[FONT_NORMAL
]) {
1284 bold_mode
= BOLD_SHADOW
;
1285 DeleteObject(fonts
[FONT_BOLD
]);
1286 fonts
[FONT_BOLD
] = 0;
1288 fontflag
[0] = fontflag
[1] = fontflag
[2] = 1;
1290 init_ucs(&cfg
, &ucsdata
);
1293 static void another_font(int fontno
)
1296 int fw_dontcare
, fw_bold
;
1300 if (fontno
< 0 || fontno
>= FONT_MAXNO
|| fontflag
[fontno
])
1303 basefont
= (fontno
& ~(FONT_BOLDUND
));
1304 if (basefont
!= fontno
&& !fontflag
[basefont
])
1305 another_font(basefont
);
1307 if (cfg
.font
.isbold
) {
1308 fw_dontcare
= FW_BOLD
;
1311 fw_dontcare
= FW_DONTCARE
;
1315 c
= cfg
.font
.charset
;
1321 if (fontno
& FONT_WIDE
)
1323 if (fontno
& FONT_NARROW
)
1325 if (fontno
& FONT_OEM
)
1327 if (fontno
& FONT_BOLD
)
1329 if (fontno
& FONT_UNDERLINE
)
1333 CreateFont(font_height
* (1 + !!(fontno
& FONT_HIGH
)), x
, 0, 0, w
,
1334 FALSE
, u
, FALSE
, c
, OUT_DEFAULT_PRECIS
,
1335 CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
1336 FIXED_PITCH
| FF_DONTCARE
, s
);
1338 fontflag
[fontno
] = 1;
1341 static void deinit_fonts(void)
1344 for (i
= 0; i
< FONT_MAXNO
; i
++) {
1346 DeleteObject(fonts
[i
]);
1352 void request_resize(void *frontend
, int w
, int h
)
1356 /* If the window is maximized supress resizing attempts */
1357 if (IsZoomed(hwnd
)) {
1358 if (cfg
.resize_action
== RESIZE_TERM
)
1362 if (cfg
.resize_action
== RESIZE_DISABLED
) return;
1363 if (h
== term
->rows
&& w
== term
->cols
) return;
1365 /* Sanity checks ... */
1367 static int first_time
= 1;
1370 switch (first_time
) {
1372 /* Get the size of the screen */
1373 if (get_fullscreen_rect(&ss
))
1374 /* first_time = 0 */ ;
1380 /* Make sure the values are sane */
1381 width
= (ss
.right
- ss
.left
- extra_width
) / 4;
1382 height
= (ss
.bottom
- ss
.top
- extra_height
) / 6;
1384 if (w
> width
|| h
> height
)
1393 term_size(term
, h
, w
, cfg
.savelines
);
1395 if (cfg
.resize_action
!= RESIZE_FONT
&& !IsZoomed(hwnd
)) {
1396 width
= extra_width
+ font_width
* w
;
1397 height
= extra_height
+ font_height
* h
;
1399 SetWindowPos(hwnd
, NULL
, 0, 0, width
, height
,
1400 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1401 SWP_NOMOVE
| SWP_NOZORDER
);
1405 InvalidateRect(hwnd
, NULL
, TRUE
);
1408 static void reset_window(int reinit
) {
1410 * This function decides how to resize or redraw when the
1411 * user changes something.
1413 * This function doesn't like to change the terminal size but if the
1414 * font size is locked that may be it's only soluion.
1416 int win_width
, win_height
;
1419 #ifdef RDB_DEBUG_PATCH
1420 debug((27, "reset_window()"));
1423 /* Current window sizes ... */
1424 GetWindowRect(hwnd
, &wr
);
1425 GetClientRect(hwnd
, &cr
);
1427 win_width
= cr
.right
- cr
.left
;
1428 win_height
= cr
.bottom
- cr
.top
;
1430 if (cfg
.resize_action
== RESIZE_DISABLED
) reinit
= 2;
1432 /* Are we being forced to reload the fonts ? */
1434 #ifdef RDB_DEBUG_PATCH
1435 debug((27, "reset_window() -- Forced deinit"));
1441 /* Oh, looks like we're minimised */
1442 if (win_width
== 0 || win_height
== 0)
1445 /* Is the window out of position ? */
1447 (offset_width
!= (win_width
-font_width
*term
->cols
)/2 ||
1448 offset_height
!= (win_height
-font_height
*term
->rows
)/2) ){
1449 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1450 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1451 InvalidateRect(hwnd
, NULL
, TRUE
);
1452 #ifdef RDB_DEBUG_PATCH
1453 debug((27, "reset_window() -> Reposition terminal"));
1457 if (IsZoomed(hwnd
)) {
1458 /* We're fullscreen, this means we must not change the size of
1459 * the window so it's the font size or the terminal itself.
1462 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
;
1463 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
;
1465 if (cfg
.resize_action
!= RESIZE_TERM
) {
1466 if ( font_width
!= win_width
/term
->cols
||
1467 font_height
!= win_height
/term
->rows
) {
1469 init_fonts(win_width
/term
->cols
, win_height
/term
->rows
);
1470 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1471 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1472 InvalidateRect(hwnd
, NULL
, TRUE
);
1473 #ifdef RDB_DEBUG_PATCH
1474 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1475 font_width
, font_height
));
1479 if ( font_width
!= win_width
/term
->cols
||
1480 font_height
!= win_height
/term
->rows
) {
1481 /* Our only choice at this point is to change the
1482 * size of the terminal; Oh well.
1484 term_size(term
, win_height
/font_height
, win_width
/font_width
,
1486 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1487 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1488 InvalidateRect(hwnd
, NULL
, TRUE
);
1489 #ifdef RDB_DEBUG_PATCH
1490 debug((27, "reset_window() -> Zoomed term_size"));
1497 /* Hmm, a force re-init means we should ignore the current window
1498 * so we resize to the default font size.
1501 #ifdef RDB_DEBUG_PATCH
1502 debug((27, "reset_window() -> Forced re-init"));
1505 offset_width
= offset_height
= cfg
.window_border
;
1506 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1507 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1509 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1510 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1512 /* If this is too large windows will resize it to the maximum
1513 * allowed window size, we will then be back in here and resize
1514 * the font or terminal to fit.
1516 SetWindowPos(hwnd
, NULL
, 0, 0,
1517 font_width
*term
->cols
+ extra_width
,
1518 font_height
*term
->rows
+ extra_height
,
1519 SWP_NOMOVE
| SWP_NOZORDER
);
1522 InvalidateRect(hwnd
, NULL
, TRUE
);
1526 /* Okay the user doesn't want us to change the font so we try the
1527 * window. But that may be too big for the screen which forces us
1528 * to change the terminal.
1530 if ((cfg
.resize_action
== RESIZE_TERM
&& reinit
<=0) ||
1531 (cfg
.resize_action
== RESIZE_EITHER
&& reinit
<0) ||
1533 offset_width
= offset_height
= cfg
.window_border
;
1534 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1535 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1537 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1538 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1543 get_fullscreen_rect(&ss
);
1545 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1546 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1549 if ( term
->rows
> height
|| term
->cols
> width
) {
1550 if (cfg
.resize_action
== RESIZE_EITHER
) {
1551 /* Make the font the biggest we can */
1552 if (term
->cols
> width
)
1553 font_width
= (ss
.right
- ss
.left
- extra_width
)
1555 if (term
->rows
> height
)
1556 font_height
= (ss
.bottom
- ss
.top
- extra_height
)
1560 init_fonts(font_width
, font_height
);
1562 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1563 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1565 if ( height
> term
->rows
) height
= term
->rows
;
1566 if ( width
> term
->cols
) width
= term
->cols
;
1567 term_size(term
, height
, width
, cfg
.savelines
);
1568 #ifdef RDB_DEBUG_PATCH
1569 debug((27, "reset_window() -> term resize to (%d,%d)",
1575 SetWindowPos(hwnd
, NULL
, 0, 0,
1576 font_width
*term
->cols
+ extra_width
,
1577 font_height
*term
->rows
+ extra_height
,
1578 SWP_NOMOVE
| SWP_NOZORDER
);
1580 InvalidateRect(hwnd
, NULL
, TRUE
);
1581 #ifdef RDB_DEBUG_PATCH
1582 debug((27, "reset_window() -> window resize to (%d,%d)",
1583 font_width
*term
->cols
+ extra_width
,
1584 font_height
*term
->rows
+ extra_height
));
1590 /* We're allowed to or must change the font but do we want to ? */
1592 if (font_width
!= (win_width
-cfg
.window_border
*2)/term
->cols
||
1593 font_height
!= (win_height
-cfg
.window_border
*2)/term
->rows
) {
1596 init_fonts((win_width
-cfg
.window_border
*2)/term
->cols
,
1597 (win_height
-cfg
.window_border
*2)/term
->rows
);
1598 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1599 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1601 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+offset_width
*2;
1602 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1604 InvalidateRect(hwnd
, NULL
, TRUE
);
1605 #ifdef RDB_DEBUG_PATCH
1606 debug((25, "reset_window() -> font resize to (%d,%d)",
1607 font_width
, font_height
));
1612 static void set_input_locale(HKL kl
)
1616 GetLocaleInfo(LOWORD(kl
), LOCALE_IDEFAULTANSICODEPAGE
,
1617 lbuf
, sizeof(lbuf
));
1619 kbd_codepage
= atoi(lbuf
);
1622 static void click(Mouse_Button b
, int x
, int y
, int shift
, int ctrl
, int alt
)
1624 int thistime
= GetMessageTime();
1626 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
)) {
1627 lastbtn
= MBT_NOTHING
;
1628 term_mouse(term
, b
, translate_button(b
), MA_CLICK
,
1629 x
, y
, shift
, ctrl
, alt
);
1633 if (lastbtn
== b
&& thistime
- lasttime
< dbltime
) {
1634 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
1635 lastact
== MA_2CLK ? MA_3CLK
:
1636 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
1641 if (lastact
!= MA_NOTHING
)
1642 term_mouse(term
, b
, translate_button(b
), lastact
,
1643 x
, y
, shift
, ctrl
, alt
);
1644 lasttime
= thistime
;
1648 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1649 * into a cooked one (SELECT, EXTEND, PASTE).
1651 static Mouse_Button
translate_button(Mouse_Button button
)
1653 if (button
== MBT_LEFT
)
1655 if (button
== MBT_MIDDLE
)
1656 return cfg
.mouse_is_xterm ? MBT_PASTE
: MBT_EXTEND
;
1657 if (button
== MBT_RIGHT
)
1658 return cfg
.mouse_is_xterm ? MBT_EXTEND
: MBT_PASTE
;
1659 return 0; /* shouldn't happen */
1662 static void show_mouseptr(int show
)
1664 static int cursor_visible
= 1;
1665 if (!cfg
.hide_mouseptr
) /* override if this feature disabled */
1667 if (cursor_visible
&& !show
)
1669 else if (!cursor_visible
&& show
)
1671 cursor_visible
= show
;
1674 static int is_alt_pressed(void)
1677 int r
= GetKeyboardState(keystate
);
1680 if (keystate
[VK_MENU
] & 0x80)
1682 if (keystate
[VK_RMENU
] & 0x80)
1687 static int is_shift_pressed(void)
1690 int r
= GetKeyboardState(keystate
);
1693 if (keystate
[VK_SHIFT
] & 0x80)
1698 static int resizing
;
1700 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1701 WPARAM wParam
, LPARAM lParam
)
1704 static int ignore_clip
= FALSE
;
1705 static int need_backend_resize
= FALSE
;
1706 static int fullscr_on_max
= FALSE
;
1710 if (pending_netevent
)
1711 enact_pending_netevent();
1712 if (GetCapture() != hwnd
||
1713 (send_raw_mouse
&& !(cfg
.mouse_override
&& is_shift_pressed())))
1719 if (cfg
.ping_interval
> 0) {
1722 if (now
- last_movement
> cfg
.ping_interval
) {
1723 back
->special(backhandle
, TS_PING
);
1724 last_movement
= now
;
1727 net_pending_errors();
1735 str
= dupprintf("%s Exit Confirmation", appname
);
1736 if (!cfg
.warn_on_close
|| session_closed
||
1738 "Are you sure you want to close this session?",
1739 str
, MB_ICONWARNING
| MB_OKCANCEL
) == IDOK
)
1740 DestroyWindow(hwnd
);
1749 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1761 PROCESS_INFORMATION pi
;
1762 HANDLE filemap
= NULL
;
1764 if (wParam
== IDM_DUPSESS
) {
1766 * Allocate a file-mapping memory chunk for the
1769 SECURITY_ATTRIBUTES sa
;
1772 sa
.nLength
= sizeof(sa
);
1773 sa
.lpSecurityDescriptor
= NULL
;
1774 sa
.bInheritHandle
= TRUE
;
1775 filemap
= CreateFileMapping((HANDLE
) 0xFFFFFFFF,
1778 0, sizeof(Config
), NULL
);
1780 p
= (Config
*) MapViewOfFile(filemap
,
1782 0, 0, sizeof(Config
));
1784 *p
= cfg
; /* structure copy */
1788 sprintf(c
, "putty &%p", filemap
);
1790 } else if (wParam
== IDM_SAVEDSESS
) {
1791 if ((lParam
- IDM_SAVED_MIN
) / 16 < sesslist
.nsessions
) {
1793 sesslist
.sessions
[(lParam
- IDM_SAVED_MIN
) / 16];
1794 cl
= snewn(16 + strlen(session
), char);
1795 /* 8, but play safe */
1798 /* not a very important failure mode */
1800 sprintf(cl
, "putty @%s", session
);
1808 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
1810 si
.lpReserved
= NULL
;
1811 si
.lpDesktop
= NULL
;
1815 si
.lpReserved2
= NULL
;
1816 CreateProcess(b
, cl
, NULL
, NULL
, TRUE
,
1817 NORMAL_PRIORITY_CLASS
, NULL
, NULL
, &si
, &pi
);
1820 CloseHandle(filemap
);
1830 GetWindowText(hwnd
, cfg
.wintitle
, sizeof(cfg
.wintitle
));
1833 if (!do_reconfig(hwnd
))
1837 /* Disable full-screen if resizing forbidden */
1838 HMENU m
= GetSystemMenu (hwnd
, FALSE
);
1839 EnableMenuItem(m
, IDM_FULLSCREEN
, MF_BYCOMMAND
|
1840 (cfg
.resize_action
== RESIZE_DISABLED
)
1841 ? MF_GRAYED
: MF_ENABLED
);
1842 /* Gracefully unzoom if necessary */
1843 if (IsZoomed(hwnd
) &&
1844 (cfg
.resize_action
== RESIZE_DISABLED
)) {
1845 ShowWindow(hwnd
, SW_RESTORE
);
1849 /* Pass new config data to the logging module */
1850 log_reconfig(logctx
, &cfg
);
1854 * Flush the line discipline's edit buffer in the
1855 * case where local editing has just been disabled.
1857 ldisc_send(ldisc
, NULL
, 0, 0);
1865 /* Pass new config data to the terminal */
1866 term_reconfig(term
, &cfg
);
1868 /* Pass new config data to the back end */
1869 back
->reconfig(backhandle
, &cfg
);
1871 /* Screen size changed ? */
1872 if (cfg
.height
!= prev_cfg
.height
||
1873 cfg
.width
!= prev_cfg
.width
||
1874 cfg
.savelines
!= prev_cfg
.savelines
||
1875 cfg
.resize_action
== RESIZE_FONT
||
1876 (cfg
.resize_action
== RESIZE_EITHER
&& IsZoomed(hwnd
)) ||
1877 cfg
.resize_action
== RESIZE_DISABLED
)
1878 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
1880 /* Enable or disable the scroll bar, etc */
1882 LONG nflg
, flag
= GetWindowLong(hwnd
, GWL_STYLE
);
1883 LONG nexflag
, exflag
=
1884 GetWindowLong(hwnd
, GWL_EXSTYLE
);
1887 if (cfg
.alwaysontop
!= prev_cfg
.alwaysontop
) {
1888 if (cfg
.alwaysontop
) {
1889 nexflag
|= WS_EX_TOPMOST
;
1890 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
1891 SWP_NOMOVE
| SWP_NOSIZE
);
1893 nexflag
&= ~(WS_EX_TOPMOST
);
1894 SetWindowPos(hwnd
, HWND_NOTOPMOST
, 0, 0, 0, 0,
1895 SWP_NOMOVE
| SWP_NOSIZE
);
1898 if (cfg
.sunken_edge
)
1899 nexflag
|= WS_EX_CLIENTEDGE
;
1901 nexflag
&= ~(WS_EX_CLIENTEDGE
);
1904 if (is_full_screen() ?
1905 cfg
.scrollbar_in_fullscreen
: cfg
.scrollbar
)
1908 nflg
&= ~WS_VSCROLL
;
1910 if (cfg
.resize_action
== RESIZE_DISABLED
||
1912 nflg
&= ~WS_THICKFRAME
;
1914 nflg
|= WS_THICKFRAME
;
1916 if (cfg
.resize_action
== RESIZE_DISABLED
)
1917 nflg
&= ~WS_MAXIMIZEBOX
;
1919 nflg
|= WS_MAXIMIZEBOX
;
1921 if (nflg
!= flag
|| nexflag
!= exflag
) {
1923 SetWindowLong(hwnd
, GWL_STYLE
, nflg
);
1924 if (nexflag
!= exflag
)
1925 SetWindowLong(hwnd
, GWL_EXSTYLE
, nexflag
);
1927 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
1928 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1929 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1937 if (cfg
.resize_action
== RESIZE_DISABLED
&& IsZoomed(hwnd
)) {
1942 set_title(NULL
, cfg
.wintitle
);
1943 if (IsIconic(hwnd
)) {
1945 cfg
.win_name_always ? window_name
:
1949 if (strcmp(cfg
.font
.name
, prev_cfg
.font
.name
) != 0 ||
1950 strcmp(cfg
.line_codepage
, prev_cfg
.line_codepage
) != 0 ||
1951 cfg
.font
.isbold
!= prev_cfg
.font
.isbold
||
1952 cfg
.font
.height
!= prev_cfg
.font
.height
||
1953 cfg
.font
.charset
!= prev_cfg
.font
.charset
||
1954 cfg
.vtmode
!= prev_cfg
.vtmode
||
1955 cfg
.bold_colour
!= prev_cfg
.bold_colour
||
1956 cfg
.resize_action
== RESIZE_DISABLED
||
1957 cfg
.resize_action
== RESIZE_EITHER
||
1958 (cfg
.resize_action
!= prev_cfg
.resize_action
))
1961 InvalidateRect(hwnd
, NULL
, TRUE
);
1962 reset_window(init_lvl
);
1963 net_pending_errors();
1974 ldisc_send(ldisc
, NULL
, 0, 0);
1980 WinHelp(hwnd
, help_path
,
1981 help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
1985 * We get this if the System menu has been activated
1992 * We get this if the System menu has been activated
1993 * using the keyboard. This might happen from within
1994 * TranslateKey, in which case it really wants to be
1995 * followed by a `space' character to actually _bring
1996 * the menu up_ rather than just sitting there in
1997 * `ready to appear' state.
1999 show_mouseptr(1); /* make sure pointer is visible */
2001 PostMessage(hwnd
, WM_CHAR
, ' ', 0);
2003 case IDM_FULLSCREEN
:
2007 if (wParam
>= IDM_SAVED_MIN
&& wParam
<= IDM_SAVED_MAX
) {
2008 SendMessage(hwnd
, WM_SYSCOMMAND
, IDM_SAVEDSESS
, wParam
);
2010 if (wParam
>= IDM_SPECIAL_MIN
&& wParam
<= IDM_SPECIAL_MAX
) {
2011 int i
= (wParam
- IDM_SPECIAL_MIN
) / 0x10;
2014 * Ensure we haven't been sent a bogus SYSCOMMAND
2015 * which would cause us to reference invalid memory
2016 * and crash. Perhaps I'm just too paranoid here.
2018 for (j
= 0; j
< i
; j
++)
2019 if (!specials
|| !specials
[j
].name
)
2022 back
->special(backhandle
, specials
[i
].code
);
2023 net_pending_errors();
2029 #define X_POS(l) ((int)(short)LOWORD(l))
2030 #define Y_POS(l) ((int)(short)HIWORD(l))
2032 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2033 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2034 case WM_LBUTTONDOWN
:
2035 case WM_MBUTTONDOWN
:
2036 case WM_RBUTTONDOWN
:
2044 case WM_LBUTTONDOWN
:
2048 case WM_MBUTTONDOWN
:
2049 button
= MBT_MIDDLE
;
2052 case WM_RBUTTONDOWN
:
2061 button
= MBT_MIDDLE
;
2069 button
= press
= 0; /* shouldn't happen */
2073 * Special case: in full-screen mode, if the left
2074 * button is clicked in the very top left corner of the
2075 * window, we put up the System menu instead of doing
2078 if (is_full_screen() && press
&& button
== MBT_LEFT
&&
2079 X_POS(lParam
) == 0 && Y_POS(lParam
) == 0) {
2080 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MOUSEMENU
, 0);
2085 TO_CHR_X(X_POS(lParam
)), TO_CHR_Y(Y_POS(lParam
)),
2086 wParam
& MK_SHIFT
, wParam
& MK_CONTROL
,
2090 term_mouse(term
, button
, translate_button(button
), MA_RELEASE
,
2091 TO_CHR_X(X_POS(lParam
)),
2092 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2093 wParam
& MK_CONTROL
, is_alt_pressed());
2101 * Add the mouse position and message time to the random
2104 noise_ultralight(lParam
);
2106 if (wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
) &&
2107 GetCapture() == hwnd
) {
2109 if (wParam
& MK_LBUTTON
)
2111 else if (wParam
& MK_MBUTTON
)
2115 term_mouse(term
, b
, translate_button(b
), MA_DRAG
,
2116 TO_CHR_X(X_POS(lParam
)),
2117 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2118 wParam
& MK_CONTROL
, is_alt_pressed());
2121 case WM_NCMOUSEMOVE
:
2123 noise_ultralight(lParam
);
2125 case WM_IGNORE_CLIP
:
2126 ignore_clip
= wParam
; /* don't panic on DESTROYCLIPBOARD */
2128 case WM_DESTROYCLIPBOARD
:
2130 term_deselect(term
);
2131 ignore_clip
= FALSE
;
2137 hdc
= BeginPaint(hwnd
, &p
);
2139 SelectPalette(hdc
, pal
, TRUE
);
2140 RealizePalette(hdc
);
2142 term_paint(term
, hdc
,
2143 (p
.rcPaint
.left
-offset_width
)/font_width
,
2144 (p
.rcPaint
.top
-offset_height
)/font_height
,
2145 (p
.rcPaint
.right
-offset_width
-1)/font_width
,
2146 (p
.rcPaint
.bottom
-offset_height
-1)/font_height
,
2150 p
.rcPaint
.left
< offset_width
||
2151 p
.rcPaint
.top
< offset_height
||
2152 p
.rcPaint
.right
>= offset_width
+ font_width
*term
->cols
||
2153 p
.rcPaint
.bottom
>= offset_height
+ font_height
*term
->rows
)
2155 HBRUSH fillcolour
, oldbrush
;
2157 fillcolour
= CreateSolidBrush (
2158 colours
[(ATTR_DEFBG
>>ATTR_BGSHIFT
)*2]);
2159 oldbrush
= SelectObject(hdc
, fillcolour
);
2160 edge
= CreatePen(PS_SOLID
, 0,
2161 colours
[(ATTR_DEFBG
>>ATTR_BGSHIFT
)*2]);
2162 oldpen
= SelectObject(hdc
, edge
);
2165 * Jordan Russell reports that this apparently
2166 * ineffectual IntersectClipRect() call masks a
2167 * Windows NT/2K bug causing strange display
2168 * problems when the PuTTY window is taller than
2169 * the primary monitor. It seems harmless enough...
2171 IntersectClipRect(hdc
,
2172 p
.rcPaint
.left
, p
.rcPaint
.top
,
2173 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2175 ExcludeClipRect(hdc
,
2176 offset_width
, offset_height
,
2177 offset_width
+font_width
*term
->cols
,
2178 offset_height
+font_height
*term
->rows
);
2180 Rectangle(hdc
, p
.rcPaint
.left
, p
.rcPaint
.top
,
2181 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2183 // SelectClipRgn(hdc, NULL);
2185 SelectObject(hdc
, oldbrush
);
2186 DeleteObject(fillcolour
);
2187 SelectObject(hdc
, oldpen
);
2190 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
2191 SelectObject(hdc
, GetStockObject(WHITE_PEN
));
2197 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2198 * but the only one that's likely to try to overload us is FD_READ.
2199 * This means buffering just one is fine.
2201 if (pending_netevent
)
2202 enact_pending_netevent();
2204 pending_netevent
= TRUE
;
2205 pend_netevent_wParam
= wParam
;
2206 pend_netevent_lParam
= lParam
;
2207 if (WSAGETSELECTEVENT(lParam
) != FD_READ
)
2208 enact_pending_netevent();
2210 time(&last_movement
);
2213 term
->has_focus
= TRUE
;
2214 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
2216 flash_window(0); /* stop */
2223 term
->has_focus
= FALSE
;
2225 caret_x
= caret_y
= -1; /* ensure caret is replaced next time */
2229 case WM_ENTERSIZEMOVE
:
2230 #ifdef RDB_DEBUG_PATCH
2231 debug((27, "WM_ENTERSIZEMOVE"));
2235 need_backend_resize
= FALSE
;
2237 case WM_EXITSIZEMOVE
:
2240 #ifdef RDB_DEBUG_PATCH
2241 debug((27, "WM_EXITSIZEMOVE"));
2243 if (need_backend_resize
) {
2244 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2245 InvalidateRect(hwnd
, NULL
, TRUE
);
2250 * This does two jobs:
2251 * 1) Keep the sizetip uptodate
2252 * 2) Make sure the window size is _stepped_ in units of the font size.
2254 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2255 int width
, height
, w
, h
, ew
, eh
;
2256 LPRECT r
= (LPRECT
) lParam
;
2258 if ( !need_backend_resize
&& cfg
.resize_action
== RESIZE_EITHER
&&
2259 (cfg
.height
!= term
->rows
|| cfg
.width
!= term
->cols
)) {
2261 * Great! It seems that both the terminal size and the
2262 * font size have been changed and the user is now dragging.
2264 * It will now be difficult to get back to the configured
2267 * This would be easier but it seems to be too confusing.
2269 term_size(term, cfg.height, cfg.width, cfg.savelines);
2272 cfg
.height
=term
->rows
; cfg
.width
=term
->cols
;
2274 InvalidateRect(hwnd
, NULL
, TRUE
);
2275 need_backend_resize
= TRUE
;
2278 width
= r
->right
- r
->left
- extra_width
;
2279 height
= r
->bottom
- r
->top
- extra_height
;
2280 w
= (width
+ font_width
/ 2) / font_width
;
2283 h
= (height
+ font_height
/ 2) / font_height
;
2286 UpdateSizeTip(hwnd
, w
, h
);
2287 ew
= width
- w
* font_width
;
2288 eh
= height
- h
* font_height
;
2290 if (wParam
== WMSZ_LEFT
||
2291 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2297 if (wParam
== WMSZ_TOP
||
2298 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2308 int width
, height
, w
, h
, rv
= 0;
2309 int ex_width
= extra_width
+ (cfg
.window_border
- offset_width
) * 2;
2310 int ex_height
= extra_height
+ (cfg
.window_border
- offset_height
) * 2;
2311 LPRECT r
= (LPRECT
) lParam
;
2313 width
= r
->right
- r
->left
- ex_width
;
2314 height
= r
->bottom
- r
->top
- ex_height
;
2316 w
= (width
+ term
->cols
/2)/term
->cols
;
2317 h
= (height
+ term
->rows
/2)/term
->rows
;
2318 if ( r
->right
!= r
->left
+ w
*term
->cols
+ ex_width
)
2321 if (wParam
== WMSZ_LEFT
||
2322 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2323 r
->left
= r
->right
- w
*term
->cols
- ex_width
;
2325 r
->right
= r
->left
+ w
*term
->cols
+ ex_width
;
2327 if (r
->bottom
!= r
->top
+ h
*term
->rows
+ ex_height
)
2330 if (wParam
== WMSZ_TOP
||
2331 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2332 r
->top
= r
->bottom
- h
*term
->rows
- ex_height
;
2334 r
->bottom
= r
->top
+ h
*term
->rows
+ ex_height
;
2338 /* break; (never reached) */
2339 case WM_FULLSCR_ON_MAX
:
2340 fullscr_on_max
= TRUE
;
2343 sys_cursor_update();
2346 #ifdef RDB_DEBUG_PATCH
2347 debug((27, "WM_SIZE %s (%d,%d)",
2348 (wParam
== SIZE_MINIMIZED
) ?
"SIZE_MINIMIZED":
2349 (wParam
== SIZE_MAXIMIZED
) ?
"SIZE_MAXIMIZED":
2350 (wParam
== SIZE_RESTORED
&& resizing
) ?
"to":
2351 (wParam
== SIZE_RESTORED
) ?
"SIZE_RESTORED":
2353 LOWORD(lParam
), HIWORD(lParam
)));
2355 if (wParam
== SIZE_MINIMIZED
)
2357 cfg
.win_name_always ? window_name
: icon_name
);
2358 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
)
2359 SetWindowText(hwnd
, window_name
);
2360 if (wParam
== SIZE_RESTORED
)
2361 clear_full_screen();
2362 if (wParam
== SIZE_MAXIMIZED
&& fullscr_on_max
) {
2363 fullscr_on_max
= FALSE
;
2367 if (cfg
.resize_action
== RESIZE_DISABLED
) {
2368 /* A resize, well it better be a minimize. */
2372 int width
, height
, w
, h
;
2374 width
= LOWORD(lParam
);
2375 height
= HIWORD(lParam
);
2378 if (wParam
== SIZE_MAXIMIZED
&& !was_zoomed
) {
2380 prev_rows
= term
->rows
;
2381 prev_cols
= term
->cols
;
2382 if (cfg
.resize_action
== RESIZE_TERM
) {
2383 w
= width
/ font_width
;
2385 h
= height
/ font_height
;
2388 term_size(term
, h
, w
, cfg
.savelines
);
2391 } else if (wParam
== SIZE_RESTORED
&& was_zoomed
) {
2393 if (cfg
.resize_action
== RESIZE_TERM
)
2394 term_size(term
, prev_rows
, prev_cols
, cfg
.savelines
);
2395 if (cfg
.resize_action
!= RESIZE_FONT
)
2400 /* This is an unexpected resize, these will normally happen
2401 * if the window is too large. Probably either the user
2402 * selected a huge font or the screen size has changed.
2404 * This is also called with minimize.
2406 else reset_window(-1);
2410 * Don't call back->size in mid-resize. (To prevent
2411 * massive numbers of resize events getting sent
2412 * down the connection during an NT opaque drag.)
2415 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2416 need_backend_resize
= TRUE
;
2417 w
= (width
-cfg
.window_border
*2) / font_width
;
2419 h
= (height
-cfg
.window_border
*2) / font_height
;
2428 sys_cursor_update();
2431 switch (LOWORD(wParam
)) {
2433 term_scroll(term
, -1, 0);
2436 term_scroll(term
, +1, 0);
2439 term_scroll(term
, 0, +1);
2442 term_scroll(term
, 0, -1);
2445 term_scroll(term
, 0, +term
->rows
/ 2);
2448 term_scroll(term
, 0, -term
->rows
/ 2);
2450 case SB_THUMBPOSITION
:
2452 term_scroll(term
, 1, HIWORD(wParam
));
2456 case WM_PALETTECHANGED
:
2457 if ((HWND
) wParam
!= hwnd
&& pal
!= NULL
) {
2458 HDC hdc
= get_ctx(NULL
);
2460 if (RealizePalette(hdc
) > 0)
2466 case WM_QUERYNEWPALETTE
:
2468 HDC hdc
= get_ctx(NULL
);
2470 if (RealizePalette(hdc
) > 0)
2482 * Add the scan code and keypress timing to the random
2485 noise_ultralight(lParam
);
2488 * We don't do TranslateMessage since it disassociates the
2489 * resulting CHAR message from the KEYDOWN that sparked it,
2490 * which we occasionally don't want. Instead, we process
2491 * KEYDOWN, and call the Win32 translator functions so that
2492 * we get the translations under _our_ control.
2495 unsigned char buf
[20];
2498 if (wParam
== VK_PROCESSKEY
) {
2501 m
.message
= WM_KEYDOWN
;
2503 m
.lParam
= lParam
& 0xdfff;
2504 TranslateMessage(&m
);
2506 len
= TranslateKey(message
, wParam
, lParam
, buf
);
2508 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2512 * Interrupt an ongoing paste. I'm not sure
2513 * this is sensible, but for the moment it's
2514 * preferable to having to faff about buffering
2520 * We need not bother about stdin backlogs
2521 * here, because in GUI PuTTY we can't do
2522 * anything about it anyway; there's no means
2523 * of asking Windows to hold off on KEYDOWN
2524 * messages. We _have_ to buffer everything
2527 term_seen_key_event(term
);
2528 ldisc_send(ldisc
, buf
, len
, 1);
2533 net_pending_errors();
2535 case WM_INPUTLANGCHANGE
:
2536 /* wParam == Font number */
2537 /* lParam == Locale */
2538 set_input_locale((HKL
)lParam
);
2539 sys_cursor_update();
2542 if(wParam
== IMN_SETOPENSTATUS
) {
2543 HIMC hImc
= ImmGetContext(hwnd
);
2544 ImmSetCompositionFont(hImc
, &lfont
);
2545 ImmReleaseContext(hwnd
, hImc
);
2549 case WM_IME_COMPOSITION
:
2555 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
||
2556 osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) break; /* no Unicode */
2558 if ((lParam
& GCS_RESULTSTR
) == 0) /* Composition unfinished. */
2559 break; /* fall back to DefWindowProc */
2561 hIMC
= ImmGetContext(hwnd
);
2562 n
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
2566 buff
= snewn(n
, char);
2567 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, buff
, n
);
2569 * Jaeyoun Chung reports that Korean character
2570 * input doesn't work correctly if we do a single
2571 * luni_send() covering the whole of buff. So
2572 * instead we luni_send the characters one by one.
2574 term_seen_key_event(term
);
2575 for (i
= 0; i
< n
; i
+= 2) {
2576 luni_send(ldisc
, (unsigned short *)(buff
+i
), 1, 1);
2580 ImmReleaseContext(hwnd
, hIMC
);
2585 if (wParam
& 0xFF00) {
2586 unsigned char buf
[2];
2589 buf
[0] = wParam
>> 8;
2590 term_seen_key_event(term
);
2591 lpage_send(ldisc
, kbd_codepage
, buf
, 2, 1);
2593 char c
= (unsigned char) wParam
;
2594 term_seen_key_event(term
);
2595 lpage_send(ldisc
, kbd_codepage
, &c
, 1, 1);
2601 * Nevertheless, we are prepared to deal with WM_CHAR
2602 * messages, should they crop up. So if someone wants to
2603 * post the things to us as part of a macro manoeuvre,
2604 * we're ready to cope.
2607 char c
= (unsigned char)wParam
;
2608 term_seen_key_event(term
);
2609 lpage_send(ldisc
, CP_ACP
, &c
, 1, 1);
2613 if (send_raw_mouse
&& LOWORD(lParam
) == HTCLIENT
) {
2614 SetCursor(LoadCursor(NULL
, IDC_ARROW
));
2618 case WM_AGENT_CALLBACK
:
2620 struct agent_callback
*c
= (struct agent_callback
*)lParam
;
2621 c
->callback(c
->callback_ctx
, c
->data
, c
->len
);
2626 if (message
== wm_mousewheel
|| message
== WM_MOUSEWHEEL
) {
2627 int shift_pressed
=0, control_pressed
=0;
2629 if (message
== WM_MOUSEWHEEL
) {
2630 wheel_accumulator
+= (short)HIWORD(wParam
);
2631 shift_pressed
=LOWORD(wParam
) & MK_SHIFT
;
2632 control_pressed
=LOWORD(wParam
) & MK_CONTROL
;
2635 wheel_accumulator
+= (int)wParam
;
2636 if (GetKeyboardState(keys
)!=0) {
2637 shift_pressed
=keys
[VK_SHIFT
]&0x80;
2638 control_pressed
=keys
[VK_CONTROL
]&0x80;
2642 /* process events when the threshold is reached */
2643 while (abs(wheel_accumulator
) >= WHEEL_DELTA
) {
2646 /* reduce amount for next time */
2647 if (wheel_accumulator
> 0) {
2649 wheel_accumulator
-= WHEEL_DELTA
;
2650 } else if (wheel_accumulator
< 0) {
2652 wheel_accumulator
+= WHEEL_DELTA
;
2656 if (send_raw_mouse
&&
2657 !(cfg
.mouse_override
&& shift_pressed
)) {
2658 /* send a mouse-down followed by a mouse up */
2659 term_mouse(term
, b
, translate_button(b
),
2661 TO_CHR_X(X_POS(lParam
)),
2662 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2663 control_pressed
, is_alt_pressed());
2664 term_mouse(term
, b
, translate_button(b
),
2665 MA_RELEASE
, TO_CHR_X(X_POS(lParam
)),
2666 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2667 control_pressed
, is_alt_pressed());
2669 /* trigger a scroll */
2670 term_scroll(term
, 0,
2672 -term
->rows
/ 2 : term
->rows
/ 2);
2679 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2683 * Move the system caret. (We maintain one, even though it's
2684 * invisible, for the benefit of blind people: apparently some
2685 * helper software tracks the system caret, so we should arrange to
2688 void sys_cursor(void *frontend
, int x
, int y
)
2692 if (!term
->has_focus
) return;
2695 * Avoid gratuitously re-updating the cursor position and IMM
2696 * window if there's no actual change required.
2698 cx
= x
* font_width
+ offset_width
;
2699 cy
= y
* font_height
+ offset_height
;
2700 if (cx
== caret_x
&& cy
== caret_y
)
2705 sys_cursor_update();
2708 static void sys_cursor_update(void)
2713 if (!term
->has_focus
) return;
2715 if (caret_x
< 0 || caret_y
< 0)
2718 SetCaretPos(caret_x
, caret_y
);
2720 /* IMM calls on Win98 and beyond only */
2721 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) return; /* 3.11 */
2723 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
&&
2724 osVersion
.dwMinorVersion
== 0) return; /* 95 */
2726 /* we should have the IMM functions */
2727 hIMC
= ImmGetContext(hwnd
);
2728 cf
.dwStyle
= CFS_POINT
;
2729 cf
.ptCurrentPos
.x
= caret_x
;
2730 cf
.ptCurrentPos
.y
= caret_y
;
2731 ImmSetCompositionWindow(hIMC
, &cf
);
2733 ImmReleaseContext(hwnd
, hIMC
);
2737 * Draw a line of text in the window, at given character
2738 * coordinates, in given attributes.
2740 * We are allowed to fiddle with the contents of `text'.
2742 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
2743 unsigned long attr
, int lattr
)
2746 int nfg
, nbg
, nfont
;
2749 int force_manual_underline
= 0;
2750 int fnt_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
2751 int char_width
= fnt_width
;
2752 int text_adjust
= 0;
2753 static int *IpDx
= 0, IpDxLEN
= 0;
2755 if (attr
& ATTR_WIDE
)
2758 if (len
> IpDxLEN
|| IpDx
[0] != char_width
) {
2760 if (len
> IpDxLEN
) {
2762 IpDx
= snewn(len
+ 16, int);
2763 IpDxLEN
= (len
+ 16);
2765 for (i
= 0; i
< IpDxLEN
; i
++)
2766 IpDx
[i
] = char_width
;
2769 /* Only want the left half of double width lines */
2770 if (lattr
!= LATTR_NORM
&& x
*2 >= term
->cols
)
2778 if ((attr
& TATTR_ACTCURS
) && (cfg
.cursor_type
== 0 || term
->big_cursor
)) {
2779 attr
&= ATTR_CUR_AND
| (bold_mode
!= BOLD_COLOURS ? ATTR_BOLD
: 0);
2780 attr
^= ATTR_CUR_XOR
;
2784 if (cfg
.vtmode
== VT_POORMAN
&& lattr
!= LATTR_NORM
) {
2785 /* Assume a poorman font is borken in other ways too. */
2795 nfont
|= FONT_WIDE
+ FONT_HIGH
;
2798 if (attr
& ATTR_NARROW
)
2799 nfont
|= FONT_NARROW
;
2801 /* Special hack for the VT100 linedraw glyphs. */
2802 if ((attr
& CSET_MASK
) == 0x2300) {
2803 if (text
[0] >= (char) 0xBA && text
[0] <= (char) 0xBD) {
2804 switch ((unsigned char) (text
[0])) {
2806 text_adjust
= -2 * font_height
/ 5;
2809 text_adjust
= -1 * font_height
/ 5;
2812 text_adjust
= font_height
/ 5;
2815 text_adjust
= 2 * font_height
/ 5;
2818 if (lattr
== LATTR_TOP
|| lattr
== LATTR_BOT
)
2821 text
[0] = (char) (ucsdata
.unitab_xterm
['q'] & CHAR_MASK
);
2822 attr
|= (ucsdata
.unitab_xterm
['q'] & CSET_MASK
);
2823 if (attr
& ATTR_UNDER
) {
2824 attr
&= ~ATTR_UNDER
;
2825 force_manual_underline
= 1;
2830 /* Anything left as an original character set is unprintable. */
2831 if (DIRECT_CHAR(attr
)) {
2834 memset(text
, 0xFD, len
);
2838 if ((attr
& CSET_MASK
) == ATTR_OEMCP
)
2841 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
2842 nfg
= 2 * (nfg
& 0xF) + (nfg
& 0x10 ?
1 : 0);
2843 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
2844 nbg
= 2 * (nbg
& 0xF) + (nbg
& 0x10 ?
1 : 0);
2845 if (bold_mode
== BOLD_FONT
&& (attr
& ATTR_BOLD
))
2847 if (und_mode
== UND_FONT
&& (attr
& ATTR_UNDER
))
2848 nfont
|= FONT_UNDERLINE
;
2849 another_font(nfont
);
2850 if (!fonts
[nfont
]) {
2851 if (nfont
& FONT_UNDERLINE
)
2852 force_manual_underline
= 1;
2853 /* Don't do the same for manual bold, it could be bad news. */
2855 nfont
&= ~(FONT_BOLD
| FONT_UNDERLINE
);
2857 another_font(nfont
);
2859 nfont
= FONT_NORMAL
;
2860 if (attr
& ATTR_REVERSE
) {
2865 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BOLD
))
2867 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BLINK
))
2871 SelectObject(hdc
, fonts
[nfont
]);
2872 SetTextColor(hdc
, fg
);
2873 SetBkColor(hdc
, bg
);
2874 SetBkMode(hdc
, OPAQUE
);
2877 line_box
.right
= x
+ char_width
* len
;
2878 line_box
.bottom
= y
+ font_height
;
2880 /* Only want the left half of double width lines */
2881 if (line_box
.right
> font_width
*term
->cols
+offset_width
)
2882 line_box
.right
= font_width
*term
->cols
+offset_width
;
2884 /* We're using a private area for direct to font. (512 chars.) */
2885 if (ucsdata
.dbcs_screenfont
&& (attr
& CSET_MASK
) == ATTR_ACP
) {
2886 /* Ho Hum, dbcs fonts are a PITA! */
2887 /* To display on W9x I have to convert to UCS */
2888 static wchar_t *uni_buf
= 0;
2889 static int uni_len
= 0;
2891 if (len
> uni_len
) {
2894 uni_buf
= snewn(uni_len
, wchar_t);
2897 for(nlen
= mptr
= 0; mptr
<len
; mptr
++) {
2898 uni_buf
[nlen
] = 0xFFFD;
2899 if (IsDBCSLeadByteEx(ucsdata
.font_codepage
, (BYTE
) text
[mptr
])) {
2900 IpDx
[nlen
] += char_width
;
2901 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
2902 text
+mptr
, 2, uni_buf
+nlen
, 1);
2907 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
2908 text
+mptr
, 1, uni_buf
+nlen
, 1);
2916 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
2917 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, uni_buf
, nlen
, IpDx
);
2918 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
2919 SetBkMode(hdc
, TRANSPARENT
);
2920 ExtTextOutW(hdc
, x
- 1,
2921 y
- font_height
* (lattr
==
2922 LATTR_BOT
) + text_adjust
,
2923 ETO_CLIPPED
, &line_box
, uni_buf
, nlen
, IpDx
);
2927 } else if (DIRECT_FONT(attr
)) {
2929 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
2930 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, text
, len
, IpDx
);
2931 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
2932 SetBkMode(hdc
, TRANSPARENT
);
2934 /* GRR: This draws the character outside it's box and can leave
2935 * 'droppings' even with the clip box! I suppose I could loop it
2936 * one character at a time ... yuk.
2938 * Or ... I could do a test print with "W", and use +1 or -1 for this
2939 * shift depending on if the leftmost column is blank...
2941 ExtTextOut(hdc
, x
- 1,
2942 y
- font_height
* (lattr
==
2943 LATTR_BOT
) + text_adjust
,
2944 ETO_CLIPPED
, &line_box
, text
, len
, IpDx
);
2947 /* And 'normal' unicode characters */
2948 static WCHAR
*wbuf
= NULL
;
2949 static int wlen
= 0;
2954 wbuf
= snewn(wlen
, WCHAR
);
2956 for (i
= 0; i
< len
; i
++)
2957 wbuf
[i
] = (WCHAR
) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
2960 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
2961 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, wbuf
, len
, IpDx
);
2963 /* And the shadow bold hack. */
2964 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
2965 SetBkMode(hdc
, TRANSPARENT
);
2966 ExtTextOutW(hdc
, x
- 1,
2967 y
- font_height
* (lattr
==
2968 LATTR_BOT
) + text_adjust
,
2969 ETO_CLIPPED
, &line_box
, wbuf
, len
, IpDx
);
2972 if (lattr
!= LATTR_TOP
&& (force_manual_underline
||
2973 (und_mode
== UND_LINE
2974 && (attr
& ATTR_UNDER
)))) {
2977 if (lattr
== LATTR_BOT
)
2978 dec
= dec
* 2 - font_height
;
2980 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, fg
));
2981 MoveToEx(hdc
, x
, y
+ dec
, NULL
);
2982 LineTo(hdc
, x
+ len
* char_width
, y
+ dec
);
2983 oldpen
= SelectObject(hdc
, oldpen
);
2984 DeleteObject(oldpen
);
2988 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
2989 unsigned long attr
, int lattr
)
2995 int ctype
= cfg
.cursor_type
;
2997 if ((attr
& TATTR_ACTCURS
) && (ctype
== 0 || term
->big_cursor
)) {
2998 if (((attr
& CSET_MASK
) | (unsigned char) *text
) != UCSWIDE
) {
2999 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
3003 attr
|= TATTR_RIGHTCURS
;
3006 fnt_width
= char_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3007 if (attr
& ATTR_WIDE
)
3014 if ((attr
& TATTR_PASCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3017 pts
[0].x
= pts
[1].x
= pts
[4].x
= x
;
3018 pts
[2].x
= pts
[3].x
= x
+ char_width
- 1;
3019 pts
[0].y
= pts
[3].y
= pts
[4].y
= y
;
3020 pts
[1].y
= pts
[2].y
= y
+ font_height
- 1;
3021 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[23]));
3022 Polyline(hdc
, pts
, 5);
3023 oldpen
= SelectObject(hdc
, oldpen
);
3024 DeleteObject(oldpen
);
3025 } else if ((attr
& (TATTR_ACTCURS
| TATTR_PASCURS
)) && ctype
!= 0) {
3026 int startx
, starty
, dx
, dy
, length
, i
;
3029 starty
= y
+ descent
;
3032 length
= char_width
;
3035 if (attr
& TATTR_RIGHTCURS
)
3036 xadjust
= char_width
- 1;
3037 startx
= x
+ xadjust
;
3041 length
= font_height
;
3043 if (attr
& TATTR_ACTCURS
) {
3046 SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[23]));
3047 MoveToEx(hdc
, startx
, starty
, NULL
);
3048 LineTo(hdc
, startx
+ dx
* length
, starty
+ dy
* length
);
3049 oldpen
= SelectObject(hdc
, oldpen
);
3050 DeleteObject(oldpen
);
3052 for (i
= 0; i
< length
; i
++) {
3054 SetPixel(hdc
, startx
, starty
, colours
[23]);
3063 /* This function gets the actual width of a character in the normal font.
3065 int char_width(Context ctx
, int uc
) {
3069 /* If the font max is the same as the font ave width then this
3070 * function is a no-op.
3072 if (!font_dualwidth
) return 1;
3074 switch (uc
& CSET_MASK
) {
3076 uc
= ucsdata
.unitab_line
[uc
& 0xFF];
3079 uc
= ucsdata
.unitab_xterm
[uc
& 0xFF];
3082 uc
= ucsdata
.unitab_scoacs
[uc
& 0xFF];
3085 if (DIRECT_FONT(uc
)) {
3086 if (ucsdata
.dbcs_screenfont
) return 1;
3088 /* Speedup, I know of no font where ascii is the wrong width */
3089 if ((uc
&CHAR_MASK
) >= ' ' && (uc
&CHAR_MASK
)<= '~')
3092 if ( (uc
& CSET_MASK
) == ATTR_ACP
) {
3093 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3094 } else if ( (uc
& CSET_MASK
) == ATTR_OEMCP
) {
3095 another_font(FONT_OEM
);
3096 if (!fonts
[FONT_OEM
]) return 0;
3098 SelectObject(hdc
, fonts
[FONT_OEM
]);
3102 if ( GetCharWidth32(hdc
, uc
&CHAR_MASK
, uc
&CHAR_MASK
, &ibuf
) != 1 &&
3103 GetCharWidth(hdc
, uc
&CHAR_MASK
, uc
&CHAR_MASK
, &ibuf
) != 1)
3106 /* Speedup, I know of no font where ascii is the wrong width */
3107 if (uc
>= ' ' && uc
<= '~') return 1;
3109 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3110 if ( GetCharWidth32W(hdc
, uc
, uc
, &ibuf
) == 1 )
3111 /* Okay that one worked */ ;
3112 else if ( GetCharWidthW(hdc
, uc
, uc
, &ibuf
) == 1 )
3113 /* This should work on 9x too, but it's "less accurate" */ ;
3118 ibuf
+= font_width
/ 2 -1;
3125 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3126 * codes. Returns number of bytes used or zero to drop the message
3127 * or -1 to forward the message to windows.
3129 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
3130 unsigned char *output
)
3133 int scan
, left_alt
= 0, key_down
, shift_state
;
3135 unsigned char *p
= output
;
3136 static int alt_sum
= 0;
3138 HKL kbd_layout
= GetKeyboardLayout(0);
3140 static WORD keys
[3];
3141 static int compose_char
= 0;
3142 static WPARAM compose_key
= 0;
3144 r
= GetKeyboardState(keystate
);
3146 memset(keystate
, 0, sizeof(keystate
));
3149 #define SHOW_TOASCII_RESULT
3150 { /* Tell us all about key events */
3151 static BYTE oldstate
[256];
3152 static int first
= 1;
3156 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3159 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
) {
3161 } else if ((HIWORD(lParam
) & KF_UP
)
3162 && scan
== (HIWORD(lParam
) & 0xFF)) {
3166 if (wParam
>= VK_F1
&& wParam
<= VK_F20
)
3167 debug(("K_F%d", wParam
+ 1 - VK_F1
));
3180 debug(("VK_%02x", wParam
));
3182 if (message
== WM_SYSKEYDOWN
|| message
== WM_SYSKEYUP
)
3184 debug((", S%02x", scan
= (HIWORD(lParam
) & 0xFF)));
3186 ch
= MapVirtualKeyEx(wParam
, 2, kbd_layout
);
3187 if (ch
>= ' ' && ch
<= '~')
3188 debug((", '%c'", ch
));
3190 debug((", $%02x", ch
));
3193 debug((", KB0=%02x", keys
[0]));
3195 debug((", KB1=%02x", keys
[1]));
3197 debug((", KB2=%02x", keys
[2]));
3199 if ((keystate
[VK_SHIFT
] & 0x80) != 0)
3201 if ((keystate
[VK_CONTROL
] & 0x80) != 0)
3203 if ((HIWORD(lParam
) & KF_EXTENDED
))
3205 if ((HIWORD(lParam
) & KF_UP
))
3209 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
);
3210 else if ((HIWORD(lParam
) & KF_UP
))
3211 oldstate
[wParam
& 0xFF] ^= 0x80;
3213 oldstate
[wParam
& 0xFF] ^= 0x81;
3215 for (ch
= 0; ch
< 256; ch
++)
3216 if (oldstate
[ch
] != keystate
[ch
])
3217 debug((", M%02x=%02x", ch
, keystate
[ch
]));
3219 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3223 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
)) {
3224 keystate
[VK_RMENU
] = keystate
[VK_MENU
];
3228 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3229 if ((cfg
.funky_type
== 3 ||
3230 (cfg
.funky_type
<= 1 && term
->app_keypad_keys
&&
3232 && wParam
== VK_NUMLOCK
&& !(keystate
[VK_SHIFT
] & 0x80)) {
3234 wParam
= VK_EXECUTE
;
3236 /* UnToggle NUMLock */
3237 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0)
3238 keystate
[VK_NUMLOCK
] ^= 1;
3241 /* And write back the 'adjusted' state */
3242 SetKeyboardState(keystate
);
3245 /* Disable Auto repeat if required */
3246 if (term
->repeat_off
&&
3247 (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
)
3250 if ((HIWORD(lParam
) & KF_ALTDOWN
) && (keystate
[VK_RMENU
] & 0x80) == 0)
3253 key_down
= ((HIWORD(lParam
) & KF_UP
) == 0);
3255 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3256 if (left_alt
&& (keystate
[VK_CONTROL
] & 0x80)) {
3257 if (cfg
.ctrlaltkeys
)
3258 keystate
[VK_MENU
] = 0;
3260 keystate
[VK_RMENU
] = 0x80;
3265 scan
= (HIWORD(lParam
) & (KF_UP
| KF_EXTENDED
| 0xFF));
3266 shift_state
= ((keystate
[VK_SHIFT
] & 0x80) != 0)
3267 + ((keystate
[VK_CONTROL
] & 0x80) != 0) * 2;
3269 /* Note if AltGr was pressed and if it was used as a compose key */
3270 if (!compose_state
) {
3271 compose_key
= 0x100;
3272 if (cfg
.compose_key
) {
3273 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
))
3274 compose_key
= wParam
;
3276 if (wParam
== VK_APPS
)
3277 compose_key
= wParam
;
3280 if (wParam
== compose_key
) {
3281 if (compose_state
== 0
3282 && (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0) compose_state
=
3284 else if (compose_state
== 1 && (HIWORD(lParam
) & KF_UP
))
3288 } else if (compose_state
== 1 && wParam
!= VK_CONTROL
)
3291 if (compose_state
> 1 && left_alt
)
3294 /* Sanitize the number pad if not using a PC NumPad */
3295 if (left_alt
|| (term
->app_keypad_keys
&& !cfg
.no_applic_k
3296 && cfg
.funky_type
!= 2)
3297 || cfg
.funky_type
== 3 || cfg
.nethack_keypad
|| compose_state
) {
3298 if ((HIWORD(lParam
) & KF_EXTENDED
) == 0) {
3302 nParam
= VK_NUMPAD0
;
3305 nParam
= VK_NUMPAD1
;
3308 nParam
= VK_NUMPAD2
;
3311 nParam
= VK_NUMPAD3
;
3314 nParam
= VK_NUMPAD4
;
3317 nParam
= VK_NUMPAD5
;
3320 nParam
= VK_NUMPAD6
;
3323 nParam
= VK_NUMPAD7
;
3326 nParam
= VK_NUMPAD8
;
3329 nParam
= VK_NUMPAD9
;
3332 nParam
= VK_DECIMAL
;
3336 if (keystate
[VK_NUMLOCK
] & 1)
3343 /* If a key is pressed and AltGr is not active */
3344 if (key_down
&& (keystate
[VK_RMENU
] & 0x80) == 0 && !compose_state
) {
3345 /* Okay, prepare for most alts then ... */
3349 /* Lets see if it's a pattern we know all about ... */
3350 if (wParam
== VK_PRIOR
&& shift_state
== 1) {
3351 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEUP
, 0);
3354 if (wParam
== VK_PRIOR
&& shift_state
== 2) {
3355 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEUP
, 0);
3358 if (wParam
== VK_NEXT
&& shift_state
== 1) {
3359 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEDOWN
, 0);
3362 if (wParam
== VK_NEXT
&& shift_state
== 2) {
3363 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEDOWN
, 0);
3366 if (wParam
== VK_INSERT
&& shift_state
== 1) {
3367 term_do_paste(term
);
3370 if (left_alt
&& wParam
== VK_F4
&& cfg
.alt_f4
) {
3373 if (left_alt
&& wParam
== VK_SPACE
&& cfg
.alt_space
) {
3374 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_KEYMENU
, 0);
3377 if (left_alt
&& wParam
== VK_RETURN
&& cfg
.fullscreenonaltenter
&&
3378 (cfg
.resize_action
!= RESIZE_DISABLED
)) {
3379 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) != KF_REPEAT
)
3383 /* Control-Numlock for app-keypad mode switch */
3384 if (wParam
== VK_PAUSE
&& shift_state
== 2) {
3385 term
->app_keypad_keys
^= 1;
3389 /* Nethack keypad */
3390 if (cfg
.nethack_keypad
&& !left_alt
) {
3393 *p
++ = shift_state ?
'B' : 'b';
3396 *p
++ = shift_state ?
'J' : 'j';
3399 *p
++ = shift_state ?
'N' : 'n';
3402 *p
++ = shift_state ?
'H' : 'h';
3405 *p
++ = shift_state ?
'.' : '.';
3408 *p
++ = shift_state ?
'L' : 'l';
3411 *p
++ = shift_state ?
'Y' : 'y';
3414 *p
++ = shift_state ?
'K' : 'k';
3417 *p
++ = shift_state ?
'U' : 'u';
3422 /* Application Keypad */
3426 if (cfg
.funky_type
== 3 ||
3427 (cfg
.funky_type
<= 1 &&
3428 term
->app_keypad_keys
&& !cfg
.no_applic_k
)) switch (wParam
) {
3442 if (term
->app_keypad_keys
&& !cfg
.no_applic_k
)
3479 if (cfg
.funky_type
== 2) {
3484 } else if (shift_state
)
3491 if (cfg
.funky_type
== 2)
3495 if (cfg
.funky_type
== 2)
3499 if (cfg
.funky_type
== 2)
3504 if (HIWORD(lParam
) & KF_EXTENDED
)
3509 if (term
->vt52_mode
) {
3510 if (xkey
>= 'P' && xkey
<= 'S')
3511 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3513 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
3515 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3520 if (wParam
== VK_BACK
&& shift_state
== 0) { /* Backspace */
3521 *p
++ = (cfg
.bksp_is_delete ?
0x7F : 0x08);
3525 if (wParam
== VK_BACK
&& shift_state
== 1) { /* Shift Backspace */
3526 /* We do the opposite of what is configured */
3527 *p
++ = (cfg
.bksp_is_delete ?
0x08 : 0x7F);
3531 if (wParam
== VK_TAB
&& shift_state
== 1) { /* Shift tab */
3537 if (wParam
== VK_SPACE
&& shift_state
== 2) { /* Ctrl-Space */
3541 if (wParam
== VK_SPACE
&& shift_state
== 3) { /* Ctrl-Shift-Space */
3545 if (wParam
== VK_CANCEL
&& shift_state
== 2) { /* Ctrl-Break */
3550 if (wParam
== VK_PAUSE
) { /* Break/Pause */
3555 /* Control-2 to Control-8 are special */
3556 if (shift_state
== 2 && wParam
>= '2' && wParam
<= '8') {
3557 *p
++ = "\000\033\034\035\036\037\177"[wParam
- '2'];
3560 if (shift_state
== 2 && (wParam
== 0xBD || wParam
== 0xBF)) {
3564 if (shift_state
== 2 && wParam
== 0xDF) {
3568 if (shift_state
== 3 && wParam
== 0xDE) {
3569 *p
++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
3572 if (shift_state
== 0 && wParam
== VK_RETURN
&& term
->cr_lf_return
) {
3579 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3580 * for integer decimal nn.)
3582 * We also deal with the weird ones here. Linux VCs replace F1
3583 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3584 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3590 code
= (keystate
[VK_SHIFT
] & 0x80 ?
23 : 11);
3593 code
= (keystate
[VK_SHIFT
] & 0x80 ?
24 : 12);
3596 code
= (keystate
[VK_SHIFT
] & 0x80 ?
25 : 13);
3599 code
= (keystate
[VK_SHIFT
] & 0x80 ?
26 : 14);
3602 code
= (keystate
[VK_SHIFT
] & 0x80 ?
28 : 15);
3605 code
= (keystate
[VK_SHIFT
] & 0x80 ?
29 : 17);
3608 code
= (keystate
[VK_SHIFT
] & 0x80 ?
31 : 18);
3611 code
= (keystate
[VK_SHIFT
] & 0x80 ?
32 : 19);
3614 code
= (keystate
[VK_SHIFT
] & 0x80 ?
33 : 20);
3617 code
= (keystate
[VK_SHIFT
] & 0x80 ?
34 : 21);
3650 if ((shift_state
&2) == 0) switch (wParam
) {
3670 /* Reorder edit keys to physical order */
3671 if (cfg
.funky_type
== 3 && code
<= 6)
3672 code
= "\0\2\1\4\5\3\6"[code
];
3674 if (term
->vt52_mode
&& code
> 0 && code
<= 6) {
3675 p
+= sprintf((char *) p
, "\x1B%c", " HLMEIG"[code
]);
3679 if (cfg
.funky_type
== 5 && /* SCO function keys */
3680 code
>= 11 && code
<= 34) {
3681 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
3684 case VK_F1
: index
= 0; break;
3685 case VK_F2
: index
= 1; break;
3686 case VK_F3
: index
= 2; break;
3687 case VK_F4
: index
= 3; break;
3688 case VK_F5
: index
= 4; break;
3689 case VK_F6
: index
= 5; break;
3690 case VK_F7
: index
= 6; break;
3691 case VK_F8
: index
= 7; break;
3692 case VK_F9
: index
= 8; break;
3693 case VK_F10
: index
= 9; break;
3694 case VK_F11
: index
= 10; break;
3695 case VK_F12
: index
= 11; break;
3697 if (keystate
[VK_SHIFT
] & 0x80) index
+= 12;
3698 if (keystate
[VK_CONTROL
] & 0x80) index
+= 24;
3699 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
3702 if (cfg
.funky_type
== 5 && /* SCO small keypad */
3703 code
>= 1 && code
<= 6) {
3704 char codes
[] = "HL.FIG";
3708 p
+= sprintf((char *) p
, "\x1B[%c", codes
[code
-1]);
3712 if ((term
->vt52_mode
|| cfg
.funky_type
== 4) && code
>= 11 && code
<= 24) {
3718 if (term
->vt52_mode
)
3719 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11 - offt
);
3722 sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11 - offt
);
3725 if (cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
3726 p
+= sprintf((char *) p
, "\x1B[[%c", code
+ 'A' - 11);
3729 if (cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
3730 if (term
->vt52_mode
)
3731 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11);
3733 p
+= sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11);
3736 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
3737 p
+= sprintf((char *) p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
3741 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
3746 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
3747 * some reason seems to send VK_CLEAR to Windows...).
3769 if (term
->vt52_mode
)
3770 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3772 int app_flg
= (term
->app_cursor_keys
&& !cfg
.no_applic_c
);
3775 * RDB: VT100 & VT102 manuals both state the
3776 * app cursor keys only work if the app keypad
3779 * SGT: That may well be true, but xterm
3780 * disagrees and so does at least one
3781 * application, so I've #if'ed this out and the
3782 * behaviour is back to PuTTY's original: app
3783 * cursor and app keypad are independently
3784 * switchable modes. If anyone complains about
3785 * _this_ I'll have to put in a configurable
3788 if (!term
->app_keypad_keys
)
3791 /* Useful mapping of Ctrl-arrows */
3792 if (shift_state
== 2)
3796 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3798 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
3805 * Finally, deal with Return ourselves. (Win95 seems to
3806 * foul it up when Alt is pressed, for some reason.)
3808 if (wParam
== VK_RETURN
) { /* Return */
3814 if (left_alt
&& wParam
>= VK_NUMPAD0
&& wParam
<= VK_NUMPAD9
)
3815 alt_sum
= alt_sum
* 10 + wParam
- VK_NUMPAD0
;
3820 /* Okay we've done everything interesting; let windows deal with
3821 * the boring stuff */
3825 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
3826 if(cfg
.xlat_capslockcyr
&& keystate
[VK_CAPITAL
] != 0) {
3828 keystate
[VK_CAPITAL
] = 0;
3831 r
= ToAsciiEx(wParam
, scan
, keystate
, keys
, 0, kbd_layout
);
3832 #ifdef SHOW_TOASCII_RESULT
3833 if (r
== 1 && !key_down
) {
3835 if (in_utf(term
) || ucsdata
.dbcs_screenfont
)
3836 debug((", (U+%04x)", alt_sum
));
3838 debug((", LCH(%d)", alt_sum
));
3840 debug((", ACH(%d)", keys
[0]));
3845 for (r1
= 0; r1
< r
; r1
++) {
3846 debug(("%s%d", r1 ?
"," : "", keys
[r1
]));
3855 * Interrupt an ongoing paste. I'm not sure this is
3856 * sensible, but for the moment it's preferable to
3857 * having to faff about buffering things.
3862 for (i
= 0; i
< r
; i
++) {
3863 unsigned char ch
= (unsigned char) keys
[i
];
3865 if (compose_state
== 2 && (ch
& 0x80) == 0 && ch
> ' ') {
3870 if (compose_state
== 3 && (ch
& 0x80) == 0 && ch
> ' ') {
3874 if ((nc
= check_compose(compose_char
, ch
)) == -1) {
3875 MessageBeep(MB_ICONHAND
);
3879 term_seen_key_event(term
);
3880 luni_send(ldisc
, &keybuf
, 1, 1);
3888 if (in_utf(term
) || ucsdata
.dbcs_screenfont
) {
3890 term_seen_key_event(term
);
3891 luni_send(ldisc
, &keybuf
, 1, 1);
3893 ch
= (char) alt_sum
;
3895 * We need not bother about stdin
3896 * backlogs here, because in GUI PuTTY
3897 * we can't do anything about it
3898 * anyway; there's no means of asking
3899 * Windows to hold off on KEYDOWN
3900 * messages. We _have_ to buffer
3901 * everything we're sent.
3903 term_seen_key_event(term
);
3904 ldisc_send(ldisc
, &ch
, 1, 1);
3908 term_seen_key_event(term
);
3909 lpage_send(ldisc
, kbd_codepage
, &ch
, 1, 1);
3912 if(capsOn
&& ch
< 0x80) {
3915 cbuf
[1] = xlat_uskbd2cyrllic(ch
);
3916 term_seen_key_event(term
);
3917 luni_send(ldisc
, cbuf
+!left_alt
, 1+!!left_alt
, 1);
3922 term_seen_key_event(term
);
3923 lpage_send(ldisc
, kbd_codepage
,
3924 cbuf
+!left_alt
, 1+!!left_alt
, 1);
3930 /* This is so the ALT-Numpad and dead keys work correctly. */
3935 /* If we're definitly not building up an ALT-54321 then clear it */
3938 /* If we will be using alt_sum fix the 256s */
3939 else if (keys
[0] && (in_utf(term
) || ucsdata
.dbcs_screenfont
))
3944 * ALT alone may or may not want to bring up the System menu.
3945 * If it's not meant to, we return 0 on presses or releases of
3946 * ALT, to show that we've swallowed the keystroke. Otherwise
3947 * we return -1, which means Windows will give the keystroke
3948 * its default handling (i.e. bring up the System menu).
3950 if (wParam
== VK_MENU
&& !cfg
.alt_only
)
3956 void request_paste(void *frontend
)
3959 * In Windows, pasting is synchronous: we can read the
3960 * clipboard with no difficulty, so request_paste() can just go
3963 term_do_paste(term
);
3966 void set_title(void *frontend
, char *title
)
3969 window_name
= snewn(1 + strlen(title
), char);
3970 strcpy(window_name
, title
);
3971 if (cfg
.win_name_always
|| !IsIconic(hwnd
))
3972 SetWindowText(hwnd
, title
);
3975 void set_icon(void *frontend
, char *title
)
3978 icon_name
= snewn(1 + strlen(title
), char);
3979 strcpy(icon_name
, title
);
3980 if (!cfg
.win_name_always
&& IsIconic(hwnd
))
3981 SetWindowText(hwnd
, title
);
3984 void set_sbar(void *frontend
, int total
, int start
, int page
)
3988 if (is_full_screen() ?
!cfg
.scrollbar_in_fullscreen
: !cfg
.scrollbar
)
3991 si
.cbSize
= sizeof(si
);
3992 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
3994 si
.nMax
= total
- 1;
3998 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
4001 Context
get_ctx(void *frontend
)
4007 SelectPalette(hdc
, pal
, FALSE
);
4013 void free_ctx(Context ctx
)
4015 SelectPalette(ctx
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
4016 ReleaseDC(hwnd
, ctx
);
4019 static void real_palette_set(int n
, int r
, int g
, int b
)
4022 logpal
->palPalEntry
[n
].peRed
= r
;
4023 logpal
->palPalEntry
[n
].peGreen
= g
;
4024 logpal
->palPalEntry
[n
].peBlue
= b
;
4025 logpal
->palPalEntry
[n
].peFlags
= PC_NOCOLLAPSE
;
4026 colours
[n
] = PALETTERGB(r
, g
, b
);
4027 SetPaletteEntries(pal
, 0, NCOLOURS
, logpal
->palPalEntry
);
4029 colours
[n
] = RGB(r
, g
, b
);
4032 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
4034 static const int first
[21] = {
4035 0, 2, 4, 6, 8, 10, 12, 14,
4036 1, 3, 5, 7, 9, 11, 13, 15,
4039 real_palette_set(first
[n
], r
, g
, b
);
4041 real_palette_set(first
[n
] + 1, r
, g
, b
);
4043 HDC hdc
= get_ctx(frontend
);
4044 UnrealizeObject(pal
);
4045 RealizePalette(hdc
);
4050 void palette_reset(void *frontend
)
4054 for (i
= 0; i
< NCOLOURS
; i
++) {
4056 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
4057 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
4058 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
4059 logpal
->palPalEntry
[i
].peFlags
= 0;
4060 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
4061 defpal
[i
].rgbtGreen
,
4062 defpal
[i
].rgbtBlue
);
4064 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
4065 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4070 SetPaletteEntries(pal
, 0, NCOLOURS
, logpal
->palPalEntry
);
4071 hdc
= get_ctx(frontend
);
4072 RealizePalette(hdc
);
4077 void write_aclip(void *frontend
, char *data
, int len
, int must_deselect
)
4082 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
4085 lock
= GlobalLock(clipdata
);
4088 memcpy(lock
, data
, len
);
4089 ((unsigned char *) lock
)[len
] = 0;
4090 GlobalUnlock(clipdata
);
4093 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4095 if (OpenClipboard(hwnd
)) {
4097 SetClipboardData(CF_TEXT
, clipdata
);
4100 GlobalFree(clipdata
);
4103 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4107 * Note: unlike write_aclip() this will not append a nul.
4109 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
4111 HGLOBAL clipdata
, clipdata2
, clipdata3
;
4113 void *lock
, *lock2
, *lock3
;
4115 len2
= WideCharToMultiByte(CP_ACP
, 0, data
, len
, 0, 0, NULL
, NULL
);
4117 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
,
4118 len
* sizeof(wchar_t));
4119 clipdata2
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len2
);
4121 if (!clipdata
|| !clipdata2
) {
4123 GlobalFree(clipdata
);
4125 GlobalFree(clipdata2
);
4128 if (!(lock
= GlobalLock(clipdata
)))
4130 if (!(lock2
= GlobalLock(clipdata2
)))
4133 memcpy(lock
, data
, len
* sizeof(wchar_t));
4134 WideCharToMultiByte(CP_ACP
, 0, data
, len
, lock2
, len2
, NULL
, NULL
);
4136 if (cfg
.rtf_paste
) {
4137 wchar_t unitab
[256];
4139 unsigned char *tdata
= (unsigned char *)lock2
;
4140 wchar_t *udata
= (wchar_t *)lock
;
4141 int rtflen
= 0, uindex
= 0, tindex
= 0;
4143 int multilen
, blen
, alen
, totallen
, i
;
4144 char before
[16], after
[4];
4146 get_unitab(CP_ACP
, unitab
, 0);
4148 rtfsize
= 100 + strlen(cfg
.font
.name
);
4149 rtf
= snewn(rtfsize
, char);
4150 sprintf(rtf
, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
4151 GetACP(), cfg
.font
.name
);
4152 rtflen
= strlen(rtf
);
4155 * We want to construct a piece of RTF that specifies the
4156 * same Unicode text. To do this we will read back in
4157 * parallel from the Unicode data in `udata' and the
4158 * non-Unicode data in `tdata'. For each character in
4159 * `tdata' which becomes the right thing in `udata' when
4160 * looked up in `unitab', we just copy straight over from
4161 * tdata. For each one that doesn't, we must WCToMB it
4162 * individually and produce a \u escape sequence.
4164 * It would probably be more robust to just bite the bullet
4165 * and WCToMB each individual Unicode character one by one,
4166 * then MBToWC each one back to see if it was an accurate
4167 * translation; but that strikes me as a horrifying number
4168 * of Windows API calls so I want to see if this faster way
4169 * will work. If it screws up badly we can always revert to
4170 * the simple and slow way.
4172 while (tindex
< len2
&& uindex
< len
&&
4173 tdata
[tindex
] && udata
[uindex
]) {
4174 if (tindex
+ 1 < len2
&&
4175 tdata
[tindex
] == '\r' &&
4176 tdata
[tindex
+1] == '\n') {
4180 if (unitab
[tdata
[tindex
]] == udata
[uindex
]) {
4186 multilen
= WideCharToMultiByte(CP_ACP
, 0, unitab
+uindex
, 1,
4187 NULL
, 0, NULL
, NULL
);
4188 if (multilen
!= 1) {
4189 blen
= sprintf(before
, "{\\uc%d\\u%d", multilen
,
4191 alen
= 1; strcpy(after
, "}");
4193 blen
= sprintf(before
, "\\u%d", udata
[uindex
]);
4194 alen
= 0; after
[0] = '\0';
4197 assert(tindex
+ multilen
<= len2
);
4198 totallen
= blen
+ alen
;
4199 for (i
= 0; i
< multilen
; i
++) {
4200 if (tdata
[tindex
+i
] == '\\' ||
4201 tdata
[tindex
+i
] == '{' ||
4202 tdata
[tindex
+i
] == '}')
4204 else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A)
4205 totallen
+= 6; /* \par\r\n */
4206 else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20)
4212 if (rtfsize
< rtflen
+ totallen
+ 3) {
4213 rtfsize
= rtflen
+ totallen
+ 512;
4214 rtf
= sresize(rtf
, rtfsize
, char);
4217 strcpy(rtf
+ rtflen
, before
); rtflen
+= blen
;
4218 for (i
= 0; i
< multilen
; i
++) {
4219 if (tdata
[tindex
+i
] == '\\' ||
4220 tdata
[tindex
+i
] == '{' ||
4221 tdata
[tindex
+i
] == '}') {
4222 rtf
[rtflen
++] = '\\';
4223 rtf
[rtflen
++] = tdata
[tindex
+i
];
4224 } else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A) {
4225 rtflen
+= sprintf(rtf
+rtflen
, "\\par\r\n");
4226 } else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20) {
4227 rtflen
+= sprintf(rtf
+rtflen
, "\\'%02x", tdata
[tindex
+i
]);
4229 rtf
[rtflen
++] = tdata
[tindex
+i
];
4232 strcpy(rtf
+ rtflen
, after
); rtflen
+= alen
;
4238 strcpy(rtf
+ rtflen
, "}");
4241 clipdata3
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, rtflen
);
4242 if (clipdata3
&& (lock3
= GlobalLock(clipdata3
)) != NULL
) {
4244 GlobalUnlock(clipdata3
);
4250 GlobalUnlock(clipdata
);
4251 GlobalUnlock(clipdata2
);
4254 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4256 if (OpenClipboard(hwnd
)) {
4258 SetClipboardData(CF_UNICODETEXT
, clipdata
);
4259 SetClipboardData(CF_TEXT
, clipdata2
);
4261 SetClipboardData(RegisterClipboardFormat(CF_RTF
), clipdata3
);
4264 GlobalFree(clipdata
);
4265 GlobalFree(clipdata2
);
4269 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4272 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
4274 static HGLOBAL clipdata
= NULL
;
4275 static wchar_t *converted
= 0;
4284 GlobalUnlock(clipdata
);
4287 } else if (OpenClipboard(NULL
)) {
4288 if ((clipdata
= GetClipboardData(CF_UNICODETEXT
))) {
4290 *p
= GlobalLock(clipdata
);
4292 for (p2
= *p
; *p2
; p2
++);
4296 } else if ( (clipdata
= GetClipboardData(CF_TEXT
)) ) {
4300 s
= GlobalLock(clipdata
);
4301 i
= MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, 0, 0);
4302 *p
= converted
= snewn(i
, wchar_t);
4303 MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, converted
, i
);
4316 * Move `lines' lines from position `from' to position `to' in the
4319 void optimised_move(void *frontend
, int to
, int from
, int lines
)
4324 min
= (to
< from ? to
: from
);
4325 max
= to
+ from
- min
;
4327 r
.left
= offset_width
;
4328 r
.right
= offset_width
+ term
->cols
* font_width
;
4329 r
.top
= offset_height
+ min
* font_height
;
4330 r
.bottom
= offset_height
+ (max
+ lines
) * font_height
;
4331 ScrollWindow(hwnd
, 0, (to
- from
) * font_height
, &r
, &r
);
4336 * Print a message box and perform a fatal exit.
4338 void fatalbox(char *fmt
, ...)
4341 char *stuff
, morestuff
[100];
4344 stuff
= dupvprintf(fmt
, ap
);
4346 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4347 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
4353 * Print a modal (Really Bad) message box and perform a fatal exit.
4355 void modalfatalbox(char *fmt
, ...)
4358 char *stuff
, morestuff
[100];
4361 stuff
= dupvprintf(fmt
, ap
);
4363 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4364 MessageBox(hwnd
, stuff
, morestuff
,
4365 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
4371 * Manage window caption / taskbar flashing, if enabled.
4372 * 0 = stop, 1 = maintain, 2 = start
4374 static void flash_window(int mode
)
4376 static long last_flash
= 0;
4377 static int flashing
= 0;
4378 if ((mode
== 0) || (cfg
.beep_ind
== B_IND_DISABLED
)) {
4381 FlashWindow(hwnd
, FALSE
);
4385 } else if (mode
== 2) {
4388 last_flash
= GetTickCount();
4390 FlashWindow(hwnd
, TRUE
);
4393 } else if ((mode
== 1) && (cfg
.beep_ind
== B_IND_FLASH
)) {
4396 long now
= GetTickCount();
4397 long fdiff
= now
- last_flash
;
4398 if (fdiff
< 0 || fdiff
> 450) {
4400 FlashWindow(hwnd
, TRUE
); /* toggle */
4409 void beep(void *frontend
, int mode
)
4411 if (mode
== BELL_DEFAULT
) {
4413 * For MessageBeep style bells, we want to be careful of
4414 * timing, because they don't have the nice property of
4415 * PlaySound bells that each one cancels the previous
4416 * active one. So we limit the rate to one per 50ms or so.
4418 static long lastbeep
= 0;
4421 beepdiff
= GetTickCount() - lastbeep
;
4422 if (beepdiff
>= 0 && beepdiff
< 50)
4426 * The above MessageBeep call takes time, so we record the
4427 * time _after_ it finishes rather than before it starts.
4429 lastbeep
= GetTickCount();
4430 } else if (mode
== BELL_WAVEFILE
) {
4431 if (!PlaySound(cfg
.bell_wavefile
.path
, NULL
,
4432 SND_ASYNC
| SND_FILENAME
)) {
4433 char buf
[sizeof(cfg
.bell_wavefile
) + 80];
4435 sprintf(buf
, "Unable to play sound file\n%s\n"
4436 "Using default sound instead", cfg
.bell_wavefile
);
4437 sprintf(otherbuf
, "%.70s Sound Error", appname
);
4438 MessageBox(hwnd
, buf
, otherbuf
,
4439 MB_OK
| MB_ICONEXCLAMATION
);
4440 cfg
.beep
= BELL_DEFAULT
;
4442 } else if (mode
== BELL_PCSPEAKER
) {
4443 static long lastbeep
= 0;
4446 beepdiff
= GetTickCount() - lastbeep
;
4447 if (beepdiff
>= 0 && beepdiff
< 50)
4451 * We must beep in different ways depending on whether this
4452 * is a 95-series or NT-series OS.
4454 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
4458 lastbeep
= GetTickCount();
4460 /* Otherwise, either visual bell or disabled; do nothing here */
4461 if (!term
->has_focus
) {
4462 flash_window(2); /* start */
4467 * Minimise or restore the window in response to a server-side
4470 void set_iconic(void *frontend
, int iconic
)
4472 if (IsIconic(hwnd
)) {
4474 ShowWindow(hwnd
, SW_RESTORE
);
4477 ShowWindow(hwnd
, SW_MINIMIZE
);
4482 * Move the window in response to a server-side request.
4484 void move_window(void *frontend
, int x
, int y
)
4486 if (cfg
.resize_action
== RESIZE_DISABLED
||
4487 cfg
.resize_action
== RESIZE_FONT
||
4491 SetWindowPos(hwnd
, NULL
, x
, y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
);
4495 * Move the window to the top or bottom of the z-order in response
4496 * to a server-side request.
4498 void set_zorder(void *frontend
, int top
)
4500 if (cfg
.alwaysontop
)
4501 return; /* ignore */
4502 SetWindowPos(hwnd
, top ? HWND_TOP
: HWND_BOTTOM
, 0, 0, 0, 0,
4503 SWP_NOMOVE
| SWP_NOSIZE
);
4507 * Refresh the window in response to a server-side request.
4509 void refresh_window(void *frontend
)
4511 InvalidateRect(hwnd
, NULL
, TRUE
);
4515 * Maximise or restore the window in response to a server-side
4518 void set_zoomed(void *frontend
, int zoomed
)
4520 if (IsZoomed(hwnd
)) {
4522 ShowWindow(hwnd
, SW_RESTORE
);
4525 ShowWindow(hwnd
, SW_MAXIMIZE
);
4530 * Report whether the window is iconic, for terminal reports.
4532 int is_iconic(void *frontend
)
4534 return IsIconic(hwnd
);
4538 * Report the window's position, for terminal reports.
4540 void get_window_pos(void *frontend
, int *x
, int *y
)
4543 GetWindowRect(hwnd
, &r
);
4549 * Report the window's pixel size, for terminal reports.
4551 void get_window_pixels(void *frontend
, int *x
, int *y
)
4554 GetWindowRect(hwnd
, &r
);
4555 *x
= r
.right
- r
.left
;
4556 *y
= r
.bottom
- r
.top
;
4560 * Return the window or icon title.
4562 char *get_window_title(void *frontend
, int icon
)
4564 return icon ? icon_name
: window_name
;
4568 * See if we're in full-screen mode.
4570 int is_full_screen()
4572 if (!IsZoomed(hwnd
))
4574 if (GetWindowLong(hwnd
, GWL_STYLE
) & WS_CAPTION
)
4579 /* Get the rect/size of a full screen window using the nearest available
4580 * monitor in multimon systems; default to something sensible if only
4581 * one monitor is present. */
4582 static int get_fullscreen_rect(RECT
* ss
)
4584 #ifdef MONITOR_DEFAULTTONEAREST
4587 mon
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
4588 mi
.cbSize
= sizeof(mi
);
4589 GetMonitorInfo(mon
, &mi
);
4591 /* structure copy */
4595 /* could also use code like this:
4596 ss->left = ss->top = 0;
4597 ss->right = GetSystemMetrics(SM_CXSCREEN);
4598 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
4600 return GetClientRect(GetDesktopWindow(), ss
);
4606 * Go full-screen. This should only be called when we are already
4609 void make_full_screen()
4614 assert(IsZoomed(hwnd
));
4616 if (is_full_screen())
4619 /* Remove the window furniture. */
4620 style
= GetWindowLong(hwnd
, GWL_STYLE
);
4621 style
&= ~(WS_CAPTION
| WS_BORDER
| WS_THICKFRAME
);
4622 if (cfg
.scrollbar_in_fullscreen
)
4623 style
|= WS_VSCROLL
;
4625 style
&= ~WS_VSCROLL
;
4626 SetWindowLong(hwnd
, GWL_STYLE
, style
);
4628 /* Resize ourselves to exactly cover the nearest monitor. */
4629 get_fullscreen_rect(&ss
);
4630 SetWindowPos(hwnd
, HWND_TOP
, ss
.left
, ss
.top
,
4635 /* Tick the menu item in the System menu. */
4636 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
4641 * Clear the full-screen attributes.
4643 void clear_full_screen()
4645 DWORD oldstyle
, style
;
4647 /* Reinstate the window furniture. */
4648 style
= oldstyle
= GetWindowLong(hwnd
, GWL_STYLE
);
4649 style
|= WS_CAPTION
| WS_BORDER
;
4650 if (cfg
.resize_action
== RESIZE_DISABLED
)
4651 style
&= ~WS_THICKFRAME
;
4653 style
|= WS_THICKFRAME
;
4655 style
|= WS_VSCROLL
;
4657 style
&= ~WS_VSCROLL
;
4658 if (style
!= oldstyle
) {
4659 SetWindowLong(hwnd
, GWL_STYLE
, style
);
4660 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
4661 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
4665 /* Untick the menu item in the System menu. */
4666 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
4671 * Toggle full-screen mode.
4673 void flip_full_screen()
4675 if (is_full_screen()) {
4676 ShowWindow(hwnd
, SW_RESTORE
);
4677 } else if (IsZoomed(hwnd
)) {
4680 SendMessage(hwnd
, WM_FULLSCR_ON_MAX
, 0, 0);
4681 ShowWindow(hwnd
, SW_MAXIMIZE
);
4685 void frontend_keypress(void *handle
)
4688 * Keypress termination in non-Close-On-Exit mode is not
4689 * currently supported in PuTTY proper, because the window
4690 * always has a perfectly good Close button anyway. So we do
4696 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
4698 return term_data(term
, is_stderr
, data
, len
);
4701 void agent_schedule_callback(void (*callback
)(void *, void *, int),
4702 void *callback_ctx
, void *data
, int len
)
4704 struct agent_callback
*c
= snew(struct agent_callback
);
4705 c
->callback
= callback
;
4706 c
->callback_ctx
= callback_ctx
;
4709 PostMessage(hwnd
, WM_AGENT_CALLBACK
, 0, (LPARAM
)c
);