Skip to content

Commit

Permalink
Get caps lock mapping working
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Dec 3, 2018
1 parent db5d0e3 commit 490830c
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 72 deletions.
7 changes: 4 additions & 3 deletions app/About.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
</connections>
</navigationItem>
<connections>
<outlet property="capsLockMappingCell" destination="31N-0g-RhF" id="UjF-21-3df"/>
<outlet property="fontSizeCell" destination="CTf-zd-xjl" id="AvD-Zl-Y54"/>
<outlet property="openGithub" destination="F4i-eC-hQ6" id="66M-sQ-RJR"/>
<outlet property="openTwitter" destination="bge-GA-6p8" id="r0A-lN-0e0"/>
Expand All @@ -186,15 +187,15 @@
<!--Caps Lock-->
<scene sceneID="VbH-40-Znm">
<objects>
<tableViewController title="Caps Lock" id="Zzy-IN-laJ" sceneMemberID="viewController">
<tableViewController title="Caps Lock" id="Zzy-IN-laJ" customClass="CapsLockMappingViewController" sceneMemberID="viewController">
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="18" sectionFooterHeight="18" id="ZlS-Ig-a8Z">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<sections>
<tableViewSection id="oTd-4i-TSa">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="zuo-ZW-n37" style="IBUITableViewCellStyleDefault" id="8ZS-Wp-1TX">
<tableViewCell clipsSubviews="YES" tag="1" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="zuo-ZW-n37" style="IBUITableViewCellStyleDefault" id="8ZS-Wp-1TX">
<rect key="frame" x="0.0" y="35" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="8ZS-Wp-1TX" id="GMp-XT-GYq">
Expand All @@ -211,7 +212,7 @@
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="NR7-h5-BZD" style="IBUITableViewCellStyleDefault" id="IMH-6g-rLD">
<tableViewCell clipsSubviews="YES" tag="2" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" textLabel="NR7-h5-BZD" style="IBUITableViewCellStyleDefault" id="IMH-6g-rLD">
<rect key="frame" x="0.0" y="79" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="IMH-6g-rLD" id="Qea-lO-LfG">
Expand Down
23 changes: 15 additions & 8 deletions app/AboutViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface AboutViewController ()
@property (weak, nonatomic) IBOutlet UITableViewCell *openTwitter;
@property (weak, nonatomic) IBOutlet UITableViewCell *fontSizeCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *themeCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *capsLockMappingCell;
@end

@implementation AboutViewController
Expand All @@ -33,15 +34,15 @@ - (void)_addObservers {
UserPreferences *prefs = [UserPreferences shared];
NSKeyValueObservingOptions opts = NSKeyValueObservingOptionNew;

[prefs addObserver:self forKeyPath:@"mapCapsLockAsControl" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"capsLockMapping" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"fontSize" options:opts context:nil];
[prefs addObserver:self forKeyPath:@"theme" options:opts context:nil];
}

- (void)_removeObservers {
@try {
UserPreferences *prefs = [UserPreferences shared];
[prefs removeObserver:self forKeyPath:@"mapCapsLockAsControl"];
[prefs removeObserver:self forKeyPath:@"capsLockMapping"];
[prefs removeObserver:self forKeyPath:@"fontSize"];
[prefs removeObserver:self forKeyPath:@"theme"];
} @catch (NSException * __unused exception) {}
Expand All @@ -57,12 +58,18 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)_updatePreferenceUI {
UserPreferences *prefs = [UserPreferences shared];
_fontSizeCell.detailTextLabel.text = prefs.fontSize.stringValue;
_themeCell.detailTextLabel.text = prefs.theme.presetName;
}

