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