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