lib: intro the iOS library
[nit.git] / lib / ios / app.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Basic structure for Nit apps on iOS
16 module app
17
18 import platform
19 import ::app
20
21 in "ObjC Header" `{
22 #import <UIKit/UIKit.h>
23
24 // Our interface to the iOS system
25 @interface AppDelegate: UIResponder <UIApplicationDelegate>
26
27 // The main window
28 @property (strong, nonatomic) UIWindow *window;
29 @end
30 `}
31
32 in "ObjC" `{
33
34 // Global reference to the App from app.nit
35 App app_nit_ios_app;
36
37 // Our own C argc and argv
38 int app_nit_ios_argc;
39 char **app_nit_ios_argv;
40
41 @implementation AppDelegate
42
43 - (BOOL)application:(UIApplication *)application
44 willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
45
46 // Set aside `application` to be used from Nit
47 App_ui_application__assign(app_nit_ios_app, application);
48 App_app_delegate__assign(app_nit_ios_app, self);
49
50 // Complete the callback
51 return App_will_finish_launching_with_options(app_nit_ios_app);
52 }
53
54 - (BOOL)application:(UIApplication *)application
55 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
56
57 return App_did_finish_launching_with_options(app_nit_ios_app);
58 }
59
60 - (void)applicationWillResignActive:(UIApplication *)application {
61 App_will_resign_active(app_nit_ios_app);
62 }
63
64 - (void)applicationDidEnterBackground:(UIApplication *)application {
65 App_did_enter_background(app_nit_ios_app);
66 }
67
68 - (void)applicationWillEnterForeground:(UIApplication *)application {
69 App_will_enter_foreground(app_nit_ios_app);
70 }
71
72 - (void)applicationDidBecomeActive:(UIApplication *)application {
73 App_did_become_active(app_nit_ios_app);
74 }
75
76 - (void)applicationWillTerminate:(UIApplication *)application {
77 App_will_terminate(app_nit_ios_app);
78 }
79
80 @end
81 `}
82
83 # Application interface to the iOS system
84 extern class AppDelegate in "ObjC" `{ AppDelegate * `}
85 end
86
87 # Graphical application to which events are sent
88 extern class UIApplication in "ObjC" `{ UIApplication * `}
89 end
90
91 redef class App
92
93 # Main graphical application
94 var ui_application: UIApplication
95
96 # Application interface to the iOS system
97 var app_delegate: AppDelegate
98
99 # Copy back to C the command line arguments
100 #
101 # Nit extracts the first arguments from the `args` sequence,
102 # so we need to add it back. That's why Nit's `args` is smaller than in C.
103 private fun register_args(program_name: NativeString, argc: Int,
104 argv: Sequence[String]) import Sequence[String].[], String.to_cstring in "ObjC" `{
105 app_nit_ios_argc = argc+1;
106
107 // TODO copy or pin the strings when needed
108 app_nit_ios_argv = malloc(argc * sizeof(char*));
109 app_nit_ios_argv[0] = program_name;
110 for (int i = 0; i < argc; i ++) {
111 String arg = Sequence_of_String__index(argv, i);
112 app_nit_ios_argv[i+1] = String_to_cstring(arg);
113 }
114 `}
115
116 # Register `self` globally in C so it can be retrieved from iOS callbacks
117 private fun register_globally in "ObjC" `{
118 App_incr_ref(recv);
119 app_nit_ios_app = recv;
120 `}
121
122 # Entry point to the iOS framework
123 private fun ui_application_main: Bool import did_finish_launching_with_options,
124 will_finish_launching_with_options,
125 will_resign_active, did_enter_background, will_enter_foreground,
126 did_become_active, will_terminate, ui_application=, app_delegate= in "ObjC" `{
127
128 @autoreleasepool {
129 return UIApplicationMain(app_nit_ios_argc, app_nit_ios_argv,
130 nil, NSStringFromClass([AppDelegate class]));
131 }
132 `}
133
134 # The application is about to launch
135 #
136 # Redef this method to set the very first custom code to be executed.
137 fun will_finish_launching_with_options: Bool do return true
138
139 # The application just launched but is not yet displayed to the user
140 #
141 # Redef this method to customize the behavior.
142 fun did_finish_launching_with_options: Bool do return true
143
144 # The application is about to move from active to inactive state
145 #
146 # This can occur for certain types of temporary interruptions
147 # (such as an incoming phone call or SMS message) or when the
148 # user quits the application and it begins the transition to
149 # the background state.
150 #
151 # Redef this method to pause ongoing tasks, disable timers, and
152 # throttle down OpenGL ES frame rates. Games should use this
153 # method to pause.
154 fun will_resign_active do end
155
156 # The application just left foreground it can be suspended at any time
157 #
158 # Redef this method to release shared resources, save user data,
159 # invalidate timers, and store application state to restore your
160 # application to its current state in case it is terminated later.
161 #
162 # If your application supports background execution, this method
163 # is called instead of `will_terminate` when the user quits.
164 fun did_enter_background do end
165
166 # The application will enter the foreground
167 #
168 # Called as part of the transition from the background to the
169 # inactive state.
170 #
171 # Redef to und changes made on entering the background.
172 fun will_enter_foreground do end
173
174 # The application just became active
175 #
176 # Redef to restart any tasks that were paused (or not yet started) while
177 # the application was inactive. If the application was previously
178 # in the background, optionally refresh the user interface.
179 fun did_become_active do end
180
181 # The application is about to terminate (not suspended)
182 #
183 # Redef to save data if appropriate.
184 fun will_terminate do end
185 end
186
187 app.register_args(program_name.to_cstring, args.length, args)
188 app.register_globally
189
190 var ret = app.ui_application_main
191 exit if ret then 0 else 1