Added Rect.xs
[sdlgit/SDL_perl.git] / MacOSX / launcher.m
1 // lancher.m
2 //
3 // Copyright (C) 2006 David J. Goehrig <dgoehrig@cpan.org>
4 // 
5 //
6
7
8 // Define _SDL_main_h to avoid using SDLMain.m
9
10 #define _SDL_main_h
11 #include <Foundation/Foundation.h>
12 #include <AppKit/AppKit.h>
13 #include <EXTERN.h>
14 #include <perl.h>
15 #include <SDL/SDL.h>
16 #include "launcher.h"
17
18 static SDLPerlMain* sdlplmain;
19 static PerlInterpreter *my_perl = NULL; 
20 char path[MAXPATHLEN];
21 char libpath[MAXPATHLEN];
22 char scriptfile[MAXPATHLEN];
23 BOOL init_path;
24
25 void xs_init (pTHX);
26 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
27 EXTERN_C void
28 xs_init(pTHX)
29 {
30         char *file = __FILE__;
31         
32         sprintf(libpath,"%s/Contents/Resources/lib/%s",path,ARCHNAME);
33         fprintf(stderr,"LIBPATH: %s\n",libpath);
34
35         dXSUB_SYS;
36
37         /* DynaLoader is a special case */
38         newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
39         SV* svpth = newSVpv(libpath,strlen(libpath));
40         AV* inc = get_av("INC",0);
41         if (inc && svpth) {
42                 av_unshift(inc,1);
43                 av_store(inc,0,svpth);
44         }
45 }
46
47
48 @implementation SDLPerlApplication
49 - (void) terminate:(id)sender
50 {
51         fprintf(stderr,"Quitting Event\n");
52         SDL_Event event;
53         event.type = SDL_QUIT;
54         SDL_PushEvent(&event);
55         [NSApp stop: nil];
56 }
57 @end
58
59 @implementation SDLPerlMain
60 - (void) setupWorkingDirectory:(BOOL)changep
61 {
62         CFURLRef bpath;
63         fprintf(stderr,"Setup working directory ? %s", (changep ? "True" : "False"));
64         if (! changep) return;
65         bpath = CFBundleCopyBundleURL(CFBundleGetMainBundle());
66         if (CFURLGetFileSystemRepresentation(bpath,true,(UInt8*)path,MAXPATHLEN)) {
67                 fprintf(stderr,"PATH: %s\n",path);
68                 chdir((char*)path);
69         }
70 }
71
72 - (void) applicationWillFinishLaunching: (NSNotification *) note
73 {
74         NSMenu* appleMenu;
75         NSMenuItem* item;
76         NSDictionary* dict;
77         NSString* appName;
78         NSString* title;
79         
80         fprintf(stderr, "Application will finish launching\n");
81
82         dict = (NSDictionary*)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
83         appName = (dict ? [dict objectForKey: @"CFBundleName"] : [[NSProcessInfo processInfo] processName]);
84
85         appleMenu = [[NSMenu alloc] initWithTitle: @""];
86         
87         title = [@"About " stringByAppendingString: appName ];
88         [appleMenu addItemWithTitle: title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
89
90         [appleMenu addItem: [NSMenuItem separatorItem]];
91         
92         title = [@"Hide " stringByAppendingString: appName ];
93         [appleMenu addItemWithTitle: title action:@selector(hide:) keyEquivalent:@"h"];
94
95         title = @"Hide Others";
96         [appleMenu addItemWithTitle: title action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
97
98         title = @"Show All";
99         [appleMenu addItemWithTitle: title action:@selector(unhideAllApplications:) keyEquivalent:@""];
100
101         [appleMenu addItem: [NSMenuItem separatorItem]];
102         
103         title = @"Quit";
104         [appleMenu addItemWithTitle: title action:@selector(terminate:) keyEquivalent:@"q"];
105
106         item = [[NSMenuItem alloc] initWithTitle:@"" action: nil keyEquivalent:@""];
107         [item setSubmenu: appleMenu];
108
109         [[NSApp mainMenu] addItem: item];
110         
111         [NSApp setMainMenu: appleMenu];
112         
113         fprintf(stderr,"Done with menu\n");
114 }
115
116 - (void) applicationDidFinishLaunching: (NSNotification *) note
117 {
118         fprintf(stderr, "Application did  finish launching\n");
119
120 //      fprintf(stderr, "SCRIPT: %s\n",scriptfile);
121 //      NSString* scr = [[NSString alloc] initWithUTF8String: scriptfile];
122 //      fprintf(stderr, "Setting directory: %s\n",init_path ? "true" : "false");
123 //      [self setupWorkingDirectory: init_path];
124 //      fprintf(stderr,"Launching perl script %s\n", scriptfile);
125 //      [self launchPerl: scr ];
126         [NSApp terminate: self];        
127 }
128
129 - (void) launchPerl: (NSString*) script
130 {
131         int count = 3;
132         char* embedding[] = { path, scriptfile , "0" } ;
133         unsigned buflen = [ script lengthOfBytesUsingEncoding: NSUTF8StringEncoding] + 1;
134         [script getCString:scriptfile maxLength: buflen encoding:NSUTF8StringEncoding];
135         fprintf(stderr,"Launching script: %s\n",scriptfile);
136         PERL_SYS_INIT3(&count,&embedding,NULL);
137         my_perl = perl_alloc();
138         perl_construct(my_perl);
139         perl_parse(my_perl,xs_init,count,embedding,NULL);
140         fprintf(stderr,"Running perl\n");
141         perl_run(my_perl);
142         fprintf(stderr,"Destructing  perl\n");
143         perl_destruct(my_perl);
144         fprintf(stderr,"Freeing perl\n");
145         perl_free(my_perl);
146         fprintf(stderr,"Quiting perl script: %s\n",scriptfile);
147         PERL_SYS_TERM();
148 }
149
150 - (BOOL) application: (NSApplication*) theApplication openFile: (NSString*) filename
151 {
152         fprintf(stderr,"openFile %s\n", [filename UTF8String]);
153         fprintf(stderr, "Setting directory: %s\n",init_path ? "true" : "false");
154         [self setupWorkingDirectory: init_path];
155         fprintf(stderr,"launchgin perl\n");
156         [self launchPerl: filename];
157 }
158
159 @end
160
161 int
162 main( int argc, char** argv)
163 {
164         int i;
165         NSAutoreleasePool* pool;
166
167         fprintf(stderr, "ARGC %d \n", argc);
168         for (i = 0; i < argc; ++i) {
169                 fprintf(stderr,"ARGV[%d] %s\n",i,argv[i]);
170         }
171
172         init_path = YES;
173         memset(scriptfile,0,MAXPATHLEN);
174         if (argc >= 2) {
175                 if ( argc == 2 ) {
176                         strncpy(scriptfile,argv[1],strlen(argv[1]));
177                 } else {
178                         strncpy(scriptfile,argv[1],strlen(argv[2]));
179                 }
180         }
181         fprintf(stderr, "[main] SCRIPT: %s\n",scriptfile);
182
183         pool = [[NSAutoreleasePool alloc] init];
184
185         [SDLPerlApplication sharedApplication];
186         [NSApp setMainMenu: [[NSMenu alloc] init]];
187         
188         sdlplmain = [[SDLPerlMain alloc] init];
189         [sdlplmain retain];
190         [NSApp setDelegate: sdlplmain];
191
192         [NSApp run];
193         
194         fprintf(stderr,"Terminating NSApp\n");
195         [sdlplmain release];
196         [pool release];
197
198         return 0;
199 }