Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unlock only when external monitor is connected feature #128

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions BLEUnlock.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = 42LGPQYC7M;
"DEVELOPMENT_TEAM[sdk=macosx*]" = "";
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Launcher/Info.plist;
INSTALL_PATH = "";
Expand All @@ -407,10 +408,12 @@
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "Developer ID Application";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
DEVELOPMENT_TEAM = 42LGPQYC7M;
"DEVELOPMENT_TEAM[sdk=macosx*]" = "";
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = Launcher/Info.plist;
INSTALL_PATH = "";
Expand Down Expand Up @@ -554,13 +557,15 @@
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = 42LGPQYC7M;
"DEVELOPMENT_TEAM[sdk=macosx*]" = "";
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = "";
INFOPLIST_FILE = BLEUnlock/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = "";
PRODUCT_BUNDLE_IDENTIFIER = jp.sone.BLEUnlock;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -582,16 +587,19 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_ENTITLEMENTS = BLEUnlock/BLEUnlock.entitlements;
CODE_SIGN_IDENTITY = "Developer ID Application";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Manual;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = 42LGPQYC7M;
"DEVELOPMENT_TEAM[sdk=macosx*]" = "";
ENABLE_HARDENED_RUNTIME = YES;
FRAMEWORK_SEARCH_PATHS = "";
INFOPLIST_FILE = BLEUnlock/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MACOSX_DEPLOYMENT_TARGET = 10.15;
MARKETING_VERSION = "";
PRODUCT_BUNDLE_IDENTIFIER = jp.sone.BLEUnlock;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
98 changes: 97 additions & 1 deletion BLEUnlock/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
let unlockRSSIMenu = NSMenu()
let timeoutMenu = NSMenu()
let lockDelayMenu = NSMenu()
let externalDisplayMenu = NSMenu()
var deviceDict: [UUID: NSMenuItem] = [:]
var monitorMenuItem : NSMenuItem?
let prefs = UserDefaults.standard
Expand All @@ -30,6 +31,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
var unlockedAt = 0.0
var inScreensaver = false
var lastRSSI: Int? = nil
var externalDisplayModelOnly = false

func menuWillOpen(_ menu: NSMenu) {
if menu == deviceMenu {
Expand Down Expand Up @@ -277,6 +279,58 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
return false
}

func getDisplayUUIDs() -> [(localizedName: String, uuidString: String)] {
var displayInfoArray: [(localizedName: String, uuidString: String)] = []

let screens = NSScreen.screens
for screen in screens {
let deviceDescription = screen.deviceDescription
if let screenNumber = deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as? CGDirectDisplayID {
let uuidRef = CGDisplayCreateUUIDFromDisplayID(screenNumber)?.takeRetainedValue()
if let uuidRef = uuidRef {
let uuidString = CFUUIDCreateString(kCFAllocatorDefault, uuidRef) as String
displayInfoArray.append((localizedName: screen.localizedName, uuidString: uuidString))
}
}
}
return displayInfoArray
}

func updateExternalMonitor() {
externalDisplayMenu.removeAllItems()
let selectedDisplayUUIDs = prefs.array(forKey: "externalDisplays") as? [String] ?? []
let displays = getDisplayUUIDs() // Assuming this returns an array of display objects with localizedName and uuidString properties

for display in displays {
let menuItem = NSMenuItem(title: display.localizedName, action: nil, keyEquivalent: "") // Default action to nil
menuItem.representedObject = display.uuidString

if display.localizedName.contains("Built-in") {
// For built-in display, do not set an action and disable the menu item
menuItem.isEnabled = false
} else {
// For external displays, set the action and state based on whether they are selected
menuItem.action = #selector(setExternalDisplays(_:))
menuItem.target = self
menuItem.state = selectedDisplayUUIDs.contains(display.uuidString) ? .on : .off
}

externalDisplayMenu.addItem(menuItem)
}
}

func isExternalDisplayConnected() -> Bool {
let selectedDisplayUUIDs = prefs.array(forKey: "externalDisplays") as? [String] ?? []

if selectedDisplayUUIDs.isEmpty {
return false
}

let connectedDisplayUUIDs = getDisplayUUIDs().map { $0.uuidString }

return selectedDisplayUUIDs.allSatisfy { connectedDisplayUUIDs.contains($0) }
}

func tryUnlockScreen() {
guard !manualLock else { return }
guard ble.presence else { return }
Expand All @@ -291,6 +345,11 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
CGEvent(keyboardEventSource: src, virtualKey: 0x35, keyDown: true)?.post(tap: .cghidEventTap)
CGEvent(keyboardEventSource: src, virtualKey: 0x35, keyDown: false)?.post(tap: .cghidEventTap)
}

if self.prefs.bool(forKey: "externalDisplayModelOnly") && !isExternalDisplayConnected() {
print("External Display is not connected")
return
}

guard !self.prefs.bool(forKey: "wakeWithoutUnlocking") else { return }

Expand Down Expand Up @@ -353,6 +412,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { _ in
checkUpdate()
})
Timer.scheduledTimer(withTimeInterval: 2, repeats: false, block: { _ in
self.updateExternalMonitor()
})
}

@objc func onScreensaverStart() {
Expand Down Expand Up @@ -489,6 +551,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
menuItem.state = value ? .on : .off
prefs.set(value, forKey: "wakeOnProximity")
}

