Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.m
changeset 1040:9e3e41eab2db
prev1036:af7b0c5905dd
next1041:5fcc39857c5c
author nkeynes
date Thu Jun 25 21:21:18 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add quick state bits to the menus
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@1040
    93
    int i;
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@1040
   141
    NSMenu *quickStateMenu = [[NSMenu alloc] initWithTitle:NSMENU_("_Quick State")];
nkeynes@1040
   142
    int quickState = dreamcast_get_quick_state();
nkeynes@1040
   143
    for( i=0; i<=MAX_QUICK_STATE; i++ ) {
nkeynes@1040
   144
    	NSString *label = [NSString stringWithFormat: NSMENU_("State _%d"), i];
nkeynes@1040
   145
    	NSString *keyEquiv = [NSString stringWithFormat: @"%d", i];
nkeynes@1040
   146
    	menuItem = [[NSMenuItem alloc] initWithTitle: label action: @selector(quick_state_action:) keyEquivalent: keyEquiv];
nkeynes@1040
   147
    	[menuItem setTag: i];
nkeynes@1040
   148
    	if( i == quickState ) {
nkeynes@1040
   149
    	    [menuItem setState:NSOnState];
nkeynes@1040
   150
    	}
nkeynes@1040
   151
    	[quickStateMenu addItem: menuItem];
nkeynes@1040
   152
    }
nkeynes@1040
   153
nkeynes@964
   154
    NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
nkeynes@964
   155
    [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
nkeynes@964
   156
    [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
nkeynes@964
   157
      setSubmenu: gdromMenu];
nkeynes@964
   158
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@964
   159
    [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
nkeynes@964
   160
      setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
nkeynes@964
   161
    [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
nkeynes@964
   162
    [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
nkeynes@964
   163
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@1040
   164
    [fileMenu addItemWithTitle: NSMENU_("L_oad State...") action: @selector(load_action:) keyEquivalent: @"o"];
nkeynes@1040
   165
    [fileMenu addItemWithTitle: NSMENU_("S_ave State...") action: @selector(save_action:) keyEquivalent: @"a"];
nkeynes@1040
   166
    menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("Select _Quick State") action: nil keyEquivalent: @""];
nkeynes@1040
   167
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@1040
   168
    [fileMenu addItemWithTitle: NSMENU_("_Load Quick State") action: @selector(quick_load_action:) keyEquivalent: @"l"]; 
nkeynes@1040
   169
    [fileMenu addItemWithTitle: NSMENU_("_Save Quick State") action: @selector(quick_save_action:) keyEquivalent: @"s"];
nkeynes@1040
   170
    [menuItem setSubmenu: quickStateMenu];
nkeynes@1040
   171
    [fileMenu addItem: menuItem];
nkeynes@1028
   172
    [fileMenu addItem: [NSMenuItem separatorItem]];
nkeynes@1028
   173
    [fileMenu addItemWithTitle: NSMENU_("_Full Screen...") action: @selector(fullscreen_action:) keyEquivalent: @"\r"];
nkeynes@964
   174
nkeynes@964
   175
    menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
nkeynes@964
   176
    [menuItem setSubmenu: fileMenu];
nkeynes@964
   177
    [menu addItem: menuItem];
nkeynes@964
   178
nkeynes@964
   179
    /* Tell the application object that this is now the application menu */
nkeynes@964
   180
    [NSApp setMainMenu: menu];
nkeynes@964
   181
    [NSApp setAppleMenu: appleMenu];
nkeynes@964
   182
    [NSApp setServicesMenu: services];
nkeynes@964
   183
nkeynes@964
   184
    /* Finally give up our references to the objects */
nkeynes@964
   185
    [appleMenu release];
nkeynes@964
   186
    [menuItem release];
nkeynes@964
   187
    [menu release];
nkeynes@964
   188
}
nkeynes@964
   189
nkeynes@964
   190
@interface LxdreamDelegate : NSObject
nkeynes@964
   191
@end
nkeynes@964
   192
nkeynes@964
   193
@implementation LxdreamDelegate
nkeynes@964
   194
- (void)windowWillClose: (NSNotification *)notice
nkeynes@964
   195
{
nkeynes@964
   196
    dreamcast_shutdown();
nkeynes@964
   197
    exit(0);
nkeynes@964
   198
}
nkeynes@964
   199
- (void)windowDidBecomeMain: (NSNotification *)notice
nkeynes@964
   200
{
nkeynes@964
   201
    if( cocoa_gui_autorun ) {
nkeynes@964
   202
        cocoa_gui_autorun = NO;
nkeynes@1019
   203
        gui_do_later(dreamcast_run);
nkeynes@964
   204
    }
nkeynes@964
   205
}
nkeynes@964
   206
- (void)windowDidBecomeKey: (NSNotification *)notice
nkeynes@964
   207
{
nkeynes@964
   208
    display_set_focused( TRUE );
nkeynes@964
   209
}
nkeynes@964
   210
- (void)windowDidResignKey: (NSNotification *)notice
nkeynes@964
   211
{
nkeynes@964
   212
    display_set_focused( FALSE );
nkeynes@964
   213
    [mainWindow setIsGrabbed: NO];
nkeynes@964
   214
}
nkeynes@964
   215
- (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
nkeynes@964
   216
{
nkeynes@964
   217
    const gchar *cname = [filename UTF8String];
nkeynes@964
   218
    if( file_load_magic(cname) ) {
nkeynes@964
   219
        // Queue up a run event
nkeynes@1019
   220
        gui_do_later(dreamcast_run);
nkeynes@964
   221
        return YES;
nkeynes@964
   222
    } else {
nkeynes@964
   223
        return NO;
nkeynes@964
   224
    }
nkeynes@964
   225
    
nkeynes@964
   226
}
nkeynes@964
   227
- (void) about_action: (id)sender
nkeynes@964
   228
{
nkeynes@964
   229
    NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
nkeynes@964
   230
    NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright),  nil];
nkeynes@964
   231
nkeynes@964
   232
    NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
nkeynes@964
   233
nkeynes@964
   234
    [NSApp orderFrontStandardAboutPanelWithOptions: options];
nkeynes@964
   235
}
nkeynes@964
   236
- (void) preferences_action: (id)sender
nkeynes@964
   237
{
nkeynes@964
   238
    cocoa_gui_show_preferences();
nkeynes@964
   239
}
nkeynes@964
   240
- (void) load_action: (id)sender
nkeynes@964
   241
{
nkeynes@964
   242
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@1036
   243
    NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_SAVE_PATH)];
