7 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
15 #define COMPILE_MULTIMON_STUBS
25 #define IDM_SHOWLOG 0x0010
26 #define IDM_NEWSESS 0x0020
27 #define IDM_DUPSESS 0x0030
28 #define IDM_RECONF 0x0040
29 #define IDM_CLRSB 0x0050
30 #define IDM_RESET 0x0060
31 #define IDM_HELP 0x0140
32 #define IDM_ABOUT 0x0150
33 #define IDM_SAVEDSESS 0x0160
34 #define IDM_COPYALL 0x0170
35 #define IDM_FULLSCREEN 0x0180
36 #define IDM_PASTE 0x0190
38 #define IDM_SESSLGP 0x0250 /* log type printable */
39 #define IDM_SESSLGA 0x0260 /* log type all chars */
40 #define IDM_SESSLGE 0x0270 /* log end */
42 #define IDM_SPECIAL_MIN 0x0400
43 #define IDM_SPECIAL_MAX 0x0800
45 #define IDM_SAVED_MIN 0x1000
46 #define IDM_SAVED_MAX 0x2000
48 #define WM_IGNORE_CLIP (WM_XUSER + 2)
49 #define WM_FULLSCR_ON_MAX (WM_XUSER + 3)
50 #define WM_AGENT_CALLBACK (WM_XUSER + 4)
52 /* Needed for Chinese support and apparently not always defined. */
54 #define VK_PROCESSKEY 0xE5
57 /* Mouse wheel support. */
59 #define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
62 #define WHEEL_DELTA 120
65 static Mouse_Button translate_button(Mouse_Button button);
66 static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
67 static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
68 unsigned char *output);
69 static void cfgtopalette(void);
70 static void systopalette(void);
71 static void init_palette(void);
72 static void init_fonts(int, int);
73 static void another_font(int);
74 static void deinit_fonts(void);
75 static void set_input_locale(HKL);
77 static int is_full_screen(void);
78 static void make_full_screen(void);
79 static void clear_full_screen(void);
80 static void flip_full_screen(void);
82 /* Window layout information */
83 static void reset_window(int);
84 static int extra_width, extra_height;
85 static int font_width, font_height, font_dualwidth;
86 static int offset_width, offset_height;
87 static int was_zoomed = 0;
88 static int prev_rows, prev_cols;
90 static int pending_netevent = 0;
91 static WPARAM pend_netevent_wParam = 0;
92 static LPARAM pend_netevent_lParam = 0;
93 static void enact_pending_netevent(void);
94 static void flash_window(int mode);
95 static void sys_cursor_update(void);
96 static int is_shift_pressed(void);
97 static int get_fullscreen_rect(RECT * ss);
99 static time_t last_movement = 0;
101 static int caret_x = -1, caret_y = -1;
103 static int kbd_codepage;
106 static Backend *back;
107 static void *backhandle;
109 static struct unicode_data ucsdata;
110 static int session_closed;
112 static const struct telnet_special *specials;
113 static int specials_menu_position;
117 int specials_submenu_pos;
119 enum { SYSMENU, CTXMENU };
121 Config cfg; /* exported to windlg.c */
123 extern struct sesslist sesslist; /* imported from windlg.c */
125 struct agent_callback {
126 void (*callback)(void *, void *, int);
132 #define FONT_NORMAL 0
134 #define FONT_UNDERLINE 2
135 #define FONT_BOLDUND 3
136 #define FONT_WIDE 0x04
137 #define FONT_HIGH 0x08
138 #define FONT_NARROW 0x10
140 #define FONT_OEM 0x20
141 #define FONT_OEMBOLD 0x21
142 #define FONT_OEMUND 0x22
143 #define FONT_OEMBOLDUND 0x23
145 #define FONT_MAXNO 0x2F
147 static HFONT fonts[FONT_MAXNO];
148 static LOGFONT lfont;
149 static int fontflag[FONT_MAXNO];
151 BOLD_COLOURS, BOLD_SHADOW, BOLD_FONT
159 static COLORREF colours[NCOLOURS];
161 static LPLOGPALETTE logpal;
162 static RGBTRIPLE defpal[NCOLOURS];
166 static HBITMAP caretbm;
168 static int dbltime, lasttime, lastact;
169 static Mouse_Button lastbtn;
171 /* this allows xterm-style mouse handling. */
172 static int send_raw_mouse = 0;
173 static int wheel_accumulator = 0;
175 static char *window_name, *icon_name;
177 static int compose_state = 0;
179 static UINT wm_mousewheel = WM_MOUSEWHEEL;
181 /* Dummy routine, only required in plink. */
182 void ldisc_update(void *frontend, int echo, int edit)
186 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
190 int guess_width, guess_height;
193 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
197 InitCommonControls();
199 /* Ensure a Maximize setting in Explorer doesn't maximise the
205 char *str = dupprintf("%s Fatal Error", appname);
206 MessageBox(NULL, "Windows refuses to report a version",
207 str, MB_OK | MB_ICONEXCLAMATION);
213 * If we're running a version of Windows that doesn't support
214 * WM_MOUSEWHEEL, find out what message number we should be
217 if (osVersion.dwMajorVersion < 4 ||
218 (osVersion.dwMajorVersion == 4 &&
219 osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT))
220 wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
223 * See if we can find our Help file.
226 char b[2048], *p, *q, *r;
228 GetModuleFileName(NULL, b, sizeof(b) - 1);
230 p = strrchr(b, '\\');
231 if (p && p >= r) r = p+1;
233 if (q && q >= r) r = q+1;
234 strcpy(r, "putty.hlp");
235 if ( (fp = fopen(b, "r")) != NULL) {
236 help_path = dupstr(b);
240 strcpy(r, "putty.cnt");
241 if ( (fp = fopen(b, "r")) != NULL) {
242 help_has_contents = TRUE;
245 help_has_contents = FALSE;
249 * Process the command line.
255 default_protocol = be_default_protocol;
256 /* Find the appropriate default port. */
259 default_port = 0; /* illegal */
260 for (i = 0; backends[i].backend != NULL; i++)
261 if (backends[i].protocol == default_protocol) {
262 default_port = backends[i].backend->default_port;
266 cfg.logtype = LGTYP_NONE;
268 do_defaults(NULL, &cfg);
273 * Process a couple of command-line options which are more
274 * easily dealt with before the line is broken up into
275 * words. These are the soon-to-be-defunct @sessionname and
276 * the internal-use-only &sharedmemoryhandle, neither of
277 * which are combined with anything else.
279 while (*p && isspace(*p))
283 while (i > 1 && isspace(p[i - 1]))
286 do_defaults(p + 1, &cfg);
287 if (!*cfg.host && !do_config()) {
290 } else if (*p == '&') {
292 * An initial & means we've been given a command line
293 * containing the hex value of a HANDLE for a file
294 * mapping object, which we must then extract as a
299 if (sscanf(p + 1, "%p", &filemap) == 1 &&
300 (cp = MapViewOfFile(filemap, FILE_MAP_READ,
301 0, 0, sizeof(Config))) != NULL) {
304 CloseHandle(filemap);
305 } else if (!do_config()) {
310 * Otherwise, break up the command line and deal with
316 split_into_argv(cmdline, &argc, &argv, NULL);
318 for (i = 0; i < argc; i++) {
322 ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
325 cmdline_error("option \"%s\" requires an argument", p);
326 } else if (ret == 2) {
327 i++; /* skip next argument */
328 } else if (ret == 1) {
329 continue; /* nothing further needs doing */
330 } else if (!strcmp(p, "-cleanup")) {
332 * `putty -cleanup'. Remove all registry
333 * entries associated with PuTTY, and also find
334 * and delete the random seed file.
337 s1 = dupprintf("This procedure will remove ALL Registry\n"
338 "entries associated with %s, and will\n"
339 "also remove the random seed file.\n"
341 "THIS PROCESS WILL DESTROY YOUR SAVED\n"
342 "SESSIONS. Are you really sure you want\n"
343 "to continue?", appname);
344 s2 = dupprintf("%s Warning", appname);
345 if (MessageBox(NULL, s1, s2,
346 MB_YESNO | MB_ICONWARNING) == IDYES) {
352 } else if (*p != '-') {
356 * If we already have a host name, treat
357 * this argument as a port number. NB we
358 * have to treat this as a saved -P
359 * argument, so that it will be deferred
360 * until it's a good moment to run it.
362 int ret = cmdline_process_param("-P", p, 1, &cfg);
364 } else if (!strncmp(q, "telnet:", 7)) {
366 * If the hostname starts with "telnet:",
367 * set the protocol to Telnet and process
368 * the string as a Telnet URL.
373 if (q[0] == '/' && q[1] == '/')
375 cfg.protocol = PROT_TELNET;
377 while (*p && *p != ':' && *p != '/')
386 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
387 cfg.host[sizeof(cfg.host) - 1] = '\0';
391 * Otherwise, treat this argument as a host
394 while (*p && !isspace(*p))
398 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
399 cfg.host[sizeof(cfg.host) - 1] = '\0';
403 cmdline_error("unknown option \"%s\"", p);
408 cmdline_run_saved(&cfg);
410 if (!*cfg.host && !do_config()) {
415 * Trim leading whitespace off the hostname if it's there.
418 int space = strspn(cfg.host, " \t");
419 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
422 /* See if host is of the form user@host */
423 if (cfg.host[0] != '\0') {
424 char *atsign = strchr(cfg.host, '@');
425 /* Make sure we're not overflowing the user field */
427 if (atsign - cfg.host < sizeof cfg.username) {
428 strncpy(cfg.username, cfg.host, atsign - cfg.host);
429 cfg.username[atsign - cfg.host] = '\0';
431 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
436 * Trim a colon suffix off the hostname if it's there.
438 cfg.host[strcspn(cfg.host, ":")] = '\0';
441 * Remove any remaining whitespace from the hostname.
445 while (cfg.host[p2] != '\0') {
446 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
447 cfg.host[p1] = cfg.host[p2];
457 * Select protocol. This is farmed out into a table in a
458 * separate file to enable an ssh-free variant.
463 for (i = 0; backends[i].backend != NULL; i++)
464 if (backends[i].protocol == cfg.protocol) {
465 back = backends[i].backend;
469 char *str = dupprintf("%s Internal Error", appname);
470 MessageBox(NULL, "Unsupported protocol number found",
471 str, MB_OK | MB_ICONEXCLAMATION);
477 /* Check for invalid Port number (i.e. zero) */
479 char *str = dupprintf("%s Internal Error", appname);
480 MessageBox(NULL, "Invalid Port Number",
481 str, MB_OK | MB_ICONEXCLAMATION);
488 wndclass.lpfnWndProc = WndProc;
489 wndclass.cbClsExtra = 0;
490 wndclass.cbWndExtra = 0;
491 wndclass.hInstance = inst;
492 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
493 wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
494 wndclass.hbrBackground = NULL;
495 wndclass.lpszMenuName = NULL;
496 wndclass.lpszClassName = appname;
498 RegisterClass(&wndclass);
503 memset(&ucsdata, 0, sizeof(ucsdata));
505 term = term_init(&cfg, &ucsdata, NULL);
506 logctx = log_init(NULL, &cfg);
507 term_provide_logctx(term, logctx);
512 * Guess some defaults for the window size. This all gets
513 * updated later, so we don't really care too much. However, we
514 * do want the font width/height guesses to correspond to a
515 * large font rather than a small one...
522 term_size(term, cfg.height, cfg.width, cfg.savelines);
523 guess_width = extra_width + font_width * term->cols;
524 guess_height = extra_height + font_height * term->rows;
527 get_fullscreen_rect(&r);
528 if (guess_width > r.right - r.left)
529 guess_width = r.right - r.left;
530 if (guess_height > r.bottom - r.top)
531 guess_height = r.bottom - r.top;
535 int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
538 winmode &= ~(WS_VSCROLL);
539 if (cfg.resize_action == RESIZE_DISABLED)
540 winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
542 exwinmode |= WS_EX_TOPMOST;
544 exwinmode |= WS_EX_CLIENTEDGE;
545 hwnd = CreateWindowEx(exwinmode, appname, appname,
546 winmode, CW_USEDEFAULT, CW_USEDEFAULT,
547 guess_width, guess_height,
548 NULL, NULL, inst, NULL);
552 * Initialise the fonts, simultaneously correcting the guesses
553 * for font_{width,height}.
558 * Correct the guesses for extra_{width,height}.
562 GetWindowRect(hwnd, &wr);
563 GetClientRect(hwnd, &cr);
564 offset_width = offset_height = cfg.window_border;
565 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
566 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
570 * Resize the window, now we know what size we _really_ want it
573 guess_width = extra_width + font_width * term->cols;
574 guess_height = extra_height + font_height * term->rows;
575 SetWindowPos(hwnd, NULL, 0, 0, guess_width, guess_height,
576 SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
579 * Set up a caret bitmap, with no content.
583 int size = (font_width + 15) / 16 * 2 * font_height;
584 bits = snewn(size, char);
585 memset(bits, 0, size);
586 caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
589 CreateCaret(hwnd, caretbm, font_width, font_height);
592 * Initialise the scroll bar.
597 si.cbSize = sizeof(si);
598 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
600 si.nMax = term->rows - 1;
601 si.nPage = term->rows;
603 SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
607 * Start up the telnet connection.
611 char msg[1024], *title;
614 error = back->init(NULL, &backhandle, &cfg,
615 cfg.host, cfg.port, &realhost, cfg.tcp_nodelay);
616 back->provide_logctx(backhandle, logctx);
618 char *str = dupprintf("%s Error", appname);
619 sprintf(msg, "Unable to open connection to\n"
620 "%.800s\n" "%s", cfg.host, error);
621 MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
625 window_name = icon_name = NULL;
627 title = cfg.wintitle;
629 sprintf(msg, "%s - %s", realhost, appname);
633 set_title(NULL, title);
634 set_icon(NULL, title);
638 * Connect the terminal to the backend for resize purposes.
640 term_provide_resize_fn(term, back->size, backhandle);
643 * Set up a line discipline.
645 ldisc = ldisc_create(&cfg, term, back, backhandle, NULL);
647 session_closed = FALSE;
650 * Prepare the mouse handler.
652 lastact = MA_NOTHING;
653 lastbtn = MBT_NOTHING;
654 dbltime = GetDoubleClickTime();
657 * Set up the session-control options on the system menu.
664 popup_menus[SYSMENU].menu = GetSystemMenu(hwnd, FALSE);
665 popup_menus[CTXMENU].menu = CreatePopupMenu();
666 AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_PASTE, "&Paste");
669 get_sesslist(&sesslist, TRUE);
671 i < ((sesslist.nsessions < 256) ? sesslist.nsessions : 256);
673 AppendMenu(s, MF_ENABLED, IDM_SAVED_MIN + (16 * i),
674 sesslist.sessions[i]);
676 for (j = 0; j < lenof(popup_menus); j++) {
677 m = popup_menus[j].menu;
679 AppendMenu(m, MF_SEPARATOR, 0, 0);
680 popup_menus[j].specials_submenu_pos = GetMenuItemCount(m);
681 AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
682 AppendMenu(m, MF_SEPARATOR, 0, 0);
683 AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
684 AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
685 AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) s, "Sa&ved Sessions");
686 AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
687 AppendMenu(m, MF_SEPARATOR, 0, 0);
688 AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
689 AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
690 AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
691 AppendMenu(m, MF_SEPARATOR, 0, 0);
692 AppendMenu(m, (cfg.resize_action == RESIZE_DISABLED) ?
693 MF_GRAYED : MF_ENABLED, IDM_FULLSCREEN, "&Full Screen");
694 AppendMenu(m, MF_SEPARATOR, 0, 0);
696 AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
697 str = dupprintf("&About %s", appname);
698 AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
703 update_specials_menu(NULL);
706 * Set up the initial input locale.
708 set_input_locale(GetKeyboardLayout(0));
711 * Open the initial log file if there is one.
716 * Finally show the window!
718 ShowWindow(hwnd, show);
719 SetForegroundWindow(hwnd);
722 * Set the palette up.
728 term->has_focus = (GetForegroundWindow() == hwnd);
731 if (GetMessage(&msg, NULL, 0, 0) == 1) {
732 int timer_id = 0, long_timer = 0;
734 while (msg.message != WM_QUIT) {
735 /* Sometimes DispatchMessage calls routines that use their own
736 * GetMessage loop, setup this timer so we get some control back.
738 * Also call term_update() from the timer so that if the host
739 * is sending data flat out we still do redraws.
741 if (timer_id && long_timer) {
742 KillTimer(hwnd, timer_id);
743 long_timer = timer_id = 0;
746 timer_id = SetTimer(hwnd, 1, 20, NULL);
747 if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
748 DispatchMessage(&msg);
750 /* Make sure we blink everything that needs it. */
753 /* Send the paste buffer if there's anything to send */
756 /* If there's nothing new in the queue then we can do everything
757 * we've delayed, reading the socket, writing, and repainting
760 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
763 if (pending_netevent) {
764 enact_pending_netevent();
766 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
770 /* Okay there is now nothing to do so we make sure the screen is
771 * completely up to date then tell windows to call us in a little
775 KillTimer(hwnd, timer_id);
779 if (GetCapture() != hwnd ||
781 !(cfg.mouse_override && is_shift_pressed())))
786 flash_window(1); /* maintain */
788 /* The messages seem unreliable; especially if we're being tricky */
789 term->has_focus = (GetForegroundWindow() == hwnd);
792 /* Hmm, term_update didn't want to do an update too soon ... */
793 timer_id = SetTimer(hwnd, 1, 50, NULL);
794 else if (!term->has_focus)
795 timer_id = SetTimer(hwnd, 1, 500, NULL);
797 timer_id = SetTimer(hwnd, 1, 100, NULL);
800 /* There's no point rescanning everything in the message queue
801 * so we do an apparently unnecessary wait here
804 if (GetMessage(&msg, NULL, 0, 0) != 1)
809 cleanup_exit(msg.wParam); /* this doesn't return... */
810 return msg.wParam; /* ... but optimiser doesn't know */
816 void cleanup_exit(int code)
827 if (cfg.protocol == PROT_SSH) {
838 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
840 char *do_select(SOCKET skt, int startup)
845 events = (FD_CONNECT | FD_READ | FD_WRITE |
846 FD_OOB | FD_CLOSE | FD_ACCEPT);
851 return "do_select(): internal error (hwnd==NULL)";
852 if (p_WSAAsyncSelect(skt, hwnd, msg, events) == SOCKET_ERROR) {
853 switch (p_WSAGetLastError()) {
855 return "Network is down";
857 return "WSAAsyncSelect(): unknown error";
864 * Update the Special Commands submenu.
866 void update_specials_menu(void *frontend)
868 HMENU m = GetSystemMenu(hwnd, FALSE);
869 int menu_already_exists = (specials != NULL);
872 specials = back->get_specials(backhandle);
874 HMENU p = CreateMenu();
875 for (i = 0; specials[i].name; i++) {
876 assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
877 if (*specials[i].name)
878 AppendMenu(p, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
881 AppendMenu(p, MF_SEPARATOR, 0, 0);
883 for (j = 0; j < lenof(popup_menus); j++) {
884 if (menu_already_exists)
885 DeleteMenu(popup_menus[j].menu,
886 popup_menus[j].specials_submenu_pos,
889 InsertMenu(popup_menus[j].menu,
890 popup_menus[j].specials_submenu_pos,
891 MF_BYPOSITION | MF_SEPARATOR, 0, 0);
892 InsertMenu(popup_menus[j].menu,
893 popup_menus[j].specials_submenu_pos,
894 MF_BYPOSITION | MF_POPUP | MF_ENABLED,
895 (UINT) p, "Special Command");
901 * set or clear the "raw mouse message" mode
903 void set_raw_mouse_mode(void *frontend, int activate)
905 activate = activate && !cfg.no_mouse_rep;
906 send_raw_mouse = activate;
907 SetCursor(LoadCursor(NULL, activate ? IDC_ARROW : IDC_IBEAM));
911 * Print a message box and close the connection.
913 void connection_fatal(void *frontend, char *fmt, ...)
916 char *stuff, morestuff[100];
919 stuff = dupvprintf(fmt, ap);
921 sprintf(morestuff, "%.70s Fatal Error", appname);
922 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
925 if (cfg.close_on_exit == FORCE_ON)
928 session_closed = TRUE;
929 sprintf(morestuff, "%.70s (inactive)", appname);
930 set_icon(NULL, morestuff);
931 set_title(NULL, morestuff);
936 * Report an error at the command-line parsing stage.
938 void cmdline_error(char *fmt, ...)
941 char *stuff, morestuff[100];
944 stuff = dupvprintf(fmt, ap);
946 sprintf(morestuff, "%.70s Command Line Error", appname);
947 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
953 * Actually do the job requested by a WM_NETEVENT
955 static void enact_pending_netevent(void)
957 static int reentering = 0;
958 extern int select_result(WPARAM, LPARAM);
962 return; /* don't unpend the pending */
964 pending_netevent = FALSE;
967 ret = select_result(pend_netevent_wParam, pend_netevent_lParam);
970 if (ret == 0 && !session_closed) {
971 /* Abnormal exits will already have set session_closed and taken
972 * appropriate action. */
973 if (cfg.close_on_exit == FORCE_ON ||
974 cfg.close_on_exit == AUTO) PostQuitMessage(0);
977 session_closed = TRUE;
978 sprintf(morestuff, "%.70s (inactive)", appname);
979 set_icon(NULL, morestuff);
980 set_title(NULL, morestuff);
981 MessageBox(hwnd, "Connection closed by remote host",
982 appname, MB_OK | MB_ICONINFORMATION);
988 * Copy the colour palette from the configuration data into defpal.
989 * This is non-trivial because the colour indices are different.
991 static void cfgtopalette(void)
994 static const int ww[] = {
995 6, 7, 8, 9, 10, 11, 12, 13,
996 14, 15, 16, 17, 18, 19, 20, 21,
997 0, 1, 2, 3, 4, 4, 5, 5
1000 for (i = 0; i < 24; i++) {
1002 defpal[i].rgbtRed = cfg.colours[w][0];
1003 defpal[i].rgbtGreen = cfg.colours[w][1];
1004 defpal[i].rgbtBlue = cfg.colours[w][2];
1007 /* Override with system colours if appropriate */
1008 if (cfg.system_colour)
1013 * Override bit of defpal with colours from the system.
1014 * (NB that this takes a copy the system colours at the time this is called,
1015 * so subsequent colour scheme changes don't take effect. To fix that we'd
1016 * probably want to be using GetSysColorBrush() and the like.)
1018 static void systopalette(void)
1021 static const struct { int nIndex; int norm; int bold; } or[] =
1023 { COLOR_WINDOWTEXT, 16, 17 }, /* Default Foreground */
1024 { COLOR_WINDOW, 18, 19 }, /* Default Background */
1025 { COLOR_HIGHLIGHTTEXT, 20, 21 }, /* Cursor Text */
1026 { COLOR_HIGHLIGHT, 22, 23 }, /* Cursor Colour */
1029 for (i = 0; i < (sizeof(or)/sizeof(or[0])); i++) {
1030 COLORREF colour = GetSysColor(or[i].nIndex);
1031 defpal[or[i].norm].rgbtRed =
1032 defpal[or[i].bold].rgbtRed = GetRValue(colour);
1033 defpal[or[i].norm].rgbtGreen =
1034 defpal[or[i].bold].rgbtGreen = GetGValue(colour);
1035 defpal[or[i].norm].rgbtBlue =
1036 defpal[or[i].bold].rgbtBlue = GetBValue(colour);
1041 * Set up the colour palette.
1043 static void init_palette(void)
1046 HDC hdc = GetDC(hwnd);
1048 if (cfg.try_palette && GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
1050 * This is a genuine case where we must use smalloc
1051 * because the snew macros can't cope.
1053 logpal = smalloc(sizeof(*logpal)
1054 - sizeof(logpal->palPalEntry)
1055 + NCOLOURS * sizeof(PALETTEENTRY));
1056 logpal->palVersion = 0x300;
1057 logpal->palNumEntries = NCOLOURS;
1058 for (i = 0; i < NCOLOURS; i++) {
1059 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
1060 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
1061 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
1062 logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
1064 pal = CreatePalette(logpal);
1066 SelectPalette(hdc, pal, FALSE);
1067 RealizePalette(hdc);
1068 SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), FALSE);
1071 ReleaseDC(hwnd, hdc);
1074 for (i = 0; i < NCOLOURS; i++)
1075 colours[i] = PALETTERGB(defpal[i].rgbtRed,
1076 defpal[i].rgbtGreen,
1077 defpal[i].rgbtBlue);
1079 for (i = 0; i < NCOLOURS; i++)
1080 colours[i] = RGB(defpal[i].rgbtRed,
1081 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
1085 * Initialise all the fonts we will need initially. There may be as many as
1086 * three or as few as one. The other (poentially) twentyone fonts are done
1087 * if/when they are needed.
1091 * - check the font width and height, correcting our guesses if
1094 * - verify that the bold font is the same width as the ordinary
1095 * one, and engage shadow bolding if not.
1097 * - verify that the underlined font is the same width as the
1098 * ordinary one (manual underlining by means of line drawing can
1099 * be done in a pinch).
1101 static void init_fonts(int pick_width, int pick_height)
1108 int fw_dontcare, fw_bold;
1110 for (i = 0; i < FONT_MAXNO; i++)
1113 bold_mode = cfg.bold_colour ? BOLD_COLOURS : BOLD_FONT;
1114 und_mode = UND_FONT;
1116 if (cfg.font.isbold) {
1117 fw_dontcare = FW_BOLD;
1120 fw_dontcare = FW_DONTCARE;
1127 font_height = pick_height;
1129 font_height = cfg.font.height;
1130 if (font_height > 0) {
1132 -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1135 font_width = pick_width;
1137 #define f(i,c,w,u) \
1138 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1139 c, OUT_DEFAULT_PRECIS, \
1140 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
1141 FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1143 f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);
1145 lfont.lfHeight = font_height;
1146 lfont.lfWidth = font_width;
1147 lfont.lfEscapement = 0;
1148 lfont.lfOrientation = 0;
1149 lfont.lfWeight = fw_dontcare;
1150 lfont.lfItalic = FALSE;
1151 lfont.lfUnderline = FALSE;
1152 lfont.lfStrikeOut = FALSE;
1153 lfont.lfCharSet = cfg.font.charset;
1154 lfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1155 lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1156 lfont.lfQuality = DEFAULT_QUALITY;
1157 lfont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1158 strncpy(lfont.lfFaceName, cfg.font.name, LF_FACESIZE);
1160 SelectObject(hdc, fonts[FONT_NORMAL]);
1161 GetTextMetrics(hdc, &tm);
1163 if (pick_width == 0 || pick_height == 0) {
1164 font_height = tm.tmHeight;
1165 font_width = tm.tmAveCharWidth;
1167 font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
1169 #ifdef RDB_DEBUG_PATCH
1170 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1171 tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
1176 DWORD cset = tm.tmCharSet;
1177 memset(&info, 0xFF, sizeof(info));
1179 /* !!! Yes the next line is right */
1180 if (cset == OEM_CHARSET)
1181 ucsdata.font_codepage = GetOEMCP();
1183 if (TranslateCharsetInfo ((DWORD *) cset, &info, TCI_SRCCHARSET))
1184 ucsdata.font_codepage = info.ciACP;
1186 ucsdata.font_codepage = -1;
1188 GetCPInfo(ucsdata.font_codepage, &cpinfo);
1189 ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
1192 f(FONT_UNDERLINE, cfg.font.charset, fw_dontcare, TRUE);
1195 * Some fonts, e.g. 9-pt Courier, draw their underlines
1196 * outside their character cell. We successfully prevent
1197 * screen corruption by clipping the text output, but then
1198 * we lose the underline completely. Here we try to work
1199 * out whether this is such a font, and if it is, we set a
1200 * flag that causes underlines to be drawn by hand.
1202 * Having tried other more sophisticated approaches (such
1203 * as examining the TEXTMETRIC structure or requesting the
1204 * height of a string), I think we'll do this the brute
1205 * force way: we create a small bitmap, draw an underlined
1206 * space on it, and test to see whether any pixels are
1207 * foreground-coloured. (Since we expect the underline to
1208 * go all the way across the character cell, we only search
1209 * down a single column of the bitmap, half way across.)
1213 HBITMAP und_bm, und_oldbm;
1217 und_dc = CreateCompatibleDC(hdc);
1218 und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
1219 und_oldbm = SelectObject(und_dc, und_bm);
1220 SelectObject(und_dc, fonts[FONT_UNDERLINE]);
1221 SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
1222 SetTextColor(und_dc, RGB(255, 255, 255));
1223 SetBkColor(und_dc, RGB(0, 0, 0));
1224 SetBkMode(und_dc, OPAQUE);
1225 ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
1227 for (i = 0; i < font_height; i++) {
1228 c = GetPixel(und_dc, font_width / 2, i);
1229 if (c != RGB(0, 0, 0))
1232 SelectObject(und_dc, und_oldbm);
1233 DeleteObject(und_bm);
1236 und_mode = UND_LINE;
1237 DeleteObject(fonts[FONT_UNDERLINE]);
1238 fonts[FONT_UNDERLINE] = 0;
1242 if (bold_mode == BOLD_FONT) {
1243 f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);
1247 descent = tm.tmAscent + 1;
1248 if (descent >= font_height)
1249 descent = font_height - 1;
1251 for (i = 0; i < 3; i++) {
1253 if (SelectObject(hdc, fonts[i]) && GetTextMetrics(hdc, &tm))
1254 fontsize[i] = tm.tmAveCharWidth + 256 * tm.tmHeight;
1261 ReleaseDC(hwnd, hdc);
1263 if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
1264 und_mode = UND_LINE;
1265 DeleteObject(fonts[FONT_UNDERLINE]);
1266 fonts[FONT_UNDERLINE] = 0;
1269 if (bold_mode == BOLD_FONT &&
1270 fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
1271 bold_mode = BOLD_SHADOW;
1272 DeleteObject(fonts[FONT_BOLD]);
1273 fonts[FONT_BOLD] = 0;
1275 fontflag[0] = fontflag[1] = fontflag[2] = 1;
1277 init_ucs(&cfg, &ucsdata);
1280 static void another_font(int fontno)
1283 int fw_dontcare, fw_bold;
1287 if (fontno < 0 || fontno >= FONT_MAXNO || fontflag[fontno])
1290 basefont = (fontno & ~(FONT_BOLDUND));
1291 if (basefont != fontno && !fontflag[basefont])
1292 another_font(basefont);
1294 if (cfg.font.isbold) {
1295 fw_dontcare = FW_BOLD;
1298 fw_dontcare = FW_DONTCARE;
1302 c = cfg.font.charset;
1308 if (fontno & FONT_WIDE)
1310 if (fontno & FONT_NARROW)
1312 if (fontno & FONT_OEM)
1314 if (fontno & FONT_BOLD)
1316 if (fontno & FONT_UNDERLINE)
1320 CreateFont(font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
1321 FALSE, u, FALSE, c, OUT_DEFAULT_PRECIS,
1322 CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1323 FIXED_PITCH | FF_DONTCARE, s);
1325 fontflag[fontno] = 1;
1328 static void deinit_fonts(void)
1331 for (i = 0; i < FONT_MAXNO; i++) {
1333 DeleteObject(fonts[i]);
1339 void request_resize(void *frontend, int w, int h)
1343 /* If the window is maximized supress resizing attempts */
1344 if (IsZoomed(hwnd)) {
1345 if (cfg.resize_action == RESIZE_TERM)
1349 if (cfg.resize_action == RESIZE_DISABLED) return;
1350 if (h == term->rows && w == term->cols) return;
1352 /* Sanity checks ... */
1354 static int first_time = 1;
1357 switch (first_time) {
1359 /* Get the size of the screen */
1360 if (get_fullscreen_rect(&ss))
1361 /* first_time = 0 */ ;
1367 /* Make sure the values are sane */
1368 width = (ss.right - ss.left - extra_width) / 4;
1369 height = (ss.bottom - ss.top - extra_height) / 6;
1371 if (w > width || h > height)
1380 term_size(term, h, w, cfg.savelines);
1382 if (cfg.resize_action != RESIZE_FONT && !IsZoomed(hwnd)) {
1383 width = extra_width + font_width * w;
1384 height = extra_height + font_height * h;
1386 SetWindowPos(hwnd, NULL, 0, 0, width, height,
1387 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1388 SWP_NOMOVE | SWP_NOZORDER);
1392 InvalidateRect(hwnd, NULL, TRUE);
1395 static void reset_window(int reinit) {
1397 * This function decides how to resize or redraw when the
1398 * user changes something.
1400 * This function doesn't like to change the terminal size but if the
1401 * font size is locked that may be it's only soluion.
1403 int win_width, win_height;
1406 #ifdef RDB_DEBUG_PATCH
1407 debug((27, "reset_window()"));
1410 /* Current window sizes ... */
1411 GetWindowRect(hwnd, &wr);
1412 GetClientRect(hwnd, &cr);
1414 win_width = cr.right - cr.left;
1415 win_height = cr.bottom - cr.top;
1417 if (cfg.resize_action == RESIZE_DISABLED) reinit = 2;
1419 /* Are we being forced to reload the fonts ? */
1421 #ifdef RDB_DEBUG_PATCH
1422 debug((27, "reset_window() -- Forced deinit"));
1428 /* Oh, looks like we're minimised */
1429 if (win_width == 0 || win_height == 0)
1432 /* Is the window out of position ? */
1434 (offset_width != (win_width-font_width*term->cols)/2 ||
1435 offset_height != (win_height-font_height*term->rows)/2) ){
1436 offset_width = (win_width-font_width*term->cols)/2;
1437 offset_height = (win_height-font_height*term->rows)/2;
1438 InvalidateRect(hwnd, NULL, TRUE);
1439 #ifdef RDB_DEBUG_PATCH
1440 debug((27, "reset_window() -> Reposition terminal"));
1444 if (IsZoomed(hwnd)) {
1445 /* We're fullscreen, this means we must not change the size of
1446 * the window so it's the font size or the terminal itself.
1449 extra_width = wr.right - wr.left - cr.right + cr.left;
1450 extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
1452 if (cfg.resize_action != RESIZE_TERM) {
1453 if ( font_width != win_width/term->cols ||
1454 font_height != win_height/term->rows) {
1456 init_fonts(win_width/term->cols, win_height/term->rows);
1457 offset_width = (win_width-font_width*term->cols)/2;
1458 offset_height = (win_height-font_height*term->rows)/2;
1459 InvalidateRect(hwnd, NULL, TRUE);
1460 #ifdef RDB_DEBUG_PATCH
1461 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1462 font_width, font_height));
1466 if ( font_width != win_width/term->cols ||
1467 font_height != win_height/term->rows) {
1468 /* Our only choice at this point is to change the
1469 * size of the terminal; Oh well.
1471 term_size(term, win_height/font_height, win_width/font_width,
1473 offset_width = (win_width-font_width*term->cols)/2;
1474 offset_height = (win_height-font_height*term->rows)/2;
1475 InvalidateRect(hwnd, NULL, TRUE);
1476 #ifdef RDB_DEBUG_PATCH
1477 debug((27, "reset_window() -> Zoomed term_size"));
1484 /* Hmm, a force re-init means we should ignore the current window
1485 * so we resize to the default font size.
1488 #ifdef RDB_DEBUG_PATCH
1489 debug((27, "reset_window() -> Forced re-init"));
1492 offset_width = offset_height = cfg.window_border;
1493 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1494 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1496 if (win_width != font_width*term->cols + offset_width*2 ||
1497 win_height != font_height*term->rows + offset_height*2) {
1499 /* If this is too large windows will resize it to the maximum
1500 * allowed window size, we will then be back in here and resize
1501 * the font or terminal to fit.
1503 SetWindowPos(hwnd, NULL, 0, 0,
1504 font_width*term->cols + extra_width,
1505 font_height*term->rows + extra_height,
1506 SWP_NOMOVE | SWP_NOZORDER);
1509 InvalidateRect(hwnd, NULL, TRUE);
1513 /* Okay the user doesn't want us to change the font so we try the
1514 * window. But that may be too big for the screen which forces us
1515 * to change the terminal.
1517 if ((cfg.resize_action == RESIZE_TERM && reinit<=0) ||
1518 (cfg.resize_action == RESIZE_EITHER && reinit<0) ||
1520 offset_width = offset_height = cfg.window_border;
1521 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1522 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1524 if (win_width != font_width*term->cols + offset_width*2 ||
1525 win_height != font_height*term->rows + offset_height*2) {
1530 get_fullscreen_rect(&ss);
1532 width = (ss.right - ss.left - extra_width) / font_width;
1533 height = (ss.bottom - ss.top - extra_height) / font_height;
1536 if ( term->rows > height || term->cols > width ) {
1537 if (cfg.resize_action == RESIZE_EITHER) {
1538 /* Make the font the biggest we can */
1539 if (term->cols > width)
1540 font_width = (ss.right - ss.left - extra_width)
1542 if (term->rows > height)
1543 font_height = (ss.bottom - ss.top - extra_height)
1547 init_fonts(font_width, font_height);
1549 width = (ss.right - ss.left - extra_width) / font_width;
1550 height = (ss.bottom - ss.top - extra_height) / font_height;
1552 if ( height > term->rows ) height = term->rows;
1553 if ( width > term->cols ) width = term->cols;
1554 term_size(term, height, width, cfg.savelines);
1555 #ifdef RDB_DEBUG_PATCH
1556 debug((27, "reset_window() -> term resize to (%d,%d)",
1562 SetWindowPos(hwnd, NULL, 0, 0,
1563 font_width*term->cols + extra_width,
1564 font_height*term->rows + extra_height,
1565 SWP_NOMOVE | SWP_NOZORDER);
1567 InvalidateRect(hwnd, NULL, TRUE);
1568 #ifdef RDB_DEBUG_PATCH
1569 debug((27, "reset_window() -> window resize to (%d,%d)",
1570 font_width*term->cols + extra_width,
1571 font_height*term->rows + extra_height));
1577 /* We're allowed to or must change the font but do we want to ? */
1579 if (font_width != (win_width-cfg.window_border*2)/term->cols ||
1580 font_height != (win_height-cfg.window_border*2)/term->rows) {
1583 init_fonts((win_width-cfg.window_border*2)/term->cols,
1584 (win_height-cfg.window_border*2)/term->rows);
1585 offset_width = (win_width-font_width*term->cols)/2;
1586 offset_height = (win_height-font_height*term->rows)/2;
1588 extra_width = wr.right - wr.left - cr.right + cr.left +offset_width*2;
1589 extra_height = wr.bottom - wr.top - cr.bottom + cr.top+offset_height*2;
1591 InvalidateRect(hwnd, NULL, TRUE);
1592 #ifdef RDB_DEBUG_PATCH
1593 debug((25, "reset_window() -> font resize to (%d,%d)",
1594 font_width, font_height));
1599 static void set_input_locale(HKL kl)
1603 GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
1604 lbuf, sizeof(lbuf));
1606 kbd_codepage = atoi(lbuf);
1609 static void click(Mouse_Button b, int x, int y, int shift, int ctrl, int alt)
1611 int thistime = GetMessageTime();
1613 if (send_raw_mouse && !(cfg.mouse_override && shift)) {
1614 lastbtn = MBT_NOTHING;
1615 term_mouse(term, b, translate_button(b), MA_CLICK,
1616 x, y, shift, ctrl, alt);
1620 if (lastbtn == b && thistime - lasttime < dbltime) {
1621 lastact = (lastact == MA_CLICK ? MA_2CLK :
1622 lastact == MA_2CLK ? MA_3CLK :
1623 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
1628 if (lastact != MA_NOTHING)
1629 term_mouse(term, b, translate_button(b), lastact,
1630 x, y, shift, ctrl, alt);
1631 lasttime = thistime;
1635 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1636 * into a cooked one (SELECT, EXTEND, PASTE).
1638 static Mouse_Button translate_button(Mouse_Button button)
1640 if (button == MBT_LEFT)
1642 if (button == MBT_MIDDLE)
1643 return cfg.mouse_is_xterm == 1 ? MBT_PASTE : MBT_EXTEND;
1644 if (button == MBT_RIGHT)
1645 return cfg.mouse_is_xterm == 1 ? MBT_EXTEND : MBT_PASTE;
1646 return 0; /* shouldn't happen */
1649 static void show_mouseptr(int show)
1651 static int cursor_visible = 1;
1652 if (!cfg.hide_mouseptr) /* override if this feature disabled */
1654 if (cursor_visible && !show)
1656 else if (!cursor_visible && show)
1658 cursor_visible = show;
1661 static int is_alt_pressed(void)
1664 int r = GetKeyboardState(keystate);
1667 if (keystate[VK_MENU] & 0x80)
1669 if (keystate[VK_RMENU] & 0x80)
1674 static int is_shift_pressed(void)
1677 int r = GetKeyboardState(keystate);
1680 if (keystate[VK_SHIFT] & 0x80)
1685 static int resizing;
1687 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1688 WPARAM wParam, LPARAM lParam)
1691 static int ignore_clip = FALSE;
1692 static int need_backend_resize = FALSE;
1693 static int fullscr_on_max = FALSE;
1697 if (pending_netevent)
1698 enact_pending_netevent();
1699 if (GetCapture() != hwnd ||
1700 (send_raw_mouse && !(cfg.mouse_override && is_shift_pressed())))
1706 if (cfg.ping_interval > 0) {
1709 if (now - last_movement > cfg.ping_interval) {
1710 back->special(backhandle, TS_PING);
1711 last_movement = now;
1714 net_pending_errors();
1722 str = dupprintf("%s Exit Confirmation", appname);
1723 if (!cfg.warn_on_close || session_closed ||
1725 "Are you sure you want to close this session?",
1726 str, MB_ICONWARNING | MB_OKCANCEL) == IDOK)
1727 DestroyWindow(hwnd);
1737 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
1749 PROCESS_INFORMATION pi;
1750 HANDLE filemap = NULL;
1752 if (wParam == IDM_DUPSESS) {
1754 * Allocate a file-mapping memory chunk for the
1757 SECURITY_ATTRIBUTES sa;
1760 sa.nLength = sizeof(sa);
1761 sa.lpSecurityDescriptor = NULL;
1762 sa.bInheritHandle = TRUE;
1763 filemap = CreateFileMapping((HANDLE) 0xFFFFFFFF,
1766 0, sizeof(Config), NULL);
1768 p = (Config *) MapViewOfFile(filemap,
1770 0, 0, sizeof(Config));
1772 *p = cfg; /* structure copy */
1776 sprintf(c, "putty &%p", filemap);
1778 } else if (wParam == IDM_SAVEDSESS) {
1779 if ((lParam - IDM_SAVED_MIN) / 16 < sesslist.nsessions) {
1781 sesslist.sessions[(lParam - IDM_SAVED_MIN) / 16];
1782 cl = snewn(16 + strlen(session), char);
1783 /* 8, but play safe */
1786 /* not a very important failure mode */
1788 sprintf(cl, "putty @%s", session);
1796 GetModuleFileName(NULL, b, sizeof(b) - 1);
1798 si.lpReserved = NULL;
1799 si.lpDesktop = NULL;
1803 si.lpReserved2 = NULL;
1804 CreateProcess(b, cl, NULL, NULL, TRUE,
1805 NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
1808 CloseHandle(filemap);
1818 GetWindowText(hwnd, cfg.wintitle, sizeof(cfg.wintitle));
1821 if (!do_reconfig(hwnd))
1825 /* Disable full-screen if resizing forbidden */
1826 HMENU m = GetSystemMenu (hwnd, FALSE);
1827 EnableMenuItem(m, IDM_FULLSCREEN, MF_BYCOMMAND |
1828 (cfg.resize_action == RESIZE_DISABLED)
1829 ? MF_GRAYED : MF_ENABLED);
1830 /* Gracefully unzoom if necessary */
1831 if (IsZoomed(hwnd) &&
1832 (cfg.resize_action == RESIZE_DISABLED)) {
1833 ShowWindow(hwnd, SW_RESTORE);
1837 /* Pass new config data to the logging module */
1838 log_reconfig(logctx, &cfg);
1842 * Flush the line discipline's edit buffer in the
1843 * case where local editing has just been disabled.
1845 ldisc_send(ldisc, NULL, 0, 0);
1853 /* Pass new config data to the terminal */
1854 term_reconfig(term, &cfg);
1856 /* Pass new config data to the back end */
1857 back->reconfig(backhandle, &cfg);
1859 /* Screen size changed ? */
1860 if (cfg.height != prev_cfg.height ||
1861 cfg.width != prev_cfg.width ||
1862 cfg.savelines != prev_cfg.savelines ||
1863 cfg.resize_action == RESIZE_FONT ||
1864 (cfg.resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
1865 cfg.resize_action == RESIZE_DISABLED)
1866 term_size(term, cfg.height, cfg.width, cfg.savelines);
1868 /* Enable or disable the scroll bar, etc */
1870 LONG nflg, flag = GetWindowLong(hwnd, GWL_STYLE);
1871 LONG nexflag, exflag =
1872 GetWindowLong(hwnd, GWL_EXSTYLE);
1875 if (cfg.alwaysontop != prev_cfg.alwaysontop) {
1876 if (cfg.alwaysontop) {
1877 nexflag |= WS_EX_TOPMOST;
1878 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1879 SWP_NOMOVE | SWP_NOSIZE);
1881 nexflag &= ~(WS_EX_TOPMOST);
1882 SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
1883 SWP_NOMOVE | SWP_NOSIZE);
1886 if (cfg.sunken_edge)
1887 nexflag |= WS_EX_CLIENTEDGE;
1889 nexflag &= ~(WS_EX_CLIENTEDGE);
1892 if (is_full_screen() ?
1893 cfg.scrollbar_in_fullscreen : cfg.scrollbar)
1896 nflg &= ~WS_VSCROLL;
1898 if (cfg.resize_action == RESIZE_DISABLED ||
1900 nflg &= ~WS_THICKFRAME;
1902 nflg |= WS_THICKFRAME;
1904 if (cfg.resize_action == RESIZE_DISABLED)
1905 nflg &= ~WS_MAXIMIZEBOX;
1907 nflg |= WS_MAXIMIZEBOX;
1909 if (nflg != flag || nexflag != exflag) {
1911 SetWindowLong(hwnd, GWL_STYLE, nflg);
1912 if (nexflag != exflag)
1913 SetWindowLong(hwnd, GWL_EXSTYLE, nexflag);
1915 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
1916 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1917 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
1925 if (cfg.resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
1930 set_title(NULL, cfg.wintitle);
1931 if (IsIconic(hwnd)) {
1933 cfg.win_name_always ? window_name :
1937 if (strcmp(cfg.font.name, prev_cfg.font.name) != 0 ||
1938 strcmp(cfg.line_codepage, prev_cfg.line_codepage) != 0 ||
1939 cfg.font.isbold != prev_cfg.font.isbold ||
1940 cfg.font.height != prev_cfg.font.height ||
1941 cfg.font.charset != prev_cfg.font.charset ||
1942 cfg.vtmode != prev_cfg.vtmode ||
1943 cfg.bold_colour != prev_cfg.bold_colour ||
1944 cfg.resize_action == RESIZE_DISABLED ||
1945 cfg.resize_action == RESIZE_EITHER ||
1946 (cfg.resize_action != prev_cfg.resize_action))
1949 InvalidateRect(hwnd, NULL, TRUE);
1950 reset_window(init_lvl);
1951 net_pending_errors();
1958 term_do_paste(term);
1965 ldisc_send(ldisc, NULL, 0, 0);
1971 WinHelp(hwnd, help_path,
1972 help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
1976 * We get this if the System menu has been activated
1983 * We get this if the System menu has been activated
1984 * using the keyboard. This might happen from within
1985 * TranslateKey, in which case it really wants to be
1986 * followed by a `space' character to actually _bring
1987 * the menu up_ rather than just sitting there in
1988 * `ready to appear' state.
1990 show_mouseptr(1); /* make sure pointer is visible */
1992 PostMessage(hwnd, WM_CHAR, ' ', 0);
1994 case IDM_FULLSCREEN:
1998 if (wParam >= IDM_SAVED_MIN && wParam <= IDM_SAVED_MAX) {
1999 SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
2001 if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
2002 int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
2005 * Ensure we haven't been sent a bogus SYSCOMMAND
2006 * which would cause us to reference invalid memory
2007 * and crash. Perhaps I'm just too paranoid here.
2009 for (j = 0; j < i; j++)
2010 if (!specials || !specials[j].name)
2013 back->special(backhandle, specials[i].code);
2014 net_pending_errors();
2020 #define X_POS(l) ((int)(short)LOWORD(l))
2021 #define Y_POS(l) ((int)(short)HIWORD(l))
2023 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2024 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2025 case WM_LBUTTONDOWN:
2026 case WM_MBUTTONDOWN:
2027 case WM_RBUTTONDOWN:
2031 if (message == WM_RBUTTONDOWN &&
2032 ((wParam & MK_CONTROL) || (cfg.mouse_is_xterm == 2))) {
2035 show_mouseptr(1); /* make sure pointer is visible */
2036 GetCursorPos(&cursorpos);
2037 TrackPopupMenu(popup_menus[CTXMENU].menu,
2038 TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2039 cursorpos.x, cursorpos.y,
2047 case WM_LBUTTONDOWN:
2051 case WM_MBUTTONDOWN:
2052 button = MBT_MIDDLE;
2055 case WM_RBUTTONDOWN:
2064 button = MBT_MIDDLE;
2072 button = press = 0; /* shouldn't happen */
2076 * Special case: in full-screen mode, if the left
2077 * button is clicked in the very top left corner of the
2078 * window, we put up the System menu instead of doing
2082 char mouse_on_hotspot = 0;
2091 mon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
2094 mi.cbSize = sizeof(MONITORINFO);
2095 GetMonitorInfo(mon, &mi);
2097 if (mi.rcMonitor.left == pt.x &&
2098 mi.rcMonitor.top == pt.y) {
2099 mouse_on_hotspot = 1;
2105 if (pt.x == 0 && pt.y == 0) {
2106 mouse_on_hotspot = 1;
2109 if (is_full_screen() && press &&
2110 button == MBT_LEFT && mouse_on_hotspot) {
2111 SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU,
2112 MAKELPARAM(pt.x, pt.y));
2119 TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
2120 wParam & MK_SHIFT, wParam & MK_CONTROL,
2124 term_mouse(term, button, translate_button(button), MA_RELEASE,
2125 TO_CHR_X(X_POS(lParam)),
2126 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
2127 wParam & MK_CONTROL, is_alt_pressed());
2135 * Add the mouse position and message time to the random
2138 noise_ultralight(lParam);
2140 if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
2141 GetCapture() == hwnd) {
2143 if (wParam & MK_LBUTTON)
2145 else if (wParam & MK_MBUTTON)
2149 term_mouse(term, b, translate_button(b), MA_DRAG,
2150 TO_CHR_X(X_POS(lParam)),
2151 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
2152 wParam & MK_CONTROL, is_alt_pressed());
2155 case WM_NCMOUSEMOVE:
2157 noise_ultralight(lParam);
2159 case WM_IGNORE_CLIP:
2160 ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */
2162 case WM_DESTROYCLIPBOARD:
2164 term_deselect(term);
2165 ignore_clip = FALSE;
2171 hdc = BeginPaint(hwnd, &p);
2173 SelectPalette(hdc, pal, TRUE);
2174 RealizePalette(hdc);
2176 term_paint(term, hdc,
2177 (p.rcPaint.left-offset_width)/font_width,
2178 (p.rcPaint.top-offset_height)/font_height,
2179 (p.rcPaint.right-offset_width-1)/font_width,
2180 (p.rcPaint.bottom-offset_height-1)/font_height,
2184 p.rcPaint.left < offset_width ||
2185 p.rcPaint.top < offset_height ||
2186 p.rcPaint.right >= offset_width + font_width*term->cols ||
2187 p.rcPaint.bottom>= offset_height + font_height*term->rows)
2189 HBRUSH fillcolour, oldbrush;
2191 fillcolour = CreateSolidBrush (
2192 colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
2193 oldbrush = SelectObject(hdc, fillcolour);
2194 edge = CreatePen(PS_SOLID, 0,
2195 colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
2196 oldpen = SelectObject(hdc, edge);
2199 * Jordan Russell reports that this apparently
2200 * ineffectual IntersectClipRect() call masks a
2201 * Windows NT/2K bug causing strange display
2202 * problems when the PuTTY window is taller than
2203 * the primary monitor. It seems harmless enough...
2205 IntersectClipRect(hdc,
2206 p.rcPaint.left, p.rcPaint.top,
2207 p.rcPaint.right, p.rcPaint.bottom);
2209 ExcludeClipRect(hdc,
2210 offset_width, offset_height,
2211 offset_width+font_width*term->cols,
2212 offset_height+font_height*term->rows);
2214 Rectangle(hdc, p.rcPaint.left, p.rcPaint.top,
2215 p.rcPaint.right, p.rcPaint.bottom);
2217 // SelectClipRgn(hdc, NULL);
2219 SelectObject(hdc, oldbrush);
2220 DeleteObject(fillcolour);
2221 SelectObject(hdc, oldpen);
2224 SelectObject(hdc, GetStockObject(SYSTEM_FONT));
2225 SelectObject(hdc, GetStockObject(WHITE_PEN));
2231 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2232 * but the only one that's likely to try to overload us is FD_READ.
2233 * This means buffering just one is fine.
2235 if (pending_netevent)
2236 enact_pending_netevent();
2238 pending_netevent = TRUE;
2239 pend_netevent_wParam = wParam;
2240 pend_netevent_lParam = lParam;
2241 if (WSAGETSELECTEVENT(lParam) != FD_READ)
2242 enact_pending_netevent();
2244 time(&last_movement);
2247 term->has_focus = TRUE;
2248 CreateCaret(hwnd, caretbm, font_width, font_height);
2250 flash_window(0); /* stop */
2257 term->has_focus = FALSE;
2259 caret_x = caret_y = -1; /* ensure caret is replaced next time */
2263 case WM_ENTERSIZEMOVE:
2264 #ifdef RDB_DEBUG_PATCH
2265 debug((27, "WM_ENTERSIZEMOVE"));
2269 need_backend_resize = FALSE;
2271 case WM_EXITSIZEMOVE:
2274 #ifdef RDB_DEBUG_PATCH
2275 debug((27, "WM_EXITSIZEMOVE"));
2277 if (need_backend_resize) {
2278 term_size(term, cfg.height, cfg.width, cfg.savelines);
2279 InvalidateRect(hwnd, NULL, TRUE);
2284 * This does two jobs:
2285 * 1) Keep the sizetip uptodate
2286 * 2) Make sure the window size is _stepped_ in units of the font size.
2288 if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
2289 int width, height, w, h, ew, eh;
2290 LPRECT r = (LPRECT) lParam;
2292 if ( !need_backend_resize && cfg.resize_action == RESIZE_EITHER &&
2293 (cfg.height != term->rows || cfg.width != term->cols )) {
2295 * Great! It seems that both the terminal size and the
2296 * font size have been changed and the user is now dragging.
2298 * It will now be difficult to get back to the configured
2301 * This would be easier but it seems to be too confusing.
2303 term_size(term, cfg.height, cfg.width, cfg.savelines);
2306 cfg.height=term->rows; cfg.width=term->cols;
2308 InvalidateRect(hwnd, NULL, TRUE);
2309 need_backend_resize = TRUE;
2312 width = r->right - r->left - extra_width;
2313 height = r->bottom - r->top - extra_height;
2314 w = (width + font_width / 2) / font_width;
2317 h = (height + font_height / 2) / font_height;
2320 UpdateSizeTip(hwnd, w, h);
2321 ew = width - w * font_width;
2322 eh = height - h * font_height;
2324 if (wParam == WMSZ_LEFT ||
2325 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
2331 if (wParam == WMSZ_TOP ||
2332 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
2342 int width, height, w, h, rv = 0;
2343 int ex_width = extra_width + (cfg.window_border - offset_width) * 2;
2344 int ex_height = extra_height + (cfg.window_border - offset_height) * 2;
2345 LPRECT r = (LPRECT) lParam;
2347 width = r->right - r->left - ex_width;
2348 height = r->bottom - r->top - ex_height;
2350 w = (width + term->cols/2)/term->cols;
2351 h = (height + term->rows/2)/term->rows;
2352 if ( r->right != r->left + w*term->cols + ex_width)
2355 if (wParam == WMSZ_LEFT ||
2356 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
2357 r->left = r->right - w*term->cols - ex_width;
2359 r->right = r->left + w*term->cols + ex_width;
2361 if (r->bottom != r->top + h*term->rows + ex_height)
2364 if (wParam == WMSZ_TOP ||
2365 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
2366 r->top = r->bottom - h*term->rows - ex_height;
2368 r->bottom = r->top + h*term->rows + ex_height;
2372 /* break; (never reached) */
2373 case WM_FULLSCR_ON_MAX:
2374 fullscr_on_max = TRUE;
2377 sys_cursor_update();
2380 #ifdef RDB_DEBUG_PATCH
2381 debug((27, "WM_SIZE %s (%d,%d)",
2382 (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
2383 (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
2384 (wParam == SIZE_RESTORED && resizing) ? "to":
2385 (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
2387 LOWORD(lParam), HIWORD(lParam)));
2389 if (wParam == SIZE_MINIMIZED)
2391 cfg.win_name_always ? window_name : icon_name);
2392 if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
2393 SetWindowText(hwnd, window_name);
2394 if (wParam == SIZE_RESTORED)
2395 clear_full_screen();
2396 if (wParam == SIZE_MAXIMIZED && fullscr_on_max) {
2397 fullscr_on_max = FALSE;
2401 if (cfg.resize_action == RESIZE_DISABLED) {
2402 /* A resize, well it better be a minimize. */
2406 int width, height, w, h;
2408 width = LOWORD(lParam);
2409 height = HIWORD(lParam);
2412 if (wParam == SIZE_MAXIMIZED && !was_zoomed) {
2414 prev_rows = term->rows;
2415 prev_cols = term->cols;
2416 if (cfg.resize_action == RESIZE_TERM) {
2417 w = width / font_width;
2419 h = height / font_height;
2422 term_size(term, h, w, cfg.savelines);
2425 } else if (wParam == SIZE_RESTORED && was_zoomed) {
2427 if (cfg.resize_action == RESIZE_TERM)
2428 term_size(term, prev_rows, prev_cols, cfg.savelines);
2429 if (cfg.resize_action != RESIZE_FONT)
2434 /* This is an unexpected resize, these will normally happen
2435 * if the window is too large. Probably either the user
2436 * selected a huge font or the screen size has changed.
2438 * This is also called with minimize.
2440 else reset_window(-1);
2444 * Don't call back->size in mid-resize. (To prevent
2445 * massive numbers of resize events getting sent
2446 * down the connection during an NT opaque drag.)
2449 if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
2450 need_backend_resize = TRUE;
2451 w = (width-cfg.window_border*2) / font_width;
2453 h = (height-cfg.window_border*2) / font_height;
2462 sys_cursor_update();
2465 switch (LOWORD(wParam)) {
2467 term_scroll(term, -1, 0);
2470 term_scroll(term, +1, 0);
2473 term_scroll(term, 0, +1);
2476 term_scroll(term, 0, -1);
2479 term_scroll(term, 0, +term->rows / 2);
2482 term_scroll(term, 0, -term->rows / 2);
2484 case SB_THUMBPOSITION:
2486 term_scroll(term, 1, HIWORD(wParam));
2490 case WM_PALETTECHANGED:
2491 if ((HWND) wParam != hwnd && pal != NULL) {
2492 HDC hdc = get_ctx(NULL);
2494 if (RealizePalette(hdc) > 0)
2500 case WM_QUERYNEWPALETTE:
2502 HDC hdc = get_ctx(NULL);
2504 if (RealizePalette(hdc) > 0)
2516 * Add the scan code and keypress timing to the random
2519 noise_ultralight(lParam);
2522 * We don't do TranslateMessage since it disassociates the
2523 * resulting CHAR message from the KEYDOWN that sparked it,
2524 * which we occasionally don't want. Instead, we process
2525 * KEYDOWN, and call the Win32 translator functions so that
2526 * we get the translations under _our_ control.
2529 unsigned char buf[20];
2532 if (wParam == VK_PROCESSKEY) {
2535 m.message = WM_KEYDOWN;
2537 m.lParam = lParam & 0xdfff;
2538 TranslateMessage(&m);
2540 len = TranslateKey(message, wParam, lParam, buf);
2542 return DefWindowProc(hwnd, message, wParam, lParam);
2546 * Interrupt an ongoing paste. I'm not sure
2547 * this is sensible, but for the moment it's
2548 * preferable to having to faff about buffering
2554 * We need not bother about stdin backlogs
2555 * here, because in GUI PuTTY we can't do
2556 * anything about it anyway; there's no means
2557 * of asking Windows to hold off on KEYDOWN
2558 * messages. We _have_ to buffer everything
2561 term_seen_key_event(term);
2562 ldisc_send(ldisc, buf, len, 1);
2567 net_pending_errors();
2569 case WM_INPUTLANGCHANGE:
2570 /* wParam == Font number */
2571 /* lParam == Locale */
2572 set_input_locale((HKL)lParam);
2573 sys_cursor_update();
2576 if(wParam == IMN_SETOPENSTATUS) {
2577 HIMC hImc = ImmGetContext(hwnd);
2578 ImmSetCompositionFont(hImc, &lfont);
2579 ImmReleaseContext(hwnd, hImc);
2583 case WM_IME_COMPOSITION:
2589 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
2590 osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
2592 if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2593 break; /* fall back to DefWindowProc */
2595 hIMC = ImmGetContext(hwnd);
2596 n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
2600 buff = snewn(n, char);
2601 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
2603 * Jaeyoun Chung reports that Korean character
2604 * input doesn't work correctly if we do a single
2605 * luni_send() covering the whole of buff. So
2606 * instead we luni_send the characters one by one.
2608 term_seen_key_event(term);
2609 for (i = 0; i < n; i += 2) {
2610 luni_send(ldisc, (unsigned short *)(buff+i), 1, 1);
2614 ImmReleaseContext(hwnd, hIMC);
2619 if (wParam & 0xFF00) {
2620 unsigned char buf[2];
2623 buf[0] = wParam >> 8;
2624 term_seen_key_event(term);
2625 lpage_send(ldisc, kbd_codepage, buf, 2, 1);
2627 char c = (unsigned char) wParam;
2628 term_seen_key_event(term);
2629 lpage_send(ldisc, kbd_codepage, &c, 1, 1);
2635 * Nevertheless, we are prepared to deal with WM_CHAR
2636 * messages, should they crop up. So if someone wants to
2637 * post the things to us as part of a macro manoeuvre,
2638 * we're ready to cope.
2641 char c = (unsigned char)wParam;
2642 term_seen_key_event(term);
2643 lpage_send(ldisc, CP_ACP, &c, 1, 1);
2647 if (send_raw_mouse && LOWORD(lParam) == HTCLIENT) {
2648 SetCursor(LoadCursor(NULL, IDC_ARROW));
2652 case WM_AGENT_CALLBACK:
2654 struct agent_callback *c = (struct agent_callback *)lParam;
2655 c->callback(c->callback_ctx, c->data, c->len);
2660 if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
2661 int shift_pressed=0, control_pressed=0;
2663 if (message == WM_MOUSEWHEEL) {
2664 wheel_accumulator += (short)HIWORD(wParam);
2665 shift_pressed=LOWORD(wParam) & MK_SHIFT;
2666 control_pressed=LOWORD(wParam) & MK_CONTROL;
2669 wheel_accumulator += (int)wParam;
2670 if (GetKeyboardState(keys)!=0) {
2671 shift_pressed=keys[VK_SHIFT]&0x80;
2672 control_pressed=keys[VK_CONTROL]&0x80;
2676 /* process events when the threshold is reached */
2677 while (abs(wheel_accumulator) >= WHEEL_DELTA) {
2680 /* reduce amount for next time */
2681 if (wheel_accumulator > 0) {
2683 wheel_accumulator -= WHEEL_DELTA;
2684 } else if (wheel_accumulator < 0) {
2686 wheel_accumulator += WHEEL_DELTA;
2690 if (send_raw_mouse &&
2691 !(cfg.mouse_override && shift_pressed)) {
2692 /* send a mouse-down followed by a mouse up */
2693 term_mouse(term, b, translate_button(b),
2695 TO_CHR_X(X_POS(lParam)),
2696 TO_CHR_Y(Y_POS(lParam)), shift_pressed,
2697 control_pressed, is_alt_pressed());
2698 term_mouse(term, b, translate_button(b),
2699 MA_RELEASE, TO_CHR_X(X_POS(lParam)),
2700 TO_CHR_Y(Y_POS(lParam)), shift_pressed,
2701 control_pressed, is_alt_pressed());
2703 /* trigger a scroll */
2704 term_scroll(term, 0,
2706 -term->rows / 2 : term->rows / 2);
2713 return DefWindowProc(hwnd, message, wParam, lParam);
2717 * Move the system caret. (We maintain one, even though it's
2718 * invisible, for the benefit of blind people: apparently some
2719 * helper software tracks the system caret, so we should arrange to
2722 void sys_cursor(void *frontend, int x, int y)
2726 if (!term->has_focus) return;
2729 * Avoid gratuitously re-updating the cursor position and IMM
2730 * window if there's no actual change required.
2732 cx = x * font_width + offset_width;
2733 cy = y * font_height + offset_height;
2734 if (cx == caret_x && cy == caret_y)
2739 sys_cursor_update();
2742 static void sys_cursor_update(void)
2747 if (!term->has_focus) return;
2749 if (caret_x < 0 || caret_y < 0)
2752 SetCaretPos(caret_x, caret_y);
2754 /* IMM calls on Win98 and beyond only */
2755 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
2757 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
2758 osVersion.dwMinorVersion == 0) return; /* 95 */
2760 /* we should have the IMM functions */
2761 hIMC = ImmGetContext(hwnd);
2762 cf.dwStyle = CFS_POINT;
2763 cf.ptCurrentPos.x = caret_x;
2764 cf.ptCurrentPos.y = caret_y;
2765 ImmSetCompositionWindow(hIMC, &cf);
2767 ImmReleaseContext(hwnd, hIMC);
2771 * Draw a line of text in the window, at given character
2772 * coordinates, in given attributes.
2774 * We are allowed to fiddle with the contents of `text'.
2776 void do_text(Context ctx, int x, int y, char *text, int len,
2777 unsigned long attr, int lattr)
2780 int nfg, nbg, nfont;
2783 int force_manual_underline = 0;
2784 int fnt_width = font_width * (1 + (lattr != LATTR_NORM));
2785 int char_width = fnt_width;
2786 int text_adjust = 0;
2787 static int *IpDx = 0, IpDxLEN = 0;
2789 if (attr & ATTR_WIDE)
2792 if (len > IpDxLEN || IpDx[0] != char_width) {
2794 if (len > IpDxLEN) {
2796 IpDx = snewn(len + 16, int);
2797 IpDxLEN = (len + 16);
2799 for (i = 0; i < IpDxLEN; i++)
2800 IpDx[i] = char_width;
2803 /* Only want the left half of double width lines */
2804 if (lattr != LATTR_NORM && x*2 >= term->cols)
2812 if ((attr & TATTR_ACTCURS) && (cfg.cursor_type == 0 || term->big_cursor)) {
2813 attr &= ATTR_CUR_AND | (bold_mode != BOLD_COLOURS ? ATTR_BOLD : 0);
2814 attr ^= ATTR_CUR_XOR;
2818 if (cfg.vtmode == VT_POORMAN && lattr != LATTR_NORM) {
2819 /* Assume a poorman font is borken in other ways too. */
2829 nfont |= FONT_WIDE + FONT_HIGH;
2832 if (attr & ATTR_NARROW)
2833 nfont |= FONT_NARROW;
2835 /* Special hack for the VT100 linedraw glyphs. */
2836 if ((attr & CSET_MASK) == 0x2300) {
2837 if (text[0] >= (char) 0xBA && text[0] <= (char) 0xBD) {
2838 switch ((unsigned char) (text[0])) {
2840 text_adjust = -2 * font_height / 5;
2843 text_adjust = -1 * font_height / 5;
2846 text_adjust = font_height / 5;
2849 text_adjust = 2 * font_height / 5;
2852 if (lattr == LATTR_TOP || lattr == LATTR_BOT)
2855 text[0] = (char) (ucsdata.unitab_xterm['q'] & CHAR_MASK);
2856 attr |= (ucsdata.unitab_xterm['q'] & CSET_MASK);
2857 if (attr & ATTR_UNDER) {
2858 attr &= ~ATTR_UNDER;
2859 force_manual_underline = 1;
2864 /* Anything left as an original character set is unprintable. */
2865 if (DIRECT_CHAR(attr)) {
2868 memset(text, 0xFD, len);
2872 if ((attr & CSET_MASK) == ATTR_OEMCP)
2875 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
2876 nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
2877 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
2878 nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
2879 if (bold_mode == BOLD_FONT && (attr & ATTR_BOLD))
2881 if (und_mode == UND_FONT && (attr & ATTR_UNDER))
2882 nfont |= FONT_UNDERLINE;
2883 another_font(nfont);
2884 if (!fonts[nfont]) {
2885 if (nfont & FONT_UNDERLINE)
2886 force_manual_underline = 1;
2887 /* Don't do the same for manual bold, it could be bad news. */
2889 nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
2891 another_font(nfont);
2893 nfont = FONT_NORMAL;
2894 if (attr & ATTR_REVERSE) {
2899 if (bold_mode == BOLD_COLOURS && (attr & ATTR_BOLD))
2901 if (bold_mode == BOLD_COLOURS && (attr & ATTR_BLINK))
2905 SelectObject(hdc, fonts[nfont]);
2906 SetTextColor(hdc, fg);
2907 SetBkColor(hdc, bg);
2908 SetBkMode(hdc, OPAQUE);
2911 line_box.right = x + char_width * len;
2912 line_box.bottom = y + font_height;
2914 /* Only want the left half of double width lines */
2915 if (line_box.right > font_width*term->cols+offset_width)
2916 line_box.right = font_width*term->cols+offset_width;
2918 /* We're using a private area for direct to font. (512 chars.) */
2919 if (ucsdata.dbcs_screenfont && (attr & CSET_MASK) == ATTR_ACP) {
2920 /* Ho Hum, dbcs fonts are a PITA! */
2921 /* To display on W9x I have to convert to UCS */
2922 static wchar_t *uni_buf = 0;
2923 static int uni_len = 0;
2925 if (len > uni_len) {
2928 uni_buf = snewn(uni_len, wchar_t);
2931 for(nlen = mptr = 0; mptr<len; mptr++) {
2932 uni_buf[nlen] = 0xFFFD;
2933 if (IsDBCSLeadByteEx(ucsdata.font_codepage, (BYTE) text[mptr])) {
2934 IpDx[nlen] += char_width;
2935 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
2936 text+mptr, 2, uni_buf+nlen, 1);
2941 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
2942 text+mptr, 1, uni_buf+nlen, 1);
2950 y - font_height * (lattr == LATTR_BOT) + text_adjust,
2951 ETO_CLIPPED | ETO_OPAQUE, &line_box, uni_buf, nlen, IpDx);
2952 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
2953 SetBkMode(hdc, TRANSPARENT);
2954 ExtTextOutW(hdc, x - 1,
2955 y - font_height * (lattr ==
2956 LATTR_BOT) + text_adjust,
2957 ETO_CLIPPED, &line_box, uni_buf, nlen, IpDx);
2961 } else if (DIRECT_FONT(attr)) {
2963 y - font_height * (lattr == LATTR_BOT) + text_adjust,
2964 ETO_CLIPPED | ETO_OPAQUE, &line_box, text, len, IpDx);
2965 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
2966 SetBkMode(hdc, TRANSPARENT);
2968 /* GRR: This draws the character outside it's box and can leave
2969 * 'droppings' even with the clip box! I suppose I could loop it
2970 * one character at a time ... yuk.
2972 * Or ... I could do a test print with "W", and use +1 or -1 for this
2973 * shift depending on if the leftmost column is blank...
2975 ExtTextOut(hdc, x - 1,
2976 y - font_height * (lattr ==
2977 LATTR_BOT) + text_adjust,
2978 ETO_CLIPPED, &line_box, text, len, IpDx);
2981 /* And 'normal' unicode characters */
2982 static WCHAR *wbuf = NULL;
2983 static int wlen = 0;
2988 wbuf = snewn(wlen, WCHAR);
2990 for (i = 0; i < len; i++)
2991 wbuf[i] = (WCHAR) ((attr & CSET_MASK) + (text[i] & CHAR_MASK));
2994 y - font_height * (lattr == LATTR_BOT) + text_adjust,
2995 ETO_CLIPPED | ETO_OPAQUE, &line_box, wbuf, len, IpDx);
2997 /* And the shadow bold hack. */
2998 if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
2999 SetBkMode(hdc, TRANSPARENT);
3000 ExtTextOutW(hdc, x - 1,
3001 y - font_height * (lattr ==
3002 LATTR_BOT) + text_adjust,
3003 ETO_CLIPPED, &line_box, wbuf, len, IpDx);
3006 if (lattr != LATTR_TOP && (force_manual_underline ||
3007 (und_mode == UND_LINE
3008 && (attr & ATTR_UNDER)))) {
3011 if (lattr == LATTR_BOT)
3012 dec = dec * 2 - font_height;
3014 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, fg));
3015 MoveToEx(hdc, x, y + dec, NULL);
3016 LineTo(hdc, x + len * char_width, y + dec);
3017 oldpen = SelectObject(hdc, oldpen);
3018 DeleteObject(oldpen);
3022 void do_cursor(Context ctx, int x, int y, char *text, int len,
3023 unsigned long attr, int lattr)
3029 int ctype = cfg.cursor_type;
3031 if ((attr & TATTR_ACTCURS) && (ctype == 0 || term->big_cursor)) {
3032 if (((attr & CSET_MASK) | (unsigned char) *text) != UCSWIDE) {
3033 do_text(ctx, x, y, text, len, attr, lattr);
3037 attr |= TATTR_RIGHTCURS;
3040 fnt_width = char_width = font_width * (1 + (lattr != LATTR_NORM));
3041 if (attr & ATTR_WIDE)
3048 if ((attr & TATTR_PASCURS) && (ctype == 0 || term->big_cursor)) {
3051 pts[0].x = pts[1].x = pts[4].x = x;
3052 pts[2].x = pts[3].x = x + char_width - 1;
3053 pts[0].y = pts[3].y = pts[4].y = y;
3054 pts[1].y = pts[2].y = y + font_height - 1;
3055 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
3056 Polyline(hdc, pts, 5);
3057 oldpen = SelectObject(hdc, oldpen);
3058 DeleteObject(oldpen);
3059 } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) && ctype != 0) {
3060 int startx, starty, dx, dy, length, i;
3063 starty = y + descent;
3066 length = char_width;
3069 if (attr & TATTR_RIGHTCURS)
3070 xadjust = char_width - 1;
3071 startx = x + xadjust;
3075 length = font_height;
3077 if (attr & TATTR_ACTCURS) {
3080 SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
3081 MoveToEx(hdc, startx, starty, NULL);
3082 LineTo(hdc, startx + dx * length, starty + dy * length);
3083 oldpen = SelectObject(hdc, oldpen);
3084 DeleteObject(oldpen);
3086 for (i = 0; i < length; i++) {
3088 SetPixel(hdc, startx, starty, colours[23]);
3097 /* This function gets the actual width of a character in the normal font.
3099 int char_width(Context ctx, int uc) {
3103 /* If the font max is the same as the font ave width then this
3104 * function is a no-op.
3106 if (!font_dualwidth) return 1;
3108 switch (uc & CSET_MASK) {
3110 uc = ucsdata.unitab_line[uc & 0xFF];
3113 uc = ucsdata.unitab_xterm[uc & 0xFF];
3116 uc = ucsdata.unitab_scoacs[uc & 0xFF];
3119 if (DIRECT_FONT(uc)) {
3120 if (ucsdata.dbcs_screenfont) return 1;
3122 /* Speedup, I know of no font where ascii is the wrong width */
3123 if ((uc&CHAR_MASK) >= ' ' && (uc&CHAR_MASK)<= '~')
3126 if ( (uc & CSET_MASK) == ATTR_ACP ) {
3127 SelectObject(hdc, fonts[FONT_NORMAL]);
3128 } else if ( (uc & CSET_MASK) == ATTR_OEMCP ) {
3129 another_font(FONT_OEM);
3130 if (!fonts[FONT_OEM]) return 0;
3132 SelectObject(hdc, fonts[FONT_OEM]);
3136 if ( GetCharWidth32(hdc, uc&CHAR_MASK, uc&CHAR_MASK, &ibuf) != 1 &&
3137 GetCharWidth(hdc, uc&CHAR_MASK, uc&CHAR_MASK, &ibuf) != 1)
3140 /* Speedup, I know of no font where ascii is the wrong width */
3141 if (uc >= ' ' && uc <= '~') return 1;
3143 SelectObject(hdc, fonts[FONT_NORMAL]);
3144 if ( GetCharWidth32W(hdc, uc, uc, &ibuf) == 1 )
3145 /* Okay that one worked */ ;
3146 else if ( GetCharWidthW(hdc, uc, uc, &ibuf) == 1 )
3147 /* This should work on 9x too, but it's "less accurate" */ ;
3152 ibuf += font_width / 2 -1;
3159 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3160 * codes. Returns number of bytes used or zero to drop the message
3161 * or -1 to forward the message to windows.
3163 static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
3164 unsigned char *output)
3167 int scan, left_alt = 0, key_down, shift_state;
3169 unsigned char *p = output;
3170 static int alt_sum = 0;
3172 HKL kbd_layout = GetKeyboardLayout(0);
3174 static WORD keys[3];
3175 static int compose_char = 0;
3176 static WPARAM compose_key = 0;
3178 r = GetKeyboardState(keystate);
3180 memset(keystate, 0, sizeof(keystate));
3183 #define SHOW_TOASCII_RESULT
3184 { /* Tell us all about key events */
3185 static BYTE oldstate[256];
3186 static int first = 1;
3190 memcpy(oldstate, keystate, sizeof(oldstate));
3193 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
3195 } else if ((HIWORD(lParam) & KF_UP)
3196 && scan == (HIWORD(lParam) & 0xFF)) {
3200 if (wParam >= VK_F1 && wParam <= VK_F20)
3201 debug(("K_F%d", wParam + 1 - VK_F1));
3214 debug(("VK_%02x", wParam));
3216 if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
3218 debug((", S%02x", scan = (HIWORD(lParam) & 0xFF)));
3220 ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
3221 if (ch >= ' ' && ch <= '~')
3222 debug((", '%c'", ch));
3224 debug((", $%02x", ch));
3227 debug((", KB0=%02x", keys[0]));
3229 debug((", KB1=%02x", keys[1]));
3231 debug((", KB2=%02x", keys[2]));
3233 if ((keystate[VK_SHIFT] & 0x80) != 0)
3235 if ((keystate[VK_CONTROL] & 0x80) != 0)
3237 if ((HIWORD(lParam) & KF_EXTENDED))
3239 if ((HIWORD(lParam) & KF_UP))
3243 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
3244 else if ((HIWORD(lParam) & KF_UP))
3245 oldstate[wParam & 0xFF] ^= 0x80;
3247 oldstate[wParam & 0xFF] ^= 0x81;
3249 for (ch = 0; ch < 256; ch++)
3250 if (oldstate[ch] != keystate[ch])
3251 debug((", M%02x=%02x", ch, keystate[ch]));
3253 memcpy(oldstate, keystate, sizeof(oldstate));
3257 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
3258 keystate[VK_RMENU] = keystate[VK_MENU];
3262 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3263 if ((cfg.funky_type == 3 ||
3264 (cfg.funky_type <= 1 && term->app_keypad_keys &&
3266 && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
3268 wParam = VK_EXECUTE;
3270 /* UnToggle NUMLock */
3271 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
3272 keystate[VK_NUMLOCK] ^= 1;
3275 /* And write back the 'adjusted' state */
3276 SetKeyboardState(keystate);
3279 /* Disable Auto repeat if required */
3280 if (term->repeat_off &&
3281 (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
3284 if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
3287 key_down = ((HIWORD(lParam) & KF_UP) == 0);
3289 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3290 if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
3291 if (cfg.ctrlaltkeys)
3292 keystate[VK_MENU] = 0;
3294 keystate[VK_RMENU] = 0x80;
3299 scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
3300 shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
3301 + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
3303 /* Note if AltGr was pressed and if it was used as a compose key */
3304 if (!compose_state) {
3305 compose_key = 0x100;
3306 if (cfg.compose_key) {
3307 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
3308 compose_key = wParam;
3310 if (wParam == VK_APPS)
3311 compose_key = wParam;
3314 if (wParam == compose_key) {
3315 if (compose_state == 0
3316 && (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0) compose_state =
3318 else if (compose_state == 1 && (HIWORD(lParam) & KF_UP))
3322 } else if (compose_state == 1 && wParam != VK_CONTROL)
3325 if (compose_state > 1 && left_alt)
3328 /* Sanitize the number pad if not using a PC NumPad */
3329 if (left_alt || (term->app_keypad_keys && !cfg.no_applic_k
3330 && cfg.funky_type != 2)
3331 || cfg.funky_type == 3 || cfg.nethack_keypad || compose_state) {
3332 if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
3336 nParam = VK_NUMPAD0;
3339 nParam = VK_NUMPAD1;
3342 nParam = VK_NUMPAD2;
3345 nParam = VK_NUMPAD3;
3348 nParam = VK_NUMPAD4;
3351 nParam = VK_NUMPAD5;
3354 nParam = VK_NUMPAD6;
3357 nParam = VK_NUMPAD7;
3360 nParam = VK_NUMPAD8;
3363 nParam = VK_NUMPAD9;
3366 nParam = VK_DECIMAL;
3370 if (keystate[VK_NUMLOCK] & 1)
3377 /* If a key is pressed and AltGr is not active */
3378 if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !compose_state) {
3379 /* Okay, prepare for most alts then ... */
3383 /* Lets see if it's a pattern we know all about ... */
3384 if (wParam == VK_PRIOR && shift_state == 1) {
3385 SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
3388 if (wParam == VK_PRIOR && shift_state == 2) {
3389 SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
3392 if (wParam == VK_NEXT && shift_state == 1) {
3393 SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
3396 if (wParam == VK_NEXT && shift_state == 2) {
3397 SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
3400 if (wParam == VK_INSERT && shift_state == 1) {
3401 term_do_paste(term);
3404 if (left_alt && wParam == VK_F4 && cfg.alt_f4) {
3407 if (left_alt && wParam == VK_SPACE && cfg.alt_space) {
3408 SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
3411 if (left_alt && wParam == VK_RETURN && cfg.fullscreenonaltenter &&
3412 (cfg.resize_action != RESIZE_DISABLED)) {
3413 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
3417 /* Control-Numlock for app-keypad mode switch */
3418 if (wParam == VK_PAUSE && shift_state == 2) {
3419 term->app_keypad_keys ^= 1;
3423 /* Nethack keypad */
3424 if (cfg.nethack_keypad && !left_alt) {
3427 *p++ = shift_state ? 'B' : 'b';
3430 *p++ = shift_state ? 'J' : 'j';
3433 *p++ = shift_state ? 'N' : 'n';
3436 *p++ = shift_state ? 'H' : 'h';
3439 *p++ = shift_state ? '.' : '.';
3442 *p++ = shift_state ? 'L' : 'l';
3445 *p++ = shift_state ? 'Y' : 'y';
3448 *p++ = shift_state ? 'K' : 'k';
3451 *p++ = shift_state ? 'U' : 'u';
3456 /* Application Keypad */
3460 if (cfg.funky_type == 3 ||
3461 (cfg.funky_type <= 1 &&
3462 term->app_keypad_keys && !cfg.no_applic_k)) switch (wParam) {
3476 if (term->app_keypad_keys && !cfg.no_applic_k)
3513 if (cfg.funky_type == 2) {
3518 } else if (shift_state)
3525 if (cfg.funky_type == 2)
3529 if (cfg.funky_type == 2)
3533 if (cfg.funky_type == 2)
3538 if (HIWORD(lParam) & KF_EXTENDED)
3543 if (term->vt52_mode) {
3544 if (xkey >= 'P' && xkey <= 'S')
3545 p += sprintf((char *) p, "\x1B%c", xkey);
3547 p += sprintf((char *) p, "\x1B?%c", xkey);
3549 p += sprintf((char *) p, "\x1BO%c", xkey);
3554 if (wParam == VK_BACK && shift_state == 0) { /* Backspace */
3555 *p++ = (cfg.bksp_is_delete ? 0x7F : 0x08);
3559 if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */
3560 /* We do the opposite of what is configured */
3561 *p++ = (cfg.bksp_is_delete ? 0x08 : 0x7F);
3565 if (wParam == VK_TAB && shift_state == 1) { /* Shift tab */
3571 if (wParam == VK_SPACE && shift_state == 2) { /* Ctrl-Space */
3575 if (wParam == VK_SPACE && shift_state == 3) { /* Ctrl-Shift-Space */
3579 if (wParam == VK_CANCEL && shift_state == 2) { /* Ctrl-Break */
3584 if (wParam == VK_PAUSE) { /* Break/Pause */
3589 /* Control-2 to Control-8 are special */
3590 if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
3591 *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
3594 if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
3598 if (shift_state == 2 && wParam == 0xDF) {
3602 if (shift_state == 3 && wParam == 0xDE) {
3603 *p++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
3606 if (shift_state == 0 && wParam == VK_RETURN && term->cr_lf_return) {
3613 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3614 * for integer decimal nn.)
3616 * We also deal with the weird ones here. Linux VCs replace F1
3617 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3618 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3624 code = (keystate[VK_SHIFT] & 0x80 ? 23 : 11);
3627 code = (keystate[VK_SHIFT] & 0x80 ? 24 : 12);
3630 code = (keystate[VK_SHIFT] & 0x80 ? 25 : 13);
3633 code = (keystate[VK_SHIFT] & 0x80 ? 26 : 14);
3636 code = (keystate[VK_SHIFT] & 0x80 ? 28 : 15);
3639 code = (keystate[VK_SHIFT] & 0x80 ? 29 : 17);
3642 code = (keystate[VK_SHIFT] & 0x80 ? 31 : 18);
3645 code = (keystate[VK_SHIFT] & 0x80 ? 32 : 19);
3648 code = (keystate[VK_SHIFT] & 0x80 ? 33 : 20);
3651 code = (keystate[VK_SHIFT] & 0x80 ? 34 : 21);
3684 if ((shift_state&2) == 0) switch (wParam) {
3704 /* Reorder edit keys to physical order */
3705 if (cfg.funky_type == 3 && code <= 6)
3706 code = "\0\2\1\4\5\3\6"[code];
3708 if (term->vt52_mode && code > 0 && code <= 6) {
3709 p += sprintf((char *) p, "\x1B%c", " HLMEIG"[code]);
3713 if (cfg.funky_type == 5 && /* SCO function keys */
3714 code >= 11 && code <= 34) {
3715 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
3718 case VK_F1: index = 0; break;
3719 case VK_F2: index = 1; break;
3720 case VK_F3: index = 2; break;
3721 case VK_F4: index = 3; break;
3722 case VK_F5: index = 4; break;
3723 case VK_F6: index = 5; break;
3724 case VK_F7: index = 6; break;
3725 case VK_F8: index = 7; break;
3726 case VK_F9: index = 8; break;
3727 case VK_F10: index = 9; break;
3728 case VK_F11: index = 10; break;
3729 case VK_F12: index = 11; break;
3731 if (keystate[VK_SHIFT] & 0x80) index += 12;
3732 if (keystate[VK_CONTROL] & 0x80) index += 24;
3733 p += sprintf((char *) p, "\x1B[%c", codes[index]);
3736 if (cfg.funky_type == 5 && /* SCO small keypad */
3737 code >= 1 && code <= 6) {
3738 char codes[] = "HL.FIG";
3742 p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
3746 if ((term->vt52_mode || cfg.funky_type == 4) && code >= 11 && code <= 24) {
3752 if (term->vt52_mode)
3753 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11 - offt);
3756 sprintf((char *) p, "\x1BO%c", code + 'P' - 11 - offt);
3759 if (cfg.funky_type == 1 && code >= 11 && code <= 15) {
3760 p += sprintf((char *) p, "\x1B[[%c", code + 'A' - 11);
3763 if (cfg.funky_type == 2 && code >= 11 && code <= 14) {
3764 if (term->vt52_mode)
3765 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11);
3767 p += sprintf((char *) p, "\x1BO%c", code + 'P' - 11);
3770 if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
3771 p += sprintf((char *) p, code == 1 ? "\x1B[H" : "\x1BOw");
3775 p += sprintf((char *) p, "\x1B[%d~", code);
3780 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
3781 * some reason seems to send VK_CLEAR to Windows...).
3803 if (term->vt52_mode)
3804 p += sprintf((char *) p, "\x1B%c", xkey);
3806 int app_flg = (term->app_cursor_keys && !cfg.no_applic_c);
3809 * RDB: VT100 & VT102 manuals both state the
3810 * app cursor keys only work if the app keypad
3813 * SGT: That may well be true, but xterm
3814 * disagrees and so does at least one
3815 * application, so I've #if'ed this out and the
3816 * behaviour is back to PuTTY's original: app
3817 * cursor and app keypad are independently
3818 * switchable modes. If anyone complains about
3819 * _this_ I'll have to put in a configurable
3822 if (!term->app_keypad_keys)
3825 /* Useful mapping of Ctrl-arrows */
3826 if (shift_state == 2)
3830 p += sprintf((char *) p, "\x1BO%c", xkey);
3832 p += sprintf((char *) p, "\x1B[%c", xkey);
3839 * Finally, deal with Return ourselves. (Win95 seems to
3840 * foul it up when Alt is pressed, for some reason.)
3842 if (wParam == VK_RETURN) { /* Return */
3848 if (left_alt && wParam >= VK_NUMPAD0 && wParam <= VK_NUMPAD9)
3849 alt_sum = alt_sum * 10 + wParam - VK_NUMPAD0;
3854 /* Okay we've done everything interesting; let windows deal with
3855 * the boring stuff */
3859 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
3860 if(cfg.xlat_capslockcyr && keystate[VK_CAPITAL] != 0) {
3862 keystate[VK_CAPITAL] = 0;
3865 r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
3866 #ifdef SHOW_TOASCII_RESULT
3867 if (r == 1 && !key_down) {
3869 if (in_utf(term) || ucsdata.dbcs_screenfont)
3870 debug((", (U+%04x)", alt_sum));
3872 debug((", LCH(%d)", alt_sum));
3874 debug((", ACH(%d)", keys[0]));
3879 for (r1 = 0; r1 < r; r1++) {
3880 debug(("%s%d", r1 ? "," : "", keys[r1]));
3889 * Interrupt an ongoing paste. I'm not sure this is
3890 * sensible, but for the moment it's preferable to
3891 * having to faff about buffering things.
3896 for (i = 0; i < r; i++) {
3897 unsigned char ch = (unsigned char) keys[i];
3899 if (compose_state == 2 && (ch & 0x80) == 0 && ch > ' ') {
3904 if (compose_state == 3 && (ch & 0x80) == 0 && ch > ' ') {
3908 if ((nc = check_compose(compose_char, ch)) == -1) {
3909 MessageBeep(MB_ICONHAND);
3913 term_seen_key_event(term);
3914 luni_send(ldisc, &keybuf, 1, 1);
3922 if (in_utf(term) || ucsdata.dbcs_screenfont) {
3924 term_seen_key_event(term);
3925 luni_send(ldisc, &keybuf, 1, 1);
3927 ch = (char) alt_sum;
3929 * We need not bother about stdin
3930 * backlogs here, because in GUI PuTTY
3931 * we can't do anything about it
3932 * anyway; there's no means of asking
3933 * Windows to hold off on KEYDOWN
3934 * messages. We _have_ to buffer
3935 * everything we're sent.
3937 term_seen_key_event(term);
3938 ldisc_send(ldisc, &ch, 1, 1);
3942 term_seen_key_event(term);
3943 lpage_send(ldisc, kbd_codepage, &ch, 1, 1);
3946 if(capsOn && ch < 0x80) {
3949 cbuf[1] = xlat_uskbd2cyrllic(ch);
3950 term_seen_key_event(term);
3951 luni_send(ldisc, cbuf+!left_alt, 1+!!left_alt, 1);
3956 term_seen_key_event(term);
3957 lpage_send(ldisc, kbd_codepage,
3958 cbuf+!left_alt, 1+!!left_alt, 1);
3964 /* This is so the ALT-Numpad and dead keys work correctly. */
3969 /* If we're definitly not building up an ALT-54321 then clear it */
3972 /* If we will be using alt_sum fix the 256s */
3973 else if (keys[0] && (in_utf(term) || ucsdata.dbcs_screenfont))
3978 * ALT alone may or may not want to bring up the System menu.
3979 * If it's not meant to, we return 0 on presses or releases of
3980 * ALT, to show that we've swallowed the keystroke. Otherwise
3981 * we return -1, which means Windows will give the keystroke
3982 * its default handling (i.e. bring up the System menu).
3984 if (wParam == VK_MENU && !cfg.alt_only)
3990 void request_paste(void *frontend)
3993 * In Windows, pasting is synchronous: we can read the
3994 * clipboard with no difficulty, so request_paste() can just go
3997 term_do_paste(term);
4000 void set_title(void *frontend, char *title)
4003 window_name = snewn(1 + strlen(title), char);
4004 strcpy(window_name, title);
4005 if (cfg.win_name_always || !IsIconic(hwnd))
4006 SetWindowText(hwnd, title);
4009 void set_icon(void *frontend, char *title)
4012 icon_name = snewn(1 + strlen(title), char);
4013 strcpy(icon_name, title);
4014 if (!cfg.win_name_always && IsIconic(hwnd))
4015 SetWindowText(hwnd, title);
4018 void set_sbar(void *frontend, int total, int start, int page)
4022 if (is_full_screen() ? !cfg.scrollbar_in_fullscreen : !cfg.scrollbar)
4025 si.cbSize = sizeof(si);
4026 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
4028 si.nMax = total - 1;
4032 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
4035 Context get_ctx(void *frontend)
4041 SelectPalette(hdc, pal, FALSE);
4047 void free_ctx(Context ctx)
4049 SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
4050 ReleaseDC(hwnd, ctx);
4053 static void real_palette_set(int n, int r, int g, int b)
4056 logpal->palPalEntry[n].peRed = r;
4057 logpal->palPalEntry[n].peGreen = g;
4058 logpal->palPalEntry[n].peBlue = b;
4059 logpal->palPalEntry[n].peFlags = PC_NOCOLLAPSE;
4060 colours[n] = PALETTERGB(r, g, b);
4061 SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
4063 colours[n] = RGB(r, g, b);
4066 void palette_set(void *frontend, int n, int r, int g, int b)
4068 static const int first[21] = {
4069 0, 2, 4, 6, 8, 10, 12, 14,
4070 1, 3, 5, 7, 9, 11, 13, 15,
4073 real_palette_set(first[n], r, g, b);
4075 real_palette_set(first[n] + 1, r, g, b);
4077 HDC hdc = get_ctx(frontend);
4078 UnrealizeObject(pal);
4079 RealizePalette(hdc);
4084 void palette_reset(void *frontend)
4088 for (i = 0; i < NCOLOURS; i++) {
4090 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
4091 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
4092 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
4093 logpal->palPalEntry[i].peFlags = 0;
4094 colours[i] = PALETTERGB(defpal[i].rgbtRed,
4095 defpal[i].rgbtGreen,
4096 defpal[i].rgbtBlue);
4098 colours[i] = RGB(defpal[i].rgbtRed,
4099 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
4104 SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
4105 hdc = get_ctx(frontend);
4106 RealizePalette(hdc);
4111 void write_aclip(void *frontend, char *data, int len, int must_deselect)
4116 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
4119 lock = GlobalLock(clipdata);
4122 memcpy(lock, data, len);
4123 ((unsigned char *) lock)[len] = 0;
4124 GlobalUnlock(clipdata);
4127 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
4129 if (OpenClipboard(hwnd)) {
4131 SetClipboardData(CF_TEXT, clipdata);
4134 GlobalFree(clipdata);
4137 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
4141 * Note: unlike write_aclip() this will not append a nul.
4143 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
4145 HGLOBAL clipdata, clipdata2, clipdata3;
4147 void *lock, *lock2, *lock3;
4149 len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
4151 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
4152 len * sizeof(wchar_t));
4153 clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
4155 if (!clipdata || !clipdata2) {
4157 GlobalFree(clipdata);
4159 GlobalFree(clipdata2);
4162 if (!(lock = GlobalLock(clipdata)))
4164 if (!(lock2 = GlobalLock(clipdata2)))
4167 memcpy(lock, data, len * sizeof(wchar_t));
4168 WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
4170 if (cfg.rtf_paste) {
4171 wchar_t unitab[256];
4173 unsigned char *tdata = (unsigned char *)lock2;
4174 wchar_t *udata = (wchar_t *)lock;
4175 int rtflen = 0, uindex = 0, tindex = 0;
4177 int multilen, blen, alen, totallen, i;
4178 char before[16], after[4];
4180 get_unitab(CP_ACP, unitab, 0);
4182 rtfsize = 100 + strlen(cfg.font.name);
4183 rtf = snewn(rtfsize, char);
4184 sprintf(rtf, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
4185 GetACP(), cfg.font.name);
4186 rtflen = strlen(rtf);
4189 * We want to construct a piece of RTF that specifies the
4190 * same Unicode text. To do this we will read back in
4191 * parallel from the Unicode data in `udata' and the
4192 * non-Unicode data in `tdata'. For each character in
4193 * `tdata' which becomes the right thing in `udata' when
4194 * looked up in `unitab', we just copy straight over from
4195 * tdata. For each one that doesn't, we must WCToMB it
4196 * individually and produce a \u escape sequence.
4198 * It would probably be more robust to just bite the bullet
4199 * and WCToMB each individual Unicode character one by one,
4200 * then MBToWC each one back to see if it was an accurate
4201 * translation; but that strikes me as a horrifying number
4202 * of Windows API calls so I want to see if this faster way
4203 * will work. If it screws up badly we can always revert to
4204 * the simple and slow way.
4206 while (tindex < len2 && uindex < len &&
4207 tdata[tindex] && udata[uindex]) {
4208 if (tindex + 1 < len2 &&
4209 tdata[tindex] == '\r' &&
4210 tdata[tindex+1] == '\n') {
4214 if (unitab[tdata[tindex]] == udata[uindex]) {
4220 multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
4221 NULL, 0, NULL, NULL);
4222 if (multilen != 1) {
4223 blen = sprintf(before, "{\\uc%d\\u%d", multilen,
4225 alen = 1; strcpy(after, "}");
4227 blen = sprintf(before, "\\u%d", udata[uindex]);
4228 alen = 0; after[0] = '\0';
4231 assert(tindex + multilen <= len2);
4232 totallen = blen + alen;
4233 for (i = 0; i < multilen; i++) {
4234 if (tdata[tindex+i] == '\\' ||
4235 tdata[tindex+i] == '{' ||
4236 tdata[tindex+i] == '}')
4238 else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A)
4239 totallen += 6; /* \par\r\n */
4240 else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20)
4246 if (rtfsize < rtflen + totallen + 3) {
4247 rtfsize = rtflen + totallen + 512;
4248 rtf = sresize(rtf, rtfsize, char);
4251 strcpy(rtf + rtflen, before); rtflen += blen;
4252 for (i = 0; i < multilen; i++) {
4253 if (tdata[tindex+i] == '\\' ||
4254 tdata[tindex+i] == '{' ||
4255 tdata[tindex+i] == '}') {
4256 rtf[rtflen++] = '\\';
4257 rtf[rtflen++] = tdata[tindex+i];
4258 } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
4259 rtflen += sprintf(rtf+rtflen, "\\par\r\n");
4260 } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
4261 rtflen += sprintf(rtf+rtflen, "\\'%02x", tdata[tindex+i]);
4263 rtf[rtflen++] = tdata[tindex+i];
4266 strcpy(rtf + rtflen, after); rtflen += alen;
4272 strcpy(rtf + rtflen, "}");
4275 clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtflen);
4276 if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
4278 GlobalUnlock(clipdata3);
4284 GlobalUnlock(clipdata);
4285 GlobalUnlock(clipdata2);
4288 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
4290 if (OpenClipboard(hwnd)) {
4292 SetClipboardData(CF_UNICODETEXT, clipdata);
4293 SetClipboardData(CF_TEXT, clipdata2);
4295 SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
4298 GlobalFree(clipdata);
4299 GlobalFree(clipdata2);
4303 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
4306 void get_clip(void *frontend, wchar_t ** p, int *len)
4308 static HGLOBAL clipdata = NULL;
4309 static wchar_t *converted = 0;
4318 GlobalUnlock(clipdata);
4321 } else if (OpenClipboard(NULL)) {
4322 if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
4324 *p = GlobalLock(clipdata);
4326 for (p2 = *p; *p2; p2++);
4330 } else if ( (clipdata = GetClipboardData(CF_TEXT)) ) {
4334 s = GlobalLock(clipdata);
4335 i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
4336 *p = converted = snewn(i, wchar_t);
4337 MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, converted, i);
4350 * Move `lines' lines from position `from' to position `to' in the
4353 void optimised_move(void *frontend, int to, int from, int lines)
4358 min = (to < from ? to : from);
4359 max = to + from - min;
4361 r.left = offset_width;
4362 r.right = offset_width + term->cols * font_width;
4363 r.top = offset_height + min * font_height;
4364 r.bottom = offset_height + (max + lines) * font_height;
4365 ScrollWindow(hwnd, 0, (to - from) * font_height, &r, &r);
4370 * Print a message box and perform a fatal exit.
4372 void fatalbox(char *fmt, ...)
4375 char *stuff, morestuff[100];
4378 stuff = dupvprintf(fmt, ap);
4380 sprintf(morestuff, "%.70s Fatal Error", appname);
4381 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
4387 * Print a modal (Really Bad) message box and perform a fatal exit.
4389 void modalfatalbox(char *fmt, ...)
4392 char *stuff, morestuff[100];
4395 stuff = dupvprintf(fmt, ap);
4397 sprintf(morestuff, "%.70s Fatal Error", appname);
4398 MessageBox(hwnd, stuff, morestuff,
4399 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
4405 * Manage window caption / taskbar flashing, if enabled.
4406 * 0 = stop, 1 = maintain, 2 = start
4408 static void flash_window(int mode)
4410 static long last_flash = 0;
4411 static int flashing = 0;
4412 if ((mode == 0) || (cfg.beep_ind == B_IND_DISABLED)) {
4415 FlashWindow(hwnd, FALSE);
4419 } else if (mode == 2) {
4422 last_flash = GetTickCount();
4424 FlashWindow(hwnd, TRUE);
4427 } else if ((mode == 1) && (cfg.beep_ind == B_IND_FLASH)) {
4430 long now = GetTickCount();
4431 long fdiff = now - last_flash;
4432 if (fdiff < 0 || fdiff > 450) {
4434 FlashWindow(hwnd, TRUE); /* toggle */
4443 void beep(void *frontend, int mode)
4445 if (mode == BELL_DEFAULT) {
4447 * For MessageBeep style bells, we want to be careful of
4448 * timing, because they don't have the nice property of
4449 * PlaySound bells that each one cancels the previous
4450 * active one. So we limit the rate to one per 50ms or so.
4452 static long lastbeep = 0;
4455 beepdiff = GetTickCount() - lastbeep;
4456 if (beepdiff >= 0 && beepdiff < 50)
4460 * The above MessageBeep call takes time, so we record the
4461 * time _after_ it finishes rather than before it starts.
4463 lastbeep = GetTickCount();
4464 } else if (mode == BELL_WAVEFILE) {
4465 if (!PlaySound(cfg.bell_wavefile.path, NULL,
4466 SND_ASYNC | SND_FILENAME)) {
4467 char buf[sizeof(cfg.bell_wavefile.path) + 80];
4469 sprintf(buf, "Unable to play sound file\n%s\n"
4470 "Using default sound instead", cfg.bell_wavefile.path);
4471 sprintf(otherbuf, "%.70s Sound Error", appname);
4472 MessageBox(hwnd, buf, otherbuf,
4473 MB_OK | MB_ICONEXCLAMATION);
4474 cfg.beep = BELL_DEFAULT;
4476 } else if (mode == BELL_PCSPEAKER) {
4477 static long lastbeep = 0;
4480 beepdiff = GetTickCount() - lastbeep;
4481 if (beepdiff >= 0 && beepdiff < 50)
4485 * We must beep in different ways depending on whether this
4486 * is a 95-series or NT-series OS.
4488 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
4492 lastbeep = GetTickCount();
4494 /* Otherwise, either visual bell or disabled; do nothing here */
4495 if (!term->has_focus) {
4496 flash_window(2); /* start */
4501 * Minimise or restore the window in response to a server-side
4504 void set_iconic(void *frontend, int iconic)
4506 if (IsIconic(hwnd)) {
4508 ShowWindow(hwnd, SW_RESTORE);
4511 ShowWindow(hwnd, SW_MINIMIZE);
4516 * Move the window in response to a server-side request.
4518 void move_window(void *frontend, int x, int y)
4520 if (cfg.resize_action == RESIZE_DISABLED ||
4521 cfg.resize_action == RESIZE_FONT ||
4525 SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
4529 * Move the window to the top or bottom of the z-order in response
4530 * to a server-side request.
4532 void set_zorder(void *frontend, int top)
4534 if (cfg.alwaysontop)
4535 return; /* ignore */
4536 SetWindowPos(hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
4537 SWP_NOMOVE | SWP_NOSIZE);
4541 * Refresh the window in response to a server-side request.
4543 void refresh_window(void *frontend)
4545 InvalidateRect(hwnd, NULL, TRUE);
4549 * Maximise or restore the window in response to a server-side
4552 void set_zoomed(void *frontend, int zoomed)
4554 if (IsZoomed(hwnd)) {
4556 ShowWindow(hwnd, SW_RESTORE);
4559 ShowWindow(hwnd, SW_MAXIMIZE);
4564 * Report whether the window is iconic, for terminal reports.
4566 int is_iconic(void *frontend)
4568 return IsIconic(hwnd);
4572 * Report the window's position, for terminal reports.
4574 void get_window_pos(void *frontend, int *x, int *y)
4577 GetWindowRect(hwnd, &r);
4583 * Report the window's pixel size, for terminal reports.
4585 void get_window_pixels(void *frontend, int *x, int *y)
4588 GetWindowRect(hwnd, &r);
4589 *x = r.right - r.left;
4590 *y = r.bottom - r.top;
4594 * Return the window or icon title.
4596 char *get_window_title(void *frontend, int icon)
4598 return icon ? icon_name : window_name;
4602 * See if we're in full-screen mode.
4604 int is_full_screen()
4606 if (!IsZoomed(hwnd))
4608 if (GetWindowLong(hwnd, GWL_STYLE) & WS_CAPTION)
4613 /* Get the rect/size of a full screen window using the nearest available
4614 * monitor in multimon systems; default to something sensible if only
4615 * one monitor is present. */
4616 static int get_fullscreen_rect(RECT * ss)
4618 #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
4621 mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
4622 mi.cbSize = sizeof(mi);
4623 GetMonitorInfo(mon, &mi);
4625 /* structure copy */
4629 /* could also use code like this:
4630 ss->left = ss->top = 0;
4631 ss->right = GetSystemMetrics(SM_CXSCREEN);
4632 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
4634 return GetClientRect(GetDesktopWindow(), ss);
4640 * Go full-screen. This should only be called when we are already
4643 void make_full_screen()
4648 assert(IsZoomed(hwnd));
4650 if (is_full_screen())
4653 /* Remove the window furniture. */
4654 style = GetWindowLong(hwnd, GWL_STYLE);
4655 style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
4656 if (cfg.scrollbar_in_fullscreen)
4657 style |= WS_VSCROLL;
4659 style &= ~WS_VSCROLL;
4660 SetWindowLong(hwnd, GWL_STYLE, style);
4662 /* Resize ourselves to exactly cover the nearest monitor. */
4663 get_fullscreen_rect(&ss);
4664 SetWindowPos(hwnd, HWND_TOP, ss.left, ss.top,
4669 /* Tick the menu item in the System menu. */
4670 CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
4675 * Clear the full-screen attributes.
4677 void clear_full_screen()
4679 DWORD oldstyle, style;
4681 /* Reinstate the window furniture. */
4682 style = oldstyle = GetWindowLong(hwnd, GWL_STYLE);
4683 style |= WS_CAPTION | WS_BORDER;
4684 if (cfg.resize_action == RESIZE_DISABLED)
4685 style &= ~WS_THICKFRAME;
4687 style |= WS_THICKFRAME;
4689 style |= WS_VSCROLL;
4691 style &= ~WS_VSCROLL;
4692 if (style != oldstyle) {
4693 SetWindowLong(hwnd, GWL_STYLE, style);
4694 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
4695 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
4699 /* Untick the menu item in the System menu. */
4700 CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
4705 * Toggle full-screen mode.
4707 void flip_full_screen()
4709 if (is_full_screen()) {
4710 ShowWindow(hwnd, SW_RESTORE);
4711 } else if (IsZoomed(hwnd)) {
4714 SendMessage(hwnd, WM_FULLSCR_ON_MAX, 0, 0);
4715 ShowWindow(hwnd, SW_MAXIMIZE);
4719 void frontend_keypress(void *handle)
4722 * Keypress termination in non-Close-On-Exit mode is not
4723 * currently supported in PuTTY proper, because the window
4724 * always has a perfectly good Close button anyway. So we do
4730 int from_backend(void *frontend, int is_stderr, const char *data, int len)
4732 return term_data(term, is_stderr, data, len);
4735 void agent_schedule_callback(void (*callback)(void *, void *, int),
4736 void *callback_ctx, void *data, int len)
4738 struct agent_callback *c = snew(struct agent_callback);
4739 c->callback = callback;
4740 c->callback_ctx = callback_ctx;
4743 PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);