Reinstate a piece of code accidentally removed in r9214, where Windows
[u/mdw/putty] / windows / window.c
CommitLineData
eaf1e20a 1/*
2 * window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
3 * emulator and backend in a window.
4 */
5
374330e2 6#include <stdio.h>
7#include <stdlib.h>
1d470ad2 8#include <ctype.h>
ec55b220 9#include <time.h>
95587c73 10#include <limits.h>
a7419ea4 11#include <assert.h>
374330e2 12
06c3eadd 13#ifndef NO_MULTIMON
14#define COMPILE_MULTIMON_STUBS
15#endif
16
32874aea 17#define PUTTY_DO_GLOBALS /* actually _define_ globals */
374330e2 18#include "putty.h"
887035a5 19#include "terminal.h"
d5859615 20#include "storage.h"
374330e2 21#include "win_res.h"
22
7440fd44 23#ifndef NO_MULTIMON
7440fd44 24#include <multimon.h>
25#endif
7440fd44 26
27#include <imm.h>
28#include <commctrl.h>
29#include <richedit.h>
30#include <mmsystem.h>
31
ce0b6f45 32/* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
33 * wParam are used by Windows, and should be masked off, so we shouldn't
34 * attempt to store information in them. Hence all these identifiers have
865e82ad 35 * the low 4 bits clear. Also, identifiers should < 0xF000. */
ce0b6f45 36
6833a413 37#define IDM_SHOWLOG 0x0010
38#define IDM_NEWSESS 0x0020
39#define IDM_DUPSESS 0x0030
e3be8de5 40#define IDM_RESTART 0x0040
41#define IDM_RECONF 0x0050
42#define IDM_CLRSB 0x0060
43#define IDM_RESET 0x0070
70133c0e 44#define IDM_HELP 0x0140
45#define IDM_ABOUT 0x0150
46#define IDM_SAVEDSESS 0x0160
47#define IDM_COPYALL 0x0170
48#define IDM_FULLSCREEN 0x0180
63be7767 49#define IDM_PASTE 0x0190
45a22cae 50#define IDM_SPECIALSEP 0x0200
6833a413 51
125105d1 52#define IDM_SPECIAL_MIN 0x0400
53#define IDM_SPECIAL_MAX 0x0800
54
6833a413 55#define IDM_SAVED_MIN 0x1000
865e82ad 56#define IDM_SAVED_MAX 0x5000
57#define MENU_SAVED_STEP 16
1f1e3232 58/* Maximum number of sessions on saved-session submenu */
865e82ad 59#define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
374330e2 60
604fab0c 61#define WM_IGNORE_CLIP (WM_APP + 2)
62#define WM_FULLSCR_ON_MAX (WM_APP + 3)
63#define WM_AGENT_CALLBACK (WM_APP + 4)
1d432454 64#define WM_GOT_CLIPDATA (WM_APP + 6)
374330e2 65
3cf144db 66/* Needed for Chinese support and apparently not always defined. */
67#ifndef VK_PROCESSKEY
68#define VK_PROCESSKEY 0xE5
69#endif
70
3f0f9388 71/* Mouse wheel support. */
5eaf1e06 72#ifndef WM_MOUSEWHEEL
3f0f9388 73#define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
74#endif
75#ifndef WHEEL_DELTA
76#define WHEEL_DELTA 120
5eaf1e06 77#endif
78
2c02ed02 79static Mouse_Button translate_button(Mouse_Button button);
32874aea 80static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
81static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
82 unsigned char *output);
4a693cfc 83static void conftopalette(void);
26d1da7b 84static void systopalette(void);
374330e2 85static void init_palette(void);
5a73255e 86static void init_fonts(int, int);
4eeb7d09 87static void another_font(int);
88static void deinit_fonts(void);
a5ce2e9f 89static void set_input_locale(HKL);
12745e35 90static void update_savedsess_menu(void);
09644e07 91static void init_flashwindow(void);
374330e2 92
1ba99d2c 93static int is_full_screen(void);
94static void make_full_screen(void);
95static void clear_full_screen(void);
96static void flip_full_screen(void);
1d432454 97static int process_clipdata(HGLOBAL clipdata, int unicode);
1ba99d2c 98
5a73255e 99/* Window layout information */
100static void reset_window(int);
a401e5f3 101static int extra_width, extra_height;
14ce9887 102static int font_width, font_height, font_dualwidth, font_varpitch;
5a73255e 103static int offset_width, offset_height;
104static int was_zoomed = 0;
105static int prev_rows, prev_cols;
106
56f8ea28 107static int pending_netevent = 0;
108static WPARAM pend_netevent_wParam = 0;
109static LPARAM pend_netevent_lParam = 0;
110static void enact_pending_netevent(void);
f8a28d1f 111static void flash_window(int mode);
c1da5066 112static void sys_cursor_update(void);
d0f1bcb4 113static int get_fullscreen_rect(RECT * ss);
59ad2c03 114
c1da5066 115static int caret_x = -1, caret_y = -1;
116
0b4f0bc0 117static int kbd_codepage;
118
b9d7bcad 119static void *ldisc;
6b78788a 120static Backend *back;
121static void *backhandle;
b9d7bcad 122
21d2b241 123static struct unicode_data ucsdata;
95587c73 124static int must_close_session, session_closed;
68a66cc3 125static int reconfiguring = FALSE;
0b4f0bc0 126
45a22cae 127static const struct telnet_special *specials = NULL;
128static HMENU specials_menu = NULL;
129static int n_specials = 0;
125105d1 130
1d432454 131static wchar_t *clipboard_contents;
132static size_t clipboard_length;
133
39934deb 134#define TIMING_TIMER_ID 1234
135static long timing_next_time;
136
63be7767 137static struct {
138 HMENU menu;
63be7767 139} popup_menus[2];
140enum { SYSMENU, CTXMENU };
12745e35 141static HMENU savedsess_menu;
63be7767 142
4a693cfc 143Conf *conf; /* exported to windlg.c */
144
145static void conf_cache_data(void);
146int cursor_type;
147int vtmode;
3ea863a3 148
12745e35 149static struct sesslist sesslist; /* for saved-session menu */
0b4f0bc0 150
c44bf5bd 151struct agent_callback {
152 void (*callback)(void *, void *, int);
153 void *callback_ctx;
154 void *data;
155 int len;
156};
157
374330e2 158#define FONT_NORMAL 0
159#define FONT_BOLD 1
160#define FONT_UNDERLINE 2
161#define FONT_BOLDUND 3
4eeb7d09 162#define FONT_WIDE 0x04
163#define FONT_HIGH 0x08
164#define FONT_NARROW 0x10
5a73255e 165
4eeb7d09 166#define FONT_OEM 0x20
167#define FONT_OEMBOLD 0x21
168#define FONT_OEMUND 0x22
169#define FONT_OEMBOLDUND 0x23
5a73255e 170
edb548cd 171#define FONT_MAXNO 0x40
4eeb7d09 172#define FONT_SHIFT 5
173static HFONT fonts[FONT_MAXNO];
71f6951d 174static LOGFONT lfont;
4eeb7d09 175static int fontflag[FONT_MAXNO];
374330e2 176static enum {
863c5362 177 BOLD_NONE, BOLD_SHADOW, BOLD_FONT
178} bold_font_mode;
179static int bold_colours;
374330e2 180static enum {
181 UND_LINE, UND_FONT
182} und_mode;
183static int descent;
184
04a8208b 185#define NCFGCOLOURS 22
cecb13f6 186#define NEXTCOLOURS 240
187#define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
188static COLORREF colours[NALLCOLOURS];
374330e2 189static HPALETTE pal;
190static LPLOGPALETTE logpal;
cecb13f6 191static RGBTRIPLE defpal[NALLCOLOURS];
374330e2 192
934c0b7a 193static HBITMAP caretbm;
194
374330e2 195static int dbltime, lasttime, lastact;
196static Mouse_Button lastbtn;
197
01c034ad 198/* this allows xterm-style mouse handling. */
199static int send_raw_mouse = 0;
200static int wheel_accumulator = 0;
201
755e0524 202static int busy_status = BUSY_NOT;
203
374330e2 204static char *window_name, *icon_name;
205
03f23ad6 206static int compose_state = 0;
207
011b0e33 208static UINT wm_mousewheel = WM_MOUSEWHEEL;
209
b03db5b6 210#define IS_HIGH_VARSEL(wch1, wch2) \
211 ((wch1) == 0xDB40 && ((wch2) >= 0xDD00 && (wch2) <= 0xDDEF))
212#define IS_LOW_VARSEL(wch) \
213 (((wch) >= 0x180B && (wch) <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR */ \
214 ((wch) >= 0xFE00 && (wch) <= 0xFE0F)) /* VARIATION SELECTOR 1-16 */
215
0965bee0 216/* Dummy routine, only required in plink. */
b9d7bcad 217void ldisc_update(void *frontend, int echo, int edit)
32874aea 218{
219}
6f34e365 220
c6ccd5c2 221char *get_ttymode(void *frontend, const char *mode)
222{
223 return term_get_ttymode(term, mode);
224}
225
e3be8de5 226static void start_backend(void)
227{
228 const char *error;
229 char msg[1024], *title;
230 char *realhost;
231 int i;
232
233 /*
234 * Select protocol. This is farmed out into a table in a
235 * separate file to enable an ssh-free variant.
236 */
4a693cfc 237 back = backend_from_proto(conf_get_int(conf, CONF_protocol));
e3be8de5 238 if (back == NULL) {
239 char *str = dupprintf("%s Internal Error", appname);
240 MessageBox(NULL, "Unsupported protocol number found",
241 str, MB_OK | MB_ICONEXCLAMATION);
242 sfree(str);
243 cleanup_exit(1);
244 }
245
4a693cfc 246 error = back->init(NULL, &backhandle, conf,
247 conf_get_str(conf, CONF_host),
248 conf_get_int(conf, CONF_port),
249 &realhost,
250 conf_get_int(conf, CONF_tcp_nodelay),
251 conf_get_int(conf, CONF_tcp_keepalives));
e3be8de5 252 back->provide_logctx(backhandle, logctx);
253 if (error) {
254 char *str = dupprintf("%s Error", appname);
255 sprintf(msg, "Unable to open connection to\n"
4a693cfc 256 "%.800s\n" "%s", conf_dest(conf), error);
e3be8de5 257 MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
258 sfree(str);
259 exit(0);
260 }
261 window_name = icon_name = NULL;
4a693cfc 262 title = conf_get_str(conf, CONF_wintitle);
263 if (!*title) {
e3be8de5 264 sprintf(msg, "%s - %s", realhost, appname);
265 title = msg;
266 }
267 sfree(realhost);
268 set_title(NULL, title);
269 set_icon(NULL, title);
270
271 /*
272 * Connect the terminal to the backend for resize purposes.
273 */
274 term_provide_resize_fn(term, back->size, backhandle);
275
276 /*
277 * Set up a line discipline.
278 */
4a693cfc 279 ldisc = ldisc_create(conf, term, back, backhandle, NULL);
e3be8de5 280
281 /*
282 * Destroy the Restart Session menu item. (This will return
283 * failure if it's already absent, as it will be the very first
284 * time we call this function. We ignore that, because as long
285 * as the menu item ends up not being there, we don't care
286 * whether it was us who removed it or not!)
287 */
288 for (i = 0; i < lenof(popup_menus); i++) {
289 DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
290 }
291
8eec6d96 292 must_close_session = FALSE;
e3be8de5 293 session_closed = FALSE;
294}
295
296static void close_session(void)
297{
298 char morestuff[100];
299 int i;
300
301 session_closed = TRUE;
302 sprintf(morestuff, "%.70s (inactive)", appname);
303 set_icon(NULL, morestuff);
304 set_title(NULL, morestuff);
305
306 if (ldisc) {
307 ldisc_free(ldisc);
308 ldisc = NULL;
309 }
310 if (back) {
311 back->free(backhandle);
312 backhandle = NULL;
313 back = NULL;
c1190260 314 term_provide_resize_fn(term, NULL, NULL);
e3be8de5 315 update_specials_menu(NULL);
316 }
317
318 /*
319 * Show the Restart Session menu item. Do a precautionary
320 * delete first to ensure we never end up with more than one.
321 */
322 for (i = 0; i < lenof(popup_menus); i++) {
323 DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
324 InsertMenu(popup_menus[i].menu, IDM_DUPSESS, MF_BYCOMMAND | MF_ENABLED,
325 IDM_RESTART, "&Restart Session");
326 }
4f1aa9a9 327
328 /*
329 * Unset the 'must_close_session' flag, or else we'll come
330 * straight back here the next time we go round the main message
331 * loop - which, worse still, will be immediately (without
332 * blocking) because we've just triggered a WM_SETTEXT by the
333 * window title change above.
334 */
335 must_close_session = FALSE;
e3be8de5 336}
337
32874aea 338int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
339{
374330e2 340 WNDCLASS wndclass;
341 MSG msg;
073e9f42 342 HRESULT hr;
374330e2 343 int guess_width, guess_height;
344
8c3cd914 345 hinst = inst;
28339579 346 hwnd = NULL;
67779be7 347 flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
73251d5d 348
8df7a775 349 sk_init();
374330e2 350
351 InitCommonControls();
352
301b66db 353 /* Ensure a Maximize setting in Explorer doesn't maximise the
354 * config box. */
355 defuse_showwindow();
356
4c48c989 357 if (!init_winver())
88485e4d 358 {
4c48c989 359 char *str = dupprintf("%s Fatal Error", appname);
360 MessageBox(NULL, "Windows refuses to report a version",
361 str, MB_OK | MB_ICONEXCLAMATION);
362 sfree(str);
363 return 1;
88485e4d 364 }
365
374330e2 366 /*
011b0e33 367 * If we're running a version of Windows that doesn't support
368 * WM_MOUSEWHEEL, find out what message number we should be
369 * using instead.
370 */
371 if (osVersion.dwMajorVersion < 4 ||
372 (osVersion.dwMajorVersion == 4 &&
373 osVersion.dwPlatformId != VER_PLATFORM_WIN32_NT))
374 wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
375
cb2708d3 376 init_help();
70133c0e 377
09644e07 378 init_flashwindow();
379
4a693cfc 380 conf = conf_new();
381
70133c0e 382 /*
073e9f42 383 * Initialize COM.
384 */
385 hr = CoInitialize(NULL);
386 if (hr != S_OK && hr != S_FALSE) {
387 char *str = dupprintf("%s Fatal Error", appname);
388 MessageBox(NULL, "Failed to initialize COM subsystem",
389 str, MB_OK | MB_ICONEXCLAMATION);
390 sfree(str);
391 return 1;
392 }
393
394 /*
374330e2 395 * Process the command line.
396 */
397 {
398 char *p;
c0a81592 399 int got_host = 0;
df7c7e52 400 /* By default, we bring up the config dialog, rather than launching
401 * a session. This gets set to TRUE if something happens to change
402 * that (e.g., a hostname is specified on the command-line). */
403 int allow_launch = FALSE;
374330e2 404
ffa79828 405 default_protocol = be_default_protocol;
406 /* Find the appropriate default port. */
407 {
9e164d82 408 Backend *b = backend_from_proto(default_protocol);
0d598aa7 409 default_port = 0; /* illegal */
9e164d82 410 if (b)
411 default_port = b->default_port;
ffa79828 412 }
4a693cfc 413 conf_set_int(conf, CONF_logtype, LGTYP_NONE);
e277c42d 414
4a693cfc 415 do_defaults(NULL, conf);
374330e2 416
417 p = cmdline;
374330e2 418
419 /*
c0a81592 420 * Process a couple of command-line options which are more
073e9f42 421 * easily dealt with before the line is broken up into words.
422 * These are the old-fashioned but convenient @sessionname and
423 * the internal-use-only &sharedmemoryhandle, neither of which
424 * are combined with anything else.
e277c42d 425 */
c0a81592 426 while (*p && isspace(*p))
e277c42d 427 p++;
374330e2 428 if (*p == '@') {
073e9f42 429 /*
430 * An initial @ means that the whole of the rest of the
431 * command line should be treated as the name of a saved
432 * session, with _no quoting or escaping_. This makes it a
433 * very convenient means of automated saved-session
434 * launching, via IDM_SAVEDSESS or Windows 7 jump lists.
435 */
f317611e 436 int i = strlen(p);
32874aea 437 while (i > 1 && isspace(p[i - 1]))
f317611e 438 i--;
439 p[i] = '\0';
4a693cfc 440 do_defaults(p + 1, conf);
441 if (!conf_launchable(conf) && !do_config()) {
7440fd44 442 cleanup_exit(0);
374330e2 443 }
df7c7e52 444 allow_launch = TRUE; /* allow it to be launched directly */
374330e2 445 } else if (*p == '&') {
446 /*
447 * An initial & means we've been given a command line
448 * containing the hex value of a HANDLE for a file
4a693cfc 449 * mapping object, which we must then interpret as a
450 * serialised Conf.
374330e2 451 */
452 HANDLE filemap;
4a693cfc 453 void *cp;
454 unsigned cpsize;
18186560 455 if (sscanf(p + 1, "%p:%u", &filemap, &cpsize) == 2 &&
374330e2 456 (cp = MapViewOfFile(filemap, FILE_MAP_READ,
4a693cfc 457 0, 0, cpsize)) != NULL) {
458 conf_deserialise(conf, cp, cpsize);
374330e2 459 UnmapViewOfFile(cp);
460 CloseHandle(filemap);
461 } else if (!do_config()) {
7440fd44 462 cleanup_exit(0);
374330e2 463 }
df7c7e52 464 allow_launch = TRUE;
c0a81592 465 } else {
32874aea 466 /*
c0a81592 467 * Otherwise, break up the command line and deal with
468 * it sensibly.
32874aea 469 */
c0a81592 470 int argc, i;
471 char **argv;
472
d3a1a808 473 split_into_argv(cmdline, &argc, &argv, NULL);
c0a81592 474
475 for (i = 0; i < argc; i++) {
476 char *p = argv[i];
477 int ret;
478
5555d393 479 ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
4a693cfc 480 1, conf);
c0a81592 481 if (ret == -2) {
482 cmdline_error("option \"%s\" requires an argument", p);
483 } else if (ret == 2) {
484 i++; /* skip next argument */
485 } else if (ret == 1) {
486 continue; /* nothing further needs doing */
a6551274 487 } else if (!strcmp(p, "-cleanup") ||
488 !strcmp(p, "-cleanup-during-uninstall")) {
c0a81592 489 /*
490 * `putty -cleanup'. Remove all registry
491 * entries associated with PuTTY, and also find
492 * and delete the random seed file.
493 */
f6f450e2 494 char *s1, *s2;
a6551274 495 /* Are we being invoked from an uninstaller? */
496 if (!strcmp(p, "-cleanup-during-uninstall")) {
497 s1 = dupprintf("Remove saved sessions and random seed file?\n"
498 "\n"
499 "If you hit Yes, ALL Registry entries associated\n"
500 "with %s will be removed, as well as the\n"
501 "random seed file. THIS PROCESS WILL\n"
502 "DESTROY YOUR SAVED SESSIONS.\n"
503 "(This only affects the currently logged-in user.)\n"
504 "\n"
505 "If you hit No, uninstallation will proceed, but\n"
506 "saved sessions etc will be left on the machine.",
507 appname);
508 s2 = dupprintf("%s Uninstallation", appname);
509 } else {
510 s1 = dupprintf("This procedure will remove ALL Registry entries\n"
511 "associated with %s, and will also remove\n"
512 "the random seed file. (This only affects the\n"
513 "currently logged-in user.)\n"
514 "\n"
515 "THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
516 "Are you really sure you want to continue?",
517 appname);
518 s2 = dupprintf("%s Warning", appname);
519 }
520 if (message_box(s1, s2,
521 MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2,
28339579 522 HELPCTXID(option_cleanup)) == IDYES) {
c0a81592 523 cleanup_all();
524 }
f6f450e2 525 sfree(s1);
526 sfree(s2);
c0a81592 527 exit(0);
2285d016 528 } else if (!strcmp(p, "-pgpfp")) {
529 pgp_fingerprints();
530 exit(1);
c0a81592 531 } else if (*p != '-') {
532 char *q = p;
533 if (got_host) {
534 /*
535 * If we already have a host name, treat
536 * this argument as a port number. NB we
537 * have to treat this as a saved -P
538 * argument, so that it will be deferred
539 * until it's a good moment to run it.
540 */
4a693cfc 541 int ret = cmdline_process_param("-P", p, 1, conf);
c0a81592 542 assert(ret == 2);
543 } else if (!strncmp(q, "telnet:", 7)) {
544 /*
545 * If the hostname starts with "telnet:",
546 * set the protocol to Telnet and process
547 * the string as a Telnet URL.
548 */
549 char c;
550
551 q += 7;
552 if (q[0] == '/' && q[1] == '/')
553 q += 2;
4a693cfc 554 conf_set_int(conf, CONF_protocol, PROT_TELNET);
c0a81592 555 p = q;
556 while (*p && *p != ':' && *p != '/')
557 p++;
558 c = *p;
559 if (*p)
560 *p++ = '\0';
561 if (c == ':')
4a693cfc 562 conf_set_int(conf, CONF_port, atoi(p));
c0a81592 563 else
4a693cfc 564 conf_set_int(conf, CONF_port, -1);
565 conf_set_str(conf, CONF_host, q);
c0a81592 566 got_host = 1;
567 } else {
568 /*
569 * Otherwise, treat this argument as a host
570 * name.
571 */
572 while (*p && !isspace(*p))
573 p++;
574 if (*p)
575 *p++ = '\0';
4a693cfc 576 conf_set_str(conf, CONF_host, q);
c0a81592 577 got_host = 1;
578 }
86256dc6 579 } else {
580 cmdline_error("unknown option \"%s\"", p);
c0a81592 581 }
374330e2 582 }
583 }
13eafebf 584
4a693cfc 585 cmdline_run_saved(conf);
c0a81592 586
df7c7e52 587 if (loaded_session || got_host)
588 allow_launch = TRUE;
589
4a693cfc 590 if ((!allow_launch || !conf_launchable(conf)) && !do_config()) {
7440fd44 591 cleanup_exit(0);
c0a81592 592 }
593
381b1876 594 /*
4a693cfc 595 * Muck about with the hostname in various ways.
381b1876 596 */
597 {
4a693cfc 598 char *hostbuf = dupstr(conf_get_str(conf, CONF_host));
599 char *host = hostbuf;
600 char *p, *q;
381b1876 601
4a693cfc 602 /*
603 * Trim leading whitespace.
604 */
605 host += strspn(host, " \t");
606
607 /*
608 * See if host is of the form user@host, and separate
609 * out the username if so.
610 */
611 if (host[0] != '\0') {
612 char *atsign = strrchr(host, '@');
613 if (atsign) {
614 *atsign = '\0';
615 conf_set_str(conf, CONF_username, host);
616 host = atsign + 1;
13eafebf 617 }
13eafebf 618 }
503ddb97 619
5fd70d07 620 /*
621 * Trim a colon suffix off the hostname if it's there. In
622 * order to protect IPv6 address literals against this
623 * treatment, we do not do this if there's _more_ than one
624 * colon.
625 */
626 {
627 char *c = strchr(host, ':');
628
629 if (c) {
630 char *d = strchr(c+1, ':');
631 if (!d)
632 *c = '\0';
633 }
634 }
05581745 635
4a693cfc 636 /*
637 * Remove any remaining whitespace.
638 */
639 p = hostbuf;
640 q = host;
641 while (*q) {
642 if (*q != ' ' && *q != '\t')
643 *p++ = *q;
644 q++;
05581745 645 }
4a693cfc 646 *p = '\0';
cae0c023 647
4a693cfc 648 conf_set_str(conf, CONF_host, hostbuf);
649 sfree(hostbuf);
cae0c023 650 }
374330e2 651 }
652
374330e2 653 if (!prev) {
32874aea 654 wndclass.style = 0;
655 wndclass.lpfnWndProc = WndProc;
656 wndclass.cbClsExtra = 0;
657 wndclass.cbWndExtra = 0;
658 wndclass.hInstance = inst;
659 wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
660 wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
5a73255e 661 wndclass.hbrBackground = NULL;
32874aea 662 wndclass.lpszMenuName = NULL;
374330e2 663 wndclass.lpszClassName = appname;
664
32874aea 665 RegisterClass(&wndclass);
374330e2 666 }
667
21d2b241 668 memset(&ucsdata, 0, sizeof(ucsdata));
669
4a693cfc 670 conf_cache_data();
671
672 conftopalette();
374330e2 673
674 /*
675 * Guess some defaults for the window size. This all gets
676 * updated later, so we don't really care too much. However, we
677 * do want the font width/height guesses to correspond to a
678 * large font rather than a small one...
679 */
32874aea 680
374330e2 681 font_width = 10;
682 font_height = 20;
683 extra_width = 25;
684 extra_height = 28;
4a693cfc 685 guess_width = extra_width + font_width * conf_get_int(conf, CONF_width);
686 guess_height = extra_height + font_height*conf_get_int(conf, CONF_height);
374330e2 687 {
688 RECT r;
f43e4b67 689 get_fullscreen_rect(&r);
374330e2 690 if (guess_width > r.right - r.left)
691 guess_width = r.right - r.left;
692 if (guess_height > r.bottom - r.top)
693 guess_height = r.bottom - r.top;
694 }
695
c9def1b8 696 {
32874aea 697 int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
698 int exwinmode = 0;
4a693cfc 699 if (!conf_get_int(conf, CONF_scrollbar))
32874aea 700 winmode &= ~(WS_VSCROLL);
4a693cfc 701 if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED)
32874aea 702 winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
4a693cfc 703 if (conf_get_int(conf, CONF_alwaysontop))
32874aea 704 exwinmode |= WS_EX_TOPMOST;
4a693cfc 705 if (conf_get_int(conf, CONF_sunken_edge))
32874aea 706 exwinmode |= WS_EX_CLIENTEDGE;
707 hwnd = CreateWindowEx(exwinmode, appname, appname,
708 winmode, CW_USEDEFAULT, CW_USEDEFAULT,
709 guess_width, guess_height,
710 NULL, NULL, inst, NULL);
711 }
374330e2 712
713 /*
74086be0 714 * Initialise the fonts, simultaneously correcting the guesses
715 * for font_{width,height}.
716 */
717 init_fonts(0,0);
718
719 /*
c52dd4c9 720 * Initialise the terminal. (We have to do this _after_
721 * creating the window, since the terminal is the first thing
722 * which will call schedule_timer(), which will in turn call
5ba93094 723 * timer_change_notify() which will expect hwnd to exist.)
c52dd4c9 724 */
4a693cfc 725 term = term_init(conf, &ucsdata, NULL);
726 logctx = log_init(NULL, conf);
c52dd4c9 727 term_provide_logctx(term, logctx);
4a693cfc 728 term_size(term, conf_get_int(conf, CONF_height),
729 conf_get_int(conf, CONF_width),
730 conf_get_int(conf, CONF_savelines));
c52dd4c9 731
732 /*
374330e2 733 * Correct the guesses for extra_{width,height}.
734 */
735 {
736 RECT cr, wr;
32874aea 737 GetWindowRect(hwnd, &wr);
738 GetClientRect(hwnd, &cr);
4a693cfc 739 offset_width = offset_height = conf_get_int(conf, CONF_window_border);
5a73255e 740 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
741 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
374330e2 742 }
743
744 /*
745 * Resize the window, now we know what size we _really_ want it
746 * to be.
747 */
887035a5 748 guess_width = extra_width + font_width * term->cols;
749 guess_height = extra_height + font_height * term->rows;
32874aea 750 SetWindowPos(hwnd, NULL, 0, 0, guess_width, guess_height,
751 SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
374330e2 752
753 /*
934c0b7a 754 * Set up a caret bitmap, with no content.
755 */
756 {
32874aea 757 char *bits;
758 int size = (font_width + 15) / 16 * 2 * font_height;
3d88e64d 759 bits = snewn(size, char);
32874aea 760 memset(bits, 0, size);
761 caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
762 sfree(bits);
934c0b7a 763 }
5b7ce734 764 CreateCaret(hwnd, caretbm, font_width, font_height);
934c0b7a 765
766 /*
374330e2 767 * Initialise the scroll bar.
768 */
769 {
770 SCROLLINFO si;
771
772 si.cbSize = sizeof(si);
c9def1b8 773 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
374330e2 774 si.nMin = 0;
887035a5 775 si.nMax = term->rows - 1;
776 si.nPage = term->rows;
374330e2 777 si.nPos = 0;
32874aea 778 SetScrollInfo(hwnd, SB_VERT, &si, FALSE);
374330e2 779 }
780
374330e2 781 /*
374330e2 782 * Prepare the mouse handler.
783 */
784 lastact = MA_NOTHING;
01c034ad 785 lastbtn = MBT_NOTHING;
374330e2 786 dbltime = GetDoubleClickTime();
787
788 /*
789 * Set up the session-control options on the system menu.
790 */
791 {
12745e35 792 HMENU m;
793 int j;
f6f450e2 794 char *str;
374330e2 795
63be7767 796 popup_menus[SYSMENU].menu = GetSystemMenu(hwnd, FALSE);
797 popup_menus[CTXMENU].menu = CreatePopupMenu();
798 AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_PASTE, "&Paste");
799
12745e35 800 savedsess_menu = CreateMenu();
0b4f0bc0 801 get_sesslist(&sesslist, TRUE);
12745e35 802 update_savedsess_menu();
63be7767 803
804 for (j = 0; j < lenof(popup_menus); j++) {
805 m = popup_menus[j].menu;
806
807 AppendMenu(m, MF_SEPARATOR, 0, 0);
63be7767 808 AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
809 AppendMenu(m, MF_SEPARATOR, 0, 0);
810 AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
811 AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
12745e35 812 AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT) savedsess_menu,
813 "Sa&ved Sessions");
63be7767 814 AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
815 AppendMenu(m, MF_SEPARATOR, 0, 0);
816 AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
817 AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
818 AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
819 AppendMenu(m, MF_SEPARATOR, 0, 0);
4a693cfc 820 AppendMenu(m, (conf_get_int(conf, CONF_resize_action)
821 == RESIZE_DISABLED) ? MF_GRAYED : MF_ENABLED,
822 IDM_FULLSCREEN, "&Full Screen");
63be7767 823 AppendMenu(m, MF_SEPARATOR, 0, 0);
cb2708d3 824 if (has_help())
63be7767 825 AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
826 str = dupprintf("&About %s", appname);
827 AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
828 sfree(str);
829 }
374330e2 830 }
831
533b1743 832 start_backend();
125105d1 833
374330e2 834 /*
a5ce2e9f 835 * Set up the initial input locale.
836 */
837 set_input_locale(GetKeyboardLayout(0));
838
839 /*
00db133f 840 * Finally show the window!
e1c8e0ed 841 */
00db133f 842 ShowWindow(hwnd, show);
843 SetForegroundWindow(hwnd);
e1c8e0ed 844
845 /*
374330e2 846 * Set the palette up.
847 */
848 pal = NULL;
849 logpal = NULL;
850 init_palette();
851
1cff1320 852 term_set_focus(term, GetForegroundWindow() == hwnd);
32874aea 853 UpdateWindow(hwnd);
374330e2 854
34292b1d 855 while (1) {
856 HANDLE *handles;
857 int nhandles, n;
858
859 handles = handle_get_events(&nhandles);
860
861 n = MsgWaitForMultipleObjects(nhandles, handles, FALSE, INFINITE,
862 QS_ALLINPUT);
863
864 if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
865 handle_got_event(handles[n - WAIT_OBJECT_0]);
866 sfree(handles);
95587c73 867 if (must_close_session)
868 close_session();
45d56852 869 } else
870 sfree(handles);
34292b1d 871
b08600c4 872 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
34292b1d 873 if (msg.message == WM_QUIT)
874 goto finished; /* two-level break */
875
32874aea 876 if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
877 DispatchMessage(&msg);
c9def1b8 878 /* Send the paste buffer if there's anything to send */
887035a5 879 term_paste(term);
59ad2c03 880 /* If there's nothing new in the queue then we can do everything
881 * we've delayed, reading the socket, writing, and repainting
882 * the window.
883 */
95587c73 884 if (must_close_session)
885 close_session();
b08600c4 886 }
0ed2f48e 887
34292b1d 888 /* The messages seem unreliable; especially if we're being tricky */
889 term_set_focus(term, GetForegroundWindow() == hwnd);
32874aea 890
56f8ea28 891 if (pending_netevent)
892 enact_pending_netevent();
893
34292b1d 894 net_pending_errors();
374330e2 895 }
896
34292b1d 897 finished:
c0d36a72 898 cleanup_exit(msg.wParam); /* this doesn't return... */
899 return msg.wParam; /* ... but optimiser doesn't know */
93b581bd 900}
901
902/*
903 * Clean up and exit.
904 */
905void cleanup_exit(int code)
906{
374330e2 907 /*
908 * Clean up.
909 */
4eeb7d09 910 deinit_fonts();
374330e2 911 sfree(logpal);
912 if (pal)
913 DeleteObject(pal);
93b581bd 914 sk_cleanup();
374330e2 915
4a693cfc 916 if (conf_get_int(conf, CONF_protocol) == PROT_SSH) {
374330e2 917 random_save_seed();
8f203108 918#ifdef MSCRYPTOAPI
919 crypto_wrapup();
920#endif
921 }
cb2708d3 922 shutdown_help();
374330e2 923
073e9f42 924 /* Clean up COM. */
925 CoUninitialize();
926
93b581bd 927 exit(code);
374330e2 928}
929
930/*
8df7a775 931 * Set up, or shut down, an AsyncSelect. Called from winnet.c.
932 */
32874aea 933char *do_select(SOCKET skt, int startup)
934{
8df7a775 935 int msg, events;
936 if (startup) {
937 msg = WM_NETEVENT;
3ad9d396 938 events = (FD_CONNECT | FD_READ | FD_WRITE |
939 FD_OOB | FD_CLOSE | FD_ACCEPT);
8df7a775 940 } else {
941 msg = events = 0;
942 }
943 if (!hwnd)
944 return "do_select(): internal error (hwnd==NULL)";
7440fd44 945 if (p_WSAAsyncSelect(skt, hwnd, msg, events) == SOCKET_ERROR) {
946 switch (p_WSAGetLastError()) {
32874aea 947 case WSAENETDOWN:
948 return "Network is down";
949 default:
950 return "WSAAsyncSelect(): unknown error";
951 }
8df7a775 952 }
953 return NULL;
954}
955
956/*
12745e35 957 * Refresh the saved-session submenu from `sesslist'.
958 */
959static void update_savedsess_menu(void)
960{
961 int i;
962 while (DeleteMenu(savedsess_menu, 0, MF_BYPOSITION)) ;
963 /* skip sesslist.sessions[0] == Default Settings */
964 for (i = 1;
965 i < ((sesslist.nsessions <= MENU_SAVED_MAX+1) ? sesslist.nsessions
966 : MENU_SAVED_MAX+1);
967 i++)
968 AppendMenu(savedsess_menu, MF_ENABLED,
969 IDM_SAVED_MIN + (i-1)*MENU_SAVED_STEP,
970 sesslist.sessions[i]);
f43e4b67 971 if (sesslist.nsessions <= 1)
972 AppendMenu(savedsess_menu, MF_GRAYED, IDM_SAVED_MIN, "(No sessions)");
12745e35 973}
974
975/*
125105d1 976 * Update the Special Commands submenu.
977 */
978void update_specials_menu(void *frontend)
979{
45a22cae 980 HMENU new_menu;
63be7767 981 int i, j;
125105d1 982
e3be8de5 983 if (back)
984 specials = back->get_specials(backhandle);
985 else
986 specials = NULL;
987
125105d1 988 if (specials) {
6f2d0cde 989 /* We can't use Windows to provide a stack for submenus, so
990 * here's a lame "stack" that will do for now. */
991 HMENU saved_menu = NULL;
992 int nesting = 1;
45a22cae 993 new_menu = CreatePopupMenu();
6f2d0cde 994 for (i = 0; nesting > 0; i++) {
125105d1 995 assert(IDM_SPECIAL_MIN + 0x10 * i < IDM_SPECIAL_MAX);
6f2d0cde 996 switch (specials[i].code) {
997 case TS_SEP:
45a22cae 998 AppendMenu(new_menu, MF_SEPARATOR, 0, 0);
6f2d0cde 999 break;
1000 case TS_SUBMENU:
1001 assert(nesting < 2);
1002 nesting++;
45a22cae 1003 saved_menu = new_menu; /* XXX lame stacking */
1004 new_menu = CreatePopupMenu();
6f2d0cde 1005 AppendMenu(saved_menu, MF_POPUP | MF_ENABLED,
45a22cae 1006 (UINT) new_menu, specials[i].name);
6f2d0cde 1007 break;
1008 case TS_EXITMENU:
1009 nesting--;
1010 if (nesting) {
45a22cae 1011 new_menu = saved_menu; /* XXX lame stacking */
6f2d0cde 1012 saved_menu = NULL;
1013 }
1014 break;
1015 default:
45a22cae 1016 AppendMenu(new_menu, MF_ENABLED, IDM_SPECIAL_MIN + 0x10 * i,
125105d1 1017 specials[i].name);
6f2d0cde 1018 break;
1019 }
125105d1 1020 }
6f2d0cde 1021 /* Squirrel the highest special. */
1022 n_specials = i - 1;
1023 } else {
45a22cae 1024 new_menu = NULL;
6f2d0cde 1025 n_specials = 0;
1026 }
e3be8de5 1027
1028 for (j = 0; j < lenof(popup_menus); j++) {
45a22cae 1029 if (specials_menu) {
6f2d0cde 1030 /* XXX does this free up all submenus? */
62ddb51e 1031 DeleteMenu(popup_menus[j].menu, (UINT)specials_menu, MF_BYCOMMAND);
45a22cae 1032 DeleteMenu(popup_menus[j].menu, IDM_SPECIALSEP, MF_BYCOMMAND);
e3be8de5 1033 }
47d1dab7 1034 if (new_menu) {
45a22cae 1035 InsertMenu(popup_menus[j].menu, IDM_SHOWLOG,
1036 MF_BYCOMMAND | MF_POPUP | MF_ENABLED,
1037 (UINT) new_menu, "S&pecial Command");
1038 InsertMenu(popup_menus[j].menu, IDM_SHOWLOG,
1039 MF_BYCOMMAND | MF_SEPARATOR, IDM_SPECIALSEP, 0);
63be7767 1040 }
125105d1 1041 }
47d1dab7 1042 specials_menu = new_menu;
125105d1 1043}
1044
755e0524 1045static void update_mouse_pointer(void)
1046{
1047 LPTSTR curstype;
1048 int force_visible = FALSE;
1049 static int forced_visible = FALSE;
1050 switch (busy_status) {
1051 case BUSY_NOT:
1052 if (send_raw_mouse)
1053 curstype = IDC_ARROW;
1054 else
1055 curstype = IDC_IBEAM;
1056 break;
1057 case BUSY_WAITING:
1058 curstype = IDC_APPSTARTING; /* this may be an abuse */
1059 force_visible = TRUE;
1060 break;
1061 case BUSY_CPU:
1062 curstype = IDC_WAIT;
1063 force_visible = TRUE;
1064 break;
1065 default:
1066 assert(0);
1067 }
1068 {
1069 HCURSOR cursor = LoadCursor(NULL, curstype);
158cb9b1 1070 SetClassLongPtr(hwnd, GCLP_HCURSOR, (LONG_PTR)cursor);
755e0524 1071 SetCursor(cursor); /* force redraw of cursor at current posn */
1072 }
1073 if (force_visible != forced_visible) {
1074 /* We want some cursor shapes to be visible always.
1075 * Along with show_mouseptr(), this manages the ShowCursor()
1076 * counter such that if we switch back to a non-force_visible
1077 * cursor, the previous visibility state is restored. */
1078 ShowCursor(force_visible);
1079 forced_visible = force_visible;
1080 }
1081}
1082
1083void set_busy_status(void *frontend, int status)
1084{
1085 busy_status = status;
1086 update_mouse_pointer();
1087}
1088
125105d1 1089/*
01c034ad 1090 * set or clear the "raw mouse message" mode
1091 */
a8327734 1092void set_raw_mouse_mode(void *frontend, int activate)
01c034ad 1093{
4a693cfc 1094 activate = activate && !conf_get_int(conf, CONF_no_mouse_rep);
01c034ad 1095 send_raw_mouse = activate;
755e0524 1096 update_mouse_pointer();
01c034ad 1097}
1098
1099/*
8d5de777 1100 * Print a message box and close the connection.
1101 */
a8327734 1102void connection_fatal(void *frontend, char *fmt, ...)
32874aea 1103{
8d5de777 1104 va_list ap;
971bcc0a 1105 char *stuff, morestuff[100];
8d5de777 1106
1107 va_start(ap, fmt);
971bcc0a 1108 stuff = dupvprintf(fmt, ap);
8d5de777 1109 va_end(ap);
f6f450e2 1110 sprintf(morestuff, "%.70s Fatal Error", appname);
1111 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
971bcc0a 1112 sfree(stuff);
1113
4a693cfc 1114 if (conf_get_int(conf, CONF_close_on_exit) == FORCE_ON)
32874aea 1115 PostQuitMessage(1);
8d5de777 1116 else {
95587c73 1117 must_close_session = TRUE;
8d5de777 1118 }
1119}
1120
1121/*
c0a81592 1122 * Report an error at the command-line parsing stage.
1123 */
1124void cmdline_error(char *fmt, ...)
1125{
1126 va_list ap;
971bcc0a 1127 char *stuff, morestuff[100];
c0a81592 1128
1129 va_start(ap, fmt);
971bcc0a 1130 stuff = dupvprintf(fmt, ap);
c0a81592 1131 va_end(ap);
f6f450e2 1132 sprintf(morestuff, "%.70s Command Line Error", appname);
1133 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
971bcc0a 1134 sfree(stuff);
c0a81592 1135 exit(1);
1136}
1137
1138/*
59ad2c03 1139 * Actually do the job requested by a WM_NETEVENT
1140 */
56f8ea28 1141static void enact_pending_netevent(void)
32874aea 1142{
9dde0b46 1143 static int reentering = 0;
8df7a775 1144 extern int select_result(WPARAM, LPARAM);
9dde0b46 1145
1146 if (reentering)
32874aea 1147 return; /* don't unpend the pending */
9dde0b46 1148
56f8ea28 1149 pending_netevent = FALSE;
1150
9dde0b46 1151 reentering = 1;
56f8ea28 1152 select_result(pend_netevent_wParam, pend_netevent_lParam);
9dde0b46 1153 reentering = 0;
59ad2c03 1154}
1155
1156/*
374330e2 1157 * Copy the colour palette from the configuration data into defpal.
1158 * This is non-trivial because the colour indices are different.
1159 */
4a693cfc 1160static void conftopalette(void)
32874aea 1161{
374330e2 1162 int i;
1163 static const int ww[] = {
cecb13f6 1164 256, 257, 258, 259, 260, 261,
1165 0, 8, 1, 9, 2, 10, 3, 11,
1166 4, 12, 5, 13, 6, 14, 7, 15
374330e2 1167 };
1168
cecb13f6 1169 for (i = 0; i < 22; i++) {
374330e2 1170 int w = ww[i];
4a693cfc 1171 defpal[w].rgbtRed = conf_get_int_int(conf, CONF_colours, i*3+0);
1172 defpal[w].rgbtGreen = conf_get_int_int(conf, CONF_colours, i*3+1);
1173 defpal[w].rgbtBlue = conf_get_int_int(conf, CONF_colours, i*3+2);
cecb13f6 1174 }
1175 for (i = 0; i < NEXTCOLOURS; i++) {
1176 if (i < 216) {
1177 int r = i / 36, g = (i / 6) % 6, b = i % 6;
8098d585 1178 defpal[i+16].rgbtRed = r ? r * 40 + 55 : 0;
1179 defpal[i+16].rgbtGreen = g ? g * 40 + 55 : 0;
1180 defpal[i+16].rgbtBlue = b ? b * 40 + 55 : 0;
cecb13f6 1181 } else {
1182 int shade = i - 216;
8098d585 1183 shade = shade * 10 + 8;
cecb13f6 1184 defpal[i+16].rgbtRed = defpal[i+16].rgbtGreen =
1185 defpal[i+16].rgbtBlue = shade;
1186 }
374330e2 1187 }
26d1da7b 1188
1189 /* Override with system colours if appropriate */
4a693cfc 1190 if (conf_get_int(conf, CONF_system_colour))
26d1da7b 1191 systopalette();
1192}
1193
1194/*
1195 * Override bit of defpal with colours from the system.
1196 * (NB that this takes a copy the system colours at the time this is called,
1197 * so subsequent colour scheme changes don't take effect. To fix that we'd
1198 * probably want to be using GetSysColorBrush() and the like.)
1199 */
1200static void systopalette(void)
1201{
1202 int i;
1203 static const struct { int nIndex; int norm; int bold; } or[] =
1204 {
cecb13f6 1205 { COLOR_WINDOWTEXT, 256, 257 }, /* Default Foreground */
1206 { COLOR_WINDOW, 258, 259 }, /* Default Background */
1207 { COLOR_HIGHLIGHTTEXT, 260, 260 }, /* Cursor Text */
1208 { COLOR_HIGHLIGHT, 261, 261 }, /* Cursor Colour */
26d1da7b 1209 };
1210
1211 for (i = 0; i < (sizeof(or)/sizeof(or[0])); i++) {
1212 COLORREF colour = GetSysColor(or[i].nIndex);
1213 defpal[or[i].norm].rgbtRed =
1214 defpal[or[i].bold].rgbtRed = GetRValue(colour);
1215 defpal[or[i].norm].rgbtGreen =
1216 defpal[or[i].bold].rgbtGreen = GetGValue(colour);
1217 defpal[or[i].norm].rgbtBlue =
1218 defpal[or[i].bold].rgbtBlue = GetBValue(colour);
1219 }
374330e2 1220}
1221
1222/*
1223 * Set up the colour palette.
1224 */
32874aea 1225static void init_palette(void)
1226{
374330e2 1227 int i;
32874aea 1228 HDC hdc = GetDC(hwnd);
374330e2 1229 if (hdc) {
4a693cfc 1230 if (conf_get_int(conf, CONF_try_palette) &&
1231 GetDeviceCaps(hdc, RASTERCAPS) & RC_PALETTE) {
3d88e64d 1232 /*
1233 * This is a genuine case where we must use smalloc
1234 * because the snew macros can't cope.
1235 */
374330e2 1236 logpal = smalloc(sizeof(*logpal)
1237 - sizeof(logpal->palPalEntry)
cecb13f6 1238 + NALLCOLOURS * sizeof(PALETTEENTRY));
374330e2 1239 logpal->palVersion = 0x300;
cecb13f6 1240 logpal->palNumEntries = NALLCOLOURS;
1241 for (i = 0; i < NALLCOLOURS; i++) {
374330e2 1242 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
1243 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
1244 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
1245 logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
1246 }
32874aea 1247 pal = CreatePalette(logpal);
374330e2 1248 if (pal) {
32874aea 1249 SelectPalette(hdc, pal, FALSE);
1250 RealizePalette(hdc);
1251 SelectPalette(hdc, GetStockObject(DEFAULT_PALETTE), FALSE);
374330e2 1252 }
1253 }
32874aea 1254 ReleaseDC(hwnd, hdc);
374330e2 1255 }
1256 if (pal)
cecb13f6 1257 for (i = 0; i < NALLCOLOURS; i++)
374330e2 1258 colours[i] = PALETTERGB(defpal[i].rgbtRed,
1259 defpal[i].rgbtGreen,
1260 defpal[i].rgbtBlue);
1261 else
cecb13f6 1262 for (i = 0; i < NALLCOLOURS; i++)
374330e2 1263 colours[i] = RGB(defpal[i].rgbtRed,
32874aea 1264 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
374330e2 1265}
1266
1267/*
f0fccd51 1268 * This is a wrapper to ExtTextOut() to force Windows to display
1269 * the precise glyphs we give it. Otherwise it would do its own
1270 * bidi and Arabic shaping, and we would end up uncertain which
1271 * characters it had put where.
1272 */
1273static void exact_textout(HDC hdc, int x, int y, CONST RECT *lprc,
1274 unsigned short *lpString, UINT cbCount,
c6958dfe 1275 CONST INT *lpDx, int opaque)
f0fccd51 1276{
b7625258 1277#ifdef __LCC__
1278 /*
1279 * The LCC include files apparently don't supply the
1280 * GCP_RESULTSW type, but we can make do with GCP_RESULTS
1281 * proper: the differences aren't important to us (the only
1282 * variable-width string parameter is one we don't use anyway).
1283 */
1284 GCP_RESULTS gcpr;
1285#else
f0fccd51 1286 GCP_RESULTSW gcpr;
b7625258 1287#endif
f0fccd51 1288 char *buffer = snewn(cbCount*2+2, char);
1289 char *classbuffer = snewn(cbCount, char);
1290 memset(&gcpr, 0, sizeof(gcpr));
1291 memset(buffer, 0, cbCount*2+2);
1292 memset(classbuffer, GCPCLASS_NEUTRAL, cbCount);
1293
1294 gcpr.lStructSize = sizeof(gcpr);
1295 gcpr.lpGlyphs = (void *)buffer;
44e975e0 1296 gcpr.lpClass = (void *)classbuffer;
f0fccd51 1297 gcpr.nGlyphs = cbCount;
f0fccd51 1298 GetCharacterPlacementW(hdc, lpString, cbCount, 0, &gcpr,
c6958dfe 1299 FLI_MASK | GCP_CLASSIN | GCP_DIACRITIC);
f0fccd51 1300
c6958dfe 1301 ExtTextOut(hdc, x, y,
1302 ETO_GLYPH_INDEX | ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
1303 lprc, buffer, cbCount, lpDx);
f0fccd51 1304}
1305
1306/*
fe75e503 1307 * The exact_textout() wrapper, unfortunately, destroys the useful
1308 * Windows `font linking' behaviour: automatic handling of Unicode
1309 * code points not supported in this font by falling back to a font
1310 * which does contain them. Therefore, we adopt a multi-layered
1311 * approach: for any potentially-bidi text, we use exact_textout(),
1312 * and for everything else we use a simple ExtTextOut as we did
1313 * before exact_textout() was introduced.
1314 */
1315static void general_textout(HDC hdc, int x, int y, CONST RECT *lprc,
1316 unsigned short *lpString, UINT cbCount,
1317 CONST INT *lpDx, int opaque)
1318{
1319 int i, j, xp, xn;
19e569a1 1320 int bkmode = 0, got_bkmode = FALSE;
fe75e503 1321
1322 xp = xn = x;
1323
62ddb51e 1324 for (i = 0; i < (int)cbCount ;) {
fe75e503 1325 int rtl = is_rtl(lpString[i]);
1326
1327 xn += lpDx[i];
1328
62ddb51e 1329 for (j = i+1; j < (int)cbCount; j++) {
fe75e503 1330 if (rtl != is_rtl(lpString[j]))
1331 break;
1332 xn += lpDx[j];
1333 }
1334
1335 /*
1336 * Now [i,j) indicates a maximal substring of lpString
1337 * which should be displayed using the same textout
1338 * function.
1339 */
1340 if (rtl) {
19e569a1 1341 exact_textout(hdc, xp, y, lprc, lpString+i, j-i,
53126a62 1342 font_varpitch ? NULL : lpDx+i, opaque);
fe75e503 1343 } else {
fe75e503 1344 ExtTextOutW(hdc, xp, y, ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
19e569a1 1345 lprc, lpString+i, j-i,
53126a62 1346 font_varpitch ? NULL : lpDx+i);
fe75e503 1347 }
1348
1349 i = j;
1350 xp = xn;
19e569a1 1351
1352 bkmode = GetBkMode(hdc);
1353 got_bkmode = TRUE;
1354 SetBkMode(hdc, TRANSPARENT);
1355 opaque = FALSE;
fe75e503 1356 }
1357
19e569a1 1358 if (got_bkmode)
1359 SetBkMode(hdc, bkmode);
fe75e503 1360}
1361
14ce9887 1362static int get_font_width(HDC hdc, const TEXTMETRIC *tm)
1363{
1364 int ret;
1365 /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
1366 if (!(tm->tmPitchAndFamily & TMPF_FIXED_PITCH)) {
1367 ret = tm->tmAveCharWidth;
1368 } else {
1369#define FIRST '0'
1370#define LAST '9'
1371 ABCFLOAT widths[LAST-FIRST + 1];
1372 int j;
1373
1374 font_varpitch = TRUE;
1375 font_dualwidth = TRUE;
1376 if (GetCharABCWidthsFloat(hdc, FIRST, LAST, widths)) {
1377 ret = 0;
1378 for (j = 0; j < lenof(widths); j++) {
1379 int width = (int)(0.5 + widths[j].abcfA +
1380 widths[j].abcfB + widths[j].abcfC);
1381 if (ret < width)
1382 ret = width;
1383 }
1384 } else {
1385 ret = tm->tmMaxCharWidth;
1386 }
1387#undef FIRST
1388#undef LAST
1389 }
1390 return ret;
1391}
1392
fe75e503 1393/*
4eeb7d09 1394 * Initialise all the fonts we will need initially. There may be as many as
efd51845 1395 * three or as few as one. The other (potentially) twenty-one fonts are done
4eeb7d09 1396 * if/when they are needed.
1397 *
1398 * We also:
374330e2 1399 *
1400 * - check the font width and height, correcting our guesses if
1401 * necessary.
1402 *
1403 * - verify that the bold font is the same width as the ordinary
1404 * one, and engage shadow bolding if not.
1405 *
1406 * - verify that the underlined font is the same width as the
1407 * ordinary one (manual underlining by means of line drawing can
1408 * be done in a pinch).
374330e2 1409 */
5a73255e 1410static void init_fonts(int pick_width, int pick_height)
32874aea 1411{
374330e2 1412 TEXTMETRIC tm;
4eeb7d09 1413 CPINFO cpinfo;
4a693cfc 1414 FontSpec *font;
4eeb7d09 1415 int fontsize[3];
97fc891e 1416 int i;
4a693cfc 1417 int quality;
374330e2 1418 HDC hdc;
1419 int fw_dontcare, fw_bold;
1420
4eeb7d09 1421 for (i = 0; i < FONT_MAXNO; i++)
374330e2 1422 fonts[i] = NULL;
1423
863c5362 1424 bold_font_mode = conf_get_int(conf, CONF_bold_style) & 1 ?
1425 BOLD_FONT : BOLD_NONE;
1426 bold_colours = conf_get_int(conf, CONF_bold_style) & 2 ? TRUE : FALSE;
5a73255e 1427 und_mode = UND_FONT;
1428
4a693cfc 1429 font = conf_get_fontspec(conf, CONF_font);
1430 if (font->isbold) {
374330e2 1431 fw_dontcare = FW_BOLD;
5b80d07f 1432 fw_bold = FW_HEAVY;
32874aea 1433 } else {
374330e2 1434 fw_dontcare = FW_DONTCARE;
1435 fw_bold = FW_BOLD;
1436 }
1437
97fc891e 1438 hdc = GetDC(hwnd);
1439
5a73255e 1440 if (pick_height)
1441 font_height = pick_height;
1442 else {
4a693cfc 1443 font_height = font->height;
5a73255e 1444 if (font_height > 0) {
1445 font_height =
1446 -MulDiv(font_height, GetDeviceCaps(hdc, LOGPIXELSY), 72);
1447 }
3b2c664e 1448 }
59ad2c03 1449 font_width = pick_width;
97fc891e 1450
4a693cfc 1451 quality = conf_get_int(conf, CONF_font_quality);
374330e2 1452#define f(i,c,w,u) \
97fc891e 1453 fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
374330e2 1454 c, OUT_DEFAULT_PRECIS, \
97318525 1455 CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality), \
4a693cfc 1456 FIXED_PITCH | FF_DONTCARE, font->name)
97fc891e 1457
4a693cfc 1458 f(FONT_NORMAL, font->charset, fw_dontcare, FALSE);
97fc891e 1459
4eeb7d09 1460 SelectObject(hdc, fonts[FONT_NORMAL]);
1461 GetTextMetrics(hdc, &tm);
5a73255e 1462
fd64bb27 1463 GetObject(fonts[FONT_NORMAL], sizeof(LOGFONT), &lfont);
1464
14ce9887 1465 /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
1466 if (!(tm.tmPitchAndFamily & TMPF_FIXED_PITCH)) {
1467 font_varpitch = FALSE;
1468 font_dualwidth = (tm.tmAveCharWidth != tm.tmMaxCharWidth);
1469 } else {
1470 font_varpitch = TRUE;
1471 font_dualwidth = TRUE;
1472 }
5a73255e 1473 if (pick_width == 0 || pick_height == 0) {
1474 font_height = tm.tmHeight;
14ce9887 1475 font_width = get_font_width(hdc, &tm);
5a73255e 1476 }
5a73255e 1477
1478#ifdef RDB_DEBUG_PATCH
1479 debug(23, "Primary font H=%d, AW=%d, MW=%d",
1480 tm.tmHeight, tm.tmAveCharWidth, tm.tmMaxCharWidth);
1481#endif
97fc891e 1482
4eeb7d09 1483 {
1484 CHARSETINFO info;
1485 DWORD cset = tm.tmCharSet;
1486 memset(&info, 0xFF, sizeof(info));
97fc891e 1487
4eeb7d09 1488 /* !!! Yes the next line is right */
1489 if (cset == OEM_CHARSET)
21d2b241 1490 ucsdata.font_codepage = GetOEMCP();
4eeb7d09 1491 else
21d2b241 1492 if (TranslateCharsetInfo ((DWORD *) cset, &info, TCI_SRCCHARSET))
1493 ucsdata.font_codepage = info.ciACP;
4eeb7d09 1494 else
21d2b241 1495 ucsdata.font_codepage = -1;
32874aea 1496
21d2b241 1497 GetCPInfo(ucsdata.font_codepage, &cpinfo);
1498 ucsdata.dbcs_screenfont = (cpinfo.MaxCharSize > 1);
4eeb7d09 1499 }
97fc891e 1500
4a693cfc 1501 f(FONT_UNDERLINE, font->charset, fw_dontcare, TRUE);
97fc891e 1502
4eeb7d09 1503 /*
1504 * Some fonts, e.g. 9-pt Courier, draw their underlines
1505 * outside their character cell. We successfully prevent
1506 * screen corruption by clipping the text output, but then
1507 * we lose the underline completely. Here we try to work
1508 * out whether this is such a font, and if it is, we set a
1509 * flag that causes underlines to be drawn by hand.
1510 *
1511 * Having tried other more sophisticated approaches (such
1512 * as examining the TEXTMETRIC structure or requesting the
1513 * height of a string), I think we'll do this the brute
1514 * force way: we create a small bitmap, draw an underlined
1515 * space on it, and test to see whether any pixels are
1516 * foreground-coloured. (Since we expect the underline to
1517 * go all the way across the character cell, we only search
1518 * down a single column of the bitmap, half way across.)
1519 */
1520 {
1521 HDC und_dc;
1522 HBITMAP und_bm, und_oldbm;
1523 int i, gotit;
1524 COLORREF c;
1525
1526 und_dc = CreateCompatibleDC(hdc);
1527 und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
1528 und_oldbm = SelectObject(und_dc, und_bm);
1529 SelectObject(und_dc, fonts[FONT_UNDERLINE]);
1530 SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
1531 SetTextColor(und_dc, RGB(255, 255, 255));
1532 SetBkColor(und_dc, RGB(0, 0, 0));
1533 SetBkMode(und_dc, OPAQUE);
1534 ExtTextOut(und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
1535 gotit = FALSE;
1536 for (i = 0; i < font_height; i++) {
1537 c = GetPixel(und_dc, font_width / 2, i);
1538 if (c != RGB(0, 0, 0))
1539 gotit = TRUE;
1540 }
1541 SelectObject(und_dc, und_oldbm);
1542 DeleteObject(und_bm);
1543 DeleteDC(und_dc);
1544 if (!gotit) {
1545 und_mode = UND_LINE;
1546 DeleteObject(fonts[FONT_UNDERLINE]);
1547 fonts[FONT_UNDERLINE] = 0;
32874aea 1548 }
4eeb7d09 1549 }
97fc891e 1550
863c5362 1551 if (bold_font_mode == BOLD_FONT) {
4a693cfc 1552 f(FONT_BOLD, font->charset, fw_bold, FALSE);
374330e2 1553 }
1554#undef f
1555
97fc891e 1556 descent = tm.tmAscent + 1;
1557 if (descent >= font_height)
1558 descent = font_height - 1;
374330e2 1559
4eeb7d09 1560 for (i = 0; i < 3; i++) {
97fc891e 1561 if (fonts[i]) {
32874aea 1562 if (SelectObject(hdc, fonts[i]) && GetTextMetrics(hdc, &tm))
14ce9887 1563 fontsize[i] = get_font_width(hdc, &tm) + 256 * tm.tmHeight;
32874aea 1564 else
4eeb7d09 1565 fontsize[i] = -i;
32874aea 1566 } else
4eeb7d09 1567 fontsize[i] = -i;
374330e2 1568 }
1569
32874aea 1570 ReleaseDC(hwnd, hdc);
374330e2 1571
4eeb7d09 1572 if (fontsize[FONT_UNDERLINE] != fontsize[FONT_NORMAL]) {
374330e2 1573 und_mode = UND_LINE;
32874aea 1574 DeleteObject(fonts[FONT_UNDERLINE]);
4eeb7d09 1575 fonts[FONT_UNDERLINE] = 0;
374330e2 1576 }
1577
863c5362 1578 if (bold_font_mode == BOLD_FONT &&
4eeb7d09 1579 fontsize[FONT_BOLD] != fontsize[FONT_NORMAL]) {
863c5362 1580 bold_font_mode = BOLD_SHADOW;
32874aea 1581 DeleteObject(fonts[FONT_BOLD]);
4eeb7d09 1582 fonts[FONT_BOLD] = 0;
374330e2 1583 }
4eeb7d09 1584 fontflag[0] = fontflag[1] = fontflag[2] = 1;
1585
4a693cfc 1586 init_ucs(conf, &ucsdata);
4eeb7d09 1587}
1588
1589static void another_font(int fontno)
1590{
1591 int basefont;
4a693cfc 1592 int fw_dontcare, fw_bold, quality;
4eeb7d09 1593 int c, u, w, x;
1594 char *s;
4a693cfc 1595 FontSpec *font;
4eeb7d09 1596
1597 if (fontno < 0 || fontno >= FONT_MAXNO || fontflag[fontno])
1598 return;
1599
1600 basefont = (fontno & ~(FONT_BOLDUND));
1601 if (basefont != fontno && !fontflag[basefont])
1602 another_font(basefont);
97fc891e 1603
4a693cfc 1604 font = conf_get_fontspec(conf, CONF_font);
1605
1606 if (font->isbold) {
4eeb7d09 1607 fw_dontcare = FW_BOLD;
1608 fw_bold = FW_HEAVY;
1609 } else {
1610 fw_dontcare = FW_DONTCARE;
1611 fw_bold = FW_BOLD;
1612 }
1613
4a693cfc 1614 c = font->charset;
4eeb7d09 1615 w = fw_dontcare;
1616 u = FALSE;
4a693cfc 1617 s = font->name;
4eeb7d09 1618 x = font_width;
1619
1620 if (fontno & FONT_WIDE)
1621 x *= 2;
1622 if (fontno & FONT_NARROW)
5a73255e 1623 x = (x+1)/2;
4eeb7d09 1624 if (fontno & FONT_OEM)
1625 c = OEM_CHARSET;
1626 if (fontno & FONT_BOLD)
1627 w = fw_bold;
1628 if (fontno & FONT_UNDERLINE)
1629 u = TRUE;
1630
4a693cfc 1631 quality = conf_get_int(conf, CONF_font_quality);
1632
4eeb7d09 1633 fonts[fontno] =
1634 CreateFont(font_height * (1 + !!(fontno & FONT_HIGH)), x, 0, 0, w,
1635 FALSE, u, FALSE, c, OUT_DEFAULT_PRECIS,
4a693cfc 1636 CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
14ce9887 1637 DEFAULT_PITCH | FF_DONTCARE, s);
4eeb7d09 1638
1639 fontflag[fontno] = 1;
1640}
1641
1642static void deinit_fonts(void)
1643{
1644 int i;
1645 for (i = 0; i < FONT_MAXNO; i++) {
1646 if (fonts[i])
1647 DeleteObject(fonts[i]);
1648 fonts[i] = 0;
1649 fontflag[i] = 0;
374330e2 1650 }
1651}
1652
a8327734 1653void request_resize(void *frontend, int w, int h)
32874aea 1654{
59ad2c03 1655 int width, height;
c9def1b8 1656
1657 /* If the window is maximized supress resizing attempts */
5a73255e 1658 if (IsZoomed(hwnd)) {
4a693cfc 1659 if (conf_get_int(conf, CONF_resize_action) == RESIZE_TERM)
5a73255e 1660 return;
1661 }
32874aea 1662
4a693cfc 1663 if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED) return;
887035a5 1664 if (h == term->rows && w == term->cols) return;
5a73255e 1665
1666 /* Sanity checks ... */
1667 {
32874aea 1668 static int first_time = 1;
1669 static RECT ss;
1670
1671 switch (first_time) {
1672 case 1:
1673 /* Get the size of the screen */
d0f1bcb4 1674 if (get_fullscreen_rect(&ss))
32874aea 1675 /* first_time = 0 */ ;
1676 else {
1677 first_time = 2;
1678 break;
1679 }
1680 case 0:
1681 /* Make sure the values are sane */
5a73255e 1682 width = (ss.right - ss.left - extra_width) / 4;
1683 height = (ss.bottom - ss.top - extra_height) / 6;
32874aea 1684
5a73255e 1685 if (w > width || h > height)
1686 return;
32874aea 1687 if (w < 15)
1688 w = 15;
1689 if (h < 1)
5a73255e 1690 h = 1;
32874aea 1691 }
c9def1b8 1692 }
59ad2c03 1693
4a693cfc 1694 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
5a73255e 1695
4a693cfc 1696 if (conf_get_int(conf, CONF_resize_action) != RESIZE_FONT &&
1697 !IsZoomed(hwnd)) {
5a73255e 1698 width = extra_width + font_width * w;
1699 height = extra_height + font_height * h;
374330e2 1700
5a73255e 1701 SetWindowPos(hwnd, NULL, 0, 0, width, height,
1702 SWP_NOACTIVATE | SWP_NOCOPYBITS |
1703 SWP_NOMOVE | SWP_NOZORDER);
1704 } else
1705 reset_window(0);
1706
1707 InvalidateRect(hwnd, NULL, TRUE);
1708}
1709
1710static void reset_window(int reinit) {
1711 /*
1712 * This function decides how to resize or redraw when the
1713 * user changes something.
1714 *
1715 * This function doesn't like to change the terminal size but if the
1716 * font size is locked that may be it's only soluion.
1717 */
4a693cfc 1718 int win_width, win_height, resize_action, window_border;
5a73255e 1719 RECT cr, wr;
1720
1721#ifdef RDB_DEBUG_PATCH
1722 debug((27, "reset_window()"));
1723#endif
1724
1725 /* Current window sizes ... */
1726 GetWindowRect(hwnd, &wr);
1727 GetClientRect(hwnd, &cr);
1728
1729 win_width = cr.right - cr.left;
1730 win_height = cr.bottom - cr.top;
1731
4a693cfc 1732 resize_action = conf_get_int(conf, CONF_resize_action);
1733 window_border = conf_get_int(conf, CONF_window_border);
1734
1735 if (resize_action == RESIZE_DISABLED)
1736 reinit = 2;
0ed2f48e 1737
5a73255e 1738 /* Are we being forced to reload the fonts ? */
1739 if (reinit>1) {
1740#ifdef RDB_DEBUG_PATCH
1741 debug((27, "reset_window() -- Forced deinit"));
1742#endif
1743 deinit_fonts();
1744 init_fonts(0,0);
1745 }
1746
1747 /* Oh, looks like we're minimised */
1748 if (win_width == 0 || win_height == 0)
1749 return;
1750
1751 /* Is the window out of position ? */
1752 if ( !reinit &&
887035a5 1753 (offset_width != (win_width-font_width*term->cols)/2 ||
1754 offset_height != (win_height-font_height*term->rows)/2) ){
1755 offset_width = (win_width-font_width*term->cols)/2;
1756 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1757 InvalidateRect(hwnd, NULL, TRUE);
1758#ifdef RDB_DEBUG_PATCH
1759 debug((27, "reset_window() -> Reposition terminal"));
1760#endif
1761 }
1762
6003660f 1763 if (IsZoomed(hwnd)) {
5a73255e 1764 /* We're fullscreen, this means we must not change the size of
1765 * the window so it's the font size or the terminal itself.
1766 */
1767
1768 extra_width = wr.right - wr.left - cr.right + cr.left;
1769 extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
1770
4a693cfc 1771 if (resize_action != RESIZE_TERM) {
1772 if (font_width != win_width/term->cols ||
1773 font_height != win_height/term->rows) {
5a73255e 1774 deinit_fonts();
887035a5 1775 init_fonts(win_width/term->cols, win_height/term->rows);
1776 offset_width = (win_width-font_width*term->cols)/2;
1777 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1778 InvalidateRect(hwnd, NULL, TRUE);
1779#ifdef RDB_DEBUG_PATCH
1780 debug((25, "reset_window() -> Z font resize to (%d, %d)",
1781 font_width, font_height));
1782#endif
1783 }
1784 } else {
4a693cfc 1785 if (font_width * term->cols != win_width ||
1786 font_height * term->rows != win_height) {
5a73255e 1787 /* Our only choice at this point is to change the
1788 * size of the terminal; Oh well.
1789 */
887035a5 1790 term_size(term, win_height/font_height, win_width/font_width,
4a693cfc 1791 conf_get_int(conf, CONF_savelines));
887035a5 1792 offset_width = (win_width-font_width*term->cols)/2;
1793 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1794 InvalidateRect(hwnd, NULL, TRUE);
1795#ifdef RDB_DEBUG_PATCH
1796 debug((27, "reset_window() -> Zoomed term_size"));
1797#endif
1798 }
1799 }
1800 return;
1801 }
1802
1803 /* Hmm, a force re-init means we should ignore the current window
1804 * so we resize to the default font size.
1805 */
1806 if (reinit>0) {
1807#ifdef RDB_DEBUG_PATCH
1808 debug((27, "reset_window() -> Forced re-init"));
1809#endif
1810
4a693cfc 1811 offset_width = offset_height = window_border;
5a73255e 1812 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1813 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1814
887035a5 1815 if (win_width != font_width*term->cols + offset_width*2 ||
1816 win_height != font_height*term->rows + offset_height*2) {
5a73255e 1817
1818 /* If this is too large windows will resize it to the maximum
1819 * allowed window size, we will then be back in here and resize
1820 * the font or terminal to fit.
1821 */
1822 SetWindowPos(hwnd, NULL, 0, 0,
887035a5 1823 font_width*term->cols + extra_width,
1824 font_height*term->rows + extra_height,
5a73255e 1825 SWP_NOMOVE | SWP_NOZORDER);
1826 }
0ed2f48e 1827
1828 InvalidateRect(hwnd, NULL, TRUE);
5a73255e 1829 return;
1830 }
1831
1832 /* Okay the user doesn't want us to change the font so we try the
1833 * window. But that may be too big for the screen which forces us
1834 * to change the terminal.
1835 */
4a693cfc 1836 if ((resize_action == RESIZE_TERM && reinit<=0) ||
1837 (resize_action == RESIZE_EITHER && reinit<0) ||
0ed2f48e 1838 reinit>0) {
4a693cfc 1839 offset_width = offset_height = window_border;
5a73255e 1840 extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
1841 extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
1842
887035a5 1843 if (win_width != font_width*term->cols + offset_width*2 ||
1844 win_height != font_height*term->rows + offset_height*2) {
5a73255e 1845
1846 static RECT ss;
1847 int width, height;
d0f1bcb4 1848
1849 get_fullscreen_rect(&ss);
5a73255e 1850
5a73255e 1851 width = (ss.right - ss.left - extra_width) / font_width;
1852 height = (ss.bottom - ss.top - extra_height) / font_height;
1853
1854 /* Grrr too big */
887035a5 1855 if ( term->rows > height || term->cols > width ) {
4a693cfc 1856 if (resize_action == RESIZE_EITHER) {
0ed2f48e 1857 /* Make the font the biggest we can */
887035a5 1858 if (term->cols > width)
1859 font_width = (ss.right - ss.left - extra_width)
1860 / term->cols;
1861 if (term->rows > height)
1862 font_height = (ss.bottom - ss.top - extra_height)
1863 / term->rows;
0ed2f48e 1864
1865 deinit_fonts();
1866 init_fonts(font_width, font_height);
1867
1868 width = (ss.right - ss.left - extra_width) / font_width;
1869 height = (ss.bottom - ss.top - extra_height) / font_height;
1870 } else {
887035a5 1871 if ( height > term->rows ) height = term->rows;
1872 if ( width > term->cols ) width = term->cols;
4a693cfc 1873 term_size(term, height, width,
1874 conf_get_int(conf, CONF_savelines));
5a73255e 1875#ifdef RDB_DEBUG_PATCH
0ed2f48e 1876 debug((27, "reset_window() -> term resize to (%d,%d)",
1877 height, width));
5a73255e 1878#endif
0ed2f48e 1879 }
5a73255e 1880 }
1881
1882 SetWindowPos(hwnd, NULL, 0, 0,
887035a5 1883 font_width*term->cols + extra_width,
1884 font_height*term->rows + extra_height,
5a73255e 1885 SWP_NOMOVE | SWP_NOZORDER);
1886
1887 InvalidateRect(hwnd, NULL, TRUE);
1888#ifdef RDB_DEBUG_PATCH
1889 debug((27, "reset_window() -> window resize to (%d,%d)",
887035a5 1890 font_width*term->cols + extra_width,
1891 font_height*term->rows + extra_height));
5a73255e 1892#endif
1893 }
1894 return;
1895 }
1896
1897 /* We're allowed to or must change the font but do we want to ? */
1898
4a693cfc 1899 if (font_width != (win_width-window_border*2)/term->cols ||
1900 font_height != (win_height-window_border*2)/term->rows) {
5a73255e 1901
1902 deinit_fonts();
4a693cfc 1903 init_fonts((win_width-window_border*2)/term->cols,
1904 (win_height-window_border*2)/term->rows);
887035a5 1905 offset_width = (win_width-font_width*term->cols)/2;
1906 offset_height = (win_height-font_height*term->rows)/2;
5a73255e 1907
1908 extra_width = wr.right - wr.left - cr.right + cr.left +offset_width*2;
1909 extra_height = wr.bottom - wr.top - cr.bottom + cr.top+offset_height*2;
1910
1911 InvalidateRect(hwnd, NULL, TRUE);
1912#ifdef RDB_DEBUG_PATCH
1913 debug((25, "reset_window() -> font resize to (%d,%d)",
1914 font_width, font_height));
1915#endif
1916 }
374330e2 1917}
1918
a5ce2e9f 1919static void set_input_locale(HKL kl)
1920{
1921 char lbuf[20];
1922
1923 GetLocaleInfo(LOWORD(kl), LOCALE_IDEFAULTANSICODEPAGE,
1924 lbuf, sizeof(lbuf));
1925
1926 kbd_codepage = atoi(lbuf);
1927}
1928
6908fed7 1929static void click(Mouse_Button b, int x, int y, int shift, int ctrl, int alt)
32874aea 1930{
fdedf2c8 1931 int thistime = GetMessageTime();
1932
4a693cfc 1933 if (send_raw_mouse &&
1934 !(shift && conf_get_int(conf, CONF_mouse_override))) {
b90840c3 1935 lastbtn = MBT_NOTHING;
fc5b0934 1936 term_mouse(term, b, translate_button(b), MA_CLICK,
1937 x, y, shift, ctrl, alt);
01c034ad 1938 return;
1939 }
1940
fdedf2c8 1941 if (lastbtn == b && thistime - lasttime < dbltime) {
374330e2 1942 lastact = (lastact == MA_CLICK ? MA_2CLK :
1943 lastact == MA_2CLK ? MA_3CLK :
1944 lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
1945 } else {
1946 lastbtn = b;
1947 lastact = MA_CLICK;
1948 }
1949 if (lastact != MA_NOTHING)
fc5b0934 1950 term_mouse(term, b, translate_button(b), lastact,
1951 x, y, shift, ctrl, alt);
fdedf2c8 1952 lasttime = thistime;
374330e2 1953}
1954
01c034ad 1955/*
1956 * Translate a raw mouse button designation (LEFT, MIDDLE, RIGHT)
1957 * into a cooked one (SELECT, EXTEND, PASTE).
1958 */
2c02ed02 1959static Mouse_Button translate_button(Mouse_Button button)
32874aea 1960{
01c034ad 1961 if (button == MBT_LEFT)
1962 return MBT_SELECT;
1963 if (button == MBT_MIDDLE)
4a693cfc 1964 return conf_get_int(conf, CONF_mouse_is_xterm) == 1 ?
1965 MBT_PASTE : MBT_EXTEND;
01c034ad 1966 if (button == MBT_RIGHT)
4a693cfc 1967 return conf_get_int(conf, CONF_mouse_is_xterm) == 1 ?
1968 MBT_EXTEND : MBT_PASTE;
2d466ffd 1969 return 0; /* shouldn't happen */
01c034ad 1970}
1971
32874aea 1972static void show_mouseptr(int show)
1973{
755e0524 1974 /* NB that the counter in ShowCursor() is also frobbed by
1975 * update_mouse_pointer() */
554c540d 1976 static int cursor_visible = 1;
4a693cfc 1977 if (!conf_get_int(conf, CONF_hide_mouseptr))
1978 show = 1; /* override if this feature disabled */
554c540d 1979 if (cursor_visible && !show)
32874aea 1980 ShowCursor(FALSE);
554c540d 1981 else if (!cursor_visible && show)
32874aea 1982 ShowCursor(TRUE);
554c540d 1983 cursor_visible = show;
1984}
1985
6908fed7 1986static int is_alt_pressed(void)
1987{
1988 BYTE keystate[256];
1989 int r = GetKeyboardState(keystate);
1990 if (!r)
1991 return FALSE;
1992 if (keystate[VK_MENU] & 0x80)
1993 return TRUE;
1994 if (keystate[VK_RMENU] & 0x80)
1995 return TRUE;
1996 return FALSE;
1997}
1998
68f9b3d9 1999static int resizing;
2000
95587c73 2001void notify_remote_exit(void *fe)
2002{
4a693cfc 2003 int exitcode, close_on_exit;
95587c73 2004
2005 if (!session_closed &&
2006 (exitcode = back->exitcode(backhandle)) >= 0) {
4a693cfc 2007 close_on_exit = conf_get_int(conf, CONF_close_on_exit);
95587c73 2008 /* Abnormal exits will already have set session_closed and taken
2009 * appropriate action. */
4a693cfc 2010 if (close_on_exit == FORCE_ON ||
2011 (close_on_exit == AUTO && exitcode != INT_MAX)) {
95587c73 2012 PostQuitMessage(0);
2013 } else {
2014 must_close_session = TRUE;
2015 session_closed = TRUE;
2016 /* exitcode == INT_MAX indicates that the connection was closed
2017 * by a fatal error, so an error box will be coming our way and
2018 * we should not generate this informational one. */
2019 if (exitcode != INT_MAX)
2020 MessageBox(hwnd, "Connection closed by remote host",
2021 appname, MB_OK | MB_ICONINFORMATION);
2022 }
2023 }
2024}
39934deb 2025
d719927e 2026void timer_change_notify(unsigned long next)
39934deb 2027{
d719927e 2028 unsigned long now = GETTICKCOUNT();
2029 long ticks;
2030 if (now - next < INT_MAX)
2031 ticks = 0;
2032 else
2033 ticks = next - now;
39934deb 2034 KillTimer(hwnd, TIMING_TIMER_ID);
2035 SetTimer(hwnd, TIMING_TIMER_ID, ticks, NULL);
2036 timing_next_time = next;
2037}
2038
4a693cfc 2039static void conf_cache_data(void)
2040{
2041 /* Cache some items from conf to speed lookups in very hot code */
2042 cursor_type = conf_get_int(conf, CONF_cursor_type);
2043 vtmode = conf_get_int(conf, CONF_vtmode);
2044}
2045
32874aea 2046static LRESULT CALLBACK WndProc(HWND hwnd, UINT message,
2047 WPARAM wParam, LPARAM lParam)
2048{
374330e2 2049 HDC hdc;
374330e2 2050 static int ignore_clip = FALSE;
3ad8c6db 2051 static int need_backend_resize = FALSE;
1ba99d2c 2052 static int fullscr_on_max = FALSE;
19caf318 2053 static int processed_resize = FALSE;
d8a13f62 2054 static UINT last_mousemove = 0;
4a693cfc 2055 int resize_action;
374330e2 2056
2057 switch (message) {
59ad2c03 2058 case WM_TIMER:
39934deb 2059 if ((UINT_PTR)wParam == TIMING_TIMER_ID) {
d719927e 2060 unsigned long next;
39934deb 2061
2062 KillTimer(hwnd, TIMING_TIMER_ID);
2063 if (run_timers(timing_next_time, &next)) {
2064 timer_change_notify(next);
2065 } else {
32874aea 2066 }
2067 }
59ad2c03 2068 return 0;
374330e2 2069 case WM_CREATE:
2070 break;
68130d34 2071 case WM_CLOSE:
f6f450e2 2072 {
2073 char *str;
2074 show_mouseptr(1);
2075 str = dupprintf("%s Exit Confirmation", appname);
4a693cfc 2076 if (session_closed || !conf_get_int(conf, CONF_warn_on_close) ||
f6f450e2 2077 MessageBox(hwnd,
2078 "Are you sure you want to close this session?",
84d264cd 2079 str, MB_ICONWARNING | MB_OKCANCEL | MB_DEFBUTTON1)
76347f46 2080 == IDOK)
f6f450e2 2081 DestroyWindow(hwnd);
2082 sfree(str);
2083 }
68130d34 2084 return 0;
374330e2 2085 case WM_DESTROY:
32874aea 2086 show_mouseptr(1);
2087 PostQuitMessage(0);
374330e2 2088 return 0;
12745e35 2089 case WM_INITMENUPOPUP:
2090 if ((HMENU)wParam == savedsess_menu) {
2091 /* About to pop up Saved Sessions sub-menu.
2092 * Refresh the session list. */
2093 get_sesslist(&sesslist, FALSE); /* free */
2094 get_sesslist(&sesslist, TRUE);
2095 update_savedsess_menu();
2096 return 0;
2097 }
2098 break;
63be7767 2099 case WM_COMMAND:
6833a413 2100 case WM_SYSCOMMAND:
2101 switch (wParam & ~0xF) { /* low 4 bits reserved to Windows */
374330e2 2102 case IDM_SHOWLOG:
c5e9c988 2103 showeventlog(hwnd);
374330e2 2104 break;
2105 case IDM_NEWSESS:
2106 case IDM_DUPSESS:
6833a413 2107 case IDM_SAVEDSESS:
374330e2 2108 {
2109 char b[2048];
2110 char c[30], *cl;
e4e4cc7e 2111 int freecl = FALSE;
8e90e4c6 2112 BOOL inherit_handles;
374330e2 2113 STARTUPINFO si;
2114 PROCESS_INFORMATION pi;
2115 HANDLE filemap = NULL;
2116
2117 if (wParam == IDM_DUPSESS) {
2118 /*
2119 * Allocate a file-mapping memory chunk for the
2120 * config structure.
2121 */
2122 SECURITY_ATTRIBUTES sa;
4a693cfc 2123 void *p;
2124 int size;
2125
2126 size = conf_serialised_size(conf);
374330e2 2127
2128 sa.nLength = sizeof(sa);
2129 sa.lpSecurityDescriptor = NULL;
2130 sa.bInheritHandle = TRUE;
4a0b6d61 2131 filemap = CreateFileMapping(INVALID_HANDLE_VALUE,
374330e2 2132 &sa,
2133 PAGE_READWRITE,
4a693cfc 2134 0, size, NULL);
4a0b6d61 2135 if (filemap && filemap != INVALID_HANDLE_VALUE) {
4a693cfc 2136 p = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, size);
374330e2 2137 if (p) {
4a693cfc 2138 conf_serialise(conf, p);
374330e2 2139 UnmapViewOfFile(p);
2140 }
2141 }
8e90e4c6 2142 inherit_handles = TRUE;
4a693cfc 2143 sprintf(c, "putty &%p:%u", filemap, (unsigned)size);
374330e2 2144 cl = c;
0a4aa984 2145 } else if (wParam == IDM_SAVEDSESS) {
865e82ad 2146 unsigned int sessno = ((lParam - IDM_SAVED_MIN)
2147 / MENU_SAVED_STEP) + 1;
62ddb51e 2148 if (sessno < (unsigned)sesslist.nsessions) {
1f1e3232 2149 char *session = sesslist.sessions[sessno];
1f1e3232 2150 cl = dupprintf("putty @%s", session);
8e90e4c6 2151 inherit_handles = FALSE;
1f1e3232 2152 freecl = TRUE;
f8d7977b 2153 } else
2154 break;
8e90e4c6 2155 } else /* IDM_NEWSESS */ {
6833a413 2156 cl = NULL;
8e90e4c6 2157 inherit_handles = FALSE;
2158 }
374330e2 2159
32874aea 2160 GetModuleFileName(NULL, b, sizeof(b) - 1);
374330e2 2161 si.cb = sizeof(si);
2162 si.lpReserved = NULL;
2163 si.lpDesktop = NULL;
2164 si.lpTitle = NULL;
2165 si.dwFlags = 0;
2166 si.cbReserved2 = 0;
2167 si.lpReserved2 = NULL;
8e90e4c6 2168 CreateProcess(b, cl, NULL, NULL, inherit_handles,
32874aea 2169 NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi);
ba8da5e1 2170 CloseHandle(pi.hProcess);
2171 CloseHandle(pi.hThread);
374330e2 2172
2173 if (filemap)
2174 CloseHandle(filemap);
e4e4cc7e 2175 if (freecl)
dcbde236 2176 sfree(cl);
374330e2 2177 }
2178 break;
e3be8de5 2179 case IDM_RESTART:
2180 if (!back) {
2181 logevent(NULL, "----- Session restarted -----");
900f9aca 2182 term_pwron(term, FALSE);
e3be8de5 2183 start_backend();
2184 }
2185
2186 break;
32874aea 2187 case IDM_RECONF:
2188 {
4a693cfc 2189 Conf *prev_conf;
5a73255e 2190 int init_lvl = 1;
68a66cc3 2191 int reconfig_result;
5a73255e 2192
359fd192 2193 if (reconfiguring)
68a66cc3 2194 break;
359fd192 2195 else
68a66cc3 2196 reconfiguring = TRUE;
359fd192 2197
4a693cfc 2198 /*
2199 * Copy the current window title into the stored
2200 * previous configuration, so that doing nothing to
2201 * the window title field in the config box doesn't
2202 * reset the title to its startup state.
2203 */
2204 conf_set_str(conf, CONF_wintitle, window_name);
2205
2206 prev_conf = conf_copy(conf);
e1c8e0ed 2207
68a66cc3 2208 reconfig_result =
b56ff5c3 2209 do_reconfig(hwnd, back ? back->cfg_info(backhandle) : 0);
68a66cc3 2210 reconfiguring = FALSE;
0c33d3a6 2211 if (!reconfig_result) {
2212 conf_free(prev_conf);
32874aea 2213 break;
0c33d3a6 2214 }
e1c8e0ed 2215
4a693cfc 2216 conf_cache_data();
2217
2218 resize_action = conf_get_int(conf, CONF_resize_action);
2cae8529 2219 {
2220 /* Disable full-screen if resizing forbidden */
52c70126 2221 int i;
2222 for (i = 0; i < lenof(popup_menus); i++)
2223 EnableMenuItem(popup_menus[i].menu, IDM_FULLSCREEN,
2224 MF_BYCOMMAND |
4a693cfc 2225 (resize_action == RESIZE_DISABLED)
52c70126 2226 ? MF_GRAYED : MF_ENABLED);
2cae8529 2227 /* Gracefully unzoom if necessary */
4a693cfc 2228 if (IsZoomed(hwnd) && (resize_action == RESIZE_DISABLED))
1ba99d2c 2229 ShowWindow(hwnd, SW_RESTORE);
a401e5f3 2230 }
2231
c229ef97 2232 /* Pass new config data to the logging module */
4a693cfc 2233 log_reconfig(logctx, conf);
e1c8e0ed 2234
32874aea 2235 sfree(logpal);
2236 /*
2237 * Flush the line discipline's edit buffer in the
2238 * case where local editing has just been disabled.
2239 */
4a693cfc 2240 ldisc_configure(ldisc, conf);
e3be8de5 2241 if (ldisc)
2242 ldisc_send(ldisc, NULL, 0, 0);
32874aea 2243 if (pal)
2244 DeleteObject(pal);
2245 logpal = NULL;
2246 pal = NULL;
4a693cfc 2247 conftopalette();
32874aea 2248 init_palette();
2249
64734920 2250 /* Pass new config data to the terminal */
4a693cfc 2251 term_reconfig(term, conf);
0d2086c5 2252
86916870 2253 /* Pass new config data to the back end */
e3be8de5 2254 if (back)
4a693cfc 2255 back->reconfig(backhandle, conf);
86916870 2256
5a73255e 2257 /* Screen size changed ? */
4a693cfc 2258 if (conf_get_int(conf, CONF_height) !=
2259 conf_get_int(prev_conf, CONF_height) ||
2260 conf_get_int(conf, CONF_width) !=
2261 conf_get_int(prev_conf, CONF_width) ||
2262 conf_get_int(conf, CONF_savelines) !=
2263 conf_get_int(prev_conf, CONF_savelines) ||
2264 resize_action == RESIZE_FONT ||
2265 (resize_action == RESIZE_EITHER && IsZoomed(hwnd)) ||
2266 resize_action == RESIZE_DISABLED)
2267 term_size(term, conf_get_int(conf, CONF_height),
2268 conf_get_int(conf, CONF_width),
2269 conf_get_int(conf, CONF_savelines));
5a73255e 2270
32874aea 2271 /* Enable or disable the scroll bar, etc */
2272 {
1e5eefb6 2273 LONG nflg, flag = GetWindowLongPtr(hwnd, GWL_STYLE);
32874aea 2274 LONG nexflag, exflag =
1e5eefb6 2275 GetWindowLongPtr(hwnd, GWL_EXSTYLE);
32874aea 2276
2277 nexflag = exflag;
4a693cfc 2278 if (conf_get_int(conf, CONF_alwaysontop) !=
2279 conf_get_int(prev_conf, CONF_alwaysontop)) {
2280 if (conf_get_int(conf, CONF_alwaysontop)) {
32874aea 2281 nexflag |= WS_EX_TOPMOST;
2282 SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0,
2283 SWP_NOMOVE | SWP_NOSIZE);
2284 } else {
2285 nexflag &= ~(WS_EX_TOPMOST);
2286 SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
2287 SWP_NOMOVE | SWP_NOSIZE);
2288 }
2289 }
4a693cfc 2290 if (conf_get_int(conf, CONF_sunken_edge))
32874aea 2291 nexflag |= WS_EX_CLIENTEDGE;
2292 else
2293 nexflag &= ~(WS_EX_CLIENTEDGE);
2294
2295 nflg = flag;
4a693cfc 2296 if (conf_get_int(conf, is_full_screen() ?
2297 CONF_scrollbar_in_fullscreen :
2298 CONF_scrollbar))
32874aea 2299 nflg |= WS_VSCROLL;
2300 else
2301 nflg &= ~WS_VSCROLL;
811f10b3 2302
4a693cfc 2303 if (resize_action == RESIZE_DISABLED ||
811f10b3 2304 is_full_screen())
2305 nflg &= ~WS_THICKFRAME;
2306 else
2307 nflg |= WS_THICKFRAME;
2308
4a693cfc 2309 if (resize_action == RESIZE_DISABLED)
811f10b3 2310 nflg &= ~WS_MAXIMIZEBOX;
32874aea 2311 else
811f10b3 2312 nflg |= WS_MAXIMIZEBOX;
32874aea 2313
2314 if (nflg != flag || nexflag != exflag) {
32874aea 2315 if (nflg != flag)
1e5eefb6 2316 SetWindowLongPtr(hwnd, GWL_STYLE, nflg);
32874aea 2317 if (nexflag != exflag)
1e5eefb6 2318 SetWindowLongPtr(hwnd, GWL_EXSTYLE, nexflag);
32874aea 2319
32874aea 2320 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
2321 SWP_NOACTIVATE | SWP_NOCOPYBITS |
5a73255e 2322 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
2323 SWP_FRAMECHANGED);
c9def1b8 2324
5a73255e 2325 init_lvl = 2;
e44d78b6 2326 }
32874aea 2327 }
5a73255e 2328
e44d78b6 2329 /* Oops */
4a693cfc 2330 if (resize_action == RESIZE_DISABLED && IsZoomed(hwnd)) {
e44d78b6 2331 force_normal(hwnd);
5a73255e 2332 init_lvl = 2;
2333 }
2334
4a693cfc 2335 set_title(NULL, conf_get_str(conf, CONF_wintitle));
32874aea 2336 if (IsIconic(hwnd)) {
2337 SetWindowText(hwnd,
4a693cfc 2338 conf_get_int(conf, CONF_win_name_always) ?
2339 window_name : icon_name);
3da0b1d2 2340 }
5a73255e 2341
4a693cfc 2342 {
2343 FontSpec *font = conf_get_fontspec(conf, CONF_font);
2344 FontSpec *prev_font = conf_get_fontspec(prev_conf,
ae62eaeb 2345 CONF_font);
4a693cfc 2346
2347 if (!strcmp(font->name, prev_font->name) ||
2348 !strcmp(conf_get_str(conf, CONF_line_codepage),
2349 conf_get_str(prev_conf, CONF_line_codepage)) ||
2350 font->isbold != prev_font->isbold ||
2351 font->height != prev_font->height ||
2352 font->charset != prev_font->charset ||
2353 conf_get_int(conf, CONF_font_quality) !=
2354 conf_get_int(prev_conf, CONF_font_quality) ||
2355 conf_get_int(conf, CONF_vtmode) !=
2356 conf_get_int(prev_conf, CONF_vtmode) ||
863c5362 2357 conf_get_int(conf, CONF_bold_style) !=
2358 conf_get_int(prev_conf, CONF_bold_style) ||
4a693cfc 2359 resize_action == RESIZE_DISABLED ||
2360 resize_action == RESIZE_EITHER ||
2361 resize_action != conf_get_int(prev_conf,
2362 CONF_resize_action))
2363 init_lvl = 2;
2364 }
5a73255e 2365
2366 InvalidateRect(hwnd, NULL, TRUE);
2367 reset_window(init_lvl);
7732d38a 2368 net_pending_errors();
4a693cfc 2369
2370 conf_free(prev_conf);
32874aea 2371 }
2372 break;
bc1235d4 2373 case IDM_COPYALL:
887035a5 2374 term_copyall(term);
bc1235d4 2375 break;
63be7767 2376 case IDM_PASTE:
e9609ce5 2377 request_paste(NULL);
63be7767 2378 break;
32874aea 2379 case IDM_CLRSB:
887035a5 2380 term_clrsb(term);
32874aea 2381 break;
2382 case IDM_RESET:
900f9aca 2383 term_pwron(term, TRUE);
e3be8de5 2384 if (ldisc)
2385 ldisc_send(ldisc, NULL, 0, 0);
32874aea 2386 break;
374330e2 2387 case IDM_ABOUT:
32874aea 2388 showabout(hwnd);
374330e2 2389 break;
70133c0e 2390 case IDM_HELP:
cb2708d3 2391 launch_help(hwnd, NULL);
70133c0e 2392 break;
0d6dcf38 2393 case SC_MOUSEMENU:
2394 /*
2395 * We get this if the System menu has been activated
2396 * using the mouse.
2397 */
2398 show_mouseptr(1);
2399 break;
dfca2656 2400 case SC_KEYMENU:
2401 /*
0d6dcf38 2402 * We get this if the System menu has been activated
2403 * using the keyboard. This might happen from within
2404 * TranslateKey, in which case it really wants to be
2405 * followed by a `space' character to actually _bring
2406 * the menu up_ rather than just sitting there in
2407 * `ready to appear' state.
dfca2656 2408 */
0d6dcf38 2409 show_mouseptr(1); /* make sure pointer is visible */
dfca2656 2410 if( lParam == 0 )
2411 PostMessage(hwnd, WM_CHAR, ' ', 0);
2412 break;
a401e5f3 2413 case IDM_FULLSCREEN:
1ba99d2c 2414 flip_full_screen();
2415 break;
32874aea 2416 default:
1f1e3232 2417 if (wParam >= IDM_SAVED_MIN && wParam < IDM_SAVED_MAX) {
32874aea 2418 SendMessage(hwnd, WM_SYSCOMMAND, IDM_SAVEDSESS, wParam);
2419 }
125105d1 2420 if (wParam >= IDM_SPECIAL_MIN && wParam <= IDM_SPECIAL_MAX) {
2421 int i = (wParam - IDM_SPECIAL_MIN) / 0x10;
125105d1 2422 /*
2423 * Ensure we haven't been sent a bogus SYSCOMMAND
2424 * which would cause us to reference invalid memory
2425 * and crash. Perhaps I'm just too paranoid here.
2426 */
6f2d0cde 2427 if (i >= n_specials)
2428 break;
2429 if (back)
2430 back->special(backhandle, specials[i].code);
2431 net_pending_errors();
125105d1 2432 }
374330e2 2433 }
2434 break;
37508af4 2435
2436#define X_POS(l) ((int)(short)LOWORD(l))
2437#define Y_POS(l) ((int)(short)HIWORD(l))
2438
5a73255e 2439#define TO_CHR_X(x) ((((x)<0 ? (x)-font_width+1 : (x))-offset_width) / font_width)
2440#define TO_CHR_Y(y) ((((y)<0 ? (y)-font_height+1: (y))-offset_height) / font_height)
374330e2 2441 case WM_LBUTTONDOWN:
374330e2 2442 case WM_MBUTTONDOWN:
374330e2 2443 case WM_RBUTTONDOWN:
01c034ad 2444 case WM_LBUTTONUP:
2445 case WM_MBUTTONUP:
374330e2 2446 case WM_RBUTTONUP:
ebc0310d 2447 if (message == WM_RBUTTONDOWN &&
4a693cfc 2448 ((wParam & MK_CONTROL) ||
2449 (conf_get_int(conf, CONF_mouse_is_xterm) == 2))) {
63be7767 2450 POINT cursorpos;
2451
ebc0310d 2452 show_mouseptr(1); /* make sure pointer is visible */
63be7767 2453 GetCursorPos(&cursorpos);
2454 TrackPopupMenu(popup_menus[CTXMENU].menu,
2455 TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RIGHTBUTTON,
2456 cursorpos.x, cursorpos.y,
2457 0, hwnd, NULL);
2458 break;
2459 }
01c034ad 2460 {
2461 int button, press;
6908fed7 2462
01c034ad 2463 switch (message) {
32874aea 2464 case WM_LBUTTONDOWN:
2465 button = MBT_LEFT;
472f9eae 2466 wParam |= MK_LBUTTON;
32874aea 2467 press = 1;
2468 break;
2469 case WM_MBUTTONDOWN:
2470 button = MBT_MIDDLE;
472f9eae 2471 wParam |= MK_MBUTTON;
32874aea 2472 press = 1;
2473 break;
2474 case WM_RBUTTONDOWN:
2475 button = MBT_RIGHT;
472f9eae 2476 wParam |= MK_RBUTTON;
32874aea 2477 press = 1;
2478 break;
2479 case WM_LBUTTONUP:
2480 button = MBT_LEFT;
472f9eae 2481 wParam &= ~MK_LBUTTON;
32874aea 2482 press = 0;
2483 break;
2484 case WM_MBUTTONUP:
2485 button = MBT_MIDDLE;
472f9eae 2486 wParam &= ~MK_MBUTTON;
32874aea 2487 press = 0;
2488 break;
2489 case WM_RBUTTONUP:
2490 button = MBT_RIGHT;
472f9eae 2491 wParam &= ~MK_RBUTTON;
32874aea 2492 press = 0;
2493 break;
2d466ffd 2494 default:
2495 button = press = 0; /* shouldn't happen */
01c034ad 2496 }
2497 show_mouseptr(1);
2cae8529 2498 /*
2499 * Special case: in full-screen mode, if the left
2500 * button is clicked in the very top left corner of the
2501 * window, we put up the System menu instead of doing
2502 * selection.
2503 */
52a4d159 2504 {
2505 char mouse_on_hotspot = 0;
2506 POINT pt;
2507
2508 GetCursorPos(&pt);
2509#ifndef NO_MULTIMON
2510 {
2511 HMONITOR mon;
2512 MONITORINFO mi;
2513
2514 mon = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL);
2515
2516 if (mon != NULL) {
2517 mi.cbSize = sizeof(MONITORINFO);
2518 GetMonitorInfo(mon, &mi);
2519
2520 if (mi.rcMonitor.left == pt.x &&
2521 mi.rcMonitor.top == pt.y) {
2522 mouse_on_hotspot = 1;
2523 }
52a4d159 2524 }
2525 }
2526#else
2527 if (pt.x == 0 && pt.y == 0) {
2528 mouse_on_hotspot = 1;
2529 }
2530#endif
2531 if (is_full_screen() && press &&
2532 button == MBT_LEFT && mouse_on_hotspot) {
2533 SendMessage(hwnd, WM_SYSCOMMAND, SC_MOUSEMENU,
2534 MAKELPARAM(pt.x, pt.y));
2535 return 0;
2536 }
2cae8529 2537 }
52a4d159 2538
01c034ad 2539 if (press) {
32874aea 2540 click(button,
2541 TO_CHR_X(X_POS(lParam)), TO_CHR_Y(Y_POS(lParam)),
6908fed7 2542 wParam & MK_SHIFT, wParam & MK_CONTROL,
2543 is_alt_pressed());
01c034ad 2544 SetCapture(hwnd);
2545 } else {
fc5b0934 2546 term_mouse(term, button, translate_button(button), MA_RELEASE,
32874aea 2547 TO_CHR_X(X_POS(lParam)),
2548 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
6908fed7 2549 wParam & MK_CONTROL, is_alt_pressed());
472f9eae 2550 if (!(wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON)))
2551 ReleaseCapture();
01c034ad 2552 }
2553 }
374330e2 2554 return 0;
2555 case WM_MOUSEMOVE:
d8a13f62 2556 {
2557 /*
2558 * Windows seems to like to occasionally send MOUSEMOVE
2559 * events even if the mouse hasn't moved. Don't unhide
2560 * the mouse pointer in this case.
2561 */
2562 static WPARAM wp = 0;
2563 static LPARAM lp = 0;
2564 if (wParam != wp || lParam != lp ||
2565 last_mousemove != WM_MOUSEMOVE) {
2566 show_mouseptr(1);
2567 wp = wParam; lp = lParam;
2568 last_mousemove = WM_MOUSEMOVE;
2569 }
2570 }
374330e2 2571 /*
2572 * Add the mouse position and message time to the random
7d6ee6ff 2573 * number noise.
374330e2 2574 */
32874aea 2575 noise_ultralight(lParam);
374330e2 2576
319bfe5a 2577 if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) &&
2578 GetCapture() == hwnd) {
374330e2 2579 Mouse_Button b;
2580 if (wParam & MK_LBUTTON)
6c6e7711 2581 b = MBT_LEFT;
374330e2 2582 else if (wParam & MK_MBUTTON)
6c6e7711 2583 b = MBT_MIDDLE;
374330e2 2584 else
6c6e7711 2585 b = MBT_RIGHT;
fc5b0934 2586 term_mouse(term, b, translate_button(b), MA_DRAG,
2587 TO_CHR_X(X_POS(lParam)),
32874aea 2588 TO_CHR_Y(Y_POS(lParam)), wParam & MK_SHIFT,
6908fed7 2589 wParam & MK_CONTROL, is_alt_pressed());
374330e2 2590 }
374330e2 2591 return 0;
d318ef8e 2592 case WM_NCMOUSEMOVE:
d8a13f62 2593 {
2594 static WPARAM wp = 0;
2595 static LPARAM lp = 0;
2596 if (wParam != wp || lParam != lp ||
2597 last_mousemove != WM_NCMOUSEMOVE) {
2598 show_mouseptr(1);
2599 wp = wParam; lp = lParam;
2600 last_mousemove = WM_NCMOUSEMOVE;
2601 }
2602 }
32874aea 2603 noise_ultralight(lParam);
f9cbc48c 2604 break;
374330e2 2605 case WM_IGNORE_CLIP:
2606 ignore_clip = wParam; /* don't panic on DESTROYCLIPBOARD */
2607 break;
2608 case WM_DESTROYCLIPBOARD:
2609 if (!ignore_clip)
887035a5 2610 term_deselect(term);
374330e2 2611 ignore_clip = FALSE;
2612 return 0;
2613 case WM_PAINT:
2614 {
2615 PAINTSTRUCT p;
39934deb 2616
32874aea 2617 HideCaret(hwnd);
2618 hdc = BeginPaint(hwnd, &p);
374330e2 2619 if (pal) {
32874aea 2620 SelectPalette(hdc, pal, TRUE);
2621 RealizePalette(hdc);
374330e2 2622 }
39934deb 2623
8788a416 2624 /*
2625 * We have to be careful about term_paint(). It will
2626 * set a bunch of character cells to INVALID and then
2627 * call do_paint(), which will redraw those cells and
2628 * _then mark them as done_. This may not be accurate:
2629 * when painting in WM_PAINT context we are restricted
2630 * to the rectangle which has just been exposed - so if
2631 * that only covers _part_ of a character cell and the
2632 * rest of it was already visible, that remainder will
2633 * not be redrawn at all. Accordingly, we must not
2634 * paint any character cell in a WM_PAINT context which
2635 * already has a pending update due to terminal output.
2636 * The simplest solution to this - and many, many
2637 * thanks to Hung-Te Lin for working all this out - is
2638 * not to do any actual painting at _all_ if there's a
2639 * pending terminal update: just mark the relevant
2640 * character cells as INVALID and wait for the
2641 * scheduled full update to sort it out.
2642 *
2643 * I have a suspicion this isn't the _right_ solution.
2644 * An alternative approach would be to have terminal.c
2645 * separately track what _should_ be on the terminal
2646 * screen and what _is_ on the terminal screen, and
2647 * have two completely different types of redraw (one
2648 * for full updates, which syncs the former with the
2649 * terminal itself, and one for WM_PAINT which syncs
2650 * the latter with the former); yet another possibility
2651 * would be to have the Windows front end do what the
2652 * GTK one already does, and maintain a bitmap of the
2653 * current terminal appearance so that WM_PAINT becomes
2654 * completely trivial. However, this should do for now.
2655 */
887035a5 2656 term_paint(term, hdc,
5a73255e 2657 (p.rcPaint.left-offset_width)/font_width,
2658 (p.rcPaint.top-offset_height)/font_height,
2659 (p.rcPaint.right-offset_width-1)/font_width,
c1b55581 2660 (p.rcPaint.bottom-offset_height-1)/font_height,
8788a416 2661 !term->window_update_pending);
5a73255e 2662
2663 if (p.fErase ||
2664 p.rcPaint.left < offset_width ||
2665 p.rcPaint.top < offset_height ||
887035a5 2666 p.rcPaint.right >= offset_width + font_width*term->cols ||
2667 p.rcPaint.bottom>= offset_height + font_height*term->rows)
5a73255e 2668 {
2669 HBRUSH fillcolour, oldbrush;
2670 HPEN edge, oldpen;
2671 fillcolour = CreateSolidBrush (
c50f9fde 2672 colours[ATTR_DEFBG>>ATTR_BGSHIFT]);
5a73255e 2673 oldbrush = SelectObject(hdc, fillcolour);
2674 edge = CreatePen(PS_SOLID, 0,
c50f9fde 2675 colours[ATTR_DEFBG>>ATTR_BGSHIFT]);
5a73255e 2676 oldpen = SelectObject(hdc, edge);
2677
bfa7a5db 2678 /*
2679 * Jordan Russell reports that this apparently
2680 * ineffectual IntersectClipRect() call masks a
2681 * Windows NT/2K bug causing strange display
2682 * problems when the PuTTY window is taller than
2683 * the primary monitor. It seems harmless enough...
2684 */
2685 IntersectClipRect(hdc,
2686 p.rcPaint.left, p.rcPaint.top,
2687 p.rcPaint.right, p.rcPaint.bottom);
2688
5a73255e 2689 ExcludeClipRect(hdc,
2690 offset_width, offset_height,
887035a5 2691 offset_width+font_width*term->cols,
2692 offset_height+font_height*term->rows);
5a73255e 2693
2694 Rectangle(hdc, p.rcPaint.left, p.rcPaint.top,
2695 p.rcPaint.right, p.rcPaint.bottom);
2696
31fb1866 2697 /* SelectClipRgn(hdc, NULL); */
5a73255e 2698
2699 SelectObject(hdc, oldbrush);
2700 DeleteObject(fillcolour);
2701 SelectObject(hdc, oldpen);
2702 DeleteObject(edge);
2703 }
32874aea 2704 SelectObject(hdc, GetStockObject(SYSTEM_FONT));
2705 SelectObject(hdc, GetStockObject(WHITE_PEN));
2706 EndPaint(hwnd, &p);
2707 ShowCaret(hwnd);
374330e2 2708 }
2709 return 0;
2710 case WM_NETEVENT:
56f8ea28 2711 /* Notice we can get multiple netevents, FD_READ, FD_WRITE etc
2712 * but the only one that's likely to try to overload us is FD_READ.
2713 * This means buffering just one is fine.
2714 */
2715 if (pending_netevent)
2716 enact_pending_netevent();
2717
2718 pending_netevent = TRUE;
2719 pend_netevent_wParam = wParam;
2720 pend_netevent_lParam = lParam;
2721 if (WSAGETSELECTEVENT(lParam) != FD_READ)
2722 enact_pending_netevent();
2723
39934deb 2724 net_pending_errors();
374330e2 2725 return 0;
2726 case WM_SETFOCUS:
1cff1320 2727 term_set_focus(term, TRUE);
32874aea 2728 CreateCaret(hwnd, caretbm, font_width, font_height);
2729 ShowCaret(hwnd);
f8a28d1f 2730 flash_window(0); /* stop */
32874aea 2731 compose_state = 0;
887035a5 2732 term_update(term);
374330e2 2733 break;
2734 case WM_KILLFOCUS:
32874aea 2735 show_mouseptr(1);
1cff1320 2736 term_set_focus(term, FALSE);
32874aea 2737 DestroyCaret();
c1da5066 2738 caret_x = caret_y = -1; /* ensure caret is replaced next time */
887035a5 2739 term_update(term);
374330e2 2740 break;
73251d5d 2741 case WM_ENTERSIZEMOVE:
5a73255e 2742#ifdef RDB_DEBUG_PATCH
2743 debug((27, "WM_ENTERSIZEMOVE"));
2744#endif
32874aea 2745 EnableSizeTip(1);
2746 resizing = TRUE;
3ad8c6db 2747 need_backend_resize = FALSE;
32874aea 2748 break;
73251d5d 2749 case WM_EXITSIZEMOVE:
32874aea 2750 EnableSizeTip(0);
2751 resizing = FALSE;
5a73255e 2752#ifdef RDB_DEBUG_PATCH
2753 debug((27, "WM_EXITSIZEMOVE"));
2754#endif
2755 if (need_backend_resize) {
4a693cfc 2756 term_size(term, conf_get_int(conf, CONF_height),
2757 conf_get_int(conf, CONF_width),
2758 conf_get_int(conf, CONF_savelines));
5a73255e 2759 InvalidateRect(hwnd, NULL, TRUE);
2760 }
32874aea 2761 break;
374330e2 2762 case WM_SIZING:
5a73255e 2763 /*
2764 * This does two jobs:
2765 * 1) Keep the sizetip uptodate
2766 * 2) Make sure the window size is _stepped_ in units of the font size.
2767 */
4a693cfc 2768 resize_action = conf_get_int(conf, CONF_resize_action);
2769 if (resize_action == RESIZE_TERM ||
2770 (resize_action == RESIZE_EITHER && !is_alt_pressed())) {
374330e2 2771 int width, height, w, h, ew, eh;
32874aea 2772 LPRECT r = (LPRECT) lParam;
374330e2 2773
4a693cfc 2774 if (!need_backend_resize && resize_action == RESIZE_EITHER &&
2775 (conf_get_int(conf, CONF_height) != term->rows ||
2776 conf_get_int(conf, CONF_width) != term->cols)) {
5a73255e 2777 /*
0ed2f48e 2778 * Great! It seems that both the terminal size and the
2779 * font size have been changed and the user is now dragging.
2780 *
2781 * It will now be difficult to get back to the configured
2782 * font size!
2783 *
2784 * This would be easier but it seems to be too confusing.
0ed2f48e 2785 */
4a693cfc 2786 conf_set_int(conf, CONF_height, term->rows);
2787 conf_set_int(conf, CONF_width, term->cols);
0ed2f48e 2788
5a73255e 2789 InvalidateRect(hwnd, NULL, TRUE);
2790 need_backend_resize = TRUE;
2791 }
2792
374330e2 2793 width = r->right - r->left - extra_width;
2794 height = r->bottom - r->top - extra_height;
32874aea 2795 w = (width + font_width / 2) / font_width;
2796 if (w < 1)
2797 w = 1;
2798 h = (height + font_height / 2) / font_height;
2799 if (h < 1)
2800 h = 1;
2801 UpdateSizeTip(hwnd, w, h);
374330e2 2802 ew = width - w * font_width;
2803 eh = height - h * font_height;
2804 if (ew != 0) {
2805 if (wParam == WMSZ_LEFT ||
32874aea 2806 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
374330e2 2807 r->left += ew;
2808 else
2809 r->right -= ew;
2810 }
2811 if (eh != 0) {
2812 if (wParam == WMSZ_TOP ||
32874aea 2813 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
374330e2 2814 r->top += eh;
2815 else
2816 r->bottom -= eh;
2817 }
2818 if (ew || eh)
2819 return 1;
2820 else
2821 return 0;
5a73255e 2822 } else {
2823 int width, height, w, h, rv = 0;
4a693cfc 2824 int window_border = conf_get_int(conf, CONF_window_border);
2825 int ex_width = extra_width + (window_border - offset_width) * 2;
2826 int ex_height = extra_height + (window_border - offset_height) * 2;
5a73255e 2827 LPRECT r = (LPRECT) lParam;
2828
2829 width = r->right - r->left - ex_width;
2830 height = r->bottom - r->top - ex_height;
2831
887035a5 2832 w = (width + term->cols/2)/term->cols;
2833 h = (height + term->rows/2)/term->rows;
2834 if ( r->right != r->left + w*term->cols + ex_width)
5a73255e 2835 rv = 1;
2836
2837 if (wParam == WMSZ_LEFT ||
2838 wParam == WMSZ_BOTTOMLEFT || wParam == WMSZ_TOPLEFT)
887035a5 2839 r->left = r->right - w*term->cols - ex_width;
5a73255e 2840 else
887035a5 2841 r->right = r->left + w*term->cols + ex_width;
5a73255e 2842
887035a5 2843 if (r->bottom != r->top + h*term->rows + ex_height)
5a73255e 2844 rv = 1;
2845
2846 if (wParam == WMSZ_TOP ||
2847 wParam == WMSZ_TOPRIGHT || wParam == WMSZ_TOPLEFT)
887035a5 2848 r->top = r->bottom - h*term->rows - ex_height;
5a73255e 2849 else
887035a5 2850 r->bottom = r->top + h*term->rows + ex_height;
5a73255e 2851
2852 return rv;
374330e2 2853 }
32874aea 2854 /* break; (never reached) */
1ba99d2c 2855 case WM_FULLSCR_ON_MAX:
2856 fullscr_on_max = TRUE;
2857 break;
c1da5066 2858 case WM_MOVE:
2859 sys_cursor_update();
2860 break;
374330e2 2861 case WM_SIZE:
4a693cfc 2862 resize_action = conf_get_int(conf, CONF_resize_action);
5a73255e 2863#ifdef RDB_DEBUG_PATCH
2864 debug((27, "WM_SIZE %s (%d,%d)",
2865 (wParam == SIZE_MINIMIZED) ? "SIZE_MINIMIZED":
2866 (wParam == SIZE_MAXIMIZED) ? "SIZE_MAXIMIZED":
2867 (wParam == SIZE_RESTORED && resizing) ? "to":
2868 (wParam == SIZE_RESTORED) ? "SIZE_RESTORED":
2869 "...",
2870 LOWORD(lParam), HIWORD(lParam)));
2871#endif
0ed2f48e 2872 if (wParam == SIZE_MINIMIZED)
32874aea 2873 SetWindowText(hwnd,
4a693cfc 2874 conf_get_int(conf, CONF_win_name_always) ?
2875 window_name : icon_name);
374330e2 2876 if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED)
32874aea 2877 SetWindowText(hwnd, window_name);
19caf318 2878 if (wParam == SIZE_RESTORED) {
2879 processed_resize = FALSE;
811f10b3 2880 clear_full_screen();
19caf318 2881 if (processed_resize) {
2882 /*
2883 * Inhibit normal processing of this WM_SIZE; a
2884 * secondary one was triggered just now by
2885 * clear_full_screen which contained the correct
2886 * client area size.
2887 */
2888 return 0;
2889 }
2890 }
811f10b3 2891 if (wParam == SIZE_MAXIMIZED && fullscr_on_max) {
811f10b3 2892 fullscr_on_max = FALSE;
19caf318 2893 processed_resize = FALSE;
d0f1bcb4 2894 make_full_screen();
19caf318 2895 if (processed_resize) {
2896 /*
2897 * Inhibit normal processing of this WM_SIZE; a
2898 * secondary one was triggered just now by
2899 * make_full_screen which contained the correct client
2900 * area size.
2901 */
2902 return 0;
2903 }
811f10b3 2904 }
5a73255e 2905
19caf318 2906 processed_resize = TRUE;
2907
4a693cfc 2908 if (resize_action == RESIZE_DISABLED) {
5a73255e 2909 /* A resize, well it better be a minimize. */
2910 reset_window(-1);
2911 } else {
2912
1a6f78fe 2913 int width, height, w, h;
4a693cfc 2914 int window_border = conf_get_int(conf, CONF_window_border);
374330e2 2915
2916 width = LOWORD(lParam);
2917 height = HIWORD(lParam);
5a73255e 2918
232c6eac 2919 if (wParam == SIZE_MAXIMIZED && !was_zoomed) {
2920 was_zoomed = 1;
2921 prev_rows = term->rows;
2922 prev_cols = term->cols;
4a693cfc 2923 if (resize_action == RESIZE_TERM) {
232c6eac 2924 w = width / font_width;
2925 if (w < 1) w = 1;
2926 h = height / font_height;
2927 if (h < 1) h = 1;
2928
5f4e628b 2929 if (resizing) {
2930 /*
2931 * As below, if we're in the middle of an
2932 * interactive resize we don't call
2933 * back->size. In Windows 7, this case can
2934 * arise in maximisation as well via the Aero
2935 * snap UI.
2936 */
2937 need_backend_resize = TRUE;
2938 conf_set_int(conf, CONF_height, h);
2939 conf_set_int(conf, CONF_width, w);
2940 } else {
2941 term_size(term, h, w,
2942 conf_get_int(conf, CONF_savelines));
2943 }
232c6eac 2944 }
2945 reset_window(0);
2946 } else if (wParam == SIZE_RESTORED && was_zoomed) {
2947 was_zoomed = 0;
4a693cfc 2948 if (resize_action == RESIZE_TERM) {
2949 w = (width-window_border*2) / font_width;
754fa076 2950 if (w < 1) w = 1;
4a693cfc 2951 h = (height-window_border*2) / font_height;
754fa076 2952 if (h < 1) h = 1;
4a693cfc 2953 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
4e05e104 2954 reset_window(2);
4a693cfc 2955 } else if (resize_action != RESIZE_FONT)
232c6eac 2956 reset_window(2);
2957 else
2958 reset_window(0);
2959 } else if (wParam == SIZE_MINIMIZED) {
2960 /* do nothing */
4a693cfc 2961 } else if (resize_action == RESIZE_TERM ||
2962 (resize_action == RESIZE_EITHER &&
47779c35 2963 !is_alt_pressed())) {
4a693cfc 2964 w = (width-window_border*2) / font_width;
232c6eac 2965 if (w < 1) w = 1;
4a693cfc 2966 h = (height-window_border*2) / font_height;
232c6eac 2967 if (h < 1) h = 1;
2968
2969 if (resizing) {
2970 /*
2971 * Don't call back->size in mid-resize. (To
2972 * prevent massive numbers of resize events
2973 * getting sent down the connection during an NT
2974 * opaque drag.)
2975 */
3ad8c6db 2976 need_backend_resize = TRUE;
4a693cfc 2977 conf_set_int(conf, CONF_height, h);
a1659af6 2978 conf_set_int(conf, CONF_width, w);
232c6eac 2979 } else {
4a693cfc 2980 term_size(term, h, w, conf_get_int(conf, CONF_savelines));
232c6eac 2981 }
2982 } else {
2983 reset_window(0);
374330e2 2984 }
2985 }
c1da5066 2986 sys_cursor_update();
374330e2 2987 return 0;
2988 case WM_VSCROLL:
2989 switch (LOWORD(wParam)) {
32874aea 2990 case SB_BOTTOM:
887035a5 2991 term_scroll(term, -1, 0);
32874aea 2992 break;
2993 case SB_TOP:
887035a5 2994 term_scroll(term, +1, 0);
32874aea 2995 break;
2996 case SB_LINEDOWN:
887035a5 2997 term_scroll(term, 0, +1);
32874aea 2998 break;
2999 case SB_LINEUP:
887035a5 3000 term_scroll(term, 0, -1);
32874aea 3001 break;
3002 case SB_PAGEDOWN:
887035a5 3003 term_scroll(term, 0, +term->rows / 2);
32874aea 3004 break;
3005 case SB_PAGEUP:
887035a5 3006 term_scroll(term, 0, -term->rows / 2);
32874aea 3007 break;
3008 case SB_THUMBPOSITION:
3009 case SB_THUMBTRACK:
e9ad953c 3010 /*
3011 * Use GetScrollInfo instead of HIWORD(wParam) to get
3012 * 32-bit scroll position.
3013 */
3014 {
3015 SCROLLINFO si;
3016
3017 si.cbSize = sizeof(si);
3018 si.fMask = SIF_TRACKPOS;
3019 if (GetScrollInfo(hwnd, SB_VERT, &si) == 0)
3020 si.nTrackPos = HIWORD(wParam);
3021 term_scroll(term, 1, si.nTrackPos);
3022 }
32874aea 3023 break;
3024 }
3025 break;
3026 case WM_PALETTECHANGED:
374330e2 3027 if ((HWND) wParam != hwnd && pal != NULL) {
a8327734 3028 HDC hdc = get_ctx(NULL);
374330e2 3029 if (hdc) {
32874aea 3030 if (RealizePalette(hdc) > 0)
3031 UpdateColors(hdc);
3032 free_ctx(hdc);
374330e2 3033 }
3034 }
3035 break;
3036 case WM_QUERYNEWPALETTE:
3037 if (pal != NULL) {
a8327734 3038 HDC hdc = get_ctx(NULL);
374330e2 3039 if (hdc) {
32874aea 3040 if (RealizePalette(hdc) > 0)
3041 UpdateColors(hdc);
3042 free_ctx(hdc);
374330e2 3043 return TRUE;
3044 }
3045 }
3046 return FALSE;
3047 case WM_KEYDOWN:
3048 case WM_SYSKEYDOWN:
c9def1b8 3049 case WM_KEYUP:
3050 case WM_SYSKEYUP:
374330e2 3051 /*
3052 * Add the scan code and keypress timing to the random
7d6ee6ff 3053 * number noise.
374330e2 3054 */
32874aea 3055 noise_ultralight(lParam);
374330e2 3056
3057 /*
3058 * We don't do TranslateMessage since it disassociates the
3059 * resulting CHAR message from the KEYDOWN that sparked it,
3060 * which we occasionally don't want. Instead, we process
3061 * KEYDOWN, and call the Win32 translator functions so that
3062 * we get the translations under _our_ control.
3063 */
3064 {
3065 unsigned char buf[20];
3066 int len;
3067
43e95114 3068 if (wParam == VK_PROCESSKEY) { /* IME PROCESS key */
3069 if (message == WM_KEYDOWN) {
3070 MSG m;
3071 m.hwnd = hwnd;
3072 m.message = WM_KEYDOWN;
3073 m.wParam = wParam;
3074 m.lParam = lParam & 0xdfff;
3075 TranslateMessage(&m);
3076 } else break; /* pass to Windows for default processing */
32874aea 3077 } else {
3078 len = TranslateKey(message, wParam, lParam, buf);
3cf144db 3079 if (len == -1)
32874aea 3080 return DefWindowProc(hwnd, message, wParam, lParam);
5471d09a 3081
b2a1eade 3082 if (len != 0) {
bca9517a 3083 /*
256cb87c 3084 * Interrupt an ongoing paste. I'm not sure
3085 * this is sensible, but for the moment it's
3086 * preferable to having to faff about buffering
3087 * things.
3088 */
887035a5 3089 term_nopaste(term);
256cb87c 3090
3091 /*
bca9517a 3092 * We need not bother about stdin backlogs
3093 * here, because in GUI PuTTY we can't do
3094 * anything about it anyway; there's no means
3095 * of asking Windows to hold off on KEYDOWN
3096 * messages. We _have_ to buffer everything
3097 * we're sent.
3098 */
887035a5 3099 term_seen_key_event(term);
e3be8de5 3100 if (ldisc)
3101 ldisc_send(ldisc, buf, len, 1);
32874aea 3102 show_mouseptr(0);
bca9517a 3103 }
3cf144db 3104 }
374330e2 3105 }
7732d38a 3106 net_pending_errors();
374330e2 3107 return 0;
4eeb7d09 3108 case WM_INPUTLANGCHANGE:
a5ce2e9f 3109 /* wParam == Font number */
3110 /* lParam == Locale */
3111 set_input_locale((HKL)lParam);
c1da5066 3112 sys_cursor_update();
4eeb7d09 3113 break;
fd64bb27 3114 case WM_IME_STARTCOMPOSITION:
3115 {
71f6951d 3116 HIMC hImc = ImmGetContext(hwnd);
3117 ImmSetCompositionFont(hImc, &lfont);
3118 ImmReleaseContext(hwnd, hImc);
71f6951d 3119 }
3120 break;
88485e4d 3121 case WM_IME_COMPOSITION:
3122 {
3123 HIMC hIMC;
3124 int n;
3125 char *buff;
a3cfd0c8 3126
88485e4d 3127 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ||
3128 osVersion.dwPlatformId == VER_PLATFORM_WIN32s) break; /* no Unicode */
3129
3130 if ((lParam & GCS_RESULTSTR) == 0) /* Composition unfinished. */
3131 break; /* fall back to DefWindowProc */
3132
3133 hIMC = ImmGetContext(hwnd);
3134 n = ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, NULL, 0);
3135
3136 if (n > 0) {
a3cfd0c8 3137 int i;
3d88e64d 3138 buff = snewn(n, char);
88485e4d 3139 ImmGetCompositionStringW(hIMC, GCS_RESULTSTR, buff, n);
a3cfd0c8 3140 /*
3141 * Jaeyoun Chung reports that Korean character
3142 * input doesn't work correctly if we do a single
3143 * luni_send() covering the whole of buff. So
3144 * instead we luni_send the characters one by one.
3145 */
887035a5 3146 term_seen_key_event(term);
b03db5b6 3147 /* don't divide SURROGATE PAIR */
3148 if (ldisc) {
3149 for (i = 0; i < n; i += 2) {
3150 WCHAR hs = *(unsigned short *)(buff+i);
3151 if (IS_HIGH_SURROGATE(hs) && i+2 < n) {
3152 WCHAR ls = *(unsigned short *)(buff+i+2);
3153 if (IS_LOW_SURROGATE(ls)) {
3154 luni_send(ldisc, (unsigned short *)(buff+i), 2, 1);
3155 i += 2;
3156 continue;
3157 }
3158 }
e3be8de5 3159 luni_send(ldisc, (unsigned short *)(buff+i), 1, 1);
b03db5b6 3160 }
2cb50250 3161 }
88485e4d 3162 free(buff);
3163 }
3164 ImmReleaseContext(hwnd, hIMC);
3165 return 1;
3166 }
3167
4eeb7d09 3168 case WM_IME_CHAR:
3169 if (wParam & 0xFF00) {
3cf144db 3170 unsigned char buf[2];
3171
3172 buf[1] = wParam;
3173 buf[0] = wParam >> 8;
887035a5 3174 term_seen_key_event(term);
e3be8de5 3175 if (ldisc)
3176 lpage_send(ldisc, kbd_codepage, buf, 2, 1);
4eeb7d09 3177 } else {
3178 char c = (unsigned char) wParam;
887035a5 3179 term_seen_key_event(term);
e3be8de5 3180 if (ldisc)
3181 lpage_send(ldisc, kbd_codepage, &c, 1, 1);
3cf144db 3182 }
4eeb7d09 3183 return (0);
374330e2 3184 case WM_CHAR:
3185 case WM_SYSCHAR:
3186 /*
3187 * Nevertheless, we are prepared to deal with WM_CHAR
3188 * messages, should they crop up. So if someone wants to
3189 * post the things to us as part of a macro manoeuvre,
3190 * we're ready to cope.
3191 */
32874aea 3192 {
4eeb7d09 3193 char c = (unsigned char)wParam;
887035a5 3194 term_seen_key_event(term);
e3be8de5 3195 if (ldisc)
3196 lpage_send(ldisc, CP_ACP, &c, 1, 1);
374330e2 3197 }
3198 return 0;
49a332ff 3199 case WM_SYSCOLORCHANGE:
4a693cfc 3200 if (conf_get_int(conf, CONF_system_colour)) {
49a332ff 3201 /* Refresh palette from system colours. */
3202 /* XXX actually this zaps the entire palette. */
3203 systopalette();
3204 init_palette();
3205 /* Force a repaint of the terminal window. */
3206 term_invalidate(term);
3207 }
3208 break;
c44bf5bd 3209 case WM_AGENT_CALLBACK:
3210 {
3211 struct agent_callback *c = (struct agent_callback *)lParam;
3212 c->callback(c->callback_ctx, c->data, c->len);
3213 sfree(c);
3214 }
3215 return 0;
1d432454 3216 case WM_GOT_CLIPDATA:
3217 if (process_clipdata((HGLOBAL)lParam, wParam))
3218 term_do_paste(term);
3219 return 0;
011b0e33 3220 default:
16837214 3221 if (message == wm_mousewheel || message == WM_MOUSEWHEEL) {
c1b55581 3222 int shift_pressed=0, control_pressed=0;
011b0e33 3223
3224 if (message == WM_MOUSEWHEEL) {
3225 wheel_accumulator += (short)HIWORD(wParam);
3226 shift_pressed=LOWORD(wParam) & MK_SHIFT;
3227 control_pressed=LOWORD(wParam) & MK_CONTROL;
3228 } else {
3229 BYTE keys[256];
3230 wheel_accumulator += (int)wParam;
3231 if (GetKeyboardState(keys)!=0) {
3232 shift_pressed=keys[VK_SHIFT]&0x80;
3233 control_pressed=keys[VK_CONTROL]&0x80;
3234 }
3235 }
3236
3237 /* process events when the threshold is reached */
3238 while (abs(wheel_accumulator) >= WHEEL_DELTA) {
3239 int b;
3240
3241 /* reduce amount for next time */
3242 if (wheel_accumulator > 0) {
3243 b = MBT_WHEEL_UP;
3244 wheel_accumulator -= WHEEL_DELTA;
3245 } else if (wheel_accumulator < 0) {
3246 b = MBT_WHEEL_DOWN;
3247 wheel_accumulator += WHEEL_DELTA;
3248 } else
3249 break;
3250
d68efe28 3251 if (send_raw_mouse &&
3252 !(conf_get_int(conf, CONF_mouse_override) &&
3253 shift_pressed)) {
7a3fbdaf 3254 /* Mouse wheel position is in screen coordinates for
3255 * some reason */
3256 POINT p;
3257 p.x = X_POS(lParam); p.y = Y_POS(lParam);
3258 if (ScreenToClient(hwnd, &p)) {
3259 /* send a mouse-down followed by a mouse up */
3260 term_mouse(term, b, translate_button(b),
3261 MA_CLICK,
3262 TO_CHR_X(p.x),
3263 TO_CHR_Y(p.y), shift_pressed,
3264 control_pressed, is_alt_pressed());
3265 term_mouse(term, b, translate_button(b),
3266 MA_RELEASE, TO_CHR_X(p.x),
3267 TO_CHR_Y(p.y), shift_pressed,
3268 control_pressed, is_alt_pressed());
3269 } /* else: not sure when this can fail */
011b0e33 3270 } else {
3271 /* trigger a scroll */
887035a5 3272 term_scroll(term, 0,
3273 b == MBT_WHEEL_UP ?
3274 -term->rows / 2 : term->rows / 2);
011b0e33 3275 }
3276 }
3277 return 0;
3278 }
374330e2 3279 }
3280
5c4dea05 3281 /*
3282 * Any messages we don't process completely above are passed through to
3283 * DefWindowProc() for default processing.
3284 */
32874aea 3285 return DefWindowProc(hwnd, message, wParam, lParam);
374330e2 3286}
3287
3288/*
ec8679e9 3289 * Move the system caret. (We maintain one, even though it's
3290 * invisible, for the benefit of blind people: apparently some
3291 * helper software tracks the system caret, so we should arrange to
3292 * have one.)
3293 */
a8327734 3294void sys_cursor(void *frontend, int x, int y)
32874aea 3295{
c1da5066 3296 int cx, cy;
3297
887035a5 3298 if (!term->has_focus) return;
c1da5066 3299
3300 /*
3301 * Avoid gratuitously re-updating the cursor position and IMM
3302 * window if there's no actual change required.
3303 */
3304 cx = x * font_width + offset_width;
3305 cy = y * font_height + offset_height;
3306 if (cx == caret_x && cy == caret_y)
3307 return;
3308 caret_x = cx;
3309 caret_y = cy;
3310
3311 sys_cursor_update();
3312}
3313
3314static void sys_cursor_update(void)
3315{
88485e4d 3316 COMPOSITIONFORM cf;
3317 HIMC hIMC;
3318
887035a5 3319 if (!term->has_focus) return;
c1da5066 3320
3321 if (caret_x < 0 || caret_y < 0)
3322 return;
3323
3324 SetCaretPos(caret_x, caret_y);
88485e4d 3325
3326 /* IMM calls on Win98 and beyond only */
3327 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32s) return; /* 3.11 */
3328
3329 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS &&
3330 osVersion.dwMinorVersion == 0) return; /* 95 */
3331
3332 /* we should have the IMM functions */
3333 hIMC = ImmGetContext(hwnd);
3334 cf.dwStyle = CFS_POINT;
c1da5066 3335 cf.ptCurrentPos.x = caret_x;
3336 cf.ptCurrentPos.y = caret_y;
88485e4d 3337 ImmSetCompositionWindow(hIMC, &cf);
3338
3339 ImmReleaseContext(hwnd, hIMC);
ec8679e9 3340}
3341
3342/*
374330e2 3343 * Draw a line of text in the window, at given character
3344 * coordinates, in given attributes.
3345 *
3346 * We are allowed to fiddle with the contents of `text'.
3347 */
c6958dfe 3348void do_text_internal(Context ctx, int x, int y, wchar_t *text, int len,
3349 unsigned long attr, int lattr)
32874aea 3350{
374330e2 3351 COLORREF fg, bg, t;
3352 int nfg, nbg, nfont;
3353 HDC hdc = ctx;
59ad2c03 3354 RECT line_box;
3355 int force_manual_underline = 0;
36566009 3356 int fnt_width, char_width;
4eeb7d09 3357 int text_adjust = 0;
14ce9887 3358 int xoffset = 0;
3359 int maxlen, remaining, opaque;
863c5362 3360 int is_cursor = FALSE;
798a50bf 3361 static int *lpDx = NULL;
3362 static int lpDx_len = 0;
3363 int *lpDx_maybe;
b03db5b6 3364 int len2; /* for SURROGATE PAIR */
4eeb7d09 3365
36566009 3366 lattr &= LATTR_MODE;
3367
3368 char_width = fnt_width = font_width * (1 + (lattr != LATTR_NORM));
3369
4eeb7d09 3370 if (attr & ATTR_WIDE)
3371 char_width *= 2;
59ad2c03 3372
5a73255e 3373 /* Only want the left half of double width lines */
887035a5 3374 if (lattr != LATTR_NORM && x*2 >= term->cols)
5a73255e 3375 return;
3376
c9def1b8 3377 x *= fnt_width;
374330e2 3378 y *= font_height;
5a73255e 3379 x += offset_width;
3380 y += offset_height;
374330e2 3381
4a693cfc 3382 if ((attr & TATTR_ACTCURS) && (cursor_type == 0 || term->big_cursor)) {
cecb13f6 3383 attr &= ~(ATTR_REVERSE|ATTR_BLINK|ATTR_COLOURS);
cecb13f6 3384 /* cursor fg and bg */
3385 attr |= (260 << ATTR_FGSHIFT) | (261 << ATTR_BGSHIFT);
863c5362 3386 is_cursor = TRUE;
374330e2 3387 }
3388
3389 nfont = 0;
4a693cfc 3390 if (vtmode == VT_POORMAN && lattr != LATTR_NORM) {
4eeb7d09 3391 /* Assume a poorman font is borken in other ways too. */
3392 lattr = LATTR_WIDE;
3393 } else
3394 switch (lattr) {
3395 case LATTR_NORM:
3396 break;
3397 case LATTR_WIDE:
3398 nfont |= FONT_WIDE;
374330e2 3399 break;
4eeb7d09 3400 default:
3401 nfont |= FONT_WIDE + FONT_HIGH;
3402 break;
3403 }
5a73255e 3404 if (attr & ATTR_NARROW)
3405 nfont |= FONT_NARROW;
4eeb7d09 3406
9517adb3 3407#ifdef USES_VTLINE_HACK
4eeb7d09 3408 /* Special hack for the VT100 linedraw glyphs. */
36566009 3409 if (text[0] >= 0x23BA && text[0] <= 0x23BD) {
3410 switch ((unsigned char) (text[0])) {
3411 case 0xBA:
3412 text_adjust = -2 * font_height / 5;
3413 break;
3414 case 0xBB:
3415 text_adjust = -1 * font_height / 5;
3416 break;
3417 case 0xBC:
3418 text_adjust = font_height / 5;
3419 break;
3420 case 0xBD:
3421 text_adjust = 2 * font_height / 5;
3422 break;
3423 }
3424 if (lattr == LATTR_TOP || lattr == LATTR_BOT)
3425 text_adjust *= 2;
3426 text[0] = ucsdata.unitab_xterm['q'];
3427 if (attr & ATTR_UNDER) {
3428 attr &= ~ATTR_UNDER;
3429 force_manual_underline = 1;
374330e2 3430 }
3431 }
9517adb3 3432#endif
374330e2 3433
4eeb7d09 3434 /* Anything left as an original character set is unprintable. */
b03db5b6 3435 if (DIRECT_CHAR(text[0]) &&
3436 (len < 2 || !IS_SURROGATE_PAIR(text[0], text[1]))) {
36566009 3437 int i;
3438 for (i = 0; i < len; i++)
3439 text[i] = 0xFFFD;
4eeb7d09 3440 }
3441
3442 /* OEM CP */
36566009 3443 if ((text[0] & CSET_MASK) == CSET_OEMCP)
4eeb7d09 3444 nfont |= FONT_OEM;
3445
37ca32ed 3446 nfg = ((attr & ATTR_FGMASK) >> ATTR_FGSHIFT);
37ca32ed 3447 nbg = ((attr & ATTR_BGMASK) >> ATTR_BGSHIFT);
863c5362 3448 if (bold_font_mode == BOLD_FONT && (attr & ATTR_BOLD))
374330e2 3449 nfont |= FONT_BOLD;
3450 if (und_mode == UND_FONT && (attr & ATTR_UNDER))
3451 nfont |= FONT_UNDERLINE;
4eeb7d09 3452 another_font(nfont);
32874aea 3453 if (!fonts[nfont]) {
3454 if (nfont & FONT_UNDERLINE)
59ad2c03 3455 force_manual_underline = 1;
3456 /* Don't do the same for manual bold, it could be bad news. */
3457
32874aea 3458 nfont &= ~(FONT_BOLD | FONT_UNDERLINE);
59ad2c03 3459 }
4eeb7d09 3460 another_font(nfont);
3461 if (!fonts[nfont])
3462 nfont = FONT_NORMAL;
374330e2 3463 if (attr & ATTR_REVERSE) {
32874aea 3464 t = nfg;
3465 nfg = nbg;
3466 nbg = t;
374330e2 3467 }
863c5362 3468 if (bold_colours && (attr & ATTR_BOLD) && !is_cursor) {
cecb13f6 3469 if (nfg < 16) nfg |= 8;
3470 else if (nfg >= 256) nfg |= 1;
3471 }
863c5362 3472 if (bold_colours && (attr & ATTR_BLINK)) {
cecb13f6 3473 if (nbg < 16) nbg |= 8;
3474 else if (nbg >= 256) nbg |= 1;
3475 }
374330e2 3476 fg = colours[nfg];
3477 bg = colours[nbg];
32874aea 3478 SelectObject(hdc, fonts[nfont]);
3479 SetTextColor(hdc, fg);
3480 SetBkColor(hdc, bg);
c6958dfe 3481 if (attr & TATTR_COMBINING)
3482 SetBkMode(hdc, TRANSPARENT);
3483 else
3484 SetBkMode(hdc, OPAQUE);
32874aea 3485 line_box.left = x;
3486 line_box.top = y;
4eeb7d09 3487 line_box.right = x + char_width * len;
32874aea 3488 line_box.bottom = y + font_height;
b03db5b6 3489 /* adjust line_box.right for SURROGATE PAIR & VARIATION SELECTOR */
3490 {
3491 int i;
3492 int rc_width = 0;
3493 for (i = 0; i < len ; i++) {
3494 if (i+1 < len && IS_HIGH_VARSEL(text[i], text[i+1])) {
3495 i++;
3496 } else if (i+1 < len && IS_SURROGATE_PAIR(text[i], text[i+1])) {
3497 rc_width += char_width;
3498 i++;
3499 } else if (IS_LOW_VARSEL(text[i])) {
3500 /* do nothing */
3501 } else {
3502 rc_width += char_width;
3503 }
3504 }
3505 line_box.right = line_box.left + rc_width;
3506 }
4eeb7d09 3507
5a73255e 3508 /* Only want the left half of double width lines */
887035a5 3509 if (line_box.right > font_width*term->cols+offset_width)
3510 line_box.right = font_width*term->cols+offset_width;
5a73255e 3511
14ce9887 3512 if (font_varpitch) {
3513 /*
3514 * If we're using a variable-pitch font, we unconditionally
3515 * draw the glyphs one at a time and centre them in their
3516 * character cells (which means in particular that we must
798a50bf 3517 * disable the lpDx mechanism). This gives slightly odd but
14ce9887 3518 * generally reasonable results.
3519 */
3520 xoffset = char_width / 2;
3521 SetTextAlign(hdc, TA_TOP | TA_CENTER | TA_NOUPDATECP);
798a50bf 3522 lpDx_maybe = NULL;
14ce9887 3523 maxlen = 1;
3524 } else {
3525 /*
3526 * In a fixed-pitch font, we draw the whole string in one go
3527 * in the normal way.
3528 */
3529 xoffset = 0;
3530 SetTextAlign(hdc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
798a50bf 3531 lpDx_maybe = lpDx;
14ce9887 3532 maxlen = len;
3533 }
3534
3535 opaque = TRUE; /* start by erasing the rectangle */
3536 for (remaining = len; remaining > 0;
b03db5b6 3537 text += len, remaining -= len, x += char_width * len2) {
14ce9887 3538 len = (maxlen < remaining ? maxlen : remaining);
b03db5b6 3539 /* don't divide SURROGATE PAIR and VARIATION SELECTOR */
3540 len2 = len;
3541 if (maxlen == 1) {
3542 if (remaining >= 1 && IS_SURROGATE_PAIR(text[0], text[1]))
3543 len++;
3544 if (remaining-len >= 1 && IS_LOW_VARSEL(text[len]))
3545 len++;
3546 else if (remaining-len >= 2 &&
3547 IS_HIGH_VARSEL(text[len], text[len+1]))
3548 len += 2;
3549 }
14ce9887 3550
dc5d05d1 3551 if (len > lpDx_len) {
3552 lpDx_len = len * 9 / 8 + 16;
3553 lpDx = sresize(lpDx, lpDx_len, int);
3554
3555 if (lpDx_maybe) lpDx_maybe = lpDx;
3556 }
3557
798a50bf 3558 {
3559 int i;
b03db5b6 3560 /* only last char has dx width in SURROGATE PAIR and
3561 * VARIATION sequence */
3562 for (i = 0; i < len; i++) {
798a50bf 3563 lpDx[i] = char_width;
b03db5b6 3564 if (i+1 < len && IS_HIGH_VARSEL(text[i], text[i+1])) {
3565 if (i > 0) lpDx[i-1] = 0;
3566 lpDx[i] = 0;
3567 i++;
3568 lpDx[i] = char_width;
3569 } else if (i+1 < len && IS_SURROGATE_PAIR(text[i],text[i+1])) {
3570 lpDx[i] = 0;
3571 i++;
3572 lpDx[i] = char_width;
3573 } else if (IS_LOW_VARSEL(text[i])) {
3574 if (i > 0) lpDx[i-1] = 0;
3575 lpDx[i] = char_width;
3576 }
3577 }
798a50bf 3578 }
3579
14ce9887 3580 /* We're using a private area for direct to font. (512 chars.) */
3581 if (ucsdata.dbcs_screenfont && (text[0] & CSET_MASK) == CSET_ACP) {
3582 /* Ho Hum, dbcs fonts are a PITA! */
3583 /* To display on W9x I have to convert to UCS */
3584 static wchar_t *uni_buf = 0;
3585 static int uni_len = 0;
3586 int nlen, mptr;
3587 if (len > uni_len) {
3588 sfree(uni_buf);
3589 uni_len = len;
3590 uni_buf = snewn(uni_len, wchar_t);
3591 }
36566009 3592
14ce9887 3593 for(nlen = mptr = 0; mptr<len; mptr++) {
3594 uni_buf[nlen] = 0xFFFD;
3595 if (IsDBCSLeadByteEx(ucsdata.font_codepage,
3596 (BYTE) text[mptr])) {
3597 char dbcstext[2];
3598 dbcstext[0] = text[mptr] & 0xFF;
3599 dbcstext[1] = text[mptr+1] & 0xFF;
798a50bf 3600 lpDx[nlen] += char_width;
14ce9887 3601 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
3602 dbcstext, 2, uni_buf+nlen, 1);
3603 mptr++;
3604 }
3605 else
3606 {
3607 char dbcstext[1];
3608 dbcstext[0] = text[mptr] & 0xFF;
3609 MultiByteToWideChar(ucsdata.font_codepage, MB_USEGLYPHCHARS,
3610 dbcstext, 1, uni_buf+nlen, 1);
3611 }
3612 nlen++;
3613 }
3614 if (nlen <= 0)
3615 return; /* Eeek! */
3616
3617 ExtTextOutW(hdc, x + xoffset,
3618 y - font_height * (lattr == LATTR_BOT) + text_adjust,
3619 ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
3620 &line_box, uni_buf, nlen,
798a50bf 3621 lpDx_maybe);
863c5362 3622 if (bold_font_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
14ce9887 3623 SetBkMode(hdc, TRANSPARENT);
3624 ExtTextOutW(hdc, x + xoffset - 1,
3625 y - font_height * (lattr ==
3626 LATTR_BOT) + text_adjust,
798a50bf 3627 ETO_CLIPPED, &line_box, uni_buf, nlen, lpDx_maybe);
14ce9887 3628 }
36566009 3629
798a50bf 3630 lpDx[0] = -1;
14ce9887 3631 } else if (DIRECT_FONT(text[0])) {
3632 static char *directbuf = NULL;
3633 static int directlen = 0;
3634 int i;
3635 if (len > directlen) {
3636 directlen = len;
3637 directbuf = sresize(directbuf, directlen, char);
3638 }
4eeb7d09 3639
14ce9887 3640 for (i = 0; i < len; i++)
3641 directbuf[i] = text[i] & 0xFF;
3642
3643 ExtTextOut(hdc, x + xoffset,
3644 y - font_height * (lattr == LATTR_BOT) + text_adjust,
3645 ETO_CLIPPED | (opaque ? ETO_OPAQUE : 0),
798a50bf 3646 &line_box, directbuf, len, lpDx_maybe);
863c5362 3647 if (bold_font_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
14ce9887 3648 SetBkMode(hdc, TRANSPARENT);
3649
3650 /* GRR: This draws the character outside its box and
3651 * can leave 'droppings' even with the clip box! I
3652 * suppose I could loop it one character at a time ...
3653 * yuk.
3654 *
3655 * Or ... I could do a test print with "W", and use +1
3656 * or -1 for this shift depending on if the leftmost
3657 * column is blank...
3658 */
3659 ExtTextOut(hdc, x + xoffset - 1,
3660 y - font_height * (lattr ==
3661 LATTR_BOT) + text_adjust,
798a50bf 3662 ETO_CLIPPED, &line_box, directbuf, len, lpDx_maybe);
14ce9887 3663 }
3664 } else {
3665 /* And 'normal' unicode characters */
3666 static WCHAR *wbuf = NULL;
3667 static int wlen = 0;
3668 int i;
3669
3670 if (wlen < len) {
3671 sfree(wbuf);
3672 wlen = len;
3673 wbuf = snewn(wlen, WCHAR);
3674 }
c6958dfe 3675
14ce9887 3676 for (i = 0; i < len; i++)
3677 wbuf[i] = text[i];
3678
3679 /* print Glyphs as they are, without Windows' Shaping*/
3680 general_textout(hdc, x + xoffset,
3681 y - font_height * (lattr==LATTR_BOT) + text_adjust,
798a50bf 3682 &line_box, wbuf, len, lpDx,
14ce9887 3683 opaque && !(attr & TATTR_COMBINING));
3684
3685 /* And the shadow bold hack. */
863c5362 3686 if (bold_font_mode == BOLD_SHADOW && (attr & ATTR_BOLD)) {
14ce9887 3687 SetBkMode(hdc, TRANSPARENT);
3688 ExtTextOutW(hdc, x + xoffset - 1,
3689 y - font_height * (lattr ==
3690 LATTR_BOT) + text_adjust,
798a50bf 3691 ETO_CLIPPED, &line_box, wbuf, len, lpDx_maybe);
14ce9887 3692 }
3693 }
c6958dfe 3694
14ce9887 3695 /*
3696 * If we're looping round again, stop erasing the background
3697 * rectangle.
3698 */
3699 SetBkMode(hdc, TRANSPARENT);
3700 opaque = FALSE;
374330e2 3701 }
4eeb7d09 3702 if (lattr != LATTR_TOP && (force_manual_underline ||
3703 (und_mode == UND_LINE
3704 && (attr & ATTR_UNDER)))) {
32874aea 3705 HPEN oldpen;
4eeb7d09 3706 int dec = descent;
3707 if (lattr == LATTR_BOT)
3708 dec = dec * 2 - font_height;
3709
32874aea 3710 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, fg));
ebe8092c 3711 MoveToEx(hdc, line_box.left, line_box.top + dec, NULL);
3712 LineTo(hdc, line_box.right, line_box.top + dec);
32874aea 3713 oldpen = SelectObject(hdc, oldpen);
3714 DeleteObject(oldpen);
374330e2 3715 }
4eeb7d09 3716}
3717
c6958dfe 3718/*
3719 * Wrapper that handles combining characters.
3720 */
3721void do_text(Context ctx, int x, int y, wchar_t *text, int len,
3722 unsigned long attr, int lattr)
3723{
3724 if (attr & TATTR_COMBINING) {
3725 unsigned long a = 0;
b03db5b6 3726 int len0 = 1;
3727 /* don't divide SURROGATE PAIR and VARIATION SELECTOR */
3728 if (len >= 2 && IS_SURROGATE_PAIR(text[0], text[1]))
3729 len0 = 2;
3730 if (len-len0 >= 1 && IS_LOW_VARSEL(text[len0])) {
3731 attr &= ~TATTR_COMBINING;
3732 do_text_internal(ctx, x, y, text, len0+1, attr, lattr);
3733 text += len0+1;
3734 len -= len0+1;
3735 a = TATTR_COMBINING;
3736 } else if (len-len0 >= 2 && IS_HIGH_VARSEL(text[len0], text[len0+1])) {
3737 attr &= ~TATTR_COMBINING;
3738 do_text_internal(ctx, x, y, text, len0+2, attr, lattr);
3739 text += len0+2;
3740 len -= len0+2;
3741 a = TATTR_COMBINING;
3742 } else {
3743 attr &= ~TATTR_COMBINING;
3744 }
3745
c6958dfe 3746 while (len--) {
b03db5b6 3747 if (len >= 1 && IS_SURROGATE_PAIR(text[0], text[1])) {
3748 do_text_internal(ctx, x, y, text, 2, attr | a, lattr);
3749 len--;
3750 text++;
3751 } else {
3752 do_text_internal(ctx, x, y, text, 1, attr | a, lattr);
3753 }
3754
c6958dfe 3755 text++;
3756 a = TATTR_COMBINING;
3757 }
3758 } else
3759 do_text_internal(ctx, x, y, text, len, attr, lattr);
3760}
3761
36566009 3762void do_cursor(Context ctx, int x, int y, wchar_t *text, int len,
4eeb7d09 3763 unsigned long attr, int lattr)
3764{
3765
3766 int fnt_width;
3767 int char_width;
3768 HDC hdc = ctx;
4a693cfc 3769 int ctype = cursor_type;
4eeb7d09 3770
8af95aa3 3771 lattr &= LATTR_MODE;
3772
887035a5 3773 if ((attr & TATTR_ACTCURS) && (ctype == 0 || term->big_cursor)) {
36566009 3774 if (*text != UCSWIDE) {
4eeb7d09 3775 do_text(ctx, x, y, text, len, attr, lattr);
3776 return;
3777 }
3778 ctype = 2;
3779 attr |= TATTR_RIGHTCURS;
3780 }
3781
3782 fnt_width = char_width = font_width * (1 + (lattr != LATTR_NORM));
3783 if (attr & ATTR_WIDE)
3784 char_width *= 2;
3785 x *= fnt_width;
3786 y *= font_height;
5a73255e 3787 x += offset_width;
3788 y += offset_height;
4eeb7d09 3789
887035a5 3790 if ((attr & TATTR_PASCURS) && (ctype == 0 || term->big_cursor)) {
374330e2 3791 POINT pts[5];
32874aea 3792 HPEN oldpen;
374330e2 3793 pts[0].x = pts[1].x = pts[4].x = x;
4eeb7d09 3794 pts[2].x = pts[3].x = x + char_width - 1;
374330e2 3795 pts[0].y = pts[3].y = pts[4].y = y;
32874aea 3796 pts[1].y = pts[2].y = y + font_height - 1;
cecb13f6 3797 oldpen = SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[261]));
32874aea 3798 Polyline(hdc, pts, 5);
3799 oldpen = SelectObject(hdc, oldpen);
3800 DeleteObject(oldpen);
4eeb7d09 3801 } else if ((attr & (TATTR_ACTCURS | TATTR_PASCURS)) && ctype != 0) {
32874aea 3802 int startx, starty, dx, dy, length, i;
4eeb7d09 3803 if (ctype == 1) {
32874aea 3804 startx = x;
3805 starty = y + descent;
3806 dx = 1;
3807 dy = 0;
4eeb7d09 3808 length = char_width;
32874aea 3809 } else {
4e30ff69 3810 int xadjust = 0;
4eeb7d09 3811 if (attr & TATTR_RIGHTCURS)
3812 xadjust = char_width - 1;
32874aea 3813 startx = x + xadjust;
3814 starty = y;
3815 dx = 0;
3816 dy = 1;
3817 length = font_height;
3818 }
4eeb7d09 3819 if (attr & TATTR_ACTCURS) {
32874aea 3820 HPEN oldpen;
3821 oldpen =
fdc94dd1 3822 SelectObject(hdc, CreatePen(PS_SOLID, 0, colours[261]));
32874aea 3823 MoveToEx(hdc, startx, starty, NULL);
3824 LineTo(hdc, startx + dx * length, starty + dy * length);
3825 oldpen = SelectObject(hdc, oldpen);
3826 DeleteObject(oldpen);
3827 } else {
3828 for (i = 0; i < length; i++) {
3829 if (i % 2 == 0) {
fdc94dd1 3830 SetPixel(hdc, startx, starty, colours[261]);
32874aea 3831 }
3832 startx += dx;
3833 starty += dy;
3834 }
3835 }
4e30ff69 3836 }
374330e2 3837}
3838
5a73255e 3839/* This function gets the actual width of a character in the normal font.
3840 */
2102eb8a 3841int char_width(Context ctx, int uc) {
5a73255e 3842 HDC hdc = ctx;
3843 int ibuf = 0;
3844
3845 /* If the font max is the same as the font ave width then this
3846 * function is a no-op.
3847 */
3848 if (!font_dualwidth) return 1;
3849
3850 switch (uc & CSET_MASK) {
36566009 3851 case CSET_ASCII:
21d2b241 3852 uc = ucsdata.unitab_line[uc & 0xFF];
5a73255e 3853 break;
36566009 3854 case CSET_LINEDRW:
21d2b241 3855 uc = ucsdata.unitab_xterm[uc & 0xFF];
5a73255e 3856 break;
36566009 3857 case CSET_SCOACS:
21d2b241 3858 uc = ucsdata.unitab_scoacs[uc & 0xFF];
5a73255e 3859 break;
3860 }
3861 if (DIRECT_FONT(uc)) {
21d2b241 3862 if (ucsdata.dbcs_screenfont) return 1;
5a73255e 3863
3864 /* Speedup, I know of no font where ascii is the wrong width */
36566009 3865 if ((uc&~CSET_MASK) >= ' ' && (uc&~CSET_MASK)<= '~')
5a73255e 3866 return 1;
3867
36566009 3868 if ( (uc & CSET_MASK) == CSET_ACP ) {
5a73255e 3869 SelectObject(hdc, fonts[FONT_NORMAL]);
36566009 3870 } else if ( (uc & CSET_MASK) == CSET_OEMCP ) {
5a73255e 3871 another_font(FONT_OEM);
3872 if (!fonts[FONT_OEM]) return 0;
3873
3874 SelectObject(hdc, fonts[FONT_OEM]);
3875 } else
3876 return 0;
3877
36566009 3878 if ( GetCharWidth32(hdc, uc&~CSET_MASK, uc&~CSET_MASK, &ibuf) != 1 &&
3879 GetCharWidth(hdc, uc&~CSET_MASK, uc&~CSET_MASK, &ibuf) != 1)
5a73255e 3880 return 0;
3881 } else {
3882 /* Speedup, I know of no font where ascii is the wrong width */
3883 if (uc >= ' ' && uc <= '~') return 1;
3884
3885 SelectObject(hdc, fonts[FONT_NORMAL]);
3886 if ( GetCharWidth32W(hdc, uc, uc, &ibuf) == 1 )
3887 /* Okay that one worked */ ;
3888 else if ( GetCharWidthW(hdc, uc, uc, &ibuf) == 1 )
3889 /* This should work on 9x too, but it's "less accurate" */ ;
3890 else
3891 return 0;
3892 }
3893
3894 ibuf += font_width / 2 -1;
3895 ibuf /= font_width;
3896
3897 return ibuf;
3898}
3899
374330e2 3900/*
c9def1b8 3901 * Translate a WM_(SYS)?KEY(UP|DOWN) message into a string of ASCII
58c840dc 3902 * codes. Returns number of bytes used, zero to drop the message,
3903 * -1 to forward the message to Windows, or another negative number
3904 * to indicate a NUL-terminated "special" string.
374330e2 3905 */
3cf144db 3906static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
32874aea 3907 unsigned char *output)
3908{
374330e2 3909 BYTE keystate[256];
32874aea 3910 int scan, left_alt = 0, key_down, shift_state;
3911 int r, i, code;
3912 unsigned char *p = output;
4eeb7d09 3913 static int alt_sum = 0;
4a693cfc 3914 int funky_type = conf_get_int(conf, CONF_funky_type);
3915 int no_applic_k = conf_get_int(conf, CONF_no_applic_k);
3916 int ctrlaltkeys = conf_get_int(conf, CONF_ctrlaltkeys);
3917 int nethack_keypad = conf_get_int(conf, CONF_nethack_keypad);
374330e2 3918
00e3ba0f 3919 HKL kbd_layout = GetKeyboardLayout(0);
3920
96af276d 3921 static wchar_t keys_unicode[3];
0c50ef57 3922 static int compose_char = 0;
4a693cfc 3923 static WPARAM compose_keycode = 0;
32874aea 3924
c9def1b8 3925 r = GetKeyboardState(keystate);
32874aea 3926 if (!r)
3927 memset(keystate, 0, sizeof(keystate));
3928 else {
ec55b220 3929#if 0
4eeb7d09 3930#define SHOW_TOASCII_RESULT
32874aea 3931 { /* Tell us all about key events */
3932 static BYTE oldstate[256];
3933 static int first = 1;
3934 static int scan;
3935 int ch;
3936 if (first)
3937 memcpy(oldstate, keystate, sizeof(oldstate));
3938 first = 0;
3939
3940 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT) {
3941 debug(("+"));
3942 } else if ((HIWORD(lParam) & KF_UP)
3943 && scan == (HIWORD(lParam) & 0xFF)) {
3944 debug((". U"));
3945 } else {
3946 debug((".\n"));
3947 if (wParam >= VK_F1 && wParam <= VK_F20)
3948 debug(("K_F%d", wParam + 1 - VK_F1));
3949 else
3950 switch (wParam) {
3951 case VK_SHIFT:
3952 debug(("SHIFT"));
3953 break;
3954 case VK_CONTROL:
3955 debug(("CTRL"));
3956 break;
3957 case VK_MENU:
3958 debug(("ALT"));
3959 break;
3960 default:
3961 debug(("VK_%02x", wParam));
3962 }
3963 if (message == WM_SYSKEYDOWN || message == WM_SYSKEYUP)
3964 debug(("*"));
3965 debug((", S%02x", scan = (HIWORD(lParam) & 0xFF)));
3966
3967 ch = MapVirtualKeyEx(wParam, 2, kbd_layout);
3968 if (ch >= ' ' && ch <= '~')
3969 debug((", '%c'", ch));
3970 else if (ch)
3971 debug((", $%02x", ch));
3972
96af276d 3973 if (keys_unicode[0])
3974 debug((", KB0=%04x", keys_unicode[0]));
3975 if (keys_unicode[1])
3976 debug((", KB1=%04x", keys_unicode[1]));
3977 if (keys_unicode[2])
3978 debug((", KB2=%04x", keys_unicode[2]));
32874aea 3979
3980 if ((keystate[VK_SHIFT] & 0x80) != 0)
3981 debug((", S"));
3982 if ((keystate[VK_CONTROL] & 0x80) != 0)
3983 debug((", C"));
3984 if ((HIWORD(lParam) & KF_EXTENDED))
3985 debug((", E"));
3986 if ((HIWORD(lParam) & KF_UP))
3987 debug((", U"));
3988 }
3989
3990 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT);
3991 else if ((HIWORD(lParam) & KF_UP))
3992 oldstate[wParam & 0xFF] ^= 0x80;
3993 else
3994 oldstate[wParam & 0xFF] ^= 0x81;
3995
3996 for (ch = 0; ch < 256; ch++)
3997 if (oldstate[ch] != keystate[ch])
3998 debug((", M%02x=%02x", ch, keystate[ch]));
3999
4000 memcpy(oldstate, keystate, sizeof(oldstate));
4001 }
ec55b220 4002#endif
4003
32874aea 4004 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED)) {
2cba1186 4005 keystate[VK_RMENU] = keystate[VK_MENU];
4006 }
4007
c9def1b8 4008
4009 /* Nastyness with NUMLock - Shift-NUMLock is left alone though */
4a693cfc 4010 if ((funky_type == FUNKY_VT400 ||
4011 (funky_type <= FUNKY_LINUX && term->app_keypad_keys &&
4012 !no_applic_k))
32874aea 4013 && wParam == VK_NUMLOCK && !(keystate[VK_SHIFT] & 0x80)) {
c9def1b8 4014
4015 wParam = VK_EXECUTE;
4016
4017 /* UnToggle NUMLock */
32874aea 4018 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0)
4019 keystate[VK_NUMLOCK] ^= 1;
c9def1b8 4020 }
4021
4022 /* And write back the 'adjusted' state */
32874aea 4023 SetKeyboardState(keystate);
c9def1b8 4024 }
4025
4026 /* Disable Auto repeat if required */
887035a5 4027 if (term->repeat_off &&
4028 (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == KF_REPEAT)
32874aea 4029 return 0;
c9def1b8 4030
32874aea 4031 if ((HIWORD(lParam) & KF_ALTDOWN) && (keystate[VK_RMENU] & 0x80) == 0)
c9def1b8 4032 left_alt = 1;
4033
32874aea 4034 key_down = ((HIWORD(lParam) & KF_UP) == 0);
c9def1b8 4035
95bbe1ae 4036 /* Make sure Ctrl-ALT is not the same as AltGr for ToAscii unless told. */
32874aea 4037 if (left_alt && (keystate[VK_CONTROL] & 0x80)) {
4a693cfc 4038 if (ctrlaltkeys)
95bbe1ae 4039 keystate[VK_MENU] = 0;
4040 else {
4041 keystate[VK_RMENU] = 0x80;
4042 left_alt = 0;
4043 }
4044 }
c9def1b8 4045
4046 scan = (HIWORD(lParam) & (KF_UP | KF_EXTENDED | 0xFF));
32874aea 4047 shift_state = ((keystate[VK_SHIFT] & 0x80) != 0)
4048 + ((keystate[VK_CONTROL] & 0x80) != 0) * 2;
374330e2 4049
95bbe1ae 4050 /* Note if AltGr was pressed and if it was used as a compose key */
4051 if (!compose_state) {
4a693cfc 4052 compose_keycode = 0x100;
4053 if (conf_get_int(conf, CONF_compose_key)) {
32874aea 4054 if (wParam == VK_MENU && (HIWORD(lParam) & KF_EXTENDED))
4a693cfc 4055 compose_keycode = wParam;
95bbe1ae 4056 }
4057 if (wParam == VK_APPS)
4a693cfc 4058 compose_keycode = wParam;
95bbe1ae 4059 }
4060
4a693cfc 4061 if (wParam == compose_keycode) {
32874aea 4062 if (compose_state == 0
4063 && (HIWORD(lParam) & (KF_UP | KF_REPEAT)) == 0) compose_state =
4064 1;
4065 else if (compose_state == 1 && (HIWORD(lParam) & KF_UP))
95bbe1ae 4066 compose_state = 2;
4067 else
4068 compose_state = 0;
32874aea 4069 } else if (compose_state == 1 && wParam != VK_CONTROL)
95bbe1ae 4070 compose_state = 0;
4071
32874aea 4072 if (compose_state > 1 && left_alt)
4073 compose_state = 0;
67c339f7 4074
c9def1b8 4075 /* Sanitize the number pad if not using a PC NumPad */
4a693cfc 4076 if (left_alt || (term->app_keypad_keys && !no_applic_k
4077 && funky_type != FUNKY_XTERM)
4078 || funky_type == FUNKY_VT400 || nethack_keypad || compose_state) {
32874aea 4079 if ((HIWORD(lParam) & KF_EXTENDED) == 0) {
c9def1b8 4080 int nParam = 0;
32874aea 4081 switch (wParam) {
4082 case VK_INSERT:
4083 nParam = VK_NUMPAD0;
4084 break;
4085 case VK_END:
4086 nParam = VK_NUMPAD1;
4087 break;
4088 case VK_DOWN:
4089 nParam = VK_NUMPAD2;
4090 break;
4091 case VK_NEXT:
4092 nParam = VK_NUMPAD3;
4093 break;
4094 case VK_LEFT:
4095 nParam = VK_NUMPAD4;
4096 break;
4097 case VK_CLEAR:
4098 nParam = VK_NUMPAD5;
4099 break;
4100 case VK_RIGHT:
4101 nParam = VK_NUMPAD6;
4102 break;
4103 case VK_HOME:
4104 nParam = VK_NUMPAD7;
4105 break;
4106 case VK_UP:
4107 nParam = VK_NUMPAD8;
4108 break;
4109 case VK_PRIOR:
4110 nParam = VK_NUMPAD9;
4111 break;
4112 case VK_DELETE:
4113 nParam = VK_DECIMAL;
4114 break;
c9def1b8 4115 }
32874aea 4116 if (nParam) {
4117 if (keystate[VK_NUMLOCK] & 1)
4118 shift_state |= 1;
c9def1b8 4119 wParam = nParam;
4120 }
25d39ef6 4121 }
4122 }
4123
c9def1b8 4124 /* If a key is pressed and AltGr is not active */
32874aea 4125 if (key_down && (keystate[VK_RMENU] & 0x80) == 0 && !compose_state) {
4126 /* Okay, prepare for most alts then ... */
4127 if (left_alt)
4128 *p++ = '\033';
374330e2 4129
c9def1b8 4130 /* Lets see if it's a pattern we know all about ... */
4131 if (wParam == VK_PRIOR && shift_state == 1) {
32874aea 4132 SendMessage(hwnd, WM_VSCROLL, SB_PAGEUP, 0);
4133 return 0;
c9def1b8 4134 }
153580da 4135 if (wParam == VK_PRIOR && shift_state == 2) {
4136 SendMessage(hwnd, WM_VSCROLL, SB_LINEUP, 0);
4137 return 0;
4138 }
c9def1b8 4139 if (wParam == VK_NEXT && shift_state == 1) {
32874aea 4140 SendMessage(hwnd, WM_VSCROLL, SB_PAGEDOWN, 0);
4141 return 0;
4142 }
153580da 4143 if (wParam == VK_NEXT && shift_state == 2) {
4144 SendMessage(hwnd, WM_VSCROLL, SB_LINEDOWN, 0);
4145 return 0;
4146 }
cf6ddb95 4147 if ((wParam == VK_PRIOR || wParam == VK_NEXT) && shift_state == 3) {
4148 term_scroll_to_selection(term, (wParam == VK_PRIOR ? 0 : 1));
4149 return 0;
4150 }
32874aea 4151 if (wParam == VK_INSERT && shift_state == 1) {
e9609ce5 4152 request_paste(NULL);
32874aea 4153 return 0;
4154 }
4a693cfc 4155 if (left_alt && wParam == VK_F4 && conf_get_int(conf, CONF_alt_f4)) {
32874aea 4156 return -1;
c9def1b8 4157 }
4a693cfc 4158 if (left_alt && wParam == VK_SPACE && conf_get_int(conf,
4159 CONF_alt_space)) {
32874aea 4160 SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, 0);
4161 return -1;
c9def1b8 4162 }
4a693cfc 4163 if (left_alt && wParam == VK_RETURN &&
4164 conf_get_int(conf, CONF_fullscreenonaltenter) &&
4165 (conf_get_int(conf, CONF_resize_action) != RESIZE_DISABLED)) {
0ed2f48e 4166 if ((HIWORD(lParam) & (KF_UP | KF_REPEAT)) != KF_REPEAT)
4167 flip_full_screen();
8f57d753 4168 return -1;
4169 }
ec55b220 4170 /* Control-Numlock for app-keypad mode switch */
4171 if (wParam == VK_PAUSE && shift_state == 2) {
887035a5 4172 term->app_keypad_keys ^= 1;
ec55b220 4173 return 0;
4174 }
374330e2 4175
c9def1b8 4176 /* Nethack keypad */
4a693cfc 4177 if (nethack_keypad && !left_alt) {
32874aea 4178 switch (wParam) {
4179 case VK_NUMPAD1:
6e67448b 4180 *p++ = "bB\002\002"[shift_state & 3];
32874aea 4181 return p - output;
4182 case VK_NUMPAD2:
6e67448b 4183 *p++ = "jJ\012\012"[shift_state & 3];
32874aea 4184 return p - output;
4185 case VK_NUMPAD3:
6e67448b 4186 *p++ = "nN\016\016"[shift_state & 3];
32874aea 4187 return p - output;
4188 case VK_NUMPAD4:
6e67448b 4189 *p++ = "hH\010\010"[shift_state & 3];
32874aea 4190 return p - output;
4191 case VK_NUMPAD5:
4192 *p++ = shift_state ? '.' : '.';
4193 return p - output;
4194 case VK_NUMPAD6:
6e67448b 4195 *p++ = "lL\014\014"[shift_state & 3];
32874aea 4196 return p - output;
4197 case VK_NUMPAD7:
6e67448b 4198 *p++ = "yY\031\031"[shift_state & 3];
32874aea 4199 return p - output;
4200 case VK_NUMPAD8:
6e67448b 4201 *p++ = "kK\013\013"[shift_state & 3];
32874aea 4202 return p - output;
4203 case VK_NUMPAD9:
6e67448b 4204 *p++ = "uU\025\025"[shift_state & 3];
32874aea 4205 return p - output;
4206 }
c9def1b8 4207 }
4208
4209 /* Application Keypad */
4210 if (!left_alt) {
32874aea 4211 int xkey = 0;
4212
4a693cfc 4213 if (funky_type == FUNKY_VT400 ||
4214 (funky_type <= FUNKY_LINUX &&
4215 term->app_keypad_keys && !no_applic_k)) switch (wParam) {
32874aea 4216 case VK_EXECUTE:
4217 xkey = 'P';
4218 break;
4219 case VK_DIVIDE:
4220 xkey = 'Q';
4221 break;
4222 case VK_MULTIPLY:
4223 xkey = 'R';
4224 break;
4225 case VK_SUBTRACT:
4226 xkey = 'S';
4227 break;
4228 }
4a693cfc 4229 if (term->app_keypad_keys && !no_applic_k)
32874aea 4230 switch (wParam) {
4231 case VK_NUMPAD0:
4232 xkey = 'p';
4233 break;
4234 case VK_NUMPAD1:
4235 xkey = 'q';
4236 break;
4237 case VK_NUMPAD2:
4238 xkey = 'r';
4239 break;
4240 case VK_NUMPAD3:
4241 xkey = 's';
4242 break;
4243 case VK_NUMPAD4:
4244 xkey = 't';
4245 break;
4246 case VK_NUMPAD5:
4247 xkey = 'u';
4248 break;
4249 case VK_NUMPAD6:
4250 xkey = 'v';
4251 break;
4252 case VK_NUMPAD7:
4253 xkey = 'w';
4254 break;
4255 case VK_NUMPAD8:
4256 xkey = 'x';
4257 break;
4258 case VK_NUMPAD9:
4259 xkey = 'y';
4260 break;
4261
4262 case VK_DECIMAL:
4263 xkey = 'n';
4264 break;
4265 case VK_ADD:
4a693cfc 4266 if (funky_type == FUNKY_XTERM) {
32874aea 4267 if (shift_state)
4268 xkey = 'l';
4269 else
4270 xkey = 'k';
4271 } else if (shift_state)
4272 xkey = 'm';
c9def1b8 4273 else
32874aea 4274 xkey = 'l';
4275 break;
4276
4277 case VK_DIVIDE:
4a693cfc 4278 if (funky_type == FUNKY_XTERM)
32874aea 4279 xkey = 'o';
4280 break;
4281 case VK_MULTIPLY:
4a693cfc 4282 if (funky_type == FUNKY_XTERM)
32874aea 4283 xkey = 'j';
4284 break;
4285 case VK_SUBTRACT:
4a693cfc 4286 if (funky_type == FUNKY_XTERM)
32874aea 4287 xkey = 'm';
4288 break;
4289
4290 case VK_RETURN:
4291 if (HIWORD(lParam) & KF_EXTENDED)
4292 xkey = 'M';
4293 break;
c9def1b8 4294 }
32874aea 4295 if (xkey) {
887035a5 4296 if (term->vt52_mode) {
32874aea 4297 if (xkey >= 'P' && xkey <= 'S')
4298 p += sprintf((char *) p, "\x1B%c", xkey);
4299 else
4300 p += sprintf((char *) p, "\x1B?%c", xkey);
4301 } else
4302 p += sprintf((char *) p, "\x1BO%c", xkey);
4303 return p - output;
c9def1b8 4304 }
4305 }
4306
32874aea 4307 if (wParam == VK_BACK && shift_state == 0) { /* Backspace */
4a693cfc 4308 *p++ = (conf_get_int(conf, CONF_bksp_is_delete) ? 0x7F : 0x08);
a5f3e637 4309 *p++ = 0;
4310 return -2;
c9def1b8 4311 }
e8e8d6e2 4312 if (wParam == VK_BACK && shift_state == 1) { /* Shift Backspace */
4313 /* We do the opposite of what is configured */
4a693cfc 4314 *p++ = (conf_get_int(conf, CONF_bksp_is_delete) ? 0x08 : 0x7F);
e8e8d6e2 4315 *p++ = 0;
4316 return -2;
4317 }
32874aea 4318 if (wParam == VK_TAB && shift_state == 1) { /* Shift tab */
4319 *p++ = 0x1B;
4320 *p++ = '[';
4321 *p++ = 'Z';
4322 return p - output;
c9def1b8 4323 }
32874aea 4324 if (wParam == VK_SPACE && shift_state == 2) { /* Ctrl-Space */
4325 *p++ = 0;
4326 return p - output;
c9def1b8 4327 }
32874aea 4328 if (wParam == VK_SPACE && shift_state == 3) { /* Ctrl-Shift-Space */
4329 *p++ = 160;
4330 return p - output;
c9def1b8 4331 }
32874aea 4332 if (wParam == VK_CANCEL && shift_state == 2) { /* Ctrl-Break */
58c840dc 4333 if (back)
4334 back->special(backhandle, TS_BRK);
4335 return 0;
c9def1b8 4336 }
32874aea 4337 if (wParam == VK_PAUSE) { /* Break/Pause */
4338 *p++ = 26;
4339 *p++ = 0;
4340 return -2;
95bbe1ae 4341 }
c9def1b8 4342 /* Control-2 to Control-8 are special */
32874aea 4343 if (shift_state == 2 && wParam >= '2' && wParam <= '8') {
4344 *p++ = "\000\033\034\035\036\037\177"[wParam - '2'];
c9def1b8 4345 return p - output;
4346 }
237f2b6e 4347 if (shift_state == 2 && (wParam == 0xBD || wParam == 0xBF)) {
c9def1b8 4348 *p++ = 0x1F;
4349 return p - output;
4350 }
e414a330 4351 if (shift_state == 2 && (wParam == 0xDF || wParam == 0xDC)) {
c9def1b8 4352 *p++ = 0x1C;
4353 return p - output;
4354 }
9aa461e4 4355 if (shift_state == 3 && wParam == 0xDE) {
4356 *p++ = 0x1E; /* Ctrl-~ == Ctrl-^ in xterm at least */
4357 return p - output;
4358 }
887035a5 4359 if (shift_state == 0 && wParam == VK_RETURN && term->cr_lf_return) {
32874aea 4360 *p++ = '\r';
4361 *p++ = '\n';
c9def1b8 4362 return p - output;
4363 }
374330e2 4364
c5e9c988 4365 /*
c9def1b8 4366 * Next, all the keys that do tilde codes. (ESC '[' nn '~',
4367 * for integer decimal nn.)
4368 *
4369 * We also deal with the weird ones here. Linux VCs replace F1
4370 * to F5 by ESC [ [ A to ESC [ [ E. rxvt doesn't do _that_, but
4371 * does replace Home and End (1~ and 4~) by ESC [ H and ESC O w
4372 * respectively.
c5e9c988 4373 */
c9def1b8 4374 code = 0;
4375 switch (wParam) {
32874aea 4376 case VK_F1:
4377 code = (keystate[VK_SHIFT] & 0x80 ? 23 : 11);
4378 break;
4379 case VK_F2:
4380 code = (keystate[VK_SHIFT] & 0x80 ? 24 : 12);
4381 break;
4382 case VK_F3:
4383 code = (keystate[VK_SHIFT] & 0x80 ? 25 : 13);
4384 break;
4385 case VK_F4:
4386 code = (keystate[VK_SHIFT] & 0x80 ? 26 : 14);
4387 break;
4388 case VK_F5:
4389 code = (keystate[VK_SHIFT] & 0x80 ? 28 : 15);
4390 break;
4391 case VK_F6:
4392 code = (keystate[VK_SHIFT] & 0x80 ? 29 : 17);
4393 break;
4394 case VK_F7:
4395 code = (keystate[VK_SHIFT] & 0x80 ? 31 : 18);
4396 break;
4397 case VK_F8:
4398 code = (keystate[VK_SHIFT] & 0x80 ? 32 : 19);
4399 break;
4400 case VK_F9:
4401 code = (keystate[VK_SHIFT] & 0x80 ? 33 : 20);
4402 break;
4403 case VK_F10:
4404 code = (keystate[VK_SHIFT] & 0x80 ? 34 : 21);
4405 break;
4406 case VK_F11:
4407 code = 23;
4408 break;
4409 case VK_F12:
4410 code = 24;
4411 break;
4412 case VK_F13:
4413 code = 25;
4414 break;
4415 case VK_F14:
4416 code = 26;
4417 break;
4418 case VK_F15:
4419 code = 28;
4420 break;
4421 case VK_F16:
4422 code = 29;
4423 break;
4424 case VK_F17:
4425 code = 31;
4426 break;
4427 case VK_F18:
4428 code = 32;
4429 break;
4430 case VK_F19:
4431 code = 33;
4432 break;
4433 case VK_F20:
4434 code = 34;
4435 break;
dfca2656 4436 }
4437 if ((shift_state&2) == 0) switch (wParam) {
32874aea 4438 case VK_HOME:
4439 code = 1;
4440 break;
4441 case VK_INSERT:
4442 code = 2;
4443 break;
4444 case VK_DELETE:
4445 code = 3;
4446 break;
4447 case VK_END:
4448 code = 4;
4449 break;
4450 case VK_PRIOR:
4451 code = 5;
4452 break;
4453 case VK_NEXT:
4454 code = 6;
4455 break;
374330e2 4456 }
ec55b220 4457 /* Reorder edit keys to physical order */
4a693cfc 4458 if (funky_type == FUNKY_VT400 && code <= 6)
32874aea 4459 code = "\0\2\1\4\5\3\6"[code];
ec55b220 4460
887035a5 4461 if (term->vt52_mode && code > 0 && code <= 6) {
32874aea 4462 p += sprintf((char *) p, "\x1B%c", " HLMEIG"[code]);
f37caa11 4463 return p - output;
4464 }
4465
4a693cfc 4466 if (funky_type == FUNKY_SCO && code >= 11 && code <= 34) {
4467 /* SCO function keys */
e24b1972 4468 char codes[] = "MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@[\\]^_`{";
4469 int index = 0;
4470 switch (wParam) {
4471 case VK_F1: index = 0; break;
4472 case VK_F2: index = 1; break;
4473 case VK_F3: index = 2; break;
4474 case VK_F4: index = 3; break;
4475 case VK_F5: index = 4; break;
4476 case VK_F6: index = 5; break;
4477 case VK_F7: index = 6; break;
4478 case VK_F8: index = 7; break;
4479 case VK_F9: index = 8; break;
4480 case VK_F10: index = 9; break;
4481 case VK_F11: index = 10; break;
4482 case VK_F12: index = 11; break;
4483 }
4484 if (keystate[VK_SHIFT] & 0x80) index += 12;
4485 if (keystate[VK_CONTROL] & 0x80) index += 24;
4486 p += sprintf((char *) p, "\x1B[%c", codes[index]);
f37caa11 4487 return p - output;
4488 }
4a693cfc 4489 if (funky_type == FUNKY_SCO && /* SCO small keypad */
9bc81a2c 4490 code >= 1 && code <= 6) {
4491 char codes[] = "HL.FIG";
4492 if (code == 3) {
4493 *p++ = '\x7F';
4494 } else {
4495 p += sprintf((char *) p, "\x1B[%c", codes[code-1]);
4496 }
4497 return p - output;
4498 }
4a693cfc 4499 if ((term->vt52_mode || funky_type == FUNKY_VT100P) && code >= 11 && code <= 24) {
f37caa11 4500 int offt = 0;
32874aea 4501 if (code > 15)
4502 offt++;
4503 if (code > 21)
4504 offt++;
887035a5 4505 if (term->vt52_mode)
32874aea 4506 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11 - offt);
f37caa11 4507 else
32874aea 4508 p +=
4509 sprintf((char *) p, "\x1BO%c", code + 'P' - 11 - offt);
f37caa11 4510 return p - output;
4511 }
4a693cfc 4512 if (funky_type == FUNKY_LINUX && code >= 11 && code <= 15) {
32874aea 4513 p += sprintf((char *) p, "\x1B[[%c", code + 'A' - 11);
c9def1b8 4514 return p - output;
4515 }
4a693cfc 4516 if (funky_type == FUNKY_XTERM && code >= 11 && code <= 14) {
887035a5 4517 if (term->vt52_mode)
32874aea 4518 p += sprintf((char *) p, "\x1B%c", code + 'P' - 11);
ec55b220 4519 else
32874aea 4520 p += sprintf((char *) p, "\x1BO%c", code + 'P' - 11);
c9def1b8 4521 return p - output;
4522 }
4a693cfc 4523 if ((code == 1 || code == 4) &&
4524 conf_get_int(conf, CONF_rxvt_homeend)) {
32874aea 4525 p += sprintf((char *) p, code == 1 ? "\x1B[H" : "\x1BOw");
c9def1b8 4526 return p - output;
4527 }
4528 if (code) {
32874aea 4529 p += sprintf((char *) p, "\x1B[%d~", code);
374330e2 4530 return p - output;
374330e2 4531 }
45dabbc5 4532
c9def1b8 4533 /*
4534 * Now the remaining keys (arrows and Keypad 5. Keypad 5 for
4535 * some reason seems to send VK_CLEAR to Windows...).
4536 */
4537 {
4538 char xkey = 0;
4539 switch (wParam) {
32874aea 4540 case VK_UP:
4541 xkey = 'A';
4542 break;
4543 case VK_DOWN:
4544 xkey = 'B';
4545 break;
4546 case VK_RIGHT:
4547 xkey = 'C';
4548 break;
4549 case VK_LEFT:
4550 xkey = 'D';
4551 break;
4552 case VK_CLEAR:
4553 xkey = 'G';
4554 break;
c9def1b8 4555 }
32874aea 4556 if (xkey) {
720f700e 4557 p += format_arrow_key(p, term, xkey, shift_state);
c9def1b8 4558 return p - output;
4559 }
4560 }
0c50ef57 4561
4562 /*
4563 * Finally, deal with Return ourselves. (Win95 seems to
4564 * foul it up when Alt is pressed, for some reason.)
4565 */
32874aea 4566 if (wParam == VK_RETURN) { /* Return */
0c50ef57 4567 *p++ = 0x0D;
a5f3e637 4568 *p++ = 0;
4569 return -2;
0c50ef57 4570 }
4eeb7d09 4571
4572 if (left_alt && wParam >= VK_NUMPAD0 && wParam <= VK_NUMPAD9)
4573 alt_sum = alt_sum * 10 + wParam - VK_NUMPAD0;
4574 else
4575 alt_sum = 0;
67c339f7 4576 }
374330e2 4577
c9def1b8 4578 /* Okay we've done everything interesting; let windows deal with
4579 * the boring stuff */
4580 {
a9c02454 4581 BOOL capsOn=0;
4582
4583 /* helg: clear CAPS LOCK state if caps lock switches to cyrillic */
4a693cfc 4584 if(keystate[VK_CAPITAL] != 0 &&
4585 conf_get_int(conf, CONF_xlat_capslockcyr)) {
a9c02454 4586 capsOn= !left_alt;
4587 keystate[VK_CAPITAL] = 0;
4588 }
4589
c264be08 4590 /* XXX how do we know what the max size of the keys array should
4591 * be is? There's indication on MS' website of an Inquire/InquireEx
4592 * functioning returning a KBINFO structure which tells us. */
4593 if (osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT) {
96af276d 4594 r = ToUnicodeEx(wParam, scan, keystate, keys_unicode,
4595 lenof(keys_unicode), 0, kbd_layout);
4596 } else {
c264be08 4597 /* XXX 'keys' parameter is declared in MSDN documentation as
4598 * 'LPWORD lpChar'.
4599 * The experience of a French user indicates that on
4600 * Win98, WORD[] should be passed in, but on Win2K, it should
4601 * be BYTE[]. German WinXP and my Win2K with "US International"
4602 * driver corroborate this.
4603 * Experimentally I've conditionalised the behaviour on the
4604 * Win9x/NT split, but I suspect it's worse than that.
4605 * See wishlist item `win-dead-keys' for more horrible detail
4606 * and speculations. */
c264be08 4607 int i;
96af276d 4608 static WORD keys[3];
4609 static BYTE keysb[3];
c264be08 4610 r = ToAsciiEx(wParam, scan, keystate, keys, 0, kbd_layout);
96af276d 4611 if (r > 0) {
4612 for (i = 0; i < r; i++) {
4613 keysb[i] = (BYTE)keys[i];
4614 }
4615 MultiByteToWideChar(CP_ACP, 0, (LPCSTR)keysb, r,
4616 keys_unicode, lenof(keys_unicode));
4617 }
c264be08 4618 }
4eeb7d09 4619#ifdef SHOW_TOASCII_RESULT
4620 if (r == 1 && !key_down) {
4621 if (alt_sum) {
21d2b241 4622 if (in_utf(term) || ucsdata.dbcs_screenfont)
4eeb7d09 4623 debug((", (U+%04x)", alt_sum));
4624 else
4625 debug((", LCH(%d)", alt_sum));
4626 } else {
96af276d 4627 debug((", ACH(%d)", keys_unicode[0]));
4eeb7d09 4628 }
4629 } else if (r > 0) {
4630 int r1;
4631 debug((", ASC("));
4632 for (r1 = 0; r1 < r; r1++) {
96af276d 4633 debug(("%s%d", r1 ? "," : "", keys_unicode[r1]));
4eeb7d09 4634 }
4635 debug((")"));
4636 }
4637#endif
32874aea 4638 if (r > 0) {
4eeb7d09 4639 WCHAR keybuf;
256cb87c 4640
4641 /*
4642 * Interrupt an ongoing paste. I'm not sure this is
4643 * sensible, but for the moment it's preferable to
4644 * having to faff about buffering things.
4645 */
887035a5 4646 term_nopaste(term);
256cb87c 4647
c9def1b8 4648 p = output;
32874aea 4649 for (i = 0; i < r; i++) {
96af276d 4650 wchar_t wch = keys_unicode[i];
14963b8f 4651
96af276d 4652 if (compose_state == 2 && wch >= ' ' && wch < 0x80) {
4653 compose_char = wch;
32874aea 4654 compose_state++;
c9def1b8 4655 continue;
4656 }
96af276d 4657 if (compose_state == 3 && wch >= ' ' && wch < 0x80) {
c9def1b8 4658 int nc;
4659 compose_state = 0;
4660
96af276d 4661 if ((nc = check_compose(compose_char, wch)) == -1) {
fe50e814 4662 MessageBeep(MB_ICONHAND);
c9def1b8 4663 return 0;
4664 }
4eeb7d09 4665 keybuf = nc;
887035a5 4666 term_seen_key_event(term);
e3be8de5 4667 if (ldisc)
4668 luni_send(ldisc, &keybuf, 1, 1);
4eeb7d09 4669 continue;
c9def1b8 4670 }
374330e2 4671
c9def1b8 4672 compose_state = 0;
374330e2 4673
4eeb7d09 4674 if (!key_down) {
4675 if (alt_sum) {
21d2b241 4676 if (in_utf(term) || ucsdata.dbcs_screenfont) {
4eeb7d09 4677 keybuf = alt_sum;
887035a5 4678 term_seen_key_event(term);
e3be8de5 4679 if (ldisc)
4680 luni_send(ldisc, &keybuf, 1, 1);
4eeb7d09 4681 } else {
96af276d 4682 char ch = (char) alt_sum;
5471d09a 4683 /*
4684 * We need not bother about stdin
4685 * backlogs here, because in GUI PuTTY
4686 * we can't do anything about it
4687 * anyway; there's no means of asking
4688 * Windows to hold off on KEYDOWN
4689 * messages. We _have_ to buffer
4690 * everything we're sent.
4691 */
887035a5 4692 term_seen_key_event(term);
e3be8de5 4693 if (ldisc)
4694 ldisc_send(ldisc, &ch, 1, 1);
4eeb7d09 4695 }
4696 alt_sum = 0;
405a0c29 4697 } else {
887035a5 4698 term_seen_key_event(term);
e3be8de5 4699 if (ldisc)
96af276d 4700 luni_send(ldisc, &wch, 1, 1);
405a0c29 4701 }
4eeb7d09 4702 } else {
96af276d 4703 if(capsOn && wch < 0x80) {
a9c02454 4704 WCHAR cbuf[2];
4705 cbuf[0] = 27;
96af276d 4706 cbuf[1] = xlat_uskbd2cyrllic(wch);
887035a5 4707 term_seen_key_event(term);
e3be8de5 4708 if (ldisc)
4709 luni_send(ldisc, cbuf+!left_alt, 1+!!left_alt, 1);
a9c02454 4710 } else {
96af276d 4711 WCHAR cbuf[2];
a9c02454 4712 cbuf[0] = '\033';
96af276d 4713 cbuf[1] = wch;
887035a5 4714 term_seen_key_event(term);
e3be8de5 4715 if (ldisc)
96af276d 4716 luni_send(ldisc, cbuf +!left_alt, 1+!!left_alt, 1);
a9c02454 4717 }
c9def1b8 4718 }
bca9517a 4719 show_mouseptr(0);
c9def1b8 4720 }
374330e2 4721
c9def1b8 4722 /* This is so the ALT-Numpad and dead keys work correctly. */
96af276d 4723 keys_unicode[0] = 0;
c9def1b8 4724
32874aea 4725 return p - output;
c9def1b8 4726 }
159eba53 4727 /* If we're definitly not building up an ALT-54321 then clear it */
32874aea 4728 if (!left_alt)
96af276d 4729 keys_unicode[0] = 0;
4eeb7d09 4730 /* If we will be using alt_sum fix the 256s */
96af276d 4731 else if (keys_unicode[0] && (in_utf(term) || ucsdata.dbcs_screenfont))
4732 keys_unicode[0] = 10;
374330e2 4733 }
4734
dfca2656 4735 /*
4736 * ALT alone may or may not want to bring up the System menu.
4737 * If it's not meant to, we return 0 on presses or releases of
4738 * ALT, to show that we've swallowed the keystroke. Otherwise
4739 * we return -1, which means Windows will give the keystroke
4740 * its default handling (i.e. bring up the System menu).
4741 */
4a693cfc 4742 if (wParam == VK_MENU && !conf_get_int(conf, CONF_alt_only))
dfca2656 4743 return 0;
374330e2 4744
c9def1b8 4745 return -1;
374330e2 4746}
4747
a8327734 4748void set_title(void *frontend, char *title)
32874aea 4749{
4750 sfree(window_name);
3d88e64d 4751 window_name = snewn(1 + strlen(title), char);
32874aea 4752 strcpy(window_name, title);
4a693cfc 4753 if (conf_get_int(conf, CONF_win_name_always) || !IsIconic(hwnd))
32874aea 4754 SetWindowText(hwnd, title);
374330e2 4755}
4756
a8327734 4757void set_icon(void *frontend, char *title)
32874aea 4758{
4759 sfree(icon_name);
3d88e64d 4760 icon_name = snewn(1 + strlen(title), char);
32874aea 4761 strcpy(icon_name, title);
4a693cfc 4762 if (!conf_get_int(conf, CONF_win_name_always) && IsIconic(hwnd))
32874aea 4763 SetWindowText(hwnd, title);
374330e2 4764}
4765
a8327734 4766void set_sbar(void *frontend, int total, int start, int page)
32874aea 4767{
374330e2 4768 SCROLLINFO si;
c9def1b8 4769
4a693cfc 4770 if (!conf_get_int(conf, is_full_screen() ?
4771 CONF_scrollbar_in_fullscreen : CONF_scrollbar))
32874aea 4772 return;
c9def1b8 4773
374330e2 4774 si.cbSize = sizeof(si);
c9def1b8 4775 si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
374330e2 4776 si.nMin = 0;
4777 si.nMax = total - 1;
4778 si.nPage = page;
4779 si.nPos = start;
c1f5f956 4780 if (hwnd)
32874aea 4781 SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
374330e2 4782}
4783
a8327734 4784Context get_ctx(void *frontend)
32874aea 4785{
374330e2 4786 HDC hdc;
4787 if (hwnd) {
32874aea 4788 hdc = GetDC(hwnd);
374330e2 4789 if (hdc && pal)
32874aea 4790 SelectPalette(hdc, pal, FALSE);
374330e2 4791 return hdc;
4792 } else
4793 return NULL;
4794}
4795
32874aea 4796void free_ctx(Context ctx)
4797{
4798 SelectPalette(ctx, GetStockObject(DEFAULT_PALETTE), FALSE);
4799 ReleaseDC(hwnd, ctx);
374330e2 4800}
4801
32874aea 4802static void real_palette_set(int n, int r, int g, int b)
4803{
374330e2 4804 if (pal) {
4805 logpal->palPalEntry[n].peRed = r;
4806 logpal->palPalEntry[n].peGreen = g;
4807 logpal->palPalEntry[n].peBlue = b;
4808 logpal->palPalEntry[n].peFlags = PC_NOCOLLAPSE;
4809 colours[n] = PALETTERGB(r, g, b);
cecb13f6 4810 SetPaletteEntries(pal, 0, NALLCOLOURS, logpal->palPalEntry);
374330e2 4811 } else
4812 colours[n] = RGB(r, g, b);
4813}
4814
a8327734 4815void palette_set(void *frontend, int n, int r, int g, int b)
32874aea 4816{
cecb13f6 4817 if (n >= 16)
4818 n += 256 - 16;
17524941 4819 if (n >= NALLCOLOURS)
cecb13f6 4820 return;
4821 real_palette_set(n, r, g, b);
374330e2 4822 if (pal) {
a8327734 4823 HDC hdc = get_ctx(frontend);
32874aea 4824 UnrealizeObject(pal);
4825 RealizePalette(hdc);
4826 free_ctx(hdc);
6092b4ee 4827 } else {
4828 if (n == (ATTR_DEFBG>>ATTR_BGSHIFT))
4829 /* If Default Background changes, we need to ensure any
4830 * space between the text area and the window border is
4831 * redrawn. */
4832 InvalidateRect(hwnd, NULL, TRUE);
374330e2 4833 }
4834}
4835
a8327734 4836void palette_reset(void *frontend)
32874aea 4837{
374330e2 4838 int i;
4839
cecb13f6 4840 /* And this */
4841 for (i = 0; i < NALLCOLOURS; i++) {
374330e2 4842 if (pal) {
4843 logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
4844 logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
4845 logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
4846 logpal->palPalEntry[i].peFlags = 0;
4847 colours[i] = PALETTERGB(defpal[i].rgbtRed,
4848 defpal[i].rgbtGreen,
4849 defpal[i].rgbtBlue);
4850 } else
4851 colours[i] = RGB(defpal[i].rgbtRed,
32874aea 4852 defpal[i].rgbtGreen, defpal[i].rgbtBlue);
374330e2 4853 }
4854
4855 if (pal) {
4856 HDC hdc;
cecb13f6 4857 SetPaletteEntries(pal, 0, NALLCOLOURS, logpal->palPalEntry);
a8327734 4858 hdc = get_ctx(frontend);
32874aea 4859 RealizePalette(hdc);
4860 free_ctx(hdc);
6092b4ee 4861 } else {
4862 /* Default Background may have changed. Ensure any space between
4863 * text area and window border is redrawn. */
4864 InvalidateRect(hwnd, NULL, TRUE);
374330e2 4865 }
4866}
4867
a8327734 4868void write_aclip(void *frontend, char *data, int len, int must_deselect)
32874aea 4869{
374330e2 4870 HGLOBAL clipdata;
4871 void *lock;
4872
32874aea 4873 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len + 1);
374330e2 4874 if (!clipdata)
4875 return;
32874aea 4876 lock = GlobalLock(clipdata);
374330e2 4877 if (!lock)
4878 return;
32874aea 4879 memcpy(lock, data, len);
4880 ((unsigned char *) lock)[len] = 0;
4881 GlobalUnlock(clipdata);
374330e2 4882
f0df44da 4883 if (!must_deselect)
32874aea 4884 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
f0df44da 4885
32874aea 4886 if (OpenClipboard(hwnd)) {
374330e2 4887 EmptyClipboard();
32874aea 4888 SetClipboardData(CF_TEXT, clipdata);
374330e2 4889 CloseClipboard();
4890 } else
32874aea 4891 GlobalFree(clipdata);
f0df44da 4892
4893 if (!must_deselect)
32874aea 4894 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
374330e2 4895}
4896
4eeb7d09 4897/*
4898 * Note: unlike write_aclip() this will not append a nul.
4899 */
c83c79bd 4900void write_clip(void *frontend, wchar_t * data, int *attr, int len, int must_deselect)
4eeb7d09 4901{
a7419ea4 4902 HGLOBAL clipdata, clipdata2, clipdata3;
4eeb7d09 4903 int len2;
a7419ea4 4904 void *lock, *lock2, *lock3;
4eeb7d09 4905
4906 len2 = WideCharToMultiByte(CP_ACP, 0, data, len, 0, 0, NULL, NULL);
4907
4908 clipdata = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE,
4909 len * sizeof(wchar_t));
4910 clipdata2 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, len2);
4911
0e5e7f46 4912 if (!clipdata || !clipdata2) {
4eeb7d09 4913 if (clipdata)
4914 GlobalFree(clipdata);
4915 if (clipdata2)
4916 GlobalFree(clipdata2);
4917 return;
4918 }
0c33d3a6 4919 if (!(lock = GlobalLock(clipdata))) {
4920 GlobalFree(clipdata);
4921 GlobalFree(clipdata2);
4eeb7d09 4922 return;
0c33d3a6 4923 }
4924 if (!(lock2 = GlobalLock(clipdata2))) {
4925 GlobalUnlock(clipdata);
4926 GlobalFree(clipdata);
4927 GlobalFree(clipdata2);
4eeb7d09 4928 return;
0c33d3a6 4929 }
4eeb7d09 4930
4931 memcpy(lock, data, len * sizeof(wchar_t));
4932 WideCharToMultiByte(CP_ACP, 0, data, len, lock2, len2, NULL, NULL);
4933
4a693cfc 4934 if (conf_get_int(conf, CONF_rtf_paste)) {
a7419ea4 4935 wchar_t unitab[256];
4936 char *rtf = NULL;
4937 unsigned char *tdata = (unsigned char *)lock2;
4938 wchar_t *udata = (wchar_t *)lock;
4939 int rtflen = 0, uindex = 0, tindex = 0;
4940 int rtfsize = 0;
4941 int multilen, blen, alen, totallen, i;
4942 char before[16], after[4];
c83c79bd 4943 int fgcolour, lastfgcolour = 0;
4944 int bgcolour, lastbgcolour = 0;
4945 int attrBold, lastAttrBold = 0;
4946 int attrUnder, lastAttrUnder = 0;
4947 int palette[NALLCOLOURS];
4948 int numcolours;
4a693cfc 4949 FontSpec *font = conf_get_fontspec(conf, CONF_font);
a7419ea4 4950
4951 get_unitab(CP_ACP, unitab, 0);
4952
4a693cfc 4953 rtfsize = 100 + strlen(font->name);
3d88e64d 4954 rtf = snewn(rtfsize, char);
c83c79bd 4955 rtflen = sprintf(rtf, "{\\rtf1\\ansi\\deff0{\\fonttbl\\f0\\fmodern %s;}\\f0\\fs%d",
4a693cfc 4956 font->name, font->height*2);
c83c79bd 4957
4958 /*
4959 * Add colour palette
4960 * {\colortbl ;\red255\green0\blue0;\red0\green0\blue128;}
4961 */
4962
4963 /*
4964 * First - Determine all colours in use
4965 * o Foregound and background colours share the same palette
4966 */
4967 if (attr) {
4968 memset(palette, 0, sizeof(palette));
4969 for (i = 0; i < (len-1); i++) {
4970 fgcolour = ((attr[i] & ATTR_FGMASK) >> ATTR_FGSHIFT);
4971 bgcolour = ((attr[i] & ATTR_BGMASK) >> ATTR_BGSHIFT);
4972
4973 if (attr[i] & ATTR_REVERSE) {
4974 int tmpcolour = fgcolour; /* Swap foreground and background */
4975 fgcolour = bgcolour;
4976 bgcolour = tmpcolour;
4977 }
4978
863c5362 4979 if (bold_colours && (attr[i] & ATTR_BOLD)) {
c83c79bd 4980 if (fgcolour < 8) /* ANSI colours */
4981 fgcolour += 8;
4982 else if (fgcolour >= 256) /* Default colours */
4983 fgcolour ++;
4984 }
4985
4986 if (attr[i] & ATTR_BLINK) {
4987 if (bgcolour < 8) /* ANSI colours */
4988 bgcolour += 8;
4989 else if (bgcolour >= 256) /* Default colours */
4990 bgcolour ++;
4991 }
4992
4993 palette[fgcolour]++;
4994 palette[bgcolour]++;
4995 }
4996
4997 /*
4998 * Next - Create a reduced palette
4999 */
5000 numcolours = 0;
5001 for (i = 0; i < NALLCOLOURS; i++) {
5002 if (palette[i] != 0)
5003 palette[i] = ++numcolours;
5004 }
5005
5006 /*
5007 * Finally - Write the colour table
5008 */
5009 rtf = sresize(rtf, rtfsize + (numcolours * 25), char);
5010 strcat(rtf, "{\\colortbl ;");
5011 rtflen = strlen(rtf);
5012
5013 for (i = 0; i < NALLCOLOURS; i++) {
5014 if (palette[i] != 0) {
5015 rtflen += sprintf(&rtf[rtflen], "\\red%d\\green%d\\blue%d;", defpal[i].rgbtRed, defpal[i].rgbtGreen, defpal[i].rgbtBlue);
5016 }
5017 }
5018 strcpy(&rtf[rtflen], "}");
5019 rtflen ++;
5020 }
a7419ea4 5021
5022 /*
5023 * We want to construct a piece of RTF that specifies the
5024 * same Unicode text. To do this we will read back in
5025 * parallel from the Unicode data in `udata' and the
5026 * non-Unicode data in `tdata'. For each character in
5027 * `tdata' which becomes the right thing in `udata' when
5028 * looked up in `unitab', we just copy straight over from
5029 * tdata. For each one that doesn't, we must WCToMB it
5030 * individually and produce a \u escape sequence.
5031 *
5032 * It would probably be more robust to just bite the bullet
5033 * and WCToMB each individual Unicode character one by one,
5034 * then MBToWC each one back to see if it was an accurate
5035 * translation; but that strikes me as a horrifying number
5036 * of Windows API calls so I want to see if this faster way
5037 * will work. If it screws up badly we can always revert to
5038 * the simple and slow way.
5039 */
5040 while (tindex < len2 && uindex < len &&
5041 tdata[tindex] && udata[uindex]) {
5042 if (tindex + 1 < len2 &&
5043 tdata[tindex] == '\r' &&
5044 tdata[tindex+1] == '\n') {
5045 tindex++;
5046 uindex++;
c83c79bd 5047 }
5048
5049 /*
5050 * Set text attributes
5051 */
5052 if (attr) {
5053 if (rtfsize < rtflen + 64) {
5054 rtfsize = rtflen + 512;
5055 rtf = sresize(rtf, rtfsize, char);
5056 }
5057
5058 /*
5059 * Determine foreground and background colours
5060 */
5061 fgcolour = ((attr[tindex] & ATTR_FGMASK) >> ATTR_FGSHIFT);
5062 bgcolour = ((attr[tindex] & ATTR_BGMASK) >> ATTR_BGSHIFT);
5063
5064 if (attr[tindex] & ATTR_REVERSE) {
5065 int tmpcolour = fgcolour; /* Swap foreground and background */
5066 fgcolour = bgcolour;
5067 bgcolour = tmpcolour;
5068 }
5069
863c5362 5070 if (bold_colours && (attr[tindex] & ATTR_BOLD)) {
c83c79bd 5071 if (fgcolour < 8) /* ANSI colours */
5072 fgcolour += 8;
5073 else if (fgcolour >= 256) /* Default colours */
5074 fgcolour ++;
5075 }
5076
5077 if (attr[tindex] & ATTR_BLINK) {
5078 if (bgcolour < 8) /* ANSI colours */
5079 bgcolour += 8;
5080 else if (bgcolour >= 256) /* Default colours */
5081 bgcolour ++;
5082 }
5083
5084 /*
5085 * Collect other attributes
5086 */
863c5362 5087 if (bold_font_mode != BOLD_NONE)
c83c79bd 5088 attrBold = attr[tindex] & ATTR_BOLD;
5089 else
5090 attrBold = 0;
5091
5092 attrUnder = attr[tindex] & ATTR_UNDER;
5093
5094 /*
5095 * Reverse video
5096 * o If video isn't reversed, ignore colour attributes for default foregound
5097 * or background.
5098 * o Special case where bolded text is displayed using the default foregound
5099 * and background colours - force to bolded RTF.
5100 */
5101 if (!(attr[tindex] & ATTR_REVERSE)) {
5102 if (bgcolour >= 256) /* Default color */
5103 bgcolour = -1; /* No coloring */
5104
5105 if (fgcolour >= 256) { /* Default colour */
863c5362 5106 if (bold_colours && (fgcolour & 1) && bgcolour == -1)
c83c79bd 5107 attrBold = ATTR_BOLD; /* Emphasize text with bold attribute */
5108
5109 fgcolour = -1; /* No coloring */
5110 }
5111 }
5112
5113 /*
5114 * Write RTF text attributes
5115 */
5116 if (lastfgcolour != fgcolour) {
5117 lastfgcolour = fgcolour;
5118 rtflen += sprintf(&rtf[rtflen], "\\cf%d ", (fgcolour >= 0) ? palette[fgcolour] : 0);
5119 }
5120
5121 if (lastbgcolour != bgcolour) {
5122 lastbgcolour = bgcolour;
5123 rtflen += sprintf(&rtf[rtflen], "\\highlight%d ", (bgcolour >= 0) ? palette[bgcolour] : 0);
5124 }
5125
5126 if (lastAttrBold != attrBold) {
5127 lastAttrBold = attrBold;
5128 rtflen += sprintf(&rtf[rtflen], "%s", attrBold ? "\\b " : "\\b0 ");
5129 }
5130
5131 if (lastAttrUnder != attrUnder) {
5132 lastAttrUnder = attrUnder;
5133 rtflen += sprintf(&rtf[rtflen], "%s", attrUnder ? "\\ul " : "\\ulnone ");
5134 }
a7419ea4 5135 }
c83c79bd 5136
a7419ea4 5137 if (unitab[tdata[tindex]] == udata[uindex]) {
5138 multilen = 1;
5139 before[0] = '\0';
5140 after[0] = '\0';
5141 blen = alen = 0;
5142 } else {
5143 multilen = WideCharToMultiByte(CP_ACP, 0, unitab+uindex, 1,
5144 NULL, 0, NULL, NULL);
5145 if (multilen != 1) {
c3b032a6 5146 blen = sprintf(before, "{\\uc%d\\u%d", multilen,
5147 udata[uindex]);
a7419ea4 5148 alen = 1; strcpy(after, "}");
5149 } else {
5150 blen = sprintf(before, "\\u%d", udata[uindex]);
5151 alen = 0; after[0] = '\0';
5152 }
5153 }
5154 assert(tindex + multilen <= len2);
5155 totallen = blen + alen;
5156 for (i = 0; i < multilen; i++) {
5157 if (tdata[tindex+i] == '\\' ||
5158 tdata[tindex+i] == '{' ||
5159 tdata[tindex+i] == '}')
5160 totallen += 2;
5161 else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A)
5162 totallen += 6; /* \par\r\n */
5163 else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20)
5164 totallen += 4;
5165 else
5166 totallen++;
5167 }
5168
5169 if (rtfsize < rtflen + totallen + 3) {
5170 rtfsize = rtflen + totallen + 512;
3d88e64d 5171 rtf = sresize(rtf, rtfsize, char);
a7419ea4 5172 }
5173
5174 strcpy(rtf + rtflen, before); rtflen += blen;
5175 for (i = 0; i < multilen; i++) {
5176 if (tdata[tindex+i] == '\\' ||
5177 tdata[tindex+i] == '{' ||
5178 tdata[tindex+i] == '}') {
5179 rtf[rtflen++] = '\\';
5180 rtf[rtflen++] = tdata[tindex+i];
5181 } else if (tdata[tindex+i] == 0x0D || tdata[tindex+i] == 0x0A) {
5182 rtflen += sprintf(rtf+rtflen, "\\par\r\n");
5183 } else if (tdata[tindex+i] > 0x7E || tdata[tindex+i] < 0x20) {
5184 rtflen += sprintf(rtf+rtflen, "\\'%02x", tdata[tindex+i]);
5185 } else {
5186 rtf[rtflen++] = tdata[tindex+i];
5187 }
5188 }
5189 strcpy(rtf + rtflen, after); rtflen += alen;
5190
5191 tindex += multilen;
5192 uindex++;
5193 }
5194
c83c79bd 5195 rtf[rtflen++] = '}'; /* Terminate RTF stream */
5196 rtf[rtflen++] = '\0';
5197 rtf[rtflen++] = '\0';
a7419ea4 5198
5199 clipdata3 = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, rtflen);
5200 if (clipdata3 && (lock3 = GlobalLock(clipdata3)) != NULL) {
c83c79bd 5201 memcpy(lock3, rtf, rtflen);
a7419ea4 5202 GlobalUnlock(clipdata3);
5203 }
5204 sfree(rtf);
5205 } else
5206 clipdata3 = NULL;
5207
4eeb7d09 5208 GlobalUnlock(clipdata);
5209 GlobalUnlock(clipdata2);
5210
5211 if (!must_deselect)
5212 SendMessage(hwnd, WM_IGNORE_CLIP, TRUE, 0);
5213
5214 if (OpenClipboard(hwnd)) {
5215 EmptyClipboard();
5216 SetClipboardData(CF_UNICODETEXT, clipdata);
5217 SetClipboardData(CF_TEXT, clipdata2);
a7419ea4 5218 if (clipdata3)
5219 SetClipboardData(RegisterClipboardFormat(CF_RTF), clipdata3);
4eeb7d09 5220 CloseClipboard();
5221 } else {
5222 GlobalFree(clipdata);
5223 GlobalFree(clipdata2);
5224 }
5225
5226 if (!must_deselect)
5227 SendMessage(hwnd, WM_IGNORE_CLIP, FALSE, 0);
5228}
5229
1d432454 5230static DWORD WINAPI clipboard_read_threadfunc(void *param)
32874aea 5231{
1d432454 5232 HWND hwnd = (HWND)param;
5233 HGLOBAL clipdata;
374330e2 5234
1d432454 5235 if (OpenClipboard(NULL)) {
2d466ffd 5236 if ((clipdata = GetClipboardData(CF_UNICODETEXT))) {
1d432454 5237 SendMessage(hwnd, WM_GOT_CLIPDATA, (WPARAM)1, (LPARAM)clipdata);
5238 } else if ((clipdata = GetClipboardData(CF_TEXT))) {
5239 SendMessage(hwnd, WM_GOT_CLIPDATA, (WPARAM)0, (LPARAM)clipdata);
5240 }
5241 CloseClipboard();
5242 }
5243
5244 return 0;
5245}
5246
5247static int process_clipdata(HGLOBAL clipdata, int unicode)
5248{
1d432454 5249 sfree(clipboard_contents);
5250 clipboard_contents = NULL;
5251 clipboard_length = 0;
5252
5253 if (unicode) {
5254 wchar_t *p = GlobalLock(clipdata);
5255 wchar_t *p2;
5256
5257 if (p) {
5258 /* Unwilling to rely on Windows having wcslen() */
5259 for (p2 = p; *p2; p2++);
5260 clipboard_length = p2 - p;
5261 clipboard_contents = snewn(clipboard_length + 1, wchar_t);
5262 memcpy(clipboard_contents, p, clipboard_length * sizeof(wchar_t));
5263 clipboard_contents[clipboard_length] = L'\0';
5264 return TRUE;
5265 }
5266 } else {
5267 char *s = GlobalLock(clipdata);
5268 int i;
5269
5270 if (s) {
4eeb7d09 5271 i = MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1, 0, 0);
1d432454 5272 clipboard_contents = snewn(i, wchar_t);
5273 MultiByteToWideChar(CP_ACP, 0, s, strlen(s) + 1,
5274 clipboard_contents, i);
5275 clipboard_length = i - 1;
5276 clipboard_contents[clipboard_length] = L'\0';
5277 return TRUE;
5278 }
374330e2 5279 }
5280
1d432454 5281 return FALSE;
5282}
5283
5284void request_paste(void *frontend)
5285{
5286 /*
5287 * I always thought pasting was synchronous in Windows; the
5288 * clipboard access functions certainly _look_ synchronous,
5289 * unlike the X ones. But in fact it seems that in some
5290 * situations the contents of the clipboard might not be
5291 * immediately available, and the clipboard-reading functions
5292 * may block. This leads to trouble if the application
5293 * delivering the clipboard data has to get hold of it by -
5294 * for example - talking over a network connection which is
5295 * forwarded through this very PuTTY.
5296 *
5297 * Hence, we spawn a subthread to read the clipboard, and do
5298 * our paste when it's finished. The thread will send a
5299 * message back to our main window when it terminates, and
5300 * that tells us it's OK to paste.
5301 */
5302 DWORD in_threadid; /* required for Win9x */
5303 CreateThread(NULL, 0, clipboard_read_threadfunc,
5304 hwnd, 0, &in_threadid);
5305}
5306
5307void get_clip(void *frontend, wchar_t **p, int *len)
5308{
5309 if (p) {
5310 *p = clipboard_contents;
5311 *len = clipboard_length;
5312 }
374330e2 5313}
5314
4eeb7d09 5315#if 0
374330e2 5316/*
5317 * Move `lines' lines from position `from' to position `to' in the
5318 * window.
5319 */
a8327734 5320void optimised_move(void *frontend, int to, int from, int lines)
32874aea 5321{
374330e2 5322 RECT r;
f67b4e85 5323 int min, max;
374330e2 5324
5325 min = (to < from ? to : from);
5326 max = to + from - min;
374330e2 5327
5a73255e 5328 r.left = offset_width;
887035a5 5329 r.right = offset_width + term->cols * font_width;
5a73255e 5330 r.top = offset_height + min * font_height;
5331 r.bottom = offset_height + (max + lines) * font_height;
32874aea 5332 ScrollWindow(hwnd, 0, (to - from) * font_height, &r, &r);
374330e2 5333}
4eeb7d09 5334#endif
374330e2 5335
5336/*
5337 * Print a message box and perform a fatal exit.
5338 */
32874aea 5339void fatalbox(char *fmt, ...)
5340{
374330e2 5341 va_list ap;
971bcc0a 5342 char *stuff, morestuff[100];
374330e2 5343
5344 va_start(ap, fmt);
971bcc0a 5345 stuff = dupvprintf(fmt, ap);
374330e2 5346 va_end(ap);
f6f450e2 5347 sprintf(morestuff, "%.70s Fatal Error", appname);
5348 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
971bcc0a 5349 sfree(stuff);
93b581bd 5350 cleanup_exit(1);
374330e2 5351}
5352
5353/*
1709795f 5354 * Print a modal (Really Bad) message box and perform a fatal exit.
5355 */
5356void modalfatalbox(char *fmt, ...)
5357{
5358 va_list ap;
971bcc0a 5359 char *stuff, morestuff[100];
1709795f 5360
5361 va_start(ap, fmt);
971bcc0a 5362 stuff = dupvprintf(fmt, ap);
1709795f 5363 va_end(ap);
f6f450e2 5364 sprintf(morestuff, "%.70s Fatal Error", appname);
5365 MessageBox(hwnd, stuff, morestuff,
1709795f 5366 MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
971bcc0a 5367 sfree(stuff);
1709795f 5368 cleanup_exit(1);
5369}
5370
33f4bde2 5371/*
5372 * Print a message box and don't close the connection.
5373 */
5374void nonfatal(char *fmt, ...)
5375{
5376 va_list ap;
5377 char *stuff, morestuff[100];
5378
5379 va_start(ap, fmt);
5380 stuff = dupvprintf(fmt, ap);
5381 va_end(ap);
5382 sprintf(morestuff, "%.70s Error", appname);
5383 MessageBox(hwnd, stuff, morestuff, MB_ICONERROR | MB_OK);
5384 sfree(stuff);
5385}
5386
9099600a 5387DECL_WINDOWS_FUNCTION(static, BOOL, FlashWindowEx, (PFLASHWINFO));
09644e07 5388
5389static void init_flashwindow(void)
5390{
bda368a5 5391 HMODULE user32_module = load_system32_dll("user32.dll");
9099600a 5392 GET_WINDOWS_FUNCTION(user32_module, FlashWindowEx);
09644e07 5393}
5394
5395static BOOL flash_window_ex(DWORD dwFlags, UINT uCount, DWORD dwTimeout)
5396{
5397 if (p_FlashWindowEx) {
5398 FLASHWINFO fi;
5399 fi.cbSize = sizeof(fi);
5400 fi.hwnd = hwnd;
5401 fi.dwFlags = dwFlags;
5402 fi.uCount = uCount;
5403 fi.dwTimeout = dwTimeout;
5404 return (*p_FlashWindowEx)(&fi);
5405 }
5406 else
5407 return FALSE; /* shrug */
5408}
5409
39934deb 5410static void flash_window(int mode);
5411static long next_flash;
5412static int flashing = 0;
5413
09644e07 5414/*
5415 * Timer for platforms where we must maintain window flashing manually
5416 * (e.g., Win95).
5417 */
d719927e 5418static void flash_window_timer(void *ctx, unsigned long now)
39934deb 5419{
d719927e 5420 if (flashing && now == next_flash) {
39934deb 5421 flash_window(1);
5422 }
5423}
5424
1709795f 5425/*
f8a28d1f 5426 * Manage window caption / taskbar flashing, if enabled.
5427 * 0 = stop, 1 = maintain, 2 = start
5428 */
5429static void flash_window(int mode)
5430{
4a693cfc 5431 int beep_ind = conf_get_int(conf, CONF_beep_ind);
5432 if ((mode == 0) || (beep_ind == B_IND_DISABLED)) {
f8a28d1f 5433 /* stop */
5434 if (flashing) {
f8a28d1f 5435 flashing = 0;
09644e07 5436 if (p_FlashWindowEx)
5437 flash_window_ex(FLASHW_STOP, 0, 0);
5438 else
5439 FlashWindow(hwnd, FALSE);
f8a28d1f 5440 }
5441
5442 } else if (mode == 2) {
5443 /* start */
5444 if (!flashing) {
f8a28d1f 5445 flashing = 1;
09644e07 5446 if (p_FlashWindowEx) {
5447 /* For so-called "steady" mode, we use uCount=2, which
5448 * seems to be the traditional number of flashes used
5449 * by user notifications (e.g., by Explorer).
5450 * uCount=0 appears to enable continuous flashing, per
5451 * "flashing" mode, although I haven't seen this
5452 * documented. */
5453 flash_window_ex(FLASHW_ALL | FLASHW_TIMER,
4a693cfc 5454 (beep_ind == B_IND_FLASH ? 0 : 2),
09644e07 5455 0 /* system cursor blink rate */);
5456 /* No need to schedule timer */
5457 } else {
5458 FlashWindow(hwnd, TRUE);
5459 next_flash = schedule_timer(450, flash_window_timer, hwnd);
5460 }
f8a28d1f 5461 }
5462
4a693cfc 5463 } else if ((mode == 1) && (beep_ind == B_IND_FLASH)) {
f8a28d1f 5464 /* maintain */
09644e07 5465 if (flashing && !p_FlashWindowEx) {
39934deb 5466 FlashWindow(hwnd, TRUE); /* toggle */
5467 next_flash = schedule_timer(450, flash_window_timer, hwnd);
f8a28d1f 5468 }
5469 }
5470}
5471
5472/*
374330e2 5473 * Beep.
5474 */
860a34f8 5475void do_beep(void *frontend, int mode)
32874aea 5476{
03169ad0 5477 if (mode == BELL_DEFAULT) {
eb04402e 5478 /*
5479 * For MessageBeep style bells, we want to be careful of
5480 * timing, because they don't have the nice property of
5481 * PlaySound bells that each one cancels the previous
5482 * active one. So we limit the rate to one per 50ms or so.
5483 */
5484 static long lastbeep = 0;
d51cdf1e 5485 long beepdiff;
eb04402e 5486
d51cdf1e 5487 beepdiff = GetTickCount() - lastbeep;
eb04402e 5488 if (beepdiff >= 0 && beepdiff < 50)
5489 return;
156686ef 5490 MessageBeep(MB_OK);
d51cdf1e 5491 /*
5492 * The above MessageBeep call takes time, so we record the
5493 * time _after_ it finishes rather than before it starts.
5494 */
5495 lastbeep = GetTickCount();
03169ad0 5496 } else if (mode == BELL_WAVEFILE) {
4a693cfc 5497 Filename *bell_wavefile = conf_get_filename(conf, CONF_bell_wavefile);
5498 if (!PlaySound(bell_wavefile->path, NULL,
9a30e26b 5499 SND_ASYNC | SND_FILENAME)) {
4a693cfc 5500 char buf[sizeof(bell_wavefile->path) + 80];
f6f450e2 5501 char otherbuf[100];
03169ad0 5502 sprintf(buf, "Unable to play sound file\n%s\n"
4a693cfc 5503 "Using default sound instead", bell_wavefile->path);
f6f450e2 5504 sprintf(otherbuf, "%.70s Sound Error", appname);
5505 MessageBox(hwnd, buf, otherbuf,
32874aea 5506 MB_OK | MB_ICONEXCLAMATION);
4a693cfc 5507 conf_set_int(conf, CONF_beep, BELL_DEFAULT);
03169ad0 5508 }
85f6b361 5509 } else if (mode == BELL_PCSPEAKER) {
5510 static long lastbeep = 0;
5511 long beepdiff;
5512
5513 beepdiff = GetTickCount() - lastbeep;
5514 if (beepdiff >= 0 && beepdiff < 50)
5515 return;
5516
5517 /*
5518 * We must beep in different ways depending on whether this
5519 * is a 95-series or NT-series OS.
5520 */
5521 if(osVersion.dwPlatformId == VER_PLATFORM_WIN32_NT)
5522 Beep(800, 100);
5523 else
5524 MessageBeep(-1);
5525 lastbeep = GetTickCount();
03169ad0 5526 }
f8a28d1f 5527 /* Otherwise, either visual bell or disabled; do nothing here */
887035a5 5528 if (!term->has_focus) {
f8a28d1f 5529 flash_window(2); /* start */
5530 }
374330e2 5531}
8f57d753 5532
5533/*
68f9b3d9 5534 * Minimise or restore the window in response to a server-side
5535 * request.
5536 */
a8327734 5537void set_iconic(void *frontend, int iconic)
68f9b3d9 5538{
5539 if (IsIconic(hwnd)) {
5540 if (!iconic)
5541 ShowWindow(hwnd, SW_RESTORE);
5542 } else {
5543 if (iconic)
5544 ShowWindow(hwnd, SW_MINIMIZE);
5545 }
5546}
5547
5548/*
5549 * Move the window in response to a server-side request.
5550 */
a8327734 5551void move_window(void *frontend, int x, int y)
68f9b3d9 5552{
4a693cfc 5553 int resize_action = conf_get_int(conf, CONF_resize_action);
5554 if (resize_action == RESIZE_DISABLED ||
5555 resize_action == RESIZE_FONT ||
d1e0a352 5556 IsZoomed(hwnd))
5557 return;
5558
68f9b3d9 5559 SetWindowPos(hwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
5560}
5561
5562/*
5563 * Move the window to the top or bottom of the z-order in response
5564 * to a server-side request.
5565 */
a8327734 5566void set_zorder(void *frontend, int top)
68f9b3d9 5567{
4a693cfc 5568 if (conf_get_int(conf, CONF_alwaysontop))
68f9b3d9 5569 return; /* ignore */
5570 SetWindowPos(hwnd, top ? HWND_TOP : HWND_BOTTOM, 0, 0, 0, 0,
5571 SWP_NOMOVE | SWP_NOSIZE);
5572}
5573
5574/*
5575 * Refresh the window in response to a server-side request.
5576 */
a8327734 5577void refresh_window(void *frontend)
68f9b3d9 5578{
5579 InvalidateRect(hwnd, NULL, TRUE);
5580}
5581
5582/*
5583 * Maximise or restore the window in response to a server-side
5584 * request.
5585 */
a8327734 5586void set_zoomed(void *frontend, int zoomed)
68f9b3d9 5587{
1ba99d2c 5588 if (IsZoomed(hwnd)) {
5589 if (!zoomed)
5590 ShowWindow(hwnd, SW_RESTORE);
68f9b3d9 5591 } else {
5592 if (zoomed)
5593 ShowWindow(hwnd, SW_MAXIMIZE);
5594 }
5595}
5596
5597/*
5598 * Report whether the window is iconic, for terminal reports.
5599 */
a8327734 5600int is_iconic(void *frontend)
68f9b3d9 5601{
5602 return IsIconic(hwnd);
5603}
5604
5605/*
5606 * Report the window's position, for terminal reports.
5607 */
a8327734 5608void get_window_pos(void *frontend, int *x, int *y)
68f9b3d9 5609{
5610 RECT r;
5611 GetWindowRect(hwnd, &r);
5612 *x = r.left;
5613 *y = r.top;
5614}
5615
5616/*
5617 * Report the window's pixel size, for terminal reports.
5618 */
a8327734 5619void get_window_pixels(void *frontend, int *x, int *y)
68f9b3d9 5620{
5621 RECT r;
5622 GetWindowRect(hwnd, &r);
5623 *x = r.right - r.left;
5624 *y = r.bottom - r.top;
5625}
5626
5627/*
5628 * Return the window or icon title.
5629 */
a8327734 5630char *get_window_title(void *frontend, int icon)
68f9b3d9 5631{
5632 return icon ? icon_name : window_name;
5633}
1ba99d2c 5634
5635/*
5636 * See if we're in full-screen mode.
5637 */
4e95095a 5638static int is_full_screen()
1ba99d2c 5639{
5640 if (!IsZoomed(hwnd))
5641 return FALSE;
1e5eefb6 5642 if (GetWindowLongPtr(hwnd, GWL_STYLE) & WS_CAPTION)
1ba99d2c 5643 return FALSE;
5644 return TRUE;
5645}
5646
d0f1bcb4 5647/* Get the rect/size of a full screen window using the nearest available
5648 * monitor in multimon systems; default to something sensible if only
5649 * one monitor is present. */
5650static int get_fullscreen_rect(RECT * ss)
5651{
5d145a14 5652#if defined(MONITOR_DEFAULTTONEAREST) && !defined(NO_MULTIMON)
d0f1bcb4 5653 HMONITOR mon;
5654 MONITORINFO mi;
5655 mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
5656 mi.cbSize = sizeof(mi);
5657 GetMonitorInfo(mon, &mi);
5658
5659 /* structure copy */
5660 *ss = mi.rcMonitor;
5661 return TRUE;
5662#else
5663/* could also use code like this:
5664 ss->left = ss->top = 0;
5665 ss->right = GetSystemMetrics(SM_CXSCREEN);
5666 ss->bottom = GetSystemMetrics(SM_CYSCREEN);
5667*/
e6f8202f 5668 return GetClientRect(GetDesktopWindow(), ss);
d0f1bcb4 5669#endif
5670}
5671
5672
1ba99d2c 5673/*
5674 * Go full-screen. This should only be called when we are already
5675 * maximised.
5676 */
4e95095a 5677static void make_full_screen()
1ba99d2c 5678{
5679 DWORD style;
d0f1bcb4 5680 RECT ss;
1ba99d2c 5681
5682 assert(IsZoomed(hwnd));
5683
d0f1bcb4 5684 if (is_full_screen())
5685 return;
5686
1ba99d2c 5687 /* Remove the window furniture. */
1e5eefb6 5688 style = GetWindowLongPtr(hwnd, GWL_STYLE);
1ba99d2c 5689 style &= ~(WS_CAPTION | WS_BORDER | WS_THICKFRAME);
4a693cfc 5690 if (conf_get_int(conf, CONF_scrollbar_in_fullscreen))
1ba99d2c 5691 style |= WS_VSCROLL;
5692 else
5693 style &= ~WS_VSCROLL;
1e5eefb6 5694 SetWindowLongPtr(hwnd, GWL_STYLE, style);
1ba99d2c 5695
5696 /* Resize ourselves to exactly cover the nearest monitor. */
d0f1bcb4 5697 get_fullscreen_rect(&ss);
5698 SetWindowPos(hwnd, HWND_TOP, ss.left, ss.top,
5699 ss.right - ss.left,
5700 ss.bottom - ss.top,
5701 SWP_FRAMECHANGED);
1ba99d2c 5702
1620020b 5703 /* We may have changed size as a result */
5704
5705 reset_window(0);
5706
52c70126 5707 /* Tick the menu item in the System and context menus. */
5708 {
5709 int i;
5710 for (i = 0; i < lenof(popup_menus); i++)
5711 CheckMenuItem(popup_menus[i].menu, IDM_FULLSCREEN, MF_CHECKED);
5712 }
1ba99d2c 5713}
5714
5715/*
5716 * Clear the full-screen attributes.
5717 */
4e95095a 5718static void clear_full_screen()
1ba99d2c 5719{
5720 DWORD oldstyle, style;
5721
5722 /* Reinstate the window furniture. */
1e5eefb6 5723 style = oldstyle = GetWindowLongPtr(hwnd, GWL_STYLE);
811f10b3 5724 style |= WS_CAPTION | WS_BORDER;
4a693cfc 5725 if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED)
811f10b3 5726 style &= ~WS_THICKFRAME;
5727 else
5728 style |= WS_THICKFRAME;
4a693cfc 5729 if (conf_get_int(conf, CONF_scrollbar))
1ba99d2c 5730 style |= WS_VSCROLL;
5731 else
5732 style &= ~WS_VSCROLL;
5733 if (style != oldstyle) {
1e5eefb6 5734 SetWindowLongPtr(hwnd, GWL_STYLE, style);
1ba99d2c 5735 SetWindowPos(hwnd, NULL, 0, 0, 0, 0,
5736 SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
5737 SWP_FRAMECHANGED);
5738 }
5739
52c70126 5740 /* Untick the menu item in the System and context menus. */
5741 {
5742 int i;
5743 for (i = 0; i < lenof(popup_menus); i++)
5744 CheckMenuItem(popup_menus[i].menu, IDM_FULLSCREEN, MF_UNCHECKED);
5745 }
1ba99d2c 5746}
5747
5748/*
5749 * Toggle full-screen mode.
5750 */
4e95095a 5751static void flip_full_screen()
1ba99d2c 5752{
5753 if (is_full_screen()) {
5754 ShowWindow(hwnd, SW_RESTORE);
5755 } else if (IsZoomed(hwnd)) {
5756 make_full_screen();
5757 } else {
5758 SendMessage(hwnd, WM_FULLSCR_ON_MAX, 0, 0);
5759 ShowWindow(hwnd, SW_MAXIMIZE);
5760 }
5761}
ab2c86b9 5762
b9d7bcad 5763void frontend_keypress(void *handle)
ab2c86b9 5764{
5765 /*
5766 * Keypress termination in non-Close-On-Exit mode is not
5767 * currently supported in PuTTY proper, because the window
5768 * always has a perfectly good Close button anyway. So we do
5769 * nothing here.
5770 */
5771 return;
5772}
fbf6cb3b 5773
5774int from_backend(void *frontend, int is_stderr, const char *data, int len)
5775{
5776 return term_data(term, is_stderr, data, len);
5777}
c44bf5bd 5778
edd0cb8a 5779int from_backend_untrusted(void *frontend, const char *data, int len)
5780{
5781 return term_data_untrusted(term, data, len);
5782}
5783
bc06669b 5784int from_backend_eof(void *frontend)
5785{
5786 return TRUE; /* do respond to incoming EOF with outgoing */
5787}
5788
edd0cb8a 5789int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
5790{
5791 int ret;
5792 ret = cmdline_get_passwd_input(p, in, inlen);
5793 if (ret == -1)
5794 ret = term_get_userpass_input(term, p, in, inlen);
5795 return ret;
5796}
5797
c44bf5bd 5798void agent_schedule_callback(void (*callback)(void *, void *, int),
5799 void *callback_ctx, void *data, int len)
5800{
5801 struct agent_callback *c = snew(struct agent_callback);
5802 c->callback = callback;
5803 c->callback_ctx = callback_ctx;
5804 c->data = data;
5805 c->len = len;
5806 PostMessage(hwnd, WM_AGENT_CALLBACK, 0, (LPARAM)c);
5807}