/
winimm.c
2619 lines (2244 loc) · 76.1 KB
/
winimm.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
/* esecannaserver --- pseudo canna server that wraps another IME.
* Copyright (C) 1999-2000 Yasuhiro Take
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* ree Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <iconv.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pwd.h>
#include <sys/types.h>
#include <pthread.h>
#include "def.h"
#include "misc.h"
#include "winimm.h"
#include <errno.h>
extern int errno;
extern void sig_terminate();
extern char convmode; /* 2004.12.06 Y.A. */
typedef struct _winimm_t
{
void *dummy; /* ダミー */
} winimm_t;
/* ここで保持しているszCompReadStr,szCompStr等は、あくまでも変換中のすべての情報であり、resize_pauseで途中からの時も全体が入っている */
typedef struct _context_t
{
struct _context_t *prev, *next; /* コンテキストのリンク用 */
short context_num; /* コンテキスト番号 */
int client_id; /* クライアント(Wnn用とかATOK用とか)の識別子 */
int fIME; /* 現在の入力ロケールにIMEがあるかどうか */
HIMC hIMC; /* オープンしているIMのハンドル(0ならクローズ状態) */
BOOL fOpen; /* オープン中か否か */
DWORD fdwConversion; /* かな漢の入力モード */
DWORD fdwSentence; /* かな漢の変換モード */
int cur_bun_no; /* 現在候補をオープンしている文節番号 04.10.04 Y.A. */
/* >> 変換中の状態保持用 */
ushort* szYomiStr; /* 外から指定された読み(これはCannaのワイド文字列) */
/* は止めて、szCompReadStrを全角・Cannaのワイド文字にしたものを入れておく */
LPDWORD dwYomiCls; /* 全角読みの情報(バイト単位) */
DWORD dwYomiClsLen; /* dwYomiClsの長さ */
LPWSTR szCompStr; /* 現在の変換後文字列 */
LPDWORD dwCompCls; /* 文節の情報(バイト単位) */
DWORD dwCompClsLen; /* dwCompClsの長さ */
LPBYTE bCompAttr; /* 属性の情報 */
DWORD dwCompAttrLen; /* bCompAttrの長さ */
LPWSTR szCompReadStr; /* 現在の変換後の読み(どうやら半角で来る) */
LPDWORD dwCompReadCls; /* (バイト単位) */
DWORD dwCompReadClsLen; /* */
LPBYTE bCompReadAttr; /* */
DWORD dwCompReadAttrLen; /* */
/* << 変換中の状態保持用 */
} context_t;
static context_t *cx_top; /* esecanna用のコンテキストのリスト(context_t)の先頭要素へのポインタ */
static client_t *client; /* esecannaが持っているクライアント(wnn用のモジュールとか)へのポインタ */
HWND hWnd_IMM; /* かな漢動作用のウィンドウ */
/* static HIMC DefaultIMMContext; *//* hWnd_IMMのデフォルト入力コンテキスト */
static short last_context_num; /* 現在のコンテキスト番号(コンテキストごとの状態の設定と変更用) */
static HDESK hdeskCurrent; /* 初期状態のデスクトップ */
static HDESK hdesk; /* オープンしたデスクトップ */
static HWINSTA hwinsta; /* */
static HWINSTA hwinstaCurrent; /* */
static int wm_create_done; /* */
#define WW_ERROR16(_buf) { \
cannaheader_t *he; \
he = (cannaheader_t *)(_buf); \
he->datalen = LSBMSB16(2); \
he->err.e16 = LSBMSB16(-1); \
}
#define WW_ERROR8(_buf) { \
cannaheader_t *he; \
he = (cannaheader_t *)(_buf); \
he->datalen = LSBMSB16(1); \
he->err.e8 = -1; \
}
/* kana_tableから ウ を外したもの */
#define MOD_TEN 0x01 /* ゛ が次に来た場合,合成(カ + ゛= ガ)するか */
#define MOD_MARU 0x02 /* ゜ が次に来た場合,合成するか */
#define MOD_TM 0x03 /* ハ行は, ゛ と ゜ 両方受け付ける */
/* cannawcsバージョン */
struct {
ushort han_start, han_end;
uchar modifiers:4,
offset:4;
ushort zenkaku2_start;
} hira_table[] =
{
/* han_start, han_end, modifiers, offset, zenkaku2_start */
{0xA600, 0xA600, 0, 0, 0xF2}, /* ヲ */
{0xA700, 0xAB00, 0, 2, 0xA1}, /* ァ-ォ */
{0xAC00, 0xAE00, 0, 2, 0xE3}, /* ャ-ョ */
{0xAF00, 0xAF00, 0, 0, 0xC3}, /* ッ */
{0xB100, 0xB500, 0, 2, 0xA2}, /* ア-オ */
{0xB600, 0xBA00, MOD_TEN, 2, 0xAB}, /* カ-コ */
{0xBB00, 0xBF00, MOD_TEN, 2, 0xB5}, /* サ-ソ */
{0xC000, 0xC100, MOD_TEN, 2, 0xBF}, /* タ-チ */
{0xC200, 0xC200, MOD_TEN, 0, 0xC4}, /* ツ */
{0xC300, 0xC400, MOD_TEN, 2, 0xC6}, /* テ-ト */
{0xC500, 0xC900, 0, 1, 0xCA}, /* ナ-ノ */
{0xCA00, 0xCE00, MOD_TM, 3, 0xCF}, /* ハ-ホ */
{0xCF00, 0xD300, 0, 1, 0xDE}, /* マ-モ */
{0xD400, 0xD600, 0, 2, 0xE4}, /* ヤ-ヨ */
{0xD700, 0xDB00, 0, 1, 0xE9}, /* ラ-ロ */
{0xDC00, 0xDC00, 0, 0, 0xEF}, /* ワ */
{0xDD00, 0xDD00, 0, 0, 0xF3}, /* ン */
{ 0, 0, 0, 0, 0}
};
ushort daku_table[] =
{
0xACA4, 0xAEA4, 0xB0A4, 0xB2A4, 0xB4A4, /* がぎぐげご */
0xB6A4, 0xB8A4, 0xBAA4, 0xBCA4, 0xBEA4, /* ざじずぜぞ */
0xC0A4, 0xC2A4, 0xC5A4, 0xC7A4, 0xC9A4, /* だぢづでど */
0xD0A4, 0xD3A4, 0xD6A4, 0xD9A4, 0xDCA4, /* ばびぶべぼ */
0xD1A4, 0xD4A4, 0xD7A4, 0xDAA4, 0xDDA4, /* ぱぴぷぺぽ */
0
};
/* 最低限必要なプロトタイプ */
static ushort *mw_after_conversion(context_t *cx, HIMC hIMC, int *nbun, int bun, int *len_r);
static int mw_open_imm32(int id, context_t *cx, char *envname);
static int mw_set_target_clause(context_t *cx, HIMC hIMC, int bun_no);
/*
* wrapper functions から使われる ユーティリティ関数s
*/
/* コンテキスト関連 */
/*
mw_switch_context
指定されたコンテキスト情報を元にかな漢の状態を切り替える
*/
static short mw_switch_context(context_t *cx)
{
HIMC hIMC = 0;
BOOL fRet;
int nbun, len; /* 04.10.05 Y.A. */
if (hWnd_IMM == 0)
return 0;
hIMC = ImmGetContext(hWnd_IMM);
if (hIMC == 0)
return 0;
if (cx->szCompReadStr != NULL)
{
/* 現在変換中のものがあれば終わらせる */
mw_set_target_clause(cx, hIMC, cx->cur_bun_no);
ImmNotifyIME(hIMC, NI_OPENCANDIDATE, 0, 0); /* 失敗しても無視 */
/* 読みの再設定 */
fRet = ImmSetCompositionStringW(hIMC, SCS_SETSTR, NULL, 0, cx->szCompReadStr, wcslen(cx->szCompReadStr) * 2); /* 読み設定 */
if (fRet == FALSE)
{
m_msg_dbg("Error:ImmSetCompositionString(SCS_SETSTR) @ mw_switch_context\n");
ImmReleaseContext(hWnd_IMM, hIMC);
return 0;
}
fRet = ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CONVERT, 0); /* 変換実行 */
if (fRet == FALSE)
{
m_msg_dbg("Error:ImmNotifyIME(NI_COMPOSITIONSTR) @ mw_switch_context\n");
ImmReleaseContext(hWnd_IMM, hIMC);
return 0;
}
/* 候補リストはクローズ */
}
cx->cur_bun_no = -1; /* 04.10.04 Y.A. */
ImmReleaseContext(hWnd_IMM, hIMC);
return 1;
}
/*
mw_new_context
新しいコンテキストの情報を作成する
*/
static short mw_new_context(int id)
{
context_t *cx, *new_context;
short cx_num;
/* コンテキストの情報のメモリが取れなかったらエラー */
if ((new_context = (context_t *)calloc(1, sizeof(context_t))) == NULL)
return -1;
/* new_contextをコンテキストのリストに組み込む */
cx = cx_top;
if (cx)
{
while (cx->next)
cx = cx->next;
cx->next = new_context;
new_context->prev = cx;
} else
{
cx_top = new_context;
}
/* リストの最後(空の要素の次)を探して空の要素に現在のクライアント情報を埋める */
cx_num = 1;
for (;;)
{
cx = cx_top;
for (;;)
{
if (cx == NULL)
{
new_context->context_num = cx_num;
new_context->client_id = id;
new_context->fIME = 0;
mw_switch_context(new_context);
return cx_num;
}
if (cx->context_num == cx_num)
{
cx_num++;
break;
}
cx = cx->next;
}
}
}
/*
mw_get_context
指定されたコンテキスト番号のコンテキスト情報を取得する
*/
static context_t *mw_get_context(short cx_num)
{
context_t *cx;
if (cx_num == -1)
return NULL;
cx = cx_top;
while (cx)
{
if (cx->context_num == cx_num)
{
if (cx->context_num != last_context_num)
{
mw_switch_context(cx);
last_context_num = cx->context_num;
}
return cx;
}
cx = cx->next;
}
return NULL;
}
/*
mw_sub_clear_context
指定されたコンテキスト番号のコンテキスト情報の中身をクリアする
*/
static int mw_sub_clear_context(context_t *cx)
{
if (cx->szYomiStr != NULL)
MYFREE(cx->szYomiStr);
if (cx->dwYomiCls != NULL)
MYFREE(cx->szYomiStr);
if (cx->szCompStr != NULL)
MYFREE(cx->szCompStr);
if (cx->szCompReadStr != NULL)
MYFREE(cx->szCompReadStr);
if (cx->bCompAttr != NULL)
MYFREE(cx->bCompAttr);
if (cx->dwCompCls != NULL)
MYFREE(cx->dwCompCls);
if (cx->bCompReadAttr != NULL)
MYFREE(cx->bCompReadAttr);
if (cx->dwCompReadCls != NULL)
MYFREE(cx->dwCompReadCls);
cx->dwYomiClsLen = cx->dwCompClsLen = cx->dwCompAttrLen = cx->dwCompReadClsLen = cx->dwCompReadAttrLen = 0;
cx->cur_bun_no = -1; /* 04.10.04 Y.A. */
return 0;
}
/*
mw_clear_context
指定されたコンテキスト番号のコンテキスト情報の中身をクリアする
*/
static int mw_clear_context(short cx_num)
{
context_t *cx;
cx = mw_get_context(cx_num);
mw_sub_clear_context(cx);
return 0;
}
/*
mw_free_context
指定されたコンテキスト番号のコンテキスト情報本体を開放する
*/
static int mw_free_context(short cx_num)
{
context_t *cx;
cx = mw_get_context(cx_num);
mw_clear_context(cx_num);
if (cx->prev)
cx->prev->next = cx->next;
else
cx_top = cx->next;
if (cx->next)
cx->next->prev = cx->prev;
free(cx);
return 0;
}
/*
* Wnn から呼ばれる関数
*/
/* 作り直し、っていうか必要? */
static int mw_ime32_message(char *s)
{
return 0;
}
/*
*
*/
/*
mw_open_imm32
かな漢動作開始
esecannaのコンテキスト毎にWindowsの入力コンテキストを作成する
*/
static int mw_open_imm32(int id, context_t *cx, char *envname)
{
HIMC hIMC;
if (hWnd_IMM == 0)
return 0;
hIMC = ImmGetContext(hWnd_IMM);
if (hIMC == 0)
return 0;
else
{
/* 状態の確認 */
HKL hKL = GetKeyboardLayout(0);
cx->fIME = ImmIsIME(hKL);
if (cx->fIME != 0)
{
ImmGetConversionStatus(hIMC, &(cx->fdwConversion), &(cx->fdwSentence));
cx->fOpen = ImmGetOpenStatus(hIMC);
}
ImmReleaseContext(hWnd_IMM, hIMC);
}
return 1;
}
/*
mw_close_imm32
クローズする(入力コンテキストを破棄する)
*/
static int mw_close_imm32(context_t *cx)
{
if (cx->fIME)
{
HIMC hIMC;
if (hWnd_IMM == 0)
return 0;
hIMC = ImmGetContext(hWnd_IMM);
if (hIMC == 0)
return 0;
/* クローズする */
if (ImmGetOpenStatus(hIMC) == TRUE)
{
ImmSetOpenStatus(hIMC, FALSE);
cx->fOpen = FALSE;
}
ImmReleaseContext(hWnd_IMM, hIMC);
}
return 1;
}
/*
* wcs2ucs() - かんなで使われるワイドキャラクタからUCS-2へ
* lenは文字数。とりあえずcannawc2euc()とeuc2sjis()を使って書いておくけど、
* 後で一個にしよう
*/
static LPWSTR mw_wcs2ucs(ushort *src)
{
int wclen, euclen;
char *workeuc;
LPWSTR dst;
static buffer_t zbuf;
iconv_t cd = (iconv_t)-1;
size_t status, inbytesleft, outbytesleft;
char *inbuf, *outbuf;
/* 途中で使用するeuc用のバッファ確保 */
wclen = cannawcstrlen(src);
workeuc = (char*)calloc(1, wclen * 3);
/* 一旦eucへ */
euclen = cannawc2euc(src, wclen, workeuc, wclen * 3); /* cannawc2euc()のsrcの長さは文字数、destの長さはバイト数、戻り値はバイト数 */
outbytesleft = (wclen + 1) * 2;
buffer_check(&zbuf, outbytesleft); /* UCSは一文字2byte */
dst = (LPWSTR)(zbuf.buf);
/* eucからUCS2へ */
cd = iconv_open("UCS-2-INTERNAL", "EUC-JP");
if (cd == (iconv_t)-1)
{
dst = NULL;
goto end_exit;
}
inbytesleft = euclen;
inbuf = workeuc;
outbuf = (char*)dst;
status = iconv(cd, (const char **)&inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (status == (size_t)-1)
{
dst = NULL;
goto end_exit;
}
dst[(((wclen + 1) * 2) - outbytesleft) / 2] = L'\0';
end_exit:
free(workeuc);
if (cd != (iconv_t)-1)
iconv_close(cd);
return dst;
}
/*
* ucs2wcs() -UCS-2から かんなで使われるワイドキャラクタへ
*
* srclen: 文字数
*/
static ushort *mw_ucs2wcs(LPCWSTR src, int srclen)
{
int euclen;
char *workeuc;
ushort *dst;
static buffer_t zbuf;
iconv_t cd = (iconv_t)-1;
size_t inbytesleft, status, outbytesleft;
uchar *inbuf;
char *outbuf;
int i;
ushort *workucs;
/* 途中で使用するeuc用のバッファ確保 */
workeuc = (char*)calloc(1, srclen * 3 + 1);
/* 途中で使用するUCS用のバッファ確保 */
workucs = (ushort*)calloc(1, srclen * 2);
/* UCS-2から一旦eucへ */
cd = iconv_open("EUC-JP", "UCS-2-INTERNAL");
if (cd == (iconv_t)-1)
{
dst = NULL;
goto end_exit;
}
/* Windows固有のコードをパッチする */
for (i=0; i<srclen; i++)
{
switch(src[i])
{
case 0x005C: /* \ */
workucs[i] = 0x00A5;
break;
case 0xFF5E: /* ~ */
workucs[i] = 0x301C;
break;
case 0x2225: /* ∥ */
workucs[i] = 0x2016;
break;
case 0xFF0D: /* - */
workucs[i] = 0x2015;
break;
case 0xFFE0: /* ¢ */
workucs[i] = 0x00A2;
break;
case 0xFFE1: /* £ */
workucs[i] = 0x00A3;
break;
case 0xFFE2: /* ¬ */
workucs[i] = 0x00AC;
break;
default:
workucs[i] = src[i];
break;
}
}
inbytesleft = srclen * 2;
outbytesleft = srclen * 3 + 1;
inbuf = (char*)&workucs[0];
outbuf = workeuc;
while(inbytesleft > 0)
{
status = iconv(cd, (const char **)&inbuf, &inbytesleft, &outbuf, &outbytesleft);
if (status == (size_t)-1)
{
/* 表現できない文字がきたので'?'で埋める */
*outbuf++ = 0xA1;
*outbuf++ = 0xA9;
inbuf += 2;
inbytesleft -= 2;
outbytesleft -= 2;
}
}
workeuc[(srclen * 3 + 1) - outbytesleft] = '\0';
euclen = strlen(workeuc);
buffer_check(&zbuf, srclen * 2 + 2);
dst = (ushort *)(zbuf.buf);
/* eucからcannawcへ */
euc2cannawc(workeuc, euclen, dst, srclen * 2 + 2); /* euc2cannawc()のsrcの長さはバイト数、destの長さは文字数、戻り値は文字数 */
end_exit:
free(workeuc);
if (cd != (iconv_t)-1)
iconv_close(cd);
return dst;
}
/*
* wcs2sjis() - かんなで使われるワイドキャラクタからsjisへ
* lenは文字数。とりあえずcannawc2euc()とeuc2sjis()を使って書いておくけど、
* 後で一個にしよう
*/
static LPSTR mw_wcs2sjis(ushort *src)
{
int wclen, euclen;
char *workeuc;
LPSTR dst;
static buffer_t zbuf;
/* 途中で使用するeuc用のバッファ確保 */
wclen = cannawcstrlen(src);
workeuc = (char*)calloc(1, wclen * 3);
/* 一旦eucへ */
euclen = cannawc2euc(src, wclen, workeuc, wclen * 3); /* cannawc2euc()のsrcの長さは文字数、destの長さはバイト数、戻り値はバイト数 */
buffer_check(&zbuf, wclen * 2 + 1); /* sjisはdbcsだから最大2byte */
dst = (LPSTR)(zbuf.buf);
/* eucからsjisへ */
euc2sjis(workeuc, euclen, dst, wclen * 2 + 1); /* euc2sjis()のsrc,destともに長さはバイト数、戻り値はバイト数 */
free(workeuc);
return dst;
}
/*
* sjis2wcs() -sjisから かんなで使われるワイドキャラクタへ
*/
static ushort *mw_sjis2wcs(LPCSTR src, int srclen)
{
/* int srclen, euclen; */
int euclen;
char *workeuc;
ushort *dst;
static buffer_t zbuf;
/* 途中で使用するeuc用のバッファ確保 */
workeuc = (char*)calloc(1, srclen * 3);
/* sjisから一旦eucへ */
euclen = sjis2euc((uchar*)src, srclen, workeuc, srclen * 3);
buffer_check(&zbuf, srclen * 2 + 2);
dst = (ushort *)(zbuf.buf);
/* eucからcannawcへ */
euc2cannawc(workeuc, euclen, dst, srclen * 2 + 2); /* euc2cannawc()のsrcの長さはバイト数、destの長さは文字数、戻り値は文字数 */
free(workeuc);
return dst;
}
/*
* mw_after_selectcand() - 変換候補を選択後呼ぶ。指定文節の現在の候補を得る。
*
* 引数 - bun 指定文節
* 戻り値 - len_r 文字数
*
*/
static ushort *mw_after_selectcand(context_t *cx, HIMC hIMC, int bun, int *len_r)
{
BOOL nRet = TRUE;
long BufLen;
ushort *ret;
/* context_t cx; */
/* 前回までの後始末 */
mw_sub_clear_context(cx);
/* 文節位置の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPCLAUSE, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->dwCompCls = (LPDWORD)calloc(1, BufLen);
cx->dwCompClsLen = ImmGetCompositionStringW(hIMC, GCS_COMPCLAUSE, cx->dwCompCls, BufLen);
}
/* 変換後文字列取得 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0); /* 変換後取得 */
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->szCompStr = (LPWSTR)calloc(1, BufLen + 1);
ImmGetCompositionStringW(hIMC, GCS_COMPSTR, cx->szCompStr, BufLen + 1);
}
/* 読み文字列取得 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPREADSTR, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->szCompReadStr = (LPWSTR)calloc(1, BufLen + 1);
ImmGetCompositionStringW(hIMC, GCS_COMPREADSTR, cx->szCompReadStr, BufLen + 1);
}
/* 文節属性の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->bCompAttr = (LPBYTE)calloc(1, BufLen);
cx->dwCompAttrLen = ImmGetCompositionStringW(hIMC,GCS_COMPATTR,cx->bCompAttr,BufLen);
}
/* 読み文節の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPREADCLAUSE, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->dwCompReadCls = (LPDWORD)calloc(1, BufLen);
cx->dwCompReadClsLen = ImmGetCompositionStringW(hIMC,GCS_COMPREADCLAUSE,cx->dwCompReadCls,BufLen);
}
/* 読み属性の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPREADATTR, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->bCompReadAttr = (LPBYTE)calloc(1, BufLen);
cx->dwCompReadAttrLen = ImmGetCompositionStringW(hIMC,GCS_COMPREADATTR,cx->bCompReadAttr,BufLen);
}
end_exit:
if (nRet == FALSE)
{ /* 変換に失敗したようなので後始末 */
ret = NULL;
*len_r = 0;
} else
{ /* 成功したのでcannawcを作って終了 */
ushort *workp;
int worklen;
int i;
static buffer_t zbuf;
/* まず文字数を数える */
workp = mw_ucs2wcs(&(cx->szCompStr[cx->dwCompCls[bun]]), wcslen(&(cx->szCompStr[cx->dwCompCls[bun]])));
if (workp == NULL)
{
ret = NULL;
*len_r = 0;
goto end_exit_2;
}
worklen = cannawcstrlen(workp);
/* 必要となるバッファを確保 */
buffer_check(&zbuf, (worklen + 1) * 2); /* 文字+ターミネート */
ret = (ushort *)(zbuf.buf);
memset((LPVOID)ret, 0, (worklen + 1) * 2);
/* 文節ごとにsjis->cannawc変換する */
*len_r = 0;
workp = mw_ucs2wcs(&(cx->szCompStr[cx->dwCompCls[bun]]), cx->dwCompCls[bun+1] - cx->dwCompCls[bun]);
if (workp == NULL)
{
ret = NULL;
*len_r = 0;
goto end_exit_2;
}
worklen = cannawcstrlen(workp);
memcpy(ret, workp, worklen * 2);
*len_r = worklen;
/* 最後に戻り値を文字列の先頭にする */
ret = (ushort *)(zbuf.buf);
}
end_exit_2:
return ret;
}
/*
* mw_after_conversion() - 変換後呼ぶ。指定文節からの各文節の最優先候補を得る。
*
* 引数 - bun 指定文節
* 戻り値 - nbun 文節数
* len_r
*
*/
static ushort *mw_after_conversion(context_t *cx, HIMC hIMC, int *nbun, int bun, int *len_r)
{
BOOL nRet = TRUE;
long BufLen;
ushort *ret;
/* 前回までの後始末 */
/* mw_clear_context(cx->context_num); */
mw_sub_clear_context(cx);
/* 文節位置の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPCLAUSE, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->dwCompCls = (LPDWORD)calloc(1, BufLen);
cx->dwCompClsLen = ImmGetCompositionStringW(hIMC, GCS_COMPCLAUSE, cx->dwCompCls, BufLen);
*nbun = cx->dwCompClsLen / sizeof(DWORD) - 1; /* バイトサイズを配列の要素数に変換 */
}
/* 変換後文字列取得 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPSTR, NULL, 0); /* 変換後取得 */
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->szCompStr = (LPWSTR)calloc(1, BufLen + 1);
ImmGetCompositionStringW(hIMC, GCS_COMPSTR, cx->szCompStr, BufLen + 1);
}
/* 読み文字列取得 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPREADSTR, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->szCompReadStr = (LPWSTR)calloc(1, BufLen + 1);
ImmGetCompositionStringW(hIMC, GCS_COMPREADSTR, cx->szCompReadStr, BufLen + 1);
}
/* 文節属性の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPATTR, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->bCompAttr = (LPBYTE)calloc(1, BufLen);
cx->dwCompAttrLen = ImmGetCompositionStringW(hIMC,GCS_COMPATTR,cx->bCompAttr,BufLen);
}
/* 読み文節の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPREADCLAUSE, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->dwCompReadCls = (LPDWORD)calloc(1, BufLen);
cx->dwCompReadClsLen = ImmGetCompositionStringW(hIMC,GCS_COMPREADCLAUSE,cx->dwCompReadCls,BufLen);
}
/* 読み属性の情報 */
BufLen = ImmGetCompositionStringW(hIMC, GCS_COMPREADATTR, NULL, 0);
if ((BufLen == IMM_ERROR_NODATA) || (BufLen == IMM_ERROR_GENERAL) || (BufLen == 0))
{
nRet = FALSE;
goto end_exit;
} else
{
cx->bCompReadAttr = (LPBYTE)calloc(1, BufLen);
cx->dwCompReadAttrLen = ImmGetCompositionStringW(hIMC,GCS_COMPREADATTR,cx->bCompReadAttr,BufLen);
}
end_exit:
if (nRet == FALSE)
{ /* 変換に失敗したようなので後始末 */
mw_clear_context(cx->context_num);
ret = NULL;
*len_r = 0;
} else
{ /* 成功したのでcannawcを作って終了 */
ushort *workp;
int worklen;
int i;
static buffer_t zbuf;
/* まず文字数を数える */
workp = mw_ucs2wcs(&(cx->szCompStr[cx->dwCompCls[bun]]), wcslen(&(cx->szCompStr[cx->dwCompCls[bun]])));
if (workp == NULL)
{
mw_clear_context(cx->context_num);
ret = NULL;
*len_r = 0;
goto end_exit_2;
}
worklen = cannawcstrlen(workp);
/* 必要となるバッファを確保 */
buffer_check(&zbuf, (worklen + *nbun + 1) * 2); /* 文字+文節間のスペース+ターミネート */
ret = (ushort *)(zbuf.buf);
memset((LPVOID)ret, 0, (worklen + *nbun + 1) * 2);
/* 文節ごとにsjis->cannawc変換する */
*len_r = 0;
for (i=bun; i<*nbun; i++)
{
workp = mw_ucs2wcs(&(cx->szCompStr[cx->dwCompCls[i]]), cx->dwCompCls[i+1] - cx->dwCompCls[i]);
if (workp == NULL)
{
mw_clear_context(cx->context_num);
ret = NULL;
*len_r = 0;
goto end_exit_2;
}
worklen = cannawcstrlen(workp);
memcpy(ret, workp, worklen * 2);
*len_r += (worklen + 1);
ret += (worklen + 1);
}
/* 最後に戻り値を文字列の先頭にする */
ret = (ushort *)(zbuf.buf);
}
end_exit_2:
return ret;
}
/*
* mw_set_target_clause()
*
* 指定された文節を変換対象文節にする
*
* 戻り値: 失敗: -1 成功: 現在の文節番号
*
*/
static int mw_set_target_clause(context_t *cx, HIMC hIMC, int bun_no)
{
int fRet = 0, CurClause = -1;
UINT uClause, uMaxClause, uCnt, uCntRead, k;
DWORD i, j;
BOOL fAttrOK = FALSE, fAttrReadOK = FALSE;
BYTE bAt;
int nbun, len;
#if 0 /* どうもだめだ 04.10.08 Y.A. */
/* >> 現在の文節情報を取り直してみたりする 04.10.07 Y.A. */
if (mw_get_curclause(cx, hIMC) == FALSE)
{
return -1; /* 現在変換中の文字列は無い */
}
/* << 現在の文節情報を取り直してみたりする 04.10.07 Y.A. */
#endif /* どうもだめだ 04.10.08 Y.A. */
/* >> 現在の変換対象の文節が指定された文節と一致するか? */
uMaxClause = (UINT)(cx->dwCompClsLen / sizeof(DWORD)) - 1;
if (uMaxClause <= 0)
{
return -1; /* 現在変換中の文字列は無い */
}
/* カウンタにCurClause使っちゃ次の判断がだめじゃんよ~ 04.10.06 Y.A. */
for (k = 0; k < uMaxClause; k++)
{
if ((cx->bCompAttr[cx->dwCompCls[k]] == ATTR_TARGET_CONVERTED) ||
(cx->bCompAttr[cx->dwCompCls[k]] == ATTR_TARGET_NOTCONVERTED))
{
CurClause = k;
break;
}
}
if (CurClause == -1)
{
return -1; /* 変換対象の文節が無いのは多分エラーだろう */
}
/* << 現在の変換対象の文節が指定された文節と一致するか? */
/* >> 現在の変換対象の文節を指定された文節に移動 */
if (CurClause != bun_no)
{
/* >> 04.10.04 Y.A. */
if (cx->cur_bun_no != -1)
{
cx->cur_bun_no = -1;
}
/* >> 04.10.04 Y.A. */
uMaxClause = (cx->dwCompClsLen / sizeof(DWORD)) - 1;
uClause = bun_no;
uCnt = 0;
if (uClause < uMaxClause)
{
for (i=0; i < uMaxClause; i++)
{
if (i == uClause)
{
switch (cx->bCompAttr[cx->dwCompCls[i]])
{
case ATTR_INPUT:
bAt = ATTR_TARGET_NOTCONVERTED;
break;
case ATTR_CONVERTED:
bAt = ATTR_TARGET_CONVERTED;