~ubuntu-branches/ubuntu/karmic/e-uae/karmic

« back to all changes in this revision

Viewing changes to src/od-macosx/main.m

  • Committer: Bazaar Package Importer
  • Author(s): Stephan Suerken
  • Date: 2008-07-05 14:02:02 UTC
  • Revision ID: james.westby@ubuntu.com-20080705140202-u5aagnhtg31pmjc3
Tags: upstream-0.8.29-WIP4
ImportĀ upstreamĀ versionĀ 0.8.29-WIP4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * E-UAE - The portable Amiga emulator
 
3
 *
 
4
 * Main entry point for our Cocoa-ized app and support
 
5
 * routines.
 
6
 *
 
7
 * Based on SDLMain.m from libSDL.
 
8
 * Initial Version: Darrell Walisser <dwaliss1@purdue.edu>
 
9
 * Non-NIB-Code & other changes: Max Horn <max@quendi.de>
 
10
 *
 
11
 * Adaption for E-UAE:
 
12
 * Steven J. Saunders
 
13
 * Richard Drummond
 
14
 */
 
15
 
 
16
#import "sysconfig.h"
 
17
#import "sysdeps.h"
 
18
 
 
19
#import "options.h"
 
20
#import "uae.h"
 
21
 
 
22
#ifdef USE_SDL
 
23
# import "SDL.h"
 
24
#endif
 
25
 
 
26
#import "main.h"
 
27
#import <sys/param.h> /* for MAXPATHLEN */
 
28
#import <unistd.h>
 
29
 
 
30
/* Use this flag to determine whether we use CPS (docking) or not */
 
31
#define SDL_USE_CPS     1
 
32
 
 
33
#ifdef SDL_USE_CPS
 
34
/* Portions of CPS.h */
 
35
typedef struct CPSProcessSerNum
 
36
{
 
37
    UInt32      lo;
 
38
    UInt32      hi;
 
39
} CPSProcessSerNum;
 
40
 
 
41
extern OSErr CPSGetCurrentProcess (CPSProcessSerNum *psn);
 
42
extern OSErr CPSEnableForegroundOperation (CPSProcessSerNum *psn, UInt32 _arg2, UInt32 _arg3, UInt32 _arg4, UInt32 _arg5);
 
43
extern OSErr CPSSetFrontProcess (CPSProcessSerNum *psn);
 
44
 
 
45
#endif /* SDL_USE_CPS */
 
46
 
 
47
static int     gArgc;
 
48
static char  **gArgv;
 
49
BOOL           gFinderLaunch;
 
50
 
 
51
NSString *getApplicationName (void)
 
52
{
 
53
    NSDictionary *dict;
 
54
    NSString *appName = 0;
 
55
 
 
56
    /* Determine the application name */
 
57
    dict = (NSDictionary *) CFBundleGetInfoDictionary (CFBundleGetMainBundle ());
 
58
    if (dict)
 
59
        appName = [dict objectForKey: @"CFBundleName"];
 
60
 
 
61
    if (![appName length])
 
62
        appName = [[NSProcessInfo processInfo] processName];
 
63
 
 
64
    return appName;
 
65
}
 
66
 
 
67
/* Fix warnings generated when using the setAppleMenu method and compiling
 
68
in Tiger or later */
 
69
@interface NSApplication (EUAE)
 
70
- (void)setAppleMenu:(NSMenu *)menu;
 
71
@end
 
72
 
 
73
 
 
74
@interface EUAE_Application : NSApplication
 
75
@end
 
76
 
 
77
@implementation EUAE_Application
 
78
 
 
79
/* Invoked from the Quit menu item */
 
80
- (void)terminate:(id)sender
 
81
{
 
82
#ifdef USE_SDL
 
83
    /* Post a SDL_QUIT event */
 
84
    SDL_Event event;
 
85
    event.type = SDL_QUIT;
 
86
    SDL_PushEvent (&event);
 
87
#else
 
88
    /* FIXME */
 
89
#endif
 
90
}
 
91
 
 
92
- (void)sendEvent:(NSEvent *)anEvent
 
93
{
 
94
    if (NSKeyDown == [anEvent type] || NSKeyUp == [anEvent type]) {
 
95
        if ([anEvent modifierFlags] & NSCommandKeyMask)
 
96
            [super sendEvent: anEvent];
 
97
    } else
 
98
        [super sendEvent: anEvent];
 
99
}
 
