-
Notifications
You must be signed in to change notification settings - Fork 2
/
SwitchIM.swift
executable file
·362 lines (297 loc) · 14.6 KB
/
SwitchIM.swift
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// SwitchIM.swift
// SwitchIM
//
// Created by NuRi on 2/6/15.
// Copyright (c) 2016 Suhwan Seo. All rights reserved.
//
import Foundation
import Cocoa
import Carbon
import AppKit
class SwitchIM {
func getList() -> [InputSourceModel] {
var inputSourceArray: [InputSourceModel] = []
// get input source list
let property1 = "\(kTISPropertyInputSourceIsSelectCapable)"
let property2 = "\(kTISPropertyInputSourceCategory)"
let dict : NSMutableDictionary = [property1: kCFBooleanTrue, property2: kTISCategoryKeyboardInputSource]
//let dic : CFDictionary? = nil
// let flag : DarwinBoolean = 0
let list = TISCreateInputSourceList(dict, false).takeUnretainedValue() as NSArray
//let support:SwitchIMSupport = SwitchIMSupport()
//let list = support.createInputSourceList(dict, flag: flag)
//let list = TISCreateASCIICapableInputSourceList().takeUnretainedValue() as NSArray
// loop list
for inputSource in list as! [TISInputSource] {
//LOG(inputSource)
let ptrType = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputSourceType)
let type = unsafeBitCast(ptrType, CFStringRef.self)
let ptrModeID = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputModeID)
var modeID = "-"
if (ptrModeID != nil) {
modeID = unsafeBitCast(ptrModeID, CFStringRef.self) as String
}
let ptrID = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputSourceID)
let ID = unsafeBitCast(ptrID, CFStringRef.self)
let ptrName = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyLocalizedName)
let name = unsafeBitCast(ptrName, CFStringRef.self)
let ptrCategory = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputSourceCategory)
let category = unsafeBitCast(ptrCategory, CFStringRef.self)
// let ptrSelectable = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputSourceIsSelectCapable)
// let selectable = unsafeBitCast(ptrSelectable, CFBooleanRef.self)
// LOG("selecable: \(CFBooleanGetValue(selectable))")
// let ptrEnable = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputSourceIsEnabled)
// if (ptrEnable != nil) {
// let enable = unsafeBitCast(ptrEnable, CFBooleanRef.self)
// LOG("enable: \(CFBooleanGetValue(enable))")
// }
LOG("Type: \(type) ID: \(ID) modeID: \(modeID) Name: \(name) category: \(category)")
if (type == kTISTypeKeyboardInputMode || type == kTISTypeKeyboardLayout) {
LOG("match : input mode")
LOG(inputSource)
if (type == kTISTypeKeyboardInputMode) {
let infoString: String = "\(inputSource)"
if infoString.rangeOfString("Parent = ", options: .RegularExpressionSearch) != nil {
let parent: String = infoString.stringByReplacingOccurrencesOfString("^.*Parent = |\\)$", withString:"", options:NSStringCompareOptions.RegularExpressionSearch, range: nil)
LOG("check parent(\(parent))...")
// get input source list
let propertyID = "\(kTISPropertyInputSourceID)"
let parentDict : NSMutableDictionary = [propertyID: parent]
let parentList = TISCreateInputSourceList(parentDict, false)
if (parentList != nil) {
LOG("OK: parent is enabled")
}
else if ((ID as String).hasPrefix("org.youknowone.inputmethod.Gureum")) {
LOG("OK: parent is disabled but it's Gureum IM")
}
else {
LOG("NG: parent is disabled. skip")
continue
}
}
}
let model = InputSourceModel(ID:ID as String, modeID:modeID, name:name as String, source:inputSource)
inputSourceArray.append(model)
}
}
return inputSourceArray
}
func changeInputSource(model:InputSourceModel, mouseClick:Bool=true) -> Bool {
LOG("change to \(model._name)")
// let pre = TISCopyCurrentKeyboardInputSource()
// LOG(pre)
let source:TISInputSourceRef = model._source
//TISEnableInputSource(source)
let status = TISSelectInputSource(source)
/*
let layoutStatus = TISSetInputMethodKeyboardLayoutOverride(source)
if (layoutStatus == Int32(paramErr)) {
LOG("fail to TISSetInputMethodKeyboardLayoutOverride")
// println("source:\(model._name)")
// let layout = TISCopyCurrentASCIICapableKeyboardLayoutInputSource()
// let layoutStatus2 = TISSetInputMethodKeyboardLayoutOverride()
// println(layoutStatus2)
// TISSelectInputSource(source)
}
let ptrModeID = TISGetInputSourceProperty(source as TISInputSource, kTISPropertyInputModeID)
if (ptrModeID != nil) {
let size: UInt32 = numericCast(sizeof(CFStringRef))
let property: TSMDocumentPropertyTag = numericCast(kTSMDocumentInputModePropertyTag)
let status = TSMSetDocumentProperty(TSMGetActiveDocument(), property, size, ptrModeID)
LOG("TSMSetDocumentProperty status:\(status)")
}
*/
// changing input source was not working well,
if mouseClick {
// do magic tricks with mouse click
self.makeMouseEvent()
}
else {
// do magic tricks with "select the previous input source shortcut"
self.makeShortcutEvent()
}
if (status == noErr) {
LOG(" - keyboard changed: \(model._name)")
return true
}
LOG(" - keyboard change failed: \(model._name)")
return false
}
func makeShortcutEvent() -> Void {
// reference sources:
// https://github.com/tekezo/Karabiner/blob/master/src/util/read-symbolichotkeys/read-symbolichotkeys/main.m
// default KeyCode: 0x31, Flag: CONTROL_L
var keyCode:CGKeyCode = CGKeyCode.init(0x31)
var keyMask = CGEventFlags.MaskControl.rawValue
repeat {
// read "select the previous input source shortcut"
let dict:NSDictionary? = NSUserDefaults.standardUserDefaults().persistentDomainForName("com.apple.symbolichotkeys")
if (dict == nil) {
LOG("Error: com.apple.symbolichotkeys is not found.")
break
}
let symbolichotkeys:NSDictionary? = dict!["AppleSymbolicHotKeys"] as? NSDictionary
if (symbolichotkeys == nil) {
LOG("Error: AppleSymbolicHotKeys is not found.")
break
}
let symbolichotkey:NSDictionary? = symbolichotkeys!["60"] as? NSDictionary
if (symbolichotkey == nil) {
// No entry
break
}
let value:NSDictionary? = symbolichotkey!["value"] as? NSDictionary
if (value == nil) {
LOG("Error: value is not found.")
break
}
let parameters:NSArray? = value!["parameters"] as? NSArray
if (parameters == nil) {
LOG("Error: parameters is not found.")
break
}
let key:Int = parameters![1] as! Int
let modifiers = parameters![2] as! Int
// reset key mask
keyMask = 0x0
if (modifiers > 0) {
if (modifiers & 0x20000 != 0) {
keyMask = keyMask | CGEventFlags.MaskShift.rawValue
}
if (modifiers & 0x40000 != 0) {
keyMask = keyMask | CGEventFlags.MaskControl.rawValue
}
if (modifiers & 0x80000 != 0) {
keyMask = keyMask | CGEventFlags.MaskAlternate.rawValue
}
if (modifiers & 0x100000 != 0) {
keyMask = keyMask | CGEventFlags.MaskCommand.rawValue
}
}
keyCode = CGKeyCode.init(key)
LOG("previous input source shortcut: \(keyMask) + \(keyCode)")
} while(false)
let keyDown = CGEventCreateKeyboardEvent(nil, keyCode, true)
let keyUp = CGEventCreateKeyboardEvent(nil, keyCode, false)
let maskFlags = CGEventFlags(rawValue: keyMask)!
CGEventSetFlags(keyDown, maskFlags)
// call shortcut 2 times with 150ms delay
for _ in 0...1 {
LOG("change input source shortcut down")
CGEventPost(CGEventTapLocation.CGHIDEventTap, keyDown)
LOG("change input source shortcut up")
CGEventPost(CGEventTapLocation.CGHIDEventTap, keyUp)
NSThread.sleepForTimeInterval(0.15)
}
}
func makeMouseEvent() -> Void {
// mouse click simulation
// remember original mouse location
let event: CGEventRef? = CGEventCreate(nil)
let currentLocation = CGEventGetLocation(event)
LOG("mouse position:\(currentLocation)")
// apple!
let location = CGPointMake(26,12)
let ptrClickDown = CGEventCreateMouseEvent(nil, CGEventType.LeftMouseDown, location, CGMouseButton.Left)
let clickDown = unsafeBitCast(ptrClickDown, CGEventRef.self)
let ptrClickUp = CGEventCreateMouseEvent(nil, CGEventType.LeftMouseUp, location, CGMouseButton.Left)
let clickUp = unsafeBitCast(ptrClickUp, CGEventRef.self)
CGEventPost(CGEventTapLocation.CGHIDEventTap, clickDown)
CGEventPost(CGEventTapLocation.CGHIDEventTap, clickUp)
CGEventPost(CGEventTapLocation.CGHIDEventTap, clickDown)
CGEventPost(CGEventTapLocation.CGHIDEventTap, clickUp)
// restore mouse location
let ptrMove = CGEventCreateMouseEvent(nil, CGEventType.MouseMoved, currentLocation, CGMouseButton.Left)
let move = unsafeBitCast(ptrMove, CGEventRef.self)
CGEventPost(CGEventTapLocation.CGHIDEventTap, move)
}
func getIconImage(inputSource: TISInputSourceRef) -> NSImage! {
var image:NSImage?
let ptrIcon = TISGetInputSourceProperty(inputSource, kTISPropertyIconRef)
if (ptrIcon != nil) {
let icon: IconRef = unsafeBitCast(ptrIcon, IconRef.self)
image = NSImage(iconRef: icon)
}
else {
let ptrIconURL = TISGetInputSourceProperty(inputSource, kTISPropertyIconImageURL)
let iconURL = unsafeBitCast(ptrIconURL, CFURLRef.self)
let url:NSURL = iconURL
// LOG("URL:\(url)")
var err: NSError?
var imageData :NSData?
do {
imageData = try NSData(contentsOfURL: url,options: NSDataReadingOptions.DataReadingMappedIfSafe)
} catch let error as NSError {
err = error
imageData = nil
LOG(err)
}
if (imageData == nil)
{
//let reason : String? = err?.localizedFailureReason
//LOG("Error reading from URL: \(err)")
// retry with tiff
LOG("retry with tiff icon")
let parentDirectory = url.URLByDeletingLastPathComponent
let fileNameWithExtension: NSString! = url.lastPathComponent
let fileName = fileNameWithExtension.stringByDeletingPathExtension
let fileExtension = "tiff"
let newFileName = NSString(format:"%@.%@",fileName,fileExtension)
let newURL: NSURL! = NSURL(string: newFileName as String, relativeToURL: parentDirectory)
do {
imageData = try NSData(contentsOfURL: newURL,options: NSDataReadingOptions.DataReadingMappedIfSafe)
} catch let error as NSError {
err = error
imageData = nil
LOG(err)
}
}
image = NSImage(data: imageData!)
}
image?.size = NSSize(width: 16, height: 16)
return image!
}
func getCurrentIconImage() -> NSImage! {
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
LOG(currentSource)
return getIconImage(currentSource)
}
func getCurrentInputSource() -> TISInputSourceRef {
let currentSource:TISInputSourceRef = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
return currentSource
}
func getInputSourceShortcutCodename(inputSource:TISInputSourceRef) -> String {
let ptrID = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputSourceID)
let ID = unsafeBitCast(ptrID, CFStringRef.self)
let ptrModeID = TISGetInputSourceProperty(inputSource as TISInputSource, kTISPropertyInputModeID)
var modeID = "-"
if (ptrModeID != nil) {
modeID = unsafeBitCast(ptrModeID, CFStringRef.self) as String
}
let model:InputSourceModel = InputSourceModel(ID: ID as String, modeID: modeID, name: "", source: inputSource)
return model.shortcutCodename()
}
}
class InputSourceModel {
var _ID:String = ""
var _modeID:String = ""
var _name:String = ""
var _source:TISInputSourceRef
init(ID:String, modeID:String, name:String, source:TISInputSourceRef) {
_ID = ID
_name = name
_source = source
_modeID = modeID
}
func description() -> String {
return String("ID: \(_ID) ModeID: \(_modeID) Name: \(_name) InputSource: \(_source)")
}
func shortcutCodename() -> String {
return self.shortcutCodename(_ID, modeID: _modeID)
}
func shortcutCodename(ID:String, modeID:String) -> String {
let codename:String = (ID + "#" + modeID).stringByReplacingOccurrencesOfString(".", withString: "_", options: [], range: nil)
return codename
}
}