@objc func toggleExternalDisplayModeOnly(_ menuItem: NSMenuItem) {
let value = !prefs.bool(forKey: "externalDisplayModelOnly")
menuItem.state = value ? .on : .off
prefs.set(value, forKey: "externalDisplayModelOnly")
}

@objc func setLockRSSI(_ menuItem: NSMenuItem) {
let value = menuItem.tag
Expand Down Expand Up @@ -551,6 +619,20 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
prefs.set(wakeWithoutUnlocking, forKey: "wakeWithoutUnlocking")
menuItem.state = wakeWithoutUnlocking ? .on : .off
}

@objc func setExternalDisplays(_ menuItem: NSMenuItem) {
guard let uuidString = menuItem.representedObject as? String else { return }

menuItem.state = (menuItem.state == .on) ? .off : .on

var selectedDisplayUUIDs = prefs.array(forKey: "externalDisplays") as? [String] ?? []
if menuItem.state == .on {
selectedDisplayUUIDs.append(uuidString)
} else {
selectedDisplayUUIDs.removeAll { $0 == uuidString }
}
prefs.set(selectedDisplayUUIDs, forKey: "externalDisplays")
}

@objc func lockNow() {
guard !isScreenLocked() else { return }
Expand Down Expand Up @@ -622,6 +704,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
if prefs.bool(forKey: "wakeOnProximity") {
item.state = .on
}

mainMenu.addItem(NSMenuItem.separator())
item = mainMenu.addItem(withTitle: t("external_display_mode_only"), action: #selector(toggleExternalDisplayModeOnly), keyEquivalent: "")
if prefs.bool(forKey: "externalDisplayModelOnly") {
item.state = .on
}

item = mainMenu.addItem(withTitle: t("external_displays"), action: nil, keyEquivalent: "")
item.submenu = externalDisplayMenu
externalDisplayMenu.delegate = self
mainMenu.addItem(NSMenuItem.separator())

item = mainMenu.addItem(withTitle: t("wake_without_unlocking"), action: #selector(toggleWakeWithoutUnlocking), keyEquivalent: "")
if prefs.bool(forKey: "wakeWithoutUnlocking") {
Expand Down Expand Up @@ -705,7 +798,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
if lockDelay != 0 {
ble.proximityTimeout = Double(lockDelay)
}


updateExternalMonitor()

NSUserNotificationCenter.default.delegate = self

let nc = NSWorkspace.shared.notificationCenter;
Expand All @@ -722,6 +817,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate, NSMenuItemVa
if ble.unlockRSSI != ble.UNLOCK_DISABLED && !prefs.bool(forKey: "wakeWithoutUnlocking") && fetchPassword() == nil {
askPassword()
}

checkAccessibility()
checkUpdate()

Expand Down
2 changes: 2 additions & 0 deletions BLEUnlock/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@
"use_screensaver_to_lock" = "Use Screensaver to Lock";
"wake_on_proximity" = "Wake on Proximity";
"wake_without_unlocking" = "Wake without Unlocking";
"external_display_mode_only" = "External Display Detection";
"external_displays" = "External Displays";
2 changes: 1 addition & 1 deletion BLEUnlock/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>1.12.2</string>
<key>CFBundleVersion</key>
<string>796</string>
<string>839</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
1 change: 1 addition & 0 deletions BLEUnlock/da.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "Brug screensaver til at låse";
"wake_on_proximity" = "Vågn op på nærhed";
"wake_without_unlocking" = "Vågn op uden at låse op";
"external_display_mode_only" = "External Display Detection";
1 change: 1 addition & 0 deletions BLEUnlock/de.lproj/Localizable.strings
rakalex marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "Den Bildschirmschoner verwenden zum Sperren";
"wake_on_proximity" = "Aufwachen bei Annäherung";
"wake_without_unlocking" = "Aufwachen ohne Entsperren";
"external_display_mode_only" = "External Display Detection";
1 change: 1 addition & 0 deletions BLEUnlock/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "スクリーンセーバーでロック";
"wake_on_proximity" = "画面スリープから復帰";
"wake_without_unlocking" = "アンロックせずに画面復帰";
"external_display_mode_only" = "External Display Detection";
1 change: 1 addition & 0 deletions BLEUnlock/nb.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "Bruke skjermsparer til å låse";
"wake_on_proximity" = "Vekk ved nærhet";
"wake_without_unlocking" = "Våkne uten å låse opp";
"external_display_mode_only" = "External Display Detection";
1 change: 1 addition & 0 deletions BLEUnlock/sv.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "Använd skärmsläckare för att låsa";
"wake_on_proximity" = "Väck om i närheten";
"wake_without_unlocking" = "Vakna utan att låsa upp";
"external_display_mode_only" = "External Display Detection";
1 change: 1 addition & 0 deletions BLEUnlock/tr.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "Kilit ekranı için ekran kuruyucu kullan";
"wake_on_proximity" = "Uyandırma yakınlığı";
"wake_without_unlocking" = "Kilidi açmadan uyanın";
"external_display_mode_only" = "External Display Detection";
1 change: 1 addition & 0 deletions BLEUnlock/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
"use_screensaver_to_lock" = "用屏保来锁定它";
"wake_on_proximity" = "靠近唤醒";
"wake_without_unlocking" = "唤醒时不需解锁";
"external_display_mode_only" = "External Display Detection";