100
 
 
101
@end
 
102
 
 
103
 
 
104
/* The main class of the application, the application's delegate */
 
105
@implementation EUAE_Main
 
106
 
 
107
/* Set the working directory to the .app's parent directory */
 
108
- (void) setupWorkingDirectory:(BOOL)shouldChdir
 
109
{
 
110
    if (shouldChdir) {
 
111
        char parentdir[MAXPATHLEN];
 
112
        CFURLRef url  = CFBundleCopyBundleURL (CFBundleGetMainBundle());
 
113
        CFURLRef url2 = CFURLCreateCopyDeletingLastPathComponent (0, url);
 
114
 
 
115
        if (CFURLGetFileSystemRepresentation (url2, true, (UInt8 *)parentdir, MAXPATHLEN)) {
 
116
            chdir (parentdir) == 0;   /* chdir to the binary app's parent */
 
117
        }
 
118
        CFRelease (url);
 
119
        CFRelease (url2);
 
120
    }
 
121
}
 
122
 
 
123
static void setApplicationMenu (void)
 
124
{
 
125
    /* warning: this code is very odd */
 
126
    NSMenu *appleMenu;
 
127
    NSMenuItem *menuItem;
 
128
    NSString *title;
 
129
    NSString *appName;
 
130
 
 
131
    appName = getApplicationName ();
 
132
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
 
133
 
 
134
    /* Add menu items */
 
135
    title = [@"About " stringByAppendingString:appName];
 
136
    [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
 
137
 
 
138
    [appleMenu addItem:[NSMenuItem separatorItem]];
 
139
 
 
140
    title = [@"Hide " stringByAppendingString:appName];
 
141
    /* E-UAE: Removed @"h" key equivalent so it doesn't interfere with using the command key as an emulated amiga key */
 
142
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@""];
 
143
 
 
144
    /* E-UAE: Removed @"h" key equivalent so it doesn't interfere with using the command key as an emulated amiga key */
 
145
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@""];
 
146
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
 
147
 
 
148
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
 
149
 
 
150
    [appleMenu addItem:[NSMenuItem separatorItem]];
 
151
 
 
152
    title = [@"Quit " stringByAppendingString:appName];
 
153
    /* E-UAE: Removed @"q" key equivalent so it doesn't interfere with using the command key as an emulated amiga key */
 
154
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@""];
 
155
 
 
156
 
 
157
    /* Put menu into the menubar */
 
158
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
 
159
    [menuItem setSubmenu:appleMenu];
 
160
    [[NSApp mainMenu] addItem:menuItem];
 
161
 
 
162
    /* Tell the application object that this is now the application menu */
 
163
    [NSApp setAppleMenu:appleMenu];
 
164
 
 
165
    /* Finally give up our references to the objects */
 
166
    [appleMenu release];
 
167
    [menuItem release];
 
168
}
 
169
 
 
170
/* Create a window menu */
 
171
static void setupWindowMenu (void)
 
172
{
 
173
    NSMenu      *windowMenu;
 
174
    NSMenuItem  *windowMenuItem;
 
175
    NSMenuItem  *menuItem;
 
176
 
 
177
    windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
 
178
 
 
179
    /* "Minimize" item */
 
180
    /* E-UAE: Removed @"m" key equivalent so it doesn't interfere with using the command key as an emulated amiga key */
 
181
    menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@""];
 
182
    [windowMenu addItem:menuItem];
 
183
    [menuItem release];
 
184
 
 
185
    /* Put menu into the menubar */
 
186
    windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
 
187
    [windowMenuItem setSubmenu:windowMenu];
 
188
    [[NSApp mainMenu] addItem:windowMenuItem];
 
189
 
 
190
    /* Tell the application object that this is now the window menu */
 
191
    [NSApp setWindowsMenu:windowMenu];
 
192
 
 
193
    /* Finally give up our references to the objects */
 
194
    [windowMenu release];
 
195
    [windowMenuItem release];
 
196
}
 
197
 
 
198
/* Replacement for NSApplicationMain */
 
199
static void CustomApplicationMain (int argc, char **argv)
 
200
{
 
201
    NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];
 
202
    EUAE_Main           *euae_main;
 
203
 
 
204
    /* Ensure the application object is initialised */
 
