4 * Copyright 2008 Aric Stewart, CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/debug.h"
29 #include "wine/list.h"
39 #include "msctf_internal.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf
);
43 static LONG MSCTF_refCount
;
45 static HINSTANCE MSCTF_hinstance
;
55 TF_LANGUAGEPROFILE LanguageProfile
;
56 ITfTextInputProcessor
*pITfTextInputProcessor
;
57 ITfThreadMgr
*pITfThreadMgr
;
58 ITfKeyEventSink
*pITfKeyEventSink
;
60 } ActivatedTextService
;
65 ActivatedTextService
*ats
;
68 static CookieInternal
*cookies
;
70 static UINT array_size
;
72 static struct list AtsList
= LIST_INIT(AtsList
);
73 static UINT activated
= 0;
76 TfClientId processId
= 0;
77 ITfCompartmentMgr
*globalCompartmentMgr
= NULL
;
79 const WCHAR szwSystemTIPKey
[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F','\\','T','I','P',0};
80 const WCHAR szwSystemCTFKey
[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F',0};
82 typedef HRESULT (*LPFNCONSTRUCTOR
)(IUnknown
*pUnkOuter
, IUnknown
**ppvOut
);
88 {&CLSID_TF_ThreadMgr
, ThreadMgr_Constructor
},
89 {&CLSID_TF_InputProcessorProfiles
, InputProcessorProfiles_Constructor
},
90 {&CLSID_TF_CategoryMgr
, CategoryMgr_Constructor
},
94 typedef struct tagClassFactory
96 const IClassFactoryVtbl
*vtbl
;
101 static void ClassFactory_Destructor(ClassFactory
*This
)
103 TRACE("Destroying class factory %p\n", This
);
104 HeapFree(GetProcessHeap(),0,This
);
108 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, LPVOID
*ppvOut
)
111 if (IsEqualIID(riid
, &IID_IClassFactory
) || IsEqualIID(riid
, &IID_IUnknown
)) {
112 IClassFactory_AddRef(iface
);
117 WARN("Unknown interface %s\n", debugstr_guid(riid
));
118 return E_NOINTERFACE
;
121 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
123 ClassFactory
*This
= (ClassFactory
*)iface
;
124 return InterlockedIncrement(&This
->ref
);
127 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
129 ClassFactory
*This
= (ClassFactory
*)iface
;
130 ULONG ret
= InterlockedDecrement(&This
->ref
);
133 ClassFactory_Destructor(This
);
137 static HRESULT WINAPI
ClassFactory_CreateInstance(IClassFactory
*iface
, IUnknown
*punkOuter
, REFIID iid
, LPVOID
*ppvOut
)
139 ClassFactory
*This
= (ClassFactory
*)iface
;
143 TRACE("(%p, %p, %s, %p)\n", iface
, punkOuter
, debugstr_guid(iid
), ppvOut
);
144 ret
= This
->ctor(punkOuter
, &obj
);
147 ret
= IUnknown_QueryInterface(obj
, iid
, ppvOut
);
148 IUnknown_Release(obj
);
152 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
154 ClassFactory
*This
= (ClassFactory
*)iface
;
156 TRACE("(%p)->(%x)\n", This
, fLock
);
159 InterlockedIncrement(&MSCTF_refCount
);
161 InterlockedDecrement(&MSCTF_refCount
);
166 static const IClassFactoryVtbl ClassFactoryVtbl
= {
168 ClassFactory_QueryInterface
,
170 ClassFactory_Release
,
173 ClassFactory_CreateInstance
,
174 ClassFactory_LockServer
177 static HRESULT
ClassFactory_Constructor(LPFNCONSTRUCTOR ctor
, LPVOID
*ppvOut
)
179 ClassFactory
*This
= HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory
));
180 This
->vtbl
= &ClassFactoryVtbl
;
184 TRACE("Created class factory %p\n", This
);
189 /*************************************************************************
190 * DWORD Cookie Management
192 DWORD
generate_Cookie(DWORD magic
, LPVOID data
)
196 /* try to reuse IDs if possible */
197 for (i
= 0; i
< id_last
; i
++)
198 if (cookies
[i
].id
== 0) break;
204 cookies
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CookieInternal
) * 10);
207 ERR("Out of memory, Unable to alloc cookies array\n");
214 CookieInternal
*new_cookies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cookies
,
215 sizeof(CookieInternal
) * (array_size
* 2));
218 ERR("Out of memory, Unable to realloc cookies array\n");
221 cookies
= new_cookies
;
226 cookies
[i
].id
= i
+ 1; /* a return of 0 is used for failure */
227 cookies
[i
].magic
= magic
;
228 cookies
[i
].data
= data
;
233 return cookies
[i
].id
;
236 DWORD
get_Cookie_magic(DWORD id
)
240 if (index
>= id_last
)
243 if (cookies
[index
].id
== 0)
246 return cookies
[index
].magic
;
249 LPVOID
get_Cookie_data(DWORD id
)
253 if (index
>= id_last
)
256 if (cookies
[index
].id
== 0)
259 return cookies
[index
].data
;
262 LPVOID
remove_Cookie(DWORD id
)
266 if (index
>= id_last
)
269 if (cookies
[index
].id
== 0)
272 cookies
[index
].id
= 0;
273 return cookies
[index
].data
;
276 DWORD
enumerate_Cookie(DWORD magic
, DWORD
*index
)
279 for (i
= *index
; i
< id_last
; i
++)
280 if (cookies
[i
].id
!= 0 && cookies
[i
].magic
== magic
)
283 return cookies
[i
].id
;
288 /*****************************************************************************
289 * Active Text Service Management
290 *****************************************************************************/
291 static HRESULT
activate_given_ts(ActivatedTextService
*actsvr
, ITfThreadMgr
* tm
)
295 /* Already Active? */
296 if (actsvr
->pITfTextInputProcessor
)
299 hr
= CoCreateInstance (&actsvr
->LanguageProfile
.clsid
, NULL
, CLSCTX_INPROC_SERVER
,
300 &IID_ITfTextInputProcessor
, (void**)&actsvr
->pITfTextInputProcessor
);
301 if (FAILED(hr
)) return hr
;
303 hr
= ITfTextInputProcessor_Activate(actsvr
->pITfTextInputProcessor
, tm
, actsvr
->tid
);
306 ITfTextInputProcessor_Release(actsvr
->pITfTextInputProcessor
);
307 actsvr
->pITfTextInputProcessor
= NULL
;
311 actsvr
->pITfThreadMgr
= tm
;
312 ITfThreadMgr_AddRef(tm
);
316 static HRESULT
deactivate_given_ts(ActivatedTextService
*actsvr
)
320 if (actsvr
->pITfTextInputProcessor
)
322 hr
= ITfTextInputProcessor_Deactivate(actsvr
->pITfTextInputProcessor
);
323 ITfTextInputProcessor_Release(actsvr
->pITfTextInputProcessor
);
324 ITfThreadMgr_Release(actsvr
->pITfThreadMgr
);
325 actsvr
->pITfTextInputProcessor
= NULL
;
326 actsvr
->pITfThreadMgr
= NULL
;
332 static void deactivate_remove_conflicting_ts(REFCLSID catid
)
334 AtsEntry
*ats
, *cursor2
;
336 LIST_FOR_EACH_ENTRY_SAFE(ats
, cursor2
, &AtsList
, AtsEntry
, entry
)
338 if (IsEqualCLSID(catid
,&ats
->ats
->LanguageProfile
.catid
))
340 deactivate_given_ts(ats
->ats
);
341 list_remove(&ats
->entry
);
342 HeapFree(GetProcessHeap(),0,ats
->ats
);
343 HeapFree(GetProcessHeap(),0,ats
);
344 /* we are guarenteeing there is only 1 */
350 HRESULT
add_active_textservice(TF_LANGUAGEPROFILE
*lp
)
352 ActivatedTextService
*actsvr
;
353 ITfCategoryMgr
*catmgr
;
355 ITfThreadMgr
*tm
= (ITfThreadMgr
*)TlsGetValue(tlsIndex
);
356 ITfClientId
*clientid
;
358 if (!tm
) return E_UNEXPECTED
;
360 actsvr
= HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService
));
361 if (!actsvr
) return E_OUTOFMEMORY
;
363 ITfThreadMgr_QueryInterface(tm
,&IID_ITfClientId
,(LPVOID
)&clientid
);
364 ITfClientId_GetClientId(clientid
, &lp
->clsid
, &actsvr
->tid
);
365 ITfClientId_Release(clientid
);
369 HeapFree(GetProcessHeap(),0,actsvr
);
370 return E_OUTOFMEMORY
;
373 actsvr
->pITfTextInputProcessor
= NULL
;
374 actsvr
->LanguageProfile
= *lp
;
375 actsvr
->LanguageProfile
.fActive
= TRUE
;
376 actsvr
->pITfKeyEventSink
= NULL
;
378 /* get TIP category */
379 if (SUCCEEDED(CategoryMgr_Constructor(NULL
,(IUnknown
**)&catmgr
)))
381 static const GUID
*list
[3] = {&GUID_TFCAT_TIP_SPEECH
, &GUID_TFCAT_TIP_KEYBOARD
, &GUID_TFCAT_TIP_HANDWRITING
};
383 ITfCategoryMgr_FindClosestCategory(catmgr
,
384 &actsvr
->LanguageProfile
.clsid
, &actsvr
->LanguageProfile
.catid
,
387 ITfCategoryMgr_Release(catmgr
);
391 ERR("CategoryMgr construction failed\n");
392 actsvr
->LanguageProfile
.catid
= GUID_NULL
;
395 if (!IsEqualGUID(&actsvr
->LanguageProfile
.catid
,&GUID_NULL
))
396 deactivate_remove_conflicting_ts(&actsvr
->LanguageProfile
.catid
);
399 activate_given_ts(actsvr
, tm
);
401 entry
= HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry
));
405 HeapFree(GetProcessHeap(),0,actsvr
);
406 return E_OUTOFMEMORY
;
410 list_add_head(&AtsList
, &entry
->entry
);
415 BOOL
get_active_textservice(REFCLSID rclsid
, TF_LANGUAGEPROFILE
*profile
)
419 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
421 if (IsEqualCLSID(rclsid
,&ats
->ats
->LanguageProfile
.clsid
))
424 *profile
= ats
->ats
->LanguageProfile
;
431 HRESULT
activate_textservices(ITfThreadMgr
*tm
)
440 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
442 hr
= activate_given_ts(ats
->ats
, tm
);
444 FIXME("Failed to activate text service\n");
449 HRESULT
deactivate_textservices(void)
457 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
458 deactivate_given_ts(ats
->ats
);
463 CLSID
get_textservice_clsid(TfClientId tid
)
467 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
468 if (ats
->ats
->tid
== tid
)
469 return ats
->ats
->LanguageProfile
.clsid
;
473 HRESULT
get_textservice_sink(TfClientId tid
, REFCLSID iid
, IUnknown
**sink
)
477 if (!IsEqualCLSID(iid
,&IID_ITfKeyEventSink
))
478 return E_NOINTERFACE
;
480 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
481 if (ats
->ats
->tid
== tid
)
483 *sink
= (IUnknown
*)ats
->ats
->pITfKeyEventSink
;
490 HRESULT
set_textservice_sink(TfClientId tid
, REFCLSID iid
, IUnknown
* sink
)
494 if (!IsEqualCLSID(iid
,&IID_ITfKeyEventSink
))
495 return E_NOINTERFACE
;
497 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
498 if (ats
->ats
->tid
== tid
)
500 ats
->ats
->pITfKeyEventSink
= (ITfKeyEventSink
*)sink
;
507 /*************************************************************************
510 BOOL WINAPI
DllMain(HINSTANCE hinst
, DWORD fdwReason
, LPVOID fImpLoad
)
512 TRACE("%p 0x%x %p\n", hinst
, fdwReason
, fImpLoad
);
515 case DLL_WINE_PREATTACH
:
516 return FALSE
; /* prefer native version */
517 case DLL_PROCESS_ATTACH
:
518 MSCTF_hinstance
= hinst
;
519 tlsIndex
= TlsAlloc();
521 case DLL_PROCESS_DETACH
:
528 /*************************************************************************
529 * DllCanUnloadNow (MSCTF.@)
531 HRESULT WINAPI
DllCanUnloadNow(void)
533 return MSCTF_refCount
? S_FALSE
: S_OK
;
536 /***********************************************************************
537 * DllGetClassObject (MSCTF.@)
539 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID iid
, LPVOID
*ppvOut
)
544 if (!IsEqualIID(iid
, &IID_IUnknown
) && !IsEqualIID(iid
, &IID_IClassFactory
))
545 return E_NOINTERFACE
;
547 for (i
= 0; ClassesTable
[i
].clsid
!= NULL
; i
++)
548 if (IsEqualCLSID(ClassesTable
[i
].clsid
, clsid
)) {
549 return ClassFactory_Constructor(ClassesTable
[i
].ctor
, ppvOut
);
551 FIXME("CLSID %s not supported\n", debugstr_guid(clsid
));
552 return CLASS_E_CLASSNOTAVAILABLE
;
555 /***********************************************************************
556 * TF_CreateThreadMgr (MSCTF.@)
558 HRESULT WINAPI
TF_CreateThreadMgr(ITfThreadMgr
**pptim
)
561 return ThreadMgr_Constructor(NULL
,(IUnknown
**)pptim
);
564 /***********************************************************************
565 * TF_GetThreadMgr (MSCTF.@)
567 HRESULT WINAPI
TF_GetThreadMgr(ITfThreadMgr
**pptim
)
570 *pptim
= TlsGetValue(tlsIndex
);
573 ITfThreadMgr_AddRef(*pptim
);
578 /***********************************************************************
579 * SetInputScope(MSCTF.@)
581 HRESULT WINAPI
SetInputScope(HWND hwnd
, INT inputscope
)
583 FIXME("STUB: %p %i\n",hwnd
,inputscope
);
587 /***********************************************************************
588 * SetInputScopes(MSCTF.@)
590 HRESULT WINAPI
SetInputScopes(HWND hwnd
, const INT
*pInputScopes
,
591 UINT cInputScopes
, WCHAR
**ppszPhraseList
,
592 UINT cPhrases
, WCHAR
*pszRegExp
, WCHAR
*pszSRGS
)
595 FIXME("STUB: %p ... %s %s\n",hwnd
, debugstr_w(pszRegExp
), debugstr_w(pszSRGS
));
596 for (i
= 0; i
< cInputScopes
; i
++)
597 TRACE("\tScope[%i] = %i\n",i
,pInputScopes
[i
]);
598 for (i
= 0; i
< cPhrases
; i
++)
599 TRACE("\tPhrase[%i] = %s\n",i
,debugstr_w(ppszPhraseList
[i
]));
604 /***********************************************************************
605 * TF_CreateInputProcessorProfiles(MSCTF.@)
607 HRESULT WINAPI
TF_CreateInputProcessorProfiles(
608 ITfInputProcessorProfiles
**ppipr
)
610 return InputProcessorProfiles_Constructor(NULL
,(IUnknown
**)ppipr
);