msctf: Implement ITfCategoryMgr::RegisterGUID, ITfCategoryMgr::GetGUID and ITfCategor...
[wine/multimedia.git] / dlls / msctf / msctf.c
blob711ae5177425df48c06aca8cf71a002461fd6cd1
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 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
257 int i;
258 for (i = *index; i < id_last; i++)
259 if (cookies[i].id != 0 && cookies[i].magic == magic)
261 *index = (i+1);
262 return cookies[i].id;
264 return 0x0;
267 /*************************************************************************
268 * MSCTF DllMain
270 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
272 TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
273 switch (fdwReason)
275 case DLL_WINE_PREATTACH:
276 return FALSE; /* prefer native version */
277 case DLL_PROCESS_ATTACH:
278 MSCTF_hinstance = hinst;
279 tlsIndex = TlsAlloc();
280 break;
281 case DLL_PROCESS_DETACH:
282 TlsFree(tlsIndex);
283 break;
285 return TRUE;
288 /*************************************************************************
289 * DllCanUnloadNow (MSCTF.@)
291 HRESULT WINAPI DllCanUnloadNow(void)
293 return MSCTF_refCount ? S_FALSE : S_OK;
296 /***********************************************************************
297 * DllGetClassObject (MSCTF.@)
299 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
301 int i;
303 *ppvOut = NULL;
304 if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
305 return E_NOINTERFACE;
307 for (i = 0; ClassesTable[i].clsid != NULL; i++)
308 if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
309 return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
311 FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
312 return CLASS_E_CLASSNOTAVAILABLE;
315 /***********************************************************************
316 * TF_CreateThreadMgr (MSCTF.@)
318 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
320 TRACE("\n");
321 return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
324 /***********************************************************************
325 * TF_GetThreadMgr (MSCTF.@)
327 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
329 TRACE("\n");
330 *pptim = TlsGetValue(tlsIndex);
332 if (*pptim)
333 ITfThreadMgr_AddRef(*pptim);
335 return S_OK;
338 /***********************************************************************
339 * SetInputScope(MSCTF.@)
341 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
343 FIXME("STUB: %p %i\n",hwnd,inputscope);
344 return S_OK;
347 /***********************************************************************
348 * SetInputScopes(MSCTF.@)
350 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
351 UINT cInputScopes, WCHAR **ppszPhraseList,
352 UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
354 int i;
355 FIXME("STUB: %p ... %s %s\n",hwnd, debugstr_w(pszRegExp), debugstr_w(pszSRGS));
356 for (i = 0; i < cInputScopes; i++)
357 TRACE("\tScope[%i] = %i\n",i,pInputScopes[i]);
358 for (i = 0; i < cPhrases; i++)
359 TRACE("\tPhrase[%i] = %s\n",i,debugstr_w(ppszPhraseList[i]));
361 return S_OK;