205
    [EUAE_Application sharedApplication];
 
206
 
 
207
#ifdef SDL_USE_CPS
 
208
    {
 
209
        CPSProcessSerNum PSN;
 
210
        /* Tell the dock about us */
 
211
        if (!CPSGetCurrentProcess(&PSN))
 
212
            if (!CPSEnableForegroundOperation(&PSN,0x03,0x3C,0x2C,0x1103))
 
213
                if (!CPSSetFrontProcess(&PSN))
 
214
                    [EUAE_Application sharedApplication];
 
215
    }
 
216
#endif /* SDL_USE_CPS */
 
217
 
 
218
    /* Set up the menubar */
 
219
    [NSApp setMainMenu:[[NSMenu alloc] init]];
 
220
    setApplicationMenu ();
 
221
    setupWindowMenu ();
 
222
 
 
223
    /* Create SDLMain and make it the app delegate */
 
224
    euae_main = [[EUAE_Main alloc] init];
 
225
    [NSApp setDelegate:euae_main];
 
226
 
 
227
    /* Start the main event loop */
 
228
    [NSApp run];
 
229
 
 
230
    [euae_main release];
 
231
    [pool release];
 
232
}
 
233
 
 
234
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
 
235
{
 
236
    return NO;
 
237
}
 
238
 
 
239
extern NSString *finderLaunchFilename;
 
240
- (BOOL) application:(NSApplication *)theApplication openFile:(NSString *)filename
 
241
{
 
242
    if (filename != nil) {
 
243
        if (finderLaunchFilename != nil)
 
244
            [finderLaunchFilename release];
 
245
        finderLaunchFilename = [[NSString alloc] initWithString:filename];
 
246
        return YES;
 
247
    }
 
248
 
 
249
    return NO;
 
250
}
 
251
 
 
252
/* Called when the internal event loop has just started running */
 
253
- (void) applicationDidFinishLaunching: (NSNotification *) note
 
254
{
 
255
    /* Set the working directory to the .app's parent directory */
 
256
    [self setupWorkingDirectory:gFinderLaunch];
 
257
 
 
258
#ifdef USE_SDL
 
259
    setenv ("SDL_ENABLEAPPEVENTS", "1", 1);
 
260
 
 
261
 
 
262
    if (init_sdl ()) 
 
263
#endif
 
264
    {
 
265
      
 
266
        /* Hand off to main application code */
 
267
        real_main (gArgc, gArgv);
 
268
    }
 
269
 
 
270
    /* We're done, thank you for playing */
 
271
    exit (EXIT_SUCCESS);
 
272
}
 
273
 
 
274
@end
 
275
 
 
276
 
 
277
#ifdef main
 
278
#  undef main
 
279
#endif
 
280
 
 
281
/* Main entry point to executable - should *not* be SDL_main! */
 
282
int main (int argc, char **argv)
 
283
{
 
284
    char logfile_path[MAX_PATH] = "~/Library/Logs/E-UAE.log";
 
285
 
 
286
    /* Copy the arguments into a global variable */
 
287
    /* This is passed if we are launched by double-clicking */
 
288
    if (argc >= 2 && strncmp (argv[1], "-psn", 4) == 0 ) {
 
289
        gArgc = 1;
 
290
        gFinderLaunch = YES;
 
291
        gFinderLaunch = YES;
 
292
        cfgfile_subst_home (logfile_path, MAX_PATH);
 
293
        set_logfile (logfile_path);
 
294
    } else {
 
295
        gArgc = argc;
 
296
        gFinderLaunch = NO;
 
297
    }
 
298
    gArgv = argv;
 
299
 
 
300
    CustomApplicationMain (argc, argv);
 
301
 
 
302
    return 0;
 
303
}
 
304
 
 
305
 
 
306
/* Dummy for now */
 
307
void setup_brkhandler (void)
 
308
{
 
309
}
 
310
 
 
311
 
 
312
/*
 
313
 * Handle target-specific cfgfile options
 
314
 */
 
315
void target_save_options (FILE *f, const struct uae_prefs *p)
 
316
{
 
317
}
 
318
 
 
319
int target_parse_option (struct uae_prefs *p, const char *option, const char *value)
 
320
{
 
321
    return 0;
 
322
}
 
323
 
 
324
void target_default_options (struct uae_prefs *p)
 
325
{
 
326
}