commit 65ab9c311530e6642366b12d73de6eb34374cdc9 Author: relikd Date: Sun Mar 19 13:19:25 2023 +0100 Initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0e1b3d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/*.app +/*.tar.gz +*.xcodeproj diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3072282 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2023 relikd + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d96b38f --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +# usage: make [CONFIG=debug|release] + +ifeq ($(CONFIG), debug) + CFLAGS=-Onone -g +else + CFLAGS=-O +endif + +PLIST=$(shell grep -A1 $(1) src/Info.plist | tail -1 | cut -d'>' -f2 | cut -d'<' -f1) + + +Darker.app: SDK_PATH=$(shell xcrun --show-sdk-path --sdk macosx) +Darker.app: src/* + @mkdir -p Darker.app/Contents/MacOS/ + # compile x64 + swiftc ${CFLAGS} src/main.swift -target x86_64-apple-macos10.10 -emit-executable -sdk ${SDK_PATH} -o bin_x64 + # compile arm64 + swiftc ${CFLAGS} src/main.swift -target arm64-apple-macos10.10 -emit-executable -sdk ${SDK_PATH} -o bin_arm64 + # make universal bundle + lipo -create bin_x64 bin_arm64 -o Darker.app/Contents/MacOS/Darker + @rm bin_x64 bin_arm64 + @echo 'APPL????' > Darker.app/Contents/PkgInfo + @mkdir -p Darker.app/Contents/Resources/ + @cp src/AppIcon.icns Darker.app/Contents/Resources/AppIcon.icns + @cp src/Info.plist Darker.app/Contents/Info.plist + @touch Darker.app + +.PHONY: sign +sign: Darker.app + codesign -v -s 'Apple Development' --options=runtime --timestamp Darker.app + @echo + @echo 'Verify Signature...' + @echo + codesign -dvv Darker.app + @echo + codesign -vvv --deep --strict Darker.app + @echo + spctl -vvv --assess --type exec Darker.app + +.PHONY: release +release: VERSION=$(call PLIST,CFBundleShortVersionString) +release: Darker.app + tar -czf "Darker_v${VERSION}.tar.gz" Darker.app diff --git a/README.md b/README.md new file mode 100644 index 0000000..4cb25a3 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +[![macOS 10.10+](https://img.shields.io/badge/macOS-10.10+-888)](#install) +[![Current release](https://img.shields.io/github/release/relikd/Darker)](https://github.com/relikd/Darker/releases) +[![All downloads](https://img.shields.io/github/downloads/relikd/Darker/total)](https://github.com/relikd/Darker/releases) + + +# Darker + +Darker allows you to dim your screen beyond the last screen brightness beam. + +![menu bar icons](img/screen.png) + + +## Install + +You will need macOS 10.10 or newer. + Download and unzip the tar.gz from [latest release](https://github.com/relikd/Darker/releases/latest). + +Alternatively, you can compile Darker from source by running `make`. + +Or call the script directly (`swift src/main.swift`) without building an app bundle. + + +## FAQ + +### Why? + +There are a number of other tools in the AppStore that do exactly the same. +So why bother in creating a new solution? +Well, previously I used QuickShade and was happy with it. +But I moved away from the AppStore and this was my last application that was only available via AppStore. +I asked the developer if the source code is available but got no reply. +So, thats why you have this open source project now ;-) + +The app is just a wrapper around a single code line (`CGSetDisplayTransferByTable`) and was finished in just half a day. +Enjoy. + + +### Develop + +You can either run the `main.swift` file directly with `swift main.swift`, via Terminal `./main.swift` (`chmod 755 main.swift`), or create a new Xcode project. +In Xcode, select the Command-Line template and replace the template provided `main.swift` with this one. diff --git a/img/icon-16-alt.svg b/img/icon-16-alt.svg new file mode 100644 index 0000000..419fbb2 --- /dev/null +++ b/img/icon-16-alt.svg @@ -0,0 +1,8 @@ + + + + + + diff --git a/img/icon-16.svg b/img/icon-16.svg new file mode 100644 index 0000000..c285947 --- /dev/null +++ b/img/icon-16.svg @@ -0,0 +1,9 @@ + + + + + + + + diff --git a/img/icon-512.svg b/img/icon-512.svg new file mode 100644 index 0000000..2ea308c --- /dev/null +++ b/img/icon-512.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + diff --git a/img/screen.png b/img/screen.png new file mode 100644 index 0000000..da67f65 Binary files /dev/null and b/img/screen.png differ diff --git a/src/AppIcon.icns b/src/AppIcon.icns new file mode 100644 index 0000000..4ae706c Binary files /dev/null and b/src/AppIcon.icns differ diff --git a/src/Info.plist b/src/Info.plist new file mode 100644 index 0000000..716bc13 --- /dev/null +++ b/src/Info.plist @@ -0,0 +1,36 @@ + + + + + CFBundleIdentifier + de.relikd.Darker + CFBundleExecutable + Darker + CFBundleName + Darker + CFBundleIconFile + AppIcon + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 42 + LSMinimumSystemVersion + 10.10 + LSBackgroundOnly + + LSUIElement + + NSHighResolutionCapable + + NSSupportsSuddenTermination + + NSPrincipalClass + NSApplication + NSHumanReadableCopyright + Copyright © 2023 relikd. + + diff --git a/src/main.swift b/src/main.swift new file mode 100644 index 0000000..4da7072 --- /dev/null +++ b/src/main.swift @@ -0,0 +1,88 @@ +#!/usr/bin/env swift +import ApplicationServices +import Cocoa +import AppKit + +class AppDelegate: NSObject, NSApplicationDelegate { + private var statusItem: NSStatusItem! + private var value: Float = 1.0 + + func applicationDidFinishLaunching(_ aNotification: Notification) { + UserDefaults.standard.register(defaults: ["value": 1.0]) + value = UserDefaults.standard.float(forKey: "value") + updateGamma() + // create status menu icon + self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) + self.statusItem.button?.image = NSImage.statusIcon + self.statusItem.menu = NSMenu(title: "") + self.statusItem.menu!.addItem(sliderMenuItem()) + self.statusItem.menu!.addItem(NSMenuItem.separator()) + self.statusItem.menu!.addItem(withTitle: "Quit", action: #selector(NSApp.terminate), keyEquivalent: "q") + } + + private func sliderMenuItem() -> NSMenuItem { + let slider = NSSlider(frame: NSRect(x: 10, y: 5, width: 150, height: 0)) + slider.frame.size.height = slider.fittingSize.height + slider.target = self + slider.action = #selector(sliderCallback) + slider.floatValue = value + slider.numberOfTickMarks = 9 + slider.allowsTickMarkValuesOnly = true + let item = NSMenuItem() + item.view = NSView(frame: slider.frame.insetBy(dx: -10, dy: -5)) // add padding + item.view!.addSubview(slider) + return item + } + + @objc + private func sliderCallback(sender: NSSlider) { + value = max(sender.floatValue, 0.1) // safety measure, never get venta black + UserDefaults.standard.setValue(value, forKey: "value") + updateGamma() + } + + private func updateGamma() { + let table: [CGGammaValue] = [0, value] + CGSetDisplayTransferByTable(CGMainDisplayID(), 2, table, table, table) + + } +} + +// MARK: - Menu Bar Icon - + +extension NSImage { + static var statusIcon: NSImage { + let img = NSImage(size: .init(width: 16, height: 16), flipped: true) { rect in + let icon = UserDefaults.standard.integer(forKey: "icon") + let w = min(rect.width, rect.height) + let ctx = NSGraphicsContext.current!.cgContext + // clip to circle + ctx.addEllipse(in: rect) + ctx.clip() + // draw moon + if icon == 0 || icon == 1 { + ctx.addRect(rect) + ctx.addEllipse(in: .init(x: 0.3 * w, y: -0.1 * w, width: 0.84 * w, height: 0.84 * w)) + ctx.fillPath(using: .evenOdd) + } + // draw stripes background + if icon == 0 || icon == 2 { + ctx.setFillColor(gray: 1.0, alpha: 0.2) + ctx.addRect(rect); ctx.fillPath() + ctx.addRect(rect.offsetBy(dx: 0, dy: 0.2 * rect.height)); ctx.fillPath() + ctx.addRect(rect.offsetBy(dx: 0, dy: 0.45 * rect.height)); ctx.fillPath() + ctx.addRect(rect.offsetBy(dx: 0, dy: 0.7 * rect.height)); ctx.fillPath() + } + return true + } + img.isTemplate = true + return img + } +} + +// MARK: - Main Entry + +let delegate = AppDelegate() +NSApplication.shared.delegate = delegate +NSApplication.shared.run() +// _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)