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