Repository navigation
Expand file tree
/
Copy pathWindow.c
More file actions
2503 lines (2234 loc) · 70.9 KB
/
Copy pathWindow.c
File metadata and controls
2503 lines (2234 loc) · 70.9 KB
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
#include <windows.h>
#include <imm.h>
#include <commctrl.h>
#include <winuser.h>
#ifndef AUTO_WINSOCK
#define AUTO_WINSOCK
//#include <winsock2.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define PUTTY_DO_GLOBALS /* actually _define_ globals */
#include "putty.h"
#include "winstuff.h"
#include "storage.h"
#include "win_res.h"
#include "powhook.h"
#include "powdefines.h"
#include "powmain.h"
#include "powbeam.h"
#define IDM_SHOWLOG 0x0010
#define IDM_NEWSESS 0x0020
#define IDM_DUPSESS 0x0030
#define IDM_RECONF 0x0040
#define IDM_CLRSB 0x0050
#define IDM_RESET 0x0060
#define IDM_TEL_AYT 0x0070
#define IDM_TEL_BRK 0x0080
#define IDM_TEL_SYNCH 0x0090
#define IDM_TEL_EC 0x00a0
#define IDM_TEL_EL 0x00b0
#define IDM_TEL_GA 0x00c0
#define IDM_TEL_NOP 0x00d0
#define IDM_TEL_ABORT 0x00e0
#define IDM_TEL_AO 0x00f0
#define IDM_TEL_IP 0x0100
#define IDM_TEL_SUSP 0x0110
#define IDM_TEL_EOR 0x0120
#define IDM_TEL_EOF 0x0130
#define IDM_ABOUT 0x0140
#define IDM_SAVEDSESS 0x0150
#define IDM_COPYALL 0x0160
#define IDM_SAVED_MIN 0x1000
#define IDM_SAVED_MAX 0x2000
#define WM_IGNORE_SIZE (WM_XUSER + 1)
#define WM_IGNORE_CLIP (WM_XUSER + 2)
/* Needed for Chinese support and apparently not always defined. */
#ifndef VK_PROCESSKEY
#define VK_PROCESSKEY 0xE5
#endif
static LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
static int TranslateKey(UINT message, WPARAM wParam, LPARAM lParam, unsigned char *output);
static void cfgtopalette(void);
static void init_palette(void);
static void init_fonts(int);
static int extra_width, extra_height;
static int pending_netevent = 0;
static WPARAM pend_netevent_wParam = 0;
static LPARAM pend_netevent_lParam = 0;
static void enact_pending_netevent(void);
static clock_t last_movement = 0;
#define FONT_NORMAL 0
#define FONT_BOLD 1
#define FONT_UNDERLINE 2
#define FONT_BOLDUND 3
#define FONT_OEM 4
#define FONT_OEMBOLD 5
#define FONT_OEMBOLDUND 6
#define FONT_OEMUND 7
static HFONT fonts[8];
static int font_needs_hand_underlining;
static enum {
BOLD_COLOURS, BOLD_SHADOW, BOLD_FONT
} bold_mode;
static enum {
UND_LINE, UND_FONT
} und_mode;
static int descent;
#define NCOLOURS 24
static COLORREF colours[NCOLOURS];
static HPALETTE pal;
static LPLOGPALETTE logpal;
static RGBTRIPLE defpal[NCOLOURS];
static HWND hwnd;
static HBITMAP caretbm;
static int dbltime, lasttime, lastact;
static Mouse_Button lastbtn;
static char *window_name, *icon_name;
static Ldisc *real_ldisc;
static int compose_state = 0;
static clock_t edit_timer=0;
void begin_session(void) {
ldisc = real_ldisc;
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) {
static char appname[] = "POWTTY";
WORD winsock_ver;
WSADATA wsadata;
WNDCLASS wndclass;
MSG msg;
int guess_width, guess_height;
hinst = inst;
flags = FLAG_VERBOSE | FLAG_INTERACTIVE;
winsock_ver = MAKEWORD(1, 1);
if (WSAStartup(winsock_ver, &wsadata)) {
MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
MB_OK | MB_ICONEXCLAMATION);
return 1;
}
if (LOBYTE(wsadata.wVersion) != 1 || HIBYTE(wsadata.wVersion) != 1) {
MessageBox(NULL, "WinSock version is incompatible with 1.1",
"WinSock Error", MB_OK | MB_ICONEXCLAMATION);
WSACleanup();
return 1;
}
/* WISHLIST: maybe allow config tweaking even if winsock not present? */
sk_init();
//InitCommonControlsEx();
/* Ensure a Maximize setting in Explorer doesn't maximise the
* config box. */
defuse_showwindow();
/*
* Process the command line.
*/
{
char *p;
int ret;
default_protocol = DEFAULT_PROTOCOL;
default_port = DEFAULT_PORT;
ret=do_defaults(NULL, &cfg);
p = cmdline;
while (*p && isspace(*p)) p++;
/*
* Process command line options first. Yes, this can be
* done better, and it will be as soon as I have the
* energy...
*/
/*
* An initial @ means to activate a saved session.
*/
if (*p) { // != '\0') {
int i = strlen(p);
while (i > 1 && isspace(p[i-1]))
i--;
p[i] = '\0';
ret=do_defaults (p, &cfg);
if (ret==1) {
sprintf(errtext,"Loaded Profile: %s\r\n",p);
} else {
sprintf(errtext,"Invalid Profile Specified: %s\r\nDefault Profile Loaded\r\n",p);
}
from_backend(0,errtext,strlen(errtext));
if (!*cfg.host && !do_config()) {
WSACleanup();
return 0;
}
} else {
if (!do_config()) {
WSACleanup();
return 0;
}
}
}
/*
* Select protocol. This is farmed out into a table in a
* separate file to enable an ssh-free variant.
*/
{
int i;
back = NULL;
for (i = 0; backends[i].backend != NULL; i++)
if (backends[i].protocol == cfg.protocol) {
back = backends[i].backend;
break;
}
if (back == NULL) {
MessageBox(NULL, "Unsupported protocol number found",
"PuTTY Internal Error", MB_OK | MB_ICONEXCLAMATION);
WSACleanup();
return 1;
}
}
real_ldisc = (cfg.ldisc_term ? &ldisc_term : &ldisc_simple);
/* To start with, we use the simple line discipline, so we can
* type passwords etc without fear of them being echoed... */
ldisc = &ldisc_simple;
if (!prev) {
wndclass.style = 0;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = inst;
wndclass.hIcon = LoadIcon (inst,
MAKEINTRESOURCE(IDI_POWTTYICON));
wndclass.hCursor = LoadCursor (NULL, IDC_IBEAM);
wndclass.hbrBackground = GetStockObject (BLACK_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = appname;
RegisterClass (&wndclass);
}
hwnd = NULL;
savelines = cfg.savelines;
term_init();
cfgtopalette();
/*
* 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;
term_size (cfg.height, cfg.width, cfg.savelines);
guess_width = extra_width + font_width * cols;
guess_height = extra_height + font_height * rows;
{
RECT r;
HWND w = GetDesktopWindow();
GetWindowRect (w, &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;
if (!cfg.scrollbar) winmode &= ~(WS_VSCROLL);
if (cfg.locksize) winmode &= ~(WS_THICKFRAME|WS_MAXIMIZEBOX);
if (cfg.alwaysontop) exwinmode = WS_EX_TOPMOST;
hwnd = CreateWindowEx (exwinmode, appname, appname,
winmode, cfg.xPos,cfg.yPos,
guess_width, guess_height,
NULL, NULL, inst, NULL);
}
/*
* Initialise the fonts, simultaneously correcting the guesses
* for font_{width,height}.
*/
bold_mode = cfg.bold_colour ? BOLD_COLOURS : BOLD_FONT;
und_mode = UND_FONT;
init_fonts(0);
/*
* Correct the guesses for extra_{width,height}.
*/
{
RECT cr, wr;
GetWindowRect (hwnd, &wr);
GetClientRect (hwnd, &cr);
extra_width = wr.right - wr.left - cr.right + cr.left;
extra_height = wr.bottom - wr.top - cr.bottom + cr.top;
}
/*
* Resize the window, now we know what size we _really_ want it
* to be.
*/
guess_width = extra_width + font_width * cols;
guess_height = extra_height + font_height * rows;
SendMessage (hwnd, WM_IGNORE_SIZE, 0, 0);
SetWindowPos (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 = smalloc(size);
memset(bits, 0, size);
caretbm = CreateBitmap(font_width, font_height, 1, 1, bits);
sfree(bits);
}
/*
* Initialise the scroll bar.
*/
{
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_ALL | SIF_DISABLENOSCROLL;
si.nMin = 0;
si.nMax = rows-1;
si.nPage = rows;
si.nPos = 0;
SetScrollInfo (hwnd, SB_VERT, &si, FALSE);
}
/*
* Start up the telnet connection.
*/
{
char *error;
char msg[1024], *title;
char *realhost;
error = back->init (cfg.host, cfg.port, &realhost);
if (error) {
sprintf(msg, "Unable to open connection:\n%s", error);
MessageBox(NULL, msg, "PuTTY Error", MB_ICONERROR | MB_OK);
return 0;
}
window_name = icon_name = NULL;
if (*cfg.wintitle) {
title = cfg.wintitle;
} else {
sprintf(msg, "%s - PowTTY (PuTTY)", realhost);
title = msg;
}
set_title (title);
set_icon (title);
}
session_closed = FALSE;
/*
* Set up the input and output buffers.
*/
inbuf_head = 0;
outbuf_reap = outbuf_head = 0;
/*
* Prepare the mouse handler.
*/
lastact = MA_NOTHING;
lastbtn = MB_NOTHING;
dbltime = GetDoubleClickTime();
/*
* Set up the session-control options on the system menu.
*/
{
HMENU m = GetSystemMenu (hwnd, FALSE);
//HMENU p,s;
HMENU s;
int i;
AppendMenu (m, MF_SEPARATOR, 0, 0);
/*
if (cfg.protocol == PROT_TELNET) {
p = CreateMenu();
AppendMenu (p, MF_ENABLED, IDM_TEL_AYT, "Are You There");
AppendMenu (p, MF_ENABLED, IDM_TEL_BRK, "Break");
AppendMenu (p, MF_ENABLED, IDM_TEL_SYNCH, "Synch");
AppendMenu (p, MF_SEPARATOR, 0, 0);
AppendMenu (p, MF_ENABLED, IDM_TEL_EC, "Erase Character");
AppendMenu (p, MF_ENABLED, IDM_TEL_EL, "Erase Line");
AppendMenu (p, MF_ENABLED, IDM_TEL_GA, "Go Ahead");
AppendMenu (p, MF_ENABLED, IDM_TEL_NOP, "No Operation");
AppendMenu (p, MF_SEPARATOR, 0, 0);
AppendMenu (p, MF_ENABLED, IDM_TEL_ABORT, "Abort Process");
AppendMenu (p, MF_ENABLED, IDM_TEL_AO, "Abort Output");
AppendMenu (p, MF_ENABLED, IDM_TEL_IP, "Interrupt Process");
AppendMenu (p, MF_ENABLED, IDM_TEL_SUSP, "Suspend Process");
AppendMenu (p, MF_SEPARATOR, 0, 0);
AppendMenu (p, MF_ENABLED, IDM_TEL_EOR, "End Of Record");
AppendMenu (p, MF_ENABLED, IDM_TEL_EOF, "End Of File");
AppendMenu (m, MF_POPUP | MF_ENABLED, (UINT) p, "Telnet Command");
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");
s = CreateMenu();
get_sesslist(TRUE);
for (i = 1 ; i < ((nsessions < 256) ? nsessions : 256) ; i++)
AppendMenu (s, MF_ENABLED, IDM_SAVED_MIN + (16 * i) , sessions[i]);
AppendMenu (m, MF_POPUP | MF_ENABLED, (UINT) s, "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, MF_ENABLED, IDM_ABOUT, "&About PuTTY");
}
/*
* Finally show the window!
*/
ShowWindow (hwnd, show);
/*
* Set the palette up.
*/
pal = NULL;
logpal = NULL;
init_palette();
has_focus = (GetForegroundWindow() == hwnd);
UpdateWindow (hwnd);
if (GetMessage (&msg, NULL, 0, 0) == 1)
{
int timer_id = 0, long_timer = 0;
/*+ let powwow initialize */
powmain();
while (msg.message != WM_QUIT) {
/* Sometimes DispatchMessage calls routines that use their own
* GetMessage loop, setup this timer so we get some control back.
*
* Also call term_update() from the timer so that if the host
* is sending data flat out we still do redraws.
*/
if(timer_id && long_timer) {
KillTimer(hwnd, timer_id);
long_timer = timer_id = 0;
}
if(!timer_id)
timer_id = SetTimer(hwnd, 1, 20, NULL);
DispatchMessage (&msg);
/* Make sure we blink everything that needs it. */
term_blink(0);
/* Send the paste buffer if there's anything to send */
term_paste();
/* If there's nothing new in the queue then we can do everything
* we've delayed, reading the socket, writing, and repainting
* the window.
*/
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
continue;
if (pending_netevent) {
enact_pending_netevent();
/* Force the cursor blink on */
term_blink(1);
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
continue;
}
/* Okay there is now nothing to do so we make sure the screen is
* completely up to date then tell windows to call us in a little
* while.
*/
if (timer_id) {
KillTimer(hwnd, timer_id);
timer_id = 0;
}
HideCaret(hwnd);
if (inbuf_head)
term_out(0);
term_update();
ShowCaret(hwnd);
if (!has_focus)
timer_id = SetTimer(hwnd, 1, 59500, NULL);
else
timer_id = SetTimer(hwnd, 1, 100, NULL);
long_timer = 1;
/* There's no point rescanning everything in the message queue
* so we do an apperently unneccesary wait here
*/
WaitMessage();
if (GetMessage (&msg, NULL, 0, 0) != 1)
break;
}
}
/*
* Clean up.
*/
{
int i;
for (i=0; i<8; i++)
if (fonts[i])
DeleteObject(fonts[i]);
}
sfree(logpal);
if (pal)
DeleteObject(pal);
WSACleanup();
if (cfg.protocol == PROT_SSH) {
random_save_seed();
}
return msg.wParam;
}
/*
* Set up, or shut down, an AsyncSelect. Called from winnet.c.
*/
char *do_select(SOCKET skt, int startup) {
int msg, events;
if (startup) {
msg = WM_NETEVENT;
events = FD_READ | FD_WRITE | FD_OOB | FD_CLOSE;
} else {
msg = events = 0;
}
if (!hwnd)
return "do_select(): internal error (hwnd==NULL)";
if (WSAAsyncSelect (skt, hwnd, msg, events) == SOCKET_ERROR) {
switch (WSAGetLastError()) {
case WSAENETDOWN: return "Network is down";
default: return "WSAAsyncSelect(): unknown error";
}
}
return NULL;
}
/*
* Print a message box and close the connection.
*/
void connection_fatal(char *fmt, ...) {
va_list ap;
char stuff[200];
va_start(ap, fmt);
vsprintf(stuff, fmt, ap);
va_end(ap);
MessageBox(hwnd, stuff, "PuTTY Fatal Error", MB_ICONERROR | MB_OK);
if (cfg.close_on_exit)
PostQuitMessage(1);
else {
session_closed = TRUE;
SetWindowText (hwnd, "PuTTY (inactive)");
}
}
/*
* Actually do the job requested by a WM_NETEVENT
*/
static void enact_pending_netevent(void) {
static int reentering = 0;
extern int select_result(WPARAM, LPARAM);
int ret;
if (reentering)
return; /* don't unpend the pending */
pending_netevent = FALSE;
reentering = 1;
ret = select_result (pend_netevent_wParam, pend_netevent_lParam);
reentering = 0;
if (ret == 0) {
if (cfg.close_on_exit)
PostQuitMessage(0);
else {
session_closed = TRUE;
MessageBox(hwnd, "Connection closed by remote host",
"PuTTY", MB_OK | MB_ICONINFORMATION);
SetWindowText (hwnd, "PuTTY (inactive)");
}
}
}
/*
* Copy the colour palette from the configuration data into defpal.
* This is non-trivial because the colour indices are different.
*/
static void cfgtopalette(void) {
int i;
static const int ww[] = {
6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21,
0, 1, 2, 3, 4, 4, 5, 5
};
for (i=0; i<24; i++) {
int w = ww[i];
defpal[i].rgbtRed = cfg.colours[w][0];
defpal[i].rgbtGreen = cfg.colours[w][1];
defpal[i].rgbtBlue = cfg.colours[w][2];
}
}
/*
* Set up the colour palette.
*/
static void init_palette(void) {
int i;
HDC hdc = GetDC (hwnd);
if (hdc) {
if (cfg.try_palette &&
GetDeviceCaps (hdc, RASTERCAPS) & RC_PALETTE) {
logpal = smalloc(sizeof(*logpal)
- sizeof(logpal->palPalEntry)
+ NCOLOURS * sizeof(PALETTEENTRY));
logpal->palVersion = 0x300;
logpal->palNumEntries = NCOLOURS;
for (i = 0; i < NCOLOURS; i++) {
logpal->palPalEntry[i].peRed = defpal[i].rgbtRed;
logpal->palPalEntry[i].peGreen = defpal[i].rgbtGreen;
logpal->palPalEntry[i].peBlue = defpal[i].rgbtBlue;
logpal->palPalEntry[i].peFlags = PC_NOCOLLAPSE;
}
pal = CreatePalette (logpal);
if (pal) {
SelectPalette (hdc, pal, FALSE);
RealizePalette (hdc);
SelectPalette (hdc, GetStockObject (DEFAULT_PALETTE),
FALSE);
}
}
ReleaseDC (hwnd, hdc);
}
if (pal)
for (i=0; i<NCOLOURS; i++)
colours[i] = PALETTERGB(defpal[i].rgbtRed,
defpal[i].rgbtGreen,
defpal[i].rgbtBlue);
else
for(i=0; i<NCOLOURS; i++)
colours[i] = RGB(defpal[i].rgbtRed,
defpal[i].rgbtGreen,
defpal[i].rgbtBlue);
}
/*
* Initialise all the fonts we will need. There may be as many as
* eight or as few as one. We also:
*
* - check the font width and height, correcting our guesses if
* necessary.
*
* - verify that the bold font is the same width as the ordinary
* one, and engage shadow bolding if not.
*
* - verify that the underlined font is the same width as the
* ordinary one (manual underlining by means of line drawing can
* be done in a pinch).
*/
static void init_fonts(int pick_width) {
TEXTMETRIC tm;
int i;
int fsize[8];
HDC hdc;
int fw_dontcare, fw_bold;
int firstchar = ' ';
#ifdef CHECKOEMFONT
font_messup:
#endif
for (i=0; i<8; i++)
fonts[i] = NULL;
if (cfg.fontisbold) {
fw_dontcare = FW_BOLD;
fw_bold = FW_HEAVY;
} else {
fw_dontcare = FW_DONTCARE;
fw_bold = FW_BOLD;
}
hdc = GetDC(hwnd);
font_height = cfg.fontheight;
font_width = pick_width;
#define f(i,c,w,u) \
fonts[i] = CreateFont (font_height, font_width, 0, 0, w, FALSE, u, FALSE, \
c, OUT_DEFAULT_PRECIS, \
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, \
FIXED_PITCH | FF_DONTCARE, cfg.font)
if (cfg.vtmode != VT_OEMONLY) {
f(FONT_NORMAL, cfg.fontcharset, fw_dontcare, FALSE);
SelectObject (hdc, fonts[FONT_NORMAL]);
GetTextMetrics(hdc, &tm);
font_height = tm.tmHeight;
font_width = tm.tmAveCharWidth;
f(FONT_UNDERLINE, cfg.fontcharset, fw_dontcare, TRUE);
/*
* Some fonts, e.g. 9-pt Courier, draw their underlines
* outside their character cell. We successfully prevent
* screen corruption by clipping the text output, but then
* we lose the underline completely. Here we try to work
* out whether this is such a font, and if it is, we set a
* flag that causes underlines to be drawn by hand.
*
* Having tried other more sophisticated approaches (such
* as examining the TEXTMETRIC structure or requesting the
* height of a string), I think we'll do this the brute
* force way: we create a small bitmap, draw an underlined
* space on it, and test to see whether any pixels are
* foreground-coloured. (Since we expect the underline to
* go all the way across the character cell, we only search
* down a single column of the bitmap, half way across.)
*/
{
HDC und_dc;
HBITMAP und_bm, und_oldbm;
int i, gotit;
COLORREF c;
und_dc = CreateCompatibleDC(hdc);
und_bm = CreateCompatibleBitmap(hdc, font_width, font_height);
und_oldbm = SelectObject(und_dc, und_bm);
SelectObject(und_dc, fonts[FONT_UNDERLINE]);
SetTextAlign(und_dc, TA_TOP | TA_LEFT | TA_NOUPDATECP);
SetTextColor (und_dc, RGB(255,255,255));
SetBkColor (und_dc, RGB(0,0,0));
SetBkMode (und_dc, OPAQUE);
ExtTextOut (und_dc, 0, 0, ETO_OPAQUE, NULL, " ", 1, NULL);
gotit = FALSE;
for (i = 0; i < font_height; i++) {
c = GetPixel(und_dc, font_width/2, i);
if (c != RGB(0,0,0))
gotit = TRUE;
}
SelectObject(und_dc, und_oldbm);
DeleteObject(und_bm);
DeleteDC(und_dc);
font_needs_hand_underlining = !gotit;
}
if (bold_mode == BOLD_FONT) {
f(FONT_BOLD, cfg.fontcharset, fw_bold, FALSE);
f(FONT_BOLDUND, cfg.fontcharset, fw_bold, TRUE);
}
if (cfg.vtmode == VT_OEMANSI) {
f(FONT_OEM, OEM_CHARSET, fw_dontcare, FALSE);
f(FONT_OEMUND, OEM_CHARSET, fw_dontcare, TRUE);
if (bold_mode == BOLD_FONT) {
f(FONT_OEMBOLD, OEM_CHARSET, fw_bold, FALSE);
f(FONT_OEMBOLDUND, OEM_CHARSET, fw_bold, TRUE);
}
}
}
else
{
f(FONT_OEM, cfg.fontcharset, fw_dontcare, FALSE);
SelectObject (hdc, fonts[FONT_OEM]);
GetTextMetrics(hdc, &tm);
font_height = tm.tmHeight;
font_width = tm.tmAveCharWidth;
f(FONT_OEMUND, cfg.fontcharset, fw_dontcare, TRUE);
if (bold_mode == BOLD_FONT) {
f(FONT_BOLD, cfg.fontcharset, fw_bold, FALSE);
f(FONT_BOLDUND, cfg.fontcharset, fw_bold, TRUE);
}
}
#undef f
descent = tm.tmAscent + 1;
if (descent >= font_height)
descent = font_height - 1;
firstchar = tm.tmFirstChar;
for (i=0; i<8; i++) {
if (fonts[i]) {
if (SelectObject (hdc, fonts[i]) &&
GetTextMetrics(hdc, &tm) )
fsize[i] = tm.tmAveCharWidth + 256 * tm.tmHeight;
else fsize[i] = -i;
}
else fsize[i] = -i;
}
ReleaseDC (hwnd, hdc);
/* ... This is wrong in OEM only mode */
if (fsize[FONT_UNDERLINE] != fsize[FONT_NORMAL] ||
(bold_mode == BOLD_FONT &&
fsize[FONT_BOLDUND] != fsize[FONT_BOLD])) {
und_mode = UND_LINE;
DeleteObject (fonts[FONT_UNDERLINE]);
if (bold_mode == BOLD_FONT)
DeleteObject (fonts[FONT_BOLDUND]);
}
if (bold_mode == BOLD_FONT &&
fsize[FONT_BOLD] != fsize[FONT_NORMAL]) {
bold_mode = BOLD_SHADOW;
DeleteObject (fonts[FONT_BOLD]);
if (und_mode == UND_FONT)
DeleteObject (fonts[FONT_BOLDUND]);
}
#ifdef CHECKOEMFONT
/* With the fascist font painting it doesn't matter if the linedraw font
* isn't exactly the right size anymore so we don't have to check this.
*/
if (cfg.vtmode == VT_OEMANSI && fsize[FONT_OEM] != fsize[FONT_NORMAL] ) {
if( cfg.fontcharset == OEM_CHARSET )
{
MessageBox(NULL, "The OEM and ANSI versions of this font are\n"
"different sizes. Using OEM-only mode instead",
"Font Size Mismatch", MB_ICONINFORMATION | MB_OK);
cfg.vtmode = VT_OEMONLY;
}
else if( firstchar < ' ' )
{
MessageBox(NULL, "The OEM and ANSI versions of this font are\n"
"different sizes. Using XTerm mode instead",
"Font Size Mismatch", MB_ICONINFORMATION | MB_OK);
cfg.vtmode = VT_XWINDOWS;
}
else
{
MessageBox(NULL, "The OEM and ANSI versions of this font are\n"
"different sizes. Using ISO8859-1 mode instead",
"Font Size Mismatch", MB_ICONINFORMATION | MB_OK);
cfg.vtmode = VT_POORMAN;
}
for (i=0; i<8; i++)
if (fonts[i])
DeleteObject (fonts[i]);
goto font_messup;
}
#endif
}
void request_resize (int w, int h, int refont) {
int width, height;
/* If the window is maximized supress resizing attempts */
if(IsZoomed(hwnd)) return;
#ifdef CHECKOEMFONT
/* Don't do this in OEMANSI, you may get disable messages */
if (refont && w != cols && (cols==80 || cols==132)
&& cfg.vtmode != VT_OEMANSI)
#else
if (refont && w != cols && (cols==80 || cols==132))
#endif
{
/* If font width too big for screen should we shrink the font more ? */
if (w==132)
font_width = ((font_width*cols+w/2)/w);
else
font_width = 0;
{
int i;
for (i=0; i<8; i++)
if (fonts[i])
DeleteObject(fonts[i]);
}
bold_mode = cfg.bold_colour ? BOLD_COLOURS : BOLD_FONT;
und_mode = UND_FONT;
init_fonts(font_width);
}
else
{
static int first_time = 1;
static RECT ss;
switch(first_time)
{
case 1:
/* Get the size of the screen */
if (GetClientRect(GetDesktopWindow(),&ss))
/* first_time = 0 */;
else { first_time = 2; break; }
case 0:
/* Make sure the values are sane */
width = (ss.right-ss.left-extra_width ) / font_width;
height = (ss.bottom-ss.top-extra_height ) / font_height;
if (w>width) w=width;
if (h>height) h=height;
if (w<15) w = 15;
if (h<1) w = 1;
}
}
width = extra_width + font_width * w;
height = extra_height + font_height * h;
SetWindowPos (hwnd, NULL, 0, 0, width, height,
SWP_NOACTIVATE | SWP_NOCOPYBITS |
SWP_NOMOVE | SWP_NOZORDER);
}
static void click (Mouse_Button b, int x, int y) {
int thistime = GetMessageTime();
if (lastbtn == b && thistime - lasttime < dbltime) {
lastact = (lastact == MA_CLICK ? MA_2CLK :
lastact == MA_2CLK ? MA_3CLK :
lastact == MA_3CLK ? MA_CLICK : MA_NOTHING);
} else {
lastbtn = b;
lastact = MA_CLICK;
}
if (lastact != MA_NOTHING)
term_mouse (b, lastact, x, y);
lasttime = thistime;
}
static LRESULT CALLBACK WndProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam) {
HDC hdc;
static int ignore_size = FALSE;
static int ignore_clip = FALSE;
static int just_reconfigged = FALSE;
static int resizing = FALSE;
clock_t now;
time(&now);
switch (message) {
case UWM_PROCESS_STOPPED:
sig_chld_bottomhalf();
break;
case WM_TIMER:
pow_exec_delays();
if (pending_netevent)
enact_pending_netevent();
if (inbuf_head)
term_out(0);
noise_regular();
HideCaret(hwnd);
term_update();
ShowCaret(hwnd);
if (cfg.ping_interval > 0)
{
//clock_t now;
//time(&now);
if (now-last_movement > cfg.ping_interval * 60 - 10)
{
back->special(TS_PING);
last_movement = now;
}
}