Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.m
changeset 1298:d0eb2307b847
prev1109:700c5ab26a63
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
file annotate diff log raw
nkeynes@964
     1
/**
nkeynes@964
     2
 * $Id$
nkeynes@964
     3
 *
nkeynes@964
     4
 * Core Cocoa-based user interface
nkeynes@964
     5
 *
nkeynes@964
     6
 * Copyright (c) 2008 Nathan Keynes.
nkeynes@964
     7
 *
nkeynes@964
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@964
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@964
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@964
    11
 * (at your option) any later version.
nkeynes@964
    12
 *
nkeynes@964
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@964
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@964
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@964
    16
 * GNU General Public License for more details.
nkeynes@964
    17
 */
nkeynes@964
    18
nkeynes@964
    19
#include <AppKit/AppKit.h>
nkeynes@964
    20
#include <stdio.h>
nkeynes@964
    21
#include <stdlib.h>
nkeynes@964
    22
#include <string.h>
nkeynes@964
    23
#include <sys/time.h>
nkeynes@964
    24
#include "lxdream.h"
nkeynes@964
    25
#include "dream.h"
nkeynes@964
    26
#include "dreamcast.h"
nkeynes@964
    27
#include "config.h"
nkeynes@1041
    28
#include "lxpaths.h"
nkeynes@964
    29
#include "display.h"
nkeynes@964
    30
#include "gui.h"
nkeynes@964
    31
#include "gdrom/gdrom.h"
nkeynes@964
    32
#include "gdlist.h"
nkeynes@964
    33
#include "loader.h"
nkeynes@964
    34
#include "cocoaui/cocoaui.h"
nkeynes@964
    35
nkeynes@964
    36
void cocoa_gui_update( void );
nkeynes@964
    37
void cocoa_gui_start( void );
nkeynes@964
    38
void cocoa_gui_stop( void );
nkeynes@964
    39
uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
nkeynes@964
    40
nkeynes@964
    41
struct dreamcast_module cocoa_gui_module = { "gui", NULL,
nkeynes@964
    42
        cocoa_gui_update, 
nkeynes@964
    43
        cocoa_gui_start, 
nkeynes@964
    44
        cocoa_gui_run_slice, 
nkeynes@964
    45
        cocoa_gui_stop, 
nkeynes@964
    46
        NULL, NULL };
nkeynes@964
    47
nkeynes@964
    48
/**
nkeynes@964
    49
 * Count of running nanoseconds - used to cut back on the GUI runtime
nkeynes@964
    50
 */
nkeynes@964
    51
static uint32_t cocoa_gui_nanos = 0;
nkeynes@964
    52
static uint32_t cocoa_gui_ticks = 0;
nkeynes@964
    53
static struct timeval cocoa_gui_lasttv;
nkeynes@964
    54
static BOOL cocoa_gui_autorun = NO;
nkeynes@964
    55
static BOOL cocoa_gui_is_running = NO;
nkeynes@964
    56
static LxdreamMainWindow *mainWindow = NULL;
nkeynes@964
    57
nkeynes@964
    58
@interface NSApplication (PrivateAdditions)
nkeynes@964
    59
- (void) setAppleMenu:(NSMenu *)aMenu;
nkeynes@964
    60
@end
nkeynes@964
    61
nkeynes@1098
    62
gboolean cocoa_gui_disc_changed( cdrom_disc_t disc, const gchar *disc_name, void *user_data )
nkeynes@964
    63
{
nkeynes@964
    64
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
nkeynes@964
    65
    LxdreamMainWindow *window = (LxdreamMainWindow *)user_data;
nkeynes@964
    66
    [window updateTitle];
nkeynes@964
    67
    [pool release];
nkeynes@964
    68
    return TRUE;
nkeynes@964
    69
}
nkeynes@964
    70
nkeynes@964
    71
/**
nkeynes@964
    72
 * Produces the menu title by looking the text up in gettext, removing any
nkeynes@964
    73
 * underscores, and returning the result as an NSString.
nkeynes@964
    74
 */
