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