appwiz.cpl: Use the fancy new icon.
[wine/multimedia.git] / dlls / msctf / msctf.c
blob759fe7ec5798236f3fcfde97c35397cb406d8db3
1 /*
2 * MSCTF Server DLL
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
26 #define COBJMACROS
28 #include "wine/debug.h"
29 #include "wine/list.h"
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include "shlguid.h"
35 #include "comcat.h"
36 #include "msctf.h"
38 #include "msctf_internal.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
42 static LONG MSCTF_refCount;
44 static HINSTANCE MSCTF_hinstance;
46 typedef struct
48 DWORD id;
49 DWORD magic;
50 LPVOID data;
51 } CookieInternal;
53 typedef struct {
54 TF_LANGUAGEPROFILE LanguageProfile;
55 ITfTextInputProcessor *pITfTextInputProcessor;
56 ITfThreadMgr *pITfThreadMgr;
57 ITfKeyEventSink *pITfKeyEventSink;
58 TfClientId tid;
59 } ActivatedTextService;
61 typedef struct
63 struct list entry;
64 ActivatedTextService *ats;
65 } AtsEntry;
67 static CookieInternal *cookies;
68 static UINT id_last;
69 static UINT array_size;
71 static struct list AtsList = LIST_INIT(AtsList);
72 static UINT activated = 0;
74 DWORD tlsIndex = 0;
75 TfClientId processId = 0;
76 ITfCompartmentMgr *globalCompartmentMgr = NULL;
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};
79 const WCHAR szwSystemCTFKey[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F',0};
81 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
83 static const struct {
84 REFCLSID clsid;
85 LPFNCONSTRUCTOR ctor;
86 } ClassesTable[] = {
87 {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
88 {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
89 {&CLSID_TF_CategoryMgr, CategoryMgr_Constructor},
90 {&CLSID_TF_LangBarMgr, LangBarMgr_Constructor},
91 {&CLSID_TF_DisplayAttributeMgr, DisplayAttributeMgr_Constructor},
92 {NULL, NULL}
95 typedef struct tagClassFactory
97 const IClassFactoryVtbl *vtbl;
98 LONG ref;
99 LPFNCONSTRUCTOR ctor;
100 } ClassFactory;
102 static void ClassFactory_Destructor(ClassFactory *This)
104 TRACE("Destroying class factory %p\n", This);
105 HeapFree(GetProcessHeap(),0,This);
106 MSCTF_refCount--;
109 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
111 *ppvOut = NULL;
112 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
113 IClassFactory_AddRef(iface);
114 *ppvOut = iface;
115 return S_OK;
118 WARN("Unknown interface %s\n", debugstr_guid(riid));
119 return E_NOINTERFACE;
122 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
124 ClassFactory *This = (ClassFactory *)iface;
125 return InterlockedIncrement(&This->ref);
128 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
130 ClassFactory *This = (ClassFactory *)iface;
131 ULONG ret = InterlockedDecrement(&This->ref);
133 if (ret == 0)
134 ClassFactory_Destructor(This);
135 return ret;
138 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
140 ClassFactory *This = (ClassFactory *)iface;
141 HRESULT ret;
142 IUnknown *obj;
144 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
145 ret = This->ctor(punkOuter, &obj);
146 if (FAILED(ret))
147 return ret;
148 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
149 IUnknown_Release(obj);
150 return ret;
153 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
155 ClassFactory *This = (ClassFactory *)iface;
157 TRACE("(%p)->(%x)\n", This, fLock);
159 if(fLock)
160 InterlockedIncrement(&MSCTF_refCount);
161 else
162 InterlockedDecrement(&MSCTF_refCount);
164 return S_OK;
167 static const IClassFactoryVtbl ClassFactoryVtbl = {
168 /* IUnknown */
169 ClassFactory_QueryInterface,
170 ClassFactory_AddRef,
171 ClassFactory_Release,
173 /* IClassFactory*/
174 ClassFactory_CreateInstance,
175 ClassFactory_LockServer
178 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
180 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
181 This->vtbl = &ClassFactoryVtbl;
182 This->ref = 1;
183 This->ctor = ctor;
184 *ppvOut = This;
185 TRACE("Created class factory %p\n", This);
186 MSCTF_refCount++;
187 return S_OK;
190 /*************************************************************************
191 * DWORD Cookie Management
193 DWORD generate_Cookie(DWORD magic, LPVOID data)
195 int i;
197 /* try to reuse IDs if possible */
198 for (i = 0; i < id_last; i++)
199 if (cookies[i].id == 0) break;
201 if (i == array_size)
203 if (!array_size)
205 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
206 if (!cookies)
208 ERR("Out of memory, Unable to alloc cookies array\n");
209 return 0;
211 array_size = 10;
213 else
215 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
216 sizeof(CookieInternal) * (array_size * 2));
217 if (!new_cookies)
219 ERR("Out of memory, Unable to realloc cookies array\n");
220 return 0;
222 cookies = new_cookies;
223 array_size *= 2;
227 cookies[i].id = i + 1; /* a return of 0 is used for failure */
228 cookies[i].magic = magic;
229 cookies[i].data = data;
231 if (i == id_last)
232 id_last++;
234 return cookies[i].id;
237 DWORD get_Cookie_magic(DWORD id)
239 UINT index = id - 1;
241 if (index >= id_last)
242 return 0;
244 if (cookies[index].id == 0)
245 return 0;
247 return cookies[index].magic;
250 LPVOID get_Cookie_data(DWORD id)
252 UINT index = id - 1;
254 if (index >= id_last)
255 return NULL;
257 if (cookies[index].id == 0)
258 return NULL;
260 return cookies[index].data;
263 LPVOID remove_Cookie(DWORD id)
265 UINT index = id - 1;
267 if (index >= id_last)
268 return NULL;
270 if (cookies[index].id == 0)
271 return NULL;
273 cookies[index].id = 0;
274 return cookies[index].data;
277 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
279 int i;
280 for (i = *index; i < id_last; i++)
281 if (cookies[i].id != 0 && cookies[i].magic == magic)
283 *index = (i+1);
284 return cookies[i].id;
286 return 0x0;
289 /*****************************************************************************
290 * Active Text Service Management
291 *****************************************************************************/
292 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgr* tm)
294 HRESULT hr;
296 /* Already Active? */
297 if (actsvr->pITfTextInputProcessor)
298 return S_OK;
300 hr = CoCreateInstance (&actsvr->LanguageProfile.clsid, NULL, CLSCTX_INPROC_SERVER,
301 &IID_ITfTextInputProcessor, (void**)&actsvr->pITfTextInputProcessor);
302 if (FAILED(hr)) return hr;
304 hr = ITfTextInputProcessor_Activate(actsvr->pITfTextInputProcessor, tm, actsvr->tid);
305 if (FAILED(hr))
307 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
308 actsvr->pITfTextInputProcessor = NULL;
309 return hr;
312 actsvr->pITfThreadMgr = tm;
313 ITfThreadMgr_AddRef(tm);
314 return hr;
317 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
319 HRESULT hr = S_OK;
321 if (actsvr->pITfTextInputProcessor)
323 hr = ITfTextInputProcessor_Deactivate(actsvr->pITfTextInputProcessor);
324 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
325 ITfThreadMgr_Release(actsvr->pITfThreadMgr);
326 actsvr->pITfTextInputProcessor = NULL;
327 actsvr->pITfThreadMgr = NULL;
330 return hr;
333 static void deactivate_remove_conflicting_ts(REFCLSID catid)
335 AtsEntry *ats, *cursor2;
337 LIST_FOR_EACH_ENTRY_SAFE(ats, cursor2, &AtsList, AtsEntry, entry)
339 if (IsEqualCLSID(catid,&ats->ats->LanguageProfile.catid))
341 deactivate_given_ts(ats->ats);
342 list_remove(&ats->entry);
343 HeapFree(GetProcessHeap(),0,ats->ats);
344 HeapFree(GetProcessHeap(),0,ats);
345 /* we are guarenteeing there is only 1 */
346 break;
351 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
353 ActivatedTextService *actsvr;
354 ITfCategoryMgr *catmgr;
355 AtsEntry *entry;
356 ITfThreadMgr *tm = TlsGetValue(tlsIndex);
357 ITfClientId *clientid;
359 if (!tm) return E_UNEXPECTED;
361 actsvr = HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService));
362 if (!actsvr) return E_OUTOFMEMORY;
364 ITfThreadMgr_QueryInterface(tm,&IID_ITfClientId,(LPVOID)&clientid);
365 ITfClientId_GetClientId(clientid, &lp->clsid, &actsvr->tid);
366 ITfClientId_Release(clientid);
368 if (!actsvr->tid)
370 HeapFree(GetProcessHeap(),0,actsvr);
371 return E_OUTOFMEMORY;
374 actsvr->pITfTextInputProcessor = NULL;
375 actsvr->LanguageProfile = *lp;
376 actsvr->LanguageProfile.fActive = TRUE;
377 actsvr->pITfKeyEventSink = NULL;
379 /* get TIP category */
380 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
382 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
384 ITfCategoryMgr_FindClosestCategory(catmgr,
385 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
386 list, 3);
388 ITfCategoryMgr_Release(catmgr);
390 else
392 ERR("CategoryMgr construction failed\n");
393 actsvr->LanguageProfile.catid = GUID_NULL;
396 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
397 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
399 if (activated > 0)
400 activate_given_ts(actsvr, tm);
402 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
404 if (!entry)
406 HeapFree(GetProcessHeap(),0,actsvr);
407 return E_OUTOFMEMORY;
410 entry->ats = actsvr;
411 list_add_head(&AtsList, &entry->entry);
413 return S_OK;
416 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
418 AtsEntry *ats;
420 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
422 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
424 if (profile)
425 *profile = ats->ats->LanguageProfile;
426 return TRUE;
429 return FALSE;
432 HRESULT activate_textservices(ITfThreadMgr *tm)
434 HRESULT hr = S_OK;
435 AtsEntry *ats;
437 activated ++;
438 if (activated > 1)
439 return S_OK;
441 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
443 hr = activate_given_ts(ats->ats, tm);
444 if (FAILED(hr))
445 FIXME("Failed to activate text service\n");
447 return hr;
450 HRESULT deactivate_textservices(void)
452 AtsEntry *ats;
454 if (activated > 0)
455 activated --;
457 if (activated == 0)
458 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
459 deactivate_given_ts(ats->ats);
461 return S_OK;
464 CLSID get_textservice_clsid(TfClientId tid)
466 AtsEntry *ats;
468 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
469 if (ats->ats->tid == tid)
470 return ats->ats->LanguageProfile.clsid;
471 return GUID_NULL;
474 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
476 AtsEntry *ats;
478 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
479 return E_NOINTERFACE;
481 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
482 if (ats->ats->tid == tid)
484 *sink = (IUnknown*)ats->ats->pITfKeyEventSink;
485 return S_OK;
488 return E_FAIL;
491 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
493 AtsEntry *ats;
495 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
496 return E_NOINTERFACE;
498 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
499 if (ats->ats->tid == tid)
501 ats->ats->pITfKeyEventSink = (ITfKeyEventSink*)sink;
502 return S_OK;
505 return E_FAIL;
508 /*************************************************************************
509 * MSCTF DllMain
511 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
513 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
514 switch (fdwReason)
516 case DLL_WINE_PREATTACH:
517 return FALSE; /* prefer native version */
518 case DLL_PROCESS_ATTACH:
519 MSCTF_hinstance = hinst;
520 tlsIndex = TlsAlloc();
521 break;
522 case DLL_PROCESS_DETACH:
523 TlsFree(tlsIndex);
524 break;
526 return TRUE;
529 /*************************************************************************
530 * DllCanUnloadNow (MSCTF.@)
532 HRESULT WINAPI DllCanUnloadNow(void)
534 return MSCTF_refCount ? S_FALSE : S_OK;
537 /***********************************************************************
538 * DllGetClassObject (MSCTF.@)
540 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
542 int i;
544 *ppvOut = NULL;
545 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
546 return E_NOINTERFACE;
548 for (i = 0; ClassesTable[i].clsid != NULL; i++)
549 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
550 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
552 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
553 return CLASS_E_CLASSNOTAVAILABLE;
556 /***********************************************************************
557 * TF_CreateThreadMgr (MSCTF.@)
559 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
561 TRACE("\n");
562 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
565 /***********************************************************************
566 * TF_GetThreadMgr (MSCTF.@)
568 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
570 TRACE("\n");
571 *pptim = TlsGetValue(tlsIndex);
573 if (*pptim)
574 ITfThreadMgr_AddRef(*pptim);
576 return S_OK;
579 /***********************************************************************
580 * SetInputScope(MSCTF.@)
582 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
584 FIXME("STUB: %p %i\n",hwnd,inputscope);
585 return S_OK;
588 /***********************************************************************
589 * SetInputScopes(MSCTF.@)
591 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
592 UINT cInputScopes, WCHAR **ppszPhraseList,
593 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
595 int i;
596 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
597 for (i = 0; i < cInputScopes; i++)
598 TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
599 for (i = 0; i < cPhrases; i++)
600 TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));
602 return S_OK;
605 /***********************************************************************
606 * TF_CreateInputProcessorProfiles(MSCTF.@)
608 HRESULT WINAPI TF_CreateInputProcessorProfiles(
609 ITfInputProcessorProfiles **ppipr)
611 return InputProcessorProfiles_Constructor(NULL,(IUnknown**)ppipr);
614 /***********************************************************************
615 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
617 HRESULT WINAPI TF_InvalidAssemblyListCacheIfExist(void)
619 FIXME("Stub\n");
620 return S_OK;
623 /***********************************************************************
624 * TF_CreateLangBarMgr (MSCTF.@)
626 HRESULT WINAPI TF_CreateLangBarMgr(ITfLangBarMgr **pppbm)
628 TRACE("\n");
629 return LangBarMgr_Constructor(NULL,(IUnknown**)pppbm);