nkeynes@964
    75
static NSString *NSMENU_( const char *text )
nkeynes@964
    76
{
nkeynes@964
    77
    const char *s = gettext(text);
nkeynes@964
    78
    char buf[strlen(s)+1];
nkeynes@964
    79
    char *d = buf;
nkeynes@964
    80
nkeynes@964
    81
    while( *s != '\0' ) {
nkeynes@964
    82
        if( *s != '_' ) {
nkeynes@964
    83
            *d++ = *s;
nkeynes@964
    84
        }
nkeynes@964
    85
        s++;
nkeynes@964
    86
    }
nkeynes@964
    87
    *d = '\0';
nkeynes@964
    88
    
nkeynes@964
    89
    return [NSString stringWithUTF8String: buf];
nkeynes@964
    90
}
nkeynes@964
    91
nkeynes@964
    92
static void cocoa_gui_create_menu(void)
nkeynes@964
    93
{
nkeynes@1040
    94
    int i;
nkeynes@964
    95
    NSMenu *appleMenu, *services;
nkeynes@964
    96
    NSMenuItem *menuItem;
nkeynes@964
    97
    NSString *title;
nkeynes@964
    98
    NSString *appName;
nkeynes@964
    99
nkeynes@964
   100
    appName = @"Lxdream";
nkeynes@964
   101
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
nkeynes@964
   102
nkeynes@964
   103
    /* Add menu items */
nkeynes@964
   104
    title = [@"About " stringByAppendingString:appName];
nkeynes@964
   105
    [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
nkeynes@964
   106
    
nkeynes@964
   107
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@964
   108
    [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
nkeynes@964
   109
nkeynes@964
   110
    // Services Menu
nkeynes@964
   111
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@964
   112
    services = [[[NSMenu alloc] init] autorelease];
nkeynes@964
   113
    [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
nkeynes@964
   114
    [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
nkeynes@964
   115
nkeynes@964
   116
    // Hide AppName
nkeynes@964
   117
    title = [@"Hide " stringByAppendingString:appName];
nkeynes@964
   118
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
nkeynes@964
   119
nkeynes@964
   120
    // Hide Others
nkeynes@964
   121
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
nkeynes@964
   122
                              action:@selector(hideOtherApplications:) 
nkeynes@964
   123
                              keyEquivalent:@"h"];
nkeynes@964
   124
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@964
   125
nkeynes@964
   126
    // Show All
nkeynes@964
   127
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
nkeynes@964
   128
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@964
   129
nkeynes@964
   130
    // Quit AppName
nkeynes@964
   131
    title = [@"Quit " stringByAppendingString:appName];
nkeynes@964
   132
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
nkeynes@964
   133
nkeynes@964
   134
    /* Put menu into the menubar */
nkeynes@964
   135
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
nkeynes@964
   136
    [menuItem setSubmenu: appleMenu];
nkeynes@964
   137
    NSMenu *menu = [NSMenu new];
nkeynes@964
   138
    [menu addItem: menuItem];
nkeynes@964
   139
nkeynes@964
   140
    NSMenu *gdromMenu = cocoa_gdrom_menu_new();
nkeynes@964
   141
nkeynes@1040
   142
    NSMenu *quickStateMenu = [[NSMenu alloc] initWithTitle:NSMENU_("_Quick State")];
nkeynes@1040
   143
    int quickState = dreamcast_get_quick_state();
nkeynes@1040
   144
    for( i=0; i<=MAX_QUICK_STATE; i++ ) {
nkeynes@1040
   145
    	NSString *label = [NSString stringWithFormat: NSMENU_("State _%d"), i];
nkeynes@1040
   146
    	NSString *keyEquiv = [NSString stringWithFormat: @"%d", i];
nkeynes@1040
   147
    	menuItem = [[NSMenuItem alloc] initWithTitle: label action: @selector(quick_state_action:) keyEquivalent: keyEquiv];
nkeynes@1040
   148
    	[menuItem setTag: i];
nkeynes@1040
   149
    	if( i == quickState ) {
nkeynes@1040
   150
    	    [menuItem setState:NSOnState];
nkeynes@1040
   151
    	}
nkeynes@1040
   152
    	[quickStateMenu addItem: menuItem];
nkeynes@1040
   153
    }
nkeynes@1040
   154
nkeynes@964
   155
    NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
nkeynes@964
   156
    [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
nkeynes@964
   157
    [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
nkeynes@964
   158
      setSubmenu: gdromMenu];
nkeynes@964
   159
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@964
   160
    [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
nkeynes@964
   161
      setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@964
   162
    [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
nkeynes@964
   163
    [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
nkeynes@964
   164
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@1040
   165
    [fileMenu addItemWithTitle: NSMENU_("L_oad State...") action: @selector(load_action:) keyEquivalent: @"o"];
nkeynes@1040
   166
    [fileMenu addItemWithTitle: NSMENU_("S_ave State...") action: @selector(save_action:) keyEquivalent: @"a"];
nkeynes@1040
   167
    menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("Select _Quick State") action: nil keyEquivalent: @""];
nkeynes@1040
   168
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@1040
   169
    [fileMenu addItemWithTitle: NSMENU_("_Load Quick State") action: @selector(quick_load_action:) keyEquivalent: @"l"]; 
nkeynes@1040
   170
    [fileMenu addItemWithTitle: NSMENU_("_Save Quick State") action: @selector(quick_save_action:) keyEquivalent: @"s"];
nkeynes@1040
   171
    [menuItem setSubmenu: quickStateMenu];
nkeynes@1040
   172
    [fileMenu addItem: menuItem];
nkeynes@1028
   173
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@1028
   174
    [fileMenu addItemWithTitle: NSMENU_("_Full Screen...") action: @selector(fullscreen_action:) keyEquivalent: @"\r"];
nkeynes@964
   175
nkeynes@964
   176
    menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
nkeynes@964
   177
    [menuItem setSubmenu: fileMenu];
nkeynes@964
   178
    [menu addItem: menuItem];
nkeynes@964
   179
nkeynes@964
   180
    /* Tell the application object that this is now the application menu */
nkeynes@964
   181
    [NSApp setMainMenu: menu];
nkeynes@964
   182
    [NSApp setAppleMenu: appleMenu];
nkeynes@964
   183
    [NSApp setServicesMenu: services];
nkeynes@964
   184
nkeynes@964
   185
    /* Finally give up our references to the objects */
nkeynes@964
   186
    [appleMenu release];
nkeynes@964
   187
    [menuItem release];
nkeynes@964
   188
    [menu release];
nkeynes@964
   189
}
nkeynes@964
   190
nkeynes@964
   191
@interface LxdreamDelegate : NSObject
nkeynes@964
   192
@end
nkeynes@964
   193
nkeynes@964
   194
@implementation LxdreamDelegate
nkeynes@964
   195
- (void)windowWillClose: (NSNotification *)notice
nkeynes@964
   196
{
nkeynes@964
   197
    dreamcast_shutdown();
nkeynes@964
   198
    exit(0);
nkeynes@964
   199
}
nkeynes@964
   200
- (void)windowDidBecomeMain: (NSNotification *)notice
nkeynes@964
   201
{
nkeynes@964
   202
    if( cocoa_gui_autorun ) {
nkeynes@964
   203
        cocoa_gui_autorun = NO;
nkeynes@1019
   204
        gui_do_later(dreamcast_run);
nkeynes@964
   205
    }
nkeynes@964
   206
}
nkeynes@964
   207
- (void)windowDidBecomeKey: (NSNotification *)notice
nkeynes@964
   208
{
nkeynes@964
   209
    display_set_focused( TRUE );
nkeynes@964
   210
}
nkeynes@964
   211
- (void)windowDidResignKey: (NSNotification *)notice
nkeynes@964
   212
{
nkeynes@964
   213
    display_set_focused( FALSE );
nkeynes@964
   214
    [mainWindow setIsGrabbed: NO];
nkeynes@964
   215
}
nkeynes@964
   216
- (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
nkeynes@964
   217
{
nkeynes@964
   218
    const gchar *cname = [filename UTF8String];
nkeynes@1109
   219
    ERROR err;
nkeynes@1109
   220
    if( file_load_magic(cname, FALSE, &err) != FILE_ERROR ) {
nkeynes@964
   221
        // Queue up a run event
nkeynes@1019
   222
        gui_do_later(dreamcast_run);
nkeynes@964
   223
        return YES;
nkeynes@964
   224
    } else {
nkeynes@964
   225
        return NO;
nkeynes@964
   226
    }
nkeynes@964
   227
    
nkeynes@964
   228
}
nkeynes@964
   229
- (void) about_action: (id)sender
nkeynes@964
   230
{
nkeynes@964
   231
    NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
nkeynes@964
   232
    NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright),  nil];
nkeynes@964
   233
nkeynes@964
   234
    NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
nkeynes@964
   235
nkeynes@964
   236
    [NSApp orderFrontStandardAboutPanelWithOptions: options];
nkeynes@964
   237
}
nkeynes@964
   238
- (void) preferences_action: (id)sender
nkeynes@964
   239
{
nkeynes@964
   240
    cocoa_gui_show_preferences();
nkeynes@964
   241
}
nkeynes@964
   242
- (void) load_action: (id)sender
nkeynes@964
   243
{
nkeynes@964
   244
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@1041
   245
    NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_SAVE_PATH)];
nkeynes@964
   246
    NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
nkeynes@964
   247
    int result = [panel runModalForDirectory: path file: nil types: fileTypes];
nkeynes@964
   248
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   249
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   250
        dreamcast_load_state( [filename UTF8String] );
nkeynes@1041
   251
        set_gui_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
nkeynes@964
   252
    }
nkeynes@964
   253
}
nkeynes@964
   254
- (void) save_action: (id)sender
nkeynes@964
   255
{
nkeynes@964
   256
    NSSavePanel *panel = [NSSavePanel savePanel];
nkeynes@1041
   257
    NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_SAVE_PATH)];
nkeynes@964
   258
    [panel setRequiredFileType: @"dst"];
nkeynes@964
   259
    int result = [panel runModalForDirectory: path file:@""];
nkeynes@964
   260
    if( result == NSOKButton ) {
nkeynes@964
   261
        NSString *filename = [panel filename];
nkeynes@964
   262
        dreamcast_save_state( [filename UTF8String] );
nkeynes@1041
   263
        set_gui_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
nkeynes@964
   264
    }
nkeynes@964
   265
}
nkeynes@964
   266
