This repository has been archived by the owner on Oct 20, 2023. It is now read-only.
/
window.c
5845 lines (5239 loc) · 193 KB
/
window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* window.c - the PuTTY(tel) main program, which runs a PuTTY terminal
* emulator and backend in a window.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#include <assert.h>
#ifdef __WINE__
#define NO_MULTIMON /* winelib doesn't have this */
#endif
#ifndef NO_MULTIMON
#define COMPILE_MULTIMON_STUBS
#endif
#include "putty.h"
#include "terminal.h"
#include "storage.h"
#include "win_res.h"
#include "winsecur.h"
#include "winseat.h"
#include "tree234.h"
#ifndef NO_MULTIMON
#include <multimon.h>
#endif
#include <imm.h>
#include <commctrl.h>
#include <richedit.h>
#include <mmsystem.h>
/* From MSDN: In the WM_SYSCOMMAND message, the four low-order bits of
* wParam are used by Windows, and should be masked off, so we shouldn't
* attempt to store information in them. Hence all these identifiers have
* the low 4 bits clear. Also, identifiers should < 0xF000. */
#define IDM_SHOWLOG 0x0010
#define IDM_NEWSESS 0x0020
#define IDM_DUPSESS 0x0030
#define IDM_RESTART 0x0040
#define IDM_RECONF 0x0050
#define IDM_CLRSB 0x0060
#define IDM_RESET 0x0070
#define IDM_HELP 0x0140
#define IDM_ABOUT 0x0150
#define IDM_SAVEDSESS 0x0160
#define IDM_COPYALL 0x0170
#define IDM_FULLSCREEN 0x0180
#define IDM_COPY 0x0190
#define IDM_PASTE 0x01A0
#define IDM_SPECIALSEP 0x0200
#define IDM_SPECIAL_MIN 0x0400
#define IDM_SPECIAL_MAX 0x0800
#define IDM_SAVED_MIN 0x1000
#define IDM_SAVED_MAX 0x5000
#define MENU_SAVED_STEP 16
/* Maximum number of sessions on saved-session submenu */
#define MENU_SAVED_MAX ((IDM_SAVED_MAX-IDM_SAVED_MIN) / MENU_SAVED_STEP)
#define WM_IGNORE_CLIP (WM_APP + 2)
#define WM_FULLSCR_ON_MAX (WM_APP + 3)
#define WM_GOT_CLIPDATA (WM_APP + 4)
/* Needed for Chinese support and apparently not always defined. */
#ifndef VK_PROCESSKEY
#define VK_PROCESSKEY 0xE5
#endif
/* Mouse wheel support. */
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 0x020A /* not defined in earlier SDKs */
#endif
#ifndef WHEEL_DELTA
#define WHEEL_DELTA 120
#endif
/* VK_PACKET, used to send Unicode characters in WM_KEYDOWNs */
#ifndef VK_PACKET
#define VK_PACKET 0xE7
#endif
static Mouse_Button translate_button(Mouse_Button button);
static void show_mouseptr(bool show);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam,
unsigned char *output);
static void conftopalette(void);
static void systopalette(void);
static void init_palette(void);
static void init_fonts(int, int);
static void another_font(int);
static void deinit_fonts(void);
static void set_input_locale(HKL);
static void update_savedsess_menu(void);
static void init_winfuncs(void);
static bool is_full_screen(void);
static void make_full_screen(void);
static void clear_full_screen(void);
static void flip_full_screen(void);
static void process_clipdata(HGLOBAL clipdata, bool unicode);
static void setup_clipboards(Terminal *, Conf *);
/* Window layout information */
static void reset_window(int);
static int extra_width, extra_height;
static int font_width, font_height;
static bool font_dualwidth, font_varpitch;
static int offset_width, offset_height;
static bool was_zoomed = false;
static int prev_rows, prev_cols;
static void flash_window(int mode);
static void sys_cursor_update(void);
static bool get_fullscreen_rect(RECT * ss);
static int caret_x = -1, caret_y = -1;
static int kbd_codepage;
static Ldisc *ldisc;
static Backend *backend;
static struct unicode_data ucsdata;
static bool session_closed;
static bool reconfiguring = false;
static const SessionSpecial *specials = NULL;
static HMENU specials_menu = NULL;
static int n_specials = 0;
#define TIMING_TIMER_ID 1234
static long timing_next_time;
static struct {
HMENU menu;
} popup_menus[2];
enum { SYSMENU, CTXMENU };
static HMENU savedsess_menu;
static Conf *conf;
static LogContext *logctx;
static Terminal *term;
struct wm_netevent_params {
/* Used to pass data to wm_netevent_callback */
WPARAM wParam;
LPARAM lParam;
};
static void conf_cache_data(void);
static int cursor_type;
static int vtmode;
static struct sesslist sesslist; /* for saved-session menu */
#define FONT_NORMAL 0
#define FONT_BOLD 1
#define FONT_UNDERLINE 2
#define FONT_BOLDUND 3
#define FONT_WIDE 0x04
#define FONT_HIGH 0x08
#define FONT_NARROW 0x10
#define FONT_OEM 0x20
#define FONT_OEMBOLD 0x21
#define FONT_OEMUND 0x22
#define FONT_OEMBOLDUND 0x23
#define FONT_MAXNO 0x40
#define FONT_SHIFT 5
static HFONT fonts[FONT_MAXNO];
static LOGFONT lfont;
static bool fontflag[FONT_MAXNO];
static enum {
BOLD_NONE, BOLD_SHADOW, BOLD_FONT
} bold_font_mode;
static bool bold_colours;
static enum {
UND_LINE, UND_FONT
} und_mode;
static int descent, font_strikethrough_y;
#define NCFGCOLOURS 22
#define NEXTCOLOURS 240
#define NALLCOLOURS (NCFGCOLOURS + NEXTCOLOURS)
static COLORREF colours[NALLCOLOURS];
static struct rgb {
int r, g, b;
} colours_rgb[NALLCOLOURS];
static HPALETTE pal;
static LPLOGPALETTE logpal;
static RGBTRIPLE defpal[NALLCOLOURS];
static HBITMAP caretbm;
static int dbltime, lasttime, lastact;
static Mouse_Button lastbtn;
/* this allows xterm-style mouse handling. */
static bool send_raw_mouse = false;
static int wheel_accumulator = 0;
static BusyStatus busy_status = BUSY_NOT;
static char *window_name, *icon_name;
static int compose_state = 0;
static UINT wm_mousewheel = WM_MOUSEWHEEL;
#define IS_HIGH_VARSEL(wch1, wch2) \
((wch1) == 0xDB40 && ((wch2) >= 0xDD00 && (wch2) <= 0xDDEF))
#define IS_LOW_VARSEL(wch) \
(((wch) >= 0x180B && (wch) <= 0x180D) || /* MONGOLIAN FREE VARIATION SELECTOR */ \
((wch) >= 0xFE00 && (wch) <= 0xFE0F)) /* VARIATION SELECTOR 1-16 */
static bool wintw_setup_draw_ctx(TermWin *);
static void wintw_draw_text(TermWin *, int x, int y, wchar_t *text, int len,
unsigned long attrs, int lattrs, truecolour tc);
static void wintw_draw_cursor(TermWin *, int x, int y, wchar_t *text, int len,
unsigned long attrs, int lattrs, truecolour tc);
static void wintw_draw_trust_sigil(TermWin *, int x, int y);
static int wintw_char_width(TermWin *, int uc);
static void wintw_free_draw_ctx(TermWin *);
static void wintw_set_cursor_pos(TermWin *, int x, int y);
static void wintw_set_raw_mouse_mode(TermWin *, bool enable);
static void wintw_set_scrollbar(TermWin *, int total, int start, int page);
static void wintw_bell(TermWin *, int mode);
static void wintw_clip_write(
TermWin *, int clipboard, wchar_t *text, int *attrs,
truecolour *colours, int len, bool must_deselect);
static void wintw_clip_request_paste(TermWin *, int clipboard);
static void wintw_refresh(TermWin *);
static void wintw_request_resize(TermWin *, int w, int h);
static void wintw_set_title(TermWin *, const char *title);
static void wintw_set_icon_title(TermWin *, const char *icontitle);
static void wintw_set_minimised(TermWin *, bool minimised);
static bool wintw_is_minimised(TermWin *);
static void wintw_set_maximised(TermWin *, bool maximised);
static void wintw_move(TermWin *, int x, int y);
static void wintw_set_zorder(TermWin *, bool top);
static bool wintw_palette_get(TermWin *, int n, int *r, int *g, int *b);
static void wintw_palette_set(TermWin *, int n, int r, int g, int b);
static void wintw_palette_reset(TermWin *);
static void wintw_get_pos(TermWin *, int *x, int *y);
static void wintw_get_pixels(TermWin *, int *x, int *y);
static const char *wintw_get_title(TermWin *, bool icon);
static bool wintw_is_utf8(TermWin *);
static const TermWinVtable windows_termwin_vt = {
.setup_draw_ctx = wintw_setup_draw_ctx,
.draw_text = wintw_draw_text,
.draw_cursor = wintw_draw_cursor,
.draw_trust_sigil = wintw_draw_trust_sigil,
.char_width = wintw_char_width,
.free_draw_ctx = wintw_free_draw_ctx,
.set_cursor_pos = wintw_set_cursor_pos,
.set_raw_mouse_mode = wintw_set_raw_mouse_mode,
.set_scrollbar = wintw_set_scrollbar,
.bell = wintw_bell,
.clip_write = wintw_clip_write,
.clip_request_paste = wintw_clip_request_paste,
.refresh = wintw_refresh,
.request_resize = wintw_request_resize,
.set_title = wintw_set_title,
.set_icon_title = wintw_set_icon_title,
.set_minimised = wintw_set_minimised,
.is_minimised = wintw_is_minimised,
.set_maximised = wintw_set_maximised,
.move = wintw_move,
.set_zorder = wintw_set_zorder,
.palette_get = wintw_palette_get,
.palette_set = wintw_palette_set,
.palette_reset = wintw_palette_reset,
.get_pos = wintw_get_pos,
.get_pixels = wintw_get_pixels,
.get_title = wintw_get_title,
.is_utf8 = wintw_is_utf8,
};
static TermWin wintw[1];
static HDC wintw_hdc;
static HICON trust_icon = INVALID_HANDLE_VALUE;
const bool share_can_be_downstream = true;
const bool share_can_be_upstream = true;
static bool is_utf8(void)
{
return ucsdata.line_codepage == CP_UTF8;
}
static bool wintw_is_utf8(TermWin *tw)
{
return is_utf8();
}
static bool win_seat_is_utf8(Seat *seat)
{
return is_utf8();
}
char *win_seat_get_ttymode(Seat *seat, const char *mode)
{
return term_get_ttymode(term, mode);
}
bool win_seat_get_window_pixel_size(Seat *seat, int *x, int *y)
{
win_get_pixels(wintw, x, y);
return true;
}
StripCtrlChars *win_seat_stripctrl_new(
Seat *seat, BinarySink *bs_out, SeatInteractionContext sic)
{
return stripctrl_new_term(bs_out, false, 0, term);
}
static size_t win_seat_output(
Seat *seat, bool is_stderr, const void *, size_t);
static bool win_seat_eof(Seat *seat);
static int win_seat_get_userpass_input(
Seat *seat, prompts_t *p, bufchain *input);
static void win_seat_notify_remote_exit(Seat *seat);
static void win_seat_connection_fatal(Seat *seat, const char *msg);
static void win_seat_update_specials_menu(Seat *seat);
static void win_seat_set_busy_status(Seat *seat, BusyStatus status);
static bool win_seat_set_trust_status(Seat *seat, bool trusted);
static bool win_seat_get_cursor_position(Seat *seat, int *x, int *y);
static const SeatVtable win_seat_vt = {
.output = win_seat_output,
.eof = win_seat_eof,
.get_userpass_input = win_seat_get_userpass_input,
.notify_remote_exit = win_seat_notify_remote_exit,
.connection_fatal = win_seat_connection_fatal,
.update_specials_menu = win_seat_update_specials_menu,
.get_ttymode = win_seat_get_ttymode,
.set_busy_status = win_seat_set_busy_status,
.verify_ssh_host_key = win_seat_verify_ssh_host_key,
.confirm_weak_crypto_primitive = win_seat_confirm_weak_crypto_primitive,
.confirm_weak_cached_hostkey = win_seat_confirm_weak_cached_hostkey,
.is_utf8 = win_seat_is_utf8,
.echoedit_update = nullseat_echoedit_update,
.get_x_display = nullseat_get_x_display,
.get_windowid = nullseat_get_windowid,
.get_window_pixel_size = win_seat_get_window_pixel_size,
.stripctrl_new = win_seat_stripctrl_new,
.set_trust_status = win_seat_set_trust_status,
.verbose = nullseat_verbose_yes,
.interactive = nullseat_interactive_yes,
.get_cursor_position = win_seat_get_cursor_position,
};
static WinGuiSeat wgs = { .seat.vt = &win_seat_vt,
.logpolicy.vt = &win_gui_logpolicy_vt };
static void start_backend(void)
{
const struct BackendVtable *vt;
const char *title;
char *error, *realhost;
int i;
/*
* Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant.
*/
vt = backend_vt_from_proto(conf_get_int(conf, CONF_protocol));
if (!vt) {
char *str = dupprintf("%s Internal Error", appname);
MessageBox(NULL, "Unsupported protocol number found",
str, MB_OK | MB_ICONEXCLAMATION);
sfree(str);
cleanup_exit(1);
}
seat_set_trust_status(&wgs.seat, true);
error = backend_init(vt, &wgs.seat, &backend, logctx, conf,
conf_get_str(conf, CONF_host),
conf_get_int(conf, CONF_port),
&realhost,
conf_get_bool(conf, CONF_tcp_nodelay),
conf_get_bool(conf, CONF_tcp_keepalives));
if (error) {
char *str = dupprintf("%s Error", appname);
char *msg = dupprintf("Unable to open connection to\n%s\n%s",
conf_dest(conf), error);
sfree(error);
MessageBox(NULL, msg, str, MB_ICONERROR | MB_OK);
sfree(str);
sfree(msg);
exit(0);
}
window_name = icon_name = NULL;
char *title_to_free = NULL;
title = conf_get_str(conf, CONF_wintitle);
if (!*title) {
title_to_free = dupprintf("%s - %s", realhost, appname);
title = title_to_free;
}
sfree(realhost);
win_set_title(wintw, title);
win_set_icon_title(wintw, title);
/*
* Connect the terminal to the backend for resize purposes.
*/
term_provide_backend(term, backend);
/*
* Set up a line discipline.
*/
ldisc = ldisc_create(conf, term, backend, &wgs.seat);
/*
* Destroy the Restart Session menu item. (This will return
* failure if it's already absent, as it will be the very first
* time we call this function. We ignore that, because as long
* as the menu item ends up not being there, we don't care
* whether it was us who removed it or not!)
*/
for (i = 0; i < lenof(popup_menus); i++) {
DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
}
session_closed = false;
sfree(title_to_free);
}
static void close_session(void *ignored_context)
{
char *newtitle;
int i;
session_closed = true;
newtitle = dupprintf("%s (inactive)", appname);
win_set_icon_title(wintw, newtitle);
win_set_title(wintw, newtitle);
sfree(newtitle);
if (ldisc) {
ldisc_free(ldisc);
ldisc = NULL;
}
if (backend) {
backend_free(backend);
backend = NULL;
term_provide_backend(term, NULL);
seat_update_specials_menu(&wgs.seat);
}
/*
* Show the Restart Session menu item. Do a precautionary
* delete first to ensure we never end up with more than one.
*/
for (i = 0; i < lenof(popup_menus); i++) {
DeleteMenu(popup_menus[i].menu, IDM_RESTART, MF_BYCOMMAND);
InsertMenu(popup_menus[i].menu, IDM_DUPSESS, MF_BYCOMMAND | MF_ENABLED,
IDM_RESTART, "&Restart Session");
}
}
const unsigned cmdline_tooltype =
TOOLTYPE_HOST_ARG |
TOOLTYPE_PORT_ARG |
TOOLTYPE_NO_VERBOSE_OPTION;
HINSTANCE hinst;
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
{
MSG msg;
HRESULT hr;
int guess_width, guess_height;
dll_hijacking_protection();
hinst = inst;
sk_init();
init_common_controls();
/* Set Explicit App User Model Id so that jump lists don't cause
PuTTY to hang on to removable media. */
set_explicit_app_user_model_id();
/* Ensure a Maximize setting in Explorer doesn't maximise the
* config box. */
defuse_showwindow();
init_winver();
/*
* If we're running a version of Windows that doesn't support
* WM_MOUSEWHEEL, find out what message number we should be
* using instead.
*/
if (osMajorVersion < 4 ||
(osMajorVersion == 4 && osPlatformId != VER_PLATFORM_WIN32_NT))
wm_mousewheel = RegisterWindowMessage("MSWHEEL_ROLLMSG");
init_help();
init_winfuncs();
conf = conf_new();
/*
* Initialize COM.
*/
hr = CoInitialize(NULL);
if (hr != S_OK && hr != S_FALSE) {
char *str = dupprintf("%s Fatal Error", appname);
MessageBox(NULL, "Failed to initialize COM subsystem",
str, MB_OK | MB_ICONEXCLAMATION);
sfree(str);
return 1;
}
/*
* Process the command line.
*/
{
char *p;
bool special_launchable_argument = false;
settings_set_default_protocol(be_default_protocol);
/* Find the appropriate default port. */
{
const struct BackendVtable *vt =
backend_vt_from_proto(be_default_protocol);
settings_set_default_port(0); /* illegal */
if (vt)
settings_set_default_port(vt->default_port);
}
conf_set_int(conf, CONF_logtype, LGTYP_NONE);
do_defaults(NULL, conf);
p = cmdline;
/*
* Process a couple of command-line options which are more
* easily dealt with before the line is broken up into words.
* These are the old-fashioned but convenient @sessionname and
* the internal-use-only &sharedmemoryhandle, plus the &R
* prefix for -restrict-acl, all of which are used by PuTTYs
* auto-launching each other via System-menu options.
*/
while (*p && isspace(*p))
p++;
if (*p == '&' && p[1] == 'R' &&
(!p[2] || p[2] == '@' || p[2] == '&')) {
/* &R restrict-acl prefix */
restrict_process_acl();
p += 2;
}
if (*p == '@') {
/*
* An initial @ means that the whole of the rest of the
* command line should be treated as the name of a saved
* session, with _no quoting or escaping_. This makes it a
* very convenient means of automated saved-session
* launching, via IDM_SAVEDSESS or Windows 7 jump lists.
*/
int i = strlen(p);
while (i > 1 && isspace(p[i - 1]))
i--;
p[i] = '\0';
do_defaults(p + 1, conf);
if (!conf_launchable(conf) && !do_config(conf)) {
cleanup_exit(0);
}
special_launchable_argument = true;
} else if (*p == '&') {
/*
* An initial & means we've been given a command line
* containing the hex value of a HANDLE for a file
* mapping object, which we must then interpret as a
* serialised Conf.
*/
HANDLE filemap;
void *cp;
unsigned cpsize;
if (sscanf(p + 1, "%p:%u", &filemap, &cpsize) == 2 &&
(cp = MapViewOfFile(filemap, FILE_MAP_READ,
0, 0, cpsize)) != NULL) {
BinarySource src[1];
BinarySource_BARE_INIT(src, cp, cpsize);
if (!conf_deserialise(conf, src))
modalfatalbox("Serialised configuration data was invalid");
UnmapViewOfFile(cp);
CloseHandle(filemap);
} else if (!do_config(conf)) {
cleanup_exit(0);
}
special_launchable_argument = true;
} else if (!*p) {
/* Do-nothing case for an empty command line - or rather,
* for a command line that's empty _after_ we strip off
* the &R prefix. */
} else {
/*
* Otherwise, break up the command line and deal with
* it sensibly.
*/
int argc, i;
char **argv;
split_into_argv(cmdline, &argc, &argv, NULL);
for (i = 0; i < argc; i++) {
char *p = argv[i];
int ret;
ret = cmdline_process_param(p, i+1<argc?argv[i+1]:NULL,
1, conf);
if (ret == -2) {
cmdline_error("option \"%s\" requires an argument", p);
} else if (ret == 2) {
i++; /* skip next argument */
} else if (ret == 1) {
continue; /* nothing further needs doing */
} else if (!strcmp(p, "-cleanup")) {
/*
* `putty -cleanup'. Remove all registry
* entries associated with PuTTY, and also find
* and delete the random seed file.
*/
char *s1, *s2;
s1 = dupprintf("This procedure will remove ALL Registry entries\n"
"associated with %s, and will also remove\n"
"the random seed file. (This only affects the\n"
"currently logged-in user.)\n"
"\n"
"THIS PROCESS WILL DESTROY YOUR SAVED SESSIONS.\n"
"Are you really sure you want to continue?",
appname);
s2 = dupprintf("%s Warning", appname);
if (message_box(NULL, s1, s2,
MB_YESNO | MB_ICONWARNING | MB_DEFBUTTON2,
HELPCTXID(option_cleanup)) == IDYES) {
cleanup_all();
}
sfree(s1);
sfree(s2);
exit(0);
} else if (!strcmp(p, "-pgpfp")) {
pgp_fingerprints_msgbox(NULL);
exit(1);
} else if (*p != '-') {
cmdline_error("unexpected argument \"%s\"", p);
} else {
cmdline_error("unknown option \"%s\"", p);
}
}
}
cmdline_run_saved(conf);
/*
* Bring up the config dialog if the command line hasn't
* (explicitly) specified a launchable configuration.
*/
if (!(special_launchable_argument || cmdline_host_ok(conf))) {
if (!do_config(conf))
cleanup_exit(0);
}
prepare_session(conf);
}
if (!prev) {
WNDCLASSW wndclass;
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = inst;
wndclass.hIcon = LoadIcon(inst, MAKEINTRESOURCE(IDI_MAINICON));
wndclass.hCursor = LoadCursor(NULL, IDC_IBEAM);
wndclass.hbrBackground = NULL;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, appname);
RegisterClassW(&wndclass);
}
memset(&ucsdata, 0, sizeof(ucsdata));
conf_cache_data();
conftopalette();
/*
* Guess some defaults for the window size. This all gets
* updated later, so we don't really care too much. However, we
* do want the font width/height guesses to correspond to a
* large font rather than a small one...
*/
font_width = 10;
font_height = 20;
extra_width = 25;
extra_height = 28;
guess_width = extra_width + font_width * conf_get_int(conf, CONF_width);
guess_height = extra_height + font_height*conf_get_int(conf, CONF_height);
{
RECT r;
get_fullscreen_rect(&r);
if (guess_width > r.right - r.left)
guess_width = r.right - r.left;
if (guess_height > r.bottom - r.top)
guess_height = r.bottom - r.top;
}
{
int winmode = WS_OVERLAPPEDWINDOW | WS_VSCROLL;
int exwinmode = 0;
const struct BackendVtable *vt =
backend_vt_from_proto(be_default_protocol);
bool resize_forbidden = false;
if (vt && vt->flags & BACKEND_RESIZE_FORBIDDEN)
resize_forbidden = true;
wchar_t *uappname = dup_mb_to_wc(DEFAULT_CODEPAGE, 0, appname);
if (!conf_get_bool(conf, CONF_scrollbar))
winmode &= ~(WS_VSCROLL);
if (conf_get_int(conf, CONF_resize_action) == RESIZE_DISABLED ||
resize_forbidden)
winmode &= ~(WS_THICKFRAME | WS_MAXIMIZEBOX);
if (conf_get_bool(conf, CONF_alwaysontop))
exwinmode |= WS_EX_TOPMOST;
if (conf_get_bool(conf, CONF_sunken_edge))
exwinmode |= WS_EX_CLIENTEDGE;
wgs.term_hwnd = CreateWindowExW(
exwinmode, uappname, uappname, winmode, CW_USEDEFAULT,
CW_USEDEFAULT, guess_width, guess_height, NULL, NULL, inst, NULL);
sfree(uappname);
}
/*
* Initialise the fonts, simultaneously correcting the guesses
* for font_{width,height}.
*/
init_fonts(0,0);
/*
* Initialise the terminal. (We have to do this _after_
* creating the window, since the terminal is the first thing
* which will call schedule_timer(), which will in turn call
* timer_change_notify() which will expect hwnd to exist.)
*/
wintw->vt = &windows_termwin_vt;
term = term_init(conf, &ucsdata, wintw);
setup_clipboards(term, conf);
logctx = log_init(&wgs.logpolicy, conf);
term_provide_logctx(term, logctx);
term_size(term, conf_get_int(conf, CONF_height),
conf_get_int(conf, CONF_width),
conf_get_int(conf, CONF_savelines));
/*
* Correct the guesses for extra_{width,height}.
*/
{
RECT cr, wr;
GetWindowRect(wgs.term_hwnd, &wr);
GetClientRect(wgs.term_hwnd, &cr);
offset_width = offset_height = conf_get_int(conf, CONF_window_border);
extra_width = wr.right - wr.left - cr.right + cr.left + offset_width*2;
extra_height = wr.bottom - wr.top - cr.bottom + cr.top +offset_height*2;
}
/*
* Resize the window, now we know what size we _really_ want it
* to be.
*/
guess_width = extra_width + font_width * term->cols;
guess_height = extra_height + font_height * term->rows;
SetWindowPos(wgs.term_hwnd, NULL, 0, 0, guess_width, guess_height,
SWP_NOMOVE | SWP_NOREDRAW | SWP_NOZORDER);
/*
* Set up a caret bitmap, with no content.
*/
{
char *bits;
int size = (font_width + 15) / 16 * 2 * font_height;
bits = snewn(size, char);
memset(bits, 0, size);
caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
sfree(bits);
}
CreateCaret(wgs.term_hwnd, caretbm, font_width, font_height);
/*
* Initialise the scroll bar.
*/
{
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = term->rows - 1;
si.nPage = term->rows;
si.nPos = 0;
SetScrollInfo(wgs.term_hwnd, SB_VERT, &si, false);
}
/*
* Prepare the mouse handler.
*/
lastact = MA_NOTHING;
lastbtn = MBT_NOTHING;
dbltime = GetDoubleClickTime();
/*
* Set up the session-control options on the system menu.
*/
{
HMENU m;
int j;
char *str;
popup_menus[SYSMENU].menu = GetSystemMenu(wgs.term_hwnd, false);
popup_menus[CTXMENU].menu = CreatePopupMenu();
AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_COPY, "&Copy");
AppendMenu(popup_menus[CTXMENU].menu, MF_ENABLED, IDM_PASTE, "&Paste");
savedsess_menu = CreateMenu();
get_sesslist(&sesslist, true);
update_savedsess_menu();
for (j = 0; j < lenof(popup_menus); j++) {
m = popup_menus[j].menu;
AppendMenu(m, MF_SEPARATOR, 0, 0);
AppendMenu(m, MF_ENABLED, IDM_SHOWLOG, "&Event Log");
AppendMenu(m, MF_SEPARATOR, 0, 0);
AppendMenu(m, MF_ENABLED, IDM_NEWSESS, "Ne&w Session...");
AppendMenu(m, MF_ENABLED, IDM_DUPSESS, "&Duplicate Session");
AppendMenu(m, MF_POPUP | MF_ENABLED, (UINT_PTR) savedsess_menu,
"Sa&ved Sessions");
AppendMenu(m, MF_ENABLED, IDM_RECONF, "Chan&ge Settings...");
AppendMenu(m, MF_SEPARATOR, 0, 0);
AppendMenu(m, MF_ENABLED, IDM_COPYALL, "C&opy All to Clipboard");
AppendMenu(m, MF_ENABLED, IDM_CLRSB, "C&lear Scrollback");
AppendMenu(m, MF_ENABLED, IDM_RESET, "Rese&t Terminal");
AppendMenu(m, MF_SEPARATOR, 0, 0);
AppendMenu(m, (conf_get_int(conf, CONF_resize_action)
== RESIZE_DISABLED) ? MF_GRAYED : MF_ENABLED,
IDM_FULLSCREEN, "&Full Screen");
AppendMenu(m, MF_SEPARATOR, 0, 0);
if (has_help())
AppendMenu(m, MF_ENABLED, IDM_HELP, "&Help");
str = dupprintf("&About %s", appname);
AppendMenu(m, MF_ENABLED, IDM_ABOUT, str);
sfree(str);
}
}
if (restricted_acl()) {
lp_eventlog(&wgs.logpolicy, "Running with restricted process ACL");
}
winselgui_set_hwnd(wgs.term_hwnd);
start_backend();
/*
* Set up the initial input locale.
*/
set_input_locale(GetKeyboardLayout(0));
/*
* Finally show the window!
*/
ShowWindow(wgs.term_hwnd, show);
SetForegroundWindow(wgs.term_hwnd);
/*
* Set the palette up.
*/
pal = NULL;
logpal = NULL;
init_palette();
term_set_focus(term, GetForegroundWindow() == wgs.term_hwnd);
UpdateWindow(wgs.term_hwnd);
while (1) {
HANDLE *handles;
int nhandles, n;
DWORD timeout;
if (toplevel_callback_pending() ||
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
/*
* If we have anything we'd like to do immediately, set
* the timeout for MsgWaitForMultipleObjects to zero so
* that we'll only do a quick check of our handles and
* then get on with whatever that was.
*
* One such option is a pending toplevel callback. The
* other is a non-empty Windows message queue, which you'd
* think we could leave to MsgWaitForMultipleObjects to
* check for us along with all the handles, but in fact we
* can't because once PeekMessage in one iteration of this
* loop has removed a message from the queue, the whole
* queue is considered uninteresting by the next
* invocation of MWFMO. So we check ourselves whether the
* message queue is non-empty, and if so, set this timeout
* to zero to ensure MWFMO doesn't block.
*/
timeout = 0;
} else {
timeout = INFINITE;
/* The messages seem unreliable; especially if we're being tricky */
term_set_focus(term, GetForegroundWindow() == wgs.term_hwnd);
}
handles = handle_get_events(&nhandles);
n = MsgWaitForMultipleObjects(nhandles, handles, false,
timeout, QS_ALLINPUT);
if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
handle_got_event(handles[n - WAIT_OBJECT_0]);
sfree(handles);
} else
sfree(handles);
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT)
goto finished; /* two-level break */
HWND logbox = event_log_window();
if (!(IsWindow(logbox) && IsDialogMessage(logbox, &msg)))
DispatchMessageW(&msg);
/*
* WM_NETEVENT messages seem to jump ahead of others in
* the message queue. I'm not sure why; the docs for
* PeekMessage mention that messages are prioritised in
* some way, but I'm unclear on which priorities go where.
*
* Anyway, in practice I observe that WM_NETEVENT seems to
* jump to the head of the queue, which means that if we
* were to only process one message every time round this
* loop, we'd get nothing but NETEVENTs if the server
* flooded us with data, and stop responding to any other
* kind of window message. So instead, we keep on round
* this loop until we've consumed at least one message
* that _isn't_ a NETEVENT, or run out of messages
* completely (whichever comes first). And we don't go to
* run_toplevel_callbacks (which is where the netevents
* are actually processed, causing fresh NETEVENT messages
* to appear) until we've done this.
*/
if (msg.message != WM_NETEVENT)
break;
}
run_toplevel_callbacks();
}
finished:
cleanup_exit(msg.wParam); /* this doesn't return... */
return msg.wParam; /* ... but optimiser doesn't know */
}
static void setup_clipboards(Terminal *term, Conf *conf)
{
assert(term->mouse_select_clipboards[0] == CLIP_LOCAL);
term->n_mouse_select_clipboards = 1;
if (conf_get_bool(conf, CONF_mouseautocopy)) {
term->mouse_select_clipboards[
term->n_mouse_select_clipboards++] = CLIP_SYSTEM;
}
switch (conf_get_int(conf, CONF_mousepaste)) {
case CLIPUI_IMPLICIT: