forked from vovkasm/input-source-switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
52 lines (45 loc) · 1.76 KB
/
utils.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
#include <Carbon/Carbon.h>
#include <CoreFoundation/CoreFoundation.h>
#include "utils.h"
#include <iostream>
std::string
stringFromCFString(CFStringRef cfStr) {
CFIndex length = CFStringGetLength(cfStr);
CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char * buffer = new char [maxSize];
buffer[0] = '\0';
CFStringGetCString(cfStr, buffer, maxSize, kCFStringEncodingUTF8);
std::string ret(buffer);
delete[] buffer;
return ret;
}
void
printInputSourceProperty(TISInputSourceRef inputSource, CFStringRef property) {
CFTypeRef value = (CFTypeRef)TISGetInputSourceProperty(inputSource, property);
if (value == NULL) return;
CFRetain(value);
CFTypeID valueType = CFGetTypeID(value);
if (valueType == CFBooleanGetTypeID()) {
std::cout << stringFromCFString(property) << " : " << (CFBooleanGetValue((CFBooleanRef)value) ? "yes" : "no") << std::endl;
}
else if (valueType == CFStringGetTypeID()) {
std::cout << stringFromCFString(property) << " : " << stringFromCFString((CFStringRef)value) << std::endl;
}
else if (valueType == CFArrayGetTypeID()) {
std::cout << stringFromCFString(property) << " : [";
CFIndex len = CFArrayGetCount((CFArrayRef)value);
for (CFIndex i = 0; i < len; ++i) {
const void* elem = CFArrayGetValueAtIndex((CFArrayRef)value, i);
if (elem != NULL) {
if (CFGetTypeID(elem) == CFStringGetTypeID()) {
if (i > 0) {
std::cout << ',';
}
std::cout << '"' << stringFromCFString((CFStringRef)elem) << '"';
}
}
}
std::cout << "]" << std::endl;
}
CFRelease(value);
}