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};
81 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
87 {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
88 {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
89 {&CLSID_TF_CategoryMgr, CategoryMgr_Constructor},
93 typedef struct tagClassFactory
95 const IClassFactoryVtbl *vtbl;
100 static void ClassFactory_Destructor(ClassFactory *This)
102 TRACE("Destroying class factory %p\n", This);
103 HeapFree(GetProcessHeap(),0,This);
107 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
110 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
111 IClassFactory_AddRef(iface);
116 WARN("Unknown interface %s\n", debugstr_guid(riid));
117 return E_NOINTERFACE;
120 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
122 ClassFactory *This = (ClassFactory *)iface;
123 return InterlockedIncrement(&This->ref);
126 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
128 ClassFactory *This = (ClassFactory *)iface;
129 ULONG ret = InterlockedDecrement(&This->ref);
132 ClassFactory_Destructor(This);
136 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
138 ClassFactory *This = (ClassFactory *)iface;
142 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
143 ret = This->ctor(punkOuter, &obj);
146 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
147 IUnknown_Release(obj);
151 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
153 ClassFactory *This = (ClassFactory *)iface;
155 TRACE("(%p)->(%x)\n", This, fLock);
158 InterlockedIncrement(&MSCTF_refCount);
160 InterlockedDecrement(&MSCTF_refCount);
165 static const IClassFactoryVtbl ClassFactoryVtbl = {
167 ClassFactory_QueryInterface,
169 ClassFactory_Release,
172 ClassFactory_CreateInstance,
173 ClassFactory_LockServer
176 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
178 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
179 This->vtbl = &ClassFactoryVtbl;
183 TRACE("Created class factory %p\n", This);
188 /*************************************************************************
189 * DWORD Cookie Management
191 DWORD generate_Cookie(DWORD magic, LPVOID data)
195 /* try to reuse IDs if possible */
196 for (i = 0; i < id_last; i++)
197 if (cookies[i].id == 0) break;
203 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
206 ERR("Out of memory, Unable to alloc cookies array\n");
213 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
214 sizeof(CookieInternal) * (array_size * 2));
217 ERR("Out of memory, Unable to realloc cookies array\n");
220 cookies = new_cookies;
225 cookies[i].id = i + 1; /* a return of 0 is used for failure */
226 cookies[i].magic = magic;
227 cookies[i].data = data;
232 return cookies[i].id;
235 DWORD get_Cookie_magic(DWORD id)
239 if (index >= id_last)
242 if (cookies[index].id == 0)
245 return cookies[index].magic;
248 LPVOID get_Cookie_data(DWORD id)
252 if (index >= id_last)
255 if (cookies[index].id == 0)
258 return cookies[index].data;
261 LPVOID remove_Cookie(DWORD id)
265 if (index >= id_last)
268 if (cookies[index].id == 0)
271 cookies[index].id = 0;
272 return cookies[index].data;
275 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
278 for (i = *index; i < id_last; i++)
279 if (cookies[i].id != 0 && cookies[i].magic == magic)
282 return cookies[i].id;
287 /*****************************************************************************
288 * Active Text Service Management
289 *****************************************************************************/
290 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgr* tm)
294 /* Already Active? */
295 if (actsvr->pITfTextInputProcessor)
298 hr = CoCreateInstance (&actsvr->LanguageProfile.clsid, NULL, CLSCTX_INPROC_SERVER,
299 &IID_ITfTextInputProcessor, (void**)&actsvr->pITfTextInputProcessor);
300 if (FAILED(hr)) return hr;
302 hr = ITfTextInputProcessor_Activate(actsvr->pITfTextInputProcessor, tm, actsvr->tid);
305 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
306 actsvr->pITfTextInputProcessor = NULL;
310 actsvr->pITfThreadMgr = tm;
311 ITfThreadMgr_AddRef(tm);
315 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
319 if (actsvr->pITfTextInputProcessor)
321 hr = ITfTextInputProcessor_Deactivate(actsvr->pITfTextInputProcessor);
322 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
323 ITfThreadMgr_Release(actsvr->pITfThreadMgr);
324 actsvr->pITfTextInputProcessor = NULL;
325 actsvr->pITfThreadMgr = NULL;
331 static void deactivate_remove_conflicting_ts(REFCLSID catid)
333 AtsEntry *ats, *cursor2;
335 LIST_FOR_EACH_ENTRY_SAFE(ats, cursor2, &AtsList, AtsEntry, entry)
337 if (IsEqualCLSID(catid,&ats->ats->LanguageProfile.catid))
339 deactivate_given_ts(ats->ats);
340 list_remove(&ats->entry);
341 HeapFree(GetProcessHeap(),0,ats->ats);
342 HeapFree(GetProcessHeap(),0,ats);
343 /* we are guarenteeing there is only 1 */
349 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
351 ActivatedTextService *actsvr;
352 ITfCategoryMgr *catmgr;
354 ITfThreadMgr *tm = (ITfThreadMgr*)TlsGetValue(tlsIndex);
355 ITfClientId *clientid;
357 if (!tm) return E_UNEXPECTED;
359 actsvr = HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService));
360 if (!actsvr) return E_OUTOFMEMORY;
362 ITfThreadMgr_QueryInterface(tm,&IID_ITfClientId,(LPVOID)&clientid);
363 ITfClientId_GetClientId(clientid, &lp->clsid, &actsvr->tid);
364 ITfClientId_Release(clientid);
368 HeapFree(GetProcessHeap(),0,actsvr);
369 return E_OUTOFMEMORY;
372 actsvr->pITfTextInputProcessor = NULL;
373 actsvr->LanguageProfile = *lp;
374 actsvr->LanguageProfile.fActive = TRUE;
375 actsvr->pITfKeyEventSink = NULL;
377 /* get TIP category */
378 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
380 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
382 ITfCategoryMgr_FindClosestCategory(catmgr,
383 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
386 ITfCategoryMgr_Release(catmgr);
390 ERR("CategoryMgr construction failed\n");
391 actsvr->LanguageProfile.catid = GUID_NULL;
394 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
395 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
398 activate_given_ts(actsvr, tm);
400 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
404 HeapFree(GetProcessHeap(),0,actsvr);
405 return E_OUTOFMEMORY;
409 list_add_head(&AtsList, &entry->entry);
414 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
418 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
420 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
423 *profile = ats->ats->LanguageProfile;
430 HRESULT activate_textservices(ITfThreadMgr *tm)
439 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
441 hr = activate_given_ts(ats->ats, tm);
443 FIXME("Failed to activate text service\n");
448 HRESULT deactivate_textservices(void)
456 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
457 deactivate_given_ts(ats->ats);
462 CLSID get_textservice_clsid(TfClientId tid)
466 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
467 if (ats->ats->tid == tid)
468 return ats->ats->LanguageProfile.clsid;
472 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
476 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
477 return E_NOINTERFACE;
479 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
480 if (ats->ats->tid == tid)
482 *sink = (IUnknown*)ats->ats->pITfKeyEventSink;
489 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
493 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
494 return E_NOINTERFACE;
496 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
497 if (ats->ats->tid == tid)
499 ats->ats->pITfKeyEventSink = (ITfKeyEventSink*)sink;
506 /*************************************************************************
509 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
511 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
514 case DLL_WINE_PREATTACH:
515 return FALSE; /* prefer native version */
516 case DLL_PROCESS_ATTACH:
517 MSCTF_hinstance = hinst;
518 tlsIndex = TlsAlloc();
520 case DLL_PROCESS_DETACH:
527 /*************************************************************************
528 * DllCanUnloadNow (MSCTF.@)
530 HRESULT WINAPI DllCanUnloadNow(void)
532 return MSCTF_refCount ? S_FALSE : S_OK;
535 /***********************************************************************
536 * DllGetClassObject (MSCTF.@)
538 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
543 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
544 return E_NOINTERFACE;
546 for (i = 0; ClassesTable[i].clsid != NULL; i++)
547 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
548 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
550 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
551 return CLASS_E_CLASSNOTAVAILABLE;
554 /***********************************************************************
555 * TF_CreateThreadMgr (MSCTF.@)
557 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
560 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
563 /***********************************************************************
564 * TF_GetThreadMgr (MSCTF.@)
566 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
569 *pptim = TlsGetValue(tlsIndex);
572 ITfThreadMgr_AddRef(*pptim);
577 /***********************************************************************
578 * SetInputScope(MSCTF.@)
580 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
582 FIXME("STUB: %p %i\n",hwnd,inputscope);
586 /***********************************************************************
587 * SetInputScopes(MSCTF.@)
589 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
590 UINT cInputScopes, WCHAR **ppszPhraseList,
591 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
594 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
595 for (i = 0; i < cInputScopes; i++)
596 TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
597 for (i = 0; i < cPhrases; i++)
598 TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));