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