-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTranscoder.m
94 lines (67 loc) · 2.64 KB
/
Transcoder.m
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
//
// Transcoder.m
// Transcode
//
// Created by flamefork on 22.08.08.
// Copyright 2008 Flamefork. All rights reserved.
//
#import "Transcoder.h"
#import "Layout.h"
@implementation Transcoder
- (NSString *) getCurrentLayoutID
{
return (NSString*)TISGetInputSourceProperty(TISCopyCurrentKeyboardLayoutInputSource(), kTISPropertyInputSourceID);
}
- (void) createLayouts
{
layouts = [NSMutableArray array];
[layouts retain];
NSDictionary *filter = [NSDictionary dictionaryWithObject:(NSString *)kTISTypeKeyboardLayout forKey:(NSString *)kTISPropertyInputSourceType];
NSArray *list = (NSArray *)TISCreateInputSourceList((CFDictionaryRef)filter, false);
for (id *isr in list) {
NSString *isid = (NSString*)TISGetInputSourceProperty((TISInputSourceRef)isr, kTISPropertyInputSourceID);
CFDataRef uchr = TISGetInputSourceProperty((TISInputSourceRef)isr, kTISPropertyUnicodeKeyLayoutData);
UCKeyboardLayout *uchrData = (UCKeyboardLayout*)CFDataGetBytePtr(uchr);
Layout *layout = [[Layout alloc] initWithUchrData:uchrData lid:isid];
[layouts addObject:layout];
}
}
- (NSArray *)decode:(NSString *)aString
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[aString length]];
int currentIndex = [layouts indexOfObject:[self getCurrentLayoutID]];
Layout *currentLayout = [layouts objectAtIndex:currentIndex];
UniChar uchar;
int i, length = [aString length];
for (i = 0; i < length; i++) {
uchar = [aString characterAtIndex:i];
NSString *s = [NSString stringWithCharacters:&uchar length:1];
KeyDiscriminant *kd = [currentLayout discriminantForChar:s];
// Do not forward if we're in really wrong ;) layout
if (!kd) return result;
[result addObject:kd];
}
return result;
}
- (NSString *)encode:(NSArray *)keyDiscriminants
{
NSMutableString *result = [NSMutableString stringWithCapacity:[keyDiscriminants count]];
int currentIndex = [layouts indexOfObject:[self getCurrentLayoutID]];
Layout *currentLayout = [layouts objectAtIndex:currentIndex];
KeyDiscriminant *kd;
for (kd in keyDiscriminants) {
[result appendString:[currentLayout charForDiscriminant:kd]];
}
return result;
}
- (void) switchLayout
{
int currentIndex = [layouts indexOfObject:[self getCurrentLayoutID]];
int nextIndex = currentIndex + 1;
if (nextIndex >= [layouts count]) nextIndex = 0;
Layout *nextLayout = [layouts objectAtIndex:nextIndex];
NSDictionary *filter = [NSDictionary dictionaryWithObject:nextLayout->layoutID forKey:(NSString *)kTISPropertyInputSourceID];
CFArrayRef list = TISCreateInputSourceList((CFDictionaryRef)filter, false);
TISSelectInputSource((TISInputSourceRef)CFArrayGetValueAtIndex(list, 0));
}
@end