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
28 #include "wine/debug.h"
29 #include "wine/list.h"
38 #include "msctf_internal.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msctf
);
42 static LONG MSCTF_refCount
;
44 static HINSTANCE MSCTF_hinstance
;
54 TF_LANGUAGEPROFILE LanguageProfile
;
55 ITfTextInputProcessor
*pITfTextInputProcessor
;
56 ITfThreadMgr
*pITfThreadMgr
;
57 ITfKeyEventSink
*pITfKeyEventSink
;
59 } ActivatedTextService
;
64 ActivatedTextService
*ats
;
67 static CookieInternal
*cookies
;
69 static UINT array_size
;
71 static struct list AtsList
= LIST_INIT(AtsList
);
72 static UINT activated
= 0;
75 TfClientId processId
= 0;
76 ITfCompartmentMgr
*globalCompartmentMgr
= NULL
;
78 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};
79 const WCHAR szwSystemCTFKey
[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\','C','T','F',0};
81 typedef HRESULT (*LPFNCONSTRUCTOR
)(IUnknown
*pUnkOuter
, IUnknown
**ppvOut
);
87 {&CLSID_TF_ThreadMgr
, ThreadMgr_Constructor
},
88 {&CLSID_TF_InputProcessorProfiles
, InputProcessorProfiles_Constructor
},
89 {&CLSID_TF_CategoryMgr
, CategoryMgr_Constructor
},
90 {&CLSID_TF_LangBarMgr
, LangBarMgr_Constructor
},
91 {&CLSID_TF_DisplayAttributeMgr
, DisplayAttributeMgr_Constructor
},
95 typedef struct tagClassFactory
97 const IClassFactoryVtbl
*vtbl
;
102 static void ClassFactory_Destructor(ClassFactory
*This
)
104 TRACE("Destroying class factory %p\n", This
);
105 HeapFree(GetProcessHeap(),0,This
);
109 static HRESULT WINAPI
ClassFactory_QueryInterface(IClassFactory
*iface
, REFIID riid
, LPVOID
*ppvOut
)
112 if (IsEqualIID(riid
, &IID_IClassFactory
) || IsEqualIID(riid
, &IID_IUnknown
)) {
113 IClassFactory_AddRef(iface
);
118 WARN("Unknown interface %s\n", debugstr_guid(riid
));
119 return E_NOINTERFACE
;
122 static ULONG WINAPI
ClassFactory_AddRef(IClassFactory
*iface
)
124 ClassFactory
*This
= (ClassFactory
*)iface
;
125 return InterlockedIncrement(&This
->ref
);
128 static ULONG WINAPI
ClassFactory_Release(IClassFactory
*iface
)
130 ClassFactory
*This
= (ClassFactory
*)iface
;
131 ULONG ret
= InterlockedDecrement(&This
->ref
);
134 ClassFactory_Destructor(This
);
138 static HRESULT WINAPI
ClassFactory_CreateInstance(IClassFactory
*iface
, IUnknown
*punkOuter
, REFIID iid
, LPVOID
*ppvOut
)
140 ClassFactory
*This
= (ClassFactory
*)iface
;
144 TRACE("(%p, %p, %s, %p)\n", iface
, punkOuter
, debugstr_guid(iid
), ppvOut
);
145 ret
= This
->ctor(punkOuter
, &obj
);
148 ret
= IUnknown_QueryInterface(obj
, iid
, ppvOut
);
149 IUnknown_Release(obj
);
153 static HRESULT WINAPI
ClassFactory_LockServer(IClassFactory
*iface
, BOOL fLock
)
155 ClassFactory
*This
= (ClassFactory
*)iface
;
157 TRACE("(%p)->(%x)\n", This
, fLock
);
160 InterlockedIncrement(&MSCTF_refCount
);
162 InterlockedDecrement(&MSCTF_refCount
);
167 static const IClassFactoryVtbl ClassFactoryVtbl
= {
169 ClassFactory_QueryInterface
,
171 ClassFactory_Release
,
174 ClassFactory_CreateInstance
,
175 ClassFactory_LockServer
178 static HRESULT
ClassFactory_Constructor(LPFNCONSTRUCTOR ctor
, LPVOID
*ppvOut
)
180 ClassFactory
*This
= HeapAlloc(GetProcessHeap(),0,sizeof(ClassFactory
));
181 This
->vtbl
= &ClassFactoryVtbl
;
185 TRACE("Created class factory %p\n", This
);
190 /*************************************************************************
191 * DWORD Cookie Management
193 DWORD
generate_Cookie(DWORD magic
, LPVOID data
)
197 /* try to reuse IDs if possible */
198 for (i
= 0; i
< id_last
; i
++)
199 if (cookies
[i
].id
== 0) break;
205 cookies
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(CookieInternal
) * 10);
208 ERR("Out of memory, Unable to alloc cookies array\n");
215 CookieInternal
*new_cookies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, cookies
,
216 sizeof(CookieInternal
) * (array_size
* 2));
219 ERR("Out of memory, Unable to realloc cookies array\n");
222 cookies
= new_cookies
;
227 cookies
[i
].id
= i
+ 1; /* a return of 0 is used for failure */
228 cookies
[i
].magic
= magic
;
229 cookies
[i
].data
= data
;
234 return cookies
[i
].id
;
237 DWORD
get_Cookie_magic(DWORD id
)
241 if (index
>= id_last
)
244 if (cookies
[index
].id
== 0)
247 return cookies
[index
].magic
;
250 LPVOID
get_Cookie_data(DWORD id
)
254 if (index
>= id_last
)
257 if (cookies
[index
].id
== 0)
260 return cookies
[index
].data
;
263 LPVOID
remove_Cookie(DWORD id
)
267 if (index
>= id_last
)
270 if (cookies
[index
].id
== 0)
273 cookies
[index
].id
= 0;
274 return cookies
[index
].data
;
277 DWORD
enumerate_Cookie(DWORD magic
, DWORD
*index
)
280 for (i
= *index
; i
< id_last
; i
++)
281 if (cookies
[i
].id
!= 0 && cookies
[i
].magic
== magic
)
284 return cookies
[i
].id
;
289 /*****************************************************************************
290 * Active Text Service Management
291 *****************************************************************************/
292 static HRESULT
activate_given_ts(ActivatedTextService
*actsvr
, ITfThreadMgr
* tm
)
296 /* Already Active? */
297 if (actsvr
->pITfTextInputProcessor
)
300 hr
= CoCreateInstance (&actsvr
->LanguageProfile
.clsid
, NULL
, CLSCTX_INPROC_SERVER
,
301 &IID_ITfTextInputProcessor
, (void**)&actsvr
->pITfTextInputProcessor
);
302 if (FAILED(hr
)) return hr
;
304 hr
= ITfTextInputProcessor_Activate(actsvr
->pITfTextInputProcessor
, tm
, actsvr
->tid
);
307 ITfTextInputProcessor_Release(actsvr
->pITfTextInputProcessor
);
308 actsvr
->pITfTextInputProcessor
= NULL
;
312 actsvr
->pITfThreadMgr
= tm
;
313 ITfThreadMgr_AddRef(tm
);
317 static HRESULT
deactivate_given_ts(ActivatedTextService
*actsvr
)
321 if (actsvr
->pITfTextInputProcessor
)
323 hr
= ITfTextInputProcessor_Deactivate(actsvr
->pITfTextInputProcessor
);
324 ITfTextInputProcessor_Release(actsvr
->pITfTextInputProcessor
);
325 ITfThreadMgr_Release(actsvr
->pITfThreadMgr
);
326 actsvr
->pITfTextInputProcessor
= NULL
;
327 actsvr
->pITfThreadMgr
= NULL
;
333 static void deactivate_remove_conflicting_ts(REFCLSID catid
)
335 AtsEntry
*ats
, *cursor2
;
337 LIST_FOR_EACH_ENTRY_SAFE(ats
, cursor2
, &AtsList
, AtsEntry
, entry
)
339 if (IsEqualCLSID(catid
,&ats
->ats
->LanguageProfile
.catid
))
341 deactivate_given_ts(ats
->ats
);
342 list_remove(&ats
->entry
);
343 HeapFree(GetProcessHeap(),0,ats
->ats
);
344 HeapFree(GetProcessHeap(),0,ats
);
345 /* we are guarenteeing there is only 1 */
351 HRESULT
add_active_textservice(TF_LANGUAGEPROFILE
*lp
)
353 ActivatedTextService
*actsvr
;
354 ITfCategoryMgr
*catmgr
;
356 ITfThreadMgr
*tm
= TlsGetValue(tlsIndex
);
357 ITfClientId
*clientid
;
359 if (!tm
) return E_UNEXPECTED
;
361 actsvr
= HeapAlloc(GetProcessHeap(),0,sizeof(ActivatedTextService
));
362 if (!actsvr
) return E_OUTOFMEMORY
;
364 ITfThreadMgr_QueryInterface(tm
,&IID_ITfClientId
,(LPVOID
)&clientid
);
365 ITfClientId_GetClientId(clientid
, &lp
->clsid
, &actsvr
->tid
);
366 ITfClientId_Release(clientid
);
370 HeapFree(GetProcessHeap(),0,actsvr
);
371 return E_OUTOFMEMORY
;
374 actsvr
->pITfTextInputProcessor
= NULL
;
375 actsvr
->LanguageProfile
= *lp
;
376 actsvr
->LanguageProfile
.fActive
= TRUE
;
377 actsvr
->pITfKeyEventSink
= NULL
;
379 /* get TIP category */
380 if (SUCCEEDED(CategoryMgr_Constructor(NULL
,(IUnknown
**)&catmgr
)))
382 static const GUID
*list
[3] = {&GUID_TFCAT_TIP_SPEECH
, &GUID_TFCAT_TIP_KEYBOARD
, &GUID_TFCAT_TIP_HANDWRITING
};
384 ITfCategoryMgr_FindClosestCategory(catmgr
,
385 &actsvr
->LanguageProfile
.clsid
, &actsvr
->LanguageProfile
.catid
,
388 ITfCategoryMgr_Release(catmgr
);
392 ERR("CategoryMgr construction failed\n");
393 actsvr
->LanguageProfile
.catid
= GUID_NULL
;
396 if (!IsEqualGUID(&actsvr
->LanguageProfile
.catid
,&GUID_NULL
))
397 deactivate_remove_conflicting_ts(&actsvr
->LanguageProfile
.catid
);
400 activate_given_ts(actsvr
, tm
);
402 entry
= HeapAlloc(GetProcessHeap(),0,sizeof(AtsEntry
));
406 HeapFree(GetProcessHeap(),0,actsvr
);
407 return E_OUTOFMEMORY
;
411 list_add_head(&AtsList
, &entry
->entry
);
416 BOOL
get_active_textservice(REFCLSID rclsid
, TF_LANGUAGEPROFILE
*profile
)
420 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
422 if (IsEqualCLSID(rclsid
,&ats
->ats
->LanguageProfile
.clsid
))
425 *profile
= ats
->ats
->LanguageProfile
;
432 HRESULT
activate_textservices(ITfThreadMgr
*tm
)
441 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
443 hr
= activate_given_ts(ats
->ats
, tm
);
445 FIXME("Failed to activate text service\n");
450 HRESULT
deactivate_textservices(void)
458 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
459 deactivate_given_ts(ats
->ats
);
464 CLSID
get_textservice_clsid(TfClientId tid
)
468 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
469 if (ats
->ats
->tid
== tid
)
470 return ats
->ats
->LanguageProfile
.clsid
;
474 HRESULT
get_textservice_sink(TfClientId tid
, REFCLSID iid
, IUnknown
**sink
)
478 if (!IsEqualCLSID(iid
,&IID_ITfKeyEventSink
))
479 return E_NOINTERFACE
;
481 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
482 if (ats
->ats
->tid
== tid
)
484 *sink
= (IUnknown
*)ats
->ats
->pITfKeyEventSink
;
491 HRESULT
set_textservice_sink(TfClientId tid
, REFCLSID iid
, IUnknown
* sink
)
495 if (!IsEqualCLSID(iid
,&IID_ITfKeyEventSink
))
496 return E_NOINTERFACE
;
498 LIST_FOR_EACH_ENTRY(ats
, &AtsList
, AtsEntry
, entry
)
499 if (ats
->ats
->tid
== tid
)
501 ats
->ats
->pITfKeyEventSink
= (ITfKeyEventSink
*)sink
;
508 /*************************************************************************
511 BOOL WINAPI
DllMain(HINSTANCE hinst
, DWORD fdwReason
, LPVOID fImpLoad
)
513 TRACE("%p 0x%x %p\n", hinst
, fdwReason
, fImpLoad
);
516 case DLL_WINE_PREATTACH
:
517 return FALSE
; /* prefer native version */
518 case DLL_PROCESS_ATTACH
:
519 MSCTF_hinstance
= hinst
;
520 tlsIndex
= TlsAlloc();
522 case DLL_PROCESS_DETACH
:
529 /*************************************************************************
530 * DllCanUnloadNow (MSCTF.@)
532 HRESULT WINAPI
DllCanUnloadNow(void)
534 return MSCTF_refCount
? S_FALSE
: S_OK
;
537 /***********************************************************************
538 * DllGetClassObject (MSCTF.@)
540 HRESULT WINAPI
DllGetClassObject(REFCLSID clsid
, REFIID iid
, LPVOID
*ppvOut
)
545 if (!IsEqualIID(iid
, &IID_IUnknown
) && !IsEqualIID(iid
, &IID_IClassFactory
))
546 return E_NOINTERFACE
;
548 for (i
= 0; ClassesTable
[i
].clsid
!= NULL
; i
++)
549 if (IsEqualCLSID(ClassesTable
[i
].clsid
, clsid
)) {
550 return ClassFactory_Constructor(ClassesTable
[i
].ctor
, ppvOut
);
552 FIXME("CLSID %s not supported\n", debugstr_guid(clsid
));
553 return CLASS_E_CLASSNOTAVAILABLE
;
556 /***********************************************************************
557 * TF_CreateThreadMgr (MSCTF.@)
559 HRESULT WINAPI
TF_CreateThreadMgr(ITfThreadMgr
**pptim
)
562 return ThreadMgr_Constructor(NULL
,(IUnknown
**)pptim
);
565 /***********************************************************************
566 * TF_GetThreadMgr (MSCTF.@)
568 HRESULT WINAPI
TF_GetThreadMgr(ITfThreadMgr
**pptim
)
571 *pptim
= TlsGetValue(tlsIndex
);
574 ITfThreadMgr_AddRef(*pptim
);
579 /***********************************************************************
580 * SetInputScope(MSCTF.@)
582 HRESULT WINAPI
SetInputScope(HWND hwnd
, INT inputscope
)
584 FIXME("STUB: %p %i\n",hwnd
,inputscope
);
588 /***********************************************************************
589 * SetInputScopes(MSCTF.@)
591 HRESULT WINAPI
SetInputScopes(HWND hwnd
, const INT
*pInputScopes
,
592 UINT cInputScopes
, WCHAR
**ppszPhraseList
,
593 UINT cPhrases
, WCHAR
*pszRegExp
, WCHAR
*pszSRGS
)
596 FIXME("STUB: %p ... %s %s\n",hwnd
, debugstr_w(pszRegExp
), debugstr_w(pszSRGS
));
597 for (i
= 0; i
< cInputScopes
; i
++)
598 TRACE("\tScope[%i] = %i\n",i
,pInputScopes
[i
]);
599 for (i
= 0; i
< cPhrases
; i
++)
600 TRACE("\tPhrase[%i] = %s\n",i
,debugstr_w(ppszPhraseList
[i
]));
605 /***********************************************************************
606 * TF_CreateInputProcessorProfiles(MSCTF.@)
608 HRESULT WINAPI
TF_CreateInputProcessorProfiles(
609 ITfInputProcessorProfiles
**ppipr
)
611 return InputProcessorProfiles_Constructor(NULL
,(IUnknown
**)ppipr
);
614 /***********************************************************************
615 * TF_InvalidAssemblyListCacheIfExist(MSCTF.@)
617 HRESULT WINAPI
TF_InvalidAssemblyListCacheIfExist(void)
623 /***********************************************************************
624 * TF_CreateLangBarMgr (MSCTF.@)
626 HRESULT WINAPI
TF_CreateLangBarMgr(ITfLangBarMgr
**pppbm
)
629 return LangBarMgr_Constructor(NULL
,(IUnknown
**)pppbm
);