hidclass.sys: Break the report descriptor into multiple lines.
[wine.git] / dlls / msctf / msctf.c
blob85d2540330bb98650abb820b88ed06f0ab34e46a
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 "rpcproxy.h"
37 #include "msctf.h"
38 #include "inputscope.h"
40 #include "msctf_internal.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
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 IClassFactory IClassFactory_iface;
98 LONG ref;
99 LPFNCONSTRUCTOR ctor;
100 } ClassFactory;
102 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
104 return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
107 static void ClassFactory_Destructor(ClassFactory *This)
109 TRACE("Destroying class factory %p\n", This);
110 HeapFree(GetProcessHeap(),0,This);
113 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
115 *ppvOut = NULL;
116 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
117 IClassFactory_AddRef(iface);
118 *ppvOut = iface;
119 return S_OK;
122 WARN("Unknown interface %s\n", debugstr_guid(riid));
123 return E_NOINTERFACE;
126 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
128 ClassFactory *This = impl_from_IClassFactory(iface);
129 return InterlockedIncrement(&This->ref);
132 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
134 ClassFactory *This = impl_from_IClassFactory(iface);
135 ULONG ret = InterlockedDecrement(&This->ref);
137 if (ret == 0)
138 ClassFactory_Destructor(This);
139 return ret;
142 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
144 ClassFactory *This = impl_from_IClassFactory(iface);
145 HRESULT ret;
146 IUnknown *obj;
148 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
149 ret = This->ctor(punkOuter, &obj);
150 if (FAILED(ret))
151 return ret;
152 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
153 IUnknown_Release(obj);
154 return ret;
157 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
159 ClassFactory *This = impl_from_IClassFactory(iface);
161 TRACE("(%p)->(%x)\n", This, fLock);
163 return S_OK;
166 static const IClassFactoryVtbl ClassFactoryVtbl = {
167 /* IUnknown */
168 ClassFactory_QueryInterface,
169 ClassFactory_AddRef,
170 ClassFactory_Release,
172 /* IClassFactory*/
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->IClassFactory_iface.lpVtbl = &ClassFactoryVtbl;
181 This->ref = 1;
182 This->ctor = ctor;
183 *ppvOut = This;
184 TRACE("Created class factory %p\n", This);
185 return S_OK;
188 /*************************************************************************
189 * DWORD Cookie Management
191 DWORD generate_Cookie(DWORD magic, LPVOID data)
193 UINT i;
195 /* try to reuse IDs if possible */
196 for (i = 0; i < id_last; i++)
197 if (cookies[i].id == 0) break;
199 if (i == array_size)
201 if (!array_size)
203 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
204 if (!cookies)
206 ERR("Out of memory, Unable to alloc cookies array\n");
207 return 0;
209 array_size = 10;
211 else
213 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
214 sizeof(CookieInternal) * (array_size * 2));
215 if (!new_cookies)
217 ERR("Out of memory, Unable to realloc cookies array\n");
218 return 0;
220 cookies = new_cookies;
221 array_size *= 2;
225 cookies[i].id = i + 1; /* a return of 0 is used for failure */
226 cookies[i].magic = magic;
227 cookies[i].data = data;
229 if (i == id_last)
230 id_last++;
232 return cookies[i].id;
235 DWORD get_Cookie_magic(DWORD id)
237 UINT index = id - 1;
239 if (index >= id_last)
240 return 0;
242 if (cookies[index].id == 0)
243 return 0;
245 return cookies[index].magic;
248 LPVOID get_Cookie_data(DWORD id)
250 UINT index = id - 1;
252 if (index >= id_last)
253 return NULL;
255 if (cookies[index].id == 0)
256 return NULL;
258 return cookies[index].data;
261 LPVOID remove_Cookie(DWORD id)
263 UINT index = id - 1;
265 if (index >= id_last)
266 return NULL;
268 if (cookies[index].id == 0)
269 return NULL;
271 cookies[index].id = 0;
272 return cookies[index].data;
275 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
277 unsigned int i;
278 for (i = *index; i < id_last; i++)
279 if (cookies[i].id != 0 && cookies[i].magic == magic)
281 *index = (i+1);
282 return cookies[i].id;
284 return 0x0;
287 /*****************************************************************************
288 * Active Text Service Management
289 *****************************************************************************/
290 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgr* tm)
292 HRESULT hr;
294 /* Already Active? */
295 if (actsvr->pITfTextInputProcessor)
296 return S_OK;
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);
303 if (FAILED(hr))
305 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
306 actsvr->pITfTextInputProcessor = NULL;
307 return hr;
310 actsvr->pITfThreadMgr = tm;
311 ITfThreadMgr_AddRef(tm);
312 return hr;
315 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
317 HRESULT hr = S_OK;
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;
328 return hr;
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 */
344 break;
349 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
351 ActivatedTextService *actsvr;
352 ITfCategoryMgr *catmgr;
353 AtsEntry *entry;
354 ITfThreadMgr *tm = 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);
366 if (!actsvr->tid)
368 HeapFree(GetProcessHeap(),0,actsvr);
369 return E_OUTOFMEMORY;
372 actsvr->pITfTextInputProcessor = NULL;
373 actsvr->LanguageProfile = *lp;
374 actsvr->pITfKeyEventSink = NULL;
376 /* get TIP category */
377 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
379 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
381 ITfCategoryMgr_FindClosestCategory(catmgr,
382 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
383 list, 3);
385 ITfCategoryMgr_Release(catmgr);
387 else
389 ERR("CategoryMgr construction failed\n");
390 actsvr->LanguageProfile.catid = GUID_NULL;
393 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
394 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
396 if (activated > 0)
397 activate_given_ts(actsvr, tm);
399 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
401 if (!entry)
403 HeapFree(GetProcessHeap(),0,actsvr);
404 return E_OUTOFMEMORY;
407 entry->ats = actsvr;
408 list_add_head(&AtsList, &entry->entry);
410 return S_OK;
413 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
415 AtsEntry *ats;
417 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
419 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
421 if (profile)
422 *profile = ats->ats->LanguageProfile;
423 return TRUE;
426 return FALSE;
429 HRESULT activate_textservices(ITfThreadMgr *tm)
431 HRESULT hr = S_OK;
432 AtsEntry *ats;
434 activated ++;
435 if (activated > 1)
436 return S_OK;
438 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
440 hr = activate_given_ts(ats->ats, tm);
441 if (FAILED(hr))
442 FIXME("Failed to activate text service\n");
444 return hr;
447 HRESULT deactivate_textservices(void)
449 AtsEntry *ats;
451 if (activated > 0)
452 activated --;
454 if (activated == 0)
455 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
456 deactivate_given_ts(ats->ats);
458 return S_OK;
461 CLSID get_textservice_clsid(TfClientId tid)
463 AtsEntry *ats;
465 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
466 if (ats->ats->tid == tid)
467 return ats->ats->LanguageProfile.clsid;
468 return GUID_NULL;
471 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
473 AtsEntry *ats;
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;
482 return S_OK;
485 return E_FAIL;
488 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
490 AtsEntry *ats;
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;
499 return S_OK;
502 return E_FAIL;
505 /*************************************************************************
506 * MSCTF DllMain
508 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
510 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
511 switch (fdwReason)
513 case DLL_WINE_PREATTACH:
514 return FALSE; /* prefer native version */
515 case DLL_PROCESS_ATTACH:
516 MSCTF_hinstance = hinst;
517 tlsIndex = TlsAlloc();
518 break;
519 case DLL_PROCESS_DETACH:
520 if (fImpLoad) break;
521 TlsFree(tlsIndex);
522 break;
524 return TRUE;
527 /*************************************************************************
528 * DllCanUnloadNow (MSCTF.@)
530 HRESULT WINAPI DllCanUnloadNow(void)
532 return S_FALSE;
535 /***********************************************************************
536 * DllGetClassObject (MSCTF.@)
538 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
540 int i;
542 *ppvOut = NULL;
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 * DllRegisterServer (MSCTF.@)
557 HRESULT WINAPI DllRegisterServer(void)
559 return __wine_register_resources( MSCTF_hinstance );
562 /***********************************************************************
563 * DllUnregisterServer (MSCTF.@)
565 HRESULT WINAPI DllUnregisterServer(void)
567 return __wine_unregister_resources( MSCTF_hinstance );
570 /***********************************************************************
571 * TF_CreateThreadMgr (MSCTF.@)
573 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
575 TRACE("\n");
576 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
579 /***********************************************************************
580 * TF_GetThreadMgr (MSCTF.@)
582 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
584 TRACE("\n");
585 *pptim = TlsGetValue(tlsIndex);
587 if (*pptim)
588 ITfThreadMgr_AddRef(*pptim);
590 return S_OK;
593 /***********************************************************************
594 * SetInputScope(MSCTF.@)
596 HRESULT WINAPI SetInputScope(HWND hwnd, InputScope inputscope)
598 FIXME("STUB: %p %i\n",hwnd,inputscope);
599 return S_OK;
602 /***********************************************************************
603 * SetInputScopes(MSCTF.@)
605 HRESULT WINAPI SetInputScopes(HWND hwnd, const InputScope *pInputScopes,
606 UINT cInputScopes, WCHAR **ppszPhraseList,
607 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
609 UINT i;
610 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
611 for (i = 0; i < cInputScopes; i++)
612 TRACE("\tScope[%u] = %i\n",i,pInputScopes[i]);
613 for (i = 0; i < cPhrases; i++)
614 TRACE("\tPhrase[%u] = %s\n",i,debugstr_w(ppszPhraseList[i]));
616 return S_OK;
619 /***********************************************************************
620 * TF_CreateInputProcessorProfiles(MSCTF.@)
622 HRESULT WINAPI TF_CreateInputProcessorProfiles(
623 ITfInputProcessorProfiles **ppipr)
625 return InputProcessorProfiles_Constructor(NULL,(IUnknown**)ppipr);
628 /***********************************************************************
629 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
631 HRESULT WINAPI TF_InvalidAssemblyListCacheIfExist(void)
633 FIXME("Stub\n");
634 return S_OK;
637 /***********************************************************************
638 * TF_CreateLangBarMgr (MSCTF.@)
640 HRESULT WINAPI TF_CreateLangBarMgr(ITfLangBarMgr **pppbm)
642 TRACE("\n");
643 return LangBarMgr_Constructor(NULL,(IUnknown**)pppbm);
646 HRESULT WINAPI TF_CreateLangBarItemMgr(ITfLangBarItemMgr **pplbim)
648 FIXME("stub %p\n", pplbim);
649 *pplbim = NULL;
651 return E_NOTIMPL;
654 /***********************************************************************
655 * TF_InitMlngInfo (MSCTF.@)
657 HRESULT WINAPI TF_InitMlngInfo(void)
659 FIXME("stub\n");
660 return S_OK;