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