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 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
40 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
48 * Count of running nanoseconds - used to cut back on the GUI runtime
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;
61 gboolean cocoa_gui_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *user_data )
63 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
64 LxdreamMainWindow *window = (LxdreamMainWindow *)user_data;
71 * Produces the menu title by looking the text up in gettext, removing any
72 * underscores, and returning the result as an NSString.
74 static NSString *NSMENU_( const char *text )
76 const char *s = gettext(text);
77 char buf[strlen(s)+1];
88 return [NSString stringWithUTF8String: buf];
91 static void cocoa_gui_create_menu(void)
94 NSMenu *appleMenu, *services;
100 appleMenu = [[NSMenu alloc] initWithTitle:@""];
103 title = [@"About " stringByAppendingString:appName];
104 [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
106 [appleMenu addItem:[NSMenuItem separatorItem]];
107 [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
110 [appleMenu addItem:[NSMenuItem separatorItem]];
111 services = [[[NSMenu alloc] init] autorelease];
112 [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
113 [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
116 title = [@"Hide " stringByAppendingString:appName];
117 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
120 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
121 action:@selector(hideOtherApplications:)
123 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
126 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
127 [appleMenu addItem:[NSMenuItem separatorItem]];
130 title = [@"Quit " stringByAppendingString:appName];
131 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
133 /* Put menu into the menubar */
134 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
135 [menuItem setSubmenu: appleMenu];
136 NSMenu *menu = [NSMenu new];
137 [menu addItem: menuItem];
139 NSMenu *gdromMenu = cocoa_gdrom_menu_new();
141 NSMenu *quickStateMenu = [[NSMenu alloc] initWithTitle:NSMENU_("_Quick State")];
142 int quickState = dreamcast_get_quick_state();
143 for( i=0; i<=MAX_QUICK_STATE; i++ ) {
144 NSString *label = [NSString stringWithFormat: NSMENU_("State _%d"), i];
145 NSString *keyEquiv = [NSString stringWithFormat: @"%d", i];
146 menuItem = [[NSMenuItem alloc] initWithTitle: label action: @selector(quick_state_action:) keyEquivalent: keyEquiv];
147 [menuItem setTag: i];
148 if( i == quickState ) {
149 [menuItem setState:NSOnState];
151 [quickStateMenu addItem: menuItem];
154 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
155 [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
156 [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
157 setSubmenu: gdromMenu];
158 [fileMenu addItem: [NSMenuItem separatorItem]];
159 [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
160 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
161 [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
162 [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
163 [fileMenu addItem: [NSMenuItem separatorItem]];
164 [fileMenu addItemWithTitle: NSMENU_("L_oad State...") action: @selector(load_action:) keyEquivalent: @"o"];
165 [fileMenu addItemWithTitle: NSMENU_("S_ave State...") action: @selector(save_action:) keyEquivalent: @"a"];
166 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("Select _Quick State") action: nil keyEquivalent: @""];
167 [fileMenu addItem: [NSMenuItem separatorItem]];
168 [fileMenu addItemWithTitle: NSMENU_("_Load Quick State") action: @selector(quick_load_action:) keyEquivalent: @"l"];
169 [fileMenu addItemWithTitle: NSMENU_("_Save Quick State") action: @selector(quick_save_action:) keyEquivalent: @"s"];
170 [menuItem setSubmenu: quickStateMenu];
171 [fileMenu addItem: menuItem];
172 [fileMenu addItem: [NSMenuItem separatorItem]];
173 [fileMenu addItemWithTitle: NSMENU_("_Full Screen...") action: @selector(fullscreen_action:) keyEquivalent: @"\r"];
175 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
176 [menuItem setSubmenu: fileMenu];
177 [menu addItem: menuItem];
179 /* Tell the application object that this is now the application menu */
180 [NSApp setMainMenu: menu];
181 [NSApp setAppleMenu: appleMenu];
182 [NSApp setServicesMenu: services];
184 /* Finally give up our references to the objects */
190 @interface LxdreamDelegate : NSObject
193 @implementation LxdreamDelegate
194 - (void)windowWillClose: (NSNotification *)notice
196 dreamcast_shutdown();
199 - (void)windowDidBecomeMain: (NSNotification *)notice
201 if( cocoa_gui_autorun ) {
202 cocoa_gui_autorun = NO;
203 gui_do_later(dreamcast_run);
206 - (void)windowDidBecomeKey: (NSNotification *)notice
208 display_set_focused( TRUE );
210 - (void)windowDidResignKey: (NSNotification *)notice
212 display_set_focused( FALSE );
213 [mainWindow setIsGrabbed: NO];
215 - (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
217 const gchar *cname = [filename UTF8String];
218 if( file_load_magic(cname) ) {
219 // Queue up a run event
220 gui_do_later(dreamcast_run);
227 - (void) about_action: (id)sender
229 NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
230 NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright), nil];
232 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
234 [NSApp orderFrontStandardAboutPanelWithOptions: options];
236 - (void) preferences_action: (id)sender
238 cocoa_gui_show_preferences();
240 - (void) load_action: (id)sender
242 NSOpenPanel *panel = [NSOpenPanel openPanel];
243 NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_SAVE_PATH)];
244 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
245 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
246 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
247 NSString *filename = [[panel filenames] objectAtIndex: 0];
248 dreamcast_load_state( [filename UTF8String] );
249 gui_set_configurable_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
252 - (void) save_action: (id)sender
254 NSSavePanel *panel = [NSSavePanel savePanel];
255 NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_SAVE_PATH)];
256 [panel setRequiredFileType: @"dst"];
257 int result = [panel runModalForDirectory: path file:@""];
258 if( result == NSOKButton ) {
259 NSString *filename = [panel filename];
260 dreamcast_save_state( [filename UTF8String] );
261 gui_set_configurable_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
264 - (void) load_binary_action: (id)sender
266 NSOpenPanel *panel = [NSOpenPanel openPanel];
267 NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_DEFAULT_PATH)];
268 int result = [panel runModalForDirectory: path file: nil types: nil];
269 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
270 NSString *filename = [[panel filenames] objectAtIndex: 0];
271 file_load_magic( [filename UTF8String] );
272 gui_set_configurable_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
275 - (void) mount_action: (id)sender
277 NSOpenPanel *panel = [NSOpenPanel openPanel];
278 NSString *path = [NSString stringWithCString: gui_get_configurable_path(CONFIG_DEFAULT_PATH)];
279 int result = [panel runModalForDirectory: path file: nil types: nil];
280 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
281 NSString *filename = [[panel filenames] objectAtIndex: 0];
282 gdrom_mount_image( [filename UTF8String] );
283 gui_set_configurable_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
286 - (void) pause_action: (id)sender
291 - (void) reset_action: (id)sender
295 - (void) run_action: (id)sender
297 if( !dreamcast_is_running() ) {
298 gui_do_later(dreamcast_run);
301 - (void) gdrom_list_action: (id)sender
303 gdrom_list_set_selection( [sender tag] );
305 - (void) fullscreen_action: (id)sender
307 [mainWindow setFullscreen: ![mainWindow isFullscreen]];
309 - (void) quick_state_action: (id)sender
311 [[[sender menu] itemWithTag: dreamcast_get_quick_state()] setState: NSOffState ];
312 [sender setState: NSOnState ];
313 dreamcast_set_quick_state( [sender tag] );
315 - (void) quick_save_action: (id)sender
317 dreamcast_quick_save();
319 - (void) quick_load_action: (id)sender
321 dreamcast_quick_load();
326 gboolean gui_parse_cmdline( int *argc, char **argv[] )
328 /* If started from the finder, the first (and only) arg will look something like
329 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
332 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
338 gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
340 dreamcast_register_module( &cocoa_gui_module );
342 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
343 [NSApplication sharedApplication];
345 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
346 [NSApp setDelegate: delegate];
347 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"lxdream" ofType:@"png"];
348 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
349 [iconImage setName: @"NSApplicationIcon"];
350 [NSApp setApplicationIconImage: iconImage];
351 cocoa_gui_create_menu();
352 mainWindow = cocoa_gui_create_main_window();
353 [mainWindow makeKeyAndOrderFront: nil];
354 [NSApp activateIgnoringOtherApps: YES];
356 register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
357 if( withFullscreen ) {
358 [mainWindow setFullscreen: YES];
364 void gui_main_loop( gboolean run )
367 cocoa_gui_autorun = YES;
369 cocoa_gui_is_running = YES;
371 cocoa_gui_is_running = NO;
374 void gui_update_state(void)
379 void gui_set_use_grab( gboolean grab )
381 [mainWindow setUseGrab: (grab ? YES : NO)];
384 gboolean gui_error_dialog( const char *msg, ... )
386 if( cocoa_gui_is_running ) {
387 NSString *error_string;
391 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
392 NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
400 void gui_update_io_activity( io_activity_type io, gboolean active )
406 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
409 NSAutoreleasePool *pool;
411 cocoa_gui_nanos += nanosecs;
412 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
413 cocoa_gui_nanos -= GUI_TICK_PERIOD;
415 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
417 // Run the event loop
418 pool = [NSAutoreleasePool new];
419 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
420 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
421 [NSApp sendEvent: event];
426 gettimeofday(&tv,NULL);
427 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
428 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
429 if( (ns * 1.05) < current_period ) {
430 // We've gotten ahead - sleep for a little bit
433 tv.tv_nsec = current_period - ns;
437 /* Update the display every 10 ticks (ie 10 times a second) and
438 * save the current tv value */
439 if( cocoa_gui_ticks > 10 ) {
441 cocoa_gui_ticks -= 10;
443 double speed = (float)( (double)current_period * 100.0 / ns );
444 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
445 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
446 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
447 [mainWindow setStatusText: buf];
454 void cocoa_gui_update( void )
459 void cocoa_gui_start( void )
461 [mainWindow setRunning: YES];
463 gettimeofday(&cocoa_gui_lasttv,NULL);
466 void cocoa_gui_stop( void )
468 [mainWindow setRunning: NO];
471 @interface DoLaterStub : NSObject
473 do_later_callback_t func;
477 @implementation DoLaterStub
478 - (id) init: (do_later_callback_t)f
491 * Queue a dreamcast_run() to execute after the currently event(s)
493 void gui_do_later( do_later_callback_t func )
495 DoLaterStub *stub = [[[DoLaterStub alloc] init: func] autorelease];
496 [[NSRunLoop currentRunLoop] performSelector: @selector(do)
497 target: stub argument: nil order: 1
498 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
501 /*************************** Convenience methods ***************************/
503 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
505 NSBitmapImageRep *rep =
506 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
507 pixelsWide: buffer->width pixelsHigh: buffer->height
508 bitsPerSample: 8 samplesPerPixel: 3
509 hasAlpha: NO isPlanar: NO
510 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
511 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
513 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
514 [image addRepresentation: rep];
519 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
521 NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
522 [label setStringValue: text];
523 [label setBordered: NO];
524 [label setDrawsBackground: NO];
525 [label setEditable: NO];
526 [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
527 if( parent != NULL ) {
528 [parent addSubview: label];
.