~mshinke/nvdajp/betterBraille

« back to all changes in this revision

Viewing changes to nvdaHelper/nvdajpimeRPC/nvdajpime.cpp

  • Committer: Masataka Shinke
  • Date: 2011-10-25 12:35:26 UTC
  • mfrom: (4175.1.10 jpmain)
  • mto: (4175.1.36 jpmain)
  • mto: This revision was merged to the branch mainline in revision 4193.
  • Revision ID: mshinke@users.sourceforge.jp-20111025123526-ze527a2rl3z0g2ky
lp:~nishimotz/nvdajp/main : 4185 をマージ

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
#A part of NonVisual Desktop Access (NVDA)
 
3
#Copyright (C) 2006-2010 NVDA Contributors <http://www.nvda-project.org/>
 
4
#This file is covered by the GNU General Public License.
 
5
#See the file COPYING for more details.
 
6
#
 
7
# nvdajpime
 
8
# by Masataka Shinke
 
9
*/
 
10
 
 
11
// nvdajpime.cpp : DLL アプリケーション用にエクスポートされる関数を定義します。
 
12
//
 
13
 
 
14
#include "nvdajpime.h"
 
15
#include "tls.h"
 
16
#include "tsf.h"
 
17
 
 
18
 
 
19
 
 
20
void* __RPC_USER midl_user_allocate(size_t size) {
 
21
        return malloc(size);
 
22
}
 
23
 
 
24
void __RPC_USER midl_user_free(void* p) {
 
25
        free(p);
 
26
}
 
27
 
 
28
 
 
29
HMODULE                         g_hModule;
 
30
 
 
31
Cnvdajpime*                     g_Cnvdajpime;
 
32
 
 
33
TLS                                     g_TLS;
 
34
DWORD                           TLS::dwTLSIndex = TLS_OUT_OF_INDEXES;
 
35
 
 
36
CnvdajpimeSharedMem     g_SharedMemory;
 
37
 
 
38
bool xx(WCHAR* buff)
 
39
{
 
40
        #ifdef NVDAJPIME_RPC
 
41
                wchar_t desktopSpecificNamespace[64];
 
42
                generateDesktopSpecificNamespace(desktopSpecificNamespace,ARRAYSIZE(desktopSpecificNamespace));
 
43
                wstringstream s;
 
44
                s<<L"ncalrpc:[NvdaCtlr."<<desktopSpecificNamespace<<L"]";
 
45
                RpcBindingFromStringBinding((RPC_WSTR)(s.str().c_str()),&nvdaControllerBindingHandle);
 
46
                nvdajpimeRPC_speakText(buff);
 
47
                //nvdajpimeRPC_speakIme(13,buff,0,buff,buff,0);
 
48
                RpcBindingFree(&nvdaControllerBindingHandle);
 
49
        #else
 
50
                //
 
51
        #endif
 
52
        return TRUE;
 
53
}
 
54
 
 
55
bool yy(WCHAR* buff)
 
56
{
 
57
        #ifdef NVDAJPIME_RPC
 
58
                wchar_t desktopSpecificNamespace[64];
 
59
                generateDesktopSpecificNamespace(desktopSpecificNamespace,ARRAYSIZE(desktopSpecificNamespace));
 
60
                wstringstream s;
 
61
                s<<L"ncalrpc:[NvdaCtlr."<<desktopSpecificNamespace<<L"]";
 
62
                RpcBindingFromStringBinding((RPC_WSTR)(s.str().c_str()),&nvdaControllerBindingHandle);
 
63
                //nvdajpimeRPC_speakText(buff);
 
64
                nvdajpimeRPC_speakIme(13,L"",0,buff,L"",0);
 
65
                RpcBindingFree(&nvdaControllerBindingHandle);
 
66
        #else
 
67
                //
 
68
        #endif
 
69
        return TRUE;
 
70
}
 
71
 
 
72
//
 
73
//
 
74
//
 
75
WCHAR* Diff(WCHAR* pOld,WCHAR* pNew,UINT pFirstCode,UINT pLastCode)
 