nkeynes@964
   244
    NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
nkeynes@964
   245
    int result = [panel runModalForDirectory: path file: nil types: fileTypes];
nkeynes@964
   246
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   247
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   248
        dreamcast_load_state( [filename UTF8String] );
nkeynes@1036
   249
        gui_set_configurable_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
nkeynes@964
   250
    }
nkeynes@964
   251
}
nkeynes@964
   252
- (void) save_action: (id)sender
nkeynes@964
   253
{
nkeynes@964
   254
    NSSavePanel *panel = [NSSavePanel savePanel];
nkeynes@1036
   255
    NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_SAVE_PATH)];
nkeynes@964
   256
    [panel setRequiredFileType: @"dst"];
nkeynes@964
   257
    int result = [panel runModalForDirectory: path file:@""];
nkeynes@964
   258
    if( result == NSOKButton ) {
nkeynes@964
   259
        NSString *filename = [panel filename];
nkeynes@964
   260
        dreamcast_save_state( [filename UTF8String] );
nkeynes@1036
   261
        gui_set_configurable_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
nkeynes@964
   262
    }
nkeynes@964
   263
}
nkeynes@964
   264
- (void) load_binary_action: (id)sender
nkeynes@964
   265
{
nkeynes@964
   266
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@1036
   267
    NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_DEFAULT_PATH)];
nkeynes@964
   268
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@964
   269
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   270
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   271
        file_load_magic( [filename UTF8String] );
nkeynes@1036
   272
        gui_set_configurable_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
nkeynes@964
   273
    }
nkeynes@964
   274
}
nkeynes@964
   275
- (void) mount_action: (id)sender
nkeynes@964
   276
{
nkeynes@964
   277
    NSOpenPanel *panel = [NSOpenPanel openPanel];
nkeynes@1036
   278
    NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_DEFAULT_PATH)];
nkeynes@964
   279
    int result = [panel runModalForDirectory: path file: nil types: nil];
