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
26 #include "wine/debug.h"
36 #include "inputscope.h"
38 #include "msctf_internal.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msctf
);
42 static HINSTANCE MSCTF_hinstance
;
52 TF_LANGUAGEPROFILE LanguageProfile
;
53 ITfTextInputProcessor
*pITfTextInputProcessor
;
54 ITfThreadMgrEx
*pITfThreadMgrEx
;
55 ITfKeyEventSink
*pITfKeyEventSink
;
57 } ActivatedTextService
;
62 ActivatedTextService
*ats
;
65 static CookieInternal
*cookies
;
67 static UINT array_size
;
69 static struct list AtsList
= LIST_INIT(AtsList
);
70 static UINT activated
= 0;
73 TfClientId processId
= 0;
74 ITfCompartmentMgr
*globalCompartmentMgr
= NULL
;
76 const WCHAR szwSystemTIPKey
[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F','\\','T','I','P',0};
77 const WCHAR szwSystemCTFKey
[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F',0};
79 typedef HRESULT (*LPFNCONSTRUCTOR
)(IUnknown
*pUnkOuter
, IUnknown
**ppvOut
);
85 {&CLSID_TF_ThreadMgr
, ThreadMgr_Constructor
},
86 {&CLSID_TF_InputProcessorProfiles
, InputProcessorProfiles_Constructor
},
87 {&CLSID_TF_CategoryMgr
, CategoryMgr_Constructor
},
88 {&CLSID_TF_LangBarMgr
, LangBarMgr_Constructor
},
89 {&CLSID_TF_DisplayAttributeMgr
, DisplayAttributeMgr_Constructor
},
93 typedef struct tagClassFactory
95 IClassFactory IClassFactory_iface
;
100 static inline ClassFactory
*impl_from_IClassFactory(IClassFactory
*iface
)
102 return CONTAINING_RECORD(iface
, ClassFactory
, IClassFactory_iface
);
105 static void ClassFactory_Destructor(ClassFactory
*This
)
107 TRACE("Destroying class factory %p\n", This
);
108 HeapFree(GetProcessHeap(),0,This
);
111 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, LPVOID
*ppvOut
)
114 if (IsEqualIID(riid
, &IID_IClassFactory
) || IsEqualIID(riid
, &IID_IUnknown
)) {
115 IClassFactory_AddRef(iface
);
120 WARN("Unknown interface %s\n", debugstr_guid(riid
));
121 return E_NOINTERFACE
;
124 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
126 ClassFactory
*This
= impl_from_IClassFactory(iface
);
127 return InterlockedIncrement(&This
->ref
);
130 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
132 ClassFactory
*This
= impl_from_IClassFactory(iface
);
133 ULONG ret
= InterlockedDecrement(&This
->ref
);
136 ClassFactory_Destructor(This
);
140 static HRESULT WINAPI
ClassFactory_CreateInstance(IClassFactory
*iface
, IUnknown
*punkOuter
, REFIID iid
, LPVOID
*ppvOut
)
142 ClassFactory
*This
= impl_from_IClassFactory(iface
);
146 TRACE("(%p, %p, %s, %p)\n", iface
, punkOuter
, debugstr_guid(iid
), ppvOut
);
147 ret
= This
->ctor(punkOuter
, &obj
);
150 ret
= IUnknown_QueryInterface(obj
, iid
, ppvOut
);
151 IUnknown_Release(obj
);
155 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
157 ClassFactory
*This
= impl_from_IClassFactory(iface
);
159 TRACE("(%p)->(%x)\n", This
, fLock
);
164 static const IClassFactoryVtbl ClassFactoryVtbl
= {
166 ClassFactory_QueryInterface
,
168 ClassFactory_Release
,
171 ClassFactory_CreateInstance
,
172 ClassFactory_LockServer
175 static HRESULT
ClassFactory_Constructor(LPFNCONSTRUCTOR ctor
, LPVOID
*ppvOut
)
177 ClassFactory
*This
= HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory
));
178 This
->IClassFactory_iface
.lpVtbl
= &ClassFactoryVtbl
;
181 *ppvOut
= &This
->IClassFactory_iface
;
182 TRACE("Created class factory %p\n", This
);
186 /*************************************************************************
187 * DWORD Cookie Management
189 DWORD
generate_Cookie(DWORD magic
, LPVOID data
)
193 /* try to reuse IDs if possible */
194 for (i
= 0; i
< id_last
; i
++)
195 if (cookies
[i
].id
== 0) break;
201 cookies
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CookieInternal
) * 10);
204 ERR("Out of memory, Unable to alloc cookies array\n");
211 CookieInternal
*new_cookies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cookies
,
212 sizeof(CookieInternal
) * (array_size
* 2));
215 ERR("Out of memory, Unable to realloc cookies array\n");
218 cookies
= new_cookies
;
223 cookies
[i
].id
= i
+ 1; /* a return of 0 is used for failure */
224 cookies
[i
].magic
= magic
;
225 cookies
[i
].data
= data
;
230 return cookies
[i
].id
;
233 DWORD
get_Cookie_magic(DWORD id
)
237 if (index
>= id_last
)
240 if (cookies
[index
].id
== 0)
243 return cookies
[index
].magic
;
246 LPVOID
get_Cookie_data(DWORD id
)
250 if (index
>= id_last
)
253 if (cookies
[index
].id
== 0)
256 return cookies
[index
].data
;
259 LPVOID
remove_Cookie(DWORD id
)
263 if (index
>= id_last
)
266 if (cookies
[index
].id
== 0)
269 cookies
[index
].id
= 0;
270 return cookies
[index
].data
;
273 DWORD
enumerate_Cookie(DWORD magic
, DWORD
*index
)
276 for (i
= *index
; i
< id_last
; i
++)
277 if (cookies
[i
].id
!= 0 && cookies
[i
].magic
== magic
)
280 return cookies
[i
].id
;
285 HRESULT
advise_sink(struct list
*sink_list
, REFIID riid
, DWORD cookie_magic
, IUnknown
*unk
, DWORD
*cookie
)
289 sink
= HeapAlloc(GetProcessHeap(), 0, sizeof(*sink
));
291 return E_OUTOFMEMORY
;
293 if (FAILED(IUnknown_QueryInterface(unk
, riid
, (void**)&sink
->interfaces
.pIUnknown
)))
295 HeapFree(GetProcessHeap(), 0, sink
);
296 return CONNECT_E_CANNOTCONNECT
;
299 list_add_head(sink_list
, &sink
->entry
);
300 *cookie
= generate_Cookie(cookie_magic
, sink
);
301 TRACE("cookie %x\n", *cookie
);
305 static void free_sink(Sink
*sink
)
307 list_remove(&sink
->entry
);
308 IUnknown_Release(sink
->interfaces
.pIUnknown
);
309 HeapFree(GetProcessHeap(), 0, sink
);
312 HRESULT
unadvise_sink(DWORD cookie
)
316 sink
= remove_Cookie(cookie
);
318 return CONNECT_E_NOCONNECTION
;
324 void free_sinks(struct list
*sink_list
)
326 while(!list_empty(sink_list
))
328 Sink
* sink
= LIST_ENTRY(sink_list
->next
, Sink
, entry
);
333 /*****************************************************************************
334 * Active Text Service Management
335 *****************************************************************************/
336 static HRESULT
activate_given_ts(ActivatedTextService
*actsvr
, ITfThreadMgrEx
*tm
)
340 /* Already Active? */
341 if (actsvr
->pITfTextInputProcessor
)
344 hr
= CoCreateInstance (&actsvr
->LanguageProfile
.clsid
, NULL
, CLSCTX_INPROC_SERVER
,
345 &IID_ITfTextInputProcessor
, (void**)&actsvr
->pITfTextInputProcessor
);
346 if (FAILED(hr
)) return hr
;
348 hr
= ITfTextInputProcessor_Activate(actsvr
->pITfTextInputProcessor
, (ITfThreadMgr
*)tm
, actsvr
->tid
);
351 ITfTextInputProcessor_Release(actsvr
->pITfTextInputProcessor
);
352 actsvr
->pITfTextInputProcessor
= NULL
;
356 actsvr
->pITfThreadMgrEx
= tm
;
357 ITfThreadMgrEx_AddRef(tm
);
361 static HRESULT
deactivate_given_ts(ActivatedTextService
*actsvr
)
365 if (actsvr
->pITfTextInputProcessor
)
367 hr
= ITfTextInputProcessor_Deactivate(actsvr
->pITfTextInputProcessor
);
368 ITfTextInputProcessor_Release(actsvr
->pITfTextInputProcessor
);
369 ITfThreadMgrEx_Release(actsvr
->pITfThreadMgrEx
);
370 actsvr
->pITfTextInputProcessor
= NULL
;
371 actsvr
->pITfThreadMgrEx
= NULL
;
377 static void deactivate_remove_conflicting_ts(REFCLSID catid
)
379 AtsEntry
*ats
, *cursor2
;
381 LIST_FOR_EACH_ENTRY_SAFE(ats
, cursor2
, &AtsList
, AtsEntry
, entry
)
383 if (IsEqualCLSID(catid
,&ats
->ats
->LanguageProfile
.catid
))
385 deactivate_given_ts(ats
->ats
);
386 list_remove(&ats
->entry
);
387 HeapFree(GetProcessHeap(),0,ats
->ats
);
388 HeapFree(GetProcessHeap(),0,ats
);
389 /* we are guaranteeing there is only 1 */
395 HRESULT
add_active_textservice(TF_LANGUAGEPROFILE
*lp
)
397 ActivatedTextService
*actsvr
;
398 ITfCategoryMgr
*catmgr
;
400 ITfThreadMgrEx
*tm
= TlsGetValue(tlsIndex
);
401 ITfClientId
*clientid
;
403 if (!tm
) return E_UNEXPECTED
;
405 actsvr
= HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService
));
406 if (!actsvr
) return E_OUTOFMEMORY
;
408 ITfThreadMgrEx_QueryInterface(tm
, &IID_ITfClientId
, (void **)&clientid
);
409 ITfClientId_GetClientId(clientid
, &lp
->clsid
, &actsvr
->tid
);
410 ITfClientId_Release(clientid
);
414 HeapFree(GetProcessHeap(),0,actsvr
);
415 return E_OUTOFMEMORY
;
418 actsvr
->pITfTextInputProcessor
= NULL
;
419 actsvr
->LanguageProfile
= *lp
;
420 actsvr
->pITfKeyEventSink
= NULL
;
422 /* get TIP category */
423 if (SUCCEEDED(CategoryMgr_Constructor(NULL
,(IUnknown
**)&catmgr
)))
425 static const GUID
*list
[3] = {&GUID_TFCAT_TIP_SPEECH
, &GUID_TFCAT_TIP_KEYBOARD
, &GUID_TFCAT_TIP_HANDWRITING
};
427 ITfCategoryMgr_FindClosestCategory(catmgr
,
428 &actsvr
->LanguageProfile
.clsid
, &actsvr
->LanguageProfile
.catid
,
431 ITfCategoryMgr_Release(catmgr
);
435 ERR("CategoryMgr construction failed\n");
436 actsvr
->LanguageProfile
.catid
= GUID_NULL
;
439 if (!IsEqualGUID(&actsvr
->LanguageProfile
.catid
,&GUID_NULL
))
440 deactivate_remove_conflicting_ts(&actsvr
->LanguageProfile
.catid
);
443 activate_given_ts(actsvr
, tm
);
445 entry
= HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry
));
449 HeapFree(GetProcessHeap(),0,actsvr
);
450 return E_OUTOFMEMORY
;
454 list_add_head(&AtsList
, &entry
->entry
);
459 BOOL
get_active_textservice(REFCLSID rclsid
, TF_LANGUAGEPROFILE
*profile
)
463 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
465 if (IsEqualCLSID(rclsid
,&ats
->ats
->LanguageProfile
.clsid
))
468 *profile
= ats
->ats
->LanguageProfile
;
475 HRESULT
activate_textservices(ITfThreadMgrEx
*tm
)
484 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
486 hr
= activate_given_ts(ats
->ats
, tm
);
488 FIXME("Failed to activate text service\n");
493 HRESULT
deactivate_textservices(void)
501 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
502 deactivate_given_ts(ats
->ats
);
507 CLSID
get_textservice_clsid(TfClientId tid
)
511 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
512 if (ats
->ats
->tid
== tid
)
513 return ats
->ats
->LanguageProfile
.clsid
;
517 HRESULT
get_textservice_sink(TfClientId tid
, REFCLSID iid
, IUnknown
**sink
)
521 if (!IsEqualCLSID(iid
,&IID_ITfKeyEventSink
))
522 return E_NOINTERFACE
;
524 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
525 if (ats
->ats
->tid
== tid
)
527 *sink
= (IUnknown
*)ats
->ats
->pITfKeyEventSink
;
534 HRESULT
set_textservice_sink(TfClientId tid
, REFCLSID iid
, IUnknown
* sink
)
538 if (!IsEqualCLSID(iid
,&IID_ITfKeyEventSink
))
539 return E_NOINTERFACE
;
541 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
542 if (ats
->ats
->tid
== tid
)
544 ats
->ats
->pITfKeyEventSink
= (ITfKeyEventSink
*)sink
;
551 /*************************************************************************
554 BOOL WINAPI
DllMain(HINSTANCE hinst
, DWORD fdwReason
, LPVOID fImpLoad
)
556 TRACE("%p 0x%x %p\n", hinst
, fdwReason
, fImpLoad
);
559 case DLL_PROCESS_ATTACH
:
560 MSCTF_hinstance
= hinst
;
561 tlsIndex
= TlsAlloc();
563 case DLL_PROCESS_DETACH
:
571 /*************************************************************************
572 * DllCanUnloadNow (MSCTF.@)
574 HRESULT WINAPI
DllCanUnloadNow(void)
579 /***********************************************************************
580 * DllGetClassObject (MSCTF.@)
582 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID iid
, LPVOID
*ppvOut
)
587 if (!IsEqualIID(iid
, &IID_IUnknown
) && !IsEqualIID(iid
, &IID_IClassFactory
))
588 return E_NOINTERFACE
;
590 for (i
= 0; ClassesTable
[i
].clsid
!= NULL
; i
++)
591 if (IsEqualCLSID(ClassesTable
[i
].clsid
, clsid
)) {
592 return ClassFactory_Constructor(ClassesTable
[i
].ctor
, ppvOut
);
594 FIXME("CLSID %s not supported\n", debugstr_guid(clsid
));
595 return CLASS_E_CLASSNOTAVAILABLE
;
598 /***********************************************************************
599 * DllRegisterServer (MSCTF.@)
601 HRESULT WINAPI
DllRegisterServer(void)
603 return __wine_register_resources( MSCTF_hinstance
);
606 /***********************************************************************
607 * DllUnregisterServer (MSCTF.@)
609 HRESULT WINAPI
DllUnregisterServer(void)
611 return __wine_unregister_resources( MSCTF_hinstance
);
614 /***********************************************************************
615 * TF_CreateThreadMgr (MSCTF.@)
617 HRESULT WINAPI
TF_CreateThreadMgr(ITfThreadMgr
**pptim
)
620 return ThreadMgr_Constructor(NULL
,(IUnknown
**)pptim
);
623 /***********************************************************************
624 * TF_GetThreadMgr (MSCTF.@)
626 HRESULT WINAPI
TF_GetThreadMgr(ITfThreadMgr
**pptim
)
629 *pptim
= TlsGetValue(tlsIndex
);
632 ITfThreadMgr_AddRef(*pptim
);
637 /***********************************************************************
638 * SetInputScope(MSCTF.@)
640 HRESULT WINAPI
SetInputScope(HWND hwnd
, InputScope inputscope
)
642 FIXME("STUB: %p %i\n",hwnd
,inputscope
);
646 /***********************************************************************
647 * SetInputScopes(MSCTF.@)
649 HRESULT WINAPI
SetInputScopes(HWND hwnd
, const InputScope
*pInputScopes
,
650 UINT cInputScopes
, WCHAR
**ppszPhraseList
,
651 UINT cPhrases
, WCHAR
*pszRegExp
, WCHAR
*pszSRGS
)
654 FIXME("STUB: %p ... %s %s\n",hwnd
, debugstr_w(pszRegExp
), debugstr_w(pszSRGS
));
655 for (i
= 0; i
< cInputScopes
; i
++)
656 TRACE("\tScope[%u] = %i\n",i
,pInputScopes
[i
]);
657 for (i
= 0; i
< cPhrases
; i
++)
658 TRACE("\tPhrase[%u] = %s\n",i
,debugstr_w(ppszPhraseList
[i
]));
663 /***********************************************************************
664 * TF_CreateInputProcessorProfiles(MSCTF.@)
666 HRESULT WINAPI
TF_CreateInputProcessorProfiles(
667 ITfInputProcessorProfiles
**ppipr
)
669 return InputProcessorProfiles_Constructor(NULL
,(IUnknown
**)ppipr
);
672 /***********************************************************************
673 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
675 HRESULT WINAPI
TF_InvalidAssemblyListCacheIfExist(void)
681 /***********************************************************************
682 * TF_CreateLangBarMgr (MSCTF.@)
684 HRESULT WINAPI
TF_CreateLangBarMgr(ITfLangBarMgr
**pppbm
)
687 return LangBarMgr_Constructor(NULL
,(IUnknown
**)pppbm
);
690 HRESULT WINAPI
TF_CreateLangBarItemMgr(ITfLangBarItemMgr
**pplbim
)
692 FIXME("stub %p\n", pplbim
);
698 /***********************************************************************
699 * TF_InitMlngInfo (MSCTF.@)
701 HRESULT WINAPI
TF_InitMlngInfo(void)