4 * Core Cocoa-based user interface
6 * Copyright (c) 2008 Nathan Keynes.
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.
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.
19 #include <AppKit/AppKit.h>
26 #include "dreamcast.h"
30 #include "gdrom/gdrom.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 void cocoa_gui_run_later( void );
39 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
41 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
49 * Count of running nanoseconds - used to cut back on the GUI runtime
51 static uint32_t cocoa_gui_nanos = 0;
52 static uint32_t cocoa_gui_ticks = 0;
53 static struct timeval cocoa_gui_lasttv;
54 static BOOL cocoa_gui_autorun = NO;
55 static BOOL cocoa_gui_is_running = NO;
57 @interface NSApplication (PrivateAdditions)
58 - (void) setAppleMenu:(NSMenu *)aMenu;
62 * Produces the menu title by looking the text up in gettext, removing any
63 * underscores, and returning the result as an NSString.
65 static NSString *NSMENU_( const char *text )
67 const char *s = gettext(text);
68 char buf[strlen(s)+1];
79 return [NSString stringWithUTF8String: buf];
82 static void cocoa_gui_create_menu(void)
84 NSMenu *appleMenu, *services;
90 appleMenu = [[NSMenu alloc] initWithTitle:@""];
93 title = [@"About " stringByAppendingString:appName];
94 [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
96 [appleMenu addItem:[NSMenuItem separatorItem]];
97 [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
100 [appleMenu addItem:[NSMenuItem separatorItem]];
101 services = [[[NSMenu alloc] init] autorelease];
102 [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
103 [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
106 title = [@"Hide " stringByAppendingString:appName];
107 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
110 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
111 action:@selector(hideOtherApplications:)
113 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
116 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
117 [appleMenu addItem:[NSMenuItem separatorItem]];
120 title = [@"Quit " stringByAppendingString:appName];
121 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
123 /* Put menu into the menubar */
124 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
125 [menuItem setSubmenu: appleMenu];
126 NSMenu *menu = [NSMenu new];
127 [menu addItem: menuItem];
129 NSMenu *gdromMenu = cocoa_gdrom_menu_new();
131 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
132 [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
133 [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
134 setSubmenu: gdromMenu];
135 [fileMenu addItem: [NSMenuItem separatorItem]];
136 [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
137 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
138 [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
139 [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
140 [fileMenu addItem: [NSMenuItem separatorItem]];
141 [fileMenu addItemWithTitle: NSMENU_("_Load State...") action: @selector(load_action:) keyEquivalent: @"o"];
142 [fileMenu addItemWithTitle: NSMENU_("_Save State...") action: @selector(save_action:) keyEquivalent: @"s"];
144 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
145 [menuItem setSubmenu: fileMenu];
146 [menu addItem: menuItem];
148 /* Tell the application object that this is now the application menu */
149 [NSApp setMainMenu: menu];
150 [NSApp setAppleMenu: appleMenu];
151 [NSApp setServicesMenu: services];
153 /* Finally give up our references to the objects */
159 @interface LxdreamDelegate : NSObject
162 @implementation LxdreamDelegate
163 - (void)windowWillClose: (NSNotification *)notice
165 dreamcast_shutdown();
168 - (void)windowDidBecomeMain: (NSNotification *)notice
170 if( cocoa_gui_autorun ) {
171 cocoa_gui_autorun = NO;
172 cocoa_gui_run_later();
175 - (void)windowDidBecomeKey: (NSNotification *)notice
177 display_set_focused( TRUE );
179 - (void)windowDidResignKey: (NSNotification *)notice
181 display_set_focused( FALSE );
182 [((LxdreamMainWindow *)[NSApp mainWindow]) setIsGrabbed: NO];
184 - (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
186 const gchar *cname = [filename UTF8String];
187 if( file_load_magic(cname) ) {
188 // Queue up a run event
189 cocoa_gui_run_later();
196 - (void) about_action: (id)sender
198 NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
199 NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright), nil];
201 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
203 [NSApp orderFrontStandardAboutPanelWithOptions: options];
205 - (void) preferences_action: (id)sender
207 cocoa_gui_show_preferences();
209 - (void) load_action: (id)sender
211 NSOpenPanel *panel = [NSOpenPanel openPanel];
212 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
213 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
214 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
215 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
216 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
217 NSString *filename = [[panel filenames] objectAtIndex: 0];
218 dreamcast_load_state( [filename UTF8String] );
221 - (void) save_action: (id)sender
223 NSSavePanel *panel = [NSSavePanel savePanel];
224 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
225 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
226 [panel setRequiredFileType: @"dst"];
227 int result = [panel runModalForDirectory: path file:@""];
228 if( result == NSOKButton ) {
229 NSString *filename = [panel filename];
230 dreamcast_save_state( [filename UTF8String] );
233 - (void) load_binary_action: (id)sender
235 NSOpenPanel *panel = [NSOpenPanel openPanel];
236 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
237 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
238 int result = [panel runModalForDirectory: path file: nil types: nil];
239 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
240 NSString *filename = [[panel filenames] objectAtIndex: 0];
241 file_load_magic( [filename UTF8String] );
244 - (void) mount_action: (id)sender
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 gdrom_mount_image( [filename UTF8String] );
255 - (void) pause_action: (id)sender
260 - (void) reset_action: (id)sender
264 - (void) run_action: (id)sender
266 cocoa_gui_run_later();
268 - (void) run_immediate
272 - (void) gdrom_list_action: (id)sender
274 gdrom_list_set_selection( [sender tag] );
279 gboolean gui_parse_cmdline( int *argc, char **argv[] )
281 /* If started from the finder, the first (and only) arg will look something like
282 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
285 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
291 gboolean gui_init( gboolean withDebug )
293 dreamcast_register_module( &cocoa_gui_module );
295 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
296 [NSApplication sharedApplication];
298 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
299 [NSApp setDelegate: delegate];
300 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
301 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
302 [iconImage setName: @"NSApplicationIcon"];
303 [NSApp setApplicationIconImage: iconImage];
304 cocoa_gui_create_menu();
305 NSWindow *window = cocoa_gui_create_main_window();
306 [window makeKeyAndOrderFront: nil];
307 [NSApp activateIgnoringOtherApps: YES];
313 void gui_main_loop( gboolean run )
316 cocoa_gui_autorun = YES;
318 cocoa_gui_is_running = YES;
320 cocoa_gui_is_running = NO;
323 void gui_update_state(void)
328 gboolean gui_error_dialog( const char *msg, ... )
330 if( cocoa_gui_is_running ) {
331 NSString *error_string;
335 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
336 NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
344 void gui_update_io_activity( io_activity_type io, gboolean active )
350 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
353 NSAutoreleasePool *pool;
355 cocoa_gui_nanos += nanosecs;
356 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
357 cocoa_gui_nanos -= GUI_TICK_PERIOD;
359 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
361 // Run the event loop
362 pool = [NSAutoreleasePool new];
363 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
364 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
365 [NSApp sendEvent: event];
370 gettimeofday(&tv,NULL);
371 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
372 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
373 if( (ns * 1.05) < current_period ) {
374 // We've gotten ahead - sleep for a little bit
377 tv.tv_nsec = current_period - ns;
381 /* Update the display every 10 ticks (ie 10 times a second) and
382 * save the current tv value */
383 if( cocoa_gui_ticks > 10 ) {
385 cocoa_gui_ticks -= 10;
387 double speed = (float)( (double)current_period * 100.0 / ns );
388 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
389 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
390 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
391 [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
398 void cocoa_gui_update( void )
403 void cocoa_gui_start( void )
405 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
406 [win setRunning: YES];
408 gettimeofday(&cocoa_gui_lasttv,NULL);
411 void cocoa_gui_stop( void )
413 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
414 [win setRunning: NO];
418 * Queue a dreamcast_run() to execute after the currently event(s)
420 void cocoa_gui_run_later( void )
422 [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate)
423 target: [NSApp delegate] argument: nil order: 1
424 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
427 /*************************** Convenience methods ***************************/
429 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
431 NSBitmapImageRep *rep =
432 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
433 pixelsWide: buffer->width pixelsHigh: buffer->height
434 bitsPerSample: 8 samplesPerPixel: 3
435 hasAlpha: NO isPlanar: NO
436 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
437 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
439 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
440 [image addRepresentation: rep];
445 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
447 NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
448 [label setStringValue: text];
449 [label setBordered: NO];
450 [label setDrawsBackground: NO];
451 [label setEditable: NO];
452 [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
453 if( parent != NULL ) {
454 [parent addSubview: label];
.