nkeynes@964
   280
    if( result == NSOKButton && [[panel filenames] count] > 0 ) {
nkeynes@964
   281
        NSString *filename = [[panel filenames] objectAtIndex: 0];
nkeynes@964
   282
        gdrom_mount_image( [filename UTF8String] );
nkeynes@1036
   283
        gui_set_configurable_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
nkeynes@964
   284
    }
nkeynes@964
   285
}
nkeynes@964
   286
- (void) pause_action: (id)sender
nkeynes@964
   287
{
nkeynes@964
   288
    dreamcast_stop();
nkeynes@964
   289
}
nkeynes@964
   290
nkeynes@964
   291
- (void) reset_action: (id)sender
nkeynes@964
   292
{
nkeynes@964
   293
    dreamcast_reset();
nkeynes@964
   294
}
nkeynes@964
   295
- (void) run_action: (id)sender
nkeynes@964
   296
{
nkeynes@964
   297
    if( !dreamcast_is_running() ) {
nkeynes@1019
   298
        gui_do_later(dreamcast_run);
nkeynes@964
   299
    }
nkeynes@964
   300
}
nkeynes@964
   301
- (void) gdrom_list_action: (id)sender
nkeynes@964
   302
{
nkeynes@964
   303
    gdrom_list_set_selection( [sender tag] );
nkeynes@964
   304
}
nkeynes@1028
   305
- (void) fullscreen_action: (id)sender
nkeynes@1028
   306
{
nkeynes@1028
   307
    [mainWindow setFullscreen: ![mainWindow isFullscreen]]; 
nkeynes@1028
   308
}
nkeynes@1040
   309
- (void) quick_state_action: (id)sender
nkeynes@1040
   310
{
nkeynes@1040
   311
    [[[sender menu] itemWithTag: dreamcast_get_quick_state()] setState: NSOffState ];
nkeynes@1040
   312
    [sender setState: NSOnState ];
nkeynes@1040
   313
    dreamcast_set_quick_state( [sender tag] );
nkeynes@1040
   314
}
nkeynes@1040
   315
- (void) quick_save_action: (id)sender
nkeynes@1040
   316
{
nkeynes@1040
   317
    dreamcast_quick_save();
nkeynes@1040
   318
}
nkeynes@1040
   319
- (void) quick_load_action: (id)sender
nkeynes@1040
   320
{
nkeynes@1040
   321
    dreamcast_quick_load();
nkeynes@1040
   322
}
nkeynes@964
   323
@end
nkeynes@964
   324
nkeynes@964
   325
nkeynes@964
   326
gboolean gui_parse_cmdline( int *argc, char **argv[] )
nkeynes@964
   327
{
nkeynes@964
   328
    /* If started from the finder, the first (and only) arg will look something like 
nkeynes@964
   329
     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
nkeynes@964
   330
     * normally
nkeynes@964
   331
     */
nkeynes@964
   332
    if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
nkeynes@964
   333
        *argc = 1;
nkeynes@964
   334
    }
nkeynes@964
   335
    return TRUE;
nkeynes@964
   336
}
nkeynes@964
   337
nkeynes@1015
   338
gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
nkeynes@964
   339
{
nkeynes@964
   340
    dreamcast_register_module( &cocoa_gui_module );
nkeynes@964
   341
nkeynes@964
   342
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
nkeynes@964
   343
    [NSApplication sharedApplication];
nkeynes@964
   344
nkeynes@964
   345
    LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
nkeynes@964
   346
    [NSApp setDelegate: delegate];
nkeynes@1017
   347
    NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"lxdream" ofType:@"png"];
nkeynes@964
   348
    NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
nkeynes@964
   349
    [iconImage setName: @"NSApplicationIcon"];
nkeynes@964
   350
    [NSApp setApplicationIconImage: iconImage];   
nkeynes@964
   351
    cocoa_gui_create_menu();
nkeynes@964
   352
    mainWindow = cocoa_gui_create_main_window();
nkeynes@964
   353
    [mainWindow makeKeyAndOrderFront: nil];
nkeynes@964
   354
    [NSApp activateIgnoringOtherApps: YES];   
nkeynes@964
   355
nkeynes@964
   356
    register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
nkeynes@1028
   357
    if( withFullscreen ) {
nkeynes@1028
   358
    	[mainWindow setFullscreen: YES];
nkeynes@1028
   359
    }
