msctf: Implement ITfKeystrokeMgr::IsPreservedKey.
[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 tagPreservedKey
60 {
61     struct list     entry;
62     GUID            guid;
63     TF_PRESERVEDKEY prekey;
64     LPWSTR          description;
65     TfClientId      tid;
66 } PreservedKey;
67
68 typedef struct tagACLMulti {
69     const ITfThreadMgrVtbl *ThreadMgrVtbl;
70     const ITfSourceVtbl *SourceVtbl;
71     const ITfKeystrokeMgrVtbl *KeystrokeMgrVtbl;
72     const ITfMessagePumpVtbl *MessagePumpVtbl;
73     const ITfClientIdVtbl *ClientIdVtbl;
74     LONG refCount;
75
76     const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl; /* internal */
77
78     ITfDocumentMgr *focus;
79
80     struct list CurrentPreservedKeys;
81
82     /* kept as separate lists to reduce unnecessary iterations */
83     struct list     ActiveLanguageProfileNotifySink;
84     struct list     DisplayAttributeNotifySink;
85     struct list     KeyTraceEventSink;
86     struct list     PreservedKeyNotifySink;
87     struct list     ThreadFocusSink;
88     struct list     ThreadMgrEventSink;
89 } ThreadMgr;
90
91 static inline ThreadMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
92 {
93     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,SourceVtbl));
94 }
95
96 static inline ThreadMgr *impl_from_ITfKeystrokeMgrVtbl(ITfKeystrokeMgr *iface)
97 {
98     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,KeystrokeMgrVtbl));
99 }
100
101 static inline ThreadMgr *impl_from_ITfMessagePumpVtbl(ITfMessagePump *iface)
102 {
103     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,MessagePumpVtbl));
104 }
105
106 static inline ThreadMgr *impl_from_ITfClientIdVtbl(ITfClientId *iface)
107 {
108     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,ClientIdVtbl));
109 }
110
111 static inline ThreadMgr *impl_from_ITfThreadMgrEventSink(ITfThreadMgrEventSink *iface)
112 {
113     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,ThreadMgrEventSinkVtbl));
114 }
115
116 static void free_sink(ThreadMgrSink *sink)
117 {
118         IUnknown_Release(sink->interfaces.pIUnknown);
119         HeapFree(GetProcessHeap(),0,sink);
120 }
121
122 static void ThreadMgr_Destructor(ThreadMgr *This)
123 {
124     struct list *cursor, *cursor2;
125
126     TlsSetValue(tlsIndex,NULL);
127     TRACE("destroying %p\n", This);
128     if (This->focus)
129         ITfDocumentMgr_Release(This->focus);
130
131     /* free sinks */
132     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ActiveLanguageProfileNotifySink)
133     {
134         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
135         list_remove(cursor);
136         free_sink(sink);
137     }
138     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->DisplayAttributeNotifySink)
139     {
140         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
141         list_remove(cursor);
142         free_sink(sink);
143     }
144     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->KeyTraceEventSink)
145     {
146         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
147         list_remove(cursor);
148         free_sink(sink);
149     }
150     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->PreservedKeyNotifySink)
151     {
152         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
153         list_remove(cursor);
154         free_sink(sink);
155     }
156     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadFocusSink)
157     {
158         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
159         list_remove(cursor);
160         free_sink(sink);
161     }
162     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadMgrEventSink)
163     {
164         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
165         list_remove(cursor);
166         free_sink(sink);
167     }
168
169     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->CurrentPreservedKeys)
170     {
171         PreservedKey* key = LIST_ENTRY(cursor,PreservedKey,entry);
172         list_remove(cursor);
173         HeapFree(GetProcessHeap(),0,key->description);
174         HeapFree(GetProcessHeap(),0,key);
175     }
176
177     HeapFree(GetProcessHeap(),0,This);
178 }
179
180 static HRESULT WINAPI ThreadMgr_QueryInterface(ITfThreadMgr *iface, REFIID iid, LPVOID *ppvOut)
181 {
182     ThreadMgr *This = (ThreadMgr *)iface;
183     *ppvOut = NULL;
184
185     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgr))
186     {
187         *ppvOut = This;
188     }
189     else if (IsEqualIID(iid, &IID_ITfSource))
190     {
191         *ppvOut = &This->SourceVtbl;
192     }
193     else if (IsEqualIID(iid, &IID_ITfKeystrokeMgr))
194     {
195         *ppvOut = &This->KeystrokeMgrVtbl;
196     }
197     else if (IsEqualIID(iid, &IID_ITfMessagePump))
198     {
199         *ppvOut = &This->MessagePumpVtbl;
200     }
201     else if (IsEqualIID(iid, &IID_ITfClientId))
202     {
203         *ppvOut = &This->ClientIdVtbl;
204     }
205
206     if (*ppvOut)
207     {
208         IUnknown_AddRef(iface);
209         return S_OK;
210     }
211
212     WARN("unsupported interface: %s\n", debugstr_guid(iid));
213     return E_NOINTERFACE;
214 }
215
216 static ULONG WINAPI ThreadMgr_AddRef(ITfThreadMgr *iface)
217 {
218     ThreadMgr *This = (ThreadMgr *)iface;
219     return InterlockedIncrement(&This->refCount);
220 }
221
222 static ULONG WINAPI ThreadMgr_Release(ITfThreadMgr *iface)
223 {
224     ThreadMgr *This = (ThreadMgr *)iface;
225     ULONG ret;
226
227     ret = InterlockedDecrement(&This->refCount);
228     if (ret == 0)
229         ThreadMgr_Destructor(This);
230     return ret;
231 }
232
233 /*****************************************************
234  * ITfThreadMgr functions
235  *****************************************************/
236
237 static HRESULT WINAPI ThreadMgr_fnActivate( ITfThreadMgr* iface, TfClientId *ptid)
238 {
239     ThreadMgr *This = (ThreadMgr *)iface;
240     FIXME("STUB:(%p)\n",This);
241     return E_NOTIMPL;
242 }
243
244 static HRESULT WINAPI ThreadMgr_fnDeactivate( ITfThreadMgr* iface)
245 {
246     ThreadMgr *This = (ThreadMgr *)iface;
247     FIXME("STUB:(%p)\n",This);
248
249     if (This->focus)
250     {
251         ITfThreadMgrEventSink_OnSetFocus((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, 0, This->focus);
252         ITfDocumentMgr_Release(This->focus);
253         This->focus = 0;
254     }
255
256     return E_NOTIMPL;
257 }
258
259 static HRESULT WINAPI ThreadMgr_CreateDocumentMgr( ITfThreadMgr* iface, ITfDocumentMgr
260 **ppdim)
261 {
262     ThreadMgr *This = (ThreadMgr *)iface;
263     TRACE("(%p)\n",iface);
264     return DocumentMgr_Constructor((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, ppdim);
265 }
266
267 static HRESULT WINAPI ThreadMgr_EnumDocumentMgrs( ITfThreadMgr* iface, IEnumTfDocumentMgrs
268 **ppEnum)
269 {
270     ThreadMgr *This = (ThreadMgr *)iface;
271     FIXME("STUB:(%p)\n",This);
272     return E_NOTIMPL;
273 }
274
275 static HRESULT WINAPI ThreadMgr_GetFocus( ITfThreadMgr* iface, ITfDocumentMgr
276 **ppdimFocus)
277 {
278     ThreadMgr *This = (ThreadMgr *)iface;
279     TRACE("(%p)\n",This);
280
281     if (!ppdimFocus)
282         return E_INVALIDARG;
283
284     *ppdimFocus = This->focus;
285
286     TRACE("->%p\n",This->focus);
287
288     if (This->focus == NULL)
289         return S_FALSE;
290
291     ITfDocumentMgr_AddRef(This->focus);
292
293     return S_OK;
294 }
295
296 static HRESULT WINAPI ThreadMgr_SetFocus( ITfThreadMgr* iface, ITfDocumentMgr *pdimFocus)
297 {
298     ITfDocumentMgr *check;
299     ThreadMgr *This = (ThreadMgr *)iface;
300
301     TRACE("(%p) %p\n",This,pdimFocus);
302
303     if (!pdimFocus || FAILED(IUnknown_QueryInterface(pdimFocus,&IID_ITfDocumentMgr,(LPVOID*) &check)))
304         return E_INVALIDARG;
305
306     ITfThreadMgrEventSink_OnSetFocus((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, check, This->focus);
307
308     if (This->focus)
309         ITfDocumentMgr_Release(This->focus);
310
311     This->focus = check;
312     return S_OK;
313 }
314
315 static HRESULT WINAPI ThreadMgr_AssociateFocus( ITfThreadMgr* iface, HWND hwnd,
316 ITfDocumentMgr *pdimNew, ITfDocumentMgr **ppdimPrev)
317 {
318     ThreadMgr *This = (ThreadMgr *)iface;
319     FIXME("STUB:(%p)\n",This);
320     return E_NOTIMPL;
321 }
322
323 static HRESULT WINAPI ThreadMgr_IsThreadFocus( ITfThreadMgr* iface, BOOL *pfThreadFocus)
324 {
325     ThreadMgr *This = (ThreadMgr *)iface;
326     FIXME("STUB:(%p)\n",This);
327     return E_NOTIMPL;
328 }
329
330 static HRESULT WINAPI ThreadMgr_GetFunctionProvider( ITfThreadMgr* iface, REFCLSID clsid,
331 ITfFunctionProvider **ppFuncProv)
332 {
333     ThreadMgr *This = (ThreadMgr *)iface;
334     FIXME("STUB:(%p)\n",This);
335     return E_NOTIMPL;
336 }
337
338 static HRESULT WINAPI ThreadMgr_EnumFunctionProviders( ITfThreadMgr* iface,
339 IEnumTfFunctionProviders **ppEnum)
340 {
341     ThreadMgr *This = (ThreadMgr *)iface;
342     FIXME("STUB:(%p)\n",This);
343     return E_NOTIMPL;
344 }
345
346 static HRESULT WINAPI ThreadMgr_GetGlobalCompartment( ITfThreadMgr* iface,
347 ITfCompartmentMgr **ppCompMgr)
348 {
349     ThreadMgr *This = (ThreadMgr *)iface;
350     FIXME("STUB:(%p)\n",This);
351     return E_NOTIMPL;
352 }
353
354 static const ITfThreadMgrVtbl ThreadMgr_ThreadMgrVtbl =
355 {
356     ThreadMgr_QueryInterface,
357     ThreadMgr_AddRef,
358     ThreadMgr_Release,
359
360     ThreadMgr_fnActivate,
361     ThreadMgr_fnDeactivate,
362     ThreadMgr_CreateDocumentMgr,
363     ThreadMgr_EnumDocumentMgrs,
364     ThreadMgr_GetFocus,
365     ThreadMgr_SetFocus,
366     ThreadMgr_AssociateFocus,
367     ThreadMgr_IsThreadFocus,
368     ThreadMgr_GetFunctionProvider,
369     ThreadMgr_EnumFunctionProviders,
370     ThreadMgr_GetGlobalCompartment
371 };
372
373
374 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
375 {
376     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
377     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
378 }
379
380 static ULONG WINAPI Source_AddRef(ITfSource *iface)
381 {
382     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
383     return ThreadMgr_AddRef((ITfThreadMgr*)This);
384 }
385
386 static ULONG WINAPI Source_Release(ITfSource *iface)
387 {
388     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
389     return ThreadMgr_Release((ITfThreadMgr *)This);
390 }
391
392 /*****************************************************
393  * ITfSource functions
394  *****************************************************/
395 static WINAPI HRESULT ThreadMgrSource_AdviseSink(ITfSource *iface,
396         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
397 {
398     ThreadMgrSink *tms;
399     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
400
401     TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
402
403     if (!riid || !punk || !pdwCookie)
404         return E_INVALIDARG;
405
406     if (IsEqualIID(riid, &IID_ITfThreadMgrEventSink))
407     {
408         tms = HeapAlloc(GetProcessHeap(),0,sizeof(ThreadMgrSink));
409         if (!tms)
410             return E_OUTOFMEMORY;
411         if (!SUCCEEDED(IUnknown_QueryInterface(punk, riid, (LPVOID*)&tms->interfaces.pITfThreadMgrEventSink)))
412         {
413             HeapFree(GetProcessHeap(),0,tms);
414             return CONNECT_E_CANNOTCONNECT;
415         }
416         list_add_head(&This->ThreadMgrEventSink,&tms->entry);
417         *pdwCookie = generate_Cookie(COOKIE_MAGIC_TMSINK, tms);
418     }
419     else
420     {
421         FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
422         return E_NOTIMPL;
423     }
424
425     TRACE("cookie %x\n",*pdwCookie);
426
427     return S_OK;
428 }
429
430 static WINAPI HRESULT ThreadMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
431 {
432     ThreadMgrSink *sink;
433     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
434
435     TRACE("(%p) %x\n",This,pdwCookie);
436
437     if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_TMSINK)
438         return E_INVALIDARG;
439
440     sink = (ThreadMgrSink*)remove_Cookie(pdwCookie);
441     if (!sink)
442         return CONNECT_E_NOCONNECTION;
443
444     list_remove(&sink->entry);
445     free_sink(sink);
446
447     return S_OK;
448 }
449
450 static const ITfSourceVtbl ThreadMgr_SourceVtbl =
451 {
452     Source_QueryInterface,
453     Source_AddRef,
454     Source_Release,
455
456     ThreadMgrSource_AdviseSink,
457     ThreadMgrSource_UnadviseSink,
458 };
459
460 /*****************************************************
461  * ITfKeystrokeMgr functions
462  *****************************************************/
463
464 static HRESULT WINAPI KeystrokeMgr_QueryInterface(ITfKeystrokeMgr *iface, REFIID iid, LPVOID *ppvOut)
465 {
466     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
467     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
468 }
469
470 static ULONG WINAPI KeystrokeMgr_AddRef(ITfKeystrokeMgr *iface)
471 {
472     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
473     return ThreadMgr_AddRef((ITfThreadMgr*)This);
474 }
475
476 static ULONG WINAPI KeystrokeMgr_Release(ITfKeystrokeMgr *iface)
477 {
478     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
479     return ThreadMgr_Release((ITfThreadMgr *)This);
480 }
481
482 static HRESULT WINAPI KeystrokeMgr_AdviseKeyEventSink(ITfKeystrokeMgr *iface,
483         TfClientId tid, ITfKeyEventSink *pSink, BOOL fForeground)
484 {
485     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
486     FIXME("STUB:(%p)\n",This);
487     return E_NOTIMPL;
488 }
489
490 static HRESULT WINAPI KeystrokeMgr_UnadviseKeyEventSink(ITfKeystrokeMgr *iface,
491         TfClientId tid)
492 {
493     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
494     FIXME("STUB:(%p)\n",This);
495     return E_NOTIMPL;
496 }
497
498 static HRESULT WINAPI KeystrokeMgr_GetForeground(ITfKeystrokeMgr *iface,
499         CLSID *pclsid)
500 {
501     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
502     FIXME("STUB:(%p)\n",This);
503     return E_NOTIMPL;
504 }
505
506 static HRESULT WINAPI KeystrokeMgr_TestKeyDown(ITfKeystrokeMgr *iface,
507         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
508 {
509     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
510     FIXME("STUB:(%p)\n",This);
511     return E_NOTIMPL;
512 }
513
514 static HRESULT WINAPI KeystrokeMgr_TestKeyUp(ITfKeystrokeMgr *iface,
515         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
516 {
517     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
518     FIXME("STUB:(%p)\n",This);
519     return E_NOTIMPL;
520 }
521
522 static HRESULT WINAPI KeystrokeMgr_KeyDown(ITfKeystrokeMgr *iface,
523         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
524 {
525     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
526     FIXME("STUB:(%p)\n",This);
527     return E_NOTIMPL;
528 }
529
530 static HRESULT WINAPI KeystrokeMgr_KeyUp(ITfKeystrokeMgr *iface,
531         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
532 {
533     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
534     FIXME("STUB:(%p)\n",This);
535     return E_NOTIMPL;
536 }
537
538 static HRESULT WINAPI KeystrokeMgr_GetPreservedKey(ITfKeystrokeMgr *iface,
539         ITfContext *pic, const TF_PRESERVEDKEY *pprekey, GUID *pguid)
540 {
541     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
542     FIXME("STUB:(%p)\n",This);
543     return E_NOTIMPL;
544 }
545
546 static HRESULT WINAPI KeystrokeMgr_IsPreservedKey(ITfKeystrokeMgr *iface,
547         REFGUID rguid, const TF_PRESERVEDKEY *pprekey, BOOL *pfRegistered)
548 {
549     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
550     struct list *cursor;
551
552     TRACE("(%p) %s (%x %x) %p\n",This,debugstr_guid(rguid), (pprekey)?pprekey->uVKey:0, (pprekey)?pprekey->uModifiers:0, pfRegistered);
553
554     if (!rguid || !pprekey || !pfRegistered)
555         return E_INVALIDARG;
556
557     LIST_FOR_EACH(cursor, &This->CurrentPreservedKeys)
558     {
559         PreservedKey* key = LIST_ENTRY(cursor,PreservedKey,entry);
560         if (IsEqualGUID(rguid,&key->guid) && pprekey->uVKey == key->prekey.uVKey && pprekey->uModifiers == key->prekey.uModifiers)
561         {
562             *pfRegistered = TRUE;
563             return S_OK;
564         }
565     }
566
567     *pfRegistered = FALSE;
568     return S_FALSE;
569 }
570
571 static HRESULT WINAPI KeystrokeMgr_PreserveKey(ITfKeystrokeMgr *iface,
572         TfClientId tid, REFGUID rguid, const TF_PRESERVEDKEY *prekey,
573         const WCHAR *pchDesc, ULONG cchDesc)
574 {
575     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
576     struct list *cursor;
577     PreservedKey *newkey;
578
579     TRACE("(%p) %x %s (%x,%x) %s\n",This,tid, debugstr_guid(rguid),(prekey)?prekey->uVKey:0,(prekey)?prekey->uModifiers:0,debugstr_wn(pchDesc,cchDesc));
580
581     if (!tid || ! rguid || !prekey || (cchDesc && !pchDesc))
582         return E_INVALIDARG;
583
584     LIST_FOR_EACH(cursor, &This->CurrentPreservedKeys)
585     {
586         PreservedKey* key = LIST_ENTRY(cursor,PreservedKey,entry);
587         if (IsEqualGUID(rguid,&key->guid) && prekey->uVKey == key->prekey.uVKey && prekey->uModifiers == key->prekey.uModifiers)
588             return TF_E_ALREADY_EXISTS;
589     }
590
591     newkey = HeapAlloc(GetProcessHeap(),0,sizeof(PreservedKey));
592     if (!newkey)
593         return E_OUTOFMEMORY;
594
595     newkey->guid  = *rguid;
596     newkey->prekey = *prekey;
597     newkey->tid = tid;
598     if (cchDesc)
599     {
600         newkey->description = HeapAlloc(GetProcessHeap(),0,cchDesc * sizeof(WCHAR));
601         if (!newkey->description)
602         {
603             HeapFree(GetProcessHeap(),0,newkey);
604             return E_OUTOFMEMORY;
605         }
606         memcpy(newkey->description, pchDesc, cchDesc*sizeof(WCHAR));
607     }
608
609     list_add_head(&This->CurrentPreservedKeys,&newkey->entry);
610
611     return S_OK;
612 }
613
614 static HRESULT WINAPI KeystrokeMgr_UnpreserveKey(ITfKeystrokeMgr *iface,
615         REFGUID rguid, const TF_PRESERVEDKEY *pprekey)
616 {
617     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
618     PreservedKey* key = NULL;
619     struct list *cursor;
620     TRACE("(%p) %s (%x %x)\n",This,debugstr_guid(rguid),(pprekey)?pprekey->uVKey:0, (pprekey)?pprekey->uModifiers:0);
621
622     if (!pprekey || !rguid)
623         return E_INVALIDARG;
624
625     LIST_FOR_EACH(cursor, &This->CurrentPreservedKeys)
626     {
627         key = LIST_ENTRY(cursor,PreservedKey,entry);
628         if (IsEqualGUID(rguid,&key->guid) && pprekey->uVKey == key->prekey.uVKey && pprekey->uModifiers == key->prekey.uModifiers)
629             break;
630         key = NULL;
631     }
632
633     if (!key)
634         return CONNECT_E_NOCONNECTION;
635
636     list_remove(&key->entry);
637     HeapFree(GetProcessHeap(),0,key->description);
638     HeapFree(GetProcessHeap(),0,key);
639
640     return S_OK;
641 }
642
643 static HRESULT WINAPI KeystrokeMgr_SetPreservedKeyDescription(ITfKeystrokeMgr *iface,
644         REFGUID rguid, const WCHAR *pchDesc, ULONG cchDesc)
645 {
646     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
647     FIXME("STUB:(%p)\n",This);
648     return E_NOTIMPL;
649 }
650
651 static HRESULT WINAPI KeystrokeMgr_GetPreservedKeyDescription(ITfKeystrokeMgr *iface,
652         REFGUID rguid, BSTR *pbstrDesc)
653 {
654     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
655     FIXME("STUB:(%p)\n",This);
656     return E_NOTIMPL;
657 }
658
659 static HRESULT WINAPI KeystrokeMgr_SimulatePreservedKey(ITfKeystrokeMgr *iface,
660         ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
661 {
662     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
663     FIXME("STUB:(%p)\n",This);
664     return E_NOTIMPL;
665 }
666
667 static const ITfKeystrokeMgrVtbl ThreadMgr_KeystrokeMgrVtbl =
668 {
669     KeystrokeMgr_QueryInterface,
670     KeystrokeMgr_AddRef,
671     KeystrokeMgr_Release,
672
673     KeystrokeMgr_AdviseKeyEventSink,
674     KeystrokeMgr_UnadviseKeyEventSink,
675     KeystrokeMgr_GetForeground,
676     KeystrokeMgr_TestKeyDown,
677     KeystrokeMgr_TestKeyUp,
678     KeystrokeMgr_KeyDown,
679     KeystrokeMgr_KeyUp,
680     KeystrokeMgr_GetPreservedKey,
681     KeystrokeMgr_IsPreservedKey,
682     KeystrokeMgr_PreserveKey,
683     KeystrokeMgr_UnpreserveKey,
684     KeystrokeMgr_SetPreservedKeyDescription,
685     KeystrokeMgr_GetPreservedKeyDescription,
686     KeystrokeMgr_SimulatePreservedKey
687 };
688
689 /*****************************************************
690  * ITfMessagePump functions
691  *****************************************************/
692
693 static HRESULT WINAPI MessagePump_QueryInterface(ITfMessagePump *iface, REFIID iid, LPVOID *ppvOut)
694 {
695     ThreadMgr *This = impl_from_ITfMessagePumpVtbl(iface);
696     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
697 }
698
699 static ULONG WINAPI MessagePump_AddRef(ITfMessagePump *iface)
700 {
701     ThreadMgr *This = impl_from_ITfMessagePumpVtbl(iface);
702     return ThreadMgr_AddRef((ITfThreadMgr*)This);
703 }
704
705 static ULONG WINAPI MessagePump_Release(ITfMessagePump *iface)
706 {
707     ThreadMgr *This = impl_from_ITfMessagePumpVtbl(iface);
708     return ThreadMgr_Release((ITfThreadMgr *)This);
709 }
710
711 static HRESULT WINAPI MessagePump_PeekMessageA(ITfMessagePump *iface,
712         LPMSG pMsg, HWND hwnd, UINT wMsgFilterMin, UINT wMsgFilterMax,
713         UINT wRemoveMsg, BOOL *pfResult)
714 {
715     if (!pfResult)
716         return E_INVALIDARG;
717     *pfResult = PeekMessageA(pMsg, hwnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
718     return S_OK;
719 }
720
721 static HRESULT WINAPI MessagePump_GetMessageA(ITfMessagePump *iface,
722         LPMSG pMsg, HWND hwnd, UINT wMsgFilterMin, UINT wMsgFilterMax,
723         BOOL *pfResult)
724 {
725     if (!pfResult)
726         return E_INVALIDARG;
727     *pfResult = GetMessageA(pMsg, hwnd, wMsgFilterMin, wMsgFilterMax);
728     return S_OK;
729 }
730
731 static HRESULT WINAPI MessagePump_PeekMessageW(ITfMessagePump *iface,
732         LPMSG pMsg, HWND hwnd, UINT wMsgFilterMin, UINT wMsgFilterMax,
733         UINT wRemoveMsg, BOOL *pfResult)
734 {
735     if (!pfResult)
736         return E_INVALIDARG;
737     *pfResult = PeekMessageW(pMsg, hwnd, wMsgFilterMin, wMsgFilterMax, wRemoveMsg);
738     return S_OK;
739 }
740
741 static HRESULT WINAPI MessagePump_GetMessageW(ITfMessagePump *iface,
742         LPMSG pMsg, HWND hwnd, UINT wMsgFilterMin, UINT wMsgFilterMax,
743         BOOL *pfResult)
744 {
745     if (!pfResult)
746         return E_INVALIDARG;
747     *pfResult = GetMessageW(pMsg, hwnd, wMsgFilterMin, wMsgFilterMax);
748     return S_OK;
749 }
750
751 static const ITfMessagePumpVtbl ThreadMgr_MessagePumpVtbl =
752 {
753     MessagePump_QueryInterface,
754     MessagePump_AddRef,
755     MessagePump_Release,
756
757     MessagePump_PeekMessageA,
758     MessagePump_GetMessageA,
759     MessagePump_PeekMessageW,
760     MessagePump_GetMessageW
761 };
762
763 /*****************************************************
764  * ITfClientId functions
765  *****************************************************/
766
767 static HRESULT WINAPI ClientId_QueryInterface(ITfClientId *iface, REFIID iid, LPVOID *ppvOut)
768 {
769     ThreadMgr *This = impl_from_ITfClientIdVtbl(iface);
770     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
771 }
772
773 static ULONG WINAPI ClientId_AddRef(ITfClientId *iface)
774 {
775     ThreadMgr *This = impl_from_ITfClientIdVtbl(iface);
776     return ThreadMgr_AddRef((ITfThreadMgr*)This);
777 }
778
779 static ULONG WINAPI ClientId_Release(ITfClientId *iface)
780 {
781     ThreadMgr *This = impl_from_ITfClientIdVtbl(iface);
782     return ThreadMgr_Release((ITfThreadMgr *)This);
783 }
784
785 static HRESULT WINAPI ClientId_GetClientId(ITfClientId *iface,
786     REFCLSID rclsid, TfClientId *ptid)
787
788 {
789     HRESULT hr;
790     ITfCategoryMgr *catmgr;
791     ThreadMgr *This = impl_from_ITfClientIdVtbl(iface);
792
793     TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
794
795     CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr);
796     hr = ITfCategoryMgr_RegisterGUID(catmgr,rclsid,ptid);
797     ITfCategoryMgr_Release(catmgr);
798
799     return hr;
800 }
801
802 static const ITfClientIdVtbl ThreadMgr_ClientIdVtbl =
803 {
804     ClientId_QueryInterface,
805     ClientId_AddRef,
806     ClientId_Release,
807
808     ClientId_GetClientId
809 };
810
811 /*****************************************************
812  * ITfThreadMgrEventSink functions  (internal)
813  *****************************************************/
814 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
815 {
816     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
817     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
818 }
819
820 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
821 {
822     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
823     return ThreadMgr_AddRef((ITfThreadMgr*)This);
824 }
825
826 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
827 {
828     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
829     return ThreadMgr_Release((ITfThreadMgr *)This);
830 }
831
832
833 static WINAPI HRESULT ThreadMgrEventSink_OnInitDocumentMgr(
834         ITfThreadMgrEventSink *iface,ITfDocumentMgr *pdim)
835 {
836     struct list *cursor;
837     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
838
839     TRACE("(%p) %p\n",This,pdim);
840
841     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
842     {
843         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
844         ITfThreadMgrEventSink_OnInitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
845     }
846
847     return S_OK;
848 }
849
850 static WINAPI HRESULT ThreadMgrEventSink_OnUninitDocumentMgr(
851         ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdim)
852 {
853     struct list *cursor;
854     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
855
856     TRACE("(%p) %p\n",This,pdim);
857
858     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
859     {
860         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
861         ITfThreadMgrEventSink_OnUninitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
862     }
863
864     return S_OK;
865 }
866
867 static WINAPI HRESULT ThreadMgrEventSink_OnSetFocus(
868         ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdimFocus,
869         ITfDocumentMgr *pdimPrevFocus)
870 {
871     struct list *cursor;
872     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
873
874     TRACE("(%p) %p %p\n",This,pdimFocus, pdimPrevFocus);
875
876     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
877     {
878         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
879         ITfThreadMgrEventSink_OnSetFocus(sink->interfaces.pITfThreadMgrEventSink, pdimFocus, pdimPrevFocus);
880     }
881
882     return S_OK;
883 }
884
885 static WINAPI HRESULT ThreadMgrEventSink_OnPushContext(
886         ITfThreadMgrEventSink *iface, ITfContext *pic)
887 {
888     struct list *cursor;
889     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
890
891     TRACE("(%p) %p\n",This,pic);
892
893     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
894     {
895         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
896         ITfThreadMgrEventSink_OnPushContext(sink->interfaces.pITfThreadMgrEventSink,pic);
897     }
898
899     return S_OK;
900 }
901
902 static WINAPI HRESULT ThreadMgrEventSink_OnPopContext(
903         ITfThreadMgrEventSink *iface, ITfContext *pic)
904 {
905     struct list *cursor;
906     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
907
908     TRACE("(%p) %p\n",This,pic);
909
910     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
911     {
912         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
913         ITfThreadMgrEventSink_OnPopContext(sink->interfaces.pITfThreadMgrEventSink,pic);
914     }
915
916     return S_OK;
917 }
918
919 static const ITfThreadMgrEventSinkVtbl ThreadMgr_ThreadMgrEventSinkVtbl =
920 {
921     ThreadMgrEventSink_QueryInterface,
922     ThreadMgrEventSink_AddRef,
923     ThreadMgrEventSink_Release,
924
925     ThreadMgrEventSink_OnInitDocumentMgr,
926     ThreadMgrEventSink_OnUninitDocumentMgr,
927     ThreadMgrEventSink_OnSetFocus,
928     ThreadMgrEventSink_OnPushContext,
929     ThreadMgrEventSink_OnPopContext
930 };
931
932 HRESULT ThreadMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
933 {
934     ThreadMgr *This;
935     if (pUnkOuter)
936         return CLASS_E_NOAGGREGATION;
937
938     /* Only 1 ThreadMgr is created per thread */
939     This = TlsGetValue(tlsIndex);
940     if (This)
941     {
942         ThreadMgr_AddRef((ITfThreadMgr*)This);
943         *ppOut = (IUnknown*)This;
944         return S_OK;
945     }
946
947     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgr));
948     if (This == NULL)
949         return E_OUTOFMEMORY;
950
951     This->ThreadMgrVtbl= &ThreadMgr_ThreadMgrVtbl;
952     This->SourceVtbl = &ThreadMgr_SourceVtbl;
953     This->KeystrokeMgrVtbl= &ThreadMgr_KeystrokeMgrVtbl;
954     This->MessagePumpVtbl= &ThreadMgr_MessagePumpVtbl;
955     This->ClientIdVtbl = &ThreadMgr_ClientIdVtbl;
956     This->ThreadMgrEventSinkVtbl = &ThreadMgr_ThreadMgrEventSinkVtbl;
957     This->refCount = 1;
958     TlsSetValue(tlsIndex,This);
959
960     list_init(&This->CurrentPreservedKeys);
961
962     list_init(&This->ActiveLanguageProfileNotifySink);
963     list_init(&This->DisplayAttributeNotifySink);
964     list_init(&This->KeyTraceEventSink);
965     list_init(&This->PreservedKeyNotifySink);
966     list_init(&This->ThreadFocusSink);
967     list_init(&This->ThreadMgrEventSink);
968
969     TRACE("returning %p\n", This);
970     *ppOut = (IUnknown *)This;
971     return S_OK;
972 }