Search
lxdream.org :: lxdream/src/cocoaui/cocoaui.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/cocoaui/cocoaui.c
changeset 681:1755a126b109
next685:fa1589b42be7
author nkeynes
date Sun Jun 01 00:47:45 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change First cut of the Cocoa GUI implementation
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 <sys/time.h>
    23 #include "lxdream.h"
    24 #include "dreamcast.h"
    25 #include "dream.h"
    26 #include "gui.h"
    27 #include "cocoaui/cocoaui.h"
    29 void cocoa_gui_update( void );
    30 void cocoa_gui_start( void );
    31 void cocoa_gui_stop( void );
    32 void cocoa_gui_run_later( void );
    33 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
    35 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
    36                   cocoa_gui_update, 
    37                   cocoa_gui_start, 
    38                   cocoa_gui_run_slice, 
    39                   cocoa_gui_stop, 
    40                   NULL, NULL };
    42 /**
    43  * Count of running nanoseconds - used to cut back on the GUI runtime
    44  */
    45 static uint32_t cocoa_gui_nanos = 0;
    46 static uint32_t cocoa_gui_ticks = 0;
    47 static struct timeval cocoa_gui_lasttv;
    48 static BOOL cocoa_gui_autorun = NO;
    50 @interface NSApplication (PrivateAdditions)
    51 - (void) setAppleMenu:(NSMenu *)aMenu;
    52 @end
    54 static void cocoa_gui_create_menu(void)
    55 {
    56     NSMenu *appleMenu, *services;
    57     NSMenuItem *menuItem;
    58     NSString *title;
    59     NSString *appName;
    61     appName = @"Lxdream";
    62     appleMenu = [[NSMenu alloc] initWithTitle:@""];
    64     /* Add menu items */
    65     title = [@"About " stringByAppendingString:appName];
    66     [appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
    67     [appleMenu addItem:[NSMenuItem separatorItem]];
    69     // Services Menu
    70     services = [[[NSMenu alloc] init] autorelease];
    71     [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
    72     [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
    74     // Hide AppName
    75     title = [@"Hide " stringByAppendingString:appName];
    76     [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
    78     // Hide Others
    79     menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others" 
    80                                                   action:@selector(hideOtherApplications:) 
    81                                            keyEquivalent:@"h"];
    82     [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
    84     // Show All
    85     [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
    86     [appleMenu addItem:[NSMenuItem separatorItem]];
    88     // Quit AppName
    89     title = [@"Quit " stringByAppendingString:appName];
    90     [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
    92     /* Put menu into the menubar */
    93     menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil  keyEquivalent:@""];
    94     [menuItem setSubmenu: appleMenu];
    95     NSMenu *menu = [NSMenu new];
    96     [menu addItem: menuItem];
    98     NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
    99     [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
   100     [fileMenu addItemWithTitle: NS_("GD-Rom") action: @selector(mount_action:) keyEquivalent: @"g"];
   101     [fileMenu addItem: [NSMenuItem separatorItem]];
   102     [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
   103       setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
   104     [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
   105     [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
   106     [fileMenu addItem: [NSMenuItem separatorItem]];
   107     [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
   108     [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
   110     menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
   111     [menuItem setSubmenu: fileMenu];
   112     [menu addItem: menuItem];
   114     /* Tell the application object that this is now the application menu */
   115     [NSApp setMainMenu: menu];
   116     [NSApp setAppleMenu: appleMenu];
   117     [NSApp setServicesMenu: services];
   119     /* Finally give up our references to the objects */
   120     [appleMenu release];
   121     [menuItem release];
   122     [menu release];
   123 }
   125 @interface LxdreamDelegate : NSObject
   126 @end
   128 @implementation LxdreamDelegate
   129 - (void)windowWillClose: (NSNotification *)notice
   130 {
   131     dreamcast_shutdown();
   132     exit(0);
   133 }
   134 - (void)windowDidBecomeMain: (NSNotification *)notice
   135 {
   136     if( cocoa_gui_autorun ) {
   137         cocoa_gui_autorun = NO;
   138         cocoa_gui_run_later();
   139     }
   140 }
   141 - (void) load_action: (id)sender
   142 {
   143     NSOpenPanel *panel = [NSOpenPanel openPanel];
   144     NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
   145     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: fileTypes];
   146     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   147         NSString *filename = [[panel filenames] objectAtIndex: 0];
   148         dreamcast_load_state( [filename UTF8String] );
   149     }
   150 }
   151 - (void) save_action: (id)sender
   152 {
   153     NSSavePanel *panel = [NSSavePanel savePanel];
   154     [panel setRequiredFileType: @"dst"];
   155     int result = [panel runModalForDirectory: NSHomeDirectory() file:@""];
   156     if( result == NSOKButton ) {
   157         NSString *filename = [panel filename];
   158         dreamcast_save_state( [filename UTF8String] );
   159     }
   160 }
   161 - (void) load_binary_action: (id)sender
   162 {
   163     NSOpenPanel *panel = [NSOpenPanel openPanel];
   164     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   165     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   166         NSString *filename = [[panel filenames] objectAtIndex: 0];
   167         file_load_magic( [filename UTF8String] );
   168     }
   169 }
   170 - (void) mount_action: (id)sender
   171 {
   172     NSOpenPanel *panel = [NSOpenPanel openPanel];
   173     int result = [panel runModalForDirectory: NSHomeDirectory() file: nil types: nil];
   174     if( result == NSOKButton && [[panel filenames] count] > 0 ) {
   175         NSString *filename = [[panel filenames] objectAtIndex: 0];
   176         gdrom_mount_image( [filename UTF8String] );
   177     }
   178 }
   179 - (void) pause_action: (id)sender
   180 {
   181     dreamcast_stop();
   182 }
   184 - (void) reset_action: (id)sender
   185 {
   186     dreamcast_reset();
   187 }
   188 - (void) run_action: (id)sender
   189 {
   190     cocoa_gui_run_later();
   191 }
   192 - (void) run_immediate
   193 {
   194     dreamcast_run();
   195 }
   196 @end
   199 gboolean gui_parse_cmdline( int *argc, char **argv[] )
   200 {
   201 	/* do nothing */
   202 }
   204 gboolean gui_init( gboolean withDebug )
   205 {
   206     dreamcast_register_module( &cocoa_gui_module );
   208     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
   209     [NSApplication sharedApplication];
   210     LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
   211     [NSApp setDelegate: delegate];
   212     cocoa_gui_create_menu();
   213     NSWindow *window = cocoa_gui_create_main_window();
   214     [window makeKeyAndOrderFront: nil];
   215     [NSApp activateIgnoringOtherApps: YES];   
   216     [pool release];
   217 }
   219 void gui_main_loop( gboolean run )
   220 {
   221     if( run ) {
   222         cocoa_gui_autorun = YES;
   223         /*
   224              */
   225     }
   226 	[NSApp run];
   227 }
   229 void gui_update_state(void)
   230 {
   231     cocoa_gui_update();
   232 }
   234 gboolean gui_error_dialog( const char *msg, ... )
   235 {
   237 }
   239 void gui_update_io_activity( io_activity_type io, gboolean active )
   240 {
   242 }
   245 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
   246 {
   247     NSEvent *event;
   248     NSAutoreleasePool *pool;
   250     cocoa_gui_nanos += nanosecs;
   251     if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
   252         cocoa_gui_nanos -= GUI_TICK_PERIOD;
   253         cocoa_gui_ticks ++;
   254         uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
   256         // Run the event loop
   257         pool = [NSAutoreleasePool new];
   258         while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil 
   259                          inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
   260             [NSApp sendEvent: event];
   261         }
   262         [pool release];
   264         struct timeval tv;
   265         gettimeofday(&tv,NULL);
   266         uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) + 
   267         (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
   268         if( (ns * 1.05) < current_period ) {
   269             // We've gotten ahead - sleep for a little bit
   270             struct timespec tv;
   271             tv.tv_sec = 0;
   272             tv.tv_nsec = current_period - ns;
   273             nanosleep(&tv, &tv);
   274         }
   276         /* Update the display every 10 ticks (ie 10 times a second) and 
   277          * save the current tv value */
   278         if( cocoa_gui_ticks > 10 ) {
   279             gchar buf[32];
   280             cocoa_gui_ticks -= 10;
   282             double speed = (float)( (double)current_period * 100.0 / ns );
   283             cocoa_gui_lasttv.tv_sec = tv.tv_sec;
   284             cocoa_gui_lasttv.tv_usec = tv.tv_usec;
   285             snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
   286             [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
   288         }
   289     }
   290     return nanosecs;
   291 }
   293 void cocoa_gui_update( void )
   294 {
   296 }
   298 void cocoa_gui_start( void )
   299 {
   300     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   301     [win setRunning: YES];
   302     cocoa_gui_nanos = 0;
   303     gettimeofday(&cocoa_gui_lasttv,NULL);
   304 }
   306 void cocoa_gui_stop( void )
   307 {
   308     LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
   309     [win setRunning: NO];
   310 }
   312 /**
   313  * Queue a dreamcast_run() to execute after the currently event(s)
   314  */
   315 void cocoa_gui_run_later( void )
   316 {
   317     [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate) 
   318          target: [NSApp delegate] argument: nil order: 1 
   319          modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
.