[MSCMS] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / win32 / msctf / msctf.c
1 /*
2 * MSCTF Server DLL
3 *
4 * Copyright 2008 Aric Stewart, CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "msctf_internal.h"
22
23 #include <rpcproxy.h>
24 #include <inputscope.h>
25
26 static HINSTANCE MSCTF_hinstance;
27
28 typedef struct
29 {
30 DWORD id;
31 DWORD magic;
32 LPVOID data;
33 } CookieInternal;
34
35 typedef struct {
36 TF_LANGUAGEPROFILE LanguageProfile;
37 ITfTextInputProcessor *pITfTextInputProcessor;
38 ITfThreadMgrEx *pITfThreadMgrEx;
39 ITfKeyEventSink *pITfKeyEventSink;
40 TfClientId tid;
41 } ActivatedTextService;
42
43 typedef struct
44 {
45 struct list entry;
46 ActivatedTextService *ats;
47 } AtsEntry;
48
49 static CookieInternal *cookies;
50 static UINT id_last;
51 static UINT array_size;
52
53 static struct list AtsList = LIST_INIT(AtsList);
54 static UINT activated = 0;
55
56 DWORD tlsIndex = 0;
57 TfClientId processId = 0;
58 ITfCompartmentMgr *globalCompartmentMgr = NULL;
59
60 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};
61 const WCHAR szwSystemCTFKey[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F',0};
62
63 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
64
65 static const struct {
66 REFCLSID clsid;
67 LPFNCONSTRUCTOR ctor;
68 } ClassesTable[] = {
69 {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
70 {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
71 {&CLSID_TF_CategoryMgr, CategoryMgr_Constructor},
72 {&CLSID_TF_LangBarMgr, LangBarMgr_Constructor},
73 {&CLSID_TF_DisplayAttributeMgr, DisplayAttributeMgr_Constructor},
74 {NULL, NULL}
75 };
76
77 typedef struct tagClassFactory
78 {
79 IClassFactory IClassFactory_iface;
80 LONG ref;
81 LPFNCONSTRUCTOR ctor;
82 } ClassFactory;
83
84 static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface)
85 {
86 return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface);
87 }
88
89 static void ClassFactory_Destructor(ClassFactory *This)
90 {
91 TRACE("Destroying class factory %p\n", This);
92 HeapFree(GetProcessHeap(),0,This);
93 }
94
95 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
96 {
97 *ppvOut = NULL;
98 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
99 IClassFactory_AddRef(iface);
100 *ppvOut = iface;
101 return S_OK;
102 }
103
104 WARN("Unknown interface %s\n", debugstr_guid(riid));
105 return E_NOINTERFACE;
106 }
107
108 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
109 {
110 ClassFactory *This = impl_from_IClassFactory(iface);
111 return InterlockedIncrement(&This->ref);
112 }
113
114 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
115 {
116 ClassFactory *This = impl_from_IClassFactory(iface);
117 ULONG ret = InterlockedDecrement(&This->ref);
118
119 if (ret == 0)
120 ClassFactory_Destructor(This);
121 return ret;
122 }
123
124 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
125 {
126 ClassFactory *This = impl_from_IClassFactory(iface);
127 HRESULT ret;
128 IUnknown *obj;
129
130 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
131 ret = This->ctor(punkOuter, &obj);
132 if (FAILED(ret))
133 return ret;
134 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
135 IUnknown_Release(obj);
136 return ret;
137 }
138
139 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
140 {
141 ClassFactory *This = impl_from_IClassFactory(iface);
142
143 TRACE("(%p)->(%x)\n", This, fLock);
144
145 return S_OK;
146 }
147
148 static const IClassFactoryVtbl ClassFactoryVtbl = {
149 /* IUnknown */
150 ClassFactory_QueryInterface,
151 ClassFactory_AddRef,
152 ClassFactory_Release,
153
154 /* IClassFactory*/
155 ClassFactory_CreateInstance,
156 ClassFactory_LockServer
157 };
158
159 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
160 {
161 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
162 This->IClassFactory_iface.lpVtbl = &ClassFactoryVtbl;
163 This->ref = 1;
164 This->ctor = ctor;
165 *ppvOut = &This->IClassFactory_iface;
166 TRACE("Created class factory %p\n", This);
167 return S_OK;
168 }
169
170 /*************************************************************************
171 * DWORD Cookie Management
172 */
173 DWORD generate_Cookie(DWORD magic, LPVOID data)
174 {
175 UINT i;
176
177 /* try to reuse IDs if possible */
178 for (i = 0; i < id_last; i++)
179 if (cookies[i].id == 0) break;
180
181 if (i == array_size)
182 {
183 if (!array_size)
184 {
185 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
186 if (!cookies)
187 {
188 ERR("Out of memory, Unable to alloc cookies array\n");
189 return 0;
190 }
191 array_size = 10;
192 }
193 else
194 {
195 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
196 sizeof(CookieInternal) * (array_size * 2));
197 if (!new_cookies)
198 {
199 ERR("Out of memory, Unable to realloc cookies array\n");
200 return 0;
201 }
202 cookies = new_cookies;
203 array_size *= 2;
204 }
205 }
206
207 cookies[i].id = i + 1; /* a return of 0 is used for failure */
208 cookies[i].magic = magic;
209 cookies[i].data = data;
210
211 if (i == id_last)
212 id_last++;
213
214 return cookies[i].id;
215 }
216
217 DWORD get_Cookie_magic(DWORD id)
218 {
219 UINT index = id - 1;
220
221 if (index >= id_last)
222 return 0;
223
224 if (cookies[index].id == 0)
225 return 0;
226
227 return cookies[index].magic;
228 }
229
230 LPVOID get_Cookie_data(DWORD id)
231 {
232 UINT index = id - 1;
233
234 if (index >= id_last)
235 return NULL;
236
237 if (cookies[index].id == 0)
238 return NULL;
239
240 return cookies[index].data;
241 }
242
243 LPVOID remove_Cookie(DWORD id)
244 {
245 UINT index = id - 1;
246
247 if (index >= id_last)
248 return NULL;
249
250 if (cookies[index].id == 0)
251 return NULL;
252
253 cookies[index].id = 0;
254 return cookies[index].data;
255 }
256
257 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
258 {
259 unsigned int i;
260 for (i = *index; i < id_last; i++)
261 if (cookies[i].id != 0 && cookies[i].magic == magic)
262 {
263 *index = (i+1);
264 return cookies[i].id;
265 }
266 return 0x0;
267 }
268
269 HRESULT advise_sink(struct list *sink_list, REFIID riid, DWORD cookie_magic, IUnknown *unk, DWORD *cookie)
270 {
271 Sink *sink;
272
273 sink = HeapAlloc(GetProcessHeap(), 0, sizeof(*sink));
274 if (!sink)
275 return E_OUTOFMEMORY;
276
277 if (FAILED(IUnknown_QueryInterface(unk, riid, (void**)&sink->interfaces.pIUnknown)))
278 {
279 HeapFree(GetProcessHeap(), 0, sink);
280 return CONNECT_E_CANNOTCONNECT;
281 }
282
283 list_add_head(sink_list, &sink->entry);
284 *cookie = generate_Cookie(cookie_magic, sink);
285 TRACE("cookie %x\n", *cookie);
286 return S_OK;
287 }
288
289 static void free_sink(Sink *sink)
290 {
291 list_remove(&sink->entry);
292 IUnknown_Release(sink->interfaces.pIUnknown);
293 HeapFree(GetProcessHeap(), 0, sink);
294 }
295
296 HRESULT unadvise_sink(DWORD cookie)
297 {
298 Sink *sink;
299
300 sink = remove_Cookie(cookie);
301 if (!sink)
302 return CONNECT_E_NOCONNECTION;
303
304 free_sink(sink);
305 return S_OK;
306 }
307
308 void free_sinks(struct list *sink_list)
309 {
310 while(!list_empty(sink_list))
311 {
312 Sink* sink = LIST_ENTRY(sink_list->next, Sink, entry);
313 free_sink(sink);
314 }
315 }
316
317 /*****************************************************************************
318 * Active Text Service Management
319 *****************************************************************************/
320 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgrEx *tm)
321 {
322 HRESULT hr;
323
324 /* Already Active? */
325 if (actsvr->pITfTextInputProcessor)
326 return S_OK;
327
328 hr = CoCreateInstance (&actsvr->LanguageProfile.clsid, NULL, CLSCTX_INPROC_SERVER,
329 &IID_ITfTextInputProcessor, (void**)&actsvr->pITfTextInputProcessor);
330 if (FAILED(hr)) return hr;
331
332 hr = ITfTextInputProcessor_Activate(actsvr->pITfTextInputProcessor, (ITfThreadMgr *)tm, actsvr->tid);
333 if (FAILED(hr))
334 {
335 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
336 actsvr->pITfTextInputProcessor = NULL;
337 return hr;
338 }
339
340 actsvr->pITfThreadMgrEx = tm;
341 ITfThreadMgrEx_AddRef(tm);
342 return hr;
343 }
344
345 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
346 {
347 HRESULT hr = S_OK;
348
349 if (actsvr->pITfTextInputProcessor)
350 {
351 hr = ITfTextInputProcessor_Deactivate(actsvr->pITfTextInputProcessor);
352 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
353 ITfThreadMgrEx_Release(actsvr->pITfThreadMgrEx);
354 actsvr->pITfTextInputProcessor = NULL;
355 actsvr->pITfThreadMgrEx = NULL;
356 }
357
358 return hr;
359 }
360
361 static void deactivate_remove_conflicting_ts(REFCLSID catid)
362 {
363 AtsEntry *ats, *cursor2;
364
365 LIST_FOR_EACH_ENTRY_SAFE(ats, cursor2, &AtsList, AtsEntry, entry)
366 {
367 if (IsEqualCLSID(catid,&ats->ats->LanguageProfile.catid))
368 {
369 deactivate_given_ts(ats->ats);
370 list_remove(&ats->entry);
371 HeapFree(GetProcessHeap(),0,ats->ats);
372 HeapFree(GetProcessHeap(),0,ats);
373 /* we are guaranteeing there is only 1 */
374 break;
375 }
376 }
377 }
378
379 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
380 {
381 ActivatedTextService *actsvr;
382 ITfCategoryMgr *catmgr;
383 AtsEntry *entry;
384 ITfThreadMgrEx *tm = TlsGetValue(tlsIndex);
385 ITfClientId *clientid;
386
387 if (!tm) return E_UNEXPECTED;
388
389 actsvr = HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService));
390 if (!actsvr) return E_OUTOFMEMORY;
391
392 ITfThreadMgrEx_QueryInterface(tm, &IID_ITfClientId, (void **)&clientid);
393 ITfClientId_GetClientId(clientid, &lp->clsid, &actsvr->tid);
394 ITfClientId_Release(clientid);
395
396 if (!actsvr->tid)
397 {
398 HeapFree(GetProcessHeap(),0,actsvr);
399 return E_OUTOFMEMORY;
400 }
401
402 actsvr->pITfTextInputProcessor = NULL;
403 actsvr->LanguageProfile = *lp;
404 actsvr->pITfKeyEventSink = NULL;
405
406 /* get TIP category */
407 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
408 {
409 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
410
411 ITfCategoryMgr_FindClosestCategory(catmgr,
412 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
413 list, 3);
414
415 ITfCategoryMgr_Release(catmgr);
416 }
417 else
418 {
419 ERR("CategoryMgr construction failed\n");
420 actsvr->LanguageProfile.catid = GUID_NULL;
421 }
422
423 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
424 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
425
426 if (activated > 0)
427 activate_given_ts(actsvr, tm);
428
429 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
430
431 if (!entry)
432 {
433 HeapFree(GetProcessHeap(),0,actsvr);
434 return E_OUTOFMEMORY;
435 }
436
437 entry->ats = actsvr;
438 list_add_head(&AtsList, &entry->entry);
439
440 return S_OK;
441 }
442
443 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
444 {
445 AtsEntry *ats;
446
447 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
448 {
449 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
450 {
451 if (profile)
452 *profile = ats->ats->LanguageProfile;
453 return TRUE;
454 }
455 }
456 return FALSE;
457 }
458
459 HRESULT activate_textservices(ITfThreadMgrEx *tm)
460 {
461 HRESULT hr = S_OK;
462 AtsEntry *ats;
463
464 activated ++;
465 if (activated > 1)
466 return S_OK;
467
468 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
469 {
470 hr = activate_given_ts(ats->ats, tm);
471 if (FAILED(hr))
472 FIXME("Failed to activate text service\n");
473 }
474 return hr;
475 }
476
477 HRESULT deactivate_textservices(void)
478 {
479 AtsEntry *ats;
480
481 if (activated > 0)
482 activated --;
483
484 if (activated == 0)
485 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
486 deactivate_given_ts(ats->ats);
487
488 return S_OK;
489 }
490
491 CLSID get_textservice_clsid(TfClientId tid)
492 {
493 AtsEntry *ats;
494
495 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
496 if (ats->ats->tid == tid)
497 return ats->ats->LanguageProfile.clsid;
498 return GUID_NULL;
499 }
500
501 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
502 {
503 AtsEntry *ats;
504
505 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
506 return E_NOINTERFACE;
507
508 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
509 if (ats->ats->tid == tid)
510 {
511 *sink = (IUnknown*)ats->ats->pITfKeyEventSink;
512 return S_OK;
513 }
514
515 return E_FAIL;
516 }
517
518 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
519 {
520 AtsEntry *ats;
521
522 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
523 return E_NOINTERFACE;
524
525 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
526 if (ats->ats->tid == tid)
527 {
528 ats->ats->pITfKeyEventSink = (ITfKeyEventSink*)sink;
529 return S_OK;
530 }
531
532 return E_FAIL;
533 }
534
535 /*************************************************************************
536 * MSCTF DllMain
537 */
538 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
539 {
540 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
541 switch (fdwReason)
542 {
543 case DLL_WINE_PREATTACH:
544 return FALSE; /* prefer native version */
545 case DLL_PROCESS_ATTACH:
546 MSCTF_hinstance = hinst;
547 tlsIndex = TlsAlloc();
548 break;
549 case DLL_PROCESS_DETACH:
550 if (fImpLoad) break;
551 TlsFree(tlsIndex);
552 break;
553 }
554 return TRUE;
555 }
556
557 /*************************************************************************
558 * DllCanUnloadNow (MSCTF.@)
559 */
560 HRESULT WINAPI DllCanUnloadNow(void)
561 {
562 return S_FALSE;
563 }
564
565 /***********************************************************************
566 * DllGetClassObject (MSCTF.@)
567 */
568 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
569 {
570 int i;
571
572 *ppvOut = NULL;
573 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
574 return E_NOINTERFACE;
575
576 for (i = 0; ClassesTable[i].clsid != NULL; i++)
577 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
578 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
579 }
580 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
581 return CLASS_E_CLASSNOTAVAILABLE;
582 }
583
584 /***********************************************************************
585 * DllRegisterServer (MSCTF.@)
586 */
587 HRESULT WINAPI DllRegisterServer(void)
588 {
589 return __wine_register_resources( MSCTF_hinstance );
590 }
591
592 /***********************************************************************
593 * DllUnregisterServer (MSCTF.@)
594 */
595 HRESULT WINAPI DllUnregisterServer(void)
596 {
597 return __wine_unregister_resources( MSCTF_hinstance );
598 }
599
600 /***********************************************************************
601 * TF_CreateThreadMgr (MSCTF.@)
602 */
603 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
604 {
605 TRACE("\n");
606 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
607 }
608
609 /***********************************************************************
610 * TF_GetThreadMgr (MSCTF.@)
611 */
612 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
613 {
614 TRACE("\n");
615 *pptim = TlsGetValue(tlsIndex);
616
617 if (*pptim)
618 ITfThreadMgr_AddRef(*pptim);
619
620 return S_OK;
621 }
622
623 /***********************************************************************
624 * SetInputScope(MSCTF.@)
625 */
626 HRESULT WINAPI SetInputScope(HWND hwnd, InputScope inputscope)
627 {
628 FIXME("STUB: %p %i\n",hwnd,inputscope);
629 return S_OK;
630 }
631
632 /***********************************************************************
633 * SetInputScopes(MSCTF.@)
634 */
635 HRESULT WINAPI SetInputScopes(HWND hwnd, const InputScope *pInputScopes,
636 UINT cInputScopes, WCHAR **ppszPhraseList,
637 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
638 {
639 UINT i;
640 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
641 for (i = 0; i < cInputScopes; i++)
642 TRACE("\tScope[%u] = %i\n",i,pInputScopes[i]);
643 for (i = 0; i < cPhrases; i++)
644 TRACE("\tPhrase[%u] = %s\n",i,debugstr_w(ppszPhraseList[i]));
645
646 return S_OK;
647 }
648
649 /***********************************************************************
650 * TF_CreateInputProcessorProfiles(MSCTF.@)
651 */
652 HRESULT WINAPI TF_CreateInputProcessorProfiles(
653 ITfInputProcessorProfiles **ppipr)
654 {
655 return InputProcessorProfiles_Constructor(NULL,(IUnknown**)ppipr);
656 }
657
658 /***********************************************************************
659 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
660 */
661 HRESULT WINAPI TF_InvalidAssemblyListCacheIfExist(void)
662 {
663 FIXME("Stub\n");
664 return S_OK;
665 }
666
667 /***********************************************************************
668 * TF_CreateLangBarMgr (MSCTF.@)
669 */
670 HRESULT WINAPI TF_CreateLangBarMgr(ITfLangBarMgr **pppbm)
671 {
672 TRACE("\n");
673 return LangBarMgr_Constructor(NULL,(IUnknown**)pppbm);
674 }
675
676 HRESULT WINAPI TF_CreateLangBarItemMgr(ITfLangBarItemMgr **pplbim)
677 {
678 FIXME("stub %p\n", pplbim);
679 *pplbim = NULL;
680
681 return E_NOTIMPL;
682 }
683
684 /***********************************************************************
685 * TF_InitMlngInfo (MSCTF.@)
686 */
687 HRESULT WINAPI TF_InitMlngInfo(void)
688 {
689 FIXME("stub\n");
690 return S_OK;
691 }