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