dmband/tests: Move the dll availability check to a separate function.
[wine/wine-gecko.git] / dlls / msctf / inputprocessor.c
blob5115a845ba1785c3759c291bc04f3af36cfca594
1 /*
2 * ITfInputProcessorProfiles implementation
4 * Copyright 2009 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
21 #include "config.h"
23 #include <stdarg.h>
25 #define COBJMACROS
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"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
40 #include "msctf.h"
41 #include "msctf_internal.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
45 static const WCHAR szwLngp[] = {'L','a','n','g','u','a','g','e','P','r','o','f','i','l','e',0};
46 static const WCHAR szwEnable[] = {'E','n','a','b','l','e',0};
47 static const WCHAR szwTipfmt[] = {'%','s','\\','%','s',0};
48 static const WCHAR szwFullLangfmt[] = {'%','s','\\','%','s','\\','%','s','\\','0','x','%','0','8','x','\\','%','s',0};
50 static const WCHAR szwAssemblies[] = {'A','s','s','e','m','b','l','i','e','s',0};
51 static const WCHAR szwDefault[] = {'D','e','f','a','u','l','t',0};
52 static const WCHAR szwProfile[] = {'P','r','o','f','i','l','e',0};
53 static const WCHAR szwDefaultFmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x','\\','%','s',0};
55 typedef struct tagInputProcessorProfilesSink {
56 struct list entry;
57 union {
58 /* InputProcessorProfile Sinks */
59 IUnknown *pIUnknown;
60 ITfLanguageProfileNotifySink *pITfLanguageProfileNotifySink;
61 } interfaces;
62 } InputProcessorProfilesSink;
64 typedef struct tagInputProcessorProfiles {
65 ITfInputProcessorProfiles ITfInputProcessorProfiles_iface;
66 ITfSource ITfSource_iface;
67 /* const ITfInputProcessorProfileMgrVtbl *InputProcessorProfileMgrVtbl; */
68 /* const ITfInputProcessorProfilesExVtbl *InputProcessorProfilesExVtbl; */
69 /* const ITfInputProcessorProfileSubstituteLayoutVtbl *InputProcessorProfileSubstituteLayoutVtbl; */
70 LONG refCount;
72 LANGID currentLanguage;
74 struct list LanguageProfileNotifySink;
75 } InputProcessorProfiles;
77 typedef struct tagProfilesEnumGuid {
78 IEnumGUID IEnumGUID_iface;
79 LONG refCount;
81 HKEY key;
82 DWORD next_index;
83 } ProfilesEnumGuid;
85 typedef struct tagEnumTfLanguageProfiles {
86 IEnumTfLanguageProfiles IEnumTfLanguageProfiles_iface;
87 LONG refCount;
89 HKEY tipkey;
90 DWORD tip_index;
91 WCHAR szwCurrentClsid[39];
93 HKEY langkey;
94 DWORD lang_index;
96 LANGID langid;
97 ITfCategoryMgr *catmgr;
98 } EnumTfLanguageProfiles;
100 static HRESULT ProfilesEnumGuid_Constructor(IEnumGUID **ppOut);
101 static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguageProfiles **ppOut);
103 static inline InputProcessorProfiles *impl_from_ITfInputProcessorProfiles(ITfInputProcessorProfiles *iface)
105 return CONTAINING_RECORD(iface, InputProcessorProfiles, ITfInputProcessorProfiles_iface);
108 static inline InputProcessorProfiles *impl_from_ITfSource(ITfSource *iface)
110 return CONTAINING_RECORD(iface, InputProcessorProfiles, ITfSource_iface);
113 static inline ProfilesEnumGuid *impl_from_IEnumGUID(IEnumGUID *iface)
115 return CONTAINING_RECORD(iface, ProfilesEnumGuid, IEnumGUID_iface);
118 static inline EnumTfLanguageProfiles *impl_from_IEnumTfLanguageProfiles(IEnumTfLanguageProfiles *iface)
120 return CONTAINING_RECORD(iface, EnumTfLanguageProfiles, IEnumTfLanguageProfiles_iface);
123 static void free_sink(InputProcessorProfilesSink *sink)
125 IUnknown_Release(sink->interfaces.pIUnknown);
126 HeapFree(GetProcessHeap(),0,sink);
129 static void InputProcessorProfiles_Destructor(InputProcessorProfiles *This)
131 struct list *cursor, *cursor2;
132 TRACE("destroying %p\n", This);
134 /* free sinks */
135 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->LanguageProfileNotifySink)
137 InputProcessorProfilesSink* sink = LIST_ENTRY(cursor,InputProcessorProfilesSink,entry);
138 list_remove(cursor);
139 free_sink(sink);
142 HeapFree(GetProcessHeap(),0,This);
145 static void add_userkey( REFCLSID rclsid, LANGID langid,
146 REFGUID guidProfile)
148 HKEY key;
149 WCHAR buf[39];
150 WCHAR buf2[39];
151 WCHAR fullkey[168];
152 DWORD disposition = 0;
153 ULONG res;
155 TRACE("\n");
157 StringFromGUID2(rclsid, buf, 39);
158 StringFromGUID2(guidProfile, buf2, 39);
159 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
161 res = RegCreateKeyExW(HKEY_CURRENT_USER,fullkey, 0, NULL, 0,
162 KEY_READ | KEY_WRITE, NULL, &key, &disposition);
164 if (!res && disposition == REG_CREATED_NEW_KEY)
166 DWORD zero = 0x0;
167 RegSetValueExW(key, szwEnable, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
170 if (!res)
171 RegCloseKey(key);
174 static HRESULT WINAPI InputProcessorProfiles_QueryInterface(ITfInputProcessorProfiles *iface, REFIID iid, LPVOID *ppvOut)
176 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
177 *ppvOut = NULL;
179 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfInputProcessorProfiles))
181 *ppvOut = &This->ITfInputProcessorProfiles_iface;
183 else if (IsEqualIID(iid, &IID_ITfSource))
185 *ppvOut = &This->ITfSource_iface;
188 if (*ppvOut)
190 ITfInputProcessorProfiles_AddRef(iface);
191 return S_OK;
194 WARN("unsupported interface: %s\n", debugstr_guid(iid));
195 return E_NOINTERFACE;
198 static ULONG WINAPI InputProcessorProfiles_AddRef(ITfInputProcessorProfiles *iface)
200 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
201 return InterlockedIncrement(&This->refCount);
204 static ULONG WINAPI InputProcessorProfiles_Release(ITfInputProcessorProfiles *iface)
206 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
207 ULONG ret;
209 ret = InterlockedDecrement(&This->refCount);
210 if (ret == 0)
211 InputProcessorProfiles_Destructor(This);
212 return ret;
215 /*****************************************************
216 * ITfInputProcessorProfiles functions
217 *****************************************************/
218 static HRESULT WINAPI InputProcessorProfiles_Register(
219 ITfInputProcessorProfiles *iface, REFCLSID rclsid)
221 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
222 HKEY tipkey;
223 WCHAR buf[39];
224 WCHAR fullkey[68];
226 TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
228 StringFromGUID2(rclsid, buf, 39);
229 sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
231 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, NULL, 0,
232 KEY_READ | KEY_WRITE, NULL, &tipkey, NULL) != ERROR_SUCCESS)
233 return E_FAIL;
235 RegCloseKey(tipkey);
237 return S_OK;
240 static HRESULT WINAPI InputProcessorProfiles_Unregister(
241 ITfInputProcessorProfiles *iface, REFCLSID rclsid)
243 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
244 WCHAR buf[39];
245 WCHAR fullkey[68];
247 TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
249 StringFromGUID2(rclsid, buf, 39);
250 sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
252 RegDeleteTreeW(HKEY_LOCAL_MACHINE, fullkey);
253 RegDeleteTreeW(HKEY_CURRENT_USER, fullkey);
255 return S_OK;
258 static HRESULT WINAPI InputProcessorProfiles_AddLanguageProfile(
259 ITfInputProcessorProfiles *iface, REFCLSID rclsid,
260 LANGID langid, REFGUID guidProfile, const WCHAR *pchDesc,
261 ULONG cchDesc, const WCHAR *pchIconFile, ULONG cchFile,
262 ULONG uIconIndex)
264 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
265 HKEY tipkey,fmtkey;
266 WCHAR buf[39];
267 WCHAR fullkey[100];
268 ULONG res;
269 DWORD disposition = 0;
271 static const WCHAR fmt2[] = {'%','s','\\','0','x','%','0','8','x','\\','%','s',0};
272 static const WCHAR desc[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
273 static const WCHAR icnf[] = {'I','c','o','n','F','i','l','e',0};
274 static const WCHAR icni[] = {'I','c','o','n','I','n','d','e','x',0};
276 TRACE("(%p) %s %x %s %s %s %i\n",This,debugstr_guid(rclsid), langid,
277 debugstr_guid(guidProfile), debugstr_wn(pchDesc,cchDesc),
278 debugstr_wn(pchIconFile,cchFile),uIconIndex);
280 StringFromGUID2(rclsid, buf, 39);
281 sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
283 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, KEY_READ | KEY_WRITE,
284 &tipkey ) != ERROR_SUCCESS)
285 return E_FAIL;
287 StringFromGUID2(guidProfile, buf, 39);
288 sprintfW(fullkey,fmt2,szwLngp,langid,buf);
290 res = RegCreateKeyExW(tipkey,fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
291 NULL, &fmtkey, &disposition);
293 if (!res)
295 DWORD zero = 0x0;
296 RegSetValueExW(fmtkey, desc, 0, REG_SZ, (const BYTE*)pchDesc, cchDesc * sizeof(WCHAR));
297 RegSetValueExW(fmtkey, icnf, 0, REG_SZ, (const BYTE*)pchIconFile, cchFile * sizeof(WCHAR));
298 RegSetValueExW(fmtkey, icni, 0, REG_DWORD, (LPBYTE)&uIconIndex, sizeof(DWORD));
299 if (disposition == REG_CREATED_NEW_KEY)
300 RegSetValueExW(fmtkey, szwEnable, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
301 RegCloseKey(fmtkey);
303 add_userkey(rclsid, langid, guidProfile);
305 RegCloseKey(tipkey);
307 if (!res)
308 return S_OK;
309 else
310 return E_FAIL;
313 static HRESULT WINAPI InputProcessorProfiles_RemoveLanguageProfile(
314 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
315 REFGUID guidProfile)
317 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
318 FIXME("STUB:(%p)\n",This);
319 return E_NOTIMPL;
322 static HRESULT WINAPI InputProcessorProfiles_EnumInputProcessorInfo(
323 ITfInputProcessorProfiles *iface, IEnumGUID **ppEnum)
325 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
326 TRACE("(%p) %p\n",This,ppEnum);
327 return ProfilesEnumGuid_Constructor(ppEnum);
330 static HRESULT WINAPI InputProcessorProfiles_GetDefaultLanguageProfile(
331 ITfInputProcessorProfiles *iface, LANGID langid, REFGUID catid,
332 CLSID *pclsid, GUID *pguidProfile)
334 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
335 WCHAR fullkey[168];
336 WCHAR buf[39];
337 HKEY hkey;
338 DWORD count;
339 ULONG res;
341 TRACE("%p) %x %s %p %p\n",This, langid, debugstr_guid(catid),pclsid,pguidProfile);
343 if (!catid || !pclsid || !pguidProfile)
344 return E_INVALIDARG;
346 StringFromGUID2(catid, buf, 39);
347 sprintfW(fullkey, szwDefaultFmt, szwSystemCTFKey, szwAssemblies, langid, buf);
349 if (RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE,
350 &hkey ) != ERROR_SUCCESS)
351 return S_FALSE;
353 count = sizeof(buf);
354 res = RegQueryValueExW(hkey, szwDefault, 0, NULL, (LPBYTE)buf, &count);
355 if (res != ERROR_SUCCESS)
357 RegCloseKey(hkey);
358 return S_FALSE;
360 CLSIDFromString(buf,pclsid);
362 res = RegQueryValueExW(hkey, szwProfile, 0, NULL, (LPBYTE)buf, &count);
363 if (res == ERROR_SUCCESS)
364 CLSIDFromString(buf,pguidProfile);
366 RegCloseKey(hkey);
368 return S_OK;
371 static HRESULT WINAPI InputProcessorProfiles_SetDefaultLanguageProfile(
372 ITfInputProcessorProfiles *iface, LANGID langid, REFCLSID rclsid,
373 REFGUID guidProfiles)
375 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
376 WCHAR fullkey[168];
377 WCHAR buf[39];
378 HKEY hkey;
379 GUID catid;
380 HRESULT hr;
381 ITfCategoryMgr *catmgr;
382 static const GUID * tipcats[3] = { &GUID_TFCAT_TIP_KEYBOARD,
383 &GUID_TFCAT_TIP_SPEECH,
384 &GUID_TFCAT_TIP_HANDWRITING };
386 TRACE("%p) %x %s %s\n",This, langid, debugstr_guid(rclsid),debugstr_guid(guidProfiles));
388 if (!rclsid || !guidProfiles)
389 return E_INVALIDARG;
391 hr = CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr);
393 if (FAILED(hr))
394 return hr;
396 if (ITfCategoryMgr_FindClosestCategory(catmgr, rclsid,
397 &catid, tipcats, 3) != S_OK)
398 hr = ITfCategoryMgr_FindClosestCategory(catmgr, rclsid,
399 &catid, NULL, 0);
400 ITfCategoryMgr_Release(catmgr);
402 if (FAILED(hr))
403 return E_FAIL;
405 StringFromGUID2(&catid, buf, 39);
406 sprintfW(fullkey, szwDefaultFmt, szwSystemCTFKey, szwAssemblies, langid, buf);
408 if (RegCreateKeyExW(HKEY_CURRENT_USER, fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
409 NULL, &hkey, NULL ) != ERROR_SUCCESS)
410 return E_FAIL;
412 StringFromGUID2(rclsid, buf, 39);
413 RegSetValueExW(hkey, szwDefault, 0, REG_SZ, (LPBYTE)buf, sizeof(buf));
414 StringFromGUID2(guidProfiles, buf, 39);
415 RegSetValueExW(hkey, szwProfile, 0, REG_SZ, (LPBYTE)buf, sizeof(buf));
416 RegCloseKey(hkey);
418 return S_OK;
421 static HRESULT WINAPI InputProcessorProfiles_ActivateLanguageProfile(
422 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
423 REFGUID guidProfiles)
425 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
426 HRESULT hr;
427 BOOL enabled;
428 TF_LANGUAGEPROFILE LanguageProfile;
430 TRACE("(%p) %s %x %s\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfiles));
432 if (langid != This->currentLanguage) return E_INVALIDARG;
434 if (get_active_textservice(rclsid,NULL))
436 TRACE("Already Active\n");
437 return E_FAIL;
440 hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(iface, rclsid,
441 langid, guidProfiles, &enabled);
442 if (FAILED(hr) || !enabled)
444 TRACE("Not Enabled\n");
445 return E_FAIL;
448 LanguageProfile.clsid = *rclsid;
449 LanguageProfile.langid = langid;
450 LanguageProfile.guidProfile = *guidProfiles;
452 hr = add_active_textservice(&LanguageProfile);
454 return hr;
457 static HRESULT WINAPI InputProcessorProfiles_GetActiveLanguageProfile(
458 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID *plangid,
459 GUID *pguidProfile)
461 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
462 TF_LANGUAGEPROFILE profile;
464 TRACE("(%p) %s %p %p\n",This,debugstr_guid(rclsid),plangid,pguidProfile);
466 if (!rclsid || !plangid || !pguidProfile)
467 return E_INVALIDARG;
469 if (get_active_textservice(rclsid, &profile))
471 *plangid = profile.langid;
472 *pguidProfile = profile.guidProfile;
473 return S_OK;
475 else
477 *pguidProfile = GUID_NULL;
478 return S_FALSE;
482 static HRESULT WINAPI InputProcessorProfiles_GetLanguageProfileDescription(
483 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
484 REFGUID guidProfile, BSTR *pbstrProfile)
486 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
487 FIXME("STUB:(%p)\n",This);
488 return E_NOTIMPL;
491 static HRESULT WINAPI InputProcessorProfiles_GetCurrentLanguage(
492 ITfInputProcessorProfiles *iface, LANGID *plangid)
494 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
495 TRACE("(%p) 0x%x\n",This,This->currentLanguage);
497 if (!plangid)
498 return E_INVALIDARG;
500 *plangid = This->currentLanguage;
502 return S_OK;
505 static HRESULT WINAPI InputProcessorProfiles_ChangeCurrentLanguage(
506 ITfInputProcessorProfiles *iface, LANGID langid)
508 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
509 struct list *cursor;
510 BOOL accept;
512 FIXME("STUB:(%p)\n",This);
514 LIST_FOR_EACH(cursor, &This->LanguageProfileNotifySink)
516 InputProcessorProfilesSink* sink = LIST_ENTRY(cursor,InputProcessorProfilesSink,entry);
517 accept = TRUE;
518 ITfLanguageProfileNotifySink_OnLanguageChange(sink->interfaces.pITfLanguageProfileNotifySink, langid, &accept);
519 if (!accept)
520 return E_FAIL;
523 /* TODO: On successful language change call OnLanguageChanged sink */
524 return E_NOTIMPL;
527 static HRESULT WINAPI InputProcessorProfiles_GetLanguageList(
528 ITfInputProcessorProfiles *iface, LANGID **ppLangId, ULONG *pulCount)
530 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
531 FIXME("Semi-STUB:(%p)\n",This);
532 *ppLangId = CoTaskMemAlloc(sizeof(LANGID));
533 **ppLangId = This->currentLanguage;
534 *pulCount = 1;
535 return S_OK;
538 static HRESULT WINAPI InputProcessorProfiles_EnumLanguageProfiles(
539 ITfInputProcessorProfiles *iface, LANGID langid,
540 IEnumTfLanguageProfiles **ppEnum)
542 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
543 TRACE("(%p) %x %p\n",This,langid,ppEnum);
544 return EnumTfLanguageProfiles_Constructor(langid, ppEnum);
547 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfile(
548 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
549 REFGUID guidProfile, BOOL fEnable)
551 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
552 HKEY key;
553 WCHAR buf[39];
554 WCHAR buf2[39];
555 WCHAR fullkey[168];
556 ULONG res;
558 TRACE("(%p) %s %x %s %i\n",This, debugstr_guid(rclsid), langid, debugstr_guid(guidProfile), fEnable);
560 StringFromGUID2(rclsid, buf, 39);
561 StringFromGUID2(guidProfile, buf2, 39);
562 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
564 res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
566 if (!res)
568 RegSetValueExW(key, szwEnable, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
569 RegCloseKey(key);
571 else
572 return E_FAIL;
574 return S_OK;
577 static HRESULT WINAPI InputProcessorProfiles_IsEnabledLanguageProfile(
578 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
579 REFGUID guidProfile, BOOL *pfEnable)
581 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
582 HKEY key;
583 WCHAR buf[39];
584 WCHAR buf2[39];
585 WCHAR fullkey[168];
586 ULONG res;
588 TRACE("(%p) %s, %i, %s, %p\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfile),pfEnable);
590 if (!pfEnable)
591 return E_INVALIDARG;
593 StringFromGUID2(rclsid, buf, 39);
594 StringFromGUID2(guidProfile, buf2, 39);
595 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
597 res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
599 if (!res)
601 DWORD count = sizeof(DWORD);
602 res = RegQueryValueExW(key, szwEnable, 0, NULL, (LPBYTE)pfEnable, &count);
603 RegCloseKey(key);
606 if (res) /* Try Default */
608 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, fullkey, 0, KEY_READ | KEY_WRITE, &key);
610 if (!res)
612 DWORD count = sizeof(DWORD);
613 res = RegQueryValueExW(key, szwEnable, 0, NULL, (LPBYTE)pfEnable, &count);
614 RegCloseKey(key);
618 if (!res)
619 return S_OK;
620 else
621 return E_FAIL;
624 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfileByDefault(
625 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
626 REFGUID guidProfile, BOOL fEnable)
628 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
629 HKEY key;
630 WCHAR buf[39];
631 WCHAR buf2[39];
632 WCHAR fullkey[168];
633 ULONG res;
635 TRACE("(%p) %s %x %s %i\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfile),fEnable);
637 StringFromGUID2(rclsid, buf, 39);
638 StringFromGUID2(guidProfile, buf2, 39);
639 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
641 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, fullkey, 0, KEY_READ | KEY_WRITE, &key);
643 if (!res)
645 RegSetValueExW(key, szwEnable, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
646 RegCloseKey(key);
648 else
649 return E_FAIL;
651 return S_OK;
654 static HRESULT WINAPI InputProcessorProfiles_SubstituteKeyboardLayout(
655 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
656 REFGUID guidProfile, HKL hKL)
658 InputProcessorProfiles *This = impl_from_ITfInputProcessorProfiles(iface);
659 FIXME("STUB:(%p)\n",This);
660 return E_NOTIMPL;
663 static const ITfInputProcessorProfilesVtbl InputProcessorProfilesVtbl =
665 InputProcessorProfiles_QueryInterface,
666 InputProcessorProfiles_AddRef,
667 InputProcessorProfiles_Release,
668 InputProcessorProfiles_Register,
669 InputProcessorProfiles_Unregister,
670 InputProcessorProfiles_AddLanguageProfile,
671 InputProcessorProfiles_RemoveLanguageProfile,
672 InputProcessorProfiles_EnumInputProcessorInfo,
673 InputProcessorProfiles_GetDefaultLanguageProfile,
674 InputProcessorProfiles_SetDefaultLanguageProfile,
675 InputProcessorProfiles_ActivateLanguageProfile,
676 InputProcessorProfiles_GetActiveLanguageProfile,
677 InputProcessorProfiles_GetLanguageProfileDescription,
678 InputProcessorProfiles_GetCurrentLanguage,
679 InputProcessorProfiles_ChangeCurrentLanguage,
680 InputProcessorProfiles_GetLanguageList,
681 InputProcessorProfiles_EnumLanguageProfiles,
682 InputProcessorProfiles_EnableLanguageProfile,
683 InputProcessorProfiles_IsEnabledLanguageProfile,
684 InputProcessorProfiles_EnableLanguageProfileByDefault,
685 InputProcessorProfiles_SubstituteKeyboardLayout
688 /*****************************************************
689 * ITfSource functions
690 *****************************************************/
691 static HRESULT WINAPI IPPSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
693 InputProcessorProfiles *This = impl_from_ITfSource(iface);
694 return ITfInputProcessorProfiles_QueryInterface(&This->ITfInputProcessorProfiles_iface, iid, ppvOut);
697 static ULONG WINAPI IPPSource_AddRef(ITfSource *iface)
699 InputProcessorProfiles *This = impl_from_ITfSource(iface);
700 return ITfInputProcessorProfiles_AddRef(&This->ITfInputProcessorProfiles_iface);
703 static ULONG WINAPI IPPSource_Release(ITfSource *iface)
705 InputProcessorProfiles *This = impl_from_ITfSource(iface);
706 return ITfInputProcessorProfiles_Release(&This->ITfInputProcessorProfiles_iface);
709 static HRESULT WINAPI IPPSource_AdviseSink(ITfSource *iface,
710 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
712 InputProcessorProfiles *This = impl_from_ITfSource(iface);
713 InputProcessorProfilesSink *ipps;
715 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
717 if (!riid || !punk || !pdwCookie)
718 return E_INVALIDARG;
720 if (IsEqualIID(riid, &IID_ITfLanguageProfileNotifySink))
722 ipps = HeapAlloc(GetProcessHeap(),0,sizeof(InputProcessorProfilesSink));
723 if (!ipps)
724 return E_OUTOFMEMORY;
725 if (FAILED(IUnknown_QueryInterface(punk, riid, (LPVOID *)&ipps->interfaces.pITfLanguageProfileNotifySink)))
727 HeapFree(GetProcessHeap(),0,ipps);
728 return CONNECT_E_CANNOTCONNECT;
730 list_add_head(&This->LanguageProfileNotifySink,&ipps->entry);
731 *pdwCookie = generate_Cookie(COOKIE_MAGIC_IPPSINK, ipps);
733 else
735 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
736 return E_NOTIMPL;
739 TRACE("cookie %x\n",*pdwCookie);
741 return S_OK;
744 static HRESULT WINAPI IPPSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
746 InputProcessorProfiles *This = impl_from_ITfSource(iface);
747 InputProcessorProfilesSink *sink;
749 TRACE("(%p) %x\n",This,pdwCookie);
751 if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_IPPSINK)
752 return E_INVALIDARG;
754 sink = remove_Cookie(pdwCookie);
755 if (!sink)
756 return CONNECT_E_NOCONNECTION;
758 list_remove(&sink->entry);
759 free_sink(sink);
761 return S_OK;
764 static const ITfSourceVtbl InputProcessorProfilesSourceVtbl =
766 IPPSource_QueryInterface,
767 IPPSource_AddRef,
768 IPPSource_Release,
769 IPPSource_AdviseSink,
770 IPPSource_UnadviseSink,
773 HRESULT InputProcessorProfiles_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
775 InputProcessorProfiles *This;
776 if (pUnkOuter)
777 return CLASS_E_NOAGGREGATION;
779 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputProcessorProfiles));
780 if (This == NULL)
781 return E_OUTOFMEMORY;
783 This->ITfInputProcessorProfiles_iface.lpVtbl= &InputProcessorProfilesVtbl;
784 This->ITfSource_iface.lpVtbl = &InputProcessorProfilesSourceVtbl;
785 This->refCount = 1;
786 This->currentLanguage = GetUserDefaultLCID();
788 list_init(&This->LanguageProfileNotifySink);
790 *ppOut = (IUnknown *)&This->ITfInputProcessorProfiles_iface;
791 TRACE("returning %p\n", *ppOut);
792 return S_OK;
795 /**************************************************
796 * IEnumGUID implementation for ITfInputProcessorProfiles::EnumInputProcessorInfo
797 **************************************************/
798 static void ProfilesEnumGuid_Destructor(ProfilesEnumGuid *This)
800 TRACE("destroying %p\n", This);
801 RegCloseKey(This->key);
802 HeapFree(GetProcessHeap(),0,This);
805 static HRESULT WINAPI ProfilesEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
807 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
808 *ppvOut = NULL;
810 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
812 *ppvOut = &This->IEnumGUID_iface;
815 if (*ppvOut)
817 IEnumGUID_AddRef(iface);
818 return S_OK;
821 WARN("unsupported interface: %s\n", debugstr_guid(iid));
822 return E_NOINTERFACE;
825 static ULONG WINAPI ProfilesEnumGuid_AddRef(IEnumGUID *iface)
827 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
828 return InterlockedIncrement(&This->refCount);
831 static ULONG WINAPI ProfilesEnumGuid_Release(IEnumGUID *iface)
833 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
834 ULONG ret;
836 ret = InterlockedDecrement(&This->refCount);
837 if (ret == 0)
838 ProfilesEnumGuid_Destructor(This);
839 return ret;
842 /*****************************************************
843 * IEnumGuid functions
844 *****************************************************/
845 static HRESULT WINAPI ProfilesEnumGuid_Next( LPENUMGUID iface,
846 ULONG celt, GUID *rgelt, ULONG *pceltFetched)
848 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
849 ULONG fetched = 0;
851 TRACE("(%p)\n",This);
853 if (rgelt == NULL) return E_POINTER;
855 if (This->key) while (fetched < celt)
857 LSTATUS res;
858 HRESULT hr;
859 WCHAR catid[39];
860 DWORD cName = 39;
862 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
863 NULL, NULL, NULL, NULL);
864 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
865 ++(This->next_index);
867 hr = CLSIDFromString(catid, rgelt);
868 if (FAILED(hr)) continue;
870 ++fetched;
871 ++rgelt;
874 if (pceltFetched) *pceltFetched = fetched;
875 return fetched == celt ? S_OK : S_FALSE;
878 static HRESULT WINAPI ProfilesEnumGuid_Skip( LPENUMGUID iface, ULONG celt)
880 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
881 TRACE("(%p)\n",This);
883 This->next_index += celt;
884 return S_OK;
887 static HRESULT WINAPI ProfilesEnumGuid_Reset( LPENUMGUID iface)
889 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
890 TRACE("(%p)\n",This);
891 This->next_index = 0;
892 return S_OK;
895 static HRESULT WINAPI ProfilesEnumGuid_Clone( LPENUMGUID iface,
896 IEnumGUID **ppenum)
898 ProfilesEnumGuid *This = impl_from_IEnumGUID(iface);
899 HRESULT res;
901 TRACE("(%p)\n",This);
903 if (ppenum == NULL) return E_POINTER;
905 res = ProfilesEnumGuid_Constructor(ppenum);
906 if (SUCCEEDED(res))
908 ProfilesEnumGuid *new_This = impl_from_IEnumGUID(*ppenum);
909 new_This->next_index = This->next_index;
911 return res;
914 static const IEnumGUIDVtbl EnumGUIDVtbl =
916 ProfilesEnumGuid_QueryInterface,
917 ProfilesEnumGuid_AddRef,
918 ProfilesEnumGuid_Release,
919 ProfilesEnumGuid_Next,
920 ProfilesEnumGuid_Skip,
921 ProfilesEnumGuid_Reset,
922 ProfilesEnumGuid_Clone
925 static HRESULT ProfilesEnumGuid_Constructor(IEnumGUID **ppOut)
927 ProfilesEnumGuid *This;
929 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ProfilesEnumGuid));
930 if (This == NULL)
931 return E_OUTOFMEMORY;
933 This->IEnumGUID_iface.lpVtbl= &EnumGUIDVtbl;
934 This->refCount = 1;
936 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szwSystemTIPKey, 0, NULL, 0,
937 KEY_READ | KEY_WRITE, NULL, &This->key, NULL) != ERROR_SUCCESS)
939 HeapFree(GetProcessHeap(), 0, This);
940 return E_FAIL;
943 *ppOut = &This->IEnumGUID_iface;
944 TRACE("returning %p\n", *ppOut);
945 return S_OK;
948 /**************************************************
949 * IEnumTfLanguageProfiles implementation
950 **************************************************/
951 static void EnumTfLanguageProfiles_Destructor(EnumTfLanguageProfiles *This)
953 TRACE("destroying %p\n", This);
954 RegCloseKey(This->tipkey);
955 if (This->langkey)
956 RegCloseKey(This->langkey);
957 ITfCategoryMgr_Release(This->catmgr);
958 HeapFree(GetProcessHeap(),0,This);
961 static HRESULT WINAPI EnumTfLanguageProfiles_QueryInterface(IEnumTfLanguageProfiles *iface, REFIID iid, LPVOID *ppvOut)
963 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
965 *ppvOut = NULL;
967 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfLanguageProfiles))
969 *ppvOut = &This->IEnumTfLanguageProfiles_iface;
972 if (*ppvOut)
974 IEnumTfLanguageProfiles_AddRef(iface);
975 return S_OK;
978 WARN("unsupported interface: %s\n", debugstr_guid(iid));
979 return E_NOINTERFACE;
982 static ULONG WINAPI EnumTfLanguageProfiles_AddRef(IEnumTfLanguageProfiles *iface)
984 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
985 return InterlockedIncrement(&This->refCount);
988 static ULONG WINAPI EnumTfLanguageProfiles_Release(IEnumTfLanguageProfiles *iface)
990 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
991 ULONG ret;
993 ret = InterlockedDecrement(&This->refCount);
994 if (ret == 0)
995 EnumTfLanguageProfiles_Destructor(This);
996 return ret;
999 /*****************************************************
1000 * IEnumGuid functions
1001 *****************************************************/
1002 static INT next_LanguageProfile(EnumTfLanguageProfiles *This, CLSID clsid, TF_LANGUAGEPROFILE *tflp)
1004 WCHAR fullkey[168];
1005 ULONG res;
1006 WCHAR profileid[39];
1007 DWORD cName = 39;
1008 GUID profile;
1010 static const WCHAR fmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x',0};
1012 if (This->langkey == NULL)
1014 sprintfW(fullkey,fmt,This->szwCurrentClsid,szwLngp,This->langid);
1015 res = RegOpenKeyExW(This->tipkey, fullkey, 0, KEY_READ | KEY_WRITE, &This->langkey);
1016 if (res)
1018 This->langkey = NULL;
1019 return -1;
1021 This->lang_index = 0;
1023 res = RegEnumKeyExW(This->langkey, This->lang_index, profileid, &cName,
1024 NULL, NULL, NULL, NULL);
1025 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
1027 RegCloseKey(This->langkey);
1028 This->langkey = NULL;
1029 return -1;
1031 ++(This->lang_index);
1033 if (tflp)
1035 static const GUID * tipcats[3] = { &GUID_TFCAT_TIP_KEYBOARD,
1036 &GUID_TFCAT_TIP_SPEECH,
1037 &GUID_TFCAT_TIP_HANDWRITING };
1038 res = CLSIDFromString(profileid, &profile);
1039 if (FAILED(res)) return 0;
1041 tflp->clsid = clsid;
1042 tflp->langid = This->langid;
1043 tflp->fActive = get_active_textservice(&clsid, NULL);
1044 tflp->guidProfile = profile;
1045 if (ITfCategoryMgr_FindClosestCategory(This->catmgr, &clsid,
1046 &tflp->catid, tipcats, 3) != S_OK)
1047 ITfCategoryMgr_FindClosestCategory(This->catmgr, &clsid,
1048 &tflp->catid, NULL, 0);
1051 return 1;
1054 static HRESULT WINAPI EnumTfLanguageProfiles_Next(IEnumTfLanguageProfiles *iface,
1055 ULONG ulCount, TF_LANGUAGEPROFILE *pProfile, ULONG *pcFetch)
1057 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
1058 ULONG fetched = 0;
1060 TRACE("(%p)\n",This);
1062 if (pProfile == NULL) return E_POINTER;
1064 if (This->tipkey) while (fetched < ulCount)
1066 LSTATUS res;
1067 HRESULT hr;
1068 DWORD cName = 39;
1069 GUID clsid;
1071 res = RegEnumKeyExW(This->tipkey, This->tip_index,
1072 This->szwCurrentClsid, &cName, NULL, NULL, NULL, NULL);
1073 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1074 ++(This->tip_index);
1075 hr = CLSIDFromString(This->szwCurrentClsid, &clsid);
1076 if (FAILED(hr)) continue;
1078 while ( fetched < ulCount)
1080 INT res = next_LanguageProfile(This, clsid, pProfile);
1081 if (res == 1)
1083 ++fetched;
1084 ++pProfile;
1086 else if (res == -1)
1087 break;
1088 else
1089 continue;
1093 if (pcFetch) *pcFetch = fetched;
1094 return fetched == ulCount ? S_OK : S_FALSE;
1097 static HRESULT WINAPI EnumTfLanguageProfiles_Skip( IEnumTfLanguageProfiles* iface, ULONG celt)
1099 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
1100 FIXME("STUB (%p)\n",This);
1101 return E_NOTIMPL;
1104 static HRESULT WINAPI EnumTfLanguageProfiles_Reset( IEnumTfLanguageProfiles* iface)
1106 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
1107 TRACE("(%p)\n",This);
1108 This->tip_index = 0;
1109 if (This->langkey)
1110 RegCloseKey(This->langkey);
1111 This->langkey = NULL;
1112 This->lang_index = 0;
1113 return S_OK;
1116 static HRESULT WINAPI EnumTfLanguageProfiles_Clone( IEnumTfLanguageProfiles *iface,
1117 IEnumTfLanguageProfiles **ppenum)
1119 EnumTfLanguageProfiles *This = impl_from_IEnumTfLanguageProfiles(iface);
1120 HRESULT res;
1122 TRACE("(%p)\n",This);
1124 if (ppenum == NULL) return E_POINTER;
1126 res = EnumTfLanguageProfiles_Constructor(This->langid, ppenum);
1127 if (SUCCEEDED(res))
1129 EnumTfLanguageProfiles *new_This = (EnumTfLanguageProfiles *)*ppenum;
1130 new_This->tip_index = This->tip_index;
1131 lstrcpynW(new_This->szwCurrentClsid,This->szwCurrentClsid,39);
1133 if (This->langkey)
1135 WCHAR fullkey[168];
1136 static const WCHAR fmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x',0};
1138 sprintfW(fullkey,fmt,This->szwCurrentClsid,szwLngp,This->langid);
1139 res = RegOpenKeyExW(new_This->tipkey, fullkey, 0, KEY_READ | KEY_WRITE, &This->langkey);
1140 new_This->lang_index = This->lang_index;
1143 return res;
1146 static const IEnumTfLanguageProfilesVtbl EnumTfLanguageProfilesVtbl =
1148 EnumTfLanguageProfiles_QueryInterface,
1149 EnumTfLanguageProfiles_AddRef,
1150 EnumTfLanguageProfiles_Release,
1151 EnumTfLanguageProfiles_Clone,
1152 EnumTfLanguageProfiles_Next,
1153 EnumTfLanguageProfiles_Reset,
1154 EnumTfLanguageProfiles_Skip
1157 static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguageProfiles **ppOut)
1159 HRESULT hr;
1160 EnumTfLanguageProfiles *This;
1162 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfLanguageProfiles));
1163 if (This == NULL)
1164 return E_OUTOFMEMORY;
1166 This->IEnumTfLanguageProfiles_iface.lpVtbl= &EnumTfLanguageProfilesVtbl;
1167 This->refCount = 1;
1168 This->langid = langid;
1170 hr = CategoryMgr_Constructor(NULL,(IUnknown**)&This->catmgr);
1171 if (FAILED(hr))
1173 HeapFree(GetProcessHeap(),0,This);
1174 return hr;
1177 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szwSystemTIPKey, 0, NULL, 0,
1178 KEY_READ | KEY_WRITE, NULL, &This->tipkey, NULL) != ERROR_SUCCESS)
1180 HeapFree(GetProcessHeap(), 0, This);
1181 return E_FAIL;
1184 *ppOut = &This->IEnumTfLanguageProfiles_iface;
1185 TRACE("returning %p\n", *ppOut);
1186 return S_OK;