Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/teraterm/teraterm/ttime.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9382 - (show annotations) (download) (as text)
Sat Aug 28 15:29:03 2021 UTC (2 years, 9 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 14600 byte(s)
ttime.cでdllutilを使ってAPIのロードを行うようにした

- 警告対策
  - warning: cast between incompatible function types from 'FARPROC' ... [-Wcast-function-type] 等
- dllutil.cpp
  - DLLGetApiAddressFromList() dllハンドルを戻せるようにした
  - DLLFreeByFileName() 追加
  - DLLFreeByHandle() 追加
1 /*
2 * Copyright (C) 1994-1998 T. Teranishi
3 * (C) 2007- 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 /* Tera Term */
30 /* TERATERM.EXE, IME interface */
31
32 #include <windows.h>
33 #if !defined(_CRTDBG_MAP_ALLOC)
34 #define _CRTDBG_MAP_ALLOC
35 #endif
36 #include <stdio.h> // for snprintf, snwprintf
37 #include <stdlib.h>
38 #include <crtdbg.h>
39 #include <string.h>
40 #include <imm.h>
41 #include <assert.h>
42 #include "asprintf.h"
43 #include "dllutil.h"
44 #include "ttime.h"
45
46 // #define ENABLE_DUMP 1
47
48 static LONG (WINAPI *PImmGetCompositionStringA)(HIMC, DWORD, LPVOID, DWORD);
49 static LONG (WINAPI *PImmGetCompositionStringW)(HIMC, DWORD, LPVOID, DWORD);
50 static HIMC (WINAPI *PImmGetContext)(HWND);
51 static BOOL (WINAPI *PImmReleaseContext)(HWND, HIMC);
52 static BOOL (WINAPI *PImmSetCompositionFontA)(HIMC, LPLOGFONTA);
53 static BOOL (WINAPI *PImmSetCompositionFontW)(HIMC, LPLOGFONTW);
54 static BOOL (WINAPI *PImmSetCompositionWindow)(HIMC, LPCOMPOSITIONFORM);
55 static BOOL (WINAPI *PImmGetOpenStatus)(HIMC);
56 static BOOL (WINAPI *PImmSetOpenStatus)(HIMC, BOOL);
57
58 static HANDLE HIMEDLL = NULL;
59 static LOGFONTA IMELogFontA;
60 static LOGFONTW IMELogFontW;
61
62 static const APIInfo imeapi[] = {
63 { "ImmGetCompositionStringW", (void **)&PImmGetCompositionStringW },
64 { "ImmGetCompositionStringA", (void **)&PImmGetCompositionStringA },
65 { "ImmGetContext", (void **)&PImmGetContext },
66 { "ImmReleaseContext", (void **)&PImmReleaseContext },
67 { "ImmSetCompositionFontA", (void **)&PImmSetCompositionFontA },
68 { "ImmSetCompositionFontW", (void **)&PImmSetCompositionFontW },
69 { "ImmSetCompositionWindow", (void **)&PImmSetCompositionWindow },
70 { "ImmGetOpenStatus", (void **)&PImmGetOpenStatus },
71 { "ImmSetOpenStatus", (void **)&PImmSetOpenStatus },
72 { NULL, NULL },
73 };
74
75 BOOL LoadIME(void)
76 {
77 HANDLE hDll;
78 DWORD error;
79 if (HIMEDLL != NULL) {
80 // 2�d������?
81 return TRUE;
82 }
83 error = DLLGetApiAddressFromList(L"imm32.dll", DLL_LOAD_LIBRARY_SYSTEM, DLL_ERROR_NOT_EXIST, imeapi, &hDll);
84 if (error == NO_ERROR) {
85 HIMEDLL = hDll;
86 }
87 return error != NO_ERROR ? FALSE : TRUE;
88 }
89
90 void FreeIME(HWND hWnd)
91 {
92 HANDLE HTemp;
93
94 if (HIMEDLL == NULL)
95 return;
96 HTemp = HIMEDLL;
97 HIMEDLL = NULL;
98
99 /* position of conv. window -> default */
100 SetConversionWindow(hWnd,-1,0);
101 Sleep(1); // for safety
102
103 DLLFreeByHandle(HTemp);
104 {
105 const APIInfo *p = imeapi;
106 while(p->ApiName != NULL) {
107 *p->func = NULL;
108 p++;
109 }
110 }
111 }
112
113 BOOL CanUseIME(void)
114 {
115 return (HIMEDLL != NULL);
116 }
117
118 void SetConversionWindow(HWND HWnd, int X, int Y)
119 {
120 HIMC hIMC;
121 COMPOSITIONFORM cf;
122
123 if (HIMEDLL == NULL) return;
124 // Adjust the position of conversion window
125 hIMC = (*PImmGetContext)(HWnd);
126 if (X>=0)
127 {
128 cf.dwStyle = CFS_POINT;
129 cf.ptCurrentPos.x = X;
130 cf.ptCurrentPos.y = Y;
131 }
132 else
133 cf.dwStyle = CFS_DEFAULT;
134 (*PImmSetCompositionWindow)(hIMC,&cf);
135 (*PImmReleaseContext)(HWnd,hIMC);
136 }
137
138 void ResetConversionLogFont(HWND HWnd)
139 {
140 HIMC hIMC;
141 if (HIMEDLL == NULL) return;
142
143 hIMC = PImmGetContext(HWnd);
144 if (hIMC != NULL) {
145 BOOL result = FALSE;
146 if (PImmSetCompositionFontW != NULL) {
147 // ImmSetCompositionFontA()���g�p������
148 // ���������������w���t�H���g���\��������������������
149 result = PImmSetCompositionFontW(hIMC, &IMELogFontW);
150 }
151 if (result == FALSE) {
152 // ImmSetCompositionFontW() ���G���[���������������� A() �����g���C
153 // 9x ���� W()�������������G���[������������������
154 PImmSetCompositionFontA(hIMC, &IMELogFontA);
155 }
156 PImmReleaseContext(HWnd,hIMC);
157 }
158 }
159
160 void SetConversionLogFont(HWND HWnd, const LOGFONTA *lf)
161 {
162 if (HIMEDLL == NULL) return;
163
164 memcpy(&IMELogFontA,lf,sizeof(LOGFONT));
165 if (PImmSetCompositionFontW != NULL) {
166 LOGFONTW *p = &IMELogFontW;
167 p->lfWeight = lf->lfWeight;
168 p->lfItalic = lf->lfItalic;
169 p->lfUnderline = lf->lfUnderline;
170 p->lfStrikeOut = lf->lfStrikeOut;
171 p->lfWidth = lf->lfWidth;
172 p->lfHeight = lf->lfHeight;
173 p->lfCharSet = lf->lfCharSet;
174 p->lfOutPrecision = lf->lfOutPrecision;
175 p->lfClipPrecision = lf->lfClipPrecision;
176 p->lfQuality = lf->lfQuality ;
177 p->lfPitchAndFamily = lf->lfPitchAndFamily;
178 MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
179 lf->lfFaceName, sizeof(lf->lfFaceName),
180 p->lfFaceName, _countof(p->lfFaceName));
181 }
182 ResetConversionLogFont(HWnd);
183 }
184
185 // �����p
186 static const char *GetConvStringA_i(HWND hWnd, DWORD index, size_t *len)
187 {
188 HIMC hIMC;
189 LONG size;
190 char *lpstr;
191
192 hIMC = (*PImmGetContext)(hWnd);
193 if (hIMC==0)
194 goto error_2;
195
196 // Get the size of the result string.
197 // ���� ImmGetCompositionStringA() �������l�� byte ��
198 size = PImmGetCompositionStringA(hIMC, index, NULL, 0);
199 if (size <= 0)
200 goto error_1;
201
202 lpstr = malloc(size + sizeof(char));
203 if (lpstr == NULL)
204 goto error_1;
205
206 size = PImmGetCompositionStringA(hIMC, index, lpstr, size);
207 if (size <= 0) {
208 free(lpstr);
209 goto error_1;
210 }
211
212 *len = size;
213 lpstr[size] = 0; // �^�[�~�l�[�g����
214
215 (*PImmReleaseContext)(hWnd, hIMC);
216 return lpstr;
217
218 error_1:
219 (*PImmReleaseContext)(hWnd, hIMC);
220 error_2:
221 *len = 0;
222 return NULL;
223 }
224
225 // �����p wchar_t��
226 static const wchar_t *GetConvStringW_i(HWND hWnd, DWORD index, size_t *len)
227 {
228 HIMC hIMC;
229 LONG size;
230 wchar_t *lpstr;
231
232 hIMC = (*PImmGetContext)(hWnd);
233 if (hIMC==0)
234 goto error_2;
235
236 // Get the size of the result string.
237 // ���� ImmGetCompositionStringW() �������l�� byte ��
238 size = PImmGetCompositionStringW(hIMC, index, NULL, 0);
239 if (size <= 0)
240 goto error_1;
241
242 lpstr = malloc(size + sizeof(wchar_t));
243 if (lpstr == NULL)
244 goto error_1;
245
246 size = PImmGetCompositionStringW(hIMC, index, lpstr, size);
247 if (size <= 0) {
248 free(lpstr);
249 goto error_1;
250 }
251
252 *len = size/2;
253 lpstr[(size/2)] = 0; // �^�[�~�l�[�g����
254
255 (*PImmReleaseContext)(hWnd, hIMC);
256 return lpstr;
257
258 error_1:
259 (*PImmReleaseContext)(hWnd, hIMC);
260 error_2:
261 *len = 0;
262 return NULL;
263 }
264
265 /*
266 * @param[out] *len wchar_t������('\0'����������)
267 * @retval ����wchar_t�����������|�C���^
268 * NULL�����������m������������(�������G���[)
269 * ���������g�p��free()��������
270 */
271 const wchar_t *GetConvStringW(HWND hWnd, LPARAM lParam, size_t *len)
272 {
273 const wchar_t *lpstr;
274
275 *len = 0;
276 if (HIMEDLL==NULL) return NULL;
277
278 if ((lParam & GCS_RESULTSTR) != 0) {
279 lpstr = GetConvStringW_i(hWnd, GCS_RESULTSTR, len);
280 } else {
281 lpstr = NULL;
282 }
283
284 return lpstr;
285 }
286
287 /*
288 * @param[out] *len ������('\0'����������)
289 * @retval ���������������|�C���^
290 * NULL�����������m������������(�������G���[)
291 * ���������g�p��free()��������
292 */
293 const char *GetConvStringA(HWND hWnd, LPARAM lParam, size_t *len)
294 {
295 const char *lpstr;
296
297 *len = 0;
298 if (HIMEDLL==NULL) return NULL;
299
300 if ((lParam & GCS_RESULTSTR) != 0) {
301 lpstr = GetConvStringA_i(hWnd, GCS_RESULTSTR, len);
302 } else {
303 lpstr = NULL;
304 }
305
306 return lpstr;
307 }
308
309 BOOL GetIMEOpenStatus(HWND hWnd)
310 {
311 HIMC hIMC;
312 BOOL stat;
313
314 if (HIMEDLL==NULL) return FALSE;
315 hIMC = (*PImmGetContext)(hWnd);
316 stat = (*PImmGetOpenStatus)(hIMC);
317 (*PImmReleaseContext)(hWnd, hIMC);
318
319 return stat;
320 }
321
322 void SetIMEOpenStatus(HWND hWnd, BOOL stat)
323 {
324 HIMC hIMC;
325
326 if (HIMEDLL==NULL) return;
327 hIMC = (*PImmGetContext)(hWnd);
328 (*PImmSetOpenStatus)(hIMC, stat);
329 (*PImmReleaseContext)(hWnd, hIMC);
330 }
331
332 #if defined(ENABLE_DUMP)
333 static void DumpReconvStringSt(RECONVERTSTRING *pReconv, BOOL unicode)
334 {
335 if (unicode) {
336 wchar_t *buf;
337 aswprintf(&buf,
338 L"Str %d,%d CompStr %d,%d TargeteStr %d,%d '%s'\n",
339 pReconv->dwStrLen,
340 pReconv->dwStrOffset,
341 pReconv->dwCompStrLen,
342 pReconv->dwCompStrOffset,
343 pReconv->dwTargetStrLen,
344 pReconv->dwTargetStrOffset,
345 (wchar_t *)(((char *)pReconv) + pReconv->dwStrOffset)
346 );
347 _OutputDebugStringW(buf);
348 free(buf);
349 } else {
350 char *buf;
351 asprintf(&buf,
352 "Str %d,%d CompStr %d,%d TargeteStr %d,%d '%s'\n",
353 pReconv->dwStrLen,
354 pReconv->dwStrOffset,
355 pReconv->dwCompStrLen,
356 pReconv->dwCompStrOffset,
357 pReconv->dwTargetStrLen,
358 pReconv->dwTargetStrOffset,
359 (((char *)pReconv) + pReconv->dwStrOffset)
360 );
361 OutputDebugStringA(buf);
362 free(buf);
363 }
364 }
365 #endif
366
367 // IME���O���Q�������@�\��������
368 // MS�������������d�l�����������������������A�A�h�z�b�N���������������������B
369 // cf. http://d.hatena.ne.jp/topiyama/20070703
370 // http://ice.hotmint.com/putty/#DOWNLOAD
371 // http://27213143.at.webry.info/201202/article_2.html
372 // http://webcache.googleusercontent.com/search?q=cache:WzlX3ouMscIJ:anago.2ch.net/test/read.cgi/software/1325573999/82+IMR_DOCUMENTFEED&cd=13&hl=ja&ct=clnk&gl=jp
373 // (2012.5.9 yutaka)
374 /**
375 * IME���O���Q�������@�\�p�\��������������
376 * msg == WM_IME_REQUEST,wParam == IMR_DOCUMENTFEED ���������g��
377 * ANSI��Unicode�E�B���h�E���������Q�������������X��������
378 * unicode TRUE������unicode
379 * str_ptr �Q�������������|�C���^
380 * str_count �Q����������������(char����bytes��,wchar_t����wchar_t��)
381 * cx �J�[�\�����u(char/wchar_t�P��)
382 * st_size ���������\�������T�C�Y
383 * �����l �\���������|�C���^
384 * �s�v���������� free()��������
385 */
386 static void *CreateReconvStringSt(HWND hWnd, BOOL unicode,
387 const void *str_ptr, size_t str_count,
388 size_t cx, size_t *st_size_)
389 {
390 static int new_str_len_bytes;
391 static int new_buf_len_bytes;
392 int new_str_len_count;
393 int str_len_bytes;
394 size_t str_len_count;
395 int cx_bytes;
396 RECONVERTSTRING *pReconv;
397 const void *comp_ptr;
398 size_t complen_count;
399 size_t complen_bytes;
400 const void *buf;
401 size_t st_size;
402 char *newbuf;
403
404 buf = str_ptr;
405 str_len_count = str_count;
406
407 if(unicode) {
408 str_len_bytes = str_len_count * sizeof(wchar_t);
409 cx_bytes = cx * sizeof(wchar_t);
410 } else {
411 str_len_bytes = str_len_count;
412 cx_bytes = cx;
413 }
414
415 // ����������������������
416 // ATOK2012�������� complen_count=0 �������B
417 if (!unicode) {
418 const char *comp_strA;
419 comp_strA = GetConvStringA_i(hWnd, GCS_COMPSTR, &complen_count);
420 complen_bytes = complen_count;
421 comp_ptr = comp_strA;
422 } else {
423 const wchar_t *comp_strW;
424 comp_strW = GetConvStringW_i(hWnd,GCS_COMPSTR, &complen_count);
425 complen_bytes = complen_count * sizeof(wchar_t);
426 comp_ptr = comp_strW;
427 }
428
429 // �����������������S��������(including null)
430 if (!unicode) {
431 new_str_len_bytes = str_len_bytes + complen_bytes;
432 new_buf_len_bytes = new_str_len_bytes + 1;
433 new_str_len_count = new_str_len_bytes;
434 } else {
435 new_str_len_bytes = str_len_bytes + complen_bytes;
436 new_buf_len_bytes = new_str_len_bytes + sizeof(wchar_t);
437 new_str_len_count = new_str_len_bytes / sizeof(wchar_t);
438 }
439
440 st_size = sizeof(RECONVERTSTRING) + new_buf_len_bytes;
441 pReconv = calloc(1, st_size);
442
443 // Len��������(char/wchar_t�P��)
444 // Offset��byte�P��
445 // pReconv->dwSize = sizeof(RECONVERTSTRING);
446 pReconv->dwSize = sizeof(RECONVERTSTRING) + new_buf_len_bytes;
447 pReconv->dwVersion = 0;
448 pReconv->dwStrLen = new_str_len_count;
449 pReconv->dwStrOffset = sizeof(RECONVERTSTRING);
450 pReconv->dwCompStrLen = complen_count;
451 pReconv->dwCompStrOffset = cx_bytes;
452 pReconv->dwTargetStrLen = complen_count; // = dwCompStrLen
453 pReconv->dwTargetStrOffset = cx_bytes; // = dwCompStrOffset
454
455 // RECONVERTSTRING��������
456 // �Q�����������R�s�[+�J�[�\�����u�������������}��
457 newbuf = (char *)pReconv + sizeof(RECONVERTSTRING);
458 if (comp_ptr != NULL) {
459 memcpy(newbuf, buf, cx_bytes);
460 newbuf += cx_bytes;
461 memcpy(newbuf, comp_ptr, complen_bytes);
462 newbuf += complen_bytes;
463 memcpy(newbuf, (char *)buf + cx_bytes, str_len_bytes - cx_bytes);
464 free((void *)comp_ptr);
465 comp_ptr = NULL;
466 } else {
467 memcpy(newbuf, buf, str_len_bytes);
468 }
469 #if defined(ENABLE_DUMP)
470 DumpReconvStringSt(pReconv, unicode);
471 #endif
472
473 *st_size_ = st_size;
474 return pReconv;
475 }
476
477 /**
478 * IME���O���Q�������@�\�p�\��������������
479 * ANSI�E�B���h�E�p
480 * msg == WM_IME_REQUEST,wParam == IMR_DOCUMENTFEED ���������g��
481 * str_ptr �Q�������������|�C���^
482 * str_count �Q����������������(char��,bytes��)
483 * cx �J�[�\�����u(char�P��)
484 * st_size ���������\�������T�C�Y(byte)
485 * �����l �\���������|�C���^
486 * �s�v���������� free()��������
487 */
488 void *CreateReconvStringStA(
489 HWND hWnd, const char *str_ptr, size_t str_count,
490 size_t cx, size_t *st_size_)
491 {
492 if (HIMEDLL == NULL) return NULL;
493 //assert(IsWindowUnicode(hWnd) == FALSE);
494 return CreateReconvStringSt(hWnd, FALSE, str_ptr, str_count, cx, st_size_);
495 }
496
497 /**
498 * IME���O���Q�������@�\�p�\��������������
499 * unicode�E�B���h�E�p
500 * msg == WM_IME_REQUEST,wParam == IMR_DOCUMENTFEED ���������g��
501 * str_ptr �Q�������������|�C���^
502 * str_count �Q����������������(wchar_t��)
503 * cx �J�[�\�����u(wchar_t�P��)
504 * st_size ���������\�������T�C�Y(byte)
505 * �����l �\���������|�C���^
506 * �s�v���������� free()��������
507 */
508 void *CreateReconvStringStW(
509 HWND hWnd, const wchar_t *str_ptr, size_t str_count,
510 size_t cx, size_t *st_size_)
511 {
512 if (HIMEDLL == NULL) return NULL;
513 //assert(IsWindowUnicode(hWnd) == TRUE);
514 return CreateReconvStringSt(hWnd, TRUE, str_ptr, str_count, cx, st_size_);
515 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26