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 init_palette(void);
79 static void init_fonts(int, int);
80 static void another_font(int);
81 static void deinit_fonts(void);
82 static void set_input_locale(HKL
);
84 static int is_full_screen(void);
85 static void make_full_screen(void);
86 static void clear_full_screen(void);
87 static void flip_full_screen(void);
89 /* Window layout information */
90 static void reset_window(int);
91 static int extra_width
, extra_height
;
92 static int font_width
, font_height
, font_dualwidth
;
93 static int offset_width
, offset_height
;
94 static int was_zoomed
= 0;
95 static int prev_rows
, prev_cols
;
97 static int pending_netevent
= 0;
98 static WPARAM pend_netevent_wParam
= 0;
99 static LPARAM pend_netevent_lParam
= 0;
100 static void enact_pending_netevent(void);
101 static void flash_window(int mode
);
102 static void sys_cursor_update(void);
103 static int is_shift_pressed(void);
104 static int get_fullscreen_rect(RECT
* ss
);
106 static time_t last_movement
= 0;
108 static int caret_x
= -1, caret_y
= -1;
110 static int kbd_codepage
;
113 static Backend
*back
;
114 static void *backhandle
;
116 static struct unicode_data ucsdata
;
117 static int session_closed
;
119 static const struct telnet_special
*specials
;
120 static int specials_menu_position
;
122 Config cfg
; /* exported to windlg.c */
124 extern struct sesslist sesslist
; /* imported from windlg.c */
126 struct agent_callback
{
127 void (*callback
)(void *, void *, int);
133 #define FONT_NORMAL 0
135 #define FONT_UNDERLINE 2
136 #define FONT_BOLDUND 3
137 #define FONT_WIDE 0x04
138 #define FONT_HIGH 0x08
139 #define FONT_NARROW 0x10
141 #define FONT_OEM 0x20
142 #define FONT_OEMBOLD 0x21
143 #define FONT_OEMUND 0x22
144 #define FONT_OEMBOLDUND 0x23
146 #define FONT_MAXNO 0x2F
148 static HFONT fonts
[FONT_MAXNO
];
149 static LOGFONT lfont
;
150 static int fontflag
[FONT_MAXNO
];
152 BOLD_COLOURS
, BOLD_SHADOW
, BOLD_FONT
160 static COLORREF colours
[NCOLOURS
];
162 static LPLOGPALETTE logpal
;
163 static RGBTRIPLE defpal
[NCOLOURS
];
167 static HBITMAP caretbm
;
169 static int dbltime
, lasttime
, lastact
;
170 static Mouse_Button lastbtn
;
172 /* this allows xterm-style mouse handling. */
173 static int send_raw_mouse
= 0;
174 static int wheel_accumulator
= 0;
176 static char *window_name
, *icon_name
;
178 static int compose_state
= 0;
180 static int wsa_started
= 0;
182 static UINT wm_mousewheel
= WM_MOUSEWHEEL
;
184 /* Dummy routine, only required in plink. */
185 void ldisc_update(void *frontend
, int echo
, int edit
)
189 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
195 int guess_width
, guess_height
;
198 flags
= FLAG_VERBOSE
| FLAG_INTERACTIVE
;
200 winsock_ver
= MAKEWORD(1, 1);
201 if (WSAStartup(winsock_ver
, &wsadata
)) {
202 MessageBox(NULL
, "Unable to initialise WinSock", "WinSock Error",
203 MB_OK
| MB_ICONEXCLAMATION
);
206 if (LOBYTE(wsadata
.wVersion
) != 1 || HIBYTE(wsadata
.wVersion
) != 1) {
207 MessageBox(NULL
, "WinSock version is incompatible with 1.1",
208 "WinSock Error", MB_OK
| MB_ICONEXCLAMATION
);
213 /* WISHLIST: maybe allow config tweaking even if winsock not present? */
216 InitCommonControls();
218 /* Ensure a Maximize setting in Explorer doesn't maximise the
224 char *str
= dupprintf("%s Fatal Error", appname
);
225 MessageBox(NULL
, "Windows refuses to report a version",
226 str
, MB_OK
| MB_ICONEXCLAMATION
);
232 * If we're running a version of Windows that doesn't support
233 * WM_MOUSEWHEEL, find out what message number we should be
236 if (osVersion
.dwMajorVersion
< 4 ||
237 (osVersion
.dwMajorVersion
== 4 &&
238 osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
))
239 wm_mousewheel
= RegisterWindowMessage("MSWHEEL_ROLLMSG");
242 * See if we can find our Help file.
245 char b
[2048], *p
, *q
, *r
;
247 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
249 p
= strrchr(b
, '\\');
250 if (p
&& p
>= r
) r
= p
+1;
252 if (q
&& q
>= r
) r
= q
+1;
253 strcpy(r
, "putty.hlp");
254 if ( (fp
= fopen(b
, "r")) != NULL
) {
255 help_path
= dupstr(b
);
259 strcpy(r
, "putty.cnt");
260 if ( (fp
= fopen(b
, "r")) != NULL
) {
261 help_has_contents
= TRUE
;
264 help_has_contents
= FALSE
;
268 * Process the command line.
274 default_protocol
= be_default_protocol
;
275 /* Find the appropriate default port. */
278 default_port
= 0; /* illegal */
279 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
280 if (backends
[i
].protocol
== default_protocol
) {
281 default_port
= backends
[i
].backend
->default_port
;
285 cfg
.logtype
= LGTYP_NONE
;
287 do_defaults(NULL
, &cfg
);
292 * Process a couple of command-line options which are more
293 * easily dealt with before the line is broken up into
294 * words. These are the soon-to-be-defunct @sessionname and
295 * the internal-use-only &sharedmemoryhandle, neither of
296 * which are combined with anything else.
298 while (*p
&& isspace(*p
))
302 while (i
> 1 && isspace(p
[i
- 1]))
305 do_defaults(p
+ 1, &cfg
);
306 if (!*cfg
.host
&& !do_config()) {
310 } else if (*p
== '&') {
312 * An initial & means we've been given a command line
313 * containing the hex value of a HANDLE for a file
314 * mapping object, which we must then extract as a
319 if (sscanf(p
+ 1, "%p", &filemap
) == 1 &&
320 (cp
= MapViewOfFile(filemap
, FILE_MAP_READ
,
321 0, 0, sizeof(Config
))) != NULL
) {
324 CloseHandle(filemap
);
325 } else if (!do_config()) {
331 * Otherwise, break up the command line and deal with
337 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
339 for (i
= 0; i
< argc
; i
++) {
343 ret
= cmdline_process_param(p
, i
+1<argc?argv
[i
+1]:NULL
,
346 cmdline_error("option \"%s\" requires an argument", p
);
347 } else if (ret
== 2) {
348 i
++; /* skip next argument */
349 } else if (ret
== 1) {
350 continue; /* nothing further needs doing */
351 } else if (!strcmp(p
, "-cleanup")) {
353 * `putty -cleanup'. Remove all registry
354 * entries associated with PuTTY, and also find
355 * and delete the random seed file.
358 s1
= dupprintf("This procedure will remove ALL Registry\n"
359 "entries associated with %s, and will\n"
360 "also remove the random seed file.\n"
362 "THIS PROCESS WILL DESTROY YOUR SAVED\n"
363 "SESSIONS. Are you really sure you want\n"
364 "to continue?", appname
);
365 s2
= dupprintf("%s Warning", appname
);
366 if (MessageBox(NULL
, s1
, s2
,
367 MB_YESNO
| MB_ICONWARNING
) == IDYES
) {
373 } else if (*p
!= '-') {
377 * If we already have a host name, treat
378 * this argument as a port number. NB we
379 * have to treat this as a saved -P
380 * argument, so that it will be deferred
381 * until it's a good moment to run it.
383 int ret
= cmdline_process_param("-P", p
, 1, &cfg
);
385 } else if (!strncmp(q
, "telnet:", 7)) {
387 * If the hostname starts with "telnet:",
388 * set the protocol to Telnet and process
389 * the string as a Telnet URL.
394 if (q
[0] == '/' && q
[1] == '/')
396 cfg
.protocol
= PROT_TELNET
;
398 while (*p
&& *p
!= ':' && *p
!= '/')
407 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
408 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
412 * Otherwise, treat this argument as a host
415 while (*p
&& !isspace(*p
))
419 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
420 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
424 cmdline_error("unknown option \"%s\"", p
);
429 cmdline_run_saved(&cfg
);
431 if (!*cfg
.host
&& !do_config()) {
437 * Trim leading whitespace off the hostname if it's there.
440 int space
= strspn(cfg
.host
, " \t");
441 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
444 /* See if host is of the form user@host */
445 if (cfg
.host
[0] != '\0') {
446 char *atsign
= strchr(cfg
.host
, '@');
447 /* Make sure we're not overflowing the user field */
449 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
450 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
451 cfg
.username
[atsign
- cfg
.host
] = '\0';
453 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
458 * Trim a colon suffix off the hostname if it's there.
460 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
463 * Remove any remaining whitespace from the hostname.
467 while (cfg
.host
[p2
] != '\0') {
468 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
469 cfg
.host
[p1
] = cfg
.host
[p2
];
479 * Select protocol. This is farmed out into a table in a
480 * separate file to enable an ssh-free variant.
485 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
486 if (backends
[i
].protocol
== cfg
.protocol
) {
487 back
= backends
[i
].backend
;
491 char *str
= dupprintf("%s Internal Error", appname
);
492 MessageBox(NULL
, "Unsupported protocol number found",
493 str
, MB_OK
| MB_ICONEXCLAMATION
);
500 /* Check for invalid Port number (i.e. zero) */
502 char *str
= dupprintf("%s Internal Error", appname
);
503 MessageBox(NULL
, "Invalid Port Number",
504 str
, MB_OK
| MB_ICONEXCLAMATION
);
512 wndclass
.lpfnWndProc
= WndProc
;
513 wndclass
.cbClsExtra
= 0;
514 wndclass
.cbWndExtra
= 0;
515 wndclass
.hInstance
= inst
;
516 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
517 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
518 wndclass
.hbrBackground
= NULL
;
519 wndclass
.lpszMenuName
= NULL
;
520 wndclass
.lpszClassName
= appname
;
522 RegisterClass(&wndclass
);
527 memset(&ucsdata
, 0, sizeof(ucsdata
));
529 term
= term_init(&cfg
, &ucsdata
, NULL
);
530 logctx
= log_init(NULL
, &cfg
);
531 term_provide_logctx(term
, logctx
);
536 * Guess some defaults for the window size. This all gets
537 * updated later, so we don't really care too much. However, we
538 * do want the font width/height guesses to correspond to a
539 * large font rather than a small one...
546 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
547 guess_width
= extra_width
+ font_width
* term
->cols
;
548 guess_height
= extra_height
+ font_height
* term
->rows
;
551 get_fullscreen_rect(&r
);
552 if (guess_width
> r
.right
- r
.left
)
553 guess_width
= r
.right
- r
.left
;
554 if (guess_height
> r
.bottom
- r
.top
)
555 guess_height
= r
.bottom
- r
.top
;
559 int winmode
= WS_OVERLAPPEDWINDOW
| WS_VSCROLL
;
562 winmode
&= ~(WS_VSCROLL
);
563 if (cfg
.resize_action
== RESIZE_DISABLED
)
564 winmode
&= ~(WS_THICKFRAME
| WS_MAXIMIZEBOX
);
566 exwinmode
|= WS_EX_TOPMOST
;
568 exwinmode
|= WS_EX_CLIENTEDGE
;
569 hwnd
= CreateWindowEx(exwinmode
, appname
, appname
,
570 winmode
, CW_USEDEFAULT
, CW_USEDEFAULT
,
571 guess_width
, guess_height
,
572 NULL
, NULL
, inst
, NULL
);
576 * Initialise the fonts, simultaneously correcting the guesses
577 * for font_{width,height}.
582 * Correct the guesses for extra_{width,height}.
586 GetWindowRect(hwnd
, &wr
);
587 GetClientRect(hwnd
, &cr
);
588 offset_width
= offset_height
= cfg
.window_border
;
589 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
590 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
594 * Resize the window, now we know what size we _really_ want it
597 guess_width
= extra_width
+ font_width
* term
->cols
;
598 guess_height
= extra_height
+ font_height
* term
->rows
;
599 SetWindowPos(hwnd
, NULL
, 0, 0, guess_width
, guess_height
,
600 SWP_NOMOVE
| SWP_NOREDRAW
| SWP_NOZORDER
);
603 * Set up a caret bitmap, with no content.
607 int size
= (font_width
+ 15) / 16 * 2 * font_height
;
608 bits
= snewn(size
, char);
609 memset(bits
, 0, size
);
610 caretbm
= CreateBitmap(font_width
, font_height
, 1, 1, bits
);
613 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
616 * Initialise the scroll bar.
621 si
.cbSize
= sizeof(si
);
622 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
624 si
.nMax
= term
->rows
- 1;
625 si
.nPage
= term
->rows
;
627 SetScrollInfo(hwnd
, SB_VERT
, &si
, FALSE
);
631 * Start up the telnet connection.
635 char msg
[1024], *title
;
638 error
= back
->init(NULL
, &backhandle
, &cfg
,
639 cfg
.host
, cfg
.port
, &realhost
, cfg
.tcp_nodelay
);
640 back
->provide_logctx(backhandle
, logctx
);
642 char *str
= dupprintf("%s Error", appname
);
643 sprintf(msg
, "Unable to open connection to\n"
644 "%.800s\n" "%s", cfg
.host
, error
);
645 MessageBox(NULL
, msg
, str
, MB_ICONERROR
| MB_OK
);
649 window_name
= icon_name
= NULL
;
651 title
= cfg
.wintitle
;
653 sprintf(msg
, "%s - %s", realhost
, appname
);
657 set_title(NULL
, title
);
658 set_icon(NULL
, title
);
662 * Connect the terminal to the backend for resize purposes.
664 term_provide_resize_fn(term
, back
->size
, backhandle
);
667 * Set up a line discipline.
669 ldisc
= ldisc_create(&cfg
, term
, back
, backhandle
, NULL
);
671 session_closed
= FALSE
;
674 * Prepare the mouse handler.
676 lastact
= MA_NOTHING
;
677 lastbtn
= MBT_NOTHING
;
678 dbltime
= GetDoubleClickTime();
681 * Set up the session-control options on the system menu.
684 HMENU m
= GetSystemMenu(hwnd
, FALSE
);
689 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
690 specials_menu_position
= GetMenuItemCount(m
);
691 AppendMenu(m
, MF_ENABLED
, IDM_SHOWLOG
, "&Event Log");
692 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
693 AppendMenu(m
, MF_ENABLED
, IDM_NEWSESS
, "Ne&w Session...");
694 AppendMenu(m
, MF_ENABLED
, IDM_DUPSESS
, "&Duplicate Session");
696 get_sesslist(&sesslist
, TRUE
);
698 i
< ((sesslist
.nsessions
< 256) ? sesslist
.nsessions
: 256);
700 AppendMenu(s
, MF_ENABLED
, IDM_SAVED_MIN
+ (16 * i
),
701 sesslist
.sessions
[i
]);
702 AppendMenu(m
, MF_POPUP
| MF_ENABLED
, (UINT
) s
, "Sa&ved Sessions");
703 AppendMenu(m
, MF_ENABLED
, IDM_RECONF
, "Chan&ge Settings...");
704 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
705 AppendMenu(m
, MF_ENABLED
, IDM_COPYALL
, "C&opy All to Clipboard");
706 AppendMenu(m
, MF_ENABLED
, IDM_CLRSB
, "C&lear Scrollback");
707 AppendMenu(m
, MF_ENABLED
, IDM_RESET
, "Rese&t Terminal");
708 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
709 AppendMenu(m
, (cfg
.resize_action
== RESIZE_DISABLED
) ?
710 MF_GRAYED
: MF_ENABLED
, IDM_FULLSCREEN
, "&Full Screen");
711 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
713 AppendMenu(m
, MF_ENABLED
, IDM_HELP
, "&Help");
714 str
= dupprintf("&About %s", appname
);
715 AppendMenu(m
, MF_ENABLED
, IDM_ABOUT
, str
);
719 update_specials_menu(NULL
);
722 * Set up the initial input locale.
724 set_input_locale(GetKeyboardLayout(0));
727 * Open the initial log file if there is one.
732 * Finally show the window!
734 ShowWindow(hwnd
, show
);
735 SetForegroundWindow(hwnd
);
738 * Set the palette up.
744 term
->has_focus
= (GetForegroundWindow() == hwnd
);
747 if (GetMessage(&msg
, NULL
, 0, 0) == 1) {
748 int timer_id
= 0, long_timer
= 0;
750 while (msg
.message
!= WM_QUIT
) {
751 /* Sometimes DispatchMessage calls routines that use their own
752 * GetMessage loop, setup this timer so we get some control back.
754 * Also call term_update() from the timer so that if the host
755 * is sending data flat out we still do redraws.
757 if (timer_id
&& long_timer
) {
758 KillTimer(hwnd
, timer_id
);
759 long_timer
= timer_id
= 0;
762 timer_id
= SetTimer(hwnd
, 1, 20, NULL
);
763 if (!(IsWindow(logbox
) && IsDialogMessage(logbox
, &msg
)))
764 DispatchMessage(&msg
);
766 /* Make sure we blink everything that needs it. */
769 /* Send the paste buffer if there's anything to send */
772 /* If there's nothing new in the queue then we can do everything
773 * we've delayed, reading the socket, writing, and repainting
776 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
779 if (pending_netevent
) {
780 enact_pending_netevent();
782 if (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
))
786 /* Okay there is now nothing to do so we make sure the screen is
787 * completely up to date then tell windows to call us in a little
791 KillTimer(hwnd
, timer_id
);
795 if (GetCapture() != hwnd
||
797 !(cfg
.mouse_override
&& is_shift_pressed())))
802 flash_window(1); /* maintain */
804 /* The messages seem unreliable; especially if we're being tricky */
805 term
->has_focus
= (GetForegroundWindow() == hwnd
);
808 /* Hmm, term_update didn't want to do an update too soon ... */
809 timer_id
= SetTimer(hwnd
, 1, 50, NULL
);
810 else if (!term
->has_focus
)
811 timer_id
= SetTimer(hwnd
, 1, 500, NULL
);
813 timer_id
= SetTimer(hwnd
, 1, 100, NULL
);
816 /* There's no point rescanning everything in the message queue
817 * so we do an apparently unnecessary wait here
820 if (GetMessage(&msg
, NULL
, 0, 0) != 1)
825 cleanup_exit(msg
.wParam
); /* this doesn't return... */
826 return msg
.wParam
; /* ... but optimiser doesn't know */
832 void cleanup_exit(int code
)
845 if (cfg
.protocol
== PROT_SSH
) {
856 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
858 char *do_select(SOCKET skt
, int startup
)
863 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
864 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
869 return "do_select(): internal error (hwnd==NULL)";
870 if (WSAAsyncSelect(skt
, hwnd
, msg
, events
) == SOCKET_ERROR
) {
871 switch (WSAGetLastError()) {
873 return "Network is down";
875 return "WSAAsyncSelect(): unknown error";
882 * Update the Special Commands submenu.
884 void update_specials_menu(void *frontend
)
886 HMENU m
= GetSystemMenu(hwnd
, FALSE
);
887 int menu_already_exists
= (specials
!= NULL
);
890 specials
= back
->get_specials(backhandle
);
892 HMENU p
= CreateMenu();
893 for (i
= 0; specials
[i
].name
; i
++) {
894 assert(IDM_SPECIAL_MIN
+ 0x10 * i
< IDM_SPECIAL_MAX
);
895 if (*specials
[i
].name
)
896 AppendMenu(p
, MF_ENABLED
, IDM_SPECIAL_MIN
+ 0x10 * i
,
899 AppendMenu(p
, MF_SEPARATOR
, 0, 0);
901 if (menu_already_exists
)
902 DeleteMenu(m
, specials_menu_position
, MF_BYPOSITION
);
904 InsertMenu(m
, specials_menu_position
,
905 MF_BYPOSITION
| MF_SEPARATOR
, 0, 0);
906 InsertMenu(m
, specials_menu_position
,
907 MF_BYPOSITION
| MF_POPUP
| MF_ENABLED
,
908 (UINT
) p
, "Special Command");
913 * set or clear the "raw mouse message" mode
915 void set_raw_mouse_mode(void *frontend
, int activate
)
917 activate
= activate
&& !cfg
.no_mouse_rep
;
918 send_raw_mouse
= activate
;
919 SetCursor(LoadCursor(NULL
, activate ? IDC_ARROW
: IDC_IBEAM
));
923 * Print a message box and close the connection.
925 void connection_fatal(void *frontend
, char *fmt
, ...)
928 char *stuff
, morestuff
[100];
931 stuff
= dupvprintf(fmt
, ap
);
933 sprintf(morestuff
, "%.70s Fatal Error", appname
);
934 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
937 if (cfg
.close_on_exit
== FORCE_ON
)
940 session_closed
= TRUE
;
941 sprintf(morestuff
, "%.70s (inactive)", appname
);
942 set_icon(NULL
, morestuff
);
943 set_title(NULL
, morestuff
);
948 * Report an error at the command-line parsing stage.
950 void cmdline_error(char *fmt
, ...)
953 char *stuff
, morestuff
[100];
956 stuff
= dupvprintf(fmt
, ap
);
958 sprintf(morestuff
, "%.70s Command Line Error", appname
);
959 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
965 * Actually do the job requested by a WM_NETEVENT
967 static void enact_pending_netevent(void)
969 static int reentering
= 0;
970 extern int select_result(WPARAM
, LPARAM
);
974 return; /* don't unpend the pending */
976 pending_netevent
= FALSE
;
979 ret
= select_result(pend_netevent_wParam
, pend_netevent_lParam
);
982 if (ret
== 0 && !session_closed
) {
983 /* Abnormal exits will already have set session_closed and taken
984 * appropriate action. */
985 if (cfg
.close_on_exit
== FORCE_ON
||
986 cfg
.close_on_exit
== AUTO
) PostQuitMessage(0);
989 session_closed
= TRUE
;
990 sprintf(morestuff
, "%.70s (inactive)", appname
);
991 set_icon(NULL
, morestuff
);
992 set_title(NULL
, morestuff
);
993 MessageBox(hwnd
, "Connection closed by remote host",
994 appname
, MB_OK
| MB_ICONINFORMATION
);
1000 * Copy the colour palette from the configuration data into defpal.
1001 * This is non-trivial because the colour indices are different.
1003 static void cfgtopalette(void)
1006 static const int ww
[] = {
1007 6, 7, 8, 9, 10, 11, 12, 13,
1008 14, 15, 16, 17, 18, 19, 20, 21,
1009 0, 1, 2, 3, 4, 4, 5, 5
1012 for (i
= 0; i
< 24; i
++) {
1014 defpal
[i
].rgbtRed
= cfg
.colours
[w
][0];
1015 defpal
[i
].rgbtGreen
= cfg
.colours
[w
][1];
1016 defpal
[i
].rgbtBlue
= cfg
.colours
[w
][2];
1021 * Set up the colour palette.
1023 static void init_palette(void)
1026 HDC hdc
= GetDC(hwnd
);
1028 if (cfg
.try_palette
&& GetDeviceCaps(hdc
, RASTERCAPS
) & RC_PALETTE
) {
1030 * This is a genuine case where we must use smalloc
1031 * because the snew macros can't cope.
1033 logpal
= smalloc(sizeof(*logpal
)
1034 - sizeof(logpal
->palPalEntry
)
1035 + NCOLOURS
* sizeof(PALETTEENTRY
));
1036 logpal
->palVersion
= 0x300;
1037 logpal
->palNumEntries
= NCOLOURS
;
1038 for (i
= 0; i
< NCOLOURS
; i
++) {
1039 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
1040 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
1041 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
1042 logpal
->palPalEntry
[i
].peFlags
= PC_NOCOLLAPSE
;
1044 pal
= CreatePalette(logpal
);
1046 SelectPalette(hdc
, pal
, FALSE
);
1047 RealizePalette(hdc
);
1048 SelectPalette(hdc
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
1051 ReleaseDC(hwnd
, hdc
);
1054 for (i
= 0; i
< NCOLOURS
; i
++)
1055 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
1056 defpal
[i
].rgbtGreen
,
1057 defpal
[i
].rgbtBlue
);
1059 for (i
= 0; i
< NCOLOURS
; i
++)
1060 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
1061 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
1065 * Initialise all the fonts we will need initially. There may be as many as
1066 * three or as few as one. The other (poentially) twentyone fonts are done
1067 * if/when they are needed.
1071 * - check the font width and height, correcting our guesses if
1074 * - verify that the bold font is the same width as the ordinary
1075 * one, and engage shadow bolding if not.
1077 * - verify that the underlined font is the same width as the
1078 * ordinary one (manual underlining by means of line drawing can
1079 * be done in a pinch).
1081 static void init_fonts(int pick_width
, int pick_height
)
1088 int fw_dontcare
, fw_bold
;
1090 for (i
= 0; i
< FONT_MAXNO
; i
++)
1093 bold_mode
= cfg
.bold_colour ? BOLD_COLOURS
: BOLD_FONT
;
1094 und_mode
= UND_FONT
;
1096 if (cfg
.font
.isbold
) {
1097 fw_dontcare
= FW_BOLD
;
1100 fw_dontcare
= FW_DONTCARE
;
1107 font_height
= pick_height
;
1109 font_height
= cfg
.font
.height
;
1110 if (font_height
> 0) {
1112 -MulDiv(font_height
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1115 font_width
= pick_width
;
1117 #define f(i,c,w,u) \
1118 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1119 c, OUT_DEFAULT_PRECIS, \
1120 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
1121 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1123 f(FONT_NORMAL
, cfg
.font
.charset
, fw_dontcare
, FALSE
);
1125 lfont
.lfHeight
= font_height
;
1126 lfont
.lfWidth
= font_width
;
1127 lfont
.lfEscapement
= 0;
1128 lfont
.lfOrientation
= 0;
1129 lfont
.lfWeight
= fw_dontcare
;
1130 lfont
.lfItalic
= FALSE
;
1131 lfont
.lfUnderline
= FALSE
;
1132 lfont
.lfStrikeOut
= FALSE
;
1133 lfont
.lfCharSet
= cfg
.font
.charset
;
1134 lfont
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1135 lfont
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1136 lfont
.lfQuality
= DEFAULT_QUALITY
;
1137 lfont
.lfPitchAndFamily
= FIXED_PITCH
| FF_DONTCARE
;
1138 strncpy(lfont
.lfFaceName
, cfg
.font
.name
, LF_FACESIZE
);
1140 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
1141 GetTextMetrics(hdc
, &tm
);
1143 if (pick_width
== 0 || pick_height
== 0) {
1144 font_height
= tm
.tmHeight
;
1145 font_width
= tm
.tmAveCharWidth
;
1147 font_dualwidth
= (tm
.tmAveCharWidth
!= tm
.tmMaxCharWidth
);
1149 #ifdef RDB_DEBUG_PATCH
1150 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1151 tm
.tmHeight
, tm
.tmAveCharWidth
, tm
.tmMaxCharWidth
);
1156 DWORD cset
= tm
.tmCharSet
;
1157 memset(&info
, 0xFF, sizeof(info
));
1159 /* !!! Yes the next line is right */
1160 if (cset
== OEM_CHARSET
)
1161 ucsdata
.font_codepage
= GetOEMCP();
1163 if (TranslateCharsetInfo ((DWORD
*) cset
, &info
, TCI_SRCCHARSET
))
1164 ucsdata
.font_codepage
= info
.ciACP
;
1166 ucsdata
.font_codepage
= -1;
1168 GetCPInfo(ucsdata
.font_codepage
, &cpinfo
);
1169 ucsdata
.dbcs_screenfont
= (cpinfo
.MaxCharSize
> 1);
1172 f(FONT_UNDERLINE
, cfg
.font
.charset
, fw_dontcare
, TRUE
);
1175 * Some fonts, e.g. 9-pt Courier, draw their underlines
1176 * outside their character cell. We successfully prevent
1177 * screen corruption by clipping the text output, but then
1178 * we lose the underline completely. Here we try to work
1179 * out whether this is such a font, and if it is, we set a
1180 * flag that causes underlines to be drawn by hand.
1182 * Having tried other more sophisticated approaches (such
1183 * as examining the TEXTMETRIC structure or requesting the
1184 * height of a string), I think we'll do this the brute
1185 * force way: we create a small bitmap, draw an underlined
1186 * space on it, and test to see whether any pixels are
1187 * foreground-coloured. (Since we expect the underline to
1188 * go all the way across the character cell, we only search
1189 * down a single column of the bitmap, half way across.)
1193 HBITMAP und_bm
, und_oldbm
;
1197 und_dc
= CreateCompatibleDC(hdc
);
1198 und_bm
= CreateCompatibleBitmap(hdc
, font_width
, font_height
);
1199 und_oldbm
= SelectObject(und_dc
, und_bm
);
1200 SelectObject(und_dc
, fonts
[FONT_UNDERLINE
]);
1201 SetTextAlign(und_dc
, TA_TOP
| TA_LEFT
| TA_NOUPDATECP
);
1202 SetTextColor(und_dc
, RGB(255, 255, 255));
1203 SetBkColor(und_dc
, RGB(0, 0, 0));
1204 SetBkMode(und_dc
, OPAQUE
);
1205 ExtTextOut(und_dc
, 0, 0, ETO_OPAQUE
, NULL
, " ", 1, NULL
);
1207 for (i
= 0; i
< font_height
; i
++) {
1208 c
= GetPixel(und_dc
, font_width
/ 2, i
);
1209 if (c
!= RGB(0, 0, 0))
1212 SelectObject(und_dc
, und_oldbm
);
1213 DeleteObject(und_bm
);
1216 und_mode
= UND_LINE
;
1217 DeleteObject(fonts
[FONT_UNDERLINE
]);
1218 fonts
[FONT_UNDERLINE
] = 0;
1222 if (bold_mode
== BOLD_FONT
) {
1223 f(FONT_BOLD
, cfg
.font
.charset
, fw_bold
, FALSE
);
1227 descent
= tm
.tmAscent
+ 1;
1228 if (descent
>= font_height
)
1229 descent
= font_height
- 1;
1231 for (i
= 0; i
< 3; i
++) {
1233 if (SelectObject(hdc
, fonts
[i
]) && GetTextMetrics(hdc
, &tm
))
1234 fontsize
[i
] = tm
.tmAveCharWidth
+ 256 * tm
.tmHeight
;
1241 ReleaseDC(hwnd
, hdc
);
1243 if (fontsize
[FONT_UNDERLINE
] != fontsize
[FONT_NORMAL
]) {
1244 und_mode
= UND_LINE
;
1245 DeleteObject(fonts
[FONT_UNDERLINE
]);
1246 fonts
[FONT_UNDERLINE
] = 0;
1249 if (bold_mode
== BOLD_FONT
&&
1250 fontsize
[FONT_BOLD
] != fontsize
[FONT_NORMAL
]) {
1251 bold_mode
= BOLD_SHADOW
;
1252 DeleteObject(fonts
[FONT_BOLD
]);
1253 fonts
[FONT_BOLD
] = 0;
1255 fontflag
[0] = fontflag
[1] = fontflag
[2] = 1;
1257 init_ucs(&cfg
, &ucsdata
);
1260 static void another_font(int fontno
)
1263 int fw_dontcare
, fw_bold
;
1267 if (fontno
< 0 || fontno
>= FONT_MAXNO
|| fontflag
[fontno
])
1270 basefont
= (fontno
& ~(FONT_BOLDUND
));
1271 if (basefont
!= fontno
&& !fontflag
[basefont
])
1272 another_font(basefont
);
1274 if (cfg
.font
.isbold
) {
1275 fw_dontcare
= FW_BOLD
;
1278 fw_dontcare
= FW_DONTCARE
;
1282 c
= cfg
.font
.charset
;
1288 if (fontno
& FONT_WIDE
)
1290 if (fontno
& FONT_NARROW
)
1292 if (fontno
& FONT_OEM
)
1294 if (fontno
& FONT_BOLD
)
1296 if (fontno
& FONT_UNDERLINE
)
1300 CreateFont(font_height
* (1 + !!(fontno
& FONT_HIGH
)), x
, 0, 0, w
,
1301 FALSE
, u
, FALSE
, c
, OUT_DEFAULT_PRECIS
,
1302 CLIP_DEFAULT_PRECIS
, DEFAULT_QUALITY
,
1303 FIXED_PITCH
| FF_DONTCARE
, s
);
1305 fontflag
[fontno
] = 1;
1308 static void deinit_fonts(void)
1311 for (i
= 0; i
< FONT_MAXNO
; i
++) {
1313 DeleteObject(fonts
[i
]);
1319 void request_resize(void *frontend
, int w
, int h
)
1323 /* If the window is maximized supress resizing attempts */
1324 if (IsZoomed(hwnd
)) {
1325 if (cfg
.resize_action
== RESIZE_TERM
)
1329 if (cfg
.resize_action
== RESIZE_DISABLED
) return;
1330 if (h
== term
->rows
&& w
== term
->cols
) return;
1332 /* Sanity checks ... */
1334 static int first_time
= 1;
1337 switch (first_time
) {
1339 /* Get the size of the screen */
1340 if (get_fullscreen_rect(&ss
))
1341 /* first_time = 0 */ ;
1347 /* Make sure the values are sane */
1348 width
= (ss
.right
- ss
.left
- extra_width
) / 4;
1349 height
= (ss
.bottom
- ss
.top
- extra_height
) / 6;
1351 if (w
> width
|| h
> height
)
1360 term_size(term
, h
, w
, cfg
.savelines
);
1362 if (cfg
.resize_action
!= RESIZE_FONT
&& !IsZoomed(hwnd
)) {
1363 width
= extra_width
+ font_width
* w
;
1364 height
= extra_height
+ font_height
* h
;
1366 SetWindowPos(hwnd
, NULL
, 0, 0, width
, height
,
1367 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1368 SWP_NOMOVE
| SWP_NOZORDER
);
1372 InvalidateRect(hwnd
, NULL
, TRUE
);
1375 static void reset_window(int reinit
) {
1377 * This function decides how to resize or redraw when the
1378 * user changes something.
1380 * This function doesn't like to change the terminal size but if the
1381 * font size is locked that may be it's only soluion.
1383 int win_width
, win_height
;
1386 #ifdef RDB_DEBUG_PATCH
1387 debug((27, "reset_window()"));
1390 /* Current window sizes ... */
1391 GetWindowRect(hwnd
, &wr
);
1392 GetClientRect(hwnd
, &cr
);
1394 win_width
= cr
.right
- cr
.left
;
1395 win_height
= cr
.bottom
- cr
.top
;
1397 if (cfg
.resize_action
== RESIZE_DISABLED
) reinit
= 2;
1399 /* Are we being forced to reload the fonts ? */
1401 #ifdef RDB_DEBUG_PATCH
1402 debug((27, "reset_window() -- Forced deinit"));
1408 /* Oh, looks like we're minimised */
1409 if (win_width
== 0 || win_height
== 0)
1412 /* Is the window out of position ? */
1414 (offset_width
!= (win_width
-font_width
*term
->cols
)/2 ||
1415 offset_height
!= (win_height
-font_height
*term
->rows
)/2) ){
1416 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1417 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1418 InvalidateRect(hwnd
, NULL
, TRUE
);
1419 #ifdef RDB_DEBUG_PATCH
1420 debug((27, "reset_window() -> Reposition terminal"));
1424 if (IsZoomed(hwnd
)) {
1425 /* We're fullscreen, this means we must not change the size of
1426 * the window so it's the font size or the terminal itself.
1429 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
;
1430 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
;
1432 if (cfg
.resize_action
!= RESIZE_TERM
) {
1433 if ( font_width
!= win_width
/term
->cols
||
1434 font_height
!= win_height
/term
->rows
) {
1436 init_fonts(win_width
/term
->cols
, win_height
/term
->rows
);
1437 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1438 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1439 InvalidateRect(hwnd
, NULL
, TRUE
);
1440 #ifdef RDB_DEBUG_PATCH
1441 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1442 font_width
, font_height
));
1446 if ( font_width
!= win_width
/term
->cols
||
1447 font_height
!= win_height
/term
->rows
) {
1448 /* Our only choice at this point is to change the
1449 * size of the terminal; Oh well.
1451 term_size(term
, win_height
/font_height
, win_width
/font_width
,
1453 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1454 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1455 InvalidateRect(hwnd
, NULL
, TRUE
);
1456 #ifdef RDB_DEBUG_PATCH
1457 debug((27, "reset_window() -> Zoomed term_size"));
1464 /* Hmm, a force re-init means we should ignore the current window
1465 * so we resize to the default font size.
1468 #ifdef RDB_DEBUG_PATCH
1469 debug((27, "reset_window() -> Forced re-init"));
1472 offset_width
= offset_height
= cfg
.window_border
;
1473 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1474 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1476 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1477 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1479 /* If this is too large windows will resize it to the maximum
1480 * allowed window size, we will then be back in here and resize
1481 * the font or terminal to fit.
1483 SetWindowPos(hwnd
, NULL
, 0, 0,
1484 font_width
*term
->cols
+ extra_width
,
1485 font_height
*term
->rows
+ extra_height
,
1486 SWP_NOMOVE
| SWP_NOZORDER
);
1489 InvalidateRect(hwnd
, NULL
, TRUE
);
1493 /* Okay the user doesn't want us to change the font so we try the
1494 * window. But that may be too big for the screen which forces us
1495 * to change the terminal.
1497 if ((cfg
.resize_action
== RESIZE_TERM
&& reinit
<=0) ||
1498 (cfg
.resize_action
== RESIZE_EITHER
&& reinit
<0) ||
1500 offset_width
= offset_height
= cfg
.window_border
;
1501 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1502 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1504 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1505 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1510 get_fullscreen_rect(&ss
);
1512 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1513 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1516 if ( term
->rows
> height
|| term
->cols
> width
) {
1517 if (cfg
.resize_action
== RESIZE_EITHER
) {
1518 /* Make the font the biggest we can */
1519 if (term
->cols
> width
)
1520 font_width
= (ss
.right
- ss
.left
- extra_width
)
1522 if (term
->rows
> height
)
1523 font_height
= (ss
.bottom
- ss
.top
- extra_height
)
1527 init_fonts(font_width
, font_height
);
1529 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1530 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1532 if ( height
> term
->rows
) height
= term
->rows
;
1533 if ( width
> term
->cols
) width
= term
->cols
;
1534 term_size(term
, height
, width
, cfg
.savelines
);
1535 #ifdef RDB_DEBUG_PATCH
1536 debug((27, "reset_window() -> term resize to (%d,%d)",
1542 SetWindowPos(hwnd
, NULL
, 0, 0,
1543 font_width
*term
->cols
+ extra_width
,
1544 font_height
*term
->rows
+ extra_height
,
1545 SWP_NOMOVE
| SWP_NOZORDER
);
1547 InvalidateRect(hwnd
, NULL
, TRUE
);
1548 #ifdef RDB_DEBUG_PATCH
1549 debug((27, "reset_window() -> window resize to (%d,%d)",
1550 font_width
*term
->cols
+ extra_width
,
1551 font_height
*term
->rows
+ extra_height
));
1557 /* We're allowed to or must change the font but do we want to ? */
1559 if (font_width
!= (win_width
-cfg
.window_border
*2)/term
->cols
||
1560 font_height
!= (win_height
-cfg
.window_border
*2)/term
->rows
) {
1563 init_fonts((win_width
-cfg
.window_border
*2)/term
->cols
,
1564 (win_height
-cfg
.window_border
*2)/term
->rows
);
1565 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1566 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1568 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+offset_width
*2;
1569 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1571 InvalidateRect(hwnd
, NULL
, TRUE
);
1572 #ifdef RDB_DEBUG_PATCH
1573 debug((25, "reset_window() -> font resize to (%d,%d)",
1574 font_width
, font_height
));
1579 static void set_input_locale(HKL kl
)
1583 GetLocaleInfo(LOWORD(kl
), LOCALE_IDEFAULTANSICODEPAGE
,
1584 lbuf
, sizeof(lbuf
));
1586 kbd_codepage
= atoi(lbuf
);
1589 static void click(Mouse_Button b
, int x
, int y
, int shift
, int ctrl
, int alt
)
1591 int thistime
= GetMessageTime();
1593 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
)) {
1594 lastbtn
= MBT_NOTHING
;
1595 term_mouse(term
, b
, translate_button(b
), MA_CLICK
,
1596 x
, y
, shift
, ctrl
, alt
);
1600 if (lastbtn
== b
&& thistime
- lasttime
< dbltime
) {
1601 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
1602 lastact
== MA_2CLK ? MA_3CLK
:
1603 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
1608 if (lastact
!= MA_NOTHING
)
1609 term_mouse(term
, b
, translate_button(b
), lastact
,
1610 x
, y
, shift
, ctrl
, alt
);
1611 lasttime
= thistime
;
1615 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1616 * into a cooked one (SELECT, EXTEND, PASTE).
1618 static Mouse_Button
translate_button(Mouse_Button button
)
1620 if (button
== MBT_LEFT
)
1622 if (button
== MBT_MIDDLE
)
1623 return cfg
.mouse_is_xterm ? MBT_PASTE
: MBT_EXTEND
;
1624 if (button
== MBT_RIGHT
)
1625 return cfg
.mouse_is_xterm ? MBT_EXTEND
: MBT_PASTE
;
1626 return 0; /* shouldn't happen */
1629 static void show_mouseptr(int show
)
1631 static int cursor_visible
= 1;
1632 if (!cfg
.hide_mouseptr
) /* override if this feature disabled */
1634 if (cursor_visible
&& !show
)
1636 else if (!cursor_visible
&& show
)
1638 cursor_visible
= show
;
1641 static int is_alt_pressed(void)
1644 int r
= GetKeyboardState(keystate
);
1647 if (keystate
[VK_MENU
] & 0x80)
1649 if (keystate
[VK_RMENU
] & 0x80)
1654 static int is_shift_pressed(void)
1657 int r
= GetKeyboardState(keystate
);
1660 if (keystate
[VK_SHIFT
] & 0x80)
1665 static int resizing
;
1667 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1668 WPARAM wParam
, LPARAM lParam
)
1671 static int ignore_clip
= FALSE
;
1672 static int need_backend_resize
= FALSE
;
1673 static int fullscr_on_max
= FALSE
;
1677 if (pending_netevent
)
1678 enact_pending_netevent();
1679 if (GetCapture() != hwnd
||
1680 (send_raw_mouse
&& !(cfg
.mouse_override
&& is_shift_pressed())))
1686 if (cfg
.ping_interval
> 0) {
1689 if (now
- last_movement
> cfg
.ping_interval
) {
1690 back
->special(backhandle
, TS_PING
);
1691 last_movement
= now
;
1694 net_pending_errors();
1702 str
= dupprintf("%s Exit Confirmation", appname
);
1703 if (!cfg
.warn_on_close
|| session_closed
||
1705 "Are you sure you want to close this session?",
1706 str
, MB_ICONWARNING
| MB_OKCANCEL
) == IDOK
)
1707 DestroyWindow(hwnd
);
1716 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1728 PROCESS_INFORMATION pi
;
1729 HANDLE filemap
= NULL
;
1731 if (wParam
== IDM_DUPSESS
) {
1733 * Allocate a file-mapping memory chunk for the
1736 SECURITY_ATTRIBUTES sa
;
1739 sa
.nLength
= sizeof(sa
);
1740 sa
.lpSecurityDescriptor
= NULL
;
1741 sa
.bInheritHandle
= TRUE
;
1742 filemap
= CreateFileMapping((HANDLE
) 0xFFFFFFFF,
1745 0, sizeof(Config
), NULL
);
1747 p
= (Config
*) MapViewOfFile(filemap
,
1749 0, 0, sizeof(Config
));
1751 *p
= cfg
; /* structure copy */
1755 sprintf(c
, "putty &%p", filemap
);
1757 } else if (wParam
== IDM_SAVEDSESS
) {
1758 if ((lParam
- IDM_SAVED_MIN
) / 16 < sesslist
.nsessions
) {
1760 sesslist
.sessions
[(lParam
- IDM_SAVED_MIN
) / 16];
1761 cl
= snewn(16 + strlen(session
), char);
1762 /* 8, but play safe */
1765 /* not a very important failure mode */
1767 sprintf(cl
, "putty @%s", session
);
1775 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
1777 si
.lpReserved
= NULL
;
1778 si
.lpDesktop
= NULL
;
1782 si
.lpReserved2
= NULL
;
1783 CreateProcess(b
, cl
, NULL
, NULL
, TRUE
,
1784 NORMAL_PRIORITY_CLASS
, NULL
, NULL
, &si
, &pi
);
1787 CloseHandle(filemap
);
1797 GetWindowText(hwnd
, cfg
.wintitle
, sizeof(cfg
.wintitle
));
1800 if (!do_reconfig(hwnd
))
1804 /* Disable full-screen if resizing forbidden */
1805 HMENU m
= GetSystemMenu (hwnd
, FALSE
);
1806 EnableMenuItem(m
, IDM_FULLSCREEN
, MF_BYCOMMAND
|
1807 (cfg
.resize_action
== RESIZE_DISABLED
)
1808 ? MF_GRAYED
: MF_ENABLED
);
1809 /* Gracefully unzoom if necessary */
1810 if (IsZoomed(hwnd
) &&
1811 (cfg
.resize_action
== RESIZE_DISABLED
)) {
1812 ShowWindow(hwnd
, SW_RESTORE
);
1816 /* Pass new config data to the logging module */
1817 log_reconfig(logctx
, &cfg
);
1821 * Flush the line discipline's edit buffer in the
1822 * case where local editing has just been disabled.
1824 ldisc_send(ldisc
, NULL
, 0, 0);
1832 /* Pass new config data to the terminal */
1833 term_reconfig(term
, &cfg
);
1835 /* Pass new config data to the back end */
1836 back
->reconfig(backhandle
, &cfg
);
1838 /* Screen size changed ? */
1839 if (cfg
.height
!= prev_cfg
.height
||
1840 cfg
.width
!= prev_cfg
.width
||
1841 cfg
.savelines
!= prev_cfg
.savelines
||
1842 cfg
.resize_action
== RESIZE_FONT
||
1843 (cfg
.resize_action
== RESIZE_EITHER
&& IsZoomed(hwnd
)) ||
1844 cfg
.resize_action
== RESIZE_DISABLED
)
1845 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
1847 /* Enable or disable the scroll bar, etc */
1849 LONG nflg
, flag
= GetWindowLong(hwnd
, GWL_STYLE
);
1850 LONG nexflag
, exflag
=
1851 GetWindowLong(hwnd
, GWL_EXSTYLE
);
1854 if (cfg
.alwaysontop
!= prev_cfg
.alwaysontop
) {
1855 if (cfg
.alwaysontop
) {
1856 nexflag
|= WS_EX_TOPMOST
;
1857 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
1858 SWP_NOMOVE
| SWP_NOSIZE
);
1860 nexflag
&= ~(WS_EX_TOPMOST
);
1861 SetWindowPos(hwnd
, HWND_NOTOPMOST
, 0, 0, 0, 0,
1862 SWP_NOMOVE
| SWP_NOSIZE
);
1865 if (cfg
.sunken_edge
)
1866 nexflag
|= WS_EX_CLIENTEDGE
;
1868 nexflag
&= ~(WS_EX_CLIENTEDGE
);
1871 if (is_full_screen() ?
1872 cfg
.scrollbar_in_fullscreen
: cfg
.scrollbar
)
1875 nflg
&= ~WS_VSCROLL
;
1877 if (cfg
.resize_action
== RESIZE_DISABLED
||
1879 nflg
&= ~WS_THICKFRAME
;
1881 nflg
|= WS_THICKFRAME
;
1883 if (cfg
.resize_action
== RESIZE_DISABLED
)
1884 nflg
&= ~WS_MAXIMIZEBOX
;
1886 nflg
|= WS_MAXIMIZEBOX
;
1888 if (nflg
!= flag
|| nexflag
!= exflag
) {
1890 SetWindowLong(hwnd
, GWL_STYLE
, nflg
);
1891 if (nexflag
!= exflag
)
1892 SetWindowLong(hwnd
, GWL_EXSTYLE
, nexflag
);
1894 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
1895 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1896 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
1904 if (cfg
.resize_action
== RESIZE_DISABLED
&& IsZoomed(hwnd
)) {
1909 set_title(NULL
, cfg
.wintitle
);
1910 if (IsIconic(hwnd
)) {
1912 cfg
.win_name_always ? window_name
:
1916 if (strcmp(cfg
.font
.name
, prev_cfg
.font
.name
) != 0 ||
1917 strcmp(cfg
.line_codepage
, prev_cfg
.line_codepage
) != 0 ||
1918 cfg
.font
.isbold
!= prev_cfg
.font
.isbold
||
1919 cfg
.font
.height
!= prev_cfg
.font
.height
||
1920 cfg
.font
.charset
!= prev_cfg
.font
.charset
||
1921 cfg
.vtmode
!= prev_cfg
.vtmode
||
1922 cfg
.bold_colour
!= prev_cfg
.bold_colour
||
1923 cfg
.resize_action
== RESIZE_DISABLED
||
1924 cfg
.resize_action
== RESIZE_EITHER
||
1925 (cfg
.resize_action
!= prev_cfg
.resize_action
))
1928 InvalidateRect(hwnd
, NULL
, TRUE
);
1929 reset_window(init_lvl
);
1930 net_pending_errors();
1941 ldisc_send(ldisc
, NULL
, 0, 0);
1947 WinHelp(hwnd
, help_path
,
1948 help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
1952 * We get this if the System menu has been activated
1959 * We get this if the System menu has been activated
1960 * using the keyboard. This might happen from within
1961 * TranslateKey, in which case it really wants to be
1962 * followed by a `space' character to actually _bring
1963 * the menu up_ rather than just sitting there in
1964 * `ready to appear' state.
1966 show_mouseptr(1); /* make sure pointer is visible */
1968 PostMessage(hwnd
, WM_CHAR
, ' ', 0);
1970 case IDM_FULLSCREEN
:
1974 if (wParam
>= IDM_SAVED_MIN
&& wParam
<= IDM_SAVED_MAX
) {
1975 SendMessage(hwnd
, WM_SYSCOMMAND
, IDM_SAVEDSESS
, wParam
);
1977 if (wParam
>= IDM_SPECIAL_MIN
&& wParam
<= IDM_SPECIAL_MAX
) {
1978 int i
= (wParam
- IDM_SPECIAL_MIN
) / 0x10;
1981 * Ensure we haven't been sent a bogus SYSCOMMAND
1982 * which would cause us to reference invalid memory
1983 * and crash. Perhaps I'm just too paranoid here.
1985 for (j
= 0; j
< i
; j
++)
1986 if (!specials
|| !specials
[j
].name
)
1989 back
->special(backhandle
, specials
[i
].code
);
1990 net_pending_errors();
1996 #define X_POS(l) ((int)(short)LOWORD(l))
1997 #define Y_POS(l) ((int)(short)HIWORD(l))
1999 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2000 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2001 case WM_LBUTTONDOWN
:
2002 case WM_MBUTTONDOWN
:
2003 case WM_RBUTTONDOWN
:
2011 case WM_LBUTTONDOWN
:
2015 case WM_MBUTTONDOWN
:
2016 button
= MBT_MIDDLE
;
2019 case WM_RBUTTONDOWN
:
2028 button
= MBT_MIDDLE
;
2036 button
= press
= 0; /* shouldn't happen */
2040 * Special case: in full-screen mode, if the left
2041 * button is clicked in the very top left corner of the
2042 * window, we put up the System menu instead of doing
2045 if (is_full_screen() && press
&& button
== MBT_LEFT
&&
2046 X_POS(lParam
) == 0 && Y_POS(lParam
) == 0) {
2047 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MOUSEMENU
, 0);
2052 TO_CHR_X(X_POS(lParam
)), TO_CHR_Y(Y_POS(lParam
)),
2053 wParam
& MK_SHIFT
, wParam
& MK_CONTROL
,
2057 term_mouse(term
, button
, translate_button(button
), MA_RELEASE
,
2058 TO_CHR_X(X_POS(lParam
)),
2059 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2060 wParam
& MK_CONTROL
, is_alt_pressed());
2068 * Add the mouse position and message time to the random
2071 noise_ultralight(lParam
);
2073 if (wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
) &&
2074 GetCapture() == hwnd
) {
2076 if (wParam
& MK_LBUTTON
)
2078 else if (wParam
& MK_MBUTTON
)
2082 term_mouse(term
, b
, translate_button(b
), MA_DRAG
,
2083 TO_CHR_X(X_POS(lParam
)),
2084 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2085 wParam
& MK_CONTROL
, is_alt_pressed());
2088 case WM_NCMOUSEMOVE
:
2090 noise_ultralight(lParam
);
2092 case WM_IGNORE_CLIP
:
2093 ignore_clip
= wParam
; /* don't panic on DESTROYCLIPBOARD */
2095 case WM_DESTROYCLIPBOARD
:
2097 term_deselect(term
);
2098 ignore_clip
= FALSE
;
2104 hdc
= BeginPaint(hwnd
, &p
);
2106 SelectPalette(hdc
, pal
, TRUE
);
2107 RealizePalette(hdc
);
2109 term_paint(term
, hdc
,
2110 (p
.rcPaint
.left
-offset_width
)/font_width
,
2111 (p
.rcPaint
.top
-offset_height
)/font_height
,
2112 (p
.rcPaint
.right
-offset_width
-1)/font_width
,
2113 (p
.rcPaint
.bottom
-offset_height
-1)/font_height
,
2117 p
.rcPaint
.left
< offset_width
||
2118 p
.rcPaint
.top
< offset_height
||
2119 p
.rcPaint
.right
>= offset_width
+ font_width
*term
->cols
||
2120 p
.rcPaint
.bottom
>= offset_height
+ font_height
*term
->rows
)
2122 HBRUSH fillcolour
, oldbrush
;
2124 fillcolour
= CreateSolidBrush (
2125 colours
[(ATTR_DEFBG
>>ATTR_BGSHIFT
)*2]);
2126 oldbrush
= SelectObject(hdc
, fillcolour
);
2127 edge
= CreatePen(PS_SOLID
, 0,
2128 colours
[(ATTR_DEFBG
>>ATTR_BGSHIFT
)*2]);
2129 oldpen
= SelectObject(hdc
, edge
);
2132 * Jordan Russell reports that this apparently
2133 * ineffectual IntersectClipRect() call masks a
2134 * Windows NT/2K bug causing strange display
2135 * problems when the PuTTY window is taller than
2136 * the primary monitor. It seems harmless enough...
2138 IntersectClipRect(hdc
,
2139 p
.rcPaint
.left
, p
.rcPaint
.top
,
2140 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2142 ExcludeClipRect(hdc
,
2143 offset_width
, offset_height
,
2144 offset_width
+font_width
*term
->cols
,
2145 offset_height
+font_height
*term
->rows
);
2147 Rectangle(hdc
, p
.rcPaint
.left
, p
.rcPaint
.top
,
2148 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2150 // SelectClipRgn(hdc, NULL);
2152 SelectObject(hdc
, oldbrush
);
2153 DeleteObject(fillcolour
);
2154 SelectObject(hdc
, oldpen
);
2157 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
2158 SelectObject(hdc
, GetStockObject(WHITE_PEN
));
2164 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2165 * but the only one that's likely to try to overload us is FD_READ.
2166 * This means buffering just one is fine.
2168 if (pending_netevent
)
2169 enact_pending_netevent();
2171 pending_netevent
= TRUE
;
2172 pend_netevent_wParam
= wParam
;
2173 pend_netevent_lParam
= lParam
;
2174 if (WSAGETSELECTEVENT(lParam
) != FD_READ
)
2175 enact_pending_netevent();
2177 time(&last_movement
);
2180 term
->has_focus
= TRUE
;
2181 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
2183 flash_window(0); /* stop */
2190 term
->has_focus
= FALSE
;
2192 caret_x
= caret_y
= -1; /* ensure caret is replaced next time */
2196 case WM_ENTERSIZEMOVE
:
2197 #ifdef RDB_DEBUG_PATCH
2198 debug((27, "WM_ENTERSIZEMOVE"));
2202 need_backend_resize
= FALSE
;
2204 case WM_EXITSIZEMOVE
:
2207 #ifdef RDB_DEBUG_PATCH
2208 debug((27, "WM_EXITSIZEMOVE"));
2210 if (need_backend_resize
) {
2211 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2212 InvalidateRect(hwnd
, NULL
, TRUE
);
2217 * This does two jobs:
2218 * 1) Keep the sizetip uptodate
2219 * 2) Make sure the window size is _stepped_ in units of the font size.
2221 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2222 int width
, height
, w
, h
, ew
, eh
;
2223 LPRECT r
= (LPRECT
) lParam
;
2225 if ( !need_backend_resize
&& cfg
.resize_action
== RESIZE_EITHER
&&
2226 (cfg
.height
!= term
->rows
|| cfg
.width
!= term
->cols
)) {
2228 * Great! It seems that both the terminal size and the
2229 * font size have been changed and the user is now dragging.
2231 * It will now be difficult to get back to the configured
2234 * This would be easier but it seems to be too confusing.
2236 term_size(term, cfg.height, cfg.width, cfg.savelines);
2239 cfg
.height
=term
->rows
; cfg
.width
=term
->cols
;
2241 InvalidateRect(hwnd
, NULL
, TRUE
);
2242 need_backend_resize
= TRUE
;
2245 width
= r
->right
- r
->left
- extra_width
;
2246 height
= r
->bottom
- r
->top
- extra_height
;
2247 w
= (width
+ font_width
/ 2) / font_width
;
2250 h
= (height
+ font_height
/ 2) / font_height
;
2253 UpdateSizeTip(hwnd
, w
, h
);
2254 ew
= width
- w
* font_width
;
2255 eh
= height
- h
* font_height
;
2257 if (wParam
== WMSZ_LEFT
||
2258 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2264 if (wParam
== WMSZ_TOP
||
2265 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2275 int width
, height
, w
, h
, rv
= 0;
2276 int ex_width
= extra_width
+ (cfg
.window_border
- offset_width
) * 2;
2277 int ex_height
= extra_height
+ (cfg
.window_border
- offset_height
) * 2;
2278 LPRECT r
= (LPRECT
) lParam
;
2280 width
= r
->right
- r
->left
- ex_width
;
2281 height
= r
->bottom
- r
->top
- ex_height
;
2283 w
= (width
+ term
->cols
/2)/term
->cols
;
2284 h
= (height
+ term
->rows
/2)/term
->rows
;
2285 if ( r
->right
!= r
->left
+ w
*term
->cols
+ ex_width
)
2288 if (wParam
== WMSZ_LEFT
||
2289 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2290 r
->left
= r
->right
- w
*term
->cols
- ex_width
;
2292 r
->right
= r
->left
+ w
*term
->cols
+ ex_width
;
2294 if (r
->bottom
!= r
->top
+ h
*term
->rows
+ ex_height
)
2297 if (wParam
== WMSZ_TOP
||
2298 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2299 r
->top
= r
->bottom
- h
*term
->rows
- ex_height
;
2301 r
->bottom
= r
->top
+ h
*term
->rows
+ ex_height
;
2305 /* break; (never reached) */
2306 case WM_FULLSCR_ON_MAX
:
2307 fullscr_on_max
= TRUE
;
2310 sys_cursor_update();
2313 #ifdef RDB_DEBUG_PATCH
2314 debug((27, "WM_SIZE %s (%d,%d)",
2315 (wParam
== SIZE_MINIMIZED
) ?
"SIZE_MINIMIZED":
2316 (wParam
== SIZE_MAXIMIZED
) ?
"SIZE_MAXIMIZED":
2317 (wParam
== SIZE_RESTORED
&& resizing
) ?
"to":
2318 (wParam
== SIZE_RESTORED
) ?
"SIZE_RESTORED":
2320 LOWORD(lParam
), HIWORD(lParam
)));
2322 if (wParam
== SIZE_MINIMIZED
)
2324 cfg
.win_name_always ? window_name
: icon_name
);
2325 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
)
2326 SetWindowText(hwnd
, window_name
);
2327 if (wParam
== SIZE_RESTORED
)
2328 clear_full_screen();
2329 if (wParam
== SIZE_MAXIMIZED
&& fullscr_on_max
) {
2330 fullscr_on_max
= FALSE
;
2334 if (cfg
.resize_action
== RESIZE_DISABLED
) {
2335 /* A resize, well it better be a minimize. */
2339 int width
, height
, w
, h
;
2341 width
= LOWORD(lParam
);
2342 height
= HIWORD(lParam
);
2345 if (wParam
== SIZE_MAXIMIZED
&& !was_zoomed
) {
2347 prev_rows
= term
->rows
;
2348 prev_cols
= term
->cols
;
2349 if (cfg
.resize_action
== RESIZE_TERM
) {
2350 w
= width
/ font_width
;
2352 h
= height
/ font_height
;
2355 term_size(term
, h
, w
, cfg
.savelines
);
2358 } else if (wParam
== SIZE_RESTORED
&& was_zoomed
) {
2360 if (cfg
.resize_action
== RESIZE_TERM
)
2361 term_size(term
, prev_rows
, prev_cols
, cfg
.savelines
);
2362 if (cfg
.resize_action
!= RESIZE_FONT
)
2367 /* This is an unexpected resize, these will normally happen
2368 * if the window is too large. Probably either the user
2369 * selected a huge font or the screen size has changed.
2371 * This is also called with minimize.
2373 else reset_window(-1);
2377 * Don't call back->size in mid-resize. (To prevent
2378 * massive numbers of resize events getting sent
2379 * down the connection during an NT opaque drag.)
2382 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2383 need_backend_resize
= TRUE
;
2384 w
= (width
-cfg
.window_border
*2) / font_width
;
2386 h
= (height
-cfg
.window_border
*2) / font_height
;
2395 sys_cursor_update();
2398 switch (LOWORD(wParam
)) {
2400 term_scroll(term
, -1, 0);
2403 term_scroll(term
, +1, 0);
2406 term_scroll(term
, 0, +1);
2409 term_scroll(term
, 0, -1);
2412 term_scroll(term
, 0, +term
->rows
/ 2);
2415 term_scroll(term
, 0, -term
->rows
/ 2);
2417 case SB_THUMBPOSITION
:
2419 term_scroll(term
, 1, HIWORD(wParam
));
2423 case WM_PALETTECHANGED
:
2424 if ((HWND
) wParam
!= hwnd
&& pal
!= NULL
) {
2425 HDC hdc
= get_ctx(NULL
);
2427 if (RealizePalette(hdc
) > 0)
2433 case WM_QUERYNEWPALETTE
:
2435 HDC hdc
= get_ctx(NULL
);
2437 if (RealizePalette(hdc
) > 0)
2449 * Add the scan code and keypress timing to the random
2452 noise_ultralight(lParam
);
2455 * We don't do TranslateMessage since it disassociates the
2456 * resulting CHAR message from the KEYDOWN that sparked it,
2457 * which we occasionally don't want. Instead, we process
2458 * KEYDOWN, and call the Win32 translator functions so that
2459 * we get the translations under _our_ control.
2462 unsigned char buf
[20];
2465 if (wParam
== VK_PROCESSKEY
) {
2468 m
.message
= WM_KEYDOWN
;
2470 m
.lParam
= lParam
& 0xdfff;
2471 TranslateMessage(&m
);
2473 len
= TranslateKey(message
, wParam
, lParam
, buf
);
2475 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2479 * Interrupt an ongoing paste. I'm not sure
2480 * this is sensible, but for the moment it's
2481 * preferable to having to faff about buffering
2487 * We need not bother about stdin backlogs
2488 * here, because in GUI PuTTY we can't do
2489 * anything about it anyway; there's no means
2490 * of asking Windows to hold off on KEYDOWN
2491 * messages. We _have_ to buffer everything
2494 term_seen_key_event(term
);
2495 ldisc_send(ldisc
, buf
, len
, 1);
2500 net_pending_errors();
2502 case WM_INPUTLANGCHANGE
:
2503 /* wParam == Font number */
2504 /* lParam == Locale */
2505 set_input_locale((HKL
)lParam
);
2506 sys_cursor_update();
2509 if(wParam
== IMN_SETOPENSTATUS
) {
2510 HIMC hImc
= ImmGetContext(hwnd
);
2511 ImmSetCompositionFont(hImc
, &lfont
);
2512 ImmReleaseContext(hwnd
, hImc
);
2516 case WM_IME_COMPOSITION
:
2522 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
||
2523 osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) break; /* no Unicode */
2525 if ((lParam
& GCS_RESULTSTR
) == 0) /* Composition unfinished. */
2526 break; /* fall back to DefWindowProc */
2528 hIMC
= ImmGetContext(hwnd
);
2529 n
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
2533 buff
= snewn(n
, char);
2534 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, buff
, n
);
2536 * Jaeyoun Chung reports that Korean character
2537 * input doesn't work correctly if we do a single
2538 * luni_send() covering the whole of buff. So
2539 * instead we luni_send the characters one by one.
2541 term_seen_key_event(term
);
2542 for (i
= 0; i
< n
; i
+= 2) {
2543 luni_send(ldisc
, (unsigned short *)(buff
+i
), 1, 1);
2547 ImmReleaseContext(hwnd
, hIMC
);
2552 if (wParam
& 0xFF00) {
2553 unsigned char buf
[2];
2556 buf
[0] = wParam
>> 8;
2557 term_seen_key_event(term
);
2558 lpage_send(ldisc
, kbd_codepage
, buf
, 2, 1);
2560 char c
= (unsigned char) wParam
;
2561 term_seen_key_event(term
);
2562 lpage_send(ldisc
, kbd_codepage
, &c
, 1, 1);
2568 * Nevertheless, we are prepared to deal with WM_CHAR
2569 * messages, should they crop up. So if someone wants to
2570 * post the things to us as part of a macro manoeuvre,
2571 * we're ready to cope.
2574 char c
= (unsigned char)wParam
;
2575 term_seen_key_event(term
);
2576 lpage_send(ldisc
, CP_ACP
, &c
, 1, 1);
2580 if (send_raw_mouse
&& LOWORD(lParam
) == HTCLIENT
) {
2581 SetCursor(LoadCursor(NULL
, IDC_ARROW
));
2585 case WM_AGENT_CALLBACK
:
2587 struct agent_callback
*c
= (struct agent_callback
*)lParam
;
2588 c
->callback(c
->callback_ctx
, c
->data
, c
->len
);
2593 if (message
== wm_mousewheel
|| message
== WM_MOUSEWHEEL
) {
2594 int shift_pressed
=0, control_pressed
=0;
2596 if (message
== WM_MOUSEWHEEL
) {
2597 wheel_accumulator
+= (short)HIWORD(wParam
);
2598 shift_pressed
=LOWORD(wParam
) & MK_SHIFT
;
2599 control_pressed
=LOWORD(wParam
) & MK_CONTROL
;
2602 wheel_accumulator
+= (int)wParam
;
2603 if (GetKeyboardState(keys
)!=0) {
2604 shift_pressed
=keys
[VK_SHIFT
]&0x80;
2605 control_pressed
=keys
[VK_CONTROL
]&0x80;
2609 /* process events when the threshold is reached */
2610 while (abs(wheel_accumulator
) >= WHEEL_DELTA
) {
2613 /* reduce amount for next time */
2614 if (wheel_accumulator
> 0) {
2616 wheel_accumulator
-= WHEEL_DELTA
;
2617 } else if (wheel_accumulator
< 0) {
2619 wheel_accumulator
+= WHEEL_DELTA
;
2623 if (send_raw_mouse
&&
2624 !(cfg
.mouse_override
&& shift_pressed
)) {
2625 /* send a mouse-down followed by a mouse up */
2626 term_mouse(term
, b
, translate_button(b
),
2628 TO_CHR_X(X_POS(lParam
)),
2629 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2630 control_pressed
, is_alt_pressed());
2631 term_mouse(term
, b
, translate_button(b
),
2632 MA_RELEASE
, TO_CHR_X(X_POS(lParam
)),
2633 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2634 control_pressed
, is_alt_pressed());
2636 /* trigger a scroll */
2637 term_scroll(term
, 0,
2639 -term
->rows
/ 2 : term
->rows
/ 2);
2646 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2650 * Move the system caret. (We maintain one, even though it's
2651 * invisible, for the benefit of blind people: apparently some
2652 * helper software tracks the system caret, so we should arrange to
2655 void sys_cursor(void *frontend
, int x
, int y
)
2659 if (!term
->has_focus
) return;
2662 * Avoid gratuitously re-updating the cursor position and IMM
2663 * window if there's no actual change required.
2665 cx
= x
* font_width
+ offset_width
;
2666 cy
= y
* font_height
+ offset_height
;
2667 if (cx
== caret_x
&& cy
== caret_y
)
2672 sys_cursor_update();
2675 static void sys_cursor_update(void)
2680 if (!term
->has_focus
) return;
2682 if (caret_x
< 0 || caret_y
< 0)
2685 SetCaretPos(caret_x
, caret_y
);
2687 /* IMM calls on Win98 and beyond only */
2688 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) return; /* 3.11 */
2690 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
&&
2691 osVersion
.dwMinorVersion
== 0) return; /* 95 */
2693 /* we should have the IMM functions */
2694 hIMC
= ImmGetContext(hwnd
);
2695 cf
.dwStyle
= CFS_POINT
;
2696 cf
.ptCurrentPos
.x
= caret_x
;
2697 cf
.ptCurrentPos
.y
= caret_y
;
2698 ImmSetCompositionWindow(hIMC
, &cf
);
2700 ImmReleaseContext(hwnd
, hIMC
);
2704 * Draw a line of text in the window, at given character
2705 * coordinates, in given attributes.
2707 * We are allowed to fiddle with the contents of `text'.
2709 void do_text(Context ctx
, int x
, int y
, char *text
, int len
,
2710 unsigned long attr
, int lattr
)
2713 int nfg
, nbg
, nfont
;
2716 int force_manual_underline
= 0;
2717 int fnt_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
2718 int char_width
= fnt_width
;
2719 int text_adjust
= 0;
2720 static int *IpDx
= 0, IpDxLEN
= 0;
2722 if (attr
& ATTR_WIDE
)
2725 if (len
> IpDxLEN
|| IpDx
[0] != char_width
) {
2727 if (len
> IpDxLEN
) {
2729 IpDx
= snewn(len
+ 16, int);
2730 IpDxLEN
= (len
+ 16);
2732 for (i
= 0; i
< IpDxLEN
; i
++)
2733 IpDx
[i
] = char_width
;
2736 /* Only want the left half of double width lines */
2737 if (lattr
!= LATTR_NORM
&& x
*2 >= term
->cols
)
2745 if ((attr
& TATTR_ACTCURS
) && (cfg
.cursor_type
== 0 || term
->big_cursor
)) {
2746 attr
&= ATTR_CUR_AND
| (bold_mode
!= BOLD_COLOURS ? ATTR_BOLD
: 0);
2747 attr
^= ATTR_CUR_XOR
;
2751 if (cfg
.vtmode
== VT_POORMAN
&& lattr
!= LATTR_NORM
) {
2752 /* Assume a poorman font is borken in other ways too. */
2762 nfont
|= FONT_WIDE
+ FONT_HIGH
;
2765 if (attr
& ATTR_NARROW
)
2766 nfont
|= FONT_NARROW
;
2768 /* Special hack for the VT100 linedraw glyphs. */
2769 if ((attr
& CSET_MASK
) == 0x2300) {
2770 if (text
[0] >= (char) 0xBA && text
[0] <= (char) 0xBD) {
2771 switch ((unsigned char) (text
[0])) {
2773 text_adjust
= -2 * font_height
/ 5;
2776 text_adjust
= -1 * font_height
/ 5;
2779 text_adjust
= font_height
/ 5;
2782 text_adjust
= 2 * font_height
/ 5;
2785 if (lattr
== LATTR_TOP
|| lattr
== LATTR_BOT
)
2788 text
[0] = (char) (ucsdata
.unitab_xterm
['q'] & CHAR_MASK
);
2789 attr
|= (ucsdata
.unitab_xterm
['q'] & CSET_MASK
);
2790 if (attr
& ATTR_UNDER
) {
2791 attr
&= ~ATTR_UNDER
;
2792 force_manual_underline
= 1;
2797 /* Anything left as an original character set is unprintable. */
2798 if (DIRECT_CHAR(attr
)) {
2801 memset(text
, 0xFD, len
);
2805 if ((attr
& CSET_MASK
) == ATTR_OEMCP
)
2808 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
2809 nfg
= 2 * (nfg
& 0xF) + (nfg
& 0x10 ?
1 : 0);
2810 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
2811 nbg
= 2 * (nbg
& 0xF) + (nbg
& 0x10 ?
1 : 0);
2812 if (bold_mode
== BOLD_FONT
&& (attr
& ATTR_BOLD
))
2814 if (und_mode
== UND_FONT
&& (attr
& ATTR_UNDER
))
2815 nfont
|= FONT_UNDERLINE
;
2816 another_font(nfont
);
2817 if (!fonts
[nfont
]) {
2818 if (nfont
& FONT_UNDERLINE
)
2819 force_manual_underline
= 1;
2820 /* Don't do the same for manual bold, it could be bad news. */
2822 nfont
&= ~(FONT_BOLD
| FONT_UNDERLINE
);
2824 another_font(nfont
);
2826 nfont
= FONT_NORMAL
;
2827 if (attr
& ATTR_REVERSE
) {
2832 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BOLD
))
2834 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BLINK
))
2838 SelectObject(hdc
, fonts
[nfont
]);
2839 SetTextColor(hdc
, fg
);
2840 SetBkColor(hdc
, bg
);
2841 SetBkMode(hdc
, OPAQUE
);
2844 line_box
.right
= x
+ char_width
* len
;
2845 line_box
.bottom
= y
+ font_height
;
2847 /* Only want the left half of double width lines */
2848 if (line_box
.right
> font_width
*term
->cols
+offset_width
)
2849 line_box
.right
= font_width
*term
->cols
+offset_width
;
2851 /* We're using a private area for direct to font. (512 chars.) */
2852 if (ucsdata
.dbcs_screenfont
&& (attr
& CSET_MASK
) == ATTR_ACP
) {
2853 /* Ho Hum, dbcs fonts are a PITA! */
2854 /* To display on W9x I have to convert to UCS */
2855 static wchar_t *uni_buf
= 0;
2856 static int uni_len
= 0;
2858 if (len
> uni_len
) {
2861 uni_buf
= snewn(uni_len
, wchar_t);
2864 for(nlen
= mptr
= 0; mptr
<len
; mptr
++) {
2865 uni_buf
[nlen
] = 0xFFFD;
2866 if (IsDBCSLeadByteEx(ucsdata
.font_codepage
, (BYTE
) text
[mptr
])) {
2867 IpDx
[nlen
] += char_width
;
2868 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
2869 text
+mptr
, 2, uni_buf
+nlen
, 1);
2874 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
2875 text
+mptr
, 1, uni_buf
+nlen
, 1);
2883 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
2884 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, uni_buf
, nlen
, IpDx
);
2885 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
2886 SetBkMode(hdc
, TRANSPARENT
);
2887 ExtTextOutW(hdc
, x
- 1,
2888 y
- font_height
* (lattr
==
2889 LATTR_BOT
) + text_adjust
,
2890 ETO_CLIPPED
, &line_box
, uni_buf
, nlen
, IpDx
);
2894 } else if (DIRECT_FONT(attr
)) {
2896 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
2897 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, text
, len
, IpDx
);
2898 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
2899 SetBkMode(hdc
, TRANSPARENT
);
2901 /* GRR: This draws the character outside it's box and can leave
2902 * 'droppings' even with the clip box! I suppose I could loop it
2903 * one character at a time ... yuk.
2905 * Or ... I could do a test print with "W", and use +1 or -1 for this
2906 * shift depending on if the leftmost column is blank...
2908 ExtTextOut(hdc
, x
- 1,
2909 y
- font_height
* (lattr
==
2910 LATTR_BOT
) + text_adjust
,
2911 ETO_CLIPPED
, &line_box
, text
, len
, IpDx
);
2914 /* And 'normal' unicode characters */
2915 static WCHAR
*wbuf
= NULL
;
2916 static int wlen
= 0;
2921 wbuf
= snewn(wlen
, WCHAR
);
2923 for (i
= 0; i
< len
; i
++)
2924 wbuf
[i
] = (WCHAR
) ((attr
& CSET_MASK
) + (text
[i
] & CHAR_MASK
));
2927 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
2928 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, wbuf
, len
, IpDx
);
2930 /* And the shadow bold hack. */
2931 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
2932 SetBkMode(hdc
, TRANSPARENT
);
2933 ExtTextOutW(hdc
, x
- 1,
2934 y
- font_height
* (lattr
==
2935 LATTR_BOT
) + text_adjust
,
2936 ETO_CLIPPED
, &line_box
, wbuf
, len
, IpDx
);
2939 if (lattr
!= LATTR_TOP
&& (force_manual_underline
||
2940 (und_mode
== UND_LINE
2941 && (attr
& ATTR_UNDER
)))) {
2944 if (lattr
== LATTR_BOT
)
2945 dec
= dec
* 2 - font_height
;
2947 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, fg
));
2948 MoveToEx(hdc
, x
, y
+ dec
, NULL
);
2949 LineTo(hdc
, x
+ len
* char_width
, y
+ dec
);
2950 oldpen
= SelectObject(hdc
, oldpen
);
2951 DeleteObject(oldpen
);
2955 void do_cursor(Context ctx
, int x
, int y
, char *text
, int len
,
2956 unsigned long attr
, int lattr
)
2962 int ctype
= cfg
.cursor_type
;
2964 if ((attr
& TATTR_ACTCURS
) && (ctype
== 0 || term
->big_cursor
)) {
2965 if (((attr
& CSET_MASK
) | (unsigned char) *text
) != UCSWIDE
) {
2966 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
2970 attr
|= TATTR_RIGHTCURS
;
2973 fnt_width
= char_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
2974 if (attr
& ATTR_WIDE
)
2981 if ((attr
& TATTR_PASCURS
) && (ctype
== 0 || term
->big_cursor
)) {
2984 pts
[0].x
= pts
[1].x
= pts
[4].x
= x
;
2985 pts
[2].x
= pts
[3].x
= x
+ char_width
- 1;
2986 pts
[0].y
= pts
[3].y
= pts
[4].y
= y
;
2987 pts
[1].y
= pts
[2].y
= y
+ font_height
- 1;
2988 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[23]));
2989 Polyline(hdc
, pts
, 5);
2990 oldpen
= SelectObject(hdc
, oldpen
);
2991 DeleteObject(oldpen
);
2992 } else if ((attr
& (TATTR_ACTCURS
| TATTR_PASCURS
)) && ctype
!= 0) {
2993 int startx
, starty
, dx
, dy
, length
, i
;
2996 starty
= y
+ descent
;
2999 length
= char_width
;
3002 if (attr
& TATTR_RIGHTCURS
)
3003 xadjust
= char_width
- 1;
3004 startx
= x
+ xadjust
;
3008 length
= font_height
;
3010 if (attr
& TATTR_ACTCURS
) {
3013 SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[23]));
3014 MoveToEx(hdc
, startx
, starty
, NULL
);
3015 LineTo(hdc
, startx
+ dx
* length
, starty
+ dy
* length
);
3016 oldpen
= SelectObject(hdc
, oldpen
);
3017 DeleteObject(oldpen
);
3019 for (i
= 0; i
< length
; i
++) {
3021 SetPixel(hdc
, startx
, starty
, colours
[23]);
3030 /* This function gets the actual width of a character in the normal font.
3032 int char_width(Context ctx
, int uc
) {
3036 /* If the font max is the same as the font ave width then this
3037 * function is a no-op.
3039 if (!font_dualwidth
) return 1;
3041 switch (uc
& CSET_MASK
) {
3043 uc
= ucsdata
.unitab_line
[uc
& 0xFF];
3046 uc
= ucsdata
.unitab_xterm
[uc
& 0xFF];
3049 uc
= ucsdata
.unitab_scoacs
[uc
& 0xFF];
3052 if (DIRECT_FONT(uc
)) {
3053 if (ucsdata
.dbcs_screenfont
) return 1;
3055 /* Speedup, I know of no font where ascii is the wrong width */
3056 if ((uc
&CHAR_MASK
) >= ' ' && (uc
&CHAR_MASK
)<= '~')
3059 if ( (uc
& CSET_MASK
) == ATTR_ACP
) {
3060 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3061 } else if ( (uc
& CSET_MASK
) == ATTR_OEMCP
) {
3062 another_font(FONT_OEM
);
3063 if (!fonts
[FONT_OEM
]) return 0;
3065 SelectObject(hdc
, fonts
[FONT_OEM
]);
3069 if ( GetCharWidth32(hdc
, uc
&CHAR_MASK
, uc
&CHAR_MASK
, &ibuf
) != 1 &&
3070 GetCharWidth(hdc
, uc
&CHAR_MASK
, uc
&CHAR_MASK
, &ibuf
) != 1)
3073 /* Speedup, I know of no font where ascii is the wrong width */
3074 if (uc
>= ' ' && uc
<= '~') return 1;
3076 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3077 if ( GetCharWidth32W(hdc
, uc
, uc
, &ibuf
) == 1 )
3078 /* Okay that one worked */ ;
3079 else if ( GetCharWidthW(hdc
, uc
, uc
, &ibuf
) == 1 )
3080 /* This should work on 9x too, but it's "less accurate" */ ;
3085 ibuf
+= font_width
/ 2 -1;
3092 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3093 * codes. Returns number of bytes used or zero to drop the message
3094 * or -1 to forward the message to windows.
3096 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
3097 unsigned char *output
)
3100 int scan
, left_alt
= 0, key_down
, shift_state
;
3102 unsigned char *p
= output
;
3103 static int alt_sum
= 0;
3105 HKL kbd_layout
= GetKeyboardLayout(0);
3107 static WORD keys
[3];
3108 static int compose_char
= 0;
3109 static WPARAM compose_key
= 0;
3111 r
= GetKeyboardState(keystate
);
3113 memset(keystate
, 0, sizeof(keystate
));
3116 #define SHOW_TOASCII_RESULT
3117 { /* Tell us all about key events */
3118 static BYTE oldstate
[256];
3119 static int first
= 1;
3123 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3126 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
) {
3128 } else if ((HIWORD(lParam
) & KF_UP
)
3129 && scan
== (HIWORD(lParam
) & 0xFF)) {
3133 if (wParam
>= VK_F1
&& wParam
<= VK_F20
)
3134 debug(("K_F%d", wParam
+ 1 - VK_F1
));
3147 debug(("VK_%02x", wParam
));
3149 if (message
== WM_SYSKEYDOWN
|| message
== WM_SYSKEYUP
)
3151 debug((", S%02x", scan
= (HIWORD(lParam
) & 0xFF)));
3153 ch
= MapVirtualKeyEx(wParam
, 2, kbd_layout
);
3154 if (ch
>= ' ' && ch
<= '~')
3155 debug((", '%c'", ch
));
3157 debug((", $%02x", ch
));
3160 debug((", KB0=%02x", keys
[0]));
3162 debug((", KB1=%02x", keys
[1]));
3164 debug((", KB2=%02x", keys
[2]));
3166 if ((keystate
[VK_SHIFT
] & 0x80) != 0)
3168 if ((keystate
[VK_CONTROL
] & 0x80) != 0)
3170 if ((HIWORD(lParam
) & KF_EXTENDED
))
3172 if ((HIWORD(lParam
) & KF_UP
))
3176 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
);
3177 else if ((HIWORD(lParam
) & KF_UP
))
3178 oldstate
[wParam
& 0xFF] ^= 0x80;
3180 oldstate
[wParam
& 0xFF] ^= 0x81;
3182 for (ch
= 0; ch
< 256; ch
++)
3183 if (oldstate
[ch
] != keystate
[ch
])
3184 debug((", M%02x=%02x", ch
, keystate
[ch
]));
3186 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3190 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
)) {
3191 keystate
[VK_RMENU
] = keystate
[VK_MENU
];
3195 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3196 if ((cfg
.funky_type
== 3 ||
3197 (cfg
.funky_type
<= 1 && term
->app_keypad_keys
&&
3199 && wParam
== VK_NUMLOCK
&& !(keystate
[VK_SHIFT
] & 0x80)) {
3201 wParam
= VK_EXECUTE
;
3203 /* UnToggle NUMLock */
3204 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0)
3205 keystate
[VK_NUMLOCK
] ^= 1;
3208 /* And write back the 'adjusted' state */
3209 SetKeyboardState(keystate
);
3212 /* Disable Auto repeat if required */
3213 if (term
->repeat_off
&&
3214 (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
)
3217 if ((HIWORD(lParam
) & KF_ALTDOWN
) && (keystate
[VK_RMENU
] & 0x80) == 0)
3220 key_down
= ((HIWORD(lParam
) & KF_UP
) == 0);
3222 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3223 if (left_alt
&& (keystate
[VK_CONTROL
] & 0x80)) {
3224 if (cfg
.ctrlaltkeys
)
3225 keystate
[VK_MENU
] = 0;
3227 keystate
[VK_RMENU
] = 0x80;
3232 scan
= (HIWORD(lParam
) & (KF_UP
| KF_EXTENDED
| 0xFF));
3233 shift_state
= ((keystate
[VK_SHIFT
] & 0x80) != 0)
3234 + ((keystate
[VK_CONTROL
] & 0x80) != 0) * 2;
3236 /* Note if AltGr was pressed and if it was used as a compose key */
3237 if (!compose_state
) {
3238 compose_key
= 0x100;
3239 if (cfg
.compose_key
) {
3240 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
))
3241 compose_key
= wParam
;
3243 if (wParam
== VK_APPS
)
3244 compose_key
= wParam
;
3247 if (wParam
== compose_key
) {
3248 if (compose_state
== 0
3249 && (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0) compose_state
=
3251 else if (compose_state
== 1 && (HIWORD(lParam
) & KF_UP
))
3255 } else if (compose_state
== 1 && wParam
!= VK_CONTROL
)
3258 if (compose_state
> 1 && left_alt
)
3261 /* Sanitize the number pad if not using a PC NumPad */
3262 if (left_alt
|| (term
->app_keypad_keys
&& !cfg
.no_applic_k
3263 && cfg
.funky_type
!= 2)
3264 || cfg
.funky_type
== 3 || cfg
.nethack_keypad
|| compose_state
) {
3265 if ((HIWORD(lParam
) & KF_EXTENDED
) == 0) {
3269 nParam
= VK_NUMPAD0
;
3272 nParam
= VK_NUMPAD1
;
3275 nParam
= VK_NUMPAD2
;
3278 nParam
= VK_NUMPAD3
;
3281 nParam
= VK_NUMPAD4
;
3284 nParam
= VK_NUMPAD5
;
3287 nParam
= VK_NUMPAD6
;
3290 nParam
= VK_NUMPAD7
;
3293 nParam
= VK_NUMPAD8
;
3296 nParam
= VK_NUMPAD9
;
3299 nParam
= VK_DECIMAL
;
3303 if (keystate
[VK_NUMLOCK
] & 1)
3310 /* If a key is pressed and AltGr is not active */
3311 if (key_down
&& (keystate
[VK_RMENU
] & 0x80) == 0 && !compose_state
) {
3312 /* Okay, prepare for most alts then ... */
3316 /* Lets see if it's a pattern we know all about ... */
3317 if (wParam
== VK_PRIOR
&& shift_state
== 1) {
3318 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEUP
, 0);
3321 if (wParam
== VK_PRIOR
&& shift_state
== 2) {
3322 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEUP
, 0);
3325 if (wParam
== VK_NEXT
&& shift_state
== 1) {
3326 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEDOWN
, 0);
3329 if (wParam
== VK_NEXT
&& shift_state
== 2) {
3330 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEDOWN
, 0);
3333 if (wParam
== VK_INSERT
&& shift_state
== 1) {
3334 term_do_paste(term
);
3337 if (left_alt
&& wParam
== VK_F4
&& cfg
.alt_f4
) {
3340 if (left_alt
&& wParam
== VK_SPACE
&& cfg
.alt_space
) {
3341 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_KEYMENU
, 0);
3344 if (left_alt
&& wParam
== VK_RETURN
&& cfg
.fullscreenonaltenter
&&
3345 (cfg
.resize_action
!= RESIZE_DISABLED
)) {
3346 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) != KF_REPEAT
)
3350 /* Control-Numlock for app-keypad mode switch */
3351 if (wParam
== VK_PAUSE
&& shift_state
== 2) {
3352 term
->app_keypad_keys
^= 1;
3356 /* Nethack keypad */
3357 if (cfg
.nethack_keypad
&& !left_alt
) {
3360 *p
++ = shift_state ?
'B' : 'b';
3363 *p
++ = shift_state ?
'J' : 'j';
3366 *p
++ = shift_state ?
'N' : 'n';
3369 *p
++ = shift_state ?
'H' : 'h';
3372 *p
++ = shift_state ?
'.' : '.';
3375 *p
++ = shift_state ?
'L' : 'l';
3378 *p
++ = shift_state ?
'Y' : 'y';
3381 *p
++ = shift_state ?
'K' : 'k';
3384 *p
++ = shift_state ?
'U' : 'u';
3389 /* Application Keypad */
3393 if (cfg
.funky_type
== 3 ||
3394 (cfg
.funky_type
<= 1 &&
3395 term
->app_keypad_keys
&& !cfg
.no_applic_k
)) switch (wParam
) {
3409 if (term
->app_keypad_keys
&& !cfg
.no_applic_k
)
3446 if (cfg
.funky_type
== 2) {
3451 } else if (shift_state
)
3458 if (cfg
.funky_type
== 2)
3462 if (cfg
.funky_type
== 2)
3466 if (cfg
.funky_type
== 2)
3471 if (HIWORD(lParam
) & KF_EXTENDED
)
3476 if (term
->vt52_mode
) {
3477 if (xkey
>= 'P' && xkey
<= 'S')
3478 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3480 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
3482 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3487 if (wParam
== VK_BACK
&& shift_state
== 0) { /* Backspace */
3488 *p
++ = (cfg
.bksp_is_delete ?
0x7F : 0x08);
3492 if (wParam
== VK_BACK
&& shift_state
== 1) { /* Shift Backspace */
3493 /* We do the opposite of what is configured */
3494 *p
++ = (cfg
.bksp_is_delete ?
0x08 : 0x7F);
3498 if (wParam
== VK_TAB
&& shift_state
== 1) { /* Shift tab */
3504 if (wParam
== VK_SPACE
&& shift_state
== 2) { /* Ctrl-Space */
3508 if (wParam
== VK_SPACE
&& shift_state
== 3) { /* Ctrl-Shift-Space */
3512 if (wParam
== VK_CANCEL
&& shift_state
== 2) { /* Ctrl-Break */
3517 if (wParam
== VK_PAUSE
) { /* Break/Pause */
3522 /* Control-2 to Control-8 are special */
3523 if (shift_state
== 2 && wParam
>= '2' && wParam
<= '8') {
3524 *p
++ = "\000\033\034\035\036\037\177"[wParam
- '2'];
3527 if (shift_state
== 2 && (wParam
== 0xBD || wParam
== 0xBF)) {
3531 if (shift_state
== 2 && wParam
== 0xDF) {
3535 if (shift_state
== 3 && wParam
== 0xDE) {
3536 *p
++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
3539 if (shift_state
== 0 && wParam
== VK_RETURN
&& term
->cr_lf_return
) {
3546 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3547 * for integer decimal nn.)
3549 * We also deal with the weird ones here. Linux VCs replace F1
3550 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3551 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3557 code
= (keystate
[VK_SHIFT
] & 0x80 ?
23 : 11);
3560 code
= (keystate
[VK_SHIFT
] & 0x80 ?
24 : 12);
3563 code
= (keystate
[VK_SHIFT
] & 0x80 ?
25 : 13);
3566 code
= (keystate
[VK_SHIFT
] & 0x80 ?
26 : 14);
3569 code
= (keystate
[VK_SHIFT
] & 0x80 ?
28 : 15);
3572 code
= (keystate
[VK_SHIFT
] & 0x80 ?
29 : 17);
3575 code
= (keystate
[VK_SHIFT
] & 0x80 ?
31 : 18);
3578 code
= (keystate
[VK_SHIFT
] & 0x80 ?
32 : 19);
3581 code
= (keystate
[VK_SHIFT
] & 0x80 ?
33 : 20);
3584 code
= (keystate
[VK_SHIFT
] & 0x80 ?
34 : 21);
3617 if ((shift_state
&2) == 0) switch (wParam
) {
3637 /* Reorder edit keys to physical order */
3638 if (cfg
.funky_type
== 3 && code
<= 6)
3639 code
= "\0\2\1\4\5\3\6"[code
];
3641 if (term
->vt52_mode
&& code
> 0 && code
<= 6) {
3642 p
+= sprintf((char *) p
, "\x1B%c", " HLMEIG"[code
]);
3646 if (cfg
.funky_type
== 5 && /* SCO function keys */
3647 code
>= 11 && code
<= 34) {
3648 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
3651 case VK_F1
: index
= 0; break;
3652 case VK_F2
: index
= 1; break;
3653 case VK_F3
: index
= 2; break;
3654 case VK_F4
: index
= 3; break;
3655 case VK_F5
: index
= 4; break;
3656 case VK_F6
: index
= 5; break;
3657 case VK_F7
: index
= 6; break;
3658 case VK_F8
: index
= 7; break;
3659 case VK_F9
: index
= 8; break;
3660 case VK_F10
: index
= 9; break;
3661 case VK_F11
: index
= 10; break;
3662 case VK_F12
: index
= 11; break;
3664 if (keystate
[VK_SHIFT
] & 0x80) index
+= 12;
3665 if (keystate
[VK_CONTROL
] & 0x80) index
+= 24;
3666 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
3669 if (cfg
.funky_type
== 5 && /* SCO small keypad */
3670 code
>= 1 && code
<= 6) {
3671 char codes
[] = "HL.FIG";
3675 p
+= sprintf((char *) p
, "\x1B[%c", codes
[code
-1]);
3679 if ((term
->vt52_mode
|| cfg
.funky_type
== 4) && code
>= 11 && code
<= 24) {
3685 if (term
->vt52_mode
)
3686 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11 - offt
);
3689 sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11 - offt
);
3692 if (cfg
.funky_type
== 1 && code
>= 11 && code
<= 15) {
3693 p
+= sprintf((char *) p
, "\x1B[[%c", code
+ 'A' - 11);
3696 if (cfg
.funky_type
== 2 && code
>= 11 && code
<= 14) {
3697 if (term
->vt52_mode
)
3698 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11);
3700 p
+= sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11);
3703 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
3704 p
+= sprintf((char *) p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
3708 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
3713 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
3714 * some reason seems to send VK_CLEAR to Windows...).
3736 if (term
->vt52_mode
)
3737 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3739 int app_flg
= (term
->app_cursor_keys
&& !cfg
.no_applic_c
);
3742 * RDB: VT100 & VT102 manuals both state the
3743 * app cursor keys only work if the app keypad
3746 * SGT: That may well be true, but xterm
3747 * disagrees and so does at least one
3748 * application, so I've #if'ed this out and the
3749 * behaviour is back to PuTTY's original: app
3750 * cursor and app keypad are independently
3751 * switchable modes. If anyone complains about
3752 * _this_ I'll have to put in a configurable
3755 if (!term
->app_keypad_keys
)
3758 /* Useful mapping of Ctrl-arrows */
3759 if (shift_state
== 2)
3763 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3765 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
3772 * Finally, deal with Return ourselves. (Win95 seems to
3773 * foul it up when Alt is pressed, for some reason.)
3775 if (wParam
== VK_RETURN
) { /* Return */
3781 if (left_alt
&& wParam
>= VK_NUMPAD0
&& wParam
<= VK_NUMPAD9
)
3782 alt_sum
= alt_sum
* 10 + wParam
- VK_NUMPAD0
;
3787 /* Okay we've done everything interesting; let windows deal with
3788 * the boring stuff */
3792 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
3793 if(cfg
.xlat_capslockcyr
&& keystate
[VK_CAPITAL
] != 0) {
3795 keystate
[VK_CAPITAL
] = 0;
3798 r
= ToAsciiEx(wParam
, scan
, keystate
, keys
, 0, kbd_layout
);
3799 #ifdef SHOW_TOASCII_RESULT
3800 if (r
== 1 && !key_down
) {
3802 if (in_utf(term
) || ucsdata
.dbcs_screenfont
)
3803 debug((", (U+%04x)", alt_sum
));
3805 debug((", LCH(%d)", alt_sum
));
3807 debug((", ACH(%d)", keys
[0]));
3812 for (r1
= 0; r1
< r
; r1
++) {
3813 debug(("%s%d", r1 ?
"," : "", keys
[r1
]));
3822 * Interrupt an ongoing paste. I'm not sure this is
3823 * sensible, but for the moment it's preferable to
3824 * having to faff about buffering things.
3829 for (i
= 0; i
< r
; i
++) {
3830 unsigned char ch
= (unsigned char) keys
[i
];
3832 if (compose_state
== 2 && (ch
& 0x80) == 0 && ch
> ' ') {
3837 if (compose_state
== 3 && (ch
& 0x80) == 0 && ch
> ' ') {
3841 if ((nc
= check_compose(compose_char
, ch
)) == -1) {
3842 MessageBeep(MB_ICONHAND
);
3846 term_seen_key_event(term
);
3847 luni_send(ldisc
, &keybuf
, 1, 1);
3855 if (in_utf(term
) || ucsdata
.dbcs_screenfont
) {
3857 term_seen_key_event(term
);
3858 luni_send(ldisc
, &keybuf
, 1, 1);
3860 ch
= (char) alt_sum
;
3862 * We need not bother about stdin
3863 * backlogs here, because in GUI PuTTY
3864 * we can't do anything about it
3865 * anyway; there's no means of asking
3866 * Windows to hold off on KEYDOWN
3867 * messages. We _have_ to buffer
3868 * everything we're sent.
3870 term_seen_key_event(term
);
3871 ldisc_send(ldisc
, &ch
, 1, 1);
3875 term_seen_key_event(term
);
3876 lpage_send(ldisc
, kbd_codepage
, &ch
, 1, 1);
3879 if(capsOn
&& ch
< 0x80) {
3882 cbuf
[1] = xlat_uskbd2cyrllic(ch
);
3883 term_seen_key_event(term
);
3884 luni_send(ldisc
, cbuf
+!left_alt
, 1+!!left_alt
, 1);
3889 term_seen_key_event(term
);
3890 lpage_send(ldisc
, kbd_codepage
,
3891 cbuf
+!left_alt
, 1+!!left_alt
, 1);
3897 /* This is so the ALT-Numpad and dead keys work correctly. */
3902 /* If we're definitly not building up an ALT-54321 then clear it */
3905 /* If we will be using alt_sum fix the 256s */
3906 else if (keys
[0] && (in_utf(term
) || ucsdata
.dbcs_screenfont
))
3911 * ALT alone may or may not want to bring up the System menu.
3912 * If it's not meant to, we return 0 on presses or releases of
3913 * ALT, to show that we've swallowed the keystroke. Otherwise
3914 * we return -1, which means Windows will give the keystroke
3915 * its default handling (i.e. bring up the System menu).
3917 if (wParam
== VK_MENU
&& !cfg
.alt_only
)
3923 void request_paste(void *frontend
)
3926 * In Windows, pasting is synchronous: we can read the
3927 * clipboard with no difficulty, so request_paste() can just go
3930 term_do_paste(term
);
3933 void set_title(void *frontend
, char *title
)
3936 window_name
= snewn(1 + strlen(title
), char);
3937 strcpy(window_name
, title
);
3938 if (cfg
.win_name_always
|| !IsIconic(hwnd
))
3939 SetWindowText(hwnd
, title
);
3942 void set_icon(void *frontend
, char *title
)
3945 icon_name
= snewn(1 + strlen(title
), char);
3946 strcpy(icon_name
, title
);
3947 if (!cfg
.win_name_always
&& IsIconic(hwnd
))
3948 SetWindowText(hwnd
, title
);
3951 void set_sbar(void *frontend
, int total
, int start
, int page
)
3955 if (is_full_screen() ?
!cfg
.scrollbar_in_fullscreen
: !cfg
.scrollbar
)
3958 si
.cbSize
= sizeof(si
);
3959 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
3961 si
.nMax
= total
- 1;
3965 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
3968 Context
get_ctx(void *frontend
)
3974 SelectPalette(hdc
, pal
, FALSE
);
3980 void free_ctx(Context ctx
)
3982 SelectPalette(ctx
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
3983 ReleaseDC(hwnd
, ctx
);
3986 static void real_palette_set(int n
, int r
, int g
, int b
)
3989 logpal
->palPalEntry
[n
].peRed
= r
;
3990 logpal
->palPalEntry
[n
].peGreen
= g
;
3991 logpal
->palPalEntry
[n
].peBlue
= b
;
3992 logpal
->palPalEntry
[n
].peFlags
= PC_NOCOLLAPSE
;
3993 colours
[n
] = PALETTERGB(r
, g
, b
);
3994 SetPaletteEntries(pal
, 0, NCOLOURS
, logpal
->palPalEntry
);
3996 colours
[n
] = RGB(r
, g
, b
);
3999 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
4001 static const int first
[21] = {
4002 0, 2, 4, 6, 8, 10, 12, 14,
4003 1, 3, 5, 7, 9, 11, 13, 15,
4006 real_palette_set(first
[n
], r
, g
, b
);
4008 real_palette_set(first
[n
] + 1, r
, g
, b
);
4010 HDC hdc
= get_ctx(frontend
);
4011 UnrealizeObject(pal
);
4012 RealizePalette(hdc
);
4017 void palette_reset(void *frontend
)
4021 for (i
= 0; i
< NCOLOURS
; i
++) {
4023 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
4024 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
4025 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
4026 logpal
->palPalEntry
[i
].peFlags
= 0;
4027 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
4028 defpal
[i
].rgbtGreen
,
4029 defpal
[i
].rgbtBlue
);
4031 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
4032 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4037 SetPaletteEntries(pal
, 0, NCOLOURS
, logpal
->palPalEntry
);
4038 hdc
= get_ctx(frontend
);
4039 RealizePalette(hdc
);
4044 void write_aclip(void *frontend
, char *data
, int len
, int must_deselect
)
4049 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
4052 lock
= GlobalLock(clipdata
);
4055 memcpy(lock
, data
, len
);
4056 ((unsigned char *) lock
)[len
] = 0;
4057 GlobalUnlock(clipdata
);
4060 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4062 if (OpenClipboard(hwnd
)) {
4064 SetClipboardData(CF_TEXT
, clipdata
);
4067 GlobalFree(clipdata
);
4070 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4074 * Note: unlike write_aclip() this will not append a nul.
4076 void write_clip(void *frontend
, wchar_t * data
, int len
, int must_deselect
)
4078 HGLOBAL clipdata
, clipdata2
, clipdata3
;
4080 void *lock
, *lock2
, *lock3
;
4082 len2
= WideCharToMultiByte(CP_ACP
, 0, data
, len
, 0, 0, NULL
, NULL
);
4084 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
,
4085 len
* sizeof(wchar_t));
4086 clipdata2
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len2
);
4088 if (!clipdata
|| !clipdata2
) {
4090 GlobalFree(clipdata
);
4092 GlobalFree(clipdata2
);
4095 if (!(lock
= GlobalLock(clipdata
)))
4097 if (!(lock2
= GlobalLock(clipdata2
)))
4100 memcpy(lock
, data
, len
* sizeof(wchar_t));
4101 WideCharToMultiByte(CP_ACP
, 0, data
, len
, lock2
, len2
, NULL
, NULL
);
4103 if (cfg
.rtf_paste
) {
4104 wchar_t unitab
[256];
4106 unsigned char *tdata
= (unsigned char *)lock2
;
4107 wchar_t *udata
= (wchar_t *)lock
;
4108 int rtflen
= 0, uindex
= 0, tindex
= 0;
4110 int multilen
, blen
, alen
, totallen
, i
;
4111 char before
[16], after
[4];
4113 get_unitab(CP_ACP
, unitab
, 0);
4115 rtfsize
= 100 + strlen(cfg
.font
.name
);
4116 rtf
= snewn(rtfsize
, char);
4117 sprintf(rtf
, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
4118 GetACP(), cfg
.font
.name
);
4119 rtflen
= strlen(rtf
);
4122 * We want to construct a piece of RTF that specifies the
4123 * same Unicode text. To do this we will read back in
4124 * parallel from the Unicode data in `udata' and the
4125 * non-Unicode data in `tdata'. For each character in
4126 * `tdata' which becomes the right thing in `udata' when
4127 * looked up in `unitab', we just copy straight over from
4128 * tdata. For each one that doesn't, we must WCToMB it
4129 * individually and produce a \u escape sequence.
4131 * It would probably be more robust to just bite the bullet
4132 * and WCToMB each individual Unicode character one by one,
4133 * then MBToWC each one back to see if it was an accurate
4134 * translation; but that strikes me as a horrifying number
4135 * of Windows API calls so I want to see if this faster way
4136 * will work. If it screws up badly we can always revert to
4137 * the simple and slow way.
4139 while (tindex
< len2
&& uindex
< len
&&
4140 tdata
[tindex
] && udata
[uindex
]) {
4141 if (tindex
+ 1 < len2
&&
4142 tdata
[tindex
] == '\r' &&
4143 tdata
[tindex
+1] == '\n') {
4147 if (unitab
[tdata
[tindex
]] == udata
[uindex
]) {
4153 multilen
= WideCharToMultiByte(CP_ACP
, 0, unitab
+uindex
, 1,
4154 NULL
, 0, NULL
, NULL
);
4155 if (multilen
!= 1) {
4156 blen
= sprintf(before
, "{\\uc%d\\u%d", multilen
,
4158 alen
= 1; strcpy(after
, "}");
4160 blen
= sprintf(before
, "\\u%d", udata
[uindex
]);
4161 alen
= 0; after
[0] = '\0';
4164 assert(tindex
+ multilen
<= len2
);
4165 totallen
= blen
+ alen
;
4166 for (i
= 0; i
< multilen
; i
++) {
4167 if (tdata
[tindex
+i
] == '\\' ||
4168 tdata
[tindex
+i
] == '{' ||
4169 tdata
[tindex
+i
] == '}')
4171 else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A)
4172 totallen
+= 6; /* \par\r\n */
4173 else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20)
4179 if (rtfsize
< rtflen
+ totallen
+ 3) {
4180 rtfsize
= rtflen
+ totallen
+ 512;
4181 rtf
= sresize(rtf
, rtfsize
, char);
4184 strcpy(rtf
+ rtflen
, before
); rtflen
+= blen
;
4185 for (i
= 0; i
< multilen
; i
++) {
4186 if (tdata
[tindex
+i
] == '\\' ||
4187 tdata
[tindex
+i
] == '{' ||
4188 tdata
[tindex
+i
] == '}') {
4189 rtf
[rtflen
++] = '\\';
4190 rtf
[rtflen
++] = tdata
[tindex
+i
];
4191 } else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A) {
4192 rtflen
+= sprintf(rtf
+rtflen
, "\\par\r\n");
4193 } else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20) {
4194 rtflen
+= sprintf(rtf
+rtflen
, "\\'%02x", tdata
[tindex
+i
]);
4196 rtf
[rtflen
++] = tdata
[tindex
+i
];
4199 strcpy(rtf
+ rtflen
, after
); rtflen
+= alen
;
4205 strcpy(rtf
+ rtflen
, "}");
4208 clipdata3
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, rtflen
);
4209 if (clipdata3
&& (lock3
= GlobalLock(clipdata3
)) != NULL
) {
4211 GlobalUnlock(clipdata3
);
4217 GlobalUnlock(clipdata
);
4218 GlobalUnlock(clipdata2
);
4221 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4223 if (OpenClipboard(hwnd
)) {
4225 SetClipboardData(CF_UNICODETEXT
, clipdata
);
4226 SetClipboardData(CF_TEXT
, clipdata2
);
4228 SetClipboardData(RegisterClipboardFormat(CF_RTF
), clipdata3
);
4231 GlobalFree(clipdata
);
4232 GlobalFree(clipdata2
);
4236 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4239 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
4241 static HGLOBAL clipdata
= NULL
;
4242 static wchar_t *converted
= 0;
4251 GlobalUnlock(clipdata
);
4254 } else if (OpenClipboard(NULL
)) {
4255 if ((clipdata
= GetClipboardData(CF_UNICODETEXT
))) {
4257 *p
= GlobalLock(clipdata
);
4259 for (p2
= *p
; *p2
; p2
++);
4263 } else if ( (clipdata
= GetClipboardData(CF_TEXT
)) ) {
4267 s
= GlobalLock(clipdata
);
4268 i
= MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, 0, 0);
4269 *p
= converted
= snewn(i
, wchar_t);
4270 MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, converted
, i
);
4283 * Move `lines' lines from position `from' to position `to' in the
4286 void optimised_move(void *frontend
, int to
, int from
, int lines
)
4291 min
= (to
< from ? to
: from
);
4292 max
= to
+ from
- min
;
4294 r
.left
= offset_width
;
4295 r
.right
= offset_width
+ term
->cols
* font_width
;
4296 r
.top
= offset_height
+ min
* font_height
;
4297 r
.bottom
= offset_height
+ (max
+ lines
) * font_height
;
4298 ScrollWindow(hwnd
, 0, (to
- from
) * font_height
, &r
, &r
);
4303 * Print a message box and perform a fatal exit.
4305 void fatalbox(char *fmt
, ...)
4308 char *stuff
, morestuff
[100];
4311 stuff
= dupvprintf(fmt
, ap
);
4313 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4314 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
4320 * Print a modal (Really Bad) message box and perform a fatal exit.
4322 void modalfatalbox(char *fmt
, ...)
4325 char *stuff
, morestuff
[100];
4328 stuff
= dupvprintf(fmt
, ap
);
4330 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4331 MessageBox(hwnd
, stuff
, morestuff
,
4332 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
4338 * Manage window caption / taskbar flashing, if enabled.
4339 * 0 = stop, 1 = maintain, 2 = start
4341 static void flash_window(int mode
)
4343 static long last_flash
= 0;
4344 static int flashing
= 0;
4345 if ((mode
== 0) || (cfg
.beep_ind
== B_IND_DISABLED
)) {
4348 FlashWindow(hwnd
, FALSE
);
4352 } else if (mode
== 2) {
4355 last_flash
= GetTickCount();
4357 FlashWindow(hwnd
, TRUE
);
4360 } else if ((mode
== 1) && (cfg
.beep_ind
== B_IND_FLASH
)) {
4363 long now
= GetTickCount();
4364 long fdiff
= now
- last_flash
;
4365 if (fdiff
< 0 || fdiff
> 450) {
4367 FlashWindow(hwnd
, TRUE
); /* toggle */
4376 void beep(void *frontend
, int mode
)
4378 if (mode
== BELL_DEFAULT
) {
4380 * For MessageBeep style bells, we want to be careful of
4381 * timing, because they don't have the nice property of
4382 * PlaySound bells that each one cancels the previous
4383 * active one. So we limit the rate to one per 50ms or so.
4385 static long lastbeep
= 0;
4388 beepdiff
= GetTickCount() - lastbeep
;
4389 if (beepdiff
>= 0 && beepdiff
< 50)
4393 * The above MessageBeep call takes time, so we record the
4394 * time _after_ it finishes rather than before it starts.
4396 lastbeep
= GetTickCount();
4397 } else if (mode
== BELL_WAVEFILE
) {
4398 if (!PlaySound(cfg
.bell_wavefile
.path
, NULL
,
4399 SND_ASYNC
| SND_FILENAME
)) {
4400 char buf
[sizeof(cfg
.bell_wavefile
) + 80];
4402 sprintf(buf
, "Unable to play sound file\n%s\n"
4403 "Using default sound instead", cfg
.bell_wavefile
);
4404 sprintf(otherbuf
, "%.70s Sound Error", appname
);
4405 MessageBox(hwnd
, buf
, otherbuf
,
4406 MB_OK
| MB_ICONEXCLAMATION
);
4407 cfg
.beep
= BELL_DEFAULT
;
4409 } else if (mode
== BELL_PCSPEAKER
) {
4410 static long lastbeep
= 0;
4413 beepdiff
= GetTickCount() - lastbeep
;
4414 if (beepdiff
>= 0 && beepdiff
< 50)
4418 * We must beep in different ways depending on whether this
4419 * is a 95-series or NT-series OS.
4421 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
4425 lastbeep
= GetTickCount();
4427 /* Otherwise, either visual bell or disabled; do nothing here */
4428 if (!term
->has_focus
) {
4429 flash_window(2); /* start */
4434 * Minimise or restore the window in response to a server-side
4437 void set_iconic(void *frontend
, int iconic
)
4439 if (IsIconic(hwnd
)) {
4441 ShowWindow(hwnd
, SW_RESTORE
);
4444 ShowWindow(hwnd
, SW_MINIMIZE
);
4449 * Move the window in response to a server-side request.
4451 void move_window(void *frontend
, int x
, int y
)
4453 if (cfg
.resize_action
== RESIZE_DISABLED
||
4454 cfg
.resize_action
== RESIZE_FONT
||
4458 SetWindowPos(hwnd
, NULL
, x
, y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
);
4462 * Move the window to the top or bottom of the z-order in response
4463 * to a server-side request.
4465 void set_zorder(void *frontend
, int top
)
4467 if (cfg
.alwaysontop
)
4468 return; /* ignore */
4469 SetWindowPos(hwnd
, top ? HWND_TOP
: HWND_BOTTOM
, 0, 0, 0, 0,
4470 SWP_NOMOVE
| SWP_NOSIZE
);
4474 * Refresh the window in response to a server-side request.
4476 void refresh_window(void *frontend
)
4478 InvalidateRect(hwnd
, NULL
, TRUE
);
4482 * Maximise or restore the window in response to a server-side
4485 void set_zoomed(void *frontend
, int zoomed
)
4487 if (IsZoomed(hwnd
)) {
4489 ShowWindow(hwnd
, SW_RESTORE
);
4492 ShowWindow(hwnd
, SW_MAXIMIZE
);
4497 * Report whether the window is iconic, for terminal reports.
4499 int is_iconic(void *frontend
)
4501 return IsIconic(hwnd
);
4505 * Report the window's position, for terminal reports.
4507 void get_window_pos(void *frontend
, int *x
, int *y
)
4510 GetWindowRect(hwnd
, &r
);
4516 * Report the window's pixel size, for terminal reports.
4518 void get_window_pixels(void *frontend
, int *x
, int *y
)
4521 GetWindowRect(hwnd
, &r
);
4522 *x
= r
.right
- r
.left
;
4523 *y
= r
.bottom
- r
.top
;
4527 * Return the window or icon title.
4529 char *get_window_title(void *frontend
, int icon
)
4531 return icon ? icon_name
: window_name
;
4535 * See if we're in full-screen mode.
4537 int is_full_screen()
4539 if (!IsZoomed(hwnd
))
4541 if (GetWindowLong(hwnd
, GWL_STYLE
) & WS_CAPTION
)
4546 /* Get the rect/size of a full screen window using the nearest available
4547 * monitor in multimon systems; default to something sensible if only
4548 * one monitor is present. */
4549 static int get_fullscreen_rect(RECT
* ss
)
4551 #ifdef MONITOR_DEFAULTTONEAREST
4554 mon
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
4555 mi
.cbSize
= sizeof(mi
);
4556 GetMonitorInfo(mon
, &mi
);
4558 /* structure copy */
4562 /* could also use code like this:
4563 ss->left = ss->top = 0;
4564 ss->right = GetSystemMetrics(SM_CXSCREEN);
4565 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
4567 return GetClientRect(GetDesktopWindow(), ss
);
4573 * Go full-screen. This should only be called when we are already
4576 void make_full_screen()
4581 assert(IsZoomed(hwnd
));
4583 if (is_full_screen())
4586 /* Remove the window furniture. */
4587 style
= GetWindowLong(hwnd
, GWL_STYLE
);
4588 style
&= ~(WS_CAPTION
| WS_BORDER
| WS_THICKFRAME
);
4589 if (cfg
.scrollbar_in_fullscreen
)
4590 style
|= WS_VSCROLL
;
4592 style
&= ~WS_VSCROLL
;
4593 SetWindowLong(hwnd
, GWL_STYLE
, style
);
4595 /* Resize ourselves to exactly cover the nearest monitor. */
4596 get_fullscreen_rect(&ss
);
4597 SetWindowPos(hwnd
, HWND_TOP
, ss
.left
, ss
.top
,
4602 /* Tick the menu item in the System menu. */
4603 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
4608 * Clear the full-screen attributes.
4610 void clear_full_screen()
4612 DWORD oldstyle
, style
;
4614 /* Reinstate the window furniture. */
4615 style
= oldstyle
= GetWindowLong(hwnd
, GWL_STYLE
);
4616 style
|= WS_CAPTION
| WS_BORDER
;
4617 if (cfg
.resize_action
== RESIZE_DISABLED
)
4618 style
&= ~WS_THICKFRAME
;
4620 style
|= WS_THICKFRAME
;
4622 style
|= WS_VSCROLL
;
4624 style
&= ~WS_VSCROLL
;
4625 if (style
!= oldstyle
) {
4626 SetWindowLong(hwnd
, GWL_STYLE
, style
);
4627 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
4628 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
4632 /* Untick the menu item in the System menu. */
4633 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
4638 * Toggle full-screen mode.
4640 void flip_full_screen()
4642 if (is_full_screen()) {
4643 ShowWindow(hwnd
, SW_RESTORE
);
4644 } else if (IsZoomed(hwnd
)) {
4647 SendMessage(hwnd
, WM_FULLSCR_ON_MAX
, 0, 0);
4648 ShowWindow(hwnd
, SW_MAXIMIZE
);
4652 void frontend_keypress(void *handle
)
4655 * Keypress termination in non-Close-On-Exit mode is not
4656 * currently supported in PuTTY proper, because the window
4657 * always has a perfectly good Close button anyway. So we do
4663 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
4665 return term_data(term
, is_stderr
, data
, len
);
4668 void agent_schedule_callback(void (*callback
)(void *, void *, int),
4669 void *callback_ctx
, void *data
, int len
)
4671 struct agent_callback
*c
= snew(struct agent_callback
);
4672 c
->callback
= callback
;
4673 c
->callback_ctx
= callback_ctx
;
4676 PostMessage(hwnd
, WM_AGENT_CALLBACK
, 0, (LPARAM
)c
);