d3d9: Introduce a helper function to create a wined3d_swapchain_desc from D3DPRESENT_...
[wine/multimedia.git] / dlls / msctf / msctf.c
blobbc12a296359d6e82d03b1c3ee106e6310b811184
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"
39 #include "msctf_internal.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
43 static LONG MSCTF_refCount;
45 static HINSTANCE MSCTF_hinstance;
47 typedef struct
49 DWORD id;
50 DWORD magic;
51 LPVOID data;
52 } CookieInternal;
54 typedef struct {
55 TF_LANGUAGEPROFILE LanguageProfile;
56 ITfTextInputProcessor *pITfTextInputProcessor;
57 ITfThreadMgr *pITfThreadMgr;
58 ITfKeyEventSink *pITfKeyEventSink;
59 TfClientId tid;
60 } ActivatedTextService;
62 typedef struct
64 struct list entry;
65 ActivatedTextService *ats;
66 } AtsEntry;
68 static CookieInternal *cookies;
69 static UINT id_last;
70 static UINT array_size;
72 static struct list AtsList = LIST_INIT(AtsList);
73 static UINT activated = 0;
75 DWORD tlsIndex = 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);
84 static const struct {
85 REFCLSID clsid;
86 LPFNCONSTRUCTOR ctor;
87 } ClassesTable[] = {
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},
93 {NULL, NULL}
96 typedef struct tagClassFactory
98 IClassFactory IClassFactory_iface;
99 LONG ref;
100 LPFNCONSTRUCTOR ctor;
101 } ClassFactory;
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);
112 MSCTF_refCount--;
115 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
117 *ppvOut = NULL;
118 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
119 IClassFactory_AddRef(iface);
120 *ppvOut = iface;
121 return S_OK;
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);
139 if (ret == 0)
140 ClassFactory_Destructor(This);
141 return ret;
144 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
146 ClassFactory *This = impl_from_IClassFactory(iface);
147 HRESULT ret;
148 IUnknown *obj;
150 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
151 ret = This->ctor(punkOuter, &obj);
152 if (FAILED(ret))
153 return ret;
154 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
155 IUnknown_Release(obj);
156 return ret;
159 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
161 ClassFactory *This = impl_from_IClassFactory(iface);
163 TRACE("(%p)->(%x)\n", This, fLock);
165 if(fLock)
166 InterlockedIncrement(&MSCTF_refCount);
167 else
168 InterlockedDecrement(&MSCTF_refCount);
170 return S_OK;
173 static const IClassFactoryVtbl ClassFactoryVtbl = {
174 /* IUnknown */
175 ClassFactory_QueryInterface,
176 ClassFactory_AddRef,
177 ClassFactory_Release,
179 /* IClassFactory*/
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;
188 This->ref = 1;
189 This->ctor = ctor;
190 *ppvOut = This;
191 TRACE("Created class factory %p\n", This);
192 MSCTF_refCount++;
193 return S_OK;
196 /*************************************************************************
197 * DWORD Cookie Management
199 DWORD generate_Cookie(DWORD magic, LPVOID data)
201 int i;
203 /* try to reuse IDs if possible */
204 for (i = 0; i < id_last; i++)
205 if (cookies[i].id == 0) break;
207 if (i == array_size)
209 if (!array_size)
211 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
212 if (!cookies)
214 ERR("Out of memory, Unable to alloc cookies array\n");
215 return 0;
217 array_size = 10;
219 else
221 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
222 sizeof(CookieInternal) * (array_size * 2));
223 if (!new_cookies)
225 ERR("Out of memory, Unable to realloc cookies array\n");
226 return 0;
228 cookies = new_cookies;
229 array_size *= 2;
233 cookies[i].id = i + 1; /* a return of 0 is used for failure */
234 cookies[i].magic = magic;
235 cookies[i].data = data;
237 if (i == id_last)
238 id_last++;
240 return cookies[i].id;
243 DWORD get_Cookie_magic(DWORD id)
245 UINT index = id - 1;
247 if (index >= id_last)
248 return 0;
250 if (cookies[index].id == 0)
251 return 0;
253 return cookies[index].magic;
256 LPVOID get_Cookie_data(DWORD id)
258 UINT index = id - 1;
260 if (index >= id_last)
261 return NULL;
263 if (cookies[index].id == 0)
264 return NULL;
266 return cookies[index].data;
269 LPVOID remove_Cookie(DWORD id)
271 UINT index = id - 1;
273 if (index >= id_last)
274 return NULL;
276 if (cookies[index].id == 0)
277 return NULL;
279 cookies[index].id = 0;
280 return cookies[index].data;
283 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
285 int i;
286 for (i = *index; i < id_last; i++)
287 if (cookies[i].id != 0 && cookies[i].magic == magic)
289 *index = (i+1);
290 return cookies[i].id;
292 return 0x0;
295 /*****************************************************************************
296 * Active Text Service Management
297 *****************************************************************************/
298 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgr* tm)
300 HRESULT hr;
302 /* Already Active? */
303 if (actsvr->pITfTextInputProcessor)
304 return S_OK;
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);
311 if (FAILED(hr))
313 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
314 actsvr->pITfTextInputProcessor = NULL;
315 return hr;
318 actsvr->pITfThreadMgr = tm;
319 ITfThreadMgr_AddRef(tm);
320 return hr;
323 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
325 HRESULT hr = S_OK;
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;
336 return hr;
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 */
352 break;
357 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
359 ActivatedTextService *actsvr;
360 ITfCategoryMgr *catmgr;
361 AtsEntry *entry;
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);
374 if (!actsvr->tid)
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,
392 list, 3);
394 ITfCategoryMgr_Release(catmgr);
396 else
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);
405 if (activated > 0)
406 activate_given_ts(actsvr, tm);
408 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
410 if (!entry)
412 HeapFree(GetProcessHeap(),0,actsvr);
413 return E_OUTOFMEMORY;
416 entry->ats = actsvr;
417 list_add_head(&AtsList, &entry->entry);
419 return S_OK;
422 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
424 AtsEntry *ats;
426 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
428 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
430 if (profile)
431 *profile = ats->ats->LanguageProfile;
432 return TRUE;
435 return FALSE;
438 HRESULT activate_textservices(ITfThreadMgr *tm)
440 HRESULT hr = S_OK;
441 AtsEntry *ats;
443 activated ++;
444 if (activated > 1)
445 return S_OK;
447 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
449 hr = activate_given_ts(ats->ats, tm);
450 if (FAILED(hr))
451 FIXME("Failed to activate text service\n");
453 return hr;
456 HRESULT deactivate_textservices(void)
458 AtsEntry *ats;
460 if (activated > 0)
461 activated --;
463 if (activated == 0)
464 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
465 deactivate_given_ts(ats->ats);
467 return S_OK;
470 CLSID get_textservice_clsid(TfClientId tid)
472 AtsEntry *ats;
474 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
475 if (ats->ats->tid == tid)
476 return ats->ats->LanguageProfile.clsid;
477 return GUID_NULL;
480 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
482 AtsEntry *ats;
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;
491 return S_OK;
494 return E_FAIL;
497 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
499 AtsEntry *ats;
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;
508 return S_OK;
511 return E_FAIL;
514 /*************************************************************************
515 * MSCTF DllMain
517 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
519 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
520 switch (fdwReason)
522 case DLL_WINE_PREATTACH:
523 return FALSE; /* prefer native version */
524 case DLL_PROCESS_ATTACH:
525 MSCTF_hinstance = hinst;
526 tlsIndex = TlsAlloc();
527 break;
528 case DLL_PROCESS_DETACH:
529 TlsFree(tlsIndex);
530 break;
532 return TRUE;
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)
548 int i;
550 *ppvOut = NULL;
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 );
570 /***********************************************************************
571 * DllUnregisterServer (MSCTF.@)
573 HRESULT WINAPI DllUnregisterServer(void)
575 return __wine_unregister_resources( MSCTF_hinstance );
578 /***********************************************************************
579 * TF_CreateThreadMgr (MSCTF.@)
581 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
583 TRACE("\n");
584 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
587 /***********************************************************************
588 * TF_GetThreadMgr (MSCTF.@)
590 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
592 TRACE("\n");
593 *pptim = TlsGetValue(tlsIndex);
595 if (*pptim)
596 ITfThreadMgr_AddRef(*pptim);
598 return S_OK;
601 /***********************************************************************
602 * SetInputScope(MSCTF.@)
604 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
606 FIXME("STUB: %p %i\n",hwnd,inputscope);
607 return S_OK;
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)
617 int i;
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]));
624 return S_OK;
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)
641 FIXME("Stub\n");
642 return S_OK;
645 /***********************************************************************
646 * TF_CreateLangBarMgr (MSCTF.@)
648 HRESULT WINAPI TF_CreateLangBarMgr(ITfLangBarMgr **pppbm)
650 TRACE("\n");
651 return LangBarMgr_Constructor(NULL,(IUnknown**)pppbm);