Skip to content

Commit

Permalink
feat: lazy commit, more fuzzing and a few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tillt committed Jan 24, 2024
1 parent bac1a9e commit 233f1d3
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 27 deletions.
6 changes: 6 additions & 0 deletions KompleteSynthesia/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ - (void)synthesiaStateUpdate:(NSString*)status
{
if (self.statusMenu.itemArray.count > 1) {
NSMenuItem* item = self.statusMenu.itemArray[1];
if ([item.title compare:status] == NSOrderedSame) {
// Lets avoid useless further UI and controller updates as we are receiving
// a lot of state updates for Synthesia.
// FIXME: Seems weird that we get flooded by state updates.
return;
}
item.title = status;
}
[self updateButtonStates];
Expand Down
7 changes: 6 additions & 1 deletion KompleteSynthesia/FuzzingWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ NS_ASSUME_NONNULL_BEGIN
@class HIDController;
@protocol PreferencesDelegate;

@interface FuzzingWindowController : NSWindowController
@interface FuzzingWindowController : NSWindowController <NSTextFieldDelegate>

@property (nonatomic, weak) IBOutlet NSTextField* initialCommand;
@property (nonatomic, weak) IBOutlet NSTextField* currentControlCommand;
@property (nonatomic, weak) IBOutlet NSSliderCell* delaySlider;

@property (nonatomic, weak) IBOutlet NSButton* pauseButton;
@property (nonatomic, weak) IBOutlet NSButton* stopButton;
@property (nonatomic, weak) IBOutlet NSButton* startButton;

@property (nonatomic, weak) HIDController* hidController;

@property (nonatomic, weak) id<PreferencesDelegate> delegate;

- (IBAction)start:(id)sender;
- (IBAction)stop:(id)sender;
- (IBAction)pause:(id)sender;

@end

Expand Down
77 changes: 71 additions & 6 deletions KompleteSynthesia/FuzzingWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface FuzzingWindowController ()
@implementation FuzzingWindowController {
NSTimer* commandUpdateTimer;
NSTimer* fuzzTimer;
BOOL paused;
}

- (NSString*)hexStringFromBinaryData:(unsigned char*)data withLength:(size_t)length
Expand All @@ -34,14 +35,26 @@ - (NSString*)hexStringFromBinaryData:(unsigned char*)data withLength:(size_t)len
return output;
}

- (void)binaryDataFromHexString:(NSString*)input withData:(unsigned char*)data withLength:(size_t)length
{
NSArray* hexStringBytes = [input componentsSeparatedByString:@" "];
size_t byteCount = MIN(length, hexStringBytes.count);
for (size_t i = 0; i < byteCount; i++) {
NSString* hex = hexStringBytes[i];
const char* chars = [hex UTF8String];
data[i] = strtoul(chars, NULL, 16);
}
}

- (void)windowDidLoad
{
[super windowDidLoad];

[_delegate preferencesUpdatedKeyState:0x00 forKeyIndex:0];

_initialCommand.stringValue = [self hexStringFromBinaryData:_hidController.initialCommand
withLength:_hidController.initialCommandLength];
_initialCommand.delegate = self;

[_delegate preferencesUpdatedKeyState:0x00 forKeyIndex:0];

commandUpdateTimer = [NSTimer
scheduledTimerWithTimeInterval:kCommandUpdateTimerDelay
Expand All @@ -51,20 +64,62 @@ - (void)windowDidLoad
[self hexStringFromBinaryData:self->_hidController.lightGuideUpdateMessage
withLength:self->_hidController.lightGuideUpdateMessageSize];
}];
paused = NO;
[self updateButtonStates];
}

- (IBAction)stop:(id)sender
- (void)controlTextDidEndEditing:(NSNotification*)notification
{
NSTextField* textField = [notification object];
if (textField != _initialCommand) {
return;
}
[self binaryDataFromHexString:textField.stringValue
withData:_hidController.initialCommand
withLength:_hidController.initialCommandLength];
}

- (void)updateButtonStates
{
if (paused) {
_startButton.enabled = NO;
_pauseButton.enabled = YES;
_stopButton.enabled = YES;
} else {
_startButton.enabled = fuzzTimer == nil;
_stopButton.enabled = fuzzTimer != nil;
_pauseButton.enabled = fuzzTimer != nil;
}
}

- (void)stopTimer
{
if (fuzzTimer != nil) {
[fuzzTimer invalidate];
}
fuzzTimer = nil;
}

- (IBAction)start:(id)sender
- (IBAction)stop:(id)sender
{
self->_hidController.lightGuideUpdateMessage[0] = 0x01;
[_delegate preferencesUpdatedKeyState:0x06 forKeyIndex:0];
[self stopTimer];
paused = NO;
[self updateButtonStates];
}

- (IBAction)pause:(id)sender
{
if (fuzzTimer != nil) {
paused = YES;
[self stopTimer];
} else {
paused = NO;
[self startTimer];
}
}

- (void)startTimer
{
if (fuzzTimer != nil) {
[fuzzTimer invalidate];
}
Expand All @@ -83,4 +138,14 @@ - (IBAction)start:(id)sender
}];
}

- (IBAction)start:(id)sender
{
_hidController.lightGuideUpdateMessage[0] = 0x01;
[_delegate preferencesUpdatedKeyState:0x06 forKeyIndex:0];

[self startTimer];
paused = NO;
[self updateButtonStates];
}

