ObjFW
 
Loading...
Searching...
No Matches
OFApplication.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2026 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
32
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
151#ifndef OF_AMIGAOS
163
164# ifdef SIGHUP
178# endif
179
180# ifdef SIGUSR1
194# endif
195
196# ifdef SIGUSR2
210# endif
211#endif
212@end
213
227OF_SUBCLASSING_RESTRICTED
229{
230 OFString *_programName;
231 OFArray OF_GENERIC(OFString *) *_arguments;
232 OFMutableDictionary OF_GENERIC(OFString *, OFString *) *_environment;
233 long _processID;
234 int *_argc;
235 char ***_argv;
236 id <OFApplicationDelegate> _Nullable _delegate;
237 void (*_Nullable _SIGINTHandler)(id, SEL);
238#ifndef OF_WINDOWS
239 void (*_Nullable _SIGHUPHandler)(id, SEL);
240 void (*_Nullable _SIGUSR1Handler)(id, SEL);
241 void (*_Nullable _SIGUSR2Handler)(id, SEL);
242#endif
243#ifdef OF_HAVE_SANDBOX
244 OFSandbox *_Nullable _activeSandbox;
245 OFSandbox *_Nullable _activeSandboxForChildProcesses;
246#endif
247}
248
249#ifdef OF_HAVE_CLASS_PROPERTIES
250@property (class, readonly, nullable, nonatomic)
251 OFApplication *sharedApplication;
252@property (class, readonly, nullable, nonatomic) OFString *programName;
253@property (class, readonly, nullable, nonatomic)
254 OFArray OF_GENERIC(OFString *) *arguments;
255@property (class, readonly, nullable, nonatomic)
256 OFDictionary OF_GENERIC(OFString *, OFString *) *environment;
257@property (class, readonly, nonatomic) long processID;
258#endif
259
263@property (readonly, nonatomic) OFString *programName;
264
268@property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *arguments;
269
273@property (readonly, nonatomic)
274 OFDictionary OF_GENERIC(OFString *, OFString *) *environment;
275
279@property (readonly, nonatomic) long processID;
280
284@property OF_NULLABLE_PROPERTY (assign, nonatomic)
285 id <OFApplicationDelegate> delegate;
286
287#ifdef OF_HAVE_SANDBOX
288@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFSandbox *activeSandbox;
289@property OF_NULLABLE_PROPERTY (readonly, nonatomic)
290 OFSandbox *activeSandboxForChildProcesses;
291#endif
292
298+ (nullable OFApplication *)sharedApplication;
299
305+ (nullable OFString *)programName;
306
312+ (nullable OFArray OF_GENERIC(OFString *) *)arguments;
313
319+ (nullable OFDictionary OF_GENERIC(OFString *, OFString *) *)environment;
320
326+ (long)processID;
327
331+ (void)terminate OF_NO_RETURN;
332
338+ (void)terminateWithStatus: (int)status OF_NO_RETURN;
339
340#ifdef OF_HAVE_SANDBOX
341+ (void)of_activateSandbox: (OFSandbox *)sandbox;
342+ (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
343#endif
344
345- (instancetype)init OF_UNAVAILABLE;
346
353- (void)getArgumentCount: (int *_Nonnull *_Nonnull)argc
354 andArgumentValues: (char *_Nullable *_Nonnull *_Nonnull[_Nonnull])argv;
355
359- (void)terminate OF_NO_RETURN;
360
366- (void)terminateWithStatus: (int)status OF_NO_RETURN;
367
368#ifdef OF_HAVE_SANDBOX
369- (void)of_activateSandbox: (OFSandbox *)sandbox;
370- (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
371#endif
372@end
373
374#ifdef __cplusplus
375extern "C" {
376#endif
377extern int OFApplicationMain(int *_Nonnull, char *_Nullable *_Nonnull[_Nonnull],
378 id <OFApplicationDelegate>);
379#ifdef __cplusplus
380}
381#endif
382
383OF_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
OFConstantString * OFNotificationName
A name for a notification.
Definition OFNotification.h:32
const struct objc_selector * SEL
A selector.
Definition ObjFWRT.h:102
A class which represents the application as an object.
Definition OFApplication.h:229
OFString * programName
The name of the program (argv[0]).
Definition OFApplication.m:191
long processID
The process ID of the application.
Definition OFApplication.m:206
void terminate()
Terminates the application with the EXIT_SUCCESS status.
Definition OFApplication.m:646
OFDictionary * environment
The environment of the application.
Definition OFApplication.m:201
OFArray * arguments
The arguments passed to the application.
Definition OFApplication.m:196
An abstract class for storing objects in an array.
Definition OFArray.h:110
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:50
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:956
instancetype init()
Initializes an already allocated object.
Definition OFObject.m:671
A class for handling strings.
Definition OFString.h:143
void applicationDidReceiveSIGINT()
A method which is called when the application received a SIGINT.
void applicationDidReceiveSIGUSR1()
A method which is called when the application received a SIGUSR1.
void applicationDidReceiveSIGUSR2()
A method which is called when the application received a SIGUSR2.
void applicationDidReceiveSIGHUP()
A method which is called when the application received a SIGHUP.