2 * window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
3 * emulator and backend in a window.
12 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
20 #define COMPILE_MULTIMON_STUBS
30 /* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
31 * wParam are used by Windows, and should be masked off, so we shouldn't
32 * attempt to store information in them. Hence all these identifiers have
33 * the low 4 bits clear. Also, identifiers should < 0xF000. */
35 #define IDM_SHOWLOG 0x0010
36 #define IDM_NEWSESS 0x0020
37 #define IDM_DUPSESS 0x0030
38 #define IDM_RESTART 0x0040
39 #define IDM_RECONF 0x0050
40 #define IDM_CLRSB 0x0060
41 #define IDM_RESET 0x0070
42 #define IDM_HELP 0x0140
43 #define IDM_ABOUT 0x0150
44 #define IDM_SAVEDSESS 0x0160
45 #define IDM_COPYALL 0x0170
46 #define IDM_FULLSCREEN 0x0180
47 #define IDM_PASTE 0x0190
48 #define IDM_SPECIALSEP 0x0200
50 #define IDM_SPECIAL_MIN 0x0400
51 #define IDM_SPECIAL_MAX 0x0800
53 #define IDM_SAVED_MIN 0x1000
54 #define IDM_SAVED_MAX 0x5000
55 #define MENU_SAVED_STEP 16
56 /* Maximum number of sessions on saved-session submenu */
57 #define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
59 #define WM_IGNORE_CLIP (WM_APP + 2)
60 #define WM_FULLSCR_ON_MAX (WM_APP + 3)
61 #define WM_AGENT_CALLBACK (WM_APP + 4)
63 /* Needed for Chinese support and apparently not always defined. */
65 #define VK_PROCESSKEY 0xE5
68 /* Mouse wheel support. */
70 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
73 #define WHEEL_DELTA 120
76 static Mouse_Button
translate_button(Mouse_Button button
);
77 static LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
78 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
79 unsigned char *output
);
80 static void cfgtopalette(void);
81 static void systopalette(void);
82 static void init_palette(void);
83 static void init_fonts(int, int);
84 static void another_font(int);
85 static void deinit_fonts(void);
86 static void set_input_locale(HKL
);
87 static void update_savedsess_menu(void);
89 static int is_full_screen(void);
90 static void make_full_screen(void);
91 static void clear_full_screen(void);
92 static void flip_full_screen(void);
94 /* Window layout information */
95 static void reset_window(int);
96 static int extra_width
, extra_height
;
97 static int font_width
, font_height
, font_dualwidth
;
98 static int offset_width
, offset_height
;
99 static int was_zoomed
= 0;
100 static int prev_rows
, prev_cols
;
102 static void enact_netevent(WPARAM
, LPARAM
);
103 static void flash_window(int mode
);
104 static void sys_cursor_update(void);
105 static int is_shift_pressed(void);
106 static int get_fullscreen_rect(RECT
* ss
);
108 static int caret_x
= -1, caret_y
= -1;
110 static int kbd_codepage
;
113 static Backend
*back
;
114 static void *backhandle
;
116 static struct unicode_data ucsdata
;
117 static int session_closed
;
118 static int reconfiguring
= FALSE
;
120 static const struct telnet_special
*specials
= NULL
;
121 static HMENU specials_menu
= NULL
;
122 static int n_specials
= 0;
124 #define TIMING_TIMER_ID 1234
125 static long timing_next_time
;
130 enum { SYSMENU
, CTXMENU
};
131 static HMENU savedsess_menu
;
133 Config cfg
; /* exported to windlg.c */
135 static struct sesslist sesslist
; /* for saved-session menu */
137 struct agent_callback
{
138 void (*callback
)(void *, void *, int);
144 #define FONT_NORMAL 0
146 #define FONT_UNDERLINE 2
147 #define FONT_BOLDUND 3
148 #define FONT_WIDE 0x04
149 #define FONT_HIGH 0x08
150 #define FONT_NARROW 0x10
152 #define FONT_OEM 0x20
153 #define FONT_OEMBOLD 0x21
154 #define FONT_OEMUND 0x22
155 #define FONT_OEMBOLDUND 0x23
157 #define FONT_MAXNO 0x2F
159 static HFONT fonts
[FONT_MAXNO
];
160 static LOGFONT lfont
;
161 static int fontflag
[FONT_MAXNO
];
163 BOLD_COLOURS
, BOLD_SHADOW
, BOLD_FONT
170 #define NCFGCOLOURS 22
171 #define NEXTCOLOURS 240
172 #define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
173 static COLORREF colours
[NALLCOLOURS
];
175 static LPLOGPALETTE logpal
;
176 static RGBTRIPLE defpal
[NALLCOLOURS
];
178 static HBITMAP caretbm
;
180 static int dbltime
, lasttime
, lastact
;
181 static Mouse_Button lastbtn
;
183 /* this allows xterm-style mouse handling. */
184 static int send_raw_mouse
= 0;
185 static int wheel_accumulator
= 0;
187 static int busy_status
= BUSY_NOT
;
189 static char *window_name
, *icon_name
;
191 static int compose_state
= 0;
193 static UINT wm_mousewheel
= WM_MOUSEWHEEL
;
195 /* Dummy routine, only required in plink. */
196 void ldisc_update(void *frontend
, int echo
, int edit
)
200 char *get_ttymode(void *frontend
, const char *mode
)
202 return term_get_ttymode(term
, mode
);
205 static void start_backend(void)
208 char msg
[1024], *title
;
213 * Select protocol. This is farmed out into a table in a
214 * separate file to enable an ssh-free variant.
217 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
218 if (backends
[i
].protocol
== cfg
.protocol
) {
219 back
= backends
[i
].backend
;
223 char *str
= dupprintf("%s Internal Error", appname
);
224 MessageBox(NULL
, "Unsupported protocol number found",
225 str
, MB_OK
| MB_ICONEXCLAMATION
);
230 error
= back
->init(NULL
, &backhandle
, &cfg
,
231 cfg
.host
, cfg
.port
, &realhost
, cfg
.tcp_nodelay
,
233 back
->provide_logctx(backhandle
, logctx
);
235 char *str
= dupprintf("%s Error", appname
);
236 sprintf(msg
, "Unable to open connection to\n"
237 "%.800s\n" "%s", cfg
.host
, error
);
238 MessageBox(NULL
, msg
, str
, MB_ICONERROR
| MB_OK
);
242 window_name
= icon_name
= NULL
;
244 title
= cfg
.wintitle
;
246 sprintf(msg
, "%s - %s", realhost
, appname
);
250 set_title(NULL
, title
);
251 set_icon(NULL
, title
);
254 * Connect the terminal to the backend for resize purposes.
256 term_provide_resize_fn(term
, back
->size
, backhandle
);
259 * Set up a line discipline.
261 ldisc
= ldisc_create(&cfg
, term
, back
, backhandle
, NULL
);
264 * Destroy the Restart Session menu item. (This will return
265 * failure if it's already absent, as it will be the very first
266 * time we call this function. We ignore that, because as long
267 * as the menu item ends up not being there, we don't care
268 * whether it was us who removed it or not!)
270 for (i
= 0; i
< lenof(popup_menus
); i
++) {
271 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
274 session_closed
= FALSE
;
277 static void close_session(void)
282 session_closed
= TRUE
;
283 sprintf(morestuff
, "%.70s (inactive)", appname
);
284 set_icon(NULL
, morestuff
);
285 set_title(NULL
, morestuff
);
292 back
->free(backhandle
);
295 update_specials_menu(NULL
);
299 * Show the Restart Session menu item. Do a precautionary
300 * delete first to ensure we never end up with more than one.
302 for (i
= 0; i
< lenof(popup_menus
); i
++) {
303 DeleteMenu(popup_menus
[i
].menu
, IDM_RESTART
, MF_BYCOMMAND
);
304 InsertMenu(popup_menus
[i
].menu
, IDM_DUPSESS
, MF_BYCOMMAND
| MF_ENABLED
,
305 IDM_RESTART
, "&Restart Session");
309 int WINAPI
WinMain(HINSTANCE inst
, HINSTANCE prev
, LPSTR cmdline
, int show
)
313 int guess_width
, guess_height
;
317 flags
= FLAG_VERBOSE
| FLAG_INTERACTIVE
;
321 InitCommonControls();
323 /* Ensure a Maximize setting in Explorer doesn't maximise the
329 char *str
= dupprintf("%s Fatal Error", appname
);
330 MessageBox(NULL
, "Windows refuses to report a version",
331 str
, MB_OK
| MB_ICONEXCLAMATION
);
337 * If we're running a version of Windows that doesn't support
338 * WM_MOUSEWHEEL, find out what message number we should be
341 if (osVersion
.dwMajorVersion
< 4 ||
342 (osVersion
.dwMajorVersion
== 4 &&
343 osVersion
.dwPlatformId
!= VER_PLATFORM_WIN32_NT
))
344 wm_mousewheel
= RegisterWindowMessage("MSWHEEL_ROLLMSG");
347 * See if we can find our Help file.
350 char b
[2048], *p
, *q
, *r
;
352 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
354 p
= strrchr(b
, '\\');
355 if (p
&& p
>= r
) r
= p
+1;
357 if (q
&& q
>= r
) r
= q
+1;
358 strcpy(r
, PUTTY_HELP_FILE
);
359 if ( (fp
= fopen(b
, "r")) != NULL
) {
360 help_path
= dupstr(b
);
364 strcpy(r
, PUTTY_HELP_CONTENTS
);
365 if ( (fp
= fopen(b
, "r")) != NULL
) {
366 help_has_contents
= TRUE
;
369 help_has_contents
= FALSE
;
373 * Process the command line.
379 default_protocol
= be_default_protocol
;
380 /* Find the appropriate default port. */
383 default_port
= 0; /* illegal */
384 for (i
= 0; backends
[i
].backend
!= NULL
; i
++)
385 if (backends
[i
].protocol
== default_protocol
) {
386 default_port
= backends
[i
].backend
->default_port
;
390 cfg
.logtype
= LGTYP_NONE
;
392 do_defaults(NULL
, &cfg
);
397 * Process a couple of command-line options which are more
398 * easily dealt with before the line is broken up into
399 * words. These are the soon-to-be-defunct @sessionname and
400 * the internal-use-only &sharedmemoryhandle, neither of
401 * which are combined with anything else.
403 while (*p
&& isspace(*p
))
407 while (i
> 1 && isspace(p
[i
- 1]))
410 do_defaults(p
+ 1, &cfg
);
411 if (!*cfg
.host
&& !do_config()) {
414 } else if (*p
== '&') {
416 * An initial & means we've been given a command line
417 * containing the hex value of a HANDLE for a file
418 * mapping object, which we must then extract as a
423 if (sscanf(p
+ 1, "%p", &filemap
) == 1 &&
424 (cp
= MapViewOfFile(filemap
, FILE_MAP_READ
,
425 0, 0, sizeof(Config
))) != NULL
) {
428 CloseHandle(filemap
);
429 } else if (!do_config()) {
434 * Otherwise, break up the command line and deal with
440 split_into_argv(cmdline
, &argc
, &argv
, NULL
);
442 for (i
= 0; i
< argc
; i
++) {
446 ret
= cmdline_process_param(p
, i
+1<argc?argv
[i
+1]:NULL
,
449 cmdline_error("option \"%s\" requires an argument", p
);
450 } else if (ret
== 2) {
451 i
++; /* skip next argument */
452 } else if (ret
== 1) {
453 continue; /* nothing further needs doing */
454 } else if (!strcmp(p
, "-cleanup") ||
455 !strcmp(p
, "-cleanup-during-uninstall")) {
457 * `putty -cleanup'. Remove all registry
458 * entries associated with PuTTY, and also find
459 * and delete the random seed file.
462 /* Are we being invoked from an uninstaller? */
463 if (!strcmp(p
, "-cleanup-during-uninstall")) {
464 s1
= dupprintf("Remove saved sessions and random seed file?\n"
466 "If you hit Yes, ALL Registry entries associated\n"
467 "with %s will be removed, as well as the\n"
468 "random seed file. THIS PROCESS WILL\n"
469 "DESTROY YOUR SAVED SESSIONS.\n"
470 "(This only affects the currently logged-in user.)\n"
472 "If you hit No, uninstallation will proceed, but\n"
473 "saved sessions etc will be left on the machine.",
475 s2
= dupprintf("%s Uninstallation", appname
);
477 s1
= dupprintf("This procedure will remove ALL Registry entries\n"
478 "associated with %s, and will also remove\n"
479 "the random seed file. (This only affects the\n"
480 "currently logged-in user.)\n"
482 "THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
483 "Are you really sure you want to continue?",
485 s2
= dupprintf("%s Warning", appname
);
487 if (message_box(s1
, s2
,
488 MB_YESNO
| MB_ICONWARNING
| MB_DEFBUTTON2
,
489 HELPCTXID(option_cleanup
)) == IDYES
) {
495 } else if (!strcmp(p
, "-pgpfp")) {
498 } else if (*p
!= '-') {
502 * If we already have a host name, treat
503 * this argument as a port number. NB we
504 * have to treat this as a saved -P
505 * argument, so that it will be deferred
506 * until it's a good moment to run it.
508 int ret
= cmdline_process_param("-P", p
, 1, &cfg
);
510 } else if (!strncmp(q
, "telnet:", 7)) {
512 * If the hostname starts with "telnet:",
513 * set the protocol to Telnet and process
514 * the string as a Telnet URL.
519 if (q
[0] == '/' && q
[1] == '/')
521 cfg
.protocol
= PROT_TELNET
;
523 while (*p
&& *p
!= ':' && *p
!= '/')
532 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
533 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
537 * Otherwise, treat this argument as a host
540 while (*p
&& !isspace(*p
))
544 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
545 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
549 cmdline_error("unknown option \"%s\"", p
);
554 cmdline_run_saved(&cfg
);
556 if (!*cfg
.host
&& !do_config()) {
561 * Trim leading whitespace off the hostname if it's there.
564 int space
= strspn(cfg
.host
, " \t");
565 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
568 /* See if host is of the form user@host */
569 if (cfg
.host
[0] != '\0') {
570 char *atsign
= strrchr(cfg
.host
, '@');
571 /* Make sure we're not overflowing the user field */
573 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
574 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
575 cfg
.username
[atsign
- cfg
.host
] = '\0';
577 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
582 * Trim a colon suffix off the hostname if it's there. In
583 * order to protect IPv6 address literals against this
584 * treatment, we do not do this if there's _more_ than one
588 char *c
= strchr(cfg
.host
, ':');
591 char *d
= strchr(c
+1, ':');
598 * Remove any remaining whitespace from the hostname.
602 while (cfg
.host
[p2
] != '\0') {
603 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
604 cfg
.host
[p1
] = cfg
.host
[p2
];
613 /* Check for invalid Port number (i.e. zero) */
615 char *str
= dupprintf("%s Internal Error", appname
);
616 MessageBox(NULL
, "Invalid Port Number",
617 str
, MB_OK
| MB_ICONEXCLAMATION
);
624 wndclass
.lpfnWndProc
= WndProc
;
625 wndclass
.cbClsExtra
= 0;
626 wndclass
.cbWndExtra
= 0;
627 wndclass
.hInstance
= inst
;
628 wndclass
.hIcon
= LoadIcon(inst
, MAKEINTRESOURCE(IDI_MAINICON
));
629 wndclass
.hCursor
= LoadCursor(NULL
, IDC_IBEAM
);
630 wndclass
.hbrBackground
= NULL
;
631 wndclass
.lpszMenuName
= NULL
;
632 wndclass
.lpszClassName
= appname
;
634 RegisterClass(&wndclass
);
637 memset(&ucsdata
, 0, sizeof(ucsdata
));
642 * Guess some defaults for the window size. This all gets
643 * updated later, so we don't really care too much. However, we
644 * do want the font width/height guesses to correspond to a
645 * large font rather than a small one...
652 guess_width
= extra_width
+ font_width
* cfg
.width
;
653 guess_height
= extra_height
+ font_height
* cfg
.height
;
656 get_fullscreen_rect(&r
);
657 if (guess_width
> r
.right
- r
.left
)
658 guess_width
= r
.right
- r
.left
;
659 if (guess_height
> r
.bottom
- r
.top
)
660 guess_height
= r
.bottom
- r
.top
;
664 int winmode
= WS_OVERLAPPEDWINDOW
| WS_VSCROLL
;
667 winmode
&= ~(WS_VSCROLL
);
668 if (cfg
.resize_action
== RESIZE_DISABLED
)
669 winmode
&= ~(WS_THICKFRAME
| WS_MAXIMIZEBOX
);
671 exwinmode
|= WS_EX_TOPMOST
;
673 exwinmode
|= WS_EX_CLIENTEDGE
;
674 hwnd
= CreateWindowEx(exwinmode
, appname
, appname
,
675 winmode
, CW_USEDEFAULT
, CW_USEDEFAULT
,
676 guess_width
, guess_height
,
677 NULL
, NULL
, inst
, NULL
);
681 * Initialise the terminal. (We have to do this _after_
682 * creating the window, since the terminal is the first thing
683 * which will call schedule_timer(), which will in turn call
684 * timer_change_notify() which will expect hwnd to exist.)
686 term
= term_init(&cfg
, &ucsdata
, NULL
);
687 logctx
= log_init(NULL
, &cfg
);
688 term_provide_logctx(term
, logctx
);
689 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
692 * Initialise the fonts, simultaneously correcting the guesses
693 * for font_{width,height}.
698 * Correct the guesses for extra_{width,height}.
702 GetWindowRect(hwnd
, &wr
);
703 GetClientRect(hwnd
, &cr
);
704 offset_width
= offset_height
= cfg
.window_border
;
705 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
706 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
710 * Resize the window, now we know what size we _really_ want it
713 guess_width
= extra_width
+ font_width
* term
->cols
;
714 guess_height
= extra_height
+ font_height
* term
->rows
;
715 SetWindowPos(hwnd
, NULL
, 0, 0, guess_width
, guess_height
,
716 SWP_NOMOVE
| SWP_NOREDRAW
| SWP_NOZORDER
);
719 * Set up a caret bitmap, with no content.
723 int size
= (font_width
+ 15) / 16 * 2 * font_height
;
724 bits
= snewn(size
, char);
725 memset(bits
, 0, size
);
726 caretbm
= CreateBitmap(font_width
, font_height
, 1, 1, bits
);
729 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
732 * Initialise the scroll bar.
737 si
.cbSize
= sizeof(si
);
738 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
740 si
.nMax
= term
->rows
- 1;
741 si
.nPage
= term
->rows
;
743 SetScrollInfo(hwnd
, SB_VERT
, &si
, FALSE
);
747 * Prepare the mouse handler.
749 lastact
= MA_NOTHING
;
750 lastbtn
= MBT_NOTHING
;
751 dbltime
= GetDoubleClickTime();
754 * Set up the session-control options on the system menu.
761 popup_menus
[SYSMENU
].menu
= GetSystemMenu(hwnd
, FALSE
);
762 popup_menus
[CTXMENU
].menu
= CreatePopupMenu();
763 AppendMenu(popup_menus
[CTXMENU
].menu
, MF_ENABLED
, IDM_PASTE
, "&Paste");
765 savedsess_menu
= CreateMenu();
766 get_sesslist(&sesslist
, TRUE
);
767 update_savedsess_menu();
769 for (j
= 0; j
< lenof(popup_menus
); j
++) {
770 m
= popup_menus
[j
].menu
;
772 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
773 AppendMenu(m
, MF_ENABLED
, IDM_SHOWLOG
, "&Event Log");
774 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
775 AppendMenu(m
, MF_ENABLED
, IDM_NEWSESS
, "Ne&w Session...");
776 AppendMenu(m
, MF_ENABLED
, IDM_DUPSESS
, "&Duplicate Session");
777 AppendMenu(m
, MF_POPUP
| MF_ENABLED
, (UINT
) savedsess_menu
,
779 AppendMenu(m
, MF_ENABLED
, IDM_RECONF
, "Chan&ge Settings...");
780 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
781 AppendMenu(m
, MF_ENABLED
, IDM_COPYALL
, "C&opy All to Clipboard");
782 AppendMenu(m
, MF_ENABLED
, IDM_CLRSB
, "C&lear Scrollback");
783 AppendMenu(m
, MF_ENABLED
, IDM_RESET
, "Rese&t Terminal");
784 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
785 AppendMenu(m
, (cfg
.resize_action
== RESIZE_DISABLED
) ?
786 MF_GRAYED
: MF_ENABLED
, IDM_FULLSCREEN
, "&Full Screen");
787 AppendMenu(m
, MF_SEPARATOR
, 0, 0);
789 AppendMenu(m
, MF_ENABLED
, IDM_HELP
, "&Help");
790 str
= dupprintf("&About %s", appname
);
791 AppendMenu(m
, MF_ENABLED
, IDM_ABOUT
, str
);
799 * Set up the initial input locale.
801 set_input_locale(GetKeyboardLayout(0));
804 * Finally show the window!
806 ShowWindow(hwnd
, show
);
807 SetForegroundWindow(hwnd
);
810 * Set the palette up.
816 term_set_focus(term
, GetForegroundWindow() == hwnd
);
823 handles
= handle_get_events(&nhandles
);
825 n
= MsgWaitForMultipleObjects(nhandles
, handles
, FALSE
, INFINITE
,
828 if ((unsigned)(n
- WAIT_OBJECT_0
) < (unsigned)nhandles
) {
829 handle_got_event(handles
[n
- WAIT_OBJECT_0
]);
836 if (GetMessage(&msg
, NULL
, 0, 0) != 1)
839 if (msg
.message
== WM_QUIT
)
840 goto finished
; /* two-level break */
842 if (!(IsWindow(logbox
) && IsDialogMessage(logbox
, &msg
)))
843 DispatchMessage(&msg
);
844 /* Send the paste buffer if there's anything to send */
846 /* If there's nothing new in the queue then we can do everything
847 * we've delayed, reading the socket, writing, and repainting
850 } while (PeekMessage(&msg
, NULL
, 0, 0, PM_REMOVE
));
852 /* The messages seem unreliable; especially if we're being tricky */
853 term_set_focus(term
, GetForegroundWindow() == hwnd
);
855 net_pending_errors();
859 cleanup_exit(msg
.wParam
); /* this doesn't return... */
860 return msg
.wParam
; /* ... but optimiser doesn't know */
866 void cleanup_exit(int code
)
877 if (cfg
.protocol
== PROT_SSH
) {
888 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
890 char *do_select(SOCKET skt
, int startup
)
895 events
= (FD_CONNECT
| FD_READ
| FD_WRITE
|
896 FD_OOB
| FD_CLOSE
| FD_ACCEPT
);
901 return "do_select(): internal error (hwnd==NULL)";
902 if (p_WSAAsyncSelect(skt
, hwnd
, msg
, events
) == SOCKET_ERROR
) {
903 switch (p_WSAGetLastError()) {
905 return "Network is down";
907 return "WSAAsyncSelect(): unknown error";
914 * Refresh the saved-session submenu from `sesslist'.
916 static void update_savedsess_menu(void)
919 while (DeleteMenu(savedsess_menu
, 0, MF_BYPOSITION
)) ;
920 /* skip sesslist.sessions[0] == Default Settings */
922 i
< ((sesslist
.nsessions
<= MENU_SAVED_MAX
+1) ? sesslist
.nsessions
925 AppendMenu(savedsess_menu
, MF_ENABLED
,
926 IDM_SAVED_MIN
+ (i
-1)*MENU_SAVED_STEP
,
927 sesslist
.sessions
[i
]);
931 * Update the Special Commands submenu.
933 void update_specials_menu(void *frontend
)
939 specials
= back
->get_specials(backhandle
);
944 /* We can't use Windows to provide a stack for submenus, so
945 * here's a lame "stack" that will do for now. */
946 HMENU saved_menu
= NULL
;
948 new_menu
= CreatePopupMenu();
949 for (i
= 0; nesting
> 0; i
++) {
950 assert(IDM_SPECIAL_MIN
+ 0x10 * i
< IDM_SPECIAL_MAX
);
951 switch (specials
[i
].code
) {
953 AppendMenu(new_menu
, MF_SEPARATOR
, 0, 0);
958 saved_menu
= new_menu
; /* XXX lame stacking */
959 new_menu
= CreatePopupMenu();
960 AppendMenu(saved_menu
, MF_POPUP
| MF_ENABLED
,
961 (UINT
) new_menu
, specials
[i
].name
);
966 new_menu
= saved_menu
; /* XXX lame stacking */
971 AppendMenu(new_menu
, MF_ENABLED
, IDM_SPECIAL_MIN
+ 0x10 * i
,
976 /* Squirrel the highest special. */
983 for (j
= 0; j
< lenof(popup_menus
); j
++) {
985 /* XXX does this free up all submenus? */
986 DeleteMenu(popup_menus
[j
].menu
, specials_menu
, MF_BYCOMMAND
);
987 DeleteMenu(popup_menus
[j
].menu
, IDM_SPECIALSEP
, MF_BYCOMMAND
);
990 InsertMenu(popup_menus
[j
].menu
, IDM_SHOWLOG
,
991 MF_BYCOMMAND
| MF_POPUP
| MF_ENABLED
,
992 (UINT
) new_menu
, "S&pecial Command");
993 InsertMenu(popup_menus
[j
].menu
, IDM_SHOWLOG
,
994 MF_BYCOMMAND
| MF_SEPARATOR
, IDM_SPECIALSEP
, 0);
997 specials_menu
= new_menu
;
1000 static void update_mouse_pointer(void)
1003 int force_visible
= FALSE
;
1004 static int forced_visible
= FALSE
;
1005 switch (busy_status
) {
1008 curstype
= IDC_ARROW
;
1010 curstype
= IDC_IBEAM
;
1013 curstype
= IDC_APPSTARTING
; /* this may be an abuse */
1014 force_visible
= TRUE
;
1017 curstype
= IDC_WAIT
;
1018 force_visible
= TRUE
;
1024 HCURSOR cursor
= LoadCursor(NULL
, curstype
);
1025 SetClassLongPtr(hwnd
, GCLP_HCURSOR
, (LONG_PTR
)cursor
);
1026 SetCursor(cursor
); /* force redraw of cursor at current posn */
1028 if (force_visible
!= forced_visible
) {
1029 /* We want some cursor shapes to be visible always.
1030 * Along with show_mouseptr(), this manages the ShowCursor()
1031 * counter such that if we switch back to a non-force_visible
1032 * cursor, the previous visibility state is restored. */
1033 ShowCursor(force_visible
);
1034 forced_visible
= force_visible
;
1038 void set_busy_status(void *frontend
, int status
)
1040 busy_status
= status
;
1041 update_mouse_pointer();
1045 * set or clear the "raw mouse message" mode
1047 void set_raw_mouse_mode(void *frontend
, int activate
)
1049 activate
= activate
&& !cfg
.no_mouse_rep
;
1050 send_raw_mouse
= activate
;
1051 update_mouse_pointer();
1055 * Print a message box and close the connection.
1057 void connection_fatal(void *frontend
, char *fmt
, ...)
1060 char *stuff
, morestuff
[100];
1063 stuff
= dupvprintf(fmt
, ap
);
1065 sprintf(morestuff
, "%.70s Fatal Error", appname
);
1066 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
1069 if (cfg
.close_on_exit
== FORCE_ON
)
1077 * Report an error at the command-line parsing stage.
1079 void cmdline_error(char *fmt
, ...)
1082 char *stuff
, morestuff
[100];
1085 stuff
= dupvprintf(fmt
, ap
);
1087 sprintf(morestuff
, "%.70s Command Line Error", appname
);
1088 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
1094 * Actually do the job requested by a WM_NETEVENT
1096 static void enact_netevent(WPARAM wParam
, LPARAM lParam
)
1098 static int reentering
= 0;
1099 extern int select_result(WPARAM
, LPARAM
);
1103 return; /* don't unpend the pending */
1106 ret
= select_result(wParam
, lParam
);
1109 if (ret
== 0 && !session_closed
) {
1110 /* Abnormal exits will already have set session_closed and taken
1111 * appropriate action. */
1112 if (cfg
.close_on_exit
== FORCE_ON
||
1113 cfg
.close_on_exit
== AUTO
) PostQuitMessage(0);
1116 session_closed
= TRUE
;
1117 MessageBox(hwnd
, "Connection closed by remote host",
1118 appname
, MB_OK
| MB_ICONINFORMATION
);
1124 * Copy the colour palette from the configuration data into defpal.
1125 * This is non-trivial because the colour indices are different.
1127 static void cfgtopalette(void)
1130 static const int ww
[] = {
1131 256, 257, 258, 259, 260, 261,
1132 0, 8, 1, 9, 2, 10, 3, 11,
1133 4, 12, 5, 13, 6, 14, 7, 15
1136 for (i
= 0; i
< 22; i
++) {
1138 defpal
[w
].rgbtRed
= cfg
.colours
[i
][0];
1139 defpal
[w
].rgbtGreen
= cfg
.colours
[i
][1];
1140 defpal
[w
].rgbtBlue
= cfg
.colours
[i
][2];
1142 for (i
= 0; i
< NEXTCOLOURS
; i
++) {
1144 int r
= i
/ 36, g
= (i
/ 6) % 6, b
= i
% 6;
1145 defpal
[i
+16].rgbtRed
= r ? r
* 40 + 55 : 0;
1146 defpal
[i
+16].rgbtGreen
= g ? g
* 40 + 55 : 0;
1147 defpal
[i
+16].rgbtBlue
= b ? b
* 40 + 55 : 0;
1149 int shade
= i
- 216;
1150 shade
= shade
* 10 + 8;
1151 defpal
[i
+16].rgbtRed
= defpal
[i
+16].rgbtGreen
=
1152 defpal
[i
+16].rgbtBlue
= shade
;
1156 /* Override with system colours if appropriate */
1157 if (cfg
.system_colour
)
1162 * Override bit of defpal with colours from the system.
1163 * (NB that this takes a copy the system colours at the time this is called,
1164 * so subsequent colour scheme changes don't take effect. To fix that we'd
1165 * probably want to be using GetSysColorBrush() and the like.)
1167 static void systopalette(void)
1170 static const struct { int nIndex
; int norm
; int bold
; } or[] =
1172 { COLOR_WINDOWTEXT
, 256, 257 }, /* Default Foreground */
1173 { COLOR_WINDOW
, 258, 259 }, /* Default Background */
1174 { COLOR_HIGHLIGHTTEXT
, 260, 260 }, /* Cursor Text */
1175 { COLOR_HIGHLIGHT
, 261, 261 }, /* Cursor Colour */
1178 for (i
= 0; i
< (sizeof(or)/sizeof(or[0])); i
++) {
1179 COLORREF colour
= GetSysColor(or[i
].nIndex
);
1180 defpal
[or[i
].norm
].rgbtRed
=
1181 defpal
[or[i
].bold
].rgbtRed
= GetRValue(colour
);
1182 defpal
[or[i
].norm
].rgbtGreen
=
1183 defpal
[or[i
].bold
].rgbtGreen
= GetGValue(colour
);
1184 defpal
[or[i
].norm
].rgbtBlue
=
1185 defpal
[or[i
].bold
].rgbtBlue
= GetBValue(colour
);
1190 * Set up the colour palette.
1192 static void init_palette(void)
1195 HDC hdc
= GetDC(hwnd
);
1197 if (cfg
.try_palette
&& GetDeviceCaps(hdc
, RASTERCAPS
) & RC_PALETTE
) {
1199 * This is a genuine case where we must use smalloc
1200 * because the snew macros can't cope.
1202 logpal
= smalloc(sizeof(*logpal
)
1203 - sizeof(logpal
->palPalEntry
)
1204 + NALLCOLOURS
* sizeof(PALETTEENTRY
));
1205 logpal
->palVersion
= 0x300;
1206 logpal
->palNumEntries
= NALLCOLOURS
;
1207 for (i
= 0; i
< NALLCOLOURS
; i
++) {
1208 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
1209 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
1210 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
1211 logpal
->palPalEntry
[i
].peFlags
= PC_NOCOLLAPSE
;
1213 pal
= CreatePalette(logpal
);
1215 SelectPalette(hdc
, pal
, FALSE
);
1216 RealizePalette(hdc
);
1217 SelectPalette(hdc
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
1220 ReleaseDC(hwnd
, hdc
);
1223 for (i
= 0; i
< NALLCOLOURS
; i
++)
1224 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
1225 defpal
[i
].rgbtGreen
,
1226 defpal
[i
].rgbtBlue
);
1228 for (i
= 0; i
< NALLCOLOURS
; i
++)
1229 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
1230 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
1234 * This is a wrapper to ExtTextOut() to force Windows to display
1235 * the precise glyphs we give it. Otherwise it would do its own
1236 * bidi and Arabic shaping, and we would end up uncertain which
1237 * characters it had put where.
1239 static void exact_textout(HDC hdc
, int x
, int y
, CONST RECT
*lprc
,
1240 unsigned short *lpString
, UINT cbCount
,
1241 CONST INT
*lpDx
, int opaque
)
1245 * The LCC include files apparently don't supply the
1246 * GCP_RESULTSW type, but we can make do with GCP_RESULTS
1247 * proper: the differences aren't important to us (the only
1248 * variable-width string parameter is one we don't use anyway).
1254 char *buffer
= snewn(cbCount
*2+2, char);
1255 char *classbuffer
= snewn(cbCount
, char);
1256 memset(&gcpr
, 0, sizeof(gcpr
));
1257 memset(buffer
, 0, cbCount
*2+2);
1258 memset(classbuffer
, GCPCLASS_NEUTRAL
, cbCount
);
1260 gcpr
.lStructSize
= sizeof(gcpr
);
1261 gcpr
.lpGlyphs
= (void *)buffer
;
1262 gcpr
.lpClass
= classbuffer
;
1263 gcpr
.nGlyphs
= cbCount
;
1265 GetCharacterPlacementW(hdc
, lpString
, cbCount
, 0, &gcpr
,
1266 FLI_MASK
| GCP_CLASSIN
| GCP_DIACRITIC
);
1268 ExtTextOut(hdc
, x
, y
,
1269 ETO_GLYPH_INDEX
| ETO_CLIPPED
| (opaque ? ETO_OPAQUE
: 0),
1270 lprc
, buffer
, cbCount
, lpDx
);
1274 * Initialise all the fonts we will need initially. There may be as many as
1275 * three or as few as one. The other (poentially) twentyone fonts are done
1276 * if/when they are needed.
1280 * - check the font width and height, correcting our guesses if
1283 * - verify that the bold font is the same width as the ordinary
1284 * one, and engage shadow bolding if not.
1286 * - verify that the underlined font is the same width as the
1287 * ordinary one (manual underlining by means of line drawing can
1288 * be done in a pinch).
1290 static void init_fonts(int pick_width
, int pick_height
)
1297 int fw_dontcare
, fw_bold
;
1299 for (i
= 0; i
< FONT_MAXNO
; i
++)
1302 bold_mode
= cfg
.bold_colour ? BOLD_COLOURS
: BOLD_FONT
;
1303 und_mode
= UND_FONT
;
1305 if (cfg
.font
.isbold
) {
1306 fw_dontcare
= FW_BOLD
;
1309 fw_dontcare
= FW_DONTCARE
;
1316 font_height
= pick_height
;
1318 font_height
= cfg
.font
.height
;
1319 if (font_height
> 0) {
1321 -MulDiv(font_height
, GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1324 font_width
= pick_width
;
1326 #define f(i,c,w,u) \
1327 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1328 c, OUT_DEFAULT_PRECIS, \
1329 CLIP_DEFAULT_PRECIS, FONT_QUALITY(cfg.font_quality), \
1330 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1332 f(FONT_NORMAL
, cfg
.font
.charset
, fw_dontcare
, FALSE
);
1334 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
1335 GetTextMetrics(hdc
, &tm
);
1337 GetObject(fonts
[FONT_NORMAL
], sizeof(LOGFONT
), &lfont
);
1339 if (pick_width
== 0 || pick_height
== 0) {
1340 font_height
= tm
.tmHeight
;
1341 font_width
= tm
.tmAveCharWidth
;
1343 font_dualwidth
= (tm
.tmAveCharWidth
!= tm
.tmMaxCharWidth
);
1345 #ifdef RDB_DEBUG_PATCH
1346 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1347 tm
.tmHeight
, tm
.tmAveCharWidth
, tm
.tmMaxCharWidth
);
1352 DWORD cset
= tm
.tmCharSet
;
1353 memset(&info
, 0xFF, sizeof(info
));
1355 /* !!! Yes the next line is right */
1356 if (cset
== OEM_CHARSET
)
1357 ucsdata
.font_codepage
= GetOEMCP();
1359 if (TranslateCharsetInfo ((DWORD
*) cset
, &info
, TCI_SRCCHARSET
))
1360 ucsdata
.font_codepage
= info
.ciACP
;
1362 ucsdata
.font_codepage
= -1;
1364 GetCPInfo(ucsdata
.font_codepage
, &cpinfo
);
1365 ucsdata
.dbcs_screenfont
= (cpinfo
.MaxCharSize
> 1);
1368 f(FONT_UNDERLINE
, cfg
.font
.charset
, fw_dontcare
, TRUE
);
1371 * Some fonts, e.g. 9-pt Courier, draw their underlines
1372 * outside their character cell. We successfully prevent
1373 * screen corruption by clipping the text output, but then
1374 * we lose the underline completely. Here we try to work
1375 * out whether this is such a font, and if it is, we set a
1376 * flag that causes underlines to be drawn by hand.
1378 * Having tried other more sophisticated approaches (such
1379 * as examining the TEXTMETRIC structure or requesting the
1380 * height of a string), I think we'll do this the brute
1381 * force way: we create a small bitmap, draw an underlined
1382 * space on it, and test to see whether any pixels are
1383 * foreground-coloured. (Since we expect the underline to
1384 * go all the way across the character cell, we only search
1385 * down a single column of the bitmap, half way across.)
1389 HBITMAP und_bm
, und_oldbm
;
1393 und_dc
= CreateCompatibleDC(hdc
);
1394 und_bm
= CreateCompatibleBitmap(hdc
, font_width
, font_height
);
1395 und_oldbm
= SelectObject(und_dc
, und_bm
);
1396 SelectObject(und_dc
, fonts
[FONT_UNDERLINE
]);
1397 SetTextAlign(und_dc
, TA_TOP
| TA_LEFT
| TA_NOUPDATECP
);
1398 SetTextColor(und_dc
, RGB(255, 255, 255));
1399 SetBkColor(und_dc
, RGB(0, 0, 0));
1400 SetBkMode(und_dc
, OPAQUE
);
1401 ExtTextOut(und_dc
, 0, 0, ETO_OPAQUE
, NULL
, " ", 1, NULL
);
1403 for (i
= 0; i
< font_height
; i
++) {
1404 c
= GetPixel(und_dc
, font_width
/ 2, i
);
1405 if (c
!= RGB(0, 0, 0))
1408 SelectObject(und_dc
, und_oldbm
);
1409 DeleteObject(und_bm
);
1412 und_mode
= UND_LINE
;
1413 DeleteObject(fonts
[FONT_UNDERLINE
]);
1414 fonts
[FONT_UNDERLINE
] = 0;
1418 if (bold_mode
== BOLD_FONT
) {
1419 f(FONT_BOLD
, cfg
.font
.charset
, fw_bold
, FALSE
);
1423 descent
= tm
.tmAscent
+ 1;
1424 if (descent
>= font_height
)
1425 descent
= font_height
- 1;
1427 for (i
= 0; i
< 3; i
++) {
1429 if (SelectObject(hdc
, fonts
[i
]) && GetTextMetrics(hdc
, &tm
))
1430 fontsize
[i
] = tm
.tmAveCharWidth
+ 256 * tm
.tmHeight
;
1437 ReleaseDC(hwnd
, hdc
);
1439 if (fontsize
[FONT_UNDERLINE
] != fontsize
[FONT_NORMAL
]) {
1440 und_mode
= UND_LINE
;
1441 DeleteObject(fonts
[FONT_UNDERLINE
]);
1442 fonts
[FONT_UNDERLINE
] = 0;
1445 if (bold_mode
== BOLD_FONT
&&
1446 fontsize
[FONT_BOLD
] != fontsize
[FONT_NORMAL
]) {
1447 bold_mode
= BOLD_SHADOW
;
1448 DeleteObject(fonts
[FONT_BOLD
]);
1449 fonts
[FONT_BOLD
] = 0;
1451 fontflag
[0] = fontflag
[1] = fontflag
[2] = 1;
1453 init_ucs(&cfg
, &ucsdata
);
1456 static void another_font(int fontno
)
1459 int fw_dontcare
, fw_bold
;
1463 if (fontno
< 0 || fontno
>= FONT_MAXNO
|| fontflag
[fontno
])
1466 basefont
= (fontno
& ~(FONT_BOLDUND
));
1467 if (basefont
!= fontno
&& !fontflag
[basefont
])
1468 another_font(basefont
);
1470 if (cfg
.font
.isbold
) {
1471 fw_dontcare
= FW_BOLD
;
1474 fw_dontcare
= FW_DONTCARE
;
1478 c
= cfg
.font
.charset
;
1484 if (fontno
& FONT_WIDE
)
1486 if (fontno
& FONT_NARROW
)
1488 if (fontno
& FONT_OEM
)
1490 if (fontno
& FONT_BOLD
)
1492 if (fontno
& FONT_UNDERLINE
)
1496 CreateFont(font_height
* (1 + !!(fontno
& FONT_HIGH
)), x
, 0, 0, w
,
1497 FALSE
, u
, FALSE
, c
, OUT_DEFAULT_PRECIS
,
1498 CLIP_DEFAULT_PRECIS
, FONT_QUALITY(cfg
.font_quality
),
1499 FIXED_PITCH
| FF_DONTCARE
, s
);
1501 fontflag
[fontno
] = 1;
1504 static void deinit_fonts(void)
1507 for (i
= 0; i
< FONT_MAXNO
; i
++) {
1509 DeleteObject(fonts
[i
]);
1515 void request_resize(void *frontend
, int w
, int h
)
1519 /* If the window is maximized supress resizing attempts */
1520 if (IsZoomed(hwnd
)) {
1521 if (cfg
.resize_action
== RESIZE_TERM
)
1525 if (cfg
.resize_action
== RESIZE_DISABLED
) return;
1526 if (h
== term
->rows
&& w
== term
->cols
) return;
1528 /* Sanity checks ... */
1530 static int first_time
= 1;
1533 switch (first_time
) {
1535 /* Get the size of the screen */
1536 if (get_fullscreen_rect(&ss
))
1537 /* first_time = 0 */ ;
1543 /* Make sure the values are sane */
1544 width
= (ss
.right
- ss
.left
- extra_width
) / 4;
1545 height
= (ss
.bottom
- ss
.top
- extra_height
) / 6;
1547 if (w
> width
|| h
> height
)
1556 term_size(term
, h
, w
, cfg
.savelines
);
1558 if (cfg
.resize_action
!= RESIZE_FONT
&& !IsZoomed(hwnd
)) {
1559 width
= extra_width
+ font_width
* w
;
1560 height
= extra_height
+ font_height
* h
;
1562 SetWindowPos(hwnd
, NULL
, 0, 0, width
, height
,
1563 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
1564 SWP_NOMOVE
| SWP_NOZORDER
);
1568 InvalidateRect(hwnd
, NULL
, TRUE
);
1571 static void reset_window(int reinit
) {
1573 * This function decides how to resize or redraw when the
1574 * user changes something.
1576 * This function doesn't like to change the terminal size but if the
1577 * font size is locked that may be it's only soluion.
1579 int win_width
, win_height
;
1582 #ifdef RDB_DEBUG_PATCH
1583 debug((27, "reset_window()"));
1586 /* Current window sizes ... */
1587 GetWindowRect(hwnd
, &wr
);
1588 GetClientRect(hwnd
, &cr
);
1590 win_width
= cr
.right
- cr
.left
;
1591 win_height
= cr
.bottom
- cr
.top
;
1593 if (cfg
.resize_action
== RESIZE_DISABLED
) reinit
= 2;
1595 /* Are we being forced to reload the fonts ? */
1597 #ifdef RDB_DEBUG_PATCH
1598 debug((27, "reset_window() -- Forced deinit"));
1604 /* Oh, looks like we're minimised */
1605 if (win_width
== 0 || win_height
== 0)
1608 /* Is the window out of position ? */
1610 (offset_width
!= (win_width
-font_width
*term
->cols
)/2 ||
1611 offset_height
!= (win_height
-font_height
*term
->rows
)/2) ){
1612 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1613 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1614 InvalidateRect(hwnd
, NULL
, TRUE
);
1615 #ifdef RDB_DEBUG_PATCH
1616 debug((27, "reset_window() -> Reposition terminal"));
1620 if (IsZoomed(hwnd
)) {
1621 /* We're fullscreen, this means we must not change the size of
1622 * the window so it's the font size or the terminal itself.
1625 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
;
1626 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
;
1628 if (cfg
.resize_action
!= RESIZE_TERM
) {
1629 if ( font_width
!= win_width
/term
->cols
||
1630 font_height
!= win_height
/term
->rows
) {
1632 init_fonts(win_width
/term
->cols
, win_height
/term
->rows
);
1633 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1634 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1635 InvalidateRect(hwnd
, NULL
, TRUE
);
1636 #ifdef RDB_DEBUG_PATCH
1637 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1638 font_width
, font_height
));
1642 if ( font_width
* term
->cols
!= win_width
||
1643 font_height
* term
->rows
!= win_height
) {
1644 /* Our only choice at this point is to change the
1645 * size of the terminal; Oh well.
1647 term_size(term
, win_height
/font_height
, win_width
/font_width
,
1649 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1650 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1651 InvalidateRect(hwnd
, NULL
, TRUE
);
1652 #ifdef RDB_DEBUG_PATCH
1653 debug((27, "reset_window() -> Zoomed term_size"));
1660 /* Hmm, a force re-init means we should ignore the current window
1661 * so we resize to the default font size.
1664 #ifdef RDB_DEBUG_PATCH
1665 debug((27, "reset_window() -> Forced re-init"));
1668 offset_width
= offset_height
= cfg
.window_border
;
1669 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1670 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1672 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1673 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1675 /* If this is too large windows will resize it to the maximum
1676 * allowed window size, we will then be back in here and resize
1677 * the font or terminal to fit.
1679 SetWindowPos(hwnd
, NULL
, 0, 0,
1680 font_width
*term
->cols
+ extra_width
,
1681 font_height
*term
->rows
+ extra_height
,
1682 SWP_NOMOVE
| SWP_NOZORDER
);
1685 InvalidateRect(hwnd
, NULL
, TRUE
);
1689 /* Okay the user doesn't want us to change the font so we try the
1690 * window. But that may be too big for the screen which forces us
1691 * to change the terminal.
1693 if ((cfg
.resize_action
== RESIZE_TERM
&& reinit
<=0) ||
1694 (cfg
.resize_action
== RESIZE_EITHER
&& reinit
<0) ||
1696 offset_width
= offset_height
= cfg
.window_border
;
1697 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+ offset_width
*2;
1698 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1700 if (win_width
!= font_width
*term
->cols
+ offset_width
*2 ||
1701 win_height
!= font_height
*term
->rows
+ offset_height
*2) {
1706 get_fullscreen_rect(&ss
);
1708 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1709 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1712 if ( term
->rows
> height
|| term
->cols
> width
) {
1713 if (cfg
.resize_action
== RESIZE_EITHER
) {
1714 /* Make the font the biggest we can */
1715 if (term
->cols
> width
)
1716 font_width
= (ss
.right
- ss
.left
- extra_width
)
1718 if (term
->rows
> height
)
1719 font_height
= (ss
.bottom
- ss
.top
- extra_height
)
1723 init_fonts(font_width
, font_height
);
1725 width
= (ss
.right
- ss
.left
- extra_width
) / font_width
;
1726 height
= (ss
.bottom
- ss
.top
- extra_height
) / font_height
;
1728 if ( height
> term
->rows
) height
= term
->rows
;
1729 if ( width
> term
->cols
) width
= term
->cols
;
1730 term_size(term
, height
, width
, cfg
.savelines
);
1731 #ifdef RDB_DEBUG_PATCH
1732 debug((27, "reset_window() -> term resize to (%d,%d)",
1738 SetWindowPos(hwnd
, NULL
, 0, 0,
1739 font_width
*term
->cols
+ extra_width
,
1740 font_height
*term
->rows
+ extra_height
,
1741 SWP_NOMOVE
| SWP_NOZORDER
);
1743 InvalidateRect(hwnd
, NULL
, TRUE
);
1744 #ifdef RDB_DEBUG_PATCH
1745 debug((27, "reset_window() -> window resize to (%d,%d)",
1746 font_width
*term
->cols
+ extra_width
,
1747 font_height
*term
->rows
+ extra_height
));
1753 /* We're allowed to or must change the font but do we want to ? */
1755 if (font_width
!= (win_width
-cfg
.window_border
*2)/term
->cols
||
1756 font_height
!= (win_height
-cfg
.window_border
*2)/term
->rows
) {
1759 init_fonts((win_width
-cfg
.window_border
*2)/term
->cols
,
1760 (win_height
-cfg
.window_border
*2)/term
->rows
);
1761 offset_width
= (win_width
-font_width
*term
->cols
)/2;
1762 offset_height
= (win_height
-font_height
*term
->rows
)/2;
1764 extra_width
= wr
.right
- wr
.left
- cr
.right
+ cr
.left
+offset_width
*2;
1765 extra_height
= wr
.bottom
- wr
.top
- cr
.bottom
+ cr
.top
+offset_height
*2;
1767 InvalidateRect(hwnd
, NULL
, TRUE
);
1768 #ifdef RDB_DEBUG_PATCH
1769 debug((25, "reset_window() -> font resize to (%d,%d)",
1770 font_width
, font_height
));
1775 static void set_input_locale(HKL kl
)
1779 GetLocaleInfo(LOWORD(kl
), LOCALE_IDEFAULTANSICODEPAGE
,
1780 lbuf
, sizeof(lbuf
));
1782 kbd_codepage
= atoi(lbuf
);
1785 static void click(Mouse_Button b
, int x
, int y
, int shift
, int ctrl
, int alt
)
1787 int thistime
= GetMessageTime();
1789 if (send_raw_mouse
&& !(cfg
.mouse_override
&& shift
)) {
1790 lastbtn
= MBT_NOTHING
;
1791 term_mouse(term
, b
, translate_button(b
), MA_CLICK
,
1792 x
, y
, shift
, ctrl
, alt
);
1796 if (lastbtn
== b
&& thistime
- lasttime
< dbltime
) {
1797 lastact
= (lastact
== MA_CLICK ? MA_2CLK
:
1798 lastact
== MA_2CLK ? MA_3CLK
:
1799 lastact
== MA_3CLK ? MA_CLICK
: MA_NOTHING
);
1804 if (lastact
!= MA_NOTHING
)
1805 term_mouse(term
, b
, translate_button(b
), lastact
,
1806 x
, y
, shift
, ctrl
, alt
);
1807 lasttime
= thistime
;
1811 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1812 * into a cooked one (SELECT, EXTEND, PASTE).
1814 static Mouse_Button
translate_button(Mouse_Button button
)
1816 if (button
== MBT_LEFT
)
1818 if (button
== MBT_MIDDLE
)
1819 return cfg
.mouse_is_xterm
== 1 ? MBT_PASTE
: MBT_EXTEND
;
1820 if (button
== MBT_RIGHT
)
1821 return cfg
.mouse_is_xterm
== 1 ? MBT_EXTEND
: MBT_PASTE
;
1822 return 0; /* shouldn't happen */
1825 static void show_mouseptr(int show
)
1827 /* NB that the counter in ShowCursor() is also frobbed by
1828 * update_mouse_pointer() */
1829 static int cursor_visible
= 1;
1830 if (!cfg
.hide_mouseptr
) /* override if this feature disabled */
1832 if (cursor_visible
&& !show
)
1834 else if (!cursor_visible
&& show
)
1836 cursor_visible
= show
;
1839 static int is_alt_pressed(void)
1842 int r
= GetKeyboardState(keystate
);
1845 if (keystate
[VK_MENU
] & 0x80)
1847 if (keystate
[VK_RMENU
] & 0x80)
1852 static int is_shift_pressed(void)
1855 int r
= GetKeyboardState(keystate
);
1858 if (keystate
[VK_SHIFT
] & 0x80)
1863 static int resizing
;
1865 void notify_remote_exit(void *fe
) { /* stub not needed in this frontend */ }
1867 void timer_change_notify(long next
)
1869 long ticks
= next
- GETTICKCOUNT();
1870 if (ticks
<= 0) ticks
= 1; /* just in case */
1871 KillTimer(hwnd
, TIMING_TIMER_ID
);
1872 SetTimer(hwnd
, TIMING_TIMER_ID
, ticks
, NULL
);
1873 timing_next_time
= next
;
1876 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT message
,
1877 WPARAM wParam
, LPARAM lParam
)
1880 static int ignore_clip
= FALSE
;
1881 static int need_backend_resize
= FALSE
;
1882 static int fullscr_on_max
= FALSE
;
1883 static UINT last_mousemove
= 0;
1887 if ((UINT_PTR
)wParam
== TIMING_TIMER_ID
) {
1890 KillTimer(hwnd
, TIMING_TIMER_ID
);
1891 if (run_timers(timing_next_time
, &next
)) {
1892 timer_change_notify(next
);
1903 str
= dupprintf("%s Exit Confirmation", appname
);
1904 if (!cfg
.warn_on_close
|| session_closed
||
1906 "Are you sure you want to close this session?",
1907 str
, MB_ICONWARNING
| MB_OKCANCEL
| MB_DEFBUTTON1
)
1909 DestroyWindow(hwnd
);
1917 case WM_INITMENUPOPUP
:
1918 if ((HMENU
)wParam
== savedsess_menu
) {
1919 /* About to pop up Saved Sessions sub-menu.
1920 * Refresh the session list. */
1921 get_sesslist(&sesslist
, FALSE
); /* free */
1922 get_sesslist(&sesslist
, TRUE
);
1923 update_savedsess_menu();
1929 switch (wParam
& ~0xF) { /* low 4 bits reserved to Windows */
1940 BOOL inherit_handles
;
1942 PROCESS_INFORMATION pi
;
1943 HANDLE filemap
= NULL
;
1945 if (wParam
== IDM_DUPSESS
) {
1947 * Allocate a file-mapping memory chunk for the
1950 SECURITY_ATTRIBUTES sa
;
1953 sa
.nLength
= sizeof(sa
);
1954 sa
.lpSecurityDescriptor
= NULL
;
1955 sa
.bInheritHandle
= TRUE
;
1956 filemap
= CreateFileMapping((HANDLE
) 0xFFFFFFFF,
1959 0, sizeof(Config
), NULL
);
1961 p
= (Config
*) MapViewOfFile(filemap
,
1963 0, 0, sizeof(Config
));
1965 *p
= cfg
; /* structure copy */
1969 inherit_handles
= TRUE
;
1970 sprintf(c
, "putty &%p", filemap
);
1972 } else if (wParam
== IDM_SAVEDSESS
) {
1973 unsigned int sessno
= ((lParam
- IDM_SAVED_MIN
)
1974 / MENU_SAVED_STEP
) + 1;
1975 if (sessno
< sesslist
.nsessions
) {
1976 char *session
= sesslist
.sessions
[sessno
];
1977 /* XXX spaces? quotes? "-load"? */
1978 cl
= dupprintf("putty @%s", session
);
1979 inherit_handles
= FALSE
;
1983 } else /* IDM_NEWSESS */ {
1985 inherit_handles
= FALSE
;
1988 GetModuleFileName(NULL
, b
, sizeof(b
) - 1);
1990 si
.lpReserved
= NULL
;
1991 si
.lpDesktop
= NULL
;
1995 si
.lpReserved2
= NULL
;
1996 CreateProcess(b
, cl
, NULL
, NULL
, inherit_handles
,
1997 NORMAL_PRIORITY_CLASS
, NULL
, NULL
, &si
, &pi
);
2000 CloseHandle(filemap
);
2007 logevent(NULL
, "----- Session restarted -----");
2008 term_pwron(term
, FALSE
);
2017 int reconfig_result
;
2022 reconfiguring
= TRUE
;
2024 GetWindowText(hwnd
, cfg
.wintitle
, sizeof(cfg
.wintitle
));
2028 do_reconfig(hwnd
, back ? back
->cfg_info(backhandle
) : 0);
2029 reconfiguring
= FALSE
;
2030 if (!reconfig_result
)
2034 /* Disable full-screen if resizing forbidden */
2035 HMENU m
= GetSystemMenu (hwnd
, FALSE
);
2036 EnableMenuItem(m
, IDM_FULLSCREEN
, MF_BYCOMMAND
|
2037 (cfg
.resize_action
== RESIZE_DISABLED
)
2038 ? MF_GRAYED
: MF_ENABLED
);
2039 /* Gracefully unzoom if necessary */
2040 if (IsZoomed(hwnd
) &&
2041 (cfg
.resize_action
== RESIZE_DISABLED
)) {
2042 ShowWindow(hwnd
, SW_RESTORE
);
2046 /* Pass new config data to the logging module */
2047 log_reconfig(logctx
, &cfg
);
2051 * Flush the line discipline's edit buffer in the
2052 * case where local editing has just been disabled.
2055 ldisc_send(ldisc
, NULL
, 0, 0);
2063 /* Pass new config data to the terminal */
2064 term_reconfig(term
, &cfg
);
2066 /* Pass new config data to the back end */
2068 back
->reconfig(backhandle
, &cfg
);
2070 /* Screen size changed ? */
2071 if (cfg
.height
!= prev_cfg
.height
||
2072 cfg
.width
!= prev_cfg
.width
||
2073 cfg
.savelines
!= prev_cfg
.savelines
||
2074 cfg
.resize_action
== RESIZE_FONT
||
2075 (cfg
.resize_action
== RESIZE_EITHER
&& IsZoomed(hwnd
)) ||
2076 cfg
.resize_action
== RESIZE_DISABLED
)
2077 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2079 /* Enable or disable the scroll bar, etc */
2081 LONG nflg
, flag
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
2082 LONG nexflag
, exflag
=
2083 GetWindowLongPtr(hwnd
, GWL_EXSTYLE
);
2086 if (cfg
.alwaysontop
!= prev_cfg
.alwaysontop
) {
2087 if (cfg
.alwaysontop
) {
2088 nexflag
|= WS_EX_TOPMOST
;
2089 SetWindowPos(hwnd
, HWND_TOPMOST
, 0, 0, 0, 0,
2090 SWP_NOMOVE
| SWP_NOSIZE
);
2092 nexflag
&= ~(WS_EX_TOPMOST
);
2093 SetWindowPos(hwnd
, HWND_NOTOPMOST
, 0, 0, 0, 0,
2094 SWP_NOMOVE
| SWP_NOSIZE
);
2097 if (cfg
.sunken_edge
)
2098 nexflag
|= WS_EX_CLIENTEDGE
;
2100 nexflag
&= ~(WS_EX_CLIENTEDGE
);
2103 if (is_full_screen() ?
2104 cfg
.scrollbar_in_fullscreen
: cfg
.scrollbar
)
2107 nflg
&= ~WS_VSCROLL
;
2109 if (cfg
.resize_action
== RESIZE_DISABLED
||
2111 nflg
&= ~WS_THICKFRAME
;
2113 nflg
|= WS_THICKFRAME
;
2115 if (cfg
.resize_action
== RESIZE_DISABLED
)
2116 nflg
&= ~WS_MAXIMIZEBOX
;
2118 nflg
|= WS_MAXIMIZEBOX
;
2120 if (nflg
!= flag
|| nexflag
!= exflag
) {
2122 SetWindowLongPtr(hwnd
, GWL_STYLE
, nflg
);
2123 if (nexflag
!= exflag
)
2124 SetWindowLongPtr(hwnd
, GWL_EXSTYLE
, nexflag
);
2126 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
2127 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
2128 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
2136 if (cfg
.resize_action
== RESIZE_DISABLED
&& IsZoomed(hwnd
)) {
2141 set_title(NULL
, cfg
.wintitle
);
2142 if (IsIconic(hwnd
)) {
2144 cfg
.win_name_always ? window_name
:
2148 if (strcmp(cfg
.font
.name
, prev_cfg
.font
.name
) != 0 ||
2149 strcmp(cfg
.line_codepage
, prev_cfg
.line_codepage
) != 0 ||
2150 cfg
.font
.isbold
!= prev_cfg
.font
.isbold
||
2151 cfg
.font
.height
!= prev_cfg
.font
.height
||
2152 cfg
.font
.charset
!= prev_cfg
.font
.charset
||
2153 cfg
.font_quality
!= prev_cfg
.font_quality
||
2154 cfg
.vtmode
!= prev_cfg
.vtmode
||
2155 cfg
.bold_colour
!= prev_cfg
.bold_colour
||
2156 cfg
.resize_action
== RESIZE_DISABLED
||
2157 cfg
.resize_action
== RESIZE_EITHER
||
2158 (cfg
.resize_action
!= prev_cfg
.resize_action
))
2161 InvalidateRect(hwnd
, NULL
, TRUE
);
2162 reset_window(init_lvl
);
2163 net_pending_errors();
2170 term_do_paste(term
);
2176 term_pwron(term
, TRUE
);
2178 ldisc_send(ldisc
, NULL
, 0, 0);
2184 WinHelp(hwnd
, help_path
,
2185 help_has_contents ? HELP_FINDER
: HELP_CONTENTS
, 0);
2189 * We get this if the System menu has been activated
2196 * We get this if the System menu has been activated
2197 * using the keyboard. This might happen from within
2198 * TranslateKey, in which case it really wants to be
2199 * followed by a `space' character to actually _bring
2200 * the menu up_ rather than just sitting there in
2201 * `ready to appear' state.
2203 show_mouseptr(1); /* make sure pointer is visible */
2205 PostMessage(hwnd
, WM_CHAR
, ' ', 0);
2207 case IDM_FULLSCREEN
:
2211 if (wParam
>= IDM_SAVED_MIN
&& wParam
< IDM_SAVED_MAX
) {
2212 SendMessage(hwnd
, WM_SYSCOMMAND
, IDM_SAVEDSESS
, wParam
);
2214 if (wParam
>= IDM_SPECIAL_MIN
&& wParam
<= IDM_SPECIAL_MAX
) {
2215 int i
= (wParam
- IDM_SPECIAL_MIN
) / 0x10;
2217 * Ensure we haven't been sent a bogus SYSCOMMAND
2218 * which would cause us to reference invalid memory
2219 * and crash. Perhaps I'm just too paranoid here.
2221 if (i
>= n_specials
)
2224 back
->special(backhandle
, specials
[i
].code
);
2225 net_pending_errors();
2230 #define X_POS(l) ((int)(short)LOWORD(l))
2231 #define Y_POS(l) ((int)(short)HIWORD(l))
2233 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2234 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2235 case WM_LBUTTONDOWN
:
2236 case WM_MBUTTONDOWN
:
2237 case WM_RBUTTONDOWN
:
2241 if (message
== WM_RBUTTONDOWN
&&
2242 ((wParam
& MK_CONTROL
) || (cfg
.mouse_is_xterm
== 2))) {
2245 show_mouseptr(1); /* make sure pointer is visible */
2246 GetCursorPos(&cursorpos
);
2247 TrackPopupMenu(popup_menus
[CTXMENU
].menu
,
2248 TPM_LEFTALIGN
| TPM_TOPALIGN
| TPM_RIGHTBUTTON
,
2249 cursorpos
.x
, cursorpos
.y
,
2257 case WM_LBUTTONDOWN
:
2261 case WM_MBUTTONDOWN
:
2262 button
= MBT_MIDDLE
;
2265 case WM_RBUTTONDOWN
:
2274 button
= MBT_MIDDLE
;
2282 button
= press
= 0; /* shouldn't happen */
2286 * Special case: in full-screen mode, if the left
2287 * button is clicked in the very top left corner of the
2288 * window, we put up the System menu instead of doing
2292 char mouse_on_hotspot
= 0;
2301 mon
= MonitorFromPoint(pt
, MONITOR_DEFAULTTONULL
);
2304 mi
.cbSize
= sizeof(MONITORINFO
);
2305 GetMonitorInfo(mon
, &mi
);
2307 if (mi
.rcMonitor
.left
== pt
.x
&&
2308 mi
.rcMonitor
.top
== pt
.y
) {
2309 mouse_on_hotspot
= 1;
2314 if (pt
.x
== 0 && pt
.y
== 0) {
2315 mouse_on_hotspot
= 1;
2318 if (is_full_screen() && press
&&
2319 button
== MBT_LEFT
&& mouse_on_hotspot
) {
2320 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_MOUSEMENU
,
2321 MAKELPARAM(pt
.x
, pt
.y
));
2328 TO_CHR_X(X_POS(lParam
)), TO_CHR_Y(Y_POS(lParam
)),
2329 wParam
& MK_SHIFT
, wParam
& MK_CONTROL
,
2333 term_mouse(term
, button
, translate_button(button
), MA_RELEASE
,
2334 TO_CHR_X(X_POS(lParam
)),
2335 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2336 wParam
& MK_CONTROL
, is_alt_pressed());
2344 * Windows seems to like to occasionally send MOUSEMOVE
2345 * events even if the mouse hasn't moved. Don't unhide
2346 * the mouse pointer in this case.
2348 static WPARAM wp
= 0;
2349 static LPARAM lp
= 0;
2350 if (wParam
!= wp
|| lParam
!= lp
||
2351 last_mousemove
!= WM_MOUSEMOVE
) {
2353 wp
= wParam
; lp
= lParam
;
2354 last_mousemove
= WM_MOUSEMOVE
;
2358 * Add the mouse position and message time to the random
2361 noise_ultralight(lParam
);
2363 if (wParam
& (MK_LBUTTON
| MK_MBUTTON
| MK_RBUTTON
) &&
2364 GetCapture() == hwnd
) {
2366 if (wParam
& MK_LBUTTON
)
2368 else if (wParam
& MK_MBUTTON
)
2372 term_mouse(term
, b
, translate_button(b
), MA_DRAG
,
2373 TO_CHR_X(X_POS(lParam
)),
2374 TO_CHR_Y(Y_POS(lParam
)), wParam
& MK_SHIFT
,
2375 wParam
& MK_CONTROL
, is_alt_pressed());
2378 case WM_NCMOUSEMOVE
:
2380 static WPARAM wp
= 0;
2381 static LPARAM lp
= 0;
2382 if (wParam
!= wp
|| lParam
!= lp
||
2383 last_mousemove
!= WM_NCMOUSEMOVE
) {
2385 wp
= wParam
; lp
= lParam
;
2386 last_mousemove
= WM_NCMOUSEMOVE
;
2389 noise_ultralight(lParam
);
2391 case WM_IGNORE_CLIP
:
2392 ignore_clip
= wParam
; /* don't panic on DESTROYCLIPBOARD */
2394 case WM_DESTROYCLIPBOARD
:
2396 term_deselect(term
);
2397 ignore_clip
= FALSE
;
2404 hdc
= BeginPaint(hwnd
, &p
);
2406 SelectPalette(hdc
, pal
, TRUE
);
2407 RealizePalette(hdc
);
2411 * We have to be careful about term_paint(). It will
2412 * set a bunch of character cells to INVALID and then
2413 * call do_paint(), which will redraw those cells and
2414 * _then mark them as done_. This may not be accurate:
2415 * when painting in WM_PAINT context we are restricted
2416 * to the rectangle which has just been exposed - so if
2417 * that only covers _part_ of a character cell and the
2418 * rest of it was already visible, that remainder will
2419 * not be redrawn at all. Accordingly, we must not
2420 * paint any character cell in a WM_PAINT context which
2421 * already has a pending update due to terminal output.
2422 * The simplest solution to this - and many, many
2423 * thanks to Hung-Te Lin for working all this out - is
2424 * not to do any actual painting at _all_ if there's a
2425 * pending terminal update: just mark the relevant
2426 * character cells as INVALID and wait for the
2427 * scheduled full update to sort it out.
2429 * I have a suspicion this isn't the _right_ solution.
2430 * An alternative approach would be to have terminal.c
2431 * separately track what _should_ be on the terminal
2432 * screen and what _is_ on the terminal screen, and
2433 * have two completely different types of redraw (one
2434 * for full updates, which syncs the former with the
2435 * terminal itself, and one for WM_PAINT which syncs
2436 * the latter with the former); yet another possibility
2437 * would be to have the Windows front end do what the
2438 * GTK one already does, and maintain a bitmap of the
2439 * current terminal appearance so that WM_PAINT becomes
2440 * completely trivial. However, this should do for now.
2442 term_paint(term
, hdc
,
2443 (p
.rcPaint
.left
-offset_width
)/font_width
,
2444 (p
.rcPaint
.top
-offset_height
)/font_height
,
2445 (p
.rcPaint
.right
-offset_width
-1)/font_width
,
2446 (p
.rcPaint
.bottom
-offset_height
-1)/font_height
,
2447 !term
->window_update_pending
);
2450 p
.rcPaint
.left
< offset_width
||
2451 p
.rcPaint
.top
< offset_height
||
2452 p
.rcPaint
.right
>= offset_width
+ font_width
*term
->cols
||
2453 p
.rcPaint
.bottom
>= offset_height
+ font_height
*term
->rows
)
2455 HBRUSH fillcolour
, oldbrush
;
2457 fillcolour
= CreateSolidBrush (
2458 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2459 oldbrush
= SelectObject(hdc
, fillcolour
);
2460 edge
= CreatePen(PS_SOLID
, 0,
2461 colours
[ATTR_DEFBG
>>ATTR_BGSHIFT
]);
2462 oldpen
= SelectObject(hdc
, edge
);
2465 * Jordan Russell reports that this apparently
2466 * ineffectual IntersectClipRect() call masks a
2467 * Windows NT/2K bug causing strange display
2468 * problems when the PuTTY window is taller than
2469 * the primary monitor. It seems harmless enough...
2471 IntersectClipRect(hdc
,
2472 p
.rcPaint
.left
, p
.rcPaint
.top
,
2473 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2475 ExcludeClipRect(hdc
,
2476 offset_width
, offset_height
,
2477 offset_width
+font_width
*term
->cols
,
2478 offset_height
+font_height
*term
->rows
);
2480 Rectangle(hdc
, p
.rcPaint
.left
, p
.rcPaint
.top
,
2481 p
.rcPaint
.right
, p
.rcPaint
.bottom
);
2483 /* SelectClipRgn(hdc, NULL); */
2485 SelectObject(hdc
, oldbrush
);
2486 DeleteObject(fillcolour
);
2487 SelectObject(hdc
, oldpen
);
2490 SelectObject(hdc
, GetStockObject(SYSTEM_FONT
));
2491 SelectObject(hdc
, GetStockObject(WHITE_PEN
));
2497 enact_netevent(wParam
, lParam
);
2498 net_pending_errors();
2501 term_set_focus(term
, TRUE
);
2502 CreateCaret(hwnd
, caretbm
, font_width
, font_height
);
2504 flash_window(0); /* stop */
2510 term_set_focus(term
, FALSE
);
2512 caret_x
= caret_y
= -1; /* ensure caret is replaced next time */
2515 case WM_ENTERSIZEMOVE
:
2516 #ifdef RDB_DEBUG_PATCH
2517 debug((27, "WM_ENTERSIZEMOVE"));
2521 need_backend_resize
= FALSE
;
2523 case WM_EXITSIZEMOVE
:
2526 #ifdef RDB_DEBUG_PATCH
2527 debug((27, "WM_EXITSIZEMOVE"));
2529 if (need_backend_resize
) {
2530 term_size(term
, cfg
.height
, cfg
.width
, cfg
.savelines
);
2531 InvalidateRect(hwnd
, NULL
, TRUE
);
2536 * This does two jobs:
2537 * 1) Keep the sizetip uptodate
2538 * 2) Make sure the window size is _stepped_ in units of the font size.
2540 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2541 int width
, height
, w
, h
, ew
, eh
;
2542 LPRECT r
= (LPRECT
) lParam
;
2544 if ( !need_backend_resize
&& cfg
.resize_action
== RESIZE_EITHER
&&
2545 (cfg
.height
!= term
->rows
|| cfg
.width
!= term
->cols
)) {
2547 * Great! It seems that both the terminal size and the
2548 * font size have been changed and the user is now dragging.
2550 * It will now be difficult to get back to the configured
2553 * This would be easier but it seems to be too confusing.
2555 term_size(term, cfg.height, cfg.width, cfg.savelines);
2558 cfg
.height
=term
->rows
; cfg
.width
=term
->cols
;
2560 InvalidateRect(hwnd
, NULL
, TRUE
);
2561 need_backend_resize
= TRUE
;
2564 width
= r
->right
- r
->left
- extra_width
;
2565 height
= r
->bottom
- r
->top
- extra_height
;
2566 w
= (width
+ font_width
/ 2) / font_width
;
2569 h
= (height
+ font_height
/ 2) / font_height
;
2572 UpdateSizeTip(hwnd
, w
, h
);
2573 ew
= width
- w
* font_width
;
2574 eh
= height
- h
* font_height
;
2576 if (wParam
== WMSZ_LEFT
||
2577 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2583 if (wParam
== WMSZ_TOP
||
2584 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2594 int width
, height
, w
, h
, rv
= 0;
2595 int ex_width
= extra_width
+ (cfg
.window_border
- offset_width
) * 2;
2596 int ex_height
= extra_height
+ (cfg
.window_border
- offset_height
) * 2;
2597 LPRECT r
= (LPRECT
) lParam
;
2599 width
= r
->right
- r
->left
- ex_width
;
2600 height
= r
->bottom
- r
->top
- ex_height
;
2602 w
= (width
+ term
->cols
/2)/term
->cols
;
2603 h
= (height
+ term
->rows
/2)/term
->rows
;
2604 if ( r
->right
!= r
->left
+ w
*term
->cols
+ ex_width
)
2607 if (wParam
== WMSZ_LEFT
||
2608 wParam
== WMSZ_BOTTOMLEFT
|| wParam
== WMSZ_TOPLEFT
)
2609 r
->left
= r
->right
- w
*term
->cols
- ex_width
;
2611 r
->right
= r
->left
+ w
*term
->cols
+ ex_width
;
2613 if (r
->bottom
!= r
->top
+ h
*term
->rows
+ ex_height
)
2616 if (wParam
== WMSZ_TOP
||
2617 wParam
== WMSZ_TOPRIGHT
|| wParam
== WMSZ_TOPLEFT
)
2618 r
->top
= r
->bottom
- h
*term
->rows
- ex_height
;
2620 r
->bottom
= r
->top
+ h
*term
->rows
+ ex_height
;
2624 /* break; (never reached) */
2625 case WM_FULLSCR_ON_MAX
:
2626 fullscr_on_max
= TRUE
;
2629 sys_cursor_update();
2632 #ifdef RDB_DEBUG_PATCH
2633 debug((27, "WM_SIZE %s (%d,%d)",
2634 (wParam
== SIZE_MINIMIZED
) ?
"SIZE_MINIMIZED":
2635 (wParam
== SIZE_MAXIMIZED
) ?
"SIZE_MAXIMIZED":
2636 (wParam
== SIZE_RESTORED
&& resizing
) ?
"to":
2637 (wParam
== SIZE_RESTORED
) ?
"SIZE_RESTORED":
2639 LOWORD(lParam
), HIWORD(lParam
)));
2641 if (wParam
== SIZE_MINIMIZED
)
2643 cfg
.win_name_always ? window_name
: icon_name
);
2644 if (wParam
== SIZE_RESTORED
|| wParam
== SIZE_MAXIMIZED
)
2645 SetWindowText(hwnd
, window_name
);
2646 if (wParam
== SIZE_RESTORED
)
2647 clear_full_screen();
2648 if (wParam
== SIZE_MAXIMIZED
&& fullscr_on_max
) {
2649 fullscr_on_max
= FALSE
;
2653 if (cfg
.resize_action
== RESIZE_DISABLED
) {
2654 /* A resize, well it better be a minimize. */
2658 int width
, height
, w
, h
;
2660 width
= LOWORD(lParam
);
2661 height
= HIWORD(lParam
);
2664 if (wParam
== SIZE_MAXIMIZED
&& !was_zoomed
) {
2666 prev_rows
= term
->rows
;
2667 prev_cols
= term
->cols
;
2668 if (cfg
.resize_action
== RESIZE_TERM
) {
2669 w
= width
/ font_width
;
2671 h
= height
/ font_height
;
2674 term_size(term
, h
, w
, cfg
.savelines
);
2677 } else if (wParam
== SIZE_RESTORED
&& was_zoomed
) {
2679 if (cfg
.resize_action
== RESIZE_TERM
)
2680 term_size(term
, prev_rows
, prev_cols
, cfg
.savelines
);
2681 if (cfg
.resize_action
!= RESIZE_FONT
)
2686 /* This is an unexpected resize, these will normally happen
2687 * if the window is too large. Probably either the user
2688 * selected a huge font or the screen size has changed.
2690 * This is also called with minimize.
2692 else reset_window(-1);
2696 * Don't call back->size in mid-resize. (To prevent
2697 * massive numbers of resize events getting sent
2698 * down the connection during an NT opaque drag.)
2701 if (cfg
.resize_action
!= RESIZE_FONT
&& !is_alt_pressed()) {
2702 need_backend_resize
= TRUE
;
2703 w
= (width
-cfg
.window_border
*2) / font_width
;
2705 h
= (height
-cfg
.window_border
*2) / font_height
;
2714 sys_cursor_update();
2717 switch (LOWORD(wParam
)) {
2719 term_scroll(term
, -1, 0);
2722 term_scroll(term
, +1, 0);
2725 term_scroll(term
, 0, +1);
2728 term_scroll(term
, 0, -1);
2731 term_scroll(term
, 0, +term
->rows
/ 2);
2734 term_scroll(term
, 0, -term
->rows
/ 2);
2736 case SB_THUMBPOSITION
:
2738 term_scroll(term
, 1, HIWORD(wParam
));
2742 case WM_PALETTECHANGED
:
2743 if ((HWND
) wParam
!= hwnd
&& pal
!= NULL
) {
2744 HDC hdc
= get_ctx(NULL
);
2746 if (RealizePalette(hdc
) > 0)
2752 case WM_QUERYNEWPALETTE
:
2754 HDC hdc
= get_ctx(NULL
);
2756 if (RealizePalette(hdc
) > 0)
2768 * Add the scan code and keypress timing to the random
2771 noise_ultralight(lParam
);
2774 * We don't do TranslateMessage since it disassociates the
2775 * resulting CHAR message from the KEYDOWN that sparked it,
2776 * which we occasionally don't want. Instead, we process
2777 * KEYDOWN, and call the Win32 translator functions so that
2778 * we get the translations under _our_ control.
2781 unsigned char buf
[20];
2784 if (wParam
== VK_PROCESSKEY
) { /* IME PROCESS key */
2785 if (message
== WM_KEYDOWN
) {
2788 m
.message
= WM_KEYDOWN
;
2790 m
.lParam
= lParam
& 0xdfff;
2791 TranslateMessage(&m
);
2792 } else break; /* pass to Windows for default processing */
2794 len
= TranslateKey(message
, wParam
, lParam
, buf
);
2796 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2800 * Interrupt an ongoing paste. I'm not sure
2801 * this is sensible, but for the moment it's
2802 * preferable to having to faff about buffering
2808 * We need not bother about stdin backlogs
2809 * here, because in GUI PuTTY we can't do
2810 * anything about it anyway; there's no means
2811 * of asking Windows to hold off on KEYDOWN
2812 * messages. We _have_ to buffer everything
2815 term_seen_key_event(term
);
2817 ldisc_send(ldisc
, buf
, len
, 1);
2822 net_pending_errors();
2824 case WM_INPUTLANGCHANGE
:
2825 /* wParam == Font number */
2826 /* lParam == Locale */
2827 set_input_locale((HKL
)lParam
);
2828 sys_cursor_update();
2830 case WM_IME_STARTCOMPOSITION
:
2832 HIMC hImc
= ImmGetContext(hwnd
);
2833 ImmSetCompositionFont(hImc
, &lfont
);
2834 ImmReleaseContext(hwnd
, hImc
);
2837 case WM_IME_COMPOSITION
:
2843 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
||
2844 osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) break; /* no Unicode */
2846 if ((lParam
& GCS_RESULTSTR
) == 0) /* Composition unfinished. */
2847 break; /* fall back to DefWindowProc */
2849 hIMC
= ImmGetContext(hwnd
);
2850 n
= ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, NULL
, 0);
2854 buff
= snewn(n
, char);
2855 ImmGetCompositionStringW(hIMC
, GCS_RESULTSTR
, buff
, n
);
2857 * Jaeyoun Chung reports that Korean character
2858 * input doesn't work correctly if we do a single
2859 * luni_send() covering the whole of buff. So
2860 * instead we luni_send the characters one by one.
2862 term_seen_key_event(term
);
2863 for (i
= 0; i
< n
; i
+= 2) {
2865 luni_send(ldisc
, (unsigned short *)(buff
+i
), 1, 1);
2869 ImmReleaseContext(hwnd
, hIMC
);
2874 if (wParam
& 0xFF00) {
2875 unsigned char buf
[2];
2878 buf
[0] = wParam
>> 8;
2879 term_seen_key_event(term
);
2881 lpage_send(ldisc
, kbd_codepage
, buf
, 2, 1);
2883 char c
= (unsigned char) wParam
;
2884 term_seen_key_event(term
);
2886 lpage_send(ldisc
, kbd_codepage
, &c
, 1, 1);
2892 * Nevertheless, we are prepared to deal with WM_CHAR
2893 * messages, should they crop up. So if someone wants to
2894 * post the things to us as part of a macro manoeuvre,
2895 * we're ready to cope.
2898 char c
= (unsigned char)wParam
;
2899 term_seen_key_event(term
);
2901 lpage_send(ldisc
, CP_ACP
, &c
, 1, 1);
2904 case WM_SYSCOLORCHANGE
:
2905 if (cfg
.system_colour
) {
2906 /* Refresh palette from system colours. */
2907 /* XXX actually this zaps the entire palette. */
2910 /* Force a repaint of the terminal window. */
2911 term_invalidate(term
);
2914 case WM_AGENT_CALLBACK
:
2916 struct agent_callback
*c
= (struct agent_callback
*)lParam
;
2917 c
->callback(c
->callback_ctx
, c
->data
, c
->len
);
2922 if (message
== wm_mousewheel
|| message
== WM_MOUSEWHEEL
) {
2923 int shift_pressed
=0, control_pressed
=0;
2925 if (message
== WM_MOUSEWHEEL
) {
2926 wheel_accumulator
+= (short)HIWORD(wParam
);
2927 shift_pressed
=LOWORD(wParam
) & MK_SHIFT
;
2928 control_pressed
=LOWORD(wParam
) & MK_CONTROL
;
2931 wheel_accumulator
+= (int)wParam
;
2932 if (GetKeyboardState(keys
)!=0) {
2933 shift_pressed
=keys
[VK_SHIFT
]&0x80;
2934 control_pressed
=keys
[VK_CONTROL
]&0x80;
2938 /* process events when the threshold is reached */
2939 while (abs(wheel_accumulator
) >= WHEEL_DELTA
) {
2942 /* reduce amount for next time */
2943 if (wheel_accumulator
> 0) {
2945 wheel_accumulator
-= WHEEL_DELTA
;
2946 } else if (wheel_accumulator
< 0) {
2948 wheel_accumulator
+= WHEEL_DELTA
;
2952 if (send_raw_mouse
&&
2953 !(cfg
.mouse_override
&& shift_pressed
)) {
2954 /* send a mouse-down followed by a mouse up */
2955 term_mouse(term
, b
, translate_button(b
),
2957 TO_CHR_X(X_POS(lParam
)),
2958 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2959 control_pressed
, is_alt_pressed());
2960 term_mouse(term
, b
, translate_button(b
),
2961 MA_RELEASE
, TO_CHR_X(X_POS(lParam
)),
2962 TO_CHR_Y(Y_POS(lParam
)), shift_pressed
,
2963 control_pressed
, is_alt_pressed());
2965 /* trigger a scroll */
2966 term_scroll(term
, 0,
2968 -term
->rows
/ 2 : term
->rows
/ 2);
2976 * Any messages we don't process completely above are passed through to
2977 * DefWindowProc() for default processing.
2979 return DefWindowProc(hwnd
, message
, wParam
, lParam
);
2983 * Move the system caret. (We maintain one, even though it's
2984 * invisible, for the benefit of blind people: apparently some
2985 * helper software tracks the system caret, so we should arrange to
2988 void sys_cursor(void *frontend
, int x
, int y
)
2992 if (!term
->has_focus
) return;
2995 * Avoid gratuitously re-updating the cursor position and IMM
2996 * window if there's no actual change required.
2998 cx
= x
* font_width
+ offset_width
;
2999 cy
= y
* font_height
+ offset_height
;
3000 if (cx
== caret_x
&& cy
== caret_y
)
3005 sys_cursor_update();
3008 static void sys_cursor_update(void)
3013 if (!term
->has_focus
) return;
3015 if (caret_x
< 0 || caret_y
< 0)
3018 SetCaretPos(caret_x
, caret_y
);
3020 /* IMM calls on Win98 and beyond only */
3021 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32s
) return; /* 3.11 */
3023 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_WINDOWS
&&
3024 osVersion
.dwMinorVersion
== 0) return; /* 95 */
3026 /* we should have the IMM functions */
3027 hIMC
= ImmGetContext(hwnd
);
3028 cf
.dwStyle
= CFS_POINT
;
3029 cf
.ptCurrentPos
.x
= caret_x
;
3030 cf
.ptCurrentPos
.y
= caret_y
;
3031 ImmSetCompositionWindow(hIMC
, &cf
);
3033 ImmReleaseContext(hwnd
, hIMC
);
3037 * Draw a line of text in the window, at given character
3038 * coordinates, in given attributes.
3040 * We are allowed to fiddle with the contents of `text'.
3042 void do_text_internal(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3043 unsigned long attr
, int lattr
)
3046 int nfg
, nbg
, nfont
;
3049 int force_manual_underline
= 0;
3050 int fnt_width
, char_width
;
3051 int text_adjust
= 0;
3052 static int *IpDx
= 0, IpDxLEN
= 0;
3054 lattr
&= LATTR_MODE
;
3056 char_width
= fnt_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3058 if (attr
& ATTR_WIDE
)
3061 if (len
> IpDxLEN
|| IpDx
[0] != char_width
) {
3063 if (len
> IpDxLEN
) {
3065 IpDx
= snewn(len
+ 16, int);
3066 IpDxLEN
= (len
+ 16);
3068 for (i
= 0; i
< IpDxLEN
; i
++)
3069 IpDx
[i
] = char_width
;
3072 /* Only want the left half of double width lines */
3073 if (lattr
!= LATTR_NORM
&& x
*2 >= term
->cols
)
3081 if ((attr
& TATTR_ACTCURS
) && (cfg
.cursor_type
== 0 || term
->big_cursor
)) {
3082 attr
&= ~(ATTR_REVERSE
|ATTR_BLINK
|ATTR_COLOURS
);
3083 if (bold_mode
== BOLD_COLOURS
)
3086 /* cursor fg and bg */
3087 attr
|= (260 << ATTR_FGSHIFT
) | (261 << ATTR_BGSHIFT
);
3091 if (cfg
.vtmode
== VT_POORMAN
&& lattr
!= LATTR_NORM
) {
3092 /* Assume a poorman font is borken in other ways too. */
3102 nfont
|= FONT_WIDE
+ FONT_HIGH
;
3105 if (attr
& ATTR_NARROW
)
3106 nfont
|= FONT_NARROW
;
3108 /* Special hack for the VT100 linedraw glyphs. */
3109 if (text
[0] >= 0x23BA && text
[0] <= 0x23BD) {
3110 switch ((unsigned char) (text
[0])) {
3112 text_adjust
= -2 * font_height
/ 5;
3115 text_adjust
= -1 * font_height
/ 5;
3118 text_adjust
= font_height
/ 5;
3121 text_adjust
= 2 * font_height
/ 5;
3124 if (lattr
== LATTR_TOP
|| lattr
== LATTR_BOT
)
3126 text
[0] = ucsdata
.unitab_xterm
['q'];
3127 if (attr
& ATTR_UNDER
) {
3128 attr
&= ~ATTR_UNDER
;
3129 force_manual_underline
= 1;
3133 /* Anything left as an original character set is unprintable. */
3134 if (DIRECT_CHAR(text
[0])) {
3136 for (i
= 0; i
< len
; i
++)
3141 if ((text
[0] & CSET_MASK
) == CSET_OEMCP
)
3144 nfg
= ((attr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
);
3145 nbg
= ((attr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
);
3146 if (bold_mode
== BOLD_FONT
&& (attr
& ATTR_BOLD
))
3148 if (und_mode
== UND_FONT
&& (attr
& ATTR_UNDER
))
3149 nfont
|= FONT_UNDERLINE
;
3150 another_font(nfont
);
3151 if (!fonts
[nfont
]) {
3152 if (nfont
& FONT_UNDERLINE
)
3153 force_manual_underline
= 1;
3154 /* Don't do the same for manual bold, it could be bad news. */
3156 nfont
&= ~(FONT_BOLD
| FONT_UNDERLINE
);
3158 another_font(nfont
);
3160 nfont
= FONT_NORMAL
;
3161 if (attr
& ATTR_REVERSE
) {
3166 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BOLD
)) {
3167 if (nfg
< 16) nfg
|= 8;
3168 else if (nfg
>= 256) nfg
|= 1;
3170 if (bold_mode
== BOLD_COLOURS
&& (attr
& ATTR_BLINK
)) {
3171 if (nbg
< 16) nbg
|= 8;
3172 else if (nbg
>= 256) nbg
|= 1;
3176 SelectObject(hdc
, fonts
[nfont
]);
3177 SetTextColor(hdc
, fg
);
3178 SetBkColor(hdc
, bg
);
3179 if (attr
& TATTR_COMBINING
)
3180 SetBkMode(hdc
, TRANSPARENT
);
3182 SetBkMode(hdc
, OPAQUE
);
3185 line_box
.right
= x
+ char_width
* len
;
3186 line_box
.bottom
= y
+ font_height
;
3188 /* Only want the left half of double width lines */
3189 if (line_box
.right
> font_width
*term
->cols
+offset_width
)
3190 line_box
.right
= font_width
*term
->cols
+offset_width
;
3192 /* We're using a private area for direct to font. (512 chars.) */
3193 if (ucsdata
.dbcs_screenfont
&& (text
[0] & CSET_MASK
) == CSET_ACP
) {
3194 /* Ho Hum, dbcs fonts are a PITA! */
3195 /* To display on W9x I have to convert to UCS */
3196 static wchar_t *uni_buf
= 0;
3197 static int uni_len
= 0;
3199 if (len
> uni_len
) {
3202 uni_buf
= snewn(uni_len
, wchar_t);
3205 for(nlen
= mptr
= 0; mptr
<len
; mptr
++) {
3206 uni_buf
[nlen
] = 0xFFFD;
3207 if (IsDBCSLeadByteEx(ucsdata
.font_codepage
, (BYTE
) text
[mptr
])) {
3209 dbcstext
[0] = text
[mptr
] & 0xFF;
3210 dbcstext
[1] = text
[mptr
+1] & 0xFF;
3211 IpDx
[nlen
] += char_width
;
3212 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3213 dbcstext
, 2, uni_buf
+nlen
, 1);
3219 dbcstext
[0] = text
[mptr
] & 0xFF;
3220 MultiByteToWideChar(ucsdata
.font_codepage
, MB_USEGLYPHCHARS
,
3221 dbcstext
, 1, uni_buf
+nlen
, 1);
3229 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3230 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, uni_buf
, nlen
, IpDx
);
3231 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3232 SetBkMode(hdc
, TRANSPARENT
);
3233 ExtTextOutW(hdc
, x
- 1,
3234 y
- font_height
* (lattr
==
3235 LATTR_BOT
) + text_adjust
,
3236 ETO_CLIPPED
, &line_box
, uni_buf
, nlen
, IpDx
);
3240 } else if (DIRECT_FONT(text
[0])) {
3241 static char *directbuf
= NULL
;
3242 static int directlen
= 0;
3244 if (len
> directlen
) {
3246 directbuf
= sresize(directbuf
, directlen
, char);
3249 for (i
= 0; i
< len
; i
++)
3250 directbuf
[i
] = text
[i
] & 0xFF;
3253 y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3254 ETO_CLIPPED
| ETO_OPAQUE
, &line_box
, directbuf
, len
, IpDx
);
3255 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3256 SetBkMode(hdc
, TRANSPARENT
);
3258 /* GRR: This draws the character outside it's box and can leave
3259 * 'droppings' even with the clip box! I suppose I could loop it
3260 * one character at a time ... yuk.
3262 * Or ... I could do a test print with "W", and use +1 or -1 for this
3263 * shift depending on if the leftmost column is blank...
3265 ExtTextOut(hdc
, x
- 1,
3266 y
- font_height
* (lattr
==
3267 LATTR_BOT
) + text_adjust
,
3268 ETO_CLIPPED
, &line_box
, directbuf
, len
, IpDx
);
3271 /* And 'normal' unicode characters */
3272 static WCHAR
*wbuf
= NULL
;
3273 static int wlen
= 0;
3279 wbuf
= snewn(wlen
, WCHAR
);
3282 for (i
= 0; i
< len
; i
++)
3285 /* print Glyphs as they are, without Windows' Shaping*/
3286 exact_textout(hdc
, x
, y
- font_height
* (lattr
== LATTR_BOT
) + text_adjust
,
3287 &line_box
, wbuf
, len
, IpDx
, !(attr
& TATTR_COMBINING
));
3288 /* ExtTextOutW(hdc, x,
3289 y - font_height * (lattr == LATTR_BOT) + text_adjust,
3290 ETO_CLIPPED | ETO_OPAQUE, &line_box, wbuf, len, IpDx);
3293 /* And the shadow bold hack. */
3294 if (bold_mode
== BOLD_SHADOW
&& (attr
& ATTR_BOLD
)) {
3295 SetBkMode(hdc
, TRANSPARENT
);
3296 ExtTextOutW(hdc
, x
- 1,
3297 y
- font_height
* (lattr
==
3298 LATTR_BOT
) + text_adjust
,
3299 ETO_CLIPPED
, &line_box
, wbuf
, len
, IpDx
);
3302 if (lattr
!= LATTR_TOP
&& (force_manual_underline
||
3303 (und_mode
== UND_LINE
3304 && (attr
& ATTR_UNDER
)))) {
3307 if (lattr
== LATTR_BOT
)
3308 dec
= dec
* 2 - font_height
;
3310 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, fg
));
3311 MoveToEx(hdc
, x
, y
+ dec
, NULL
);
3312 LineTo(hdc
, x
+ len
* char_width
, y
+ dec
);
3313 oldpen
= SelectObject(hdc
, oldpen
);
3314 DeleteObject(oldpen
);
3319 * Wrapper that handles combining characters.
3321 void do_text(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3322 unsigned long attr
, int lattr
)
3324 if (attr
& TATTR_COMBINING
) {
3325 unsigned long a
= 0;
3326 attr
&= ~TATTR_COMBINING
;
3328 do_text_internal(ctx
, x
, y
, text
, 1, attr
| a
, lattr
);
3330 a
= TATTR_COMBINING
;
3333 do_text_internal(ctx
, x
, y
, text
, len
, attr
, lattr
);
3336 void do_cursor(Context ctx
, int x
, int y
, wchar_t *text
, int len
,
3337 unsigned long attr
, int lattr
)
3343 int ctype
= cfg
.cursor_type
;
3345 lattr
&= LATTR_MODE
;
3347 if ((attr
& TATTR_ACTCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3348 if (*text
!= UCSWIDE
) {
3349 do_text(ctx
, x
, y
, text
, len
, attr
, lattr
);
3353 attr
|= TATTR_RIGHTCURS
;
3356 fnt_width
= char_width
= font_width
* (1 + (lattr
!= LATTR_NORM
));
3357 if (attr
& ATTR_WIDE
)
3364 if ((attr
& TATTR_PASCURS
) && (ctype
== 0 || term
->big_cursor
)) {
3367 pts
[0].x
= pts
[1].x
= pts
[4].x
= x
;
3368 pts
[2].x
= pts
[3].x
= x
+ char_width
- 1;
3369 pts
[0].y
= pts
[3].y
= pts
[4].y
= y
;
3370 pts
[1].y
= pts
[2].y
= y
+ font_height
- 1;
3371 oldpen
= SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3372 Polyline(hdc
, pts
, 5);
3373 oldpen
= SelectObject(hdc
, oldpen
);
3374 DeleteObject(oldpen
);
3375 } else if ((attr
& (TATTR_ACTCURS
| TATTR_PASCURS
)) && ctype
!= 0) {
3376 int startx
, starty
, dx
, dy
, length
, i
;
3379 starty
= y
+ descent
;
3382 length
= char_width
;
3385 if (attr
& TATTR_RIGHTCURS
)
3386 xadjust
= char_width
- 1;
3387 startx
= x
+ xadjust
;
3391 length
= font_height
;
3393 if (attr
& TATTR_ACTCURS
) {
3396 SelectObject(hdc
, CreatePen(PS_SOLID
, 0, colours
[261]));
3397 MoveToEx(hdc
, startx
, starty
, NULL
);
3398 LineTo(hdc
, startx
+ dx
* length
, starty
+ dy
* length
);
3399 oldpen
= SelectObject(hdc
, oldpen
);
3400 DeleteObject(oldpen
);
3402 for (i
= 0; i
< length
; i
++) {
3404 SetPixel(hdc
, startx
, starty
, colours
[261]);
3413 /* This function gets the actual width of a character in the normal font.
3415 int char_width(Context ctx
, int uc
) {
3419 /* If the font max is the same as the font ave width then this
3420 * function is a no-op.
3422 if (!font_dualwidth
) return 1;
3424 switch (uc
& CSET_MASK
) {
3426 uc
= ucsdata
.unitab_line
[uc
& 0xFF];
3429 uc
= ucsdata
.unitab_xterm
[uc
& 0xFF];
3432 uc
= ucsdata
.unitab_scoacs
[uc
& 0xFF];
3435 if (DIRECT_FONT(uc
)) {
3436 if (ucsdata
.dbcs_screenfont
) return 1;
3438 /* Speedup, I know of no font where ascii is the wrong width */
3439 if ((uc
&~CSET_MASK
) >= ' ' && (uc
&~CSET_MASK
)<= '~')
3442 if ( (uc
& CSET_MASK
) == CSET_ACP
) {
3443 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3444 } else if ( (uc
& CSET_MASK
) == CSET_OEMCP
) {
3445 another_font(FONT_OEM
);
3446 if (!fonts
[FONT_OEM
]) return 0;
3448 SelectObject(hdc
, fonts
[FONT_OEM
]);
3452 if ( GetCharWidth32(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1 &&
3453 GetCharWidth(hdc
, uc
&~CSET_MASK
, uc
&~CSET_MASK
, &ibuf
) != 1)
3456 /* Speedup, I know of no font where ascii is the wrong width */
3457 if (uc
>= ' ' && uc
<= '~') return 1;
3459 SelectObject(hdc
, fonts
[FONT_NORMAL
]);
3460 if ( GetCharWidth32W(hdc
, uc
, uc
, &ibuf
) == 1 )
3461 /* Okay that one worked */ ;
3462 else if ( GetCharWidthW(hdc
, uc
, uc
, &ibuf
) == 1 )
3463 /* This should work on 9x too, but it's "less accurate" */ ;
3468 ibuf
+= font_width
/ 2 -1;
3475 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3476 * codes. Returns number of bytes used or zero to drop the message
3477 * or -1 to forward the message to windows.
3479 static int TranslateKey(UINT message
, WPARAM wParam
, LPARAM lParam
,
3480 unsigned char *output
)
3483 int scan
, left_alt
= 0, key_down
, shift_state
;
3485 unsigned char *p
= output
;
3486 static int alt_sum
= 0;
3488 HKL kbd_layout
= GetKeyboardLayout(0);
3490 /* keys is for ToAsciiEx. There's some ick here, see below. */
3491 static WORD keys
[3];
3492 static int compose_char
= 0;
3493 static WPARAM compose_key
= 0;
3495 r
= GetKeyboardState(keystate
);
3497 memset(keystate
, 0, sizeof(keystate
));
3500 #define SHOW_TOASCII_RESULT
3501 { /* Tell us all about key events */
3502 static BYTE oldstate
[256];
3503 static int first
= 1;
3507 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3510 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
) {
3512 } else if ((HIWORD(lParam
) & KF_UP
)
3513 && scan
== (HIWORD(lParam
) & 0xFF)) {
3517 if (wParam
>= VK_F1
&& wParam
<= VK_F20
)
3518 debug(("K_F%d", wParam
+ 1 - VK_F1
));
3531 debug(("VK_%02x", wParam
));
3533 if (message
== WM_SYSKEYDOWN
|| message
== WM_SYSKEYUP
)
3535 debug((", S%02x", scan
= (HIWORD(lParam
) & 0xFF)));
3537 ch
= MapVirtualKeyEx(wParam
, 2, kbd_layout
);
3538 if (ch
>= ' ' && ch
<= '~')
3539 debug((", '%c'", ch
));
3541 debug((", $%02x", ch
));
3544 debug((", KB0=%02x", keys
[0]));
3546 debug((", KB1=%02x", keys
[1]));
3548 debug((", KB2=%02x", keys
[2]));
3550 if ((keystate
[VK_SHIFT
] & 0x80) != 0)
3552 if ((keystate
[VK_CONTROL
] & 0x80) != 0)
3554 if ((HIWORD(lParam
) & KF_EXTENDED
))
3556 if ((HIWORD(lParam
) & KF_UP
))
3560 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
);
3561 else if ((HIWORD(lParam
) & KF_UP
))
3562 oldstate
[wParam
& 0xFF] ^= 0x80;
3564 oldstate
[wParam
& 0xFF] ^= 0x81;
3566 for (ch
= 0; ch
< 256; ch
++)
3567 if (oldstate
[ch
] != keystate
[ch
])
3568 debug((", M%02x=%02x", ch
, keystate
[ch
]));
3570 memcpy(oldstate
, keystate
, sizeof(oldstate
));
3574 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
)) {
3575 keystate
[VK_RMENU
] = keystate
[VK_MENU
];
3579 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3580 if ((cfg
.funky_type
== FUNKY_VT400
||
3581 (cfg
.funky_type
<= FUNKY_LINUX
&& term
->app_keypad_keys
&&
3583 && wParam
== VK_NUMLOCK
&& !(keystate
[VK_SHIFT
] & 0x80)) {
3585 wParam
= VK_EXECUTE
;
3587 /* UnToggle NUMLock */
3588 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0)
3589 keystate
[VK_NUMLOCK
] ^= 1;
3592 /* And write back the 'adjusted' state */
3593 SetKeyboardState(keystate
);
3596 /* Disable Auto repeat if required */
3597 if (term
->repeat_off
&&
3598 (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == KF_REPEAT
)
3601 if ((HIWORD(lParam
) & KF_ALTDOWN
) && (keystate
[VK_RMENU
] & 0x80) == 0)
3604 key_down
= ((HIWORD(lParam
) & KF_UP
) == 0);
3606 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3607 if (left_alt
&& (keystate
[VK_CONTROL
] & 0x80)) {
3608 if (cfg
.ctrlaltkeys
)
3609 keystate
[VK_MENU
] = 0;
3611 keystate
[VK_RMENU
] = 0x80;
3616 scan
= (HIWORD(lParam
) & (KF_UP
| KF_EXTENDED
| 0xFF));
3617 shift_state
= ((keystate
[VK_SHIFT
] & 0x80) != 0)
3618 + ((keystate
[VK_CONTROL
] & 0x80) != 0) * 2;
3620 /* Note if AltGr was pressed and if it was used as a compose key */
3621 if (!compose_state
) {
3622 compose_key
= 0x100;
3623 if (cfg
.compose_key
) {
3624 if (wParam
== VK_MENU
&& (HIWORD(lParam
) & KF_EXTENDED
))
3625 compose_key
= wParam
;
3627 if (wParam
== VK_APPS
)
3628 compose_key
= wParam
;
3631 if (wParam
== compose_key
) {
3632 if (compose_state
== 0
3633 && (HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) == 0) compose_state
=
3635 else if (compose_state
== 1 && (HIWORD(lParam
) & KF_UP
))
3639 } else if (compose_state
== 1 && wParam
!= VK_CONTROL
)
3642 if (compose_state
> 1 && left_alt
)
3645 /* Sanitize the number pad if not using a PC NumPad */
3646 if (left_alt
|| (term
->app_keypad_keys
&& !cfg
.no_applic_k
3647 && cfg
.funky_type
!= FUNKY_XTERM
)
3648 || cfg
.funky_type
== FUNKY_VT400
|| cfg
.nethack_keypad
|| compose_state
) {
3649 if ((HIWORD(lParam
) & KF_EXTENDED
) == 0) {
3653 nParam
= VK_NUMPAD0
;
3656 nParam
= VK_NUMPAD1
;
3659 nParam
= VK_NUMPAD2
;
3662 nParam
= VK_NUMPAD3
;
3665 nParam
= VK_NUMPAD4
;
3668 nParam
= VK_NUMPAD5
;
3671 nParam
= VK_NUMPAD6
;
3674 nParam
= VK_NUMPAD7
;
3677 nParam
= VK_NUMPAD8
;
3680 nParam
= VK_NUMPAD9
;
3683 nParam
= VK_DECIMAL
;
3687 if (keystate
[VK_NUMLOCK
] & 1)
3694 /* If a key is pressed and AltGr is not active */
3695 if (key_down
&& (keystate
[VK_RMENU
] & 0x80) == 0 && !compose_state
) {
3696 /* Okay, prepare for most alts then ... */
3700 /* Lets see if it's a pattern we know all about ... */
3701 if (wParam
== VK_PRIOR
&& shift_state
== 1) {
3702 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEUP
, 0);
3705 if (wParam
== VK_PRIOR
&& shift_state
== 2) {
3706 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEUP
, 0);
3709 if (wParam
== VK_NEXT
&& shift_state
== 1) {
3710 SendMessage(hwnd
, WM_VSCROLL
, SB_PAGEDOWN
, 0);
3713 if (wParam
== VK_NEXT
&& shift_state
== 2) {
3714 SendMessage(hwnd
, WM_VSCROLL
, SB_LINEDOWN
, 0);
3717 if (wParam
== VK_INSERT
&& shift_state
== 1) {
3718 term_do_paste(term
);
3721 if (left_alt
&& wParam
== VK_F4
&& cfg
.alt_f4
) {
3724 if (left_alt
&& wParam
== VK_SPACE
&& cfg
.alt_space
) {
3725 SendMessage(hwnd
, WM_SYSCOMMAND
, SC_KEYMENU
, 0);
3728 if (left_alt
&& wParam
== VK_RETURN
&& cfg
.fullscreenonaltenter
&&
3729 (cfg
.resize_action
!= RESIZE_DISABLED
)) {
3730 if ((HIWORD(lParam
) & (KF_UP
| KF_REPEAT
)) != KF_REPEAT
)
3734 /* Control-Numlock for app-keypad mode switch */
3735 if (wParam
== VK_PAUSE
&& shift_state
== 2) {
3736 term
->app_keypad_keys
^= 1;
3740 /* Nethack keypad */
3741 if (cfg
.nethack_keypad
&& !left_alt
) {
3744 *p
++ = "bB\002\002"[shift_state
& 3];
3747 *p
++ = "jJ\012\012"[shift_state
& 3];
3750 *p
++ = "nN\016\016"[shift_state
& 3];
3753 *p
++ = "hH\010\010"[shift_state
& 3];
3756 *p
++ = shift_state ?
'.' : '.';
3759 *p
++ = "lL\014\014"[shift_state
& 3];
3762 *p
++ = "yY\031\031"[shift_state
& 3];
3765 *p
++ = "kK\013\013"[shift_state
& 3];
3768 *p
++ = "uU\025\025"[shift_state
& 3];
3773 /* Application Keypad */
3777 if (cfg
.funky_type
== FUNKY_VT400
||
3778 (cfg
.funky_type
<= FUNKY_LINUX
&&
3779 term
->app_keypad_keys
&& !cfg
.no_applic_k
)) switch (wParam
) {
3793 if (term
->app_keypad_keys
&& !cfg
.no_applic_k
)
3830 if (cfg
.funky_type
== FUNKY_XTERM
) {
3835 } else if (shift_state
)
3842 if (cfg
.funky_type
== FUNKY_XTERM
)
3846 if (cfg
.funky_type
== FUNKY_XTERM
)
3850 if (cfg
.funky_type
== FUNKY_XTERM
)
3855 if (HIWORD(lParam
) & KF_EXTENDED
)
3860 if (term
->vt52_mode
) {
3861 if (xkey
>= 'P' && xkey
<= 'S')
3862 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
3864 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
3866 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
3871 if (wParam
== VK_BACK
&& shift_state
== 0) { /* Backspace */
3872 *p
++ = (cfg
.bksp_is_delete ?
0x7F : 0x08);
3876 if (wParam
== VK_BACK
&& shift_state
== 1) { /* Shift Backspace */
3877 /* We do the opposite of what is configured */
3878 *p
++ = (cfg
.bksp_is_delete ?
0x08 : 0x7F);
3882 if (wParam
== VK_TAB
&& shift_state
== 1) { /* Shift tab */
3888 if (wParam
== VK_SPACE
&& shift_state
== 2) { /* Ctrl-Space */
3892 if (wParam
== VK_SPACE
&& shift_state
== 3) { /* Ctrl-Shift-Space */
3896 if (wParam
== VK_CANCEL
&& shift_state
== 2) { /* Ctrl-Break */
3901 if (wParam
== VK_PAUSE
) { /* Break/Pause */
3906 /* Control-2 to Control-8 are special */
3907 if (shift_state
== 2 && wParam
>= '2' && wParam
<= '8') {
3908 *p
++ = "\000\033\034\035\036\037\177"[wParam
- '2'];
3911 if (shift_state
== 2 && (wParam
== 0xBD || wParam
== 0xBF)) {
3915 if (shift_state
== 2 && wParam
== 0xDF) {
3919 if (shift_state
== 3 && wParam
== 0xDE) {
3920 *p
++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
3923 if (shift_state
== 0 && wParam
== VK_RETURN
&& term
->cr_lf_return
) {
3930 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3931 * for integer decimal nn.)
3933 * We also deal with the weird ones here. Linux VCs replace F1
3934 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3935 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3941 code
= (keystate
[VK_SHIFT
] & 0x80 ?
23 : 11);
3944 code
= (keystate
[VK_SHIFT
] & 0x80 ?
24 : 12);
3947 code
= (keystate
[VK_SHIFT
] & 0x80 ?
25 : 13);
3950 code
= (keystate
[VK_SHIFT
] & 0x80 ?
26 : 14);
3953 code
= (keystate
[VK_SHIFT
] & 0x80 ?
28 : 15);
3956 code
= (keystate
[VK_SHIFT
] & 0x80 ?
29 : 17);
3959 code
= (keystate
[VK_SHIFT
] & 0x80 ?
31 : 18);
3962 code
= (keystate
[VK_SHIFT
] & 0x80 ?
32 : 19);
3965 code
= (keystate
[VK_SHIFT
] & 0x80 ?
33 : 20);
3968 code
= (keystate
[VK_SHIFT
] & 0x80 ?
34 : 21);
4001 if ((shift_state
&2) == 0) switch (wParam
) {
4021 /* Reorder edit keys to physical order */
4022 if (cfg
.funky_type
== FUNKY_VT400
&& code
<= 6)
4023 code
= "\0\2\1\4\5\3\6"[code
];
4025 if (term
->vt52_mode
&& code
> 0 && code
<= 6) {
4026 p
+= sprintf((char *) p
, "\x1B%c", " HLMEIG"[code
]);
4030 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO function keys */
4031 code
>= 11 && code
<= 34) {
4032 char codes
[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
4035 case VK_F1
: index
= 0; break;
4036 case VK_F2
: index
= 1; break;
4037 case VK_F3
: index
= 2; break;
4038 case VK_F4
: index
= 3; break;
4039 case VK_F5
: index
= 4; break;
4040 case VK_F6
: index
= 5; break;
4041 case VK_F7
: index
= 6; break;
4042 case VK_F8
: index
= 7; break;
4043 case VK_F9
: index
= 8; break;
4044 case VK_F10
: index
= 9; break;
4045 case VK_F11
: index
= 10; break;
4046 case VK_F12
: index
= 11; break;
4048 if (keystate
[VK_SHIFT
] & 0x80) index
+= 12;
4049 if (keystate
[VK_CONTROL
] & 0x80) index
+= 24;
4050 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
4053 if (cfg
.funky_type
== FUNKY_SCO
&& /* SCO small keypad */
4054 code
>= 1 && code
<= 6) {
4055 char codes
[] = "HL.FIG";
4059 p
+= sprintf((char *) p
, "\x1B[%c", codes
[code
-1]);
4063 if ((term
->vt52_mode
|| cfg
.funky_type
== FUNKY_VT100P
) && code
>= 11 && code
<= 24) {
4069 if (term
->vt52_mode
)
4070 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11 - offt
);
4073 sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11 - offt
);
4076 if (cfg
.funky_type
== FUNKY_LINUX
&& code
>= 11 && code
<= 15) {
4077 p
+= sprintf((char *) p
, "\x1B[[%c", code
+ 'A' - 11);
4080 if (cfg
.funky_type
== FUNKY_XTERM
&& code
>= 11 && code
<= 14) {
4081 if (term
->vt52_mode
)
4082 p
+= sprintf((char *) p
, "\x1B%c", code
+ 'P' - 11);
4084 p
+= sprintf((char *) p
, "\x1BO%c", code
+ 'P' - 11);
4087 if (cfg
.rxvt_homeend
&& (code
== 1 || code
== 4)) {
4088 p
+= sprintf((char *) p
, code
== 1 ?
"\x1B[H" : "\x1BOw");
4092 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
4097 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
4098 * some reason seems to send VK_CLEAR to Windows...).
4120 if (term
->vt52_mode
)
4121 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
4123 int app_flg
= (term
->app_cursor_keys
&& !cfg
.no_applic_c
);
4126 * RDB: VT100 & VT102 manuals both state the
4127 * app cursor keys only work if the app keypad
4130 * SGT: That may well be true, but xterm
4131 * disagrees and so does at least one
4132 * application, so I've #if'ed this out and the
4133 * behaviour is back to PuTTY's original: app
4134 * cursor and app keypad are independently
4135 * switchable modes. If anyone complains about
4136 * _this_ I'll have to put in a configurable
4139 if (!term
->app_keypad_keys
)
4142 /* Useful mapping of Ctrl-arrows */
4143 if (shift_state
== 2)
4147 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
4149 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
4156 * Finally, deal with Return ourselves. (Win95 seems to
4157 * foul it up when Alt is pressed, for some reason.)
4159 if (wParam
== VK_RETURN
) { /* Return */
4165 if (left_alt
&& wParam
>= VK_NUMPAD0
&& wParam
<= VK_NUMPAD9
)
4166 alt_sum
= alt_sum
* 10 + wParam
- VK_NUMPAD0
;
4171 /* Okay we've done everything interesting; let windows deal with
4172 * the boring stuff */
4176 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4177 if(cfg
.xlat_capslockcyr
&& keystate
[VK_CAPITAL
] != 0) {
4179 keystate
[VK_CAPITAL
] = 0;
4182 /* XXX how do we know what the max size of the keys array should
4183 * be is? There's indication on MS' website of an Inquire/InquireEx
4184 * functioning returning a KBINFO structure which tells us. */
4185 if (osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
) {
4186 /* XXX 'keys' parameter is declared in MSDN documentation as
4188 * The experience of a French user indicates that on
4189 * Win98, WORD[] should be passed in, but on Win2K, it should
4190 * be BYTE[]. German WinXP and my Win2K with "US International"
4191 * driver corroborate this.
4192 * Experimentally I've conditionalised the behaviour on the
4193 * Win9x/NT split, but I suspect it's worse than that.
4194 * See wishlist item `win-dead-keys' for more horrible detail
4195 * and speculations. */
4198 r
= ToAsciiEx(wParam
, scan
, keystate
, (LPWORD
)keybs
, 0, kbd_layout
);
4199 for (i
=0; i
<3; i
++) keys
[i
] = keybs
[i
];
4201 r
= ToAsciiEx(wParam
, scan
, keystate
, keys
, 0, kbd_layout
);
4203 #ifdef SHOW_TOASCII_RESULT
4204 if (r
== 1 && !key_down
) {
4206 if (in_utf(term
) || ucsdata
.dbcs_screenfont
)
4207 debug((", (U+%04x)", alt_sum
));
4209 debug((", LCH(%d)", alt_sum
));
4211 debug((", ACH(%d)", keys
[0]));
4216 for (r1
= 0; r1
< r
; r1
++) {
4217 debug(("%s%d", r1 ?
"," : "", keys
[r1
]));
4226 * Interrupt an ongoing paste. I'm not sure this is
4227 * sensible, but for the moment it's preferable to
4228 * having to faff about buffering things.
4233 for (i
= 0; i
< r
; i
++) {
4234 unsigned char ch
= (unsigned char) keys
[i
];
4236 if (compose_state
== 2 && (ch
& 0x80) == 0 && ch
> ' ') {
4241 if (compose_state
== 3 && (ch
& 0x80) == 0 && ch
> ' ') {
4245 if ((nc
= check_compose(compose_char
, ch
)) == -1) {
4246 MessageBeep(MB_ICONHAND
);
4250 term_seen_key_event(term
);
4252 luni_send(ldisc
, &keybuf
, 1, 1);
4260 if (in_utf(term
) || ucsdata
.dbcs_screenfont
) {
4262 term_seen_key_event(term
);
4264 luni_send(ldisc
, &keybuf
, 1, 1);
4266 ch
= (char) alt_sum
;
4268 * We need not bother about stdin
4269 * backlogs here, because in GUI PuTTY
4270 * we can't do anything about it
4271 * anyway; there's no means of asking
4272 * Windows to hold off on KEYDOWN
4273 * messages. We _have_ to buffer
4274 * everything we're sent.
4276 term_seen_key_event(term
);
4278 ldisc_send(ldisc
, &ch
, 1, 1);
4282 term_seen_key_event(term
);
4284 lpage_send(ldisc
, kbd_codepage
, &ch
, 1, 1);
4287 if(capsOn
&& ch
< 0x80) {
4290 cbuf
[1] = xlat_uskbd2cyrllic(ch
);
4291 term_seen_key_event(term
);
4293 luni_send(ldisc
, cbuf
+!left_alt
, 1+!!left_alt
, 1);
4298 term_seen_key_event(term
);
4300 lpage_send(ldisc
, kbd_codepage
,
4301 cbuf
+!left_alt
, 1+!!left_alt
, 1);
4307 /* This is so the ALT-Numpad and dead keys work correctly. */
4312 /* If we're definitly not building up an ALT-54321 then clear it */
4315 /* If we will be using alt_sum fix the 256s */
4316 else if (keys
[0] && (in_utf(term
) || ucsdata
.dbcs_screenfont
))
4321 * ALT alone may or may not want to bring up the System menu.
4322 * If it's not meant to, we return 0 on presses or releases of
4323 * ALT, to show that we've swallowed the keystroke. Otherwise
4324 * we return -1, which means Windows will give the keystroke
4325 * its default handling (i.e. bring up the System menu).
4327 if (wParam
== VK_MENU
&& !cfg
.alt_only
)
4333 void request_paste(void *frontend
)
4336 * In Windows, pasting is synchronous: we can read the
4337 * clipboard with no difficulty, so request_paste() can just go
4340 term_do_paste(term
);
4343 void set_title(void *frontend
, char *title
)
4346 window_name
= snewn(1 + strlen(title
), char);
4347 strcpy(window_name
, title
);
4348 if (cfg
.win_name_always
|| !IsIconic(hwnd
))
4349 SetWindowText(hwnd
, title
);
4352 void set_icon(void *frontend
, char *title
)
4355 icon_name
= snewn(1 + strlen(title
), char);
4356 strcpy(icon_name
, title
);
4357 if (!cfg
.win_name_always
&& IsIconic(hwnd
))
4358 SetWindowText(hwnd
, title
);
4361 void set_sbar(void *frontend
, int total
, int start
, int page
)
4365 if (is_full_screen() ?
!cfg
.scrollbar_in_fullscreen
: !cfg
.scrollbar
)
4368 si
.cbSize
= sizeof(si
);
4369 si
.fMask
= SIF_ALL
| SIF_DISABLENOSCROLL
;
4371 si
.nMax
= total
- 1;
4375 SetScrollInfo(hwnd
, SB_VERT
, &si
, TRUE
);
4378 Context
get_ctx(void *frontend
)
4384 SelectPalette(hdc
, pal
, FALSE
);
4390 void free_ctx(Context ctx
)
4392 SelectPalette(ctx
, GetStockObject(DEFAULT_PALETTE
), FALSE
);
4393 ReleaseDC(hwnd
, ctx
);
4396 static void real_palette_set(int n
, int r
, int g
, int b
)
4399 logpal
->palPalEntry
[n
].peRed
= r
;
4400 logpal
->palPalEntry
[n
].peGreen
= g
;
4401 logpal
->palPalEntry
[n
].peBlue
= b
;
4402 logpal
->palPalEntry
[n
].peFlags
= PC_NOCOLLAPSE
;
4403 colours
[n
] = PALETTERGB(r
, g
, b
);
4404 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4406 colours
[n
] = RGB(r
, g
, b
);
4409 void palette_set(void *frontend
, int n
, int r
, int g
, int b
)
4413 if (n
> NALLCOLOURS
)
4415 real_palette_set(n
, r
, g
, b
);
4417 HDC hdc
= get_ctx(frontend
);
4418 UnrealizeObject(pal
);
4419 RealizePalette(hdc
);
4422 if (n
== (ATTR_DEFBG
>>ATTR_BGSHIFT
))
4423 /* If Default Background changes, we need to ensure any
4424 * space between the text area and the window border is
4426 InvalidateRect(hwnd
, NULL
, TRUE
);
4430 void palette_reset(void *frontend
)
4435 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4437 logpal
->palPalEntry
[i
].peRed
= defpal
[i
].rgbtRed
;
4438 logpal
->palPalEntry
[i
].peGreen
= defpal
[i
].rgbtGreen
;
4439 logpal
->palPalEntry
[i
].peBlue
= defpal
[i
].rgbtBlue
;
4440 logpal
->palPalEntry
[i
].peFlags
= 0;
4441 colours
[i
] = PALETTERGB(defpal
[i
].rgbtRed
,
4442 defpal
[i
].rgbtGreen
,
4443 defpal
[i
].rgbtBlue
);
4445 colours
[i
] = RGB(defpal
[i
].rgbtRed
,
4446 defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4451 SetPaletteEntries(pal
, 0, NALLCOLOURS
, logpal
->palPalEntry
);
4452 hdc
= get_ctx(frontend
);
4453 RealizePalette(hdc
);
4456 /* Default Background may have changed. Ensure any space between
4457 * text area and window border is redrawn. */
4458 InvalidateRect(hwnd
, NULL
, TRUE
);
4462 void write_aclip(void *frontend
, char *data
, int len
, int must_deselect
)
4467 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len
+ 1);
4470 lock
= GlobalLock(clipdata
);
4473 memcpy(lock
, data
, len
);
4474 ((unsigned char *) lock
)[len
] = 0;
4475 GlobalUnlock(clipdata
);
4478 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4480 if (OpenClipboard(hwnd
)) {
4482 SetClipboardData(CF_TEXT
, clipdata
);
4485 GlobalFree(clipdata
);
4488 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4492 * Note: unlike write_aclip() this will not append a nul.
4494 void write_clip(void *frontend
, wchar_t * data
, int *attr
, int len
, int must_deselect
)
4496 HGLOBAL clipdata
, clipdata2
, clipdata3
;
4498 void *lock
, *lock2
, *lock3
;
4500 len2
= WideCharToMultiByte(CP_ACP
, 0, data
, len
, 0, 0, NULL
, NULL
);
4502 clipdata
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
,
4503 len
* sizeof(wchar_t));
4504 clipdata2
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, len2
);
4506 if (!clipdata
|| !clipdata2
) {
4508 GlobalFree(clipdata
);
4510 GlobalFree(clipdata2
);
4513 if (!(lock
= GlobalLock(clipdata
)))
4515 if (!(lock2
= GlobalLock(clipdata2
)))
4518 memcpy(lock
, data
, len
* sizeof(wchar_t));
4519 WideCharToMultiByte(CP_ACP
, 0, data
, len
, lock2
, len2
, NULL
, NULL
);
4521 if (cfg
.rtf_paste
) {
4522 wchar_t unitab
[256];
4524 unsigned char *tdata
= (unsigned char *)lock2
;
4525 wchar_t *udata
= (wchar_t *)lock
;
4526 int rtflen
= 0, uindex
= 0, tindex
= 0;
4528 int multilen
, blen
, alen
, totallen
, i
;
4529 char before
[16], after
[4];
4530 int fgcolour
, lastfgcolour
= 0;
4531 int bgcolour
, lastbgcolour
= 0;
4532 int attrBold
, lastAttrBold
= 0;
4533 int attrUnder
, lastAttrUnder
= 0;
4534 int palette
[NALLCOLOURS
];
4537 get_unitab(CP_ACP
, unitab
, 0);
4539 rtfsize
= 100 + strlen(cfg
.font
.name
);
4540 rtf
= snewn(rtfsize
, char);
4541 rtflen
= sprintf(rtf
, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
4542 cfg
.font
.name
, cfg
.font
.height
*2);
4545 * Add colour palette
4546 * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
4550 * First - Determine all colours in use
4551 * o Foregound and background colours share the same palette
4554 memset(palette
, 0, sizeof(palette
));
4555 for (i
= 0; i
< (len
-1); i
++) {
4556 fgcolour
= ((attr
[i
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4557 bgcolour
= ((attr
[i
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4559 if (attr
[i
] & ATTR_REVERSE
) {
4560 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4561 fgcolour
= bgcolour
;
4562 bgcolour
= tmpcolour
;
4565 if (bold_mode
== BOLD_COLOURS
&& (attr
[i
] & ATTR_BOLD
)) {
4566 if (fgcolour
< 8) /* ANSI colours */
4568 else if (fgcolour
>= 256) /* Default colours */
4572 if (attr
[i
] & ATTR_BLINK
) {
4573 if (bgcolour
< 8) /* ANSI colours */
4575 else if (bgcolour
>= 256) /* Default colours */
4579 palette
[fgcolour
]++;
4580 palette
[bgcolour
]++;
4584 * Next - Create a reduced palette
4587 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4588 if (palette
[i
] != 0)
4589 palette
[i
] = ++numcolours
;
4593 * Finally - Write the colour table
4595 rtf
= sresize(rtf
, rtfsize
+ (numcolours
* 25), char);
4596 strcat(rtf
, "{\\colortbl ;");
4597 rtflen
= strlen(rtf
);
4599 for (i
= 0; i
< NALLCOLOURS
; i
++) {
4600 if (palette
[i
] != 0) {
4601 rtflen
+= sprintf(&rtf
[rtflen
], "\\red%d\\green%d\\blue%d;", defpal
[i
].rgbtRed
, defpal
[i
].rgbtGreen
, defpal
[i
].rgbtBlue
);
4604 strcpy(&rtf
[rtflen
], "}");
4609 * We want to construct a piece of RTF that specifies the
4610 * same Unicode text. To do this we will read back in
4611 * parallel from the Unicode data in `udata' and the
4612 * non-Unicode data in `tdata'. For each character in
4613 * `tdata' which becomes the right thing in `udata' when
4614 * looked up in `unitab', we just copy straight over from
4615 * tdata. For each one that doesn't, we must WCToMB it
4616 * individually and produce a \u escape sequence.
4618 * It would probably be more robust to just bite the bullet
4619 * and WCToMB each individual Unicode character one by one,
4620 * then MBToWC each one back to see if it was an accurate
4621 * translation; but that strikes me as a horrifying number
4622 * of Windows API calls so I want to see if this faster way
4623 * will work. If it screws up badly we can always revert to
4624 * the simple and slow way.
4626 while (tindex
< len2
&& uindex
< len
&&
4627 tdata
[tindex
] && udata
[uindex
]) {
4628 if (tindex
+ 1 < len2
&&
4629 tdata
[tindex
] == '\r' &&
4630 tdata
[tindex
+1] == '\n') {
4636 * Set text attributes
4639 if (rtfsize
< rtflen
+ 64) {
4640 rtfsize
= rtflen
+ 512;
4641 rtf
= sresize(rtf
, rtfsize
, char);
4645 * Determine foreground and background colours
4647 fgcolour
= ((attr
[tindex
] & ATTR_FGMASK
) >> ATTR_FGSHIFT
);
4648 bgcolour
= ((attr
[tindex
] & ATTR_BGMASK
) >> ATTR_BGSHIFT
);
4650 if (attr
[tindex
] & ATTR_REVERSE
) {
4651 int tmpcolour
= fgcolour
; /* Swap foreground and background */
4652 fgcolour
= bgcolour
;
4653 bgcolour
= tmpcolour
;
4656 if (bold_mode
== BOLD_COLOURS
&& (attr
[tindex
] & ATTR_BOLD
)) {
4657 if (fgcolour
< 8) /* ANSI colours */
4659 else if (fgcolour
>= 256) /* Default colours */
4663 if (attr
[tindex
] & ATTR_BLINK
) {
4664 if (bgcolour
< 8) /* ANSI colours */
4666 else if (bgcolour
>= 256) /* Default colours */
4671 * Collect other attributes
4673 if (bold_mode
!= BOLD_COLOURS
)
4674 attrBold
= attr
[tindex
] & ATTR_BOLD
;
4678 attrUnder
= attr
[tindex
] & ATTR_UNDER
;
4682 * o If video isn't reversed, ignore colour attributes for default foregound
4684 * o Special case where bolded text is displayed using the default foregound
4685 * and background colours - force to bolded RTF.
4687 if (!(attr
[tindex
] & ATTR_REVERSE
)) {
4688 if (bgcolour
>= 256) /* Default color */
4689 bgcolour
= -1; /* No coloring */
4691 if (fgcolour
>= 256) { /* Default colour */
4692 if (bold_mode
== BOLD_COLOURS
&& (fgcolour
& 1) && bgcolour
== -1)
4693 attrBold
= ATTR_BOLD
; /* Emphasize text with bold attribute */
4695 fgcolour
= -1; /* No coloring */
4700 * Write RTF text attributes
4702 if (lastfgcolour
!= fgcolour
) {
4703 lastfgcolour
= fgcolour
;
4704 rtflen
+= sprintf(&rtf
[rtflen
], "\\cf%d ", (fgcolour
>= 0) ? palette
[fgcolour
] : 0);
4707 if (lastbgcolour
!= bgcolour
) {
4708 lastbgcolour
= bgcolour
;
4709 rtflen
+= sprintf(&rtf
[rtflen
], "\\highlight%d ", (bgcolour
>= 0) ? palette
[bgcolour
] : 0);
4712 if (lastAttrBold
!= attrBold
) {
4713 lastAttrBold
= attrBold
;
4714 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrBold ?
"\\b " : "\\b0 ");
4717 if (lastAttrUnder
!= attrUnder
) {
4718 lastAttrUnder
= attrUnder
;
4719 rtflen
+= sprintf(&rtf
[rtflen
], "%s", attrUnder ?
"\\ul " : "\\ulnone ");
4723 if (unitab
[tdata
[tindex
]] == udata
[uindex
]) {
4729 multilen
= WideCharToMultiByte(CP_ACP
, 0, unitab
+uindex
, 1,
4730 NULL
, 0, NULL
, NULL
);
4731 if (multilen
!= 1) {
4732 blen
= sprintf(before
, "{\\uc%d\\u%d", multilen
,
4734 alen
= 1; strcpy(after
, "}");
4736 blen
= sprintf(before
, "\\u%d", udata
[uindex
]);
4737 alen
= 0; after
[0] = '\0';
4740 assert(tindex
+ multilen
<= len2
);
4741 totallen
= blen
+ alen
;
4742 for (i
= 0; i
< multilen
; i
++) {
4743 if (tdata
[tindex
+i
] == '\\' ||
4744 tdata
[tindex
+i
] == '{' ||
4745 tdata
[tindex
+i
] == '}')
4747 else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A)
4748 totallen
+= 6; /* \par\r\n */
4749 else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20)
4755 if (rtfsize
< rtflen
+ totallen
+ 3) {
4756 rtfsize
= rtflen
+ totallen
+ 512;
4757 rtf
= sresize(rtf
, rtfsize
, char);
4760 strcpy(rtf
+ rtflen
, before
); rtflen
+= blen
;
4761 for (i
= 0; i
< multilen
; i
++) {
4762 if (tdata
[tindex
+i
] == '\\' ||
4763 tdata
[tindex
+i
] == '{' ||
4764 tdata
[tindex
+i
] == '}') {
4765 rtf
[rtflen
++] = '\\';
4766 rtf
[rtflen
++] = tdata
[tindex
+i
];
4767 } else if (tdata
[tindex
+i
] == 0x0D || tdata
[tindex
+i
] == 0x0A) {
4768 rtflen
+= sprintf(rtf
+rtflen
, "\\par\r\n");
4769 } else if (tdata
[tindex
+i
] > 0x7E || tdata
[tindex
+i
] < 0x20) {
4770 rtflen
+= sprintf(rtf
+rtflen
, "\\'%02x", tdata
[tindex
+i
]);
4772 rtf
[rtflen
++] = tdata
[tindex
+i
];
4775 strcpy(rtf
+ rtflen
, after
); rtflen
+= alen
;
4781 rtf
[rtflen
++] = '}'; /* Terminate RTF stream */
4782 rtf
[rtflen
++] = '\0';
4783 rtf
[rtflen
++] = '\0';
4785 clipdata3
= GlobalAlloc(GMEM_DDESHARE
| GMEM_MOVEABLE
, rtflen
);
4786 if (clipdata3
&& (lock3
= GlobalLock(clipdata3
)) != NULL
) {
4787 memcpy(lock3
, rtf
, rtflen
);
4788 GlobalUnlock(clipdata3
);
4794 GlobalUnlock(clipdata
);
4795 GlobalUnlock(clipdata2
);
4798 SendMessage(hwnd
, WM_IGNORE_CLIP
, TRUE
, 0);
4800 if (OpenClipboard(hwnd
)) {
4802 SetClipboardData(CF_UNICODETEXT
, clipdata
);
4803 SetClipboardData(CF_TEXT
, clipdata2
);
4805 SetClipboardData(RegisterClipboardFormat(CF_RTF
), clipdata3
);
4808 GlobalFree(clipdata
);
4809 GlobalFree(clipdata2
);
4813 SendMessage(hwnd
, WM_IGNORE_CLIP
, FALSE
, 0);
4816 void get_clip(void *frontend
, wchar_t ** p
, int *len
)
4818 static HGLOBAL clipdata
= NULL
;
4819 static wchar_t *converted
= 0;
4828 GlobalUnlock(clipdata
);
4831 } else if (OpenClipboard(NULL
)) {
4832 if ((clipdata
= GetClipboardData(CF_UNICODETEXT
))) {
4834 *p
= GlobalLock(clipdata
);
4836 for (p2
= *p
; *p2
; p2
++);
4840 } else if ( (clipdata
= GetClipboardData(CF_TEXT
)) ) {
4844 s
= GlobalLock(clipdata
);
4845 i
= MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, 0, 0);
4846 *p
= converted
= snewn(i
, wchar_t);
4847 MultiByteToWideChar(CP_ACP
, 0, s
, strlen(s
) + 1, converted
, i
);
4860 * Move `lines' lines from position `from' to position `to' in the
4863 void optimised_move(void *frontend
, int to
, int from
, int lines
)
4868 min
= (to
< from ? to
: from
);
4869 max
= to
+ from
- min
;
4871 r
.left
= offset_width
;
4872 r
.right
= offset_width
+ term
->cols
* font_width
;
4873 r
.top
= offset_height
+ min
* font_height
;
4874 r
.bottom
= offset_height
+ (max
+ lines
) * font_height
;
4875 ScrollWindow(hwnd
, 0, (to
- from
) * font_height
, &r
, &r
);
4880 * Print a message box and perform a fatal exit.
4882 void fatalbox(char *fmt
, ...)
4885 char *stuff
, morestuff
[100];
4888 stuff
= dupvprintf(fmt
, ap
);
4890 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4891 MessageBox(hwnd
, stuff
, morestuff
, MB_ICONERROR
| MB_OK
);
4897 * Print a modal (Really Bad) message box and perform a fatal exit.
4899 void modalfatalbox(char *fmt
, ...)
4902 char *stuff
, morestuff
[100];
4905 stuff
= dupvprintf(fmt
, ap
);
4907 sprintf(morestuff
, "%.70s Fatal Error", appname
);
4908 MessageBox(hwnd
, stuff
, morestuff
,
4909 MB_SYSTEMMODAL
| MB_ICONERROR
| MB_OK
);
4914 static void flash_window(int mode
);
4915 static long next_flash
;
4916 static int flashing
= 0;
4918 static void flash_window_timer(void *ctx
, long now
)
4920 if (flashing
&& now
- next_flash
>= 0) {
4926 * Manage window caption / taskbar flashing, if enabled.
4927 * 0 = stop, 1 = maintain, 2 = start
4929 static void flash_window(int mode
)
4931 if ((mode
== 0) || (cfg
.beep_ind
== B_IND_DISABLED
)) {
4934 FlashWindow(hwnd
, FALSE
);
4938 } else if (mode
== 2) {
4942 FlashWindow(hwnd
, TRUE
);
4943 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
4946 } else if ((mode
== 1) && (cfg
.beep_ind
== B_IND_FLASH
)) {
4949 FlashWindow(hwnd
, TRUE
); /* toggle */
4950 next_flash
= schedule_timer(450, flash_window_timer
, hwnd
);
4958 void do_beep(void *frontend
, int mode
)
4960 if (mode
== BELL_DEFAULT
) {
4962 * For MessageBeep style bells, we want to be careful of
4963 * timing, because they don't have the nice property of
4964 * PlaySound bells that each one cancels the previous
4965 * active one. So we limit the rate to one per 50ms or so.
4967 static long lastbeep
= 0;
4970 beepdiff
= GetTickCount() - lastbeep
;
4971 if (beepdiff
>= 0 && beepdiff
< 50)
4975 * The above MessageBeep call takes time, so we record the
4976 * time _after_ it finishes rather than before it starts.
4978 lastbeep
= GetTickCount();
4979 } else if (mode
== BELL_WAVEFILE
) {
4980 if (!PlaySound(cfg
.bell_wavefile
.path
, NULL
,
4981 SND_ASYNC
| SND_FILENAME
)) {
4982 char buf
[sizeof(cfg
.bell_wavefile
.path
) + 80];
4984 sprintf(buf
, "Unable to play sound file\n%s\n"
4985 "Using default sound instead", cfg
.bell_wavefile
.path
);
4986 sprintf(otherbuf
, "%.70s Sound Error", appname
);
4987 MessageBox(hwnd
, buf
, otherbuf
,
4988 MB_OK
| MB_ICONEXCLAMATION
);
4989 cfg
.beep
= BELL_DEFAULT
;
4991 } else if (mode
== BELL_PCSPEAKER
) {
4992 static long lastbeep
= 0;
4995 beepdiff
= GetTickCount() - lastbeep
;
4996 if (beepdiff
>= 0 && beepdiff
< 50)
5000 * We must beep in different ways depending on whether this
5001 * is a 95-series or NT-series OS.
5003 if(osVersion
.dwPlatformId
== VER_PLATFORM_WIN32_NT
)
5007 lastbeep
= GetTickCount();
5009 /* Otherwise, either visual bell or disabled; do nothing here */
5010 if (!term
->has_focus
) {
5011 flash_window(2); /* start */
5016 * Minimise or restore the window in response to a server-side
5019 void set_iconic(void *frontend
, int iconic
)
5021 if (IsIconic(hwnd
)) {
5023 ShowWindow(hwnd
, SW_RESTORE
);
5026 ShowWindow(hwnd
, SW_MINIMIZE
);
5031 * Move the window in response to a server-side request.
5033 void move_window(void *frontend
, int x
, int y
)
5035 if (cfg
.resize_action
== RESIZE_DISABLED
||
5036 cfg
.resize_action
== RESIZE_FONT
||
5040 SetWindowPos(hwnd
, NULL
, x
, y
, 0, 0, SWP_NOSIZE
| SWP_NOZORDER
);
5044 * Move the window to the top or bottom of the z-order in response
5045 * to a server-side request.
5047 void set_zorder(void *frontend
, int top
)
5049 if (cfg
.alwaysontop
)
5050 return; /* ignore */
5051 SetWindowPos(hwnd
, top ? HWND_TOP
: HWND_BOTTOM
, 0, 0, 0, 0,
5052 SWP_NOMOVE
| SWP_NOSIZE
);
5056 * Refresh the window in response to a server-side request.
5058 void refresh_window(void *frontend
)
5060 InvalidateRect(hwnd
, NULL
, TRUE
);
5064 * Maximise or restore the window in response to a server-side
5067 void set_zoomed(void *frontend
, int zoomed
)
5069 if (IsZoomed(hwnd
)) {
5071 ShowWindow(hwnd
, SW_RESTORE
);
5074 ShowWindow(hwnd
, SW_MAXIMIZE
);
5079 * Report whether the window is iconic, for terminal reports.
5081 int is_iconic(void *frontend
)
5083 return IsIconic(hwnd
);
5087 * Report the window's position, for terminal reports.
5089 void get_window_pos(void *frontend
, int *x
, int *y
)
5092 GetWindowRect(hwnd
, &r
);
5098 * Report the window's pixel size, for terminal reports.
5100 void get_window_pixels(void *frontend
, int *x
, int *y
)
5103 GetWindowRect(hwnd
, &r
);
5104 *x
= r
.right
- r
.left
;
5105 *y
= r
.bottom
- r
.top
;
5109 * Return the window or icon title.
5111 char *get_window_title(void *frontend
, int icon
)
5113 return icon ? icon_name
: window_name
;
5117 * See if we're in full-screen mode.
5119 static int is_full_screen()
5121 if (!IsZoomed(hwnd
))
5123 if (GetWindowLongPtr(hwnd
, GWL_STYLE
) & WS_CAPTION
)
5128 /* Get the rect/size of a full screen window using the nearest available
5129 * monitor in multimon systems; default to something sensible if only
5130 * one monitor is present. */
5131 static int get_fullscreen_rect(RECT
* ss
)
5133 #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
5136 mon
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
5137 mi
.cbSize
= sizeof(mi
);
5138 GetMonitorInfo(mon
, &mi
);
5140 /* structure copy */
5144 /* could also use code like this:
5145 ss->left = ss->top = 0;
5146 ss->right = GetSystemMetrics(SM_CXSCREEN);
5147 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
5149 return GetClientRect(GetDesktopWindow(), ss
);
5155 * Go full-screen. This should only be called when we are already
5158 static void make_full_screen()
5163 assert(IsZoomed(hwnd
));
5165 if (is_full_screen())
5168 /* Remove the window furniture. */
5169 style
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5170 style
&= ~(WS_CAPTION
| WS_BORDER
| WS_THICKFRAME
);
5171 if (cfg
.scrollbar_in_fullscreen
)
5172 style
|= WS_VSCROLL
;
5174 style
&= ~WS_VSCROLL
;
5175 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5177 /* Resize ourselves to exactly cover the nearest monitor. */
5178 get_fullscreen_rect(&ss
);
5179 SetWindowPos(hwnd
, HWND_TOP
, ss
.left
, ss
.top
,
5184 /* We may have changed size as a result */
5188 /* Tick the menu item in the System menu. */
5189 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
5194 * Clear the full-screen attributes.
5196 static void clear_full_screen()
5198 DWORD oldstyle
, style
;
5200 /* Reinstate the window furniture. */
5201 style
= oldstyle
= GetWindowLongPtr(hwnd
, GWL_STYLE
);
5202 style
|= WS_CAPTION
| WS_BORDER
;
5203 if (cfg
.resize_action
== RESIZE_DISABLED
)
5204 style
&= ~WS_THICKFRAME
;
5206 style
|= WS_THICKFRAME
;
5208 style
|= WS_VSCROLL
;
5210 style
&= ~WS_VSCROLL
;
5211 if (style
!= oldstyle
) {
5212 SetWindowLongPtr(hwnd
, GWL_STYLE
, style
);
5213 SetWindowPos(hwnd
, NULL
, 0, 0, 0, 0,
5214 SWP_NOMOVE
| SWP_NOSIZE
| SWP_NOZORDER
|
5218 /* Untick the menu item in the System menu. */
5219 CheckMenuItem(GetSystemMenu(hwnd
, FALSE
), IDM_FULLSCREEN
,
5224 * Toggle full-screen mode.
5226 static void flip_full_screen()
5228 if (is_full_screen()) {
5229 ShowWindow(hwnd
, SW_RESTORE
);
5230 } else if (IsZoomed(hwnd
)) {
5233 SendMessage(hwnd
, WM_FULLSCR_ON_MAX
, 0, 0);
5234 ShowWindow(hwnd
, SW_MAXIMIZE
);
5238 void frontend_keypress(void *handle
)
5241 * Keypress termination in non-Close-On-Exit mode is not
5242 * currently supported in PuTTY proper, because the window
5243 * always has a perfectly good Close button anyway. So we do
5249 int from_backend(void *frontend
, int is_stderr
, const char *data
, int len
)
5251 return term_data(term
, is_stderr
, data
, len
);
5254 int from_backend_untrusted(void *frontend
, const char *data
, int len
)
5256 return term_data_untrusted(term
, data
, len
);
5259 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
5262 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
5264 ret
= term_get_userpass_input(term
, p
, in
, inlen
);
5268 void agent_schedule_callback(void (*callback
)(void *, void *, int),
5269 void *callback_ctx
, void *data
, int len
)
5271 struct agent_callback
*c
= snew(struct agent_callback
);
5272 c
->callback
= callback
;
5273 c
->callback_ctx
= callback_ctx
;
5276 PostMessage(hwnd
, WM_AGENT_CALLBACK
, 0, (LPARAM
)c
);