@end
28 changes: 21 additions & 7 deletions KompleteSynthesia/FuzzingWindowController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<outlet property="currentControlCommand" destination="1CH-OP-Lad" id="RoF-B8-tvM"/>
<outlet property="delaySlider" destination="Zup-ll-duJ" id="ICQ-an-1yh"/>
<outlet property="initialCommand" destination="AyS-QQ-osj" id="4ft-ng-j2O"/>
<outlet property="pauseButton" destination="gRN-uL-ta6" id="MJx-ey-XUT"/>
<outlet property="startButton" destination="GvE-ZM-Mmg" id="usk-ZW-ebC"/>
<outlet property="stopButton" destination="HY4-8F-3wK" id="ITY-OA-Pc9"/>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
</connections>
</customObject>
Expand Down Expand Up @@ -47,7 +50,7 @@
<textField focusRingType="none" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="AyS-QQ-osj">
<rect key="frame" x="166" y="257" width="134" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" allowsUndo="NO" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="13u-gd-xUW">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" allowsUndo="NO" sendsActionOnEndEditing="YES" borderStyle="bezel" drawsBackground="YES" id="13u-gd-xUW">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -72,7 +75,7 @@
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="HY4-8F-3wK">
<rect key="frame" x="384" y="279" width="83" height="32"/>
<rect key="frame" x="357" y="287" width="110" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Stop" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="foJ-zF-duB">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -83,7 +86,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="GvE-ZM-Mmg">
<rect key="frame" x="13" y="279" width="107" height="32"/>
<rect key="frame" x="15" y="287" width="110" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Start" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="faJ-MR-qNw">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -93,17 +96,28 @@
<action selector="start:" target="-2" id="nDR-1B-AK8"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="gRN-uL-ta6">
<rect key="frame" x="186" y="287" width="110" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Pause" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="84w-1K-5ez">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="pause:" target="-2" id="hK9-uf-FEn"/>
</connections>
</button>
<slider verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="kgo-Da-Vcf">
<rect key="frame" x="164" y="320" width="298" height="28"/>
<rect key="frame" x="74" y="320" width="388" height="28"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<sliderCell key="cell" alignment="left" minValue="1" maxValue="20" doubleValue="1" tickMarkPosition="below" numberOfTickMarks="10" sliderType="linear" id="Zup-ll-duJ">
<sliderCell key="cell" alignment="left" minValue="1" maxValue="50" doubleValue="1" tickMarkPosition="below" numberOfTickMarks="10" sliderType="linear" id="Zup-ll-duJ">
<font key="font" size="12" name="Helvetica"/>
</sliderCell>
</slider>
<textField focusRingType="none" horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="AZx-yN-tSx">
<rect key="frame" x="20" y="329" width="140" height="16"/>
<rect key="frame" x="20" y="329" width="50" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" lineBreakMode="clipping" alignment="right" title="Delay:" id="4Ks-Le-152">
<textFieldCell key="cell" lineBreakMode="clipping" alignment="left" title="Delay:" id="4Ks-Le-152">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
Expand Down
12 changes: 8 additions & 4 deletions KompleteSynthesia/HIDController.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// Confirmed that this is the way KompleteKontrol initializes the controller by capturing
// the USB traffic.
const uint8_t kCommandInit = 0xA0;
const uint8_t kKompleteKontrolInit[] = {kCommandInit, 0x00, 0x00};
uint8_t kKompleteKontrolInit[] = {kCommandInit, 0x00, 0x00};

/*
// FIXME: This likely is not be enough to get the MK3 controller fully initialized. It is what
Expand Down Expand Up @@ -314,10 +314,14 @@ - (void)receivedReport:(unsigned char*)report length:(int)length
for (int i = 0; i < length; i++) {
[hex appendFormat:@"%02x ", report[i]];
}
NSLog(@"hid report: %@", hex);
[log logLine:[NSString stringWithFormat:@"hid report: %@", hex]];
NSString* trail = @"";
if (report[0] != 0x01) {
NSLog(@"ignoring report %02Xh", report[0]);
trail = [trail stringByAppendingString:@" (ignored!)"];
}
[log logLine:[NSString stringWithFormat:@"hid report: %@%@", hex, trail]];
#endif

NSLog(@"hid report: %@", hex);
if (report[0] != 0x01) {
NSLog(@"ignoring report %02Xh", report[0]);
return;
Expand Down
18 changes: 9 additions & 9 deletions KompleteSynthesia/SynthesiaController.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ - (id)initWithLogViewController:(LogViewController*)logViewController delegate:(
dataFormatExpected = NO;
needsConfigurationPatch = YES;

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(synthesiaMayHaveChangedStatus:)
name:NSWorkspaceDidActivateApplicationNotification
object:nil];

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(synthesiaMayHaveChangedStatus:)
name:NSWorkspaceDidTerminateApplicationNotification
object:nil];
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
[[workspace notificationCenter] addObserver:self
selector:@selector(synthesiaMayHaveChangedStatus:)
name:NSWorkspaceDidActivateApplicationNotification
object:nil];
[[workspace notificationCenter] addObserver:self
selector:@selector(synthesiaMayHaveChangedStatus:)
name:NSWorkspaceDidTerminateApplicationNotification
object:nil];
}
return self;
}
Expand Down

0 comments on commit 233f1d3

Please sign in to comment.