nkeynes@964
   360
    [pool release];
nkeynes@964
   361
    return TRUE;
nkeynes@964
   362
}
nkeynes@964
   363
nkeynes@964
   364
void gui_main_loop( gboolean run )
nkeynes@964
   365
{
nkeynes@964
   366
    if( run ) {
nkeynes@964
   367
        cocoa_gui_autorun = YES;
nkeynes@964
   368
    }
nkeynes@964
   369
    cocoa_gui_is_running = YES;
nkeynes@964
   370
    [NSApp run];
nkeynes@964
   371
    cocoa_gui_is_running = NO;
nkeynes@964
   372
}
nkeynes@964
   373
nkeynes@964
   374
void gui_update_state(void)
nkeynes@964
   375
{
nkeynes@964
   376
    cocoa_gui_update();
nkeynes@964
   377
}
nkeynes@964
   378
nkeynes@964
   379
void gui_set_use_grab( gboolean grab )
nkeynes@964
   380
{
nkeynes@964
   381
    [mainWindow setUseGrab: (grab ? YES : NO)];
nkeynes@964
   382
}
nkeynes@964
   383
nkeynes@964
   384
gboolean gui_error_dialog( const char *msg, ... )
nkeynes@964
   385
{
nkeynes@964
   386
    if( cocoa_gui_is_running ) {
nkeynes@964
   387
        NSString *error_string;
nkeynes@964
   388
nkeynes@964
   389
        va_list args;
nkeynes@964
   390
        va_start(args, msg);
nkeynes@964
   391
        error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
nkeynes@964
   392
        NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
nkeynes@964
   393
        va_end(args);
nkeynes@964
   394
        return TRUE;
nkeynes@964
   395
    } else {
nkeynes@964
   396
        return FALSE;
nkeynes@964
   397
    }
nkeynes@964
   398
}
nkeynes@964
   399
nkeynes@964
   400
void gui_update_io_activity( io_activity_type io, gboolean active )
nkeynes@964
   401
{
nkeynes@964
   402
nkeynes@964
   403
}
nkeynes@964
   404
nkeynes@964
   405
nkeynes@964
   406
uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
nkeynes@964
   407
{
nkeynes@964
   408
    NSEvent *event;
nkeynes@964
   409
    NSAutoreleasePool *pool;
nkeynes@964
   410
nkeynes@964
   411
    cocoa_gui_nanos += nanosecs;
nkeynes@964
   412
    if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
nkeynes@964
   413
        cocoa_gui_nanos -= GUI_TICK_PERIOD;
nkeynes@964
   414
        cocoa_gui_ticks ++;
nkeynes@964
   415
        uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
nkeynes@964
   416
nkeynes@964
   417
        // Run the event loop
nkeynes@964
   418
        pool = [NSAutoreleasePool new];
nkeynes@964
   419
        while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
nkeynes@964
   420
                         inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
nkeynes@964
   421
            [NSApp sendEvent: event];
nkeynes@964
   422
        }
nkeynes@964
   423
        [pool release];
nkeynes@964
   424
nkeynes@964
   425
        struct timeval tv;
nkeynes@964
   426
        gettimeofday(&tv,NULL);
nkeynes@964
   427
        uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
nkeynes@964
   428
        (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
nkeynes@964
   429
        if( (ns * 1.05) < current_period ) {
nkeynes@964
   430
            // We've gotten ahead - sleep for a little bit
nkeynes@964
   431
            struct timespec tv;
nkeynes@964
   432
            tv.tv_sec = 0;
nkeynes@964
   433
            tv.tv_nsec = current_period - ns;
nkeynes@964
   434
            nanosleep(&tv, &tv);
nkeynes@964
   435
        }
nkeynes@964
   436
nkeynes@964
   437
        /* Update the display every 10 ticks (ie 10 times a second) and 
nkeynes@964
   438
         * save the current tv value */
nkeynes@964
   439
        if( cocoa_gui_ticks > 10 ) {
nkeynes@964
   440
            gchar buf[32];
nkeynes@964
   441
            cocoa_gui_ticks -= 10;
nkeynes@964
   442
nkeynes@964
   443
            double speed = (float)( (double)current_period * 100.0 / ns );
nkeynes@964
   444
            cocoa_gui_lasttv.tv_sec = tv.tv_sec;
nkeynes@964
   445
            cocoa_gui_lasttv.tv_usec = tv.tv_usec;
nkeynes@964
   446
            snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
nkeynes@964
   447
            [mainWindow setStatusText: buf];
nkeynes@964
   448
nkeynes@964
   449
        }
nkeynes@964
   450
    }
