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