3 // Copyright (C) 2006 David J. Goehrig <dgoehrig@cpan.org>
8 // Define _SDL_main_h to avoid using SDLMain.m
11 #include <Foundation/Foundation.h>
12 #include <AppKit/AppKit.h>
18 static SDLPerlMain* sdlplmain;
19 static PerlInterpreter *my_perl = NULL;
20 char path[MAXPATHLEN];
21 char libpath[MAXPATHLEN];
22 char scriptfile[MAXPATHLEN];
26 EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
30 char *file = __FILE__;
32 sprintf(libpath,"%s/Contents/Resources/lib/%s",path,ARCHNAME);
33 fprintf(stderr,"LIBPATH: %s\n",libpath);
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);
43 av_store(inc,0,svpth);
48 @implementation SDLPerlApplication
49 - (void) terminate:(id)sender
51 fprintf(stderr,"Quitting Event\n");
53 event.type = SDL_QUIT;
54 SDL_PushEvent(&event);
59 @implementation SDLPerlMain
60 - (void) setupWorkingDirectory:(BOOL)changep
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);
72 - (void) applicationWillFinishLaunching: (NSNotification *) note
80 fprintf(stderr, "Application will finish launching\n");
82 dict = (NSDictionary*)CFBundleGetInfoDictionary(CFBundleGetMainBundle());
83 appName = (dict ? [dict objectForKey: @"CFBundleName"] : [[NSProcessInfo processInfo] processName]);
85 appleMenu = [[NSMenu alloc] initWithTitle: @""];
87 title = [@"About " stringByAppendingString: appName ];
88 [appleMenu addItemWithTitle: title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
90 [appleMenu addItem: [NSMenuItem separatorItem]];
92 title = [@"Hide " stringByAppendingString: appName ];
93 [appleMenu addItemWithTitle: title action:@selector(hide:) keyEquivalent:@"h"];
95 title = @"Hide Others";
96 [appleMenu addItemWithTitle: title action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
99 [appleMenu addItemWithTitle: title action:@selector(unhideAllApplications:) keyEquivalent:@""];
101 [appleMenu addItem: [NSMenuItem separatorItem]];
104 [appleMenu addItemWithTitle: title action:@selector(terminate:) keyEquivalent:@"q"];
106 item = [[NSMenuItem alloc] initWithTitle:@"" action: nil keyEquivalent:@""];
107 [item setSubmenu: appleMenu];
109 [[NSApp mainMenu] addItem: item];
111 [NSApp setMainMenu: appleMenu];
113 fprintf(stderr,"Done with menu\n");
116 - (void) applicationDidFinishLaunching: (NSNotification *) note
118 fprintf(stderr, "Application did finish launching\n");
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];
129 - (void) launchPerl: (NSString*) script
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");
142 fprintf(stderr,"Destructing perl\n");
143 perl_destruct(my_perl);
144 fprintf(stderr,"Freeing perl\n");
146 fprintf(stderr,"Quiting perl script: %s\n",scriptfile);
150 - (BOOL) application: (NSApplication*) theApplication openFile: (NSString*) filename
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];
162 main( int argc, char** argv)
165 NSAutoreleasePool* pool;
167 fprintf(stderr, "ARGC %d \n", argc);
168 for (i = 0; i < argc; ++i) {
169 fprintf(stderr,"ARGV[%d] %s\n",i,argv[i]);
173 memset(scriptfile,0,MAXPATHLEN);
176 strncpy(scriptfile,argv[1],strlen(argv[1]));
178 strncpy(scriptfile,argv[1],strlen(argv[2]));
181 fprintf(stderr, "[main] SCRIPT: %s\n",scriptfile);
183 pool = [[NSAutoreleasePool alloc] init];
185 [SDLPerlApplication sharedApplication];
186 [NSApp setMainMenu: [[NSMenu alloc] init]];
188 sdlplmain = [[SDLPerlMain alloc] init];
190 [NSApp setDelegate: sdlplmain];
194 fprintf(stderr,"Terminating NSApp\n");