76
{
 
77
 
 
78
        std::wstring Old(pOld);
 
79
        std::wstring New(pNew);
 
80
        std::wstring Dif(L"");
 
81
        WCHAR Ret[256]={NULL};
 
82
 
 
83
        if(!New.empty())
 
84
        {
 
85
                switch(pLastCode)
 
86
                {
 
87
                case 28: // VK_CONVERT
 
88
                case 32:   // VK_SPACE
 
89
                case 38:   // VK_UP
 
90
                case 40:   // VK_DOWN
 
91
                case 117: // VK_F6
 
92
                case 118: // VK_F7
 
93
                case 119: // VK_F8
 
94
                case 120: // VK_F9
 
95
                case 121: // VK_F10
 
96
                        if(New!=Old)
 
97
                                wsprintf(Ret,L"%s",New.c_str());
 
98
                        else if(pFirstCode!=pLastCode)                  //@@
 
99
                                wsprintf(Ret,L"%s",New.c_str());        //@@
 
100
                        else if(pFirstCode==38)                                 //@@
 
101
                                wsprintf(Ret,L"%s",New.c_str());        //@@
 
102
                        else if(pFirstCode==40)                                 //@@
 
103
                                wsprintf(Ret,L"%s",New.c_str());        //@@
 
104
                        else
 
105
                                wsprintf(Ret,L"%s",L"");
 
106
                        return &Ret[0];
 
107
                        break;          
 
108
                default:
 
109
                        break;
 
110
                }
 
111
        }
 
112
 
 
113
        for(int i=(UINT)Old.length()-1,j=(UINT)New.length()-1;(i>=0)&&(j>=0);i--,j--)   //@@
 
114
        {
 
115
                if(Old[i]!=New[j])
 
116
                {
 
117
                        Dif = New[j];
 
118
                        wsprintf(Ret,L"%s",Dif.c_str());
 
119
 
 
120
 
 
121
                        if((i>0)&(j>0))
 
122
                        {
 
123
                                if(Old[i-1]!=New[j-1])
 
124
                                {
 
125
 
 
126
                                        if( Dif.compare(std::wstring(L"ゃ")) == 0 )
 
127
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
128
                                        else if (Dif.compare(std::wstring(L"ゅ")) == 0 )
 
129
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
130
                                        else if (Dif.compare(std::wstring(L"ょ")) == 0 )
 
131
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
132
                                        else if (Dif.compare(std::wstring(L"ぁ")) == 0 )
 
133
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
134
                                        else if (Dif.compare(std::wstring(L"ぃ")) == 0 )
 
135
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
136
                                        else if (Dif.compare(std::wstring(L"ぉ")) == 0 )
 
137
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
138
 
 
139
 
 
140
                                        else if (Dif.compare(std::wstring(L"ャ")) == 0 )
 
141
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
142
                                        else if (Dif.compare(std::wstring(L"ュ")) == 0 )
 
143
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
144
                                        else if (Dif.compare(std::wstring(L"ョ")) == 0 )
 
145
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
146
                                        else if (Dif.compare(std::wstring(L"ァ")) == 0 )
 
147
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
148
                                        else if (Dif.compare(std::wstring(L"ィ")) == 0 )
 
149
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
150
                                        else if (Dif.compare(std::wstring(L"ォ")) == 0 )
 
151
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
152
 
 
153
 
 
154
                                        else if (Dif.compare(std::wstring(L"ャ")) == 0 )
 
155
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
156
                                        else if (Dif.compare(std::wstring(L"ュ")) == 0 )
 
157
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
158
                                        else if (Dif.compare(std::wstring(L"ョ")) == 0 )
 
159
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
160
                                        else if (Dif.compare(std::wstring(L"ァ")) == 0 )
 
161
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
162
                                        else if (Dif.compare(std::wstring(L"ィ")) == 0 )
 
163
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
164
                                        else if (Dif.compare(std::wstring(L"ォ")) == 0 )
 
165
                                                wsprintf(Ret,L"%s",New.substr(j-1,2).c_str());
 
166
 
 
167
 
 
168
                                }
 
169
                        }
 
170
 
 
171
 
 
172
                        break;
 
173
                }
 
174
                
 
175
        }
 
176
        if(Dif.empty() & (Old.length()<New.length()) )
 
177
        {
 
178
                Dif = New[New.length()-1];
 
179
                wsprintf(Ret,L"%s",Dif.c_str());
 
180
        }
 
181
 
 
182
        return &Ret[0];
 
183
}
 
184
 
 
185
BOOL GetIMMOpenStatus()
 
186
{
 
187
    HWND hwnd = NULL;
 
188
    HIMC himc = NULL;
 
189
        BOOL Ret = FALSE;
 
190
 
 
191
    hwnd = GetFocus();
 
192
    if (!hwnd)
 
193
        return  GetSharedMemory()->ImeOpenStatus;
 
194
 
 
195
    himc = ImmGetContext(hwnd);
 
196
    if (himc)
 
197
    {
 
198
                Ret = ImmGetOpenStatus(himc);
 
199
        ImmReleaseContext(hwnd, himc);
 
200
    }
 
201
        return Ret;
 
202
}
 
