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