msctf: Make use of generated cookies in sinks to allow 64 bit compatibility.
[wine/hacks.git] / dlls / msctf / msctf.c
blobfac0f9d90bb88d1e97e8013d68bbd81f77a69191
1 /*
2 * MSCTF Server DLL
4 * Copyright 2008 Aric Stewart, CodeWeavers
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <stdio.h>
26 #define COBJMACROS
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"
38 #include "msctf_internal.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
42 static LONG MSCTF_refCount;
44 static HINSTANCE MSCTF_hinstance;
46 typedef struct
48 DWORD id;
49 DWORD magic;
50 LPVOID data;
51 } CookieInternal;
53 static CookieInternal *cookies;
54 static UINT id_last;
55 static UINT array_size;
57 DWORD tlsIndex = 0;
59 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};
61 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
63 static const struct {
64 REFCLSID clsid;
65 LPFNCONSTRUCTOR ctor;
66 } ClassesTable[] = {
67 {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
68 {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
69 {&CLSID_TF_CategoryMgr, CategoryMgr_Constructor},
70 {NULL, NULL}
73 typedef struct tagClassFactory
75 const IClassFactoryVtbl *vtbl;
76 LONG ref;
77 LPFNCONSTRUCTOR ctor;
78 } ClassFactory;
80 static void ClassFactory_Destructor(ClassFactory *This)
82 TRACE("Destroying class factory %p\n", This);
83 HeapFree(GetProcessHeap(),0,This);
84 MSCTF_refCount--;
87 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
89 *ppvOut = NULL;
90 if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
91 IClassFactory_AddRef(iface);
92 *ppvOut = iface;
93 return S_OK;
96 WARN("Unknown interface %s\n", debugstr_guid(riid));
97 return E_NOINTERFACE;
100 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
102 ClassFactory *This = (ClassFactory *)iface;
103 return InterlockedIncrement(&This->ref);
106 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
108 ClassFactory *This = (ClassFactory *)iface;
109 ULONG ret = InterlockedDecrement(&This->ref);
111 if (ret == 0)
112 ClassFactory_Destructor(This);
113 return ret;
116 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
118 ClassFactory *This = (ClassFactory *)iface;
119 HRESULT ret;
120 IUnknown *obj;
122 TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
123 ret = This->ctor(punkOuter, &obj);
124 if (FAILED(ret))
125 return ret;
126 ret = IUnknown_QueryInterface(obj, iid, ppvOut);
127 IUnknown_Release(obj);
128 return ret;
131 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
133 ClassFactory *This = (ClassFactory *)iface;
135 TRACE("(%p)->(%x)\n", This, fLock);
137 if(fLock)
138 InterlockedIncrement(&MSCTF_refCount);
139 else
140 InterlockedDecrement(&MSCTF_refCount);
142 return S_OK;
145 static const IClassFactoryVtbl ClassFactoryVtbl = {
146 /* IUnknown */
147 ClassFactory_QueryInterface,
148 ClassFactory_AddRef,
149 ClassFactory_Release,
151 /* IClassFactory*/
152 ClassFactory_CreateInstance,
153 ClassFactory_LockServer
156 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
158 ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
159 This->vtbl = &ClassFactoryVtbl;
160 This->ref = 1;
161 This->ctor = ctor;
162 *ppvOut = This;
163 TRACE("Created class factory %p\n", This);
164 MSCTF_refCount++;
165 return S_OK;
168 /*************************************************************************
169 * DWORD Cookie Management
171 DWORD generate_Cookie(DWORD magic, LPVOID data)
173 int i;
175 /* try to reuse IDs if possible */
176 for (i = 0; i < id_last; i++)
177 if (cookies[i].id == 0) break;
179 if (i == array_size)
181 if (!array_size)
183 cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
184 if (!cookies)
186 ERR("Out of memory, Unable to alloc cookies array\n");
187 return 0;
189 array_size = 10;
191 else
193 CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
194 sizeof(CookieInternal) * (array_size * 2));
195 if (!new_cookies)
197 ERR("Out of memory, Unable to realloc cookies array\n");
198 return 0;
200 cookies = new_cookies;
201 array_size *= 2;
205 cookies[i].id = i + 1; /* a return of 0 is used for failure */
206 cookies[i].magic = magic;
207 cookies[i].data = data;
209 if (i == id_last)
210 id_last++;
212 return cookies[i].id;
215 DWORD get_Cookie_magic(DWORD id)
217 UINT index = id - 1;
219 if (index >= id_last)
220 return 0;
222 if (cookies[index].id == 0)
223 return 0;
225 return cookies[index].magic;
228 LPVOID get_Cookie_data(DWORD id)
230 UINT index = id - 1;
232 if (index >= id_last)
233 return NULL;
235 if (cookies[index].id == 0)
236 return NULL;
238 return cookies[index].data;
241 LPVOID remove_Cookie(DWORD id)
243 UINT index = id - 1;
245 if (index >= id_last)
246 return NULL;
248 if (cookies[index].id == 0)
249 return NULL;
251 cookies[index].id = 0;
252 return cookies[index].data;
255 /*************************************************************************
256 * MSCTF DllMain
258 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
260 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
261 switch (fdwReason)
263 case DLL_WINE_PREATTACH:
264 return FALSE; /* prefer native version */
265 case DLL_PROCESS_ATTACH:
266 MSCTF_hinstance = hinst;
267 tlsIndex = TlsAlloc();
268 break;
269 case DLL_PROCESS_DETACH:
270 TlsFree(tlsIndex);
271 break;
273 return TRUE;
276 /*************************************************************************
277 * DllCanUnloadNow (MSCTF.@)
279 HRESULT WINAPI DllCanUnloadNow(void)
281 return MSCTF_refCount ? S_FALSE : S_OK;
284 /***********************************************************************
285 * DllGetClassObject (MSCTF.@)
287 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
289 int i;
291 *ppvOut = NULL;
292 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
293 return E_NOINTERFACE;
295 for (i = 0; ClassesTable[i].clsid != NULL; i++)
296 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
297 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
299 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
300 return CLASS_E_CLASSNOTAVAILABLE;
303 /***********************************************************************
304 * TF_CreateThreadMgr (MSCTF.@)
306 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
308 TRACE("\n");
309 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
312 /***********************************************************************
313 * TF_GetThreadMgr (MSCTF.@)
315 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
317 TRACE("\n");
318 *pptim = TlsGetValue(tlsIndex);
320 if (*pptim)
321 ITfThreadMgr_AddRef(*pptim);
323 return S_OK;
326 /***********************************************************************
327 * SetInputScope(MSCTF.@)
329 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
331 FIXME("STUB: %p %i\n",hwnd,inputscope);
332 return S_OK;
335 /***********************************************************************
336 * SetInputScopes(MSCTF.@)
338 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
339 UINT cInputScopes, WCHAR **ppszPhraseList,
340 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
342 int i;
343 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
344 for (i = 0; i < cInputScopes; i++)
345 TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
346 for (i = 0; i < cPhrases; i++)
347 TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));
349 return S_OK;