203
 
 
204
// Cnvdajpimeクラス
 
205
 
 
206
/*
 
207
UINT WINAPI Cnvdajpime::Thread(void *p_this)
 
208
{
 
209
        Cnvdajpime*     m_this = reinterpret_cast<Cnvdajpime*>(p_this);
 
210
 
 
211
        HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, EVENT_NAME);
 
212
 
 
213
        while(m_this->m_RunFlag)
 
214
        {
 
215
 
 
216
                WaitForSingleObject(hEvent, INFINITE);
 
217
                ResetEvent(hEvent);
 
218
                //Sleep(1);
 
219
 
 
220
                m_this->m_Result.ImeOpenStatus=GetSharedMemory()->ImeOpenStatus;
 
221
 
 
222
                wsprintf(m_this->m_Result.OldValue,L"%s",m_this->m_Result.NewValue);
 
223
                wsprintf(m_this->m_Result.NewValue,L"%s",GetSharedMemory()->TextValue);
 
224
                wsprintf(m_this->m_Result.DiffValue,L"%s",Diff(m_this->m_Result.OldValue,m_this->m_Result.NewValue,m_this->m_Result.LastKeyCode,GetSharedMemory()->LastKeyCode));
 
225
 
 
226
                m_this->m_Result.LastKeyCode=GetSharedMemory()->LastKeyCode;
 
227
 
 
228
                if(m_this->m_CallBack!=NULL)
 
229
                {
 
230
                        m_this->m_CallBack(
 
231
                                m_this->m_Result.LastKeyCode,
 
232
                                m_this->m_Result.DiffValue,
 
233
                                m_this->m_Result.ImeOpenStatus,
 
234
                                m_this->m_Result.OldValue,
 
235
                                m_this->m_Result.NewValue
 
236
                        );
 
237
                }
 
238
                /////////////////////////////////////////////////////////////
 
239
                #ifdef NVDAJPIME_RPC
 
240
                        if(m_this->m_Result.DiffValue[0]!=0)
 
241
                        {
 
242
                                        wchar_t desktopSpecificNamespace[64];
 
243
                                        generateDesktopSpecificNamespace(desktopSpecificNamespace,ARRAYSIZE(desktopSpecificNamespace));
 
244
                                        wstringstream s;
 
245
                                        s<<L"ncalrpc:[NvdaCtlr."<<desktopSpecificNamespace<<L"]";
 
246
                                        RpcBindingFromStringBinding((RPC_WSTR)(s.str().c_str()),&nvdaControllerBindingHandle);
 
247
 
 
248
                                        if(nvdajpimeRPC_testIfRunning()==0)
 
249
                                        {
 
250
                                                nvdajpimeRPC_speakText(m_this->m_Result.DiffValue);
 
251
                                        }
 
252
                                        RpcBindingFree(&nvdaControllerBindingHandle);
 
253
                        }       
 
254
                #else
 
255
                        //
 
256
                #endif
 
257
 
 
258
 
 
259
        }
 
260
 
 
261
        CloseHandle(hEvent);
 
262
 
 
263
 
 
264
        return 0;
 
265
}
 
266
*/
 
267
 
 
268
 
 
269
 
 
270
//+---------------------------------------------------------------------------
 
271
// _SysGetMsgProc
 
272
//+---------------------------------------------------------------------------
 
273
 
 
274
UINT _SysGetMsgProc(WPARAM wParam, LPARAM lParam)
 