- (void) load_binary_action: (id)sender
nkeynes@964
   267
{
nkeynes@964
   268
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@1041
   269
    NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_DEFAULT_PATH)];
nkeynes@964
   270
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@964
   271
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   272
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@1109
   273
        ERROR err;
nkeynes@1109
   274
        gboolean ok = file_load_exec( [filename UTF8String], &err );
nkeynes@1041
   275
        set_gui_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
nkeynes@1109
   276
        if( !ok ) {
nkeynes@1109
   277
            ERROR( err.msg );
nkeynes@1109
   278
        }
nkeynes@964
   279
    }
nkeynes@964
   280
}
nkeynes@964
   281
- (void) mount_action: (id)sender
nkeynes@964
   282
{
nkeynes@964
   283
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@1041
   284
    NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_DEFAULT_PATH)];
nkeynes@964
   285
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@964
   286
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@1109
   287
        ERROR err;
nkeynes@964
   288
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@1109
   289
        gboolean ok = gdrom_mount_image( [filename UTF8String], &err );
nkeynes@1109
   290
        if( !ok ) {
nkeynes@1109
   291
            ERROR(err.msg);
nkeynes@1109
   292
        }
nkeynes@1041
   293
        set_gui_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
nkeynes@964
   294
    }
nkeynes@964
   295
}
nkeynes@964
   296
