11#import < cstddef>
2+ #import < cstring>
23#import < cstdlib>
34#import < dlfcn.h>
45#import < spawn.h>
@@ -42,19 +43,6 @@ - (void)__completeAndRunAsPlugin;
4243- (void )_run ;
4344@end
4445
45- @interface FBSUIApplicationWorkspace : NSObject
46- - (instancetype )initWithSerialQueue : (id )arg1 ;
47- - (void )setDelegate : (id )arg1 ;
48- @end
49-
50- @interface MyFBSUIApplicationWorkspaceDelegate : NSObject
51- @end
52-
53- @implementation MyFBSUIApplicationWorkspaceDelegate
54- - (void )workspaceShouldExit : (FBSUIApplicationWorkspace *)arg1 {}
55- - (void )workspace : (FBSUIApplicationWorkspace *)arg1 didLaunchWithCompletion : (void (^)(id ))arg2 {}
56- @end
57-
5846
5947#pragma mark -
6048
@@ -68,41 +56,91 @@ static __used void _HUDEventCallback(void *target, void *refcon, IOHIDServiceRef
6856}
6957
7058
59+ #pragma mark -
60+
61+ static NSString *_cachesDirectoryPath = nil ;
62+ static NSString *_hudPIDFilePath = nil ;
63+ static NSString *GetPIDFilePath (void )
64+ {
65+ static dispatch_once_t onceToken;
66+ dispatch_once (&onceToken, ^{
67+ _cachesDirectoryPath =
68+ [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES ) firstObject ];
69+ _hudPIDFilePath = [_cachesDirectoryPath stringByAppendingPathComponent: @" hud.pid" ];
70+ });
71+ return _hudPIDFilePath;
72+ }
73+
74+
7175#pragma mark -
7276
7377int main (int argc, char *argv[])
7478{
7579 @autoreleasepool
7680 {
77- os_log_debug (OS_LOG_DEFAULT, " Launched with bundle identifier %{public}@, uid = %{public}d " , NSBundle . mainBundle . bundleIdentifier , getuid () );
81+ os_log_debug (OS_LOG_DEFAULT, " launched argc %{public}d, argv[1] %{public}s " , argc, argc > 1 ? argv[ 1 ] : " NULL " );
7882
79- if (argc < 2 || strcmp (argv[ 1 ], " -hud " ) != 0 )
83+ if (argc <= 1 )
8084 return UIApplicationMain (argc, argv, @" MainApplication" , @" MainApplicationDelegate" );
8185
82- [UIScreen initialize ];
83- CFRunLoopGetCurrent ();
84-
85- GSInitialize ();
86- BKSDisplayServicesStart ();
87- UIApplicationInitialize ();
88-
89- [NSRunLoop currentRunLoop ];
90- BKSHIDEventRegisterEventCallback (_HUDEventCallback);
91-
92- GSEventInitialize (0 );
93- GSEventPushRunLoopMode (kCFRunLoopDefaultMode );
94-
95- UIApplicationInstantiateSingleton (objc_getClass (" HUDMainApplication" ));
96- static id <UIApplicationDelegate> appDelegate = [[objc_getClass (" HUDMainApplicationDelegate" ) alloc ] init ];
97- [UIApplication.sharedApplication setDelegate: appDelegate];
98- [UIApplication.sharedApplication _accessibilityInit ];
99- [UIApplication.sharedApplication __completeAndRunAsPlugin ];
100-
101- static FBSUIApplicationWorkspace *workspace =
102- [[objc_getClass (" FBSUIApplicationWorkspace" ) alloc ] initWithSerialQueue: [UIApplication.sharedApplication _systemAnimationFenceExemptQueue ]];
103- [workspace setDelegate: [MyFBSUIApplicationWorkspaceDelegate new ]];
104-
105- CFRunLoopRun ();
106- return EXIT_SUCCESS;
86+ if (strcmp (argv[1 ], " -hud" ) == 0 )
87+ {
88+ NSString *pidString = [NSString stringWithFormat: @" %d " , getpid ()];
89+ [pidString writeToFile: GetPIDFilePath ()
90+ atomically: YES
91+ encoding: NSUTF8StringEncoding
92+ error: nil ];
93+
94+ [UIScreen initialize ];
95+ CFRunLoopGetCurrent ();
96+
97+ GSInitialize ();
98+ BKSDisplayServicesStart ();
99+ UIApplicationInitialize ();
100+
101+ [NSRunLoop currentRunLoop ];
102+ BKSHIDEventRegisterEventCallback (_HUDEventCallback);
103+
104+ GSEventInitialize (0 );
105+ GSEventPushRunLoopMode (kCFRunLoopDefaultMode );
106+
107+ UIApplicationInstantiateSingleton (objc_getClass (" HUDMainApplication" ));
108+ static id <UIApplicationDelegate> appDelegate = [[objc_getClass (" HUDMainApplicationDelegate" ) alloc ] init ];
109+ [UIApplication.sharedApplication setDelegate: appDelegate];
110+ [UIApplication.sharedApplication _accessibilityInit ];
111+ [UIApplication.sharedApplication __completeAndRunAsPlugin ];
112+
113+ CFRunLoopRun ();
114+ return EXIT_SUCCESS;
115+ }
116+ else if (strcmp (argv[1 ], " -exit" ) == 0 )
117+ {
118+ NSString *pidString = [NSString stringWithContentsOfFile: GetPIDFilePath ()
119+ encoding: NSUTF8StringEncoding
120+ error: nil ];
121+
122+ if (pidString)
123+ {
124+ pid_t pid = (pid_t )[pidString intValue ];
125+ kill (pid, SIGKILL);
126+ unlink (GetPIDFilePath ().UTF8String );
127+ }
128+
129+ return EXIT_SUCCESS;
130+ }
131+ else if (strcmp (argv[1 ], " -check" ) == 0 )
132+ {
133+ NSString *pidString = [NSString stringWithContentsOfFile: GetPIDFilePath ()
134+ encoding: NSUTF8StringEncoding
135+ error: nil ];
136+
137+ if (pidString)
138+ {
139+ pid_t pid = (pid_t )[pidString intValue ];
140+ int killed = kill (pid, 0 );
141+ return (killed == 0 ? EXIT_FAILURE : EXIT_SUCCESS);
142+ }
143+ else return EXIT_SUCCESS; // No PID file, so HUD is not running
144+ }
107145 }
108146}
0 commit comments