Skip to content

Instantly share code, notes, and snippets.

@qkdreyer
Created Sep 3, 2021
Embed
What would you like to do?
TIS + launchd

make make install make start-without-launchd # layout: com.apple.keylayout.US with effect make start-with-launchd # layout: com.apple.keylayout.US without effect make uninstall

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>dev.qkdreyer.tislaunchd</string>
<key>Program</key>
<string>/tmp/tislaunchd</string>
<key>StandardInPath</key>
<string>/tmp/tislaunchd.log</string>
<key>StandardOutPath</key>
<string>/tmp/tislaunchd.log</string>
<key>StandardErrorPath</key>
<string>/tmp/tislaunchd.log</string>
</dict>
</plist>
all:
gcc -o tislaunchd tislaunchd.m -framework appkit -framework carbon
install:
chmod u+x tislaunchd
cp tislaunchd /tmp
sudo cp dev.qkdreyer.tislaunchd.plist /Library/LaunchDaemons/
sudo launchctl load /Library/LaunchDaemons/dev.qkdreyer.tislaunchd.plist
uninstall:
rm /tmp/tislaunchd
sudo launchctl unload /Library/LaunchDaemons/dev.qkdreyer.tislaunchd.plist
sudo rm /Library/LaunchDaemons/dev.qkdreyer.tislaunchd.plist
start-without-launchd:
sudo /tmp/tislaunchd
start-with-launchd:
sudo launchctl start dev.qkdreyer.tislaunchd
sleep 5
cat /tmp/tislaunchd.log
sudo rm /tmp/tislaunchd.log
#import <AppKit/AppKit.h>
#import <Carbon/Carbon.H>
int main()
{
NSLog(@"getuid: %d", getuid());
bool root = getuid() == 0;
if (root) {
system("sudo -u qkdreyer /tmp/tislaunchd");
return 0;
}
CFMutableDictionaryRef filter = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
CFStringRef value = CFSTR("com.apple.keylayout.US");
CFDictionarySetValue(filter, kTISPropertyInputSourceID, value);
CFArrayRef inputSources = TISCreateInputSourceList(filter, true);
TISInputSourceRef lumInputSource = (TISInputSourceRef) CFArrayGetValueAtIndex(inputSources, 0);
bool m_isLumInputSourceEnabled = TISGetInputSourceProperty(lumInputSource, kTISPropertyInputSourceIsEnabled) == kCFBooleanTrue;
bool m_isLumInputSourceSelected = TISGetInputSourceProperty(lumInputSource, kTISPropertyInputSourceIsSelected) == kCFBooleanTrue;
if (!m_isLumInputSourceEnabled)
TISEnableInputSource(lumInputSource);
if (!m_isLumInputSourceSelected)
TISSelectInputSource(lumInputSource);
NSString* layout = (__bridge NSString*) TISGetInputSourceProperty(TISCopyCurrentKeyboardInputSource(), kTISPropertyInputSourceID);
NSLog(@"layout: %@", layout);
CFRelease(value);
CFRelease(filter);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment