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