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