- (void) pause_action: (id)sender
nkeynes@964
   297
{
nkeynes@964
   298
    dreamcast_stop();
nkeynes@964
   299
}
nkeynes@964
   300
nkeynes@964
   301
- (void) reset_action: (id)sender
nkeynes@964
   302
{
nkeynes@964
   303
    dreamcast_reset();
nkeynes@964
   304
}
nkeynes@964
   305
- (void) run_action: (id)sender
nkeynes@964
   306
{
nkeynes@964
   307
    if( !dreamcast_is_running() ) {
nkeynes@1019
   308
        gui_do_later(dreamcast_run);
nkeynes@964
   309
    }
nkeynes@964
   310
}
nkeynes@964
   311
- (void) gdrom_list_action: (id)sender
nkeynes@964
   312
{
nkeynes@1109
   313
    ERROR err;
nkeynes@1109
   314
    gboolean ok = gdrom_list_set_selection( [sender tag], &err );
nkeynes@1109
   315
    if( !ok ) {
nkeynes@1109
   316
        ERROR( err.msg );
nkeynes@1109
   317
    }
nkeynes@964
   318
}
nkeynes@1028
   319
- (void) fullscreen_action: (id)sender
nkeynes@1028
   320
{
nkeynes@1028
   321
    [mainWindow setFullscreen: ![mainWindow isFullscreen]]; 
nkeynes@1028
   322
}
nkeynes@1040
   323
