msctf: ThreadMgr sink framework.
[wine] / dlls / msctf / threadmgr.c
1 /*
2  *  ITfThreadMgr implementation
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
25 #define COBJMACROS
26
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "shlwapi.h"
33 #include "winerror.h"
34 #include "objbase.h"
35
36 #include "wine/unicode.h"
37 #include "wine/list.h"
38
39 #include "msctf.h"
40 #include "msctf_internal.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
43
44 typedef struct tagThreadMgrSink {
45     struct list         entry;
46     union {
47         /* ThreadMgr Sinks */
48         IUnknown            *pIUnknown;
49         /* ITfActiveLanguageProfileNotifySink *pITfActiveLanguageProfileNotifySink; */
50         /* ITfDisplayAttributeNotifySink *pITfDisplayAttributeNotifySink; */
51         /* ITfKeyTraceEventSink *pITfKeyTraceEventSink; */
52         /* ITfPreservedKeyNotifySink *pITfPreservedKeyNotifySink; */
53         /* ITfThreadFocusSink *pITfThreadFocusSink; */
54         /* ITfThreadMgrEventSink *pITfThreadMgrEventSink; */
55     } interfaces;
56 } ThreadMgrSink;
57
58 typedef struct tagACLMulti {
59     const ITfThreadMgrVtbl *ThreadMgrVtbl;
60     const ITfSourceVtbl *SourceVtbl;
61     LONG refCount;
62
63     ITfDocumentMgr *focus;
64
65     /* kept as separate lists to reduce unnecessary iterations */
66     struct list     ActiveLanguageProfileNotifySink;
67     struct list     DisplayAttributeNotifySink;
68     struct list     KeyTraceEventSink;
69     struct list     PreservedKeyNotifySink;
70     struct list     ThreadFocusSink;
71     struct list     ThreadMgrEventSink;
72 } ThreadMgr;
73
74 static inline ThreadMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
75 {
76     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,SourceVtbl));
77 }
78
79 static void free_sink(ThreadMgrSink *sink)
80 {
81         IUnknown_Release(sink->interfaces.pIUnknown);
82         HeapFree(GetProcessHeap(),0,sink);
83 }
84
85 static void ThreadMgr_Destructor(ThreadMgr *This)
86 {
87     struct list *cursor, *cursor2;
88
89     TlsSetValue(tlsIndex,NULL);
90     TRACE("destroying %p\n", This);
91     if (This->focus)
92         ITfDocumentMgr_Release(This->focus);
93
94     /* free sinks */
95     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ActiveLanguageProfileNotifySink)
96     {
97         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
98         list_remove(cursor);
99         free_sink(sink);
100     }
101     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->DisplayAttributeNotifySink)
102     {
103         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
104         list_remove(cursor);
105         free_sink(sink);
106     }
107     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->KeyTraceEventSink)
108     {
109         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
110         list_remove(cursor);
111         free_sink(sink);
112     }
113     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->PreservedKeyNotifySink)
114     {
115         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
116         list_remove(cursor);
117         free_sink(sink);
118     }
119     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadFocusSink)
120     {
121         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
122         list_remove(cursor);
123         free_sink(sink);
124     }
125     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadMgrEventSink)
126     {
127         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
128         list_remove(cursor);
129         free_sink(sink);
130     }
131
132     HeapFree(GetProcessHeap(),0,This);
133 }
134
135 static HRESULT WINAPI ThreadMgr_QueryInterface(ITfThreadMgr *iface, REFIID iid, LPVOID *ppvOut)
136 {
137     ThreadMgr *This = (ThreadMgr *)iface;
138     *ppvOut = NULL;
139
140     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgr))
141     {
142         *ppvOut = This;
143     }
144     else if (IsEqualIID(iid, &IID_ITfSource))
145     {
146         *ppvOut = &This->SourceVtbl;
147     }
148
149     if (*ppvOut)
150     {
151         IUnknown_AddRef(iface);
152         return S_OK;
153     }
154
155     WARN("unsupported interface: %s\n", debugstr_guid(iid));
156     return E_NOINTERFACE;
157 }
158
159 static ULONG WINAPI ThreadMgr_AddRef(ITfThreadMgr *iface)
160 {
161     ThreadMgr *This = (ThreadMgr *)iface;
162     return InterlockedIncrement(&This->refCount);
163 }
164
165 static ULONG WINAPI ThreadMgr_Release(ITfThreadMgr *iface)
166 {
167     ThreadMgr *This = (ThreadMgr *)iface;
168     ULONG ret;
169
170     ret = InterlockedDecrement(&This->refCount);
171     if (ret == 0)
172         ThreadMgr_Destructor(This);
173     return ret;
174 }
175
176 /*****************************************************
177  * ITfThreadMgr functions
178  *****************************************************/
179
180 static HRESULT WINAPI ThreadMgr_fnActivate( ITfThreadMgr* iface, TfClientId *ptid)
181 {
182     ThreadMgr *This = (ThreadMgr *)iface;
183     FIXME("STUB:(%p)\n",This);
184     return E_NOTIMPL;
185 }
186
187 static HRESULT WINAPI ThreadMgr_fnDeactivate( ITfThreadMgr* iface)
188 {
189     ThreadMgr *This = (ThreadMgr *)iface;
190     FIXME("STUB:(%p)\n",This);
191     return E_NOTIMPL;
192 }
193
194 static HRESULT WINAPI ThreadMgr_CreateDocumentMgr( ITfThreadMgr* iface, ITfDocumentMgr
195 **ppdim)
196 {
197     TRACE("(%p)\n",iface);
198     return DocumentMgr_Constructor(ppdim);
199 }
200
201 static HRESULT WINAPI ThreadMgr_EnumDocumentMgrs( ITfThreadMgr* iface, IEnumTfDocumentMgrs
202 **ppEnum)
203 {
204     ThreadMgr *This = (ThreadMgr *)iface;
205     FIXME("STUB:(%p)\n",This);
206     return E_NOTIMPL;
207 }
208
209 static HRESULT WINAPI ThreadMgr_GetFocus( ITfThreadMgr* iface, ITfDocumentMgr
210 **ppdimFocus)
211 {
212     ThreadMgr *This = (ThreadMgr *)iface;
213     TRACE("(%p)\n",This);
214
215     if (!ppdimFocus)
216         return E_INVALIDARG;
217
218     *ppdimFocus = This->focus;
219
220     TRACE("->%p\n",This->focus);
221
222     if (This->focus == NULL)
223         return S_FALSE;
224
225     ITfDocumentMgr_AddRef(This->focus);
226
227     return S_OK;
228 }
229
230 static HRESULT WINAPI ThreadMgr_SetFocus( ITfThreadMgr* iface, ITfDocumentMgr *pdimFocus)
231 {
232     ITfDocumentMgr *check;
233     ThreadMgr *This = (ThreadMgr *)iface;
234
235     TRACE("(%p) %p\n",This,pdimFocus);
236
237     if (!pdimFocus || FAILED(IUnknown_QueryInterface(pdimFocus,&IID_ITfDocumentMgr,(LPVOID*) &check)))
238         return E_INVALIDARG;
239
240     if (This->focus)
241         ITfDocumentMgr_Release(This->focus);
242
243     This->focus = check;
244     return S_OK;
245 }
246
247 static HRESULT WINAPI ThreadMgr_AssociateFocus( ITfThreadMgr* iface, HWND hwnd,
248 ITfDocumentMgr *pdimNew, ITfDocumentMgr **ppdimPrev)
249 {
250     ThreadMgr *This = (ThreadMgr *)iface;
251     FIXME("STUB:(%p)\n",This);
252     return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI ThreadMgr_IsThreadFocus( ITfThreadMgr* iface, BOOL *pfThreadFocus)
256 {
257     ThreadMgr *This = (ThreadMgr *)iface;
258     FIXME("STUB:(%p)\n",This);
259     return E_NOTIMPL;
260 }
261
262 static HRESULT WINAPI ThreadMgr_GetFunctionProvider( ITfThreadMgr* iface, REFCLSID clsid,
263 ITfFunctionProvider **ppFuncProv)
264 {
265     ThreadMgr *This = (ThreadMgr *)iface;
266     FIXME("STUB:(%p)\n",This);
267     return E_NOTIMPL;
268 }
269
270 static HRESULT WINAPI ThreadMgr_EnumFunctionProviders( ITfThreadMgr* iface,
271 IEnumTfFunctionProviders **ppEnum)
272 {
273     ThreadMgr *This = (ThreadMgr *)iface;
274     FIXME("STUB:(%p)\n",This);
275     return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI ThreadMgr_GetGlobalCompartment( ITfThreadMgr* iface,
279 ITfCompartmentMgr **ppCompMgr)
280 {
281     ThreadMgr *This = (ThreadMgr *)iface;
282     FIXME("STUB:(%p)\n",This);
283     return E_NOTIMPL;
284 }
285
286 static const ITfThreadMgrVtbl ThreadMgr_ThreadMgrVtbl =
287 {
288     ThreadMgr_QueryInterface,
289     ThreadMgr_AddRef,
290     ThreadMgr_Release,
291
292     ThreadMgr_fnActivate,
293     ThreadMgr_fnDeactivate,
294     ThreadMgr_CreateDocumentMgr,
295     ThreadMgr_EnumDocumentMgrs,
296     ThreadMgr_GetFocus,
297     ThreadMgr_SetFocus,
298     ThreadMgr_AssociateFocus,
299     ThreadMgr_IsThreadFocus,
300     ThreadMgr_GetFunctionProvider,
301     ThreadMgr_EnumFunctionProviders,
302     ThreadMgr_GetGlobalCompartment
303 };
304
305
306 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
307 {
308     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
309     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
310 }
311
312 static ULONG WINAPI Source_AddRef(ITfSource *iface)
313 {
314     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
315     return ThreadMgr_AddRef((ITfThreadMgr*)This);
316 }
317
318 static ULONG WINAPI Source_Release(ITfSource *iface)
319 {
320     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
321     return ThreadMgr_Release((ITfThreadMgr *)This);
322 }
323
324 /*****************************************************
325  * ITfSource functions
326  *****************************************************/
327 static WINAPI HRESULT ThreadMgrSource_AdviseSink(ITfSource *iface,
328         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
329 {
330     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
331     FIXME("STUB:(%p)\n",This);
332     return E_NOTIMPL;
333 }
334
335 static WINAPI HRESULT ThreadMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
336 {
337     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
338     FIXME("STUB:(%p)\n",This);
339     return E_NOTIMPL;
340 }
341
342 static const ITfSourceVtbl ThreadMgr_SourceVtbl =
343 {
344     Source_QueryInterface,
345     Source_AddRef,
346     Source_Release,
347
348     ThreadMgrSource_AdviseSink,
349     ThreadMgrSource_UnadviseSink,
350 };
351
352 HRESULT ThreadMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
353 {
354     ThreadMgr *This;
355     if (pUnkOuter)
356         return CLASS_E_NOAGGREGATION;
357
358     /* Only 1 ThreadMgr is created per thread */
359     This = TlsGetValue(tlsIndex);
360     if (This)
361     {
362         ThreadMgr_AddRef((ITfThreadMgr*)This);
363         *ppOut = (IUnknown*)This;
364         return S_OK;
365     }
366
367     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgr));
368     if (This == NULL)
369         return E_OUTOFMEMORY;
370
371     This->ThreadMgrVtbl= &ThreadMgr_ThreadMgrVtbl;
372     This->SourceVtbl = &ThreadMgr_SourceVtbl;
373     This->refCount = 1;
374     TlsSetValue(tlsIndex,This);
375
376     list_init(&This->ActiveLanguageProfileNotifySink);
377     list_init(&This->DisplayAttributeNotifySink);
378     list_init(&This->KeyTraceEventSink);
379     list_init(&This->PreservedKeyNotifySink);
380     list_init(&This->ThreadFocusSink);
381     list_init(&This->ThreadMgrEventSink);
382
383     TRACE("returning %p\n", This);
384     *ppOut = (IUnknown *)This;
385     return S_OK;
386 }