275
{
 
276
    MSG *pmsg;
 
277
    UINT uMsg;
 
278
 
 
279
    pmsg = (MSG *)lParam;
 
280
    uMsg = pmsg->message;
 
281
 
 
282
        BOOL ImeStatus;
 
283
 
 
284
    switch (uMsg)
 
285
    {
 
286
                case WM_IME_NOTIFY:
 
287
                        break;
 
288
        case WM_IME_STARTCOMPOSITION:
 
289
                        break;
 
290
        case WM_IME_ENDCOMPOSITION:
 
291
                        break;
 
292
        case WM_IME_COMPOSITION:
 
293
                        break;
 
294
                case WM_KEYDOWN:
 
295
                        if ( GetAsyncKeyState(VK_SPACE) & 0x8000 )
 
296
                        {
 
297
                                GetSharedMemory()->NewKeyCode = VK_SPACE;
 
298
                                //xx(L"SPACE");
 
299
                        }
 
300
                        else if ( GetAsyncKeyState(VK_CONVERT) & 0x8000 )
 
301
                        {                               
 
302
                                GetSharedMemory()->NewKeyCode  = VK_CONVERT;
 
303
                        }
 
304
                        else if ( GetAsyncKeyState(VK_UP) & 0x8000 )
 
305
                        {                               
 
306
                                GetSharedMemory()->NewKeyCode  = VK_UP;
 
307
                        }
 
308
                        else if ( GetAsyncKeyState(VK_DOWN) & 0x8000 )
 
309
                        {                               
 
310
                                GetSharedMemory()->NewKeyCode  = VK_DOWN;
 
311
                        }
 
312
                        else if ( GetAsyncKeyState(VK_F6) & 0x8000 )
 
313
                        {                               
 
314
                                GetSharedMemory()->NewKeyCode  = VK_F6;
 
315
                        }
 
316
                        else if ( GetAsyncKeyState(VK_F7) & 0x8000 )
 
317
                        {                               
 
318
                                GetSharedMemory()->NewKeyCode  = VK_F7;
 
319
                        }
 
320
                        else if ( GetAsyncKeyState(VK_F8) & 0x8000 )
 
321
                        {                               
 
322
                                GetSharedMemory()->NewKeyCode  = VK_F8;
 
323
                        }
 
324
                        else if ( GetAsyncKeyState(VK_F9) & 0x8000 )
 
325
                        {                               
 
326
                                GetSharedMemory()->NewKeyCode  = VK_F9;
 
327
                        }
 
328
                        else if ( GetAsyncKeyState(VK_F10) & 0x8000 )
 
329
                        {                               
 
330
                                GetSharedMemory()->NewKeyCode  = VK_F10;
 
331
                        }
 
332
                        else if ( GetAsyncKeyState(VK_RETURN) & 0x8000 )
 
333
                        {                               
 
334
                                GetSharedMemory()->NewKeyCode  = VK_RETURN;
 
335
                                if(GetSharedMemory()->ImeOpenStatus)
 
336
                                {
 
337
                                        yy(GetSharedMemory()->NewValue);
 
338
                                }
 
339
                        }
 
340
                        else 
 
341
                        {
 
342
                                GetSharedMemory()->NewKeyCode = 0;
 
343
                        }
 
344
                
 
345
                        if(GetSharedMemory()->ImeOpenStatus)
 
346
                        {
 
347
                                GetSharedMemory()->TsfMode=TRUE;
 
348
                                CnvdajpimeTSF::Init();
 
349
                        }
 
350
                        break;
 
351
                case WM_KEYUP:
 
352
                        break;
 
353
        default:
 
354
                        ImeStatus=GetIMMOpenStatus();
 
355
                        if(ImeStatus!=GetSharedMemory()->ImeOpenStatus)
 
356
                        {
 
357
                                GetSharedMemory()->ImeOpenStatus=ImeStatus;
 
358
                                if ( GetAsyncKeyState(VK_OEM_COPY) & 0x8000 )
 
359
                                {
 
360
                                        GetSharedMemory()->NewKeyCode = VK_OEM_COPY;
 
361
                                }
 
362
                                else if ( GetAsyncKeyState(VK_OEM_AUTO) & 0x8000 )
 
363
                                {
 
364
                                        GetSharedMemory()->NewKeyCode = VK_OEM_AUTO;
 
365
                                }
 
366
                                else if ( GetAsyncKeyState(VK_OEM_ENLW) & 0x8000 )
 
367
                                {
 
368
                                        GetSharedMemory()->NewKeyCode = VK_OEM_ENLW;
 
369
                                }
 
370
                                else if ( GetAsyncKeyState(VK_KANJI) & 0x8000 ) // ALT-` (us-keyboard)
 
371
                                {
 
372
                                        GetSharedMemory()->NewKeyCode = VK_KANJI;
 
373
                                }
 
374
                                else 
 
375
                                {
 
376
                                        GetSharedMemory()->NewKeyCode = 0;
 
377
                                }
 
378
 
 
379
                                if(((GetSharedMemory()->NewKeyCode==VK_OEM_COPY)||(GetSharedMemory()->NewKeyCode==VK_OEM_AUTO)||(GetSharedMemory()->NewKeyCode==VK_KANJI))
 
380
                                        &(ImeStatus==FALSE))
 
381
                                {
 
382
                                        xx(L"にほんごオフ");
 
383
                                }
 
384
                                else if(GetSharedMemory()->NewKeyCode!=0) 
 
385
                                {
 
386
                                        xx(L"にほんごオン");
 
387
                                }
 
388
                        }
 
389
            break;
 
390
    }
 
391
    return 1;
 
392
}
 
393
 
 
394
//+---------------------------------------------------------------------------
 
395
// SysGetMsgProc
 
396
//+---------------------------------------------------------------------------
 
