ObjFW
Loading...
Searching...
No Matches
OFApplication.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2024 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
45
50
75#ifndef OF_WINDOWS
76# define OF_APPLICATION_DELEGATE(class_) \
77 int \
78 main(int argc, char *argv[]) \
79 { \
80 return OFApplicationMain(&argc, &argv, \
81 (class_ *)[[class_ alloc] init]); \
82 }
83#else
84# define OF_APPLICATION_DELEGATE(class_) \
85 int \
86 main(int argc, char *argv[]) \
87 { \
88 return OFApplicationMain(&argc, &argv, \
89 (class_ *)[[class_ alloc] init]); \
90 } \
91 \
92 WINAPI int \
93 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, \
94 LPSTR lpCmdLine, int nShowCmd) \
95 { \
96 int argc = 0, si = 0; \
97 char **argv = NULL, **envp = NULL; \
98 \
99 __getmainargs(&argc, &argv, &envp, _CRT_glob, &si); \
100 \
101 return OFApplicationMain(&argc, &argv, \
102 (class_ *)[[class_ alloc] init]); \
103 }
104# ifdef __cplusplus
105extern "C" {
106# endif
107extern void __getmainargs(int *_Nonnull, char *_Nonnull *_Nullable *_Nullable,
108 char *_Nonnull *_Nullable *_Nullable, int, int *_Nonnull);
109extern int _CRT_glob;
110# ifdef __cplusplus
111}
112# endif
113#endif
114
115#ifdef OF_HAVE_PLEDGE
116# define OF_HAVE_SANDBOX
117#endif
118
126@protocol OFApplicationDelegate <OFObject>
134- (void)applicationDidFinishLaunching: (OFNotification *)notification;
135
136@optional
143- (void)applicationWillTerminate: (OFNotification *)notification;
144
153- (void)applicationDidReceiveSIGINT;
154
155#ifdef SIGHUP
166- (void)applicationDidReceiveSIGHUP;
167#endif
168
169#ifdef SIGUSR1
180- (void)applicationDidReceiveSIGUSR1;
181#endif
182
183#ifdef SIGUSR2
194- (void)applicationDidReceiveSIGUSR2;
195#endif
196@end
197
211OF_SUBCLASSING_RESTRICTED
213{
214 OFString *_programName;
215 OFArray OF_GENERIC(OFString *) *_arguments;
216 OFMutableDictionary OF_GENERIC(OFString *, OFString *) *_environment;
217 int *_argc;
218 char ***_argv;
219 id <OFApplicationDelegate> _Nullable _delegate;
220 void (*_Nullable _SIGINTHandler)(id, SEL);
221#ifndef OF_WINDOWS
222 void (*_Nullable _SIGHUPHandler)(id, SEL);
223 void (*_Nullable _SIGUSR1Handler)(id, SEL);
224 void (*_Nullable _SIGUSR2Handler)(id, SEL);
225#endif
226#ifdef OF_HAVE_SANDBOX
227 OFSandbox *_Nullable _activeSandbox;
228 OFSandbox *_Nullable _activeSandboxForChildProcesses;
229#endif
230}
231
232#ifdef OF_HAVE_CLASS_PROPERTIES
233@property (class, readonly, nullable, nonatomic)
234 OFApplication *sharedApplication;
235@property (class, readonly, nullable, nonatomic) OFString *programName;
236@property (class, readonly, nullable, nonatomic)
237 OFArray OF_GENERIC(OFString *) *arguments;
238@property (class, readonly, nullable, nonatomic)
239 OFDictionary OF_GENERIC(OFString *, OFString *) *environment;
240#endif
241
245@property (readonly, nonatomic) OFString *programName;
246
250@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *arguments;
251
255@property (readonly, nonatomic)
257
261@property OF_NULLABLE_PROPERTY (assign, nonatomic)
262 id <OFApplicationDelegate> delegate;
263
264#ifdef OF_HAVE_SANDBOX
265@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFSandbox *activeSandbox;
266@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
267 OFSandbox *activeSandboxForChildProcesses;
268#endif
269
275+ (nullable OFApplication *)sharedApplication;
276
282+ (nullable OFString *)programName;
283
289+ (nullable OFArray OF_GENERIC(OFString *) *)arguments;
290
296+ (nullable OFDictionary OF_GENERIC(OFString *, OFString *) *)environment;
297
301+ (void)terminate OF_NO_RETURN;
302
308+ (void)terminateWithStatus: (int)status OF_NO_RETURN;
309
310#ifdef OF_HAVE_SANDBOX
311+ (void)of_activateSandbox: (OFSandbox *)sandbox;
312+ (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
313#endif
314
315- (instancetype)init OF_UNAVAILABLE;
316
323- (void)getArgumentCount: (int *_Nonnull *_Nonnull)argc
324 andArgumentValues: (char *_Nullable *_Nonnull *_Nonnull[_Nonnull])argv;
325
329- (void)terminate OF_NO_RETURN;
330
336- (void)terminateWithStatus: (int)status OF_NO_RETURN;
337
338#ifdef OF_HAVE_SANDBOX
339- (void)of_activateSandbox: (OFSandbox *)sandbox;
340- (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
341#endif
342@end
343
344#ifdef __cplusplus
345extern "C" {
346#endif
347extern int OFApplicationMain(int *_Nonnull, char *_Nullable *_Nonnull[_Nonnull],
348 id <OFApplicationDelegate>);
349#ifdef __cplusplus
350}
351#endif
352
353OF_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:213
OFDictionary * environment
The environment of the application.
Definition OFApplication.h:256
OFArray * arguments
The arguments passed to the application.
Definition OFApplication.h:250
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