msctf: Implement ITfInputProcessorProfiles::IsEnabledLanguageProfile.
[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 "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
33 #include "shlguid.h"
34 #include "comcat.h"
35 #include "initguid.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 DWORD tlsIndex = 0;
47
48 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};
49
50 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
51
52 static const struct {
53     REFCLSID clsid;
54     LPFNCONSTRUCTOR ctor;
55 } ClassesTable[] = {
56     {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
57     {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
58     {&CLSID_TF_CategoryMgr, CategoryMgr_Constructor},
59     {NULL, NULL}
60 };
61
62 typedef struct tagClassFactory
63 {
64     const IClassFactoryVtbl *vtbl;
65     LONG   ref;
66     LPFNCONSTRUCTOR ctor;
67 } ClassFactory;
68
69 static void ClassFactory_Destructor(ClassFactory *This)
70 {
71     TRACE("Destroying class factory %p\n", This);
72     HeapFree(GetProcessHeap(),0,This);
73     MSCTF_refCount--;
74 }
75
76 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
77 {
78     *ppvOut = NULL;
79     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
80         IClassFactory_AddRef(iface);
81         *ppvOut = iface;
82         return S_OK;
83     }
84
85     WARN("Unknown interface %s\n", debugstr_guid(riid));
86     return E_NOINTERFACE;
87 }
88
89 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
90 {
91     ClassFactory *This = (ClassFactory *)iface;
92     return InterlockedIncrement(&This->ref);
93 }
94
95 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
96 {
97     ClassFactory *This = (ClassFactory *)iface;
98     ULONG ret = InterlockedDecrement(&This->ref);
99
100     if (ret == 0)
101         ClassFactory_Destructor(This);
102     return ret;
103 }
104
105 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
106 {
107     ClassFactory *This = (ClassFactory *)iface;
108     HRESULT ret;
109     IUnknown *obj;
110
111     TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
112     ret = This->ctor(punkOuter, &obj);
113     if (FAILED(ret))
114         return ret;
115     ret = IUnknown_QueryInterface(obj, iid, ppvOut);
116     IUnknown_Release(obj);
117     return ret;
118 }
119
120 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
121 {
122     ClassFactory *This = (ClassFactory *)iface;
123
124     TRACE("(%p)->(%x)\n", This, fLock);
125
126     if(fLock)
127         InterlockedIncrement(&MSCTF_refCount);
128     else
129         InterlockedDecrement(&MSCTF_refCount);
130
131     return S_OK;
132 }
133
134 static const IClassFactoryVtbl ClassFactoryVtbl = {
135     /* IUnknown */
136     ClassFactory_QueryInterface,
137     ClassFactory_AddRef,
138     ClassFactory_Release,
139
140     /* IClassFactory*/
141     ClassFactory_CreateInstance,
142     ClassFactory_LockServer
143 };
144
145 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
146 {
147     ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
148     This->vtbl = &ClassFactoryVtbl;
149     This->ref = 1;
150     This->ctor = ctor;
151     *ppvOut = (LPVOID)This;
152     TRACE("Created class factory %p\n", This);
153     MSCTF_refCount++;
154     return S_OK;
155 }
156
157 /*************************************************************************
158  * MSCTF DllMain
159  */
160 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
161 {
162     TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
163     switch (fdwReason)
164     {
165         case DLL_WINE_PREATTACH:
166             return FALSE;   /* prefer native version */
167         case DLL_PROCESS_ATTACH:
168             MSCTF_hinstance = hinst;
169             tlsIndex = TlsAlloc();
170             break;
171         case DLL_PROCESS_DETACH:
172             TlsFree(tlsIndex);
173             break;
174     }
175     return TRUE;
176 }
177
178 /*************************************************************************
179  *              DllCanUnloadNow (MSCTF.@)
180  */
181 HRESULT WINAPI DllCanUnloadNow(void)
182 {
183     return MSCTF_refCount ? S_FALSE : S_OK;
184 }
185
186 /***********************************************************************
187  *              DllGetClassObject (MSCTF.@)
188  */
189 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
190 {
191     int i;
192
193     *ppvOut = NULL;
194     if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
195         return E_NOINTERFACE;
196
197     for (i = 0; ClassesTable[i].clsid != NULL; i++)
198         if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
199             return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
200         }
201     FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
202     return CLASS_E_CLASSNOTAVAILABLE;
203 }
204
205 /***********************************************************************
206  *              TF_CreateThreadMgr (MSCTF.@)
207  */
208 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
209 {
210     TRACE("\n");
211     return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
212 }
213
214 /***********************************************************************
215  *              TF_GetThreadMgr (MSCTF.@)
216  */
217 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
218 {
219     TRACE("\n");
220     *pptim = (ITfThreadMgr*)TlsGetValue(tlsIndex);
221
222     if (*pptim)
223         ITfThreadMgr_AddRef(*pptim);
224
225     return S_OK;
226 }
227
228 /***********************************************************************
229  *              SetInputScope(MSCTF.@)
230  */
231 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
232 {
233     FIXME("STUB: %p %i\n",hwnd,inputscope);
234     return S_OK;
235 }
236
237 /***********************************************************************
238  *              SetInputScopes(MSCTF.@)
239  */
240 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
241                               UINT cInputScopes, WCHAR **ppszPhraseList,
242                               UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
243 {
244     int i;
245     FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
246     for (i = 0; i < cInputScopes; i++)
247         TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
248     for (i = 0; i < cPhrases; i++)
249         TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));
250
251     return S_OK;
252 }