397
 
 
398
LRESULT CALLBACK SysGetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
 
399
{
 
400
    HHOOK hHook;
 
401
 
 
402
    hHook = GetSharedMemory()->hSysGetMsgHook;
 
403
 
 
404
    if (nCode == HC_ACTION && (wParam & PM_REMOVE))
 
405
    {
 
406
        _SysGetMsgProc(wParam, lParam);
 
407
    }
 
408
    return CallNextHookEx(hHook, nCode, wParam, lParam);
 
409
}
 
410
 
 
411
//+---------------------------------------------------------------------------
 
412
//
 
413
// SysGetMsgProc2
 
414
//
 
415
//+---------------------------------------------------------------------------
 
416
 
 
417
LRESULT CALLBACK SysGetMsgProc2(int nCode, WPARAM wParam, LPARAM lParam)
 
418
{
 
419
    HHOOK hHook;
 
420
    hHook = GetSharedMemory()->hSysGetMsgHook2;
 
421
        GetSharedMemory()->NewKeyCode = (UINT)wParam;
 
422
        return CallNextHookEx(hHook, nCode, wParam, lParam);
 
423
}
 
424
 
 
425
//////////////////////////////////////////////////////////////////////////////
 
426
// コンストラクタ
 
427
Cnvdajpime::Cnvdajpime()
 
428
{
 
429
        return;
 
430
}
 
431
 
 
432
//////////////////////////////////////////////////////////////////////////////
 
433
// デストラクタ
 
434
Cnvdajpime::~Cnvdajpime()
 
435
{
 
436
        return;
 
437
}
 
438
 
 
439
//////////////////////////////////////////////////////////////////////////////
 
440
// エクスポート関数。
 
441
extern "C" NVDAJPIME_API BOOL WINAPI Initialize(void)
 
442
{
 
443
 
 
444
        g_Cnvdajpime    = new Cnvdajpime();
 
445
        
 
446
    HHOOK hSysGetMsgHook = SetWindowsHookEx(
 
447
                WH_GETMESSAGE, 
 
448
                SysGetMsgProc, 
 
449
                g_hModule, 
 
450
                0);
 
451
    if (!hSysGetMsgHook)
 
452
        return FALSE;
 
453
    GetSharedMemory()->hSysGetMsgHook = hSysGetMsgHook;
 
454
 
 
455
 
 
456
        /*
 
457
        HHOOK hSysGetMsgHook2 = SetWindowsHookEx(
 
458
                WH_KEYBOARD, 
 
459
                SysGetMsgProc2, 
 
460
                g_hModule, 
 
461
                0);
 
462
 
 
463
    if (!hSysGetMsgHook2)
 
464
        return FALSE;
 
465
    GetSharedMemory()->hSysGetMsgHook2  = hSysGetMsgHook2;      // Masataka.Shinke
 
466
        */
 
467
        
 
468
        GetSharedMemory()->TsfMode=FALSE;
 
469
        
 
470
        GetSharedMemory()->OldKeyCode=0;
 
471
        GetSharedMemory()->NewKeyCode=0;
 
472
        
 
473
        wsprintf(GetSharedMemory()->TextValue,L"%s",L"");
 
474
        wsprintf(GetSharedMemory()->DiffValue,L"%s",L"");
 
475
        wsprintf(GetSharedMemory()->OldValue,L"%s",L"");
 
476
        wsprintf(GetSharedMemory()->NewValue,L"%s",L"");
 
477
        
 
478
        
 
479
        GetSharedMemory()->fReadCompRunning = TRUE;
 
480
        GetSharedMemory()->ImeOpenStatus=TRUE;
 
481
        
 
482
        return TRUE; 
 
483
}
 
484
 
 
485
//////////////////////////////////////////////////////////////////////////////
 
486
// エクスポート関数。
 
487
extern "C" NVDAJPIME_API BOOL WINAPI Terminate(void)
 
488
{
 
489
 
 
490
        delete g_Cnvdajpime;
 
491
 
 
492
        GetSharedMemory()->fReadCompRunning = FALSE;
 
493
 
 
494
        /*
 
495
    HHOOK hSysGetMsgHook2 = GetSharedMemory()->hSysGetMsgHook2;
 
496
    UnhookWindowsHookEx(hSysGetMsgHook2);
 
497
        */
 
498
        
 
499
        HHOOK hSysGetMsgHook = GetSharedMemory()->hSysGetMsgHook;
 
500
    UnhookWindowsHookEx(hSysGetMsgHook);
 
501
 
 
502
        return TRUE;
 
503
 
 
504
}
 
505
 
 
506