Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.m
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.m
changeset 1028:f99eeaf084c2
prev1019:87f191f92f8f
next1036:af7b0c5905dd
author nkeynes
date Sun Jun 14 11:44:19 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change Add fullscreen support
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 "gdrom/gdrom.h"
    31 #include "gdlist.h"
    32 #include "loader.h"
    33 #include "cocoaui/cocoaui.h"
    35 void cocoa_gui_update( void );
    36 void cocoa_gui_start( void );
    37 void cocoa_gui_stop( void );
    38 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
    40 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
    41         cocoa_gui_update, 
    42         cocoa_gui_start, 
    43         cocoa_gui_run_slice, 
    44         cocoa_gui_stop, 
    45         NULL, NULL };
    47 /**
    48  * Count of running nanoseconds - used to cut back on the GUI runtime
    49  */
    50 static uint32_t cocoa_gui_nanos = 0;
    51 static uint32_t cocoa_gui_ticks = 0;
    52 static struct timeval cocoa_gui_lasttv;
    53 static BOOL cocoa_gui_autorun = NO;
    54 static BOOL cocoa_gui_is_running = NO;
    55 static LxdreamMainWindow *mainWindow = NULL;
    57 @interface NSApplication (PrivateAdditions)
    58 - (void) setAppleMenu:(NSMenu *)aMenu;
    59 @end
    61 gboolean cocoa_gui_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *user_data )
    62 {
    63     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    64     LxdreamMainWindow *window = (LxdreamMainWindow *)user_data;
    65     [window updateTitle];
    66     [pool release];
    67     return TRUE;
    68 }
    70 /**
    71  * Produces the menu title by looking the text up in gettext, removing any
    72  * underscores, and returning the result as an NSString.
    73  */
    74 static NSString *NSMENU_( const char *text )
    75 {
    76     const char *s = gettext(text);
    77     char buf[strlen(s)+1];
    78     char *d = buf;
    80     while( *s != '\0' ) {
    81         if( *s != '_' ) {
    82             *d++ = *s;
    83         }
    84         s++;
    85     }
    86     *d = '\0';
    88     return [NSString stringWithUTF8String: buf];
    89 }
    91 static void cocoa_gui_create_menu(void)
    92 {
    93     NSMenu *appleMenu, *services;
    94     NSMenuItem *menuItem;
    95     NSString *title;
    96     NSString *appName;
    98     appName = @"Lxdream";
    99     appleMenu = [[NSMenu alloc] initWithTitle:@""];
   101     /* Add menu items */
   102     title = [@"About " stringByAppendingString:appName];
   103     [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
   105     [appleMenu addItem:[NSMenuItem separatorItem]];
   106     [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
   108     // Services Menu
   109     [appleMenu addItem:[NSMenuItem separatorItem]];
   110     services = [[[NSMenu alloc] init] autorelease];
   111     [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
   112     [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
   114     // Hide AppName
   115     title = [@"Hide " stringByAppendingString:appName];
   116     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
   118     // Hide Others
   119     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
   120                               action:@selector(hideOtherApplications:) 
   121                               keyEquivalent:@"h"];
   122     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   124     // Show All
   125     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
   126     [appleMenu addItem:[NSMenuItem separatorItem]];
   128     // Quit AppName
   129     title = [@"Quit " stringByAppendingString:appName];
   130     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
   132     /* Put menu into the menubar */
   133     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
   134     [menuItem setSubmenu: appleMenu];
   135     NSMenu *menu = [NSMenu new];
   136     [menu addItem: menuItem];
   138     NSMenu *gdromMenu = cocoa_gdrom_menu_new();
   140     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
   141     [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   142     [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
   143       setSubmenu: gdromMenu];
   144     [fileMenu addItem: [NSMenuItem separatorItem]];
   145     [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   146       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   147     [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   148     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   149     [fileMenu addItem: [NSMenuItem separatorItem]];
   150     [fileMenu addItemWithTitle: NSMENU_("_Load State...") action: @selector(load_action:) keyEquivalent: @"o"];
   151     [fileMenu addItemWithTitle: NSMENU_("_Save State...") action: @selector(save_action:) keyEquivalent: @"s"];
   152     [fileMenu addItem: [NSMenuItem separatorItem]];
   153     [fileMenu addItemWithTitle: NSMENU_("_Full Screen...") action: @selector(fullscreen_action:) keyEquivalent: @"\r"];
   155     menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
   156     [menuItem setSubmenu: fileMenu];
   157     [menu addItem: menuItem];
   159     /* Tell the application object that this is now the application menu */
   160     [NSApp setMainMenu: menu];
   161     [NSApp setAppleMenu: appleMenu];
   162     [NSApp setServicesMenu: services];
   164     /* Finally give up our references to the objects */
   165     [appleMenu release];
   166     [menuItem release];
   167     [menu release];
   168 }
   170 @interface LxdreamDelegate : NSObject
   171 @end
   173 @implementation LxdreamDelegate
   174 - (void)windowWillClose: (NSNotification *)notice
   175 {
   176     dreamcast_shutdown();
   177     exit(0);
   178 }
   179 - (void)windowDidBecomeMain: (NSNotification *)notice
   180 {
   181     if( cocoa_gui_autorun ) {
   182         cocoa_gui_autorun = NO;
   183         gui_do_later(dreamcast_run);
   184     }
   185 }
   186 - (void)windowDidBecomeKey: (NSNotification *)notice
   187 {
   188     display_set_focused( TRUE );
   189 }
   190 - (void)windowDidResignKey: (NSNotification *)notice
   191 {
   192     display_set_focused( FALSE );
   193     [mainWindow setIsGrabbed: NO];
   194 }
   195 - (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
   196 {
   197     const gchar *cname = [filename UTF8String];
   198     if( file_load_magic(cname) ) {
   199         // Queue up a run event
   200         gui_do_later(dreamcast_run);
   201         return YES;
   202     } else {
   203         return NO;
   204     }
   206 }
   207 - (void) about_action: (id)sender
   208 {
   209     NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
   210     NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright),  nil];
   212     NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
   214     [NSApp orderFrontStandardAboutPanelWithOptions: options];
   215 }
   216 - (void) preferences_action: (id)sender
   217 {
   218     cocoa_gui_show_preferences();
   219 }
   220 - (void) load_action: (id)sender
   221 {
   222     NSOpenPanel *panel = [NSOpenPanel openPanel];
   223     const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
   224     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   225     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   226     int result = [panel runModalForDirectory: path file: nil types: fileTypes];
   227     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   228         NSString *filename = [[panel filenames] objectAtIndex: 0];
   229         dreamcast_load_state( [filename UTF8String] );
   230     }
   231 }
   232 - (void) save_action: (id)sender
   233 {
   234     NSSavePanel *panel = [NSSavePanel savePanel];
   235     const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
   236     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   237     [panel setRequiredFileType: @"dst"];
   238     int result = [panel runModalForDirectory: path file:@""];
   239     if( result == NSOKButton ) {
   240         NSString *filename = [panel filename];
   241         dreamcast_save_state( [filename UTF8String] );
   242     }
   243 }
   244 - (void) load_binary_action: (id)sender
   245 {
   246     NSOpenPanel *panel = [NSOpenPanel openPanel];
   247     const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   248     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   249     int result = [panel runModalForDirectory: path file: nil types: nil];
   250     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   251         NSString *filename = [[panel filenames] objectAtIndex: 0];
   252         file_load_magic( [filename UTF8String] );
   253     }
   254 }
   255 - (void) mount_action: (id)sender
   256 {
   257     NSOpenPanel *panel = [NSOpenPanel openPanel];
   258     const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
   259     NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
   260     int result = [panel runModalForDirectory: path file: nil types: nil];
   261     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   262         NSString *filename = [[panel filenames] objectAtIndex: 0];
   263         gdrom_mount_image( [filename UTF8String] );
   264     }
   265 }
   266 - (void) pause_action: (id)sender
   267 {
   268     dreamcast_stop();
   269 }
   271 - (void) reset_action: (id)sender
   272 {
   273     dreamcast_reset();
   274 }
   275 - (void) run_action: (id)sender
   276 {
   277     if( !dreamcast_is_running() ) {
   278         gui_do_later(dreamcast_run);
   279     }
   280 }
   281 - (void) gdrom_list_action: (id)sender
   282 {
   283     gdrom_list_set_selection( [sender tag] );
   284 }
   285 - (void) fullscreen_action: (id)sender
   286 {
   287     [mainWindow setFullscreen: ![mainWindow isFullscreen]]; 
   288 }
   289 @end
   292 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   293 {
   294     /* If started from the finder, the first (and only) arg will look something like 
   295      * -psn_0_... - we want to remove this so that lxdream doesn't try to process it 
   296      * normally
   297      */
   298     if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
   299         *argc = 1;
   300     }
   301     return TRUE;
   302 }
   304 gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
   305 {
   306     dreamcast_register_module( &cocoa_gui_module );
   308     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   309     [NSApplication sharedApplication];
   311     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   312     [NSApp setDelegate: delegate];
   313     NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"lxdream" ofType:@"png"];
   314     NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
   315     [iconImage setName: @"NSApplicationIcon"];
   316     [NSApp setApplicationIconImage: iconImage];   
   317     cocoa_gui_create_menu();
   318     mainWindow = cocoa_gui_create_main_window();
   319     [mainWindow makeKeyAndOrderFront: nil];
   320     [NSApp activateIgnoringOtherApps: YES];   
   322     register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
   323     if( withFullscreen ) {
   324     	[mainWindow setFullscreen: YES];
   325     }
   326     [pool release];
   327     return TRUE;
   328 }
   330 void gui_main_loop( gboolean run )
   331 {
   332     if( run ) {
   333         cocoa_gui_autorun = YES;
   334     }
   335     cocoa_gui_is_running = YES;
   336     [NSApp run];
   337     cocoa_gui_is_running = NO;
   338 }
   340 void gui_update_state(void)
   341 {
   342     cocoa_gui_update();
   343 }
   345 void gui_set_use_grab( gboolean grab )
   346 {
   347     [mainWindow setUseGrab: (grab ? YES : NO)];
   348 }
   350 gboolean gui_error_dialog( const char *msg, ... )
   351 {
   352     if( cocoa_gui_is_running ) {
   353         NSString *error_string;
   355         va_list args;
   356         va_start(args, msg);
   357         error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
   358         NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
   359         va_end(args);
   360         return TRUE;
   361     } else {
   362         return FALSE;
   363     }
   364 }
   366 void gui_update_io_activity( io_activity_type io, gboolean active )
   367 {
   369 }
   372 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   373 {
   374     NSEvent *event;
   375     NSAutoreleasePool *pool;
   377     cocoa_gui_nanos += nanosecs;
   378     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   379         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   380         cocoa_gui_ticks ++;
   381         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   383         // Run the event loop
   384         pool = [NSAutoreleasePool new];
   385         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   386                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   387             [NSApp sendEvent: event];
   388         }
   389         [pool release];
   391         struct timeval tv;
   392         gettimeofday(&tv,NULL);
   393         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   394         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   395         if( (ns * 1.05) < current_period ) {
   396             // We've gotten ahead - sleep for a little bit
   397             struct timespec tv;
   398             tv.tv_sec = 0;
   399             tv.tv_nsec = current_period - ns;
   400             nanosleep(&tv, &tv);
   401         }
   403         /* Update the display every 10 ticks (ie 10 times a second) and 
   404          * save the current tv value */
   405         if( cocoa_gui_ticks > 10 ) {
   406             gchar buf[32];
   407             cocoa_gui_ticks -= 10;
   409             double speed = (float)( (double)current_period * 100.0 / ns );
   410             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   411             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   412             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   413             [mainWindow setStatusText: buf];
   415         }
   416     }
   417     return nanosecs;
   418 }
   420 void cocoa_gui_update( void )
   421 {
   423 }
   425 void cocoa_gui_start( void )
   426 {
   427     [mainWindow setRunning: YES];
   428     cocoa_gui_nanos = 0;
   429     gettimeofday(&cocoa_gui_lasttv,NULL);
   430 }
   432 void cocoa_gui_stop( void )
   433 {
   434     [mainWindow setRunning: NO];
   435 }
   437 @interface DoLaterStub : NSObject
   438 {
   439     do_later_callback_t func;
   440 }
   441 @end    
   443 @implementation DoLaterStub
   444 - (id) init: (do_later_callback_t)f
   445 {
   446     [super init];
   447     func = f;
   448     return self;
   449 }
   450 - (void) do
   451 {
   452     func();
   453 }
   454 @end
   456 /**
   457  * Queue a dreamcast_run() to execute after the currently event(s)
   458  */
   459 void gui_do_later( do_later_callback_t func )
   460 {
   461     DoLaterStub *stub = [[[DoLaterStub alloc] init: func] autorelease]; 
   462     [[NSRunLoop currentRunLoop] performSelector: @selector(do) 
   463      target: stub argument: nil order: 1 
   464      modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
   465 }
   467 /*************************** Convenience methods ***************************/
   469 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
   470 {
   471     NSBitmapImageRep *rep = 
   472         [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
   473          pixelsWide: buffer->width  pixelsHigh: buffer->height
   474          bitsPerSample: 8 samplesPerPixel: 3
   475          hasAlpha: NO isPlanar: NO
   476          colorSpaceName: NSDeviceRGBColorSpace  bitmapFormat: 0
   477          bytesPerRow: buffer->rowstride  bitsPerPixel: 24];
   479     NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
   480     [image addRepresentation: rep];
   481     return image;
   482 }
   485 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
   486 {
   487     NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
   488     [label setStringValue: text];
   489     [label setBordered: NO];
   490     [label setDrawsBackground: NO];
   491     [label setEditable: NO];
   492     [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
   493     if( parent != NULL ) {
   494         [parent addSubview: label];
   495     }
   496     return label;
   497 }
.