msctf: Add stub implementation of ITfInputProcessorProfiles.
[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 typedef HRESULT (*LPFNCONSTRUCTOR)(IUnknown *pUnkOuter, IUnknown **ppvOut);
49
50 static const struct {
51     REFCLSID clsid;
52     LPFNCONSTRUCTOR ctor;
53 } ClassesTable[] = {
54     {&CLSID_TF_ThreadMgr, ThreadMgr_Constructor},
55     {&CLSID_TF_InputProcessorProfiles, InputProcessorProfiles_Constructor},
56     {NULL, NULL}
57 };
58
59 typedef struct tagClassFactory
60 {
61     const IClassFactoryVtbl *vtbl;
62     LONG   ref;
63     LPFNCONSTRUCTOR ctor;
64 } ClassFactory;
65
66 static void ClassFactory_Destructor(ClassFactory *This)
67 {
68     TRACE("Destroying class factory %p\n", This);
69     HeapFree(GetProcessHeap(),0,This);
70     MSCTF_refCount--;
71 }
72
73 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppvOut)
74 {
75     *ppvOut = NULL;
76     if (IsEqualIID(riid, &IID_IClassFactory) || IsEqualIID(riid, &IID_IUnknown)) {
77         IClassFactory_AddRef(iface);
78         *ppvOut = iface;
79         return S_OK;
80     }
81
82     WARN("Unknown interface %s\n", debugstr_guid(riid));
83     return E_NOINTERFACE;
84 }
85
86 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
87 {
88     ClassFactory *This = (ClassFactory *)iface;
89     return InterlockedIncrement(&This->ref);
90 }
91
92 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
93 {
94     ClassFactory *This = (ClassFactory *)iface;
95     ULONG ret = InterlockedDecrement(&This->ref);
96
97     if (ret == 0)
98         ClassFactory_Destructor(This);
99     return ret;
100 }
101
102 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *punkOuter, REFIID iid, LPVOID *ppvOut)
103 {
104     ClassFactory *This = (ClassFactory *)iface;
105     HRESULT ret;
106     IUnknown *obj;
107
108     TRACE("(%p, %p, %s, %p)\n", iface, punkOuter, debugstr_guid(iid), ppvOut);
109     ret = This->ctor(punkOuter, &obj);
110     if (FAILED(ret))
111         return ret;
112     ret = IUnknown_QueryInterface(obj, iid, ppvOut);
113     IUnknown_Release(obj);
114     return ret;
115 }
116
117 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL fLock)
118 {
119     ClassFactory *This = (ClassFactory *)iface;
120
121     TRACE("(%p)->(%x)\n", This, fLock);
122
123     if(fLock)
124         InterlockedIncrement(&MSCTF_refCount);
125     else
126         InterlockedDecrement(&MSCTF_refCount);
127
128     return S_OK;
129 }
130
131 static const IClassFactoryVtbl ClassFactoryVtbl = {
132     /* IUnknown */
133     ClassFactory_QueryInterface,
134     ClassFactory_AddRef,
135     ClassFactory_Release,
136
137     /* IClassFactory*/
138     ClassFactory_CreateInstance,
139     ClassFactory_LockServer
140 };
141
142 static HRESULT ClassFactory_Constructor(LPFNCONSTRUCTOR ctor, LPVOID *ppvOut)
143 {
144     ClassFactory *This = HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory));
145     This->vtbl = &ClassFactoryVtbl;
146     This->ref = 1;
147     This->ctor = ctor;
148     *ppvOut = (LPVOID)This;
149     TRACE("Created class factory %p\n", This);
150     MSCTF_refCount++;
151     return S_OK;
152 }
153
154 /*************************************************************************
155  * MSCTF DllMain
156  */
157 BOOL WINAPI DllMain(HINSTANCE hinst, DWORD fdwReason, LPVOID fImpLoad)
158 {
159     TRACE("%p 0x%x %p\n", hinst, fdwReason, fImpLoad);
160     switch (fdwReason)
161     {
162         case DLL_WINE_PREATTACH:
163             return FALSE;   /* prefer native version */
164         case DLL_PROCESS_ATTACH:
165             MSCTF_hinstance = hinst;
166             tlsIndex = TlsAlloc();
167             break;
168         case DLL_PROCESS_DETACH:
169             TlsFree(tlsIndex);
170             break;
171     }
172     return TRUE;
173 }
174
175 /*************************************************************************
176  *              DllCanUnloadNow (MSCTF.@)
177  */
178 HRESULT WINAPI DllCanUnloadNow(void)
179 {
180     return MSCTF_refCount ? S_FALSE : S_OK;
181 }
182
183 /***********************************************************************
184  *              DllGetClassObject (MSCTF.@)
185  */
186 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, LPVOID *ppvOut)
187 {
188     int i;
189
190     *ppvOut = NULL;
191     if (!IsEqualIID(iid, &IID_IUnknown) && !IsEqualIID(iid, &IID_IClassFactory))
192         return E_NOINTERFACE;
193
194     for (i = 0; ClassesTable[i].clsid != NULL; i++)
195         if (IsEqualCLSID(ClassesTable[i].clsid, clsid)) {
196             return ClassFactory_Constructor(ClassesTable[i].ctor, ppvOut);
197         }
198     FIXME("CLSID %s not supported\n", debugstr_guid(clsid));
199     return CLASS_E_CLASSNOTAVAILABLE;
200 }