Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.m
changeset 964:f2f3c7612d06
next1015:ad448bedc48a
author nkeynes
date Thu Jan 15 04:15:11 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add support for the Intel ICC compiler (C only, icc doesn't support Obj-C)
- Rename Obj-C source to .m
- Separate paths.c into paths_unix.c and paths_osx.m
- Add configuration detection of ICC, along with specific opt flags
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@964
    28
#include "display.h"
nkeynes@964
    29
#include "gui.h"
nkeynes@964
    30
#include "gdrom/gdrom.h"
nkeynes@964
    31
#include "gdlist.h"
nkeynes@964
    32
#include "loader.h"
nkeynes@964
    33
#include "cocoaui/cocoaui.h"
nkeynes@964
    34
nkeynes@964
    35
void cocoa_gui_update( void );
nkeynes@964
    36
void cocoa_gui_start( void );
nkeynes@964
    37
void cocoa_gui_stop( void );
nkeynes@964
    38
void cocoa_gui_run_later( 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@964
    62
gboolean cocoa_gui_disc_changed( gdrom_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@964
    94
    NSMenu *appleMenu, *services;
nkeynes@964
    95
    NSMenuItem *menuItem;
nkeynes@964
    96
    NSString *title;
nkeynes@964
    97
    NSString *appName;
nkeynes@964
    98
nkeynes@964
    99
    appName = @"Lxdream";
nkeynes@964
   100
    appleMenu = [[NSMenu alloc] initWithTitle:@""];
nkeynes@964
   101
nkeynes@964
   102
    /* Add menu items */
nkeynes@964
   103
    title = [@"About " stringByAppendingString:appName];
nkeynes@964
   104
    [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
nkeynes@964
   105
    
nkeynes@964
   106
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@964
   107
    [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
nkeynes@964
   108
nkeynes@964
   109
    // Services Menu
nkeynes@964
   110
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@964
   111
    services = [[[NSMenu alloc] init] autorelease];
nkeynes@964
   112
    [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
nkeynes@964
   113
    [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
nkeynes@964
   114
nkeynes@964
   115
    // Hide AppName
nkeynes@964
   116
    title = [@"Hide " stringByAppendingString:appName];
nkeynes@964
   117
    [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
nkeynes@964
   118
nkeynes@964
   119
    // Hide Others
nkeynes@964
   120
    menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
nkeynes@964
   121
                              action:@selector(hideOtherApplications:) 
nkeynes@964
   122
                              keyEquivalent:@"h"];
nkeynes@964
   123
    [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@964
   124
nkeynes@964
   125
    // Show All
nkeynes@964
   126
    [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
nkeynes@964
   127
    [appleMenu addItem:[NSMenuItem separatorItem]];
nkeynes@964
   128
nkeynes@964
   129
    // Quit AppName
nkeynes@964
   130
    title = [@"Quit " stringByAppendingString:appName];
nkeynes@964
   131
    [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
nkeynes@964
   132
nkeynes@964
   133
    /* Put menu into the menubar */
nkeynes@964
   134
    menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
nkeynes@964
   135
    [menuItem setSubmenu: appleMenu];
nkeynes@964
   136
    NSMenu *menu = [NSMenu new];
nkeynes@964
   137
    [menu addItem: menuItem];
nkeynes@964
   138
nkeynes@964
   139
    NSMenu *gdromMenu = cocoa_gdrom_menu_new();
nkeynes@964
   140
nkeynes@964
   141
    NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
nkeynes@964
   142
    [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
nkeynes@964
   143
    [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
nkeynes@964
   144
      setSubmenu: gdromMenu];
nkeynes@964
   145
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@964
   146
    [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
nkeynes@964
   147
      setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@964
   148
    [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
nkeynes@964
   149
    [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
nkeynes@964
   150
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@964
   151
    [fileMenu addItemWithTitle: NSMENU_("_Load State...") action: @selector(load_action:) keyEquivalent: @"o"];
nkeynes@964
   152
    [fileMenu addItemWithTitle: NSMENU_("_Save State...") action: @selector(save_action:) keyEquivalent: @"s"];
nkeynes@964
   153
nkeynes@964
   154
    menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
nkeynes@964
   155
    [menuItem setSubmenu: fileMenu];
nkeynes@964
   156
    [menu addItem: menuItem];
nkeynes@964
   157
nkeynes@964
   158
    /* Tell the application object that this is now the application menu */
nkeynes@964
   159
    [NSApp setMainMenu: menu];
nkeynes@964
   160
    [NSApp setAppleMenu: appleMenu];
nkeynes@964
   161
    [NSApp setServicesMenu: services];
nkeynes@964
   162
nkeynes@964
   163
    /* Finally give up our references to the objects */
nkeynes@964
   164
    [appleMenu release];
nkeynes@964
   165
    [menuItem release];
nkeynes@964
   166
    [menu release];
nkeynes@964
   167
}
nkeynes@964
   168
nkeynes@964
   169
@interface LxdreamDelegate : NSObject
nkeynes@964
   170
@end
nkeynes@964
   171
nkeynes@964
   172
@implementation LxdreamDelegate
nkeynes@964
   173
- (void)windowWillClose: (NSNotification *)notice
nkeynes@964
   174
{
nkeynes@964
   175
    dreamcast_shutdown();
nkeynes@964
   176
    exit(0);
nkeynes@964
   177
}
nkeynes@964
   178
- (void)windowDidBecomeMain: (NSNotification *)notice
nkeynes@964
   179
{
nkeynes@964
   180
    if( cocoa_gui_autorun ) {
nkeynes@964
   181
        cocoa_gui_autorun = NO;
nkeynes@964
   182
        cocoa_gui_run_later();
nkeynes@964
   183
    }
nkeynes@964
   184
}
nkeynes@964
   185
- (void)windowDidBecomeKey: (NSNotification *)notice
nkeynes@964
   186
{
nkeynes@964
   187
    display_set_focused( TRUE );
nkeynes@964
   188
}
nkeynes@964
   189
- (void)windowDidResignKey: (NSNotification *)notice
nkeynes@964
   190
{
nkeynes@964
   191
    display_set_focused( FALSE );
nkeynes@964
   192
    [mainWindow setIsGrabbed: NO];
nkeynes@964
   193
}
nkeynes@964
   194
- (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
nkeynes@964
   195
{
nkeynes@964
   196
    const gchar *cname = [filename UTF8String];
nkeynes@964
   197
    if( file_load_magic(cname) ) {
nkeynes@964
   198
        // Queue up a run event
nkeynes@964
   199
        cocoa_gui_run_later();
nkeynes@964
   200
        return YES;
nkeynes@964
   201
    } else {
nkeynes@964
   202
        return NO;
nkeynes@964
   203
    }
nkeynes@964
   204
    
nkeynes@964
   205
}
nkeynes@964
   206
- (void) about_action: (id)sender
nkeynes@964
   207
{
nkeynes@964
   208
    NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
nkeynes@964
   209
    NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright),  nil];
nkeynes@964
   210
nkeynes@964
   211
    NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
nkeynes@964
   212
nkeynes@964
   213
    [NSApp orderFrontStandardAboutPanelWithOptions: options];
nkeynes@964
   214
}
nkeynes@964
   215
- (void) preferences_action: (id)sender
nkeynes@964
   216
{
nkeynes@964
   217
    cocoa_gui_show_preferences();
nkeynes@964
   218
}
nkeynes@964
   219
- (void) load_action: (id)sender
nkeynes@964
   220
{
nkeynes@964
   221
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@964
   222
    const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
nkeynes@964
   223
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@964
   224
    NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
nkeynes@964
   225
    int result = [panel runModalForDirectory: path file: nil types: fileTypes];
nkeynes@964
   226
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   227
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   228
        dreamcast_load_state( [filename UTF8String] );
nkeynes@964
   229
    }
nkeynes@964
   230
}
nkeynes@964
   231
- (void) save_action: (id)sender
nkeynes@964
   232
{
nkeynes@964
   233
    NSSavePanel *panel = [NSSavePanel savePanel];
nkeynes@964
   234
    const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
nkeynes@964
   235
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@964
   236
    [panel setRequiredFileType: @"dst"];
nkeynes@964
   237
    int result = [panel runModalForDirectory: path file:@""];
nkeynes@964
   238
    if( result == NSOKButton ) {
nkeynes@964
   239
        NSString *filename = [panel filename];
nkeynes@964
   240
        dreamcast_save_state( [filename UTF8String] );
nkeynes@964
   241
    }
nkeynes@964
   242
}
nkeynes@964
   243
- (void) load_binary_action: (id)sender
nkeynes@964
   244
{
nkeynes@964
   245
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@964
   246
    const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
nkeynes@964
   247
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@964
   248
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@964
   249
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   250
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   251
        file_load_magic( [filename UTF8String] );
nkeynes@964
   252
    }
nkeynes@964
   253
}
nkeynes@964
   254
- (void) mount_action: (id)sender
nkeynes@964
   255
{
nkeynes@964
   256
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@964
   257
    const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
nkeynes@964
   258
    NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
nkeynes@964
   259
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@964
   260
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   261
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   262
        gdrom_mount_image( [filename UTF8String] );
nkeynes@964
   263
    }
nkeynes@964
   264
}
nkeynes@964
   265
- (void) pause_action: (id)sender
nkeynes@964
   266
{
nkeynes@964
   267
    dreamcast_stop();
nkeynes@964
   268
}
nkeynes@964
   269
nkeynes@964
   270
- (void) reset_action: (id)sender
nkeynes@964
   271
{
nkeynes@964
   272
    dreamcast_reset();
nkeynes@964
   273
}
nkeynes@964
   274
- (void) run_action: (id)sender
nkeynes@964
   275
{
nkeynes@964
   276
    if( !dreamcast_is_running() ) {
nkeynes@964
   277
        cocoa_gui_run_later();
nkeynes@964
   278
    }
nkeynes@964
   279
}
nkeynes@964
   280
- (void) run_immediate
nkeynes@964
   281
{
nkeynes@964
   282
    dreamcast_run();
nkeynes@964
   283
    [mainWindow setRunning: NO];
nkeynes@964
   284
}
nkeynes@964
   285
- (void) gdrom_list_action: (id)sender
nkeynes@964
   286
{
nkeynes@964
   287
    gdrom_list_set_selection( [sender tag] );
nkeynes@964
   288
}
nkeynes@964
   289
@end
nkeynes@964
   290
nkeynes@964
   291
nkeynes@964
   292
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@964
   293
{
nkeynes@964
   294
    /* If started from the finder, the first (and only) arg will look something like 
nkeynes@964
   295
     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
nkeynes@964
   296
     * normally
nkeynes@964
   297
     */
nkeynes@964
   298
    if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
nkeynes@964
   299
        *argc = 1;
nkeynes@964
   300
    }
nkeynes@964
   301
    return TRUE;
nkeynes@964
   302
}
nkeynes@964
   303
nkeynes@964
   304
gboolean gui_init( gboolean withDebug )
nkeynes@964
   305
{
nkeynes@964
   306
    dreamcast_register_module( &cocoa_gui_module );
nkeynes@964
   307
nkeynes@964
   308
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
nkeynes@964
   309
    [NSApplication sharedApplication];
nkeynes@964
   310
nkeynes@964
   311
    LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
nkeynes@964
   312
    [NSApp setDelegate: delegate];
nkeynes@964
   313
    NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
nkeynes@964
   314
    NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
nkeynes@964
   315
    [iconImage setName: @"NSApplicationIcon"];
nkeynes@964
   316
    [NSApp setApplicationIconImage: iconImage];   
nkeynes@964
   317
    cocoa_gui_create_menu();
nkeynes@964
   318
    mainWindow = cocoa_gui_create_main_window();
nkeynes@964
   319
    [mainWindow makeKeyAndOrderFront: nil];
nkeynes@964
   320
    [NSApp activateIgnoringOtherApps: YES];   
nkeynes@964
   321
nkeynes@964
   322
    register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
nkeynes@964
   323
    
nkeynes@964
   324
    [pool release];
nkeynes@964
   325
    return TRUE;
nkeynes@964
   326
}
nkeynes@964
   327
nkeynes@964
   328
void gui_main_loop( gboolean run )
nkeynes@964
   329
{
nkeynes@964
   330
    if( run ) {
nkeynes@964
   331
        cocoa_gui_autorun = YES;
nkeynes@964
   332
    }
nkeynes@964
   333
    cocoa_gui_is_running = YES;
nkeynes@964
   334
    [NSApp run];
nkeynes@964
   335
    cocoa_gui_is_running = NO;
nkeynes@964
   336
}
nkeynes@964
   337
nkeynes@964
   338
void gui_update_state(void)
nkeynes@964
   339
{
nkeynes@964
   340
    cocoa_gui_update();
nkeynes@964
   341
}
nkeynes@964
   342
nkeynes@964
   343
void gui_set_use_grab( gboolean grab )
nkeynes@964
   344
{
nkeynes@964
   345
    [mainWindow setUseGrab: (grab ? YES : NO)];
nkeynes@964
   346
}
nkeynes@964
   347
nkeynes@964
   348
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@964
   349
{
nkeynes@964
   350
    if( cocoa_gui_is_running ) {
nkeynes@964
   351
        NSString *error_string;
nkeynes@964
   352
nkeynes@964
   353
        va_list args;
nkeynes@964
   354
        va_start(args, msg);
nkeynes@964
   355
        error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
nkeynes@964
   356
        NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
nkeynes@964
   357
        va_end(args);
nkeynes@964
   358
        return TRUE;
nkeynes@964
   359
    } else {
nkeynes@964
   360
        return FALSE;
nkeynes@964
   361
    }
nkeynes@964
   362
}
nkeynes@964
   363
nkeynes@964
   364
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@964
   365
{
nkeynes@964
   366
nkeynes@964
   367
}
nkeynes@964
   368
nkeynes@964
   369
nkeynes@964
   370
uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
nkeynes@964
   371
{
nkeynes@964
   372
    NSEvent *event;
nkeynes@964
   373
    NSAutoreleasePool *pool;
nkeynes@964
   374
nkeynes@964
   375
    cocoa_gui_nanos += nanosecs;
nkeynes@964
   376
    if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@964
   377
        cocoa_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@964
   378
        cocoa_gui_ticks ++;
nkeynes@964
   379
        uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
nkeynes@964
   380
nkeynes@964
   381
        // Run the event loop
nkeynes@964
   382
        pool = [NSAutoreleasePool new];
nkeynes@964
   383
        while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
nkeynes@964
   384
                         inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
nkeynes@964
   385
            [NSApp sendEvent: event];
nkeynes@964
   386
        }
nkeynes@964
   387
        [pool release];
nkeynes@964
   388
nkeynes@964
   389
        struct timeval tv;
nkeynes@964
   390
        gettimeofday(&tv,NULL);
nkeynes@964
   391
        uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@964
   392
        (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
nkeynes@964
   393
        if( (ns * 1.05) < current_period ) {
nkeynes@964
   394
            // We've gotten ahead - sleep for a little bit
nkeynes@964
   395
            struct timespec tv;
nkeynes@964
   396
            tv.tv_sec = 0;
nkeynes@964
   397
            tv.tv_nsec = current_period - ns;
nkeynes@964
   398
            nanosleep(&tv, &tv);
nkeynes@964
   399
        }
nkeynes@964
   400
nkeynes@964
   401
        /* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@964
   402
         * save the current tv value */
nkeynes@964
   403
        if( cocoa_gui_ticks > 10 ) {
nkeynes@964
   404
            gchar buf[32];
nkeynes@964
   405
            cocoa_gui_ticks -= 10;
nkeynes@964
   406
nkeynes@964
   407
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@964
   408
            cocoa_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@964
   409
            cocoa_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@964
   410
            snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
nkeynes@964
   411
            [mainWindow setStatusText: buf];
nkeynes@964
   412
nkeynes@964
   413
        }
nkeynes@964
   414
    }
nkeynes@964
   415
    return nanosecs;
nkeynes@964
   416
}
nkeynes@964
   417
nkeynes@964
   418
void cocoa_gui_update( void )
nkeynes@964
   419
{
nkeynes@964
   420
nkeynes@964
   421
}
nkeynes@964
   422
nkeynes@964
   423
void cocoa_gui_start( void )
nkeynes@964
   424
{
nkeynes@964
   425
    [mainWindow setRunning: YES];
nkeynes@964
   426
    cocoa_gui_nanos = 0;
nkeynes@964
   427
    gettimeofday(&cocoa_gui_lasttv,NULL);
nkeynes@964
   428
}
nkeynes@964
   429
nkeynes@964
   430
void cocoa_gui_stop( void )
nkeynes@964
   431
{
nkeynes@964
   432
    [mainWindow setRunning: NO];
nkeynes@964
   433
}
nkeynes@964
   434
nkeynes@964
   435
/**
nkeynes@964
   436
 * Queue a dreamcast_run() to execute after the currently event(s)
nkeynes@964
   437
 */
nkeynes@964
   438
void cocoa_gui_run_later( void )
nkeynes@964
   439
{
nkeynes@964
   440
    [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
nkeynes@964
   441
     target: [NSApp delegate] argument: nil order: 1 
nkeynes@964
   442
     modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
nkeynes@964
   443
}
nkeynes@964
   444
nkeynes@964
   445
/*************************** Convenience methods ***************************/
nkeynes@964
   446
nkeynes@964
   447
NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
nkeynes@964
   448
{
nkeynes@964
   449
    NSBitmapImageRep *rep = 
nkeynes@964
   450
        [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
nkeynes@964
   451
         pixelsWide: buffer->width  pixelsHigh: buffer->height
nkeynes@964
   452
         bitsPerSample: 8 samplesPerPixel: 3
nkeynes@964
   453
         hasAlpha: NO isPlanar: NO
nkeynes@964
   454
         colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
nkeynes@964
   455
         bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
nkeynes@964
   456
nkeynes@964
   457
    NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
nkeynes@964
   458
    [image addRepresentation: rep];
nkeynes@964
   459
    return image;
nkeynes@964
   460
}
nkeynes@964
   461
nkeynes@964
   462
nkeynes@964
   463
NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
nkeynes@964
   464
{
nkeynes@964
   465
    NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
nkeynes@964
   466
    [label setStringValue: text];
nkeynes@964
   467
    [label setBordered: NO];
nkeynes@964
   468
    [label setDrawsBackground: NO];
nkeynes@964
   469
    [label setEditable: NO];
nkeynes@964
   470
    [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
nkeynes@964
   471
    if( parent != NULL ) {
nkeynes@964
   472
        [parent addSubview: label];
nkeynes@964
   473
    }
nkeynes@964
   474
    return label;
nkeynes@964
   475
}
.