Skip to content

Commit

Permalink
Fall back to defaults if user preference is set to incorrect type
Browse files Browse the repository at this point in the history
  • Loading branch information
emreyolcu committed May 29, 2024
1 parent 2b69abd commit a59f543
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions DiscreteScroll.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,14 @@
buildSettings = {
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
INFOPLIST_FILE = DiscreteScroll/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.9;
MARKETING_VERSION = 1.0.0;
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.emreyolcu.DiscreteScroll;
PRODUCT_NAME = "$(TARGET_NAME)";
};
Expand All @@ -266,13 +267,14 @@
buildSettings = {
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 2;
INFOPLIST_FILE = DiscreteScroll/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.9;
MARKETING_VERSION = 1.0.0;
MARKETING_VERSION = 1.0.1;
PRODUCT_BUNDLE_IDENTIFIER = com.emreyolcu.DiscreteScroll;
PRODUCT_NAME = "$(TARGET_NAME)";
};
Expand Down
2 changes: 1 addition & 1 deletion DiscreteScroll/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
17 changes: 9 additions & 8 deletions DiscreteScroll/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include <ApplicationServices/ApplicationServices.h>

#define DEFAULT_LINES 3
#define SIGN(x) (((x) > 0) - ((x) < 0))

static int LINES;

CGEventRef callback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *userInfo)
{
if (!CGEventGetIntegerValueField(event, kCGScrollWheelEventIsContinuous)) {
int64_t delta = CGEventGetIntegerValueField(event, kCGScrollWheelEventPointDeltaAxis1);
if (CGEventGetIntegerValueField(event, kCGScrollWheelEventIsContinuous) == 0) {
int delta = (int)CGEventGetIntegerValueField(event, kCGScrollWheelEventPointDeltaAxis1);
CGEventSetIntegerValueField(event, kCGScrollWheelEventDeltaAxis1, SIGN(delta) * LINES);
}

Expand All @@ -32,24 +33,24 @@ int main(void)
(const void **)&kAXTrustedCheckOptionPrompt, (const void **)&kCFBooleanTrue, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks
);
Boolean trusted = AXIsProcessTrustedWithOptions(options);
bool trusted = AXIsProcessTrustedWithOptions(options);
CFRelease(options);
if (!trusted)
displayNoticeAndExit(
CFSTR("Restart DiscreteScroll after granting it access to accessibility features.")
);

CFStringRef key = CFSTR("lines");
CFNumberRef value = (CFNumberRef)CFPreferencesCopyAppValue(
key, kCFPreferencesCurrentApplication
CFSTR("lines"), kCFPreferencesCurrentApplication
);
Boolean got = false;
bool got = false;
if (value) {
got = CFNumberGetValue(value, kCFNumberIntType, &LINES);
if (CFGetTypeID(value) == CFNumberGetTypeID())
got = CFNumberGetValue(value, kCFNumberIntType, &LINES);
CFRelease(value);
}
if (!got)
LINES = 3;
LINES = DEFAULT_LINES;

CFMachPortRef tap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault,
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ As of May 2024, this application works on macOS versions 10.9–14.0.

### Installation

You may download the binary [here](https://github.com/emreyolcu/discrete-scroll/releases/download/v1.0.0/DiscreteScroll.zip).
You may download the binary [here](https://github.com/emreyolcu/discrete-scroll/releases/download/v1.0.1/DiscreteScroll.zip).

It needs to be run each time you boot.
If you want this to be automatic, do the following:
Expand All @@ -40,7 +40,10 @@ This number may even be negative, which inverts scrolling direction.
defaults write com.emreyolcu.DiscreteScroll lines -int LINES
```

You should restart the application for this setting to take effect.
If you set the key `lines` to some value other than an integer,
the default value of 3 is used as a fallback.

You should restart the application for the setting to take effect.

### Potential problems

Expand Down

0 comments on commit a59f543

Please sign in to comment.