-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathime.cpp
159 lines (143 loc) · 4.55 KB
/
ime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "ime.h"
#ifdef __ANDROID__
#include "options.h"
#include "sdltiles.h"
#endif
#ifdef _WIN32
#if 1 // Prevent IWYU reordering this below <imm.h>
#include "platform_win.h"
#endif
#include <imm.h>
class imm_wrapper
{
private:
HMODULE hImm;
using pImmGetContext_t = HIMC( WINAPI * )( HWND );
using pImmGetOpenStatus_t = BOOL( WINAPI * )( HIMC );
using pImmSetOpenStatus_t = BOOL( WINAPI * )( HIMC, BOOL );
using pImmReleaseContext_t = BOOL( WINAPI * )( HWND, HIMC );
pImmGetContext_t pImmGetContext;
pImmGetOpenStatus_t pImmGetOpenStatus;
pImmSetOpenStatus_t pImmSetOpenStatus;
pImmReleaseContext_t pImmReleaseContext;
template<typename T>
static T fun_ptr_cast( FARPROC p ) {
// workaround function cast warning
return reinterpret_cast<T>( reinterpret_cast<void *>( p ) );
}
public:
imm_wrapper() {
// Check if East Asian support is available
hImm = LoadLibraryW( L"imm32.dll" );
if( hImm ) {
pImmGetContext = fun_ptr_cast<pImmGetContext_t>(
GetProcAddress( hImm, "ImmGetContext" ) );
pImmGetOpenStatus = fun_ptr_cast<pImmGetOpenStatus_t>(
GetProcAddress( hImm, "ImmGetOpenStatus" ) );
pImmSetOpenStatus = fun_ptr_cast<pImmSetOpenStatus_t>(
GetProcAddress( hImm, "ImmSetOpenStatus" ) );
pImmReleaseContext = fun_ptr_cast<pImmReleaseContext_t>(
GetProcAddress( hImm, "ImmReleaseContext" ) );
if( !pImmGetContext || !pImmGetOpenStatus ||
!pImmSetOpenStatus || !pImmReleaseContext ) {
FreeLibrary( hImm );
hImm = nullptr;
pImmGetContext = nullptr;
pImmGetOpenStatus = nullptr;
pImmSetOpenStatus = nullptr;
pImmReleaseContext = nullptr;
}
}
}
~imm_wrapper() {
if( hImm ) {
FreeLibrary( hImm );
}
}
bool ime_enabled() {
if( hImm ) {
// NOLINTNEXTLINE(misc-misplaced-const)
const HWND hwnd = getWindowHandle();
// NOLINTNEXTLINE(misc-misplaced-const)
const HIMC himc = pImmGetContext( hwnd );
bool enabled = pImmGetOpenStatus( himc );
pImmReleaseContext( hwnd, himc );
return enabled;
}
return false;
}
void enable_ime() {
if( hImm ) {
// NOLINTNEXTLINE(misc-misplaced-const)
const HWND hwnd = getWindowHandle();
// NOLINTNEXTLINE(misc-misplaced-const)
const HIMC himc = pImmGetContext( hwnd );
pImmSetOpenStatus( himc, TRUE );
pImmReleaseContext( hwnd, himc );
}
}
void disable_ime() {
if( hImm ) {
// NOLINTNEXTLINE(misc-misplaced-const)
const HWND hwnd = getWindowHandle();
// NOLINTNEXTLINE(misc-misplaced-const)
const HIMC himc = pImmGetContext( hwnd );
pImmSetOpenStatus( himc, FALSE );
pImmReleaseContext( hwnd, himc );
}
}
};
static imm_wrapper imm;
#endif
static bool ime_enabled()
{
#if defined( __ANDROID__ )
return false; // always call disable_ime() (i.e. do nothing) on return
#elif defined( _WIN32 )
return imm.ime_enabled();
#endif
return false;
// TODO: other platforms?
}
void enable_ime()
{
#if defined( __ANDROID__ )
if( get_option<bool>( "ANDROID_AUTO_KEYBOARD" ) ) {
SDL_StartTextInput();
}
#elif defined( _WIN32 )
imm.enable_ime();
#endif
// TODO: other platforms?
}
void disable_ime()
{
#if defined( __ANDROID__ )
// the original android code did nothing, so don't change it
#elif defined( _WIN32 )
imm.disable_ime();
#endif
// TODO: other platforms?
}
ime_sentry::ime_sentry( ime_sentry::mode m ) : previously_enabled( ime_enabled() )
{
switch( m ) {
case enable:
enable_ime();
break;
case disable:
disable_ime();
break;
case keep:
// do nothing
break;
}
}
ime_sentry::~ime_sentry()
{
if( previously_enabled ) {
enable_ime();
} else {
disable_ime();
}
}