Skip to content

Commit 79fda26

Browse files
committed
refactor(CCompartment): port to Rust
1 parent 2f9a34b commit 79fda26

File tree

10 files changed

+297
-89
lines changed

10 files changed

+297
-89
lines changed

cpp/SampleIME/Compartment.cpp

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Private.h"
99
#include "Compartment.h"
1010
#include "Globals.h"
11+
#include "cbindgen/itf_components.h"
1112

1213
//////////////////////////////////////////////////////////////////////
1314
//
@@ -21,12 +22,7 @@
2122

2223
CCompartment::CCompartment(_In_ IUnknown* punk, TfClientId tfClientId, _In_ REFGUID guidCompartment)
2324
{
24-
_guidCompartment = guidCompartment;
25-
26-
_punk = punk;
27-
_punk->AddRef();
28-
29-
_tfClientId = tfClientId;
25+
compartment = compartment_new(punk, tfClientId, &guidCompartment);
3026
}
3127

3228
//+---------------------------------------------------------------------------
@@ -35,26 +31,7 @@ CCompartment::CCompartment(_In_ IUnknown* punk, TfClientId tfClientId, _In_ REFG
3531

3632
CCompartment::~CCompartment()
3733
{
38-
_punk->Release();
39-
}
40-
41-
//+---------------------------------------------------------------------------
42-
// _GetCompartment
43-
//----------------------------------------------------------------------------
44-
45-
HRESULT CCompartment::_GetCompartment(_Outptr_ ITfCompartment **ppCompartment)
46-
{
47-
HRESULT hr = S_OK;
48-
ITfCompartmentMgr* pCompartmentMgr = nullptr;
49-
50-
hr = _punk->QueryInterface(IID_ITfCompartmentMgr, (void **)&pCompartmentMgr);
51-
if (SUCCEEDED(hr))
52-
{
53-
hr = pCompartmentMgr->GetCompartment(_guidCompartment, ppCompartment);
54-
pCompartmentMgr->Release();
55-
}
56-
57-
return hr;
34+
compartment_free(compartment);
5835
}
5936

6037
//+---------------------------------------------------------------------------
@@ -63,10 +40,7 @@ HRESULT CCompartment::_GetCompartment(_Outptr_ ITfCompartment **ppCompartment)
6340

6441
HRESULT CCompartment::_GetCompartmentBOOL(bool& flag)
6542
{
66-
uint32_t dwValue = 0;
67-
HRESULT hr = _GetCompartmentU32(dwValue);
68-
flag = (dwValue != 0);
69-
return hr;
43+
return compartment_get_bool(compartment, &flag);
7044
}
7145

7246
//+---------------------------------------------------------------------------
@@ -84,29 +58,7 @@ HRESULT CCompartment::_SetCompartmentBOOL(bool flag)
8458

8559
HRESULT CCompartment::_GetCompartmentU32(uint32_t& dw)
8660
{
87-
HRESULT hr = S_OK;
88-
ITfCompartment* pCompartment = nullptr;
89-
dw = 0;
90-
91-
hr = _GetCompartment(&pCompartment);
92-
if (SUCCEEDED(hr))
93-
{
94-
VARIANT var;
95-
if ((hr = pCompartment->GetValue(&var)) == S_OK)
96-
{
97-
if (var.vt == VT_I4) // Even VT_EMPTY, GetValue() can succeed
98-
{
99-
dw = var.lVal;
100-
}
101-
else
102-
{
103-
hr = S_FALSE;
104-
}
105-
}
106-
pCompartment->Release();
107-
}
108-
109-
return hr;
61+
return compartment_get_u32(compartment, &dw);
11062
}
11163

11264
//+---------------------------------------------------------------------------
@@ -115,20 +67,7 @@ HRESULT CCompartment::_GetCompartmentU32(uint32_t& dw)
11567

11668
HRESULT CCompartment::_SetCompartmentU32(uint32_t dw)
11769
{
118-
HRESULT hr = S_OK;
119-
ITfCompartment* pCompartment = nullptr;
120-
121-
hr = _GetCompartment(&pCompartment);
122-
if (SUCCEEDED(hr))
123-
{
124-
VARIANT var;
125-
var.vt = VT_I4;
126-
var.lVal = dw;
127-
hr = pCompartment->SetValue(_tfClientId, &var);
128-
pCompartment->Release();
129-
}
130-
131-
return hr;
70+
return compartment_set_u32(compartment, dw);
13271
}
13372

13473
//+---------------------------------------------------------------------------
@@ -139,21 +78,11 @@ HRESULT CCompartment::_SetCompartmentU32(uint32_t dw)
13978

14079
HRESULT CCompartment::_ClearCompartment()
14180
{
142-
if (IsEqualGUID(_guidCompartment, GUID_COMPARTMENT_KEYBOARD_OPENCLOSE))
143-
{
144-
return S_FALSE;
145-
}
146-
147-
HRESULT hr = S_OK;
148-
ITfCompartmentMgr* pCompartmentMgr = nullptr;
149-
150-
if ((hr = _punk->QueryInterface(IID_ITfCompartmentMgr, (void **)&pCompartmentMgr)) == S_OK)
151-
{
152-
hr = pCompartmentMgr->ClearCompartment(_tfClientId, _guidCompartment);
153-
pCompartmentMgr->Release();
154-
}
81+
return compartment_clear(compartment);
82+
}
15583

156-
return hr;
84+
void CCompartment::_GetGUID(GUID* pguid) const {
85+
compartment_guid(compartment, pguid);
15786
}
15887

15988
//////////////////////////////////////////////////////////////////////

cpp/SampleIME/Compartment.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,10 @@ class CCompartment
2222
HRESULT _SetCompartmentU32(uint32_t dw);
2323
HRESULT _ClearCompartment();
2424

25-
VOID _GetGUID(GUID *pguid)
26-
{
27-
*pguid = _guidCompartment;
28-
}
25+
void _GetGUID(GUID* pguid) const;
2926

3027
private:
31-
GUID _guidCompartment;
32-
IUnknown* _punk;
33-
TfClientId _tfClientId;
28+
void* compartment;
3429
};
3530

