| 1 |
/* |
| 2 |
* Copyright (C) 1994-1998 T. Teranishi |
| 3 |
* (C) 2006- TeraTerm Project |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* 3. The name of the author may not be used to endorse or promote products |
| 16 |
* derived from this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR |
| 19 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
|
| 30 |
/* TERATERM.EXE, main */ |
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include <crtdbg.h> |
| 34 |
#include <windows.h> |
| 35 |
#include <htmlhelp.h> |
| 36 |
#include "teraterm.h" |
| 37 |
#include "tttypes.h" |
| 38 |
#include "commlib.h" |
| 39 |
#include "ttwinman.h" |
| 40 |
#include "buffer.h" |
| 41 |
#include "vtterm.h" |
| 42 |
#include "vtwin.h" |
| 43 |
#include "clipboar.h" |
| 44 |
#include "filesys.h" |
| 45 |
#include "telnet.h" |
| 46 |
#include "tektypes.h" |
| 47 |
#include "tekwin.h" |
| 48 |
#include "ttdde.h" |
| 49 |
#include "keyboard.h" |
| 50 |
#include "dllutil.h" |
| 51 |
#include "compat_win.h" |
| 52 |
#include "dlglib.h" |
| 53 |
#include "teraterml.h" |
| 54 |
#include "sendmem.h" |
| 55 |
#include "ttdebug.h" |
| 56 |
#include "win32helper.h" |
| 57 |
#include "asprintf.h" |
| 58 |
#if ENABLE_GDIPLUS |
| 59 |
#include "ttgdiplus.h" |
| 60 |
#endif |
| 61 |
|
| 62 |
#if defined(_DEBUG) && defined(_MSC_VER) |
| 63 |
#define new ::new(_NORMAL_BLOCK, __FILE__, __LINE__) |
| 64 |
#endif |
| 65 |
|
| 66 |
static BOOL AddFontFlag; |
| 67 |
static wchar_t *TSpecialFont; |
| 68 |
static CVTWindow* pVTWin; |
| 69 |
static DWORD HtmlHelpCookie; |
| 70 |
|
| 71 |
static void LoadSpecialFont(void) |
| 72 |
{ |
| 73 |
wchar_t *mod_path; |
| 74 |
if (IsExistFontW(L"Tera Special", SYMBOL_CHARSET, TRUE)) { |
| 75 |
// ���������������������[�h������ |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
if (hGetModuleFileNameW(NULL, &mod_path) != 0) { |
| 80 |
AddFontFlag = FALSE; |
| 81 |
return; |
| 82 |
} |
| 83 |
*wcsrchr(mod_path, L'\\') = 0; |
| 84 |
aswprintf(&TSpecialFont, L"%s\\TSPECIAL1.TTF", mod_path); |
| 85 |
free(mod_path); |
| 86 |
|
| 87 |
// teraterm.exe�������L�����t�H���g�������B |
| 88 |
// remove�����������I��������OS������������ |
| 89 |
int r = 0; |
| 90 |
if (pAddFontResourceExW != NULL) { |
| 91 |
r = pAddFontResourceExW(TSpecialFont, FR_PRIVATE, NULL); |
| 92 |
} |
| 93 |
if (r == 0) { |
| 94 |
// AddFontResourceEx() ���g���������� |
| 95 |
// �V�X�e���S�����g�����t�H���g������ |
| 96 |
// remove��������OS������������������ |
| 97 |
r = AddFontResourceW(TSpecialFont); |
| 98 |
} |
| 99 |
if (r != 0) { |
| 100 |
AddFontFlag = TRUE; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static void UnloadSpecialFont(void) |
| 105 |
{ |
| 106 |
if (!AddFontFlag) { |
| 107 |
return; |
| 108 |
} |
| 109 |
int r = 0; |
| 110 |
if (pRemoveFontResourceExW != NULL) { |
| 111 |
r = pRemoveFontResourceExW(TSpecialFont, FR_PRIVATE, NULL); |
| 112 |
} |
| 113 |
if (r == 0) { |
| 114 |
RemoveFontResourceW(TSpecialFont); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
static void init(void) |
| 119 |
{ |
| 120 |
DLLInit(); |
| 121 |
WinCompatInit(); |
| 122 |
DebugSetException(); |
| 123 |
LoadSpecialFont(); |
| 124 |
#if defined(DEBUG_OPEN_CONSOLE_AT_STARTUP) |
| 125 |
DebugConsoleOpen(); |
| 126 |
#endif |
| 127 |
} |
| 128 |
|
| 129 |
// Tera Term main engine |
| 130 |
static BOOL OnIdle(LONG lCount) |
| 131 |
{ |
| 132 |
static int Busy = 2; |
| 133 |
int nx, ny; |
| 134 |
BOOL Size; |
| 135 |
|
| 136 |
if (lCount==0) Busy = 2; |
| 137 |
|
| 138 |
if (cv.Ready) |
| 139 |
{ |
| 140 |
/* Sender */ |
| 141 |
CommSend(&cv); |
| 142 |
|
| 143 |
/* Parser */ |
| 144 |
if ((TelStatus==TelIdle) && cv.TelMode) |
| 145 |
TelStatus = TelIAC; |
| 146 |
|
| 147 |
if (TelStatus != TelIdle) |
| 148 |
{ |
| 149 |
ParseTel(&Size,&nx,&ny); |
| 150 |
if (Size) { |
| 151 |
LockBuffer(); |
| 152 |
ChangeTerminalSize(nx,ny); |
| 153 |
UnlockBuffer(); |
| 154 |
} |
| 155 |
} |
| 156 |
else { |
| 157 |
int Change; |
| 158 |
if (ProtoGetProtoFlag()) Change = ProtoDlgParse(); |
| 159 |
else { |
| 160 |
switch (ActiveWin) { |
| 161 |
case IdVT: |
| 162 |
Change = pVTWin->Parse(); |
| 163 |
// TEK window���A�N�e�B�u���� pause ���g�����ACPU�g�p��100%������ |
| 164 |
// ���������b�������B(2006.2.6 yutaka) |
| 165 |
// �����������������A�R���e�L�X�g�X�C�b�`�����������B(2006.3.20 yutaka) |
| 166 |
Sleep(0); |
| 167 |
break; |
| 168 |
|
| 169 |
case IdTEK: |
| 170 |
if (pTEKWin != NULL) { |
| 171 |
Change = ((CTEKWindow*)pTEKWin)->Parse(); |
| 172 |
// TEK window���A�N�e�B�u���� pause ���g�����ACPU�g�p��100%������ |
| 173 |
// ���������b�������B(2006.2.6 yutaka) |
| 174 |
Sleep(1); |
| 175 |
} |
| 176 |
else { |
| 177 |
Change = IdVT; |
| 178 |
} |
| 179 |
break; |
| 180 |
|
| 181 |
default: |
| 182 |
Change = 0; |
| 183 |
} |
| 184 |
|
| 185 |
switch (Change) { |
| 186 |
case IdVT: |
| 187 |
VTActivate(); |
| 188 |
break; |
| 189 |
case IdTEK: |
| 190 |
pVTWin->OpenTEK(); |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
FLogWriteFile(); |
| 197 |
|
| 198 |
if (DDELog && AdvFlag) { |
| 199 |
DDEAdv(); |
| 200 |
} |
| 201 |
|
| 202 |
/* Talker */ |
| 203 |
switch (TalkStatus) { |
| 204 |
case IdTalkFile: |
| 205 |
FileSend(); |
| 206 |
break; /* file */ |
| 207 |
case IdTalkSendMem: |
| 208 |
SendMemContinuously(); |
| 209 |
break; |
| 210 |
default: |
| 211 |
break; |
| 212 |
} |
| 213 |
|
| 214 |
/* Receiver */ |
| 215 |
if (DDELog && DDEGetCount() > 0) { |
| 216 |
// ���O�o�b�t�@������DDE�N���C�A���g�����������������������A |
| 217 |
// TCP�p�P�b�g�����M���s�������B |
| 218 |
// �A���������M���s�����A���O�o�b�t�@�����E���h���r�������������M���f�[�^�� |
| 219 |
// �������������������\���������B(2007.6.14 yutaka) |
| 220 |
|
| 221 |
} else { |
| 222 |
CommReceive(&cv); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
if (cv.Ready && |
| 228 |
(cv.RRQ || (cv.OutBuffCount>0) || (cv.InBuffCount>0) || (cv.FlushLen>0) || FLogGetCount() > 0 || (DDEGetCount()>0)) ) { |
| 229 |
Busy = 2; |
| 230 |
} |
| 231 |
else { |
| 232 |
Busy--; |
| 233 |
} |
| 234 |
|
| 235 |
return (Busy>0); |
| 236 |
} |
| 237 |
|
| 238 |
static HWND main_window; |
| 239 |
HWND GetHWND(void) |
| 240 |
{ |
| 241 |
return main_window; |
| 242 |
} |
| 243 |
|
| 244 |
static HWND hModelessDlg; |
| 245 |
|
| 246 |
void AddModelessHandle(HWND hWnd) |
| 247 |
{ |
| 248 |
hModelessDlg = hWnd; |
| 249 |
} |
| 250 |
|
| 251 |
void RemoveModelessHandle(HWND hWnd) |
| 252 |
{ |
| 253 |
(void)hWnd; |
| 254 |
hModelessDlg = 0; |
| 255 |
} |
| 256 |
|
| 257 |
static UINT nMsgLast; |
| 258 |
static POINT ptCursorLast; |
| 259 |
|
| 260 |
/** |
| 261 |
* idle�������������������� |
| 262 |
*/ |
| 263 |
static BOOL IsIdleMessage(const MSG* pMsg) |
| 264 |
{ |
| 265 |
if (pMsg->message == WM_MOUSEMOVE || |
| 266 |
pMsg->message == WM_NCMOUSEMOVE) |
| 267 |
{ |
| 268 |
if (pMsg->message == nMsgLast && |
| 269 |
pMsg->pt.x == ptCursorLast.x && |
| 270 |
pMsg->pt.y == ptCursorLast.y) |
| 271 |
{ // �������u��������idle������������ |
| 272 |
return FALSE; |
| 273 |
} |
| 274 |
|
| 275 |
ptCursorLast = pMsg->pt; |
| 276 |
nMsgLast = pMsg->message; |
| 277 |
return TRUE; |
| 278 |
} |
| 279 |
|
| 280 |
if (pMsg->message == WM_PAINT || |
| 281 |
pMsg->message == 0x0118/*WM_SYSTIMER*/) |
| 282 |
{ |
| 283 |
return FALSE; |
| 284 |
} |
| 285 |
|
| 286 |
return TRUE; |
| 287 |
} |
| 288 |
|
| 289 |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInst, |
| 290 |
LPSTR lpszCmdLine, int nCmdShow) |
| 291 |
{ |
| 292 |
(void)hPreInst; |
| 293 |
(void)lpszCmdLine; |
| 294 |
(void)nCmdShow; |
| 295 |
#ifdef _DEBUG |
| 296 |
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); |
| 297 |
#endif |
| 298 |
|
| 299 |
srand((unsigned int)time(NULL)); |
| 300 |
|
| 301 |
ts.TeraTermInstance = hInstance; |
| 302 |
hInst = hInstance; |
| 303 |
init(); |
| 304 |
_HtmlHelpW(NULL, NULL, HH_INITIALIZE, (DWORD_PTR)&HtmlHelpCookie); |
| 305 |
|
| 306 |
#if ENABLE_GDIPLUS |
| 307 |
GDIPInit(); |
| 308 |
#endif |
| 309 |
|
| 310 |
CVTWindow *m_pMainWnd = new CVTWindow(hInstance); |
| 311 |
pVTWin = m_pMainWnd; |
| 312 |
main_window = m_pMainWnd->m_hWnd; |
| 313 |
// [Tera Term]�Z�N�V������DLG_SYSTEM_FONT�������������Z�b�g���� |
| 314 |
SetDialogFont(ts.DialogFontNameW, ts.DialogFontPoint, ts.DialogFontCharSet, |
| 315 |
ts.UILanguageFileW, "Tera Term", "DLG_SYSTEM_FONT"); |
| 316 |
|
| 317 |
BOOL bIdle = TRUE; // idle������? |
| 318 |
LONG lCount = 0; |
| 319 |
MSG msg; |
| 320 |
for (;;) { |
| 321 |
// idle���������b�Z�[�W���������� |
| 322 |
while (bIdle) { |
| 323 |
if (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE) != FALSE) { |
| 324 |
// ���b�Z�[�W���������� |
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
const BOOL continue_idle = OnIdle(lCount++); |
| 329 |
if (!continue_idle) { |
| 330 |
// FALSE��������������idle�������s�v |
| 331 |
bIdle = FALSE; |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
// ���b�Z�[�W���������������������� |
| 337 |
for(;;) { |
| 338 |
// ���b�Z�[�W���������������AGetMessage()���u���b�N�������������� |
| 339 |
if (::GetMessage(&msg, NULL, 0, 0) == FALSE) { |
| 340 |
// WM_QUIT |
| 341 |
goto exit_message_loop; |
| 342 |
} |
| 343 |
|
| 344 |
if (hModelessDlg == 0 || |
| 345 |
::IsDialogMessage(hModelessDlg, &msg) == FALSE) |
| 346 |
{ |
| 347 |
bool message_processed = false; |
| 348 |
|
| 349 |
if (m_pMainWnd->m_hAccel != NULL) { |
| 350 |
if (!MetaKey(ts.MetaKey)) { |
| 351 |
// matakey���������������� |
| 352 |
if (::TranslateAccelerator(m_pMainWnd->m_hWnd , m_pMainWnd->m_hAccel, &msg)) { |
| 353 |
// �A�N�Z�����[�^�[�L�[���������� |
| 354 |
message_processed = true; |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
if (!message_processed) { |
| 360 |
::TranslateMessage(&msg); |
| 361 |
::DispatchMessage(&msg); |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// idle������������? |
| 366 |
if (IsIdleMessage(&msg)) { |
| 367 |
bIdle = TRUE; |
| 368 |
lCount = 0; |
| 369 |
} |
| 370 |
|
| 371 |
if (::PeekMessage(&msg, NULL, NULL, NULL, PM_NOREMOVE) == FALSE) { |
| 372 |
// ���b�Z�[�W������������ |
| 373 |
break; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
exit_message_loop: |
| 378 |
|
| 379 |
delete m_pMainWnd; |
| 380 |
m_pMainWnd = NULL; |
| 381 |
|
| 382 |
#if ENABLE_GDIPLUS |
| 383 |
GDIPUninit(); |
| 384 |
#endif |
| 385 |
|
| 386 |
_HtmlHelpW(NULL, NULL, HH_CLOSE_ALL, 0); |
| 387 |
_HtmlHelpW(NULL, NULL, HH_UNINITIALIZE, HtmlHelpCookie); |
| 388 |
|
| 389 |
free(TSpecialFont); |
| 390 |
TSpecialFont = NULL; |
| 391 |
UnloadSpecialFont(); |
| 392 |
DLLExit(); |
| 393 |
|
| 394 |
return (int)msg.wParam; |
| 395 |
} |