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},
91 {&CLSID_TF_LangBarMgr, LangBarMgr_Constructor},
92 {&CLSID_TF_DisplayAttributeMgr, DisplayAttributeMgr_Constructor},
96 typedef struct tagClassFactory
98 IClassFactory IClassFactory_iface;
100 LPFNCONSTRUCTOR ctor;
103 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
105 return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
108 static void ClassFactory_Destructor(ClassFactory *This)
110 TRACE("Destroying class factory %p\n", This);
111 HeapFree(GetProcessHeap(),0,This);
115 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
118 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
119 IClassFactory_AddRef(iface);
124 WARN("Unknown interface %s\n", debugstr_guid(riid));
125 return E_NOINTERFACE;
128 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
130 ClassFactory *This = impl_from_IClassFactory(iface);
131 return InterlockedIncrement(&This->ref);
134 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
136 ClassFactory *This = impl_from_IClassFactory(iface);
137 ULONG ret = InterlockedDecrement(&This->ref);
140 ClassFactory_Destructor(This);
144 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
146 ClassFactory *This = impl_from_IClassFactory(iface);
150 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
151 ret = This->ctor(punkOuter, &obj);
154 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
155 IUnknown_Release(obj);
159 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
161 ClassFactory *This = impl_from_IClassFactory(iface);
163 TRACE("(%p)->(%x)\n", This, fLock);
166 InterlockedIncrement(&MSCTF_refCount);
168 InterlockedDecrement(&MSCTF_refCount);
173 static const IClassFactoryVtbl ClassFactoryVtbl = {
175 ClassFactory_QueryInterface,
177 ClassFactory_Release,
180 ClassFactory_CreateInstance,
181 ClassFactory_LockServer
184 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
186 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
187 This->IClassFactory_iface.lpVtbl = &ClassFactoryVtbl;
191 TRACE("Created class factory %p\n", This);
196 /*************************************************************************
197 * DWORD Cookie Management
199 DWORD generate_Cookie(DWORD magic, LPVOID data)
203 /* try to reuse IDs if possible */
204 for (i = 0; i < id_last; i++)
205 if (cookies[i].id == 0) break;
211 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
214 ERR("Out of memory, Unable to alloc cookies array\n");
221 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
222 sizeof(CookieInternal) * (array_size * 2));
225 ERR("Out of memory, Unable to realloc cookies array\n");
228 cookies = new_cookies;
233 cookies[i].id = i + 1; /* a return of 0 is used for failure */
234 cookies[i].magic = magic;
235 cookies[i].data = data;
240 return cookies[i].id;
243 DWORD get_Cookie_magic(DWORD id)
247 if (index >= id_last)
250 if (cookies[index].id == 0)
253 return cookies[index].magic;
256 LPVOID get_Cookie_data(DWORD id)
260 if (index >= id_last)
263 if (cookies[index].id == 0)
266 return cookies[index].data;
269 LPVOID remove_Cookie(DWORD id)
273 if (index >= id_last)
276 if (cookies[index].id == 0)
279 cookies[index].id = 0;
280 return cookies[index].data;
283 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
286 for (i = *index; i < id_last; i++)
287 if (cookies[i].id != 0 && cookies[i].magic == magic)
290 return cookies[i].id;
295 /*****************************************************************************
296 * Active Text Service Management
297 *****************************************************************************/
298 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgr* tm)
302 /* Already Active? */
303 if (actsvr->pITfTextInputProcessor)
306 hr = CoCreateInstance (&actsvr->LanguageProfile.clsid, NULL, CLSCTX_INPROC_SERVER,
307 &IID_ITfTextInputProcessor, (void**)&actsvr->pITfTextInputProcessor);
308 if (FAILED(hr)) return hr;
310 hr = ITfTextInputProcessor_Activate(actsvr->pITfTextInputProcessor, tm, actsvr->tid);
313 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
314 actsvr->pITfTextInputProcessor = NULL;
318 actsvr->pITfThreadMgr = tm;
319 ITfThreadMgr_AddRef(tm);
323 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
327 if (actsvr->pITfTextInputProcessor)
329 hr = ITfTextInputProcessor_Deactivate(actsvr->pITfTextInputProcessor);
330 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
331 ITfThreadMgr_Release(actsvr->pITfThreadMgr);
332 actsvr->pITfTextInputProcessor = NULL;
333 actsvr->pITfThreadMgr = NULL;
339 static void deactivate_remove_conflicting_ts(REFCLSID catid)
341 AtsEntry *ats, *cursor2;
343 LIST_FOR_EACH_ENTRY_SAFE(ats, cursor2, &AtsList, AtsEntry, entry)
345 if (IsEqualCLSID(catid,&ats->ats->LanguageProfile.catid))
347 deactivate_given_ts(ats->ats);
348 list_remove(&ats->entry);
349 HeapFree(GetProcessHeap(),0,ats->ats);
350 HeapFree(GetProcessHeap(),0,ats);
351 /* we are guarenteeing there is only 1 */
357 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
359 ActivatedTextService *actsvr;
360 ITfCategoryMgr *catmgr;
362 ITfThreadMgr *tm = TlsGetValue(tlsIndex);
363 ITfClientId *clientid;
365 if (!tm) return E_UNEXPECTED;
367 actsvr = HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService));
368 if (!actsvr) return E_OUTOFMEMORY;
370 ITfThreadMgr_QueryInterface(tm,&IID_ITfClientId,(LPVOID)&clientid);
371 ITfClientId_GetClientId(clientid, &lp->clsid, &actsvr->tid);
372 ITfClientId_Release(clientid);
376 HeapFree(GetProcessHeap(),0,actsvr);
377 return E_OUTOFMEMORY;
380 actsvr->pITfTextInputProcessor = NULL;
381 actsvr->LanguageProfile = *lp;
382 actsvr->LanguageProfile.fActive = TRUE;
383 actsvr->pITfKeyEventSink = NULL;
385 /* get TIP category */
386 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
388 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
390 ITfCategoryMgr_FindClosestCategory(catmgr,
391 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
394 ITfCategoryMgr_Release(catmgr);
398 ERR("CategoryMgr construction failed\n");
399 actsvr->LanguageProfile.catid = GUID_NULL;
402 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
403 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
406 activate_given_ts(actsvr, tm);
408 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
412 HeapFree(GetProcessHeap(),0,actsvr);
413 return E_OUTOFMEMORY;
417 list_add_head(&AtsList, &entry->entry);
422 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
426 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
428 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
431 *profile = ats->ats->LanguageProfile;
438 HRESULT activate_textservices(ITfThreadMgr *tm)
447 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
449 hr = activate_given_ts(ats->ats, tm);
451 FIXME("Failed to activate text service\n");
456 HRESULT deactivate_textservices(void)
464 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
465 deactivate_given_ts(ats->ats);
470 CLSID get_textservice_clsid(TfClientId tid)
474 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
475 if (ats->ats->tid == tid)
476 return ats->ats->LanguageProfile.clsid;
480 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
484 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
485 return E_NOINTERFACE;
487 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
488 if (ats->ats->tid == tid)
490 *sink = (IUnknown*)ats->ats->pITfKeyEventSink;
497 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
501 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
502 return E_NOINTERFACE;
504 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
505 if (ats->ats->tid == tid)
507 ats->ats->pITfKeyEventSink = (ITfKeyEventSink*)sink;
514 /*************************************************************************
517 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
519 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
522 case DLL_WINE_PREATTACH:
523 return FALSE; /* prefer native version */
524 case DLL_PROCESS_ATTACH:
525 MSCTF_hinstance = hinst;
526 tlsIndex = TlsAlloc();
528 case DLL_PROCESS_DETACH:
535 /*************************************************************************
536 * DllCanUnloadNow (MSCTF.@)
538 HRESULT WINAPI DllCanUnloadNow(void)
540 return MSCTF_refCount ? S_FALSE : S_OK;
543 /***********************************************************************
544 * DllGetClassObject (MSCTF.@)
546 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
551 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
552 return E_NOINTERFACE;
554 for (i = 0; ClassesTable[i].clsid != NULL; i++)
555 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
556 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
558 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
559 return CLASS_E_CLASSNOTAVAILABLE;
562 /***********************************************************************
563 * DllRegisterServer (MSCTF.@)
565 HRESULT WINAPI DllRegisterServer(void)
567 return __wine_register_resources( MSCTF_hinstance, NULL );
570 /***********************************************************************
571 * DllUnregisterServer (MSCTF.@)
573 HRESULT WINAPI DllUnregisterServer(void)
575 return __wine_unregister_resources( MSCTF_hinstance, NULL );
578 /***********************************************************************
579 * TF_CreateThreadMgr (MSCTF.@)
581 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
584 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
587 /***********************************************************************
588 * TF_GetThreadMgr (MSCTF.@)
590 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
593 *pptim = TlsGetValue(tlsIndex);
596 ITfThreadMgr_AddRef(*pptim);
601 /***********************************************************************
602 * SetInputScope(MSCTF.@)
604 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
606 FIXME("STUB: %p %i\n",hwnd,inputscope);
610 /***********************************************************************
611 * SetInputScopes(MSCTF.@)
613 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
614 UINT cInputScopes, WCHAR **ppszPhraseList,
615 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
618 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
619 for (i = 0; i < cInputScopes; i++)
620 TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
621 for (i = 0; i < cPhrases; i++)
622 TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));
627 /***********************************************************************
628 * TF_CreateInputProcessorProfiles(MSCTF.@)
630 HRESULT WINAPI TF_CreateInputProcessorProfiles(
631 ITfInputProcessorProfiles **ppipr)
633 return InputProcessorProfiles_Constructor(NULL,(IUnknown**)ppipr);
636 /***********************************************************************
637 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
639 HRESULT WINAPI TF_InvalidAssemblyListCacheIfExist(void)
645 /***********************************************************************
646 * TF_CreateLangBarMgr (MSCTF.@)
648 HRESULT WINAPI TF_CreateLangBarMgr(ITfLangBarMgr **pppbm)
651 return LangBarMgr_Constructor(NULL,(IUnknown**)pppbm);