Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
12 KiB

  1. //////////////////////////////////////////////////////////////////
  2. // File : cpadsvr.cpp
  3. // Purpose :
  4. //
  5. //
  6. // Date : Fri Apr 16 15:39:33 1999
  7. // Author : ToshiaK
  8. //
  9. // Copyright(c) 1995-1999, Microsoft Corp. All rights reserved
  10. //////////////////////////////////////////////////////////////////
  11. #ifndef WIN32_LEAN_AND_MEAN
  12. #define WIN32_LEAN_AND_MEAN
  13. #endif
  14. #include <windows.h>
  15. #ifdef UNDER_CE // stub for CE
  16. #include "stub_ce.h"
  17. #endif // UNDER_CE
  18. #include "imepadsv.h"
  19. #include "cpadsvrc.h"
  20. #include "cpadcb.h"
  21. #include "cpaddbg.h"
  22. //----------------------------------------------------------------
  23. //misc definition
  24. //----------------------------------------------------------------
  25. #define Unref(a) UNREFERENCED_PARAMETER(a)
  26. //990812:ToshiaK For Win64. Use Global Alloc/Free Ptr.
  27. #include <windowsx.h>
  28. #define MemAlloc(a) GlobalAllocPtr(GMEM_FIXED, a)
  29. #define MemFree(a) GlobalFreePtr(a)
  30. inline LPVOID
  31. WinSetUserPtr(HWND hwnd, LPVOID lpVoid)
  32. {
  33. #ifdef _WIN64
  34. return (LPVOID)SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)lpVoid);
  35. #else
  36. return (LPVOID)SetWindowLong(hwnd, GWL_USERDATA, (LONG)lpVoid);
  37. #endif
  38. }
  39. inline LPVOID
  40. WinGetUserPtr(HWND hwnd)
  41. {
  42. #ifdef _WIN64
  43. return (LPVOID)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  44. #else
  45. return (LPVOID)GetWindowLong(hwnd, GWL_USERDATA);
  46. #endif
  47. }
  48. //----------------------------------------------------------------
  49. //
  50. //public method decalre
  51. //
  52. //----------------------------------------------------------------
  53. CImePadSvrCOM::CImePadSvrCOM(VOID)
  54. {
  55. Dbg(("CImePadSvrCOM::CImePadSvrCOM() constructor START\n"));
  56. m_fShowReqStatus = FALSE;
  57. m_fLastActiveCtx = FALSE;
  58. m_hwndIF = NULL;
  59. m_lpIUnkIImeIPoint = NULL;
  60. m_lpIUnkIImeCallback= NULL;
  61. m_lpIImePadServer = NULL;
  62. m_lpCImePadCallback = NULL;
  63. m_dwRegAdvise = 0;
  64. m_fCoInitSuccess = FALSE;
  65. Dbg(("CImePadSvrCOM::CImePadSvrCOM() constructor END\n"));
  66. }
  67. CImePadSvrCOM::~CImePadSvrCOM(VOID)
  68. {
  69. Dbg(("CImePadSvrCOM::CImePadSvrCOM() Destructor START\n"));
  70. this->TermOleAPI();
  71. this->DestroyIFHWND(TRUE);
  72. m_fShowReqStatus = FALSE;
  73. m_fLastActiveCtx = FALSE;
  74. m_hwndIF = NULL;
  75. m_lpIUnkIImeIPoint = NULL;
  76. m_lpIUnkIImeCallback= NULL;
  77. m_lpIImePadServer = NULL;
  78. m_lpCImePadCallback = NULL;
  79. m_dwRegAdvise = 0;
  80. m_fCoInitSuccess = FALSE;
  81. Dbg(("CImePadSvrCOM::CImePadSvrCOM() Destructor END\n"));
  82. }
  83. INT
  84. CImePadSvrCOM::ForceDisConnect(VOID)
  85. {
  86. Dbg(("CImePadSvrCOM::ForceDisConnect START\n"));
  87. if(m_lpIImePadServer) {
  88. m_lpIImePadServer = NULL;
  89. if(m_lpCImePadCallback) {
  90. m_lpCImePadCallback->Release();
  91. m_lpCImePadCallback = NULL;
  92. }
  93. }
  94. this->DestroyIFHWND(TRUE);
  95. Dbg(("CImePadSvrCOM::ForceDisConnect END\n"));
  96. return 0;
  97. }
  98. BOOL
  99. CImePadSvrCOM::IsCoInitialized(VOID)
  100. {
  101. return m_fCoInitSuccess;
  102. }
  103. BOOL
  104. CImePadSvrCOM::IsAvailable(VOID)
  105. {
  106. return TRUE;
  107. }
  108. BOOL
  109. CImePadSvrCOM::OnIMEPadClose(VOID)
  110. {
  111. m_fShowReqStatus = FALSE;
  112. return 0;
  113. }
  114. INT
  115. CImePadSvrCOM::Initialize(LANGID imeLangID,
  116. DWORD dwImeInputID,
  117. LPVOID lpVoid)
  118. {
  119. HRESULT hr;
  120. if(!this->InitOleAPI()) {
  121. return -1;
  122. }
  123. if(!m_fCoInitSuccess) {
  124. hr = (*m_fnCoInitialize)(NULL);
  125. if(hr == S_OK) {
  126. m_fCoInitSuccess = TRUE;
  127. }
  128. }
  129. if(m_lpIImePadServer) {
  130. Dbg(("CImePadSvrCOM::Initialize() already Initialized\n"));
  131. return 0;
  132. }
  133. if(!m_fnCoCreateInstance) {
  134. return -1;
  135. }
  136. hr = (*m_fnCoCreateInstance)(CLSID_IImePadServerComponent,
  137. NULL,
  138. CLSCTX_LOCAL_SERVER,
  139. IID_IImePadServer,
  140. (LPVOID *)&m_lpIImePadServer);
  141. if(!SUCCEEDED(hr)) {
  142. Dbg(("CImePadSvrCOM::Initialize() Error[0x%08x]\n", hr));
  143. return -1;
  144. }
  145. if(!m_lpIImePadServer) {
  146. Dbg(("CImePadSvrCOM::m_lpIImePadServer is NULL\n"));
  147. return -1;
  148. }
  149. m_lpCImePadCallback = new CImePadCallback(m_hwndIF, this);
  150. if(!m_lpCImePadCallback) {
  151. Dbg(("m_lpCImePadCallback is NULL\n"));
  152. return -1;
  153. }
  154. m_lpCImePadCallback->AddRef();
  155. #if 0
  156. hr = m_lpIImePadServer->Initialize(::GetCurrentProcessId(),
  157. ::GetCurrentThreadId(),
  158. imeLangID,
  159. dwImeInputID,
  160. m_lpCImePadCallback,
  161. &m_dwRegAdvise,
  162. 0,
  163. 0);
  164. #endif
  165. Dbg(("CImePadSvrCOM::Initialize() Initialize ret[0x%08x]\n", hr));
  166. this->CreateIFHWND(); //Create internal Interface Window.
  167. return 0;
  168. Unref(imeLangID);
  169. Unref(lpVoid);
  170. }
  171. INT
  172. CImePadSvrCOM::Terminate(LPVOID)
  173. {
  174. Dbg(("CImePadSvrCOM::::Terminate() START \n"));
  175. HRESULT hr = S_OK;
  176. if(m_lpIImePadServer) {
  177. hr = m_lpIImePadServer->Terminate(m_dwRegAdvise, 0);
  178. if(SUCCEEDED(hr)) {
  179. m_dwRegAdvise = 0;
  180. if(m_lpCImePadCallback) {
  181. delete m_lpCImePadCallback;
  182. m_lpCImePadCallback = NULL;
  183. }
  184. hr = m_lpIImePadServer->Release();
  185. m_lpIImePadServer = NULL;
  186. }
  187. else {
  188. DBGShowError(hr, "IImePad::Terminate");
  189. //Dbg(("Call CoDisconnectObject()\n"));
  190. //::CoDisconnectObject((IUnknown *)m_lpIImePadServer, 0);
  191. m_lpIImePadServer = NULL;
  192. }
  193. }
  194. //----------------------------------------------------------------
  195. //if server has downed, some times CoUninitialize() cause GPF.
  196. //First we should check what is the real problem,
  197. //And remove GPF Bug with code.
  198. //After that, for warrent, we should use _try/exception code.
  199. //----------------------------------------------------------------
  200. Dbg(("Call Uninitialize\n"));
  201. if(m_fnCoUninitialize && m_fCoInitSuccess) {
  202. Dbg(("Call CoUninitialize()\n"));
  203. (*m_fnCoUninitialize)();
  204. m_fCoInitSuccess = FALSE;
  205. }
  206. Dbg(("-->CoUninitialize End\n"));
  207. this->TermOleAPI();
  208. #ifdef _RELEASEMODULE
  209. __try {
  210. if(m_fnCoUninitialize && m_fCoInitSuccess) {
  211. Dbg(("Call CoUninitialize()\n"));
  212. (*m_fnCoUninitialize)();
  213. m_fCoInitSuccess = FALSE;
  214. }
  215. this->TermOleAPI();
  216. }
  217. __except(EXCEPTION_EXECUTE_HANDLER) {
  218. Dbg(("########################################################\n"));
  219. Dbg(("#### CoUninitialize() CAUSE EXCEPTION!!!!!!! \n"));
  220. Dbg(("########################################################\n"));
  221. }
  222. #endif
  223. this->DestroyIFHWND(TRUE);
  224. Dbg(("CImePadSvrCOM::Terminate() End\n"));
  225. return 0;
  226. }
  227. INT
  228. CImePadSvrCOM::ShowUI(BOOL fShow)
  229. {
  230. if(m_lpIImePadServer) {
  231. m_lpIImePadServer->ShowUI(fShow);
  232. }
  233. m_fShowReqStatus = fShow;
  234. return 0;
  235. }
  236. INT
  237. CImePadSvrCOM::IsVisible(BOOL *pfVisible)
  238. {
  239. return 0;
  240. Unref(pfVisible);
  241. }
  242. INT
  243. CImePadSvrCOM::ActivateApplet(UINT activateID,
  244. DWORD dwActivateParam,
  245. LPWSTR lpwstr1,
  246. LPWSTR lpwstr2)
  247. {
  248. return 0;
  249. Unref(activateID);
  250. Unref(dwActivateParam);
  251. Unref(lpwstr1);
  252. Unref(lpwstr2);
  253. }
  254. INT
  255. CImePadSvrCOM::Notify(INT id, WPARAM wParam, LPARAM lParam)
  256. {
  257. switch(id) {
  258. case IMEPADNOTIFY_ACTIVATECONTEXT:
  259. Dbg(("CImePadSvrCOM::Notify: ActivateContext\n"));
  260. m_fLastActiveCtx = (BOOL)wParam;
  261. ::KillTimer(m_hwndIF, TIMERID_NOTIFY_ACTIVATECONTEXT);
  262. if(m_fLastActiveCtx) {
  263. ::SetTimer(m_hwndIF,
  264. TIMERID_NOTIFY_ACTIVATECONTEXT,
  265. TIMERELAPS_ACTIVATE,
  266. NULL);
  267. }
  268. else {
  269. ::SetTimer(m_hwndIF,
  270. TIMERID_NOTIFY_ACTIVATECONTEXT,
  271. TIMERELAPS_INACTIVATE,
  272. NULL);
  273. }
  274. break;
  275. case IMEPADNOTIFY_MODECHANGED:
  276. break;
  277. case IMEPADNOTIFY_STARTCOMPOSITION:
  278. break;
  279. case IMEPADNOTIFY_COMPOSITION:
  280. break;
  281. case IMEPADNOTIFY_ENDCOMPOSITION:
  282. break;
  283. case IMEPADNOTIFY_OPENCANDIDATE:
  284. break;
  285. case IMEPADNOTIFY_CLOSECANDIDATE:
  286. break;
  287. case IMEPADNOTIFY_APPLYCANDIDATE:
  288. break;
  289. case IMEPADNOTIFY_QUERYCANDIDATE:
  290. break;
  291. case IMEPADNOTIFY_APPLYCANDIDATE_EX:
  292. break;
  293. default:
  294. if(m_lpIImePadServer) {
  295. m_lpIImePadServer->Notify(id, wParam, lParam);
  296. }
  297. break;
  298. }
  299. return 0;
  300. Unref(wParam);
  301. Unref(lParam);
  302. }
  303. INT
  304. CImePadSvrCOM::GetAppletInfoList(INT *pCountApplet, LPVOID *pList)
  305. {
  306. return 0;
  307. Unref(pCountApplet);
  308. Unref(pList);
  309. }
  310. IUnknown *
  311. CImePadSvrCOM::SetIUnkIImeIPoint(IUnknown *pIUnkIImeIPoint)
  312. {
  313. return m_lpIUnkIImeIPoint = pIUnkIImeIPoint;
  314. }
  315. IUnknown *
  316. CImePadSvrCOM::SetIUnkIImeCallback(IUnknown *pIUnkIImeCallback)
  317. {
  318. return m_lpIUnkIImeCallback = pIUnkIImeCallback;
  319. }
  320. IUnknown*
  321. CImePadSvrCOM::GetIUnkIImeIPoint(VOID)
  322. {
  323. return m_lpIUnkIImeIPoint;
  324. }
  325. IUnknown*
  326. CImePadSvrCOM::GetIUnkIImeCallback(VOID)
  327. {
  328. return m_lpIUnkIImeCallback;
  329. }
  330. //----------------------------------------------------------------
  331. //
  332. //private static method
  333. //
  334. //----------------------------------------------------------------
  335. LRESULT CALLBACK
  336. CImePadSvrCOM::InterfaceWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  337. {
  338. LPCImePadSvrCOM lpCImePadSvr = NULL;
  339. switch(uMsg) {
  340. case WM_NCCREATE:
  341. lpCImePadSvr = (LPCImePadSvrCOM)((LPCREATESTRUCT)lParam)->lpCreateParams;
  342. WinSetUserPtr(hwnd, (LPVOID)lpCImePadSvr);
  343. break;
  344. case WM_NCDESTROY:
  345. WinSetUserPtr(hwnd, (LPVOID)NULL);
  346. break;
  347. default:
  348. lpCImePadSvr = (LPCImePadSvrCOM)WinGetUserPtr(hwnd);
  349. if(lpCImePadSvr) {
  350. return lpCImePadSvr->RealWndProc(hwnd, uMsg, wParam, lParam);
  351. }
  352. break;
  353. }
  354. return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
  355. }
  356. LRESULT
  357. CImePadSvrCOM::RealWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  358. {
  359. switch(uMsg) {
  360. case WM_COPYDATA:
  361. return MsgCopyData(hwnd, wParam, lParam);
  362. case WM_TIMER:
  363. return MsgTimer(hwnd, wParam, lParam);
  364. case WM_USER+500:
  365. return MsgUser(hwnd, wParam, lParam);
  366. default:
  367. break;
  368. }
  369. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  370. }
  371. LRESULT
  372. CImePadSvrCOM::MsgCopyData(HWND hwnd, WPARAM wParam, LPARAM lParam)
  373. {
  374. return 0;
  375. Unref(hwnd);
  376. Unref(wParam);
  377. Unref(lParam);
  378. }
  379. LRESULT
  380. CImePadSvrCOM::MsgTimer(HWND hwnd, WPARAM wParam, LPARAM lParam)
  381. {
  382. switch(wParam) {
  383. case TIMERID_NOTIFY_ACTIVATECONTEXT:
  384. ::KillTimer(hwnd, wParam);
  385. if(m_lpIImePadServer) {
  386. //try try try..
  387. #if 0
  388. if(!m_fLastActiveCtx) { //Inactivate case
  389. HWND hwndFG;
  390. DWORD dwTID, dwPID, dwPIDFG, dwTIDFG;
  391. hwndFG = ::GetForegroundWindow();
  392. dwTIDFG = ::GetWindowThreadProcessId(hwndFG, &dwPIDFG);
  393. dwTID = ::GetCurrentThreadId();
  394. dwPID = ::GetCurrentProcessId();
  395. TCHAR szClass[256];
  396. ::GetClassName(hwndFG, szClass, sizeof(szClass));
  397. Dbg(("FG class [%s]\n", szClass));
  398. if(dwTIDFG == dwTID) {
  399. return 0;
  400. }
  401. else {
  402. m_lpIImePadServer->Notify(IMEPADNOTIFY_ACTIVATECONTEXT,
  403. (WPARAM)FALSE,
  404. 0);
  405. }
  406. }
  407. #endif
  408. m_lpIImePadServer->Notify(IMEPADNOTIFY_ACTIVATECONTEXT,
  409. (WPARAM)m_fLastActiveCtx,
  410. 0);
  411. }
  412. break;
  413. default:
  414. ::KillTimer(hwnd, wParam);
  415. break;
  416. }
  417. return 0;
  418. Unref(hwnd);
  419. Unref(wParam);
  420. Unref(lParam);
  421. }
  422. LRESULT
  423. CImePadSvrCOM::MsgUser(HWND hwnd, WPARAM wParam, LPARAM lParam)
  424. {
  425. return 0;
  426. Unref(hwnd);
  427. Unref(wParam);
  428. Unref(lParam);
  429. }
  430. HWND
  431. CImePadSvrCOM::CreateIFHWND(VOID)
  432. {
  433. return NULL;
  434. #if 0
  435. Dbg(("CImePadSvrCOM::CreateIFHWND START\n"));
  436. if(m_hwndIF && ::IsWindow(m_hwndIF)) {
  437. return m_hwndIF;
  438. }
  439. ATOM atom;
  440. HWND hwnd;
  441. #ifndef UNDER_CE // No Ex
  442. WNDCLASSEX wc;
  443. #else // UNDER_CE
  444. WNDCLASS wc;
  445. #endif // UNDER_CE
  446. #ifndef UNDER_CE // No Ex
  447. wc.cbSize = sizeof(wc);
  448. #endif // UNDER_CE
  449. wc.style = 0;
  450. wc.lpfnWndProc = (WNDPROC)CImePadSvrCOM::InterfaceWndProc;
  451. wc.cbClsExtra = 0;
  452. wc.cbWndExtra = 0;
  453. wc.hInstance = m_ghModClient;
  454. wc.hIcon = (HICON)NULL;
  455. wc.hCursor = (HCURSOR)NULL;
  456. wc.hbrBackground = (HBRUSH)NULL;
  457. wc.lpszMenuName = NULL;
  458. wc.lpszClassName = SZ_IMEPADCLIENTCLASS;
  459. #ifndef UNDER_CE // No Ex
  460. wc.hIconSm = NULL;
  461. atom = ::RegisterClassEx(&wc);
  462. #else // UNDER_CE
  463. atom = ::RegisterClass(&wc);
  464. #endif // UNDER_CE
  465. hwnd = ::CreateWindowEx(0,
  466. SZ_IMEPADCLIENTCLASS,
  467. NULL,
  468. WS_POPUP | WS_DISABLED,
  469. 0,0,0,0,
  470. NULL,
  471. NULL,
  472. m_ghModClient,
  473. (LPVOID)this);
  474. if(!hwnd) {
  475. Dbg(("CreateWindowEx Error %d\n", GetLastError()));
  476. }
  477. m_hwndIF = hwnd;
  478. Dbg(("CImePadSvrCOM::CreateIFHWND END\n"));
  479. return hwnd;
  480. #endif
  481. }
  482. BOOL
  483. CImePadSvrCOM::DestroyIFHWND(BOOL fReserved)
  484. {
  485. Dbg(("CImePadSvrCOM::DestroyIFHWND() START\n"));
  486. #if 0
  487. if(m_hwndIF && ::IsWindow(m_hwndIF)) {
  488. ::DestroyWindow(m_hwndIF);
  489. m_hwndIF = NULL;
  490. }
  491. //Must Unregister class.
  492. BOOL fRet = ::UnregisterClass(SZ_IMEPADCLIENTCLASS, m_ghModClient);
  493. if(!fRet) {
  494. Dbg(("UnregisterClass Failed [%d]\n", GetLastError()));
  495. }
  496. Dbg(("CImePadSvrCOM::DestroyIFHWND() END\n"));
  497. #endif
  498. return TRUE;
  499. Unref(fReserved);
  500. }