Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 718:047a244936d1
prev693:b16608c4ad1e
next725:4d4018e8eeb8
author nkeynes
date Mon Jun 30 04:58:26 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Use the configured paths for the dialog boxes by default
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 "dream.h"
    26 #include "dreamcast.h"
    27 #include "config.h"
    28 #include "display.h"
    29 #include "gui.h"
    30 #include "cocoaui/cocoaui.h"
    32 void cocoa_gui_update( void );
    33 void cocoa_gui_start( void );
    34 void cocoa_gui_stop( void );
    35 void cocoa_gui_run_later( void );
    36 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
    38 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
    39                   cocoa_gui_update, 
    40                   cocoa_gui_start, 
    41                   cocoa_gui_run_slice, 
    42                   cocoa_gui_stop, 
    43                   NULL, NULL };
    45 /**
    46  * Count of running nanoseconds - used to cut back on the GUI runtime
    47  */
    48 static uint32_t cocoa_gui_nanos = 0;
    49 static uint32_t cocoa_gui_ticks = 0;
    50 static struct timeval cocoa_gui_lasttv;
    51 static BOOL cocoa_gui_autorun = NO;
    52 static BOOL cocoa_gui_is_running = NO;
    54 @interface NSApplication (PrivateAdditions)
    55 - (void) setAppleMenu:(NSMenu *)aMenu;
    56 @end
    58 static void cocoa_gui_create_menu(void)
    59 {
    60     NSMenu *appleMenu, *services;
    61     NSMenuItem *menuItem;
    62     NSString *title;
    63     NSString *appName;
    65     appName = @"Lxdream";
    66     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    68     /* Add menu items */
    69     title = [@"About " stringByAppendingString:appName];
    70     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    71     [appleMenu addItem:[NSMenuItem separatorItem]];
    73     // Services Menu
    74     services = [[[NSMenu alloc] init] autorelease];
    75     [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
    76     [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
    78     // Hide AppName
    79     title = [@"Hide " stringByAppendingString:appName];
    80     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
    82     // Hide Others
    83     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
    84                                                   action:@selector(hideOtherApplications:) 
    85                                            keyEquivalent:@"h"];
    86     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    88     // Show All
    89     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
    90     [appleMenu addItem:[NSMenuItem separatorItem]];
    92     // Quit AppName
    93     title = [@"Quit " stringByAppendingString:appName];
    94     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
    96     /* Put menu into the menubar */
    97     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
    98     [menuItem setSubmenu: appleMenu];
    99     NSMenu *menu = [NSMenu new];
   100     [menu addItem: menuItem];
   102     NSMenu *gdromMenu = cocoa_gdrom_menu_new();
   104     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
   105     [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   106     [[fileMenu addItemWithTitle: NS_("GD-Rom") action: nil keyEquivalent: @""]
   107       setSubmenu: gdromMenu];
   108     [fileMenu addItem: [NSMenuItem separatorItem]];
   109     [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   110       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   111     [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   112     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   113     [fileMenu addItem: [NSMenuItem separatorItem]];
   114     [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
   115     [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
   117     menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
   118     [menuItem setSubmenu: fileMenu];
   119     [menu addItem: menuItem];
   121     /* Tell the application object that this is now the application menu */
   122     [NSApp setMainMenu: menu];
   123     [NSApp setAppleMenu: appleMenu];
   124     [NSApp setServicesMenu: services];
   126     /* Finally give up our references to the objects */
   127     [appleMenu release];
   128     [menuItem release];
   129     [menu release];
   130 }
   132 @interface LxdreamDelegate : NSObject
   133 @end
   135 @implementation LxdreamDelegate
   136 - (void)windowWillClose: (NSNotification *)notice
   137 {
   138     dreamcast_shutdown();
   139     exit(0);
   140 }
   141 - (void)windowDidBecomeMain: (NSNotification *)notice
   142 {
   143     if( cocoa_gui_autorun ) {
   144         cocoa_gui_autorun = NO;
   145         cocoa_gui_run_later();
   146     }
   147 }
   148 - (void) load_action: (id)sender
   149 {
   150     NSOpenPanel *panel = [NSOpenPanel openPanel];
   151     const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
   152     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   153     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   154     int result = [panel runModalForDirectory: path file: nil types: fileTypes];
   155     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   156         NSString *filename = [[panel filenames] objectAtIndex: 0];
   157         dreamcast_load_state( [filename UTF8String] );
   158     }
   159 }
   160 - (void) save_action: (id)sender
   161 {
   162     NSSavePanel *panel = [NSSavePanel savePanel];
   163     const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
   164     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   165     [panel setRequiredFileType: @"dst"];
   166     int result = [panel runModalForDirectory: path file:@""];
   167     if( result == NSOKButton ) {
   168         NSString *filename = [panel filename];
   169         dreamcast_save_state( [filename UTF8String] );
   170     }
   171 }
   172 - (void) load_binary_action: (id)sender
   173 {
   174     NSOpenPanel *panel = [NSOpenPanel openPanel];
   175     const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   176     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   177     int result = [panel runModalForDirectory: path file: nil types: nil];
   178     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   179         NSString *filename = [[panel filenames] objectAtIndex: 0];
   180         file_load_magic( [filename UTF8String] );
   181     }
   182 }
   183 - (void) mount_action: (id)sender
   184 {
   185     NSOpenPanel *panel = [NSOpenPanel openPanel];
   186     const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   187     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   188     int result = [panel runModalForDirectory: path file: nil types: nil];
   189     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   190         NSString *filename = [[panel filenames] objectAtIndex: 0];
   191         gdrom_mount_image( [filename UTF8String] );
   192     }
   193 }
   194 - (void) pause_action: (id)sender
   195 {
   196     dreamcast_stop();
   197 }
   199 - (void) reset_action: (id)sender
   200 {
   201     dreamcast_reset();
   202 }
   203 - (void) run_action: (id)sender
   204 {
   205     cocoa_gui_run_later();
   206 }
   207 - (void) run_immediate
   208 {
   209     dreamcast_run();
   210 }
   211 - (void) gdrom_list_action: (id)sender
   212 {
   213     gdrom_list_set_selection( [sender tag] );
   214 }
   215 @end
   218 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   219 {
   220     /* If started from the finder, the first (and only) arg will look something like 
   221     * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
   222     * normally
   223     */
   224     if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
   225         *argc = 1;
   226     }
   227     return TRUE;
   228 }
   230 gboolean gui_init( gboolean withDebug )
   231 {
   232     dreamcast_register_module( &cocoa_gui_module );
   234     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   235     [NSApplication sharedApplication];
   237     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   238     [NSApp setDelegate: delegate];
   239     NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
   240     NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
   241     [NSApp setApplicationIconImage: iconImage];   
   242     cocoa_gui_create_menu();
   243     NSWindow *window = cocoa_gui_create_main_window();
   244     [window makeKeyAndOrderFront: nil];
   245     [NSApp activateIgnoringOtherApps: YES];   
   247     [pool release];
   248 }
   250 void gui_main_loop( gboolean run )
   251 {
   252     if( run ) {
   253         cocoa_gui_autorun = YES;
   254     }
   255     cocoa_gui_is_running = YES;
   256 	[NSApp run];
   257 	cocoa_gui_is_running = NO;
   258 }
   260 void gui_update_state(void)
   261 {
   262     cocoa_gui_update();
   263 }
   265 gboolean gui_error_dialog( const char *msg, ... )
   266 {
   267     if( cocoa_gui_is_running ) {
   268         NSString *error_string;
   270         va_list args;
   271         va_start(args, msg);
   272         error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
   273         NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
   274         va_end(args);
   275         return TRUE;
   276     } else {
   277         return FALSE;
   278     }
   279 }
   281 void gui_update_io_activity( io_activity_type io, gboolean active )
   282 {
   284 }
   287 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   288 {
   289     NSEvent *event;
   290     NSAutoreleasePool *pool;
   292     cocoa_gui_nanos += nanosecs;
   293     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   294         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   295         cocoa_gui_ticks ++;
   296         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   298         // Run the event loop
   299         pool = [NSAutoreleasePool new];
   300         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   301                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   302             [NSApp sendEvent: event];
   303         }
   304         [pool release];
   306         struct timeval tv;
   307         gettimeofday(&tv,NULL);
   308         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   309         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   310         if( (ns * 1.05) < current_period ) {
   311             // We've gotten ahead - sleep for a little bit
   312             struct timespec tv;
   313             tv.tv_sec = 0;
   314             tv.tv_nsec = current_period - ns;
   315             nanosleep(&tv, &tv);
   316         }
   318         /* Update the display every 10 ticks (ie 10 times a second) and 
   319          * save the current tv value */
   320         if( cocoa_gui_ticks > 10 ) {
   321             gchar buf[32];
   322             cocoa_gui_ticks -= 10;
   324             double speed = (float)( (double)current_period * 100.0 / ns );
   325             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   326             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   327             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   328             [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
   330         }
   331     }
   332     return nanosecs;
   333 }
   335 void cocoa_gui_update( void )
   336 {
   338 }
   340 void cocoa_gui_start( void )
   341 {
   342     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   343     [win setRunning: YES];
   344     cocoa_gui_nanos = 0;
   345     gettimeofday(&cocoa_gui_lasttv,NULL);
   346 }
   348 void cocoa_gui_stop( void )
   349 {
   350     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   351     [win setRunning: NO];
   352 }
   354 /**
   355  * Queue a dreamcast_run() to execute after the currently event(s)
   356  */
   357 void cocoa_gui_run_later( void )
   358 {
   359     [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
   360          target: [NSApp delegate] argument: nil order: 1 
   361          modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
   362 }
   364 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
   365 {
   366     NSBitmapImageRep *rep = 
   367         [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
   368          pixelsWide: buffer->width  pixelsHigh: buffer->height
   369         bitsPerSample: 8 samplesPerPixel: 3
   370         hasAlpha: NO isPlanar: NO
   371         colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
   372         bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
   374     NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
   375     [image addRepresentation: rep];
   376     return image;
   377 }
.