Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 693:b16608c4ad1e
prev691:ad3356543392
next718:047a244936d1
author nkeynes
date Fri Jun 20 05:43:34 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Cocoa: Don't try to display the error dialog if the gui isn't running
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;
    50 static BOOL cocoa_gui_is_running = NO;
    52 @interface NSApplication (PrivateAdditions)
    53 - (void) setAppleMenu:(NSMenu *)aMenu;
    54 @end
    56 static void cocoa_gui_create_menu(void)
    57 {
    58     NSMenu *appleMenu, *services;
    59     NSMenuItem *menuItem;
    60     NSString *title;
    61     NSString *appName;
    63     appName = @"Lxdream";
    64     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    66     /* Add menu items */
    67     title = [@"About " stringByAppendingString:appName];
    68     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    69     [appleMenu addItem:[NSMenuItem separatorItem]];
    71     // Services Menu
    72     services = [[[NSMenu alloc] init] autorelease];
    73     [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
    74     [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
    76     // Hide AppName
    77     title = [@"Hide " stringByAppendingString:appName];
    78     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
    80     // Hide Others
    81     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
    82                                                   action:@selector(hideOtherApplications:) 
    83                                            keyEquivalent:@"h"];
    84     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    86     // Show All
    87     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
    88     [appleMenu addItem:[NSMenuItem separatorItem]];
    90     // Quit AppName
    91     title = [@"Quit " stringByAppendingString:appName];
    92     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
    94     /* Put menu into the menubar */
    95     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
    96     [menuItem setSubmenu: appleMenu];
    97     NSMenu *menu = [NSMenu new];
    98     [menu addItem: menuItem];
   100     NSMenu *gdromMenu = cocoa_gdrom_menu_new();
   102     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
   103     [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   104     [[fileMenu addItemWithTitle: NS_("GD-Rom") action: nil keyEquivalent: @""]
   105       setSubmenu: gdromMenu];
   106     [fileMenu addItem: [NSMenuItem separatorItem]];
   107     [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   108       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   109     [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   110     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   111     [fileMenu addItem: [NSMenuItem separatorItem]];
   112     [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
   113     [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
   115     menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
   116     [menuItem setSubmenu: fileMenu];
   117     [menu addItem: menuItem];
   119     /* Tell the application object that this is now the application menu */
   120     [NSApp setMainMenu: menu];
   121     [NSApp setAppleMenu: appleMenu];
   122     [NSApp setServicesMenu: services];
   124     /* Finally give up our references to the objects */
   125     [appleMenu release];
   126     [menuItem release];
   127     [menu release];
   128 }
   130 @interface LxdreamDelegate : NSObject
   131 @end
   133 @implementation LxdreamDelegate
   134 - (void)windowWillClose: (NSNotification *)notice
   135 {
   136     dreamcast_shutdown();
   137     exit(0);
   138 }
   139 - (void)windowDidBecomeMain: (NSNotification *)notice
   140 {
   141     if( cocoa_gui_autorun ) {
   142         cocoa_gui_autorun = NO;
   143         cocoa_gui_run_later();
   144     }
   145 }
   146 - (void) load_action: (id)sender
   147 {
   148     NSOpenPanel *panel = [NSOpenPanel openPanel];
   149     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   150     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: fileTypes];
   151     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   152         NSString *filename = [[panel filenames] objectAtIndex: 0];
   153         dreamcast_load_state( [filename UTF8String] );
   154     }
   155 }
   156 - (void) save_action: (id)sender
   157 {
   158     NSSavePanel *panel = [NSSavePanel savePanel];
   159     [panel setRequiredFileType: @"dst"];
   160     int result = [panel runModalForDirectory: NSHomeDirectory() file:@""];
   161     if( result == NSOKButton ) {
   162         NSString *filename = [panel filename];
   163         dreamcast_save_state( [filename UTF8String] );
   164     }
   165 }
   166 - (void) load_binary_action: (id)sender
   167 {
   168     NSOpenPanel *panel = [NSOpenPanel openPanel];
   169     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   170     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   171         NSString *filename = [[panel filenames] objectAtIndex: 0];
   172         file_load_magic( [filename UTF8String] );
   173     }
   174 }
   175 - (void) mount_action: (id)sender
   176 {
   177     NSOpenPanel *panel = [NSOpenPanel openPanel];
   178     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   179     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   180         NSString *filename = [[panel filenames] objectAtIndex: 0];
   181         gdrom_mount_image( [filename UTF8String] );
   182     }
   183 }
   184 - (void) pause_action: (id)sender
   185 {
   186     dreamcast_stop();
   187 }
   189 - (void) reset_action: (id)sender
   190 {
   191     dreamcast_reset();
   192 }
   193 - (void) run_action: (id)sender
   194 {
   195     cocoa_gui_run_later();
   196 }
   197 - (void) run_immediate
   198 {
   199     dreamcast_run();
   200 }
   201 - (void) gdrom_list_action: (id)sender
   202 {
   203     gdrom_list_set_selection( [sender tag] );
   204 }
   205 @end
   208 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   209 {
   210     /* If started from the finder, the first (and only) arg will look something like 
   211     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
   212     * normally
   213     */
   214     if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
   215         *argc = 1;
   216     }
   217     return TRUE;
   218 }
   220 gboolean gui_init( gboolean withDebug )
   221 {
   222     dreamcast_register_module( &cocoa_gui_module );
   224     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   225     [NSApplication sharedApplication];
   226     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   227     [NSApp setDelegate: delegate];
   228     NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
   229     NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
   230     [NSApp setApplicationIconImage: iconImage];   
   231     cocoa_gui_create_menu();
   232     NSWindow *window = cocoa_gui_create_main_window();
   233     [window makeKeyAndOrderFront: nil];
   234     [NSApp activateIgnoringOtherApps: YES];   
   235     [pool release];
   236 }
   238 void gui_main_loop( gboolean run )
   239 {
   240     if( run ) {
   241         cocoa_gui_autorun = YES;
   242     }
   243     cocoa_gui_is_running = YES;
   244 	[NSApp run];
   245 	cocoa_gui_is_running = NO;
   246 }
   248 void gui_update_state(void)
   249 {
   250     cocoa_gui_update();
   251 }
   253 gboolean gui_error_dialog( const char *msg, ... )
   254 {
   255     if( cocoa_gui_is_running ) {
   256         NSString *error_string;
   258         va_list args;
   259         va_start(args, msg);
   260         error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
   261         NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
   262         va_end(args);
   263         return TRUE;
   264     } else {
   265         return FALSE;
   266     }
   267 }
   269 void gui_update_io_activity( io_activity_type io, gboolean active )
   270 {
   272 }
   275 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   276 {
   277     NSEvent *event;
   278     NSAutoreleasePool *pool;
   280     cocoa_gui_nanos += nanosecs;
   281     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   282         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   283         cocoa_gui_ticks ++;
   284         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   286         // Run the event loop
   287         pool = [NSAutoreleasePool new];
   288         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   289                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   290             [NSApp sendEvent: event];
   291         }
   292         [pool release];
   294         struct timeval tv;
   295         gettimeofday(&tv,NULL);
   296         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   297         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   298         if( (ns * 1.05) < current_period ) {
   299             // We've gotten ahead - sleep for a little bit
   300             struct timespec tv;
   301             tv.tv_sec = 0;
   302             tv.tv_nsec = current_period - ns;
   303             nanosleep(&tv, &tv);
   304         }
   306         /* Update the display every 10 ticks (ie 10 times a second) and 
   307          * save the current tv value */
   308         if( cocoa_gui_ticks > 10 ) {
   309             gchar buf[32];
   310             cocoa_gui_ticks -= 10;
   312             double speed = (float)( (double)current_period * 100.0 / ns );
   313             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   314             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   315             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   316             [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
   318         }
   319     }
   320     return nanosecs;
   321 }
   323 void cocoa_gui_update( void )
   324 {
   326 }
   328 void cocoa_gui_start( void )
   329 {
   330     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   331     [win setRunning: YES];
   332     cocoa_gui_nanos = 0;
   333     gettimeofday(&cocoa_gui_lasttv,NULL);
   334 }
   336 void cocoa_gui_stop( void )
   337 {
   338     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   339     [win setRunning: NO];
   340 }
   342 /**
   343  * Queue a dreamcast_run() to execute after the currently event(s)
   344  */
   345 void cocoa_gui_run_later( void )
   346 {
   347     [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
   348          target: [NSApp delegate] argument: nil order: 1 
   349          modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
.