Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 691:ad3356543392
prev686:afd1bd3f1acc
next693:b16608c4ad1e
author nkeynes
date Thu Jun 19 04:40:37 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Refactor the gd-rom list management out of the GUI (devices, recent files, etc).
Add gd-rom list to the cocoa UI.
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 *gdromMenu = cocoa_gdrom_menu_new();
   101     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
   102     [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   103     [[fileMenu addItemWithTitle: NS_("GD-Rom") action: nil keyEquivalent: @""]
   104       setSubmenu: gdromMenu];
   105     [fileMenu addItem: [NSMenuItem separatorItem]];
   106     [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   107       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   108     [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   109     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   110     [fileMenu addItem: [NSMenuItem separatorItem]];
   111     [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
   112     [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
   114     menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
   115     [menuItem setSubmenu: fileMenu];
   116     [menu addItem: menuItem];
   118     /* Tell the application object that this is now the application menu */
   119     [NSApp setMainMenu: menu];
   120     [NSApp setAppleMenu: appleMenu];
   121     [NSApp setServicesMenu: services];
   123     /* Finally give up our references to the objects */
   124     [appleMenu release];
   125     [menuItem release];
   126     [menu release];
   127 }
   129 @interface LxdreamDelegate : NSObject
   130 @end
   132 @implementation LxdreamDelegate
   133 - (void)windowWillClose: (NSNotification *)notice
   134 {
   135     dreamcast_shutdown();
   136     exit(0);
   137 }
   138 - (void)windowDidBecomeMain: (NSNotification *)notice
   139 {
   140     if( cocoa_gui_autorun ) {
   141         cocoa_gui_autorun = NO;
   142         cocoa_gui_run_later();
   143     }
   144 }
   145 - (void) load_action: (id)sender
   146 {
   147     NSOpenPanel *panel = [NSOpenPanel openPanel];
   148     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   149     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: fileTypes];
   150     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   151         NSString *filename = [[panel filenames] objectAtIndex: 0];
   152         dreamcast_load_state( [filename UTF8String] );
   153     }
   154 }
   155 - (void) save_action: (id)sender
   156 {
   157     NSSavePanel *panel = [NSSavePanel savePanel];
   158     [panel setRequiredFileType: @"dst"];
   159     int result = [panel runModalForDirectory: NSHomeDirectory() file:@""];
   160     if( result == NSOKButton ) {
   161         NSString *filename = [panel filename];
   162         dreamcast_save_state( [filename UTF8String] );
   163     }
   164 }
   165 - (void) load_binary_action: (id)sender
   166 {
   167     NSOpenPanel *panel = [NSOpenPanel openPanel];
   168     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   169     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   170         NSString *filename = [[panel filenames] objectAtIndex: 0];
   171         file_load_magic( [filename UTF8String] );
   172     }
   173 }
   174 - (void) mount_action: (id)sender
   175 {
   176     NSOpenPanel *panel = [NSOpenPanel openPanel];
   177     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   178     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   179         NSString *filename = [[panel filenames] objectAtIndex: 0];
   180         gdrom_mount_image( [filename UTF8String] );
   181     }
   182 }
   183 - (void) pause_action: (id)sender
   184 {
   185     dreamcast_stop();
   186 }
   188 - (void) reset_action: (id)sender
   189 {
   190     dreamcast_reset();
   191 }
   192 - (void) run_action: (id)sender
   193 {
   194     cocoa_gui_run_later();
   195 }
   196 - (void) run_immediate
   197 {
   198     dreamcast_run();
   199 }
   200 - (void) gdrom_list_action: (id)sender
   201 {
   202     gdrom_list_set_selection( [sender tag] );
   203 }
   204 @end
   207 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   208 {
   209     /* If started from the finder, the first (and only) arg will look something like 
   210     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
   211     * normally
   212     */
   213     if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
   214         *argc = 1;
   215     }
   216     return TRUE;
   217 }
   219 gboolean gui_init( gboolean withDebug )
   220 {
   221     dreamcast_register_module( &cocoa_gui_module );
   223     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   224     [NSApplication sharedApplication];
   225     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   226     [NSApp setDelegate: delegate];
   227     NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
   228     NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
   229     [NSApp setApplicationIconImage: iconImage];   
   230     cocoa_gui_create_menu();
   231     NSWindow *window = cocoa_gui_create_main_window();
   232     [window makeKeyAndOrderFront: nil];
   233     [NSApp activateIgnoringOtherApps: YES];   
   234     [pool release];
   235 }
   237 void gui_main_loop( gboolean run )
   238 {
   239     if( run ) {
   240         cocoa_gui_autorun = YES;
   241         /*
   242              */
   243     }
   244 	[NSApp run];
   245 }
   247 void gui_update_state(void)
   248 {
   249     cocoa_gui_update();
   250 }
   252 gboolean gui_error_dialog( const char *msg, ... )
   253 {
   254     NSString *error_string;
   256     va_list args;
   257     va_start(args, msg);
   258     error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
   259     NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
   260     va_end(args);
   261 }
   263 void gui_update_io_activity( io_activity_type io, gboolean active )
   264 {
   266 }
   269 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   270 {
   271     NSEvent *event;
   272     NSAutoreleasePool *pool;
   274     cocoa_gui_nanos += nanosecs;
   275     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   276         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   277         cocoa_gui_ticks ++;
   278         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   280         // Run the event loop
   281         pool = [NSAutoreleasePool new];
   282         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   283                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   284             [NSApp sendEvent: event];
   285         }
   286         [pool release];
   288         struct timeval tv;
   289         gettimeofday(&tv,NULL);
   290         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   291         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   292         if( (ns * 1.05) < current_period ) {
   293             // We've gotten ahead - sleep for a little bit
   294             struct timespec tv;
   295             tv.tv_sec = 0;
   296             tv.tv_nsec = current_period - ns;
   297             nanosleep(&tv, &tv);
   298         }
   300         /* Update the display every 10 ticks (ie 10 times a second) and 
   301          * save the current tv value */
   302         if( cocoa_gui_ticks > 10 ) {
   303             gchar buf[32];
   304             cocoa_gui_ticks -= 10;
   306             double speed = (float)( (double)current_period * 100.0 / ns );
   307             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   308             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   309             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   310             [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
   312         }
   313     }
   314     return nanosecs;
   315 }
   317 void cocoa_gui_update( void )
   318 {
   320 }
   322 void cocoa_gui_start( void )
   323 {
   324     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   325     [win setRunning: YES];
   326     cocoa_gui_nanos = 0;
   327     gettimeofday(&cocoa_gui_lasttv,NULL);
   328 }
   330 void cocoa_gui_stop( void )
   331 {
   332     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   333     [win setRunning: NO];
   334 }
   336 /**
   337  * Queue a dreamcast_run() to execute after the currently event(s)
   338  */
   339 void cocoa_gui_run_later( void )
   340 {
   341     [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
   342          target: [NSApp delegate] argument: nil order: 1 
   343          modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
.