ObjFW
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 
29 OF_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
41 extern "C" {
42 #endif
43 
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
111 extern "C" {
112 # endif
113 extern void __getmainargs(int *_Nonnull, char *_Nonnull *_Nullable *_Nullable,
114  char *_Nonnull *_Nullable *_Nullable, int, int *_Nonnull);
115 extern 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 
140 - (void)applicationDidFinishLaunching: (OFNotification *)notification;
141 
142 @optional
149 - (void)applicationWillTerminate: (OFNotification *)notification;
150 
151 #ifndef OF_AMIGAOS
152 
162 - (void)applicationDidReceiveSIGINT;
163 
164 # ifdef SIGHUP
165 
177 - (void)applicationDidReceiveSIGHUP;
178 # endif
179 
180 # ifdef SIGUSR1
181 
193 - (void)applicationDidReceiveSIGUSR1;
194 # endif
195 
196 # ifdef SIGUSR2
197 
209 - (void)applicationDidReceiveSIGUSR2;
210 # endif
211 #endif
212 @end
213 
227 OF_SUBCLASSING_RESTRICTED
229 {
230  OFString *_programName;
231  OFArray OF_GENERIC(OFString *) *_arguments;
232  OFMutableDictionary OF_GENERIC(OFString *, OFString *) *_environment;
233  int *_argc;
234  char ***_argv;
235  id <OFApplicationDelegate> _Nullable _delegate;
236  void (*_Nullable _SIGINTHandler)(id, SEL);
237 #ifndef OF_WINDOWS
238  void (*_Nullable _SIGHUPHandler)(id, SEL);
239  void (*_Nullable _SIGUSR1Handler)(id, SEL);
240  void (*_Nullable _SIGUSR2Handler)(id, SEL);
241 #endif
242 #ifdef OF_HAVE_SANDBOX
243  OFSandbox *_Nullable _activeSandbox;
244  OFSandbox *_Nullable _activeSandboxForChildProcesses;
245 #endif
246 }
247 
248 #ifdef OF_HAVE_CLASS_PROPERTIES
249 @property (class, readonly, nullable, nonatomic)
250  OFApplication *sharedApplication;
251 @property (class, readonly, nullable, nonatomic) OFString *programName;
252 @property (class, readonly, nullable, nonatomic)
253  OFArray OF_GENERIC(OFString *) *arguments;
254 @property (class, readonly, nullable, nonatomic)
255  OFDictionary OF_GENERIC(OFString *, OFString *) *environment;
256 #endif
257 
261 @property (readonly, nonatomic) OFString *programName;
262 
266 @property (readonly, nonatomic) OFArray OF_GENERIC(OFString *) *arguments;
267 
271 @property (readonly, nonatomic)
272  OFDictionary OF_GENERIC(OFString *, OFString *) *environment;
273 
277 @property OF_NULLABLE_PROPERTY (assign, nonatomic)
278  id <OFApplicationDelegate> delegate;
279 
280 #ifdef OF_HAVE_SANDBOX
281 @property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFSandbox *activeSandbox;
282 @property OF_NULLABLE_PROPERTY (readonly, nonatomic)
283  OFSandbox *activeSandboxForChildProcesses;
284 #endif
285 
291 + (nullable OFApplication *)sharedApplication;
292 
298 + (nullable OFString *)programName;
299 
305 + (nullable OFArray OF_GENERIC(OFString *) *)arguments;
306 
312 + (nullable OFDictionary OF_GENERIC(OFString *, OFString *) *)environment;
313 
317 + (void)terminate OF_NO_RETURN;
318 
324 + (void)terminateWithStatus: (int)status OF_NO_RETURN;
325 
326 #ifdef OF_HAVE_SANDBOX
327 + (void)of_activateSandbox: (OFSandbox *)sandbox;
328 + (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
329 #endif
330 
331 - (instancetype)init OF_UNAVAILABLE;
332 
339 - (void)getArgumentCount: (int *_Nonnull *_Nonnull)argc
340  andArgumentValues: (char *_Nullable *_Nonnull *_Nonnull[_Nonnull])argv;
341 
345 - (void)terminate OF_NO_RETURN;
346 
352 - (void)terminateWithStatus: (int)status OF_NO_RETURN;
353 
354 #ifdef OF_HAVE_SANDBOX
355 - (void)of_activateSandbox: (OFSandbox *)sandbox;
356 - (void)of_activateSandboxForChildProcesses: (OFSandbox *)sandbox;
357 #endif
358 @end
359 
360 #ifdef __cplusplus
361 extern "C" {
362 #endif
363 extern int OFApplicationMain(int *_Nonnull, char *_Nullable *_Nonnull[_Nonnull],
364  id <OFApplicationDelegate>);
365 #ifdef __cplusplus
366 }
367 #endif
368 
369 OF_ASSUME_NONNULL_END
const struct objc_selector * SEL
A selector.
Definition: ObjFWRT.h:102
An abstract class for storing objects in an array.
Definition: OFArray.h:108
A class to represent a notification for or from OFNotificationCenter.
Definition: OFNotification.h:40
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:41
The root class for all other classes inside ObjFW.
Definition: OFObject.h:920
An abstract class for storing, adding and removing objects in an array.
Definition: OFMutableArray.h:49
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
A class for handling strings.
Definition: OFString.h:142
An abstract class for storing objects in a dictionary.
Definition: OFDictionary.h:82
An abstract class for storing and changing objects in a dictionary.
Definition: OFMutableDictionary.h:46
A protocol for delegates of OFApplication.
A class which represents the application as an object.
Definition: OFApplication.h:228