ObjFW
Loading...
Searching...
No Matches
OFApplication.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2025 Jonathan Schleifer <js@nil.im>
3 *
4 * All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version 3.0 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * version 3.0 for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * version 3.0 along with this program. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#include <signal.h>
21
22#import "OFObject.h"
23#import "OFNotification.h"
24
25#ifdef OF_WINDOWS
26# include <windows.h>
27#endif
28
29OF_ASSUME_NONNULL_BEGIN
30
33@class OFArray OF_GENERIC(ObjectType);
34@class OFDictionary OF_GENERIC(KeyType, ObjectType);
35@class OFMutableArray OF_GENERIC(ObjectType);
36@class OFMutableDictionary OF_GENERIC(KeyType, ObjectType);
37@class OFSandbox;
38@class OFString;
39
40#ifdef __cplusplus
41extern "C" {
42#endif
48
53#ifdef __cplusplus
54}
55#endif
56
81#ifndef OF_WINDOWS
82# define OF_APPLICATION_DELEGATE(class_) \
83 int \
84 main(int argc, char *argv[]) \
85 { \
86 return OFApplicationMain(&argc, &argv, \
87 (class_ *)[[class_ alloc] init]); \
88 }
89#else
90# define OF_APPLICATION_DELEGATE(class_) \
91 int \
92 main(int argc, char *argv[]) \
93 { \
94 return OFApplicationMain(&argc, &argv, \
95 (class_ *)[[class_ alloc] init]); \
96 } \
97 \
98 WINAPI int \
99 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, \
100 LPSTR lpCmdLine, int nShowCmd) \
101 { \
102 int argc = 0, si = 0; \
103 char **argv = NULL, **envp = NULL; \
104 \
105 __getmainargs(&argc, &argv, &envp, _CRT_glob, &si); \
106 \
107 return OFApplicationMain(&argc, &argv, \
108 (class_ *)[[class_ alloc] init]); \
109 }
110# ifdef __cplusplus
111extern "C" {
112# endif
113extern void __getmainargs(int *_Nonnull, char *_Nonnull *_Nullable *_Nullable,
114 char *_Nonnull *_Nullable *_Nullable, int, int *_Nonnull);
115extern int _CRT_glob;
116# ifdef __cplusplus
117}
118# endif
119#endif
120
121#ifdef OF_HAVE_PLEDGE
122# define OF_HAVE_SANDBOX
123#endif
124
132@protocol OFApplicationDelegate <OFObject>
140- (void)applicationDidFinishLaunching: (OFNotification *)notification;
141
142@optional
149- (void)applicationWillTerminate: (OFNotification *)notification;
150
159- (void)applicationDidReceiveSIGINT;
160
161#ifdef SIGHUP
172- (void)applicationDidReceiveSIGHUP;
173#endif
174
175#ifdef SIGUSR1
186- (void)applicationDidReceiveSIGUSR1;
187#endif
188
189#ifdef SIGUSR2
200- (void)applicationDidReceiveSIGUSR2;
201#endif
202@end
203
217OF_SUBCLASSING_RESTRICTED
219{
220 OFString *_programName;
221 OFArray OF_GENERIC(OFString *) *_arguments;
222 OFMutableDictionary OF_GENERIC(OFString *, OFString *) *_environment;
223 int *_argc;
224 char ***_argv;
225 id <OFApplicationDelegate> _Nullable _delegate;
226 void (*_Nullable _SIGINTHandler)(id, SEL);
227#ifndef OF_WINDOWS
228 void (*_Nullable _SIGHUPHandler)(id, SEL);
229 void (*_Nullable _SIGUSR1Handler)(id, SEL);
230 void (*_Nullable _SIGUSR2Handler)(id, SEL);
231#endif
232#ifdef OF_HAVE_SANDBOX
233 OFSandbox *_Nullable _activeSandbox;
234 OFSandbox *_Nullable _activeSandboxForChildProcesses;
235#endif
236}
237
238#ifdef OF_HAVE_CLASS_PROPERTIES
239@property (class, readonly, nullable, nonatomic)
240 OFApplication *sharedApplication;
241@property (class, readonly, nullable, nonatomic) OFString *programName;
242@property (class, readonly, nullable, nonatomic)
243 OFArray OF_GENERIC(OFString *) *arguments;
244@property (class, readonly, nullable, nonatomic)
245 OFDictionary OF_GENERIC(OFString *, OFString *) *environment;
246#endif
247
251@property (readonly, nonatomic) OFString *programName;
252
256@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *arguments;
257
261@property (readonly, nonatomic)
263
267@property OF_NULLABLE_PROPERTY (assign, nonatomic)
268 id <OFApplicationDelegate> delegate;
269
270#ifdef OF_HAVE_SANDBOX
271@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFSandbox *activeSandbox;
272@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
273 OFSandbox *activeSandboxForChildProcesses;
274#endif
275
281+ (nullable OFApplication *)sharedApplication;
282
288+ (nullable OFString *)programName;
289
295+ (nullable OFArray OF_GENERIC(OFString *) *)arguments;
296
302+ (nullable OFDictionary OF_GENERIC(OFString *, OFString *) *)environment;
303
307+ (void)terminate OF_NO_RETURN;
308
314+ (void)terminateWithStatus: (int)status OF_NO_RETURN;
315
316#ifdef OF_HAVE_SANDBOX
317+ (void)of_activateSandbox: (OFSandbox *)sandbox;
318+ (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
319#endif
320
321- (instancetype)init OF_UNAVAILABLE;
322
329- (void)getArgumentCount: (int *_Nonnull *_Nonnull)argc
330 andArgumentValues: (char *_Nullable *_Nonnull *_Nonnull[_Nonnull])argv;
331
335- (void)terminate OF_NO_RETURN;
336
342- (void)terminateWithStatus: (int)status OF_NO_RETURN;
343
344#ifdef OF_HAVE_SANDBOX
345- (void)of_activateSandbox: (OFSandbox *)sandbox;
346- (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
347#endif
348@end
349
350#ifdef __cplusplus
351extern "C" {
352#endif
353extern int OFApplicationMain(int *_Nonnull, char *_Nullable *_Nonnull[_Nonnull],
354 id <OFApplicationDelegate>);
355#ifdef __cplusplus
356}
357#endif
358
359OF_ASSUME_NONNULL_END
const OFNotificationName OFApplicationWillTerminateNotification
A notification that will be sent when the application will terminate.
Definition OFApplication.m:98
const OFNotificationName OFApplicationDidFinishLaunchingNotification
A notification that will be sent when the application did finish launching.
Definition OFApplication.m:96
const struct objc_selector * SEL
A selector.
Definition ObjFWRT.h:102
A class which represents the application as an object.
Definition OFApplication.h:219
OFDictionary * environment
The environment of the application.
Definition OFApplication.h:262
OFArray * arguments
The arguments passed to the application.
Definition OFApplication.h:256
An abstract class for storing objects in an array.
Definition OFArray.h:109
A class for storing constant strings using the @"" literal.
Definition OFConstantString.h:42
An abstract class for storing objects in a dictionary.
Definition OFDictionary.h:84
An abstract class for storing, adding and removing objects in an array.
Definition OFMutableArray.h:48
An abstract class for storing and changing objects in a dictionary.
Definition OFMutableDictionary.h:48
A class to represent a notification for or from OFNotificationCenter.
Definition OFNotification.h:42
The root class for all other classes inside ObjFW.
Definition OFObject.h:692
A class for handling strings.
Definition OFString.h:143