2 * ITfThreadMgr implementation
4 * Copyright 2008 Aric Stewart, CodeWeavers
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.
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.
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
27 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
41 #include "msctf_internal.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
45 typedef struct tagThreadMgrSink {
50 /* ITfActiveLanguageProfileNotifySink *pITfActiveLanguageProfileNotifySink; */
51 /* ITfDisplayAttributeNotifySink *pITfDisplayAttributeNotifySink; */
52 /* ITfKeyTraceEventSink *pITfKeyTraceEventSink; */
53 /* ITfPreservedKeyNotifySink *pITfPreservedKeyNotifySink; */
54 /* ITfThreadFocusSink *pITfThreadFocusSink; */
55 ITfThreadMgrEventSink *pITfThreadMgrEventSink;
59 typedef struct tagACLMulti {
60 const ITfThreadMgrVtbl *ThreadMgrVtbl;
61 const ITfSourceVtbl *SourceVtbl;
64 const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl; /* internal */
66 ITfDocumentMgr *focus;
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;
77 static inline ThreadMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
79 return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,SourceVtbl));
82 static inline ThreadMgr *impl_from_ITfThreadMgrEventSink(ITfThreadMgrEventSink *iface)
84 return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,ThreadMgrEventSinkVtbl));
87 static void free_sink(ThreadMgrSink *sink)
89 IUnknown_Release(sink->interfaces.pIUnknown);
90 HeapFree(GetProcessHeap(),0,sink);
93 static void ThreadMgr_Destructor(ThreadMgr *This)
95 struct list *cursor, *cursor2;
97 TlsSetValue(tlsIndex,NULL);
98 TRACE("destroying %p\n", This);
100 ITfDocumentMgr_Release(This->focus);
103 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ActiveLanguageProfileNotifySink)
105 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
109 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->DisplayAttributeNotifySink)
111 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
115 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->KeyTraceEventSink)
117 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
121 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->PreservedKeyNotifySink)
123 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
127 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadFocusSink)
129 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
133 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadMgrEventSink)
135 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
140 HeapFree(GetProcessHeap(),0,This);
143 static HRESULT WINAPI ThreadMgr_QueryInterface(ITfThreadMgr *iface, REFIID iid, LPVOID *ppvOut)
145 ThreadMgr *This = (ThreadMgr *)iface;
148 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgr))
152 else if (IsEqualIID(iid, &IID_ITfSource))
154 *ppvOut = &This->SourceVtbl;
159 IUnknown_AddRef(iface);
163 WARN("unsupported interface: %s\n", debugstr_guid(iid));
164 return E_NOINTERFACE;
167 static ULONG WINAPI ThreadMgr_AddRef(ITfThreadMgr *iface)
169 ThreadMgr *This = (ThreadMgr *)iface;
170 return InterlockedIncrement(&This->refCount);
173 static ULONG WINAPI ThreadMgr_Release(ITfThreadMgr *iface)
175 ThreadMgr *This = (ThreadMgr *)iface;
178 ret = InterlockedDecrement(&This->refCount);
180 ThreadMgr_Destructor(This);
184 /*****************************************************
185 * ITfThreadMgr functions
186 *****************************************************/
188 static HRESULT WINAPI ThreadMgr_fnActivate( ITfThreadMgr* iface, TfClientId *ptid)
190 ThreadMgr *This = (ThreadMgr *)iface;
191 FIXME("STUB:(%p)\n",This);
195 static HRESULT WINAPI ThreadMgr_fnDeactivate( ITfThreadMgr* iface)
197 ThreadMgr *This = (ThreadMgr *)iface;
198 FIXME("STUB:(%p)\n",This);
202 static HRESULT WINAPI ThreadMgr_CreateDocumentMgr( ITfThreadMgr* iface, ITfDocumentMgr
205 ThreadMgr *This = (ThreadMgr *)iface;
206 TRACE("(%p)\n",iface);
207 return DocumentMgr_Constructor((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, ppdim);
210 static HRESULT WINAPI ThreadMgr_EnumDocumentMgrs( ITfThreadMgr* iface, IEnumTfDocumentMgrs
213 ThreadMgr *This = (ThreadMgr *)iface;
214 FIXME("STUB:(%p)\n",This);
218 static HRESULT WINAPI ThreadMgr_GetFocus( ITfThreadMgr* iface, ITfDocumentMgr
221 ThreadMgr *This = (ThreadMgr *)iface;
222 TRACE("(%p)\n",This);
227 *ppdimFocus = This->focus;
229 TRACE("->%p\n",This->focus);
231 if (This->focus == NULL)
234 ITfDocumentMgr_AddRef(This->focus);
239 static HRESULT WINAPI ThreadMgr_SetFocus( ITfThreadMgr* iface, ITfDocumentMgr *pdimFocus)
241 ITfDocumentMgr *check;
242 ThreadMgr *This = (ThreadMgr *)iface;
244 TRACE("(%p) %p\n",This,pdimFocus);
246 if (!pdimFocus || FAILED(IUnknown_QueryInterface(pdimFocus,&IID_ITfDocumentMgr,(LPVOID*) &check)))
249 ITfThreadMgrEventSink_OnSetFocus((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, This->focus, check);
252 ITfDocumentMgr_Release(This->focus);
258 static HRESULT WINAPI ThreadMgr_AssociateFocus( ITfThreadMgr* iface, HWND hwnd,
259 ITfDocumentMgr *pdimNew, ITfDocumentMgr **ppdimPrev)
261 ThreadMgr *This = (ThreadMgr *)iface;
262 FIXME("STUB:(%p)\n",This);
266 static HRESULT WINAPI ThreadMgr_IsThreadFocus( ITfThreadMgr* iface, BOOL *pfThreadFocus)
268 ThreadMgr *This = (ThreadMgr *)iface;
269 FIXME("STUB:(%p)\n",This);
273 static HRESULT WINAPI ThreadMgr_GetFunctionProvider( ITfThreadMgr* iface, REFCLSID clsid,
274 ITfFunctionProvider **ppFuncProv)
276 ThreadMgr *This = (ThreadMgr *)iface;
277 FIXME("STUB:(%p)\n",This);
281 static HRESULT WINAPI ThreadMgr_EnumFunctionProviders( ITfThreadMgr* iface,
282 IEnumTfFunctionProviders **ppEnum)
284 ThreadMgr *This = (ThreadMgr *)iface;
285 FIXME("STUB:(%p)\n",This);
289 static HRESULT WINAPI ThreadMgr_GetGlobalCompartment( ITfThreadMgr* iface,
290 ITfCompartmentMgr **ppCompMgr)
292 ThreadMgr *This = (ThreadMgr *)iface;
293 FIXME("STUB:(%p)\n",This);
297 static const ITfThreadMgrVtbl ThreadMgr_ThreadMgrVtbl =
299 ThreadMgr_QueryInterface,
303 ThreadMgr_fnActivate,
304 ThreadMgr_fnDeactivate,
305 ThreadMgr_CreateDocumentMgr,
306 ThreadMgr_EnumDocumentMgrs,
309 ThreadMgr_AssociateFocus,
310 ThreadMgr_IsThreadFocus,
311 ThreadMgr_GetFunctionProvider,
312 ThreadMgr_EnumFunctionProviders,
313 ThreadMgr_GetGlobalCompartment
317 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
319 ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
320 return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
323 static ULONG WINAPI Source_AddRef(ITfSource *iface)
325 ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
326 return ThreadMgr_AddRef((ITfThreadMgr*)This);
329 static ULONG WINAPI Source_Release(ITfSource *iface)
331 ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
332 return ThreadMgr_Release((ITfThreadMgr *)This);
335 /*****************************************************
336 * ITfSource functions
337 *****************************************************/
338 static WINAPI HRESULT ThreadMgrSource_AdviseSink(ITfSource *iface,
339 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
342 ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
344 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
346 if (!riid || !punk || !pdwCookie)
349 if (IsEqualIID(riid, &IID_ITfThreadMgrEventSink))
351 tms = HeapAlloc(GetProcessHeap(),0,sizeof(ThreadMgrSink));
353 return E_OUTOFMEMORY;
354 if (!SUCCEEDED(IUnknown_QueryInterface(punk, riid, (LPVOID*)&tms->interfaces.pITfThreadMgrEventSink)))
356 HeapFree(GetProcessHeap(),0,tms);
357 return CONNECT_E_CANNOTCONNECT;
359 list_add_head(&This->ThreadMgrEventSink,&tms->entry);
360 *pdwCookie = (DWORD)tms;
364 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
368 TRACE("cookie %x\n",*pdwCookie);
373 static WINAPI HRESULT ThreadMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
375 ThreadMgrSink *sink = (ThreadMgrSink*)pdwCookie;
376 ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
377 TRACE("(%p) %x\n",This,pdwCookie);
379 list_remove(&sink->entry);
385 static const ITfSourceVtbl ThreadMgr_SourceVtbl =
387 Source_QueryInterface,
391 ThreadMgrSource_AdviseSink,
392 ThreadMgrSource_UnadviseSink,
395 /*****************************************************
396 * ITfThreadMgrEventSink functions (internal)
397 *****************************************************/
398 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
400 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
401 return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
404 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
406 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
407 return ThreadMgr_AddRef((ITfThreadMgr*)This);
410 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
412 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
413 return ThreadMgr_Release((ITfThreadMgr *)This);
417 static WINAPI HRESULT ThreadMgrEventSink_OnInitDocumentMgr(
418 ITfThreadMgrEventSink *iface,ITfDocumentMgr *pdim)
421 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
423 TRACE("(%p) %p\n",This,pdim);
425 LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
427 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
428 ITfThreadMgrEventSink_OnInitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
434 static WINAPI HRESULT ThreadMgrEventSink_OnUninitDocumentMgr(
435 ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdim)
438 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
440 TRACE("(%p) %p\n",This,pdim);
442 LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
444 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
445 ITfThreadMgrEventSink_OnUninitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
451 static WINAPI HRESULT ThreadMgrEventSink_OnSetFocus(
452 ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdimFocus,
453 ITfDocumentMgr *pdimPrevFocus)
456 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
458 TRACE("(%p) %p %p\n",This,pdimFocus, pdimPrevFocus);
460 LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
462 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
463 ITfThreadMgrEventSink_OnSetFocus(sink->interfaces.pITfThreadMgrEventSink, pdimFocus, pdimPrevFocus);
469 static WINAPI HRESULT ThreadMgrEventSink_OnPushContext(
470 ITfThreadMgrEventSink *iface, ITfContext *pic)
473 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
475 TRACE("(%p) %p\n",This,pic);
477 LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
479 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
480 ITfThreadMgrEventSink_OnPushContext(sink->interfaces.pITfThreadMgrEventSink,pic);
486 static WINAPI HRESULT ThreadMgrEventSink_OnPopContext(
487 ITfThreadMgrEventSink *iface, ITfContext *pic)
490 ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
492 TRACE("(%p) %p\n",This,pic);
494 LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
496 ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
497 ITfThreadMgrEventSink_OnPopContext(sink->interfaces.pITfThreadMgrEventSink,pic);
503 static const ITfThreadMgrEventSinkVtbl ThreadMgr_ThreadMgrEventSinkVtbl =
505 ThreadMgrEventSink_QueryInterface,
506 ThreadMgrEventSink_AddRef,
507 ThreadMgrEventSink_Release,
509 ThreadMgrEventSink_OnInitDocumentMgr,
510 ThreadMgrEventSink_OnUninitDocumentMgr,
511 ThreadMgrEventSink_OnSetFocus,
512 ThreadMgrEventSink_OnPushContext,
513 ThreadMgrEventSink_OnPopContext
516 HRESULT ThreadMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
520 return CLASS_E_NOAGGREGATION;
522 /* Only 1 ThreadMgr is created per thread */
523 This = TlsGetValue(tlsIndex);
526 ThreadMgr_AddRef((ITfThreadMgr*)This);
527 *ppOut = (IUnknown*)This;
531 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgr));
533 return E_OUTOFMEMORY;
535 This->ThreadMgrVtbl= &ThreadMgr_ThreadMgrVtbl;
536 This->SourceVtbl = &ThreadMgr_SourceVtbl;
537 This->ThreadMgrEventSinkVtbl = &ThreadMgr_ThreadMgrEventSinkVtbl;
539 TlsSetValue(tlsIndex,This);
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);
548 TRACE("returning %p\n", This);
549 *ppOut = (IUnknown *)This;