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