[MSCTF] Sync with Wine Staging 1.9.4. CORE-10912
[reactos.git] / reactos / 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;
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 /*****************************************************************************
270 * Active Text Service Management
271 *****************************************************************************/
272 static HRESULT activate_given_ts(ActivatedTextService *actsvr, ITfThreadMgrEx *tm)
273 {
274 HRESULT hr;
275
276 /* Already Active? */
277 if (actsvr->pITfTextInputProcessor)
278 return S_OK;
279
280 hr = CoCreateInstance (&actsvr->LanguageProfile.clsid, NULL, CLSCTX_INPROC_SERVER,
281 &IID_ITfTextInputProcessor, (void**)&actsvr->pITfTextInputProcessor);
282 if (FAILED(hr)) return hr;
283
284 hr = ITfTextInputProcessor_Activate(actsvr->pITfTextInputProcessor, (ITfThreadMgr *)tm, actsvr->tid);
285 if (FAILED(hr))
286 {
287 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
288 actsvr->pITfTextInputProcessor = NULL;
289 return hr;
290 }
291
292 actsvr->pITfThreadMgrEx = tm;
293 ITfThreadMgrEx_AddRef(tm);
294 return hr;
295 }
296
297 static HRESULT deactivate_given_ts(ActivatedTextService *actsvr)
298 {
299 HRESULT hr = S_OK;
300
301 if (actsvr->pITfTextInputProcessor)
302 {
303 hr = ITfTextInputProcessor_Deactivate(actsvr->pITfTextInputProcessor);
304 ITfTextInputProcessor_Release(actsvr->pITfTextInputProcessor);
305 ITfThreadMgrEx_Release(actsvr->pITfThreadMgrEx);
306 actsvr->pITfTextInputProcessor = NULL;
307 actsvr->pITfThreadMgrEx = NULL;
308 }
309
310 return hr;
311 }
312
313 static void deactivate_remove_conflicting_ts(REFCLSID catid)
314 {
315 AtsEntry *ats, *cursor2;
316
317 LIST_FOR_EACH_ENTRY_SAFE(ats, cursor2, &AtsList, AtsEntry, entry)
318 {
319 if (IsEqualCLSID(catid,&ats->ats->LanguageProfile.catid))
320 {
321 deactivate_given_ts(ats->ats);
322 list_remove(&ats->entry);
323 HeapFree(GetProcessHeap(),0,ats->ats);
324 HeapFree(GetProcessHeap(),0,ats);
325 /* we are guarenteeing there is only 1 */
326 break;
327 }
328 }
329 }
330
331 HRESULT add_active_textservice(TF_LANGUAGEPROFILE *lp)
332 {
333 ActivatedTextService *actsvr;
334 ITfCategoryMgr *catmgr;
335 AtsEntry *entry;
336 ITfThreadMgrEx *tm = TlsGetValue(tlsIndex);
337 ITfClientId *clientid;
338
339 if (!tm) return E_UNEXPECTED;
340
341 actsvr = HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService));
342 if (!actsvr) return E_OUTOFMEMORY;
343
344 ITfThreadMgrEx_QueryInterface(tm, &IID_ITfClientId, (void **)&clientid);
345 ITfClientId_GetClientId(clientid, &lp->clsid, &actsvr->tid);
346 ITfClientId_Release(clientid);
347
348 if (!actsvr->tid)
349 {
350 HeapFree(GetProcessHeap(),0,actsvr);
351 return E_OUTOFMEMORY;
352 }
353
354 actsvr->pITfTextInputProcessor = NULL;
355 actsvr->LanguageProfile = *lp;
356 actsvr->pITfKeyEventSink = NULL;
357
358 /* get TIP category */
359 if (SUCCEEDED(CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr)))
360 {
361 static const GUID *list[3] = {&GUID_TFCAT_TIP_SPEECH, &GUID_TFCAT_TIP_KEYBOARD, &GUID_TFCAT_TIP_HANDWRITING};
362
363 ITfCategoryMgr_FindClosestCategory(catmgr,
364 &actsvr->LanguageProfile.clsid, &actsvr->LanguageProfile.catid,
365 list, 3);
366
367 ITfCategoryMgr_Release(catmgr);
368 }
369 else
370 {
371 ERR("CategoryMgr construction failed\n");
372 actsvr->LanguageProfile.catid = GUID_NULL;
373 }
374
375 if (!IsEqualGUID(&actsvr->LanguageProfile.catid,&GUID_NULL))
376 deactivate_remove_conflicting_ts(&actsvr->LanguageProfile.catid);
377
378 if (activated > 0)
379 activate_given_ts(actsvr, tm);
380
381 entry = HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry));
382
383 if (!entry)
384 {
385 HeapFree(GetProcessHeap(),0,actsvr);
386 return E_OUTOFMEMORY;
387 }
388
389 entry->ats = actsvr;
390 list_add_head(&AtsList, &entry->entry);
391
392 return S_OK;
393 }
394
395 BOOL get_active_textservice(REFCLSID rclsid, TF_LANGUAGEPROFILE *profile)
396 {
397 AtsEntry *ats;
398
399 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
400 {
401 if (IsEqualCLSID(rclsid,&ats->ats->LanguageProfile.clsid))
402 {
403 if (profile)
404 *profile = ats->ats->LanguageProfile;
405 return TRUE;
406 }
407 }
408 return FALSE;
409 }
410
411 HRESULT activate_textservices(ITfThreadMgrEx *tm)
412 {
413 HRESULT hr = S_OK;
414 AtsEntry *ats;
415
416 activated ++;
417 if (activated > 1)
418 return S_OK;
419
420 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
421 {
422 hr = activate_given_ts(ats->ats, tm);
423 if (FAILED(hr))
424 FIXME("Failed to activate text service\n");
425 }
426 return hr;
427 }
428
429 HRESULT deactivate_textservices(void)
430 {
431 AtsEntry *ats;
432
433 if (activated > 0)
434 activated --;
435
436 if (activated == 0)
437 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
438 deactivate_given_ts(ats->ats);
439
440 return S_OK;
441 }
442
443 CLSID get_textservice_clsid(TfClientId tid)
444 {
445 AtsEntry *ats;
446
447 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
448 if (ats->ats->tid == tid)
449 return ats->ats->LanguageProfile.clsid;
450 return GUID_NULL;
451 }
452
453 HRESULT get_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown **sink)
454 {
455 AtsEntry *ats;
456
457 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
458 return E_NOINTERFACE;
459
460 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
461 if (ats->ats->tid == tid)
462 {
463 *sink = (IUnknown*)ats->ats->pITfKeyEventSink;
464 return S_OK;
465 }
466
467 return E_FAIL;
468 }
469
470 HRESULT set_textservice_sink(TfClientId tid, REFCLSID iid, IUnknown* sink)
471 {
472 AtsEntry *ats;
473
474 if (!IsEqualCLSID(iid,&IID_ITfKeyEventSink))
475 return E_NOINTERFACE;
476
477 LIST_FOR_EACH_ENTRY(ats, &AtsList, AtsEntry, entry)
478 if (ats->ats->tid == tid)
479 {
480 ats->ats->pITfKeyEventSink = (ITfKeyEventSink*)sink;
481 return S_OK;
482 }
483
484 return E_FAIL;
485 }
486
487 /*************************************************************************
488 * MSCTF DllMain
489 */
490 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
491 {
492 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
493 switch (fdwReason)
494 {
495 case DLL_WINE_PREATTACH:
496 return FALSE; /* prefer native version */
497 case DLL_PROCESS_ATTACH:
498 MSCTF_hinstance = hinst;
499 tlsIndex = TlsAlloc();
500 break;
501 case DLL_PROCESS_DETACH:
502 if (fImpLoad) break;
503 TlsFree(tlsIndex);
504 break;
505 }
506 return TRUE;
507 }
508
509 /*************************************************************************
510 * DllCanUnloadNow (MSCTF.@)
511 */
512 HRESULT WINAPI DllCanUnloadNow(void)
513 {
514 return S_FALSE;
515 }
516
517 /***********************************************************************
518 * DllGetClassObject (MSCTF.@)
519 */
520 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
521 {
522 int i;
523
524 *ppvOut = NULL;
525 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
526 return E_NOINTERFACE;
527
528 for (i = 0; ClassesTable[i].clsid != NULL; i++)
529 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
530 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
531 }
532 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
533 return CLASS_E_CLASSNOTAVAILABLE;
534 }
535
536 /***********************************************************************
537 * DllRegisterServer (MSCTF.@)
538 */
539 HRESULT WINAPI DllRegisterServer(void)
540 {
541 return __wine_register_resources( MSCTF_hinstance );
542 }
543
544 /***********************************************************************
545 * DllUnregisterServer (MSCTF.@)
546 */
547 HRESULT WINAPI DllUnregisterServer(void)
548 {
549 return __wine_unregister_resources( MSCTF_hinstance );
550 }
551
552 /***********************************************************************
553 * TF_CreateThreadMgr (MSCTF.@)
554 */
555 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
556 {
557 TRACE("\n");
558 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
559 }
560
561 /***********************************************************************
562 * TF_GetThreadMgr (MSCTF.@)
563 */
564 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
565 {
566 TRACE("\n");
567 *pptim = TlsGetValue(tlsIndex);
568
569 if (*pptim)
570 ITfThreadMgr_AddRef(*pptim);
571
572 return S_OK;
573 }
574
575 /***********************************************************************
576 * SetInputScope(MSCTF.@)
577 */
578 HRESULT WINAPI SetInputScope(HWND hwnd, InputScope inputscope)
579 {
580 FIXME("STUB: %p %i\n",hwnd,inputscope);
581 return S_OK;
582 }
583
584 /***********************************************************************
585 * SetInputScopes(MSCTF.@)
586 */
587 HRESULT WINAPI SetInputScopes(HWND hwnd, const InputScope *pInputScopes,
588 UINT cInputScopes, WCHAR **ppszPhraseList,
589 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
590 {
591 UINT i;
592 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
593 for (i = 0; i < cInputScopes; i++)
594 TRACE("\tScope[%u] = %i\n",i,pInputScopes[i]);
595 for (i = 0; i < cPhrases; i++)
596 TRACE("\tPhrase[%u] = %s\n",i,debugstr_w(ppszPhraseList[i]));
597
598 return S_OK;
599 }
600
601 /***********************************************************************
602 * TF_CreateInputProcessorProfiles(MSCTF.@)
603 */
604 HRESULT WINAPI TF_CreateInputProcessorProfiles(
605 ITfInputProcessorProfiles **ppipr)
606 {
607 return InputProcessorProfiles_Constructor(NULL,(IUnknown**)ppipr);
608 }
609
610 /***********************************************************************
611 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
612 */
613 HRESULT WINAPI TF_InvalidAssemblyListCacheIfExist(void)
614 {
615 FIXME("Stub\n");
616 return S_OK;
617 }
618
619 /***********************************************************************
620 * TF_CreateLangBarMgr (MSCTF.@)
621 */
622 HRESULT WINAPI TF_CreateLangBarMgr(ITfLangBarMgr **pppbm)
623 {
624 TRACE("\n");
625 return LangBarMgr_Constructor(NULL,(IUnknown**)pppbm);
626 }
627
628 HRESULT WINAPI TF_CreateLangBarItemMgr(ITfLangBarItemMgr **pplbim)
629 {
630 FIXME("stub %p\n", pplbim);
631 *pplbim = NULL;
632
633 return E_NOTIMPL;
634 }
635
636 /***********************************************************************
637 * TF_InitMlngInfo (MSCTF.@)
638 */
639 HRESULT WINAPI TF_InitMlngInfo(void)
640 {
641 FIXME("stub\n");
642 return S_OK;
643 }