- (IBAction)didSwitchCapsLock:(id)sender {

self.fontSizeCell.detailTextLabel.text = prefs.fontSize.stringValue;
self.themeCell.detailTextLabel.text = prefs.theme.presetName;
NSString *capsLockMappingDescr;
switch (prefs.capsLockMapping) {
case CapsLockMapNone:
capsLockMappingDescr = @"None"; break;
case CapsLockMapControl:
capsLockMappingDescr = @"Control"; break;
case CapsLockMapEscape:
capsLockMappingDescr = @"Escape"; break;
}
self.capsLockMappingCell.detailTextLabel.text = capsLockMappingDescr;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
16 changes: 16 additions & 0 deletions app/CapsLockMappingViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// CapsLockMappingViewController.h
// iSH
//
// Created by Theodore Dubois on 12/2/18.
//

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface CapsLockMappingViewController : UITableViewController

@end

NS_ASSUME_NONNULL_END
43 changes: 43 additions & 0 deletions app/CapsLockMappingViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// CapsLockMappingViewController.m
// iSH
//
// Created by Theodore Dubois on 12/2/18.
//

#import "CapsLockMappingViewController.h"
#import "UserPreferences.h"

@interface CapsLockMappingViewController ()

@end

@implementation CapsLockMappingViewController

- (void)viewDidLoad {
[super viewDidLoad];

[UserPreferences.shared addObserver:self forKeyPath:@"capsLockMapping" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc {
[UserPreferences.shared removeObserver:self forKeyPath:@"capsLockMapping"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
[self.tableView reloadData];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (cell.tag == UserPreferences.shared.capsLockMapping)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UserPreferences.shared.capsLockMapping = cell.tag;
}

@end
2 changes: 0 additions & 2 deletions app/TerminalView.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

@interface TerminalView : UIView <UIKeyInput, WKScriptMessageHandler>

- (void)registerExternalKeyboardNotificationsToNotificationCenter:(NSNotificationCenter *)center;

@property (nonatomic) UIKeyboardAppearance keyboardAppearance;

@property (weak) IBOutlet UIInputView *inputAccessoryView;
Expand Down
52 changes: 5 additions & 47 deletions app/TerminalView.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,16 @@
//

#import "TerminalView.h"
#import "UserPreferences.h"

@interface TerminalView ()

typedef enum {
kNone,
kEsc,
kCtrl,
} CapsLockTarget;


@property (nonatomic) NSMutableArray<UIKeyCommand *> *keyCommands;
@property (nonatomic) CapsLockTarget currentCapsLocktarget;

@end

@implementation TerminalView

- (void)registerExternalKeyboardNotificationsToNotificationCenter:(NSNotificationCenter *)center {
[center addObserver:self
selector:@selector(keyboardDidChange:)
name:UITextInputCurrentInputModeDidChangeNotification
object:nil];
[center addObserver:self
selector:@selector(appDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}

- (void)keyboardDidChange:(NSNotification *)notification {
self.currentCapsLocktarget = [self capsLockTarget];
}

- (void)appDidBecomeActive:(NSNotification *)notification {
self.currentCapsLocktarget = [self capsLockTarget];
}

- (void)setTerminal:(Terminal *)terminal {
if (self.terminal) {
// remove old terminal
Expand Down Expand Up @@ -184,11 +158,6 @@ - (void)handleKeyCommand:(UIKeyCommand *)command {

static const char *alphabet = "abcdefghijklmnopqrstuvwxyz";
static const char *controlKeys = "abcdefghijklmnopqrstuvwxyz26-=[]\\";
NSString *kiSHCapsLockMapping = @"kiSHCapsLockMapping";

- (BOOL)shouldRemapCapsLock {
return self.currentCapsLocktarget != kNone;
}

- (NSArray<UIKeyCommand *> *)keyCommands {
if (_keyCommands != nil)
Expand All @@ -199,7 +168,7 @@ - (BOOL)shouldRemapCapsLock {
UIKeyInputLeftArrow, UIKeyInputRightArrow, @"\t"]) {
[self addKey:specialKey withModifiers:0];
}
if ([self shouldRemapCapsLock]) {
if (UserPreferences.shared.capsLockMapping != CapsLockMapNone) {
[self addKeys:controlKeys withModifiers:UIKeyModifierAlphaShift];
[self addKeys:alphabet withModifiers:0];
[self addKeys:alphabet withModifiers:UIKeyModifierShift];
Expand All @@ -222,34 +191,23 @@ - (void)addKey:(NSString *)key withModifiers:(UIKeyModifierFlags)modifiers {

}

- (CapsLockTarget)capsLockTarget {
NSString *target = [[NSUserDefaults standardUserDefaults] stringForKey:kiSHCapsLockMapping];
if([target isEqualToString:@"esc"]) {
return kEsc;
} else if([target isEqualToString:@"ctrl"]) {
return kCtrl;
}
return kNone;
}

- (void)keyCommandTriggered:(UIKeyCommand *)sender {
dispatch_async(dispatch_get_main_queue(), ^{
[self handleKeyCommand:sender];
});
}

- (void)handleCapsLockWithCommand:(UIKeyCommand *)command {
CapsLockTarget target = self.currentCapsLocktarget;
CapsLockMapping target = UserPreferences.shared.capsLockMapping;
NSString *newInput = command.input ? command.input : @"";
UIKeyModifierFlags flags = command.modifierFlags;
flags ^= UIKeyModifierAlphaShift;
if(target == kEsc) {
if(target == CapsLockMapEscape) {
newInput = UIKeyInputEscape;
} else if(target == kCtrl) {
} else if(target == CapsLockMapControl) {
if([newInput length] == 0) {
return;
}

flags |= UIKeyModifierControl;
} else {
return;
Expand Down
1 change: 0 additions & 1 deletion app/TerminalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ - (void)viewDidLoad {
[self _updateStyleFromPreferences:NO];
[[UserPreferences shared] addObserver:self forKeyPath:@"theme" options:NSKeyValueObservingOptionNew context:nil];

[self.termView registerExternalKeyboardNotificationsToNotificationCenter:center];
if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[self.bar removeArrangedSubview:self.hideKeyboardButton];
[self.hideKeyboardButton removeFromSuperview];
Expand Down
10 changes: 5 additions & 5 deletions app/UserPreferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, CapslockMapping) {
CapslockMapControl,
CapslockMapEscape,
CapslockMapNone,
typedef NS_ENUM(NSInteger, CapsLockMapping) {
CapsLockMapNone = 0,
CapsLockMapControl,
CapsLockMapEscape,
};

NS_ASSUME_NONNULL_BEGIN
Expand All @@ -35,7 +35,7 @@ extern NSString *const kThemeBackgroundColor;

@interface UserPreferences : NSObject

@property (nonatomic) CapslockMapping capslockMapping;
@property (nonatomic) CapsLockMapping capsLockMapping;
@property (nonatomic) Theme *theme;
@property (nonatomic, copy) NSNumber *fontSize;

Expand Down
14 changes: 8 additions & 6 deletions app/UserPreferences.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#import <UIKit/UIKit.h>
#import "UserPreferences.h"

static NSString *const kPreferenceMapCapsLockAsControlKey = @"Caps Lock Mapping";
static NSString *const kPreferenceCapsLockMappingKey = @"Caps Lock Mapping";
static NSString *const kPreferenceFontSizeKey = @"Font Size";
static NSString *const kPreferenceThemeKey = @"Theme";
static NSString *const kPreferenceBackgroundKey = @"Background Color";
Expand All @@ -32,18 +32,20 @@ - (instancetype)init {
_defaults = [NSUserDefaults standardUserDefaults];
Theme *defaultTheme = [Theme presetThemeNamed:@"Light"];
[_defaults registerDefaults:@{kPreferenceFontSizeKey: @(12),
kPreferenceThemeKey: defaultTheme.properties}];
kPreferenceThemeKey: defaultTheme.properties,
kPreferenceCapsLockMappingKey: @(CapsLockMapControl),
}];
_theme = [[Theme alloc] initWithProperties:[_defaults objectForKey:kPreferenceThemeKey]];
}
return self;
}

- (BOOL)mapCapsLockAsControl {
return [_defaults boolForKey:kPreferenceMapCapsLockAsControlKey];
- (CapsLockMapping)capsLockMapping {
return [_defaults integerForKey:kPreferenceCapsLockMappingKey];
}

- (void)setMapCapsLockAsControl:(BOOL)mapCapsLockAsControl {
[_defaults setBool:mapCapsLockAsControl forKey:kPreferenceMapCapsLockAsControlKey];
- (void)setCapsLockMapping:(CapsLockMapping)capsLockMapping {
[_defaults setInteger:capsLockMapping forKey:kPreferenceCapsLockMappingKey];
}

- (NSNumber *)fontSize {
Expand Down
6 changes: 6 additions & 0 deletions iSH.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
BB792B5D1F96D90D00FFB7A4 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = BB792B5C1F96D90D00FFB7A4 /* Assets.xcassets */; };
BB792B601F96D90D00FFB7A4 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = BB792B5E1F96D90D00FFB7A4 /* LaunchScreen.storyboard */; };
BB792B631F96D90D00FFB7A4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = BB792B621F96D90D00FFB7A4 /* main.m */; };
BB82A7FD21B4C2E8006AA5FD /* CapsLockMappingViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BB82A7FC21B4C2E8006AA5FD /* CapsLockMappingViewController.m */; };
BB88F4942154760800A341FD /* FileProviderExtension.m in Sources */ = {isa = PBXBuildFile; fileRef = BB88F4932154760800A341FD /* FileProviderExtension.m */; };
BB88F4972154760800A341FD /* FileProviderItem.m in Sources */ = {isa = PBXBuildFile; fileRef = BB88F4962154760800A341FD /* FileProviderItem.m */; };
BB88F49A2154760800A341FD /* FileProviderEnumerator.m in Sources */ = {isa = PBXBuildFile; fileRef = BB88F4992154760800A341FD /* FileProviderEnumerator.m */; };
Expand Down Expand Up @@ -195,6 +196,8 @@
BB7D93802087C2890008DA78 /* tty.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = tty.h; sourceTree = "<group>"; };
BB7D93812087C2890008DA78 /* debug.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = debug.h; sourceTree = "<group>"; };
BB7D93822087C2890008DA78 /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
BB82A7FB21B4C2E8006AA5FD /* CapsLockMappingViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CapsLockMappingViewController.h; sourceTree = "<group>"; };
BB82A7FC21B4C2E8006AA5FD /* CapsLockMappingViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CapsLockMappingViewController.m; sourceTree = "<group>"; };
BB8636F92167F4F200E7ADC0 /* cpu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cpu.h; sourceTree = "<group>"; };
BB8636FA2167F4F200E7ADC0 /* cpuid.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cpuid.h; sourceTree = "<group>"; };
BB8636FB2167F4F200E7ADC0 /* decode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = decode.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -611,6 +614,8 @@
8632A7BE219A59FB00F02325 /* UserPreferences.m */,
9A28E4E8219A8B670073D200 /* ThemeViewController.h */,
9A28E4E9219A8B670073D200 /* ThemeViewController.m */,
BB82A7FB21B4C2E8006AA5FD /* CapsLockMappingViewController.h */,
BB82A7FC21B4C2E8006AA5FD /* CapsLockMappingViewController.m */,
);
name = About;
sourceTree = "<group>";
Expand Down Expand Up @@ -888,6 +893,7 @@
BBFB557621586F9200DFE6DE /* DismissSegue.m in Sources */,
BB455E111FB37F6600AFB48B /* DelayedUITask.m in Sources */,
BB0FC5921F980A6C00803272 /* Terminal.m in Sources */,
BB82A7FD21B4C2E8006AA5FD /* CapsLockMappingViewController.m in Sources */,
8632A7BF219A59FB00F02325 /* UserPreferences.m in Sources */,
BB792B631F96D90D00FFB7A4 /* main.m in Sources */,
BB60F55221573FCA003A4E52 /* BarButton.m in Sources */,
Expand Down

0 comments on commit 490830c

Please sign in to comment.