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;
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)
93 NSMenu *appleMenu, *services;
99 appleMenu = [[NSMenu alloc] initWithTitle:@""];
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:@","];
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"]];
115 title = [@"Hide " stringByAppendingString:appName];
116 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
119 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
120 action:@selector(hideOtherApplications:)
122 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
125 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
126 [appleMenu addItem:[NSMenuItem separatorItem]];
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"];
153 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
154 [menuItem setSubmenu: fileMenu];
155 [menu addItem: menuItem];
157 /* Tell the application object that this is now the application menu */
158 [NSApp setMainMenu: menu];
159 [NSApp setAppleMenu: appleMenu];
160 [NSApp setServicesMenu: services];
162 /* Finally give up our references to the objects */
168 @interface LxdreamDelegate : NSObject
171 @implementation LxdreamDelegate
172 - (void)windowWillClose: (NSNotification *)notice
174 dreamcast_shutdown();
177 - (void)windowDidBecomeMain: (NSNotification *)notice
179 if( cocoa_gui_autorun ) {
180 cocoa_gui_autorun = NO;
181 cocoa_gui_run_later();
184 - (void)windowDidBecomeKey: (NSNotification *)notice
186 display_set_focused( TRUE );
188 - (void)windowDidResignKey: (NSNotification *)notice
190 display_set_focused( FALSE );
191 [((LxdreamMainWindow *)[NSApp mainWindow]) setIsGrabbed: NO];
193 - (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
195 const gchar *cname = [filename UTF8String];
196 if( file_load_magic(cname) ) {
197 // Queue up a run event
198 cocoa_gui_run_later();
205 - (void) about_action: (id)sender
207 NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
208 NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright), nil];
210 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
212 [NSApp orderFrontStandardAboutPanelWithOptions: options];
214 - (void) preferences_action: (id)sender
216 cocoa_gui_show_preferences();
218 - (void) load_action: (id)sender
220 NSOpenPanel *panel = [NSOpenPanel openPanel];
221 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
222 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
223 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
224 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
225 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
226 NSString *filename = [[panel filenames] objectAtIndex: 0];
227 dreamcast_load_state( [filename UTF8String] );
230 - (void) save_action: (id)sender
232 NSSavePanel *panel = [NSSavePanel savePanel];
233 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
234 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
235 [panel setRequiredFileType: @"dst"];
236 int result = [panel runModalForDirectory: path file:@""];
237 if( result == NSOKButton ) {
238 NSString *filename = [panel filename];
239 dreamcast_save_state( [filename UTF8String] );
242 - (void) load_binary_action: (id)sender
244 NSOpenPanel *panel = [NSOpenPanel openPanel];
245 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
246 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
247 int result = [panel runModalForDirectory: path file: nil types: nil];
248 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
249 NSString *filename = [[panel filenames] objectAtIndex: 0];
250 file_load_magic( [filename UTF8String] );
253 - (void) mount_action: (id)sender
255 NSOpenPanel *panel = [NSOpenPanel openPanel];
256 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
257 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
258 int result = [panel runModalForDirectory: path file: nil types: nil];
259 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
260 NSString *filename = [[panel filenames] objectAtIndex: 0];
261 gdrom_mount_image( [filename UTF8String] );
264 - (void) pause_action: (id)sender
269 - (void) reset_action: (id)sender
273 - (void) run_action: (id)sender
275 cocoa_gui_run_later();
277 - (void) run_immediate
281 - (void) gdrom_list_action: (id)sender
283 gdrom_list_set_selection( [sender tag] );
285 - (BOOL)validateMenuItem: (NSMenuItem *)item
287 if( [item action] == @selector(run_action:) ) {
288 return dreamcast_can_run() ? YES : NO;
293 - (BOOL)validateToolbarItem: (NSToolbarItem *)item
295 if( [item action] == @selector(run_action:) ) {
296 return dreamcast_can_run() ? YES : NO;
304 gboolean gui_parse_cmdline( int *argc, char **argv[] )
306 /* If started from the finder, the first (and only) arg will look something like
307 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
310 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
316 gboolean gui_init( gboolean withDebug )
318 dreamcast_register_module( &cocoa_gui_module );
320 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
321 [NSApplication sharedApplication];
323 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
324 [NSApp setDelegate: delegate];
325 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
326 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
327 [iconImage setName: @"NSApplicationIcon"];
328 [NSApp setApplicationIconImage: iconImage];
329 cocoa_gui_create_menu();
330 NSWindow *window = cocoa_gui_create_main_window();
331 [window makeKeyAndOrderFront: nil];
332 [NSApp activateIgnoringOtherApps: YES];
334 register_gdrom_disc_change_hook( cocoa_gui_disc_changed, window );
340 void gui_main_loop( gboolean run )
343 cocoa_gui_autorun = YES;
345 cocoa_gui_is_running = YES;
347 cocoa_gui_is_running = NO;
350 void gui_update_state(void)
355 void gui_set_use_grab( gboolean grab )
357 [((LxdreamMainWindow *)[NSApp mainWindow]) setUseGrab: (grab ? YES : NO)];
360 gboolean gui_error_dialog( const char *msg, ... )
362 if( cocoa_gui_is_running ) {
363 NSString *error_string;
367 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
368 NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
376 void gui_update_io_activity( io_activity_type io, gboolean active )
382 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
385 NSAutoreleasePool *pool;
387 cocoa_gui_nanos += nanosecs;
388 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
389 cocoa_gui_nanos -= GUI_TICK_PERIOD;
391 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
393 // Run the event loop
394 pool = [NSAutoreleasePool new];
395 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
396 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
397 [NSApp sendEvent: event];
402 gettimeofday(&tv,NULL);
403 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
404 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
405 if( (ns * 1.05) < current_period ) {
406 // We've gotten ahead - sleep for a little bit
409 tv.tv_nsec = current_period - ns;
413 /* Update the display every 10 ticks (ie 10 times a second) and
414 * save the current tv value */
415 if( cocoa_gui_ticks > 10 ) {
417 cocoa_gui_ticks -= 10;
419 double speed = (float)( (double)current_period * 100.0 / ns );
420 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
421 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
422 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
423 [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
430 void cocoa_gui_update( void )
435 void cocoa_gui_start( void )
437 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
438 [win setRunning: YES];
440 gettimeofday(&cocoa_gui_lasttv,NULL);
443 void cocoa_gui_stop( void )
445 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
446 [win setRunning: NO];
450 * Queue a dreamcast_run() to execute after the currently event(s)
452 void cocoa_gui_run_later( void )
454 [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate)
455 target: [NSApp delegate] argument: nil order: 1
456 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
459 /*************************** Convenience methods ***************************/
461 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
463 NSBitmapImageRep *rep =
464 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
465 pixelsWide: buffer->width pixelsHigh: buffer->height
466 bitsPerSample: 8 samplesPerPixel: 3
467 hasAlpha: NO isPlanar: NO
468 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
469 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
471 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
472 [image addRepresentation: rep];
477 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
479 NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
480 [label setStringValue: text];
481 [label setBordered: NO];
482 [label setDrawsBackground: NO];
483 [label setEditable: NO];
484 [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
485 if( parent != NULL ) {
486 [parent addSubview: label];
.