- (void) quick_state_action: (id)sender
nkeynes@1040
   324
{
nkeynes@1040
   325
    [[[sender menu] itemWithTag: dreamcast_get_quick_state()] setState: NSOffState ];
nkeynes@1040
   326
    [sender setState: NSOnState ];
nkeynes@1040
   327
    dreamcast_set_quick_state( [sender tag] );
nkeynes@1040
   328
}
nkeynes@1040
   329
- (void) quick_save_action: (id)sender
nkeynes@1040
   330
{
nkeynes@1040
   331
    dreamcast_quick_save();
nkeynes@1040
   332
}
nkeynes@1040
   333
- (void) quick_load_action: (id)sender
nkeynes@1040
   334
{
nkeynes@1040
   335
    dreamcast_quick_load();
nkeynes@1040
   336
}
nkeynes@964
   337
@end
nkeynes@964
   338
nkeynes@964
   339
nkeynes@964
   340
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@964
   341
{
nkeynes@964
   342
    /* If started from the finder, the first (and only) arg will look something like 
nkeynes@964
   343
     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
nkeynes@964
   344
     * normally
nkeynes@964
   345
     */
nkeynes@964
   346
    if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
nkeynes@964
   347
        *argc = 1;
nkeynes@964
   348
    }
nkeynes@964
   349
    return TRUE;
nkeynes@964
   350
}
nkeynes@964
   351
nkeynes@1015
   352
gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
nkeynes@964
   353
{
nkeynes@964
   354
    dreamcast_register_module( &cocoa_gui_module );
nkeynes@964
   355
nkeynes@964
   356
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
nkeynes@964
   357
    [NSApplication sharedApplication];
nkeynes@964
   358
nkeynes@964
   359
    LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
nkeynes@1298
   360
    [NSApp setDelegate: (id)delegate];
nkeynes@1017
   361
    NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"lxdream" ofType:@"png"];
nkeynes@964
   362
    NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
nkeynes@964
   363
    [iconImage setName: @"NSApplicationIcon"];
nkeynes@964
   364
    [NSApp setApplicationIconImage: iconImage];   
nkeynes@964
   365
    cocoa_gui_create_menu();
nkeynes@964
   366
    mainWindow = cocoa_gui_create_main_window();
nkeynes@964
   367
    [mainWindow makeKeyAndOrderFront: nil];
nkeynes@964
   368
    [NSApp activateIgnoringOtherApps: YES];   
nkeynes@964
   369
nkeynes@964
   370
    register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
nkeynes@1028
   371
    if( withFullscreen ) {
nkeynes@1028
   372
    	[mainWindow setFullscreen: YES];
nkeynes@1028
   373
    }
nkeynes@964
   374
    [pool release];
