]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - window.c
ccebea778cdf43a37b9740a5d0c50fc8756f58fd
[PuTTY.git] / window.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <ctype.h>
4 #include <time.h>
5 #include <assert.h>
6
7 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
8 #include "putty.h"
9 #include "terminal.h"
10 #include "storage.h"
11 #include "win_res.h"
12
13 #ifndef NO_MULTIMON
14 #if WINVER < 0x0500
15 #define COMPILE_MULTIMON_STUBS
16 #include <multimon.h>
17 #endif
18 #endif
19
20 #include <imm.h>
21 #include <commctrl.h>
22 #include <richedit.h>
23 #include <mmsystem.h>
24
25 /* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
26  * wParam are used by Windows, and should be masked off, so we shouldn't
27  * attempt to store information in them. Hence all these identifiers have
28  * the low 4 bits clear. Also, identifiers should < 0xF000. */
29
30 #define IDM_SHOWLOG   0x0010
31 #define IDM_NEWSESS   0x0020
32 #define IDM_DUPSESS   0x0030
33 #define IDM_RESTART   0x0040
34 #define IDM_RECONF    0x0050
35 #define IDM_CLRSB     0x0060
36 #define IDM_RESET     0x0070
37 #define IDM_HELP      0x0140
38 #define IDM_ABOUT     0x0150
39 #define IDM_SAVEDSESS 0x0160
40 #define IDM_COPYALL   0x0170
41 #define IDM_FULLSCREEN  0x0180
42 #define IDM_PASTE     0x0190
43
44 #define IDM_SPECIAL_MIN 0x0400
45 #define IDM_SPECIAL_MAX 0x0800
46
47 #define IDM_SAVED_MIN 0x1000
48 #define IDM_SAVED_MAX 0x5000
49 #define MENU_SAVED_STEP 16
50 /* Maximum number of sessions on saved-session submenu */
51 #define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
52
53 #define WM_IGNORE_CLIP (WM_XUSER + 2)
54 #define WM_FULLSCR_ON_MAX (WM_XUSER + 3)
55 #define WM_AGENT_CALLBACK (WM_XUSER + 4)
56
57 /* Needed for Chinese support and apparently not always defined. */
58 #ifndef VK_PROCESSKEY
59 #define VK_PROCESSKEY 0xE5
60 #endif
61
62 /* Mouse wheel support. */
63 #ifndef WM_MOUSEWHEEL
64 #define WM_MOUSEWHEEL 0x020A           /* not defined in earlier SDKs */
65 #endif
66 #ifndef WHEEL_DELTA
67 #define WHEEL_DELTA 120
68 #endif
69
70 static Mouse_Button translate_button(Mouse_Button button);
71 static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
72 static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
73                         unsigned char *output);
74 static void cfgtopalette(void);
75 static void systopalette(void);
76 static void init_palette(void);
77 static void init_fonts(int, int);
78 static void another_font(int);
79 static void deinit_fonts(void);
80 static void set_input_locale(HKL);
81
82 static int is_full_screen(void);
83 static void make_full_screen(void);
84 static void clear_full_screen(void);
85 static void flip_full_screen(void);
86
87 /* Window layout information */
88 static void reset_window(int);
89 static int extra_width, extra_height;
90 static int font_width, font_height, font_dualwidth;
91 static int offset_width, offset_height;
92 static int was_zoomed = 0;
93 static int prev_rows, prev_cols;
94   
95 static int pending_netevent = 0;
96 static WPARAM pend_netevent_wParam = 0;
97 static LPARAM pend_netevent_lParam = 0;
98 static void enact_pending_netevent(void);
99 static void flash_window(int mode);
100 static void sys_cursor_update(void);
101 static int is_shift_pressed(void);
102 static int get_fullscreen_rect(RECT * ss);
103
104 static time_t last_movement = 0;
105
106 static int caret_x = -1, caret_y = -1;
107
108 static int kbd_codepage;
109
110 static void *ldisc;
111 static Backend *back;
112 static void *backhandle;
113
114 static struct unicode_data ucsdata;
115 static int session_closed;
116
117 static const struct telnet_special *specials;
118 static int n_specials;
119
120 static struct {
121     HMENU menu;
122     int specials_submenu_pos;
123 } popup_menus[2];
124 enum { SYSMENU, CTXMENU };
125
126 Config cfg;                            /* exported to windlg.c */
127
128 extern struct sesslist sesslist;       /* imported from windlg.c */
129
130 struct agent_callback {
131     void (*callback)(void *, void *, int);
132     void *callback_ctx;
133     void *data;
134     int len;
135 };
136
137 #define FONT_NORMAL 0
138 #define FONT_BOLD 1
139 #define FONT_UNDERLINE 2
140 #define FONT_BOLDUND 3
141 #define FONT_WIDE       0x04
142 #define FONT_HIGH       0x08
143 #define FONT_NARROW     0x10
144
145 #define FONT_OEM        0x20
146 #define FONT_OEMBOLD    0x21
147 #define FONT_OEMUND     0x22
148 #define FONT_OEMBOLDUND 0x23
149
150 #define FONT_MAXNO      0x2F
151 #define FONT_SHIFT      5
152 static HFONT fonts[FONT_MAXNO];
153 static LOGFONT lfont;
154 static int fontflag[FONT_MAXNO];
155 static enum {
156     BOLD_COLOURS, BOLD_SHADOW, BOLD_FONT
157 } bold_mode;
158 static enum {
159     UND_LINE, UND_FONT
160 } und_mode;
161 static int descent;
162
163 #define NCOLOURS 24
164 static COLORREF colours[NCOLOURS];
165 static HPALETTE pal;
166 static LPLOGPALETTE logpal;
167 static RGBTRIPLE defpal[NCOLOURS];
168
169 static HWND hwnd;
170
171 static HBITMAP caretbm;
172
173 static int dbltime, lasttime, lastact;
174 static Mouse_Button lastbtn;
175
176 /* this allows xterm-style mouse handling. */
177 static int send_raw_mouse = 0;
178 static int wheel_accumulator = 0;
179
180 static char *window_name, *icon_name;
181
182 static int compose_state = 0;
183
184 static UINT wm_mousewheel = WM_MOUSEWHEEL;
185
186 /* Dummy routine, only required in plink. */
187 void ldisc_update(void *frontend, int echo, int edit)
188 {
189 }
190
191 static void start_backend(void)
192 {
193     const char *error;
194     char msg[1024], *title;
195     char *realhost;
196     int i;
197
198     /*
199      * Select protocol. This is farmed out into a table in a
200      * separate file to enable an ssh-free variant.
201      */
202     back = NULL;
203     for (i = 0; backends[i].backend != NULL; i++)
204         if (backends[i].protocol == cfg.protocol) {
205             back = backends[i].backend;
206             break;
207         }
208     if (back == NULL) {
209         char *str = dupprintf("%s Internal Error", appname);
210         MessageBox(NULL, "Unsupported protocol number found",
211                    str, MB_OK | MB_ICONEXCLAMATION);
212         sfree(str);
213         cleanup_exit(1);
214     }
215
216     error = back->init(NULL, &backhandle, &cfg,
217                        cfg.host, cfg.port, &realhost, cfg.tcp_nodelay,
218                        cfg.tcp_keepalives);
219     back->provide_logctx(backhandle, logctx);
220     if (error) {
221         char *str = dupprintf("%s Error", appname);
222         sprintf(msg, "Unable to open connection to\n"
223                 "%.800s\n" "%s", cfg.host, error);
224         MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
225         sfree(str);
226         exit(0);
227     }
228     window_name = icon_name = NULL;
229     if (*cfg.wintitle) {
230         title = cfg.wintitle;
231     } else {
232         sprintf(msg, "%s - %s", realhost, appname);
233         title = msg;
234     }
235     sfree(realhost);
236     set_title(NULL, title);
237     set_icon(NULL, title);
238
239     /*
240      * Connect the terminal to the backend for resize purposes.
241      */
242     term_provide_resize_fn(term, back->size, backhandle);
243
244     /*
245      * Set up a line discipline.
246      */
247     ldisc = ldisc_create(&cfg, term, back, backhandle, NULL);
248
249     /*
250      * Destroy the Restart Session menu item. (This will return
251      * failure if it's already absent, as it will be the very first
252      * time we call this function. We ignore that, because as long
253      * as the menu item ends up not being there, we don't care
254      * whether it was us who removed it or not!)
255      */
256     for (i = 0; i < lenof(popup_menus); i++) {
257         DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
258     }
259
260     session_closed = FALSE;
261 }
262
263 static void close_session(void)
264 {
265     char morestuff[100];
266     int i;
267
268     session_closed = TRUE;
269     sprintf(morestuff, "%.70s (inactive)", appname);
270     set_icon(NULL, morestuff);
271     set_title(NULL, morestuff);
272
273     if (ldisc) {
274         ldisc_free(ldisc);
275         ldisc = NULL;
276     }
277     if (back) {
278         back->free(backhandle);
279         backhandle = NULL;
280         back = NULL;
281         update_specials_menu(NULL);
282     }
283
284     /*
285      * Show the Restart Session menu item. Do a precautionary
286      * delete first to ensure we never end up with more than one.
287      */
288     for (i = 0; i < lenof(popup_menus); i++) {
289         DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
290         InsertMenu(popup_menus[i].menu, IDM_DUPSESS, MF_BYCOMMAND | MF_ENABLED,
291                    IDM_RESTART, "&Restart Session");
292     }
293 }
294
295 int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
296 {
297     WNDCLASS wndclass;
298     MSG msg;
299     int guess_width, guess_height;
300
301     hinst = inst;
302     flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
303
304     sk_init();
305
306     InitCommonControls();
307
308     /* Ensure a Maximize setting in Explorer doesn't maximise the
309      * config box. */
310     defuse_showwindow();
311
312     if (!init_winver())
313     {
314         char *str = dupprintf("%s Fatal Error", appname);
315         MessageBox(NULL, "Windows refuses to report a version",
316                    str, MB_OK | MB_ICONEXCLAMATION);
317         sfree(str);
318         return 1;
319     }
320
321     /*
322      * If we're running a version of Windows that doesn't support
323      * WM_MOUSEWHEEL, find out what message number we should be
324      * using instead.
325      */
326     if (osVersion.dwMajorVersion < 4 ||
327         (osVersion.dwMajorVersion == 4 && 
328          osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT))
329         wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
330
331     /*
332      * See if we can find our Help file.
333      */
334     {
335         char b[2048], *p, *q, *r;
336         FILE *fp;
337         GetModuleFileName(NULL, b, sizeof(b) - 1);
338         r = b;
339         p = strrchr(b, '\\');
340         if (p && p >= r) r = p+1;
341         q = strrchr(b, ':');
342         if (q && q >= r) r = q+1;
343         strcpy(r, "putty.hlp");
344         if ( (fp = fopen(b, "r")) != NULL) {
345             help_path = dupstr(b);
346             fclose(fp);
347         } else
348             help_path = NULL;
349         strcpy(r, "putty.cnt");
350         if ( (fp = fopen(b, "r")) != NULL) {
351             help_has_contents = TRUE;
352             fclose(fp);
353         } else
354             help_has_contents = FALSE;
355     }
356
357     /*
358      * Process the command line.
359      */
360     {
361         char *p;
362         int got_host = 0;
363
364         default_protocol = be_default_protocol;
365         /* Find the appropriate default port. */
366         {
367             int i;
368             default_port = 0; /* illegal */
369             for (i = 0; backends[i].backend != NULL; i++)
370                 if (backends[i].protocol == default_protocol) {
371                     default_port = backends[i].backend->default_port;
372                     break;
373                 }
374         }
375         cfg.logtype = LGTYP_NONE;
376
377         do_defaults(NULL, &cfg);
378
379         p = cmdline;
380
381         /*
382          * Process a couple of command-line options which are more
383          * easily dealt with before the line is broken up into
384          * words. These are the soon-to-be-defunct @sessionname and
385          * the internal-use-only &sharedmemoryhandle, neither of
386          * which are combined with anything else.
387          */
388         while (*p && isspace(*p))
389             p++;
390         if (*p == '@') {
391             int i = strlen(p);
392             while (i > 1 && isspace(p[i - 1]))
393                 i--;
394             p[i] = '\0';
395             do_defaults(p + 1, &cfg);
396             if (!*cfg.host && !do_config()) {
397                 cleanup_exit(0);
398             }
399         } else if (*p == '&') {
400             /*
401              * An initial & means we've been given a command line
402              * containing the hex value of a HANDLE for a file
403              * mapping object, which we must then extract as a
404              * config.
405              */
406             HANDLE filemap;
407             Config *cp;
408             if (sscanf(p + 1, "%p", &filemap) == 1 &&
409                 (cp = MapViewOfFile(filemap, FILE_MAP_READ,
410                                     0, 0, sizeof(Config))) != NULL) {
411                 cfg = *cp;
412                 UnmapViewOfFile(cp);
413                 CloseHandle(filemap);
414             } else if (!do_config()) {
415                 cleanup_exit(0);
416             }
417         } else {
418             /*
419              * Otherwise, break up the command line and deal with
420              * it sensibly.
421              */
422             int argc, i;
423             char **argv;
424             
425             split_into_argv(cmdline, &argc, &argv, NULL);
426
427             for (i = 0; i < argc; i++) {
428                 char *p = argv[i];
429                 int ret;
430
431                 ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
432                                             1, &cfg);
433                 if (ret == -2) {
434                     cmdline_error("option \"%s\" requires an argument", p);
435                 } else if (ret == 2) {
436                     i++;               /* skip next argument */
437                 } else if (ret == 1) {
438                     continue;          /* nothing further needs doing */
439                 } else if (!strcmp(p, "-cleanup")) {
440                     /*
441                      * `putty -cleanup'. Remove all registry
442                      * entries associated with PuTTY, and also find
443                      * and delete the random seed file.
444                      */
445                     char *s1, *s2;
446                     s1 = dupprintf("This procedure will remove ALL Registry\n"
447                                    "entries associated with %s, and will\n"
448                                    "also remove the random seed file.\n"
449                                    "\n"
450                                    "THIS PROCESS WILL DESTROY YOUR SAVED\n"
451                                    "SESSIONS. Are you really sure you want\n"
452                                    "to continue?", appname);
453                     s2 = dupprintf("%s Warning", appname);
454                     if (MessageBox(NULL, s1, s2,
455                                    MB_YESNO | MB_ICONWARNING) == IDYES) {
456                         cleanup_all();
457                     }
458                     sfree(s1);
459                     sfree(s2);
460                     exit(0);
461                 } else if (*p != '-') {
462                     char *q = p;
463                     if (got_host) {
464                         /*
465                          * If we already have a host name, treat
466                          * this argument as a port number. NB we
467                          * have to treat this as a saved -P
468                          * argument, so that it will be deferred
469                          * until it's a good moment to run it.
470                          */
471                         int ret = cmdline_process_param("-P", p, 1, &cfg);
472                         assert(ret == 2);
473                     } else if (!strncmp(q, "telnet:", 7)) {
474                         /*
475                          * If the hostname starts with "telnet:",
476                          * set the protocol to Telnet and process
477                          * the string as a Telnet URL.
478                          */
479                         char c;
480
481                         q += 7;
482                         if (q[0] == '/' && q[1] == '/')
483                             q += 2;
484                         cfg.protocol = PROT_TELNET;
485                         p = q;
486                         while (*p && *p != ':' && *p != '/')
487                             p++;
488                         c = *p;
489                         if (*p)
490                             *p++ = '\0';
491                         if (c == ':')
492                             cfg.port = atoi(p);
493                         else
494                             cfg.port = -1;
495                         strncpy(cfg.host, q, sizeof(cfg.host) - 1);
496                         cfg.host[sizeof(cfg.host) - 1] = '\0';
497                         got_host = 1;
498                     } else {
499                         /*
500                          * Otherwise, treat this argument as a host
501                          * name.
502                          */
503                         while (*p && !isspace(*p))
504                             p++;
505                         if (*p)
506                             *p++ = '\0';
507                         strncpy(cfg.host, q, sizeof(cfg.host) - 1);
508                         cfg.host[sizeof(cfg.host) - 1] = '\0';
509                         got_host = 1;
510                     }
511                 } else {
512                     cmdline_error("unknown option \"%s\"", p);
513                 }
514             }
515         }
516
517         cmdline_run_saved(&cfg);
518
519         if (!*cfg.host && !do_config()) {
520             cleanup_exit(0);
521         }
522
523         /*
524          * Trim leading whitespace off the hostname if it's there.
525          */
526         {
527             int space = strspn(cfg.host, " \t");
528             memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
529         }
530
531         /* See if host is of the form user@host */
532         if (cfg.host[0] != '\0') {
533             char *atsign = strrchr(cfg.host, '@');
534             /* Make sure we're not overflowing the user field */
535             if (atsign) {
536                 if (atsign - cfg.host < sizeof cfg.username) {
537                     strncpy(cfg.username, cfg.host, atsign - cfg.host);
538                     cfg.username[atsign - cfg.host] = '\0';
539                 }
540                 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
541             }
542         }
543
544         /*
545          * Trim a colon suffix off the hostname if it's there.
546          */
547         cfg.host[strcspn(cfg.host, ":")] = '\0';
548
549         /*
550          * Remove any remaining whitespace from the hostname.
551          */
552         {
553             int p1 = 0, p2 = 0;
554             while (cfg.host[p2] != '\0') {
555                 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
556                     cfg.host[p1] = cfg.host[p2];
557                     p1++;
558                 }
559                 p2++;
560             }
561             cfg.host[p1] = '\0';
562         }
563     }
564
565     /* Check for invalid Port number (i.e. zero) */
566     if (cfg.port == 0) {
567         char *str = dupprintf("%s Internal Error", appname);
568         MessageBox(NULL, "Invalid Port Number",
569                    str, MB_OK | MB_ICONEXCLAMATION);
570         sfree(str);
571         cleanup_exit(1);
572     }
573
574     if (!prev) {
575         wndclass.style = 0;
576         wndclass.lpfnWndProc = WndProc;
577         wndclass.cbClsExtra = 0;
578         wndclass.cbWndExtra = 0;
579         wndclass.hInstance = inst;
580         wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
581         wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
582         wndclass.hbrBackground = NULL;
583         wndclass.lpszMenuName = NULL;
584         wndclass.lpszClassName = appname;
585
586         RegisterClass(&wndclass);
587     }
588
589     hwnd = NULL;
590
591     memset(&ucsdata, 0, sizeof(ucsdata));
592
593     term = term_init(&cfg, &ucsdata, NULL);
594     logctx = log_init(NULL, &cfg);
595     term_provide_logctx(term, logctx);
596
597     cfgtopalette();
598
599     /*
600      * Guess some defaults for the window size. This all gets
601      * updated later, so we don't really care too much. However, we
602      * do want the font width/height guesses to correspond to a
603      * large font rather than a small one...
604      */
605
606     font_width = 10;
607     font_height = 20;
608     extra_width = 25;
609     extra_height = 28;
610     term_size(term, cfg.height, cfg.width, cfg.savelines);
611     guess_width = extra_width + font_width * term->cols;
612     guess_height = extra_height + font_height * term->rows;
613     {
614         RECT r;
615                 get_fullscreen_rect(&r);
616         if (guess_width > r.right - r.left)
617             guess_width = r.right - r.left;
618         if (guess_height > r.bottom - r.top)
619             guess_height = r.bottom - r.top;
620     }
621
622     {
623         int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
624         int exwinmode = 0;
625         if (!cfg.scrollbar)
626             winmode &= ~(WS_VSCROLL);
627         if (cfg.resize_action == RESIZE_DISABLED)
628             winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
629         if (cfg.alwaysontop)
630             exwinmode |= WS_EX_TOPMOST;
631         if (cfg.sunken_edge)
632             exwinmode |= WS_EX_CLIENTEDGE;
633         hwnd = CreateWindowEx(exwinmode, appname, appname,
634                               winmode, CW_USEDEFAULT, CW_USEDEFAULT,
635                               guess_width, guess_height,
636                               NULL, NULL, inst, NULL);
637     }
638
639     /*
640      * Initialise the fonts, simultaneously correcting the guesses
641      * for font_{width,height}.
642      */
643     init_fonts(0,0);
644
645     /*
646      * Correct the guesses for extra_{width,height}.
647      */
648     {
649         RECT cr, wr;
650         GetWindowRect(hwnd, &wr);
651         GetClientRect(hwnd, &cr);
652         offset_width = offset_height = cfg.window_border;
653         extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
654         extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
655     }
656
657     /*
658      * Resize the window, now we know what size we _really_ want it
659      * to be.
660      */
661     guess_width = extra_width + font_width * term->cols;
662     guess_height = extra_height + font_height * term->rows;
663     SetWindowPos(hwnd, NULL, 0, 0, guess_width, guess_height,
664                  SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
665
666     /*
667      * Set up a caret bitmap, with no content.
668      */
669     {
670         char *bits;
671         int size = (font_width + 15) / 16 * 2 * font_height;
672         bits = snewn(size, char);
673         memset(bits, 0, size);
674         caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
675         sfree(bits);
676     }
677     CreateCaret(hwnd, caretbm, font_width, font_height);
678
679     /*
680      * Initialise the scroll bar.
681      */
682     {
683         SCROLLINFO si;
684
685         si.cbSize = sizeof(si);
686         si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
687         si.nMin = 0;
688         si.nMax = term->rows - 1;
689         si.nPage = term->rows;
690         si.nPos = 0;
691         SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
692     }
693
694     /*
695      * Prepare the mouse handler.
696      */
697     lastact = MA_NOTHING;
698     lastbtn = MBT_NOTHING;
699     dbltime = GetDoubleClickTime();
700
701     /*
702      * Set up the session-control options on the system menu.
703      */
704     {
705         HMENU s, m;
706         int i, j;
707         char *str;
708
709         popup_menus[SYSMENU].menu = GetSystemMenu(hwnd, FALSE);
710         popup_menus[CTXMENU].menu = CreatePopupMenu();
711         AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_PASTE, "&Paste");
712
713         s = CreateMenu();
714         get_sesslist(&sesslist, TRUE);
715         /* skip sesslist.sessions[0] == Default Settings */
716         for (i = 1;
717              i < ((sesslist.nsessions <= MENU_SAVED_MAX+1) ? sesslist.nsessions
718                                                            : MENU_SAVED_MAX+1);
719              i++)
720             AppendMenu(s, MF_ENABLED, IDM_SAVED_MIN + (i-1)*MENU_SAVED_STEP,
721                        sesslist.sessions[i]);
722
723         for (j = 0; j < lenof(popup_menus); j++) {
724             m = popup_menus[j].menu;
725
726             AppendMenu(m, MF_SEPARATOR, 0, 0);
727             popup_menus[j].specials_submenu_pos = GetMenuItemCount(m);
728             AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
729             AppendMenu(m, MF_SEPARATOR, 0, 0);
730             AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
731             AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
732             AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) s, "Sa&ved Sessions");
733             AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
734             AppendMenu(m, MF_SEPARATOR, 0, 0);
735             AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
736             AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
737             AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
738             AppendMenu(m, MF_SEPARATOR, 0, 0);
739             AppendMenu(m, (cfg.resize_action == RESIZE_DISABLED) ?
740                        MF_GRAYED : MF_ENABLED, IDM_FULLSCREEN, "&Full Screen");
741             AppendMenu(m, MF_SEPARATOR, 0, 0);
742             if (help_path)
743                 AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
744             str = dupprintf("&About %s", appname);
745             AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
746             sfree(str);
747         }
748     }
749
750     start_backend();
751
752     /*
753      * Set up the initial input locale.
754      */
755     set_input_locale(GetKeyboardLayout(0));
756
757     /*
758      * Open the initial log file if there is one.
759      */
760     logfopen(logctx);
761
762     /*
763      * Finally show the window!
764      */
765     ShowWindow(hwnd, show);
766     SetForegroundWindow(hwnd);
767
768     /*
769      * Set the palette up.
770      */
771     pal = NULL;
772     logpal = NULL;
773     init_palette();
774
775     term->has_focus = (GetForegroundWindow() == hwnd);
776     UpdateWindow(hwnd);
777
778     if (GetMessage(&msg, NULL, 0, 0) == 1) {
779         int timer_id = 0, long_timer = 0;
780
781         while (msg.message != WM_QUIT) {
782             /* Sometimes DispatchMessage calls routines that use their own
783              * GetMessage loop, setup this timer so we get some control back.
784              *
785              * Also call term_update() from the timer so that if the host
786              * is sending data flat out we still do redraws.
787              */
788             if (timer_id && long_timer) {
789                 KillTimer(hwnd, timer_id);
790                 long_timer = timer_id = 0;
791             }
792             if (!timer_id)
793                 timer_id = SetTimer(hwnd, 1, 20, NULL);
794             if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
795                 DispatchMessage(&msg);
796
797             /* Make sure we blink everything that needs it. */
798             term_blink(term, 0);
799
800             /* Send the paste buffer if there's anything to send */
801             term_paste(term);
802
803             /* If there's nothing new in the queue then we can do everything
804              * we've delayed, reading the socket, writing, and repainting
805              * the window.
806              */
807             if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
808                 continue;
809
810             if (pending_netevent) {
811                 enact_pending_netevent();
812
813                 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
814                     continue;
815             }
816
817             /* Okay there is now nothing to do so we make sure the screen is
818              * completely up to date then tell windows to call us in a little 
819              * while.
820              */
821             if (timer_id) {
822                 KillTimer(hwnd, timer_id);
823                 timer_id = 0;
824             }
825             HideCaret(hwnd);
826             if (GetCapture() != hwnd || 
827                 (send_raw_mouse &&
828                  !(cfg.mouse_override && is_shift_pressed())))
829                 term_out(term);
830             term_update(term);
831             ShowCaret(hwnd);
832
833             flash_window(1);           /* maintain */
834
835             /* The messages seem unreliable; especially if we're being tricky */
836             term->has_focus = (GetForegroundWindow() == hwnd);
837
838             if (term->in_vbell)
839                 /* Hmm, term_update didn't want to do an update too soon ... */
840                 timer_id = SetTimer(hwnd, 1, 50, NULL);
841             else if (!term->has_focus)
842                 timer_id = SetTimer(hwnd, 1, 500, NULL);
843             else
844                 timer_id = SetTimer(hwnd, 1, 100, NULL);
845             long_timer = 1;
846
847             /* There's no point rescanning everything in the message queue
848              * so we do an apparently unnecessary wait here
849              */
850             WaitMessage();
851             if (GetMessage(&msg, NULL, 0, 0) != 1)
852                 break;
853         }
854     }
855
856     cleanup_exit(msg.wParam);          /* this doesn't return... */
857     return msg.wParam;                 /* ... but optimiser doesn't know */
858 }
859
860 /*
861  * Clean up and exit.
862  */
863 void cleanup_exit(int code)
864 {
865     /*
866      * Clean up.
867      */
868     deinit_fonts();
869     sfree(logpal);
870     if (pal)
871         DeleteObject(pal);
872     sk_cleanup();
873
874     if (cfg.protocol == PROT_SSH) {
875         random_save_seed();
876 #ifdef MSCRYPTOAPI
877         crypto_wrapup();
878 #endif
879     }
880
881     exit(code);
882 }
883
884 /*
885  * Set up, or shut down, an AsyncSelect. Called from winnet.c.
886  */
887 char *do_select(SOCKET skt, int startup)
888 {
889     int msg, events;
890     if (startup) {
891         msg = WM_NETEVENT;
892         events = (FD_CONNECT | FD_READ | FD_WRITE |
893                   FD_OOB | FD_CLOSE | FD_ACCEPT);
894     } else {
895         msg = events = 0;
896     }
897     if (!hwnd)
898         return "do_select(): internal error (hwnd==NULL)";
899     if (p_WSAAsyncSelect(skt, hwnd, msg, events) == SOCKET_ERROR) {
900         switch (p_WSAGetLastError()) {
901           case WSAENETDOWN:
902             return "Network is down";
903           default:
904             return "WSAAsyncSelect(): unknown error";
905         }
906     }
907     return NULL;
908 }
909
910 /*
911  * Update the Special Commands submenu.
912  */
913 void update_specials_menu(void *frontend)
914 {
915     HMENU p;
916     int menu_already_exists = (specials != NULL);
917     int i, j;
918
919     if (back)
920         specials = back->get_specials(backhandle);
921     else
922         specials = NULL;
923
924     if (specials) {
925         /* We can't use Windows to provide a stack for submenus, so
926          * here's a lame "stack" that will do for now. */
927         HMENU saved_menu = NULL;
928         int nesting = 1;
929         p = CreatePopupMenu();
930         for (i = 0; nesting > 0; i++) {
931             assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
932             switch (specials[i].code) {
933               case TS_SEP:
934                 AppendMenu(p, MF_SEPARATOR, 0, 0);
935                 break;
936               case TS_SUBMENU:
937                 assert(nesting < 2);
938                 nesting++;
939                 saved_menu = p; /* XXX lame stacking */
940                 p = CreatePopupMenu();
941                 AppendMenu(saved_menu, MF_POPUP | MF_ENABLED,
942                            (UINT) p, specials[i].name);
943                 break;
944               case TS_EXITMENU:
945                 nesting--;
946                 if (nesting) {
947                     p = saved_menu; /* XXX lame stacking */
948                     saved_menu = NULL;
949                 }
950                 break;
951               default:
952                 AppendMenu(p, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
953                            specials[i].name);
954                 break;
955             }
956         }
957         /* Squirrel the highest special. */
958         n_specials = i - 1;
959     } else {
960         p = NULL;
961         n_specials = 0;
962     }
963
964     for (j = 0; j < lenof(popup_menus); j++) {
965         if (menu_already_exists) {
966             /* XXX does this free up all submenus? */
967             DeleteMenu(popup_menus[j].menu,
968                        popup_menus[j].specials_submenu_pos,
969                        MF_BYPOSITION);
970             DeleteMenu(popup_menus[j].menu,
971                        popup_menus[j].specials_submenu_pos,
972                        MF_BYPOSITION);
973         }
974         if (specials) {
975             InsertMenu(popup_menus[j].menu,
976                        popup_menus[j].specials_submenu_pos,
977                        MF_BYPOSITION | MF_SEPARATOR, 0, 0);
978             InsertMenu(popup_menus[j].menu,
979                        popup_menus[j].specials_submenu_pos,
980                        MF_BYPOSITION | MF_POPUP | MF_ENABLED,
981                        (UINT) p, "S&pecial Command");
982         }
983     }
984 }
985
986 /*
987  * set or clear the "raw mouse message" mode
988  */
989 void set_raw_mouse_mode(void *frontend, int activate)
990 {
991     activate = activate && !cfg.no_mouse_rep;
992     send_raw_mouse = activate;
993     SetCursor(LoadCursor(NULL, activate ? IDC_ARROW : IDC_IBEAM));
994 }
995
996 /*
997  * Print a message box and close the connection.
998  */
999 void connection_fatal(void *frontend, char *fmt, ...)
1000 {
1001     va_list ap;
1002     char *stuff, morestuff[100];
1003
1004     va_start(ap, fmt);
1005     stuff = dupvprintf(fmt, ap);
1006     va_end(ap);
1007     sprintf(morestuff, "%.70s Fatal Error", appname);
1008     MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
1009     sfree(stuff);
1010
1011     if (cfg.close_on_exit == FORCE_ON)
1012         PostQuitMessage(1);
1013     else {
1014         close_session();
1015     }
1016 }
1017
1018 /*
1019  * Report an error at the command-line parsing stage.
1020  */
1021 void cmdline_error(char *fmt, ...)
1022 {
1023     va_list ap;
1024     char *stuff, morestuff[100];
1025
1026     va_start(ap, fmt);
1027     stuff = dupvprintf(fmt, ap);
1028     va_end(ap);
1029     sprintf(morestuff, "%.70s Command Line Error", appname);
1030     MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
1031     sfree(stuff);
1032     exit(1);
1033 }
1034
1035 /*
1036  * Actually do the job requested by a WM_NETEVENT
1037  */
1038 static void enact_pending_netevent(void)
1039 {
1040     static int reentering = 0;
1041     extern int select_result(WPARAM, LPARAM);
1042     int ret;
1043
1044     if (reentering)
1045         return;                        /* don't unpend the pending */
1046
1047     pending_netevent = FALSE;
1048
1049     reentering = 1;
1050     ret = select_result(pend_netevent_wParam, pend_netevent_lParam);
1051     reentering = 0;
1052
1053     if (ret == 0 && !session_closed) {
1054         /* Abnormal exits will already have set session_closed and taken
1055          * appropriate action. */
1056         if (cfg.close_on_exit == FORCE_ON ||
1057             cfg.close_on_exit == AUTO) PostQuitMessage(0);
1058         else {
1059             close_session();
1060             session_closed = TRUE;
1061             MessageBox(hwnd, "Connection closed by remote host",
1062                        appname, MB_OK | MB_ICONINFORMATION);
1063         }
1064     }
1065 }
1066
1067 /*
1068  * Copy the colour palette from the configuration data into defpal.
1069  * This is non-trivial because the colour indices are different.
1070  */
1071 static void cfgtopalette(void)
1072 {
1073     int i;
1074     static const int ww[] = {
1075         6, 7, 8, 9, 10, 11, 12, 13,
1076         14, 15, 16, 17, 18, 19, 20, 21,
1077         0, 1, 2, 3, 4, 4, 5, 5
1078     };
1079
1080     for (i = 0; i < 24; i++) {
1081         int w = ww[i];
1082         defpal[i].rgbtRed = cfg.colours[w][0];
1083         defpal[i].rgbtGreen = cfg.colours[w][1];
1084         defpal[i].rgbtBlue = cfg.colours[w][2];
1085     }
1086
1087     /* Override with system colours if appropriate */
1088     if (cfg.system_colour)
1089         systopalette();
1090 }
1091
1092 /*
1093  * Override bit of defpal with colours from the system.
1094  * (NB that this takes a copy the system colours at the time this is called,
1095  * so subsequent colour scheme changes don't take effect. To fix that we'd
1096  * probably want to be using GetSysColorBrush() and the like.)
1097  */
1098 static void systopalette(void)
1099 {
1100     int i;
1101     static const struct { int nIndex; int norm; int bold; } or[] =
1102     {
1103         { COLOR_WINDOWTEXT,     16, 17 }, /* Default Foreground */
1104         { COLOR_WINDOW,         18, 19 }, /* Default Background */
1105         { COLOR_HIGHLIGHTTEXT,  20, 21 }, /* Cursor Text */
1106         { COLOR_HIGHLIGHT,      22, 23 }, /* Cursor Colour */
1107     };
1108
1109     for (i = 0; i < (sizeof(or)/sizeof(or[0])); i++) {
1110         COLORREF colour = GetSysColor(or[i].nIndex);
1111         defpal[or[i].norm].rgbtRed =
1112            defpal[or[i].bold].rgbtRed = GetRValue(colour);
1113         defpal[or[i].norm].rgbtGreen =
1114            defpal[or[i].bold].rgbtGreen = GetGValue(colour);
1115         defpal[or[i].norm].rgbtBlue =
1116            defpal[or[i].bold].rgbtBlue = GetBValue(colour);
1117     }
1118 }
1119
1120 /*
1121  * Set up the colour palette.
1122  */
1123 static void init_palette(void)
1124 {
1125     int i;
1126     HDC hdc = GetDC(hwnd);
1127     if (hdc) {
1128         if (cfg.try_palette && GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
1129             /*
1130              * This is a genuine case where we must use smalloc
1131              * because the snew macros can't cope.
1132              */
1133             logpal = smalloc(sizeof(*logpal)
1134                              - sizeof(logpal->palPalEntry)
1135                              + NCOLOURS * sizeof(PALETTEENTRY));
1136             logpal->palVersion = 0x300;
1137             logpal->palNumEntries = NCOLOURS;
1138             for (i = 0; i < NCOLOURS; i++) {
1139                 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
1140                 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
1141                 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
1142                 logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
1143             }
1144             pal = CreatePalette(logpal);
1145             if (pal) {
1146                 SelectPalette(hdc, pal, FALSE);
1147                 RealizePalette(hdc);
1148                 SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), FALSE);
1149             }
1150         }
1151         ReleaseDC(hwnd, hdc);
1152     }
1153     if (pal)
1154         for (i = 0; i < NCOLOURS; i++)
1155             colours[i] = PALETTERGB(defpal[i].rgbtRed,
1156                                     defpal[i].rgbtGreen,
1157                                     defpal[i].rgbtBlue);
1158     else
1159         for (i = 0; i < NCOLOURS; i++)
1160             colours[i] = RGB(defpal[i].rgbtRed,
1161                              defpal[i].rgbtGreen, defpal[i].rgbtBlue);
1162 }
1163
1164 /*
1165  * This is a wrapper to ExtTextOut() to force Windows to display
1166  * the precise glyphs we give it. Otherwise it would do its own
1167  * bidi and Arabic shaping, and we would end up uncertain which
1168  * characters it had put where.
1169  */
1170 static void exact_textout(HDC hdc, int x, int y, CONST RECT *lprc,
1171                           unsigned short *lpString, UINT cbCount,
1172                           CONST INT *lpDx, int opaque)
1173 {
1174
1175     GCP_RESULTSW gcpr;
1176     char *buffer = snewn(cbCount*2+2, char);
1177     char *classbuffer = snewn(cbCount, char);
1178     memset(&gcpr, 0, sizeof(gcpr));
1179     memset(buffer, 0, cbCount*2+2);
1180     memset(classbuffer, GCPCLASS_NEUTRAL, cbCount);
1181
1182     gcpr.lStructSize = sizeof(gcpr);
1183     gcpr.lpGlyphs = (void *)buffer;
1184     gcpr.lpClass = classbuffer;
1185     gcpr.nGlyphs = cbCount;
1186
1187     GetCharacterPlacementW(hdc, lpString, cbCount, 0, &gcpr,
1188                            FLI_MASK | GCP_CLASSIN | GCP_DIACRITIC);
1189
1190     ExtTextOut(hdc, x, y,
1191                ETO_GLYPH_INDEX | ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
1192                lprc, buffer, cbCount, lpDx);
1193 }
1194
1195 /*
1196  * Initialise all the fonts we will need initially. There may be as many as
1197  * three or as few as one.  The other (poentially) twentyone fonts are done
1198  * if/when they are needed.
1199  *
1200  * We also:
1201  *
1202  * - check the font width and height, correcting our guesses if
1203  *   necessary.
1204  *
1205  * - verify that the bold font is the same width as the ordinary
1206  *   one, and engage shadow bolding if not.
1207  * 
1208  * - verify that the underlined font is the same width as the
1209  *   ordinary one (manual underlining by means of line drawing can
1210  *   be done in a pinch).
1211  */
1212 static void init_fonts(int pick_width, int pick_height)
1213 {
1214     TEXTMETRIC tm;
1215     CPINFO cpinfo;
1216     int fontsize[3];
1217     int i;
1218     HDC hdc;
1219     int fw_dontcare, fw_bold;
1220
1221     for (i = 0; i < FONT_MAXNO; i++)
1222         fonts[i] = NULL;
1223
1224     bold_mode = cfg.bold_colour ? BOLD_COLOURS : BOLD_FONT;
1225     und_mode = UND_FONT;
1226
1227     if (cfg.font.isbold) {
1228         fw_dontcare = FW_BOLD;
1229         fw_bold = FW_HEAVY;
1230     } else {
1231         fw_dontcare = FW_DONTCARE;
1232         fw_bold = FW_BOLD;
1233     }
1234
1235     hdc = GetDC(hwnd);
1236
1237     if (pick_height)
1238         font_height = pick_height;
1239     else {
1240         font_height = cfg.font.height;
1241         if (font_height > 0) {
1242             font_height =
1243                 -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1244         }
1245     }
1246     font_width = pick_width;
1247
1248 #define f(i,c,w,u) \
1249     fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
1250                            c, OUT_DEFAULT_PRECIS, \
1251                            CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
1252                            FIXED_PITCH | FF_DONTCARE, cfg.font.name)
1253
1254     f(FONT_NORMAL, cfg.font.charset, fw_dontcare, FALSE);
1255
1256     lfont.lfHeight = font_height;
1257     lfont.lfWidth = font_width;
1258     lfont.lfEscapement = 0;
1259     lfont.lfOrientation  = 0;
1260     lfont.lfWeight  = fw_dontcare;
1261     lfont.lfItalic = FALSE;
1262     lfont.lfUnderline = FALSE;
1263     lfont.lfStrikeOut = FALSE;
1264     lfont.lfCharSet = cfg.font.charset;
1265     lfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
1266     lfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1267     lfont.lfQuality = DEFAULT_QUALITY;
1268     lfont.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1269     strncpy(lfont.lfFaceName, cfg.font.name, LF_FACESIZE);
1270
1271     SelectObject(hdc, fonts[FONT_NORMAL]);
1272     GetTextMetrics(hdc, &tm);
1273
1274     if (pick_width == 0 || pick_height == 0) {
1275         font_height = tm.tmHeight;
1276         font_width = tm.tmAveCharWidth;
1277     }
1278     font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
1279
1280 #ifdef RDB_DEBUG_PATCH
1281     debug(23, "Primary font H=%d, AW=%d, MW=%d",
1282             tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
1283 #endif
1284
1285     {
1286         CHARSETINFO info;
1287         DWORD cset = tm.tmCharSet;
1288         memset(&info, 0xFF, sizeof(info));
1289
1290         /* !!! Yes the next line is right */
1291         if (cset == OEM_CHARSET)
1292             ucsdata.font_codepage = GetOEMCP();
1293         else
1294             if (TranslateCharsetInfo ((DWORD *) cset, &info, TCI_SRCCHARSET))
1295                 ucsdata.font_codepage = info.ciACP;
1296         else
1297             ucsdata.font_codepage = -1;
1298
1299         GetCPInfo(ucsdata.font_codepage, &cpinfo);
1300         ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
1301     }
1302
1303     f(FONT_UNDERLINE, cfg.font.charset, fw_dontcare, TRUE);
1304
1305     /*
1306      * Some fonts, e.g. 9-pt Courier, draw their underlines
1307      * outside their character cell. We successfully prevent
1308      * screen corruption by clipping the text output, but then
1309      * we lose the underline completely. Here we try to work
1310      * out whether this is such a font, and if it is, we set a
1311      * flag that causes underlines to be drawn by hand.
1312      *
1313      * Having tried other more sophisticated approaches (such
1314      * as examining the TEXTMETRIC structure or requesting the
1315      * height of a string), I think we'll do this the brute
1316      * force way: we create a small bitmap, draw an underlined
1317      * space on it, and test to see whether any pixels are
1318      * foreground-coloured. (Since we expect the underline to
1319      * go all the way across the character cell, we only search
1320      * down a single column of the bitmap, half way across.)
1321      */
1322     {
1323         HDC und_dc;
1324         HBITMAP und_bm, und_oldbm;
1325         int i, gotit;
1326         COLORREF c;
1327
1328         und_dc = CreateCompatibleDC(hdc);
1329         und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
1330         und_oldbm = SelectObject(und_dc, und_bm);
1331         SelectObject(und_dc, fonts[FONT_UNDERLINE]);
1332         SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
1333         SetTextColor(und_dc, RGB(255, 255, 255));
1334         SetBkColor(und_dc, RGB(0, 0, 0));
1335         SetBkMode(und_dc, OPAQUE);
1336         ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
1337         gotit = FALSE;
1338         for (i = 0; i < font_height; i++) {
1339             c = GetPixel(und_dc, font_width / 2, i);
1340             if (c != RGB(0, 0, 0))
1341                 gotit = TRUE;
1342         }
1343         SelectObject(und_dc, und_oldbm);
1344         DeleteObject(und_bm);
1345         DeleteDC(und_dc);
1346         if (!gotit) {
1347             und_mode = UND_LINE;
1348             DeleteObject(fonts[FONT_UNDERLINE]);
1349             fonts[FONT_UNDERLINE] = 0;
1350         }
1351     }
1352
1353     if (bold_mode == BOLD_FONT) {
1354         f(FONT_BOLD, cfg.font.charset, fw_bold, FALSE);
1355     }
1356 #undef f
1357
1358     descent = tm.tmAscent + 1;
1359     if (descent >= font_height)
1360         descent = font_height - 1;
1361
1362     for (i = 0; i < 3; i++) {
1363         if (fonts[i]) {
1364             if (SelectObject(hdc, fonts[i]) && GetTextMetrics(hdc, &tm))
1365                 fontsize[i] = tm.tmAveCharWidth + 256 * tm.tmHeight;
1366             else
1367                 fontsize[i] = -i;
1368         } else
1369             fontsize[i] = -i;
1370     }
1371
1372     ReleaseDC(hwnd, hdc);
1373
1374     if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
1375         und_mode = UND_LINE;
1376         DeleteObject(fonts[FONT_UNDERLINE]);
1377         fonts[FONT_UNDERLINE] = 0;
1378     }
1379
1380     if (bold_mode == BOLD_FONT &&
1381         fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
1382         bold_mode = BOLD_SHADOW;
1383         DeleteObject(fonts[FONT_BOLD]);
1384         fonts[FONT_BOLD] = 0;
1385     }
1386     fontflag[0] = fontflag[1] = fontflag[2] = 1;
1387
1388     init_ucs(&cfg, &ucsdata);
1389 }
1390
1391 static void another_font(int fontno)
1392 {
1393     int basefont;
1394     int fw_dontcare, fw_bold;
1395     int c, u, w, x;
1396     char *s;
1397
1398     if (fontno < 0 || fontno >= FONT_MAXNO || fontflag[fontno])
1399         return;
1400
1401     basefont = (fontno & ~(FONT_BOLDUND));
1402     if (basefont != fontno && !fontflag[basefont])
1403         another_font(basefont);
1404
1405     if (cfg.font.isbold) {
1406         fw_dontcare = FW_BOLD;
1407         fw_bold = FW_HEAVY;
1408     } else {
1409         fw_dontcare = FW_DONTCARE;
1410         fw_bold = FW_BOLD;
1411     }
1412
1413     c = cfg.font.charset;
1414     w = fw_dontcare;
1415     u = FALSE;
1416     s = cfg.font.name;
1417     x = font_width;
1418
1419     if (fontno & FONT_WIDE)
1420         x *= 2;
1421     if (fontno & FONT_NARROW)
1422         x = (x+1)/2;
1423     if (fontno & FONT_OEM)
1424         c = OEM_CHARSET;
1425     if (fontno & FONT_BOLD)
1426         w = fw_bold;
1427     if (fontno & FONT_UNDERLINE)
1428         u = TRUE;
1429
1430     fonts[fontno] =
1431         CreateFont(font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
1432                    FALSE, u, FALSE, c, OUT_DEFAULT_PRECIS,
1433                    CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
1434                    FIXED_PITCH | FF_DONTCARE, s);
1435
1436     fontflag[fontno] = 1;
1437 }
1438
1439 static void deinit_fonts(void)
1440 {
1441     int i;
1442     for (i = 0; i < FONT_MAXNO; i++) {
1443         if (fonts[i])
1444             DeleteObject(fonts[i]);
1445         fonts[i] = 0;
1446         fontflag[i] = 0;
1447     }
1448 }
1449
1450 void request_resize(void *frontend, int w, int h)
1451 {
1452     int width, height;
1453
1454     /* If the window is maximized supress resizing attempts */
1455     if (IsZoomed(hwnd)) {
1456         if (cfg.resize_action == RESIZE_TERM)
1457             return;
1458     }
1459
1460     if (cfg.resize_action == RESIZE_DISABLED) return;
1461     if (h == term->rows && w == term->cols) return;
1462
1463     /* Sanity checks ... */
1464     {
1465         static int first_time = 1;
1466         static RECT ss;
1467
1468         switch (first_time) {
1469           case 1:
1470             /* Get the size of the screen */
1471             if (get_fullscreen_rect(&ss))
1472                 /* first_time = 0 */ ;
1473             else {
1474                 first_time = 2;
1475                 break;
1476             }
1477           case 0:
1478             /* Make sure the values are sane */
1479             width = (ss.right - ss.left - extra_width) / 4;
1480             height = (ss.bottom - ss.top - extra_height) / 6;
1481
1482             if (w > width || h > height)
1483                 return;
1484             if (w < 15)
1485                 w = 15;
1486             if (h < 1)
1487                 h = 1;
1488         }
1489     }
1490
1491     term_size(term, h, w, cfg.savelines);
1492
1493     if (cfg.resize_action != RESIZE_FONT && !IsZoomed(hwnd)) {
1494         width = extra_width + font_width * w;
1495         height = extra_height + font_height * h;
1496
1497         SetWindowPos(hwnd, NULL, 0, 0, width, height,
1498             SWP_NOACTIVATE | SWP_NOCOPYBITS |
1499             SWP_NOMOVE | SWP_NOZORDER);
1500     } else
1501         reset_window(0);
1502
1503     InvalidateRect(hwnd, NULL, TRUE);
1504 }
1505
1506 static void reset_window(int reinit) {
1507     /*
1508      * This function decides how to resize or redraw when the 
1509      * user changes something. 
1510      *
1511      * This function doesn't like to change the terminal size but if the
1512      * font size is locked that may be it's only soluion.
1513      */
1514     int win_width, win_height;
1515     RECT cr, wr;
1516
1517 #ifdef RDB_DEBUG_PATCH
1518     debug((27, "reset_window()"));
1519 #endif
1520
1521     /* Current window sizes ... */
1522     GetWindowRect(hwnd, &wr);
1523     GetClientRect(hwnd, &cr);
1524
1525     win_width  = cr.right - cr.left;
1526     win_height = cr.bottom - cr.top;
1527
1528     if (cfg.resize_action == RESIZE_DISABLED) reinit = 2;
1529
1530     /* Are we being forced to reload the fonts ? */
1531     if (reinit>1) {
1532 #ifdef RDB_DEBUG_PATCH
1533         debug((27, "reset_window() -- Forced deinit"));
1534 #endif
1535         deinit_fonts();
1536         init_fonts(0,0);
1537     }
1538
1539     /* Oh, looks like we're minimised */
1540     if (win_width == 0 || win_height == 0)
1541         return;
1542
1543     /* Is the window out of position ? */
1544     if ( !reinit && 
1545             (offset_width != (win_width-font_width*term->cols)/2 ||
1546              offset_height != (win_height-font_height*term->rows)/2) ){
1547         offset_width = (win_width-font_width*term->cols)/2;
1548         offset_height = (win_height-font_height*term->rows)/2;
1549         InvalidateRect(hwnd, NULL, TRUE);
1550 #ifdef RDB_DEBUG_PATCH
1551         debug((27, "reset_window() -> Reposition terminal"));
1552 #endif
1553     }
1554
1555     if (IsZoomed(hwnd)) {
1556         /* We're fullscreen, this means we must not change the size of
1557          * the window so it's the font size or the terminal itself.
1558          */
1559
1560         extra_width = wr.right - wr.left - cr.right + cr.left;
1561         extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
1562
1563         if (cfg.resize_action != RESIZE_TERM) {
1564             if (  font_width != win_width/term->cols || 
1565                   font_height != win_height/term->rows) {
1566                 deinit_fonts();
1567                 init_fonts(win_width/term->cols, win_height/term->rows);
1568                 offset_width = (win_width-font_width*term->cols)/2;
1569                 offset_height = (win_height-font_height*term->rows)/2;
1570                 InvalidateRect(hwnd, NULL, TRUE);
1571 #ifdef RDB_DEBUG_PATCH
1572                 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1573                         font_width, font_height));
1574 #endif
1575             }
1576         } else {
1577             if (  font_width != win_width/term->cols || 
1578                   font_height != win_height/term->rows) {
1579                 /* Our only choice at this point is to change the 
1580                  * size of the terminal; Oh well.
1581                  */
1582                 term_size(term, win_height/font_height, win_width/font_width,
1583                           cfg.savelines);
1584                 offset_width = (win_width-font_width*term->cols)/2;
1585                 offset_height = (win_height-font_height*term->rows)/2;
1586                 InvalidateRect(hwnd, NULL, TRUE);
1587 #ifdef RDB_DEBUG_PATCH
1588                 debug((27, "reset_window() -> Zoomed term_size"));
1589 #endif
1590             }
1591         }
1592         return;
1593     }
1594
1595     /* Hmm, a force re-init means we should ignore the current window
1596      * so we resize to the default font size.
1597      */
1598     if (reinit>0) {
1599 #ifdef RDB_DEBUG_PATCH
1600         debug((27, "reset_window() -> Forced re-init"));
1601 #endif
1602
1603         offset_width = offset_height = cfg.window_border;
1604         extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1605         extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1606
1607         if (win_width != font_width*term->cols + offset_width*2 ||
1608             win_height != font_height*term->rows + offset_height*2) {
1609
1610             /* If this is too large windows will resize it to the maximum
1611              * allowed window size, we will then be back in here and resize
1612              * the font or terminal to fit.
1613              */
1614             SetWindowPos(hwnd, NULL, 0, 0, 
1615                          font_width*term->cols + extra_width, 
1616                          font_height*term->rows + extra_height,
1617                          SWP_NOMOVE | SWP_NOZORDER);
1618         }
1619
1620         InvalidateRect(hwnd, NULL, TRUE);
1621         return;
1622     }
1623
1624     /* Okay the user doesn't want us to change the font so we try the 
1625      * window. But that may be too big for the screen which forces us
1626      * to change the terminal.
1627      */
1628     if ((cfg.resize_action == RESIZE_TERM && reinit<=0) ||
1629         (cfg.resize_action == RESIZE_EITHER && reinit<0) ||
1630             reinit>0) {
1631         offset_width = offset_height = cfg.window_border;
1632         extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1633         extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1634
1635         if (win_width != font_width*term->cols + offset_width*2 ||
1636             win_height != font_height*term->rows + offset_height*2) {
1637
1638             static RECT ss;
1639             int width, height;
1640                 
1641                 get_fullscreen_rect(&ss);
1642
1643             width = (ss.right - ss.left - extra_width) / font_width;
1644             height = (ss.bottom - ss.top - extra_height) / font_height;
1645
1646             /* Grrr too big */
1647             if ( term->rows > height || term->cols > width ) {
1648                 if (cfg.resize_action == RESIZE_EITHER) {
1649                     /* Make the font the biggest we can */
1650                     if (term->cols > width)
1651                         font_width = (ss.right - ss.left - extra_width)
1652                             / term->cols;
1653                     if (term->rows > height)
1654                         font_height = (ss.bottom - ss.top - extra_height)
1655                             / term->rows;
1656
1657                     deinit_fonts();
1658                     init_fonts(font_width, font_height);
1659
1660                     width = (ss.right - ss.left - extra_width) / font_width;
1661                     height = (ss.bottom - ss.top - extra_height) / font_height;
1662                 } else {
1663                     if ( height > term->rows ) height = term->rows;
1664                     if ( width > term->cols )  width = term->cols;
1665                     term_size(term, height, width, cfg.savelines);
1666 #ifdef RDB_DEBUG_PATCH
1667                     debug((27, "reset_window() -> term resize to (%d,%d)",
1668                                height, width));
1669 #endif
1670                 }
1671             }
1672             
1673             SetWindowPos(hwnd, NULL, 0, 0, 
1674                          font_width*term->cols + extra_width, 
1675                          font_height*term->rows + extra_height,
1676                          SWP_NOMOVE | SWP_NOZORDER);
1677
1678             InvalidateRect(hwnd, NULL, TRUE);
1679 #ifdef RDB_DEBUG_PATCH
1680             debug((27, "reset_window() -> window resize to (%d,%d)",
1681                         font_width*term->cols + extra_width,
1682                         font_height*term->rows + extra_height));
1683 #endif
1684         }
1685         return;
1686     }
1687
1688     /* We're allowed to or must change the font but do we want to ?  */
1689
1690     if (font_width != (win_width-cfg.window_border*2)/term->cols || 
1691         font_height != (win_height-cfg.window_border*2)/term->rows) {
1692
1693         deinit_fonts();
1694         init_fonts((win_width-cfg.window_border*2)/term->cols, 
1695                    (win_height-cfg.window_border*2)/term->rows);
1696         offset_width = (win_width-font_width*term->cols)/2;
1697         offset_height = (win_height-font_height*term->rows)/2;
1698
1699         extra_width = wr.right - wr.left - cr.right + cr.left +offset_width*2;
1700         extra_height = wr.bottom - wr.top - cr.bottom + cr.top+offset_height*2;
1701
1702         InvalidateRect(hwnd, NULL, TRUE);
1703 #ifdef RDB_DEBUG_PATCH
1704         debug((25, "reset_window() -> font resize to (%d,%d)", 
1705                    font_width, font_height));
1706 #endif
1707     }
1708 }
1709
1710 static void set_input_locale(HKL kl)
1711 {
1712     char lbuf[20];
1713
1714     GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
1715                   lbuf, sizeof(lbuf));
1716
1717     kbd_codepage = atoi(lbuf);
1718 }
1719
1720 static void click(Mouse_Button b, int x, int y, int shift, int ctrl, int alt)
1721 {
1722     int thistime = GetMessageTime();
1723
1724     if (send_raw_mouse && !(cfg.mouse_override && shift)) {
1725         lastbtn = MBT_NOTHING;
1726         term_mouse(term, b, translate_button(b), MA_CLICK,
1727                    x, y, shift, ctrl, alt);
1728         return;
1729     }
1730
1731     if (lastbtn == b && thistime - lasttime < dbltime) {
1732         lastact = (lastact == MA_CLICK ? MA_2CLK :
1733                    lastact == MA_2CLK ? MA_3CLK :
1734                    lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
1735     } else {
1736         lastbtn = b;
1737         lastact = MA_CLICK;
1738     }
1739     if (lastact != MA_NOTHING)
1740         term_mouse(term, b, translate_button(b), lastact,
1741                    x, y, shift, ctrl, alt);
1742     lasttime = thistime;
1743 }
1744
1745 /*
1746  * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1747  * into a cooked one (SELECT, EXTEND, PASTE).
1748  */
1749 static Mouse_Button translate_button(Mouse_Button button)
1750 {
1751     if (button == MBT_LEFT)
1752         return MBT_SELECT;
1753     if (button == MBT_MIDDLE)
1754         return cfg.mouse_is_xterm == 1 ? MBT_PASTE : MBT_EXTEND;
1755     if (button == MBT_RIGHT)
1756         return cfg.mouse_is_xterm == 1 ? MBT_EXTEND : MBT_PASTE;
1757     return 0;                          /* shouldn't happen */
1758 }
1759
1760 static void show_mouseptr(int show)
1761 {
1762     static int cursor_visible = 1;
1763     if (!cfg.hide_mouseptr)            /* override if this feature disabled */
1764         show = 1;
1765     if (cursor_visible && !show)
1766         ShowCursor(FALSE);
1767     else if (!cursor_visible && show)
1768         ShowCursor(TRUE);
1769     cursor_visible = show;
1770 }
1771
1772 static int is_alt_pressed(void)
1773 {
1774     BYTE keystate[256];
1775     int r = GetKeyboardState(keystate);
1776     if (!r)
1777         return FALSE;
1778     if (keystate[VK_MENU] & 0x80)
1779         return TRUE;
1780     if (keystate[VK_RMENU] & 0x80)
1781         return TRUE;
1782     return FALSE;
1783 }
1784
1785 static int is_shift_pressed(void)
1786 {
1787     BYTE keystate[256];
1788     int r = GetKeyboardState(keystate);
1789     if (!r)
1790         return FALSE;
1791     if (keystate[VK_SHIFT] & 0x80)
1792         return TRUE;
1793     return FALSE;
1794 }
1795
1796 static int resizing;
1797
1798 static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
1799                                 WPARAM wParam, LPARAM lParam)
1800 {
1801     HDC hdc;
1802     static int ignore_clip = FALSE;
1803     static int need_backend_resize = FALSE;
1804     static int fullscr_on_max = FALSE;
1805     static UINT last_mousemove = 0;
1806
1807     switch (message) {
1808       case WM_TIMER:
1809         if (pending_netevent)
1810             enact_pending_netevent();
1811         if (GetCapture() != hwnd || 
1812             (send_raw_mouse && !(cfg.mouse_override && is_shift_pressed())))
1813             term_out(term);
1814         noise_regular();
1815         HideCaret(hwnd);
1816         term_update(term);
1817         ShowCaret(hwnd);
1818         if (cfg.ping_interval > 0) {
1819             time_t now;
1820             time(&now);
1821             if (now - last_movement > cfg.ping_interval) {
1822                 if (back)
1823                     back->special(backhandle, TS_PING);
1824                 last_movement = now;
1825             }
1826         }
1827         net_pending_errors();
1828         return 0;
1829       case WM_CREATE:
1830         break;
1831       case WM_CLOSE:
1832         {
1833             char *str;
1834             show_mouseptr(1);
1835             str = dupprintf("%s Exit Confirmation", appname);
1836             if (!cfg.warn_on_close || session_closed ||
1837                 MessageBox(hwnd,
1838                            "Are you sure you want to close this session?",
1839                            str, MB_ICONWARNING | MB_OKCANCEL) == IDOK)
1840                 DestroyWindow(hwnd);
1841             sfree(str);
1842         }
1843         return 0;
1844       case WM_DESTROY:
1845         show_mouseptr(1);
1846         PostQuitMessage(0);
1847         return 0;
1848       case WM_COMMAND:
1849       case WM_SYSCOMMAND:
1850         switch (wParam & ~0xF) {       /* low 4 bits reserved to Windows */
1851           case IDM_SHOWLOG:
1852             showeventlog(hwnd);
1853             break;
1854           case IDM_NEWSESS:
1855           case IDM_DUPSESS:
1856           case IDM_SAVEDSESS:
1857             {
1858                 char b[2048];
1859                 char c[30], *cl;
1860                 int freecl = FALSE;
1861                 STARTUPINFO si;
1862                 PROCESS_INFORMATION pi;
1863                 HANDLE filemap = NULL;
1864
1865                 if (wParam == IDM_DUPSESS) {
1866                     /*
1867                      * Allocate a file-mapping memory chunk for the
1868                      * config structure.
1869                      */
1870                     SECURITY_ATTRIBUTES sa;
1871                     Config *p;
1872
1873                     sa.nLength = sizeof(sa);
1874                     sa.lpSecurityDescriptor = NULL;
1875                     sa.bInheritHandle = TRUE;
1876                     filemap = CreateFileMapping((HANDLE) 0xFFFFFFFF,
1877                                                 &sa,
1878                                                 PAGE_READWRITE,
1879                                                 0, sizeof(Config), NULL);
1880                     if (filemap) {
1881                         p = (Config *) MapViewOfFile(filemap,
1882                                                      FILE_MAP_WRITE,
1883                                                      0, 0, sizeof(Config));
1884                         if (p) {
1885                             *p = cfg;  /* structure copy */
1886                             UnmapViewOfFile(p);
1887                         }
1888                     }
1889                     sprintf(c, "putty &%p", filemap);
1890                     cl = c;
1891                 } else if (wParam == IDM_SAVEDSESS) {
1892                     unsigned int sessno = ((lParam - IDM_SAVED_MIN)
1893                                            / MENU_SAVED_STEP) + 1;
1894                     if (sessno < sesslist.nsessions) {
1895                         char *session = sesslist.sessions[sessno];
1896                         /* XXX spaces? quotes? "-load"? */
1897                         cl = dupprintf("putty @%s", session);
1898                         freecl = TRUE;
1899                     } else
1900                         break;
1901                 } else
1902                     cl = NULL;
1903
1904                 GetModuleFileName(NULL, b, sizeof(b) - 1);
1905                 si.cb = sizeof(si);
1906                 si.lpReserved = NULL;
1907                 si.lpDesktop = NULL;
1908                 si.lpTitle = NULL;
1909                 si.dwFlags = 0;
1910                 si.cbReserved2 = 0;
1911                 si.lpReserved2 = NULL;
1912                 CreateProcess(b, cl, NULL, NULL, TRUE,
1913                               NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
1914
1915                 if (filemap)
1916                     CloseHandle(filemap);
1917                 if (freecl)
1918                     sfree(cl);
1919             }
1920             break;
1921           case IDM_RESTART:
1922             if (!back) {
1923                 logevent(NULL, "----- Session restarted -----");
1924                 start_backend();
1925             }
1926
1927             break;
1928           case IDM_RECONF:
1929             {
1930                 Config prev_cfg;
1931                 int init_lvl = 1;
1932
1933                 GetWindowText(hwnd, cfg.wintitle, sizeof(cfg.wintitle));
1934                 prev_cfg = cfg;
1935
1936                 if (!do_reconfig(hwnd))
1937                     break;
1938
1939                 {
1940                     /* Disable full-screen if resizing forbidden */
1941                     HMENU m = GetSystemMenu (hwnd, FALSE);
1942                     EnableMenuItem(m, IDM_FULLSCREEN, MF_BYCOMMAND | 
1943                                    (cfg.resize_action == RESIZE_DISABLED)
1944                                    ? MF_GRAYED : MF_ENABLED);
1945                     /* Gracefully unzoom if necessary */
1946                     if (IsZoomed(hwnd) &&
1947                         (cfg.resize_action == RESIZE_DISABLED)) {
1948                         ShowWindow(hwnd, SW_RESTORE);
1949                     }
1950                 }
1951
1952                 /* Pass new config data to the logging module */
1953                 log_reconfig(logctx, &cfg);
1954
1955                 sfree(logpal);
1956                 /*
1957                  * Flush the line discipline's edit buffer in the
1958                  * case where local editing has just been disabled.
1959                  */
1960                 if (ldisc)
1961                     ldisc_send(ldisc, NULL, 0, 0);
1962                 if (pal)
1963                     DeleteObject(pal);
1964                 logpal = NULL;
1965                 pal = NULL;
1966                 cfgtopalette();
1967                 init_palette();
1968
1969                 /* Pass new config data to the terminal */
1970                 term_reconfig(term, &cfg);
1971
1972                 /* Pass new config data to the back end */
1973                 if (back)
1974                     back->reconfig(backhandle, &cfg);
1975
1976                 /* Screen size changed ? */
1977                 if (cfg.height != prev_cfg.height ||
1978                     cfg.width != prev_cfg.width ||
1979                     cfg.savelines != prev_cfg.savelines ||
1980                     cfg.resize_action == RESIZE_FONT ||
1981                     (cfg.resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
1982                     cfg.resize_action == RESIZE_DISABLED)
1983                     term_size(term, cfg.height, cfg.width, cfg.savelines);
1984
1985                 /* Enable or disable the scroll bar, etc */
1986                 {
1987                     LONG nflg, flag = GetWindowLong(hwnd, GWL_STYLE);
1988                     LONG nexflag, exflag =
1989                         GetWindowLong(hwnd, GWL_EXSTYLE);
1990
1991                     nexflag = exflag;
1992                     if (cfg.alwaysontop != prev_cfg.alwaysontop) {
1993                         if (cfg.alwaysontop) {
1994                             nexflag |= WS_EX_TOPMOST;
1995                             SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
1996                                          SWP_NOMOVE | SWP_NOSIZE);
1997                         } else {
1998                             nexflag &= ~(WS_EX_TOPMOST);
1999                             SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
2000                                          SWP_NOMOVE | SWP_NOSIZE);
2001                         }
2002                     }
2003                     if (cfg.sunken_edge)
2004                         nexflag |= WS_EX_CLIENTEDGE;
2005                     else
2006                         nexflag &= ~(WS_EX_CLIENTEDGE);
2007
2008                     nflg = flag;
2009                     if (is_full_screen() ?
2010                         cfg.scrollbar_in_fullscreen : cfg.scrollbar)
2011                         nflg |= WS_VSCROLL;
2012                     else
2013                         nflg &= ~WS_VSCROLL;
2014
2015                     if (cfg.resize_action == RESIZE_DISABLED ||
2016                         is_full_screen())
2017                         nflg &= ~WS_THICKFRAME;
2018                     else
2019                         nflg |= WS_THICKFRAME;
2020
2021                     if (cfg.resize_action == RESIZE_DISABLED)
2022                         nflg &= ~WS_MAXIMIZEBOX;
2023                     else
2024                         nflg |= WS_MAXIMIZEBOX;
2025
2026                     if (nflg != flag || nexflag != exflag) {
2027                         if (nflg != flag)
2028                             SetWindowLong(hwnd, GWL_STYLE, nflg);
2029                         if (nexflag != exflag)
2030                             SetWindowLong(hwnd, GWL_EXSTYLE, nexflag);
2031
2032                         SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
2033                                      SWP_NOACTIVATE | SWP_NOCOPYBITS |
2034                                      SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
2035                                      SWP_FRAMECHANGED);
2036
2037                         init_lvl = 2;
2038                     }
2039                 }
2040
2041                 /* Oops */
2042                 if (cfg.resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
2043                     force_normal(hwnd);
2044                     init_lvl = 2;
2045                 }
2046
2047                 set_title(NULL, cfg.wintitle);
2048                 if (IsIconic(hwnd)) {
2049                     SetWindowText(hwnd,
2050                                   cfg.win_name_always ? window_name :
2051                                   icon_name);
2052                 }
2053
2054                 if (strcmp(cfg.font.name, prev_cfg.font.name) != 0 ||
2055                     strcmp(cfg.line_codepage, prev_cfg.line_codepage) != 0 ||
2056                     cfg.font.isbold != prev_cfg.font.isbold ||
2057                     cfg.font.height != prev_cfg.font.height ||
2058                     cfg.font.charset != prev_cfg.font.charset ||
2059                     cfg.vtmode != prev_cfg.vtmode ||
2060                     cfg.bold_colour != prev_cfg.bold_colour ||
2061                     cfg.resize_action == RESIZE_DISABLED ||
2062                     cfg.resize_action == RESIZE_EITHER ||
2063                     (cfg.resize_action != prev_cfg.resize_action))
2064                     init_lvl = 2;
2065
2066                 InvalidateRect(hwnd, NULL, TRUE);
2067                 reset_window(init_lvl);
2068                 net_pending_errors();
2069             }
2070             break;
2071           case IDM_COPYALL:
2072             term_copyall(term);
2073             break;
2074           case IDM_PASTE:
2075             term_do_paste(term);
2076             break;
2077           case IDM_CLRSB:
2078             term_clrsb(term);
2079             break;
2080           case IDM_RESET:
2081             term_pwron(term);
2082             if (ldisc)
2083                 ldisc_send(ldisc, NULL, 0, 0);
2084             break;
2085           case IDM_ABOUT:
2086             showabout(hwnd);
2087             break;
2088           case IDM_HELP:
2089             WinHelp(hwnd, help_path,
2090                     help_has_contents ? HELP_FINDER : HELP_CONTENTS, 0);
2091             break;
2092           case SC_MOUSEMENU:
2093             /*
2094              * We get this if the System menu has been activated
2095              * using the mouse.
2096              */
2097             show_mouseptr(1);
2098             break;
2099           case SC_KEYMENU:
2100             /*
2101              * We get this if the System menu has been activated
2102              * using the keyboard. This might happen from within
2103              * TranslateKey, in which case it really wants to be
2104              * followed by a `space' character to actually _bring
2105              * the menu up_ rather than just sitting there in
2106              * `ready to appear' state.
2107              */
2108             show_mouseptr(1);          /* make sure pointer is visible */
2109             if( lParam == 0 )
2110                 PostMessage(hwnd, WM_CHAR, ' ', 0);
2111             break;
2112           case IDM_FULLSCREEN:
2113             flip_full_screen();
2114             break;
2115           default:
2116             if (wParam >= IDM_SAVED_MIN && wParam < IDM_SAVED_MAX) {
2117                 SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
2118             }
2119             if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
2120                 int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
2121                 /*
2122                  * Ensure we haven't been sent a bogus SYSCOMMAND
2123                  * which would cause us to reference invalid memory
2124                  * and crash. Perhaps I'm just too paranoid here.
2125                  */
2126                 if (i >= n_specials)
2127                     break;
2128                 if (back)
2129                     back->special(backhandle, specials[i].code);
2130                 net_pending_errors();
2131             }
2132         }
2133         break;
2134
2135 #define X_POS(l) ((int)(short)LOWORD(l))
2136 #define Y_POS(l) ((int)(short)HIWORD(l))
2137
2138 #define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2139 #define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
2140       case WM_LBUTTONDOWN:
2141       case WM_MBUTTONDOWN:
2142       case WM_RBUTTONDOWN:
2143       case WM_LBUTTONUP:
2144       case WM_MBUTTONUP:
2145       case WM_RBUTTONUP:
2146         if (message == WM_RBUTTONDOWN &&
2147             ((wParam & MK_CONTROL) || (cfg.mouse_is_xterm == 2))) {
2148             POINT cursorpos;
2149
2150             show_mouseptr(1);          /* make sure pointer is visible */
2151             GetCursorPos(&cursorpos);
2152             TrackPopupMenu(popup_menus[CTXMENU].menu,
2153                            TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2154                            cursorpos.x, cursorpos.y,
2155                            0, hwnd, NULL);
2156             break;
2157         }
2158         {
2159             int button, press;
2160
2161             switch (message) {
2162               case WM_LBUTTONDOWN:
2163                 button = MBT_LEFT;
2164                 press = 1;
2165                 break;
2166               case WM_MBUTTONDOWN:
2167                 button = MBT_MIDDLE;
2168                 press = 1;
2169                 break;
2170               case WM_RBUTTONDOWN:
2171                 button = MBT_RIGHT;
2172                 press = 1;
2173                 break;
2174               case WM_LBUTTONUP:
2175                 button = MBT_LEFT;
2176                 press = 0;
2177                 break;
2178               case WM_MBUTTONUP:
2179                 button = MBT_MIDDLE;
2180                 press = 0;
2181                 break;
2182               case WM_RBUTTONUP:
2183                 button = MBT_RIGHT;
2184                 press = 0;
2185                 break;
2186               default:
2187                 button = press = 0;    /* shouldn't happen */
2188             }
2189             show_mouseptr(1);
2190             /*
2191              * Special case: in full-screen mode, if the left
2192              * button is clicked in the very top left corner of the
2193              * window, we put up the System menu instead of doing
2194              * selection.
2195              */
2196             {
2197                 char mouse_on_hotspot = 0;
2198                 POINT pt;
2199
2200                 GetCursorPos(&pt);
2201 #ifndef NO_MULTIMON
2202                 {
2203                     HMONITOR mon;
2204                     MONITORINFO mi;
2205
2206                     mon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
2207
2208                     if (mon != NULL) {
2209                         mi.cbSize = sizeof(MONITORINFO);
2210                         GetMonitorInfo(mon, &mi);
2211
2212                         if (mi.rcMonitor.left == pt.x &&
2213                             mi.rcMonitor.top == pt.y) {
2214                             mouse_on_hotspot = 1;
2215                         }
2216                     }
2217                 }
2218 #else
2219                 if (pt.x == 0 && pt.y == 0) {
2220                     mouse_on_hotspot = 1;
2221                 }
2222 #endif
2223                 if (is_full_screen() && press &&
2224                     button == MBT_LEFT && mouse_on_hotspot) {
2225                     SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU,
2226                                 MAKELPARAM(pt.x, pt.y));
2227                     return 0;
2228                 }
2229             }
2230
2231             if (press) {
2232                 click(button,
2233                       TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
2234                       wParam & MK_SHIFT, wParam & MK_CONTROL,
2235                       is_alt_pressed());
2236                 SetCapture(hwnd);
2237             } else {
2238                 term_mouse(term, button, translate_button(button), MA_RELEASE,
2239                            TO_CHR_X(X_POS(lParam)),
2240                            TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
2241                            wParam & MK_CONTROL, is_alt_pressed());
2242                 ReleaseCapture();
2243             }
2244         }
2245         return 0;
2246       case WM_MOUSEMOVE:
2247         {
2248             /*
2249              * Windows seems to like to occasionally send MOUSEMOVE
2250              * events even if the mouse hasn't moved. Don't unhide
2251              * the mouse pointer in this case.
2252              */
2253             static WPARAM wp = 0;
2254             static LPARAM lp = 0;
2255             if (wParam != wp || lParam != lp ||
2256                 last_mousemove != WM_MOUSEMOVE) {
2257                 show_mouseptr(1);
2258                 wp = wParam; lp = lParam;
2259                 last_mousemove = WM_MOUSEMOVE;
2260             }
2261         }
2262         /*
2263          * Add the mouse position and message time to the random
2264          * number noise.
2265          */
2266         noise_ultralight(lParam);
2267
2268         if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
2269             GetCapture() == hwnd) {
2270             Mouse_Button b;
2271             if (wParam & MK_LBUTTON)
2272                 b = MBT_LEFT;
2273             else if (wParam & MK_MBUTTON)
2274                 b = MBT_MIDDLE;
2275             else
2276                 b = MBT_RIGHT;
2277             term_mouse(term, b, translate_button(b), MA_DRAG,
2278                        TO_CHR_X(X_POS(lParam)),
2279                        TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
2280                        wParam & MK_CONTROL, is_alt_pressed());
2281         }
2282         return 0;
2283       case WM_NCMOUSEMOVE:
2284         {
2285             static WPARAM wp = 0;
2286             static LPARAM lp = 0;
2287             if (wParam != wp || lParam != lp ||
2288                 last_mousemove != WM_NCMOUSEMOVE) {
2289                 show_mouseptr(1);
2290                 wp = wParam; lp = lParam;
2291                 last_mousemove = WM_NCMOUSEMOVE;
2292             }
2293         }
2294         noise_ultralight(lParam);
2295         break;
2296       case WM_IGNORE_CLIP:
2297         ignore_clip = wParam;          /* don't panic on DESTROYCLIPBOARD */
2298         break;
2299       case WM_DESTROYCLIPBOARD:
2300         if (!ignore_clip)
2301             term_deselect(term);
2302         ignore_clip = FALSE;
2303         return 0;
2304       case WM_PAINT:
2305         {
2306             PAINTSTRUCT p;
2307             HideCaret(hwnd);
2308             hdc = BeginPaint(hwnd, &p);
2309             if (pal) {
2310                 SelectPalette(hdc, pal, TRUE);
2311                 RealizePalette(hdc);
2312             }
2313             term_paint(term, hdc, 
2314                        (p.rcPaint.left-offset_width)/font_width,
2315                        (p.rcPaint.top-offset_height)/font_height,
2316                        (p.rcPaint.right-offset_width-1)/font_width,
2317                        (p.rcPaint.bottom-offset_height-1)/font_height,
2318                        is_alt_pressed());
2319
2320             if (p.fErase ||
2321                 p.rcPaint.left  < offset_width  ||
2322                 p.rcPaint.top   < offset_height ||
2323                 p.rcPaint.right >= offset_width + font_width*term->cols ||
2324                 p.rcPaint.bottom>= offset_height + font_height*term->rows)
2325             {
2326                 HBRUSH fillcolour, oldbrush;
2327                 HPEN   edge, oldpen;
2328                 fillcolour = CreateSolidBrush (
2329                                     colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
2330                 oldbrush = SelectObject(hdc, fillcolour);
2331                 edge = CreatePen(PS_SOLID, 0, 
2332                                     colours[(ATTR_DEFBG>>ATTR_BGSHIFT)*2]);
2333                 oldpen = SelectObject(hdc, edge);
2334
2335                 /*
2336                  * Jordan Russell reports that this apparently
2337                  * ineffectual IntersectClipRect() call masks a
2338                  * Windows NT/2K bug causing strange display
2339                  * problems when the PuTTY window is taller than
2340                  * the primary monitor. It seems harmless enough...
2341                  */
2342                 IntersectClipRect(hdc,
2343                         p.rcPaint.left, p.rcPaint.top,
2344                         p.rcPaint.right, p.rcPaint.bottom);
2345
2346                 ExcludeClipRect(hdc, 
2347                         offset_width, offset_height,
2348                         offset_width+font_width*term->cols,
2349                         offset_height+font_height*term->rows);
2350
2351                 Rectangle(hdc, p.rcPaint.left, p.rcPaint.top, 
2352                           p.rcPaint.right, p.rcPaint.bottom);
2353
2354                 // SelectClipRgn(hdc, NULL);
2355
2356                 SelectObject(hdc, oldbrush);
2357                 DeleteObject(fillcolour);
2358                 SelectObject(hdc, oldpen);
2359                 DeleteObject(edge);
2360             }
2361             SelectObject(hdc, GetStockObject(SYSTEM_FONT));
2362             SelectObject(hdc, GetStockObject(WHITE_PEN));
2363             EndPaint(hwnd, &p);
2364             ShowCaret(hwnd);
2365         }
2366         return 0;
2367       case WM_NETEVENT:
2368         /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2369          * but the only one that's likely to try to overload us is FD_READ.
2370          * This means buffering just one is fine.
2371          */
2372         if (pending_netevent)
2373             enact_pending_netevent();
2374
2375         pending_netevent = TRUE;
2376         pend_netevent_wParam = wParam;
2377         pend_netevent_lParam = lParam;
2378         if (WSAGETSELECTEVENT(lParam) != FD_READ)
2379             enact_pending_netevent();
2380
2381         time(&last_movement);
2382         return 0;
2383       case WM_SETFOCUS:
2384         term->has_focus = TRUE;
2385         CreateCaret(hwnd, caretbm, font_width, font_height);
2386         ShowCaret(hwnd);
2387         flash_window(0);               /* stop */
2388         compose_state = 0;
2389         term_out(term);
2390         term_update(term);
2391         break;
2392       case WM_KILLFOCUS:
2393         show_mouseptr(1);
2394         term->has_focus = FALSE;
2395         DestroyCaret();
2396         caret_x = caret_y = -1;        /* ensure caret is replaced next time */
2397         term_out(term);
2398         term_update(term);
2399         break;
2400       case WM_ENTERSIZEMOVE:
2401 #ifdef RDB_DEBUG_PATCH
2402         debug((27, "WM_ENTERSIZEMOVE"));
2403 #endif
2404         EnableSizeTip(1);
2405         resizing = TRUE;
2406         need_backend_resize = FALSE;
2407         break;
2408       case WM_EXITSIZEMOVE:
2409         EnableSizeTip(0);
2410         resizing = FALSE;
2411 #ifdef RDB_DEBUG_PATCH
2412         debug((27, "WM_EXITSIZEMOVE"));
2413 #endif
2414         if (need_backend_resize) {
2415             term_size(term, cfg.height, cfg.width, cfg.savelines);
2416             InvalidateRect(hwnd, NULL, TRUE);
2417         }
2418         break;
2419       case WM_SIZING:
2420         /*
2421          * This does two jobs:
2422          * 1) Keep the sizetip uptodate
2423          * 2) Make sure the window size is _stepped_ in units of the font size.
2424          */
2425         if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
2426             int width, height, w, h, ew, eh;
2427             LPRECT r = (LPRECT) lParam;
2428
2429             if ( !need_backend_resize && cfg.resize_action == RESIZE_EITHER &&
2430                     (cfg.height != term->rows || cfg.width != term->cols )) {
2431                 /* 
2432                  * Great! It seems that both the terminal size and the
2433                  * font size have been changed and the user is now dragging.
2434                  * 
2435                  * It will now be difficult to get back to the configured
2436                  * font size!
2437                  *
2438                  * This would be easier but it seems to be too confusing.
2439
2440                 term_size(term, cfg.height, cfg.width, cfg.savelines);
2441                 reset_window(2);
2442                  */
2443                 cfg.height=term->rows; cfg.width=term->cols;
2444
2445                 InvalidateRect(hwnd, NULL, TRUE);
2446                 need_backend_resize = TRUE;
2447             }
2448
2449             width = r->right - r->left - extra_width;
2450             height = r->bottom - r->top - extra_height;
2451             w = (width + font_width / 2) / font_width;
2452             if (w < 1)
2453                 w = 1;
2454             h = (height + font_height / 2) / font_height;
2455             if (h < 1)
2456                 h = 1;
2457             UpdateSizeTip(hwnd, w, h);
2458             ew = width - w * font_width;
2459             eh = height - h * font_height;
2460             if (ew != 0) {
2461                 if (wParam == WMSZ_LEFT ||
2462                     wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
2463                     r->left += ew;
2464                 else
2465                     r->right -= ew;
2466             }
2467             if (eh != 0) {
2468                 if (wParam == WMSZ_TOP ||
2469                     wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
2470                     r->top += eh;
2471                 else
2472                     r->bottom -= eh;
2473             }
2474             if (ew || eh)
2475                 return 1;
2476             else
2477                 return 0;
2478         } else {
2479             int width, height, w, h, rv = 0;
2480             int ex_width = extra_width + (cfg.window_border - offset_width) * 2;
2481             int ex_height = extra_height + (cfg.window_border - offset_height) * 2;
2482             LPRECT r = (LPRECT) lParam;
2483
2484             width = r->right - r->left - ex_width;
2485             height = r->bottom - r->top - ex_height;
2486
2487             w = (width + term->cols/2)/term->cols;
2488             h = (height + term->rows/2)/term->rows;
2489             if ( r->right != r->left + w*term->cols + ex_width)
2490                 rv = 1;
2491
2492             if (wParam == WMSZ_LEFT ||
2493                 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
2494                 r->left = r->right - w*term->cols - ex_width;
2495             else
2496                 r->right = r->left + w*term->cols + ex_width;
2497
2498             if (r->bottom != r->top + h*term->rows + ex_height)
2499                 rv = 1;
2500
2501             if (wParam == WMSZ_TOP ||
2502                 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
2503                 r->top = r->bottom - h*term->rows - ex_height;
2504             else
2505                 r->bottom = r->top + h*term->rows + ex_height;
2506
2507             return rv;
2508         }
2509         /* break;  (never reached) */
2510       case WM_FULLSCR_ON_MAX:
2511         fullscr_on_max = TRUE;
2512         break;
2513       case WM_MOVE:
2514         sys_cursor_update();
2515         break;
2516       case WM_SIZE:
2517 #ifdef RDB_DEBUG_PATCH
2518         debug((27, "WM_SIZE %s (%d,%d)",
2519                 (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
2520                 (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
2521                 (wParam == SIZE_RESTORED && resizing) ? "to":
2522                 (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
2523                 "...",
2524             LOWORD(lParam), HIWORD(lParam)));
2525 #endif
2526         if (wParam == SIZE_MINIMIZED)
2527             SetWindowText(hwnd,
2528                           cfg.win_name_always ? window_name : icon_name);
2529         if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
2530             SetWindowText(hwnd, window_name);
2531         if (wParam == SIZE_RESTORED)
2532             clear_full_screen();
2533         if (wParam == SIZE_MAXIMIZED && fullscr_on_max) {
2534             fullscr_on_max = FALSE;
2535             make_full_screen();
2536         }
2537
2538         if (cfg.resize_action == RESIZE_DISABLED) {
2539             /* A resize, well it better be a minimize. */
2540             reset_window(-1);
2541         } else {
2542
2543             int width, height, w, h;
2544
2545             width = LOWORD(lParam);
2546             height = HIWORD(lParam);
2547
2548             if (!resizing) {
2549                 if (wParam == SIZE_MAXIMIZED && !was_zoomed) {
2550                     was_zoomed = 1;
2551                     prev_rows = term->rows;
2552                     prev_cols = term->cols;
2553                     if (cfg.resize_action == RESIZE_TERM) {
2554                         w = width / font_width;
2555                         if (w < 1) w = 1;
2556                         h = height / font_height;
2557                         if (h < 1) h = 1;
2558
2559                         term_size(term, h, w, cfg.savelines);
2560                     }
2561                     reset_window(0);
2562                 } else if (wParam == SIZE_RESTORED && was_zoomed) {
2563                     was_zoomed = 0;
2564                     if (cfg.resize_action == RESIZE_TERM)
2565                         term_size(term, prev_rows, prev_cols, cfg.savelines);
2566                     if (cfg.resize_action != RESIZE_FONT)
2567                         reset_window(2);
2568                     else
2569                         reset_window(0);
2570                 }
2571                 /* This is an unexpected resize, these will normally happen
2572                  * if the window is too large. Probably either the user
2573                  * selected a huge font or the screen size has changed.
2574                  *
2575                  * This is also called with minimize.
2576                  */
2577                 else reset_window(-1);
2578             }
2579
2580             /*
2581              * Don't call back->size in mid-resize. (To prevent
2582              * massive numbers of resize events getting sent
2583              * down the connection during an NT opaque drag.)
2584              */
2585             if (resizing) {
2586                 if (cfg.resize_action != RESIZE_FONT && !is_alt_pressed()) {
2587                     need_backend_resize = TRUE;
2588                     w = (width-cfg.window_border*2) / font_width;
2589                     if (w < 1) w = 1;
2590                     h = (height-cfg.window_border*2) / font_height;
2591                     if (h < 1) h = 1;
2592
2593                     cfg.height = h;
2594                     cfg.width = w;
2595                 } else 
2596                     reset_window(0);
2597             }
2598         }
2599         sys_cursor_update();
2600         return 0;
2601       case WM_VSCROLL:
2602         switch (LOWORD(wParam)) {
2603           case SB_BOTTOM:
2604             term_scroll(term, -1, 0);
2605             break;
2606           case SB_TOP:
2607             term_scroll(term, +1, 0);
2608             break;
2609           case SB_LINEDOWN:
2610             term_scroll(term, 0, +1);
2611             break;
2612           case SB_LINEUP:
2613             term_scroll(term, 0, -1);
2614             break;
2615           case SB_PAGEDOWN:
2616             term_scroll(term, 0, +term->rows / 2);
2617             break;
2618           case SB_PAGEUP:
2619             term_scroll(term, 0, -term->rows / 2);
2620             break;
2621           case SB_THUMBPOSITION:
2622           case SB_THUMBTRACK:
2623             term_scroll(term, 1, HIWORD(wParam));
2624             break;
2625         }
2626         break;
2627       case WM_PALETTECHANGED:
2628         if ((HWND) wParam != hwnd && pal != NULL) {
2629             HDC hdc = get_ctx(NULL);
2630             if (hdc) {
2631                 if (RealizePalette(hdc) > 0)
2632                     UpdateColors(hdc);
2633                 free_ctx(hdc);
2634             }
2635         }
2636         break;
2637       case WM_QUERYNEWPALETTE:
2638         if (pal != NULL) {
2639             HDC hdc = get_ctx(NULL);
2640             if (hdc) {
2641                 if (RealizePalette(hdc) > 0)
2642                     UpdateColors(hdc);
2643                 free_ctx(hdc);
2644                 return TRUE;
2645             }
2646         }
2647         return FALSE;
2648       case WM_KEYDOWN:
2649       case WM_SYSKEYDOWN:
2650       case WM_KEYUP:
2651       case WM_SYSKEYUP:
2652         /*
2653          * Add the scan code and keypress timing to the random
2654          * number noise.
2655          */
2656         noise_ultralight(lParam);
2657
2658         /*
2659          * We don't do TranslateMessage since it disassociates the
2660          * resulting CHAR message from the KEYDOWN that sparked it,
2661          * which we occasionally don't want. Instead, we process
2662          * KEYDOWN, and call the Win32 translator functions so that
2663          * we get the translations under _our_ control.
2664          */
2665         {
2666             unsigned char buf[20];
2667             int len;
2668
2669             if (wParam == VK_PROCESSKEY) {
2670                 MSG m;
2671                 m.hwnd = hwnd;
2672                 m.message = WM_KEYDOWN;
2673                 m.wParam = wParam;
2674                 m.lParam = lParam & 0xdfff;
2675                 TranslateMessage(&m);
2676             } else {
2677                 len = TranslateKey(message, wParam, lParam, buf);
2678                 if (len == -1)
2679                     return DefWindowProc(hwnd, message, wParam, lParam);
2680
2681                 if (len != 0) {
2682                     /*
2683                      * Interrupt an ongoing paste. I'm not sure
2684                      * this is sensible, but for the moment it's
2685                      * preferable to having to faff about buffering
2686                      * things.
2687                      */
2688                     term_nopaste(term);
2689
2690                     /*
2691                      * We need not bother about stdin backlogs
2692                      * here, because in GUI PuTTY we can't do
2693                      * anything about it anyway; there's no means
2694                      * of asking Windows to hold off on KEYDOWN
2695                      * messages. We _have_ to buffer everything
2696                      * we're sent.
2697                      */
2698                     term_seen_key_event(term);
2699                     if (ldisc)
2700                         ldisc_send(ldisc, buf, len, 1);
2701                     show_mouseptr(0);
2702                 }
2703             }
2704         }
2705         net_pending_errors();
2706         return 0;
2707       case WM_INPUTLANGCHANGE:
2708         /* wParam == Font number */
2709         /* lParam == Locale */
2710         set_input_locale((HKL)lParam);
2711         sys_cursor_update();
2712         break;
2713       case WM_IME_NOTIFY:
2714         if(wParam == IMN_SETOPENSTATUS) {
2715             HIMC hImc = ImmGetContext(hwnd);
2716             ImmSetCompositionFont(hImc, &lfont);
2717             ImmReleaseContext(hwnd, hImc);
2718             return 0;
2719         }
2720         break;
2721       case WM_IME_COMPOSITION:
2722         {
2723             HIMC hIMC;
2724             int n;
2725             char *buff;
2726
2727             if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS || 
2728                 osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
2729
2730             if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
2731                 break; /* fall back to DefWindowProc */
2732
2733             hIMC = ImmGetContext(hwnd);
2734             n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
2735
2736             if (n > 0) {
2737                 int i;
2738                 buff = snewn(n, char);
2739                 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
2740                 /*
2741                  * Jaeyoun Chung reports that Korean character
2742                  * input doesn't work correctly if we do a single
2743                  * luni_send() covering the whole of buff. So
2744                  * instead we luni_send the characters one by one.
2745                  */
2746                 term_seen_key_event(term);
2747                 for (i = 0; i < n; i += 2) {
2748                     if (ldisc)
2749                         luni_send(ldisc, (unsigned short *)(buff+i), 1, 1);
2750                 }
2751                 free(buff);
2752             }
2753             ImmReleaseContext(hwnd, hIMC);
2754             return 1;
2755         }
2756
2757       case WM_IME_CHAR:
2758         if (wParam & 0xFF00) {
2759             unsigned char buf[2];
2760
2761             buf[1] = wParam;
2762             buf[0] = wParam >> 8;
2763             term_seen_key_event(term);
2764             if (ldisc)
2765                 lpage_send(ldisc, kbd_codepage, buf, 2, 1);
2766         } else {
2767             char c = (unsigned char) wParam;
2768             term_seen_key_event(term);
2769             if (ldisc)
2770                 lpage_send(ldisc, kbd_codepage, &c, 1, 1);
2771         }
2772         return (0);
2773       case WM_CHAR:
2774       case WM_SYSCHAR:
2775         /*
2776          * Nevertheless, we are prepared to deal with WM_CHAR
2777          * messages, should they crop up. So if someone wants to
2778          * post the things to us as part of a macro manoeuvre,
2779          * we're ready to cope.
2780          */
2781         {
2782             char c = (unsigned char)wParam;
2783             term_seen_key_event(term);
2784             if (ldisc)
2785                 lpage_send(ldisc, CP_ACP, &c, 1, 1);
2786         }
2787         return 0;
2788       case WM_SETCURSOR:
2789         if (send_raw_mouse && LOWORD(lParam) == HTCLIENT) {
2790             SetCursor(LoadCursor(NULL, IDC_ARROW));
2791             return TRUE;
2792         }
2793         break;
2794       case WM_SYSCOLORCHANGE:
2795         if (cfg.system_colour) {
2796             /* Refresh palette from system colours. */
2797             /* XXX actually this zaps the entire palette. */
2798             systopalette();
2799             init_palette();
2800             /* Force a repaint of the terminal window. */
2801             term_invalidate(term);
2802         }
2803         break;
2804       case WM_AGENT_CALLBACK:
2805         {
2806             struct agent_callback *c = (struct agent_callback *)lParam;
2807             c->callback(c->callback_ctx, c->data, c->len);
2808             sfree(c);
2809         }
2810         return 0;
2811       default:
2812         if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
2813             int shift_pressed=0, control_pressed=0;
2814
2815             if (message == WM_MOUSEWHEEL) {
2816                 wheel_accumulator += (short)HIWORD(wParam);
2817                 shift_pressed=LOWORD(wParam) & MK_SHIFT;
2818                 control_pressed=LOWORD(wParam) & MK_CONTROL;
2819             } else {
2820                 BYTE keys[256];
2821                 wheel_accumulator += (int)wParam;
2822                 if (GetKeyboardState(keys)!=0) {
2823                     shift_pressed=keys[VK_SHIFT]&0x80;
2824                     control_pressed=keys[VK_CONTROL]&0x80;
2825                 }
2826             }
2827
2828             /* process events when the threshold is reached */
2829             while (abs(wheel_accumulator) >= WHEEL_DELTA) {
2830                 int b;
2831
2832                 /* reduce amount for next time */
2833                 if (wheel_accumulator > 0) {
2834                     b = MBT_WHEEL_UP;
2835                     wheel_accumulator -= WHEEL_DELTA;
2836                 } else if (wheel_accumulator < 0) {
2837                     b = MBT_WHEEL_DOWN;
2838                     wheel_accumulator += WHEEL_DELTA;
2839                 } else
2840                     break;
2841
2842                 if (send_raw_mouse &&
2843                     !(cfg.mouse_override && shift_pressed)) {
2844                     /* send a mouse-down followed by a mouse up */
2845                     term_mouse(term, b, translate_button(b),
2846                                MA_CLICK,
2847                                TO_CHR_X(X_POS(lParam)),
2848                                TO_CHR_Y(Y_POS(lParam)), shift_pressed,
2849                                control_pressed, is_alt_pressed());
2850                     term_mouse(term, b, translate_button(b),
2851                                MA_RELEASE, TO_CHR_X(X_POS(lParam)),
2852                                TO_CHR_Y(Y_POS(lParam)), shift_pressed,
2853                                control_pressed, is_alt_pressed());
2854                 } else {
2855                     /* trigger a scroll */
2856                     term_scroll(term, 0,
2857                                 b == MBT_WHEEL_UP ?
2858                                 -term->rows / 2 : term->rows / 2);
2859                 }
2860             }
2861             return 0;
2862         }
2863     }
2864
2865     return DefWindowProc(hwnd, message, wParam, lParam);
2866 }
2867
2868 /*
2869  * Move the system caret. (We maintain one, even though it's
2870  * invisible, for the benefit of blind people: apparently some
2871  * helper software tracks the system caret, so we should arrange to
2872  * have one.)
2873  */
2874 void sys_cursor(void *frontend, int x, int y)
2875 {
2876     int cx, cy;
2877
2878     if (!term->has_focus) return;
2879
2880     /*
2881      * Avoid gratuitously re-updating the cursor position and IMM
2882      * window if there's no actual change required.
2883      */
2884     cx = x * font_width + offset_width;
2885     cy = y * font_height + offset_height;
2886     if (cx == caret_x && cy == caret_y)
2887         return;
2888     caret_x = cx;
2889     caret_y = cy;
2890
2891     sys_cursor_update();
2892 }
2893
2894 static void sys_cursor_update(void)
2895 {
2896     COMPOSITIONFORM cf;
2897     HIMC hIMC;
2898
2899     if (!term->has_focus) return;
2900
2901     if (caret_x < 0 || caret_y < 0)
2902         return;
2903
2904     SetCaretPos(caret_x, caret_y);
2905
2906     /* IMM calls on Win98 and beyond only */
2907     if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
2908     
2909     if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
2910             osVersion.dwMinorVersion == 0) return; /* 95 */
2911
2912     /* we should have the IMM functions */
2913     hIMC = ImmGetContext(hwnd);
2914     cf.dwStyle = CFS_POINT;
2915     cf.ptCurrentPos.x = caret_x;
2916     cf.ptCurrentPos.y = caret_y;
2917     ImmSetCompositionWindow(hIMC, &cf);
2918
2919     ImmReleaseContext(hwnd, hIMC);
2920 }
2921
2922 /*
2923  * Draw a line of text in the window, at given character
2924  * coordinates, in given attributes.
2925  *
2926  * We are allowed to fiddle with the contents of `text'.
2927  */
2928 void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
2929                       unsigned long attr, int lattr)
2930 {
2931     COLORREF fg, bg, t;
2932     int nfg, nbg, nfont;
2933     HDC hdc = ctx;
2934     RECT line_box;
2935     int force_manual_underline = 0;
2936     int fnt_width, char_width;
2937     int text_adjust = 0;
2938     static int *IpDx = 0, IpDxLEN = 0;
2939
2940     lattr &= LATTR_MODE;
2941
2942     char_width = fnt_width = font_width * (1 + (lattr != LATTR_NORM));
2943
2944     if (attr & ATTR_WIDE)
2945         char_width *= 2;
2946
2947     if (len > IpDxLEN || IpDx[0] != char_width) {
2948         int i;
2949         if (len > IpDxLEN) {
2950             sfree(IpDx);
2951             IpDx = snewn(len + 16, int);
2952             IpDxLEN = (len + 16);
2953         }
2954         for (i = 0; i < IpDxLEN; i++)
2955             IpDx[i] = char_width;
2956     }
2957
2958     /* Only want the left half of double width lines */
2959     if (lattr != LATTR_NORM && x*2 >= term->cols)
2960         return;
2961
2962     x *= fnt_width;
2963     y *= font_height;
2964     x += offset_width;
2965     y += offset_height;
2966
2967     if ((attr & TATTR_ACTCURS) && (cfg.cursor_type == 0 || term->big_cursor)) {
2968         attr &= ATTR_CUR_AND | (bold_mode != BOLD_COLOURS ? ATTR_BOLD : 0);
2969         attr ^= ATTR_CUR_XOR;
2970     }
2971
2972     nfont = 0;
2973     if (cfg.vtmode == VT_POORMAN && lattr != LATTR_NORM) {
2974         /* Assume a poorman font is borken in other ways too. */
2975         lattr = LATTR_WIDE;
2976     } else
2977         switch (lattr) {
2978           case LATTR_NORM:
2979             break;
2980           case LATTR_WIDE:
2981             nfont |= FONT_WIDE;
2982             break;
2983           default:
2984             nfont |= FONT_WIDE + FONT_HIGH;
2985             break;
2986         }
2987     if (attr & ATTR_NARROW)
2988         nfont |= FONT_NARROW;
2989
2990     /* Special hack for the VT100 linedraw glyphs. */
2991     if (text[0] >= 0x23BA && text[0] <= 0x23BD) {
2992         switch ((unsigned char) (text[0])) {
2993           case 0xBA:
2994             text_adjust = -2 * font_height / 5;
2995             break;
2996           case 0xBB:
2997             text_adjust = -1 * font_height / 5;
2998             break;
2999           case 0xBC:
3000             text_adjust = font_height / 5;
3001             break;
3002           case 0xBD:
3003             text_adjust = 2 * font_height / 5;
3004             break;
3005         }
3006         if (lattr == LATTR_TOP || lattr == LATTR_BOT)
3007             text_adjust *= 2;
3008         text[0] = ucsdata.unitab_xterm['q'];
3009         if (attr & ATTR_UNDER) {
3010             attr &= ~ATTR_UNDER;
3011             force_manual_underline = 1;
3012         }
3013     }
3014
3015     /* Anything left as an original character set is unprintable. */
3016     if (DIRECT_CHAR(text[0])) {
3017         int i;
3018         for (i = 0; i < len; i++)
3019             text[i] = 0xFFFD;
3020     }
3021
3022     /* OEM CP */
3023     if ((text[0] & CSET_MASK) == CSET_OEMCP)
3024         nfont |= FONT_OEM;
3025
3026     nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
3027     nfg = 2 * (nfg & 0xF) + (nfg & 0x10 ? 1 : 0);
3028     nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
3029     nbg = 2 * (nbg & 0xF) + (nbg & 0x10 ? 1 : 0);
3030     if (bold_mode == BOLD_FONT && (attr & ATTR_BOLD))
3031         nfont |= FONT_BOLD;
3032     if (und_mode == UND_FONT && (attr & ATTR_UNDER))
3033         nfont |= FONT_UNDERLINE;
3034     another_font(nfont);
3035     if (!fonts[nfont]) {
3036         if (nfont & FONT_UNDERLINE)
3037             force_manual_underline = 1;
3038         /* Don't do the same for manual bold, it could be bad news. */
3039
3040         nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
3041     }
3042     another_font(nfont);
3043     if (!fonts[nfont])
3044         nfont = FONT_NORMAL;
3045     if (attr & ATTR_REVERSE) {
3046         t = nfg;
3047         nfg = nbg;
3048         nbg = t;
3049     }
3050     if (bold_mode == BOLD_COLOURS && (attr & ATTR_BOLD))
3051         nfg |= 1;
3052     if (bold_mode == BOLD_COLOURS && (attr & ATTR_BLINK))
3053         nbg |= 1;
3054     fg = colours[nfg];
3055     bg = colours[nbg];
3056     SelectObject(hdc, fonts[nfont]);
3057     SetTextColor(hdc, fg);
3058     SetBkColor(hdc, bg);
3059     if (attr & TATTR_COMBINING)
3060         SetBkMode(hdc, TRANSPARENT);
3061     else
3062         SetBkMode(hdc, OPAQUE);
3063     line_box.left = x;
3064     line_box.top = y;
3065     line_box.right = x + char_width * len;
3066     line_box.bottom = y + font_height;
3067
3068     /* Only want the left half of double width lines */
3069     if (line_box.right > font_width*term->cols+offset_width)
3070         line_box.right = font_width*term->cols+offset_width;
3071
3072     /* We're using a private area for direct to font. (512 chars.) */
3073     if (ucsdata.dbcs_screenfont && (text[0] & CSET_MASK) == CSET_ACP) {
3074         /* Ho Hum, dbcs fonts are a PITA! */
3075         /* To display on W9x I have to convert to UCS */
3076         static wchar_t *uni_buf = 0;
3077         static int uni_len = 0;
3078         int nlen, mptr;
3079         if (len > uni_len) {
3080             sfree(uni_buf);
3081             uni_len = len;
3082             uni_buf = snewn(uni_len, wchar_t);
3083         }
3084
3085         for(nlen = mptr = 0; mptr<len; mptr++) {
3086             uni_buf[nlen] = 0xFFFD;
3087             if (IsDBCSLeadByteEx(ucsdata.font_codepage, (BYTE) text[mptr])) {
3088                 char dbcstext[2];
3089                 dbcstext[0] = text[mptr] & 0xFF;
3090                 dbcstext[1] = text[mptr+1] & 0xFF;
3091                 IpDx[nlen] += char_width;
3092                 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
3093                                     dbcstext, 2, uni_buf+nlen, 1);
3094                 mptr++;
3095             }
3096             else
3097             {
3098                 char dbcstext[1];
3099                 dbcstext[0] = text[mptr] & 0xFF;
3100                 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
3101                                     dbcstext, 1, uni_buf+nlen, 1);
3102             }
3103             nlen++;
3104         }
3105         if (nlen <= 0)
3106             return;                    /* Eeek! */
3107
3108         ExtTextOutW(hdc, x,
3109                     y - font_height * (lattr == LATTR_BOT) + text_adjust,
3110                     ETO_CLIPPED | ETO_OPAQUE, &line_box, uni_buf, nlen, IpDx);
3111         if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
3112             SetBkMode(hdc, TRANSPARENT);
3113             ExtTextOutW(hdc, x - 1,
3114                         y - font_height * (lattr ==
3115                                            LATTR_BOT) + text_adjust,
3116                         ETO_CLIPPED, &line_box, uni_buf, nlen, IpDx);
3117         }
3118
3119         IpDx[0] = -1;
3120     } else if (DIRECT_FONT(text[0])) {
3121         static char *directbuf = NULL;
3122         static int directlen = 0;
3123         int i;
3124         if (len > directlen) {
3125             directlen = len;
3126             directbuf = sresize(directbuf, directlen, char);
3127         }
3128
3129         for (i = 0; i < len; i++)
3130             directbuf[i] = text[i] & 0xFF;
3131
3132         ExtTextOut(hdc, x,
3133                    y - font_height * (lattr == LATTR_BOT) + text_adjust,
3134                    ETO_CLIPPED | ETO_OPAQUE, &line_box, directbuf, len, IpDx);
3135         if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
3136             SetBkMode(hdc, TRANSPARENT);
3137
3138             /* GRR: This draws the character outside it's box and can leave
3139              * 'droppings' even with the clip box! I suppose I could loop it
3140              * one character at a time ... yuk. 
3141              * 
3142              * Or ... I could do a test print with "W", and use +1 or -1 for this
3143              * shift depending on if the leftmost column is blank...
3144              */
3145             ExtTextOut(hdc, x - 1,
3146                        y - font_height * (lattr ==
3147                                           LATTR_BOT) + text_adjust,
3148                        ETO_CLIPPED, &line_box, directbuf, len, IpDx);
3149         }
3150     } else {
3151         /* And 'normal' unicode characters */
3152         static WCHAR *wbuf = NULL;
3153         static int wlen = 0;
3154         int i;
3155
3156         if (wlen < len) {
3157             sfree(wbuf);
3158             wlen = len;
3159             wbuf = snewn(wlen, WCHAR);
3160         }
3161
3162         for (i = 0; i < len; i++)
3163             wbuf[i] = text[i];
3164
3165         /* print Glyphs as they are, without Windows' Shaping*/
3166         exact_textout(hdc, x, y - font_height * (lattr == LATTR_BOT) + text_adjust,
3167                       &line_box, wbuf, len, IpDx, !(attr & TATTR_COMBINING));
3168 /*      ExtTextOutW(hdc, x,
3169                     y - font_height * (lattr == LATTR_BOT) + text_adjust,
3170                     ETO_CLIPPED | ETO_OPAQUE, &line_box, wbuf, len, IpDx);
3171  */
3172
3173         /* And the shadow bold hack. */
3174         if (bold_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
3175             SetBkMode(hdc, TRANSPARENT);
3176             ExtTextOutW(hdc, x - 1,
3177                         y - font_height * (lattr ==
3178                                            LATTR_BOT) + text_adjust,
3179                         ETO_CLIPPED, &line_box, wbuf, len, IpDx);
3180         }
3181     }
3182     if (lattr != LATTR_TOP && (force_manual_underline ||
3183                                (und_mode == UND_LINE
3184                                 && (attr & ATTR_UNDER)))) {
3185         HPEN oldpen;
3186         int dec = descent;
3187         if (lattr == LATTR_BOT)
3188             dec = dec * 2 - font_height;
3189
3190         oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, fg));
3191         MoveToEx(hdc, x, y + dec, NULL);
3192         LineTo(hdc, x + len * char_width, y + dec);
3193         oldpen = SelectObject(hdc, oldpen);
3194         DeleteObject(oldpen);
3195     }
3196 }
3197
3198 /*
3199  * Wrapper that handles combining characters.
3200  */
3201 void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3202              unsigned long attr, int lattr)
3203 {
3204     if (attr & TATTR_COMBINING) {
3205         unsigned long a = 0;
3206         attr &= ~TATTR_COMBINING;
3207         while (len--) {
3208             do_text_internal(ctx, x, y, text, 1, attr | a, lattr);
3209             text++;
3210             a = TATTR_COMBINING;
3211         }
3212     } else
3213         do_text_internal(ctx, x, y, text, len, attr, lattr);
3214 }
3215
3216 void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
3217                unsigned long attr, int lattr)
3218 {
3219
3220     int fnt_width;
3221     int char_width;
3222     HDC hdc = ctx;
3223     int ctype = cfg.cursor_type;
3224
3225     lattr &= LATTR_MODE;
3226
3227     if ((attr & TATTR_ACTCURS) && (ctype == 0 || term->big_cursor)) {
3228         if (*text != UCSWIDE) {
3229             do_text(ctx, x, y, text, len, attr, lattr);
3230             return;
3231         }
3232         ctype = 2;
3233         attr |= TATTR_RIGHTCURS;
3234     }
3235
3236     fnt_width = char_width = font_width * (1 + (lattr != LATTR_NORM));
3237     if (attr & ATTR_WIDE)
3238         char_width *= 2;
3239     x *= fnt_width;
3240     y *= font_height;
3241     x += offset_width;
3242     y += offset_height;
3243
3244     if ((attr & TATTR_PASCURS) && (ctype == 0 || term->big_cursor)) {
3245         POINT pts[5];
3246         HPEN oldpen;
3247         pts[0].x = pts[1].x = pts[4].x = x;
3248         pts[2].x = pts[3].x = x + char_width - 1;
3249         pts[0].y = pts[3].y = pts[4].y = y;
3250         pts[1].y = pts[2].y = y + font_height - 1;
3251         oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
3252         Polyline(hdc, pts, 5);
3253         oldpen = SelectObject(hdc, oldpen);
3254         DeleteObject(oldpen);
3255     } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) && ctype != 0) {
3256         int startx, starty, dx, dy, length, i;
3257         if (ctype == 1) {
3258             startx = x;
3259             starty = y + descent;
3260             dx = 1;
3261             dy = 0;
3262             length = char_width;
3263         } else {
3264             int xadjust = 0;
3265             if (attr & TATTR_RIGHTCURS)
3266                 xadjust = char_width - 1;
3267             startx = x + xadjust;
3268             starty = y;
3269             dx = 0;
3270             dy = 1;
3271             length = font_height;
3272         }
3273         if (attr & TATTR_ACTCURS) {
3274             HPEN oldpen;
3275             oldpen =
3276                 SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[23]));
3277             MoveToEx(hdc, startx, starty, NULL);
3278             LineTo(hdc, startx + dx * length, starty + dy * length);
3279             oldpen = SelectObject(hdc, oldpen);
3280             DeleteObject(oldpen);
3281         } else {
3282             for (i = 0; i < length; i++) {
3283                 if (i % 2 == 0) {
3284                     SetPixel(hdc, startx, starty, colours[23]);
3285                 }
3286                 startx += dx;
3287                 starty += dy;
3288             }
3289         }
3290     }
3291 }
3292
3293 /* This function gets the actual width of a character in the normal font.
3294  */
3295 int char_width(Context ctx, int uc) {
3296     HDC hdc = ctx;
3297     int ibuf = 0;
3298
3299     /* If the font max is the same as the font ave width then this
3300      * function is a no-op.
3301      */
3302     if (!font_dualwidth) return 1;
3303
3304     switch (uc & CSET_MASK) {
3305       case CSET_ASCII:
3306         uc = ucsdata.unitab_line[uc & 0xFF];
3307         break;
3308       case CSET_LINEDRW:
3309         uc = ucsdata.unitab_xterm[uc & 0xFF];
3310         break;
3311       case CSET_SCOACS:
3312         uc = ucsdata.unitab_scoacs[uc & 0xFF];
3313         break;
3314     }
3315     if (DIRECT_FONT(uc)) {
3316         if (ucsdata.dbcs_screenfont) return 1;
3317
3318         /* Speedup, I know of no font where ascii is the wrong width */
3319         if ((uc&~CSET_MASK) >= ' ' && (uc&~CSET_MASK)<= '~')
3320             return 1;
3321
3322         if ( (uc & CSET_MASK) == CSET_ACP ) {
3323             SelectObject(hdc, fonts[FONT_NORMAL]);
3324         } else if ( (uc & CSET_MASK) == CSET_OEMCP ) {
3325             another_font(FONT_OEM);
3326             if (!fonts[FONT_OEM]) return 0;
3327
3328             SelectObject(hdc, fonts[FONT_OEM]);
3329         } else
3330             return 0;
3331
3332         if ( GetCharWidth32(hdc, uc&~CSET_MASK, uc&~CSET_MASK, &ibuf) != 1 &&
3333              GetCharWidth(hdc, uc&~CSET_MASK, uc&~CSET_MASK, &ibuf) != 1)
3334             return 0;
3335     } else {
3336         /* Speedup, I know of no font where ascii is the wrong width */
3337         if (uc >= ' ' && uc <= '~') return 1;
3338
3339         SelectObject(hdc, fonts[FONT_NORMAL]);
3340         if ( GetCharWidth32W(hdc, uc, uc, &ibuf) == 1 )
3341             /* Okay that one worked */ ;
3342         else if ( GetCharWidthW(hdc, uc, uc, &ibuf) == 1 )
3343             /* This should work on 9x too, but it's "less accurate" */ ;
3344         else
3345             return 0;
3346     }
3347
3348     ibuf += font_width / 2 -1;
3349     ibuf /= font_width;
3350
3351     return ibuf;
3352 }
3353
3354 /*
3355  * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
3356  * codes. Returns number of bytes used or zero to drop the message
3357  * or -1 to forward the message to windows.
3358  */
3359 static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
3360                         unsigned char *output)
3361 {
3362     BYTE keystate[256];
3363     int scan, left_alt = 0, key_down, shift_state;
3364     int r, i, code;
3365     unsigned char *p = output;
3366     static int alt_sum = 0;
3367
3368     HKL kbd_layout = GetKeyboardLayout(0);
3369
3370     /* keys is for ToAsciiEx. There's some ick here, see below. */
3371     static WORD keys[3];
3372     static int compose_char = 0;
3373     static WPARAM compose_key = 0;
3374
3375     r = GetKeyboardState(keystate);
3376     if (!r)
3377         memset(keystate, 0, sizeof(keystate));
3378     else {
3379 #if 0
3380 #define SHOW_TOASCII_RESULT
3381         {                              /* Tell us all about key events */
3382             static BYTE oldstate[256];
3383             static int first = 1;
3384             static int scan;
3385             int ch;
3386             if (first)
3387                 memcpy(oldstate, keystate, sizeof(oldstate));
3388             first = 0;
3389
3390             if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
3391                 debug(("+"));
3392             } else if ((HIWORD(lParam) & KF_UP)
3393                        && scan == (HIWORD(lParam) & 0xFF)) {
3394                 debug((". U"));
3395             } else {
3396                 debug((".\n"));
3397                 if (wParam >= VK_F1 && wParam <= VK_F20)
3398                     debug(("K_F%d", wParam + 1 - VK_F1));
3399                 else
3400                     switch (wParam) {
3401                       case VK_SHIFT:
3402                         debug(("SHIFT"));
3403                         break;
3404                       case VK_CONTROL:
3405                         debug(("CTRL"));
3406                         break;
3407                       case VK_MENU:
3408                         debug(("ALT"));
3409                         break;
3410                       default:
3411                         debug(("VK_%02x", wParam));
3412                     }
3413                 if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
3414                     debug(("*"));
3415                 debug((", S%02x", scan = (HIWORD(lParam) & 0xFF)));
3416
3417                 ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
3418                 if (ch >= ' ' && ch <= '~')
3419                     debug((", '%c'", ch));
3420                 else if (ch)
3421                     debug((", $%02x", ch));
3422
3423                 if (keys[0])
3424                     debug((", KB0=%02x", keys[0]));
3425                 if (keys[1])
3426                     debug((", KB1=%02x", keys[1]));
3427                 if (keys[2])
3428                     debug((", KB2=%02x", keys[2]));
3429
3430                 if ((keystate[VK_SHIFT] & 0x80) != 0)
3431                     debug((", S"));
3432                 if ((keystate[VK_CONTROL] & 0x80) != 0)
3433                     debug((", C"));
3434                 if ((HIWORD(lParam) & KF_EXTENDED))
3435                     debug((", E"));
3436                 if ((HIWORD(lParam) & KF_UP))
3437                     debug((", U"));
3438             }
3439
3440             if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
3441             else if ((HIWORD(lParam) & KF_UP))
3442                 oldstate[wParam & 0xFF] ^= 0x80;
3443             else
3444                 oldstate[wParam & 0xFF] ^= 0x81;
3445
3446             for (ch = 0; ch < 256; ch++)
3447                 if (oldstate[ch] != keystate[ch])
3448                     debug((", M%02x=%02x", ch, keystate[ch]));
3449
3450             memcpy(oldstate, keystate, sizeof(oldstate));
3451         }
3452 #endif
3453
3454         if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
3455             keystate[VK_RMENU] = keystate[VK_MENU];
3456         }
3457
3458
3459         /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
3460         if ((cfg.funky_type == FUNKY_VT400 ||
3461              (cfg.funky_type <= FUNKY_LINUX && term->app_keypad_keys &&
3462               !cfg.no_applic_k))
3463             && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
3464
3465             wParam = VK_EXECUTE;
3466
3467             /* UnToggle NUMLock */
3468             if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
3469                 keystate[VK_NUMLOCK] ^= 1;
3470         }
3471
3472         /* And write back the 'adjusted' state */
3473         SetKeyboardState(keystate);
3474     }
3475
3476     /* Disable Auto repeat if required */
3477     if (term->repeat_off &&
3478         (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
3479         return 0;
3480
3481     if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
3482         left_alt = 1;
3483
3484     key_down = ((HIWORD(lParam) & KF_UP) == 0);
3485
3486     /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
3487     if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
3488         if (cfg.ctrlaltkeys)
3489             keystate[VK_MENU] = 0;
3490         else {
3491             keystate[VK_RMENU] = 0x80;
3492             left_alt = 0;
3493         }
3494     }
3495
3496     scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
3497     shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
3498         + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
3499
3500     /* Note if AltGr was pressed and if it was used as a compose key */
3501     if (!compose_state) {
3502         compose_key = 0x100;
3503         if (cfg.compose_key) {
3504             if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
3505                 compose_key = wParam;
3506         }
3507         if (wParam == VK_APPS)
3508             compose_key = wParam;
3509     }
3510
3511     if (wParam == compose_key) {
3512         if (compose_state == 0
3513             && (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0) compose_state =
3514                 1;
3515         else if (compose_state == 1 && (HIWORD(lParam) & KF_UP))
3516             compose_state = 2;
3517         else
3518             compose_state = 0;
3519     } else if (compose_state == 1 && wParam != VK_CONTROL)
3520         compose_state = 0;
3521
3522     if (compose_state > 1 && left_alt)
3523         compose_state = 0;
3524
3525     /* Sanitize the number pad if not using a PC NumPad */
3526     if (left_alt || (term->app_keypad_keys && !cfg.no_applic_k
3527                      && cfg.funky_type != FUNKY_XTERM)
3528         || cfg.funky_type == FUNKY_VT400 || cfg.nethack_keypad || compose_state) {
3529         if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
3530             int nParam = 0;
3531             switch (wParam) {
3532               case VK_INSERT:
3533                 nParam = VK_NUMPAD0;
3534                 break;
3535               case VK_END:
3536                 nParam = VK_NUMPAD1;
3537                 break;
3538               case VK_DOWN:
3539                 nParam = VK_NUMPAD2;
3540                 break;
3541               case VK_NEXT:
3542                 nParam = VK_NUMPAD3;
3543                 break;
3544               case VK_LEFT:
3545                 nParam = VK_NUMPAD4;
3546                 break;
3547               case VK_CLEAR:
3548                 nParam = VK_NUMPAD5;
3549                 break;
3550               case VK_RIGHT:
3551                 nParam = VK_NUMPAD6;
3552                 break;
3553               case VK_HOME:
3554                 nParam = VK_NUMPAD7;
3555                 break;
3556               case VK_UP:
3557                 nParam = VK_NUMPAD8;
3558                 break;
3559               case VK_PRIOR:
3560                 nParam = VK_NUMPAD9;
3561                 break;
3562               case VK_DELETE:
3563                 nParam = VK_DECIMAL;
3564                 break;
3565             }
3566             if (nParam) {
3567                 if (keystate[VK_NUMLOCK] & 1)
3568                     shift_state |= 1;
3569                 wParam = nParam;
3570             }
3571         }
3572     }
3573
3574     /* If a key is pressed and AltGr is not active */
3575     if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !compose_state) {
3576         /* Okay, prepare for most alts then ... */
3577         if (left_alt)
3578             *p++ = '\033';
3579
3580         /* Lets see if it's a pattern we know all about ... */
3581         if (wParam == VK_PRIOR && shift_state == 1) {
3582             SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
3583             return 0;
3584         }
3585         if (wParam == VK_PRIOR && shift_state == 2) {
3586             SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
3587             return 0;
3588         }
3589         if (wParam == VK_NEXT && shift_state == 1) {
3590             SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
3591             return 0;
3592         }
3593         if (wParam == VK_NEXT && shift_state == 2) {
3594             SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
3595             return 0;
3596         }
3597         if (wParam == VK_INSERT && shift_state == 1) {
3598             term_do_paste(term);
3599             return 0;
3600         }
3601         if (left_alt && wParam == VK_F4 && cfg.alt_f4) {
3602             return -1;
3603         }
3604         if (left_alt && wParam == VK_SPACE && cfg.alt_space) {
3605             SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
3606             return -1;
3607         }
3608         if (left_alt && wParam == VK_RETURN && cfg.fullscreenonaltenter &&
3609             (cfg.resize_action != RESIZE_DISABLED)) {
3610             if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
3611                 flip_full_screen();
3612             return -1;
3613         }
3614         /* Control-Numlock for app-keypad mode switch */
3615         if (wParam == VK_PAUSE && shift_state == 2) {
3616             term->app_keypad_keys ^= 1;
3617             return 0;
3618         }
3619
3620         /* Nethack keypad */
3621         if (cfg.nethack_keypad && !left_alt) {
3622             switch (wParam) {
3623               case VK_NUMPAD1:
3624                 *p++ = shift_state ? 'B' : 'b';
3625                 return p - output;
3626               case VK_NUMPAD2:
3627                 *p++ = shift_state ? 'J' : 'j';
3628                 return p - output;
3629               case VK_NUMPAD3:
3630                 *p++ = shift_state ? 'N' : 'n';
3631                 return p - output;
3632               case VK_NUMPAD4:
3633                 *p++ = shift_state ? 'H' : 'h';
3634                 return p - output;
3635               case VK_NUMPAD5:
3636                 *p++ = shift_state ? '.' : '.';
3637                 return p - output;
3638               case VK_NUMPAD6:
3639                 *p++ = shift_state ? 'L' : 'l';
3640                 return p - output;
3641               case VK_NUMPAD7:
3642                 *p++ = shift_state ? 'Y' : 'y';
3643                 return p - output;
3644               case VK_NUMPAD8:
3645                 *p++ = shift_state ? 'K' : 'k';
3646                 return p - output;
3647               case VK_NUMPAD9:
3648                 *p++ = shift_state ? 'U' : 'u';
3649                 return p - output;
3650             }
3651         }
3652
3653         /* Application Keypad */
3654         if (!left_alt) {
3655             int xkey = 0;
3656
3657             if (cfg.funky_type == FUNKY_VT400 ||
3658                 (cfg.funky_type <= FUNKY_LINUX &&
3659                  term->app_keypad_keys && !cfg.no_applic_k)) switch (wParam) {
3660                   case VK_EXECUTE:
3661                     xkey = 'P';
3662                     break;
3663                   case VK_DIVIDE:
3664                     xkey = 'Q';
3665                     break;
3666                   case VK_MULTIPLY:
3667                     xkey = 'R';
3668                     break;
3669                   case VK_SUBTRACT:
3670                     xkey = 'S';
3671                     break;
3672                 }
3673             if (term->app_keypad_keys && !cfg.no_applic_k)
3674                 switch (wParam) {
3675                   case VK_NUMPAD0:
3676                     xkey = 'p';
3677                     break;
3678                   case VK_NUMPAD1:
3679                     xkey = 'q';
3680                     break;
3681                   case VK_NUMPAD2:
3682                     xkey = 'r';
3683                     break;
3684                   case VK_NUMPAD3:
3685                     xkey = 's';
3686                     break;
3687                   case VK_NUMPAD4:
3688                     xkey = 't';
3689                     break;
3690                   case VK_NUMPAD5:
3691                     xkey = 'u';
3692                     break;
3693                   case VK_NUMPAD6:
3694                     xkey = 'v';
3695                     break;
3696                   case VK_NUMPAD7:
3697                     xkey = 'w';
3698                     break;
3699                   case VK_NUMPAD8:
3700                     xkey = 'x';
3701                     break;
3702                   case VK_NUMPAD9:
3703                     xkey = 'y';
3704                     break;
3705
3706                   case VK_DECIMAL:
3707                     xkey = 'n';
3708                     break;
3709                   case VK_ADD:
3710                     if (cfg.funky_type == FUNKY_XTERM) {
3711                         if (shift_state)
3712                             xkey = 'l';
3713                         else
3714                             xkey = 'k';
3715                     } else if (shift_state)
3716                         xkey = 'm';
3717                     else
3718                         xkey = 'l';
3719                     break;
3720
3721                   case VK_DIVIDE:
3722                     if (cfg.funky_type == FUNKY_XTERM)
3723                         xkey = 'o';
3724                     break;
3725                   case VK_MULTIPLY:
3726                     if (cfg.funky_type == FUNKY_XTERM)
3727                         xkey = 'j';
3728                     break;
3729                   case VK_SUBTRACT:
3730                     if (cfg.funky_type == FUNKY_XTERM)
3731                         xkey = 'm';
3732                     break;
3733
3734                   case VK_RETURN:
3735                     if (HIWORD(lParam) & KF_EXTENDED)
3736                         xkey = 'M';
3737                     break;
3738                 }
3739             if (xkey) {
3740                 if (term->vt52_mode) {
3741                     if (xkey >= 'P' && xkey <= 'S')
3742                         p += sprintf((char *) p, "\x1B%c", xkey);
3743                     else
3744                         p += sprintf((char *) p, "\x1B?%c", xkey);
3745                 } else
3746                     p += sprintf((char *) p, "\x1BO%c", xkey);
3747                 return p - output;
3748             }
3749         }
3750
3751         if (wParam == VK_BACK && shift_state == 0) {    /* Backspace */
3752             *p++ = (cfg.bksp_is_delete ? 0x7F : 0x08);
3753             *p++ = 0;
3754             return -2;
3755         }
3756         if (wParam == VK_BACK && shift_state == 1) {    /* Shift Backspace */
3757             /* We do the opposite of what is configured */
3758             *p++ = (cfg.bksp_is_delete ? 0x08 : 0x7F);
3759             *p++ = 0;
3760             return -2;
3761         }
3762         if (wParam == VK_TAB && shift_state == 1) {     /* Shift tab */
3763             *p++ = 0x1B;
3764             *p++ = '[';
3765             *p++ = 'Z';
3766             return p - output;
3767         }
3768         if (wParam == VK_SPACE && shift_state == 2) {   /* Ctrl-Space */
3769             *p++ = 0;
3770             return p - output;
3771         }
3772         if (wParam == VK_SPACE && shift_state == 3) {   /* Ctrl-Shift-Space */
3773             *p++ = 160;
3774             return p - output;
3775         }
3776         if (wParam == VK_CANCEL && shift_state == 2) {  /* Ctrl-Break */
3777             *p++ = 3;
3778             *p++ = 0;
3779             return -2;
3780         }
3781         if (wParam == VK_PAUSE) {      /* Break/Pause */
3782             *p++ = 26;
3783             *p++ = 0;
3784             return -2;
3785         }
3786         /* Control-2 to Control-8 are special */
3787         if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
3788             *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
3789             return p - output;
3790         }
3791         if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
3792             *p++ = 0x1F;
3793             return p - output;
3794         }
3795         if (shift_state == 2 && wParam == 0xDF) {
3796             *p++ = 0x1C;
3797             return p - output;
3798         }
3799         if (shift_state == 3 && wParam == 0xDE) {
3800             *p++ = 0x1E;               /* Ctrl-~ == Ctrl-^ in xterm at least */
3801             return p - output;
3802         }
3803         if (shift_state == 0 && wParam == VK_RETURN && term->cr_lf_return) {
3804             *p++ = '\r';
3805             *p++ = '\n';
3806             return p - output;
3807         }
3808
3809         /*
3810          * Next, all the keys that do tilde codes. (ESC '[' nn '~',
3811          * for integer decimal nn.)
3812          *
3813          * We also deal with the weird ones here. Linux VCs replace F1
3814          * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
3815          * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
3816          * respectively.
3817          */
3818         code = 0;
3819         switch (wParam) {
3820           case VK_F1:
3821             code = (keystate[VK_SHIFT] & 0x80 ? 23 : 11);
3822             break;
3823           case VK_F2:
3824             code = (keystate[VK_SHIFT] & 0x80 ? 24 : 12);
3825             break;
3826           case VK_F3:
3827             code = (keystate[VK_SHIFT] & 0x80 ? 25 : 13);
3828             break;
3829           case VK_F4:
3830             code = (keystate[VK_SHIFT] & 0x80 ? 26 : 14);
3831             break;
3832           case VK_F5:
3833             code = (keystate[VK_SHIFT] & 0x80 ? 28 : 15);
3834             break;
3835           case VK_F6:
3836             code = (keystate[VK_SHIFT] & 0x80 ? 29 : 17);
3837             break;
3838           case VK_F7:
3839             code = (keystate[VK_SHIFT] & 0x80 ? 31 : 18);
3840             break;
3841           case VK_F8:
3842             code = (keystate[VK_SHIFT] & 0x80 ? 32 : 19);
3843             break;
3844           case VK_F9:
3845             code = (keystate[VK_SHIFT] & 0x80 ? 33 : 20);
3846             break;
3847           case VK_F10:
3848             code = (keystate[VK_SHIFT] & 0x80 ? 34 : 21);
3849             break;
3850           case VK_F11:
3851             code = 23;
3852             break;
3853           case VK_F12:
3854             code = 24;
3855             break;
3856           case VK_F13:
3857             code = 25;
3858             break;
3859           case VK_F14:
3860             code = 26;
3861             break;
3862           case VK_F15:
3863             code = 28;
3864             break;
3865           case VK_F16:
3866             code = 29;
3867             break;
3868           case VK_F17:
3869             code = 31;
3870             break;
3871           case VK_F18:
3872             code = 32;
3873             break;
3874           case VK_F19:
3875             code = 33;
3876             break;
3877           case VK_F20:
3878             code = 34;
3879             break;
3880         }
3881         if ((shift_state&2) == 0) switch (wParam) {
3882           case VK_HOME:
3883             code = 1;
3884             break;
3885           case VK_INSERT:
3886             code = 2;
3887             break;
3888           case VK_DELETE:
3889             code = 3;
3890             break;
3891           case VK_END:
3892             code = 4;
3893             break;
3894           case VK_PRIOR:
3895             code = 5;
3896             break;
3897           case VK_NEXT:
3898             code = 6;
3899             break;
3900         }
3901         /* Reorder edit keys to physical order */
3902         if (cfg.funky_type == FUNKY_VT400 && code <= 6)
3903             code = "\0\2\1\4\5\3\6"[code];
3904
3905         if (term->vt52_mode && code > 0 && code <= 6) {
3906             p += sprintf((char *) p, "\x1B%c", " HLMEIG"[code]);
3907             return p - output;
3908         }
3909
3910         if (cfg.funky_type == FUNKY_SCO &&     /* SCO function keys */
3911             code >= 11 && code <= 34) {
3912             char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
3913             int index = 0;
3914             switch (wParam) {
3915               case VK_F1: index = 0; break;
3916               case VK_F2: index = 1; break;
3917               case VK_F3: index = 2; break;
3918               case VK_F4: index = 3; break;
3919               case VK_F5: index = 4; break;
3920               case VK_F6: index = 5; break;
3921               case VK_F7: index = 6; break;
3922               case VK_F8: index = 7; break;
3923               case VK_F9: index = 8; break;
3924               case VK_F10: index = 9; break;
3925               case VK_F11: index = 10; break;
3926               case VK_F12: index = 11; break;
3927             }
3928             if (keystate[VK_SHIFT] & 0x80) index += 12;
3929             if (keystate[VK_CONTROL] & 0x80) index += 24;
3930             p += sprintf((char *) p, "\x1B[%c", codes[index]);
3931             return p - output;
3932         }
3933         if (cfg.funky_type == FUNKY_SCO &&     /* SCO small keypad */
3934             code >= 1 && code <= 6) {
3935             char codes[] = "HL.FIG";
3936             if (code == 3) {
3937                 *p++ = '\x7F';
3938             } else {
3939                 p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
3940             }
3941             return p - output;
3942         }
3943         if ((term->vt52_mode || cfg.funky_type == FUNKY_VT100P) && code >= 11 && code <= 24) {
3944             int offt = 0;
3945             if (code > 15)
3946                 offt++;
3947             if (code > 21)
3948                 offt++;
3949             if (term->vt52_mode)
3950                 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11 - offt);
3951             else
3952                 p +=
3953                     sprintf((char *) p, "\x1BO%c", code + 'P' - 11 - offt);
3954             return p - output;
3955         }
3956         if (cfg.funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
3957             p += sprintf((char *) p, "\x1B[[%c", code + 'A' - 11);
3958             return p - output;
3959         }
3960         if (cfg.funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
3961             if (term->vt52_mode)
3962                 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11);
3963             else
3964                 p += sprintf((char *) p, "\x1BO%c", code + 'P' - 11);
3965             return p - output;
3966         }
3967         if (cfg.rxvt_homeend && (code == 1 || code == 4)) {
3968             p += sprintf((char *) p, code == 1 ? "\x1B[H" : "\x1BOw");
3969             return p - output;
3970         }
3971         if (code) {
3972             p += sprintf((char *) p, "\x1B[%d~", code);
3973             return p - output;
3974         }
3975
3976         /*
3977          * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
3978          * some reason seems to send VK_CLEAR to Windows...).
3979          */
3980         {
3981             char xkey = 0;
3982             switch (wParam) {
3983               case VK_UP:
3984                 xkey = 'A';
3985                 break;
3986               case VK_DOWN:
3987                 xkey = 'B';
3988                 break;
3989               case VK_RIGHT:
3990                 xkey = 'C';
3991                 break;
3992               case VK_LEFT:
3993                 xkey = 'D';
3994                 break;
3995               case VK_CLEAR:
3996                 xkey = 'G';
3997                 break;
3998             }
3999             if (xkey) {
4000                 if (term->vt52_mode)
4001                     p += sprintf((char *) p, "\x1B%c", xkey);
4002                 else {
4003                     int app_flg = (term->app_cursor_keys && !cfg.no_applic_c);
4004 #if 0
4005                     /*
4006                      * RDB: VT100 & VT102 manuals both state the
4007                      * app cursor keys only work if the app keypad
4008                      * is on.
4009                      * 
4010                      * SGT: That may well be true, but xterm
4011                      * disagrees and so does at least one
4012                      * application, so I've #if'ed this out and the
4013                      * behaviour is back to PuTTY's original: app
4014                      * cursor and app keypad are independently
4015                      * switchable modes. If anyone complains about
4016                      * _this_ I'll have to put in a configurable
4017                      * option.
4018                      */
4019                     if (!term->app_keypad_keys)
4020                         app_flg = 0;
4021 #endif
4022                     /* Useful mapping of Ctrl-arrows */
4023                     if (shift_state == 2)
4024                         app_flg = !app_flg;
4025
4026                     if (app_flg)
4027                         p += sprintf((char *) p, "\x1BO%c", xkey);
4028                     else
4029                         p += sprintf((char *) p, "\x1B[%c", xkey);
4030                 }
4031                 return p - output;
4032             }
4033         }
4034
4035         /*
4036          * Finally, deal with Return ourselves. (Win95 seems to
4037          * foul it up when Alt is pressed, for some reason.)
4038          */
4039         if (wParam == VK_RETURN) {     /* Return */
4040             *p++ = 0x0D;
4041             *p++ = 0;
4042             return -2;
4043         }
4044
4045         if (left_alt && wParam >= VK_NUMPAD0 && wParam <= VK_NUMPAD9)
4046             alt_sum = alt_sum * 10 + wParam - VK_NUMPAD0;
4047         else
4048             alt_sum = 0;
4049     }
4050
4051     /* Okay we've done everything interesting; let windows deal with 
4052      * the boring stuff */
4053     {
4054         BOOL capsOn=0;
4055
4056         /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4057         if(cfg.xlat_capslockcyr && keystate[VK_CAPITAL] != 0) {
4058             capsOn= !left_alt;
4059             keystate[VK_CAPITAL] = 0;
4060         }
4061
4062         /* XXX how do we know what the max size of the keys array should
4063          * be is? There's indication on MS' website of an Inquire/InquireEx
4064          * functioning returning a KBINFO structure which tells us. */
4065         if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) {
4066             /* XXX 'keys' parameter is declared in MSDN documentation as
4067              * 'LPWORD lpChar'.
4068              * The experience of a French user indicates that on
4069              * Win98, WORD[] should be passed in, but on Win2K, it should
4070              * be BYTE[]. German WinXP and my Win2K with "US International"
4071              * driver corroborate this.
4072              * Experimentally I've conditionalised the behaviour on the
4073              * Win9x/NT split, but I suspect it's worse than that.
4074              * See wishlist item `win-dead-keys' for more horrible detail
4075              * and speculations. */
4076             BYTE keybs[3];
4077             int i;
4078             r = ToAsciiEx(wParam, scan, keystate, (LPWORD)keybs, 0, kbd_layout);
4079             for (i=0; i<3; i++) keys[i] = keybs[i];
4080         } else {
4081             r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
4082         }
4083 #ifdef SHOW_TOASCII_RESULT
4084         if (r == 1 && !key_down) {
4085             if (alt_sum) {
4086                 if (in_utf(term) || ucsdata.dbcs_screenfont)
4087                     debug((", (U+%04x)", alt_sum));
4088                 else
4089                     debug((", LCH(%d)", alt_sum));
4090             } else {
4091                 debug((", ACH(%d)", keys[0]));
4092             }
4093         } else if (r > 0) {
4094             int r1;
4095             debug((", ASC("));
4096             for (r1 = 0; r1 < r; r1++) {
4097                 debug(("%s%d", r1 ? "," : "", keys[r1]));
4098             }
4099             debug((")"));
4100         }
4101 #endif
4102         if (r > 0) {
4103             WCHAR keybuf;
4104
4105             /*
4106              * Interrupt an ongoing paste. I'm not sure this is
4107              * sensible, but for the moment it's preferable to
4108              * having to faff about buffering things.
4109              */
4110             term_nopaste(term);
4111
4112             p = output;
4113             for (i = 0; i < r; i++) {
4114                 unsigned char ch = (unsigned char) keys[i];
4115
4116                 if (compose_state == 2 && (ch & 0x80) == 0 && ch > ' ') {
4117                     compose_char = ch;
4118                     compose_state++;
4119                     continue;
4120                 }
4121                 if (compose_state == 3 && (ch & 0x80) == 0 && ch > ' ') {
4122                     int nc;
4123                     compose_state = 0;
4124
4125                     if ((nc = check_compose(compose_char, ch)) == -1) {
4126                         MessageBeep(MB_ICONHAND);
4127                         return 0;
4128                     }
4129                     keybuf = nc;
4130                     term_seen_key_event(term);
4131                     if (ldisc)
4132                         luni_send(ldisc, &keybuf, 1, 1);
4133                     continue;
4134                 }
4135
4136                 compose_state = 0;
4137
4138                 if (!key_down) {
4139                     if (alt_sum) {
4140                         if (in_utf(term) || ucsdata.dbcs_screenfont) {
4141                             keybuf = alt_sum;
4142                             term_seen_key_event(term);
4143                             if (ldisc)
4144                                 luni_send(ldisc, &keybuf, 1, 1);
4145                         } else {
4146                             ch = (char) alt_sum;
4147                             /*
4148                              * We need not bother about stdin
4149                              * backlogs here, because in GUI PuTTY
4150                              * we can't do anything about it
4151                              * anyway; there's no means of asking
4152                              * Windows to hold off on KEYDOWN
4153                              * messages. We _have_ to buffer
4154                              * everything we're sent.
4155                              */
4156                             term_seen_key_event(term);
4157                             if (ldisc)
4158                                 ldisc_send(ldisc, &ch, 1, 1);
4159                         }
4160                         alt_sum = 0;
4161                     } else {
4162                         term_seen_key_event(term);
4163                         if (ldisc)
4164                             lpage_send(ldisc, kbd_codepage, &ch, 1, 1);
4165                     }
4166                 } else {
4167                     if(capsOn && ch < 0x80) {
4168                         WCHAR cbuf[2];
4169                         cbuf[0] = 27;
4170                         cbuf[1] = xlat_uskbd2cyrllic(ch);
4171                         term_seen_key_event(term);
4172                         if (ldisc)
4173                             luni_send(ldisc, cbuf+!left_alt, 1+!!left_alt, 1);
4174                     } else {
4175                         char cbuf[2];
4176                         cbuf[0] = '\033';
4177                         cbuf[1] = ch;
4178                         term_seen_key_event(term);
4179                         if (ldisc)
4180                             lpage_send(ldisc, kbd_codepage,
4181                                        cbuf+!left_alt, 1+!!left_alt, 1);
4182                     }
4183                 }
4184                 show_mouseptr(0);
4185             }
4186
4187             /* This is so the ALT-Numpad and dead keys work correctly. */
4188             keys[0] = 0;
4189
4190             return p - output;
4191         }
4192         /* If we're definitly not building up an ALT-54321 then clear it */
4193         if (!left_alt)
4194             keys[0] = 0;
4195         /* If we will be using alt_sum fix the 256s */
4196         else if (keys[0] && (in_utf(term) || ucsdata.dbcs_screenfont))
4197             keys[0] = 10;
4198     }
4199
4200     /*
4201      * ALT alone may or may not want to bring up the System menu.
4202      * If it's not meant to, we return 0 on presses or releases of
4203      * ALT, to show that we've swallowed the keystroke. Otherwise
4204      * we return -1, which means Windows will give the keystroke
4205      * its default handling (i.e. bring up the System menu).
4206      */
4207     if (wParam == VK_MENU && !cfg.alt_only)
4208         return 0;
4209
4210     return -1;
4211 }
4212
4213 void request_paste(void *frontend)
4214 {
4215     /*
4216      * In Windows, pasting is synchronous: we can read the
4217      * clipboard with no difficulty, so request_paste() can just go
4218      * ahead and paste.
4219      */
4220     term_do_paste(term);
4221 }
4222
4223 void set_title(void *frontend, char *title)
4224 {
4225     sfree(window_name);
4226     window_name = snewn(1 + strlen(title), char);
4227     strcpy(window_name, title);
4228     if (cfg.win_name_always || !IsIconic(hwnd))
4229         SetWindowText(hwnd, title);
4230 }
4231
4232 void set_icon(void *frontend, char *title)
4233 {
4234     sfree(icon_name);
4235     icon_name = snewn(1 + strlen(title), char);
4236     strcpy(icon_name, title);
4237     if (!cfg.win_name_always && IsIconic(hwnd))
4238         SetWindowText(hwnd, title);
4239 }
4240
4241 void set_sbar(void *frontend, int total, int start, int page)
4242 {
4243     SCROLLINFO si;
4244
4245     if (is_full_screen() ? !cfg.scrollbar_in_fullscreen : !cfg.scrollbar)
4246         return;
4247
4248     si.cbSize = sizeof(si);
4249     si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
4250     si.nMin = 0;
4251     si.nMax = total - 1;
4252     si.nPage = page;
4253     si.nPos = start;
4254     if (hwnd)
4255         SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
4256 }
4257
4258 Context get_ctx(void *frontend)
4259 {
4260     HDC hdc;
4261     if (hwnd) {
4262         hdc = GetDC(hwnd);
4263         if (hdc && pal)
4264             SelectPalette(hdc, pal, FALSE);
4265         return hdc;
4266     } else
4267         return NULL;
4268 }
4269
4270 void free_ctx(Context ctx)
4271 {
4272     SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
4273     ReleaseDC(hwnd, ctx);
4274 }
4275
4276 static void real_palette_set(int n, int r, int g, int b)
4277 {
4278     if (pal) {
4279         logpal->palPalEntry[n].peRed = r;
4280         logpal->palPalEntry[n].peGreen = g;
4281         logpal->palPalEntry[n].peBlue = b;
4282         logpal->palPalEntry[n].peFlags = PC_NOCOLLAPSE;
4283         colours[n] = PALETTERGB(r, g, b);
4284         SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
4285     } else
4286         colours[n] = RGB(r, g, b);
4287 }
4288
4289 void palette_set(void *frontend, int n, int r, int g, int b)
4290 {
4291     static const int first[21] = {
4292         0, 2, 4, 6, 8, 10, 12, 14,
4293         1, 3, 5, 7, 9, 11, 13, 15,
4294         16, 17, 18, 20, 22
4295     };
4296     real_palette_set(first[n], r, g, b);
4297     if (first[n] >= 18)
4298         real_palette_set(first[n] + 1, r, g, b);
4299     if (pal) {
4300         HDC hdc = get_ctx(frontend);
4301         UnrealizeObject(pal);
4302         RealizePalette(hdc);
4303         free_ctx(hdc);
4304     }
4305 }
4306
4307 void palette_reset(void *frontend)
4308 {
4309     int i;
4310
4311     for (i = 0; i < NCOLOURS; i++) {
4312         if (pal) {
4313             logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
4314             logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
4315             logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
4316             logpal->palPalEntry[i].peFlags = 0;
4317             colours[i] = PALETTERGB(defpal[i].rgbtRed,
4318                                     defpal[i].rgbtGreen,
4319                                     defpal[i].rgbtBlue);
4320         } else
4321             colours[i] = RGB(defpal[i].rgbtRed,
4322                              defpal[i].rgbtGreen, defpal[i].rgbtBlue);
4323     }
4324
4325     if (pal) {
4326         HDC hdc;
4327         SetPaletteEntries(pal, 0, NCOLOURS, logpal->palPalEntry);
4328         hdc = get_ctx(frontend);
4329         RealizePalette(hdc);
4330         free_ctx(hdc);
4331     }
4332 }
4333
4334 void write_aclip(void *frontend, char *data, int len, int must_deselect)
4335 {
4336     HGLOBAL clipdata;
4337     void *lock;
4338
4339     clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
4340     if (!clipdata)
4341         return;
4342     lock = GlobalLock(clipdata);
4343     if (!lock)
4344         return;
4345     memcpy(lock, data, len);
4346     ((unsigned char *) lock)[len] = 0;
4347     GlobalUnlock(clipdata);
4348
4349     if (!must_deselect)
4350         SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
4351
4352     if (OpenClipboard(hwnd)) {
4353         EmptyClipboard();
4354         SetClipboardData(CF_TEXT, clipdata);
4355         CloseClipboard();
4356     } else
4357         GlobalFree(clipdata);
4358
4359     if (!must_deselect)
4360         SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
4361 }
4362
4363 /*
4364  * Note: unlike write_aclip() this will not append a nul.
4365  */
4366 void write_clip(void *frontend, wchar_t * data, int len, int must_deselect)
4367 {
4368     HGLOBAL clipdata, clipdata2, clipdata3;
4369     int len2;
4370     void *lock, *lock2, *lock3;
4371
4372     len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
4373
4374     clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
4375                            len * sizeof(wchar_t));
4376     clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
4377
4378     if (!clipdata || !clipdata2) {
4379         if (clipdata)
4380             GlobalFree(clipdata);
4381         if (clipdata2)
4382             GlobalFree(clipdata2);
4383         return;
4384     }
4385     if (!(lock = GlobalLock(clipdata)))
4386         return;
4387     if (!(lock2 = GlobalLock(clipdata2)))
4388         return;
4389
4390     memcpy(lock, data, len * sizeof(wchar_t));
4391     WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
4392
4393     if (cfg.rtf_paste) {
4394         wchar_t unitab[256];
4395         char *rtf = NULL;
4396         unsigned char *tdata = (unsigned char *)lock2;
4397         wchar_t *udata = (wchar_t *)lock;
4398         int rtflen = 0, uindex = 0, tindex = 0;
4399         int rtfsize = 0;
4400         int multilen, blen, alen, totallen, i;
4401         char before[16], after[4];
4402
4403         get_unitab(CP_ACP, unitab, 0);
4404
4405         rtfsize = 100 + strlen(cfg.font.name);
4406         rtf = snewn(rtfsize, char);
4407         sprintf(rtf, "{\\rtf1\\ansi%d{\\fonttbl\\f0\\fmodern %s;}\\f0",
4408                 GetACP(), cfg.font.name);
4409         rtflen = strlen(rtf);
4410
4411         /*
4412          * We want to construct a piece of RTF that specifies the
4413          * same Unicode text. To do this we will read back in
4414          * parallel from the Unicode data in `udata' and the
4415          * non-Unicode data in `tdata'. For each character in
4416          * `tdata' which becomes the right thing in `udata' when
4417          * looked up in `unitab', we just copy straight over from
4418          * tdata. For each one that doesn't, we must WCToMB it
4419          * individually and produce a \u escape sequence.
4420          * 
4421          * It would probably be more robust to just bite the bullet
4422          * and WCToMB each individual Unicode character one by one,
4423          * then MBToWC each one back to see if it was an accurate
4424          * translation; but that strikes me as a horrifying number
4425          * of Windows API calls so I want to see if this faster way
4426          * will work. If it screws up badly we can always revert to
4427          * the simple and slow way.
4428          */
4429         while (tindex < len2 && uindex < len &&
4430                tdata[tindex] && udata[uindex]) {
4431             if (tindex + 1 < len2 &&
4432                 tdata[tindex] == '\r' &&
4433                 tdata[tindex+1] == '\n') {
4434                 tindex++;
4435                 uindex++;
4436             }
4437             if (unitab[tdata[tindex]] == udata[uindex]) {
4438                 multilen = 1;
4439                 before[0] = '\0';
4440                 after[0] = '\0';
4441                 blen = alen = 0;
4442             } else {
4443                 multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
4444                                                NULL, 0, NULL, NULL);
4445                 if (multilen != 1) {
4446                     blen = sprintf(before, "{\\uc%d\\u%d", multilen,
4447                                    udata[uindex]);
4448                     alen = 1; strcpy(after, "}");
4449                 } else {
4450                     blen = sprintf(before, "\\u%d", udata[uindex]);
4451                     alen = 0; after[0] = '\0';
4452                 }
4453             }
4454             assert(tindex + multilen <= len2);
4455             totallen = blen + alen;
4456             for (i = 0; i < multilen; i++) {
4457                 if (tdata[tindex+i] == '\\' ||
4458                     tdata[tindex+i] == '{' ||
4459                     tdata[tindex+i] == '}')
4460                     totallen += 2;
4461                 else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A)
4462                     totallen += 6;     /* \par\r\n */
4463                 else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20)
4464                     totallen += 4;
4465                 else
4466                     totallen++;
4467             }
4468
4469             if (rtfsize < rtflen + totallen + 3) {
4470                 rtfsize = rtflen + totallen + 512;
4471                 rtf = sresize(rtf, rtfsize, char);
4472             }
4473
4474             strcpy(rtf + rtflen, before); rtflen += blen;
4475             for (i = 0; i < multilen; i++) {
4476                 if (tdata[tindex+i] == '\\' ||
4477                     tdata[tindex+i] == '{' ||
4478                     tdata[tindex+i] == '}') {
4479                     rtf[rtflen++] = '\\';
4480                     rtf[rtflen++] = tdata[tindex+i];
4481                 } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
4482                     rtflen += sprintf(rtf+rtflen, "\\par\r\n");
4483                 } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
4484                     rtflen += sprintf(rtf+rtflen, "\\'%02x", tdata[tindex+i]);
4485                 } else {
4486                     rtf[rtflen++] = tdata[tindex+i];
4487                 }
4488             }
4489             strcpy(rtf + rtflen, after); rtflen += alen;
4490
4491             tindex += multilen;
4492             uindex++;
4493         }
4494
4495         strcpy(rtf + rtflen, "}");
4496         rtflen += 2;
4497
4498         clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtflen);
4499         if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
4500             strcpy(lock3, rtf);
4501             GlobalUnlock(clipdata3);
4502         }
4503         sfree(rtf);
4504     } else
4505         clipdata3 = NULL;
4506
4507     GlobalUnlock(clipdata);
4508     GlobalUnlock(clipdata2);
4509
4510     if (!must_deselect)
4511         SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
4512
4513     if (OpenClipboard(hwnd)) {
4514         EmptyClipboard();
4515         SetClipboardData(CF_UNICODETEXT, clipdata);
4516         SetClipboardData(CF_TEXT, clipdata2);
4517         if (clipdata3)
4518             SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
4519         CloseClipboard();
4520     } else {
4521         GlobalFree(clipdata);
4522         GlobalFree(clipdata2);
4523     }
4524
4525     if (!must_deselect)
4526         SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
4527 }
4528
4529 void get_clip(void *frontend, wchar_t ** p, int *len)
4530 {
4531     static HGLOBAL clipdata = NULL;
4532     static wchar_t *converted = 0;
4533     wchar_t *p2;
4534
4535     if (converted) {
4536         sfree(converted);
4537         converted = 0;
4538     }
4539     if (!p) {
4540         if (clipdata)
4541             GlobalUnlock(clipdata);
4542         clipdata = NULL;
4543         return;
4544     } else if (OpenClipboard(NULL)) {
4545         if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
4546             CloseClipboard();
4547             *p = GlobalLock(clipdata);
4548             if (*p) {
4549                 for (p2 = *p; *p2; p2++);
4550                 *len = p2 - *p;
4551                 return;
4552             }
4553         } else if ( (clipdata = GetClipboardData(CF_TEXT)) ) {
4554             char *s;
4555             int i;
4556             CloseClipboard();
4557             s = GlobalLock(clipdata);
4558             i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
4559             *p = converted = snewn(i, wchar_t);
4560             MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, converted, i);
4561             *len = i - 1;
4562             return;
4563         } else
4564             CloseClipboard();
4565     }
4566
4567     *p = NULL;
4568     *len = 0;
4569 }
4570
4571 #if 0
4572 /*
4573  * Move `lines' lines from position `from' to position `to' in the
4574  * window.
4575  */
4576 void optimised_move(void *frontend, int to, int from, int lines)
4577 {
4578     RECT r;
4579     int min, max;
4580
4581     min = (to < from ? to : from);
4582     max = to + from - min;
4583
4584     r.left = offset_width;
4585     r.right = offset_width + term->cols * font_width;
4586     r.top = offset_height + min * font_height;
4587     r.bottom = offset_height + (max + lines) * font_height;
4588     ScrollWindow(hwnd, 0, (to - from) * font_height, &r, &r);
4589 }
4590 #endif
4591
4592 /*
4593  * Print a message box and perform a fatal exit.
4594  */
4595 void fatalbox(char *fmt, ...)
4596 {
4597     va_list ap;
4598     char *stuff, morestuff[100];
4599
4600     va_start(ap, fmt);
4601     stuff = dupvprintf(fmt, ap);
4602     va_end(ap);
4603     sprintf(morestuff, "%.70s Fatal Error", appname);
4604     MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
4605     sfree(stuff);
4606     cleanup_exit(1);
4607 }
4608
4609 /*
4610  * Print a modal (Really Bad) message box and perform a fatal exit.
4611  */
4612 void modalfatalbox(char *fmt, ...)
4613 {
4614     va_list ap;
4615     char *stuff, morestuff[100];
4616
4617     va_start(ap, fmt);
4618     stuff = dupvprintf(fmt, ap);
4619     va_end(ap);
4620     sprintf(morestuff, "%.70s Fatal Error", appname);
4621     MessageBox(hwnd, stuff, morestuff,
4622                MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
4623     sfree(stuff);
4624     cleanup_exit(1);
4625 }
4626
4627 /*
4628  * Manage window caption / taskbar flashing, if enabled.
4629  * 0 = stop, 1 = maintain, 2 = start
4630  */
4631 static void flash_window(int mode)
4632 {
4633     static long last_flash = 0;
4634     static int flashing = 0;
4635     if ((mode == 0) || (cfg.beep_ind == B_IND_DISABLED)) {
4636         /* stop */
4637         if (flashing) {
4638             FlashWindow(hwnd, FALSE);
4639             flashing = 0;
4640         }
4641
4642     } else if (mode == 2) {
4643         /* start */
4644         if (!flashing) {
4645             last_flash = GetTickCount();
4646             flashing = 1;
4647             FlashWindow(hwnd, TRUE);
4648         }
4649
4650     } else if ((mode == 1) && (cfg.beep_ind == B_IND_FLASH)) {
4651         /* maintain */
4652         if (flashing) {
4653             long now = GetTickCount();
4654             long fdiff = now - last_flash;
4655             if (fdiff < 0 || fdiff > 450) {
4656                 last_flash = now;
4657                 FlashWindow(hwnd, TRUE);        /* toggle */
4658             }
4659         }
4660     }
4661 }
4662
4663 /*
4664  * Beep.
4665  */
4666 void beep(void *frontend, int mode)
4667 {
4668     if (mode == BELL_DEFAULT) {
4669         /*
4670          * For MessageBeep style bells, we want to be careful of
4671          * timing, because they don't have the nice property of
4672          * PlaySound bells that each one cancels the previous
4673          * active one. So we limit the rate to one per 50ms or so.
4674          */
4675         static long lastbeep = 0;
4676         long beepdiff;
4677
4678         beepdiff = GetTickCount() - lastbeep;
4679         if (beepdiff >= 0 && beepdiff < 50)
4680             return;
4681         MessageBeep(MB_OK);
4682         /*
4683          * The above MessageBeep call takes time, so we record the
4684          * time _after_ it finishes rather than before it starts.
4685          */
4686         lastbeep = GetTickCount();
4687     } else if (mode == BELL_WAVEFILE) {
4688         if (!PlaySound(cfg.bell_wavefile.path, NULL,
4689                        SND_ASYNC | SND_FILENAME)) {
4690             char buf[sizeof(cfg.bell_wavefile.path) + 80];
4691             char otherbuf[100];
4692             sprintf(buf, "Unable to play sound file\n%s\n"
4693                     "Using default sound instead", cfg.bell_wavefile.path);
4694             sprintf(otherbuf, "%.70s Sound Error", appname);
4695             MessageBox(hwnd, buf, otherbuf,
4696                        MB_OK | MB_ICONEXCLAMATION);
4697             cfg.beep = BELL_DEFAULT;
4698         }
4699     } else if (mode == BELL_PCSPEAKER) {
4700         static long lastbeep = 0;
4701         long beepdiff;
4702
4703         beepdiff = GetTickCount() - lastbeep;
4704         if (beepdiff >= 0 && beepdiff < 50)
4705             return;
4706
4707         /*
4708          * We must beep in different ways depending on whether this
4709          * is a 95-series or NT-series OS.
4710          */
4711         if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
4712             Beep(800, 100);
4713         else
4714             MessageBeep(-1);
4715         lastbeep = GetTickCount();
4716     }
4717     /* Otherwise, either visual bell or disabled; do nothing here */
4718     if (!term->has_focus) {
4719         flash_window(2);               /* start */
4720     }
4721 }
4722
4723 /*
4724  * Minimise or restore the window in response to a server-side
4725  * request.
4726  */
4727 void set_iconic(void *frontend, int iconic)
4728 {
4729     if (IsIconic(hwnd)) {
4730         if (!iconic)
4731             ShowWindow(hwnd, SW_RESTORE);
4732     } else {
4733         if (iconic)
4734             ShowWindow(hwnd, SW_MINIMIZE);
4735     }
4736 }
4737
4738 /*
4739  * Move the window in response to a server-side request.
4740  */
4741 void move_window(void *frontend, int x, int y)
4742 {
4743     if (cfg.resize_action == RESIZE_DISABLED || 
4744         cfg.resize_action == RESIZE_FONT ||
4745         IsZoomed(hwnd))
4746        return;
4747
4748     SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
4749 }
4750
4751 /*
4752  * Move the window to the top or bottom of the z-order in response
4753  * to a server-side request.
4754  */
4755 void set_zorder(void *frontend, int top)
4756 {
4757     if (cfg.alwaysontop)
4758         return;                        /* ignore */
4759     SetWindowPos(hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
4760                  SWP_NOMOVE | SWP_NOSIZE);
4761 }
4762
4763 /*
4764  * Refresh the window in response to a server-side request.
4765  */
4766 void refresh_window(void *frontend)
4767 {
4768     InvalidateRect(hwnd, NULL, TRUE);
4769 }
4770
4771 /*
4772  * Maximise or restore the window in response to a server-side
4773  * request.
4774  */
4775 void set_zoomed(void *frontend, int zoomed)
4776 {
4777     if (IsZoomed(hwnd)) {
4778         if (!zoomed)
4779             ShowWindow(hwnd, SW_RESTORE);
4780     } else {
4781         if (zoomed)
4782             ShowWindow(hwnd, SW_MAXIMIZE);
4783     }
4784 }
4785
4786 /*
4787  * Report whether the window is iconic, for terminal reports.
4788  */
4789 int is_iconic(void *frontend)
4790 {
4791     return IsIconic(hwnd);
4792 }
4793
4794 /*
4795  * Report the window's position, for terminal reports.
4796  */
4797 void get_window_pos(void *frontend, int *x, int *y)
4798 {
4799     RECT r;
4800     GetWindowRect(hwnd, &r);
4801     *x = r.left;
4802     *y = r.top;
4803 }
4804
4805 /*
4806  * Report the window's pixel size, for terminal reports.
4807  */
4808 void get_window_pixels(void *frontend, int *x, int *y)
4809 {
4810     RECT r;
4811     GetWindowRect(hwnd, &r);
4812     *x = r.right - r.left;
4813     *y = r.bottom - r.top;
4814 }
4815
4816 /*
4817  * Return the window or icon title.
4818  */
4819 char *get_window_title(void *frontend, int icon)
4820 {
4821     return icon ? icon_name : window_name;
4822 }
4823
4824 /*
4825  * See if we're in full-screen mode.
4826  */
4827 static int is_full_screen()
4828 {
4829     if (!IsZoomed(hwnd))
4830         return FALSE;
4831     if (GetWindowLong(hwnd, GWL_STYLE) & WS_CAPTION)
4832         return FALSE;
4833     return TRUE;
4834 }
4835
4836 /* Get the rect/size of a full screen window using the nearest available
4837  * monitor in multimon systems; default to something sensible if only
4838  * one monitor is present. */
4839 static int get_fullscreen_rect(RECT * ss)
4840 {
4841 #if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
4842         HMONITOR mon;
4843         MONITORINFO mi;
4844         mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
4845         mi.cbSize = sizeof(mi);
4846         GetMonitorInfo(mon, &mi);
4847
4848         /* structure copy */
4849         *ss = mi.rcMonitor;
4850         return TRUE;
4851 #else
4852 /* could also use code like this:
4853         ss->left = ss->top = 0;
4854         ss->right = GetSystemMetrics(SM_CXSCREEN);
4855         ss->bottom = GetSystemMetrics(SM_CYSCREEN);
4856 */ 
4857         return GetClientRect(GetDesktopWindow(), ss);
4858 #endif
4859 }
4860
4861
4862 /*
4863  * Go full-screen. This should only be called when we are already
4864  * maximised.
4865  */
4866 static void make_full_screen()
4867 {
4868     DWORD style;
4869         RECT ss;
4870
4871     assert(IsZoomed(hwnd));
4872
4873         if (is_full_screen())
4874                 return;
4875         
4876     /* Remove the window furniture. */
4877     style = GetWindowLong(hwnd, GWL_STYLE);
4878     style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
4879     if (cfg.scrollbar_in_fullscreen)
4880         style |= WS_VSCROLL;
4881     else
4882         style &= ~WS_VSCROLL;
4883     SetWindowLong(hwnd, GWL_STYLE, style);
4884
4885     /* Resize ourselves to exactly cover the nearest monitor. */
4886         get_fullscreen_rect(&ss);
4887     SetWindowPos(hwnd, HWND_TOP, ss.left, ss.top,
4888                         ss.right - ss.left,
4889                         ss.bottom - ss.top,
4890                         SWP_FRAMECHANGED);
4891
4892     /* Tick the menu item in the System menu. */
4893     CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
4894                   MF_CHECKED);
4895 }
4896
4897 /*
4898  * Clear the full-screen attributes.
4899  */
4900 static void clear_full_screen()
4901 {
4902     DWORD oldstyle, style;
4903
4904     /* Reinstate the window furniture. */
4905     style = oldstyle = GetWindowLong(hwnd, GWL_STYLE);
4906     style |= WS_CAPTION | WS_BORDER;
4907     if (cfg.resize_action == RESIZE_DISABLED)
4908         style &= ~WS_THICKFRAME;
4909     else
4910         style |= WS_THICKFRAME;
4911     if (cfg.scrollbar)
4912         style |= WS_VSCROLL;
4913     else
4914         style &= ~WS_VSCROLL;
4915     if (style != oldstyle) {
4916         SetWindowLong(hwnd, GWL_STYLE, style);
4917         SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
4918                      SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
4919                      SWP_FRAMECHANGED);
4920     }
4921
4922     /* Untick the menu item in the System menu. */
4923     CheckMenuItem(GetSystemMenu(hwnd, FALSE), IDM_FULLSCREEN,
4924                   MF_UNCHECKED);
4925 }
4926
4927 /*
4928  * Toggle full-screen mode.
4929  */
4930 static void flip_full_screen()
4931 {
4932     if (is_full_screen()) {
4933         ShowWindow(hwnd, SW_RESTORE);
4934     } else if (IsZoomed(hwnd)) {
4935         make_full_screen();
4936     } else {
4937         SendMessage(hwnd, WM_FULLSCR_ON_MAX, 0, 0);
4938         ShowWindow(hwnd, SW_MAXIMIZE);
4939     }
4940 }
4941
4942 void frontend_keypress(void *handle)
4943 {
4944     /*
4945      * Keypress termination in non-Close-On-Exit mode is not
4946      * currently supported in PuTTY proper, because the window
4947      * always has a perfectly good Close button anyway. So we do
4948      * nothing here.
4949      */
4950     return;
4951 }
4952
4953 int from_backend(void *frontend, int is_stderr, const char *data, int len)
4954 {
4955     return term_data(term, is_stderr, data, len);
4956 }
4957
4958 void agent_schedule_callback(void (*callback)(void *, void *, int),
4959                              void *callback_ctx, void *data, int len)
4960 {
4961     struct agent_callback *c = snew(struct agent_callback);
4962     c->callback = callback;
4963     c->callback_ctx = callback_ctx;
4964     c->data = data;
4965     c->len = len;
4966     PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);
4967 }