Skip to content

Commit

Permalink
Merge pull request #36 from igorkulman/capslock-fix
Browse files Browse the repository at this point in the history
Setting the initial Capslock state
  • Loading branch information
MSzturc authored Jul 24, 2020
2 parents 8d8430c + b71623e commit f1f3f2a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ThinkpadAssistant-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
// Use this file to import your target's public headers that you would like to expose to Swift.
//

#include <stdbool.h>

int IOBluetoothPreferenceGetControllerPowerState();
void IOBluetoothPreferenceSetControllerPowerState(int state);

bool getCapslockState();
4 changes: 4 additions & 0 deletions ThinkpadAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
C9B3F7932462E0540082AD67 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = C9B3F7912462E0540082AD67 /* InfoPlist.strings */; };
C9DFD0252451BAA2000103A2 /* IconProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9DFD0242451BAA2000103A2 /* IconProvider.swift */; };
DC56F590D93F6994C91F60C8 /* Pods_ThinkpadAssistant.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A2D81A3D8A645E8C957C4863 /* Pods_ThinkpadAssistant.framework */; };
F34E410024B1BADF00A7983F /* Capslock.m in Sources */ = {isa = PBXBuildFile; fileRef = F34E40FF24B1BADF00A7983F /* Capslock.m */; };
F397902F24A2916D009C1F22 /* CapslockMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F397902E24A2916D009C1F22 /* CapslockMonitor.swift */; };
FC56DEBE1EE98AC7001ACE3F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC56DEBD1EE98AC7001ACE3F /* AppDelegate.swift */; };
FC56DEC21EE98AC7001ACE3F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = FC56DEC11EE98AC7001ACE3F /* Assets.xcassets */; };
Expand Down Expand Up @@ -89,6 +90,7 @@
F31BF22D24718B3900F40D56 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/InfoPlist.strings; sourceTree = "<group>"; };
F31BF22E24718B3900F40D56 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/Localizable.strings; sourceTree = "<group>"; };
F31BF22F24718B3900F40D56 /* cs */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/InfoPlist.strings; sourceTree = "<group>"; };
F34E40FF24B1BADF00A7983F /* Capslock.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Capslock.m; sourceTree = "<group>"; };
F397902E24A2916D009C1F22 /* CapslockMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapslockMonitor.swift; sourceTree = "<group>"; };
FC21613D1EE9C64E00E7DB46 /* AutoLaunchHelper.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AutoLaunchHelper.entitlements; sourceTree = "<group>"; };
FC56DEBA1EE98AC7001ACE3F /* ThinkpadAssistant.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ThinkpadAssistant.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -192,6 +194,7 @@
C9B3F78A2462DC360082AD67 /* Localizable.strings */,
C93B8424244A077000ECFFBD /* ShortcutManager.swift */,
C947BB06247AFECB00E6AF14 /* BluetoothManager.swift */,
F34E40FF24B1BADF00A7983F /* Capslock.m */,
F397902E24A2916D009C1F22 /* CapslockMonitor.swift */,
C93B8460244E0DB700ECFFBD /* MuteMicManager.swift */,
C93B8466244E20D500ECFFBD /* WifiManager.swift */,
Expand Down Expand Up @@ -388,6 +391,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F34E410024B1BADF00A7983F /* Capslock.m in Sources */,
C91E2345244E3780007A1962 /* DisplayManager.swift in Sources */,
C93B8425244A077000ECFFBD /* ShortcutManager.swift in Sources */,
C93B8458244DAA4500ECFFBD /* HUD.swift in Sources */,
Expand Down
59 changes: 59 additions & 0 deletions ThinkpadAssistant/Capslock.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Capslock.m
// ThinkpadAssistant
//
// Created by Igor Kulman on 05/07/2020.
// Copyright © 2020 Matthäus Szturc. All rights reserved.
//

#include <IOKit/IOKitLib.h>
#include <IOKit/hidsystem/IOHIDLib.h>
#include <IOKit/hidsystem/IOHIDParameter.h>
#include <CoreFoundation/CoreFoundation.h>
#include <stdbool.h>

#include <libgen.h>

/*!
@brief Gets the current Capslock state
@discussion This method can be used the gets the initial Capslock state whhen the application is first run. Needs to be done in Objective-C as Swift can only listen for modifier key presses only do determine Capslock being pressed.
To use it, simply call getCapslockState()
@return true if Capslock is enabled, false otherwise
*/
bool getCapslockState()
{
kern_return_t kr;
io_service_t ios;
io_connect_t ioc;
CFMutableDictionaryRef mdict;
bool state;

mdict = IOServiceMatching(kIOHIDSystemClass);
ios = IOServiceGetMatchingService(kIOMasterPortDefault, (CFDictionaryRef) mdict);
if (!ios)
{
if (mdict)
CFRelease(mdict);
return false;
}

kr = IOServiceOpen(ios, mach_task_self(), kIOHIDParamConnectType, &ioc);
IOObjectRelease(ios);
if (kr != KERN_SUCCESS)
{
return false;
}

kr = IOHIDGetModifierLockState(ioc, kIOHIDCapsLockState, &state);
if (kr != KERN_SUCCESS)
{
IOServiceClose(ioc);
return false;
}

IOServiceClose(ioc);
return (bool)state;
}
3 changes: 3 additions & 0 deletions ThinkpadAssistant/CapslockMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ final class CapslockMonitor {
}

static func register() {
// get initial capslock state
lastState = getCapslockState()

NSEvent.addGlobalMonitorForEvents(matching: .flagsChanged, handler: flagsChanged)
}

Expand Down

0 comments on commit f1f3f2a

Please sign in to comment.