nkeynes@964
   375
    return TRUE;
nkeynes@964
   376
}
nkeynes@964
   377
nkeynes@964
   378
void gui_main_loop( gboolean run )
nkeynes@964
   379
{
nkeynes@964
   380
    if( run ) {
nkeynes@964
   381
        cocoa_gui_autorun = YES;
nkeynes@964
   382
    }
nkeynes@964
   383
    cocoa_gui_is_running = YES;
nkeynes@964
   384
    [NSApp run];
nkeynes@964
   385
    cocoa_gui_is_running = NO;
nkeynes@964
   386
}
nkeynes@964
   387
nkeynes@964
   388
void gui_update_state(void)
nkeynes@964
   389
{
nkeynes@964
   390
    cocoa_gui_update();
nkeynes@964
   391
}
nkeynes@964
   392
nkeynes@964
   393
void gui_set_use_grab( gboolean grab )
nkeynes@964
   394
{
nkeynes@964
   395
    [mainWindow setUseGrab: (grab ? YES : NO)];
nkeynes@964
   396
}
nkeynes@964
   397
nkeynes@964
   398
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@964
   399
{
nkeynes@964
   400
    if( cocoa_gui_is_running ) {
nkeynes@964
   401
        NSString *error_string;
nkeynes@964
   402
nkeynes@964
   403
        va_list args;
nkeynes@964
   404
        va_start(args, msg);
nkeynes@964
   405
        error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
nkeynes@1298
   406
        NSRunAlertPanel(NS_("Error in Lxdream"), @"%@", nil, nil, nil, error_string);
nkeynes@964
   407
        va_end(args);
nkeynes@964
   408
        return TRUE;
nkeynes@964
   409
    } else {
nkeynes@964
   410
        return FALSE;
nkeynes@964
   411
    }
nkeynes@964
   412
}
nkeynes@964
   413
nkeynes@964
   414
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@964
   415
{
nkeynes@964
   416
nkeynes@964
   417
}
nkeynes@964
   418
nkeynes@964
   419
nkeynes@964
   420
uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
nkeynes@964
   421
{
nkeynes@964
   422
    NSEvent *event;
nkeynes@964
   423
    NSAutoreleasePool *pool;
nkeynes@964
   424
nkeynes@964
   425
    cocoa_gui_nanos += nanosecs;
nkeynes@964
   426
    if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@964
   427
        cocoa_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@964
   428
        cocoa_gui_ticks ++;
nkeynes@964
   429
        uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
nkeynes@964
   430
nkeynes@964
   431
        // Run the event loop
nkeynes@964
   432
        pool = [NSAutoreleasePool new];
nkeynes@964
   433
        while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
nkeynes@964
   434
                         inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
nkeynes@964
   435
            [NSApp sendEvent: event];
nkeynes@964
   436
        }
nkeynes@964
   437
        [pool release];
nkeynes@964
   438
nkeynes@964
   439
        struct timeval tv;
nkeynes@964
   440
        gettimeofday(&tv,NULL);
nkeynes@964
   441
        uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@964
   442
        (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
nkeynes@964
   443
        if( (ns * 1.05) < current_period ) {
nkeynes@964
   444
            // We've gotten ahead - sleep for a little bit
nkeynes@964
   445
            struct timespec tv;
nkeynes@964
   446
            tv.tv_sec = 0;
nkeynes@964
   447
            tv.tv_nsec = current_period - ns;
nkeynes@964
   448
            nanosleep(&tv, &tv);
nkeynes@964
   449
        }
nkeynes@964
   450
nkeynes@964
   451
        /* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@964
   452
         * save the current tv value */
nkeynes@964
   453
        if( cocoa_gui_ticks > 10 ) {
nkeynes@964
   454
            gchar buf[32];
nkeynes@964
   455
            cocoa_gui_ticks -= 10;
nkeynes@964
   456
nkeynes@964
   457
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@964
   458
            cocoa_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@964
   459
            cocoa_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@964
   460
            snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
nkeynes@964
   461
            [mainWindow setStatusText: buf];
nkeynes@964
   462
nkeynes@964
   463
        }
nkeynes@964
   464
    }
