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;
78 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 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
86 {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
87 {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
88 {&CLSID_TF_CategoryMgr, CategoryMgr_Constructor},
92 typedef struct tagClassFactory
94 const IClassFactoryVtbl *vtbl;
99 static void ClassFactory_Destructor(ClassFactory *This)
101 TRACE("Destroying class factory %p\n", This);
102 HeapFree(GetProcessHeap(),0,This);
106 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
109 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
110 IClassFactory_AddRef(iface);
115 WARN("Unknown interface %s\n", debugstr_guid(riid));
116 return E_NOINTERFACE;
119 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
121 ClassFactory *This = (ClassFactory *)iface;
122 return InterlockedIncrement(&This->ref);
125 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
127 ClassFactory *This = (ClassFactory *)iface;
128 ULONG ret = InterlockedDecrement(&This->ref);
131 ClassFactory_Destructor(This);
135 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
137 ClassFactory *This = (ClassFactory *)iface;
141 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
142 ret = This->ctor(punkOuter, &obj);
145 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
146 IUnknown_Release(obj);
150 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
152 ClassFactory *This = (ClassFactory *)iface;
154 TRACE("(%p)->(%x)\n", This, fLock);
157 InterlockedIncrement(&MSCTF_refCount);
159 InterlockedDecrement(&MSCTF_refCount);
164 static const IClassFactoryVtbl ClassFactoryVtbl = {
166 ClassFactory_QueryInterface,
168 ClassFactory_Release,
171 ClassFactory_CreateInstance,
172 ClassFactory_LockServer
175 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
177 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
178 This->vtbl = &ClassFactoryVtbl;
182 TRACE("Created class factory %p\n", This);
187 /*************************************************************************
188 * DWORD Cookie Management
190 DWORD generate_Cookie(DWORD magic, LPVOID data)
194 /* try to reuse IDs if possible */
195 for (i = 0; i < id_last; i++)
196 if (cookies[i].id == 0) break;
202 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
205 ERR("Out of memory, Unable to alloc cookies array\n");
212 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
213 sizeof(CookieInternal) * (array_size * 2));
216 ERR("Out of memory, Unable to realloc cookies array\n");
219 cookies = new_cookies;
224 cookies[i].id = i + 1; /* a return of 0 is used for failure */
225 cookies[i].magic = magic;
226 cookies[i].data = data;
231 return cookies[i].id;
234 DWORD get_Cookie_magic(DWORD id)
238 if (index >= id_last)
241 if (cookies[index].id == 0)
244 return cookies[index].magic;
247 LPVOID get_Cookie_data(DWORD id)
251 if (index >= id_last)
254 if (cookies[index].id == 0)
257 return cookies[index].data;
260 LPVOID remove_Cookie(DWORD id)
264 if (index >= id_last)
267 if (cookies[index].id == 0)
270 cookies[index].id = 0;
271 return cookies[index].data;
274 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
277 for (i = *index; i < id_last; i++)
278 if (cookies[i].id != 0 && cookies[i].magic == magic)
281 return cookies[i].id;
286 /*****************************************************************************
287 * Active Text Service Management
288 *****************************************************************************/
289 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgr* tm)
293 /* Already Active? */
294 if (actsvr->pITfTextInputProcessor)
297 hr = CoCreateInstance (&actsvr->LanguageProfile.clsid, NULL, CLSCTX_INPROC_SERVER,
298 &IID_ITfTextInputProcessor, (void**)&actsvr->pITfTextInputProcessor);
299 if (FAILED(hr)) return hr;
301 hr = ITfTextInputProcessor_Activate(actsvr->pITfTextInputProcessor, tm, actsvr->tid);
304 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
305 actsvr->pITfTextInputProcessor = NULL;
309 actsvr->pITfThreadMgr = tm;
310 ITfThreadMgr_AddRef(tm);
314 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
318 if (actsvr->pITfTextInputProcessor)
320 hr = ITfTextInputProcessor_Deactivate(actsvr->pITfTextInputProcessor);
321 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
322 ITfThreadMgr_Release(actsvr->pITfThreadMgr);
323 actsvr->pITfTextInputProcessor = NULL;
324 actsvr->pITfThreadMgr = NULL;
330 static void deactivate_remove_conflicting_ts(REFCLSID catid)
332 AtsEntry *ats, *cursor2;
334 LIST_FOR_EACH_ENTRY_SAFE(ats, cursor2, &AtsList, AtsEntry, entry)
336 if (IsEqualCLSID(catid,&ats->ats->LanguageProfile.catid))
338 deactivate_given_ts(ats->ats);
339 list_remove(&ats->entry);
340 HeapFree(GetProcessHeap(),0,ats->ats);
341 HeapFree(GetProcessHeap(),0,ats);
342 /* we are guarenteeing there is only 1 */
348 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
350 ActivatedTextService *actsvr;
351 ITfCategoryMgr *catmgr;
353 ITfThreadMgr *tm = (ITfThreadMgr*)TlsGetValue(tlsIndex);
354 ITfClientId *clientid;
356 if (!tm) return E_UNEXPECTED;
358 actsvr = HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService));
359 if (!actsvr) return E_OUTOFMEMORY;
361 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
365 HeapFree(GetProcessHeap(),0,actsvr);
366 return E_OUTOFMEMORY;
369 ITfThreadMgr_QueryInterface(tm,&IID_ITfClientId,(LPVOID)&clientid);
370 ITfClientId_GetClientId(clientid, &lp->clsid, &actsvr->tid);
371 ITfClientId_Release(clientid);
375 HeapFree(GetProcessHeap(),0,actsvr);
376 return E_OUTOFMEMORY;
379 actsvr->pITfTextInputProcessor = NULL;
380 actsvr->LanguageProfile = *lp;
381 actsvr->LanguageProfile.fActive = TRUE;
382 actsvr->pITfKeyEventSink = NULL;
384 /* get TIP category */
385 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
387 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
389 ITfCategoryMgr_FindClosestCategory(catmgr,
390 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
393 ITfCategoryMgr_Release(catmgr);
397 ERR("CategoryMgr construction failed\n");
398 actsvr->LanguageProfile.catid = GUID_NULL;
401 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
402 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
405 activate_given_ts(actsvr, tm);
408 list_add_head(&AtsList, &entry->entry);
413 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
417 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
419 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
422 *profile = ats->ats->LanguageProfile;
429 HRESULT activate_textservices(ITfThreadMgr *tm)
438 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
440 hr = activate_given_ts(ats->ats, tm);
442 FIXME("Failed to activate text service\n");
447 HRESULT deactivate_textservices(void)
455 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
456 deactivate_given_ts(ats->ats);
461 CLSID get_textservice_clsid(TfClientId tid)
465 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
466 if (ats->ats->tid == tid)
467 return ats->ats->LanguageProfile.clsid;
471 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
475 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
476 return E_NOINTERFACE;
478 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
479 if (ats->ats->tid == tid)
481 *sink = (IUnknown*)ats->ats->pITfKeyEventSink;
488 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
492 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
493 return E_NOINTERFACE;
495 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
496 if (ats->ats->tid == tid)
498 ats->ats->pITfKeyEventSink = (ITfKeyEventSink*)sink;
505 /*************************************************************************
508 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
510 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
513 case DLL_WINE_PREATTACH:
514 return FALSE; /* prefer native version */
515 case DLL_PROCESS_ATTACH:
516 MSCTF_hinstance = hinst;
517 tlsIndex = TlsAlloc();
519 case DLL_PROCESS_DETACH:
526 /*************************************************************************
527 * DllCanUnloadNow (MSCTF.@)
529 HRESULT WINAPI DllCanUnloadNow(void)
531 return MSCTF_refCount ? S_FALSE : S_OK;
534 /***********************************************************************
535 * DllGetClassObject (MSCTF.@)
537 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
542 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
543 return E_NOINTERFACE;
545 for (i = 0; ClassesTable[i].clsid != NULL; i++)
546 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
547 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
549 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
550 return CLASS_E_CLASSNOTAVAILABLE;
553 /***********************************************************************
554 * TF_CreateThreadMgr (MSCTF.@)
556 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
559 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
562 /***********************************************************************
563 * TF_GetThreadMgr (MSCTF.@)
565 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
568 *pptim = TlsGetValue(tlsIndex);
571 ITfThreadMgr_AddRef(*pptim);
576 /***********************************************************************
577 * SetInputScope(MSCTF.@)
579 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
581 FIXME("STUB: %p %i\n",hwnd,inputscope);
585 /***********************************************************************
586 * SetInputScopes(MSCTF.@)
588 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
589 UINT cInputScopes, WCHAR **ppszPhraseList,
590 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
593 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
594 for (i = 0; i < cInputScopes; i++)
595 TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
596 for (i = 0; i < cPhrases; i++)
597 TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));