msctf: Implement ThreadMgr ITfSource::UnadviseSink.
[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     const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl; /* internal */
65
66     ITfDocumentMgr *focus;
67
68     /* kept as separate lists to reduce unnecessary iterations */
69     struct list     ActiveLanguageProfileNotifySink;
70     struct list     DisplayAttributeNotifySink;
71     struct list     KeyTraceEventSink;
72     struct list     PreservedKeyNotifySink;
73     struct list     ThreadFocusSink;
74     struct list     ThreadMgrEventSink;
75 } ThreadMgr;
76
77 static inline ThreadMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
78 {
79     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,SourceVtbl));
80 }
81
82 static inline ThreadMgr *impl_from_ITfThreadMgrEventSink(ITfThreadMgrEventSink *iface)
83 {
84     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,ThreadMgrEventSinkVtbl));
85 }
86
87 static void free_sink(ThreadMgrSink *sink)
88 {
89         IUnknown_Release(sink->interfaces.pIUnknown);
90         HeapFree(GetProcessHeap(),0,sink);
91 }
92
93 static void ThreadMgr_Destructor(ThreadMgr *This)
94 {
95     struct list *cursor, *cursor2;
96
97     TlsSetValue(tlsIndex,NULL);
98     TRACE("destroying %p\n", This);
99     if (This->focus)
100         ITfDocumentMgr_Release(This->focus);
101
102     /* free sinks */
103     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ActiveLanguageProfileNotifySink)
104     {
105         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
106         list_remove(cursor);
107         free_sink(sink);
108     }
109     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->DisplayAttributeNotifySink)
110     {
111         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
112         list_remove(cursor);
113         free_sink(sink);
114     }
115     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->KeyTraceEventSink)
116     {
117         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
118         list_remove(cursor);
119         free_sink(sink);
120     }
121     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->PreservedKeyNotifySink)
122     {
123         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
124         list_remove(cursor);
125         free_sink(sink);
126     }
127     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadFocusSink)
128     {
129         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
130         list_remove(cursor);
131         free_sink(sink);
132     }
133     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadMgrEventSink)
134     {
135         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
136         list_remove(cursor);
137         free_sink(sink);
138     }
139
140     HeapFree(GetProcessHeap(),0,This);
141 }
142
143 static HRESULT WINAPI ThreadMgr_QueryInterface(ITfThreadMgr *iface, REFIID iid, LPVOID *ppvOut)
144 {
145     ThreadMgr *This = (ThreadMgr *)iface;
146     *ppvOut = NULL;
147
148     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgr))
149     {
150         *ppvOut = This;
151     }
152     else if (IsEqualIID(iid, &IID_ITfSource))
153     {
154         *ppvOut = &This->SourceVtbl;
155     }
156
157     if (*ppvOut)
158     {
159         IUnknown_AddRef(iface);
160         return S_OK;
161     }
162
163     WARN("unsupported interface: %s\n", debugstr_guid(iid));
164     return E_NOINTERFACE;
165 }
166
167 static ULONG WINAPI ThreadMgr_AddRef(ITfThreadMgr *iface)
168 {
169     ThreadMgr *This = (ThreadMgr *)iface;
170     return InterlockedIncrement(&This->refCount);
171 }
172
173 static ULONG WINAPI ThreadMgr_Release(ITfThreadMgr *iface)
174 {
175     ThreadMgr *This = (ThreadMgr *)iface;
176     ULONG ret;
177
178     ret = InterlockedDecrement(&This->refCount);
179     if (ret == 0)
180         ThreadMgr_Destructor(This);
181     return ret;
182 }
183
184 /*****************************************************
185  * ITfThreadMgr functions
186  *****************************************************/
187
188 static HRESULT WINAPI ThreadMgr_fnActivate( ITfThreadMgr* iface, TfClientId *ptid)
189 {
190     ThreadMgr *This = (ThreadMgr *)iface;
191     FIXME("STUB:(%p)\n",This);
192     return E_NOTIMPL;
193 }
194
195 static HRESULT WINAPI ThreadMgr_fnDeactivate( ITfThreadMgr* iface)
196 {
197     ThreadMgr *This = (ThreadMgr *)iface;
198     FIXME("STUB:(%p)\n",This);
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI ThreadMgr_CreateDocumentMgr( ITfThreadMgr* iface, ITfDocumentMgr
203 **ppdim)
204 {
205     ThreadMgr *This = (ThreadMgr *)iface;
206     TRACE("(%p)\n",iface);
207     return DocumentMgr_Constructor((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, ppdim);
208 }
209
210 static HRESULT WINAPI ThreadMgr_EnumDocumentMgrs( ITfThreadMgr* iface, IEnumTfDocumentMgrs
211 **ppEnum)
212 {
213     ThreadMgr *This = (ThreadMgr *)iface;
214     FIXME("STUB:(%p)\n",This);
215     return E_NOTIMPL;
216 }
217
218 static HRESULT WINAPI ThreadMgr_GetFocus( ITfThreadMgr* iface, ITfDocumentMgr
219 **ppdimFocus)
220 {
221     ThreadMgr *This = (ThreadMgr *)iface;
222     TRACE("(%p)\n",This);
223
224     if (!ppdimFocus)
225         return E_INVALIDARG;
226
227     *ppdimFocus = This->focus;
228
229     TRACE("->%p\n",This->focus);
230
231     if (This->focus == NULL)
232         return S_FALSE;
233
234     ITfDocumentMgr_AddRef(This->focus);
235
236     return S_OK;
237 }
238
239 static HRESULT WINAPI ThreadMgr_SetFocus( ITfThreadMgr* iface, ITfDocumentMgr *pdimFocus)
240 {
241     ITfDocumentMgr *check;
242     ThreadMgr *This = (ThreadMgr *)iface;
243
244     TRACE("(%p) %p\n",This,pdimFocus);
245
246     if (!pdimFocus || FAILED(IUnknown_QueryInterface(pdimFocus,&IID_ITfDocumentMgr,(LPVOID*) &check)))
247         return E_INVALIDARG;
248
249     ITfThreadMgrEventSink_OnSetFocus((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, This->focus, check);
250
251     if (This->focus)
252         ITfDocumentMgr_Release(This->focus);
253
254     This->focus = check;
255     return S_OK;
256 }
257
258 static HRESULT WINAPI ThreadMgr_AssociateFocus( ITfThreadMgr* iface, HWND hwnd,
259 ITfDocumentMgr *pdimNew, ITfDocumentMgr **ppdimPrev)
260 {
261     ThreadMgr *This = (ThreadMgr *)iface;
262     FIXME("STUB:(%p)\n",This);
263     return E_NOTIMPL;
264 }
265
266 static HRESULT WINAPI ThreadMgr_IsThreadFocus( ITfThreadMgr* iface, BOOL *pfThreadFocus)
267 {
268     ThreadMgr *This = (ThreadMgr *)iface;
269     FIXME("STUB:(%p)\n",This);
270     return E_NOTIMPL;
271 }
272
273 static HRESULT WINAPI ThreadMgr_GetFunctionProvider( ITfThreadMgr* iface, REFCLSID clsid,
274 ITfFunctionProvider **ppFuncProv)
275 {
276     ThreadMgr *This = (ThreadMgr *)iface;
277     FIXME("STUB:(%p)\n",This);
278     return E_NOTIMPL;
279 }
280
281 static HRESULT WINAPI ThreadMgr_EnumFunctionProviders( ITfThreadMgr* iface,
282 IEnumTfFunctionProviders **ppEnum)
283 {
284     ThreadMgr *This = (ThreadMgr *)iface;
285     FIXME("STUB:(%p)\n",This);
286     return E_NOTIMPL;
287 }
288
289 static HRESULT WINAPI ThreadMgr_GetGlobalCompartment( ITfThreadMgr* iface,
290 ITfCompartmentMgr **ppCompMgr)
291 {
292     ThreadMgr *This = (ThreadMgr *)iface;
293     FIXME("STUB:(%p)\n",This);
294     return E_NOTIMPL;
295 }
296
297 static const ITfThreadMgrVtbl ThreadMgr_ThreadMgrVtbl =
298 {
299     ThreadMgr_QueryInterface,
300     ThreadMgr_AddRef,
301     ThreadMgr_Release,
302
303     ThreadMgr_fnActivate,
304     ThreadMgr_fnDeactivate,
305     ThreadMgr_CreateDocumentMgr,
306     ThreadMgr_EnumDocumentMgrs,
307     ThreadMgr_GetFocus,
308     ThreadMgr_SetFocus,
309     ThreadMgr_AssociateFocus,
310     ThreadMgr_IsThreadFocus,
311     ThreadMgr_GetFunctionProvider,
312     ThreadMgr_EnumFunctionProviders,
313     ThreadMgr_GetGlobalCompartment
314 };
315
316
317 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
318 {
319     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
320     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
321 }
322
323 static ULONG WINAPI Source_AddRef(ITfSource *iface)
324 {
325     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
326     return ThreadMgr_AddRef((ITfThreadMgr*)This);
327 }
328
329 static ULONG WINAPI Source_Release(ITfSource *iface)
330 {
331     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
332     return ThreadMgr_Release((ITfThreadMgr *)This);
333 }
334
335 /*****************************************************
336  * ITfSource functions
337  *****************************************************/
338 static WINAPI HRESULT ThreadMgrSource_AdviseSink(ITfSource *iface,
339         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
340 {
341     ThreadMgrSink *tms;
342     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
343
344     TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
345
346     if (!riid || !punk || !pdwCookie)
347         return E_INVALIDARG;
348
349     if (IsEqualIID(riid, &IID_ITfThreadMgrEventSink))
350     {
351         tms = HeapAlloc(GetProcessHeap(),0,sizeof(ThreadMgrSink));
352         if (!tms)
353             return E_OUTOFMEMORY;
354         if (!SUCCEEDED(IUnknown_QueryInterface(punk, riid, (LPVOID*)&tms->interfaces.pITfThreadMgrEventSink)))
355         {
356             HeapFree(GetProcessHeap(),0,tms);
357             return CONNECT_E_CANNOTCONNECT;
358         }
359         list_add_head(&This->ThreadMgrEventSink,&tms->entry);
360         *pdwCookie = (DWORD)tms;
361     }
362     else
363     {
364         FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
365         return E_NOTIMPL;
366     }
367
368     TRACE("cookie %x\n",*pdwCookie);
369
370     return S_OK;
371 }
372
373 static WINAPI HRESULT ThreadMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
374 {
375     ThreadMgrSink *sink = (ThreadMgrSink*)pdwCookie;
376     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
377     TRACE("(%p) %x\n",This,pdwCookie);
378
379     list_remove(&sink->entry);
380     free_sink(sink);
381
382     return S_OK;
383 }
384
385 static const ITfSourceVtbl ThreadMgr_SourceVtbl =
386 {
387     Source_QueryInterface,
388     Source_AddRef,
389     Source_Release,
390
391     ThreadMgrSource_AdviseSink,
392     ThreadMgrSource_UnadviseSink,
393 };
394
395 /*****************************************************
396  * ITfThreadMgrEventSink functions  (internal)
397  *****************************************************/
398 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
399 {
400     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
401     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
402 }
403
404 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
405 {
406     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
407     return ThreadMgr_AddRef((ITfThreadMgr*)This);
408 }
409
410 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
411 {
412     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
413     return ThreadMgr_Release((ITfThreadMgr *)This);
414 }
415
416
417 static WINAPI HRESULT ThreadMgrEventSink_OnInitDocumentMgr(
418         ITfThreadMgrEventSink *iface,ITfDocumentMgr *pdim)
419 {
420     struct list *cursor;
421     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
422
423     TRACE("(%p) %p\n",This,pdim);
424
425     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
426     {
427         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
428         ITfThreadMgrEventSink_OnInitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
429     }
430
431     return S_OK;
432 }
433
434 static WINAPI HRESULT ThreadMgrEventSink_OnUninitDocumentMgr(
435         ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdim)
436 {
437     struct list *cursor;
438     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
439
440     TRACE("(%p) %p\n",This,pdim);
441
442     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
443     {
444         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
445         ITfThreadMgrEventSink_OnUninitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
446     }
447
448     return S_OK;
449 }
450
451 static WINAPI HRESULT ThreadMgrEventSink_OnSetFocus(
452         ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdimFocus,
453         ITfDocumentMgr *pdimPrevFocus)
454 {
455     struct list *cursor;
456     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
457
458     TRACE("(%p) %p %p\n",This,pdimFocus, pdimPrevFocus);
459
460     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
461     {
462         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
463         ITfThreadMgrEventSink_OnSetFocus(sink->interfaces.pITfThreadMgrEventSink, pdimFocus, pdimPrevFocus);
464     }
465
466     return S_OK;
467 }
468
469 static WINAPI HRESULT ThreadMgrEventSink_OnPushContext(
470         ITfThreadMgrEventSink *iface, ITfContext *pic)
471 {
472     struct list *cursor;
473     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
474
475     TRACE("(%p) %p\n",This,pic);
476
477     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
478     {
479         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
480         ITfThreadMgrEventSink_OnPushContext(sink->interfaces.pITfThreadMgrEventSink,pic);
481     }
482
483     return S_OK;
484 }
485
486 static WINAPI HRESULT ThreadMgrEventSink_OnPopContext(
487         ITfThreadMgrEventSink *iface, ITfContext *pic)
488 {
489     struct list *cursor;
490     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
491
492     TRACE("(%p) %p\n",This,pic);
493
494     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
495     {
496         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
497         ITfThreadMgrEventSink_OnPopContext(sink->interfaces.pITfThreadMgrEventSink,pic);
498     }
499
500     return S_OK;
501 }
502
503 static const ITfThreadMgrEventSinkVtbl ThreadMgr_ThreadMgrEventSinkVtbl =
504 {
505     ThreadMgrEventSink_QueryInterface,
506     ThreadMgrEventSink_AddRef,
507     ThreadMgrEventSink_Release,
508
509     ThreadMgrEventSink_OnInitDocumentMgr,
510     ThreadMgrEventSink_OnUninitDocumentMgr,
511     ThreadMgrEventSink_OnSetFocus,
512     ThreadMgrEventSink_OnPushContext,
513     ThreadMgrEventSink_OnPopContext
514 };
515
516 HRESULT ThreadMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
517 {
518     ThreadMgr *This;
519     if (pUnkOuter)
520         return CLASS_E_NOAGGREGATION;
521
522     /* Only 1 ThreadMgr is created per thread */
523     This = TlsGetValue(tlsIndex);
524     if (This)
525     {
526         ThreadMgr_AddRef((ITfThreadMgr*)This);
527         *ppOut = (IUnknown*)This;
528         return S_OK;
529     }
530
531     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgr));
532     if (This == NULL)
533         return E_OUTOFMEMORY;
534
535     This->ThreadMgrVtbl= &ThreadMgr_ThreadMgrVtbl;
536     This->SourceVtbl = &ThreadMgr_SourceVtbl;
537     This->ThreadMgrEventSinkVtbl = &ThreadMgr_ThreadMgrEventSinkVtbl;
538     This->refCount = 1;
539     TlsSetValue(tlsIndex,This);
540
541     list_init(&This->ActiveLanguageProfileNotifySink);
542     list_init(&This->DisplayAttributeNotifySink);
543     list_init(&This->KeyTraceEventSink);
544     list_init(&This->PreservedKeyNotifySink);
545     list_init(&This->ThreadFocusSink);
546     list_init(&This->ThreadMgrEventSink);
547
548     TRACE("returning %p\n", This);
549     *ppOut = (IUnknown *)This;
550     return S_OK;
551 }