nkeynes@964
   465
    return nanosecs;
nkeynes@964
   466
}
nkeynes@964
   467
nkeynes@964
   468
void cocoa_gui_update( void )
nkeynes@964
   469
{
nkeynes@964
   470
nkeynes@964
   471
}
nkeynes@964
   472
nkeynes@964
   473
void cocoa_gui_start( void )
nkeynes@964
   474
{
nkeynes@964
   475
    [mainWindow setRunning: YES];
nkeynes@964
   476
    cocoa_gui_nanos = 0;
nkeynes@964
   477
    gettimeofday(&cocoa_gui_lasttv,NULL);
nkeynes@964
   478
}
nkeynes@964
   479
nkeynes@964
   480
void cocoa_gui_stop( void )
nkeynes@964
   481
{
nkeynes@964
   482
    [mainWindow setRunning: NO];
nkeynes@964
   483
}
nkeynes@964
   484
nkeynes@1019
   485
@interface DoLaterStub : NSObject
nkeynes@1019
   486
{
nkeynes@1019
   487
    do_later_callback_t func;
nkeynes@1019
   488
}
nkeynes@1019
   489
@end    
nkeynes@1019
   490
nkeynes@1019
   491
@implementation DoLaterStub
nkeynes@1019
   492
- (id) init: (do_later_callback_t)f
nkeynes@1019
   493
{
nkeynes@1019
   494
    [super init];
nkeynes@1019
   495
    func = f;
nkeynes@1019
   496
    return self;
nkeynes@1019
   497
}
nkeynes@1019
   498
- (void) do
nkeynes@1019
   499
{
nkeynes@1019
   500
    func();
nkeynes@1019
   501
}
nkeynes@1019
   502
@end
nkeynes@1019
   503
nkeynes@964
   504
/**
nkeynes@964
   505
 * Queue a dreamcast_run() to execute after the currently event(s)
nkeynes@964
   506
 */
nkeynes@1019
   507
void gui_do_later( do_later_callback_t func )
nkeynes@964
   508
{
nkeynes@1019
   509
    DoLaterStub *stub = [[[DoLaterStub alloc] init: func] autorelease]; 
nkeynes@1019
   510
    [[NSRunLoop currentRunLoop] performSelector: @selector(do) 
nkeynes@1019
   511
     target: stub argument: nil order: 1 
nkeynes@964
   512
     modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
nkeynes@964
   513
}
nkeynes@964
   514
nkeynes@964
   515
/*************************** Convenience methods ***************************/
nkeynes@964
   516
nkeynes@964
   517
NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
nkeynes@964
   518
{
nkeynes@964
   519
    NSBitmapImageRep *rep = 
nkeynes@964
   520
        [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
nkeynes@964
   521
         pixelsWide: buffer->width  pixelsHigh: buffer->height
nkeynes@964
   522
         bitsPerSample: 8 samplesPerPixel: 3
nkeynes@964
   523
         hasAlpha: NO isPlanar: NO
nkeynes@964
   524
         colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
nkeynes@964
   525
         bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
nkeynes@964
   526
nkeynes@964
   527
    NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
nkeynes@964
   528
    [image addRepresentation: rep];
nkeynes@964
   529
    return image;
nkeynes@964
   530
}
nkeynes@964
   531
nkeynes@964
   532
nkeynes@964
   533
NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
nkeynes@964
   534
{
nkeynes@964
   535
    NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
nkeynes@964
   536
    [label setStringValue: text];
nkeynes@964
   537
    [label setBordered: NO];
nkeynes@964
   538
    [label setDrawsBackground: NO];
nkeynes@964
   539
    [label setEditable: NO];
nkeynes@964
   540
    [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
nkeynes@964
   541
    if( parent != NULL ) {
nkeynes@964
   542
        [parent addSubview: label];
nkeynes@964
   543
    }
nkeynes@964
   544
    return label;
nkeynes@964
   545
}
.