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