3631
typedef HRESULT (*CESCALLBACK)(void *pv, REFGUID guidCompartment);

cpp/SampleIME/SampleIME.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
<SubSystem>Windows</SubSystem>
202202
<GenerateDebugInformation>true</GenerateDebugInformation>
203203
<AdditionalLibraryDirectories>$(MSBuildProgramFiles32)\Windows Kits\8.0\Lib\win8\um\x64;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
204-
<AdditionalDependencies>shlwapi.lib;$(SolutionDir)../rust/target/debug/compare_with_wildcard.lib;$(SolutionDir)../rust/target/debug/composition_processor.lib;$(SolutionDir)../rust/target/debug/dictionary_parser.lib;$(SolutionDir)../rust/target/debug/globals.lib;$(SolutionDir)../rust/target/debug/ime.lib;$(SolutionDir)../rust/target/debug/numberkey_windows.lib;$(SolutionDir)../rust/target/debug/ruststringrange.lib;%(AdditionalDependencies)</AdditionalDependencies>
204+
<AdditionalDependencies>shlwapi.lib;$(SolutionDir)../rust/target/debug/compare_with_wildcard.lib;$(SolutionDir)../rust/target/debug/composition_processor.lib;$(SolutionDir)../rust/target/debug/dictionary_parser.lib;$(SolutionDir)../rust/target/debug/globals.lib;$(SolutionDir)../rust/target/debug/ime.lib;$(SolutionDir)../rust/target/debug/itf_components.lib;$(SolutionDir)../rust/target/debug/numberkey_windows.lib;$(SolutionDir)../rust/target/debug/ruststringrange.lib;%(AdditionalDependencies)</AdditionalDependencies>
205205
<ModuleDefinitionFile>SampleIME.def</ModuleDefinitionFile>
206206
</Link>
207207
<PreBuildEvent>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <cstdarg>
2+
#include <cstdint>
3+
#include <cstdlib>
4+
#include <new>
5+
6+
extern "C" {
7+
8+
HRESULT compartment_clear(void *compartment);
9+
10+
void compartment_free(void *compartment);
11+
12+
HRESULT compartment_get_bool(void *compartment, bool *data);
13+
14+
HRESULT compartment_get_u32(void *compartment, uint32_t *data);
15+
16+
void compartment_guid(void *compartment, GUID *guid);
17+
18+
void *compartment_new(void *punk, uint32_t tf_client_id, const GUID *guid);
19+
20+
HRESULT compartment_set_bool(void *compartment, bool flag);
21+
22+
HRESULT compartment_set_u32(void *compartment, uint32_t data);
23+
24+
} // extern "C"

cpp/prebuild.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pushd $scriptDir/../rust
33
cargo build
44
cbindgen --crate composition_processor --output ../cpp/SampleIME/cbindgen/composition_processor.h
55
cbindgen --crate dictionary_parser --output ../cpp/SampleIME/cbindgen/dictionary_parser.h
6+
cbindgen --crate itf_components --output ../cpp/SampleIME/cbindgen/itf_components.h
67
cbindgen --crate globals --output ../cpp/SampleIME/cbindgen/globals.h
78
cbindgen --crate ime --output ../cpp/SampleIME/cbindgen/ime.h
89
cbindgen --crate numberkey_windows --output ../cpp/SampleIME/cbindgen/numberkey_windows.h

rust/Cargo.lock

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"dictionary_parser",
66
"globals",
77
"ime",
8+
"itf_components",
89
"numberkey_windows",
910
"ruststringrange"
1011
]

rust/itf_components/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "itf_components"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[lib]
9+
name = "itf_components"
10+
crate-type = ["staticlib"]
11+
12+
[dependencies]
13+
globals = { path = "../globals" }
14+
15+
[dependencies.windows]
16+
version = "0.26.0"
17+
features = [
18+
"build",
19+
"std",
20+
"Win32_Foundation",
21+
"Win32_UI_TextServices",
22+
"Win32_UI_WindowsAndMessaging",
23+
"Win32_System_Ole",
24+
]

0 commit comments

Comments
 (0)