wined3d: Pack hardcoded local constants in ARB.
[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 typedef struct
47 {
48     DWORD id;
49     DWORD magic;
50     LPVOID data;
51 } CookieInternal;
52
53 static CookieInternal *cookies;
54 static UINT id_last;
55 static UINT array_size;
56
57 DWORD tlsIndex = 0;
58
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};
60
61 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
62
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}
71 };
72
73 typedef struct tagClassFactory
74 {
75     const IClassFactoryVtbl *vtbl;
76     LONG   ref;
77     LPFNCONSTRUCTOR ctor;
78 } ClassFactory;
79
80 static void ClassFactory_Destructor(ClassFactory *This)
81 {
82     TRACE("Destroying class factory %p\n", This);
83     HeapFree(GetProcessHeap(),0,This);
84     MSCTF_refCount--;
85 }
86
87 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
88 {
89     *ppvOut = NULL;
90     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
91         IClassFactory_AddRef(iface);
92         *ppvOut = iface;
93         return S_OK;
94     }
95
96     WARN("Unknown interface %s\n", debugstr_guid(riid));
97     return E_NOINTERFACE;
98 }
99
100 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
101 {
102     ClassFactory *This = (ClassFactory *)iface;
103     return InterlockedIncrement(&This->ref);
104 }
105
106 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
107 {
108     ClassFactory *This = (ClassFactory *)iface;
109     ULONG ret = InterlockedDecrement(&This->ref);
110
111     if (ret == 0)
112         ClassFactory_Destructor(This);
113     return ret;
114 }
115
116 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
117 {
118     ClassFactory *This = (ClassFactory *)iface;
119     HRESULT ret;
120     IUnknown *obj;
121
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;
129 }
130
131 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
132 {
133     ClassFactory *This = (ClassFactory *)iface;
134
135     TRACE("(%p)->(%x)\n", This, fLock);
136
137     if(fLock)
138         InterlockedIncrement(&MSCTF_refCount);
139     else
140         InterlockedDecrement(&MSCTF_refCount);
141
142     return S_OK;
143 }
144
145 static const IClassFactoryVtbl ClassFactoryVtbl = {
146     /* IUnknown */
147     ClassFactory_QueryInterface,
148     ClassFactory_AddRef,
149     ClassFactory_Release,
150
151     /* IClassFactory*/
152     ClassFactory_CreateInstance,
153     ClassFactory_LockServer
154 };
155
156 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
157 {
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;
166 }
167
168 /*************************************************************************
169  * DWORD Cookie Management
170  */
171 DWORD generate_Cookie(DWORD magic, LPVOID data)
172 {
173     int i;
174
175     /* try to reuse IDs if possible */
176     for (i = 0; i < id_last; i++)
177         if (cookies[i].id == 0) break;
178
179     if (i == array_size)
180     {
181         if (!array_size)
182         {
183             cookies = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CookieInternal) * 10);
184             if (!cookies)
185             {
186                 ERR("Out of memory, Unable to alloc cookies array\n");
187                 return 0;
188             }
189             array_size = 10;
190         }
191         else
192         {
193             CookieInternal *new_cookies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cookies,
194                                                       sizeof(CookieInternal) * (array_size * 2));
195             if (!new_cookies)
196             {
197                 ERR("Out of memory, Unable to realloc cookies array\n");
198                 return 0;
199             }
200             cookies = new_cookies;
201             array_size *= 2;
202         }
203     }
204
205     cookies[i].id = i + 1; /* a return of 0 is used for failure */
206     cookies[i].magic = magic;
207     cookies[i].data = data;
208
209     if (i == id_last)
210         id_last++;
211
212     return cookies[i].id;
213 }
214
215 DWORD get_Cookie_magic(DWORD id)
216 {
217     UINT index = id - 1;
218
219     if (index >= id_last)
220         return 0;
221
222     if (cookies[index].id == 0)
223         return 0;
224
225     return cookies[index].magic;
226 }
227
228 LPVOID get_Cookie_data(DWORD id)
229 {
230     UINT index = id - 1;
231
232     if (index >= id_last)
233         return NULL;
234
235     if (cookies[index].id == 0)
236         return NULL;
237
238     return cookies[index].data;
239 }
240
241 LPVOID remove_Cookie(DWORD id)
242 {
243     UINT index = id - 1;
244
245     if (index >= id_last)
246         return NULL;
247
248     if (cookies[index].id == 0)
249         return NULL;
250
251     cookies[index].id = 0;
252     return cookies[index].data;
253 }
254
255 DWORD enumerate_Cookie(DWORD magic, DWORD *index)
256 {
257     int i;
258     for (i = *index; i < id_last; i++)
259         if (cookies[i].id != 0 && cookies[i].magic == magic)
260         {
261             *index = (i+1);
262             return cookies[i].id;
263         }
264     return 0x0;
265 }
266
267 /*************************************************************************
268  * MSCTF DllMain
269  */
270 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
271 {
272     TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
273     switch (fdwReason)
274     {
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;
284     }
285     return TRUE;
286 }
287
288 /*************************************************************************
289  *              DllCanUnloadNow (MSCTF.@)
290  */
291 HRESULT WINAPI DllCanUnloadNow(void)
292 {
293     return MSCTF_refCount ? S_FALSE : S_OK;
294 }
295
296 /***********************************************************************
297  *              DllGetClassObject (MSCTF.@)
298  */
299 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
300 {
301     int i;
302
303     *ppvOut = NULL;
304     if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
305         return E_NOINTERFACE;
306
307     for (i = 0; ClassesTable[i].clsid != NULL; i++)
308         if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
309             return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
310         }
311     FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
312     return CLASS_E_CLASSNOTAVAILABLE;
313 }
314
315 /***********************************************************************
316  *              TF_CreateThreadMgr (MSCTF.@)
317  */
318 HRESULT WINAPI TF_CreateThreadMgr(ITfThreadMgr **pptim)
319 {
320     TRACE("\n");
321     return ThreadMgr_Constructor(NULL,(IUnknown**)pptim);
322 }
323
324 /***********************************************************************
325  *              TF_GetThreadMgr (MSCTF.@)
326  */
327 HRESULT WINAPI TF_GetThreadMgr(ITfThreadMgr **pptim)
328 {
329     TRACE("\n");
330     *pptim = TlsGetValue(tlsIndex);
331
332     if (*pptim)
333         ITfThreadMgr_AddRef(*pptim);
334
335     return S_OK;
336 }
337
338 /***********************************************************************
339  *              SetInputScope(MSCTF.@)
340  */
341 HRESULT WINAPI SetInputScope(HWND hwnd, INT inputscope)
342 {
343     FIXME("STUB: %p %i\n",hwnd,inputscope);
344     return S_OK;
345 }
346
347 /***********************************************************************
348  *              SetInputScopes(MSCTF.@)
349  */
350 HRESULT WINAPI SetInputScopes(HWND hwnd, const INT *pInputScopes,
351                               UINT cInputScopes, WCHAR **ppszPhraseList,
352                               UINT cPhrases, WCHAR *pszRegExp, WCHAR *pszSRGS)
353 {
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]));
360
361     return S_OK;
362 }