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