forked from rime/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquirrelInputController.m
More file actions
335 lines (293 loc) · 9.53 KB
/
SquirrelInputController.m
File metadata and controls
335 lines (293 loc) · 9.53 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#import "SquirrelInputController.h"
#import "SquirrelApplicationDelegate.h"
#import "macos_keycode.h"
#import <rime_api.h>
#import <rime/key_table.h>
// forward declaration of 'Private' category
@interface SquirrelInputController(Private)
-(void)createSession;
-(void)destroySession;
-(void)rimeConsumeCommittedText;
-(void)rimeUpdate;
@end
// implementation of the public interface
@implementation SquirrelInputController
/*!
@method
@abstract Receive incoming event
@discussion This method receives key events from the client application.
*/
-(BOOL)handleEvent:(NSEvent*)event client:(id)sender
{
// Return YES to indicate the the key input was received and dealt with.
// Key processing will not continue in that case. In other words the
// system will not deliver a key down event to the application.
// Returning NO means the original key down will be passed on to the client.
//NSLog(@"handleEvent:client:");
if (!_session || !RimeFindSession(_session)) {
[self createSession];
if (!_session) return NO;
}
_currentClient = sender;
BOOL handled = NO;
NSUInteger modifiers = [event modifierFlags];
switch ([event type]) {
case NSFlagsChanged:
{
if (_lastModifier == modifiers)
return YES;
//NSLog(@"FLAGSCHANGED self: 0x%x, client: 0x%x, modifiers: 0x%x", self, sender, modifiers);
int rime_modifiers = osx_modifiers_to_rime_modifiers(modifiers);
int release_mask = 0;
int changes = _lastModifier ^ modifiers;
if (changes & OSX_SHIFT_MASK)
{
release_mask = modifiers & OSX_SHIFT_MASK ? 0 : kReleaseMask;
RimeProcessKey(_session, XK_Shift_L, rime_modifiers | release_mask);
}
if (changes & OSX_CTRL_MASK)
{
release_mask = modifiers & OSX_CTRL_MASK ? 0 : kReleaseMask;
RimeProcessKey(_session, XK_Control_L, rime_modifiers | release_mask);
}
if (changes & OSX_ALT_MASK)
{
release_mask = modifiers & OSX_ALT_MASK ? 0 : kReleaseMask;
RimeProcessKey(_session, XK_Alt_L, rime_modifiers | release_mask);
}
if (changes & OSX_COMMAND_MASK)
{
release_mask = modifiers & OSX_COMMAND_MASK ? 0 : kReleaseMask;
RimeProcessKey(_session, XK_Super_L, rime_modifiers | release_mask);
}
[self rimeUpdate];
}
break;
case NSKeyDown:
{
NSInteger keyCode = [event keyCode];
NSString* keyChars = [event characters];
//NSLog(@"KEYDOWN self: 0x%x, client: 0x%x, modifiers: 0x%x, keyCode: %d, keyChars: [%@]",
// self, sender, modifiers, keyCode, keyChars);
// ignore Command+X hotkeys.
if (modifiers & OSX_CAPITAL_MASK || modifiers & OSX_COMMAND_MASK)
break;
// translate osx keyevents to rime keyevents
int rime_keycode = osx_keycode_to_rime_keycode(keyCode,
[keyChars UTF8String][0],
modifiers & OSX_SHIFT_MASK);
if (rime_keycode)
{
int rime_modifiers = osx_modifiers_to_rime_modifiers(modifiers);
handled = (BOOL)RimeProcessKey(_session, rime_keycode, rime_modifiers);
[self rimeUpdate];
}
}
break;
defaults:
break;
}
_lastModifier = modifiers;
_lastEventType = [event type];
return handled;
}
-(NSUInteger)recognizedEvents:(id)sender
{
//NSLog(@"recognizedEvents:");
return NSKeyDownMask | NSFlagsChangedMask;
}
-(void)activateServer:(id)sender
{
//NSLog(@"activateServer:");
if ([[NSApp delegate] useUSKeyboardLayout]) {
[sender overrideKeyboardWithKeyboardNamed:@"com.apple.keylayout.US"];
}
_preeditString = @"";
}
-(id)initWithServer:(IMKServer*)server delegate:(id)delegate client:(id)inputClient
{
//NSLog(@"initWithServer:delegate:client:");
if (self = [super initWithServer:server delegate:delegate client:inputClient])
[self createSession];
return self;
}
-(void)deactivateServer:(id)sender
{
//NSLog(@"deactivateServer:");
[[[NSApp delegate] panel] hide];
[self commitComposition:sender];
}
/*!
@method
@abstract Called when a user action was taken that ends an input session.
Typically triggered by the user selecting a new input method
or keyboard layout.
@discussion When this method is called your controller should send the
current input buffer to the client via a call to
insertText:replacementRange:. Additionally, this is the time
to clean up if that is necessary.
*/
-(void)commitComposition:(id)sender
{
//NSLog(@"commitComposition:");
// FIXME: chrome's address bar issues this callback when showing suggestions.
if ([[sender bundleIdentifier] isEqualToString:@"com.google.Chrome"])
return;
// force committing existing Rime composition
if (_session && RimeCommitComposition(_session)) {
[self rimeConsumeCommittedText];
}
}
// a piece of comment from SunPinyin's macos wrapper says:
// > though we specified the showPrefPanel: in SunPinyinApplicationDelegate as the
// > action receiver, the IMKInputController will actually receive the event.
// so here we deliver messages to our responsible SquirrelApplicationDelegate
-(void)deploy:(id)sender
{
[[NSApp delegate] deploy:sender];
}
-(void)configure:(id)sender
{
[[NSApp delegate] configure:sender];
}
-(void)checkForUpdates:(id)sender
{
[[[NSApp delegate] updater] performSelector:@selector(checkForUpdates:) withObject:sender];
}
-(void)openWiki:(id)sender
{
[[NSApp delegate] openWiki:sender];
}
-(NSMenu*)menu
{
return [[NSApp delegate] menu];
}
-(NSArray*)candidates:(id)sender
{
return _candidates;
}
-(void)dealloc
{
[_preeditString release];
[_candidates release];
[self destroySession];
[super dealloc];
}
-(void)commitString:(NSString*)string
{
//NSLog(@"commitString:");
[_currentClient insertText:string
replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
[_preeditString release];
_preeditString = @"";
[[[NSApp delegate] panel] hide];
}
-(void)showPreeditString:(NSString*)preedit
selRange:(NSRange)range
caretPos:(NSUInteger)pos
{
//NSLog(@"showPreeditString: '%@'", preedit);
if ([_preeditString isEqual:preedit])
return;
[preedit retain];
[_preeditString release];
_preeditString = preedit;
NSDictionary* attrs;
NSAttributedString* attrString;
attrs = [self markForStyle:kTSMHiliteSelectedRawText atRange:range];
attrString = [[NSAttributedString alloc] initWithString:preedit attributes:attrs];
[_currentClient setMarkedText:attrString
selectionRange:NSMakeRange(pos, 0)
replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
[attrString release];
}
-(void)showCandidates:(NSArray*)candidates
andComments:(NSArray*)comments
withLabels:(NSString*)labels
highlighted:(NSUInteger)index
{
//NSLog(@"showCandidates:");
[candidates retain];
[_candidates release];
_candidates = candidates;
NSRect caretPos;
[_currentClient attributesForCharacterIndex:0 lineHeightRectangle:&caretPos];
SquirrelPanel* panel = [[NSApp delegate] panel];
[panel updatePosition:caretPos];
[panel updateCandidates:candidates andComments:comments withLabels:labels highlighted:index];
}
@end // SquirrelController
// implementation of private interface
@implementation SquirrelInputController(Private)
-(void)createSession
{
//NSLog(@"createSession:");
_session = RimeCreateSession();
}
-(void)destroySession
{
//NSLog(@"destroySession:");
if (_session) {
RimeDestroySession(_session);
_session = 0;
}
}
-(void)rimeConsumeCommittedText
{
RimeCommit commit = {0};
if (RimeGetCommit(_session, &commit)) {
NSString *commitText = [NSString stringWithUTF8String:commit.text];
[self commitString: commitText];
RimeFreeCommit(&commit);
}
}
-(void)rimeUpdate
{
//NSLog(@"update");
[self rimeConsumeCommittedText];
RimeContext ctx = {0};
RIME_STRUCT_INIT(RimeContext, ctx);
if (RimeGetContext(_session, &ctx)) {
// update preedit text
const char *preedit = ctx.composition.preedit;
NSString *preeditText = @"";
if (preedit) {
preeditText = [NSString stringWithUTF8String:preedit];
}
NSUInteger start = 0;
NSUInteger end = 0;
NSUInteger caretPos = 0;
if (ctx.composition.sel_start < ctx.composition.sel_end) {
start = utf8len(preedit, ctx.composition.sel_start);
end = utf8len(preedit, ctx.composition.sel_end);
}
if (ctx.composition.cursor_pos > 0) {
caretPos = utf8len(preedit, ctx.composition.cursor_pos);
}
NSRange selRange = NSMakeRange(start, end - start);
[self showPreeditString:preeditText selRange:selRange caretPos:caretPos];
// update candidates
NSMutableArray *candidates = [NSMutableArray array];
NSMutableArray *comments = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < ctx.menu.num_candidates; ++i) {
[candidates addObject:[NSString stringWithUTF8String:ctx.menu.candidates[i].text]];
if (ctx.menu.candidates[i].comment) {
[comments addObject:[NSString stringWithUTF8String:ctx.menu.candidates[i].comment]];
}
else {
[comments addObject:@""];
}
}
NSString* labels = @"";
if (ctx.menu.select_keys) {
labels = [NSString stringWithUTF8String:ctx.menu.select_keys];
}
[self showCandidates:candidates
andComments:comments
withLabels:labels
highlighted:ctx.menu.highlighted_candidate_index];
RimeFreeContext(&ctx);
}
}
@end // SquirrelController(Private)