Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 780:4e4ea322cb84
prev773:146c3210768b
next785:00235838aaec
author nkeynes
date Mon Jul 28 10:13:13 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Move grab management to window
Clear grab if the window loses key focus (probably a good idea in general)
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 "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 /**
    59  * Produces the menu title by looking the text up in gettext, removing any
    60  * underscores, and returning the result as an NSString.
    61  */
    62 static NSString *NSMENU_( const char *text )
    63 {
    64     return [[NSString stringWithUTF8String: gettext(text)] stringByReplacingOccurrencesOfString: @"_" withString: @""];
    65 }
    67 static void cocoa_gui_create_menu(void)
    68 {
    69     NSMenu *appleMenu, *services;
    70     NSMenuItem *menuItem;
    71     NSString *title;
    72     NSString *appName;
    74     appName = @"Lxdream";
    75     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    77     /* Add menu items */
    78     title = [@"About " stringByAppendingString:appName];
    79     [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
    81     [appleMenu addItem:[NSMenuItem separatorItem]];
    82     [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
    84     // Services Menu
    85     [appleMenu addItem:[NSMenuItem separatorItem]];
    86     services = [[[NSMenu alloc] init] autorelease];
    87     [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
    88     [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
    90     // Hide AppName
    91     title = [@"Hide " stringByAppendingString:appName];
    92     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
    94     // Hide Others
    95     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
    96                               action:@selector(hideOtherApplications:) 
    97                               keyEquivalent:@"h"];
    98     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   100     // Show All
   101     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
   102     [appleMenu addItem:[NSMenuItem separatorItem]];
   104     // Quit AppName
   105     title = [@"Quit " stringByAppendingString:appName];
   106     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
   108     /* Put menu into the menubar */
   109     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
   110     [menuItem setSubmenu: appleMenu];
   111     NSMenu *menu = [NSMenu new];
   112     [menu addItem: menuItem];
   114     NSMenu *gdromMenu = cocoa_gdrom_menu_new();
   116     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
   117     [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   118     [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
   119       setSubmenu: gdromMenu];
   120     [fileMenu addItem: [NSMenuItem separatorItem]];
   121     [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   122       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   123     [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   124     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   125     [fileMenu addItem: [NSMenuItem separatorItem]];
   126     [fileMenu addItemWithTitle: NSMENU_("_Load State...") action: @selector(load_action:) keyEquivalent: @"o"];
   127     [fileMenu addItemWithTitle: NSMENU_("_Save State...") action: @selector(save_action:) keyEquivalent: @"s"];
   129     menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
   130     [menuItem setSubmenu: fileMenu];
   131     [menu addItem: menuItem];
   133     /* Tell the application object that this is now the application menu */
   134     [NSApp setMainMenu: menu];
   135     [NSApp setAppleMenu: appleMenu];
   136     [NSApp setServicesMenu: services];
   138     /* Finally give up our references to the objects */
   139     [appleMenu release];
   140     [menuItem release];
   141     [menu release];
   142 }
   144 @interface LxdreamDelegate : NSObject
   145 @end
   147 @implementation LxdreamDelegate
   148 - (void)windowWillClose: (NSNotification *)notice
   149 {
   150     dreamcast_shutdown();
   151     exit(0);
   152 }
   153 - (void)windowDidBecomeMain: (NSNotification *)notice
   154 {
   155     if( cocoa_gui_autorun ) {
   156         cocoa_gui_autorun = NO;
   157         cocoa_gui_run_later();
   158     }
   159 }
   160 - (void)windowDidBecomeKey: (NSNotification *)notice
   161 {
   162     display_set_focused( TRUE );
   163 }
   164 - (void)windowDidResignKey: (NSNotification *)notice
   165 {
   166     display_set_focused( FALSE );
   167     [((LxdreamMainWindow *)[NSApp mainWindow]) setIsGrabbed: NO];
   168 }
   169 - (void) about_action: (id)sender
   170 {
   171     NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
   172     NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright),  nil];
   174     NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
   176     [NSApp orderFrontStandardAboutPanelWithOptions: options];
   177 }
   178 - (void) preferences_action: (id)sender
   179 {
   180     cocoa_gui_show_preferences();
   181 }
   182 - (void) load_action: (id)sender
   183 {
   184     NSOpenPanel *panel = [NSOpenPanel openPanel];
   185     const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
   186     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   187     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   188     int result = [panel runModalForDirectory: path file: nil types: fileTypes];
   189     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   190         NSString *filename = [[panel filenames] objectAtIndex: 0];
   191         dreamcast_load_state( [filename UTF8String] );
   192     }
   193 }
   194 - (void) save_action: (id)sender
   195 {
   196     NSSavePanel *panel = [NSSavePanel savePanel];
   197     const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
   198     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   199     [panel setRequiredFileType: @"dst"];
   200     int result = [panel runModalForDirectory: path file:@""];
   201     if( result == NSOKButton ) {
   202         NSString *filename = [panel filename];
   203         dreamcast_save_state( [filename UTF8String] );
   204     }
   205 }
   206 - (void) load_binary_action: (id)sender
   207 {
   208     NSOpenPanel *panel = [NSOpenPanel openPanel];
   209     const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   210     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   211     int result = [panel runModalForDirectory: path file: nil types: nil];
   212     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   213         NSString *filename = [[panel filenames] objectAtIndex: 0];
   214         file_load_magic( [filename UTF8String] );
   215     }
   216 }
   217 - (void) mount_action: (id)sender
   218 {
   219     NSOpenPanel *panel = [NSOpenPanel openPanel];
   220     const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   221     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   222     int result = [panel runModalForDirectory: path file: nil types: nil];
   223     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   224         NSString *filename = [[panel filenames] objectAtIndex: 0];
   225         gdrom_mount_image( [filename UTF8String] );
   226     }
   227 }
   228 - (void) pause_action: (id)sender
   229 {
   230     dreamcast_stop();
   231 }
   233 - (void) reset_action: (id)sender
   234 {
   235     dreamcast_reset();
   236 }
   237 - (void) run_action: (id)sender
   238 {
   239     cocoa_gui_run_later();
   240 }
   241 - (void) run_immediate
   242 {
   243     dreamcast_run();
   244 }
   245 - (void) gdrom_list_action: (id)sender
   246 {
   247     gdrom_list_set_selection( [sender tag] );
   248 }
   249 @end
   252 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   253 {
   254     /* If started from the finder, the first (and only) arg will look something like 
   255      * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
   256      * normally
   257      */
   258     if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
   259         *argc = 1;
   260     }
   261     return TRUE;
   262 }
   264 gboolean gui_init( gboolean withDebug )
   265 {
   266     dreamcast_register_module( &cocoa_gui_module );
   268     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   269     [NSApplication sharedApplication];
   271     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   272     [NSApp setDelegate: delegate];
   273     NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
   274     NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
   275     [iconImage setName: @"NSApplicationIcon"];
   276     [NSApp setApplicationIconImage: iconImage];   
   277     cocoa_gui_create_menu();
   278     NSWindow *window = cocoa_gui_create_main_window();
   279     [window makeKeyAndOrderFront: nil];
   280     [NSApp activateIgnoringOtherApps: YES];   
   282     [pool release];
   283 }
   285 void gui_main_loop( gboolean run )
   286 {
   287     if( run ) {
   288         cocoa_gui_autorun = YES;
   289     }
   290     cocoa_gui_is_running = YES;
   291     [NSApp run];
   292     cocoa_gui_is_running = NO;
   293 }
   295 void gui_update_state(void)
   296 {
   297     cocoa_gui_update();
   298 }
   300 gboolean gui_error_dialog( const char *msg, ... )
   301 {
   302     if( cocoa_gui_is_running ) {
   303         NSString *error_string;
   305         va_list args;
   306         va_start(args, msg);
   307         error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
   308         NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
   309         va_end(args);
   310         return TRUE;
   311     } else {
   312         return FALSE;
   313     }
   314 }
   316 void gui_update_io_activity( io_activity_type io, gboolean active )
   317 {
   319 }
   322 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   323 {
   324     NSEvent *event;
   325     NSAutoreleasePool *pool;
   327     cocoa_gui_nanos += nanosecs;
   328     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   329         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   330         cocoa_gui_ticks ++;
   331         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   333         // Run the event loop
   334         pool = [NSAutoreleasePool new];
   335         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   336                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   337             [NSApp sendEvent: event];
   338         }
   339         [pool release];
   341         struct timeval tv;
   342         gettimeofday(&tv,NULL);
   343         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   344         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   345         if( (ns * 1.05) < current_period ) {
   346             // We've gotten ahead - sleep for a little bit
   347             struct timespec tv;
   348             tv.tv_sec = 0;
   349             tv.tv_nsec = current_period - ns;
   350             nanosleep(&tv, &tv);
   351         }
   353         /* Update the display every 10 ticks (ie 10 times a second) and 
   354          * save the current tv value */
   355         if( cocoa_gui_ticks > 10 ) {
   356             gchar buf[32];
   357             cocoa_gui_ticks -= 10;
   359             double speed = (float)( (double)current_period * 100.0 / ns );
   360             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   361             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   362             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   363             [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
   365         }
   366     }
   367     return nanosecs;
   368 }
   370 void cocoa_gui_update( void )
   371 {
   373 }
   375 void cocoa_gui_start( void )
   376 {
   377     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   378     [win setRunning: YES];
   379     cocoa_gui_nanos = 0;
   380     gettimeofday(&cocoa_gui_lasttv,NULL);
   381 }
   383 void cocoa_gui_stop( void )
   384 {
   385     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   386     [win setRunning: NO];
   387 }
   389 /**
   390  * Queue a dreamcast_run() to execute after the currently event(s)
   391  */
   392 void cocoa_gui_run_later( void )
   393 {
   394     [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
   395      target: [NSApp delegate] argument: nil order: 1 
   396      modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
   397 }
   399 /*************************** Convenience methods ***************************/
   401 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
   402 {
   403     NSBitmapImageRep *rep = 
   404         [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
   405          pixelsWide: buffer->width  pixelsHigh: buffer->height
   406          bitsPerSample: 8 samplesPerPixel: 3
   407          hasAlpha: NO isPlanar: NO
   408          colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
   409          bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
   411     NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
   412     [image addRepresentation: rep];
   413     return image;
   414 }
   417 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
   418 {
   419     NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
   420     [label setStringValue: text];
   421     [label setBordered: NO];
   422     [label setDrawsBackground: NO];
   423     [label setEditable: NO];
   424     [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
   425     if( parent != NULL ) {
   426         [parent addSubview: label];
   427     }
   428     return label;
   429 }
.