Skip to content

Commit 5cdc205

Browse files
committed
Add the debug logging feature using the Logger API
The app now requires macOS 11.0+
1 parent 288b2f6 commit 5cdc205

File tree

3 files changed

+179
-25
lines changed

3 files changed

+179
-25
lines changed

MoveCursorToCenterOfActiveWindow.xcodeproj/project.pbxproj

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 50;
6+
objectVersion = 54;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -45,6 +45,7 @@
4545
15427B62255138F800291848 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4646
15427B6E25514F9200291848 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
4747
15427B7125515CC000291848 /* Right Option to Launch MoveCursorToCenterOfActiveWindow.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "Right Option to Launch MoveCursorToCenterOfActiveWindow.json"; sourceTree = "<group>"; };
48+
15C06B782B72827700FAC2ED /* Right Option to Launch MoveCursorToCenterOfActiveWindow in Debug Mode.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = "Right Option to Launch MoveCursorToCenterOfActiveWindow in Debug Mode.json"; sourceTree = "<group>"; };
4849
/* End PBXFileReference section */
4950

5051
/* Begin PBXFrameworksBuildPhase section */
@@ -126,6 +127,7 @@
126127
15427B7025515AFE00291848 /* Karabiner-Element Rules */ = {
127128
isa = PBXGroup;
128129
children = (
130+
15C06B782B72827700FAC2ED /* Right Option to Launch MoveCursorToCenterOfActiveWindow in Debug Mode.json */,
129131
15427B7125515CC000291848 /* Right Option to Launch MoveCursorToCenterOfActiveWindow.json */,
130132
);
131133
path = "Karabiner-Element Rules";
@@ -193,8 +195,9 @@
193195
15427B34255138F100291848 /* Project object */ = {
194196
isa = PBXProject;
195197
attributes = {
198+
BuildIndependentTargetsInParallel = YES;
196199
LastSwiftUpdateCheck = 1130;
197-
LastUpgradeCheck = 1130;
200+
LastUpgradeCheck = 1520;
198201
ORGANIZATIONNAME = Eucrasia;
199202
TargetAttributes = {
200203
15427B3B255138F100291848 = {
@@ -323,16 +326,19 @@
323326
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
324327
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
325328
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
329+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
326330
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
327331
CLANG_WARN_STRICT_PROTOTYPES = YES;
328332
CLANG_WARN_SUSPICIOUS_MOVE = YES;
329333
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
330334
CLANG_WARN_UNREACHABLE_CODE = YES;
331335
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
332336
COPY_PHASE_STRIP = NO;
337+
DEAD_CODE_STRIPPING = YES;
333338
DEBUG_INFORMATION_FORMAT = dwarf;
334339
ENABLE_STRICT_OBJC_MSGSEND = YES;
335340
ENABLE_TESTABILITY = YES;
341+
ENABLE_USER_SCRIPT_SANDBOXING = YES;
336342
GCC_C_LANGUAGE_STANDARD = gnu11;
337343
GCC_DYNAMIC_NO_PIC = NO;
338344
GCC_NO_COMMON_BLOCKS = YES;
@@ -383,16 +389,19 @@
383389
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
384390
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
385391
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
392+
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
386393
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
387394
CLANG_WARN_STRICT_PROTOTYPES = YES;
388395
CLANG_WARN_SUSPICIOUS_MOVE = YES;
389396
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
390397
CLANG_WARN_UNREACHABLE_CODE = YES;
391398
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
392399
COPY_PHASE_STRIP = NO;
400+
DEAD_CODE_STRIPPING = YES;
393401
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
394402
ENABLE_NS_ASSERTIONS = NO;
395403
ENABLE_STRICT_OBJC_MSGSEND = YES;
404+
ENABLE_USER_SCRIPT_SANDBOXING = YES;
396405
GCC_C_LANGUAGE_STANDARD = gnu11;
397406
GCC_NO_COMMON_BLOCKS = YES;
398407
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
@@ -417,13 +426,15 @@
417426
CODE_SIGN_ENTITLEMENTS = MoveCursorToCenterOfActiveWindow/MoveCursorToCenterOfActiveWindow.entitlements;
418427
CODE_SIGN_STYLE = Automatic;
419428
COMBINE_HIDPI_IMAGES = YES;
429+
DEAD_CODE_STRIPPING = YES;
420430
ENABLE_PREVIEWS = YES;
421431
INFOPLIST_FILE = MoveCursorToCenterOfActiveWindow/Info.plist;
422432
LD_RUNPATH_SEARCH_PATHS = (
423433
"$(inherited)",
424434
"@executable_path/../Frameworks",
425435
);
426-
MARKETING_VERSION = 0.1;
436+
MACOSX_DEPLOYMENT_TARGET = 11.0;
437+
MARKETING_VERSION = 0.2;
427438
PRODUCT_BUNDLE_IDENTIFIER = asia.eucr.MoveCursorToCenterOfActiveWindow;
428439
PRODUCT_NAME = "$(TARGET_NAME)";
429440
SWIFT_VERSION = 5.0;
@@ -437,13 +448,15 @@
437448
CODE_SIGN_ENTITLEMENTS = MoveCursorToCenterOfActiveWindow/MoveCursorToCenterOfActiveWindow.entitlements;
438449
CODE_SIGN_STYLE = Automatic;
439450
COMBINE_HIDPI_IMAGES = YES;
451+
DEAD_CODE_STRIPPING = YES;
440452
ENABLE_PREVIEWS = YES;
441453
INFOPLIST_FILE = MoveCursorToCenterOfActiveWindow/Info.plist;
442454
LD_RUNPATH_SEARCH_PATHS = (
443455
"$(inherited)",
444456
"@executable_path/../Frameworks",
445457
);
446-
MARKETING_VERSION = 0.1;
458+
MACOSX_DEPLOYMENT_TARGET = 11.0;
459+
MARKETING_VERSION = 0.2;
447460
PRODUCT_BUNDLE_IDENTIFIER = asia.eucr.MoveCursorToCenterOfActiveWindow;
448461
PRODUCT_NAME = "$(TARGET_NAME)";
449462
SWIFT_VERSION = 5.0;
@@ -457,6 +470,7 @@
457470
BUNDLE_LOADER = "$(TEST_HOST)";
458471
CODE_SIGN_STYLE = Automatic;
459472
COMBINE_HIDPI_IMAGES = YES;
473+
DEAD_CODE_STRIPPING = YES;
460474
INFOPLIST_FILE = MoveCursorToCenterOfActiveWindowTests/Info.plist;
461475
LD_RUNPATH_SEARCH_PATHS = (
462476
"$(inherited)",
@@ -478,6 +492,7 @@
478492
BUNDLE_LOADER = "$(TEST_HOST)";
479493
CODE_SIGN_STYLE = Automatic;
480494
COMBINE_HIDPI_IMAGES = YES;
495+
DEAD_CODE_STRIPPING = YES;
481496
INFOPLIST_FILE = MoveCursorToCenterOfActiveWindowTests/Info.plist;
482497
LD_RUNPATH_SEARCH_PATHS = (
483498
"$(inherited)",
@@ -498,12 +513,14 @@
498513
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
499514
CODE_SIGN_STYLE = Automatic;
500515
COMBINE_HIDPI_IMAGES = YES;
516+
DEAD_CODE_STRIPPING = YES;
501517
INFOPLIST_FILE = MoveCursorToCenterOfActiveWindowUITests/Info.plist;
502518
LD_RUNPATH_SEARCH_PATHS = (
503519
"$(inherited)",
504520
"@executable_path/../Frameworks",
505521
"@loader_path/../Frameworks",
506522
);
523+
MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
507524
PRODUCT_BUNDLE_IDENTIFIER = asia.eucr.MoveCursorToCenterOfActiveWindowUITests;
508525
PRODUCT_NAME = "$(TARGET_NAME)";
509526
SWIFT_VERSION = 5.0;
@@ -517,12 +534,14 @@
517534
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
518535
CODE_SIGN_STYLE = Automatic;
519536
COMBINE_HIDPI_IMAGES = YES;
537+
DEAD_CODE_STRIPPING = YES;
520538
INFOPLIST_FILE = MoveCursorToCenterOfActiveWindowUITests/Info.plist;
521539
LD_RUNPATH_SEARCH_PATHS = (
522540
"$(inherited)",
523541
"@executable_path/../Frameworks",
524542
"@loader_path/../Frameworks",
525543
);
544+
MACOSX_DEPLOYMENT_TARGET = "$(RECOMMENDED_MACOSX_DEPLOYMENT_TARGET)";
526545
PRODUCT_BUNDLE_IDENTIFIER = asia.eucr.MoveCursorToCenterOfActiveWindowUITests;
527546
PRODUCT_NAME = "$(TARGET_NAME)";
528547
SWIFT_VERSION = 5.0;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "1520"
4+
version = "1.7">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "15427B3B255138F100291848"
18+
BuildableName = "MoveCursorToCenterOfActiveWindow.app"
19+
BlueprintName = "MoveCursorToCenterOfActiveWindow"
20+
ReferencedContainer = "container:MoveCursorToCenterOfActiveWindow.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES"
30+
shouldAutocreateTestPlan = "YES">
31+
<Testables>
32+
<TestableReference
33+
skipped = "NO">
34+
<BuildableReference
35+
BuildableIdentifier = "primary"
36+
BlueprintIdentifier = "15427B50255138F800291848"
37+
BuildableName = "MoveCursorToCenterOfActiveWindowTests.xctest"
38+
BlueprintName = "MoveCursorToCenterOfActiveWindowTests"
39+
ReferencedContainer = "container:MoveCursorToCenterOfActiveWindow.xcodeproj">
40+
</BuildableReference>
41+
</TestableReference>
42+
<TestableReference
43+
skipped = "NO">
44+
<BuildableReference
45+
BuildableIdentifier = "primary"
46+
BlueprintIdentifier = "15427B5B255138F800291848"
47+
BuildableName = "MoveCursorToCenterOfActiveWindowUITests.xctest"
48+
BlueprintName = "MoveCursorToCenterOfActiveWindowUITests"
49+
ReferencedContainer = "container:MoveCursorToCenterOfActiveWindow.xcodeproj">
50+
</BuildableReference>
51+
</TestableReference>
52+
</Testables>
53+
</TestAction>
54+
<LaunchAction
55+
buildConfiguration = "Debug"
56+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
57+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
58+
launchStyle = "0"
59+
useCustomWorkingDirectory = "NO"
60+
ignoresPersistentStateOnLaunch = "NO"
61+
debugDocumentVersioning = "YES"
62+
debugServiceExtension = "internal"
63+
allowLocationSimulation = "YES">
64+
<BuildableProductRunnable
65+
runnableDebuggingMode = "0">
66+
<BuildableReference
67+
BuildableIdentifier = "primary"
68+
BlueprintIdentifier = "15427B3B255138F100291848"
69+
BuildableName = "MoveCursorToCenterOfActiveWindow.app"
70+
BlueprintName = "MoveCursorToCenterOfActiveWindow"
71+
ReferencedContainer = "container:MoveCursorToCenterOfActiveWindow.xcodeproj">
72+
</BuildableReference>
73+
</BuildableProductRunnable>
74+
<CommandLineArguments>
75+
<CommandLineArgument
76+
argument = "--debug"
77+
isEnabled = "YES">
78+
</CommandLineArgument>
79+
</CommandLineArguments>
80+
</LaunchAction>
81+
<ProfileAction
82+
buildConfiguration = "Release"
83+
shouldUseLaunchSchemeArgsEnv = "YES"
84+
savedToolIdentifier = ""
85+
useCustomWorkingDirectory = "NO"
86+
debugDocumentVersioning = "YES">
87+
<BuildableProductRunnable
88+
runnableDebuggingMode = "0">
89+
<BuildableReference
90+
BuildableIdentifier = "primary"
91+
BlueprintIdentifier = "15427B3B255138F100291848"
92+
BuildableName = "MoveCursorToCenterOfActiveWindow.app"
93+
BlueprintName = "MoveCursorToCenterOfActiveWindow"
94+
ReferencedContainer = "container:MoveCursorToCenterOfActiveWindow.xcodeproj">
95+
</BuildableReference>
96+
</BuildableProductRunnable>
97+
</ProfileAction>
98+
<AnalyzeAction
99+
buildConfiguration = "Debug">
100+
</AnalyzeAction>
101+
<ArchiveAction
102+
buildConfiguration = "Release"
103+
revealArchiveInOrganizer = "YES">
104+
</ArchiveAction>
105+
</Scheme>

MoveCursorToCenterOfActiveWindow/AppDelegate.swift

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,62 @@
55

66
import Cocoa
77
import CoreGraphics
8+
import OSLog
89

9-
// @NSApplicationMain
1010
class AppDelegate: NSObject, NSApplicationDelegate {
1111

12-
var window: NSWindow!
13-
1412
func applicationDidFinishLaunching(_ aNotification: Notification) {
15-
if
16-
let windowList = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [NSDictionary],
17-
let activeWindowInfo = windowList.first(where: { $0[kCGWindowLayer] as? Int == 0
18-
}),
19-
let activeWindowBounds = activeWindowInfo[kCGWindowBounds] as? NSDictionary,
20-
let rect = CGRect(dictionaryRepresentation: activeWindowBounds)
21-
{
22-
// debugPrint(rect)
23-
let centerOfActiveWindow = CGPoint(x: rect.midX, y: rect.midY)
24-
25-
let error = CGWarpMouseCursorPosition(centerOfActiveWindow)
26-
if error == CGError.success {
27-
exit(EX_OK)
28-
} else {
29-
debugPrint(error)
30-
exit(EX_UNAVAILABLE)
31-
}
13+
14+
//
15+
// Usage: /path/to/MoveCursorToCenterOfActiveWindow.app [--debug]
16+
//
17+
let debugMode = CommandLine.arguments.contains("--debug")
18+
19+
let bundleID = Bundle.main.bundleIdentifier ?? ""
20+
let category = OSLog.Category.pointsOfInterest.rawValue
21+
let logger = Logger(subsystem: bundleID, category: category)
22+
23+
// We don't use Logger log levels below the default one
24+
// (because logs having those levels don't persist after the app exits, and this app exits rather quickly)
25+
26+
if debugMode {
27+
logger.notice("[DEBUG] Screens have separate spaces: \(NSScreen.screensHaveSeparateSpaces, privacy: .public)")
28+
logger.notice("[DEBUG] Screens: \(NSScreen.screens, privacy: .public)")
29+
}
30+
31+
guard let windowList = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [NSDictionary] else {
32+
debugMode ? logger.error("[DEBUG] Cannot retrieve the window list") : logger.error("Cannot retrieve the window list")
33+
exit(EX_UNAVAILABLE)
34+
}
35+
36+
guard let activeWindowInfo = windowList.first(where: { $0[kCGWindowLayer] as? Int == 0 }) else {
37+
debugMode ? logger.notice("[DEBUG] No active window found") : logger.notice("No active window found")
38+
exit(EX_NOINPUT)
39+
}
40+
41+
guard let activeWindowBounds = activeWindowInfo[kCGWindowBounds] as? NSDictionary,
42+
let rect = CGRect(dictionaryRepresentation: activeWindowBounds) else {
43+
if debugMode {
44+
logger.error("[DEBUG] Cannot retrieve the bounds of the active window: \(activeWindowInfo, privacy: .public)")
45+
} else { logger.error("Cannot retrieve the bounds of the active window") }
46+
exit(EX_UNAVAILABLE)
47+
}
48+
49+
let centerOfActiveWindow = CGPoint(x: rect.midX, y: rect.midY)
50+
51+
if debugMode {
52+
logger.notice("[DEBUG] Active window: \(activeWindowInfo, privacy: .public)")
53+
logger.notice("[DEBUG] Attempting to move the cursor to \(centerOfActiveWindow.debugDescription, privacy: .public)")
54+
}
55+
let cgError = CGWarpMouseCursorPosition(centerOfActiveWindow)
56+
57+
if cgError == CGError.success {
58+
if debugMode { logger.notice("[DEBUG] Successfully moved the cursor") }
59+
exit(EX_OK)
3260
} else {
61+
if debugMode {
62+
logger.error("[DEBUG] Unable to move the cursor: \(String(describing: cgError), privacy: .public)")
63+
} else { logger.error("Unable to move the cursor") }
3364
exit(EX_UNAVAILABLE)
3465
}
3566
}
@@ -38,4 +69,3 @@ class AppDelegate: NSObject, NSApplicationDelegate {
3869
// Insert code here to tear down your application
3970
}
4071
}
41-

0 commit comments

Comments
 (0)