aboutsummaryrefslogtreecommitdiffstats
path: root/code/macosx/Q3Controller.m
blob: 6ddf9d5406676473b285bdd598797ec79a9f838c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#import "Q3Controller.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

#include "client.h"
#include "macosx_local.h"
//#include "GameRanger SDK/gameranger.h"
#ifdef OMNI_TIMER
#import "macosx_timers.h"
#endif

#define MAX_ARGC 1024

static qboolean Sys_IsProcessingTerminationRequest = qfalse;
static void Sys_CreatePathToFile(NSString *path, NSDictionary *attributes);

@interface Q3Controller (Private)
- (void)quakeMain;
@end

@implementation Q3Controller

#ifndef DEDICATED

- (void)applicationDidFinishLaunching:(NSNotification *)notification;
{
    NS_DURING {
        [self quakeMain];
    } NS_HANDLER {
        Sys_Error("%@", [localException reason]);
    } NS_ENDHANDLER;
    Sys_Quit();
}

- (void)applicationDidUnhide:(NSNotification *)notification;
{
    // Don't reactivate the game if we are asking whether to quit
    if (Sys_IsProcessingTerminationRequest)
        return;
        
    if (!Sys_Unhide())
        // Didn't work -- hide again so we should get another chance to unhide later
        [NSApp hide: nil];
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
{
    int choice;

    if (!Sys_IsHidden) {
        // We're terminating via -terminate:
        return NSTerminateNow;
    }
    
    // Avoid reactivating GL when we unhide due to this panel
    Sys_IsProcessingTerminationRequest = qtrue;
    choice = NSRunAlertPanel(nil, @"Quit without saving?", @"Don't Quit", @"Quit", nil);
    Sys_IsProcessingTerminationRequest = qfalse;

    if (choice == NSAlertAlternateReturn)
        return NSTerminateNow;
        
    // Make sure we get re-hidden
    [NSApp hide:nil];
    
    return NSTerminateCancel;
}

// Actions

- (IBAction)paste:(id)sender;
{
    int shiftWasDown, insertWasDown;
    unsigned int currentTime;

    currentTime = Sys_Milliseconds();
    // Save the original keyboard state
    shiftWasDown = keys[K_SHIFT].down;
    insertWasDown = keys[K_INS].down;
    // Fake a Shift-Insert keyboard event
    keys[K_SHIFT].down = qtrue;
    Sys_QueEvent(currentTime, SE_KEY, K_INS, qtrue, 0, NULL);
    Sys_QueEvent(currentTime, SE_KEY, K_INS, qfalse, 0, NULL);
    // Restore the original keyboard state
    keys[K_SHIFT].down = shiftWasDown;
    keys[K_INS].down = insertWasDown;
}

extern void CL_Quit_f(void);


- (IBAction)requestTerminate:(id)sender;
{
    Com_Quit_f();
    // UI_QuitMenu();
}

- (void)showBanner;
{
    static BOOL hasShownBanner = NO;

    if (!hasShownBanner) {
        cvar_t *showBanner;

        hasShownBanner = YES;
        showBanner = Cvar_Get("cl_showBanner", "1", 0);
        if (showBanner->integer != 0) {
            NSPanel *splashPanel;
            NSImage *bannerImage;
            NSRect bannerRect;
            NSImageView *bannerImageView;
            
            bannerImage = [[NSImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForImageResource:@"banner.jpg"]];
            bannerRect = NSMakeRect(0.0, 0.0, [bannerImage size].width, [bannerImage size].height);
            
            splashPanel = [[NSPanel alloc] initWithContentRect:bannerRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
            
            bannerImageView = [[NSImageView alloc] initWithFrame:bannerRect];
            [bannerImageView setImage:bannerImage];
            [splashPanel setContentView:bannerImageView];
            [bannerImageView release];
            
            [splashPanel center];
            [splashPanel setHasShadow:YES];
            [splashPanel orderFront: nil];
            [NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:2.5]];
            [splashPanel close];
            
            [bannerImage release];
        }
    }
}

// Services

- (void)connectToServer:(NSPasteboard *)pasteboard userData:(NSString *)data error:(NSString **)error;
{
    NSArray *pasteboardTypes;

    pasteboardTypes = [pasteboard types];
    if ([pasteboardTypes containsObject:NSStringPboardType]) {
        NSString *requestedServer;

        requestedServer = [pasteboard stringForType:NSStringPboardType];
        if (requestedServer) {
            Cbuf_AddText(va("connect %s\n", [requestedServer cString]));
            return;
        }
    }
    *error = @"Unable to connect to server:  could not find string on pasteboard";
}

- (void)performCommand:(NSPasteboard *)pasteboard userData:(NSString *)data error:(NSString **)error;
{
    NSArray *pasteboardTypes;

    pasteboardTypes = [pasteboard types];
    if ([pasteboardTypes containsObject:NSStringPboardType]) {
        NSString *requestedCommand;

        requestedCommand = [pasteboard stringForType:NSStringPboardType];
        if (requestedCommand) {
            Cbuf_AddText(va("%s\n", [requestedCommand cString]));
            return;
        }
    }
    *error = @"Unable to perform command:  could not find string on pasteboard";
}

#endif

- (void)quakeMain;
{
    NSAutoreleasePool *pool;
    int argc = 0;
    const char *argv[MAX_ARGC];
    NSProcessInfo *processInfo;
    NSArray *arguments;
    unsigned int argumentIndex, argumentCount;
    NSFileManager *defaultManager;
    unsigned int commandLineLength;
    NSString *installationPathKey, *installationPath;
    char *cmdline;
    BOOL foundDirectory;
    NSString *appName, *demoAppName, *selectButton;
    int count = 0;
    pool = [[NSAutoreleasePool alloc] init];

    [NSApp setServicesProvider:self];

    processInfo = [NSProcessInfo processInfo];
    arguments = [processInfo arguments];
    argumentCount = [arguments count];
    for (argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++) {
        NSString *arg;
        
        arg = [arguments objectAtIndex:argumentIndex];
        // Don't pass the Process Serial Number command line arg that the Window Server/Finder invokes us with
        if ([arg hasPrefix: @"-psn_"])
            continue;
            
        argv[argc++] = strdup([arg cString]);
    }
    
    // Figure out where the level data is stored.
    installationPathKey = @"RetailInstallationPath";

    installationPath = [[NSUserDefaults standardUserDefaults] objectForKey:installationPathKey];
    if (!installationPath) {
        // Default to the directory containing the executable (which is where most users will want to put it
        installationPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
    }

#if !defined(DEDICATED)    
    appName = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleName"];
#else
	// We are hard coding the app name here since the dedicated server is a tool, not a app bundle and does not have access to the Info.plist that the client app does.  Suck.
    appName = @"Quake3";
#endif
    demoAppName = appName;

    while (YES) {
        NSString *dataPath;
        NSOpenPanel *openPanel;
        int result;
        
        foundDirectory = NO;
        defaultManager = [NSFileManager defaultManager];
        //NSLog(@"Candidate installation path = %@", installationPath);
        dataPath = [installationPath stringByAppendingPathComponent: @"baseq3"];
        
        if ([defaultManager fileExistsAtPath: dataPath]) {
            // Check that the data directory contains at least one .pk3 file.  We don't know what it will be named, so don't hard code a name (for example it might be named 'french.pk3' for a French release
            NSArray *files;
            unsigned int fileIndex;
            
            files = [defaultManager directoryContentsAtPath: dataPath];
            fileIndex = [files count];
            while (fileIndex--) {
                if ([[files objectAtIndex: fileIndex] hasSuffix: @"pk3"]) {
                    //NSLog(@"Found %@.", [files objectAtIndex: fileIndex]);
                    foundDirectory = YES;
                    break;
                }
            }
        }
        
        if (foundDirectory)
            break;

#ifdef DEDICATED
            break;
#warning TJW: We are hard coding the app name and default domain here since the dedicated server is a tool, not a app bundle and does not have access to the Info.plist that the client app does.  Suck.
        NSLog(@"Unable to determine installation directory.  Please move the executable into the '%@' installation directory or add a '%@' key in the 'Q3DedicatedServer' defaults domain.", appName, installationPathKey, [[NSBundle mainBundle] bundleIdentifier]);
        Sys_Quit();
        exit(1);
#else
        selectButton = @"Select Retail Installation...";

        result = NSRunAlertPanel(demoAppName, @"You need to select the installation directory for %@ (not any directory inside of it -- the installation directory itself).", selectButton, @"Quit", nil, appName);
        switch (result) {
            case NSAlertDefaultReturn:
                break;
            default:
                Sys_Quit();
                break;
        }
        
        openPanel = [NSOpenPanel openPanel];
        [openPanel setAllowsMultipleSelection:NO];
        [openPanel setCanChooseDirectories:YES];
        [openPanel setCanChooseFiles:NO];
        result = [openPanel runModalForDirectory:nil file:nil];
        if (result == NSOKButton) {
            NSArray *filenames;

            filenames = [openPanel filenames];
            if ([filenames count] == 1) {
                installationPath = [filenames objectAtIndex:0];
                [[NSUserDefaults standardUserDefaults] setObject:installationPath forKey:installationPathKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
        }
#endif
    }
    
    // Create the application support directory if it doesn't exist already
    do {
        NSArray *results;
        NSString *libraryPath, *homePath, *filePath;
        NSDictionary *attributes;
        
        results = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        if (![results count])
            break;
        
        libraryPath = [results objectAtIndex: 0];
        homePath = [libraryPath stringByAppendingPathComponent: @"Application Support"];
        homePath = [homePath stringByAppendingPathComponent: appName];
        filePath = [homePath stringByAppendingPathComponent: @"foo"];
        
        attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithUnsignedInt: 0750], NSFilePosixPermissions, nil];
        NS_DURING {
            Sys_CreatePathToFile(filePath, attributes);
            Sys_SetDefaultHomePath([homePath fileSystemRepresentation]);
        } NS_HANDLER {
            NSLog(@"Exception: %@", localException);
#ifndef DEDICATED
            NSRunAlertPanel(nil, @"Unable to create '%@'.  Please make sure that you have permission to write to this folder and re-run the game.", @"OK", nil, nil, homePath);
#endif
            Sys_Quit();
        } NS_ENDHANDLER;
    } while(0);
    
    // Provoke the CD scanning code into looking up the CD.
    Sys_CheckCD();
    
    // Let the filesystem know where our local install is
    Sys_SetDefaultInstallPath([installationPath cString]);

    cmdline = NULL;
#if 0
    if (GRCheckFileForCmd()) {
	GRGetWaitingCmd();
	if (GRHasProperty( 'Exec' )) {
            NSString *cfgPath, *grCfg;
            cfgPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
            cfgPath = [cfgPath stringByAppendingPathComponent: [NSString stringWithCString: GRGetPropertyStr( 'Exec' )]];
            grCfg = [NSString stringWithContentsOfFile: cfgPath];
            cmdline = malloc(strlen([grCfg cString])+1);
            [grCfg getCString: cmdline];
	}
    }
#endif
    if (!cmdline) {
        // merge the command line, this is kinda silly	
        for (commandLineLength = 1, argumentIndex = 1; argumentIndex < argc; argumentIndex++)
            commandLineLength += strlen(argv[argumentIndex]) + 1;
        cmdline = malloc(commandLineLength);
        *cmdline = '\0';
        for (argumentIndex = 1; argumentIndex < argc; argumentIndex++) {
            if (argumentIndex > 1)
                strcat(cmdline, " ");
            strcat(cmdline, argv[argumentIndex]);
        }
    }
    Com_Printf("command line: %s\n", cmdline);
    
    Com_Init(cmdline);

#ifndef DEDICATED
    [NSApp activateIgnoringOtherApps:YES];
#endif

    while (1) {
        Com_Frame();

        if ((count & 15)==0) {
            // We should think about doing this less frequently than every frame
            [pool release];
            pool = [[NSAutoreleasePool alloc] init];
        }
    }

    [pool release];
}

@end



// Creates any directories needed to be able to create a file at the specified path.  Raises an exception on failure.
static void Sys_CreatePathToFile(NSString *path, NSDictionary *attributes)
{
    NSArray *pathComponents;
    unsigned int dirIndex, dirCount;
    unsigned int startingIndex;
    NSFileManager *manager;
    
    manager = [NSFileManager defaultManager];
    pathComponents = [path pathComponents];
    dirCount = [pathComponents count] - 1;

    startingIndex = 0;
    for (dirIndex = startingIndex; dirIndex < dirCount; dirIndex++) {
        NSString *partialPath;
        BOOL fileExists;

        partialPath = [NSString pathWithComponents:[pathComponents subarrayWithRange:NSMakeRange(0, dirIndex + 1)]];
        
        // Don't use the 'fileExistsAtPath:isDirectory:' version since it doesn't traverse symlinks
        fileExists = [manager fileExistsAtPath:partialPath];
        if (!fileExists) {
            if (![manager createDirectoryAtPath:partialPath attributes:attributes]) {
                [NSException raise:NSGenericException format:@"Unable to create a directory at path: %@", partialPath];
            }
        } else {
            NSDictionary *attributes;

            attributes = [manager fileAttributesAtPath:partialPath traverseLink:YES];
            if (![[attributes objectForKey:NSFileType] isEqualToString: NSFileTypeDirectory]) {
                [NSException raise:NSGenericException format:@"Unable to write to path \"%@\" because \"%@\" is not a directory",
                    path, partialPath];
            }
        }
    }
}

#ifdef DEDICATED
void S_ClearSoundBuffer( void ) {
}
#endif