Skip to content

Commit

Permalink
restore system notifier compartment
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhngtu committed Jan 28, 2024
1 parent 293750d commit 901ccb3
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 0 deletions.
9 changes: 9 additions & 0 deletions VietTypeATL/EngineController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static const GUID GUID_SettingsCompartment_Toggle = {
// {CCA3D390-EF1A-4DE4-B2FF-B6BC76D68C3B}
static const GUID GUID_LanguageBarButton_Item = {
0xcca3d390, 0xef1a, 0x4de4, {0xb2, 0xff, 0xb6, 0xbc, 0x76, 0xd6, 0x8c, 0x3b}};
// {B2FBD2E7-922F-4996-BE77-21085B91A8F0}
static const GUID GUID_SystemNotifyCompartment = {
0xb2fbd2e7, 0x922f, 0x4996, {0xbe, 0x77, 0x21, 0x8, 0x5b, 0x91, 0xa8, 0xf0}};

_Check_return_ HRESULT
EngineController::Initialize(_In_ Telex::TelexEngine* engine, _In_ ITfThreadMgr* threadMgr, _In_ TfClientId clientid) {
Expand All @@ -31,6 +34,9 @@ EngineController::Initialize(_In_ Telex::TelexEngine* engine, _In_ ITfThreadMgr*
// init settings compartment & listener
hr = CreateInitialize(&_settings, this, threadMgr, clientid);
HRESULT_CHECK_RETURN(hr, L"%s", L"CreateInitialize(_settings) failed");
hr = CreateInitialize(
&_systemNotify, threadMgr, clientid, GUID_SystemNotifyCompartment, true, [this] { return UpdateStates(true); });
DBG_HRESULT_CHECK(hr, L"%s", L"_systemNotify->Initialize failed");
// must cache defaultEnabled early since it's used right away
_settings->IsDefaultEnabled(&_defaultEnabled);

Expand Down Expand Up @@ -59,6 +65,9 @@ HRESULT EngineController::Uninitialize() {
DBG_HRESULT_CHECK(hr, L"%s", L"_enabled->Uninitialize failed");
_enabled.Release();

hr = _systemNotify->Uninitialize();
DBG_HRESULT_CHECK(hr, L"%s", L"_systemNotify->Uninitialize failed");
_systemNotify.Release();
hr = _settings->Uninitialize();
DBG_HRESULT_CHECK(hr, L"%s", L"_settings->Uninitialize failed");
_settings.Release();
Expand Down
1 change: 1 addition & 0 deletions VietTypeATL/EngineController.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class EngineController : public CComObjectRootEx<CComSingleThreadModel> {
// cached settings
DWORD _defaultEnabled = 0;
DWORD _backconvertOnBackspace = 0;
CComPtr<CompartmentNotifier> _systemNotify;

BlockedKind _blocked = BlockedKind::Free;
};
Expand Down
52 changes: 52 additions & 0 deletions VietTypeATL/SettingsStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,58 @@ class NotifiedSetting : public ITfCompartmentEventSink {
callback_type _callback = [] { return S_OK; };
};

class CompartmentNotifier : public CComObjectRootEx<CComSingleThreadModel>, public NotifiedSetting<long> {
public:
DECLARE_NO_REGISTRY()
DECLARE_NOT_AGGREGATABLE(CompartmentNotifier)
BEGIN_COM_MAP(CompartmentNotifier)
COM_INTERFACE_ENTRY(ITfCompartmentEventSink)
END_COM_MAP()
DECLARE_PROTECT_FINAL_CONSTRUCT()

// Inherited via Setting
virtual _Check_return_ HRESULT GetValue(_Out_ long* val) override {
return _compartment.GetValue(val);
}
virtual _Check_return_ HRESULT GetValueOrWriteback(_Out_ long* val, const long& defaultValue) override {
return _compartment.GetValueOrWriteback(val, defaultValue);
}
virtual HRESULT SetValue(const long& val) override {
return _compartment.SetValue(val);
}
HRESULT Increment() {
long val;
HRESULT hr = GetValueOrWriteback(&val, 0);
if (SUCCEEDED(hr)) {
return SetValue(static_cast<unsigned long>(val) + 1);
} else {
return hr;
}
}

_Check_return_ HRESULT Initialize(
_In_ IUnknown* punk,
_In_ TfClientId clientid,
_In_ const GUID& guidCompartment,
_In_ bool global = false,
_In_ NotifiedSetting<long>::callback_type callback = [] { return S_OK; }) {

_guidCompartment = guidCompartment;
_callback = callback;

return SettingsStore::InitializeSink(
punk, clientid, guidCompartment, _compartment, _compartmentEventSink, this, global);
}

HRESULT Uninitialize() {
return SettingsStore::UninitializeSink(_compartment, _compartmentEventSink);
}

private:
Compartment<long> _compartment;
SinkAdvisor<ITfCompartmentEventSink> _compartmentEventSink;
};

template <typename T>
class CachedCompartmentSetting : public CComObjectRootEx<CComSingleThreadModel>, public NotifiedSetting<T> {
public:
Expand Down
34 changes: 34 additions & 0 deletions VietTypeConfig/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,40 @@
using System.Runtime.InteropServices;

namespace VietTypeConfig {
[ComImport]
[Guid("aa80e801-2021-11d2-93e0-0060b067b86e")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITfThreadMgr {
int Activate(out uint tid);
int Deactivate();
int CreateDocumentMgr(out object ppdim);
int EnumDocumentMgrs(out object ppEnum);
int GetFocus(out object ppdimFocus);
int SetFocus(out object pdimFocus);
int AssociateFocus(IntPtr hwnd, ref object pdimNew, out object ppdimPrev);
int IsThreadFocus(out bool pfThreadFocus);
int GetFunctionProvider(ref Guid clsid, out object ppFuncProv);
int EnumFunctionProviders(out object ppEnum);
int GetGlobalCompartment(out ITfCompartmentMgr ppCompMgr);
}

[ComImport]
[Guid("7dcf57ac-18ad-438b-824d-979bffb74b7c")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITfCompartmentMgr {
int GetCompartment(ref Guid rguid, out ITfCompartment compartment);
int ClearCompartment(uint tid, ref Guid rguid);
int EnumCompartments(out object ppEnum);
}

[ComImport]
[Guid("bb08f7a9-607a-4384-8623-056892b64371")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface ITfCompartment {
int SetValue(uint tid, ref object value);
int GetValue(out object value);
}

internal static class VietTypeRegistrar {
public const int S_OK = 0;
public const int S_FALSE = 1;
Expand Down
15 changes: 15 additions & 0 deletions VietTypeConfig/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace VietTypeConfig {
internal class Settings : INotifyPropertyChanged {
public const string Subkey = "Software\\VietType";
private static readonly Guid GUID_SystemNotifyCompartment = Guid.Parse("{B2FBD2E7-922F-4996-BE77-21085B91A8F0}");
private static readonly Guid CLSID_TF_ThreadMgr = Guid.Parse("{529a9e6b-6587-4f23-ab9e-9c7d683e3c50}");
private static readonly Guid CLSID_TextService = Guid.Parse("{c0dd01a1-0deb-454b-8b42-d22ced1b4b23}");
private static readonly Guid GUID_Profile = Guid.Parse("{8D93D10A-203B-4C5F-A122-8898EF9C56F5}");
Expand Down Expand Up @@ -148,6 +149,20 @@ public static void SaveSettings(Settings settings) {
regKey.SetValue(nameof(backconvert_on_backspace), settings.BackconvertOnBackspace ? 1 : 0);
regKey.SetValue(nameof(optimize_multilang), settings.OptimizeMultilang);
regKey.SetValue(nameof(autocorrect), settings.Autocorrect ? 1 : 0);

var threadMgr = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_TF_ThreadMgr, true)) as ITfThreadMgr;
if (threadMgr.Activate(out uint tid) >= 0) {
try {
if (threadMgr.GetGlobalCompartment(out ITfCompartmentMgr globalMgr) >= 0 &&
globalMgr.GetCompartment(GUID_SystemNotifyCompartment, out ITfCompartment globalCompartment) >= 0) {
if (globalCompartment.GetValue(out object oldGlobal) >= 0) {
globalCompartment.SetValue(tid, unchecked((oldGlobal as int? ?? 0) + 1));
}
}
} finally {
threadMgr.Deactivate();
}
}
}
}

Expand Down

0 comments on commit 901ccb3

Please sign in to comment.