nkeynes@964
   451
    return nanosecs;
nkeynes@964
   452
}
nkeynes@964
   453
nkeynes@964
   454
void cocoa_gui_update( void )
nkeynes@964
   455
{
nkeynes@964
   456
nkeynes@964
   457
}
nkeynes@964
   458
nkeynes@964
   459
void cocoa_gui_start( void )
nkeynes@964
   460
{
nkeynes@964
   461
    [mainWindow setRunning: YES];
nkeynes@964
   462
    cocoa_gui_nanos = 0;
nkeynes@964
   463
    gettimeofday(&cocoa_gui_lasttv,NULL);
nkeynes@964
   464
}
nkeynes@964
   465
nkeynes@964
   466
void cocoa_gui_stop( void )
nkeynes@964
   467
{
nkeynes@964
   468
    [mainWindow setRunning: NO];
nkeynes@964
   469
}
nkeynes@964
   470
nkeynes@1019
   471
@interface DoLaterStub : NSObject
nkeynes@1019
   472
{
nkeynes@1019
   473
    do_later_callback_t func;
nkeynes@1019
   474
}
nkeynes@1019
   475
@end    
nkeynes@1019
   476
nkeynes@1019
   477
@implementation DoLaterStub
nkeynes@1019
   478
- (id) init: (do_later_callback_t)f
nkeynes@1019
   479
{
nkeynes@1019
   480
    [super init];
nkeynes@1019
   481
    func = f;
nkeynes@1019
   482
    return self;
nkeynes@1019
   483
}
nkeynes@1019
   484
- (void) do
nkeynes@1019
   485
{
nkeynes@1019
   486
    func();
nkeynes@1019
   487
}
nkeynes@1019
   488
@end
nkeynes@1019
   489
nkeynes@964
   490
/**
nkeynes@964
   491
 * Queue a dreamcast_run() to execute after the currently event(s)
nkeynes@964
   492
 */
nkeynes@1019
   493
void gui_do_later( do_later_callback_t func )
nkeynes@964
   494
{
nkeynes@1019
   495
    DoLaterStub *stub = [[[DoLaterStub alloc] init: func] autorelease]; 
nkeynes@1019
   496
    [[NSRunLoop currentRunLoop] performSelector: @selector(do) 
nkeynes@1019
   497
     target: stub argument: nil order: 1 
nkeynes@964
   498
     modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
nkeynes@964
   499
}
nkeynes@964
   500
nkeynes@964
   501
/*************************** Convenience methods ***************************/
nkeynes@964
   502
nkeynes@964
   503
NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
nkeynes@964
   504
{
nkeynes@964
   505
    NSBitmapImageRep *rep = 
nkeynes@964
   506
        [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
nkeynes@964
   507
         pixelsWide: buffer->width  pixelsHigh: buffer->height
nkeynes@964
   508
         bitsPerSample: 8 samplesPerPixel: 3
nkeynes@964
   509
         hasAlpha: NO isPlanar: NO
nkeynes@964
   510
         colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
nkeynes@964
   511
         bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
nkeynes@964
   512
nkeynes@964
   513
    NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
nkeynes@964
   514
    [image addRepresentation: rep];
nkeynes@964
   515
    return image;
nkeynes@964
   516
}
nkeynes@964
   517
nkeynes@964
   518
nkeynes@964
   519
NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
nkeynes@964
   520
{
nkeynes@964
   521
    NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
nkeynes@964
   522
    [label setStringValue: text];
nkeynes@964
   523
    [label setBordered: NO];
nkeynes@964
   524
    [label setDrawsBackground: NO];
nkeynes@964
   525
    [label setEditable: NO];
nkeynes@964
   526
    [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
nkeynes@964
   527
    if( parent != NULL ) {
nkeynes@964
   528
        [parent addSubview: label];
nkeynes@964
   529
    }
nkeynes@964
   530
    return label;
nkeynes@964
   531
}
.