Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 685:fa1589b42be7
prev681:1755a126b109
next686:afd1bd3f1acc
author nkeynes
date Tue Jun 03 11:16:51 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Add alert dialog for errors
Handle the command line when launched from finder
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * Core Cocoa-based user interface
     5  *
     6  * Copyright (c) 2005 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 "dreamcast.h"
    26 #include "dream.h"
    27 #include "gui.h"
    28 #include "cocoaui/cocoaui.h"
    30 void cocoa_gui_update( void );
    31 void cocoa_gui_start( void );
    32 void cocoa_gui_stop( void );
    33 void cocoa_gui_run_later( void );
    34 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
    36 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
    37                   cocoa_gui_update, 
    38                   cocoa_gui_start, 
    39                   cocoa_gui_run_slice, 
    40                   cocoa_gui_stop, 
    41                   NULL, NULL };
    43 /**
    44  * Count of running nanoseconds - used to cut back on the GUI runtime
    45  */
    46 static uint32_t cocoa_gui_nanos = 0;
    47 static uint32_t cocoa_gui_ticks = 0;
    48 static struct timeval cocoa_gui_lasttv;
    49 static BOOL cocoa_gui_autorun = NO;
    51 @interface NSApplication (PrivateAdditions)
    52 - (void) setAppleMenu:(NSMenu *)aMenu;
    53 @end
    55 static void cocoa_gui_create_menu(void)
    56 {
    57     NSMenu *appleMenu, *services;
    58     NSMenuItem *menuItem;
    59     NSString *title;
    60     NSString *appName;
    62     appName = @"Lxdream";
    63     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    65     /* Add menu items */
    66     title = [@"About " stringByAppendingString:appName];
    67     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    68     [appleMenu addItem:[NSMenuItem separatorItem]];
    70     // Services Menu
    71     services = [[[NSMenu alloc] init] autorelease];
    72     [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
    73     [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
    75     // Hide AppName
    76     title = [@"Hide " stringByAppendingString:appName];
    77     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
    79     // Hide Others
    80     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
    81                                                   action:@selector(hideOtherApplications:) 
    82                                            keyEquivalent:@"h"];
    83     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    85     // Show All
    86     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
    87     [appleMenu addItem:[NSMenuItem separatorItem]];
    89     // Quit AppName
    90     title = [@"Quit " stringByAppendingString:appName];
    91     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
    93     /* Put menu into the menubar */
    94     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
    95     [menuItem setSubmenu: appleMenu];
    96     NSMenu *menu = [NSMenu new];
    97     [menu addItem: menuItem];
    99     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
   100     [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   101     [fileMenu addItemWithTitle: NS_("GD-Rom") action: @selector(mount_action:) keyEquivalent: @"g"];
   102     [fileMenu addItem: [NSMenuItem separatorItem]];
   103     [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   104       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   105     [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   106     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   107     [fileMenu addItem: [NSMenuItem separatorItem]];
   108     [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
   109     [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
   111     menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
   112     [menuItem setSubmenu: fileMenu];
   113     [menu addItem: menuItem];
   115     /* Tell the application object that this is now the application menu */
   116     [NSApp setMainMenu: menu];
   117     [NSApp setAppleMenu: appleMenu];
   118     [NSApp setServicesMenu: services];
   120     /* Finally give up our references to the objects */
   121     [appleMenu release];
   122     [menuItem release];
   123     [menu release];
   124 }
   126 @interface LxdreamDelegate : NSObject
   127 @end
   129 @implementation LxdreamDelegate
   130 - (void)windowWillClose: (NSNotification *)notice
   131 {
   132     dreamcast_shutdown();
   133     exit(0);
   134 }
   135 - (void)windowDidBecomeMain: (NSNotification *)notice
   136 {
   137     if( cocoa_gui_autorun ) {
   138         cocoa_gui_autorun = NO;
   139         cocoa_gui_run_later();
   140     }
   141 }
   142 - (void) load_action: (id)sender
   143 {
   144     NSOpenPanel *panel = [NSOpenPanel openPanel];
   145     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   146     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: fileTypes];
   147     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   148         NSString *filename = [[panel filenames] objectAtIndex: 0];
   149         dreamcast_load_state( [filename UTF8String] );
   150     }
   151 }
   152 - (void) save_action: (id)sender
   153 {
   154     NSSavePanel *panel = [NSSavePanel savePanel];
   155     [panel setRequiredFileType: @"dst"];
   156     int result = [panel runModalForDirectory: NSHomeDirectory() file:@""];
   157     if( result == NSOKButton ) {
   158         NSString *filename = [panel filename];
   159         dreamcast_save_state( [filename UTF8String] );
   160     }
   161 }
   162 - (void) load_binary_action: (id)sender
   163 {
   164     NSOpenPanel *panel = [NSOpenPanel openPanel];
   165     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   166     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   167         NSString *filename = [[panel filenames] objectAtIndex: 0];
   168         file_load_magic( [filename UTF8String] );
   169     }
   170 }
   171 - (void) mount_action: (id)sender
   172 {
   173     NSOpenPanel *panel = [NSOpenPanel openPanel];
   174     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   175     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   176         NSString *filename = [[panel filenames] objectAtIndex: 0];
   177         gdrom_mount_image( [filename UTF8String] );
   178     }
   179 }
   180 - (void) pause_action: (id)sender
   181 {
   182     dreamcast_stop();
   183 }
   185 - (void) reset_action: (id)sender
   186 {
   187     dreamcast_reset();
   188 }
   189 - (void) run_action: (id)sender
   190 {
   191     cocoa_gui_run_later();
   192 }
   193 - (void) run_immediate
   194 {
   195     dreamcast_run();
   196 }
   197 @end
   200 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   201 {
   202     /* If started from the finder, the first (and only) arg will look something like 
   203     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
   204     * normally
   205     */
   206     if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
   207         *argc = 1;
   208     }
   209     return TRUE;
   210 }
   212 gboolean gui_init( gboolean withDebug )
   213 {
   214     dreamcast_register_module( &cocoa_gui_module );
   216     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   217     [NSApplication sharedApplication];
   218     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   219     [NSApp setDelegate: delegate];
   220     cocoa_gui_create_menu();
   221     NSWindow *window = cocoa_gui_create_main_window();
   222     [window makeKeyAndOrderFront: nil];
   223     [NSApp activateIgnoringOtherApps: YES];   
   224     [pool release];
   225 }
   227 void gui_main_loop( gboolean run )
   228 {
   229     if( run ) {
   230         cocoa_gui_autorun = YES;
   231         /*
   232              */
   233     }
   234 	[NSApp run];
   235 }
   237 void gui_update_state(void)
   238 {
   239     cocoa_gui_update();
   240 }
   242 gboolean gui_error_dialog( const char *msg, ... )
   243 {
   244     NSString *error_string;
   246     va_list args;
   247     va_start(args, msg);
   248     error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
   249     NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
   250     va_end(args);
   251 }
   253 void gui_update_io_activity( io_activity_type io, gboolean active )
   254 {
   256 }
   259 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   260 {
   261     NSEvent *event;
   262     NSAutoreleasePool *pool;
   264     cocoa_gui_nanos += nanosecs;
   265     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   266         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   267         cocoa_gui_ticks ++;
   268         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   270         // Run the event loop
   271         pool = [NSAutoreleasePool new];
   272         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   273                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   274             [NSApp sendEvent: event];
   275         }
   276         [pool release];
   278         struct timeval tv;
   279         gettimeofday(&tv,NULL);
   280         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   281         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   282         if( (ns * 1.05) < current_period ) {
   283             // We've gotten ahead - sleep for a little bit
   284             struct timespec tv;
   285             tv.tv_sec = 0;
   286             tv.tv_nsec = current_period - ns;
   287             nanosleep(&tv, &tv);
   288         }
   290         /* Update the display every 10 ticks (ie 10 times a second) and 
   291          * save the current tv value */
   292         if( cocoa_gui_ticks > 10 ) {
   293             gchar buf[32];
   294             cocoa_gui_ticks -= 10;
   296             double speed = (float)( (double)current_period * 100.0 / ns );
   297             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   298             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   299             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   300             [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
   302         }
   303     }
   304     return nanosecs;
   305 }
   307 void cocoa_gui_update( void )
   308 {
   310 }
   312 void cocoa_gui_start( void )
   313 {
   314     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   315     [win setRunning: YES];
   316     cocoa_gui_nanos = 0;
   317     gettimeofday(&cocoa_gui_lasttv,NULL);
   318 }
   320 void cocoa_gui_stop( void )
   321 {
   322     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   323     [win setRunning: NO];
   324 }
   326 /**
   327  * Queue a dreamcast_run() to execute after the currently event(s)
   328  */
   329 void cocoa_gui_run_later( void )
   330 {
   331     [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
   332          target: [NSApp delegate] argument: nil order: 1 
   333          modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
.