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